blob: 53765e982d1fee0d74bfa804084a6cccdc9ae9df [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 */
70static int i; /* 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 Anvin76690a12002-04-30 20:52:49 +0000253 * symbols are an error condition). Must update the global `i' to
254 * 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{
267 return i = scanfunc(scpriv, tokval);
268}
269
270/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000271 * Grammar parsed is:
272 *
273 * expr : bexpr [ WRT expr6 ]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800274 * bexpr : rexp0
H. Peter Anvin76690a12002-04-30 20:52:49 +0000275 * rexp0 : rexp1 [ {||} rexp1...]
276 * rexp1 : rexp2 [ {^^} rexp2...]
277 * rexp2 : rexp3 [ {&&} rexp3...]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800278 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=,<=>} expr0 ]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000279 * expr0 : expr1 [ {|} expr1...]
280 * expr1 : expr2 [ {^} expr2...]
281 * expr2 : expr3 [ {&} expr3...]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800282 * expr3 : expr4 [ {<<,>>,<<<,>>>} expr4...]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000283 * expr4 : expr5 [ {+,-} expr5...]
284 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700285 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000286 * | (bexpr)
287 * | symbol
288 * | $
289 * | number
290 */
291
H. Peter Anvinef427b32018-11-28 10:19:50 -0800292static expr *rexp0(void), *rexp1(void), *rexp2(void), *rexp3(void);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000293
H. Peter Anvinef427b32018-11-28 10:19:50 -0800294static expr *expr0(void), *expr1(void), *expr2(void), *expr3(void);
295static expr *expr4(void), *expr5(void), *expr6(void);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000296
H. Peter Anvinca605a32018-11-28 10:07:19 -0800297/* This inline is a placeholder for the root of the basic expression */
H. Peter Anvinef427b32018-11-28 10:19:50 -0800298static inline expr *bexpr(void)
H. Peter Anvinca605a32018-11-28 10:07:19 -0800299{
H. Peter Anvinef427b32018-11-28 10:19:50 -0800300 return rexp0();
H. Peter Anvinca605a32018-11-28 10:07:19 -0800301}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000302
H. Peter Anvinef427b32018-11-28 10:19:50 -0800303static expr *rexp0(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000304{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000305 expr *e, *f;
306
H. Peter Anvinef427b32018-11-28 10:19:50 -0800307 e = rexp1();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000308 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000310
H. Peter Anvine2c80182005-01-15 22:15:51 +0000311 while (i == TOKEN_DBL_OR) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800312 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800313 f = rexp1();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314 if (!f)
315 return NULL;
316 if (!(is_simple(e) || is_just_unknown(e)) ||
317 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800318 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000319 " scalar values");
320 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000321
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 if (is_just_unknown(e) || is_just_unknown(f))
323 e = unknown_expr();
324 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700325 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000326 }
327 return e;
328}
329
H. Peter Anvinef427b32018-11-28 10:19:50 -0800330static expr *rexp1(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000331{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000332 expr *e, *f;
333
H. Peter Anvinef427b32018-11-28 10:19:50 -0800334 e = rexp2();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000335 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000336 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000337
H. Peter Anvine2c80182005-01-15 22:15:51 +0000338 while (i == TOKEN_DBL_XOR) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800339 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800340 f = rexp2();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000341 if (!f)
342 return NULL;
343 if (!(is_simple(e) || is_just_unknown(e)) ||
344 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800345 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000346 " scalar values");
347 }
348
349 if (is_just_unknown(e) || is_just_unknown(f))
350 e = unknown_expr();
351 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700352 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000353 }
354 return e;
355}
356
H. Peter Anvinef427b32018-11-28 10:19:50 -0800357static expr *rexp2(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000359 expr *e, *f;
360
H. Peter Anvinef427b32018-11-28 10:19:50 -0800361 e = rexp3();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000363 return NULL;
364 while (i == TOKEN_DBL_AND) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800365 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800366 f = rexp3();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367 if (!f)
368 return NULL;
369 if (!(is_simple(e) || is_just_unknown(e)) ||
370 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800371 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372 " scalar values");
373 }
374 if (is_just_unknown(e) || is_just_unknown(f))
375 e = unknown_expr();
376 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700377 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000378 }
379 return e;
380}
381
H. Peter Anvinef427b32018-11-28 10:19:50 -0800382static expr *rexp3(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000383{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000384 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700385 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000386
H. Peter Anvinef427b32018-11-28 10:19:50 -0800387 e = expr0();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000388 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000390
H. Peter Anvin76690a12002-04-30 20:52:49 +0000391 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700392 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE ||
393 i == TOKEN_LEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 int j = i;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800395 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800396 f = expr0();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397 if (!f)
398 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700400 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000401
H. Peter Anvine2c80182005-01-15 22:15:51 +0000402 switch (j) {
403 case TOKEN_EQ:
404 case TOKEN_NE:
405 if (is_unknown(e))
406 v = -1; /* means unknown */
407 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700408 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700410 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411 break;
412 default:
413 if (is_unknown(e))
414 v = -1; /* means unknown */
415 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800416 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 "`%s': operands differ by a non-scalar",
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700418 (j == TOKEN_LE ? "<=" :
419 j == TOKEN_LT ? "<" :
420 j == TOKEN_GE ? ">=" :
421 j == TOKEN_GT ? ">" :
422 j == TOKEN_LEG ? "<=>" :
423 "<internal error>"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424 v = 0; /* must set it to _something_ */
425 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300426 int64_t vv = reloc_value(e);
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700427 if (j == TOKEN_LEG)
428 v = (vv < 0) ? -1 : (vv > 0) ? 1 : 0;
429 else if (vv == 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 v = (j == TOKEN_LE || j == TOKEN_GE);
431 else if (vv > 0)
432 v = (j == TOKEN_GE || j == TOKEN_GT);
433 else /* vv < 0 */
434 v = (j == TOKEN_LE || j == TOKEN_LT);
435 }
436 break;
437 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000438
H. Peter Anvine2c80182005-01-15 22:15:51 +0000439 if (v == -1)
440 e = unknown_expr();
441 else
442 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000443 }
444 return e;
445}
446
H. Peter Anvinef427b32018-11-28 10:19:50 -0800447static expr *expr0(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000448{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000449 expr *e, *f;
450
H. Peter Anvinef427b32018-11-28 10:19:50 -0800451 e = expr1();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000452 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000454
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 while (i == '|') {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800456 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800457 f = expr1();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 if (!f)
459 return NULL;
460 if (!(is_simple(e) || is_just_unknown(e)) ||
461 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800462 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 " scalar values");
464 }
465 if (is_just_unknown(e) || is_just_unknown(f))
466 e = unknown_expr();
467 else
468 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000469 }
470 return e;
471}
472
H. Peter Anvinef427b32018-11-28 10:19:50 -0800473static expr *expr1(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000474{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000475 expr *e, *f;
476
H. Peter Anvinef427b32018-11-28 10:19:50 -0800477 e = expr2();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000478 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000479 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000480
H. Peter Anvin76690a12002-04-30 20:52:49 +0000481 while (i == '^') {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800482 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800483 f = expr2();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000484 if (!f)
485 return NULL;
486 if (!(is_simple(e) || is_just_unknown(e)) ||
487 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800488 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 " scalar values");
490 }
491 if (is_just_unknown(e) || is_just_unknown(f))
492 e = unknown_expr();
493 else
494 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000495 }
496 return e;
497}
498
H. Peter Anvinef427b32018-11-28 10:19:50 -0800499static expr *expr2(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000500{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000501 expr *e, *f;
502
H. Peter Anvinef427b32018-11-28 10:19:50 -0800503 e = expr3();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000504 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000505 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000506
H. Peter Anvin76690a12002-04-30 20:52:49 +0000507 while (i == '&') {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800508 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800509 f = expr3();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 if (!f)
511 return NULL;
512 if (!(is_simple(e) || is_just_unknown(e)) ||
513 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800514 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000515 " scalar values");
516 }
517 if (is_just_unknown(e) || is_just_unknown(f))
518 e = unknown_expr();
519 else
520 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000521 }
522 return e;
523}
524
H. Peter Anvinef427b32018-11-28 10:19:50 -0800525static expr *expr3(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000527 expr *e, *f;
528
H. Peter Anvinef427b32018-11-28 10:19:50 -0800529 e = expr4();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000530 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000532
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700533 while (i == TOKEN_SHL || i == TOKEN_SHR || i == TOKEN_SAR) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 int j = i;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800535 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800536 f = expr4();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 if (!f)
538 return NULL;
539 if (!(is_simple(e) || is_just_unknown(e)) ||
540 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800541 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 " scalar values");
543 } else if (is_just_unknown(e) || is_just_unknown(f)) {
544 e = unknown_expr();
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700545 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 switch (j) {
547 case TOKEN_SHL:
548 e = scalarvect(reloc_value(e) << reloc_value(f));
549 break;
550 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700551 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552 reloc_value(f));
553 break;
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700554 case TOKEN_SAR:
555 e = scalarvect(((int64_t)reloc_value(e)) >>
556 reloc_value(f));
557 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 }
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700559 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000560 }
561 return e;
562}
563
H. Peter Anvinef427b32018-11-28 10:19:50 -0800564static expr *expr4(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 = expr5();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000569 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 return NULL;
571 while (i == '+' || i == '-') {
572 int j = i;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800573 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800574 f = expr5();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 if (!f)
576 return NULL;
577 switch (j) {
578 case '+':
579 e = add_vectors(e, f);
580 break;
581 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700582 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 break;
584 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000585 }
586 return e;
587}
588
H. Peter Anvinef427b32018-11-28 10:19:50 -0800589static expr *expr5(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000590{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000591 expr *e, *f;
592
H. Peter Anvinef427b32018-11-28 10:19:50 -0800593 e = expr6();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000594 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000596 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 i == TOKEN_SDIV || i == TOKEN_SMOD) {
598 int j = i;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800599 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800600 f = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 if (!f)
602 return NULL;
603 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
604 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800605 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 " scalar values");
607 return NULL;
608 }
Cyrill Gorcunovceec0d82018-10-14 01:26:19 +0300609 if (j != '*' && !is_just_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800610 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 return NULL;
612 }
613 switch (j) {
614 case '*':
615 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700616 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700618 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 else if (is_just_unknown(e) && is_just_unknown(f))
620 e = unknown_expr();
621 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800622 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 "non-scalar objects");
624 return NULL;
625 }
626 break;
627 case '/':
628 if (is_just_unknown(e) || is_just_unknown(f))
629 e = unknown_expr();
630 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700631 e = scalarvect(((uint64_t)reloc_value(e)) /
632 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 break;
634 case '%':
635 if (is_just_unknown(e) || is_just_unknown(f))
636 e = unknown_expr();
637 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700638 e = scalarvect(((uint64_t)reloc_value(e)) %
639 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 break;
641 case TOKEN_SDIV:
642 if (is_just_unknown(e) || is_just_unknown(f))
643 e = unknown_expr();
644 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700645 e = scalarvect(((int64_t)reloc_value(e)) /
646 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647 break;
648 case TOKEN_SMOD:
649 if (is_just_unknown(e) || is_just_unknown(f))
650 e = unknown_expr();
651 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700652 e = scalarvect(((int64_t)reloc_value(e)) %
653 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 break;
655 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000656 }
657 return e;
658}
659
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700660static expr *eval_floatize(enum floatize type)
661{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300662 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700663 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300664 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700665 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300666 { 1, 0, 1 }, /* FLOAT_8 */
667 { 2, 0, 2 }, /* FLOAT_16 */
668 { 4, 0, 4 }, /* FLOAT_32 */
669 { 8, 0, 8 }, /* FLOAT_64 */
670 { 10, 0, 8 }, /* FLOAT_80M */
671 { 10, 8, 2 }, /* FLOAT_80E */
672 { 16, 0, 8 }, /* FLOAT_128L */
673 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700674 };
675 int sign = 1;
676 int64_t val;
677 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700678
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800679 scan();
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700680 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800681 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300682 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700683 }
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800684 scan();
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700685 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300686 sign = (i == '-') ? -1 : 1;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800687 scan();
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700688 }
689 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800690 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300691 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700692 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800693 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300694 return NULL;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800695 scan();
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700696 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800697 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300698 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700699 }
700
701 p = result+formats[type].start+formats[type].len;
702 val = 0;
703 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300704 p--;
705 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700706 }
707
708 begintemp();
709 addtotemp(EXPR_SIMPLE, val);
710
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800711 scan();
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700712 return finishtemp();
713}
714
H. Peter Anvin9c749102008-06-14 21:08:38 -0700715static expr *eval_strfunc(enum strfunc type)
716{
717 char *string;
718 size_t string_len;
719 int64_t val;
720 bool parens, rn_warn;
721
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200722 parens = false;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800723 scan();
H. Peter Anvin9c749102008-06-14 21:08:38 -0700724 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300725 parens = true;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800726 scan();
H. Peter Anvin9c749102008-06-14 21:08:38 -0700727 }
728 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800729 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300730 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700731 }
732 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300733 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700734 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800735 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300736 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700737 }
738
739 val = readstrnum(string, string_len, &rn_warn);
740 if (parens) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800741 scan();
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300742 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800743 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300744 return NULL;
745 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700746 }
747
748 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800749 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700750
751 begintemp();
752 addtotemp(EXPR_SIMPLE, val);
753
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800754 scan();
H. Peter Anvin9c749102008-06-14 21:08:38 -0700755 return finishtemp();
756}
757
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700758static int64_t eval_ifunc(int64_t val, enum ifunc func)
759{
760 int errtype;
761 uint64_t uval = (uint64_t)val;
762 int64_t rv;
763
764 switch (func) {
765 case IFUNC_ILOG2E:
766 case IFUNC_ILOG2W:
767 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400768
769 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800770 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700771 /* fall through */
772 case IFUNC_ILOG2F:
773 rv = ilog2_64(uval);
774 break;
775
776 case IFUNC_ILOG2C:
777 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
778 break;
779
780 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700781 nasm_panic("invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700782 rv = 0;
783 break;
784 }
785
786 return rv;
787}
788
H. Peter Anvinef427b32018-11-28 10:19:50 -0800789static expr *expr6(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000790{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000791 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000792 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800793 int32_t label_seg;
794 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700795 int64_t tmpval;
796 bool rn_warn;
H. Peter Anvin98578072018-06-01 18:02:54 -0700797 const char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000798
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700799 if (++deadman > nasm_limit[LIMIT_EVAL]) {
800 nasm_error(ERR_NONFATAL, "expression too long");
801 return NULL;
802 }
803
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700804 switch (i) {
805 case '-':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800806 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800807 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 if (!e)
809 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700810 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700811
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700812 case '+':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800813 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800814 return expr6();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700815
816 case '~':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800817 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800818 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 if (!e)
820 return NULL;
821 if (is_just_unknown(e))
822 return unknown_expr();
823 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800824 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 " scalar values");
826 return NULL;
827 }
828 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700829
830 case '!':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800831 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800832 e = expr6();
Chuck Craynecb9bc212007-05-02 04:21:26 +0000833 if (!e)
834 return NULL;
835 if (is_just_unknown(e))
836 return unknown_expr();
837 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800838 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000839 " scalar values");
840 return NULL;
841 }
842 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700843
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700844 case TOKEN_IFUNC:
845 {
846 enum ifunc func = tokval->t_integer;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800847 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800848 e = expr6();
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700849 if (!e)
850 return NULL;
851 if (is_just_unknown(e))
852 return unknown_expr();
853 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800854 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700855 " scalar values");
856 return NULL;
857 }
858 return scalarvect(eval_ifunc(reloc_value(e), func));
859 }
860
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700861 case TOKEN_SEG:
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800862 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800863 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000864 if (!e)
865 return NULL;
866 e = segment_part(e);
867 if (!e)
868 return NULL;
869 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800870 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 return NULL;
872 }
873 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700874
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700875 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300876 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700877
H. Peter Anvin9c749102008-06-14 21:08:38 -0700878 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300879 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700880
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700881 case '(':
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800882 scan();
H. Peter Anvinef427b32018-11-28 10:19:50 -0800883 e = bexpr();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000884 if (!e)
885 return NULL;
886 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800887 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 return NULL;
889 }
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800890 scan();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700892
893 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700894 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700895 case TOKEN_REG:
896 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300897 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700898 case TOKEN_HERE:
899 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700900 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 begintemp();
902 switch (i) {
903 case TOKEN_NUM:
904 addtotemp(EXPR_SIMPLE, tokval->t_integer);
905 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300906 case TOKEN_STR:
907 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
908 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800909 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700910 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300911 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 case TOKEN_REG:
913 addtotemp(tokval->t_integer, 1L);
914 if (hint && hint->type == EAH_NOHINT)
915 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
916 break;
917 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300918 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 case TOKEN_HERE:
920 case TOKEN_BASE:
921 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800922 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 * symbol, Here or Base references are valid because we
924 * are in preprocess-only mode.
925 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800926 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800927 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300929 (i == TOKEN_HERE ? "`$'" :
930 i == TOKEN_BASE ? "`$$'" :
931 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 addtotemp(EXPR_UNKNOWN, 1L);
933 break;
934 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000935
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
937 if (i == TOKEN_BASE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800938 label_seg = in_absolute ? absolute.segment : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 label_ofs = 0;
940 } else if (i == TOKEN_HERE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800941 label_seg = in_absolute ? absolute.segment : location.segment;
942 label_ofs = in_absolute ? absolute.offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800944 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700945 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800947 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700948 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 return NULL;
950 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800951 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700952 "symbol `%s%s' not defined before use",
953 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 return NULL;
955 } else {
956 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400957 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 type = EXPR_UNKNOWN;
959 label_seg = NO_SEG;
960 label_ofs = 1;
961 }
962 }
963 if (opflags && is_extern(tokval->t_charptr))
964 *opflags |= OPFLAG_EXTERN;
965 }
966 addtotemp(type, label_ofs);
967 if (label_seg != NO_SEG)
968 addtotemp(EXPR_SEGBASE + label_seg, 1L);
969 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700970 case TOKEN_DECORATOR:
971 addtotemp(EXPR_RDSAE, tokval->t_integer);
972 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 }
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800974 scan();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700976
977 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800978 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000980 }
981}
982
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvinef427b32018-11-28 10:19:50 -0800984 int *fwref, int crit, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000985{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000986 expr *e;
987 expr *f = NULL;
988
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700989 deadman = 0;
990
H. Peter Anvin76690a12002-04-30 20:52:49 +0000991 hint = hints;
992 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000994
H. Peter Anvinef427b32018-11-28 10:19:50 -0800995 critical = crit & ~CRITICAL;
H. Peter Anvin99fcda02018-11-28 10:27:30 -0800996 scanfunc = sc;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000997 scpriv = scprivate;
998 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000999 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001000
1001 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvin99fcda02018-11-28 10:27:30 -08001002 scan();
H. Peter Anvin76690a12002-04-30 20:52:49 +00001003 else
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001005
Keith Kaniosb7a89542007-04-12 02:40:54 +00001006 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001008
H. Peter Anvinef427b32018-11-28 10:19:50 -08001009 e = bexpr();
H. Peter Anvin76690a12002-04-30 20:52:49 +00001010 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001012
1013 if (i == TOKEN_WRT) {
H. Peter Anvin99fcda02018-11-28 10:27:30 -08001014 scan(); /* eat the WRT */
H. Peter Anvinef427b32018-11-28 10:19:50 -08001015 f = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 if (!f)
1017 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001018 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001019 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001020 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 expr *g;
1022 if (is_just_unknown(f))
1023 g = unknown_expr();
1024 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001025 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 begintemp();
1027 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001028 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 return NULL;
1030 }
1031 value = reloc_seg(f);
1032 if (value == NO_SEG)
1033 value = reloc_value(f) | SEG_ABS;
1034 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001035 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 return NULL;
1037 }
1038 addtotemp(EXPR_WRT, value);
1039 g = finishtemp();
1040 }
1041 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001042 }
1043 return e;
1044}