1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
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
54 #define TEMPEXPRS_DELTA 128
55 #define TEMPEXPR_DELTA 8
57 static scanner scan
; /* Address of scanner routine */
59 static expr
**tempexprs
= NULL
;
60 static int ntempexprs
;
61 static int tempexprs_size
= 0;
63 static expr
*tempexpr
;
65 static int tempexpr_size
;
67 static struct tokenval
*tokval
; /* The current token */
68 static int i
; /* The t_type of tokval */
73 static struct eval_hints
*hint
;
77 * Unimportant cleanup is done to avoid confusing people who are trying
78 * to debug real memory leaks
80 void eval_cleanup(void)
83 nasm_free(tempexprs
[--ntempexprs
]);
88 * Construct a temporary expression.
90 static void begintemp(void)
93 tempexpr_size
= ntempexpr
= 0;
96 static void addtotemp(int32_t type
, int64_t value
)
98 while (ntempexpr
>= tempexpr_size
) {
99 tempexpr_size
+= TEMPEXPR_DELTA
;
100 tempexpr
= nasm_realloc(tempexpr
,
101 tempexpr_size
* sizeof(*tempexpr
));
103 tempexpr
[ntempexpr
].type
= type
;
104 tempexpr
[ntempexpr
++].value
= value
;
107 static expr
*finishtemp(void)
109 addtotemp(0L, 0L); /* terminate */
110 while (ntempexprs
>= tempexprs_size
) {
111 tempexprs_size
+= TEMPEXPRS_DELTA
;
112 tempexprs
= nasm_realloc(tempexprs
,
113 tempexprs_size
* sizeof(*tempexprs
));
115 return tempexprs
[ntempexprs
++] = tempexpr
;
119 * Add two vector datatypes. We have some bizarre behaviour on far-
120 * absolute segment types: we preserve them during addition _only_
121 * if one of the segments is a truly pure scalar.
123 static expr
*add_vectors(expr
* p
, expr
* q
)
127 preserve
= is_really_simple(p
) || is_really_simple(q
);
131 while (p
->type
&& q
->type
&&
132 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
133 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
136 if (p
->type
> q
->type
) {
137 addtotemp(q
->type
, q
->value
);
138 lasttype
= q
++->type
;
139 } else if (p
->type
< q
->type
) {
140 addtotemp(p
->type
, p
->value
);
141 lasttype
= p
++->type
;
142 } else { /* *p and *q have same type */
143 int64_t sum
= p
->value
+ q
->value
;
145 addtotemp(p
->type
, sum
);
147 hint
->type
= EAH_SUMMED
;
152 if (lasttype
== EXPR_UNKNOWN
) {
156 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
157 addtotemp(p
->type
, p
->value
);
160 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
161 addtotemp(q
->type
, q
->value
);
169 * Multiply a vector by a scalar. Strip far-absolute segment part
172 * Explicit treatment of UNKNOWN is not required in this routine,
173 * since it will silently do the Right Thing anyway.
175 * If `affect_hints' is set, we also change the hint type to
176 * NOTBASE if a MAKEBASE hint points at a register being
177 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
178 * as the base register.
180 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
184 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
185 p
->value
= scalar
* (p
->value
);
186 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
187 p
->type
== hint
->base
&& affect_hints
)
188 hint
->type
= EAH_NOTBASE
;
196 static expr
*scalarvect(int64_t scalar
)
199 addtotemp(EXPR_SIMPLE
, scalar
);
203 static expr
*unknown_expr(void)
206 addtotemp(EXPR_UNKNOWN
, 1L);
211 * The SEG operator: calculate the segment part of a relocatable
212 * value. Return NULL, as usual, if an error occurs. Report the
215 static expr
*segment_part(expr
* e
)
220 return unknown_expr();
223 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
229 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
231 } else if (seg
& SEG_ABS
) {
232 return scalarvect(seg
& ~SEG_ABS
);
233 } else if (seg
& 1) {
234 nasm_error(ERR_NONFATAL
, "SEG applied to something which"
235 " is already a segment base");
238 int32_t base
= ofmt
->segbase(seg
+ 1);
241 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
248 * Recursive-descent parser. Called with a single boolean operand,
249 * which is true if the evaluation is critical (i.e. unresolved
250 * symbols are an error condition). Must update the global `i' to
251 * reflect the token after the parsed string. May return NULL.
253 * evaluate() should report its own errors: on return it is assumed
254 * that if NULL has been returned, the error has already been
261 * expr : bexpr [ WRT expr6 ]
262 * bexpr : rexp0 or expr0 depending on relative-mode setting
263 * rexp0 : rexp1 [ {||} rexp1...]
264 * rexp1 : rexp2 [ {^^} rexp2...]
265 * rexp2 : rexp3 [ {&&} rexp3...]
266 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
267 * expr0 : expr1 [ {|} expr1...]
268 * expr1 : expr2 [ {^} expr2...]
269 * expr2 : expr3 [ {&} expr3...]
270 * expr3 : expr4 [ {<<,>>} expr4...]
271 * expr4 : expr5 [ {+,-} expr5...]
272 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
273 * expr6 : { ~,+,-,IFUNC,SEG } expr6
280 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
282 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
283 static expr
*expr4(int), *expr5(int), *expr6(int);
285 static expr
*(*bexpr
) (int);
287 static expr
*rexp0(int critical
)
295 while (i
== TOKEN_DBL_OR
) {
296 i
= scan(scpriv
, tokval
);
300 if (!(is_simple(e
) || is_just_unknown(e
)) ||
301 !(is_simple(f
) || is_just_unknown(f
))) {
302 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
306 if (is_just_unknown(e
) || is_just_unknown(f
))
309 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
314 static expr
*rexp1(int critical
)
322 while (i
== TOKEN_DBL_XOR
) {
323 i
= scan(scpriv
, tokval
);
327 if (!(is_simple(e
) || is_just_unknown(e
)) ||
328 !(is_simple(f
) || is_just_unknown(f
))) {
329 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
333 if (is_just_unknown(e
) || is_just_unknown(f
))
336 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
341 static expr
*rexp2(int critical
)
348 while (i
== TOKEN_DBL_AND
) {
349 i
= scan(scpriv
, tokval
);
353 if (!(is_simple(e
) || is_just_unknown(e
)) ||
354 !(is_simple(f
) || is_just_unknown(f
))) {
355 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
358 if (is_just_unknown(e
) || is_just_unknown(f
))
361 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
366 static expr
*rexp3(int critical
)
375 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
376 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
378 i
= scan(scpriv
, tokval
);
383 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
389 v
= -1; /* means unknown */
390 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
391 v
= (j
== TOKEN_NE
); /* unequal, so return true if NE */
393 v
= (j
== TOKEN_EQ
); /* equal, so return true if EQ */
397 v
= -1; /* means unknown */
398 else if (!is_really_simple(e
)) {
399 nasm_error(ERR_NONFATAL
,
400 "`%s': operands differ by a non-scalar",
401 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
402 TOKEN_GE
? ">=" : ">"));
403 v
= 0; /* must set it to _something_ */
405 int64_t vv
= reloc_value(e
);
407 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
409 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
411 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
424 static expr
*expr0(int critical
)
433 i
= scan(scpriv
, tokval
);
437 if (!(is_simple(e
) || is_just_unknown(e
)) ||
438 !(is_simple(f
) || is_just_unknown(f
))) {
439 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
442 if (is_just_unknown(e
) || is_just_unknown(f
))
445 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
450 static expr
*expr1(int critical
)
459 i
= scan(scpriv
, tokval
);
463 if (!(is_simple(e
) || is_just_unknown(e
)) ||
464 !(is_simple(f
) || is_just_unknown(f
))) {
465 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
468 if (is_just_unknown(e
) || is_just_unknown(f
))
471 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
476 static expr
*expr2(int critical
)
485 i
= scan(scpriv
, tokval
);
489 if (!(is_simple(e
) || is_just_unknown(e
)) ||
490 !(is_simple(f
) || is_just_unknown(f
))) {
491 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
494 if (is_just_unknown(e
) || is_just_unknown(f
))
497 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
502 static expr
*expr3(int critical
)
510 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
512 i
= scan(scpriv
, tokval
);
516 if (!(is_simple(e
) || is_just_unknown(e
)) ||
517 !(is_simple(f
) || is_just_unknown(f
))) {
518 nasm_error(ERR_NONFATAL
, "shift operator may only be applied to"
520 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
525 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
528 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
536 static expr
*expr4(int critical
)
543 while (i
== '+' || i
== '-') {
545 i
= scan(scpriv
, tokval
);
551 e
= add_vectors(e
, f
);
554 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
561 static expr
*expr5(int critical
)
568 while (i
== '*' || i
== '/' || i
== '%' ||
569 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
571 i
= scan(scpriv
, tokval
);
575 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
576 !(is_simple(f
) || is_just_unknown(f
)))) {
577 nasm_error(ERR_NONFATAL
, "division operator may only be applied to"
581 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
582 nasm_error(ERR_NONFATAL
, "division by zero");
588 e
= scalar_mult(f
, reloc_value(e
), true);
589 else if (is_simple(f
))
590 e
= scalar_mult(e
, reloc_value(f
), true);
591 else if (is_just_unknown(e
) && is_just_unknown(f
))
594 nasm_error(ERR_NONFATAL
, "unable to multiply two "
595 "non-scalar objects");
600 if (is_just_unknown(e
) || is_just_unknown(f
))
603 e
= scalarvect(((uint64_t)reloc_value(e
)) /
604 ((uint64_t)reloc_value(f
)));
607 if (is_just_unknown(e
) || is_just_unknown(f
))
610 e
= scalarvect(((uint64_t)reloc_value(e
)) %
611 ((uint64_t)reloc_value(f
)));
614 if (is_just_unknown(e
) || is_just_unknown(f
))
617 e
= scalarvect(((int64_t)reloc_value(e
)) /
618 ((int64_t)reloc_value(f
)));
621 if (is_just_unknown(e
) || is_just_unknown(f
))
624 e
= scalarvect(((int64_t)reloc_value(e
)) %
625 ((int64_t)reloc_value(f
)));
632 static expr
*eval_floatize(enum floatize type
)
634 uint8_t result
[16], *p
; /* Up to 128 bits */
635 static const struct {
636 int bytes
, start
, len
;
638 { 1, 0, 1 }, /* FLOAT_8 */
639 { 2, 0, 2 }, /* FLOAT_16 */
640 { 4, 0, 4 }, /* FLOAT_32 */
641 { 8, 0, 8 }, /* FLOAT_64 */
642 { 10, 0, 8 }, /* FLOAT_80M */
643 { 10, 8, 2 }, /* FLOAT_80E */
644 { 16, 0, 8 }, /* FLOAT_128L */
645 { 16, 8, 8 }, /* FLOAT_128H */
651 i
= scan(scpriv
, tokval
);
653 nasm_error(ERR_NONFATAL
, "expecting `('");
656 i
= scan(scpriv
, tokval
);
657 if (i
== '-' || i
== '+') {
658 sign
= (i
== '-') ? -1 : 1;
659 i
= scan(scpriv
, tokval
);
661 if (i
!= TOKEN_FLOAT
) {
662 nasm_error(ERR_NONFATAL
, "expecting floating-point number");
665 if (!float_const(tokval
->t_charptr
, sign
, result
, formats
[type
].bytes
))
667 i
= scan(scpriv
, tokval
);
669 nasm_error(ERR_NONFATAL
, "expecting `)'");
673 p
= result
+formats
[type
].start
+formats
[type
].len
;
675 for (j
= formats
[type
].len
; j
; j
--) {
677 val
= (val
<< 8) + *p
;
681 addtotemp(EXPR_SIMPLE
, val
);
683 i
= scan(scpriv
, tokval
);
687 static expr
*eval_strfunc(enum strfunc type
)
692 bool parens
, rn_warn
;
695 i
= scan(scpriv
, tokval
);
698 i
= scan(scpriv
, tokval
);
700 if (i
!= TOKEN_STR
) {
701 nasm_error(ERR_NONFATAL
, "expecting string");
704 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
706 if (string_len
== (size_t)-1) {
707 nasm_error(ERR_NONFATAL
, "invalid string for transform");
711 val
= readstrnum(string
, string_len
, &rn_warn
);
713 i
= scan(scpriv
, tokval
);
715 nasm_error(ERR_NONFATAL
, "expecting `)'");
721 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
724 addtotemp(EXPR_SIMPLE
, val
);
726 i
= scan(scpriv
, tokval
);
730 static int64_t eval_ifunc(int64_t val
, enum ifunc func
)
733 uint64_t uval
= (uint64_t)val
;
739 errtype
= (func
== IFUNC_ILOG2E
) ? ERR_NONFATAL
: ERR_WARNING
;
741 if (!is_power2(uval
))
742 nasm_error(errtype
, "ilog2 argument is not a power of two");
749 rv
= (uval
< 2) ? 0 : ilog2_64(uval
-1) + 1;
753 nasm_panic(0, "invalid IFUNC token %d", func
);
761 static expr
*expr6(int critical
)
773 i
= scan(scpriv
, tokval
);
777 return scalar_mult(e
, -1L, false);
780 i
= scan(scpriv
, tokval
);
781 return expr6(critical
);
784 i
= scan(scpriv
, tokval
);
788 if (is_just_unknown(e
))
789 return unknown_expr();
790 else if (!is_simple(e
)) {
791 nasm_error(ERR_NONFATAL
, "`~' operator may only be applied to"
795 return scalarvect(~reloc_value(e
));
798 i
= scan(scpriv
, tokval
);
802 if (is_just_unknown(e
))
803 return unknown_expr();
804 else if (!is_simple(e
)) {
805 nasm_error(ERR_NONFATAL
, "`!' operator may only be applied to"
809 return scalarvect(!reloc_value(e
));
813 enum ifunc func
= tokval
->t_integer
;
814 i
= scan(scpriv
, tokval
);
818 if (is_just_unknown(e
))
819 return unknown_expr();
820 else if (!is_simple(e
)) {
821 nasm_error(ERR_NONFATAL
, "function may only be applied to"
825 return scalarvect(eval_ifunc(reloc_value(e
), func
));
829 i
= scan(scpriv
, tokval
);
836 if (is_unknown(e
) && critical
) {
837 nasm_error(ERR_NONFATAL
, "unable to determine segment base");
843 return eval_floatize(tokval
->t_integer
);
846 return eval_strfunc(tokval
->t_integer
);
849 i
= scan(scpriv
, tokval
);
854 nasm_error(ERR_NONFATAL
, "expecting `)'");
857 i
= scan(scpriv
, tokval
);
864 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
867 case TOKEN_DECORATOR
:
871 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
874 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
876 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
877 addtotemp(EXPR_SIMPLE
, tmpval
);
880 addtotemp(tokval
->t_integer
, 1L);
881 if (hint
&& hint
->type
== EAH_NOHINT
)
882 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
889 * If !location.known, this indicates that no
890 * symbol, Here or Base references are valid because we
891 * are in preprocess-only mode.
893 if (!location
.known
) {
894 nasm_error(ERR_NONFATAL
,
895 "%s not supported in preprocess-only mode",
896 (i
== TOKEN_HERE
? "`$'" :
897 i
== TOKEN_BASE
? "`$$'" :
898 "symbol references"));
899 addtotemp(EXPR_UNKNOWN
, 1L);
903 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
904 if (i
== TOKEN_BASE
) {
905 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
907 } else if (i
== TOKEN_HERE
) {
908 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
909 label_ofs
= in_absolute
? absolute
.offset
: location
.offset
;
911 if (!lookup_label(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
912 scope
= local_scope(tokval
->t_charptr
);
914 nasm_error(ERR_NONFATAL
, "symbol `%s%s' undefined",
915 scope
,tokval
->t_charptr
);
917 } else if (critical
== 1) {
918 nasm_error(ERR_NONFATAL
,
919 "symbol `%s%s' not defined before use",
920 scope
,tokval
->t_charptr
);
924 *opflags
|= OPFLAG_FORWARD
;
930 if (opflags
&& is_extern(tokval
->t_charptr
))
931 *opflags
|= OPFLAG_EXTERN
;
933 addtotemp(type
, label_ofs
);
934 if (label_seg
!= NO_SEG
)
935 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
937 case TOKEN_DECORATOR
:
938 addtotemp(EXPR_RDSAE
, tokval
->t_integer
);
941 i
= scan(scpriv
, tokval
);
945 nasm_error(ERR_NONFATAL
, "expression syntax error");
950 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
951 int *fwref
, int critical
, struct eval_hints
*hints
)
958 hint
->type
= EAH_NOHINT
;
960 if (critical
& CRITICAL
) {
961 critical
&= ~CRITICAL
;
971 if (tokval
->t_type
== TOKEN_INVALID
)
972 i
= scan(scpriv
, tokval
);
976 while (ntempexprs
) /* initialize temporary storage */
977 nasm_free(tempexprs
[--ntempexprs
]);
983 if (i
== TOKEN_WRT
) {
984 i
= scan(scpriv
, tokval
); /* eat the WRT */
989 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
992 if (is_just_unknown(f
))
998 nasm_error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
1001 value
= reloc_seg(f
);
1002 if (value
== NO_SEG
)
1003 value
= reloc_value(f
) | SEG_ABS
;
1004 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
1005 nasm_error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
1008 addtotemp(EXPR_WRT
, value
);
1011 e
= add_vectors(e
, g
);