blob: e6680555b8d504860f636f8c8c2bd2ad12cf569d [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 +000030enum { /* special tokens */
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +000031 S_ABS, S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD, S_REL,
H. Peter Anvin01377d82002-05-21 03:16:33 +000032 S_SHORT, S_STRICT, S_TO, S_TWORD, S_WORD
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000033};
34
H. Peter Anvine2c80182005-01-15 22:15:51 +000035static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037static int i;
38static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000039static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000040static struct ofmt *outfmt; /* Structure of addresses of output routines */
41static loc_t *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000042
H. Peter Anvine2c80182005-01-15 22:15:51 +000043void parser_global_info(struct ofmt *output, loc_t * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000044{
45 outfmt = output;
46 location = locp;
47}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
Keith Kaniosa6dfa782007-04-13 16:47:53 +000049insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +000050 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +000051{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052 int operand;
53 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000054 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055
H. Peter Anvin76690a12002-04-30 20:52:49 +000056 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058
H. Peter Anvin76690a12002-04-30 20:52:49 +000059 stdscan_reset();
60 stdscan_bufptr = buffer;
61 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062
H. Peter Anvine2c80182005-01-15 22:15:51 +000063 result->label = NULL; /* Assume no label */
64 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +000065 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066
H. Peter Anvine2c80182005-01-15 22:15:51 +000067 if (i == 0) { /* blank line - ignore */
68 result->opcode = -1; /* and no instruction either */
69 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070 }
71 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvine2c80182005-01-15 22:15:51 +000072 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
73 error(ERR_NONFATAL, "label or instruction expected"
74 " at start of line");
75 result->opcode = -1;
76 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000077 }
78
H. Peter Anvine2c80182005-01-15 22:15:51 +000079 if (i == TOKEN_ID) { /* there's a label here */
80 result->label = tokval.t_charptr;
81 i = stdscan(NULL, &tokval);
82 if (i == ':') { /* skip over the optional colon */
83 i = stdscan(NULL, &tokval);
84 } else if (i == 0) {
85 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
86 "label alone on a line without a colon might be in error");
87 }
88 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
89 /*
90 * FIXME: location->segment could be NO_SEG, in which case
91 * it is possible we should be passing 'abs_seg'. Look into this.
92 * Work out whether that is *really* what we should be doing.
93 * Generally fix things. I think this is right as it is, but
94 * am still not certain.
95 */
96 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
97 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
98 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000099 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101 if (i == 0) {
102 result->opcode = -1; /* this line contains just a label */
103 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000104 }
105
106 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000107 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000108
109 while (i == TOKEN_PREFIX ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000110 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000111 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 /*
113 * Handle special case: the TIMES prefix.
114 */
115 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
116 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000117
H. Peter Anvine2c80182005-01-15 22:15:51 +0000118 i = stdscan(NULL, &tokval);
119 value =
120 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
121 i = tokval.t_type;
122 if (!value) { /* but, error in evaluator */
123 result->opcode = -1; /* unrecoverable parse error: */
124 return result; /* ignore this instruction */
125 }
126 if (!is_simple(value)) {
127 error(ERR_NONFATAL,
128 "non-constant argument supplied to TIMES");
129 result->times = 1L;
130 } else {
131 result->times = value->value;
132 if (value->value < 0) {
133 error(ERR_NONFATAL, "TIMES value %d is negative",
134 value->value);
135 result->times = 0;
136 }
137 }
138 } else {
139 if (result->nprefix == MAXPREFIX)
140 error(ERR_NONFATAL,
141 "instruction has more than %d prefixes", MAXPREFIX);
142 else
143 result->prefixes[result->nprefix++] = tokval.t_integer;
144 i = stdscan(NULL, &tokval);
145 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000146 }
147
148 if (i != TOKEN_INSN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 if (result->nprefix > 0 && i == 0) {
150 /*
151 * Instruction prefixes are present, but no actual
152 * instruction. This is allowed: at this point we
153 * invent a notional instruction of RESB 0.
154 */
155 result->opcode = I_RESB;
156 result->operands = 1;
157 result->oprs[0].type = IMMEDIATE;
158 result->oprs[0].offset = 0L;
159 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
160 return result;
161 } else {
162 error(ERR_NONFATAL, "parser: instruction expected");
163 result->opcode = -1;
164 return result;
165 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000166 }
167
168 result->opcode = tokval.t_integer;
169 result->condition = tokval.t_inttwo;
170
171 /*
172 * RESB, RESW and RESD cannot be satisfied with incorrectly
173 * evaluated operands, since the correct values _must_ be known
174 * on the first pass. Hence, even in pass one, we set the
175 * `critical' flag on calling evaluate(), so that it will bomb
176 * out on undefined symbols. Nasty, but there's nothing we can
177 * do about it.
178 *
179 * For the moment, EQU has the same difficulty, so we'll
180 * include that.
181 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000182 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 */
183 critical = pass0;
184 } else
185 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000186
187 if (result->opcode == I_DB ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000188 result->opcode == I_DW ||
189 result->opcode == I_DD ||
190 result->opcode == I_DQ ||
191 result->opcode == I_DT || result->opcode == I_INCBIN) {
192 extop *eop, **tail = &result->eops, **fixptr;
193 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000194
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 result->eops_float = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000196
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197 /*
198 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
199 */
200 while (1) {
201 i = stdscan(NULL, &tokval);
202 if (i == 0)
203 break;
204 fixptr = tail;
205 eop = *tail = nasm_malloc(sizeof(extop));
206 tail = &eop->next;
207 eop->next = NULL;
208 eop->type = EOT_NOTHING;
209 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000210
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
212 eop->type = EOT_DB_STRING;
213 eop->stringval = tokval.t_charptr;
214 eop->stringlen = tokval.t_inttwo;
215 i = stdscan(NULL, &tokval); /* eat the comma */
216 continue;
217 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000220 int32_t sign = +1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222 if (i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000223 char *save = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000224 i = stdscan(NULL, &tokval);
225 sign = -1L;
226 if (i != TOKEN_FLOAT || !is_comma_next()) {
227 stdscan_bufptr = save;
228 i = tokval.t_type = '-';
229 }
230 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000231
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232 if (i == TOKEN_FLOAT) {
233 eop->type = EOT_DB_STRING;
234 result->eops_float = TRUE;
235 if (result->opcode == I_DD)
236 eop->stringlen = 4;
237 else if (result->opcode == I_DQ)
238 eop->stringlen = 8;
239 else if (result->opcode == I_DT)
240 eop->stringlen = 10;
241 else {
242 error(ERR_NONFATAL, "floating-point constant"
243 " encountered in `D%c' instruction",
244 result->opcode == I_DW ? 'W' : 'B');
245 /*
246 * fix suggested by Pedro Gimeno... original line
247 * was:
248 * eop->type = EOT_NOTHING;
249 */
250 eop->stringlen = 0;
251 }
252 eop =
253 nasm_realloc(eop, sizeof(extop) + eop->stringlen);
254 tail = &eop->next;
255 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000256 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257 if (eop->stringlen < 4 ||
258 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000259 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000260 eop->stringlen, error))
261 eop->type = EOT_NOTHING;
262 i = stdscan(NULL, &tokval); /* eat the comma */
263 continue;
264 }
265 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000266
H. Peter Anvine2c80182005-01-15 22:15:51 +0000267 /* anything else */
268 {
269 expr *value;
270 value = evaluate(stdscan, NULL, &tokval, NULL,
271 critical, error, NULL);
272 i = tokval.t_type;
273 if (!value) { /* error in evaluator */
274 result->opcode = -1; /* unrecoverable parse error: */
275 return result; /* ignore this instruction */
276 }
277 if (is_unknown(value)) {
278 eop->type = EOT_DB_NUMBER;
279 eop->offset = 0; /* doesn't matter what we put */
280 eop->segment = eop->wrt = NO_SEG; /* likewise */
281 } else if (is_reloc(value)) {
282 eop->type = EOT_DB_NUMBER;
283 eop->offset = reloc_value(value);
284 eop->segment = reloc_seg(value);
285 eop->wrt = reloc_wrt(value);
286 } else {
287 error(ERR_NONFATAL,
288 "operand %d: expression is not simple"
289 " or relocatable", oper_num);
290 }
291 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000292
H. Peter Anvine2c80182005-01-15 22:15:51 +0000293 /*
294 * We're about to call stdscan(), which will eat the
295 * comma that we're currently sitting on between
296 * arguments. However, we'd better check first that it
297 * _is_ a comma.
298 */
299 if (i == 0) /* also could be EOL */
300 break;
301 if (i != ',') {
302 error(ERR_NONFATAL, "comma expected after operand %d",
303 oper_num);
304 result->opcode = -1; /* unrecoverable parse error: */
305 return result; /* ignore this instruction */
306 }
307 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000308
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309 if (result->opcode == I_INCBIN) {
310 /*
311 * Correct syntax for INCBIN is that there should be
312 * one string operand, followed by one or two numeric
313 * operands.
314 */
315 if (!result->eops || result->eops->type != EOT_DB_STRING)
316 error(ERR_NONFATAL, "`incbin' expects a file name");
317 else if (result->eops->next &&
318 result->eops->next->type != EOT_DB_NUMBER)
319 error(ERR_NONFATAL, "`incbin': second parameter is",
320 " non-numeric");
321 else if (result->eops->next && result->eops->next->next &&
322 result->eops->next->next->type != EOT_DB_NUMBER)
323 error(ERR_NONFATAL, "`incbin': third parameter is",
324 " non-numeric");
325 else if (result->eops->next && result->eops->next->next &&
326 result->eops->next->next->next)
327 error(ERR_NONFATAL,
328 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000329 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330 return result;
331 /*
332 * If we reach here, one of the above errors happened.
333 * Throw the instruction away.
334 */
335 result->opcode = -1;
336 return result;
337 } else /* DB ... */ if (oper_num == 0)
338 error(ERR_WARNING | ERR_PASS1,
339 "no operand for data declaration");
340 else
341 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000342
H. Peter Anvine2c80182005-01-15 22:15:51 +0000343 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000344 }
345
346 /* right. Now we begin to parse the operands. There may be up to three
347 * of these, separated by commas, and terminated by a zero token. */
348
349 for (operand = 0; operand < 3; operand++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000350 expr *value; /* used most of the time */
351 int mref; /* is this going to be a memory ref? */
352 int bracket; /* is it a [] mref, or a & mref? */
353 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000354
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 result->oprs[operand].addr_size = 0; /* have to zero this whatever */
356 result->oprs[operand].eaflags = 0; /* and this */
357 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 i = stdscan(NULL, &tokval);
360 if (i == 0)
361 break; /* end of operands: get out of here */
362 result->oprs[operand].type = 0; /* so far, no override */
363 while (i == TOKEN_SPECIAL) { /* size specifiers */
364 switch ((int)tokval.t_integer) {
365 case S_BYTE:
366 if (!setsize) /* we want to use only the first */
367 result->oprs[operand].type |= BITS8;
368 setsize = 1;
369 break;
370 case S_WORD:
371 if (!setsize)
372 result->oprs[operand].type |= BITS16;
373 setsize = 1;
374 break;
375 case S_DWORD:
376 case S_LONG:
377 if (!setsize)
378 result->oprs[operand].type |= BITS32;
379 setsize = 1;
380 break;
381 case S_QWORD:
382 if (!setsize)
383 result->oprs[operand].type |= BITS64;
384 setsize = 1;
385 break;
386 case S_TWORD:
387 if (!setsize)
388 result->oprs[operand].type |= BITS80;
389 setsize = 1;
390 break;
391 case S_TO:
392 result->oprs[operand].type |= TO;
393 break;
394 case S_STRICT:
395 result->oprs[operand].type |= STRICT;
396 break;
397 case S_FAR:
398 result->oprs[operand].type |= FAR;
399 break;
400 case S_NEAR:
401 result->oprs[operand].type |= NEAR;
402 break;
403 case S_SHORT:
404 result->oprs[operand].type |= SHORT;
405 break;
406 default:
407 error(ERR_NONFATAL, "invalid operand size specification");
408 }
409 i = stdscan(NULL, &tokval);
410 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000411
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 if (i == '[' || i == '&') { /* memory reference */
413 mref = TRUE;
414 bracket = (i == '[');
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000415 while ((i = stdscan(NULL, &tokval)) == TOKEN_SPECIAL) {
416 /* check for address directives */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 if (tasm_compatible_mode) {
418 switch ((int)tokval.t_integer) {
419 /* For TASM compatibility a size override inside the
420 * brackets changes the size of the operand, not the
421 * address type of the operand as it does in standard
422 * NASM syntax. Hence:
423 *
424 * mov eax,[DWORD val]
425 *
426 * is valid syntax in TASM compatibility mode. Note that
427 * you lose the ability to override the default address
428 * type for the instruction, but we never use anything
429 * but 32-bit flat model addressing in our code.
430 */
431 case S_BYTE:
432 result->oprs[operand].type |= BITS8;
433 break;
434 case S_WORD:
435 result->oprs[operand].type |= BITS16;
436 break;
437 case S_DWORD:
438 case S_LONG:
439 result->oprs[operand].type |= BITS32;
440 break;
441 case S_QWORD:
442 result->oprs[operand].type |= BITS64;
443 break;
444 case S_TWORD:
445 result->oprs[operand].type |= BITS80;
446 break;
447 default:
448 error(ERR_NONFATAL,
449 "invalid operand size specification");
450 }
451 } else {
452 /* Standard NASM compatible syntax */
453 switch ((int)tokval.t_integer) {
454 case S_NOSPLIT:
455 result->oprs[operand].eaflags |= EAF_TIMESTWO;
456 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000457 case S_REL:
458 result->oprs[operand].eaflags |= EAF_REL;
459 break;
460 case S_ABS:
461 result->oprs[operand].eaflags |= EAF_ABS;
462 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 case S_BYTE:
464 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
465 break;
466 case S_WORD:
467 result->oprs[operand].addr_size = 16;
468 result->oprs[operand].eaflags |= EAF_WORDOFFS;
469 break;
470 case S_DWORD:
471 case S_LONG:
472 result->oprs[operand].addr_size = 32;
473 result->oprs[operand].eaflags |= EAF_WORDOFFS;
474 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000475 case S_QWORD:
476 result->oprs[operand].addr_size = 64;
477 result->oprs[operand].eaflags |= EAF_WORDOFFS;
478 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 default:
480 error(ERR_NONFATAL, "invalid size specification in"
481 " effective address");
482 }
483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 }
485 } else { /* immediate operand, or register */
486 mref = FALSE;
487 bracket = FALSE; /* placate optimisers */
488 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000489
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 if ((result->oprs[operand].type & FAR) && !mref &&
491 result->opcode != I_JMP && result->opcode != I_CALL) {
492 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
493 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000494
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495 value = evaluate(stdscan, NULL, &tokval,
496 &result->oprs[operand].opflags,
497 critical, error, &hints);
498 i = tokval.t_type;
499 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
500 result->forw_ref = TRUE;
501 }
502 if (!value) { /* error in evaluator */
503 result->opcode = -1; /* unrecoverable parse error: */
504 return result; /* ignore this instruction */
505 }
506 if (i == ':' && mref) { /* it was seg:offset */
507 /*
508 * Process the segment override.
509 */
510 if (value[1].type != 0 || value->value != 1 ||
511 REG_SREG & ~reg_flags[value->type])
512 error(ERR_NONFATAL, "invalid segment override");
513 else if (result->nprefix == MAXPREFIX)
514 error(ERR_NONFATAL,
515 "instruction has more than %d prefixes", MAXPREFIX);
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000516 else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 result->prefixes[result->nprefix++] = value->type;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000518 result->oprs[operand].eaflags |= EAF_SEGOVER;
519 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000520
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 i = stdscan(NULL, &tokval); /* then skip the colon */
522 if (i == TOKEN_SPECIAL) { /* another check for size override */
523 switch ((int)tokval.t_integer) {
524 case S_WORD:
525 result->oprs[operand].addr_size = 16;
526 break;
527 case S_DWORD:
528 case S_LONG:
529 result->oprs[operand].addr_size = 32;
530 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000531 case S_QWORD:
532 result->oprs[operand].addr_size = 64;
533 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 default:
535 error(ERR_NONFATAL, "invalid size specification in"
536 " effective address");
537 }
538 i = stdscan(NULL, &tokval);
539 }
540 value = evaluate(stdscan, NULL, &tokval,
541 &result->oprs[operand].opflags,
542 critical, error, &hints);
543 i = tokval.t_type;
544 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
545 result->forw_ref = TRUE;
546 }
547 /* and get the offset */
548 if (!value) { /* but, error in evaluator */
549 result->opcode = -1; /* unrecoverable parse error: */
550 return result; /* ignore this instruction */
551 }
552 }
553 if (mref && bracket) { /* find ] at the end */
554 if (i != ']') {
555 error(ERR_NONFATAL, "parser: expecting ]");
556 do { /* error recovery again */
557 i = stdscan(NULL, &tokval);
558 } while (i != 0 && i != ',');
559 } else /* we got the required ] */
560 i = stdscan(NULL, &tokval);
561 } else { /* immediate operand */
562 if (i != 0 && i != ',' && i != ':') {
563 error(ERR_NONFATAL, "comma or end of line expected");
564 do { /* error recovery */
565 i = stdscan(NULL, &tokval);
566 } while (i != 0 && i != ',');
567 } else if (i == ':') {
568 result->oprs[operand].type |= COLON;
569 }
570 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000571
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 /* now convert the exprs returned from evaluate() into operand
573 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000574
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 if (mref) { /* it's a memory reference */
576 expr *e = value;
577 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000578 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000579
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 b = i = -1, o = s = 0;
581 result->oprs[operand].hintbase = hints.base;
582 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000583
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
585 if (e->value == 1) /* in fact it can be basereg */
586 b = e->type;
587 else /* no, it has to be indexreg */
588 i = e->type, s = e->value;
589 e++;
590 }
591 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
592 if (b != -1) /* If the first was the base, ... */
593 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000594
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 else if (e->value != 1) { /* If both want to be index */
596 error(ERR_NONFATAL,
597 "beroset-p-592-invalid effective address");
598 result->opcode = -1;
599 return result;
600 } else
601 b = e->type;
602 e++;
603 }
604 if (e->type != 0) { /* is there an offset? */
605 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
606 error(ERR_NONFATAL,
607 "beroset-p-603-invalid effective address");
608 result->opcode = -1;
609 return result;
610 } else {
611 if (e->type == EXPR_UNKNOWN) {
612 o = 0; /* doesn't matter what */
613 result->oprs[operand].wrt = NO_SEG; /* nor this */
614 result->oprs[operand].segment = NO_SEG; /* or this */
615 while (e->type)
616 e++; /* go to the end of the line */
617 } else {
618 if (e->type == EXPR_SIMPLE) {
619 o = e->value;
620 e++;
621 }
622 if (e->type == EXPR_WRT) {
623 result->oprs[operand].wrt = e->value;
624 e++;
625 } else
626 result->oprs[operand].wrt = NO_SEG;
627 /*
628 * Look for a segment base type.
629 */
630 if (e->type && e->type < EXPR_SEGBASE) {
631 error(ERR_NONFATAL,
632 "beroset-p-630-invalid effective address");
633 result->opcode = -1;
634 return result;
635 }
636 while (e->type && e->value == 0)
637 e++;
638 if (e->type && e->value != 1) {
639 error(ERR_NONFATAL,
640 "beroset-p-637-invalid effective address");
641 result->opcode = -1;
642 return result;
643 }
644 if (e->type) {
645 result->oprs[operand].segment =
646 e->type - EXPR_SEGBASE;
647 e++;
648 } else
649 result->oprs[operand].segment = NO_SEG;
650 while (e->type && e->value == 0)
651 e++;
652 if (e->type) {
653 error(ERR_NONFATAL,
654 "beroset-p-650-invalid effective address");
655 result->opcode = -1;
656 return result;
657 }
658 }
659 }
660 } else {
661 o = 0;
662 result->oprs[operand].wrt = NO_SEG;
663 result->oprs[operand].segment = NO_SEG;
664 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000665
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 if (e->type != 0) { /* there'd better be nothing left! */
667 error(ERR_NONFATAL,
668 "beroset-p-663-invalid effective address");
669 result->opcode = -1;
670 return result;
671 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000672
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 result->oprs[operand].type |= MEMORY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000674
675 if (b == -1 && (i == -1 || s == 0)) {
676 int is_rel = globalbits == 64 &&
677 !(result->oprs[operand].eaflags & EAF_ABS) &&
678 ((globalrel &&
679 !(result->oprs[operand].eaflags & EAF_SEGOVER)) ||
680 (result->oprs[operand].eaflags & EAF_REL));
681
682 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
683 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 result->oprs[operand].basereg = b;
685 result->oprs[operand].indexreg = i;
686 result->oprs[operand].scale = s;
687 result->oprs[operand].offset = o;
688 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000689
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 if (is_just_unknown(value)) { /* it's immediate but unknown */
691 result->oprs[operand].type |= IMMEDIATE;
692 result->oprs[operand].offset = 0; /* don't care */
693 result->oprs[operand].segment = NO_SEG; /* don't care again */
694 result->oprs[operand].wrt = NO_SEG; /* still don't care */
695 } else if (is_reloc(value)) { /* it's immediate */
696 result->oprs[operand].type |= IMMEDIATE;
697 result->oprs[operand].offset = reloc_value(value);
698 result->oprs[operand].segment = reloc_seg(value);
699 result->oprs[operand].wrt = reloc_wrt(value);
700 if (is_simple(value)) {
701 if (reloc_value(value) == 1)
702 result->oprs[operand].type |= UNITY;
703 if (optimizing >= 0 &&
704 !(result->oprs[operand].type & STRICT)) {
705 if (reloc_value(value) >= -128 &&
706 reloc_value(value) <= 127)
707 result->oprs[operand].type |= SBYTE;
708 }
709 }
710 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000711
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 if (value->type >= EXPR_SIMPLE || value->value != 1) {
713 error(ERR_NONFATAL, "invalid operand type");
714 result->opcode = -1;
715 return result;
716 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000717
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 /*
719 * check that its only 1 register, not an expression...
720 */
721 for (i = 1; value[i].type; i++)
722 if (value[i].value) {
723 error(ERR_NONFATAL, "invalid operand type");
724 result->opcode = -1;
725 return result;
726 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000727
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 /* clear overrides, except TO which applies to FPU regs */
729 if (result->oprs[operand].type & ~TO) {
730 /*
731 * we want to produce a warning iff the specified size
732 * is different from the register size
733 */
734 i = result->oprs[operand].type & SIZE_MASK;
735 } else
736 i = 0;
737
738 result->oprs[operand].type &= TO;
739 result->oprs[operand].type |= REGISTER;
740 result->oprs[operand].type |= reg_flags[value->type];
741 result->oprs[operand].basereg = value->type;
742
743 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
744 error(ERR_WARNING | ERR_PASS1,
745 "register size specification ignored");
746 }
747 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000748 }
749
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000751
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 while (operand < 3) /* clear remaining operands */
753 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000754
755 /*
756 * Transform RESW, RESD, RESQ, REST into RESB.
757 */
758 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 case I_RESW:
760 result->opcode = I_RESB;
761 result->oprs[0].offset *= 2;
762 break;
763 case I_RESD:
764 result->opcode = I_RESB;
765 result->oprs[0].offset *= 4;
766 break;
767 case I_RESQ:
768 result->opcode = I_RESB;
769 result->oprs[0].offset *= 8;
770 break;
771 case I_REST:
772 result->opcode = I_RESB;
773 result->oprs[0].offset *= 10;
774 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000775 }
776
777 return result;
778}
779
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000781{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000782 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000783 int i;
784 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000785
H. Peter Anvin76690a12002-04-30 20:52:49 +0000786 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000788 stdscan_bufptr = p;
789 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000790}
791
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000793{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000794 extop *e;
795
796 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 e = i->eops;
798 i->eops = i->eops->next;
799 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800 }
801}