blob: e8b10ede4b80820db0f2a9a43deca07d1cee0917 [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
H. Peter Anvinfe501952007-10-02 21:53:51 -070011#include "compiler.h"
12
H. Peter Anvin76690a12002-04-30 20:52:49 +000013#include <stdio.h>
14#include <stdlib.h>
15#include <stddef.h>
16#include <string.h>
17#include <ctype.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000018#include <inttypes.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000019
20#include "nasm.h"
21#include "nasmlib.h"
22#include "eval.h"
H. Peter Anvineba20a72002-04-30 20:53:55 +000023#include "labels.h"
H. Peter Anvindc467ba2007-09-24 12:30:54 -070024#include "float.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000025
H. Peter Anvin76690a12002-04-30 20:52:49 +000026#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000027#define TEMPEXPR_DELTA 8
28
H. Peter Anvine2c80182005-01-15 22:15:51 +000029static scanner scan; /* Address of scanner routine */
30static efunc error; /* Address of error reporting routine */
31static lfunc labelfunc; /* Address of label routine */
H. Peter Anvineba20a72002-04-30 20:53:55 +000032
H. Peter Anvine2c80182005-01-15 22:15:51 +000033static struct ofmt *outfmt; /* Structure of addresses of output routines */
H. Peter Anvineba20a72002-04-30 20:53:55 +000034
35static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000036static int ntempexprs;
37static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000038
H. Peter Anvine2c80182005-01-15 22:15:51 +000039static expr *tempexpr;
40static int ntempexpr;
41static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000042
H. Peter Anvine2c80182005-01-15 22:15:51 +000043static struct tokenval *tokval; /* The current token */
44static int i; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000045
H. Peter Anvin76690a12002-04-30 20:52:49 +000046static void *scpriv;
H. Peter Anvinbfc17122007-10-03 21:24:51 -070047static cloc_t *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000048static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000049
50static struct eval_hints *hint;
51
H. Peter Anvine2c80182005-01-15 22:15:51 +000052extern int in_abs_seg; /* ABSOLUTE segment flag */
Keith Kaniosb7a89542007-04-12 02:40:54 +000053extern int32_t abs_seg; /* ABSOLUTE segment */
54extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvin667dd802002-05-26 19:49:41 +000055
H. Peter Anvin76690a12002-04-30 20:52:49 +000056/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000057 * Unimportant cleanup is done to avoid confusing people who are trying
58 * to debug real memory leaks
59 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000060void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000061{
62 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000063 nasm_free(tempexprs[--ntempexprs]);
64 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000065}
66
67/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000068 * Construct a temporary expression.
69 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000070static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000071{
H. Peter Anvin76690a12002-04-30 20:52:49 +000072 tempexpr = NULL;
73 tempexpr_size = ntempexpr = 0;
74}
75
Keith Kaniosb7a89542007-04-12 02:40:54 +000076static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +000077{
H. Peter Anvin76690a12002-04-30 20:52:49 +000078 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000079 tempexpr_size += TEMPEXPR_DELTA;
80 tempexpr = nasm_realloc(tempexpr,
81 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +000082 }
83 tempexpr[ntempexpr].type = type;
84 tempexpr[ntempexpr++].value = value;
85}
86
H. Peter Anvine2c80182005-01-15 22:15:51 +000087static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000088{
H. Peter Anvine2c80182005-01-15 22:15:51 +000089 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +000090 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000091 tempexprs_size += TEMPEXPRS_DELTA;
92 tempexprs = nasm_realloc(tempexprs,
93 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +000094 }
95 return tempexprs[ntempexprs++] = tempexpr;
96}
97
98/*
99 * Add two vector datatypes. We have some bizarre behaviour on far-
100 * absolute segment types: we preserve them during addition _only_
101 * if one of the segments is a truly pure scalar.
102 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000104{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000105 int preserve;
106
107 preserve = is_really_simple(p) || is_really_simple(q);
108
109 begintemp();
110
111 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 p->type < EXPR_SEGBASE + SEG_ABS &&
113 q->type < EXPR_SEGBASE + SEG_ABS) {
114 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000115
H. Peter Anvine2c80182005-01-15 22:15:51 +0000116 if (p->type > q->type) {
117 addtotemp(q->type, q->value);
118 lasttype = q++->type;
119 } else if (p->type < q->type) {
120 addtotemp(p->type, p->value);
121 lasttype = p++->type;
122 } else { /* *p and *q have same type */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000123 int32_t sum = p->value + q->value;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124 if (sum)
125 addtotemp(p->type, sum);
126 lasttype = p->type;
127 p++, q++;
128 }
129 if (lasttype == EXPR_UNKNOWN) {
130 return finishtemp();
131 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000132 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
134 addtotemp(p->type, p->value);
135 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000136 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000137 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
138 addtotemp(q->type, q->value);
139 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000140 }
141
142 return finishtemp();
143}
144
145/*
146 * Multiply a vector by a scalar. Strip far-absolute segment part
147 * if present.
148 *
149 * Explicit treatment of UNKNOWN is not required in this routine,
150 * since it will silently do the Right Thing anyway.
151 *
152 * If `affect_hints' is set, we also change the hint type to
153 * NOTBASE if a MAKEBASE hint points at a register being
154 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
155 * as the base register.
156 */
Keith Kaniosb7a89542007-04-12 02:40:54 +0000157static expr *scalar_mult(expr * vect, int32_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000158{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000159 expr *p = vect;
160
H. Peter Anvine2c80182005-01-15 22:15:51 +0000161 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
162 p->value = scalar * (p->value);
163 if (hint && hint->type == EAH_MAKEBASE &&
164 p->type == hint->base && affect_hints)
165 hint->type = EAH_NOTBASE;
166 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000167 }
168 p->type = 0;
169
170 return vect;
171}
172
Keith Kaniosb7a89542007-04-12 02:40:54 +0000173static expr *scalarvect(int32_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000174{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000175 begintemp();
176 addtotemp(EXPR_SIMPLE, scalar);
177 return finishtemp();
178}
179
H. Peter Anvine2c80182005-01-15 22:15:51 +0000180static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000181{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000182 begintemp();
183 addtotemp(EXPR_UNKNOWN, 1L);
184 return finishtemp();
185}
186
187/*
188 * The SEG operator: calculate the segment part of a relocatable
189 * value. Return NULL, as usual, if an error occurs. Report the
190 * error too.
191 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000192static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000193{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000194 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000195
196 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000197 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000198
199 if (!is_reloc(e)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000200 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
201 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000202 }
203
204 seg = reloc_seg(e);
205 if (seg == NO_SEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206 error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
207 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000208 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000210 } else if (seg & 1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000211 error(ERR_NONFATAL, "SEG applied to something which"
212 " is already a segment base");
213 return NULL;
214 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000215 int32_t base = outfmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000216
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217 begintemp();
218 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
219 1L);
220 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000221 }
222}
223
224/*
225 * Recursive-descent parser. Called with a single boolean operand,
226 * which is TRUE if the evaluation is critical (i.e. unresolved
227 * symbols are an error condition). Must update the global `i' to
228 * reflect the token after the parsed string. May return NULL.
229 *
230 * evaluate() should report its own errors: on return it is assumed
231 * that if NULL has been returned, the error has already been
232 * reported.
233 */
234
235/*
236 * Grammar parsed is:
237 *
238 * expr : bexpr [ WRT expr6 ]
239 * bexpr : rexp0 or expr0 depending on relative-mode setting
240 * rexp0 : rexp1 [ {||} rexp1...]
241 * rexp1 : rexp2 [ {^^} rexp2...]
242 * rexp2 : rexp3 [ {&&} rexp3...]
243 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
244 * expr0 : expr1 [ {|} expr1...]
245 * expr1 : expr2 [ {^} expr2...]
246 * expr2 : expr3 [ {&} expr3...]
247 * expr3 : expr4 [ {<<,>>} expr4...]
248 * expr4 : expr5 [ {+,-} expr5...]
249 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
250 * expr6 : { ~,+,-,SEG } expr6
251 * | (bexpr)
252 * | symbol
253 * | $
254 * | number
255 */
256
257static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
258
259static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
260static expr *expr4(int), *expr5(int), *expr6(int);
261
H. Peter Anvine2c80182005-01-15 22:15:51 +0000262static expr *(*bexpr) (int);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000263
H. Peter Anvine2c80182005-01-15 22:15:51 +0000264static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000265{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000266 expr *e, *f;
267
268 e = rexp1(critical);
269 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000270 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000271
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272 while (i == TOKEN_DBL_OR) {
273 i = scan(scpriv, tokval);
274 f = rexp1(critical);
275 if (!f)
276 return NULL;
277 if (!(is_simple(e) || is_just_unknown(e)) ||
278 !(is_simple(f) || is_just_unknown(f))) {
279 error(ERR_NONFATAL, "`|' operator may only be applied to"
280 " scalar values");
281 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000282
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283 if (is_just_unknown(e) || is_just_unknown(f))
284 e = unknown_expr();
285 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000286 e = scalarvect((int32_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000287 }
288 return e;
289}
290
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000292{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000293 expr *e, *f;
294
295 e = rexp2(critical);
296 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000298
H. Peter Anvine2c80182005-01-15 22:15:51 +0000299 while (i == TOKEN_DBL_XOR) {
300 i = scan(scpriv, tokval);
301 f = rexp2(critical);
302 if (!f)
303 return NULL;
304 if (!(is_simple(e) || is_just_unknown(e)) ||
305 !(is_simple(f) || is_just_unknown(f))) {
306 error(ERR_NONFATAL, "`^' operator may only be applied to"
307 " scalar values");
308 }
309
310 if (is_just_unknown(e) || is_just_unknown(f))
311 e = unknown_expr();
312 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000313 e = scalarvect((int32_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000314 }
315 return e;
316}
317
H. Peter Anvine2c80182005-01-15 22:15:51 +0000318static expr *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000319{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000320 expr *e, *f;
321
322 e = rexp3(critical);
323 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000324 return NULL;
325 while (i == TOKEN_DBL_AND) {
326 i = scan(scpriv, tokval);
327 f = rexp3(critical);
328 if (!f)
329 return NULL;
330 if (!(is_simple(e) || is_just_unknown(e)) ||
331 !(is_simple(f) || is_just_unknown(f))) {
332 error(ERR_NONFATAL, "`&' operator may only be applied to"
333 " scalar values");
334 }
335 if (is_just_unknown(e) || is_just_unknown(f))
336 e = unknown_expr();
337 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000338 e = scalarvect((int32_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000339 }
340 return e;
341}
342
H. Peter Anvine2c80182005-01-15 22:15:51 +0000343static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000345 expr *e, *f;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000346 int32_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000347
348 e = expr0(critical);
349 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000350 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000351
H. Peter Anvin76690a12002-04-30 20:52:49 +0000352 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
354 int j = i;
355 i = scan(scpriv, tokval);
356 f = expr0(critical);
357 if (!f)
358 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000359
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360 e = add_vectors(e, scalar_mult(f, -1L, FALSE));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000361
H. Peter Anvine2c80182005-01-15 22:15:51 +0000362 switch (j) {
363 case TOKEN_EQ:
364 case TOKEN_NE:
365 if (is_unknown(e))
366 v = -1; /* means unknown */
367 else if (!is_really_simple(e) || reloc_value(e) != 0)
368 v = (j == TOKEN_NE); /* unequal, so return TRUE if NE */
369 else
370 v = (j == TOKEN_EQ); /* equal, so return TRUE if EQ */
371 break;
372 default:
373 if (is_unknown(e))
374 v = -1; /* means unknown */
375 else if (!is_really_simple(e)) {
376 error(ERR_NONFATAL,
377 "`%s': operands differ by a non-scalar",
378 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
379 TOKEN_GE ? ">=" : ">"));
380 v = 0; /* must set it to _something_ */
381 } else {
382 int vv = reloc_value(e);
383 if (vv == 0)
384 v = (j == TOKEN_LE || j == TOKEN_GE);
385 else if (vv > 0)
386 v = (j == TOKEN_GE || j == TOKEN_GT);
387 else /* vv < 0 */
388 v = (j == TOKEN_LE || j == TOKEN_LT);
389 }
390 break;
391 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000392
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 if (v == -1)
394 e = unknown_expr();
395 else
396 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000397 }
398 return e;
399}
400
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000402{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000403 expr *e, *f;
404
405 e = expr1(critical);
406 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000408
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 while (i == '|') {
410 i = scan(scpriv, tokval);
411 f = expr1(critical);
412 if (!f)
413 return NULL;
414 if (!(is_simple(e) || is_just_unknown(e)) ||
415 !(is_simple(f) || is_just_unknown(f))) {
416 error(ERR_NONFATAL, "`|' operator may only be applied to"
417 " scalar values");
418 }
419 if (is_just_unknown(e) || is_just_unknown(f))
420 e = unknown_expr();
421 else
422 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000423 }
424 return e;
425}
426
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000428{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000429 expr *e, *f;
430
431 e = expr2(critical);
432 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000434
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 i = scan(scpriv, tokval);
437 f = expr2(critical);
438 if (!f)
439 return NULL;
440 if (!(is_simple(e) || is_just_unknown(e)) ||
441 !(is_simple(f) || is_just_unknown(f))) {
442 error(ERR_NONFATAL, "`^' operator may only be applied to"
443 " scalar values");
444 }
445 if (is_just_unknown(e) || is_just_unknown(f))
446 e = unknown_expr();
447 else
448 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449 }
450 return e;
451}
452
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000454{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000455 expr *e, *f;
456
457 e = expr3(critical);
458 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000460
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 i = scan(scpriv, tokval);
463 f = expr3(critical);
464 if (!f)
465 return NULL;
466 if (!(is_simple(e) || is_just_unknown(e)) ||
467 !(is_simple(f) || is_just_unknown(f))) {
468 error(ERR_NONFATAL, "`&' operator may only be applied to"
469 " scalar values");
470 }
471 if (is_just_unknown(e) || is_just_unknown(f))
472 e = unknown_expr();
473 else
474 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000475 }
476 return e;
477}
478
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000480{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000481 expr *e, *f;
482
483 e = expr4(critical);
484 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000486
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 while (i == TOKEN_SHL || i == TOKEN_SHR) {
488 int j = i;
489 i = scan(scpriv, tokval);
490 f = expr4(critical);
491 if (!f)
492 return NULL;
493 if (!(is_simple(e) || is_just_unknown(e)) ||
494 !(is_simple(f) || is_just_unknown(f))) {
495 error(ERR_NONFATAL, "shift operator may only be applied to"
496 " scalar values");
497 } else if (is_just_unknown(e) || is_just_unknown(f)) {
498 e = unknown_expr();
499 } else
500 switch (j) {
501 case TOKEN_SHL:
502 e = scalarvect(reloc_value(e) << reloc_value(f));
503 break;
504 case TOKEN_SHR:
Keith Kaniosb7a89542007-04-12 02:40:54 +0000505 e = scalarvect(((uint32_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 reloc_value(f));
507 break;
508 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000509 }
510 return e;
511}
512
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000514{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000515 expr *e, *f;
516
517 e = expr5(critical);
518 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 return NULL;
520 while (i == '+' || i == '-') {
521 int j = i;
522 i = scan(scpriv, tokval);
523 f = expr5(critical);
524 if (!f)
525 return NULL;
526 switch (j) {
527 case '+':
528 e = add_vectors(e, f);
529 break;
530 case '-':
531 e = add_vectors(e, scalar_mult(f, -1L, FALSE));
532 break;
533 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000534 }
535 return e;
536}
537
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000539{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000540 expr *e, *f;
541
542 e = expr6(critical);
543 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000545 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 i == TOKEN_SDIV || i == TOKEN_SMOD) {
547 int j = i;
548 i = scan(scpriv, tokval);
549 f = expr6(critical);
550 if (!f)
551 return NULL;
552 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
553 !(is_simple(f) || is_just_unknown(f)))) {
554 error(ERR_NONFATAL, "division operator may only be applied to"
555 " scalar values");
556 return NULL;
557 }
558 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
559 error(ERR_NONFATAL, "division by zero");
560 return NULL;
561 }
562 switch (j) {
563 case '*':
564 if (is_simple(e))
565 e = scalar_mult(f, reloc_value(e), TRUE);
566 else if (is_simple(f))
567 e = scalar_mult(e, reloc_value(f), TRUE);
568 else if (is_just_unknown(e) && is_just_unknown(f))
569 e = unknown_expr();
570 else {
571 error(ERR_NONFATAL, "unable to multiply two "
572 "non-scalar objects");
573 return NULL;
574 }
575 break;
576 case '/':
577 if (is_just_unknown(e) || is_just_unknown(f))
578 e = unknown_expr();
579 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000580 e = scalarvect(((uint32_t)reloc_value(e)) /
581 ((uint32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 break;
583 case '%':
584 if (is_just_unknown(e) || is_just_unknown(f))
585 e = unknown_expr();
586 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000587 e = scalarvect(((uint32_t)reloc_value(e)) %
588 ((uint32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 break;
590 case TOKEN_SDIV:
591 if (is_just_unknown(e) || is_just_unknown(f))
592 e = unknown_expr();
593 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000594 e = scalarvect(((int32_t)reloc_value(e)) /
595 ((int32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 break;
597 case TOKEN_SMOD:
598 if (is_just_unknown(e) || is_just_unknown(f))
599 e = unknown_expr();
600 else
Keith Kaniosb7a89542007-04-12 02:40:54 +0000601 e = scalarvect(((int32_t)reloc_value(e)) %
602 ((int32_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 break;
604 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000605 }
606 return e;
607}
608
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700609static expr *eval_floatize(enum floatize type)
610{
611 uint8_t result[16], *p; /* Up to 128 bits */
612 static const struct {
613 int bytes, start, len;
614 } formats[] = {
615 { 2, 0, 2 }, /* FLOAT_16 */
616 { 4, 0, 4 }, /* FLOAT_32 */
617 { 8, 0, 8 }, /* FLOAT_64 */
618 { 10, 0, 8 }, /* FLOAT_80M */
619 { 10, 8, 2 }, /* FLOAT_80E */
620 { 16, 0, 8 }, /* FLOAT_128L */
621 { 16, 8, 8 }, /* FLOAT_128H */
622 };
623 int sign = 1;
624 int64_t val;
625 int j;
626
627 i = scan(scpriv, tokval);
628 if (i != '(') {
629 error(ERR_NONFATAL, "expecting `('");
630 return NULL;
631 }
632 i = scan(scpriv, tokval);
633 if (i == '-' || i == '+') {
634 sign = (i == '-') ? -1 : 1;
635 i = scan(scpriv, tokval);
636 }
637 if (i != TOKEN_FLOAT) {
638 error(ERR_NONFATAL, "expecting floating-point number");
639 return NULL;
640 }
641 if (!float_const(tokval->t_charptr, sign, result,
642 formats[type].bytes, error))
643 return NULL;
644 i = scan(scpriv, tokval);
645 if (i != ')') {
646 error(ERR_NONFATAL, "expecting `)'");
647 return NULL;
648 }
649
650 p = result+formats[type].start+formats[type].len;
651 val = 0;
652 for (j = formats[type].len; j; j--) {
653 p--;
654 val = (val << 8) + *p;
655 }
656
657 begintemp();
658 addtotemp(EXPR_SIMPLE, val);
659
660 i = scan(scpriv, tokval);
661 return finishtemp();
662}
663
H. Peter Anvine2c80182005-01-15 22:15:51 +0000664static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000665{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000666 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000667 expr *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000668 int32_t label_seg, label_ofs;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000669
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700670 switch (i) {
671 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 i = scan(scpriv, tokval);
673 e = expr6(critical);
674 if (!e)
675 return NULL;
676 return scalar_mult(e, -1L, FALSE);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700677
678
679 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000680 i = scan(scpriv, tokval);
681 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700682
683 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000684 i = scan(scpriv, tokval);
685 e = expr6(critical);
686 if (!e)
687 return NULL;
688 if (is_just_unknown(e))
689 return unknown_expr();
690 else if (!is_simple(e)) {
691 error(ERR_NONFATAL, "`~' operator may only be applied to"
692 " scalar values");
693 return NULL;
694 }
695 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700696
697 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000698 i = scan(scpriv, tokval);
699 e = expr6(critical);
700 if (!e)
701 return NULL;
702 if (is_just_unknown(e))
703 return unknown_expr();
704 else if (!is_simple(e)) {
705 error(ERR_NONFATAL, "`!' operator may only be applied to"
706 " scalar values");
707 return NULL;
708 }
709 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700710
711 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000712 i = scan(scpriv, tokval);
713 e = expr6(critical);
714 if (!e)
715 return NULL;
716 e = segment_part(e);
717 if (!e)
718 return NULL;
719 if (is_unknown(e) && critical) {
720 error(ERR_NONFATAL, "unable to determine segment base");
721 return NULL;
722 }
723 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700724
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700725 case TOKEN_FLOATIZE:
726 return eval_floatize(tokval->t_integer);
727
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700728 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 i = scan(scpriv, tokval);
730 e = bexpr(critical);
731 if (!e)
732 return NULL;
733 if (i != ')') {
734 error(ERR_NONFATAL, "expecting `)'");
735 return NULL;
736 }
737 i = scan(scpriv, tokval);
738 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700739
740 case TOKEN_NUM:
741 case TOKEN_REG:
742 case TOKEN_ID:
743 case TOKEN_HERE:
744 case TOKEN_BASE:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 begintemp();
746 switch (i) {
747 case TOKEN_NUM:
748 addtotemp(EXPR_SIMPLE, tokval->t_integer);
749 break;
750 case TOKEN_REG:
751 addtotemp(tokval->t_integer, 1L);
752 if (hint && hint->type == EAH_NOHINT)
753 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
754 break;
755 case TOKEN_ID:
756 case TOKEN_HERE:
757 case TOKEN_BASE:
758 /*
759 * If !location->known, this indicates that no
760 * symbol, Here or Base references are valid because we
761 * are in preprocess-only mode.
762 */
763 if (!location->known) {
764 error(ERR_NONFATAL,
765 "%s not supported in preprocess-only mode",
766 (i == TOKEN_ID ? "symbol references" :
767 i == TOKEN_HERE ? "`$'" : "`$$'"));
768 addtotemp(EXPR_UNKNOWN, 1L);
769 break;
770 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000771
H. Peter Anvine2c80182005-01-15 22:15:51 +0000772 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
773 if (i == TOKEN_BASE) {
774 label_seg = in_abs_seg ? abs_seg : location->segment;
775 label_ofs = 0;
776 } else if (i == TOKEN_HERE) {
777 label_seg = in_abs_seg ? abs_seg : location->segment;
778 label_ofs = in_abs_seg ? abs_offset : location->offset;
779 } else {
780 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
781 if (critical == 2) {
782 error(ERR_NONFATAL, "symbol `%s' undefined",
783 tokval->t_charptr);
784 return NULL;
785 } else if (critical == 1) {
786 error(ERR_NONFATAL,
787 "symbol `%s' not defined before use",
788 tokval->t_charptr);
789 return NULL;
790 } else {
791 if (opflags)
792 *opflags |= 1;
793 type = EXPR_UNKNOWN;
794 label_seg = NO_SEG;
795 label_ofs = 1;
796 }
797 }
798 if (opflags && is_extern(tokval->t_charptr))
799 *opflags |= OPFLAG_EXTERN;
800 }
801 addtotemp(type, label_ofs);
802 if (label_seg != NO_SEG)
803 addtotemp(EXPR_SEGBASE + label_seg, 1L);
804 break;
805 }
806 i = scan(scpriv, tokval);
807 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700808
809 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 error(ERR_NONFATAL, "expression syntax error");
811 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000812 }
813}
814
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815void eval_global_info(struct ofmt *output, lfunc lookup_label,
H. Peter Anvinbfc17122007-10-03 21:24:51 -0700816 cloc_t * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000817{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000818 outfmt = output;
819 labelfunc = lookup_label;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000820 location = locp;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000821}
822
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
824 int *fwref, int critical, efunc report_error,
825 struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000826{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000827 expr *e;
828 expr *f = NULL;
829
830 hint = hints;
831 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000833
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000834 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 critical &= ~CRITICAL;
836 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000837 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000839
840 scan = sc;
841 scpriv = scprivate;
842 tokval = tv;
843 error = report_error;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000844 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000845
846 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000848 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000850
Keith Kaniosb7a89542007-04-12 02:40:54 +0000851 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000853
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000855 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000857
858 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 i = scan(scpriv, tokval); /* eat the WRT */
860 f = expr6(critical);
861 if (!f)
862 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000863 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 e = scalar_mult(e, 1L, FALSE); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000865 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 expr *g;
867 if (is_just_unknown(f))
868 g = unknown_expr();
869 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000870 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 begintemp();
872 if (!is_reloc(f)) {
873 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
874 return NULL;
875 }
876 value = reloc_seg(f);
877 if (value == NO_SEG)
878 value = reloc_value(f) | SEG_ABS;
879 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
880 error(ERR_NONFATAL, "invalid right-hand operand to WRT");
881 return NULL;
882 }
883 addtotemp(EXPR_WRT, value);
884 g = finishtemp();
885 }
886 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000887 }
888 return e;
889}