blob: 029d8a045b7bb11b295758a20db7f30343cc8079 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvincd7893d2016-02-18 01:25:46 -08003 * Copyright 1996-2016 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"
48#include "eval.h"
H. Peter Anvineba20a72002-04-30 20:53:55 +000049#include "labels.h"
H. Peter Anvindc467ba2007-09-24 12:30:54 -070050#include "float.h"
H. Peter Anvin76690a12002-04-30 20:52:49 +000051
H. Peter Anvin76690a12002-04-30 20:52:49 +000052#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000053#define TEMPEXPR_DELTA 8
54
H. Peter Anvine2c80182005-01-15 22:15:51 +000055static scanner scan; /* Address of scanner routine */
H. Peter Anvineba20a72002-04-30 20:53:55 +000056
H. Peter Anvineba20a72002-04-30 20:53:55 +000057static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000058static int ntempexprs;
59static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000060
H. Peter Anvine2c80182005-01-15 22:15:51 +000061static expr *tempexpr;
62static int ntempexpr;
63static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000064
H. Peter Anvine2c80182005-01-15 22:15:51 +000065static struct tokenval *tokval; /* The current token */
66static int i; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000067
H. Peter Anvin76690a12002-04-30 20:52:49 +000068static void *scpriv;
H. Peter Anvineba20a72002-04-30 20:53:55 +000069static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000070
71static struct eval_hints *hint;
72
H. Peter Anvine2c80182005-01-15 22:15:51 +000073extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030074extern int32_t abs_seg; /* ABSOLUTE segment */
75extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvin667dd802002-05-26 19:49:41 +000076
H. Peter Anvin76690a12002-04-30 20:52:49 +000077/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000078 * Unimportant cleanup is done to avoid confusing people who are trying
79 * to debug real memory leaks
80 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000081void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000082{
83 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000084 nasm_free(tempexprs[--ntempexprs]);
85 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000086}
87
88/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000089 * Construct a temporary expression.
90 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000091static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000092{
H. Peter Anvin76690a12002-04-30 20:52:49 +000093 tempexpr = NULL;
94 tempexpr_size = ntempexpr = 0;
95}
96
Keith Kaniosb7a89542007-04-12 02:40:54 +000097static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +000098{
H. Peter Anvin76690a12002-04-30 20:52:49 +000099 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000100 tempexpr_size += TEMPEXPR_DELTA;
101 tempexpr = nasm_realloc(tempexpr,
102 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000103 }
104 tempexpr[ntempexpr].type = type;
105 tempexpr[ntempexpr++].value = value;
106}
107
H. Peter Anvine2c80182005-01-15 22:15:51 +0000108static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000109{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000110 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000111 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000112 tempexprs_size += TEMPEXPRS_DELTA;
113 tempexprs = nasm_realloc(tempexprs,
114 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000115 }
116 return tempexprs[ntempexprs++] = tempexpr;
117}
118
119/*
120 * Add two vector datatypes. We have some bizarre behaviour on far-
121 * absolute segment types: we preserve them during addition _only_
122 * if one of the segments is a truly pure scalar.
123 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000125{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000126 int preserve;
127
128 preserve = is_really_simple(p) || is_really_simple(q);
129
130 begintemp();
131
132 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000133 p->type < EXPR_SEGBASE + SEG_ABS &&
134 q->type < EXPR_SEGBASE + SEG_ABS) {
135 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000136
H. Peter Anvine2c80182005-01-15 22:15:51 +0000137 if (p->type > q->type) {
138 addtotemp(q->type, q->value);
139 lasttype = q++->type;
140 } else if (p->type < q->type) {
141 addtotemp(p->type, p->value);
142 lasttype = p++->type;
143 } else { /* *p and *q have same type */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700144 int64_t sum = p->value + q->value;
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800145 if (sum) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000146 addtotemp(p->type, sum);
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800147 if (hint)
148 hint->type = EAH_SUMMED;
149 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000150 lasttype = p->type;
151 p++, q++;
152 }
153 if (lasttype == EXPR_UNKNOWN) {
154 return finishtemp();
155 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000156 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000157 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
158 addtotemp(p->type, p->value);
159 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000160 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000161 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
162 addtotemp(q->type, q->value);
163 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000164 }
165
166 return finishtemp();
167}
168
169/*
170 * Multiply a vector by a scalar. Strip far-absolute segment part
171 * if present.
172 *
173 * Explicit treatment of UNKNOWN is not required in this routine,
174 * since it will silently do the Right Thing anyway.
175 *
176 * If `affect_hints' is set, we also change the hint type to
177 * NOTBASE if a MAKEBASE hint points at a register being
178 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
179 * as the base register.
180 */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700181static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000182{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000183 expr *p = vect;
184
H. Peter Anvine2c80182005-01-15 22:15:51 +0000185 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
186 p->value = scalar * (p->value);
187 if (hint && hint->type == EAH_MAKEBASE &&
188 p->type == hint->base && affect_hints)
189 hint->type = EAH_NOTBASE;
190 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000191 }
192 p->type = 0;
193
194 return vect;
195}
196
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700197static expr *scalarvect(int64_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000198{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000199 begintemp();
200 addtotemp(EXPR_SIMPLE, scalar);
201 return finishtemp();
202}
203
H. Peter Anvine2c80182005-01-15 22:15:51 +0000204static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000205{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000206 begintemp();
207 addtotemp(EXPR_UNKNOWN, 1L);
208 return finishtemp();
209}
210
211/*
212 * The SEG operator: calculate the segment part of a relocatable
213 * value. Return NULL, as usual, if an error occurs. Report the
214 * error too.
215 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000216static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000217{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000218 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000219
220 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000221 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000222
223 if (!is_reloc(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800224 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000225 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000226 }
227
228 seg = reloc_seg(e);
229 if (seg == NO_SEG) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800230 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000231 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000232 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000233 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000234 } else if (seg & 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800235 nasm_error(ERR_NONFATAL, "SEG applied to something which"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236 " is already a segment base");
237 return NULL;
238 } else {
H. Peter Anvin36034ec2016-02-18 01:18:50 -0800239 int32_t base = ofmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000240
H. Peter Anvine2c80182005-01-15 22:15:51 +0000241 begintemp();
242 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
243 1L);
244 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000245 }
246}
247
248/*
249 * Recursive-descent parser. Called with a single boolean operand,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700250 * which is true if the evaluation is critical (i.e. unresolved
H. Peter Anvin76690a12002-04-30 20:52:49 +0000251 * symbols are an error condition). Must update the global `i' to
252 * reflect the token after the parsed string. May return NULL.
253 *
254 * evaluate() should report its own errors: on return it is assumed
255 * that if NULL has been returned, the error has already been
256 * reported.
257 */
258
259/*
260 * Grammar parsed is:
261 *
262 * expr : bexpr [ WRT expr6 ]
263 * bexpr : rexp0 or expr0 depending on relative-mode setting
264 * rexp0 : rexp1 [ {||} rexp1...]
265 * rexp1 : rexp2 [ {^^} rexp2...]
266 * rexp2 : rexp3 [ {&&} rexp3...]
267 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
268 * expr0 : expr1 [ {|} expr1...]
269 * expr1 : expr2 [ {^} expr2...]
270 * expr2 : expr3 [ {&} expr3...]
271 * expr3 : expr4 [ {<<,>>} expr4...]
272 * expr4 : expr5 [ {+,-} expr5...]
273 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700274 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000275 * | (bexpr)
276 * | symbol
277 * | $
278 * | number
279 */
280
281static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
282
283static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
284static expr *expr4(int), *expr5(int), *expr6(int);
285
H. Peter Anvine2c80182005-01-15 22:15:51 +0000286static expr *(*bexpr) (int);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000287
H. Peter Anvine2c80182005-01-15 22:15:51 +0000288static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000289{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000290 expr *e, *f;
291
292 e = rexp1(critical);
293 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000294 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000295
H. Peter Anvine2c80182005-01-15 22:15:51 +0000296 while (i == TOKEN_DBL_OR) {
297 i = scan(scpriv, tokval);
298 f = rexp1(critical);
299 if (!f)
300 return NULL;
301 if (!(is_simple(e) || is_just_unknown(e)) ||
302 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800303 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000304 " scalar values");
305 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000306
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307 if (is_just_unknown(e) || is_just_unknown(f))
308 e = unknown_expr();
309 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700310 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000311 }
312 return e;
313}
314
H. Peter Anvine2c80182005-01-15 22:15:51 +0000315static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000316{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000317 expr *e, *f;
318
319 e = rexp2(critical);
320 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000321 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000322
H. Peter Anvine2c80182005-01-15 22:15:51 +0000323 while (i == TOKEN_DBL_XOR) {
324 i = scan(scpriv, tokval);
325 f = rexp2(critical);
326 if (!f)
327 return NULL;
328 if (!(is_simple(e) || is_just_unknown(e)) ||
329 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800330 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000331 " scalar values");
332 }
333
334 if (is_just_unknown(e) || is_just_unknown(f))
335 e = unknown_expr();
336 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700337 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000338 }
339 return e;
340}
341
H. Peter Anvine2c80182005-01-15 22:15:51 +0000342static expr *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000344 expr *e, *f;
345
346 e = rexp3(critical);
347 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000348 return NULL;
349 while (i == TOKEN_DBL_AND) {
350 i = scan(scpriv, tokval);
351 f = rexp3(critical);
352 if (!f)
353 return NULL;
354 if (!(is_simple(e) || is_just_unknown(e)) ||
355 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800356 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000357 " scalar values");
358 }
359 if (is_just_unknown(e) || is_just_unknown(f))
360 e = unknown_expr();
361 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700362 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000363 }
364 return e;
365}
366
H. Peter Anvine2c80182005-01-15 22:15:51 +0000367static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000368{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000369 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700370 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000371
372 e = expr0(critical);
373 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000374 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000375
H. Peter Anvin76690a12002-04-30 20:52:49 +0000376 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
378 int j = i;
379 i = scan(scpriv, tokval);
380 f = expr0(critical);
381 if (!f)
382 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000383
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700384 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000385
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386 switch (j) {
387 case TOKEN_EQ:
388 case TOKEN_NE:
389 if (is_unknown(e))
390 v = -1; /* means unknown */
391 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700392 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000393 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700394 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000395 break;
396 default:
397 if (is_unknown(e))
398 v = -1; /* means unknown */
399 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800400 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000401 "`%s': operands differ by a non-scalar",
402 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
403 TOKEN_GE ? ">=" : ">"));
404 v = 0; /* must set it to _something_ */
405 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300406 int64_t vv = reloc_value(e);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000407 if (vv == 0)
408 v = (j == TOKEN_LE || j == TOKEN_GE);
409 else if (vv > 0)
410 v = (j == TOKEN_GE || j == TOKEN_GT);
411 else /* vv < 0 */
412 v = (j == TOKEN_LE || j == TOKEN_LT);
413 }
414 break;
415 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000416
H. Peter Anvine2c80182005-01-15 22:15:51 +0000417 if (v == -1)
418 e = unknown_expr();
419 else
420 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000421 }
422 return e;
423}
424
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000426{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000427 expr *e, *f;
428
429 e = expr1(critical);
430 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000431 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000432
H. Peter Anvine2c80182005-01-15 22:15:51 +0000433 while (i == '|') {
434 i = scan(scpriv, tokval);
435 f = expr1(critical);
436 if (!f)
437 return NULL;
438 if (!(is_simple(e) || is_just_unknown(e)) ||
439 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800440 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000441 " scalar values");
442 }
443 if (is_just_unknown(e) || is_just_unknown(f))
444 e = unknown_expr();
445 else
446 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000447 }
448 return e;
449}
450
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000452{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000453 expr *e, *f;
454
455 e = expr2(critical);
456 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000457 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000458
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 i = scan(scpriv, tokval);
461 f = expr2(critical);
462 if (!f)
463 return NULL;
464 if (!(is_simple(e) || is_just_unknown(e)) ||
465 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800466 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000467 " scalar values");
468 }
469 if (is_just_unknown(e) || is_just_unknown(f))
470 e = unknown_expr();
471 else
472 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000473 }
474 return e;
475}
476
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000478{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000479 expr *e, *f;
480
481 e = expr3(critical);
482 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000484
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 i = scan(scpriv, tokval);
487 f = expr3(critical);
488 if (!f)
489 return NULL;
490 if (!(is_simple(e) || is_just_unknown(e)) ||
491 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800492 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000493 " scalar values");
494 }
495 if (is_just_unknown(e) || is_just_unknown(f))
496 e = unknown_expr();
497 else
498 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000499 }
500 return e;
501}
502
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000505 expr *e, *f;
506
507 e = expr4(critical);
508 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000509 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000510
H. Peter Anvine2c80182005-01-15 22:15:51 +0000511 while (i == TOKEN_SHL || i == TOKEN_SHR) {
512 int j = i;
513 i = scan(scpriv, tokval);
514 f = expr4(critical);
515 if (!f)
516 return NULL;
517 if (!(is_simple(e) || is_just_unknown(e)) ||
518 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800519 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 " scalar values");
521 } else if (is_just_unknown(e) || is_just_unknown(f)) {
522 e = unknown_expr();
523 } else
524 switch (j) {
525 case TOKEN_SHL:
526 e = scalarvect(reloc_value(e) << reloc_value(f));
527 break;
528 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700529 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530 reloc_value(f));
531 break;
532 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000533 }
534 return e;
535}
536
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000538{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000539 expr *e, *f;
540
541 e = expr5(critical);
542 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 return NULL;
544 while (i == '+' || i == '-') {
545 int j = i;
546 i = scan(scpriv, tokval);
547 f = expr5(critical);
548 if (!f)
549 return NULL;
550 switch (j) {
551 case '+':
552 e = add_vectors(e, f);
553 break;
554 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700555 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 break;
557 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000558 }
559 return e;
560}
561
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000563{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000564 expr *e, *f;
565
566 e = expr6(critical);
567 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000569 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 i == TOKEN_SDIV || i == TOKEN_SMOD) {
571 int j = i;
572 i = scan(scpriv, tokval);
573 f = expr6(critical);
574 if (!f)
575 return NULL;
576 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
577 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800578 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 " scalar values");
580 return NULL;
581 }
582 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800583 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 return NULL;
585 }
586 switch (j) {
587 case '*':
588 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700589 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700591 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 else if (is_just_unknown(e) && is_just_unknown(f))
593 e = unknown_expr();
594 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800595 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 "non-scalar objects");
597 return NULL;
598 }
599 break;
600 case '/':
601 if (is_just_unknown(e) || is_just_unknown(f))
602 e = unknown_expr();
603 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700604 e = scalarvect(((uint64_t)reloc_value(e)) /
605 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 break;
607 case '%':
608 if (is_just_unknown(e) || is_just_unknown(f))
609 e = unknown_expr();
610 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700611 e = scalarvect(((uint64_t)reloc_value(e)) %
612 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 break;
614 case TOKEN_SDIV:
615 if (is_just_unknown(e) || is_just_unknown(f))
616 e = unknown_expr();
617 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700618 e = scalarvect(((int64_t)reloc_value(e)) /
619 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 break;
621 case TOKEN_SMOD:
622 if (is_just_unknown(e) || is_just_unknown(f))
623 e = unknown_expr();
624 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700625 e = scalarvect(((int64_t)reloc_value(e)) %
626 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 break;
628 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000629 }
630 return e;
631}
632
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700633static expr *eval_floatize(enum floatize type)
634{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300635 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700636 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300637 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700638 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300639 { 1, 0, 1 }, /* FLOAT_8 */
640 { 2, 0, 2 }, /* FLOAT_16 */
641 { 4, 0, 4 }, /* FLOAT_32 */
642 { 8, 0, 8 }, /* FLOAT_64 */
643 { 10, 0, 8 }, /* FLOAT_80M */
644 { 10, 8, 2 }, /* FLOAT_80E */
645 { 16, 0, 8 }, /* FLOAT_128L */
646 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700647 };
648 int sign = 1;
649 int64_t val;
650 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700651
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700652 i = scan(scpriv, tokval);
653 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800654 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300655 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700656 }
657 i = scan(scpriv, tokval);
658 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300659 sign = (i == '-') ? -1 : 1;
660 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700661 }
662 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800663 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300664 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700665 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800666 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300667 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700668 i = scan(scpriv, tokval);
669 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800670 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300671 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700672 }
673
674 p = result+formats[type].start+formats[type].len;
675 val = 0;
676 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300677 p--;
678 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700679 }
680
681 begintemp();
682 addtotemp(EXPR_SIMPLE, val);
683
684 i = scan(scpriv, tokval);
685 return finishtemp();
686}
687
H. Peter Anvin9c749102008-06-14 21:08:38 -0700688static expr *eval_strfunc(enum strfunc type)
689{
690 char *string;
691 size_t string_len;
692 int64_t val;
693 bool parens, rn_warn;
694
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200695 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700696 i = scan(scpriv, tokval);
697 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300698 parens = true;
699 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700700 }
701 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800702 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300703 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700704 }
705 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300706 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700707 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800708 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300709 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700710 }
711
712 val = readstrnum(string, string_len, &rn_warn);
713 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300714 i = scan(scpriv, tokval);
715 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800716 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300717 return NULL;
718 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700719 }
720
721 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800722 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700723
724 begintemp();
725 addtotemp(EXPR_SIMPLE, val);
726
727 i = scan(scpriv, tokval);
728 return finishtemp();
729}
730
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700731static int64_t eval_ifunc(int64_t val, enum ifunc func)
732{
733 int errtype;
734 uint64_t uval = (uint64_t)val;
735 int64_t rv;
736
737 switch (func) {
738 case IFUNC_ILOG2E:
739 case IFUNC_ILOG2W:
740 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400741
742 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800743 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700744 /* fall through */
745 case IFUNC_ILOG2F:
746 rv = ilog2_64(uval);
747 break;
748
749 case IFUNC_ILOG2C:
750 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
751 break;
752
753 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800754 nasm_panic(0, "invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700755 rv = 0;
756 break;
757 }
758
759 return rv;
760}
761
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000763{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000764 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000765 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800766 int32_t label_seg;
767 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700768 int64_t tmpval;
769 bool rn_warn;
Charles Crayned60059e2008-03-12 22:39:03 -0700770 char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000771
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700772 switch (i) {
773 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000774 i = scan(scpriv, tokval);
775 e = expr6(critical);
776 if (!e)
777 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700778 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700779
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700780 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 i = scan(scpriv, tokval);
782 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700783
784 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 i = scan(scpriv, tokval);
786 e = expr6(critical);
787 if (!e)
788 return NULL;
789 if (is_just_unknown(e))
790 return unknown_expr();
791 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800792 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 " scalar values");
794 return NULL;
795 }
796 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700797
798 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000799 i = scan(scpriv, tokval);
800 e = expr6(critical);
801 if (!e)
802 return NULL;
803 if (is_just_unknown(e))
804 return unknown_expr();
805 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800806 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000807 " scalar values");
808 return NULL;
809 }
810 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700811
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700812 case TOKEN_IFUNC:
813 {
814 enum ifunc func = tokval->t_integer;
815 i = scan(scpriv, tokval);
816 e = expr6(critical);
817 if (!e)
818 return NULL;
819 if (is_just_unknown(e))
820 return unknown_expr();
821 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800822 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700823 " scalar values");
824 return NULL;
825 }
826 return scalarvect(eval_ifunc(reloc_value(e), func));
827 }
828
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700829 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 i = scan(scpriv, tokval);
831 e = expr6(critical);
832 if (!e)
833 return NULL;
834 e = segment_part(e);
835 if (!e)
836 return NULL;
837 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800838 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 return NULL;
840 }
841 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700842
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700843 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300844 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700845
H. Peter Anvin9c749102008-06-14 21:08:38 -0700846 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300847 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700848
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700849 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 i = scan(scpriv, tokval);
851 e = bexpr(critical);
852 if (!e)
853 return NULL;
854 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800855 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 return NULL;
857 }
858 i = scan(scpriv, tokval);
859 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700860
861 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700862 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700863 case TOKEN_REG:
864 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300865 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700866 case TOKEN_HERE:
867 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700868 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 begintemp();
870 switch (i) {
871 case TOKEN_NUM:
872 addtotemp(EXPR_SIMPLE, tokval->t_integer);
873 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300874 case TOKEN_STR:
875 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
876 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800877 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700878 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300879 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 case TOKEN_REG:
881 addtotemp(tokval->t_integer, 1L);
882 if (hint && hint->type == EAH_NOHINT)
883 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
884 break;
885 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300886 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000887 case TOKEN_HERE:
888 case TOKEN_BASE:
889 /*
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800890 * If !location.known, this indicates that no
H. Peter Anvine2c80182005-01-15 22:15:51 +0000891 * symbol, Here or Base references are valid because we
892 * are in preprocess-only mode.
893 */
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800894 if (!location.known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800895 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300897 (i == TOKEN_HERE ? "`$'" :
898 i == TOKEN_BASE ? "`$$'" :
899 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 addtotemp(EXPR_UNKNOWN, 1L);
901 break;
902 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000903
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
905 if (i == TOKEN_BASE) {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800906 label_seg = in_abs_seg ? abs_seg : location.segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 label_ofs = 0;
908 } else if (i == TOKEN_HERE) {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800909 label_seg = in_abs_seg ? abs_seg : location.segment;
910 label_ofs = in_abs_seg ? abs_offset : location.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000911 } else {
H. Peter Anvincd7893d2016-02-18 01:25:46 -0800912 if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700913 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800915 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700916 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 return NULL;
918 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800919 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700920 "symbol `%s%s' not defined before use",
921 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 return NULL;
923 } else {
924 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400925 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 type = EXPR_UNKNOWN;
927 label_seg = NO_SEG;
928 label_ofs = 1;
929 }
930 }
931 if (opflags && is_extern(tokval->t_charptr))
932 *opflags |= OPFLAG_EXTERN;
933 }
934 addtotemp(type, label_ofs);
935 if (label_seg != NO_SEG)
936 addtotemp(EXPR_SEGBASE + label_seg, 1L);
937 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700938 case TOKEN_DECORATOR:
939 addtotemp(EXPR_RDSAE, tokval->t_integer);
940 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 }
942 i = scan(scpriv, tokval);
943 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700944
945 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800946 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000948 }
949}
950
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800952 int *fwref, int critical, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000953{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000954 expr *e;
955 expr *f = NULL;
956
957 hint = hints;
958 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000960
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000961 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 critical &= ~CRITICAL;
963 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000964 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000966
967 scan = sc;
968 scpriv = scprivate;
969 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000970 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000971
972 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000974 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000976
Keith Kaniosb7a89542007-04-12 02:40:54 +0000977 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000979
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000981 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000983
984 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 i = scan(scpriv, tokval); /* eat the WRT */
986 f = expr6(critical);
987 if (!f)
988 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000989 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700990 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000991 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 expr *g;
993 if (is_just_unknown(f))
994 g = unknown_expr();
995 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000996 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 begintemp();
998 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800999 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 return NULL;
1001 }
1002 value = reloc_seg(f);
1003 if (value == NO_SEG)
1004 value = reloc_value(f) | SEG_ABS;
1005 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001006 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 return NULL;
1008 }
1009 addtotemp(EXPR_WRT, value);
1010 g = finishtemp();
1011 }
1012 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001013 }
1014 return e;
1015}