blob: a2c8a68473d396407e0f6ab7b1f514ff8d409a0d [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.
H. Peter Anvinca605a32018-11-28 10:07:19 -0800258 *
H. Peter Anvin76690a12002-04-30 20:52:49 +0000259 */
260
261/*
262 * Grammar parsed is:
263 *
264 * expr : bexpr [ WRT expr6 ]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800265 * bexpr : rexp0
H. Peter Anvin76690a12002-04-30 20:52:49 +0000266 * rexp0 : rexp1 [ {||} rexp1...]
267 * rexp1 : rexp2 [ {^^} rexp2...]
268 * rexp2 : rexp3 [ {&&} rexp3...]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800269 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=,<=>} expr0 ]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000270 * expr0 : expr1 [ {|} expr1...]
271 * expr1 : expr2 [ {^} expr2...]
272 * expr2 : expr3 [ {&} expr3...]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800273 * expr3 : expr4 [ {<<,>>,<<<,>>>} expr4...]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000274 * expr4 : expr5 [ {+,-} expr5...]
275 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700276 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000277 * | (bexpr)
278 * | symbol
279 * | $
280 * | number
281 */
282
283static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
284
285static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
286static expr *expr4(int), *expr5(int), *expr6(int);
287
H. Peter Anvinca605a32018-11-28 10:07:19 -0800288/* This inline is a placeholder for the root of the basic expression */
289static inline expr *bexpr(int critical)
290{
291 return rexp0(critical);
292}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000293
H. Peter Anvine2c80182005-01-15 22:15:51 +0000294static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000295{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000296 expr *e, *f;
297
298 e = rexp1(critical);
299 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000300 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000301
H. Peter Anvine2c80182005-01-15 22:15:51 +0000302 while (i == TOKEN_DBL_OR) {
303 i = scan(scpriv, tokval);
304 f = rexp1(critical);
305 if (!f)
306 return NULL;
307 if (!(is_simple(e) || is_just_unknown(e)) ||
308 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800309 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310 " scalar values");
311 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000312
H. Peter Anvine2c80182005-01-15 22:15:51 +0000313 if (is_just_unknown(e) || is_just_unknown(f))
314 e = unknown_expr();
315 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700316 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000317 }
318 return e;
319}
320
H. Peter Anvine2c80182005-01-15 22:15:51 +0000321static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000322{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000323 expr *e, *f;
324
325 e = rexp2(critical);
326 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000327 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000328
H. Peter Anvine2c80182005-01-15 22:15:51 +0000329 while (i == TOKEN_DBL_XOR) {
330 i = scan(scpriv, tokval);
331 f = rexp2(critical);
332 if (!f)
333 return NULL;
334 if (!(is_simple(e) || is_just_unknown(e)) ||
335 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800336 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000337 " scalar values");
338 }
339
340 if (is_just_unknown(e) || is_just_unknown(f))
341 e = unknown_expr();
342 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700343 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000344 }
345 return e;
346}
347
H. Peter Anvine2c80182005-01-15 22:15:51 +0000348static expr *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000349{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000350 expr *e, *f;
351
352 e = rexp3(critical);
353 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000354 return NULL;
355 while (i == TOKEN_DBL_AND) {
356 i = scan(scpriv, tokval);
357 f = rexp3(critical);
358 if (!f)
359 return NULL;
360 if (!(is_simple(e) || is_just_unknown(e)) ||
361 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800362 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000363 " scalar values");
364 }
365 if (is_just_unknown(e) || is_just_unknown(f))
366 e = unknown_expr();
367 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700368 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369 }
370 return e;
371}
372
H. Peter Anvine2c80182005-01-15 22:15:51 +0000373static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000374{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000375 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700376 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000377
378 e = expr0(critical);
379 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000381
H. Peter Anvin76690a12002-04-30 20:52:49 +0000382 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700383 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE ||
384 i == TOKEN_LEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 int j = i;
386 i = scan(scpriv, tokval);
387 f = expr0(critical);
388 if (!f)
389 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000390
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700391 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000392
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 switch (j) {
394 case TOKEN_EQ:
395 case TOKEN_NE:
396 if (is_unknown(e))
397 v = -1; /* means unknown */
398 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700399 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700401 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 break;
403 default:
404 if (is_unknown(e))
405 v = -1; /* means unknown */
406 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800407 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000408 "`%s': operands differ by a non-scalar",
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700409 (j == TOKEN_LE ? "<=" :
410 j == TOKEN_LT ? "<" :
411 j == TOKEN_GE ? ">=" :
412 j == TOKEN_GT ? ">" :
413 j == TOKEN_LEG ? "<=>" :
414 "<internal error>"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415 v = 0; /* must set it to _something_ */
416 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300417 int64_t vv = reloc_value(e);
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700418 if (j == TOKEN_LEG)
419 v = (vv < 0) ? -1 : (vv > 0) ? 1 : 0;
420 else if (vv == 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000421 v = (j == TOKEN_LE || j == TOKEN_GE);
422 else if (vv > 0)
423 v = (j == TOKEN_GE || j == TOKEN_GT);
424 else /* vv < 0 */
425 v = (j == TOKEN_LE || j == TOKEN_LT);
426 }
427 break;
428 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000429
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 if (v == -1)
431 e = unknown_expr();
432 else
433 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000434 }
435 return e;
436}
437
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000439{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000440 expr *e, *f;
441
442 e = expr1(critical);
443 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000445
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 while (i == '|') {
447 i = scan(scpriv, tokval);
448 f = expr1(critical);
449 if (!f)
450 return NULL;
451 if (!(is_simple(e) || is_just_unknown(e)) ||
452 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800453 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454 " scalar values");
455 }
456 if (is_just_unknown(e) || is_just_unknown(f))
457 e = unknown_expr();
458 else
459 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000460 }
461 return e;
462}
463
H. Peter Anvine2c80182005-01-15 22:15:51 +0000464static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000465{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000466 expr *e, *f;
467
468 e = expr2(critical);
469 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000471
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000473 i = scan(scpriv, tokval);
474 f = expr2(critical);
475 if (!f)
476 return NULL;
477 if (!(is_simple(e) || is_just_unknown(e)) ||
478 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800479 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480 " scalar values");
481 }
482 if (is_just_unknown(e) || is_just_unknown(f))
483 e = unknown_expr();
484 else
485 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000486 }
487 return e;
488}
489
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000491{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000492 expr *e, *f;
493
494 e = expr3(critical);
495 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000497
H. Peter Anvin76690a12002-04-30 20:52:49 +0000498 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 i = scan(scpriv, tokval);
500 f = expr3(critical);
501 if (!f)
502 return NULL;
503 if (!(is_simple(e) || is_just_unknown(e)) ||
504 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800505 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 " scalar values");
507 }
508 if (is_just_unknown(e) || is_just_unknown(f))
509 e = unknown_expr();
510 else
511 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000512 }
513 return e;
514}
515
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000518 expr *e, *f;
519
520 e = expr4(critical);
521 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700524 while (i == TOKEN_SHL || i == TOKEN_SHR || i == TOKEN_SAR) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 int j = i;
526 i = scan(scpriv, tokval);
527 f = expr4(critical);
528 if (!f)
529 return NULL;
530 if (!(is_simple(e) || is_just_unknown(e)) ||
531 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800532 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 " scalar values");
534 } else if (is_just_unknown(e) || is_just_unknown(f)) {
535 e = unknown_expr();
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700536 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 switch (j) {
538 case TOKEN_SHL:
539 e = scalarvect(reloc_value(e) << reloc_value(f));
540 break;
541 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700542 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 reloc_value(f));
544 break;
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700545 case TOKEN_SAR:
546 e = scalarvect(((int64_t)reloc_value(e)) >>
547 reloc_value(f));
548 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 }
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700550 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000551 }
552 return e;
553}
554
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000556{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000557 expr *e, *f;
558
559 e = expr5(critical);
560 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 return NULL;
562 while (i == '+' || i == '-') {
563 int j = i;
564 i = scan(scpriv, tokval);
565 f = expr5(critical);
566 if (!f)
567 return NULL;
568 switch (j) {
569 case '+':
570 e = add_vectors(e, f);
571 break;
572 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700573 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 break;
575 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576 }
577 return e;
578}
579
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000581{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000582 expr *e, *f;
583
584 e = expr6(critical);
585 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000587 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 i == TOKEN_SDIV || i == TOKEN_SMOD) {
589 int j = i;
590 i = scan(scpriv, tokval);
591 f = expr6(critical);
592 if (!f)
593 return NULL;
594 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
595 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800596 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 " scalar values");
598 return NULL;
599 }
Cyrill Gorcunovceec0d82018-10-14 01:26:19 +0300600 if (j != '*' && !is_just_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800601 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 return NULL;
603 }
604 switch (j) {
605 case '*':
606 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700607 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700609 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 else if (is_just_unknown(e) && is_just_unknown(f))
611 e = unknown_expr();
612 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800613 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 "non-scalar objects");
615 return NULL;
616 }
617 break;
618 case '/':
619 if (is_just_unknown(e) || is_just_unknown(f))
620 e = unknown_expr();
621 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700622 e = scalarvect(((uint64_t)reloc_value(e)) /
623 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 break;
625 case '%':
626 if (is_just_unknown(e) || is_just_unknown(f))
627 e = unknown_expr();
628 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700629 e = scalarvect(((uint64_t)reloc_value(e)) %
630 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 break;
632 case TOKEN_SDIV:
633 if (is_just_unknown(e) || is_just_unknown(f))
634 e = unknown_expr();
635 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700636 e = scalarvect(((int64_t)reloc_value(e)) /
637 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 break;
639 case TOKEN_SMOD:
640 if (is_just_unknown(e) || is_just_unknown(f))
641 e = unknown_expr();
642 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700643 e = scalarvect(((int64_t)reloc_value(e)) %
644 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 break;
646 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000647 }
648 return e;
649}
650
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700651static expr *eval_floatize(enum floatize type)
652{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300653 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700654 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300655 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700656 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300657 { 1, 0, 1 }, /* FLOAT_8 */
658 { 2, 0, 2 }, /* FLOAT_16 */
659 { 4, 0, 4 }, /* FLOAT_32 */
660 { 8, 0, 8 }, /* FLOAT_64 */
661 { 10, 0, 8 }, /* FLOAT_80M */
662 { 10, 8, 2 }, /* FLOAT_80E */
663 { 16, 0, 8 }, /* FLOAT_128L */
664 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700665 };
666 int sign = 1;
667 int64_t val;
668 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700669
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700670 i = scan(scpriv, tokval);
671 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800672 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300673 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700674 }
675 i = scan(scpriv, tokval);
676 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300677 sign = (i == '-') ? -1 : 1;
678 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700679 }
680 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800681 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300682 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700683 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800684 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300685 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700686 i = scan(scpriv, tokval);
687 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800688 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300689 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700690 }
691
692 p = result+formats[type].start+formats[type].len;
693 val = 0;
694 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300695 p--;
696 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700697 }
698
699 begintemp();
700 addtotemp(EXPR_SIMPLE, val);
701
702 i = scan(scpriv, tokval);
703 return finishtemp();
704}
705
H. Peter Anvin9c749102008-06-14 21:08:38 -0700706static expr *eval_strfunc(enum strfunc type)
707{
708 char *string;
709 size_t string_len;
710 int64_t val;
711 bool parens, rn_warn;
712
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200713 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700714 i = scan(scpriv, tokval);
715 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300716 parens = true;
717 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700718 }
719 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800720 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300721 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700722 }
723 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300724 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700725 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800726 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300727 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700728 }
729
730 val = readstrnum(string, string_len, &rn_warn);
731 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300732 i = scan(scpriv, tokval);
733 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800734 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300735 return NULL;
736 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700737 }
738
739 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800740 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700741
742 begintemp();
743 addtotemp(EXPR_SIMPLE, val);
744
745 i = scan(scpriv, tokval);
746 return finishtemp();
747}
748
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700749static int64_t eval_ifunc(int64_t val, enum ifunc func)
750{
751 int errtype;
752 uint64_t uval = (uint64_t)val;
753 int64_t rv;
754
755 switch (func) {
756 case IFUNC_ILOG2E:
757 case IFUNC_ILOG2W:
758 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400759
760 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800761 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700762 /* fall through */
763 case IFUNC_ILOG2F:
764 rv = ilog2_64(uval);
765 break;
766
767 case IFUNC_ILOG2C:
768 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
769 break;
770
771 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700772 nasm_panic("invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700773 rv = 0;
774 break;
775 }
776
777 return rv;
778}
779
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000781{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000782 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000783 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800784 int32_t label_seg;
785 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700786 int64_t tmpval;
787 bool rn_warn;
H. Peter Anvin98578072018-06-01 18:02:54 -0700788 const char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000789
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700790 if (++deadman > nasm_limit[LIMIT_EVAL]) {
791 nasm_error(ERR_NONFATAL, "expression too long");
792 return NULL;
793 }
794
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700795 switch (i) {
796 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 i = scan(scpriv, tokval);
798 e = expr6(critical);
799 if (!e)
800 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700801 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700802
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700803 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 i = scan(scpriv, tokval);
805 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700806
807 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 i = scan(scpriv, tokval);
809 e = expr6(critical);
810 if (!e)
811 return NULL;
812 if (is_just_unknown(e))
813 return unknown_expr();
814 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800815 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 " scalar values");
817 return NULL;
818 }
819 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700820
821 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000822 i = scan(scpriv, tokval);
823 e = expr6(critical);
824 if (!e)
825 return NULL;
826 if (is_just_unknown(e))
827 return unknown_expr();
828 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800829 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000830 " scalar values");
831 return NULL;
832 }
833 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700834
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700835 case TOKEN_IFUNC:
836 {
837 enum ifunc func = tokval->t_integer;
838 i = scan(scpriv, tokval);
839 e = expr6(critical);
840 if (!e)
841 return NULL;
842 if (is_just_unknown(e))
843 return unknown_expr();
844 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800845 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700846 " scalar values");
847 return NULL;
848 }
849 return scalarvect(eval_ifunc(reloc_value(e), func));
850 }
851
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700852 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 i = scan(scpriv, tokval);
854 e = expr6(critical);
855 if (!e)
856 return NULL;
857 e = segment_part(e);
858 if (!e)
859 return NULL;
860 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800861 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 return NULL;
863 }
864 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700865
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700866 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300867 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700868
H. Peter Anvin9c749102008-06-14 21:08:38 -0700869 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300870 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700871
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700872 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000873 i = scan(scpriv, tokval);
874 e = bexpr(critical);
875 if (!e)
876 return NULL;
877 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800878 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 return NULL;
880 }
881 i = scan(scpriv, tokval);
882 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700883
884 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700885 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700886 case TOKEN_REG:
887 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300888 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700889 case TOKEN_HERE:
890 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700891 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 begintemp();
893 switch (i) {
894 case TOKEN_NUM:
895 addtotemp(EXPR_SIMPLE, tokval->t_integer);
896 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300897 case TOKEN_STR:
898 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
899 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800900 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700901 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300902 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 case TOKEN_REG:
904 addtotemp(tokval->t_integer, 1L);
905 if (hint && hint->type == EAH_NOHINT)
906 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
907 break;
908 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300909 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 case TOKEN_HERE:
911 case TOKEN_BASE:
912 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800913 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 * symbol, Here or Base references are valid because we
915 * are in preprocess-only mode.
916 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800917 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800918 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300920 (i == TOKEN_HERE ? "`$'" :
921 i == TOKEN_BASE ? "`$$'" :
922 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 addtotemp(EXPR_UNKNOWN, 1L);
924 break;
925 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000926
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
928 if (i == TOKEN_BASE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800929 label_seg = in_absolute ? absolute.segment : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 label_ofs = 0;
931 } else if (i == TOKEN_HERE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800932 label_seg = in_absolute ? absolute.segment : location.segment;
933 label_ofs = in_absolute ? absolute.offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800935 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700936 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800938 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700939 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 return NULL;
941 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800942 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700943 "symbol `%s%s' not defined before use",
944 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 return NULL;
946 } else {
947 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400948 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 type = EXPR_UNKNOWN;
950 label_seg = NO_SEG;
951 label_ofs = 1;
952 }
953 }
954 if (opflags && is_extern(tokval->t_charptr))
955 *opflags |= OPFLAG_EXTERN;
956 }
957 addtotemp(type, label_ofs);
958 if (label_seg != NO_SEG)
959 addtotemp(EXPR_SEGBASE + label_seg, 1L);
960 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700961 case TOKEN_DECORATOR:
962 addtotemp(EXPR_RDSAE, tokval->t_integer);
963 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 }
965 i = scan(scpriv, tokval);
966 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700967
968 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800969 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000971 }
972}
973
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800975 int *fwref, int critical, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000976{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000977 expr *e;
978 expr *f = NULL;
979
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700980 deadman = 0;
981
H. Peter Anvin76690a12002-04-30 20:52:49 +0000982 hint = hints;
983 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000985
H. Peter Anvinca605a32018-11-28 10:07:19 -0800986 critical &= ~CRITICAL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000987
988 scan = sc;
989 scpriv = scprivate;
990 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000991 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000992
993 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000995 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000997
Keith Kaniosb7a89542007-04-12 02:40:54 +0000998 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001000
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001002 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001004
1005 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 i = scan(scpriv, tokval); /* eat the WRT */
1007 f = expr6(critical);
1008 if (!f)
1009 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001010 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001011 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001012 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 expr *g;
1014 if (is_just_unknown(f))
1015 g = unknown_expr();
1016 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001017 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 begintemp();
1019 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001020 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 return NULL;
1022 }
1023 value = reloc_seg(f);
1024 if (value == NO_SEG)
1025 value = reloc_value(f) | SEG_ABS;
1026 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001027 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 return NULL;
1029 }
1030 addtotemp(EXPR_WRT, value);
1031 g = finishtemp();
1032 }
1033 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001034 }
1035 return e;
1036}