blob: 8a376fd1ff06addab168d0fa3b92202acac8230d [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 Anvine2c80182005-01-15 22:15:51 +0000178 if (result->opcode == I_RESB || result->opcode == I_RESW || result->opcode == I_RESD || result->opcode == I_RESQ || result->opcode == I_REST || result->opcode == I_EQU || result->opcode == I_INCBIN) { /* fbk */
179 critical = pass0;
180 } else
181 critical = (pass == 2 ? 2 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000182
183 if (result->opcode == I_DB ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184 result->opcode == I_DW ||
185 result->opcode == I_DD ||
186 result->opcode == I_DQ ||
187 result->opcode == I_DT || result->opcode == I_INCBIN) {
188 extop *eop, **tail = &result->eops, **fixptr;
189 int oper_num = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000190
H. Peter Anvine2c80182005-01-15 22:15:51 +0000191 result->eops_float = FALSE;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000192
H. Peter Anvine2c80182005-01-15 22:15:51 +0000193 /*
194 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
195 */
196 while (1) {
197 i = stdscan(NULL, &tokval);
198 if (i == 0)
199 break;
200 fixptr = tail;
201 eop = *tail = nasm_malloc(sizeof(extop));
202 tail = &eop->next;
203 eop->next = NULL;
204 eop->type = EOT_NOTHING;
205 oper_num++;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000206
H. Peter Anvine2c80182005-01-15 22:15:51 +0000207 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
208 eop->type = EOT_DB_STRING;
209 eop->stringval = tokval.t_charptr;
210 eop->stringlen = tokval.t_inttwo;
211 i = stdscan(NULL, &tokval); /* eat the comma */
212 continue;
213 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000216 int32_t sign = +1L;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217
H. Peter Anvine2c80182005-01-15 22:15:51 +0000218 if (i == '-') {
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000219 char *save = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 i = stdscan(NULL, &tokval);
221 sign = -1L;
222 if (i != TOKEN_FLOAT || !is_comma_next()) {
223 stdscan_bufptr = save;
224 i = tokval.t_type = '-';
225 }
226 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000227
H. Peter Anvine2c80182005-01-15 22:15:51 +0000228 if (i == TOKEN_FLOAT) {
229 eop->type = EOT_DB_STRING;
230 result->eops_float = TRUE;
231 if (result->opcode == I_DD)
232 eop->stringlen = 4;
233 else if (result->opcode == I_DQ)
234 eop->stringlen = 8;
235 else if (result->opcode == I_DT)
236 eop->stringlen = 10;
237 else {
238 error(ERR_NONFATAL, "floating-point constant"
239 " encountered in `D%c' instruction",
240 result->opcode == I_DW ? 'W' : 'B');
241 /*
242 * fix suggested by Pedro Gimeno... original line
243 * was:
244 * eop->type = EOT_NOTHING;
245 */
246 eop->stringlen = 0;
247 }
248 eop =
249 nasm_realloc(eop, sizeof(extop) + eop->stringlen);
250 tail = &eop->next;
251 *fixptr = eop;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000252 eop->stringval = (char *)eop + sizeof(extop);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000253 if (eop->stringlen < 4 ||
254 !float_const(tokval.t_charptr, sign,
Keith Kaniosb7a89542007-04-12 02:40:54 +0000255 (uint8_t *)eop->stringval,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000256 eop->stringlen, error))
257 eop->type = EOT_NOTHING;
258 i = stdscan(NULL, &tokval); /* eat the comma */
259 continue;
260 }
261 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000262
H. Peter Anvine2c80182005-01-15 22:15:51 +0000263 /* anything else */
264 {
265 expr *value;
266 value = evaluate(stdscan, NULL, &tokval, NULL,
267 critical, error, NULL);
268 i = tokval.t_type;
269 if (!value) { /* error in evaluator */
270 result->opcode = -1; /* unrecoverable parse error: */
271 return result; /* ignore this instruction */
272 }
273 if (is_unknown(value)) {
274 eop->type = EOT_DB_NUMBER;
275 eop->offset = 0; /* doesn't matter what we put */
276 eop->segment = eop->wrt = NO_SEG; /* likewise */
277 } else if (is_reloc(value)) {
278 eop->type = EOT_DB_NUMBER;
279 eop->offset = reloc_value(value);
280 eop->segment = reloc_seg(value);
281 eop->wrt = reloc_wrt(value);
282 } else {
283 error(ERR_NONFATAL,
284 "operand %d: expression is not simple"
285 " or relocatable", oper_num);
286 }
287 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000288
H. Peter Anvine2c80182005-01-15 22:15:51 +0000289 /*
290 * We're about to call stdscan(), which will eat the
291 * comma that we're currently sitting on between
292 * arguments. However, we'd better check first that it
293 * _is_ a comma.
294 */
295 if (i == 0) /* also could be EOL */
296 break;
297 if (i != ',') {
298 error(ERR_NONFATAL, "comma expected after operand %d",
299 oper_num);
300 result->opcode = -1; /* unrecoverable parse error: */
301 return result; /* ignore this instruction */
302 }
303 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000304
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305 if (result->opcode == I_INCBIN) {
306 /*
307 * Correct syntax for INCBIN is that there should be
308 * one string operand, followed by one or two numeric
309 * operands.
310 */
311 if (!result->eops || result->eops->type != EOT_DB_STRING)
312 error(ERR_NONFATAL, "`incbin' expects a file name");
313 else if (result->eops->next &&
314 result->eops->next->type != EOT_DB_NUMBER)
315 error(ERR_NONFATAL, "`incbin': second parameter is",
316 " non-numeric");
317 else if (result->eops->next && result->eops->next->next &&
318 result->eops->next->next->type != EOT_DB_NUMBER)
319 error(ERR_NONFATAL, "`incbin': third parameter is",
320 " non-numeric");
321 else if (result->eops->next && result->eops->next->next &&
322 result->eops->next->next->next)
323 error(ERR_NONFATAL,
324 "`incbin': more than three parameters");
H. Peter Anvineba20a72002-04-30 20:53:55 +0000325 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326 return result;
327 /*
328 * If we reach here, one of the above errors happened.
329 * Throw the instruction away.
330 */
331 result->opcode = -1;
332 return result;
333 } else /* DB ... */ if (oper_num == 0)
334 error(ERR_WARNING | ERR_PASS1,
335 "no operand for data declaration");
336 else
337 result->operands = oper_num;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338
H. Peter Anvine2c80182005-01-15 22:15:51 +0000339 return result;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000340 }
341
342 /* right. Now we begin to parse the operands. There may be up to three
343 * of these, separated by commas, and terminated by a zero token. */
344
345 for (operand = 0; operand < 3; operand++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000346 expr *value; /* used most of the time */
347 int mref; /* is this going to be a memory ref? */
348 int bracket; /* is it a [] mref, or a & mref? */
349 int setsize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000350
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 result->oprs[operand].addr_size = 0; /* have to zero this whatever */
352 result->oprs[operand].eaflags = 0; /* and this */
353 result->oprs[operand].opflags = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000354
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 i = stdscan(NULL, &tokval);
356 if (i == 0)
357 break; /* end of operands: get out of here */
358 result->oprs[operand].type = 0; /* so far, no override */
359 while (i == TOKEN_SPECIAL) { /* size specifiers */
360 switch ((int)tokval.t_integer) {
361 case S_BYTE:
362 if (!setsize) /* we want to use only the first */
363 result->oprs[operand].type |= BITS8;
364 setsize = 1;
365 break;
366 case S_WORD:
367 if (!setsize)
368 result->oprs[operand].type |= BITS16;
369 setsize = 1;
370 break;
371 case S_DWORD:
372 case S_LONG:
373 if (!setsize)
374 result->oprs[operand].type |= BITS32;
375 setsize = 1;
376 break;
377 case S_QWORD:
378 if (!setsize)
379 result->oprs[operand].type |= BITS64;
380 setsize = 1;
381 break;
382 case S_TWORD:
383 if (!setsize)
384 result->oprs[operand].type |= BITS80;
385 setsize = 1;
386 break;
387 case S_TO:
388 result->oprs[operand].type |= TO;
389 break;
390 case S_STRICT:
391 result->oprs[operand].type |= STRICT;
392 break;
393 case S_FAR:
394 result->oprs[operand].type |= FAR;
395 break;
396 case S_NEAR:
397 result->oprs[operand].type |= NEAR;
398 break;
399 case S_SHORT:
400 result->oprs[operand].type |= SHORT;
401 break;
402 default:
403 error(ERR_NONFATAL, "invalid operand size specification");
404 }
405 i = stdscan(NULL, &tokval);
406 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000407
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 if (i == '[' || i == '&') { /* memory reference */
409 mref = TRUE;
410 bracket = (i == '[');
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000411 while ((i = stdscan(NULL, &tokval)) == TOKEN_SPECIAL) {
412 /* check for address directives */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000413 if (tasm_compatible_mode) {
414 switch ((int)tokval.t_integer) {
415 /* For TASM compatibility a size override inside the
416 * brackets changes the size of the operand, not the
417 * address type of the operand as it does in standard
418 * NASM syntax. Hence:
419 *
420 * mov eax,[DWORD val]
421 *
422 * is valid syntax in TASM compatibility mode. Note that
423 * you lose the ability to override the default address
424 * type for the instruction, but we never use anything
425 * but 32-bit flat model addressing in our code.
426 */
427 case S_BYTE:
428 result->oprs[operand].type |= BITS8;
429 break;
430 case S_WORD:
431 result->oprs[operand].type |= BITS16;
432 break;
433 case S_DWORD:
434 case S_LONG:
435 result->oprs[operand].type |= BITS32;
436 break;
437 case S_QWORD:
438 result->oprs[operand].type |= BITS64;
439 break;
440 case S_TWORD:
441 result->oprs[operand].type |= BITS80;
442 break;
443 default:
444 error(ERR_NONFATAL,
445 "invalid operand size specification");
446 }
447 } else {
448 /* Standard NASM compatible syntax */
449 switch ((int)tokval.t_integer) {
450 case S_NOSPLIT:
451 result->oprs[operand].eaflags |= EAF_TIMESTWO;
452 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000453 case S_REL:
454 result->oprs[operand].eaflags |= EAF_REL;
455 break;
456 case S_ABS:
457 result->oprs[operand].eaflags |= EAF_ABS;
458 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 case S_BYTE:
460 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
461 break;
462 case S_WORD:
463 result->oprs[operand].addr_size = 16;
464 result->oprs[operand].eaflags |= EAF_WORDOFFS;
465 break;
466 case S_DWORD:
467 case S_LONG:
468 result->oprs[operand].addr_size = 32;
469 result->oprs[operand].eaflags |= EAF_WORDOFFS;
470 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000471 case S_QWORD:
472 result->oprs[operand].addr_size = 64;
473 result->oprs[operand].eaflags |= EAF_WORDOFFS;
474 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 default:
476 error(ERR_NONFATAL, "invalid size specification in"
477 " effective address");
478 }
479 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 }
481 } else { /* immediate operand, or register */
482 mref = FALSE;
483 bracket = FALSE; /* placate optimisers */
484 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000485
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 if ((result->oprs[operand].type & FAR) && !mref &&
487 result->opcode != I_JMP && result->opcode != I_CALL) {
488 error(ERR_NONFATAL, "invalid use of FAR operand specifier");
489 }
Debbie Wiles63b53f72002-06-04 19:31:24 +0000490
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 value = evaluate(stdscan, NULL, &tokval,
492 &result->oprs[operand].opflags,
493 critical, error, &hints);
494 i = tokval.t_type;
495 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
496 result->forw_ref = TRUE;
497 }
498 if (!value) { /* error in evaluator */
499 result->opcode = -1; /* unrecoverable parse error: */
500 return result; /* ignore this instruction */
501 }
502 if (i == ':' && mref) { /* it was seg:offset */
503 /*
504 * Process the segment override.
505 */
506 if (value[1].type != 0 || value->value != 1 ||
507 REG_SREG & ~reg_flags[value->type])
508 error(ERR_NONFATAL, "invalid segment override");
509 else if (result->nprefix == MAXPREFIX)
510 error(ERR_NONFATAL,
511 "instruction has more than %d prefixes", MAXPREFIX);
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000512 else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 result->prefixes[result->nprefix++] = value->type;
H. Peter Anvin490bbcd2007-08-29 20:30:31 +0000514 if (!(REG_FSGS & ~reg_flags[value->type]))
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000515 result->oprs[operand].eaflags |= EAF_FSGS;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000516 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000517
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 i = stdscan(NULL, &tokval); /* then skip the colon */
519 if (i == TOKEN_SPECIAL) { /* another check for size override */
520 switch ((int)tokval.t_integer) {
521 case S_WORD:
522 result->oprs[operand].addr_size = 16;
523 break;
524 case S_DWORD:
525 case S_LONG:
526 result->oprs[operand].addr_size = 32;
527 break;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000528 case S_QWORD:
529 result->oprs[operand].addr_size = 64;
530 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 default:
532 error(ERR_NONFATAL, "invalid size specification in"
533 " effective address");
534 }
535 i = stdscan(NULL, &tokval);
536 }
537 value = evaluate(stdscan, NULL, &tokval,
538 &result->oprs[operand].opflags,
539 critical, error, &hints);
540 i = tokval.t_type;
541 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
542 result->forw_ref = TRUE;
543 }
544 /* and get the offset */
545 if (!value) { /* but, error in evaluator */
546 result->opcode = -1; /* unrecoverable parse error: */
547 return result; /* ignore this instruction */
548 }
549 }
550 if (mref && bracket) { /* find ] at the end */
551 if (i != ']') {
552 error(ERR_NONFATAL, "parser: expecting ]");
553 do { /* error recovery again */
554 i = stdscan(NULL, &tokval);
555 } while (i != 0 && i != ',');
556 } else /* we got the required ] */
557 i = stdscan(NULL, &tokval);
558 } else { /* immediate operand */
559 if (i != 0 && i != ',' && i != ':') {
560 error(ERR_NONFATAL, "comma or end of line expected");
561 do { /* error recovery */
562 i = stdscan(NULL, &tokval);
563 } while (i != 0 && i != ',');
564 } else if (i == ':') {
565 result->oprs[operand].type |= COLON;
566 }
567 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000568
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 /* now convert the exprs returned from evaluate() into operand
570 * descriptions... */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000571
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 if (mref) { /* it's a memory reference */
573 expr *e = value;
574 int b, i, s; /* basereg, indexreg, scale */
Keith Kanios7a68f302007-04-16 04:56:06 +0000575 int64_t o; /* offset */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000576
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 b = i = -1, o = s = 0;
578 result->oprs[operand].hintbase = hints.base;
579 result->oprs[operand].hinttype = hints.type;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000580
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 if (e->type && e->type <= EXPR_REG_END) { /* this bit's a register */
582 if (e->value == 1) /* in fact it can be basereg */
583 b = e->type;
584 else /* no, it has to be indexreg */
585 i = e->type, s = e->value;
586 e++;
587 }
588 if (e->type && e->type <= EXPR_REG_END) { /* it's a 2nd register */
589 if (b != -1) /* If the first was the base, ... */
590 i = e->type, s = e->value; /* second has to be indexreg */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000591
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 else if (e->value != 1) { /* If both want to be index */
593 error(ERR_NONFATAL,
594 "beroset-p-592-invalid effective address");
595 result->opcode = -1;
596 return result;
597 } else
598 b = e->type;
599 e++;
600 }
601 if (e->type != 0) { /* is there an offset? */
602 if (e->type <= EXPR_REG_END) { /* in fact, is there an error? */
603 error(ERR_NONFATAL,
604 "beroset-p-603-invalid effective address");
605 result->opcode = -1;
606 return result;
607 } else {
608 if (e->type == EXPR_UNKNOWN) {
609 o = 0; /* doesn't matter what */
610 result->oprs[operand].wrt = NO_SEG; /* nor this */
611 result->oprs[operand].segment = NO_SEG; /* or this */
612 while (e->type)
613 e++; /* go to the end of the line */
614 } else {
615 if (e->type == EXPR_SIMPLE) {
616 o = e->value;
617 e++;
618 }
619 if (e->type == EXPR_WRT) {
620 result->oprs[operand].wrt = e->value;
621 e++;
622 } else
623 result->oprs[operand].wrt = NO_SEG;
624 /*
625 * Look for a segment base type.
626 */
627 if (e->type && e->type < EXPR_SEGBASE) {
628 error(ERR_NONFATAL,
629 "beroset-p-630-invalid effective address");
630 result->opcode = -1;
631 return result;
632 }
633 while (e->type && e->value == 0)
634 e++;
635 if (e->type && e->value != 1) {
636 error(ERR_NONFATAL,
637 "beroset-p-637-invalid effective address");
638 result->opcode = -1;
639 return result;
640 }
641 if (e->type) {
642 result->oprs[operand].segment =
643 e->type - EXPR_SEGBASE;
644 e++;
645 } else
646 result->oprs[operand].segment = NO_SEG;
647 while (e->type && e->value == 0)
648 e++;
649 if (e->type) {
650 error(ERR_NONFATAL,
651 "beroset-p-650-invalid effective address");
652 result->opcode = -1;
653 return result;
654 }
655 }
656 }
657 } else {
658 o = 0;
659 result->oprs[operand].wrt = NO_SEG;
660 result->oprs[operand].segment = NO_SEG;
661 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000662
H. Peter Anvine2c80182005-01-15 22:15:51 +0000663 if (e->type != 0) { /* there'd better be nothing left! */
664 error(ERR_NONFATAL,
665 "beroset-p-663-invalid effective address");
666 result->opcode = -1;
667 return result;
668 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000669
H. Peter Anvine2c80182005-01-15 22:15:51 +0000670 result->oprs[operand].type |= MEMORY;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000671
672 if (b == -1 && (i == -1 || s == 0)) {
673 int is_rel = globalbits == 64 &&
674 !(result->oprs[operand].eaflags & EAF_ABS) &&
675 ((globalrel &&
H. Peter Anvin150e20d2007-08-29 15:19:19 +0000676 !(result->oprs[operand].eaflags & EAF_FSGS)) ||
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +0000677 (result->oprs[operand].eaflags & EAF_REL));
678
679 result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
680 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 result->oprs[operand].basereg = b;
682 result->oprs[operand].indexreg = i;
683 result->oprs[operand].scale = s;
684 result->oprs[operand].offset = o;
685 } else { /* it's not a memory reference */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686
H. Peter Anvine2c80182005-01-15 22:15:51 +0000687 if (is_just_unknown(value)) { /* it's immediate but unknown */
688 result->oprs[operand].type |= IMMEDIATE;
689 result->oprs[operand].offset = 0; /* don't care */
690 result->oprs[operand].segment = NO_SEG; /* don't care again */
691 result->oprs[operand].wrt = NO_SEG; /* still don't care */
692 } else if (is_reloc(value)) { /* it's immediate */
693 result->oprs[operand].type |= IMMEDIATE;
694 result->oprs[operand].offset = reloc_value(value);
695 result->oprs[operand].segment = reloc_seg(value);
696 result->oprs[operand].wrt = reloc_wrt(value);
697 if (is_simple(value)) {
698 if (reloc_value(value) == 1)
699 result->oprs[operand].type |= UNITY;
700 if (optimizing >= 0 &&
701 !(result->oprs[operand].type & STRICT)) {
702 if (reloc_value(value) >= -128 &&
703 reloc_value(value) <= 127)
704 result->oprs[operand].type |= SBYTE;
705 }
706 }
707 } else { /* it's a register */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000708
H. Peter Anvine2c80182005-01-15 22:15:51 +0000709 if (value->type >= EXPR_SIMPLE || value->value != 1) {
710 error(ERR_NONFATAL, "invalid operand type");
711 result->opcode = -1;
712 return result;
713 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000714
H. Peter Anvine2c80182005-01-15 22:15:51 +0000715 /*
716 * check that its only 1 register, not an expression...
717 */
718 for (i = 1; value[i].type; i++)
719 if (value[i].value) {
720 error(ERR_NONFATAL, "invalid operand type");
721 result->opcode = -1;
722 return result;
723 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000724
H. Peter Anvine2c80182005-01-15 22:15:51 +0000725 /* clear overrides, except TO which applies to FPU regs */
726 if (result->oprs[operand].type & ~TO) {
727 /*
728 * we want to produce a warning iff the specified size
729 * is different from the register size
730 */
731 i = result->oprs[operand].type & SIZE_MASK;
732 } else
733 i = 0;
734
735 result->oprs[operand].type &= TO;
736 result->oprs[operand].type |= REGISTER;
737 result->oprs[operand].type |= reg_flags[value->type];
738 result->oprs[operand].basereg = value->type;
739
740 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
741 error(ERR_WARNING | ERR_PASS1,
742 "register size specification ignored");
743 }
744 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000745 }
746
H. Peter Anvine2c80182005-01-15 22:15:51 +0000747 result->operands = operand; /* set operand count */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000748
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 while (operand < 3) /* clear remaining operands */
750 result->oprs[operand++].type = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000751
752 /*
753 * Transform RESW, RESD, RESQ, REST into RESB.
754 */
755 switch (result->opcode) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000756 case I_RESW:
757 result->opcode = I_RESB;
758 result->oprs[0].offset *= 2;
759 break;
760 case I_RESD:
761 result->opcode = I_RESB;
762 result->oprs[0].offset *= 4;
763 break;
764 case I_RESQ:
765 result->opcode = I_RESB;
766 result->oprs[0].offset *= 8;
767 break;
768 case I_REST:
769 result->opcode = I_RESB;
770 result->oprs[0].offset *= 10;
771 break;
H. Peter Anvin16b0a332007-09-12 20:27:41 -0700772 default:
773 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000774 }
775
776 return result;
777}
778
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779static int is_comma_next(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000780{
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000781 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000782 int i;
783 struct tokenval tv;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000784
H. Peter Anvin76690a12002-04-30 20:52:49 +0000785 p = stdscan_bufptr;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 i = stdscan(NULL, &tv);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000787 stdscan_bufptr = p;
788 return (i == ',' || i == ';' || !i);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000789}
790
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791void cleanup_insn(insn * i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000792{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000793 extop *e;
794
795 while (i->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 e = i->eops;
797 i->eops = i->eops->next;
798 nasm_free(e);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799 }
800}