blob: 451ffa672f6d1783c717514d4f3f98c11011fbce [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>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000043#include <ctype.h>
44
45#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000046#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080048#include "error.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000049#include "stdscan.h"
H. Peter Anvin00444ae2009-07-18 18:49:55 -070050#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051#include "parser.h"
52#include "float.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080053#include "assemble.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055
H. Peter Anvind0e365d2002-05-26 18:19:19 +000056
H. Peter Anvine2c80182005-01-15 22:15:51 +000057static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000059static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040061static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070062{
63 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080064 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030065 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070066 case R_CS:
67 case R_DS:
68 case R_SS:
69 case R_ES:
70 case R_FS:
71 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030072 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070073 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080074 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070075 case P_REP:
76 case P_REPE:
77 case P_REPZ:
78 case P_REPNE:
79 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080080 case P_XACQUIRE:
81 case P_XRELEASE:
Jin Kyu Song03041092013-10-15 19:38:51 -070082 case P_BND:
Jin Kyu Songb287ff02013-12-04 20:05:55 -080083 case P_NOBND:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080084 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070085 case P_O16:
86 case P_O32:
87 case P_O64:
88 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030089 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070090 case P_A16:
91 case P_A32:
92 case P_A64:
93 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030094 return PPS_ASIZE;
Jin Kyu Song945b1b82013-10-25 19:29:53 -070095 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -080096 case P_VEX3:
97 case P_VEX2:
98 return PPS_VEX;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070099 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700100 nasm_panic("Invalid value %d passed to prefix_slot()", prefix);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300101 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700102 }
103}
104
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700105static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700106{
107 if (tasm_compatible_mode) {
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800108 switch (tokval.t_integer) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300109 /* For TASM compatibility a size override inside the
110 * brackets changes the size of the operand, not the
111 * address type of the operand as it does in standard
112 * NASM syntax. Hence:
113 *
114 * mov eax,[DWORD val]
115 *
116 * is valid syntax in TASM compatibility mode. Note that
117 * you lose the ability to override the default address
118 * type for the instruction, but we never use anything
119 * but 32-bit flat model addressing in our code.
120 */
121 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700122 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300123 break;
124 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700125 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300126 break;
127 case S_DWORD:
128 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700129 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300130 break;
131 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700132 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300133 break;
134 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700135 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300136 break;
137 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700138 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300139 break;
140 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300141 nasm_nonfatal("invalid operand size specification");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300142 break;
143 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700144 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300145 /* Standard NASM compatible syntax */
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800146 switch (tokval.t_integer) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300147 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700148 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300149 break;
150 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700151 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300152 break;
153 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700154 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300155 break;
156 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700157 op->disp_size = 8;
158 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300159 break;
160 case P_A16:
161 case P_A32:
162 case P_A64:
163 if (result->prefixes[PPS_ASIZE] &&
164 result->prefixes[PPS_ASIZE] != tokval.t_integer)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300165 nasm_nonfatal("conflicting address size specifications");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300166 else
167 result->prefixes[PPS_ASIZE] = tokval.t_integer;
168 break;
169 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700170 op->disp_size = 16;
171 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300172 break;
173 case S_DWORD:
174 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700175 op->disp_size = 32;
176 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300177 break;
178 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700179 op->disp_size = 64;
180 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300181 break;
182 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300183 nasm_nonfatal("invalid size specification in"
184 " effective address");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300185 break;
186 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700187 }
188}
189
Jin Kyu Song72018a22013-08-05 20:46:18 -0700190/*
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700191 * Brace decorators are are parsed here. opmask and zeroing
192 * decorators can be placed in any order. e.g. zmm1 {k2}{z} or zmm2
193 * {z}{k3} decorator(s) are placed at the end of an operand.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700194 */
195static bool parse_braces(decoflags_t *decoflags)
196{
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700197 int i, j;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700198
199 i = tokval.t_type;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700200
201 while (true) {
202 switch (i) {
203 case TOKEN_OPMASK:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700204 if (*decoflags & OPMASK_MASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300205 nasm_nonfatal("opmask k%"PRIu64" is already set",
206 *decoflags & OPMASK_MASK);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700207 *decoflags &= ~OPMASK_MASK;
208 }
209 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700210 break;
211 case TOKEN_DECORATOR:
212 j = tokval.t_integer;
213 switch (j) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700214 case BRC_Z:
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700215 *decoflags |= Z_MASK;
216 break;
217 case BRC_1TO2:
218 case BRC_1TO4:
219 case BRC_1TO8:
220 case BRC_1TO16:
221 *decoflags |= BRDCAST_MASK | VAL_BRNUM(j - BRC_1TO2);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700222 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300224 nasm_nonfatal("{%s} is not an expected decorator",
225 tokval.t_charptr);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700226 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700227 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700228 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700229 case ',':
230 case TOKEN_EOS:
231 return false;
232 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300233 nasm_nonfatal("only a series of valid decorators expected");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700234 return true;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700235 }
236 i = stdscan(NULL, &tokval);
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700237 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700238}
239
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700240static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700241{
242 int b, i, s; /* basereg, indexreg, scale */
243 int64_t o; /* offset */
244
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700245 b = i = -1;
246 o = s = 0;
H. Peter Anvin164d2462017-02-20 02:39:56 -0800247 op->segment = op->wrt = NO_SEG;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700248
249 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
250 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
251
252 if (is_gpr && e->value == 1)
253 b = e->type; /* It can be basereg */
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700254 else /* No, it has to be indexreg */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700255 i = e->type, s = e->value;
256 e++;
257 }
258 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
259 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
260
261 if (b != -1) /* If the first was the base, ... */
262 i = e->type, s = e->value; /* second has to be indexreg */
263
264 else if (!is_gpr || e->value != 1) {
265 /* If both want to be index */
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300266 nasm_nonfatal("invalid effective address: two index registers");
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700267 return -1;
268 } else
269 b = e->type;
270 e++;
271 }
H. Peter Anvin164d2462017-02-20 02:39:56 -0800272
273 if (e->type) { /* is there an offset? */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700274 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300275 nasm_nonfatal("invalid effective address: impossible register");
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700276 return -1;
277 } else {
278 if (e->type == EXPR_UNKNOWN) {
279 op->opflags |= OPFLAG_UNKNOWN;
280 o = 0; /* doesn't matter what */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700281 while (e->type)
282 e++; /* go to the end of the line */
283 } else {
284 if (e->type == EXPR_SIMPLE) {
285 o = e->value;
286 e++;
287 }
288 if (e->type == EXPR_WRT) {
289 op->wrt = e->value;
290 e++;
H. Peter Anvin164d2462017-02-20 02:39:56 -0800291 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700292 /*
293 * Look for a segment base type.
294 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800295 for (; e->type; e++) {
296 if (!e->value)
297 continue;
298
299 if (e->type <= EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300300 nasm_nonfatal("invalid effective address: too many registers");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800301 return -1;
302 } else if (e->type < EXPR_SEGBASE) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300303 nasm_nonfatal("invalid effective address: bad subexpression type");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800304 return -1;
305 } else if (e->value == 1) {
306 if (op->segment != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300307 nasm_nonfatal("invalid effective address: multiple base segments");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800308 return -1;
309 }
310 op->segment = e->type - EXPR_SEGBASE;
311 } else if (e->value == -1 &&
312 e->type == location.segment + EXPR_SEGBASE &&
313 !(op->opflags & OPFLAG_RELATIVE)) {
314 op->opflags |= OPFLAG_RELATIVE;
315 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300316 nasm_nonfatal("invalid effective address: impossible segment base multiplier");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800317 return -1;
318 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700319 }
320 }
321 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700322 }
323
H. Peter Anvin164d2462017-02-20 02:39:56 -0800324 nasm_assert(!e->type); /* We should be at the end */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700325
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700326 op->basereg = b;
327 op->indexreg = i;
328 op->scale = s;
329 op->offset = o;
330 return 0;
331}
332
333static void mref_set_optype(operand *op)
334{
335 int b = op->basereg;
336 int i = op->indexreg;
337 int s = op->scale;
338
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700339 /* It is memory, but it can match any r/m operand */
340 op->type |= MEMORY_ANY;
341
342 if (b == -1 && (i == -1 || s == 0)) {
343 int is_rel = globalbits == 64 &&
344 !(op->eaflags & EAF_ABS) &&
345 ((globalrel &&
346 !(op->eaflags & EAF_FSGS)) ||
347 (op->eaflags & EAF_REL));
348
349 op->type |= is_rel ? IP_REL : MEM_OFFS;
350 }
351
352 if (i != -1) {
353 opflags_t iclass = nasm_reg_flags[i];
354
355 if (is_class(XMMREG,iclass))
356 op->type |= XMEM;
357 else if (is_class(YMMREG,iclass))
358 op->type |= YMEM;
359 else if (is_class(ZMMREG,iclass))
360 op->type |= ZMEM;
361 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700362}
363
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700364/*
365 * Convert an expression vector returned from evaluate() into an
366 * extop structure. Return zero on success.
367 */
368static int value_to_extop(expr * vect, extop *eop, int32_t myseg)
369{
370 eop->type = EOT_DB_NUMBER;
371 eop->offset = 0;
372 eop->segment = eop->wrt = NO_SEG;
373 eop->relative = false;
374
375 for (; vect->type; vect++) {
376 if (!vect->value) /* zero term, safe to ignore */
377 continue;
378
Cyrill Gorcunovfd610f22016-11-28 23:57:08 +0300379 if (vect->type <= EXPR_REG_END) /* false if a register is present */
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700380 return -1;
381
382 if (vect->type == EXPR_UNKNOWN) /* something we can't resolve yet */
383 return 0;
384
385 if (vect->type == EXPR_SIMPLE) {
386 /* Simple number expression */
387 eop->offset += vect->value;
388 continue;
389 }
390 if (eop->wrt == NO_SEG && !eop->relative && vect->type == EXPR_WRT) {
391 /* WRT term */
392 eop->wrt = vect->value;
393 continue;
394 }
395
H. Peter Anvind97ccee2017-02-21 11:31:35 -0800396 if (!eop->relative &&
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700397 vect->type == EXPR_SEGBASE + myseg && vect->value == -1) {
398 /* Expression of the form: foo - $ */
399 eop->relative = true;
400 continue;
401 }
402
403 if (eop->segment == NO_SEG && vect->type >= EXPR_SEGBASE &&
404 vect->value == 1) {
405 eop->segment = vect->type - EXPR_SEGBASE;
406 continue;
407 }
408
409 /* Otherwise, badness */
410 return -1;
411 }
412
413 /* We got to the end and it was all okay */
414 return 0;
415}
416
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800417insn *parse_line(char *buffer, insn *result)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000418{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400419 bool insn_is_label = false;
420 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700421 int opnum;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800422 bool critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800423 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700424 bool recover;
Martin Lindhe58f37c12016-11-16 16:43:16 +0100425 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000426
H. Peter Anvin7daa26f2018-06-02 23:48:16 -0700427 nasm_static_assert(P_none == 0);
428
H. Peter Anvin9c987692007-11-04 21:09:32 -0800429restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400430 first = true;
431 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000432
H. Peter Anvin76690a12002-04-30 20:52:49 +0000433 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300434 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000436
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700437 memset(result->prefixes, P_none, sizeof(result->prefixes));
438 result->times = 1; /* No TIMES either yet */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400439 result->label = NULL; /* Assume no label */
440 result->eops = NULL; /* must do this, whatever happens */
441 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700442 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
443 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000444
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400445 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700446 if (i == TOKEN_EOS)
447 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400448
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400449 if (i != TOKEN_ID &&
450 i != TOKEN_INSN &&
451 i != TOKEN_PREFIX &&
452 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300453 nasm_nonfatal("label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700454 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000455 }
456
H. Peter Anvin9c987692007-11-04 21:09:32 -0800457 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
458 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300459 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 result->label = tokval.t_charptr;
461 i = stdscan(NULL, &tokval);
462 if (i == ':') { /* skip over the optional colon */
463 i = stdscan(NULL, &tokval);
464 } else if (i == 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800465 /*!
466 *!orphan-labels [on] labels alone on lines without trailing `:'
467 *! warns about source lines which contain no instruction but define
468 *! a label without a trailing colon. This is most likely indicative
469 *! of a typo, but is technically correct NASM syntax (see \k{syntax}.)
470 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800471 nasm_warn(WARN_ORPHAN_LABELS ,
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300472 "label alone on a line without a colon might be in error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 }
474 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
475 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800476 * FIXME: location.segment could be NO_SEG, in which case
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800477 * it is possible we should be passing 'absolute.segment'. Look into this.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 * Work out whether that is *really* what we should be doing.
479 * Generally fix things. I think this is right as it is, but
480 * am still not certain.
481 */
H. Peter Anvin (Intel)415b6b32018-06-25 14:09:52 -0700482 define_label(result->label,
483 in_absolute ? absolute.segment : location.segment,
H. Peter Anvin98578072018-06-01 18:02:54 -0700484 location.offset, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000486 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000487
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400488 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700489 if (i == TOKEN_EOS)
490 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000491
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400493 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300494 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800495
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 /*
497 * Handle special case: the TIMES prefix.
498 */
499 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
500 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000501
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 i = stdscan(NULL, &tokval);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800503 value = evaluate(stdscan, NULL, &tokval, NULL, pass_stable(), NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700505 if (!value) /* Error in evaluator */
506 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 if (!is_simple(value)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300508 nasm_nonfatal("non-constant argument supplied to TIMES");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 result->times = 1L;
510 } else {
511 result->times = value->value;
H. Peter Anvin94ead272017-09-27 15:22:23 -0700512 if (value->value < 0) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300513 nasm_nonfatalf(ERR_PASS2, "TIMES value %"PRId64" is negative", value->value);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 result->times = 0;
515 }
516 }
517 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300518 int slot = prefix_slot(tokval.t_integer);
519 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700520 if (result->prefixes[slot] == tokval.t_integer)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800521 nasm_warn(WARN_OTHER, "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700522 else
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300523 nasm_nonfatal("instruction has conflicting prefixes");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300524 }
525 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 i = stdscan(NULL, &tokval);
527 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000528 }
529
530 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300531 int j;
532 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700533
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400534 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300535 if ((pfx = result->prefixes[j]) != P_none)
536 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400537 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700538
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700539 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 /*
541 * Instruction prefixes are present, but no actual
542 * instruction. This is allowed: at this point we
543 * invent a notional instruction of RESB 0.
544 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400545 result->opcode = I_RESB;
546 result->operands = 1;
H. Peter Anvin1980abf2017-03-31 14:52:03 -0700547 nasm_zero(result->oprs);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400548 result->oprs[0].type = IMMEDIATE;
549 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
551 return result;
552 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300553 nasm_nonfatal("parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700554 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000556 }
557
558 result->opcode = tokval.t_integer;
559 result->condition = tokval.t_inttwo;
560
561 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700562 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000563 * evaluated operands, since the correct values _must_ be known
564 * on the first pass. Hence, even in pass one, we set the
565 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700566 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000567 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800568 critical = pass_final() || (result->opcode == I_INCBIN);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700570 if (opcode_is_db(result->opcode) || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 extop *eop, **tail = &result->eops, **fixptr;
572 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300573 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000574
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700575 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700578 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 */
580 while (1) {
581 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400582 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300584 else if (first && i == ':') {
585 insn_is_label = true;
586 goto restart_parse;
587 }
588 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 fixptr = tail;
590 eop = *tail = nasm_malloc(sizeof(extop));
591 tail = &eop->next;
592 eop->next = NULL;
593 eop->type = EOT_NOTHING;
594 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300595 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000596
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300597 /*
598 * is_comma_next() here is to distinguish this from
599 * a string used as part of an expression...
600 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700601 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400602 eop->type = EOT_DB_STRING;
603 eop->stringval = tokval.t_charptr;
604 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300606 } else if (i == TOKEN_STRFUNC) {
607 bool parens = false;
608 const char *funcname = tokval.t_charptr;
609 enum strfunc func = tokval.t_integer;
610 i = stdscan(NULL, &tokval);
611 if (i == '(') {
612 parens = true;
613 i = stdscan(NULL, &tokval);
614 }
615 if (i != TOKEN_STR) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300616 nasm_nonfatal("%s must be followed by a string constant",
617 funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300618 eop->type = EOT_NOTHING;
619 } else {
620 eop->type = EOT_DB_STRING_FREE;
621 eop->stringlen =
622 string_transform(tokval.t_charptr, tokval.t_inttwo,
623 &eop->stringval, func);
624 if (eop->stringlen == (size_t)-1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300625 nasm_nonfatal("invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300626 eop->type = EOT_NOTHING;
627 }
628 }
629 if (parens && i && i != ')') {
630 i = stdscan(NULL, &tokval);
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300631 if (i != ')')
632 nasm_nonfatal("unterminated %s function", funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300633 }
634 if (i && i != ',')
635 i = stdscan(NULL, &tokval);
636 } else if (i == '-' || i == '+') {
637 char *save = stdscan_get();
638 int token = i;
639 sign = (i == '-') ? -1 : 1;
640 i = stdscan(NULL, &tokval);
641 if (i != TOKEN_FLOAT) {
642 stdscan_set(save);
643 i = tokval.t_type = token;
644 goto is_expression;
645 } else {
646 goto is_float;
647 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700648 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300649is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300650 eop->type = EOT_DB_STRING;
651 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300652
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700653 eop->stringlen = db_bytes(result->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300654 if (eop->stringlen > 16) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300655 nasm_nonfatal("floating-point constant"
656 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300657 eop->stringlen = 0;
658 } else if (eop->stringlen < 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300659 nasm_nonfatal("floating-point constant"
660 " encountered in unknown instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300661 /*
662 * fix suggested by Pedro Gimeno... original line was:
663 * eop->type = EOT_NOTHING;
664 */
665 eop->stringlen = 0;
666 }
667
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300668 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
669 tail = &eop->next;
670 *fixptr = eop;
671 eop->stringval = (char *)eop + sizeof(extop);
672 if (!eop->stringlen ||
673 !float_const(tokval.t_charptr, sign,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800674 (uint8_t *)eop->stringval, eop->stringlen))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300675 eop->type = EOT_NOTHING;
676 i = stdscan(NULL, &tokval); /* eat the comma */
677 } else {
678 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700680
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300681is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800683 critical, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700685 if (!value) /* Error in evaluator */
686 goto fail;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700687 if (value_to_extop(value, eop, location.segment)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300688 nasm_nonfatal("operand %d: expression is not simple or relocatable",
689 oper_num);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 }
691 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000692
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 /*
694 * We're about to call stdscan(), which will eat the
695 * comma that we're currently sitting on between
696 * arguments. However, we'd better check first that it
697 * _is_ a comma.
698 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400699 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 break;
701 if (i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300702 nasm_nonfatal("comma expected after operand %d", oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700703 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 }
705 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000706
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 if (result->opcode == I_INCBIN) {
708 /*
709 * Correct syntax for INCBIN is that there should be
710 * one string operand, followed by one or two numeric
711 * operands.
712 */
713 if (!result->eops || result->eops->type != EOT_DB_STRING)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300714 nasm_nonfatal("`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 else if (result->eops->next &&
716 result->eops->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300717 nasm_nonfatal("`incbin': second parameter is"
718 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000719 else if (result->eops->next && result->eops->next->next &&
720 result->eops->next->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300721 nasm_nonfatal("`incbin': third parameter is"
722 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000723 else if (result->eops->next && result->eops->next->next &&
724 result->eops->next->next->next)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300725 nasm_nonfatal("`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000726 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 return result;
728 /*
729 * If we reach here, one of the above errors happened.
730 * Throw the instruction away.
731 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700732 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800734 nasm_warn(WARN_OTHER, "no operand for data declaration");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 else
736 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000737
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739 }
740
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400741 /*
742 * Now we begin to parse the operands. There may be up to four
743 * of these, separated by commas, and terminated by a zero token.
744 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000745
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700746 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
747 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300748 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700749 bool mref; /* is this going to be a memory ref? */
750 bool bracket; /* is it a [] mref, or a & mref? */
751 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700753 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000754
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700755 op->disp_size = 0; /* have to zero this whatever */
756 op->eaflags = 0; /* and this */
757 op->opflags = 0;
758 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000759
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400761 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300763 else if (first && i == ':') {
764 insn_is_label = true;
765 goto restart_parse;
766 }
767 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700768 op->type = 0; /* so far, no override */
H. Peter Anvin11599f42018-12-22 23:09:54 -0800769 /* size specifiers */
770 while (i == TOKEN_SPECIAL || i == TOKEN_SIZE) {
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 */
H. Peter Anvin11599f42018-12-22 23:09:54 -0800838 while (i == TOKEN_SPECIAL || i == TOKEN_SIZE ||
839 i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700840 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700841 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700843 /* when a comma follows an opening bracket - [ , eax*4] */
844 if (i == ',') {
845 /* treat as if there is a zero displacement virtually */
846 tokval.t_type = TOKEN_NUM;
847 tokval.t_integer = 0;
848 stdscan_set(stdscan_get() - 1); /* rewind the comma */
849 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700851 mref = false;
852 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000854
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700855 if ((op->type & FAR) && !mref &&
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300856 result->opcode != I_JMP && result->opcode != I_CALL)
857 nasm_nonfatal("invalid use of FAR operand specifier");
Debbie Wiles63b53f72002-06-04 19:31:24 +0000858
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800860 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700862 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700863 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700865 if (!value) /* Error in evaluator */
866 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 if (i == ':' && mref) { /* it was seg:offset */
868 /*
869 * Process the segment override.
870 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400871 if (value[1].type != 0 ||
872 value->value != 1 ||
873 !IS_SREG(value->type))
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300874 nasm_nonfatal("invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700875 else if (result->prefixes[PPS_SEG])
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300876 nasm_nonfatal("instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000877 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300878 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400879 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700880 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300881 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000882
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvin11599f42018-12-22 23:09:54 -0800884 while (i == TOKEN_SPECIAL || i == TOKEN_SIZE ||
885 i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700886 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 i = stdscan(NULL, &tokval);
888 }
889 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800890 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700892 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700893 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 }
895 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700896 if (!value) /* Error in evaluator */
897 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200899
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700900 mib = false;
901 if (mref && bracket && i == ',') {
902 /* [seg:base+offset,index*scale] syntax (mib) */
903
904 operand o1, o2; /* Partial operands */
905
906 if (parse_mref(&o1, value))
907 goto fail;
908
909 i = stdscan(NULL, &tokval); /* Eat comma */
910 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800911 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700912 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +0300913 if (!value)
914 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700915
916 if (parse_mref(&o2, value))
917 goto fail;
918
919 if (o2.basereg != -1 && o2.indexreg == -1) {
920 o2.indexreg = o2.basereg;
921 o2.scale = 1;
922 o2.basereg = -1;
923 }
924
925 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
926 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300927 nasm_nonfatal("invalid mib expression");
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700928 goto fail;
929 }
930
931 op->basereg = o1.basereg;
932 op->indexreg = o2.indexreg;
933 op->scale = o2.scale;
934 op->offset = o1.offset;
935 op->segment = o1.segment;
936 op->wrt = o1.wrt;
937
938 if (op->basereg != -1) {
939 op->hintbase = op->basereg;
940 op->hinttype = EAH_MAKEBASE;
941 } else if (op->indexreg != -1) {
942 op->hintbase = op->indexreg;
943 op->hinttype = EAH_NOTBASE;
944 } else {
945 op->hintbase = -1;
946 op->hinttype = EAH_NOHINT;
947 }
948
949 mib = true;
950 }
951
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700952 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 if (mref && bracket) { /* find ] at the end */
954 if (i != ']') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300955 nasm_nonfatal("parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200956 recover = true;
957 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 i = stdscan(NULL, &tokval);
H. Peter Anvinc33d95f2017-03-31 14:37:24 -0700959 if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
960 /* parse opmask (and zeroing) after an operand */
961 recover = parse_braces(&brace_flags);
962 i = tokval.t_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700963 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200964 if (i != 0 && i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300965 nasm_nonfatal("comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200966 recover = true;
967 }
968 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700970 if (i != 0 && i != ',' && i != ':' &&
971 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300972 nasm_nonfatal("comma, colon, decorator or end of "
973 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200974 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700976 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700977 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
978 /* parse opmask (and zeroing) after an operand */
979 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 }
981 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200982 if (recover) {
983 do { /* error recovery */
984 i = stdscan(NULL, &tokval);
985 } while (i != 0 && i != ',');
986 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000987
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300988 /*
989 * now convert the exprs returned from evaluate()
990 * into operand descriptions...
991 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700992 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000993
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700995 /* A mib reference was fully parsed already */
996 if (!mib) {
997 if (parse_mref(op, value))
998 goto fail;
999 op->hintbase = hints.base;
1000 op->hinttype = hints.type;
1001 }
1002 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001005 op->type |= IMMEDIATE;
1006 op->opflags |= OPFLAG_UNKNOWN;
1007 op->offset = 0; /* don't care */
1008 op->segment = NO_SEG; /* don't care again */
1009 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001010
Chang S. Baea5786342018-08-15 23:22:21 +03001011 if(optimizing.level >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001012 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001013 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001014 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001015 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvin87646092017-02-28 17:44:24 -08001017 uint64_t n = reloc_value(value);
1018
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001019 op->type |= IMMEDIATE;
H. Peter Anvin87646092017-02-28 17:44:24 -08001020 op->offset = n;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001021 op->segment = reloc_seg(value);
1022 op->wrt = reloc_wrt(value);
H. Peter Anvin164d2462017-02-20 02:39:56 -08001023 op->opflags |= is_self_relative(value) ? OPFLAG_RELATIVE : 0;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001026 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001027 op->type |= UNITY;
Chang S. Baea5786342018-08-15 23:22:21 +03001028 if (optimizing.level >= 0 && !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001029 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001030 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001031 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 op->type |= SBYTEWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001033 if (n <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001034 op->type |= UDWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001035 if (n + UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001036 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 }
1038 }
H. Peter Anvin164d2462017-02-20 02:39:56 -08001039 } else if (value->type == EXPR_RDSAE) {
Jin Kyu Song72018a22013-08-05 20:46:18 -07001040 /*
1041 * it's not an operand but a rounding or SAE decorator.
1042 * put the decorator information in the (opflag_t) type field
1043 * of previous operand.
1044 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001045 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001046 switch (value->value) {
1047 case BRC_RN:
1048 case BRC_RU:
1049 case BRC_RD:
1050 case BRC_RZ:
1051 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001052 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001053 result->evex_rm = value->value;
1054 break;
1055 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001056 nasm_nonfatal("invalid decorator");
Jin Kyu Song72018a22013-08-05 20:46:18 -07001057 break;
1058 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001060 opflags_t rs;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001061 uint64_t regset_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001062
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 if (value->type >= EXPR_SIMPLE || value->value != 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001064 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001065 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 /*
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001069 * We do not allow any kind of expression, except for
1070 * reg+value in which case it is a register set.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 */
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001072 for (i = 1; value[i].type; i++) {
1073 if (!value[i].value)
1074 continue;
1075
1076 switch (value[i].type) {
1077 case EXPR_SIMPLE:
1078 if (!regset_size) {
1079 regset_size = value[i].value + 1;
1080 break;
1081 }
1082 /* fallthrough */
1083 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001084 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001085 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 }
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001087 }
1088
1089 if ((regset_size & (regset_size - 1)) ||
1090 regset_size >= (UINT64_C(1) << REGSET_BITS)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001091 nasm_nonfatalf(ERR_PASS2, "invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001092 regset_size = 0;
1093 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001094
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001096 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 /*
1098 * we want to produce a warning iff the specified size
1099 * is different from the register size
1100 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001101 rs = op->type & SIZE_MASK;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001102 } else {
H. Peter Anvin68222142007-11-18 22:18:09 -08001103 rs = 0;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001104 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001106 /*
1107 * Make sure we're not out of nasm_reg_flags, still
1108 * probably this should be fixed when we're defining
1109 * the label.
1110 *
1111 * An easy trigger is
1112 *
1113 * e equ 0x80000000:0
1114 * pshufw word e-0
1115 *
1116 */
1117 if (value->type < EXPR_REG_START ||
1118 value->type > EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001119 nasm_nonfatal("invalid operand type");
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001120 goto fail;
1121 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001123 op->type &= TO;
1124 op->type |= REGISTER;
1125 op->type |= nasm_reg_flags[value->type];
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001126 op->type |= (regset_size >> 1) << REGSET_SHIFT;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001127 op->decoflags |= brace_flags;
1128 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001130 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001131 nasm_warn(WARN_OTHER, "register size specification ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 }
1133 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001134
1135 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001136 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1137 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001138 }
1139
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001140 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001141
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001142 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001143 while (opnum < MAX_OPERANDS)
1144 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001145
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001146 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001147
1148fail:
1149 result->opcode = I_none;
1150 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001151}
1152
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001154{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001155 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001156 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001157 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001158
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001159 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001161 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001162
H. Peter Anvin76690a12002-04-30 20:52:49 +00001163 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001164}
1165
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001167{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001168 extop *e;
1169
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001170 while ((e = i->eops)) {
1171 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001172 if (e->type == EOT_DB_STRING_FREE)
1173 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001175 }
1176}