blob: 37d71389344a80735c990b9b831d0c967ef48b14 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07003 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * 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:
84 case P_REP:
85 case P_REPE:
86 case P_REPZ:
87 case P_REPNE:
88 case P_REPNZ:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030089 return PPS_LREP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070090 case P_O16:
91 case P_O32:
92 case P_O64:
93 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030094 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070095 case P_A16:
96 case P_A32:
97 case P_A64:
98 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030099 return PPS_ASIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700100 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300101 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
102 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700103 }
104}
105
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300106static void process_size_override(insn *result, int operand)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700107{
108 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300109 switch ((int)tokval.t_integer) {
110 /* For TASM compatibility a size override inside the
111 * brackets changes the size of the operand, not the
112 * address type of the operand as it does in standard
113 * NASM syntax. Hence:
114 *
115 * mov eax,[DWORD val]
116 *
117 * is valid syntax in TASM compatibility mode. Note that
118 * you lose the ability to override the default address
119 * type for the instruction, but we never use anything
120 * but 32-bit flat model addressing in our code.
121 */
122 case S_BYTE:
123 result->oprs[operand].type |= BITS8;
124 break;
125 case S_WORD:
126 result->oprs[operand].type |= BITS16;
127 break;
128 case S_DWORD:
129 case S_LONG:
130 result->oprs[operand].type |= BITS32;
131 break;
132 case S_QWORD:
133 result->oprs[operand].type |= BITS64;
134 break;
135 case S_TWORD:
136 result->oprs[operand].type |= BITS80;
137 break;
138 case S_OWORD:
139 result->oprs[operand].type |= BITS128;
140 break;
141 default:
142 nasm_error(ERR_NONFATAL,
143 "invalid operand size specification");
144 break;
145 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700146 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300147 /* Standard NASM compatible syntax */
148 switch ((int)tokval.t_integer) {
149 case S_NOSPLIT:
150 result->oprs[operand].eaflags |= EAF_TIMESTWO;
151 break;
152 case S_REL:
153 result->oprs[operand].eaflags |= EAF_REL;
154 break;
155 case S_ABS:
156 result->oprs[operand].eaflags |= EAF_ABS;
157 break;
158 case S_BYTE:
159 result->oprs[operand].disp_size = 8;
160 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
161 break;
162 case P_A16:
163 case P_A32:
164 case P_A64:
165 if (result->prefixes[PPS_ASIZE] &&
166 result->prefixes[PPS_ASIZE] != tokval.t_integer)
167 nasm_error(ERR_NONFATAL,
168 "conflicting address size specifications");
169 else
170 result->prefixes[PPS_ASIZE] = tokval.t_integer;
171 break;
172 case S_WORD:
173 result->oprs[operand].disp_size = 16;
174 result->oprs[operand].eaflags |= EAF_WORDOFFS;
175 break;
176 case S_DWORD:
177 case S_LONG:
178 result->oprs[operand].disp_size = 32;
179 result->oprs[operand].eaflags |= EAF_WORDOFFS;
180 break;
181 case S_QWORD:
182 result->oprs[operand].disp_size = 64;
183 result->oprs[operand].eaflags |= EAF_WORDOFFS;
184 break;
185 default:
186 nasm_error(ERR_NONFATAL, "invalid size specification in"
187 " effective address");
188 break;
189 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700190 }
191}
192
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700193insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400195 bool insn_is_label = false;
196 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000197 int operand;
198 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800199 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700200 bool recover;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400201 int j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000202
H. Peter Anvin9c987692007-11-04 21:09:32 -0800203restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400204 first = true;
205 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206
H. Peter Anvin76690a12002-04-30 20:52:49 +0000207 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300208 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000209 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000210
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400211 result->label = NULL; /* Assume no label */
212 result->eops = NULL; /* must do this, whatever happens */
213 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400215 /* Ignore blank lines */
216 if (i == TOKEN_EOS) {
217 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000218 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000219 }
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400220
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400221 if (i != TOKEN_ID &&
222 i != TOKEN_INSN &&
223 i != TOKEN_PREFIX &&
224 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
225 nasm_error(ERR_NONFATAL,
226 "label or instruction expected at start of line");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400227 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000228 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000229 }
230
H. Peter Anvin9c987692007-11-04 21:09:32 -0800231 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
232 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300233 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000234 result->label = tokval.t_charptr;
235 i = stdscan(NULL, &tokval);
236 if (i == ':') { /* skip over the optional colon */
237 i = stdscan(NULL, &tokval);
238 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700239 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000240 "label alone on a line without a colon might be in error");
241 }
242 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
243 /*
244 * FIXME: location->segment could be NO_SEG, in which case
245 * it is possible we should be passing 'abs_seg'. Look into this.
246 * Work out whether that is *really* what we should be doing.
247 * Generally fix things. I think this is right as it is, but
248 * am still not certain.
249 */
250 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700251 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000252 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000254
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400255 /* Just a label here */
256 if (i == TOKEN_EOS) {
257 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000258 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000259 }
260
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700261 for (j = 0; j < MAXPREFIX; j++)
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300262 result->prefixes[j] = P_none;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000263 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000264
265 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400266 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300267 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800268
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269 /*
270 * Handle special case: the TIMES prefix.
271 */
272 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
273 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000274
H. Peter Anvine2c80182005-01-15 22:15:51 +0000275 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400276 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000277 i = tokval.t_type;
278 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400279 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000280 return result; /* ignore this instruction */
281 }
282 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700283 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000284 "non-constant argument supplied to TIMES");
285 result->times = 1L;
286 } else {
287 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700288 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200289 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000290 value->value);
291 result->times = 0;
292 }
293 }
294 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300295 int slot = prefix_slot(tokval.t_integer);
296 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700297 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100298 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300299 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700300 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300301 nasm_error(ERR_NONFATAL,
302 "instruction has conflicting prefixes");
303 }
304 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305 i = stdscan(NULL, &tokval);
306 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000307 }
308
309 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300310 int j;
311 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700312
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400313 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300314 if ((pfx = result->prefixes[j]) != P_none)
315 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400316 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700317
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700318 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000319 /*
320 * Instruction prefixes are present, but no actual
321 * instruction. This is allowed: at this point we
322 * invent a notional instruction of RESB 0.
323 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400324 result->opcode = I_RESB;
325 result->operands = 1;
326 result->oprs[0].type = IMMEDIATE;
327 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
329 return result;
330 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700331 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400332 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333 return result;
334 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000335 }
336
337 result->opcode = tokval.t_integer;
338 result->condition = tokval.t_inttwo;
339
340 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700341 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000342 * evaluated operands, since the correct values _must_ be known
343 * on the first pass. Hence, even in pass one, we set the
344 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700345 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346 */
Charles Crayne2581c862008-09-10 19:21:52 -0700347 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700348 critical = (pass0 < 2 ? 1 : 2);
349
H. Peter Anvine2c80182005-01-15 22:15:51 +0000350 } else
351 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000352
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700353 if (result->opcode == I_DB || result->opcode == I_DW ||
354 result->opcode == I_DD || result->opcode == I_DQ ||
355 result->opcode == I_DT || result->opcode == I_DO ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300356 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000357 extop *eop, **tail = &result->eops, **fixptr;
358 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300359 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000360
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700361 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000362
H. Peter Anvine2c80182005-01-15 22:15:51 +0000363 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700364 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365 */
366 while (1) {
367 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400368 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000369 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300370 else if (first && i == ':') {
371 insn_is_label = true;
372 goto restart_parse;
373 }
374 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 fixptr = tail;
376 eop = *tail = nasm_malloc(sizeof(extop));
377 tail = &eop->next;
378 eop->next = NULL;
379 eop->type = EOT_NOTHING;
380 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300381 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000382
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300383 /*
384 * is_comma_next() here is to distinguish this from
385 * a string used as part of an expression...
386 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700387 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400388 eop->type = EOT_DB_STRING;
389 eop->stringval = tokval.t_charptr;
390 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300392 } else if (i == TOKEN_STRFUNC) {
393 bool parens = false;
394 const char *funcname = tokval.t_charptr;
395 enum strfunc func = tokval.t_integer;
396 i = stdscan(NULL, &tokval);
397 if (i == '(') {
398 parens = true;
399 i = stdscan(NULL, &tokval);
400 }
401 if (i != TOKEN_STR) {
402 nasm_error(ERR_NONFATAL,
403 "%s must be followed by a string constant",
404 funcname);
405 eop->type = EOT_NOTHING;
406 } else {
407 eop->type = EOT_DB_STRING_FREE;
408 eop->stringlen =
409 string_transform(tokval.t_charptr, tokval.t_inttwo,
410 &eop->stringval, func);
411 if (eop->stringlen == (size_t)-1) {
412 nasm_error(ERR_NONFATAL, "invalid string for transform");
413 eop->type = EOT_NOTHING;
414 }
415 }
416 if (parens && i && i != ')') {
417 i = stdscan(NULL, &tokval);
418 if (i != ')') {
419 nasm_error(ERR_NONFATAL, "unterminated %s function",
420 funcname);
421 }
422 }
423 if (i && i != ',')
424 i = stdscan(NULL, &tokval);
425 } else if (i == '-' || i == '+') {
426 char *save = stdscan_get();
427 int token = i;
428 sign = (i == '-') ? -1 : 1;
429 i = stdscan(NULL, &tokval);
430 if (i != TOKEN_FLOAT) {
431 stdscan_set(save);
432 i = tokval.t_type = token;
433 goto is_expression;
434 } else {
435 goto is_float;
436 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700437 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300438is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300439 eop->type = EOT_DB_STRING;
440 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300441
442 eop->stringlen = idata_bytes(result->opcode);
443 if (eop->stringlen > 16) {
444 nasm_error(ERR_NONFATAL, "floating-point constant"
445 " encountered in DY instruction");
446 eop->stringlen = 0;
447 } else if (eop->stringlen < 1) {
448 nasm_error(ERR_NONFATAL, "floating-point constant"
449 " encountered in unknown instruction");
450 /*
451 * fix suggested by Pedro Gimeno... original line was:
452 * eop->type = EOT_NOTHING;
453 */
454 eop->stringlen = 0;
455 }
456
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300457 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
458 tail = &eop->next;
459 *fixptr = eop;
460 eop->stringval = (char *)eop + sizeof(extop);
461 if (!eop->stringlen ||
462 !float_const(tokval.t_charptr, sign,
463 (uint8_t *)eop->stringval,
464 eop->stringlen, nasm_error))
465 eop->type = EOT_NOTHING;
466 i = stdscan(NULL, &tokval); /* eat the comma */
467 } else {
468 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700470
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300471is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700473 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 i = tokval.t_type;
475 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400476 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 return result; /* ignore this instruction */
478 }
479 if (is_unknown(value)) {
480 eop->type = EOT_DB_NUMBER;
481 eop->offset = 0; /* doesn't matter what we put */
482 eop->segment = eop->wrt = NO_SEG; /* likewise */
483 } else if (is_reloc(value)) {
484 eop->type = EOT_DB_NUMBER;
485 eop->offset = reloc_value(value);
486 eop->segment = reloc_seg(value);
487 eop->wrt = reloc_wrt(value);
488 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700489 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 "operand %d: expression is not simple"
491 " or relocatable", oper_num);
492 }
493 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000494
H. Peter Anvine2c80182005-01-15 22:15:51 +0000495 /*
496 * We're about to call stdscan(), which will eat the
497 * comma that we're currently sitting on between
498 * arguments. However, we'd better check first that it
499 * _is_ a comma.
500 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400501 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 break;
503 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700504 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400505 oper_num);
506 result->opcode = I_none;/* unrecoverable parse error: */
507 return result; /* ignore this instruction */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 }
509 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000510
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 if (result->opcode == I_INCBIN) {
512 /*
513 * Correct syntax for INCBIN is that there should be
514 * one string operand, followed by one or two numeric
515 * operands.
516 */
517 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700518 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 else if (result->eops->next &&
520 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200521 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400522 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 else if (result->eops->next && result->eops->next->next &&
524 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200525 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400526 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527 else if (result->eops->next && result->eops->next->next &&
528 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700529 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400530 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000531 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 return result;
533 /*
534 * If we reach here, one of the above errors happened.
535 * Throw the instruction away.
536 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400537 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 return result;
539 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700540 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 "no operand for data declaration");
542 else
543 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000544
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000546 }
547
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400548 /*
549 * Now we begin to parse the operands. There may be up to four
550 * of these, separated by commas, and terminated by a zero token.
551 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000552
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700553 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300554 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 int mref; /* is this going to be a memory ref? */
556 int bracket; /* is it a [] mref, or a & mref? */
557 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000558
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700559 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400560 result->oprs[operand].eaflags = 0; /* and this */
561 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000562
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400564 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300566 else if (first && i == ':') {
567 insn_is_label = true;
568 goto restart_parse;
569 }
570 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 result->oprs[operand].type = 0; /* so far, no override */
572 while (i == TOKEN_SPECIAL) { /* size specifiers */
573 switch ((int)tokval.t_integer) {
574 case S_BYTE:
575 if (!setsize) /* we want to use only the first */
576 result->oprs[operand].type |= BITS8;
577 setsize = 1;
578 break;
579 case S_WORD:
580 if (!setsize)
581 result->oprs[operand].type |= BITS16;
582 setsize = 1;
583 break;
584 case S_DWORD:
585 case S_LONG:
586 if (!setsize)
587 result->oprs[operand].type |= BITS32;
588 setsize = 1;
589 break;
590 case S_QWORD:
591 if (!setsize)
592 result->oprs[operand].type |= BITS64;
593 setsize = 1;
594 break;
595 case S_TWORD:
596 if (!setsize)
597 result->oprs[operand].type |= BITS80;
598 setsize = 1;
599 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700600 case S_OWORD:
601 if (!setsize)
602 result->oprs[operand].type |= BITS128;
603 setsize = 1;
604 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700605 case S_YWORD:
606 if (!setsize)
607 result->oprs[operand].type |= BITS256;
608 setsize = 1;
609 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 case S_TO:
611 result->oprs[operand].type |= TO;
612 break;
613 case S_STRICT:
614 result->oprs[operand].type |= STRICT;
615 break;
616 case S_FAR:
617 result->oprs[operand].type |= FAR;
618 break;
619 case S_NEAR:
620 result->oprs[operand].type |= NEAR;
621 break;
622 case S_SHORT:
623 result->oprs[operand].type |= SHORT;
624 break;
625 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700626 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 }
628 i = stdscan(NULL, &tokval);
629 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000630
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700632 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700634 i = stdscan(NULL, &tokval); /* then skip the colon */
635 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300636 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700637 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 }
639 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700640 mref = false;
641 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000643
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 if ((result->oprs[operand].type & FAR) && !mref &&
645 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700646 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000648
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 value = evaluate(stdscan, NULL, &tokval,
650 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700651 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 i = tokval.t_type;
653 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700654 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700656 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400657 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 return result; /* ignore this instruction */
659 }
660 if (i == ':' && mref) { /* it was seg:offset */
661 /*
662 * Process the segment override.
663 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400664 if (value[1].type != 0 ||
665 value->value != 1 ||
666 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700667 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700668 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700669 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700670 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000671 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300672 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400673 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300674 result->oprs[operand].eaflags |= EAF_FSGS;
675 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000676
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700678 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300679 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 i = stdscan(NULL, &tokval);
681 }
682 value = evaluate(stdscan, NULL, &tokval,
683 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700684 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 i = tokval.t_type;
686 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700687 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 }
689 /* and get the offset */
690 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400691 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000692 return result; /* ignore this instruction */
693 }
694 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200695
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700696 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 if (mref && bracket) { /* find ] at the end */
698 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700699 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200700 recover = true;
701 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000702 i = stdscan(NULL, &tokval);
Victor van den Elzen02846d32009-06-23 03:47:07 +0200703 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700704 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200705 recover = true;
706 }
707 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 } else { /* immediate operand */
709 if (i != 0 && i != ',' && i != ':') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700710 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200711 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 } else if (i == ':') {
713 result->oprs[operand].type |= COLON;
714 }
715 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200716 if (recover) {
717 do { /* error recovery */
718 i = stdscan(NULL, &tokval);
719 } while (i != 0 && i != ',');
720 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000721
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300722 /*
723 * now convert the exprs returned from evaluate()
724 * into operand descriptions...
725 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000726
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 if (mref) { /* it's a memory reference */
728 expr *e = value;
729 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300730 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000731
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 b = i = -1, o = s = 0;
733 result->oprs[operand].hintbase = hints.base;
734 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000735
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
737 if (e->value == 1) /* in fact it can be basereg */
738 b = e->type;
739 else /* no, it has to be indexreg */
740 i = e->type, s = e->value;
741 e++;
742 }
743 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
744 if (b != -1) /* If the first was the base, ... */
745 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 else if (e->value != 1) { /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700748 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 "beroset-p-592-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400750 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000751 return result;
752 } else
753 b = e->type;
754 e++;
755 }
756 if (e->type != 0) { /* is there an offset? */
757 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700758 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400760 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 return result;
762 } else {
763 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100764 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765 o = 0; /* doesn't matter what */
766 result->oprs[operand].wrt = NO_SEG; /* nor this */
767 result->oprs[operand].segment = NO_SEG; /* or this */
768 while (e->type)
769 e++; /* go to the end of the line */
770 } else {
771 if (e->type == EXPR_SIMPLE) {
772 o = e->value;
773 e++;
774 }
775 if (e->type == EXPR_WRT) {
776 result->oprs[operand].wrt = e->value;
777 e++;
778 } else
779 result->oprs[operand].wrt = NO_SEG;
780 /*
781 * Look for a segment base type.
782 */
783 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700784 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 "beroset-p-630-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400786 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 return result;
788 }
789 while (e->type && e->value == 0)
790 e++;
791 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700792 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400794 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 return result;
796 }
797 if (e->type) {
798 result->oprs[operand].segment =
799 e->type - EXPR_SEGBASE;
800 e++;
801 } else
802 result->oprs[operand].segment = NO_SEG;
803 while (e->type && e->value == 0)
804 e++;
805 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700806 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400808 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 return result;
810 }
811 }
812 }
813 } else {
814 o = 0;
815 result->oprs[operand].wrt = NO_SEG;
816 result->oprs[operand].segment = NO_SEG;
817 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700820 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400822 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 return result;
824 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000825
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300826 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700827 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000828
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300829 if (b == -1 && (i == -1 || s == 0)) {
830 int is_rel = globalbits == 64 &&
831 !(result->oprs[operand].eaflags & EAF_ABS) &&
832 ((globalrel &&
833 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
834 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000835
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300836 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
837 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 result->oprs[operand].basereg = b;
839 result->oprs[operand].indexreg = i;
840 result->oprs[operand].scale = s;
841 result->oprs[operand].offset = o;
842 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000843 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400844 result->oprs[operand].type |= IMMEDIATE;
845 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
846 result->oprs[operand].offset = 0; /* don't care */
847 result->oprs[operand].segment = NO_SEG; /* don't care again */
848 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100849
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400850 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300851 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700852 result->oprs[operand].type |=
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400853 SBYTE16 | SBYTE32 | SBYTE64 | UDWORD64 | SDWORD64;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300854 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400856 result->oprs[operand].type |= IMMEDIATE;
857 result->oprs[operand].offset = reloc_value(value);
858 result->oprs[operand].segment = reloc_seg(value);
859 result->oprs[operand].wrt = reloc_wrt(value);
860
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 if (is_simple(value)) {
862 if (reloc_value(value) == 1)
863 result->oprs[operand].type |= UNITY;
864 if (optimizing >= 0 &&
865 !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300866 int64_t v64 = reloc_value(value);
867 int32_t v32 = (int32_t)v64;
868 int16_t v16 = (int16_t)v32;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700869
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400870 if (v64 >= -128 && v64 <= 127)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700871 result->oprs[operand].type |= SBYTE64;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400872 if (v32 >= -128 && v32 <= 127)
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300873 result->oprs[operand].type |= SBYTE32;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400874 if (v16 >= -128 && v16 <= 127)
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300875 result->oprs[operand].type |= SBYTE16;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400876 if ((uint64_t)v64 <= UINT64_C(0xffffffff))
877 result->oprs[operand].type |= UDWORD64;
878 if (v64 >= -INT64_C(0x80000000) &&
879 v64 <= INT64_C(0x7fffffff))
880 result->oprs[operand].type |= SDWORD64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000881 }
882 }
883 } else { /* it's a register */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300884 unsigned int rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000885
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700887 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400888 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 return result;
890 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000891
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 /*
893 * check that its only 1 register, not an expression...
894 */
895 for (i = 1; value[i].type; i++)
896 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700897 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400898 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 return result;
900 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000901
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 /* clear overrides, except TO which applies to FPU regs */
903 if (result->oprs[operand].type & ~TO) {
904 /*
905 * we want to produce a warning iff the specified size
906 * is different from the register size
907 */
H. Peter Anvin68222142007-11-18 22:18:09 -0800908 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 } else
H. Peter Anvin68222142007-11-18 22:18:09 -0800910 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400912 result->oprs[operand].type &= TO;
913 result->oprs[operand].type |= REGISTER;
914 result->oprs[operand].type |= nasm_reg_flags[value->type];
915 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916
H. Peter Anvin68222142007-11-18 22:18:09 -0800917 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700918 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 "register size specification ignored");
920 }
921 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000922 }
923
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000925
Cyrill Gorcunovc2509502009-10-14 15:36:45 +0400926 /* clear remaining operands */
927 while (operand < MAX_OPERANDS)
928 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000929
930 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -0700931 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000932 */
933 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 case I_RESW:
935 result->opcode = I_RESB;
936 result->oprs[0].offset *= 2;
937 break;
938 case I_RESD:
939 result->opcode = I_RESB;
940 result->oprs[0].offset *= 4;
941 break;
942 case I_RESQ:
943 result->opcode = I_RESB;
944 result->oprs[0].offset *= 8;
945 break;
946 case I_REST:
947 result->opcode = I_RESB;
948 result->oprs[0].offset *= 10;
949 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700950 case I_RESO:
951 result->opcode = I_RESB;
952 result->oprs[0].offset *= 16;
953 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700954 case I_RESY:
955 result->opcode = I_RESB;
956 result->oprs[0].offset *= 32;
957 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700958 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300959 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000960 }
961
962 return result;
963}
964
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000966{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400967 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000968 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000969 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000970
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300971 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300973 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400974
H. Peter Anvin76690a12002-04-30 20:52:49 +0000975 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000976}
977
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000979{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000980 extop *e;
981
H. Peter Anvin2aa77392008-06-15 17:39:45 -0700982 while ((e = i->eops)) {
983 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300984 if (e->type == EOT_DB_STRING_FREE)
985 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000987 }
988}