%{_arch} doesn't work with newer RPMs; use %{_target_cpu} instead
[nasm/avx512.git] / eval.c
blob28aca642b12ea87d027f7135e8153ee58660c3c5
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
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <ctype.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "eval.h"
20 #include "labels.h"
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;
36 static int ntempexpr;
37 static int tempexpr_size;
39 static struct tokenval *tokval; /* The current token */
40 static int i; /* The t_type of tokval */
42 static void *scpriv;
43 static loc_t *location; /* Pointer to current line's segment,offset */
44 static int *opflags;
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)
58 while (ntempexprs)
59 nasm_free (tempexprs[--ntempexprs]);
60 nasm_free (tempexprs);
64 * Construct a temporary expression.
66 static void begintemp(void)
68 tempexpr = NULL;
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)
101 int preserve;
103 preserve = is_really_simple(p) || is_really_simple(q);
105 begintemp();
107 while (p->type && q->type &&
108 p->type < EXPR_SEGBASE+SEG_ABS &&
109 q->type < EXPR_SEGBASE+SEG_ABS)
111 int lasttype;
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;
121 if (sum)
122 addtotemp(p->type, sum);
123 lasttype = p->type;
124 p++, q++;
126 if (lasttype == EXPR_UNKNOWN) {
127 return finishtemp();
130 while (p->type &&
131 (preserve || p->type < EXPR_SEGBASE+SEG_ABS))
133 addtotemp(p->type, p->value);
134 p++;
136 while (q->type &&
137 (preserve || q->type < EXPR_SEGBASE+SEG_ABS))
139 addtotemp(q->type, q->value);
140 q++;
143 return finishtemp();
147 * Multiply a vector by a scalar. Strip far-absolute segment part
148 * if present.
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)
160 expr *p = vect;
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;
167 p++;
169 p->type = 0;
171 return vect;
174 static expr *scalarvect (long scalar)
176 begintemp();
177 addtotemp(EXPR_SIMPLE, scalar);
178 return finishtemp();
181 static expr *unknown_expr (void)
183 begintemp();
184 addtotemp(EXPR_UNKNOWN, 1L);
185 return finishtemp();
189 * The SEG operator: calculate the segment part of a relocatable
190 * value. Return NULL, as usual, if an error occurs. Report the
191 * error too.
193 static expr *segment_part (expr *e)
195 long seg;
197 if (is_unknown(e))
198 return unknown_expr();
200 if (!is_reloc(e)) {
201 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
202 return NULL;
205 seg = reloc_seg(e);
206 if (seg == NO_SEG) {
207 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
208 return NULL;
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");
214 return NULL;
216 else {
217 long base = outfmt->segbase(seg+1);
219 begintemp();
220 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE+base), 1L);
221 return finishtemp();
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
233 * reported.
237 * Grammar parsed is:
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
252 * | (bexpr)
253 * | symbol
254 * | $
255 * | number
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)
267 expr *e, *f;
269 e = rexp1(critical);
270 if (!e)
271 return NULL;
273 while (i == TOKEN_DBL_OR)
275 i = scan(scpriv, tokval);
276 f = rexp1(critical);
277 if (!f)
278 return NULL;
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"
283 " scalar values");
286 if (is_just_unknown(e) || is_just_unknown(f))
287 e = unknown_expr();
288 else
289 e = scalarvect ((long) (reloc_value(e) || reloc_value(f)));
291 return e;
294 static expr *rexp1(int critical)
296 expr *e, *f;
298 e = rexp2(critical);
299 if (!e)
300 return NULL;
302 while (i == TOKEN_DBL_XOR)
304 i = scan(scpriv, tokval);
305 f = rexp2(critical);
306 if (!f)
307 return NULL;
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"
312 " scalar values");
315 if (is_just_unknown(e) || is_just_unknown(f))
316 e = unknown_expr();
317 else
318 e = scalarvect ((long) (!reloc_value(e) ^ !reloc_value(f)));
320 return e;
323 static expr *rexp2(int critical)
325 expr *e, *f;
327 e = rexp3(critical);
328 if (!e)
329 return NULL;
330 while (i == TOKEN_DBL_AND)
332 i = scan(scpriv, tokval);
333 f = rexp3(critical);
334 if (!f)
335 return NULL;
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"
340 " scalar values");
342 if (is_just_unknown(e) || is_just_unknown(f))
343 e = unknown_expr();
344 else
345 e = scalarvect ((long) (reloc_value(e) && reloc_value(f)));
347 return e;
350 static expr *rexp3(int critical)
352 expr *e, *f;
353 long v;
355 e = expr0(critical);
356 if (!e)
357 return NULL;
359 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
360 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE)
362 int j = i;
363 i = scan(scpriv, tokval);
364 f = expr0(critical);
365 if (!f)
366 return NULL;
368 e = add_vectors (e, scalar_mult(f, -1L, FALSE));
370 switch (j)
372 case TOKEN_EQ: case TOKEN_NE:
373 if (is_unknown(e))
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 */
377 else
378 v = (j == TOKEN_EQ); /* equal, so return TRUE if EQ */
379 break;
380 default:
381 if (is_unknown(e))
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_ */
388 } else {
389 int vv = reloc_value(e);
390 if (vv == 0)
391 v = (j == TOKEN_LE || j == TOKEN_GE);
392 else if (vv > 0)
393 v = (j == TOKEN_GE || j == TOKEN_GT);
394 else /* vv < 0 */
395 v = (j == TOKEN_LE || j == TOKEN_LT);
397 break;
400 if (v == -1)
401 e = unknown_expr();
402 else
403 e = scalarvect(v);
405 return e;
408 static expr *expr0(int critical)
410 expr *e, *f;
412 e = expr1(critical);
413 if (!e)
414 return NULL;
416 while (i == '|')
418 i = scan(scpriv, tokval);
419 f = expr1(critical);
420 if (!f)
421 return NULL;
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"
426 " scalar values");
428 if (is_just_unknown(e) || is_just_unknown(f))
429 e = unknown_expr();
430 else
431 e = scalarvect (reloc_value(e) | reloc_value(f));
433 return e;
436 static expr *expr1(int critical)
438 expr *e, *f;
440 e = expr2(critical);
441 if (!e)
442 return NULL;
444 while (i == '^') {
445 i = scan(scpriv, tokval);
446 f = expr2(critical);
447 if (!f)
448 return NULL;
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"
453 " scalar values");
455 if (is_just_unknown(e) || is_just_unknown(f))
456 e = unknown_expr();
457 else
458 e = scalarvect (reloc_value(e) ^ reloc_value(f));
460 return e;
463 static expr *expr2(int critical)
465 expr *e, *f;
467 e = expr3(critical);
468 if (!e)
469 return NULL;
471 while (i == '&') {
472 i = scan(scpriv, tokval);
473 f = expr3(critical);
474 if (!f)
475 return NULL;
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"
480 " scalar values");
482 if (is_just_unknown(e) || is_just_unknown(f))
483 e = unknown_expr();
484 else
485 e = scalarvect (reloc_value(e) & reloc_value(f));
487 return e;
490 static expr *expr3(int critical)
492 expr *e, *f;
494 e = expr4(critical);
495 if (!e)
496 return NULL;
498 while (i == TOKEN_SHL || i == TOKEN_SHR)
500 int j = i;
501 i = scan(scpriv, tokval);
502 f = expr4(critical);
503 if (!f)
504 return NULL;
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"
509 " scalar values");
510 } else if (is_just_unknown(e) || is_just_unknown(f)) {
511 e = unknown_expr();
512 } else switch (j) {
513 case TOKEN_SHL:
514 e = scalarvect (reloc_value(e) << reloc_value(f));
515 break;
516 case TOKEN_SHR:
517 e = scalarvect (((unsigned long)reloc_value(e)) >>
518 reloc_value(f));
519 break;
522 return e;
525 static expr *expr4(int critical)
527 expr *e, *f;
529 e = expr5(critical);
530 if (!e)
531 return NULL;
532 while (i == '+' || i == '-')
534 int j = i;
535 i = scan(scpriv, tokval);
536 f = expr5(critical);
537 if (!f)
538 return NULL;
539 switch (j) {
540 case '+':
541 e = add_vectors (e, f);
542 break;
543 case '-':
544 e = add_vectors (e, scalar_mult(f, -1L, FALSE));
545 break;
548 return e;
551 static expr *expr5(int critical)
553 expr *e, *f;
555 e = expr6(critical);
556 if (!e)
557 return NULL;
558 while (i == '*' || i == '/' || i == '%' ||
559 i == TOKEN_SDIV || i == TOKEN_SMOD)
561 int j = i;
562 i = scan(scpriv, tokval);
563 f = expr6(critical);
564 if (!f)
565 return NULL;
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"
570 " scalar values");
571 return NULL;
573 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
574 error(ERR_NONFATAL, "division by zero");
575 return NULL;
577 switch (j) {
578 case '*':
579 if (is_simple(e))
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))
584 e = unknown_expr();
585 else {
586 error(ERR_NONFATAL, "unable to multiply two "
587 "non-scalar objects");
588 return NULL;
590 break;
591 case '/':
592 if (is_just_unknown(e) || is_just_unknown(f))
593 e = unknown_expr();
594 else
595 e = scalarvect (((unsigned long)reloc_value(e)) /
596 ((unsigned long)reloc_value(f)));
597 break;
598 case '%':
599 if (is_just_unknown(e) || is_just_unknown(f))
600 e = unknown_expr();
601 else
602 e = scalarvect (((unsigned long)reloc_value(e)) %
603 ((unsigned long)reloc_value(f)));
604 break;
605 case TOKEN_SDIV:
606 if (is_just_unknown(e) || is_just_unknown(f))
607 e = unknown_expr();
608 else
609 e = scalarvect (((signed long)reloc_value(e)) /
610 ((signed long)reloc_value(f)));
611 break;
612 case TOKEN_SMOD:
613 if (is_just_unknown(e) || is_just_unknown(f))
614 e = unknown_expr();
615 else
616 e = scalarvect (((signed long)reloc_value(e)) %
617 ((signed long)reloc_value(f)));
618 break;
621 return e;
624 static expr *expr6(int critical)
626 long type;
627 expr *e;
628 long label_seg, label_ofs;
630 if (i == '-') {
631 i = scan(scpriv, tokval);
632 e = expr6(critical);
633 if (!e)
634 return NULL;
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);
641 e = expr6(critical);
642 if (!e)
643 return NULL;
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"
648 " scalar values");
649 return NULL;
651 return scalarvect(~reloc_value(e));
652 } else if (i == TOKEN_SEG) {
653 i = scan(scpriv, tokval);
654 e = expr6(critical);
655 if (!e)
656 return NULL;
657 e = segment_part(e);
658 if (!e)
659 return NULL;
660 if (is_unknown(e) && critical) {
661 error(ERR_NONFATAL, "unable to determine segment base");
662 return NULL;
664 return e;
665 } else if (i == '(') {
666 i = scan(scpriv, tokval);
667 e = bexpr(critical);
668 if (!e)
669 return NULL;
670 if (i != ')') {
671 error(ERR_NONFATAL, "expecting `)'");
672 return NULL;
674 i = scan(scpriv, tokval);
675 return e;
677 else if (i == TOKEN_NUM || i == TOKEN_REG || i == TOKEN_ID ||
678 i == TOKEN_HERE || i == TOKEN_BASE)
680 begintemp();
681 switch (i) {
682 case TOKEN_NUM:
683 addtotemp(EXPR_SIMPLE, tokval->t_integer);
684 break;
685 case TOKEN_REG:
686 addtotemp(tokval->t_integer, 1L);
687 if (hint && hint->type == EAH_NOHINT)
688 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
689 break;
690 case TOKEN_ID:
691 case TOKEN_HERE:
692 case TOKEN_BASE:
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) {
699 error(ERR_NONFATAL,
700 "%s not supported in preprocess-only mode",
701 (i == TOKEN_ID ? "symbol references" :
702 i == TOKEN_HERE ? "`$'" : "`$$'"));
703 addtotemp(EXPR_UNKNOWN, 1L);
704 break;
707 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
708 if (i == TOKEN_BASE)
710 label_seg = in_abs_seg ? abs_seg : location->segment;
711 label_ofs = 0;
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;
715 } else {
716 if (!labelfunc(tokval->t_charptr,&label_seg,&label_ofs))
718 if (critical == 2) {
719 error (ERR_NONFATAL, "symbol `%s' undefined",
720 tokval->t_charptr);
721 return NULL;
722 } else if (critical == 1) {
723 error (ERR_NONFATAL,
724 "symbol `%s' not defined before use",
725 tokval->t_charptr);
726 return NULL;
727 } else {
728 if (opflags)
729 *opflags |= 1;
730 type = EXPR_UNKNOWN;
731 label_seg = NO_SEG;
732 label_ofs = 1;
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);
741 break;
743 i = scan(scpriv, tokval);
744 return finishtemp();
745 } else {
746 error(ERR_NONFATAL, "expression syntax error");
747 return NULL;
751 void eval_global_info (struct ofmt *output, lfunc lookup_label, loc_t *locp)
753 outfmt = output;
754 labelfunc = lookup_label;
755 location = locp;
758 expr *evaluate (scanner sc, void *scprivate, struct tokenval *tv,
759 int *fwref, int critical, efunc report_error,
760 struct eval_hints *hints)
762 expr *e;
763 expr *f = NULL;
765 hint = hints;
766 if (hint)
767 hint->type = EAH_NOHINT;
769 if (critical & CRITICAL) {
770 critical &= ~CRITICAL;
771 bexpr = rexp0;
772 } else
773 bexpr = expr0;
775 scan = sc;
776 scpriv = scprivate;
777 tokval = tv;
778 error = report_error;
779 opflags = fwref;
781 if (tokval->t_type == TOKEN_INVALID)
782 i = scan(scpriv, tokval);
783 else
784 i = tokval->t_type;
786 while (ntempexprs) /* initialise temporary storage */
787 nasm_free (tempexprs[--ntempexprs]);
789 e = bexpr (critical);
790 if (!e)
791 return NULL;
793 if (i == TOKEN_WRT) {
794 i = scan(scpriv, tokval); /* eat the WRT */
795 f = expr6 (critical);
796 if (!f)
797 return NULL;
799 e = scalar_mult (e, 1L, FALSE); /* strip far-absolute segment part */
800 if (f) {
801 expr *g;
802 if (is_just_unknown(f))
803 g = unknown_expr();
804 else {
805 long value;
806 begintemp();
807 if (!is_reloc(f)) {
808 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
809 return NULL;
811 value = reloc_seg(f);
812 if (value == NO_SEG)
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");
817 return NULL;
819 addtotemp(EXPR_WRT, value);
820 g = finishtemp();
822 e = add_vectors (e, g);
824 return e;