blob: 343f35e5da9c75c9fe6b0bdd62332ecd66c39516 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvine20ca022013-07-19 17:06:08 -07003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * parser.c source line parser for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000048#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000049#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000050#include "stdscan.h"
H. Peter Anvin00444ae2009-07-18 18:49:55 -070051#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052#include "parser.h"
53#include "float.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055
H. Peter Anvine2c80182005-01-15 22:15:51 +000056extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030057extern int32_t abs_seg; /* ABSOLUTE segment */
58extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000059
H. Peter Anvine2c80182005-01-15 22:15:51 +000060static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062static int i;
63static struct tokenval tokval;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030064static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000065
H. Peter Anvin605f5152009-07-18 18:31:41 -070066void parser_global_info(struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000067{
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 location = locp;
69}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040071static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070072{
73 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080074 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030075 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070076 case R_CS:
77 case R_DS:
78 case R_SS:
79 case R_ES:
80 case R_FS:
81 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030082 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070083 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080084 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070085 case P_REP:
86 case P_REPE:
87 case P_REPZ:
88 case P_REPNE:
89 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080090 case P_XACQUIRE:
91 case P_XRELEASE:
Jin Kyu Song03041092013-10-15 19:38:51 -070092 case P_BND:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080093 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070094 case P_O16:
95 case P_O32:
96 case P_O64:
97 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030098 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070099 case P_A16:
100 case P_A32:
101 case P_A64:
102 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300103 return PPS_ASIZE;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700104 case P_EVEX:
105 return PPS_EVEX;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700106 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300107 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
108 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700109 }
110}
111
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700112static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700113{
114 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300115 switch ((int)tokval.t_integer) {
116 /* For TASM compatibility a size override inside the
117 * brackets changes the size of the operand, not the
118 * address type of the operand as it does in standard
119 * NASM syntax. Hence:
120 *
121 * mov eax,[DWORD val]
122 *
123 * is valid syntax in TASM compatibility mode. Note that
124 * you lose the ability to override the default address
125 * type for the instruction, but we never use anything
126 * but 32-bit flat model addressing in our code.
127 */
128 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700129 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300130 break;
131 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700132 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300133 break;
134 case S_DWORD:
135 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700136 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300137 break;
138 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700139 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300140 break;
141 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700142 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300143 break;
144 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700145 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300146 break;
147 default:
148 nasm_error(ERR_NONFATAL,
149 "invalid operand size specification");
150 break;
151 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700152 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300153 /* Standard NASM compatible syntax */
154 switch ((int)tokval.t_integer) {
155 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700156 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300157 break;
158 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700159 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300160 break;
161 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700162 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300163 break;
164 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700165 op->disp_size = 8;
166 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300167 break;
168 case P_A16:
169 case P_A32:
170 case P_A64:
171 if (result->prefixes[PPS_ASIZE] &&
172 result->prefixes[PPS_ASIZE] != tokval.t_integer)
173 nasm_error(ERR_NONFATAL,
174 "conflicting address size specifications");
175 else
176 result->prefixes[PPS_ASIZE] = tokval.t_integer;
177 break;
178 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700179 op->disp_size = 16;
180 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300181 break;
182 case S_DWORD:
183 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700184 op->disp_size = 32;
185 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300186 break;
187 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700188 op->disp_size = 64;
189 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300190 break;
191 default:
192 nasm_error(ERR_NONFATAL, "invalid size specification in"
193 " effective address");
194 break;
195 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700196 }
197}
198
Jin Kyu Song72018a22013-08-05 20:46:18 -0700199/*
200 * when two or more decorators follow a register operand,
201 * consecutive decorators are parsed here.
Jin Kyu Songf9a71e02013-08-21 19:29:09 -0700202 * opmask and zeroing decorators can be placed in any order.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700203 * e.g. zmm1 {k2}{z} or zmm2 {z,k3}
204 * decorator(s) are placed at the end of an operand.
205 */
206static bool parse_braces(decoflags_t *decoflags)
207{
208 int i;
209 bool recover = false;
210
211 i = tokval.t_type;
212 do {
213 if (i == TOKEN_OPMASK) {
214 if (*decoflags & OPMASK_MASK) {
Jin Kyu Songc9486b92013-10-28 17:07:57 -0700215 nasm_error(ERR_NONFATAL, "opmask k%"PRIu64" is already set",
Jin Kyu Song72018a22013-08-05 20:46:18 -0700216 *decoflags & OPMASK_MASK);
217 *decoflags &= ~OPMASK_MASK;
218 }
219 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
220 } else if (i == TOKEN_DECORATOR) {
221 switch (tokval.t_integer) {
222 case BRC_Z:
223 /*
224 * according to AVX512 spec, only zeroing/merging decorator
225 * is supported with opmask
226 */
227 *decoflags |= GEN_Z(0);
228 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700229 default:
230 nasm_error(ERR_NONFATAL, "{%s} is not an expected decorator",
231 tokval.t_charptr);
232 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700233 }
234 } else if (i == ',' || i == TOKEN_EOS){
235 break;
236 } else {
237 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
238 " expected");
239 recover = true;
240 break;
241 }
242 i = stdscan(NULL, &tokval);
243 } while(1);
244
245 return recover;
246}
247
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700248static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700249{
250 int b, i, s; /* basereg, indexreg, scale */
251 int64_t o; /* offset */
252
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700253 b = i = -1;
254 o = s = 0;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700255
256 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
257 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
258
259 if (is_gpr && e->value == 1)
260 b = e->type; /* It can be basereg */
261 else /* No, it has to be indexreg */
262 i = e->type, s = e->value;
263 e++;
264 }
265 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
266 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
267
268 if (b != -1) /* If the first was the base, ... */
269 i = e->type, s = e->value; /* second has to be indexreg */
270
271 else if (!is_gpr || e->value != 1) {
272 /* If both want to be index */
273 nasm_error(ERR_NONFATAL,
274 "invalid effective address: two index registers");
275 return -1;
276 } else
277 b = e->type;
278 e++;
279 }
280 if (e->type != 0) { /* is there an offset? */
281 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
282 nasm_error(ERR_NONFATAL,
283 "beroset-p-603-invalid effective address");
284 return -1;
285 } else {
286 if (e->type == EXPR_UNKNOWN) {
287 op->opflags |= OPFLAG_UNKNOWN;
288 o = 0; /* doesn't matter what */
289 op->wrt = NO_SEG; /* nor this */
290 op->segment = NO_SEG; /* or this */
291 while (e->type)
292 e++; /* go to the end of the line */
293 } else {
294 if (e->type == EXPR_SIMPLE) {
295 o = e->value;
296 e++;
297 }
298 if (e->type == EXPR_WRT) {
299 op->wrt = e->value;
300 e++;
301 } else
302 op->wrt = NO_SEG;
303 /*
304 * Look for a segment base type.
305 */
306 if (e->type && e->type < EXPR_SEGBASE) {
307 nasm_error(ERR_NONFATAL,
308 "beroset-p-630-invalid effective address");
309 return -1;
310 }
311 while (e->type && e->value == 0)
312 e++;
313 if (e->type && e->value != 1) {
314 nasm_error(ERR_NONFATAL,
315 "beroset-p-637-invalid effective address");
316 return -1;
317 }
318 if (e->type) {
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700319 op->segment = e->type - EXPR_SEGBASE;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700320 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
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700344 op->basereg = b;
345 op->indexreg = i;
346 op->scale = s;
347 op->offset = o;
348 return 0;
349}
350
351static void mref_set_optype(operand *op)
352{
353 int b = op->basereg;
354 int i = op->indexreg;
355 int s = op->scale;
356
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700357 /* It is memory, but it can match any r/m operand */
358 op->type |= MEMORY_ANY;
359
360 if (b == -1 && (i == -1 || s == 0)) {
361 int is_rel = globalbits == 64 &&
362 !(op->eaflags & EAF_ABS) &&
363 ((globalrel &&
364 !(op->eaflags & EAF_FSGS)) ||
365 (op->eaflags & EAF_REL));
366
367 op->type |= is_rel ? IP_REL : MEM_OFFS;
368 }
369
370 if (i != -1) {
371 opflags_t iclass = nasm_reg_flags[i];
372
373 if (is_class(XMMREG,iclass))
374 op->type |= XMEM;
375 else if (is_class(YMMREG,iclass))
376 op->type |= YMEM;
377 else if (is_class(ZMMREG,iclass))
378 op->type |= ZMEM;
379 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700380}
381
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700382insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000383{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400384 bool insn_is_label = false;
385 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700386 int opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000387 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800388 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700389 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000390
H. Peter Anvin9c987692007-11-04 21:09:32 -0800391restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400392 first = true;
393 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000394
H. Peter Anvin76690a12002-04-30 20:52:49 +0000395 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300396 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000397 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000398
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400399 result->label = NULL; /* Assume no label */
400 result->eops = NULL; /* must do this, whatever happens */
401 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700402 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
403 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000404
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400405 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700406 if (i == TOKEN_EOS)
407 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400408
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400409 if (i != TOKEN_ID &&
410 i != TOKEN_INSN &&
411 i != TOKEN_PREFIX &&
412 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
413 nasm_error(ERR_NONFATAL,
414 "label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700415 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000416 }
417
H. Peter Anvin9c987692007-11-04 21:09:32 -0800418 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
419 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300420 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 result->label = tokval.t_charptr;
422 i = stdscan(NULL, &tokval);
423 if (i == ':') { /* skip over the optional colon */
424 i = stdscan(NULL, &tokval);
425 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700426 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427 "label alone on a line without a colon might be in error");
428 }
429 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
430 /*
431 * FIXME: location->segment could be NO_SEG, in which case
432 * it is possible we should be passing 'abs_seg'. Look into this.
433 * Work out whether that is *really* what we should be doing.
434 * Generally fix things. I think this is right as it is, but
435 * am still not certain.
436 */
437 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700438 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000440 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000441
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400442 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700443 if (i == TOKEN_EOS)
444 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000445
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400446 nasm_build_assert(P_none != 0);
447 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000448 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000449
450 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400451 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300452 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800453
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 /*
455 * Handle special case: the TIMES prefix.
456 */
457 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
458 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000459
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400461 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700463 if (!value) /* Error in evaluator */
464 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700466 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 "non-constant argument supplied to TIMES");
468 result->times = 1L;
469 } else {
470 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700471 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200472 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 value->value);
474 result->times = 0;
475 }
476 }
477 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300478 int slot = prefix_slot(tokval.t_integer);
479 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700480 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100481 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300482 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700483 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300484 nasm_error(ERR_NONFATAL,
485 "instruction has conflicting prefixes");
486 }
487 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 i = stdscan(NULL, &tokval);
489 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000490 }
491
492 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300493 int j;
494 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700495
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400496 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300497 if ((pfx = result->prefixes[j]) != P_none)
498 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400499 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700500
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700501 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 /*
503 * Instruction prefixes are present, but no actual
504 * instruction. This is allowed: at this point we
505 * invent a notional instruction of RESB 0.
506 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400507 result->opcode = I_RESB;
508 result->operands = 1;
509 result->oprs[0].type = IMMEDIATE;
510 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
512 return result;
513 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700514 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700515 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000517 }
518
519 result->opcode = tokval.t_integer;
520 result->condition = tokval.t_inttwo;
521
522 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700523 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000524 * evaluated operands, since the correct values _must_ be known
525 * on the first pass. Hence, even in pass one, we set the
526 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700527 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000528 */
Charles Crayne2581c862008-09-10 19:21:52 -0700529 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700530 critical = (pass0 < 2 ? 1 : 2);
531
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 } else
533 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000534
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700535 if (result->opcode == I_DB || result->opcode == I_DW ||
536 result->opcode == I_DD || result->opcode == I_DQ ||
537 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700538 result->opcode == I_DY || result->opcode == I_DZ ||
539 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 extop *eop, **tail = &result->eops, **fixptr;
541 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300542 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000543
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700544 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000545
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700547 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 */
549 while (1) {
550 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400551 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300553 else if (first && i == ':') {
554 insn_is_label = true;
555 goto restart_parse;
556 }
557 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 fixptr = tail;
559 eop = *tail = nasm_malloc(sizeof(extop));
560 tail = &eop->next;
561 eop->next = NULL;
562 eop->type = EOT_NOTHING;
563 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300564 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000565
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300566 /*
567 * is_comma_next() here is to distinguish this from
568 * a string used as part of an expression...
569 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700570 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400571 eop->type = EOT_DB_STRING;
572 eop->stringval = tokval.t_charptr;
573 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300575 } else if (i == TOKEN_STRFUNC) {
576 bool parens = false;
577 const char *funcname = tokval.t_charptr;
578 enum strfunc func = tokval.t_integer;
579 i = stdscan(NULL, &tokval);
580 if (i == '(') {
581 parens = true;
582 i = stdscan(NULL, &tokval);
583 }
584 if (i != TOKEN_STR) {
585 nasm_error(ERR_NONFATAL,
586 "%s must be followed by a string constant",
587 funcname);
588 eop->type = EOT_NOTHING;
589 } else {
590 eop->type = EOT_DB_STRING_FREE;
591 eop->stringlen =
592 string_transform(tokval.t_charptr, tokval.t_inttwo,
593 &eop->stringval, func);
594 if (eop->stringlen == (size_t)-1) {
595 nasm_error(ERR_NONFATAL, "invalid string for transform");
596 eop->type = EOT_NOTHING;
597 }
598 }
599 if (parens && i && i != ')') {
600 i = stdscan(NULL, &tokval);
601 if (i != ')') {
602 nasm_error(ERR_NONFATAL, "unterminated %s function",
603 funcname);
604 }
605 }
606 if (i && i != ',')
607 i = stdscan(NULL, &tokval);
608 } else if (i == '-' || i == '+') {
609 char *save = stdscan_get();
610 int token = i;
611 sign = (i == '-') ? -1 : 1;
612 i = stdscan(NULL, &tokval);
613 if (i != TOKEN_FLOAT) {
614 stdscan_set(save);
615 i = tokval.t_type = token;
616 goto is_expression;
617 } else {
618 goto is_float;
619 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700620 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300621is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300622 eop->type = EOT_DB_STRING;
623 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300624
625 eop->stringlen = idata_bytes(result->opcode);
626 if (eop->stringlen > 16) {
627 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700628 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300629 eop->stringlen = 0;
630 } else if (eop->stringlen < 1) {
631 nasm_error(ERR_NONFATAL, "floating-point constant"
632 " encountered in unknown instruction");
633 /*
634 * fix suggested by Pedro Gimeno... original line was:
635 * eop->type = EOT_NOTHING;
636 */
637 eop->stringlen = 0;
638 }
639
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300640 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
641 tail = &eop->next;
642 *fixptr = eop;
643 eop->stringval = (char *)eop + sizeof(extop);
644 if (!eop->stringlen ||
645 !float_const(tokval.t_charptr, sign,
646 (uint8_t *)eop->stringval,
647 eop->stringlen, nasm_error))
648 eop->type = EOT_NOTHING;
649 i = stdscan(NULL, &tokval); /* eat the comma */
650 } else {
651 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700653
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300654is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700656 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700658 if (!value) /* Error in evaluator */
659 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 if (is_unknown(value)) {
661 eop->type = EOT_DB_NUMBER;
662 eop->offset = 0; /* doesn't matter what we put */
663 eop->segment = eop->wrt = NO_SEG; /* likewise */
664 } else if (is_reloc(value)) {
665 eop->type = EOT_DB_NUMBER;
666 eop->offset = reloc_value(value);
667 eop->segment = reloc_seg(value);
668 eop->wrt = reloc_wrt(value);
669 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700670 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 "operand %d: expression is not simple"
672 " or relocatable", oper_num);
673 }
674 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000675
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676 /*
677 * We're about to call stdscan(), which will eat the
678 * comma that we're currently sitting on between
679 * arguments. However, we'd better check first that it
680 * _is_ a comma.
681 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400682 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 break;
684 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700685 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400686 oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700687 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 }
689 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000690
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 if (result->opcode == I_INCBIN) {
692 /*
693 * Correct syntax for INCBIN is that there should be
694 * one string operand, followed by one or two numeric
695 * operands.
696 */
697 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700698 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 else if (result->eops->next &&
700 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200701 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400702 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 else if (result->eops->next && result->eops->next->next &&
704 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200705 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400706 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 else if (result->eops->next && result->eops->next->next &&
708 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700709 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400710 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000711 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 return result;
713 /*
714 * If we reach here, one of the above errors happened.
715 * Throw the instruction away.
716 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700717 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700719 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 "no operand for data declaration");
721 else
722 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723
H. Peter Anvine2c80182005-01-15 22:15:51 +0000724 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000725 }
726
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400727 /*
728 * Now we begin to parse the operands. There may be up to four
729 * of these, separated by commas, and terminated by a zero token.
730 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000731
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700732 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
733 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300734 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700735 bool mref; /* is this going to be a memory ref? */
736 bool bracket; /* is it a [] mref, or a & mref? */
737 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700739 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000740
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700741 op->disp_size = 0; /* have to zero this whatever */
742 op->eaflags = 0; /* and this */
743 op->opflags = 0;
744 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000745
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400747 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300749 else if (first && i == ':') {
750 insn_is_label = true;
751 goto restart_parse;
752 }
753 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700754 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 while (i == TOKEN_SPECIAL) { /* size specifiers */
756 switch ((int)tokval.t_integer) {
757 case S_BYTE:
758 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700759 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 setsize = 1;
761 break;
762 case S_WORD:
763 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700764 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 setsize = 1;
766 break;
767 case S_DWORD:
768 case S_LONG:
769 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700770 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 setsize = 1;
772 break;
773 case S_QWORD:
774 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700775 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 setsize = 1;
777 break;
778 case S_TWORD:
779 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700780 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 setsize = 1;
782 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700783 case S_OWORD:
784 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700785 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700786 setsize = 1;
787 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700788 case S_YWORD:
789 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700790 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700791 setsize = 1;
792 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700793 case S_ZWORD:
794 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700795 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700796 setsize = 1;
797 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700799 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 break;
801 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700802 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 break;
804 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700805 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 break;
807 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700808 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 break;
810 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700811 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 break;
813 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700814 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 }
816 i = stdscan(NULL, &tokval);
817 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700820 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700822 i = stdscan(NULL, &tokval); /* then skip the colon */
823 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700824 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700825 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700827 /* when a comma follows an opening bracket - [ , eax*4] */
828 if (i == ',') {
829 /* treat as if there is a zero displacement virtually */
830 tokval.t_type = TOKEN_NUM;
831 tokval.t_integer = 0;
832 stdscan_set(stdscan_get() - 1); /* rewind the comma */
833 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700835 mref = false;
836 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000838
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700839 if ((op->type & FAR) && !mref &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700841 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000843
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700845 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700846 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700848 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700849 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700851 if (!value) /* Error in evaluator */
852 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 if (i == ':' && mref) { /* it was seg:offset */
854 /*
855 * Process the segment override.
856 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400857 if (value[1].type != 0 ||
858 value->value != 1 ||
859 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700860 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700861 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700862 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700863 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000864 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300865 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400866 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700867 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300868 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000869
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700871 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700872 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 i = stdscan(NULL, &tokval);
874 }
875 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700876 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700877 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700879 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700880 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 }
882 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700883 if (!value) /* Error in evaluator */
884 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200886
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700887 mib = false;
888 if (mref && bracket && i == ',') {
889 /* [seg:base+offset,index*scale] syntax (mib) */
890
891 operand o1, o2; /* Partial operands */
892
893 if (parse_mref(&o1, value))
894 goto fail;
895
896 i = stdscan(NULL, &tokval); /* Eat comma */
897 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
898 critical, nasm_error, &hints);
899 i = tokval.t_type;
900
901 if (parse_mref(&o2, value))
902 goto fail;
903
904 if (o2.basereg != -1 && o2.indexreg == -1) {
905 o2.indexreg = o2.basereg;
906 o2.scale = 1;
907 o2.basereg = -1;
908 }
909
910 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
911 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
912 nasm_error(ERR_NONFATAL, "invalid mib expression");
913 goto fail;
914 }
915
916 op->basereg = o1.basereg;
917 op->indexreg = o2.indexreg;
918 op->scale = o2.scale;
919 op->offset = o1.offset;
920 op->segment = o1.segment;
921 op->wrt = o1.wrt;
922
923 if (op->basereg != -1) {
924 op->hintbase = op->basereg;
925 op->hinttype = EAH_MAKEBASE;
926 } else if (op->indexreg != -1) {
927 op->hintbase = op->indexreg;
928 op->hinttype = EAH_NOTBASE;
929 } else {
930 op->hintbase = -1;
931 op->hinttype = EAH_NOHINT;
932 }
933
934 mib = true;
935 }
936
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700937 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 if (mref && bracket) { /* find ] at the end */
939 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700940 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200941 recover = true;
942 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700944 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700945 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700946 * according to AVX512 spec, broacast or opmask decorator
947 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700948 */
949 if (tokval.t_flag & TFLAG_BRDCAST) {
Jin Kyu Song25c22122013-10-30 03:12:45 -0700950 brace_flags |= GEN_BRDCAST(0) |
951 VAL_BRNUM(tokval.t_integer - BRC_1TO8);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700952 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700953 } else if (i == TOKEN_OPMASK) {
954 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
955 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700956 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700957 nasm_error(ERR_NONFATAL, "broadcast or opmask "
958 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700959 recover = true;
960 }
961 }
962
Victor van den Elzen02846d32009-06-23 03:47:07 +0200963 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700964 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200965 recover = true;
966 }
967 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700969 if (i != 0 && i != ',' && i != ':' &&
970 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
971 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
972 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200973 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700975 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700976 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
977 /* parse opmask (and zeroing) after an operand */
978 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 }
980 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200981 if (recover) {
982 do { /* error recovery */
983 i = stdscan(NULL, &tokval);
984 } while (i != 0 && i != ',');
985 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000986
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300987 /*
988 * now convert the exprs returned from evaluate()
989 * into operand descriptions...
990 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700991 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700994 /* A mib reference was fully parsed already */
995 if (!mib) {
996 if (parse_mref(op, value))
997 goto fail;
998 op->hintbase = hints.base;
999 op->hinttype = hints.type;
1000 }
1001 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001004 op->type |= IMMEDIATE;
1005 op->opflags |= OPFLAG_UNKNOWN;
1006 op->offset = 0; /* don't care */
1007 op->segment = NO_SEG; /* don't care again */
1008 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001009
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001010 if(optimizing >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001011 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001012 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001013 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001014 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001016 op->type |= IMMEDIATE;
1017 op->offset = reloc_value(value);
1018 op->segment = reloc_seg(value);
1019 op->wrt = reloc_wrt(value);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001022 uint64_t n = reloc_value(value);
1023 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001024 op->type |= UNITY;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 if (optimizing >= 0 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001026 !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001027 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001028 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001029 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001030 op->type |= SBYTEWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001031 if (n <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 op->type |= UDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001033 if (n + 0x80000000 <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001034 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 }
1036 }
Jin Kyu Song72018a22013-08-05 20:46:18 -07001037 } else if(value->type == EXPR_RDSAE) {
1038 /*
1039 * it's not an operand but a rounding or SAE decorator.
1040 * put the decorator information in the (opflag_t) type field
1041 * of previous operand.
1042 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001043 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001044 switch (value->value) {
1045 case BRC_RN:
1046 case BRC_RU:
1047 case BRC_RD:
1048 case BRC_RZ:
1049 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001050 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001051 result->evex_rm = value->value;
1052 break;
1053 default:
1054 nasm_error(ERR_NONFATAL, "invalid decorator");
1055 break;
1056 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001058 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001059
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001061 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001062 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 /*
1066 * check that its only 1 register, not an expression...
1067 */
1068 for (i = 1; value[i].type; i++)
1069 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001070 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001071 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001075 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 /*
1077 * we want to produce a warning iff the specified size
1078 * is different from the register size
1079 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001080 rs = op->type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001082 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001084 op->type &= TO;
1085 op->type |= REGISTER;
1086 op->type |= nasm_reg_flags[value->type];
1087 op->decoflags |= brace_flags;
1088 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001090 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001091 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 "register size specification ignored");
1093 }
1094 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001095
1096 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001097 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1098 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001099 }
1100
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001101 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001102
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001103 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001104 while (opnum < MAX_OPERANDS)
1105 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001106
1107 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001108 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001109 */
1110 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 case I_RESW:
1112 result->opcode = I_RESB;
1113 result->oprs[0].offset *= 2;
1114 break;
1115 case I_RESD:
1116 result->opcode = I_RESB;
1117 result->oprs[0].offset *= 4;
1118 break;
1119 case I_RESQ:
1120 result->opcode = I_RESB;
1121 result->oprs[0].offset *= 8;
1122 break;
1123 case I_REST:
1124 result->opcode = I_RESB;
1125 result->oprs[0].offset *= 10;
1126 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001127 case I_RESO:
1128 result->opcode = I_RESB;
1129 result->oprs[0].offset *= 16;
1130 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001131 case I_RESY:
1132 result->opcode = I_RESB;
1133 result->oprs[0].offset *= 32;
1134 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001135 case I_RESZ:
1136 result->opcode = I_RESB;
1137 result->oprs[0].offset *= 64;
1138 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001139 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001140 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001141 }
1142
1143 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001144
1145fail:
1146 result->opcode = I_none;
1147 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001148}
1149
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001151{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001152 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001153 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001154 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001155
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001156 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001158 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001159
H. Peter Anvin76690a12002-04-30 20:52:49 +00001160 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001161}
1162
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001164{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001165 extop *e;
1166
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001167 while ((e = i->eops)) {
1168 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001169 if (e->type == EOT_DB_STRING_FREE)
1170 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001172 }
1173}