blob: c5e5c43b7f8c0b2e7786f560681400e7361c1cf7 [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 Anvin99fcda02018-11-28 10:27:30 -080058static scanner scanfunc; /* Address of scanner routine */
59static void *scpriv; /* Scanner private pointer */
H. Peter Anvineba20a72002-04-30 20:53:55 +000060
H. Peter Anvineba20a72002-04-30 20:53:55 +000061static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000062static int ntempexprs;
63static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000064
H. Peter Anvine2c80182005-01-15 22:15:51 +000065static expr *tempexpr;
66static int ntempexpr;
67static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000068
H. Peter Anvine2c80182005-01-15 22:15:51 +000069static struct tokenval *tokval; /* The current token */
H. Peter Anvin6fdf7102018-11-28 10:33:16 -080070static int tt; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000071
H. Peter Anvinef427b32018-11-28 10:19:50 -080072static int critical;
H. Peter Anvineba20a72002-04-30 20:53:55 +000073static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000074
75static struct eval_hints *hint;
H. Peter Anvina3d96d02018-06-15 17:51:39 -070076static int64_t deadman;
H. Peter Anvin76690a12002-04-30 20:52:49 +000077
H. Peter Anvin667dd802002-05-26 19:49:41 +000078
H. Peter Anvin76690a12002-04-30 20:52:49 +000079/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000080 * Unimportant cleanup is done to avoid confusing people who are trying
81 * to debug real memory leaks
82 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000083void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000084{
85 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000086 nasm_free(tempexprs[--ntempexprs]);
87 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000088}
89
90/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000091 * Construct a temporary expression.
92 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000093static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000094{
H. Peter Anvin76690a12002-04-30 20:52:49 +000095 tempexpr = NULL;
96 tempexpr_size = ntempexpr = 0;
97}
98
Keith Kaniosb7a89542007-04-12 02:40:54 +000099static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000100{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000101 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000102 tempexpr_size += TEMPEXPR_DELTA;
103 tempexpr = nasm_realloc(tempexpr,
104 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000105 }
106 tempexpr[ntempexpr].type = type;
107 tempexpr[ntempexpr++].value = value;
108}
109
H. Peter Anvine2c80182005-01-15 22:15:51 +0000110static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000111{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000113 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000114 tempexprs_size += TEMPEXPRS_DELTA;
115 tempexprs = nasm_realloc(tempexprs,
116 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000117 }
118 return tempexprs[ntempexprs++] = tempexpr;
119}
120
121/*
122 * Add two vector datatypes. We have some bizarre behaviour on far-
123 * absolute segment types: we preserve them during addition _only_
124 * if one of the segments is a truly pure scalar.
125 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000126static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000127{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000128 int preserve;
129
130 preserve = is_really_simple(p) || is_really_simple(q);
131
132 begintemp();
133
134 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000135 p->type < EXPR_SEGBASE + SEG_ABS &&
136 q->type < EXPR_SEGBASE + SEG_ABS) {
137 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000138
H. Peter Anvine2c80182005-01-15 22:15:51 +0000139 if (p->type > q->type) {
140 addtotemp(q->type, q->value);
141 lasttype = q++->type;
142 } else if (p->type < q->type) {
143 addtotemp(p->type, p->value);
144 lasttype = p++->type;
145 } else { /* *p and *q have same type */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700146 int64_t sum = p->value + q->value;
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800147 if (sum) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000148 addtotemp(p->type, sum);
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800149 if (hint)
150 hint->type = EAH_SUMMED;
151 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000152 lasttype = p->type;
153 p++, q++;
154 }
155 if (lasttype == EXPR_UNKNOWN) {
156 return finishtemp();
157 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000158 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000159 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
160 addtotemp(p->type, p->value);
161 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000162 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000163 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
164 addtotemp(q->type, q->value);
165 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000166 }
167
168 return finishtemp();
169}
170
171/*
172 * Multiply a vector by a scalar. Strip far-absolute segment part
173 * if present.
174 *
175 * Explicit treatment of UNKNOWN is not required in this routine,
176 * since it will silently do the Right Thing anyway.
177 *
178 * If `affect_hints' is set, we also change the hint type to
179 * NOTBASE if a MAKEBASE hint points at a register being
180 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
181 * as the base register.
182 */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700183static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000184{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000185 expr *p = vect;
186
H. Peter Anvine2c80182005-01-15 22:15:51 +0000187 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
188 p->value = scalar * (p->value);
189 if (hint && hint->type == EAH_MAKEBASE &&
190 p->type == hint->base && affect_hints)
191 hint->type = EAH_NOTBASE;
192 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000193 }
194 p->type = 0;
195
196 return vect;
197}
198
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700199static expr *scalarvect(int64_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000200{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000201 begintemp();
202 addtotemp(EXPR_SIMPLE, scalar);
203 return finishtemp();
204}
205
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000207{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000208 begintemp();
209 addtotemp(EXPR_UNKNOWN, 1L);
210 return finishtemp();
211}
212
213/*
214 * The SEG operator: calculate the segment part of a relocatable
215 * value. Return NULL, as usual, if an error occurs. Report the
216 * error too.
217 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000218static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000219{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000220 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000221
222 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000223 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000224
225 if (!is_reloc(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800226 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000227 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000228 }
229
230 seg = reloc_seg(e);
231 if (seg == NO_SEG) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800232 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000233 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000234 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000235 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000236 } else if (seg & 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800237 nasm_error(ERR_NONFATAL, "SEG applied to something which"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000238 " is already a segment base");
239 return NULL;
240 } else {
H. Peter Anvin36034ec2016-02-18 01:18:50 -0800241 int32_t base = ofmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000242
H. Peter Anvine2c80182005-01-15 22:15:51 +0000243 begintemp();
244 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
245 1L);
246 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000247 }
248}
249
250/*
251 * Recursive-descent parser. Called with a single boolean operand,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700252 * which is true if the evaluation is critical (i.e. unresolved
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800253 * symbols are an error condition). Must update the global `tt' to
H. Peter Anvin76690a12002-04-30 20:52:49 +0000254 * reflect the token after the parsed string. May return NULL.
255 *
256 * evaluate() should report its own errors: on return it is assumed
257 * that if NULL has been returned, the error has already been
258 * reported.
H. Peter Anvinca605a32018-11-28 10:07:19 -0800259 *
H. Peter Anvin76690a12002-04-30 20:52:49 +0000260 */
261
262/*
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800263 * Wrapper function around the scanner
264 */
265static int scan(void)
266{
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800267 return tt = scanfunc(scpriv, tokval);
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800268}
269
270/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000271 * Grammar parsed is:
272 *
273 * expr : bexpr [ WRT expr6 ]
H. Peter Anvin099cc172018-11-28 13:13:16 -0800274 * bexpr : cexpr
275 * cexpr : rexp0 [ {?} bexpr {:} cexpr ]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000276 * rexp0 : rexp1 [ {||} rexp1...]
277 * rexp1 : rexp2 [ {^^} rexp2...]
278 * rexp2 : rexp3 [ {&&} rexp3...]
H. Peter Anvin099cc172018-11-28 13:13:16 -0800279 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=,<=>} expr0... ]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000280 * expr0 : expr1 [ {|} expr1...]
281 * expr1 : expr2 [ {^} expr2...]
282 * expr2 : expr3 [ {&} expr3...]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800283 * expr3 : expr4 [ {<<,>>,<<<,>>>} expr4...]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000284 * expr4 : expr5 [ {+,-} expr5...]
285 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700286 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000287 * | (bexpr)
288 * | symbol
289 * | $
290 * | number
291 */
292
H. Peter Anvin099cc172018-11-28 13:13:16 -0800293static expr *cexpr(void);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800294static expr *rexp0(void), *rexp1(void), *rexp2(void), *rexp3(void);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000295
H. Peter Anvinef427b32018-11-28 10:19:50 -0800296static expr *expr0(void), *expr1(void), *expr2(void), *expr3(void);
297static expr *expr4(void), *expr5(void), *expr6(void);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000298
H. Peter Anvinca605a32018-11-28 10:07:19 -0800299/* This inline is a placeholder for the root of the basic expression */
H. Peter Anvinef427b32018-11-28 10:19:50 -0800300static inline expr *bexpr(void)
H. Peter Anvinca605a32018-11-28 10:07:19 -0800301{
H. Peter Anvin099cc172018-11-28 13:13:16 -0800302 return cexpr();
303}
304
305static expr *cexpr(void)
306{
307 expr *e, *f, *g;
308
309 e = rexp0();
310 if (!e)
311 return NULL;
312
313 if (tt == '?') {
314 scan();
315 f = bexpr();
316 if (!f)
317 return NULL;
318
319 if (tt != ':') {
320 nasm_error(ERR_NONFATAL, "`?' without matching `:'");
321 return NULL;
322 }
323
324 scan();
325 g = cexpr();
326 if (!g)
327 return NULL;
328
329 if (is_simple(e)) {
330 e = reloc_value(e) ? f : g;
331 } else if (is_just_unknown(e)) {
332 e = unknown_expr();
333 } else {
334 nasm_error(ERR_NONFATAL, "the left-hand side of `?' must be "
335 "a scalar value");
336 }
337 }
338
339 return e;
H. Peter Anvinca605a32018-11-28 10:07:19 -0800340}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000341
H. Peter Anvinef427b32018-11-28 10:19:50 -0800342static expr *rexp0(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000344 expr *e, *f;
345
H. Peter Anvinef427b32018-11-28 10:19:50 -0800346 e = rexp1();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000347 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000348 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000349
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800350 while (tt == TOKEN_DBL_OR) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800351 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800352 f = rexp1();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 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 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000360
H. Peter Anvine2c80182005-01-15 22:15:51 +0000361 if (is_just_unknown(e) || is_just_unknown(f))
362 e = unknown_expr();
363 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700364 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000365 }
366 return e;
367}
368
H. Peter Anvinef427b32018-11-28 10:19:50 -0800369static expr *rexp1(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000370{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000371 expr *e, *f;
372
H. Peter Anvinef427b32018-11-28 10:19:50 -0800373 e = rexp2();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000374 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000375 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000376
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800377 while (tt == TOKEN_DBL_XOR) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800378 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800379 f = rexp2();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 if (!f)
381 return NULL;
382 if (!(is_simple(e) || is_just_unknown(e)) ||
383 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800384 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 " scalar values");
386 }
387
388 if (is_just_unknown(e) || is_just_unknown(f))
389 e = unknown_expr();
390 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700391 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000392 }
393 return e;
394}
395
H. Peter Anvinef427b32018-11-28 10:19:50 -0800396static expr *rexp2(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000397{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000398 expr *e, *f;
399
H. Peter Anvinef427b32018-11-28 10:19:50 -0800400 e = rexp3();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000401 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 return NULL;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800403 while (tt == TOKEN_DBL_AND) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800404 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800405 f = rexp3();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 if (!f)
407 return NULL;
408 if (!(is_simple(e) || is_just_unknown(e)) ||
409 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800410 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 " scalar values");
412 }
413 if (is_just_unknown(e) || is_just_unknown(f))
414 e = unknown_expr();
415 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700416 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000417 }
418 return e;
419}
420
H. Peter Anvinef427b32018-11-28 10:19:50 -0800421static expr *rexp3(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000422{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000423 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700424 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000425
H. Peter Anvinef427b32018-11-28 10:19:50 -0800426 e = expr0();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000427 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000429
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800430 while (tt == TOKEN_EQ || tt == TOKEN_LT || tt == TOKEN_GT ||
431 tt == TOKEN_NE || tt == TOKEN_LE || tt == TOKEN_GE ||
432 tt == TOKEN_LEG) {
433 int tto = tt;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800434 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800435 f = expr0();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 if (!f)
437 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000438
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700439 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000440
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800441 switch (tto) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000442 case TOKEN_EQ:
443 case TOKEN_NE:
444 if (is_unknown(e))
445 v = -1; /* means unknown */
446 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800447 v = (tto == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000448 else
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800449 v = (tto == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 break;
451 default:
452 if (is_unknown(e))
453 v = -1; /* means unknown */
454 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800455 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 "`%s': operands differ by a non-scalar",
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800457 (tto == TOKEN_LE ? "<=" :
458 tto == TOKEN_LT ? "<" :
459 tto == TOKEN_GE ? ">=" :
460 tto == TOKEN_GT ? ">" :
461 tto == TOKEN_LEG ? "<=>" :
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700462 "<internal error>"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 v = 0; /* must set it to _something_ */
464 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300465 int64_t vv = reloc_value(e);
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800466 if (tto == TOKEN_LEG)
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700467 v = (vv < 0) ? -1 : (vv > 0) ? 1 : 0;
468 else if (vv == 0)
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800469 v = (tto == TOKEN_LE || tto == TOKEN_GE);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 else if (vv > 0)
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800471 v = (tto == TOKEN_GE || tto == TOKEN_GT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 else /* vv < 0 */
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800473 v = (tto == TOKEN_LE || tto == TOKEN_LT);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 }
475 break;
476 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477
H. Peter Anvine2c80182005-01-15 22:15:51 +0000478 if (v == -1)
479 e = unknown_expr();
480 else
481 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000482 }
483 return e;
484}
485
H. Peter Anvinef427b32018-11-28 10:19:50 -0800486static expr *expr0(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000487{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000488 expr *e, *f;
489
H. Peter Anvinef427b32018-11-28 10:19:50 -0800490 e = expr1();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000491 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800494 while (tt == '|') {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800495 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800496 f = expr1();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 if (!f)
498 return NULL;
499 if (!(is_simple(e) || is_just_unknown(e)) ||
500 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800501 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 " scalar values");
503 }
504 if (is_just_unknown(e) || is_just_unknown(f))
505 e = unknown_expr();
506 else
507 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000508 }
509 return e;
510}
511
H. Peter Anvinef427b32018-11-28 10:19:50 -0800512static expr *expr1(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000513{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000514 expr *e, *f;
515
H. Peter Anvinef427b32018-11-28 10:19:50 -0800516 e = expr2();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000517 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800520 while (tt == '^') {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800521 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800522 f = expr2();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 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, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 " scalar values");
529 }
530 if (is_just_unknown(e) || is_just_unknown(f))
531 e = unknown_expr();
532 else
533 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000534 }
535 return e;
536}
537
H. Peter Anvinef427b32018-11-28 10:19:50 -0800538static expr *expr2(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000539{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000540 expr *e, *f;
541
H. Peter Anvinef427b32018-11-28 10:19:50 -0800542 e = expr3();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000543 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000545
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800546 while (tt == '&') {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800547 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800548 f = expr3();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 if (!f)
550 return NULL;
551 if (!(is_simple(e) || is_just_unknown(e)) ||
552 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800553 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554 " scalar values");
555 }
556 if (is_just_unknown(e) || is_just_unknown(f))
557 e = unknown_expr();
558 else
559 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000560 }
561 return e;
562}
563
H. Peter Anvinef427b32018-11-28 10:19:50 -0800564static expr *expr3(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000565{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000566 expr *e, *f;
567
H. Peter Anvinef427b32018-11-28 10:19:50 -0800568 e = expr4();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000569 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000571
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800572 while (tt == TOKEN_SHL || tt == TOKEN_SHR || tt == TOKEN_SAR) {
573 int tto = tt;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800574 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800575 f = expr4();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 if (!f)
577 return NULL;
578 if (!(is_simple(e) || is_just_unknown(e)) ||
579 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800580 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 " scalar values");
582 } else if (is_just_unknown(e) || is_just_unknown(f)) {
583 e = unknown_expr();
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700584 } else {
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800585 switch (tto) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 case TOKEN_SHL:
587 e = scalarvect(reloc_value(e) << reloc_value(f));
588 break;
589 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700590 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 reloc_value(f));
592 break;
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700593 case TOKEN_SAR:
594 e = scalarvect(((int64_t)reloc_value(e)) >>
595 reloc_value(f));
596 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 }
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700598 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000599 }
600 return e;
601}
602
H. Peter Anvinef427b32018-11-28 10:19:50 -0800603static expr *expr4(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000605 expr *e, *f;
606
H. Peter Anvinef427b32018-11-28 10:19:50 -0800607 e = expr5();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000608 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 return NULL;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800610 while (tt == '+' || tt == '-') {
611 int tto = tt;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800612 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800613 f = expr5();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 if (!f)
615 return NULL;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800616 switch (tto) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 case '+':
618 e = add_vectors(e, f);
619 break;
620 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700621 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000622 break;
623 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000624 }
625 return e;
626}
627
H. Peter Anvinef427b32018-11-28 10:19:50 -0800628static expr *expr5(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000629{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000630 expr *e, *f;
631
H. Peter Anvinef427b32018-11-28 10:19:50 -0800632 e = expr6();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000633 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 return NULL;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800635 while (tt == '*' || tt == '/' || tt == '%' ||
636 tt == TOKEN_SDIV || tt == TOKEN_SMOD) {
637 int tto = tt;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800638 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800639 f = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 if (!f)
641 return NULL;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800642 if (tto != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800644 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 " scalar values");
646 return NULL;
647 }
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800648 if (tto != '*' && !is_just_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800649 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 return NULL;
651 }
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800652 switch (tto) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 case '*':
654 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700655 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700657 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 else if (is_just_unknown(e) && is_just_unknown(f))
659 e = unknown_expr();
660 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800661 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000662 "non-scalar objects");
663 return NULL;
664 }
665 break;
666 case '/':
667 if (is_just_unknown(e) || is_just_unknown(f))
668 e = unknown_expr();
669 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700670 e = scalarvect(((uint64_t)reloc_value(e)) /
671 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 break;
673 case '%':
674 if (is_just_unknown(e) || is_just_unknown(f))
675 e = unknown_expr();
676 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700677 e = scalarvect(((uint64_t)reloc_value(e)) %
678 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 break;
680 case TOKEN_SDIV:
681 if (is_just_unknown(e) || is_just_unknown(f))
682 e = unknown_expr();
683 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700684 e = scalarvect(((int64_t)reloc_value(e)) /
685 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000686 break;
687 case TOKEN_SMOD:
688 if (is_just_unknown(e) || is_just_unknown(f))
689 e = unknown_expr();
690 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700691 e = scalarvect(((int64_t)reloc_value(e)) %
692 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 break;
694 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000695 }
696 return e;
697}
698
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700699static expr *eval_floatize(enum floatize type)
700{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300701 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700702 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300703 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700704 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300705 { 1, 0, 1 }, /* FLOAT_8 */
706 { 2, 0, 2 }, /* FLOAT_16 */
707 { 4, 0, 4 }, /* FLOAT_32 */
708 { 8, 0, 8 }, /* FLOAT_64 */
709 { 10, 0, 8 }, /* FLOAT_80M */
710 { 10, 8, 2 }, /* FLOAT_80E */
711 { 16, 0, 8 }, /* FLOAT_128L */
712 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700713 };
714 int sign = 1;
715 int64_t val;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800716 int i;
H. Peter Anvin70653092007-10-19 14:42:29 -0700717
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800718 scan();
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800719 if (tt != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800720 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300721 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700722 }
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800723 scan();
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800724 if (tt == '-' || tt == '+') {
725 sign = (tt == '-') ? -1 : 1;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800726 scan();
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700727 }
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800728 if (tt != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800729 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300730 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700731 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800732 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300733 return NULL;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800734 scan();
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800735 if (tt != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800736 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300737 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700738 }
739
740 p = result+formats[type].start+formats[type].len;
741 val = 0;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800742 for (i = formats[type].len; i; i--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300743 p--;
744 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700745 }
746
747 begintemp();
748 addtotemp(EXPR_SIMPLE, val);
749
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800750 scan();
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700751 return finishtemp();
752}
753
H. Peter Anvin9c749102008-06-14 21:08:38 -0700754static expr *eval_strfunc(enum strfunc type)
755{
756 char *string;
757 size_t string_len;
758 int64_t val;
759 bool parens, rn_warn;
760
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200761 parens = false;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800762 scan();
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800763 if (tt == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300764 parens = true;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800765 scan();
H. Peter Anvin9c749102008-06-14 21:08:38 -0700766 }
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800767 if (tt != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800768 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300769 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700770 }
771 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300772 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700773 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800774 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300775 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700776 }
777
778 val = readstrnum(string, string_len, &rn_warn);
779 if (parens) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800780 scan();
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800781 if (tt != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800782 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300783 return NULL;
784 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700785 }
786
787 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800788 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700789
790 begintemp();
791 addtotemp(EXPR_SIMPLE, val);
792
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800793 scan();
H. Peter Anvin9c749102008-06-14 21:08:38 -0700794 return finishtemp();
795}
796
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700797static int64_t eval_ifunc(int64_t val, enum ifunc func)
798{
799 int errtype;
800 uint64_t uval = (uint64_t)val;
801 int64_t rv;
802
803 switch (func) {
804 case IFUNC_ILOG2E:
805 case IFUNC_ILOG2W:
806 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400807
808 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800809 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700810 /* fall through */
811 case IFUNC_ILOG2F:
812 rv = ilog2_64(uval);
813 break;
814
815 case IFUNC_ILOG2C:
816 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
817 break;
818
819 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700820 nasm_panic("invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700821 rv = 0;
822 break;
823 }
824
825 return rv;
826}
827
H. Peter Anvinef427b32018-11-28 10:19:50 -0800828static expr *expr6(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000829{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000830 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000831 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800832 int32_t label_seg;
833 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700834 int64_t tmpval;
835 bool rn_warn;
H. Peter Anvin98578072018-06-01 18:02:54 -0700836 const char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000837
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700838 if (++deadman > nasm_limit[LIMIT_EVAL]) {
839 nasm_error(ERR_NONFATAL, "expression too long");
840 return NULL;
841 }
842
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800843 switch (tt) {
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700844 case '-':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800845 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800846 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 if (!e)
848 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700849 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700850
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700851 case '+':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800852 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800853 return expr6();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700854
855 case '~':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800856 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800857 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 if (!e)
859 return NULL;
860 if (is_just_unknown(e))
861 return unknown_expr();
862 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800863 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 " scalar values");
865 return NULL;
866 }
867 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700868
869 case '!':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800870 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800871 e = expr6();
Chuck Craynecb9bc212007-05-02 04:21:26 +0000872 if (!e)
873 return NULL;
874 if (is_just_unknown(e))
875 return unknown_expr();
876 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800877 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000878 " scalar values");
879 return NULL;
880 }
881 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700882
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700883 case TOKEN_IFUNC:
884 {
885 enum ifunc func = tokval->t_integer;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800886 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800887 e = expr6();
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700888 if (!e)
889 return NULL;
890 if (is_just_unknown(e))
891 return unknown_expr();
892 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800893 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700894 " scalar values");
895 return NULL;
896 }
897 return scalarvect(eval_ifunc(reloc_value(e), func));
898 }
899
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700900 case TOKEN_SEG:
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800901 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800902 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 if (!e)
904 return NULL;
905 e = segment_part(e);
906 if (!e)
907 return NULL;
908 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800909 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 return NULL;
911 }
912 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700913
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700914 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300915 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700916
H. Peter Anvin9c749102008-06-14 21:08:38 -0700917 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300918 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700919
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700920 case '(':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800921 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800922 e = bexpr();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 if (!e)
924 return NULL;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800925 if (tt != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800926 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 return NULL;
928 }
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800929 scan();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700931
932 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700933 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700934 case TOKEN_REG:
935 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300936 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700937 case TOKEN_HERE:
938 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700939 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 begintemp();
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800941 switch (tt) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 case TOKEN_NUM:
943 addtotemp(EXPR_SIMPLE, tokval->t_integer);
944 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300945 case TOKEN_STR:
946 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
947 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800948 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700949 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300950 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 case TOKEN_REG:
952 addtotemp(tokval->t_integer, 1L);
953 if (hint && hint->type == EAH_NOHINT)
954 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
955 break;
956 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300957 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 case TOKEN_HERE:
959 case TOKEN_BASE:
960 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800961 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 * symbol, Here or Base references are valid because we
963 * are in preprocess-only mode.
964 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800965 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800966 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 "%s not supported in preprocess-only mode",
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800968 (tt == TOKEN_HERE ? "`$'" :
969 tt == TOKEN_BASE ? "`$$'" :
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300970 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 addtotemp(EXPR_UNKNOWN, 1L);
972 break;
973 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000974
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800976 if (tt == TOKEN_BASE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800977 label_seg = in_absolute ? absolute.segment : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 label_ofs = 0;
H. Peter Anvin6fdf7102018-11-28 10:33:16 -0800979 } else if (tt == TOKEN_HERE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800980 label_seg = in_absolute ? absolute.segment : location.segment;
981 label_ofs = in_absolute ? absolute.offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800983 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700984 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800986 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700987 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 return NULL;
989 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800990 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700991 "symbol `%s%s' not defined before use",
992 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 return NULL;
994 } else {
995 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400996 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 type = EXPR_UNKNOWN;
998 label_seg = NO_SEG;
999 label_ofs = 1;
1000 }
1001 }
1002 if (opflags && is_extern(tokval->t_charptr))
1003 *opflags |= OPFLAG_EXTERN;
1004 }
1005 addtotemp(type, label_ofs);
1006 if (label_seg != NO_SEG)
1007 addtotemp(EXPR_SEGBASE + label_seg, 1L);
1008 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -07001009 case TOKEN_DECORATOR:
1010 addtotemp(EXPR_RDSAE, tokval->t_integer);
1011 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 }
H. Peter Anvin99fcda02018-11-28 10:27:30 -08001013 scan();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -07001015
1016 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -08001017 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001019 }
1020}
1021
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvinef427b32018-11-28 10:19:50 -08001023 int *fwref, int crit, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001024{
H. Peter Anvin76690a12002-04-30 20:52:49 +00001025 expr *e;
1026 expr *f = NULL;
1027
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07001028 deadman = 0;
H. Peter Anvin099cc172018-11-28 13:13:16 -08001029
H. Peter Anvin76690a12002-04-30 20:52:49 +00001030 hint = hints;
1031 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001033
H. Peter Anvinef427b32018-11-28 10:19:50 -08001034 critical = crit & ~CRITICAL;
H. Peter Anvin99fcda02018-11-28 10:27:30 -08001035 scanfunc = sc;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001036 scpriv = scprivate;
1037 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001038 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001039
Keith Kaniosb7a89542007-04-12 02:40:54 +00001040 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001042
H. Peter Anvin099cc172018-11-28 13:13:16 -08001043 tt = tokval->t_type;
1044 if (tt == TOKEN_INVALID)
1045 scan();
1046
H. Peter Anvinef427b32018-11-28 10:19:50 -08001047 e = bexpr();
H. Peter Anvin76690a12002-04-30 20:52:49 +00001048 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001050
H. Peter Anvin6fdf7102018-11-28 10:33:16 -08001051 if (tt == TOKEN_WRT) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -08001052 scan(); /* eat the WRT */
H. Peter Anvinef427b32018-11-28 10:19:50 -08001053 f = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 if (!f)
1055 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001056 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001057 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001058 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 expr *g;
1060 if (is_just_unknown(f))
1061 g = unknown_expr();
1062 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001063 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 begintemp();
1065 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001066 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 return NULL;
1068 }
1069 value = reloc_seg(f);
1070 if (value == NO_SEG)
1071 value = reloc_value(f) | SEG_ABS;
1072 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001073 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 return NULL;
1075 }
1076 addtotemp(EXPR_WRT, value);
1077 g = finishtemp();
1078 }
1079 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001080 }
1081 return e;
1082}