comparison old-trunk/lwasm/old/lwasm.h @ 339:eb230fa7d28e

Prepare for migration to hg
author lost
date Fri, 19 Mar 2010 02:54:14 +0000
parents
children
comparison
equal deleted inserted replaced
338:e7885b3ee266 339:eb230fa7d28e
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 #define OUTPUT_OS9 4 // os9 module target
35
36 // structure for tracking sections
37 typedef struct section_reloc_list_s section_reloc_list_t;
38 struct section_reloc_list_s
39 {
40 int offset; // offset into section
41 int relocsize; // size of relocation in bytes
42 lwasm_expr_stack_t *expr; // value definition
43 int context; // symbol context (for local syms)
44 section_reloc_list_t *next; // next relocation
45 };
46
47 typedef struct export_list_s export_list_t;
48 struct export_list_s
49 {
50 int offset; // offset of symbol
51 char *sym; // name of symbol
52 export_list_t *next; // next export
53 };
54
55 #define SECTION_BSS 1 // the section contains no actual code - just uninit vars
56 typedef struct sectiontab_s sectiontab_t;
57 struct sectiontab_s
58 {
59 char *name; // name of the section
60 int offset; // current offset in the section
61 int flags; // section flags
62 sectiontab_t *next; // next section
63 // the following are used during code output
64 unsigned char *obytes; // output bytes
65 int oblen; // how many bytes output so far?
66 int obsize; // how big is output buffer so far?
67 section_reloc_list_t *rl; // relocation list
68 export_list_t *exports; // export list for the section
69 };
70
71 // structure for tracking macros
72 typedef struct macrotab_s macrotab_t;
73 struct macrotab_s
74 {
75 char *name;
76 char **lines;
77 int numlines;
78 macrotab_t *next;
79 };
80
81 // structure for tracking errors
82 typedef struct lwasm_error_s lwasm_error_t;
83 struct lwasm_error_s
84 {
85 char *mess; // the actual error message
86 lwasm_error_t *next; // ptr to next error
87 };
88
89 // structure for keeping track of lines
90 // it also as space for 4 expressions which is enough for all known
91 // instructions and addressing modes
92 // on pass 1, the expressions are parsed, on pass 2 they are re-evaluated
93 // to determine constancy
94 typedef struct lwasm_line_s lwasm_line_t;
95 struct lwasm_line_s {
96 char *text; // the actual text of the line
97 int lineno; // line number within the file
98 char *filename; // file name reference
99 lwasm_line_t *next; // next line
100 lwasm_line_t *prev; // previous line
101 lwasm_error_t *err; // error messages
102 int fsize; // forced size (0 = no forced size)
103 char *sym; // scratch area to record the presence of a symbol
104 unsigned char *bytes; // actual bytes emitted
105 int codelen; // number of bytes emitted
106 int codesize; // the size of the code buffer
107 int codeaddr; // address the code goes at
108 int nocodelen; // for "RMB" type instructions
109 int addrset; // set if this instruction sets the assembly address
110 int symaddr; // set if this instruction sets a symbol addr with EQU or the like
111 int badop; // bad operation - ignore it
112 int context; // the symbol context for this line
113 int forceglobal; // force a "global" symbol definition if constant
114
115 // the following are used for obj format - for external references, inter-section
116 // references, and intrasection relocations
117 int relocoff; // offset into insn where relocation value goes
118 int reloc8bit; // size of relocation (0 = 16 bit, 1 = 8 bit)
119 lwasm_expr_stack_t *exprs[4]; // non-constant expression values
120 int exprvals[4]; // constant expression values
121 char *exprends[4]; // pointer to character after end of expression
122 int inmod; // in an os9 module
123
124 sectiontab_t *sect; // which section is the line in?
125 };
126
127 // for keeping track of symbols
128 #define SYMBOL_SET 1 // the symbol was used for "SET"
129 #define SYMBOL_COMPLEX 2 // register symbol as a complex symbol (from l -> expr)
130 #define SYMBOL_FORCE 4 // force resetting the symbol value if it already exists on pass 2
131 #define SYMBOL_NORM 0 // no flags
132 #define SYMBOL_EXTERN 8 // the symbol is an external reference
133 #define SYMBOL_GLOBAL 16 // force global if non-complex symbol
134 typedef struct lwasm_symbol_ent_s lwasm_symbol_ent_t;
135 struct lwasm_symbol_ent_s
136 {
137 char *sym; // the symbol
138 int context; // the context number of the symbol (-1 for global)
139 int value; // the value of the symbol
140 int flags; // flags for the symbol
141 char *externalname; // for external references that are aliased locally
142 sectiontab_t *sect; // the section the symbol exists in; NULL for none
143 lwasm_expr_stack_t *expr; // expression for a symbol that is not constant NULL for const
144 lwasm_symbol_ent_t *next; // next symbol in the table
145 lwasm_symbol_ent_t *prev; // previous symbol in the table
146 };
147
148 // keep track of current assembler state
149 typedef struct {
150 int dpval; // current dp value (setdp)
151 int addr; // current address
152 int context; // context counter (for local symbols)
153 int errorcount; // error count
154 int passnum; // which pass are we on?
155 int execaddr; // execution address for the program (END ....)
156 int pragmas; // what pragmas are in effect?
157
158 lwasm_line_t *lineshead; // first line of source code
159 lwasm_line_t *linestail; // last line of source code
160
161 lwasm_symbol_ent_t *symhead; // first entry in symbol table
162 lwasm_symbol_ent_t *symtail; // last entry in symbol table
163
164 macrotab_t *macros; // macro table
165
166 const char *infile; // input file
167 const char *outfile; // output file
168 const char *listfile; // output listing file
169 int outformat; // output format type
170 char **filelist; // files that have been read
171 int filelistlen; // number of files in the list
172
173 int endseen; // set to true if "end" has been seen
174 int skipcond; // skipping a condition?
175 int skipcount; // how many?
176 int skipmacro; // skipping a macro?
177 int inmacro; // are we currently in a macro?
178 int macroex; // current depth of macro expansion
179 int nextcontext; // next context number
180 int skiplines; // number of lines to skip
181
182 // items used only for the "object" target
183 sectiontab_t *sections; // pointer to section table
184 sectiontab_t *csect; // current section - NULL if not in one
185
186 // only 6809 ops?
187 int no6309;
188
189 // for os9 mode
190 int inmod; // in a module?
191 unsigned char crc[3]; // running crc count
192 } asmstate_t;
193
194 // do not rewrite XXX,r to ,r if XXX evaluates to 0
195 #define PRAGMA_NOINDEX0TONONE 1
196 // any undefined symbols are considered external
197 #define PRAGMA_UNDEFEXTERN 2
198 // allow C-style escapes in fcc, fcs, and fcn directives
199 #define PRAGMA_CESCAPES 4
200 // allow "export <undefsym>" to import the symbol
201 #define PRAGMA_IMPORTUNDEFEXPORT 8
202 // don't have $ as a local symbol
203 #define PRAGMA_DOLLARNOTLOCAL 16
204
205 #ifndef __lwasm_c_seen__
206 #define __lwasm_E__ extern
207 #else
208 #define __lwasm_E__
209 #endif
210
211 __lwasm_E__ int debug_level;
212
213 __lwasm_E__ int register_error(asmstate_t *as, lwasm_line_t *l, int pass, const char *fmt, ...);
214 __lwasm_E__ void debug_message(int level, const char *fmt, ...);
215
216 __lwasm_E__ void lwasm_emit(asmstate_t *as, lwasm_line_t *l, int b);
217 __lwasm_E__ void lwasm_emitop(asmstate_t *as, lwasm_line_t *l, int o);
218 __lwasm_E__ int lwasm_lookupreg2(const char *reglist, char **str);
219 __lwasm_E__ int lwasm_lookupreg3(const char *rlist, const char **str);
220
221 __lwasm_E__ lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp, int flags);
222
223
224 // return next context number and update it
225 __lwasm_E__ int lwasm_next_context(asmstate_t *as);
226
227 // also throw an error on expression eval failure
228 // return 0 on ok, -1 on error, 1 if a complex expression was returned
229 #define EXPR_NOFLAG 0
230 #define EXPR_PASS1CONST 1 // no forward references on pass 1
231 #define EXPR_SECTCONST 2 // resolve symbols local to section
232 #define EXPR_REEVAL 4 // re-evaluate the expression
233 __lwasm_E__ int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val);
234 __lwasm_E__ int lwasm_expr_result2(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val, int slot);
235
236 #undef __lwasm_E__
237
238
239 #ifndef __symbol_c_seen__
240 #define __lwasm_E__ extern
241 #else
242 #define __lwasm_E__
243 #endif
244
245 __lwasm_E__ int lwasm_register_symbol(asmstate_t *as, lwasm_line_t *l, char *sym, int val, int flags);
246 __lwasm_E__ lwasm_symbol_ent_t *lwasm_find_symbol(asmstate_t *as, char *sym, int scontext);
247 __lwasm_E__ int lwasm_set_symbol(asmstate_t *as, char *sym, int scontext, int val);
248 __lwasm_E__ void lwasm_list_symbols(asmstate_t *as, FILE *f);
249 #undef __lwasm_E__
250
251
252 #define skip_operand(p) do { char **p2 = (char **)(p); for ( ; **p2; (*p2)++ ) ; } while (0)
253
254 #endif //__lwasm_h_seen__