]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/eval.c
cfd02ff30478584b63fcdf591502e56c0952c836
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* eval.c expression evaluator for the Netwide Assembler
27 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
28 * Julian Hall. All rights reserved. The software is
29 * redistributable under the licence given in the file "Licence"
30 * distributed in the NASM archive.
32 * initial version 27/iii/95 by Simon Tatham
45 static expr
**tempexprs
= NULL
;
46 static int ntempexprs
, tempexprs_size
= 0;
47 #define TEMPEXPRS_DELTA 128
49 static expr
*tempexpr
;
50 static int ntempexpr
, tempexpr_size
;
51 #define TEMPEXPR_DELTA 8
55 static struct tokenval
*tokval
;
59 static char *label
= NULL
, special_empty_string
[] = "";
60 static lfunc labelfunc
;
61 static struct ofmt
*outfmt
;
64 static struct eval_hints
*hint
;
67 * Construct a temporary expression.
69 static void begintemp(void) {
71 tempexpr_size
= ntempexpr
= 0;
74 static void addtotemp(long type
, long 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) {
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
) {
102 preserve
= is_really_simple(p
) || is_really_simple(q
);
106 while (p
->type
&& q
->type
&&
107 p
->type
< EXPR_SEGBASE
+SEG_ABS
&&
108 q
->type
< EXPR_SEGBASE
+SEG_ABS
) {
111 if (p
->type
> q
->type
) {
112 addtotemp(q
->type
, q
->value
);
113 lasttype
= q
++->type
;
114 } else if (p
->type
< q
->type
) {
115 addtotemp(p
->type
, p
->value
);
116 lasttype
= p
++->type
;
117 } else { /* *p and *q have same type */
118 addtotemp(p
->type
, p
->value
+ q
->value
);
122 if (lasttype
== EXPR_UNKNOWN
) {
127 (preserve
|| p
->type
< EXPR_SEGBASE
+SEG_ABS
)) {
128 addtotemp(p
->type
, p
->value
);
132 (preserve
|| q
->type
< EXPR_SEGBASE
+SEG_ABS
)) {
133 addtotemp(q
->type
, q
->value
);
141 * Multiply a vector by a scalar. Strip far-absolute segment part
144 * Explicit treatment of UNKNOWN is not required in this routine,
145 * since it will silently do the Right Thing anyway.
147 * If `affect_hints' is set, we also change the hint type to
148 * NOTBASE if a MAKEBASE hint points at a register being
149 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
150 * as the base register.
152 static expr
*scalar_mult(expr
*vect
, long scalar
, int affect_hints
) {
155 while (p
->type
&& p
->type
< EXPR_SEGBASE
+SEG_ABS
) {
156 p
->value
= scalar
* (p
->value
);
157 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
158 p
->type
== hint
->base
&& affect_hints
)
159 hint
->type
= EAH_NOTBASE
;
167 static expr
*scalarvect (long scalar
) {
169 addtotemp(EXPR_SIMPLE
, scalar
);
173 static expr
*unknown_expr (void) {
175 addtotemp(EXPR_UNKNOWN
, 1L);
180 * The SEG operator: calculate the segment part of a relocatable
181 * value. Return NULL, as usual, if an error occurs. Report the
184 static expr
*segment_part (expr
*e
) {
188 return unknown_expr();
191 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
197 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
199 } else if (seg
& SEG_ABS
) {
200 return scalarvect(seg
& ~SEG_ABS
);
201 } else if (seg
& 1) {
202 error(ERR_NONFATAL
, "SEG applied to something which"
203 " is already a segment base");
207 long base
= outfmt
->segbase(seg
+1);
210 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+base
), 1L);
216 * Recursive-descent parser. Called with a single boolean operand,
217 * which is TRUE if the evaluation is critical (i.e. unresolved
218 * symbols are an error condition). Must update the global `i' to
219 * reflect the token after the parsed string. May return NULL.
221 * evaluate() should report its own errors: on return it is assumed
222 * that if NULL has been returned, the error has already been
229 * expr : bexpr [ WRT expr6 ]
230 * bexpr : rexp0 or expr0 depending on relative-mode setting
231 * rexp0 : rexp1 [ {||} rexp1...]
232 * rexp1 : rexp2 [ {^^} rexp2...]
233 * rexp2 : rexp3 [ {&&} rexp3...]
234 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
235 * expr0 : expr1 [ {|} expr1...]
236 * expr1 : expr2 [ {^} expr2...]
237 * expr2 : expr3 [ {&} expr3...]
238 * expr3 : expr4 [ {<<,>>} expr4...]
239 * expr4 : expr5 [ {+,-} expr5...]
240 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
241 * expr6 : { ~,+,-,SEG } expr6
248 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
250 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
251 static expr
*expr4(int), *expr5(int), *expr6(int);
253 static expr
*(*bexpr
)(int);
255 static expr
*rexp0(int critical
) {
261 while (i
== TOKEN_DBL_OR
) {
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
*rexp1(int critical
) {
285 while (i
== TOKEN_DBL_XOR
) {
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
*rexp2(int critical
) {
309 while (i
== TOKEN_DBL_AND
) {
310 i
= scan(scpriv
, tokval
);
314 if (!(is_simple(e
) || is_just_unknown(e
)) ||
315 !(is_simple(f
) || is_just_unknown(f
))) {
316 error(ERR_NONFATAL
, "`&' operator may only be applied to"
319 if (is_just_unknown(e
) || is_just_unknown(f
))
322 e
= scalarvect ((long) (reloc_value(e
) && reloc_value(f
)));
327 static expr
*rexp3(int critical
) {
334 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
335 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
337 i
= scan(scpriv
, tokval
);
341 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
343 case TOKEN_EQ
: case TOKEN_NE
:
345 v
= -1; /* means unknown */
346 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
347 v
= (j
== TOKEN_NE
); /* unequal, so return TRUE if NE */
349 v
= (j
== TOKEN_EQ
); /* equal, so return TRUE if EQ */
353 v
= -1; /* means unknown */
354 else if (!is_really_simple(e
)) {
355 error(ERR_NONFATAL
, "`%s': operands differ by a non-scalar",
356 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" :
357 j
== TOKEN_GE
? ">=" : ">"));
358 v
= 0; /* must set it to _something_ */
360 int vv
= reloc_value(e
);
362 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
364 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
366 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
378 static expr
*expr0(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
*expr1(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
*expr2(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
*expr3(int critical
) {
456 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
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
, "shift operator may only be applied to"
466 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
470 e
= scalarvect (reloc_value(e
) << reloc_value(f
));
473 e
= scalarvect (((unsigned long)reloc_value(e
)) >>
481 static expr
*expr4(int critical
) {
487 while (i
== '+' || i
== '-') {
489 i
= scan(scpriv
, tokval
);
495 e
= add_vectors (e
, f
);
498 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
505 static expr
*expr5(int critical
) {
511 while (i
== '*' || i
== '/' || i
== '%' ||
512 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
514 i
= scan(scpriv
, tokval
);
518 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
519 !(is_simple(f
) || is_just_unknown(f
)))) {
520 error(ERR_NONFATAL
, "division operator may only be applied to"
524 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
525 error(ERR_NONFATAL
, "division by zero");
531 e
= scalar_mult (f
, reloc_value(e
), TRUE
);
532 else if (is_simple(f
))
533 e
= scalar_mult (e
, reloc_value(f
), TRUE
);
534 else if (is_just_unknown(e
) && is_just_unknown(f
))
537 error(ERR_NONFATAL
, "unable to multiply two "
538 "non-scalar objects");
543 if (is_just_unknown(e
) || is_just_unknown(f
))
546 e
= scalarvect (((unsigned long)reloc_value(e
)) /
547 ((unsigned long)reloc_value(f
)));
550 if (is_just_unknown(e
) || is_just_unknown(f
))
553 e
= scalarvect (((unsigned long)reloc_value(e
)) %
554 ((unsigned long)reloc_value(f
)));
557 if (is_just_unknown(e
) || is_just_unknown(f
))
560 e
= scalarvect (((signed long)reloc_value(e
)) /
561 ((signed long)reloc_value(f
)));
564 if (is_just_unknown(e
) || is_just_unknown(f
))
567 e
= scalarvect (((signed long)reloc_value(e
)) %
568 ((signed long)reloc_value(f
)));
575 static expr
*expr6(int critical
) {
578 long label_seg
, label_ofs
;
581 i
= scan(scpriv
, tokval
);
585 return scalar_mult (e
, -1L, FALSE
);
586 } else if (i
== '+') {
587 i
= scan(scpriv
, tokval
);
588 return expr6(critical
);
589 } else if (i
== '~') {
590 i
= scan(scpriv
, tokval
);
594 if (is_just_unknown(e
))
595 return unknown_expr();
596 else if (!is_simple(e
)) {
597 error(ERR_NONFATAL
, "`~' operator may only be applied to"
601 return scalarvect(~reloc_value(e
));
602 } else if (i
== TOKEN_SEG
) {
603 i
= scan(scpriv
, tokval
);
608 if (is_unknown(e
) && critical
) {
609 error(ERR_NONFATAL
, "unable to determine segment base");
613 } else if (i
== '(') {
614 i
= scan(scpriv
, tokval
);
619 error(ERR_NONFATAL
, "expecting `)'");
622 i
= scan(scpriv
, tokval
);
624 } else if (i
== TOKEN_NUM
|| i
== TOKEN_REG
|| i
== TOKEN_ID
||
625 i
== TOKEN_HERE
|| i
== TOKEN_BASE
) {
629 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
632 addtotemp(tokval
->t_integer
, 1L);
633 if (hint
&& hint
->type
== EAH_NOHINT
)
634 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
640 * If "label" begins with "%", this indicates that no
641 * symbol, Here or Base references are valid because we
642 * are in preprocess-only mode.
646 "%s not supported in preprocess-only mode",
647 (i
== TOKEN_ID
? "symbol references" :
648 i
== TOKEN_HERE
? "`$'" : "`$$'"));
649 addtotemp(EXPR_UNKNOWN
, 1L);
654 * Since the whole line is parsed before the label it
655 * defines is given to the label manager, we have
656 * problems with lines such as
658 * end: TIMES 512-(end-start) DB 0
660 * where `end' is not known on pass one, despite not
661 * really being a forward reference, and due to
662 * criticality it is _needed_. Hence we check our label
663 * against the currently defined one, and do our own
664 * resolution of it if we have to.
666 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
667 if (i
== TOKEN_BASE
) {
670 } else if (i
== TOKEN_HERE
|| !strcmp(tokval
->t_charptr
, label
)) {
673 } else if (!labelfunc(tokval
->t_charptr
,&label_seg
,&label_ofs
)) {
675 error (ERR_NONFATAL
, "symbol `%s' undefined",
678 } else if (critical
== 1) {
679 error (ERR_NONFATAL
, "symbol `%s' not defined before use",
690 addtotemp(type
, label_ofs
);
691 if (label_seg
!=NO_SEG
)
692 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
695 i
= scan(scpriv
, tokval
);
698 error(ERR_NONFATAL
, "expression syntax error");
703 void eval_global_info (struct ofmt
*output
, lfunc lookup_label
) {
705 labelfunc
= lookup_label
;
708 void eval_info (char *labelname
, long segment
, long offset
) {
709 if (label
!= special_empty_string
)
712 label
= nasm_strdup(labelname
);
714 label
= special_empty_string
;
720 expr
*evaluate (scanner sc
, void *scprivate
, struct tokenval
*tv
,
721 int *fwref
, int critical
, efunc report_error
,
722 struct eval_hints
*hints
) {
728 hint
->type
= EAH_NOHINT
;
730 if (critical
& 0x10) {
739 error
= report_error
;
742 if (tokval
->t_type
== TOKEN_INVALID
)
743 i
= scan(scpriv
, tokval
);
747 while (ntempexprs
) /* initialise temporary storage */
748 nasm_free (tempexprs
[--ntempexprs
]);
750 e
= bexpr (critical
);
754 if (i
== TOKEN_WRT
) {
755 i
= scan(scpriv
, tokval
); /* eat the WRT */
756 f
= expr6 (critical
);
760 e
= scalar_mult (e
, 1L, FALSE
); /* strip far-absolute segment part */
763 if (is_just_unknown(f
))
769 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
772 value
= reloc_seg(f
);
774 value
= reloc_value(f
) | SEG_ABS
;
775 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
776 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
779 addtotemp(EXPR_WRT
, value
);
782 e
= add_vectors (e
, g
);