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
51 #define TEMPEXPRS_DELTA 128
52 #define TEMPEXPR_DELTA 8
54 static scanner scanfunc
; /* Address of scanner routine */
55 static void *scpriv
; /* Scanner private pointer */
57 static expr
**tempexprs
= NULL
;
58 static int ntempexprs
;
59 static int tempexprs_size
= 0;
61 static expr
*tempexpr
;
63 static int tempexpr_size
;
65 static struct tokenval
*tokval
; /* The current token */
66 static int tt
; /* The t_type of tokval */
71 static struct eval_hints
*hint
;
72 static int64_t deadman
;
76 * Unimportant cleanup is done to avoid confusing people who are trying
77 * to debug real memory leaks
79 void eval_cleanup(void)
82 nasm_free(tempexprs
[--ntempexprs
]);
87 * Construct a temporary expression.
89 static void begintemp(void)
92 tempexpr_size
= ntempexpr
= 0;
95 static void addtotemp(int32_t type
, int64_t value
)
97 while (ntempexpr
>= tempexpr_size
) {
98 tempexpr_size
+= TEMPEXPR_DELTA
;
99 tempexpr
= nasm_realloc(tempexpr
,
100 tempexpr_size
* sizeof(*tempexpr
));
102 tempexpr
[ntempexpr
].type
= type
;
103 tempexpr
[ntempexpr
++].value
= value
;
106 static expr
*finishtemp(void)
108 addtotemp(0L, 0L); /* terminate */
109 while (ntempexprs
>= tempexprs_size
) {
110 tempexprs_size
+= TEMPEXPRS_DELTA
;
111 tempexprs
= nasm_realloc(tempexprs
,
112 tempexprs_size
* sizeof(*tempexprs
));
114 return tempexprs
[ntempexprs
++] = tempexpr
;
118 * Add two vector datatypes. We have some bizarre behaviour on far-
119 * absolute segment types: we preserve them during addition _only_
120 * if one of the segments is a truly pure scalar.
122 static expr
*add_vectors(expr
* p
, expr
* q
)
126 preserve
= is_really_simple(p
) || is_really_simple(q
);
130 while (p
->type
&& q
->type
&&
131 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
132 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
135 if (p
->type
> q
->type
) {
136 addtotemp(q
->type
, q
->value
);
137 lasttype
= q
++->type
;
138 } else if (p
->type
< q
->type
) {
139 addtotemp(p
->type
, p
->value
);
140 lasttype
= p
++->type
;
141 } else { /* *p and *q have same type */
142 int64_t sum
= p
->value
+ q
->value
;
144 addtotemp(p
->type
, sum
);
146 hint
->type
= EAH_SUMMED
;
151 if (lasttype
== EXPR_UNKNOWN
) {
155 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
156 addtotemp(p
->type
, p
->value
);
159 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
160 addtotemp(q
->type
, q
->value
);
168 * Multiply a vector by a scalar. Strip far-absolute segment part
171 * Explicit treatment of UNKNOWN is not required in this routine,
172 * since it will silently do the Right Thing anyway.
174 * If `affect_hints' is set, we also change the hint type to
175 * NOTBASE if a MAKEBASE hint points at a register being
176 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
177 * as the base register.
179 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
183 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
184 p
->value
= scalar
* (p
->value
);
185 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
186 p
->type
== hint
->base
&& affect_hints
)
187 hint
->type
= EAH_NOTBASE
;
195 static expr
*scalarvect(int64_t scalar
)
198 addtotemp(EXPR_SIMPLE
, scalar
);
202 static expr
*unknown_expr(void)
205 addtotemp(EXPR_UNKNOWN
, 1L);
210 * The SEG operator: calculate the segment part of a relocatable
211 * value. Return NULL, as usual, if an error occurs. Report the
214 static expr
*segment_part(expr
* e
)
219 return unknown_expr();
222 nasm_nonfatal("cannot apply SEG to a non-relocatable value");
228 nasm_nonfatal("cannot apply SEG to a non-relocatable value");
230 } else if (seg
& SEG_ABS
) {
231 return scalarvect(seg
& ~SEG_ABS
);
232 } else if (seg
& 1) {
233 nasm_nonfatal("SEG applied to something which"
234 " is already a segment base");
237 int32_t base
= ofmt
->segbase(seg
+ 1);
240 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
247 * Recursive-descent parser. Called with a single boolean operand,
248 * which is true if the evaluation is critical (i.e. unresolved
249 * symbols are an error condition). Must update the global `tt' to
250 * reflect the token after the parsed string. May return NULL.
252 * evaluate() should report its own errors: on return it is assumed
253 * that if NULL has been returned, the error has already been
259 * Wrapper function around the scanner
261 static int scan(void)
263 return tt
= scanfunc(scpriv
, tokval
);
269 * expr : bexpr [ WRT expr6 ]
271 * cexpr : rexp0 [ {?} bexpr {:} cexpr ]
272 * rexp0 : rexp1 [ {||} rexp1...]
273 * rexp1 : rexp2 [ {^^} rexp2...]
274 * rexp2 : rexp3 [ {&&} rexp3...]
275 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=,<=>} expr0... ]
276 * expr0 : expr1 [ {|} expr1...]
277 * expr1 : expr2 [ {^} expr2...]
278 * expr2 : expr3 [ {&} expr3...]
279 * expr3 : expr4 [ {<<,>>,<<<,>>>} expr4...]
280 * expr4 : expr5 [ {+,-} expr5...]
281 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
282 * expr6 : { ~,+,-,IFUNC,SEG } expr6
289 static expr
*cexpr(void);
290 static expr
*rexp0(void), *rexp1(void), *rexp2(void), *rexp3(void);
292 static expr
*expr0(void), *expr1(void), *expr2(void), *expr3(void);
293 static expr
*expr4(void), *expr5(void), *expr6(void);
295 /* This inline is a placeholder for the root of the basic expression */
296 static inline expr
*bexpr(void)
301 static expr
*cexpr(void)
309 if (tt
== TOKEN_QMARK
) {
316 nasm_nonfatal("`?' without matching `:'");
326 e
= reloc_value(e
) ? f
: g
;
327 } else if (is_just_unknown(e
)) {
330 nasm_nonfatal("the left-hand side of `?' must be "
338 static expr
*rexp0(void)
346 while (tt
== TOKEN_DBL_OR
) {
351 if (!(is_simple(e
) || is_just_unknown(e
)) ||
352 !(is_simple(f
) || is_just_unknown(f
))) {
353 nasm_nonfatal("`|' operator may only be applied to"
357 if (is_just_unknown(e
) || is_just_unknown(f
))
360 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
365 static expr
*rexp1(void)
373 while (tt
== TOKEN_DBL_XOR
) {
378 if (!(is_simple(e
) || is_just_unknown(e
)) ||
379 !(is_simple(f
) || is_just_unknown(f
))) {
380 nasm_nonfatal("`^' operator may only be applied to"
384 if (is_just_unknown(e
) || is_just_unknown(f
))
387 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
392 static expr
*rexp2(void)
399 while (tt
== TOKEN_DBL_AND
) {
404 if (!(is_simple(e
) || is_just_unknown(e
)) ||
405 !(is_simple(f
) || is_just_unknown(f
))) {
406 nasm_nonfatal("`&' operator may only be applied to"
409 if (is_just_unknown(e
) || is_just_unknown(f
))
412 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
417 static expr
*rexp3(void)
426 while (tt
== TOKEN_EQ
|| tt
== TOKEN_LT
|| tt
== TOKEN_GT
||
427 tt
== TOKEN_NE
|| tt
== TOKEN_LE
|| tt
== TOKEN_GE
||
435 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
441 v
= -1; /* means unknown */
442 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
443 v
= (tto
== TOKEN_NE
); /* unequal, so return true if NE */
445 v
= (tto
== TOKEN_EQ
); /* equal, so return true if EQ */
449 v
= -1; /* means unknown */
450 else if (!is_really_simple(e
)) {
451 nasm_nonfatal("`%s': operands differ by a non-scalar",
452 (tto
== TOKEN_LE
? "<=" :
453 tto
== TOKEN_LT
? "<" :
454 tto
== TOKEN_GE
? ">=" :
455 tto
== TOKEN_GT
? ">" :
456 tto
== TOKEN_LEG
? "<=>" :
457 "<internal error>"));
458 v
= 0; /* must set it to _something_ */
460 int64_t vv
= reloc_value(e
);
461 if (tto
== TOKEN_LEG
)
462 v
= (vv
< 0) ? -1 : (vv
> 0) ? 1 : 0;
464 v
= (tto
== TOKEN_LE
|| tto
== TOKEN_GE
);
466 v
= (tto
== TOKEN_GE
|| tto
== TOKEN_GT
);
468 v
= (tto
== TOKEN_LE
|| tto
== TOKEN_LT
);
481 static expr
*expr0(void)
494 if (!(is_simple(e
) || is_just_unknown(e
)) ||
495 !(is_simple(f
) || is_just_unknown(f
))) {
496 nasm_nonfatal("`|' operator may only be applied to"
499 if (is_just_unknown(e
) || is_just_unknown(f
))
502 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
507 static expr
*expr1(void)
520 if (!(is_simple(e
) || is_just_unknown(e
)) ||
521 !(is_simple(f
) || is_just_unknown(f
))) {
522 nasm_nonfatal("`^' operator may only be applied to"
525 if (is_just_unknown(e
) || is_just_unknown(f
))
528 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
533 static expr
*expr2(void)
546 if (!(is_simple(e
) || is_just_unknown(e
)) ||
547 !(is_simple(f
) || is_just_unknown(f
))) {
548 nasm_nonfatal("`&' operator may only be applied to"
551 if (is_just_unknown(e
) || is_just_unknown(f
))
554 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
559 static expr
*expr3(void)
567 while (tt
== TOKEN_SHL
|| tt
== TOKEN_SHR
|| tt
== TOKEN_SAR
) {
573 if (!(is_simple(e
) || is_just_unknown(e
)) ||
574 !(is_simple(f
) || is_just_unknown(f
))) {
575 nasm_nonfatal("shift operator may only be applied to"
577 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
582 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
585 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
589 e
= scalarvect(((int64_t)reloc_value(e
)) >>
598 static expr
*expr4(void)
605 while (tt
== '+' || tt
== '-') {
613 e
= add_vectors(e
, f
);
616 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
623 static expr
*expr5(void)
630 while (tt
== '*' || tt
== '/' || tt
== '%' ||
631 tt
== TOKEN_SDIV
|| tt
== TOKEN_SMOD
) {
637 if (tto
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
638 !(is_simple(f
) || is_just_unknown(f
)))) {
639 nasm_nonfatal("division operator may only be applied to"
643 if (tto
!= '*' && !is_just_unknown(f
) && reloc_value(f
) == 0) {
644 nasm_nonfatal("division by zero");
650 e
= scalar_mult(f
, reloc_value(e
), true);
651 else if (is_simple(f
))
652 e
= scalar_mult(e
, reloc_value(f
), true);
653 else if (is_just_unknown(e
) && is_just_unknown(f
))
656 nasm_nonfatal("unable to multiply two "
657 "non-scalar objects");
662 if (is_just_unknown(e
) || is_just_unknown(f
))
665 e
= scalarvect(((uint64_t)reloc_value(e
)) /
666 ((uint64_t)reloc_value(f
)));
669 if (is_just_unknown(e
) || is_just_unknown(f
))
672 e
= scalarvect(((uint64_t)reloc_value(e
)) %
673 ((uint64_t)reloc_value(f
)));
676 if (is_just_unknown(e
) || is_just_unknown(f
))
679 e
= scalarvect(((int64_t)reloc_value(e
)) /
680 ((int64_t)reloc_value(f
)));
683 if (is_just_unknown(e
) || is_just_unknown(f
))
686 e
= scalarvect(((int64_t)reloc_value(e
)) %
687 ((int64_t)reloc_value(f
)));
694 static expr
*eval_floatize(enum floatize type
)
696 uint8_t result
[16], *p
; /* Up to 128 bits */
697 static const struct {
698 int bytes
, start
, len
;
700 { 1, 0, 1 }, /* FLOAT_8 */
701 { 2, 0, 2 }, /* FLOAT_16 */
702 { 4, 0, 4 }, /* FLOAT_32 */
703 { 8, 0, 8 }, /* FLOAT_64 */
704 { 10, 0, 8 }, /* FLOAT_80M */
705 { 10, 8, 2 }, /* FLOAT_80E */
706 { 16, 0, 8 }, /* FLOAT_128L */
707 { 16, 8, 8 }, /* FLOAT_128H */
715 nasm_nonfatal("expecting `('");
719 if (tt
== '-' || tt
== '+') {
720 sign
= (tt
== '-') ? -1 : 1;
723 if (tt
!= TOKEN_FLOAT
) {
724 nasm_nonfatal("expecting floating-point number");
727 if (!float_const(tokval
->t_charptr
, sign
, result
, formats
[type
].bytes
))
731 nasm_nonfatal("expecting `)'");
735 p
= result
+formats
[type
].start
+formats
[type
].len
;
737 for (i
= formats
[type
].len
; i
; i
--) {
739 val
= (val
<< 8) + *p
;
743 addtotemp(EXPR_SIMPLE
, val
);
749 static expr
*eval_strfunc(enum strfunc type
, const char *name
)
754 bool parens
, rn_warn
;
762 if (tt
!= TOKEN_STR
) {
763 nasm_nonfatal("expecting string as argument to %s", name
);
766 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
768 if (string_len
== (size_t)-1) {
769 nasm_nonfatal("invalid input string to %s", name
);
773 val
= readstrnum(string
, string_len
, &rn_warn
);
777 nasm_nonfatal("expecting `)'");
783 nasm_warn(WARN_OTHER
, "character constant too long");
786 addtotemp(EXPR_SIMPLE
, val
);
792 static int64_t eval_ifunc(int64_t val
, enum ifunc func
)
794 uint64_t uval
= (uint64_t)val
;
800 if (!is_power2(uval
))
801 nasm_error((func
== IFUNC_ILOG2E
) ? ERR_NONFATAL
: ERR_WARNING
|WARN_OTHER
,
802 "ilog2 argument is not a power of two");
809 rv
= (uval
< 2) ? 0 : ilog2_64(uval
-1) + 1;
813 nasm_panic("invalid IFUNC token %d", func
);
821 static expr
*expr6(void)
831 if (++deadman
> nasm_limit
[LIMIT_EVAL
]) {
832 nasm_nonfatal("expression too long");
842 return scalar_mult(e
, -1L, false);
853 if (is_just_unknown(e
))
854 return unknown_expr();
855 else if (!is_simple(e
)) {
856 nasm_nonfatal("`~' operator may only be applied to"
860 return scalarvect(~reloc_value(e
));
867 if (is_just_unknown(e
))
868 return unknown_expr();
869 else if (!is_simple(e
)) {
870 nasm_nonfatal("`!' operator may only be applied to"
874 return scalarvect(!reloc_value(e
));
878 enum ifunc func
= tokval
->t_integer
;
883 if (is_just_unknown(e
))
884 return unknown_expr();
885 else if (!is_simple(e
)) {
886 nasm_nonfatal("function may only be applied to"
890 return scalarvect(eval_ifunc(reloc_value(e
), func
));
901 if (is_unknown(e
) && critical
) {
902 nasm_nonfatal("unable to determine segment base");
908 return eval_floatize(tokval
->t_integer
);
911 return eval_strfunc(tokval
->t_integer
, tokval
->t_charptr
);
919 nasm_nonfatal("expecting `)'");
929 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
932 case TOKEN_DECORATOR
:
936 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
939 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
941 nasm_warn(WARN_OTHER
, "character constant too long");
942 addtotemp(EXPR_SIMPLE
, tmpval
);
945 addtotemp(tokval
->t_integer
, 1L);
946 if (hint
&& hint
->type
== EAH_NOHINT
)
947 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
954 * If !location.known, this indicates that no
955 * symbol, Here or Base references are valid because we
956 * are in preprocess-only mode.
958 if (!location
.known
) {
959 nasm_nonfatal("%s not supported in preprocess-only mode",
960 (tt
== TOKEN_HERE
? "`$'" :
961 tt
== TOKEN_BASE
? "`$$'" :
962 "symbol references"));
963 addtotemp(EXPR_UNKNOWN
, 1L);
967 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
968 if (tt
== TOKEN_BASE
) {
969 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
971 } else if (tt
== TOKEN_HERE
) {
972 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
973 label_ofs
= in_absolute
? absolute
.offset
: location
.offset
;
975 enum label_type ltype
;
976 ltype
= lookup_label(tokval
->t_charptr
, &label_seg
, &label_ofs
);
977 if (ltype
== LBL_none
) {
978 scope
= local_scope(tokval
->t_charptr
);
980 nasm_nonfatal("symbol `%s%s' not defined%s",
981 scope
,tokval
->t_charptr
,
982 pass_first() ? " before use" : "");
986 *opflags
|= OPFLAG_FORWARD
;
990 } else if (is_extern(ltype
)) {
992 *opflags
|= OPFLAG_EXTERN
;
995 addtotemp(type
, label_ofs
);
996 if (label_seg
!= NO_SEG
)
997 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
999 case TOKEN_DECORATOR
:
1000 addtotemp(EXPR_RDSAE
, tokval
->t_integer
);
1004 return finishtemp();
1007 nasm_nonfatal("expression syntax error");
1012 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
1013 int *fwref
, bool crit
, struct eval_hints
*hints
)
1022 hint
->type
= EAH_NOHINT
;
1030 while (ntempexprs
) /* initialize temporary storage */
1031 nasm_free(tempexprs
[--ntempexprs
]);
1033 tt
= tokval
->t_type
;
1034 if (tt
== TOKEN_INVALID
)
1041 if (tt
== TOKEN_WRT
) {
1042 scan(); /* eat the WRT */
1047 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
1050 if (is_just_unknown(f
))
1056 nasm_nonfatal("invalid right-hand operand to WRT");
1059 value
= reloc_seg(f
);
1060 if (value
== NO_SEG
)
1061 value
= reloc_value(f
) | SEG_ABS
;
1062 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
1063 nasm_nonfatal("invalid right-hand operand to WRT");
1066 addtotemp(EXPR_WRT
, value
);
1069 e
= add_vectors(e
, g
);