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

Subversion Repositories zipcpu

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

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
//              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 46 dgisselq
        OP_HALT,  OP_RTU, OP_BUSY, OP_RETN,
72 13 dgisselq
        // 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 46 dgisselq
        virtual ~AST(void) {}
99 13 dgisselq
        char    m_node_type;
100
        virtual bool    isdefined(void) = 0;
101
        virtual int     eval(void) = 0;
102
        virtual void    reduce(void) = 0;
103
        virtual void    dump(FILE *fp) = 0;
104
};
105
 
106
/*
107
class   ULINE : public ASMLINE  { // Unprocessed line of assembly
108
public:
109
        std::string     m_file, m_text;
110
        int             m_lineno;
111
        ULINE(const int line, const std::string file,
112
                const std::string text) : m_file(file), m_text(text),
113
                                        m_lineno(line) { m_state = 'U'; }
114
        class TLINE     *eval(void);
115
        int             nlines(void) { return 0; };
116
        virtual bool    isdefined(void) { return false; };
117
};
118
*/
119
 
120
class   ILINE : public ASMLINE { // Instruction line
121
public:
122
        ZIPI    m_in;
123 26 dgisselq
        ILINE(const ZIPI in) : m_in(in) { m_state = 'I'; m_lineno = yylineno; };
124 13 dgisselq
        virtual bool    isdefined(void) { return true; };
125
        virtual int     nlines(void) { return 1; }
126
        virtual unsigned int    eval(const int lno);
127
        void    dump(FILE *fp) { fprintf(fp, "IN: %08x\n", m_in); }
128
};
129
 
130
class   VLINE : public ASMLINE { // Void line
131
public:
132 26 dgisselq
        VLINE(void) { m_state = 'V'; m_lineno = yylineno; };
133 13 dgisselq
        virtual bool    isdefined(void) { return true; };
134
        virtual int     nlines(void) { return 0; }
135
        virtual unsigned int    eval(const int lno);
136
        void    dump(FILE *fp) { fprintf(fp, "void\n"); }
137
};
138
 
139
class   DLINE : public ASMLINE { // Data line
140
public:
141
        ZIPI    m_data;
142 26 dgisselq
        DLINE(const ZIPI dat) : m_data(dat) { m_state = 'D'; m_lineno = yylineno; };
143 13 dgisselq
        virtual bool    isdefined(void) { return true; };
144
        virtual int     nlines(void) { return 1; }
145
        virtual unsigned int    eval(const int lno);
146
        void    dump(FILE *fp) { fprintf(fp, "\tWORD\t%08d\n", m_data); }
147
};
148
 
149
class   LLINE : public ASMLINE { // List line -- one line that has turned into
150
                // many
151
public:
152
        int     m_nlines;
153
        ASMLINE **m_lines;
154 26 dgisselq
        LLINE(void) : m_nlines(0), m_lines(NULL) { m_state = 'L'; m_lineno = yylineno; };
155 13 dgisselq
        void addline(ASMLINE *line) ;
156
        virtual bool    isdefined(void);
157
        ~LLINE(void);
158
        virtual int     nlines(void) { return m_nlines; }
159
        virtual unsigned int    eval(const int lno);
160
        void    dump(FILE *fp) {
161
                for(int i=0; i<m_nlines; i++)
162
                        m_lines[i]->dump(fp);
163
        }
164
};
165
 
166
class   TLINE : public ASMLINE { // Expression tree
167
public:
168
        LEXOPCODE               m_opcode;
169
        ZPARSER::ZIPCOND        m_cond;
170
        AST                     *m_imm;
171
        ZPARSER::ZIPREG         m_opb, m_opa;
172
 
173
        TLINE(void) : m_opcode(OP_NONE), m_cond(ZPARSER::ZIPC_ALWAYS),
174
                        m_imm(NULL), m_opa(ZPARSER::ZIP_Rnone), m_opb(ZPARSER::ZIP_Rnone)
175 26 dgisselq
                        { m_state = 'T'; m_lineno = yylineno; };
176 13 dgisselq
        virtual bool    isdefined(void) {
177
                if (m_imm != NULL) {
178
                        bool answer = m_imm->isdefined();
179
                        return answer;
180
                } else return true;
181
        }
182
        ASMLINE *eval(void);
183
        ~TLINE(void) { if (m_imm) delete m_imm; }
184
        virtual int     nlines(void); //  { return 1; }
185
        virtual unsigned int    eval(const int lno);
186
        void    dump(FILE *fp);
187
};
188
 
189
class   AST_BRANCH : public AST { // The actual expression tree
190
public:
191
        int     m_op; // An operator
192
        AST     *m_left, *m_right;
193
 
194
        AST_BRANCH(int op, AST *l, AST *r)
195
                : m_op(op), m_left(l), m_right(r) { m_node_type = 'B';
196
                }
197 46 dgisselq
        ~AST_BRANCH(void) {
198
                if (m_left)
199
                        delete  m_left;
200
                if (m_right)
201
                        delete  m_right;
202
        }
203 13 dgisselq
 
204
        bool    isdefined(void) {
205
                return ((m_left)&&(m_right)
206
                                &&(m_left->isdefined())
207
                                &&(m_right->isdefined()));
208
        }
209 46 dgisselq
 
210 13 dgisselq
        int     eval(void);
211
        void    reduce(void);
212
        void    dump(FILE *fp) {
213
                fprintf(fp, "(");
214
                m_left->dump(fp);
215
                fprintf(fp, ") %c (", m_node_type);
216
                m_right->dump(fp);
217
                fprintf(fp, ")");
218
        }
219
};
220
 
221
class   AST_NUMBER : public AST { // A number at the base of the tree
222
public:
223
        int     m_val;
224
        AST_NUMBER(int val) : m_val(val) { m_node_type = 'N'; }
225
        bool    isdefined(void) { return true; }
226
        int     eval(void);
227
        void    reduce(void);
228
        void    dump(FILE *fp) { fprintf(fp, "%d", m_val); }
229
};
230
 
231
class   AST_IDENTIFIER : public AST { // An identifier within the expression tree
232
public:
233
        std::string     m_id;
234
        AST_IDENTIFIER(const char *id) : m_id(id) { m_node_type = 'I'; }
235
        AST_IDENTIFIER(AST *ida, const char *idb);
236
        bool    isdefined(void);
237
        int     eval(void);
238
        void    reduce(void);
239
        void    dump(FILE *fp) { fprintf(fp, "%s", m_id.c_str()); }
240
};
241
 
242
class   AST_LABEL : public AST { // An address label within the expression tree
243
        // This is special, because it cannot be evaluated without knowing
244
        // the PC value at this address
245
public:
246
        std::string     m_label;
247
        AST_LABEL(const char *id) : m_label(id) { m_node_type = 'L'; }
248
        bool    isdefined(void);
249
        int     eval(void);
250
        void    reduce(void);
251
        void    dump(FILE *fp) { fprintf(fp, "%s", m_label.c_str()); }
252
};
253
 
254
class   SAVED_ASMLINE {
255
public:
256
        unsigned int    m_pc;
257
        ASMLINE         *m_ln;
258
        SAVED_ASMLINE(const unsigned int pc, ASMLINE *ln) : m_pc(pc), m_ln(ln)
259
                {}
260
};
261
 
262
class   OBJFILE {
263
        typedef std::list<SAVED_ASMLINE> SVDTBL;
264
 
265
        SVDTBL          m_tbl;
266
        unsigned int    m_pc;
267
        FILE            *m_fp;
268
 
269
public:
270
        OBJFILE(void) { m_fp = NULL; m_pc = 0; }
271
        void open(const char *fname);
272 26 dgisselq
        void close(void) { fclose(m_fp); };
273 13 dgisselq
        unsigned int pc(void) { return m_pc; }
274
        void    operator+=(ASMLINE *ln);
275
        bool    reduce(void);
276
};
277
 
278
// The file we are building
279
extern  OBJFILE objcode;
280
 
281
// Functions for accessing and defining elements in our symbol table
282
extern  bool    stb_isdefined(const char *key);
283
extern  int     stb_value(const char *key);
284
extern  void    stb_define(const char *key, AST *value);
285
extern  void    gbl_define(const char *key, AST *value);
286
extern  bool    stb_isdefined(const std::string &key); //  { return stb_isdefined(key.c_str()); }
287
extern  int     stb_value(const std::string &key); //  { return stb_value(key.c_str()); }
288
extern  void    stb_define(const std::string &key, AST *value); //  { return stb_define(key.c_str(), value); }
289
extern  void    gbl_define(const std::string &key, AST *value); //  { return stb_define(key.c_str(), value); }
290
extern  void    create_new_context(void); // Create a new symbol table context
291
 
292
 
293
#endif // ASMDATA_H

powered by: WebSVN 2.1.0

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