annotate lwasm/output.c @ 267:bec6d2480f08 2.6

Added add'l generated files for release
author lost
date Tue, 22 Dec 2009 05:33:32 +0000
parents f9f01a499525
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
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
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
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 */
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 204
diff changeset
23 #include <config.h>
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
24 //#include <ctype.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
25 #include <errno.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
26 #include <stdio.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
27 //#include <stdlib.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
28 #include <string.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
29 #include <unistd.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30 #define __output_c_seen__
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
31 //#include "instab.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
32 #include "lwasm.h"
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
33 #include "util.h"
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
34
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
35 void write_code_raw(asmstate_t *as, FILE *of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
36 void write_code_decb(asmstate_t *as, FILE *of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
37 void write_code_rawrel(asmstate_t *as, FILE *of);
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
38 void write_code_obj(asmstate_t *as, FILE *of);
236
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
39 void write_code_os9(asmstate_t *as, FILE *of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
40
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
41 // 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
42 #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
43
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
44 void lwasm_output(asmstate_t *as)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
45 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
46 FILE *of;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
48 if (as -> errorcount > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
49 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
50 fprintf(stderr, "Not doing output due to assembly errors.\n");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
51 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54 of = fopen(as -> outfile, "wb");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
55 if (!of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
56 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57 fprintf(stderr, "Cannot open '%s' for output", as -> outfile);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
58 perror("");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
60 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
62 switch (as -> outformat)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
63 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
64 case OUTPUT_RAW:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
65 write_code_raw(as, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
66 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
67
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
68 case OUTPUT_DECB:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
69 write_code_decb(as, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
70 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
71
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
72 case OUTPUT_RAWREL:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
73 write_code_rawrel(as, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
74 break;
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
75
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
76 case OUTPUT_OBJ:
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
77 write_code_obj(as, of);
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
78 break;
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
79
236
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
80 case OUTPUT_OS9:
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
81 write_code_os9(as, of);
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
82 break;
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
83
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
84 default:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
85 fprintf(stderr, "BUG: unrecognized output format when generating output file\n");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
86 fclose(of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
87 unlink(as -> outfile);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
88 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
89 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
90
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
91 fclose(of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
92 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
93
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
94 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
95 rawrel output treats an ORG directive as an offset from the start of the
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
96 file. Undefined results will occur if an ORG directive moves the output
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
97 pointer backward. This particular implementation uses "fseek" to handle
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
98 ORG requests and to skip over RMBs.
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
99
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
100 This simple brain damanged method simply does an fseek before outputting
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
101 each instruction.
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
102 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
103 void write_code_rawrel(asmstate_t *as, FILE *of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
104 {
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
105 lwasm_line_t *cl;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
106
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
107 for (cl = as -> lineshead; cl; cl = cl -> next)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
108 {
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
109 if (cl -> codelen == 0)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
110 continue;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
111
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
112 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
113 writebytes(cl -> bytes, cl -> codelen, 1, of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
114 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
115 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
116
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
117 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
118 raw merely writes all the bytes directly to the file as is. ORG is just a
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
119 reference for the assembler to handle absolute references. Multiple ORG
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
120 statements will produce mostly useless results
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
121 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
122 void write_code_raw(asmstate_t *as, FILE *of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
123 {
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
124 lwasm_line_t *cl;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
125
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
126 for (cl = as -> lineshead; cl; cl = cl -> next)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
127 {
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
128 if (cl -> nocodelen)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
129 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
130 int i;
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
131 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
132 writebytes("\0", 1, 1, of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
133 continue;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
134 }
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
135 writebytes(cl -> bytes, cl -> codelen, 1, of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
136 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
137 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
138
236
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
139
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
140 /*
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
141 OS9 target also just writes all the bytes in order. No need for anything
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
142 else.
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
143 */
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
144 void write_code_os9(asmstate_t *as, FILE *of)
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
145 {
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
146 lwasm_line_t *cl;
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
147
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
148 for (cl = as -> lineshead; cl; cl = cl -> next)
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
149 {
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
150 if (cl -> inmod == 0)
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
151 continue;
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
152 if (cl -> nocodelen)
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
153 {
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
154 int i;
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
155 for (i = 0; i < cl -> nocodelen; i++)
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
156 writebytes("\0", 1, 1, of);
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
157 continue;
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
158 }
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
159 writebytes(cl -> bytes, cl -> codelen, 1, of);
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
160 }
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
161 }
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 212
diff changeset
162
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
163 void write_code_decb(asmstate_t *as, FILE *of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
164 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
165 long preambloc;
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
166 lwasm_line_t *cl;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
167 int blocklen = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
168 int nextcalc = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
169 unsigned char outbuf[5];
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
170
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
171 for (cl = as -> lineshead; cl; cl = cl -> next)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
172 {
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
173 if (cl -> nocodelen)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
174 continue;
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
175 if (cl -> codeaddr != nextcalc && cl -> codelen > 0)
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
176 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
177 // need preamble here
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
178 if (blocklen > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
179 {
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
180 // update previous preamble if needed
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
181 fseek(of, preambloc, SEEK_SET);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
182 outbuf[0] = (blocklen >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
183 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
184 writebytes(outbuf, 2, 1, of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
185 fseek(of, 0, SEEK_END);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
186 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
187 blocklen = 0;
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
188 nextcalc = cl -> codeaddr;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
189 outbuf[0] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
190 outbuf[1] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
191 outbuf[2] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
192 outbuf[3] = (nextcalc >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
193 outbuf[4] = nextcalc & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
194 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
195 writebytes(outbuf, 5, 1, of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
196 }
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
197 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
198 writebytes(cl -> bytes, cl -> codelen, 1, of);
46
b962cee20bf4 Ported output modules forward from old version
lost
parents: 4
diff changeset
199 blocklen += cl -> codelen;
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
200 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
201 if (blocklen > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
202 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
203 fseek(of, preambloc, SEEK_SET);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
204 outbuf[0] = (blocklen >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
205 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
206 writebytes(outbuf, 2, 1, of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
207 fseek(of, 0, SEEK_END);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
208 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
209
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
210 // now write postamble
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
211 outbuf[0] = 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
212 outbuf[1] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
213 outbuf[2] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
214 outbuf[3] = (as -> execaddr >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
215 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
216 writebytes(outbuf, 5, 1, of);
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
217 }
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
218
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
219 void write_code_obj_sbadd(sectiontab_t *s, unsigned char b)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
220 {
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
221 if (s -> oblen >= s -> obsize)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
222 {
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
223 s -> obytes = lwasm_realloc(s -> obytes, s -> obsize + 128);
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
224 s -> obsize += 128;
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
225 }
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
226 s -> obytes[s -> oblen] = b;
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
227 s -> oblen += 1;
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
228 }
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
229
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
230 void write_code_obj(asmstate_t *as, FILE *of)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
231 {
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
232 lwasm_line_t *l;
87
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
233 sectiontab_t *s;
88
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
234 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
235 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
236 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
237 lwasm_expr_stack_node_t *sn;
88
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
238
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
239 int i;
87
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
240 unsigned char buf[16];
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
241
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
242 // output the magic number and file header
87
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
243 // the 8 is NOT an error
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
244 writebytes("LWOBJ16", 8, 1, of);
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
245
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
246 // run through the entire system and build the byte streams for each
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
247 // section; at the same time, generate a list of "local" symbols to
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
248 // output for each section
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
249 // NOTE: for "local" symbols, we will append \x01 and the ascii string
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
250 // of the context identifier (so sym in context 1 would be "sym\x011"
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
251 // we can do this because the linker can handle symbols with any
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
252 // character other than NUL.
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
253 // also we will generate a list of incomplete references for each
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
254 // section along with the actual definition that will be output
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
255
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
256 // once all this information is generated, we will output each section
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
257 // to the file
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
258
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
259 // NOTE: we build everything in memory then output it because the
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
260 // assembler accepts multiple instances of the same section but the
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
261 // linker expects only one instance of each section in the object file
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
262 // so we need to collect all the various pieces of a section together
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
263 // (also, the assembler treated multiple instances of the same section
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
264 // as continuations of previous sections so we would need to collect
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
265 // them together anyway.
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
266
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
267 for (l = as -> lineshead; l; l = l -> next)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
268 {
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
269 if (l -> sect)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
270 {
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
271 // we're in a section - need to output some bytes
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
272 for (i = 0; i < l -> codelen; i++)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
273 write_code_obj_sbadd(l -> sect, l -> bytes[i]);
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
274 for (i = 0; i < l -> nocodelen; i++)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
275 write_code_obj_sbadd(l -> sect, 0);
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
276
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
277 // do we have a "relocation"? If so, add a reference to the
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
278 // relocation table
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
279 if (l -> relocoff >= 0)
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
280 {
86
033a328a10ae Checkpoint: object target output
lost
parents: 85
diff changeset
281 // build the relocation reference for the linker
033a328a10ae Checkpoint: object target output
lost
parents: 85
diff changeset
282 re = lwasm_alloc(sizeof(section_reloc_list_t));
033a328a10ae Checkpoint: object target output
lost
parents: 85
diff changeset
283 re -> next = l -> sect -> rl;
033a328a10ae Checkpoint: object target output
lost
parents: 85
diff changeset
284 l -> sect -> rl = re;
033a328a10ae Checkpoint: object target output
lost
parents: 85
diff changeset
285
033a328a10ae Checkpoint: object target output
lost
parents: 85
diff changeset
286 re -> offset = l -> codeaddr + l -> relocoff;
033a328a10ae Checkpoint: object target output
lost
parents: 85
diff changeset
287 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
288 re -> context = l -> context;
204
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
289 re -> relocsize = 2;
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
290 if (l -> reloc8bit)
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
291 re -> relocsize = 1;
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
292 }
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
293 }
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
294 }
87
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
295
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
296 // run through the sections
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
297 for (s = as -> sections; s; s = s -> next)
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
298 {
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
299 // write the name
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
300 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
301
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
302 // write the flags
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
303 if (s -> flags & SECTION_BSS)
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
304 writebytes("\x01", 1, 1, of);
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
305
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
306 // indicate end of flags - the "" is NOT an error
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
307 writebytes("", 1, 1, of);
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
308
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
309
88
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
310 // now the local symbols
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
311 for (se = as -> symhead; se; se = se -> next)
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
312 {
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
313 // ignore symbols not in this section
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
314 if (se -> sect != s)
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
315 continue;
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
316
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
317 if (se -> flags & SYMBOL_SET)
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
318 continue;
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
319
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
320 if (se -> flags & SYMBOL_EXTERN)
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
321 continue;
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
322
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
323 writebytes(se -> sym, strlen(se -> sym), 1, of);
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
324 if (se -> context >= 0)
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
325 {
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
326 writebytes("\x01", 1, 1, of);
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
327 sprintf(buf, "%d", se -> context);
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
328 writebytes(buf, strlen(buf), 1, of);
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
329 }
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
330 // the "" is NOT an error
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
331 writebytes("", 1, 1, of);
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
332
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
333 // write the address
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
334 buf[0] = (se -> value >> 8) & 0xff;
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
335 buf[1] = se -> value & 0xff;
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
336 writebytes(buf, 2, 1, of);
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
337 }
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
338 // flag end of local symbol table - "" is NOT an error
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
339 writebytes("", 1, 1, of);
6460a1fb5f1f Checkpoint: object format output
lost
parents: 87
diff changeset
340
91
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
341 // 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
342 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
343 {
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
344 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
345 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
346 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
347 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
348 }
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
349
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
350 // 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
351 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
352
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
353 // 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
354 // this being the most complex bit
243
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
355 for (i = 0; i < as -> nextdeps; i++)
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
356 {
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
357 // flag 0-width reference
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
358 writebytes("\xFF\x02", 2, 1, of);
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
359 // write external symbol ref
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
360 writebytes("\x02", 1, 1, of);
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
361 writebytes(as -> extdeps[i], strlen(as -> extdeps[i]) + 1, 1, of);
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
362 // flag end of expr & offset 0 - the 3 is not an error
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
363 writebytes("\0\0", 3, 1, of);
f9f01a499525 Added zero-width external references
lost
parents: 236
diff changeset
364 }
91
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
365 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
366 {
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
367 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
368 {
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
369 // 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
370 // 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
371 continue;
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
372 }
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
373
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
374 // 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
375 // the proper equivalent to the object file
204
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
376 if (re -> relocsize == 1)
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
377 {
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
378 // flag an 8 bit relocation (low 8 bits will be used)
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
379 buf[0] = 0xFF;
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
380 buf[1] = 0x01;
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
381 writebytes(buf, 2, 1, of);
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 151
diff changeset
382 }
91
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
383 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
384 {
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
385 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
386 {
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
387 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
388 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
389 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
390 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
391 break;
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
392
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
393 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
394 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
395 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
396 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
397 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
398 break;
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 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
401 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
402 break;
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
403
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
404 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
405 // 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
406 // 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
407 // 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
408 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
409 if (!se)
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
410 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
411 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
412 {
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
413 // 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
414 // 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
415 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
416 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
417 break;
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
418 }
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
419 // 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
420 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
421 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
422 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
423 {
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
424 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
425 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
426 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
427 }
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
428 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
429 break;
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
430
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
431 default:
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
432 // 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
433 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
434 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
435 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
436 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
437 break;
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
438 }
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
439 }
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
440
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
441 // 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
442 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
443
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
444 // 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
445 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
446 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
447 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
448 }
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 88
diff changeset
449 // 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
450 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
451
87
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
452 // now blast out the code
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
453
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
454 // length
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
455 buf[0] = s -> oblen >> 8 & 0xff;
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
456 buf[1] = s -> oblen & 0xff;
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
457 writebytes(buf, 2, 1, of);
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
458
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
459 if (!(s -> flags & SECTION_BSS))
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
460 {
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
461 writebytes(s -> obytes, s -> oblen, 1, of);
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
462 }
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
463 }
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
464
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
465 // flag no more sections
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
466 // the "" is NOT an error
41ff4686b46b Checkpoint: object format output
lost
parents: 86
diff changeset
467 writebytes("", 1, 1, of);
85
918be0c02239 Started adding object target output
lost
parents: 48
diff changeset
468 }