blob: 816983b93d868454522e6dbc11a28a4fcff0a3f7 [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 Anvine2c80182005-01-15 22:15:51 +0000378 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
379 int j = i;
380 i = scan(scpriv, tokval);
381 f = expr0(critical);
382 if (!f)
383 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000384
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700385 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000386
H. Peter Anvine2c80182005-01-15 22:15:51 +0000387 switch (j) {
388 case TOKEN_EQ:
389 case TOKEN_NE:
390 if (is_unknown(e))
391 v = -1; /* means unknown */
392 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700393 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700395 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 break;
397 default:
398 if (is_unknown(e))
399 v = -1; /* means unknown */
400 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800401 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 "`%s': operands differ by a non-scalar",
403 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
404 TOKEN_GE ? ">=" : ">"));
405 v = 0; /* must set it to _something_ */
406 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300407 int64_t vv = reloc_value(e);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 if (vv == 0)
409 v = (j == TOKEN_LE || j == TOKEN_GE);
410 else if (vv > 0)
411 v = (j == TOKEN_GE || j == TOKEN_GT);
412 else /* vv < 0 */
413 v = (j == TOKEN_LE || j == TOKEN_LT);
414 }
415 break;
416 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000417
H. Peter Anvine2c80182005-01-15 22:15:51 +0000418 if (v == -1)
419 e = unknown_expr();
420 else
421 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000422 }
423 return e;
424}
425
H. Peter Anvine2c80182005-01-15 22:15:51 +0000426static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000427{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000428 expr *e, *f;
429
430 e = expr1(critical);
431 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000433
H. Peter Anvine2c80182005-01-15 22:15:51 +0000434 while (i == '|') {
435 i = scan(scpriv, tokval);
436 f = expr1(critical);
437 if (!f)
438 return NULL;
439 if (!(is_simple(e) || is_just_unknown(e)) ||
440 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800441 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 " scalar values");
443 }
444 if (is_just_unknown(e) || is_just_unknown(f))
445 e = unknown_expr();
446 else
447 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000448 }
449 return e;
450}
451
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000453{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000454 expr *e, *f;
455
456 e = expr2(critical);
457 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000459
H. Peter Anvin76690a12002-04-30 20:52:49 +0000460 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000461 i = scan(scpriv, tokval);
462 f = expr2(critical);
463 if (!f)
464 return NULL;
465 if (!(is_simple(e) || is_just_unknown(e)) ||
466 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800467 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 " scalar values");
469 }
470 if (is_just_unknown(e) || is_just_unknown(f))
471 e = unknown_expr();
472 else
473 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000474 }
475 return e;
476}
477
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000479{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000480 expr *e, *f;
481
482 e = expr3(critical);
483 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000485
H. Peter Anvin76690a12002-04-30 20:52:49 +0000486 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000487 i = scan(scpriv, tokval);
488 f = expr3(critical);
489 if (!f)
490 return NULL;
491 if (!(is_simple(e) || is_just_unknown(e)) ||
492 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800493 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 " scalar values");
495 }
496 if (is_just_unknown(e) || is_just_unknown(f))
497 e = unknown_expr();
498 else
499 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000500 }
501 return e;
502}
503
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000505{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000506 expr *e, *f;
507
508 e = expr4(critical);
509 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000511
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700512 while (i == TOKEN_SHL || i == TOKEN_SHR || i == TOKEN_SAR) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000513 int j = i;
514 i = scan(scpriv, tokval);
515 f = expr4(critical);
516 if (!f)
517 return NULL;
518 if (!(is_simple(e) || is_just_unknown(e)) ||
519 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800520 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 " scalar values");
522 } else if (is_just_unknown(e) || is_just_unknown(f)) {
523 e = unknown_expr();
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700524 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 switch (j) {
526 case TOKEN_SHL:
527 e = scalarvect(reloc_value(e) << reloc_value(f));
528 break;
529 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700530 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 reloc_value(f));
532 break;
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700533 case TOKEN_SAR:
534 e = scalarvect(((int64_t)reloc_value(e)) >>
535 reloc_value(f));
536 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 }
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700538 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000539 }
540 return e;
541}
542
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000544{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000545 expr *e, *f;
546
547 e = expr5(critical);
548 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 return NULL;
550 while (i == '+' || i == '-') {
551 int j = i;
552 i = scan(scpriv, tokval);
553 f = expr5(critical);
554 if (!f)
555 return NULL;
556 switch (j) {
557 case '+':
558 e = add_vectors(e, f);
559 break;
560 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700561 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 break;
563 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000564 }
565 return e;
566}
567
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000569{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000570 expr *e, *f;
571
572 e = expr6(critical);
573 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000575 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 i == TOKEN_SDIV || i == TOKEN_SMOD) {
577 int j = i;
578 i = scan(scpriv, tokval);
579 f = expr6(critical);
580 if (!f)
581 return NULL;
582 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
583 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800584 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000585 " scalar values");
586 return NULL;
587 }
588 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800589 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 return NULL;
591 }
592 switch (j) {
593 case '*':
594 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700595 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700597 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 else if (is_just_unknown(e) && is_just_unknown(f))
599 e = unknown_expr();
600 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800601 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 "non-scalar objects");
603 return NULL;
604 }
605 break;
606 case '/':
607 if (is_just_unknown(e) || is_just_unknown(f))
608 e = unknown_expr();
609 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700610 e = scalarvect(((uint64_t)reloc_value(e)) /
611 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 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 TOKEN_SDIV:
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(((int64_t)reloc_value(e)) /
625 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 break;
627 case TOKEN_SMOD:
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 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000635 }
636 return e;
637}
638
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700639static expr *eval_floatize(enum floatize type)
640{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300641 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700642 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300643 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700644 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300645 { 1, 0, 1 }, /* FLOAT_8 */
646 { 2, 0, 2 }, /* FLOAT_16 */
647 { 4, 0, 4 }, /* FLOAT_32 */
648 { 8, 0, 8 }, /* FLOAT_64 */
649 { 10, 0, 8 }, /* FLOAT_80M */
650 { 10, 8, 2 }, /* FLOAT_80E */
651 { 16, 0, 8 }, /* FLOAT_128L */
652 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700653 };
654 int sign = 1;
655 int64_t val;
656 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700657
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700658 i = scan(scpriv, tokval);
659 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800660 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300661 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700662 }
663 i = scan(scpriv, tokval);
664 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300665 sign = (i == '-') ? -1 : 1;
666 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700667 }
668 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800669 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300670 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700671 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800672 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300673 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700674 i = scan(scpriv, tokval);
675 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800676 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300677 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700678 }
679
680 p = result+formats[type].start+formats[type].len;
681 val = 0;
682 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300683 p--;
684 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700685 }
686
687 begintemp();
688 addtotemp(EXPR_SIMPLE, val);
689
690 i = scan(scpriv, tokval);
691 return finishtemp();
692}
693
H. Peter Anvin9c749102008-06-14 21:08:38 -0700694static expr *eval_strfunc(enum strfunc type)
695{
696 char *string;
697 size_t string_len;
698 int64_t val;
699 bool parens, rn_warn;
700
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200701 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700702 i = scan(scpriv, tokval);
703 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300704 parens = true;
705 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700706 }
707 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800708 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300709 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700710 }
711 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300712 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700713 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800714 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300715 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700716 }
717
718 val = readstrnum(string, string_len, &rn_warn);
719 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300720 i = scan(scpriv, tokval);
721 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800722 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300723 return NULL;
724 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700725 }
726
727 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800728 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700729
730 begintemp();
731 addtotemp(EXPR_SIMPLE, val);
732
733 i = scan(scpriv, tokval);
734 return finishtemp();
735}
736
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700737static int64_t eval_ifunc(int64_t val, enum ifunc func)
738{
739 int errtype;
740 uint64_t uval = (uint64_t)val;
741 int64_t rv;
742
743 switch (func) {
744 case IFUNC_ILOG2E:
745 case IFUNC_ILOG2W:
746 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400747
748 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800749 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700750 /* fall through */
751 case IFUNC_ILOG2F:
752 rv = ilog2_64(uval);
753 break;
754
755 case IFUNC_ILOG2C:
756 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
757 break;
758
759 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700760 nasm_panic("invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700761 rv = 0;
762 break;
763 }
764
765 return rv;
766}
767
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000769{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000770 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000771 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800772 int32_t label_seg;
773 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700774 int64_t tmpval;
775 bool rn_warn;
H. Peter Anvin98578072018-06-01 18:02:54 -0700776 const char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000777
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700778 if (++deadman > nasm_limit[LIMIT_EVAL]) {
779 nasm_error(ERR_NONFATAL, "expression too long");
780 return NULL;
781 }
782
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700783 switch (i) {
784 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 i = scan(scpriv, tokval);
786 e = expr6(critical);
787 if (!e)
788 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700789 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700790
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700791 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 i = scan(scpriv, tokval);
793 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700794
795 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 i = scan(scpriv, tokval);
797 e = expr6(critical);
798 if (!e)
799 return NULL;
800 if (is_just_unknown(e))
801 return unknown_expr();
802 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800803 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 " scalar values");
805 return NULL;
806 }
807 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700808
809 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000810 i = scan(scpriv, tokval);
811 e = expr6(critical);
812 if (!e)
813 return NULL;
814 if (is_just_unknown(e))
815 return unknown_expr();
816 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800817 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000818 " scalar values");
819 return NULL;
820 }
821 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700822
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700823 case TOKEN_IFUNC:
824 {
825 enum ifunc func = tokval->t_integer;
826 i = scan(scpriv, tokval);
827 e = expr6(critical);
828 if (!e)
829 return NULL;
830 if (is_just_unknown(e))
831 return unknown_expr();
832 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800833 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700834 " scalar values");
835 return NULL;
836 }
837 return scalarvect(eval_ifunc(reloc_value(e), func));
838 }
839
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700840 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 i = scan(scpriv, tokval);
842 e = expr6(critical);
843 if (!e)
844 return NULL;
845 e = segment_part(e);
846 if (!e)
847 return NULL;
848 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800849 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 return NULL;
851 }
852 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700853
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700854 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300855 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700856
H. Peter Anvin9c749102008-06-14 21:08:38 -0700857 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300858 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700859
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700860 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 i = scan(scpriv, tokval);
862 e = bexpr(critical);
863 if (!e)
864 return NULL;
865 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800866 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 return NULL;
868 }
869 i = scan(scpriv, tokval);
870 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700871
872 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700873 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700874 case TOKEN_REG:
875 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300876 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700877 case TOKEN_HERE:
878 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700879 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 begintemp();
881 switch (i) {
882 case TOKEN_NUM:
883 addtotemp(EXPR_SIMPLE, tokval->t_integer);
884 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300885 case TOKEN_STR:
886 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
887 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800888 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700889 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300890 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 case TOKEN_REG:
892 addtotemp(tokval->t_integer, 1L);
893 if (hint && hint->type == EAH_NOHINT)
894 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
895 break;
896 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300897 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 case TOKEN_HERE:
899 case TOKEN_BASE:
900 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800901 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000902 * symbol, Here or Base references are valid because we
903 * are in preprocess-only mode.
904 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800905 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800906 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300908 (i == TOKEN_HERE ? "`$'" :
909 i == TOKEN_BASE ? "`$$'" :
910 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 addtotemp(EXPR_UNKNOWN, 1L);
912 break;
913 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000914
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
916 if (i == TOKEN_BASE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800917 label_seg = in_absolute ? absolute.segment : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 label_ofs = 0;
919 } else if (i == TOKEN_HERE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800920 label_seg = in_absolute ? absolute.segment : location.segment;
921 label_ofs = in_absolute ? absolute.offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800923 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700924 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800926 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700927 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 return NULL;
929 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800930 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700931 "symbol `%s%s' not defined before use",
932 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 return NULL;
934 } else {
935 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400936 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 type = EXPR_UNKNOWN;
938 label_seg = NO_SEG;
939 label_ofs = 1;
940 }
941 }
942 if (opflags && is_extern(tokval->t_charptr))
943 *opflags |= OPFLAG_EXTERN;
944 }
945 addtotemp(type, label_ofs);
946 if (label_seg != NO_SEG)
947 addtotemp(EXPR_SEGBASE + label_seg, 1L);
948 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700949 case TOKEN_DECORATOR:
950 addtotemp(EXPR_RDSAE, tokval->t_integer);
951 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 }
953 i = scan(scpriv, tokval);
954 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700955
956 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800957 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000959 }
960}
961
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800963 int *fwref, int critical, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000964{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000965 expr *e;
966 expr *f = NULL;
967
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700968 deadman = 0;
969
H. Peter Anvin76690a12002-04-30 20:52:49 +0000970 hint = hints;
971 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000973
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000974 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 critical &= ~CRITICAL;
976 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000977 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000979
980 scan = sc;
981 scpriv = scprivate;
982 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000983 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000984
985 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000987 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000989
Keith Kaniosb7a89542007-04-12 02:40:54 +0000990 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000994 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000996
997 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 i = scan(scpriv, tokval); /* eat the WRT */
999 f = expr6(critical);
1000 if (!f)
1001 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001002 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001003 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001004 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 expr *g;
1006 if (is_just_unknown(f))
1007 g = unknown_expr();
1008 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001009 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 begintemp();
1011 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001012 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 return NULL;
1014 }
1015 value = reloc_seg(f);
1016 if (value == NO_SEG)
1017 value = reloc_value(f) | SEG_ABS;
1018 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
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 addtotemp(EXPR_WRT, value);
1023 g = finishtemp();
1024 }
1025 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001026 }
1027 return e;
1028}