blob: c97a8316f0ea836447e9cb6573b3ec59c7c43508 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvin76690a12002-04-30 20:52:49 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * eval.c expression evaluator for the Netwide Assembler
H. Peter Anvin76690a12002-04-30 20:52:49 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvin76690a12002-04-30 20:52:49 +000040#include <stdio.h>
41#include <stdlib.h>
42#include <stddef.h>
43#include <string.h>
44#include <ctype.h>
45
46#include "nasm.h"
47#include "nasmlib.h"
H. Peter Anvin0a126062017-09-27 13:34:42 -070048#include "ilog2.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080049#include "error.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000050#include "eval.h"
H. Peter Anvineba20a72002-04-30 20:53:55 +000051#include "labels.h"
H. Peter Anvindc467ba2007-09-24 12:30:54 -070052#include "float.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080053#include "assemble.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000054
H. Peter Anvin76690a12002-04-30 20:52:49 +000055#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000056#define TEMPEXPR_DELTA 8
57
H. Peter Anvine2c80182005-01-15 22:15:51 +000058static scanner scan; /* Address of scanner routine */
H. Peter Anvineba20a72002-04-30 20:53:55 +000059
H. Peter Anvineba20a72002-04-30 20:53:55 +000060static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000061static int ntempexprs;
62static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000063
H. Peter Anvine2c80182005-01-15 22:15:51 +000064static expr *tempexpr;
65static int ntempexpr;
66static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000067
H. Peter Anvine2c80182005-01-15 22:15:51 +000068static struct tokenval *tokval; /* The current token */
69static int i; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000070
H. Peter Anvinef427b32018-11-28 10:19:50 -080071static int critical;
H. Peter Anvin76690a12002-04-30 20:52:49 +000072static void *scpriv;
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/*
263 * Grammar parsed is:
264 *
265 * expr : bexpr [ WRT expr6 ]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800266 * bexpr : rexp0
H. Peter Anvin76690a12002-04-30 20:52:49 +0000267 * rexp0 : rexp1 [ {||} rexp1...]
268 * rexp1 : rexp2 [ {^^} rexp2...]
269 * rexp2 : rexp3 [ {&&} rexp3...]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800270 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=,<=>} expr0 ]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000271 * expr0 : expr1 [ {|} expr1...]
272 * expr1 : expr2 [ {^} expr2...]
273 * expr2 : expr3 [ {&} expr3...]
H. Peter Anvinca605a32018-11-28 10:07:19 -0800274 * expr3 : expr4 [ {<<,>>,<<<,>>>} expr4...]
H. Peter Anvin76690a12002-04-30 20:52:49 +0000275 * expr4 : expr5 [ {+,-} expr5...]
276 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700277 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000278 * | (bexpr)
279 * | symbol
280 * | $
281 * | number
282 */
283
H. Peter Anvinef427b32018-11-28 10:19:50 -0800284static expr *rexp0(void), *rexp1(void), *rexp2(void), *rexp3(void);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000285
H. Peter Anvinef427b32018-11-28 10:19:50 -0800286static expr *expr0(void), *expr1(void), *expr2(void), *expr3(void);
287static expr *expr4(void), *expr5(void), *expr6(void);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000288
H. Peter Anvinca605a32018-11-28 10:07:19 -0800289/* This inline is a placeholder for the root of the basic expression */
H. Peter Anvinef427b32018-11-28 10:19:50 -0800290static inline expr *bexpr(void)
H. Peter Anvinca605a32018-11-28 10:07:19 -0800291{
H. Peter Anvinef427b32018-11-28 10:19:50 -0800292 return rexp0();
H. Peter Anvinca605a32018-11-28 10:07:19 -0800293}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000294
H. Peter Anvinef427b32018-11-28 10:19:50 -0800295static expr *rexp0(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000296{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000297 expr *e, *f;
298
H. Peter Anvinef427b32018-11-28 10:19:50 -0800299 e = rexp1();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000300 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000301 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000302
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303 while (i == TOKEN_DBL_OR) {
304 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800305 f = rexp1();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306 if (!f)
307 return NULL;
308 if (!(is_simple(e) || is_just_unknown(e)) ||
309 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800310 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000311 " scalar values");
312 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000313
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314 if (is_just_unknown(e) || is_just_unknown(f))
315 e = unknown_expr();
316 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700317 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000318 }
319 return e;
320}
321
H. Peter Anvinef427b32018-11-28 10:19:50 -0800322static expr *rexp1(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000323{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000324 expr *e, *f;
325
H. Peter Anvinef427b32018-11-28 10:19:50 -0800326 e = rexp2();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000327 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000329
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330 while (i == TOKEN_DBL_XOR) {
331 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800332 f = rexp2();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000333 if (!f)
334 return NULL;
335 if (!(is_simple(e) || is_just_unknown(e)) ||
336 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800337 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000338 " scalar values");
339 }
340
341 if (is_just_unknown(e) || is_just_unknown(f))
342 e = unknown_expr();
343 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700344 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000345 }
346 return e;
347}
348
H. Peter Anvinef427b32018-11-28 10:19:50 -0800349static expr *rexp2(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000350{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000351 expr *e, *f;
352
H. Peter Anvinef427b32018-11-28 10:19:50 -0800353 e = rexp3();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000354 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000355 return NULL;
356 while (i == TOKEN_DBL_AND) {
357 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800358 f = rexp3();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000359 if (!f)
360 return NULL;
361 if (!(is_simple(e) || is_just_unknown(e)) ||
362 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800363 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000364 " scalar values");
365 }
366 if (is_just_unknown(e) || is_just_unknown(f))
367 e = unknown_expr();
368 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700369 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000370 }
371 return e;
372}
373
H. Peter Anvinef427b32018-11-28 10:19:50 -0800374static expr *rexp3(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000375{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000376 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700377 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000378
H. Peter Anvinef427b32018-11-28 10:19:50 -0800379 e = expr0();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000380 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000381 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000382
H. Peter Anvin76690a12002-04-30 20:52:49 +0000383 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700384 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE ||
385 i == TOKEN_LEG) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 int j = i;
387 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800388 f = expr0();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 if (!f)
390 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000391
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700392 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000393
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 switch (j) {
395 case TOKEN_EQ:
396 case TOKEN_NE:
397 if (is_unknown(e))
398 v = -1; /* means unknown */
399 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700400 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700402 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000403 break;
404 default:
405 if (is_unknown(e))
406 v = -1; /* means unknown */
407 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800408 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000409 "`%s': operands differ by a non-scalar",
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700410 (j == TOKEN_LE ? "<=" :
411 j == TOKEN_LT ? "<" :
412 j == TOKEN_GE ? ">=" :
413 j == TOKEN_GT ? ">" :
414 j == TOKEN_LEG ? "<=>" :
415 "<internal error>"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 v = 0; /* must set it to _something_ */
417 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300418 int64_t vv = reloc_value(e);
H. Peter Anvin3bb1dd02018-06-15 18:46:11 -0700419 if (j == TOKEN_LEG)
420 v = (vv < 0) ? -1 : (vv > 0) ? 1 : 0;
421 else if (vv == 0)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 v = (j == TOKEN_LE || j == TOKEN_GE);
423 else if (vv > 0)
424 v = (j == TOKEN_GE || j == TOKEN_GT);
425 else /* vv < 0 */
426 v = (j == TOKEN_LE || j == TOKEN_LT);
427 }
428 break;
429 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000430
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 if (v == -1)
432 e = unknown_expr();
433 else
434 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000435 }
436 return e;
437}
438
H. Peter Anvinef427b32018-11-28 10:19:50 -0800439static expr *expr0(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000440{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000441 expr *e, *f;
442
H. Peter Anvinef427b32018-11-28 10:19:50 -0800443 e = expr1();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000445 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000446
H. Peter Anvine2c80182005-01-15 22:15:51 +0000447 while (i == '|') {
448 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800449 f = expr1();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 if (!f)
451 return NULL;
452 if (!(is_simple(e) || is_just_unknown(e)) ||
453 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800454 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000455 " scalar values");
456 }
457 if (is_just_unknown(e) || is_just_unknown(f))
458 e = unknown_expr();
459 else
460 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000461 }
462 return e;
463}
464
H. Peter Anvinef427b32018-11-28 10:19:50 -0800465static expr *expr1(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000466{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000467 expr *e, *f;
468
H. Peter Anvinef427b32018-11-28 10:19:50 -0800469 e = expr2();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000470 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000471 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000472
H. Peter Anvin76690a12002-04-30 20:52:49 +0000473 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800475 f = expr2();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 if (!f)
477 return NULL;
478 if (!(is_simple(e) || is_just_unknown(e)) ||
479 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800480 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000481 " scalar values");
482 }
483 if (is_just_unknown(e) || is_just_unknown(f))
484 e = unknown_expr();
485 else
486 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000487 }
488 return e;
489}
490
H. Peter Anvinef427b32018-11-28 10:19:50 -0800491static expr *expr2(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000492{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000493 expr *e, *f;
494
H. Peter Anvinef427b32018-11-28 10:19:50 -0800495 e = expr3();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000496 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000498
H. Peter Anvin76690a12002-04-30 20:52:49 +0000499 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000500 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800501 f = expr3();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 if (!f)
503 return NULL;
504 if (!(is_simple(e) || is_just_unknown(e)) ||
505 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800506 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507 " scalar values");
508 }
509 if (is_just_unknown(e) || is_just_unknown(f))
510 e = unknown_expr();
511 else
512 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000513 }
514 return e;
515}
516
H. Peter Anvinef427b32018-11-28 10:19:50 -0800517static expr *expr3(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000518{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000519 expr *e, *f;
520
H. Peter Anvinef427b32018-11-28 10:19:50 -0800521 e = expr4();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000522 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000524
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700525 while (i == TOKEN_SHL || i == TOKEN_SHR || i == TOKEN_SAR) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 int j = i;
527 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800528 f = expr4();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 if (!f)
530 return NULL;
531 if (!(is_simple(e) || is_just_unknown(e)) ||
532 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800533 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 " scalar values");
535 } else if (is_just_unknown(e) || is_just_unknown(f)) {
536 e = unknown_expr();
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700537 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 switch (j) {
539 case TOKEN_SHL:
540 e = scalarvect(reloc_value(e) << reloc_value(f));
541 break;
542 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700543 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 reloc_value(f));
545 break;
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700546 case TOKEN_SAR:
547 e = scalarvect(((int64_t)reloc_value(e)) >>
548 reloc_value(f));
549 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000550 }
H. Peter Anvin94adf7d2018-06-15 18:37:32 -0700551 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000552 }
553 return e;
554}
555
H. Peter Anvinef427b32018-11-28 10:19:50 -0800556static expr *expr4(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000557{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000558 expr *e, *f;
559
H. Peter Anvinef427b32018-11-28 10:19:50 -0800560 e = expr5();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000561 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 return NULL;
563 while (i == '+' || i == '-') {
564 int j = i;
565 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800566 f = expr5();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 if (!f)
568 return NULL;
569 switch (j) {
570 case '+':
571 e = add_vectors(e, f);
572 break;
573 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700574 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 break;
576 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000577 }
578 return e;
579}
580
H. Peter Anvinef427b32018-11-28 10:19:50 -0800581static expr *expr5(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000583 expr *e, *f;
584
H. Peter Anvinef427b32018-11-28 10:19:50 -0800585 e = expr6();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000586 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000588 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 i == TOKEN_SDIV || i == TOKEN_SMOD) {
590 int j = i;
591 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800592 f = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 if (!f)
594 return NULL;
595 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
596 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800597 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 " scalar values");
599 return NULL;
600 }
Cyrill Gorcunovceec0d82018-10-14 01:26:19 +0300601 if (j != '*' && !is_just_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800602 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 return NULL;
604 }
605 switch (j) {
606 case '*':
607 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700608 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700610 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 else if (is_just_unknown(e) && is_just_unknown(f))
612 e = unknown_expr();
613 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800614 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 "non-scalar objects");
616 return NULL;
617 }
618 break;
619 case '/':
620 if (is_just_unknown(e) || is_just_unknown(f))
621 e = unknown_expr();
622 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700623 e = scalarvect(((uint64_t)reloc_value(e)) /
624 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 break;
626 case '%':
627 if (is_just_unknown(e) || is_just_unknown(f))
628 e = unknown_expr();
629 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700630 e = scalarvect(((uint64_t)reloc_value(e)) %
631 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 break;
633 case TOKEN_SDIV:
634 if (is_just_unknown(e) || is_just_unknown(f))
635 e = unknown_expr();
636 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700637 e = scalarvect(((int64_t)reloc_value(e)) /
638 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 break;
640 case TOKEN_SMOD:
641 if (is_just_unknown(e) || is_just_unknown(f))
642 e = unknown_expr();
643 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700644 e = scalarvect(((int64_t)reloc_value(e)) %
645 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646 break;
647 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000648 }
649 return e;
650}
651
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700652static expr *eval_floatize(enum floatize type)
653{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300654 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700655 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300656 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700657 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300658 { 1, 0, 1 }, /* FLOAT_8 */
659 { 2, 0, 2 }, /* FLOAT_16 */
660 { 4, 0, 4 }, /* FLOAT_32 */
661 { 8, 0, 8 }, /* FLOAT_64 */
662 { 10, 0, 8 }, /* FLOAT_80M */
663 { 10, 8, 2 }, /* FLOAT_80E */
664 { 16, 0, 8 }, /* FLOAT_128L */
665 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700666 };
667 int sign = 1;
668 int64_t val;
669 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700670
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700671 i = scan(scpriv, tokval);
672 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800673 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300674 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700675 }
676 i = scan(scpriv, tokval);
677 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300678 sign = (i == '-') ? -1 : 1;
679 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700680 }
681 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800682 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300683 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700684 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800685 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300686 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700687 i = scan(scpriv, tokval);
688 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800689 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300690 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700691 }
692
693 p = result+formats[type].start+formats[type].len;
694 val = 0;
695 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300696 p--;
697 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700698 }
699
700 begintemp();
701 addtotemp(EXPR_SIMPLE, val);
702
703 i = scan(scpriv, tokval);
704 return finishtemp();
705}
706
H. Peter Anvin9c749102008-06-14 21:08:38 -0700707static expr *eval_strfunc(enum strfunc type)
708{
709 char *string;
710 size_t string_len;
711 int64_t val;
712 bool parens, rn_warn;
713
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200714 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700715 i = scan(scpriv, tokval);
716 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300717 parens = true;
718 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700719 }
720 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800721 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300722 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700723 }
724 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300725 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700726 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800727 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300728 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700729 }
730
731 val = readstrnum(string, string_len, &rn_warn);
732 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300733 i = scan(scpriv, tokval);
734 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800735 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300736 return NULL;
737 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700738 }
739
740 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800741 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700742
743 begintemp();
744 addtotemp(EXPR_SIMPLE, val);
745
746 i = scan(scpriv, tokval);
747 return finishtemp();
748}
749
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700750static int64_t eval_ifunc(int64_t val, enum ifunc func)
751{
752 int errtype;
753 uint64_t uval = (uint64_t)val;
754 int64_t rv;
755
756 switch (func) {
757 case IFUNC_ILOG2E:
758 case IFUNC_ILOG2W:
759 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400760
761 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800762 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700763 /* fall through */
764 case IFUNC_ILOG2F:
765 rv = ilog2_64(uval);
766 break;
767
768 case IFUNC_ILOG2C:
769 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
770 break;
771
772 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700773 nasm_panic("invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700774 rv = 0;
775 break;
776 }
777
778 return rv;
779}
780
H. Peter Anvinef427b32018-11-28 10:19:50 -0800781static expr *expr6(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000782{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000783 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000784 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800785 int32_t label_seg;
786 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700787 int64_t tmpval;
788 bool rn_warn;
H. Peter Anvin98578072018-06-01 18:02:54 -0700789 const char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000790
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700791 if (++deadman > nasm_limit[LIMIT_EVAL]) {
792 nasm_error(ERR_NONFATAL, "expression too long");
793 return NULL;
794 }
795
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700796 switch (i) {
797 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800799 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 if (!e)
801 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700802 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700803
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700804 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800806 return expr6();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700807
808 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800810 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 if (!e)
812 return NULL;
813 if (is_just_unknown(e))
814 return unknown_expr();
815 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800816 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 " scalar values");
818 return NULL;
819 }
820 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700821
822 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000823 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800824 e = expr6();
Chuck Craynecb9bc212007-05-02 04:21:26 +0000825 if (!e)
826 return NULL;
827 if (is_just_unknown(e))
828 return unknown_expr();
829 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800830 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000831 " scalar values");
832 return NULL;
833 }
834 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700835
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700836 case TOKEN_IFUNC:
837 {
838 enum ifunc func = tokval->t_integer;
839 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800840 e = expr6();
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700841 if (!e)
842 return NULL;
843 if (is_just_unknown(e))
844 return unknown_expr();
845 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800846 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700847 " scalar values");
848 return NULL;
849 }
850 return scalarvect(eval_ifunc(reloc_value(e), func));
851 }
852
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700853 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800855 e = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 if (!e)
857 return NULL;
858 e = segment_part(e);
859 if (!e)
860 return NULL;
861 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800862 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 return NULL;
864 }
865 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700866
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700867 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300868 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700869
H. Peter Anvin9c749102008-06-14 21:08:38 -0700870 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300871 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700872
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700873 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 i = scan(scpriv, tokval);
H. Peter Anvinef427b32018-11-28 10:19:50 -0800875 e = bexpr();
H. Peter Anvine2c80182005-01-15 22:15:51 +0000876 if (!e)
877 return NULL;
878 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800879 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 return NULL;
881 }
882 i = scan(scpriv, tokval);
883 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700884
885 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700886 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700887 case TOKEN_REG:
888 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300889 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700890 case TOKEN_HERE:
891 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700892 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 begintemp();
894 switch (i) {
895 case TOKEN_NUM:
896 addtotemp(EXPR_SIMPLE, tokval->t_integer);
897 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300898 case TOKEN_STR:
899 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
900 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800901 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700902 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300903 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 case TOKEN_REG:
905 addtotemp(tokval->t_integer, 1L);
906 if (hint && hint->type == EAH_NOHINT)
907 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
908 break;
909 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300910 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 case TOKEN_HERE:
912 case TOKEN_BASE:
913 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800914 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 * symbol, Here or Base references are valid because we
916 * are in preprocess-only mode.
917 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800918 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800919 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300921 (i == TOKEN_HERE ? "`$'" :
922 i == TOKEN_BASE ? "`$$'" :
923 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 addtotemp(EXPR_UNKNOWN, 1L);
925 break;
926 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000927
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
929 if (i == TOKEN_BASE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800930 label_seg = in_absolute ? absolute.segment : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 label_ofs = 0;
932 } else if (i == TOKEN_HERE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800933 label_seg = in_absolute ? absolute.segment : location.segment;
934 label_ofs = in_absolute ? absolute.offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800936 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700937 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800939 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700940 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 return NULL;
942 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800943 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700944 "symbol `%s%s' not defined before use",
945 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 return NULL;
947 } else {
948 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400949 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 type = EXPR_UNKNOWN;
951 label_seg = NO_SEG;
952 label_ofs = 1;
953 }
954 }
955 if (opflags && is_extern(tokval->t_charptr))
956 *opflags |= OPFLAG_EXTERN;
957 }
958 addtotemp(type, label_ofs);
959 if (label_seg != NO_SEG)
960 addtotemp(EXPR_SEGBASE + label_seg, 1L);
961 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700962 case TOKEN_DECORATOR:
963 addtotemp(EXPR_RDSAE, tokval->t_integer);
964 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 }
966 i = scan(scpriv, tokval);
967 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700968
969 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800970 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000972 }
973}
974
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvinef427b32018-11-28 10:19:50 -0800976 int *fwref, int crit, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000977{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000978 expr *e;
979 expr *f = NULL;
980
H. Peter Anvin987dc9c2018-06-12 13:50:37 -0700981 deadman = 0;
982
H. Peter Anvin76690a12002-04-30 20:52:49 +0000983 hint = hints;
984 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000986
H. Peter Anvinef427b32018-11-28 10:19:50 -0800987 critical = crit & ~CRITICAL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000988 scan = sc;
989 scpriv = scprivate;
990 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000991 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000992
993 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000995 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000997
Keith Kaniosb7a89542007-04-12 02:40:54 +0000998 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001000
H. Peter Anvinef427b32018-11-28 10:19:50 -08001001 e = bexpr();
H. Peter Anvin76690a12002-04-30 20:52:49 +00001002 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001004
1005 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 i = scan(scpriv, tokval); /* eat the WRT */
H. Peter Anvinef427b32018-11-28 10:19:50 -08001007 f = expr6();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 if (!f)
1009 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001010 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001011 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001012 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 expr *g;
1014 if (is_just_unknown(f))
1015 g = unknown_expr();
1016 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001017 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 begintemp();
1019 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001020 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 return NULL;
1022 }
1023 value = reloc_seg(f);
1024 if (value == NO_SEG)
1025 value = reloc_value(f) | SEG_ABS;
1026 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001027 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 return NULL;
1029 }
1030 addtotemp(EXPR_WRT, value);
1031 g = finishtemp();
1032 }
1033 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001034 }
1035 return e;
1036}