blob: e90f35d5be65be63324d606d995d749599fe9915 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* parser.c source line parser for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 *
8 * initial version 27/iii/95 by Simon Tatham
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <stddef.h>
14#include <string.h>
15#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000016#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000017
18#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000019#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000020#include "nasmlib.h"
21#include "parser.h"
22#include "float.h"
23
H. Peter Anvine2c80182005-01-15 22:15:51 +000024extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000025extern int32_t abs_seg; /* ABSOLUTE segment */
26extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000027
H. Peter Anvine2c80182005-01-15 22:15:51 +000028#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000029
H. Peter Anvine2c80182005-01-15 22:15:51 +000030static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000031
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000032static int i;
33static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000034static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000035static struct ofmt *outfmt; /* Structure of addresses of output routines */
36static loc_t *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000037
H. Peter Anvine2c80182005-01-15 22:15:51 +000038void parser_global_info(struct ofmt *output, loc_t * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000039{
40 outfmt = output;
41 location = locp;
42}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000043
Keith Kaniosa6dfa782007-04-13 16:47:53 +000044insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +000045 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +000046{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047 int operand;
48 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000049 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000050
H. Peter Anvin76690a12002-04-30 20:52:49 +000051 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053
H. Peter Anvin76690a12002-04-30 20:52:49 +000054 stdscan_reset();
55 stdscan_bufptr = buffer;
56 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057
H. Peter Anvine2c80182005-01-15 22:15:51 +000058 result->label = NULL; /* Assume no label */
59 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +000060 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061
H. Peter Anvine2c80182005-01-15 22:15:51 +000062 if (i == 0) { /* blank line - ignore */
63 result->opcode = -1; /* and no instruction either */
64 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000065 }
66 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvine2c80182005-01-15 22:15:51 +000067 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
68 error(ERR_NONFATAL, "label or instruction expected"
69 " at start of line");
70 result->opcode = -1;
71 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072 }
73
H. Peter Anvine2c80182005-01-15 22:15:51 +000074 if (i == TOKEN_ID) { /* there's a label here */
75 result->label = tokval.t_charptr;
76 i = stdscan(NULL, &tokval);
77 if (i == ':') { /* skip over the optional colon */
78 i = stdscan(NULL, &tokval);
79 } else if (i == 0) {
80 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
81 "label alone on a line without a colon might be in error");
82 }
83 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
84 /*
85 * FIXME: location->segment could be NO_SEG, in which case
86 * it is possible we should be passing 'abs_seg'. Look into this.
87 * Work out whether that is *really* what we should be doing.
88 * Generally fix things. I think this is right as it is, but
89 * am still not certain.
90 */
91 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
92 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
93 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000094 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000095
H. Peter Anvine2c80182005-01-15 22:15:51 +000096 if (i == 0) {
97 result->opcode = -1; /* this line contains just a label */
98 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000099 }
100
101 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000102 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000103
104 while (i == TOKEN_PREFIX ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000105 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000106 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107 /*
108 * Handle special case: the TIMES prefix.
109 */
110 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
111 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000112
H. Peter Anvine2c80182005-01-15 22:15:51 +0000113 i = stdscan(NULL, &tokval);
114 value =
115 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
116 i = tokval.t_type;
117 if (!value) { /* but, error in evaluator */
118 result->opcode = -1; /* unrecoverable parse error: */
119 return result; /* ignore this instruction */
120 }
121 if (!is_simple(value)) {
122 error(ERR_NONFATAL,
123 "non-constant argument supplied to TIMES");
124 result->times = 1L;
125 } else {
126 result->times = value->value;
127 if (value->value < 0) {
128 error(ERR_NONFATAL, "TIMES value %d is negative",
129 value->value);
130 result->times = 0;
131 }
132 }
133 } else {
134 if (result->nprefix == MAXPREFIX)
135 error(ERR_NONFATAL,
136 "instruction has more than %d prefixes", MAXPREFIX);
137 else
138 result->prefixes[result->nprefix++] = tokval.t_integer;
139 i = stdscan(NULL, &tokval);
140 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000141 }
142
143 if (i != TOKEN_INSN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000144 if (result->nprefix > 0 && i == 0) {
145 /*
146 * Instruction prefixes are present, but no actual
147 * instruction. This is allowed: at this point we
148 * invent a notional instruction of RESB 0.
149 */
150 result->opcode = I_RESB;
151 result->operands = 1;
152 result->oprs[0].type = IMMEDIATE;
153 result->oprs[0].offset = 0L;
154 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
155 return result;
156 } else {
157 error(ERR_NONFATAL, "parser: instruction expected");
158 result->opcode = -1;
159 return result;
160 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000161 }
162
163 result->opcode = tokval.t_integer;
164 result->condition = tokval.t_inttwo;
165
166 /*
167 * RESB, RESW and RESD cannot be satisfied with incorrectly
168 * evaluated operands, since the correct values _must_ be known
169 * on the first pass. Hence, even in pass one, we set the
170 * `critical' flag on calling evaluate(), so that it will bomb
171 * out on undefined symbols. Nasty, but there's nothing we can
172 * do about it.
173 *
174 * For the moment, EQU has the same difficulty, so we'll
175 * include that.
176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177 if (result->opcode == I_RESB || result->opcode == I_RESW || result->opcode == I_RESD || result->opcode == I_RESQ || result->opcode == I_REST || result->opcode == I_EQU || result->opcode == I_INCBIN) { /* fbk */
178 critical = pass0;
179 } else
180 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000181
182 if (result->opcode == I_DB ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000183 result->opcode == I_DW ||
184 result->opcode == I_DD ||
185 result->opcode == I_DQ ||
186 result->opcode == I_DT || result->opcode == I_INCBIN) {
187 extop *eop, **tail = &result->eops, **fixptr;
188 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 result->eops_float = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000191
H. Peter Anvine2c80182005-01-15 22:15:51 +0000192 /*
193 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
194 */
195 while (1) {
196 i = stdscan(NULL, &tokval);
197 if (i == 0)
198 break;
199 fixptr = tail;
200 eop = *tail = nasm_malloc(sizeof(extop));
201 tail = &eop->next;
202 eop->next = NULL;
203 eop->type = EOT_NOTHING;
204 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000205
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
207 eop->type = EOT_DB_STRING;
208 eop->stringval = tokval.t_charptr;
209 eop->stringlen = tokval.t_inttwo;
210 i = stdscan(NULL, &tokval); /* eat the comma */
211 continue;
212 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000213
H. Peter Anvine2c80182005-01-15 22:15:51 +0000214 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000215 int32_t sign = +1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000216
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 if (i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000218 char *save = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219 i = stdscan(NULL, &tokval);
220 sign = -1L;
221 if (i != TOKEN_FLOAT || !is_comma_next()) {
222 stdscan_bufptr = save;
223 i = tokval.t_type = '-';
224 }
225 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000226
H. Peter Anvine2c80182005-01-15 22:15:51 +0000227 if (i == TOKEN_FLOAT) {
228 eop->type = EOT_DB_STRING;
229 result->eops_float = TRUE;
230 if (result->opcode == I_DD)
231 eop->stringlen = 4;
232 else if (result->opcode == I_DQ)
233 eop->stringlen = 8;
234 else if (result->opcode == I_DT)
235 eop->stringlen = 10;
236 else {
237 error(ERR_NONFATAL, "floating-point constant"
238 " encountered in `D%c' instruction",
239 result->opcode == I_DW ? 'W' : 'B');
240 /*
241 * fix suggested by Pedro Gimeno... original line
242 * was:
243 * eop->type = EOT_NOTHING;
244 */
245 eop->stringlen = 0;
246 }
247 eop =
248 nasm_realloc(eop, sizeof(extop) + eop->stringlen);
249 tail = &eop->next;
250 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000251 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000252 if (eop->stringlen < 4 ||
253 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000254 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255 eop->stringlen, error))
256 eop->type = EOT_NOTHING;
257 i = stdscan(NULL, &tokval); /* eat the comma */
258 continue;
259 }
260 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000261
H. Peter Anvine2c80182005-01-15 22:15:51 +0000262 /* anything else */
263 {
264 expr *value;
265 value = evaluate(stdscan, NULL, &tokval, NULL,
266 critical, error, NULL);
267 i = tokval.t_type;
268 if (!value) { /* error in evaluator */
269 result->opcode = -1; /* unrecoverable parse error: */
270 return result; /* ignore this instruction */
271 }
272 if (is_unknown(value)) {
273 eop->type = EOT_DB_NUMBER;
274 eop->offset = 0; /* doesn't matter what we put */
275 eop->segment = eop->wrt = NO_SEG; /* likewise */
276 } else if (is_reloc(value)) {
277 eop->type = EOT_DB_NUMBER;
278 eop->offset = reloc_value(value);
279 eop->segment = reloc_seg(value);
280 eop->wrt = reloc_wrt(value);
281 } else {
282 error(ERR_NONFATAL,
283 "operand %d: expression is not simple"
284 " or relocatable", oper_num);
285 }
286 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000287
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288 /*
289 * We're about to call stdscan(), which will eat the
290 * comma that we're currently sitting on between
291 * arguments. However, we'd better check first that it
292 * _is_ a comma.
293 */
294 if (i == 0) /* also could be EOL */
295 break;
296 if (i != ',') {
297 error(ERR_NONFATAL, "comma expected after operand %d",
298 oper_num);
299 result->opcode = -1; /* unrecoverable parse error: */
300 return result; /* ignore this instruction */
301 }
302 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000303
H. Peter Anvine2c80182005-01-15 22:15:51 +0000304 if (result->opcode == I_INCBIN) {
305 /*
306 * Correct syntax for INCBIN is that there should be
307 * one string operand, followed by one or two numeric
308 * operands.
309 */
310 if (!result->eops || result->eops->type != EOT_DB_STRING)
311 error(ERR_NONFATAL, "`incbin' expects a file name");
312 else if (result->eops->next &&
313 result->eops->next->type != EOT_DB_NUMBER)
314 error(ERR_NONFATAL, "`incbin': second parameter is",
315 " non-numeric");
316 else if (result->eops->next && result->eops->next->next &&
317 result->eops->next->next->type != EOT_DB_NUMBER)
318 error(ERR_NONFATAL, "`incbin': third parameter is",
319 " non-numeric");
320 else if (result->eops->next && result->eops->next->next &&
321 result->eops->next->next->next)
322 error(ERR_NONFATAL,
323 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000324 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000325 return result;
326 /*
327 * If we reach here, one of the above errors happened.
328 * Throw the instruction away.
329 */
330 result->opcode = -1;
331 return result;
332 } else /* DB ... */ if (oper_num == 0)
333 error(ERR_WARNING | ERR_PASS1,
334 "no operand for data declaration");
335 else
336 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvine2c80182005-01-15 22:15:51 +0000338 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000339 }
340
341 /* right. Now we begin to parse the operands. There may be up to three
342 * of these, separated by commas, and terminated by a zero token. */
343
344 for (operand = 0; operand < 3; operand++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000345 expr *value; /* used most of the time */
346 int mref; /* is this going to be a memory ref? */
347 int bracket; /* is it a [] mref, or a & mref? */
348 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000349
H. Peter Anvine2c80182005-01-15 22:15:51 +0000350 result->oprs[operand].addr_size = 0; /* have to zero this whatever */
351 result->oprs[operand].eaflags = 0; /* and this */
352 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000353
H. Peter Anvine2c80182005-01-15 22:15:51 +0000354 i = stdscan(NULL, &tokval);
355 if (i == 0)
356 break; /* end of operands: get out of here */
357 result->oprs[operand].type = 0; /* so far, no override */
358 while (i == TOKEN_SPECIAL) { /* size specifiers */
359 switch ((int)tokval.t_integer) {
360 case S_BYTE:
361 if (!setsize) /* we want to use only the first */
362 result->oprs[operand].type |= BITS8;
363 setsize = 1;
364 break;
365 case S_WORD:
366 if (!setsize)
367 result->oprs[operand].type |= BITS16;
368 setsize = 1;
369 break;
370 case S_DWORD:
371 case S_LONG:
372 if (!setsize)
373 result->oprs[operand].type |= BITS32;
374 setsize = 1;
375 break;
376 case S_QWORD:
377 if (!setsize)
378 result->oprs[operand].type |= BITS64;
379 setsize = 1;
380 break;
381 case S_TWORD:
382 if (!setsize)
383 result->oprs[operand].type |= BITS80;
384 setsize = 1;
385 break;
386 case S_TO:
387 result->oprs[operand].type |= TO;
388 break;
389 case S_STRICT:
390 result->oprs[operand].type |= STRICT;
391 break;
392 case S_FAR:
393 result->oprs[operand].type |= FAR;
394 break;
395 case S_NEAR:
396 result->oprs[operand].type |= NEAR;
397 break;
398 case S_SHORT:
399 result->oprs[operand].type |= SHORT;
400 break;
401 default:
402 error(ERR_NONFATAL, "invalid operand size specification");
403 }
404 i = stdscan(NULL, &tokval);
405 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000406
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 if (i == '[' || i == '&') { /* memory reference */
408 mref = TRUE;
409 bracket = (i == '[');
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000410 while ((i = stdscan(NULL, &tokval)) == TOKEN_SPECIAL) {
411 /* check for address directives */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 if (tasm_compatible_mode) {
413 switch ((int)tokval.t_integer) {
414 /* For TASM compatibility a size override inside the
415 * brackets changes the size of the operand, not the
416 * address type of the operand as it does in standard
417 * NASM syntax. Hence:
418 *
419 * mov eax,[DWORD val]
420 *
421 * is valid syntax in TASM compatibility mode. Note that
422 * you lose the ability to override the default address
423 * type for the instruction, but we never use anything
424 * but 32-bit flat model addressing in our code.
425 */
426 case S_BYTE:
427 result->oprs[operand].type |= BITS8;
428 break;
429 case S_WORD:
430 result->oprs[operand].type |= BITS16;
431 break;
432 case S_DWORD:
433 case S_LONG:
434 result->oprs[operand].type |= BITS32;
435 break;
436 case S_QWORD:
437 result->oprs[operand].type |= BITS64;
438 break;
439 case S_TWORD:
440 result->oprs[operand].type |= BITS80;
441 break;
442 default:
443 error(ERR_NONFATAL,
444 "invalid operand size specification");
445 }
446 } else {
447 /* Standard NASM compatible syntax */
448 switch ((int)tokval.t_integer) {
449 case S_NOSPLIT:
450 result->oprs[operand].eaflags |= EAF_TIMESTWO;
451 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000452 case S_REL:
453 result->oprs[operand].eaflags |= EAF_REL;
454 break;
455 case S_ABS:
456 result->oprs[operand].eaflags |= EAF_ABS;
457 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 case S_BYTE:
459 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
460 break;
461 case S_WORD:
462 result->oprs[operand].addr_size = 16;
463 result->oprs[operand].eaflags |= EAF_WORDOFFS;
464 break;
465 case S_DWORD:
466 case S_LONG:
467 result->oprs[operand].addr_size = 32;
468 result->oprs[operand].eaflags |= EAF_WORDOFFS;
469 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000470 case S_QWORD:
471 result->oprs[operand].addr_size = 64;
472 result->oprs[operand].eaflags |= EAF_WORDOFFS;
473 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 default:
475 error(ERR_NONFATAL, "invalid size specification in"
476 " effective address");
477 }
478 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 }
480 } else { /* immediate operand, or register */
481 mref = FALSE;
482 bracket = FALSE; /* placate optimisers */
483 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000484
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 if ((result->oprs[operand].type & FAR) && !mref &&
486 result->opcode != I_JMP && result->opcode != I_CALL) {
487 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
488 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000489
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 value = evaluate(stdscan, NULL, &tokval,
491 &result->oprs[operand].opflags,
492 critical, error, &hints);
493 i = tokval.t_type;
494 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
495 result->forw_ref = TRUE;
496 }
497 if (!value) { /* error in evaluator */
498 result->opcode = -1; /* unrecoverable parse error: */
499 return result; /* ignore this instruction */
500 }
501 if (i == ':' && mref) { /* it was seg:offset */
502 /*
503 * Process the segment override.
504 */
505 if (value[1].type != 0 || value->value != 1 ||
506 REG_SREG & ~reg_flags[value->type])
507 error(ERR_NONFATAL, "invalid segment override");
508 else if (result->nprefix == MAXPREFIX)
509 error(ERR_NONFATAL,
510 "instruction has more than %d prefixes", MAXPREFIX);
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000511 else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 result->prefixes[result->nprefix++] = value->type;
H. Peter Anvin490bbcd2007-08-29 20:30:31 +0000513 if (!(REG_FSGS & ~reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000514 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000515 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000516
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 i = stdscan(NULL, &tokval); /* then skip the colon */
518 if (i == TOKEN_SPECIAL) { /* another check for size override */
519 switch ((int)tokval.t_integer) {
520 case S_WORD:
521 result->oprs[operand].addr_size = 16;
522 break;
523 case S_DWORD:
524 case S_LONG:
525 result->oprs[operand].addr_size = 32;
526 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000527 case S_QWORD:
528 result->oprs[operand].addr_size = 64;
529 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 default:
531 error(ERR_NONFATAL, "invalid size specification in"
532 " effective address");
533 }
534 i = stdscan(NULL, &tokval);
535 }
536 value = evaluate(stdscan, NULL, &tokval,
537 &result->oprs[operand].opflags,
538 critical, error, &hints);
539 i = tokval.t_type;
540 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
541 result->forw_ref = TRUE;
542 }
543 /* and get the offset */
544 if (!value) { /* but, error in evaluator */
545 result->opcode = -1; /* unrecoverable parse error: */
546 return result; /* ignore this instruction */
547 }
548 }
549 if (mref && bracket) { /* find ] at the end */
550 if (i != ']') {
551 error(ERR_NONFATAL, "parser: expecting ]");
552 do { /* error recovery again */
553 i = stdscan(NULL, &tokval);
554 } while (i != 0 && i != ',');
555 } else /* we got the required ] */
556 i = stdscan(NULL, &tokval);
557 } else { /* immediate operand */
558 if (i != 0 && i != ',' && i != ':') {
559 error(ERR_NONFATAL, "comma or end of line expected");
560 do { /* error recovery */
561 i = stdscan(NULL, &tokval);
562 } while (i != 0 && i != ',');
563 } else if (i == ':') {
564 result->oprs[operand].type |= COLON;
565 }
566 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000567
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 /* now convert the exprs returned from evaluate() into operand
569 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000570
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 if (mref) { /* it's a memory reference */
572 expr *e = value;
573 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000574 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000575
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 b = i = -1, o = s = 0;
577 result->oprs[operand].hintbase = hints.base;
578 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000579
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
581 if (e->value == 1) /* in fact it can be basereg */
582 b = e->type;
583 else /* no, it has to be indexreg */
584 i = e->type, s = e->value;
585 e++;
586 }
587 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
588 if (b != -1) /* If the first was the base, ... */
589 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000590
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 else if (e->value != 1) { /* If both want to be index */
592 error(ERR_NONFATAL,
593 "beroset-p-592-invalid effective address");
594 result->opcode = -1;
595 return result;
596 } else
597 b = e->type;
598 e++;
599 }
600 if (e->type != 0) { /* is there an offset? */
601 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
602 error(ERR_NONFATAL,
603 "beroset-p-603-invalid effective address");
604 result->opcode = -1;
605 return result;
606 } else {
607 if (e->type == EXPR_UNKNOWN) {
608 o = 0; /* doesn't matter what */
609 result->oprs[operand].wrt = NO_SEG; /* nor this */
610 result->oprs[operand].segment = NO_SEG; /* or this */
611 while (e->type)
612 e++; /* go to the end of the line */
613 } else {
614 if (e->type == EXPR_SIMPLE) {
615 o = e->value;
616 e++;
617 }
618 if (e->type == EXPR_WRT) {
619 result->oprs[operand].wrt = e->value;
620 e++;
621 } else
622 result->oprs[operand].wrt = NO_SEG;
623 /*
624 * Look for a segment base type.
625 */
626 if (e->type && e->type < EXPR_SEGBASE) {
627 error(ERR_NONFATAL,
628 "beroset-p-630-invalid effective address");
629 result->opcode = -1;
630 return result;
631 }
632 while (e->type && e->value == 0)
633 e++;
634 if (e->type && e->value != 1) {
635 error(ERR_NONFATAL,
636 "beroset-p-637-invalid effective address");
637 result->opcode = -1;
638 return result;
639 }
640 if (e->type) {
641 result->oprs[operand].segment =
642 e->type - EXPR_SEGBASE;
643 e++;
644 } else
645 result->oprs[operand].segment = NO_SEG;
646 while (e->type && e->value == 0)
647 e++;
648 if (e->type) {
649 error(ERR_NONFATAL,
650 "beroset-p-650-invalid effective address");
651 result->opcode = -1;
652 return result;
653 }
654 }
655 }
656 } else {
657 o = 0;
658 result->oprs[operand].wrt = NO_SEG;
659 result->oprs[operand].segment = NO_SEG;
660 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000661
H. Peter Anvine2c80182005-01-15 22:15:51 +0000662 if (e->type != 0) { /* there'd better be nothing left! */
663 error(ERR_NONFATAL,
664 "beroset-p-663-invalid effective address");
665 result->opcode = -1;
666 return result;
667 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000668
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 result->oprs[operand].type |= MEMORY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000670
671 if (b == -1 && (i == -1 || s == 0)) {
672 int is_rel = globalbits == 64 &&
673 !(result->oprs[operand].eaflags & EAF_ABS) &&
674 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000675 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000676 (result->oprs[operand].eaflags & EAF_REL));
677
678 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
679 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 result->oprs[operand].basereg = b;
681 result->oprs[operand].indexreg = i;
682 result->oprs[operand].scale = s;
683 result->oprs[operand].offset = o;
684 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 if (is_just_unknown(value)) { /* it's immediate but unknown */
687 result->oprs[operand].type |= IMMEDIATE;
688 result->oprs[operand].offset = 0; /* don't care */
689 result->oprs[operand].segment = NO_SEG; /* don't care again */
690 result->oprs[operand].wrt = NO_SEG; /* still don't care */
691 } else if (is_reloc(value)) { /* it's immediate */
692 result->oprs[operand].type |= IMMEDIATE;
693 result->oprs[operand].offset = reloc_value(value);
694 result->oprs[operand].segment = reloc_seg(value);
695 result->oprs[operand].wrt = reloc_wrt(value);
696 if (is_simple(value)) {
697 if (reloc_value(value) == 1)
698 result->oprs[operand].type |= UNITY;
699 if (optimizing >= 0 &&
700 !(result->oprs[operand].type & STRICT)) {
701 if (reloc_value(value) >= -128 &&
702 reloc_value(value) <= 127)
703 result->oprs[operand].type |= SBYTE;
704 }
705 }
706 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000707
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 if (value->type >= EXPR_SIMPLE || value->value != 1) {
709 error(ERR_NONFATAL, "invalid operand type");
710 result->opcode = -1;
711 return result;
712 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 /*
715 * check that its only 1 register, not an expression...
716 */
717 for (i = 1; value[i].type; i++)
718 if (value[i].value) {
719 error(ERR_NONFATAL, "invalid operand type");
720 result->opcode = -1;
721 return result;
722 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000723
H. Peter Anvine2c80182005-01-15 22:15:51 +0000724 /* clear overrides, except TO which applies to FPU regs */
725 if (result->oprs[operand].type & ~TO) {
726 /*
727 * we want to produce a warning iff the specified size
728 * is different from the register size
729 */
730 i = result->oprs[operand].type & SIZE_MASK;
731 } else
732 i = 0;
733
734 result->oprs[operand].type &= TO;
735 result->oprs[operand].type |= REGISTER;
736 result->oprs[operand].type |= reg_flags[value->type];
737 result->oprs[operand].basereg = value->type;
738
739 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
740 error(ERR_WARNING | ERR_PASS1,
741 "register size specification ignored");
742 }
743 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000744 }
745
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000747
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 while (operand < 3) /* clear remaining operands */
749 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000750
751 /*
752 * Transform RESW, RESD, RESQ, REST into RESB.
753 */
754 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 case I_RESW:
756 result->opcode = I_RESB;
757 result->oprs[0].offset *= 2;
758 break;
759 case I_RESD:
760 result->opcode = I_RESB;
761 result->oprs[0].offset *= 4;
762 break;
763 case I_RESQ:
764 result->opcode = I_RESB;
765 result->oprs[0].offset *= 8;
766 break;
767 case I_REST:
768 result->opcode = I_RESB;
769 result->oprs[0].offset *= 10;
770 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000771 }
772
773 return result;
774}
775
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000777{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000778 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000779 int i;
780 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000781
H. Peter Anvin76690a12002-04-30 20:52:49 +0000782 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000784 stdscan_bufptr = p;
785 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000786}
787
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000789{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000790 extop *e;
791
792 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 e = i->eops;
794 i->eops = i->eops->next;
795 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000796 }
797}