blob: 69ae3790d88299e3a024e4f581afbdf62dc55eb1 [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"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000021#include "stdscan.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022#include "parser.h"
23#include "float.h"
24
H. Peter Anvine2c80182005-01-15 22:15:51 +000025extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000026extern int32_t abs_seg; /* ABSOLUTE segment */
27extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000028
H. Peter Anvine2c80182005-01-15 22:15:51 +000029#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000030
H. Peter Anvine2c80182005-01-15 22:15:51 +000031static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000032
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000033static int i;
34static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000036static struct ofmt *outfmt; /* Structure of addresses of output routines */
37static loc_t *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000038
H. Peter Anvine2c80182005-01-15 22:15:51 +000039void parser_global_info(struct ofmt *output, loc_t * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000040{
41 outfmt = output;
42 location = locp;
43}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044
Keith Kaniosa6dfa782007-04-13 16:47:53 +000045insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +000046 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +000047{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048 int operand;
49 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000050 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051
H. Peter Anvin76690a12002-04-30 20:52:49 +000052 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000054
H. Peter Anvin76690a12002-04-30 20:52:49 +000055 stdscan_reset();
56 stdscan_bufptr = buffer;
57 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058
H. Peter Anvine2c80182005-01-15 22:15:51 +000059 result->label = NULL; /* Assume no label */
60 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +000061 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062
H. Peter Anvine2c80182005-01-15 22:15:51 +000063 if (i == 0) { /* blank line - ignore */
64 result->opcode = -1; /* and no instruction either */
65 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066 }
67 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvine2c80182005-01-15 22:15:51 +000068 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
69 error(ERR_NONFATAL, "label or instruction expected"
70 " at start of line");
71 result->opcode = -1;
72 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000073 }
74
H. Peter Anvine2c80182005-01-15 22:15:51 +000075 if (i == TOKEN_ID) { /* there's a label here */
76 result->label = tokval.t_charptr;
77 i = stdscan(NULL, &tokval);
78 if (i == ':') { /* skip over the optional colon */
79 i = stdscan(NULL, &tokval);
80 } else if (i == 0) {
81 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
82 "label alone on a line without a colon might be in error");
83 }
84 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
85 /*
86 * FIXME: location->segment could be NO_SEG, in which case
87 * it is possible we should be passing 'abs_seg'. Look into this.
88 * Work out whether that is *really* what we should be doing.
89 * Generally fix things. I think this is right as it is, but
90 * am still not certain.
91 */
92 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
93 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
94 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000095 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000096
H. Peter Anvine2c80182005-01-15 22:15:51 +000097 if (i == 0) {
98 result->opcode = -1; /* this line contains just a label */
99 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100 }
101
102 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000104
105 while (i == TOKEN_PREFIX ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000107 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108 /*
109 * Handle special case: the TIMES prefix.
110 */
111 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
112 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000113
H. Peter Anvine2c80182005-01-15 22:15:51 +0000114 i = stdscan(NULL, &tokval);
115 value =
116 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
117 i = tokval.t_type;
118 if (!value) { /* but, error in evaluator */
119 result->opcode = -1; /* unrecoverable parse error: */
120 return result; /* ignore this instruction */
121 }
122 if (!is_simple(value)) {
123 error(ERR_NONFATAL,
124 "non-constant argument supplied to TIMES");
125 result->times = 1L;
126 } else {
127 result->times = value->value;
128 if (value->value < 0) {
129 error(ERR_NONFATAL, "TIMES value %d is negative",
130 value->value);
131 result->times = 0;
132 }
133 }
134 } else {
135 if (result->nprefix == MAXPREFIX)
136 error(ERR_NONFATAL,
137 "instruction has more than %d prefixes", MAXPREFIX);
138 else
139 result->prefixes[result->nprefix++] = tokval.t_integer;
140 i = stdscan(NULL, &tokval);
141 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142 }
143
144 if (i != TOKEN_INSN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000145 if (result->nprefix > 0 && i == 0) {
146 /*
147 * Instruction prefixes are present, but no actual
148 * instruction. This is allowed: at this point we
149 * invent a notional instruction of RESB 0.
150 */
151 result->opcode = I_RESB;
152 result->operands = 1;
153 result->oprs[0].type = IMMEDIATE;
154 result->oprs[0].offset = 0L;
155 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
156 return result;
157 } else {
158 error(ERR_NONFATAL, "parser: instruction expected");
159 result->opcode = -1;
160 return result;
161 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000162 }
163
164 result->opcode = tokval.t_integer;
165 result->condition = tokval.t_inttwo;
166
167 /*
168 * RESB, RESW and RESD cannot be satisfied with incorrectly
169 * evaluated operands, since the correct values _must_ be known
170 * on the first pass. Hence, even in pass one, we set the
171 * `critical' flag on calling evaluate(), so that it will bomb
172 * out on undefined symbols. Nasty, but there's nothing we can
173 * do about it.
174 *
175 * For the moment, EQU has the same difficulty, so we'll
176 * include that.
177 */
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700178 if (result->opcode == I_RESB || result->opcode == I_RESW ||
179 result->opcode == I_RESD || result->opcode == I_RESQ ||
180 result->opcode == I_REST || result->opcode == I_RESO ||
181 result->opcode == I_EQU || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000182 critical = pass0;
183 } else
184 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000185
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700186 if (result->opcode == I_DB || result->opcode == I_DW ||
187 result->opcode == I_DD || result->opcode == I_DQ ||
188 result->opcode == I_DT || result->opcode == I_DO ||
189 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 extop *eop, **tail = &result->eops, **fixptr;
191 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193 result->eops_float = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700196 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197 */
198 while (1) {
199 i = stdscan(NULL, &tokval);
200 if (i == 0)
201 break;
202 fixptr = tail;
203 eop = *tail = nasm_malloc(sizeof(extop));
204 tail = &eop->next;
205 eop->next = NULL;
206 eop->type = EOT_NOTHING;
207 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
210 eop->type = EOT_DB_STRING;
211 eop->stringval = tokval.t_charptr;
212 eop->stringlen = tokval.t_inttwo;
213 i = stdscan(NULL, &tokval); /* eat the comma */
214 continue;
215 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000216
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000218 int32_t sign = +1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000219
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 if (i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000221 char *save = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222 i = stdscan(NULL, &tokval);
223 sign = -1L;
224 if (i != TOKEN_FLOAT || !is_comma_next()) {
225 stdscan_bufptr = save;
226 i = tokval.t_type = '-';
227 }
228 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000229
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 if (i == TOKEN_FLOAT) {
231 eop->type = EOT_DB_STRING;
232 result->eops_float = TRUE;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700233 switch (result->opcode) {
234 case I_DW:
235 eop->stringlen = 2;
236 break;
237 case I_DD:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000238 eop->stringlen = 4;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700239 break;
240 case I_DQ:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000241 eop->stringlen = 8;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700242 break;
243 case I_DT:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000244 eop->stringlen = 10;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700245 break;
246 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000247 error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700248 " encountered in `d%c' instruction"
249 ? (result->opcode == I_DO) ? 'o' : 'b');
H. Peter Anvine2c80182005-01-15 22:15:51 +0000250 /*
251 * fix suggested by Pedro Gimeno... original line
252 * was:
253 * eop->type = EOT_NOTHING;
254 */
255 eop->stringlen = 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700256 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000257 }
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700258 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259 tail = &eop->next;
260 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000261 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700262 if (!eop->stringlen ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000263 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000264 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000265 eop->stringlen, error))
266 eop->type = EOT_NOTHING;
267 i = stdscan(NULL, &tokval); /* eat the comma */
268 continue;
269 }
270 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000271
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272 /* anything else */
273 {
274 expr *value;
275 value = evaluate(stdscan, NULL, &tokval, NULL,
276 critical, error, NULL);
277 i = tokval.t_type;
278 if (!value) { /* error in evaluator */
279 result->opcode = -1; /* unrecoverable parse error: */
280 return result; /* ignore this instruction */
281 }
282 if (is_unknown(value)) {
283 eop->type = EOT_DB_NUMBER;
284 eop->offset = 0; /* doesn't matter what we put */
285 eop->segment = eop->wrt = NO_SEG; /* likewise */
286 } else if (is_reloc(value)) {
287 eop->type = EOT_DB_NUMBER;
288 eop->offset = reloc_value(value);
289 eop->segment = reloc_seg(value);
290 eop->wrt = reloc_wrt(value);
291 } else {
292 error(ERR_NONFATAL,
293 "operand %d: expression is not simple"
294 " or relocatable", oper_num);
295 }
296 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000297
H. Peter Anvine2c80182005-01-15 22:15:51 +0000298 /*
299 * We're about to call stdscan(), which will eat the
300 * comma that we're currently sitting on between
301 * arguments. However, we'd better check first that it
302 * _is_ a comma.
303 */
304 if (i == 0) /* also could be EOL */
305 break;
306 if (i != ',') {
307 error(ERR_NONFATAL, "comma expected after operand %d",
308 oper_num);
309 result->opcode = -1; /* unrecoverable parse error: */
310 return result; /* ignore this instruction */
311 }
312 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000313
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314 if (result->opcode == I_INCBIN) {
315 /*
316 * Correct syntax for INCBIN is that there should be
317 * one string operand, followed by one or two numeric
318 * operands.
319 */
320 if (!result->eops || result->eops->type != EOT_DB_STRING)
321 error(ERR_NONFATAL, "`incbin' expects a file name");
322 else if (result->eops->next &&
323 result->eops->next->type != EOT_DB_NUMBER)
324 error(ERR_NONFATAL, "`incbin': second parameter is",
325 " non-numeric");
326 else if (result->eops->next && result->eops->next->next &&
327 result->eops->next->next->type != EOT_DB_NUMBER)
328 error(ERR_NONFATAL, "`incbin': third parameter is",
329 " non-numeric");
330 else if (result->eops->next && result->eops->next->next &&
331 result->eops->next->next->next)
332 error(ERR_NONFATAL,
333 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000334 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000335 return result;
336 /*
337 * If we reach here, one of the above errors happened.
338 * Throw the instruction away.
339 */
340 result->opcode = -1;
341 return result;
342 } else /* DB ... */ if (oper_num == 0)
343 error(ERR_WARNING | ERR_PASS1,
344 "no operand for data declaration");
345 else
346 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347
H. Peter Anvine2c80182005-01-15 22:15:51 +0000348 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000349 }
350
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700351 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000352 * of these, separated by commas, and terminated by a zero token. */
353
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700354 for (operand = 0; operand < MAX_OPERANDS; operand++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 expr *value; /* used most of the time */
356 int mref; /* is this going to be a memory ref? */
357 int bracket; /* is it a [] mref, or a & mref? */
358 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360 result->oprs[operand].addr_size = 0; /* have to zero this whatever */
361 result->oprs[operand].eaflags = 0; /* and this */
362 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000363
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 i = stdscan(NULL, &tokval);
365 if (i == 0)
366 break; /* end of operands: get out of here */
367 result->oprs[operand].type = 0; /* so far, no override */
368 while (i == TOKEN_SPECIAL) { /* size specifiers */
369 switch ((int)tokval.t_integer) {
370 case S_BYTE:
371 if (!setsize) /* we want to use only the first */
372 result->oprs[operand].type |= BITS8;
373 setsize = 1;
374 break;
375 case S_WORD:
376 if (!setsize)
377 result->oprs[operand].type |= BITS16;
378 setsize = 1;
379 break;
380 case S_DWORD:
381 case S_LONG:
382 if (!setsize)
383 result->oprs[operand].type |= BITS32;
384 setsize = 1;
385 break;
386 case S_QWORD:
387 if (!setsize)
388 result->oprs[operand].type |= BITS64;
389 setsize = 1;
390 break;
391 case S_TWORD:
392 if (!setsize)
393 result->oprs[operand].type |= BITS80;
394 setsize = 1;
395 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700396 case S_OWORD:
397 if (!setsize)
398 result->oprs[operand].type |= BITS128;
399 setsize = 1;
400 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 case S_TO:
402 result->oprs[operand].type |= TO;
403 break;
404 case S_STRICT:
405 result->oprs[operand].type |= STRICT;
406 break;
407 case S_FAR:
408 result->oprs[operand].type |= FAR;
409 break;
410 case S_NEAR:
411 result->oprs[operand].type |= NEAR;
412 break;
413 case S_SHORT:
414 result->oprs[operand].type |= SHORT;
415 break;
416 default:
417 error(ERR_NONFATAL, "invalid operand size specification");
418 }
419 i = stdscan(NULL, &tokval);
420 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000421
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 if (i == '[' || i == '&') { /* memory reference */
423 mref = TRUE;
424 bracket = (i == '[');
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000425 while ((i = stdscan(NULL, &tokval)) == TOKEN_SPECIAL) {
426 /* check for address directives */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 if (tasm_compatible_mode) {
428 switch ((int)tokval.t_integer) {
429 /* For TASM compatibility a size override inside the
430 * brackets changes the size of the operand, not the
431 * address type of the operand as it does in standard
432 * NASM syntax. Hence:
433 *
434 * mov eax,[DWORD val]
435 *
436 * is valid syntax in TASM compatibility mode. Note that
437 * you lose the ability to override the default address
438 * type for the instruction, but we never use anything
439 * but 32-bit flat model addressing in our code.
440 */
441 case S_BYTE:
442 result->oprs[operand].type |= BITS8;
443 break;
444 case S_WORD:
445 result->oprs[operand].type |= BITS16;
446 break;
447 case S_DWORD:
448 case S_LONG:
449 result->oprs[operand].type |= BITS32;
450 break;
451 case S_QWORD:
452 result->oprs[operand].type |= BITS64;
453 break;
454 case S_TWORD:
455 result->oprs[operand].type |= BITS80;
456 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700457 case S_OWORD:
458 result->oprs[operand].type |= BITS128;
459 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 default:
461 error(ERR_NONFATAL,
462 "invalid operand size specification");
463 }
464 } else {
465 /* Standard NASM compatible syntax */
466 switch ((int)tokval.t_integer) {
467 case S_NOSPLIT:
468 result->oprs[operand].eaflags |= EAF_TIMESTWO;
469 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000470 case S_REL:
471 result->oprs[operand].eaflags |= EAF_REL;
472 break;
473 case S_ABS:
474 result->oprs[operand].eaflags |= EAF_ABS;
475 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 case S_BYTE:
477 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
478 break;
479 case S_WORD:
480 result->oprs[operand].addr_size = 16;
481 result->oprs[operand].eaflags |= EAF_WORDOFFS;
482 break;
483 case S_DWORD:
484 case S_LONG:
485 result->oprs[operand].addr_size = 32;
486 result->oprs[operand].eaflags |= EAF_WORDOFFS;
487 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000488 case S_QWORD:
489 result->oprs[operand].addr_size = 64;
490 result->oprs[operand].eaflags |= EAF_WORDOFFS;
491 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 default:
493 error(ERR_NONFATAL, "invalid size specification in"
494 " effective address");
495 }
496 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 }
498 } else { /* immediate operand, or register */
499 mref = FALSE;
500 bracket = FALSE; /* placate optimisers */
501 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000502
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 if ((result->oprs[operand].type & FAR) && !mref &&
504 result->opcode != I_JMP && result->opcode != I_CALL) {
505 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
506 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000507
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 value = evaluate(stdscan, NULL, &tokval,
509 &result->oprs[operand].opflags,
510 critical, error, &hints);
511 i = tokval.t_type;
512 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
513 result->forw_ref = TRUE;
514 }
515 if (!value) { /* error in evaluator */
516 result->opcode = -1; /* unrecoverable parse error: */
517 return result; /* ignore this instruction */
518 }
519 if (i == ':' && mref) { /* it was seg:offset */
520 /*
521 * Process the segment override.
522 */
523 if (value[1].type != 0 || value->value != 1 ||
524 REG_SREG & ~reg_flags[value->type])
525 error(ERR_NONFATAL, "invalid segment override");
526 else if (result->nprefix == MAXPREFIX)
527 error(ERR_NONFATAL,
528 "instruction has more than %d prefixes", MAXPREFIX);
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000529 else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 result->prefixes[result->nprefix++] = value->type;
H. Peter Anvin490bbcd2007-08-29 20:30:31 +0000531 if (!(REG_FSGS & ~reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000532 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000533 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000534
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 i = stdscan(NULL, &tokval); /* then skip the colon */
536 if (i == TOKEN_SPECIAL) { /* another check for size override */
537 switch ((int)tokval.t_integer) {
538 case S_WORD:
539 result->oprs[operand].addr_size = 16;
540 break;
541 case S_DWORD:
542 case S_LONG:
543 result->oprs[operand].addr_size = 32;
544 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000545 case S_QWORD:
546 result->oprs[operand].addr_size = 64;
547 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 default:
549 error(ERR_NONFATAL, "invalid size specification in"
550 " effective address");
551 }
552 i = stdscan(NULL, &tokval);
553 }
554 value = evaluate(stdscan, NULL, &tokval,
555 &result->oprs[operand].opflags,
556 critical, error, &hints);
557 i = tokval.t_type;
558 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
559 result->forw_ref = TRUE;
560 }
561 /* and get the offset */
562 if (!value) { /* but, error in evaluator */
563 result->opcode = -1; /* unrecoverable parse error: */
564 return result; /* ignore this instruction */
565 }
566 }
567 if (mref && bracket) { /* find ] at the end */
568 if (i != ']') {
569 error(ERR_NONFATAL, "parser: expecting ]");
570 do { /* error recovery again */
571 i = stdscan(NULL, &tokval);
572 } while (i != 0 && i != ',');
573 } else /* we got the required ] */
574 i = stdscan(NULL, &tokval);
575 } else { /* immediate operand */
576 if (i != 0 && i != ',' && i != ':') {
577 error(ERR_NONFATAL, "comma or end of line expected");
578 do { /* error recovery */
579 i = stdscan(NULL, &tokval);
580 } while (i != 0 && i != ',');
581 } else if (i == ':') {
582 result->oprs[operand].type |= COLON;
583 }
584 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000585
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 /* now convert the exprs returned from evaluate() into operand
587 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000588
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 if (mref) { /* it's a memory reference */
590 expr *e = value;
591 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000592 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000593
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 b = i = -1, o = s = 0;
595 result->oprs[operand].hintbase = hints.base;
596 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
599 if (e->value == 1) /* in fact it can be basereg */
600 b = e->type;
601 else /* no, it has to be indexreg */
602 i = e->type, s = e->value;
603 e++;
604 }
605 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
606 if (b != -1) /* If the first was the base, ... */
607 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000608
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 else if (e->value != 1) { /* If both want to be index */
610 error(ERR_NONFATAL,
611 "beroset-p-592-invalid effective address");
612 result->opcode = -1;
613 return result;
614 } else
615 b = e->type;
616 e++;
617 }
618 if (e->type != 0) { /* is there an offset? */
619 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
620 error(ERR_NONFATAL,
621 "beroset-p-603-invalid effective address");
622 result->opcode = -1;
623 return result;
624 } else {
625 if (e->type == EXPR_UNKNOWN) {
626 o = 0; /* doesn't matter what */
627 result->oprs[operand].wrt = NO_SEG; /* nor this */
628 result->oprs[operand].segment = NO_SEG; /* or this */
629 while (e->type)
630 e++; /* go to the end of the line */
631 } else {
632 if (e->type == EXPR_SIMPLE) {
633 o = e->value;
634 e++;
635 }
636 if (e->type == EXPR_WRT) {
637 result->oprs[operand].wrt = e->value;
638 e++;
639 } else
640 result->oprs[operand].wrt = NO_SEG;
641 /*
642 * Look for a segment base type.
643 */
644 if (e->type && e->type < EXPR_SEGBASE) {
645 error(ERR_NONFATAL,
646 "beroset-p-630-invalid effective address");
647 result->opcode = -1;
648 return result;
649 }
650 while (e->type && e->value == 0)
651 e++;
652 if (e->type && e->value != 1) {
653 error(ERR_NONFATAL,
654 "beroset-p-637-invalid effective address");
655 result->opcode = -1;
656 return result;
657 }
658 if (e->type) {
659 result->oprs[operand].segment =
660 e->type - EXPR_SEGBASE;
661 e++;
662 } else
663 result->oprs[operand].segment = NO_SEG;
664 while (e->type && e->value == 0)
665 e++;
666 if (e->type) {
667 error(ERR_NONFATAL,
668 "beroset-p-650-invalid effective address");
669 result->opcode = -1;
670 return result;
671 }
672 }
673 }
674 } else {
675 o = 0;
676 result->oprs[operand].wrt = NO_SEG;
677 result->oprs[operand].segment = NO_SEG;
678 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000679
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 if (e->type != 0) { /* there'd better be nothing left! */
681 error(ERR_NONFATAL,
682 "beroset-p-663-invalid effective address");
683 result->opcode = -1;
684 return result;
685 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000686
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700687 /* It is memory, but it can match any r/m operand */
688 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000689
690 if (b == -1 && (i == -1 || s == 0)) {
691 int is_rel = globalbits == 64 &&
692 !(result->oprs[operand].eaflags & EAF_ABS) &&
693 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000694 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000695 (result->oprs[operand].eaflags & EAF_REL));
696
697 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
698 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 result->oprs[operand].basereg = b;
700 result->oprs[operand].indexreg = i;
701 result->oprs[operand].scale = s;
702 result->oprs[operand].offset = o;
703 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000704
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 if (is_just_unknown(value)) { /* it's immediate but unknown */
706 result->oprs[operand].type |= IMMEDIATE;
707 result->oprs[operand].offset = 0; /* don't care */
708 result->oprs[operand].segment = NO_SEG; /* don't care again */
709 result->oprs[operand].wrt = NO_SEG; /* still don't care */
710 } else if (is_reloc(value)) { /* it's immediate */
711 result->oprs[operand].type |= IMMEDIATE;
712 result->oprs[operand].offset = reloc_value(value);
713 result->oprs[operand].segment = reloc_seg(value);
714 result->oprs[operand].wrt = reloc_wrt(value);
715 if (is_simple(value)) {
716 if (reloc_value(value) == 1)
717 result->oprs[operand].type |= UNITY;
718 if (optimizing >= 0 &&
719 !(result->oprs[operand].type & STRICT)) {
720 if (reloc_value(value) >= -128 &&
721 reloc_value(value) <= 127)
722 result->oprs[operand].type |= SBYTE;
723 }
724 }
725 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000726
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 if (value->type >= EXPR_SIMPLE || value->value != 1) {
728 error(ERR_NONFATAL, "invalid operand type");
729 result->opcode = -1;
730 return result;
731 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000732
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 /*
734 * check that its only 1 register, not an expression...
735 */
736 for (i = 1; value[i].type; i++)
737 if (value[i].value) {
738 error(ERR_NONFATAL, "invalid operand type");
739 result->opcode = -1;
740 return result;
741 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000742
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 /* clear overrides, except TO which applies to FPU regs */
744 if (result->oprs[operand].type & ~TO) {
745 /*
746 * we want to produce a warning iff the specified size
747 * is different from the register size
748 */
749 i = result->oprs[operand].type & SIZE_MASK;
750 } else
751 i = 0;
752
753 result->oprs[operand].type &= TO;
754 result->oprs[operand].type |= REGISTER;
755 result->oprs[operand].type |= reg_flags[value->type];
756 result->oprs[operand].basereg = value->type;
757
758 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
759 error(ERR_WARNING | ERR_PASS1,
760 "register size specification ignored");
761 }
762 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000763 }
764
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000766
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 while (operand < 3) /* clear remaining operands */
768 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000769
770 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700771 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000772 */
773 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 case I_RESW:
775 result->opcode = I_RESB;
776 result->oprs[0].offset *= 2;
777 break;
778 case I_RESD:
779 result->opcode = I_RESB;
780 result->oprs[0].offset *= 4;
781 break;
782 case I_RESQ:
783 result->opcode = I_RESB;
784 result->oprs[0].offset *= 8;
785 break;
786 case I_REST:
787 result->opcode = I_RESB;
788 result->oprs[0].offset *= 10;
789 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700790 case I_RESO:
791 result->opcode = I_RESB;
792 result->oprs[0].offset *= 16;
793 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700794 default:
795 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000796 }
797
798 return result;
799}
800
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000802{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000803 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000804 int i;
805 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000806
H. Peter Anvin76690a12002-04-30 20:52:49 +0000807 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000809 stdscan_bufptr = p;
810 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811}
812
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000814{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815 extop *e;
816
817 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 e = i->eops;
819 i->eops = i->eops->next;
820 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000821 }
822}