blob: 644929d73b47075a430532a476a8001c048b0584 [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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000016#include <inttypes.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000017
18#include "nasm.h"
19#include "nasmlib.h"
20#include "eval.h"
H. Peter Anvineba20a72002-04-30 20:53:55 +000021#include "labels.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000022
H. Peter Anvin76690a12002-04-30 20:52:49 +000023#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000024#define TEMPEXPR_DELTA 8
25
H. Peter Anvine2c80182005-01-15 22:15:51 +000026static scanner scan; /* Address of scanner routine */
27static efunc error; /* Address of error reporting routine */
28static lfunc labelfunc; /* Address of label routine */
H. Peter Anvineba20a72002-04-30 20:53:55 +000029
H. Peter Anvine2c80182005-01-15 22:15:51 +000030static struct ofmt *outfmt; /* Structure of addresses of output routines */
H. Peter Anvineba20a72002-04-30 20:53:55 +000031
32static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000033static int ntempexprs;
34static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000035
H. Peter Anvine2c80182005-01-15 22:15:51 +000036static expr *tempexpr;
37static int ntempexpr;
38static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000039
H. Peter Anvine2c80182005-01-15 22:15:51 +000040static struct tokenval *tokval; /* The current token */
41static int i; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000042
H. Peter Anvin76690a12002-04-30 20:52:49 +000043static void *scpriv;
H. Peter Anvine2c80182005-01-15 22:15:51 +000044static loc_t *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000045static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000046
47static struct eval_hints *hint;
48
H. Peter Anvine2c80182005-01-15 22:15:51 +000049extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000050extern int32_t abs_seg; /* ABSOLUTE segment */
51extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvin667dd802002-05-26 19:49:41 +000052
H. Peter Anvin76690a12002-04-30 20:52:49 +000053/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000054 * Unimportant cleanup is done to avoid confusing people who are trying
55 * to debug real memory leaks
56 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000057void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000058{
59 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000060 nasm_free(tempexprs[--ntempexprs]);
61 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000062}
63
64/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000065 * Construct a temporary expression.
66 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000067static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000068{
H. Peter Anvin76690a12002-04-30 20:52:49 +000069 tempexpr = NULL;
70 tempexpr_size = ntempexpr = 0;
71}
72
Keith Kaniosb7a89542007-04-12 02:40:54 +000073static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +000074{
H. Peter Anvin76690a12002-04-30 20:52:49 +000075 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000076 tempexpr_size += TEMPEXPR_DELTA;
77 tempexpr = nasm_realloc(tempexpr,
78 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +000079 }
80 tempexpr[ntempexpr].type = type;
81 tempexpr[ntempexpr++].value = value;
82}
83
H. Peter Anvine2c80182005-01-15 22:15:51 +000084static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000085{
H. Peter Anvine2c80182005-01-15 22:15:51 +000086 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +000087 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000088 tempexprs_size += TEMPEXPRS_DELTA;
89 tempexprs = nasm_realloc(tempexprs,
90 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +000091 }
92 return tempexprs[ntempexprs++] = tempexpr;
93}
94
95/*
96 * Add two vector datatypes. We have some bizarre behaviour on far-
97 * absolute segment types: we preserve them during addition _only_
98 * if one of the segments is a truly pure scalar.
99 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000100static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000101{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000102 int preserve;
103
104 preserve = is_really_simple(p) || is_really_simple(q);
105
106 begintemp();
107
108 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109 p->type < EXPR_SEGBASE + SEG_ABS &&
110 q->type < EXPR_SEGBASE + SEG_ABS) {
111 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000112
H. Peter Anvine2c80182005-01-15 22:15:51 +0000113 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 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000120 int32_t sum = p->value + q->value;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000121 if (sum)
122 addtotemp(p->type, sum);
123 lasttype = p->type;
124 p++, q++;
125 }
126 if (lasttype == EXPR_UNKNOWN) {
127 return finishtemp();
128 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000129 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000130 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
131 addtotemp(p->type, p->value);
132 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000133 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000134 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
135 addtotemp(q->type, q->value);
136 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000137 }
138
139 return finishtemp();
140}
141
142/*
143 * Multiply a vector by a scalar. Strip far-absolute segment part
144 * if present.
145 *
146 * Explicit treatment of UNKNOWN is not required in this routine,
147 * since it will silently do the Right Thing anyway.
148 *
149 * If `affect_hints' is set, we also change the hint type to
150 * NOTBASE if a MAKEBASE hint points at a register being
151 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
152 * as the base register.
153 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000154static expr *scalar_mult(expr * vect, int32_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000155{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000156 expr *p = vect;
157
H. Peter Anvine2c80182005-01-15 22:15:51 +0000158 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
159 p->value = scalar * (p->value);
160 if (hint && hint->type == EAH_MAKEBASE &&
161 p->type == hint->base && affect_hints)
162 hint->type = EAH_NOTBASE;
163 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000164 }
165 p->type = 0;
166
167 return vect;
168}
169
Keith Kaniosb7a89542007-04-12 02:40:54 +0000170static expr *scalarvect(int32_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000171{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000172 begintemp();
173 addtotemp(EXPR_SIMPLE, scalar);
174 return finishtemp();
175}
176
H. Peter Anvine2c80182005-01-15 22:15:51 +0000177static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000178{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000179 begintemp();
180 addtotemp(EXPR_UNKNOWN, 1L);
181 return finishtemp();
182}
183
184/*
185 * The SEG operator: calculate the segment part of a relocatable
186 * value. Return NULL, as usual, if an error occurs. Report the
187 * error too.
188 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000189static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000190{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000191 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000192
193 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000194 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000195
196 if (!is_reloc(e)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
198 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000199 }
200
201 seg = reloc_seg(e);
202 if (seg == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000203 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
204 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000205 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000207 } else if (seg & 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000208 error(ERR_NONFATAL, "SEG applied to something which"
209 " is already a segment base");
210 return NULL;
211 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000212 int32_t base = outfmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000213
H. Peter Anvine2c80182005-01-15 22:15:51 +0000214 begintemp();
215 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
216 1L);
217 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000218 }
219}
220
221/*
222 * Recursive-descent parser. Called with a single boolean operand,
223 * which is TRUE if the evaluation is critical (i.e. unresolved
224 * symbols are an error condition). Must update the global `i' to
225 * reflect the token after the parsed string. May return NULL.
226 *
227 * evaluate() should report its own errors: on return it is assumed
228 * that if NULL has been returned, the error has already been
229 * reported.
230 */
231
232/*
233 * Grammar parsed is:
234 *
235 * expr : bexpr [ WRT expr6 ]
236 * bexpr : rexp0 or expr0 depending on relative-mode setting
237 * rexp0 : rexp1 [ {||} rexp1...]
238 * rexp1 : rexp2 [ {^^} rexp2...]
239 * rexp2 : rexp3 [ {&&} rexp3...]
240 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
241 * expr0 : expr1 [ {|} expr1...]
242 * expr1 : expr2 [ {^} expr2...]
243 * expr2 : expr3 [ {&} expr3...]
244 * expr3 : expr4 [ {<<,>>} expr4...]
245 * expr4 : expr5 [ {+,-} expr5...]
246 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
247 * expr6 : { ~,+,-,SEG } expr6
248 * | (bexpr)
249 * | symbol
250 * | $
251 * | number
252 */
253
254static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
255
256static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
257static expr *expr4(int), *expr5(int), *expr6(int);
258
H. Peter Anvine2c80182005-01-15 22:15:51 +0000259static expr *(*bexpr) (int);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000260
H. Peter Anvine2c80182005-01-15 22:15:51 +0000261static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000262{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000263 expr *e, *f;
264
265 e = rexp1(critical);
266 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000267 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000268
H. Peter Anvine2c80182005-01-15 22:15:51 +0000269 while (i == TOKEN_DBL_OR) {
270 i = scan(scpriv, tokval);
271 f = rexp1(critical);
272 if (!f)
273 return NULL;
274 if (!(is_simple(e) || is_just_unknown(e)) ||
275 !(is_simple(f) || is_just_unknown(f))) {
276 error(ERR_NONFATAL, "`|' operator may only be applied to"
277 " scalar values");
278 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000279
H. Peter Anvine2c80182005-01-15 22:15:51 +0000280 if (is_just_unknown(e) || is_just_unknown(f))
281 e = unknown_expr();
282 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000283 e = scalarvect((int32_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000284 }
285 return e;
286}
287
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000289{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000290 expr *e, *f;
291
292 e = rexp2(critical);
293 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000294 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000295
H. Peter Anvine2c80182005-01-15 22:15:51 +0000296 while (i == TOKEN_DBL_XOR) {
297 i = scan(scpriv, tokval);
298 f = rexp2(critical);
299 if (!f)
300 return NULL;
301 if (!(is_simple(e) || is_just_unknown(e)) ||
302 !(is_simple(f) || is_just_unknown(f))) {
303 error(ERR_NONFATAL, "`^' operator may only be applied to"
304 " scalar values");
305 }
306
307 if (is_just_unknown(e) || is_just_unknown(f))
308 e = unknown_expr();
309 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000310 e = scalarvect((int32_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000311 }
312 return e;
313}
314
H. Peter Anvine2c80182005-01-15 22:15:51 +0000315static expr *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000316{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000317 expr *e, *f;
318
319 e = rexp3(critical);
320 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000321 return NULL;
322 while (i == TOKEN_DBL_AND) {
323 i = scan(scpriv, tokval);
324 f = rexp3(critical);
325 if (!f)
326 return NULL;
327 if (!(is_simple(e) || is_just_unknown(e)) ||
328 !(is_simple(f) || is_just_unknown(f))) {
329 error(ERR_NONFATAL, "`&' operator may only be applied to"
330 " scalar values");
331 }
332 if (is_just_unknown(e) || is_just_unknown(f))
333 e = unknown_expr();
334 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000335 e = scalarvect((int32_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000336 }
337 return e;
338}
339
H. Peter Anvine2c80182005-01-15 22:15:51 +0000340static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000341{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000342 expr *e, *f;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000343 int32_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000344
345 e = expr0(critical);
346 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000347 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000348
H. Peter Anvin76690a12002-04-30 20:52:49 +0000349 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000350 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
351 int j = i;
352 i = scan(scpriv, tokval);
353 f = expr0(critical);
354 if (!f)
355 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000356
H. Peter Anvine2c80182005-01-15 22:15:51 +0000357 e = add_vectors(e, scalar_mult(f, -1L, FALSE));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 switch (j) {
360 case TOKEN_EQ:
361 case TOKEN_NE:
362 if (is_unknown(e))
363 v = -1; /* means unknown */
364 else if (!is_really_simple(e) || reloc_value(e) != 0)
365 v = (j == TOKEN_NE); /* unequal, so return TRUE if NE */
366 else
367 v = (j == TOKEN_EQ); /* equal, so return TRUE if EQ */
368 break;
369 default:
370 if (is_unknown(e))
371 v = -1; /* means unknown */
372 else if (!is_really_simple(e)) {
373 error(ERR_NONFATAL,
374 "`%s': operands differ by a non-scalar",
375 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
376 TOKEN_GE ? ">=" : ">"));
377 v = 0; /* must set it to _something_ */
378 } else {
379 int vv = reloc_value(e);
380 if (vv == 0)
381 v = (j == TOKEN_LE || j == TOKEN_GE);
382 else if (vv > 0)
383 v = (j == TOKEN_GE || j == TOKEN_GT);
384 else /* vv < 0 */
385 v = (j == TOKEN_LE || j == TOKEN_LT);
386 }
387 break;
388 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000389
H. Peter Anvine2c80182005-01-15 22:15:51 +0000390 if (v == -1)
391 e = unknown_expr();
392 else
393 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000394 }
395 return e;
396}
397
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000400 expr *e, *f;
401
402 e = expr1(critical);
403 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000405
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 while (i == '|') {
407 i = scan(scpriv, tokval);
408 f = expr1(critical);
409 if (!f)
410 return NULL;
411 if (!(is_simple(e) || is_just_unknown(e)) ||
412 !(is_simple(f) || is_just_unknown(f))) {
413 error(ERR_NONFATAL, "`|' operator may only be applied to"
414 " scalar values");
415 }
416 if (is_just_unknown(e) || is_just_unknown(f))
417 e = unknown_expr();
418 else
419 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000420 }
421 return e;
422}
423
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000425{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000426 expr *e, *f;
427
428 e = expr2(critical);
429 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000431
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 i = scan(scpriv, tokval);
434 f = expr2(critical);
435 if (!f)
436 return NULL;
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"
440 " scalar values");
441 }
442 if (is_just_unknown(e) || is_just_unknown(f))
443 e = unknown_expr();
444 else
445 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 }
447 return e;
448}
449
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000451{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000452 expr *e, *f;
453
454 e = expr3(critical);
455 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000457
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 i = scan(scpriv, tokval);
460 f = expr3(critical);
461 if (!f)
462 return NULL;
463 if (!(is_simple(e) || is_just_unknown(e)) ||
464 !(is_simple(f) || is_just_unknown(f))) {
465 error(ERR_NONFATAL, "`&' operator may only be applied to"
466 " scalar values");
467 }
468 if (is_just_unknown(e) || is_just_unknown(f))
469 e = unknown_expr();
470 else
471 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472 }
473 return e;
474}
475
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000478 expr *e, *f;
479
480 e = expr4(critical);
481 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000483
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 while (i == TOKEN_SHL || i == TOKEN_SHR) {
485 int j = i;
486 i = scan(scpriv, tokval);
487 f = expr4(critical);
488 if (!f)
489 return NULL;
490 if (!(is_simple(e) || is_just_unknown(e)) ||
491 !(is_simple(f) || is_just_unknown(f))) {
492 error(ERR_NONFATAL, "shift operator may only be applied to"
493 " scalar values");
494 } else if (is_just_unknown(e) || is_just_unknown(f)) {
495 e = unknown_expr();
496 } else
497 switch (j) {
498 case TOKEN_SHL:
499 e = scalarvect(reloc_value(e) << reloc_value(f));
500 break;
501 case TOKEN_SHR:
Keith Kaniosb7a89542007-04-12 02:40:54 +0000502 e = scalarvect(((uint32_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503 reloc_value(f));
504 break;
505 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000506 }
507 return e;
508}
509
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000512 expr *e, *f;
513
514 e = expr5(critical);
515 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 return NULL;
517 while (i == '+' || i == '-') {
518 int j = i;
519 i = scan(scpriv, tokval);
520 f = expr5(critical);
521 if (!f)
522 return NULL;
523 switch (j) {
524 case '+':
525 e = add_vectors(e, f);
526 break;
527 case '-':
528 e = add_vectors(e, scalar_mult(f, -1L, FALSE));
529 break;
530 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000531 }
532 return e;
533}
534
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000536{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000537 expr *e, *f;
538
539 e = expr6(critical);
540 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000542 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 i == TOKEN_SDIV || i == TOKEN_SMOD) {
544 int j = i;
545 i = scan(scpriv, tokval);
546 f = expr6(critical);
547 if (!f)
548 return NULL;
549 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
550 !(is_simple(f) || is_just_unknown(f)))) {
551 error(ERR_NONFATAL, "division operator may only be applied to"
552 " scalar values");
553 return NULL;
554 }
555 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
556 error(ERR_NONFATAL, "division by zero");
557 return NULL;
558 }
559 switch (j) {
560 case '*':
561 if (is_simple(e))
562 e = scalar_mult(f, reloc_value(e), TRUE);
563 else if (is_simple(f))
564 e = scalar_mult(e, reloc_value(f), TRUE);
565 else if (is_just_unknown(e) && is_just_unknown(f))
566 e = unknown_expr();
567 else {
568 error(ERR_NONFATAL, "unable to multiply two "
569 "non-scalar objects");
570 return NULL;
571 }
572 break;
573 case '/':
574 if (is_just_unknown(e) || is_just_unknown(f))
575 e = unknown_expr();
576 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000577 e = scalarvect(((uint32_t)reloc_value(e)) /
578 ((uint32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 break;
580 case '%':
581 if (is_just_unknown(e) || is_just_unknown(f))
582 e = unknown_expr();
583 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000584 e = scalarvect(((uint32_t)reloc_value(e)) %
585 ((uint32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 break;
587 case TOKEN_SDIV:
588 if (is_just_unknown(e) || is_just_unknown(f))
589 e = unknown_expr();
590 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000591 e = scalarvect(((int32_t)reloc_value(e)) /
592 ((int32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 break;
594 case TOKEN_SMOD:
595 if (is_just_unknown(e) || is_just_unknown(f))
596 e = unknown_expr();
597 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000598 e = scalarvect(((int32_t)reloc_value(e)) %
599 ((int32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 break;
601 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000602 }
603 return e;
604}
605
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000607{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000608 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000609 expr *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000610 int32_t label_seg, label_ofs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000611
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700612 switch (i) {
613 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 i = scan(scpriv, tokval);
615 e = expr6(critical);
616 if (!e)
617 return NULL;
618 return scalar_mult(e, -1L, FALSE);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700619
620
621 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 i = scan(scpriv, tokval);
623 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700624
625 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 i = scan(scpriv, tokval);
627 e = expr6(critical);
628 if (!e)
629 return NULL;
630 if (is_just_unknown(e))
631 return unknown_expr();
632 else if (!is_simple(e)) {
633 error(ERR_NONFATAL, "`~' operator may only be applied to"
634 " scalar values");
635 return NULL;
636 }
637 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700638
639 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000640 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));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700652
653 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 i = scan(scpriv, tokval);
655 e = expr6(critical);
656 if (!e)
657 return NULL;
658 e = segment_part(e);
659 if (!e)
660 return NULL;
661 if (is_unknown(e) && critical) {
662 error(ERR_NONFATAL, "unable to determine segment base");
663 return NULL;
664 }
665 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700666
667 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000668 i = scan(scpriv, tokval);
669 e = bexpr(critical);
670 if (!e)
671 return NULL;
672 if (i != ')') {
673 error(ERR_NONFATAL, "expecting `)'");
674 return NULL;
675 }
676 i = scan(scpriv, tokval);
677 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700678
679 case TOKEN_NUM:
680 case TOKEN_REG:
681 case TOKEN_ID:
682 case TOKEN_HERE:
683 case TOKEN_BASE:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 begintemp();
685 switch (i) {
686 case TOKEN_NUM:
687 addtotemp(EXPR_SIMPLE, tokval->t_integer);
688 break;
689 case TOKEN_REG:
690 addtotemp(tokval->t_integer, 1L);
691 if (hint && hint->type == EAH_NOHINT)
692 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
693 break;
694 case TOKEN_ID:
695 case TOKEN_HERE:
696 case TOKEN_BASE:
697 /*
698 * If !location->known, this indicates that no
699 * symbol, Here or Base references are valid because we
700 * are in preprocess-only mode.
701 */
702 if (!location->known) {
703 error(ERR_NONFATAL,
704 "%s not supported in preprocess-only mode",
705 (i == TOKEN_ID ? "symbol references" :
706 i == TOKEN_HERE ? "`$'" : "`$$'"));
707 addtotemp(EXPR_UNKNOWN, 1L);
708 break;
709 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000710
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
712 if (i == TOKEN_BASE) {
713 label_seg = in_abs_seg ? abs_seg : location->segment;
714 label_ofs = 0;
715 } else if (i == TOKEN_HERE) {
716 label_seg = in_abs_seg ? abs_seg : location->segment;
717 label_ofs = in_abs_seg ? abs_offset : location->offset;
718 } else {
719 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
720 if (critical == 2) {
721 error(ERR_NONFATAL, "symbol `%s' undefined",
722 tokval->t_charptr);
723 return NULL;
724 } else if (critical == 1) {
725 error(ERR_NONFATAL,
726 "symbol `%s' not defined before use",
727 tokval->t_charptr);
728 return NULL;
729 } else {
730 if (opflags)
731 *opflags |= 1;
732 type = EXPR_UNKNOWN;
733 label_seg = NO_SEG;
734 label_ofs = 1;
735 }
736 }
737 if (opflags && is_extern(tokval->t_charptr))
738 *opflags |= OPFLAG_EXTERN;
739 }
740 addtotemp(type, label_ofs);
741 if (label_seg != NO_SEG)
742 addtotemp(EXPR_SEGBASE + label_seg, 1L);
743 break;
744 }
745 i = scan(scpriv, tokval);
746 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700747
748 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000749 error(ERR_NONFATAL, "expression syntax error");
750 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000751 }
752}
753
H. Peter Anvine2c80182005-01-15 22:15:51 +0000754void eval_global_info(struct ofmt *output, lfunc lookup_label,
755 loc_t * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000756{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000757 outfmt = output;
758 labelfunc = lookup_label;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000759 location = locp;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000760}
761
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
763 int *fwref, int critical, efunc report_error,
764 struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000765{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000766 expr *e;
767 expr *f = NULL;
768
769 hint = hints;
770 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000772
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000773 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 critical &= ~CRITICAL;
775 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000776 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000778
779 scan = sc;
780 scpriv = scprivate;
781 tokval = tv;
782 error = report_error;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000783 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000784
785 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000787 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000789
Keith Kaniosb7a89542007-04-12 02:40:54 +0000790 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000792
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000794 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000796
797 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 i = scan(scpriv, tokval); /* eat the WRT */
799 f = expr6(critical);
800 if (!f)
801 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000802 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 e = scalar_mult(e, 1L, FALSE); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000804 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 expr *g;
806 if (is_just_unknown(f))
807 g = unknown_expr();
808 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000809 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 begintemp();
811 if (!is_reloc(f)) {
812 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
813 return NULL;
814 }
815 value = reloc_seg(f);
816 if (value == NO_SEG)
817 value = reloc_value(f) | SEG_ABS;
818 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
819 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
820 return NULL;
821 }
822 addtotemp(EXPR_WRT, value);
823 g = finishtemp();
824 }
825 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000826 }
827 return e;
828}