1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 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
55 #define TEMPEXPRS_DELTA 128
56 #define TEMPEXPR_DELTA 8
58 static scanner scanfunc
; /* Address of scanner routine */
59 static void *scpriv
; /* Scanner private pointer */
61 static expr
**tempexprs
= NULL
;
62 static int ntempexprs
;
63 static int tempexprs_size
= 0;
65 static expr
*tempexpr
;
67 static int tempexpr_size
;
69 static struct tokenval
*tokval
; /* The current token */
70 static int tt
; /* The t_type of tokval */
75 static struct eval_hints
*hint
;
76 static int64_t deadman
;
80 * Unimportant cleanup is done to avoid confusing people who are trying
81 * to debug real memory leaks
83 void eval_cleanup(void)
86 nasm_free(tempexprs
[--ntempexprs
]);
91 * Construct a temporary expression.
93 static void begintemp(void)
96 tempexpr_size
= ntempexpr
= 0;
99 static void addtotemp(int32_t type
, int64_t value
)
101 while (ntempexpr
>= tempexpr_size
) {
102 tempexpr_size
+= TEMPEXPR_DELTA
;
103 tempexpr
= nasm_realloc(tempexpr
,
104 tempexpr_size
* sizeof(*tempexpr
));
106 tempexpr
[ntempexpr
].type
= type
;
107 tempexpr
[ntempexpr
++].value
= value
;
110 static expr
*finishtemp(void)
112 addtotemp(0L, 0L); /* terminate */
113 while (ntempexprs
>= tempexprs_size
) {
114 tempexprs_size
+= TEMPEXPRS_DELTA
;
115 tempexprs
= nasm_realloc(tempexprs
,
116 tempexprs_size
* sizeof(*tempexprs
));
118 return tempexprs
[ntempexprs
++] = tempexpr
;
122 * Add two vector datatypes. We have some bizarre behaviour on far-
123 * absolute segment types: we preserve them during addition _only_
124 * if one of the segments is a truly pure scalar.
126 static expr
*add_vectors(expr
* p
, expr
* q
)
130 preserve
= is_really_simple(p
) || is_really_simple(q
);
134 while (p
->type
&& q
->type
&&
135 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
136 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
139 if (p
->type
> q
->type
) {
140 addtotemp(q
->type
, q
->value
);
141 lasttype
= q
++->type
;
142 } else if (p
->type
< q
->type
) {
143 addtotemp(p
->type
, p
->value
);
144 lasttype
= p
++->type
;
145 } else { /* *p and *q have same type */
146 int64_t sum
= p
->value
+ q
->value
;
148 addtotemp(p
->type
, sum
);
150 hint
->type
= EAH_SUMMED
;
155 if (lasttype
== EXPR_UNKNOWN
) {
159 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
160 addtotemp(p
->type
, p
->value
);
163 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
164 addtotemp(q
->type
, q
->value
);
172 * Multiply a vector by a scalar. Strip far-absolute segment part
175 * Explicit treatment of UNKNOWN is not required in this routine,
176 * since it will silently do the Right Thing anyway.
178 * If `affect_hints' is set, we also change the hint type to
179 * NOTBASE if a MAKEBASE hint points at a register being
180 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
181 * as the base register.
183 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
187 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
188 p
->value
= scalar
* (p
->value
);
189 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
190 p
->type
== hint
->base
&& affect_hints
)
191 hint
->type
= EAH_NOTBASE
;
199 static expr
*scalarvect(int64_t scalar
)
202 addtotemp(EXPR_SIMPLE
, scalar
);
206 static expr
*unknown_expr(void)
209 addtotemp(EXPR_UNKNOWN
, 1L);
214 * The SEG operator: calculate the segment part of a relocatable
215 * value. Return NULL, as usual, if an error occurs. Report the
218 static expr
*segment_part(expr
* e
)
223 return unknown_expr();
226 nasm_nonfatal("cannot apply SEG to a non-relocatable value");
232 nasm_nonfatal("cannot apply SEG to a non-relocatable value");
234 } else if (seg
& SEG_ABS
) {
235 return scalarvect(seg
& ~SEG_ABS
);
236 } else if (seg
& 1) {
237 nasm_nonfatal("SEG applied to something which"
238 " is already a segment base");
241 int32_t base
= ofmt
->segbase(seg
+ 1);
244 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
251 * Recursive-descent parser. Called with a single boolean operand,
252 * which is true if the evaluation is critical (i.e. unresolved
253 * symbols are an error condition). Must update the global `tt' to
254 * reflect the token after the parsed string. May return NULL.
256 * evaluate() should report its own errors: on return it is assumed
257 * that if NULL has been returned, the error has already been
263 * Wrapper function around the scanner
265 static int scan(void)
267 return tt
= scanfunc(scpriv
, tokval
);
273 * expr : bexpr [ WRT expr6 ]
275 * cexpr : rexp0 [ {?} bexpr {:} cexpr ]
276 * rexp0 : rexp1 [ {||} rexp1...]
277 * rexp1 : rexp2 [ {^^} rexp2...]
278 * rexp2 : rexp3 [ {&&} rexp3...]
279 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=,<=>} expr0... ]
280 * expr0 : expr1 [ {|} expr1...]
281 * expr1 : expr2 [ {^} expr2...]
282 * expr2 : expr3 [ {&} expr3...]
283 * expr3 : expr4 [ {<<,>>,<<<,>>>} expr4...]
284 * expr4 : expr5 [ {+,-} expr5...]
285 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
286 * expr6 : { ~,+,-,IFUNC,SEG } expr6
293 static expr
*cexpr(void);
294 static expr
*rexp0(void), *rexp1(void), *rexp2(void), *rexp3(void);
296 static expr
*expr0(void), *expr1(void), *expr2(void), *expr3(void);
297 static expr
*expr4(void), *expr5(void), *expr6(void);
299 /* This inline is a placeholder for the root of the basic expression */
300 static inline expr
*bexpr(void)
305 static expr
*cexpr(void)
320 nasm_nonfatal("`?' without matching `:'");
330 e
= reloc_value(e
) ? f
: g
;
331 } else if (is_just_unknown(e
)) {
334 nasm_nonfatal("the left-hand side of `?' must be "
342 static expr
*rexp0(void)
350 while (tt
== TOKEN_DBL_OR
) {
355 if (!(is_simple(e
) || is_just_unknown(e
)) ||
356 !(is_simple(f
) || is_just_unknown(f
))) {
357 nasm_nonfatal("`|' operator may only be applied to"
361 if (is_just_unknown(e
) || is_just_unknown(f
))
364 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
369 static expr
*rexp1(void)
377 while (tt
== TOKEN_DBL_XOR
) {
382 if (!(is_simple(e
) || is_just_unknown(e
)) ||
383 !(is_simple(f
) || is_just_unknown(f
))) {
384 nasm_nonfatal("`^' operator may only be applied to"
388 if (is_just_unknown(e
) || is_just_unknown(f
))
391 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
396 static expr
*rexp2(void)
403 while (tt
== TOKEN_DBL_AND
) {
408 if (!(is_simple(e
) || is_just_unknown(e
)) ||
409 !(is_simple(f
) || is_just_unknown(f
))) {
410 nasm_nonfatal("`&' operator may only be applied to"
413 if (is_just_unknown(e
) || is_just_unknown(f
))
416 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
421 static expr
*rexp3(void)
430 while (tt
== TOKEN_EQ
|| tt
== TOKEN_LT
|| tt
== TOKEN_GT
||
431 tt
== TOKEN_NE
|| tt
== TOKEN_LE
|| tt
== TOKEN_GE
||
439 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
445 v
= -1; /* means unknown */
446 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
447 v
= (tto
== TOKEN_NE
); /* unequal, so return true if NE */
449 v
= (tto
== TOKEN_EQ
); /* equal, so return true if EQ */
453 v
= -1; /* means unknown */
454 else if (!is_really_simple(e
)) {
455 nasm_nonfatal("`%s': operands differ by a non-scalar",
456 (tto
== TOKEN_LE
? "<=" :
457 tto
== TOKEN_LT
? "<" :
458 tto
== TOKEN_GE
? ">=" :
459 tto
== TOKEN_GT
? ">" :
460 tto
== TOKEN_LEG
? "<=>" :
461 "<internal error>"));
462 v
= 0; /* must set it to _something_ */
464 int64_t vv
= reloc_value(e
);
465 if (tto
== TOKEN_LEG
)
466 v
= (vv
< 0) ? -1 : (vv
> 0) ? 1 : 0;
468 v
= (tto
== TOKEN_LE
|| tto
== TOKEN_GE
);
470 v
= (tto
== TOKEN_GE
|| tto
== TOKEN_GT
);
472 v
= (tto
== TOKEN_LE
|| tto
== TOKEN_LT
);
485 static expr
*expr0(void)
498 if (!(is_simple(e
) || is_just_unknown(e
)) ||
499 !(is_simple(f
) || is_just_unknown(f
))) {
500 nasm_nonfatal("`|' operator may only be applied to"
503 if (is_just_unknown(e
) || is_just_unknown(f
))
506 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
511 static expr
*expr1(void)
524 if (!(is_simple(e
) || is_just_unknown(e
)) ||
525 !(is_simple(f
) || is_just_unknown(f
))) {
526 nasm_nonfatal("`^' operator may only be applied to"
529 if (is_just_unknown(e
) || is_just_unknown(f
))
532 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
537 static expr
*expr2(void)
550 if (!(is_simple(e
) || is_just_unknown(e
)) ||
551 !(is_simple(f
) || is_just_unknown(f
))) {
552 nasm_nonfatal("`&' operator may only be applied to"
555 if (is_just_unknown(e
) || is_just_unknown(f
))
558 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
563 static expr
*expr3(void)
571 while (tt
== TOKEN_SHL
|| tt
== TOKEN_SHR
|| tt
== TOKEN_SAR
) {
577 if (!(is_simple(e
) || is_just_unknown(e
)) ||
578 !(is_simple(f
) || is_just_unknown(f
))) {
579 nasm_nonfatal("shift operator may only be applied to"
581 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
586 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
589 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
593 e
= scalarvect(((int64_t)reloc_value(e
)) >>
602 static expr
*expr4(void)
609 while (tt
== '+' || tt
== '-') {
617 e
= add_vectors(e
, f
);
620 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
627 static expr
*expr5(void)
634 while (tt
== '*' || tt
== '/' || tt
== '%' ||
635 tt
== TOKEN_SDIV
|| tt
== TOKEN_SMOD
) {
641 if (tto
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
642 !(is_simple(f
) || is_just_unknown(f
)))) {
643 nasm_nonfatal("division operator may only be applied to"
647 if (tto
!= '*' && !is_just_unknown(f
) && reloc_value(f
) == 0) {
648 nasm_nonfatal("division by zero");
654 e
= scalar_mult(f
, reloc_value(e
), true);
655 else if (is_simple(f
))
656 e
= scalar_mult(e
, reloc_value(f
), true);
657 else if (is_just_unknown(e
) && is_just_unknown(f
))
660 nasm_nonfatal("unable to multiply two "
661 "non-scalar objects");
666 if (is_just_unknown(e
) || is_just_unknown(f
))
669 e
= scalarvect(((uint64_t)reloc_value(e
)) /
670 ((uint64_t)reloc_value(f
)));
673 if (is_just_unknown(e
) || is_just_unknown(f
))
676 e
= scalarvect(((uint64_t)reloc_value(e
)) %
677 ((uint64_t)reloc_value(f
)));
680 if (is_just_unknown(e
) || is_just_unknown(f
))
683 e
= scalarvect(((int64_t)reloc_value(e
)) /
684 ((int64_t)reloc_value(f
)));
687 if (is_just_unknown(e
) || is_just_unknown(f
))
690 e
= scalarvect(((int64_t)reloc_value(e
)) %
691 ((int64_t)reloc_value(f
)));
698 static expr
*eval_floatize(enum floatize type
)
700 uint8_t result
[16], *p
; /* Up to 128 bits */
701 static const struct {
702 int bytes
, start
, len
;
704 { 1, 0, 1 }, /* FLOAT_8 */
705 { 2, 0, 2 }, /* FLOAT_16 */
706 { 4, 0, 4 }, /* FLOAT_32 */
707 { 8, 0, 8 }, /* FLOAT_64 */
708 { 10, 0, 8 }, /* FLOAT_80M */
709 { 10, 8, 2 }, /* FLOAT_80E */
710 { 16, 0, 8 }, /* FLOAT_128L */
711 { 16, 8, 8 }, /* FLOAT_128H */
719 nasm_nonfatal("expecting `('");
723 if (tt
== '-' || tt
== '+') {
724 sign
= (tt
== '-') ? -1 : 1;
727 if (tt
!= TOKEN_FLOAT
) {
728 nasm_nonfatal("expecting floating-point number");
731 if (!float_const(tokval
->t_charptr
, sign
, result
, formats
[type
].bytes
))
735 nasm_nonfatal("expecting `)'");
739 p
= result
+formats
[type
].start
+formats
[type
].len
;
741 for (i
= formats
[type
].len
; i
; i
--) {
743 val
= (val
<< 8) + *p
;
747 addtotemp(EXPR_SIMPLE
, val
);
753 static expr
*eval_strfunc(enum strfunc type
)
758 bool parens
, rn_warn
;
766 if (tt
!= TOKEN_STR
) {
767 nasm_nonfatal("expecting string");
770 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
772 if (string_len
== (size_t)-1) {
773 nasm_nonfatal("invalid string for transform");
777 val
= readstrnum(string
, string_len
, &rn_warn
);
781 nasm_nonfatal("expecting `)'");
787 nasm_warnf(ERR_PASS1
, "character constant too long");
790 addtotemp(EXPR_SIMPLE
, val
);
796 static int64_t eval_ifunc(int64_t val
, enum ifunc func
)
798 uint64_t uval
= (uint64_t)val
;
804 if (!is_power2(uval
))
805 nasm_error((func
== IFUNC_ILOG2E
) ? ERR_NONFATAL
: ERR_WARNING
,
806 "ilog2 argument is not a power of two");
813 rv
= (uval
< 2) ? 0 : ilog2_64(uval
-1) + 1;
817 nasm_panic("invalid IFUNC token %d", func
);
825 static expr
*expr6(void)
835 if (++deadman
> nasm_limit
[LIMIT_EVAL
]) {
836 nasm_nonfatal("expression too long");
846 return scalar_mult(e
, -1L, false);
857 if (is_just_unknown(e
))
858 return unknown_expr();
859 else if (!is_simple(e
)) {
860 nasm_nonfatal("`~' operator may only be applied to"
864 return scalarvect(~reloc_value(e
));
871 if (is_just_unknown(e
))
872 return unknown_expr();
873 else if (!is_simple(e
)) {
874 nasm_nonfatal("`!' operator may only be applied to"
878 return scalarvect(!reloc_value(e
));
882 enum ifunc func
= tokval
->t_integer
;
887 if (is_just_unknown(e
))
888 return unknown_expr();
889 else if (!is_simple(e
)) {
890 nasm_nonfatal("function may only be applied to"
894 return scalarvect(eval_ifunc(reloc_value(e
), func
));
905 if (is_unknown(e
) && critical
) {
906 nasm_nonfatal("unable to determine segment base");
912 return eval_floatize(tokval
->t_integer
);
915 return eval_strfunc(tokval
->t_integer
);
923 nasm_nonfatal("expecting `)'");
933 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
936 case TOKEN_DECORATOR
:
940 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
943 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
945 nasm_warnf(ERR_PASS1
, "character constant too long");
946 addtotemp(EXPR_SIMPLE
, tmpval
);
949 addtotemp(tokval
->t_integer
, 1L);
950 if (hint
&& hint
->type
== EAH_NOHINT
)
951 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
958 * If !location.known, this indicates that no
959 * symbol, Here or Base references are valid because we
960 * are in preprocess-only mode.
962 if (!location
.known
) {
963 nasm_nonfatal("%s not supported in preprocess-only mode",
964 (tt
== TOKEN_HERE
? "`$'" :
965 tt
== TOKEN_BASE
? "`$$'" :
966 "symbol references"));
967 addtotemp(EXPR_UNKNOWN
, 1L);
971 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
972 if (tt
== TOKEN_BASE
) {
973 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
975 } else if (tt
== TOKEN_HERE
) {
976 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
977 label_ofs
= in_absolute
? absolute
.offset
: location
.offset
;
979 if (!lookup_label(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
980 scope
= local_scope(tokval
->t_charptr
);
982 nasm_nonfatal("symbol `%s%s' undefined",
983 scope
,tokval
->t_charptr
);
985 } else if (critical
== 1) {
986 nasm_nonfatal("symbol `%s%s' not defined before use",
987 scope
,tokval
->t_charptr
);
991 *opflags
|= OPFLAG_FORWARD
;
997 if (opflags
&& is_extern(tokval
->t_charptr
))
998 *opflags
|= OPFLAG_EXTERN
;
1000 addtotemp(type
, label_ofs
);
1001 if (label_seg
!= NO_SEG
)
1002 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
1004 case TOKEN_DECORATOR
:
1005 addtotemp(EXPR_RDSAE
, tokval
->t_integer
);
1009 return finishtemp();
1012 nasm_nonfatal("expression syntax error");
1017 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
1018 int *fwref
, int crit
, struct eval_hints
*hints
)
1027 hint
->type
= EAH_NOHINT
;
1029 critical
= crit
& ~CRITICAL
;
1035 while (ntempexprs
) /* initialize temporary storage */
1036 nasm_free(tempexprs
[--ntempexprs
]);
1038 tt
= tokval
->t_type
;
1039 if (tt
== TOKEN_INVALID
)
1046 if (tt
== TOKEN_WRT
) {
1047 scan(); /* eat the WRT */
1052 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
1055 if (is_just_unknown(f
))
1061 nasm_nonfatal("invalid right-hand operand to WRT");
1064 value
= reloc_seg(f
);
1065 if (value
== NO_SEG
)
1066 value
= reloc_value(f
) | SEG_ABS
;
1067 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
1068 nasm_nonfatal("invalid right-hand operand to WRT");
1071 addtotemp(EXPR_WRT
, value
);
1074 e
= add_vectors(e
, g
);