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 efunc error
; /* Address of error reporting routine */
58 static lfunc labelfunc
; /* Address of label routine */
60 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
62 static expr
**tempexprs
= NULL
;
63 static int ntempexprs
;
64 static int tempexprs_size
= 0;
66 static expr
*tempexpr
;
68 static int tempexpr_size
;
70 static struct tokenval
*tokval
; /* The current token */
71 static int i
; /* The t_type of tokval */
74 static struct location
*location
; /* Pointer to current line's segment,offset */
77 static struct eval_hints
*hint
;
79 extern int in_abs_seg
; /* ABSOLUTE segment flag */
80 extern int32_t abs_seg
; /* ABSOLUTE segment */
81 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
84 * Unimportant cleanup is done to avoid confusing people who are trying
85 * to debug real memory leaks
87 void eval_cleanup(void)
90 nasm_free(tempexprs
[--ntempexprs
]);
95 * Construct a temporary expression.
97 static void begintemp(void)
100 tempexpr_size
= ntempexpr
= 0;
103 static void addtotemp(int32_t type
, int64_t value
)
105 while (ntempexpr
>= tempexpr_size
) {
106 tempexpr_size
+= TEMPEXPR_DELTA
;
107 tempexpr
= nasm_realloc(tempexpr
,
108 tempexpr_size
* sizeof(*tempexpr
));
110 tempexpr
[ntempexpr
].type
= type
;
111 tempexpr
[ntempexpr
++].value
= value
;
114 static expr
*finishtemp(void)
116 addtotemp(0L, 0L); /* terminate */
117 while (ntempexprs
>= tempexprs_size
) {
118 tempexprs_size
+= TEMPEXPRS_DELTA
;
119 tempexprs
= nasm_realloc(tempexprs
,
120 tempexprs_size
* sizeof(*tempexprs
));
122 return tempexprs
[ntempexprs
++] = tempexpr
;
126 * Add two vector datatypes. We have some bizarre behaviour on far-
127 * absolute segment types: we preserve them during addition _only_
128 * if one of the segments is a truly pure scalar.
130 static expr
*add_vectors(expr
* p
, expr
* q
)
134 preserve
= is_really_simple(p
) || is_really_simple(q
);
138 while (p
->type
&& q
->type
&&
139 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
140 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
143 if (p
->type
> q
->type
) {
144 addtotemp(q
->type
, q
->value
);
145 lasttype
= q
++->type
;
146 } else if (p
->type
< q
->type
) {
147 addtotemp(p
->type
, p
->value
);
148 lasttype
= p
++->type
;
149 } else { /* *p and *q have same type */
150 int64_t sum
= p
->value
+ q
->value
;
152 addtotemp(p
->type
, sum
);
154 hint
->type
= EAH_SUMMED
;
159 if (lasttype
== EXPR_UNKNOWN
) {
163 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
164 addtotemp(p
->type
, p
->value
);
167 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
168 addtotemp(q
->type
, q
->value
);
176 * Multiply a vector by a scalar. Strip far-absolute segment part
179 * Explicit treatment of UNKNOWN is not required in this routine,
180 * since it will silently do the Right Thing anyway.
182 * If `affect_hints' is set, we also change the hint type to
183 * NOTBASE if a MAKEBASE hint points at a register being
184 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
185 * as the base register.
187 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
191 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
192 p
->value
= scalar
* (p
->value
);
193 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
194 p
->type
== hint
->base
&& affect_hints
)
195 hint
->type
= EAH_NOTBASE
;
203 static expr
*scalarvect(int64_t scalar
)
206 addtotemp(EXPR_SIMPLE
, scalar
);
210 static expr
*unknown_expr(void)
213 addtotemp(EXPR_UNKNOWN
, 1L);
218 * The SEG operator: calculate the segment part of a relocatable
219 * value. Return NULL, as usual, if an error occurs. Report the
222 static expr
*segment_part(expr
* e
)
227 return unknown_expr();
230 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
236 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
238 } else if (seg
& SEG_ABS
) {
239 return scalarvect(seg
& ~SEG_ABS
);
240 } else if (seg
& 1) {
241 error(ERR_NONFATAL
, "SEG applied to something which"
242 " is already a segment base");
245 int32_t base
= outfmt
->segbase(seg
+ 1);
248 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
255 * Recursive-descent parser. Called with a single boolean operand,
256 * which is true if the evaluation is critical (i.e. unresolved
257 * symbols are an error condition). Must update the global `i' to
258 * reflect the token after the parsed string. May return NULL.
260 * evaluate() should report its own errors: on return it is assumed
261 * that if NULL has been returned, the error has already been
268 * expr : bexpr [ WRT expr6 ]
269 * bexpr : rexp0 or expr0 depending on relative-mode setting
270 * rexp0 : rexp1 [ {||} rexp1...]
271 * rexp1 : rexp2 [ {^^} rexp2...]
272 * rexp2 : rexp3 [ {&&} rexp3...]
273 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
274 * expr0 : expr1 [ {|} expr1...]
275 * expr1 : expr2 [ {^} expr2...]
276 * expr2 : expr3 [ {&} expr3...]
277 * expr3 : expr4 [ {<<,>>} expr4...]
278 * expr4 : expr5 [ {+,-} expr5...]
279 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
280 * expr6 : { ~,+,-,IFUNC,SEG } expr6
287 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
289 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
290 static expr
*expr4(int), *expr5(int), *expr6(int);
292 static expr
*(*bexpr
) (int);
294 static expr
*rexp0(int critical
)
302 while (i
== TOKEN_DBL_OR
) {
303 i
= scan(scpriv
, tokval
);
307 if (!(is_simple(e
) || is_just_unknown(e
)) ||
308 !(is_simple(f
) || is_just_unknown(f
))) {
309 error(ERR_NONFATAL
, "`|' operator may only be applied to"
313 if (is_just_unknown(e
) || is_just_unknown(f
))
316 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
321 static expr
*rexp1(int critical
)
329 while (i
== TOKEN_DBL_XOR
) {
330 i
= scan(scpriv
, tokval
);
334 if (!(is_simple(e
) || is_just_unknown(e
)) ||
335 !(is_simple(f
) || is_just_unknown(f
))) {
336 error(ERR_NONFATAL
, "`^' operator may only be applied to"
340 if (is_just_unknown(e
) || is_just_unknown(f
))
343 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
348 static expr
*rexp2(int critical
)
355 while (i
== TOKEN_DBL_AND
) {
356 i
= scan(scpriv
, tokval
);
360 if (!(is_simple(e
) || is_just_unknown(e
)) ||
361 !(is_simple(f
) || is_just_unknown(f
))) {
362 error(ERR_NONFATAL
, "`&' operator may only be applied to"
365 if (is_just_unknown(e
) || is_just_unknown(f
))
368 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
373 static expr
*rexp3(int critical
)
382 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
383 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
385 i
= scan(scpriv
, tokval
);
390 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
396 v
= -1; /* means unknown */
397 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
398 v
= (j
== TOKEN_NE
); /* unequal, so return true if NE */
400 v
= (j
== TOKEN_EQ
); /* equal, so return true if EQ */
404 v
= -1; /* means unknown */
405 else if (!is_really_simple(e
)) {
407 "`%s': operands differ by a non-scalar",
408 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
409 TOKEN_GE
? ">=" : ">"));
410 v
= 0; /* must set it to _something_ */
412 int64_t vv
= reloc_value(e
);
414 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
416 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
418 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
431 static expr
*expr0(int critical
)
440 i
= scan(scpriv
, tokval
);
444 if (!(is_simple(e
) || is_just_unknown(e
)) ||
445 !(is_simple(f
) || is_just_unknown(f
))) {
446 error(ERR_NONFATAL
, "`|' operator may only be applied to"
449 if (is_just_unknown(e
) || is_just_unknown(f
))
452 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
457 static expr
*expr1(int critical
)
466 i
= scan(scpriv
, tokval
);
470 if (!(is_simple(e
) || is_just_unknown(e
)) ||
471 !(is_simple(f
) || is_just_unknown(f
))) {
472 error(ERR_NONFATAL
, "`^' operator may only be applied to"
475 if (is_just_unknown(e
) || is_just_unknown(f
))
478 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
483 static expr
*expr2(int critical
)
492 i
= scan(scpriv
, tokval
);
496 if (!(is_simple(e
) || is_just_unknown(e
)) ||
497 !(is_simple(f
) || is_just_unknown(f
))) {
498 error(ERR_NONFATAL
, "`&' operator may only be applied to"
501 if (is_just_unknown(e
) || is_just_unknown(f
))
504 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
509 static expr
*expr3(int critical
)
517 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
519 i
= scan(scpriv
, tokval
);
523 if (!(is_simple(e
) || is_just_unknown(e
)) ||
524 !(is_simple(f
) || is_just_unknown(f
))) {
525 error(ERR_NONFATAL
, "shift operator may only be applied to"
527 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
532 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
535 e
= scalarvect(((uint64_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 error(ERR_NONFATAL
, "division operator may only be applied to"
588 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
589 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 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 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 error(ERR_NONFATAL
, "expecting floating-point number");
672 if (!float_const(tokval
->t_charptr
, sign
, result
,
673 formats
[type
].bytes
, error
))
675 i
= scan(scpriv
, tokval
);
677 error(ERR_NONFATAL
, "expecting `)'");
681 p
= result
+formats
[type
].start
+formats
[type
].len
;
683 for (j
= formats
[type
].len
; j
; j
--) {
685 val
= (val
<< 8) + *p
;
689 addtotemp(EXPR_SIMPLE
, val
);
691 i
= scan(scpriv
, tokval
);
695 static expr
*eval_strfunc(enum strfunc type
)
700 bool parens
, rn_warn
;
703 i
= scan(scpriv
, tokval
);
706 i
= scan(scpriv
, tokval
);
708 if (i
!= TOKEN_STR
) {
709 error(ERR_NONFATAL
, "expecting string");
712 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
714 if (string_len
== (size_t)-1) {
715 error(ERR_NONFATAL
, "invalid string for transform");
719 val
= readstrnum(string
, string_len
, &rn_warn
);
721 i
= scan(scpriv
, tokval
);
723 error(ERR_NONFATAL
, "expecting `)'");
729 error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
732 addtotemp(EXPR_SIMPLE
, val
);
734 i
= scan(scpriv
, tokval
);
738 static int64_t eval_ifunc(int64_t val
, enum ifunc func
)
741 uint64_t uval
= (uint64_t)val
;
747 errtype
= (func
== IFUNC_ILOG2E
) ? ERR_NONFATAL
: ERR_WARNING
;
749 if (!is_power2(uval
))
750 error(errtype
, "ilog2 argument is not a power of two");
757 rv
= (uval
< 2) ? 0 : ilog2_64(uval
-1) + 1;
761 error(ERR_PANIC
, "invalid IFUNC token %d", func
);
769 static expr
*expr6(int critical
)
781 i
= scan(scpriv
, tokval
);
785 return scalar_mult(e
, -1L, false);
788 i
= scan(scpriv
, tokval
);
789 return expr6(critical
);
792 i
= scan(scpriv
, tokval
);
796 if (is_just_unknown(e
))
797 return unknown_expr();
798 else if (!is_simple(e
)) {
799 error(ERR_NONFATAL
, "`~' operator may only be applied to"
803 return scalarvect(~reloc_value(e
));
806 i
= scan(scpriv
, tokval
);
810 if (is_just_unknown(e
))
811 return unknown_expr();
812 else if (!is_simple(e
)) {
813 error(ERR_NONFATAL
, "`!' operator may only be applied to"
817 return scalarvect(!reloc_value(e
));
821 enum ifunc func
= tokval
->t_integer
;
822 i
= scan(scpriv
, tokval
);
826 if (is_just_unknown(e
))
827 return unknown_expr();
828 else if (!is_simple(e
)) {
829 error(ERR_NONFATAL
, "function may only be applied to"
833 return scalarvect(eval_ifunc(reloc_value(e
), func
));
837 i
= scan(scpriv
, tokval
);
844 if (is_unknown(e
) && critical
) {
845 error(ERR_NONFATAL
, "unable to determine segment base");
851 return eval_floatize(tokval
->t_integer
);
854 return eval_strfunc(tokval
->t_integer
);
857 i
= scan(scpriv
, tokval
);
862 error(ERR_NONFATAL
, "expecting `)'");
865 i
= scan(scpriv
, tokval
);
872 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
875 case TOKEN_DECORATOR
:
879 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
882 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
884 error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
885 addtotemp(EXPR_SIMPLE
, tmpval
);
888 addtotemp(tokval
->t_integer
, 1L);
889 if (hint
&& hint
->type
== EAH_NOHINT
)
890 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
897 * If !location->known, this indicates that no
898 * symbol, Here or Base references are valid because we
899 * are in preprocess-only mode.
901 if (!location
->known
) {
903 "%s not supported in preprocess-only mode",
904 (i
== TOKEN_HERE
? "`$'" :
905 i
== TOKEN_BASE
? "`$$'" :
906 "symbol references"));
907 addtotemp(EXPR_UNKNOWN
, 1L);
911 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
912 if (i
== TOKEN_BASE
) {
913 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
915 } else if (i
== TOKEN_HERE
) {
916 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
917 label_ofs
= in_abs_seg
? abs_offset
: location
->offset
;
919 if (!labelfunc(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
920 scope
= local_scope(tokval
->t_charptr
);
922 error(ERR_NONFATAL
, "symbol `%s%s' undefined",
923 scope
,tokval
->t_charptr
);
925 } else if (critical
== 1) {
927 "symbol `%s%s' not defined before use",
928 scope
,tokval
->t_charptr
);
932 *opflags
|= OPFLAG_FORWARD
;
938 if (opflags
&& is_extern(tokval
->t_charptr
))
939 *opflags
|= OPFLAG_EXTERN
;
941 addtotemp(type
, label_ofs
);
942 if (label_seg
!= NO_SEG
)
943 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
945 case TOKEN_DECORATOR
:
946 addtotemp(EXPR_RDSAE
, tokval
->t_integer
);
949 i
= scan(scpriv
, tokval
);
953 error(ERR_NONFATAL
, "expression syntax error");
958 void eval_global_info(struct ofmt
*output
, lfunc lookup_label
,
959 struct location
* locp
)
962 labelfunc
= lookup_label
;
966 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
967 int *fwref
, int critical
, efunc report_error
,
968 struct eval_hints
*hints
)
975 hint
->type
= EAH_NOHINT
;
977 if (critical
& CRITICAL
) {
978 critical
&= ~CRITICAL
;
986 error
= report_error
;
989 if (tokval
->t_type
== TOKEN_INVALID
)
990 i
= scan(scpriv
, tokval
);
994 while (ntempexprs
) /* initialize temporary storage */
995 nasm_free(tempexprs
[--ntempexprs
]);
1001 if (i
== TOKEN_WRT
) {
1002 i
= scan(scpriv
, tokval
); /* eat the WRT */
1003 f
= expr6(critical
);
1007 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
1010 if (is_just_unknown(f
))
1016 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
1019 value
= reloc_seg(f
);
1020 if (value
== NO_SEG
)
1021 value
= reloc_value(f
) | SEG_ABS
;
1022 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
1023 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
1026 addtotemp(EXPR_WRT
, value
);
1029 e
= add_vectors(e
, g
);