blob: 585abe207da311740d541098afd5e5ea726bee3a [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;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700663 case S_ZWORD:
664 if (!setsize)
665 result->oprs[operand].type |= BITS512;
666 setsize = 1;
667 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 case S_TO:
669 result->oprs[operand].type |= TO;
670 break;
671 case S_STRICT:
672 result->oprs[operand].type |= STRICT;
673 break;
674 case S_FAR:
675 result->oprs[operand].type |= FAR;
676 break;
677 case S_NEAR:
678 result->oprs[operand].type |= NEAR;
679 break;
680 case S_SHORT:
681 result->oprs[operand].type |= SHORT;
682 break;
683 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700684 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 }
686 i = stdscan(NULL, &tokval);
687 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000688
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700690 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700692 i = stdscan(NULL, &tokval); /* then skip the colon */
693 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300694 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700695 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 }
697 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700698 mref = false;
699 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000701
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 if ((result->oprs[operand].type & FAR) && !mref &&
703 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700704 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000706
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 value = evaluate(stdscan, NULL, &tokval,
708 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700709 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 i = tokval.t_type;
711 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700712 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700714 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400715 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 return result; /* ignore this instruction */
717 }
718 if (i == ':' && mref) { /* it was seg:offset */
719 /*
720 * Process the segment override.
721 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400722 if (value[1].type != 0 ||
723 value->value != 1 ||
724 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700725 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700726 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700727 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700728 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000729 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300730 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400731 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300732 result->oprs[operand].eaflags |= EAF_FSGS;
733 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000734
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700736 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300737 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 i = stdscan(NULL, &tokval);
739 }
740 value = evaluate(stdscan, NULL, &tokval,
741 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700742 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 i = tokval.t_type;
744 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700745 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000746 }
747 /* and get the offset */
748 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400749 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 return result; /* ignore this instruction */
751 }
752 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200753
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700754 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 if (mref && bracket) { /* find ] at the end */
756 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700757 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200758 recover = true;
759 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700761 if ((i == TOKEN_DECORATOR) || (i == TOKEN_OPMASK)) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700762 /*
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700763 * according to AVX512 spec, broacast or opmask decorator
764 * is expected for memory reference operands
Jin Kyu Song72018a22013-08-05 20:46:18 -0700765 */
766 if (tokval.t_flag & TFLAG_BRDCAST) {
767 brace_flags |= GEN_BRDCAST(0);
768 i = stdscan(NULL, &tokval);
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700769 } else if (i == TOKEN_OPMASK) {
770 brace_flags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
771 i = stdscan(NULL, &tokval);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700772 } else {
Jin Kyu Song50ab1522013-08-21 19:29:12 -0700773 nasm_error(ERR_NONFATAL, "broadcast or opmask "
774 "decorator expected inside braces");
Jin Kyu Song72018a22013-08-05 20:46:18 -0700775 recover = true;
776 }
777 }
778
Victor van den Elzen02846d32009-06-23 03:47:07 +0200779 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700780 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200781 recover = true;
782 }
783 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700785 if (i != 0 && i != ',' && i != ':' &&
786 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
787 nasm_error(ERR_NONFATAL, "comma, colon, decorator or end of "
788 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200789 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 } else if (i == ':') {
791 result->oprs[operand].type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700792 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
793 /* parse opmask (and zeroing) after an operand */
794 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 }
796 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200797 if (recover) {
798 do { /* error recovery */
799 i = stdscan(NULL, &tokval);
800 } while (i != 0 && i != ',');
801 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000802
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300803 /*
804 * now convert the exprs returned from evaluate()
805 * into operand descriptions...
806 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000807
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 if (mref) { /* it's a memory reference */
809 expr *e = value;
810 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300811 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000812
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 b = i = -1, o = s = 0;
814 result->oprs[operand].hintbase = hints.base;
815 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000816
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700818 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
819
820 if (is_gpr && e->value == 1)
821 b = e->type; /* It can be basereg */
822 else /* No, it has to be indexreg */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 i = e->type, s = e->value;
824 e++;
825 }
826 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700827 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
828
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 if (b != -1) /* If the first was the base, ... */
830 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000831
H. Peter Anvine20ca022013-07-19 17:06:08 -0700832 else if (!is_gpr || e->value != 1) {
833 /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700834 nasm_error(ERR_NONFATAL,
H. Peter Anvine20ca022013-07-19 17:06:08 -0700835 "invalid effective address: two index registers");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400836 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 return result;
838 } else
839 b = e->type;
840 e++;
841 }
842 if (e->type != 0) { /* is there an offset? */
843 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700844 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400846 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 return result;
848 } else {
849 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100850 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 o = 0; /* doesn't matter what */
852 result->oprs[operand].wrt = NO_SEG; /* nor this */
853 result->oprs[operand].segment = NO_SEG; /* or this */
854 while (e->type)
855 e++; /* go to the end of the line */
856 } else {
857 if (e->type == EXPR_SIMPLE) {
858 o = e->value;
859 e++;
860 }
861 if (e->type == EXPR_WRT) {
862 result->oprs[operand].wrt = e->value;
863 e++;
864 } else
865 result->oprs[operand].wrt = NO_SEG;
866 /*
867 * Look for a segment base type.
868 */
869 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700870 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 "beroset-p-630-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 while (e->type && e->value == 0)
876 e++;
877 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700878 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400880 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 return result;
882 }
883 if (e->type) {
884 result->oprs[operand].segment =
885 e->type - EXPR_SEGBASE;
886 e++;
887 } else
888 result->oprs[operand].segment = NO_SEG;
889 while (e->type && e->value == 0)
890 e++;
891 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700892 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400894 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 return result;
896 }
897 }
898 }
899 } else {
900 o = 0;
901 result->oprs[operand].wrt = NO_SEG;
902 result->oprs[operand].segment = NO_SEG;
903 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000904
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700906 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400908 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 return result;
910 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000911
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300912 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700913 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000914
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300915 if (b == -1 && (i == -1 || s == 0)) {
916 int is_rel = globalbits == 64 &&
917 !(result->oprs[operand].eaflags & EAF_ABS) &&
918 ((globalrel &&
919 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
920 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000921
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300922 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
923 }
H. Peter Anvine20ca022013-07-19 17:06:08 -0700924
925 if (i != -1) {
926 opflags_t iclass = nasm_reg_flags[i];
927
928 if (is_class(XMMREG,iclass))
929 result->oprs[operand].type |= XMEM;
930 else if (is_class(YMMREG,iclass))
931 result->oprs[operand].type |= YMEM;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700932 else if (is_class(ZMMREG,iclass))
933 result->oprs[operand].type |= ZMEM;
H. Peter Anvine20ca022013-07-19 17:06:08 -0700934 }
935
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 result->oprs[operand].basereg = b;
937 result->oprs[operand].indexreg = i;
938 result->oprs[operand].scale = s;
939 result->oprs[operand].offset = o;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700940 result->oprs[operand].decoflags |= brace_flags;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400943 result->oprs[operand].type |= IMMEDIATE;
944 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
945 result->oprs[operand].offset = 0; /* don't care */
946 result->oprs[operand].segment = NO_SEG; /* don't care again */
947 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100948
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400949 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300950 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700951 result->oprs[operand].type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400952 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300953 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400955 result->oprs[operand].type |= IMMEDIATE;
956 result->oprs[operand].offset = reloc_value(value);
957 result->oprs[operand].segment = reloc_seg(value);
958 result->oprs[operand].wrt = reloc_wrt(value);
959
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400961 uint64_t n = reloc_value(value);
962 if (n == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 result->oprs[operand].type |= UNITY;
964 if (optimizing >= 0 &&
965 !(result->oprs[operand].type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400966 if ((uint32_t) (n + 128) <= 255)
967 result->oprs[operand].type |= SBYTEDWORD;
968 if ((uint16_t) (n + 128) <= 255)
969 result->oprs[operand].type |= SBYTEWORD;
970 if (n <= 0xFFFFFFFF)
971 result->oprs[operand].type |= UDWORD;
972 if (n + 0x80000000 <= 0xFFFFFFFF)
973 result->oprs[operand].type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 }
975 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700976 } else if(value->type == EXPR_RDSAE) {
977 /*
978 * it's not an operand but a rounding or SAE decorator.
979 * put the decorator information in the (opflag_t) type field
980 * of previous operand.
981 */
982 operand --;
983 switch (value->value) {
984 case BRC_RN:
985 case BRC_RU:
986 case BRC_RD:
987 case BRC_RZ:
988 case BRC_SAE:
989 result->oprs[operand].decoflags |=
990 (value->value == BRC_SAE ? SAE : ER);
991 result->evex_rm = value->value;
992 break;
993 default:
994 nasm_error(ERR_NONFATAL, "invalid decorator");
995 break;
996 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +0400998 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001001 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +04001002 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 return result;
1004 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001005
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 /*
1007 * check that its only 1 register, not an expression...
1008 */
1009 for (i = 1; value[i].type; i++)
1010 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001011 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +04001012 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 return result;
1014 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001015
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 /* clear overrides, except TO which applies to FPU regs */
1017 if (result->oprs[operand].type & ~TO) {
1018 /*
1019 * we want to produce a warning iff the specified size
1020 * is different from the register size
1021 */
H. Peter Anvin68222142007-11-18 22:18:09 -08001022 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 } else
H. Peter Anvin68222142007-11-18 22:18:09 -08001024 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001026 result->oprs[operand].type &= TO;
1027 result->oprs[operand].type |= REGISTER;
1028 result->oprs[operand].type |= nasm_reg_flags[value->type];
Jin Kyu Song72018a22013-08-05 20:46:18 -07001029 result->oprs[operand].decoflags |= brace_flags;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001030 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031
H. Peter Anvin68222142007-11-18 22:18:09 -08001032 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -07001033 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 "register size specification ignored");
1035 }
1036 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001037 }
1038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001040
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001041 /* clear remaining operands */
1042 while (operand < MAX_OPERANDS)
1043 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001044
1045 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -07001046 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001047 */
1048 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 case I_RESW:
1050 result->opcode = I_RESB;
1051 result->oprs[0].offset *= 2;
1052 break;
1053 case I_RESD:
1054 result->opcode = I_RESB;
1055 result->oprs[0].offset *= 4;
1056 break;
1057 case I_RESQ:
1058 result->opcode = I_RESB;
1059 result->oprs[0].offset *= 8;
1060 break;
1061 case I_REST:
1062 result->opcode = I_RESB;
1063 result->oprs[0].offset *= 10;
1064 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -07001065 case I_RESO:
1066 result->opcode = I_RESB;
1067 result->oprs[0].offset *= 16;
1068 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -07001069 case I_RESY:
1070 result->opcode = I_RESB;
1071 result->oprs[0].offset *= 32;
1072 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -07001073 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001074 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001075 }
1076
1077 return result;
1078}
1079
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001081{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001082 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001083 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001084 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001085
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001086 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001088 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001089
H. Peter Anvin76690a12002-04-30 20:52:49 +00001090 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001091}
1092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001094{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001095 extop *e;
1096
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001097 while ((e = i->eops)) {
1098 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001099 if (e->type == EOT_DB_STRING_FREE)
1100 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001102 }
1103}