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