blob: a88e8837285834def370feaa04957dea6fc3ad11 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* parser.c source line parser for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
Beroset095e6a22007-12-29 09:44:23 -05005 * redistributable under the license given in the file "LICENSE"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 * distributed in the NASM archive.
7 *
8 * initial version 27/iii/95 by Simon Tatham
9 */
10
H. Peter Anvinfe501952007-10-02 21:53:51 -070011#include "compiler.h"
12
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000013#include <stdio.h>
14#include <stdlib.h>
15#include <stddef.h>
16#include <string.h>
17#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000018#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000019
20#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000021#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000023#include "stdscan.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000024#include "parser.h"
25#include "float.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070026#include "tables.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000027
H. Peter Anvine2c80182005-01-15 22:15:51 +000028extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000029extern int32_t abs_seg; /* ABSOLUTE segment */
30extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000031
H. Peter Anvine2c80182005-01-15 22:15:51 +000032static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000033
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000034static int i;
35static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000037static struct ofmt *outfmt; /* Structure of addresses of output routines */
H. Peter Anvin12e46512007-10-03 21:30:57 -070038static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000039
H. Peter Anvin12e46512007-10-03 21:30:57 -070040void parser_global_info(struct ofmt *output, struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000041{
42 outfmt = output;
43 location = locp;
44}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000045
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070046static int prefix_slot(enum prefixes prefix)
47{
48 switch (prefix) {
49 case R_CS:
50 case R_DS:
51 case R_SS:
52 case R_ES:
53 case R_FS:
54 case R_GS:
55 return PPS_SEG;
56 case P_LOCK:
57 case P_REP:
58 case P_REPE:
59 case P_REPZ:
60 case P_REPNE:
61 case P_REPNZ:
62 return PPS_LREP;
63 case P_O16:
64 case P_O32:
65 case P_O64:
66 case P_OSP:
67 return PPS_OSIZE;
68 case P_A16:
69 case P_A32:
70 case P_A64:
71 case P_ASP:
72 return PPS_ASIZE;
73 default:
74 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
75 return -1;
76 }
77}
78
79static void process_size_override(insn * result, int operand)
80{
81 if (tasm_compatible_mode) {
82 switch ((int)tokval.t_integer) {
83 /* For TASM compatibility a size override inside the
84 * brackets changes the size of the operand, not the
85 * address type of the operand as it does in standard
86 * NASM syntax. Hence:
87 *
88 * mov eax,[DWORD val]
89 *
90 * is valid syntax in TASM compatibility mode. Note that
91 * you lose the ability to override the default address
92 * type for the instruction, but we never use anything
93 * but 32-bit flat model addressing in our code.
94 */
95 case S_BYTE:
96 result->oprs[operand].type |= BITS8;
97 break;
98 case S_WORD:
99 result->oprs[operand].type |= BITS16;
100 break;
101 case S_DWORD:
102 case S_LONG:
103 result->oprs[operand].type |= BITS32;
104 break;
105 case S_QWORD:
106 result->oprs[operand].type |= BITS64;
107 break;
108 case S_TWORD:
109 result->oprs[operand].type |= BITS80;
110 break;
111 case S_OWORD:
112 result->oprs[operand].type |= BITS128;
113 break;
114 default:
115 error(ERR_NONFATAL,
116 "invalid operand size specification");
117 break;
118 }
119 } else {
120 /* Standard NASM compatible syntax */
121 switch ((int)tokval.t_integer) {
122 case S_NOSPLIT:
123 result->oprs[operand].eaflags |= EAF_TIMESTWO;
124 break;
125 case S_REL:
126 result->oprs[operand].eaflags |= EAF_REL;
127 break;
128 case S_ABS:
129 result->oprs[operand].eaflags |= EAF_ABS;
130 break;
131 case S_BYTE:
132 result->oprs[operand].disp_size = 8;
133 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
134 break;
135 case P_A16:
136 case P_A32:
137 case P_A64:
138 if (result->prefixes[PPS_ASIZE] &&
139 result->prefixes[PPS_ASIZE] != tokval.t_integer)
140 error(ERR_NONFATAL,
141 "conflicting address size specifications");
142 else
143 result->prefixes[PPS_ASIZE] = tokval.t_integer;
144 break;
145 case S_WORD:
146 result->oprs[operand].disp_size = 16;
147 result->oprs[operand].eaflags |= EAF_WORDOFFS;
148 break;
149 case S_DWORD:
150 case S_LONG:
151 result->oprs[operand].disp_size = 32;
152 result->oprs[operand].eaflags |= EAF_WORDOFFS;
153 break;
154 case S_QWORD:
155 result->oprs[operand].disp_size = 64;
156 result->oprs[operand].eaflags |= EAF_WORDOFFS;
157 break;
158 default:
159 error(ERR_NONFATAL, "invalid size specification in"
160 " effective address");
161 break;
162 }
163 }
164}
165
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000166insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000167 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000168{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000169 int operand;
170 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000171 struct eval_hints hints;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700172 int j;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800173 bool first;
174 bool insn_is_label = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000175
H. Peter Anvin9c987692007-11-04 21:09:32 -0800176restart_parse:
177 first = true;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700178 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000179 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
H. Peter Anvin76690a12002-04-30 20:52:49 +0000181 stdscan_reset();
182 stdscan_bufptr = buffer;
183 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
H. Peter Anvine2c80182005-01-15 22:15:51 +0000185 result->label = NULL; /* Assume no label */
186 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000187 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000188
H. Peter Anvine2c80182005-01-15 22:15:51 +0000189 if (i == 0) { /* blank line - ignore */
190 result->opcode = -1; /* and no instruction either */
191 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192 }
193 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvina4835d42008-05-20 14:21:29 -0700194 (i != TOKEN_REG || (REG_SREG & ~nasm_reg_flags[tokval.t_integer]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 error(ERR_NONFATAL, "label or instruction expected"
196 " at start of line");
197 result->opcode = -1;
198 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000199 }
200
H. Peter Anvin9c987692007-11-04 21:09:32 -0800201 if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
202 /* there's a label here */
203 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000204 result->label = tokval.t_charptr;
205 i = stdscan(NULL, &tokval);
206 if (i == ':') { /* skip over the optional colon */
207 i = stdscan(NULL, &tokval);
208 } else if (i == 0) {
209 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
210 "label alone on a line without a colon might be in error");
211 }
212 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
213 /*
214 * FIXME: location->segment could be NO_SEG, in which case
215 * it is possible we should be passing 'abs_seg'. Look into this.
216 * Work out whether that is *really* what we should be doing.
217 * Generally fix things. I think this is right as it is, but
218 * am still not certain.
219 */
220 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700221 location->offset, NULL, true, false, outfmt, errfunc);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000223 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224
H. Peter Anvine2c80182005-01-15 22:15:51 +0000225 if (i == 0) {
226 result->opcode = -1; /* this line contains just a label */
227 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000228 }
229
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700230 for (j = 0; j < MAXPREFIX; j++)
231 result->prefixes[j] = P_none;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000233
234 while (i == TOKEN_PREFIX ||
H. Peter Anvina4835d42008-05-20 14:21:29 -0700235 (i == TOKEN_REG && !(REG_SREG & ~nasm_reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000236 {
H. Peter Anvin9c987692007-11-04 21:09:32 -0800237 first = false;
238
H. Peter Anvine2c80182005-01-15 22:15:51 +0000239 /*
240 * Handle special case: the TIMES prefix.
241 */
242 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
243 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000244
H. Peter Anvine2c80182005-01-15 22:15:51 +0000245 i = stdscan(NULL, &tokval);
246 value =
247 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
248 i = tokval.t_type;
249 if (!value) { /* but, error in evaluator */
250 result->opcode = -1; /* unrecoverable parse error: */
251 return result; /* ignore this instruction */
252 }
253 if (!is_simple(value)) {
254 error(ERR_NONFATAL,
255 "non-constant argument supplied to TIMES");
256 result->times = 1L;
257 } else {
258 result->times = value->value;
259 if (value->value < 0) {
260 error(ERR_NONFATAL, "TIMES value %d is negative",
261 value->value);
262 result->times = 0;
263 }
264 }
265 } else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700266 int slot = prefix_slot(tokval.t_integer);
267 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700268 if (result->prefixes[slot] == tokval.t_integer)
H. Peter Anvin9c987692007-11-04 21:09:32 -0800269 error(ERR_WARNING,
Charles Crayne052c0bd2007-10-29 18:24:59 -0700270 "instruction has redundant prefixes");
271 else
272 error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 "instruction has conflicting prefixes");
274 }
275 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000276 i = stdscan(NULL, &tokval);
277 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000278 }
279
280 if (i != TOKEN_INSN) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700281 int j;
282 enum prefixes pfx;
283
284 for (j = 0; j < MAXPREFIX; j++)
285 if ((pfx = result->prefixes[j]) != P_none)
286 break;
H. Peter Anvincb583b92007-10-28 22:04:42 -0700287
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700288 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000289 /*
290 * Instruction prefixes are present, but no actual
291 * instruction. This is allowed: at this point we
292 * invent a notional instruction of RESB 0.
293 */
294 result->opcode = I_RESB;
295 result->operands = 1;
296 result->oprs[0].type = IMMEDIATE;
297 result->oprs[0].offset = 0L;
298 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
299 return result;
300 } else {
301 error(ERR_NONFATAL, "parser: instruction expected");
302 result->opcode = -1;
303 return result;
304 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000305 }
306
307 result->opcode = tokval.t_integer;
308 result->condition = tokval.t_inttwo;
309
310 /*
311 * RESB, RESW and RESD cannot be satisfied with incorrectly
312 * evaluated operands, since the correct values _must_ be known
313 * on the first pass. Hence, even in pass one, we set the
314 * `critical' flag on calling evaluate(), so that it will bomb
315 * out on undefined symbols. Nasty, but there's nothing we can
316 * do about it.
317 *
318 * For the moment, EQU has the same difficulty, so we'll
319 * include that.
320 */
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700321 if (result->opcode == I_RESB || result->opcode == I_RESW ||
322 result->opcode == I_RESD || result->opcode == I_RESQ ||
323 result->opcode == I_REST || result->opcode == I_RESO ||
H. Peter Anvindfb91802008-05-20 11:43:53 -0700324 result->opcode == I_RESY ||
Charles Craynecd341802008-06-04 15:53:21 -0700325 result->opcode == I_INCBIN) {
Charles Crayne5a7976c2008-03-26 17:20:21 -0700326 critical = (pass0 < 2 ? 1 : 2);
327
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 } else
329 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000330
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700331 if (result->opcode == I_DB || result->opcode == I_DW ||
332 result->opcode == I_DD || result->opcode == I_DQ ||
333 result->opcode == I_DT || result->opcode == I_DO ||
H. Peter Anvindfb91802008-05-20 11:43:53 -0700334 result->opcode == I_DY || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000335 extop *eop, **tail = &result->eops, **fixptr;
336 int oper_num = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700337 int32_t sign;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000338
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700339 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000340
H. Peter Anvine2c80182005-01-15 22:15:51 +0000341 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700342 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000343 */
344 while (1) {
345 i = stdscan(NULL, &tokval);
346 if (i == 0)
347 break;
H. Peter Anvin9c987692007-11-04 21:09:32 -0800348 else if (first && i == ':') {
349 insn_is_label = true;
350 goto restart_parse;
351 }
352 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 fixptr = tail;
354 eop = *tail = nasm_malloc(sizeof(extop));
355 tail = &eop->next;
356 eop->next = NULL;
357 eop->type = EOT_NOTHING;
358 oper_num++;
H. Peter Anvin518df302008-06-14 16:53:48 -0700359 sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000360
H. Peter Anvin518df302008-06-14 16:53:48 -0700361 /* is_comma_next() here is to distinguish this from
362 a string used as part of an expression... */
H. Peter Anvin11627042008-06-09 20:45:19 -0700363 if (i == TOKEN_STR && is_comma_next()) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 eop->type = EOT_DB_STRING;
365 eop->stringval = tokval.t_charptr;
366 eop->stringlen = tokval.t_inttwo;
367 i = stdscan(NULL, &tokval); /* eat the comma */
H. Peter Anvin518df302008-06-14 16:53:48 -0700368 } else if (i == TOKEN_STRFUNC) {
369 bool parens = false;
370 const char *funcname = tokval.t_charptr;
371 enum strfunc func = tokval.t_integer;
372 i = stdscan(NULL, &tokval);
373 if (i == '(') {
374 parens = true;
375 i = stdscan(NULL, &tokval);
376 }
377 if (i != TOKEN_STR) {
378 error(ERR_NONFATAL,
379 "%s must be followed by a string constant",
380 funcname);
381 eop->type = EOT_NOTHING;
382 } else {
383 eop->type = EOT_DB_STRING_FREE;
384 eop->stringlen =
385 string_transform(tokval.t_charptr, tokval.t_inttwo,
386 &eop->stringval, func);
387 if (eop->stringlen == (size_t)-1) {
388 error(ERR_NONFATAL, "invalid string for transform");
389 eop->type = EOT_NOTHING;
390 }
391 }
392 if (parens && i && i != ')') {
393 i = stdscan(NULL, &tokval);
394 if (i != ')') {
395 error(ERR_NONFATAL, "unterminated %s function",
396 funcname);
397 }
398 }
399 if (i && i != ',')
400 i = stdscan(NULL, &tokval);
401 } else if (i == '-' || i == '+') {
402 char *save = stdscan_bufptr;
403 int token = i;
404 sign = (i == '-') ? -1 : 1;
405 i = stdscan(NULL, &tokval);
406 if (i != TOKEN_FLOAT) {
407 stdscan_bufptr = save;
408 i = tokval.t_type = token;
409 goto is_expression;
410 } else {
411 goto is_float;
412 }
413 } else if (i == TOKEN_FLOAT) {
414 is_float:
415 eop->type = EOT_DB_STRING;
416 result->eops_float = true;
417 switch (result->opcode) {
418 case I_DB:
419 eop->stringlen = 1;
420 break;
421 case I_DW:
422 eop->stringlen = 2;
423 break;
424 case I_DD:
425 eop->stringlen = 4;
426 break;
427 case I_DQ:
428 eop->stringlen = 8;
429 break;
430 case I_DT:
431 eop->stringlen = 10;
432 break;
433 case I_DO:
434 eop->stringlen = 16;
435 break;
436 case I_DY:
437 error(ERR_NONFATAL, "floating-point constant"
438 " encountered in DY instruction");
439 eop->stringlen = 0;
440 break;
441 default:
442 error(ERR_NONFATAL, "floating-point constant"
443 " encountered in unknown instruction");
444 /*
445 * fix suggested by Pedro Gimeno... original line
446 * was:
447 * eop->type = EOT_NOTHING;
448 */
449 eop->stringlen = 0;
450 break;
451 }
452 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
453 tail = &eop->next;
454 *fixptr = eop;
455 eop->stringval = (char *)eop + sizeof(extop);
456 if (!eop->stringlen ||
457 !float_const(tokval.t_charptr, sign,
458 (uint8_t *)eop->stringval,
459 eop->stringlen, error))
460 eop->type = EOT_NOTHING;
461 i = stdscan(NULL, &tokval); /* eat the comma */
462 } else {
463 /* anything else, assume it is an expression */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000464 expr *value;
H. Peter Anvin518df302008-06-14 16:53:48 -0700465
466 is_expression:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 value = evaluate(stdscan, NULL, &tokval, NULL,
468 critical, error, NULL);
469 i = tokval.t_type;
470 if (!value) { /* error in evaluator */
471 result->opcode = -1; /* unrecoverable parse error: */
472 return result; /* ignore this instruction */
473 }
474 if (is_unknown(value)) {
475 eop->type = EOT_DB_NUMBER;
476 eop->offset = 0; /* doesn't matter what we put */
477 eop->segment = eop->wrt = NO_SEG; /* likewise */
478 } else if (is_reloc(value)) {
479 eop->type = EOT_DB_NUMBER;
480 eop->offset = reloc_value(value);
481 eop->segment = reloc_seg(value);
482 eop->wrt = reloc_wrt(value);
483 } else {
484 error(ERR_NONFATAL,
485 "operand %d: expression is not simple"
486 " or relocatable", oper_num);
487 }
488 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000489
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 /*
491 * We're about to call stdscan(), which will eat the
492 * comma that we're currently sitting on between
493 * arguments. However, we'd better check first that it
494 * _is_ a comma.
495 */
496 if (i == 0) /* also could be EOL */
497 break;
498 if (i != ',') {
499 error(ERR_NONFATAL, "comma expected after operand %d",
500 oper_num);
501 result->opcode = -1; /* unrecoverable parse error: */
502 return result; /* ignore this instruction */
503 }
504 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000505
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 if (result->opcode == I_INCBIN) {
507 /*
508 * Correct syntax for INCBIN is that there should be
509 * one string operand, followed by one or two numeric
510 * operands.
511 */
512 if (!result->eops || result->eops->type != EOT_DB_STRING)
513 error(ERR_NONFATAL, "`incbin' expects a file name");
514 else if (result->eops->next &&
515 result->eops->next->type != EOT_DB_NUMBER)
516 error(ERR_NONFATAL, "`incbin': second parameter is",
517 " non-numeric");
518 else if (result->eops->next && result->eops->next->next &&
519 result->eops->next->next->type != EOT_DB_NUMBER)
520 error(ERR_NONFATAL, "`incbin': third parameter is",
521 " non-numeric");
522 else if (result->eops->next && result->eops->next->next &&
523 result->eops->next->next->next)
524 error(ERR_NONFATAL,
525 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527 return result;
528 /*
529 * If we reach here, one of the above errors happened.
530 * Throw the instruction away.
531 */
532 result->opcode = -1;
533 return result;
534 } else /* DB ... */ if (oper_num == 0)
535 error(ERR_WARNING | ERR_PASS1,
536 "no operand for data declaration");
537 else
538 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000539
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000541 }
542
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700543 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000544 * of these, separated by commas, and terminated by a zero token. */
545
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700546 for (operand = 0; operand < MAX_OPERANDS; operand++) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700547 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 int mref; /* is this going to be a memory ref? */
549 int bracket; /* is it a [] mref, or a & mref? */
550 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000551
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700552 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 result->oprs[operand].eaflags = 0; /* and this */
554 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000555
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 i = stdscan(NULL, &tokval);
557 if (i == 0)
558 break; /* end of operands: get out of here */
H. Peter Anvin9c987692007-11-04 21:09:32 -0800559 else if (first && i == ':') {
560 insn_is_label = true;
561 goto restart_parse;
562 }
563 first = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 result->oprs[operand].type = 0; /* so far, no override */
565 while (i == TOKEN_SPECIAL) { /* size specifiers */
566 switch ((int)tokval.t_integer) {
567 case S_BYTE:
568 if (!setsize) /* we want to use only the first */
569 result->oprs[operand].type |= BITS8;
570 setsize = 1;
571 break;
572 case S_WORD:
573 if (!setsize)
574 result->oprs[operand].type |= BITS16;
575 setsize = 1;
576 break;
577 case S_DWORD:
578 case S_LONG:
579 if (!setsize)
580 result->oprs[operand].type |= BITS32;
581 setsize = 1;
582 break;
583 case S_QWORD:
584 if (!setsize)
585 result->oprs[operand].type |= BITS64;
586 setsize = 1;
587 break;
588 case S_TWORD:
589 if (!setsize)
590 result->oprs[operand].type |= BITS80;
591 setsize = 1;
592 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700593 case S_OWORD:
594 if (!setsize)
595 result->oprs[operand].type |= BITS128;
596 setsize = 1;
597 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700598 case S_YWORD:
599 if (!setsize)
600 result->oprs[operand].type |= BITS256;
601 setsize = 1;
602 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 case S_TO:
604 result->oprs[operand].type |= TO;
605 break;
606 case S_STRICT:
607 result->oprs[operand].type |= STRICT;
608 break;
609 case S_FAR:
610 result->oprs[operand].type |= FAR;
611 break;
612 case S_NEAR:
613 result->oprs[operand].type |= NEAR;
614 break;
615 case S_SHORT:
616 result->oprs[operand].type |= SHORT;
617 break;
618 default:
619 error(ERR_NONFATAL, "invalid operand size specification");
620 }
621 i = stdscan(NULL, &tokval);
622 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000623
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700625 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700627 i = stdscan(NULL, &tokval); /* then skip the colon */
628 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
629 process_size_override(result, operand);
630 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 }
632 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700633 mref = false;
634 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000636
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 if ((result->oprs[operand].type & FAR) && !mref &&
638 result->opcode != I_JMP && result->opcode != I_CALL) {
639 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
640 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000641
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 value = evaluate(stdscan, NULL, &tokval,
643 &result->oprs[operand].opflags,
644 critical, error, &hints);
645 i = tokval.t_type;
646 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700647 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 }
649 if (!value) { /* error in evaluator */
650 result->opcode = -1; /* unrecoverable parse error: */
651 return result; /* ignore this instruction */
652 }
653 if (i == ':' && mref) { /* it was seg:offset */
654 /*
655 * Process the segment override.
656 */
657 if (value[1].type != 0 || value->value != 1 ||
H. Peter Anvina4835d42008-05-20 14:21:29 -0700658 REG_SREG & ~nasm_reg_flags[value->type])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700660 else if (result->prefixes[PPS_SEG])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700662 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000663 else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700664 result->prefixes[PPS_SEG] = value->type;
H. Peter Anvina4835d42008-05-20 14:21:29 -0700665 if (!(REG_FSGS & ~nasm_reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000666 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000667 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000668
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700670 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
671 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 i = stdscan(NULL, &tokval);
673 }
674 value = evaluate(stdscan, NULL, &tokval,
675 &result->oprs[operand].opflags,
676 critical, error, &hints);
677 i = tokval.t_type;
678 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700679 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 }
681 /* and get the offset */
682 if (!value) { /* but, error in evaluator */
683 result->opcode = -1; /* unrecoverable parse error: */
684 return result; /* ignore this instruction */
685 }
686 }
687 if (mref && bracket) { /* find ] at the end */
688 if (i != ']') {
689 error(ERR_NONFATAL, "parser: expecting ]");
690 do { /* error recovery again */
691 i = stdscan(NULL, &tokval);
692 } while (i != 0 && i != ',');
693 } else /* we got the required ] */
694 i = stdscan(NULL, &tokval);
695 } else { /* immediate operand */
696 if (i != 0 && i != ',' && i != ':') {
697 error(ERR_NONFATAL, "comma or end of line expected");
698 do { /* error recovery */
699 i = stdscan(NULL, &tokval);
700 } while (i != 0 && i != ',');
701 } else if (i == ':') {
702 result->oprs[operand].type |= COLON;
703 }
704 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000705
H. Peter Anvine2c80182005-01-15 22:15:51 +0000706 /* now convert the exprs returned from evaluate() into operand
707 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000708
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 if (mref) { /* it's a memory reference */
710 expr *e = value;
711 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000712 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000713
H. Peter Anvine2c80182005-01-15 22:15:51 +0000714 b = i = -1, o = s = 0;
715 result->oprs[operand].hintbase = hints.base;
716 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000717
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
719 if (e->value == 1) /* in fact it can be basereg */
720 b = e->type;
721 else /* no, it has to be indexreg */
722 i = e->type, s = e->value;
723 e++;
724 }
725 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
726 if (b != -1) /* If the first was the base, ... */
727 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000728
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 else if (e->value != 1) { /* If both want to be index */
730 error(ERR_NONFATAL,
731 "beroset-p-592-invalid effective address");
732 result->opcode = -1;
733 return result;
734 } else
735 b = e->type;
736 e++;
737 }
738 if (e->type != 0) { /* is there an offset? */
739 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
740 error(ERR_NONFATAL,
741 "beroset-p-603-invalid effective address");
742 result->opcode = -1;
743 return result;
744 } else {
745 if (e->type == EXPR_UNKNOWN) {
746 o = 0; /* doesn't matter what */
747 result->oprs[operand].wrt = NO_SEG; /* nor this */
748 result->oprs[operand].segment = NO_SEG; /* or this */
749 while (e->type)
750 e++; /* go to the end of the line */
751 } else {
752 if (e->type == EXPR_SIMPLE) {
753 o = e->value;
754 e++;
755 }
756 if (e->type == EXPR_WRT) {
757 result->oprs[operand].wrt = e->value;
758 e++;
759 } else
760 result->oprs[operand].wrt = NO_SEG;
761 /*
762 * Look for a segment base type.
763 */
764 if (e->type && e->type < EXPR_SEGBASE) {
765 error(ERR_NONFATAL,
766 "beroset-p-630-invalid effective address");
767 result->opcode = -1;
768 return result;
769 }
770 while (e->type && e->value == 0)
771 e++;
772 if (e->type && e->value != 1) {
773 error(ERR_NONFATAL,
774 "beroset-p-637-invalid effective address");
775 result->opcode = -1;
776 return result;
777 }
778 if (e->type) {
779 result->oprs[operand].segment =
780 e->type - EXPR_SEGBASE;
781 e++;
782 } else
783 result->oprs[operand].segment = NO_SEG;
784 while (e->type && e->value == 0)
785 e++;
786 if (e->type) {
787 error(ERR_NONFATAL,
788 "beroset-p-650-invalid effective address");
789 result->opcode = -1;
790 return result;
791 }
792 }
793 }
794 } else {
795 o = 0;
796 result->oprs[operand].wrt = NO_SEG;
797 result->oprs[operand].segment = NO_SEG;
798 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 if (e->type != 0) { /* there'd better be nothing left! */
801 error(ERR_NONFATAL,
802 "beroset-p-663-invalid effective address");
803 result->opcode = -1;
804 return result;
805 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000806
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700807 /* It is memory, but it can match any r/m operand */
808 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000809
810 if (b == -1 && (i == -1 || s == 0)) {
811 int is_rel = globalbits == 64 &&
812 !(result->oprs[operand].eaflags & EAF_ABS) &&
813 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000814 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000815 (result->oprs[operand].eaflags & EAF_REL));
816
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700817 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000818 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 result->oprs[operand].basereg = b;
820 result->oprs[operand].indexreg = i;
821 result->oprs[operand].scale = s;
822 result->oprs[operand].offset = o;
823 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000824
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 if (is_just_unknown(value)) { /* it's immediate but unknown */
826 result->oprs[operand].type |= IMMEDIATE;
827 result->oprs[operand].offset = 0; /* don't care */
828 result->oprs[operand].segment = NO_SEG; /* don't care again */
829 result->oprs[operand].wrt = NO_SEG; /* still don't care */
830 } else if (is_reloc(value)) { /* it's immediate */
831 result->oprs[operand].type |= IMMEDIATE;
832 result->oprs[operand].offset = reloc_value(value);
833 result->oprs[operand].segment = reloc_seg(value);
834 result->oprs[operand].wrt = reloc_wrt(value);
835 if (is_simple(value)) {
836 if (reloc_value(value) == 1)
837 result->oprs[operand].type |= UNITY;
838 if (optimizing >= 0 &&
839 !(result->oprs[operand].type & STRICT)) {
H. Peter Anvin32cd4c22008-04-04 13:34:53 -0700840 int64_t v64 = reloc_value(value);
841 int32_t v32 = (int32_t)v64;
842 int16_t v16 = (int16_t)v32;
843
844 if (v64 >= -128 && v64 <= 127)
845 result->oprs[operand].type |= SBYTE64;
846 if (v32 >= -128 && v32 <= 127)
847 result->oprs[operand].type |= SBYTE32;
848 if (v16 >= -128 && v16 <= 127)
849 result->oprs[operand].type |= SBYTE16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 }
851 }
852 } else { /* it's a register */
H. Peter Anvin68222142007-11-18 22:18:09 -0800853 unsigned int rs;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000854
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 if (value->type >= EXPR_SIMPLE || value->value != 1) {
856 error(ERR_NONFATAL, "invalid operand type");
857 result->opcode = -1;
858 return result;
859 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000860
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 /*
862 * check that its only 1 register, not an expression...
863 */
864 for (i = 1; value[i].type; i++)
865 if (value[i].value) {
866 error(ERR_NONFATAL, "invalid operand type");
867 result->opcode = -1;
868 return result;
869 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000870
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 /* clear overrides, except TO which applies to FPU regs */
872 if (result->oprs[operand].type & ~TO) {
873 /*
874 * we want to produce a warning iff the specified size
875 * is different from the register size
876 */
H. Peter Anvin68222142007-11-18 22:18:09 -0800877 rs = result->oprs[operand].type & SIZE_MASK;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000878 } else
H. Peter Anvin68222142007-11-18 22:18:09 -0800879 rs = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880
881 result->oprs[operand].type &= TO;
882 result->oprs[operand].type |= REGISTER;
H. Peter Anvina4835d42008-05-20 14:21:29 -0700883 result->oprs[operand].type |= nasm_reg_flags[value->type];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 result->oprs[operand].basereg = value->type;
885
H. Peter Anvin68222142007-11-18 22:18:09 -0800886 if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 error(ERR_WARNING | ERR_PASS1,
888 "register size specification ignored");
889 }
890 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000891 }
892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000894
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700895/* clear remaining operands */
896while (operand < MAX_OPERANDS)
897 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000898
899 /*
H. Peter Anvindfb91802008-05-20 11:43:53 -0700900 * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000901 */
902 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 case I_RESW:
904 result->opcode = I_RESB;
905 result->oprs[0].offset *= 2;
906 break;
907 case I_RESD:
908 result->opcode = I_RESB;
909 result->oprs[0].offset *= 4;
910 break;
911 case I_RESQ:
912 result->opcode = I_RESB;
913 result->oprs[0].offset *= 8;
914 break;
915 case I_REST:
916 result->opcode = I_RESB;
917 result->oprs[0].offset *= 10;
918 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700919 case I_RESO:
920 result->opcode = I_RESB;
921 result->oprs[0].offset *= 16;
922 break;
H. Peter Anvindfb91802008-05-20 11:43:53 -0700923 case I_RESY:
924 result->opcode = I_RESB;
925 result->oprs[0].offset *= 32;
926 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700927 default:
928 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000929 }
930
931 return result;
932}
933
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000935{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000936 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000937 int i;
938 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000939
H. Peter Anvin76690a12002-04-30 20:52:49 +0000940 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000942 stdscan_bufptr = p;
943 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000944}
945
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000947{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000948 extop *e;
949
H. Peter Anvin2aa77392008-06-15 17:39:45 -0700950 while ((e = i->eops)) {
951 i->eops = e->next;
952 if (e->type == EOT_DB_STRING_FREE)
953 nasm_free(e->stringval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000955 }
956}