1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2012 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
53 #define TEMPEXPRS_DELTA 128
54 #define TEMPEXPR_DELTA 8
56 static scanner scan
; /* Address of scanner routine */
57 static lfunc labelfunc
; /* Address of label routine */
59 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
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 i
; /* The t_type of tokval */
73 static struct location
*location
; /* Pointer to current line's segment,offset */
76 static struct eval_hints
*hint
;
78 extern int in_abs_seg
; /* ABSOLUTE segment flag */
79 extern int32_t abs_seg
; /* ABSOLUTE segment */
80 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
83 * Unimportant cleanup is done to avoid confusing people who are trying
84 * to debug real memory leaks
86 void eval_cleanup(void)
89 nasm_free(tempexprs
[--ntempexprs
]);
94 * Construct a temporary expression.
96 static void begintemp(void)
99 tempexpr_size
= ntempexpr
= 0;
102 static void addtotemp(int32_t type
, int64_t value
)
104 while (ntempexpr
>= tempexpr_size
) {
105 tempexpr_size
+= TEMPEXPR_DELTA
;
106 tempexpr
= nasm_realloc(tempexpr
,
107 tempexpr_size
* sizeof(*tempexpr
));
109 tempexpr
[ntempexpr
].type
= type
;
110 tempexpr
[ntempexpr
++].value
= value
;
113 static expr
*finishtemp(void)
115 addtotemp(0L, 0L); /* terminate */
116 while (ntempexprs
>= tempexprs_size
) {
117 tempexprs_size
+= TEMPEXPRS_DELTA
;
118 tempexprs
= nasm_realloc(tempexprs
,
119 tempexprs_size
* sizeof(*tempexprs
));
121 return tempexprs
[ntempexprs
++] = tempexpr
;
125 * Add two vector datatypes. We have some bizarre behaviour on far-
126 * absolute segment types: we preserve them during addition _only_
127 * if one of the segments is a truly pure scalar.
129 static expr
*add_vectors(expr
* p
, expr
* q
)
133 preserve
= is_really_simple(p
) || is_really_simple(q
);
137 while (p
->type
&& q
->type
&&
138 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
139 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
142 if (p
->type
> q
->type
) {
143 addtotemp(q
->type
, q
->value
);
144 lasttype
= q
++->type
;
145 } else if (p
->type
< q
->type
) {
146 addtotemp(p
->type
, p
->value
);
147 lasttype
= p
++->type
;
148 } else { /* *p and *q have same type */
149 int64_t sum
= p
->value
+ q
->value
;
151 addtotemp(p
->type
, sum
);
153 hint
->type
= EAH_SUMMED
;
158 if (lasttype
== EXPR_UNKNOWN
) {
162 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
163 addtotemp(p
->type
, p
->value
);
166 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
167 addtotemp(q
->type
, q
->value
);
175 * Multiply a vector by a scalar. Strip far-absolute segment part
178 * Explicit treatment of UNKNOWN is not required in this routine,
179 * since it will silently do the Right Thing anyway.
181 * If `affect_hints' is set, we also change the hint type to
182 * NOTBASE if a MAKEBASE hint points at a register being
183 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
184 * as the base register.
186 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
190 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
191 p
->value
= scalar
* (p
->value
);
192 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
193 p
->type
== hint
->base
&& affect_hints
)
194 hint
->type
= EAH_NOTBASE
;
202 static expr
*scalarvect(int64_t scalar
)
205 addtotemp(EXPR_SIMPLE
, scalar
);
209 static expr
*unknown_expr(void)
212 addtotemp(EXPR_UNKNOWN
, 1L);
217 * The SEG operator: calculate the segment part of a relocatable
218 * value. Return NULL, as usual, if an error occurs. Report the
221 static expr
*segment_part(expr
* e
)
226 return unknown_expr();
229 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
235 nasm_error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
237 } else if (seg
& SEG_ABS
) {
238 return scalarvect(seg
& ~SEG_ABS
);
239 } else if (seg
& 1) {
240 nasm_error(ERR_NONFATAL
, "SEG applied to something which"
241 " is already a segment base");
244 int32_t base
= outfmt
->segbase(seg
+ 1);
247 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
254 * Recursive-descent parser. Called with a single boolean operand,
255 * which is true if the evaluation is critical (i.e. unresolved
256 * symbols are an error condition). Must update the global `i' to
257 * reflect the token after the parsed string. May return NULL.
259 * evaluate() should report its own errors: on return it is assumed
260 * that if NULL has been returned, the error has already been
267 * expr : bexpr [ WRT expr6 ]
268 * bexpr : rexp0 or expr0 depending on relative-mode setting
269 * rexp0 : rexp1 [ {||} rexp1...]
270 * rexp1 : rexp2 [ {^^} rexp2...]
271 * rexp2 : rexp3 [ {&&} rexp3...]
272 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
273 * expr0 : expr1 [ {|} expr1...]
274 * expr1 : expr2 [ {^} expr2...]
275 * expr2 : expr3 [ {&} expr3...]
276 * expr3 : expr4 [ {<<,>>} expr4...]
277 * expr4 : expr5 [ {+,-} expr5...]
278 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
279 * expr6 : { ~,+,-,IFUNC,SEG } expr6
286 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
288 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
289 static expr
*expr4(int), *expr5(int), *expr6(int);
291 static expr
*(*bexpr
) (int);
293 static expr
*rexp0(int critical
)
301 while (i
== TOKEN_DBL_OR
) {
302 i
= scan(scpriv
, tokval
);
306 if (!(is_simple(e
) || is_just_unknown(e
)) ||
307 !(is_simple(f
) || is_just_unknown(f
))) {
308 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
312 if (is_just_unknown(e
) || is_just_unknown(f
))
315 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
320 static expr
*rexp1(int critical
)
328 while (i
== TOKEN_DBL_XOR
) {
329 i
= scan(scpriv
, tokval
);
333 if (!(is_simple(e
) || is_just_unknown(e
)) ||
334 !(is_simple(f
) || is_just_unknown(f
))) {
335 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
339 if (is_just_unknown(e
) || is_just_unknown(f
))
342 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
347 static expr
*rexp2(int critical
)
354 while (i
== TOKEN_DBL_AND
) {
355 i
= scan(scpriv
, tokval
);
359 if (!(is_simple(e
) || is_just_unknown(e
)) ||
360 !(is_simple(f
) || is_just_unknown(f
))) {
361 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
364 if (is_just_unknown(e
) || is_just_unknown(f
))
367 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
372 static expr
*rexp3(int critical
)
381 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
382 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
384 i
= scan(scpriv
, tokval
);
389 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
395 v
= -1; /* means unknown */
396 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
397 v
= (j
== TOKEN_NE
); /* unequal, so return true if NE */
399 v
= (j
== TOKEN_EQ
); /* equal, so return true if EQ */
403 v
= -1; /* means unknown */
404 else if (!is_really_simple(e
)) {
405 nasm_error(ERR_NONFATAL
,
406 "`%s': operands differ by a non-scalar",
407 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
408 TOKEN_GE
? ">=" : ">"));
409 v
= 0; /* must set it to _something_ */
411 int64_t vv
= reloc_value(e
);
413 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
415 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
417 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
430 static expr
*expr0(int critical
)
439 i
= scan(scpriv
, tokval
);
443 if (!(is_simple(e
) || is_just_unknown(e
)) ||
444 !(is_simple(f
) || is_just_unknown(f
))) {
445 nasm_error(ERR_NONFATAL
, "`|' operator may only be applied to"
448 if (is_just_unknown(e
) || is_just_unknown(f
))
451 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
456 static expr
*expr1(int critical
)
465 i
= scan(scpriv
, tokval
);
469 if (!(is_simple(e
) || is_just_unknown(e
)) ||
470 !(is_simple(f
) || is_just_unknown(f
))) {
471 nasm_error(ERR_NONFATAL
, "`^' operator may only be applied to"
474 if (is_just_unknown(e
) || is_just_unknown(f
))
477 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
482 static expr
*expr2(int critical
)
491 i
= scan(scpriv
, tokval
);
495 if (!(is_simple(e
) || is_just_unknown(e
)) ||
496 !(is_simple(f
) || is_just_unknown(f
))) {
497 nasm_error(ERR_NONFATAL
, "`&' operator may only be applied to"
500 if (is_just_unknown(e
) || is_just_unknown(f
))
503 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
508 static expr
*expr3(int critical
)
516 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
518 i
= scan(scpriv
, tokval
);
522 if (!(is_simple(e
) || is_just_unknown(e
)) ||
523 !(is_simple(f
) || is_just_unknown(f
))) {
524 nasm_error(ERR_NONFATAL
, "shift operator may only be applied to"
526 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
531 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
534 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
542 static expr
*expr4(int critical
)
549 while (i
== '+' || i
== '-') {
551 i
= scan(scpriv
, tokval
);
557 e
= add_vectors(e
, f
);
560 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
567 static expr
*expr5(int critical
)
574 while (i
== '*' || i
== '/' || i
== '%' ||
575 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
577 i
= scan(scpriv
, tokval
);
581 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
582 !(is_simple(f
) || is_just_unknown(f
)))) {
583 nasm_error(ERR_NONFATAL
, "division operator may only be applied to"
587 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
588 nasm_error(ERR_NONFATAL
, "division by zero");
594 e
= scalar_mult(f
, reloc_value(e
), true);
595 else if (is_simple(f
))
596 e
= scalar_mult(e
, reloc_value(f
), true);
597 else if (is_just_unknown(e
) && is_just_unknown(f
))
600 nasm_error(ERR_NONFATAL
, "unable to multiply two "
601 "non-scalar objects");
606 if (is_just_unknown(e
) || is_just_unknown(f
))
609 e
= scalarvect(((uint64_t)reloc_value(e
)) /
610 ((uint64_t)reloc_value(f
)));
613 if (is_just_unknown(e
) || is_just_unknown(f
))
616 e
= scalarvect(((uint64_t)reloc_value(e
)) %
617 ((uint64_t)reloc_value(f
)));
620 if (is_just_unknown(e
) || is_just_unknown(f
))
623 e
= scalarvect(((int64_t)reloc_value(e
)) /
624 ((int64_t)reloc_value(f
)));
627 if (is_just_unknown(e
) || is_just_unknown(f
))
630 e
= scalarvect(((int64_t)reloc_value(e
)) %
631 ((int64_t)reloc_value(f
)));
638 static expr
*eval_floatize(enum floatize type
)
640 uint8_t result
[16], *p
; /* Up to 128 bits */
641 static const struct {
642 int bytes
, start
, len
;
644 { 1, 0, 1 }, /* FLOAT_8 */
645 { 2, 0, 2 }, /* FLOAT_16 */
646 { 4, 0, 4 }, /* FLOAT_32 */
647 { 8, 0, 8 }, /* FLOAT_64 */
648 { 10, 0, 8 }, /* FLOAT_80M */
649 { 10, 8, 2 }, /* FLOAT_80E */
650 { 16, 0, 8 }, /* FLOAT_128L */
651 { 16, 8, 8 }, /* FLOAT_128H */
657 i
= scan(scpriv
, tokval
);
659 nasm_error(ERR_NONFATAL
, "expecting `('");
662 i
= scan(scpriv
, tokval
);
663 if (i
== '-' || i
== '+') {
664 sign
= (i
== '-') ? -1 : 1;
665 i
= scan(scpriv
, tokval
);
667 if (i
!= TOKEN_FLOAT
) {
668 nasm_error(ERR_NONFATAL
, "expecting floating-point number");
671 if (!float_const(tokval
->t_charptr
, sign
, result
, formats
[type
].bytes
))
673 i
= scan(scpriv
, tokval
);
675 nasm_error(ERR_NONFATAL
, "expecting `)'");
679 p
= result
+formats
[type
].start
+formats
[type
].len
;
681 for (j
= formats
[type
].len
; j
; j
--) {
683 val
= (val
<< 8) + *p
;
687 addtotemp(EXPR_SIMPLE
, val
);
689 i
= scan(scpriv
, tokval
);
693 static expr
*eval_strfunc(enum strfunc type
)
698 bool parens
, rn_warn
;
701 i
= scan(scpriv
, tokval
);
704 i
= scan(scpriv
, tokval
);
706 if (i
!= TOKEN_STR
) {
707 nasm_error(ERR_NONFATAL
, "expecting string");
710 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
712 if (string_len
== (size_t)-1) {
713 nasm_error(ERR_NONFATAL
, "invalid string for transform");
717 val
= readstrnum(string
, string_len
, &rn_warn
);
719 i
= scan(scpriv
, tokval
);
721 nasm_error(ERR_NONFATAL
, "expecting `)'");
727 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
730 addtotemp(EXPR_SIMPLE
, val
);
732 i
= scan(scpriv
, tokval
);
736 static int64_t eval_ifunc(int64_t val
, enum ifunc func
)
739 uint64_t uval
= (uint64_t)val
;
745 errtype
= (func
== IFUNC_ILOG2E
) ? ERR_NONFATAL
: ERR_WARNING
;
747 if (!is_power2(uval
))
748 nasm_error(errtype
, "ilog2 argument is not a power of two");
755 rv
= (uval
< 2) ? 0 : ilog2_64(uval
-1) + 1;
759 nasm_panic(0, "invalid IFUNC token %d", func
);
767 static expr
*expr6(int critical
)
779 i
= scan(scpriv
, tokval
);
783 return scalar_mult(e
, -1L, false);
786 i
= scan(scpriv
, tokval
);
787 return expr6(critical
);
790 i
= scan(scpriv
, tokval
);
794 if (is_just_unknown(e
))
795 return unknown_expr();
796 else if (!is_simple(e
)) {
797 nasm_error(ERR_NONFATAL
, "`~' operator may only be applied to"
801 return scalarvect(~reloc_value(e
));
804 i
= scan(scpriv
, tokval
);
808 if (is_just_unknown(e
))
809 return unknown_expr();
810 else if (!is_simple(e
)) {
811 nasm_error(ERR_NONFATAL
, "`!' operator may only be applied to"
815 return scalarvect(!reloc_value(e
));
819 enum ifunc func
= tokval
->t_integer
;
820 i
= scan(scpriv
, tokval
);
824 if (is_just_unknown(e
))
825 return unknown_expr();
826 else if (!is_simple(e
)) {
827 nasm_error(ERR_NONFATAL
, "function may only be applied to"
831 return scalarvect(eval_ifunc(reloc_value(e
), func
));
835 i
= scan(scpriv
, tokval
);
842 if (is_unknown(e
) && critical
) {
843 nasm_error(ERR_NONFATAL
, "unable to determine segment base");
849 return eval_floatize(tokval
->t_integer
);
852 return eval_strfunc(tokval
->t_integer
);
855 i
= scan(scpriv
, tokval
);
860 nasm_error(ERR_NONFATAL
, "expecting `)'");
863 i
= scan(scpriv
, tokval
);
870 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
873 case TOKEN_DECORATOR
:
877 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
880 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
882 nasm_error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
883 addtotemp(EXPR_SIMPLE
, tmpval
);
886 addtotemp(tokval
->t_integer
, 1L);
887 if (hint
&& hint
->type
== EAH_NOHINT
)
888 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
895 * If !location->known, this indicates that no
896 * symbol, Here or Base references are valid because we
897 * are in preprocess-only mode.
899 if (!location
->known
) {
900 nasm_error(ERR_NONFATAL
,
901 "%s not supported in preprocess-only mode",
902 (i
== TOKEN_HERE
? "`$'" :
903 i
== TOKEN_BASE
? "`$$'" :
904 "symbol references"));
905 addtotemp(EXPR_UNKNOWN
, 1L);
909 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
910 if (i
== TOKEN_BASE
) {
911 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
913 } else if (i
== TOKEN_HERE
) {
914 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
915 label_ofs
= in_abs_seg
? abs_offset
: location
->offset
;
917 if (!labelfunc(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
918 scope
= local_scope(tokval
->t_charptr
);
920 nasm_error(ERR_NONFATAL
, "symbol `%s%s' undefined",
921 scope
,tokval
->t_charptr
);
923 } else if (critical
== 1) {
924 nasm_error(ERR_NONFATAL
,
925 "symbol `%s%s' not defined before use",
926 scope
,tokval
->t_charptr
);
930 *opflags
|= OPFLAG_FORWARD
;
936 if (opflags
&& is_extern(tokval
->t_charptr
))
937 *opflags
|= OPFLAG_EXTERN
;
939 addtotemp(type
, label_ofs
);
940 if (label_seg
!= NO_SEG
)
941 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
943 case TOKEN_DECORATOR
:
944 addtotemp(EXPR_RDSAE
, tokval
->t_integer
);
947 i
= scan(scpriv
, tokval
);
951 nasm_error(ERR_NONFATAL
, "expression syntax error");
956 void eval_global_info(struct ofmt
*output
, lfunc lookup_label
,
957 struct location
* locp
)
960 labelfunc
= lookup_label
;
964 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
965 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
);