blob: afc422a9b2ba353fb68ac78dbf7d477305a7c15b [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvine20ca022013-07-19 17:06:08 -07003 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * parser.c source line parser for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000048#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000049#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000050#include "stdscan.h"
H. Peter Anvin00444ae2009-07-18 18:49:55 -070051#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052#include "parser.h"
53#include "float.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055
H. Peter Anvine2c80182005-01-15 22:15:51 +000056extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030057extern int32_t abs_seg; /* ABSOLUTE segment */
58extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000059
H. Peter Anvine2c80182005-01-15 22:15:51 +000060static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062static int i;
63static struct tokenval tokval;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030064static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000065
H. Peter Anvin605f5152009-07-18 18:31:41 -070066void parser_global_info(struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000067{
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 location = locp;
69}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040071static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070072{
73 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080074 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030075 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070076 case R_CS:
77 case R_DS:
78 case R_SS:
79 case R_ES:
80 case R_FS:
81 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030082 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070083 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080084 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070085 case P_REP:
86 case P_REPE:
87 case P_REPZ:
88 case P_REPNE:
89 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080090 case P_XACQUIRE:
91 case P_XRELEASE:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080092 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070093 case P_O16:
94 case P_O32:
95 case P_O64:
96 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030097 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070098 case P_A16:
99 case P_A32:
100 case P_A64:
101 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300102 return PPS_ASIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700103 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300104 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
105 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700106 }
107}
108
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300109static void process_size_override(insn *result, int operand)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700110{
111 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300112 switch ((int)tokval.t_integer) {
113 /* For TASM compatibility a size override inside the
114 * brackets changes the size of the operand, not the
115 * address type of the operand as it does in standard
116 * NASM syntax. Hence:
117 *
118 * mov eax,[DWORD val]
119 *
120 * is valid syntax in TASM compatibility mode. Note that
121 * you lose the ability to override the default address
122 * type for the instruction, but we never use anything
123 * but 32-bit flat model addressing in our code.
124 */
125 case S_BYTE:
126 result->oprs[operand].type |= BITS8;
127 break;
128 case S_WORD:
129 result->oprs[operand].type |= BITS16;
130 break;
131 case S_DWORD:
132 case S_LONG:
133 result->oprs[operand].type |= BITS32;
134 break;
135 case S_QWORD:
136 result->oprs[operand].type |= BITS64;
137 break;
138 case S_TWORD:
139 result->oprs[operand].type |= BITS80;
140 break;
141 case S_OWORD:
142 result->oprs[operand].type |= BITS128;
143 break;
144 default:
145 nasm_error(ERR_NONFATAL,
146 "invalid operand size specification");
147 break;
148 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700149 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300150 /* Standard NASM compatible syntax */
151 switch ((int)tokval.t_integer) {
152 case S_NOSPLIT:
153 result->oprs[operand].eaflags |= EAF_TIMESTWO;
154 break;
155 case S_REL:
156 result->oprs[operand].eaflags |= EAF_REL;
157 break;
158 case S_ABS:
159 result->oprs[operand].eaflags |= EAF_ABS;
160 break;
161 case S_BYTE:
162 result->oprs[operand].disp_size = 8;
163 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
164 break;
165 case P_A16:
166 case P_A32:
167 case P_A64:
168 if (result->prefixes[PPS_ASIZE] &&
169 result->prefixes[PPS_ASIZE] != tokval.t_integer)
170 nasm_error(ERR_NONFATAL,
171 "conflicting address size specifications");
172 else
173 result->prefixes[PPS_ASIZE] = tokval.t_integer;
174 break;
175 case S_WORD:
176 result->oprs[operand].disp_size = 16;
177 result->oprs[operand].eaflags |= EAF_WORDOFFS;
178 break;
179 case S_DWORD:
180 case S_LONG:
181 result->oprs[operand].disp_size = 32;
182 result->oprs[operand].eaflags |= EAF_WORDOFFS;
183 break;
184 case S_QWORD:
185 result->oprs[operand].disp_size = 64;
186 result->oprs[operand].eaflags |= EAF_WORDOFFS;
187 break;
188 default:
189 nasm_error(ERR_NONFATAL, "invalid size specification in"
190 " effective address");
191 break;
192 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700193 }
194}
195
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700196insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400198 bool insn_is_label = false;
199 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000200 int operand;
201 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800202 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700203 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000204
H. Peter Anvin9c987692007-11-04 21:09:32 -0800205restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400206 first = true;
207 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208
H. Peter Anvin76690a12002-04-30 20:52:49 +0000209 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300210 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000211 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000212
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400213 result->label = NULL; /* Assume no label */
214 result->eops = NULL; /* must do this, whatever happens */
215 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000216
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400217 /* Ignore blank lines */
218 if (i == TOKEN_EOS) {
219 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221 }
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400222
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400223 if (i != TOKEN_ID &&
224 i != TOKEN_INSN &&
225 i != TOKEN_PREFIX &&
226 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
227 nasm_error(ERR_NONFATAL,
228 "label or instruction expected at start of line");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400229 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000231 }
232
H. Peter Anvin9c987692007-11-04 21:09:32 -0800233 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
234 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300235 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236 result->label = tokval.t_charptr;
237 i = stdscan(NULL, &tokval);
238 if (i == ':') { /* skip over the optional colon */
239 i = stdscan(NULL, &tokval);
240 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700241 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000242 "label alone on a line without a colon might be in error");
243 }
244 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
245 /*
246 * FIXME: location->segment could be NO_SEG, in which case
247 * it is possible we should be passing 'abs_seg'. Look into this.
248 * Work out whether that is *really* what we should be doing.
249 * Generally fix things. I think this is right as it is, but
250 * am still not certain.
251 */
252 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700253 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000254 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000255 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000256
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400257 /* Just a label here */
258 if (i == TOKEN_EOS) {
259 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000260 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000261 }
262
Cyrill Gorcunov836492f2013-07-16 01:33:09 +0400263 nasm_build_assert(P_none != 0);
264 memset(result->prefixes, P_none, sizeof(result->prefixes));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000265 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000266
267 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400268 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300269 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800270
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271 /*
272 * Handle special case: the TIMES prefix.
273 */
274 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
275 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000276
H. Peter Anvine2c80182005-01-15 22:15:51 +0000277 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400278 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000279 i = tokval.t_type;
280 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400281 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000282 return result; /* ignore this instruction */
283 }
284 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700285 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000286 "non-constant argument supplied to TIMES");
287 result->times = 1L;
288 } else {
289 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700290 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200291 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000292 value->value);
293 result->times = 0;
294 }
295 }
296 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300297 int slot = prefix_slot(tokval.t_integer);
298 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700299 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100300 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300301 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700302 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300303 nasm_error(ERR_NONFATAL,
304 "instruction has conflicting prefixes");
305 }
306 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307 i = stdscan(NULL, &tokval);
308 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000309 }
310
311 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300312 int j;
313 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700314
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400315 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300316 if ((pfx = result->prefixes[j]) != P_none)
317 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400318 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700319
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700320 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000321 /*
322 * Instruction prefixes are present, but no actual
323 * instruction. This is allowed: at this point we
324 * invent a notional instruction of RESB 0.
325 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400326 result->opcode = I_RESB;
327 result->operands = 1;
328 result->oprs[0].type = IMMEDIATE;
329 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
331 return result;
332 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700333 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400334 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000335 return result;
336 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000337 }
338
339 result->opcode = tokval.t_integer;
340 result->condition = tokval.t_inttwo;
341
342 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700343 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000344 * evaluated operands, since the correct values _must_ be known
345 * on the first pass. Hence, even in pass one, we set the
346 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700347 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000348 */
Charles Crayne2581c862008-09-10 19:21:52 -0700349 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700350 critical = (pass0 < 2 ? 1 : 2);
351
H. Peter Anvine2c80182005-01-15 22:15:51 +0000352 } else
353 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000354
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700355 if (result->opcode == I_DB || result->opcode == I_DW ||
356 result->opcode == I_DD || result->opcode == I_DQ ||
357 result->opcode == I_DT || result->opcode == I_DO ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300358 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 extop *eop, **tail = &result->eops, **fixptr;
360 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300361 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000362
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700363 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000364
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700366 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367 */
368 while (1) {
369 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400370 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300372 else if (first && i == ':') {
373 insn_is_label = true;
374 goto restart_parse;
375 }
376 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377 fixptr = tail;
378 eop = *tail = nasm_malloc(sizeof(extop));
379 tail = &eop->next;
380 eop->next = NULL;
381 eop->type = EOT_NOTHING;
382 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300383 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000384
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300385 /*
386 * is_comma_next() here is to distinguish this from
387 * a string used as part of an expression...
388 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700389 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400390 eop->type = EOT_DB_STRING;
391 eop->stringval = tokval.t_charptr;
392 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300394 } else if (i == TOKEN_STRFUNC) {
395 bool parens = false;
396 const char *funcname = tokval.t_charptr;
397 enum strfunc func = tokval.t_integer;
398 i = stdscan(NULL, &tokval);
399 if (i == '(') {
400 parens = true;
401 i = stdscan(NULL, &tokval);
402 }
403 if (i != TOKEN_STR) {
404 nasm_error(ERR_NONFATAL,
405 "%s must be followed by a string constant",
406 funcname);
407 eop->type = EOT_NOTHING;
408 } else {
409 eop->type = EOT_DB_STRING_FREE;
410 eop->stringlen =
411 string_transform(tokval.t_charptr, tokval.t_inttwo,
412 &eop->stringval, func);
413 if (eop->stringlen == (size_t)-1) {
414 nasm_error(ERR_NONFATAL, "invalid string for transform");
415 eop->type = EOT_NOTHING;
416 }
417 }
418 if (parens && i && i != ')') {
419 i = stdscan(NULL, &tokval);
420 if (i != ')') {
421 nasm_error(ERR_NONFATAL, "unterminated %s function",
422 funcname);
423 }
424 }
425 if (i && i != ',')
426 i = stdscan(NULL, &tokval);
427 } else if (i == '-' || i == '+') {
428 char *save = stdscan_get();
429 int token = i;
430 sign = (i == '-') ? -1 : 1;
431 i = stdscan(NULL, &tokval);
432 if (i != TOKEN_FLOAT) {
433 stdscan_set(save);
434 i = tokval.t_type = token;
435 goto is_expression;
436 } else {
437 goto is_float;
438 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700439 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300440is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300441 eop->type = EOT_DB_STRING;
442 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300443
444 eop->stringlen = idata_bytes(result->opcode);
445 if (eop->stringlen > 16) {
446 nasm_error(ERR_NONFATAL, "floating-point constant"
447 " encountered in DY instruction");
448 eop->stringlen = 0;
449 } else if (eop->stringlen < 1) {
450 nasm_error(ERR_NONFATAL, "floating-point constant"
451 " encountered in unknown instruction");
452 /*
453 * fix suggested by Pedro Gimeno... original line was:
454 * eop->type = EOT_NOTHING;
455 */
456 eop->stringlen = 0;
457 }
458
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300459 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
460 tail = &eop->next;
461 *fixptr = eop;
462 eop->stringval = (char *)eop + sizeof(extop);
463 if (!eop->stringlen ||
464 !float_const(tokval.t_charptr, sign,
465 (uint8_t *)eop->stringval,
466 eop->stringlen, nasm_error))
467 eop->type = EOT_NOTHING;
468 i = stdscan(NULL, &tokval); /* eat the comma */
469 } else {
470 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700472
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300473is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700475 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 i = tokval.t_type;
477 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400478 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 return result; /* ignore this instruction */
480 }
481 if (is_unknown(value)) {
482 eop->type = EOT_DB_NUMBER;
483 eop->offset = 0; /* doesn't matter what we put */
484 eop->segment = eop->wrt = NO_SEG; /* likewise */
485 } else if (is_reloc(value)) {
486 eop->type = EOT_DB_NUMBER;
487 eop->offset = reloc_value(value);
488 eop->segment = reloc_seg(value);
489 eop->wrt = reloc_wrt(value);
490 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700491 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 "operand %d: expression is not simple"
493 " or relocatable", oper_num);
494 }
495 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000496
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 /*
498 * We're about to call stdscan(), which will eat the
499 * comma that we're currently sitting on between
500 * arguments. However, we'd better check first that it
501 * _is_ a comma.
502 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400503 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 break;
505 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700506 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400507 oper_num);
508 result->opcode = I_none;/* unrecoverable parse error: */
509 return result; /* ignore this instruction */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 }
511 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000512
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 if (result->opcode == I_INCBIN) {
514 /*
515 * Correct syntax for INCBIN is that there should be
516 * one string operand, followed by one or two numeric
517 * operands.
518 */
519 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700520 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 else if (result->eops->next &&
522 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200523 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400524 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 else if (result->eops->next && result->eops->next->next &&
526 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200527 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400528 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 else if (result->eops->next && result->eops->next->next &&
530 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700531 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400532 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000533 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 return result;
535 /*
536 * If we reach here, one of the above errors happened.
537 * Throw the instruction away.
538 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400539 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 return result;
541 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700542 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 "no operand for data declaration");
544 else
545 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000546
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000548 }
549
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400550 /*
551 * Now we begin to parse the operands. There may be up to four
552 * of these, separated by commas, and terminated by a zero token.
553 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000554
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700555 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300556 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 int mref; /* is this going to be a memory ref? */
558 int bracket; /* is it a [] mref, or a & mref? */
559 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000560
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700561 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400562 result->oprs[operand].eaflags = 0; /* and this */
563 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000564
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400566 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300568 else if (first && i == ':') {
569 insn_is_label = true;
570 goto restart_parse;
571 }
572 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 result->oprs[operand].type = 0; /* so far, no override */
574 while (i == TOKEN_SPECIAL) { /* size specifiers */
575 switch ((int)tokval.t_integer) {
576 case S_BYTE:
577 if (!setsize) /* we want to use only the first */
578 result->oprs[operand].type |= BITS8;
579 setsize = 1;
580 break;
581 case S_WORD:
582 if (!setsize)
583 result->oprs[operand].type |= BITS16;
584 setsize = 1;
585 break;
586 case S_DWORD:
587 case S_LONG:
588 if (!setsize)
589 result->oprs[operand].type |= BITS32;
590 setsize = 1;
591 break;
592 case S_QWORD:
593 if (!setsize)
594 result->oprs[operand].type |= BITS64;
595 setsize = 1;
596 break;
597 case S_TWORD:
598 if (!setsize)
599 result->oprs[operand].type |= BITS80;
600 setsize = 1;
601 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700602 case S_OWORD:
603 if (!setsize)
604 result->oprs[operand].type |= BITS128;
605 setsize = 1;
606 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700607 case S_YWORD:
608 if (!setsize)
609 result->oprs[operand].type |= BITS256;
610 setsize = 1;
611 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 case S_TO:
613 result->oprs[operand].type |= TO;
614 break;
615 case S_STRICT:
616 result->oprs[operand].type |= STRICT;
617 break;
618 case S_FAR:
619 result->oprs[operand].type |= FAR;
620 break;
621 case S_NEAR:
622 result->oprs[operand].type |= NEAR;
623 break;
624 case S_SHORT:
625 result->oprs[operand].type |= SHORT;
626 break;
627 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700628 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 }
630 i = stdscan(NULL, &tokval);
631 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000632
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700634 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700636 i = stdscan(NULL, &tokval); /* then skip the colon */
637 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300638 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700639 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 }
641 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700642 mref = false;
643 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000645
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 if ((result->oprs[operand].type & FAR) && !mref &&
647 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700648 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000650
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 value = evaluate(stdscan, NULL, &tokval,
652 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700653 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 i = tokval.t_type;
655 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700656 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700658 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400659 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 return result; /* ignore this instruction */
661 }
662 if (i == ':' && mref) { /* it was seg:offset */
663 /*
664 * Process the segment override.
665 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400666 if (value[1].type != 0 ||
667 value->value != 1 ||
668 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700669 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700670 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700671 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700672 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000673 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300674 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400675 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300676 result->oprs[operand].eaflags |= EAF_FSGS;
677 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000678
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700680 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300681 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 i = stdscan(NULL, &tokval);
683 }
684 value = evaluate(stdscan, NULL, &tokval,
685 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700686 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 i = tokval.t_type;
688 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700689 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 }
691 /* and get the offset */
692 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400693 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694 return result; /* ignore this instruction */
695 }
696 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200697
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700698 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 if (mref && bracket) { /* find ] at the end */
700 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700701 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200702 recover = true;
703 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 i = stdscan(NULL, &tokval);
Victor van den Elzen02846d32009-06-23 03:47:07 +0200705 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700706 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200707 recover = true;
708 }
709 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000710 } else { /* immediate operand */
711 if (i != 0 && i != ',' && i != ':') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700712 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200713 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 } else if (i == ':') {
715 result->oprs[operand].type |= COLON;
716 }
717 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200718 if (recover) {
719 do { /* error recovery */
720 i = stdscan(NULL, &tokval);
721 } while (i != 0 && i != ',');
722 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000723
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300724 /*
725 * now convert the exprs returned from evaluate()
726 * into operand descriptions...
727 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 if (mref) { /* it's a memory reference */
730 expr *e = value;
731 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300732 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000733
H. Peter Anvine2c80182005-01-15 22:15:51 +0000734 b = i = -1, o = s = 0;
735 result->oprs[operand].hintbase = hints.base;
736 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000737
H. Peter Anvine2c80182005-01-15 22:15:51 +0000738 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700739 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
740
741 if (is_gpr && e->value == 1)
742 b = e->type; /* It can be basereg */
743 else /* No, it has to be indexreg */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 i = e->type, s = e->value;
745 e++;
746 }
747 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
H. Peter Anvine20ca022013-07-19 17:06:08 -0700748 bool is_gpr = is_class(REG_GPR,nasm_reg_flags[e->type]);
749
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 if (b != -1) /* If the first was the base, ... */
751 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000752
H. Peter Anvine20ca022013-07-19 17:06:08 -0700753 else if (!is_gpr || e->value != 1) {
754 /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700755 nasm_error(ERR_NONFATAL,
H. Peter Anvine20ca022013-07-19 17:06:08 -0700756 "invalid effective address: two index registers");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400757 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 return result;
759 } else
760 b = e->type;
761 e++;
762 }
763 if (e->type != 0) { /* is there an offset? */
764 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700765 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400767 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 return result;
769 } else {
770 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100771 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 o = 0; /* doesn't matter what */
773 result->oprs[operand].wrt = NO_SEG; /* nor this */
774 result->oprs[operand].segment = NO_SEG; /* or this */
775 while (e->type)
776 e++; /* go to the end of the line */
777 } else {
778 if (e->type == EXPR_SIMPLE) {
779 o = e->value;
780 e++;
781 }
782 if (e->type == EXPR_WRT) {
783 result->oprs[operand].wrt = e->value;
784 e++;
785 } else
786 result->oprs[operand].wrt = NO_SEG;
787 /*
788 * Look for a segment base type.
789 */
790 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700791 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 "beroset-p-630-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400793 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 return result;
795 }
796 while (e->type && e->value == 0)
797 e++;
798 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700799 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400801 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 return result;
803 }
804 if (e->type) {
805 result->oprs[operand].segment =
806 e->type - EXPR_SEGBASE;
807 e++;
808 } else
809 result->oprs[operand].segment = NO_SEG;
810 while (e->type && e->value == 0)
811 e++;
812 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700813 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400815 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 return result;
817 }
818 }
819 }
820 } else {
821 o = 0;
822 result->oprs[operand].wrt = NO_SEG;
823 result->oprs[operand].segment = NO_SEG;
824 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000825
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700827 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400829 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 return result;
831 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000832
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300833 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700834 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000835
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300836 if (b == -1 && (i == -1 || s == 0)) {
837 int is_rel = globalbits == 64 &&
838 !(result->oprs[operand].eaflags & EAF_ABS) &&
839 ((globalrel &&
840 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
841 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000842
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300843 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
844 }
H. Peter Anvine20ca022013-07-19 17:06:08 -0700845
846 if (i != -1) {
847 opflags_t iclass = nasm_reg_flags[i];
848
849 if (is_class(XMMREG,iclass))
850 result->oprs[operand].type |= XMEM;
851 else if (is_class(YMMREG,iclass))
852 result->oprs[operand].type |= YMEM;
853 }
854
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 result->oprs[operand].basereg = b;
856 result->oprs[operand].indexreg = i;
857 result->oprs[operand].scale = s;
858 result->oprs[operand].offset = o;
859 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400861 result->oprs[operand].type |= IMMEDIATE;
862 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
863 result->oprs[operand].offset = 0; /* don't care */
864 result->oprs[operand].segment = NO_SEG; /* don't care again */
865 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100866
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400867 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300868 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700869 result->oprs[operand].type |=
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400870 UNITY | SBYTEWORD | SBYTEDWORD | UDWORD | SDWORD;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300871 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400873 result->oprs[operand].type |= IMMEDIATE;
874 result->oprs[operand].offset = reloc_value(value);
875 result->oprs[operand].segment = reloc_seg(value);
876 result->oprs[operand].wrt = reloc_wrt(value);
877
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 if (is_simple(value)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400879 uint64_t n = reloc_value(value);
880 if (n == 1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 result->oprs[operand].type |= UNITY;
882 if (optimizing >= 0 &&
883 !(result->oprs[operand].type & STRICT)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400884 if ((uint32_t) (n + 128) <= 255)
885 result->oprs[operand].type |= SBYTEDWORD;
886 if ((uint16_t) (n + 128) <= 255)
887 result->oprs[operand].type |= SBYTEWORD;
888 if (n <= 0xFFFFFFFF)
889 result->oprs[operand].type |= UDWORD;
890 if (n + 0x80000000 <= 0xFFFFFFFF)
891 result->oprs[operand].type |= SDWORD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 }
893 }
894 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +0400895 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000896
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700898 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400899 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 return result;
901 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000902
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 /*
904 * check that its only 1 register, not an expression...
905 */
906 for (i = 1; value[i].type; i++)
907 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700908 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400909 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 return result;
911 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000912
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 /* clear overrides, except TO which applies to FPU regs */
914 if (result->oprs[operand].type & ~TO) {
915 /*
916 * we want to produce a warning iff the specified size
917 * is different from the register size
918 */
H. Peter Anvin68222142007-11-18 22:18:09 -0800919 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 } else
H. Peter Anvin68222142007-11-18 22:18:09 -0800921 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400923 result->oprs[operand].type &= TO;
924 result->oprs[operand].type |= REGISTER;
925 result->oprs[operand].type |= nasm_reg_flags[value->type];
926 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927
H. Peter Anvin68222142007-11-18 22:18:09 -0800928 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700929 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 "register size specification ignored");
931 }
932 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000933 }
934
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000936
Cyrill Gorcunovc2509502009-10-14 15:36:45 +0400937 /* clear remaining operands */
938 while (operand < MAX_OPERANDS)
939 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000940
941 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -0700942 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000943 */
944 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 case I_RESW:
946 result->opcode = I_RESB;
947 result->oprs[0].offset *= 2;
948 break;
949 case I_RESD:
950 result->opcode = I_RESB;
951 result->oprs[0].offset *= 4;
952 break;
953 case I_RESQ:
954 result->opcode = I_RESB;
955 result->oprs[0].offset *= 8;
956 break;
957 case I_REST:
958 result->opcode = I_RESB;
959 result->oprs[0].offset *= 10;
960 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700961 case I_RESO:
962 result->opcode = I_RESB;
963 result->oprs[0].offset *= 16;
964 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700965 case I_RESY:
966 result->opcode = I_RESB;
967 result->oprs[0].offset *= 32;
968 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700969 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300970 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000971 }
972
973 return result;
974}
975
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000977{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400978 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000979 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000980 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000981
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300982 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300984 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400985
H. Peter Anvin76690a12002-04-30 20:52:49 +0000986 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000987}
988
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000990{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000991 extop *e;
992
H. Peter Anvin2aa77392008-06-15 17:39:45 -0700993 while ((e = i->eops)) {
994 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300995 if (e->type == EOT_DB_STRING_FREE)
996 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000998 }
999}