blob: 0068ca3602705db241cfb2719351807999a3732b [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 Anvin9148fb52013-09-27 16:39:16 -0700245static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700246{
247 int b, i, s; /* basereg, indexreg, scale */
248 int64_t o; /* offset */
249
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700250 b = i = -1;
251 o = s = 0;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700252
253 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
254 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
255
256 if (is_gpr && e->value == 1)
257 b = e->type; /* It can be basereg */
258 else /* No, it has to be indexreg */
259 i = e->type, s = e->value;
260 e++;
261 }
262 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
263 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
264
265 if (b != -1) /* If the first was the base, ... */
266 i = e->type, s = e->value; /* second has to be indexreg */
267
268 else if (!is_gpr || e->value != 1) {
269 /* If both want to be index */
270 nasm_error(ERR_NONFATAL,
271 "invalid effective address: two index registers");
272 return -1;
273 } else
274 b = e->type;
275 e++;
276 }
277 if (e->type != 0) { /* is there an offset? */
278 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
279 nasm_error(ERR_NONFATAL,
280 "beroset-p-603-invalid effective address");
281 return -1;
282 } else {
283 if (e->type == EXPR_UNKNOWN) {
284 op->opflags |= OPFLAG_UNKNOWN;
285 o = 0; /* doesn't matter what */
286 op->wrt = NO_SEG; /* nor this */
287 op->segment = NO_SEG; /* or this */
288 while (e->type)
289 e++; /* go to the end of the line */
290 } else {
291 if (e->type == EXPR_SIMPLE) {
292 o = e->value;
293 e++;
294 }
295 if (e->type == EXPR_WRT) {
296 op->wrt = e->value;
297 e++;
298 } else
299 op->wrt = NO_SEG;
300 /*
301 * Look for a segment base type.
302 */
303 if (e->type && e->type < EXPR_SEGBASE) {
304 nasm_error(ERR_NONFATAL,
305 "beroset-p-630-invalid effective address");
306 return -1;
307 }
308 while (e->type && e->value == 0)
309 e++;
310 if (e->type && e->value != 1) {
311 nasm_error(ERR_NONFATAL,
312 "beroset-p-637-invalid effective address");
313 return -1;
314 }
315 if (e->type) {
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700316 op->segment = e->type - EXPR_SEGBASE;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700317 e++;
318 } else
319 op->segment = NO_SEG;
320 while (e->type && e->value == 0)
321 e++;
322 if (e->type) {
323 nasm_error(ERR_NONFATAL,
324 "beroset-p-650-invalid effective address");
325 return -1;
326 }
327 }
328 }
329 } else {
330 o = 0;
331 op->wrt = NO_SEG;
332 op->segment = NO_SEG;
333 }
334
335 if (e->type != 0) { /* there'd better be nothing left! */
336 nasm_error(ERR_NONFATAL,
337 "beroset-p-663-invalid effective address");
338 return -1;
339 }
340
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700341 op->basereg = b;
342 op->indexreg = i;
343 op->scale = s;
344 op->offset = o;
345 return 0;
346}
347
348static void mref_set_optype(operand *op)
349{
350 int b = op->basereg;
351 int i = op->indexreg;
352 int s = op->scale;
353
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700354 /* It is memory, but it can match any r/m operand */
355 op->type |= MEMORY_ANY;
356
357 if (b == -1 && (i == -1 || s == 0)) {
358 int is_rel = globalbits == 64 &&
359 !(op->eaflags & EAF_ABS) &&
360 ((globalrel &&
361 !(op->eaflags & EAF_FSGS)) ||
362 (op->eaflags & EAF_REL));
363
364 op->type |= is_rel ? IP_REL : MEM_OFFS;
365 }
366
367 if (i != -1) {
368 opflags_t iclass = nasm_reg_flags[i];
369
370 if (is_class(XMMREG,iclass))
371 op->type |= XMEM;
372 else if (is_class(YMMREG,iclass))
373 op->type |= YMEM;
374 else if (is_class(ZMMREG,iclass))
375 op->type |= ZMEM;
376 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700377}
378
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700379insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000380{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400381 bool insn_is_label = false;
382 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700383 int opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000384 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800385 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700386 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000387
H. Peter Anvin9c987692007-11-04 21:09:32 -0800388restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400389 first = true;
390 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000391
H. Peter Anvin76690a12002-04-30 20:52:49 +0000392 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300393 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000394 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000395
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400396 result->label = NULL; /* Assume no label */
397 result->eops = NULL; /* must do this, whatever happens */
398 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700399 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
400 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000401
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400402 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700403 if (i == TOKEN_EOS)
404 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400405
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400406 if (i != TOKEN_ID &&
407 i != TOKEN_INSN &&
408 i != TOKEN_PREFIX &&
409 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
410 nasm_error(ERR_NONFATAL,
411 "label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700412 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000413 }
414
H. Peter Anvin9c987692007-11-04 21:09:32 -0800415 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
416 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300417 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 result->label = tokval.t_charptr;
419 i = stdscan(NULL, &tokval);
420 if (i == ':') { /* skip over the optional colon */
421 i = stdscan(NULL, &tokval);
422 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700423 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 "label alone on a line without a colon might be in error");
425 }
426 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
427 /*
428 * FIXME: location->segment could be NO_SEG, in which case
429 * it is possible we should be passing 'abs_seg'. Look into this.
430 * Work out whether that is *really* what we should be doing.
431 * Generally fix things. I think this is right as it is, but
432 * am still not certain.
433 */
434 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700435 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000437 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000438
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400439 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700440 if (i == TOKEN_EOS)
441 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000442
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400443 nasm_build_assert(P_none != 0);
444 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000445 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000446
447 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400448 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300449 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800450
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 /*
452 * Handle special case: the TIMES prefix.
453 */
454 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
455 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000456
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400458 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700460 if (!value) /* Error in evaluator */
461 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700463 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000464 "non-constant argument supplied to TIMES");
465 result->times = 1L;
466 } else {
467 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700468 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200469 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 value->value);
471 result->times = 0;
472 }
473 }
474 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300475 int slot = prefix_slot(tokval.t_integer);
476 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700477 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100478 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300479 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700480 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300481 nasm_error(ERR_NONFATAL,
482 "instruction has conflicting prefixes");
483 }
484 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 i = stdscan(NULL, &tokval);
486 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000487 }
488
489 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300490 int j;
491 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700492
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400493 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300494 if ((pfx = result->prefixes[j]) != P_none)
495 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400496 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700497
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700498 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 /*
500 * Instruction prefixes are present, but no actual
501 * instruction. This is allowed: at this point we
502 * invent a notional instruction of RESB 0.
503 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400504 result->opcode = I_RESB;
505 result->operands = 1;
506 result->oprs[0].type = IMMEDIATE;
507 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
509 return result;
510 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700511 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700512 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000514 }
515
516 result->opcode = tokval.t_integer;
517 result->condition = tokval.t_inttwo;
518
519 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700520 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000521 * evaluated operands, since the correct values _must_ be known
522 * on the first pass. Hence, even in pass one, we set the
523 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700524 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000525 */
Charles Crayne2581c862008-09-10 19:21:52 -0700526 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700527 critical = (pass0 < 2 ? 1 : 2);
528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 } else
530 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000531
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700532 if (result->opcode == I_DB || result->opcode == I_DW ||
533 result->opcode == I_DD || result->opcode == I_DQ ||
534 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700535 result->opcode == I_DY || result->opcode == I_DZ ||
536 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 extop *eop, **tail = &result->eops, **fixptr;
538 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300539 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000540
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700541 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000542
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700544 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 */
546 while (1) {
547 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400548 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300550 else if (first && i == ':') {
551 insn_is_label = true;
552 goto restart_parse;
553 }
554 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 fixptr = tail;
556 eop = *tail = nasm_malloc(sizeof(extop));
557 tail = &eop->next;
558 eop->next = NULL;
559 eop->type = EOT_NOTHING;
560 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300561 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300563 /*
564 * is_comma_next() here is to distinguish this from
565 * a string used as part of an expression...
566 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700567 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400568 eop->type = EOT_DB_STRING;
569 eop->stringval = tokval.t_charptr;
570 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300572 } else if (i == TOKEN_STRFUNC) {
573 bool parens = false;
574 const char *funcname = tokval.t_charptr;
575 enum strfunc func = tokval.t_integer;
576 i = stdscan(NULL, &tokval);
577 if (i == '(') {
578 parens = true;
579 i = stdscan(NULL, &tokval);
580 }
581 if (i != TOKEN_STR) {
582 nasm_error(ERR_NONFATAL,
583 "%s must be followed by a string constant",
584 funcname);
585 eop->type = EOT_NOTHING;
586 } else {
587 eop->type = EOT_DB_STRING_FREE;
588 eop->stringlen =
589 string_transform(tokval.t_charptr, tokval.t_inttwo,
590 &eop->stringval, func);
591 if (eop->stringlen == (size_t)-1) {
592 nasm_error(ERR_NONFATAL, "invalid string for transform");
593 eop->type = EOT_NOTHING;
594 }
595 }
596 if (parens && i && i != ')') {
597 i = stdscan(NULL, &tokval);
598 if (i != ')') {
599 nasm_error(ERR_NONFATAL, "unterminated %s function",
600 funcname);
601 }
602 }
603 if (i && i != ',')
604 i = stdscan(NULL, &tokval);
605 } else if (i == '-' || i == '+') {
606 char *save = stdscan_get();
607 int token = i;
608 sign = (i == '-') ? -1 : 1;
609 i = stdscan(NULL, &tokval);
610 if (i != TOKEN_FLOAT) {
611 stdscan_set(save);
612 i = tokval.t_type = token;
613 goto is_expression;
614 } else {
615 goto is_float;
616 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700617 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300618is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300619 eop->type = EOT_DB_STRING;
620 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300621
622 eop->stringlen = idata_bytes(result->opcode);
623 if (eop->stringlen > 16) {
624 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700625 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300626 eop->stringlen = 0;
627 } else if (eop->stringlen < 1) {
628 nasm_error(ERR_NONFATAL, "floating-point constant"
629 " encountered in unknown instruction");
630 /*
631 * fix suggested by Pedro Gimeno... original line was:
632 * eop->type = EOT_NOTHING;
633 */
634 eop->stringlen = 0;
635 }
636
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300637 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
638 tail = &eop->next;
639 *fixptr = eop;
640 eop->stringval = (char *)eop + sizeof(extop);
641 if (!eop->stringlen ||
642 !float_const(tokval.t_charptr, sign,
643 (uint8_t *)eop->stringval,
644 eop->stringlen, nasm_error))
645 eop->type = EOT_NOTHING;
646 i = stdscan(NULL, &tokval); /* eat the comma */
647 } else {
648 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700650
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300651is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700653 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700655 if (!value) /* Error in evaluator */
656 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 if (is_unknown(value)) {
658 eop->type = EOT_DB_NUMBER;
659 eop->offset = 0; /* doesn't matter what we put */
660 eop->segment = eop->wrt = NO_SEG; /* likewise */
661 } else if (is_reloc(value)) {
662 eop->type = EOT_DB_NUMBER;
663 eop->offset = reloc_value(value);
664 eop->segment = reloc_seg(value);
665 eop->wrt = reloc_wrt(value);
666 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700667 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 "operand %d: expression is not simple"
669 " or relocatable", oper_num);
670 }
671 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000672
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 /*
674 * We're about to call stdscan(), which will eat the
675 * comma that we're currently sitting on between
676 * arguments. However, we'd better check first that it
677 * _is_ a comma.
678 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400679 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 break;
681 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700682 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400683 oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700684 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 }
686 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000687
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 if (result->opcode == I_INCBIN) {
689 /*
690 * Correct syntax for INCBIN is that there should be
691 * one string operand, followed by one or two numeric
692 * operands.
693 */
694 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700695 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 else if (result->eops->next &&
697 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200698 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400699 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 else if (result->eops->next && result->eops->next->next &&
701 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200702 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400703 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 else if (result->eops->next && result->eops->next->next &&
705 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700706 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400707 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000708 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 return result;
710 /*
711 * If we reach here, one of the above errors happened.
712 * Throw the instruction away.
713 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700714 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700716 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 "no operand for data declaration");
718 else
719 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000720
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000722 }
723
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400724 /*
725 * Now we begin to parse the operands. There may be up to four
726 * of these, separated by commas, and terminated by a zero token.
727 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700729 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
730 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300731 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700732 bool mref; /* is this going to be a memory ref? */
733 bool bracket; /* is it a [] mref, or a & mref? */
734 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700736 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000737
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700738 op->disp_size = 0; /* have to zero this whatever */
739 op->eaflags = 0; /* and this */
740 op->opflags = 0;
741 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000742
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400744 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300746 else if (first && i == ':') {
747 insn_is_label = true;
748 goto restart_parse;
749 }
750 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700751 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 while (i == TOKEN_SPECIAL) { /* size specifiers */
753 switch ((int)tokval.t_integer) {
754 case S_BYTE:
755 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700756 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 setsize = 1;
758 break;
759 case S_WORD:
760 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700761 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 setsize = 1;
763 break;
764 case S_DWORD:
765 case S_LONG:
766 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700767 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 setsize = 1;
769 break;
770 case S_QWORD:
771 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700772 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 setsize = 1;
774 break;
775 case S_TWORD:
776 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700777 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 setsize = 1;
779 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700780 case S_OWORD:
781 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700782 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700783 setsize = 1;
784 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700785 case S_YWORD:
786 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700787 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700788 setsize = 1;
789 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700790 case S_ZWORD:
791 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700792 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700793 setsize = 1;
794 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700796 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 break;
798 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700799 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 break;
801 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700802 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 break;
804 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700805 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 break;
807 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700808 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 break;
810 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700811 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 }
813 i = stdscan(NULL, &tokval);
814 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700817 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700819 i = stdscan(NULL, &tokval); /* then skip the colon */
820 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700821 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700822 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700824 /* when a comma follows an opening bracket - [ , eax*4] */
825 if (i == ',') {
826 /* treat as if there is a zero displacement virtually */
827 tokval.t_type = TOKEN_NUM;
828 tokval.t_integer = 0;
829 stdscan_set(stdscan_get() - 1); /* rewind the comma */
830 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700832 mref = false;
833 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000835
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700836 if ((op->type & FAR) && !mref &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700838 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000840
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700842 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700843 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700845 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700846 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700848 if (!value) /* Error in evaluator */
849 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 if (i == ':' && mref) { /* it was seg:offset */
851 /*
852 * Process the segment override.
853 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400854 if (value[1].type != 0 ||
855 value->value != 1 ||
856 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700857 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700858 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700859 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700860 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000861 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300862 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400863 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700864 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300865 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000866
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700868 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700869 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 i = stdscan(NULL, &tokval);
871 }
872 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700873 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700874 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700876 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700877 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 }
879 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700880 if (!value) /* Error in evaluator */
881 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200883
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700884 mib = false;
885 if (mref && bracket && i == ',') {
886 /* [seg:base+offset,index*scale] syntax (mib) */
887
888 operand o1, o2; /* Partial operands */
889
890 if (parse_mref(&o1, value))
891 goto fail;
892
893 i = stdscan(NULL, &tokval); /* Eat comma */
894 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
895 critical, nasm_error, &hints);
896 i = tokval.t_type;
897
898 if (parse_mref(&o2, value))
899 goto fail;
900
901 if (o2.basereg != -1 && o2.indexreg == -1) {
902 o2.indexreg = o2.basereg;
903 o2.scale = 1;
904 o2.basereg = -1;
905 }
906
907 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
908 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
909 nasm_error(ERR_NONFATAL, "invalid mib expression");
910 goto fail;
911 }
912
913 op->basereg = o1.basereg;
914 op->indexreg = o2.indexreg;
915 op->scale = o2.scale;
916 op->offset = o1.offset;
917 op->segment = o1.segment;
918 op->wrt = o1.wrt;
919
920 if (op->basereg != -1) {
921 op->hintbase = op->basereg;
922 op->hinttype = EAH_MAKEBASE;
923 } else if (op->indexreg != -1) {
924 op->hintbase = op->indexreg;
925 op->hinttype = EAH_NOTBASE;
926 } else {
927 op->hintbase = -1;
928 op->hinttype = EAH_NOHINT;
929 }
930
931 mib = true;
932 }
933
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700934 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 if (mref && bracket) { /* find ] at the end */
936 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700937 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200938 recover = true;
939 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700941 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700942 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700943 * according to AVX512 spec, broacast or opmask decorator
944 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700945 */
946 if (tokval.t_flag & TFLAG_BRDCAST) {
947 brace_flags |= GEN_BRDCAST(0);
948 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700949 } else if (i == TOKEN_OPMASK) {
950 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
951 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700952 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700953 nasm_error(ERR_NONFATAL, "broadcast or opmask "
954 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700955 recover = true;
956 }
957 }
958
Victor van den Elzen02846d32009-06-23 03:47:07 +0200959 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700960 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200961 recover = true;
962 }
963 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700965 if (i != 0 && i != ',' && i != ':' &&
966 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
967 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
968 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200969 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700971 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700972 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
973 /* parse opmask (and zeroing) after an operand */
974 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 }
976 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200977 if (recover) {
978 do { /* error recovery */
979 i = stdscan(NULL, &tokval);
980 } while (i != 0 && i != ',');
981 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000982
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300983 /*
984 * now convert the exprs returned from evaluate()
985 * into operand descriptions...
986 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700987 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000988
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700990 /* A mib reference was fully parsed already */
991 if (!mib) {
992 if (parse_mref(op, value))
993 goto fail;
994 op->hintbase = hints.base;
995 op->hinttype = hints.type;
996 }
997 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001000 op->type |= IMMEDIATE;
1001 op->opflags |= OPFLAG_UNKNOWN;
1002 op->offset = 0; /* don't care */
1003 op->segment = NO_SEG; /* don't care again */
1004 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001005
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001006 if(optimizing >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001007 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001008 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001009 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001010 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001012 op->type |= IMMEDIATE;
1013 op->offset = reloc_value(value);
1014 op->segment = reloc_seg(value);
1015 op->wrt = reloc_wrt(value);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001016
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001018 uint64_t n = reloc_value(value);
1019 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001020 op->type |= UNITY;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 if (optimizing >= 0 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001022 !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001023 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001024 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001025 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001026 op->type |= SBYTEWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001027 if (n <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001028 op->type |= UDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001029 if (n + 0x80000000 <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001030 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 }
1032 }
Jin Kyu Song72018a22013-08-05 20:46:18 -07001033 } else if(value->type == EXPR_RDSAE) {
1034 /*
1035 * it's not an operand but a rounding or SAE decorator.
1036 * put the decorator information in the (opflag_t) type field
1037 * of previous operand.
1038 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001039 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001040 switch (value->value) {
1041 case BRC_RN:
1042 case BRC_RU:
1043 case BRC_RD:
1044 case BRC_RZ:
1045 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001046 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001047 result->evex_rm = value->value;
1048 break;
1049 default:
1050 nasm_error(ERR_NONFATAL, "invalid decorator");
1051 break;
1052 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001054 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001057 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001058 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 /*
1062 * check that its only 1 register, not an expression...
1063 */
1064 for (i = 1; value[i].type; i++)
1065 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001066 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001067 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001069
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001071 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 /*
1073 * we want to produce a warning iff the specified size
1074 * is different from the register size
1075 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001076 rs = op->type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001078 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001080 op->type &= TO;
1081 op->type |= REGISTER;
1082 op->type |= nasm_reg_flags[value->type];
1083 op->decoflags |= brace_flags;
1084 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001086 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001087 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 "register size specification ignored");
1089 }
1090 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001091
1092 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001093 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1094 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001095 }
1096
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001097 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001098
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001099 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001100 while (opnum < MAX_OPERANDS)
1101 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001102
1103 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001104 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001105 */
1106 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 case I_RESW:
1108 result->opcode = I_RESB;
1109 result->oprs[0].offset *= 2;
1110 break;
1111 case I_RESD:
1112 result->opcode = I_RESB;
1113 result->oprs[0].offset *= 4;
1114 break;
1115 case I_RESQ:
1116 result->opcode = I_RESB;
1117 result->oprs[0].offset *= 8;
1118 break;
1119 case I_REST:
1120 result->opcode = I_RESB;
1121 result->oprs[0].offset *= 10;
1122 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001123 case I_RESO:
1124 result->opcode = I_RESB;
1125 result->oprs[0].offset *= 16;
1126 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001127 case I_RESY:
1128 result->opcode = I_RESB;
1129 result->oprs[0].offset *= 32;
1130 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001131 case I_RESZ:
1132 result->opcode = I_RESB;
1133 result->oprs[0].offset *= 64;
1134 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001135 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001136 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001137 }
1138
1139 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001140
1141fail:
1142 result->opcode = I_none;
1143 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001144}
1145
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001147{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001148 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001149 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001150 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001151
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001152 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001154 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001155
H. Peter Anvin76690a12002-04-30 20:52:49 +00001156 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001157}
1158
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001160{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001161 extop *e;
1162
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001163 while ((e = i->eops)) {
1164 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001165 if (e->type == EOT_DB_STRING_FREE)
1166 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001168 }
1169}