339
|
1 /*
|
|
2 insn_gen.c, Copyright © 2009 William Astle
|
|
3
|
|
4 This file is part of LWASM.
|
|
5
|
|
6 LWASM is free software: you can redistribute it and/or modify it under the
|
|
7 terms of the GNU General Public License as published by the Free Software
|
|
8 Foundation, either version 3 of the License, or (at your option) any later
|
|
9 version.
|
|
10
|
|
11 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
14 more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License along with
|
|
17 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
|
19 Contains code for parsing general addressing modes (IMM+DIR+EXT+IND)
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <ctype.h>
|
|
25 #include <stdlib.h>
|
|
26
|
361
|
27 #include <lw_expr.h>
|
|
28
|
339
|
29 #include "lwasm.h"
|
|
30 #include "instab.h"
|
|
31
|
361
|
32 extern void insn_indexed_parse_aux(asmstate_t *as, line_t *l, char **p);
|
|
33 extern void insn_indexed_resolve_aux(asmstate_t *as, line_t *l, int force);
|
|
34 extern void insn_indexed_emit_aux(asmstate_t *as, line_t *l);
|
339
|
35
|
|
36 // "extra" is required due to the way OIM, EIM, TIM, and AIM work
|
361
|
37 void insn_parse_gen_aux(asmstate_t *as, line_t *l, char **p)
|
339
|
38 {
|
|
39 const char *optr2;
|
|
40 int v1, tv, rval;
|
361
|
41 lw_expr_t *s;
|
339
|
42
|
361
|
43 optr2 = *p;
|
339
|
44 while (*optr2 && !isspace(*optr2) && *optr2 != ',') optr2++
|
|
45 /* do nothing */ ;
|
|
46
|
361
|
47 if (*optr2 == ',' || **p == '[')
|
339
|
48 {
|
361
|
49 l -> lint = -1;
|
|
50 l -> lint2 = 1;
|
|
51 insn_parse_indexed_aux(as, l, p);
|
|
52 goto out;
|
|
53 }
|
339
|
54
|
361
|
55 if (**p == '<')
|
|
56 {
|
|
57 (*p)++;
|
|
58 l -> lint2 = 0;
|
|
59 }
|
|
60
|
|
61 // for compatibility with asxxxx
|
|
62 // * followed by a digit, alpha, or _, or ., or ?, or another * is "f8"
|
|
63 else if (**p == '*')
|
|
64 {
|
|
65 tv = *(*p + 1);
|
|
66 if (isdigit(tv) || isalpha(tv) || tv == '_' || tv == '.' || tv == '?' || tv == '@' || tv == '*' || tv == '+' || tv == '-')
|
339
|
67 {
|
361
|
68 l -> lint2 = 0;
|
|
69 (*p)++;
|
339
|
70 }
|
361
|
71 }
|
|
72 else if (**p == '>')
|
|
73 {
|
|
74 (*p)++;
|
|
75 l -> lint2 = 2;
|
|
76 }
|
|
77 else
|
|
78 {
|
|
79 l -> lint2 = -1;
|
|
80 }
|
|
81
|
|
82 s = lwasm_parse_expr(as, p);
|
|
83 if (!s)
|
|
84 {
|
|
85 lwasm_register_error(as, l, "Bad operand");
|
|
86 return;
|
|
87 }
|
|
88
|
|
89 lwasm_save_expr(l, 0, s);
|
|
90
|
|
91 if (as -> output_format == OUTPUT_OBJ && l -> lint2 == -1)
|
|
92 {
|
|
93 l -> lint2 = 2;
|
|
94 goto out;
|
|
95 }
|
|
96
|
|
97 // if we have a constant now, figure out dp vs nondp
|
|
98 if (lw_expr_istype(s, lw_expr_type_int))
|
|
99 {
|
|
100 v1 = lw_expr_intval(s);
|
|
101 if (((v1 >> 8) & 0xff) == (l -> dpval & 0xff))
|
339
|
102 {
|
361
|
103 l -> lint2 = 0;
|
|
104 goto out;
|
339
|
105 }
|
361
|
106 l -> lint2 = 2;
|
|
107 }
|
|
108
|
|
109 out:
|
|
110 if (l -> lint2 != -1)
|
|
111 {
|
|
112 if (l -> lint2 == 0)
|
|
113 {
|
|
114 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
|
|
115 }
|
|
116 else if (l -> lint2 == 2)
|
|
117 {
|
|
118 l -> len = OPLEN(instab[l -> insn].ops[2]) + 2;
|
|
119 }
|
|
120 else if (l -> lint2 == 1 && l -> lint != -1)
|
339
|
121 {
|
361
|
122 l -> len = OPLEN(instab[l -> insn].ops[1]) + l -> lint + 1;
|
339
|
123 }
|
361
|
124 }
|
|
125 }
|
339
|
126
|
361
|
127 void insn_resolve_gen_aux(asmstate_t *as, line_t *l, int force)
|
|
128 {
|
|
129 lw_expr_t *e;
|
|
130
|
|
131 if (l -> lint2 == 1)
|
|
132 {
|
|
133 // indexed
|
|
134 insn_resolve_indexed_aux(as, l, force);
|
|
135 goto out;
|
|
136 }
|
|
137
|
|
138 if (l -> lint2 != -1)
|
|
139 return;
|
|
140
|
|
141 e = lwasm_fetch_expr(l, 0);
|
|
142 if (lw_expr_istype(e, lw_expr_type_int))
|
|
143 {
|
|
144 int v;
|
339
|
145
|
361
|
146 v = lw_expr_intval(e);
|
|
147
|
|
148 if (((v >> 8) & 0xff) == (l -> dpval & 0xff))
|
339
|
149 {
|
361
|
150 l -> lint2 = 0;
|
|
151 goto out;
|
339
|
152 }
|
361
|
153 l -> lint2 = 2;
|
|
154 goto out;
|
|
155 }
|
|
156
|
|
157 if (force)
|
|
158 {
|
|
159 l -> lint2 = 2;
|
|
160 }
|
|
161
|
|
162 out:
|
|
163 if (l -> lint2 != -1)
|
|
164 {
|
|
165 if (l -> lint2 == 0)
|
|
166 {
|
|
167 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
|
|
168 }
|
|
169 else if (l -> lint2 == 2)
|
339
|
170 {
|
361
|
171 l -> len = OPLEN(instab[l -> insn].ops[2]) + 2;
|
339
|
172 }
|
361
|
173 else if (l -> lint2 == 1 && l -> lint != -1)
|
|
174 {
|
|
175 l -> len = OPLEN(instab[l -> insn].ops[1]) + l -> lint + 1;
|
|
176 }
|
|
177 }
|
|
178 }
|
339
|
179
|
362
|
180 void insn_emit_gen_aux(asmstate_t *as, line_t *l, int extra)
|
361
|
181 {
|
|
182 lw_expr_t e;
|
|
183
|
|
184 e = lwasm_fetch_expr(l, 0);
|
|
185 lwasm_emitop(l, instab[l -> insn].ops[l -> lint2]);
|
362
|
186
|
|
187 if (extra != -1)
|
|
188 lwasm_emit(l, extra);
|
|
189
|
361
|
190 if (l, l -> lint2 == 1)
|
|
191 {
|
|
192 lwasm_emit(l, l -> pb);
|
|
193 lwasm_emitexpr(l, e, l -> lint);
|
|
194 return;
|
|
195 }
|
|
196
|
|
197 if (l -> lint2 == 2)
|
|
198 lwasm_emitexpr(l, e, 2);
|
|
199 else
|
|
200 lwasm_emitexpr(l, e, 1);
|
339
|
201 }
|
|
202
|
|
203 // the various insn_gen? functions have an immediate mode of ? bits
|
361
|
204 PARSEFUNC(insn_parse_gen0)
|
339
|
205 {
|
|
206 if (**p == '#')
|
|
207 {
|
361
|
208 lwasm_register_error(as, l, "Immediate mode not allowed");
|
339
|
209 return;
|
|
210 }
|
|
211
|
|
212 // handle non-immediate
|
361
|
213 insn_parse_gen_aux(as, l, p);
|
339
|
214 }
|
|
215
|
361
|
216 RESOLVEFUNC(insn_resolve_gen0)
|
339
|
217 {
|
361
|
218 if (l -> len != -1)
|
|
219 return;
|
|
220
|
|
221 // handle non-immediate
|
|
222 insn_resolve_gen_aux(as, l, force);
|
|
223 }
|
|
224
|
|
225 EMITFUNC(insn_emit_gen0)
|
|
226 {
|
362
|
227 insn_emit_gen_aux(as, l, -1);
|
361
|
228 }
|
|
229
|
|
230 PARSEFUNC(insn_parse_gen8)
|
|
231 {
|
339
|
232 if (**p == '#')
|
|
233 {
|
361
|
234 lw_expr_t e;
|
|
235
|
339
|
236 (*p)++;
|
361
|
237 e = lwasm_parse_expr(as, p);
|
|
238 if (!e)
|
339
|
239 {
|
361
|
240 lwasm_register_error(as, l, "Bad operand");
|
|
241 return;
|
339
|
242 }
|
361
|
243 l -> len = OPLEN(instab[l -> insn].ops[3]) + 1;
|
|
244 l -> lint2 = 3;
|
|
245 lwasm_save_expr(l, 0, e);
|
339
|
246 return;
|
|
247 }
|
|
248
|
361
|
249 // handle non-immediate
|
|
250 insn_parse_gen_aux(as, l, p);
|
|
251 if (l -> lint2 != -1)
|
|
252 {
|
|
253 if (l -> lint2 == 0)
|
|
254 {
|
|
255 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
|
|
256 }
|
|
257 else if (l -> lint2 == 2)
|
|
258 {
|
|
259 l -> len = OPLEN(instab[l -> insn].ops[2]) + 2;
|
|
260 }
|
|
261 else if (l -> lint2 == 1 && l -> lint != -1)
|
|
262 {
|
|
263 l -> len = OPLEN(instab[l -> insn].ops[1]) + l -> lint + 1;
|
|
264 }
|
|
265 }
|
339
|
266 }
|
|
267
|
361
|
268 RESOLVEFUNC(insn_resolve_gen8)
|
|
269 {
|
|
270 if (l -> len != -1)
|
|
271 return;
|
|
272
|
|
273 // handle non-immediate
|
|
274 insn_resolve_gen_aux(as, l, force);
|
|
275 }
|
|
276
|
|
277 EMITFUNC(insn_emit_gen8)
|
339
|
278 {
|
361
|
279 if (l -> lint2 == 3)
|
|
280 {
|
|
281 lw_expr_t e;
|
|
282 e = lwasm_fetch_expr(l, 0);
|
|
283 lwasm_emitop(l, instab[l -> insn].ops[3]);
|
|
284 lwasm_emitexpr(l, e, 1);
|
|
285 return;
|
|
286 }
|
|
287
|
362
|
288 insn_emit_gen_aux(as, l, -1);
|
361
|
289 }
|
|
290
|
|
291 PARSEFUNC(insn_parse_gen16)
|
|
292 {
|
339
|
293 if (**p == '#')
|
|
294 {
|
361
|
295 lw_expr_t e;
|
|
296
|
339
|
297 (*p)++;
|
361
|
298 e = lwasm_parse_expr(as, p);
|
|
299 if (!e)
|
339
|
300 {
|
361
|
301 lwasm_register_error(as, l, "Bad operand");
|
|
302 return;
|
339
|
303 }
|
361
|
304 l -> len = OPLEN(instab[l -> insn].ops[3]) + 2;
|
|
305 l -> lint2 = 3;
|
|
306 lwasm_save_expr(l, 0, e);
|
339
|
307 return;
|
|
308 }
|
|
309
|
361
|
310 // handle non-immediate
|
|
311 insn_parse_gen_aux(as, l, p);
|
|
312 if (l -> lint2 != -1)
|
|
313 {
|
|
314 if (l -> lint2 == 0)
|
|
315 {
|
|
316 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
|
|
317 }
|
|
318 else if (l -> lint2 == 2)
|
|
319 {
|
|
320 l -> len = OPLEN(instab[l -> insn].ops[2]) + 2;
|
|
321 }
|
|
322 else if (l -> lint2 == 1 && l -> lint != -1)
|
|
323 {
|
|
324 l -> len = OPLEN(instab[l -> insn].ops[1]) + l -> lint + 1;
|
|
325 }
|
|
326 }
|
339
|
327 }
|
|
328
|
361
|
329 RESOLVEFUNC(insn_resolve_gen16)
|
|
330 {
|
|
331 if (l -> len != -1)
|
|
332 return;
|
|
333
|
|
334 // handle non-immediate
|
|
335 insn_resolve_gen_aux(as, l, force);
|
|
336 }
|
|
337
|
|
338 EMITFUNC(insn_emit_gen16)
|
339
|
339 {
|
361
|
340 if (l -> lint2 == 3)
|
|
341 {
|
|
342 lw_expr_t e;
|
|
343 e = lwasm_fetch_expr(l, 0);
|
|
344 lwasm_emitop(l, instab[l -> insn].ops[3]);
|
|
345 lwasm_emitexpr(l, e, 2);
|
|
346 return;
|
|
347 }
|
|
348
|
362
|
349 insn_emit_gen_aux(as, l, -1);
|
361
|
350 }
|
|
351
|
|
352 PARSEFUNC(insn_parse_gen32)
|
|
353 {
|
339
|
354 if (**p == '#')
|
|
355 {
|
361
|
356 lw_expr_t e;
|
|
357
|
339
|
358 (*p)++;
|
361
|
359 e = lwasm_parse_expr(as, p);
|
|
360 if (!e)
|
339
|
361 {
|
361
|
362 lwasm_register_error(as, l, "Bad operand");
|
|
363 return;
|
339
|
364 }
|
361
|
365 l -> len = OPLEN(instab[l -> insn].ops[3]) + 4;
|
|
366 l -> lint2 = 3;
|
|
367 lwasm_save_expr(l, 0, e);
|
339
|
368 return;
|
|
369 }
|
|
370
|
361
|
371 // handle non-immediate
|
|
372 insn_parse_gen_aux(as, l, p);
|
|
373 if (l -> lint2 != -1)
|
|
374 {
|
|
375 if (l -> lint2 == 0)
|
|
376 {
|
|
377 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
|
|
378 }
|
|
379 else if (l -> lint2 == 2)
|
|
380 {
|
|
381 l -> len = OPLEN(instab[l -> insn].ops[2]) + 2;
|
|
382 }
|
|
383 else if (l -> lint2 == 1 && l -> lint != -1)
|
|
384 {
|
|
385 l -> len = OPLEN(instab[l -> insn].ops[1]) + l -> lint + 1;
|
|
386 }
|
|
387 }
|
339
|
388 }
|
|
389
|
361
|
390 RESOLVEFUNC(insn_resolve_gen32)
|
|
391 {
|
|
392 if (l -> len != -1)
|
|
393 return;
|
|
394
|
|
395 // handle non-immediate
|
|
396 insn_resolve_gen_aux(as, l, force);
|
|
397 }
|
|
398
|
|
399 EMITFUNC(insn_emit_gen32)
|
339
|
400 {
|
361
|
401 if (l -> lint2 == 3)
|
|
402 {
|
|
403 lw_expr_t e;
|
|
404 e = lwasm_fetch_expr(l, 0);
|
|
405 lwasm_emitop(l, instab[l -> insn].ops[3]);
|
|
406 lwasm_emitexpr(l, e, 4);
|
|
407 return;
|
|
408 }
|
|
409
|
362
|
410 insn_emit_gen_aux(as, l, -1);
|
361
|
411 }
|
|
412
|
|
413 PARSEFUNC(insn_parse_imm8)
|
|
414 {
|
|
415 lw_expr_t e;
|
339
|
416
|
|
417 if (**p == '#')
|
|
418 {
|
|
419 (*p)++;
|
|
420
|
361
|
421 e = lwasm_parse_expr(as, p);
|
|
422 if (!e)
|
|
423 {
|
|
424 lwasm_register_error(as, l, "Bad operand");
|
|
425 return;
|
|
426 }
|
|
427 l -> len = OPLEN(instab[l -> insn].ops[0]) + 1;
|
|
428 lwasm_save_expr(l, 0, e);
|
|
429 }
|
|
430 }
|
339
|
431
|
361
|
432 EMITFUNC(insn_emit_imm8)
|
|
433 {
|
|
434 lw_expr_t e;
|
|
435
|
|
436 lwasm_emitop(l, instab[l -> insn].ops[0]);
|
|
437 lwasm_emitexpr(l, e, 1);
|
339
|
438 }
|