NASM 0.98p3.3
[nasm/autotest.git] / parser.c
blob704e2eb4dd030fb53e5741f047d2100164dc5ae1
1 /* parser.c source line parser for the Netwide Assembler
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.
8 * initial version 27/iii/95 by Simon Tatham
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <ctype.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "parser.h"
20 #include "float.h"
22 static long reg_flags[] = { /* sizes and special flags */
23 0, REG8, REG_AL, REG_AX, REG8, REG8, REG16, REG16, REG8, REG_CL,
24 REG_CREG, REG_CREG, REG_CREG, REG_CR4, REG_CS, REG_CX, REG8,
25 REG16, REG8, REG_DREG, REG_DREG, REG_DREG, REG_DREG, REG_DREG,
26 REG_DREG, REG_DESS, REG_DX, REG_EAX, REG32, REG32, REG_ECX,
27 REG32, REG32, REG_DESS, REG32, REG32, REG_FSGS, REG_FSGS,
28 MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG, MMXREG,
29 REG16, REG16, REG_DESS, FPU0, FPUREG, FPUREG, FPUREG, FPUREG,
30 FPUREG, FPUREG, FPUREG, REG_TREG, REG_TREG, REG_TREG, REG_TREG,
31 REG_TREG
34 enum { /* special tokens */
35 S_BYTE, S_DWORD, S_FAR, S_LONG, S_NEAR, S_NOSPLIT, S_QWORD,
36 S_SHORT, S_TO, S_TWORD, S_WORD
39 static int is_comma_next (void);
41 static int i;
42 static struct tokenval tokval;
43 static efunc error;
44 static struct ofmt *outfmt; /* Structure of addresses of output routines */
45 static loc_t *location; /* Pointer to current line's segment,offset */
47 void parser_global_info (struct ofmt *output, loc_t *locp)
49 outfmt = output;
50 location = locp;
53 insn *parse_line (int pass, char *buffer, insn *result,
54 efunc errfunc, evalfunc evaluate, ldfunc ldef)
56 int operand;
57 int critical;
58 struct eval_hints hints;
60 result->forw_ref = FALSE;
61 error = errfunc;
63 stdscan_reset();
64 stdscan_bufptr = buffer;
65 i = stdscan(NULL, &tokval);
67 result->label = NULL; /* Assume no label */
68 result->eops = NULL; /* must do this, whatever happens */
69 result->operands = 0; /* must initialise this */
71 if (i==0) { /* blank line - ignore */
72 result->opcode = -1; /* and no instruction either */
73 return result;
75 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
76 (i!=TOKEN_REG || (REG_SREG & ~reg_flags[tokval.t_integer]))) {
77 error (ERR_NONFATAL, "label or instruction expected"
78 " at start of line");
79 result->opcode = -1;
80 return result;
83 if (i == TOKEN_ID) { /* there's a label here */
84 result->label = tokval.t_charptr;
85 i = stdscan(NULL, &tokval);
86 if (i == ':') { /* skip over the optional colon */
87 i = stdscan(NULL, &tokval);
88 } else if (i == 0) {
89 error (ERR_WARNING|ERR_WARN_OL|ERR_PASS1,
90 "label alone on a line without a colon might be in error");
92 if (i != TOKEN_INSN || tokval.t_integer != I_EQU)
95 * FIXME: location->segment could be NO_SEG, in which case
96 * it is possible we should be passing 'abs_seg'. Look into this.
97 * Work out whether that is *really* what we should be doing.
98 * Generally fix things. I think this is right as it is, but
99 * am still not certain.
101 ldef (result->label, location->segment,
102 location->offset, NULL, TRUE, FALSE, outfmt, errfunc);
106 if (i==0) {
107 result->opcode = -1; /* this line contains just a label */
108 return result;
111 result->nprefix = 0;
112 result->times = 1L;
114 while (i == TOKEN_PREFIX ||
115 (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer])))
118 * Handle special case: the TIMES prefix.
120 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
121 expr *value;
123 i = stdscan(NULL, &tokval);
124 value = evaluate (stdscan, NULL, &tokval, NULL, pass, error, NULL);
125 i = tokval.t_type;
126 if (!value) { /* but, error in evaluator */
127 result->opcode = -1; /* unrecoverable parse error: */
128 return result; /* ignore this instruction */
130 if (!is_simple (value)) {
131 error (ERR_NONFATAL,
132 "non-constant argument supplied to TIMES");
133 result->times = 1L;
134 } else {
135 result->times = value->value;
136 if (value->value < 0) {
137 error(ERR_NONFATAL, "TIMES value %d is negative",
138 value->value);
139 result->times = 0;
142 } else {
143 if (result->nprefix == MAXPREFIX)
144 error (ERR_NONFATAL,
145 "instruction has more than %d prefixes", MAXPREFIX);
146 else
147 result->prefixes[result->nprefix++] = tokval.t_integer;
148 i = stdscan(NULL, &tokval);
152 if (i != TOKEN_INSN) {
153 if (result->nprefix > 0 && i == 0) {
155 * Instruction prefixes are present, but no actual
156 * instruction. This is allowed: at this point we
157 * invent a notional instruction of RESB 0.
159 result->opcode = I_RESB;
160 result->operands = 1;
161 result->oprs[0].type = IMMEDIATE;
162 result->oprs[0].offset = 0L;
163 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
164 return result;
165 } else {
166 error (ERR_NONFATAL, "parser: instruction expected");
167 result->opcode = -1;
168 return result;
172 result->opcode = tokval.t_integer;
173 result->condition = tokval.t_inttwo;
176 * RESB, RESW and RESD cannot be satisfied with incorrectly
177 * evaluated operands, since the correct values _must_ be known
178 * on the first pass. Hence, even in pass one, we set the
179 * `critical' flag on calling evaluate(), so that it will bomb
180 * out on undefined symbols. Nasty, but there's nothing we can
181 * do about it.
183 * For the moment, EQU has the same difficulty, so we'll
184 * include that.
186 if (result->opcode == I_RESB ||
187 result->opcode == I_RESW ||
188 result->opcode == I_RESD ||
189 result->opcode == I_RESQ ||
190 result->opcode == I_REST ||
191 result->opcode == I_EQU)
193 critical = pass;
195 else
196 critical = (pass==2 ? 2 : 0);
198 if (result->opcode == I_DB ||
199 result->opcode == I_DW ||
200 result->opcode == I_DD ||
201 result->opcode == I_DQ ||
202 result->opcode == I_DT ||
203 result->opcode == I_INCBIN)
205 extop *eop, **tail = &result->eops, **fixptr;
206 int oper_num = 0;
208 result->eops_float = FALSE;
211 * Begin to read the DB/DW/DD/DQ/DT/INCBIN operands.
213 while (1) {
214 i = stdscan(NULL, &tokval);
215 if (i == 0)
216 break;
217 fixptr = tail;
218 eop = *tail = nasm_malloc(sizeof(extop));
219 tail = &eop->next;
220 eop->next = NULL;
221 eop->type = EOT_NOTHING;
222 oper_num++;
224 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
225 eop->type = EOT_DB_STRING;
226 eop->stringval = tokval.t_charptr;
227 eop->stringlen = tokval.t_inttwo;
228 i = stdscan(NULL, &tokval); /* eat the comma */
229 continue;
232 if ((i == TOKEN_FLOAT && is_comma_next()) || i == '-') {
233 long sign = +1L;
235 if (i == '-') {
236 char *save = stdscan_bufptr;
237 i = stdscan(NULL, &tokval);
238 sign = -1L;
239 if (i != TOKEN_FLOAT || !is_comma_next()) {
240 stdscan_bufptr = save;
241 i = tokval.t_type = '-';
245 if (i == TOKEN_FLOAT) {
246 eop->type = EOT_DB_STRING;
247 result->eops_float = TRUE;
248 if (result->opcode == I_DD)
249 eop->stringlen = 4;
250 else if (result->opcode == I_DQ)
251 eop->stringlen = 8;
252 else if (result->opcode == I_DT)
253 eop->stringlen = 10;
254 else {
255 error(ERR_NONFATAL, "floating-point constant"
256 " encountered in `D%c' instruction",
257 result->opcode == I_DW ? 'W' : 'B');
259 * fix suggested by Pedro Gimeno... original line
260 * was:
261 * eop->type = EOT_NOTHING;
263 eop->stringlen = 0;
265 eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen);
266 tail = &eop->next;
267 *fixptr = eop;
268 eop->stringval = (char *)eop + sizeof(extop);
269 if (eop->stringlen < 4 ||
270 !float_const (tokval.t_charptr, sign,
271 (unsigned char *)eop->stringval,
272 eop->stringlen, error))
273 eop->type = EOT_NOTHING;
274 i = stdscan(NULL, &tokval); /* eat the comma */
275 continue;
279 /* anything else */
281 expr *value;
282 value = evaluate (stdscan, NULL, &tokval, NULL,
283 critical, error, NULL);
284 i = tokval.t_type;
285 if (!value) { /* error in evaluator */
286 result->opcode = -1;/* unrecoverable parse error: */
287 return result; /* ignore this instruction */
289 if (is_unknown(value)) {
290 eop->type = EOT_DB_NUMBER;
291 eop->offset = 0; /* doesn't matter what we put */
292 eop->segment = eop->wrt = NO_SEG; /* likewise */
293 } else if (is_reloc(value)) {
294 eop->type = EOT_DB_NUMBER;
295 eop->offset = reloc_value(value);
296 eop->segment = reloc_seg(value);
297 eop->wrt = reloc_wrt(value);
298 } else {
299 error (ERR_NONFATAL,
300 "operand %d: expression is not simple"
301 " or relocatable", oper_num);
306 * We're about to call stdscan(), which will eat the
307 * comma that we're currently sitting on between
308 * arguments. However, we'd better check first that it
309 * _is_ a comma.
311 if (i == 0) /* also could be EOL */
312 break;
313 if (i != ',') {
314 error (ERR_NONFATAL, "comma expected after operand %d",
315 oper_num);
316 result->opcode = -1;/* unrecoverable parse error: */
317 return result; /* ignore this instruction */
321 if (result->opcode == I_INCBIN) {
323 * Correct syntax for INCBIN is that there should be
324 * one string operand, followed by one or two numeric
325 * operands.
327 if (!result->eops || result->eops->type != EOT_DB_STRING)
328 error (ERR_NONFATAL, "`incbin' expects a file name");
329 else if (result->eops->next &&
330 result->eops->next->type != EOT_DB_NUMBER)
331 error (ERR_NONFATAL, "`incbin': second parameter is",
332 " non-numeric");
333 else if (result->eops->next && result->eops->next->next &&
334 result->eops->next->next->type != EOT_DB_NUMBER)
335 error (ERR_NONFATAL, "`incbin': third parameter is",
336 " non-numeric");
337 else if (result->eops->next && result->eops->next->next &&
338 result->eops->next->next->next)
339 error (ERR_NONFATAL, "`incbin': more than three parameters");
340 else
341 return result;
343 * If we reach here, one of the above errors happened.
344 * Throw the instruction away.
346 result->opcode = -1;
347 return result;
348 } else /* DB ... */
349 if (oper_num == 0)
350 error (ERR_WARNING|ERR_PASS1,
351 "no operand for data declaration");
352 else
353 result->operands = oper_num;
355 return result;
358 /* right. Now we begin to parse the operands. There may be up to three
359 * of these, separated by commas, and terminated by a zero token. */
361 for (operand = 0; operand < 3; operand++) {
362 expr *value; /* used most of the time */
363 int mref; /* is this going to be a memory ref? */
364 int bracket; /* is it a [] mref, or a & mref? */
365 int setsize = 0;
367 result->oprs[operand].addr_size = 0;/* have to zero this whatever */
368 result->oprs[operand].eaflags = 0; /* and this */
369 result->oprs[operand].opflags = 0;
371 i = stdscan(NULL, &tokval);
372 if (i == 0) break; /* end of operands: get out of here */
373 result->oprs[operand].type = 0; /* so far, no override */
374 while (i == TOKEN_SPECIAL) {/* size specifiers */
375 switch ((int)tokval.t_integer) {
376 case S_BYTE:
377 if (!setsize) /* we want to use only the first */
378 result->oprs[operand].type |= BITS8;
379 setsize = 1;
380 break;
381 case S_WORD:
382 if (!setsize)
383 result->oprs[operand].type |= BITS16;
384 setsize = 1;
385 break;
386 case S_DWORD:
387 case S_LONG:
388 if (!setsize)
389 result->oprs[operand].type |= BITS32;
390 setsize = 1;
391 break;
392 case S_QWORD:
393 if (!setsize)
394 result->oprs[operand].type |= BITS64;
395 setsize = 1;
396 break;
397 case S_TWORD:
398 if (!setsize)
399 result->oprs[operand].type |= BITS80;
400 setsize = 1;
401 break;
402 case S_TO:
403 result->oprs[operand].type |= TO;
404 break;
405 case S_FAR:
406 result->oprs[operand].type |= FAR;
407 break;
408 case S_NEAR:
409 result->oprs[operand].type |= NEAR;
410 break;
411 case S_SHORT:
412 result->oprs[operand].type |= SHORT;
413 break;
414 default:
415 error (ERR_NONFATAL, "invalid operand size specification");
417 i = stdscan(NULL, &tokval);
420 if (i == '[' || i == '&') { /* memory reference */
421 mref = TRUE;
422 bracket = (i == '[');
423 i = stdscan(NULL, &tokval);
424 if (i == TOKEN_SPECIAL) { /* check for address size override */
425 switch ((int)tokval.t_integer) {
426 case S_NOSPLIT:
427 result->oprs[operand].eaflags |= EAF_TIMESTWO;
428 break;
429 case S_BYTE:
430 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
431 break;
432 case S_WORD:
433 result->oprs[operand].addr_size = 16;
434 result->oprs[operand].eaflags |= EAF_WORDOFFS;
435 break;
436 case S_DWORD:
437 case S_LONG:
438 result->oprs[operand].addr_size = 32;
439 result->oprs[operand].eaflags |= EAF_WORDOFFS;
440 break;
441 default:
442 error (ERR_NONFATAL, "invalid size specification in"
443 " effective address");
445 i = stdscan(NULL, &tokval);
447 } else { /* immediate operand, or register */
448 mref = FALSE;
449 bracket = FALSE; /* placate optimisers */
452 value = evaluate (stdscan, NULL, &tokval,
453 &result->oprs[operand].opflags,
454 critical, error, &hints);
455 i = tokval.t_type;
456 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
457 result->forw_ref = TRUE;
459 if (!value) { /* error in evaluator */
460 result->opcode = -1; /* unrecoverable parse error: */
461 return result; /* ignore this instruction */
463 if (i == ':' && mref) { /* it was seg:offset */
465 * Process the segment override.
467 if (value[1].type!=0 || value->value!=1 ||
468 REG_SREG & ~reg_flags[value->type])
469 error (ERR_NONFATAL, "invalid segment override");
470 else if (result->nprefix == MAXPREFIX)
471 error (ERR_NONFATAL,
472 "instruction has more than %d prefixes",
473 MAXPREFIX);
474 else
475 result->prefixes[result->nprefix++] = value->type;
477 i = stdscan(NULL, &tokval); /* then skip the colon */
478 if (i == TOKEN_SPECIAL) { /* another check for size override */
479 switch ((int)tokval.t_integer) {
480 case S_WORD:
481 result->oprs[operand].addr_size = 16;
482 break;
483 case S_DWORD:
484 case S_LONG:
485 result->oprs[operand].addr_size = 32;
486 break;
487 default:
488 error (ERR_NONFATAL, "invalid size specification in"
489 " effective address");
491 i = stdscan(NULL, &tokval);
493 value = evaluate (stdscan, NULL, &tokval,
494 &result->oprs[operand].opflags,
495 critical, error, &hints);
496 i = tokval.t_type;
497 if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
498 result->forw_ref = TRUE;
500 /* and get the offset */
501 if (!value) { /* but, error in evaluator */
502 result->opcode = -1; /* unrecoverable parse error: */
503 return result; /* ignore this instruction */
506 if (mref && bracket) { /* find ] at the end */
507 if (i != ']') {
508 error (ERR_NONFATAL, "parser: expecting ]");
509 do { /* error recovery again */
510 i = stdscan(NULL, &tokval);
511 } while (i != 0 && i != ',');
512 } else /* we got the required ] */
513 i = stdscan(NULL, &tokval);
514 } else { /* immediate operand */
515 if (i != 0 && i != ',' && i != ':') {
516 error (ERR_NONFATAL, "comma or end of line expected");
517 do { /* error recovery */
518 i = stdscan(NULL, &tokval);
519 } while (i != 0 && i != ',');
520 } else if (i == ':') {
521 result->oprs[operand].type |= COLON;
525 /* now convert the exprs returned from evaluate() into operand
526 * descriptions... */
528 if (mref) { /* it's a memory reference */
529 expr *e = value;
530 int b, i, s; /* basereg, indexreg, scale */
531 long o; /* offset */
533 b = i = -1, o = s = 0;
534 result->oprs[operand].hintbase = hints.base;
535 result->oprs[operand].hinttype = hints.type;
537 if (e->type <= EXPR_REG_END) { /* this bit's a register */
538 if (e->value == 1) /* in fact it can be basereg */
539 b = e->type;
540 else /* no, it has to be indexreg */
541 i = e->type, s = e->value;
542 e++;
544 if (e->type && e->type <= EXPR_REG_END) /* it's a 2nd register */
546 if (b != -1) /* If the first was the base, ... */
547 i = e->type, s = e->value; /* second has to be indexreg */
549 else if (e->value != 1) /* If both want to be index */
551 error(ERR_NONFATAL, "invalid effective address");
552 result->opcode = -1;
553 return result;
555 else
556 b = e->type;
557 e++;
559 if (e->type != 0) { /* is there an offset? */
560 if (e->type <= EXPR_REG_END) /* in fact, is there an error? */
562 error (ERR_NONFATAL, "invalid effective address");
563 result->opcode = -1;
564 return result;
566 else
568 if (e->type == EXPR_UNKNOWN) {
569 o = 0; /* doesn't matter what */
570 result->oprs[operand].wrt = NO_SEG; /* nor this */
571 result->oprs[operand].segment = NO_SEG; /* or this */
572 while (e->type) e++; /* go to the end of the line */
574 else
576 if (e->type == EXPR_SIMPLE) {
577 o = e->value;
578 e++;
580 if (e->type == EXPR_WRT) {
581 result->oprs[operand].wrt = e->value;
582 e++;
583 } else
584 result->oprs[operand].wrt = NO_SEG;
586 * Look for a segment base type.
588 if (e->type && e->type < EXPR_SEGBASE) {
589 error (ERR_NONFATAL, "invalid effective address");
590 result->opcode = -1;
591 return result;
593 while (e->type && e->value == 0)
594 e++;
595 if (e->type && e->value != 1) {
596 error (ERR_NONFATAL, "invalid effective address");
597 result->opcode = -1;
598 return result;
600 if (e->type) {
601 result->oprs[operand].segment =
602 e->type - EXPR_SEGBASE;
603 e++;
604 } else
605 result->oprs[operand].segment = NO_SEG;
606 while (e->type && e->value == 0)
607 e++;
608 if (e->type) {
609 error (ERR_NONFATAL, "invalid effective address");
610 result->opcode = -1;
611 return result;
615 } else {
616 o = 0;
617 result->oprs[operand].wrt = NO_SEG;
618 result->oprs[operand].segment = NO_SEG;
621 if (e->type != 0) { /* there'd better be nothing left! */
622 error (ERR_NONFATAL, "invalid effective address");
623 result->opcode = -1;
624 return result;
627 result->oprs[operand].type |= MEMORY;
628 if (b==-1 && (i==-1 || s==0))
629 result->oprs[operand].type |= MEM_OFFS;
630 result->oprs[operand].basereg = b;
631 result->oprs[operand].indexreg = i;
632 result->oprs[operand].scale = s;
633 result->oprs[operand].offset = o;
635 else /* it's not a memory reference */
637 if (is_just_unknown(value)) { /* it's immediate but unknown */
638 result->oprs[operand].type |= IMMEDIATE;
639 result->oprs[operand].offset = 0; /* don't care */
640 result->oprs[operand].segment = NO_SEG; /* don't care again */
641 result->oprs[operand].wrt = NO_SEG;/* still don't care */
643 else if (is_reloc(value)) /* it's immediate */
645 result->oprs[operand].type |= IMMEDIATE;
646 result->oprs[operand].offset = reloc_value(value);
647 result->oprs[operand].segment = reloc_seg(value);
648 result->oprs[operand].wrt = reloc_wrt(value);
649 if (is_simple(value) && reloc_value(value)==1)
650 result->oprs[operand].type |= UNITY;
652 else /* it's a register */
654 int i;
656 if (value->type>=EXPR_SIMPLE || value->value!=1) {
657 error (ERR_NONFATAL, "invalid operand type");
658 result->opcode = -1;
659 return result;
663 * check that its only 1 register, not an expression...
665 for (i = 1; value[i].type; i++)
666 if (value[i].value) {
667 error (ERR_NONFATAL, "invalid operand type");
668 result->opcode = -1;
669 return result;
672 /* clear overrides, except TO which applies to FPU regs */
673 if (result->oprs[operand].type & ~TO) {
675 * we want to produce a warning iff the specified size
676 * is different from the register size
678 i = result->oprs[operand].type & SIZE_MASK;
680 else
681 i = 0;
683 result->oprs[operand].type &= TO;
684 result->oprs[operand].type |= REGISTER;
685 result->oprs[operand].type |= reg_flags[value->type];
686 result->oprs[operand].basereg = value->type;
688 if (i && (result->oprs[operand].type & SIZE_MASK) != i)
689 error (ERR_WARNING|ERR_PASS1,
690 "register size specification ignored");
695 result->operands = operand; /* set operand count */
697 while (operand<3) /* clear remaining operands */
698 result->oprs[operand++].type = 0;
701 * Transform RESW, RESD, RESQ, REST into RESB.
703 switch (result->opcode) {
704 case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break;
705 case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break;
706 case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break;
707 case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break;
710 return result;
713 static int is_comma_next (void)
715 char *p;
716 int i;
717 struct tokenval tv;
719 p = stdscan_bufptr;
720 i = stdscan (NULL, &tv);
721 stdscan_bufptr = p;
722 return (i == ',' || i == ';' || !i);
725 void cleanup_insn (insn *i)
727 extop *e;
729 while (i->eops) {
730 e = i->eops;
731 i->eops = i->eops->next;
732 nasm_free (e);