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 scan
; /* Address of scanner routine */
60 static expr
**tempexprs
= NULL
;
61 static int ntempexprs
;
62 static int tempexprs_size
= 0;
64 static expr
*tempexpr
;
66 static int tempexpr_size
;
68 static struct tokenval
*tokval
; /* The current token */
69 static int i
; /* The t_type of tokval */
74 static struct eval_hints
*hint
;
75 static int64_t deadman
;
79 * Unimportant cleanup is done to avoid confusing people who are trying
80 * to debug real memory leaks
82 void eval_cleanup(void)
85 nasm_free(tempexprs
[--ntempexprs
]);
90 * Construct a temporary expression.
92 static void begintemp(void)
95 tempexpr_size
= ntempexpr
= 0;
98 static void addtotemp(int32_t type
, int64_t value
)
100 while (ntempexpr
>= tempexpr_size
) {
101 tempexpr_size
+= TEMPEXPR_DELTA
;
102 tempexpr
= nasm_realloc(tempexpr
,
103 tempexpr_size
* sizeof(*tempexpr
));
105 tempexpr
[ntempexpr
].type
= type
;
106 tempexpr
[ntempexpr
++].value
= value
;
109 static expr
*finishtemp(void)
111 addtotemp(0L, 0L); /* terminate */
112 while (ntempexprs
>= tempexprs_size
) {
113 tempexprs_size
+= TEMPEXPRS_DELTA
;
114 tempexprs
= nasm_realloc(tempexprs
,
115 tempexprs_size
* sizeof(*tempexprs
));
117 return tempexprs
[ntempexprs
++] = tempexpr
;
121 * Add two vector datatypes. We have some bizarre behaviour on far-
122 * absolute segment types: we preserve them during addition _only_
123 * if one of the segments is a truly pure scalar.
125 static expr
*add_vectors(expr
* p
, expr
* q
)
129 preserve
= is_really_simple(p
) || is_really_simple(q
);
133 while (p
->type
&& q
->type
&&
134 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
135 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
138 if (p
->type
> q
->type
) {
139 addtotemp(q
->type
, q
->value
);
140 lasttype
= q
++->type
;
141 } else if (p
->type
< q
->type
) {
142 addtotemp(p
->type
, p
->value
);
143 lasttype
= p
++->type
;
144 } else { /* *p and *q have same type */
145 int64_t sum
= p
->value
+ q
->value
;
147 addtotemp(p
->type
, sum
);
149 hint
->type
= EAH_SUMMED
;
154 if (lasttype
== EXPR_UNKNOWN
) {
158 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
159 addtotemp(p
->type
, p
->value
);
162 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
163 addtotemp(q
->type
, q
->value
);
171 * Multiply a vector by a scalar. Strip far-absolute segment part
174 * Explicit treatment of UNKNOWN is not required in this routine,
175 * since it will silently do the Right Thing anyway.
177 * If `affect_hints' is set, we also change the hint type to
178 * NOTBASE if a MAKEBASE hint points at a register being
179 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
180 * as the base register.
182 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
186 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
187 p
->value
= scalar
* (p
->value
);
188 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
189 p
->type
== hint
->base
&& affect_hints
)
190 hint
->type
= EAH_NOTBASE
;
198 static expr
*scalarvect(int64_t scalar
)
201 addtotemp(EXPR_SIMPLE
, scalar
);
205 static expr
*unknown_expr(void)
208 addtotemp(EXPR_UNKNOWN
, 1L);
213 * The SEG operator: calculate the segment part of a relocatable
214 * value. Return NULL, as usual, if an error occurs. Report the
217 static expr
*segment_part(expr
* e
)
222 return unknown_expr();
225 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
231 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
233 } else if (seg
& SEG_ABS
) {
234 return scalarvect(seg
& ~SEG_ABS
);
235 } else if (seg
& 1) {
236 nasm_error(ERR_NONFATAL
, "SEG applied to something which"
237 " is already a segment base");
240 int32_t base
= ofmt
->segbase(seg
+ 1);
243 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
250 * Recursive-descent parser. Called with a single boolean operand,
251 * which is true if the evaluation is critical (i.e. unresolved
252 * symbols are an error condition). Must update the global `i' to
253 * reflect the token after the parsed string. May return NULL.
255 * evaluate() should report its own errors: on return it is assumed
256 * that if NULL has been returned, the error has already been
263 * expr : bexpr [ WRT expr6 ]
264 * bexpr : rexp0 or expr0 depending on relative-mode setting
265 * rexp0 : rexp1 [ {||} rexp1...]
266 * rexp1 : rexp2 [ {^^} rexp2...]
267 * rexp2 : rexp3 [ {&&} rexp3...]
268 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
269 * expr0 : expr1 [ {|} expr1...]
270 * expr1 : expr2 [ {^} expr2...]
271 * expr2 : expr3 [ {&} expr3...]
272 * expr3 : expr4 [ {<<,>>} expr4...]
273 * expr4 : expr5 [ {+,-} expr5...]
274 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
275 * expr6 : { ~,+,-,IFUNC,SEG } expr6
282 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
284 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
285 static expr
*expr4(int), *expr5(int), *expr6(int);
287 static expr
*(*bexpr
) (int);
289 static expr
*rexp0(int critical
)
297 while (i
== TOKEN_DBL_OR
) {
298 i
= scan(scpriv
, tokval
);
302 if (!(is_simple(e
) || is_just_unknown(e
)) ||
303 !(is_simple(f
) || is_just_unknown(f
))) {
304 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
308 if (is_just_unknown(e
) || is_just_unknown(f
))
311 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
316 static expr
*rexp1(int critical
)
324 while (i
== TOKEN_DBL_XOR
) {
325 i
= scan(scpriv
, tokval
);
329 if (!(is_simple(e
) || is_just_unknown(e
)) ||
330 !(is_simple(f
) || is_just_unknown(f
))) {
331 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
335 if (is_just_unknown(e
) || is_just_unknown(f
))
338 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
343 static expr
*rexp2(int critical
)
350 while (i
== TOKEN_DBL_AND
) {
351 i
= scan(scpriv
, tokval
);
355 if (!(is_simple(e
) || is_just_unknown(e
)) ||
356 !(is_simple(f
) || is_just_unknown(f
))) {
357 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
360 if (is_just_unknown(e
) || is_just_unknown(f
))
363 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
368 static expr
*rexp3(int critical
)
377 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
378 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
380 i
= scan(scpriv
, tokval
);
385 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
391 v
= -1; /* means unknown */
392 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
393 v
= (j
== TOKEN_NE
); /* unequal, so return true if NE */
395 v
= (j
== TOKEN_EQ
); /* equal, so return true if EQ */
399 v
= -1; /* means unknown */
400 else if (!is_really_simple(e
)) {
401 nasm_error(ERR_NONFATAL
,
402 "`%s': operands differ by a non-scalar",
403 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
404 TOKEN_GE
? ">=" : ">"));
405 v
= 0; /* must set it to _something_ */
407 int64_t vv
= reloc_value(e
);
409 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
411 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
413 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
426 static expr
*expr0(int critical
)
435 i
= scan(scpriv
, tokval
);
439 if (!(is_simple(e
) || is_just_unknown(e
)) ||
440 !(is_simple(f
) || is_just_unknown(f
))) {
441 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
444 if (is_just_unknown(e
) || is_just_unknown(f
))
447 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
452 static expr
*expr1(int critical
)
461 i
= scan(scpriv
, tokval
);
465 if (!(is_simple(e
) || is_just_unknown(e
)) ||
466 !(is_simple(f
) || is_just_unknown(f
))) {
467 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
470 if (is_just_unknown(e
) || is_just_unknown(f
))
473 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
478 static expr
*expr2(int critical
)
487 i
= scan(scpriv
, tokval
);
491 if (!(is_simple(e
) || is_just_unknown(e
)) ||
492 !(is_simple(f
) || is_just_unknown(f
))) {
493 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
496 if (is_just_unknown(e
) || is_just_unknown(f
))
499 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
504 static expr
*expr3(int critical
)
512 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
|| i
== TOKEN_SAR
) {
514 i
= scan(scpriv
, tokval
);
518 if (!(is_simple(e
) || is_just_unknown(e
)) ||
519 !(is_simple(f
) || is_just_unknown(f
))) {
520 nasm_error(ERR_NONFATAL
, "shift operator may only be applied to"
522 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
527 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
530 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
534 e
= scalarvect(((int64_t)reloc_value(e
)) >>
543 static expr
*expr4(int critical
)
550 while (i
== '+' || i
== '-') {
552 i
= scan(scpriv
, tokval
);
558 e
= add_vectors(e
, f
);
561 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
568 static expr
*expr5(int critical
)
575 while (i
== '*' || i
== '/' || i
== '%' ||
576 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
578 i
= scan(scpriv
, tokval
);
582 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
583 !(is_simple(f
) || is_just_unknown(f
)))) {
584 nasm_error(ERR_NONFATAL
, "division operator may only be applied to"
588 if (j
!= '*' && !is_just_unknown(f
) && reloc_value(f
) == 0) {
589 nasm_error(ERR_NONFATAL
, "division by zero");
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
))
601 nasm_error(ERR_NONFATAL
, "unable to multiply two "
602 "non-scalar objects");
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(((uint64_t)reloc_value(e
)) %
618 ((uint64_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
)));
628 if (is_just_unknown(e
) || is_just_unknown(f
))
631 e
= scalarvect(((int64_t)reloc_value(e
)) %
632 ((int64_t)reloc_value(f
)));
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
;
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 */
658 i
= scan(scpriv
, tokval
);
660 nasm_error(ERR_NONFATAL
, "expecting `('");
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 nasm_error(ERR_NONFATAL
, "expecting floating-point number");
672 if (!float_const(tokval
->t_charptr
, sign
, result
, formats
[type
].bytes
))
674 i
= scan(scpriv
, tokval
);
676 nasm_error(ERR_NONFATAL
, "expecting `)'");
680 p
= result
+formats
[type
].start
+formats
[type
].len
;
682 for (j
= formats
[type
].len
; j
; j
--) {
684 val
= (val
<< 8) + *p
;
688 addtotemp(EXPR_SIMPLE
, val
);
690 i
= scan(scpriv
, tokval
);
694 static expr
*eval_strfunc(enum strfunc type
)
699 bool parens
, rn_warn
;
702 i
= scan(scpriv
, tokval
);
705 i
= scan(scpriv
, tokval
);
707 if (i
!= TOKEN_STR
) {
708 nasm_error(ERR_NONFATAL
, "expecting string");
711 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
713 if (string_len
== (size_t)-1) {
714 nasm_error(ERR_NONFATAL
, "invalid string for transform");
718 val
= readstrnum(string
, string_len
, &rn_warn
);
720 i
= scan(scpriv
, tokval
);
722 nasm_error(ERR_NONFATAL
, "expecting `)'");
728 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
731 addtotemp(EXPR_SIMPLE
, val
);
733 i
= scan(scpriv
, tokval
);
737 static int64_t eval_ifunc(int64_t val
, enum ifunc func
)
740 uint64_t uval
= (uint64_t)val
;
746 errtype
= (func
== IFUNC_ILOG2E
) ? ERR_NONFATAL
: ERR_WARNING
;
748 if (!is_power2(uval
))
749 nasm_error(errtype
, "ilog2 argument is not a power of two");
756 rv
= (uval
< 2) ? 0 : ilog2_64(uval
-1) + 1;
760 nasm_panic("invalid IFUNC token %d", func
);
768 static expr
*expr6(int critical
)
778 if (++deadman
> nasm_limit
[LIMIT_EVAL
]) {
779 nasm_error(ERR_NONFATAL
, "expression too long");
785 i
= scan(scpriv
, tokval
);
789 return scalar_mult(e
, -1L, false);
792 i
= scan(scpriv
, tokval
);
793 return expr6(critical
);
796 i
= scan(scpriv
, tokval
);
800 if (is_just_unknown(e
))
801 return unknown_expr();
802 else if (!is_simple(e
)) {
803 nasm_error(ERR_NONFATAL
, "`~' operator may only be applied to"
807 return scalarvect(~reloc_value(e
));
810 i
= scan(scpriv
, tokval
);
814 if (is_just_unknown(e
))
815 return unknown_expr();
816 else if (!is_simple(e
)) {
817 nasm_error(ERR_NONFATAL
, "`!' operator may only be applied to"
821 return scalarvect(!reloc_value(e
));
825 enum ifunc func
= tokval
->t_integer
;
826 i
= scan(scpriv
, tokval
);
830 if (is_just_unknown(e
))
831 return unknown_expr();
832 else if (!is_simple(e
)) {
833 nasm_error(ERR_NONFATAL
, "function may only be applied to"
837 return scalarvect(eval_ifunc(reloc_value(e
), func
));
841 i
= scan(scpriv
, tokval
);
848 if (is_unknown(e
) && critical
) {
849 nasm_error(ERR_NONFATAL
, "unable to determine segment base");
855 return eval_floatize(tokval
->t_integer
);
858 return eval_strfunc(tokval
->t_integer
);
861 i
= scan(scpriv
, tokval
);
866 nasm_error(ERR_NONFATAL
, "expecting `)'");
869 i
= scan(scpriv
, tokval
);
876 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
879 case TOKEN_DECORATOR
:
883 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
886 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
888 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
889 addtotemp(EXPR_SIMPLE
, tmpval
);
892 addtotemp(tokval
->t_integer
, 1L);
893 if (hint
&& hint
->type
== EAH_NOHINT
)
894 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
901 * If !location.known, this indicates that no
902 * symbol, Here or Base references are valid because we
903 * are in preprocess-only mode.
905 if (!location
.known
) {
906 nasm_error(ERR_NONFATAL
,
907 "%s not supported in preprocess-only mode",
908 (i
== TOKEN_HERE
? "`$'" :
909 i
== TOKEN_BASE
? "`$$'" :
910 "symbol references"));
911 addtotemp(EXPR_UNKNOWN
, 1L);
915 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
916 if (i
== TOKEN_BASE
) {
917 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
919 } else if (i
== TOKEN_HERE
) {
920 label_seg
= in_absolute
? absolute
.segment
: location
.segment
;
921 label_ofs
= in_absolute
? absolute
.offset
: location
.offset
;
923 if (!lookup_label(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
924 scope
= local_scope(tokval
->t_charptr
);
926 nasm_error(ERR_NONFATAL
, "symbol `%s%s' undefined",
927 scope
,tokval
->t_charptr
);
929 } else if (critical
== 1) {
930 nasm_error(ERR_NONFATAL
,
931 "symbol `%s%s' not defined before use",
932 scope
,tokval
->t_charptr
);
936 *opflags
|= OPFLAG_FORWARD
;
942 if (opflags
&& is_extern(tokval
->t_charptr
))
943 *opflags
|= OPFLAG_EXTERN
;
945 addtotemp(type
, label_ofs
);
946 if (label_seg
!= NO_SEG
)
947 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
949 case TOKEN_DECORATOR
:
950 addtotemp(EXPR_RDSAE
, tokval
->t_integer
);
953 i
= scan(scpriv
, tokval
);
957 nasm_error(ERR_NONFATAL
, "expression syntax error");
962 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
963 int *fwref
, int critical
, struct eval_hints
*hints
)
972 hint
->type
= EAH_NOHINT
;
974 if (critical
& CRITICAL
) {
975 critical
&= ~CRITICAL
;
985 if (tokval
->t_type
== TOKEN_INVALID
)
986 i
= scan(scpriv
, tokval
);
990 while (ntempexprs
) /* initialize temporary storage */
991 nasm_free(tempexprs
[--ntempexprs
]);
997 if (i
== TOKEN_WRT
) {
998 i
= scan(scpriv
, tokval
); /* eat the WRT */
1003 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
1006 if (is_just_unknown(f
))
1012 nasm_error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
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");
1022 addtotemp(EXPR_WRT
, value
);
1025 e
= add_vectors(e
, g
);