blob: 072e884299937e3956f5827c4deddd23724da26e [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvin (Intel)b1e15f42019-08-09 02:44:46 -07003 * Copyright 1996-2019 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 Anvinc2f3f262018-12-27 12:37:25 -080040#include "nctype.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000041
42#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000043#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080045#include "error.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000046#include "stdscan.h"
H. Peter Anvin00444ae2009-07-18 18:49:55 -070047#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048#include "parser.h"
49#include "float.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080050#include "assemble.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070051#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052
H. Peter Anvind0e365d2002-05-26 18:19:19 +000053
H. Peter Anvine2c80182005-01-15 22:15:51 +000054static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000056static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000057
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040058static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070059{
60 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080061 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030062 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070063 case R_CS:
64 case R_DS:
65 case R_SS:
66 case R_ES:
67 case R_FS:
68 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030069 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070070 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080071 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070072 case P_REP:
73 case P_REPE:
74 case P_REPZ:
75 case P_REPNE:
76 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080077 case P_XACQUIRE:
78 case P_XRELEASE:
Jin Kyu Song03041092013-10-15 19:38:51 -070079 case P_BND:
Jin Kyu Songb287ff02013-12-04 20:05:55 -080080 case P_NOBND:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080081 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070082 case P_O16:
83 case P_O32:
84 case P_O64:
85 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030086 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070087 case P_A16:
88 case P_A32:
89 case P_A64:
90 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030091 return PPS_ASIZE;
Jin Kyu Song945b1b82013-10-25 19:29:53 -070092 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -080093 case P_VEX3:
94 case P_VEX2:
95 return PPS_VEX;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070096 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -070097 nasm_panic("Invalid value %d passed to prefix_slot()", prefix);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030098 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070099 }
100}
101
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700102static void process_size_override(insn *result, operand *op)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700103{
104 if (tasm_compatible_mode) {
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800105 switch (tokval.t_integer) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300106 /* For TASM compatibility a size override inside the
107 * brackets changes the size of the operand, not the
108 * address type of the operand as it does in standard
109 * NASM syntax. Hence:
110 *
111 * mov eax,[DWORD val]
112 *
113 * is valid syntax in TASM compatibility mode. Note that
114 * you lose the ability to override the default address
115 * type for the instruction, but we never use anything
116 * but 32-bit flat model addressing in our code.
117 */
118 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700119 op->type |= BITS8;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300120 break;
121 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700122 op->type |= BITS16;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300123 break;
124 case S_DWORD:
125 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700126 op->type |= BITS32;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300127 break;
128 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700129 op->type |= BITS64;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300130 break;
131 case S_TWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700132 op->type |= BITS80;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300133 break;
134 case S_OWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700135 op->type |= BITS128;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300136 break;
137 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300138 nasm_nonfatal("invalid operand size specification");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300139 break;
140 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700141 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300142 /* Standard NASM compatible syntax */
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800143 switch (tokval.t_integer) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300144 case S_NOSPLIT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700145 op->eaflags |= EAF_TIMESTWO;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300146 break;
147 case S_REL:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700148 op->eaflags |= EAF_REL;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300149 break;
150 case S_ABS:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700151 op->eaflags |= EAF_ABS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300152 break;
153 case S_BYTE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700154 op->disp_size = 8;
155 op->eaflags |= EAF_BYTEOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300156 break;
157 case P_A16:
158 case P_A32:
159 case P_A64:
160 if (result->prefixes[PPS_ASIZE] &&
161 result->prefixes[PPS_ASIZE] != tokval.t_integer)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300162 nasm_nonfatal("conflicting address size specifications");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300163 else
164 result->prefixes[PPS_ASIZE] = tokval.t_integer;
165 break;
166 case S_WORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700167 op->disp_size = 16;
168 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300169 break;
170 case S_DWORD:
171 case S_LONG:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700172 op->disp_size = 32;
173 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300174 break;
175 case S_QWORD:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700176 op->disp_size = 64;
177 op->eaflags |= EAF_WORDOFFS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300178 break;
179 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300180 nasm_nonfatal("invalid size specification in"
181 " effective address");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300182 break;
183 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700184 }
185}
186
Jin Kyu Song72018a22013-08-05 20:46:18 -0700187/*
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700188 * Brace decorators are are parsed here. opmask and zeroing
189 * decorators can be placed in any order. e.g. zmm1 {k2}{z} or zmm2
190 * {z}{k3} decorator(s) are placed at the end of an operand.
Jin Kyu Song72018a22013-08-05 20:46:18 -0700191 */
192static bool parse_braces(decoflags_t *decoflags)
193{
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700194 int i, j;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700195
196 i = tokval.t_type;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700197
198 while (true) {
199 switch (i) {
200 case TOKEN_OPMASK:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700201 if (*decoflags & OPMASK_MASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300202 nasm_nonfatal("opmask k%"PRIu64" is already set",
203 *decoflags & OPMASK_MASK);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700204 *decoflags &= ~OPMASK_MASK;
205 }
206 *decoflags |= VAL_OPMASK(nasm_regvals[tokval.t_integer]);
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700207 break;
208 case TOKEN_DECORATOR:
209 j = tokval.t_integer;
210 switch (j) {
Jin Kyu Song72018a22013-08-05 20:46:18 -0700211 case BRC_Z:
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700212 *decoflags |= Z_MASK;
213 break;
214 case BRC_1TO2:
215 case BRC_1TO4:
216 case BRC_1TO8:
217 case BRC_1TO16:
218 *decoflags |= BRDCAST_MASK | VAL_BRNUM(j - BRC_1TO2);
Jin Kyu Song72018a22013-08-05 20:46:18 -0700219 break;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300221 nasm_nonfatal("{%s} is not an expected decorator",
222 tokval.t_charptr);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700224 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700225 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700226 case ',':
227 case TOKEN_EOS:
228 return false;
229 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300230 nasm_nonfatal("only a series of valid decorators expected");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700231 return true;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700232 }
233 i = stdscan(NULL, &tokval);
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700234 }
Jin Kyu Song72018a22013-08-05 20:46:18 -0700235}
236
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700237static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700238{
239 int b, i, s; /* basereg, indexreg, scale */
240 int64_t o; /* offset */
241
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700242 b = i = -1;
243 o = s = 0;
H. Peter Anvin164d2462017-02-20 02:39:56 -0800244 op->segment = op->wrt = NO_SEG;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700245
246 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
247 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
248
249 if (is_gpr && e->value == 1)
250 b = e->type; /* It can be basereg */
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700251 else /* No, it has to be indexreg */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700252 i = e->type, s = e->value;
253 e++;
254 }
255 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
256 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
257
258 if (b != -1) /* If the first was the base, ... */
259 i = e->type, s = e->value; /* second has to be indexreg */
260
261 else if (!is_gpr || e->value != 1) {
262 /* If both want to be index */
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300263 nasm_nonfatal("invalid effective address: two index registers");
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700264 return -1;
265 } else
266 b = e->type;
267 e++;
268 }
H. Peter Anvin164d2462017-02-20 02:39:56 -0800269
270 if (e->type) { /* is there an offset? */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700271 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300272 nasm_nonfatal("invalid effective address: impossible register");
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700273 return -1;
274 } else {
275 if (e->type == EXPR_UNKNOWN) {
276 op->opflags |= OPFLAG_UNKNOWN;
277 o = 0; /* doesn't matter what */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700278 while (e->type)
279 e++; /* go to the end of the line */
280 } else {
281 if (e->type == EXPR_SIMPLE) {
282 o = e->value;
283 e++;
284 }
285 if (e->type == EXPR_WRT) {
286 op->wrt = e->value;
287 e++;
H. Peter Anvin164d2462017-02-20 02:39:56 -0800288 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700289 /*
290 * Look for a segment base type.
291 */
H. Peter Anvin164d2462017-02-20 02:39:56 -0800292 for (; e->type; e++) {
293 if (!e->value)
294 continue;
295
296 if (e->type <= EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300297 nasm_nonfatal("invalid effective address: too many registers");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800298 return -1;
299 } else if (e->type < EXPR_SEGBASE) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300300 nasm_nonfatal("invalid effective address: bad subexpression type");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800301 return -1;
302 } else if (e->value == 1) {
303 if (op->segment != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300304 nasm_nonfatal("invalid effective address: multiple base segments");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800305 return -1;
306 }
307 op->segment = e->type - EXPR_SEGBASE;
308 } else if (e->value == -1 &&
309 e->type == location.segment + EXPR_SEGBASE &&
310 !(op->opflags & OPFLAG_RELATIVE)) {
311 op->opflags |= OPFLAG_RELATIVE;
312 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300313 nasm_nonfatal("invalid effective address: impossible segment base multiplier");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800314 return -1;
315 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700316 }
317 }
318 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700319 }
320
H. Peter Anvin164d2462017-02-20 02:39:56 -0800321 nasm_assert(!e->type); /* We should be at the end */
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700322
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700323 op->basereg = b;
324 op->indexreg = i;
325 op->scale = s;
326 op->offset = o;
327 return 0;
328}
329
330static void mref_set_optype(operand *op)
331{
332 int b = op->basereg;
333 int i = op->indexreg;
334 int s = op->scale;
335
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700336 /* It is memory, but it can match any r/m operand */
337 op->type |= MEMORY_ANY;
338
339 if (b == -1 && (i == -1 || s == 0)) {
340 int is_rel = globalbits == 64 &&
341 !(op->eaflags & EAF_ABS) &&
342 ((globalrel &&
343 !(op->eaflags & EAF_FSGS)) ||
344 (op->eaflags & EAF_REL));
345
346 op->type |= is_rel ? IP_REL : MEM_OFFS;
347 }
348
349 if (i != -1) {
350 opflags_t iclass = nasm_reg_flags[i];
351
352 if (is_class(XMMREG,iclass))
353 op->type |= XMEM;
354 else if (is_class(YMMREG,iclass))
355 op->type |= YMEM;
356 else if (is_class(ZMMREG,iclass))
357 op->type |= ZMEM;
358 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700359}
360
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700361/*
362 * Convert an expression vector returned from evaluate() into an
363 * extop structure. Return zero on success.
364 */
365static int value_to_extop(expr * vect, extop *eop, int32_t myseg)
366{
367 eop->type = EOT_DB_NUMBER;
368 eop->offset = 0;
369 eop->segment = eop->wrt = NO_SEG;
370 eop->relative = false;
371
372 for (; vect->type; vect++) {
373 if (!vect->value) /* zero term, safe to ignore */
374 continue;
375
Cyrill Gorcunovfd610f22016-11-28 23:57:08 +0300376 if (vect->type <= EXPR_REG_END) /* false if a register is present */
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700377 return -1;
378
379 if (vect->type == EXPR_UNKNOWN) /* something we can't resolve yet */
380 return 0;
381
382 if (vect->type == EXPR_SIMPLE) {
383 /* Simple number expression */
384 eop->offset += vect->value;
385 continue;
386 }
387 if (eop->wrt == NO_SEG && !eop->relative && vect->type == EXPR_WRT) {
388 /* WRT term */
389 eop->wrt = vect->value;
390 continue;
391 }
392
H. Peter Anvind97ccee2017-02-21 11:31:35 -0800393 if (!eop->relative &&
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700394 vect->type == EXPR_SEGBASE + myseg && vect->value == -1) {
395 /* Expression of the form: foo - $ */
396 eop->relative = true;
397 continue;
398 }
399
400 if (eop->segment == NO_SEG && vect->type >= EXPR_SEGBASE &&
401 vect->value == 1) {
402 eop->segment = vect->type - EXPR_SEGBASE;
403 continue;
404 }
405
406 /* Otherwise, badness */
407 return -1;
408 }
409
410 /* We got to the end and it was all okay */
411 return 0;
412}
413
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800414insn *parse_line(char *buffer, insn *result)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000415{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400416 bool insn_is_label = false;
417 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700418 int opnum;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800419 bool critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800420 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700421 bool recover;
Martin Lindhe58f37c12016-11-16 16:43:16 +0100422 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000423
H. Peter Anvin7daa26f2018-06-02 23:48:16 -0700424 nasm_static_assert(P_none == 0);
425
H. Peter Anvin9c987692007-11-04 21:09:32 -0800426restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400427 first = true;
428 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000429
H. Peter Anvin76690a12002-04-30 20:52:49 +0000430 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300431 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000433
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700434 memset(result->prefixes, P_none, sizeof(result->prefixes));
435 result->times = 1; /* No TIMES either yet */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400436 result->label = NULL; /* Assume no label */
437 result->eops = NULL; /* must do this, whatever happens */
438 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700439 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
440 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000441
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400442 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700443 if (i == TOKEN_EOS)
444 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400445
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400446 if (i != TOKEN_ID &&
447 i != TOKEN_INSN &&
448 i != TOKEN_PREFIX &&
449 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300450 nasm_nonfatal("label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700451 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000452 }
453
H. Peter Anvin9c987692007-11-04 21:09:32 -0800454 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
455 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300456 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 result->label = tokval.t_charptr;
458 i = stdscan(NULL, &tokval);
459 if (i == ':') { /* skip over the optional colon */
460 i = stdscan(NULL, &tokval);
461 } else if (i == 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800462 /*!
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700463 *!label-orphan [on] labels alone on lines without trailing `:'
464 *!=orphan-labels
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800465 *! warns about source lines which contain no instruction but define
466 *! a label without a trailing colon. This is most likely indicative
467 *! of a typo, but is technically correct NASM syntax (see \k{syntax}.)
468 */
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700469 nasm_warn(WARN_LABEL_ORPHAN ,
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300470 "label alone on a line without a colon might be in error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 }
472 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
473 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800474 * FIXME: location.segment could be NO_SEG, in which case
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800475 * it is possible we should be passing 'absolute.segment'. Look into this.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 * Work out whether that is *really* what we should be doing.
477 * Generally fix things. I think this is right as it is, but
478 * am still not certain.
479 */
H. Peter Anvin (Intel)415b6b32018-06-25 14:09:52 -0700480 define_label(result->label,
481 in_absolute ? absolute.segment : location.segment,
H. Peter Anvin98578072018-06-01 18:02:54 -0700482 location.offset, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000484 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000485
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400486 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700487 if (i == TOKEN_EOS)
488 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000489
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000490 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400491 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300492 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800493
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 /*
495 * Handle special case: the TIMES prefix.
496 */
497 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
498 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000499
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 i = stdscan(NULL, &tokval);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800501 value = evaluate(stdscan, NULL, &tokval, NULL, pass_stable(), NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700503 if (!value) /* Error in evaluator */
504 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 if (!is_simple(value)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300506 nasm_nonfatal("non-constant argument supplied to TIMES");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 result->times = 1L;
508 } else {
509 result->times = value->value;
H. Peter Anvin94ead272017-09-27 15:22:23 -0700510 if (value->value < 0) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300511 nasm_nonfatalf(ERR_PASS2, "TIMES value %"PRId64" is negative", value->value);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 result->times = 0;
513 }
514 }
515 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300516 int slot = prefix_slot(tokval.t_integer);
517 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700518 if (result->prefixes[slot] == tokval.t_integer)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800519 nasm_warn(WARN_OTHER, "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700520 else
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300521 nasm_nonfatal("instruction has conflicting prefixes");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300522 }
523 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 i = stdscan(NULL, &tokval);
525 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000526 }
527
528 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300529 int j;
530 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700531
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400532 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300533 if ((pfx = result->prefixes[j]) != P_none)
534 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400535 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700536
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700537 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 /*
539 * Instruction prefixes are present, but no actual
540 * instruction. This is allowed: at this point we
541 * invent a notional instruction of RESB 0.
542 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400543 result->opcode = I_RESB;
544 result->operands = 1;
H. Peter Anvin1980abf2017-03-31 14:52:03 -0700545 nasm_zero(result->oprs);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400546 result->oprs[0].type = IMMEDIATE;
547 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
549 return result;
550 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300551 nasm_nonfatal("parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700552 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000554 }
555
556 result->opcode = tokval.t_integer;
557 result->condition = tokval.t_inttwo;
558
559 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700560 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000561 * evaluated operands, since the correct values _must_ be known
562 * on the first pass. Hence, even in pass one, we set the
563 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700564 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000565 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800566 critical = pass_final() || (result->opcode == I_INCBIN);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000567
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700568 if (opcode_is_db(result->opcode) || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 extop *eop, **tail = &result->eops, **fixptr;
570 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300571 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000572
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700573 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000574
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 /*
H. Peter Anvin9d546102013-10-02 18:25:19 -0700576 * Begin to read the DB/DW/DD/DQ/DT/DO/DY/DZ/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 */
578 while (1) {
579 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400580 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300582 else if (first && i == ':') {
583 insn_is_label = true;
584 goto restart_parse;
585 }
586 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 fixptr = tail;
588 eop = *tail = nasm_malloc(sizeof(extop));
589 tail = &eop->next;
590 eop->next = NULL;
591 eop->type = EOT_NOTHING;
592 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300593 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000594
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300595 /*
596 * is_comma_next() here is to distinguish this from
597 * a string used as part of an expression...
598 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700599 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400600 eop->type = EOT_DB_STRING;
601 eop->stringval = tokval.t_charptr;
602 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300604 } else if (i == TOKEN_STRFUNC) {
605 bool parens = false;
606 const char *funcname = tokval.t_charptr;
607 enum strfunc func = tokval.t_integer;
608 i = stdscan(NULL, &tokval);
609 if (i == '(') {
610 parens = true;
611 i = stdscan(NULL, &tokval);
612 }
613 if (i != TOKEN_STR) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300614 nasm_nonfatal("%s must be followed by a string constant",
615 funcname);
H. Peter Anvin236f4a82019-06-06 17:17:16 -0700616 eop->type = EOT_NOTHING;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300617 } else {
618 eop->type = EOT_DB_STRING_FREE;
619 eop->stringlen =
620 string_transform(tokval.t_charptr, tokval.t_inttwo,
621 &eop->stringval, func);
622 if (eop->stringlen == (size_t)-1) {
H. Peter Anvin236f4a82019-06-06 17:17:16 -0700623 nasm_nonfatal("invalid input string to %s", funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300624 eop->type = EOT_NOTHING;
625 }
626 }
627 if (parens && i && i != ')') {
628 i = stdscan(NULL, &tokval);
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300629 if (i != ')')
630 nasm_nonfatal("unterminated %s function", funcname);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300631 }
632 if (i && i != ',')
633 i = stdscan(NULL, &tokval);
634 } else if (i == '-' || i == '+') {
635 char *save = stdscan_get();
636 int token = i;
637 sign = (i == '-') ? -1 : 1;
638 i = stdscan(NULL, &tokval);
639 if (i != TOKEN_FLOAT) {
640 stdscan_set(save);
641 i = tokval.t_type = token;
642 goto is_expression;
643 } else {
644 goto is_float;
645 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700646 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300647is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300648 eop->type = EOT_DB_STRING;
649 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300650
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700651 eop->stringlen = db_bytes(result->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300652 if (eop->stringlen > 16) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300653 nasm_nonfatal("floating-point constant"
654 " encountered in DY or DZ instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300655 eop->stringlen = 0;
656 } else if (eop->stringlen < 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300657 nasm_nonfatal("floating-point constant"
658 " encountered in unknown instruction");
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300659 /*
660 * fix suggested by Pedro Gimeno... original line was:
661 * eop->type = EOT_NOTHING;
662 */
663 eop->stringlen = 0;
664 }
665
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300666 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
667 tail = &eop->next;
668 *fixptr = eop;
669 eop->stringval = (char *)eop + sizeof(extop);
670 if (!eop->stringlen ||
671 !float_const(tokval.t_charptr, sign,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800672 (uint8_t *)eop->stringval, eop->stringlen))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300673 eop->type = EOT_NOTHING;
674 i = stdscan(NULL, &tokval); /* eat the comma */
675 } else {
676 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700678
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300679is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800681 critical, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700683 if (!value) /* Error in evaluator */
684 goto fail;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700685 if (value_to_extop(value, eop, location.segment)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300686 nasm_nonfatal("operand %d: expression is not simple or relocatable",
687 oper_num);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 }
689 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000690
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 /*
692 * We're about to call stdscan(), which will eat the
693 * comma that we're currently sitting on between
694 * arguments. However, we'd better check first that it
695 * _is_ a comma.
696 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400697 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 break;
699 if (i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300700 nasm_nonfatal("comma expected after operand %d", oper_num);
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700701 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 }
703 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000704
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 if (result->opcode == I_INCBIN) {
706 /*
707 * Correct syntax for INCBIN is that there should be
708 * one string operand, followed by one or two numeric
709 * operands.
710 */
711 if (!result->eops || result->eops->type != EOT_DB_STRING)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300712 nasm_nonfatal("`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 else if (result->eops->next &&
714 result->eops->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300715 nasm_nonfatal("`incbin': second parameter is"
716 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717 else if (result->eops->next && result->eops->next->next &&
718 result->eops->next->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300719 nasm_nonfatal("`incbin': third parameter is"
720 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721 else if (result->eops->next && result->eops->next->next &&
722 result->eops->next->next->next)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300723 nasm_nonfatal("`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 return result;
726 /*
727 * If we reach here, one of the above errors happened.
728 * Throw the instruction away.
729 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700730 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000731 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800732 nasm_warn(WARN_OTHER, "no operand for data declaration");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 else
734 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000735
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000737 }
738
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400739 /*
740 * Now we begin to parse the operands. There may be up to four
741 * of these, separated by commas, and terminated by a zero token.
742 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700744 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
745 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300746 expr *value; /* used most of the time */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700747 bool mref; /* is this going to be a memory ref? */
748 bool bracket; /* is it a [] mref, or a & mref? */
749 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700751 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000752
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700753 op->disp_size = 0; /* have to zero this whatever */
754 op->eaflags = 0; /* and this */
755 op->opflags = 0;
756 op->decoflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000757
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400759 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300761 else if (first && i == ':') {
762 insn_is_label = true;
763 goto restart_parse;
764 }
765 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700766 op->type = 0; /* so far, no override */
H. Peter Anvin11599f42018-12-22 23:09:54 -0800767 /* size specifiers */
768 while (i == TOKEN_SPECIAL || i == TOKEN_SIZE) {
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 */
H. Peter Anvin11599f42018-12-22 23:09:54 -0800836 while (i == TOKEN_SPECIAL || i == TOKEN_SIZE ||
837 i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700838 process_size_override(result, op);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700839 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 }
Jin Kyu Song164d6072013-10-15 19:10:13 -0700841 /* when a comma follows an opening bracket - [ , eax*4] */
842 if (i == ',') {
843 /* treat as if there is a zero displacement virtually */
844 tokval.t_type = TOKEN_NUM;
845 tokval.t_integer = 0;
846 stdscan_set(stdscan_get() - 1); /* rewind the comma */
847 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700849 mref = false;
850 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000852
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700853 if ((op->type & FAR) && !mref &&
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300854 result->opcode != I_JMP && result->opcode != I_CALL)
855 nasm_nonfatal("invalid use of FAR operand specifier");
Debbie Wiles63b53f72002-06-04 19:31:24 +0000856
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800858 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700860 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700861 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700863 if (!value) /* Error in evaluator */
864 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 if (i == ':' && mref) { /* it was seg:offset */
866 /*
867 * Process the segment override.
868 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400869 if (value[1].type != 0 ||
870 value->value != 1 ||
871 !IS_SREG(value->type))
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300872 nasm_nonfatal("invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700873 else if (result->prefixes[PPS_SEG])
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300874 nasm_nonfatal("instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000875 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300876 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400877 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700878 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300879 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000880
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvin11599f42018-12-22 23:09:54 -0800882 while (i == TOKEN_SPECIAL || i == TOKEN_SIZE ||
883 i == TOKEN_PREFIX) {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700884 process_size_override(result, op);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 i = stdscan(NULL, &tokval);
886 }
887 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800888 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700890 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700891 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 }
893 /* and get the offset */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700894 if (!value) /* Error in evaluator */
895 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200897
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700898 mib = false;
899 if (mref && bracket && i == ',') {
900 /* [seg:base+offset,index*scale] syntax (mib) */
901
902 operand o1, o2; /* Partial operands */
903
904 if (parse_mref(&o1, value))
905 goto fail;
906
907 i = stdscan(NULL, &tokval); /* Eat comma */
908 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800909 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700910 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +0300911 if (!value)
912 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700913
914 if (parse_mref(&o2, value))
915 goto fail;
916
917 if (o2.basereg != -1 && o2.indexreg == -1) {
918 o2.indexreg = o2.basereg;
919 o2.scale = 1;
920 o2.basereg = -1;
921 }
922
923 if (o1.indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
924 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300925 nasm_nonfatal("invalid mib expression");
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700926 goto fail;
927 }
928
929 op->basereg = o1.basereg;
930 op->indexreg = o2.indexreg;
931 op->scale = o2.scale;
932 op->offset = o1.offset;
933 op->segment = o1.segment;
934 op->wrt = o1.wrt;
935
936 if (op->basereg != -1) {
937 op->hintbase = op->basereg;
938 op->hinttype = EAH_MAKEBASE;
939 } else if (op->indexreg != -1) {
940 op->hintbase = op->indexreg;
941 op->hinttype = EAH_NOTBASE;
942 } else {
943 op->hintbase = -1;
944 op->hinttype = EAH_NOHINT;
945 }
946
947 mib = true;
948 }
949
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700950 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 if (mref && bracket) { /* find ] at the end */
952 if (i != ']') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300953 nasm_nonfatal("parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200954 recover = true;
955 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 i = stdscan(NULL, &tokval);
H. Peter Anvinc33d95f2017-03-31 14:37:24 -0700957 if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
958 /* parse opmask (and zeroing) after an operand */
959 recover = parse_braces(&brace_flags);
960 i = tokval.t_type;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700961 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200962 if (i != 0 && i != ',') {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300963 nasm_nonfatal("comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200964 recover = true;
965 }
966 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -0700968 if (i != 0 && i != ',' && i != ':' &&
969 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300970 nasm_nonfatal("comma, colon, decorator or end of "
971 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200972 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700974 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700975 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
976 /* parse opmask (and zeroing) after an operand */
977 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 }
979 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200980 if (recover) {
981 do { /* error recovery */
982 i = stdscan(NULL, &tokval);
983 } while (i != 0 && i != ',');
984 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000985
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300986 /*
987 * now convert the exprs returned from evaluate()
988 * into operand descriptions...
989 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700990 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700993 /* A mib reference was fully parsed already */
994 if (!mib) {
995 if (parse_mref(op, value))
996 goto fail;
997 op->hintbase = hints.base;
998 op->hinttype = hints.type;
999 }
1000 mref_set_optype(op);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001003 op->type |= IMMEDIATE;
1004 op->opflags |= OPFLAG_UNKNOWN;
1005 op->offset = 0; /* don't care */
1006 op->segment = NO_SEG; /* don't care again */
1007 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001008
Chang S. Baea5786342018-08-15 23:22:21 +03001009 if(optimizing.level >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001010 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001011 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001012 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001013 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvin87646092017-02-28 17:44:24 -08001015 uint64_t n = reloc_value(value);
1016
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001017 op->type |= IMMEDIATE;
H. Peter Anvin87646092017-02-28 17:44:24 -08001018 op->offset = n;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001019 op->segment = reloc_seg(value);
1020 op->wrt = reloc_wrt(value);
H. Peter Anvin164d2462017-02-20 02:39:56 -08001021 op->opflags |= is_self_relative(value) ? OPFLAG_RELATIVE : 0;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001022
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001024 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001025 op->type |= UNITY;
Chang S. Baea5786342018-08-15 23:22:21 +03001026 if (optimizing.level >= 0 && !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001027 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001028 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001029 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001030 op->type |= SBYTEWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001031 if (n <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001032 op->type |= UDWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001033 if (n + UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001034 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 }
1036 }
H. Peter Anvin164d2462017-02-20 02:39:56 -08001037 } else if (value->type == EXPR_RDSAE) {
Jin Kyu Song72018a22013-08-05 20:46:18 -07001038 /*
1039 * it's not an operand but a rounding or SAE decorator.
1040 * put the decorator information in the (opflag_t) type field
1041 * of previous operand.
1042 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001043 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001044 switch (value->value) {
1045 case BRC_RN:
1046 case BRC_RU:
1047 case BRC_RD:
1048 case BRC_RZ:
1049 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001050 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001051 result->evex_rm = value->value;
1052 break;
1053 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001054 nasm_nonfatal("invalid decorator");
Jin Kyu Song72018a22013-08-05 20:46:18 -07001055 break;
1056 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001058 opflags_t rs;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001059 uint64_t regset_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001060
H. Peter Anvine2c80182005-01-15 22:15:51 +00001061 if (value->type >= EXPR_SIMPLE || value->value != 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001062 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001063 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 /*
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001067 * We do not allow any kind of expression, except for
1068 * reg+value in which case it is a register set.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 */
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001070 for (i = 1; value[i].type; i++) {
1071 if (!value[i].value)
1072 continue;
1073
1074 switch (value[i].type) {
1075 case EXPR_SIMPLE:
1076 if (!regset_size) {
1077 regset_size = value[i].value + 1;
1078 break;
1079 }
1080 /* fallthrough */
1081 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001082 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001083 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 }
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001085 }
1086
1087 if ((regset_size & (regset_size - 1)) ||
1088 regset_size >= (UINT64_C(1) << REGSET_BITS)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001089 nasm_nonfatalf(ERR_PASS2, "invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001090 regset_size = 0;
1091 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001094 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 /*
1096 * we want to produce a warning iff the specified size
1097 * is different from the register size
1098 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001099 rs = op->type & SIZE_MASK;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001100 } else {
H. Peter Anvin68222142007-11-18 22:18:09 -08001101 rs = 0;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001102 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001104 /*
1105 * Make sure we're not out of nasm_reg_flags, still
1106 * probably this should be fixed when we're defining
1107 * the label.
1108 *
1109 * An easy trigger is
1110 *
1111 * e equ 0x80000000:0
1112 * pshufw word e-0
1113 *
1114 */
1115 if (value->type < EXPR_REG_START ||
1116 value->type > EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001117 nasm_nonfatal("invalid operand type");
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001118 goto fail;
1119 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001121 op->type &= TO;
1122 op->type |= REGISTER;
1123 op->type |= nasm_reg_flags[value->type];
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001124 op->type |= (regset_size >> 1) << REGSET_SHIFT;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001125 op->decoflags |= brace_flags;
1126 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127
H. Peter Anvin (Intel)b1e15f42019-08-09 02:44:46 -07001128 if (rs) {
1129 opflags_t opsize = nasm_reg_flags[value->type] & SIZE_MASK;
1130 if (!opsize) {
1131 op->type |= rs; /* For non-size-specific registers, permit size override */
1132 } else if (opsize != rs) {
1133 /*!
1134 *!regsize [on] register size specification ignored
1135 *!
1136 *! warns about a register with implicit size (such as \c{EAX}, which is always 32 bits)
1137 *! been given an explicit size specification which is inconsistent with the size
1138 *! of the named register, e.g. \c{WORD EAX}. \c{DWORD EAX} or \c{WORD AX} are
1139 *! permitted, and do not trigger this warning. Some registers which \e{do not} imply
1140 *! a specific size, such as \c{K0}, may need this specification unless the instruction
1141 *! itself implies the instruction size:
1142 *!
1143 *! \c KMOVW K0,[foo] ; Permitted, KMOVW implies 16 bits
1144 *! \c KMOV WORD K0,[foo] ; Permitted, WORD K0 specifies instruction size
1145 *! \c KMOV K0,WORD [foo] ; Permitted, WORD [foo] specifies instruction size
1146 *! \c KMOV K0,[foo] ; Not permitted, instruction size ambiguous
1147 */
1148 nasm_warn(WARN_REGSIZE, "invalid register size specification ignored");
1149 }
1150 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 }
1152 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001153
1154 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001155 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1156 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001157 }
1158
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001159 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001160
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001161 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001162 while (opnum < MAX_OPERANDS)
1163 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001164
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001165 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001166
1167fail:
1168 result->opcode = I_none;
1169 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001170}
1171
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001173{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001174 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001175 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001176 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001177
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001178 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001180 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001181
H. Peter Anvin76690a12002-04-30 20:52:49 +00001182 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001183}
1184
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001186{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001187 extop *e;
1188
H. Peter Anvin2aa77392008-06-15 17:39:45 -07001189 while ((e = i->eops)) {
1190 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001191 if (e->type == EOT_DB_STRING_FREE)
1192 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001194 }
1195}