blob: 72bb4d03ea33436971ffd9b429a0747d6443db83 [file] [log] [blame]
H. Peter Anvin76690a12002-04-30 20:52:49 +00001/* eval.c expression evaluator for the Netwide Assembler
2 *
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.
7 *
8 * initial version 27/iii/95 by Simon Tatham
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <stddef.h>
14#include <string.h>
15#include <ctype.h>
16
17#include "nasm.h"
18#include "nasmlib.h"
19#include "eval.h"
H. Peter Anvineba20a72002-04-30 20:53:55 +000020#include "labels.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000021
H. Peter Anvin76690a12002-04-30 20:52:49 +000022#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000023#define TEMPEXPR_DELTA 8
24
H. Peter Anvineba20a72002-04-30 20:53:55 +000025static scanner scan; /* Address of scanner routine */
26static efunc error; /* Address of error reporting routine */
27static lfunc labelfunc; /* Address of label routine */
28
29static struct ofmt *outfmt; /* Structure of addresses of output routines */
30
31static expr **tempexprs = NULL;
32static int ntempexprs;
33static int tempexprs_size = 0;
34
35static expr *tempexpr;
36static int ntempexpr;
37static int tempexpr_size;
38
39static struct tokenval *tokval; /* The current token */
40static int i; /* The t_type of tokval */
41
H. Peter Anvin76690a12002-04-30 20:52:49 +000042static void *scpriv;
H. Peter Anvineba20a72002-04-30 20:53:55 +000043static loc_t *location; /* Pointer to current line's segment,offset */
44static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000045
46static struct eval_hints *hint;
47
H. Peter Anvin667dd802002-05-26 19:49:41 +000048extern int in_abs_seg; /* ABSOLUTE segment flag */
49extern long abs_seg; /* ABSOLUTE segment */
50extern long abs_offset; /* ABSOLUTE segment offset */
51
H. Peter Anvin76690a12002-04-30 20:52:49 +000052/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000053 * Unimportant cleanup is done to avoid confusing people who are trying
54 * to debug real memory leaks
55 */
56void eval_cleanup(void)
57{
58 while (ntempexprs)
59 nasm_free (tempexprs[--ntempexprs]);
60 nasm_free (tempexprs);
61}
62
63/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000064 * Construct a temporary expression.
65 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000066static void begintemp(void)
67{
H. Peter Anvin76690a12002-04-30 20:52:49 +000068 tempexpr = NULL;
69 tempexpr_size = ntempexpr = 0;
70}
71
H. Peter Anvineba20a72002-04-30 20:53:55 +000072static void addtotemp(long type, long value)
73{
H. Peter Anvin76690a12002-04-30 20:52:49 +000074 while (ntempexpr >= tempexpr_size) {
75 tempexpr_size += TEMPEXPR_DELTA;
76 tempexpr = nasm_realloc(tempexpr,
77 tempexpr_size*sizeof(*tempexpr));
78 }
79 tempexpr[ntempexpr].type = type;
80 tempexpr[ntempexpr++].value = value;
81}
82
H. Peter Anvineba20a72002-04-30 20:53:55 +000083static expr *finishtemp(void)
84{
H. Peter Anvin76690a12002-04-30 20:52:49 +000085 addtotemp (0L, 0L); /* terminate */
86 while (ntempexprs >= tempexprs_size) {
87 tempexprs_size += TEMPEXPRS_DELTA;
88 tempexprs = nasm_realloc(tempexprs,
89 tempexprs_size*sizeof(*tempexprs));
90 }
91 return tempexprs[ntempexprs++] = tempexpr;
92}
93
94/*
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.
98 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000099static expr *add_vectors(expr *p, expr *q)
100{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000101 int preserve;
102
103 preserve = is_really_simple(p) || is_really_simple(q);
104
105 begintemp();
106
107 while (p->type && q->type &&
108 p->type < EXPR_SEGBASE+SEG_ABS &&
H. Peter Anvineba20a72002-04-30 20:53:55 +0000109 q->type < EXPR_SEGBASE+SEG_ABS)
110 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000111 int lasttype;
112
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 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000120 long sum = p->value + q->value;
121 if (sum)
122 addtotemp(p->type, sum);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000123 lasttype = p->type;
124 p++, q++;
125 }
126 if (lasttype == EXPR_UNKNOWN) {
127 return finishtemp();
128 }
129 }
130 while (p->type &&
H. Peter Anvineba20a72002-04-30 20:53:55 +0000131 (preserve || p->type < EXPR_SEGBASE+SEG_ABS))
132 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000133 addtotemp(p->type, p->value);
134 p++;
135 }
136 while (q->type &&
H. Peter Anvineba20a72002-04-30 20:53:55 +0000137 (preserve || q->type < EXPR_SEGBASE+SEG_ABS))
138 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000139 addtotemp(q->type, q->value);
140 q++;
141 }
142
143 return finishtemp();
144}
145
146/*
147 * Multiply a vector by a scalar. Strip far-absolute segment part
148 * if present.
149 *
150 * Explicit treatment of UNKNOWN is not required in this routine,
151 * since it will silently do the Right Thing anyway.
152 *
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.
157 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000158static expr *scalar_mult(expr *vect, long scalar, int affect_hints)
159{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000160 expr *p = vect;
161
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++;
168 }
169 p->type = 0;
170
171 return vect;
172}
173
H. Peter Anvineba20a72002-04-30 20:53:55 +0000174static expr *scalarvect (long scalar)
175{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000176 begintemp();
177 addtotemp(EXPR_SIMPLE, scalar);
178 return finishtemp();
179}
180
H. Peter Anvineba20a72002-04-30 20:53:55 +0000181static expr *unknown_expr (void)
182{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000183 begintemp();
184 addtotemp(EXPR_UNKNOWN, 1L);
185 return finishtemp();
186}
187
188/*
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.
192 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193static expr *segment_part (expr *e)
194{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000195 long seg;
196
197 if (is_unknown(e))
198 return unknown_expr();
199
200 if (!is_reloc(e)) {
201 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
202 return NULL;
203 }
204
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;
215 }
216 else {
217 long base = outfmt->segbase(seg+1);
218
219 begintemp();
220 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE+base), 1L);
221 return finishtemp();
222 }
223}
224
225/*
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.
230 *
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.
234 */
235
236/*
237 * Grammar parsed is:
238 *
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
256 */
257
258static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
259
260static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
261static expr *expr4(int), *expr5(int), *expr6(int);
262
263static expr *(*bexpr)(int);
264
H. Peter Anvineba20a72002-04-30 20:53:55 +0000265static expr *rexp0(int critical)
266{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000267 expr *e, *f;
268
269 e = rexp1(critical);
270 if (!e)
271 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000272
273 while (i == TOKEN_DBL_OR)
274 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000275 i = scan(scpriv, tokval);
276 f = rexp1(critical);
277 if (!f)
278 return NULL;
279 if (!(is_simple(e) || is_just_unknown(e)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000280 !(is_simple(f) || is_just_unknown(f)))
281 {
282 error(ERR_NONFATAL, "`|' operator may only be applied to"
283 " scalar values");
284 }
285
H. Peter Anvin76690a12002-04-30 20:52:49 +0000286 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)));
290 }
291 return e;
292}
293
H. Peter Anvineba20a72002-04-30 20:53:55 +0000294static expr *rexp1(int critical)
295{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000296 expr *e, *f;
297
298 e = rexp2(critical);
299 if (!e)
300 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000301
302 while (i == TOKEN_DBL_XOR)
303 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000304 i = scan(scpriv, tokval);
305 f = rexp2(critical);
306 if (!f)
307 return NULL;
308 if (!(is_simple(e) || is_just_unknown(e)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000309 !(is_simple(f) || is_just_unknown(f)))
310 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000311 error(ERR_NONFATAL, "`^' operator may only be applied to"
312 " scalar values");
313 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000314
H. Peter Anvin76690a12002-04-30 20:52:49 +0000315 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)));
319 }
320 return e;
321}
322
H. Peter Anvineba20a72002-04-30 20:53:55 +0000323static expr *rexp2(int critical)
324{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000325 expr *e, *f;
326
327 e = rexp3(critical);
328 if (!e)
329 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000330 while (i == TOKEN_DBL_AND)
331 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000332 i = scan(scpriv, tokval);
333 f = rexp3(critical);
334 if (!f)
335 return NULL;
336 if (!(is_simple(e) || is_just_unknown(e)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000337 !(is_simple(f) || is_just_unknown(f)))
338 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000339 error(ERR_NONFATAL, "`&' operator may only be applied to"
340 " scalar values");
341 }
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)));
346 }
347 return e;
348}
349
H. Peter Anvineba20a72002-04-30 20:53:55 +0000350static expr *rexp3(int critical)
351{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000352 expr *e, *f;
353 long v;
354
355 e = expr0(critical);
356 if (!e)
357 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358
H. Peter Anvin76690a12002-04-30 20:52:49 +0000359 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000360 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE)
361 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 int j = i;
363 i = scan(scpriv, tokval);
364 f = expr0(critical);
365 if (!f)
366 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000367
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368 e = add_vectors (e, scalar_mult(f, -1L, FALSE));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000369
370 switch (j)
371 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000372 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);
396 }
397 break;
398 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399
H. Peter Anvin76690a12002-04-30 20:52:49 +0000400 if (v == -1)
401 e = unknown_expr();
402 else
403 e = scalarvect(v);
404 }
405 return e;
406}
407
H. Peter Anvineba20a72002-04-30 20:53:55 +0000408static expr *expr0(int critical)
409{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000410 expr *e, *f;
411
412 e = expr1(critical);
413 if (!e)
414 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000415
416 while (i == '|')
417 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000418 i = scan(scpriv, tokval);
419 f = expr1(critical);
420 if (!f)
421 return NULL;
422 if (!(is_simple(e) || is_just_unknown(e)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000423 !(is_simple(f) || is_just_unknown(f)))
424 {
425 error(ERR_NONFATAL, "`|' operator may only be applied to"
426 " scalar values");
427 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000428 if (is_just_unknown(e) || is_just_unknown(f))
429 e = unknown_expr();
430 else
431 e = scalarvect (reloc_value(e) | reloc_value(f));
432 }
433 return e;
434}
435
H. Peter Anvineba20a72002-04-30 20:53:55 +0000436static expr *expr1(int critical)
437{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000438 expr *e, *f;
439
440 e = expr2(critical);
441 if (!e)
442 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000443
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444 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)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000450 !(is_simple(f) || is_just_unknown(f)))
451 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000452 error(ERR_NONFATAL, "`^' operator may only be applied to"
453 " scalar values");
454 }
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));
459 }
460 return e;
461}
462
H. Peter Anvineba20a72002-04-30 20:53:55 +0000463static expr *expr2(int critical)
464{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000465 expr *e, *f;
466
467 e = expr3(critical);
468 if (!e)
469 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000470
H. Peter Anvin76690a12002-04-30 20:52:49 +0000471 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)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477 !(is_simple(f) || is_just_unknown(f)))
478 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000479 error(ERR_NONFATAL, "`&' operator may only be applied to"
480 " scalar values");
481 }
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));
486 }
487 return e;
488}
489
H. Peter Anvineba20a72002-04-30 20:53:55 +0000490static expr *expr3(int critical)
491{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000492 expr *e, *f;
493
494 e = expr4(critical);
495 if (!e)
496 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497
498 while (i == TOKEN_SHL || i == TOKEN_SHR)
499 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000500 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)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000506 !(is_simple(f) || is_just_unknown(f)))
507 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000508 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;
520 }
521 }
522 return e;
523}
524
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525static expr *expr4(int critical)
526{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000527 expr *e, *f;
528
529 e = expr5(critical);
530 if (!e)
531 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532 while (i == '+' || i == '-')
533 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000534 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;
546 }
547 }
548 return e;
549}
550
H. Peter Anvineba20a72002-04-30 20:53:55 +0000551static expr *expr5(int critical)
552{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000553 expr *e, *f;
554
555 e = expr6(critical);
556 if (!e)
557 return NULL;
558 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000559 i == TOKEN_SDIV || i == TOKEN_SMOD)
560 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000561 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)) ||
H. Peter Anvineba20a72002-04-30 20:53:55 +0000567 !(is_simple(f) || is_just_unknown(f))))
568 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000569 error(ERR_NONFATAL, "division operator may only be applied to"
570 " scalar values");
571 return NULL;
572 }
573 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
574 error(ERR_NONFATAL, "division by zero");
575 return NULL;
576 }
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;
589 }
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;
619 }
620 }
621 return e;
622}
623
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624static expr *expr6(int critical)
625{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000626 long type;
627 expr *e;
628 long label_seg, label_ofs;
629
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;
650 }
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 (is_unknown(e) && critical) {
659 error(ERR_NONFATAL, "unable to determine segment base");
660 return NULL;
661 }
662 return e;
663 } else if (i == '(') {
664 i = scan(scpriv, tokval);
665 e = bexpr(critical);
666 if (!e)
667 return NULL;
668 if (i != ')') {
669 error(ERR_NONFATAL, "expecting `)'");
670 return NULL;
671 }
672 i = scan(scpriv, tokval);
673 return e;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000674 }
675 else if (i == TOKEN_NUM || i == TOKEN_REG || i == TOKEN_ID ||
676 i == TOKEN_HERE || i == TOKEN_BASE)
677 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000678 begintemp();
679 switch (i) {
680 case TOKEN_NUM:
681 addtotemp(EXPR_SIMPLE, tokval->t_integer);
682 break;
683 case TOKEN_REG:
684 addtotemp(tokval->t_integer, 1L);
685 if (hint && hint->type == EAH_NOHINT)
686 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
687 break;
688 case TOKEN_ID:
689 case TOKEN_HERE:
690 case TOKEN_BASE:
691 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +0000692 * If !location->known, this indicates that no
H. Peter Anvin76690a12002-04-30 20:52:49 +0000693 * symbol, Here or Base references are valid because we
694 * are in preprocess-only mode.
695 */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000696 if (!location->known) {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000697 error(ERR_NONFATAL,
698 "%s not supported in preprocess-only mode",
699 (i == TOKEN_ID ? "symbol references" :
700 i == TOKEN_HERE ? "`$'" : "`$$'"));
701 addtotemp(EXPR_UNKNOWN, 1L);
702 break;
703 }
704
H. Peter Anvin76690a12002-04-30 20:52:49 +0000705 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
H. Peter Anvince14ce62002-04-30 20:54:58 +0000706 if (i == TOKEN_BASE)
707 {
H. Peter Anvin667dd802002-05-26 19:49:41 +0000708 label_seg = in_abs_seg ? abs_seg : location->segment;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000709 label_ofs = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000710 } else if (i == TOKEN_HERE) {
H. Peter Anvin667dd802002-05-26 19:49:41 +0000711 label_seg = in_abs_seg ? abs_seg : location->segment;
712 label_ofs = in_abs_seg ? abs_offset : location->offset;
H. Peter Anvince14ce62002-04-30 20:54:58 +0000713 } else {
714 if (!labelfunc(tokval->t_charptr,&label_seg,&label_ofs))
715 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000716 if (critical == 2) {
717 error (ERR_NONFATAL, "symbol `%s' undefined",
718 tokval->t_charptr);
719 return NULL;
720 } else if (critical == 1) {
H. Peter Anvince14ce62002-04-30 20:54:58 +0000721 error (ERR_NONFATAL,
722 "symbol `%s' not defined before use",
H. Peter Anvin76690a12002-04-30 20:52:49 +0000723 tokval->t_charptr);
724 return NULL;
725 } else {
H. Peter Anvineba20a72002-04-30 20:53:55 +0000726 if (opflags)
727 *opflags |= 1;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000728 type = EXPR_UNKNOWN;
729 label_seg = NO_SEG;
730 label_ofs = 1;
731 }
732 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000733 if (opflags && is_extern (tokval->t_charptr))
734 *opflags |= OPFLAG_EXTERN;
735 }
H. Peter Anvince14ce62002-04-30 20:54:58 +0000736 addtotemp(type, label_ofs);
737 if (label_seg!=NO_SEG)
738 addtotemp(EXPR_SEGBASE + label_seg, 1L);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000739 break;
740 }
741 i = scan(scpriv, tokval);
742 return finishtemp();
743 } else {
744 error(ERR_NONFATAL, "expression syntax error");
745 return NULL;
746 }
747}
748
H. Peter Anvineba20a72002-04-30 20:53:55 +0000749void eval_global_info (struct ofmt *output, lfunc lookup_label, loc_t *locp)
750{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000751 outfmt = output;
752 labelfunc = lookup_label;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000753 location = locp;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000754}
755
756expr *evaluate (scanner sc, void *scprivate, struct tokenval *tv,
757 int *fwref, int critical, efunc report_error,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000758 struct eval_hints *hints)
759{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000760 expr *e;
761 expr *f = NULL;
762
763 hint = hints;
764 if (hint)
765 hint->type = EAH_NOHINT;
766
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000767 if (critical & CRITICAL) {
768 critical &= ~CRITICAL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000769 bexpr = rexp0;
770 } else
771 bexpr = expr0;
772
773 scan = sc;
774 scpriv = scprivate;
775 tokval = tv;
776 error = report_error;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000777 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000778
779 if (tokval->t_type == TOKEN_INVALID)
780 i = scan(scpriv, tokval);
781 else
782 i = tokval->t_type;
783
784 while (ntempexprs) /* initialise temporary storage */
785 nasm_free (tempexprs[--ntempexprs]);
786
787 e = bexpr (critical);
788 if (!e)
789 return NULL;
790
791 if (i == TOKEN_WRT) {
792 i = scan(scpriv, tokval); /* eat the WRT */
793 f = expr6 (critical);
794 if (!f)
795 return NULL;
796 }
797 e = scalar_mult (e, 1L, FALSE); /* strip far-absolute segment part */
798 if (f) {
799 expr *g;
800 if (is_just_unknown(f))
801 g = unknown_expr();
802 else {
803 long value;
804 begintemp();
805 if (!is_reloc(f)) {
806 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
807 return NULL;
808 }
809 value = reloc_seg(f);
810 if (value == NO_SEG)
811 value = reloc_value(f) | SEG_ABS;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000812 else if (!(value & SEG_ABS) && !(value % 2) && critical)
813 {
H. Peter Anvin76690a12002-04-30 20:52:49 +0000814 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
815 return NULL;
816 }
817 addtotemp(EXPR_WRT, value);
818 g = finishtemp();
819 }
820 e = add_vectors (e, g);
821 }
822 return e;
823}