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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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