blob: 170993363b11beea1066255bf305d7e7175cdc10 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +03002 *
H. Peter Anvin290b4cb2012-05-31 10:25:37 -07003 * Copyright 1996-2012 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>
Keith Kaniosb7a89542007-04-12 02:40:54 +000045#include <inttypes.h>
H. Peter Anvin76690a12002-04-30 20:52:49 +000046
47#include "nasm.h"
48#include "nasmlib.h"
49#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 Anvin76690a12002-04-30 20:52:49 +000052
H. Peter Anvin76690a12002-04-30 20:52:49 +000053#define TEMPEXPRS_DELTA 128
H. Peter Anvin76690a12002-04-30 20:52:49 +000054#define TEMPEXPR_DELTA 8
55
H. Peter Anvine2c80182005-01-15 22:15:51 +000056static scanner scan; /* Address of scanner routine */
H. Peter Anvine2c80182005-01-15 22:15:51 +000057static lfunc labelfunc; /* Address of label 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 Anvin12e46512007-10-03 21:30:57 -070071static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000072static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000073
74static struct eval_hints *hint;
75
H. Peter Anvine2c80182005-01-15 22:15:51 +000076extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030077extern int32_t abs_seg; /* ABSOLUTE segment */
78extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvin667dd802002-05-26 19:49:41 +000079
H. Peter Anvin76690a12002-04-30 20:52:49 +000080/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000081 * Unimportant cleanup is done to avoid confusing people who are trying
82 * to debug real memory leaks
83 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000084void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000085{
86 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000087 nasm_free(tempexprs[--ntempexprs]);
88 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000089}
90
91/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000092 * Construct a temporary expression.
93 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000094static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000095{
H. Peter Anvin76690a12002-04-30 20:52:49 +000096 tempexpr = NULL;
97 tempexpr_size = ntempexpr = 0;
98}
99
Keith Kaniosb7a89542007-04-12 02:40:54 +0000100static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000101{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000102 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000103 tempexpr_size += TEMPEXPR_DELTA;
104 tempexpr = nasm_realloc(tempexpr,
105 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000106 }
107 tempexpr[ntempexpr].type = type;
108 tempexpr[ntempexpr++].value = value;
109}
110
H. Peter Anvine2c80182005-01-15 22:15:51 +0000111static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000112{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000113 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000114 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000115 tempexprs_size += TEMPEXPRS_DELTA;
116 tempexprs = nasm_realloc(tempexprs,
117 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000118 }
119 return tempexprs[ntempexprs++] = tempexpr;
120}
121
122/*
123 * Add two vector datatypes. We have some bizarre behaviour on far-
124 * absolute segment types: we preserve them during addition _only_
125 * if one of the segments is a truly pure scalar.
126 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000127static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000128{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000129 int preserve;
130
131 preserve = is_really_simple(p) || is_really_simple(q);
132
133 begintemp();
134
135 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000136 p->type < EXPR_SEGBASE + SEG_ABS &&
137 q->type < EXPR_SEGBASE + SEG_ABS) {
138 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000139
H. Peter Anvine2c80182005-01-15 22:15:51 +0000140 if (p->type > q->type) {
141 addtotemp(q->type, q->value);
142 lasttype = q++->type;
143 } else if (p->type < q->type) {
144 addtotemp(p->type, p->value);
145 lasttype = p++->type;
146 } else { /* *p and *q have same type */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700147 int64_t sum = p->value + q->value;
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800148 if (sum) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000149 addtotemp(p->type, sum);
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800150 if (hint)
151 hint->type = EAH_SUMMED;
152 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000153 lasttype = p->type;
154 p++, q++;
155 }
156 if (lasttype == EXPR_UNKNOWN) {
157 return finishtemp();
158 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000159 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000160 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
161 addtotemp(p->type, p->value);
162 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000163 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000164 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
165 addtotemp(q->type, q->value);
166 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000167 }
168
169 return finishtemp();
170}
171
172/*
173 * Multiply a vector by a scalar. Strip far-absolute segment part
174 * if present.
175 *
176 * Explicit treatment of UNKNOWN is not required in this routine,
177 * since it will silently do the Right Thing anyway.
178 *
179 * If `affect_hints' is set, we also change the hint type to
180 * NOTBASE if a MAKEBASE hint points at a register being
181 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
182 * as the base register.
183 */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700184static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000185{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000186 expr *p = vect;
187
H. Peter Anvine2c80182005-01-15 22:15:51 +0000188 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
189 p->value = scalar * (p->value);
190 if (hint && hint->type == EAH_MAKEBASE &&
191 p->type == hint->base && affect_hints)
192 hint->type = EAH_NOTBASE;
193 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000194 }
195 p->type = 0;
196
197 return vect;
198}
199
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700200static expr *scalarvect(int64_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000201{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000202 begintemp();
203 addtotemp(EXPR_SIMPLE, scalar);
204 return finishtemp();
205}
206
H. Peter Anvine2c80182005-01-15 22:15:51 +0000207static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000208{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000209 begintemp();
210 addtotemp(EXPR_UNKNOWN, 1L);
211 return finishtemp();
212}
213
214/*
215 * The SEG operator: calculate the segment part of a relocatable
216 * value. Return NULL, as usual, if an error occurs. Report the
217 * error too.
218 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000220{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000221 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000222
223 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000224 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000225
226 if (!is_reloc(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800227 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000228 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000229 }
230
231 seg = reloc_seg(e);
232 if (seg == NO_SEG) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800233 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000234 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000235 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000237 } else if (seg & 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800238 nasm_error(ERR_NONFATAL, "SEG applied to something which"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000239 " is already a segment base");
240 return NULL;
241 } else {
H. Peter Anvin36034ec2016-02-18 01:18:50 -0800242 int32_t base = ofmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000243
H. Peter Anvine2c80182005-01-15 22:15:51 +0000244 begintemp();
245 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
246 1L);
247 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000248 }
249}
250
251/*
252 * Recursive-descent parser. Called with a single boolean operand,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700253 * which is true if the evaluation is critical (i.e. unresolved
H. Peter Anvin76690a12002-04-30 20:52:49 +0000254 * symbols are an error condition). Must update the global `i' to
255 * reflect the token after the parsed string. May return NULL.
256 *
257 * evaluate() should report its own errors: on return it is assumed
258 * that if NULL has been returned, the error has already been
259 * reported.
260 */
261
262/*
263 * Grammar parsed is:
264 *
265 * expr : bexpr [ WRT expr6 ]
266 * bexpr : rexp0 or expr0 depending on relative-mode setting
267 * rexp0 : rexp1 [ {||} rexp1...]
268 * rexp1 : rexp2 [ {^^} rexp2...]
269 * rexp2 : rexp3 [ {&&} rexp3...]
270 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
271 * expr0 : expr1 [ {|} expr1...]
272 * expr1 : expr2 [ {^} expr2...]
273 * expr2 : expr3 [ {&} expr3...]
274 * expr3 : expr4 [ {<<,>>} expr4...]
275 * 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
284static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
285
286static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
287static expr *expr4(int), *expr5(int), *expr6(int);
288
H. Peter Anvine2c80182005-01-15 22:15:51 +0000289static expr *(*bexpr) (int);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000290
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000292{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000293 expr *e, *f;
294
295 e = rexp1(critical);
296 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000297 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000298
H. Peter Anvine2c80182005-01-15 22:15:51 +0000299 while (i == TOKEN_DBL_OR) {
300 i = scan(scpriv, tokval);
301 f = rexp1(critical);
302 if (!f)
303 return NULL;
304 if (!(is_simple(e) || is_just_unknown(e)) ||
305 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800306 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000307 " scalar values");
308 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000309
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310 if (is_just_unknown(e) || is_just_unknown(f))
311 e = unknown_expr();
312 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700313 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000314 }
315 return e;
316}
317
H. Peter Anvine2c80182005-01-15 22:15:51 +0000318static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000319{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000320 expr *e, *f;
321
322 e = rexp2(critical);
323 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000324 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000325
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326 while (i == TOKEN_DBL_XOR) {
327 i = scan(scpriv, tokval);
328 f = rexp2(critical);
329 if (!f)
330 return NULL;
331 if (!(is_simple(e) || is_just_unknown(e)) ||
332 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800333 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000334 " scalar values");
335 }
336
337 if (is_just_unknown(e) || is_just_unknown(f))
338 e = unknown_expr();
339 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700340 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000341 }
342 return e;
343}
344
H. Peter Anvine2c80182005-01-15 22:15:51 +0000345static expr *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000346{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000347 expr *e, *f;
348
349 e = rexp3(critical);
350 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000351 return NULL;
352 while (i == TOKEN_DBL_AND) {
353 i = scan(scpriv, tokval);
354 f = rexp3(critical);
355 if (!f)
356 return NULL;
357 if (!(is_simple(e) || is_just_unknown(e)) ||
358 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800359 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000360 " scalar values");
361 }
362 if (is_just_unknown(e) || is_just_unknown(f))
363 e = unknown_expr();
364 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700365 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000366 }
367 return e;
368}
369
H. Peter Anvine2c80182005-01-15 22:15:51 +0000370static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000371{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000372 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700373 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000374
375 e = expr0(critical);
376 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000377 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000378
H. Peter Anvin76690a12002-04-30 20:52:49 +0000379 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000380 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
381 int j = i;
382 i = scan(scpriv, tokval);
383 f = expr0(critical);
384 if (!f)
385 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000386
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700387 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000388
H. Peter Anvine2c80182005-01-15 22:15:51 +0000389 switch (j) {
390 case TOKEN_EQ:
391 case TOKEN_NE:
392 if (is_unknown(e))
393 v = -1; /* means unknown */
394 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700395 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000396 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700397 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 break;
399 default:
400 if (is_unknown(e))
401 v = -1; /* means unknown */
402 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800403 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000404 "`%s': operands differ by a non-scalar",
405 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
406 TOKEN_GE ? ">=" : ">"));
407 v = 0; /* must set it to _something_ */
408 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300409 int64_t vv = reloc_value(e);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000410 if (vv == 0)
411 v = (j == TOKEN_LE || j == TOKEN_GE);
412 else if (vv > 0)
413 v = (j == TOKEN_GE || j == TOKEN_GT);
414 else /* vv < 0 */
415 v = (j == TOKEN_LE || j == TOKEN_LT);
416 }
417 break;
418 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000419
H. Peter Anvine2c80182005-01-15 22:15:51 +0000420 if (v == -1)
421 e = unknown_expr();
422 else
423 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000424 }
425 return e;
426}
427
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000429{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000430 expr *e, *f;
431
432 e = expr1(critical);
433 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000434 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000435
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 while (i == '|') {
437 i = scan(scpriv, tokval);
438 f = expr1(critical);
439 if (!f)
440 return NULL;
441 if (!(is_simple(e) || is_just_unknown(e)) ||
442 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800443 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444 " scalar values");
445 }
446 if (is_just_unknown(e) || is_just_unknown(f))
447 e = unknown_expr();
448 else
449 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000450 }
451 return e;
452}
453
H. Peter Anvine2c80182005-01-15 22:15:51 +0000454static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000455{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000456 expr *e, *f;
457
458 e = expr2(critical);
459 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000461
H. Peter Anvin76690a12002-04-30 20:52:49 +0000462 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000463 i = scan(scpriv, tokval);
464 f = expr2(critical);
465 if (!f)
466 return NULL;
467 if (!(is_simple(e) || is_just_unknown(e)) ||
468 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 " scalar values");
471 }
472 if (is_just_unknown(e) || is_just_unknown(f))
473 e = unknown_expr();
474 else
475 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000476 }
477 return e;
478}
479
H. Peter Anvine2c80182005-01-15 22:15:51 +0000480static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000481{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000482 expr *e, *f;
483
484 e = expr3(critical);
485 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000487
H. Peter Anvin76690a12002-04-30 20:52:49 +0000488 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000489 i = scan(scpriv, tokval);
490 f = expr3(critical);
491 if (!f)
492 return NULL;
493 if (!(is_simple(e) || is_just_unknown(e)) ||
494 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800495 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 " scalar values");
497 }
498 if (is_just_unknown(e) || is_just_unknown(f))
499 e = unknown_expr();
500 else
501 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000502 }
503 return e;
504}
505
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000507{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000508 expr *e, *f;
509
510 e = expr4(critical);
511 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000513
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 while (i == TOKEN_SHL || i == TOKEN_SHR) {
515 int j = i;
516 i = scan(scpriv, tokval);
517 f = expr4(critical);
518 if (!f)
519 return NULL;
520 if (!(is_simple(e) || is_just_unknown(e)) ||
521 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800522 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 " scalar values");
524 } else if (is_just_unknown(e) || is_just_unknown(f)) {
525 e = unknown_expr();
526 } else
527 switch (j) {
528 case TOKEN_SHL:
529 e = scalarvect(reloc_value(e) << reloc_value(f));
530 break;
531 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700532 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 reloc_value(f));
534 break;
535 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000536 }
537 return e;
538}
539
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000541{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000542 expr *e, *f;
543
544 e = expr5(critical);
545 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 return NULL;
547 while (i == '+' || i == '-') {
548 int j = i;
549 i = scan(scpriv, tokval);
550 f = expr5(critical);
551 if (!f)
552 return NULL;
553 switch (j) {
554 case '+':
555 e = add_vectors(e, f);
556 break;
557 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700558 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 break;
560 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000561 }
562 return e;
563}
564
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000566{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000567 expr *e, *f;
568
569 e = expr6(critical);
570 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000572 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 i == TOKEN_SDIV || i == TOKEN_SMOD) {
574 int j = i;
575 i = scan(scpriv, tokval);
576 f = expr6(critical);
577 if (!f)
578 return NULL;
579 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
580 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800581 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 " scalar values");
583 return NULL;
584 }
585 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800586 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 return NULL;
588 }
589 switch (j) {
590 case '*':
591 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700592 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700594 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 else if (is_just_unknown(e) && is_just_unknown(f))
596 e = unknown_expr();
597 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800598 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 "non-scalar objects");
600 return NULL;
601 }
602 break;
603 case '/':
604 if (is_just_unknown(e) || is_just_unknown(f))
605 e = unknown_expr();
606 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700607 e = scalarvect(((uint64_t)reloc_value(e)) /
608 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 break;
610 case '%':
611 if (is_just_unknown(e) || is_just_unknown(f))
612 e = unknown_expr();
613 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700614 e = scalarvect(((uint64_t)reloc_value(e)) %
615 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 break;
617 case TOKEN_SDIV:
618 if (is_just_unknown(e) || is_just_unknown(f))
619 e = unknown_expr();
620 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700621 e = scalarvect(((int64_t)reloc_value(e)) /
622 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 break;
624 case TOKEN_SMOD:
625 if (is_just_unknown(e) || is_just_unknown(f))
626 e = unknown_expr();
627 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700628 e = scalarvect(((int64_t)reloc_value(e)) %
629 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 break;
631 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000632 }
633 return e;
634}
635
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700636static expr *eval_floatize(enum floatize type)
637{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300638 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700639 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300640 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700641 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300642 { 1, 0, 1 }, /* FLOAT_8 */
643 { 2, 0, 2 }, /* FLOAT_16 */
644 { 4, 0, 4 }, /* FLOAT_32 */
645 { 8, 0, 8 }, /* FLOAT_64 */
646 { 10, 0, 8 }, /* FLOAT_80M */
647 { 10, 8, 2 }, /* FLOAT_80E */
648 { 16, 0, 8 }, /* FLOAT_128L */
649 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700650 };
651 int sign = 1;
652 int64_t val;
653 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700654
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700655 i = scan(scpriv, tokval);
656 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800657 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300658 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700659 }
660 i = scan(scpriv, tokval);
661 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300662 sign = (i == '-') ? -1 : 1;
663 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700664 }
665 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800666 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300667 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700668 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800669 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300670 return NULL;
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
677 p = result+formats[type].start+formats[type].len;
678 val = 0;
679 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300680 p--;
681 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700682 }
683
684 begintemp();
685 addtotemp(EXPR_SIMPLE, val);
686
687 i = scan(scpriv, tokval);
688 return finishtemp();
689}
690
H. Peter Anvin9c749102008-06-14 21:08:38 -0700691static expr *eval_strfunc(enum strfunc type)
692{
693 char *string;
694 size_t string_len;
695 int64_t val;
696 bool parens, rn_warn;
697
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200698 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700699 i = scan(scpriv, tokval);
700 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300701 parens = true;
702 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700703 }
704 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800705 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300706 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700707 }
708 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300709 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700710 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800711 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300712 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700713 }
714
715 val = readstrnum(string, string_len, &rn_warn);
716 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300717 i = scan(scpriv, tokval);
718 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800719 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300720 return NULL;
721 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700722 }
723
724 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800725 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700726
727 begintemp();
728 addtotemp(EXPR_SIMPLE, val);
729
730 i = scan(scpriv, tokval);
731 return finishtemp();
732}
733
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700734static int64_t eval_ifunc(int64_t val, enum ifunc func)
735{
736 int errtype;
737 uint64_t uval = (uint64_t)val;
738 int64_t rv;
739
740 switch (func) {
741 case IFUNC_ILOG2E:
742 case IFUNC_ILOG2W:
743 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400744
745 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800746 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700747 /* fall through */
748 case IFUNC_ILOG2F:
749 rv = ilog2_64(uval);
750 break;
751
752 case IFUNC_ILOG2C:
753 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
754 break;
755
756 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800757 nasm_error(ERR_PANIC, "invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700758 rv = 0;
759 break;
760 }
761
762 return rv;
763}
764
H. Peter Anvine2c80182005-01-15 22:15:51 +0000765static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000766{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000767 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000768 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800769 int32_t label_seg;
770 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700771 int64_t tmpval;
772 bool rn_warn;
Charles Crayned60059e2008-03-12 22:39:03 -0700773 char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000774
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700775 switch (i) {
776 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 i = scan(scpriv, tokval);
778 e = expr6(critical);
779 if (!e)
780 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700781 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700782
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700783 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 i = scan(scpriv, tokval);
785 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700786
787 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 i = scan(scpriv, tokval);
789 e = expr6(critical);
790 if (!e)
791 return NULL;
792 if (is_just_unknown(e))
793 return unknown_expr();
794 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800795 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 " scalar values");
797 return NULL;
798 }
799 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700800
801 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000802 i = scan(scpriv, tokval);
803 e = expr6(critical);
804 if (!e)
805 return NULL;
806 if (is_just_unknown(e))
807 return unknown_expr();
808 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800809 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000810 " scalar values");
811 return NULL;
812 }
813 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700814
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700815 case TOKEN_IFUNC:
816 {
817 enum ifunc func = tokval->t_integer;
818 i = scan(scpriv, tokval);
819 e = expr6(critical);
820 if (!e)
821 return NULL;
822 if (is_just_unknown(e))
823 return unknown_expr();
824 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800825 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700826 " scalar values");
827 return NULL;
828 }
829 return scalarvect(eval_ifunc(reloc_value(e), func));
830 }
831
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700832 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 i = scan(scpriv, tokval);
834 e = expr6(critical);
835 if (!e)
836 return NULL;
837 e = segment_part(e);
838 if (!e)
839 return NULL;
840 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800841 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 return NULL;
843 }
844 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700845
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700846 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300847 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700848
H. Peter Anvin9c749102008-06-14 21:08:38 -0700849 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300850 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700851
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700852 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 i = scan(scpriv, tokval);
854 e = bexpr(critical);
855 if (!e)
856 return NULL;
857 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800858 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 return NULL;
860 }
861 i = scan(scpriv, tokval);
862 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700863
864 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700865 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700866 case TOKEN_REG:
867 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300868 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700869 case TOKEN_HERE:
870 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700871 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 begintemp();
873 switch (i) {
874 case TOKEN_NUM:
875 addtotemp(EXPR_SIMPLE, tokval->t_integer);
876 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300877 case TOKEN_STR:
878 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
879 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800880 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700881 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300882 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000883 case TOKEN_REG:
884 addtotemp(tokval->t_integer, 1L);
885 if (hint && hint->type == EAH_NOHINT)
886 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
887 break;
888 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300889 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000890 case TOKEN_HERE:
891 case TOKEN_BASE:
892 /*
893 * If !location->known, this indicates that no
894 * symbol, Here or Base references are valid because we
895 * are in preprocess-only mode.
896 */
897 if (!location->known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800898 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000899 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300900 (i == TOKEN_HERE ? "`$'" :
901 i == TOKEN_BASE ? "`$$'" :
902 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 addtotemp(EXPR_UNKNOWN, 1L);
904 break;
905 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000906
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
908 if (i == TOKEN_BASE) {
909 label_seg = in_abs_seg ? abs_seg : location->segment;
910 label_ofs = 0;
911 } else if (i == TOKEN_HERE) {
912 label_seg = in_abs_seg ? abs_seg : location->segment;
913 label_ofs = in_abs_seg ? abs_offset : location->offset;
914 } else {
915 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700916 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800918 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700919 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 return NULL;
921 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800922 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700923 "symbol `%s%s' not defined before use",
924 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 return NULL;
926 } else {
927 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400928 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 type = EXPR_UNKNOWN;
930 label_seg = NO_SEG;
931 label_ofs = 1;
932 }
933 }
934 if (opflags && is_extern(tokval->t_charptr))
935 *opflags |= OPFLAG_EXTERN;
936 }
937 addtotemp(type, label_ofs);
938 if (label_seg != NO_SEG)
939 addtotemp(EXPR_SEGBASE + label_seg, 1L);
940 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700941 case TOKEN_DECORATOR:
942 addtotemp(EXPR_RDSAE, tokval->t_integer);
943 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 }
945 i = scan(scpriv, tokval);
946 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700947
948 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800949 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000951 }
952}
953
H. Peter Anvin36034ec2016-02-18 01:18:50 -0800954void eval_global_info(lfunc lookup_label, struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000955{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000956 labelfunc = lookup_label;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000957 location = locp;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000958}
959
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800961 int *fwref, int critical, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000962{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000963 expr *e;
964 expr *f = NULL;
965
966 hint = hints;
967 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000969
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000970 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 critical &= ~CRITICAL;
972 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000973 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000975
976 scan = sc;
977 scpriv = scprivate;
978 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000979 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000980
981 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000983 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000985
Keith Kaniosb7a89542007-04-12 02:40:54 +0000986 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000988
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000990 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000992
993 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 i = scan(scpriv, tokval); /* eat the WRT */
995 f = expr6(critical);
996 if (!f)
997 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000998 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700999 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001000 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 expr *g;
1002 if (is_just_unknown(f))
1003 g = unknown_expr();
1004 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001005 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 begintemp();
1007 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001008 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 return NULL;
1010 }
1011 value = reloc_seg(f);
1012 if (value == NO_SEG)
1013 value = reloc_value(f) | SEG_ABS;
1014 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001015 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 return NULL;
1017 }
1018 addtotemp(EXPR_WRT, value);
1019 g = finishtemp();
1020 }
1021 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001022 }
1023 return e;
1024}