annotate lwasm/output.c @ 423:079d43b6967b 3.0-beta2 3.0-beta2

Added missing file
author lost@l-w.ca
date Sun, 19 Sep 2010 01:44:47 -0600
parents 35a0b086bf4a
children 5cccf90bf838
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 output.c
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
3 Copyright © 2009, 2010 William Astle
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 Contains the code for actually outputting the assembled code
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24 #include <errno.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <string.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <unistd.h>
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
28
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
29 #include <lw_alloc.h>
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
30 #include <lw_expr.h>
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
31
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 #include "lwasm.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 void write_code_raw(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 void write_code_decb(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 void write_code_rawrel(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 void write_code_obj(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 void write_code_os9(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 // this prevents warnings about not using the return value of fwrite()
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
43 void do_output(asmstate_t *as)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 FILE *of;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 if (as -> errorcount > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 fprintf(stderr, "Not doing output due to assembly errors.\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
53 of = fopen(as -> output_file, "wb");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 if (!of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
56 fprintf(stderr, "Cannot open '%s' for output", as -> output_file);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
61 switch (as -> output_format)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 case OUTPUT_RAW:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 write_code_raw(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 case OUTPUT_DECB:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 write_code_decb(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 case OUTPUT_RAWREL:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 write_code_rawrel(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 case OUTPUT_OBJ:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 write_code_obj(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 case OUTPUT_OS9:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 write_code_os9(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 default:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 fprintf(stderr, "BUG: unrecognized output format when generating output file\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 fclose(of);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
86 unlink(as -> output_file);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 fclose(of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 rawrel output treats an ORG directive as an offset from the start of the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 file. Undefined results will occur if an ORG directive moves the output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 pointer backward. This particular implementation uses "fseek" to handle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 ORG requests and to skip over RMBs.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 This simple brain damanged method simply does an fseek before outputting
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 each instruction.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 void write_code_rawrel(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
104 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
106 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
108 if (cl -> outputl <= 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
111 fseek(of, lw_expr_intval(cl -> addr), SEEK_SET);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
112 writebytes(cl -> output, cl -> outputl, 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 raw merely writes all the bytes directly to the file as is. ORG is just a
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 reference for the assembler to handle absolute references. Multiple ORG
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 statements will produce mostly useless results
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 void write_code_raw(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
123 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
125 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
127 if (cl -> len > 0 && cl -> outputl == 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 int i;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
130 for (i = 0; i < cl -> len; i++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 writebytes("\0", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
134 else if (cl -> outputl > 0)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
135 writebytes(cl -> output, cl -> outputl, 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 OS9 target also just writes all the bytes in order. No need for anything
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 else.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 void write_code_os9(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
146 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
148 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 if (cl -> inmod == 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 continue;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
152 if (cl -> len > 0 && cl -> outputl == 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 int i;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
155 for (i = 0; i < cl -> len; i++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 writebytes("\0", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
159 else if (cl -> outputl > 0)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
160 writebytes(cl -> output, cl -> outputl, 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 void write_code_decb(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166 long preambloc;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
167 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 int blocklen = -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 int nextcalc = -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 unsigned char outbuf[5];
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
171 int caddr;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
173 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
175 if (cl -> outputl < 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176 continue;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
177 caddr = lw_expr_intval(cl -> addr);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
178 if (caddr != nextcalc && cl -> outputl > 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
179 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
180 // need preamble here
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
181 if (blocklen > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
182 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
183 // update previous preamble if needed
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
184 fseek(of, preambloc, SEEK_SET);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
185 outbuf[0] = (blocklen >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
186 outbuf[1] = blocklen & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
187 writebytes(outbuf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
188 fseek(of, 0, SEEK_END);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
189 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
190 blocklen = 0;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
191 nextcalc = caddr;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192 outbuf[0] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
193 outbuf[1] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
194 outbuf[2] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
195 outbuf[3] = (nextcalc >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
196 outbuf[4] = nextcalc & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
197 preambloc = ftell(of) + 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
198 writebytes(outbuf, 5, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
199 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
200 nextcalc += cl -> outputl;
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
201 writebytes(cl -> output, cl -> outputl, 1, of);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
202 blocklen += cl -> outputl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
203 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
204 if (blocklen > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
205 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
206 fseek(of, preambloc, SEEK_SET);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
207 outbuf[0] = (blocklen >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
208 outbuf[1] = blocklen & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
209 writebytes(outbuf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
210 fseek(of, 0, SEEK_END);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
211 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
212
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
213 // now write postamble
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
214 outbuf[0] = 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
215 outbuf[1] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
216 outbuf[2] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
217 outbuf[3] = (as -> execaddr >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
218 outbuf[4] = (as -> execaddr) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
219 writebytes(outbuf, 5, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
220 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
221
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
222 void write_code_obj_sbadd(sectiontab_t *s, unsigned char b)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
223 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
224 if (s -> oblen >= s -> obsize)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
225 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
226 s -> obytes = lw_realloc(s -> obytes, s -> obsize + 128);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
227 s -> obsize += 128;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
228 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
229 s -> obytes[s -> oblen] = b;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
230 s -> oblen += 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
231 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
232
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
233
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
234 int write_code_obj_expraux(lw_expr_t e, void *of)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
235 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
236 int tt;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
237 int v;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
238 unsigned char buf[16];
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
239
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
240 tt = lw_expr_type(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
241
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
242 switch (tt)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
243 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
244 case lw_expr_type_oper:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
245 buf[0] = 0x04;
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
246 switch (lw_expr_whichop(e))
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
247 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
248 case lw_expr_oper_plus:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
249 buf[1] = 0x01;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
250 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
251
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
252 case lw_expr_oper_minus:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
253 buf[1] = 0x02;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
254 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
255
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
256 case lw_expr_oper_times:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
257 buf[1] = 0x03;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
258 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
259
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
260 case lw_expr_oper_divide:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
261 buf[1] = 0x04;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
262 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
263
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
264 case lw_expr_oper_mod:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
265 buf[1] = 0x05;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
266 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
267
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
268 case lw_expr_oper_intdiv:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
269 buf[1] = 0x06;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
270 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
271
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
272 case lw_expr_oper_bwand:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
273 buf[1] = 0x07;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
274 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
275
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
276 case lw_expr_oper_bwor:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
277 buf[1] = 0x08;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
278 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
279
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
280 case lw_expr_oper_bwxor:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
281 buf[1] = 0x09;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
282 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
283
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
284 case lw_expr_oper_and:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
285 buf[1] = 0x0A;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
286 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
287
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
288 case lw_expr_oper_or:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
289 buf[1] = 0x0B;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
290 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
291
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
292 case lw_expr_oper_neg:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
293 buf[1] = 0x0C;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
294 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
295
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
296 case lw_expr_oper_com:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
297 buf[1] = 0x0D;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
298 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
299
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
300 default:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
301 buf[1] = 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
302 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
303 writebytes(buf, 2, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
304 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
305
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
306 case lw_expr_type_int:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
307 v = lw_expr_intval(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
308 buf[0] = 0x01;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
309 buf[1] = (v >> 8) & 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
310 buf[2] = v & 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
311 writebytes(buf, 3, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
312 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
313
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
314 case lw_expr_type_special:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
315 v = lw_expr_specint(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
316 switch (v)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
317 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
318 case lwasm_expr_secbase:
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
319 {
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
320 // replaced with a synthetic symbol
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
321 sectiontab_t *se;
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
322 se = lw_expr_specptr(e);
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
323
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
324 writebytes("\x03\x02", 2, 1, of);
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
325 writebytes(se -> name, strlen(se -> name) + 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
326 return 0;
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
327 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
328 case lwasm_expr_import:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
329 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
330 importlist_t *ie;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
331 ie = lw_expr_specptr(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
332 buf[0] = 0x02;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
333 writebytes(buf, 1, 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
334 writebytes(ie -> symbol, strlen(ie -> symbol) + 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
335 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
336 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
337 case lwasm_expr_syment:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
338 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
339 struct symtabe *se;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
340 se = lw_expr_specptr(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
341 buf[0] = 0x03;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
342 writebytes(buf, 1, 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
343 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
344 if (se -> context != -1)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
345 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
346 sprintf(buf, "\x01%d", se -> context);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
347 writebytes(buf, strlen(buf), 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
348 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
349 writebytes("", 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
350 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
351 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
352 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
353
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
354 default:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
355 // unrecognized term type - replace with integer 0
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
356 // fprintf(stderr, "Unrecognized term type: %s\n", lw_expr_print(e));
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
357 buf[0] = 0x01;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
358 buf[1] = 0x00;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
359 buf[2] = 0x00;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
360 writebytes(buf, 3, 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
361 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
362 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
363 return 0;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
364 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
365
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
366
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
367 void write_code_obj(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
368 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
369 line_t *l;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
370 sectiontab_t *s;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
371 reloctab_t *re;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
372 exportlist_t *ex;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
373 struct symtabe *se;
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
374
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
375 int i;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
376 unsigned char buf[16];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
377
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
378 // output the magic number and file header
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
379 // the 8 is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
380 writebytes("LWOBJ16", 8, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
381
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
382 // run through the entire system and build the byte streams for each
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
383 // section; at the same time, generate a list of "local" symbols to
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
384 // output for each section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
385 // NOTE: for "local" symbols, we will append \x01 and the ascii string
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
386 // of the context identifier (so sym in context 1 would be "sym\x011"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
387 // we can do this because the linker can handle symbols with any
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
388 // character other than NUL.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
389 // also we will generate a list of incomplete references for each
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
390 // section along with the actual definition that will be output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
391
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
392 // once all this information is generated, we will output each section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
393 // to the file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
394
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
395 // NOTE: we build everything in memory then output it because the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
396 // assembler accepts multiple instances of the same section but the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
397 // linker expects only one instance of each section in the object file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
398 // so we need to collect all the various pieces of a section together
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
399 // (also, the assembler treated multiple instances of the same section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
400 // as continuations of previous sections so we would need to collect
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
401 // them together anyway.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
402
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
403 for (l = as -> line_head; l; l = l -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
404 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
405 if (l -> csect)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
406 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
407 // we're in a section - need to output some bytes
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
408 if (l -> outputl > 0)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
409 for (i = 0; i < l -> outputl; i++)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
410 write_code_obj_sbadd(l -> csect, l -> output[i]);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
411 else if (l -> outputl == 0 || l -> outputl == -1)
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
412 for (i = 0; i < l -> len; i++)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
413 write_code_obj_sbadd(l -> csect, 0);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
414 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
415 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
416
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
417 // run through the sections
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
418 for (s = as -> sections; s; s = s -> next)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
419 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
420 // write the name
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
421 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
422
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
423 // write the flags
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
424 if (s -> flags & section_flag_bss)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
425 writebytes("\x01", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
426
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
427 // indicate end of flags - the "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
428 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
429
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
430 // now the local symbols
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
431
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
432 // a symbol for section base address
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
433 writebytes("\x02", 1, 1, of);
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
434 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
435 // address 0; "\0" is not an error
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
436 writebytes("\0", 2, 1, of);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
437 for (se = as -> symtab.head; se; se = se -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
438 {
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
439 lw_expr_t te;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
440
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
441 debug_message(as, 200, "Consider symbol %s (%p) for export in section %p", se -> symbol, se -> section, s);
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
442
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
443 // ignore symbols not in this section
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
444 if (se -> section != s)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
445 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
446
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
447 debug_message(as, 200, " In section");
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
448
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
449 if (se -> flags & symbol_flag_set)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
450 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
451
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
452 debug_message(as, 200, " Not symbol_flag_set");
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
453
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
454 te = lw_expr_copy(se -> value);
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
455 debug_message(as, 200, " Value=%s", lw_expr_print(te));
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
456 as -> exportcheck = 1;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
457 as -> csect = s;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
458 lwasm_reduce_expr(as, te);
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
459 as -> exportcheck = 0;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
460
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
461 debug_message(as, 200, " Value2=%s", lw_expr_print(te));
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
462
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
463 // don't output non-constant symbols
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
464 if (!lw_expr_istype(te, lw_expr_type_int))
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
465 {
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
466 lw_expr_destroy(te);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
467 continue;
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
468 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
469
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
470 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
471 if (se -> context >= 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
472 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
473 writebytes("\x01", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
474 sprintf(buf, "%d", se -> context);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
475 writebytes(buf, strlen(buf), 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
476 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
477 // the "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
478 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
479
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
480 // write the address
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
481 buf[0] = (lw_expr_intval(te) >> 8) & 0xff;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
482 buf[1] = lw_expr_intval(te) & 0xff;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
483 writebytes(buf, 2, 1, of);
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
484 lw_expr_destroy(te);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
485 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
486 // flag end of local symbol table - "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
487 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
488
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
489 // now the exports -- FIXME
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
490 for (ex = as -> exportlist; ex; ex = ex -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
491 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
492 int eval;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
493 lw_expr_t te;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
494 line_t tl;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
495
412
35a0b086bf4a Added dummy .bank pseudo and fixed segfault in output for object target (on imported symbol)
lost@l-w.ca
parents: 393
diff changeset
496 if (ex -> se == NULL)
35a0b086bf4a Added dummy .bank pseudo and fixed segfault in output for object target (on imported symbol)
lost@l-w.ca
parents: 393
diff changeset
497 continue;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
498 if (ex -> se -> section != s)
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
499 continue;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
500 te = lw_expr_copy(ex -> se -> value);
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
501 as -> csect = ex -> se -> section;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
502 as -> exportcheck = 1;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
503 tl.as = as;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
504 as -> cl = &tl;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
505 lwasm_reduce_expr(as, te);
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
506 as -> exportcheck = 0;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
507 as -> cl = NULL;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
508 if (!lw_expr_istype(te, lw_expr_type_int))
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
509 {
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
510 lw_expr_destroy(te);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
511 continue;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
512 }
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
513 eval = lw_expr_intval(te);
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
514 lw_expr_destroy(te);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
515 writebytes(ex -> symbol, strlen(ex -> symbol) + 1, 1, of);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
516 buf[0] = (eval >> 8) & 0xff;
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
517 buf[1] = eval & 0xff;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
518 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
519 }
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
520
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
521 // flag end of exported symbols - "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
522 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
523
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
524 // FIXME - relocation table
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
525 for (re = s -> reloctab; re; re = re -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
526 {
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
527 int offset;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
528 lw_expr_t te;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
529 line_t tl;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
530
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
531 tl.as = as;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
532 as -> cl = &tl;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
533 as -> csect = s;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
534 as -> exportcheck = 1;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
535
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
536 if (re -> expr == NULL)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
537 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
538 // this is an error but we'll simply ignore it
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
539 // and not output this expression
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
540 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
541 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
542
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
543 // work through each term in the expression and output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
544 // the proper equivalent to the object file
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
545 if (re -> size == 1)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
546 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
547 // flag an 8 bit relocation (low 8 bits will be used)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
548 buf[0] = 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
549 buf[1] = 0x01;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
550 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
551 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
552
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
553 te = lw_expr_copy(re -> offset);
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
554 lwasm_reduce_expr(as, te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
555 if (!lw_expr_istype(te, lw_expr_type_int))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
556 {
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
557 lw_expr_destroy(te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
558 continue;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
559 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
560 offset = lw_expr_intval(te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
561 lw_expr_destroy(te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
562
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
563 // output expression
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
564 lw_expr_testterms(re -> expr, write_code_obj_expraux, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
565
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
566 // flag end of expressions
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
567 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
568
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
569 // write the offset
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
570 buf[0] = (offset >> 8) & 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
571 buf[1] = offset & 0xff;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
572 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
573 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
574
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
575 // flag end of incomplete references list
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
576 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
577
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
578 // now blast out the code
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
579
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
580 // length
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
581 buf[0] = s -> oblen >> 8 & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
582 buf[1] = s -> oblen & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
583 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
584
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
585 if (!(s -> flags & section_flag_bss))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
586 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
587 writebytes(s -> obytes, s -> oblen, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
588 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
589 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
590
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
591 // flag no more sections
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
592 // the "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
593 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
594 }