blob: ca12a0975a4c5c5742f4d561755bfa9f7a6096de [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
11#include <stdio.h>
12#include <stdlib.h>
13#include <stddef.h>
14#include <string.h>
15#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000016#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000017
18#include "nasm.h"
H. Peter Anvin24cfef42002-09-12 16:34:06 +000019#include "insns.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000020#include "nasmlib.h"
H. Peter Anvin74cc5e52007-08-30 22:35:34 +000021#include "stdscan.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000022#include "parser.h"
23#include "float.h"
24
H. Peter Anvine2c80182005-01-15 22:15:51 +000025extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000026extern int32_t abs_seg; /* ABSOLUTE segment */
27extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvind0e365d2002-05-26 18:19:19 +000028
H. Peter Anvine2c80182005-01-15 22:15:51 +000029#include "regflags.c" /* List of register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000030
H. Peter Anvine2c80182005-01-15 22:15:51 +000031static int is_comma_next(void);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000032
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000033static int i;
34static struct tokenval tokval;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035static efunc error;
H. Peter Anvine2c80182005-01-15 22:15:51 +000036static struct ofmt *outfmt; /* Structure of addresses of output routines */
37static loc_t *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000038
H. Peter Anvine2c80182005-01-15 22:15:51 +000039void parser_global_info(struct ofmt *output, loc_t * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +000040{
41 outfmt = output;
42 location = locp;
43}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000044
Keith Kaniosa6dfa782007-04-13 16:47:53 +000045insn *parse_line(int pass, char *buffer, insn * result,
H. Peter Anvine2c80182005-01-15 22:15:51 +000046 efunc errfunc, evalfunc evaluate, ldfunc ldef)
H. Peter Anvineba20a72002-04-30 20:53:55 +000047{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048 int operand;
49 int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000050 struct eval_hints hints;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000051
H. Peter Anvin76690a12002-04-30 20:52:49 +000052 result->forw_ref = FALSE;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000053 error = errfunc;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000054
H. Peter Anvin76690a12002-04-30 20:52:49 +000055 stdscan_reset();
56 stdscan_bufptr = buffer;
57 i = stdscan(NULL, &tokval);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058
H. Peter Anvine2c80182005-01-15 22:15:51 +000059 result->label = NULL; /* Assume no label */
60 result->eops = NULL; /* must do this, whatever happens */
Keith Kaniosb7a89542007-04-12 02:40:54 +000061 result->operands = 0; /* must initialize this */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000062
H. Peter Anvine2c80182005-01-15 22:15:51 +000063 if (i == 0) { /* blank line - ignore */
64 result->opcode = -1; /* and no instruction either */
65 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000066 }
67 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
H. Peter Anvine2c80182005-01-15 22:15:51 +000068 (i != TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
69 error(ERR_NONFATAL, "label or instruction expected"
70 " at start of line");
71 result->opcode = -1;
72 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000073 }
74
H. Peter Anvine2c80182005-01-15 22:15:51 +000075 if (i == TOKEN_ID) { /* there's a label here */
76 result->label = tokval.t_charptr;
77 i = stdscan(NULL, &tokval);
78 if (i == ':') { /* skip over the optional colon */
79 i = stdscan(NULL, &tokval);
80 } else if (i == 0) {
81 error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
82 "label alone on a line without a colon might be in error");
83 }
84 if (i != TOKEN_INSN || tokval.t_integer != I_EQU) {
85 /*
86 * FIXME: location->segment could be NO_SEG, in which case
87 * it is possible we should be passing 'abs_seg'. Look into this.
88 * Work out whether that is *really* what we should be doing.
89 * Generally fix things. I think this is right as it is, but
90 * am still not certain.
91 */
92 ldef(result->label, in_abs_seg ? abs_seg : location->segment,
93 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
94 }
H. Peter Anvineba20a72002-04-30 20:53:55 +000095 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000096
H. Peter Anvine2c80182005-01-15 22:15:51 +000097 if (i == 0) {
98 result->opcode = -1; /* this line contains just a label */
99 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100 }
101
102 result->nprefix = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 result->times = 1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000104
105 while (i == TOKEN_PREFIX ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000106 (i == TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
H. Peter Anvineba20a72002-04-30 20:53:55 +0000107 {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108 /*
109 * Handle special case: the TIMES prefix.
110 */
111 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
112 expr *value;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000113
H. Peter Anvine2c80182005-01-15 22:15:51 +0000114 i = stdscan(NULL, &tokval);
115 value =
116 evaluate(stdscan, NULL, &tokval, NULL, pass0, error, NULL);
117 i = tokval.t_type;
118 if (!value) { /* but, error in evaluator */
119 result->opcode = -1; /* unrecoverable parse error: */
120 return result; /* ignore this instruction */
121 }
122 if (!is_simple(value)) {
123 error(ERR_NONFATAL,
124 "non-constant argument supplied to TIMES");
125 result->times = 1L;
126 } else {
127 result->times = value->value;
128 if (value->value < 0) {
129 error(ERR_NONFATAL, "TIMES value %d is negative",
130 value->value);
131 result->times = 0;
132 }
133 }
134 } else {
135 if (result->nprefix == MAXPREFIX)
136 error(ERR_NONFATAL,
137 "instruction has more than %d prefixes", MAXPREFIX);
138 else
139 result->prefixes[result->nprefix++] = tokval.t_integer;
140 i = stdscan(NULL, &tokval);
141 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000142 }
143
144 if (i != TOKEN_INSN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000145 if (result->nprefix > 0 && i == 0) {
146 /*
147 * Instruction prefixes are present, but no actual
148 * instruction. This is allowed: at this point we
149 * invent a notional instruction of RESB 0.
150 */
151 result->opcode = I_RESB;
152 result->operands = 1;
153 result->oprs[0].type = IMMEDIATE;
154 result->oprs[0].offset = 0L;
155 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
156 return result;
157 } else {
158 error(ERR_NONFATAL, "parser: instruction expected");
159 result->opcode = -1;
160 return result;
161 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000162 }
163
164 result->opcode = tokval.t_integer;
165 result->condition = tokval.t_inttwo;
166
167 /*
168 * RESB, RESW and RESD cannot be satisfied with incorrectly
169 * evaluated operands, since the correct values _must_ be known
170 * on the first pass. Hence, even in pass one, we set the
171 * `critical' flag on calling evaluate(), so that it will bomb
172 * out on undefined symbols. Nasty, but there's nothing we can
173 * do about it.
174 *
175 * For the moment, EQU has the same difficulty, so we'll
176 * include that.
177 */
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700178 if (result->opcode == I_RESB || result->opcode == I_RESW ||
179 result->opcode == I_RESD || result->opcode == I_RESQ ||
180 result->opcode == I_REST || result->opcode == I_RESO ||
181 result->opcode == I_EQU || result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000182 critical = pass0;
183 } else
184 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000185
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700186 if (result->opcode == I_DB || result->opcode == I_DW ||
187 result->opcode == I_DD || result->opcode == I_DQ ||
188 result->opcode == I_DT || result->opcode == I_DO ||
189 result->opcode == I_INCBIN) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 extop *eop, **tail = &result->eops, **fixptr;
191 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193 result->eops_float = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194
H. Peter Anvine2c80182005-01-15 22:15:51 +0000195 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700196 * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197 */
198 while (1) {
199 i = stdscan(NULL, &tokval);
200 if (i == 0)
201 break;
202 fixptr = tail;
203 eop = *tail = nasm_malloc(sizeof(extop));
204 tail = &eop->next;
205 eop->next = NULL;
206 eop->type = EOT_NOTHING;
207 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000208
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
210 eop->type = EOT_DB_STRING;
211 eop->stringval = tokval.t_charptr;
212 eop->stringlen = tokval.t_inttwo;
213 i = stdscan(NULL, &tokval); /* eat the comma */
214 continue;
215 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000216
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000218 int32_t sign = +1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000219
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 if (i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000221 char *save = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222 i = stdscan(NULL, &tokval);
223 sign = -1L;
224 if (i != TOKEN_FLOAT || !is_comma_next()) {
225 stdscan_bufptr = save;
226 i = tokval.t_type = '-';
227 }
228 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000229
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 if (i == TOKEN_FLOAT) {
231 eop->type = EOT_DB_STRING;
232 result->eops_float = TRUE;
233 if (result->opcode == I_DD)
234 eop->stringlen = 4;
235 else if (result->opcode == I_DQ)
236 eop->stringlen = 8;
237 else if (result->opcode == I_DT)
238 eop->stringlen = 10;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700239 else if (result->opcode == I_DO)
240 eop->stringlen = 16;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000241 else {
242 error(ERR_NONFATAL, "floating-point constant"
243 " encountered in `D%c' instruction",
244 result->opcode == I_DW ? 'W' : 'B');
245 /*
246 * fix suggested by Pedro Gimeno... original line
247 * was:
248 * eop->type = EOT_NOTHING;
249 */
250 eop->stringlen = 0;
251 }
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700252 eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000253 tail = &eop->next;
254 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000255 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000256 if (eop->stringlen < 4 ||
257 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000258 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259 eop->stringlen, error))
260 eop->type = EOT_NOTHING;
261 i = stdscan(NULL, &tokval); /* eat the comma */
262 continue;
263 }
264 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000265
H. Peter Anvine2c80182005-01-15 22:15:51 +0000266 /* anything else */
267 {
268 expr *value;
269 value = evaluate(stdscan, NULL, &tokval, NULL,
270 critical, error, NULL);
271 i = tokval.t_type;
272 if (!value) { /* error in evaluator */
273 result->opcode = -1; /* unrecoverable parse error: */
274 return result; /* ignore this instruction */
275 }
276 if (is_unknown(value)) {
277 eop->type = EOT_DB_NUMBER;
278 eop->offset = 0; /* doesn't matter what we put */
279 eop->segment = eop->wrt = NO_SEG; /* likewise */
280 } else if (is_reloc(value)) {
281 eop->type = EOT_DB_NUMBER;
282 eop->offset = reloc_value(value);
283 eop->segment = reloc_seg(value);
284 eop->wrt = reloc_wrt(value);
285 } else {
286 error(ERR_NONFATAL,
287 "operand %d: expression is not simple"
288 " or relocatable", oper_num);
289 }
290 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000291
H. Peter Anvine2c80182005-01-15 22:15:51 +0000292 /*
293 * We're about to call stdscan(), which will eat the
294 * comma that we're currently sitting on between
295 * arguments. However, we'd better check first that it
296 * _is_ a comma.
297 */
298 if (i == 0) /* also could be EOL */
299 break;
300 if (i != ',') {
301 error(ERR_NONFATAL, "comma expected after operand %d",
302 oper_num);
303 result->opcode = -1; /* unrecoverable parse error: */
304 return result; /* ignore this instruction */
305 }
306 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000307
H. Peter Anvine2c80182005-01-15 22:15:51 +0000308 if (result->opcode == I_INCBIN) {
309 /*
310 * Correct syntax for INCBIN is that there should be
311 * one string operand, followed by one or two numeric
312 * operands.
313 */
314 if (!result->eops || result->eops->type != EOT_DB_STRING)
315 error(ERR_NONFATAL, "`incbin' expects a file name");
316 else if (result->eops->next &&
317 result->eops->next->type != EOT_DB_NUMBER)
318 error(ERR_NONFATAL, "`incbin': second parameter is",
319 " non-numeric");
320 else if (result->eops->next && result->eops->next->next &&
321 result->eops->next->next->type != EOT_DB_NUMBER)
322 error(ERR_NONFATAL, "`incbin': third parameter is",
323 " non-numeric");
324 else if (result->eops->next && result->eops->next->next &&
325 result->eops->next->next->next)
326 error(ERR_NONFATAL,
327 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000328 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329 return result;
330 /*
331 * If we reach here, one of the above errors happened.
332 * Throw the instruction away.
333 */
334 result->opcode = -1;
335 return result;
336 } else /* DB ... */ if (oper_num == 0)
337 error(ERR_WARNING | ERR_PASS1,
338 "no operand for data declaration");
339 else
340 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000341
H. Peter Anvine2c80182005-01-15 22:15:51 +0000342 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000343 }
344
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700345 /* right. Now we begin to parse the operands. There may be up to four
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346 * of these, separated by commas, and terminated by a zero token. */
347
H. Peter Anvin8f94f982007-09-17 16:31:33 -0700348 for (operand = 0; operand < MAX_OPERANDS; operand++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000349 expr *value; /* used most of the time */
350 int mref; /* is this going to be a memory ref? */
351 int bracket; /* is it a [] mref, or a & mref? */
352 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353
H. Peter Anvine2c80182005-01-15 22:15:51 +0000354 result->oprs[operand].addr_size = 0; /* have to zero this whatever */
355 result->oprs[operand].eaflags = 0; /* and this */
356 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000357
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358 i = stdscan(NULL, &tokval);
359 if (i == 0)
360 break; /* end of operands: get out of here */
361 result->oprs[operand].type = 0; /* so far, no override */
362 while (i == TOKEN_SPECIAL) { /* size specifiers */
363 switch ((int)tokval.t_integer) {
364 case S_BYTE:
365 if (!setsize) /* we want to use only the first */
366 result->oprs[operand].type |= BITS8;
367 setsize = 1;
368 break;
369 case S_WORD:
370 if (!setsize)
371 result->oprs[operand].type |= BITS16;
372 setsize = 1;
373 break;
374 case S_DWORD:
375 case S_LONG:
376 if (!setsize)
377 result->oprs[operand].type |= BITS32;
378 setsize = 1;
379 break;
380 case S_QWORD:
381 if (!setsize)
382 result->oprs[operand].type |= BITS64;
383 setsize = 1;
384 break;
385 case S_TWORD:
386 if (!setsize)
387 result->oprs[operand].type |= BITS80;
388 setsize = 1;
389 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700390 case S_OWORD:
391 if (!setsize)
392 result->oprs[operand].type |= BITS128;
393 setsize = 1;
394 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 case S_TO:
396 result->oprs[operand].type |= TO;
397 break;
398 case S_STRICT:
399 result->oprs[operand].type |= STRICT;
400 break;
401 case S_FAR:
402 result->oprs[operand].type |= FAR;
403 break;
404 case S_NEAR:
405 result->oprs[operand].type |= NEAR;
406 break;
407 case S_SHORT:
408 result->oprs[operand].type |= SHORT;
409 break;
410 default:
411 error(ERR_NONFATAL, "invalid operand size specification");
412 }
413 i = stdscan(NULL, &tokval);
414 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000415
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 if (i == '[' || i == '&') { /* memory reference */
417 mref = TRUE;
418 bracket = (i == '[');
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000419 while ((i = stdscan(NULL, &tokval)) == TOKEN_SPECIAL) {
420 /* check for address directives */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 if (tasm_compatible_mode) {
422 switch ((int)tokval.t_integer) {
423 /* For TASM compatibility a size override inside the
424 * brackets changes the size of the operand, not the
425 * address type of the operand as it does in standard
426 * NASM syntax. Hence:
427 *
428 * mov eax,[DWORD val]
429 *
430 * is valid syntax in TASM compatibility mode. Note that
431 * you lose the ability to override the default address
432 * type for the instruction, but we never use anything
433 * but 32-bit flat model addressing in our code.
434 */
435 case S_BYTE:
436 result->oprs[operand].type |= BITS8;
437 break;
438 case S_WORD:
439 result->oprs[operand].type |= BITS16;
440 break;
441 case S_DWORD:
442 case S_LONG:
443 result->oprs[operand].type |= BITS32;
444 break;
445 case S_QWORD:
446 result->oprs[operand].type |= BITS64;
447 break;
448 case S_TWORD:
449 result->oprs[operand].type |= BITS80;
450 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700451 case S_OWORD:
452 result->oprs[operand].type |= BITS128;
453 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 default:
455 error(ERR_NONFATAL,
456 "invalid operand size specification");
457 }
458 } else {
459 /* Standard NASM compatible syntax */
460 switch ((int)tokval.t_integer) {
461 case S_NOSPLIT:
462 result->oprs[operand].eaflags |= EAF_TIMESTWO;
463 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000464 case S_REL:
465 result->oprs[operand].eaflags |= EAF_REL;
466 break;
467 case S_ABS:
468 result->oprs[operand].eaflags |= EAF_ABS;
469 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 case S_BYTE:
471 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
472 break;
473 case S_WORD:
474 result->oprs[operand].addr_size = 16;
475 result->oprs[operand].eaflags |= EAF_WORDOFFS;
476 break;
477 case S_DWORD:
478 case S_LONG:
479 result->oprs[operand].addr_size = 32;
480 result->oprs[operand].eaflags |= EAF_WORDOFFS;
481 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000482 case S_QWORD:
483 result->oprs[operand].addr_size = 64;
484 result->oprs[operand].eaflags |= EAF_WORDOFFS;
485 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 default:
487 error(ERR_NONFATAL, "invalid size specification in"
488 " effective address");
489 }
490 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 }
492 } else { /* immediate operand, or register */
493 mref = FALSE;
494 bracket = FALSE; /* placate optimisers */
495 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000496
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 if ((result->oprs[operand].type & FAR) && !mref &&
498 result->opcode != I_JMP && result->opcode != I_CALL) {
499 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
500 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000501
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 value = evaluate(stdscan, NULL, &tokval,
503 &result->oprs[operand].opflags,
504 critical, error, &hints);
505 i = tokval.t_type;
506 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
507 result->forw_ref = TRUE;
508 }
509 if (!value) { /* error in evaluator */
510 result->opcode = -1; /* unrecoverable parse error: */
511 return result; /* ignore this instruction */
512 }
513 if (i == ':' && mref) { /* it was seg:offset */
514 /*
515 * Process the segment override.
516 */
517 if (value[1].type != 0 || value->value != 1 ||
518 REG_SREG & ~reg_flags[value->type])
519 error(ERR_NONFATAL, "invalid segment override");
520 else if (result->nprefix == MAXPREFIX)
521 error(ERR_NONFATAL,
522 "instruction has more than %d prefixes", MAXPREFIX);
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000523 else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 result->prefixes[result->nprefix++] = value->type;
H. Peter Anvin490bbcd2007-08-29 20:30:31 +0000525 if (!(REG_FSGS & ~reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000526 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000527 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 i = stdscan(NULL, &tokval); /* then skip the colon */
530 if (i == TOKEN_SPECIAL) { /* another check for size override */
531 switch ((int)tokval.t_integer) {
532 case S_WORD:
533 result->oprs[operand].addr_size = 16;
534 break;
535 case S_DWORD:
536 case S_LONG:
537 result->oprs[operand].addr_size = 32;
538 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000539 case S_QWORD:
540 result->oprs[operand].addr_size = 64;
541 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 default:
543 error(ERR_NONFATAL, "invalid size specification in"
544 " effective address");
545 }
546 i = stdscan(NULL, &tokval);
547 }
548 value = evaluate(stdscan, NULL, &tokval,
549 &result->oprs[operand].opflags,
550 critical, error, &hints);
551 i = tokval.t_type;
552 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
553 result->forw_ref = TRUE;
554 }
555 /* and get the offset */
556 if (!value) { /* but, error in evaluator */
557 result->opcode = -1; /* unrecoverable parse error: */
558 return result; /* ignore this instruction */
559 }
560 }
561 if (mref && bracket) { /* find ] at the end */
562 if (i != ']') {
563 error(ERR_NONFATAL, "parser: expecting ]");
564 do { /* error recovery again */
565 i = stdscan(NULL, &tokval);
566 } while (i != 0 && i != ',');
567 } else /* we got the required ] */
568 i = stdscan(NULL, &tokval);
569 } else { /* immediate operand */
570 if (i != 0 && i != ',' && i != ':') {
571 error(ERR_NONFATAL, "comma or end of line expected");
572 do { /* error recovery */
573 i = stdscan(NULL, &tokval);
574 } while (i != 0 && i != ',');
575 } else if (i == ':') {
576 result->oprs[operand].type |= COLON;
577 }
578 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000579
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 /* now convert the exprs returned from evaluate() into operand
581 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000582
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 if (mref) { /* it's a memory reference */
584 expr *e = value;
585 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000586 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000587
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 b = i = -1, o = s = 0;
589 result->oprs[operand].hintbase = hints.base;
590 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000591
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
593 if (e->value == 1) /* in fact it can be basereg */
594 b = e->type;
595 else /* no, it has to be indexreg */
596 i = e->type, s = e->value;
597 e++;
598 }
599 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
600 if (b != -1) /* If the first was the base, ... */
601 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000602
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 else if (e->value != 1) { /* If both want to be index */
604 error(ERR_NONFATAL,
605 "beroset-p-592-invalid effective address");
606 result->opcode = -1;
607 return result;
608 } else
609 b = e->type;
610 e++;
611 }
612 if (e->type != 0) { /* is there an offset? */
613 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
614 error(ERR_NONFATAL,
615 "beroset-p-603-invalid effective address");
616 result->opcode = -1;
617 return result;
618 } else {
619 if (e->type == EXPR_UNKNOWN) {
620 o = 0; /* doesn't matter what */
621 result->oprs[operand].wrt = NO_SEG; /* nor this */
622 result->oprs[operand].segment = NO_SEG; /* or this */
623 while (e->type)
624 e++; /* go to the end of the line */
625 } else {
626 if (e->type == EXPR_SIMPLE) {
627 o = e->value;
628 e++;
629 }
630 if (e->type == EXPR_WRT) {
631 result->oprs[operand].wrt = e->value;
632 e++;
633 } else
634 result->oprs[operand].wrt = NO_SEG;
635 /*
636 * Look for a segment base type.
637 */
638 if (e->type && e->type < EXPR_SEGBASE) {
639 error(ERR_NONFATAL,
640 "beroset-p-630-invalid effective address");
641 result->opcode = -1;
642 return result;
643 }
644 while (e->type && e->value == 0)
645 e++;
646 if (e->type && e->value != 1) {
647 error(ERR_NONFATAL,
648 "beroset-p-637-invalid effective address");
649 result->opcode = -1;
650 return result;
651 }
652 if (e->type) {
653 result->oprs[operand].segment =
654 e->type - EXPR_SEGBASE;
655 e++;
656 } else
657 result->oprs[operand].segment = NO_SEG;
658 while (e->type && e->value == 0)
659 e++;
660 if (e->type) {
661 error(ERR_NONFATAL,
662 "beroset-p-650-invalid effective address");
663 result->opcode = -1;
664 return result;
665 }
666 }
667 }
668 } else {
669 o = 0;
670 result->oprs[operand].wrt = NO_SEG;
671 result->oprs[operand].segment = NO_SEG;
672 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000673
H. Peter Anvine2c80182005-01-15 22:15:51 +0000674 if (e->type != 0) { /* there'd better be nothing left! */
675 error(ERR_NONFATAL,
676 "beroset-p-663-invalid effective address");
677 result->opcode = -1;
678 return result;
679 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000680
H. Peter Anvin0da6b582007-09-12 20:32:39 -0700681 /* It is memory, but it can match any r/m operand */
682 result->oprs[operand].type |= MEMORY_ANY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000683
684 if (b == -1 && (i == -1 || s == 0)) {
685 int is_rel = globalbits == 64 &&
686 !(result->oprs[operand].eaflags & EAF_ABS) &&
687 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000688 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000689 (result->oprs[operand].eaflags & EAF_REL));
690
691 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
692 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 result->oprs[operand].basereg = b;
694 result->oprs[operand].indexreg = i;
695 result->oprs[operand].scale = s;
696 result->oprs[operand].offset = o;
697 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000698
H. Peter Anvine2c80182005-01-15 22:15:51 +0000699 if (is_just_unknown(value)) { /* it's immediate but unknown */
700 result->oprs[operand].type |= IMMEDIATE;
701 result->oprs[operand].offset = 0; /* don't care */
702 result->oprs[operand].segment = NO_SEG; /* don't care again */
703 result->oprs[operand].wrt = NO_SEG; /* still don't care */
704 } else if (is_reloc(value)) { /* it's immediate */
705 result->oprs[operand].type |= IMMEDIATE;
706 result->oprs[operand].offset = reloc_value(value);
707 result->oprs[operand].segment = reloc_seg(value);
708 result->oprs[operand].wrt = reloc_wrt(value);
709 if (is_simple(value)) {
710 if (reloc_value(value) == 1)
711 result->oprs[operand].type |= UNITY;
712 if (optimizing >= 0 &&
713 !(result->oprs[operand].type & STRICT)) {
714 if (reloc_value(value) >= -128 &&
715 reloc_value(value) <= 127)
716 result->oprs[operand].type |= SBYTE;
717 }
718 }
719 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000720
H. Peter Anvine2c80182005-01-15 22:15:51 +0000721 if (value->type >= EXPR_SIMPLE || value->value != 1) {
722 error(ERR_NONFATAL, "invalid operand type");
723 result->opcode = -1;
724 return result;
725 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000726
H. Peter Anvine2c80182005-01-15 22:15:51 +0000727 /*
728 * check that its only 1 register, not an expression...
729 */
730 for (i = 1; value[i].type; i++)
731 if (value[i].value) {
732 error(ERR_NONFATAL, "invalid operand type");
733 result->opcode = -1;
734 return result;
735 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000736
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 /* clear overrides, except TO which applies to FPU regs */
738 if (result->oprs[operand].type & ~TO) {
739 /*
740 * we want to produce a warning iff the specified size
741 * is different from the register size
742 */
743 i = result->oprs[operand].type & SIZE_MASK;
744 } else
745 i = 0;
746
747 result->oprs[operand].type &= TO;
748 result->oprs[operand].type |= REGISTER;
749 result->oprs[operand].type |= reg_flags[value->type];
750 result->oprs[operand].basereg = value->type;
751
752 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
753 error(ERR_WARNING | ERR_PASS1,
754 "register size specification ignored");
755 }
756 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000757 }
758
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 while (operand < 3) /* clear remaining operands */
762 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000763
764 /*
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700765 * Transform RESW, RESD, RESQ, REST, RESO into RESB.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000766 */
767 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 case I_RESW:
769 result->opcode = I_RESB;
770 result->oprs[0].offset *= 2;
771 break;
772 case I_RESD:
773 result->opcode = I_RESB;
774 result->oprs[0].offset *= 4;
775 break;
776 case I_RESQ:
777 result->opcode = I_RESB;
778 result->oprs[0].offset *= 8;
779 break;
780 case I_REST:
781 result->opcode = I_RESB;
782 result->oprs[0].offset *= 10;
783 break;
H. Peter Anvin41c9f6f2007-09-18 13:01:32 -0700784 case I_RESO:
785 result->opcode = I_RESB;
786 result->oprs[0].offset *= 16;
787 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700788 default:
789 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000790 }
791
792 return result;
793}
794
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000796{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000797 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000798 int i;
799 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800
H. Peter Anvin76690a12002-04-30 20:52:49 +0000801 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000803 stdscan_bufptr = p;
804 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805}
806
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000808{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000809 extop *e;
810
811 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 e = i->eops;
813 i->eops = i->eops->next;
814 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815 }
816}