preproc: add dummy %pragma directive
[nasm.git] / eval.c
blob72e13c9d7dda5d7622a5e6d3264de6c12fd5284e
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2012 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 */
57 static lfunc labelfunc; /* Address of label routine */
59 static struct ofmt *outfmt; /* Structure of addresses of output routines */
61 static expr **tempexprs = NULL;
62 static int ntempexprs;
63 static int tempexprs_size = 0;
65 static expr *tempexpr;
66 static int ntempexpr;
67 static int tempexpr_size;
69 static struct tokenval *tokval; /* The current token */
70 static int i; /* The t_type of tokval */
72 static void *scpriv;
73 static struct location *location; /* Pointer to current line's segment,offset */
74 static int *opflags;
76 static struct eval_hints *hint;
78 extern int in_abs_seg; /* ABSOLUTE segment flag */
79 extern int32_t abs_seg; /* ABSOLUTE segment */
80 extern int32_t abs_offset; /* ABSOLUTE segment offset */
83 * Unimportant cleanup is done to avoid confusing people who are trying
84 * to debug real memory leaks
86 void eval_cleanup(void)
88 while (ntempexprs)
89 nasm_free(tempexprs[--ntempexprs]);
90 nasm_free(tempexprs);
94 * Construct a temporary expression.
96 static void begintemp(void)
98 tempexpr = NULL;
99 tempexpr_size = ntempexpr = 0;
102 static void addtotemp(int32_t type, int64_t value)
104 while (ntempexpr >= tempexpr_size) {
105 tempexpr_size += TEMPEXPR_DELTA;
106 tempexpr = nasm_realloc(tempexpr,
107 tempexpr_size * sizeof(*tempexpr));
109 tempexpr[ntempexpr].type = type;
110 tempexpr[ntempexpr++].value = value;
113 static expr *finishtemp(void)
115 addtotemp(0L, 0L); /* terminate */
116 while (ntempexprs >= tempexprs_size) {
117 tempexprs_size += TEMPEXPRS_DELTA;
118 tempexprs = nasm_realloc(tempexprs,
119 tempexprs_size * sizeof(*tempexprs));
121 return tempexprs[ntempexprs++] = tempexpr;
125 * Add two vector datatypes. We have some bizarre behaviour on far-
126 * absolute segment types: we preserve them during addition _only_
127 * if one of the segments is a truly pure scalar.
129 static expr *add_vectors(expr * p, expr * q)
131 int preserve;
133 preserve = is_really_simple(p) || is_really_simple(q);
135 begintemp();
137 while (p->type && q->type &&
138 p->type < EXPR_SEGBASE + SEG_ABS &&
139 q->type < EXPR_SEGBASE + SEG_ABS) {
140 int lasttype;
142 if (p->type > q->type) {
143 addtotemp(q->type, q->value);
144 lasttype = q++->type;
145 } else if (p->type < q->type) {
146 addtotemp(p->type, p->value);
147 lasttype = p++->type;
148 } else { /* *p and *q have same type */
149 int64_t sum = p->value + q->value;
150 if (sum) {
151 addtotemp(p->type, sum);
152 if (hint)
153 hint->type = EAH_SUMMED;
155 lasttype = p->type;
156 p++, q++;
158 if (lasttype == EXPR_UNKNOWN) {
159 return finishtemp();
162 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
163 addtotemp(p->type, p->value);
164 p++;
166 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
167 addtotemp(q->type, q->value);
168 q++;
171 return finishtemp();
175 * Multiply a vector by a scalar. Strip far-absolute segment part
176 * if present.
178 * Explicit treatment of UNKNOWN is not required in this routine,
179 * since it will silently do the Right Thing anyway.
181 * If `affect_hints' is set, we also change the hint type to
182 * NOTBASE if a MAKEBASE hint points at a register being
183 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
184 * as the base register.
186 static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
188 expr *p = vect;
190 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
191 p->value = scalar * (p->value);
192 if (hint && hint->type == EAH_MAKEBASE &&
193 p->type == hint->base && affect_hints)
194 hint->type = EAH_NOTBASE;
195 p++;
197 p->type = 0;
199 return vect;
202 static expr *scalarvect(int64_t scalar)
204 begintemp();
205 addtotemp(EXPR_SIMPLE, scalar);
206 return finishtemp();
209 static expr *unknown_expr(void)
211 begintemp();
212 addtotemp(EXPR_UNKNOWN, 1L);
213 return finishtemp();
217 * The SEG operator: calculate the segment part of a relocatable
218 * value. Return NULL, as usual, if an error occurs. Report the
219 * error too.
221 static expr *segment_part(expr * e)
223 int32_t seg;
225 if (is_unknown(e))
226 return unknown_expr();
228 if (!is_reloc(e)) {
229 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
230 return NULL;
233 seg = reloc_seg(e);
234 if (seg == NO_SEG) {
235 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
236 return NULL;
237 } else if (seg & SEG_ABS) {
238 return scalarvect(seg & ~SEG_ABS);
239 } else if (seg & 1) {
240 nasm_error(ERR_NONFATAL, "SEG applied to something which"
241 " is already a segment base");
242 return NULL;
243 } else {
244 int32_t base = outfmt->segbase(seg + 1);
246 begintemp();
247 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
248 1L);
249 return finishtemp();
254 * Recursive-descent parser. Called with a single boolean operand,
255 * which is true if the evaluation is critical (i.e. unresolved
256 * symbols are an error condition). Must update the global `i' to
257 * reflect the token after the parsed string. May return NULL.
259 * evaluate() should report its own errors: on return it is assumed
260 * that if NULL has been returned, the error has already been
261 * reported.
265 * Grammar parsed is:
267 * expr : bexpr [ WRT expr6 ]
268 * bexpr : rexp0 or expr0 depending on relative-mode setting
269 * rexp0 : rexp1 [ {||} rexp1...]
270 * rexp1 : rexp2 [ {^^} rexp2...]
271 * rexp2 : rexp3 [ {&&} rexp3...]
272 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
273 * expr0 : expr1 [ {|} expr1...]
274 * expr1 : expr2 [ {^} expr2...]
275 * expr2 : expr3 [ {&} expr3...]
276 * expr3 : expr4 [ {<<,>>} expr4...]
277 * expr4 : expr5 [ {+,-} expr5...]
278 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
279 * expr6 : { ~,+,-,IFUNC,SEG } expr6
280 * | (bexpr)
281 * | symbol
282 * | $
283 * | number
286 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
288 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
289 static expr *expr4(int), *expr5(int), *expr6(int);
291 static expr *(*bexpr) (int);
293 static expr *rexp0(int critical)
295 expr *e, *f;
297 e = rexp1(critical);
298 if (!e)
299 return NULL;
301 while (i == TOKEN_DBL_OR) {
302 i = scan(scpriv, tokval);
303 f = rexp1(critical);
304 if (!f)
305 return NULL;
306 if (!(is_simple(e) || is_just_unknown(e)) ||
307 !(is_simple(f) || is_just_unknown(f))) {
308 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
309 " scalar values");
312 if (is_just_unknown(e) || is_just_unknown(f))
313 e = unknown_expr();
314 else
315 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
317 return e;
320 static expr *rexp1(int critical)
322 expr *e, *f;
324 e = rexp2(critical);
325 if (!e)
326 return NULL;
328 while (i == TOKEN_DBL_XOR) {
329 i = scan(scpriv, tokval);
330 f = rexp2(critical);
331 if (!f)
332 return NULL;
333 if (!(is_simple(e) || is_just_unknown(e)) ||
334 !(is_simple(f) || is_just_unknown(f))) {
335 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
336 " scalar values");
339 if (is_just_unknown(e) || is_just_unknown(f))
340 e = unknown_expr();
341 else
342 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
344 return e;
347 static expr *rexp2(int critical)
349 expr *e, *f;
351 e = rexp3(critical);
352 if (!e)
353 return NULL;
354 while (i == TOKEN_DBL_AND) {
355 i = scan(scpriv, tokval);
356 f = rexp3(critical);
357 if (!f)
358 return NULL;
359 if (!(is_simple(e) || is_just_unknown(e)) ||
360 !(is_simple(f) || is_just_unknown(f))) {
361 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
362 " scalar values");
364 if (is_just_unknown(e) || is_just_unknown(f))
365 e = unknown_expr();
366 else
367 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
369 return e;
372 static expr *rexp3(int critical)
374 expr *e, *f;
375 int64_t v;
377 e = expr0(critical);
378 if (!e)
379 return NULL;
381 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
382 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
383 int j = i;
384 i = scan(scpriv, tokval);
385 f = expr0(critical);
386 if (!f)
387 return NULL;
389 e = add_vectors(e, scalar_mult(f, -1L, false));
391 switch (j) {
392 case TOKEN_EQ:
393 case TOKEN_NE:
394 if (is_unknown(e))
395 v = -1; /* means unknown */
396 else if (!is_really_simple(e) || reloc_value(e) != 0)
397 v = (j == TOKEN_NE); /* unequal, so return true if NE */
398 else
399 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
400 break;
401 default:
402 if (is_unknown(e))
403 v = -1; /* means unknown */
404 else if (!is_really_simple(e)) {
405 nasm_error(ERR_NONFATAL,
406 "`%s': operands differ by a non-scalar",
407 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
408 TOKEN_GE ? ">=" : ">"));
409 v = 0; /* must set it to _something_ */
410 } else {
411 int64_t vv = reloc_value(e);
412 if (vv == 0)
413 v = (j == TOKEN_LE || j == TOKEN_GE);
414 else if (vv > 0)
415 v = (j == TOKEN_GE || j == TOKEN_GT);
416 else /* vv < 0 */
417 v = (j == TOKEN_LE || j == TOKEN_LT);
419 break;
422 if (v == -1)
423 e = unknown_expr();
424 else
425 e = scalarvect(v);
427 return e;
430 static expr *expr0(int critical)
432 expr *e, *f;
434 e = expr1(critical);
435 if (!e)
436 return NULL;
438 while (i == '|') {
439 i = scan(scpriv, tokval);
440 f = expr1(critical);
441 if (!f)
442 return NULL;
443 if (!(is_simple(e) || is_just_unknown(e)) ||
444 !(is_simple(f) || is_just_unknown(f))) {
445 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
446 " scalar values");
448 if (is_just_unknown(e) || is_just_unknown(f))
449 e = unknown_expr();
450 else
451 e = scalarvect(reloc_value(e) | reloc_value(f));
453 return e;
456 static expr *expr1(int critical)
458 expr *e, *f;
460 e = expr2(critical);
461 if (!e)
462 return NULL;
464 while (i == '^') {
465 i = scan(scpriv, tokval);
466 f = expr2(critical);
467 if (!f)
468 return NULL;
469 if (!(is_simple(e) || is_just_unknown(e)) ||
470 !(is_simple(f) || is_just_unknown(f))) {
471 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
472 " scalar values");
474 if (is_just_unknown(e) || is_just_unknown(f))
475 e = unknown_expr();
476 else
477 e = scalarvect(reloc_value(e) ^ reloc_value(f));
479 return e;
482 static expr *expr2(int critical)
484 expr *e, *f;
486 e = expr3(critical);
487 if (!e)
488 return NULL;
490 while (i == '&') {
491 i = scan(scpriv, tokval);
492 f = expr3(critical);
493 if (!f)
494 return NULL;
495 if (!(is_simple(e) || is_just_unknown(e)) ||
496 !(is_simple(f) || is_just_unknown(f))) {
497 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
498 " scalar values");
500 if (is_just_unknown(e) || is_just_unknown(f))
501 e = unknown_expr();
502 else
503 e = scalarvect(reloc_value(e) & reloc_value(f));
505 return e;
508 static expr *expr3(int critical)
510 expr *e, *f;
512 e = expr4(critical);
513 if (!e)
514 return NULL;
516 while (i == TOKEN_SHL || i == TOKEN_SHR) {
517 int j = i;
518 i = scan(scpriv, tokval);
519 f = expr4(critical);
520 if (!f)
521 return NULL;
522 if (!(is_simple(e) || is_just_unknown(e)) ||
523 !(is_simple(f) || is_just_unknown(f))) {
524 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
525 " scalar values");
526 } else if (is_just_unknown(e) || is_just_unknown(f)) {
527 e = unknown_expr();
528 } else
529 switch (j) {
530 case TOKEN_SHL:
531 e = scalarvect(reloc_value(e) << reloc_value(f));
532 break;
533 case TOKEN_SHR:
534 e = scalarvect(((uint64_t)reloc_value(e)) >>
535 reloc_value(f));
536 break;
539 return e;
542 static expr *expr4(int critical)
544 expr *e, *f;
546 e = expr5(critical);
547 if (!e)
548 return NULL;
549 while (i == '+' || i == '-') {
550 int j = i;
551 i = scan(scpriv, tokval);
552 f = expr5(critical);
553 if (!f)
554 return NULL;
555 switch (j) {
556 case '+':
557 e = add_vectors(e, f);
558 break;
559 case '-':
560 e = add_vectors(e, scalar_mult(f, -1L, false));
561 break;
564 return e;
567 static expr *expr5(int critical)
569 expr *e, *f;
571 e = expr6(critical);
572 if (!e)
573 return NULL;
574 while (i == '*' || i == '/' || i == '%' ||
575 i == TOKEN_SDIV || i == TOKEN_SMOD) {
576 int j = i;
577 i = scan(scpriv, tokval);
578 f = expr6(critical);
579 if (!f)
580 return NULL;
581 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
582 !(is_simple(f) || is_just_unknown(f)))) {
583 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
584 " scalar values");
585 return NULL;
587 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
588 nasm_error(ERR_NONFATAL, "division by zero");
589 return NULL;
591 switch (j) {
592 case '*':
593 if (is_simple(e))
594 e = scalar_mult(f, reloc_value(e), true);
595 else if (is_simple(f))
596 e = scalar_mult(e, reloc_value(f), true);
597 else if (is_just_unknown(e) && is_just_unknown(f))
598 e = unknown_expr();
599 else {
600 nasm_error(ERR_NONFATAL, "unable to multiply two "
601 "non-scalar objects");
602 return NULL;
604 break;
605 case '/':
606 if (is_just_unknown(e) || is_just_unknown(f))
607 e = unknown_expr();
608 else
609 e = scalarvect(((uint64_t)reloc_value(e)) /
610 ((uint64_t)reloc_value(f)));
611 break;
612 case '%':
613 if (is_just_unknown(e) || is_just_unknown(f))
614 e = unknown_expr();
615 else
616 e = scalarvect(((uint64_t)reloc_value(e)) %
617 ((uint64_t)reloc_value(f)));
618 break;
619 case TOKEN_SDIV:
620 if (is_just_unknown(e) || is_just_unknown(f))
621 e = unknown_expr();
622 else
623 e = scalarvect(((int64_t)reloc_value(e)) /
624 ((int64_t)reloc_value(f)));
625 break;
626 case TOKEN_SMOD:
627 if (is_just_unknown(e) || is_just_unknown(f))
628 e = unknown_expr();
629 else
630 e = scalarvect(((int64_t)reloc_value(e)) %
631 ((int64_t)reloc_value(f)));
632 break;
635 return e;
638 static expr *eval_floatize(enum floatize type)
640 uint8_t result[16], *p; /* Up to 128 bits */
641 static const struct {
642 int bytes, start, len;
643 } formats[] = {
644 { 1, 0, 1 }, /* FLOAT_8 */
645 { 2, 0, 2 }, /* FLOAT_16 */
646 { 4, 0, 4 }, /* FLOAT_32 */
647 { 8, 0, 8 }, /* FLOAT_64 */
648 { 10, 0, 8 }, /* FLOAT_80M */
649 { 10, 8, 2 }, /* FLOAT_80E */
650 { 16, 0, 8 }, /* FLOAT_128L */
651 { 16, 8, 8 }, /* FLOAT_128H */
653 int sign = 1;
654 int64_t val;
655 int j;
657 i = scan(scpriv, tokval);
658 if (i != '(') {
659 nasm_error(ERR_NONFATAL, "expecting `('");
660 return NULL;
662 i = scan(scpriv, tokval);
663 if (i == '-' || i == '+') {
664 sign = (i == '-') ? -1 : 1;
665 i = scan(scpriv, tokval);
667 if (i != TOKEN_FLOAT) {
668 nasm_error(ERR_NONFATAL, "expecting floating-point number");
669 return NULL;
671 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
672 return NULL;
673 i = scan(scpriv, tokval);
674 if (i != ')') {
675 nasm_error(ERR_NONFATAL, "expecting `)'");
676 return NULL;
679 p = result+formats[type].start+formats[type].len;
680 val = 0;
681 for (j = formats[type].len; j; j--) {
682 p--;
683 val = (val << 8) + *p;
686 begintemp();
687 addtotemp(EXPR_SIMPLE, val);
689 i = scan(scpriv, tokval);
690 return finishtemp();
693 static expr *eval_strfunc(enum strfunc type)
695 char *string;
696 size_t string_len;
697 int64_t val;
698 bool parens, rn_warn;
700 parens = false;
701 i = scan(scpriv, tokval);
702 if (i == '(') {
703 parens = true;
704 i = scan(scpriv, tokval);
706 if (i != TOKEN_STR) {
707 nasm_error(ERR_NONFATAL, "expecting string");
708 return NULL;
710 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
711 &string, type);
712 if (string_len == (size_t)-1) {
713 nasm_error(ERR_NONFATAL, "invalid string for transform");
714 return NULL;
717 val = readstrnum(string, string_len, &rn_warn);
718 if (parens) {
719 i = scan(scpriv, tokval);
720 if (i != ')') {
721 nasm_error(ERR_NONFATAL, "expecting `)'");
722 return NULL;
726 if (rn_warn)
727 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
729 begintemp();
730 addtotemp(EXPR_SIMPLE, val);
732 i = scan(scpriv, tokval);
733 return finishtemp();
736 static int64_t eval_ifunc(int64_t val, enum ifunc func)
738 int errtype;
739 uint64_t uval = (uint64_t)val;
740 int64_t rv;
742 switch (func) {
743 case IFUNC_ILOG2E:
744 case IFUNC_ILOG2W:
745 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
747 if (!is_power2(uval))
748 nasm_error(errtype, "ilog2 argument is not a power of two");
749 /* fall through */
750 case IFUNC_ILOG2F:
751 rv = ilog2_64(uval);
752 break;
754 case IFUNC_ILOG2C:
755 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
756 break;
758 default:
759 nasm_panic(0, "invalid IFUNC token %d", func);
760 rv = 0;
761 break;
764 return rv;
767 static expr *expr6(int critical)
769 int32_t type;
770 expr *e;
771 int32_t label_seg;
772 int64_t label_ofs;
773 int64_t tmpval;
774 bool rn_warn;
775 char *scope;
777 switch (i) {
778 case '-':
779 i = scan(scpriv, tokval);
780 e = expr6(critical);
781 if (!e)
782 return NULL;
783 return scalar_mult(e, -1L, false);
785 case '+':
786 i = scan(scpriv, tokval);
787 return expr6(critical);
789 case '~':
790 i = scan(scpriv, tokval);
791 e = expr6(critical);
792 if (!e)
793 return NULL;
794 if (is_just_unknown(e))
795 return unknown_expr();
796 else if (!is_simple(e)) {
797 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
798 " scalar values");
799 return NULL;
801 return scalarvect(~reloc_value(e));
803 case '!':
804 i = scan(scpriv, tokval);
805 e = expr6(critical);
806 if (!e)
807 return NULL;
808 if (is_just_unknown(e))
809 return unknown_expr();
810 else if (!is_simple(e)) {
811 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
812 " scalar values");
813 return NULL;
815 return scalarvect(!reloc_value(e));
817 case TOKEN_IFUNC:
819 enum ifunc func = tokval->t_integer;
820 i = scan(scpriv, tokval);
821 e = expr6(critical);
822 if (!e)
823 return NULL;
824 if (is_just_unknown(e))
825 return unknown_expr();
826 else if (!is_simple(e)) {
827 nasm_error(ERR_NONFATAL, "function may only be applied to"
828 " scalar values");
829 return NULL;
831 return scalarvect(eval_ifunc(reloc_value(e), func));
834 case TOKEN_SEG:
835 i = scan(scpriv, tokval);
836 e = expr6(critical);
837 if (!e)
838 return NULL;
839 e = segment_part(e);
840 if (!e)
841 return NULL;
842 if (is_unknown(e) && critical) {
843 nasm_error(ERR_NONFATAL, "unable to determine segment base");
844 return NULL;
846 return e;
848 case TOKEN_FLOATIZE:
849 return eval_floatize(tokval->t_integer);
851 case TOKEN_STRFUNC:
852 return eval_strfunc(tokval->t_integer);
854 case '(':
855 i = scan(scpriv, tokval);
856 e = bexpr(critical);
857 if (!e)
858 return NULL;
859 if (i != ')') {
860 nasm_error(ERR_NONFATAL, "expecting `)'");
861 return NULL;
863 i = scan(scpriv, tokval);
864 return e;
866 case TOKEN_NUM:
867 case TOKEN_STR:
868 case TOKEN_REG:
869 case TOKEN_ID:
870 case TOKEN_INSN: /* Opcodes that occur here are really labels */
871 case TOKEN_HERE:
872 case TOKEN_BASE:
873 case TOKEN_DECORATOR:
874 begintemp();
875 switch (i) {
876 case TOKEN_NUM:
877 addtotemp(EXPR_SIMPLE, tokval->t_integer);
878 break;
879 case TOKEN_STR:
880 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
881 if (rn_warn)
882 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
883 addtotemp(EXPR_SIMPLE, tmpval);
884 break;
885 case TOKEN_REG:
886 addtotemp(tokval->t_integer, 1L);
887 if (hint && hint->type == EAH_NOHINT)
888 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
889 break;
890 case TOKEN_ID:
891 case TOKEN_INSN:
892 case TOKEN_HERE:
893 case TOKEN_BASE:
895 * If !location->known, this indicates that no
896 * symbol, Here or Base references are valid because we
897 * are in preprocess-only mode.
899 if (!location->known) {
900 nasm_error(ERR_NONFATAL,
901 "%s not supported in preprocess-only mode",
902 (i == TOKEN_HERE ? "`$'" :
903 i == TOKEN_BASE ? "`$$'" :
904 "symbol references"));
905 addtotemp(EXPR_UNKNOWN, 1L);
906 break;
909 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
910 if (i == TOKEN_BASE) {
911 label_seg = in_abs_seg ? abs_seg : location->segment;
912 label_ofs = 0;
913 } else if (i == TOKEN_HERE) {
914 label_seg = in_abs_seg ? abs_seg : location->segment;
915 label_ofs = in_abs_seg ? abs_offset : location->offset;
916 } else {
917 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
918 scope = local_scope(tokval->t_charptr);
919 if (critical == 2) {
920 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
921 scope,tokval->t_charptr);
922 return NULL;
923 } else if (critical == 1) {
924 nasm_error(ERR_NONFATAL,
925 "symbol `%s%s' not defined before use",
926 scope,tokval->t_charptr);
927 return NULL;
928 } else {
929 if (opflags)
930 *opflags |= OPFLAG_FORWARD;
931 type = EXPR_UNKNOWN;
932 label_seg = NO_SEG;
933 label_ofs = 1;
936 if (opflags && is_extern(tokval->t_charptr))
937 *opflags |= OPFLAG_EXTERN;
939 addtotemp(type, label_ofs);
940 if (label_seg != NO_SEG)
941 addtotemp(EXPR_SEGBASE + label_seg, 1L);
942 break;
943 case TOKEN_DECORATOR:
944 addtotemp(EXPR_RDSAE, tokval->t_integer);
945 break;
947 i = scan(scpriv, tokval);
948 return finishtemp();
950 default:
951 nasm_error(ERR_NONFATAL, "expression syntax error");
952 return NULL;
956 void eval_global_info(struct ofmt *output, lfunc lookup_label,
957 struct location * locp)
959 outfmt = output;
960 labelfunc = lookup_label;
961 location = locp;
964 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
965 int *fwref, int critical, struct eval_hints *hints)
967 expr *e;
968 expr *f = NULL;
970 hint = hints;
971 if (hint)
972 hint->type = EAH_NOHINT;
974 if (critical & CRITICAL) {
975 critical &= ~CRITICAL;
976 bexpr = rexp0;
977 } else
978 bexpr = expr0;
980 scan = sc;
981 scpriv = scprivate;
982 tokval = tv;
983 opflags = fwref;
985 if (tokval->t_type == TOKEN_INVALID)
986 i = scan(scpriv, tokval);
987 else
988 i = tokval->t_type;
990 while (ntempexprs) /* initialize temporary storage */
991 nasm_free(tempexprs[--ntempexprs]);
993 e = bexpr(critical);
994 if (!e)
995 return NULL;
997 if (i == TOKEN_WRT) {
998 i = scan(scpriv, tokval); /* eat the WRT */
999 f = expr6(critical);
1000 if (!f)
1001 return NULL;
1003 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
1004 if (f) {
1005 expr *g;
1006 if (is_just_unknown(f))
1007 g = unknown_expr();
1008 else {
1009 int64_t value;
1010 begintemp();
1011 if (!is_reloc(f)) {
1012 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1013 return NULL;
1015 value = reloc_seg(f);
1016 if (value == NO_SEG)
1017 value = reloc_value(f) | SEG_ABS;
1018 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
1019 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1020 return NULL;
1022 addtotemp(EXPR_WRT, value);
1023 g = finishtemp();
1025 e = add_vectors(e, g);
1027 return e;