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
22 #define TEMPEXPRS_DELTA 128
23 #define TEMPEXPR_DELTA 8
25 static scanner scan
; /* Address of scanner routine */
26 static efunc error
; /* Address of error reporting routine */
27 static lfunc labelfunc
; /* Address of label routine */
29 static struct ofmt
*outfmt
; /* Structure of addresses of output routines */
31 static expr
**tempexprs
= NULL
;
32 static int ntempexprs
;
33 static int tempexprs_size
= 0;
35 static expr
*tempexpr
;
37 static int tempexpr_size
;
39 static struct tokenval
*tokval
; /* The current token */
40 static int i
; /* The t_type of tokval */
43 static loc_t
*location
; /* Pointer to current line's segment,offset */
46 static struct eval_hints
*hint
;
48 extern int in_abs_seg
; /* ABSOLUTE segment flag */
49 extern long abs_seg
; /* ABSOLUTE segment */
50 extern long abs_offset
; /* ABSOLUTE segment offset */
53 * Unimportant cleanup is done to avoid confusing people who are trying
54 * to debug real memory leaks
56 void eval_cleanup(void)
59 nasm_free(tempexprs
[--ntempexprs
]);
64 * Construct a temporary expression.
66 static void begintemp(void)
69 tempexpr_size
= ntempexpr
= 0;
72 static void addtotemp(long type
, long value
)
74 while (ntempexpr
>= tempexpr_size
) {
75 tempexpr_size
+= TEMPEXPR_DELTA
;
76 tempexpr
= nasm_realloc(tempexpr
,
77 tempexpr_size
* sizeof(*tempexpr
));
79 tempexpr
[ntempexpr
].type
= type
;
80 tempexpr
[ntempexpr
++].value
= value
;
83 static expr
*finishtemp(void)
85 addtotemp(0L, 0L); /* terminate */
86 while (ntempexprs
>= tempexprs_size
) {
87 tempexprs_size
+= TEMPEXPRS_DELTA
;
88 tempexprs
= nasm_realloc(tempexprs
,
89 tempexprs_size
* sizeof(*tempexprs
));
91 return tempexprs
[ntempexprs
++] = tempexpr
;
95 * Add two vector datatypes. We have some bizarre behaviour on far-
96 * absolute segment types: we preserve them during addition _only_
97 * if one of the segments is a truly pure scalar.
99 static expr
*add_vectors(expr
* p
, expr
* q
)
103 preserve
= is_really_simple(p
) || is_really_simple(q
);
107 while (p
->type
&& q
->type
&&
108 p
->type
< EXPR_SEGBASE
+ SEG_ABS
&&
109 q
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
112 if (p
->type
> q
->type
) {
113 addtotemp(q
->type
, q
->value
);
114 lasttype
= q
++->type
;
115 } else if (p
->type
< q
->type
) {
116 addtotemp(p
->type
, p
->value
);
117 lasttype
= p
++->type
;
118 } else { /* *p and *q have same type */
119 long sum
= p
->value
+ q
->value
;
121 addtotemp(p
->type
, sum
);
125 if (lasttype
== EXPR_UNKNOWN
) {
129 while (p
->type
&& (preserve
|| p
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
130 addtotemp(p
->type
, p
->value
);
133 while (q
->type
&& (preserve
|| q
->type
< EXPR_SEGBASE
+ SEG_ABS
)) {
134 addtotemp(q
->type
, q
->value
);
142 * Multiply a vector by a scalar. Strip far-absolute segment part
145 * Explicit treatment of UNKNOWN is not required in this routine,
146 * since it will silently do the Right Thing anyway.
148 * If `affect_hints' is set, we also change the hint type to
149 * NOTBASE if a MAKEBASE hint points at a register being
150 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
151 * as the base register.
153 static expr
*scalar_mult(expr
* vect
, long scalar
, int affect_hints
)
157 while (p
->type
&& p
->type
< EXPR_SEGBASE
+ SEG_ABS
) {
158 p
->value
= scalar
* (p
->value
);
159 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
160 p
->type
== hint
->base
&& affect_hints
)
161 hint
->type
= EAH_NOTBASE
;
169 static expr
*scalarvect(long scalar
)
172 addtotemp(EXPR_SIMPLE
, scalar
);
176 static expr
*unknown_expr(void)
179 addtotemp(EXPR_UNKNOWN
, 1L);
184 * The SEG operator: calculate the segment part of a relocatable
185 * value. Return NULL, as usual, if an error occurs. Report the
188 static expr
*segment_part(expr
* e
)
193 return unknown_expr();
196 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
202 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
204 } else if (seg
& SEG_ABS
) {
205 return scalarvect(seg
& ~SEG_ABS
);
206 } else if (seg
& 1) {
207 error(ERR_NONFATAL
, "SEG applied to something which"
208 " is already a segment base");
211 long base
= outfmt
->segbase(seg
+ 1);
214 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+ base
),
221 * Recursive-descent parser. Called with a single boolean operand,
222 * which is TRUE if the evaluation is critical (i.e. unresolved
223 * symbols are an error condition). Must update the global `i' to
224 * reflect the token after the parsed string. May return NULL.
226 * evaluate() should report its own errors: on return it is assumed
227 * that if NULL has been returned, the error has already been
234 * expr : bexpr [ WRT expr6 ]
235 * bexpr : rexp0 or expr0 depending on relative-mode setting
236 * rexp0 : rexp1 [ {||} rexp1...]
237 * rexp1 : rexp2 [ {^^} rexp2...]
238 * rexp2 : rexp3 [ {&&} rexp3...]
239 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
240 * expr0 : expr1 [ {|} expr1...]
241 * expr1 : expr2 [ {^} expr2...]
242 * expr2 : expr3 [ {&} expr3...]
243 * expr3 : expr4 [ {<<,>>} expr4...]
244 * expr4 : expr5 [ {+,-} expr5...]
245 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
246 * expr6 : { ~,+,-,SEG } expr6
253 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
255 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
256 static expr
*expr4(int), *expr5(int), *expr6(int);
258 static expr
*(*bexpr
) (int);
260 static expr
*rexp0(int critical
)
268 while (i
== TOKEN_DBL_OR
) {
269 i
= scan(scpriv
, tokval
);
273 if (!(is_simple(e
) || is_just_unknown(e
)) ||
274 !(is_simple(f
) || is_just_unknown(f
))) {
275 error(ERR_NONFATAL
, "`|' operator may only be applied to"
279 if (is_just_unknown(e
) || is_just_unknown(f
))
282 e
= scalarvect((long)(reloc_value(e
) || reloc_value(f
)));
287 static expr
*rexp1(int critical
)
295 while (i
== TOKEN_DBL_XOR
) {
296 i
= scan(scpriv
, tokval
);
300 if (!(is_simple(e
) || is_just_unknown(e
)) ||
301 !(is_simple(f
) || is_just_unknown(f
))) {
302 error(ERR_NONFATAL
, "`^' operator may only be applied to"
306 if (is_just_unknown(e
) || is_just_unknown(f
))
309 e
= scalarvect((long)(!reloc_value(e
) ^ !reloc_value(f
)));
314 static expr
*rexp2(int critical
)
321 while (i
== TOKEN_DBL_AND
) {
322 i
= scan(scpriv
, tokval
);
326 if (!(is_simple(e
) || is_just_unknown(e
)) ||
327 !(is_simple(f
) || is_just_unknown(f
))) {
328 error(ERR_NONFATAL
, "`&' operator may only be applied to"
331 if (is_just_unknown(e
) || is_just_unknown(f
))
334 e
= scalarvect((long)(reloc_value(e
) && reloc_value(f
)));
339 static expr
*rexp3(int critical
)
348 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
349 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
351 i
= scan(scpriv
, tokval
);
356 e
= add_vectors(e
, scalar_mult(f
, -1L, FALSE
));
362 v
= -1; /* means unknown */
363 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
364 v
= (j
== TOKEN_NE
); /* unequal, so return TRUE if NE */
366 v
= (j
== TOKEN_EQ
); /* equal, so return TRUE if EQ */
370 v
= -1; /* means unknown */
371 else if (!is_really_simple(e
)) {
373 "`%s': operands differ by a non-scalar",
374 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" : j
==
375 TOKEN_GE
? ">=" : ">"));
376 v
= 0; /* must set it to _something_ */
378 int vv
= reloc_value(e
);
380 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
382 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
384 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
397 static expr
*expr0(int critical
)
406 i
= scan(scpriv
, tokval
);
410 if (!(is_simple(e
) || is_just_unknown(e
)) ||
411 !(is_simple(f
) || is_just_unknown(f
))) {
412 error(ERR_NONFATAL
, "`|' operator may only be applied to"
415 if (is_just_unknown(e
) || is_just_unknown(f
))
418 e
= scalarvect(reloc_value(e
) | reloc_value(f
));
423 static expr
*expr1(int critical
)
432 i
= scan(scpriv
, tokval
);
436 if (!(is_simple(e
) || is_just_unknown(e
)) ||
437 !(is_simple(f
) || is_just_unknown(f
))) {
438 error(ERR_NONFATAL
, "`^' operator may only be applied to"
441 if (is_just_unknown(e
) || is_just_unknown(f
))
444 e
= scalarvect(reloc_value(e
) ^ reloc_value(f
));
449 static expr
*expr2(int critical
)
458 i
= scan(scpriv
, tokval
);
462 if (!(is_simple(e
) || is_just_unknown(e
)) ||
463 !(is_simple(f
) || is_just_unknown(f
))) {
464 error(ERR_NONFATAL
, "`&' operator may only be applied to"
467 if (is_just_unknown(e
) || is_just_unknown(f
))
470 e
= scalarvect(reloc_value(e
) & reloc_value(f
));
475 static expr
*expr3(int critical
)
483 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
485 i
= scan(scpriv
, tokval
);
489 if (!(is_simple(e
) || is_just_unknown(e
)) ||
490 !(is_simple(f
) || is_just_unknown(f
))) {
491 error(ERR_NONFATAL
, "shift operator may only be applied to"
493 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
498 e
= scalarvect(reloc_value(e
) << reloc_value(f
));
501 e
= scalarvect(((unsigned long)reloc_value(e
)) >>
509 static expr
*expr4(int critical
)
516 while (i
== '+' || i
== '-') {
518 i
= scan(scpriv
, tokval
);
524 e
= add_vectors(e
, f
);
527 e
= add_vectors(e
, scalar_mult(f
, -1L, FALSE
));
534 static expr
*expr5(int critical
)
541 while (i
== '*' || i
== '/' || i
== '%' ||
542 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
544 i
= scan(scpriv
, tokval
);
548 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
549 !(is_simple(f
) || is_just_unknown(f
)))) {
550 error(ERR_NONFATAL
, "division operator may only be applied to"
554 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
555 error(ERR_NONFATAL
, "division by zero");
561 e
= scalar_mult(f
, reloc_value(e
), TRUE
);
562 else if (is_simple(f
))
563 e
= scalar_mult(e
, reloc_value(f
), TRUE
);
564 else if (is_just_unknown(e
) && is_just_unknown(f
))
567 error(ERR_NONFATAL
, "unable to multiply two "
568 "non-scalar objects");
573 if (is_just_unknown(e
) || is_just_unknown(f
))
576 e
= scalarvect(((unsigned long)reloc_value(e
)) /
577 ((unsigned long)reloc_value(f
)));
580 if (is_just_unknown(e
) || is_just_unknown(f
))
583 e
= scalarvect(((unsigned long)reloc_value(e
)) %
584 ((unsigned long)reloc_value(f
)));
587 if (is_just_unknown(e
) || is_just_unknown(f
))
590 e
= scalarvect(((signed long)reloc_value(e
)) /
591 ((signed long)reloc_value(f
)));
594 if (is_just_unknown(e
) || is_just_unknown(f
))
597 e
= scalarvect(((signed long)reloc_value(e
)) %
598 ((signed long)reloc_value(f
)));
605 static expr
*expr6(int critical
)
609 long label_seg
, label_ofs
;
612 i
= scan(scpriv
, tokval
);
616 return scalar_mult(e
, -1L, FALSE
);
617 } else if (i
== '+') {
618 i
= scan(scpriv
, tokval
);
619 return expr6(critical
);
620 } else if (i
== '~') {
621 i
= scan(scpriv
, tokval
);
625 if (is_just_unknown(e
))
626 return unknown_expr();
627 else if (!is_simple(e
)) {
628 error(ERR_NONFATAL
, "`~' operator may only be applied to"
632 return scalarvect(~reloc_value(e
));
633 } else if (i
== TOKEN_SEG
) {
634 i
= scan(scpriv
, tokval
);
641 if (is_unknown(e
) && critical
) {
642 error(ERR_NONFATAL
, "unable to determine segment base");
646 } else if (i
== '(') {
647 i
= scan(scpriv
, tokval
);
652 error(ERR_NONFATAL
, "expecting `)'");
655 i
= scan(scpriv
, tokval
);
657 } else if (i
== TOKEN_NUM
|| i
== TOKEN_REG
|| i
== TOKEN_ID
||
658 i
== TOKEN_HERE
|| i
== TOKEN_BASE
) {
662 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
665 addtotemp(tokval
->t_integer
, 1L);
666 if (hint
&& hint
->type
== EAH_NOHINT
)
667 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
673 * If !location->known, this indicates that no
674 * symbol, Here or Base references are valid because we
675 * are in preprocess-only mode.
677 if (!location
->known
) {
679 "%s not supported in preprocess-only mode",
680 (i
== TOKEN_ID
? "symbol references" :
681 i
== TOKEN_HERE
? "`$'" : "`$$'"));
682 addtotemp(EXPR_UNKNOWN
, 1L);
686 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
687 if (i
== TOKEN_BASE
) {
688 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
690 } else if (i
== TOKEN_HERE
) {
691 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
692 label_ofs
= in_abs_seg
? abs_offset
: location
->offset
;
694 if (!labelfunc(tokval
->t_charptr
, &label_seg
, &label_ofs
)) {
696 error(ERR_NONFATAL
, "symbol `%s' undefined",
699 } else if (critical
== 1) {
701 "symbol `%s' not defined before use",
712 if (opflags
&& is_extern(tokval
->t_charptr
))
713 *opflags
|= OPFLAG_EXTERN
;
715 addtotemp(type
, label_ofs
);
716 if (label_seg
!= NO_SEG
)
717 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
720 i
= scan(scpriv
, tokval
);
723 error(ERR_NONFATAL
, "expression syntax error");
728 void eval_global_info(struct ofmt
*output
, lfunc lookup_label
,
732 labelfunc
= lookup_label
;
736 expr
*evaluate(scanner sc
, void *scprivate
, struct tokenval
*tv
,
737 int *fwref
, int critical
, efunc report_error
,
738 struct eval_hints
*hints
)
745 hint
->type
= EAH_NOHINT
;
747 if (critical
& CRITICAL
) {
748 critical
&= ~CRITICAL
;
756 error
= report_error
;
759 if (tokval
->t_type
== TOKEN_INVALID
)
760 i
= scan(scpriv
, tokval
);
764 while (ntempexprs
) /* initialise temporary storage */
765 nasm_free(tempexprs
[--ntempexprs
]);
771 if (i
== TOKEN_WRT
) {
772 i
= scan(scpriv
, tokval
); /* eat the WRT */
777 e
= scalar_mult(e
, 1L, FALSE
); /* strip far-absolute segment part */
780 if (is_just_unknown(f
))
786 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
789 value
= reloc_seg(f
);
791 value
= reloc_value(f
) | SEG_ABS
;
792 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
793 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
796 addtotemp(EXPR_WRT
, value
);
799 e
= add_vectors(e
, g
);