blob: 6210cc69b3a34cbfd2a65926bae8dac633422618 [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>
45
46#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000047#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000049#include "stdscan.h"
H. Peter Anvin00444ae2009-07-18 18:49:55 -070050#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
52#include "float.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070053#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000054
H. Peter Anvine2c80182005-01-15 22:15:51 +000055extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030056extern int32_t abs_seg; /* ABSOLUTE segment */
57extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000058
H. Peter Anvine2c80182005-01-15 22:15:51 +000059static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061static int i;
62static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000063
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040064static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070065{
66 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080067 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030068 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070069 case R_CS:
70 case R_DS:
71 case R_SS:
72 case R_ES:
73 case R_FS:
74 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030075 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070076 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080077 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070078 case P_REP:
79 case P_REPE:
80 case P_REPZ:
81 case P_REPNE:
82 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080083 case P_XACQUIRE:
84 case P_XRELEASE:
Jin Kyu Song03041092013-10-15 19:38:51 -070085 case P_BND:
Jin Kyu Songb287ff02013-12-04 20:05:55 -080086 case P_NOBND:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080087 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070088 case P_O16:
89 case P_O32:
90 case P_O64:
91 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030092 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070093 case P_A16:
94 case P_A32:
95 case P_A64:
96 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030097 return PPS_ASIZE;
Jin Kyu Song945b1b82013-10-25 19:29:53 -070098 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -080099 case P_VEX3:
100 case P_VEX2:
101 return PPS_VEX;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700102 default:
H. Peter Anvin41087062016-03-03 14:27:34 -0800103 nasm_panic(0, "Invalid value %d passed to prefix_slot()", prefix);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300104 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700105 }
106}
107
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700108static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700109{
110 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300111 switch ((int)tokval.t_integer) {
112 /* For TASM compatibility a size override inside the
113 * brackets changes the size of the operand, not the
114 * address type of the operand as it does in standard
115 * NASM syntax. Hence:
116 *
117 * mov eax,[DWORD val]
118 *
119 * is valid syntax in TASM compatibility mode. Note that
120 * you lose the ability to override the default address
121 * type for the instruction, but we never use anything
122 * but 32-bit flat model addressing in our code.
123 */
124 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700125 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300126 break;
127 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700128 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300129 break;
130 case S_DWORD:
131 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700132 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300133 break;
134 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700135 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300136 break;
137 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700138 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300139 break;
140 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700141 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300142 break;
143 default:
144 nasm_error(ERR_NONFATAL,
145 "invalid operand size specification");
146 break;
147 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700148 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300149 /* Standard NASM compatible syntax */
150 switch ((int)tokval.t_integer) {
151 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700152 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300153 break;
154 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700155 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300156 break;
157 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700158 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300159 break;
160 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700161 op->disp_size = 8;
162 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300163 break;
164 case P_A16:
165 case P_A32:
166 case P_A64:
167 if (result->prefixes[PPS_ASIZE] &&
168 result->prefixes[PPS_ASIZE] != tokval.t_integer)
169 nasm_error(ERR_NONFATAL,
170 "conflicting address size specifications");
171 else
172 result->prefixes[PPS_ASIZE] = tokval.t_integer;
173 break;
174 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700175 op->disp_size = 16;
176 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300177 break;
178 case S_DWORD:
179 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700180 op->disp_size = 32;
181 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300182 break;
183 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700184 op->disp_size = 64;
185 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300186 break;
187 default:
188 nasm_error(ERR_NONFATAL, "invalid size specification in"
189 " effective address");
190 break;
191 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700192 }
193}
194
Jin Kyu Song72018a22013-08-05 20:46:18 -0700195/*
196 * when two or more decorators follow a register operand,
197 * consecutive decorators are parsed here.
Jin Kyu Songf9a71e02013-08-21 19:29:09 -0700198 * opmask and zeroing decorators can be placed in any order.
Jin Kyu Song487f3522013-11-27 14:10:40 -0800199 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
Jin Kyu Song72018a22013-08-05 20:46:18 -0700200 * decorator(s) are placed at the end of an operand.
201 */
202static bool parse_braces(decoflags_t *decoflags)
203{
204 int i;
205 bool recover = false;
206
207 i = tokval.t_type;
208 do {
209 if (i == TOKEN_OPMASK) {
210 if (*decoflags & OPMASK_MASK) {
Jin Kyu Songc9486b92013-10-28 17:07:57 -0700211 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
Jin Kyu Song72018a22013-08-05 20:46:18 -0700212 *decoflags & OPMASK_MASK);
213 *decoflags &= ~OPMASK_MASK;
214 }
215 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
216 } else if (i == TOKEN_DECORATOR) {
217 switch (tokval.t_integer) {
218 case BRC_Z:
219 /*
220 * according to AVX512 spec, only zeroing/merging decorator
221 * is supported with opmask
222 */
223 *decoflags |= GEN_Z(0);
224 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700225 default:
226 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
227 tokval.t_charptr);
228 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700229 }
230 } else if (i == ',' || i == TOKEN_EOS){
231 break;
232 } else {
233 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
234 " expected");
235 recover = true;
236 break;
237 }
238 i = stdscan(NULL, &tokval);
239 } while(1);
240
241 return recover;
242}
243
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700244static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700245{
246 int b, i, s; /* basereg, indexreg, scale */
247 int64_t o; /* offset */
248
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700249 b = i = -1;
250 o = s = 0;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700251
252 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
253 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
254
255 if (is_gpr && e->value == 1)
256 b = e->type; /* It can be basereg */
257 else /* No, it has to be indexreg */
258 i = e->type, s = e->value;
259 e++;
260 }
261 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
262 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
263
264 if (b != -1) /* If the first was the base, ... */
265 i = e->type, s = e->value; /* second has to be indexreg */
266
267 else if (!is_gpr || e->value != 1) {
268 /* If both want to be index */
269 nasm_error(ERR_NONFATAL,
270 "invalid effective address: two index registers");
271 return -1;
272 } else
273 b = e->type;
274 e++;
275 }
276 if (e->type != 0) { /* is there an offset? */
277 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
278 nasm_error(ERR_NONFATAL,
279 "beroset-p-603-invalid effective address");
280 return -1;
281 } else {
282 if (e->type == EXPR_UNKNOWN) {
283 op->opflags |= OPFLAG_UNKNOWN;
284 o = 0; /* doesn't matter what */
285 op->wrt = NO_SEG; /* nor this */
286 op->segment = NO_SEG; /* or this */
287 while (e->type)
288 e++; /* go to the end of the line */
289 } else {
290 if (e->type == EXPR_SIMPLE) {
291 o = e->value;
292 e++;
293 }
294 if (e->type == EXPR_WRT) {
295 op->wrt = e->value;
296 e++;
297 } else
298 op->wrt = NO_SEG;
299 /*
300 * Look for a segment base type.
301 */
302 if (e->type && e->type < EXPR_SEGBASE) {
303 nasm_error(ERR_NONFATAL,
304 "beroset-p-630-invalid effective address");
305 return -1;
306 }
307 while (e->type && e->value == 0)
308 e++;
309 if (e->type && e->value != 1) {
310 nasm_error(ERR_NONFATAL,
311 "beroset-p-637-invalid effective address");
312 return -1;
313 }
314 if (e->type) {
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700315 op->segment = e->type - EXPR_SEGBASE;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700316 e++;
317 } else
318 op->segment = NO_SEG;
319 while (e->type && e->value == 0)
320 e++;
321 if (e->type) {
322 nasm_error(ERR_NONFATAL,
323 "beroset-p-650-invalid effective address");
324 return -1;
325 }
326 }
327 }
328 } else {
329 o = 0;
330 op->wrt = NO_SEG;
331 op->segment = NO_SEG;
332 }
333
334 if (e->type != 0) { /* there'd better be nothing left! */
335 nasm_error(ERR_NONFATAL,
336 "beroset-p-663-invalid effective address");
337 return -1;
338 }
339
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700340 op->basereg = b;
341 op->indexreg = i;
342 op->scale = s;
343 op->offset = o;
344 return 0;
345}
346
347static void mref_set_optype(operand *op)
348{
349 int b = op->basereg;
350 int i = op->indexreg;
351 int s = op->scale;
352
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700353 /* It is memory, but it can match any r/m operand */
354 op->type |= MEMORY_ANY;
355
356 if (b == -1 && (i == -1 || s == 0)) {
357 int is_rel = globalbits == 64 &&
358 !(op->eaflags & EAF_ABS) &&
359 ((globalrel &&
360 !(op->eaflags & EAF_FSGS)) ||
361 (op->eaflags & EAF_REL));
362
363 op->type |= is_rel ? IP_REL : MEM_OFFS;
364 }
365
366 if (i != -1) {
367 opflags_t iclass = nasm_reg_flags[i];
368
369 if (is_class(XMMREG,iclass))
370 op->type |= XMEM;
371 else if (is_class(YMMREG,iclass))
372 op->type |= YMEM;
373 else if (is_class(ZMMREG,iclass))
374 op->type |= ZMEM;
375 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700376}
377
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700378insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000379{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400380 bool insn_is_label = false;
381 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700382 int opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000383 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800384 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700385 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000386
H. Peter Anvin9c987692007-11-04 21:09:32 -0800387restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400388 first = true;
389 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000390
H. Peter Anvin76690a12002-04-30 20:52:49 +0000391 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300392 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000393 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000394
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400395 result->label = NULL; /* Assume no label */
396 result->eops = NULL; /* must do this, whatever happens */
397 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700398 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
399 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000400
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400401 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700402 if (i == TOKEN_EOS)
403 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400404
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400405 if (i != TOKEN_ID &&
406 i != TOKEN_INSN &&
407 i != TOKEN_PREFIX &&
408 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
409 nasm_error(ERR_NONFATAL,
410 "label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700411 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000412 }
413
H. Peter Anvin9c987692007-11-04 21:09:32 -0800414 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
415 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300416 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 result->label = tokval.t_charptr;
418 i = stdscan(NULL, &tokval);
419 if (i == ':') { /* skip over the optional colon */
420 i = stdscan(NULL, &tokval);
421 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700422 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 "label alone on a line without a colon might be in error");
424 }
425 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
426 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800427 * FIXME: location.segment could be NO_SEG, in which case
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 * it is possible we should be passing 'abs_seg'. Look into this.
429 * Work out whether that is *really* what we should be doing.
430 * Generally fix things. I think this is right as it is, but
431 * am still not certain.
432 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800433 ldef(result->label, in_abs_seg ? abs_seg : location.segment,
434 location.offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000435 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000436 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000437
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400438 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700439 if (i == TOKEN_EOS)
440 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000441
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400442 nasm_build_assert(P_none != 0);
443 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000444 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000445
446 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400447 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300448 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800449
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 /*
451 * Handle special case: the TIMES prefix.
452 */
453 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
454 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000455
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 i = stdscan(NULL, &tokval);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800457 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700459 if (!value) /* Error in evaluator */
460 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700462 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 "non-constant argument supplied to TIMES");
464 result->times = 1L;
465 } else {
466 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700467 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200468 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 value->value);
470 result->times = 0;
471 }
472 }
473 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300474 int slot = prefix_slot(tokval.t_integer);
475 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700476 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100477 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300478 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700479 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300480 nasm_error(ERR_NONFATAL,
481 "instruction has conflicting prefixes");
482 }
483 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 i = stdscan(NULL, &tokval);
485 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000486 }
487
488 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300489 int j;
490 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700491
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400492 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300493 if ((pfx = result->prefixes[j]) != P_none)
494 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400495 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700496
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700497 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 /*
499 * Instruction prefixes are present, but no actual
500 * instruction. This is allowed: at this point we
501 * invent a notional instruction of RESB 0.
502 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400503 result->opcode = I_RESB;
504 result->operands = 1;
505 result->oprs[0].type = IMMEDIATE;
506 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
508 return result;
509 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700510 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700511 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000513 }
514
515 result->opcode = tokval.t_integer;
516 result->condition = tokval.t_inttwo;
517
518 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700519 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000520 * evaluated operands, since the correct values _must_ be known
521 * on the first pass. Hence, even in pass one, we set the
522 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700523 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000524 */
Charles Crayne2581c862008-09-10 19:21:52 -0700525 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700526 critical = (pass0 < 2 ? 1 : 2);
527
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 } else
529 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000530
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700531 if (result->opcode == I_DB || result->opcode == I_DW ||
532 result->opcode == I_DD || result->opcode == I_DQ ||
533 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700534 result->opcode == I_DY || result->opcode == I_DZ ||
535 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536 extop *eop, **tail = &result->eops, **fixptr;
537 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300538 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000539
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700540 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000541
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700543 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 */
545 while (1) {
546 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400547 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300549 else if (first && i == ':') {
550 insn_is_label = true;
551 goto restart_parse;
552 }
553 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 fixptr = tail;
555 eop = *tail = nasm_malloc(sizeof(extop));
556 tail = &eop->next;
557 eop->next = NULL;
558 eop->type = EOT_NOTHING;
559 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300560 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000561
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300562 /*
563 * is_comma_next() here is to distinguish this from
564 * a string used as part of an expression...
565 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700566 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400567 eop->type = EOT_DB_STRING;
568 eop->stringval = tokval.t_charptr;
569 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300571 } else if (i == TOKEN_STRFUNC) {
572 bool parens = false;
573 const char *funcname = tokval.t_charptr;
574 enum strfunc func = tokval.t_integer;
575 i = stdscan(NULL, &tokval);
576 if (i == '(') {
577 parens = true;
578 i = stdscan(NULL, &tokval);
579 }
580 if (i != TOKEN_STR) {
581 nasm_error(ERR_NONFATAL,
582 "%s must be followed by a string constant",
583 funcname);
584 eop->type = EOT_NOTHING;
585 } else {
586 eop->type = EOT_DB_STRING_FREE;
587 eop->stringlen =
588 string_transform(tokval.t_charptr, tokval.t_inttwo,
589 &eop->stringval, func);
590 if (eop->stringlen == (size_t)-1) {
591 nasm_error(ERR_NONFATAL, "invalid string for transform");
592 eop->type = EOT_NOTHING;
593 }
594 }
595 if (parens && i && i != ')') {
596 i = stdscan(NULL, &tokval);
597 if (i != ')') {
598 nasm_error(ERR_NONFATAL, "unterminated %s function",
599 funcname);
600 }
601 }
602 if (i && i != ',')
603 i = stdscan(NULL, &tokval);
604 } else if (i == '-' || i == '+') {
605 char *save = stdscan_get();
606 int token = i;
607 sign = (i == '-') ? -1 : 1;
608 i = stdscan(NULL, &tokval);
609 if (i != TOKEN_FLOAT) {
610 stdscan_set(save);
611 i = tokval.t_type = token;
612 goto is_expression;
613 } else {
614 goto is_float;
615 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700616 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300617is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300618 eop->type = EOT_DB_STRING;
619 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300620
621 eop->stringlen = idata_bytes(result->opcode);
622 if (eop->stringlen > 16) {
623 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700624 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300625 eop->stringlen = 0;
626 } else if (eop->stringlen < 1) {
627 nasm_error(ERR_NONFATAL, "floating-point constant"
628 " encountered in unknown instruction");
629 /*
630 * fix suggested by Pedro Gimeno... original line was:
631 * eop->type = EOT_NOTHING;
632 */
633 eop->stringlen = 0;
634 }
635
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300636 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
637 tail = &eop->next;
638 *fixptr = eop;
639 eop->stringval = (char *)eop + sizeof(extop);
640 if (!eop->stringlen ||
641 !float_const(tokval.t_charptr, sign,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800642 (uint8_t *)eop->stringval, eop->stringlen))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300643 eop->type = EOT_NOTHING;
644 i = stdscan(NULL, &tokval); /* eat the comma */
645 } else {
646 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700648
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300649is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800651 critical, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700653 if (!value) /* Error in evaluator */
654 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 if (is_unknown(value)) {
656 eop->type = EOT_DB_NUMBER;
657 eop->offset = 0; /* doesn't matter what we put */
658 eop->segment = eop->wrt = NO_SEG; /* likewise */
659 } else if (is_reloc(value)) {
660 eop->type = EOT_DB_NUMBER;
661 eop->offset = reloc_value(value);
662 eop->segment = reloc_seg(value);
663 eop->wrt = reloc_wrt(value);
664 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700665 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 "operand %d: expression is not simple"
667 " or relocatable", oper_num);
668 }
669 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000670
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 /*
672 * We're about to call stdscan(), which will eat the
673 * comma that we're currently sitting on between
674 * arguments. However, we'd better check first that it
675 * _is_ a comma.
676 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400677 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 break;
679 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700680 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400681 oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700682 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 }
684 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000685
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 if (result->opcode == I_INCBIN) {
687 /*
688 * Correct syntax for INCBIN is that there should be
689 * one string operand, followed by one or two numeric
690 * operands.
691 */
692 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700693 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694 else if (result->eops->next &&
695 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200696 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400697 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 else if (result->eops->next && result->eops->next->next &&
699 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200700 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400701 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 else if (result->eops->next && result->eops->next->next &&
703 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700704 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400705 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000706 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 return result;
708 /*
709 * If we reach here, one of the above errors happened.
710 * Throw the instruction away.
711 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700712 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700714 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 "no operand for data declaration");
716 else
717 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000718
H. Peter Anvine2c80182005-01-15 22:15:51 +0000719 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000720 }
721
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400722 /*
723 * Now we begin to parse the operands. There may be up to four
724 * of these, separated by commas, and terminated by a zero token.
725 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000726
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700727 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
728 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300729 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700730 bool mref; /* is this going to be a memory ref? */
731 bool bracket; /* is it a [] mref, or a & mref? */
732 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700734 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000735
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700736 op->disp_size = 0; /* have to zero this whatever */
737 op->eaflags = 0; /* and this */
738 op->opflags = 0;
739 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000740
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400742 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300744 else if (first && i == ':') {
745 insn_is_label = true;
746 goto restart_parse;
747 }
748 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700749 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 while (i == TOKEN_SPECIAL) { /* size specifiers */
751 switch ((int)tokval.t_integer) {
752 case S_BYTE:
753 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700754 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 setsize = 1;
756 break;
757 case S_WORD:
758 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700759 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 setsize = 1;
761 break;
762 case S_DWORD:
763 case S_LONG:
764 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700765 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 setsize = 1;
767 break;
768 case S_QWORD:
769 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700770 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 setsize = 1;
772 break;
773 case S_TWORD:
774 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700775 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 setsize = 1;
777 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700778 case S_OWORD:
779 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700780 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700781 setsize = 1;
782 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700783 case S_YWORD:
784 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700785 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700786 setsize = 1;
787 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700788 case S_ZWORD:
789 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700790 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700791 setsize = 1;
792 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700794 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 break;
796 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700797 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 break;
799 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700800 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 break;
802 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700803 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 break;
805 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700806 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 break;
808 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700809 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 }
811 i = stdscan(NULL, &tokval);
812 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000813
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700815 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700817 i = stdscan(NULL, &tokval); /* then skip the colon */
818 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700819 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700820 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700822 /* when a comma follows an opening bracket - [ , eax*4] */
823 if (i == ',') {
824 /* treat as if there is a zero displacement virtually */
825 tokval.t_type = TOKEN_NUM;
826 tokval.t_integer = 0;
827 stdscan_set(stdscan_get() - 1); /* rewind the comma */
828 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700830 mref = false;
831 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700834 if ((op->type & FAR) && !mref &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700836 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800840 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700842 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700843 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700845 if (!value) /* Error in evaluator */
846 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 if (i == ':' && mref) { /* it was seg:offset */
848 /*
849 * Process the segment override.
850 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400851 if (value[1].type != 0 ||
852 value->value != 1 ||
853 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700854 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700855 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700856 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700857 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000858 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300859 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400860 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700861 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300862 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000863
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700865 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700866 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 i = stdscan(NULL, &tokval);
868 }
869 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800870 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700872 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700873 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 }
875 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700876 if (!value) /* Error in evaluator */
877 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200879
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700880 mib = false;
881 if (mref && bracket && i == ',') {
882 /* [seg:base+offset,index*scale] syntax (mib) */
883
884 operand o1, o2; /* Partial operands */
885
886 if (parse_mref(&o1, value))
887 goto fail;
888
889 i = stdscan(NULL, &tokval); /* Eat comma */
890 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800891 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700892 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +0300893 if (!value)
894 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700895
896 if (parse_mref(&o2, value))
897 goto fail;
898
899 if (o2.basereg != -1 && o2.indexreg == -1) {
900 o2.indexreg = o2.basereg;
901 o2.scale = 1;
902 o2.basereg = -1;
903 }
904
905 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
906 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
907 nasm_error(ERR_NONFATAL, "invalid mib expression");
908 goto fail;
909 }
910
911 op->basereg = o1.basereg;
912 op->indexreg = o2.indexreg;
913 op->scale = o2.scale;
914 op->offset = o1.offset;
915 op->segment = o1.segment;
916 op->wrt = o1.wrt;
917
918 if (op->basereg != -1) {
919 op->hintbase = op->basereg;
920 op->hinttype = EAH_MAKEBASE;
921 } else if (op->indexreg != -1) {
922 op->hintbase = op->indexreg;
923 op->hinttype = EAH_NOTBASE;
924 } else {
925 op->hintbase = -1;
926 op->hinttype = EAH_NOHINT;
927 }
928
929 mib = true;
930 }
931
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700932 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 if (mref && bracket) { /* find ] at the end */
934 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700935 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200936 recover = true;
937 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700939 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700940 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700941 * according to AVX512 spec, broacast or opmask decorator
942 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700943 */
944 if (tokval.t_flag & TFLAG_BRDCAST) {
Jin Kyu Song25c22122013-10-30 03:12:45 -0700945 brace_flags |= GEN_BRDCAST(0) |
Mark Charneydcaef4b2014-10-09 13:45:17 -0400946 VAL_BRNUM(tokval.t_integer - BRC_1TO2);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700947 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700948 } else if (i == TOKEN_OPMASK) {
949 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
950 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700951 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700952 nasm_error(ERR_NONFATAL, "broadcast or opmask "
953 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700954 recover = true;
955 }
956 }
957
Victor van den Elzen02846d32009-06-23 03:47:07 +0200958 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700959 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200960 recover = true;
961 }
962 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700964 if (i != 0 && i != ',' && i != ':' &&
965 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
966 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
967 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200968 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700970 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700971 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
972 /* parse opmask (and zeroing) after an operand */
973 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 }
975 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200976 if (recover) {
977 do { /* error recovery */
978 i = stdscan(NULL, &tokval);
979 } while (i != 0 && i != ',');
980 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000981
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300982 /*
983 * now convert the exprs returned from evaluate()
984 * into operand descriptions...
985 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700986 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700989 /* A mib reference was fully parsed already */
990 if (!mib) {
991 if (parse_mref(op, value))
992 goto fail;
993 op->hintbase = hints.base;
994 op->hinttype = hints.type;
995 }
996 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700999 op->type |= IMMEDIATE;
1000 op->opflags |= OPFLAG_UNKNOWN;
1001 op->offset = 0; /* don't care */
1002 op->segment = NO_SEG; /* don't care again */
1003 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001004
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001005 if(optimizing >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001006 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001007 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001008 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001009 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001011 op->type |= IMMEDIATE;
1012 op->offset = reloc_value(value);
1013 op->segment = reloc_seg(value);
1014 op->wrt = reloc_wrt(value);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001017 uint64_t n = reloc_value(value);
1018 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001019 op->type |= UNITY;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 if (optimizing >= 0 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001021 !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001022 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001023 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001024 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001025 op->type |= SBYTEWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001026 if (n <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001027 op->type |= UDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001028 if (n + 0x80000000 <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001029 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 }
1031 }
Jin Kyu Song72018a22013-08-05 20:46:18 -07001032 } else if(value->type == EXPR_RDSAE) {
1033 /*
1034 * it's not an operand but a rounding or SAE decorator.
1035 * put the decorator information in the (opflag_t) type field
1036 * of previous operand.
1037 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001038 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001039 switch (value->value) {
1040 case BRC_RN:
1041 case BRC_RU:
1042 case BRC_RD:
1043 case BRC_RZ:
1044 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001045 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001046 result->evex_rm = value->value;
1047 break;
1048 default:
1049 nasm_error(ERR_NONFATAL, "invalid decorator");
1050 break;
1051 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001053 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001056 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001057 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 /*
1061 * check that its only 1 register, not an expression...
1062 */
1063 for (i = 1; value[i].type; i++)
1064 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001065 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001066 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001070 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 /*
1072 * we want to produce a warning iff the specified size
1073 * is different from the register size
1074 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001075 rs = op->type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001077 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001079 op->type &= TO;
1080 op->type |= REGISTER;
1081 op->type |= nasm_reg_flags[value->type];
1082 op->decoflags |= brace_flags;
1083 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001085 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001086 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 "register size specification ignored");
1088 }
1089 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001090
1091 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001092 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1093 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001094 }
1095
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001096 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001097
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001098 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001099 while (opnum < MAX_OPERANDS)
1100 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001101
1102 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001103 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001104 */
1105 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 case I_RESW:
1107 result->opcode = I_RESB;
1108 result->oprs[0].offset *= 2;
1109 break;
1110 case I_RESD:
1111 result->opcode = I_RESB;
1112 result->oprs[0].offset *= 4;
1113 break;
1114 case I_RESQ:
1115 result->opcode = I_RESB;
1116 result->oprs[0].offset *= 8;
1117 break;
1118 case I_REST:
1119 result->opcode = I_RESB;
1120 result->oprs[0].offset *= 10;
1121 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001122 case I_RESO:
1123 result->opcode = I_RESB;
1124 result->oprs[0].offset *= 16;
1125 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001126 case I_RESY:
1127 result->opcode = I_RESB;
1128 result->oprs[0].offset *= 32;
1129 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001130 case I_RESZ:
1131 result->opcode = I_RESB;
1132 result->oprs[0].offset *= 64;
1133 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001134 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001135 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001136 }
1137
1138 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001139
1140fail:
1141 result->opcode = I_none;
1142 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001143}
1144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001146{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001147 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001148 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001149 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001150
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001151 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001153 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001154
H. Peter Anvin76690a12002-04-30 20:52:49 +00001155 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001156}
1157
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001159{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001160 extop *e;
1161
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001162 while ((e = i->eops)) {
1163 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001164 if (e->type == EOT_DB_STRING_FREE)
1165 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001167 }
1168}