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
26 #define TEMPEXPRS_DELTA 128
27 #define TEMPEXPR_DELTA 8
29 static scanner scan; /* Address of scanner routine */
30 static efunc error; /* Address of error reporting routine */
31 static lfunc labelfunc; /* Address of label routine */
33 static struct ofmt *outfmt; /* Structure of addresses of output routines */
35 static expr **tempexprs = NULL;
36 static int ntempexprs;
37 static int tempexprs_size = 0;
39 static expr *tempexpr;
41 static int tempexpr_size;
43 static struct tokenval *tokval; /* The current token */
44 static int i; /* The t_type of tokval */
47 static struct location *location; /* Pointer to current line's segment,offset */
50 static struct eval_hints *hint;
52 extern int in_abs_seg; /* ABSOLUTE segment flag */
53 extern int32_t abs_seg; /* ABSOLUTE segment */
54 extern int32_t abs_offset; /* ABSOLUTE segment offset */
57 * Unimportant cleanup is done to avoid confusing people who are trying
58 * to debug real memory leaks
60 void eval_cleanup(void)
63 nasm_free(tempexprs[--ntempexprs]);
68 * Construct a temporary expression.
70 static void begintemp(void)
73 tempexpr_size = ntempexpr = 0;
76 static void addtotemp(int32_t type, int64_t value)
78 while (ntempexpr >= tempexpr_size) {
79 tempexpr_size += TEMPEXPR_DELTA;
80 tempexpr = nasm_realloc(tempexpr,
81 tempexpr_size * sizeof(*tempexpr));
83 tempexpr[ntempexpr].type = type;
84 tempexpr[ntempexpr++].value = value;
87 static expr *finishtemp(void)
89 addtotemp(0L, 0L); /* terminate */
90 while (ntempexprs >= tempexprs_size) {
91 tempexprs_size += TEMPEXPRS_DELTA;
92 tempexprs = nasm_realloc(tempexprs,
93 tempexprs_size * sizeof(*tempexprs));
95 return tempexprs[ntempexprs++] = tempexpr;
99 * Add two vector datatypes. We have some bizarre behaviour on far-
100 * absolute segment types: we preserve them during addition _only_
101 * if one of the segments is a truly pure scalar.
103 static expr *add_vectors(expr * p, expr * q)
107 preserve = is_really_simple(p) || is_really_simple(q);
111 while (p->type && q->type &&
112 p->type < EXPR_SEGBASE + SEG_ABS &&
113 q->type < EXPR_SEGBASE + SEG_ABS) {
116 if (p->type > q->type) {
117 addtotemp(q->type, q->value);
118 lasttype = q++->type;
119 } else if (p->type < q->type) {
120 addtotemp(p->type, p->value);
121 lasttype = p++->type;
122 } else { /* *p and *q have same type */
123 int64_t sum = p->value + q->value;
125 addtotemp(p->type, sum);
129 if (lasttype == EXPR_UNKNOWN) {
133 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
134 addtotemp(p->type, p->value);
137 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
138 addtotemp(q->type, q->value);
146 * Multiply a vector by a scalar. Strip far-absolute segment part
149 * Explicit treatment of UNKNOWN is not required in this routine,
150 * since it will silently do the Right Thing anyway.
152 * If `affect_hints' is set, we also change the hint type to
153 * NOTBASE if a MAKEBASE hint points at a register being
154 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
155 * as the base register.
157 static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
161 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
162 p->value = scalar * (p->value);
163 if (hint && hint->type == EAH_MAKEBASE &&
164 p->type == hint->base && affect_hints)
165 hint->type = EAH_NOTBASE;
173 static expr *scalarvect(int64_t scalar)
176 addtotemp(EXPR_SIMPLE, scalar);
180 static expr *unknown_expr(void)
183 addtotemp(EXPR_UNKNOWN, 1L);
188 * The SEG operator: calculate the segment part of a relocatable
189 * value. Return NULL, as usual, if an error occurs. Report the
192 static expr *segment_part(expr * e)
197 return unknown_expr();
200 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
206 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
208 } else if (seg & SEG_ABS) {
209 return scalarvect(seg & ~SEG_ABS);
210 } else if (seg & 1) {
211 error(ERR_NONFATAL, "SEG applied to something which"
212 " is already a segment base");
215 int32_t base = outfmt->segbase(seg + 1);
218 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
225 * Recursive-descent parser. Called with a single boolean operand,
226 * which is true if the evaluation is critical (i.e. unresolved
227 * symbols are an error condition). Must update the global `i' to
228 * reflect the token after the parsed string. May return NULL.
230 * evaluate() should report its own errors: on return it is assumed
231 * that if NULL has been returned, the error has already been
238 * expr : bexpr [ WRT expr6 ]
239 * bexpr : rexp0 or expr0 depending on relative-mode setting
240 * rexp0 : rexp1 [ {||} rexp1...]
241 * rexp1 : rexp2 [ {^^} rexp2...]
242 * rexp2 : rexp3 [ {&&} rexp3...]
243 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
244 * expr0 : expr1 [ {|} expr1...]
245 * expr1 : expr2 [ {^} expr2...]
246 * expr2 : expr3 [ {&} expr3...]
247 * expr3 : expr4 [ {<<,>>} expr4...]
248 * expr4 : expr5 [ {+,-} expr5...]
249 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
250 * expr6 : { ~,+,-,SEG } expr6
257 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
259 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
260 static expr *expr4(int), *expr5(int), *expr6(int);
262 static expr *(*bexpr) (int);
264 static expr *rexp0(int critical)
272 while (i == TOKEN_DBL_OR) {
273 i = scan(scpriv, tokval);
277 if (!(is_simple(e) || is_just_unknown(e)) ||
278 !(is_simple(f) || is_just_unknown(f))) {
279 error(ERR_NONFATAL, "`|' operator may only be applied to"
283 if (is_just_unknown(e) || is_just_unknown(f))
286 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
291 static expr *rexp1(int critical)
299 while (i == TOKEN_DBL_XOR) {
300 i = scan(scpriv, tokval);
304 if (!(is_simple(e) || is_just_unknown(e)) ||
305 !(is_simple(f) || is_just_unknown(f))) {
306 error(ERR_NONFATAL, "`^' operator may only be applied to"
310 if (is_just_unknown(e) || is_just_unknown(f))
313 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
318 static expr *rexp2(int critical)
325 while (i == TOKEN_DBL_AND) {
326 i = scan(scpriv, tokval);
330 if (!(is_simple(e) || is_just_unknown(e)) ||
331 !(is_simple(f) || is_just_unknown(f))) {
332 error(ERR_NONFATAL, "`&' operator may only be applied to"
335 if (is_just_unknown(e) || is_just_unknown(f))
338 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
343 static expr *rexp3(int critical)
352 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
353 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
355 i = scan(scpriv, tokval);
360 e = add_vectors(e, scalar_mult(f, -1L, false));
366 v = -1; /* means unknown */
367 else if (!is_really_simple(e) || reloc_value(e) != 0)
368 v = (j == TOKEN_NE); /* unequal, so return true if NE */
370 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
374 v = -1; /* means unknown */
375 else if (!is_really_simple(e)) {
377 "`%s': operands differ by a non-scalar",
378 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
379 TOKEN_GE ? ">=" : ">"));
380 v = 0; /* must set it to _something_ */
382 int vv = reloc_value(e);
384 v = (j == TOKEN_LE || j == TOKEN_GE);
386 v = (j == TOKEN_GE || j == TOKEN_GT);
388 v = (j == TOKEN_LE || j == TOKEN_LT);
401 static expr *expr0(int critical)
410 i = scan(scpriv, tokval);
414 if (!(is_simple(e) || is_just_unknown(e)) ||
415 !(is_simple(f) || is_just_unknown(f))) {
416 error(ERR_NONFATAL, "`|' operator may only be applied to"
419 if (is_just_unknown(e) || is_just_unknown(f))
422 e = scalarvect(reloc_value(e) | reloc_value(f));
427 static expr *expr1(int critical)
436 i = scan(scpriv, tokval);
440 if (!(is_simple(e) || is_just_unknown(e)) ||
441 !(is_simple(f) || is_just_unknown(f))) {
442 error(ERR_NONFATAL, "`^' operator may only be applied to"
445 if (is_just_unknown(e) || is_just_unknown(f))
448 e = scalarvect(reloc_value(e) ^ reloc_value(f));
453 static expr *expr2(int critical)
462 i = scan(scpriv, tokval);
466 if (!(is_simple(e) || is_just_unknown(e)) ||
467 !(is_simple(f) || is_just_unknown(f))) {
468 error(ERR_NONFATAL, "`&' operator may only be applied to"
471 if (is_just_unknown(e) || is_just_unknown(f))
474 e = scalarvect(reloc_value(e) & reloc_value(f));
479 static expr *expr3(int critical)
487 while (i == TOKEN_SHL || i == TOKEN_SHR) {
489 i = scan(scpriv, tokval);
493 if (!(is_simple(e) || is_just_unknown(e)) ||
494 !(is_simple(f) || is_just_unknown(f))) {
495 error(ERR_NONFATAL, "shift operator may only be applied to"
497 } else if (is_just_unknown(e) || is_just_unknown(f)) {
502 e = scalarvect(reloc_value(e) << reloc_value(f));
505 e = scalarvect(((uint64_t)reloc_value(e)) >>
513 static expr *expr4(int critical)
520 while (i == '+' || i == '-') {
522 i = scan(scpriv, tokval);
528 e = add_vectors(e, f);
531 e = add_vectors(e, scalar_mult(f, -1L, false));
538 static expr *expr5(int critical)
545 while (i == '*' || i == '/' || i == '%' ||
546 i == TOKEN_SDIV || i == TOKEN_SMOD) {
548 i = scan(scpriv, tokval);
552 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
553 !(is_simple(f) || is_just_unknown(f)))) {
554 error(ERR_NONFATAL, "division operator may only be applied to"
558 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
559 error(ERR_NONFATAL, "division by zero");
565 e = scalar_mult(f, reloc_value(e), true);
566 else if (is_simple(f))
567 e = scalar_mult(e, reloc_value(f), true);
568 else if (is_just_unknown(e) && is_just_unknown(f))
571 error(ERR_NONFATAL, "unable to multiply two "
572 "non-scalar objects");
577 if (is_just_unknown(e) || is_just_unknown(f))
580 e = scalarvect(((uint64_t)reloc_value(e)) /
581 ((uint64_t)reloc_value(f)));
584 if (is_just_unknown(e) || is_just_unknown(f))
587 e = scalarvect(((uint64_t)reloc_value(e)) %
588 ((uint64_t)reloc_value(f)));
591 if (is_just_unknown(e) || is_just_unknown(f))
594 e = scalarvect(((int64_t)reloc_value(e)) /
595 ((int64_t)reloc_value(f)));
598 if (is_just_unknown(e) || is_just_unknown(f))
601 e = scalarvect(((int64_t)reloc_value(e)) %
602 ((int64_t)reloc_value(f)));
609 static expr *eval_floatize(enum floatize type)
611 uint8_t result[16], *p; /* Up to 128 bits */
612 static const struct {
613 int bytes, start, len;
615 { 1, 0, 1 }, /* FLOAT_8 */
616 { 2, 0, 2 }, /* FLOAT_16 */
617 { 4, 0, 4 }, /* FLOAT_32 */
618 { 8, 0, 8 }, /* FLOAT_64 */
619 { 10, 0, 8 }, /* FLOAT_80M */
620 { 10, 8, 2 }, /* FLOAT_80E */
621 { 16, 0, 8 }, /* FLOAT_128L */
622 { 16, 8, 8 }, /* FLOAT_128H */
628 i = scan(scpriv, tokval);
630 error(ERR_NONFATAL, "expecting `('");
633 i = scan(scpriv, tokval);
634 if (i == '-' || i == '+') {
635 sign = (i == '-') ? -1 : 1;
636 i = scan(scpriv, tokval);
638 if (i != TOKEN_FLOAT) {
639 error(ERR_NONFATAL, "expecting floating-point number");
642 if (!float_const(tokval->t_charptr, sign, result,
643 formats[type].bytes, error))
645 i = scan(scpriv, tokval);
647 error(ERR_NONFATAL, "expecting `)'");
651 p = result+formats[type].start+formats[type].len;
653 for (j = formats[type].len; j; j--) {
655 val = (val << 8) + *p;
659 addtotemp(EXPR_SIMPLE, val);
661 i = scan(scpriv, tokval);
665 static expr *expr6(int critical)
669 int32_t label_seg, label_ofs;
673 i = scan(scpriv, tokval);
677 return scalar_mult(e, -1L, false);
681 i = scan(scpriv, tokval);
682 return expr6(critical);
685 i = scan(scpriv, tokval);
689 if (is_just_unknown(e))
690 return unknown_expr();
691 else if (!is_simple(e)) {
692 error(ERR_NONFATAL, "`~' operator may only be applied to"
696 return scalarvect(~reloc_value(e));
699 i = scan(scpriv, tokval);
703 if (is_just_unknown(e))
704 return unknown_expr();
705 else if (!is_simple(e)) {
706 error(ERR_NONFATAL, "`!' operator may only be applied to"
710 return scalarvect(!reloc_value(e));
713 i = scan(scpriv, tokval);
720 if (is_unknown(e) && critical) {
721 error(ERR_NONFATAL, "unable to determine segment base");
727 return eval_floatize(tokval->t_integer);
730 i = scan(scpriv, tokval);
735 error(ERR_NONFATAL, "expecting `)'");
738 i = scan(scpriv, tokval);
749 addtotemp(EXPR_SIMPLE, tokval->t_integer);
752 addtotemp(tokval->t_integer, 1L);
753 if (hint && hint->type == EAH_NOHINT)
754 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
760 * If !location->known, this indicates that no
761 * symbol, Here or Base references are valid because we
762 * are in preprocess-only mode.
764 if (!location->known) {
766 "%s not supported in preprocess-only mode",
767 (i == TOKEN_ID ? "symbol references" :
768 i == TOKEN_HERE ? "`$'" : "`$$'"));
769 addtotemp(EXPR_UNKNOWN, 1L);
773 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
774 if (i == TOKEN_BASE) {
775 label_seg = in_abs_seg ? abs_seg : location->segment;
777 } else if (i == TOKEN_HERE) {
778 label_seg = in_abs_seg ? abs_seg : location->segment;
779 label_ofs = in_abs_seg ? abs_offset : location->offset;
781 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
783 error(ERR_NONFATAL, "symbol `%s' undefined",
786 } else if (critical == 1) {
788 "symbol `%s' not defined before use",
799 if (opflags && is_extern(tokval->t_charptr))
800 *opflags |= OPFLAG_EXTERN;
802 addtotemp(type, label_ofs);
803 if (label_seg != NO_SEG)
804 addtotemp(EXPR_SEGBASE + label_seg, 1L);
807 i = scan(scpriv, tokval);
811 error(ERR_NONFATAL, "expression syntax error");
816 void eval_global_info(struct ofmt *output, lfunc lookup_label,
817 struct location * locp)
820 labelfunc = lookup_label;
824 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
825 int *fwref, int critical, efunc report_error,
826 struct eval_hints *hints)
833 hint->type = EAH_NOHINT;
835 if (critical & CRITICAL) {
836 critical &= ~CRITICAL;
844 error = report_error;
847 if (tokval->t_type == TOKEN_INVALID)
848 i = scan(scpriv, tokval);
852 while (ntempexprs) /* initialize temporary storage */
853 nasm_free(tempexprs[--ntempexprs]);
859 if (i == TOKEN_WRT) {
860 i = scan(scpriv, tokval); /* eat the WRT */
865 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
868 if (is_just_unknown(f))
874 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
877 value = reloc_seg(f);
879 value = reloc_value(f) | SEG_ABS;
880 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
881 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
884 addtotemp(EXPR_WRT, value);
887 e = add_vectors(e, g);