NASM 0.96
[nasm.git] / parser.c
blobdb465cd98fe21fb3771965de7d358074e2a3481e
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;
45 insn *parse_line (int pass, char *buffer, insn *result,
46 efunc errfunc, evalfunc evaluate, evalinfofunc einfo) {
47 int operand;
48 int critical;
49 struct eval_hints hints;
51 result->forw_ref = FALSE;
52 error = errfunc;
53 einfo ("", 0L, 0L);
55 stdscan_reset();
56 stdscan_bufptr = buffer;
57 i = stdscan(NULL, &tokval);
59 result->eops = NULL; /* must do this, whatever happens */
60 result->operands = 0; /* must initialise this */
62 if (i==0) { /* blank line - ignore */
63 result->label = NULL; /* so, no label on it */
64 result->opcode = -1; /* and no instruction either */
65 return result;
67 if (i != TOKEN_ID && i != TOKEN_INSN && i != TOKEN_PREFIX &&
68 (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->label = NULL;
72 result->opcode = -1;
73 return result;
76 if (i == TOKEN_ID) { /* there's a label here */
77 result->label = tokval.t_charptr;
78 einfo (result->label, 0L, 0L);
79 i = stdscan(NULL, &tokval);
80 if (i == ':') { /* skip over the optional colon */
81 i = stdscan(NULL, &tokval);
82 } else if (i == 0 && pass == 1) {
83 error (ERR_WARNING|ERR_WARN_OL,
84 "label alone on a line without a colon might be in error");
86 } else /* no label; so, moving swiftly on */
87 result->label = NULL;
89 if (i==0) {
90 result->opcode = -1; /* this line contains just a label */
91 return result;
94 result->nprefix = 0;
95 result->times = 1L;
97 while (i == TOKEN_PREFIX ||
98 (i==TOKEN_REG && !(REG_SREG & ~reg_flags[tokval.t_integer]))) {
100 * Handle special case: the TIMES prefix.
102 if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
103 expr *value;
105 i = stdscan(NULL, &tokval);
106 value = evaluate (stdscan, NULL, &tokval, NULL, pass, error, NULL);
107 i = tokval.t_type;
108 if (!value) { /* but, error in evaluator */
109 result->opcode = -1; /* unrecoverable parse error: */
110 return result; /* ignore this instruction */
112 if (!is_simple (value)) {
113 error (ERR_NONFATAL,
114 "non-constant argument supplied to TIMES");
115 result->times = 1L;
116 } else {
117 result->times = value->value;
118 if (value->value < 0)
119 error(ERR_NONFATAL, "TIMES value %d is negative",
120 value->value);
122 } else {
123 if (result->nprefix == MAXPREFIX)
124 error (ERR_NONFATAL,
125 "instruction has more than %d prefixes", MAXPREFIX);
126 else
127 result->prefixes[result->nprefix++] = tokval.t_integer;
128 i = stdscan(NULL, &tokval);
132 if (i != TOKEN_INSN) {
133 if (result->nprefix > 0 && i == 0) {
135 * Instruction prefixes are present, but no actual
136 * instruction. This is allowed: at this point we
137 * invent a notional instruction of RESB 0.
139 result->opcode = I_RESB;
140 result->operands = 1;
141 result->oprs[0].type = IMMEDIATE;
142 result->oprs[0].offset = 0L;
143 result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
144 return result;
145 } else {
146 error (ERR_NONFATAL, "parser: instruction expected");
147 result->opcode = -1;
148 return result;
152 result->opcode = tokval.t_integer;
153 result->condition = tokval.t_inttwo;
156 * RESB, RESW and RESD cannot be satisfied with incorrectly
157 * evaluated operands, since the correct values _must_ be known
158 * on the first pass. Hence, even in pass one, we set the
159 * `critical' flag on calling evaluate(), so that it will bomb
160 * out on undefined symbols. Nasty, but there's nothing we can
161 * do about it.
163 * For the moment, EQU has the same difficulty, so we'll
164 * include that.
166 if (result->opcode == I_RESB ||
167 result->opcode == I_RESW ||
168 result->opcode == I_RESD ||
169 result->opcode == I_RESQ ||
170 result->opcode == I_REST ||
171 result->opcode == I_EQU)
172 critical = pass;
173 else
174 critical = (pass==2 ? 2 : 0);
176 if (result->opcode == I_DB ||
177 result->opcode == I_DW ||
178 result->opcode == I_DD ||
179 result->opcode == I_DQ ||
180 result->opcode == I_DT ||
181 result->opcode == I_INCBIN) {
182 extop *eop, **tail = &result->eops;
183 int oper_num = 0;
186 * Begin to read the DB/DW/DD/DQ/DT operands.
188 while (1) {
189 i = stdscan(NULL, &tokval);
190 if (i == 0)
191 break;
192 eop = *tail = nasm_malloc(sizeof(extop));
193 tail = &eop->next;
194 eop->next = NULL;
195 eop->type = EOT_NOTHING;
196 oper_num++;
198 if (i == TOKEN_NUM && tokval.t_charptr && is_comma_next()) {
199 eop->type = EOT_DB_STRING;
200 eop->stringval = tokval.t_charptr;
201 eop->stringlen = tokval.t_inttwo;
202 i = stdscan(NULL, &tokval); /* eat the comma */
203 continue;
206 if (i == TOKEN_FLOAT || i == '-') {
207 long sign = +1L;
209 if (i == '-') {
210 char *save = stdscan_bufptr;
211 i = stdscan(NULL, &tokval);
212 sign = -1L;
213 if (i != TOKEN_FLOAT) {
214 stdscan_bufptr = save;
215 i = tokval.t_type = '-';
219 if (i == TOKEN_FLOAT) {
220 eop->type = EOT_DB_STRING;
221 if (result->opcode == I_DD)
222 eop->stringlen = 4;
223 else if (result->opcode == I_DQ)
224 eop->stringlen = 8;
225 else if (result->opcode == I_DT)
226 eop->stringlen = 10;
227 else {
228 error(ERR_NONFATAL, "floating-point constant"
229 " encountered in `D%c' instruction",
230 result->opcode == I_DW ? 'W' : 'B');
231 eop->type = EOT_NOTHING;
233 eop = nasm_realloc(eop, sizeof(extop)+eop->stringlen);
234 eop->stringval = (char *)eop + sizeof(extop);
235 if (!float_const (tokval.t_charptr, sign,
236 (unsigned char *)eop->stringval,
237 eop->stringlen, error))
238 eop->type = EOT_NOTHING;
239 i = stdscan(NULL, &tokval); /* eat the comma */
240 continue;
244 /* anything else */ {
245 expr *value;
246 value = evaluate (stdscan, NULL, &tokval, NULL,
247 critical, error, NULL);
248 i = tokval.t_type;
249 if (!value) { /* error in evaluator */
250 result->opcode = -1;/* unrecoverable parse error: */
251 return result; /* ignore this instruction */
253 if (is_unknown(value)) {
254 eop->type = EOT_DB_NUMBER;
255 eop->offset = 0; /* doesn't matter what we put */
256 eop->segment = eop->wrt = NO_SEG; /* likewise */
257 } else if (is_reloc(value)) {
258 eop->type = EOT_DB_NUMBER;
259 eop->offset = reloc_value(value);
260 eop->segment = reloc_seg(value);
261 eop->wrt = reloc_wrt(value);
262 } else {
263 error (ERR_NONFATAL,
264 "operand %d: expression is not simple"
265 " or relocatable", oper_num);
270 * We're about to call stdscan(), which will eat the
271 * comma that we're currently sitting on between
272 * arguments. However, we'd better check first that it
273 * _is_ a comma.
275 if (i == 0) /* also could be EOL */
276 break;
277 if (i != ',') {
278 error (ERR_NONFATAL, "comma expected after operand %d",
279 oper_num);
280 result->opcode = -1;/* unrecoverable parse error: */
281 return result; /* ignore this instruction */
285 if (result->opcode == I_INCBIN) {
287 * Correct syntax for INCBIN is that there should be
288 * one string operand, followed by one or two numeric
289 * operands.
291 if (!result->eops || result->eops->type != EOT_DB_STRING)
292 error (ERR_NONFATAL, "`incbin' expects a file name");
293 else if (result->eops->next &&
294 result->eops->next->type != EOT_DB_NUMBER)
295 error (ERR_NONFATAL, "`incbin': second parameter is",
296 " non-numeric");
297 else if (result->eops->next && result->eops->next->next &&
298 result->eops->next->next->type != EOT_DB_NUMBER)
299 error (ERR_NONFATAL, "`incbin': third parameter is",
300 " non-numeric");
301 else if (result->eops->next && result->eops->next->next &&
302 result->eops->next->next->next)
303 error (ERR_NONFATAL, "`incbin': more than three parameters");
304 else
305 return result;
307 * If we reach here, one of the above errors happened.
308 * Throw the instruction away.
310 result->opcode = -1;
311 return result;
314 return result;
317 /* right. Now we begin to parse the operands. There may be up to three
318 * of these, separated by commas, and terminated by a zero token. */
320 for (operand = 0; operand < 3; operand++) {
321 expr *value; /* used most of the time */
322 int mref; /* is this going to be a memory ref? */
323 int bracket; /* is it a [] mref, or a & mref? */
325 result->oprs[operand].addr_size = 0;/* have to zero this whatever */
326 result->oprs[operand].eaflags = 0; /* and this */
327 i = stdscan(NULL, &tokval);
328 if (i == 0) break; /* end of operands: get out of here */
329 result->oprs[operand].type = 0; /* so far, no override */
330 while (i == TOKEN_SPECIAL) {/* size specifiers */
331 switch ((int)tokval.t_integer) {
332 case S_BYTE:
333 result->oprs[operand].type |= BITS8;
334 break;
335 case S_WORD:
336 result->oprs[operand].type |= BITS16;
337 break;
338 case S_DWORD:
339 case S_LONG:
340 result->oprs[operand].type |= BITS32;
341 break;
342 case S_QWORD:
343 result->oprs[operand].type |= BITS64;
344 break;
345 case S_TWORD:
346 result->oprs[operand].type |= BITS80;
347 break;
348 case S_TO:
349 result->oprs[operand].type |= TO;
350 break;
351 case S_FAR:
352 result->oprs[operand].type |= FAR;
353 break;
354 case S_NEAR:
355 result->oprs[operand].type |= NEAR;
356 break;
357 case S_SHORT:
358 result->oprs[operand].type |= SHORT;
359 break;
361 i = stdscan(NULL, &tokval);
364 if (i == '[' || i == '&') { /* memory reference */
365 mref = TRUE;
366 bracket = (i == '[');
367 i = stdscan(NULL, &tokval);
368 if (i == TOKEN_SPECIAL) { /* check for address size override */
369 switch ((int)tokval.t_integer) {
370 case S_NOSPLIT:
371 result->oprs[operand].eaflags |= EAF_TIMESTWO;
372 break;
373 case S_BYTE:
374 result->oprs[operand].eaflags |= EAF_BYTEOFFS;
375 break;
376 case S_WORD:
377 result->oprs[operand].addr_size = 16;
378 result->oprs[operand].eaflags |= EAF_WORDOFFS;
379 break;
380 case S_DWORD:
381 case S_LONG:
382 result->oprs[operand].addr_size = 32;
383 result->oprs[operand].eaflags |= EAF_WORDOFFS;
384 break;
385 default:
386 error (ERR_NONFATAL, "invalid size specification in"
387 " effective address");
389 i = stdscan(NULL, &tokval);
391 } else { /* immediate operand, or register */
392 mref = FALSE;
393 bracket = FALSE; /* placate optimisers */
396 value = evaluate (stdscan, NULL, &tokval,
397 &result->forw_ref, critical, error, &hints);
398 i = tokval.t_type;
399 if (!value) { /* error in evaluator */
400 result->opcode = -1; /* unrecoverable parse error: */
401 return result; /* ignore this instruction */
403 if (i == ':' && mref) { /* it was seg:offset */
405 * Process the segment override.
407 if (value[1].type!=0 || value->value!=1 ||
408 REG_SREG & ~reg_flags[value->type])
409 error (ERR_NONFATAL, "invalid segment override");
410 else if (result->nprefix == MAXPREFIX)
411 error (ERR_NONFATAL,
412 "instruction has more than %d prefixes",
413 MAXPREFIX);
414 else
415 result->prefixes[result->nprefix++] = value->type;
417 i = stdscan(NULL, &tokval); /* then skip the colon */
418 if (i == TOKEN_SPECIAL) { /* another check for size override */
419 switch ((int)tokval.t_integer) {
420 case S_WORD:
421 result->oprs[operand].addr_size = 16;
422 break;
423 case S_DWORD:
424 case S_LONG:
425 result->oprs[operand].addr_size = 32;
426 break;
427 default:
428 error (ERR_NONFATAL, "invalid size specification in"
429 " effective address");
431 i = stdscan(NULL, &tokval);
433 value = evaluate (stdscan, NULL, &tokval,
434 &result->forw_ref, critical, error, &hints);
435 i = tokval.t_type;
436 /* and get the offset */
437 if (!value) { /* but, error in evaluator */
438 result->opcode = -1; /* unrecoverable parse error: */
439 return result; /* ignore this instruction */
442 if (mref && bracket) { /* find ] at the end */
443 if (i != ']') {
444 error (ERR_NONFATAL, "parser: expecting ]");
445 do { /* error recovery again */
446 i = stdscan(NULL, &tokval);
447 } while (i != 0 && i != ',');
448 } else /* we got the required ] */
449 i = stdscan(NULL, &tokval);
450 } else { /* immediate operand */
451 if (i != 0 && i != ',' && i != ':') {
452 error (ERR_NONFATAL, "comma or end of line expected");
453 do { /* error recovery */
454 i = stdscan(NULL, &tokval);
455 } while (i != 0 && i != ',');
456 } else if (i == ':') {
457 result->oprs[operand].type |= COLON;
461 /* now convert the exprs returned from evaluate() into operand
462 * descriptions... */
464 if (mref) { /* it's a memory reference */
465 expr *e = value;
466 int b, i, s; /* basereg, indexreg, scale */
467 long o; /* offset */
469 b = i = -1, o = s = 0;
470 result->oprs[operand].hintbase = hints.base;
471 result->oprs[operand].hinttype = hints.type;
473 if (e->type <= EXPR_REG_END) { /* this bit's a register */
474 if (e->value == 1) /* in fact it can be basereg */
475 b = e->type;
476 else /* no, it has to be indexreg */
477 i = e->type, s = e->value;
478 e++;
480 if (e->type && e->type <= EXPR_REG_END) {/* it's a 2nd register */
481 if (e->value != 1) { /* it has to be indexreg */
482 if (i != -1) { /* but it can't be */
483 error(ERR_NONFATAL, "invalid effective address");
484 result->opcode = -1;
485 return result;
486 } else
487 i = e->type, s = e->value;
488 } else { /* it can be basereg */
489 if (b != -1) /* or can it? */
490 i = e->type, s = 1;
491 else
492 b = e->type;
494 e++;
496 if (e->type != 0) { /* is there an offset? */
497 if (e->type <= EXPR_REG_END) {/* in fact, is there an error? */
498 error (ERR_NONFATAL, "invalid effective address");
499 result->opcode = -1;
500 return result;
501 } else {
502 if (e->type == EXPR_UNKNOWN) {
503 o = 0; /* doesn't matter what */
504 result->oprs[operand].wrt = NO_SEG; /* nor this */
505 result->oprs[operand].segment = NO_SEG; /* or this */
506 while (e->type) e++; /* go to the end of the line */
507 } else {
508 if (e->type == EXPR_SIMPLE) {
509 o = e->value;
510 e++;
512 if (e->type == EXPR_WRT) {
513 result->oprs[operand].wrt = e->value;
514 e++;
515 } else
516 result->oprs[operand].wrt = NO_SEG;
518 * Look for a segment base type.
520 if (e->type && e->type < EXPR_SEGBASE) {
521 error (ERR_NONFATAL, "invalid effective address");
522 result->opcode = -1;
523 return result;
525 while (e->type && e->value == 0)
526 e++;
527 if (e->type && e->value != 1) {
528 error (ERR_NONFATAL, "invalid effective address");
529 result->opcode = -1;
530 return result;
532 if (e->type) {
533 result->oprs[operand].segment =
534 e->type - EXPR_SEGBASE;
535 e++;
536 } else
537 result->oprs[operand].segment = NO_SEG;
538 while (e->type && e->value == 0)
539 e++;
540 if (e->type) {
541 error (ERR_NONFATAL, "invalid effective address");
542 result->opcode = -1;
543 return result;
547 } else {
548 o = 0;
549 result->oprs[operand].wrt = NO_SEG;
550 result->oprs[operand].segment = NO_SEG;
553 if (e->type != 0) { /* there'd better be nothing left! */
554 error (ERR_NONFATAL, "invalid effective address");
555 result->opcode = -1;
556 return result;
559 result->oprs[operand].type |= MEMORY;
560 if (b==-1 && (i==-1 || s==0))
561 result->oprs[operand].type |= MEM_OFFS;
562 result->oprs[operand].basereg = b;
563 result->oprs[operand].indexreg = i;
564 result->oprs[operand].scale = s;
565 result->oprs[operand].offset = o;
566 } else { /* it's not a memory reference */
567 if (is_just_unknown(value)) { /* it's immediate but unknown */
568 result->oprs[operand].type |= IMMEDIATE;
569 result->oprs[operand].offset = 0; /* don't care */
570 result->oprs[operand].segment = NO_SEG; /* don't care again */
571 result->oprs[operand].wrt = NO_SEG;/* still don't care */
572 } else if (is_reloc(value)) { /* it's immediate */
573 result->oprs[operand].type |= IMMEDIATE;
574 result->oprs[operand].offset = reloc_value(value);
575 result->oprs[operand].segment = reloc_seg(value);
576 result->oprs[operand].wrt = reloc_wrt(value);
577 if (is_simple(value) && reloc_value(value)==1)
578 result->oprs[operand].type |= UNITY;
579 } else { /* it's a register */
580 if (value->type>=EXPR_SIMPLE || value->value!=1) {
581 error (ERR_NONFATAL, "invalid operand type");
582 result->opcode = -1;
583 return result;
585 /* clear overrides, except TO which applies to FPU regs */
586 result->oprs[operand].type &= TO;
587 result->oprs[operand].type |= REGISTER;
588 result->oprs[operand].type |= reg_flags[value->type];
589 result->oprs[operand].basereg = value->type;
594 result->operands = operand; /* set operand count */
596 while (operand<3) /* clear remaining operands */
597 result->oprs[operand++].type = 0;
600 * Transform RESW, RESD, RESQ, REST into RESB.
602 switch (result->opcode) {
603 case I_RESW: result->opcode=I_RESB; result->oprs[0].offset*=2; break;
604 case I_RESD: result->opcode=I_RESB; result->oprs[0].offset*=4; break;
605 case I_RESQ: result->opcode=I_RESB; result->oprs[0].offset*=8; break;
606 case I_REST: result->opcode=I_RESB; result->oprs[0].offset*=10; break;
609 return result;
612 static int is_comma_next (void) {
613 char *p;
614 int i;
615 struct tokenval tv;
617 p = stdscan_bufptr;
618 i = stdscan (NULL, &tv);
619 stdscan_bufptr = p;
620 return (i == ',' || i == ';' || !i);
623 void cleanup_insn (insn *i) {
624 extop *e;
626 while (i->eops) {
627 e = i->eops;
628 i->eops = i->eops->next;
629 nasm_free (e);