blob: d2ad1d271894f2e87e61674a5af0ba348d9583b2 [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:
Jin Kyu Songb287ff02013-12-04 20:05:55 -080093 case P_NOBND:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080094 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070095 case P_O16:
96 case P_O32:
97 case P_O64:
98 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030099 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700100 case P_A16:
101 case P_A32:
102 case P_A64:
103 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300104 return PPS_ASIZE;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700105 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800106 case P_VEX3:
107 case P_VEX2:
108 return PPS_VEX;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700109 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300110 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
111 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700112 }
113}
114
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700115static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700116{
117 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300118 switch ((int)tokval.t_integer) {
119 /* For TASM compatibility a size override inside the
120 * brackets changes the size of the operand, not the
121 * address type of the operand as it does in standard
122 * NASM syntax. Hence:
123 *
124 * mov eax,[DWORD val]
125 *
126 * is valid syntax in TASM compatibility mode. Note that
127 * you lose the ability to override the default address
128 * type for the instruction, but we never use anything
129 * but 32-bit flat model addressing in our code.
130 */
131 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700132 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300133 break;
134 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700135 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300136 break;
137 case S_DWORD:
138 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700139 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300140 break;
141 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700142 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300143 break;
144 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700145 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300146 break;
147 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700148 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300149 break;
150 default:
151 nasm_error(ERR_NONFATAL,
152 "invalid operand size specification");
153 break;
154 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700155 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300156 /* Standard NASM compatible syntax */
157 switch ((int)tokval.t_integer) {
158 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700159 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300160 break;
161 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700162 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300163 break;
164 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700165 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300166 break;
167 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700168 op->disp_size = 8;
169 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300170 break;
171 case P_A16:
172 case P_A32:
173 case P_A64:
174 if (result->prefixes[PPS_ASIZE] &&
175 result->prefixes[PPS_ASIZE] != tokval.t_integer)
176 nasm_error(ERR_NONFATAL,
177 "conflicting address size specifications");
178 else
179 result->prefixes[PPS_ASIZE] = tokval.t_integer;
180 break;
181 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700182 op->disp_size = 16;
183 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300184 break;
185 case S_DWORD:
186 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700187 op->disp_size = 32;
188 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300189 break;
190 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700191 op->disp_size = 64;
192 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300193 break;
194 default:
195 nasm_error(ERR_NONFATAL, "invalid size specification in"
196 " effective address");
197 break;
198 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700199 }
200}
201
Jin Kyu Song72018a22013-08-05 20:46:18 -0700202/*
203 * when two or more decorators follow a register operand,
204 * consecutive decorators are parsed here.
Jin Kyu Songf9a71e02013-08-21 19:29:09 -0700205 * opmask and zeroing decorators can be placed in any order.
Jin Kyu Song487f3522013-11-27 14:10:40 -0800206 * e.g. zmm1 {k2}{z} or zmm2 {z}{k3}
Jin Kyu Song72018a22013-08-05 20:46:18 -0700207 * decorator(s) are placed at the end of an operand.
208 */
209static bool parse_braces(decoflags_t *decoflags)
210{
211 int i;
212 bool recover = false;
213
214 i = tokval.t_type;
215 do {
216 if (i == TOKEN_OPMASK) {
217 if (*decoflags & OPMASK_MASK) {
Jin Kyu Songc9486b92013-10-28 17:07:57 -0700218 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
Jin Kyu Song72018a22013-08-05 20:46:18 -0700219 *decoflags & OPMASK_MASK);
220 *decoflags &= ~OPMASK_MASK;
221 }
222 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
223 } else if (i == TOKEN_DECORATOR) {
224 switch (tokval.t_integer) {
225 case BRC_Z:
226 /*
227 * according to AVX512 spec, only zeroing/merging decorator
228 * is supported with opmask
229 */
230 *decoflags |= GEN_Z(0);
231 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700232 default:
233 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
234 tokval.t_charptr);
235 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700236 }
237 } else if (i == ',' || i == TOKEN_EOS){
238 break;
239 } else {
240 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
241 " expected");
242 recover = true;
243 break;
244 }
245 i = stdscan(NULL, &tokval);
246 } while(1);
247
248 return recover;
249}
250
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700251static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700252{
253 int b, i, s; /* basereg, indexreg, scale */
254 int64_t o; /* offset */
255
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700256 b = i = -1;
257 o = s = 0;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700258
259 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
260 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
261
262 if (is_gpr && e->value == 1)
263 b = e->type; /* It can be basereg */
264 else /* No, it has to be indexreg */
265 i = e->type, s = e->value;
266 e++;
267 }
268 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
269 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
270
271 if (b != -1) /* If the first was the base, ... */
272 i = e->type, s = e->value; /* second has to be indexreg */
273
274 else if (!is_gpr || e->value != 1) {
275 /* If both want to be index */
276 nasm_error(ERR_NONFATAL,
277 "invalid effective address: two index registers");
278 return -1;
279 } else
280 b = e->type;
281 e++;
282 }
283 if (e->type != 0) { /* is there an offset? */
284 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
285 nasm_error(ERR_NONFATAL,
286 "beroset-p-603-invalid effective address");
287 return -1;
288 } else {
289 if (e->type == EXPR_UNKNOWN) {
290 op->opflags |= OPFLAG_UNKNOWN;
291 o = 0; /* doesn't matter what */
292 op->wrt = NO_SEG; /* nor this */
293 op->segment = NO_SEG; /* or this */
294 while (e->type)
295 e++; /* go to the end of the line */
296 } else {
297 if (e->type == EXPR_SIMPLE) {
298 o = e->value;
299 e++;
300 }
301 if (e->type == EXPR_WRT) {
302 op->wrt = e->value;
303 e++;
304 } else
305 op->wrt = NO_SEG;
306 /*
307 * Look for a segment base type.
308 */
309 if (e->type && e->type < EXPR_SEGBASE) {
310 nasm_error(ERR_NONFATAL,
311 "beroset-p-630-invalid effective address");
312 return -1;
313 }
314 while (e->type && e->value == 0)
315 e++;
316 if (e->type && e->value != 1) {
317 nasm_error(ERR_NONFATAL,
318 "beroset-p-637-invalid effective address");
319 return -1;
320 }
321 if (e->type) {
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700322 op->segment = e->type - EXPR_SEGBASE;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700323 e++;
324 } else
325 op->segment = NO_SEG;
326 while (e->type && e->value == 0)
327 e++;
328 if (e->type) {
329 nasm_error(ERR_NONFATAL,
330 "beroset-p-650-invalid effective address");
331 return -1;
332 }
333 }
334 }
335 } else {
336 o = 0;
337 op->wrt = NO_SEG;
338 op->segment = NO_SEG;
339 }
340
341 if (e->type != 0) { /* there'd better be nothing left! */
342 nasm_error(ERR_NONFATAL,
343 "beroset-p-663-invalid effective address");
344 return -1;
345 }
346
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700347 op->basereg = b;
348 op->indexreg = i;
349 op->scale = s;
350 op->offset = o;
351 return 0;
352}
353
354static void mref_set_optype(operand *op)
355{
356 int b = op->basereg;
357 int i = op->indexreg;
358 int s = op->scale;
359
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700360 /* It is memory, but it can match any r/m operand */
361 op->type |= MEMORY_ANY;
362
363 if (b == -1 && (i == -1 || s == 0)) {
364 int is_rel = globalbits == 64 &&
365 !(op->eaflags & EAF_ABS) &&
366 ((globalrel &&
367 !(op->eaflags & EAF_FSGS)) ||
368 (op->eaflags & EAF_REL));
369
370 op->type |= is_rel ? IP_REL : MEM_OFFS;
371 }
372
373 if (i != -1) {
374 opflags_t iclass = nasm_reg_flags[i];
375
376 if (is_class(XMMREG,iclass))
377 op->type |= XMEM;
378 else if (is_class(YMMREG,iclass))
379 op->type |= YMEM;
380 else if (is_class(ZMMREG,iclass))
381 op->type |= ZMEM;
382 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700383}
384
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700385insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000386{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400387 bool insn_is_label = false;
388 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700389 int opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000390 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800391 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700392 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000393
H. Peter Anvin9c987692007-11-04 21:09:32 -0800394restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400395 first = true;
396 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000397
H. Peter Anvin76690a12002-04-30 20:52:49 +0000398 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300399 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000400 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000401
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400402 result->label = NULL; /* Assume no label */
403 result->eops = NULL; /* must do this, whatever happens */
404 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700405 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
406 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000407
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400408 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700409 if (i == TOKEN_EOS)
410 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400411
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400412 if (i != TOKEN_ID &&
413 i != TOKEN_INSN &&
414 i != TOKEN_PREFIX &&
415 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
416 nasm_error(ERR_NONFATAL,
417 "label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700418 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000419 }
420
H. Peter Anvin9c987692007-11-04 21:09:32 -0800421 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
422 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300423 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 result->label = tokval.t_charptr;
425 i = stdscan(NULL, &tokval);
426 if (i == ':') { /* skip over the optional colon */
427 i = stdscan(NULL, &tokval);
428 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700429 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 "label alone on a line without a colon might be in error");
431 }
432 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
433 /*
434 * FIXME: location->segment could be NO_SEG, in which case
435 * it is possible we should be passing 'abs_seg'. Look into this.
436 * Work out whether that is *really* what we should be doing.
437 * Generally fix things. I think this is right as it is, but
438 * am still not certain.
439 */
440 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700441 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000443 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000444
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400445 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700446 if (i == TOKEN_EOS)
447 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000448
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400449 nasm_build_assert(P_none != 0);
450 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000451 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000452
453 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400454 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300455 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800456
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 /*
458 * Handle special case: the TIMES prefix.
459 */
460 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
461 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000462
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 i = stdscan(NULL, &tokval);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800464 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700466 if (!value) /* Error in evaluator */
467 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700469 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 "non-constant argument supplied to TIMES");
471 result->times = 1L;
472 } else {
473 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700474 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200475 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 value->value);
477 result->times = 0;
478 }
479 }
480 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300481 int slot = prefix_slot(tokval.t_integer);
482 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700483 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100484 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300485 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700486 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300487 nasm_error(ERR_NONFATAL,
488 "instruction has conflicting prefixes");
489 }
490 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 i = stdscan(NULL, &tokval);
492 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493 }
494
495 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300496 int j;
497 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700498
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400499 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300500 if ((pfx = result->prefixes[j]) != P_none)
501 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400502 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700503
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700504 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 /*
506 * Instruction prefixes are present, but no actual
507 * instruction. This is allowed: at this point we
508 * invent a notional instruction of RESB 0.
509 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400510 result->opcode = I_RESB;
511 result->operands = 1;
512 result->oprs[0].type = IMMEDIATE;
513 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
515 return result;
516 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700517 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700518 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000520 }
521
522 result->opcode = tokval.t_integer;
523 result->condition = tokval.t_inttwo;
524
525 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700526 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000527 * evaluated operands, since the correct values _must_ be known
528 * on the first pass. Hence, even in pass one, we set the
529 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700530 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000531 */
Charles Crayne2581c862008-09-10 19:21:52 -0700532 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700533 critical = (pass0 < 2 ? 1 : 2);
534
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 } else
536 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000537
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700538 if (result->opcode == I_DB || result->opcode == I_DW ||
539 result->opcode == I_DD || result->opcode == I_DQ ||
540 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700541 result->opcode == I_DY || result->opcode == I_DZ ||
542 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 extop *eop, **tail = &result->eops, **fixptr;
544 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300545 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000546
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700547 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000548
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700550 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 */
552 while (1) {
553 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400554 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300556 else if (first && i == ':') {
557 insn_is_label = true;
558 goto restart_parse;
559 }
560 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 fixptr = tail;
562 eop = *tail = nasm_malloc(sizeof(extop));
563 tail = &eop->next;
564 eop->next = NULL;
565 eop->type = EOT_NOTHING;
566 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300567 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000568
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300569 /*
570 * is_comma_next() here is to distinguish this from
571 * a string used as part of an expression...
572 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700573 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400574 eop->type = EOT_DB_STRING;
575 eop->stringval = tokval.t_charptr;
576 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300578 } else if (i == TOKEN_STRFUNC) {
579 bool parens = false;
580 const char *funcname = tokval.t_charptr;
581 enum strfunc func = tokval.t_integer;
582 i = stdscan(NULL, &tokval);
583 if (i == '(') {
584 parens = true;
585 i = stdscan(NULL, &tokval);
586 }
587 if (i != TOKEN_STR) {
588 nasm_error(ERR_NONFATAL,
589 "%s must be followed by a string constant",
590 funcname);
591 eop->type = EOT_NOTHING;
592 } else {
593 eop->type = EOT_DB_STRING_FREE;
594 eop->stringlen =
595 string_transform(tokval.t_charptr, tokval.t_inttwo,
596 &eop->stringval, func);
597 if (eop->stringlen == (size_t)-1) {
598 nasm_error(ERR_NONFATAL, "invalid string for transform");
599 eop->type = EOT_NOTHING;
600 }
601 }
602 if (parens && i && i != ')') {
603 i = stdscan(NULL, &tokval);
604 if (i != ')') {
605 nasm_error(ERR_NONFATAL, "unterminated %s function",
606 funcname);
607 }
608 }
609 if (i && i != ',')
610 i = stdscan(NULL, &tokval);
611 } else if (i == '-' || i == '+') {
612 char *save = stdscan_get();
613 int token = i;
614 sign = (i == '-') ? -1 : 1;
615 i = stdscan(NULL, &tokval);
616 if (i != TOKEN_FLOAT) {
617 stdscan_set(save);
618 i = tokval.t_type = token;
619 goto is_expression;
620 } else {
621 goto is_float;
622 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700623 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300624is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300625 eop->type = EOT_DB_STRING;
626 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300627
628 eop->stringlen = idata_bytes(result->opcode);
629 if (eop->stringlen > 16) {
630 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700631 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300632 eop->stringlen = 0;
633 } else if (eop->stringlen < 1) {
634 nasm_error(ERR_NONFATAL, "floating-point constant"
635 " encountered in unknown instruction");
636 /*
637 * fix suggested by Pedro Gimeno... original line was:
638 * eop->type = EOT_NOTHING;
639 */
640 eop->stringlen = 0;
641 }
642
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300643 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
644 tail = &eop->next;
645 *fixptr = eop;
646 eop->stringval = (char *)eop + sizeof(extop);
647 if (!eop->stringlen ||
648 !float_const(tokval.t_charptr, sign,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800649 (uint8_t *)eop->stringval, eop->stringlen))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300650 eop->type = EOT_NOTHING;
651 i = stdscan(NULL, &tokval); /* eat the comma */
652 } else {
653 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700655
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300656is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800658 critical, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700660 if (!value) /* Error in evaluator */
661 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000662 if (is_unknown(value)) {
663 eop->type = EOT_DB_NUMBER;
664 eop->offset = 0; /* doesn't matter what we put */
665 eop->segment = eop->wrt = NO_SEG; /* likewise */
666 } else if (is_reloc(value)) {
667 eop->type = EOT_DB_NUMBER;
668 eop->offset = reloc_value(value);
669 eop->segment = reloc_seg(value);
670 eop->wrt = reloc_wrt(value);
671 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700672 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 "operand %d: expression is not simple"
674 " or relocatable", oper_num);
675 }
676 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000677
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 /*
679 * We're about to call stdscan(), which will eat the
680 * comma that we're currently sitting on between
681 * arguments. However, we'd better check first that it
682 * _is_ a comma.
683 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400684 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 break;
686 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700687 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400688 oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700689 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 }
691 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000692
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 if (result->opcode == I_INCBIN) {
694 /*
695 * Correct syntax for INCBIN is that there should be
696 * one string operand, followed by one or two numeric
697 * operands.
698 */
699 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700700 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701 else if (result->eops->next &&
702 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200703 nasm_error(ERR_NONFATAL, "`incbin': second 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->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200707 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400708 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 else if (result->eops->next && result->eops->next->next &&
710 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700711 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400712 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000713 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 return result;
715 /*
716 * If we reach here, one of the above errors happened.
717 * Throw the instruction away.
718 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700719 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700721 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 "no operand for data declaration");
723 else
724 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000725
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727 }
728
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400729 /*
730 * Now we begin to parse the operands. There may be up to four
731 * of these, separated by commas, and terminated by a zero token.
732 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700734 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
735 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300736 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700737 bool mref; /* is this going to be a memory ref? */
738 bool bracket; /* is it a [] mref, or a & mref? */
739 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700741 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000742
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700743 op->disp_size = 0; /* have to zero this whatever */
744 op->eaflags = 0; /* and this */
745 op->opflags = 0;
746 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000747
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400749 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300751 else if (first && i == ':') {
752 insn_is_label = true;
753 goto restart_parse;
754 }
755 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700756 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 while (i == TOKEN_SPECIAL) { /* size specifiers */
758 switch ((int)tokval.t_integer) {
759 case S_BYTE:
760 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700761 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 setsize = 1;
763 break;
764 case S_WORD:
765 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700766 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 setsize = 1;
768 break;
769 case S_DWORD:
770 case S_LONG:
771 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700772 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 setsize = 1;
774 break;
775 case S_QWORD:
776 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700777 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 setsize = 1;
779 break;
780 case S_TWORD:
781 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700782 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 setsize = 1;
784 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700785 case S_OWORD:
786 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700787 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700788 setsize = 1;
789 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700790 case S_YWORD:
791 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700792 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700793 setsize = 1;
794 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700795 case S_ZWORD:
796 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700797 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700798 setsize = 1;
799 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700801 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 break;
803 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700804 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 break;
806 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700807 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 break;
809 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700810 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 break;
812 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700813 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 break;
815 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700816 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 }
818 i = stdscan(NULL, &tokval);
819 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000820
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700822 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700824 i = stdscan(NULL, &tokval); /* then skip the colon */
825 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700826 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700827 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700829 /* when a comma follows an opening bracket - [ , eax*4] */
830 if (i == ',') {
831 /* treat as if there is a zero displacement virtually */
832 tokval.t_type = TOKEN_NUM;
833 tokval.t_integer = 0;
834 stdscan_set(stdscan_get() - 1); /* rewind the comma */
835 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700837 mref = false;
838 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000840
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700841 if ((op->type & FAR) && !mref &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700843 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000845
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800847 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700849 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700850 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700852 if (!value) /* Error in evaluator */
853 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 if (i == ':' && mref) { /* it was seg:offset */
855 /*
856 * Process the segment override.
857 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400858 if (value[1].type != 0 ||
859 value->value != 1 ||
860 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700861 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700862 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700863 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700864 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000865 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300866 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400867 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700868 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300869 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000870
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700872 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700873 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 i = stdscan(NULL, &tokval);
875 }
876 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800877 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700879 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700880 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 }
882 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700883 if (!value) /* Error in evaluator */
884 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200886
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700887 mib = false;
888 if (mref && bracket && i == ',') {
889 /* [seg:base+offset,index*scale] syntax (mib) */
890
891 operand o1, o2; /* Partial operands */
892
893 if (parse_mref(&o1, value))
894 goto fail;
895
896 i = stdscan(NULL, &tokval); /* Eat comma */
897 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800898 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700899 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +0300900 if (!value)
901 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700902
903 if (parse_mref(&o2, value))
904 goto fail;
905
906 if (o2.basereg != -1 && o2.indexreg == -1) {
907 o2.indexreg = o2.basereg;
908 o2.scale = 1;
909 o2.basereg = -1;
910 }
911
912 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
913 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
914 nasm_error(ERR_NONFATAL, "invalid mib expression");
915 goto fail;
916 }
917
918 op->basereg = o1.basereg;
919 op->indexreg = o2.indexreg;
920 op->scale = o2.scale;
921 op->offset = o1.offset;
922 op->segment = o1.segment;
923 op->wrt = o1.wrt;
924
925 if (op->basereg != -1) {
926 op->hintbase = op->basereg;
927 op->hinttype = EAH_MAKEBASE;
928 } else if (op->indexreg != -1) {
929 op->hintbase = op->indexreg;
930 op->hinttype = EAH_NOTBASE;
931 } else {
932 op->hintbase = -1;
933 op->hinttype = EAH_NOHINT;
934 }
935
936 mib = true;
937 }
938
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700939 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 if (mref && bracket) { /* find ] at the end */
941 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700942 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200943 recover = true;
944 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700946 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700947 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700948 * according to AVX512 spec, broacast or opmask decorator
949 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700950 */
951 if (tokval.t_flag & TFLAG_BRDCAST) {
Jin Kyu Song25c22122013-10-30 03:12:45 -0700952 brace_flags |= GEN_BRDCAST(0) |
Mark Charneydcaef4b2014-10-09 13:45:17 -0400953 VAL_BRNUM(tokval.t_integer - BRC_1TO2);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700954 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700955 } else if (i == TOKEN_OPMASK) {
956 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
957 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700958 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700959 nasm_error(ERR_NONFATAL, "broadcast or opmask "
960 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700961 recover = true;
962 }
963 }
964
Victor van den Elzen02846d32009-06-23 03:47:07 +0200965 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700966 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200967 recover = true;
968 }
969 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700971 if (i != 0 && i != ',' && i != ':' &&
972 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
973 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
974 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200975 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700977 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700978 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
979 /* parse opmask (and zeroing) after an operand */
980 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 }
982 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200983 if (recover) {
984 do { /* error recovery */
985 i = stdscan(NULL, &tokval);
986 } while (i != 0 && i != ',');
987 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000988
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300989 /*
990 * now convert the exprs returned from evaluate()
991 * into operand descriptions...
992 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700993 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700996 /* A mib reference was fully parsed already */
997 if (!mib) {
998 if (parse_mref(op, value))
999 goto fail;
1000 op->hintbase = hints.base;
1001 op->hinttype = hints.type;
1002 }
1003 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001006 op->type |= IMMEDIATE;
1007 op->opflags |= OPFLAG_UNKNOWN;
1008 op->offset = 0; /* don't care */
1009 op->segment = NO_SEG; /* don't care again */
1010 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001011
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001012 if(optimizing >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001013 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001014 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001015 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001016 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001018 op->type |= IMMEDIATE;
1019 op->offset = reloc_value(value);
1020 op->segment = reloc_seg(value);
1021 op->wrt = reloc_wrt(value);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001024 uint64_t n = reloc_value(value);
1025 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001026 op->type |= UNITY;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 if (optimizing >= 0 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001028 !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001029 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001030 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001031 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 op->type |= SBYTEWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001033 if (n <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001034 op->type |= UDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001035 if (n + 0x80000000 <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001036 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 }
1038 }
Jin Kyu Song72018a22013-08-05 20:46:18 -07001039 } else if(value->type == EXPR_RDSAE) {
1040 /*
1041 * it's not an operand but a rounding or SAE decorator.
1042 * put the decorator information in the (opflag_t) type field
1043 * of previous operand.
1044 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001045 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001046 switch (value->value) {
1047 case BRC_RN:
1048 case BRC_RU:
1049 case BRC_RD:
1050 case BRC_RZ:
1051 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001052 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001053 result->evex_rm = value->value;
1054 break;
1055 default:
1056 nasm_error(ERR_NONFATAL, "invalid decorator");
1057 break;
1058 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001060 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001061
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001063 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001064 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001066
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 /*
1068 * check that its only 1 register, not an expression...
1069 */
1070 for (i = 1; value[i].type; i++)
1071 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001072 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001073 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001075
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001077 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 /*
1079 * we want to produce a warning iff the specified size
1080 * is different from the register size
1081 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001082 rs = op->type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001084 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001086 op->type &= TO;
1087 op->type |= REGISTER;
1088 op->type |= nasm_reg_flags[value->type];
1089 op->decoflags |= brace_flags;
1090 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001092 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001093 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 "register size specification ignored");
1095 }
1096 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001097
1098 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001099 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1100 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001101 }
1102
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001103 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001104
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001105 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001106 while (opnum < MAX_OPERANDS)
1107 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001108
1109 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001110 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001111 */
1112 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 case I_RESW:
1114 result->opcode = I_RESB;
1115 result->oprs[0].offset *= 2;
1116 break;
1117 case I_RESD:
1118 result->opcode = I_RESB;
1119 result->oprs[0].offset *= 4;
1120 break;
1121 case I_RESQ:
1122 result->opcode = I_RESB;
1123 result->oprs[0].offset *= 8;
1124 break;
1125 case I_REST:
1126 result->opcode = I_RESB;
1127 result->oprs[0].offset *= 10;
1128 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001129 case I_RESO:
1130 result->opcode = I_RESB;
1131 result->oprs[0].offset *= 16;
1132 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001133 case I_RESY:
1134 result->opcode = I_RESB;
1135 result->oprs[0].offset *= 32;
1136 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001137 case I_RESZ:
1138 result->opcode = I_RESB;
1139 result->oprs[0].offset *= 64;
1140 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001141 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001142 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001143 }
1144
1145 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001146
1147fail:
1148 result->opcode = I_none;
1149 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001150}
1151
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001153{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001154 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001155 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001156 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001157
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001158 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001160 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001161
H. Peter Anvin76690a12002-04-30 20:52:49 +00001162 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001163}
1164
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001166{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001167 extop *e;
1168
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001169 while ((e = i->eops)) {
1170 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001171 if (e->type == EOT_DB_STRING_FREE)
1172 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001174 }
1175}