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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * eval.c expression evaluator for the Netwide Assembler
65 #define TEMPEXPRS_DELTA 128
66 #define TEMPEXPR_DELTA 8
68 static scanner scan
; /* Address of scanner routine */
69 static efunc error
; /* Address of error reporting routine */
70 static lfunc labelfunc
; /* Address of label routine */
72 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
74 static expr
**tempexprs
= NULL
;
75 static int ntempexprs
;
76 static int tempexprs_size
= 0;
78 static expr
*tempexpr
;
80 static int tempexpr_size
;
82 static struct tokenval
*tokval
; /* The current token */
83 static int i
; /* The t_type of tokval */
86 static struct location
*location
; /* Pointer to current line's segment,offset */
89 static struct eval_hints
*hint
;
91 extern int in_abs_seg
; /* ABSOLUTE segment flag */
92 extern int32_t abs_seg
; /* ABSOLUTE segment */
93 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
96 * Unimportant cleanup is done to avoid confusing people who are trying
97 * to debug real memory leaks
99 void eval_cleanup(void)
102 nasm_free(tempexprs
[--ntempexprs
]);
103 nasm_free(tempexprs
);
107 * Construct a temporary expression.
109 static void begintemp(void)
112 tempexpr_size
= ntempexpr
= 0;
115 static void addtotemp(int32_t type
, int64_t value
)
117 while (ntempexpr
>= tempexpr_size
) {
118 tempexpr_size
+= TEMPEXPR_DELTA
;
119 tempexpr
= nasm_realloc(tempexpr
,
120 tempexpr_size
* sizeof(*tempexpr
));
122 tempexpr
[ntempexpr
].type
= type
;
123 tempexpr
[ntempexpr
++].value
= value
;
126 static expr
*finishtemp(void)
128 addtotemp(0L, 0L); /* terminate */
129 while (ntempexprs
>= tempexprs_size
) {
130 tempexprs_size
+= TEMPEXPRS_DELTA
;
131 tempexprs
= nasm_realloc(tempexprs
,
132 tempexprs_size
* sizeof(*tempexprs
));
134 return tempexprs
[ntempexprs
++] = tempexpr
;
138 * Add two vector datatypes. We have some bizarre behaviour on far-
139 * absolute segment types: we preserve them during addition _only_
140 * if one of the segments is a truly pure scalar.
142 static expr
*add_vectors(expr
* p
, expr
* q
)
146 preserve
= is_really_simple(p
) || is_really_simple(q
);
150 while (p
->type
&& q
->type
&&
151 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
152 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
155 if (p
->type
> q
->type
) {
156 addtotemp(q
->type
, q
->value
);
157 lasttype
= q
++->type
;
158 } else if (p
->type
< q
->type
) {
159 addtotemp(p
->type
, p
->value
);
160 lasttype
= p
++->type
;
161 } else { /* *p and *q have same type */
162 int64_t sum
= p
->value
+ q
->value
;
164 addtotemp(p
->type
, sum
);
168 if (lasttype
== EXPR_UNKNOWN
) {
172 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
173 addtotemp(p
->type
, p
->value
);
176 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
177 addtotemp(q
->type
, q
->value
);
185 * Multiply a vector by a scalar. Strip far-absolute segment part
188 * Explicit treatment of UNKNOWN is not required in this routine,
189 * since it will silently do the Right Thing anyway.
191 * If `affect_hints' is set, we also change the hint type to
192 * NOTBASE if a MAKEBASE hint points at a register being
193 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
194 * as the base register.
196 static expr
*scalar_mult(expr
* vect
, int64_t scalar
, int affect_hints
)
200 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
201 p
->value
= scalar
* (p
->value
);
202 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
203 p
->type
== hint
->base
&& affect_hints
)
204 hint
->type
= EAH_NOTBASE
;
212 static expr
*scalarvect(int64_t scalar
)
215 addtotemp(EXPR_SIMPLE
, scalar
);
219 static expr
*unknown_expr(void)
222 addtotemp(EXPR_UNKNOWN
, 1L);
227 * The SEG operator: calculate the segment part of a relocatable
228 * value. Return NULL, as usual, if an error occurs. Report the
231 static expr
*segment_part(expr
* e
)
236 return unknown_expr();
239 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
245 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
247 } else if (seg
& SEG_ABS
) {
248 return scalarvect(seg
& ~SEG_ABS
);
249 } else if (seg
& 1) {
250 error(ERR_NONFATAL
, "SEG applied to something which"
251 " is already a segment base");
254 int32_t base
= outfmt
->segbase(seg
+ 1);
257 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
264 * Recursive-descent parser. Called with a single boolean operand,
265 * which is true if the evaluation is critical (i.e. unresolved
266 * symbols are an error condition). Must update the global `i' to
267 * reflect the token after the parsed string. May return NULL.
269 * evaluate() should report its own errors: on return it is assumed
270 * that if NULL has been returned, the error has already been
277 * expr : bexpr [ WRT expr6 ]
278 * bexpr : rexp0 or expr0 depending on relative-mode setting
279 * rexp0 : rexp1 [ {||} rexp1...]
280 * rexp1 : rexp2 [ {^^} rexp2...]
281 * rexp2 : rexp3 [ {&&} rexp3...]
282 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
283 * expr0 : expr1 [ {|} expr1...]
284 * expr1 : expr2 [ {^} expr2...]
285 * expr2 : expr3 [ {&} expr3...]
286 * expr3 : expr4 [ {<<,>>} expr4...]
287 * expr4 : expr5 [ {+,-} expr5...]
288 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
289 * expr6 : { ~,+,-,SEG } expr6
296 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
298 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
299 static expr
*expr4(int), *expr5(int), *expr6(int);
301 static expr
*(*bexpr
) (int);
303 static expr
*rexp0(int critical
)
311 while (i
== TOKEN_DBL_OR
) {
312 i
= scan(scpriv
, tokval
);
316 if (!(is_simple(e
) || is_just_unknown(e
)) ||
317 !(is_simple(f
) || is_just_unknown(f
))) {
318 error(ERR_NONFATAL
, "`|' operator may only be applied to"
322 if (is_just_unknown(e
) || is_just_unknown(f
))
325 e
= scalarvect((int64_t)(reloc_value(e
) || reloc_value(f
)));
330 static expr
*rexp1(int critical
)
338 while (i
== TOKEN_DBL_XOR
) {
339 i
= scan(scpriv
, tokval
);
343 if (!(is_simple(e
) || is_just_unknown(e
)) ||
344 !(is_simple(f
) || is_just_unknown(f
))) {
345 error(ERR_NONFATAL
, "`^' operator may only be applied to"
349 if (is_just_unknown(e
) || is_just_unknown(f
))
352 e
= scalarvect((int64_t)(!reloc_value(e
) ^ !reloc_value(f
)));
357 static expr
*rexp2(int critical
)
364 while (i
== TOKEN_DBL_AND
) {
365 i
= scan(scpriv
, tokval
);
369 if (!(is_simple(e
) || is_just_unknown(e
)) ||
370 !(is_simple(f
) || is_just_unknown(f
))) {
371 error(ERR_NONFATAL
, "`&' operator may only be applied to"
374 if (is_just_unknown(e
) || is_just_unknown(f
))
377 e
= scalarvect((int64_t)(reloc_value(e
) && reloc_value(f
)));
382 static expr
*rexp3(int critical
)
391 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
392 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
394 i
= scan(scpriv
, tokval
);
399 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
405 v
= -1; /* means unknown */
406 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
407 v
= (j
== TOKEN_NE
); /* unequal, so return true if NE */
409 v
= (j
== TOKEN_EQ
); /* equal, so return true if EQ */
413 v
= -1; /* means unknown */
414 else if (!is_really_simple(e
)) {
416 "`%s': operands differ by a non-scalar",
417 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
418 TOKEN_GE
? ">=" : ">"));
419 v
= 0; /* must set it to _something_ */
421 int vv
= reloc_value(e
);
423 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
425 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
427 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
440 static expr
*expr0(int critical
)
449 i
= scan(scpriv
, tokval
);
453 if (!(is_simple(e
) || is_just_unknown(e
)) ||
454 !(is_simple(f
) || is_just_unknown(f
))) {
455 error(ERR_NONFATAL
, "`|' operator may only be applied to"
458 if (is_just_unknown(e
) || is_just_unknown(f
))
461 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
466 static expr
*expr1(int critical
)
475 i
= scan(scpriv
, tokval
);
479 if (!(is_simple(e
) || is_just_unknown(e
)) ||
480 !(is_simple(f
) || is_just_unknown(f
))) {
481 error(ERR_NONFATAL
, "`^' operator may only be applied to"
484 if (is_just_unknown(e
) || is_just_unknown(f
))
487 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
492 static expr
*expr2(int critical
)
501 i
= scan(scpriv
, tokval
);
505 if (!(is_simple(e
) || is_just_unknown(e
)) ||
506 !(is_simple(f
) || is_just_unknown(f
))) {
507 error(ERR_NONFATAL
, "`&' operator may only be applied to"
510 if (is_just_unknown(e
) || is_just_unknown(f
))
513 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
518 static expr
*expr3(int critical
)
526 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
528 i
= scan(scpriv
, tokval
);
532 if (!(is_simple(e
) || is_just_unknown(e
)) ||
533 !(is_simple(f
) || is_just_unknown(f
))) {
534 error(ERR_NONFATAL
, "shift operator may only be applied to"
536 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
541 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
544 e
= scalarvect(((uint64_t)reloc_value(e
)) >>
552 static expr
*expr4(int critical
)
559 while (i
== '+' || i
== '-') {
561 i
= scan(scpriv
, tokval
);
567 e
= add_vectors(e
, f
);
570 e
= add_vectors(e
, scalar_mult(f
, -1L, false));
577 static expr
*expr5(int critical
)
584 while (i
== '*' || i
== '/' || i
== '%' ||
585 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
587 i
= scan(scpriv
, tokval
);
591 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
592 !(is_simple(f
) || is_just_unknown(f
)))) {
593 error(ERR_NONFATAL
, "division operator may only be applied to"
597 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
598 error(ERR_NONFATAL
, "division by zero");
604 e
= scalar_mult(f
, reloc_value(e
), true);
605 else if (is_simple(f
))
606 e
= scalar_mult(e
, reloc_value(f
), true);
607 else if (is_just_unknown(e
) && is_just_unknown(f
))
610 error(ERR_NONFATAL
, "unable to multiply two "
611 "non-scalar objects");
616 if (is_just_unknown(e
) || is_just_unknown(f
))
619 e
= scalarvect(((uint64_t)reloc_value(e
)) /
620 ((uint64_t)reloc_value(f
)));
623 if (is_just_unknown(e
) || is_just_unknown(f
))
626 e
= scalarvect(((uint64_t)reloc_value(e
)) %
627 ((uint64_t)reloc_value(f
)));
630 if (is_just_unknown(e
) || is_just_unknown(f
))
633 e
= scalarvect(((int64_t)reloc_value(e
)) /
634 ((int64_t)reloc_value(f
)));
637 if (is_just_unknown(e
) || is_just_unknown(f
))
640 e
= scalarvect(((int64_t)reloc_value(e
)) %
641 ((int64_t)reloc_value(f
)));
648 static expr
*eval_floatize(enum floatize type
)
650 uint8_t result
[16], *p
; /* Up to 128 bits */
651 static const struct {
652 int bytes
, start
, len
;
654 { 1, 0, 1 }, /* FLOAT_8 */
655 { 2, 0, 2 }, /* FLOAT_16 */
656 { 4, 0, 4 }, /* FLOAT_32 */
657 { 8, 0, 8 }, /* FLOAT_64 */
658 { 10, 0, 8 }, /* FLOAT_80M */
659 { 10, 8, 2 }, /* FLOAT_80E */
660 { 16, 0, 8 }, /* FLOAT_128L */
661 { 16, 8, 8 }, /* FLOAT_128H */
667 i
= scan(scpriv
, tokval
);
669 error(ERR_NONFATAL
, "expecting `('");
672 i
= scan(scpriv
, tokval
);
673 if (i
== '-' || i
== '+') {
674 sign
= (i
== '-') ? -1 : 1;
675 i
= scan(scpriv
, tokval
);
677 if (i
!= TOKEN_FLOAT
) {
678 error(ERR_NONFATAL
, "expecting floating-point number");
681 if (!float_const(tokval
->t_charptr
, sign
, result
,
682 formats
[type
].bytes
, error
))
684 i
= scan(scpriv
, tokval
);
686 error(ERR_NONFATAL
, "expecting `)'");
690 p
= result
+formats
[type
].start
+formats
[type
].len
;
692 for (j
= formats
[type
].len
; j
; j
--) {
694 val
= (val
<< 8) + *p
;
698 addtotemp(EXPR_SIMPLE
, val
);
700 i
= scan(scpriv
, tokval
);
704 static expr
*eval_strfunc(enum strfunc type
)
709 bool parens
, rn_warn
;
712 i
= scan(scpriv
, tokval
);
715 i
= scan(scpriv
, tokval
);
717 if (i
!= TOKEN_STR
) {
718 error(ERR_NONFATAL
, "expecting string");
721 string_len
= string_transform(tokval
->t_charptr
, tokval
->t_inttwo
,
723 if (string_len
== (size_t)-1) {
724 error(ERR_NONFATAL
, "invalid string for transform");
728 val
= readstrnum(string
, string_len
, &rn_warn
);
730 i
= scan(scpriv
, tokval
);
732 error(ERR_NONFATAL
, "expecting `)'");
738 error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
741 addtotemp(EXPR_SIMPLE
, val
);
743 i
= scan(scpriv
, tokval
);
747 static expr
*expr6(int critical
)
759 i
= scan(scpriv
, tokval
);
763 return scalar_mult(e
, -1L, false);
767 i
= scan(scpriv
, tokval
);
768 return expr6(critical
);
771 i
= scan(scpriv
, tokval
);
775 if (is_just_unknown(e
))
776 return unknown_expr();
777 else if (!is_simple(e
)) {
778 error(ERR_NONFATAL
, "`~' operator may only be applied to"
782 return scalarvect(~reloc_value(e
));
785 i
= scan(scpriv
, tokval
);
789 if (is_just_unknown(e
))
790 return unknown_expr();
791 else if (!is_simple(e
)) {
792 error(ERR_NONFATAL
, "`!' operator may only be applied to"
796 return scalarvect(!reloc_value(e
));
799 i
= scan(scpriv
, tokval
);
806 if (is_unknown(e
) && critical
) {
807 error(ERR_NONFATAL
, "unable to determine segment base");
813 return eval_floatize(tokval
->t_integer
);
816 return eval_strfunc(tokval
->t_integer
);
819 i
= scan(scpriv
, tokval
);
824 error(ERR_NONFATAL
, "expecting `)'");
827 i
= scan(scpriv
, tokval
);
834 case TOKEN_INSN
: /* Opcodes that occur here are really labels */
840 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
843 tmpval
= readstrnum(tokval
->t_charptr
, tokval
->t_inttwo
, &rn_warn
);
845 error(ERR_WARNING
|ERR_PASS1
, "character constant too long");
846 addtotemp(EXPR_SIMPLE
, tmpval
);
849 addtotemp(tokval
->t_integer
, 1L);
850 if (hint
&& hint
->type
== EAH_NOHINT
)
851 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
858 * If !location->known, this indicates that no
859 * symbol, Here or Base references are valid because we
860 * are in preprocess-only mode.
862 if (!location
->known
) {
864 "%s not supported in preprocess-only mode",
865 (i
== TOKEN_HERE
? "`$'" :
866 i
== TOKEN_BASE
? "`$$'" :
867 "symbol references"));
868 addtotemp(EXPR_UNKNOWN
, 1L);
872 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
873 if (i
== TOKEN_BASE
) {
874 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
876 } else if (i
== TOKEN_HERE
) {
877 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
878 label_ofs
= in_abs_seg
? abs_offset
: location
->offset
;
880 if (!labelfunc(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
881 scope
= local_scope(tokval
->t_charptr
);
883 error(ERR_NONFATAL
, "symbol `%s%s' undefined",
884 scope
,tokval
->t_charptr
);
886 } else if (critical
== 1) {
888 "symbol `%s%s' not defined before use",
889 scope
,tokval
->t_charptr
);
899 if (opflags
&& is_extern(tokval
->t_charptr
))
900 *opflags
|= OPFLAG_EXTERN
;
902 addtotemp(type
, label_ofs
);
903 if (label_seg
!= NO_SEG
)
904 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
907 i
= scan(scpriv
, tokval
);
911 error(ERR_NONFATAL
, "expression syntax error");
916 void eval_global_info(struct ofmt
*output
, lfunc lookup_label
,
917 struct location
* locp
)
920 labelfunc
= lookup_label
;
924 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
925 int *fwref
, int critical
, efunc report_error
,
926 struct eval_hints
*hints
)
933 hint
->type
= EAH_NOHINT
;
935 if (critical
& CRITICAL
) {
936 critical
&= ~CRITICAL
;
944 error
= report_error
;
947 if (tokval
->t_type
== TOKEN_INVALID
)
948 i
= scan(scpriv
, tokval
);
952 while (ntempexprs
) /* initialize temporary storage */
953 nasm_free(tempexprs
[--ntempexprs
]);
959 if (i
== TOKEN_WRT
) {
960 i
= scan(scpriv
, tokval
); /* eat the WRT */
965 e
= scalar_mult(e
, 1L, false); /* strip far-absolute segment part */
968 if (is_just_unknown(f
))
974 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
977 value
= reloc_seg(f
);
979 value
= reloc_value(f
) | SEG_ABS
;
980 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
981 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
984 addtotemp(EXPR_WRT
, value
);
987 e
= add_vectors(e
, g
);