blob: 2ff6e36efe9d10ff198fd214b68d6e0e0e4b40de [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
2 *
3 * 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.
17 *
18 * 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 */
Keith Kaniosb7a89542007-04-12 02:40:54 +000057extern 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;
H. Peter Anvin12e46512007-10-03 21:30:57 -070064static 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:
75 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:
82 return PPS_SEG;
83 case P_LOCK:
84 case P_REP:
85 case P_REPE:
86 case P_REPZ:
87 case P_REPNE:
88 case P_REPNZ:
89 return PPS_LREP;
90 case P_O16:
91 case P_O32:
92 case P_O64:
93 case P_OSP:
94 return PPS_OSIZE;
95 case P_A16:
96 case P_A32:
97 case P_A64:
98 case P_ASP:
99 return PPS_ASIZE;
100 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700101 nasm_error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700102 return -1;
103 }
104}
105
106static void process_size_override(insn * result, int operand)
107{
108 if (tasm_compatible_mode) {
109 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:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700142 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700143 "invalid operand size specification");
144 break;
145 }
146 } else {
147 /* 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)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700167 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700168 "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:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700186 nasm_error(ERR_NONFATAL, "invalid size specification in"
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700187 " effective address");
188 break;
189 }
190 }
191}
192
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700193insn *parse_line(int pass, char *buffer, insn *result, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000195 int operand;
196 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000197 struct eval_hints hints;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700198 int j;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800199 bool first;
200 bool insn_is_label = false;
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700201 bool recover;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000202
H. Peter Anvin9c987692007-11-04 21:09:32 -0800203restart_parse:
204 first = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700205 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206
H. Peter Anvin76690a12002-04-30 20:52:49 +0000207 stdscan_reset();
208 stdscan_bufptr = buffer;
209 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000210
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211 result->label = NULL; /* Assume no label */
212 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000213 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215 if (i == 0) { /* blank line - ignore */
216 result->opcode = -1; /* and no instruction either */
217 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218 }
219 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvina4835d42008-05-20 14:21:29 -0700220 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700221 nasm_error(ERR_NONFATAL, "label or instruction expected"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222 " at start of line");
223 result->opcode = -1;
224 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000225 }
226
H. Peter Anvin9c987692007-11-04 21:09:32 -0800227 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
228 /* there's a label here */
229 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 result->label = tokval.t_charptr;
231 i = stdscan(NULL, &tokval);
232 if (i == ':') { /* skip over the optional colon */
233 i = stdscan(NULL, &tokval);
234 } else if (i == 0) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700235 nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236 "label alone on a line without a colon might be in error");
237 }
238 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
239 /*
240 * FIXME: location->segment could be NO_SEG, in which case
241 * it is possible we should be passing 'abs_seg'. Look into this.
242 * Work out whether that is *really* what we should be doing.
243 * Generally fix things. I think this is right as it is, but
244 * am still not certain.
245 */
246 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin605f5152009-07-18 18:31:41 -0700247 location->offset, NULL, true, false);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000248 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000249 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000250
H. Peter Anvine2c80182005-01-15 22:15:51 +0000251 if (i == 0) {
252 result->opcode = -1; /* this line contains just a label */
253 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000254 }
255
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700256 for (j = 0; j < MAXPREFIX; j++)
257 result->prefixes[j] = P_none;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000258 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000259
260 while (i == TOKEN_PREFIX ||
H. Peter Anvina4835d42008-05-20 14:21:29 -0700261 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000262 {
H. Peter Anvin9c987692007-11-04 21:09:32 -0800263 first = false;
264
H. Peter Anvine2c80182005-01-15 22:15:51 +0000265 /*
266 * Handle special case: the TIMES prefix.
267 */
268 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
269 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000270
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271 i = stdscan(NULL, &tokval);
272 value =
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700273 evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000274 i = tokval.t_type;
275 if (!value) { /* but, error in evaluator */
276 result->opcode = -1; /* unrecoverable parse error: */
277 return result; /* ignore this instruction */
278 }
279 if (!is_simple(value)) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700280 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281 "non-constant argument supplied to TIMES");
282 result->times = 1L;
283 } else {
284 result->times = value->value;
Charles Crayne7f596e72008-09-23 21:49:09 -0700285 if (value->value < 0 && pass0 == 2) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700286 nasm_error(ERR_NONFATAL, "TIMES value %d is negative",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287 value->value);
288 result->times = 0;
289 }
290 }
291 } else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700292 int slot = prefix_slot(tokval.t_integer);
293 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700294 if (result->prefixes[slot] == tokval.t_integer)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700295 nasm_error(ERR_WARNING,
Charles Crayne052c0bd2007-10-29 18:24:59 -0700296 "instruction has redundant prefixes");
297 else
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700298 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700299 "instruction has conflicting prefixes");
300 }
301 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000302 i = stdscan(NULL, &tokval);
303 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000304 }
305
306 if (i != TOKEN_INSN) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700307 int j;
308 enum prefixes pfx;
309
310 for (j = 0; j < MAXPREFIX; j++)
311 if ((pfx = result->prefixes[j]) != P_none)
312 break;
H. Peter Anvincb583b92007-10-28 22:04:42 -0700313
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700314 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000315 /*
316 * Instruction prefixes are present, but no actual
317 * instruction. This is allowed: at this point we
318 * invent a notional instruction of RESB 0.
319 */
320 result->opcode = I_RESB;
321 result->operands = 1;
322 result->oprs[0].type = IMMEDIATE;
323 result->oprs[0].offset = 0L;
324 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
325 return result;
326 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700327 nasm_error(ERR_NONFATAL, "parser: instruction expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 result->opcode = -1;
329 return result;
330 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000331 }
332
333 result->opcode = tokval.t_integer;
334 result->condition = tokval.t_inttwo;
335
336 /*
Charles Crayne2581c862008-09-10 19:21:52 -0700337 * INCBIN cannot be satisfied with incorrectly
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000338 * evaluated operands, since the correct values _must_ be known
339 * on the first pass. Hence, even in pass one, we set the
340 * `critical' flag on calling evaluate(), so that it will bomb
Charles Crayne2581c862008-09-10 19:21:52 -0700341 * out on undefined symbols.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000342 */
Charles Crayne2581c862008-09-10 19:21:52 -0700343 if (result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700344 critical = (pass0 < 2 ? 1 : 2);
345
H. Peter Anvine2c80182005-01-15 22:15:51 +0000346 } else
347 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000348
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700349 if (result->opcode == I_DB || result->opcode == I_DW ||
350 result->opcode == I_DD || result->opcode == I_DQ ||
351 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvindfb91802008-05-20 11:43:53 -0700352 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 extop *eop, **tail = &result->eops, **fixptr;
354 int oper_num = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700355 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700357 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700360 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000361 */
362 while (1) {
363 i = stdscan(NULL, &tokval);
364 if (i == 0)
365 break;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800366 else if (first && i == ':') {
367 insn_is_label = true;
368 goto restart_parse;
369 }
370 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000371 fixptr = tail;
372 eop = *tail = nasm_malloc(sizeof(extop));
373 tail = &eop->next;
374 eop->next = NULL;
375 eop->type = EOT_NOTHING;
376 oper_num++;
H. Peter Anvin518df302008-06-14 16:53:48 -0700377 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000378
H. Peter Anvin518df302008-06-14 16:53:48 -0700379 /* is_comma_next() here is to distinguish this from
380 a string used as part of an expression... */
H. Peter Anvin11627042008-06-09 20:45:19 -0700381 if (i == TOKEN_STR && is_comma_next()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382 eop->type = EOT_DB_STRING;
383 eop->stringval = tokval.t_charptr;
384 eop->stringlen = tokval.t_inttwo;
385 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvin518df302008-06-14 16:53:48 -0700386 } else if (i == TOKEN_STRFUNC) {
387 bool parens = false;
388 const char *funcname = tokval.t_charptr;
389 enum strfunc func = tokval.t_integer;
390 i = stdscan(NULL, &tokval);
391 if (i == '(') {
392 parens = true;
393 i = stdscan(NULL, &tokval);
394 }
395 if (i != TOKEN_STR) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700396 nasm_error(ERR_NONFATAL,
H. Peter Anvin518df302008-06-14 16:53:48 -0700397 "%s must be followed by a string constant",
398 funcname);
399 eop->type = EOT_NOTHING;
400 } else {
401 eop->type = EOT_DB_STRING_FREE;
402 eop->stringlen =
403 string_transform(tokval.t_charptr, tokval.t_inttwo,
404 &eop->stringval, func);
405 if (eop->stringlen == (size_t)-1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700406 nasm_error(ERR_NONFATAL, "invalid string for transform");
H. Peter Anvin518df302008-06-14 16:53:48 -0700407 eop->type = EOT_NOTHING;
408 }
409 }
410 if (parens && i && i != ')') {
411 i = stdscan(NULL, &tokval);
412 if (i != ')') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700413 nasm_error(ERR_NONFATAL, "unterminated %s function",
H. Peter Anvin518df302008-06-14 16:53:48 -0700414 funcname);
415 }
416 }
417 if (i && i != ',')
418 i = stdscan(NULL, &tokval);
419 } else if (i == '-' || i == '+') {
420 char *save = stdscan_bufptr;
421 int token = i;
422 sign = (i == '-') ? -1 : 1;
423 i = stdscan(NULL, &tokval);
424 if (i != TOKEN_FLOAT) {
425 stdscan_bufptr = save;
426 i = tokval.t_type = token;
427 goto is_expression;
428 } else {
429 goto is_float;
430 }
431 } else if (i == TOKEN_FLOAT) {
432 is_float:
433 eop->type = EOT_DB_STRING;
434 result->eops_float = true;
435 switch (result->opcode) {
436 case I_DB:
437 eop->stringlen = 1;
438 break;
439 case I_DW:
440 eop->stringlen = 2;
441 break;
442 case I_DD:
443 eop->stringlen = 4;
444 break;
445 case I_DQ:
446 eop->stringlen = 8;
447 break;
448 case I_DT:
449 eop->stringlen = 10;
450 break;
451 case I_DO:
452 eop->stringlen = 16;
453 break;
454 case I_DY:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700455 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin518df302008-06-14 16:53:48 -0700456 " encountered in DY instruction");
457 eop->stringlen = 0;
458 break;
459 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700460 nasm_error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin518df302008-06-14 16:53:48 -0700461 " encountered in unknown instruction");
462 /*
463 * fix suggested by Pedro Gimeno... original line
464 * was:
465 * eop->type = EOT_NOTHING;
466 */
467 eop->stringlen = 0;
468 break;
469 }
470 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
471 tail = &eop->next;
472 *fixptr = eop;
473 eop->stringval = (char *)eop + sizeof(extop);
474 if (!eop->stringlen ||
475 !float_const(tokval.t_charptr, sign,
476 (uint8_t *)eop->stringval,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700477 eop->stringlen, nasm_error))
H. Peter Anvin518df302008-06-14 16:53:48 -0700478 eop->type = EOT_NOTHING;
479 i = stdscan(NULL, &tokval); /* eat the comma */
480 } else {
481 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700483
484 is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 value = evaluate(stdscan, NULL, &tokval, NULL,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700486 critical, nasm_error, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 i = tokval.t_type;
488 if (!value) { /* error in evaluator */
489 result->opcode = -1; /* unrecoverable parse error: */
490 return result; /* ignore this instruction */
491 }
492 if (is_unknown(value)) {
493 eop->type = EOT_DB_NUMBER;
494 eop->offset = 0; /* doesn't matter what we put */
495 eop->segment = eop->wrt = NO_SEG; /* likewise */
496 } else if (is_reloc(value)) {
497 eop->type = EOT_DB_NUMBER;
498 eop->offset = reloc_value(value);
499 eop->segment = reloc_seg(value);
500 eop->wrt = reloc_wrt(value);
501 } else {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700502 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 "operand %d: expression is not simple"
504 " or relocatable", oper_num);
505 }
506 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000507
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 /*
509 * We're about to call stdscan(), which will eat the
510 * comma that we're currently sitting on between
511 * arguments. However, we'd better check first that it
512 * _is_ a comma.
513 */
514 if (i == 0) /* also could be EOL */
515 break;
516 if (i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700517 nasm_error(ERR_NONFATAL, "comma expected after operand %d",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 oper_num);
519 result->opcode = -1; /* unrecoverable parse error: */
520 return result; /* ignore this instruction */
521 }
522 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000523
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 if (result->opcode == I_INCBIN) {
525 /*
526 * Correct syntax for INCBIN is that there should be
527 * one string operand, followed by one or two numeric
528 * operands.
529 */
530 if (!result->eops || result->eops->type != EOT_DB_STRING)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700531 nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 else if (result->eops->next &&
533 result->eops->next->type != EOT_DB_NUMBER)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700534 nasm_error(ERR_NONFATAL, "`incbin': second parameter is",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 " non-numeric");
536 else if (result->eops->next && result->eops->next->next &&
537 result->eops->next->next->type != EOT_DB_NUMBER)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700538 nasm_error(ERR_NONFATAL, "`incbin': third parameter is",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 " non-numeric");
540 else if (result->eops->next && result->eops->next->next &&
541 result->eops->next->next->next)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700542 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000544 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 return result;
546 /*
547 * If we reach here, one of the above errors happened.
548 * Throw the instruction away.
549 */
550 result->opcode = -1;
551 return result;
552 } else /* DB ... */ if (oper_num == 0)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700553 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 "no operand for data declaration");
555 else
556 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000557
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000559 }
560
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700561 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562 * of these, separated by commas, and terminated by a zero token. */
563
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700564 for (operand = 0; operand < MAX_OPERANDS; operand++) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700565 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 int mref; /* is this going to be a memory ref? */
567 int bracket; /* is it a [] mref, or a & mref? */
568 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700570 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 result->oprs[operand].eaflags = 0; /* and this */
572 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 i = stdscan(NULL, &tokval);
575 if (i == 0)
576 break; /* end of operands: get out of here */
H. Peter Anvin9c987692007-11-04 21:09:32 -0800577 else if (first && i == ':') {
578 insn_is_label = true;
579 goto restart_parse;
580 }
581 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 result->oprs[operand].type = 0; /* so far, no override */
583 while (i == TOKEN_SPECIAL) { /* size specifiers */
584 switch ((int)tokval.t_integer) {
585 case S_BYTE:
586 if (!setsize) /* we want to use only the first */
587 result->oprs[operand].type |= BITS8;
588 setsize = 1;
589 break;
590 case S_WORD:
591 if (!setsize)
592 result->oprs[operand].type |= BITS16;
593 setsize = 1;
594 break;
595 case S_DWORD:
596 case S_LONG:
597 if (!setsize)
598 result->oprs[operand].type |= BITS32;
599 setsize = 1;
600 break;
601 case S_QWORD:
602 if (!setsize)
603 result->oprs[operand].type |= BITS64;
604 setsize = 1;
605 break;
606 case S_TWORD:
607 if (!setsize)
608 result->oprs[operand].type |= BITS80;
609 setsize = 1;
610 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700611 case S_OWORD:
612 if (!setsize)
613 result->oprs[operand].type |= BITS128;
614 setsize = 1;
615 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700616 case S_YWORD:
617 if (!setsize)
618 result->oprs[operand].type |= BITS256;
619 setsize = 1;
620 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 case S_TO:
622 result->oprs[operand].type |= TO;
623 break;
624 case S_STRICT:
625 result->oprs[operand].type |= STRICT;
626 break;
627 case S_FAR:
628 result->oprs[operand].type |= FAR;
629 break;
630 case S_NEAR:
631 result->oprs[operand].type |= NEAR;
632 break;
633 case S_SHORT:
634 result->oprs[operand].type |= SHORT;
635 break;
636 default:
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700637 nasm_error(ERR_NONFATAL, "invalid operand size specification");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 }
639 i = stdscan(NULL, &tokval);
640 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000641
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700643 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700645 i = stdscan(NULL, &tokval); /* then skip the colon */
646 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
647 process_size_override(result, operand);
648 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 }
650 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700651 mref = false;
652 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 if ((result->oprs[operand].type & FAR) && !mref &&
656 result->opcode != I_JMP && result->opcode != I_CALL) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700657 nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000659
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 value = evaluate(stdscan, NULL, &tokval,
661 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700662 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 i = tokval.t_type;
664 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700665 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 }
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700667 if (!value) { /* nasm_error in evaluator */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 result->opcode = -1; /* unrecoverable parse error: */
669 return result; /* ignore this instruction */
670 }
671 if (i == ':' && mref) { /* it was seg:offset */
672 /*
673 * Process the segment override.
674 */
675 if (value[1].type != 0 || value->value != 1 ||
H. Peter Anvina4835d42008-05-20 14:21:29 -0700676 REG_SREG & ~nasm_reg_flags[value->type])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700677 nasm_error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700678 else if (result->prefixes[PPS_SEG])
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700679 nasm_error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700680 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000681 else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700682 result->prefixes[PPS_SEG] = value->type;
H. Peter Anvina4835d42008-05-20 14:21:29 -0700683 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000684 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000685 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000686
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700688 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
689 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000690 i = stdscan(NULL, &tokval);
691 }
692 value = evaluate(stdscan, NULL, &tokval,
693 &result->oprs[operand].opflags,
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700694 critical, nasm_error, &hints);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695 i = tokval.t_type;
696 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700697 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 }
699 /* and get the offset */
700 if (!value) { /* but, error in evaluator */
701 result->opcode = -1; /* unrecoverable parse error: */
702 return result; /* ignore this instruction */
703 }
704 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200705
H. Peter Anvin552bc2c2009-06-23 11:34:42 -0700706 recover = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000707 if (mref && bracket) { /* find ] at the end */
708 if (i != ']') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700709 nasm_error(ERR_NONFATAL, "parser: expecting ]");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200710 recover = true;
711 } else { /* we got the required ] */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 i = stdscan(NULL, &tokval);
Victor van den Elzen02846d32009-06-23 03:47:07 +0200713 if (i != 0 && i != ',') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700714 nasm_error(ERR_NONFATAL, "comma or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200715 recover = true;
716 }
717 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 } else { /* immediate operand */
719 if (i != 0 && i != ',' && i != ':') {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700720 nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
Victor van den Elzen02846d32009-06-23 03:47:07 +0200721 recover = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000722 } else if (i == ':') {
723 result->oprs[operand].type |= COLON;
724 }
725 }
Victor van den Elzen02846d32009-06-23 03:47:07 +0200726 if (recover) {
727 do { /* error recovery */
728 i = stdscan(NULL, &tokval);
729 } while (i != 0 && i != ',');
730 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000731
H. Peter Anvine2c80182005-01-15 22:15:51 +0000732 /* now convert the exprs returned from evaluate() into operand
733 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000734
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 if (mref) { /* it's a memory reference */
736 expr *e = value;
737 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000738 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 b = i = -1, o = s = 0;
741 result->oprs[operand].hintbase = hints.base;
742 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
745 if (e->value == 1) /* in fact it can be basereg */
746 b = e->type;
747 else /* no, it has to be indexreg */
748 i = e->type, s = e->value;
749 e++;
750 }
751 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
752 if (b != -1) /* If the first was the base, ... */
753 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000754
H. Peter Anvine2c80182005-01-15 22:15:51 +0000755 else if (e->value != 1) { /* If both want to be index */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700756 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000757 "beroset-p-592-invalid effective address");
758 result->opcode = -1;
759 return result;
760 } else
761 b = e->type;
762 e++;
763 }
764 if (e->type != 0) { /* is there an offset? */
765 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700766 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767 "beroset-p-603-invalid effective address");
768 result->opcode = -1;
769 return result;
770 } else {
771 if (e->type == EXPR_UNKNOWN) {
Victor van den Elzen154e5922009-02-25 17:32:00 +0100772 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 o = 0; /* doesn't matter what */
774 result->oprs[operand].wrt = NO_SEG; /* nor this */
775 result->oprs[operand].segment = NO_SEG; /* or this */
776 while (e->type)
777 e++; /* go to the end of the line */
778 } else {
779 if (e->type == EXPR_SIMPLE) {
780 o = e->value;
781 e++;
782 }
783 if (e->type == EXPR_WRT) {
784 result->oprs[operand].wrt = e->value;
785 e++;
786 } else
787 result->oprs[operand].wrt = NO_SEG;
788 /*
789 * Look for a segment base type.
790 */
791 if (e->type && e->type < EXPR_SEGBASE) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700792 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 "beroset-p-630-invalid effective address");
794 result->opcode = -1;
795 return result;
796 }
797 while (e->type && e->value == 0)
798 e++;
799 if (e->type && e->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700800 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 "beroset-p-637-invalid effective address");
802 result->opcode = -1;
803 return result;
804 }
805 if (e->type) {
806 result->oprs[operand].segment =
807 e->type - EXPR_SEGBASE;
808 e++;
809 } else
810 result->oprs[operand].segment = NO_SEG;
811 while (e->type && e->value == 0)
812 e++;
813 if (e->type) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700814 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 "beroset-p-650-invalid effective address");
816 result->opcode = -1;
817 return result;
818 }
819 }
820 }
821 } else {
822 o = 0;
823 result->oprs[operand].wrt = NO_SEG;
824 result->oprs[operand].segment = NO_SEG;
825 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 if (e->type != 0) { /* there'd better be nothing left! */
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700828 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 "beroset-p-663-invalid effective address");
830 result->opcode = -1;
831 return result;
832 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700834 /* It is memory, but it can match any r/m operand */
835 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000836
837 if (b == -1 && (i == -1 || s == 0)) {
838 int is_rel = globalbits == 64 &&
839 !(result->oprs[operand].eaflags & EAF_ABS) &&
840 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000841 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000842 (result->oprs[operand].eaflags & EAF_REL));
843
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700844 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000845 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000846 result->oprs[operand].basereg = b;
847 result->oprs[operand].indexreg = i;
848 result->oprs[operand].scale = s;
849 result->oprs[operand].offset = o;
850 } else { /* it's not a memory reference */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 if (is_just_unknown(value)) { /* it's immediate but unknown */
852 result->oprs[operand].type |= IMMEDIATE;
Victor van den Elzen154e5922009-02-25 17:32:00 +0100853 result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 result->oprs[operand].offset = 0; /* don't care */
855 result->oprs[operand].segment = NO_SEG; /* don't care again */
856 result->oprs[operand].wrt = NO_SEG; /* still don't care */
Victor van den Elzen154e5922009-02-25 17:32:00 +0100857
858 if(optimizing >= 0 && !(result->oprs[operand].type & STRICT))
859 {
860 /* Be optimistic */
861 result->oprs[operand].type |= SBYTE16 | SBYTE32 | SBYTE64;
862 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 } else if (is_reloc(value)) { /* it's immediate */
864 result->oprs[operand].type |= IMMEDIATE;
865 result->oprs[operand].offset = reloc_value(value);
866 result->oprs[operand].segment = reloc_seg(value);
867 result->oprs[operand].wrt = reloc_wrt(value);
868 if (is_simple(value)) {
869 if (reloc_value(value) == 1)
870 result->oprs[operand].type |= UNITY;
871 if (optimizing >= 0 &&
872 !(result->oprs[operand].type & STRICT)) {
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700873 int64_t v64 = reloc_value(value);
874 int32_t v32 = (int32_t)v64;
875 int16_t v16 = (int16_t)v32;
876
877 if (v64 >= -128 && v64 <= 127)
878 result->oprs[operand].type |= SBYTE64;
879 if (v32 >= -128 && v32 <= 127)
880 result->oprs[operand].type |= SBYTE32;
881 if (v16 >= -128 && v16 <= 127)
882 result->oprs[operand].type |= SBYTE16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 }
884 }
885 } else { /* it's a register */
H. Peter Anvin68222142007-11-18 22:18:09 -0800886 unsigned int rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000887
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 if (value->type >= EXPR_SIMPLE || value->value != 1) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700889 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 result->opcode = -1;
891 return result;
892 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 /*
895 * check that its only 1 register, not an expression...
896 */
897 for (i = 1; value[i].type; i++)
898 if (value[i].value) {
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700899 nasm_error(ERR_NONFATAL, "invalid operand type");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 result->opcode = -1;
901 return result;
902 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000903
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 /* clear overrides, except TO which applies to FPU regs */
905 if (result->oprs[operand].type & ~TO) {
906 /*
907 * we want to produce a warning iff the specified size
908 * is different from the register size
909 */
H. Peter Anvin68222142007-11-18 22:18:09 -0800910 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 } else
H. Peter Anvin68222142007-11-18 22:18:09 -0800912 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913
914 result->oprs[operand].type &= TO;
915 result->oprs[operand].type |= REGISTER;
H. Peter Anvina4835d42008-05-20 14:21:29 -0700916 result->oprs[operand].type |= nasm_reg_flags[value->type];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 result->oprs[operand].basereg = value->type;
918
H. Peter Anvin68222142007-11-18 22:18:09 -0800919 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvin00444ae2009-07-18 18:49:55 -0700920 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 "register size specification ignored");
922 }
923 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000924 }
925
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000927
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700928/* clear remaining operands */
929while (operand < MAX_OPERANDS)
930 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000931
932 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -0700933 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000934 */
935 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 case I_RESW:
937 result->opcode = I_RESB;
938 result->oprs[0].offset *= 2;
939 break;
940 case I_RESD:
941 result->opcode = I_RESB;
942 result->oprs[0].offset *= 4;
943 break;
944 case I_RESQ:
945 result->opcode = I_RESB;
946 result->oprs[0].offset *= 8;
947 break;
948 case I_REST:
949 result->opcode = I_RESB;
950 result->oprs[0].offset *= 10;
951 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700952 case I_RESO:
953 result->opcode = I_RESB;
954 result->oprs[0].offset *= 16;
955 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700956 case I_RESY:
957 result->opcode = I_RESB;
958 result->oprs[0].offset *= 32;
959 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700960 default:
961 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000962 }
963
964 return result;
965}
966
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000968{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000969 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000970 int i;
971 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000972
H. Peter Anvin76690a12002-04-30 20:52:49 +0000973 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000975 stdscan_bufptr = p;
976 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;
985 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}