blob: 89f66fb7f0da88db9faa02737a69d03adae90ae8 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvinb20bc732017-03-07 19:23:03 -08003 * Copyright 1996-2017 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 Anvinb20bc732017-03-07 19:23:03 -080048#include "error.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000049#include "eval.h"
H. Peter Anvineba20a72002-04-30 20:53:55 +000050#include "labels.h"
H. Peter Anvindc467ba2007-09-24 12:30:54 -070051#include "float.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080052#include "assemble.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000053
H. Peter Anvin76690a12002-04-30 20:52:49 +000054#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000055#define TEMPEXPR_DELTA 8
56
H. Peter Anvine2c80182005-01-15 22:15:51 +000057static scanner scan; /* Address of scanner routine */
H. Peter Anvineba20a72002-04-30 20:53:55 +000058
H. Peter Anvineba20a72002-04-30 20:53:55 +000059static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000060static int ntempexprs;
61static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvine2c80182005-01-15 22:15:51 +000063static expr *tempexpr;
64static int ntempexpr;
65static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000066
H. Peter Anvine2c80182005-01-15 22:15:51 +000067static struct tokenval *tokval; /* The current token */
68static int i; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000069
H. Peter Anvin76690a12002-04-30 20:52:49 +000070static void *scpriv;
H. Peter Anvineba20a72002-04-30 20:53:55 +000071static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000072
73static struct eval_hints *hint;
74
H. Peter Anvin667dd802002-05-26 19:49:41 +000075
H. Peter Anvin76690a12002-04-30 20:52:49 +000076/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000077 * Unimportant cleanup is done to avoid confusing people who are trying
78 * to debug real memory leaks
79 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000080void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000081{
82 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000083 nasm_free(tempexprs[--ntempexprs]);
84 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000085}
86
87/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000088 * Construct a temporary expression.
89 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000090static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000091{
H. Peter Anvin76690a12002-04-30 20:52:49 +000092 tempexpr = NULL;
93 tempexpr_size = ntempexpr = 0;
94}
95
Keith Kaniosb7a89542007-04-12 02:40:54 +000096static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +000097{
H. Peter Anvin76690a12002-04-30 20:52:49 +000098 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000099 tempexpr_size += TEMPEXPR_DELTA;
100 tempexpr = nasm_realloc(tempexpr,
101 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000102 }
103 tempexpr[ntempexpr].type = type;
104 tempexpr[ntempexpr++].value = value;
105}
106
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000108{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000109 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000110 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000111 tempexprs_size += TEMPEXPRS_DELTA;
112 tempexprs = nasm_realloc(tempexprs,
113 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000114 }
115 return tempexprs[ntempexprs++] = tempexpr;
116}
117
118/*
119 * Add two vector datatypes. We have some bizarre behaviour on far-
120 * absolute segment types: we preserve them during addition _only_
121 * if one of the segments is a truly pure scalar.
122 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000124{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000125 int preserve;
126
127 preserve = is_really_simple(p) || is_really_simple(q);
128
129 begintemp();
130
131 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000132 p->type < EXPR_SEGBASE + SEG_ABS &&
133 q->type < EXPR_SEGBASE + SEG_ABS) {
134 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000135
H. Peter Anvine2c80182005-01-15 22:15:51 +0000136 if (p->type > q->type) {
137 addtotemp(q->type, q->value);
138 lasttype = q++->type;
139 } else if (p->type < q->type) {
140 addtotemp(p->type, p->value);
141 lasttype = p++->type;
142 } else { /* *p and *q have same type */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700143 int64_t sum = p->value + q->value;
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800144 if (sum) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000145 addtotemp(p->type, sum);
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800146 if (hint)
147 hint->type = EAH_SUMMED;
148 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 lasttype = p->type;
150 p++, q++;
151 }
152 if (lasttype == EXPR_UNKNOWN) {
153 return finishtemp();
154 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000155 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000156 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
157 addtotemp(p->type, p->value);
158 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000159 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000160 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
161 addtotemp(q->type, q->value);
162 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000163 }
164
165 return finishtemp();
166}
167
168/*
169 * Multiply a vector by a scalar. Strip far-absolute segment part
170 * if present.
171 *
172 * Explicit treatment of UNKNOWN is not required in this routine,
173 * since it will silently do the Right Thing anyway.
174 *
175 * If `affect_hints' is set, we also change the hint type to
176 * NOTBASE if a MAKEBASE hint points at a register being
177 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
178 * as the base register.
179 */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700180static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000181{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000182 expr *p = vect;
183
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
185 p->value = scalar * (p->value);
186 if (hint && hint->type == EAH_MAKEBASE &&
187 p->type == hint->base && affect_hints)
188 hint->type = EAH_NOTBASE;
189 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000190 }
191 p->type = 0;
192
193 return vect;
194}
195
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700196static expr *scalarvect(int64_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000197{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000198 begintemp();
199 addtotemp(EXPR_SIMPLE, scalar);
200 return finishtemp();
201}
202
H. Peter Anvine2c80182005-01-15 22:15:51 +0000203static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000204{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000205 begintemp();
206 addtotemp(EXPR_UNKNOWN, 1L);
207 return finishtemp();
208}
209
210/*
211 * The SEG operator: calculate the segment part of a relocatable
212 * value. Return NULL, as usual, if an error occurs. Report the
213 * error too.
214 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000216{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000217 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000218
219 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000220 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000221
222 if (!is_reloc(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800223 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000224 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000225 }
226
227 seg = reloc_seg(e);
228 if (seg == NO_SEG) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800229 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000230 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000231 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000232 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000233 } else if (seg & 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800234 nasm_error(ERR_NONFATAL, "SEG applied to something which"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000235 " is already a segment base");
236 return NULL;
237 } else {
H. Peter Anvin36034ec2016-02-18 01:18:50 -0800238 int32_t base = ofmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000239
H. Peter Anvine2c80182005-01-15 22:15:51 +0000240 begintemp();
241 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
242 1L);
243 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000244 }
245}
246
247/*
248 * Recursive-descent parser. Called with a single boolean operand,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700249 * which is true if the evaluation is critical (i.e. unresolved
H. Peter Anvin76690a12002-04-30 20:52:49 +0000250 * symbols are an error condition). Must update the global `i' to
251 * reflect the token after the parsed string. May return NULL.
252 *
253 * evaluate() should report its own errors: on return it is assumed
254 * that if NULL has been returned, the error has already been
255 * reported.
256 */
257
258/*
259 * Grammar parsed is:
260 *
261 * expr : bexpr [ WRT expr6 ]
262 * bexpr : rexp0 or expr0 depending on relative-mode setting
263 * rexp0 : rexp1 [ {||} rexp1...]
264 * rexp1 : rexp2 [ {^^} rexp2...]
265 * rexp2 : rexp3 [ {&&} rexp3...]
266 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
267 * expr0 : expr1 [ {|} expr1...]
268 * expr1 : expr2 [ {^} expr2...]
269 * expr2 : expr3 [ {&} expr3...]
270 * expr3 : expr4 [ {<<,>>} expr4...]
271 * expr4 : expr5 [ {+,-} expr5...]
272 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700273 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000274 * | (bexpr)
275 * | symbol
276 * | $
277 * | number
278 */
279
280static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
281
282static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
283static expr *expr4(int), *expr5(int), *expr6(int);
284
H. Peter Anvine2c80182005-01-15 22:15:51 +0000285static expr *(*bexpr) (int);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000286
H. Peter Anvine2c80182005-01-15 22:15:51 +0000287static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000288{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000289 expr *e, *f;
290
291 e = rexp1(critical);
292 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000293 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000294
H. Peter Anvine2c80182005-01-15 22:15:51 +0000295 while (i == TOKEN_DBL_OR) {
296 i = scan(scpriv, tokval);
297 f = rexp1(critical);
298 if (!f)
299 return NULL;
300 if (!(is_simple(e) || is_just_unknown(e)) ||
301 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800302 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303 " scalar values");
304 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000305
H. Peter Anvine2c80182005-01-15 22:15:51 +0000306 if (is_just_unknown(e) || is_just_unknown(f))
307 e = unknown_expr();
308 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700309 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000310 }
311 return e;
312}
313
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000315{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000316 expr *e, *f;
317
318 e = rexp2(critical);
319 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000320 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000321
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322 while (i == TOKEN_DBL_XOR) {
323 i = scan(scpriv, tokval);
324 f = rexp2(critical);
325 if (!f)
326 return NULL;
327 if (!(is_simple(e) || is_just_unknown(e)) ||
328 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800329 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000330 " scalar values");
331 }
332
333 if (is_just_unknown(e) || is_just_unknown(f))
334 e = unknown_expr();
335 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700336 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000337 }
338 return e;
339}
340
H. Peter Anvine2c80182005-01-15 22:15:51 +0000341static expr *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000342{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000343 expr *e, *f;
344
345 e = rexp3(critical);
346 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000347 return NULL;
348 while (i == TOKEN_DBL_AND) {
349 i = scan(scpriv, tokval);
350 f = rexp3(critical);
351 if (!f)
352 return NULL;
353 if (!(is_simple(e) || is_just_unknown(e)) ||
354 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800355 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000356 " scalar values");
357 }
358 if (is_just_unknown(e) || is_just_unknown(f))
359 e = unknown_expr();
360 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700361 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000362 }
363 return e;
364}
365
H. Peter Anvine2c80182005-01-15 22:15:51 +0000366static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000367{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700369 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000370
371 e = expr0(critical);
372 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000373 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000374
H. Peter Anvin76690a12002-04-30 20:52:49 +0000375 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
377 int j = i;
378 i = scan(scpriv, tokval);
379 f = expr0(critical);
380 if (!f)
381 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000382
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700383 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000384
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385 switch (j) {
386 case TOKEN_EQ:
387 case TOKEN_NE:
388 if (is_unknown(e))
389 v = -1; /* means unknown */
390 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700391 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000392 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700393 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000394 break;
395 default:
396 if (is_unknown(e))
397 v = -1; /* means unknown */
398 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800399 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 "`%s': operands differ by a non-scalar",
401 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
402 TOKEN_GE ? ">=" : ">"));
403 v = 0; /* must set it to _something_ */
404 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300405 int64_t vv = reloc_value(e);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 if (vv == 0)
407 v = (j == TOKEN_LE || j == TOKEN_GE);
408 else if (vv > 0)
409 v = (j == TOKEN_GE || j == TOKEN_GT);
410 else /* vv < 0 */
411 v = (j == TOKEN_LE || j == TOKEN_LT);
412 }
413 break;
414 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000415
H. Peter Anvine2c80182005-01-15 22:15:51 +0000416 if (v == -1)
417 e = unknown_expr();
418 else
419 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000420 }
421 return e;
422}
423
H. Peter Anvine2c80182005-01-15 22:15:51 +0000424static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000425{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000426 expr *e, *f;
427
428 e = expr1(critical);
429 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000431
H. Peter Anvine2c80182005-01-15 22:15:51 +0000432 while (i == '|') {
433 i = scan(scpriv, tokval);
434 f = expr1(critical);
435 if (!f)
436 return NULL;
437 if (!(is_simple(e) || is_just_unknown(e)) ||
438 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800439 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000440 " scalar values");
441 }
442 if (is_just_unknown(e) || is_just_unknown(f))
443 e = unknown_expr();
444 else
445 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446 }
447 return e;
448}
449
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000451{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000452 expr *e, *f;
453
454 e = expr2(critical);
455 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000457
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000459 i = scan(scpriv, tokval);
460 f = expr2(critical);
461 if (!f)
462 return NULL;
463 if (!(is_simple(e) || is_just_unknown(e)) ||
464 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800465 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466 " scalar values");
467 }
468 if (is_just_unknown(e) || is_just_unknown(f))
469 e = unknown_expr();
470 else
471 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000472 }
473 return e;
474}
475
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000478 expr *e, *f;
479
480 e = expr3(critical);
481 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000483
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 i = scan(scpriv, tokval);
486 f = expr3(critical);
487 if (!f)
488 return NULL;
489 if (!(is_simple(e) || is_just_unknown(e)) ||
490 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800491 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 " scalar values");
493 }
494 if (is_just_unknown(e) || is_just_unknown(f))
495 e = unknown_expr();
496 else
497 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000498 }
499 return e;
500}
501
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000503{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000504 expr *e, *f;
505
506 e = expr4(critical);
507 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000509
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510 while (i == TOKEN_SHL || i == TOKEN_SHR) {
511 int j = i;
512 i = scan(scpriv, tokval);
513 f = expr4(critical);
514 if (!f)
515 return NULL;
516 if (!(is_simple(e) || is_just_unknown(e)) ||
517 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800518 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 " scalar values");
520 } else if (is_just_unknown(e) || is_just_unknown(f)) {
521 e = unknown_expr();
522 } else
523 switch (j) {
524 case TOKEN_SHL:
525 e = scalarvect(reloc_value(e) << reloc_value(f));
526 break;
527 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700528 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 reloc_value(f));
530 break;
531 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000532 }
533 return e;
534}
535
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000537{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000538 expr *e, *f;
539
540 e = expr5(critical);
541 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 return NULL;
543 while (i == '+' || i == '-') {
544 int j = i;
545 i = scan(scpriv, tokval);
546 f = expr5(critical);
547 if (!f)
548 return NULL;
549 switch (j) {
550 case '+':
551 e = add_vectors(e, f);
552 break;
553 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700554 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 break;
556 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000557 }
558 return e;
559}
560
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000562{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000563 expr *e, *f;
564
565 e = expr6(critical);
566 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000568 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 i == TOKEN_SDIV || i == TOKEN_SMOD) {
570 int j = i;
571 i = scan(scpriv, tokval);
572 f = expr6(critical);
573 if (!f)
574 return NULL;
575 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
576 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800577 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 " scalar values");
579 return NULL;
580 }
581 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800582 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 return NULL;
584 }
585 switch (j) {
586 case '*':
587 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700588 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700590 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 else if (is_just_unknown(e) && is_just_unknown(f))
592 e = unknown_expr();
593 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800594 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 "non-scalar objects");
596 return NULL;
597 }
598 break;
599 case '/':
600 if (is_just_unknown(e) || is_just_unknown(f))
601 e = unknown_expr();
602 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700603 e = scalarvect(((uint64_t)reloc_value(e)) /
604 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 break;
606 case '%':
607 if (is_just_unknown(e) || is_just_unknown(f))
608 e = unknown_expr();
609 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700610 e = scalarvect(((uint64_t)reloc_value(e)) %
611 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612 break;
613 case TOKEN_SDIV:
614 if (is_just_unknown(e) || is_just_unknown(f))
615 e = unknown_expr();
616 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700617 e = scalarvect(((int64_t)reloc_value(e)) /
618 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 break;
620 case TOKEN_SMOD:
621 if (is_just_unknown(e) || is_just_unknown(f))
622 e = unknown_expr();
623 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700624 e = scalarvect(((int64_t)reloc_value(e)) %
625 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 break;
627 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000628 }
629 return e;
630}
631
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700632static expr *eval_floatize(enum floatize type)
633{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300634 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700635 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300636 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700637 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300638 { 1, 0, 1 }, /* FLOAT_8 */
639 { 2, 0, 2 }, /* FLOAT_16 */
640 { 4, 0, 4 }, /* FLOAT_32 */
641 { 8, 0, 8 }, /* FLOAT_64 */
642 { 10, 0, 8 }, /* FLOAT_80M */
643 { 10, 8, 2 }, /* FLOAT_80E */
644 { 16, 0, 8 }, /* FLOAT_128L */
645 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700646 };
647 int sign = 1;
648 int64_t val;
649 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700650
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700651 i = scan(scpriv, tokval);
652 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800653 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300654 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700655 }
656 i = scan(scpriv, tokval);
657 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300658 sign = (i == '-') ? -1 : 1;
659 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700660 }
661 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800662 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300663 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700664 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800665 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300666 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700667 i = scan(scpriv, tokval);
668 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800669 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300670 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700671 }
672
673 p = result+formats[type].start+formats[type].len;
674 val = 0;
675 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300676 p--;
677 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700678 }
679
680 begintemp();
681 addtotemp(EXPR_SIMPLE, val);
682
683 i = scan(scpriv, tokval);
684 return finishtemp();
685}
686
H. Peter Anvin9c749102008-06-14 21:08:38 -0700687static expr *eval_strfunc(enum strfunc type)
688{
689 char *string;
690 size_t string_len;
691 int64_t val;
692 bool parens, rn_warn;
693
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200694 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700695 i = scan(scpriv, tokval);
696 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300697 parens = true;
698 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700699 }
700 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800701 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300702 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700703 }
704 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300705 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700706 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800707 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300708 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700709 }
710
711 val = readstrnum(string, string_len, &rn_warn);
712 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300713 i = scan(scpriv, tokval);
714 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800715 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300716 return NULL;
717 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700718 }
719
720 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800721 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700722
723 begintemp();
724 addtotemp(EXPR_SIMPLE, val);
725
726 i = scan(scpriv, tokval);
727 return finishtemp();
728}
729
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700730static int64_t eval_ifunc(int64_t val, enum ifunc func)
731{
732 int errtype;
733 uint64_t uval = (uint64_t)val;
734 int64_t rv;
735
736 switch (func) {
737 case IFUNC_ILOG2E:
738 case IFUNC_ILOG2W:
739 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400740
741 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800742 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700743 /* fall through */
744 case IFUNC_ILOG2F:
745 rv = ilog2_64(uval);
746 break;
747
748 case IFUNC_ILOG2C:
749 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
750 break;
751
752 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800753 nasm_panic(0, "invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700754 rv = 0;
755 break;
756 }
757
758 return rv;
759}
760
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000762{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000763 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000764 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800765 int32_t label_seg;
766 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700767 int64_t tmpval;
768 bool rn_warn;
Charles Crayned60059e2008-03-12 22:39:03 -0700769 char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000770
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700771 switch (i) {
772 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 i = scan(scpriv, tokval);
774 e = expr6(critical);
775 if (!e)
776 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700777 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700778
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700779 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000780 i = scan(scpriv, tokval);
781 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700782
783 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 i = scan(scpriv, tokval);
785 e = expr6(critical);
786 if (!e)
787 return NULL;
788 if (is_just_unknown(e))
789 return unknown_expr();
790 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800791 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 " scalar values");
793 return NULL;
794 }
795 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700796
797 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000798 i = scan(scpriv, tokval);
799 e = expr6(critical);
800 if (!e)
801 return NULL;
802 if (is_just_unknown(e))
803 return unknown_expr();
804 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800805 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000806 " scalar values");
807 return NULL;
808 }
809 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700810
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700811 case TOKEN_IFUNC:
812 {
813 enum ifunc func = tokval->t_integer;
814 i = scan(scpriv, tokval);
815 e = expr6(critical);
816 if (!e)
817 return NULL;
818 if (is_just_unknown(e))
819 return unknown_expr();
820 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800821 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700822 " scalar values");
823 return NULL;
824 }
825 return scalarvect(eval_ifunc(reloc_value(e), func));
826 }
827
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700828 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 i = scan(scpriv, tokval);
830 e = expr6(critical);
831 if (!e)
832 return NULL;
833 e = segment_part(e);
834 if (!e)
835 return NULL;
836 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800837 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 return NULL;
839 }
840 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700841
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700842 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300843 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700844
H. Peter Anvin9c749102008-06-14 21:08:38 -0700845 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300846 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700847
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700848 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 i = scan(scpriv, tokval);
850 e = bexpr(critical);
851 if (!e)
852 return NULL;
853 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800854 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 return NULL;
856 }
857 i = scan(scpriv, tokval);
858 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700859
860 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700861 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700862 case TOKEN_REG:
863 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300864 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700865 case TOKEN_HERE:
866 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700867 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 begintemp();
869 switch (i) {
870 case TOKEN_NUM:
871 addtotemp(EXPR_SIMPLE, tokval->t_integer);
872 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300873 case TOKEN_STR:
874 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
875 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800876 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700877 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300878 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000879 case TOKEN_REG:
880 addtotemp(tokval->t_integer, 1L);
881 if (hint && hint->type == EAH_NOHINT)
882 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
883 break;
884 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300885 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 case TOKEN_HERE:
887 case TOKEN_BASE:
888 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800889 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 * symbol, Here or Base references are valid because we
891 * are in preprocess-only mode.
892 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800893 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800894 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300896 (i == TOKEN_HERE ? "`$'" :
897 i == TOKEN_BASE ? "`$$'" :
898 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 addtotemp(EXPR_UNKNOWN, 1L);
900 break;
901 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000902
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
904 if (i == TOKEN_BASE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800905 label_seg = in_absolute ? absolute.segment : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 label_ofs = 0;
907 } else if (i == TOKEN_HERE) {
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800908 label_seg = in_absolute ? absolute.segment : location.segment;
909 label_ofs = in_absolute ? absolute.offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800911 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700912 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800914 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700915 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 return NULL;
917 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800918 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700919 "symbol `%s%s' not defined before use",
920 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 return NULL;
922 } else {
923 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400924 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 type = EXPR_UNKNOWN;
926 label_seg = NO_SEG;
927 label_ofs = 1;
928 }
929 }
930 if (opflags && is_extern(tokval->t_charptr))
931 *opflags |= OPFLAG_EXTERN;
932 }
933 addtotemp(type, label_ofs);
934 if (label_seg != NO_SEG)
935 addtotemp(EXPR_SEGBASE + label_seg, 1L);
936 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700937 case TOKEN_DECORATOR:
938 addtotemp(EXPR_RDSAE, tokval->t_integer);
939 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 }
941 i = scan(scpriv, tokval);
942 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700943
944 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800945 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000947 }
948}
949
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800951 int *fwref, int critical, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000952{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000953 expr *e;
954 expr *f = NULL;
955
956 hint = hints;
957 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000959
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000960 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 critical &= ~CRITICAL;
962 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000963 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000965
966 scan = sc;
967 scpriv = scprivate;
968 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000969 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000970
971 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000973 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000975
Keith Kaniosb7a89542007-04-12 02:40:54 +0000976 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000978
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000980 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000982
983 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 i = scan(scpriv, tokval); /* eat the WRT */
985 f = expr6(critical);
986 if (!f)
987 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000988 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700989 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000990 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 expr *g;
992 if (is_just_unknown(f))
993 g = unknown_expr();
994 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000995 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 begintemp();
997 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800998 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 return NULL;
1000 }
1001 value = reloc_seg(f);
1002 if (value == NO_SEG)
1003 value = reloc_value(f) | SEG_ABS;
1004 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001005 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 return NULL;
1007 }
1008 addtotemp(EXPR_WRT, value);
1009 g = finishtemp();
1010 }
1011 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001012 }
1013 return e;
1014}