blob: 5ca9f860ed7ce2c227d2f21b0bef4a377ffbe7fe [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{
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);
276 value =
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700277 evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000278 i = tokval.t_type;
279 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400280 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281 return result; /* ignore this instruction */
282 }
283 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700284 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285 "non-constant argument supplied to TIMES");
286 result->times = 1L;
287 } else {
288 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700289 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200290 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291 value->value);
292 result->times = 0;
293 }
294 }
295 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300296 int slot = prefix_slot(tokval.t_integer);
297 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700298 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100299 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300300 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700301 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300302 nasm_error(ERR_NONFATAL,
303 "instruction has conflicting prefixes");
304 }
305 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306 i = stdscan(NULL, &tokval);
307 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000308 }
309
310 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300311 int j;
312 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700313
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400314 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300315 if ((pfx = result->prefixes[j]) != P_none)
316 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400317 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700318
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700319 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000320 /*
321 * Instruction prefixes are present, but no actual
322 * instruction. This is allowed: at this point we
323 * invent a notional instruction of RESB 0.
324 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400325 result->opcode = I_RESB;
326 result->operands = 1;
327 result->oprs[0].type = IMMEDIATE;
328 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
330 return result;
331 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700332 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400333 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000334 return result;
335 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000336 }
337
338 result->opcode = tokval.t_integer;
339 result->condition = tokval.t_inttwo;
340
341 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700342 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000343 * evaluated operands, since the correct values _must_ be known
344 * on the first pass. Hence, even in pass one, we set the
345 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700346 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000347 */
Charles Crayne2581c862008-09-10 19:21:52 -0700348 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700349 critical = (pass0 < 2 ? 1 : 2);
350
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 } else
352 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700354 if (result->opcode == I_DB || result->opcode == I_DW ||
355 result->opcode == I_DD || result->opcode == I_DQ ||
356 result->opcode == I_DT || result->opcode == I_DO ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300357 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358 extop *eop, **tail = &result->eops, **fixptr;
359 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300360 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000361
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700362 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000363
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700365 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 */
367 while (1) {
368 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400369 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300371 else if (first && i == ':') {
372 insn_is_label = true;
373 goto restart_parse;
374 }
375 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 fixptr = tail;
377 eop = *tail = nasm_malloc(sizeof(extop));
378 tail = &eop->next;
379 eop->next = NULL;
380 eop->type = EOT_NOTHING;
381 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300382 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000383
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300384 /*
385 * is_comma_next() here is to distinguish this from
386 * a string used as part of an expression...
387 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700388 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400389 eop->type = EOT_DB_STRING;
390 eop->stringval = tokval.t_charptr;
391 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300393 } else if (i == TOKEN_STRFUNC) {
394 bool parens = false;
395 const char *funcname = tokval.t_charptr;
396 enum strfunc func = tokval.t_integer;
397 i = stdscan(NULL, &tokval);
398 if (i == '(') {
399 parens = true;
400 i = stdscan(NULL, &tokval);
401 }
402 if (i != TOKEN_STR) {
403 nasm_error(ERR_NONFATAL,
404 "%s must be followed by a string constant",
405 funcname);
406 eop->type = EOT_NOTHING;
407 } else {
408 eop->type = EOT_DB_STRING_FREE;
409 eop->stringlen =
410 string_transform(tokval.t_charptr, tokval.t_inttwo,
411 &eop->stringval, func);
412 if (eop->stringlen == (size_t)-1) {
413 nasm_error(ERR_NONFATAL, "invalid string for transform");
414 eop->type = EOT_NOTHING;
415 }
416 }
417 if (parens && i && i != ')') {
418 i = stdscan(NULL, &tokval);
419 if (i != ')') {
420 nasm_error(ERR_NONFATAL, "unterminated %s function",
421 funcname);
422 }
423 }
424 if (i && i != ',')
425 i = stdscan(NULL, &tokval);
426 } else if (i == '-' || i == '+') {
427 char *save = stdscan_get();
428 int token = i;
429 sign = (i == '-') ? -1 : 1;
430 i = stdscan(NULL, &tokval);
431 if (i != TOKEN_FLOAT) {
432 stdscan_set(save);
433 i = tokval.t_type = token;
434 goto is_expression;
435 } else {
436 goto is_float;
437 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700438 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300439is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300440 eop->type = EOT_DB_STRING;
441 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300442
443 eop->stringlen = idata_bytes(result->opcode);
444 if (eop->stringlen > 16) {
445 nasm_error(ERR_NONFATAL, "floating-point constant"
446 " encountered in DY instruction");
447 eop->stringlen = 0;
448 } else if (eop->stringlen < 1) {
449 nasm_error(ERR_NONFATAL, "floating-point constant"
450 " encountered in unknown instruction");
451 /*
452 * fix suggested by Pedro Gimeno... original line was:
453 * eop->type = EOT_NOTHING;
454 */
455 eop->stringlen = 0;
456 }
457
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300458 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
459 tail = &eop->next;
460 *fixptr = eop;
461 eop->stringval = (char *)eop + sizeof(extop);
462 if (!eop->stringlen ||
463 !float_const(tokval.t_charptr, sign,
464 (uint8_t *)eop->stringval,
465 eop->stringlen, nasm_error))
466 eop->type = EOT_NOTHING;
467 i = stdscan(NULL, &tokval); /* eat the comma */
468 } else {
469 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700471
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300472is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700474 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 i = tokval.t_type;
476 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400477 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 return result; /* ignore this instruction */
479 }
480 if (is_unknown(value)) {
481 eop->type = EOT_DB_NUMBER;
482 eop->offset = 0; /* doesn't matter what we put */
483 eop->segment = eop->wrt = NO_SEG; /* likewise */
484 } else if (is_reloc(value)) {
485 eop->type = EOT_DB_NUMBER;
486 eop->offset = reloc_value(value);
487 eop->segment = reloc_seg(value);
488 eop->wrt = reloc_wrt(value);
489 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700490 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 "operand %d: expression is not simple"
492 " or relocatable", oper_num);
493 }
494 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000495
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 /*
497 * We're about to call stdscan(), which will eat the
498 * comma that we're currently sitting on between
499 * arguments. However, we'd better check first that it
500 * _is_ a comma.
501 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400502 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 break;
504 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700505 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400506 oper_num);
507 result->opcode = I_none;/* unrecoverable parse error: */
508 return result; /* ignore this instruction */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 }
510 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 if (result->opcode == I_INCBIN) {
513 /*
514 * Correct syntax for INCBIN is that there should be
515 * one string operand, followed by one or two numeric
516 * operands.
517 */
518 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700519 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 else if (result->eops->next &&
521 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200522 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400523 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 else if (result->eops->next && result->eops->next->next &&
525 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200526 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400527 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 else if (result->eops->next && result->eops->next->next &&
529 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700530 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400531 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 return result;
534 /*
535 * If we reach here, one of the above errors happened.
536 * Throw the instruction away.
537 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400538 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 return result;
540 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700541 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 "no operand for data declaration");
543 else
544 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000545
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000547 }
548
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400549 /*
550 * Now we begin to parse the operands. There may be up to four
551 * of these, separated by commas, and terminated by a zero token.
552 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000553
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700554 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300555 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 int mref; /* is this going to be a memory ref? */
557 int bracket; /* is it a [] mref, or a & mref? */
558 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000559
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700560 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400561 result->oprs[operand].eaflags = 0; /* and this */
562 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000563
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400565 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300567 else if (first && i == ':') {
568 insn_is_label = true;
569 goto restart_parse;
570 }
571 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 result->oprs[operand].type = 0; /* so far, no override */
573 while (i == TOKEN_SPECIAL) { /* size specifiers */
574 switch ((int)tokval.t_integer) {
575 case S_BYTE:
576 if (!setsize) /* we want to use only the first */
577 result->oprs[operand].type |= BITS8;
578 setsize = 1;
579 break;
580 case S_WORD:
581 if (!setsize)
582 result->oprs[operand].type |= BITS16;
583 setsize = 1;
584 break;
585 case S_DWORD:
586 case S_LONG:
587 if (!setsize)
588 result->oprs[operand].type |= BITS32;
589 setsize = 1;
590 break;
591 case S_QWORD:
592 if (!setsize)
593 result->oprs[operand].type |= BITS64;
594 setsize = 1;
595 break;
596 case S_TWORD:
597 if (!setsize)
598 result->oprs[operand].type |= BITS80;
599 setsize = 1;
600 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700601 case S_OWORD:
602 if (!setsize)
603 result->oprs[operand].type |= BITS128;
604 setsize = 1;
605 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700606 case S_YWORD:
607 if (!setsize)
608 result->oprs[operand].type |= BITS256;
609 setsize = 1;
610 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 case S_TO:
612 result->oprs[operand].type |= TO;
613 break;
614 case S_STRICT:
615 result->oprs[operand].type |= STRICT;
616 break;
617 case S_FAR:
618 result->oprs[operand].type |= FAR;
619 break;
620 case S_NEAR:
621 result->oprs[operand].type |= NEAR;
622 break;
623 case S_SHORT:
624 result->oprs[operand].type |= SHORT;
625 break;
626 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700627 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000628 }
629 i = stdscan(NULL, &tokval);
630 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000631
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700633 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700635 i = stdscan(NULL, &tokval); /* then skip the colon */
636 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300637 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700638 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 }
640 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700641 mref = false;
642 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000644
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 if ((result->oprs[operand].type & FAR) && !mref &&
646 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700647 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000649
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 value = evaluate(stdscan, NULL, &tokval,
651 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700652 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 i = tokval.t_type;
654 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700655 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700657 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400658 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 return result; /* ignore this instruction */
660 }
661 if (i == ':' && mref) { /* it was seg:offset */
662 /*
663 * Process the segment override.
664 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400665 if (value[1].type != 0 ||
666 value->value != 1 ||
667 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700668 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700669 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700670 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700671 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000672 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300673 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400674 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300675 result->oprs[operand].eaflags |= EAF_FSGS;
676 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000677
H. Peter Anvine2c80182005-01-15 22:15:51 +0000678 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700679 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300680 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 i = stdscan(NULL, &tokval);
682 }
683 value = evaluate(stdscan, NULL, &tokval,
684 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700685 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 i = tokval.t_type;
687 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700688 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 }
690 /* and get the offset */
691 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400692 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 return result; /* ignore this instruction */
694 }
695 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200696
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700697 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 if (mref && bracket) { /* find ] at the end */
699 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700700 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200701 recover = true;
702 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 i = stdscan(NULL, &tokval);
Victor van den Elzen02846d32009-06-23 03:47:07 +0200704 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700705 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200706 recover = true;
707 }
708 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 } else { /* immediate operand */
710 if (i != 0 && i != ',' && i != ':') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700711 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200712 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000713 } else if (i == ':') {
714 result->oprs[operand].type |= COLON;
715 }
716 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200717 if (recover) {
718 do { /* error recovery */
719 i = stdscan(NULL, &tokval);
720 } while (i != 0 && i != ',');
721 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000722
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300723 /*
724 * now convert the exprs returned from evaluate()
725 * into operand descriptions...
726 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000727
H. Peter Anvine2c80182005-01-15 22:15:51 +0000728 if (mref) { /* it's a memory reference */
729 expr *e = value;
730 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300731 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000732
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 b = i = -1, o = s = 0;
734 result->oprs[operand].hintbase = hints.base;
735 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000736
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
738 if (e->value == 1) /* in fact it can be basereg */
739 b = e->type;
740 else /* no, it has to be indexreg */
741 i = e->type, s = e->value;
742 e++;
743 }
744 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
745 if (b != -1) /* If the first was the base, ... */
746 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000747
H. Peter Anvine2c80182005-01-15 22:15:51 +0000748 else if (e->value != 1) { /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700749 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 "beroset-p-592-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400751 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 return result;
753 } else
754 b = e->type;
755 e++;
756 }
757 if (e->type != 0) { /* is there an offset? */
758 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700759 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400761 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 return result;
763 } else {
764 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100765 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 o = 0; /* doesn't matter what */
767 result->oprs[operand].wrt = NO_SEG; /* nor this */
768 result->oprs[operand].segment = NO_SEG; /* or this */
769 while (e->type)
770 e++; /* go to the end of the line */
771 } else {
772 if (e->type == EXPR_SIMPLE) {
773 o = e->value;
774 e++;
775 }
776 if (e->type == EXPR_WRT) {
777 result->oprs[operand].wrt = e->value;
778 e++;
779 } else
780 result->oprs[operand].wrt = NO_SEG;
781 /*
782 * Look for a segment base type.
783 */
784 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700785 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 "beroset-p-630-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 while (e->type && e->value == 0)
791 e++;
792 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700793 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400795 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 return result;
797 }
798 if (e->type) {
799 result->oprs[operand].segment =
800 e->type - EXPR_SEGBASE;
801 e++;
802 } else
803 result->oprs[operand].segment = NO_SEG;
804 while (e->type && e->value == 0)
805 e++;
806 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700807 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400809 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 return result;
811 }
812 }
813 }
814 } else {
815 o = 0;
816 result->oprs[operand].wrt = NO_SEG;
817 result->oprs[operand].segment = NO_SEG;
818 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700821 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400823 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 return result;
825 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300827 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700828 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000829
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300830 if (b == -1 && (i == -1 || s == 0)) {
831 int is_rel = globalbits == 64 &&
832 !(result->oprs[operand].eaflags & EAF_ABS) &&
833 ((globalrel &&
834 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
835 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000836
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300837 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
838 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 result->oprs[operand].basereg = b;
840 result->oprs[operand].indexreg = i;
841 result->oprs[operand].scale = s;
842 result->oprs[operand].offset = o;
843 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400845 result->oprs[operand].type |= IMMEDIATE;
846 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
847 result->oprs[operand].offset = 0; /* don't care */
848 result->oprs[operand].segment = NO_SEG; /* don't care again */
849 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100850
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400851 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300852 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700853 result->oprs[operand].type |=
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400854 SBYTE16 | SBYTE32 | SBYTE64 | UDWORD64 | SDWORD64;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300855 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400857 result->oprs[operand].type |= IMMEDIATE;
858 result->oprs[operand].offset = reloc_value(value);
859 result->oprs[operand].segment = reloc_seg(value);
860 result->oprs[operand].wrt = reloc_wrt(value);
861
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 if (is_simple(value)) {
863 if (reloc_value(value) == 1)
864 result->oprs[operand].type |= UNITY;
865 if (optimizing >= 0 &&
866 !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300867 int64_t v64 = reloc_value(value);
868 int32_t v32 = (int32_t)v64;
869 int16_t v16 = (int16_t)v32;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700870
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400871 if (v64 >= -128 && v64 <= 127)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700872 result->oprs[operand].type |= SBYTE64;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400873 if (v32 >= -128 && v32 <= 127)
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300874 result->oprs[operand].type |= SBYTE32;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400875 if (v16 >= -128 && v16 <= 127)
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300876 result->oprs[operand].type |= SBYTE16;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400877 if ((uint64_t)v64 <= UINT64_C(0xffffffff))
878 result->oprs[operand].type |= UDWORD64;
879 if (v64 >= -INT64_C(0x80000000) &&
880 v64 <= INT64_C(0x7fffffff))
881 result->oprs[operand].type |= SDWORD64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 }
883 }
884 } else { /* it's a register */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300885 unsigned int rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000886
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700888 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400889 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 return result;
891 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 /*
894 * check that its only 1 register, not an expression...
895 */
896 for (i = 1; value[i].type; i++)
897 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700898 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400899 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 return result;
901 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000902
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 /* clear overrides, except TO which applies to FPU regs */
904 if (result->oprs[operand].type & ~TO) {
905 /*
906 * we want to produce a warning iff the specified size
907 * is different from the register size
908 */
H. Peter Anvin68222142007-11-18 22:18:09 -0800909 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 } else
H. Peter Anvin68222142007-11-18 22:18:09 -0800911 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400913 result->oprs[operand].type &= TO;
914 result->oprs[operand].type |= REGISTER;
915 result->oprs[operand].type |= nasm_reg_flags[value->type];
916 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917
H. Peter Anvin68222142007-11-18 22:18:09 -0800918 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700919 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 "register size specification ignored");
921 }
922 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000923 }
924
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000926
Cyrill Gorcunovc2509502009-10-14 15:36:45 +0400927 /* clear remaining operands */
928 while (operand < MAX_OPERANDS)
929 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000930
931 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -0700932 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000933 */
934 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 case I_RESW:
936 result->opcode = I_RESB;
937 result->oprs[0].offset *= 2;
938 break;
939 case I_RESD:
940 result->opcode = I_RESB;
941 result->oprs[0].offset *= 4;
942 break;
943 case I_RESQ:
944 result->opcode = I_RESB;
945 result->oprs[0].offset *= 8;
946 break;
947 case I_REST:
948 result->opcode = I_RESB;
949 result->oprs[0].offset *= 10;
950 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700951 case I_RESO:
952 result->opcode = I_RESB;
953 result->oprs[0].offset *= 16;
954 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700955 case I_RESY:
956 result->opcode = I_RESB;
957 result->oprs[0].offset *= 32;
958 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700959 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300960 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000961 }
962
963 return result;
964}
965
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000967{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400968 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000969 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000970 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000971
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300972 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300974 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400975
H. Peter Anvin76690a12002-04-30 20:52:49 +0000976 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000977}
978
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000980{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000981 extop *e;
982
H. Peter Anvin2aa77392008-06-15 17:39:45 -0700983 while ((e = i->eops)) {
984 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300985 if (e->type == EOT_DB_STRING_FREE)
986 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000988 }
989}