]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/eval.c
f6020afb331a2359aadb335ed73d7d05df754f33
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* eval.c expression evaluator for the Netwide Assembler
26 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
27 * Julian Hall. All rights reserved. The software is
28 * redistributable under the licence given in the file "Licence"
29 * distributed in the NASM archive.
31 * initial version 27/iii/95 by Simon Tatham
44 static expr
**tempexprs
= NULL
;
45 static int ntempexprs
, tempexprs_size
= 0;
46 #define TEMPEXPRS_DELTA 128
48 static expr
*tempexpr
;
49 static int ntempexpr
, tempexpr_size
;
50 #define TEMPEXPR_DELTA 8
54 static struct tokenval
*tokval
;
58 static char *label
= NULL
, special_empty_string
[] = "";
59 static lfunc labelfunc
;
60 static struct ofmt
*outfmt
;
63 static struct eval_hints
*hint
;
66 * Construct a temporary expression.
68 static void begintemp(void) {
70 tempexpr_size
= ntempexpr
= 0;
73 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) {
84 addtotemp (0L, 0L); /* terminate */
85 while (ntempexprs
>= tempexprs_size
) {
86 tempexprs_size
+= TEMPEXPRS_DELTA
;
87 tempexprs
= nasm_realloc(tempexprs
,
88 tempexprs_size
*sizeof(*tempexprs
));
90 return tempexprs
[ntempexprs
++] = tempexpr
;
94 * Add two vector datatypes. We have some bizarre behaviour on far-
95 * absolute segment types: we preserve them during addition _only_
96 * if one of the segments is a truly pure scalar.
98 static expr
*add_vectors(expr
*p
, expr
*q
) {
101 preserve
= is_really_simple(p
) || is_really_simple(q
);
105 while (p
->type
&& q
->type
&&
106 p
->type
< EXPR_SEGBASE
+SEG_ABS
&&
107 q
->type
< EXPR_SEGBASE
+SEG_ABS
) {
110 if (p
->type
> q
->type
) {
111 addtotemp(q
->type
, q
->value
);
112 lasttype
= q
++->type
;
113 } else if (p
->type
< q
->type
) {
114 addtotemp(p
->type
, p
->value
);
115 lasttype
= p
++->type
;
116 } else { /* *p and *q have same type */
117 addtotemp(p
->type
, p
->value
+ q
->value
);
121 if (lasttype
== EXPR_UNKNOWN
) {
126 (preserve
|| p
->type
< EXPR_SEGBASE
+SEG_ABS
)) {
127 addtotemp(p
->type
, p
->value
);
131 (preserve
|| q
->type
< EXPR_SEGBASE
+SEG_ABS
)) {
132 addtotemp(q
->type
, q
->value
);
140 * Multiply a vector by a scalar. Strip far-absolute segment part
143 * Explicit treatment of UNKNOWN is not required in this routine,
144 * since it will silently do the Right Thing anyway.
146 * If `affect_hints' is set, we also change the hint type to
147 * NOTBASE if a MAKEBASE hint points at a register being
148 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
149 * as the base register.
151 static expr
*scalar_mult(expr
*vect
, long scalar
, int affect_hints
) {
154 while (p
->type
&& p
->type
< EXPR_SEGBASE
+SEG_ABS
) {
155 p
->value
= scalar
* (p
->value
);
156 if (hint
&& hint
->type
== EAH_MAKEBASE
&&
157 p
->type
== hint
->base
&& affect_hints
)
158 hint
->type
= EAH_NOTBASE
;
166 static expr
*scalarvect (long scalar
) {
168 addtotemp(EXPR_SIMPLE
, scalar
);
172 static expr
*unknown_expr (void) {
174 addtotemp(EXPR_UNKNOWN
, 1L);
179 * The SEG operator: calculate the segment part of a relocatable
180 * value. Return NULL, as usual, if an error occurs. Report the
183 static expr
*segment_part (expr
*e
) {
187 return unknown_expr();
190 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
196 error(ERR_NONFATAL
, "cannot apply SEG to a non-relocatable value");
198 } else if (seg
& SEG_ABS
) {
199 return scalarvect(seg
& ~SEG_ABS
);
200 } else if (seg
& 1) {
201 error(ERR_NONFATAL
, "SEG applied to something which"
202 " is already a segment base");
206 long base
= outfmt
->segbase(seg
+1);
209 addtotemp((base
== NO_SEG
? EXPR_UNKNOWN
: EXPR_SEGBASE
+base
), 1L);
215 * Recursive-descent parser. Called with a single boolean operand,
216 * which is TRUE if the evaluation is critical (i.e. unresolved
217 * symbols are an error condition). Must update the global `i' to
218 * reflect the token after the parsed string. May return NULL.
220 * evaluate() should report its own errors: on return it is assumed
221 * that if NULL has been returned, the error has already been
228 * expr : bexpr [ WRT expr6 ]
229 * bexpr : rexp0 or expr0 depending on relative-mode setting
230 * rexp0 : rexp1 [ {||} rexp1...]
231 * rexp1 : rexp2 [ {^^} rexp2...]
232 * rexp2 : rexp3 [ {&&} rexp3...]
233 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
234 * expr0 : expr1 [ {|} expr1...]
235 * expr1 : expr2 [ {^} expr2...]
236 * expr2 : expr3 [ {&} expr3...]
237 * expr3 : expr4 [ {<<,>>} expr4...]
238 * expr4 : expr5 [ {+,-} expr5...]
239 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
240 * expr6 : { ~,+,-,SEG } expr6
247 static expr
*rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
249 static expr
*expr0(int), *expr1(int), *expr2(int), *expr3(int);
250 static expr
*expr4(int), *expr5(int), *expr6(int);
252 static expr
*(*bexpr
)(int);
254 static expr
*rexp0(int critical
) {
260 while (i
== TOKEN_DBL_OR
) {
261 i
= scan(scpriv
, tokval
);
265 if (!(is_simple(e
) || is_just_unknown(e
)) ||
266 !(is_simple(f
) || is_just_unknown(f
))) {
267 error(ERR_NONFATAL
, "`|' operator may only be applied to"
270 if (is_just_unknown(e
) || is_just_unknown(f
))
273 e
= scalarvect ((long) (reloc_value(e
) || reloc_value(f
)));
278 static expr
*rexp1(int critical
) {
284 while (i
== TOKEN_DBL_XOR
) {
285 i
= scan(scpriv
, tokval
);
289 if (!(is_simple(e
) || is_just_unknown(e
)) ||
290 !(is_simple(f
) || is_just_unknown(f
))) {
291 error(ERR_NONFATAL
, "`^' operator may only be applied to"
294 if (is_just_unknown(e
) || is_just_unknown(f
))
297 e
= scalarvect ((long) (!reloc_value(e
) ^ !reloc_value(f
)));
302 static expr
*rexp2(int critical
) {
308 while (i
== TOKEN_DBL_AND
) {
309 i
= scan(scpriv
, tokval
);
313 if (!(is_simple(e
) || is_just_unknown(e
)) ||
314 !(is_simple(f
) || is_just_unknown(f
))) {
315 error(ERR_NONFATAL
, "`&' operator may only be applied to"
318 if (is_just_unknown(e
) || is_just_unknown(f
))
321 e
= scalarvect ((long) (reloc_value(e
) && reloc_value(f
)));
326 static expr
*rexp3(int critical
) {
333 while (i
== TOKEN_EQ
|| i
== TOKEN_LT
|| i
== TOKEN_GT
||
334 i
== TOKEN_NE
|| i
== TOKEN_LE
|| i
== TOKEN_GE
) {
336 i
= scan(scpriv
, tokval
);
340 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
342 case TOKEN_EQ
: case TOKEN_NE
:
344 v
= -1; /* means unknown */
345 else if (!is_really_simple(e
) || reloc_value(e
) != 0)
346 v
= (j
== TOKEN_NE
); /* unequal, so return TRUE if NE */
348 v
= (j
== TOKEN_EQ
); /* equal, so return TRUE if EQ */
352 v
= -1; /* means unknown */
353 else if (!is_really_simple(e
)) {
354 error(ERR_NONFATAL
, "`%s': operands differ by a non-scalar",
355 (j
== TOKEN_LE
? "<=" : j
== TOKEN_LT
? "<" :
356 j
== TOKEN_GE
? ">=" : ">"));
357 v
= 0; /* must set it to _something_ */
359 int vv
= reloc_value(e
);
361 v
= (j
== TOKEN_LE
|| j
== TOKEN_GE
);
363 v
= (j
== TOKEN_GE
|| j
== TOKEN_GT
);
365 v
= (j
== TOKEN_LE
|| j
== TOKEN_LT
);
377 static expr
*expr0(int critical
) {
384 i
= scan(scpriv
, tokval
);
388 if (!(is_simple(e
) || is_just_unknown(e
)) ||
389 !(is_simple(f
) || is_just_unknown(f
))) {
390 error(ERR_NONFATAL
, "`|' operator may only be applied to"
393 if (is_just_unknown(e
) || is_just_unknown(f
))
396 e
= scalarvect (reloc_value(e
) | reloc_value(f
));
401 static expr
*expr1(int critical
) {
408 i
= scan(scpriv
, tokval
);
412 if (!(is_simple(e
) || is_just_unknown(e
)) ||
413 !(is_simple(f
) || is_just_unknown(f
))) {
414 error(ERR_NONFATAL
, "`^' operator may only be applied to"
417 if (is_just_unknown(e
) || is_just_unknown(f
))
420 e
= scalarvect (reloc_value(e
) ^ reloc_value(f
));
425 static expr
*expr2(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
*expr3(int critical
) {
455 while (i
== TOKEN_SHL
|| i
== TOKEN_SHR
) {
457 i
= scan(scpriv
, tokval
);
461 if (!(is_simple(e
) || is_just_unknown(e
)) ||
462 !(is_simple(f
) || is_just_unknown(f
))) {
463 error(ERR_NONFATAL
, "shift operator may only be applied to"
465 } else if (is_just_unknown(e
) || is_just_unknown(f
)) {
469 e
= scalarvect (reloc_value(e
) << reloc_value(f
));
472 e
= scalarvect (((unsigned long)reloc_value(e
)) >>
480 static expr
*expr4(int critical
) {
486 while (i
== '+' || i
== '-') {
488 i
= scan(scpriv
, tokval
);
494 e
= add_vectors (e
, f
);
497 e
= add_vectors (e
, scalar_mult(f
, -1L, FALSE
));
504 static expr
*expr5(int critical
) {
510 while (i
== '*' || i
== '/' || i
== '%' ||
511 i
== TOKEN_SDIV
|| i
== TOKEN_SMOD
) {
513 i
= scan(scpriv
, tokval
);
517 if (j
!= '*' && (!(is_simple(e
) || is_just_unknown(e
)) ||
518 !(is_simple(f
) || is_just_unknown(f
)))) {
519 error(ERR_NONFATAL
, "division operator may only be applied to"
523 if (j
!= '*' && !is_unknown(f
) && reloc_value(f
) == 0) {
524 error(ERR_NONFATAL
, "division by zero");
530 e
= scalar_mult (f
, reloc_value(e
), TRUE
);
531 else if (is_simple(f
))
532 e
= scalar_mult (e
, reloc_value(f
), TRUE
);
533 else if (is_just_unknown(e
) && is_just_unknown(f
))
536 error(ERR_NONFATAL
, "unable to multiply two "
537 "non-scalar objects");
542 if (is_just_unknown(e
) || is_just_unknown(f
))
545 e
= scalarvect (((unsigned long)reloc_value(e
)) /
546 ((unsigned long)reloc_value(f
)));
549 if (is_just_unknown(e
) || is_just_unknown(f
))
552 e
= scalarvect (((unsigned long)reloc_value(e
)) %
553 ((unsigned long)reloc_value(f
)));
556 if (is_just_unknown(e
) || is_just_unknown(f
))
559 e
= scalarvect (((signed long)reloc_value(e
)) /
560 ((signed long)reloc_value(f
)));
563 if (is_just_unknown(e
) || is_just_unknown(f
))
566 e
= scalarvect (((signed long)reloc_value(e
)) %
567 ((signed long)reloc_value(f
)));
574 static expr
*expr6(int critical
) {
577 long label_seg
, label_ofs
;
580 i
= scan(scpriv
, tokval
);
584 return scalar_mult (e
, -1L, FALSE
);
585 } else if (i
== '+') {
586 i
= scan(scpriv
, tokval
);
587 return expr6(critical
);
588 } else if (i
== '~') {
589 i
= scan(scpriv
, tokval
);
593 if (is_just_unknown(e
))
594 return unknown_expr();
595 else if (!is_simple(e
)) {
596 error(ERR_NONFATAL
, "`~' operator may only be applied to"
600 return scalarvect(~reloc_value(e
));
601 } else if (i
== TOKEN_SEG
) {
602 i
= scan(scpriv
, tokval
);
607 if (is_unknown(e
) && critical
) {
608 error(ERR_NONFATAL
, "unable to determine segment base");
612 } else if (i
== '(') {
613 i
= scan(scpriv
, tokval
);
618 error(ERR_NONFATAL
, "expecting `)'");
621 i
= scan(scpriv
, tokval
);
623 } else if (i
== TOKEN_NUM
|| i
== TOKEN_REG
|| i
== TOKEN_ID
||
624 i
== TOKEN_HERE
|| i
== TOKEN_BASE
) {
628 addtotemp(EXPR_SIMPLE
, tokval
->t_integer
);
631 addtotemp(tokval
->t_integer
, 1L);
632 if (hint
&& hint
->type
== EAH_NOHINT
)
633 hint
->base
= tokval
->t_integer
, hint
->type
= EAH_MAKEBASE
;
639 * If "label" begins with "%", this indicates that no
640 * symbol, Here or Base references are valid because we
641 * are in preprocess-only mode.
645 "%s not supported in preprocess-only mode",
646 (i
== TOKEN_ID
? "symbol references" :
647 i
== TOKEN_HERE
? "`$'" : "`$$'"));
648 addtotemp(EXPR_UNKNOWN
, 1L);
653 * Since the whole line is parsed before the label it
654 * defines is given to the label manager, we have
655 * problems with lines such as
657 * end: TIMES 512-(end-start) DB 0
659 * where `end' is not known on pass one, despite not
660 * really being a forward reference, and due to
661 * criticality it is _needed_. Hence we check our label
662 * against the currently defined one, and do our own
663 * resolution of it if we have to.
665 type
= EXPR_SIMPLE
; /* might get overridden by UNKNOWN */
666 if (i
== TOKEN_BASE
) {
669 } else if (i
== TOKEN_HERE
|| !strcmp(tokval
->t_charptr
, label
)) {
672 } else if (!labelfunc(tokval
->t_charptr
,&label_seg
,&label_ofs
)) {
674 error (ERR_NONFATAL
, "symbol `%s' undefined",
677 } else if (critical
== 1) {
678 error (ERR_NONFATAL
, "symbol `%s' not defined before use",
689 addtotemp(type
, label_ofs
);
690 if (label_seg
!=NO_SEG
)
691 addtotemp(EXPR_SEGBASE
+ label_seg
, 1L);
694 i
= scan(scpriv
, tokval
);
697 error(ERR_NONFATAL
, "expression syntax error");
702 void eval_global_info (struct ofmt
*output
, lfunc lookup_label
) {
704 labelfunc
= lookup_label
;
707 void eval_info (char *labelname
, long segment
, long offset
) {
708 if (label
!= special_empty_string
)
711 label
= nasm_strdup(labelname
);
713 label
= special_empty_string
;
719 expr
*evaluate (scanner sc
, void *scprivate
, struct tokenval
*tv
,
720 int *fwref
, int critical
, efunc report_error
,
721 struct eval_hints
*hints
) {
727 hint
->type
= EAH_NOHINT
;
729 if (critical
& 0x10) {
738 error
= report_error
;
741 if (tokval
->t_type
== TOKEN_INVALID
)
742 i
= scan(scpriv
, tokval
);
746 while (ntempexprs
) /* initialise temporary storage */
747 nasm_free (tempexprs
[--ntempexprs
]);
749 e
= bexpr (critical
);
753 if (i
== TOKEN_WRT
) {
754 i
= scan(scpriv
, tokval
); /* eat the WRT */
755 f
= expr6 (critical
);
759 e
= scalar_mult (e
, 1L, FALSE
); /* strip far-absolute segment part */
762 if (is_just_unknown(f
))
768 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
771 value
= reloc_seg(f
);
773 value
= reloc_value(f
) | SEG_ABS
;
774 else if (!(value
& SEG_ABS
) && !(value
% 2) && critical
) {
775 error(ERR_NONFATAL
, "invalid right-hand operand to WRT");
778 addtotemp(EXPR_WRT
, value
);
781 e
= add_vectors (e
, g
);