1 |
13 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: asmdata.h
|
4 |
|
|
//
|
5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU core
|
6 |
|
|
//
|
7 |
|
|
// Purpose: Data structures for the assembler. In particular, this file
|
8 |
|
|
// declares: Abstract Syntax Trees (ASTs) to store operations for
|
9 |
|
|
// later calculation, ASMLINEs or assembled lines (which may or
|
10 |
|
|
// may not be defined, depending upon symbol table status),
|
11 |
|
|
// symbol table access and the final output object file together
|
12 |
|
|
// with its necessary relocations. Yes, linking is done, but as
|
13 |
|
|
// part of the assembler and not part of a separate linker.
|
14 |
|
|
//
|
15 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
16 |
|
|
// Gisselquist Tecnology, LLC
|
17 |
|
|
//
|
18 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
//
|
20 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
21 |
|
|
//
|
22 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
23 |
|
|
// modify it under the terms of the GNU General Public License as published
|
24 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
25 |
|
|
// your option) any later version.
|
26 |
|
|
//
|
27 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
28 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
29 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
30 |
|
|
// for more details.
|
31 |
|
|
//
|
32 |
|
|
// You should have received a copy of the GNU General Public License along
|
33 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
34 |
|
|
// target there if the PDF file isn't present.) If not, see
|
35 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
36 |
|
|
//
|
37 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
38 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
39 |
|
|
//
|
40 |
|
|
//
|
41 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
42 |
|
|
#ifndef ASMDATA_H
|
43 |
|
|
#define ASMDATA_H
|
44 |
|
|
|
45 |
|
|
#include <stdio.h>
|
46 |
|
|
#include <string>
|
47 |
|
|
#include <list>
|
48 |
|
|
#include "zopcodes.h"
|
49 |
|
|
#include "zparser.h"
|
50 |
|
|
|
51 |
|
|
extern "C" char *linecp;
|
52 |
26 |
dgisselq |
extern int yylineno;
|
53 |
13 |
dgisselq |
|
54 |
|
|
typedef enum {
|
55 |
|
|
// TST OPCND
|
56 |
|
|
// Dual operand instructions that take conditions
|
57 |
26 |
dgisselq |
OP_CMP, OP_TST, OP_MOV, OP_LDIHI, OP_LDILO, OP_MPYU, OP_MPYS, OP_ROL,
|
58 |
13 |
dgisselq |
OP_SUB, OP_AND, OP_ADD, OP_OR, OP_XOR,
|
59 |
|
|
OP_LSL, OP_ASR, OP_LSR,
|
60 |
|
|
// Memory operands/operators
|
61 |
|
|
OP_LOD, OP_STO,
|
62 |
|
|
// Dual operand instructions that do not take conditions
|
63 |
|
|
OP_LDI,
|
64 |
|
|
// Single operand instructions that can take conditions
|
65 |
|
|
OP_CLRF, OP_JMP, OP_LJMP, OP_NOT,
|
66 |
|
|
// Branch operands
|
67 |
|
|
OP_BRA, OP_BZ, OP_BNZ, OP_BGE, OP_BGT, OP_BLT, OP_BRC, OP_BRV,
|
68 |
|
|
// Single operand instructions that have no explicit conditions
|
69 |
26 |
dgisselq |
OP_CLR, OP_TRAP, OP_NEG,
|
70 |
13 |
dgisselq |
// BAREOPs that can have conditions
|
71 |
|
|
OP_HALT, OP_RTU, OP_BUSY,
|
72 |
|
|
// BAREOPs without conditions
|
73 |
|
|
OP_BREAK, OP_NOOP,
|
74 |
|
|
// Error condition--undefined operand
|
75 |
|
|
OP_NONE
|
76 |
|
|
} LEXOPCODE;
|
77 |
|
|
|
78 |
|
|
#define DEFAULT_LINE 0x4e000000
|
79 |
|
|
|
80 |
|
|
class ASMLINE {
|
81 |
|
|
public:
|
82 |
|
|
char m_state;
|
83 |
26 |
dgisselq |
int m_lineno;
|
84 |
13 |
dgisselq |
virtual bool isdefined(void) { return true; };
|
85 |
|
|
virtual ~ASMLINE(void) {};
|
86 |
|
|
virtual int nlines(void) { return 0; }
|
87 |
|
|
virtual unsigned int eval(const int lno) { return DEFAULT_LINE; }
|
88 |
|
|
virtual void dump(FILE *fp) = 0;
|
89 |
|
|
};
|
90 |
|
|
|
91 |
|
|
class AST { // The actual expression tree
|
92 |
|
|
public:
|
93 |
|
|
// node type is one of:
|
94 |
|
|
// 'B' a branch with two sides
|
95 |
|
|
// 'N' a number
|
96 |
|
|
// 'I' an identifier
|
97 |
|
|
// 'A' ?? an address ?? would we need this?
|
98 |
|
|
char m_node_type;
|
99 |
|
|
virtual bool isdefined(void) = 0;
|
100 |
|
|
virtual int eval(void) = 0;
|
101 |
|
|
virtual void reduce(void) = 0;
|
102 |
|
|
virtual void dump(FILE *fp) = 0;
|
103 |
|
|
};
|
104 |
|
|
|
105 |
|
|
/*
|
106 |
|
|
class ULINE : public ASMLINE { // Unprocessed line of assembly
|
107 |
|
|
public:
|
108 |
|
|
std::string m_file, m_text;
|
109 |
|
|
int m_lineno;
|
110 |
|
|
ULINE(const int line, const std::string file,
|
111 |
|
|
const std::string text) : m_file(file), m_text(text),
|
112 |
|
|
m_lineno(line) { m_state = 'U'; }
|
113 |
|
|
class TLINE *eval(void);
|
114 |
|
|
int nlines(void) { return 0; };
|
115 |
|
|
virtual bool isdefined(void) { return false; };
|
116 |
|
|
};
|
117 |
|
|
*/
|
118 |
|
|
|
119 |
|
|
class ILINE : public ASMLINE { // Instruction line
|
120 |
|
|
public:
|
121 |
|
|
ZIPI m_in;
|
122 |
26 |
dgisselq |
ILINE(const ZIPI in) : m_in(in) { m_state = 'I'; m_lineno = yylineno; };
|
123 |
13 |
dgisselq |
virtual bool isdefined(void) { return true; };
|
124 |
|
|
virtual int nlines(void) { return 1; }
|
125 |
|
|
virtual unsigned int eval(const int lno);
|
126 |
|
|
void dump(FILE *fp) { fprintf(fp, "IN: %08x\n", m_in); }
|
127 |
|
|
};
|
128 |
|
|
|
129 |
|
|
class VLINE : public ASMLINE { // Void line
|
130 |
|
|
public:
|
131 |
26 |
dgisselq |
VLINE(void) { m_state = 'V'; m_lineno = yylineno; };
|
132 |
13 |
dgisselq |
virtual bool isdefined(void) { return true; };
|
133 |
|
|
virtual int nlines(void) { return 0; }
|
134 |
|
|
virtual unsigned int eval(const int lno);
|
135 |
|
|
void dump(FILE *fp) { fprintf(fp, "void\n"); }
|
136 |
|
|
};
|
137 |
|
|
|
138 |
|
|
class DLINE : public ASMLINE { // Data line
|
139 |
|
|
public:
|
140 |
|
|
ZIPI m_data;
|
141 |
26 |
dgisselq |
DLINE(const ZIPI dat) : m_data(dat) { m_state = 'D'; m_lineno = yylineno; };
|
142 |
13 |
dgisselq |
virtual bool isdefined(void) { return true; };
|
143 |
|
|
virtual int nlines(void) { return 1; }
|
144 |
|
|
virtual unsigned int eval(const int lno);
|
145 |
|
|
void dump(FILE *fp) { fprintf(fp, "\tWORD\t%08d\n", m_data); }
|
146 |
|
|
};
|
147 |
|
|
|
148 |
|
|
class LLINE : public ASMLINE { // List line -- one line that has turned into
|
149 |
|
|
// many
|
150 |
|
|
public:
|
151 |
|
|
int m_nlines;
|
152 |
|
|
ASMLINE **m_lines;
|
153 |
26 |
dgisselq |
LLINE(void) : m_nlines(0), m_lines(NULL) { m_state = 'L'; m_lineno = yylineno; };
|
154 |
13 |
dgisselq |
void addline(ASMLINE *line) ;
|
155 |
|
|
virtual bool isdefined(void);
|
156 |
|
|
~LLINE(void);
|
157 |
|
|
virtual int nlines(void) { return m_nlines; }
|
158 |
|
|
virtual unsigned int eval(const int lno);
|
159 |
|
|
void dump(FILE *fp) {
|
160 |
|
|
for(int i=0; i<m_nlines; i++)
|
161 |
|
|
m_lines[i]->dump(fp);
|
162 |
|
|
}
|
163 |
|
|
};
|
164 |
|
|
|
165 |
|
|
class TLINE : public ASMLINE { // Expression tree
|
166 |
|
|
public:
|
167 |
|
|
LEXOPCODE m_opcode;
|
168 |
|
|
ZPARSER::ZIPCOND m_cond;
|
169 |
|
|
AST *m_imm;
|
170 |
|
|
ZPARSER::ZIPREG m_opb, m_opa;
|
171 |
|
|
|
172 |
|
|
TLINE(void) : m_opcode(OP_NONE), m_cond(ZPARSER::ZIPC_ALWAYS),
|
173 |
|
|
m_imm(NULL), m_opa(ZPARSER::ZIP_Rnone), m_opb(ZPARSER::ZIP_Rnone)
|
174 |
26 |
dgisselq |
{ m_state = 'T'; m_lineno = yylineno; };
|
175 |
13 |
dgisselq |
virtual bool isdefined(void) {
|
176 |
|
|
if (m_imm != NULL) {
|
177 |
|
|
bool answer = m_imm->isdefined();
|
178 |
|
|
return answer;
|
179 |
|
|
} else return true;
|
180 |
|
|
}
|
181 |
|
|
ASMLINE *eval(void);
|
182 |
|
|
~TLINE(void) { if (m_imm) delete m_imm; }
|
183 |
|
|
virtual int nlines(void); // { return 1; }
|
184 |
|
|
virtual unsigned int eval(const int lno);
|
185 |
|
|
void dump(FILE *fp);
|
186 |
|
|
};
|
187 |
|
|
|
188 |
|
|
class AST_BRANCH : public AST { // The actual expression tree
|
189 |
|
|
public:
|
190 |
|
|
int m_op; // An operator
|
191 |
|
|
AST *m_left, *m_right;
|
192 |
|
|
|
193 |
|
|
AST_BRANCH(int op, AST *l, AST *r)
|
194 |
|
|
: m_op(op), m_left(l), m_right(r) { m_node_type = 'B';
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
bool isdefined(void) {
|
198 |
|
|
return ((m_left)&&(m_right)
|
199 |
|
|
&&(m_left->isdefined())
|
200 |
|
|
&&(m_right->isdefined()));
|
201 |
|
|
}
|
202 |
|
|
~AST_BRANCH(void) { delete m_left; delete m_right; }
|
203 |
|
|
int eval(void);
|
204 |
|
|
void reduce(void);
|
205 |
|
|
void dump(FILE *fp) {
|
206 |
|
|
fprintf(fp, "(");
|
207 |
|
|
m_left->dump(fp);
|
208 |
|
|
fprintf(fp, ") %c (", m_node_type);
|
209 |
|
|
m_right->dump(fp);
|
210 |
|
|
fprintf(fp, ")");
|
211 |
|
|
}
|
212 |
|
|
};
|
213 |
|
|
|
214 |
|
|
class AST_NUMBER : public AST { // A number at the base of the tree
|
215 |
|
|
public:
|
216 |
|
|
int m_val;
|
217 |
|
|
AST_NUMBER(int val) : m_val(val) { m_node_type = 'N'; }
|
218 |
|
|
bool isdefined(void) { return true; }
|
219 |
|
|
int eval(void);
|
220 |
|
|
void reduce(void);
|
221 |
|
|
void dump(FILE *fp) { fprintf(fp, "%d", m_val); }
|
222 |
|
|
};
|
223 |
|
|
|
224 |
|
|
class AST_IDENTIFIER : public AST { // An identifier within the expression tree
|
225 |
|
|
public:
|
226 |
|
|
std::string m_id;
|
227 |
|
|
AST_IDENTIFIER(const char *id) : m_id(id) { m_node_type = 'I'; }
|
228 |
|
|
AST_IDENTIFIER(AST *ida, const char *idb);
|
229 |
|
|
bool isdefined(void);
|
230 |
|
|
int eval(void);
|
231 |
|
|
void reduce(void);
|
232 |
|
|
void dump(FILE *fp) { fprintf(fp, "%s", m_id.c_str()); }
|
233 |
|
|
};
|
234 |
|
|
|
235 |
|
|
class AST_LABEL : public AST { // An address label within the expression tree
|
236 |
|
|
// This is special, because it cannot be evaluated without knowing
|
237 |
|
|
// the PC value at this address
|
238 |
|
|
public:
|
239 |
|
|
std::string m_label;
|
240 |
|
|
AST_LABEL(const char *id) : m_label(id) { m_node_type = 'L'; }
|
241 |
|
|
bool isdefined(void);
|
242 |
|
|
int eval(void);
|
243 |
|
|
void reduce(void);
|
244 |
|
|
void dump(FILE *fp) { fprintf(fp, "%s", m_label.c_str()); }
|
245 |
|
|
};
|
246 |
|
|
|
247 |
|
|
class SAVED_ASMLINE {
|
248 |
|
|
public:
|
249 |
|
|
unsigned int m_pc;
|
250 |
|
|
ASMLINE *m_ln;
|
251 |
|
|
SAVED_ASMLINE(const unsigned int pc, ASMLINE *ln) : m_pc(pc), m_ln(ln)
|
252 |
|
|
{}
|
253 |
|
|
};
|
254 |
|
|
|
255 |
|
|
class OBJFILE {
|
256 |
|
|
typedef std::list<SAVED_ASMLINE> SVDTBL;
|
257 |
|
|
|
258 |
|
|
SVDTBL m_tbl;
|
259 |
|
|
unsigned int m_pc;
|
260 |
|
|
FILE *m_fp;
|
261 |
|
|
|
262 |
|
|
public:
|
263 |
|
|
OBJFILE(void) { m_fp = NULL; m_pc = 0; }
|
264 |
|
|
void open(const char *fname);
|
265 |
26 |
dgisselq |
void close(void) { fclose(m_fp); };
|
266 |
13 |
dgisselq |
unsigned int pc(void) { return m_pc; }
|
267 |
|
|
void operator+=(ASMLINE *ln);
|
268 |
|
|
bool reduce(void);
|
269 |
|
|
};
|
270 |
|
|
|
271 |
|
|
// The file we are building
|
272 |
|
|
extern OBJFILE objcode;
|
273 |
|
|
|
274 |
|
|
// Functions for accessing and defining elements in our symbol table
|
275 |
|
|
extern bool stb_isdefined(const char *key);
|
276 |
|
|
extern int stb_value(const char *key);
|
277 |
|
|
extern void stb_define(const char *key, AST *value);
|
278 |
|
|
extern void gbl_define(const char *key, AST *value);
|
279 |
|
|
extern bool stb_isdefined(const std::string &key); // { return stb_isdefined(key.c_str()); }
|
280 |
|
|
extern int stb_value(const std::string &key); // { return stb_value(key.c_str()); }
|
281 |
|
|
extern void stb_define(const std::string &key, AST *value); // { return stb_define(key.c_str(), value); }
|
282 |
|
|
extern void gbl_define(const std::string &key, AST *value); // { return stb_define(key.c_str(), value); }
|
283 |
|
|
extern void create_new_context(void); // Create a new symbol table context
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
#endif // ASMDATA_H
|