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
21 static expr
**tempexprs
= NULL
;
22 static int ntempexprs
, tempexprs_size
= 0;
23 #define TEMPEXPRS_DELTA 128
25 static expr
*tempexpr
;
26 static int ntempexpr
, tempexpr_size
;
27 #define TEMPEXPR_DELTA 8
31 static struct tokenval
*tokval
;
35 static char *label
= NULL
, special_empty_string
[] = "";
36 static lfunc labelfunc
;
37 static struct ofmt
*outfmt
;
40 static struct eval_hints
*hint
;
43 * Construct a temporary expression.
45 static void begintemp(void) {
47 tempexpr_size
= ntempexpr
= 0;
50 static void addtotemp(long type
, long value
) {
51 while (ntempexpr
>= tempexpr_size
) {
52 tempexpr_size
+= TEMPEXPR_DELTA
;
53 tempexpr
= nasm_realloc(tempexpr
,
54 tempexpr_size
*sizeof(*tempexpr
));
56 tempexpr
[ntempexpr
].type
= type
;
57 tempexpr
[ntempexpr
++].value
= value
;
60 static expr
*finishtemp(void) {
61 addtotemp (0L, 0L); /* terminate */
62 while (ntempexprs
>= tempexprs_size
) {
63 tempexprs_size
+= TEMPEXPRS_DELTA
;
64 tempexprs
= nasm_realloc(tempexprs
,
65 tempexprs_size
*sizeof(*tempexprs
));
67 return tempexprs
[ntempexprs
++] = tempexpr
;
71 * Add two vector datatypes. We have some bizarre behaviour on far-
72 * absolute segment types: we preserve them during addition _only_
73 * if one of the segments is a truly pure scalar.
75 static expr
*add_vectors(expr
*p
, expr
*q
) {
78 preserve
= is_really_simple(p
) || is_really_simple(q
);
82 while (p
->type
&& q
->type
&&
83 p
->type
< EXPR_SEGBASE
+SEG_ABS
&&
84 q
->type
< EXPR_SEGBASE
+SEG_ABS
) {
87 if (p
->type
> q
->type
) {
88 addtotemp(q
->type
, q
->value
);
90 } else if (p
->type
< q
->type
) {
91 addtotemp(p
->type
, p
->value
);
93 } else { /* *p and *q have same type */
94 addtotemp(p
->type
, p
->value
+ q
->value
);
98 if (lasttype
== EXPR_UNKNOWN
) {
103 (preserve
|| p
->type
< EXPR_SEGBASE
+SEG_ABS
)) {
104 addtotemp(p
->type
, p
->value
);
108 (preserve
|| q
->type
< EXPR_SEGBASE
+SEG_ABS
)) {
109 addtotemp(q
->type
, q
->value
);
117 * Multiply a vector by a scalar. Strip far-absolute segment part
120 * Explicit treatment of UNKNOWN is not required in this routine,
121 * since it will silently do the Right Thing anyway.
123 * If `affect_hints' is set, we also change the hint type to
124 * NOTBASE if a MAKEBASE hint points at a register being
125 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
126 * as the base register.
128 static expr
*scalar_mult(expr
*vect
, long scalar
, int affect_hints
) {
131 while (p
->type
&& p
->type
< EXPR_SEGBASE
+SEG_ABS
) {
132 p
->value
= scalar
* (p
->value
);
133 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
134 p
->type
== hint
->base
&& affect_hints
)
135 hint
->type
= EAH_NOTBASE
;
143 static expr
*scalarvect (long scalar
) {
145 addtotemp(EXPR_SIMPLE
, scalar
);
149 static expr
*unknown_expr (void) {
151 addtotemp(EXPR_UNKNOWN
, 1L);
156 * The SEG operator: calculate the segment part of a relocatable
157 * value. Return NULL, as usual, if an error occurs. Report the
160 static expr
*segment_part (expr
*e
) {
164 return unknown_expr();
167 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
173 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
175 } else if (seg
& SEG_ABS
) {
176 return scalarvect(seg
& ~SEG_ABS
);
177 } else if (seg
& 1) {
178 error(ERR_NONFATAL
, "SEG applied to something which"
179 " is already a segment base");
183 long base
= outfmt
->segbase(seg
+1);
186 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+base
), 1L);
192 * Recursive-descent parser. Called with a single boolean operand,
193 * which is TRUE if the evaluation is critical (i.e. unresolved
194 * symbols are an error condition). Must update the global `i' to
195 * reflect the token after the parsed string. May return NULL.
197 * evaluate() should report its own errors: on return it is assumed
198 * that if NULL has been returned, the error has already been
205 * expr : bexpr [ WRT expr6 ]
206 * bexpr : rexp0 or expr0 depending on relative-mode setting
207 * rexp0 : rexp1 [ {||} rexp1...]
208 * rexp1 : rexp2 [ {^^} rexp2...]
209 * rexp2 : rexp3 [ {&&} rexp3...]
210 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
211 * expr0 : expr1 [ {|} expr1...]
212 * expr1 : expr2 [ {^} expr2...]
213 * expr2 : expr3 [ {&} expr3...]
214 * expr3 : expr4 [ {<<,>>} expr4...]
215 * expr4 : expr5 [ {+,-} expr5...]
216 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
217 * expr6 : { ~,+,-,SEG } expr6
224 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
226 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
227 static expr
*expr4(int), *expr5(int), *expr6(int);
229 static expr
*(*bexpr
)(int);
231 static expr
*rexp0(int critical
) {
237 while (i
== TOKEN_DBL_OR
) {
238 i
= scan(scpriv
, tokval
);
242 if (!(is_simple(e
) || is_just_unknown(e
)) ||
243 !(is_simple(f
) || is_just_unknown(f
))) {
244 error(ERR_NONFATAL
, "`|' operator may only be applied to"
247 if (is_just_unknown(e
) || is_just_unknown(f
))
250 e
= scalarvect ((long) (reloc_value(e
) || reloc_value(f
)));
255 static expr
*rexp1(int critical
) {
261 while (i
== TOKEN_DBL_XOR
) {
262 i
= scan(scpriv
, tokval
);
266 if (!(is_simple(e
) || is_just_unknown(e
)) ||
267 !(is_simple(f
) || is_just_unknown(f
))) {
268 error(ERR_NONFATAL
, "`^' operator may only be applied to"
271 if (is_just_unknown(e
) || is_just_unknown(f
))
274 e
= scalarvect ((long) (!reloc_value(e
) ^ !reloc_value(f
)));
279 static expr
*rexp2(int critical
) {
285 while (i
== TOKEN_DBL_AND
) {
286 i
= scan(scpriv
, tokval
);
290 if (!(is_simple(e
) || is_just_unknown(e
)) ||
291 !(is_simple(f
) || is_just_unknown(f
))) {
292 error(ERR_NONFATAL
, "`&' operator may only be applied to"
295 if (is_just_unknown(e
) || is_just_unknown(f
))
298 e
= scalarvect ((long) (reloc_value(e
) && reloc_value(f
)));
303 static expr
*rexp3(int critical
) {
310 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
311 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
313 i
= scan(scpriv
, tokval
);
317 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
319 case TOKEN_EQ
: case TOKEN_NE
:
321 v
= -1; /* means unknown */
322 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
323 v
= (j
== TOKEN_NE
); /* unequal, so return TRUE if NE */
325 v
= (j
== TOKEN_EQ
); /* equal, so return TRUE if EQ */
329 v
= -1; /* means unknown */
330 else if (!is_really_simple(e
)) {
331 error(ERR_NONFATAL
, "`%s': operands differ by a non-scalar",
332 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" :
333 j
== TOKEN_GE
? ">=" : ">"));
334 v
= 0; /* must set it to _something_ */
336 int vv
= reloc_value(e
);
338 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
340 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
342 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
354 static expr
*expr0(int critical
) {
361 i
= scan(scpriv
, tokval
);
365 if (!(is_simple(e
) || is_just_unknown(e
)) ||
366 !(is_simple(f
) || is_just_unknown(f
))) {
367 error(ERR_NONFATAL
, "`|' operator may only be applied to"
370 if (is_just_unknown(e
) || is_just_unknown(f
))
373 e
= scalarvect (reloc_value(e
) | reloc_value(f
));
378 static expr
*expr1(int critical
) {
385 i
= scan(scpriv
, tokval
);
389 if (!(is_simple(e
) || is_just_unknown(e
)) ||
390 !(is_simple(f
) || is_just_unknown(f
))) {
391 error(ERR_NONFATAL
, "`^' operator may only be applied to"
394 if (is_just_unknown(e
) || is_just_unknown(f
))
397 e
= scalarvect (reloc_value(e
) ^ reloc_value(f
));
402 static expr
*expr2(int critical
) {
409 i
= scan(scpriv
, tokval
);
413 if (!(is_simple(e
) || is_just_unknown(e
)) ||
414 !(is_simple(f
) || is_just_unknown(f
))) {
415 error(ERR_NONFATAL
, "`&' operator may only be applied to"
418 if (is_just_unknown(e
) || is_just_unknown(f
))
421 e
= scalarvect (reloc_value(e
) & reloc_value(f
));
426 static expr
*expr3(int critical
) {
432 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
434 i
= scan(scpriv
, tokval
);
438 if (!(is_simple(e
) || is_just_unknown(e
)) ||
439 !(is_simple(f
) || is_just_unknown(f
))) {
440 error(ERR_NONFATAL
, "shift operator may only be applied to"
442 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
446 e
= scalarvect (reloc_value(e
) << reloc_value(f
));
449 e
= scalarvect (((unsigned long)reloc_value(e
)) >>
457 static expr
*expr4(int critical
) {
463 while (i
== '+' || i
== '-') {
465 i
= scan(scpriv
, tokval
);
471 e
= add_vectors (e
, f
);
474 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
481 static expr
*expr5(int critical
) {
487 while (i
== '*' || i
== '/' || i
== '%' ||
488 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
490 i
= scan(scpriv
, tokval
);
494 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
495 !(is_simple(f
) || is_just_unknown(f
)))) {
496 error(ERR_NONFATAL
, "division operator may only be applied to"
500 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
501 error(ERR_NONFATAL
, "division by zero");
507 e
= scalar_mult (f
, reloc_value(e
), TRUE
);
508 else if (is_simple(f
))
509 e
= scalar_mult (e
, reloc_value(f
), TRUE
);
510 else if (is_just_unknown(e
) && is_just_unknown(f
))
513 error(ERR_NONFATAL
, "unable to multiply two "
514 "non-scalar objects");
519 if (is_just_unknown(e
) || is_just_unknown(f
))
522 e
= scalarvect (((unsigned long)reloc_value(e
)) /
523 ((unsigned long)reloc_value(f
)));
526 if (is_just_unknown(e
) || is_just_unknown(f
))
529 e
= scalarvect (((unsigned long)reloc_value(e
)) %
530 ((unsigned long)reloc_value(f
)));
533 if (is_just_unknown(e
) || is_just_unknown(f
))
536 e
= scalarvect (((signed long)reloc_value(e
)) /
537 ((signed long)reloc_value(f
)));
540 if (is_just_unknown(e
) || is_just_unknown(f
))
543 e
= scalarvect (((signed long)reloc_value(e
)) %
544 ((signed long)reloc_value(f
)));
551 static expr
*expr6(int critical
) {
554 long label_seg
, label_ofs
;
557 i
= scan(scpriv
, tokval
);
561 return scalar_mult (e
, -1L, FALSE
);
562 } else if (i
== '+') {
563 i
= scan(scpriv
, tokval
);
564 return expr6(critical
);
565 } else if (i
== '~') {
566 i
= scan(scpriv
, tokval
);
570 if (is_just_unknown(e
))
571 return unknown_expr();
572 else if (!is_simple(e
)) {
573 error(ERR_NONFATAL
, "`~' operator may only be applied to"
577 return scalarvect(~reloc_value(e
));
578 } else if (i
== TOKEN_SEG
) {
579 i
= scan(scpriv
, tokval
);
584 if (is_unknown(e
) && critical
) {
585 error(ERR_NONFATAL
, "unable to determine segment base");
589 } else if (i
== '(') {
590 i
= scan(scpriv
, tokval
);
595 error(ERR_NONFATAL
, "expecting `)'");
598 i
= scan(scpriv
, tokval
);
600 } else if (i
== TOKEN_NUM
|| i
== TOKEN_REG
|| i
== TOKEN_ID
||
601 i
== TOKEN_HERE
|| i
== TOKEN_BASE
) {
605 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
608 addtotemp(tokval
->t_integer
, 1L);
609 if (hint
&& hint
->type
== EAH_NOHINT
)
610 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
616 * If "label" begins with "%", this indicates that no
617 * symbol, Here or Base references are valid because we
618 * are in preprocess-only mode.
622 "%s not supported in preprocess-only mode",
623 (i
== TOKEN_ID
? "symbol references" :
624 i
== TOKEN_HERE
? "`$'" : "`$$'"));
625 addtotemp(EXPR_UNKNOWN
, 1L);
630 * Since the whole line is parsed before the label it
631 * defines is given to the label manager, we have
632 * problems with lines such as
634 * end: TIMES 512-(end-start) DB 0
636 * where `end' is not known on pass one, despite not
637 * really being a forward reference, and due to
638 * criticality it is _needed_. Hence we check our label
639 * against the currently defined one, and do our own
640 * resolution of it if we have to.
642 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
643 if (i
== TOKEN_BASE
) {
646 } else if (i
== TOKEN_HERE
|| !strcmp(tokval
->t_charptr
, label
)) {
649 } else if (!labelfunc(tokval
->t_charptr
,&label_seg
,&label_ofs
)) {
651 error (ERR_NONFATAL
, "symbol `%s' undefined",
654 } else if (critical
== 1) {
655 error (ERR_NONFATAL
, "symbol `%s' not defined before use",
666 addtotemp(type
, label_ofs
);
667 if (label_seg
!=NO_SEG
)
668 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
671 i
= scan(scpriv
, tokval
);
674 error(ERR_NONFATAL
, "expression syntax error");
679 void eval_global_info (struct ofmt
*output
, lfunc lookup_label
) {
681 labelfunc
= lookup_label
;
684 void eval_info (char *labelname
, long segment
, long offset
) {
685 if (label
!= special_empty_string
)
688 label
= nasm_strdup(labelname
);
690 label
= special_empty_string
;
696 expr
*evaluate (scanner sc
, void *scprivate
, struct tokenval
*tv
,
697 int *fwref
, int critical
, efunc report_error
,
698 struct eval_hints
*hints
) {
704 hint
->type
= EAH_NOHINT
;
706 if (critical
& 0x10) {
715 error
= report_error
;
718 if (tokval
->t_type
== TOKEN_INVALID
)
719 i
= scan(scpriv
, tokval
);
723 while (ntempexprs
) /* initialise temporary storage */
724 nasm_free (tempexprs
[--ntempexprs
]);
726 e
= bexpr (critical
);
730 if (i
== TOKEN_WRT
) {
731 i
= scan(scpriv
, tokval
); /* eat the WRT */
732 f
= expr6 (critical
);
736 e
= scalar_mult (e
, 1L, FALSE
); /* strip far-absolute segment part */
739 if (is_just_unknown(f
))
745 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
748 value
= reloc_seg(f
);
750 value
= reloc_value(f
) | SEG_ABS
;
751 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
752 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
755 addtotemp(EXPR_WRT
, value
);
758 e
= add_vectors (e
, g
);