Add wrappers around fopen(), use mmap on glibc
[nasm.git] / eval.c
blob029d8a045b7bb11b295758a20db7f30343cc8079
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>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "eval.h"
49 #include "labels.h"
50 #include "float.h"
52 #define TEMPEXPRS_DELTA 128
53 #define TEMPEXPR_DELTA 8
55 static scanner scan; /* Address of scanner routine */
57 static expr **tempexprs = NULL;
58 static int ntempexprs;
59 static int tempexprs_size = 0;
61 static expr *tempexpr;
62 static int ntempexpr;
63 static int tempexpr_size;
65 static struct tokenval *tokval; /* The current token */
66 static int i; /* The t_type of tokval */
68 static void *scpriv;
69 static int *opflags;
71 static struct eval_hints *hint;
73 extern int in_abs_seg; /* ABSOLUTE segment flag */
74 extern int32_t abs_seg; /* ABSOLUTE segment */
75 extern int32_t abs_offset; /* ABSOLUTE segment offset */
78 * Unimportant cleanup is done to avoid confusing people who are trying
79 * to debug real memory leaks
81 void eval_cleanup(void)
83 while (ntempexprs)
84 nasm_free(tempexprs[--ntempexprs]);
85 nasm_free(tempexprs);
89 * Construct a temporary expression.
91 static void begintemp(void)
93 tempexpr = NULL;
94 tempexpr_size = ntempexpr = 0;
97 static void addtotemp(int32_t type, int64_t value)
99 while (ntempexpr >= tempexpr_size) {
100 tempexpr_size += TEMPEXPR_DELTA;
101 tempexpr = nasm_realloc(tempexpr,
102 tempexpr_size * sizeof(*tempexpr));
104 tempexpr[ntempexpr].type = type;
105 tempexpr[ntempexpr++].value = value;
108 static expr *finishtemp(void)
110 addtotemp(0L, 0L); /* terminate */
111 while (ntempexprs >= tempexprs_size) {
112 tempexprs_size += TEMPEXPRS_DELTA;
113 tempexprs = nasm_realloc(tempexprs,
114 tempexprs_size * sizeof(*tempexprs));
116 return tempexprs[ntempexprs++] = tempexpr;
120 * Add two vector datatypes. We have some bizarre behaviour on far-
121 * absolute segment types: we preserve them during addition _only_
122 * if one of the segments is a truly pure scalar.
124 static expr *add_vectors(expr * p, expr * q)
126 int preserve;
128 preserve = is_really_simple(p) || is_really_simple(q);
130 begintemp();
132 while (p->type && q->type &&
133 p->type < EXPR_SEGBASE + SEG_ABS &&
134 q->type < EXPR_SEGBASE + SEG_ABS) {
135 int lasttype;
137 if (p->type > q->type) {
138 addtotemp(q->type, q->value);
139 lasttype = q++->type;
140 } else if (p->type < q->type) {
141 addtotemp(p->type, p->value);
142 lasttype = p++->type;
143 } else { /* *p and *q have same type */
144 int64_t sum = p->value + q->value;
145 if (sum) {
146 addtotemp(p->type, sum);
147 if (hint)
148 hint->type = EAH_SUMMED;
150 lasttype = p->type;
151 p++, q++;
153 if (lasttype == EXPR_UNKNOWN) {
154 return finishtemp();
157 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
158 addtotemp(p->type, p->value);
159 p++;
161 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
162 addtotemp(q->type, q->value);
163 q++;
166 return finishtemp();
170 * Multiply a vector by a scalar. Strip far-absolute segment part
171 * if present.
173 * Explicit treatment of UNKNOWN is not required in this routine,
174 * since it will silently do the Right Thing anyway.
176 * If `affect_hints' is set, we also change the hint type to
177 * NOTBASE if a MAKEBASE hint points at a register being
178 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
179 * as the base register.
181 static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
183 expr *p = vect;
185 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
186 p->value = scalar * (p->value);
187 if (hint && hint->type == EAH_MAKEBASE &&
188 p->type == hint->base && affect_hints)
189 hint->type = EAH_NOTBASE;
190 p++;
192 p->type = 0;
194 return vect;
197 static expr *scalarvect(int64_t scalar)
199 begintemp();
200 addtotemp(EXPR_SIMPLE, scalar);
201 return finishtemp();
204 static expr *unknown_expr(void)
206 begintemp();
207 addtotemp(EXPR_UNKNOWN, 1L);
208 return finishtemp();
212 * The SEG operator: calculate the segment part of a relocatable
213 * value. Return NULL, as usual, if an error occurs. Report the
214 * error too.
216 static expr *segment_part(expr * e)
218 int32_t seg;
220 if (is_unknown(e))
221 return unknown_expr();
223 if (!is_reloc(e)) {
224 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
225 return NULL;
228 seg = reloc_seg(e);
229 if (seg == NO_SEG) {
230 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
231 return NULL;
232 } else if (seg & SEG_ABS) {
233 return scalarvect(seg & ~SEG_ABS);
234 } else if (seg & 1) {
235 nasm_error(ERR_NONFATAL, "SEG applied to something which"
236 " is already a segment base");
237 return NULL;
238 } else {
239 int32_t base = ofmt->segbase(seg + 1);
241 begintemp();
242 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
243 1L);
244 return finishtemp();
249 * Recursive-descent parser. Called with a single boolean operand,
250 * which is true if the evaluation is critical (i.e. unresolved
251 * symbols are an error condition). Must update the global `i' to
252 * reflect the token after the parsed string. May return NULL.
254 * evaluate() should report its own errors: on return it is assumed
255 * that if NULL has been returned, the error has already been
256 * reported.
260 * Grammar parsed is:
262 * expr : bexpr [ WRT expr6 ]
263 * bexpr : rexp0 or expr0 depending on relative-mode setting
264 * rexp0 : rexp1 [ {||} rexp1...]
265 * rexp1 : rexp2 [ {^^} rexp2...]
266 * rexp2 : rexp3 [ {&&} rexp3...]
267 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
268 * expr0 : expr1 [ {|} expr1...]
269 * expr1 : expr2 [ {^} expr2...]
270 * expr2 : expr3 [ {&} expr3...]
271 * expr3 : expr4 [ {<<,>>} expr4...]
272 * expr4 : expr5 [ {+,-} expr5...]
273 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
274 * expr6 : { ~,+,-,IFUNC,SEG } expr6
275 * | (bexpr)
276 * | symbol
277 * | $
278 * | number
281 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
283 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
284 static expr *expr4(int), *expr5(int), *expr6(int);
286 static expr *(*bexpr) (int);
288 static expr *rexp0(int critical)
290 expr *e, *f;
292 e = rexp1(critical);
293 if (!e)
294 return NULL;
296 while (i == TOKEN_DBL_OR) {
297 i = scan(scpriv, tokval);
298 f = rexp1(critical);
299 if (!f)
300 return NULL;
301 if (!(is_simple(e) || is_just_unknown(e)) ||
302 !(is_simple(f) || is_just_unknown(f))) {
303 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
304 " scalar values");
307 if (is_just_unknown(e) || is_just_unknown(f))
308 e = unknown_expr();
309 else
310 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
312 return e;
315 static expr *rexp1(int critical)
317 expr *e, *f;
319 e = rexp2(critical);
320 if (!e)
321 return NULL;
323 while (i == TOKEN_DBL_XOR) {
324 i = scan(scpriv, tokval);
325 f = rexp2(critical);
326 if (!f)
327 return NULL;
328 if (!(is_simple(e) || is_just_unknown(e)) ||
329 !(is_simple(f) || is_just_unknown(f))) {
330 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
331 " scalar values");
334 if (is_just_unknown(e) || is_just_unknown(f))
335 e = unknown_expr();
336 else
337 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
339 return e;
342 static expr *rexp2(int critical)
344 expr *e, *f;
346 e = rexp3(critical);
347 if (!e)
348 return NULL;
349 while (i == TOKEN_DBL_AND) {
350 i = scan(scpriv, tokval);
351 f = rexp3(critical);
352 if (!f)
353 return NULL;
354 if (!(is_simple(e) || is_just_unknown(e)) ||
355 !(is_simple(f) || is_just_unknown(f))) {
356 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
357 " scalar values");
359 if (is_just_unknown(e) || is_just_unknown(f))
360 e = unknown_expr();
361 else
362 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
364 return e;
367 static expr *rexp3(int critical)
369 expr *e, *f;
370 int64_t v;
372 e = expr0(critical);
373 if (!e)
374 return NULL;
376 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
377 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
378 int j = i;
379 i = scan(scpriv, tokval);
380 f = expr0(critical);
381 if (!f)
382 return NULL;
384 e = add_vectors(e, scalar_mult(f, -1L, false));
386 switch (j) {
387 case TOKEN_EQ:
388 case TOKEN_NE:
389 if (is_unknown(e))
390 v = -1; /* means unknown */
391 else if (!is_really_simple(e) || reloc_value(e) != 0)
392 v = (j == TOKEN_NE); /* unequal, so return true if NE */
393 else
394 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
395 break;
396 default:
397 if (is_unknown(e))
398 v = -1; /* means unknown */
399 else if (!is_really_simple(e)) {
400 nasm_error(ERR_NONFATAL,
401 "`%s': operands differ by a non-scalar",
402 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
403 TOKEN_GE ? ">=" : ">"));
404 v = 0; /* must set it to _something_ */
405 } else {
406 int64_t vv = reloc_value(e);
407 if (vv == 0)
408 v = (j == TOKEN_LE || j == TOKEN_GE);
409 else if (vv > 0)
410 v = (j == TOKEN_GE || j == TOKEN_GT);
411 else /* vv < 0 */
412 v = (j == TOKEN_LE || j == TOKEN_LT);
414 break;
417 if (v == -1)
418 e = unknown_expr();
419 else
420 e = scalarvect(v);
422 return e;
425 static expr *expr0(int critical)
427 expr *e, *f;
429 e = expr1(critical);
430 if (!e)
431 return NULL;
433 while (i == '|') {
434 i = scan(scpriv, tokval);
435 f = expr1(critical);
436 if (!f)
437 return NULL;
438 if (!(is_simple(e) || is_just_unknown(e)) ||
439 !(is_simple(f) || is_just_unknown(f))) {
440 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
441 " scalar values");
443 if (is_just_unknown(e) || is_just_unknown(f))
444 e = unknown_expr();
445 else
446 e = scalarvect(reloc_value(e) | reloc_value(f));
448 return e;
451 static expr *expr1(int critical)
453 expr *e, *f;
455 e = expr2(critical);
456 if (!e)
457 return NULL;
459 while (i == '^') {
460 i = scan(scpriv, tokval);
461 f = expr2(critical);
462 if (!f)
463 return NULL;
464 if (!(is_simple(e) || is_just_unknown(e)) ||
465 !(is_simple(f) || is_just_unknown(f))) {
466 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
467 " scalar values");
469 if (is_just_unknown(e) || is_just_unknown(f))
470 e = unknown_expr();
471 else
472 e = scalarvect(reloc_value(e) ^ reloc_value(f));
474 return e;
477 static expr *expr2(int critical)
479 expr *e, *f;
481 e = expr3(critical);
482 if (!e)
483 return NULL;
485 while (i == '&') {
486 i = scan(scpriv, tokval);
487 f = expr3(critical);
488 if (!f)
489 return NULL;
490 if (!(is_simple(e) || is_just_unknown(e)) ||
491 !(is_simple(f) || is_just_unknown(f))) {
492 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
493 " scalar values");
495 if (is_just_unknown(e) || is_just_unknown(f))
496 e = unknown_expr();
497 else
498 e = scalarvect(reloc_value(e) & reloc_value(f));
500 return e;
503 static expr *expr3(int critical)
505 expr *e, *f;
507 e = expr4(critical);
508 if (!e)
509 return NULL;
511 while (i == TOKEN_SHL || i == TOKEN_SHR) {
512 int j = i;
513 i = scan(scpriv, tokval);
514 f = expr4(critical);
515 if (!f)
516 return NULL;
517 if (!(is_simple(e) || is_just_unknown(e)) ||
518 !(is_simple(f) || is_just_unknown(f))) {
519 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
520 " scalar values");
521 } else if (is_just_unknown(e) || is_just_unknown(f)) {
522 e = unknown_expr();
523 } else
524 switch (j) {
525 case TOKEN_SHL:
526 e = scalarvect(reloc_value(e) << reloc_value(f));
527 break;
528 case TOKEN_SHR:
529 e = scalarvect(((uint64_t)reloc_value(e)) >>
530 reloc_value(f));
531 break;
534 return e;
537 static expr *expr4(int critical)
539 expr *e, *f;
541 e = expr5(critical);
542 if (!e)
543 return NULL;
544 while (i == '+' || i == '-') {
545 int j = i;
546 i = scan(scpriv, tokval);
547 f = expr5(critical);
548 if (!f)
549 return NULL;
550 switch (j) {
551 case '+':
552 e = add_vectors(e, f);
553 break;
554 case '-':
555 e = add_vectors(e, scalar_mult(f, -1L, false));
556 break;
559 return e;
562 static expr *expr5(int critical)
564 expr *e, *f;
566 e = expr6(critical);
567 if (!e)
568 return NULL;
569 while (i == '*' || i == '/' || i == '%' ||
570 i == TOKEN_SDIV || i == TOKEN_SMOD) {
571 int j = i;
572 i = scan(scpriv, tokval);
573 f = expr6(critical);
574 if (!f)
575 return NULL;
576 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
577 !(is_simple(f) || is_just_unknown(f)))) {
578 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
579 " scalar values");
580 return NULL;
582 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
583 nasm_error(ERR_NONFATAL, "division by zero");
584 return NULL;
586 switch (j) {
587 case '*':
588 if (is_simple(e))
589 e = scalar_mult(f, reloc_value(e), true);
590 else if (is_simple(f))
591 e = scalar_mult(e, reloc_value(f), true);
592 else if (is_just_unknown(e) && is_just_unknown(f))
593 e = unknown_expr();
594 else {
595 nasm_error(ERR_NONFATAL, "unable to multiply two "
596 "non-scalar objects");
597 return NULL;
599 break;
600 case '/':
601 if (is_just_unknown(e) || is_just_unknown(f))
602 e = unknown_expr();
603 else
604 e = scalarvect(((uint64_t)reloc_value(e)) /
605 ((uint64_t)reloc_value(f)));
606 break;
607 case '%':
608 if (is_just_unknown(e) || is_just_unknown(f))
609 e = unknown_expr();
610 else
611 e = scalarvect(((uint64_t)reloc_value(e)) %
612 ((uint64_t)reloc_value(f)));
613 break;
614 case TOKEN_SDIV:
615 if (is_just_unknown(e) || is_just_unknown(f))
616 e = unknown_expr();
617 else
618 e = scalarvect(((int64_t)reloc_value(e)) /
619 ((int64_t)reloc_value(f)));
620 break;
621 case TOKEN_SMOD:
622 if (is_just_unknown(e) || is_just_unknown(f))
623 e = unknown_expr();
624 else
625 e = scalarvect(((int64_t)reloc_value(e)) %
626 ((int64_t)reloc_value(f)));
627 break;
630 return e;
633 static expr *eval_floatize(enum floatize type)
635 uint8_t result[16], *p; /* Up to 128 bits */
636 static const struct {
637 int bytes, start, len;
638 } formats[] = {
639 { 1, 0, 1 }, /* FLOAT_8 */
640 { 2, 0, 2 }, /* FLOAT_16 */
641 { 4, 0, 4 }, /* FLOAT_32 */
642 { 8, 0, 8 }, /* FLOAT_64 */
643 { 10, 0, 8 }, /* FLOAT_80M */
644 { 10, 8, 2 }, /* FLOAT_80E */
645 { 16, 0, 8 }, /* FLOAT_128L */
646 { 16, 8, 8 }, /* FLOAT_128H */
648 int sign = 1;
649 int64_t val;
650 int j;
652 i = scan(scpriv, tokval);
653 if (i != '(') {
654 nasm_error(ERR_NONFATAL, "expecting `('");
655 return NULL;
657 i = scan(scpriv, tokval);
658 if (i == '-' || i == '+') {
659 sign = (i == '-') ? -1 : 1;
660 i = scan(scpriv, tokval);
662 if (i != TOKEN_FLOAT) {
663 nasm_error(ERR_NONFATAL, "expecting floating-point number");
664 return NULL;
666 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
667 return NULL;
668 i = scan(scpriv, tokval);
669 if (i != ')') {
670 nasm_error(ERR_NONFATAL, "expecting `)'");
671 return NULL;
674 p = result+formats[type].start+formats[type].len;
675 val = 0;
676 for (j = formats[type].len; j; j--) {
677 p--;
678 val = (val << 8) + *p;
681 begintemp();
682 addtotemp(EXPR_SIMPLE, val);
684 i = scan(scpriv, tokval);
685 return finishtemp();
688 static expr *eval_strfunc(enum strfunc type)
690 char *string;
691 size_t string_len;
692 int64_t val;
693 bool parens, rn_warn;
695 parens = false;
696 i = scan(scpriv, tokval);
697 if (i == '(') {
698 parens = true;
699 i = scan(scpriv, tokval);
701 if (i != TOKEN_STR) {
702 nasm_error(ERR_NONFATAL, "expecting string");
703 return NULL;
705 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
706 &string, type);
707 if (string_len == (size_t)-1) {
708 nasm_error(ERR_NONFATAL, "invalid string for transform");
709 return NULL;
712 val = readstrnum(string, string_len, &rn_warn);
713 if (parens) {
714 i = scan(scpriv, tokval);
715 if (i != ')') {
716 nasm_error(ERR_NONFATAL, "expecting `)'");
717 return NULL;
721 if (rn_warn)
722 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
724 begintemp();
725 addtotemp(EXPR_SIMPLE, val);
727 i = scan(scpriv, tokval);
728 return finishtemp();
731 static int64_t eval_ifunc(int64_t val, enum ifunc func)
733 int errtype;
734 uint64_t uval = (uint64_t)val;
735 int64_t rv;
737 switch (func) {
738 case IFUNC_ILOG2E:
739 case IFUNC_ILOG2W:
740 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
742 if (!is_power2(uval))
743 nasm_error(errtype, "ilog2 argument is not a power of two");
744 /* fall through */
745 case IFUNC_ILOG2F:
746 rv = ilog2_64(uval);
747 break;
749 case IFUNC_ILOG2C:
750 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
751 break;
753 default:
754 nasm_panic(0, "invalid IFUNC token %d", func);
755 rv = 0;
756 break;
759 return rv;
762 static expr *expr6(int critical)
764 int32_t type;
765 expr *e;
766 int32_t label_seg;
767 int64_t label_ofs;
768 int64_t tmpval;
769 bool rn_warn;
770 char *scope;
772 switch (i) {
773 case '-':
774 i = scan(scpriv, tokval);
775 e = expr6(critical);
776 if (!e)
777 return NULL;
778 return scalar_mult(e, -1L, false);
780 case '+':
781 i = scan(scpriv, tokval);
782 return expr6(critical);
784 case '~':
785 i = scan(scpriv, tokval);
786 e = expr6(critical);
787 if (!e)
788 return NULL;
789 if (is_just_unknown(e))
790 return unknown_expr();
791 else if (!is_simple(e)) {
792 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
793 " scalar values");
794 return NULL;
796 return scalarvect(~reloc_value(e));
798 case '!':
799 i = scan(scpriv, tokval);
800 e = expr6(critical);
801 if (!e)
802 return NULL;
803 if (is_just_unknown(e))
804 return unknown_expr();
805 else if (!is_simple(e)) {
806 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
807 " scalar values");
808 return NULL;
810 return scalarvect(!reloc_value(e));
812 case TOKEN_IFUNC:
814 enum ifunc func = tokval->t_integer;
815 i = scan(scpriv, tokval);
816 e = expr6(critical);
817 if (!e)
818 return NULL;
819 if (is_just_unknown(e))
820 return unknown_expr();
821 else if (!is_simple(e)) {
822 nasm_error(ERR_NONFATAL, "function may only be applied to"
823 " scalar values");
824 return NULL;
826 return scalarvect(eval_ifunc(reloc_value(e), func));
829 case TOKEN_SEG:
830 i = scan(scpriv, tokval);
831 e = expr6(critical);
832 if (!e)
833 return NULL;
834 e = segment_part(e);
835 if (!e)
836 return NULL;
837 if (is_unknown(e) && critical) {
838 nasm_error(ERR_NONFATAL, "unable to determine segment base");
839 return NULL;
841 return e;
843 case TOKEN_FLOATIZE:
844 return eval_floatize(tokval->t_integer);
846 case TOKEN_STRFUNC:
847 return eval_strfunc(tokval->t_integer);
849 case '(':
850 i = scan(scpriv, tokval);
851 e = bexpr(critical);
852 if (!e)
853 return NULL;
854 if (i != ')') {
855 nasm_error(ERR_NONFATAL, "expecting `)'");
856 return NULL;
858 i = scan(scpriv, tokval);
859 return e;
861 case TOKEN_NUM:
862 case TOKEN_STR:
863 case TOKEN_REG:
864 case TOKEN_ID:
865 case TOKEN_INSN: /* Opcodes that occur here are really labels */
866 case TOKEN_HERE:
867 case TOKEN_BASE:
868 case TOKEN_DECORATOR:
869 begintemp();
870 switch (i) {
871 case TOKEN_NUM:
872 addtotemp(EXPR_SIMPLE, tokval->t_integer);
873 break;
874 case TOKEN_STR:
875 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
876 if (rn_warn)
877 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
878 addtotemp(EXPR_SIMPLE, tmpval);
879 break;
880 case TOKEN_REG:
881 addtotemp(tokval->t_integer, 1L);
882 if (hint && hint->type == EAH_NOHINT)
883 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
884 break;
885 case TOKEN_ID:
886 case TOKEN_INSN:
887 case TOKEN_HERE:
888 case TOKEN_BASE:
890 * If !location.known, this indicates that no
891 * symbol, Here or Base references are valid because we
892 * are in preprocess-only mode.
894 if (!location.known) {
895 nasm_error(ERR_NONFATAL,
896 "%s not supported in preprocess-only mode",
897 (i == TOKEN_HERE ? "`$'" :
898 i == TOKEN_BASE ? "`$$'" :
899 "symbol references"));
900 addtotemp(EXPR_UNKNOWN, 1L);
901 break;
904 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
905 if (i == TOKEN_BASE) {
906 label_seg = in_abs_seg ? abs_seg : location.segment;
907 label_ofs = 0;
908 } else if (i == TOKEN_HERE) {
909 label_seg = in_abs_seg ? abs_seg : location.segment;
910 label_ofs = in_abs_seg ? abs_offset : location.offset;
911 } else {
912 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
913 scope = local_scope(tokval->t_charptr);
914 if (critical == 2) {
915 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
916 scope,tokval->t_charptr);
917 return NULL;
918 } else if (critical == 1) {
919 nasm_error(ERR_NONFATAL,
920 "symbol `%s%s' not defined before use",
921 scope,tokval->t_charptr);
922 return NULL;
923 } else {
924 if (opflags)
925 *opflags |= OPFLAG_FORWARD;
926 type = EXPR_UNKNOWN;
927 label_seg = NO_SEG;
928 label_ofs = 1;
931 if (opflags && is_extern(tokval->t_charptr))
932 *opflags |= OPFLAG_EXTERN;
934 addtotemp(type, label_ofs);
935 if (label_seg != NO_SEG)
936 addtotemp(EXPR_SEGBASE + label_seg, 1L);
937 break;
938 case TOKEN_DECORATOR:
939 addtotemp(EXPR_RDSAE, tokval->t_integer);
940 break;
942 i = scan(scpriv, tokval);
943 return finishtemp();
945 default:
946 nasm_error(ERR_NONFATAL, "expression syntax error");
947 return NULL;
951 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
952 int *fwref, int critical, struct eval_hints *hints)
954 expr *e;
955 expr *f = NULL;
957 hint = hints;
958 if (hint)
959 hint->type = EAH_NOHINT;
961 if (critical & CRITICAL) {
962 critical &= ~CRITICAL;
963 bexpr = rexp0;
964 } else
965 bexpr = expr0;
967 scan = sc;
968 scpriv = scprivate;
969 tokval = tv;
970 opflags = fwref;
972 if (tokval->t_type == TOKEN_INVALID)
973 i = scan(scpriv, tokval);
974 else
975 i = tokval->t_type;
977 while (ntempexprs) /* initialize temporary storage */
978 nasm_free(tempexprs[--ntempexprs]);
980 e = bexpr(critical);
981 if (!e)
982 return NULL;
984 if (i == TOKEN_WRT) {
985 i = scan(scpriv, tokval); /* eat the WRT */
986 f = expr6(critical);
987 if (!f)
988 return NULL;
990 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
991 if (f) {
992 expr *g;
993 if (is_just_unknown(f))
994 g = unknown_expr();
995 else {
996 int64_t value;
997 begintemp();
998 if (!is_reloc(f)) {
999 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1000 return NULL;
1002 value = reloc_seg(f);
1003 if (value == NO_SEG)
1004 value = reloc_value(f) | SEG_ABS;
1005 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
1006 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1007 return NULL;
1009 addtotemp(EXPR_WRT, value);
1010 g = finishtemp();
1012 e = add_vectors(e, g);
1014 return e;