1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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
52 #define TEMPEXPRS_DELTA 128
53 #define TEMPEXPR_DELTA 8
55 static scanner scan
; /* Address of scanner routine */
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 i
; /* The t_type of tokval */
71 static struct eval_hints
*hint
;
73 extern int in_abs_seg
; /* ABSOLUTE segment flag */
74 extern int32_t abs_seg
; /* ABSOLUTE segment */
75 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
78 * Unimportant cleanup is done to avoid confusing people who are trying
79 * to debug real memory leaks
81 void eval_cleanup(void)
84 nasm_free(tempexprs
[--ntempexprs
]);
89 * Construct a temporary expression.
91 static void begintemp(void)
94 tempexpr_size
= ntempexpr
= 0;
97 static void addtotemp(int32_t type
, int64_t value
)
99 while (ntempexpr
>= tempexpr_size
) {
100 tempexpr_size
+= TEMPEXPR_DELTA
;
101 tempexpr
= nasm_realloc(tempexpr
,
102 tempexpr_size
* sizeof(*tempexpr
));
104 tempexpr
[ntempexpr
].type
= type
;
105 tempexpr
[ntempexpr
++].value
= value
;
108 static expr
*finishtemp(void)
110 addtotemp(0L, 0L); /* terminate */
111 while (ntempexprs
>= tempexprs_size
) {
112 tempexprs_size
+= TEMPEXPRS_DELTA
;
113 tempexprs
= nasm_realloc(tempexprs
,
114 tempexprs_size
* sizeof(*tempexprs
));
116 return tempexprs
[ntempexprs
++] = tempexpr
;
120 * Add two vector datatypes. We have some bizarre behaviour on far-
121 * absolute segment types: we preserve them during addition _only_
122 * if one of the segments is a truly pure scalar.
124 static expr
*add_vectors(expr
* p
, expr
* q
)
128 preserve
= is_really_simple(p
) || is_really_simple(q
);
132 while (p
->type
&& q
->type
&&
133 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
134 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
137 if (p
->type
> q
->type
) {
138 addtotemp(q
->type
, q
->value
);
139 lasttype
= q
++->type
;
140 } else if (p
->type
< q
->type
) {
141 addtotemp(p
->type
, p
->value
);
142 lasttype
= p
++->type
;
143 } else { /* *p and *q have same type */
144 int64_t sum
= p
->value
+ q
->value
;
146 addtotemp(p
->type
, sum
);
148 hint
->type
= EAH_SUMMED
;
153 if (lasttype
== EXPR_UNKNOWN
) {
157 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
158 addtotemp(p
->type
, p
->value
);
161 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
162 addtotemp(q
->type
, q
->value
);
170 * Multiply a vector by a scalar. Strip far-absolute segment part
173 * Explicit treatment of UNKNOWN is not required in this routine,
174 * since it will silently do the Right Thing anyway.
176 * If `affect_hints' is set, we also change the hint type to
177 * NOTBASE if a MAKEBASE hint points at a register being
178 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
179 * as the base register.
181 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
185 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
186 p
->value
= scalar
* (p
->value
);
187 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
188 p
->type
== hint
->base
&& affect_hints
)
189 hint
->type
= EAH_NOTBASE
;
197 static expr
*scalarvect(int64_t scalar
)
200 addtotemp(EXPR_SIMPLE
, scalar
);
204 static expr
*unknown_expr(void)
207 addtotemp(EXPR_UNKNOWN
, 1L);
212 * The SEG operator: calculate the segment part of a relocatable
213 * value. Return NULL, as usual, if an error occurs. Report the
216 static expr
*segment_part(expr
* e
)
221 return unknown_expr();
224 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
230 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
232 } else if (seg
& SEG_ABS
) {
233 return scalarvect(seg
& ~SEG_ABS
);
234 } else if (seg
& 1) {
235 nasm_error(ERR_NONFATAL
, "SEG applied to something which"
236 " is already a segment base");
239 int32_t base
= ofmt
->segbase(seg
+ 1);
242 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
249 * Recursive-descent parser. Called with a single boolean operand,
250 * which is true if the evaluation is critical (i.e. unresolved
251 * symbols are an error condition). Must update the global `i' to
252 * reflect the token after the parsed string. May return NULL.
254 * evaluate() should report its own errors: on return it is assumed
255 * that if NULL has been returned, the error has already been
262 * expr : bexpr [ WRT expr6 ]
263 * bexpr : rexp0 or expr0 depending on relative-mode setting
264 * rexp0 : rexp1 [ {||} rexp1...]
265 * rexp1 : rexp2 [ {^^} rexp2...]
266 * rexp2 : rexp3 [ {&&} rexp3...]
267 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
268 * expr0 : expr1 [ {|} expr1...]
269 * expr1 : expr2 [ {^} expr2...]
270 * expr2 : expr3 [ {&} expr3...]
271 * expr3 : expr4 [ {<<,>>} expr4...]
272 * expr4 : expr5 [ {+,-} expr5...]
273 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
274 * expr6 : { ~,+,-,IFUNC,SEG } expr6
281 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
283 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
284 static expr
*expr4(int), *expr5(int), *expr6(int);
286 static expr
*(*bexpr
) (int);
288 static expr
*rexp0(int critical
)
296 while (i
== TOKEN_DBL_OR
) {
297 i
= scan(scpriv
, tokval
);
301 if (!(is_simple(e
) || is_just_unknown(e
)) ||
302 !(is_simple(f
) || is_just_unknown(f
))) {
303 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
307 if (is_just_unknown(e
) || is_just_unknown(f
))
310 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
315 static expr
*rexp1(int critical
)
323 while (i
== TOKEN_DBL_XOR
) {
324 i
= scan(scpriv
, tokval
);
328 if (!(is_simple(e
) || is_just_unknown(e
)) ||
329 !(is_simple(f
) || is_just_unknown(f
))) {
330 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
334 if (is_just_unknown(e
) || is_just_unknown(f
))
337 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
342 static expr
*rexp2(int critical
)
349 while (i
== TOKEN_DBL_AND
) {
350 i
= scan(scpriv
, tokval
);
354 if (!(is_simple(e
) || is_just_unknown(e
)) ||
355 !(is_simple(f
) || is_just_unknown(f
))) {
356 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
359 if (is_just_unknown(e
) || is_just_unknown(f
))
362 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
367 static expr
*rexp3(int critical
)
376 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
377 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
379 i
= scan(scpriv
, tokval
);
384 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
390 v
= -1; /* means unknown */
391 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
392 v
= (j
== TOKEN_NE
); /* unequal, so return true if NE */
394 v
= (j
== TOKEN_EQ
); /* equal, so return true if EQ */
398 v
= -1; /* means unknown */
399 else if (!is_really_simple(e
)) {
400 nasm_error(ERR_NONFATAL
,
401 "`%s': operands differ by a non-scalar",
402 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
403 TOKEN_GE
? ">=" : ">"));
404 v
= 0; /* must set it to _something_ */
406 int64_t vv
= reloc_value(e
);
408 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
410 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
412 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
425 static expr
*expr0(int critical
)
434 i
= scan(scpriv
, tokval
);
438 if (!(is_simple(e
) || is_just_unknown(e
)) ||
439 !(is_simple(f
) || is_just_unknown(f
))) {
440 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
443 if (is_just_unknown(e
) || is_just_unknown(f
))
446 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
451 static expr
*expr1(int critical
)
460 i
= scan(scpriv
, tokval
);
464 if (!(is_simple(e
) || is_just_unknown(e
)) ||
465 !(is_simple(f
) || is_just_unknown(f
))) {
466 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
469 if (is_just_unknown(e
) || is_just_unknown(f
))
472 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
477 static expr
*expr2(int critical
)
486 i
= scan(scpriv
, tokval
);
490 if (!(is_simple(e
) || is_just_unknown(e
)) ||
491 !(is_simple(f
) || is_just_unknown(f
))) {
492 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
495 if (is_just_unknown(e
) || is_just_unknown(f
))
498 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
503 static expr
*expr3(int critical
)
511 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
513 i
= scan(scpriv
, tokval
);
517 if (!(is_simple(e
) || is_just_unknown(e
)) ||
518 !(is_simple(f
) || is_just_unknown(f
))) {
519 nasm_error(ERR_NONFATAL
, "shift operator may only be applied to"
521 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
526 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
529 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
537 static expr
*expr4(int critical
)
544 while (i
== '+' || i
== '-') {
546 i
= scan(scpriv
, tokval
);
552 e
= add_vectors(e
, f
);
555 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
562 static expr
*expr5(int critical
)
569 while (i
== '*' || i
== '/' || i
== '%' ||
570 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
572 i
= scan(scpriv
, tokval
);
576 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
577 !(is_simple(f
) || is_just_unknown(f
)))) {
578 nasm_error(ERR_NONFATAL
, "division operator may only be applied to"
582 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
583 nasm_error(ERR_NONFATAL
, "division by zero");
589 e
= scalar_mult(f
, reloc_value(e
), true);
590 else if (is_simple(f
))
591 e
= scalar_mult(e
, reloc_value(f
), true);
592 else if (is_just_unknown(e
) && is_just_unknown(f
))
595 nasm_error(ERR_NONFATAL
, "unable to multiply two "
596 "non-scalar objects");
601 if (is_just_unknown(e
) || is_just_unknown(f
))
604 e
= scalarvect(((uint64_t)reloc_value(e
)) /
605 ((uint64_t)reloc_value(f
)));
608 if (is_just_unknown(e
) || is_just_unknown(f
))
611 e
= scalarvect(((uint64_t)reloc_value(e
)) %
612 ((uint64_t)reloc_value(f
)));
615 if (is_just_unknown(e
) || is_just_unknown(f
))
618 e
= scalarvect(((int64_t)reloc_value(e
)) /
619 ((int64_t)reloc_value(f
)));
622 if (is_just_unknown(e
) || is_just_unknown(f
))
625 e
= scalarvect(((int64_t)reloc_value(e
)) %
626 ((int64_t)reloc_value(f
)));
633 static expr
*eval_floatize(enum floatize type
)
635 uint8_t result
[16], *p
; /* Up to 128 bits */
636 static const struct {
637 int bytes
, start
, len
;
639 { 1, 0, 1 }, /* FLOAT_8 */
640 { 2, 0, 2 }, /* FLOAT_16 */
641 { 4, 0, 4 }, /* FLOAT_32 */
642 { 8, 0, 8 }, /* FLOAT_64 */
643 { 10, 0, 8 }, /* FLOAT_80M */
644 { 10, 8, 2 }, /* FLOAT_80E */
645 { 16, 0, 8 }, /* FLOAT_128L */
646 { 16, 8, 8 }, /* FLOAT_128H */
652 i
= scan(scpriv
, tokval
);
654 nasm_error(ERR_NONFATAL
, "expecting `('");
657 i
= scan(scpriv
, tokval
);
658 if (i
== '-' || i
== '+') {
659 sign
= (i
== '-') ? -1 : 1;
660 i
= scan(scpriv
, tokval
);
662 if (i
!= TOKEN_FLOAT
) {
663 nasm_error(ERR_NONFATAL
, "expecting floating-point number");
666 if (!float_const(tokval
->t_charptr
, sign
, result
, formats
[type
].bytes
))
668 i
= scan(scpriv
, tokval
);
670 nasm_error(ERR_NONFATAL
, "expecting `)'");
674 p
= result
+formats
[type
].start
+formats
[type
].len
;
676 for (j
= formats
[type
].len
; j
; j
--) {
678 val
= (val
<< 8) + *p
;
682 addtotemp(EXPR_SIMPLE
, val
);
684 i
= scan(scpriv
, tokval
);
688 static expr
*eval_strfunc(enum strfunc type
)
693 bool parens
, rn_warn
;
696 i
= scan(scpriv
, tokval
);
699 i
= scan(scpriv
, tokval
);
701 if (i
!= TOKEN_STR
) {
702 nasm_error(ERR_NONFATAL
, "expecting string");
705 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
707 if (string_len
== (size_t)-1) {
708 nasm_error(ERR_NONFATAL
, "invalid string for transform");
712 val
= readstrnum(string
, string_len
, &rn_warn
);
714 i
= scan(scpriv
, tokval
);
716 nasm_error(ERR_NONFATAL
, "expecting `)'");
722 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
725 addtotemp(EXPR_SIMPLE
, val
);
727 i
= scan(scpriv
, tokval
);
731 static int64_t eval_ifunc(int64_t val
, enum ifunc func
)
734 uint64_t uval
= (uint64_t)val
;
740 errtype
= (func
== IFUNC_ILOG2E
) ? ERR_NONFATAL
: ERR_WARNING
;
742 if (!is_power2(uval
))
743 nasm_error(errtype
, "ilog2 argument is not a power of two");
750 rv
= (uval
< 2) ? 0 : ilog2_64(uval
-1) + 1;
754 nasm_panic(0, "invalid IFUNC token %d", func
);
762 static expr
*expr6(int critical
)
774 i
= scan(scpriv
, tokval
);
778 return scalar_mult(e
, -1L, false);
781 i
= scan(scpriv
, tokval
);
782 return expr6(critical
);
785 i
= scan(scpriv
, tokval
);
789 if (is_just_unknown(e
))
790 return unknown_expr();
791 else if (!is_simple(e
)) {
792 nasm_error(ERR_NONFATAL
, "`~' operator may only be applied to"
796 return scalarvect(~reloc_value(e
));
799 i
= scan(scpriv
, tokval
);
803 if (is_just_unknown(e
))
804 return unknown_expr();
805 else if (!is_simple(e
)) {
806 nasm_error(ERR_NONFATAL
, "`!' operator may only be applied to"
810 return scalarvect(!reloc_value(e
));
814 enum ifunc func
= tokval
->t_integer
;
815 i
= scan(scpriv
, tokval
);
819 if (is_just_unknown(e
))
820 return unknown_expr();
821 else if (!is_simple(e
)) {
822 nasm_error(ERR_NONFATAL
, "function may only be applied to"
826 return scalarvect(eval_ifunc(reloc_value(e
), func
));
830 i
= scan(scpriv
, tokval
);
837 if (is_unknown(e
) && critical
) {
838 nasm_error(ERR_NONFATAL
, "unable to determine segment base");
844 return eval_floatize(tokval
->t_integer
);
847 return eval_strfunc(tokval
->t_integer
);
850 i
= scan(scpriv
, tokval
);
855 nasm_error(ERR_NONFATAL
, "expecting `)'");
858 i
= scan(scpriv
, tokval
);
865 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
868 case TOKEN_DECORATOR
:
872 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
875 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
877 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
878 addtotemp(EXPR_SIMPLE
, tmpval
);
881 addtotemp(tokval
->t_integer
, 1L);
882 if (hint
&& hint
->type
== EAH_NOHINT
)
883 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
890 * If !location.known, this indicates that no
891 * symbol, Here or Base references are valid because we
892 * are in preprocess-only mode.
894 if (!location
.known
) {
895 nasm_error(ERR_NONFATAL
,
896 "%s not supported in preprocess-only mode",
897 (i
== TOKEN_HERE
? "`$'" :
898 i
== TOKEN_BASE
? "`$$'" :
899 "symbol references"));
900 addtotemp(EXPR_UNKNOWN
, 1L);
904 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
905 if (i
== TOKEN_BASE
) {
906 label_seg
= in_abs_seg
? abs_seg
: location
.segment
;
908 } else if (i
== TOKEN_HERE
) {
909 label_seg
= in_abs_seg
? abs_seg
: location
.segment
;
910 label_ofs
= in_abs_seg
? abs_offset
: location
.offset
;
912 if (!lookup_label(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
913 scope
= local_scope(tokval
->t_charptr
);
915 nasm_error(ERR_NONFATAL
, "symbol `%s%s' undefined",
916 scope
,tokval
->t_charptr
);
918 } else if (critical
== 1) {
919 nasm_error(ERR_NONFATAL
,
920 "symbol `%s%s' not defined before use",
921 scope
,tokval
->t_charptr
);
925 *opflags
|= OPFLAG_FORWARD
;
931 if (opflags
&& is_extern(tokval
->t_charptr
))
932 *opflags
|= OPFLAG_EXTERN
;
934 addtotemp(type
, label_ofs
);
935 if (label_seg
!= NO_SEG
)
936 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
938 case TOKEN_DECORATOR
:
939 addtotemp(EXPR_RDSAE
, tokval
->t_integer
);
942 i
= scan(scpriv
, tokval
);
946 nasm_error(ERR_NONFATAL
, "expression syntax error");
951 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
952 int *fwref
, int critical
, struct eval_hints
*hints
)
959 hint
->type
= EAH_NOHINT
;
961 if (critical
& CRITICAL
) {
962 critical
&= ~CRITICAL
;
972 if (tokval
->t_type
== TOKEN_INVALID
)
973 i
= scan(scpriv
, tokval
);
977 while (ntempexprs
) /* initialize temporary storage */
978 nasm_free(tempexprs
[--ntempexprs
]);
984 if (i
== TOKEN_WRT
) {
985 i
= scan(scpriv
, tokval
); /* eat the WRT */
990 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
993 if (is_just_unknown(f
))
999 nasm_error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
1002 value
= reloc_seg(f
);
1003 if (value
== NO_SEG
)
1004 value
= reloc_value(f
) | SEG_ABS
;
1005 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
1006 nasm_error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
1009 addtotemp(EXPR_WRT
, value
);
1012 e
= add_vectors(e
, g
);