1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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
);
156 if (lasttype
== EXPR_UNKNOWN
) {
160 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
161 addtotemp(p
->type
, p
->value
);
164 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
165 addtotemp(q
->type
, q
->value
);
173 * Multiply a vector by a scalar. Strip far-absolute segment part
176 * Explicit treatment of UNKNOWN is not required in this routine,
177 * since it will silently do the Right Thing anyway.
179 * If `affect_hints' is set, we also change the hint type to
180 * NOTBASE if a MAKEBASE hint points at a register being
181 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
182 * as the base register.
184 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
188 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
189 p
->value
= scalar
* (p
->value
);
190 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
191 p
->type
== hint
->base
&& affect_hints
)
192 hint
->type
= EAH_NOTBASE
;
200 static expr
*scalarvect(int64_t scalar
)
203 addtotemp(EXPR_SIMPLE
, scalar
);
207 static expr
*unknown_expr(void)
210 addtotemp(EXPR_UNKNOWN
, 1L);
215 * The SEG operator: calculate the segment part of a relocatable
216 * value. Return NULL, as usual, if an error occurs. Report the
219 static expr
*segment_part(expr
* e
)
224 return unknown_expr();
227 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
233 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
235 } else if (seg
& SEG_ABS
) {
236 return scalarvect(seg
& ~SEG_ABS
);
237 } else if (seg
& 1) {
238 error(ERR_NONFATAL
, "SEG applied to something which"
239 " is already a segment base");
242 int32_t base
= outfmt
->segbase(seg
+ 1);
245 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
252 * Recursive-descent parser. Called with a single boolean operand,
253 * which is true if the evaluation is critical (i.e. unresolved
254 * symbols are an error condition). Must update the global `i' to
255 * reflect the token after the parsed string. May return NULL.
257 * evaluate() should report its own errors: on return it is assumed
258 * that if NULL has been returned, the error has already been
265 * expr : bexpr [ WRT expr6 ]
266 * bexpr : rexp0 or expr0 depending on relative-mode setting
267 * rexp0 : rexp1 [ {||} rexp1...]
268 * rexp1 : rexp2 [ {^^} rexp2...]
269 * rexp2 : rexp3 [ {&&} rexp3...]
270 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
271 * expr0 : expr1 [ {|} expr1...]
272 * expr1 : expr2 [ {^} expr2...]
273 * expr2 : expr3 [ {&} expr3...]
274 * expr3 : expr4 [ {<<,>>} expr4...]
275 * expr4 : expr5 [ {+,-} expr5...]
276 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
277 * expr6 : { ~,+,-,SEG } expr6
284 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
286 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
287 static expr
*expr4(int), *expr5(int), *expr6(int);
289 static expr
*(*bexpr
) (int);
291 static expr
*rexp0(int critical
)
299 while (i
== TOKEN_DBL_OR
) {
300 i
= scan(scpriv
, tokval
);
304 if (!(is_simple(e
) || is_just_unknown(e
)) ||
305 !(is_simple(f
) || is_just_unknown(f
))) {
306 error(ERR_NONFATAL
, "`|' operator may only be applied to"
310 if (is_just_unknown(e
) || is_just_unknown(f
))
313 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
318 static expr
*rexp1(int critical
)
326 while (i
== TOKEN_DBL_XOR
) {
327 i
= scan(scpriv
, tokval
);
331 if (!(is_simple(e
) || is_just_unknown(e
)) ||
332 !(is_simple(f
) || is_just_unknown(f
))) {
333 error(ERR_NONFATAL
, "`^' operator may only be applied to"
337 if (is_just_unknown(e
) || is_just_unknown(f
))
340 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
345 static expr
*rexp2(int critical
)
352 while (i
== TOKEN_DBL_AND
) {
353 i
= scan(scpriv
, tokval
);
357 if (!(is_simple(e
) || is_just_unknown(e
)) ||
358 !(is_simple(f
) || is_just_unknown(f
))) {
359 error(ERR_NONFATAL
, "`&' operator may only be applied to"
362 if (is_just_unknown(e
) || is_just_unknown(f
))
365 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
370 static expr
*rexp3(int critical
)
379 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
380 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
382 i
= scan(scpriv
, tokval
);
387 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
393 v
= -1; /* means unknown */
394 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
395 v
= (j
== TOKEN_NE
); /* unequal, so return true if NE */
397 v
= (j
== TOKEN_EQ
); /* equal, so return true if EQ */
401 v
= -1; /* means unknown */
402 else if (!is_really_simple(e
)) {
404 "`%s': operands differ by a non-scalar",
405 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
406 TOKEN_GE
? ">=" : ">"));
407 v
= 0; /* must set it to _something_ */
409 int vv
= reloc_value(e
);
411 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
413 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
415 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
428 static expr
*expr0(int critical
)
437 i
= scan(scpriv
, tokval
);
441 if (!(is_simple(e
) || is_just_unknown(e
)) ||
442 !(is_simple(f
) || is_just_unknown(f
))) {
443 error(ERR_NONFATAL
, "`|' operator may only be applied to"
446 if (is_just_unknown(e
) || is_just_unknown(f
))
449 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
454 static expr
*expr1(int critical
)
463 i
= scan(scpriv
, tokval
);
467 if (!(is_simple(e
) || is_just_unknown(e
)) ||
468 !(is_simple(f
) || is_just_unknown(f
))) {
469 error(ERR_NONFATAL
, "`^' operator may only be applied to"
472 if (is_just_unknown(e
) || is_just_unknown(f
))
475 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
480 static expr
*expr2(int critical
)
489 i
= scan(scpriv
, tokval
);
493 if (!(is_simple(e
) || is_just_unknown(e
)) ||
494 !(is_simple(f
) || is_just_unknown(f
))) {
495 error(ERR_NONFATAL
, "`&' operator may only be applied to"
498 if (is_just_unknown(e
) || is_just_unknown(f
))
501 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
506 static expr
*expr3(int critical
)
514 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
516 i
= scan(scpriv
, tokval
);
520 if (!(is_simple(e
) || is_just_unknown(e
)) ||
521 !(is_simple(f
) || is_just_unknown(f
))) {
522 error(ERR_NONFATAL
, "shift operator may only be applied to"
524 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
529 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
532 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
540 static expr
*expr4(int critical
)
547 while (i
== '+' || i
== '-') {
549 i
= scan(scpriv
, tokval
);
555 e
= add_vectors(e
, f
);
558 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
565 static expr
*expr5(int critical
)
572 while (i
== '*' || i
== '/' || i
== '%' ||
573 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
575 i
= scan(scpriv
, tokval
);
579 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
580 !(is_simple(f
) || is_just_unknown(f
)))) {
581 error(ERR_NONFATAL
, "division operator may only be applied to"
585 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
586 error(ERR_NONFATAL
, "division by zero");
592 e
= scalar_mult(f
, reloc_value(e
), true);
593 else if (is_simple(f
))
594 e
= scalar_mult(e
, reloc_value(f
), true);
595 else if (is_just_unknown(e
) && is_just_unknown(f
))
598 error(ERR_NONFATAL
, "unable to multiply two "
599 "non-scalar objects");
604 if (is_just_unknown(e
) || is_just_unknown(f
))
607 e
= scalarvect(((uint64_t)reloc_value(e
)) /
608 ((uint64_t)reloc_value(f
)));
611 if (is_just_unknown(e
) || is_just_unknown(f
))
614 e
= scalarvect(((uint64_t)reloc_value(e
)) %
615 ((uint64_t)reloc_value(f
)));
618 if (is_just_unknown(e
) || is_just_unknown(f
))
621 e
= scalarvect(((int64_t)reloc_value(e
)) /
622 ((int64_t)reloc_value(f
)));
625 if (is_just_unknown(e
) || is_just_unknown(f
))
628 e
= scalarvect(((int64_t)reloc_value(e
)) %
629 ((int64_t)reloc_value(f
)));
636 static expr
*eval_floatize(enum floatize type
)
638 uint8_t result
[16], *p
; /* Up to 128 bits */
639 static const struct {
640 int bytes
, start
, len
;
642 { 1, 0, 1 }, /* FLOAT_8 */
643 { 2, 0, 2 }, /* FLOAT_16 */
644 { 4, 0, 4 }, /* FLOAT_32 */
645 { 8, 0, 8 }, /* FLOAT_64 */
646 { 10, 0, 8 }, /* FLOAT_80M */
647 { 10, 8, 2 }, /* FLOAT_80E */
648 { 16, 0, 8 }, /* FLOAT_128L */
649 { 16, 8, 8 }, /* FLOAT_128H */
655 i
= scan(scpriv
, tokval
);
657 error(ERR_NONFATAL
, "expecting `('");
660 i
= scan(scpriv
, tokval
);
661 if (i
== '-' || i
== '+') {
662 sign
= (i
== '-') ? -1 : 1;
663 i
= scan(scpriv
, tokval
);
665 if (i
!= TOKEN_FLOAT
) {
666 error(ERR_NONFATAL
, "expecting floating-point number");
669 if (!float_const(tokval
->t_charptr
, sign
, result
,
670 formats
[type
].bytes
, error
))
672 i
= scan(scpriv
, tokval
);
674 error(ERR_NONFATAL
, "expecting `)'");
678 p
= result
+formats
[type
].start
+formats
[type
].len
;
680 for (j
= formats
[type
].len
; j
; j
--) {
682 val
= (val
<< 8) + *p
;
686 addtotemp(EXPR_SIMPLE
, val
);
688 i
= scan(scpriv
, tokval
);
692 static expr
*eval_strfunc(enum strfunc type
)
697 bool parens
, rn_warn
;
700 i
= scan(scpriv
, tokval
);
703 i
= scan(scpriv
, tokval
);
705 if (i
!= TOKEN_STR
) {
706 error(ERR_NONFATAL
, "expecting string");
709 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
711 if (string_len
== (size_t)-1) {
712 error(ERR_NONFATAL
, "invalid string for transform");
716 val
= readstrnum(string
, string_len
, &rn_warn
);
718 i
= scan(scpriv
, tokval
);
720 error(ERR_NONFATAL
, "expecting `)'");
726 error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
729 addtotemp(EXPR_SIMPLE
, val
);
731 i
= scan(scpriv
, tokval
);
735 static expr
*expr6(int critical
)
747 i
= scan(scpriv
, tokval
);
751 return scalar_mult(e
, -1L, false);
754 i
= scan(scpriv
, tokval
);
755 return expr6(critical
);
758 i
= scan(scpriv
, tokval
);
762 if (is_just_unknown(e
))
763 return unknown_expr();
764 else if (!is_simple(e
)) {
765 error(ERR_NONFATAL
, "`~' operator may only be applied to"
769 return scalarvect(~reloc_value(e
));
772 i
= scan(scpriv
, tokval
);
776 if (is_just_unknown(e
))
777 return unknown_expr();
778 else if (!is_simple(e
)) {
779 error(ERR_NONFATAL
, "`!' operator may only be applied to"
783 return scalarvect(!reloc_value(e
));
786 i
= scan(scpriv
, tokval
);
793 if (is_unknown(e
) && critical
) {
794 error(ERR_NONFATAL
, "unable to determine segment base");
800 return eval_floatize(tokval
->t_integer
);
803 return eval_strfunc(tokval
->t_integer
);
806 i
= scan(scpriv
, tokval
);
811 error(ERR_NONFATAL
, "expecting `)'");
814 i
= scan(scpriv
, tokval
);
821 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
827 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
830 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
832 error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
833 addtotemp(EXPR_SIMPLE
, tmpval
);
836 addtotemp(tokval
->t_integer
, 1L);
837 if (hint
&& hint
->type
== EAH_NOHINT
)
838 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
845 * If !location->known, this indicates that no
846 * symbol, Here or Base references are valid because we
847 * are in preprocess-only mode.
849 if (!location
->known
) {
851 "%s not supported in preprocess-only mode",
852 (i
== TOKEN_HERE
? "`$'" :
853 i
== TOKEN_BASE
? "`$$'" :
854 "symbol references"));
855 addtotemp(EXPR_UNKNOWN
, 1L);
859 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
860 if (i
== TOKEN_BASE
) {
861 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
863 } else if (i
== TOKEN_HERE
) {
864 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
865 label_ofs
= in_abs_seg
? abs_offset
: location
->offset
;
867 if (!labelfunc(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
868 scope
= local_scope(tokval
->t_charptr
);
870 error(ERR_NONFATAL
, "symbol `%s%s' undefined",
871 scope
,tokval
->t_charptr
);
873 } else if (critical
== 1) {
875 "symbol `%s%s' not defined before use",
876 scope
,tokval
->t_charptr
);
886 if (opflags
&& is_extern(tokval
->t_charptr
))
887 *opflags
|= OPFLAG_EXTERN
;
889 addtotemp(type
, label_ofs
);
890 if (label_seg
!= NO_SEG
)
891 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
894 i
= scan(scpriv
, tokval
);
898 error(ERR_NONFATAL
, "expression syntax error");
903 void eval_global_info(struct ofmt
*output
, lfunc lookup_label
,
904 struct location
* locp
)
907 labelfunc
= lookup_label
;
911 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
912 int *fwref
, int critical
, efunc report_error
,
913 struct eval_hints
*hints
)
920 hint
->type
= EAH_NOHINT
;
922 if (critical
& CRITICAL
) {
923 critical
&= ~CRITICAL
;
931 error
= report_error
;
934 if (tokval
->t_type
== TOKEN_INVALID
)
935 i
= scan(scpriv
, tokval
);
939 while (ntempexprs
) /* initialize temporary storage */
940 nasm_free(tempexprs
[--ntempexprs
]);
946 if (i
== TOKEN_WRT
) {
947 i
= scan(scpriv
, tokval
); /* eat the WRT */
952 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
955 if (is_just_unknown(f
))
961 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
964 value
= reloc_seg(f
);
966 value
= reloc_value(f
) | SEG_ABS
;
967 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
968 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
971 addtotemp(EXPR_WRT
, value
);
974 e
= add_vectors(e
, g
);