comparison src/index.c @ 0:57495da01900

Initial checking of LWASM
author lost
date Fri, 03 Oct 2008 02:44:20 +0000
parents
children 34568fab6058
comparison
equal deleted inserted replaced
-1:000000000000 0:57495da01900
1 /*
2 * index.c
3 *
4 * code for parsing indexed addressing modes
5 */
6
7 #include <ctype.h>
8 //#include <errno.h>
9 //#include <stdio.h>
10 //#include <stdlib.h>
11 #include <string.h>
12 #define __index_c_seen__
13 //#include "instab.h"
14 #include "lwasm.h"
15 #include "instab.h"
16
17
18 int parse_index_expr(asmstate_t *as, sourceline_t *cl, char **optr, int *postbyte, int *opmode, int *v1)
19 {
20 static const char *regs = "X Y U S W PCRPC ";
21 static const struct { char *opstr; int pb; } simpleindex[] =
22 {
23 {",x", 0x84}, {",y", 0xa4}, {",u", 0xc4}, {",s", 0xe4},
24 {",x+", 0x80}, {",y+", 0xa0}, {",u+", 0xc0}, {",s+", 0xe0},
25 {",x++", 0x81}, {",y++", 0xa1}, {",u++", 0xc1}, {",s++", 0xe1},
26 {",-x", 0x82}, {",-y", 0xa2}, {",-u", 0xc2}, {",-s", 0xe2},
27 {",--x", 0x83}, {",--y", 0xa3}, {",--u", 0xc3}, {",--s", 0xe3},
28 {"a,x", 0x86}, {"a,y", 0xa6}, {"a,u", 0xc6}, {"a,s", 0xe6},
29 {"b,x", 0x85}, {"b,y", 0xa5}, {"b,u", 0xc5}, {"b,s", 0xe5},
30 {"e,x", 0x87}, {"e,y", 0xa7}, {"e,u", 0xc7}, {"e,s", 0xe7},
31 {"f,x", 0x8a}, {"f,y", 0xaa}, {"f,u", 0xca}, {"f,s", 0xea},
32 {"d,x", 0x8b}, {"d,y", 0xab}, {"d,u", 0xcb}, {"d,s", 0xed},
33 {"w,x", 0x8e}, {"w,y", 0xae}, {"w,u", 0xce}, {"w,s", 0xee},
34 {",w", 0x8f}, {",w++", 0xcf}, {",--w", 0xef},
35
36 {"[,x]", 0x94}, {"[,y]", 0xb4}, {"[,u", 0xd4}, {"[,s]", 0xf4},
37 {"[,x++]", 0x91}, {"[,y++]", 0xb1}, {"[,u++]", 0xd1}, {"[,s++]", 0xf1},
38 {"[,--x]", 0x93}, {"[,--y]", 0xb3}, {"[,--u]", 0xd3}, {"[,--s]", 0xf3},
39 {"[a,x]", 0x96}, {"[a,y]", 0xb6}, {"[a,u]", 0xd6}, {"[a,s]", 0xf6},
40 {"[b,x]", 0x95}, {"[b,y]", 0xb5}, {"[b,u]", 0xd5}, {"[b,s]", 0xf5},
41 {"[e,x]", 0x97}, {"[e,y]", 0xb7}, {"[e,u]", 0xd7}, {"[e,s]", 0xf7},
42 {"[f,x]", 0x9a}, {"[f,y]", 0xba}, {"[f,u]", 0xda}, {"[f,s]", 0xfa},
43 {"[d,x]", 0x9b}, {"[d,y]", 0xbb}, {"[d,u]", 0xdb}, {"[d,s]", 0xfd},
44 {"[w,x]", 0x9e}, {"[w,y]", 0xbe}, {"[w,u]", 0xde}, {"[w,s]", 0xfe},
45 {"[,w]", 0x90}, {"[,w++]", 0xd0}, {"[,--w]", 0xf0},
46
47 { "", -1 }
48 };
49 char stbuf[25];
50 int i;
51 int f8 = 0, f16 = 0;
52 int rn;
53 int indir = 0;
54 int rval;
55 int f0 = 0;
56
57 for (i = 0; i < 24; i++)
58 {
59 if (*((*optr) + i) && !isspace(*((*optr) + i)))
60 stbuf[i] = *((*optr) + i);
61 else
62 break;
63 }
64 stbuf[i] = '\0';
65 if (!*((*optr) + i) || isspace(*((*optr) + i)))
66 {
67 int j;
68 // do simple lookup
69 for (j = 0; simpleindex[j].opstr[0]; j++)
70 {
71 if (!strcasecmp(stbuf, simpleindex[j].opstr))
72 break;
73 }
74 if (simpleindex[j].opstr[0])
75 {
76 *postbyte = simpleindex[j].pb;
77 *opmode = OPER_INDEX;
78 (*optr) += i;
79 return 0;
80 }
81 }
82
83 // now we have the hard ones to work out here
84
85 if (**optr == '[')
86 {
87 indir = 1;
88 (*optr)++;
89 }
90
91 rn = 0;
92 for (i = 0; (*optr)[i] && !isspace((*optr)[i]); i++)
93 {
94 if ((*optr)[i] == ',')
95 {
96 rn = 1;
97 break;
98 }
99 }
100
101 if (!rn && indir)
102 {
103 // extended indir
104 *postbyte = 0x9f;
105 *opmode = OPER_EXTIND;
106 rval = eval_expr(as, cl, optr, v1);
107 if (**optr != ']')
108 {
109 errorp1(ERR_BADOPER);
110 }
111 (*optr)++;
112 return 0;
113 }
114
115 if (cl -> p1f16)
116 f16 = 1;
117 if (**optr == '<')
118 {
119 f8 = 1;
120 (*optr)++;
121 }
122 else if (**optr == '>')
123 {
124 f16 = 1;
125 (*optr)++;
126 }
127
128 if (**optr == '0' && *(*optr+1) == ',')
129 {
130 f0 = 1;
131 }
132
133 // now we have to evaluate the expression
134 rval = eval_expr(as, cl, optr, v1);
135
136 if (cl -> undef && as -> passnum == 1)
137 {
138 cl -> p1f16 = 1;
139 f16 = 1;
140 }
141
142 // now look for a comma; if not present, explode
143 if (rval == -1 || *(*optr)++ != ',')
144 {
145 // syntax error; force 0 bit
146 *postbyte = -1;
147 *opmode = OPER_INDEX;
148 return -2;
149 }
150
151 // now get the register
152 rn = lookupreg3(regs, optr);
153 if (rn < 0)
154 goto reterr;
155
156 // debug("Reg: %d\n", rn);
157
158 if (indir && **optr != ']')
159 goto reterr;
160 else
161 (*optr)++;
162
163 // nnnn,W is only 16 bit
164 if (rn == 4)
165 {
166 if (f8)
167 goto reterr;
168 if (!f16 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE) && *v1 == 0)
169 {
170 *opmode = OPER_INDEX;
171 if (indir)
172 *postbyte = 0x90;
173 else
174 *postbyte = 0x8f;
175 return 0;
176 }
177 *opmode = OPER_INDEX16;
178 if (indir)
179 *postbyte = 0xb0;
180 else
181 *postbyte = 0xcf;
182 return 0;
183 }
184
185 if (indir) indir = 0x10;
186
187 // forward ref; we can't figure anything
188 if (cl -> undef)
189 {
190 f16 = 1;
191 }
192
193 // PCR? redo v1, v2 relative to current address
194 if (rn == 5)
195 {
196 *v1 -= as -> addr;
197
198 // we have a slight problem here
199 // PCR based on current insn loc is really
200 // -125 <= offset <= +130 (8 bit offset)
201 if (f8 || (!f16 && *v1 >= -125 && *v1 <= 130))
202 {
203 *opmode = OPER_INDEX8;
204 *postbyte = indir | 0x8C;
205 *v1 -= 3;
206 if (*v1 < -128 || *v1 > 127)
207 errorp2(ERR_OVERFLOW);
208 return 0;
209 }
210
211 // anything else is 16 bit offset
212 // need 16 bit
213 *opmode = OPER_INDEX16;
214 *postbyte = indir | 0x8D;
215 *v1 -= 4;
216 return 0;
217 }
218
219 // constant offset from PC
220 if (rn == 6)
221 {
222 if (f8 || (!f16 && *v1 >= -128 && *v1 <= 127))
223 {
224 *opmode = OPER_INDEX8;
225 *postbyte = indir | 0x8C;
226 if (*v1 < -128 || *v1 > 127)
227 errorp2(ERR_OVERFLOW);
228 return 0;
229 }
230
231 // everything else must be 16 bit
232 // need 16 bit
233 *opmode = OPER_INDEX16;
234 *postbyte = indir | 0x8D;
235 return 0;
236
237 }
238
239 // we only have to deal with x,y,u,s here
240 if (!f8 && !f16 && *v1 >= -16 && *v1 <= 15)
241 {
242 if (*v1 == 0 && !f0 && !(as -> pragmas & PRAGMA_NOINDEX0TONONE))
243 {
244 *postbyte = rn << 5 | indir | 0x80 | 0x04;
245 *opmode = OPER_INDEX;
246 return 0;
247 }
248 // we pick the smallest value here
249 if (indir)
250 {
251 f8 = 1;
252 goto no5bit;
253 }
254 *postbyte = rn << 5 | (*v1 & 0x1F);
255 *opmode = OPER_INDEX5;
256 return 0;
257 }
258 no5bit:
259 if (f16 || (!f8 && (*v1 < -128 || *v1 > 127)))
260 {
261 // must be a 16 bit offset here
262 *postbyte = rn << 5 | indir | 0x80 | 0x09;
263 *opmode = OPER_INDEX16;
264 return 0;
265 }
266
267 // if we're here, we have an 8 bit offset
268 *postbyte = rn << 5 | indir | 0x80 | 0x08;
269 *opmode = OPER_INDEX8;
270 if (*v1 < -128 || *v1 > 127)
271 errorp2(ERR_OVERFLOW);
272 return 0;
273 reterr:
274 *postbyte = -1;
275 *opmode = OPER_INDEX;
276 return -2;
277 }
278
279 void insn_indexed_aux(asmstate_t *as, sourceline_t *cl, char **optr, int *b1, int *b2, int *b3)
280 {
281 int rval;
282 int v1, pb = -1;
283
284 *b1 = *b2 = *b3 = -1;
285
286 rval = parse_index_expr(as, cl, optr, &pb, &(cl -> addrmode), &v1);
287 if (rval < 0)
288 errorp1(ERR_BADOPER);
289 *b1 = pb;
290
291 if (cl -> addrmode == OPER_INDEX8)
292 {
293 *b2 = v1 & 0xff;
294 }
295 else if (cl -> addrmode == OPER_INDEX16 || cl -> addrmode == OPER_EXTIND)
296 {
297 *b2 = (v1 & 0xffff) >> 8;
298 *b3 = v1 & 0xff;
299 }
300 }
301
302 void insn_indexed(asmstate_t *as, sourceline_t *cl, char **optr)
303 {
304 int b1, b2, b3;
305
306 emitop(instab[cl -> opcode].ops[0]);
307 insn_indexed_aux(as, cl, optr, &b1, &b2, &b3);
308 if (b1 != -1)
309 emit(b1);
310 if (b2 != -1)
311 emit(b2);
312 if (b3 != -1)
313 emit(b3);
314 }
315