blob: de24103e1d23ccf4b45f16ddc4a2838b628f57ea [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"
H. Peter Anvin (Intel)6e9554f2020-06-14 23:21:44 -070049#include "floats.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 Anvin (Intel)84b852b2019-10-16 14:29:16 -070054static int end_expression_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 Anvin (Intel)89817242019-08-14 15:24:56 -0700237static inline const expr *next_expr(const expr *e, const expr **next_list)
238{
239 e++;
240 if (!e->type) {
241 if (next_list) {
242 e = *next_list;
243 *next_list = NULL;
244 } else {
245 e = NULL;
246 }
247 }
248 return e;
249}
250
251static inline void init_operand(operand *op)
252{
253 memset(op, 0, sizeof *op);
254
255 op->basereg = -1;
256 op->indexreg = -1;
257 op->segment = NO_SEG;
258 op->wrt = NO_SEG;
259}
260
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700261static int parse_mref(operand *op, const expr *e)
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700262{
263 int b, i, s; /* basereg, indexreg, scale */
264 int64_t o; /* offset */
265
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700266 b = op->basereg;
267 i = op->indexreg;
268 s = op->scale;
269 o = op->offset;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700270
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700271 for (; e->type; e++) {
272 if (e->type <= EXPR_REG_END) {
273 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700274
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700275 if (is_gpr && e->value == 1 && b == -1) {
276 /* It can be basereg */
277 b = e->type;
278 } else if (i == -1) {
279 /* Must be index register */
280 i = e->type;
281 s = e->value;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700282 } else {
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700283 if (b == -1)
284 nasm_nonfatal("invalid effective address: two index registers");
285 else if (!is_gpr)
286 nasm_nonfatal("invalid effective address: impossible register");
287 else
288 nasm_nonfatal("invalid effective address: too many registers");
289 return -1;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700290 }
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700291 } else if (e->type == EXPR_UNKNOWN) {
292 op->opflags |= OPFLAG_UNKNOWN;
293 } else if (e->type == EXPR_SIMPLE) {
294 o += e->value;
295 } else if (e->type == EXPR_WRT) {
296 op->wrt = e->value;
297 } else if (e->type >= EXPR_SEGBASE) {
298 if (e->value == 1) {
299 if (op->segment != NO_SEG) {
300 nasm_nonfatal("invalid effective address: multiple base segments");
301 return -1;
302 }
303 op->segment = e->type - EXPR_SEGBASE;
304 } else if (e->value == -1 &&
305 e->type == location.segment + EXPR_SEGBASE &&
306 !(op->opflags & OPFLAG_RELATIVE)) {
307 op->opflags |= OPFLAG_RELATIVE;
308 } else {
309 nasm_nonfatal("invalid effective address: impossible segment base multiplier");
310 return -1;
311 }
312 } else {
313 nasm_nonfatal("invalid effective address: bad subexpression type");
314 return -1;
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700315 }
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700316 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700317
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700318 op->basereg = b;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700319 op->indexreg = i;
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700320 op->scale = s;
321 op->offset = o;
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700322 return 0;
323}
324
325static void mref_set_optype(operand *op)
326{
327 int b = op->basereg;
328 int i = op->indexreg;
329 int s = op->scale;
330
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700331 /* It is memory, but it can match any r/m operand */
332 op->type |= MEMORY_ANY;
333
334 if (b == -1 && (i == -1 || s == 0)) {
335 int is_rel = globalbits == 64 &&
336 !(op->eaflags & EAF_ABS) &&
337 ((globalrel &&
338 !(op->eaflags & EAF_FSGS)) ||
339 (op->eaflags & EAF_REL));
340
341 op->type |= is_rel ? IP_REL : MEM_OFFS;
342 }
343
344 if (i != -1) {
345 opflags_t iclass = nasm_reg_flags[i];
346
347 if (is_class(XMMREG,iclass))
348 op->type |= XMEM;
349 else if (is_class(YMMREG,iclass))
350 op->type |= YMEM;
351 else if (is_class(ZMMREG,iclass))
352 op->type |= ZMEM;
353 }
H. Peter Anvin9f4706f2013-09-26 17:28:39 -0700354}
355
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700356/*
357 * Convert an expression vector returned from evaluate() into an
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700358 * extop structure. Return zero on success. Note that the eop
359 * already has dup and elem set, so we can't clear it here.
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700360 */
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700361static int value_to_extop(expr *vect, extop *eop, int32_t myseg)
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700362{
363 eop->type = EOT_DB_NUMBER;
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700364 eop->val.num.offset = 0;
365 eop->val.num.segment = eop->val.num.wrt = NO_SEG;
366 eop->val.num.relative = false;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700367
368 for (; vect->type; vect++) {
369 if (!vect->value) /* zero term, safe to ignore */
370 continue;
371
Cyrill Gorcunovfd610f22016-11-28 23:57:08 +0300372 if (vect->type <= EXPR_REG_END) /* false if a register is present */
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700373 return -1;
374
375 if (vect->type == EXPR_UNKNOWN) /* something we can't resolve yet */
376 return 0;
377
378 if (vect->type == EXPR_SIMPLE) {
379 /* Simple number expression */
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700380 eop->val.num.offset += vect->value;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700381 continue;
382 }
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700383 if (eop->val.num.wrt == NO_SEG && !eop->val.num.relative &&
384 vect->type == EXPR_WRT) {
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700385 /* WRT term */
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700386 eop->val.num.wrt = vect->value;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700387 continue;
388 }
389
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700390 if (!eop->val.num.relative &&
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700391 vect->type == EXPR_SEGBASE + myseg && vect->value == -1) {
392 /* Expression of the form: foo - $ */
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700393 eop->val.num.relative = true;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700394 continue;
395 }
396
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700397 if (eop->val.num.segment == NO_SEG &&
398 vect->type >= EXPR_SEGBASE && vect->value == 1) {
399 eop->val.num.segment = vect->type - EXPR_SEGBASE;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700400 continue;
401 }
402
403 /* Otherwise, badness */
404 return -1;
405 }
406
407 /* We got to the end and it was all okay */
408 return 0;
409}
410
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700411/*
412 * Parse an extended expression, used by db et al. "elem" is the element
413 * size; initially comes from the specific opcode (e.g. db == 1) but
414 * can be overridden.
415 */
416static int parse_eops(extop **result, bool critical, int elem)
417{
418 extop *eop = NULL, *prev = NULL;
419 extop **tail = result;
420 int sign;
421 int i = tokval.t_type;
422 int oper_num = 0;
423 bool do_subexpr = false;
424
425 *tail = NULL;
426
427 /* End of string is obvious; ) ends a sub-expression list e.g. DUP */
428 for (i = tokval.t_type; i != TOKEN_EOS; i = stdscan(NULL, &tokval)) {
429 char endparen = ')'; /* Is a right paren the end of list? */
430
431 if (i == ')')
432 break;
433
434 if (!eop) {
435 nasm_new(eop);
436 eop->dup = 1;
437 eop->elem = elem;
438 do_subexpr = false;
439 }
440 sign = +1;
441
442 /*
443 * end_expression_next() here is to distinguish this from
444 * a string used as part of an expression...
445 */
446 if (i == TOKEN_QMARK) {
447 eop->type = EOT_DB_RESERVE;
448 } else if (do_subexpr && i == '(') {
449 extop *subexpr;
450
451 stdscan(NULL, &tokval); /* Skip paren */
452 if (parse_eops(&eop->val.subexpr, critical, eop->elem) < 0)
453 goto fail;
454
455 subexpr = eop->val.subexpr;
456 if (!subexpr) {
457 /* Subexpression is empty */
458 eop->type = EOT_NOTHING;
459 } else if (!subexpr->next) {
460 /* Subexpression is a single element, flatten */
461 eop->val = subexpr->val;
462 eop->type = subexpr->type;
463 eop->dup *= subexpr->dup;
464 nasm_free(subexpr);
465 } else {
466 eop->type = EOT_EXTOP;
467 }
468
469 /* We should have ended on a closing paren */
470 if (tokval.t_type != ')') {
471 nasm_nonfatal("expected `)' after subexpression, got `%s'",
472 i == TOKEN_EOS ?
473 "end of line" : tokval.t_charptr);
474 goto fail;
475 }
476 endparen = 0; /* This time the paren is not the end */
477 } else if (i == '%') {
478 /* %(expression_list) */
479 do_subexpr = true;
480 continue;
481 } else if (i == TOKEN_SIZE) {
482 /* Element size override */
483 eop->elem = tokval.t_inttwo;
484 do_subexpr = true;
485 continue;
486 } else if (i == TOKEN_STR && end_expression_next()) {
487 eop->type = EOT_DB_STRING;
488 eop->val.string.data = tokval.t_charptr;
489 eop->val.string.len = tokval.t_inttwo;
490 } else if (i == TOKEN_STRFUNC) {
491 bool parens = false;
492 const char *funcname = tokval.t_charptr;
493 enum strfunc func = tokval.t_integer;
494
495 i = stdscan(NULL, &tokval);
496 if (i == '(') {
497 parens = true;
498 endparen = 0;
499 i = stdscan(NULL, &tokval);
500 }
501 if (i != TOKEN_STR) {
502 nasm_nonfatal("%s must be followed by a string constant",
503 funcname);
504 eop->type = EOT_NOTHING;
505 } else {
506 eop->type = EOT_DB_STRING_FREE;
507 eop->val.string.len =
508 string_transform(tokval.t_charptr, tokval.t_inttwo,
509 &eop->val.string.data, func);
510 if (eop->val.string.len == (size_t)-1) {
511 nasm_nonfatal("invalid input string to %s", funcname);
512 eop->type = EOT_NOTHING;
513 }
514 }
515 if (parens && i && i != ')') {
516 i = stdscan(NULL, &tokval);
517 if (i != ')')
518 nasm_nonfatal("unterminated %s function", funcname);
519 }
520 } else if (i == '-' || i == '+') {
521 char *save = stdscan_get();
522 struct tokenval tmptok;
523
524 sign = (i == '-') ? -1 : 1;
525 if (stdscan(NULL, &tmptok) != TOKEN_FLOAT) {
526 stdscan_set(save);
527 goto is_expression;
528 } else {
529 tokval = tmptok;
530 goto is_float;
531 }
532 } else if (i == TOKEN_FLOAT) {
533 is_float:
534 eop->type = EOT_DB_FLOAT;
535
536 if (eop->elem > 16) {
537 nasm_nonfatal("no %d-bit floating-point format supported",
538 eop->elem << 3);
539 eop->val.string.len = 0;
540 } else if (eop->elem < 1) {
541 nasm_nonfatal("floating-point constant"
542 " encountered in unknown instruction");
543 /*
544 * fix suggested by Pedro Gimeno... original line was:
545 * eop->type = EOT_NOTHING;
546 */
547 eop->val.string.len = 0;
548 } else {
549 eop->val.string.len = eop->elem;
550
551 eop = nasm_realloc(eop, sizeof(extop) + eop->val.string.len);
552 eop->val.string.data = (char *)eop + sizeof(extop);
553 if (!float_const(tokval.t_charptr, sign,
554 (uint8_t *)eop->val.string.data,
555 eop->val.string.len))
556 eop->val.string.len = 0;
557 }
558 if (!eop->val.string.len)
559 eop->type = EOT_NOTHING;
560 } else {
561 /* anything else, assume it is an expression */
562 expr *value;
563
564 is_expression:
565 value = evaluate(stdscan, NULL, &tokval, NULL,
566 critical, NULL);
567 i = tokval.t_type;
568 if (!value) /* Error in evaluator */
569 goto fail;
570 if (tokval.t_flag & TFLAG_DUP) {
571 /* Expression followed by DUP */
572 if (!is_simple(value)) {
573 nasm_nonfatal("non-constant argument supplied to DUP");
574 goto fail;
575 } else if (value->value < 0) {
576 nasm_nonfatal("negative argument supplied to DUP");
577 goto fail;
578 }
579 eop->dup *= (size_t)value->value;
580 do_subexpr = true;
581 continue;
582 }
583 if (value_to_extop(value, eop, location.segment)) {
584 nasm_nonfatal("expression is not simple or relocatable");
585 }
586 }
587
588 if (eop->dup == 0 || eop->type == EOT_NOTHING) {
589 nasm_free(eop);
590 } else if (eop->type == EOT_DB_RESERVE &&
591 prev && prev->type == EOT_DB_RESERVE &&
592 prev->elem == eop->elem) {
593 /* Coalesce multiple EOT_DB_RESERVE */
594 prev->dup += eop->dup;
595 nasm_free(eop);
596 } else {
597 /* Add this eop to the end of the chain */
598 prev = eop;
599 *tail = eop;
600 tail = &eop->next;
601 }
602
603 oper_num++;
604 eop = NULL; /* Done with this operand */
605
606 /*
607 * We're about to call stdscan(), which will eat the
608 * comma that we're currently sitting on between
609 * arguments. However, we'd better check first that it
610 * _is_ a comma.
611 */
612 if (i == TOKEN_EOS || i == endparen) /* Already at end? */
613 break;
614 if (i != ',') {
615 i = stdscan(NULL, &tokval); /* eat the comma or final paren */
616 if (i == TOKEN_EOS || i == ')') /* got end of expression */
617 break;
618 if (i != ',') {
619 nasm_nonfatal("comma expected after operand");
620 goto fail;
621 }
622 }
623 }
624
625 return oper_num;
626
627fail:
628 if (eop)
629 nasm_free(eop);
630 return -1;
631}
632
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800633insn *parse_line(char *buffer, insn *result)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400635 bool insn_is_label = false;
636 struct eval_hints hints;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700637 int opnum;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800638 bool critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800639 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700640 bool recover;
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700641 bool far_jmp_ok;
Martin Lindhe58f37c12016-11-16 16:43:16 +0100642 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000643
H. Peter Anvin7daa26f2018-06-02 23:48:16 -0700644 nasm_static_assert(P_none == 0);
645
H. Peter Anvin9c987692007-11-04 21:09:32 -0800646restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400647 first = true;
648 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000649
H. Peter Anvin76690a12002-04-30 20:52:49 +0000650 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300651 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000652 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000653
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700654 memset(result->prefixes, P_none, sizeof(result->prefixes));
655 result->times = 1; /* No TIMES either yet */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400656 result->label = NULL; /* Assume no label */
657 result->eops = NULL; /* must do this, whatever happens */
658 result->operands = 0; /* must initialize this */
Jin Kyu Songe3a06b92013-08-28 19:15:23 -0700659 result->evex_rm = 0; /* Ensure EVEX rounding mode is reset */
660 result->evex_brerop = -1; /* Reset EVEX broadcasting/ER op position */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000661
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400662 /* Ignore blank lines */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700663 if (i == TOKEN_EOS)
664 goto fail;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400665
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400666 if (i != TOKEN_ID &&
667 i != TOKEN_INSN &&
668 i != TOKEN_PREFIX &&
669 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300670 nasm_nonfatal("label or instruction expected at start of line");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700671 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000672 }
673
H. Peter Anvin9c987692007-11-04 21:09:32 -0800674 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
675 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300676 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 result->label = tokval.t_charptr;
678 i = stdscan(NULL, &tokval);
679 if (i == ':') { /* skip over the optional colon */
680 i = stdscan(NULL, &tokval);
681 } else if (i == 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800682 /*!
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700683 *!label-orphan [on] labels alone on lines without trailing `:'
684 *!=orphan-labels
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800685 *! warns about source lines which contain no instruction but define
686 *! a label without a trailing colon. This is most likely indicative
687 *! of a typo, but is technically correct NASM syntax (see \k{syntax}.)
688 */
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700689 nasm_warn(WARN_LABEL_ORPHAN ,
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300690 "label alone on a line without a colon might be in error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 }
692 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
693 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800694 * FIXME: location.segment could be NO_SEG, in which case
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800695 * it is possible we should be passing 'absolute.segment'. Look into this.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000696 * Work out whether that is *really* what we should be doing.
697 * Generally fix things. I think this is right as it is, but
698 * am still not certain.
699 */
H. Peter Anvin (Intel)415b6b32018-06-25 14:09:52 -0700700 define_label(result->label,
701 in_absolute ? absolute.segment : location.segment,
H. Peter Anvin98578072018-06-01 18:02:54 -0700702 location.offset, true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000704 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000705
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400706 /* Just a label here */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700707 if (i == TOKEN_EOS)
708 goto fail;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000709
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000710 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400711 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300712 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 /*
715 * Handle special case: the TIMES prefix.
716 */
717 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
718 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000719
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 i = stdscan(NULL, &tokval);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800721 value = evaluate(stdscan, NULL, &tokval, NULL, pass_stable(), NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700723 if (!value) /* Error in evaluator */
724 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 if (!is_simple(value)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300726 nasm_nonfatal("non-constant argument supplied to TIMES");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 result->times = 1L;
728 } else {
729 result->times = value->value;
H. Peter Anvin94ead272017-09-27 15:22:23 -0700730 if (value->value < 0) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300731 nasm_nonfatalf(ERR_PASS2, "TIMES value %"PRId64" is negative", value->value);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 result->times = 0;
733 }
734 }
735 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300736 int slot = prefix_slot(tokval.t_integer);
737 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700738 if (result->prefixes[slot] == tokval.t_integer)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800739 nasm_warn(WARN_OTHER, "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700740 else
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300741 nasm_nonfatal("instruction has conflicting prefixes");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300742 }
743 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 i = stdscan(NULL, &tokval);
745 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746 }
747
748 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300749 int j;
750 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700751
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400752 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300753 if ((pfx = result->prefixes[j]) != P_none)
754 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400755 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700756
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700757 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 /*
759 * Instruction prefixes are present, but no actual
760 * instruction. This is allowed: at this point we
761 * invent a notional instruction of RESB 0.
762 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400763 result->opcode = I_RESB;
764 result->operands = 1;
H. Peter Anvin1980abf2017-03-31 14:52:03 -0700765 nasm_zero(result->oprs);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400766 result->oprs[0].type = IMMEDIATE;
767 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
769 return result;
770 } else {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300771 nasm_nonfatal("parser: instruction expected");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700772 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000774 }
775
776 result->opcode = tokval.t_integer;
777 result->condition = tokval.t_inttwo;
778
779 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700780 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000781 * evaluated operands, since the correct values _must_ be known
782 * on the first pass. Hence, even in pass one, we set the
783 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700784 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000785 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800786 critical = pass_final() || (result->opcode == I_INCBIN);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000787
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700788 if (opcode_is_db(result->opcode) || result->opcode == I_INCBIN) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700789 int oper_num;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000790
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700791 i = stdscan(NULL, &tokval);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000792
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700793 if (first && i == ':') {
794 /* Really a label */
795 insn_is_label = true;
796 goto restart_parse;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 }
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700798 first = false;
799 oper_num = parse_eops(&result->eops, critical, db_bytes(result->opcode));
800 if (oper_num < 0)
801 goto fail;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000802
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 if (result->opcode == I_INCBIN) {
804 /*
805 * Correct syntax for INCBIN is that there should be
806 * one string operand, followed by one or two numeric
807 * operands.
808 */
809 if (!result->eops || result->eops->type != EOT_DB_STRING)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300810 nasm_nonfatal("`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 else if (result->eops->next &&
812 result->eops->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300813 nasm_nonfatal("`incbin': second parameter is"
814 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 else if (result->eops->next && result->eops->next->next &&
816 result->eops->next->next->type != EOT_DB_NUMBER)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300817 nasm_nonfatal("`incbin': third parameter is"
818 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 else if (result->eops->next && result->eops->next->next &&
820 result->eops->next->next->next)
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300821 nasm_nonfatal("`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000822 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 return result;
824 /*
825 * If we reach here, one of the above errors happened.
826 * Throw the instruction away.
827 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700828 goto fail;
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700829 } else {
830 /* DB et al */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 result->operands = oper_num;
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700832 if (oper_num == 0)
833 nasm_warn(WARN_OTHER, "no operand for data declaration");
834 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000836 }
837
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400838 /*
839 * Now we begin to parse the operands. There may be up to four
840 * of these, separated by commas, and terminated by a zero token.
841 */
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700842 far_jmp_ok = result->opcode == I_JMP || result->opcode == I_CALL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000843
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700844 for (opnum = 0; opnum < MAX_OPERANDS; opnum++) {
845 operand *op = &result->oprs[opnum];
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300846 expr *value; /* used most of the time */
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700847 bool mref = false; /* is this going to be a memory ref? */
848 int bracket = 0; /* is it a [] mref, or a "naked" mref? */
H. Peter Anvin9148fb52013-09-27 16:39:16 -0700849 bool mib; /* compound (mib) mref? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 int setsize = 0;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700851 decoflags_t brace_flags = 0; /* flags for decorators in braces */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000852
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700853 init_operand(op);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000854
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400856 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300858 else if (first && i == ':') {
859 insn_is_label = true;
860 goto restart_parse;
861 }
862 first = false;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700863 op->type = 0; /* so far, no override */
H. Peter Anvin11599f42018-12-22 23:09:54 -0800864 /* size specifiers */
865 while (i == TOKEN_SPECIAL || i == TOKEN_SIZE) {
H. Peter Anvin09dff8b2017-03-01 01:01:37 -0800866 switch (tokval.t_integer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 case S_BYTE:
868 if (!setsize) /* we want to use only the first */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700869 op->type |= BITS8;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 setsize = 1;
871 break;
872 case S_WORD:
873 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700874 op->type |= BITS16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 setsize = 1;
876 break;
877 case S_DWORD:
878 case S_LONG:
879 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700880 op->type |= BITS32;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 setsize = 1;
882 break;
883 case S_QWORD:
884 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700885 op->type |= BITS64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 setsize = 1;
887 break;
888 case S_TWORD:
889 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700890 op->type |= BITS80;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 setsize = 1;
892 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700893 case S_OWORD:
894 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700895 op->type |= BITS128;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700896 setsize = 1;
897 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700898 case S_YWORD:
899 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700900 op->type |= BITS256;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700901 setsize = 1;
902 break;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700903 case S_ZWORD:
904 if (!setsize)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700905 op->type |= BITS512;
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700906 setsize = 1;
907 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 case S_TO:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700909 op->type |= TO;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 break;
911 case S_STRICT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700912 op->type |= STRICT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 break;
914 case S_FAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700915 op->type |= FAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 break;
917 case S_NEAR:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700918 op->type |= NEAR;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 break;
920 case S_SHORT:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700921 op->type |= SHORT;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 break;
923 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +0300924 nasm_nonfatal("invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 }
926 i = stdscan(NULL, &tokval);
927 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000928
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700929 if (i == '[' || i == TOKEN_MASM_PTR || i == '&') {
930 /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700931 mref = true;
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700932 bracket += (i == '[');
933 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000935
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700936 mref_more:
937 if (mref) {
938 bool done = false;
939 bool nofw = false;
940
941 while (!done) {
942 switch (i) {
943 case TOKEN_SPECIAL:
944 case TOKEN_SIZE:
945 case TOKEN_PREFIX:
946 process_size_override(result, op);
947 break;
948
949 case '[':
950 bracket++;
951 break;
952
953 case ',':
954 tokval.t_type = TOKEN_NUM;
955 tokval.t_integer = 0;
956 stdscan_set(stdscan_get() - 1); /* rewind the comma */
957 done = nofw = true;
958 break;
959
960 case TOKEN_MASM_FLAT:
961 i = stdscan(NULL, &tokval);
962 if (i != ':') {
963 nasm_nonfatal("unknown use of FLAT in MASM emulation");
964 nofw = true;
965 }
966 done = true;
967 break;
968
969 default:
970 done = nofw = true;
971 break;
972 }
973
974 if (!nofw)
975 i = stdscan(NULL, &tokval);
976 }
977 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000978
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 value = evaluate(stdscan, NULL, &tokval,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800980 &op->opflags, critical, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 i = tokval.t_type;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700982 if (op->opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700983 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 }
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -0700985 if (!value) /* Error in evaluator */
986 goto fail;
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -0700987
988 if (i == '[' && !bracket) {
989 /* displacement[regs] syntax */
990 mref = true;
991 parse_mref(op, value); /* Process what we have so far */
992 goto mref_more;
993 }
994
995 if (i == ':' && (mref || !far_jmp_ok)) {
996 /* segment override? */
997 mref = true;
998
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 /*
1000 * Process the segment override.
1001 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +04001002 if (value[1].type != 0 ||
1003 value->value != 1 ||
1004 !IS_SREG(value->type))
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001005 nasm_nonfatal("invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001006 else if (result->prefixes[PPS_SEG])
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001007 nasm_nonfatal("instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00001008 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001009 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +04001010 if (IS_FSGS(value->type))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001011 op->eaflags |= EAF_FSGS;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001012 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001013
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001015 goto mref_more;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 }
Victor van den Elzen02846d32009-06-23 03:47:07 +02001017
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001018 mib = false;
1019 if (mref && bracket && i == ',') {
1020 /* [seg:base+offset,index*scale] syntax (mib) */
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001021 operand o2; /* Index operand */
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001022
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001023 if (parse_mref(op, value))
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001024 goto fail;
1025
1026 i = stdscan(NULL, &tokval); /* Eat comma */
1027 value = evaluate(stdscan, NULL, &tokval, &op->opflags,
H. Peter Anvin130736c2016-02-17 20:27:41 -08001028 critical, &hints);
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001029 i = tokval.t_type;
Cyrill Gorcunov5c0b0822014-11-22 18:20:29 +03001030 if (!value)
1031 goto fail;
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001032
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001033 init_operand(&o2);
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001034 if (parse_mref(&o2, value))
1035 goto fail;
1036
1037 if (o2.basereg != -1 && o2.indexreg == -1) {
1038 o2.indexreg = o2.basereg;
1039 o2.scale = 1;
1040 o2.basereg = -1;
1041 }
1042
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001043 if (op->indexreg != -1 || o2.basereg != -1 || o2.offset != 0 ||
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001044 o2.segment != NO_SEG || o2.wrt != NO_SEG) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001045 nasm_nonfatal("invalid mib expression");
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001046 goto fail;
1047 }
1048
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001049 op->indexreg = o2.indexreg;
1050 op->scale = o2.scale;
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001051
1052 if (op->basereg != -1) {
1053 op->hintbase = op->basereg;
1054 op->hinttype = EAH_MAKEBASE;
1055 } else if (op->indexreg != -1) {
1056 op->hintbase = op->indexreg;
1057 op->hinttype = EAH_NOTBASE;
1058 } else {
1059 op->hintbase = -1;
1060 op->hinttype = EAH_NOHINT;
1061 }
1062
1063 mib = true;
1064 }
1065
H. Peter Anvin552bc2c2009-06-23 11:34:42 -07001066 recover = false;
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001067 if (mref) {
1068 if (bracket == 1) {
1069 if (i == ']') {
1070 bracket--;
1071 i = stdscan(NULL, &tokval);
1072 } else {
1073 nasm_nonfatal("expecting ] at end of memory operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +02001074 recover = true;
1075 }
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001076 } else if (bracket == 0) {
1077 /* Do nothing */
1078 } else if (bracket > 0) {
1079 nasm_nonfatal("excess brackets in memory operand");
1080 recover = true;
1081 } else if (bracket < 0) {
1082 nasm_nonfatal("unmatched ] in memory operand");
1083 recover = true;
1084 }
1085
1086 if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
1087 /* parse opmask (and zeroing) after an operand */
1088 recover = parse_braces(&brace_flags);
1089 i = tokval.t_type;
1090 }
1091 if (!recover && i != 0 && i != ',') {
1092 nasm_nonfatal("comma, decorator or end of line expected, got %d", i);
1093 recover = true;
Victor van den Elzen02846d32009-06-23 03:47:07 +02001094 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 } else { /* immediate operand */
Jin Kyu Song72018a22013-08-05 20:46:18 -07001096 if (i != 0 && i != ',' && i != ':' &&
1097 i != TOKEN_DECORATOR && i != TOKEN_OPMASK) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001098 nasm_nonfatal("comma, colon, decorator or end of "
1099 "line expected after operand");
Victor van den Elzen02846d32009-06-23 03:47:07 +02001100 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 } else if (i == ':') {
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001102 op->type |= COLON;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001103 } else if (i == TOKEN_DECORATOR || i == TOKEN_OPMASK) {
1104 /* parse opmask (and zeroing) after an operand */
1105 recover = parse_braces(&brace_flags);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 }
1107 }
Victor van den Elzen02846d32009-06-23 03:47:07 +02001108 if (recover) {
1109 do { /* error recovery */
1110 i = stdscan(NULL, &tokval);
1111 } while (i != 0 && i != ',');
1112 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001113
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03001114 /*
1115 * now convert the exprs returned from evaluate()
1116 * into operand descriptions...
1117 */
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001118 op->decoflags |= brace_flags;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 if (mref) { /* it's a memory reference */
H. Peter Anvin9148fb52013-09-27 16:39:16 -07001121 /* A mib reference was fully parsed already */
1122 if (!mib) {
1123 if (parse_mref(op, value))
1124 goto fail;
1125 op->hintbase = hints.base;
1126 op->hinttype = hints.type;
1127 }
1128 mref_set_optype(op);
H. Peter Anvin (Intel)89817242019-08-14 15:24:56 -07001129 } else if ((op->type & FAR) && !far_jmp_ok) {
1130 nasm_nonfatal("invalid use of FAR operand specifier");
1131 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 if (is_just_unknown(value)) { /* it's immediate but unknown */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001134 op->type |= IMMEDIATE;
1135 op->opflags |= OPFLAG_UNKNOWN;
1136 op->offset = 0; /* don't care */
1137 op->segment = NO_SEG; /* don't care again */
1138 op->wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +01001139
Chang S. Baea5786342018-08-15 23:22:21 +03001140 if(optimizing.level >= 0 && !(op->type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001141 /* Be optimistic */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001142 op->type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001143 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +03001144 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 } else if (is_reloc(value)) { /* it's immediate */
H. Peter Anvin87646092017-02-28 17:44:24 -08001146 uint64_t n = reloc_value(value);
1147
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001148 op->type |= IMMEDIATE;
H. Peter Anvin87646092017-02-28 17:44:24 -08001149 op->offset = n;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001150 op->segment = reloc_seg(value);
1151 op->wrt = reloc_wrt(value);
H. Peter Anvin164d2462017-02-20 02:39:56 -08001152 op->opflags |= is_self_relative(value) ? OPFLAG_RELATIVE : 0;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001155 if (n == 1)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001156 op->type |= UNITY;
Chang S. Baea5786342018-08-15 23:22:21 +03001157 if (optimizing.level >= 0 && !(op->type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001158 if ((uint32_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001159 op->type |= SBYTEDWORD;
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001160 if ((uint16_t) (n + 128) <= 255)
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001161 op->type |= SBYTEWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001162 if (n <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001163 op->type |= UDWORD;
H. Peter Anvin87646092017-02-28 17:44:24 -08001164 if (n + UINT64_C(0x80000000) <= UINT64_C(0xFFFFFFFF))
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001165 op->type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 }
1167 }
H. Peter Anvin164d2462017-02-20 02:39:56 -08001168 } else if (value->type == EXPR_RDSAE) {
Jin Kyu Song72018a22013-08-05 20:46:18 -07001169 /*
1170 * it's not an operand but a rounding or SAE decorator.
1171 * put the decorator information in the (opflag_t) type field
1172 * of previous operand.
1173 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001174 opnum--; op--;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001175 switch (value->value) {
1176 case BRC_RN:
1177 case BRC_RU:
1178 case BRC_RD:
1179 case BRC_RZ:
1180 case BRC_SAE:
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001181 op->decoflags |= (value->value == BRC_SAE ? SAE : ER);
Jin Kyu Song72018a22013-08-05 20:46:18 -07001182 result->evex_rm = value->value;
1183 break;
1184 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001185 nasm_nonfatal("invalid decorator");
Jin Kyu Song72018a22013-08-05 20:46:18 -07001186 break;
1187 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04001189 opflags_t rs;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001190 uint64_t regset_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001191
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 if (value->type >= EXPR_SIMPLE || value->value != 1) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001193 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001194 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001196
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 /*
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001198 * We do not allow any kind of expression, except for
1199 * reg+value in which case it is a register set.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 */
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001201 for (i = 1; value[i].type; i++) {
1202 if (!value[i].value)
1203 continue;
1204
1205 switch (value[i].type) {
1206 case EXPR_SIMPLE:
1207 if (!regset_size) {
1208 regset_size = value[i].value + 1;
1209 break;
1210 }
1211 /* fallthrough */
1212 default:
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001213 nasm_nonfatal("invalid operand type");
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001214 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 }
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001216 }
1217
1218 if ((regset_size & (regset_size - 1)) ||
1219 regset_size >= (UINT64_C(1) << REGSET_BITS)) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001220 nasm_nonfatalf(ERR_PASS2, "invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001221 regset_size = 0;
1222 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00001223
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 /* clear overrides, except TO which applies to FPU regs */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001225 if (op->type & ~TO) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 /*
1227 * we want to produce a warning iff the specified size
1228 * is different from the register size
1229 */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001230 rs = op->type & SIZE_MASK;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001231 } else {
H. Peter Anvin68222142007-11-18 22:18:09 -08001232 rs = 0;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001233 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001235 /*
1236 * Make sure we're not out of nasm_reg_flags, still
1237 * probably this should be fixed when we're defining
1238 * the label.
1239 *
1240 * An easy trigger is
1241 *
1242 * e equ 0x80000000:0
1243 * pshufw word e-0
1244 *
1245 */
1246 if (value->type < EXPR_REG_START ||
1247 value->type > EXPR_REG_END) {
Cyrill Gorcunova14e6562018-12-01 20:20:50 +03001248 nasm_nonfatal("invalid operand type");
Cyrill Gorcunova28c40d2018-10-13 18:10:26 +03001249 goto fail;
1250 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001252 op->type &= TO;
1253 op->type |= REGISTER;
1254 op->type |= nasm_reg_flags[value->type];
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07001255 op->type |= (regset_size >> 1) << REGSET_SHIFT;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001256 op->decoflags |= brace_flags;
1257 op->basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258
H. Peter Anvin (Intel)b1e15f42019-08-09 02:44:46 -07001259 if (rs) {
1260 opflags_t opsize = nasm_reg_flags[value->type] & SIZE_MASK;
1261 if (!opsize) {
1262 op->type |= rs; /* For non-size-specific registers, permit size override */
1263 } else if (opsize != rs) {
1264 /*!
1265 *!regsize [on] register size specification ignored
1266 *!
1267 *! warns about a register with implicit size (such as \c{EAX}, which is always 32 bits)
1268 *! been given an explicit size specification which is inconsistent with the size
1269 *! of the named register, e.g. \c{WORD EAX}. \c{DWORD EAX} or \c{WORD AX} are
1270 *! permitted, and do not trigger this warning. Some registers which \e{do not} imply
1271 *! a specific size, such as \c{K0}, may need this specification unless the instruction
1272 *! itself implies the instruction size:
H. Peter Anvin7ad824b2019-10-03 22:18:35 -07001273 *!-
H. Peter Anvin (Intel)b1e15f42019-08-09 02:44:46 -07001274 *! \c KMOVW K0,[foo] ; Permitted, KMOVW implies 16 bits
1275 *! \c KMOV WORD K0,[foo] ; Permitted, WORD K0 specifies instruction size
1276 *! \c KMOV K0,WORD [foo] ; Permitted, WORD [foo] specifies instruction size
1277 *! \c KMOV K0,[foo] ; Not permitted, instruction size ambiguous
1278 */
1279 nasm_warn(WARN_REGSIZE, "invalid register size specification ignored");
1280 }
1281 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 }
1283 }
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001284
1285 /* remember the position of operand having broadcasting/ER mode */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001286 if (op->decoflags & (BRDCAST_MASK | ER | SAE))
1287 result->evex_brerop = opnum;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001288 }
1289
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001290 result->operands = opnum; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001291
Cyrill Gorcunovc2509502009-10-14 15:36:45 +04001292 /* clear remaining operands */
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001293 while (opnum < MAX_OPERANDS)
1294 result->oprs[opnum++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001295
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001296 return result;
H. Peter Anvindf0d1ba2013-09-26 17:23:08 -07001297
1298fail:
1299 result->opcode = I_none;
1300 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001301}
1302
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001303static int end_expression_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001304{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001305 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001306 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001307 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001308
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001309 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +03001311 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +04001312
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001313 return (i == ',' || i == ';' || i == ')' || !i);
1314}
1315
1316static void free_eops(extop *e)
1317{
1318 extop *next;
1319
1320 while (e) {
1321 next = e->next;
1322 switch (e->type) {
1323 case EOT_EXTOP:
1324 free_eops(e->val.subexpr);
1325 break;
1326
1327 case EOT_DB_STRING_FREE:
1328 nasm_free(e->val.string.data);
1329 break;
1330
1331 default:
1332 break;
1333 }
1334
1335 nasm_free(e);
1336 e = next;
1337 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001338}
1339
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001341{
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001342 free_eops(i->eops);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001343}