blob: ba994b41ace58a996bf7cb394837b0fbc6dcd31e [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 Anvin00444ae2009-07-18 18:49:55 -0700245insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000246{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400247 bool insn_is_label = false;
248 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700249 int opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000250 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800251 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700252 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000253
H. Peter Anvin9c987692007-11-04 21:09:32 -0800254restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400255 first = true;
256 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000257
H. Peter Anvin76690a12002-04-30 20:52:49 +0000258 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300259 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000260 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000261
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400262 result->label = NULL; /* Assume no label */
263 result->eops = NULL; /* must do this, whatever happens */
264 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700265 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
266 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000267
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400268 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700269 if (i == TOKEN_EOS)
270 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400271
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400272 if (i != TOKEN_ID &&
273 i != TOKEN_INSN &&
274 i != TOKEN_PREFIX &&
275 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
276 nasm_error(ERR_NONFATAL,
277 "label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700278 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000279 }
280
H. Peter Anvin9c987692007-11-04 21:09:32 -0800281 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
282 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300283 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284 result->label = tokval.t_charptr;
285 i = stdscan(NULL, &tokval);
286 if (i == ':') { /* skip over the optional colon */
287 i = stdscan(NULL, &tokval);
288 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700289 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000290 "label alone on a line without a colon might be in error");
291 }
292 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
293 /*
294 * FIXME: location->segment could be NO_SEG, in which case
295 * it is possible we should be passing 'abs_seg'. Look into this.
296 * Work out whether that is *really* what we should be doing.
297 * Generally fix things. I think this is right as it is, but
298 * am still not certain.
299 */
300 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700301 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000302 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000303 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000304
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400305 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700306 if (i == TOKEN_EOS)
307 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000308
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400309 nasm_build_assert(P_none != 0);
310 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000312
313 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400314 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300315 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800316
H. Peter Anvine2c80182005-01-15 22:15:51 +0000317 /*
318 * Handle special case: the TIMES prefix.
319 */
320 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
321 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000322
H. Peter Anvine2c80182005-01-15 22:15:51 +0000323 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400324 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000325 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700326 if (!value) /* Error in evaluator */
327 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700329 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330 "non-constant argument supplied to TIMES");
331 result->times = 1L;
332 } else {
333 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700334 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200335 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000336 value->value);
337 result->times = 0;
338 }
339 }
340 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300341 int slot = prefix_slot(tokval.t_integer);
342 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700343 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100344 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300345 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700346 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300347 nasm_error(ERR_NONFATAL,
348 "instruction has conflicting prefixes");
349 }
350 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 i = stdscan(NULL, &tokval);
352 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353 }
354
355 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300356 int j;
357 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700358
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400359 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300360 if ((pfx = result->prefixes[j]) != P_none)
361 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400362 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700363
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700364 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365 /*
366 * Instruction prefixes are present, but no actual
367 * instruction. This is allowed: at this point we
368 * invent a notional instruction of RESB 0.
369 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400370 result->opcode = I_RESB;
371 result->operands = 1;
372 result->oprs[0].type = IMMEDIATE;
373 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
375 return result;
376 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700377 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700378 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000380 }
381
382 result->opcode = tokval.t_integer;
383 result->condition = tokval.t_inttwo;
384
385 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700386 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000387 * evaluated operands, since the correct values _must_ be known
388 * on the first pass. Hence, even in pass one, we set the
389 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700390 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000391 */
Charles Crayne2581c862008-09-10 19:21:52 -0700392 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700393 critical = (pass0 < 2 ? 1 : 2);
394
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 } else
396 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000397
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700398 if (result->opcode == I_DB || result->opcode == I_DW ||
399 result->opcode == I_DD || result->opcode == I_DQ ||
400 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700401 result->opcode == I_DY || result->opcode == I_DZ ||
402 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 extop *eop, **tail = &result->eops, **fixptr;
404 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300405 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000406
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700407 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000408
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700410 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 */
412 while (1) {
413 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400414 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300416 else if (first && i == ':') {
417 insn_is_label = true;
418 goto restart_parse;
419 }
420 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 fixptr = tail;
422 eop = *tail = nasm_malloc(sizeof(extop));
423 tail = &eop->next;
424 eop->next = NULL;
425 eop->type = EOT_NOTHING;
426 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300427 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000428
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300429 /*
430 * is_comma_next() here is to distinguish this from
431 * a string used as part of an expression...
432 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700433 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400434 eop->type = EOT_DB_STRING;
435 eop->stringval = tokval.t_charptr;
436 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000437 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300438 } else if (i == TOKEN_STRFUNC) {
439 bool parens = false;
440 const char *funcname = tokval.t_charptr;
441 enum strfunc func = tokval.t_integer;
442 i = stdscan(NULL, &tokval);
443 if (i == '(') {
444 parens = true;
445 i = stdscan(NULL, &tokval);
446 }
447 if (i != TOKEN_STR) {
448 nasm_error(ERR_NONFATAL,
449 "%s must be followed by a string constant",
450 funcname);
451 eop->type = EOT_NOTHING;
452 } else {
453 eop->type = EOT_DB_STRING_FREE;
454 eop->stringlen =
455 string_transform(tokval.t_charptr, tokval.t_inttwo,
456 &eop->stringval, func);
457 if (eop->stringlen == (size_t)-1) {
458 nasm_error(ERR_NONFATAL, "invalid string for transform");
459 eop->type = EOT_NOTHING;
460 }
461 }
462 if (parens && i && i != ')') {
463 i = stdscan(NULL, &tokval);
464 if (i != ')') {
465 nasm_error(ERR_NONFATAL, "unterminated %s function",
466 funcname);
467 }
468 }
469 if (i && i != ',')
470 i = stdscan(NULL, &tokval);
471 } else if (i == '-' || i == '+') {
472 char *save = stdscan_get();
473 int token = i;
474 sign = (i == '-') ? -1 : 1;
475 i = stdscan(NULL, &tokval);
476 if (i != TOKEN_FLOAT) {
477 stdscan_set(save);
478 i = tokval.t_type = token;
479 goto is_expression;
480 } else {
481 goto is_float;
482 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700483 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300484is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300485 eop->type = EOT_DB_STRING;
486 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300487
488 eop->stringlen = idata_bytes(result->opcode);
489 if (eop->stringlen > 16) {
490 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700491 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300492 eop->stringlen = 0;
493 } else if (eop->stringlen < 1) {
494 nasm_error(ERR_NONFATAL, "floating-point constant"
495 " encountered in unknown instruction");
496 /*
497 * fix suggested by Pedro Gimeno... original line was:
498 * eop->type = EOT_NOTHING;
499 */
500 eop->stringlen = 0;
501 }
502
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300503 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
504 tail = &eop->next;
505 *fixptr = eop;
506 eop->stringval = (char *)eop + sizeof(extop);
507 if (!eop->stringlen ||
508 !float_const(tokval.t_charptr, sign,
509 (uint8_t *)eop->stringval,
510 eop->stringlen, nasm_error))
511 eop->type = EOT_NOTHING;
512 i = stdscan(NULL, &tokval); /* eat the comma */
513 } else {
514 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700516
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300517is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700519 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700521 if (!value) /* Error in evaluator */
522 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 if (is_unknown(value)) {
524 eop->type = EOT_DB_NUMBER;
525 eop->offset = 0; /* doesn't matter what we put */
526 eop->segment = eop->wrt = NO_SEG; /* likewise */
527 } else if (is_reloc(value)) {
528 eop->type = EOT_DB_NUMBER;
529 eop->offset = reloc_value(value);
530 eop->segment = reloc_seg(value);
531 eop->wrt = reloc_wrt(value);
532 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700533 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 "operand %d: expression is not simple"
535 " or relocatable", oper_num);
536 }
537 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000538
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 /*
540 * We're about to call stdscan(), which will eat the
541 * comma that we're currently sitting on between
542 * arguments. However, we'd better check first that it
543 * _is_ a comma.
544 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400545 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 break;
547 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700548 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400549 oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700550 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 }
552 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000553
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 if (result->opcode == I_INCBIN) {
555 /*
556 * Correct syntax for INCBIN is that there should be
557 * one string operand, followed by one or two numeric
558 * operands.
559 */
560 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700561 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 else if (result->eops->next &&
563 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200564 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400565 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 else if (result->eops->next && result->eops->next->next &&
567 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200568 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400569 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 else if (result->eops->next && result->eops->next->next &&
571 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700572 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400573 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000574 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 return result;
576 /*
577 * If we reach here, one of the above errors happened.
578 * Throw the instruction away.
579 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700580 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700582 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 "no operand for data declaration");
584 else
585 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000588 }
589
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400590 /*
591 * Now we begin to parse the operands. There may be up to four
592 * of these, separated by commas, and terminated by a zero token.
593 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000594
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700595 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
596 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300597 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 int mref; /* is this going to be a memory ref? */
599 int bracket; /* is it a [] mref, or a & mref? */
600 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700601 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000602
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700603 op->disp_size = 0; /* have to zero this whatever */
604 op->eaflags = 0; /* and this */
605 op->opflags = 0;
606 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000607
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400609 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300611 else if (first && i == ':') {
612 insn_is_label = true;
613 goto restart_parse;
614 }
615 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700616 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 while (i == TOKEN_SPECIAL) { /* size specifiers */
618 switch ((int)tokval.t_integer) {
619 case S_BYTE:
620 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700621 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 setsize = 1;
623 break;
624 case S_WORD:
625 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700626 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 setsize = 1;
628 break;
629 case S_DWORD:
630 case S_LONG:
631 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700632 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 setsize = 1;
634 break;
635 case S_QWORD:
636 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700637 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 setsize = 1;
639 break;
640 case S_TWORD:
641 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700642 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 setsize = 1;
644 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700645 case S_OWORD:
646 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700647 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700648 setsize = 1;
649 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700650 case S_YWORD:
651 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700652 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700653 setsize = 1;
654 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700655 case S_ZWORD:
656 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700657 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700658 setsize = 1;
659 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700661 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000662 break;
663 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700664 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000665 break;
666 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700667 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 break;
669 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700670 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 break;
672 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700673 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 break;
675 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700676 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 }
678 i = stdscan(NULL, &tokval);
679 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000680
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700682 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700684 i = stdscan(NULL, &tokval); /* then skip the colon */
685 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700686 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700687 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 }
689 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700690 mref = false;
691 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000693
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700694 if ((op->type & FAR) && !mref &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700696 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000698
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700700 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700701 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700703 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700704 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700706 if (!value) /* Error in evaluator */
707 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 if (i == ':' && mref) { /* it was seg:offset */
709 /*
710 * Process the segment override.
711 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400712 if (value[1].type != 0 ||
713 value->value != 1 ||
714 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700715 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700716 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700717 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700718 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000719 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300720 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400721 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700722 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300723 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000724
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700726 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700727 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 i = stdscan(NULL, &tokval);
729 }
730 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700731 &op->opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700732 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700734 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700735 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 }
737 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700738 if (!value) /* Error in evaluator */
739 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200741
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700742 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 if (mref && bracket) { /* find ] at the end */
744 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700745 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200746 recover = true;
747 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700749 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700750 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700751 * according to AVX512 spec, broacast or opmask decorator
752 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700753 */
754 if (tokval.t_flag & TFLAG_BRDCAST) {
755 brace_flags |= GEN_BRDCAST(0);
756 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700757 } else if (i == TOKEN_OPMASK) {
758 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
759 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700760 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700761 nasm_error(ERR_NONFATAL, "broadcast or opmask "
762 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700763 recover = true;
764 }
765 }
766
Victor van den Elzen02846d32009-06-23 03:47:07 +0200767 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700768 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200769 recover = true;
770 }
771 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700773 if (i != 0 && i != ',' && i != ':' &&
774 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
775 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
776 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200777 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700779 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700780 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
781 /* parse opmask (and zeroing) after an operand */
782 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 }
784 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200785 if (recover) {
786 do { /* error recovery */
787 i = stdscan(NULL, &tokval);
788 } while (i != 0 && i != ',');
789 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000790
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300791 /*
792 * now convert the exprs returned from evaluate()
793 * into operand descriptions...
794 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000795
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 if (mref) { /* it's a memory reference */
797 expr *e = value;
798 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300799 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 b = i = -1, o = s = 0;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700802 op->hintbase = hints.base;
803 op->hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000804
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700806 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
807
808 if (is_gpr && e->value == 1)
809 b = e->type; /* It can be basereg */
810 else /* No, it has to be indexreg */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 i = e->type, s = e->value;
812 e++;
813 }
814 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700815 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
816
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 if (b != -1) /* If the first was the base, ... */
818 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000819
H. Peter Anvine20ca022013-07-19 17:06:08 -0700820 else if (!is_gpr || e->value != 1) {
821 /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700822 nasm_error(ERR_NONFATAL,
H. Peter Anvine20ca022013-07-19 17:06:08 -0700823 "invalid effective address: two index registers");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700824 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 } else
826 b = e->type;
827 e++;
828 }
829 if (e->type != 0) { /* is there an offset? */
830 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700831 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 "beroset-p-603-invalid effective address");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700833 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 } else {
835 if (e->type == EXPR_UNKNOWN) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700836 op->opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 o = 0; /* doesn't matter what */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700838 op->wrt = NO_SEG; /* nor this */
839 op->segment = NO_SEG; /* or this */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 while (e->type)
841 e++; /* go to the end of the line */
842 } else {
843 if (e->type == EXPR_SIMPLE) {
844 o = e->value;
845 e++;
846 }
847 if (e->type == EXPR_WRT) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700848 op->wrt = e->value;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 e++;
850 } else
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700851 op->wrt = NO_SEG;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 /*
853 * Look for a segment base type.
854 */
855 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700856 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 "beroset-p-630-invalid effective address");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700858 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 }
860 while (e->type && e->value == 0)
861 e++;
862 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700863 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 "beroset-p-637-invalid effective address");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700865 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 }
867 if (e->type) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700868 op->segment =
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 e->type - EXPR_SEGBASE;
870 e++;
871 } else
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700872 op->segment = NO_SEG;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 while (e->type && e->value == 0)
874 e++;
875 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700876 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000877 "beroset-p-650-invalid effective address");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700878 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 }
880 }
881 }
882 } else {
883 o = 0;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700884 op->wrt = NO_SEG;
885 op->segment = NO_SEG;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000887
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700889 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 "beroset-p-663-invalid effective address");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700891 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000893
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300894 /* It is memory, but it can match any r/m operand */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700895 op->type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000896
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300897 if (b == -1 && (i == -1 || s == 0)) {
898 int is_rel = globalbits == 64 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700899 !(op->eaflags & EAF_ABS) &&
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300900 ((globalrel &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700901 !(op->eaflags & EAF_FSGS)) ||
902 (op->eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000903
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700904 op->type |= is_rel ? IP_REL : MEM_OFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300905 }
H. Peter Anvine20ca022013-07-19 17:06:08 -0700906
907 if (i != -1) {
908 opflags_t iclass = nasm_reg_flags[i];
909
910 if (is_class(XMMREG,iclass))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700911 op->type |= XMEM;
H. Peter Anvine20ca022013-07-19 17:06:08 -0700912 else if (is_class(YMMREG,iclass))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700913 op->type |= YMEM;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700914 else if (is_class(ZMMREG,iclass))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700915 op->type |= ZMEM;
H. Peter Anvine20ca022013-07-19 17:06:08 -0700916 }
917
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700918 op->basereg = b;
919 op->indexreg = i;
920 op->scale = s;
921 op->offset = o;
922 op->decoflags |= brace_flags;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700925 op->type |= IMMEDIATE;
926 op->opflags |= OPFLAG_UNKNOWN;
927 op->offset = 0; /* don't care */
928 op->segment = NO_SEG; /* don't care again */
929 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100930
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700931 if(optimizing >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300932 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700933 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400934 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300935 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700937 op->type |= IMMEDIATE;
938 op->offset = reloc_value(value);
939 op->segment = reloc_seg(value);
940 op->wrt = reloc_wrt(value);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400941
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400943 uint64_t n = reloc_value(value);
944 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700945 op->type |= UNITY;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 if (optimizing >= 0 &&
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700947 !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400948 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700949 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400950 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700951 op->type |= SBYTEWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400952 if (n <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700953 op->type |= UDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400954 if (n + 0x80000000 <= 0xFFFFFFFF)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700955 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 }
957 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700958 } else if(value->type == EXPR_RDSAE) {
959 /*
960 * it's not an operand but a rounding or SAE decorator.
961 * put the decorator information in the (opflag_t) type field
962 * of previous operand.
963 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700964 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700965 switch (value->value) {
966 case BRC_RN:
967 case BRC_RU:
968 case BRC_RD:
969 case BRC_RZ:
970 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700971 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700972 result->evex_rm = value->value;
973 break;
974 default:
975 nasm_error(ERR_NONFATAL, "invalid decorator");
976 break;
977 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +0400979 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000980
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700982 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700983 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000985
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 /*
987 * check that its only 1 register, not an expression...
988 */
989 for (i = 1; value[i].type; i++)
990 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700991 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700992 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000994
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700996 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 /*
998 * we want to produce a warning iff the specified size
999 * is different from the register size
1000 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001001 rs = op->type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001003 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001005 op->type &= TO;
1006 op->type |= REGISTER;
1007 op->type |= nasm_reg_flags[value->type];
1008 op->decoflags |= brace_flags;
1009 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001011 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001012 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 "register size specification ignored");
1014 }
1015 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001016
1017 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001018 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1019 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001020 }
1021
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001022 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001023
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001024 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001025 while (opnum < MAX_OPERANDS)
1026 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001027
1028 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001029 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001030 */
1031 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 case I_RESW:
1033 result->opcode = I_RESB;
1034 result->oprs[0].offset *= 2;
1035 break;
1036 case I_RESD:
1037 result->opcode = I_RESB;
1038 result->oprs[0].offset *= 4;
1039 break;
1040 case I_RESQ:
1041 result->opcode = I_RESB;
1042 result->oprs[0].offset *= 8;
1043 break;
1044 case I_REST:
1045 result->opcode = I_RESB;
1046 result->oprs[0].offset *= 10;
1047 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001048 case I_RESO:
1049 result->opcode = I_RESB;
1050 result->oprs[0].offset *= 16;
1051 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001052 case I_RESY:
1053 result->opcode = I_RESB;
1054 result->oprs[0].offset *= 32;
1055 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001056 case I_RESZ:
1057 result->opcode = I_RESB;
1058 result->oprs[0].offset *= 64;
1059 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001060 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001061 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001062 }
1063
1064 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001065
1066fail:
1067 result->opcode = I_none;
1068 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001069}
1070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001072{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001073 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001074 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001075 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001076
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001077 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001079 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001080
H. Peter Anvin76690a12002-04-30 20:52:49 +00001081 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001082}
1083
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001085{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001086 extop *e;
1087
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001088 while ((e = i->eops)) {
1089 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001090 if (e->type == EOT_DB_STRING_FREE)
1091 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001093 }
1094}