Mercurial > hg-old > index.cgi
annotate lwasm/lwasm.h @ 198:6ddc861a07d4
Allow export to import undefined symbols
author | lost |
---|---|
date | Sun, 22 Mar 2009 18:26:59 +0000 |
parents | 563adfccb645 |
children | 048ebb85f6ef |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 lwasm.h |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
3 Copyright © 2008 William Astle |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
20 Contains the main defs used by the assembler |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
21 */ |
0 | 22 |
23 | |
24 #ifndef __lwasm_h_seen__ | |
25 #define __lwasm_h_seen__ | |
26 | |
58 | 27 #include <stdio.h> |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
28 #include "expr.h" |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
29 |
0 | 30 #define OUTPUT_DECB 0 // DECB multirecord format |
31 #define OUTPUT_RAW 1 // raw sequence of bytes | |
21 | 32 #define OUTPUT_OBJ 2 // proprietary object file format |
46 | 33 #define OUTPUT_RAWREL 3 // raw bytes where ORG causes a SEEK in the file |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
34 |
74 | 35 // structure for tracking sections |
86 | 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 | |
91
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
90
diff
changeset
|
41 int context; // symbol context (for local syms) |
86 | 42 section_reloc_list_t *next; // next relocation |
43 }; | |
44 | |
90 | 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 | |
74 | 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 | |
86 | 61 // the following are used during code output |
85 | 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? | |
86 | 65 section_reloc_list_t *rl; // relocation list |
90 | 66 export_list_t *exports; // export list for the section |
74 | 67 }; |
68 | |
57 | 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 | |
26 | 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 | |
21 | 87 // structure for keeping track of lines |
77 | 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 | |
21 | 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 | |
26 | 99 lwasm_error_t *err; // error messages |
28 | 100 int fsize; // forced size (0 = no forced size) |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
101 char *sym; // scratch area to record the presence of a symbol |
42 | 102 unsigned char *bytes; // actual bytes emitted |
103 int codelen; // number of bytes emitted | |
104 int codesize; // the size of the code buffer | |
44 | 105 int codeaddr; // address the code goes at |
46 | 106 int nocodelen; // for "RMB" type instructions |
49
21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
lost
parents:
46
diff
changeset
|
107 int addrset; // set if this instruction sets the assembly address |
21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
lost
parents:
46
diff
changeset
|
108 int symaddr; // set if this instruction sets a symbol addr with EQU or the like |
67 | 109 int badop; // bad operation - ignore it |
91
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
90
diff
changeset
|
110 int context; // the symbol context for this line |
190 | 111 int forceglobal; // force a "global" symbol definition if constant |
74 | 112 |
113 // the following are used for obj format - for external references, inter-section | |
114 // references, and intrasection relocations | |
115 int relocoff; // offset into insn where relocation value goes | |
77 | 116 lwasm_expr_stack_t *exprs[4]; // non-constant expression values |
117 int exprvals[4]; // constant expression values | |
118 char *exprends[4]; // pointer to character after end of expression | |
85 | 119 |
120 sectiontab_t *sect; // which section is the line in? | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
121 }; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
122 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
123 // for keeping track of symbols |
75
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
124 #define SYMBOL_SET 1 // the symbol was used for "SET" |
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
125 #define SYMBOL_COMPLEX 2 // register symbol as a complex symbol (from l -> expr) |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
126 #define SYMBOL_FORCE 4 // force resetting the symbol value if it already exists on pass 2 |
75
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
127 #define SYMBOL_NORM 0 // no flags |
82 | 128 #define SYMBOL_EXTERN 8 // the symbol is an external reference |
190 | 129 #define SYMBOL_GLOBAL 16 // force global if non-complex symbol |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
130 typedef struct lwasm_symbol_ent_s lwasm_symbol_ent_t; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
131 struct lwasm_symbol_ent_s |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
132 { |
75
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
133 char *sym; // the symbol |
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
134 int context; // the context number of the symbol (-1 for global) |
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
135 int value; // the value of the symbol |
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
136 int flags; // flags for the symbol |
82 | 137 char *externalname; // for external references that are aliased locally |
75
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
138 sectiontab_t *sect; // the section the symbol exists in; NULL for none |
92eb93bffa28
Rejigged symbol system to be able to handle non-constant references
lost
parents:
74
diff
changeset
|
139 lwasm_expr_stack_t *expr; // expression for a symbol that is not constant NULL for const |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
140 lwasm_symbol_ent_t *next; // next symbol in the table |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
141 lwasm_symbol_ent_t *prev; // previous symbol in the table |
21 | 142 }; |
0 | 143 |
144 // keep track of current assembler state | |
145 typedef struct { | |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
146 int dpval; // current dp value (setdp) |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
147 int addr; // current address |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
148 int context; // context counter (for local symbols) |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
149 int errorcount; // error count |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
150 int passnum; // which pass are we on? |
21 | 151 int execaddr; // execution address for the program (END ....) |
152 int pragmas; // what pragmas are in effect? | |
153 | |
154 lwasm_line_t *lineshead; // first line of source code | |
155 lwasm_line_t *linestail; // last line of source code | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
156 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
157 lwasm_symbol_ent_t *symhead; // first entry in symbol table |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
158 lwasm_symbol_ent_t *symtail; // last entry in symbol table |
57 | 159 |
160 macrotab_t *macros; // macro table | |
21 | 161 |
13
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
162 const char *infile; // input file |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
163 const char *outfile; // output file |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
164 const char *listfile; // output listing file |
05d4115b4860
Started work on new expression evaluator system and major code re-work for next release
lost
parents:
4
diff
changeset
|
165 int outformat; // output format type |
21 | 166 char **filelist; // files that have been read |
167 int filelistlen; // number of files in the list | |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
49
diff
changeset
|
168 |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
49
diff
changeset
|
169 int endseen; // set to true if "end" has been seen |
57 | 170 int skipcond; // skipping a condition? |
171 int skipcount; // how many? | |
172 int skipmacro; // skipping a macro? | |
173 int inmacro; // are we currently in a macro? | |
174 int macroex; // current depth of macro expansion | |
175 int nextcontext; // next context number | |
176 int skiplines; // number of lines to skip | |
74 | 177 |
178 // items used only for the "object" target | |
179 sectiontab_t *sections; // pointer to section table | |
180 sectiontab_t *csect; // current section - NULL if not in one | |
0 | 181 } asmstate_t; |
182 | |
143
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
101
diff
changeset
|
183 // do not rewrite XXX,r to ,r if XXX evaluates to 0 |
0 | 184 #define PRAGMA_NOINDEX0TONONE 1 |
143
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
101
diff
changeset
|
185 // any undefined symbols are considered external |
160
b061350c17e4
Added cescapes pragma and a few other compatibility pseudo ops
lost
parents:
151
diff
changeset
|
186 #define PRAGMA_UNDEFEXTERN 2 |
b061350c17e4
Added cescapes pragma and a few other compatibility pseudo ops
lost
parents:
151
diff
changeset
|
187 // allow C-style escapes in fcc, fcs, and fcn directives |
b061350c17e4
Added cescapes pragma and a few other compatibility pseudo ops
lost
parents:
151
diff
changeset
|
188 #define PRAGMA_CESCAPES 4 |
0 | 189 |
198 | 190 // allow "export <undefsym>" to import the symbol |
191 #define PRAGMA_IMPORTUNDEFEXPORT 8 | |
192 | |
26 | 193 #ifndef __lwasm_c_seen__ |
194 #define __lwasm_E__ extern | |
195 #else | |
196 #define __lwasm_E__ | |
197 #endif | |
198 | |
38 | 199 __lwasm_E__ int debug_level; |
200 | |
26 | 201 __lwasm_E__ int register_error(asmstate_t *as, lwasm_line_t *l, int pass, const char *fmt, ...); |
38 | 202 __lwasm_E__ void debug_message(int level, const char *fmt, ...); |
26 | 203 |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
204 __lwasm_E__ void lwasm_emit(asmstate_t *as, lwasm_line_t *l, int b); |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
205 __lwasm_E__ void lwasm_emitop(asmstate_t *as, lwasm_line_t *l, int o); |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
206 __lwasm_E__ int lwasm_lookupreg2(const char *reglist, char **str); |
32 | 207 __lwasm_E__ int lwasm_lookupreg3(const char *rlist, const char **str); |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
208 |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
209 __lwasm_E__ lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp, int flags); |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
210 |
55 | 211 |
57 | 212 // return next context number and update it |
213 __lwasm_E__ int lwasm_next_context(asmstate_t *as); | |
214 | |
55 | 215 // also throw an error on expression eval failure |
76 | 216 // return 0 on ok, -1 on error, 1 if a complex expression was returned |
55 | 217 #define EXPR_NOFLAG 0 |
76 | 218 #define EXPR_PASS1CONST 1 // no forward references on pass 1 |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
219 #define EXPR_SECTCONST 2 // resolve symbols local to section |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
220 #define EXPR_REEVAL 4 // re-evaluate the expression |
55 | 221 __lwasm_E__ int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val); |
77 | 222 __lwasm_E__ int lwasm_expr_result2(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val, int slot); |
55 | 223 |
26 | 224 #undef __lwasm_E__ |
225 | |
226 | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
227 #ifndef __symbol_c_seen__ |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
228 #define __lwasm_E__ extern |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
229 #else |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
230 #define __lwasm_E__ |
0 | 231 #endif |
232 | |
64 | 233 __lwasm_E__ int lwasm_register_symbol(asmstate_t *as, lwasm_line_t *l, char *sym, int val, int flags); |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
234 __lwasm_E__ lwasm_symbol_ent_t *lwasm_find_symbol(asmstate_t *as, char *sym, int scontext); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
235 __lwasm_E__ int lwasm_set_symbol(asmstate_t *as, char *sym, int scontext, int val); |
58 | 236 __lwasm_E__ void lwasm_list_symbols(asmstate_t *as, FILE *f); |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
237 #undef __lwasm_E__ |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
238 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
239 |
0 | 240 |
241 #endif //__lwasm_h_seen__ |