Add missing static declarations in rdoff/ldrdf.c
[nasm.git] / eval.c
blob403a793e577efcb3606401bb90660037fd407fbd
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 efunc error; /* Address of error reporting routine */
58 static lfunc labelfunc; /* Address of label routine */
60 static struct ofmt *outfmt; /* Structure of addresses of output routines */
62 static expr **tempexprs = NULL;
63 static int ntempexprs;
64 static int tempexprs_size = 0;
66 static expr *tempexpr;
67 static int ntempexpr;
68 static int tempexpr_size;
70 static struct tokenval *tokval; /* The current token */
71 static int i; /* The t_type of tokval */
73 static void *scpriv;
74 static struct location *location; /* Pointer to current line's segment,offset */
75 static int *opflags;
77 static struct eval_hints *hint;
79 extern int in_abs_seg; /* ABSOLUTE segment flag */
80 extern int32_t abs_seg; /* ABSOLUTE segment */
81 extern int32_t abs_offset; /* ABSOLUTE segment offset */
84 * Unimportant cleanup is done to avoid confusing people who are trying
85 * to debug real memory leaks
87 void eval_cleanup(void)
89 while (ntempexprs)
90 nasm_free(tempexprs[--ntempexprs]);
91 nasm_free(tempexprs);
95 * Construct a temporary expression.
97 static void begintemp(void)
99 tempexpr = NULL;
100 tempexpr_size = ntempexpr = 0;
103 static void addtotemp(int32_t type, int64_t value)
105 while (ntempexpr >= tempexpr_size) {
106 tempexpr_size += TEMPEXPR_DELTA;
107 tempexpr = nasm_realloc(tempexpr,
108 tempexpr_size * sizeof(*tempexpr));
110 tempexpr[ntempexpr].type = type;
111 tempexpr[ntempexpr++].value = value;
114 static expr *finishtemp(void)
116 addtotemp(0L, 0L); /* terminate */
117 while (ntempexprs >= tempexprs_size) {
118 tempexprs_size += TEMPEXPRS_DELTA;
119 tempexprs = nasm_realloc(tempexprs,
120 tempexprs_size * sizeof(*tempexprs));
122 return tempexprs[ntempexprs++] = tempexpr;
126 * Add two vector datatypes. We have some bizarre behaviour on far-
127 * absolute segment types: we preserve them during addition _only_
128 * if one of the segments is a truly pure scalar.
130 static expr *add_vectors(expr * p, expr * q)
132 int preserve;
134 preserve = is_really_simple(p) || is_really_simple(q);
136 begintemp();
138 while (p->type && q->type &&
139 p->type < EXPR_SEGBASE + SEG_ABS &&
140 q->type < EXPR_SEGBASE + SEG_ABS) {
141 int lasttype;
143 if (p->type > q->type) {
144 addtotemp(q->type, q->value);
145 lasttype = q++->type;
146 } else if (p->type < q->type) {
147 addtotemp(p->type, p->value);
148 lasttype = p++->type;
149 } else { /* *p and *q have same type */
150 int64_t sum = p->value + q->value;
151 if (sum) {
152 addtotemp(p->type, sum);
153 if (hint)
154 hint->type = EAH_SUMMED;
156 lasttype = p->type;
157 p++, q++;
159 if (lasttype == EXPR_UNKNOWN) {
160 return finishtemp();
163 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
164 addtotemp(p->type, p->value);
165 p++;
167 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
168 addtotemp(q->type, q->value);
169 q++;
172 return finishtemp();
176 * Multiply a vector by a scalar. Strip far-absolute segment part
177 * if present.
179 * Explicit treatment of UNKNOWN is not required in this routine,
180 * since it will silently do the Right Thing anyway.
182 * If `affect_hints' is set, we also change the hint type to
183 * NOTBASE if a MAKEBASE hint points at a register being
184 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
185 * as the base register.
187 static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
189 expr *p = vect;
191 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
192 p->value = scalar * (p->value);
193 if (hint && hint->type == EAH_MAKEBASE &&
194 p->type == hint->base && affect_hints)
195 hint->type = EAH_NOTBASE;
196 p++;
198 p->type = 0;
200 return vect;
203 static expr *scalarvect(int64_t scalar)
205 begintemp();
206 addtotemp(EXPR_SIMPLE, scalar);
207 return finishtemp();
210 static expr *unknown_expr(void)
212 begintemp();
213 addtotemp(EXPR_UNKNOWN, 1L);
214 return finishtemp();
218 * The SEG operator: calculate the segment part of a relocatable
219 * value. Return NULL, as usual, if an error occurs. Report the
220 * error too.
222 static expr *segment_part(expr * e)
224 int32_t seg;
226 if (is_unknown(e))
227 return unknown_expr();
229 if (!is_reloc(e)) {
230 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
231 return NULL;
234 seg = reloc_seg(e);
235 if (seg == NO_SEG) {
236 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
237 return NULL;
238 } else if (seg & SEG_ABS) {
239 return scalarvect(seg & ~SEG_ABS);
240 } else if (seg & 1) {
241 error(ERR_NONFATAL, "SEG applied to something which"
242 " is already a segment base");
243 return NULL;
244 } else {
245 int32_t base = outfmt->segbase(seg + 1);
247 begintemp();
248 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
249 1L);
250 return finishtemp();
255 * Recursive-descent parser. Called with a single boolean operand,
256 * which is true if the evaluation is critical (i.e. unresolved
257 * symbols are an error condition). Must update the global `i' to
258 * reflect the token after the parsed string. May return NULL.
260 * evaluate() should report its own errors: on return it is assumed
261 * that if NULL has been returned, the error has already been
262 * reported.
266 * Grammar parsed is:
268 * expr : bexpr [ WRT expr6 ]
269 * bexpr : rexp0 or expr0 depending on relative-mode setting
270 * rexp0 : rexp1 [ {||} rexp1...]
271 * rexp1 : rexp2 [ {^^} rexp2...]
272 * rexp2 : rexp3 [ {&&} rexp3...]
273 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
274 * expr0 : expr1 [ {|} expr1...]
275 * expr1 : expr2 [ {^} expr2...]
276 * expr2 : expr3 [ {&} expr3...]
277 * expr3 : expr4 [ {<<,>>} expr4...]
278 * expr4 : expr5 [ {+,-} expr5...]
279 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
280 * expr6 : { ~,+,-,IFUNC,SEG } expr6
281 * | (bexpr)
282 * | symbol
283 * | $
284 * | number
287 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
289 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
290 static expr *expr4(int), *expr5(int), *expr6(int);
292 static expr *(*bexpr) (int);
294 static expr *rexp0(int critical)
296 expr *e, *f;
298 e = rexp1(critical);
299 if (!e)
300 return NULL;
302 while (i == TOKEN_DBL_OR) {
303 i = scan(scpriv, tokval);
304 f = rexp1(critical);
305 if (!f)
306 return NULL;
307 if (!(is_simple(e) || is_just_unknown(e)) ||
308 !(is_simple(f) || is_just_unknown(f))) {
309 error(ERR_NONFATAL, "`|' operator may only be applied to"
310 " scalar values");
313 if (is_just_unknown(e) || is_just_unknown(f))
314 e = unknown_expr();
315 else
316 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
318 return e;
321 static expr *rexp1(int critical)
323 expr *e, *f;
325 e = rexp2(critical);
326 if (!e)
327 return NULL;
329 while (i == TOKEN_DBL_XOR) {
330 i = scan(scpriv, tokval);
331 f = rexp2(critical);
332 if (!f)
333 return NULL;
334 if (!(is_simple(e) || is_just_unknown(e)) ||
335 !(is_simple(f) || is_just_unknown(f))) {
336 error(ERR_NONFATAL, "`^' operator may only be applied to"
337 " scalar values");
340 if (is_just_unknown(e) || is_just_unknown(f))
341 e = unknown_expr();
342 else
343 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
345 return e;
348 static expr *rexp2(int critical)
350 expr *e, *f;
352 e = rexp3(critical);
353 if (!e)
354 return NULL;
355 while (i == TOKEN_DBL_AND) {
356 i = scan(scpriv, tokval);
357 f = rexp3(critical);
358 if (!f)
359 return NULL;
360 if (!(is_simple(e) || is_just_unknown(e)) ||
361 !(is_simple(f) || is_just_unknown(f))) {
362 error(ERR_NONFATAL, "`&' operator may only be applied to"
363 " scalar values");
365 if (is_just_unknown(e) || is_just_unknown(f))
366 e = unknown_expr();
367 else
368 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
370 return e;
373 static expr *rexp3(int critical)
375 expr *e, *f;
376 int64_t v;
378 e = expr0(critical);
379 if (!e)
380 return NULL;
382 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
383 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
384 int j = i;
385 i = scan(scpriv, tokval);
386 f = expr0(critical);
387 if (!f)
388 return NULL;
390 e = add_vectors(e, scalar_mult(f, -1L, false));
392 switch (j) {
393 case TOKEN_EQ:
394 case TOKEN_NE:
395 if (is_unknown(e))
396 v = -1; /* means unknown */
397 else if (!is_really_simple(e) || reloc_value(e) != 0)
398 v = (j == TOKEN_NE); /* unequal, so return true if NE */
399 else
400 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
401 break;
402 default:
403 if (is_unknown(e))
404 v = -1; /* means unknown */
405 else if (!is_really_simple(e)) {
406 error(ERR_NONFATAL,
407 "`%s': operands differ by a non-scalar",
408 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
409 TOKEN_GE ? ">=" : ">"));
410 v = 0; /* must set it to _something_ */
411 } else {
412 int64_t vv = reloc_value(e);
413 if (vv == 0)
414 v = (j == TOKEN_LE || j == TOKEN_GE);
415 else if (vv > 0)
416 v = (j == TOKEN_GE || j == TOKEN_GT);
417 else /* vv < 0 */
418 v = (j == TOKEN_LE || j == TOKEN_LT);
420 break;
423 if (v == -1)
424 e = unknown_expr();
425 else
426 e = scalarvect(v);
428 return e;
431 static expr *expr0(int critical)
433 expr *e, *f;
435 e = expr1(critical);
436 if (!e)
437 return NULL;
439 while (i == '|') {
440 i = scan(scpriv, tokval);
441 f = expr1(critical);
442 if (!f)
443 return NULL;
444 if (!(is_simple(e) || is_just_unknown(e)) ||
445 !(is_simple(f) || is_just_unknown(f))) {
446 error(ERR_NONFATAL, "`|' operator may only be applied to"
447 " scalar values");
449 if (is_just_unknown(e) || is_just_unknown(f))
450 e = unknown_expr();
451 else
452 e = scalarvect(reloc_value(e) | reloc_value(f));
454 return e;
457 static expr *expr1(int critical)
459 expr *e, *f;
461 e = expr2(critical);
462 if (!e)
463 return NULL;
465 while (i == '^') {
466 i = scan(scpriv, tokval);
467 f = expr2(critical);
468 if (!f)
469 return NULL;
470 if (!(is_simple(e) || is_just_unknown(e)) ||
471 !(is_simple(f) || is_just_unknown(f))) {
472 error(ERR_NONFATAL, "`^' operator may only be applied to"
473 " scalar values");
475 if (is_just_unknown(e) || is_just_unknown(f))
476 e = unknown_expr();
477 else
478 e = scalarvect(reloc_value(e) ^ reloc_value(f));
480 return e;
483 static expr *expr2(int critical)
485 expr *e, *f;
487 e = expr3(critical);
488 if (!e)
489 return NULL;
491 while (i == '&') {
492 i = scan(scpriv, tokval);
493 f = expr3(critical);
494 if (!f)
495 return NULL;
496 if (!(is_simple(e) || is_just_unknown(e)) ||
497 !(is_simple(f) || is_just_unknown(f))) {
498 error(ERR_NONFATAL, "`&' operator may only be applied to"
499 " scalar values");
501 if (is_just_unknown(e) || is_just_unknown(f))
502 e = unknown_expr();
503 else
504 e = scalarvect(reloc_value(e) & reloc_value(f));
506 return e;
509 static expr *expr3(int critical)
511 expr *e, *f;
513 e = expr4(critical);
514 if (!e)
515 return NULL;
517 while (i == TOKEN_SHL || i == TOKEN_SHR) {
518 int j = i;
519 i = scan(scpriv, tokval);
520 f = expr4(critical);
521 if (!f)
522 return NULL;
523 if (!(is_simple(e) || is_just_unknown(e)) ||
524 !(is_simple(f) || is_just_unknown(f))) {
525 error(ERR_NONFATAL, "shift operator may only be applied to"
526 " scalar values");
527 } else if (is_just_unknown(e) || is_just_unknown(f)) {
528 e = unknown_expr();
529 } else
530 switch (j) {
531 case TOKEN_SHL:
532 e = scalarvect(reloc_value(e) << reloc_value(f));
533 break;
534 case TOKEN_SHR:
535 e = scalarvect(((uint64_t)reloc_value(e)) >>
536 reloc_value(f));
537 break;
540 return e;
543 static expr *expr4(int critical)
545 expr *e, *f;
547 e = expr5(critical);
548 if (!e)
549 return NULL;
550 while (i == '+' || i == '-') {
551 int j = i;
552 i = scan(scpriv, tokval);
553 f = expr5(critical);
554 if (!f)
555 return NULL;
556 switch (j) {
557 case '+':
558 e = add_vectors(e, f);
559 break;
560 case '-':
561 e = add_vectors(e, scalar_mult(f, -1L, false));
562 break;
565 return e;
568 static expr *expr5(int critical)
570 expr *e, *f;
572 e = expr6(critical);
573 if (!e)
574 return NULL;
575 while (i == '*' || i == '/' || i == '%' ||
576 i == TOKEN_SDIV || i == TOKEN_SMOD) {
577 int j = i;
578 i = scan(scpriv, tokval);
579 f = expr6(critical);
580 if (!f)
581 return NULL;
582 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
583 !(is_simple(f) || is_just_unknown(f)))) {
584 error(ERR_NONFATAL, "division operator may only be applied to"
585 " scalar values");
586 return NULL;
588 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
589 error(ERR_NONFATAL, "division by zero");
590 return NULL;
592 switch (j) {
593 case '*':
594 if (is_simple(e))
595 e = scalar_mult(f, reloc_value(e), true);
596 else if (is_simple(f))
597 e = scalar_mult(e, reloc_value(f), true);
598 else if (is_just_unknown(e) && is_just_unknown(f))
599 e = unknown_expr();
600 else {
601 error(ERR_NONFATAL, "unable to multiply two "
602 "non-scalar objects");
603 return NULL;
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 '%':
614 if (is_just_unknown(e) || is_just_unknown(f))
615 e = unknown_expr();
616 else
617 e = scalarvect(((uint64_t)reloc_value(e)) %
618 ((uint64_t)reloc_value(f)));
619 break;
620 case TOKEN_SDIV:
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;
627 case TOKEN_SMOD:
628 if (is_just_unknown(e) || is_just_unknown(f))
629 e = unknown_expr();
630 else
631 e = scalarvect(((int64_t)reloc_value(e)) %
632 ((int64_t)reloc_value(f)));
633 break;
636 return e;
639 static expr *eval_floatize(enum floatize type)
641 uint8_t result[16], *p; /* Up to 128 bits */
642 static const struct {
643 int bytes, start, len;
644 } formats[] = {
645 { 1, 0, 1 }, /* FLOAT_8 */
646 { 2, 0, 2 }, /* FLOAT_16 */
647 { 4, 0, 4 }, /* FLOAT_32 */
648 { 8, 0, 8 }, /* FLOAT_64 */
649 { 10, 0, 8 }, /* FLOAT_80M */
650 { 10, 8, 2 }, /* FLOAT_80E */
651 { 16, 0, 8 }, /* FLOAT_128L */
652 { 16, 8, 8 }, /* FLOAT_128H */
654 int sign = 1;
655 int64_t val;
656 int j;
658 i = scan(scpriv, tokval);
659 if (i != '(') {
660 error(ERR_NONFATAL, "expecting `('");
661 return NULL;
663 i = scan(scpriv, tokval);
664 if (i == '-' || i == '+') {
665 sign = (i == '-') ? -1 : 1;
666 i = scan(scpriv, tokval);
668 if (i != TOKEN_FLOAT) {
669 error(ERR_NONFATAL, "expecting floating-point number");
670 return NULL;
672 if (!float_const(tokval->t_charptr, sign, result,
673 formats[type].bytes, error))
674 return NULL;
675 i = scan(scpriv, tokval);
676 if (i != ')') {
677 error(ERR_NONFATAL, "expecting `)'");
678 return NULL;
681 p = result+formats[type].start+formats[type].len;
682 val = 0;
683 for (j = formats[type].len; j; j--) {
684 p--;
685 val = (val << 8) + *p;
688 begintemp();
689 addtotemp(EXPR_SIMPLE, val);
691 i = scan(scpriv, tokval);
692 return finishtemp();
695 static expr *eval_strfunc(enum strfunc type)
697 char *string;
698 size_t string_len;
699 int64_t val;
700 bool parens, rn_warn;
702 parens = false;
703 i = scan(scpriv, tokval);
704 if (i == '(') {
705 parens = true;
706 i = scan(scpriv, tokval);
708 if (i != TOKEN_STR) {
709 error(ERR_NONFATAL, "expecting string");
710 return NULL;
712 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
713 &string, type);
714 if (string_len == (size_t)-1) {
715 error(ERR_NONFATAL, "invalid string for transform");
716 return NULL;
719 val = readstrnum(string, string_len, &rn_warn);
720 if (parens) {
721 i = scan(scpriv, tokval);
722 if (i != ')') {
723 error(ERR_NONFATAL, "expecting `)'");
724 return NULL;
728 if (rn_warn)
729 error(ERR_WARNING|ERR_PASS1, "character constant too long");
731 begintemp();
732 addtotemp(EXPR_SIMPLE, val);
734 i = scan(scpriv, tokval);
735 return finishtemp();
738 static int64_t eval_ifunc(int64_t val, enum ifunc func)
740 int errtype;
741 uint64_t uval = (uint64_t)val;
742 int64_t rv;
744 switch (func) {
745 case IFUNC_ILOG2E:
746 case IFUNC_ILOG2W:
747 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
749 if (!is_power2(uval))
750 error(errtype, "ilog2 argument is not a power of two");
751 /* fall through */
752 case IFUNC_ILOG2F:
753 rv = ilog2_64(uval);
754 break;
756 case IFUNC_ILOG2C:
757 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
758 break;
760 default:
761 error(ERR_PANIC, "invalid IFUNC token %d", func);
762 rv = 0;
763 break;
766 return rv;
769 static expr *expr6(int critical)
771 int32_t type;
772 expr *e;
773 int32_t label_seg;
774 int64_t label_ofs;
775 int64_t tmpval;
776 bool rn_warn;
777 char *scope;
779 switch (i) {
780 case '-':
781 i = scan(scpriv, tokval);
782 e = expr6(critical);
783 if (!e)
784 return NULL;
785 return scalar_mult(e, -1L, false);
787 case '+':
788 i = scan(scpriv, tokval);
789 return expr6(critical);
791 case '~':
792 i = scan(scpriv, tokval);
793 e = expr6(critical);
794 if (!e)
795 return NULL;
796 if (is_just_unknown(e))
797 return unknown_expr();
798 else if (!is_simple(e)) {
799 error(ERR_NONFATAL, "`~' operator may only be applied to"
800 " scalar values");
801 return NULL;
803 return scalarvect(~reloc_value(e));
805 case '!':
806 i = scan(scpriv, tokval);
807 e = expr6(critical);
808 if (!e)
809 return NULL;
810 if (is_just_unknown(e))
811 return unknown_expr();
812 else if (!is_simple(e)) {
813 error(ERR_NONFATAL, "`!' operator may only be applied to"
814 " scalar values");
815 return NULL;
817 return scalarvect(!reloc_value(e));
819 case TOKEN_IFUNC:
821 enum ifunc func = tokval->t_integer;
822 i = scan(scpriv, tokval);
823 e = expr6(critical);
824 if (!e)
825 return NULL;
826 if (is_just_unknown(e))
827 return unknown_expr();
828 else if (!is_simple(e)) {
829 error(ERR_NONFATAL, "function may only be applied to"
830 " scalar values");
831 return NULL;
833 return scalarvect(eval_ifunc(reloc_value(e), func));
836 case TOKEN_SEG:
837 i = scan(scpriv, tokval);
838 e = expr6(critical);
839 if (!e)
840 return NULL;
841 e = segment_part(e);
842 if (!e)
843 return NULL;
844 if (is_unknown(e) && critical) {
845 error(ERR_NONFATAL, "unable to determine segment base");
846 return NULL;
848 return e;
850 case TOKEN_FLOATIZE:
851 return eval_floatize(tokval->t_integer);
853 case TOKEN_STRFUNC:
854 return eval_strfunc(tokval->t_integer);
856 case '(':
857 i = scan(scpriv, tokval);
858 e = bexpr(critical);
859 if (!e)
860 return NULL;
861 if (i != ')') {
862 error(ERR_NONFATAL, "expecting `)'");
863 return NULL;
865 i = scan(scpriv, tokval);
866 return e;
868 case TOKEN_NUM:
869 case TOKEN_STR:
870 case TOKEN_REG:
871 case TOKEN_ID:
872 case TOKEN_INSN: /* Opcodes that occur here are really labels */
873 case TOKEN_HERE:
874 case TOKEN_BASE:
875 case TOKEN_DECORATOR:
876 begintemp();
877 switch (i) {
878 case TOKEN_NUM:
879 addtotemp(EXPR_SIMPLE, tokval->t_integer);
880 break;
881 case TOKEN_STR:
882 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
883 if (rn_warn)
884 error(ERR_WARNING|ERR_PASS1, "character constant too long");
885 addtotemp(EXPR_SIMPLE, tmpval);
886 break;
887 case TOKEN_REG:
888 addtotemp(tokval->t_integer, 1L);
889 if (hint && hint->type == EAH_NOHINT)
890 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
891 break;
892 case TOKEN_ID:
893 case TOKEN_INSN:
894 case TOKEN_HERE:
895 case TOKEN_BASE:
897 * If !location->known, this indicates that no
898 * symbol, Here or Base references are valid because we
899 * are in preprocess-only mode.
901 if (!location->known) {
902 error(ERR_NONFATAL,
903 "%s not supported in preprocess-only mode",
904 (i == TOKEN_HERE ? "`$'" :
905 i == TOKEN_BASE ? "`$$'" :
906 "symbol references"));
907 addtotemp(EXPR_UNKNOWN, 1L);
908 break;
911 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
912 if (i == TOKEN_BASE) {
913 label_seg = in_abs_seg ? abs_seg : location->segment;
914 label_ofs = 0;
915 } else if (i == TOKEN_HERE) {
916 label_seg = in_abs_seg ? abs_seg : location->segment;
917 label_ofs = in_abs_seg ? abs_offset : location->offset;
918 } else {
919 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
920 scope = local_scope(tokval->t_charptr);
921 if (critical == 2) {
922 error(ERR_NONFATAL, "symbol `%s%s' undefined",
923 scope,tokval->t_charptr);
924 return NULL;
925 } else if (critical == 1) {
926 error(ERR_NONFATAL,
927 "symbol `%s%s' not defined before use",
928 scope,tokval->t_charptr);
929 return NULL;
930 } else {
931 if (opflags)
932 *opflags |= OPFLAG_FORWARD;
933 type = EXPR_UNKNOWN;
934 label_seg = NO_SEG;
935 label_ofs = 1;
938 if (opflags && is_extern(tokval->t_charptr))
939 *opflags |= OPFLAG_EXTERN;
941 addtotemp(type, label_ofs);
942 if (label_seg != NO_SEG)
943 addtotemp(EXPR_SEGBASE + label_seg, 1L);
944 break;
945 case TOKEN_DECORATOR:
946 addtotemp(EXPR_RDSAE, tokval->t_integer);
947 break;
949 i = scan(scpriv, tokval);
950 return finishtemp();
952 default:
953 error(ERR_NONFATAL, "expression syntax error");
954 return NULL;
958 void eval_global_info(struct ofmt *output, lfunc lookup_label,
959 struct location * locp)
961 outfmt = output;
962 labelfunc = lookup_label;
963 location = locp;
966 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
967 int *fwref, int critical, efunc report_error,
968 struct eval_hints *hints)
970 expr *e;
971 expr *f = NULL;
973 hint = hints;
974 if (hint)
975 hint->type = EAH_NOHINT;
977 if (critical & CRITICAL) {
978 critical &= ~CRITICAL;
979 bexpr = rexp0;
980 } else
981 bexpr = expr0;
983 scan = sc;
984 scpriv = scprivate;
985 tokval = tv;
986 error = report_error;
987 opflags = fwref;
989 if (tokval->t_type == TOKEN_INVALID)
990 i = scan(scpriv, tokval);
991 else
992 i = tokval->t_type;
994 while (ntempexprs) /* initialize temporary storage */
995 nasm_free(tempexprs[--ntempexprs]);
997 e = bexpr(critical);
998 if (!e)
999 return NULL;
1001 if (i == TOKEN_WRT) {
1002 i = scan(scpriv, tokval); /* eat the WRT */
1003 f = expr6(critical);
1004 if (!f)
1005 return NULL;
1007 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
1008 if (f) {
1009 expr *g;
1010 if (is_just_unknown(f))
1011 g = unknown_expr();
1012 else {
1013 int64_t value;
1014 begintemp();
1015 if (!is_reloc(f)) {
1016 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1017 return NULL;
1019 value = reloc_seg(f);
1020 if (value == NO_SEG)
1021 value = reloc_value(f) | SEG_ABS;
1022 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
1023 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
1024 return NULL;
1026 addtotemp(EXPR_WRT, value);
1027 g = finishtemp();
1029 e = add_vectors(e, g);
1031 return e;