blob: f7139f328916a9b2a2c6a2b7128862f631dac829 [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
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300109static void process_size_override(insn *result, int operand)
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:
126 result->oprs[operand].type |= BITS8;
127 break;
128 case S_WORD:
129 result->oprs[operand].type |= BITS16;
130 break;
131 case S_DWORD:
132 case S_LONG:
133 result->oprs[operand].type |= BITS32;
134 break;
135 case S_QWORD:
136 result->oprs[operand].type |= BITS64;
137 break;
138 case S_TWORD:
139 result->oprs[operand].type |= BITS80;
140 break;
141 case S_OWORD:
142 result->oprs[operand].type |= BITS128;
143 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:
153 result->oprs[operand].eaflags |= EAF_TIMESTWO;
154 break;
155 case S_REL:
156 result->oprs[operand].eaflags |= EAF_REL;
157 break;
158 case S_ABS:
159 result->oprs[operand].eaflags |= EAF_ABS;
160 break;
161 case S_BYTE:
162 result->oprs[operand].disp_size = 8;
163 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
164 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:
176 result->oprs[operand].disp_size = 16;
177 result->oprs[operand].eaflags |= EAF_WORDOFFS;
178 break;
179 case S_DWORD:
180 case S_LONG:
181 result->oprs[operand].disp_size = 32;
182 result->oprs[operand].eaflags |= EAF_WORDOFFS;
183 break;
184 case S_QWORD:
185 result->oprs[operand].disp_size = 64;
186 result->oprs[operand].eaflags |= EAF_WORDOFFS;
187 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.
199 * the order of decorators does not matter.
200 * 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;
226 }
227 } else if (i == ',' || i == TOKEN_EOS){
228 break;
229 } else {
230 nasm_error(ERR_NONFATAL, "only a series of valid decorators"
231 " expected");
232 recover = true;
233 break;
234 }
235 i = stdscan(NULL, &tokval);
236 } while(1);
237
238 return recover;
239}
240
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700241insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000242{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400243 bool insn_is_label = false;
244 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000245 int operand;
246 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800247 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700248 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000249
H. Peter Anvin9c987692007-11-04 21:09:32 -0800250restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400251 first = true;
252 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000253
H. Peter Anvin76690a12002-04-30 20:52:49 +0000254 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300255 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000256 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000257
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400258 result->label = NULL; /* Assume no label */
259 result->eops = NULL; /* must do this, whatever happens */
260 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000261
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400262 /* Ignore blank lines */
263 if (i == TOKEN_EOS) {
264 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000265 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000266 }
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400267
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400268 if (i != TOKEN_ID &&
269 i != TOKEN_INSN &&
270 i != TOKEN_PREFIX &&
271 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
272 nasm_error(ERR_NONFATAL,
273 "label or instruction expected at start of line");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400274 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000275 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000276 }
277
H. Peter Anvin9c987692007-11-04 21:09:32 -0800278 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
279 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300280 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281 result->label = tokval.t_charptr;
282 i = stdscan(NULL, &tokval);
283 if (i == ':') { /* skip over the optional colon */
284 i = stdscan(NULL, &tokval);
285 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700286 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287 "label alone on a line without a colon might be in error");
288 }
289 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
290 /*
291 * FIXME: location->segment could be NO_SEG, in which case
292 * it is possible we should be passing 'abs_seg'. Look into this.
293 * Work out whether that is *really* what we should be doing.
294 * Generally fix things. I think this is right as it is, but
295 * am still not certain.
296 */
297 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700298 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000299 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000300 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000301
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400302 /* Just a label here */
303 if (i == TOKEN_EOS) {
304 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000306 }
307
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400308 nasm_build_assert(P_none != 0);
309 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000311
312 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400313 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300314 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800315
H. Peter Anvine2c80182005-01-15 22:15:51 +0000316 /*
317 * Handle special case: the TIMES prefix.
318 */
319 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
320 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000321
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400323 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000324 i = tokval.t_type;
325 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400326 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327 return result; /* ignore this instruction */
328 }
329 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700330 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000331 "non-constant argument supplied to TIMES");
332 result->times = 1L;
333 } else {
334 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700335 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200336 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000337 value->value);
338 result->times = 0;
339 }
340 }
341 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300342 int slot = prefix_slot(tokval.t_integer);
343 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700344 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100345 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300346 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700347 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300348 nasm_error(ERR_NONFATAL,
349 "instruction has conflicting prefixes");
350 }
351 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000352 i = stdscan(NULL, &tokval);
353 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000354 }
355
356 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300357 int j;
358 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700359
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400360 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300361 if ((pfx = result->prefixes[j]) != P_none)
362 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400363 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700364
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700365 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 /*
367 * Instruction prefixes are present, but no actual
368 * instruction. This is allowed: at this point we
369 * invent a notional instruction of RESB 0.
370 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400371 result->opcode = I_RESB;
372 result->operands = 1;
373 result->oprs[0].type = IMMEDIATE;
374 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
376 return result;
377 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700378 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400379 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 return result;
381 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000382 }
383
384 result->opcode = tokval.t_integer;
385 result->condition = tokval.t_inttwo;
386
387 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700388 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000389 * evaluated operands, since the correct values _must_ be known
390 * on the first pass. Hence, even in pass one, we set the
391 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700392 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000393 */
Charles Crayne2581c862008-09-10 19:21:52 -0700394 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700395 critical = (pass0 < 2 ? 1 : 2);
396
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 } else
398 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000399
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700400 if (result->opcode == I_DB || result->opcode == I_DW ||
401 result->opcode == I_DD || result->opcode == I_DQ ||
402 result->opcode == I_DT || result->opcode == I_DO ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300403 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 extop *eop, **tail = &result->eops, **fixptr;
405 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300406 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000407
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700408 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000409
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700411 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 */
413 while (1) {
414 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400415 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300417 else if (first && i == ':') {
418 insn_is_label = true;
419 goto restart_parse;
420 }
421 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 fixptr = tail;
423 eop = *tail = nasm_malloc(sizeof(extop));
424 tail = &eop->next;
425 eop->next = NULL;
426 eop->type = EOT_NOTHING;
427 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300428 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000429
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300430 /*
431 * is_comma_next() here is to distinguish this from
432 * a string used as part of an expression...
433 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700434 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400435 eop->type = EOT_DB_STRING;
436 eop->stringval = tokval.t_charptr;
437 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300439 } else if (i == TOKEN_STRFUNC) {
440 bool parens = false;
441 const char *funcname = tokval.t_charptr;
442 enum strfunc func = tokval.t_integer;
443 i = stdscan(NULL, &tokval);
444 if (i == '(') {
445 parens = true;
446 i = stdscan(NULL, &tokval);
447 }
448 if (i != TOKEN_STR) {
449 nasm_error(ERR_NONFATAL,
450 "%s must be followed by a string constant",
451 funcname);
452 eop->type = EOT_NOTHING;
453 } else {
454 eop->type = EOT_DB_STRING_FREE;
455 eop->stringlen =
456 string_transform(tokval.t_charptr, tokval.t_inttwo,
457 &eop->stringval, func);
458 if (eop->stringlen == (size_t)-1) {
459 nasm_error(ERR_NONFATAL, "invalid string for transform");
460 eop->type = EOT_NOTHING;
461 }
462 }
463 if (parens && i && i != ')') {
464 i = stdscan(NULL, &tokval);
465 if (i != ')') {
466 nasm_error(ERR_NONFATAL, "unterminated %s function",
467 funcname);
468 }
469 }
470 if (i && i != ',')
471 i = stdscan(NULL, &tokval);
472 } else if (i == '-' || i == '+') {
473 char *save = stdscan_get();
474 int token = i;
475 sign = (i == '-') ? -1 : 1;
476 i = stdscan(NULL, &tokval);
477 if (i != TOKEN_FLOAT) {
478 stdscan_set(save);
479 i = tokval.t_type = token;
480 goto is_expression;
481 } else {
482 goto is_float;
483 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700484 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300485is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300486 eop->type = EOT_DB_STRING;
487 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300488
489 eop->stringlen = idata_bytes(result->opcode);
490 if (eop->stringlen > 16) {
491 nasm_error(ERR_NONFATAL, "floating-point constant"
492 " encountered in DY instruction");
493 eop->stringlen = 0;
494 } else if (eop->stringlen < 1) {
495 nasm_error(ERR_NONFATAL, "floating-point constant"
496 " encountered in unknown instruction");
497 /*
498 * fix suggested by Pedro Gimeno... original line was:
499 * eop->type = EOT_NOTHING;
500 */
501 eop->stringlen = 0;
502 }
503
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300504 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
505 tail = &eop->next;
506 *fixptr = eop;
507 eop->stringval = (char *)eop + sizeof(extop);
508 if (!eop->stringlen ||
509 !float_const(tokval.t_charptr, sign,
510 (uint8_t *)eop->stringval,
511 eop->stringlen, nasm_error))
512 eop->type = EOT_NOTHING;
513 i = stdscan(NULL, &tokval); /* eat the comma */
514 } else {
515 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700517
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300518is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700520 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 i = tokval.t_type;
522 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400523 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 return result; /* ignore this instruction */
525 }
526 if (is_unknown(value)) {
527 eop->type = EOT_DB_NUMBER;
528 eop->offset = 0; /* doesn't matter what we put */
529 eop->segment = eop->wrt = NO_SEG; /* likewise */
530 } else if (is_reloc(value)) {
531 eop->type = EOT_DB_NUMBER;
532 eop->offset = reloc_value(value);
533 eop->segment = reloc_seg(value);
534 eop->wrt = reloc_wrt(value);
535 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700536 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 "operand %d: expression is not simple"
538 " or relocatable", oper_num);
539 }
540 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000541
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 /*
543 * We're about to call stdscan(), which will eat the
544 * comma that we're currently sitting on between
545 * arguments. However, we'd better check first that it
546 * _is_ a comma.
547 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400548 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 break;
550 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700551 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400552 oper_num);
553 result->opcode = I_none;/* unrecoverable parse error: */
554 return result; /* ignore this instruction */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 }
556 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000557
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 if (result->opcode == I_INCBIN) {
559 /*
560 * Correct syntax for INCBIN is that there should be
561 * one string operand, followed by one or two numeric
562 * operands.
563 */
564 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700565 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 else if (result->eops->next &&
567 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200568 nasm_error(ERR_NONFATAL, "`incbin': second 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->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200572 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400573 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 else if (result->eops->next && result->eops->next->next &&
575 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700576 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400577 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 return result;
580 /*
581 * If we reach here, one of the above errors happened.
582 * Throw the instruction away.
583 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400584 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000585 return result;
586 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700587 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 "no operand for data declaration");
589 else
590 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000593 }
594
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400595 /*
596 * Now we begin to parse the operands. There may be up to four
597 * of these, separated by commas, and terminated by a zero token.
598 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000599
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700600 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300601 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 int mref; /* is this going to be a memory ref? */
603 int bracket; /* is it a [] mref, or a & mref? */
604 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700605 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000606
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700607 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400608 result->oprs[operand].eaflags = 0; /* and this */
609 result->oprs[operand].opflags = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700610 result->oprs[operand].decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000611
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400613 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300615 else if (first && i == ':') {
616 insn_is_label = true;
617 goto restart_parse;
618 }
619 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 result->oprs[operand].type = 0; /* so far, no override */
621 while (i == TOKEN_SPECIAL) { /* size specifiers */
622 switch ((int)tokval.t_integer) {
623 case S_BYTE:
624 if (!setsize) /* we want to use only the first */
625 result->oprs[operand].type |= BITS8;
626 setsize = 1;
627 break;
628 case S_WORD:
629 if (!setsize)
630 result->oprs[operand].type |= BITS16;
631 setsize = 1;
632 break;
633 case S_DWORD:
634 case S_LONG:
635 if (!setsize)
636 result->oprs[operand].type |= BITS32;
637 setsize = 1;
638 break;
639 case S_QWORD:
640 if (!setsize)
641 result->oprs[operand].type |= BITS64;
642 setsize = 1;
643 break;
644 case S_TWORD:
645 if (!setsize)
646 result->oprs[operand].type |= BITS80;
647 setsize = 1;
648 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700649 case S_OWORD:
650 if (!setsize)
651 result->oprs[operand].type |= BITS128;
652 setsize = 1;
653 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700654 case S_YWORD:
655 if (!setsize)
656 result->oprs[operand].type |= BITS256;
657 setsize = 1;
658 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 case S_TO:
660 result->oprs[operand].type |= TO;
661 break;
662 case S_STRICT:
663 result->oprs[operand].type |= STRICT;
664 break;
665 case S_FAR:
666 result->oprs[operand].type |= FAR;
667 break;
668 case S_NEAR:
669 result->oprs[operand].type |= NEAR;
670 break;
671 case S_SHORT:
672 result->oprs[operand].type |= SHORT;
673 break;
674 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700675 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676 }
677 i = stdscan(NULL, &tokval);
678 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000679
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700681 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700683 i = stdscan(NULL, &tokval); /* then skip the colon */
684 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300685 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700686 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 }
688 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700689 mref = false;
690 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000692
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 if ((result->oprs[operand].type & FAR) && !mref &&
694 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700695 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000697
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 value = evaluate(stdscan, NULL, &tokval,
699 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700700 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701 i = tokval.t_type;
702 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700703 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700705 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400706 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 return result; /* ignore this instruction */
708 }
709 if (i == ':' && mref) { /* it was seg:offset */
710 /*
711 * Process the segment override.
712 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400713 if (value[1].type != 0 ||
714 value->value != 1 ||
715 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700716 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700717 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700718 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700719 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000720 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300721 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400722 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300723 result->oprs[operand].eaflags |= EAF_FSGS;
724 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000725
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700727 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300728 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 i = stdscan(NULL, &tokval);
730 }
731 value = evaluate(stdscan, NULL, &tokval,
732 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700733 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000734 i = tokval.t_type;
735 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700736 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 }
738 /* and get the offset */
739 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400740 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 return result; /* ignore this instruction */
742 }
743 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200744
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700745 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 if (mref && bracket) { /* find ] at the end */
747 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700748 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200749 recover = true;
750 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700752 if (i == TOKEN_DECORATOR) {
753 /*
754 * according to AVX512 spec, only broacast decorator is
755 * expected for memory reference operands
756 */
757 if (tokval.t_flag & TFLAG_BRDCAST) {
758 brace_flags |= GEN_BRDCAST(0);
759 i = stdscan(NULL, &tokval);
760 } else {
761 nasm_error(ERR_NONFATAL, "broadcast decorator"
762 "expected inside braces");
763 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 == ':') {
779 result->oprs[operand].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;
802 result->oprs[operand].hintbase = hints.base;
803 result->oprs[operand].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");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400824 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 return result;
826 } else
827 b = e->type;
828 e++;
829 }
830 if (e->type != 0) { /* is there an offset? */
831 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700832 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400834 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 return result;
836 } else {
837 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100838 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 o = 0; /* doesn't matter what */
840 result->oprs[operand].wrt = NO_SEG; /* nor this */
841 result->oprs[operand].segment = NO_SEG; /* or this */
842 while (e->type)
843 e++; /* go to the end of the line */
844 } else {
845 if (e->type == EXPR_SIMPLE) {
846 o = e->value;
847 e++;
848 }
849 if (e->type == EXPR_WRT) {
850 result->oprs[operand].wrt = e->value;
851 e++;
852 } else
853 result->oprs[operand].wrt = NO_SEG;
854 /*
855 * Look for a segment base type.
856 */
857 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700858 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 "beroset-p-630-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400860 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 return result;
862 }
863 while (e->type && e->value == 0)
864 e++;
865 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700866 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400868 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 return result;
870 }
871 if (e->type) {
872 result->oprs[operand].segment =
873 e->type - EXPR_SEGBASE;
874 e++;
875 } else
876 result->oprs[operand].segment = NO_SEG;
877 while (e->type && e->value == 0)
878 e++;
879 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700880 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400882 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 return result;
884 }
885 }
886 }
887 } else {
888 o = 0;
889 result->oprs[operand].wrt = NO_SEG;
890 result->oprs[operand].segment = NO_SEG;
891 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700894 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400896 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 return result;
898 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000899
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300900 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700901 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000902
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300903 if (b == -1 && (i == -1 || s == 0)) {
904 int is_rel = globalbits == 64 &&
905 !(result->oprs[operand].eaflags & EAF_ABS) &&
906 ((globalrel &&
907 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
908 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000909
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300910 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
911 }
H. Peter Anvine20ca022013-07-19 17:06:08 -0700912
913 if (i != -1) {
914 opflags_t iclass = nasm_reg_flags[i];
915
916 if (is_class(XMMREG,iclass))
917 result->oprs[operand].type |= XMEM;
918 else if (is_class(YMMREG,iclass))
919 result->oprs[operand].type |= YMEM;
920 }
921
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 result->oprs[operand].basereg = b;
923 result->oprs[operand].indexreg = i;
924 result->oprs[operand].scale = s;
925 result->oprs[operand].offset = o;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700926 result->oprs[operand].decoflags |= brace_flags;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400929 result->oprs[operand].type |= IMMEDIATE;
930 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
931 result->oprs[operand].offset = 0; /* don't care */
932 result->oprs[operand].segment = NO_SEG; /* don't care again */
933 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100934
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400935 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300936 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700937 result->oprs[operand].type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400938 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300939 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400941 result->oprs[operand].type |= IMMEDIATE;
942 result->oprs[operand].offset = reloc_value(value);
943 result->oprs[operand].segment = reloc_seg(value);
944 result->oprs[operand].wrt = reloc_wrt(value);
945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400947 uint64_t n = reloc_value(value);
948 if (n == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 result->oprs[operand].type |= UNITY;
950 if (optimizing >= 0 &&
951 !(result->oprs[operand].type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400952 if ((uint32_t) (n + 128) <= 255)
953 result->oprs[operand].type |= SBYTEDWORD;
954 if ((uint16_t) (n + 128) <= 255)
955 result->oprs[operand].type |= SBYTEWORD;
956 if (n <= 0xFFFFFFFF)
957 result->oprs[operand].type |= UDWORD;
958 if (n + 0x80000000 <= 0xFFFFFFFF)
959 result->oprs[operand].type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 }
961 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700962 } else if(value->type == EXPR_RDSAE) {
963 /*
964 * it's not an operand but a rounding or SAE decorator.
965 * put the decorator information in the (opflag_t) type field
966 * of previous operand.
967 */
968 operand --;
969 switch (value->value) {
970 case BRC_RN:
971 case BRC_RU:
972 case BRC_RD:
973 case BRC_RZ:
974 case BRC_SAE:
975 result->oprs[operand].decoflags |=
976 (value->value == BRC_SAE ? SAE : ER);
977 result->evex_rm = value->value;
978 break;
979 default:
980 nasm_error(ERR_NONFATAL, "invalid decorator");
981 break;
982 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +0400984 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000985
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700987 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400988 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 return result;
990 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 /*
993 * check that its only 1 register, not an expression...
994 */
995 for (i = 1; value[i].type; i++)
996 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700997 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400998 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 return result;
1000 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001001
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 /* clear overrides, except TO which applies to FPU regs */
1003 if (result->oprs[operand].type & ~TO) {
1004 /*
1005 * we want to produce a warning iff the specified size
1006 * is different from the register size
1007 */
H. Peter Anvin68222142007-11-18 22:18:09 -08001008 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001010 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001012 result->oprs[operand].type &= TO;
1013 result->oprs[operand].type |= REGISTER;
1014 result->oprs[operand].type |= nasm_reg_flags[value->type];
Jin Kyu Song72018a22013-08-05 20:46:18 -07001015 result->oprs[operand].decoflags |= brace_flags;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001016 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017
H. Peter Anvin68222142007-11-18 22:18:09 -08001018 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001019 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 "register size specification ignored");
1021 }
1022 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001023 }
1024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001026
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001027 /* clear remaining operands */
1028 while (operand < MAX_OPERANDS)
1029 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001030
1031 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -07001032 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001033 */
1034 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 case I_RESW:
1036 result->opcode = I_RESB;
1037 result->oprs[0].offset *= 2;
1038 break;
1039 case I_RESD:
1040 result->opcode = I_RESB;
1041 result->oprs[0].offset *= 4;
1042 break;
1043 case I_RESQ:
1044 result->opcode = I_RESB;
1045 result->oprs[0].offset *= 8;
1046 break;
1047 case I_REST:
1048 result->opcode = I_RESB;
1049 result->oprs[0].offset *= 10;
1050 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001051 case I_RESO:
1052 result->opcode = I_RESB;
1053 result->oprs[0].offset *= 16;
1054 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001055 case I_RESY:
1056 result->opcode = I_RESB;
1057 result->oprs[0].offset *= 32;
1058 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001059 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001060 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001061 }
1062
1063 return result;
1064}
1065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001067{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001068 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001069 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001070 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001071
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001072 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001074 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001075
H. Peter Anvin76690a12002-04-30 20:52:49 +00001076 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001077}
1078
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001080{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001081 extop *e;
1082
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001083 while ((e = i->eops)) {
1084 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001085 if (e->type == EOT_DB_STRING_FREE)
1086 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001088 }
1089}