Merge branch 'master' into elfmerge
[nasm.git] / eval.c
blobf1fb05eb8ca07b74476cfc99df5e6d5ea017cda9
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "eval.h"
50 #include "labels.h"
51 #include "float.h"
53 #define TEMPEXPRS_DELTA 128
54 #define TEMPEXPR_DELTA 8
56 static scanner scan; /* Address of scanner routine */
58 static expr **tempexprs = NULL;
59 static int ntempexprs;
60 static int tempexprs_size = 0;
62 static expr *tempexpr;
63 static int ntempexpr;
64 static int tempexpr_size;
66 static struct tokenval *tokval; /* The current token */
67 static int i; /* The t_type of tokval */
69 static void *scpriv;
70 static int *opflags;
72 static struct eval_hints *hint;
74 extern int in_abs_seg; /* ABSOLUTE segment flag */
75 extern int32_t abs_seg; /* ABSOLUTE segment */
76 extern int32_t abs_offset; /* ABSOLUTE segment offset */
79 * Unimportant cleanup is done to avoid confusing people who are trying
80 * to debug real memory leaks
82 void eval_cleanup(void)
84 while (ntempexprs)
85 nasm_free(tempexprs[--ntempexprs]);
86 nasm_free(tempexprs);
90 * Construct a temporary expression.
92 static void begintemp(void)
94 tempexpr = NULL;
95 tempexpr_size = ntempexpr = 0;
98 static void addtotemp(int32_t type, int64_t value)
100 while (ntempexpr >= tempexpr_size) {
101 tempexpr_size += TEMPEXPR_DELTA;
102 tempexpr = nasm_realloc(tempexpr,
103 tempexpr_size * sizeof(*tempexpr));
105 tempexpr[ntempexpr].type = type;
106 tempexpr[ntempexpr++].value = value;
109 static expr *finishtemp(void)
111 addtotemp(0L, 0L); /* terminate */
112 while (ntempexprs >= tempexprs_size) {
113 tempexprs_size += TEMPEXPRS_DELTA;
114 tempexprs = nasm_realloc(tempexprs,
115 tempexprs_size * sizeof(*tempexprs));
117 return tempexprs[ntempexprs++] = tempexpr;
121 * Add two vector datatypes. We have some bizarre behaviour on far-
122 * absolute segment types: we preserve them during addition _only_
123 * if one of the segments is a truly pure scalar.
125 static expr *add_vectors(expr * p, expr * q)
127 int preserve;
129 preserve = is_really_simple(p) || is_really_simple(q);
131 begintemp();
133 while (p->type && q->type &&
134 p->type < EXPR_SEGBASE + SEG_ABS &&
135 q->type < EXPR_SEGBASE + SEG_ABS) {
136 int lasttype;
138 if (p->type > q->type) {
139 addtotemp(q->type, q->value);
140 lasttype = q++->type;
141 } else if (p->type < q->type) {
142 addtotemp(p->type, p->value);
143 lasttype = p++->type;
144 } else { /* *p and *q have same type */
145 int64_t sum = p->value + q->value;
146 if (sum) {
147 addtotemp(p->type, sum);
148 if (hint)
149 hint->type = EAH_SUMMED;
151 lasttype = p->type;
152 p++, q++;
154 if (lasttype == EXPR_UNKNOWN) {
155 return finishtemp();
158 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
159 addtotemp(p->type, p->value);
160 p++;
162 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
163 addtotemp(q->type, q->value);
164 q++;
167 return finishtemp();
171 * Multiply a vector by a scalar. Strip far-absolute segment part
172 * if present.
174 * Explicit treatment of UNKNOWN is not required in this routine,
175 * since it will silently do the Right Thing anyway.
177 * If `affect_hints' is set, we also change the hint type to
178 * NOTBASE if a MAKEBASE hint points at a register being
179 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
180 * as the base register.
182 static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
184 expr *p = vect;
186 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
187 p->value = scalar * (p->value);
188 if (hint && hint->type == EAH_MAKEBASE &&
189 p->type == hint->base && affect_hints)
190 hint->type = EAH_NOTBASE;
191 p++;
193 p->type = 0;
195 return vect;
198 static expr *scalarvect(int64_t scalar)
200 begintemp();
201 addtotemp(EXPR_SIMPLE, scalar);
202 return finishtemp();
205 static expr *unknown_expr(void)
207 begintemp();
208 addtotemp(EXPR_UNKNOWN, 1L);
209 return finishtemp();
213 * The SEG operator: calculate the segment part of a relocatable
214 * value. Return NULL, as usual, if an error occurs. Report the
215 * error too.
217 static expr *segment_part(expr * e)
219 int32_t seg;
221 if (is_unknown(e))
222 return unknown_expr();
224 if (!is_reloc(e)) {
225 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
226 return NULL;
229 seg = reloc_seg(e);
230 if (seg == NO_SEG) {
231 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
232 return NULL;
233 } else if (seg & SEG_ABS) {
234 return scalarvect(seg & ~SEG_ABS);
235 } else if (seg & 1) {
236 nasm_error(ERR_NONFATAL, "SEG applied to something which"
237 " is already a segment base");
238 return NULL;
239 } else {
240 int32_t base = ofmt->segbase(seg + 1);
242 begintemp();
243 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
244 1L);
245 return finishtemp();
250 * Recursive-descent parser. Called with a single boolean operand,
251 * which is true if the evaluation is critical (i.e. unresolved
252 * symbols are an error condition). Must update the global `i' to
253 * reflect the token after the parsed string. May return NULL.
255 * evaluate() should report its own errors: on return it is assumed
256 * that if NULL has been returned, the error has already been
257 * reported.
261 * Grammar parsed is:
263 * expr : bexpr [ WRT expr6 ]
264 * bexpr : rexp0 or expr0 depending on relative-mode setting
265 * rexp0 : rexp1 [ {||} rexp1...]
266 * rexp1 : rexp2 [ {^^} rexp2...]
267 * rexp2 : rexp3 [ {&&} rexp3...]
268 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
269 * expr0 : expr1 [ {|} expr1...]
270 * expr1 : expr2 [ {^} expr2...]
271 * expr2 : expr3 [ {&} expr3...]
272 * expr3 : expr4 [ {<<,>>} expr4...]
273 * expr4 : expr5 [ {+,-} expr5...]
274 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
275 * expr6 : { ~,+,-,IFUNC,SEG } expr6
276 * | (bexpr)
277 * | symbol
278 * | $
279 * | number
282 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
284 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
285 static expr *expr4(int), *expr5(int), *expr6(int);
287 static expr *(*bexpr) (int);
289 static expr *rexp0(int critical)
291 expr *e, *f;
293 e = rexp1(critical);
294 if (!e)
295 return NULL;
297 while (i == TOKEN_DBL_OR) {
298 i = scan(scpriv, tokval);
299 f = rexp1(critical);
300 if (!f)
301 return NULL;
302 if (!(is_simple(e) || is_just_unknown(e)) ||
303 !(is_simple(f) || is_just_unknown(f))) {
304 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
305 " scalar values");
308 if (is_just_unknown(e) || is_just_unknown(f))
309 e = unknown_expr();
310 else
311 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
313 return e;
316 static expr *rexp1(int critical)
318 expr *e, *f;
320 e = rexp2(critical);
321 if (!e)
322 return NULL;
324 while (i == TOKEN_DBL_XOR) {
325 i = scan(scpriv, tokval);
326 f = rexp2(critical);
327 if (!f)
328 return NULL;
329 if (!(is_simple(e) || is_just_unknown(e)) ||
330 !(is_simple(f) || is_just_unknown(f))) {
331 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
332 " 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 *rexp2(int critical)
345 expr *e, *f;
347 e = rexp3(critical);
348 if (!e)
349 return NULL;
350 while (i == TOKEN_DBL_AND) {
351 i = scan(scpriv, tokval);
352 f = rexp3(critical);
353 if (!f)
354 return NULL;
355 if (!(is_simple(e) || is_just_unknown(e)) ||
356 !(is_simple(f) || is_just_unknown(f))) {
357 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
358 " scalar values");
360 if (is_just_unknown(e) || is_just_unknown(f))
361 e = unknown_expr();
362 else
363 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
365 return e;
368 static expr *rexp3(int critical)
370 expr *e, *f;
371 int64_t v;
373 e = expr0(critical);
374 if (!e)
375 return NULL;
377 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
378 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
379 int j = i;
380 i = scan(scpriv, tokval);
381 f = expr0(critical);
382 if (!f)
383 return NULL;
385 e = add_vectors(e, scalar_mult(f, -1L, false));
387 switch (j) {
388 case TOKEN_EQ:
389 case TOKEN_NE:
390 if (is_unknown(e))
391 v = -1; /* means unknown */
392 else if (!is_really_simple(e) || reloc_value(e) != 0)
393 v = (j == TOKEN_NE); /* unequal, so return true if NE */
394 else
395 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
396 break;
397 default:
398 if (is_unknown(e))
399 v = -1; /* means unknown */
400 else if (!is_really_simple(e)) {
401 nasm_error(ERR_NONFATAL,
402 "`%s': operands differ by a non-scalar",
403 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
404 TOKEN_GE ? ">=" : ">"));
405 v = 0; /* must set it to _something_ */
406 } else {
407 int64_t vv = reloc_value(e);
408 if (vv == 0)
409 v = (j == TOKEN_LE || j == TOKEN_GE);
410 else if (vv > 0)
411 v = (j == TOKEN_GE || j == TOKEN_GT);
412 else /* vv < 0 */
413 v = (j == TOKEN_LE || j == TOKEN_LT);
415 break;
418 if (v == -1)
419 e = unknown_expr();
420 else
421 e = scalarvect(v);
423 return e;
426 static expr *expr0(int critical)
428 expr *e, *f;
430 e = expr1(critical);
431 if (!e)
432 return NULL;
434 while (i == '|') {
435 i = scan(scpriv, tokval);
436 f = expr1(critical);
437 if (!f)
438 return NULL;
439 if (!(is_simple(e) || is_just_unknown(e)) ||
440 !(is_simple(f) || is_just_unknown(f))) {
441 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
442 " scalar values");
444 if (is_just_unknown(e) || is_just_unknown(f))
445 e = unknown_expr();
446 else
447 e = scalarvect(reloc_value(e) | reloc_value(f));
449 return e;
452 static expr *expr1(int critical)
454 expr *e, *f;
456 e = expr2(critical);
457 if (!e)
458 return NULL;
460 while (i == '^') {
461 i = scan(scpriv, tokval);
462 f = expr2(critical);
463 if (!f)
464 return NULL;
465 if (!(is_simple(e) || is_just_unknown(e)) ||
466 !(is_simple(f) || is_just_unknown(f))) {
467 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
468 " scalar values");
470 if (is_just_unknown(e) || is_just_unknown(f))
471 e = unknown_expr();
472 else
473 e = scalarvect(reloc_value(e) ^ reloc_value(f));
475 return e;
478 static expr *expr2(int critical)
480 expr *e, *f;
482 e = expr3(critical);
483 if (!e)
484 return NULL;
486 while (i == '&') {
487 i = scan(scpriv, tokval);
488 f = expr3(critical);
489 if (!f)
490 return NULL;
491 if (!(is_simple(e) || is_just_unknown(e)) ||
492 !(is_simple(f) || is_just_unknown(f))) {
493 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
494 " scalar values");
496 if (is_just_unknown(e) || is_just_unknown(f))
497 e = unknown_expr();
498 else
499 e = scalarvect(reloc_value(e) & reloc_value(f));
501 return e;
504 static expr *expr3(int critical)
506 expr *e, *f;
508 e = expr4(critical);
509 if (!e)
510 return NULL;
512 while (i == TOKEN_SHL || i == TOKEN_SHR) {
513 int j = i;
514 i = scan(scpriv, tokval);
515 f = expr4(critical);
516 if (!f)
517 return NULL;
518 if (!(is_simple(e) || is_just_unknown(e)) ||
519 !(is_simple(f) || is_just_unknown(f))) {
520 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
521 " scalar values");
522 } else if (is_just_unknown(e) || is_just_unknown(f)) {
523 e = unknown_expr();
524 } else
525 switch (j) {
526 case TOKEN_SHL:
527 e = scalarvect(reloc_value(e) << reloc_value(f));
528 break;
529 case TOKEN_SHR:
530 e = scalarvect(((uint64_t)reloc_value(e)) >>
531 reloc_value(f));
532 break;
535 return e;
538 static expr *expr4(int critical)
540 expr *e, *f;
542 e = expr5(critical);
543 if (!e)
544 return NULL;
545 while (i == '+' || i == '-') {
546 int j = i;
547 i = scan(scpriv, tokval);
548 f = expr5(critical);
549 if (!f)
550 return NULL;
551 switch (j) {
552 case '+':
553 e = add_vectors(e, f);
554 break;
555 case '-':
556 e = add_vectors(e, scalar_mult(f, -1L, false));
557 break;
560 return e;
563 static expr *expr5(int critical)
565 expr *e, *f;
567 e = expr6(critical);
568 if (!e)
569 return NULL;
570 while (i == '*' || i == '/' || i == '%' ||
571 i == TOKEN_SDIV || i == TOKEN_SMOD) {
572 int j = i;
573 i = scan(scpriv, tokval);
574 f = expr6(critical);
575 if (!f)
576 return NULL;
577 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
578 !(is_simple(f) || is_just_unknown(f)))) {
579 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
580 " scalar values");
581 return NULL;
583 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
584 nasm_error(ERR_NONFATAL, "division by zero");
585 return NULL;
587 switch (j) {
588 case '*':
589 if (is_simple(e))
590 e = scalar_mult(f, reloc_value(e), true);
591 else if (is_simple(f))
592 e = scalar_mult(e, reloc_value(f), true);
593 else if (is_just_unknown(e) && is_just_unknown(f))
594 e = unknown_expr();
595 else {
596 nasm_error(ERR_NONFATAL, "unable to multiply two "
597 "non-scalar objects");
598 return NULL;
600 break;
601 case '/':
602 if (is_just_unknown(e) || is_just_unknown(f))
603 e = unknown_expr();
604 else
605 e = scalarvect(((uint64_t)reloc_value(e)) /
606 ((uint64_t)reloc_value(f)));
607 break;
608 case '%':
609 if (is_just_unknown(e) || is_just_unknown(f))
610 e = unknown_expr();
611 else
612 e = scalarvect(((uint64_t)reloc_value(e)) %
613 ((uint64_t)reloc_value(f)));
614 break;
615 case TOKEN_SDIV:
616 if (is_just_unknown(e) || is_just_unknown(f))
617 e = unknown_expr();
618 else
619 e = scalarvect(((int64_t)reloc_value(e)) /
620 ((int64_t)reloc_value(f)));
621 break;
622 case TOKEN_SMOD:
623 if (is_just_unknown(e) || is_just_unknown(f))
624 e = unknown_expr();
625 else
626 e = scalarvect(((int64_t)reloc_value(e)) %
627 ((int64_t)reloc_value(f)));
628 break;
631 return e;
634 static expr *eval_floatize(enum floatize type)
636 uint8_t result[16], *p; /* Up to 128 bits */
637 static const struct {
638 int bytes, start, len;
639 } formats[] = {
640 { 1, 0, 1 }, /* FLOAT_8 */
641 { 2, 0, 2 }, /* FLOAT_16 */
642 { 4, 0, 4 }, /* FLOAT_32 */
643 { 8, 0, 8 }, /* FLOAT_64 */
644 { 10, 0, 8 }, /* FLOAT_80M */
645 { 10, 8, 2 }, /* FLOAT_80E */
646 { 16, 0, 8 }, /* FLOAT_128L */
647 { 16, 8, 8 }, /* FLOAT_128H */
649 int sign = 1;
650 int64_t val;
651 int j;
653 i = scan(scpriv, tokval);
654 if (i != '(') {
655 nasm_error(ERR_NONFATAL, "expecting `('");
656 return NULL;
658 i = scan(scpriv, tokval);
659 if (i == '-' || i == '+') {
660 sign = (i == '-') ? -1 : 1;
661 i = scan(scpriv, tokval);
663 if (i != TOKEN_FLOAT) {
664 nasm_error(ERR_NONFATAL, "expecting floating-point number");
665 return NULL;
667 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
668 return NULL;
669 i = scan(scpriv, tokval);
670 if (i != ')') {
671 nasm_error(ERR_NONFATAL, "expecting `)'");
672 return NULL;
675 p = result+formats[type].start+formats[type].len;
676 val = 0;
677 for (j = formats[type].len; j; j--) {
678 p--;
679 val = (val << 8) + *p;
682 begintemp();
683 addtotemp(EXPR_SIMPLE, val);
685 i = scan(scpriv, tokval);
686 return finishtemp();
689 static expr *eval_strfunc(enum strfunc type)
691 char *string;
692 size_t string_len;
693 int64_t val;
694 bool parens, rn_warn;
696 parens = false;
697 i = scan(scpriv, tokval);
698 if (i == '(') {
699 parens = true;
700 i = scan(scpriv, tokval);
702 if (i != TOKEN_STR) {
703 nasm_error(ERR_NONFATAL, "expecting string");
704 return NULL;
706 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
707 &string, type);
708 if (string_len == (size_t)-1) {
709 nasm_error(ERR_NONFATAL, "invalid string for transform");
710 return NULL;
713 val = readstrnum(string, string_len, &rn_warn);
714 if (parens) {
715 i = scan(scpriv, tokval);
716 if (i != ')') {
717 nasm_error(ERR_NONFATAL, "expecting `)'");
718 return NULL;
722 if (rn_warn)
723 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
725 begintemp();
726 addtotemp(EXPR_SIMPLE, val);
728 i = scan(scpriv, tokval);
729 return finishtemp();
732 static int64_t eval_ifunc(int64_t val, enum ifunc func)
734 int errtype;
735 uint64_t uval = (uint64_t)val;
736 int64_t rv;
738 switch (func) {
739 case IFUNC_ILOG2E:
740 case IFUNC_ILOG2W:
741 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
743 if (!is_power2(uval))
744 nasm_error(errtype, "ilog2 argument is not a power of two");
745 /* fall through */
746 case IFUNC_ILOG2F:
747 rv = ilog2_64(uval);
748 break;
750 case IFUNC_ILOG2C:
751 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
752 break;
754 default:
755 nasm_error(ERR_PANIC, "invalid IFUNC token %d", func);
756 rv = 0;
757 break;
760 return rv;
763 static expr *expr6(int critical)
765 int32_t type;
766 expr *e;
767 int32_t label_seg;
768 int64_t label_ofs;
769 int64_t tmpval;
770 bool rn_warn;
771 char *scope;
773 switch (i) {
774 case '-':
775 i = scan(scpriv, tokval);
776 e = expr6(critical);
777 if (!e)
778 return NULL;
779 return scalar_mult(e, -1L, false);
781 case '+':
782 i = scan(scpriv, tokval);
783 return expr6(critical);
785 case '~':
786 i = scan(scpriv, tokval);
787 e = expr6(critical);
788 if (!e)
789 return NULL;
790 if (is_just_unknown(e))
791 return unknown_expr();
792 else if (!is_simple(e)) {
793 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
794 " scalar values");
795 return NULL;
797 return scalarvect(~reloc_value(e));
799 case '!':
800 i = scan(scpriv, tokval);
801 e = expr6(critical);
802 if (!e)
803 return NULL;
804 if (is_just_unknown(e))
805 return unknown_expr();
806 else if (!is_simple(e)) {
807 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
808 " scalar values");
809 return NULL;
811 return scalarvect(!reloc_value(e));
813 case TOKEN_IFUNC:
815 enum ifunc func = tokval->t_integer;
816 i = scan(scpriv, tokval);
817 e = expr6(critical);
818 if (!e)
819 return NULL;
820 if (is_just_unknown(e))
821 return unknown_expr();
822 else if (!is_simple(e)) {
823 nasm_error(ERR_NONFATAL, "function may only be applied to"
824 " scalar values");
825 return NULL;
827 return scalarvect(eval_ifunc(reloc_value(e), func));
830 case TOKEN_SEG:
831 i = scan(scpriv, tokval);
832 e = expr6(critical);
833 if (!e)
834 return NULL;
835 e = segment_part(e);
836 if (!e)
837 return NULL;
838 if (is_unknown(e) && critical) {
839 nasm_error(ERR_NONFATAL, "unable to determine segment base");
840 return NULL;
842 return e;
844 case TOKEN_FLOATIZE:
845 return eval_floatize(tokval->t_integer);
847 case TOKEN_STRFUNC:
848 return eval_strfunc(tokval->t_integer);
850 case '(':
851 i = scan(scpriv, tokval);
852 e = bexpr(critical);
853 if (!e)
854 return NULL;
855 if (i != ')') {
856 nasm_error(ERR_NONFATAL, "expecting `)'");
857 return NULL;
859 i = scan(scpriv, tokval);
860 return e;
862 case TOKEN_NUM:
863 case TOKEN_STR:
864 case TOKEN_REG:
865 case TOKEN_ID:
866 case TOKEN_INSN: /* Opcodes that occur here are really labels */
867 case TOKEN_HERE:
868 case TOKEN_BASE:
869 case TOKEN_DECORATOR:
870 begintemp();
871 switch (i) {
872 case TOKEN_NUM:
873 addtotemp(EXPR_SIMPLE, tokval->t_integer);
874 break;
875 case TOKEN_STR:
876 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
877 if (rn_warn)
878 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
879 addtotemp(EXPR_SIMPLE, tmpval);
880 break;
881 case TOKEN_REG:
882 addtotemp(tokval->t_integer, 1L);
883 if (hint && hint->type == EAH_NOHINT)
884 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
885 break;
886 case TOKEN_ID:
887 case TOKEN_INSN:
888 case TOKEN_HERE:
889 case TOKEN_BASE:
891 * If !location.known, this indicates that no
892 * symbol, Here or Base references are valid because we
893 * are in preprocess-only mode.
895 if (!location.known) {
896 nasm_error(ERR_NONFATAL,
897 "%s not supported in preprocess-only mode",
898 (i == TOKEN_HERE ? "`$'" :
899 i == TOKEN_BASE ? "`$$'" :
900 "symbol references"));
901 addtotemp(EXPR_UNKNOWN, 1L);
902 break;
905 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
906 if (i == TOKEN_BASE) {
907 label_seg = in_abs_seg ? abs_seg : location.segment;
908 label_ofs = 0;
909 } else if (i == TOKEN_HERE) {
910 label_seg = in_abs_seg ? abs_seg : location.segment;
911 label_ofs = in_abs_seg ? abs_offset : location.offset;
912 } else {
913 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
914 scope = local_scope(tokval->t_charptr);
915 if (critical == 2) {
916 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
917 scope,tokval->t_charptr);
918 return NULL;
919 } else if (critical == 1) {
920 nasm_error(ERR_NONFATAL,
921 "symbol `%s%s' not defined before use",
922 scope,tokval->t_charptr);
923 return NULL;
924 } else {
925 if (opflags)
926 *opflags |= OPFLAG_FORWARD;
927 type = EXPR_UNKNOWN;
928 label_seg = NO_SEG;
929 label_ofs = 1;
932 if (opflags && is_extern(tokval->t_charptr))
933 *opflags |= OPFLAG_EXTERN;
935 addtotemp(type, label_ofs);
936 if (label_seg != NO_SEG)
937 addtotemp(EXPR_SEGBASE + label_seg, 1L);
938 break;
939 case TOKEN_DECORATOR:
940 addtotemp(EXPR_RDSAE, tokval->t_integer);
941 break;
943 i = scan(scpriv, tokval);
944 return finishtemp();
946 default:
947 nasm_error(ERR_NONFATAL, "expression syntax error");
948 return NULL;
952 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
953 int *fwref, int critical, struct eval_hints *hints)
955 expr *e;
956 expr *f = NULL;
958 hint = hints;
959 if (hint)
960 hint->type = EAH_NOHINT;
962 if (critical & CRITICAL) {
963 critical &= ~CRITICAL;
964 bexpr = rexp0;
965 } else
966 bexpr = expr0;
968 scan = sc;
969 scpriv = scprivate;
970 tokval = tv;
971 opflags = fwref;
973 if (tokval->t_type == TOKEN_INVALID)
974 i = scan(scpriv, tokval);
975 else
976 i = tokval->t_type;
978 while (ntempexprs) /* initialize temporary storage */
979 nasm_free(tempexprs[--ntempexprs]);
981 e = bexpr(critical);
982 if (!e)
983 return NULL;
985 if (i == TOKEN_WRT) {
986 i = scan(scpriv, tokval); /* eat the WRT */
987 f = expr6(critical);
988 if (!f)
989 return NULL;
991 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
992 if (f) {
993 expr *g;
994 if (is_just_unknown(f))
995 g = unknown_expr();
996 else {
997 int64_t value;
998 begintemp();
999 if (!is_reloc(f)) {
1000 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1001 return NULL;
1003 value = reloc_seg(f);
1004 if (value == NO_SEG)
1005 value = reloc_value(f) | SEG_ABS;
1006 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
1007 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1008 return NULL;
1010 addtotemp(EXPR_WRT, value);
1011 g = finishtemp();
1013 e = add_vectors(e, g);
1015 return e;