Apply Nindent to all .c and .h files
[nasm/avx512.git] / eval.c
blob62bd8b29067ad34d50cc84759bdac31eb981ab3e
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) {
110 int lasttype;
112 if (p->type > q->type) {
113 addtotemp(q->type, q->value);
114 lasttype = q++->type;
115 } else if (p->type < q->type) {
116 addtotemp(p->type, p->value);
117 lasttype = p++->type;
118 } else { /* *p and *q have same type */
119 long sum = p->value + q->value;
120 if (sum)
121 addtotemp(p->type, sum);
122 lasttype = p->type;
123 p++, q++;
125 if (lasttype == EXPR_UNKNOWN) {
126 return finishtemp();
129 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
130 addtotemp(p->type, p->value);
131 p++;
133 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
134 addtotemp(q->type, q->value);
135 q++;
138 return finishtemp();
142 * Multiply a vector by a scalar. Strip far-absolute segment part
143 * if present.
145 * Explicit treatment of UNKNOWN is not required in this routine,
146 * since it will silently do the Right Thing anyway.
148 * If `affect_hints' is set, we also change the hint type to
149 * NOTBASE if a MAKEBASE hint points at a register being
150 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
151 * as the base register.
153 static expr *scalar_mult(expr * vect, long scalar, int affect_hints)
155 expr *p = vect;
157 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
158 p->value = scalar * (p->value);
159 if (hint && hint->type == EAH_MAKEBASE &&
160 p->type == hint->base && affect_hints)
161 hint->type = EAH_NOTBASE;
162 p++;
164 p->type = 0;
166 return vect;
169 static expr *scalarvect(long scalar)
171 begintemp();
172 addtotemp(EXPR_SIMPLE, scalar);
173 return finishtemp();
176 static expr *unknown_expr(void)
178 begintemp();
179 addtotemp(EXPR_UNKNOWN, 1L);
180 return finishtemp();
184 * The SEG operator: calculate the segment part of a relocatable
185 * value. Return NULL, as usual, if an error occurs. Report the
186 * error too.
188 static expr *segment_part(expr * e)
190 long seg;
192 if (is_unknown(e))
193 return unknown_expr();
195 if (!is_reloc(e)) {
196 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
197 return NULL;
200 seg = reloc_seg(e);
201 if (seg == NO_SEG) {
202 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
203 return NULL;
204 } else if (seg & SEG_ABS) {
205 return scalarvect(seg & ~SEG_ABS);
206 } else if (seg & 1) {
207 error(ERR_NONFATAL, "SEG applied to something which"
208 " is already a segment base");
209 return NULL;
210 } else {
211 long base = outfmt->segbase(seg + 1);
213 begintemp();
214 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
215 1L);
216 return finishtemp();
221 * Recursive-descent parser. Called with a single boolean operand,
222 * which is TRUE if the evaluation is critical (i.e. unresolved
223 * symbols are an error condition). Must update the global `i' to
224 * reflect the token after the parsed string. May return NULL.
226 * evaluate() should report its own errors: on return it is assumed
227 * that if NULL has been returned, the error has already been
228 * reported.
232 * Grammar parsed is:
234 * expr : bexpr [ WRT expr6 ]
235 * bexpr : rexp0 or expr0 depending on relative-mode setting
236 * rexp0 : rexp1 [ {||} rexp1...]
237 * rexp1 : rexp2 [ {^^} rexp2...]
238 * rexp2 : rexp3 [ {&&} rexp3...]
239 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
240 * expr0 : expr1 [ {|} expr1...]
241 * expr1 : expr2 [ {^} expr2...]
242 * expr2 : expr3 [ {&} expr3...]
243 * expr3 : expr4 [ {<<,>>} expr4...]
244 * expr4 : expr5 [ {+,-} expr5...]
245 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
246 * expr6 : { ~,+,-,SEG } expr6
247 * | (bexpr)
248 * | symbol
249 * | $
250 * | number
253 static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
255 static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
256 static expr *expr4(int), *expr5(int), *expr6(int);
258 static expr *(*bexpr) (int);
260 static expr *rexp0(int critical)
262 expr *e, *f;
264 e = rexp1(critical);
265 if (!e)
266 return NULL;
268 while (i == TOKEN_DBL_OR) {
269 i = scan(scpriv, tokval);
270 f = rexp1(critical);
271 if (!f)
272 return NULL;
273 if (!(is_simple(e) || is_just_unknown(e)) ||
274 !(is_simple(f) || is_just_unknown(f))) {
275 error(ERR_NONFATAL, "`|' operator may only be applied to"
276 " scalar values");
279 if (is_just_unknown(e) || is_just_unknown(f))
280 e = unknown_expr();
281 else
282 e = scalarvect((long)(reloc_value(e) || reloc_value(f)));
284 return e;
287 static expr *rexp1(int critical)
289 expr *e, *f;
291 e = rexp2(critical);
292 if (!e)
293 return NULL;
295 while (i == TOKEN_DBL_XOR) {
296 i = scan(scpriv, tokval);
297 f = rexp2(critical);
298 if (!f)
299 return NULL;
300 if (!(is_simple(e) || is_just_unknown(e)) ||
301 !(is_simple(f) || is_just_unknown(f))) {
302 error(ERR_NONFATAL, "`^' operator may only be applied to"
303 " scalar values");
306 if (is_just_unknown(e) || is_just_unknown(f))
307 e = unknown_expr();
308 else
309 e = scalarvect((long)(!reloc_value(e) ^ !reloc_value(f)));
311 return e;
314 static expr *rexp2(int critical)
316 expr *e, *f;
318 e = rexp3(critical);
319 if (!e)
320 return NULL;
321 while (i == TOKEN_DBL_AND) {
322 i = scan(scpriv, tokval);
323 f = rexp3(critical);
324 if (!f)
325 return NULL;
326 if (!(is_simple(e) || is_just_unknown(e)) ||
327 !(is_simple(f) || is_just_unknown(f))) {
328 error(ERR_NONFATAL, "`&' operator may only be applied to"
329 " scalar values");
331 if (is_just_unknown(e) || is_just_unknown(f))
332 e = unknown_expr();
333 else
334 e = scalarvect((long)(reloc_value(e) && reloc_value(f)));
336 return e;
339 static expr *rexp3(int critical)
341 expr *e, *f;
342 long v;
344 e = expr0(critical);
345 if (!e)
346 return NULL;
348 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
349 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
350 int j = i;
351 i = scan(scpriv, tokval);
352 f = expr0(critical);
353 if (!f)
354 return NULL;
356 e = add_vectors(e, scalar_mult(f, -1L, FALSE));
358 switch (j) {
359 case TOKEN_EQ:
360 case TOKEN_NE:
361 if (is_unknown(e))
362 v = -1; /* means unknown */
363 else if (!is_really_simple(e) || reloc_value(e) != 0)
364 v = (j == TOKEN_NE); /* unequal, so return TRUE if NE */
365 else
366 v = (j == TOKEN_EQ); /* equal, so return TRUE if EQ */
367 break;
368 default:
369 if (is_unknown(e))
370 v = -1; /* means unknown */
371 else if (!is_really_simple(e)) {
372 error(ERR_NONFATAL,
373 "`%s': operands differ by a non-scalar",
374 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
375 TOKEN_GE ? ">=" : ">"));
376 v = 0; /* must set it to _something_ */
377 } else {
378 int vv = reloc_value(e);
379 if (vv == 0)
380 v = (j == TOKEN_LE || j == TOKEN_GE);
381 else if (vv > 0)
382 v = (j == TOKEN_GE || j == TOKEN_GT);
383 else /* vv < 0 */
384 v = (j == TOKEN_LE || j == TOKEN_LT);
386 break;
389 if (v == -1)
390 e = unknown_expr();
391 else
392 e = scalarvect(v);
394 return e;
397 static expr *expr0(int critical)
399 expr *e, *f;
401 e = expr1(critical);
402 if (!e)
403 return NULL;
405 while (i == '|') {
406 i = scan(scpriv, tokval);
407 f = expr1(critical);
408 if (!f)
409 return NULL;
410 if (!(is_simple(e) || is_just_unknown(e)) ||
411 !(is_simple(f) || is_just_unknown(f))) {
412 error(ERR_NONFATAL, "`|' operator may only be applied to"
413 " scalar values");
415 if (is_just_unknown(e) || is_just_unknown(f))
416 e = unknown_expr();
417 else
418 e = scalarvect(reloc_value(e) | reloc_value(f));
420 return e;
423 static expr *expr1(int critical)
425 expr *e, *f;
427 e = expr2(critical);
428 if (!e)
429 return NULL;
431 while (i == '^') {
432 i = scan(scpriv, tokval);
433 f = expr2(critical);
434 if (!f)
435 return NULL;
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"
439 " scalar values");
441 if (is_just_unknown(e) || is_just_unknown(f))
442 e = unknown_expr();
443 else
444 e = scalarvect(reloc_value(e) ^ reloc_value(f));
446 return e;
449 static expr *expr2(int critical)
451 expr *e, *f;
453 e = expr3(critical);
454 if (!e)
455 return NULL;
457 while (i == '&') {
458 i = scan(scpriv, tokval);
459 f = expr3(critical);
460 if (!f)
461 return NULL;
462 if (!(is_simple(e) || is_just_unknown(e)) ||
463 !(is_simple(f) || is_just_unknown(f))) {
464 error(ERR_NONFATAL, "`&' operator may only be applied to"
465 " scalar values");
467 if (is_just_unknown(e) || is_just_unknown(f))
468 e = unknown_expr();
469 else
470 e = scalarvect(reloc_value(e) & reloc_value(f));
472 return e;
475 static expr *expr3(int critical)
477 expr *e, *f;
479 e = expr4(critical);
480 if (!e)
481 return NULL;
483 while (i == TOKEN_SHL || i == TOKEN_SHR) {
484 int j = i;
485 i = scan(scpriv, tokval);
486 f = expr4(critical);
487 if (!f)
488 return NULL;
489 if (!(is_simple(e) || is_just_unknown(e)) ||
490 !(is_simple(f) || is_just_unknown(f))) {
491 error(ERR_NONFATAL, "shift operator may only be applied to"
492 " scalar values");
493 } else if (is_just_unknown(e) || is_just_unknown(f)) {
494 e = unknown_expr();
495 } else
496 switch (j) {
497 case TOKEN_SHL:
498 e = scalarvect(reloc_value(e) << reloc_value(f));
499 break;
500 case TOKEN_SHR:
501 e = scalarvect(((unsigned long)reloc_value(e)) >>
502 reloc_value(f));
503 break;
506 return e;
509 static expr *expr4(int critical)
511 expr *e, *f;
513 e = expr5(critical);
514 if (!e)
515 return NULL;
516 while (i == '+' || i == '-') {
517 int j = i;
518 i = scan(scpriv, tokval);
519 f = expr5(critical);
520 if (!f)
521 return NULL;
522 switch (j) {
523 case '+':
524 e = add_vectors(e, f);
525 break;
526 case '-':
527 e = add_vectors(e, scalar_mult(f, -1L, FALSE));
528 break;
531 return e;
534 static expr *expr5(int critical)
536 expr *e, *f;
538 e = expr6(critical);
539 if (!e)
540 return NULL;
541 while (i == '*' || i == '/' || i == '%' ||
542 i == TOKEN_SDIV || i == TOKEN_SMOD) {
543 int j = i;
544 i = scan(scpriv, tokval);
545 f = expr6(critical);
546 if (!f)
547 return NULL;
548 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
549 !(is_simple(f) || is_just_unknown(f)))) {
550 error(ERR_NONFATAL, "division operator may only be applied to"
551 " scalar values");
552 return NULL;
554 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
555 error(ERR_NONFATAL, "division by zero");
556 return NULL;
558 switch (j) {
559 case '*':
560 if (is_simple(e))
561 e = scalar_mult(f, reloc_value(e), TRUE);
562 else if (is_simple(f))
563 e = scalar_mult(e, reloc_value(f), TRUE);
564 else if (is_just_unknown(e) && is_just_unknown(f))
565 e = unknown_expr();
566 else {
567 error(ERR_NONFATAL, "unable to multiply two "
568 "non-scalar objects");
569 return NULL;
571 break;
572 case '/':
573 if (is_just_unknown(e) || is_just_unknown(f))
574 e = unknown_expr();
575 else
576 e = scalarvect(((unsigned long)reloc_value(e)) /
577 ((unsigned long)reloc_value(f)));
578 break;
579 case '%':
580 if (is_just_unknown(e) || is_just_unknown(f))
581 e = unknown_expr();
582 else
583 e = scalarvect(((unsigned long)reloc_value(e)) %
584 ((unsigned long)reloc_value(f)));
585 break;
586 case TOKEN_SDIV:
587 if (is_just_unknown(e) || is_just_unknown(f))
588 e = unknown_expr();
589 else
590 e = scalarvect(((signed long)reloc_value(e)) /
591 ((signed long)reloc_value(f)));
592 break;
593 case TOKEN_SMOD:
594 if (is_just_unknown(e) || is_just_unknown(f))
595 e = unknown_expr();
596 else
597 e = scalarvect(((signed long)reloc_value(e)) %
598 ((signed long)reloc_value(f)));
599 break;
602 return e;
605 static expr *expr6(int critical)
607 long type;
608 expr *e;
609 long label_seg, label_ofs;
611 if (i == '-') {
612 i = scan(scpriv, tokval);
613 e = expr6(critical);
614 if (!e)
615 return NULL;
616 return scalar_mult(e, -1L, FALSE);
617 } else if (i == '+') {
618 i = scan(scpriv, tokval);
619 return expr6(critical);
620 } else if (i == '~') {
621 i = scan(scpriv, tokval);
622 e = expr6(critical);
623 if (!e)
624 return NULL;
625 if (is_just_unknown(e))
626 return unknown_expr();
627 else if (!is_simple(e)) {
628 error(ERR_NONFATAL, "`~' operator may only be applied to"
629 " scalar values");
630 return NULL;
632 return scalarvect(~reloc_value(e));
633 } else if (i == TOKEN_SEG) {
634 i = scan(scpriv, tokval);
635 e = expr6(critical);
636 if (!e)
637 return NULL;
638 e = segment_part(e);
639 if (!e)
640 return NULL;
641 if (is_unknown(e) && critical) {
642 error(ERR_NONFATAL, "unable to determine segment base");
643 return NULL;
645 return e;
646 } else if (i == '(') {
647 i = scan(scpriv, tokval);
648 e = bexpr(critical);
649 if (!e)
650 return NULL;
651 if (i != ')') {
652 error(ERR_NONFATAL, "expecting `)'");
653 return NULL;
655 i = scan(scpriv, tokval);
656 return e;
657 } else if (i == TOKEN_NUM || i == TOKEN_REG || i == TOKEN_ID ||
658 i == TOKEN_HERE || i == TOKEN_BASE) {
659 begintemp();
660 switch (i) {
661 case TOKEN_NUM:
662 addtotemp(EXPR_SIMPLE, tokval->t_integer);
663 break;
664 case TOKEN_REG:
665 addtotemp(tokval->t_integer, 1L);
666 if (hint && hint->type == EAH_NOHINT)
667 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
668 break;
669 case TOKEN_ID:
670 case TOKEN_HERE:
671 case TOKEN_BASE:
673 * If !location->known, this indicates that no
674 * symbol, Here or Base references are valid because we
675 * are in preprocess-only mode.
677 if (!location->known) {
678 error(ERR_NONFATAL,
679 "%s not supported in preprocess-only mode",
680 (i == TOKEN_ID ? "symbol references" :
681 i == TOKEN_HERE ? "`$'" : "`$$'"));
682 addtotemp(EXPR_UNKNOWN, 1L);
683 break;
686 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
687 if (i == TOKEN_BASE) {
688 label_seg = in_abs_seg ? abs_seg : location->segment;
689 label_ofs = 0;
690 } else if (i == TOKEN_HERE) {
691 label_seg = in_abs_seg ? abs_seg : location->segment;
692 label_ofs = in_abs_seg ? abs_offset : location->offset;
693 } else {
694 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
695 if (critical == 2) {
696 error(ERR_NONFATAL, "symbol `%s' undefined",
697 tokval->t_charptr);
698 return NULL;
699 } else if (critical == 1) {
700 error(ERR_NONFATAL,
701 "symbol `%s' not defined before use",
702 tokval->t_charptr);
703 return NULL;
704 } else {
705 if (opflags)
706 *opflags |= 1;
707 type = EXPR_UNKNOWN;
708 label_seg = NO_SEG;
709 label_ofs = 1;
712 if (opflags && is_extern(tokval->t_charptr))
713 *opflags |= OPFLAG_EXTERN;
715 addtotemp(type, label_ofs);
716 if (label_seg != NO_SEG)
717 addtotemp(EXPR_SEGBASE + label_seg, 1L);
718 break;
720 i = scan(scpriv, tokval);
721 return finishtemp();
722 } else {
723 error(ERR_NONFATAL, "expression syntax error");
724 return NULL;
728 void eval_global_info(struct ofmt *output, lfunc lookup_label,
729 loc_t * locp)
731 outfmt = output;
732 labelfunc = lookup_label;
733 location = locp;
736 expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
737 int *fwref, int critical, efunc report_error,
738 struct eval_hints *hints)
740 expr *e;
741 expr *f = NULL;
743 hint = hints;
744 if (hint)
745 hint->type = EAH_NOHINT;
747 if (critical & CRITICAL) {
748 critical &= ~CRITICAL;
749 bexpr = rexp0;
750 } else
751 bexpr = expr0;
753 scan = sc;
754 scpriv = scprivate;
755 tokval = tv;
756 error = report_error;
757 opflags = fwref;
759 if (tokval->t_type == TOKEN_INVALID)
760 i = scan(scpriv, tokval);
761 else
762 i = tokval->t_type;
764 while (ntempexprs) /* initialise temporary storage */
765 nasm_free(tempexprs[--ntempexprs]);
767 e = bexpr(critical);
768 if (!e)
769 return NULL;
771 if (i == TOKEN_WRT) {
772 i = scan(scpriv, tokval); /* eat the WRT */
773 f = expr6(critical);
774 if (!f)
775 return NULL;
777 e = scalar_mult(e, 1L, FALSE); /* strip far-absolute segment part */
778 if (f) {
779 expr *g;
780 if (is_just_unknown(f))
781 g = unknown_expr();
782 else {
783 long value;
784 begintemp();
785 if (!is_reloc(f)) {
786 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
787 return NULL;
789 value = reloc_seg(f);
790 if (value == NO_SEG)
791 value = reloc_value(f) | SEG_ABS;
792 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
793 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
794 return NULL;
796 addtotemp(EXPR_WRT, value);
797 g = finishtemp();
799 e = add_vectors(e, g);
801 return e;