blob: abc1ea4096a458c8adefcdf9ee210145693a4635 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvin98578072018-06-01 18:02:54 -07003 * Copyright 1996-2018 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>
45
46#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000047#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.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 Anvinb20bc732017-03-07 19:23:03 -080054#include "assemble.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070055#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056
H. Peter Anvind0e365d2002-05-26 18:19:19 +000057
H. Peter Anvine2c80182005-01-15 22:15:51 +000058static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040062static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070063{
64 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080065 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030066 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070067 case R_CS:
68 case R_DS:
69 case R_SS:
70 case R_ES:
71 case R_FS:
72 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030073 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070074 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080075 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070076 case P_REP:
77 case P_REPE:
78 case P_REPZ:
79 case P_REPNE:
80 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080081 case P_XACQUIRE:
82 case P_XRELEASE:
Jin Kyu Song03041092013-10-15 19:38:51 -070083 case P_BND:
Jin Kyu Songb287ff02013-12-04 20:05:55 -080084 case P_NOBND:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080085 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070086 case P_O16:
87 case P_O32:
88 case P_O64:
89 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030090 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070091 case P_A16:
92 case P_A32:
93 case P_A64:
94 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030095 return PPS_ASIZE;
Jin Kyu Song945b1b82013-10-25 19:29:53 -070096 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -080097 case P_VEX3:
98 case P_VEX2:
99 return PPS_VEX;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700100 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700101 nasm_panic("Invalid value %d passed to prefix_slot()", prefix);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300102 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700103 }
104}
105
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700106static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700107{
108 if (tasm_compatible_mode) {
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800109 switch (tokval.t_integer) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300110 /* For TASM compatibility a size override inside the
111 * brackets changes the size of the operand, not the
112 * address type of the operand as it does in standard
113 * NASM syntax. Hence:
114 *
115 * mov eax,[DWORD val]
116 *
117 * is valid syntax in TASM compatibility mode. Note that
118 * you lose the ability to override the default address
119 * type for the instruction, but we never use anything
120 * but 32-bit flat model addressing in our code.
121 */
122 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700123 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300124 break;
125 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700126 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300127 break;
128 case S_DWORD:
129 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700130 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300131 break;
132 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700133 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300134 break;
135 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700136 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300137 break;
138 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700139 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300140 break;
141 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300142 nasm_nonfatal("invalid operand size specification");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300143 break;
144 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700145 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300146 /* Standard NASM compatible syntax */
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800147 switch (tokval.t_integer) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300148 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700149 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300150 break;
151 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700152 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300153 break;
154 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700155 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300156 break;
157 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700158 op->disp_size = 8;
159 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300160 break;
161 case P_A16:
162 case P_A32:
163 case P_A64:
164 if (result->prefixes[PPS_ASIZE] &&
165 result->prefixes[PPS_ASIZE] != tokval.t_integer)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300166 nasm_nonfatal("conflicting address size specifications");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300167 else
168 result->prefixes[PPS_ASIZE] = tokval.t_integer;
169 break;
170 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700171 op->disp_size = 16;
172 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300173 break;
174 case S_DWORD:
175 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700176 op->disp_size = 32;
177 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300178 break;
179 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700180 op->disp_size = 64;
181 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300182 break;
183 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300184 nasm_nonfatal("invalid size specification in"
185 " effective address");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300186 break;
187 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700188 }
189}
190
Jin Kyu Song72018a22013-08-05 20:46:18 -0700191/*
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700192 * Brace decorators are are parsed here. opmask and zeroing
193 * decorators can be placed in any order. e.g. zmm1 {k2}{z} or zmm2
194 * {z}{k3} decorator(s) are placed at the end of an operand.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700195 */
196static bool parse_braces(decoflags_t *decoflags)
197{
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700198 int i, j;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700199
200 i = tokval.t_type;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700201
202 while (true) {
203 switch (i) {
204 case TOKEN_OPMASK:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700205 if (*decoflags & OPMASK_MASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300206 nasm_nonfatal("opmask k%"PRIu64" is already set",
207 *decoflags & OPMASK_MASK);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700208 *decoflags &= ~OPMASK_MASK;
209 }
210 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700211 break;
212 case TOKEN_DECORATOR:
213 j = tokval.t_integer;
214 switch (j) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700215 case BRC_Z:
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700216 *decoflags |= Z_MASK;
217 break;
218 case BRC_1TO2:
219 case BRC_1TO4:
220 case BRC_1TO8:
221 case BRC_1TO16:
222 *decoflags |= BRDCAST_MASK | VAL_BRNUM(j - BRC_1TO2);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700223 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700224 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300225 nasm_nonfatal("{%s} is not an expected decorator",
226 tokval.t_charptr);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700227 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700228 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700229 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700230 case ',':
231 case TOKEN_EOS:
232 return false;
233 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300234 nasm_nonfatal("only a series of valid decorators expected");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700235 return true;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700236 }
237 i = stdscan(NULL, &tokval);
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700238 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700239}
240
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700241static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700242{
243 int b, i, s; /* basereg, indexreg, scale */
244 int64_t o; /* offset */
245
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700246 b = i = -1;
247 o = s = 0;
H. Peter Anvin164d2462017-02-20 02:39:56 -0800248 op->segment = op->wrt = NO_SEG;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700249
250 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
251 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
252
253 if (is_gpr && e->value == 1)
254 b = e->type; /* It can be basereg */
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700255 else /* No, it has to be indexreg */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700256 i = e->type, s = e->value;
257 e++;
258 }
259 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
260 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
261
262 if (b != -1) /* If the first was the base, ... */
263 i = e->type, s = e->value; /* second has to be indexreg */
264
265 else if (!is_gpr || e->value != 1) {
266 /* If both want to be index */
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300267 nasm_nonfatal("invalid effective address: two index registers");
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700268 return -1;
269 } else
270 b = e->type;
271 e++;
272 }
H. Peter Anvin164d2462017-02-20 02:39:56 -0800273
274 if (e->type) { /* is there an offset? */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700275 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300276 nasm_nonfatal("invalid effective address: impossible register");
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700277 return -1;
278 } else {
279 if (e->type == EXPR_UNKNOWN) {
280 op->opflags |= OPFLAG_UNKNOWN;
281 o = 0; /* doesn't matter what */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700282 while (e->type)
283 e++; /* go to the end of the line */
284 } else {
285 if (e->type == EXPR_SIMPLE) {
286 o = e->value;
287 e++;
288 }
289 if (e->type == EXPR_WRT) {
290 op->wrt = e->value;
291 e++;
H. Peter Anvin164d2462017-02-20 02:39:56 -0800292 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700293 /*
294 * Look for a segment base type.
295 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800296 for (; e->type; e++) {
297 if (!e->value)
298 continue;
299
300 if (e->type <= EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300301 nasm_nonfatal("invalid effective address: too many registers");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800302 return -1;
303 } else if (e->type < EXPR_SEGBASE) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300304 nasm_nonfatal("invalid effective address: bad subexpression type");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800305 return -1;
306 } else if (e->value == 1) {
307 if (op->segment != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300308 nasm_nonfatal("invalid effective address: multiple base segments");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800309 return -1;
310 }
311 op->segment = e->type - EXPR_SEGBASE;
312 } else if (e->value == -1 &&
313 e->type == location.segment + EXPR_SEGBASE &&
314 !(op->opflags & OPFLAG_RELATIVE)) {
315 op->opflags |= OPFLAG_RELATIVE;
316 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300317 nasm_nonfatal("invalid effective address: impossible segment base multiplier");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800318 return -1;
319 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700320 }
321 }
322 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700323 }
324
H. Peter Anvin164d2462017-02-20 02:39:56 -0800325 nasm_assert(!e->type); /* We should be at the end */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700326
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700327 op->basereg = b;
328 op->indexreg = i;
329 op->scale = s;
330 op->offset = o;
331 return 0;
332}
333
334static void mref_set_optype(operand *op)
335{
336 int b = op->basereg;
337 int i = op->indexreg;
338 int s = op->scale;
339
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700340 /* It is memory, but it can match any r/m operand */
341 op->type |= MEMORY_ANY;
342
343 if (b == -1 && (i == -1 || s == 0)) {
344 int is_rel = globalbits == 64 &&
345 !(op->eaflags & EAF_ABS) &&
346 ((globalrel &&
347 !(op->eaflags & EAF_FSGS)) ||
348 (op->eaflags & EAF_REL));
349
350 op->type |= is_rel ? IP_REL : MEM_OFFS;
351 }
352
353 if (i != -1) {
354 opflags_t iclass = nasm_reg_flags[i];
355
356 if (is_class(XMMREG,iclass))
357 op->type |= XMEM;
358 else if (is_class(YMMREG,iclass))
359 op->type |= YMEM;
360 else if (is_class(ZMMREG,iclass))
361 op->type |= ZMEM;
362 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700363}
364
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700365/*
366 * Convert an expression vector returned from evaluate() into an
367 * extop structure. Return zero on success.
368 */
369static int value_to_extop(expr * vect, extop *eop, int32_t myseg)
370{
371 eop->type = EOT_DB_NUMBER;
372 eop->offset = 0;
373 eop->segment = eop->wrt = NO_SEG;
374 eop->relative = false;
375
376 for (; vect->type; vect++) {
377 if (!vect->value) /* zero term, safe to ignore */
378 continue;
379
Cyrill Gorcunovfd610f22016-11-28 23:57:08 +0300380 if (vect->type <= EXPR_REG_END) /* false if a register is present */
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700381 return -1;
382
383 if (vect->type == EXPR_UNKNOWN) /* something we can't resolve yet */
384 return 0;
385
386 if (vect->type == EXPR_SIMPLE) {
387 /* Simple number expression */
388 eop->offset += vect->value;
389 continue;
390 }
391 if (eop->wrt == NO_SEG && !eop->relative && vect->type == EXPR_WRT) {
392 /* WRT term */
393 eop->wrt = vect->value;
394 continue;
395 }
396
H. Peter Anvind97ccee2017-02-21 11:31:35 -0800397 if (!eop->relative &&
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700398 vect->type == EXPR_SEGBASE + myseg && vect->value == -1) {
399 /* Expression of the form: foo - $ */
400 eop->relative = true;
401 continue;
402 }
403
404 if (eop->segment == NO_SEG && vect->type >= EXPR_SEGBASE &&
405 vect->value == 1) {
406 eop->segment = vect->type - EXPR_SEGBASE;
407 continue;
408 }
409
410 /* Otherwise, badness */
411 return -1;
412 }
413
414 /* We got to the end and it was all okay */
415 return 0;
416}
417
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800418insn *parse_line(char *buffer, insn *result)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000419{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400420 bool insn_is_label = false;
421 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700422 int opnum;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800423 bool critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800424 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700425 bool recover;
Martin Lindhe58f37c12016-11-16 16:43:16 +0100426 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000427
H. Peter Anvin7daa26f2018-06-02 23:48:16 -0700428 nasm_static_assert(P_none == 0);
429
H. Peter Anvin9c987692007-11-04 21:09:32 -0800430restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400431 first = true;
432 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000433
H. Peter Anvin76690a12002-04-30 20:52:49 +0000434 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300435 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000436 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000437
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700438 memset(result->prefixes, P_none, sizeof(result->prefixes));
439 result->times = 1; /* No TIMES either yet */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400440 result->label = NULL; /* Assume no label */
441 result->eops = NULL; /* must do this, whatever happens */
442 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700443 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
444 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000445
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400446 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700447 if (i == TOKEN_EOS)
448 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400449
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400450 if (i != TOKEN_ID &&
451 i != TOKEN_INSN &&
452 i != TOKEN_PREFIX &&
453 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300454 nasm_nonfatal("label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700455 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000456 }
457
H. Peter Anvin9c987692007-11-04 21:09:32 -0800458 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
459 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300460 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 result->label = tokval.t_charptr;
462 i = stdscan(NULL, &tokval);
463 if (i == ':') { /* skip over the optional colon */
464 i = stdscan(NULL, &tokval);
465 } else if (i == 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800466 /*!
467 *!orphan-labels [on] labels alone on lines without trailing `:'
468 *! warns about source lines which contain no instruction but define
469 *! a label without a trailing colon. This is most likely indicative
470 *! of a typo, but is technically correct NASM syntax (see \k{syntax}.)
471 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800472 nasm_warn(WARN_ORPHAN_LABELS ,
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300473 "label alone on a line without a colon might be in error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 }
475 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
476 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800477 * FIXME: location.segment could be NO_SEG, in which case
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800478 * it is possible we should be passing 'absolute.segment'. Look into this.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 * Work out whether that is *really* what we should be doing.
480 * Generally fix things. I think this is right as it is, but
481 * am still not certain.
482 */
H. Peter Anvin (Intel)415b6b32018-06-25 14:09:52 -0700483 define_label(result->label,
484 in_absolute ? absolute.segment : location.segment,
H. Peter Anvin98578072018-06-01 18:02:54 -0700485 location.offset, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000487 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000488
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400489 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700490 if (i == TOKEN_EOS)
491 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000493 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400494 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300495 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800496
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 /*
498 * Handle special case: the TIMES prefix.
499 */
500 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
501 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000502
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 i = stdscan(NULL, &tokval);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800504 value = evaluate(stdscan, NULL, &tokval, NULL, pass_stable(), NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700506 if (!value) /* Error in evaluator */
507 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 if (!is_simple(value)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300509 nasm_nonfatal("non-constant argument supplied to TIMES");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 result->times = 1L;
511 } else {
512 result->times = value->value;
H. Peter Anvin94ead272017-09-27 15:22:23 -0700513 if (value->value < 0) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300514 nasm_nonfatalf(ERR_PASS2, "TIMES value %"PRId64" is negative", value->value);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 result->times = 0;
516 }
517 }
518 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300519 int slot = prefix_slot(tokval.t_integer);
520 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700521 if (result->prefixes[slot] == tokval.t_integer)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800522 nasm_warn(WARN_OTHER, "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700523 else
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300524 nasm_nonfatal("instruction has conflicting prefixes");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300525 }
526 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527 i = stdscan(NULL, &tokval);
528 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000529 }
530
531 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300532 int j;
533 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700534
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400535 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300536 if ((pfx = result->prefixes[j]) != P_none)
537 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400538 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700539
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700540 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 /*
542 * Instruction prefixes are present, but no actual
543 * instruction. This is allowed: at this point we
544 * invent a notional instruction of RESB 0.
545 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400546 result->opcode = I_RESB;
547 result->operands = 1;
H. Peter Anvin1980abf2017-03-31 14:52:03 -0700548 nasm_zero(result->oprs);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400549 result->oprs[0].type = IMMEDIATE;
550 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
552 return result;
553 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300554 nasm_nonfatal("parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700555 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000557 }
558
559 result->opcode = tokval.t_integer;
560 result->condition = tokval.t_inttwo;
561
562 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700563 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000564 * evaluated operands, since the correct values _must_ be known
565 * on the first pass. Hence, even in pass one, we set the
566 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700567 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000568 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800569 critical = pass_final() || (result->opcode == I_INCBIN);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000570
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700571 if (opcode_is_db(result->opcode) || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 extop *eop, **tail = &result->eops, **fixptr;
573 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300574 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000575
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700576 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000577
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700579 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 */
581 while (1) {
582 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400583 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300585 else if (first && i == ':') {
586 insn_is_label = true;
587 goto restart_parse;
588 }
589 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 fixptr = tail;
591 eop = *tail = nasm_malloc(sizeof(extop));
592 tail = &eop->next;
593 eop->next = NULL;
594 eop->type = EOT_NOTHING;
595 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300596 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300598 /*
599 * is_comma_next() here is to distinguish this from
600 * a string used as part of an expression...
601 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700602 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400603 eop->type = EOT_DB_STRING;
604 eop->stringval = tokval.t_charptr;
605 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300607 } else if (i == TOKEN_STRFUNC) {
608 bool parens = false;
609 const char *funcname = tokval.t_charptr;
610 enum strfunc func = tokval.t_integer;
611 i = stdscan(NULL, &tokval);
612 if (i == '(') {
613 parens = true;
614 i = stdscan(NULL, &tokval);
615 }
616 if (i != TOKEN_STR) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300617 nasm_nonfatal("%s must be followed by a string constant",
618 funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300619 eop->type = EOT_NOTHING;
620 } else {
621 eop->type = EOT_DB_STRING_FREE;
622 eop->stringlen =
623 string_transform(tokval.t_charptr, tokval.t_inttwo,
624 &eop->stringval, func);
625 if (eop->stringlen == (size_t)-1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300626 nasm_nonfatal("invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300627 eop->type = EOT_NOTHING;
628 }
629 }
630 if (parens && i && i != ')') {
631 i = stdscan(NULL, &tokval);
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300632 if (i != ')')
633 nasm_nonfatal("unterminated %s function", funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300634 }
635 if (i && i != ',')
636 i = stdscan(NULL, &tokval);
637 } else if (i == '-' || i == '+') {
638 char *save = stdscan_get();
639 int token = i;
640 sign = (i == '-') ? -1 : 1;
641 i = stdscan(NULL, &tokval);
642 if (i != TOKEN_FLOAT) {
643 stdscan_set(save);
644 i = tokval.t_type = token;
645 goto is_expression;
646 } else {
647 goto is_float;
648 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700649 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300650is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300651 eop->type = EOT_DB_STRING;
652 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300653
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700654 eop->stringlen = db_bytes(result->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300655 if (eop->stringlen > 16) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300656 nasm_nonfatal("floating-point constant"
657 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300658 eop->stringlen = 0;
659 } else if (eop->stringlen < 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300660 nasm_nonfatal("floating-point constant"
661 " encountered in unknown instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300662 /*
663 * fix suggested by Pedro Gimeno... original line was:
664 * eop->type = EOT_NOTHING;
665 */
666 eop->stringlen = 0;
667 }
668
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300669 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
670 tail = &eop->next;
671 *fixptr = eop;
672 eop->stringval = (char *)eop + sizeof(extop);
673 if (!eop->stringlen ||
674 !float_const(tokval.t_charptr, sign,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800675 (uint8_t *)eop->stringval, eop->stringlen))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300676 eop->type = EOT_NOTHING;
677 i = stdscan(NULL, &tokval); /* eat the comma */
678 } else {
679 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700681
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300682is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800684 critical, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700686 if (!value) /* Error in evaluator */
687 goto fail;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700688 if (value_to_extop(value, eop, location.segment)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300689 nasm_nonfatal("operand %d: expression is not simple or relocatable",
690 oper_num);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 }
692 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000693
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694 /*
695 * We're about to call stdscan(), which will eat the
696 * comma that we're currently sitting on between
697 * arguments. However, we'd better check first that it
698 * _is_ a comma.
699 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400700 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701 break;
702 if (i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300703 nasm_nonfatal("comma expected after operand %d", oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700704 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 }
706 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000707
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 if (result->opcode == I_INCBIN) {
709 /*
710 * Correct syntax for INCBIN is that there should be
711 * one string operand, followed by one or two numeric
712 * operands.
713 */
714 if (!result->eops || result->eops->type != EOT_DB_STRING)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300715 nasm_nonfatal("`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000716 else if (result->eops->next &&
717 result->eops->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300718 nasm_nonfatal("`incbin': second parameter is"
719 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 else if (result->eops->next && result->eops->next->next &&
721 result->eops->next->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300722 nasm_nonfatal("`incbin': third parameter is"
723 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000724 else if (result->eops->next && result->eops->next->next &&
725 result->eops->next->next->next)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300726 nasm_nonfatal("`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000727 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 return result;
729 /*
730 * If we reach here, one of the above errors happened.
731 * Throw the instruction away.
732 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700733 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000734 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800735 nasm_warn(WARN_OTHER, "no operand for data declaration");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 else
737 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000738
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000740 }
741
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400742 /*
743 * Now we begin to parse the operands. There may be up to four
744 * of these, separated by commas, and terminated by a zero token.
745 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700747 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
748 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300749 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700750 bool mref; /* is this going to be a memory ref? */
751 bool bracket; /* is it a [] mref, or a & mref? */
752 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000753 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700754 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000755
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700756 op->disp_size = 0; /* have to zero this whatever */
757 op->eaflags = 0; /* and this */
758 op->opflags = 0;
759 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000760
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400762 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300764 else if (first && i == ':') {
765 insn_is_label = true;
766 goto restart_parse;
767 }
768 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700769 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 while (i == TOKEN_SPECIAL) { /* size specifiers */
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800771 switch (tokval.t_integer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 case S_BYTE:
773 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700774 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 setsize = 1;
776 break;
777 case S_WORD:
778 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700779 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 setsize = 1;
781 break;
782 case S_DWORD:
783 case S_LONG:
784 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700785 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 setsize = 1;
787 break;
788 case S_QWORD:
789 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700790 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 setsize = 1;
792 break;
793 case S_TWORD:
794 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700795 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 setsize = 1;
797 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700798 case S_OWORD:
799 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700800 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700801 setsize = 1;
802 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700803 case S_YWORD:
804 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700805 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700806 setsize = 1;
807 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700808 case S_ZWORD:
809 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700810 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700811 setsize = 1;
812 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700814 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 break;
816 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700817 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 break;
819 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700820 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 break;
822 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700823 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 break;
825 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700826 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 break;
828 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300829 nasm_nonfatal("invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 }
831 i = stdscan(NULL, &tokval);
832 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700835 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700837 i = stdscan(NULL, &tokval); /* then skip the colon */
838 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700839 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700840 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700842 /* when a comma follows an opening bracket - [ , eax*4] */
843 if (i == ',') {
844 /* treat as if there is a zero displacement virtually */
845 tokval.t_type = TOKEN_NUM;
846 tokval.t_integer = 0;
847 stdscan_set(stdscan_get() - 1); /* rewind the comma */
848 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700850 mref = false;
851 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000853
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700854 if ((op->type & FAR) && !mref &&
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300855 result->opcode != I_JMP && result->opcode != I_CALL)
856 nasm_nonfatal("invalid use of FAR operand specifier");
Debbie Wiles63b53f72002-06-04 19:31:24 +0000857
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800859 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700861 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700862 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700864 if (!value) /* Error in evaluator */
865 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 if (i == ':' && mref) { /* it was seg:offset */
867 /*
868 * Process the segment override.
869 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400870 if (value[1].type != 0 ||
871 value->value != 1 ||
872 !IS_SREG(value->type))
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300873 nasm_nonfatal("invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700874 else if (result->prefixes[PPS_SEG])
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300875 nasm_nonfatal("instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000876 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300877 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400878 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700879 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300880 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000881
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700883 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700884 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 i = stdscan(NULL, &tokval);
886 }
887 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800888 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700890 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700891 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 }
893 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700894 if (!value) /* Error in evaluator */
895 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200897
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700898 mib = false;
899 if (mref && bracket && i == ',') {
900 /* [seg:base+offset,index*scale] syntax (mib) */
901
902 operand o1, o2; /* Partial operands */
903
904 if (parse_mref(&o1, value))
905 goto fail;
906
907 i = stdscan(NULL, &tokval); /* Eat comma */
908 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800909 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700910 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +0300911 if (!value)
912 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700913
914 if (parse_mref(&o2, value))
915 goto fail;
916
917 if (o2.basereg != -1 && o2.indexreg == -1) {
918 o2.indexreg = o2.basereg;
919 o2.scale = 1;
920 o2.basereg = -1;
921 }
922
923 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
924 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300925 nasm_nonfatal("invalid mib expression");
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700926 goto fail;
927 }
928
929 op->basereg = o1.basereg;
930 op->indexreg = o2.indexreg;
931 op->scale = o2.scale;
932 op->offset = o1.offset;
933 op->segment = o1.segment;
934 op->wrt = o1.wrt;
935
936 if (op->basereg != -1) {
937 op->hintbase = op->basereg;
938 op->hinttype = EAH_MAKEBASE;
939 } else if (op->indexreg != -1) {
940 op->hintbase = op->indexreg;
941 op->hinttype = EAH_NOTBASE;
942 } else {
943 op->hintbase = -1;
944 op->hinttype = EAH_NOHINT;
945 }
946
947 mib = true;
948 }
949
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700950 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 if (mref && bracket) { /* find ] at the end */
952 if (i != ']') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300953 nasm_nonfatal("parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200954 recover = true;
955 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 i = stdscan(NULL, &tokval);
H. Peter Anvinc33d95f2017-03-31 14:37:24 -0700957 if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
958 /* parse opmask (and zeroing) after an operand */
959 recover = parse_braces(&brace_flags);
960 i = tokval.t_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700961 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200962 if (i != 0 && i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300963 nasm_nonfatal("comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200964 recover = true;
965 }
966 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700968 if (i != 0 && i != ',' && i != ':' &&
969 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300970 nasm_nonfatal("comma, colon, decorator or end of "
971 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200972 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700974 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700975 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
976 /* parse opmask (and zeroing) after an operand */
977 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 }
979 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200980 if (recover) {
981 do { /* error recovery */
982 i = stdscan(NULL, &tokval);
983 } while (i != 0 && i != ',');
984 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000985
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300986 /*
987 * now convert the exprs returned from evaluate()
988 * into operand descriptions...
989 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700990 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700993 /* A mib reference was fully parsed already */
994 if (!mib) {
995 if (parse_mref(op, value))
996 goto fail;
997 op->hintbase = hints.base;
998 op->hinttype = hints.type;
999 }
1000 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001003 op->type |= IMMEDIATE;
1004 op->opflags |= OPFLAG_UNKNOWN;
1005 op->offset = 0; /* don't care */
1006 op->segment = NO_SEG; /* don't care again */
1007 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001008
Chang S. Baea5786342018-08-15 23:22:21 +03001009 if(optimizing.level >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001010 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001011 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001012 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001013 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvin87646092017-02-28 17:44:24 -08001015 uint64_t n = reloc_value(value);
1016
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001017 op->type |= IMMEDIATE;
H. Peter Anvin87646092017-02-28 17:44:24 -08001018 op->offset = n;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001019 op->segment = reloc_seg(value);
1020 op->wrt = reloc_wrt(value);
H. Peter Anvin164d2462017-02-20 02:39:56 -08001021 op->opflags |= is_self_relative(value) ? OPFLAG_RELATIVE : 0;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001024 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001025 op->type |= UNITY;
Chang S. Baea5786342018-08-15 23:22:21 +03001026 if (optimizing.level >= 0 && !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001027 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001028 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001029 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001030 op->type |= SBYTEWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001031 if (n <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 op->type |= UDWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001033 if (n + UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001034 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 }
1036 }
H. Peter Anvin164d2462017-02-20 02:39:56 -08001037 } else if (value->type == EXPR_RDSAE) {
Jin Kyu Song72018a22013-08-05 20:46:18 -07001038 /*
1039 * it's not an operand but a rounding or SAE decorator.
1040 * put the decorator information in the (opflag_t) type field
1041 * of previous operand.
1042 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001043 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001044 switch (value->value) {
1045 case BRC_RN:
1046 case BRC_RU:
1047 case BRC_RD:
1048 case BRC_RZ:
1049 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001050 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001051 result->evex_rm = value->value;
1052 break;
1053 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001054 nasm_nonfatal("invalid decorator");
Jin Kyu Song72018a22013-08-05 20:46:18 -07001055 break;
1056 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001058 opflags_t rs;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001059 uint64_t regset_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 if (value->type >= EXPR_SIMPLE || value->value != 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001062 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001063 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 /*
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001067 * We do not allow any kind of expression, except for
1068 * reg+value in which case it is a register set.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 */
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001070 for (i = 1; value[i].type; i++) {
1071 if (!value[i].value)
1072 continue;
1073
1074 switch (value[i].type) {
1075 case EXPR_SIMPLE:
1076 if (!regset_size) {
1077 regset_size = value[i].value + 1;
1078 break;
1079 }
1080 /* fallthrough */
1081 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001082 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001083 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 }
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001085 }
1086
1087 if ((regset_size & (regset_size - 1)) ||
1088 regset_size >= (UINT64_C(1) << REGSET_BITS)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001089 nasm_nonfatalf(ERR_PASS2, "invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001090 regset_size = 0;
1091 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001094 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 /*
1096 * we want to produce a warning iff the specified size
1097 * is different from the register size
1098 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001099 rs = op->type & SIZE_MASK;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001100 } else {
H. Peter Anvin68222142007-11-18 22:18:09 -08001101 rs = 0;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001102 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001104 /*
1105 * Make sure we're not out of nasm_reg_flags, still
1106 * probably this should be fixed when we're defining
1107 * the label.
1108 *
1109 * An easy trigger is
1110 *
1111 * e equ 0x80000000:0
1112 * pshufw word e-0
1113 *
1114 */
1115 if (value->type < EXPR_REG_START ||
1116 value->type > EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001117 nasm_nonfatal("invalid operand type");
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001118 goto fail;
1119 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001121 op->type &= TO;
1122 op->type |= REGISTER;
1123 op->type |= nasm_reg_flags[value->type];
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001124 op->type |= (regset_size >> 1) << REGSET_SHIFT;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001125 op->decoflags |= brace_flags;
1126 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001128 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001129 nasm_warn(WARN_OTHER, "register size specification ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001130 }
1131 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001132
1133 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001134 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1135 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001136 }
1137
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001138 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001139
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001140 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001141 while (opnum < MAX_OPERANDS)
1142 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001143
1144 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001145 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001146 */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001147 if (opcode_is_resb(result->opcode)) {
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -07001148 result->oprs[0].offset *= resb_bytes(result->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001149 result->oprs[0].offset *= result->times;
1150 result->times = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 result->opcode = I_RESB;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001152 }
1153
1154 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001155
1156fail:
1157 result->opcode = I_none;
1158 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001159}
1160
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001162{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001163 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001164 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001165 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001166
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001167 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001169 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001170
H. Peter Anvin76690a12002-04-30 20:52:49 +00001171 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001172}
1173
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001175{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001176 extop *e;
1177
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001178 while ((e = i->eops)) {
1179 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001180 if (e->type == EOT_DB_STRING_FREE)
1181 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001183 }
1184}