Mercurial > hg-old > index.cgi
annotate lwasm/insn_indexed.c @ 254:c7a41b4c89b3 2.x
Added struct support to LWASM
author | lost |
---|---|
date | Sat, 19 Dec 2009 06:38:43 +0000 |
parents | 0df2a39a268c |
children |
rev | line source |
---|---|
32 | 1 /* |
2 insn_indexed.c | |
33
74a3fef7c8d0
Added general addressing modes (immediate, base page, extended, indexed)
lost
parents:
32
diff
changeset
|
3 Copyright © 2009 William Astle |
32 | 4 |
5 This file is part of LWASM. | |
6 | |
7 LWASM is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 /* | |
22 for handling indexed mode instructions | |
23 */ | |
24 | |
25 #define __insn_indexed_c_seen__ | |
26 | |
212 | 27 #include <config.h> |
28 | |
32 | 29 #include <ctype.h> |
30 #include <string.h> | |
31 | |
32 #include "lwasm.h" | |
33 #include "instab.h" | |
34 #include "expr.h" | |
35 | |
36 void insn_indexed_aux(asmstate_t *as, lwasm_line_t *l, const char **p, int *b1, int *b2, int *b3) | |
37 { | |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
38 struct opvals { char *opstr; int pb; }; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
39 |
32 | 40 static const char *regs = "X Y U S W PCRPC "; |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
41 static const struct opvals simpleindex[] = |
32 | 42 { |
43 {",x", 0x84}, {",y", 0xa4}, {",u", 0xc4}, {",s", 0xe4}, | |
44 {",x+", 0x80}, {",y+", 0xa0}, {",u+", 0xc0}, {",s+", 0xe0}, | |
45 {",x++", 0x81}, {",y++", 0xa1}, {",u++", 0xc1}, {",s++", 0xe1}, | |
46 {",-x", 0x82}, {",-y", 0xa2}, {",-u", 0xc2}, {",-s", 0xe2}, | |
47 {",--x", 0x83}, {",--y", 0xa3}, {",--u", 0xc3}, {",--s", 0xe3}, | |
48 {"a,x", 0x86}, {"a,y", 0xa6}, {"a,u", 0xc6}, {"a,s", 0xe6}, | |
49 {"b,x", 0x85}, {"b,y", 0xa5}, {"b,u", 0xc5}, {"b,s", 0xe5}, | |
50 {"e,x", 0x87}, {"e,y", 0xa7}, {"e,u", 0xc7}, {"e,s", 0xe7}, | |
51 {"f,x", 0x8a}, {"f,y", 0xaa}, {"f,u", 0xca}, {"f,s", 0xea}, | |
52 {"d,x", 0x8b}, {"d,y", 0xab}, {"d,u", 0xcb}, {"d,s", 0xed}, | |
53 {"w,x", 0x8e}, {"w,y", 0xae}, {"w,u", 0xce}, {"w,s", 0xee}, | |
54 {",w", 0x8f}, {",w++", 0xcf}, {",--w", 0xef}, | |
55 | |
56 {"[,x]", 0x94}, {"[,y]", 0xb4}, {"[,u", 0xd4}, {"[,s]", 0xf4}, | |
57 {"[,x++]", 0x91}, {"[,y++]", 0xb1}, {"[,u++]", 0xd1}, {"[,s++]", 0xf1}, | |
58 {"[,--x]", 0x93}, {"[,--y]", 0xb3}, {"[,--u]", 0xd3}, {"[,--s]", 0xf3}, | |
59 {"[a,x]", 0x96}, {"[a,y]", 0xb6}, {"[a,u]", 0xd6}, {"[a,s]", 0xf6}, | |
60 {"[b,x]", 0x95}, {"[b,y]", 0xb5}, {"[b,u]", 0xd5}, {"[b,s]", 0xf5}, | |
61 {"[e,x]", 0x97}, {"[e,y]", 0xb7}, {"[e,u]", 0xd7}, {"[e,s]", 0xf7}, | |
62 {"[f,x]", 0x9a}, {"[f,y]", 0xba}, {"[f,u]", 0xda}, {"[f,s]", 0xfa}, | |
63 {"[d,x]", 0x9b}, {"[d,y]", 0xbb}, {"[d,u]", 0xdb}, {"[d,s]", 0xfd}, | |
64 {"[w,x]", 0x9e}, {"[w,y]", 0xbe}, {"[w,u]", 0xde}, {"[w,s]", 0xfe}, | |
65 {"[,w]", 0x90}, {"[,w++]", 0xd0}, {"[,--w]", 0xf0}, | |
66 | |
67 { "", -1 } | |
68 }; | |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
69 |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
70 static const char *regs9 = "X Y U S PCRPC "; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
71 static const struct opvals simpleindex9[] = |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
72 { |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
73 {",x", 0x84}, {",y", 0xa4}, {",u", 0xc4}, {",s", 0xe4}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
74 {",x+", 0x80}, {",y+", 0xa0}, {",u+", 0xc0}, {",s+", 0xe0}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
75 {",x++", 0x81}, {",y++", 0xa1}, {",u++", 0xc1}, {",s++", 0xe1}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
76 {",-x", 0x82}, {",-y", 0xa2}, {",-u", 0xc2}, {",-s", 0xe2}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
77 {",--x", 0x83}, {",--y", 0xa3}, {",--u", 0xc3}, {",--s", 0xe3}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
78 {"a,x", 0x86}, {"a,y", 0xa6}, {"a,u", 0xc6}, {"a,s", 0xe6}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
79 {"b,x", 0x85}, {"b,y", 0xa5}, {"b,u", 0xc5}, {"b,s", 0xe5}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
80 {"d,x", 0x8b}, {"d,y", 0xab}, {"d,u", 0xcb}, {"d,s", 0xed}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
81 |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
82 {"[,x]", 0x94}, {"[,y]", 0xb4}, {"[,u", 0xd4}, {"[,s]", 0xf4}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
83 {"[,x++]", 0x91}, {"[,y++]", 0xb1}, {"[,u++]", 0xd1}, {"[,s++]", 0xf1}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
84 {"[,--x]", 0x93}, {"[,--y]", 0xb3}, {"[,--u]", 0xd3}, {"[,--s]", 0xf3}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
85 {"[a,x]", 0x96}, {"[a,y]", 0xb6}, {"[a,u]", 0xd6}, {"[a,s]", 0xf6}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
86 {"[b,x]", 0x95}, {"[b,y]", 0xb5}, {"[b,u]", 0xd5}, {"[b,s]", 0xf5}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
87 {"[d,x]", 0x9b}, {"[d,y]", 0xbb}, {"[d,u]", 0xdb}, {"[d,s]", 0xfd}, |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
88 |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
89 { "", -1 } |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
90 }; |
32 | 91 char stbuf[25]; |
81 | 92 int i, j, rn; |
93 int f8 = 0, f16 = 0, f0 = 0; | |
94 int r, v; | |
32 | 95 int indir = 0; |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
96 int fs8 = 0, fs16 = 0; |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
97 const struct opvals *simples; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
98 const char *reglist; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
99 |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
100 if (as -> no6309) |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
101 { |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
102 simples = simpleindex9; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
103 reglist = regs9; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
104 } |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
105 else |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
106 { |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
107 simples = simpleindex; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
108 reglist = regs; |
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
109 } |
81 | 110 |
111 // initialize output bytes | |
32 | 112 *b1 = *b2 = *b3 = -1; |
113 | |
81 | 114 // fetch out operand for lookup |
32 | 115 for (i = 0; i < 24; i++) |
116 { | |
117 if (*((*p) + i) && !isspace(*((*p) + i))) | |
118 stbuf[i] = *((*p) + i); | |
119 else | |
120 break; | |
121 } | |
122 stbuf[i] = '\0'; | |
81 | 123 |
124 // now look up operand in "simple" table | |
32 | 125 if (!*((*p) + i) || isspace(*((*p) + i))) |
126 { | |
127 // do simple lookup | |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
128 for (j = 0; simples[j].opstr[0]; j++) |
32 | 129 { |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
130 if (!strcasecmp(stbuf, simples[j].opstr)) |
32 | 131 break; |
132 } | |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
133 if (simples[j].opstr[0]) |
32 | 134 { |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
135 *b1 = simples[j].pb; |
32 | 136 (*p) += i; |
137 return; | |
138 } | |
139 } | |
81 | 140 |
141 // now do the "hard" ones | |
142 | |
143 // is it indirect? | |
32 | 144 if (**p == '[') |
145 { | |
146 indir = 1; | |
147 (*p)++; | |
148 } | |
149 | |
81 | 150 // look for a "," - all indexed modes have a "," except extended indir |
32 | 151 rn = 0; |
152 for (i = 0; (*p)[i] && !isspace((*p)[i]); i++) | |
153 { | |
154 if ((*p)[i] == ',') | |
155 { | |
156 rn = 1; | |
157 break; | |
158 } | |
159 } | |
81 | 160 |
161 // if no "," and indirect, do extended indir | |
32 | 162 if (!rn && indir) |
163 { | |
164 // extended indir | |
165 *b1 = 0x9f; | |
166 *b2 = 0; | |
167 *b3 = 0; | |
81 | 168 r = lwasm_expr_result2(as, l, (char **)p, 0, &v, 0); |
169 if (r < 0) | |
32 | 170 { |
171 return; | |
172 } | |
173 if (**p != ']') | |
174 { | |
175 register_error(as, l, 1, "Bad operand"); | |
176 return; | |
177 } | |
81 | 178 |
32 | 179 (*p)++; |
81 | 180 |
181 if (r == 1 && as -> passnum == 2) | |
32 | 182 { |
81 | 183 l -> relocoff = as -> addr - l -> codeaddr + 1; |
32 | 184 } |
81 | 185 |
186 *b2 = (v >> 8) & 0xff; | |
187 *b3 = v & 0xff; | |
32 | 188 return; |
189 } | |
190 | |
81 | 191 // if we've previously forced the offset size, make a note of it |
192 if (l -> fsize == 1) | |
193 f8 = 1; | |
194 else if (l -> fsize == 2) | |
195 f16 = 1; | |
196 | |
32 | 197 if (**p == '<') |
198 { | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
199 fs8 = 1; |
32 | 200 (*p)++; |
201 } | |
202 else if (**p == '>') | |
203 { | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
204 fs16 = 1; |
32 | 205 (*p)++; |
206 } | |
207 | |
208 if (**p == '0' && *(*p+1) == ',') | |
209 { | |
210 f0 = 1; | |
211 } | |
212 | |
213 // now we have to evaluate the expression | |
81 | 214 r = lwasm_expr_result2(as, l, (char **)p, 0, &v, 0); |
215 if (r < 0) | |
32 | 216 { |
217 return; | |
218 } | |
219 // now look for a comma; if not present, explode | |
220 if (*(*p)++ != ',') | |
221 { | |
222 // syntax error; force 0 bit | |
223 *b1 = 00; | |
81 | 224 l -> fsize = 0; |
32 | 225 return; |
226 } | |
227 | |
228 // now get the register | |
230
0df2a39a268c
Added --6809/--6309 switches and some cleanups in error reporting
lost
parents:
212
diff
changeset
|
229 rn = lwasm_lookupreg3(reglist, p); |
32 | 230 if (rn < 0) |
81 | 231 { |
232 *b1 = 0; | |
233 l -> fsize = 0; | |
234 register_error(as, l, 1, "Bad register"); | |
235 return; | |
236 } | |
237 | |
32 | 238 if (indir) |
239 { | |
240 if (**p != ']') | |
81 | 241 { |
242 register_error(as, l, 1, "Bad operand"); | |
243 l -> fsize = 0; | |
244 *b1 = 0; | |
245 return; | |
246 } | |
32 | 247 else |
248 (*p)++; | |
249 } | |
250 | |
81 | 251 // incomplete reference on pass 1 forces 16 bit |
252 if (r == 1 && as -> passnum == 1) | |
253 { | |
254 f16 = 1; | |
255 l -> fsize = 2; | |
256 } | |
257 | |
258 // incomplete reference on pass 2 needs relocoff set | |
259 if (r == 1 && as -> passnum == 2) | |
260 { | |
261 l -> relocoff = as -> addr - l -> codeaddr + 1; | |
262 } | |
263 | |
264 // nnnn,W is only 16 bit (or 0 bit) | |
32 | 265 if (rn == 4) |
266 { | |
267 if (f8) | |
81 | 268 { |
269 register_error(as, l, 1, "n,W cannot be 8 bit"); | |
270 l -> fsize = 0; | |
271 *b1 = 0; | |
272 return; | |
273 } | |
274 // note: set f16 above for incomplete references | |
275 // also set reloc offset | |
276 if (!f16 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE) && v == 0) | |
32 | 277 { |
278 if (indir) | |
279 *b1 = 0x90; | |
280 else | |
281 *b1 = 0x8f; | |
282 return; | |
283 } | |
81 | 284 |
32 | 285 if (indir) |
286 *b1 = 0xb0; | |
287 else | |
288 *b1 = 0xcf; | |
81 | 289 *b2 = (v >> 8) & 0xff; |
290 *b3 = v & 0xff; | |
32 | 291 return; |
292 } | |
293 | |
81 | 294 // set indir to correct bit value |
32 | 295 if (indir) indir = 0x10; |
296 | |
81 | 297 // PCR? then we have PC relative addressing (like B??, LB??) |
32 | 298 if (rn == 5) |
299 { | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
300 lwasm_expr_term_t *t; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
301 // external references are handled exactly the same as for |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
302 // relative addressing modes |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
303 // on pass 1, adjust the expression for a subtraction of the |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
304 // current address |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
305 |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
306 // need to re-evaluate the expression with "SECTCONST"... |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
307 r = lwasm_expr_result2(as, l, (char **)p, EXPR_SECTCONST | EXPR_REEVAL, &v, 0); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
308 if (r != 0) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
309 v = 0; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
310 if (as -> passnum == 1) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
311 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
312 l -> fsize = 0; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
313 } |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
314 f8 = f16 = 0; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
315 if (r == 1 && as -> passnum == 1 && !fs8) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
316 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
317 l -> fsize = 2; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
318 f16 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
319 } |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
320 if (fs8) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
321 f8 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
322 if (fs16) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
323 f16 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
324 if (l -> fsize == 2) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
325 f16 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
326 else if (l -> fsize == 1) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
327 f8 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
328 if (as -> passnum == 1) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
329 v -= as -> addr; |
32 | 330 |
331 // we have a slight problem here | |
332 // PCR based on current insn loc is really | |
333 // -125 <= offset <= +130 (8 bit offset) | |
334 // NOTE: when we are called, we already have the opcode emitted | |
335 // so we only need to worry about the size of the operand | |
336 // hence the 2 and 3 magic numbers below instead of 3 and 4 | |
337 // (and that also avoids errors with two byte opcodes, etc) | |
81 | 338 if (f8 || (!f16 && v >= -125 && v <= 130)) |
32 | 339 { |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
340 f8 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
341 l -> fsize = 1; |
32 | 342 *b1 = indir | 0x8C; |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
343 if (as -> passnum == 1) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
344 v -= 2; |
81 | 345 if (v < -128 || v > 127) |
32 | 346 register_error(as, l, 2, "Byte overflow"); |
81 | 347 *b2 = v & 0xff; |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
348 if (r != 0 && as -> passnum == 2) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
349 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
350 register_error(as, l, 2, "Illegal incomplete reference"); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
351 } |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
352 goto finpcr; |
32 | 353 } |
354 | |
355 // anything else is 16 bit offset | |
356 // need 16 bit | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
357 |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
358 l -> fsize = 2; |
32 | 359 *b1 = indir | 0x8D; |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
360 if (as -> passnum == 1) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
361 v -= 3; |
81 | 362 *b2 = (v >> 8) & 0xff; |
363 *b3 = v & 0xff; | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
364 if (as -> passnum == 2 && r == 1) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
365 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
366 t = lwasm_expr_term_create_secbase(); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
367 lwasm_expr_stack_push(l -> exprs[0], t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
368 lwasm_expr_term_free(t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
369 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
370 lwasm_expr_stack_push(l -> exprs[0], t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
371 lwasm_expr_term_free(t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
372 } |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
373 |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
374 finpcr: |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
375 if (as -> passnum == 1) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
376 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
377 // need to adjust the expression |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
378 if (l -> exprs[0]) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
379 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
380 t = lwasm_expr_term_create_int(as -> addr + (f8 ? 2 : 3)); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
381 lwasm_expr_stack_push(l -> exprs[0], t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
382 lwasm_expr_term_free(t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
383 t = lwasm_expr_term_create_oper(LWASM_OPER_MINUS); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
384 lwasm_expr_stack_push(l -> exprs[0], t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
385 lwasm_expr_term_free(t); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
386 } |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
387 else |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
388 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
389 l -> exprvals[0] -= as -> addr + (f8 ? 2 : 3); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
390 } |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
391 } |
32 | 392 return; |
393 } | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
394 if (fs16) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
395 f16 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
396 if (fs8) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
397 f8 = 1; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
398 |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
399 if (f8 && r != 0) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
400 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
401 register_error(as, l, 2, "Illegal external or inter-section reference"); |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
402 r = 0; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
403 v = 0; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
84
diff
changeset
|
404 } |
32 | 405 |
81 | 406 // constant offset from PC (using PC as regular register :) ) |
407 // FIXME: handle external references intelligently | |
32 | 408 if (rn == 6) |
409 { | |
81 | 410 if (f8 || (!f16 && v >= -128 && v <= 127)) |
32 | 411 { |
412 *b1 = indir | 0x8C; | |
81 | 413 if (v < -128 || v > 127) |
32 | 414 register_error(as, l, 2, "Byte overflow"); |
81 | 415 *b2 = v & 0xff; |
32 | 416 return; |
417 } | |
418 | |
419 // everything else must be 16 bit | |
420 // need 16 bit | |
421 *b1 = indir | 0x8D; | |
81 | 422 *b2 = (v >> 8) & 0xff; |
423 *b3 = v & 0xff; | |
32 | 424 return; |
425 } | |
426 | |
427 // we only have to deal with x,y,u,s here | |
81 | 428 if (!f8 && !f16 && v >= -16 && v <= 15) |
32 | 429 { |
81 | 430 // zero offset going to ,R? |
431 if (v == 0 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE)) | |
32 | 432 { |
433 *b1 = rn << 5 | indir | 0x80 | 0x04; | |
434 return; | |
435 } | |
81 | 436 |
437 // no 5 bit on indirect | |
32 | 438 if (indir) |
439 { | |
440 f8 = 1; | |
81 | 441 l -> fsize = 1; |
32 | 442 goto no5bit; |
443 } | |
81 | 444 |
445 // 5 bit addressing | |
446 *b1 = rn << 5 | (v & 0x1F); | |
32 | 447 return; |
448 } | |
81 | 449 |
32 | 450 no5bit: |
81 | 451 if (f16 || (!f8 && (v < -128 || v > 127))) |
32 | 452 { |
453 // must be a 16 bit offset here | |
454 *b1 = rn << 5 | indir | 0x80 | 0x09; | |
81 | 455 *b2 = (v >> 8) & 0xff; |
456 *b3 = v & 0xff; | |
32 | 457 return; |
458 } | |
459 | |
460 // if we're here, we have an 8 bit offset | |
81 | 461 // note: cannot get here if incomplete reference detected above |
32 | 462 *b1 = rn << 5 | indir | 0x80 | 0x08; |
81 | 463 if (v < -128 || v > 127) |
32 | 464 register_error(as, l, 2, "Byte overflow"); |
81 | 465 *b2 = v & 0xff; |
32 | 466 return; |
467 } | |
468 | |
469 OPFUNC(insn_indexed) | |
470 { | |
471 int b1, b2, b3; | |
472 | |
473 lwasm_emitop(as, l, instab[opnum].ops[0]); | |
474 | |
475 insn_indexed_aux(as, l, (const char **)p, &b1, &b2, &b3); | |
476 if (b1 != -1) | |
477 lwasm_emit(as, l, b1); | |
478 if (b2 != -1) | |
479 lwasm_emit(as, l, b2); | |
480 if (b3 != -1) | |
481 lwasm_emit(as, l, b3); | |
482 } |