blob: 37a5e1cf26074baa02219a7556b150e55e32731a [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.
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 Anvinea6e34d2002-04-30 20:51:32 +0000249 int operand;
250 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 */
269 if (i == TOKEN_EOS) {
270 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000272 }
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400273
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400274 if (i != TOKEN_ID &&
275 i != TOKEN_INSN &&
276 i != TOKEN_PREFIX &&
277 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
278 nasm_error(ERR_NONFATAL,
279 "label or instruction expected at start of line");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400280 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000282 }
283
H. Peter Anvin9c987692007-11-04 21:09:32 -0800284 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
285 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300286 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287 result->label = tokval.t_charptr;
288 i = stdscan(NULL, &tokval);
289 if (i == ':') { /* skip over the optional colon */
290 i = stdscan(NULL, &tokval);
291 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700292 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000293 "label alone on a line without a colon might be in error");
294 }
295 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
296 /*
297 * FIXME: location->segment could be NO_SEG, in which case
298 * it is possible we should be passing 'abs_seg'. Look into this.
299 * Work out whether that is *really* what we should be doing.
300 * Generally fix things. I think this is right as it is, but
301 * am still not certain.
302 */
303 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700304 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000306 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000307
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400308 /* Just a label here */
309 if (i == TOKEN_EOS) {
310 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000311 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000312 }
313
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400314 nasm_build_assert(P_none != 0);
315 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000316 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000317
318 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400319 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300320 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800321
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 /*
323 * Handle special case: the TIMES prefix.
324 */
325 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
326 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000327
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400329 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330 i = tokval.t_type;
331 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400332 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333 return result; /* ignore this instruction */
334 }
335 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700336 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000337 "non-constant argument supplied to TIMES");
338 result->times = 1L;
339 } else {
340 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700341 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200342 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000343 value->value);
344 result->times = 0;
345 }
346 }
347 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300348 int slot = prefix_slot(tokval.t_integer);
349 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700350 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100351 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300352 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700353 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300354 nasm_error(ERR_NONFATAL,
355 "instruction has conflicting prefixes");
356 }
357 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358 i = stdscan(NULL, &tokval);
359 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000360 }
361
362 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300363 int j;
364 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700365
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400366 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300367 if ((pfx = result->prefixes[j]) != P_none)
368 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400369 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700370
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700371 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372 /*
373 * Instruction prefixes are present, but no actual
374 * instruction. This is allowed: at this point we
375 * invent a notional instruction of RESB 0.
376 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400377 result->opcode = I_RESB;
378 result->operands = 1;
379 result->oprs[0].type = IMMEDIATE;
380 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
382 return result;
383 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700384 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400385 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 return result;
387 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000388 }
389
390 result->opcode = tokval.t_integer;
391 result->condition = tokval.t_inttwo;
392
393 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700394 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000395 * evaluated operands, since the correct values _must_ be known
396 * on the first pass. Hence, even in pass one, we set the
397 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700398 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000399 */
Charles Crayne2581c862008-09-10 19:21:52 -0700400 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700401 critical = (pass0 < 2 ? 1 : 2);
402
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 } else
404 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000405
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700406 if (result->opcode == I_DB || result->opcode == I_DW ||
407 result->opcode == I_DD || result->opcode == I_DQ ||
408 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvin9d546102013-10-02 18:25:19 -0700409 result->opcode == I_DY || result->opcode == I_DZ ||
410 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 extop *eop, **tail = &result->eops, **fixptr;
412 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300413 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000414
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700415 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000416
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700418 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000419 */
420 while (1) {
421 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400422 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000423 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300424 else if (first && i == ':') {
425 insn_is_label = true;
426 goto restart_parse;
427 }
428 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429 fixptr = tail;
430 eop = *tail = nasm_malloc(sizeof(extop));
431 tail = &eop->next;
432 eop->next = NULL;
433 eop->type = EOT_NOTHING;
434 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300435 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000436
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300437 /*
438 * is_comma_next() here is to distinguish this from
439 * a string used as part of an expression...
440 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700441 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400442 eop->type = EOT_DB_STRING;
443 eop->stringval = tokval.t_charptr;
444 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300446 } else if (i == TOKEN_STRFUNC) {
447 bool parens = false;
448 const char *funcname = tokval.t_charptr;
449 enum strfunc func = tokval.t_integer;
450 i = stdscan(NULL, &tokval);
451 if (i == '(') {
452 parens = true;
453 i = stdscan(NULL, &tokval);
454 }
455 if (i != TOKEN_STR) {
456 nasm_error(ERR_NONFATAL,
457 "%s must be followed by a string constant",
458 funcname);
459 eop->type = EOT_NOTHING;
460 } else {
461 eop->type = EOT_DB_STRING_FREE;
462 eop->stringlen =
463 string_transform(tokval.t_charptr, tokval.t_inttwo,
464 &eop->stringval, func);
465 if (eop->stringlen == (size_t)-1) {
466 nasm_error(ERR_NONFATAL, "invalid string for transform");
467 eop->type = EOT_NOTHING;
468 }
469 }
470 if (parens && i && i != ')') {
471 i = stdscan(NULL, &tokval);
472 if (i != ')') {
473 nasm_error(ERR_NONFATAL, "unterminated %s function",
474 funcname);
475 }
476 }
477 if (i && i != ',')
478 i = stdscan(NULL, &tokval);
479 } else if (i == '-' || i == '+') {
480 char *save = stdscan_get();
481 int token = i;
482 sign = (i == '-') ? -1 : 1;
483 i = stdscan(NULL, &tokval);
484 if (i != TOKEN_FLOAT) {
485 stdscan_set(save);
486 i = tokval.t_type = token;
487 goto is_expression;
488 } else {
489 goto is_float;
490 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700491 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300492is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300493 eop->type = EOT_DB_STRING;
494 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300495
496 eop->stringlen = idata_bytes(result->opcode);
497 if (eop->stringlen > 16) {
498 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin9d546102013-10-02 18:25:19 -0700499 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300500 eop->stringlen = 0;
501 } else if (eop->stringlen < 1) {
502 nasm_error(ERR_NONFATAL, "floating-point constant"
503 " encountered in unknown instruction");
504 /*
505 * fix suggested by Pedro Gimeno... original line was:
506 * eop->type = EOT_NOTHING;
507 */
508 eop->stringlen = 0;
509 }
510
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300511 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
512 tail = &eop->next;
513 *fixptr = eop;
514 eop->stringval = (char *)eop + sizeof(extop);
515 if (!eop->stringlen ||
516 !float_const(tokval.t_charptr, sign,
517 (uint8_t *)eop->stringval,
518 eop->stringlen, nasm_error))
519 eop->type = EOT_NOTHING;
520 i = stdscan(NULL, &tokval); /* eat the comma */
521 } else {
522 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700524
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300525is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700527 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 i = tokval.t_type;
529 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400530 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 return result; /* ignore this instruction */
532 }
533 if (is_unknown(value)) {
534 eop->type = EOT_DB_NUMBER;
535 eop->offset = 0; /* doesn't matter what we put */
536 eop->segment = eop->wrt = NO_SEG; /* likewise */
537 } else if (is_reloc(value)) {
538 eop->type = EOT_DB_NUMBER;
539 eop->offset = reloc_value(value);
540 eop->segment = reloc_seg(value);
541 eop->wrt = reloc_wrt(value);
542 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700543 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 "operand %d: expression is not simple"
545 " or relocatable", oper_num);
546 }
547 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000548
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 /*
550 * We're about to call stdscan(), which will eat the
551 * comma that we're currently sitting on between
552 * arguments. However, we'd better check first that it
553 * _is_ a comma.
554 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400555 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 break;
557 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700558 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400559 oper_num);
560 result->opcode = I_none;/* unrecoverable parse error: */
561 return result; /* ignore this instruction */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 }
563 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000564
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 if (result->opcode == I_INCBIN) {
566 /*
567 * Correct syntax for INCBIN is that there should be
568 * one string operand, followed by one or two numeric
569 * operands.
570 */
571 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700572 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 else if (result->eops->next &&
574 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200575 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400576 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 else if (result->eops->next && result->eops->next->next &&
578 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200579 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400580 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 else if (result->eops->next && result->eops->next->next &&
582 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700583 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400584 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000585 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 return result;
587 /*
588 * If we reach here, one of the above errors happened.
589 * Throw the instruction away.
590 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400591 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 return result;
593 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700594 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 "no operand for data declaration");
596 else
597 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000600 }
601
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400602 /*
603 * Now we begin to parse the operands. There may be up to four
604 * of these, separated by commas, and terminated by a zero token.
605 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000606
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700607 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300608 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 int mref; /* is this going to be a memory ref? */
610 int bracket; /* is it a [] mref, or a & mref? */
611 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700612 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000613
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700614 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400615 result->oprs[operand].eaflags = 0; /* and this */
616 result->oprs[operand].opflags = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700617 result->oprs[operand].decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400620 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300622 else if (first && i == ':') {
623 insn_is_label = true;
624 goto restart_parse;
625 }
626 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 result->oprs[operand].type = 0; /* so far, no override */
628 while (i == TOKEN_SPECIAL) { /* size specifiers */
629 switch ((int)tokval.t_integer) {
630 case S_BYTE:
631 if (!setsize) /* we want to use only the first */
632 result->oprs[operand].type |= BITS8;
633 setsize = 1;
634 break;
635 case S_WORD:
636 if (!setsize)
637 result->oprs[operand].type |= BITS16;
638 setsize = 1;
639 break;
640 case S_DWORD:
641 case S_LONG:
642 if (!setsize)
643 result->oprs[operand].type |= BITS32;
644 setsize = 1;
645 break;
646 case S_QWORD:
647 if (!setsize)
648 result->oprs[operand].type |= BITS64;
649 setsize = 1;
650 break;
651 case S_TWORD:
652 if (!setsize)
653 result->oprs[operand].type |= BITS80;
654 setsize = 1;
655 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700656 case S_OWORD:
657 if (!setsize)
658 result->oprs[operand].type |= BITS128;
659 setsize = 1;
660 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700661 case S_YWORD:
662 if (!setsize)
663 result->oprs[operand].type |= BITS256;
664 setsize = 1;
665 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700666 case S_ZWORD:
667 if (!setsize)
668 result->oprs[operand].type |= BITS512;
669 setsize = 1;
670 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 case S_TO:
672 result->oprs[operand].type |= TO;
673 break;
674 case S_STRICT:
675 result->oprs[operand].type |= STRICT;
676 break;
677 case S_FAR:
678 result->oprs[operand].type |= FAR;
679 break;
680 case S_NEAR:
681 result->oprs[operand].type |= NEAR;
682 break;
683 case S_SHORT:
684 result->oprs[operand].type |= SHORT;
685 break;
686 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700687 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 }
689 i = stdscan(NULL, &tokval);
690 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000691
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700693 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700695 i = stdscan(NULL, &tokval); /* then skip the colon */
696 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300697 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700698 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 }
700 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700701 mref = false;
702 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000704
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 if ((result->oprs[operand].type & FAR) && !mref &&
706 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700707 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000709
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 value = evaluate(stdscan, NULL, &tokval,
711 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700712 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 i = tokval.t_type;
714 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700715 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700717 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400718 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000719 return result; /* ignore this instruction */
720 }
721 if (i == ':' && mref) { /* it was seg:offset */
722 /*
723 * Process the segment override.
724 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400725 if (value[1].type != 0 ||
726 value->value != 1 ||
727 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700728 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700729 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700730 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700731 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000732 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300733 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400734 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300735 result->oprs[operand].eaflags |= EAF_FSGS;
736 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000737
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700739 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300740 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 i = stdscan(NULL, &tokval);
742 }
743 value = evaluate(stdscan, NULL, &tokval,
744 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700745 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 i = tokval.t_type;
747 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700748 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 }
750 /* and get the offset */
751 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400752 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 return result; /* ignore this instruction */
754 }
755 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200756
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700757 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 if (mref && bracket) { /* find ] at the end */
759 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700760 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200761 recover = true;
762 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700764 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700765 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700766 * according to AVX512 spec, broacast or opmask decorator
767 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700768 */
769 if (tokval.t_flag & TFLAG_BRDCAST) {
770 brace_flags |= GEN_BRDCAST(0);
771 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700772 } else if (i == TOKEN_OPMASK) {
773 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
774 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700775 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700776 nasm_error(ERR_NONFATAL, "broadcast or opmask "
777 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700778 recover = true;
779 }
780 }
781
Victor van den Elzen02846d32009-06-23 03:47:07 +0200782 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700783 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200784 recover = true;
785 }
786 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700788 if (i != 0 && i != ',' && i != ':' &&
789 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
790 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
791 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200792 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 } else if (i == ':') {
794 result->oprs[operand].type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700795 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
796 /* parse opmask (and zeroing) after an operand */
797 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 }
799 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200800 if (recover) {
801 do { /* error recovery */
802 i = stdscan(NULL, &tokval);
803 } while (i != 0 && i != ',');
804 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300806 /*
807 * now convert the exprs returned from evaluate()
808 * into operand descriptions...
809 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 if (mref) { /* it's a memory reference */
812 expr *e = value;
813 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300814 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 b = i = -1, o = s = 0;
817 result->oprs[operand].hintbase = hints.base;
818 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700821 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
822
823 if (is_gpr && e->value == 1)
824 b = e->type; /* It can be basereg */
825 else /* No, it has to be indexreg */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 i = e->type, s = e->value;
827 e++;
828 }
829 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700830 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
831
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 if (b != -1) /* If the first was the base, ... */
833 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000834
H. Peter Anvine20ca022013-07-19 17:06:08 -0700835 else if (!is_gpr || e->value != 1) {
836 /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700837 nasm_error(ERR_NONFATAL,
H. Peter Anvine20ca022013-07-19 17:06:08 -0700838 "invalid effective address: two index registers");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400839 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 return result;
841 } else
842 b = e->type;
843 e++;
844 }
845 if (e->type != 0) { /* is there an offset? */
846 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700847 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400849 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 return result;
851 } else {
852 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100853 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 o = 0; /* doesn't matter what */
855 result->oprs[operand].wrt = NO_SEG; /* nor this */
856 result->oprs[operand].segment = NO_SEG; /* or this */
857 while (e->type)
858 e++; /* go to the end of the line */
859 } else {
860 if (e->type == EXPR_SIMPLE) {
861 o = e->value;
862 e++;
863 }
864 if (e->type == EXPR_WRT) {
865 result->oprs[operand].wrt = e->value;
866 e++;
867 } else
868 result->oprs[operand].wrt = NO_SEG;
869 /*
870 * Look for a segment base type.
871 */
872 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700873 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 "beroset-p-630-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400875 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 return result;
877 }
878 while (e->type && e->value == 0)
879 e++;
880 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700881 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400883 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 return result;
885 }
886 if (e->type) {
887 result->oprs[operand].segment =
888 e->type - EXPR_SEGBASE;
889 e++;
890 } else
891 result->oprs[operand].segment = NO_SEG;
892 while (e->type && e->value == 0)
893 e++;
894 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700895 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400897 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 return result;
899 }
900 }
901 }
902 } else {
903 o = 0;
904 result->oprs[operand].wrt = NO_SEG;
905 result->oprs[operand].segment = NO_SEG;
906 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000907
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700909 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400911 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 return result;
913 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000914
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300915 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700916 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000917
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300918 if (b == -1 && (i == -1 || s == 0)) {
919 int is_rel = globalbits == 64 &&
920 !(result->oprs[operand].eaflags & EAF_ABS) &&
921 ((globalrel &&
922 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
923 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000924
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300925 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
926 }
H. Peter Anvine20ca022013-07-19 17:06:08 -0700927
928 if (i != -1) {
929 opflags_t iclass = nasm_reg_flags[i];
930
931 if (is_class(XMMREG,iclass))
932 result->oprs[operand].type |= XMEM;
933 else if (is_class(YMMREG,iclass))
934 result->oprs[operand].type |= YMEM;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700935 else if (is_class(ZMMREG,iclass))
936 result->oprs[operand].type |= ZMEM;
H. Peter Anvine20ca022013-07-19 17:06:08 -0700937 }
938
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 result->oprs[operand].basereg = b;
940 result->oprs[operand].indexreg = i;
941 result->oprs[operand].scale = s;
942 result->oprs[operand].offset = o;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700943 result->oprs[operand].decoflags |= brace_flags;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400946 result->oprs[operand].type |= IMMEDIATE;
947 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
948 result->oprs[operand].offset = 0; /* don't care */
949 result->oprs[operand].segment = NO_SEG; /* don't care again */
950 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100951
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400952 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300953 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700954 result->oprs[operand].type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400955 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300956 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400958 result->oprs[operand].type |= IMMEDIATE;
959 result->oprs[operand].offset = reloc_value(value);
960 result->oprs[operand].segment = reloc_seg(value);
961 result->oprs[operand].wrt = reloc_wrt(value);
962
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400964 uint64_t n = reloc_value(value);
965 if (n == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 result->oprs[operand].type |= UNITY;
967 if (optimizing >= 0 &&
968 !(result->oprs[operand].type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400969 if ((uint32_t) (n + 128) <= 255)
970 result->oprs[operand].type |= SBYTEDWORD;
971 if ((uint16_t) (n + 128) <= 255)
972 result->oprs[operand].type |= SBYTEWORD;
973 if (n <= 0xFFFFFFFF)
974 result->oprs[operand].type |= UDWORD;
975 if (n + 0x80000000 <= 0xFFFFFFFF)
976 result->oprs[operand].type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 }
978 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700979 } else if(value->type == EXPR_RDSAE) {
980 /*
981 * it's not an operand but a rounding or SAE decorator.
982 * put the decorator information in the (opflag_t) type field
983 * of previous operand.
984 */
985 operand --;
986 switch (value->value) {
987 case BRC_RN:
988 case BRC_RU:
989 case BRC_RD:
990 case BRC_RZ:
991 case BRC_SAE:
992 result->oprs[operand].decoflags |=
993 (value->value == BRC_SAE ? SAE : ER);
994 result->evex_rm = value->value;
995 break;
996 default:
997 nasm_error(ERR_NONFATAL, "invalid decorator");
998 break;
999 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001001 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001002
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001004 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +04001005 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 return result;
1007 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001008
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 /*
1010 * check that its only 1 register, not an expression...
1011 */
1012 for (i = 1; value[i].type; i++)
1013 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001014 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +04001015 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 return result;
1017 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001018
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 /* clear overrides, except TO which applies to FPU regs */
1020 if (result->oprs[operand].type & ~TO) {
1021 /*
1022 * we want to produce a warning iff the specified size
1023 * is different from the register size
1024 */
H. Peter Anvin68222142007-11-18 22:18:09 -08001025 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001027 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001029 result->oprs[operand].type &= TO;
1030 result->oprs[operand].type |= REGISTER;
1031 result->oprs[operand].type |= nasm_reg_flags[value->type];
Jin Kyu Song72018a22013-08-05 20:46:18 -07001032 result->oprs[operand].decoflags |= brace_flags;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001033 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034
H. Peter Anvin68222142007-11-18 22:18:09 -08001035 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001036 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 "register size specification ignored");
1038 }
1039 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001040
1041 /* remember the position of operand having broadcasting/ER mode */
1042 if (result->oprs[operand].decoflags & (BRDCAST_MASK | ER | SAE))
1043 result->evex_brerop = operand;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001044 }
1045
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001047
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001048 /* clear remaining operands */
1049 while (operand < MAX_OPERANDS)
1050 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001051
1052 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001053 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001054 */
1055 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 case I_RESW:
1057 result->opcode = I_RESB;
1058 result->oprs[0].offset *= 2;
1059 break;
1060 case I_RESD:
1061 result->opcode = I_RESB;
1062 result->oprs[0].offset *= 4;
1063 break;
1064 case I_RESQ:
1065 result->opcode = I_RESB;
1066 result->oprs[0].offset *= 8;
1067 break;
1068 case I_REST:
1069 result->opcode = I_RESB;
1070 result->oprs[0].offset *= 10;
1071 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001072 case I_RESO:
1073 result->opcode = I_RESB;
1074 result->oprs[0].offset *= 16;
1075 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001076 case I_RESY:
1077 result->opcode = I_RESB;
1078 result->oprs[0].offset *= 32;
1079 break;
H. Peter Anvin9d546102013-10-02 18:25:19 -07001080 case I_RESZ:
1081 result->opcode = I_RESB;
1082 result->oprs[0].offset *= 64;
1083 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001084 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001085 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001086 }
1087
1088 return result;
1089}
1090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001092{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001093 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001094 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001095 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001096
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001097 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001099 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001100
H. Peter Anvin76690a12002-04-30 20:52:49 +00001101 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001102}
1103
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001105{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001106 extop *e;
1107
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001108 while ((e = i->eops)) {
1109 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001110 if (e->type == EOT_DB_STRING_FREE)
1111 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001113 }
1114}