blob: 4b3f059c801a3de235900a07ca435a6baf463c2e [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 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000265
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400266 /* Ignore blank lines */
267 if (i == TOKEN_EOS) {
268 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000270 }
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400271
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400272 if (i != TOKEN_ID &&
273 i != TOKEN_INSN &&
274 i != TOKEN_PREFIX &&
275 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
276 nasm_error(ERR_NONFATAL,
277 "label or instruction expected at start of line");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400278 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000279 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000280 }
281
H. Peter Anvin9c987692007-11-04 21:09:32 -0800282 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
283 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300284 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285 result->label = tokval.t_charptr;
286 i = stdscan(NULL, &tokval);
287 if (i == ':') { /* skip over the optional colon */
288 i = stdscan(NULL, &tokval);
289 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700290 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291 "label alone on a line without a colon might be in error");
292 }
293 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
294 /*
295 * FIXME: location->segment could be NO_SEG, in which case
296 * it is possible we should be passing 'abs_seg'. Look into this.
297 * Work out whether that is *really* what we should be doing.
298 * Generally fix things. I think this is right as it is, but
299 * am still not certain.
300 */
301 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700302 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000304 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000305
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400306 /* Just a label here */
307 if (i == TOKEN_EOS) {
308 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000310 }
311
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400312 nasm_build_assert(P_none != 0);
313 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000315
316 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400317 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300318 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800319
H. Peter Anvine2c80182005-01-15 22:15:51 +0000320 /*
321 * Handle special case: the TIMES prefix.
322 */
323 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
324 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000325
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400327 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 i = tokval.t_type;
329 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400330 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000331 return result; /* ignore this instruction */
332 }
333 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700334 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000335 "non-constant argument supplied to TIMES");
336 result->times = 1L;
337 } else {
338 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700339 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200340 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000341 value->value);
342 result->times = 0;
343 }
344 }
345 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300346 int slot = prefix_slot(tokval.t_integer);
347 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700348 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100349 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300350 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700351 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300352 nasm_error(ERR_NONFATAL,
353 "instruction has conflicting prefixes");
354 }
355 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000356 i = stdscan(NULL, &tokval);
357 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000358 }
359
360 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300361 int j;
362 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700363
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400364 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300365 if ((pfx = result->prefixes[j]) != P_none)
366 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400367 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700368
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700369 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370 /*
371 * Instruction prefixes are present, but no actual
372 * instruction. This is allowed: at this point we
373 * invent a notional instruction of RESB 0.
374 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400375 result->opcode = I_RESB;
376 result->operands = 1;
377 result->oprs[0].type = IMMEDIATE;
378 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
380 return result;
381 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700382 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400383 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 return result;
385 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000386 }
387
388 result->opcode = tokval.t_integer;
389 result->condition = tokval.t_inttwo;
390
391 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700392 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000393 * evaluated operands, since the correct values _must_ be known
394 * on the first pass. Hence, even in pass one, we set the
395 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700396 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000397 */
Charles Crayne2581c862008-09-10 19:21:52 -0700398 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700399 critical = (pass0 < 2 ? 1 : 2);
400
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 } else
402 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000403
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700404 if (result->opcode == I_DB || result->opcode == I_DW ||
405 result->opcode == I_DD || result->opcode == I_DQ ||
406 result->opcode == I_DT || result->opcode == I_DO ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300407 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 extop *eop, **tail = &result->eops, **fixptr;
409 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300410 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000411
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700412 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000413
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700415 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 */
417 while (1) {
418 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400419 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300421 else if (first && i == ':') {
422 insn_is_label = true;
423 goto restart_parse;
424 }
425 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426 fixptr = tail;
427 eop = *tail = nasm_malloc(sizeof(extop));
428 tail = &eop->next;
429 eop->next = NULL;
430 eop->type = EOT_NOTHING;
431 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300432 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000433
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300434 /*
435 * is_comma_next() here is to distinguish this from
436 * a string used as part of an expression...
437 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700438 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400439 eop->type = EOT_DB_STRING;
440 eop->stringval = tokval.t_charptr;
441 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300443 } else if (i == TOKEN_STRFUNC) {
444 bool parens = false;
445 const char *funcname = tokval.t_charptr;
446 enum strfunc func = tokval.t_integer;
447 i = stdscan(NULL, &tokval);
448 if (i == '(') {
449 parens = true;
450 i = stdscan(NULL, &tokval);
451 }
452 if (i != TOKEN_STR) {
453 nasm_error(ERR_NONFATAL,
454 "%s must be followed by a string constant",
455 funcname);
456 eop->type = EOT_NOTHING;
457 } else {
458 eop->type = EOT_DB_STRING_FREE;
459 eop->stringlen =
460 string_transform(tokval.t_charptr, tokval.t_inttwo,
461 &eop->stringval, func);
462 if (eop->stringlen == (size_t)-1) {
463 nasm_error(ERR_NONFATAL, "invalid string for transform");
464 eop->type = EOT_NOTHING;
465 }
466 }
467 if (parens && i && i != ')') {
468 i = stdscan(NULL, &tokval);
469 if (i != ')') {
470 nasm_error(ERR_NONFATAL, "unterminated %s function",
471 funcname);
472 }
473 }
474 if (i && i != ',')
475 i = stdscan(NULL, &tokval);
476 } else if (i == '-' || i == '+') {
477 char *save = stdscan_get();
478 int token = i;
479 sign = (i == '-') ? -1 : 1;
480 i = stdscan(NULL, &tokval);
481 if (i != TOKEN_FLOAT) {
482 stdscan_set(save);
483 i = tokval.t_type = token;
484 goto is_expression;
485 } else {
486 goto is_float;
487 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700488 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300489is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300490 eop->type = EOT_DB_STRING;
491 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300492
493 eop->stringlen = idata_bytes(result->opcode);
494 if (eop->stringlen > 16) {
495 nasm_error(ERR_NONFATAL, "floating-point constant"
496 " encountered in DY instruction");
497 eop->stringlen = 0;
498 } else if (eop->stringlen < 1) {
499 nasm_error(ERR_NONFATAL, "floating-point constant"
500 " encountered in unknown instruction");
501 /*
502 * fix suggested by Pedro Gimeno... original line was:
503 * eop->type = EOT_NOTHING;
504 */
505 eop->stringlen = 0;
506 }
507
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300508 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
509 tail = &eop->next;
510 *fixptr = eop;
511 eop->stringval = (char *)eop + sizeof(extop);
512 if (!eop->stringlen ||
513 !float_const(tokval.t_charptr, sign,
514 (uint8_t *)eop->stringval,
515 eop->stringlen, nasm_error))
516 eop->type = EOT_NOTHING;
517 i = stdscan(NULL, &tokval); /* eat the comma */
518 } else {
519 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700521
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300522is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700524 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 i = tokval.t_type;
526 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400527 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 return result; /* ignore this instruction */
529 }
530 if (is_unknown(value)) {
531 eop->type = EOT_DB_NUMBER;
532 eop->offset = 0; /* doesn't matter what we put */
533 eop->segment = eop->wrt = NO_SEG; /* likewise */
534 } else if (is_reloc(value)) {
535 eop->type = EOT_DB_NUMBER;
536 eop->offset = reloc_value(value);
537 eop->segment = reloc_seg(value);
538 eop->wrt = reloc_wrt(value);
539 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700540 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 "operand %d: expression is not simple"
542 " or relocatable", oper_num);
543 }
544 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000545
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 /*
547 * We're about to call stdscan(), which will eat the
548 * comma that we're currently sitting on between
549 * arguments. However, we'd better check first that it
550 * _is_ a comma.
551 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400552 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 break;
554 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700555 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400556 oper_num);
557 result->opcode = I_none;/* unrecoverable parse error: */
558 return result; /* ignore this instruction */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 }
560 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000561
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 if (result->opcode == I_INCBIN) {
563 /*
564 * Correct syntax for INCBIN is that there should be
565 * one string operand, followed by one or two numeric
566 * operands.
567 */
568 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700569 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 else if (result->eops->next &&
571 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200572 nasm_error(ERR_NONFATAL, "`incbin': second 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->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200576 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400577 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 else if (result->eops->next && result->eops->next->next &&
579 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700580 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400581 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 return result;
584 /*
585 * If we reach here, one of the above errors happened.
586 * Throw the instruction away.
587 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400588 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 return result;
590 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700591 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 "no operand for data declaration");
593 else
594 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597 }
598
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400599 /*
600 * Now we begin to parse the operands. There may be up to four
601 * of these, separated by commas, and terminated by a zero token.
602 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000603
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700604 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300605 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 int mref; /* is this going to be a memory ref? */
607 int bracket; /* is it a [] mref, or a & mref? */
608 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700609 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000610
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700611 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400612 result->oprs[operand].eaflags = 0; /* and this */
613 result->oprs[operand].opflags = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700614 result->oprs[operand].decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000615
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400617 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300619 else if (first && i == ':') {
620 insn_is_label = true;
621 goto restart_parse;
622 }
623 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 result->oprs[operand].type = 0; /* so far, no override */
625 while (i == TOKEN_SPECIAL) { /* size specifiers */
626 switch ((int)tokval.t_integer) {
627 case S_BYTE:
628 if (!setsize) /* we want to use only the first */
629 result->oprs[operand].type |= BITS8;
630 setsize = 1;
631 break;
632 case S_WORD:
633 if (!setsize)
634 result->oprs[operand].type |= BITS16;
635 setsize = 1;
636 break;
637 case S_DWORD:
638 case S_LONG:
639 if (!setsize)
640 result->oprs[operand].type |= BITS32;
641 setsize = 1;
642 break;
643 case S_QWORD:
644 if (!setsize)
645 result->oprs[operand].type |= BITS64;
646 setsize = 1;
647 break;
648 case S_TWORD:
649 if (!setsize)
650 result->oprs[operand].type |= BITS80;
651 setsize = 1;
652 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700653 case S_OWORD:
654 if (!setsize)
655 result->oprs[operand].type |= BITS128;
656 setsize = 1;
657 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700658 case S_YWORD:
659 if (!setsize)
660 result->oprs[operand].type |= BITS256;
661 setsize = 1;
662 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 case S_TO:
664 result->oprs[operand].type |= TO;
665 break;
666 case S_STRICT:
667 result->oprs[operand].type |= STRICT;
668 break;
669 case S_FAR:
670 result->oprs[operand].type |= FAR;
671 break;
672 case S_NEAR:
673 result->oprs[operand].type |= NEAR;
674 break;
675 case S_SHORT:
676 result->oprs[operand].type |= SHORT;
677 break;
678 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700679 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 }
681 i = stdscan(NULL, &tokval);
682 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000683
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700685 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700687 i = stdscan(NULL, &tokval); /* then skip the colon */
688 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300689 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700690 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 }
692 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700693 mref = false;
694 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000696
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 if ((result->oprs[operand].type & FAR) && !mref &&
698 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700699 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000701
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 value = evaluate(stdscan, NULL, &tokval,
703 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700704 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 i = tokval.t_type;
706 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700707 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700709 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400710 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 return result; /* ignore this instruction */
712 }
713 if (i == ':' && mref) { /* it was seg:offset */
714 /*
715 * Process the segment override.
716 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400717 if (value[1].type != 0 ||
718 value->value != 1 ||
719 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700720 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700721 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700722 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700723 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000724 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300725 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400726 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300727 result->oprs[operand].eaflags |= EAF_FSGS;
728 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000729
H. Peter Anvine2c80182005-01-15 22:15:51 +0000730 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700731 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300732 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 i = stdscan(NULL, &tokval);
734 }
735 value = evaluate(stdscan, NULL, &tokval,
736 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700737 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 i = tokval.t_type;
739 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700740 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 }
742 /* and get the offset */
743 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400744 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 return result; /* ignore this instruction */
746 }
747 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200748
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700749 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 if (mref && bracket) { /* find ] at the end */
751 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700752 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200753 recover = true;
754 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700756 if (i == TOKEN_DECORATOR) {
757 /*
758 * according to AVX512 spec, only broacast decorator is
759 * expected for memory reference operands
760 */
761 if (tokval.t_flag & TFLAG_BRDCAST) {
762 brace_flags |= GEN_BRDCAST(0);
763 i = stdscan(NULL, &tokval);
764 } else {
765 nasm_error(ERR_NONFATAL, "broadcast decorator"
766 "expected inside braces");
767 recover = true;
768 }
769 }
770
Victor van den Elzen02846d32009-06-23 03:47:07 +0200771 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700772 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200773 recover = true;
774 }
775 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700777 if (i != 0 && i != ',' && i != ':' &&
778 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
779 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
780 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200781 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000782 } else if (i == ':') {
783 result->oprs[operand].type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700784 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
785 /* parse opmask (and zeroing) after an operand */
786 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 }
788 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200789 if (recover) {
790 do { /* error recovery */
791 i = stdscan(NULL, &tokval);
792 } while (i != 0 && i != ',');
793 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000794
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300795 /*
796 * now convert the exprs returned from evaluate()
797 * into operand descriptions...
798 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 if (mref) { /* it's a memory reference */
801 expr *e = value;
802 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300803 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000804
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 b = i = -1, o = s = 0;
806 result->oprs[operand].hintbase = hints.base;
807 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000808
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700810 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
811
812 if (is_gpr && e->value == 1)
813 b = e->type; /* It can be basereg */
814 else /* No, it has to be indexreg */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 i = e->type, s = e->value;
816 e++;
817 }
818 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700819 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
820
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 if (b != -1) /* If the first was the base, ... */
822 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000823
H. Peter Anvine20ca022013-07-19 17:06:08 -0700824 else if (!is_gpr || e->value != 1) {
825 /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700826 nasm_error(ERR_NONFATAL,
H. Peter Anvine20ca022013-07-19 17:06:08 -0700827 "invalid effective address: two index registers");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400828 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 return result;
830 } else
831 b = e->type;
832 e++;
833 }
834 if (e->type != 0) { /* is there an offset? */
835 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700836 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400838 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 return result;
840 } else {
841 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100842 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 o = 0; /* doesn't matter what */
844 result->oprs[operand].wrt = NO_SEG; /* nor this */
845 result->oprs[operand].segment = NO_SEG; /* or this */
846 while (e->type)
847 e++; /* go to the end of the line */
848 } else {
849 if (e->type == EXPR_SIMPLE) {
850 o = e->value;
851 e++;
852 }
853 if (e->type == EXPR_WRT) {
854 result->oprs[operand].wrt = e->value;
855 e++;
856 } else
857 result->oprs[operand].wrt = NO_SEG;
858 /*
859 * Look for a segment base type.
860 */
861 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700862 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 "beroset-p-630-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400864 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 return result;
866 }
867 while (e->type && e->value == 0)
868 e++;
869 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700870 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400872 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 return result;
874 }
875 if (e->type) {
876 result->oprs[operand].segment =
877 e->type - EXPR_SEGBASE;
878 e++;
879 } else
880 result->oprs[operand].segment = NO_SEG;
881 while (e->type && e->value == 0)
882 e++;
883 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700884 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400886 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 return result;
888 }
889 }
890 }
891 } else {
892 o = 0;
893 result->oprs[operand].wrt = NO_SEG;
894 result->oprs[operand].segment = NO_SEG;
895 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000896
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700898 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400900 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 return result;
902 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000903
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300904 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700905 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000906
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300907 if (b == -1 && (i == -1 || s == 0)) {
908 int is_rel = globalbits == 64 &&
909 !(result->oprs[operand].eaflags & EAF_ABS) &&
910 ((globalrel &&
911 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
912 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000913
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300914 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
915 }
H. Peter Anvine20ca022013-07-19 17:06:08 -0700916
917 if (i != -1) {
918 opflags_t iclass = nasm_reg_flags[i];
919
920 if (is_class(XMMREG,iclass))
921 result->oprs[operand].type |= XMEM;
922 else if (is_class(YMMREG,iclass))
923 result->oprs[operand].type |= YMEM;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700924 else if (is_class(ZMMREG,iclass))
925 result->oprs[operand].type |= ZMEM;
H. Peter Anvine20ca022013-07-19 17:06:08 -0700926 }
927
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 result->oprs[operand].basereg = b;
929 result->oprs[operand].indexreg = i;
930 result->oprs[operand].scale = s;
931 result->oprs[operand].offset = o;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700932 result->oprs[operand].decoflags |= brace_flags;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400935 result->oprs[operand].type |= IMMEDIATE;
936 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
937 result->oprs[operand].offset = 0; /* don't care */
938 result->oprs[operand].segment = NO_SEG; /* don't care again */
939 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100940
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400941 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300942 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700943 result->oprs[operand].type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400944 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300945 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400947 result->oprs[operand].type |= IMMEDIATE;
948 result->oprs[operand].offset = reloc_value(value);
949 result->oprs[operand].segment = reloc_seg(value);
950 result->oprs[operand].wrt = reloc_wrt(value);
951
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400953 uint64_t n = reloc_value(value);
954 if (n == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 result->oprs[operand].type |= UNITY;
956 if (optimizing >= 0 &&
957 !(result->oprs[operand].type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400958 if ((uint32_t) (n + 128) <= 255)
959 result->oprs[operand].type |= SBYTEDWORD;
960 if ((uint16_t) (n + 128) <= 255)
961 result->oprs[operand].type |= SBYTEWORD;
962 if (n <= 0xFFFFFFFF)
963 result->oprs[operand].type |= UDWORD;
964 if (n + 0x80000000 <= 0xFFFFFFFF)
965 result->oprs[operand].type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 }
967 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700968 } else if(value->type == EXPR_RDSAE) {
969 /*
970 * it's not an operand but a rounding or SAE decorator.
971 * put the decorator information in the (opflag_t) type field
972 * of previous operand.
973 */
974 operand --;
975 switch (value->value) {
976 case BRC_RN:
977 case BRC_RU:
978 case BRC_RD:
979 case BRC_RZ:
980 case BRC_SAE:
981 result->oprs[operand].decoflags |=
982 (value->value == BRC_SAE ? SAE : ER);
983 result->evex_rm = value->value;
984 break;
985 default:
986 nasm_error(ERR_NONFATAL, "invalid decorator");
987 break;
988 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +0400990 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700993 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400994 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 return result;
996 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000997
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 /*
999 * check that its only 1 register, not an expression...
1000 */
1001 for (i = 1; value[i].type; i++)
1002 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001003 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +04001004 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 return result;
1006 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001007
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 /* clear overrides, except TO which applies to FPU regs */
1009 if (result->oprs[operand].type & ~TO) {
1010 /*
1011 * we want to produce a warning iff the specified size
1012 * is different from the register size
1013 */
H. Peter Anvin68222142007-11-18 22:18:09 -08001014 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001016 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001018 result->oprs[operand].type &= TO;
1019 result->oprs[operand].type |= REGISTER;
1020 result->oprs[operand].type |= nasm_reg_flags[value->type];
Jin Kyu Song72018a22013-08-05 20:46:18 -07001021 result->oprs[operand].decoflags |= brace_flags;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001022 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023
H. Peter Anvin68222142007-11-18 22:18:09 -08001024 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001025 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 "register size specification ignored");
1027 }
1028 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001029 }
1030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001032
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001033 /* clear remaining operands */
1034 while (operand < MAX_OPERANDS)
1035 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001036
1037 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -07001038 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001039 */
1040 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 case I_RESW:
1042 result->opcode = I_RESB;
1043 result->oprs[0].offset *= 2;
1044 break;
1045 case I_RESD:
1046 result->opcode = I_RESB;
1047 result->oprs[0].offset *= 4;
1048 break;
1049 case I_RESQ:
1050 result->opcode = I_RESB;
1051 result->oprs[0].offset *= 8;
1052 break;
1053 case I_REST:
1054 result->opcode = I_RESB;
1055 result->oprs[0].offset *= 10;
1056 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001057 case I_RESO:
1058 result->opcode = I_RESB;
1059 result->oprs[0].offset *= 16;
1060 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001061 case I_RESY:
1062 result->opcode = I_RESB;
1063 result->oprs[0].offset *= 32;
1064 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001065 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001066 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001067 }
1068
1069 return result;
1070}
1071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001073{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001074 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001075 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001076 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001077
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001078 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001080 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001081
H. Peter Anvin76690a12002-04-30 20:52:49 +00001082 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001083}
1084
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001086{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001087 extop *e;
1088
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001089 while ((e = i->eops)) {
1090 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001091 if (e->type == EOT_DB_STRING_FREE)
1092 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001094 }
1095}