Mercurial > hg-old > index.cgi
annotate lwasm/pseudo.c @ 159:71561c12b20b
Updated docs to reflect new cescapes pragma and discuss implicit assumption of the bss section flag for sections named bss and .bss
author | lost |
---|---|
date | Sat, 31 Jan 2009 06:32:27 +0000 |
parents | f0527dc3804d |
children | b061350c17e4 |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 pseudo.c |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
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 This file implements the various pseudo operations. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
22 */ |
0 | 23 |
24 #include <stdlib.h> | |
50 | 25 #include <string.h> |
0 | 26 #include "lwasm.h" |
27 #include "instab.h" | |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
28 #include "expr.h" |
52 | 29 #include "util.h" |
0 | 30 |
52 | 31 extern int lwasm_read_file(asmstate_t *as, const char *filename); |
0 | 32 |
158
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
33 OPFUNC(pseudo_noop) |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
34 { |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
35 |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
36 } |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
37 |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
38 OPFUNC(pseudo_org) |
0 | 39 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
40 int v, r; |
52 | 41 |
74 | 42 if (as -> csect) |
43 { | |
44 register_error(as, l, 1, "ORG not allowed within sections"); | |
45 return; | |
46 } | |
47 | |
52 | 48 if (as -> passnum != 1) |
49 { | |
50 // org is not needed to be processed on pass 2 | |
51 // this will prevent phasing errors for forward references that | |
52 // resolve on the second pass | |
53 // we saved the org address in l -> codeaddr on pass 1 | |
54 as -> addr = l -> codeaddr; | |
55 return; | |
56 } | |
0 | 57 |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
58 if (l -> sym) |
0 | 59 { |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
60 register_error(as, l, 1, "No symbol allowed with ORG"); |
0 | 61 } |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
62 |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
63 r = lwasm_expr_result2(as, l, p, EXPR_PASS1CONST, &v, 0); |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
64 if (r != 0) |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
65 return; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
66 l -> codeaddr = v; |
49
21ae0fab469b
Added needed infra for useful listing of EQU and ORG type statements
lost
parents:
47
diff
changeset
|
67 l -> addrset = 1; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
68 as -> addr = v; |
0 | 69 } |
70 | |
47
804d7465e0f9
Implemented ORG and fixed problems with constants using $, &, or @ to specify base
lost
parents:
4
diff
changeset
|
71 /* |
52 | 72 The operand for include is a string optionally enclosed in " |
73 */ | |
74 OPFUNC(pseudo_include) | |
0 | 75 { |
76 int v1; | |
52 | 77 char *fn; |
57 | 78 |
52 | 79 // only include files on pass 1 |
80 // but make sure local include context is right | |
81 // for the next line... | |
0 | 82 if (as -> passnum != 1) |
52 | 83 { |
57 | 84 as -> context = lwasm_next_context(as); |
0 | 85 return; |
52 | 86 } |
87 | |
88 while (**p && isspace(**p)) | |
89 (*p)++; | |
90 | |
91 if (!**p) | |
0 | 92 { |
52 | 93 register_error(as, l, 1, "Bad file name"); |
0 | 94 return; |
95 } | |
52 | 96 |
97 if (**p == '"') | |
98 { | |
99 // search for ending " | |
100 (*p)++; | |
101 for (v1 = 0; *((*p)+v1) && *((*p)+v1) != '"'; v1++) | |
102 /* do nothing */ ; | |
103 if (*((*p)+v1) != '"') | |
104 { | |
105 register_error(as, l, 1, "Bad file name"); | |
106 return; | |
107 } | |
108 } | |
109 else | |
0 | 110 { |
52 | 111 // search for a space type character |
112 for (v1 = 0; *((*p)+v1) && !isspace(*((*p)+v1)); v1++) | |
113 ; | |
0 | 114 } |
52 | 115 |
116 fn = lwasm_alloc(v1 + 1); | |
117 memcpy(fn, *p, v1); | |
118 fn[v1] = '\0'; | |
119 | |
120 // end local label context on include | |
57 | 121 as -> context = lwasm_next_context(as); |
52 | 122 if (lwasm_read_file(as, fn) < 0) |
123 { | |
124 register_error(as, l, 1, "File include error (%s)", fn); | |
125 } | |
126 lwasm_free(fn); | |
0 | 127 } |
128 | |
50 | 129 OPFUNC(pseudo_rmb) |
0 | 130 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
131 int r, v; |
0 | 132 |
95 | 133 if (as -> passnum == 2) |
0 | 134 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
135 as -> addr += l -> nocodelen; |
0 | 136 return; |
137 } | |
103 | 138 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, -1); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
139 if (r != 0) |
0 | 140 return; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
141 l -> nocodelen = v; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
142 as -> addr += v; |
0 | 143 } |
53 | 144 |
145 OPFUNC(pseudo_rmd) | |
0 | 146 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
147 int r, v; |
0 | 148 |
95 | 149 if (as -> passnum == 2) |
0 | 150 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
151 as -> addr += l -> nocodelen; |
0 | 152 return; |
153 } | |
103 | 154 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
155 if (r != 0) |
0 | 156 return; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
157 v *= 2; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
158 l -> nocodelen = v; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
159 as -> addr += v; |
0 | 160 } |
161 | |
53 | 162 OPFUNC(pseudo_rmq) |
0 | 163 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
164 int r, v; |
0 | 165 |
95 | 166 if (as -> passnum == 2) |
0 | 167 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
168 as -> addr += l -> nocodelen; |
0 | 169 return; |
170 } | |
103 | 171 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
172 if (r != 0) |
0 | 173 return; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
174 v *= 4; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
175 l -> nocodelen = v; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
176 as -> addr += v; |
0 | 177 } |
178 | |
53 | 179 OPFUNC(pseudo_zmb) |
0 | 180 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
181 int r, v; |
0 | 182 |
103 | 183 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
184 if (r != 0) |
0 | 185 return; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
186 while (v--) |
53 | 187 lwasm_emit(as, l, 0); |
0 | 188 } |
189 | |
53 | 190 OPFUNC(pseudo_zmd) |
0 | 191 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
192 int r, v; |
0 | 193 |
103 | 194 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
195 if (r != 0) |
0 | 196 return; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
197 v *= 2; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
198 while (v--) |
53 | 199 lwasm_emit(as, l, 0); |
0 | 200 } |
201 | |
53 | 202 OPFUNC(pseudo_zmq) |
203 { | |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
204 int r, v; |
53 | 205 |
103 | 206 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
207 if (r != 0) |
53 | 208 return; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
209 v *= 4; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
210 while (v--) |
53 | 211 lwasm_emit(as, l, 0); |
212 } | |
213 | |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
214 OPFUNC(pseudo_end) |
0 | 215 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
216 int r, v; |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
217 lwasm_expr_stack_t *s; |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
218 |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
219 |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
220 as -> endseen = 1; |
0 | 221 |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
222 // address only matters for DECB output |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
223 if (as -> outformat != OUTPUT_DECB) |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
224 return; |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
225 |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
226 r = lwasm_expr_result2(as, l, p, 0, &v, 0); |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
227 if (r != 0) |
0 | 228 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
229 register_error(as, l, 2, "Bad operand"); |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
230 } |
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
231 |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
232 v = v & 0xffff; |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
233 if (as -> passnum == 2) |
0 | 234 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
235 as -> execaddr = v; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
236 l -> symaddr = v; |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
237 l -> addrset = 2; |
0 | 238 } |
54
360d53062bb9
Fixed typo in instruction table and added END directive
lost
parents:
53
diff
changeset
|
239 } |
0 | 240 |
56 | 241 |
242 OPFUNC(pseudo_align) | |
0 | 243 { |
244 int cn; | |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
245 int r, v; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
246 |
56 | 247 if (as -> passnum == 2) |
0 | 248 { |
56 | 249 while (as -> addr < l -> symaddr) |
250 lwasm_emit(as, l, 0); | |
251 return; | |
252 } | |
253 | |
103 | 254 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
255 if (r != 0) |
56 | 256 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
257 l -> symaddr = as -> addr; |
0 | 258 return; |
259 } | |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
260 |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
261 if (v < 1) |
56 | 262 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
263 register_error(as, l, 1, "Illegal alignment %d", v); |
56 | 264 return; |
0 | 265 } |
56 | 266 |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
267 cn = l -> codeaddr % v; |
56 | 268 if (cn) |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
269 cn = v - cn; |
56 | 270 |
271 while (cn--) | |
272 { | |
273 lwasm_emit(as, l, 0); | |
274 } | |
275 l -> symaddr = as -> addr; | |
0 | 276 } |
56 | 277 |
50 | 278 OPFUNC(pseudo_equ) |
0 | 279 { |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
280 int r, v; |
52 | 281 |
50 | 282 if (l -> sym == NULL) |
0 | 283 { |
50 | 284 register_error(as, l, 1, "No symbol specified"); |
0 | 285 return; |
286 } | |
50 | 287 |
103 | 288 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
289 if (r < 0) |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
290 v = 0; |
63
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
291 |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
292 l -> symaddr = v & 0xFFFF; |
50 | 293 l -> addrset = 2; |
63
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
294 |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
295 // note: we need to do this because the symbol might have resolved |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
296 // to a constant! |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
74
diff
changeset
|
297 lwasm_register_symbol(as, l, l -> sym, v, (r > 0 ? SYMBOL_COMPLEX: SYMBOL_NORM) | SYMBOL_FORCE); |
0 | 298 } |
63
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
299 |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
300 OPFUNC(pseudo_set) |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
301 { |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
302 int r, v; |
64 | 303 |
304 // set MUST run on both passes as the symbol value changes! | |
305 | |
306 if (l -> sym == NULL) | |
307 { | |
308 register_error(as, l, 1, "No symbol specified"); | |
309 return; | |
310 } | |
311 | |
103 | 312 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
313 if (r < 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
314 v = 0; |
64 | 315 |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
316 l -> symaddr = v & 0xFFFF; |
64 | 317 l -> addrset = 2; |
318 | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
319 lwasm_register_symbol(as, l, l -> sym, v, (r > 0 ? SYMBOL_COMPLEX: SYMBOL_NORM) | SYMBOL_SET); |
63
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
320 } |
d85ba47b1e8f
Moved symbol registration so symbols that are in skipped code do not get registered and so EQU/SET can do their own registration
lost
parents:
57
diff
changeset
|
321 |
65
31d8e85706e7
Implemented setdp and corrected handling of direct page detection in insn_gen_aux()
lost
parents:
64
diff
changeset
|
322 OPFUNC(pseudo_setdp) |
0 | 323 { |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
324 int r, v; |
0 | 325 |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
326 if (as -> outformat == OUTPUT_OBJ) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
327 { |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
328 register_error(as, l, 1, "SETDP not permitted with OBJ target"); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
329 return; |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
330 } |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
331 |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
332 // setdp is needed on both passes; must resolve to a constant on pass 1 |
103 | 333 r = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
334 if (r != 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
335 return; |
65
31d8e85706e7
Implemented setdp and corrected handling of direct page detection in insn_gen_aux()
lost
parents:
64
diff
changeset
|
336 |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
337 if (v < -127 || v > 255) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
338 { |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
339 register_error(as, l, 1, "Byte overflow"); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
340 return; |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
341 } |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
342 |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
343 l -> symaddr = v & 0xFF; |
65
31d8e85706e7
Implemented setdp and corrected handling of direct page detection in insn_gen_aux()
lost
parents:
64
diff
changeset
|
344 l -> addrset = 2; |
31d8e85706e7
Implemented setdp and corrected handling of direct page detection in insn_gen_aux()
lost
parents:
64
diff
changeset
|
345 |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
346 as -> dpval = v & 0xFF; |
0 | 347 } |
348 | |
56 | 349 OPFUNC(pseudo_fcc) |
350 { | |
0 | 351 int delim = 0; |
352 | |
56 | 353 delim = **p; |
0 | 354 if (!delim) |
355 { | |
56 | 356 register_error(as, l, 1, "Bad operand"); |
357 return; | |
0 | 358 } |
56 | 359 *p += 1; |
360 while (**p && **p != delim) | |
0 | 361 { |
56 | 362 lwasm_emit(as, l, **p); |
363 (*p)++; | |
0 | 364 } |
56 | 365 if (**p) |
366 (*p)++; | |
0 | 367 } |
368 | |
56 | 369 |
370 OPFUNC(pseudo_fcs) | |
0 | 371 { |
372 int delim = 0; | |
56 | 373 |
374 delim = **p; | |
0 | 375 if (!delim) |
376 { | |
56 | 377 register_error(as, l, 1, "Bad operand"); |
378 return; | |
0 | 379 } |
56 | 380 *p += 1; |
381 while (**p && **p != delim) | |
0 | 382 { |
56 | 383 if (!*((*p) + 1) || *((*p) + 1) == delim) |
384 lwasm_emit(as, l, **p | 0x80); | |
385 else | |
386 lwasm_emit(as, l, **p); | |
387 (*p)++; | |
0 | 388 } |
56 | 389 if (**p) |
390 (*p)++; | |
0 | 391 } |
392 | |
56 | 393 OPFUNC(pseudo_fcn) |
0 | 394 { |
395 int delim = 0; | |
396 | |
56 | 397 delim = **p; |
0 | 398 if (!delim) |
399 { | |
56 | 400 register_error(as, l, 1, "Bad operand"); |
401 return; | |
0 | 402 } |
56 | 403 *p += 1; |
404 while (**p && **p != delim) | |
0 | 405 { |
56 | 406 lwasm_emit(as, l, **p); |
407 (*p)++; | |
0 | 408 } |
56 | 409 if (**p) |
410 (*p)++; | |
411 lwasm_emit(as, l, 0); | |
0 | 412 } |
56 | 413 |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
414 // FIXME: handle external, etc., references in a useful manner |
56 | 415 OPFUNC(pseudo_fcb) |
0 | 416 { |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
417 int r, v; |
0 | 418 |
419 fcb_again: | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
420 r = lwasm_expr_result2(as, l, p, 0, &v, -1); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
421 if (r < 0) |
56 | 422 return; |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
423 |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
424 if (r > 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
425 { |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
426 register_error(as, l, 2, "Illegal external or inter-segment reference"); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
427 v = 0; |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
428 } |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
429 |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
430 if (v < -127 || v > 255) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
431 { |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
432 register_error(as, l, 1, "Byte overflow"); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
433 } |
56 | 434 |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
435 lwasm_emit(as, l, v); |
56 | 436 if (**p == ',') |
0 | 437 { |
56 | 438 (*p)++; |
0 | 439 goto fcb_again; |
440 } | |
441 } | |
442 | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
443 // FIXME: handle external references in an intelligent way |
56 | 444 OPFUNC(pseudo_fdb) |
0 | 445 { |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
446 int r, v; |
103 | 447 int extseen = 0; |
448 char *p1; | |
0 | 449 |
450 fdb_again: | |
103 | 451 p1 = *p; |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
452 r = lwasm_expr_result2(as, l, p, 0, &v, -1); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
453 if (r < 0) |
56 | 454 return; |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
455 |
103 | 456 if (r > 0 && extseen == 1) |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
457 { |
103 | 458 register_error(as, l, 2, "Illegal external or inter-segment reference (only 1 per FDB line)"); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
459 v = 0; |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
460 } |
103 | 461 else if (r > 0) |
462 { | |
463 l -> relocoff = as -> addr - l -> codeaddr; | |
464 *p = p1; | |
465 r = lwasm_expr_result2(as, l, p, 0, &v, 0); | |
466 } | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
467 |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
468 lwasm_emit(as, l, v >> 8); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
469 lwasm_emit(as, l, v & 0xff); |
56 | 470 if (**p == ',') |
0 | 471 { |
56 | 472 (*p)++; |
0 | 473 goto fdb_again; |
474 } | |
475 } | |
476 | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
477 // FIXME: handle external references in a sensible way |
56 | 478 OPFUNC(pseudo_fqb) |
0 | 479 { |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
480 int r, v; |
56 | 481 |
0 | 482 fqb_again: |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
483 r = lwasm_expr_result2(as, l, p, 0, &v, -1); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
484 if (r < 0) |
56 | 485 return; |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
486 |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
487 if (r > 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
488 { |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
489 register_error(as, l, 2, "Illegal external or inter-segment reference"); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
490 v = 0; |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
491 } |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
492 |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
493 lwasm_emit(as, l, v >> 24); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
494 lwasm_emit(as, l, v >> 16); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
495 lwasm_emit(as, l, v >> 8); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
496 lwasm_emit(as, l, v & 0xff); |
56 | 497 if (**p == ',') |
0 | 498 { |
56 | 499 (*p)++; |
0 | 500 goto fqb_again; |
501 } | |
502 } | |
503 | |
504 // don't need to do anything if we are executing one of these | |
57 | 505 OPFUNC(pseudo_endc) |
0 | 506 { |
57 | 507 if (as -> skipcond && !(as -> skipmacro)) |
508 { | |
509 as -> skipcount -= 1; | |
510 if (as -> skipcount <= 0) | |
511 { | |
512 as -> skipcond = 0; | |
513 } | |
514 } | |
0 | 515 return; |
516 } | |
517 | |
518 // if "else" executes, we must be going into an "ignore" state | |
57 | 519 OPFUNC(pseudo_else) |
0 | 520 { |
57 | 521 if (as -> skipmacro) |
522 return; | |
523 | |
524 if (as -> skipcond) | |
525 { | |
526 if (as -> skipcount == 1) | |
527 { | |
528 as -> skipcount = 0; | |
529 as -> skipcond = 0; | |
530 } | |
531 return; | |
532 } | |
533 | |
0 | 534 as -> skipcond = 1; |
535 as -> skipcount = 1; | |
536 } | |
537 | |
57 | 538 OPFUNC(pseudo_ifne) |
0 | 539 { |
540 int v1; | |
541 int rval; | |
57 | 542 |
543 if (as -> skipcond && !(as -> skipmacro)) | |
0 | 544 { |
57 | 545 as -> skipcount++; |
546 return; | |
547 } | |
548 | |
103 | 549 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
550 if (rval != 0) |
57 | 551 return; |
552 if (!v1) | |
553 { | |
554 as -> skipcond = 1; | |
555 as -> skipcount = 1; | |
0 | 556 } |
557 } | |
57 | 558 |
104 | 559 OPFUNC(pseudo_ifdef) |
560 { | |
561 lwasm_symbol_ent_t *se; | |
562 char *sym; | |
563 char *p2; | |
564 | |
565 if (as -> skipcond && !(as -> skipmacro)) | |
566 { | |
567 as -> skipcount++; | |
568 return; | |
569 } | |
570 | |
571 if (as -> passnum != 1) | |
572 { | |
573 if (!(l -> fsize)) | |
574 { | |
575 as -> skipcond = 1; | |
576 as -> skipcount = 1; | |
577 } | |
578 return; | |
579 } | |
580 | |
581 if (!**p) | |
582 { | |
583 register_error(as, l, 1, "Need symbol name"); | |
584 return; | |
585 } | |
586 | |
587 for (p2 = *p; *p2 && !isspace(*p2); p2++) | |
588 /* do nothing */ ; | |
589 | |
590 sym = lwasm_alloc(p2 - *p + 1); | |
591 memcpy(sym, *p, p2 - *p); | |
592 sym[p2 - *p] = '\0'; | |
593 | |
594 *p = p2; | |
595 | |
596 se = lwasm_find_symbol(as, sym, l -> context); | |
597 if (!se) | |
598 se = lwasm_find_symbol(as, sym, -1); | |
599 | |
600 lwasm_free(sym); | |
601 | |
602 if (!se) | |
603 { | |
604 as -> skipcond = 1; | |
605 as -> skipcount = 1; | |
606 l -> fsize = 0; | |
607 } | |
608 else | |
609 { | |
610 l -> fsize = 1; | |
611 } | |
612 } | |
613 | |
614 OPFUNC(pseudo_ifndef) | |
615 { | |
616 lwasm_symbol_ent_t *se; | |
617 char *sym; | |
618 char *p2; | |
619 | |
620 if (as -> skipcond && !(as -> skipmacro)) | |
621 { | |
622 as -> skipcount++; | |
623 return; | |
624 } | |
625 | |
626 if (as -> passnum != 1) | |
627 { | |
628 if (l -> fsize) | |
629 { | |
630 as -> skipcond = 1; | |
631 as -> skipcount = 1; | |
632 } | |
633 return; | |
634 } | |
635 | |
636 if (!**p) | |
637 { | |
638 register_error(as, l, 1, "Need symbol name"); | |
639 return; | |
640 } | |
641 | |
642 for (p2 = *p; *p2 && !isspace(*p2); p2++) | |
643 /* do nothing */ ; | |
644 | |
645 sym = lwasm_alloc(p2 - *p + 1); | |
646 memcpy(sym, *p, p2 - *p); | |
647 sym[p2 - *p] = '\0'; | |
648 | |
649 *p = p2; | |
650 | |
651 se = lwasm_find_symbol(as, sym, l -> context); | |
652 if (!se) | |
653 se = lwasm_find_symbol(as, sym, -1); | |
654 | |
655 lwasm_free(sym); | |
656 | |
657 if (se) | |
658 { | |
659 as -> skipcond = 1; | |
660 as -> skipcount = 1; | |
661 l -> fsize = 0; | |
662 } | |
663 else | |
664 { | |
665 l -> fsize = 1; | |
666 } | |
667 } | |
668 | |
57 | 669 OPFUNC(pseudo_ifeq) |
0 | 670 { |
671 int v1; | |
672 int rval; | |
57 | 673 |
674 if (as -> skipcond && !(as -> skipmacro)) | |
0 | 675 { |
57 | 676 as -> skipcount++; |
677 return; | |
0 | 678 } |
57 | 679 |
103 | 680 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
681 if (rval != 0) |
57 | 682 return; |
683 if (v1) | |
0 | 684 { |
57 | 685 as -> skipcond = 1; |
686 as -> skipcount = 1; | |
0 | 687 } |
688 } | |
57 | 689 |
690 OPFUNC(pseudo_iflt) | |
0 | 691 { |
692 int v1; | |
693 int rval; | |
57 | 694 |
695 if (as -> skipcond && !(as -> skipmacro)) | |
0 | 696 { |
57 | 697 as -> skipcount++; |
698 return; | |
0 | 699 } |
57 | 700 |
103 | 701 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
702 if (rval != 0) |
57 | 703 return; |
704 if (v1 >= 0) | |
0 | 705 { |
57 | 706 as -> skipcond = 1; |
707 as -> skipcount = 1; | |
0 | 708 } |
709 } | |
57 | 710 |
711 OPFUNC(pseudo_ifle) | |
0 | 712 { |
713 int v1; | |
714 int rval; | |
57 | 715 |
716 if (as -> skipcond && !(as -> skipmacro)) | |
0 | 717 { |
57 | 718 as -> skipcount++; |
719 return; | |
0 | 720 } |
57 | 721 |
103 | 722 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
723 if (rval != 0) |
57 | 724 return; |
725 if (v1 > 0) | |
0 | 726 { |
57 | 727 as -> skipcond = 1; |
728 as -> skipcount = 1; | |
0 | 729 } |
730 } | |
57 | 731 |
732 OPFUNC(pseudo_ifgt) | |
0 | 733 { |
734 int v1; | |
735 int rval; | |
57 | 736 |
737 if (as -> skipcond && !(as -> skipmacro)) | |
0 | 738 { |
57 | 739 as -> skipcount++; |
740 return; | |
0 | 741 } |
57 | 742 |
103 | 743 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
744 if (rval != 0) |
57 | 745 return; |
746 if (v1 <= 0) | |
0 | 747 { |
57 | 748 as -> skipcond = 1; |
749 as -> skipcount = 1; | |
0 | 750 } |
751 } | |
57 | 752 |
753 OPFUNC(pseudo_ifge) | |
0 | 754 { |
755 int v1; | |
756 int rval; | |
57 | 757 |
758 if (as -> skipcond && !(as -> skipmacro)) | |
0 | 759 { |
57 | 760 as -> skipcount++; |
761 return; | |
0 | 762 } |
57 | 763 |
103 | 764 rval = lwasm_expr_result2(as, l, p, EXPR_SECTCONST | EXPR_PASS1CONST, &v1, 0); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
765 if (rval != 0) |
57 | 766 return; |
767 if (v1 < 0) | |
0 | 768 { |
57 | 769 as -> skipcond = 1; |
770 as -> skipcount = 1; | |
0 | 771 } |
772 } | |
773 | |
56 | 774 OPFUNC(pseudo_error) |
0 | 775 { |
56 | 776 register_error(as, l, 1, "User error: %s", *p); |
0 | 777 } |
56 | 778 |
74 | 779 |
780 OPFUNC(pseudo_section) | |
781 { | |
782 sectiontab_t *s; | |
783 char *p2; | |
784 char *sn; | |
785 char *opts; | |
786 | |
99 | 787 |
74 | 788 if (as -> outformat != OUTPUT_OBJ) |
789 { | |
790 register_error(as, l, 1, "Sections only supported for obj target"); | |
791 return; | |
792 } | |
793 | |
794 if (as -> csect) | |
795 { | |
796 as -> csect -> offset = as -> addr; | |
797 as -> csect = NULL; | |
798 } | |
799 | |
800 if (!**p) | |
801 { | |
802 register_error(as, l, 1, "Need section name"); | |
803 return; | |
804 } | |
805 | |
806 for (p2 = *p; *p2 && !isspace(*p2); p2++) | |
807 /* do nothing */ ; | |
808 | |
809 sn = lwasm_alloc(p2 - *p + 1); | |
810 memcpy(sn, *p, p2 - *p); | |
811 sn[p2 - *p] = '\0'; | |
812 | |
813 *p = p2; | |
814 | |
815 opts = strchr(sn, ','); | |
816 if (opts) | |
817 { | |
818 *opts++ = '\0'; | |
819 } | |
820 | |
821 // have we seen the section name already? | |
822 for (s = as -> sections; s; s = s -> next) | |
823 { | |
824 if (!strcmp(s -> name, sn)) | |
825 break; | |
826 } | |
827 | |
99 | 828 if (s && as -> passnum == 1) |
74 | 829 { |
830 lwasm_free(sn); | |
831 if (opts) | |
832 { | |
833 register_error(as, l, 1, "Section options can only be specified the first time"); | |
834 return; | |
835 } | |
836 } | |
837 else if (!s) | |
838 { | |
839 s = lwasm_alloc(sizeof(sectiontab_t)); | |
840 s -> name = sn; | |
841 s -> offset = 0; | |
842 s -> flags = 0; | |
85 | 843 s -> obytes = NULL; |
844 s -> oblen = 0; | |
845 s -> obsize = 0; | |
86 | 846 s -> rl = NULL; |
90 | 847 s -> exports = NULL; |
74 | 848 // parse options; only one "bss" |
849 if (opts && as -> passnum == 1) | |
850 { | |
851 if (!strcasecmp(opts, "bss")) | |
852 { | |
853 s -> flags = SECTION_BSS; | |
854 } | |
855 else | |
856 { | |
857 register_error(as, l, 1, "Unrecognized section option '%s'", opts); | |
858 lwasm_free(s -> name); | |
859 lwasm_free(s); | |
860 return; | |
861 } | |
862 } | |
863 | |
864 s -> next = as -> sections; | |
865 as -> sections = s; | |
866 } | |
867 as -> addr = s -> offset; | |
868 as -> csect = s; | |
869 as -> context = lwasm_next_context(as); | |
870 } | |
871 | |
872 OPFUNC(pseudo_endsection) | |
873 { | |
874 if (as -> outformat != OUTPUT_OBJ) | |
875 { | |
876 register_error(as, l, 1, "Sections only supported for obj target"); | |
877 return; | |
878 } | |
879 | |
880 if (!(as -> csect)) | |
881 { | |
882 register_error(as, l, 1, "ENDSECTION when not in a section"); | |
883 return; | |
884 } | |
885 | |
886 as -> csect -> offset = as -> addr; | |
887 as -> addr = 0; | |
888 as -> csect = 0; | |
889 as -> context = lwasm_next_context(as); | |
890 } | |
82 | 891 |
892 OPFUNC(pseudo_extern) | |
893 { | |
894 if (as -> passnum != 1) | |
895 return; | |
896 | |
897 if (as -> outformat != OUTPUT_OBJ) | |
898 { | |
899 register_error(as, l, 1, "External references only supported for obj target"); | |
900 return; | |
901 } | |
902 | |
903 if (as -> csect) | |
904 { | |
905 register_error(as, l, 1, "Cannot declare external symbols within a section"); | |
906 return; | |
907 } | |
908 | |
909 lwasm_register_symbol(as, l, l -> sym, 0, SYMBOL_EXTERN); | |
910 } | |
90 | 911 |
912 OPFUNC(pseudo_export) | |
913 { | |
914 lwasm_symbol_ent_t *se; | |
915 export_list_t *ex; | |
158
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
916 char *sym2, *sym3; |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
917 |
90 | 918 if (as -> outformat != OUTPUT_OBJ) |
919 { | |
920 register_error(as, l, 1, "Symbol exports only supported for obj target"); | |
921 return; | |
922 } | |
158
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
923 |
90 | 924 if (as -> passnum == 1) |
925 return; | |
926 | |
158
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
927 if (!(l -> sym)) |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
928 { |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
929 // look for symbol after op |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
930 if (**p) |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
931 { |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
932 for (sym2 = *p; **p && !isspace(**p); (*p)++) |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
933 /* do nothing */ |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
934 sym3 = lwasm_alloc(*p - sym2 + 1); |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
935 memcpy(sym3, sym2, *p - sym2); |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
936 sym3[*p - sym2] = '\0'; |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
937 |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
938 l -> sym = sym3; |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
939 } |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
940 } |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
941 |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
942 if (!(l -> sym)) |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
943 { |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
944 register_error(as, l, 2, "No symbol"); |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
945 return; |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
946 } |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
947 |
f0527dc3804d
Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
lost
parents:
151
diff
changeset
|
948 |
90 | 949 // the symbol better be defined at this point (pass 2) |
950 // local symbols cannot be exported nor can "global" symbols | |
951 se = lwasm_find_symbol(as, l -> sym, -1); | |
952 if (!se) | |
953 { | |
954 register_error(as, l, 2, "Exported symbols must be fully defined within a section"); | |
955 return; | |
956 } | |
957 if (se -> sect == NULL) | |
958 { | |
959 register_error(as, l, 2, "Only non-local symbols within a section can be exported"); | |
960 return; | |
961 } | |
962 | |
963 if (se -> flags & SYMBOL_SET) | |
964 { | |
965 register_error(as, l, 2, "You cannot export symbols defined with SET"); | |
966 return; | |
967 } | |
968 | |
969 // if the symbol is not already a simple value, re-evaluate it | |
970 // and see if it becomes simple | |
971 | |
972 | |
973 if (se -> flags & SYMBOL_COMPLEX) | |
974 { | |
975 register_error(as, l, 2, "Exported symbols must be fully resolved on pass 2"); | |
976 return; | |
977 } | |
978 | |
979 // search for existing export | |
980 for (ex = se -> sect -> exports; ex; ex = ex -> next) | |
981 if (!strcmp(l -> sym, ex -> sym)) | |
982 break; | |
983 if (ex) | |
984 { | |
985 register_error(as, l, 2, "Symbol %s already exported", l -> sym); | |
986 return; | |
987 } | |
988 | |
989 // add an external reference | |
990 ex = lwasm_alloc(sizeof(export_list_t)); | |
991 ex -> next = se -> sect -> exports; | |
992 se -> sect -> exports = ex; | |
993 ex -> offset = se -> value; | |
994 ex -> sym = lwasm_strdup(se -> sym); | |
995 } |