1 /* eval.c expression evaluator for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
23 #define TEMPEXPRS_DELTA 128
24 #define TEMPEXPR_DELTA 8
26 static scanner scan
; /* Address of scanner routine */
27 static efunc error
; /* Address of error reporting routine */
28 static lfunc labelfunc
; /* Address of label routine */
30 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
32 static expr
**tempexprs
= NULL
;
33 static int ntempexprs
;
34 static int tempexprs_size
= 0;
36 static expr
*tempexpr
;
38 static int tempexpr_size
;
40 static struct tokenval
*tokval
; /* The current token */
41 static int i
; /* The t_type of tokval */
44 static loc_t
*location
; /* Pointer to current line's segment,offset */
47 static struct eval_hints
*hint
;
49 extern int in_abs_seg
; /* ABSOLUTE segment flag */
50 extern int32_t abs_seg
; /* ABSOLUTE segment */
51 extern int32_t abs_offset
; /* ABSOLUTE segment offset */
54 * Unimportant cleanup is done to avoid confusing people who are trying
55 * to debug real memory leaks
57 void eval_cleanup(void)
60 nasm_free(tempexprs
[--ntempexprs
]);
65 * Construct a temporary expression.
67 static void begintemp(void)
70 tempexpr_size
= ntempexpr
= 0;
73 static void addtotemp(int32_t type
, int64_t value
)
75 while (ntempexpr
>= tempexpr_size
) {
76 tempexpr_size
+= TEMPEXPR_DELTA
;
77 tempexpr
= nasm_realloc(tempexpr
,
78 tempexpr_size
* sizeof(*tempexpr
));
80 tempexpr
[ntempexpr
].type
= type
;
81 tempexpr
[ntempexpr
++].value
= value
;
84 static expr
*finishtemp(void)
86 addtotemp(0L, 0L); /* terminate */
87 while (ntempexprs
>= tempexprs_size
) {
88 tempexprs_size
+= TEMPEXPRS_DELTA
;
89 tempexprs
= nasm_realloc(tempexprs
,
90 tempexprs_size
* sizeof(*tempexprs
));
92 return tempexprs
[ntempexprs
++] = tempexpr
;
96 * Add two vector datatypes. We have some bizarre behaviour on far-
97 * absolute segment types: we preserve them during addition _only_
98 * if one of the segments is a truly pure scalar.
100 static expr
*add_vectors(expr
* p
, expr
* q
)
104 preserve
= is_really_simple(p
) || is_really_simple(q
);
108 while (p
->type
&& q
->type
&&
109 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
110 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
113 if (p
->type
> q
->type
) {
114 addtotemp(q
->type
, q
->value
);
115 lasttype
= q
++->type
;
116 } else if (p
->type
< q
->type
) {
117 addtotemp(p
->type
, p
->value
);
118 lasttype
= p
++->type
;
119 } else { /* *p and *q have same type */
120 int32_t sum
= p
->value
+ q
->value
;
122 addtotemp(p
->type
, sum
);
126 if (lasttype
== EXPR_UNKNOWN
) {
130 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
131 addtotemp(p
->type
, p
->value
);
134 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
135 addtotemp(q
->type
, q
->value
);
143 * Multiply a vector by a scalar. Strip far-absolute segment part
146 * Explicit treatment of UNKNOWN is not required in this routine,
147 * since it will silently do the Right Thing anyway.
149 * If `affect_hints' is set, we also change the hint type to
150 * NOTBASE if a MAKEBASE hint points at a register being
151 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
152 * as the base register.
154 static expr
*scalar_mult(expr
* vect
, int32_t scalar
, int affect_hints
)
158 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
159 p
->value
= scalar
* (p
->value
);
160 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
161 p
->type
== hint
->base
&& affect_hints
)
162 hint
->type
= EAH_NOTBASE
;
170 static expr
*scalarvect(int32_t scalar
)
173 addtotemp(EXPR_SIMPLE
, scalar
);
177 static expr
*unknown_expr(void)
180 addtotemp(EXPR_UNKNOWN
, 1L);
185 * The SEG operator: calculate the segment part of a relocatable
186 * value. Return NULL, as usual, if an error occurs. Report the
189 static expr
*segment_part(expr
* e
)
194 return unknown_expr();
197 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
203 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
205 } else if (seg
& SEG_ABS
) {
206 return scalarvect(seg
& ~SEG_ABS
);
207 } else if (seg
& 1) {
208 error(ERR_NONFATAL
, "SEG applied to something which"
209 " is already a segment base");
212 int32_t base
= outfmt
->segbase(seg
+ 1);
215 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
222 * Recursive-descent parser. Called with a single boolean operand,
223 * which is TRUE if the evaluation is critical (i.e. unresolved
224 * symbols are an error condition). Must update the global `i' to
225 * reflect the token after the parsed string. May return NULL.
227 * evaluate() should report its own errors: on return it is assumed
228 * that if NULL has been returned, the error has already been
235 * expr : bexpr [ WRT expr6 ]
236 * bexpr : rexp0 or expr0 depending on relative-mode setting
237 * rexp0 : rexp1 [ {||} rexp1...]
238 * rexp1 : rexp2 [ {^^} rexp2...]
239 * rexp2 : rexp3 [ {&&} rexp3...]
240 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
241 * expr0 : expr1 [ {|} expr1...]
242 * expr1 : expr2 [ {^} expr2...]
243 * expr2 : expr3 [ {&} expr3...]
244 * expr3 : expr4 [ {<<,>>} expr4...]
245 * expr4 : expr5 [ {+,-} expr5...]
246 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
247 * expr6 : { ~,+,-,SEG } expr6
254 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
256 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
257 static expr
*expr4(int), *expr5(int), *expr6(int);
259 static expr
*(*bexpr
) (int);
261 static expr
*rexp0(int critical
)
269 while (i
== TOKEN_DBL_OR
) {
270 i
= scan(scpriv
, tokval
);
274 if (!(is_simple(e
) || is_just_unknown(e
)) ||
275 !(is_simple(f
) || is_just_unknown(f
))) {
276 error(ERR_NONFATAL
, "`|' operator may only be applied to"
280 if (is_just_unknown(e
) || is_just_unknown(f
))
283 e
= scalarvect((int32_t)(reloc_value(e
) || reloc_value(f
)));
288 static expr
*rexp1(int critical
)
296 while (i
== TOKEN_DBL_XOR
) {
297 i
= scan(scpriv
, tokval
);
301 if (!(is_simple(e
) || is_just_unknown(e
)) ||
302 !(is_simple(f
) || is_just_unknown(f
))) {
303 error(ERR_NONFATAL
, "`^' operator may only be applied to"
307 if (is_just_unknown(e
) || is_just_unknown(f
))
310 e
= scalarvect((int32_t)(!reloc_value(e
) ^ !reloc_value(f
)));
315 static expr
*rexp2(int critical
)
322 while (i
== TOKEN_DBL_AND
) {
323 i
= scan(scpriv
, tokval
);
327 if (!(is_simple(e
) || is_just_unknown(e
)) ||
328 !(is_simple(f
) || is_just_unknown(f
))) {
329 error(ERR_NONFATAL
, "`&' operator may only be applied to"
332 if (is_just_unknown(e
) || is_just_unknown(f
))
335 e
= scalarvect((int32_t)(reloc_value(e
) && reloc_value(f
)));
340 static expr
*rexp3(int critical
)
349 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
350 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
352 i
= scan(scpriv
, tokval
);
357 e
= add_vectors(e
, scalar_mult(f
, -1L, FALSE
));
363 v
= -1; /* means unknown */
364 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
365 v
= (j
== TOKEN_NE
); /* unequal, so return TRUE if NE */
367 v
= (j
== TOKEN_EQ
); /* equal, so return TRUE if EQ */
371 v
= -1; /* means unknown */
372 else if (!is_really_simple(e
)) {
374 "`%s': operands differ by a non-scalar",
375 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
376 TOKEN_GE
? ">=" : ">"));
377 v
= 0; /* must set it to _something_ */
379 int vv
= reloc_value(e
);
381 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
383 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
385 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
398 static expr
*expr0(int critical
)
407 i
= scan(scpriv
, tokval
);
411 if (!(is_simple(e
) || is_just_unknown(e
)) ||
412 !(is_simple(f
) || is_just_unknown(f
))) {
413 error(ERR_NONFATAL
, "`|' operator may only be applied to"
416 if (is_just_unknown(e
) || is_just_unknown(f
))
419 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
424 static expr
*expr1(int critical
)
433 i
= scan(scpriv
, tokval
);
437 if (!(is_simple(e
) || is_just_unknown(e
)) ||
438 !(is_simple(f
) || is_just_unknown(f
))) {
439 error(ERR_NONFATAL
, "`^' operator may only be applied to"
442 if (is_just_unknown(e
) || is_just_unknown(f
))
445 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
450 static expr
*expr2(int critical
)
459 i
= scan(scpriv
, tokval
);
463 if (!(is_simple(e
) || is_just_unknown(e
)) ||
464 !(is_simple(f
) || is_just_unknown(f
))) {
465 error(ERR_NONFATAL
, "`&' operator may only be applied to"
468 if (is_just_unknown(e
) || is_just_unknown(f
))
471 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
476 static expr
*expr3(int critical
)
484 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
486 i
= scan(scpriv
, tokval
);
490 if (!(is_simple(e
) || is_just_unknown(e
)) ||
491 !(is_simple(f
) || is_just_unknown(f
))) {
492 error(ERR_NONFATAL
, "shift operator may only be applied to"
494 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
499 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
502 e
= scalarvect(((uint32_t)reloc_value(e
)) >>
510 static expr
*expr4(int critical
)
517 while (i
== '+' || i
== '-') {
519 i
= scan(scpriv
, tokval
);
525 e
= add_vectors(e
, f
);
528 e
= add_vectors(e
, scalar_mult(f
, -1L, FALSE
));
535 static expr
*expr5(int critical
)
542 while (i
== '*' || i
== '/' || i
== '%' ||
543 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
545 i
= scan(scpriv
, tokval
);
549 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
550 !(is_simple(f
) || is_just_unknown(f
)))) {
551 error(ERR_NONFATAL
, "division operator may only be applied to"
555 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
556 error(ERR_NONFATAL
, "division by zero");
562 e
= scalar_mult(f
, reloc_value(e
), TRUE
);
563 else if (is_simple(f
))
564 e
= scalar_mult(e
, reloc_value(f
), TRUE
);
565 else if (is_just_unknown(e
) && is_just_unknown(f
))
568 error(ERR_NONFATAL
, "unable to multiply two "
569 "non-scalar objects");
574 if (is_just_unknown(e
) || is_just_unknown(f
))
577 e
= scalarvect(((uint32_t)reloc_value(e
)) /
578 ((uint32_t)reloc_value(f
)));
581 if (is_just_unknown(e
) || is_just_unknown(f
))
584 e
= scalarvect(((uint32_t)reloc_value(e
)) %
585 ((uint32_t)reloc_value(f
)));
588 if (is_just_unknown(e
) || is_just_unknown(f
))
591 e
= scalarvect(((int32_t)reloc_value(e
)) /
592 ((int32_t)reloc_value(f
)));
595 if (is_just_unknown(e
) || is_just_unknown(f
))
598 e
= scalarvect(((int32_t)reloc_value(e
)) %
599 ((int32_t)reloc_value(f
)));
606 static expr
*expr6(int critical
)
610 int32_t label_seg
, label_ofs
;
613 i
= scan(scpriv
, tokval
);
617 return scalar_mult(e
, -1L, FALSE
);
618 } else if (i
== '+') {
619 i
= scan(scpriv
, tokval
);
620 return expr6(critical
);
621 } else if (i
== '~') {
622 i
= scan(scpriv
, tokval
);
626 if (is_just_unknown(e
))
627 return unknown_expr();
628 else if (!is_simple(e
)) {
629 error(ERR_NONFATAL
, "`~' operator may only be applied to"
633 return scalarvect(~reloc_value(e
));
634 } else if (i
== TOKEN_SEG
) {
635 i
= scan(scpriv
, tokval
);
642 if (is_unknown(e
) && critical
) {
643 error(ERR_NONFATAL
, "unable to determine segment base");
647 } else if (i
== '(') {
648 i
= scan(scpriv
, tokval
);
653 error(ERR_NONFATAL
, "expecting `)'");
656 i
= scan(scpriv
, tokval
);
658 } else if (i
== TOKEN_NUM
|| i
== TOKEN_REG
|| i
== TOKEN_ID
||
659 i
== TOKEN_HERE
|| i
== TOKEN_BASE
) {
663 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
666 addtotemp(tokval
->t_integer
, 1L);
667 if (hint
&& hint
->type
== EAH_NOHINT
)
668 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
674 * If !location->known, this indicates that no
675 * symbol, Here or Base references are valid because we
676 * are in preprocess-only mode.
678 if (!location
->known
) {
680 "%s not supported in preprocess-only mode",
681 (i
== TOKEN_ID
? "symbol references" :
682 i
== TOKEN_HERE
? "`$'" : "`$$'"));
683 addtotemp(EXPR_UNKNOWN
, 1L);
687 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
688 if (i
== TOKEN_BASE
) {
689 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
691 } else if (i
== TOKEN_HERE
) {
692 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
693 label_ofs
= in_abs_seg
? abs_offset
: location
->offset
;
695 if (!labelfunc(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
697 error(ERR_NONFATAL
, "symbol `%s' undefined",
700 } else if (critical
== 1) {
702 "symbol `%s' not defined before use",
713 if (opflags
&& is_extern(tokval
->t_charptr
))
714 *opflags
|= OPFLAG_EXTERN
;
716 addtotemp(type
, label_ofs
);
717 if (label_seg
!= NO_SEG
)
718 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
721 i
= scan(scpriv
, tokval
);
724 error(ERR_NONFATAL
, "expression syntax error");
729 void eval_global_info(struct ofmt
*output
, lfunc lookup_label
,
733 labelfunc
= lookup_label
;
737 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
738 int *fwref
, int critical
, efunc report_error
,
739 struct eval_hints
*hints
)
746 hint
->type
= EAH_NOHINT
;
748 if (critical
& CRITICAL
) {
749 critical
&= ~CRITICAL
;
757 error
= report_error
;
760 if (tokval
->t_type
== TOKEN_INVALID
)
761 i
= scan(scpriv
, tokval
);
765 while (ntempexprs
) /* initialize temporary storage */
766 nasm_free(tempexprs
[--ntempexprs
]);
772 if (i
== TOKEN_WRT
) {
773 i
= scan(scpriv
, tokval
); /* eat the WRT */
778 e
= scalar_mult(e
, 1L, FALSE
); /* strip far-absolute segment part */
781 if (is_just_unknown(f
))
787 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
790 value
= reloc_seg(f
);
792 value
= reloc_value(f
) | SEG_ABS
;
793 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
794 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
797 addtotemp(EXPR_WRT
, value
);
800 e
= add_vectors(e
, g
);