blob: 889adf39e7c40a5e61f39bd70e0abede225fdc97 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07003 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * parser.c source line parser for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
47#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000048#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000049#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000050#include "stdscan.h"
H. Peter Anvin00444ae2009-07-18 18:49:55 -070051#include "eval.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000052#include "parser.h"
53#include "float.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070054#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000055
H. Peter Anvine2c80182005-01-15 22:15:51 +000056extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030057extern int32_t abs_seg; /* ABSOLUTE segment */
58extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000059
H. Peter Anvine2c80182005-01-15 22:15:51 +000060static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062static int i;
63static struct tokenval tokval;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030064static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000065
H. Peter Anvin605f5152009-07-18 18:31:41 -070066void parser_global_info(struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000067{
H. Peter Anvineba20a72002-04-30 20:53:55 +000068 location = locp;
69}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000070
Cyrill Gorcunov18914e62011-11-12 11:41:51 +040071static int prefix_slot(int prefix)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070072{
73 switch (prefix) {
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -080074 case P_WAIT:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030075 return PPS_WAIT;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070076 case R_CS:
77 case R_DS:
78 case R_SS:
79 case R_ES:
80 case R_FS:
81 case R_GS:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030082 return PPS_SEG;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070083 case P_LOCK:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080084 return PPS_LOCK;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070085 case P_REP:
86 case P_REPE:
87 case P_REPZ:
88 case P_REPNE:
89 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -080090 case P_XACQUIRE:
91 case P_XRELEASE:
H. Peter Anvin10da41e2012-02-24 20:57:04 -080092 return PPS_REP;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070093 case P_O16:
94 case P_O32:
95 case P_O64:
96 case P_OSP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030097 return PPS_OSIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070098 case P_A16:
99 case P_A32:
100 case P_A64:
101 case P_ASP:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300102 return PPS_ASIZE;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700103 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300104 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
105 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700106 }
107}
108
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300109static void process_size_override(insn *result, int operand)
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700110{
111 if (tasm_compatible_mode) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300112 switch ((int)tokval.t_integer) {
113 /* For TASM compatibility a size override inside the
114 * brackets changes the size of the operand, not the
115 * address type of the operand as it does in standard
116 * NASM syntax. Hence:
117 *
118 * mov eax,[DWORD val]
119 *
120 * is valid syntax in TASM compatibility mode. Note that
121 * you lose the ability to override the default address
122 * type for the instruction, but we never use anything
123 * but 32-bit flat model addressing in our code.
124 */
125 case S_BYTE:
126 result->oprs[operand].type |= BITS8;
127 break;
128 case S_WORD:
129 result->oprs[operand].type |= BITS16;
130 break;
131 case S_DWORD:
132 case S_LONG:
133 result->oprs[operand].type |= BITS32;
134 break;
135 case S_QWORD:
136 result->oprs[operand].type |= BITS64;
137 break;
138 case S_TWORD:
139 result->oprs[operand].type |= BITS80;
140 break;
141 case S_OWORD:
142 result->oprs[operand].type |= BITS128;
143 break;
144 default:
145 nasm_error(ERR_NONFATAL,
146 "invalid operand size specification");
147 break;
148 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700149 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300150 /* Standard NASM compatible syntax */
151 switch ((int)tokval.t_integer) {
152 case S_NOSPLIT:
153 result->oprs[operand].eaflags |= EAF_TIMESTWO;
154 break;
155 case S_REL:
156 result->oprs[operand].eaflags |= EAF_REL;
157 break;
158 case S_ABS:
159 result->oprs[operand].eaflags |= EAF_ABS;
160 break;
161 case S_BYTE:
162 result->oprs[operand].disp_size = 8;
163 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
164 break;
165 case P_A16:
166 case P_A32:
167 case P_A64:
168 if (result->prefixes[PPS_ASIZE] &&
169 result->prefixes[PPS_ASIZE] != tokval.t_integer)
170 nasm_error(ERR_NONFATAL,
171 "conflicting address size specifications");
172 else
173 result->prefixes[PPS_ASIZE] = tokval.t_integer;
174 break;
175 case S_WORD:
176 result->oprs[operand].disp_size = 16;
177 result->oprs[operand].eaflags |= EAF_WORDOFFS;
178 break;
179 case S_DWORD:
180 case S_LONG:
181 result->oprs[operand].disp_size = 32;
182 result->oprs[operand].eaflags |= EAF_WORDOFFS;
183 break;
184 case S_QWORD:
185 result->oprs[operand].disp_size = 64;
186 result->oprs[operand].eaflags |= EAF_WORDOFFS;
187 break;
188 default:
189 nasm_error(ERR_NONFATAL, "invalid size specification in"
190 " effective address");
191 break;
192 }
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700193 }
194}
195
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700196insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400198 bool insn_is_label = false;
199 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000200 int operand;
201 int critical;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800202 bool first;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700203 bool recover;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400204 int j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000205
H. Peter Anvin9c987692007-11-04 21:09:32 -0800206restart_parse:
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400207 first = true;
208 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000209
H. Peter Anvin76690a12002-04-30 20:52:49 +0000210 stdscan_reset();
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300211 stdscan_set(buffer);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000212 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000213
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400214 result->label = NULL; /* Assume no label */
215 result->eops = NULL; /* must do this, whatever happens */
216 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400218 /* Ignore blank lines */
219 if (i == TOKEN_EOS) {
220 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000221 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000222 }
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400223
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400224 if (i != TOKEN_ID &&
225 i != TOKEN_INSN &&
226 i != TOKEN_PREFIX &&
227 (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
228 nasm_error(ERR_NONFATAL,
229 "label or instruction expected at start of line");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400230 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000231 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000232 }
233
H. Peter Anvin9c987692007-11-04 21:09:32 -0800234 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
235 /* there's a label here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300236 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000237 result->label = tokval.t_charptr;
238 i = stdscan(NULL, &tokval);
239 if (i == ':') { /* skip over the optional colon */
240 i = stdscan(NULL, &tokval);
241 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700242 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000243 "label alone on a line without a colon might be in error");
244 }
245 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
246 /*
247 * FIXME: location->segment could be NO_SEG, in which case
248 * it is possible we should be passing 'abs_seg'. Look into this.
249 * Work out whether that is *really* what we should be doing.
250 * Generally fix things. I think this is right as it is, but
251 * am still not certain.
252 */
253 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700254 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000255 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000256 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000257
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400258 /* Just a label here */
259 if (i == TOKEN_EOS) {
260 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000261 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000262 }
263
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700264 for (j = 0; j < MAXPREFIX; j++)
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300265 result->prefixes[j] = P_none;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000267
268 while (i == TOKEN_PREFIX ||
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400269 (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300270 first = false;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800271
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272 /*
273 * Handle special case: the TIMES prefix.
274 */
275 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
276 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000277
H. Peter Anvine2c80182005-01-15 22:15:51 +0000278 i = stdscan(NULL, &tokval);
Cyrill Gorcunov1f4ccb92011-08-28 19:53:11 +0400279 value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000280 i = tokval.t_type;
281 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400282 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283 return result; /* ignore this instruction */
284 }
285 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700286 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287 "non-constant argument supplied to TIMES");
288 result->times = 1L;
289 } else {
290 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700291 if (value->value < 0 && pass0 == 2) {
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200292 nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000293 value->value);
294 result->times = 0;
295 }
296 }
297 } else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300298 int slot = prefix_slot(tokval.t_integer);
299 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700300 if (result->prefixes[slot] == tokval.t_integer)
Victor van den Elzend55a1582010-11-07 23:47:13 +0100301 nasm_error(ERR_WARNING | ERR_PASS1,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300302 "instruction has redundant prefixes");
Charles Crayne052c0bd2007-10-29 18:24:59 -0700303 else
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300304 nasm_error(ERR_NONFATAL,
305 "instruction has conflicting prefixes");
306 }
307 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000308 i = stdscan(NULL, &tokval);
309 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000310 }
311
312 if (i != TOKEN_INSN) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300313 int j;
314 enum prefixes pfx;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700315
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400316 for (j = 0; j < MAXPREFIX; j++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300317 if ((pfx = result->prefixes[j]) != P_none)
318 break;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400319 }
H. Peter Anvincb583b92007-10-28 22:04:42 -0700320
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700321 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 /*
323 * Instruction prefixes are present, but no actual
324 * instruction. This is allowed: at this point we
325 * invent a notional instruction of RESB 0.
326 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400327 result->opcode = I_RESB;
328 result->operands = 1;
329 result->oprs[0].type = IMMEDIATE;
330 result->oprs[0].offset = 0L;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000331 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
332 return result;
333 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700334 nasm_error(ERR_NONFATAL, "parser: instruction expected");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400335 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000336 return result;
337 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000338 }
339
340 result->opcode = tokval.t_integer;
341 result->condition = tokval.t_inttwo;
342
343 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700344 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000345 * evaluated operands, since the correct values _must_ be known
346 * on the first pass. Hence, even in pass one, we set the
347 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700348 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000349 */
Charles Crayne2581c862008-09-10 19:21:52 -0700350 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700351 critical = (pass0 < 2 ? 1 : 2);
352
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 } else
354 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000355
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700356 if (result->opcode == I_DB || result->opcode == I_DW ||
357 result->opcode == I_DD || result->opcode == I_DQ ||
358 result->opcode == I_DT || result->opcode == I_DO ||
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300359 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360 extop *eop, **tail = &result->eops, **fixptr;
361 int oper_num = 0;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300362 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000363
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700364 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000365
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700367 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 */
369 while (1) {
370 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400371 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300373 else if (first && i == ':') {
374 insn_is_label = true;
375 goto restart_parse;
376 }
377 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000378 fixptr = tail;
379 eop = *tail = nasm_malloc(sizeof(extop));
380 tail = &eop->next;
381 eop->next = NULL;
382 eop->type = EOT_NOTHING;
383 oper_num++;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300384 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000385
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300386 /*
387 * is_comma_next() here is to distinguish this from
388 * a string used as part of an expression...
389 */
H. Peter Anvin11627042008-06-09 20:45:19 -0700390 if (i == TOKEN_STR && is_comma_next()) {
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400391 eop->type = EOT_DB_STRING;
392 eop->stringval = tokval.t_charptr;
393 eop->stringlen = tokval.t_inttwo;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 i = stdscan(NULL, &tokval); /* eat the comma */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300395 } else if (i == TOKEN_STRFUNC) {
396 bool parens = false;
397 const char *funcname = tokval.t_charptr;
398 enum strfunc func = tokval.t_integer;
399 i = stdscan(NULL, &tokval);
400 if (i == '(') {
401 parens = true;
402 i = stdscan(NULL, &tokval);
403 }
404 if (i != TOKEN_STR) {
405 nasm_error(ERR_NONFATAL,
406 "%s must be followed by a string constant",
407 funcname);
408 eop->type = EOT_NOTHING;
409 } else {
410 eop->type = EOT_DB_STRING_FREE;
411 eop->stringlen =
412 string_transform(tokval.t_charptr, tokval.t_inttwo,
413 &eop->stringval, func);
414 if (eop->stringlen == (size_t)-1) {
415 nasm_error(ERR_NONFATAL, "invalid string for transform");
416 eop->type = EOT_NOTHING;
417 }
418 }
419 if (parens && i && i != ')') {
420 i = stdscan(NULL, &tokval);
421 if (i != ')') {
422 nasm_error(ERR_NONFATAL, "unterminated %s function",
423 funcname);
424 }
425 }
426 if (i && i != ',')
427 i = stdscan(NULL, &tokval);
428 } else if (i == '-' || i == '+') {
429 char *save = stdscan_get();
430 int token = i;
431 sign = (i == '-') ? -1 : 1;
432 i = stdscan(NULL, &tokval);
433 if (i != TOKEN_FLOAT) {
434 stdscan_set(save);
435 i = tokval.t_type = token;
436 goto is_expression;
437 } else {
438 goto is_float;
439 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700440 } else if (i == TOKEN_FLOAT) {
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300441is_float:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300442 eop->type = EOT_DB_STRING;
443 result->eops_float = true;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300444
445 eop->stringlen = idata_bytes(result->opcode);
446 if (eop->stringlen > 16) {
447 nasm_error(ERR_NONFATAL, "floating-point constant"
448 " encountered in DY instruction");
449 eop->stringlen = 0;
450 } else if (eop->stringlen < 1) {
451 nasm_error(ERR_NONFATAL, "floating-point constant"
452 " encountered in unknown instruction");
453 /*
454 * fix suggested by Pedro Gimeno... original line was:
455 * eop->type = EOT_NOTHING;
456 */
457 eop->stringlen = 0;
458 }
459
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300460 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
461 tail = &eop->next;
462 *fixptr = eop;
463 eop->stringval = (char *)eop + sizeof(extop);
464 if (!eop->stringlen ||
465 !float_const(tokval.t_charptr, sign,
466 (uint8_t *)eop->stringval,
467 eop->stringlen, nasm_error))
468 eop->type = EOT_NOTHING;
469 i = stdscan(NULL, &tokval); /* eat the comma */
470 } else {
471 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700473
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300474is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700476 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477 i = tokval.t_type;
478 if (!value) { /* error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400479 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 return result; /* ignore this instruction */
481 }
482 if (is_unknown(value)) {
483 eop->type = EOT_DB_NUMBER;
484 eop->offset = 0; /* doesn't matter what we put */
485 eop->segment = eop->wrt = NO_SEG; /* likewise */
486 } else if (is_reloc(value)) {
487 eop->type = EOT_DB_NUMBER;
488 eop->offset = reloc_value(value);
489 eop->segment = reloc_seg(value);
490 eop->wrt = reloc_wrt(value);
491 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700492 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 "operand %d: expression is not simple"
494 " or relocatable", oper_num);
495 }
496 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000497
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 /*
499 * We're about to call stdscan(), which will eat the
500 * comma that we're currently sitting on between
501 * arguments. However, we'd better check first that it
502 * _is_ a comma.
503 */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400504 if (i == TOKEN_EOS) /* also could be EOL */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 break;
506 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700507 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400508 oper_num);
509 result->opcode = I_none;/* unrecoverable parse error: */
510 return result; /* ignore this instruction */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 }
512 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000513
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 if (result->opcode == I_INCBIN) {
515 /*
516 * Correct syntax for INCBIN is that there should be
517 * one string operand, followed by one or two numeric
518 * operands.
519 */
520 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700521 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 else if (result->eops->next &&
523 result->eops->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200524 nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400525 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 else if (result->eops->next && result->eops->next->next &&
527 result->eops->next->next->type != EOT_DB_NUMBER)
Victor van den Elzen15bb2332009-08-11 02:10:16 +0200528 nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400529 " non-numeric");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 else if (result->eops->next && result->eops->next->next &&
531 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700532 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400533 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000534 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 return result;
536 /*
537 * If we reach here, one of the above errors happened.
538 * Throw the instruction away.
539 */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400540 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 return result;
542 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700543 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 "no operand for data declaration");
545 else
546 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000547
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000549 }
550
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400551 /*
552 * Now we begin to parse the operands. There may be up to four
553 * of these, separated by commas, and terminated by a zero token.
554 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000555
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700556 for (operand = 0; operand < MAX_OPERANDS; operand++) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300557 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 int mref; /* is this going to be a memory ref? */
559 int bracket; /* is it a [] mref, or a & mref? */
560 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000561
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700562 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400563 result->oprs[operand].eaflags = 0; /* and this */
564 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000565
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 i = stdscan(NULL, &tokval);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400567 if (i == TOKEN_EOS)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 break; /* end of operands: get out of here */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300569 else if (first && i == ':') {
570 insn_is_label = true;
571 goto restart_parse;
572 }
573 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 result->oprs[operand].type = 0; /* so far, no override */
575 while (i == TOKEN_SPECIAL) { /* size specifiers */
576 switch ((int)tokval.t_integer) {
577 case S_BYTE:
578 if (!setsize) /* we want to use only the first */
579 result->oprs[operand].type |= BITS8;
580 setsize = 1;
581 break;
582 case S_WORD:
583 if (!setsize)
584 result->oprs[operand].type |= BITS16;
585 setsize = 1;
586 break;
587 case S_DWORD:
588 case S_LONG:
589 if (!setsize)
590 result->oprs[operand].type |= BITS32;
591 setsize = 1;
592 break;
593 case S_QWORD:
594 if (!setsize)
595 result->oprs[operand].type |= BITS64;
596 setsize = 1;
597 break;
598 case S_TWORD:
599 if (!setsize)
600 result->oprs[operand].type |= BITS80;
601 setsize = 1;
602 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700603 case S_OWORD:
604 if (!setsize)
605 result->oprs[operand].type |= BITS128;
606 setsize = 1;
607 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700608 case S_YWORD:
609 if (!setsize)
610 result->oprs[operand].type |= BITS256;
611 setsize = 1;
612 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 case S_TO:
614 result->oprs[operand].type |= TO;
615 break;
616 case S_STRICT:
617 result->oprs[operand].type |= STRICT;
618 break;
619 case S_FAR:
620 result->oprs[operand].type |= FAR;
621 break;
622 case S_NEAR:
623 result->oprs[operand].type |= NEAR;
624 break;
625 case S_SHORT:
626 result->oprs[operand].type |= SHORT;
627 break;
628 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700629 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 }
631 i = stdscan(NULL, &tokval);
632 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000633
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700635 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000636 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700637 i = stdscan(NULL, &tokval); /* then skip the colon */
638 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300639 process_size_override(result, operand);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700640 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 }
642 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700643 mref = false;
644 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000646
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 if ((result->oprs[operand].type & FAR) && !mref &&
648 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700649 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000651
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 value = evaluate(stdscan, NULL, &tokval,
653 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700654 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 i = tokval.t_type;
656 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700657 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700659 if (!value) { /* nasm_error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400660 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 return result; /* ignore this instruction */
662 }
663 if (i == ':' && mref) { /* it was seg:offset */
664 /*
665 * Process the segment override.
666 */
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400667 if (value[1].type != 0 ||
668 value->value != 1 ||
669 !IS_SREG(value->type))
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700670 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700671 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700672 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700673 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000674 else {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300675 result->prefixes[PPS_SEG] = value->type;
Cyrill Gorcunov5abbe372011-08-28 18:49:00 +0400676 if (IS_FSGS(value->type))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300677 result->oprs[operand].eaflags |= EAF_FSGS;
678 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000679
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700681 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300682 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000683 i = stdscan(NULL, &tokval);
684 }
685 value = evaluate(stdscan, NULL, &tokval,
686 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700687 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000688 i = tokval.t_type;
689 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700690 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000691 }
692 /* and get the offset */
693 if (!value) { /* but, error in evaluator */
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400694 result->opcode = I_none; /* unrecoverable parse error: */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 return result; /* ignore this instruction */
696 }
697 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200698
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700699 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000700 if (mref && bracket) { /* find ] at the end */
701 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700702 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200703 recover = true;
704 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 i = stdscan(NULL, &tokval);
Victor van den Elzen02846d32009-06-23 03:47:07 +0200706 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700707 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200708 recover = true;
709 }
710 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 } else { /* immediate operand */
712 if (i != 0 && i != ',' && i != ':') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700713 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200714 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 } else if (i == ':') {
716 result->oprs[operand].type |= COLON;
717 }
718 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200719 if (recover) {
720 do { /* error recovery */
721 i = stdscan(NULL, &tokval);
722 } while (i != 0 && i != ',');
723 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000724
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300725 /*
726 * now convert the exprs returned from evaluate()
727 * into operand descriptions...
728 */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000729
H. Peter Anvine2c80182005-01-15 22:15:51 +0000730 if (mref) { /* it's a memory reference */
731 expr *e = value;
732 int b, i, s; /* basereg, indexreg, scale */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300733 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000734
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 b = i = -1, o = s = 0;
736 result->oprs[operand].hintbase = hints.base;
737 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000738
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
740 if (e->value == 1) /* in fact it can be basereg */
741 b = e->type;
742 else /* no, it has to be indexreg */
743 i = e->type, s = e->value;
744 e++;
745 }
746 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
747 if (b != -1) /* If the first was the base, ... */
748 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000749
H. Peter Anvine2c80182005-01-15 22:15:51 +0000750 else if (e->value != 1) { /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700751 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000752 "beroset-p-592-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 b = e->type;
757 e++;
758 }
759 if (e->type != 0) { /* is there an offset? */
760 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700761 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 "beroset-p-603-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400763 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 return result;
765 } else {
766 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100767 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 o = 0; /* doesn't matter what */
769 result->oprs[operand].wrt = NO_SEG; /* nor this */
770 result->oprs[operand].segment = NO_SEG; /* or this */
771 while (e->type)
772 e++; /* go to the end of the line */
773 } else {
774 if (e->type == EXPR_SIMPLE) {
775 o = e->value;
776 e++;
777 }
778 if (e->type == EXPR_WRT) {
779 result->oprs[operand].wrt = e->value;
780 e++;
781 } else
782 result->oprs[operand].wrt = NO_SEG;
783 /*
784 * Look for a segment base type.
785 */
786 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700787 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 "beroset-p-630-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400789 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 return result;
791 }
792 while (e->type && e->value == 0)
793 e++;
794 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700795 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 "beroset-p-637-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400797 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 return result;
799 }
800 if (e->type) {
801 result->oprs[operand].segment =
802 e->type - EXPR_SEGBASE;
803 e++;
804 } else
805 result->oprs[operand].segment = NO_SEG;
806 while (e->type && e->value == 0)
807 e++;
808 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700809 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 "beroset-p-650-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400811 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 return result;
813 }
814 }
815 }
816 } else {
817 o = 0;
818 result->oprs[operand].wrt = NO_SEG;
819 result->oprs[operand].segment = NO_SEG;
820 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000821
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700823 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 "beroset-p-663-invalid effective address");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400825 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 return result;
827 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000828
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300829 /* It is memory, but it can match any r/m operand */
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700830 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000831
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300832 if (b == -1 && (i == -1 || s == 0)) {
833 int is_rel = globalbits == 64 &&
834 !(result->oprs[operand].eaflags & EAF_ABS) &&
835 ((globalrel &&
836 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
837 (result->oprs[operand].eaflags & EAF_REL));
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000838
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300839 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
840 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 result->oprs[operand].basereg = b;
842 result->oprs[operand].indexreg = i;
843 result->oprs[operand].scale = s;
844 result->oprs[operand].offset = o;
845 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 if (is_just_unknown(value)) { /* it's immediate but unknown */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400847 result->oprs[operand].type |= IMMEDIATE;
848 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
849 result->oprs[operand].offset = 0; /* don't care */
850 result->oprs[operand].segment = NO_SEG; /* don't care again */
851 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100852
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400853 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300854 /* Be optimistic */
H. Peter Anvin9df01072010-08-24 14:08:16 -0700855 result->oprs[operand].type |=
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400856 SBYTE16 | SBYTE32 | SBYTE64 | UDWORD64 | SDWORD64;
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300857 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 } else if (is_reloc(value)) { /* it's immediate */
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400859 result->oprs[operand].type |= IMMEDIATE;
860 result->oprs[operand].offset = reloc_value(value);
861 result->oprs[operand].segment = reloc_seg(value);
862 result->oprs[operand].wrt = reloc_wrt(value);
863
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 if (is_simple(value)) {
865 if (reloc_value(value) == 1)
866 result->oprs[operand].type |= UNITY;
867 if (optimizing >= 0 &&
868 !(result->oprs[operand].type & STRICT)) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300869 int64_t v64 = reloc_value(value);
870 int32_t v32 = (int32_t)v64;
871 int16_t v16 = (int16_t)v32;
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700872
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400873 if (v64 >= -128 && v64 <= 127)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700874 result->oprs[operand].type |= SBYTE64;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400875 if (v32 >= -128 && v32 <= 127)
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300876 result->oprs[operand].type |= SBYTE32;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400877 if (v16 >= -128 && v16 <= 127)
Cyrill Gorcunov210c1012009-11-01 10:24:48 +0300878 result->oprs[operand].type |= SBYTE16;
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400879 if ((uint64_t)v64 <= UINT64_C(0xffffffff))
880 result->oprs[operand].type |= UDWORD64;
881 if (v64 >= -INT64_C(0x80000000) &&
882 v64 <= INT64_C(0x7fffffff))
883 result->oprs[operand].type |= SDWORD64;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 }
885 }
886 } else { /* it's a register */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +0400887 opflags_t rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000888
H. Peter Anvine2c80182005-01-15 22:15:51 +0000889 if (value->type >= EXPR_SIMPLE || value->value != 1) {
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 /*
896 * check that its only 1 register, not an expression...
897 */
898 for (i = 1; value[i].type; i++)
899 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700900 nasm_error(ERR_NONFATAL, "invalid operand type");
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400901 result->opcode = I_none;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 return result;
903 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000904
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 /* clear overrides, except TO which applies to FPU regs */
906 if (result->oprs[operand].type & ~TO) {
907 /*
908 * we want to produce a warning iff the specified size
909 * is different from the register size
910 */
H. Peter Anvin68222142007-11-18 22:18:09 -0800911 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 } else
H. Peter Anvin68222142007-11-18 22:18:09 -0800913 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400915 result->oprs[operand].type &= TO;
916 result->oprs[operand].type |= REGISTER;
917 result->oprs[operand].type |= nasm_reg_flags[value->type];
918 result->oprs[operand].basereg = value->type;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919
H. Peter Anvin68222142007-11-18 22:18:09 -0800920 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700921 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 "register size specification ignored");
923 }
924 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000925 }
926
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000928
Cyrill Gorcunovc2509502009-10-14 15:36:45 +0400929 /* clear remaining operands */
930 while (operand < MAX_OPERANDS)
931 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000932
933 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -0700934 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000935 */
936 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 case I_RESW:
938 result->opcode = I_RESB;
939 result->oprs[0].offset *= 2;
940 break;
941 case I_RESD:
942 result->opcode = I_RESB;
943 result->oprs[0].offset *= 4;
944 break;
945 case I_RESQ:
946 result->opcode = I_RESB;
947 result->oprs[0].offset *= 8;
948 break;
949 case I_REST:
950 result->opcode = I_RESB;
951 result->oprs[0].offset *= 10;
952 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700953 case I_RESO:
954 result->opcode = I_RESB;
955 result->oprs[0].offset *= 16;
956 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700957 case I_RESY:
958 result->opcode = I_RESB;
959 result->oprs[0].offset *= 32;
960 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700961 default:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300962 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000963 }
964
965 return result;
966}
967
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000969{
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400970 struct tokenval tv;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000971 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000972 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000973
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300974 p = stdscan_get();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 i = stdscan(NULL, &tv);
Cyrill Gorcunov917117f2009-10-29 23:09:18 +0300976 stdscan_set(p);
Cyrill Gorcunov447e20c2011-08-28 18:02:31 +0400977
H. Peter Anvin76690a12002-04-30 20:52:49 +0000978 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000979}
980
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000982{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000983 extop *e;
984
H. Peter Anvin2aa77392008-06-15 17:39:45 -0700985 while ((e = i->eops)) {
986 i->eops = e->next;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300987 if (e->type == EOT_DB_STRING_FREE)
988 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000990 }
991}