OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [asmdata.h] - Blame information for rev 126

Details | Compare with Previous | View Log

Line No. Rev Author Line
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 69 dgisselq
//              Gisselquist Technology, LLC
17 13 dgisselq
//
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 126 dgisselq
        // New multiplies
61
        OP_MPY, OP_MPYSHI, OP_MPYUHI,
62 69 dgisselq
        // New bit-wise operations
63
        OP_BREV, OP_POPC,
64
        // New divide instruction
65
        OP_DIVU, OP_DIVS,
66
        // New floating point instructions
67
        OP_FPADD, OP_FPSUB, OP_FPMUL, OP_FPDIV, OP_FPCVT, OP_FPINT,
68 13 dgisselq
        // Memory operands/operators
69
        OP_LOD, OP_STO,
70
        // Dual operand instructions that do not take conditions
71
        OP_LDI,
72
        // Single operand instructions that can take conditions
73 97 dgisselq
        OP_CLRF, OP_JMP, OP_NOT,
74 13 dgisselq
        // Branch operands
75 69 dgisselq
        OP_BRA, OP_BLT, OP_BZ, OP_BNZ, OP_BGT, OP_BGE, OP_BRC, OP_BRV,
76 13 dgisselq
        // Single operand instructions that have no explicit conditions
77 26 dgisselq
        OP_CLR, OP_TRAP, OP_NEG,
78 13 dgisselq
        // BAREOPs that can have conditions
79 46 dgisselq
        OP_HALT,  OP_RTU, OP_BUSY, OP_RETN,
80 13 dgisselq
        // BAREOPs without conditions
81 97 dgisselq
        OP_BREAK, OP_NOOP, OP_LOCK, OP_LJMP,
82 13 dgisselq
        // Error condition--undefined operand
83
        OP_NONE
84
} LEXOPCODE;
85
 
86 69 dgisselq
#define DEFAULT_LINE    0x76000000
87 13 dgisselq
 
88
class   ASMLINE {
89
public:
90
        char    m_state;
91 26 dgisselq
        int     m_lineno;
92 13 dgisselq
        virtual bool    isdefined(void) { return true; };
93
        virtual ~ASMLINE(void) {};
94
        virtual int     nlines(void) { return 0; }
95
        virtual unsigned int    eval(const int lno) { return DEFAULT_LINE; }
96
        virtual void    dump(FILE *fp) = 0;
97
};
98
 
99
class   AST { // The actual expression tree
100
public:
101
        // node type is one of:
102
        // 'B' a branch with two sides
103
        // 'N' a number
104
        // 'I' an identifier
105
        // 'A' ?? an address ?? would we need this?
106 46 dgisselq
        virtual ~AST(void) {}
107 13 dgisselq
        char    m_node_type;
108
        virtual bool    isdefined(void) = 0;
109
        virtual int     eval(void) = 0;
110
        virtual void    reduce(void) = 0;
111
        virtual void    dump(FILE *fp) = 0;
112
};
113
 
114
/*
115
class   ULINE : public ASMLINE  { // Unprocessed line of assembly
116
public:
117
        std::string     m_file, m_text;
118
        int             m_lineno;
119
        ULINE(const int line, const std::string file,
120
                const std::string text) : m_file(file), m_text(text),
121
                                        m_lineno(line) { m_state = 'U'; }
122
        class TLINE     *eval(void);
123
        int             nlines(void) { return 0; };
124
        virtual bool    isdefined(void) { return false; };
125
};
126
*/
127
 
128
class   ILINE : public ASMLINE { // Instruction line
129
public:
130
        ZIPI    m_in;
131 26 dgisselq
        ILINE(const ZIPI in) : m_in(in) { m_state = 'I'; m_lineno = yylineno; };
132 13 dgisselq
        virtual bool    isdefined(void) { return true; };
133
        virtual int     nlines(void) { return 1; }
134
        virtual unsigned int    eval(const int lno);
135
        void    dump(FILE *fp) { fprintf(fp, "IN: %08x\n", m_in); }
136
};
137
 
138
class   VLINE : public ASMLINE { // Void line
139
public:
140 26 dgisselq
        VLINE(void) { m_state = 'V'; m_lineno = yylineno; };
141 13 dgisselq
        virtual bool    isdefined(void) { return true; };
142
        virtual int     nlines(void) { return 0; }
143
        virtual unsigned int    eval(const int lno);
144
        void    dump(FILE *fp) { fprintf(fp, "void\n"); }
145
};
146
 
147
class   DLINE : public ASMLINE { // Data line
148
public:
149
        ZIPI    m_data;
150 26 dgisselq
        DLINE(const ZIPI dat) : m_data(dat) { m_state = 'D'; m_lineno = yylineno; };
151 13 dgisselq
        virtual bool    isdefined(void) { return true; };
152
        virtual int     nlines(void) { return 1; }
153
        virtual unsigned int    eval(const int lno);
154
        void    dump(FILE *fp) { fprintf(fp, "\tWORD\t%08d\n", m_data); }
155
};
156
 
157
class   LLINE : public ASMLINE { // List line -- one line that has turned into
158
                // many
159
public:
160
        int     m_nlines;
161
        ASMLINE **m_lines;
162 26 dgisselq
        LLINE(void) : m_nlines(0), m_lines(NULL) { m_state = 'L'; m_lineno = yylineno; };
163 13 dgisselq
        void addline(ASMLINE *line) ;
164
        virtual bool    isdefined(void);
165
        ~LLINE(void);
166
        virtual int     nlines(void) { return m_nlines; }
167
        virtual unsigned int    eval(const int lno);
168
        void    dump(FILE *fp) {
169
                for(int i=0; i<m_nlines; i++)
170
                        m_lines[i]->dump(fp);
171
        }
172
};
173
 
174
class   TLINE : public ASMLINE { // Expression tree
175
public:
176
        LEXOPCODE               m_opcode;
177
        ZPARSER::ZIPCOND        m_cond;
178
        AST                     *m_imm;
179
        ZPARSER::ZIPREG         m_opb, m_opa;
180
 
181
        TLINE(void) : m_opcode(OP_NONE), m_cond(ZPARSER::ZIPC_ALWAYS),
182
                        m_imm(NULL), m_opa(ZPARSER::ZIP_Rnone), m_opb(ZPARSER::ZIP_Rnone)
183 26 dgisselq
                        { m_state = 'T'; m_lineno = yylineno; };
184 13 dgisselq
        virtual bool    isdefined(void) {
185
                if (m_imm != NULL) {
186
                        bool answer = m_imm->isdefined();
187
                        return answer;
188
                } else return true;
189
        }
190
        ASMLINE *eval(void);
191
        ~TLINE(void) { if (m_imm) delete m_imm; }
192
        virtual int     nlines(void); //  { return 1; }
193
        virtual unsigned int    eval(const int lno);
194
        void    dump(FILE *fp);
195
};
196
 
197
class   AST_BRANCH : public AST { // The actual expression tree
198
public:
199
        int     m_op; // An operator
200
        AST     *m_left, *m_right;
201
 
202
        AST_BRANCH(int op, AST *l, AST *r)
203
                : m_op(op), m_left(l), m_right(r) { m_node_type = 'B';
204
                }
205 46 dgisselq
        ~AST_BRANCH(void) {
206
                if (m_left)
207
                        delete  m_left;
208
                if (m_right)
209
                        delete  m_right;
210
        }
211 13 dgisselq
 
212
        bool    isdefined(void) {
213
                return ((m_left)&&(m_right)
214
                                &&(m_left->isdefined())
215
                                &&(m_right->isdefined()));
216
        }
217 46 dgisselq
 
218 13 dgisselq
        int     eval(void);
219
        void    reduce(void);
220
        void    dump(FILE *fp) {
221
                fprintf(fp, "(");
222
                m_left->dump(fp);
223
                fprintf(fp, ") %c (", m_node_type);
224
                m_right->dump(fp);
225
                fprintf(fp, ")");
226
        }
227
};
228
 
229
class   AST_NUMBER : public AST { // A number at the base of the tree
230
public:
231
        int     m_val;
232
        AST_NUMBER(int val) : m_val(val) { m_node_type = 'N'; }
233
        bool    isdefined(void) { return true; }
234
        int     eval(void);
235
        void    reduce(void);
236
        void    dump(FILE *fp) { fprintf(fp, "%d", m_val); }
237
};
238
 
239
class   AST_IDENTIFIER : public AST { // An identifier within the expression tree
240
public:
241
        std::string     m_id;
242
        AST_IDENTIFIER(const char *id) : m_id(id) { m_node_type = 'I'; }
243
        AST_IDENTIFIER(AST *ida, const char *idb);
244
        bool    isdefined(void);
245
        int     eval(void);
246
        void    reduce(void);
247
        void    dump(FILE *fp) { fprintf(fp, "%s", m_id.c_str()); }
248
};
249
 
250
class   AST_LABEL : public AST { // An address label within the expression tree
251
        // This is special, because it cannot be evaluated without knowing
252
        // the PC value at this address
253
public:
254
        std::string     m_label;
255
        AST_LABEL(const char *id) : m_label(id) { m_node_type = 'L'; }
256
        bool    isdefined(void);
257
        int     eval(void);
258
        void    reduce(void);
259
        void    dump(FILE *fp) { fprintf(fp, "%s", m_label.c_str()); }
260
};
261
 
262
class   SAVED_ASMLINE {
263
public:
264
        unsigned int    m_pc;
265
        ASMLINE         *m_ln;
266
        SAVED_ASMLINE(const unsigned int pc, ASMLINE *ln) : m_pc(pc), m_ln(ln)
267
                {}
268
};
269
 
270
class   OBJFILE {
271
        typedef std::list<SAVED_ASMLINE> SVDTBL;
272
 
273
        SVDTBL          m_tbl;
274
        unsigned int    m_pc;
275
        FILE            *m_fp;
276
 
277
public:
278
        OBJFILE(void) { m_fp = NULL; m_pc = 0; }
279
        void open(const char *fname);
280 26 dgisselq
        void close(void) { fclose(m_fp); };
281 13 dgisselq
        unsigned int pc(void) { return m_pc; }
282
        void    operator+=(ASMLINE *ln);
283
        bool    reduce(void);
284
};
285
 
286
// The file we are building
287
extern  OBJFILE objcode;
288
 
289
// Functions for accessing and defining elements in our symbol table
290
extern  bool    stb_isdefined(const char *key);
291
extern  int     stb_value(const char *key);
292
extern  void    stb_define(const char *key, AST *value);
293
extern  void    gbl_define(const char *key, AST *value);
294
extern  bool    stb_isdefined(const std::string &key); //  { return stb_isdefined(key.c_str()); }
295
extern  int     stb_value(const std::string &key); //  { return stb_value(key.c_str()); }
296
extern  void    stb_define(const std::string &key, AST *value); //  { return stb_define(key.c_str(), value); }
297
extern  void    gbl_define(const std::string &key, AST *value); //  { return stb_define(key.c_str(), value); }
298
extern  void    create_new_context(void); // Create a new symbol table context
299
 
300
 
301
#endif // ASMDATA_H

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.