NASM 2.04rc6
[nasm/avx512.git] / eval.c
blob4fd3b4b87ab3a54f29ad4f80c02342e617292551
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 license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
9 */
11 #include "compiler.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <inttypes.h>
20 #include "nasm.h"
21 #include "nasmlib.h"
22 #include "eval.h"
23 #include "labels.h"
24 #include "float.h"
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;
40 static int ntempexpr;
41 static int tempexpr_size;
43 static struct tokenval *tokval; /* The current token */
44 static int i; /* The t_type of tokval */
46 static void *scpriv;
47 static struct location *location; /* Pointer to current line's segment,offset */
48 static int *opflags;
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)
62 while (ntempexprs)
63 nasm_free(tempexprs[--ntempexprs]);
64 nasm_free(tempexprs);
68 * Construct a temporary expression.
70 static void begintemp(void)
72 tempexpr = NULL;
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)
105 int preserve;
107 preserve = is_really_simple(p) || is_really_simple(q);
109 begintemp();
111 while (p->type && q->type &&
112 p->type < EXPR_SEGBASE + SEG_ABS &&
113 q->type < EXPR_SEGBASE + SEG_ABS) {
114 int lasttype;
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;
124 if (sum)
125 addtotemp(p->type, sum);
126 lasttype = p->type;
127 p++, q++;
129 if (lasttype == EXPR_UNKNOWN) {
130 return finishtemp();
133 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
134 addtotemp(p->type, p->value);
135 p++;
137 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
138 addtotemp(q->type, q->value);
139 q++;
142 return finishtemp();
146 * Multiply a vector by a scalar. Strip far-absolute segment part
147 * if present.
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)
159 expr *p = vect;
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;
166 p++;
168 p->type = 0;
170 return vect;
173 static expr *scalarvect(int64_t scalar)
175 begintemp();
176 addtotemp(EXPR_SIMPLE, scalar);
177 return finishtemp();
180 static expr *unknown_expr(void)
182 begintemp();
183 addtotemp(EXPR_UNKNOWN, 1L);
184 return finishtemp();
188 * The SEG operator: calculate the segment part of a relocatable
189 * value. Return NULL, as usual, if an error occurs. Report the
190 * error too.
192 static expr *segment_part(expr * e)
194 int32_t seg;
196 if (is_unknown(e))
197 return unknown_expr();
199 if (!is_reloc(e)) {
200 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
201 return NULL;
204 seg = reloc_seg(e);
205 if (seg == NO_SEG) {
206 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
207 return NULL;
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");
213 return NULL;
214 } else {
215 int32_t base = outfmt->segbase(seg + 1);
217 begintemp();
218 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
219 1L);
220 return finishtemp();
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
232 * reported.
236 * Grammar parsed is:
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
251 * | (bexpr)
252 * | symbol
253 * | $
254 * | number
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)
266 expr *e, *f;
268 e = rexp1(critical);
269 if (!e)
270 return NULL;
272 while (i == TOKEN_DBL_OR) {
273 i = scan(scpriv, tokval);
274 f = rexp1(critical);
275 if (!f)
276 return NULL;
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"
280 " scalar values");
283 if (is_just_unknown(e) || is_just_unknown(f))
284 e = unknown_expr();
285 else
286 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
288 return e;
291 static expr *rexp1(int critical)
293 expr *e, *f;
295 e = rexp2(critical);
296 if (!e)
297 return NULL;
299 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))) {
306 error(ERR_NONFATAL, "`^' operator may only be applied to"
307 " scalar values");
310 if (is_just_unknown(e) || is_just_unknown(f))
311 e = unknown_expr();
312 else
313 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
315 return e;
318 static expr *rexp2(int critical)
320 expr *e, *f;
322 e = rexp3(critical);
323 if (!e)
324 return NULL;
325 while (i == TOKEN_DBL_AND) {
326 i = scan(scpriv, tokval);
327 f = rexp3(critical);
328 if (!f)
329 return NULL;
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"
333 " scalar values");
335 if (is_just_unknown(e) || is_just_unknown(f))
336 e = unknown_expr();
337 else
338 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
340 return e;
343 static expr *rexp3(int critical)
345 expr *e, *f;
346 int64_t v;
348 e = expr0(critical);
349 if (!e)
350 return NULL;
352 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
353 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
354 int j = i;
355 i = scan(scpriv, tokval);
356 f = expr0(critical);
357 if (!f)
358 return NULL;
360 e = add_vectors(e, scalar_mult(f, -1L, false));
362 switch (j) {
363 case TOKEN_EQ:
364 case TOKEN_NE:
365 if (is_unknown(e))
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 */
369 else
370 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
371 break;
372 default:
373 if (is_unknown(e))
374 v = -1; /* means unknown */
375 else if (!is_really_simple(e)) {
376 error(ERR_NONFATAL,
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_ */
381 } else {
382 int vv = reloc_value(e);
383 if (vv == 0)
384 v = (j == TOKEN_LE || j == TOKEN_GE);
385 else if (vv > 0)
386 v = (j == TOKEN_GE || j == TOKEN_GT);
387 else /* vv < 0 */
388 v = (j == TOKEN_LE || j == TOKEN_LT);
390 break;
393 if (v == -1)
394 e = unknown_expr();
395 else
396 e = scalarvect(v);
398 return e;
401 static expr *expr0(int critical)
403 expr *e, *f;
405 e = expr1(critical);
406 if (!e)
407 return NULL;
409 while (i == '|') {
410 i = scan(scpriv, tokval);
411 f = expr1(critical);
412 if (!f)
413 return NULL;
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"
417 " scalar values");
419 if (is_just_unknown(e) || is_just_unknown(f))
420 e = unknown_expr();
421 else
422 e = scalarvect(reloc_value(e) | reloc_value(f));
424 return e;
427 static expr *expr1(int critical)
429 expr *e, *f;
431 e = expr2(critical);
432 if (!e)
433 return NULL;
435 while (i == '^') {
436 i = scan(scpriv, tokval);
437 f = expr2(critical);
438 if (!f)
439 return NULL;
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"
443 " scalar values");
445 if (is_just_unknown(e) || is_just_unknown(f))
446 e = unknown_expr();
447 else
448 e = scalarvect(reloc_value(e) ^ reloc_value(f));
450 return e;
453 static expr *expr2(int critical)
455 expr *e, *f;
457 e = expr3(critical);
458 if (!e)
459 return NULL;
461 while (i == '&') {
462 i = scan(scpriv, tokval);
463 f = expr3(critical);
464 if (!f)
465 return NULL;
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"
469 " scalar values");
471 if (is_just_unknown(e) || is_just_unknown(f))
472 e = unknown_expr();
473 else
474 e = scalarvect(reloc_value(e) & reloc_value(f));
476 return e;
479 static expr *expr3(int critical)
481 expr *e, *f;
483 e = expr4(critical);
484 if (!e)
485 return NULL;
487 while (i == TOKEN_SHL || i == TOKEN_SHR) {
488 int j = i;
489 i = scan(scpriv, tokval);
490 f = expr4(critical);
491 if (!f)
492 return NULL;
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"
496 " scalar values");
497 } else if (is_just_unknown(e) || is_just_unknown(f)) {
498 e = unknown_expr();
499 } else
500 switch (j) {
501 case TOKEN_SHL:
502 e = scalarvect(reloc_value(e) << reloc_value(f));
503 break;
504 case TOKEN_SHR:
505 e = scalarvect(((uint64_t)reloc_value(e)) >>
506 reloc_value(f));
507 break;
510 return e;
513 static expr *expr4(int critical)
515 expr *e, *f;
517 e = expr5(critical);
518 if (!e)
519 return NULL;
520 while (i == '+' || i == '-') {
521 int j = i;
522 i = scan(scpriv, tokval);
523 f = expr5(critical);
524 if (!f)
525 return NULL;
526 switch (j) {
527 case '+':
528 e = add_vectors(e, f);
529 break;
530 case '-':
531 e = add_vectors(e, scalar_mult(f, -1L, false));
532 break;
535 return e;
538 static expr *expr5(int critical)
540 expr *e, *f;
542 e = expr6(critical);
543 if (!e)
544 return NULL;
545 while (i == '*' || i == '/' || i == '%' ||
546 i == TOKEN_SDIV || i == TOKEN_SMOD) {
547 int j = i;
548 i = scan(scpriv, tokval);
549 f = expr6(critical);
550 if (!f)
551 return NULL;
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"
555 " scalar values");
556 return NULL;
558 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
559 error(ERR_NONFATAL, "division by zero");
560 return NULL;
562 switch (j) {
563 case '*':
564 if (is_simple(e))
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))
569 e = unknown_expr();
570 else {
571 error(ERR_NONFATAL, "unable to multiply two "
572 "non-scalar objects");
573 return NULL;
575 break;
576 case '/':
577 if (is_just_unknown(e) || is_just_unknown(f))
578 e = unknown_expr();
579 else
580 e = scalarvect(((uint64_t)reloc_value(e)) /
581 ((uint64_t)reloc_value(f)));
582 break;
583 case '%':
584 if (is_just_unknown(e) || is_just_unknown(f))
585 e = unknown_expr();
586 else
587 e = scalarvect(((uint64_t)reloc_value(e)) %
588 ((uint64_t)reloc_value(f)));
589 break;
590 case TOKEN_SDIV:
591 if (is_just_unknown(e) || is_just_unknown(f))
592 e = unknown_expr();
593 else
594 e = scalarvect(((int64_t)reloc_value(e)) /
595 ((int64_t)reloc_value(f)));
596 break;
597 case TOKEN_SMOD:
598 if (is_just_unknown(e) || is_just_unknown(f))
599 e = unknown_expr();
600 else
601 e = scalarvect(((int64_t)reloc_value(e)) %
602 ((int64_t)reloc_value(f)));
603 break;
606 return e;
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;
614 } formats[] = {
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 */
624 int sign = 1;
625 int64_t val;
626 int j;
628 i = scan(scpriv, tokval);
629 if (i != '(') {
630 error(ERR_NONFATAL, "expecting `('");
631 return NULL;
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");
640 return NULL;
642 if (!float_const(tokval->t_charptr, sign, result,
643 formats[type].bytes, error))
644 return NULL;
645 i = scan(scpriv, tokval);
646 if (i != ')') {
647 error(ERR_NONFATAL, "expecting `)'");
648 return NULL;
651 p = result+formats[type].start+formats[type].len;
652 val = 0;
653 for (j = formats[type].len; j; j--) {
654 p--;
655 val = (val << 8) + *p;
658 begintemp();
659 addtotemp(EXPR_SIMPLE, val);
661 i = scan(scpriv, tokval);
662 return finishtemp();
665 static expr *eval_strfunc(enum strfunc type)
667 char *string;
668 size_t string_len;
669 int64_t val;
670 bool parens, rn_warn;
672 parens = false;
673 i = scan(scpriv, tokval);
674 if (i == '(') {
675 parens = true;
676 i = scan(scpriv, tokval);
678 if (i != TOKEN_STR) {
679 error(ERR_NONFATAL, "expecting string");
680 return NULL;
682 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
683 &string, type);
684 if (string_len == (size_t)-1) {
685 error(ERR_NONFATAL, "invalid string for transform");
686 return NULL;
689 val = readstrnum(string, string_len, &rn_warn);
690 if (parens) {
691 i = scan(scpriv, tokval);
692 if (i != ')') {
693 error(ERR_NONFATAL, "expecting `)'");
694 return NULL;
698 if (rn_warn)
699 error(ERR_WARNING|ERR_PASS1, "character constant too long");
701 begintemp();
702 addtotemp(EXPR_SIMPLE, val);
704 i = scan(scpriv, tokval);
705 return finishtemp();
708 static expr *expr6(int critical)
710 int32_t type;
711 expr *e;
712 int32_t label_seg;
713 int64_t label_ofs;
714 int64_t tmpval;
715 bool rn_warn;
716 char *scope;
718 switch (i) {
719 case '-':
720 i = scan(scpriv, tokval);
721 e = expr6(critical);
722 if (!e)
723 return NULL;
724 return scalar_mult(e, -1L, false);
727 case '+':
728 i = scan(scpriv, tokval);
729 return expr6(critical);
731 case '~':
732 i = scan(scpriv, tokval);
733 e = expr6(critical);
734 if (!e)
735 return NULL;
736 if (is_just_unknown(e))
737 return unknown_expr();
738 else if (!is_simple(e)) {
739 error(ERR_NONFATAL, "`~' operator may only be applied to"
740 " scalar values");
741 return NULL;
743 return scalarvect(~reloc_value(e));
745 case '!':
746 i = scan(scpriv, tokval);
747 e = expr6(critical);
748 if (!e)
749 return NULL;
750 if (is_just_unknown(e))
751 return unknown_expr();
752 else if (!is_simple(e)) {
753 error(ERR_NONFATAL, "`!' operator may only be applied to"
754 " scalar values");
755 return NULL;
757 return scalarvect(!reloc_value(e));
759 case TOKEN_SEG:
760 i = scan(scpriv, tokval);
761 e = expr6(critical);
762 if (!e)
763 return NULL;
764 e = segment_part(e);
765 if (!e)
766 return NULL;
767 if (is_unknown(e) && critical) {
768 error(ERR_NONFATAL, "unable to determine segment base");
769 return NULL;
771 return e;
773 case TOKEN_FLOATIZE:
774 return eval_floatize(tokval->t_integer);
776 case TOKEN_STRFUNC:
777 return eval_strfunc(tokval->t_integer);
779 case '(':
780 i = scan(scpriv, tokval);
781 e = bexpr(critical);
782 if (!e)
783 return NULL;
784 if (i != ')') {
785 error(ERR_NONFATAL, "expecting `)'");
786 return NULL;
788 i = scan(scpriv, tokval);
789 return e;
791 case TOKEN_NUM:
792 case TOKEN_STR:
793 case TOKEN_REG:
794 case TOKEN_ID:
795 case TOKEN_INSN: /* Opcodes that occur here are really labels */
796 case TOKEN_HERE:
797 case TOKEN_BASE:
798 begintemp();
799 switch (i) {
800 case TOKEN_NUM:
801 addtotemp(EXPR_SIMPLE, tokval->t_integer);
802 break;
803 case TOKEN_STR:
804 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
805 if (rn_warn)
806 error(ERR_WARNING|ERR_PASS1, "character constant too long");
807 addtotemp(EXPR_SIMPLE, tmpval);
808 break;
809 case TOKEN_REG:
810 addtotemp(tokval->t_integer, 1L);
811 if (hint && hint->type == EAH_NOHINT)
812 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
813 break;
814 case TOKEN_ID:
815 case TOKEN_INSN:
816 case TOKEN_HERE:
817 case TOKEN_BASE:
819 * If !location->known, this indicates that no
820 * symbol, Here or Base references are valid because we
821 * are in preprocess-only mode.
823 if (!location->known) {
824 error(ERR_NONFATAL,
825 "%s not supported in preprocess-only mode",
826 (i == TOKEN_HERE ? "`$'" :
827 i == TOKEN_BASE ? "`$$'" :
828 "symbol references"));
829 addtotemp(EXPR_UNKNOWN, 1L);
830 break;
833 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
834 if (i == TOKEN_BASE) {
835 label_seg = in_abs_seg ? abs_seg : location->segment;
836 label_ofs = 0;
837 } else if (i == TOKEN_HERE) {
838 label_seg = in_abs_seg ? abs_seg : location->segment;
839 label_ofs = in_abs_seg ? abs_offset : location->offset;
840 } else {
841 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
842 scope = local_scope(tokval->t_charptr);
843 if (critical == 2) {
844 error(ERR_NONFATAL, "symbol `%s%s' undefined",
845 scope,tokval->t_charptr);
846 return NULL;
847 } else if (critical == 1) {
848 error(ERR_NONFATAL,
849 "symbol `%s%s' not defined before use",
850 scope,tokval->t_charptr);
851 return NULL;
852 } else {
853 if (opflags)
854 *opflags |= 1;
855 type = EXPR_UNKNOWN;
856 label_seg = NO_SEG;
857 label_ofs = 1;
860 if (opflags && is_extern(tokval->t_charptr))
861 *opflags |= OPFLAG_EXTERN;
863 addtotemp(type, label_ofs);
864 if (label_seg != NO_SEG)
865 addtotemp(EXPR_SEGBASE + label_seg, 1L);
866 break;
868 i = scan(scpriv, tokval);
869 return finishtemp();
871 default:
872 error(ERR_NONFATAL, "expression syntax error");
873 return NULL;
877 void eval_global_info(struct ofmt *output, lfunc lookup_label,
878 struct location * locp)
880 outfmt = output;
881 labelfunc = lookup_label;
882 location = locp;
885 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
886 int *fwref, int critical, efunc report_error,
887 struct eval_hints *hints)
889 expr *e;
890 expr *f = NULL;
892 hint = hints;
893 if (hint)
894 hint->type = EAH_NOHINT;
896 if (critical & CRITICAL) {
897 critical &= ~CRITICAL;
898 bexpr = rexp0;
899 } else
900 bexpr = expr0;
902 scan = sc;
903 scpriv = scprivate;
904 tokval = tv;
905 error = report_error;
906 opflags = fwref;
908 if (tokval->t_type == TOKEN_INVALID)
909 i = scan(scpriv, tokval);
910 else
911 i = tokval->t_type;
913 while (ntempexprs) /* initialize temporary storage */
914 nasm_free(tempexprs[--ntempexprs]);
916 e = bexpr(critical);
917 if (!e)
918 return NULL;
920 if (i == TOKEN_WRT) {
921 i = scan(scpriv, tokval); /* eat the WRT */
922 f = expr6(critical);
923 if (!f)
924 return NULL;
926 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
927 if (f) {
928 expr *g;
929 if (is_just_unknown(f))
930 g = unknown_expr();
931 else {
932 int64_t value;
933 begintemp();
934 if (!is_reloc(f)) {
935 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
936 return NULL;
938 value = reloc_seg(f);
939 if (value == NO_SEG)
940 value = reloc_value(f) | SEG_ABS;
941 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
942 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
943 return NULL;
945 addtotemp(EXPR_WRT, value);
946 g = finishtemp();
948 e = add_vectors(e, g);
950 return e;