blob: dbfb11cbd696ad7f83a8de9f3808a1d429692939 [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)df4d3422018-12-12 17:48:38 -0800466 nasm_warnf(WARN_OL | ERR_PASS1,
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300467 "label alone on a line without a colon might be in error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 }
469 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
470 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800471 * FIXME: location.segment could be NO_SEG, in which case
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800472 * it is possible we should be passing 'absolute.segment'. Look into this.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 * Work out whether that is *really* what we should be doing.
474 * Generally fix things. I think this is right as it is, but
475 * am still not certain.
476 */
H. Peter Anvin (Intel)415b6b32018-06-25 14:09:52 -0700477 define_label(result->label,
478 in_absolute ? absolute.segment : location.segment,
H. Peter Anvin98578072018-06-01 18:02:54 -0700479 location.offset, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000481 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000482
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400483 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700484 if (i == TOKEN_EOS)
485 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000486
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000487 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400488 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300489 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 /*
492 * Handle special case: the TIMES prefix.
493 */
494 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
495 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000496
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 i = stdscan(NULL, &tokval);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800498 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700500 if (!value) /* Error in evaluator */
501 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 if (!is_simple(value)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300503 nasm_nonfatal("non-constant argument supplied to TIMES");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 result->times = 1L;
505 } else {
506 result->times = value->value;
H. Peter Anvin94ead272017-09-27 15:22:23 -0700507 if (value->value < 0) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300508 nasm_nonfatalf(ERR_PASS2, "TIMES value %"PRId64" is negative", value->value);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 result->times = 0;
510 }
511 }
512 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300513 int slot = prefix_slot(tokval.t_integer);
514 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700515 if (result->prefixes[slot] == tokval.t_integer)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300516 nasm_warnf(ERR_PASS1, "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700517 else
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300518 nasm_nonfatal("instruction has conflicting prefixes");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300519 }
520 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 i = stdscan(NULL, &tokval);
522 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000523 }
524
525 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300526 int j;
527 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700528
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400529 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300530 if ((pfx = result->prefixes[j]) != P_none)
531 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400532 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700533
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700534 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 /*
536 * Instruction prefixes are present, but no actual
537 * instruction. This is allowed: at this point we
538 * invent a notional instruction of RESB 0.
539 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400540 result->opcode = I_RESB;
541 result->operands = 1;
H. Peter Anvin1980abf2017-03-31 14:52:03 -0700542 nasm_zero(result->oprs);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400543 result->oprs[0].type = IMMEDIATE;
544 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
546 return result;
547 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300548 nasm_nonfatal("parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700549 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000551 }
552
553 result->opcode = tokval.t_integer;
554 result->condition = tokval.t_inttwo;
555
556 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700557 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000558 * evaluated operands, since the correct values _must_ be known
559 * on the first pass. Hence, even in pass one, we set the
560 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700561 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562 */
Charles Crayne2581c862008-09-10 19:21:52 -0700563 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700564 critical = (pass0 < 2 ? 1 : 2);
565
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 } else
567 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000568
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700569 if (opcode_is_db(result->opcode) || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 extop *eop, **tail = &result->eops, **fixptr;
571 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300572 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000573
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700574 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700577 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 */
579 while (1) {
580 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400581 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300583 else if (first && i == ':') {
584 insn_is_label = true;
585 goto restart_parse;
586 }
587 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 fixptr = tail;
589 eop = *tail = nasm_malloc(sizeof(extop));
590 tail = &eop->next;
591 eop->next = NULL;
592 eop->type = EOT_NOTHING;
593 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300594 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300596 /*
597 * is_comma_next() here is to distinguish this from
598 * a string used as part of an expression...
599 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700600 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400601 eop->type = EOT_DB_STRING;
602 eop->stringval = tokval.t_charptr;
603 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300605 } else if (i == TOKEN_STRFUNC) {
606 bool parens = false;
607 const char *funcname = tokval.t_charptr;
608 enum strfunc func = tokval.t_integer;
609 i = stdscan(NULL, &tokval);
610 if (i == '(') {
611 parens = true;
612 i = stdscan(NULL, &tokval);
613 }
614 if (i != TOKEN_STR) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300615 nasm_nonfatal("%s must be followed by a string constant",
616 funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300617 eop->type = EOT_NOTHING;
618 } else {
619 eop->type = EOT_DB_STRING_FREE;
620 eop->stringlen =
621 string_transform(tokval.t_charptr, tokval.t_inttwo,
622 &eop->stringval, func);
623 if (eop->stringlen == (size_t)-1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300624 nasm_nonfatal("invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300625 eop->type = EOT_NOTHING;
626 }
627 }
628 if (parens && i && i != ')') {
629 i = stdscan(NULL, &tokval);
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300630 if (i != ')')
631 nasm_nonfatal("unterminated %s function", funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300632 }
633 if (i && i != ',')
634 i = stdscan(NULL, &tokval);
635 } else if (i == '-' || i == '+') {
636 char *save = stdscan_get();
637 int token = i;
638 sign = (i == '-') ? -1 : 1;
639 i = stdscan(NULL, &tokval);
640 if (i != TOKEN_FLOAT) {
641 stdscan_set(save);
642 i = tokval.t_type = token;
643 goto is_expression;
644 } else {
645 goto is_float;
646 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700647 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300648is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300649 eop->type = EOT_DB_STRING;
650 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300651
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700652 eop->stringlen = db_bytes(result->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300653 if (eop->stringlen > 16) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300654 nasm_nonfatal("floating-point constant"
655 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300656 eop->stringlen = 0;
657 } else if (eop->stringlen < 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300658 nasm_nonfatal("floating-point constant"
659 " encountered in unknown instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300660 /*
661 * fix suggested by Pedro Gimeno... original line was:
662 * eop->type = EOT_NOTHING;
663 */
664 eop->stringlen = 0;
665 }
666
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300667 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
668 tail = &eop->next;
669 *fixptr = eop;
670 eop->stringval = (char *)eop + sizeof(extop);
671 if (!eop->stringlen ||
672 !float_const(tokval.t_charptr, sign,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800673 (uint8_t *)eop->stringval, eop->stringlen))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300674 eop->type = EOT_NOTHING;
675 i = stdscan(NULL, &tokval); /* eat the comma */
676 } else {
677 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700679
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300680is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800682 critical, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700684 if (!value) /* Error in evaluator */
685 goto fail;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700686 if (value_to_extop(value, eop, location.segment)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300687 nasm_nonfatal("operand %d: expression is not simple or relocatable",
688 oper_num);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 }
690 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000691
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692 /*
693 * We're about to call stdscan(), which will eat the
694 * comma that we're currently sitting on between
695 * arguments. However, we'd better check first that it
696 * _is_ a comma.
697 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400698 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 break;
700 if (i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300701 nasm_nonfatal("comma expected after operand %d", oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700702 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 }
704 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000705
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 if (result->opcode == I_INCBIN) {
707 /*
708 * Correct syntax for INCBIN is that there should be
709 * one string operand, followed by one or two numeric
710 * operands.
711 */
712 if (!result->eops || result->eops->type != EOT_DB_STRING)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300713 nasm_nonfatal("`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 else if (result->eops->next &&
715 result->eops->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300716 nasm_nonfatal("`incbin': second parameter is"
717 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 else if (result->eops->next && result->eops->next->next &&
719 result->eops->next->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300720 nasm_nonfatal("`incbin': third parameter is"
721 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 else if (result->eops->next && result->eops->next->next &&
723 result->eops->next->next->next)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300724 nasm_nonfatal("`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000725 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000726 return result;
727 /*
728 * If we reach here, one of the above errors happened.
729 * Throw the instruction away.
730 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700731 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 } else /* DB ... */ if (oper_num == 0)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300733 nasm_warnf(ERR_PASS1, "no operand for data declaration");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000734 else
735 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000736
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000738 }
739
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400740 /*
741 * Now we begin to parse the operands. There may be up to four
742 * of these, separated by commas, and terminated by a zero token.
743 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000744
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700745 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
746 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300747 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700748 bool mref; /* is this going to be a memory ref? */
749 bool bracket; /* is it a [] mref, or a & mref? */
750 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700752 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000753
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700754 op->disp_size = 0; /* have to zero this whatever */
755 op->eaflags = 0; /* and this */
756 op->opflags = 0;
757 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000758
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400760 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300762 else if (first && i == ':') {
763 insn_is_label = true;
764 goto restart_parse;
765 }
766 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700767 op->type = 0; /* so far, no override */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 while (i == TOKEN_SPECIAL) { /* size specifiers */
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800769 switch (tokval.t_integer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 case S_BYTE:
771 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700772 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 setsize = 1;
774 break;
775 case S_WORD:
776 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700777 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 setsize = 1;
779 break;
780 case S_DWORD:
781 case S_LONG:
782 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700783 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 setsize = 1;
785 break;
786 case S_QWORD:
787 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700788 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 setsize = 1;
790 break;
791 case S_TWORD:
792 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700793 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 setsize = 1;
795 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700796 case S_OWORD:
797 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700798 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700799 setsize = 1;
800 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700801 case S_YWORD:
802 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700803 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700804 setsize = 1;
805 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700806 case S_ZWORD:
807 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700808 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700809 setsize = 1;
810 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700812 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 break;
814 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700815 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 break;
817 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700818 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 break;
820 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700821 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 break;
823 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700824 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 break;
826 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300827 nasm_nonfatal("invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 }
829 i = stdscan(NULL, &tokval);
830 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000831
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700833 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700835 i = stdscan(NULL, &tokval); /* then skip the colon */
836 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700837 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700838 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700840 /* when a comma follows an opening bracket - [ , eax*4] */
841 if (i == ',') {
842 /* treat as if there is a zero displacement virtually */
843 tokval.t_type = TOKEN_NUM;
844 tokval.t_integer = 0;
845 stdscan_set(stdscan_get() - 1); /* rewind the comma */
846 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700848 mref = false;
849 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000851
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700852 if ((op->type & FAR) && !mref &&
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300853 result->opcode != I_JMP && result->opcode != I_CALL)
854 nasm_nonfatal("invalid use of FAR operand specifier");
Debbie Wiles63b53f72002-06-04 19:31:24 +0000855
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800857 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700859 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700860 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700862 if (!value) /* Error in evaluator */
863 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 if (i == ':' && mref) { /* it was seg:offset */
865 /*
866 * Process the segment override.
867 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400868 if (value[1].type != 0 ||
869 value->value != 1 ||
870 !IS_SREG(value->type))
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300871 nasm_nonfatal("invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700872 else if (result->prefixes[PPS_SEG])
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300873 nasm_nonfatal("instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000874 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300875 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400876 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700877 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300878 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000879
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700881 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700882 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 i = stdscan(NULL, &tokval);
884 }
885 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800886 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700888 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700889 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 }
891 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700892 if (!value) /* Error in evaluator */
893 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200895
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700896 mib = false;
897 if (mref && bracket && i == ',') {
898 /* [seg:base+offset,index*scale] syntax (mib) */
899
900 operand o1, o2; /* Partial operands */
901
902 if (parse_mref(&o1, value))
903 goto fail;
904
905 i = stdscan(NULL, &tokval); /* Eat comma */
906 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800907 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700908 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +0300909 if (!value)
910 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700911
912 if (parse_mref(&o2, value))
913 goto fail;
914
915 if (o2.basereg != -1 && o2.indexreg == -1) {
916 o2.indexreg = o2.basereg;
917 o2.scale = 1;
918 o2.basereg = -1;
919 }
920
921 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
922 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300923 nasm_nonfatal("invalid mib expression");
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700924 goto fail;
925 }
926
927 op->basereg = o1.basereg;
928 op->indexreg = o2.indexreg;
929 op->scale = o2.scale;
930 op->offset = o1.offset;
931 op->segment = o1.segment;
932 op->wrt = o1.wrt;
933
934 if (op->basereg != -1) {
935 op->hintbase = op->basereg;
936 op->hinttype = EAH_MAKEBASE;
937 } else if (op->indexreg != -1) {
938 op->hintbase = op->indexreg;
939 op->hinttype = EAH_NOTBASE;
940 } else {
941 op->hintbase = -1;
942 op->hinttype = EAH_NOHINT;
943 }
944
945 mib = true;
946 }
947
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700948 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 if (mref && bracket) { /* find ] at the end */
950 if (i != ']') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300951 nasm_nonfatal("parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200952 recover = true;
953 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 i = stdscan(NULL, &tokval);
H. Peter Anvinc33d95f2017-03-31 14:37:24 -0700955 if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
956 /* parse opmask (and zeroing) after an operand */
957 recover = parse_braces(&brace_flags);
958 i = tokval.t_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700959 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200960 if (i != 0 && i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300961 nasm_nonfatal("comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200962 recover = true;
963 }
964 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700966 if (i != 0 && i != ',' && i != ':' &&
967 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300968 nasm_nonfatal("comma, colon, decorator or end of "
969 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200970 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700972 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700973 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
974 /* parse opmask (and zeroing) after an operand */
975 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 }
977 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200978 if (recover) {
979 do { /* error recovery */
980 i = stdscan(NULL, &tokval);
981 } while (i != 0 && i != ',');
982 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000983
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300984 /*
985 * now convert the exprs returned from evaluate()
986 * into operand descriptions...
987 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700988 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000989
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700991 /* A mib reference was fully parsed already */
992 if (!mib) {
993 if (parse_mref(op, value))
994 goto fail;
995 op->hintbase = hints.base;
996 op->hinttype = hints.type;
997 }
998 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001001 op->type |= IMMEDIATE;
1002 op->opflags |= OPFLAG_UNKNOWN;
1003 op->offset = 0; /* don't care */
1004 op->segment = NO_SEG; /* don't care again */
1005 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001006
Chang S. Baea5786342018-08-15 23:22:21 +03001007 if(optimizing.level >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001008 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001009 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001010 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001011 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvin87646092017-02-28 17:44:24 -08001013 uint64_t n = reloc_value(value);
1014
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001015 op->type |= IMMEDIATE;
H. Peter Anvin87646092017-02-28 17:44:24 -08001016 op->offset = n;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001017 op->segment = reloc_seg(value);
1018 op->wrt = reloc_wrt(value);
H. Peter Anvin164d2462017-02-20 02:39:56 -08001019 op->opflags |= is_self_relative(value) ? OPFLAG_RELATIVE : 0;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001022 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001023 op->type |= UNITY;
Chang S. Baea5786342018-08-15 23:22:21 +03001024 if (optimizing.level >= 0 && !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001025 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001026 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001027 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001028 op->type |= SBYTEWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001029 if (n <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001030 op->type |= UDWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001031 if (n + UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 }
1034 }
H. Peter Anvin164d2462017-02-20 02:39:56 -08001035 } else if (value->type == EXPR_RDSAE) {
Jin Kyu Song72018a22013-08-05 20:46:18 -07001036 /*
1037 * it's not an operand but a rounding or SAE decorator.
1038 * put the decorator information in the (opflag_t) type field
1039 * of previous operand.
1040 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001041 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001042 switch (value->value) {
1043 case BRC_RN:
1044 case BRC_RU:
1045 case BRC_RD:
1046 case BRC_RZ:
1047 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001048 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001049 result->evex_rm = value->value;
1050 break;
1051 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001052 nasm_nonfatal("invalid decorator");
Jin Kyu Song72018a22013-08-05 20:46:18 -07001053 break;
1054 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001056 opflags_t rs;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001057 uint64_t regset_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 if (value->type >= EXPR_SIMPLE || value->value != 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001060 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001061 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 /*
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001065 * We do not allow any kind of expression, except for
1066 * reg+value in which case it is a register set.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 */
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001068 for (i = 1; value[i].type; i++) {
1069 if (!value[i].value)
1070 continue;
1071
1072 switch (value[i].type) {
1073 case EXPR_SIMPLE:
1074 if (!regset_size) {
1075 regset_size = value[i].value + 1;
1076 break;
1077 }
1078 /* fallthrough */
1079 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001080 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001081 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 }
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001083 }
1084
1085 if ((regset_size & (regset_size - 1)) ||
1086 regset_size >= (UINT64_C(1) << REGSET_BITS)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001087 nasm_nonfatalf(ERR_PASS2, "invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001088 regset_size = 0;
1089 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001092 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 /*
1094 * we want to produce a warning iff the specified size
1095 * is different from the register size
1096 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001097 rs = op->type & SIZE_MASK;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001098 } else {
H. Peter Anvin68222142007-11-18 22:18:09 -08001099 rs = 0;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001100 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001102 /*
1103 * Make sure we're not out of nasm_reg_flags, still
1104 * probably this should be fixed when we're defining
1105 * the label.
1106 *
1107 * An easy trigger is
1108 *
1109 * e equ 0x80000000:0
1110 * pshufw word e-0
1111 *
1112 */
1113 if (value->type < EXPR_REG_START ||
1114 value->type > EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001115 nasm_nonfatal("invalid operand type");
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001116 goto fail;
1117 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001119 op->type &= TO;
1120 op->type |= REGISTER;
1121 op->type |= nasm_reg_flags[value->type];
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001122 op->type |= (regset_size >> 1) << REGSET_SHIFT;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001123 op->decoflags |= brace_flags;
1124 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001126 if (rs && (op->type & SIZE_MASK) != rs)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001127 nasm_warnf(ERR_PASS1, "register size specification ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 }
1129 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001130
1131 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001132 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1133 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001134 }
1135
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001136 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001137
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001138 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001139 while (opnum < MAX_OPERANDS)
1140 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001141
1142 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -07001143 * Transform RESW, RESD, RESQ, REST, RESO, RESY, RESZ into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001144 */
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001145 if (opcode_is_resb(result->opcode)) {
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -07001146 result->oprs[0].offset *= resb_bytes(result->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001147 result->oprs[0].offset *= result->times;
1148 result->times = 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 result->opcode = I_RESB;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001150 }
1151
1152 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001153
1154fail:
1155 result->opcode = I_none;
1156 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001157}
1158
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001160{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001161 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001162 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001163 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001164
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001165 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001167 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001168
H. Peter Anvin76690a12002-04-30 20:52:49 +00001169 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001170}
1171
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001173{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001174 extop *e;
1175
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001176 while ((e = i->eops)) {
1177 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001178 if (e->type == EOT_DB_STRING_FREE)
1179 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001181 }
1182}