Mercurial > hg-old > index.cgi
annotate src/output.c @ 112:a567dbb3f1d4
command line options
author | lost |
---|---|
date | Sat, 17 Jan 2009 16:55:53 +0000 |
parents | 718998b673ee |
children |
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 output.c |
46 | 3 Copyright © 2009 William Astle |
4
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 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
21 Contains the code for actually outputting the assembled code |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
22 */ |
0 | 23 |
24 //#include <ctype.h> | |
25 #include <errno.h> | |
26 #include <stdio.h> | |
27 //#include <stdlib.h> | |
28 #include <string.h> | |
29 #include <unistd.h> | |
30 #define __output_c_seen__ | |
31 //#include "instab.h" | |
32 #include "lwasm.h" | |
85 | 33 #include "util.h" |
0 | 34 |
35 void write_code_raw(asmstate_t *as, FILE *of); | |
36 void write_code_decb(asmstate_t *as, FILE *of); | |
37 void write_code_rawrel(asmstate_t *as, FILE *of); | |
85 | 38 void write_code_obj(asmstate_t *as, FILE *of); |
0 | 39 |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
40 // this prevents warnings about not using the return value of fwrite() |
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
41 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0) |
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
42 |
46 | 43 void lwasm_output(asmstate_t *as) |
0 | 44 { |
45 FILE *of; | |
46 | |
47 if (as -> errorcount > 0) | |
48 { | |
49 fprintf(stderr, "Not doing output due to assembly errors.\n"); | |
50 return; | |
51 } | |
52 | |
53 of = fopen(as -> outfile, "wb"); | |
54 if (!of) | |
55 { | |
56 fprintf(stderr, "Cannot open '%s' for output", as -> outfile); | |
57 perror(""); | |
58 return; | |
59 } | |
60 | |
61 switch (as -> outformat) | |
62 { | |
63 case OUTPUT_RAW: | |
64 write_code_raw(as, of); | |
65 break; | |
66 | |
67 case OUTPUT_DECB: | |
68 write_code_decb(as, of); | |
69 break; | |
70 | |
71 case OUTPUT_RAWREL: | |
72 write_code_rawrel(as, of); | |
73 break; | |
85 | 74 |
75 case OUTPUT_OBJ: | |
76 write_code_obj(as, of); | |
77 break; | |
78 | |
0 | 79 default: |
80 fprintf(stderr, "BUG: unrecognized output format when generating output file\n"); | |
81 fclose(of); | |
82 unlink(as -> outfile); | |
83 return; | |
84 } | |
85 | |
86 fclose(of); | |
87 } | |
88 | |
89 /* | |
90 rawrel output treats an ORG directive as an offset from the start of the | |
91 file. Undefined results will occur if an ORG directive moves the output | |
92 pointer backward. This particular implementation uses "fseek" to handle | |
93 ORG requests and to skip over RMBs. | |
94 | |
95 This simple brain damanged method simply does an fseek before outputting | |
96 each instruction. | |
97 */ | |
98 void write_code_rawrel(asmstate_t *as, FILE *of) | |
99 { | |
46 | 100 lwasm_line_t *cl; |
0 | 101 |
46 | 102 for (cl = as -> lineshead; cl; cl = cl -> next) |
0 | 103 { |
46 | 104 if (cl -> codelen == 0) |
0 | 105 continue; |
106 | |
46 | 107 fseek(of, cl -> codeaddr, SEEK_SET); |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
108 writebytes(cl -> bytes, cl -> codelen, 1, of); |
0 | 109 } |
110 } | |
111 | |
112 /* | |
113 raw merely writes all the bytes directly to the file as is. ORG is just a | |
114 reference for the assembler to handle absolute references. Multiple ORG | |
115 statements will produce mostly useless results | |
116 */ | |
117 void write_code_raw(asmstate_t *as, FILE *of) | |
118 { | |
46 | 119 lwasm_line_t *cl; |
0 | 120 |
46 | 121 for (cl = as -> lineshead; cl; cl = cl -> next) |
0 | 122 { |
46 | 123 if (cl -> nocodelen) |
0 | 124 { |
125 int i; | |
46 | 126 for (i = 0; i < cl -> nocodelen; i++) |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
127 writebytes("\0", 1, 1, of); |
0 | 128 continue; |
129 } | |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
130 writebytes(cl -> bytes, cl -> codelen, 1, of); |
0 | 131 } |
132 } | |
133 | |
134 void write_code_decb(asmstate_t *as, FILE *of) | |
135 { | |
136 long preambloc; | |
46 | 137 lwasm_line_t *cl; |
0 | 138 int blocklen = -1; |
139 int nextcalc = -1; | |
140 unsigned char outbuf[5]; | |
141 | |
46 | 142 for (cl = as -> lineshead; cl; cl = cl -> next) |
0 | 143 { |
46 | 144 if (cl -> nocodelen) |
0 | 145 continue; |
46 | 146 if (cl -> codeaddr != nextcalc && cl -> codelen > 0) |
0 | 147 { |
148 // need preamble here | |
149 if (blocklen > 0) | |
150 { | |
46 | 151 // update previous preamble if needed |
0 | 152 fseek(of, preambloc, SEEK_SET); |
153 outbuf[0] = (blocklen >> 8) & 0xFF; | |
154 outbuf[1] = blocklen & 0xFF; | |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
155 writebytes(outbuf, 2, 1, of); |
0 | 156 fseek(of, 0, SEEK_END); |
157 } | |
158 blocklen = 0; | |
46 | 159 nextcalc = cl -> codeaddr; |
0 | 160 outbuf[0] = 0x00; |
161 outbuf[1] = 0x00; | |
162 outbuf[2] = 0x00; | |
163 outbuf[3] = (nextcalc >> 8) & 0xFF; | |
164 outbuf[4] = nextcalc & 0xFF; | |
165 preambloc = ftell(of) + 1; | |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
166 writebytes(outbuf, 5, 1, of); |
0 | 167 } |
46 | 168 nextcalc += cl -> codelen; |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
169 writebytes(cl -> bytes, cl -> codelen, 1, of); |
46 | 170 blocklen += cl -> codelen; |
0 | 171 } |
172 if (blocklen > 0) | |
173 { | |
174 fseek(of, preambloc, SEEK_SET); | |
175 outbuf[0] = (blocklen >> 8) & 0xFF; | |
176 outbuf[1] = blocklen & 0xFF; | |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
177 writebytes(outbuf, 2, 1, of); |
0 | 178 fseek(of, 0, SEEK_END); |
179 } | |
180 | |
181 // now write postamble | |
182 outbuf[0] = 0xFF; | |
183 outbuf[1] = 0x00; | |
184 outbuf[2] = 0x00; | |
185 outbuf[3] = (as -> execaddr >> 8) & 0xFF; | |
186 outbuf[4] = (as -> execaddr) & 0xFF; | |
48
6de358e7903f
Cleaned up warnings about the return value of fwrite() being ignored (it still is but now there's no warning)
lost
parents:
46
diff
changeset
|
187 writebytes(outbuf, 5, 1, of); |
0 | 188 } |
85 | 189 |
190 void write_code_obj_sbadd(sectiontab_t *s, unsigned char b) | |
191 { | |
192 if (s -> oblen >= s -> obsize) | |
193 { | |
194 s -> obytes = lwasm_realloc(s -> obytes, s -> obsize + 128); | |
195 s -> obsize += 128; | |
196 } | |
197 s -> obytes[s -> oblen] = b; | |
198 s -> oblen += 1; | |
199 } | |
200 | |
201 void write_code_obj(asmstate_t *as, FILE *of) | |
202 { | |
203 lwasm_line_t *l; | |
87 | 204 sectiontab_t *s; |
88 | 205 lwasm_symbol_ent_t *se; |
91
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
206 export_list_t *ex; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
207 section_reloc_list_t *re; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
208 lwasm_expr_stack_node_t *sn; |
88 | 209 |
85 | 210 int i; |
87 | 211 unsigned char buf[16]; |
85 | 212 |
213 // output the magic number and file header | |
87 | 214 // the 8 is NOT an error |
215 writebytes("LWOBJ16", 8, 1, of); | |
85 | 216 |
217 // run through the entire system and build the byte streams for each | |
218 // section; at the same time, generate a list of "local" symbols to | |
219 // output for each section | |
220 // NOTE: for "local" symbols, we will append \x01 and the ascii string | |
221 // of the context identifier (so sym in context 1 would be "sym\x011" | |
222 // we can do this because the linker can handle symbols with any | |
223 // character other than NUL. | |
224 // also we will generate a list of incomplete references for each | |
225 // section along with the actual definition that will be output | |
226 | |
227 // once all this information is generated, we will output each section | |
228 // to the file | |
229 | |
230 // NOTE: we build everything in memory then output it because the | |
231 // assembler accepts multiple instances of the same section but the | |
232 // linker expects only one instance of each section in the object file | |
233 // so we need to collect all the various pieces of a section together | |
234 // (also, the assembler treated multiple instances of the same section | |
235 // as continuations of previous sections so we would need to collect | |
236 // them together anyway. | |
237 | |
238 for (l = as -> lineshead; l; l = l -> next) | |
239 { | |
240 if (l -> sect) | |
241 { | |
242 // we're in a section - need to output some bytes | |
243 for (i = 0; i < l -> codelen; i++) | |
244 write_code_obj_sbadd(l -> sect, l -> bytes[i]); | |
245 for (i = 0; i < l -> nocodelen; i++) | |
246 write_code_obj_sbadd(l -> sect, 0); | |
247 | |
248 // do we have a "relocation"? If so, add a reference to the | |
249 // relocation table | |
250 if (l -> relocoff >= 0) | |
251 { | |
86 | 252 // build the relocation reference for the linker |
253 re = lwasm_alloc(sizeof(section_reloc_list_t)); | |
254 re -> next = l -> sect -> rl; | |
255 l -> sect -> rl = re; | |
256 | |
257 re -> offset = l -> codeaddr + l -> relocoff; | |
258 re -> expr = l -> exprs[0]; | |
91
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
259 re -> context = l -> context; |
85 | 260 } |
261 } | |
262 } | |
87 | 263 |
264 // run through the sections | |
265 for (s = as -> sections; s; s = s -> next) | |
266 { | |
267 // write the name | |
268 writebytes(s -> name, strlen(s -> name) + 1, 1, of); | |
269 | |
270 // write the flags | |
271 if (s -> flags & SECTION_BSS) | |
272 writebytes("\x01", 1, 1, of); | |
273 | |
274 // indicate end of flags - the "" is NOT an error | |
275 writebytes("", 1, 1, of); | |
276 | |
277 | |
88 | 278 // now the local symbols |
279 for (se = as -> symhead; se; se = se -> next) | |
280 { | |
281 // ignore symbols not in this section | |
282 if (se -> sect != s) | |
283 continue; | |
284 | |
285 if (se -> flags & SYMBOL_SET) | |
286 continue; | |
287 | |
288 if (se -> flags & SYMBOL_EXTERN) | |
289 continue; | |
290 | |
291 writebytes(se -> sym, strlen(se -> sym), 1, of); | |
292 if (se -> context >= 0) | |
293 { | |
294 writebytes("\x01", 1, 1, of); | |
295 sprintf(buf, "%d", se -> context); | |
296 writebytes(buf, strlen(buf), 1, of); | |
297 } | |
298 // the "" is NOT an error | |
299 writebytes("", 1, 1, of); | |
300 | |
301 // write the address | |
302 buf[0] = (se -> value >> 8) & 0xff; | |
303 buf[1] = se -> value & 0xff; | |
304 writebytes(buf, 2, 1, of); | |
305 } | |
306 // flag end of local symbol table - "" is NOT an error | |
307 writebytes("", 1, 1, of); | |
308 | |
91
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
309 // now the exports |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
310 for (ex = s -> exports; ex; ex = ex -> next) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
311 { |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
312 writebytes(ex -> sym, strlen(ex -> sym) + 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
313 buf[0] = (ex -> offset >> 8) & 0xff; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
314 buf[1] = ex -> offset & 0xff; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
315 writebytes(buf, 2, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
316 } |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
317 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
318 // flag end of exported symbols - "" is NOT an error |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
319 writebytes("", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
320 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
321 // now output the "incomplete references" |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
322 // this being the most complex bit |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
323 for (re = s -> rl; re; re = re -> next) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
324 { |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
325 if (re -> expr == NULL) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
326 { |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
327 // this is an error but we'll simply ignore it |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
328 // and not output this expression |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
329 continue; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
330 } |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
331 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
332 // work through each term in the expression and output |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
333 // the proper equivalent to the object file |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
334 for (sn = re -> expr -> head; sn; sn = sn -> next) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
335 { |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
336 switch (sn -> term -> term_type) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
337 { |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
338 case LWASM_TERM_OPER: |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
339 buf[0] = 0x04; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
340 buf[1] = sn -> term -> value; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
341 writebytes(buf, 2, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
342 break; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
343 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
344 case LWASM_TERM_INT: |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
345 buf[0] = 0x01; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
346 buf[1] = (sn -> term -> value >> 8) & 0xff; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
347 buf[2] = sn -> term -> value & 0xff; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
348 writebytes(buf, 3, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
349 break; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
350 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
351 case LWASM_TERM_SECBASE: |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
352 writebytes("\x05", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
353 break; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
354 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
355 case LWASM_TERM_SYM: |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
356 // now for the ugly part - resolve a symbol reference |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
357 // and determine whether it's internal, external, or |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
358 // a section base |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
359 se = lwasm_find_symbol(as, sn -> term -> symbol, re -> context); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
360 if (!se) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
361 se = lwasm_find_symbol(as, sn -> term -> symbol, -1); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
362 if (!se || se -> flags & SYMBOL_EXTERN) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
363 { |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
364 // not found - assume external reference |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
365 // found but flagged external - handle it |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
366 writebytes("\x02", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
367 writebytes(se -> sym, strlen(se -> sym) + 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
368 break; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
369 } |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
370 // a local symbol reference here |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
371 writebytes("\x03", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
372 writebytes(se -> sym, strlen(se -> sym), 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
373 if (se -> context >= 0) |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
374 { |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
375 writebytes("\x01", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
376 sprintf(buf, "%d", se -> context); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
377 writebytes(buf, strlen(buf), 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
378 } |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
379 writebytes("", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
380 break; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
381 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
382 default: |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
383 // unrecognized term type - replace with integer 0 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
384 buf[0] = 0x01; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
385 buf[1] = 0x00; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
386 buf[2] = 0x00; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
387 writebytes(buf, 3, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
388 break; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
389 } |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
390 } |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
391 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
392 // flag end of expressions |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
393 writebytes("", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
394 |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
395 // write the offset |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
396 buf[0] = (re -> offset >> 8) & 0xff; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
397 buf[1] = re -> offset & 0xff; |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
398 writebytes(buf, 2, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
399 } |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
400 // flag end of incomplete references list |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
401 writebytes("", 1, 1, of); |
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
88
diff
changeset
|
402 |
87 | 403 // now blast out the code |
404 | |
405 // length | |
406 buf[0] = s -> oblen >> 8 & 0xff; | |
407 buf[1] = s -> oblen & 0xff; | |
408 writebytes(buf, 2, 1, of); | |
409 | |
410 if (!(s -> flags & SECTION_BSS)) | |
411 { | |
412 writebytes(s -> obytes, s -> oblen, 1, of); | |
413 } | |
414 } | |
415 | |
416 // flag no more sections | |
417 // the "" is NOT an error | |
418 writebytes("", 1, 1, of); | |
85 | 419 } |