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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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