blob: fda423eff1bf7b0cafe0bbe3aad547ea328980b5 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvin76690a12002-04-30 20:52:49 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * eval.c expression evaluator for the Netwide Assembler
H. Peter Anvin76690a12002-04-30 20:52:49 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvin76690a12002-04-30 20:52:49 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <ctype.h>
45
46#include "nasm.h"
47#include "nasmlib.h"
H. Peter Anvin0a126062017-09-27 13:34:42 -070048#include "ilog2.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000050#include "eval.h"
H. Peter Anvineba20a72002-04-30 20:53:55 +000051#include "labels.h"
H. Peter Anvindc467ba2007-09-24 12:30:54 -070052#include "float.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080053#include "assemble.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000054
H. Peter Anvin76690a12002-04-30 20:52:49 +000055#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000056#define TEMPEXPR_DELTA 8
57
H. Peter Anvine2c80182005-01-15 22:15:51 +000058static scanner scan; /* Address of scanner routine */
H. Peter Anvineba20a72002-04-30 20:53:55 +000059
H. Peter Anvineba20a72002-04-30 20:53:55 +000060static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000061static int ntempexprs;
62static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000063
H. Peter Anvine2c80182005-01-15 22:15:51 +000064static expr *tempexpr;
65static int ntempexpr;
66static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000067
H. Peter Anvine2c80182005-01-15 22:15:51 +000068static struct tokenval *tokval; /* The current token */
69static int i; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000070
H. Peter Anvin76690a12002-04-30 20:52:49 +000071static void *scpriv;
H. Peter Anvineba20a72002-04-30 20:53:55 +000072static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000073
74static struct eval_hints *hint;
H. Peter Anvina3d96d02018-06-15 17:51:39 -070075static int64_t deadman;
H. Peter Anvin76690a12002-04-30 20:52:49 +000076
H. Peter Anvin667dd802002-05-26 19:49:41 +000077
H. Peter Anvin76690a12002-04-30 20:52:49 +000078/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000079 * Unimportant cleanup is done to avoid confusing people who are trying
80 * to debug real memory leaks
81 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000082void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000083{
84 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000085 nasm_free(tempexprs[--ntempexprs]);
86 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000087}
88
89/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000090 * Construct a temporary expression.
91 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000092static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000093{
H. Peter Anvin76690a12002-04-30 20:52:49 +000094 tempexpr = NULL;
95 tempexpr_size = ntempexpr = 0;
96}
97
Keith Kaniosb7a89542007-04-12 02:40:54 +000098static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +000099{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000100 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000101 tempexpr_size += TEMPEXPR_DELTA;
102 tempexpr = nasm_realloc(tempexpr,
103 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000104 }
105 tempexpr[ntempexpr].type = type;
106 tempexpr[ntempexpr++].value = value;
107}
108
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000110{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000111 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000112 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000113 tempexprs_size += TEMPEXPRS_DELTA;
114 tempexprs = nasm_realloc(tempexprs,
115 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000116 }
117 return tempexprs[ntempexprs++] = tempexpr;
118}
119
120/*
121 * Add two vector datatypes. We have some bizarre behaviour on far-
122 * absolute segment types: we preserve them during addition _only_
123 * if one of the segments is a truly pure scalar.
124 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000125static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000126{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000127 int preserve;
128
129 preserve = is_really_simple(p) || is_really_simple(q);
130
131 begintemp();
132
133 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000134 p->type < EXPR_SEGBASE + SEG_ABS &&
135 q->type < EXPR_SEGBASE + SEG_ABS) {
136 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000137
H. Peter Anvine2c80182005-01-15 22:15:51 +0000138 if (p->type > q->type) {
139 addtotemp(q->type, q->value);
140 lasttype = q++->type;
141 } else if (p->type < q->type) {
142 addtotemp(p->type, p->value);
143 lasttype = p++->type;
144 } else { /* *p and *q have same type */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700145 int64_t sum = p->value + q->value;
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800146 if (sum) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000147 addtotemp(p->type, sum);
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800148 if (hint)
149 hint->type = EAH_SUMMED;
150 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000151 lasttype = p->type;
152 p++, q++;
153 }
154 if (lasttype == EXPR_UNKNOWN) {
155 return finishtemp();
156 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000157 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000158 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
159 addtotemp(p->type, p->value);
160 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000161 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000162 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
163 addtotemp(q->type, q->value);
164 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000165 }
166
167 return finishtemp();
168}
169
170/*
171 * Multiply a vector by a scalar. Strip far-absolute segment part
172 * if present.
173 *
174 * Explicit treatment of UNKNOWN is not required in this routine,
175 * since it will silently do the Right Thing anyway.
176 *
177 * If `affect_hints' is set, we also change the hint type to
178 * NOTBASE if a MAKEBASE hint points at a register being
179 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
180 * as the base register.
181 */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700182static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000183{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000184 expr *p = vect;
185
H. Peter Anvine2c80182005-01-15 22:15:51 +0000186 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
187 p->value = scalar * (p->value);
188 if (hint && hint->type == EAH_MAKEBASE &&
189 p->type == hint->base && affect_hints)
190 hint->type = EAH_NOTBASE;
191 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000192 }
193 p->type = 0;
194
195 return vect;
196}
197
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700198static expr *scalarvect(int64_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000199{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000200 begintemp();
201 addtotemp(EXPR_SIMPLE, scalar);
202 return finishtemp();
203}
204
H. Peter Anvine2c80182005-01-15 22:15:51 +0000205static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000206{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000207 begintemp();
208 addtotemp(EXPR_UNKNOWN, 1L);
209 return finishtemp();
210}
211
212/*
213 * The SEG operator: calculate the segment part of a relocatable
214 * value. Return NULL, as usual, if an error occurs. Report the
215 * error too.
216 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000217static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000218{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000219 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000220
221 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000222 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000223
224 if (!is_reloc(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800225 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000227 }
228
229 seg = reloc_seg(e);
230 if (seg == NO_SEG) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800231 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000233 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000234 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000235 } else if (seg & 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800236 nasm_error(ERR_NONFATAL, "SEG applied to something which"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000237 " is already a segment base");
238 return NULL;
239 } else {
H. Peter Anvin36034ec2016-02-18 01:18:50 -0800240 int32_t base = ofmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000241
H. Peter Anvine2c80182005-01-15 22:15:51 +0000242 begintemp();
243 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
244 1L);
245 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000246 }
247}
248
249/*
250 * Recursive-descent parser. Called with a single boolean operand,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700251 * which is true if the evaluation is critical (i.e. unresolved
H. Peter Anvin76690a12002-04-30 20:52:49 +0000252 * symbols are an error condition). Must update the global `i' to
253 * reflect the token after the parsed string. May return NULL.
254 *
255 * evaluate() should report its own errors: on return it is assumed
256 * that if NULL has been returned, the error has already been
257 * reported.
258 */
259
260/*
261 * Grammar parsed is:
262 *
263 * expr : bexpr [ WRT expr6 ]
264 * bexpr : rexp0 or expr0 depending on relative-mode setting
265 * rexp0 : rexp1 [ {||} rexp1...]
266 * rexp1 : rexp2 [ {^^} rexp2...]
267 * rexp2 : rexp3 [ {&&} rexp3...]
268 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
269 * expr0 : expr1 [ {|} expr1...]
270 * expr1 : expr2 [ {^} expr2...]
271 * expr2 : expr3 [ {&} expr3...]
272 * expr3 : expr4 [ {<<,>>} expr4...]
273 * expr4 : expr5 [ {+,-} expr5...]
274 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700275 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000276 * | (bexpr)
277 * | symbol
278 * | $
279 * | number
280 */
281
282static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
283
284static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
285static expr *expr4(int), *expr5(int), *expr6(int);
286
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287static expr *(*bexpr) (int);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000288
H. Peter Anvine2c80182005-01-15 22:15:51 +0000289static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000290{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000291 expr *e, *f;
292
293 e = rexp1(critical);
294 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000295 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000296
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297 while (i == TOKEN_DBL_OR) {
298 i = scan(scpriv, tokval);
299 f = rexp1(critical);
300 if (!f)
301 return NULL;
302 if (!(is_simple(e) || is_just_unknown(e)) ||
303 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800304 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305 " scalar values");
306 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000307
H. Peter Anvine2c80182005-01-15 22:15:51 +0000308 if (is_just_unknown(e) || is_just_unknown(f))
309 e = unknown_expr();
310 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700311 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000312 }
313 return e;
314}
315
H. Peter Anvine2c80182005-01-15 22:15:51 +0000316static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000317{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000318 expr *e, *f;
319
320 e = rexp2(critical);
321 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000323
H. Peter Anvine2c80182005-01-15 22:15:51 +0000324 while (i == TOKEN_DBL_XOR) {
325 i = scan(scpriv, tokval);
326 f = rexp2(critical);
327 if (!f)
328 return NULL;
329 if (!(is_simple(e) || is_just_unknown(e)) ||
330 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800331 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000332 " scalar values");
333 }
334
335 if (is_just_unknown(e) || is_just_unknown(f))
336 e = unknown_expr();
337 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700338 e = scalarvect((int64_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 *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000345 expr *e, *f;
346
347 e = rexp3(critical);
348 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000349 return NULL;
350 while (i == TOKEN_DBL_AND) {
351 i = scan(scpriv, tokval);
352 f = rexp3(critical);
353 if (!f)
354 return NULL;
355 if (!(is_simple(e) || is_just_unknown(e)) ||
356 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800357 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000358 " scalar values");
359 }
360 if (is_just_unknown(e) || is_just_unknown(f))
361 e = unknown_expr();
362 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700363 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000364 }
365 return e;
366}
367
H. Peter Anvine2c80182005-01-15 22:15:51 +0000368static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000369{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000370 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700371 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000372
373 e = expr0(critical);
374 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000376
H. Peter Anvin76690a12002-04-30 20:52:49 +0000377 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700378 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE ||
379 i == TOKEN_LEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 int j = i;
381 i = scan(scpriv, tokval);
382 f = expr0(critical);
383 if (!f)
384 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000385
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700386 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000387
H. Peter Anvine2c80182005-01-15 22:15:51 +0000388 switch (j) {
389 case TOKEN_EQ:
390 case TOKEN_NE:
391 if (is_unknown(e))
392 v = -1; /* means unknown */
393 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700394 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700396 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 break;
398 default:
399 if (is_unknown(e))
400 v = -1; /* means unknown */
401 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800402 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 "`%s': operands differ by a non-scalar",
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700404 (j == TOKEN_LE ? "<=" :
405 j == TOKEN_LT ? "<" :
406 j == TOKEN_GE ? ">=" :
407 j == TOKEN_GT ? ">" :
408 j == TOKEN_LEG ? "<=>" :
409 "<internal error>"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 v = 0; /* must set it to _something_ */
411 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300412 int64_t vv = reloc_value(e);
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700413 if (j == TOKEN_LEG)
414 v = (vv < 0) ? -1 : (vv > 0) ? 1 : 0;
415 else if (vv == 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 v = (j == TOKEN_LE || j == TOKEN_GE);
417 else if (vv > 0)
418 v = (j == TOKEN_GE || j == TOKEN_GT);
419 else /* vv < 0 */
420 v = (j == TOKEN_LE || j == TOKEN_LT);
421 }
422 break;
423 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000424
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425 if (v == -1)
426 e = unknown_expr();
427 else
428 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000429 }
430 return e;
431}
432
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000434{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435 expr *e, *f;
436
437 e = expr1(critical);
438 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000440
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 while (i == '|') {
442 i = scan(scpriv, tokval);
443 f = expr1(critical);
444 if (!f)
445 return NULL;
446 if (!(is_simple(e) || is_just_unknown(e)) ||
447 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800448 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000449 " scalar values");
450 }
451 if (is_just_unknown(e) || is_just_unknown(f))
452 e = unknown_expr();
453 else
454 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000455 }
456 return e;
457}
458
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000460{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461 expr *e, *f;
462
463 e = expr2(critical);
464 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000466
H. Peter Anvin76690a12002-04-30 20:52:49 +0000467 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 i = scan(scpriv, tokval);
469 f = expr2(critical);
470 if (!f)
471 return NULL;
472 if (!(is_simple(e) || is_just_unknown(e)) ||
473 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800474 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000475 " scalar values");
476 }
477 if (is_just_unknown(e) || is_just_unknown(f))
478 e = unknown_expr();
479 else
480 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000481 }
482 return e;
483}
484
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000486{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000487 expr *e, *f;
488
489 e = expr3(critical);
490 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000492
H. Peter Anvin76690a12002-04-30 20:52:49 +0000493 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 i = scan(scpriv, tokval);
495 f = expr3(critical);
496 if (!f)
497 return NULL;
498 if (!(is_simple(e) || is_just_unknown(e)) ||
499 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800500 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 " scalar values");
502 }
503 if (is_just_unknown(e) || is_just_unknown(f))
504 e = unknown_expr();
505 else
506 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000507 }
508 return e;
509}
510
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000512{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000513 expr *e, *f;
514
515 e = expr4(critical);
516 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000517 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700519 while (i == TOKEN_SHL || i == TOKEN_SHR || i == TOKEN_SAR) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 int j = i;
521 i = scan(scpriv, tokval);
522 f = expr4(critical);
523 if (!f)
524 return NULL;
525 if (!(is_simple(e) || is_just_unknown(e)) ||
526 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800527 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 " scalar values");
529 } else if (is_just_unknown(e) || is_just_unknown(f)) {
530 e = unknown_expr();
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700531 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532 switch (j) {
533 case TOKEN_SHL:
534 e = scalarvect(reloc_value(e) << reloc_value(f));
535 break;
536 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700537 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 reloc_value(f));
539 break;
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700540 case TOKEN_SAR:
541 e = scalarvect(((int64_t)reloc_value(e)) >>
542 reloc_value(f));
543 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 }
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700545 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000546 }
547 return e;
548}
549
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000551{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000552 expr *e, *f;
553
554 e = expr5(critical);
555 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 return NULL;
557 while (i == '+' || i == '-') {
558 int j = i;
559 i = scan(scpriv, tokval);
560 f = expr5(critical);
561 if (!f)
562 return NULL;
563 switch (j) {
564 case '+':
565 e = add_vectors(e, f);
566 break;
567 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700568 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 break;
570 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000571 }
572 return e;
573}
574
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000577 expr *e, *f;
578
579 e = expr6(critical);
580 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000582 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 i == TOKEN_SDIV || i == TOKEN_SMOD) {
584 int j = i;
585 i = scan(scpriv, tokval);
586 f = expr6(critical);
587 if (!f)
588 return NULL;
589 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
590 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800591 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 " scalar values");
593 return NULL;
594 }
595 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800596 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 return NULL;
598 }
599 switch (j) {
600 case '*':
601 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700602 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700604 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 else if (is_just_unknown(e) && is_just_unknown(f))
606 e = unknown_expr();
607 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800608 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 "non-scalar objects");
610 return NULL;
611 }
612 break;
613 case '/':
614 if (is_just_unknown(e) || is_just_unknown(f))
615 e = unknown_expr();
616 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700617 e = scalarvect(((uint64_t)reloc_value(e)) /
618 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 break;
620 case '%':
621 if (is_just_unknown(e) || is_just_unknown(f))
622 e = unknown_expr();
623 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700624 e = scalarvect(((uint64_t)reloc_value(e)) %
625 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 break;
627 case TOKEN_SDIV:
628 if (is_just_unknown(e) || is_just_unknown(f))
629 e = unknown_expr();
630 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700631 e = scalarvect(((int64_t)reloc_value(e)) /
632 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 break;
634 case TOKEN_SMOD:
635 if (is_just_unknown(e) || is_just_unknown(f))
636 e = unknown_expr();
637 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700638 e = scalarvect(((int64_t)reloc_value(e)) %
639 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 break;
641 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000642 }
643 return e;
644}
645
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700646static expr *eval_floatize(enum floatize type)
647{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300648 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700649 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300650 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700651 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300652 { 1, 0, 1 }, /* FLOAT_8 */
653 { 2, 0, 2 }, /* FLOAT_16 */
654 { 4, 0, 4 }, /* FLOAT_32 */
655 { 8, 0, 8 }, /* FLOAT_64 */
656 { 10, 0, 8 }, /* FLOAT_80M */
657 { 10, 8, 2 }, /* FLOAT_80E */
658 { 16, 0, 8 }, /* FLOAT_128L */
659 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700660 };
661 int sign = 1;
662 int64_t val;
663 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700664
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700665 i = scan(scpriv, tokval);
666 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800667 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300668 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700669 }
670 i = scan(scpriv, tokval);
671 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300672 sign = (i == '-') ? -1 : 1;
673 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700674 }
675 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800676 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300677 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700678 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800679 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300680 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700681 i = scan(scpriv, tokval);
682 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800683 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300684 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700685 }
686
687 p = result+formats[type].start+formats[type].len;
688 val = 0;
689 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300690 p--;
691 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700692 }
693
694 begintemp();
695 addtotemp(EXPR_SIMPLE, val);
696
697 i = scan(scpriv, tokval);
698 return finishtemp();
699}
700
H. Peter Anvin9c749102008-06-14 21:08:38 -0700701static expr *eval_strfunc(enum strfunc type)
702{
703 char *string;
704 size_t string_len;
705 int64_t val;
706 bool parens, rn_warn;
707
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200708 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700709 i = scan(scpriv, tokval);
710 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300711 parens = true;
712 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700713 }
714 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800715 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300716 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700717 }
718 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300719 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700720 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800721 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300722 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700723 }
724
725 val = readstrnum(string, string_len, &rn_warn);
726 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300727 i = scan(scpriv, tokval);
728 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800729 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300730 return NULL;
731 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700732 }
733
734 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800735 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700736
737 begintemp();
738 addtotemp(EXPR_SIMPLE, val);
739
740 i = scan(scpriv, tokval);
741 return finishtemp();
742}
743
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700744static int64_t eval_ifunc(int64_t val, enum ifunc func)
745{
746 int errtype;
747 uint64_t uval = (uint64_t)val;
748 int64_t rv;
749
750 switch (func) {
751 case IFUNC_ILOG2E:
752 case IFUNC_ILOG2W:
753 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400754
755 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800756 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700757 /* fall through */
758 case IFUNC_ILOG2F:
759 rv = ilog2_64(uval);
760 break;
761
762 case IFUNC_ILOG2C:
763 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
764 break;
765
766 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700767 nasm_panic("invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700768 rv = 0;
769 break;
770 }
771
772 return rv;
773}
774
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000776{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000777 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000778 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800779 int32_t label_seg;
780 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700781 int64_t tmpval;
782 bool rn_warn;
H. Peter Anvin98578072018-06-01 18:02:54 -0700783 const char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000784
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700785 if (++deadman > nasm_limit[LIMIT_EVAL]) {
786 nasm_error(ERR_NONFATAL, "expression too long");
787 return NULL;
788 }
789
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700790 switch (i) {
791 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 i = scan(scpriv, tokval);
793 e = expr6(critical);
794 if (!e)
795 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700796 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700797
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700798 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 i = scan(scpriv, tokval);
800 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700801
802 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 i = scan(scpriv, tokval);
804 e = expr6(critical);
805 if (!e)
806 return NULL;
807 if (is_just_unknown(e))
808 return unknown_expr();
809 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800810 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 " scalar values");
812 return NULL;
813 }
814 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700815
816 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000817 i = scan(scpriv, tokval);
818 e = expr6(critical);
819 if (!e)
820 return NULL;
821 if (is_just_unknown(e))
822 return unknown_expr();
823 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800824 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000825 " scalar values");
826 return NULL;
827 }
828 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700829
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700830 case TOKEN_IFUNC:
831 {
832 enum ifunc func = tokval->t_integer;
833 i = scan(scpriv, tokval);
834 e = expr6(critical);
835 if (!e)
836 return NULL;
837 if (is_just_unknown(e))
838 return unknown_expr();
839 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800840 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700841 " scalar values");
842 return NULL;
843 }
844 return scalarvect(eval_ifunc(reloc_value(e), func));
845 }
846
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700847 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000848 i = scan(scpriv, tokval);
849 e = expr6(critical);
850 if (!e)
851 return NULL;
852 e = segment_part(e);
853 if (!e)
854 return NULL;
855 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800856 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 return NULL;
858 }
859 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700860
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700861 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300862 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700863
H. Peter Anvin9c749102008-06-14 21:08:38 -0700864 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300865 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700866
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700867 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 i = scan(scpriv, tokval);
869 e = bexpr(critical);
870 if (!e)
871 return NULL;
872 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800873 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 return NULL;
875 }
876 i = scan(scpriv, tokval);
877 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700878
879 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700880 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700881 case TOKEN_REG:
882 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300883 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700884 case TOKEN_HERE:
885 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700886 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 begintemp();
888 switch (i) {
889 case TOKEN_NUM:
890 addtotemp(EXPR_SIMPLE, tokval->t_integer);
891 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300892 case TOKEN_STR:
893 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
894 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800895 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700896 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300897 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 case TOKEN_REG:
899 addtotemp(tokval->t_integer, 1L);
900 if (hint && hint->type == EAH_NOHINT)
901 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
902 break;
903 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300904 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 case TOKEN_HERE:
906 case TOKEN_BASE:
907 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800908 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 * symbol, Here or Base references are valid because we
910 * are in preprocess-only mode.
911 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800912 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800913 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300915 (i == TOKEN_HERE ? "`$'" :
916 i == TOKEN_BASE ? "`$$'" :
917 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 addtotemp(EXPR_UNKNOWN, 1L);
919 break;
920 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000921
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
923 if (i == TOKEN_BASE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800924 label_seg = in_absolute ? absolute.segment : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 label_ofs = 0;
926 } else if (i == TOKEN_HERE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800927 label_seg = in_absolute ? absolute.segment : location.segment;
928 label_ofs = in_absolute ? absolute.offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800930 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700931 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800933 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700934 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 return NULL;
936 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800937 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700938 "symbol `%s%s' not defined before use",
939 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 return NULL;
941 } else {
942 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400943 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 type = EXPR_UNKNOWN;
945 label_seg = NO_SEG;
946 label_ofs = 1;
947 }
948 }
949 if (opflags && is_extern(tokval->t_charptr))
950 *opflags |= OPFLAG_EXTERN;
951 }
952 addtotemp(type, label_ofs);
953 if (label_seg != NO_SEG)
954 addtotemp(EXPR_SEGBASE + label_seg, 1L);
955 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700956 case TOKEN_DECORATOR:
957 addtotemp(EXPR_RDSAE, tokval->t_integer);
958 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 }
960 i = scan(scpriv, tokval);
961 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700962
963 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800964 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000966 }
967}
968
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800970 int *fwref, int critical, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000971{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000972 expr *e;
973 expr *f = NULL;
974
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700975 deadman = 0;
976
H. Peter Anvin76690a12002-04-30 20:52:49 +0000977 hint = hints;
978 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000980
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000981 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 critical &= ~CRITICAL;
983 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000984 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000986
987 scan = sc;
988 scpriv = scprivate;
989 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000990 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000991
992 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000994 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000996
Keith Kaniosb7a89542007-04-12 02:40:54 +0000997 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000999
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001001 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001003
1004 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 i = scan(scpriv, tokval); /* eat the WRT */
1006 f = expr6(critical);
1007 if (!f)
1008 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001009 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001010 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001011 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 expr *g;
1013 if (is_just_unknown(f))
1014 g = unknown_expr();
1015 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001016 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 begintemp();
1018 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001019 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 return NULL;
1021 }
1022 value = reloc_seg(f);
1023 if (value == NO_SEG)
1024 value = reloc_value(f) | SEG_ABS;
1025 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001026 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 return NULL;
1028 }
1029 addtotemp(EXPR_WRT, value);
1030 g = finishtemp();
1031 }
1032 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001033 }
1034 return e;
1035}