blob: 74ea77c9b07e485749dcbc8360668e500c4660fb [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 Anvin98578072018-06-01 18:02:54 -0700418insn *parse_line(int pass, 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 Anvinea6e34d2002-04-30 20:51:32 +0000423 int 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)80c4f232018-12-14 13:33:24 -0800472 nasm_warn(WARN_ORPHAN_LABELS | ERR_PASS1,
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 Anvin130736c2016-02-17 20:27:41 -0800504 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, 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)c3c6cea2018-12-14 13:44:35 -0800522 nasm_warn(WARN_OTHER|ERR_PASS1, "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 */
Charles Crayne2581c862008-09-10 19:21:52 -0700569 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700570 critical = (pass0 < 2 ? 1 : 2);
571
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 } else
573 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000574
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700575 if (opcode_is_db(result->opcode) || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 extop *eop, **tail = &result->eops, **fixptr;
577 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300578 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000579
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700580 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000581
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700583 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 */
585 while (1) {
586 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400587 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300589 else if (first && i == ':') {
590 insn_is_label = true;
591 goto restart_parse;
592 }
593 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 fixptr = tail;
595 eop = *tail = nasm_malloc(sizeof(extop));
596 tail = &eop->next;
597 eop->next = NULL;
598 eop->type = EOT_NOTHING;
599 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300600 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000601
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300602 /*
603 * is_comma_next() here is to distinguish this from
604 * a string used as part of an expression...
605 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700606 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400607 eop->type = EOT_DB_STRING;
608 eop->stringval = tokval.t_charptr;
609 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300611 } else if (i == TOKEN_STRFUNC) {
612 bool parens = false;
613 const char *funcname = tokval.t_charptr;
614 enum strfunc func = tokval.t_integer;
615 i = stdscan(NULL, &tokval);
616 if (i == '(') {
617 parens = true;
618 i = stdscan(NULL, &tokval);
619 }
620 if (i != TOKEN_STR) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300621 nasm_nonfatal("%s must be followed by a string constant",
622 funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300623 eop->type = EOT_NOTHING;
624 } else {
625 eop->type = EOT_DB_STRING_FREE;
626 eop->stringlen =
627 string_transform(tokval.t_charptr, tokval.t_inttwo,
628 &eop->stringval, func);
629 if (eop->stringlen == (size_t)-1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300630 nasm_nonfatal("invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300631 eop->type = EOT_NOTHING;
632 }
633 }
634 if (parens && i && i != ')') {
635 i = stdscan(NULL, &tokval);
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300636 if (i != ')')
637 nasm_nonfatal("unterminated %s function", funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300638 }
639 if (i && i != ',')
640 i = stdscan(NULL, &tokval);
641 } else if (i == '-' || i == '+') {
642 char *save = stdscan_get();
643 int token = i;
644 sign = (i == '-') ? -1 : 1;
645 i = stdscan(NULL, &tokval);
646 if (i != TOKEN_FLOAT) {
647 stdscan_set(save);
648 i = tokval.t_type = token;
649 goto is_expression;
650 } else {
651 goto is_float;
652 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700653 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300654is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300655 eop->type = EOT_DB_STRING;
656 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300657
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700658 eop->stringlen = db_bytes(result->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300659 if (eop->stringlen > 16) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300660 nasm_nonfatal("floating-point constant"
661 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300662 eop->stringlen = 0;
663 } else if (eop->stringlen < 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300664 nasm_nonfatal("floating-point constant"
665 " encountered in unknown instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300666 /*
667 * fix suggested by Pedro Gimeno... original line was:
668 * eop->type = EOT_NOTHING;
669 */
670 eop->stringlen = 0;
671 }
672
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300673 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
674 tail = &eop->next;
675 *fixptr = eop;
676 eop->stringval = (char *)eop + sizeof(extop);
677 if (!eop->stringlen ||
678 !float_const(tokval.t_charptr, sign,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800679 (uint8_t *)eop->stringval, eop->stringlen))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300680 eop->type = EOT_NOTHING;
681 i = stdscan(NULL, &tokval); /* eat the comma */
682 } else {
683 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700685
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300686is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800688 critical, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700690 if (!value) /* Error in evaluator */
691 goto fail;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700692 if (value_to_extop(value, eop, location.segment)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300693 nasm_nonfatal("operand %d: expression is not simple or relocatable",
694 oper_num);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 }
696 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000697
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 /*
699 * We're about to call stdscan(), which will eat the
700 * comma that we're currently sitting on between
701 * arguments. However, we'd better check first that it
702 * _is_ a comma.
703 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400704 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 break;
706 if (i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300707 nasm_nonfatal("comma expected after operand %d", oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700708 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 }
710 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000711
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 if (result->opcode == I_INCBIN) {
713 /*
714 * Correct syntax for INCBIN is that there should be
715 * one string operand, followed by one or two numeric
716 * operands.
717 */
718 if (!result->eops || result->eops->type != EOT_DB_STRING)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300719 nasm_nonfatal("`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 else if (result->eops->next &&
721 result->eops->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300722 nasm_nonfatal("`incbin': second 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->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300726 nasm_nonfatal("`incbin': third parameter is"
727 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 else if (result->eops->next && result->eops->next->next &&
729 result->eops->next->next->next)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300730 nasm_nonfatal("`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000731 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 return result;
733 /*
734 * If we reach here, one of the above errors happened.
735 * Throw the instruction away.
736 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700737 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800739 nasm_warn(WARN_OTHER|ERR_PASS1, "no operand for data declaration");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 else
741 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000742
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000744 }
745
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400746 /*
747 * Now we begin to parse the operands. There may be up to four
748 * of these, separated by commas, and terminated by a zero token.
749 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000750
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700751 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
752 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300753 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700754 bool mref; /* is this going to be a memory ref? */
755 bool bracket; /* is it a [] mref, or a & mref? */
756 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700758 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000759
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700760 op->disp_size = 0; /* have to zero this whatever */
761 op->eaflags = 0; /* and this */
762 op->opflags = 0;
763 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000764
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400766 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300768 else if (first && i == ':') {
769 insn_is_label = true;
770 goto restart_parse;
771 }
772 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700773 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 while (i == TOKEN_SPECIAL) { /* size specifiers */
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800775 switch (tokval.t_integer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000776 case S_BYTE:
777 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700778 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 setsize = 1;
780 break;
781 case S_WORD:
782 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700783 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 setsize = 1;
785 break;
786 case S_DWORD:
787 case S_LONG:
788 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700789 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 setsize = 1;
791 break;
792 case S_QWORD:
793 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700794 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 setsize = 1;
796 break;
797 case S_TWORD:
798 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700799 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 setsize = 1;
801 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700802 case S_OWORD:
803 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700804 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700805 setsize = 1;
806 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700807 case S_YWORD:
808 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700809 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700810 setsize = 1;
811 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700812 case S_ZWORD:
813 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700814 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700815 setsize = 1;
816 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700818 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 break;
820 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700821 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 break;
823 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700824 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 break;
826 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700827 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 break;
829 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700830 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 break;
832 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300833 nasm_nonfatal("invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 }
835 i = stdscan(NULL, &tokval);
836 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700839 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700841 i = stdscan(NULL, &tokval); /* then skip the colon */
842 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700843 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700844 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700846 /* when a comma follows an opening bracket - [ , eax*4] */
847 if (i == ',') {
848 /* treat as if there is a zero displacement virtually */
849 tokval.t_type = TOKEN_NUM;
850 tokval.t_integer = 0;
851 stdscan_set(stdscan_get() - 1); /* rewind the comma */
852 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700854 mref = false;
855 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000857
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700858 if ((op->type & FAR) && !mref &&
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300859 result->opcode != I_JMP && result->opcode != I_CALL)
860 nasm_nonfatal("invalid use of FAR operand specifier");
Debbie Wiles63b53f72002-06-04 19:31:24 +0000861
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800863 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700865 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700866 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700868 if (!value) /* Error in evaluator */
869 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 if (i == ':' && mref) { /* it was seg:offset */
871 /*
872 * Process the segment override.
873 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400874 if (value[1].type != 0 ||
875 value->value != 1 ||
876 !IS_SREG(value->type))
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300877 nasm_nonfatal("invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700878 else if (result->prefixes[PPS_SEG])
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300879 nasm_nonfatal("instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000880 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300881 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400882 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700883 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300884 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000885
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700887 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700888 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 i = stdscan(NULL, &tokval);
890 }
891 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800892 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700894 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700895 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 }
897 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700898 if (!value) /* Error in evaluator */
899 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200901
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700902 mib = false;
903 if (mref && bracket && i == ',') {
904 /* [seg:base+offset,index*scale] syntax (mib) */
905
906 operand o1, o2; /* Partial operands */
907
908 if (parse_mref(&o1, value))
909 goto fail;
910
911 i = stdscan(NULL, &tokval); /* Eat comma */
912 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800913 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700914 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +0300915 if (!value)
916 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700917
918 if (parse_mref(&o2, value))
919 goto fail;
920
921 if (o2.basereg != -1 && o2.indexreg == -1) {
922 o2.indexreg = o2.basereg;
923 o2.scale = 1;
924 o2.basereg = -1;
925 }
926
927 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
928 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300929 nasm_nonfatal("invalid mib expression");
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700930 goto fail;
931 }
932
933 op->basereg = o1.basereg;
934 op->indexreg = o2.indexreg;
935 op->scale = o2.scale;
936 op->offset = o1.offset;
937 op->segment = o1.segment;
938 op->wrt = o1.wrt;
939
940 if (op->basereg != -1) {
941 op->hintbase = op->basereg;
942 op->hinttype = EAH_MAKEBASE;
943 } else if (op->indexreg != -1) {
944 op->hintbase = op->indexreg;
945 op->hinttype = EAH_NOTBASE;
946 } else {
947 op->hintbase = -1;
948 op->hinttype = EAH_NOHINT;
949 }
950
951 mib = true;
952 }
953
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700954 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 if (mref && bracket) { /* find ] at the end */
956 if (i != ']') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300957 nasm_nonfatal("parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200958 recover = true;
959 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 i = stdscan(NULL, &tokval);
H. Peter Anvinc33d95f2017-03-31 14:37:24 -0700961 if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
962 /* parse opmask (and zeroing) after an operand */
963 recover = parse_braces(&brace_flags);
964 i = tokval.t_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700965 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200966 if (i != 0 && i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300967 nasm_nonfatal("comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200968 recover = true;
969 }
970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700972 if (i != 0 && i != ',' && i != ':' &&
973 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300974 nasm_nonfatal("comma, colon, decorator or end of "
975 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200976 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700978 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700979 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
980 /* parse opmask (and zeroing) after an operand */
981 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 }
983 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200984 if (recover) {
985 do { /* error recovery */
986 i = stdscan(NULL, &tokval);
987 } while (i != 0 && i != ',');
988 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000989
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300990 /*
991 * now convert the exprs returned from evaluate()
992 * into operand descriptions...
993 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700994 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000995
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700997 /* A mib reference was fully parsed already */
998 if (!mib) {
999 if (parse_mref(op, value))
1000 goto fail;
1001 op->hintbase = hints.base;
1002 op->hinttype = hints.type;
1003 }
1004 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001007 op->type |= IMMEDIATE;
1008 op->opflags |= OPFLAG_UNKNOWN;
1009 op->offset = 0; /* don't care */
1010 op->segment = NO_SEG; /* don't care again */
1011 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001012
Chang S. Baea5786342018-08-15 23:22:21 +03001013 if(optimizing.level >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001014 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001015 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001016 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001017 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvin87646092017-02-28 17:44:24 -08001019 uint64_t n = reloc_value(value);
1020
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001021 op->type |= IMMEDIATE;
H. Peter Anvin87646092017-02-28 17:44:24 -08001022 op->offset = n;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001023 op->segment = reloc_seg(value);
1024 op->wrt = reloc_wrt(value);
H. Peter Anvin164d2462017-02-20 02:39:56 -08001025 op->opflags |= is_self_relative(value) ? OPFLAG_RELATIVE : 0;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001026
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001028 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001029 op->type |= UNITY;
Chang S. Baea5786342018-08-15 23:22:21 +03001030 if (optimizing.level >= 0 && !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001031 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001033 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001034 op->type |= SBYTEWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001035 if (n <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001036 op->type |= UDWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001037 if (n + UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001038 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 }
1040 }
H. Peter Anvin164d2462017-02-20 02:39:56 -08001041 } else if (value->type == EXPR_RDSAE) {
Jin Kyu Song72018a22013-08-05 20:46:18 -07001042 /*
1043 * it's not an operand but a rounding or SAE decorator.
1044 * put the decorator information in the (opflag_t) type field
1045 * of previous operand.
1046 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001047 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001048 switch (value->value) {
1049 case BRC_RN:
1050 case BRC_RU:
1051 case BRC_RD:
1052 case BRC_RZ:
1053 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001054 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001055 result->evex_rm = value->value;
1056 break;
1057 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001058 nasm_nonfatal("invalid decorator");
Jin Kyu Song72018a22013-08-05 20:46:18 -07001059 break;
1060 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001062 opflags_t rs;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001063 uint64_t regset_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 if (value->type >= EXPR_SIMPLE || value->value != 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001066 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001067 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001069
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 /*
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001071 * We do not allow any kind of expression, except for
1072 * reg+value in which case it is a register set.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 */
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001074 for (i = 1; value[i].type; i++) {
1075 if (!value[i].value)
1076 continue;
1077
1078 switch (value[i].type) {
1079 case EXPR_SIMPLE:
1080 if (!regset_size) {
1081 regset_size = value[i].value + 1;
1082 break;
1083 }
1084 /* fallthrough */
1085 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001086 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001087 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 }
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001089 }
1090
1091 if ((regset_size & (regset_size - 1)) ||
1092 regset_size >= (UINT64_C(1) << REGSET_BITS)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001093 nasm_nonfatalf(ERR_PASS2, "invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001094 regset_size = 0;
1095 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001098 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /*
1100 * we want to produce a warning iff the specified size
1101 * is different from the register size
1102 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001103 rs = op->type & SIZE_MASK;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001104 } else {
H. Peter Anvin68222142007-11-18 22:18:09 -08001105 rs = 0;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001106 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001108 /*
1109 * Make sure we're not out of nasm_reg_flags, still
1110 * probably this should be fixed when we're defining
1111 * the label.
1112 *
1113 * An easy trigger is
1114 *
1115 * e equ 0x80000000:0
1116 * pshufw word e-0
1117 *
1118 */
1119 if (value->type < EXPR_REG_START ||
1120 value->type > EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001121 nasm_nonfatal("invalid operand type");
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001122 goto fail;
1123 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001125 op->type &= TO;
1126 op->type |= REGISTER;
1127 op->type |= nasm_reg_flags[value->type];
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001128 op->type |= (regset_size >> 1) << REGSET_SHIFT;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001129 op->decoflags |= brace_flags;
1130 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001132 if (rs && (op->type & SIZE_MASK) != rs)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001133 nasm_warn(WARN_OTHER|ERR_PASS1, "register size specification ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 }
1135 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001136
1137 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001138 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1139 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001140 }
1141
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001142 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001143
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001144 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001145 while (opnum < MAX_OPERANDS)
1146 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001147
1148 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001149 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001150 */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001151 if (opcode_is_resb(result->opcode)) {
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -07001152 result->oprs[0].offset *= resb_bytes(result->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001153 result->oprs[0].offset *= result->times;
1154 result->times = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 result->opcode = I_RESB;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001156 }
1157
1158 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001159
1160fail:
1161 result->opcode = I_none;
1162 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001163}
1164
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001166{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001167 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001168 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001169 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001170
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001171 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001173 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001174
H. Peter Anvin76690a12002-04-30 20:52:49 +00001175 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001176}
1177
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001179{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001180 extop *e;
1181
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001182 while ((e = i->eops)) {
1183 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001184 if (e->type == EOT_DB_STRING_FREE)
1185 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001187 }
1188}