blob: e61d0a6f6d3eae4c7988d3ceb9c979e1a6e81e2e [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvine20ca022013-07-19 17:06:08 -07003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * parser.c source line parser for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000048#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000049#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000050#include "stdscan.h"
H. Peter Anvin00444ae2009-07-18 18:49:55 -070051#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052#include "parser.h"
53#include "float.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055
H. Peter Anvine2c80182005-01-15 22:15:51 +000056extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030057extern int32_t abs_seg; /* ABSOLUTE segment */
58extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000059
H. Peter Anvine2c80182005-01-15 22:15:51 +000060static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062static int i;
63static struct tokenval tokval;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030064static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000065
H. Peter Anvin605f5152009-07-18 18:31:41 -070066void parser_global_info(struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000067{
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 location = locp;
69}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040071static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070072{
73 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080074 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030075 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070076 case R_CS:
77 case R_DS:
78 case R_SS:
79 case R_ES:
80 case R_FS:
81 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030082 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070083 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080084 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070085 case P_REP:
86 case P_REPE:
87 case P_REPZ:
88 case P_REPNE:
89 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080090 case P_XACQUIRE:
91 case P_XRELEASE:
Jin Kyu Song03041092013-10-15 19:38:51 -070092 case P_BND:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080093 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070094 case P_O16:
95 case P_O32:
96 case P_O64:
97 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030098 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070099 case P_A16:
100 case P_A32:
101 case P_A64:
102 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300103 return PPS_ASIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700104 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300105 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
106 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700107 }
108}
109
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700110static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700111{
112 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300113 switch ((int)tokval.t_integer) {
114 /* For TASM compatibility a size override inside the
115 * brackets changes the size of the operand, not the
116 * address type of the operand as it does in standard
117 * NASM syntax. Hence:
118 *
119 * mov eax,[DWORD val]
120 *
121 * is valid syntax in TASM compatibility mode. Note that
122 * you lose the ability to override the default address
123 * type for the instruction, but we never use anything
124 * but 32-bit flat model addressing in our code.
125 */
126 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700127 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300128 break;
129 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700130 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300131 break;
132 case S_DWORD:
133 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700134 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300135 break;
136 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700137 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300138 break;
139 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700140 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300141 break;
142 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700143 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300144 break;
145 default:
146 nasm_error(ERR_NONFATAL,
147 "invalid operand size specification");
148 break;
149 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700150 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300151 /* Standard NASM compatible syntax */
152 switch ((int)tokval.t_integer) {
153 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700154 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300155 break;
156 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700157 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300158 break;
159 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700160 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300161 break;
162 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700163 op->disp_size = 8;
164 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300165 break;
166 case P_A16:
167 case P_A32:
168 case P_A64:
169 if (result->prefixes[PPS_ASIZE] &&
170 result->prefixes[PPS_ASIZE] != tokval.t_integer)
171 nasm_error(ERR_NONFATAL,
172 "conflicting address size specifications");
173 else
174 result->prefixes[PPS_ASIZE] = tokval.t_integer;
175 break;
176 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700177 op->disp_size = 16;
178 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300179 break;
180 case S_DWORD:
181 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700182 op->disp_size = 32;
183 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300184 break;
185 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700186 op->disp_size = 64;
187 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300188 break;
189 default:
190 nasm_error(ERR_NONFATAL, "invalid size specification in"
191 " effective address");
192 break;
193 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700194 }
195}
196
Jin Kyu Song72018a22013-08-05 20:46:18 -0700197/*
198 * when two or more decorators follow a register operand,
199 * consecutive decorators are parsed here.
Jin Kyu Songf9a71e02013-08-21 19:29:09 -0700200 * opmask and zeroing decorators can be placed in any order.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700201 * e.g. zmm1 {k2}{z} or zmm2 {z,k3}
202 * decorator(s) are placed at the end of an operand.
203 */
204static bool parse_braces(decoflags_t *decoflags)
205{
206 int i;
207 bool recover = false;
208
209 i = tokval.t_type;
210 do {
211 if (i == TOKEN_OPMASK) {
212 if (*decoflags & OPMASK_MASK) {
213 nasm_error(ERR_NONFATAL, "opmask k%lu is already set",
214 *decoflags & OPMASK_MASK);
215 *decoflags &= ~OPMASK_MASK;
216 }
217 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
218 } else if (i == TOKEN_DECORATOR) {
219 switch (tokval.t_integer) {
220 case BRC_Z:
221 /*
222 * according to AVX512 spec, only zeroing/merging decorator
223 * is supported with opmask
224 */
225 *decoflags |= GEN_Z(0);
226 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700227 default:
228 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
229 tokval.t_charptr);
230 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700231 }
232 } else if (i == ',' || i == TOKEN_EOS){
233 break;
234 } else {
235 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
236 " expected");
237 recover = true;
238 break;
239 }
240 i = stdscan(NULL, &tokval);
241 } while(1);
242
243 return recover;
244}
245
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700246static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700247{
248 int b, i, s; /* basereg, indexreg, scale */
249 int64_t o; /* offset */
250
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700251 b = i = -1;
252 o = s = 0;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700253
254 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
255 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
256
257 if (is_gpr && e->value == 1)
258 b = e->type; /* It can be basereg */
259 else /* No, it has to be indexreg */
260 i = e->type, s = e->value;
261 e++;
262 }
263 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
264 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
265
266 if (b != -1) /* If the first was the base, ... */
267 i = e->type, s = e->value; /* second has to be indexreg */
268
269 else if (!is_gpr || e->value != 1) {
270 /* If both want to be index */
271 nasm_error(ERR_NONFATAL,
272 "invalid effective address: two index registers");
273 return -1;
274 } else
275 b = e->type;
276 e++;
277 }
278 if (e->type != 0) { /* is there an offset? */
279 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
280 nasm_error(ERR_NONFATAL,
281 "beroset-p-603-invalid effective address");
282 return -1;
283 } else {
284 if (e->type == EXPR_UNKNOWN) {
285 op->opflags |= OPFLAG_UNKNOWN;
286 o = 0; /* doesn't matter what */
287 op->wrt = NO_SEG; /* nor this */
288 op->segment = NO_SEG; /* or this */
289 while (e->type)
290 e++; /* go to the end of the line */
291 } else {
292 if (e->type == EXPR_SIMPLE) {
293 o = e->value;
294 e++;
295 }
296 if (e->type == EXPR_WRT) {
297 op->wrt = e->value;
298 e++;
299 } else
300 op->wrt = NO_SEG;
301 /*
302 * Look for a segment base type.
303 */
304 if (e->type && e->type < EXPR_SEGBASE) {
305 nasm_error(ERR_NONFATAL,
306 "beroset-p-630-invalid effective address");
307 return -1;
308 }
309 while (e->type && e->value == 0)
310 e++;
311 if (e->type && e->value != 1) {
312 nasm_error(ERR_NONFATAL,
313 "beroset-p-637-invalid effective address");
314 return -1;
315 }
316 if (e->type) {
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700317 op->segment = e->type - EXPR_SEGBASE;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700318 e++;
319 } else
320 op->segment = NO_SEG;
321 while (e->type && e->value == 0)
322 e++;
323 if (e->type) {
324 nasm_error(ERR_NONFATAL,
325 "beroset-p-650-invalid effective address");
326 return -1;
327 }
328 }
329 }
330 } else {
331 o = 0;
332 op->wrt = NO_SEG;
333 op->segment = NO_SEG;
334 }
335
336 if (e->type != 0) { /* there'd better be nothing left! */
337 nasm_error(ERR_NONFATAL,
338 "beroset-p-663-invalid effective address");
339 return -1;
340 }
341
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700342 op->basereg = b;
343 op->indexreg = i;
344 op->scale = s;
345 op->offset = o;
346 return 0;
347}
348
349static void mref_set_optype(operand *op)
350{
351 int b = op->basereg;
352 int i = op->indexreg;
353 int s = op->scale;
354
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700355 /* It is memory, but it can match any r/m operand */
356 op->type |= MEMORY_ANY;
357
358 if (b == -1 && (i == -1 || s == 0)) {
359 int is_rel = globalbits == 64 &&
360 !(op->eaflags & EAF_ABS) &&
361 ((globalrel &&
362 !(op->eaflags & EAF_FSGS)) ||
363 (op->eaflags & EAF_REL));
364
365 op->type |= is_rel ? IP_REL : MEM_OFFS;
366 }
367
368 if (i != -1) {
369 opflags_t iclass = nasm_reg_flags[i];
370
371 if (is_class(XMMREG,iclass))
372 op->type |= XMEM;
373 else if (is_class(YMMREG,iclass))
374 op->type |= YMEM;
375 else if (is_class(ZMMREG,iclass))
376 op->type |= ZMEM;
377 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700378}
379
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700380insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000381{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400382 bool insn_is_label = false;
383 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700384 int opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000385 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800386 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700387 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000388
H. Peter Anvin9c987692007-11-04 21:09:32 -0800389restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400390 first = true;
391 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000392
H. Peter Anvin76690a12002-04-30 20:52:49 +0000393 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300394 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000395 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000396
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400397 result->label = NULL; /* Assume no label */
398 result->eops = NULL; /* must do this, whatever happens */
399 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700400 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
401 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000402
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400403 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700404 if (i == TOKEN_EOS)
405 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400406
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400407 if (i != TOKEN_ID &&
408 i != TOKEN_INSN &&
409 i != TOKEN_PREFIX &&
410 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
411 nasm_error(ERR_NONFATAL,
412 "label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700413 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000414 }
415
H. Peter Anvin9c987692007-11-04 21:09:32 -0800416 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
417 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300418 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 result->label = tokval.t_charptr;
420 i = stdscan(NULL, &tokval);
421 if (i == ':') { /* skip over the optional colon */
422 i = stdscan(NULL, &tokval);
423 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700424 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 "label alone on a line without a colon might be in error");
426 }
427 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
428 /*
429 * FIXME: location->segment could be NO_SEG, in which case
430 * it is possible we should be passing 'abs_seg'. Look into this.
431 * Work out whether that is *really* what we should be doing.
432 * Generally fix things. I think this is right as it is, but
433 * am still not certain.
434 */
435 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700436 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000438 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000439
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400440 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700441 if (i == TOKEN_EOS)
442 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000443
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400444 nasm_build_assert(P_none != 0);
445 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000446 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000447
448 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400449 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300450 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800451
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 /*
453 * Handle special case: the TIMES prefix.
454 */
455 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
456 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000457
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400459 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700461 if (!value) /* Error in evaluator */
462 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700464 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 "non-constant argument supplied to TIMES");
466 result->times = 1L;
467 } else {
468 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700469 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200470 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 value->value);
472 result->times = 0;
473 }
474 }
475 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300476 int slot = prefix_slot(tokval.t_integer);
477 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700478 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100479 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300480 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700481 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300482 nasm_error(ERR_NONFATAL,
483 "instruction has conflicting prefixes");
484 }
485 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 i = stdscan(NULL, &tokval);
487 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488 }
489
490 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300491 int j;
492 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700493
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400494 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300495 if ((pfx = result->prefixes[j]) != P_none)
496 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400497 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700498
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700499 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 /*
501 * Instruction prefixes are present, but no actual
502 * instruction. This is allowed: at this point we
503 * invent a notional instruction of RESB 0.
504 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400505 result->opcode = I_RESB;
506 result->operands = 1;
507 result->oprs[0].type = IMMEDIATE;
508 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
510 return result;
511 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700512 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700513 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000515 }
516
517 result->opcode = tokval.t_integer;
518 result->condition = tokval.t_inttwo;
519
520 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700521 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000522 * evaluated operands, since the correct values _must_ be known
523 * on the first pass. Hence, even in pass one, we set the
524 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700525 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000526 */
Charles Crayne2581c862008-09-10 19:21:52 -0700527 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700528 critical = (pass0 < 2 ? 1 : 2);
529
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 } else
531 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000532
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700533 if (result->opcode == I_DB || result->opcode == I_DW ||
534 result->opcode == I_DD || result->opcode == I_DQ ||
535 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700536 result->opcode == I_DY || result->opcode == I_DZ ||
537 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 extop *eop, **tail = &result->eops, **fixptr;
539 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300540 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000541
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700542 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000543
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700545 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 */
547 while (1) {
548 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400549 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300551 else if (first && i == ':') {
552 insn_is_label = true;
553 goto restart_parse;
554 }
555 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 fixptr = tail;
557 eop = *tail = nasm_malloc(sizeof(extop));
558 tail = &eop->next;
559 eop->next = NULL;
560 eop->type = EOT_NOTHING;
561 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300562 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000563
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300564 /*
565 * is_comma_next() here is to distinguish this from
566 * a string used as part of an expression...
567 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700568 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400569 eop->type = EOT_DB_STRING;
570 eop->stringval = tokval.t_charptr;
571 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300573 } else if (i == TOKEN_STRFUNC) {
574 bool parens = false;
575 const char *funcname = tokval.t_charptr;
576 enum strfunc func = tokval.t_integer;
577 i = stdscan(NULL, &tokval);
578 if (i == '(') {
579 parens = true;
580 i = stdscan(NULL, &tokval);
581 }
582 if (i != TOKEN_STR) {
583 nasm_error(ERR_NONFATAL,
584 "%s must be followed by a string constant",
585 funcname);
586 eop->type = EOT_NOTHING;
587 } else {
588 eop->type = EOT_DB_STRING_FREE;
589 eop->stringlen =
590 string_transform(tokval.t_charptr, tokval.t_inttwo,
591 &eop->stringval, func);
592 if (eop->stringlen == (size_t)-1) {
593 nasm_error(ERR_NONFATAL, "invalid string for transform");
594 eop->type = EOT_NOTHING;
595 }
596 }
597 if (parens && i && i != ')') {
598 i = stdscan(NULL, &tokval);
599 if (i != ')') {
600 nasm_error(ERR_NONFATAL, "unterminated %s function",
601 funcname);
602 }
603 }
604 if (i && i != ',')
605 i = stdscan(NULL, &tokval);
606 } else if (i == '-' || i == '+') {
607 char *save = stdscan_get();
608 int token = i;
609 sign = (i == '-') ? -1 : 1;
610 i = stdscan(NULL, &tokval);
611 if (i != TOKEN_FLOAT) {
612 stdscan_set(save);
613 i = tokval.t_type = token;
614 goto is_expression;
615 } else {
616 goto is_float;
617 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700618 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300619is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300620 eop->type = EOT_DB_STRING;
621 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300622
623 eop->stringlen = idata_bytes(result->opcode);
624 if (eop->stringlen > 16) {
625 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700626 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300627 eop->stringlen = 0;
628 } else if (eop->stringlen < 1) {
629 nasm_error(ERR_NONFATAL, "floating-point constant"
630 " encountered in unknown instruction");
631 /*
632 * fix suggested by Pedro Gimeno... original line was:
633 * eop->type = EOT_NOTHING;
634 */
635 eop->stringlen = 0;
636 }
637
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300638 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
639 tail = &eop->next;
640 *fixptr = eop;
641 eop->stringval = (char *)eop + sizeof(extop);
642 if (!eop->stringlen ||
643 !float_const(tokval.t_charptr, sign,
644 (uint8_t *)eop->stringval,
645 eop->stringlen, nasm_error))
646 eop->type = EOT_NOTHING;
647 i = stdscan(NULL, &tokval); /* eat the comma */
648 } else {
649 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700651
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300652is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700654 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700656 if (!value) /* Error in evaluator */
657 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 if (is_unknown(value)) {
659 eop->type = EOT_DB_NUMBER;
660 eop->offset = 0; /* doesn't matter what we put */
661 eop->segment = eop->wrt = NO_SEG; /* likewise */
662 } else if (is_reloc(value)) {
663 eop->type = EOT_DB_NUMBER;
664 eop->offset = reloc_value(value);
665 eop->segment = reloc_seg(value);
666 eop->wrt = reloc_wrt(value);
667 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700668 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 "operand %d: expression is not simple"
670 " or relocatable", oper_num);
671 }
672 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000673
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 /*
675 * We're about to call stdscan(), which will eat the
676 * comma that we're currently sitting on between
677 * arguments. However, we'd better check first that it
678 * _is_ a comma.
679 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400680 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 break;
682 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700683 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400684 oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700685 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 }
687 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000688
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 if (result->opcode == I_INCBIN) {
690 /*
691 * Correct syntax for INCBIN is that there should be
692 * one string operand, followed by one or two numeric
693 * operands.
694 */
695 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700696 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 else if (result->eops->next &&
698 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200699 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400700 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701 else if (result->eops->next && result->eops->next->next &&
702 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200703 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400704 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 else if (result->eops->next && result->eops->next->next &&
706 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700707 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400708 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000709 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 return result;
711 /*
712 * If we reach here, one of the above errors happened.
713 * Throw the instruction away.
714 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700715 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700717 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 "no operand for data declaration");
719 else
720 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000721
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000723 }
724
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400725 /*
726 * Now we begin to parse the operands. There may be up to four
727 * of these, separated by commas, and terminated by a zero token.
728 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000729
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700730 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
731 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300732 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700733 bool mref; /* is this going to be a memory ref? */
734 bool bracket; /* is it a [] mref, or a & mref? */
735 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700737 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000738
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700739 op->disp_size = 0; /* have to zero this whatever */
740 op->eaflags = 0; /* and this */
741 op->opflags = 0;
742 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000743
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400745 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300747 else if (first && i == ':') {
748 insn_is_label = true;
749 goto restart_parse;
750 }
751 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700752 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 while (i == TOKEN_SPECIAL) { /* size specifiers */
754 switch ((int)tokval.t_integer) {
755 case S_BYTE:
756 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700757 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 setsize = 1;
759 break;
760 case S_WORD:
761 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700762 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 setsize = 1;
764 break;
765 case S_DWORD:
766 case S_LONG:
767 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700768 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 setsize = 1;
770 break;
771 case S_QWORD:
772 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700773 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 setsize = 1;
775 break;
776 case S_TWORD:
777 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700778 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 setsize = 1;
780 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700781 case S_OWORD:
782 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700783 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700784 setsize = 1;
785 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700786 case S_YWORD:
787 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700788 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700789 setsize = 1;
790 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700791 case S_ZWORD:
792 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700793 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700794 setsize = 1;
795 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700797 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 break;
799 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700800 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 break;
802 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700803 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 break;
805 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700806 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 break;
808 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700809 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 break;
811 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700812 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 }
814 i = stdscan(NULL, &tokval);
815 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000816
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700818 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700820 i = stdscan(NULL, &tokval); /* then skip the colon */
821 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700822 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700823 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700825 /* when a comma follows an opening bracket - [ , eax*4] */
826 if (i == ',') {
827 /* treat as if there is a zero displacement virtually */
828 tokval.t_type = TOKEN_NUM;
829 tokval.t_integer = 0;
830 stdscan_set(stdscan_get() - 1); /* rewind the comma */
831 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700833 mref = false;
834 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000836
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700837 if ((op->type & FAR) && !mref &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700839 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700843 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700844 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700846 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700847 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700849 if (!value) /* Error in evaluator */
850 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 if (i == ':' && mref) { /* it was seg:offset */
852 /*
853 * Process the segment override.
854 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400855 if (value[1].type != 0 ||
856 value->value != 1 ||
857 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700858 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700859 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700860 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700861 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000862 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300863 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400864 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700865 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300866 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000867
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700869 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700870 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 i = stdscan(NULL, &tokval);
872 }
873 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700874 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700875 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700877 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700878 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 }
880 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700881 if (!value) /* Error in evaluator */
882 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200884
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700885 mib = false;
886 if (mref && bracket && i == ',') {
887 /* [seg:base+offset,index*scale] syntax (mib) */
888
889 operand o1, o2; /* Partial operands */
890
891 if (parse_mref(&o1, value))
892 goto fail;
893
894 i = stdscan(NULL, &tokval); /* Eat comma */
895 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
896 critical, nasm_error, &hints);
897 i = tokval.t_type;
898
899 if (parse_mref(&o2, value))
900 goto fail;
901
902 if (o2.basereg != -1 && o2.indexreg == -1) {
903 o2.indexreg = o2.basereg;
904 o2.scale = 1;
905 o2.basereg = -1;
906 }
907
908 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
909 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
910 nasm_error(ERR_NONFATAL, "invalid mib expression");
911 goto fail;
912 }
913
914 op->basereg = o1.basereg;
915 op->indexreg = o2.indexreg;
916 op->scale = o2.scale;
917 op->offset = o1.offset;
918 op->segment = o1.segment;
919 op->wrt = o1.wrt;
920
921 if (op->basereg != -1) {
922 op->hintbase = op->basereg;
923 op->hinttype = EAH_MAKEBASE;
924 } else if (op->indexreg != -1) {
925 op->hintbase = op->indexreg;
926 op->hinttype = EAH_NOTBASE;
927 } else {
928 op->hintbase = -1;
929 op->hinttype = EAH_NOHINT;
930 }
931
932 mib = true;
933 }
934
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700935 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 if (mref && bracket) { /* find ] at the end */
937 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700938 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200939 recover = true;
940 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700942 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700943 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700944 * according to AVX512 spec, broacast or opmask decorator
945 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700946 */
947 if (tokval.t_flag & TFLAG_BRDCAST) {
948 brace_flags |= GEN_BRDCAST(0);
949 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700950 } else if (i == TOKEN_OPMASK) {
951 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
952 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700953 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700954 nasm_error(ERR_NONFATAL, "broadcast or opmask "
955 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700956 recover = true;
957 }
958 }
959
Victor van den Elzen02846d32009-06-23 03:47:07 +0200960 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700961 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200962 recover = true;
963 }
964 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700966 if (i != 0 && i != ',' && i != ':' &&
967 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
968 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
969 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200970 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700972 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700973 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
974 /* parse opmask (and zeroing) after an operand */
975 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 }
977 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200978 if (recover) {
979 do { /* error recovery */
980 i = stdscan(NULL, &tokval);
981 } while (i != 0 && i != ',');
982 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000983
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300984 /*
985 * now convert the exprs returned from evaluate()
986 * into operand descriptions...
987 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700988 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000989
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700991 /* A mib reference was fully parsed already */
992 if (!mib) {
993 if (parse_mref(op, value))
994 goto fail;
995 op->hintbase = hints.base;
996 op->hinttype = hints.type;
997 }
998 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001001 op->type |= IMMEDIATE;
1002 op->opflags |= OPFLAG_UNKNOWN;
1003 op->offset = 0; /* don't care */
1004 op->segment = NO_SEG; /* don't care again */
1005 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001006
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001007 if(optimizing >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001008 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001009 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001010 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001011 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001013 op->type |= IMMEDIATE;
1014 op->offset = reloc_value(value);
1015 op->segment = reloc_seg(value);
1016 op->wrt = reloc_wrt(value);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001017
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001019 uint64_t n = reloc_value(value);
1020 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001021 op->type |= UNITY;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 if (optimizing >= 0 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001023 !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001024 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001025 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001026 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001027 op->type |= SBYTEWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001028 if (n <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001029 op->type |= UDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001030 if (n + 0x80000000 <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001031 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 }
1033 }
Jin Kyu Song72018a22013-08-05 20:46:18 -07001034 } else if(value->type == EXPR_RDSAE) {
1035 /*
1036 * it's not an operand but a rounding or SAE decorator.
1037 * put the decorator information in the (opflag_t) type field
1038 * of previous operand.
1039 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001040 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001041 switch (value->value) {
1042 case BRC_RN:
1043 case BRC_RU:
1044 case BRC_RD:
1045 case BRC_RZ:
1046 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001047 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001048 result->evex_rm = value->value;
1049 break;
1050 default:
1051 nasm_error(ERR_NONFATAL, "invalid decorator");
1052 break;
1053 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001055 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001058 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001059 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 /*
1063 * check that its only 1 register, not an expression...
1064 */
1065 for (i = 1; value[i].type; i++)
1066 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001067 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001068 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001072 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 /*
1074 * we want to produce a warning iff the specified size
1075 * is different from the register size
1076 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001077 rs = op->type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001079 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001081 op->type &= TO;
1082 op->type |= REGISTER;
1083 op->type |= nasm_reg_flags[value->type];
1084 op->decoflags |= brace_flags;
1085 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001087 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001088 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 "register size specification ignored");
1090 }
1091 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001092
1093 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001094 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1095 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001096 }
1097
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001098 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001099
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001100 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001101 while (opnum < MAX_OPERANDS)
1102 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001103
1104 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001105 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001106 */
1107 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 case I_RESW:
1109 result->opcode = I_RESB;
1110 result->oprs[0].offset *= 2;
1111 break;
1112 case I_RESD:
1113 result->opcode = I_RESB;
1114 result->oprs[0].offset *= 4;
1115 break;
1116 case I_RESQ:
1117 result->opcode = I_RESB;
1118 result->oprs[0].offset *= 8;
1119 break;
1120 case I_REST:
1121 result->opcode = I_RESB;
1122 result->oprs[0].offset *= 10;
1123 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001124 case I_RESO:
1125 result->opcode = I_RESB;
1126 result->oprs[0].offset *= 16;
1127 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001128 case I_RESY:
1129 result->opcode = I_RESB;
1130 result->oprs[0].offset *= 32;
1131 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001132 case I_RESZ:
1133 result->opcode = I_RESB;
1134 result->oprs[0].offset *= 64;
1135 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001136 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001137 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001138 }
1139
1140 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001141
1142fail:
1143 result->opcode = I_none;
1144 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001145}
1146
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001148{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001149 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001150 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001151 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001152
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001153 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001155 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001156
H. Peter Anvin76690a12002-04-30 20:52:49 +00001157 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001158}
1159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001161{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001162 extop *e;
1163
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001164 while ((e = i->eops)) {
1165 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001166 if (e->type == EOT_DB_STRING_FREE)
1167 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001169 }
1170}