blob: 9b7f61760dc1eb3265dfbbbcdc993b9a7fe9f6b1 [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
5 * redistributable under the licence given in the file "Licence"
6 * 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"
26
H. Peter Anvine2c80182005-01-15 22:15:51 +000027extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000028extern int32_t abs_seg; /* ABSOLUTE segment */
29extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000030
H. Peter Anvine2c80182005-01-15 22:15:51 +000031#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000032
H. Peter Anvine2c80182005-01-15 22:15:51 +000033static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000034
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035static int i;
36static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000037static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000038static struct ofmt *outfmt; /* Structure of addresses of output routines */
H. Peter Anvin12e46512007-10-03 21:30:57 -070039static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000040
H. Peter Anvin12e46512007-10-03 21:30:57 -070041void parser_global_info(struct ofmt *output, struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000042{
43 outfmt = output;
44 location = locp;
45}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000046
H. Peter Anvinde4b89b2007-10-01 15:41:25 -070047static int prefix_slot(enum prefixes prefix)
48{
49 switch (prefix) {
50 case R_CS:
51 case R_DS:
52 case R_SS:
53 case R_ES:
54 case R_FS:
55 case R_GS:
56 return PPS_SEG;
57 case P_LOCK:
58 case P_REP:
59 case P_REPE:
60 case P_REPZ:
61 case P_REPNE:
62 case P_REPNZ:
63 return PPS_LREP;
64 case P_O16:
65 case P_O32:
66 case P_O64:
67 case P_OSP:
68 return PPS_OSIZE;
69 case P_A16:
70 case P_A32:
71 case P_A64:
72 case P_ASP:
73 return PPS_ASIZE;
74 default:
75 error(ERR_PANIC, "Invalid value %d passed to prefix_slot()", prefix);
76 return -1;
77 }
78}
79
80static void process_size_override(insn * result, int operand)
81{
82 if (tasm_compatible_mode) {
83 switch ((int)tokval.t_integer) {
84 /* For TASM compatibility a size override inside the
85 * brackets changes the size of the operand, not the
86 * address type of the operand as it does in standard
87 * NASM syntax. Hence:
88 *
89 * mov eax,[DWORD val]
90 *
91 * is valid syntax in TASM compatibility mode. Note that
92 * you lose the ability to override the default address
93 * type for the instruction, but we never use anything
94 * but 32-bit flat model addressing in our code.
95 */
96 case S_BYTE:
97 result->oprs[operand].type |= BITS8;
98 break;
99 case S_WORD:
100 result->oprs[operand].type |= BITS16;
101 break;
102 case S_DWORD:
103 case S_LONG:
104 result->oprs[operand].type |= BITS32;
105 break;
106 case S_QWORD:
107 result->oprs[operand].type |= BITS64;
108 break;
109 case S_TWORD:
110 result->oprs[operand].type |= BITS80;
111 break;
112 case S_OWORD:
113 result->oprs[operand].type |= BITS128;
114 break;
115 default:
116 error(ERR_NONFATAL,
117 "invalid operand size specification");
118 break;
119 }
120 } else {
121 /* Standard NASM compatible syntax */
122 switch ((int)tokval.t_integer) {
123 case S_NOSPLIT:
124 result->oprs[operand].eaflags |= EAF_TIMESTWO;
125 break;
126 case S_REL:
127 result->oprs[operand].eaflags |= EAF_REL;
128 break;
129 case S_ABS:
130 result->oprs[operand].eaflags |= EAF_ABS;
131 break;
132 case S_BYTE:
133 result->oprs[operand].disp_size = 8;
134 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
135 break;
136 case P_A16:
137 case P_A32:
138 case P_A64:
139 if (result->prefixes[PPS_ASIZE] &&
140 result->prefixes[PPS_ASIZE] != tokval.t_integer)
141 error(ERR_NONFATAL,
142 "conflicting address size specifications");
143 else
144 result->prefixes[PPS_ASIZE] = tokval.t_integer;
145 break;
146 case S_WORD:
147 result->oprs[operand].disp_size = 16;
148 result->oprs[operand].eaflags |= EAF_WORDOFFS;
149 break;
150 case S_DWORD:
151 case S_LONG:
152 result->oprs[operand].disp_size = 32;
153 result->oprs[operand].eaflags |= EAF_WORDOFFS;
154 break;
155 case S_QWORD:
156 result->oprs[operand].disp_size = 64;
157 result->oprs[operand].eaflags |= EAF_WORDOFFS;
158 break;
159 default:
160 error(ERR_NONFATAL, "invalid size specification in"
161 " effective address");
162 break;
163 }
164 }
165}
166
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000167insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000168 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000169{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000170 int operand;
171 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000172 struct eval_hints hints;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700173 int j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000174
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700175 result->forw_ref = false;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000177
H. Peter Anvin76690a12002-04-30 20:52:49 +0000178 stdscan_reset();
179 stdscan_bufptr = buffer;
180 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000181
H. Peter Anvine2c80182005-01-15 22:15:51 +0000182 result->label = NULL; /* Assume no label */
183 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000184 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000185
H. Peter Anvine2c80182005-01-15 22:15:51 +0000186 if (i == 0) { /* blank line - ignore */
187 result->opcode = -1; /* and no instruction either */
188 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189 }
190 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000191 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
192 error(ERR_NONFATAL, "label or instruction expected"
193 " at start of line");
194 result->opcode = -1;
195 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000196 }
197
H. Peter Anvine2c80182005-01-15 22:15:51 +0000198 if (i == TOKEN_ID) { /* there's a label here */
199 result->label = tokval.t_charptr;
200 i = stdscan(NULL, &tokval);
201 if (i == ':') { /* skip over the optional colon */
202 i = stdscan(NULL, &tokval);
203 } else if (i == 0) {
204 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
205 "label alone on a line without a colon might be in error");
206 }
207 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
208 /*
209 * FIXME: location->segment could be NO_SEG, in which case
210 * it is possible we should be passing 'abs_seg'. Look into this.
211 * Work out whether that is *really* what we should be doing.
212 * Generally fix things. I think this is right as it is, but
213 * am still not certain.
214 */
215 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700216 location->offset, NULL, true, false, outfmt, errfunc);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000218 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000219
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 if (i == 0) {
221 result->opcode = -1; /* this line contains just a label */
222 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000223 }
224
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700225 for (j = 0; j < MAXPREFIX; j++)
226 result->prefixes[j] = P_none;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000227 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000228
229 while (i == TOKEN_PREFIX ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000231 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232 /*
233 * Handle special case: the TIMES prefix.
234 */
235 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
236 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000237
H. Peter Anvine2c80182005-01-15 22:15:51 +0000238 i = stdscan(NULL, &tokval);
239 value =
240 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
241 i = tokval.t_type;
242 if (!value) { /* but, error in evaluator */
243 result->opcode = -1; /* unrecoverable parse error: */
244 return result; /* ignore this instruction */
245 }
246 if (!is_simple(value)) {
247 error(ERR_NONFATAL,
248 "non-constant argument supplied to TIMES");
249 result->times = 1L;
250 } else {
251 result->times = value->value;
252 if (value->value < 0) {
253 error(ERR_NONFATAL, "TIMES value %d is negative",
254 value->value);
255 result->times = 0;
256 }
257 }
258 } else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700259 int slot = prefix_slot(tokval.t_integer);
260 if (result->prefixes[slot]) {
Charles Crayne052c0bd2007-10-29 18:24:59 -0700261 if (result->prefixes[slot] == tokval.t_integer)
262 error(ERR_WARNING,
263 "instruction has redundant prefixes");
264 else
265 error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700266 "instruction has conflicting prefixes");
267 }
268 result->prefixes[slot] = tokval.t_integer;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269 i = stdscan(NULL, &tokval);
270 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000271 }
272
273 if (i != TOKEN_INSN) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 int j;
275 enum prefixes pfx;
276
277 for (j = 0; j < MAXPREFIX; j++)
278 if ((pfx = result->prefixes[j]) != P_none)
279 break;
H. Peter Anvincb583b92007-10-28 22:04:42 -0700280
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700281 if (i == 0 && pfx != P_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000282 /*
283 * Instruction prefixes are present, but no actual
284 * instruction. This is allowed: at this point we
285 * invent a notional instruction of RESB 0.
286 */
287 result->opcode = I_RESB;
288 result->operands = 1;
289 result->oprs[0].type = IMMEDIATE;
290 result->oprs[0].offset = 0L;
291 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
292 return result;
293 } else {
294 error(ERR_NONFATAL, "parser: instruction expected");
295 result->opcode = -1;
296 return result;
297 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000298 }
299
300 result->opcode = tokval.t_integer;
301 result->condition = tokval.t_inttwo;
302
303 /*
304 * RESB, RESW and RESD cannot be satisfied with incorrectly
305 * evaluated operands, since the correct values _must_ be known
306 * on the first pass. Hence, even in pass one, we set the
307 * `critical' flag on calling evaluate(), so that it will bomb
308 * out on undefined symbols. Nasty, but there's nothing we can
309 * do about it.
310 *
311 * For the moment, EQU has the same difficulty, so we'll
312 * include that.
313 */
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700314 if (result->opcode == I_RESB || result->opcode == I_RESW ||
315 result->opcode == I_RESD || result->opcode == I_RESQ ||
316 result->opcode == I_REST || result->opcode == I_RESO ||
317 result->opcode == I_EQU || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000318 critical = pass0;
319 } else
320 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000321
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700322 if (result->opcode == I_DB || result->opcode == I_DW ||
323 result->opcode == I_DD || result->opcode == I_DQ ||
324 result->opcode == I_DT || result->opcode == I_DO ||
325 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326 extop *eop, **tail = &result->eops, **fixptr;
327 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000328
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700329 result->eops_float = false;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000330
H. Peter Anvine2c80182005-01-15 22:15:51 +0000331 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700332 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333 */
334 while (1) {
335 i = stdscan(NULL, &tokval);
336 if (i == 0)
337 break;
338 fixptr = tail;
339 eop = *tail = nasm_malloc(sizeof(extop));
340 tail = &eop->next;
341 eop->next = NULL;
342 eop->type = EOT_NOTHING;
343 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000344
H. Peter Anvine2c80182005-01-15 22:15:51 +0000345 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
346 eop->type = EOT_DB_STRING;
347 eop->stringval = tokval.t_charptr;
348 eop->stringlen = tokval.t_inttwo;
349 i = stdscan(NULL, &tokval); /* eat the comma */
350 continue;
351 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000352
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700353 if ((i == TOKEN_FLOAT && is_comma_next())
354 || i == '-' || i == '+') {
355 int32_t sign = +1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000356
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700357 if (i == '+' || i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000358 char *save = stdscan_bufptr;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700359 int token = i;
360 sign = (i == '-') ? -1 : 1;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000361 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000362 if (i != TOKEN_FLOAT || !is_comma_next()) {
363 stdscan_bufptr = save;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700364 i = tokval.t_type = token;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000365 }
366 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000367
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368 if (i == TOKEN_FLOAT) {
369 eop->type = EOT_DB_STRING;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700370 result->eops_float = true;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700371 switch (result->opcode) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700372 case I_DB:
373 eop->stringlen = 1;
374 break;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700375 case I_DW:
376 eop->stringlen = 2;
377 break;
378 case I_DD:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 eop->stringlen = 4;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700380 break;
381 case I_DQ:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382 eop->stringlen = 8;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700383 break;
384 case I_DT:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 eop->stringlen = 10;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700386 break;
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700387 case I_DO:
388 eop->stringlen = 16;
389 break;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700390 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391 error(ERR_NONFATAL, "floating-point constant"
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700392 " encountered in unknown instruction");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 /*
394 * fix suggested by Pedro Gimeno... original line
395 * was:
396 * eop->type = EOT_NOTHING;
397 */
398 eop->stringlen = 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700399 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 }
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700401 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 tail = &eop->next;
403 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000404 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700405 if (!eop->stringlen ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000407 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 eop->stringlen, error))
409 eop->type = EOT_NOTHING;
410 i = stdscan(NULL, &tokval); /* eat the comma */
411 continue;
412 }
413 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000414
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 /* anything else */
416 {
417 expr *value;
418 value = evaluate(stdscan, NULL, &tokval, NULL,
419 critical, error, NULL);
420 i = tokval.t_type;
421 if (!value) { /* error in evaluator */
422 result->opcode = -1; /* unrecoverable parse error: */
423 return result; /* ignore this instruction */
424 }
425 if (is_unknown(value)) {
426 eop->type = EOT_DB_NUMBER;
427 eop->offset = 0; /* doesn't matter what we put */
428 eop->segment = eop->wrt = NO_SEG; /* likewise */
429 } else if (is_reloc(value)) {
430 eop->type = EOT_DB_NUMBER;
431 eop->offset = reloc_value(value);
432 eop->segment = reloc_seg(value);
433 eop->wrt = reloc_wrt(value);
434 } else {
435 error(ERR_NONFATAL,
436 "operand %d: expression is not simple"
437 " or relocatable", oper_num);
438 }
439 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000440
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 /*
442 * We're about to call stdscan(), which will eat the
443 * comma that we're currently sitting on between
444 * arguments. However, we'd better check first that it
445 * _is_ a comma.
446 */
447 if (i == 0) /* also could be EOL */
448 break;
449 if (i != ',') {
450 error(ERR_NONFATAL, "comma expected after operand %d",
451 oper_num);
452 result->opcode = -1; /* unrecoverable parse error: */
453 return result; /* ignore this instruction */
454 }
455 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000456
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 if (result->opcode == I_INCBIN) {
458 /*
459 * Correct syntax for INCBIN is that there should be
460 * one string operand, followed by one or two numeric
461 * operands.
462 */
463 if (!result->eops || result->eops->type != EOT_DB_STRING)
464 error(ERR_NONFATAL, "`incbin' expects a file name");
465 else if (result->eops->next &&
466 result->eops->next->type != EOT_DB_NUMBER)
467 error(ERR_NONFATAL, "`incbin': second parameter is",
468 " non-numeric");
469 else if (result->eops->next && result->eops->next->next &&
470 result->eops->next->next->type != EOT_DB_NUMBER)
471 error(ERR_NONFATAL, "`incbin': third parameter is",
472 " non-numeric");
473 else if (result->eops->next && result->eops->next->next &&
474 result->eops->next->next->next)
475 error(ERR_NONFATAL,
476 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 return result;
479 /*
480 * If we reach here, one of the above errors happened.
481 * Throw the instruction away.
482 */
483 result->opcode = -1;
484 return result;
485 } else /* DB ... */ if (oper_num == 0)
486 error(ERR_WARNING | ERR_PASS1,
487 "no operand for data declaration");
488 else
489 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000492 }
493
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700494 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000495 * of these, separated by commas, and terminated by a zero token. */
496
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700497 for (operand = 0; operand < MAX_OPERANDS; operand++) {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700498 expr *value; /* used most of the time */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 int mref; /* is this going to be a memory ref? */
500 int bracket; /* is it a [] mref, or a & mref? */
501 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000502
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700503 result->oprs[operand].disp_size = 0; /* have to zero this whatever */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 result->oprs[operand].eaflags = 0; /* and this */
505 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000506
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 i = stdscan(NULL, &tokval);
508 if (i == 0)
509 break; /* end of operands: get out of here */
510 result->oprs[operand].type = 0; /* so far, no override */
511 while (i == TOKEN_SPECIAL) { /* size specifiers */
512 switch ((int)tokval.t_integer) {
513 case S_BYTE:
514 if (!setsize) /* we want to use only the first */
515 result->oprs[operand].type |= BITS8;
516 setsize = 1;
517 break;
518 case S_WORD:
519 if (!setsize)
520 result->oprs[operand].type |= BITS16;
521 setsize = 1;
522 break;
523 case S_DWORD:
524 case S_LONG:
525 if (!setsize)
526 result->oprs[operand].type |= BITS32;
527 setsize = 1;
528 break;
529 case S_QWORD:
530 if (!setsize)
531 result->oprs[operand].type |= BITS64;
532 setsize = 1;
533 break;
534 case S_TWORD:
535 if (!setsize)
536 result->oprs[operand].type |= BITS80;
537 setsize = 1;
538 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700539 case S_OWORD:
540 if (!setsize)
541 result->oprs[operand].type |= BITS128;
542 setsize = 1;
543 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 case S_TO:
545 result->oprs[operand].type |= TO;
546 break;
547 case S_STRICT:
548 result->oprs[operand].type |= STRICT;
549 break;
550 case S_FAR:
551 result->oprs[operand].type |= FAR;
552 break;
553 case S_NEAR:
554 result->oprs[operand].type |= NEAR;
555 break;
556 case S_SHORT:
557 result->oprs[operand].type |= SHORT;
558 break;
559 default:
560 error(ERR_NONFATAL, "invalid operand size specification");
561 }
562 i = stdscan(NULL, &tokval);
563 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000564
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 if (i == '[' || i == '&') { /* memory reference */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700566 mref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 bracket = (i == '[');
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700568 i = stdscan(NULL, &tokval); /* then skip the colon */
569 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
570 process_size_override(result, operand);
571 i = stdscan(NULL, &tokval);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 }
573 } else { /* immediate operand, or register */
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700574 mref = false;
575 bracket = false; /* placate optimisers */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000577
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 if ((result->oprs[operand].type & FAR) && !mref &&
579 result->opcode != I_JMP && result->opcode != I_CALL) {
580 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
581 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000582
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 value = evaluate(stdscan, NULL, &tokval,
584 &result->oprs[operand].opflags,
585 critical, error, &hints);
586 i = tokval.t_type;
587 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700588 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 }
590 if (!value) { /* error in evaluator */
591 result->opcode = -1; /* unrecoverable parse error: */
592 return result; /* ignore this instruction */
593 }
594 if (i == ':' && mref) { /* it was seg:offset */
595 /*
596 * Process the segment override.
597 */
598 if (value[1].type != 0 || value->value != 1 ||
599 REG_SREG & ~reg_flags[value->type])
600 error(ERR_NONFATAL, "invalid segment override");
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700601 else if (result->prefixes[PPS_SEG])
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 error(ERR_NONFATAL,
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700603 "instruction has conflicting segment overrides");
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000604 else {
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700605 result->prefixes[PPS_SEG] = value->type;
H. Peter Anvin490bbcd2007-08-29 20:30:31 +0000606 if (!(REG_FSGS & ~reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000607 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000608 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000609
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 i = stdscan(NULL, &tokval); /* then skip the colon */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700611 while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
612 process_size_override(result, operand);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 i = stdscan(NULL, &tokval);
614 }
615 value = evaluate(stdscan, NULL, &tokval,
616 &result->oprs[operand].opflags,
617 critical, error, &hints);
618 i = tokval.t_type;
619 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700620 result->forw_ref = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 }
622 /* and get the offset */
623 if (!value) { /* but, error in evaluator */
624 result->opcode = -1; /* unrecoverable parse error: */
625 return result; /* ignore this instruction */
626 }
627 }
628 if (mref && bracket) { /* find ] at the end */
629 if (i != ']') {
630 error(ERR_NONFATAL, "parser: expecting ]");
631 do { /* error recovery again */
632 i = stdscan(NULL, &tokval);
633 } while (i != 0 && i != ',');
634 } else /* we got the required ] */
635 i = stdscan(NULL, &tokval);
636 } else { /* immediate operand */
637 if (i != 0 && i != ',' && i != ':') {
638 error(ERR_NONFATAL, "comma or end of line expected");
639 do { /* error recovery */
640 i = stdscan(NULL, &tokval);
641 } while (i != 0 && i != ',');
642 } else if (i == ':') {
643 result->oprs[operand].type |= COLON;
644 }
645 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000646
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 /* now convert the exprs returned from evaluate() into operand
648 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000649
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 if (mref) { /* it's a memory reference */
651 expr *e = value;
652 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000653 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000654
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 b = i = -1, o = s = 0;
656 result->oprs[operand].hintbase = hints.base;
657 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000658
H. Peter Anvine2c80182005-01-15 22:15:51 +0000659 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
660 if (e->value == 1) /* in fact it can be basereg */
661 b = e->type;
662 else /* no, it has to be indexreg */
663 i = e->type, s = e->value;
664 e++;
665 }
666 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
667 if (b != -1) /* If the first was the base, ... */
668 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000669
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670 else if (e->value != 1) { /* If both want to be index */
671 error(ERR_NONFATAL,
672 "beroset-p-592-invalid effective address");
673 result->opcode = -1;
674 return result;
675 } else
676 b = e->type;
677 e++;
678 }
679 if (e->type != 0) { /* is there an offset? */
680 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
681 error(ERR_NONFATAL,
682 "beroset-p-603-invalid effective address");
683 result->opcode = -1;
684 return result;
685 } else {
686 if (e->type == EXPR_UNKNOWN) {
687 o = 0; /* doesn't matter what */
688 result->oprs[operand].wrt = NO_SEG; /* nor this */
689 result->oprs[operand].segment = NO_SEG; /* or this */
690 while (e->type)
691 e++; /* go to the end of the line */
692 } else {
693 if (e->type == EXPR_SIMPLE) {
694 o = e->value;
695 e++;
696 }
697 if (e->type == EXPR_WRT) {
698 result->oprs[operand].wrt = e->value;
699 e++;
700 } else
701 result->oprs[operand].wrt = NO_SEG;
702 /*
703 * Look for a segment base type.
704 */
705 if (e->type && e->type < EXPR_SEGBASE) {
706 error(ERR_NONFATAL,
707 "beroset-p-630-invalid effective address");
708 result->opcode = -1;
709 return result;
710 }
711 while (e->type && e->value == 0)
712 e++;
713 if (e->type && e->value != 1) {
714 error(ERR_NONFATAL,
715 "beroset-p-637-invalid effective address");
716 result->opcode = -1;
717 return result;
718 }
719 if (e->type) {
720 result->oprs[operand].segment =
721 e->type - EXPR_SEGBASE;
722 e++;
723 } else
724 result->oprs[operand].segment = NO_SEG;
725 while (e->type && e->value == 0)
726 e++;
727 if (e->type) {
728 error(ERR_NONFATAL,
729 "beroset-p-650-invalid effective address");
730 result->opcode = -1;
731 return result;
732 }
733 }
734 }
735 } else {
736 o = 0;
737 result->oprs[operand].wrt = NO_SEG;
738 result->oprs[operand].segment = NO_SEG;
739 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000740
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 if (e->type != 0) { /* there'd better be nothing left! */
742 error(ERR_NONFATAL,
743 "beroset-p-663-invalid effective address");
744 result->opcode = -1;
745 return result;
746 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000747
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700748 /* It is memory, but it can match any r/m operand */
749 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000750
751 if (b == -1 && (i == -1 || s == 0)) {
752 int is_rel = globalbits == 64 &&
753 !(result->oprs[operand].eaflags & EAF_ABS) &&
754 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000755 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000756 (result->oprs[operand].eaflags & EAF_REL));
757
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700758 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000759 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 result->oprs[operand].basereg = b;
761 result->oprs[operand].indexreg = i;
762 result->oprs[operand].scale = s;
763 result->oprs[operand].offset = o;
764 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000765
H. Peter Anvine2c80182005-01-15 22:15:51 +0000766 if (is_just_unknown(value)) { /* it's immediate but unknown */
767 result->oprs[operand].type |= IMMEDIATE;
768 result->oprs[operand].offset = 0; /* don't care */
769 result->oprs[operand].segment = NO_SEG; /* don't care again */
770 result->oprs[operand].wrt = NO_SEG; /* still don't care */
771 } else if (is_reloc(value)) { /* it's immediate */
772 result->oprs[operand].type |= IMMEDIATE;
773 result->oprs[operand].offset = reloc_value(value);
774 result->oprs[operand].segment = reloc_seg(value);
775 result->oprs[operand].wrt = reloc_wrt(value);
776 if (is_simple(value)) {
777 if (reloc_value(value) == 1)
778 result->oprs[operand].type |= UNITY;
779 if (optimizing >= 0 &&
780 !(result->oprs[operand].type & STRICT)) {
781 if (reloc_value(value) >= -128 &&
782 reloc_value(value) <= 127)
783 result->oprs[operand].type |= SBYTE;
784 }
785 }
786 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000787
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 if (value->type >= EXPR_SIMPLE || value->value != 1) {
789 error(ERR_NONFATAL, "invalid operand type");
790 result->opcode = -1;
791 return result;
792 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000793
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 /*
795 * check that its only 1 register, not an expression...
796 */
797 for (i = 1; value[i].type; i++)
798 if (value[i].value) {
799 error(ERR_NONFATAL, "invalid operand type");
800 result->opcode = -1;
801 return result;
802 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 /* clear overrides, except TO which applies to FPU regs */
805 if (result->oprs[operand].type & ~TO) {
806 /*
807 * we want to produce a warning iff the specified size
808 * is different from the register size
809 */
810 i = result->oprs[operand].type & SIZE_MASK;
811 } else
812 i = 0;
813
814 result->oprs[operand].type &= TO;
815 result->oprs[operand].type |= REGISTER;
816 result->oprs[operand].type |= reg_flags[value->type];
817 result->oprs[operand].basereg = value->type;
818
819 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
820 error(ERR_WARNING | ERR_PASS1,
821 "register size specification ignored");
822 }
823 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000824 }
825
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000827
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700828/* clear remaining operands */
829while (operand < MAX_OPERANDS)
830 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000831
832 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700833 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000834 */
835 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 case I_RESW:
837 result->opcode = I_RESB;
838 result->oprs[0].offset *= 2;
839 break;
840 case I_RESD:
841 result->opcode = I_RESB;
842 result->oprs[0].offset *= 4;
843 break;
844 case I_RESQ:
845 result->opcode = I_RESB;
846 result->oprs[0].offset *= 8;
847 break;
848 case I_REST:
849 result->opcode = I_RESB;
850 result->oprs[0].offset *= 10;
851 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700852 case I_RESO:
853 result->opcode = I_RESB;
854 result->oprs[0].offset *= 16;
855 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700856 default:
857 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000858 }
859
860 return result;
861}
862
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000864{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000865 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000866 int i;
867 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000868
H. Peter Anvin76690a12002-04-30 20:52:49 +0000869 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000870 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000871 stdscan_bufptr = p;
872 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000873}
874
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000876{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000877 extop *e;
878
879 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 e = i->eops;
881 i->eops = i->eops->next;
882 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000883 }
884}