comparison lwasm/lwasm.h @ 151:427e268e876b

renamed src to lwasm to better reflect its purpose
author lost
date Fri, 30 Jan 2009 04:01:55 +0000
parents src/lwasm.h@0ee5f65bccf9
children b061350c17e4
comparison
equal deleted inserted replaced
150:f0881c115010 151:427e268e876b
1 /*
2 lwasm.h
3 Copyright © 2008 William Astle
4
5 This file is part of LWASM.
6
7 LWASM is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation, either version 3 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>.
19
20 Contains the main defs used by the assembler
21 */
22
23
24 #ifndef __lwasm_h_seen__
25 #define __lwasm_h_seen__
26
27 #include <stdio.h>
28 #include "expr.h"
29
30 #define OUTPUT_DECB 0 // DECB multirecord format
31 #define OUTPUT_RAW 1 // raw sequence of bytes
32 #define OUTPUT_OBJ 2 // proprietary object file format
33 #define OUTPUT_RAWREL 3 // raw bytes where ORG causes a SEEK in the file
34
35 // structure for tracking sections
36 typedef struct section_reloc_list_s section_reloc_list_t;
37 struct section_reloc_list_s
38 {
39 int offset; // offset into section
40 lwasm_expr_stack_t *expr; // value definition
41 int context; // symbol context (for local syms)
42 section_reloc_list_t *next; // next relocation
43 };
44
45 typedef struct export_list_s export_list_t;
46 struct export_list_s
47 {
48 int offset; // offset of symbol
49 char *sym; // name of symbol
50 export_list_t *next; // next export
51 };
52
53 #define SECTION_BSS 1 // the section contains no actual code - just uninit vars
54 typedef struct sectiontab_s sectiontab_t;
55 struct sectiontab_s
56 {
57 char *name; // name of the section
58 int offset; // current offset in the section
59 int flags; // section flags
60 sectiontab_t *next; // next section
61 // the following are used during code output
62 unsigned char *obytes; // output bytes
63 int oblen; // how many bytes output so far?
64 int obsize; // how big is output buffer so far?
65 section_reloc_list_t *rl; // relocation list
66 export_list_t *exports; // export list for the section
67 };
68
69 // structure for tracking macros
70 typedef struct macrotab_s macrotab_t;
71 struct macrotab_s
72 {
73 char *name;
74 char **lines;
75 int numlines;
76 macrotab_t *next;
77 };
78
79 // structure for tracking errors
80 typedef struct lwasm_error_s lwasm_error_t;
81 struct lwasm_error_s
82 {
83 char *mess; // the actual error message
84 lwasm_error_t *next; // ptr to next error
85 };
86
87 // structure for keeping track of lines
88 // it also as space for 4 expressions which is enough for all known
89 // instructions and addressing modes
90 // on pass 1, the expressions are parsed, on pass 2 they are re-evaluated
91 // to determine constancy
92 typedef struct lwasm_line_s lwasm_line_t;
93 struct lwasm_line_s {
94 char *text; // the actual text of the line
95 int lineno; // line number within the file
96 char *filename; // file name reference
97 lwasm_line_t *next; // next line
98 lwasm_line_t *prev; // previous line
99 lwasm_error_t *err; // error messages
100 int fsize; // forced size (0 = no forced size)
101 char *sym; // scratch area to record the presence of a symbol
102 unsigned char *bytes; // actual bytes emitted
103 int codelen; // number of bytes emitted
104 int codesize; // the size of the code buffer
105 int codeaddr; // address the code goes at
106 int nocodelen; // for "RMB" type instructions
107 int addrset; // set if this instruction sets the assembly address
108 int symaddr; // set if this instruction sets a symbol addr with EQU or the like
109 int badop; // bad operation - ignore it
110 int context; // the symbol context for this line
111
112 // the following are used for obj format - for external references, inter-section
113 // references, and intrasection relocations
114 int relocoff; // offset into insn where relocation value goes
115 lwasm_expr_stack_t *exprs[4]; // non-constant expression values
116 int exprvals[4]; // constant expression values
117 char *exprends[4]; // pointer to character after end of expression
118
119 sectiontab_t *sect; // which section is the line in?
120 };
121
122 // for keeping track of symbols
123 #define SYMBOL_SET 1 // the symbol was used for "SET"
124 #define SYMBOL_COMPLEX 2 // register symbol as a complex symbol (from l -> expr)
125 #define SYMBOL_FORCE 4 // force resetting the symbol value if it already exists on pass 2
126 #define SYMBOL_NORM 0 // no flags
127 #define SYMBOL_EXTERN 8 // the symbol is an external reference
128 typedef struct lwasm_symbol_ent_s lwasm_symbol_ent_t;
129 struct lwasm_symbol_ent_s
130 {
131 char *sym; // the symbol
132 int context; // the context number of the symbol (-1 for global)
133 int value; // the value of the symbol
134 int flags; // flags for the symbol
135 char *externalname; // for external references that are aliased locally
136 sectiontab_t *sect; // the section the symbol exists in; NULL for none
137 lwasm_expr_stack_t *expr; // expression for a symbol that is not constant NULL for const
138 lwasm_symbol_ent_t *next; // next symbol in the table
139 lwasm_symbol_ent_t *prev; // previous symbol in the table
140 };
141
142 // keep track of current assembler state
143 typedef struct {
144 int dpval; // current dp value (setdp)
145 int addr; // current address
146 int context; // context counter (for local symbols)
147 int errorcount; // error count
148 int passnum; // which pass are we on?
149 int execaddr; // execution address for the program (END ....)
150 int pragmas; // what pragmas are in effect?
151
152 lwasm_line_t *lineshead; // first line of source code
153 lwasm_line_t *linestail; // last line of source code
154
155 lwasm_symbol_ent_t *symhead; // first entry in symbol table
156 lwasm_symbol_ent_t *symtail; // last entry in symbol table
157
158 macrotab_t *macros; // macro table
159
160 const char *infile; // input file
161 const char *outfile; // output file
162 const char *listfile; // output listing file
163 int outformat; // output format type
164 char **filelist; // files that have been read
165 int filelistlen; // number of files in the list
166
167 int endseen; // set to true if "end" has been seen
168 int skipcond; // skipping a condition?
169 int skipcount; // how many?
170 int skipmacro; // skipping a macro?
171 int inmacro; // are we currently in a macro?
172 int macroex; // current depth of macro expansion
173 int nextcontext; // next context number
174 int skiplines; // number of lines to skip
175
176 // items used only for the "object" target
177 sectiontab_t *sections; // pointer to section table
178 sectiontab_t *csect; // current section - NULL if not in one
179 } asmstate_t;
180
181 // do not rewrite XXX,r to ,r if XXX evaluates to 0
182 #define PRAGMA_NOINDEX0TONONE 1
183 // any undefined symbols are considered external
184 #define PRAGMA_UNDEFEXTERN 2
185
186 #ifndef __lwasm_c_seen__
187 #define __lwasm_E__ extern
188 #else
189 #define __lwasm_E__
190 #endif
191
192 __lwasm_E__ int debug_level;
193
194 __lwasm_E__ int register_error(asmstate_t *as, lwasm_line_t *l, int pass, const char *fmt, ...);
195 __lwasm_E__ void debug_message(int level, const char *fmt, ...);
196
197 __lwasm_E__ void lwasm_emit(asmstate_t *as, lwasm_line_t *l, int b);
198 __lwasm_E__ void lwasm_emitop(asmstate_t *as, lwasm_line_t *l, int o);
199 __lwasm_E__ int lwasm_lookupreg2(const char *reglist, char **str);
200 __lwasm_E__ int lwasm_lookupreg3(const char *rlist, const char **str);
201
202 __lwasm_E__ lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp, int flags);
203
204
205 // return next context number and update it
206 __lwasm_E__ int lwasm_next_context(asmstate_t *as);
207
208 // also throw an error on expression eval failure
209 // return 0 on ok, -1 on error, 1 if a complex expression was returned
210 #define EXPR_NOFLAG 0
211 #define EXPR_PASS1CONST 1 // no forward references on pass 1
212 #define EXPR_SECTCONST 2 // resolve symbols local to section
213 #define EXPR_REEVAL 4 // re-evaluate the expression
214 __lwasm_E__ int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val);
215 __lwasm_E__ int lwasm_expr_result2(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val, int slot);
216
217 #undef __lwasm_E__
218
219
220 #ifndef __symbol_c_seen__
221 #define __lwasm_E__ extern
222 #else
223 #define __lwasm_E__
224 #endif
225
226 __lwasm_E__ int lwasm_register_symbol(asmstate_t *as, lwasm_line_t *l, char *sym, int val, int flags);
227 __lwasm_E__ lwasm_symbol_ent_t *lwasm_find_symbol(asmstate_t *as, char *sym, int scontext);
228 __lwasm_E__ int lwasm_set_symbol(asmstate_t *as, char *sym, int scontext, int val);
229 __lwasm_E__ void lwasm_list_symbols(asmstate_t *as, FILE *f);
230 #undef __lwasm_E__
231
232
233
234 #endif //__lwasm_h_seen__