changes.src: definitely making 2.13 next
[nasm.git] / asm / eval.c
blob89f66fb7f0da88db9faa02737a69d03adae90ae8
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * eval.c expression evaluator for the Netwide Assembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <string.h>
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "error.h"
49 #include "eval.h"
50 #include "labels.h"
51 #include "float.h"
52 #include "assemble.h"
54 #define TEMPEXPRS_DELTA 128
55 #define TEMPEXPR_DELTA 8
57 static scanner scan; /* Address of scanner routine */
59 static expr **tempexprs = NULL;
60 static int ntempexprs;
61 static int tempexprs_size = 0;
63 static expr *tempexpr;
64 static int ntempexpr;
65 static int tempexpr_size;
67 static struct tokenval *tokval; /* The current token */
68 static int i; /* The t_type of tokval */
70 static void *scpriv;
71 static int *opflags;
73 static struct eval_hints *hint;
77 * Unimportant cleanup is done to avoid confusing people who are trying
78 * to debug real memory leaks
80 void eval_cleanup(void)
82 while (ntempexprs)
83 nasm_free(tempexprs[--ntempexprs]);
84 nasm_free(tempexprs);
88 * Construct a temporary expression.
90 static void begintemp(void)
92 tempexpr = NULL;
93 tempexpr_size = ntempexpr = 0;
96 static void addtotemp(int32_t type, int64_t value)
98 while (ntempexpr >= tempexpr_size) {
99 tempexpr_size += TEMPEXPR_DELTA;
100 tempexpr = nasm_realloc(tempexpr,
101 tempexpr_size * sizeof(*tempexpr));
103 tempexpr[ntempexpr].type = type;
104 tempexpr[ntempexpr++].value = value;
107 static expr *finishtemp(void)
109 addtotemp(0L, 0L); /* terminate */
110 while (ntempexprs >= tempexprs_size) {
111 tempexprs_size += TEMPEXPRS_DELTA;
112 tempexprs = nasm_realloc(tempexprs,
113 tempexprs_size * sizeof(*tempexprs));
115 return tempexprs[ntempexprs++] = tempexpr;
119 * Add two vector datatypes. We have some bizarre behaviour on far-
120 * absolute segment types: we preserve them during addition _only_
121 * if one of the segments is a truly pure scalar.
123 static expr *add_vectors(expr * p, expr * q)
125 int preserve;
127 preserve = is_really_simple(p) || is_really_simple(q);
129 begintemp();
131 while (p->type && q->type &&
132 p->type < EXPR_SEGBASE + SEG_ABS &&
133 q->type < EXPR_SEGBASE + SEG_ABS) {
134 int lasttype;
136 if (p->type > q->type) {
137 addtotemp(q->type, q->value);
138 lasttype = q++->type;
139 } else if (p->type < q->type) {
140 addtotemp(p->type, p->value);
141 lasttype = p++->type;
142 } else { /* *p and *q have same type */
143 int64_t sum = p->value + q->value;
144 if (sum) {
145 addtotemp(p->type, sum);
146 if (hint)
147 hint->type = EAH_SUMMED;
149 lasttype = p->type;
150 p++, q++;
152 if (lasttype == EXPR_UNKNOWN) {
153 return finishtemp();
156 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
157 addtotemp(p->type, p->value);
158 p++;
160 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
161 addtotemp(q->type, q->value);
162 q++;
165 return finishtemp();
169 * Multiply a vector by a scalar. Strip far-absolute segment part
170 * if present.
172 * Explicit treatment of UNKNOWN is not required in this routine,
173 * since it will silently do the Right Thing anyway.
175 * If `affect_hints' is set, we also change the hint type to
176 * NOTBASE if a MAKEBASE hint points at a register being
177 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
178 * as the base register.
180 static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
182 expr *p = vect;
184 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
185 p->value = scalar * (p->value);
186 if (hint && hint->type == EAH_MAKEBASE &&
187 p->type == hint->base && affect_hints)
188 hint->type = EAH_NOTBASE;
189 p++;
191 p->type = 0;
193 return vect;
196 static expr *scalarvect(int64_t scalar)
198 begintemp();
199 addtotemp(EXPR_SIMPLE, scalar);
200 return finishtemp();
203 static expr *unknown_expr(void)
205 begintemp();
206 addtotemp(EXPR_UNKNOWN, 1L);
207 return finishtemp();
211 * The SEG operator: calculate the segment part of a relocatable
212 * value. Return NULL, as usual, if an error occurs. Report the
213 * error too.
215 static expr *segment_part(expr * e)
217 int32_t seg;
219 if (is_unknown(e))
220 return unknown_expr();
222 if (!is_reloc(e)) {
223 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
224 return NULL;
227 seg = reloc_seg(e);
228 if (seg == NO_SEG) {
229 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
230 return NULL;
231 } else if (seg & SEG_ABS) {
232 return scalarvect(seg & ~SEG_ABS);
233 } else if (seg & 1) {
234 nasm_error(ERR_NONFATAL, "SEG applied to something which"
235 " is already a segment base");
236 return NULL;
237 } else {
238 int32_t base = ofmt->segbase(seg + 1);
240 begintemp();
241 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
242 1L);
243 return finishtemp();
248 * Recursive-descent parser. Called with a single boolean operand,
249 * which is true if the evaluation is critical (i.e. unresolved
250 * symbols are an error condition). Must update the global `i' to
251 * reflect the token after the parsed string. May return NULL.
253 * evaluate() should report its own errors: on return it is assumed
254 * that if NULL has been returned, the error has already been
255 * reported.
259 * Grammar parsed is:
261 * expr : bexpr [ WRT expr6 ]
262 * bexpr : rexp0 or expr0 depending on relative-mode setting
263 * rexp0 : rexp1 [ {||} rexp1...]
264 * rexp1 : rexp2 [ {^^} rexp2...]
265 * rexp2 : rexp3 [ {&&} rexp3...]
266 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
267 * expr0 : expr1 [ {|} expr1...]
268 * expr1 : expr2 [ {^} expr2...]
269 * expr2 : expr3 [ {&} expr3...]
270 * expr3 : expr4 [ {<<,>>} expr4...]
271 * expr4 : expr5 [ {+,-} expr5...]
272 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
273 * expr6 : { ~,+,-,IFUNC,SEG } expr6
274 * | (bexpr)
275 * | symbol
276 * | $
277 * | number
280 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
282 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
283 static expr *expr4(int), *expr5(int), *expr6(int);
285 static expr *(*bexpr) (int);
287 static expr *rexp0(int critical)
289 expr *e, *f;
291 e = rexp1(critical);
292 if (!e)
293 return NULL;
295 while (i == TOKEN_DBL_OR) {
296 i = scan(scpriv, tokval);
297 f = rexp1(critical);
298 if (!f)
299 return NULL;
300 if (!(is_simple(e) || is_just_unknown(e)) ||
301 !(is_simple(f) || is_just_unknown(f))) {
302 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
303 " scalar values");
306 if (is_just_unknown(e) || is_just_unknown(f))
307 e = unknown_expr();
308 else
309 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
311 return e;
314 static expr *rexp1(int critical)
316 expr *e, *f;
318 e = rexp2(critical);
319 if (!e)
320 return NULL;
322 while (i == TOKEN_DBL_XOR) {
323 i = scan(scpriv, tokval);
324 f = rexp2(critical);
325 if (!f)
326 return NULL;
327 if (!(is_simple(e) || is_just_unknown(e)) ||
328 !(is_simple(f) || is_just_unknown(f))) {
329 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
330 " scalar values");
333 if (is_just_unknown(e) || is_just_unknown(f))
334 e = unknown_expr();
335 else
336 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
338 return e;
341 static expr *rexp2(int critical)
343 expr *e, *f;
345 e = rexp3(critical);
346 if (!e)
347 return NULL;
348 while (i == TOKEN_DBL_AND) {
349 i = scan(scpriv, tokval);
350 f = rexp3(critical);
351 if (!f)
352 return NULL;
353 if (!(is_simple(e) || is_just_unknown(e)) ||
354 !(is_simple(f) || is_just_unknown(f))) {
355 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
356 " scalar values");
358 if (is_just_unknown(e) || is_just_unknown(f))
359 e = unknown_expr();
360 else
361 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
363 return e;
366 static expr *rexp3(int critical)
368 expr *e, *f;
369 int64_t v;
371 e = expr0(critical);
372 if (!e)
373 return NULL;
375 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
376 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
377 int j = i;
378 i = scan(scpriv, tokval);
379 f = expr0(critical);
380 if (!f)
381 return NULL;
383 e = add_vectors(e, scalar_mult(f, -1L, false));
385 switch (j) {
386 case TOKEN_EQ:
387 case TOKEN_NE:
388 if (is_unknown(e))
389 v = -1; /* means unknown */
390 else if (!is_really_simple(e) || reloc_value(e) != 0)
391 v = (j == TOKEN_NE); /* unequal, so return true if NE */
392 else
393 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
394 break;
395 default:
396 if (is_unknown(e))
397 v = -1; /* means unknown */
398 else if (!is_really_simple(e)) {
399 nasm_error(ERR_NONFATAL,
400 "`%s': operands differ by a non-scalar",
401 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
402 TOKEN_GE ? ">=" : ">"));
403 v = 0; /* must set it to _something_ */
404 } else {
405 int64_t vv = reloc_value(e);
406 if (vv == 0)
407 v = (j == TOKEN_LE || j == TOKEN_GE);
408 else if (vv > 0)
409 v = (j == TOKEN_GE || j == TOKEN_GT);
410 else /* vv < 0 */
411 v = (j == TOKEN_LE || j == TOKEN_LT);
413 break;
416 if (v == -1)
417 e = unknown_expr();
418 else
419 e = scalarvect(v);
421 return e;
424 static expr *expr0(int critical)
426 expr *e, *f;
428 e = expr1(critical);
429 if (!e)
430 return NULL;
432 while (i == '|') {
433 i = scan(scpriv, tokval);
434 f = expr1(critical);
435 if (!f)
436 return NULL;
437 if (!(is_simple(e) || is_just_unknown(e)) ||
438 !(is_simple(f) || is_just_unknown(f))) {
439 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
440 " scalar values");
442 if (is_just_unknown(e) || is_just_unknown(f))
443 e = unknown_expr();
444 else
445 e = scalarvect(reloc_value(e) | reloc_value(f));
447 return e;
450 static expr *expr1(int critical)
452 expr *e, *f;
454 e = expr2(critical);
455 if (!e)
456 return NULL;
458 while (i == '^') {
459 i = scan(scpriv, tokval);
460 f = expr2(critical);
461 if (!f)
462 return NULL;
463 if (!(is_simple(e) || is_just_unknown(e)) ||
464 !(is_simple(f) || is_just_unknown(f))) {
465 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
466 " scalar values");
468 if (is_just_unknown(e) || is_just_unknown(f))
469 e = unknown_expr();
470 else
471 e = scalarvect(reloc_value(e) ^ reloc_value(f));
473 return e;
476 static expr *expr2(int critical)
478 expr *e, *f;
480 e = expr3(critical);
481 if (!e)
482 return NULL;
484 while (i == '&') {
485 i = scan(scpriv, tokval);
486 f = expr3(critical);
487 if (!f)
488 return NULL;
489 if (!(is_simple(e) || is_just_unknown(e)) ||
490 !(is_simple(f) || is_just_unknown(f))) {
491 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
492 " scalar values");
494 if (is_just_unknown(e) || is_just_unknown(f))
495 e = unknown_expr();
496 else
497 e = scalarvect(reloc_value(e) & reloc_value(f));
499 return e;
502 static expr *expr3(int critical)
504 expr *e, *f;
506 e = expr4(critical);
507 if (!e)
508 return NULL;
510 while (i == TOKEN_SHL || i == TOKEN_SHR) {
511 int j = i;
512 i = scan(scpriv, tokval);
513 f = expr4(critical);
514 if (!f)
515 return NULL;
516 if (!(is_simple(e) || is_just_unknown(e)) ||
517 !(is_simple(f) || is_just_unknown(f))) {
518 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
519 " scalar values");
520 } else if (is_just_unknown(e) || is_just_unknown(f)) {
521 e = unknown_expr();
522 } else
523 switch (j) {
524 case TOKEN_SHL:
525 e = scalarvect(reloc_value(e) << reloc_value(f));
526 break;
527 case TOKEN_SHR:
528 e = scalarvect(((uint64_t)reloc_value(e)) >>
529 reloc_value(f));
530 break;
533 return e;
536 static expr *expr4(int critical)
538 expr *e, *f;
540 e = expr5(critical);
541 if (!e)
542 return NULL;
543 while (i == '+' || i == '-') {
544 int j = i;
545 i = scan(scpriv, tokval);
546 f = expr5(critical);
547 if (!f)
548 return NULL;
549 switch (j) {
550 case '+':
551 e = add_vectors(e, f);
552 break;
553 case '-':
554 e = add_vectors(e, scalar_mult(f, -1L, false));
555 break;
558 return e;
561 static expr *expr5(int critical)
563 expr *e, *f;
565 e = expr6(critical);
566 if (!e)
567 return NULL;
568 while (i == '*' || i == '/' || i == '%' ||
569 i == TOKEN_SDIV || i == TOKEN_SMOD) {
570 int j = i;
571 i = scan(scpriv, tokval);
572 f = expr6(critical);
573 if (!f)
574 return NULL;
575 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
576 !(is_simple(f) || is_just_unknown(f)))) {
577 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
578 " scalar values");
579 return NULL;
581 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
582 nasm_error(ERR_NONFATAL, "division by zero");
583 return NULL;
585 switch (j) {
586 case '*':
587 if (is_simple(e))
588 e = scalar_mult(f, reloc_value(e), true);
589 else if (is_simple(f))
590 e = scalar_mult(e, reloc_value(f), true);
591 else if (is_just_unknown(e) && is_just_unknown(f))
592 e = unknown_expr();
593 else {
594 nasm_error(ERR_NONFATAL, "unable to multiply two "
595 "non-scalar objects");
596 return NULL;
598 break;
599 case '/':
600 if (is_just_unknown(e) || is_just_unknown(f))
601 e = unknown_expr();
602 else
603 e = scalarvect(((uint64_t)reloc_value(e)) /
604 ((uint64_t)reloc_value(f)));
605 break;
606 case '%':
607 if (is_just_unknown(e) || is_just_unknown(f))
608 e = unknown_expr();
609 else
610 e = scalarvect(((uint64_t)reloc_value(e)) %
611 ((uint64_t)reloc_value(f)));
612 break;
613 case TOKEN_SDIV:
614 if (is_just_unknown(e) || is_just_unknown(f))
615 e = unknown_expr();
616 else
617 e = scalarvect(((int64_t)reloc_value(e)) /
618 ((int64_t)reloc_value(f)));
619 break;
620 case TOKEN_SMOD:
621 if (is_just_unknown(e) || is_just_unknown(f))
622 e = unknown_expr();
623 else
624 e = scalarvect(((int64_t)reloc_value(e)) %
625 ((int64_t)reloc_value(f)));
626 break;
629 return e;
632 static expr *eval_floatize(enum floatize type)
634 uint8_t result[16], *p; /* Up to 128 bits */
635 static const struct {
636 int bytes, start, len;
637 } formats[] = {
638 { 1, 0, 1 }, /* FLOAT_8 */
639 { 2, 0, 2 }, /* FLOAT_16 */
640 { 4, 0, 4 }, /* FLOAT_32 */
641 { 8, 0, 8 }, /* FLOAT_64 */
642 { 10, 0, 8 }, /* FLOAT_80M */
643 { 10, 8, 2 }, /* FLOAT_80E */
644 { 16, 0, 8 }, /* FLOAT_128L */
645 { 16, 8, 8 }, /* FLOAT_128H */
647 int sign = 1;
648 int64_t val;
649 int j;
651 i = scan(scpriv, tokval);
652 if (i != '(') {
653 nasm_error(ERR_NONFATAL, "expecting `('");
654 return NULL;
656 i = scan(scpriv, tokval);
657 if (i == '-' || i == '+') {
658 sign = (i == '-') ? -1 : 1;
659 i = scan(scpriv, tokval);
661 if (i != TOKEN_FLOAT) {
662 nasm_error(ERR_NONFATAL, "expecting floating-point number");
663 return NULL;
665 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
666 return NULL;
667 i = scan(scpriv, tokval);
668 if (i != ')') {
669 nasm_error(ERR_NONFATAL, "expecting `)'");
670 return NULL;
673 p = result+formats[type].start+formats[type].len;
674 val = 0;
675 for (j = formats[type].len; j; j--) {
676 p--;
677 val = (val << 8) + *p;
680 begintemp();
681 addtotemp(EXPR_SIMPLE, val);
683 i = scan(scpriv, tokval);
684 return finishtemp();
687 static expr *eval_strfunc(enum strfunc type)
689 char *string;
690 size_t string_len;
691 int64_t val;
692 bool parens, rn_warn;
694 parens = false;
695 i = scan(scpriv, tokval);
696 if (i == '(') {
697 parens = true;
698 i = scan(scpriv, tokval);
700 if (i != TOKEN_STR) {
701 nasm_error(ERR_NONFATAL, "expecting string");
702 return NULL;
704 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
705 &string, type);
706 if (string_len == (size_t)-1) {
707 nasm_error(ERR_NONFATAL, "invalid string for transform");
708 return NULL;
711 val = readstrnum(string, string_len, &rn_warn);
712 if (parens) {
713 i = scan(scpriv, tokval);
714 if (i != ')') {
715 nasm_error(ERR_NONFATAL, "expecting `)'");
716 return NULL;
720 if (rn_warn)
721 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
723 begintemp();
724 addtotemp(EXPR_SIMPLE, val);
726 i = scan(scpriv, tokval);
727 return finishtemp();
730 static int64_t eval_ifunc(int64_t val, enum ifunc func)
732 int errtype;
733 uint64_t uval = (uint64_t)val;
734 int64_t rv;
736 switch (func) {
737 case IFUNC_ILOG2E:
738 case IFUNC_ILOG2W:
739 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
741 if (!is_power2(uval))
742 nasm_error(errtype, "ilog2 argument is not a power of two");
743 /* fall through */
744 case IFUNC_ILOG2F:
745 rv = ilog2_64(uval);
746 break;
748 case IFUNC_ILOG2C:
749 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
750 break;
752 default:
753 nasm_panic(0, "invalid IFUNC token %d", func);
754 rv = 0;
755 break;
758 return rv;
761 static expr *expr6(int critical)
763 int32_t type;
764 expr *e;
765 int32_t label_seg;
766 int64_t label_ofs;
767 int64_t tmpval;
768 bool rn_warn;
769 char *scope;
771 switch (i) {
772 case '-':
773 i = scan(scpriv, tokval);
774 e = expr6(critical);
775 if (!e)
776 return NULL;
777 return scalar_mult(e, -1L, false);
779 case '+':
780 i = scan(scpriv, tokval);
781 return expr6(critical);
783 case '~':
784 i = scan(scpriv, tokval);
785 e = expr6(critical);
786 if (!e)
787 return NULL;
788 if (is_just_unknown(e))
789 return unknown_expr();
790 else if (!is_simple(e)) {
791 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
792 " scalar values");
793 return NULL;
795 return scalarvect(~reloc_value(e));
797 case '!':
798 i = scan(scpriv, tokval);
799 e = expr6(critical);
800 if (!e)
801 return NULL;
802 if (is_just_unknown(e))
803 return unknown_expr();
804 else if (!is_simple(e)) {
805 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
806 " scalar values");
807 return NULL;
809 return scalarvect(!reloc_value(e));
811 case TOKEN_IFUNC:
813 enum ifunc func = tokval->t_integer;
814 i = scan(scpriv, tokval);
815 e = expr6(critical);
816 if (!e)
817 return NULL;
818 if (is_just_unknown(e))
819 return unknown_expr();
820 else if (!is_simple(e)) {
821 nasm_error(ERR_NONFATAL, "function may only be applied to"
822 " scalar values");
823 return NULL;
825 return scalarvect(eval_ifunc(reloc_value(e), func));
828 case TOKEN_SEG:
829 i = scan(scpriv, tokval);
830 e = expr6(critical);
831 if (!e)
832 return NULL;
833 e = segment_part(e);
834 if (!e)
835 return NULL;
836 if (is_unknown(e) && critical) {
837 nasm_error(ERR_NONFATAL, "unable to determine segment base");
838 return NULL;
840 return e;
842 case TOKEN_FLOATIZE:
843 return eval_floatize(tokval->t_integer);
845 case TOKEN_STRFUNC:
846 return eval_strfunc(tokval->t_integer);
848 case '(':
849 i = scan(scpriv, tokval);
850 e = bexpr(critical);
851 if (!e)
852 return NULL;
853 if (i != ')') {
854 nasm_error(ERR_NONFATAL, "expecting `)'");
855 return NULL;
857 i = scan(scpriv, tokval);
858 return e;
860 case TOKEN_NUM:
861 case TOKEN_STR:
862 case TOKEN_REG:
863 case TOKEN_ID:
864 case TOKEN_INSN: /* Opcodes that occur here are really labels */
865 case TOKEN_HERE:
866 case TOKEN_BASE:
867 case TOKEN_DECORATOR:
868 begintemp();
869 switch (i) {
870 case TOKEN_NUM:
871 addtotemp(EXPR_SIMPLE, tokval->t_integer);
872 break;
873 case TOKEN_STR:
874 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
875 if (rn_warn)
876 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
877 addtotemp(EXPR_SIMPLE, tmpval);
878 break;
879 case TOKEN_REG:
880 addtotemp(tokval->t_integer, 1L);
881 if (hint && hint->type == EAH_NOHINT)
882 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
883 break;
884 case TOKEN_ID:
885 case TOKEN_INSN:
886 case TOKEN_HERE:
887 case TOKEN_BASE:
889 * If !location.known, this indicates that no
890 * symbol, Here or Base references are valid because we
891 * are in preprocess-only mode.
893 if (!location.known) {
894 nasm_error(ERR_NONFATAL,
895 "%s not supported in preprocess-only mode",
896 (i == TOKEN_HERE ? "`$'" :
897 i == TOKEN_BASE ? "`$$'" :
898 "symbol references"));
899 addtotemp(EXPR_UNKNOWN, 1L);
900 break;
903 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
904 if (i == TOKEN_BASE) {
905 label_seg = in_absolute ? absolute.segment : location.segment;
906 label_ofs = 0;
907 } else if (i == TOKEN_HERE) {
908 label_seg = in_absolute ? absolute.segment : location.segment;
909 label_ofs = in_absolute ? absolute.offset : location.offset;
910 } else {
911 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
912 scope = local_scope(tokval->t_charptr);
913 if (critical == 2) {
914 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
915 scope,tokval->t_charptr);
916 return NULL;
917 } else if (critical == 1) {
918 nasm_error(ERR_NONFATAL,
919 "symbol `%s%s' not defined before use",
920 scope,tokval->t_charptr);
921 return NULL;
922 } else {
923 if (opflags)
924 *opflags |= OPFLAG_FORWARD;
925 type = EXPR_UNKNOWN;
926 label_seg = NO_SEG;
927 label_ofs = 1;
930 if (opflags && is_extern(tokval->t_charptr))
931 *opflags |= OPFLAG_EXTERN;
933 addtotemp(type, label_ofs);
934 if (label_seg != NO_SEG)
935 addtotemp(EXPR_SEGBASE + label_seg, 1L);
936 break;
937 case TOKEN_DECORATOR:
938 addtotemp(EXPR_RDSAE, tokval->t_integer);
939 break;
941 i = scan(scpriv, tokval);
942 return finishtemp();
944 default:
945 nasm_error(ERR_NONFATAL, "expression syntax error");
946 return NULL;
950 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
951 int *fwref, int critical, struct eval_hints *hints)
953 expr *e;
954 expr *f = NULL;
956 hint = hints;
957 if (hint)
958 hint->type = EAH_NOHINT;
960 if (critical & CRITICAL) {
961 critical &= ~CRITICAL;
962 bexpr = rexp0;
963 } else
964 bexpr = expr0;
966 scan = sc;
967 scpriv = scprivate;
968 tokval = tv;
969 opflags = fwref;
971 if (tokval->t_type == TOKEN_INVALID)
972 i = scan(scpriv, tokval);
973 else
974 i = tokval->t_type;
976 while (ntempexprs) /* initialize temporary storage */
977 nasm_free(tempexprs[--ntempexprs]);
979 e = bexpr(critical);
980 if (!e)
981 return NULL;
983 if (i == TOKEN_WRT) {
984 i = scan(scpriv, tokval); /* eat the WRT */
985 f = expr6(critical);
986 if (!f)
987 return NULL;
989 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
990 if (f) {
991 expr *g;
992 if (is_just_unknown(f))
993 g = unknown_expr();
994 else {
995 int64_t value;
996 begintemp();
997 if (!is_reloc(f)) {
998 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
999 return NULL;
1001 value = reloc_seg(f);
1002 if (value == NO_SEG)
1003 value = reloc_value(f) | SEG_ABS;
1004 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
1005 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1006 return NULL;
1008 addtotemp(EXPR_WRT, value);
1009 g = finishtemp();
1011 e = add_vectors(e, g);
1013 return e;