Mercurial > hg-old > index.cgi
annotate src/insn_indexed.c @ 95:f3497072ac44
Fixed think-o in rm* ops (reversed pass number test)
author | lost |
---|---|
date | Sat, 17 Jan 2009 06:57:58 +0000 |
parents | e12edcfbebd5 |
children | f59c0916753d |
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; |
81 | 70 |
71 // initialize output bytes | |
32 | 72 *b1 = *b2 = *b3 = -1; |
73 | |
81 | 74 // fetch out operand for lookup |
32 | 75 for (i = 0; i < 24; i++) |
76 { | |
77 if (*((*p) + i) && !isspace(*((*p) + i))) | |
78 stbuf[i] = *((*p) + i); | |
79 else | |
80 break; | |
81 } | |
82 stbuf[i] = '\0'; | |
81 | 83 |
84 // now look up operand in "simple" table | |
32 | 85 if (!*((*p) + i) || isspace(*((*p) + i))) |
86 { | |
87 // do simple lookup | |
88 for (j = 0; simpleindex[j].opstr[0]; j++) | |
89 { | |
90 if (!strcasecmp(stbuf, simpleindex[j].opstr)) | |
91 break; | |
92 } | |
93 if (simpleindex[j].opstr[0]) | |
94 { | |
95 *b1 = simpleindex[j].pb; | |
96 (*p) += i; | |
97 return; | |
98 } | |
99 } | |
81 | 100 |
101 // now do the "hard" ones | |
102 | |
103 // is it indirect? | |
32 | 104 if (**p == '[') |
105 { | |
106 indir = 1; | |
107 (*p)++; | |
108 } | |
109 | |
81 | 110 // look for a "," - all indexed modes have a "," except extended indir |
32 | 111 rn = 0; |
112 for (i = 0; (*p)[i] && !isspace((*p)[i]); i++) | |
113 { | |
114 if ((*p)[i] == ',') | |
115 { | |
116 rn = 1; | |
117 break; | |
118 } | |
119 } | |
81 | 120 |
121 // if no "," and indirect, do extended indir | |
32 | 122 if (!rn && indir) |
123 { | |
124 // extended indir | |
125 *b1 = 0x9f; | |
126 *b2 = 0; | |
127 *b3 = 0; | |
81 | 128 r = lwasm_expr_result2(as, l, (char **)p, 0, &v, 0); |
129 if (r < 0) | |
32 | 130 { |
131 return; | |
132 } | |
133 if (**p != ']') | |
134 { | |
135 register_error(as, l, 1, "Bad operand"); | |
136 return; | |
137 } | |
81 | 138 |
32 | 139 (*p)++; |
81 | 140 |
141 if (r == 1 && as -> passnum == 2) | |
32 | 142 { |
81 | 143 l -> relocoff = as -> addr - l -> codeaddr + 1; |
32 | 144 } |
81 | 145 |
146 *b2 = (v >> 8) & 0xff; | |
147 *b3 = v & 0xff; | |
32 | 148 return; |
149 } | |
150 | |
81 | 151 // if we've previously forced the offset size, make a note of it |
152 if (l -> fsize == 1) | |
153 f8 = 1; | |
154 else if (l -> fsize == 2) | |
155 f16 = 1; | |
156 | |
32 | 157 if (**p == '<') |
158 { | |
159 f8 = 1; | |
160 (*p)++; | |
161 } | |
162 else if (**p == '>') | |
163 { | |
164 f16 = 1; | |
165 (*p)++; | |
166 } | |
167 | |
168 if (**p == '0' && *(*p+1) == ',') | |
169 { | |
170 f0 = 1; | |
171 } | |
172 | |
173 // now we have to evaluate the expression | |
81 | 174 r = lwasm_expr_result2(as, l, (char **)p, 0, &v, 0); |
175 if (r < 0) | |
32 | 176 { |
177 return; | |
178 } | |
81 | 179 if (f8 && r != 0) |
32 | 180 { |
81 | 181 register_error(as, l, 2, "Illegal external or inter-section reference"); |
182 r = 0; | |
183 v = 0; | |
32 | 184 } |
81 | 185 |
32 | 186 // now look for a comma; if not present, explode |
187 if (*(*p)++ != ',') | |
188 { | |
189 // syntax error; force 0 bit | |
190 *b1 = 00; | |
81 | 191 l -> fsize = 0; |
32 | 192 return; |
193 } | |
194 | |
195 // now get the register | |
196 rn = lwasm_lookupreg3(regs, p); | |
197 if (rn < 0) | |
81 | 198 { |
199 *b1 = 0; | |
200 l -> fsize = 0; | |
201 register_error(as, l, 1, "Bad register"); | |
202 return; | |
203 } | |
204 | |
32 | 205 if (indir) |
206 { | |
207 if (**p != ']') | |
81 | 208 { |
209 register_error(as, l, 1, "Bad operand"); | |
210 l -> fsize = 0; | |
211 *b1 = 0; | |
212 return; | |
213 } | |
32 | 214 else |
215 (*p)++; | |
216 } | |
217 | |
81 | 218 // incomplete reference on pass 1 forces 16 bit |
219 if (r == 1 && as -> passnum == 1) | |
220 { | |
221 f16 = 1; | |
222 l -> fsize = 2; | |
223 } | |
224 | |
225 // incomplete reference on pass 2 needs relocoff set | |
226 if (r == 1 && as -> passnum == 2) | |
227 { | |
228 l -> relocoff = as -> addr - l -> codeaddr + 1; | |
229 } | |
230 | |
231 // nnnn,W is only 16 bit (or 0 bit) | |
32 | 232 if (rn == 4) |
233 { | |
234 if (f8) | |
81 | 235 { |
236 register_error(as, l, 1, "n,W cannot be 8 bit"); | |
237 l -> fsize = 0; | |
238 *b1 = 0; | |
239 return; | |
240 } | |
241 // note: set f16 above for incomplete references | |
242 // also set reloc offset | |
243 if (!f16 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE) && v == 0) | |
32 | 244 { |
245 if (indir) | |
246 *b1 = 0x90; | |
247 else | |
248 *b1 = 0x8f; | |
249 return; | |
250 } | |
81 | 251 |
32 | 252 if (indir) |
253 *b1 = 0xb0; | |
254 else | |
255 *b1 = 0xcf; | |
81 | 256 *b2 = (v >> 8) & 0xff; |
257 *b3 = v & 0xff; | |
32 | 258 return; |
259 } | |
260 | |
81 | 261 // set indir to correct bit value |
32 | 262 if (indir) indir = 0x10; |
263 | |
81 | 264 // PCR? then we have PC relative addressing (like B??, LB??) |
32 | 265 if (rn == 5) |
266 { | |
81 | 267 // FIXME: handle external references sensibly |
268 v -= as -> addr; | |
32 | 269 |
270 // we have a slight problem here | |
271 // PCR based on current insn loc is really | |
272 // -125 <= offset <= +130 (8 bit offset) | |
273 // NOTE: when we are called, we already have the opcode emitted | |
274 // so we only need to worry about the size of the operand | |
275 // hence the 2 and 3 magic numbers below instead of 3 and 4 | |
276 // (and that also avoids errors with two byte opcodes, etc) | |
81 | 277 if (f8 || (!f16 && v >= -125 && v <= 130)) |
32 | 278 { |
279 *b1 = indir | 0x8C; | |
81 | 280 v -= 2; |
281 if (v < -128 || v > 127) | |
32 | 282 register_error(as, l, 2, "Byte overflow"); |
81 | 283 *b2 = v & 0xff; |
32 | 284 return; |
285 } | |
286 | |
287 // anything else is 16 bit offset | |
288 // need 16 bit | |
289 *b1 = indir | 0x8D; | |
81 | 290 v -= 3; |
291 *b2 = (v >> 8) & 0xff; | |
292 *b3 = v & 0xff; | |
32 | 293 return; |
294 } | |
295 | |
81 | 296 // constant offset from PC (using PC as regular register :) ) |
297 // FIXME: handle external references intelligently | |
32 | 298 if (rn == 6) |
299 { | |
81 | 300 if (f8 || (!f16 && v >= -128 && v <= 127)) |
32 | 301 { |
302 *b1 = indir | 0x8C; | |
81 | 303 if (v < -128 || v > 127) |
32 | 304 register_error(as, l, 2, "Byte overflow"); |
81 | 305 *b2 = v & 0xff; |
32 | 306 return; |
307 } | |
308 | |
309 // everything else must be 16 bit | |
310 // need 16 bit | |
311 *b1 = indir | 0x8D; | |
81 | 312 *b2 = (v >> 8) & 0xff; |
313 *b3 = v & 0xff; | |
32 | 314 return; |
315 } | |
316 | |
317 // we only have to deal with x,y,u,s here | |
81 | 318 if (!f8 && !f16 && v >= -16 && v <= 15) |
32 | 319 { |
81 | 320 // zero offset going to ,R? |
321 if (v == 0 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE)) | |
32 | 322 { |
323 *b1 = rn << 5 | indir | 0x80 | 0x04; | |
324 return; | |
325 } | |
81 | 326 |
327 // no 5 bit on indirect | |
32 | 328 if (indir) |
329 { | |
330 f8 = 1; | |
81 | 331 l -> fsize = 1; |
32 | 332 goto no5bit; |
333 } | |
81 | 334 |
335 // 5 bit addressing | |
336 *b1 = rn << 5 | (v & 0x1F); | |
32 | 337 return; |
338 } | |
81 | 339 |
32 | 340 no5bit: |
81 | 341 if (f16 || (!f8 && (v < -128 || v > 127))) |
32 | 342 { |
343 // must be a 16 bit offset here | |
344 *b1 = rn << 5 | indir | 0x80 | 0x09; | |
81 | 345 *b2 = (v >> 8) & 0xff; |
346 *b3 = v & 0xff; | |
32 | 347 return; |
348 } | |
349 | |
350 // if we're here, we have an 8 bit offset | |
81 | 351 // note: cannot get here if incomplete reference detected above |
32 | 352 *b1 = rn << 5 | indir | 0x80 | 0x08; |
81 | 353 if (v < -128 || v > 127) |
32 | 354 register_error(as, l, 2, "Byte overflow"); |
81 | 355 *b2 = v & 0xff; |
32 | 356 return; |
357 } | |
358 | |
359 OPFUNC(insn_indexed) | |
360 { | |
361 int b1, b2, b3; | |
362 | |
363 lwasm_emitop(as, l, instab[opnum].ops[0]); | |
364 | |
365 insn_indexed_aux(as, l, (const char **)p, &b1, &b2, &b3); | |
366 if (b1 != -1) | |
367 lwasm_emit(as, l, b1); | |
368 if (b2 != -1) | |
369 lwasm_emit(as, l, b2); | |
370 if (b3 != -1) | |
371 lwasm_emit(as, l, b3); | |
372 } |