NASM 0.98p3.5
[nasm.git] / eval.c
bloba44ff7b3f2ff2a21e9861a39bee94981399774c2
1 /* eval.c expression evaluator 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 "eval.h"
20 #include "labels.h"
22 #define TEMPEXPRS_DELTA 128
23 #define TEMPEXPR_DELTA 8
25 static scanner scan; /* Address of scanner routine */
26 static efunc error; /* Address of error reporting routine */
27 static lfunc labelfunc; /* Address of label routine */
29 static struct ofmt *outfmt; /* Structure of addresses of output routines */
31 static expr **tempexprs = NULL;
32 static int ntempexprs;
33 static int tempexprs_size = 0;
35 static expr *tempexpr;
36 static int ntempexpr;
37 static int tempexpr_size;
39 static struct tokenval *tokval; /* The current token */
40 static int i; /* The t_type of tokval */
42 static void *scpriv;
43 static loc_t *location; /* Pointer to current line's segment,offset */
44 static int *opflags;
46 static struct eval_hints *hint;
49 * Unimportant cleanup is done to avoid confusing people who are trying
50 * to debug real memory leaks
52 void eval_cleanup(void)
54 while (ntempexprs)
55 nasm_free (tempexprs[--ntempexprs]);
56 nasm_free (tempexprs);
60 * Construct a temporary expression.
62 static void begintemp(void)
64 tempexpr = NULL;
65 tempexpr_size = ntempexpr = 0;
68 static void addtotemp(long type, long value)
70 while (ntempexpr >= tempexpr_size) {
71 tempexpr_size += TEMPEXPR_DELTA;
72 tempexpr = nasm_realloc(tempexpr,
73 tempexpr_size*sizeof(*tempexpr));
75 tempexpr[ntempexpr].type = type;
76 tempexpr[ntempexpr++].value = value;
79 static expr *finishtemp(void)
81 addtotemp (0L, 0L); /* terminate */
82 while (ntempexprs >= tempexprs_size) {
83 tempexprs_size += TEMPEXPRS_DELTA;
84 tempexprs = nasm_realloc(tempexprs,
85 tempexprs_size*sizeof(*tempexprs));
87 return tempexprs[ntempexprs++] = tempexpr;
91 * Add two vector datatypes. We have some bizarre behaviour on far-
92 * absolute segment types: we preserve them during addition _only_
93 * if one of the segments is a truly pure scalar.
95 static expr *add_vectors(expr *p, expr *q)
97 int preserve;
99 preserve = is_really_simple(p) || is_really_simple(q);
101 begintemp();
103 while (p->type && q->type &&
104 p->type < EXPR_SEGBASE+SEG_ABS &&
105 q->type < EXPR_SEGBASE+SEG_ABS)
107 int lasttype;
109 if (p->type > q->type) {
110 addtotemp(q->type, q->value);
111 lasttype = q++->type;
112 } else if (p->type < q->type) {
113 addtotemp(p->type, p->value);
114 lasttype = p++->type;
115 } else { /* *p and *q have same type */
116 long sum = p->value + q->value;
117 if (sum)
118 addtotemp(p->type, sum);
119 lasttype = p->type;
120 p++, q++;
122 if (lasttype == EXPR_UNKNOWN) {
123 return finishtemp();
126 while (p->type &&
127 (preserve || p->type < EXPR_SEGBASE+SEG_ABS))
129 addtotemp(p->type, p->value);
130 p++;
132 while (q->type &&
133 (preserve || q->type < EXPR_SEGBASE+SEG_ABS))
135 addtotemp(q->type, q->value);
136 q++;
139 return finishtemp();
143 * Multiply a vector by a scalar. Strip far-absolute segment part
144 * if present.
146 * Explicit treatment of UNKNOWN is not required in this routine,
147 * since it will silently do the Right Thing anyway.
149 * If `affect_hints' is set, we also change the hint type to
150 * NOTBASE if a MAKEBASE hint points at a register being
151 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
152 * as the base register.
154 static expr *scalar_mult(expr *vect, long scalar, int affect_hints)
156 expr *p = vect;
158 while (p->type && p->type < EXPR_SEGBASE+SEG_ABS) {
159 p->value = scalar * (p->value);
160 if (hint && hint->type == EAH_MAKEBASE &&
161 p->type == hint->base && affect_hints)
162 hint->type = EAH_NOTBASE;
163 p++;
165 p->type = 0;
167 return vect;
170 static expr *scalarvect (long scalar)
172 begintemp();
173 addtotemp(EXPR_SIMPLE, scalar);
174 return finishtemp();
177 static expr *unknown_expr (void)
179 begintemp();
180 addtotemp(EXPR_UNKNOWN, 1L);
181 return finishtemp();
185 * The SEG operator: calculate the segment part of a relocatable
186 * value. Return NULL, as usual, if an error occurs. Report the
187 * error too.
189 static expr *segment_part (expr *e)
191 long seg;
193 if (is_unknown(e))
194 return unknown_expr();
196 if (!is_reloc(e)) {
197 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
198 return NULL;
201 seg = reloc_seg(e);
202 if (seg == NO_SEG) {
203 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
204 return NULL;
205 } else if (seg & SEG_ABS) {
206 return scalarvect(seg & ~SEG_ABS);
207 } else if (seg & 1) {
208 error(ERR_NONFATAL, "SEG applied to something which"
209 " is already a segment base");
210 return NULL;
212 else {
213 long base = outfmt->segbase(seg+1);
215 begintemp();
216 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE+base), 1L);
217 return finishtemp();
222 * Recursive-descent parser. Called with a single boolean operand,
223 * which is TRUE if the evaluation is critical (i.e. unresolved
224 * symbols are an error condition). Must update the global `i' to
225 * reflect the token after the parsed string. May return NULL.
227 * evaluate() should report its own errors: on return it is assumed
228 * that if NULL has been returned, the error has already been
229 * reported.
233 * Grammar parsed is:
235 * expr : bexpr [ WRT expr6 ]
236 * bexpr : rexp0 or expr0 depending on relative-mode setting
237 * rexp0 : rexp1 [ {||} rexp1...]
238 * rexp1 : rexp2 [ {^^} rexp2...]
239 * rexp2 : rexp3 [ {&&} rexp3...]
240 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
241 * expr0 : expr1 [ {|} expr1...]
242 * expr1 : expr2 [ {^} expr2...]
243 * expr2 : expr3 [ {&} expr3...]
244 * expr3 : expr4 [ {<<,>>} expr4...]
245 * expr4 : expr5 [ {+,-} expr5...]
246 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
247 * expr6 : { ~,+,-,SEG } expr6
248 * | (bexpr)
249 * | symbol
250 * | $
251 * | number
254 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
256 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
257 static expr *expr4(int), *expr5(int), *expr6(int);
259 static expr *(*bexpr)(int);
261 static expr *rexp0(int critical)
263 expr *e, *f;
265 e = rexp1(critical);
266 if (!e)
267 return NULL;
269 while (i == TOKEN_DBL_OR)
271 i = scan(scpriv, tokval);
272 f = rexp1(critical);
273 if (!f)
274 return NULL;
275 if (!(is_simple(e) || is_just_unknown(e)) ||
276 !(is_simple(f) || is_just_unknown(f)))
278 error(ERR_NONFATAL, "`|' operator may only be applied to"
279 " scalar values");
282 if (is_just_unknown(e) || is_just_unknown(f))
283 e = unknown_expr();
284 else
285 e = scalarvect ((long) (reloc_value(e) || reloc_value(f)));
287 return e;
290 static expr *rexp1(int critical)
292 expr *e, *f;
294 e = rexp2(critical);
295 if (!e)
296 return NULL;
298 while (i == TOKEN_DBL_XOR)
300 i = scan(scpriv, tokval);
301 f = rexp2(critical);
302 if (!f)
303 return NULL;
304 if (!(is_simple(e) || is_just_unknown(e)) ||
305 !(is_simple(f) || is_just_unknown(f)))
307 error(ERR_NONFATAL, "`^' operator may only be applied to"
308 " scalar values");
311 if (is_just_unknown(e) || is_just_unknown(f))
312 e = unknown_expr();
313 else
314 e = scalarvect ((long) (!reloc_value(e) ^ !reloc_value(f)));
316 return e;
319 static expr *rexp2(int critical)
321 expr *e, *f;
323 e = rexp3(critical);
324 if (!e)
325 return NULL;
326 while (i == TOKEN_DBL_AND)
328 i = scan(scpriv, tokval);
329 f = rexp3(critical);
330 if (!f)
331 return NULL;
332 if (!(is_simple(e) || is_just_unknown(e)) ||
333 !(is_simple(f) || is_just_unknown(f)))
335 error(ERR_NONFATAL, "`&' operator may only be applied to"
336 " scalar values");
338 if (is_just_unknown(e) || is_just_unknown(f))
339 e = unknown_expr();
340 else
341 e = scalarvect ((long) (reloc_value(e) && reloc_value(f)));
343 return e;
346 static expr *rexp3(int critical)
348 expr *e, *f;
349 long v;
351 e = expr0(critical);
352 if (!e)
353 return NULL;
355 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
356 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE)
358 int j = i;
359 i = scan(scpriv, tokval);
360 f = expr0(critical);
361 if (!f)
362 return NULL;
364 e = add_vectors (e, scalar_mult(f, -1L, FALSE));
366 switch (j)
368 case TOKEN_EQ: case TOKEN_NE:
369 if (is_unknown(e))
370 v = -1; /* means unknown */
371 else if (!is_really_simple(e) || reloc_value(e) != 0)
372 v = (j == TOKEN_NE); /* unequal, so return TRUE if NE */
373 else
374 v = (j == TOKEN_EQ); /* equal, so return TRUE if EQ */
375 break;
376 default:
377 if (is_unknown(e))
378 v = -1; /* means unknown */
379 else if (!is_really_simple(e)) {
380 error(ERR_NONFATAL, "`%s': operands differ by a non-scalar",
381 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" :
382 j == TOKEN_GE ? ">=" : ">"));
383 v = 0; /* must set it to _something_ */
384 } else {
385 int vv = reloc_value(e);
386 if (vv == 0)
387 v = (j == TOKEN_LE || j == TOKEN_GE);
388 else if (vv > 0)
389 v = (j == TOKEN_GE || j == TOKEN_GT);
390 else /* vv < 0 */
391 v = (j == TOKEN_LE || j == TOKEN_LT);
393 break;
396 if (v == -1)
397 e = unknown_expr();
398 else
399 e = scalarvect(v);
401 return e;
404 static expr *expr0(int critical)
406 expr *e, *f;
408 e = expr1(critical);
409 if (!e)
410 return NULL;
412 while (i == '|')
414 i = scan(scpriv, tokval);
415 f = expr1(critical);
416 if (!f)
417 return NULL;
418 if (!(is_simple(e) || is_just_unknown(e)) ||
419 !(is_simple(f) || is_just_unknown(f)))
421 error(ERR_NONFATAL, "`|' operator may only be applied to"
422 " scalar values");
424 if (is_just_unknown(e) || is_just_unknown(f))
425 e = unknown_expr();
426 else
427 e = scalarvect (reloc_value(e) | reloc_value(f));
429 return e;
432 static expr *expr1(int critical)
434 expr *e, *f;
436 e = expr2(critical);
437 if (!e)
438 return NULL;
440 while (i == '^') {
441 i = scan(scpriv, tokval);
442 f = expr2(critical);
443 if (!f)
444 return NULL;
445 if (!(is_simple(e) || is_just_unknown(e)) ||
446 !(is_simple(f) || is_just_unknown(f)))
448 error(ERR_NONFATAL, "`^' operator may only be applied to"
449 " scalar values");
451 if (is_just_unknown(e) || is_just_unknown(f))
452 e = unknown_expr();
453 else
454 e = scalarvect (reloc_value(e) ^ reloc_value(f));
456 return e;
459 static expr *expr2(int critical)
461 expr *e, *f;
463 e = expr3(critical);
464 if (!e)
465 return NULL;
467 while (i == '&') {
468 i = scan(scpriv, tokval);
469 f = expr3(critical);
470 if (!f)
471 return NULL;
472 if (!(is_simple(e) || is_just_unknown(e)) ||
473 !(is_simple(f) || is_just_unknown(f)))
475 error(ERR_NONFATAL, "`&' operator may only be applied to"
476 " scalar values");
478 if (is_just_unknown(e) || is_just_unknown(f))
479 e = unknown_expr();
480 else
481 e = scalarvect (reloc_value(e) & reloc_value(f));
483 return e;
486 static expr *expr3(int critical)
488 expr *e, *f;
490 e = expr4(critical);
491 if (!e)
492 return NULL;
494 while (i == TOKEN_SHL || i == TOKEN_SHR)
496 int j = i;
497 i = scan(scpriv, tokval);
498 f = expr4(critical);
499 if (!f)
500 return NULL;
501 if (!(is_simple(e) || is_just_unknown(e)) ||
502 !(is_simple(f) || is_just_unknown(f)))
504 error(ERR_NONFATAL, "shift operator may only be applied to"
505 " scalar values");
506 } else if (is_just_unknown(e) || is_just_unknown(f)) {
507 e = unknown_expr();
508 } else switch (j) {
509 case TOKEN_SHL:
510 e = scalarvect (reloc_value(e) << reloc_value(f));
511 break;
512 case TOKEN_SHR:
513 e = scalarvect (((unsigned long)reloc_value(e)) >>
514 reloc_value(f));
515 break;
518 return e;
521 static expr *expr4(int critical)
523 expr *e, *f;
525 e = expr5(critical);
526 if (!e)
527 return NULL;
528 while (i == '+' || i == '-')
530 int j = i;
531 i = scan(scpriv, tokval);
532 f = expr5(critical);
533 if (!f)
534 return NULL;
535 switch (j) {
536 case '+':
537 e = add_vectors (e, f);
538 break;
539 case '-':
540 e = add_vectors (e, scalar_mult(f, -1L, FALSE));
541 break;
544 return e;
547 static expr *expr5(int critical)
549 expr *e, *f;
551 e = expr6(critical);
552 if (!e)
553 return NULL;
554 while (i == '*' || i == '/' || i == '%' ||
555 i == TOKEN_SDIV || i == TOKEN_SMOD)
557 int j = i;
558 i = scan(scpriv, tokval);
559 f = expr6(critical);
560 if (!f)
561 return NULL;
562 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
563 !(is_simple(f) || is_just_unknown(f))))
565 error(ERR_NONFATAL, "division operator may only be applied to"
566 " scalar values");
567 return NULL;
569 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
570 error(ERR_NONFATAL, "division by zero");
571 return NULL;
573 switch (j) {
574 case '*':
575 if (is_simple(e))
576 e = scalar_mult (f, reloc_value(e), TRUE);
577 else if (is_simple(f))
578 e = scalar_mult (e, reloc_value(f), TRUE);
579 else if (is_just_unknown(e) && is_just_unknown(f))
580 e = unknown_expr();
581 else {
582 error(ERR_NONFATAL, "unable to multiply two "
583 "non-scalar objects");
584 return NULL;
586 break;
587 case '/':
588 if (is_just_unknown(e) || is_just_unknown(f))
589 e = unknown_expr();
590 else
591 e = scalarvect (((unsigned long)reloc_value(e)) /
592 ((unsigned long)reloc_value(f)));
593 break;
594 case '%':
595 if (is_just_unknown(e) || is_just_unknown(f))
596 e = unknown_expr();
597 else
598 e = scalarvect (((unsigned long)reloc_value(e)) %
599 ((unsigned long)reloc_value(f)));
600 break;
601 case TOKEN_SDIV:
602 if (is_just_unknown(e) || is_just_unknown(f))
603 e = unknown_expr();
604 else
605 e = scalarvect (((signed long)reloc_value(e)) /
606 ((signed long)reloc_value(f)));
607 break;
608 case TOKEN_SMOD:
609 if (is_just_unknown(e) || is_just_unknown(f))
610 e = unknown_expr();
611 else
612 e = scalarvect (((signed long)reloc_value(e)) %
613 ((signed long)reloc_value(f)));
614 break;
617 return e;
620 static expr *expr6(int critical)
622 long type;
623 expr *e;
624 long label_seg, label_ofs;
626 if (i == '-') {
627 i = scan(scpriv, tokval);
628 e = expr6(critical);
629 if (!e)
630 return NULL;
631 return scalar_mult (e, -1L, FALSE);
632 } else if (i == '+') {
633 i = scan(scpriv, tokval);
634 return expr6(critical);
635 } else if (i == '~') {
636 i = scan(scpriv, tokval);
637 e = expr6(critical);
638 if (!e)
639 return NULL;
640 if (is_just_unknown(e))
641 return unknown_expr();
642 else if (!is_simple(e)) {
643 error(ERR_NONFATAL, "`~' operator may only be applied to"
644 " scalar values");
645 return NULL;
647 return scalarvect(~reloc_value(e));
648 } else if (i == TOKEN_SEG) {
649 i = scan(scpriv, tokval);
650 e = expr6(critical);
651 if (!e)
652 return NULL;
653 e = segment_part(e);
654 if (is_unknown(e) && critical) {
655 error(ERR_NONFATAL, "unable to determine segment base");
656 return NULL;
658 return e;
659 } else if (i == '(') {
660 i = scan(scpriv, tokval);
661 e = bexpr(critical);
662 if (!e)
663 return NULL;
664 if (i != ')') {
665 error(ERR_NONFATAL, "expecting `)'");
666 return NULL;
668 i = scan(scpriv, tokval);
669 return e;
671 else if (i == TOKEN_NUM || i == TOKEN_REG || i == TOKEN_ID ||
672 i == TOKEN_HERE || i == TOKEN_BASE)
674 begintemp();
675 switch (i) {
676 case TOKEN_NUM:
677 addtotemp(EXPR_SIMPLE, tokval->t_integer);
678 break;
679 case TOKEN_REG:
680 addtotemp(tokval->t_integer, 1L);
681 if (hint && hint->type == EAH_NOHINT)
682 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
683 break;
684 case TOKEN_ID:
685 case TOKEN_HERE:
686 case TOKEN_BASE:
688 * If !location->known, this indicates that no
689 * symbol, Here or Base references are valid because we
690 * are in preprocess-only mode.
692 if (!location->known) {
693 error(ERR_NONFATAL,
694 "%s not supported in preprocess-only mode",
695 (i == TOKEN_ID ? "symbol references" :
696 i == TOKEN_HERE ? "`$'" : "`$$'"));
697 addtotemp(EXPR_UNKNOWN, 1L);
698 break;
701 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
702 if (i == TOKEN_BASE)
704 label_seg = location->segment;
705 label_ofs = 0;
706 } else if (i == TOKEN_HERE) {
707 label_seg = location->segment;
708 label_ofs = location->offset;
709 } else {
710 if (!labelfunc(tokval->t_charptr,&label_seg,&label_ofs))
712 if (critical == 2) {
713 error (ERR_NONFATAL, "symbol `%s' undefined",
714 tokval->t_charptr);
715 return NULL;
716 } else if (critical == 1) {
717 error (ERR_NONFATAL,
718 "symbol `%s' not defined before use",
719 tokval->t_charptr);
720 return NULL;
721 } else {
722 if (opflags)
723 *opflags |= 1;
724 type = EXPR_UNKNOWN;
725 label_seg = NO_SEG;
726 label_ofs = 1;
729 if (opflags && is_extern (tokval->t_charptr))
730 *opflags |= OPFLAG_EXTERN;
732 addtotemp(type, label_ofs);
733 if (label_seg!=NO_SEG)
734 addtotemp(EXPR_SEGBASE + label_seg, 1L);
735 break;
737 i = scan(scpriv, tokval);
738 return finishtemp();
739 } else {
740 error(ERR_NONFATAL, "expression syntax error");
741 return NULL;
745 void eval_global_info (struct ofmt *output, lfunc lookup_label, loc_t *locp)
747 outfmt = output;
748 labelfunc = lookup_label;
749 location = locp;
752 expr *evaluate (scanner sc, void *scprivate, struct tokenval *tv,
753 int *fwref, int critical, efunc report_error,
754 struct eval_hints *hints)
756 expr *e;
757 expr *f = NULL;
759 hint = hints;
760 if (hint)
761 hint->type = EAH_NOHINT;
763 if (critical & 0x10) {
764 critical &= ~0x10;
765 bexpr = rexp0;
766 } else
767 bexpr = expr0;
769 scan = sc;
770 scpriv = scprivate;
771 tokval = tv;
772 error = report_error;
773 opflags = fwref;
775 if (tokval->t_type == TOKEN_INVALID)
776 i = scan(scpriv, tokval);
777 else
778 i = tokval->t_type;
780 while (ntempexprs) /* initialise temporary storage */
781 nasm_free (tempexprs[--ntempexprs]);
783 e = bexpr (critical);
784 if (!e)
785 return NULL;
787 if (i == TOKEN_WRT) {
788 i = scan(scpriv, tokval); /* eat the WRT */
789 f = expr6 (critical);
790 if (!f)
791 return NULL;
793 e = scalar_mult (e, 1L, FALSE); /* strip far-absolute segment part */
794 if (f) {
795 expr *g;
796 if (is_just_unknown(f))
797 g = unknown_expr();
798 else {
799 long value;
800 begintemp();
801 if (!is_reloc(f)) {
802 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
803 return NULL;
805 value = reloc_seg(f);
806 if (value == NO_SEG)
807 value = reloc_value(f) | SEG_ABS;
808 else if (!(value & SEG_ABS) && !(value % 2) && critical)
810 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
811 return NULL;
813 addtotemp(EXPR_WRT, value);
814 g = finishtemp();
816 e = add_vectors (e, g);
818 return e;