blob: ed568f2079d0c667d2b9c2c1d6b60a863bac7e5f [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:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080092 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070093 case P_O16:
94 case P_O32:
95 case P_O64:
96 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030097 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070098 case P_A16:
99 case P_A32:
100 case P_A64:
101 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300102 return PPS_ASIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700103 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300104 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
105 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700106 }
107}
108
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700109static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700110{
111 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300112 switch ((int)tokval.t_integer) {
113 /* For TASM compatibility a size override inside the
114 * brackets changes the size of the operand, not the
115 * address type of the operand as it does in standard
116 * NASM syntax. Hence:
117 *
118 * mov eax,[DWORD val]
119 *
120 * is valid syntax in TASM compatibility mode. Note that
121 * you lose the ability to override the default address
122 * type for the instruction, but we never use anything
123 * but 32-bit flat model addressing in our code.
124 */
125 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700126 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300127 break;
128 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700129 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300130 break;
131 case S_DWORD:
132 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700133 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300134 break;
135 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700136 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300137 break;
138 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700139 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300140 break;
141 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700142 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300143 break;
144 default:
145 nasm_error(ERR_NONFATAL,
146 "invalid operand size specification");
147 break;
148 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700149 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300150 /* Standard NASM compatible syntax */
151 switch ((int)tokval.t_integer) {
152 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700153 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300154 break;
155 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700156 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300157 break;
158 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700159 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300160 break;
161 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700162 op->disp_size = 8;
163 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300164 break;
165 case P_A16:
166 case P_A32:
167 case P_A64:
168 if (result->prefixes[PPS_ASIZE] &&
169 result->prefixes[PPS_ASIZE] != tokval.t_integer)
170 nasm_error(ERR_NONFATAL,
171 "conflicting address size specifications");
172 else
173 result->prefixes[PPS_ASIZE] = tokval.t_integer;
174 break;
175 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700176 op->disp_size = 16;
177 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300178 break;
179 case S_DWORD:
180 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700181 op->disp_size = 32;
182 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300183 break;
184 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700185 op->disp_size = 64;
186 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300187 break;
188 default:
189 nasm_error(ERR_NONFATAL, "invalid size specification in"
190 " effective address");
191 break;
192 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700193 }
194}
195
Jin Kyu Song72018a22013-08-05 20:46:18 -0700196/*
197 * when two or more decorators follow a register operand,
198 * consecutive decorators are parsed here.
Jin Kyu Songf9a71e02013-08-21 19:29:09 -0700199 * opmask and zeroing decorators can be placed in any order.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700200 * e.g. zmm1 {k2}{z} or zmm2 {z,k3}
201 * decorator(s) are placed at the end of an operand.
202 */
203static bool parse_braces(decoflags_t *decoflags)
204{
205 int i;
206 bool recover = false;
207
208 i = tokval.t_type;
209 do {
210 if (i == TOKEN_OPMASK) {
211 if (*decoflags & OPMASK_MASK) {
212 nasm_error(ERR_NONFATAL, "opmask k%lu is already set",
213 *decoflags & OPMASK_MASK);
214 *decoflags &= ~OPMASK_MASK;
215 }
216 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
217 } else if (i == TOKEN_DECORATOR) {
218 switch (tokval.t_integer) {
219 case BRC_Z:
220 /*
221 * according to AVX512 spec, only zeroing/merging decorator
222 * is supported with opmask
223 */
224 *decoflags |= GEN_Z(0);
225 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700226 default:
227 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
228 tokval.t_charptr);
229 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700230 }
231 } else if (i == ',' || i == TOKEN_EOS){
232 break;
233 } else {
234 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
235 " expected");
236 recover = true;
237 break;
238 }
239 i = stdscan(NULL, &tokval);
240 } while(1);
241
242 return recover;
243}
244
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700245static int parse_mref(operand *op, const expr *e,
246 const struct eval_hints *hints, decoflags_t brace_flags)
247{
248 int b, i, s; /* basereg, indexreg, scale */
249 int64_t o; /* offset */
250
251 b = i = -1, o = s = 0;
252 op->hintbase = hints->base;
253 op->hinttype = hints->type;
254
255 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
256 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
257
258 if (is_gpr && e->value == 1)
259 b = e->type; /* It can be basereg */
260 else /* No, it has to be indexreg */
261 i = e->type, s = e->value;
262 e++;
263 }
264 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
265 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
266
267 if (b != -1) /* If the first was the base, ... */
268 i = e->type, s = e->value; /* second has to be indexreg */
269
270 else if (!is_gpr || e->value != 1) {
271 /* If both want to be index */
272 nasm_error(ERR_NONFATAL,
273 "invalid effective address: two index registers");
274 return -1;
275 } else
276 b = e->type;
277 e++;
278 }
279 if (e->type != 0) { /* is there an offset? */
280 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
281 nasm_error(ERR_NONFATAL,
282 "beroset-p-603-invalid effective address");
283 return -1;
284 } else {
285 if (e->type == EXPR_UNKNOWN) {
286 op->opflags |= OPFLAG_UNKNOWN;
287 o = 0; /* doesn't matter what */
288 op->wrt = NO_SEG; /* nor this */
289 op->segment = NO_SEG; /* or this */
290 while (e->type)
291 e++; /* go to the end of the line */
292 } else {
293 if (e->type == EXPR_SIMPLE) {
294 o = e->value;
295 e++;
296 }
297 if (e->type == EXPR_WRT) {
298 op->wrt = e->value;
299 e++;
300 } else
301 op->wrt = NO_SEG;
302 /*
303 * Look for a segment base type.
304 */
305 if (e->type && e->type < EXPR_SEGBASE) {
306 nasm_error(ERR_NONFATAL,
307 "beroset-p-630-invalid effective address");
308 return -1;
309 }
310 while (e->type && e->value == 0)
311 e++;
312 if (e->type && e->value != 1) {
313 nasm_error(ERR_NONFATAL,
314 "beroset-p-637-invalid effective address");
315 return -1;
316 }
317 if (e->type) {
318 op->segment =
319 e->type - EXPR_SEGBASE;
320 e++;
321 } else
322 op->segment = NO_SEG;
323 while (e->type && e->value == 0)
324 e++;
325 if (e->type) {
326 nasm_error(ERR_NONFATAL,
327 "beroset-p-650-invalid effective address");
328 return -1;
329 }
330 }
331 }
332 } else {
333 o = 0;
334 op->wrt = NO_SEG;
335 op->segment = NO_SEG;
336 }
337
338 if (e->type != 0) { /* there'd better be nothing left! */
339 nasm_error(ERR_NONFATAL,
340 "beroset-p-663-invalid effective address");
341 return -1;
342 }
343
344 /* It is memory, but it can match any r/m operand */
345 op->type |= MEMORY_ANY;
346
347 if (b == -1 && (i == -1 || s == 0)) {
348 int is_rel = globalbits == 64 &&
349 !(op->eaflags & EAF_ABS) &&
350 ((globalrel &&
351 !(op->eaflags & EAF_FSGS)) ||
352 (op->eaflags & EAF_REL));
353
354 op->type |= is_rel ? IP_REL : MEM_OFFS;
355 }
356
357 if (i != -1) {
358 opflags_t iclass = nasm_reg_flags[i];
359
360 if (is_class(XMMREG,iclass))
361 op->type |= XMEM;
362 else if (is_class(YMMREG,iclass))
363 op->type |= YMEM;
364 else if (is_class(ZMMREG,iclass))
365 op->type |= ZMEM;
366 }
367
368 op->basereg = b;
369 op->indexreg = i;
370 op->scale = s;
371 op->offset = o;
372 op->decoflags |= brace_flags;
373 return 0;
374}
375
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700376insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000377{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400378 bool insn_is_label = false;
379 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700380 int opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000381 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800382 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700383 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000384
H. Peter Anvin9c987692007-11-04 21:09:32 -0800385restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400386 first = true;
387 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000388
H. Peter Anvin76690a12002-04-30 20:52:49 +0000389 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300390 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000391 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000392
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400393 result->label = NULL; /* Assume no label */
394 result->eops = NULL; /* must do this, whatever happens */
395 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700396 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
397 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000398
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400399 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700400 if (i == TOKEN_EOS)
401 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400402
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400403 if (i != TOKEN_ID &&
404 i != TOKEN_INSN &&
405 i != TOKEN_PREFIX &&
406 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
407 nasm_error(ERR_NONFATAL,
408 "label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700409 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000410 }
411
H. Peter Anvin9c987692007-11-04 21:09:32 -0800412 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
413 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300414 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 result->label = tokval.t_charptr;
416 i = stdscan(NULL, &tokval);
417 if (i == ':') { /* skip over the optional colon */
418 i = stdscan(NULL, &tokval);
419 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700420 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 "label alone on a line without a colon might be in error");
422 }
423 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
424 /*
425 * FIXME: location->segment could be NO_SEG, in which case
426 * it is possible we should be passing 'abs_seg'. Look into this.
427 * Work out whether that is *really* what we should be doing.
428 * Generally fix things. I think this is right as it is, but
429 * am still not certain.
430 */
431 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700432 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000434 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000435
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400436 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700437 if (i == TOKEN_EOS)
438 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000439
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400440 nasm_build_assert(P_none != 0);
441 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000442 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000443
444 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400445 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300446 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800447
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 /*
449 * Handle special case: the TIMES prefix.
450 */
451 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
452 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000453
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400455 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700457 if (!value) /* Error in evaluator */
458 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700460 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 "non-constant argument supplied to TIMES");
462 result->times = 1L;
463 } else {
464 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700465 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200466 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 value->value);
468 result->times = 0;
469 }
470 }
471 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300472 int slot = prefix_slot(tokval.t_integer);
473 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700474 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100475 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300476 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700477 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300478 nasm_error(ERR_NONFATAL,
479 "instruction has conflicting prefixes");
480 }
481 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 i = stdscan(NULL, &tokval);
483 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000484 }
485
486 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300487 int j;
488 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700489
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400490 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300491 if ((pfx = result->prefixes[j]) != P_none)
492 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400493 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700494
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700495 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 /*
497 * Instruction prefixes are present, but no actual
498 * instruction. This is allowed: at this point we
499 * invent a notional instruction of RESB 0.
500 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400501 result->opcode = I_RESB;
502 result->operands = 1;
503 result->oprs[0].type = IMMEDIATE;
504 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
506 return result;
507 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700508 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700509 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000511 }
512
513 result->opcode = tokval.t_integer;
514 result->condition = tokval.t_inttwo;
515
516 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700517 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000518 * evaluated operands, since the correct values _must_ be known
519 * on the first pass. Hence, even in pass one, we set the
520 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700521 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000522 */
Charles Crayne2581c862008-09-10 19:21:52 -0700523 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700524 critical = (pass0 < 2 ? 1 : 2);
525
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 } else
527 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000528
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700529 if (result->opcode == I_DB || result->opcode == I_DW ||
530 result->opcode == I_DD || result->opcode == I_DQ ||
531 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700532 result->opcode == I_DY || result->opcode == I_DZ ||
533 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 extop *eop, **tail = &result->eops, **fixptr;
535 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300536 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000537
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700538 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000539
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700541 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 */
543 while (1) {
544 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400545 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300547 else if (first && i == ':') {
548 insn_is_label = true;
549 goto restart_parse;
550 }
551 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 fixptr = tail;
553 eop = *tail = nasm_malloc(sizeof(extop));
554 tail = &eop->next;
555 eop->next = NULL;
556 eop->type = EOT_NOTHING;
557 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300558 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000559
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300560 /*
561 * is_comma_next() here is to distinguish this from
562 * a string used as part of an expression...
563 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700564 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400565 eop->type = EOT_DB_STRING;
566 eop->stringval = tokval.t_charptr;
567 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300569 } else if (i == TOKEN_STRFUNC) {
570 bool parens = false;
571 const char *funcname = tokval.t_charptr;
572 enum strfunc func = tokval.t_integer;
573 i = stdscan(NULL, &tokval);
574 if (i == '(') {
575 parens = true;
576 i = stdscan(NULL, &tokval);
577 }
578 if (i != TOKEN_STR) {
579 nasm_error(ERR_NONFATAL,
580 "%s must be followed by a string constant",
581 funcname);
582 eop->type = EOT_NOTHING;
583 } else {
584 eop->type = EOT_DB_STRING_FREE;
585 eop->stringlen =
586 string_transform(tokval.t_charptr, tokval.t_inttwo,
587 &eop->stringval, func);
588 if (eop->stringlen == (size_t)-1) {
589 nasm_error(ERR_NONFATAL, "invalid string for transform");
590 eop->type = EOT_NOTHING;
591 }
592 }
593 if (parens && i && i != ')') {
594 i = stdscan(NULL, &tokval);
595 if (i != ')') {
596 nasm_error(ERR_NONFATAL, "unterminated %s function",
597 funcname);
598 }
599 }
600 if (i && i != ',')
601 i = stdscan(NULL, &tokval);
602 } else if (i == '-' || i == '+') {
603 char *save = stdscan_get();
604 int token = i;
605 sign = (i == '-') ? -1 : 1;
606 i = stdscan(NULL, &tokval);
607 if (i != TOKEN_FLOAT) {
608 stdscan_set(save);
609 i = tokval.t_type = token;
610 goto is_expression;
611 } else {
612 goto is_float;
613 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700614 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300615is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300616 eop->type = EOT_DB_STRING;
617 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300618
619 eop->stringlen = idata_bytes(result->opcode);
620 if (eop->stringlen > 16) {
621 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700622 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300623 eop->stringlen = 0;
624 } else if (eop->stringlen < 1) {
625 nasm_error(ERR_NONFATAL, "floating-point constant"
626 " encountered in unknown instruction");
627 /*
628 * fix suggested by Pedro Gimeno... original line was:
629 * eop->type = EOT_NOTHING;
630 */
631 eop->stringlen = 0;
632 }
633
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300634 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
635 tail = &eop->next;
636 *fixptr = eop;
637 eop->stringval = (char *)eop + sizeof(extop);
638 if (!eop->stringlen ||
639 !float_const(tokval.t_charptr, sign,
640 (uint8_t *)eop->stringval,
641 eop->stringlen, nasm_error))
642 eop->type = EOT_NOTHING;
643 i = stdscan(NULL, &tokval); /* eat the comma */
644 } else {
645 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700647
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300648is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700650 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700652 if (!value) /* Error in evaluator */
653 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 if (is_unknown(value)) {
655 eop->type = EOT_DB_NUMBER;
656 eop->offset = 0; /* doesn't matter what we put */
657 eop->segment = eop->wrt = NO_SEG; /* likewise */
658 } else if (is_reloc(value)) {
659 eop->type = EOT_DB_NUMBER;
660 eop->offset = reloc_value(value);
661 eop->segment = reloc_seg(value);
662 eop->wrt = reloc_wrt(value);
663 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700664 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 "operand %d: expression is not simple"
666 " or relocatable", oper_num);
667 }
668 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000669
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670 /*
671 * We're about to call stdscan(), which will eat the
672 * comma that we're currently sitting on between
673 * arguments. However, we'd better check first that it
674 * _is_ a comma.
675 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400676 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 break;
678 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700679 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400680 oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700681 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 }
683 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000684
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 if (result->opcode == I_INCBIN) {
686 /*
687 * Correct syntax for INCBIN is that there should be
688 * one string operand, followed by one or two numeric
689 * operands.
690 */
691 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700692 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 else if (result->eops->next &&
694 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200695 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400696 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 else if (result->eops->next && result->eops->next->next &&
698 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200699 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400700 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701 else if (result->eops->next && result->eops->next->next &&
702 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700703 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400704 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000705 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 return result;
707 /*
708 * If we reach here, one of the above errors happened.
709 * Throw the instruction away.
710 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700711 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700713 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 "no operand for data declaration");
715 else
716 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000717
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000719 }
720
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400721 /*
722 * Now we begin to parse the operands. There may be up to four
723 * of these, separated by commas, and terminated by a zero token.
724 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000725
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700726 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
727 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300728 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 int mref; /* is this going to be a memory ref? */
730 int bracket; /* is it a [] mref, or a & mref? */
731 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700732 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700734 op->disp_size = 0; /* have to zero this whatever */
735 op->eaflags = 0; /* and this */
736 op->opflags = 0;
737 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000738
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400740 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300742 else if (first && i == ':') {
743 insn_is_label = true;
744 goto restart_parse;
745 }
746 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700747 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 while (i == TOKEN_SPECIAL) { /* size specifiers */
749 switch ((int)tokval.t_integer) {
750 case S_BYTE:
751 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700752 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 setsize = 1;
754 break;
755 case S_WORD:
756 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700757 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 setsize = 1;
759 break;
760 case S_DWORD:
761 case S_LONG:
762 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700763 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 setsize = 1;
765 break;
766 case S_QWORD:
767 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700768 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 setsize = 1;
770 break;
771 case S_TWORD:
772 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700773 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 setsize = 1;
775 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700776 case S_OWORD:
777 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700778 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700779 setsize = 1;
780 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700781 case S_YWORD:
782 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700783 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700784 setsize = 1;
785 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700786 case S_ZWORD:
787 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700788 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700789 setsize = 1;
790 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700792 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 break;
794 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700795 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 break;
797 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700798 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 break;
800 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700801 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 break;
803 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700804 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 break;
806 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700807 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 }
809 i = stdscan(NULL, &tokval);
810 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700813 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700815 i = stdscan(NULL, &tokval); /* then skip the colon */
816 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700817 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700818 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 }
820 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700821 mref = false;
822 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000824
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700825 if ((op->type & FAR) && !mref &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700827 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000829
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700831 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700832 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700834 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700835 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700837 if (!value) /* Error in evaluator */
838 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 if (i == ':' && mref) { /* it was seg:offset */
840 /*
841 * Process the segment override.
842 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400843 if (value[1].type != 0 ||
844 value->value != 1 ||
845 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700846 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700847 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700848 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700849 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000850 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300851 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400852 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700853 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300854 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700857 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700858 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 i = stdscan(NULL, &tokval);
860 }
861 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700862 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700863 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700865 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700866 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 }
868 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700869 if (!value) /* Error in evaluator */
870 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200872
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700873 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 if (mref && bracket) { /* find ] at the end */
875 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700876 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200877 recover = true;
878 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700880 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700881 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700882 * according to AVX512 spec, broacast or opmask decorator
883 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700884 */
885 if (tokval.t_flag & TFLAG_BRDCAST) {
886 brace_flags |= GEN_BRDCAST(0);
887 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700888 } else if (i == TOKEN_OPMASK) {
889 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
890 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700891 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700892 nasm_error(ERR_NONFATAL, "broadcast or opmask "
893 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700894 recover = true;
895 }
896 }
897
Victor van den Elzen02846d32009-06-23 03:47:07 +0200898 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700899 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200900 recover = true;
901 }
902 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700904 if (i != 0 && i != ',' && i != ':' &&
905 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
906 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
907 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200908 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700910 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700911 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
912 /* parse opmask (and zeroing) after an operand */
913 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 }
915 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200916 if (recover) {
917 do { /* error recovery */
918 i = stdscan(NULL, &tokval);
919 } while (i != 0 && i != ',');
920 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000921
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300922 /*
923 * now convert the exprs returned from evaluate()
924 * into operand descriptions...
925 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000926
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 if (mref) { /* it's a memory reference */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700928 if (parse_mref(op, value, &hints, brace_flags))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700929 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700932 op->type |= IMMEDIATE;
933 op->opflags |= OPFLAG_UNKNOWN;
934 op->offset = 0; /* don't care */
935 op->segment = NO_SEG; /* don't care again */
936 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100937
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700938 if(optimizing >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300939 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700940 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400941 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300942 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700944 op->type |= IMMEDIATE;
945 op->offset = reloc_value(value);
946 op->segment = reloc_seg(value);
947 op->wrt = reloc_wrt(value);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400950 uint64_t n = reloc_value(value);
951 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700952 op->type |= UNITY;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 if (optimizing >= 0 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700954 !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400955 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700956 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400957 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700958 op->type |= SBYTEWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400959 if (n <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700960 op->type |= UDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400961 if (n + 0x80000000 <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700962 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 }
964 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700965 } else if(value->type == EXPR_RDSAE) {
966 /*
967 * it's not an operand but a rounding or SAE decorator.
968 * put the decorator information in the (opflag_t) type field
969 * of previous operand.
970 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700971 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700972 switch (value->value) {
973 case BRC_RN:
974 case BRC_RU:
975 case BRC_RD:
976 case BRC_RZ:
977 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700978 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700979 result->evex_rm = value->value;
980 break;
981 default:
982 nasm_error(ERR_NONFATAL, "invalid decorator");
983 break;
984 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +0400986 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700989 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700990 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 /*
994 * check that its only 1 register, not an expression...
995 */
996 for (i = 1; value[i].type; i++)
997 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700998 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700999 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001001
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001003 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 /*
1005 * we want to produce a warning iff the specified size
1006 * is different from the register size
1007 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001008 rs = op->type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001010 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001012 op->type &= TO;
1013 op->type |= REGISTER;
1014 op->type |= nasm_reg_flags[value->type];
1015 op->decoflags |= brace_flags;
1016 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001018 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001019 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 "register size specification ignored");
1021 }
1022 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001023
1024 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001025 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1026 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001027 }
1028
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001029 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001030
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001031 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 while (opnum < MAX_OPERANDS)
1033 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001034
1035 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001036 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001037 */
1038 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 case I_RESW:
1040 result->opcode = I_RESB;
1041 result->oprs[0].offset *= 2;
1042 break;
1043 case I_RESD:
1044 result->opcode = I_RESB;
1045 result->oprs[0].offset *= 4;
1046 break;
1047 case I_RESQ:
1048 result->opcode = I_RESB;
1049 result->oprs[0].offset *= 8;
1050 break;
1051 case I_REST:
1052 result->opcode = I_RESB;
1053 result->oprs[0].offset *= 10;
1054 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001055 case I_RESO:
1056 result->opcode = I_RESB;
1057 result->oprs[0].offset *= 16;
1058 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001059 case I_RESY:
1060 result->opcode = I_RESB;
1061 result->oprs[0].offset *= 32;
1062 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001063 case I_RESZ:
1064 result->opcode = I_RESB;
1065 result->oprs[0].offset *= 64;
1066 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001067 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001068 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001069 }
1070
1071 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001072
1073fail:
1074 result->opcode = I_none;
1075 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001076}
1077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001079{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001080 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001081 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001082 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001083
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001084 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001086 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001087
H. Peter Anvin76690a12002-04-30 20:52:49 +00001088 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001089}
1090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001092{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001093 extop *e;
1094
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001095 while ((e = i->eops)) {
1096 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001097 if (e->type == EOT_DB_STRING_FREE)
1098 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001100 }
1101}