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
]);
60 nasm_free (tempexprs
);
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
)
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 long sum
= p
->value
+ q
->value
;
122 addtotemp(p
->type
, sum
);
126 if (lasttype
== EXPR_UNKNOWN
) {
131 (preserve
|| p
->type
< EXPR_SEGBASE
+SEG_ABS
))
133 addtotemp(p
->type
, p
->value
);
137 (preserve
|| q
->type
< EXPR_SEGBASE
+SEG_ABS
))
139 addtotemp(q
->type
, q
->value
);
147 * Multiply a vector by a scalar. Strip far-absolute segment part
150 * Explicit treatment of UNKNOWN is not required in this routine,
151 * since it will silently do the Right Thing anyway.
153 * If `affect_hints' is set, we also change the hint type to
154 * NOTBASE if a MAKEBASE hint points at a register being
155 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
156 * as the base register.
158 static expr
*scalar_mult(expr
*vect
, long scalar
, int affect_hints
)
162 while (p
->type
&& p
->type
< EXPR_SEGBASE
+SEG_ABS
) {
163 p
->value
= scalar
* (p
->value
);
164 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
165 p
->type
== hint
->base
&& affect_hints
)
166 hint
->type
= EAH_NOTBASE
;
174 static expr
*scalarvect (long scalar
)
177 addtotemp(EXPR_SIMPLE
, scalar
);
181 static expr
*unknown_expr (void)
184 addtotemp(EXPR_UNKNOWN
, 1L);
189 * The SEG operator: calculate the segment part of a relocatable
190 * value. Return NULL, as usual, if an error occurs. Report the
193 static expr
*segment_part (expr
*e
)
198 return unknown_expr();
201 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
207 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
209 } else if (seg
& SEG_ABS
) {
210 return scalarvect(seg
& ~SEG_ABS
);
211 } else if (seg
& 1) {
212 error(ERR_NONFATAL
, "SEG applied to something which"
213 " is already a segment base");
217 long base
= outfmt
->segbase(seg
+1);
220 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+base
), 1L);
226 * Recursive-descent parser. Called with a single boolean operand,
227 * which is TRUE if the evaluation is critical (i.e. unresolved
228 * symbols are an error condition). Must update the global `i' to
229 * reflect the token after the parsed string. May return NULL.
231 * evaluate() should report its own errors: on return it is assumed
232 * that if NULL has been returned, the error has already been
239 * expr : bexpr [ WRT expr6 ]
240 * bexpr : rexp0 or expr0 depending on relative-mode setting
241 * rexp0 : rexp1 [ {||} rexp1...]
242 * rexp1 : rexp2 [ {^^} rexp2...]
243 * rexp2 : rexp3 [ {&&} rexp3...]
244 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
245 * expr0 : expr1 [ {|} expr1...]
246 * expr1 : expr2 [ {^} expr2...]
247 * expr2 : expr3 [ {&} expr3...]
248 * expr3 : expr4 [ {<<,>>} expr4...]
249 * expr4 : expr5 [ {+,-} expr5...]
250 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
251 * expr6 : { ~,+,-,SEG } expr6
258 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
260 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
261 static expr
*expr4(int), *expr5(int), *expr6(int);
263 static expr
*(*bexpr
)(int);
265 static expr
*rexp0(int critical
)
273 while (i
== TOKEN_DBL_OR
)
275 i
= scan(scpriv
, tokval
);
279 if (!(is_simple(e
) || is_just_unknown(e
)) ||
280 !(is_simple(f
) || is_just_unknown(f
)))
282 error(ERR_NONFATAL
, "`|' operator may only be applied to"
286 if (is_just_unknown(e
) || is_just_unknown(f
))
289 e
= scalarvect ((long) (reloc_value(e
) || reloc_value(f
)));
294 static expr
*rexp1(int critical
)
302 while (i
== TOKEN_DBL_XOR
)
304 i
= scan(scpriv
, tokval
);
308 if (!(is_simple(e
) || is_just_unknown(e
)) ||
309 !(is_simple(f
) || is_just_unknown(f
)))
311 error(ERR_NONFATAL
, "`^' operator may only be applied to"
315 if (is_just_unknown(e
) || is_just_unknown(f
))
318 e
= scalarvect ((long) (!reloc_value(e
) ^ !reloc_value(f
)));
323 static expr
*rexp2(int critical
)
330 while (i
== TOKEN_DBL_AND
)
332 i
= scan(scpriv
, tokval
);
336 if (!(is_simple(e
) || is_just_unknown(e
)) ||
337 !(is_simple(f
) || is_just_unknown(f
)))
339 error(ERR_NONFATAL
, "`&' operator may only be applied to"
342 if (is_just_unknown(e
) || is_just_unknown(f
))
345 e
= scalarvect ((long) (reloc_value(e
) && reloc_value(f
)));
350 static expr
*rexp3(int critical
)
359 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
360 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
)
363 i
= scan(scpriv
, tokval
);
368 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
372 case TOKEN_EQ
: case TOKEN_NE
:
374 v
= -1; /* means unknown */
375 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
376 v
= (j
== TOKEN_NE
); /* unequal, so return TRUE if NE */
378 v
= (j
== TOKEN_EQ
); /* equal, so return TRUE if EQ */
382 v
= -1; /* means unknown */
383 else if (!is_really_simple(e
)) {
384 error(ERR_NONFATAL
, "`%s': operands differ by a non-scalar",
385 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" :
386 j
== TOKEN_GE
? ">=" : ">"));
387 v
= 0; /* must set it to _something_ */
389 int vv
= reloc_value(e
);
391 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
393 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
395 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
408 static expr
*expr0(int critical
)
418 i
= scan(scpriv
, tokval
);
422 if (!(is_simple(e
) || is_just_unknown(e
)) ||
423 !(is_simple(f
) || is_just_unknown(f
)))
425 error(ERR_NONFATAL
, "`|' operator may only be applied to"
428 if (is_just_unknown(e
) || is_just_unknown(f
))
431 e
= scalarvect (reloc_value(e
) | reloc_value(f
));
436 static expr
*expr1(int critical
)
445 i
= scan(scpriv
, tokval
);
449 if (!(is_simple(e
) || is_just_unknown(e
)) ||
450 !(is_simple(f
) || is_just_unknown(f
)))
452 error(ERR_NONFATAL
, "`^' operator may only be applied to"
455 if (is_just_unknown(e
) || is_just_unknown(f
))
458 e
= scalarvect (reloc_value(e
) ^ reloc_value(f
));
463 static expr
*expr2(int critical
)
472 i
= scan(scpriv
, tokval
);
476 if (!(is_simple(e
) || is_just_unknown(e
)) ||
477 !(is_simple(f
) || is_just_unknown(f
)))
479 error(ERR_NONFATAL
, "`&' operator may only be applied to"
482 if (is_just_unknown(e
) || is_just_unknown(f
))
485 e
= scalarvect (reloc_value(e
) & reloc_value(f
));
490 static expr
*expr3(int critical
)
498 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
)
501 i
= scan(scpriv
, tokval
);
505 if (!(is_simple(e
) || is_just_unknown(e
)) ||
506 !(is_simple(f
) || is_just_unknown(f
)))
508 error(ERR_NONFATAL
, "shift operator may only be applied to"
510 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
514 e
= scalarvect (reloc_value(e
) << reloc_value(f
));
517 e
= scalarvect (((unsigned long)reloc_value(e
)) >>
525 static expr
*expr4(int critical
)
532 while (i
== '+' || i
== '-')
535 i
= scan(scpriv
, tokval
);
541 e
= add_vectors (e
, f
);
544 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
551 static expr
*expr5(int critical
)
558 while (i
== '*' || i
== '/' || i
== '%' ||
559 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
)
562 i
= scan(scpriv
, tokval
);
566 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
567 !(is_simple(f
) || is_just_unknown(f
))))
569 error(ERR_NONFATAL
, "division operator may only be applied to"
573 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
574 error(ERR_NONFATAL
, "division by zero");
580 e
= scalar_mult (f
, reloc_value(e
), TRUE
);
581 else if (is_simple(f
))
582 e
= scalar_mult (e
, reloc_value(f
), TRUE
);
583 else if (is_just_unknown(e
) && is_just_unknown(f
))
586 error(ERR_NONFATAL
, "unable to multiply two "
587 "non-scalar objects");
592 if (is_just_unknown(e
) || is_just_unknown(f
))
595 e
= scalarvect (((unsigned long)reloc_value(e
)) /
596 ((unsigned long)reloc_value(f
)));
599 if (is_just_unknown(e
) || is_just_unknown(f
))
602 e
= scalarvect (((unsigned long)reloc_value(e
)) %
603 ((unsigned long)reloc_value(f
)));
606 if (is_just_unknown(e
) || is_just_unknown(f
))
609 e
= scalarvect (((signed long)reloc_value(e
)) /
610 ((signed long)reloc_value(f
)));
613 if (is_just_unknown(e
) || is_just_unknown(f
))
616 e
= scalarvect (((signed long)reloc_value(e
)) %
617 ((signed long)reloc_value(f
)));
624 static expr
*expr6(int critical
)
628 long label_seg
, label_ofs
;
631 i
= scan(scpriv
, tokval
);
635 return scalar_mult (e
, -1L, FALSE
);
636 } else if (i
== '+') {
637 i
= scan(scpriv
, tokval
);
638 return expr6(critical
);
639 } else if (i
== '~') {
640 i
= scan(scpriv
, tokval
);
644 if (is_just_unknown(e
))
645 return unknown_expr();
646 else if (!is_simple(e
)) {
647 error(ERR_NONFATAL
, "`~' operator may only be applied to"
651 return scalarvect(~reloc_value(e
));
652 } else if (i
== TOKEN_SEG
) {
653 i
= scan(scpriv
, tokval
);
660 if (is_unknown(e
) && critical
) {
661 error(ERR_NONFATAL
, "unable to determine segment base");
665 } else if (i
== '(') {
666 i
= scan(scpriv
, tokval
);
671 error(ERR_NONFATAL
, "expecting `)'");
674 i
= scan(scpriv
, tokval
);
677 else if (i
== TOKEN_NUM
|| i
== TOKEN_REG
|| i
== TOKEN_ID
||
678 i
== TOKEN_HERE
|| i
== TOKEN_BASE
)
683 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
686 addtotemp(tokval
->t_integer
, 1L);
687 if (hint
&& hint
->type
== EAH_NOHINT
)
688 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
694 * If !location->known, this indicates that no
695 * symbol, Here or Base references are valid because we
696 * are in preprocess-only mode.
698 if (!location
->known
) {
700 "%s not supported in preprocess-only mode",
701 (i
== TOKEN_ID
? "symbol references" :
702 i
== TOKEN_HERE
? "`$'" : "`$$'"));
703 addtotemp(EXPR_UNKNOWN
, 1L);
707 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
710 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
712 } else if (i
== TOKEN_HERE
) {
713 label_seg
= in_abs_seg
? abs_seg
: location
->segment
;
714 label_ofs
= in_abs_seg
? abs_offset
: location
->offset
;
716 if (!labelfunc(tokval
->t_charptr
,&label_seg
,&label_ofs
))
719 error (ERR_NONFATAL
, "symbol `%s' undefined",
722 } else if (critical
== 1) {
724 "symbol `%s' not defined before use",
735 if (opflags
&& is_extern (tokval
->t_charptr
))
736 *opflags
|= OPFLAG_EXTERN
;
738 addtotemp(type
, label_ofs
);
739 if (label_seg
!=NO_SEG
)
740 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
743 i
= scan(scpriv
, tokval
);
746 error(ERR_NONFATAL
, "expression syntax error");
751 void eval_global_info (struct ofmt
*output
, lfunc lookup_label
, loc_t
*locp
)
754 labelfunc
= lookup_label
;
758 expr
*evaluate (scanner sc
, void *scprivate
, struct tokenval
*tv
,
759 int *fwref
, int critical
, efunc report_error
,
760 struct eval_hints
*hints
)
767 hint
->type
= EAH_NOHINT
;
769 if (critical
& CRITICAL
) {
770 critical
&= ~CRITICAL
;
778 error
= report_error
;
781 if (tokval
->t_type
== TOKEN_INVALID
)
782 i
= scan(scpriv
, tokval
);
786 while (ntempexprs
) /* initialise temporary storage */
787 nasm_free (tempexprs
[--ntempexprs
]);
789 e
= bexpr (critical
);
793 if (i
== TOKEN_WRT
) {
794 i
= scan(scpriv
, tokval
); /* eat the WRT */
795 f
= expr6 (critical
);
799 e
= scalar_mult (e
, 1L, FALSE
); /* strip far-absolute segment part */
802 if (is_just_unknown(f
))
808 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
811 value
= reloc_seg(f
);
813 value
= reloc_value(f
) | SEG_ABS
;
814 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
)
816 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
819 addtotemp(EXPR_WRT
, value
);
822 e
= add_vectors (e
, g
);