blob: 42ac1097f35cd061eed3bfc39873bb1a4406850c [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
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070071static int prefix_slot(enum prefixes prefix)
72{
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{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000195 int operand;
196 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000197 struct eval_hints hints;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700198 int j;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800199 bool first;
200 bool insn_is_label = false;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700201 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000202
H. Peter Anvin9c987692007-11-04 21:09:32 -0800203restart_parse:
204 first = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700205 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
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211 result->label = NULL; /* Assume no label */
212 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000213 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215 if (i == 0) { /* blank line - ignore */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400216 result->opcode = I_none; /* and no instruction either */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218 }
219 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvina4835d42008-05-20 14:21:29 -0700220 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700221 nasm_error(ERR_NONFATAL, "label or instruction expected"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222 " at start of line");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400223 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000224 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000225 }
226
H. Peter Anvin9c987692007-11-04 21:09:32 -0800227 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
228 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300229 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 result->label = tokval.t_charptr;
231 i = stdscan(NULL, &tokval);
232 if (i == ':') { /* skip over the optional colon */
233 i = stdscan(NULL, &tokval);
234 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700235 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236 "label alone on a line without a colon might be in error");
237 }
238 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
239 /*
240 * FIXME: location->segment could be NO_SEG, in which case
241 * it is possible we should be passing 'abs_seg'. Look into this.
242 * Work out whether that is *really* what we should be doing.
243 * Generally fix things. I think this is right as it is, but
244 * am still not certain.
245 */
246 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700247 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000248 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000249 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000250
H. Peter Anvine2c80182005-01-15 22:15:51 +0000251 if (i == 0) {
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400252 result->opcode = I_none; /* this line contains just a label */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000253 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000254 }
255
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700256 for (j = 0; j < MAXPREFIX; j++)
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300257 result->prefixes[j] = P_none;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000259
260 while (i == TOKEN_PREFIX ||
H. Peter Anvina4835d42008-05-20 14:21:29 -0700261 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000262 {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300263 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800264
H. Peter Anvine2c80182005-01-15 22:15:51 +0000265 /*
266 * Handle special case: the TIMES prefix.
267 */
268 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
269 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000270
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271 i = stdscan(NULL, &tokval);
272 value =
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700273 evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000274 i = tokval.t_type;
275 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400276 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000277 return result; /* ignore this instruction */
278 }
279 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700280 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281 "non-constant argument supplied to TIMES");
282 result->times = 1L;
283 } else {
284 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700285 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200286 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287 value->value);
288 result->times = 0;
289 }
290 }
291 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300292 int slot = prefix_slot(tokval.t_integer);
293 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700294 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100295 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300296 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700297 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300298 nasm_error(ERR_NONFATAL,
299 "instruction has conflicting prefixes");
300 }
301 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000302 i = stdscan(NULL, &tokval);
303 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000304 }
305
306 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300307 int j;
308 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700309
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300310 for (j = 0; j < MAXPREFIX; j++)
311 if ((pfx = result->prefixes[j]) != P_none)
312 break;
H. Peter Anvincb583b92007-10-28 22:04:42 -0700313
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700314 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000315 /*
316 * Instruction prefixes are present, but no actual
317 * instruction. This is allowed: at this point we
318 * invent a notional instruction of RESB 0.
319 */
320 result->opcode = I_RESB;
321 result->operands = 1;
322 result->oprs[0].type = IMMEDIATE;
323 result->oprs[0].offset = 0L;
324 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
325 return result;
326 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700327 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400328 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329 return result;
330 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000331 }
332
333 result->opcode = tokval.t_integer;
334 result->condition = tokval.t_inttwo;
335
336 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700337 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000338 * evaluated operands, since the correct values _must_ be known
339 * on the first pass. Hence, even in pass one, we set the
340 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700341 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000342 */
Charles Crayne2581c862008-09-10 19:21:52 -0700343 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700344 critical = (pass0 < 2 ? 1 : 2);
345
H. Peter Anvine2c80182005-01-15 22:15:51 +0000346 } else
347 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000348
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700349 if (result->opcode == I_DB || result->opcode == I_DW ||
350 result->opcode == I_DD || result->opcode == I_DQ ||
351 result->opcode == I_DT || result->opcode == I_DO ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300352 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 extop *eop, **tail = &result->eops, **fixptr;
354 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300355 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700357 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700360 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000361 */
362 while (1) {
363 i = stdscan(NULL, &tokval);
364 if (i == 0)
365 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300366 else if (first && i == ':') {
367 insn_is_label = true;
368 goto restart_parse;
369 }
370 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371 fixptr = tail;
372 eop = *tail = nasm_malloc(sizeof(extop));
373 tail = &eop->next;
374 eop->next = NULL;
375 eop->type = EOT_NOTHING;
376 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300377 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000378
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300379 /*
380 * is_comma_next() here is to distinguish this from
381 * a string used as part of an expression...
382 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700383 if (i == TOKEN_STR && is_comma_next()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384 eop->type = EOT_DB_STRING;
385 eop->stringval = tokval.t_charptr;
386 eop->stringlen = tokval.t_inttwo;
387 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300388 } else if (i == TOKEN_STRFUNC) {
389 bool parens = false;
390 const char *funcname = tokval.t_charptr;
391 enum strfunc func = tokval.t_integer;
392 i = stdscan(NULL, &tokval);
393 if (i == '(') {
394 parens = true;
395 i = stdscan(NULL, &tokval);
396 }
397 if (i != TOKEN_STR) {
398 nasm_error(ERR_NONFATAL,
399 "%s must be followed by a string constant",
400 funcname);
401 eop->type = EOT_NOTHING;
402 } else {
403 eop->type = EOT_DB_STRING_FREE;
404 eop->stringlen =
405 string_transform(tokval.t_charptr, tokval.t_inttwo,
406 &eop->stringval, func);
407 if (eop->stringlen == (size_t)-1) {
408 nasm_error(ERR_NONFATAL, "invalid string for transform");
409 eop->type = EOT_NOTHING;
410 }
411 }
412 if (parens && i && i != ')') {
413 i = stdscan(NULL, &tokval);
414 if (i != ')') {
415 nasm_error(ERR_NONFATAL, "unterminated %s function",
416 funcname);
417 }
418 }
419 if (i && i != ',')
420 i = stdscan(NULL, &tokval);
421 } else if (i == '-' || i == '+') {
422 char *save = stdscan_get();
423 int token = i;
424 sign = (i == '-') ? -1 : 1;
425 i = stdscan(NULL, &tokval);
426 if (i != TOKEN_FLOAT) {
427 stdscan_set(save);
428 i = tokval.t_type = token;
429 goto is_expression;
430 } else {
431 goto is_float;
432 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700433 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300434is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300435 eop->type = EOT_DB_STRING;
436 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300437
438 eop->stringlen = idata_bytes(result->opcode);
439 if (eop->stringlen > 16) {
440 nasm_error(ERR_NONFATAL, "floating-point constant"
441 " encountered in DY instruction");
442 eop->stringlen = 0;
443 } else if (eop->stringlen < 1) {
444 nasm_error(ERR_NONFATAL, "floating-point constant"
445 " encountered in unknown instruction");
446 /*
447 * fix suggested by Pedro Gimeno... original line was:
448 * eop->type = EOT_NOTHING;
449 */
450 eop->stringlen = 0;
451 }
452
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300453 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
454 tail = &eop->next;
455 *fixptr = eop;
456 eop->stringval = (char *)eop + sizeof(extop);
457 if (!eop->stringlen ||
458 !float_const(tokval.t_charptr, sign,
459 (uint8_t *)eop->stringval,
460 eop->stringlen, nasm_error))
461 eop->type = EOT_NOTHING;
462 i = stdscan(NULL, &tokval); /* eat the comma */
463 } else {
464 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700466
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300467is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700469 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 i = tokval.t_type;
471 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400472 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 return result; /* ignore this instruction */
474 }
475 if (is_unknown(value)) {
476 eop->type = EOT_DB_NUMBER;
477 eop->offset = 0; /* doesn't matter what we put */
478 eop->segment = eop->wrt = NO_SEG; /* likewise */
479 } else if (is_reloc(value)) {
480 eop->type = EOT_DB_NUMBER;
481 eop->offset = reloc_value(value);
482 eop->segment = reloc_seg(value);
483 eop->wrt = reloc_wrt(value);
484 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700485 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 "operand %d: expression is not simple"
487 " or relocatable", oper_num);
488 }
489 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 /*
492 * We're about to call stdscan(), which will eat the
493 * comma that we're currently sitting on between
494 * arguments. However, we'd better check first that it
495 * _is_ a comma.
496 */
497 if (i == 0) /* also could be EOL */
498 break;
499 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700500 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 oper_num);
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400502 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 return result; /* ignore this instruction */
504 }
505 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000506
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 if (result->opcode == I_INCBIN) {
508 /*
509 * Correct syntax for INCBIN is that there should be
510 * one string operand, followed by one or two numeric
511 * operands.
512 */
513 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700514 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 else if (result->eops->next &&
516 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200517 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 " non-numeric");
519 else if (result->eops->next && result->eops->next->next &&
520 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200521 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 " non-numeric");
523 else if (result->eops->next && result->eops->next->next &&
524 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700525 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 return result;
529 /*
530 * If we reach here, one of the above errors happened.
531 * Throw the instruction away.
532 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400533 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 return result;
535 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700536 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 "no operand for data declaration");
538 else
539 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000540
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000542 }
543
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700544 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000545 * of these, separated by commas, and terminated by a zero token. */
546
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700547 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300548 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 int mref; /* is this going to be a memory ref? */
550 int bracket; /* is it a [] mref, or a & mref? */
551 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000552
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700553 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 result->oprs[operand].eaflags = 0; /* and this */
555 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000556
H. Peter Anvine2c80182005-01-15 22:15:51 +0000557 i = stdscan(NULL, &tokval);
558 if (i == 0)
559 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300560 else if (first && i == ':') {
561 insn_is_label = true;
562 goto restart_parse;
563 }
564 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 result->oprs[operand].type = 0; /* so far, no override */
566 while (i == TOKEN_SPECIAL) { /* size specifiers */
567 switch ((int)tokval.t_integer) {
568 case S_BYTE:
569 if (!setsize) /* we want to use only the first */
570 result->oprs[operand].type |= BITS8;
571 setsize = 1;
572 break;
573 case S_WORD:
574 if (!setsize)
575 result->oprs[operand].type |= BITS16;
576 setsize = 1;
577 break;
578 case S_DWORD:
579 case S_LONG:
580 if (!setsize)
581 result->oprs[operand].type |= BITS32;
582 setsize = 1;
583 break;
584 case S_QWORD:
585 if (!setsize)
586 result->oprs[operand].type |= BITS64;
587 setsize = 1;
588 break;
589 case S_TWORD:
590 if (!setsize)
591 result->oprs[operand].type |= BITS80;
592 setsize = 1;
593 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700594 case S_OWORD:
595 if (!setsize)
596 result->oprs[operand].type |= BITS128;
597 setsize = 1;
598 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700599 case S_YWORD:
600 if (!setsize)
601 result->oprs[operand].type |= BITS256;
602 setsize = 1;
603 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 case S_TO:
605 result->oprs[operand].type |= TO;
606 break;
607 case S_STRICT:
608 result->oprs[operand].type |= STRICT;
609 break;
610 case S_FAR:
611 result->oprs[operand].type |= FAR;
612 break;
613 case S_NEAR:
614 result->oprs[operand].type |= NEAR;
615 break;
616 case S_SHORT:
617 result->oprs[operand].type |= SHORT;
618 break;
619 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700620 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 }
622 i = stdscan(NULL, &tokval);
623 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000624
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700626 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700628 i = stdscan(NULL, &tokval); /* then skip the colon */
629 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300630 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700631 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 }
633 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700634 mref = false;
635 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000637
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 if ((result->oprs[operand].type & FAR) && !mref &&
639 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700640 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000642
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 value = evaluate(stdscan, NULL, &tokval,
644 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700645 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 i = tokval.t_type;
647 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700648 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700650 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400651 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 return result; /* ignore this instruction */
653 }
654 if (i == ':' && mref) { /* it was seg:offset */
655 /*
656 * Process the segment override.
657 */
658 if (value[1].type != 0 || value->value != 1 ||
H. Peter Anvina4835d42008-05-20 14:21:29 -0700659 REG_SREG & ~nasm_reg_flags[value->type])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700660 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700661 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700662 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700663 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000664 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300665 result->prefixes[PPS_SEG] = value->type;
666 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
667 result->oprs[operand].eaflags |= EAF_FSGS;
668 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000669
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700671 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300672 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000673 i = stdscan(NULL, &tokval);
674 }
675 value = evaluate(stdscan, NULL, &tokval,
676 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700677 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 i = tokval.t_type;
679 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700680 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 }
682 /* and get the offset */
683 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400684 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685 return result; /* ignore this instruction */
686 }
687 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200688
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700689 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 if (mref && bracket) { /* find ] at the end */
691 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700692 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200693 recover = true;
694 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 i = stdscan(NULL, &tokval);
Victor van den Elzen02846d32009-06-23 03:47:07 +0200696 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700697 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200698 recover = true;
699 }
700 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000701 } else { /* immediate operand */
702 if (i != 0 && i != ',' && i != ':') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700703 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200704 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 } else if (i == ':') {
706 result->oprs[operand].type |= COLON;
707 }
708 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200709 if (recover) {
710 do { /* error recovery */
711 i = stdscan(NULL, &tokval);
712 } while (i != 0 && i != ',');
713 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000714
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300715 /*
716 * now convert the exprs returned from evaluate()
717 * into operand descriptions...
718 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000719
H. Peter Anvine2c80182005-01-15 22:15:51 +0000720 if (mref) { /* it's a memory reference */
721 expr *e = value;
722 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300723 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000724
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 b = i = -1, o = s = 0;
726 result->oprs[operand].hintbase = hints.base;
727 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000728
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
730 if (e->value == 1) /* in fact it can be basereg */
731 b = e->type;
732 else /* no, it has to be indexreg */
733 i = e->type, s = e->value;
734 e++;
735 }
736 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
737 if (b != -1) /* If the first was the base, ... */
738 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000739
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 else if (e->value != 1) { /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700741 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000742 "beroset-p-592-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400743 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 return result;
745 } else
746 b = e->type;
747 e++;
748 }
749 if (e->type != 0) { /* is there an offset? */
750 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700751 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400753 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000754 return result;
755 } else {
756 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100757 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000758 o = 0; /* doesn't matter what */
759 result->oprs[operand].wrt = NO_SEG; /* nor this */
760 result->oprs[operand].segment = NO_SEG; /* or this */
761 while (e->type)
762 e++; /* go to the end of the line */
763 } else {
764 if (e->type == EXPR_SIMPLE) {
765 o = e->value;
766 e++;
767 }
768 if (e->type == EXPR_WRT) {
769 result->oprs[operand].wrt = e->value;
770 e++;
771 } else
772 result->oprs[operand].wrt = NO_SEG;
773 /*
774 * Look for a segment base type.
775 */
776 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700777 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000778 "beroset-p-630-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400779 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 return result;
781 }
782 while (e->type && e->value == 0)
783 e++;
784 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700785 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400787 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 return result;
789 }
790 if (e->type) {
791 result->oprs[operand].segment =
792 e->type - EXPR_SEGBASE;
793 e++;
794 } else
795 result->oprs[operand].segment = NO_SEG;
796 while (e->type && e->value == 0)
797 e++;
798 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700799 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 "beroset-p-650-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 }
805 }
806 } else {
807 o = 0;
808 result->oprs[operand].wrt = NO_SEG;
809 result->oprs[operand].segment = NO_SEG;
810 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700813 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 "beroset-p-663-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 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300819 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700820 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000821
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300822 if (b == -1 && (i == -1 || s == 0)) {
823 int is_rel = globalbits == 64 &&
824 !(result->oprs[operand].eaflags & EAF_ABS) &&
825 ((globalrel &&
826 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
827 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000828
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300829 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
830 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 result->oprs[operand].basereg = b;
832 result->oprs[operand].indexreg = i;
833 result->oprs[operand].scale = s;
834 result->oprs[operand].offset = o;
835 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 if (is_just_unknown(value)) { /* it's immediate but unknown */
837 result->oprs[operand].type |= IMMEDIATE;
Victor van den Elzen154e5922009-02-25 17:32:00 +0100838 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 result->oprs[operand].offset = 0; /* don't care */
840 result->oprs[operand].segment = NO_SEG; /* don't care again */
841 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100842
843 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT))
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300844 {
845 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700846 result->oprs[operand].type |=
847 SBYTE16 | SBYTE32 | SBYTE64 | UDWORD64 | SDWORD64;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300848 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 } else if (is_reloc(value)) { /* it's immediate */
850 result->oprs[operand].type |= IMMEDIATE;
851 result->oprs[operand].offset = reloc_value(value);
852 result->oprs[operand].segment = reloc_seg(value);
853 result->oprs[operand].wrt = reloc_wrt(value);
854 if (is_simple(value)) {
855 if (reloc_value(value) == 1)
856 result->oprs[operand].type |= UNITY;
857 if (optimizing >= 0 &&
858 !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300859 int64_t v64 = reloc_value(value);
860 int32_t v32 = (int32_t)v64;
861 int16_t v16 = (int16_t)v32;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700862
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300863 if (v64 >= -128 && v64 <= 127)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700864 result->oprs[operand].type |= SBYTE64;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300865 if (v32 >= -128 && v32 <= 127)
866 result->oprs[operand].type |= SBYTE32;
867 if (v16 >= -128 && v16 <= 127)
868 result->oprs[operand].type |= SBYTE16;
H. Peter Anvin9df01072010-08-24 14:08:16 -0700869 if ((uint64_t)v64 <= UINT64_C(0xffffffff))
870 result->oprs[operand].type |= UDWORD64;
Cyrill Gorcunov68a34402010-08-27 23:25:04 +0400871 if (v64 >= -INT64_C(0x80000000) &&
872 v64 <= INT64_C(0x7fffffff))
H. Peter Anvin9df01072010-08-24 14:08:16 -0700873 result->oprs[operand].type |= SDWORD64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 }
875 }
876 } else { /* it's a register */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300877 unsigned int rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000878
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700880 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400881 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 return result;
883 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000884
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 /*
886 * check that its only 1 register, not an expression...
887 */
888 for (i = 1; value[i].type; i++)
889 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700890 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400891 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 return result;
893 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000894
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 /* clear overrides, except TO which applies to FPU regs */
896 if (result->oprs[operand].type & ~TO) {
897 /*
898 * we want to produce a warning iff the specified size
899 * is different from the register size
900 */
H. Peter Anvin68222142007-11-18 22:18:09 -0800901 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 } else
H. Peter Anvin68222142007-11-18 22:18:09 -0800903 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904
905 result->oprs[operand].type &= TO;
906 result->oprs[operand].type |= REGISTER;
H. Peter Anvina4835d42008-05-20 14:21:29 -0700907 result->oprs[operand].type |= nasm_reg_flags[value->type];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 result->oprs[operand].basereg = value->type;
909
H. Peter Anvin68222142007-11-18 22:18:09 -0800910 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700911 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 "register size specification ignored");
913 }
914 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000915 }
916
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000918
Cyrill Gorcunovc2509502009-10-14 15:36:45 +0400919 /* clear remaining operands */
920 while (operand < MAX_OPERANDS)
921 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000922
923 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -0700924 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000925 */
926 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 case I_RESW:
928 result->opcode = I_RESB;
929 result->oprs[0].offset *= 2;
930 break;
931 case I_RESD:
932 result->opcode = I_RESB;
933 result->oprs[0].offset *= 4;
934 break;
935 case I_RESQ:
936 result->opcode = I_RESB;
937 result->oprs[0].offset *= 8;
938 break;
939 case I_REST:
940 result->opcode = I_RESB;
941 result->oprs[0].offset *= 10;
942 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700943 case I_RESO:
944 result->opcode = I_RESB;
945 result->oprs[0].offset *= 16;
946 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700947 case I_RESY:
948 result->opcode = I_RESB;
949 result->oprs[0].offset *= 32;
950 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700951 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300952 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000953 }
954
955 return result;
956}
957
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000959{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000960 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000961 int i;
962 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000963
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300964 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300966 stdscan_set(p);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000967 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000968}
969
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000971{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000972 extop *e;
973
H. Peter Anvin2aa77392008-06-15 17:39:45 -0700974 while ((e = i->eops)) {
975 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300976 if (e->type == EOT_DB_STRING_FREE)
977 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000979 }
980}