blob: a8e463f208de52af17a0fb7c7b9cb0bbafac79c1 [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 Anvine2c80182005-01-15 22:15:51 +000059static struct ofmt *outfmt; /* Structure of addresses of output routines */
H. Peter Anvineba20a72002-04-30 20:53:55 +000060
61static expr **tempexprs = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +000062static int ntempexprs;
63static int tempexprs_size = 0;
H. Peter Anvineba20a72002-04-30 20:53:55 +000064
H. Peter Anvine2c80182005-01-15 22:15:51 +000065static expr *tempexpr;
66static int ntempexpr;
67static int tempexpr_size;
H. Peter Anvineba20a72002-04-30 20:53:55 +000068
H. Peter Anvine2c80182005-01-15 22:15:51 +000069static struct tokenval *tokval; /* The current token */
70static int i; /* The t_type of tokval */
H. Peter Anvineba20a72002-04-30 20:53:55 +000071
H. Peter Anvin76690a12002-04-30 20:52:49 +000072static void *scpriv;
H. Peter Anvin12e46512007-10-03 21:30:57 -070073static struct location *location; /* Pointer to current line's segment,offset */
H. Peter Anvineba20a72002-04-30 20:53:55 +000074static int *opflags;
H. Peter Anvin76690a12002-04-30 20:52:49 +000075
76static struct eval_hints *hint;
77
H. Peter Anvine2c80182005-01-15 22:15:51 +000078extern int in_abs_seg; /* ABSOLUTE segment flag */
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +030079extern int32_t abs_seg; /* ABSOLUTE segment */
80extern int32_t abs_offset; /* ABSOLUTE segment offset */
H. Peter Anvin667dd802002-05-26 19:49:41 +000081
H. Peter Anvin76690a12002-04-30 20:52:49 +000082/*
H. Peter Anvineba20a72002-04-30 20:53:55 +000083 * Unimportant cleanup is done to avoid confusing people who are trying
84 * to debug real memory leaks
85 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000086void eval_cleanup(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000087{
88 while (ntempexprs)
H. Peter Anvine2c80182005-01-15 22:15:51 +000089 nasm_free(tempexprs[--ntempexprs]);
90 nasm_free(tempexprs);
H. Peter Anvineba20a72002-04-30 20:53:55 +000091}
92
93/*
H. Peter Anvin76690a12002-04-30 20:52:49 +000094 * Construct a temporary expression.
95 */
H. Peter Anvine2c80182005-01-15 22:15:51 +000096static void begintemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +000097{
H. Peter Anvin76690a12002-04-30 20:52:49 +000098 tempexpr = NULL;
99 tempexpr_size = ntempexpr = 0;
100}
101
Keith Kaniosb7a89542007-04-12 02:40:54 +0000102static void addtotemp(int32_t type, int64_t value)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000103{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000104 while (ntempexpr >= tempexpr_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000105 tempexpr_size += TEMPEXPR_DELTA;
106 tempexpr = nasm_realloc(tempexpr,
107 tempexpr_size * sizeof(*tempexpr));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000108 }
109 tempexpr[ntempexpr].type = type;
110 tempexpr[ntempexpr++].value = value;
111}
112
H. Peter Anvine2c80182005-01-15 22:15:51 +0000113static expr *finishtemp(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000114{
H. Peter Anvine2c80182005-01-15 22:15:51 +0000115 addtotemp(0L, 0L); /* terminate */
H. Peter Anvin76690a12002-04-30 20:52:49 +0000116 while (ntempexprs >= tempexprs_size) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000117 tempexprs_size += TEMPEXPRS_DELTA;
118 tempexprs = nasm_realloc(tempexprs,
119 tempexprs_size * sizeof(*tempexprs));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000120 }
121 return tempexprs[ntempexprs++] = tempexpr;
122}
123
124/*
125 * Add two vector datatypes. We have some bizarre behaviour on far-
126 * absolute segment types: we preserve them during addition _only_
127 * if one of the segments is a truly pure scalar.
128 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000129static expr *add_vectors(expr * p, expr * q)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000130{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000131 int preserve;
132
133 preserve = is_really_simple(p) || is_really_simple(q);
134
135 begintemp();
136
137 while (p->type && q->type &&
H. Peter Anvine2c80182005-01-15 22:15:51 +0000138 p->type < EXPR_SEGBASE + SEG_ABS &&
139 q->type < EXPR_SEGBASE + SEG_ABS) {
140 int lasttype;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000141
H. Peter Anvine2c80182005-01-15 22:15:51 +0000142 if (p->type > q->type) {
143 addtotemp(q->type, q->value);
144 lasttype = q++->type;
145 } else if (p->type < q->type) {
146 addtotemp(p->type, p->value);
147 lasttype = p++->type;
148 } else { /* *p and *q have same type */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700149 int64_t sum = p->value + q->value;
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800150 if (sum) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000151 addtotemp(p->type, sum);
Jin Kyu Song4360ba22013-12-10 16:24:45 -0800152 if (hint)
153 hint->type = EAH_SUMMED;
154 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000155 lasttype = p->type;
156 p++, q++;
157 }
158 if (lasttype == EXPR_UNKNOWN) {
159 return finishtemp();
160 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000161 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000162 while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
163 addtotemp(p->type, p->value);
164 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000165 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000166 while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
167 addtotemp(q->type, q->value);
168 q++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000169 }
170
171 return finishtemp();
172}
173
174/*
175 * Multiply a vector by a scalar. Strip far-absolute segment part
176 * if present.
177 *
178 * Explicit treatment of UNKNOWN is not required in this routine,
179 * since it will silently do the Right Thing anyway.
180 *
181 * If `affect_hints' is set, we also change the hint type to
182 * NOTBASE if a MAKEBASE hint points at a register being
183 * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
184 * as the base register.
185 */
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700186static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000187{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000188 expr *p = vect;
189
H. Peter Anvine2c80182005-01-15 22:15:51 +0000190 while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
191 p->value = scalar * (p->value);
192 if (hint && hint->type == EAH_MAKEBASE &&
193 p->type == hint->base && affect_hints)
194 hint->type = EAH_NOTBASE;
195 p++;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000196 }
197 p->type = 0;
198
199 return vect;
200}
201
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700202static expr *scalarvect(int64_t scalar)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000203{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000204 begintemp();
205 addtotemp(EXPR_SIMPLE, scalar);
206 return finishtemp();
207}
208
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209static expr *unknown_expr(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000210{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000211 begintemp();
212 addtotemp(EXPR_UNKNOWN, 1L);
213 return finishtemp();
214}
215
216/*
217 * The SEG operator: calculate the segment part of a relocatable
218 * value. Return NULL, as usual, if an error occurs. Report the
219 * error too.
220 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000221static expr *segment_part(expr * e)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000222{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000223 int32_t seg;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000224
225 if (is_unknown(e))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000226 return unknown_expr();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000227
228 if (!is_reloc(e)) {
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 }
232
233 seg = reloc_seg(e);
234 if (seg == NO_SEG) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800235 nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000236 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000237 } else if (seg & SEG_ABS) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000238 return scalarvect(seg & ~SEG_ABS);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000239 } else if (seg & 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800240 nasm_error(ERR_NONFATAL, "SEG applied to something which"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000241 " is already a segment base");
242 return NULL;
243 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000244 int32_t base = outfmt->segbase(seg + 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000245
H. Peter Anvine2c80182005-01-15 22:15:51 +0000246 begintemp();
247 addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
248 1L);
249 return finishtemp();
H. Peter Anvin76690a12002-04-30 20:52:49 +0000250 }
251}
252
253/*
254 * Recursive-descent parser. Called with a single boolean operand,
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700255 * which is true if the evaluation is critical (i.e. unresolved
H. Peter Anvin76690a12002-04-30 20:52:49 +0000256 * symbols are an error condition). Must update the global `i' to
257 * reflect the token after the parsed string. May return NULL.
258 *
259 * evaluate() should report its own errors: on return it is assumed
260 * that if NULL has been returned, the error has already been
261 * reported.
262 */
263
264/*
265 * Grammar parsed is:
266 *
267 * expr : bexpr [ WRT expr6 ]
268 * bexpr : rexp0 or expr0 depending on relative-mode setting
269 * rexp0 : rexp1 [ {||} rexp1...]
270 * rexp1 : rexp2 [ {^^} rexp2...]
271 * rexp2 : rexp3 [ {&&} rexp3...]
272 * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
273 * expr0 : expr1 [ {|} expr1...]
274 * expr1 : expr2 [ {^} expr2...]
275 * expr2 : expr3 [ {&} expr3...]
276 * expr3 : expr4 [ {<<,>>} expr4...]
277 * expr4 : expr5 [ {+,-} expr5...]
278 * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700279 * expr6 : { ~,+,-,IFUNC,SEG } expr6
H. Peter Anvin76690a12002-04-30 20:52:49 +0000280 * | (bexpr)
281 * | symbol
282 * | $
283 * | number
284 */
285
286static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
287
288static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
289static expr *expr4(int), *expr5(int), *expr6(int);
290
H. Peter Anvine2c80182005-01-15 22:15:51 +0000291static expr *(*bexpr) (int);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000292
H. Peter Anvine2c80182005-01-15 22:15:51 +0000293static expr *rexp0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000294{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000295 expr *e, *f;
296
297 e = rexp1(critical);
298 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000299 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000300
H. Peter Anvine2c80182005-01-15 22:15:51 +0000301 while (i == TOKEN_DBL_OR) {
302 i = scan(scpriv, tokval);
303 f = rexp1(critical);
304 if (!f)
305 return NULL;
306 if (!(is_simple(e) || is_just_unknown(e)) ||
307 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800308 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309 " scalar values");
310 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000311
H. Peter Anvine2c80182005-01-15 22:15:51 +0000312 if (is_just_unknown(e) || is_just_unknown(f))
313 e = unknown_expr();
314 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700315 e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000316 }
317 return e;
318}
319
H. Peter Anvine2c80182005-01-15 22:15:51 +0000320static expr *rexp1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000321{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000322 expr *e, *f;
323
324 e = rexp2(critical);
325 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000326 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000327
H. Peter Anvine2c80182005-01-15 22:15:51 +0000328 while (i == TOKEN_DBL_XOR) {
329 i = scan(scpriv, tokval);
330 f = rexp2(critical);
331 if (!f)
332 return NULL;
333 if (!(is_simple(e) || is_just_unknown(e)) ||
334 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800335 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000336 " scalar values");
337 }
338
339 if (is_just_unknown(e) || is_just_unknown(f))
340 e = unknown_expr();
341 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700342 e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000343 }
344 return e;
345}
346
H. Peter Anvine2c80182005-01-15 22:15:51 +0000347static expr *rexp2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000348{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000349 expr *e, *f;
350
351 e = rexp3(critical);
352 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000353 return NULL;
354 while (i == TOKEN_DBL_AND) {
355 i = scan(scpriv, tokval);
356 f = rexp3(critical);
357 if (!f)
358 return NULL;
359 if (!(is_simple(e) || is_just_unknown(e)) ||
360 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800361 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000362 " scalar values");
363 }
364 if (is_just_unknown(e) || is_just_unknown(f))
365 e = unknown_expr();
366 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700367 e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000368 }
369 return e;
370}
371
H. Peter Anvine2c80182005-01-15 22:15:51 +0000372static expr *rexp3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000373{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000374 expr *e, *f;
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700375 int64_t v;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000376
377 e = expr0(critical);
378 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000379 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000380
H. Peter Anvin76690a12002-04-30 20:52:49 +0000381 while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000382 i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
383 int j = i;
384 i = scan(scpriv, tokval);
385 f = expr0(critical);
386 if (!f)
387 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000388
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700389 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvineba20a72002-04-30 20:53:55 +0000390
H. Peter Anvine2c80182005-01-15 22:15:51 +0000391 switch (j) {
392 case TOKEN_EQ:
393 case TOKEN_NE:
394 if (is_unknown(e))
395 v = -1; /* means unknown */
396 else if (!is_really_simple(e) || reloc_value(e) != 0)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700397 v = (j == TOKEN_NE); /* unequal, so return true if NE */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000398 else
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700399 v = (j == TOKEN_EQ); /* equal, so return true if EQ */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000400 break;
401 default:
402 if (is_unknown(e))
403 v = -1; /* means unknown */
404 else if (!is_really_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800405 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000406 "`%s': operands differ by a non-scalar",
407 (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
408 TOKEN_GE ? ">=" : ">"));
409 v = 0; /* must set it to _something_ */
410 } else {
Cyrill Gorcunov9f135ed2010-11-06 23:04:12 +0300411 int64_t vv = reloc_value(e);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000412 if (vv == 0)
413 v = (j == TOKEN_LE || j == TOKEN_GE);
414 else if (vv > 0)
415 v = (j == TOKEN_GE || j == TOKEN_GT);
416 else /* vv < 0 */
417 v = (j == TOKEN_LE || j == TOKEN_LT);
418 }
419 break;
420 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000421
H. Peter Anvine2c80182005-01-15 22:15:51 +0000422 if (v == -1)
423 e = unknown_expr();
424 else
425 e = scalarvect(v);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000426 }
427 return e;
428}
429
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430static expr *expr0(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000431{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000432 expr *e, *f;
433
434 e = expr1(critical);
435 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000436 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000437
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438 while (i == '|') {
439 i = scan(scpriv, tokval);
440 f = expr1(critical);
441 if (!f)
442 return NULL;
443 if (!(is_simple(e) || is_just_unknown(e)) ||
444 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800445 nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000446 " scalar values");
447 }
448 if (is_just_unknown(e) || is_just_unknown(f))
449 e = unknown_expr();
450 else
451 e = scalarvect(reloc_value(e) | reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000452 }
453 return e;
454}
455
H. Peter Anvine2c80182005-01-15 22:15:51 +0000456static expr *expr1(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000457{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 expr *e, *f;
459
460 e = expr2(critical);
461 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000462 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000463
H. Peter Anvin76690a12002-04-30 20:52:49 +0000464 while (i == '^') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 i = scan(scpriv, tokval);
466 f = expr2(critical);
467 if (!f)
468 return NULL;
469 if (!(is_simple(e) || is_just_unknown(e)) ||
470 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800471 nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000472 " scalar values");
473 }
474 if (is_just_unknown(e) || is_just_unknown(f))
475 e = unknown_expr();
476 else
477 e = scalarvect(reloc_value(e) ^ reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000478 }
479 return e;
480}
481
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482static expr *expr2(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000483{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484 expr *e, *f;
485
486 e = expr3(critical);
487 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000489
H. Peter Anvin76690a12002-04-30 20:52:49 +0000490 while (i == '&') {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000491 i = scan(scpriv, tokval);
492 f = expr3(critical);
493 if (!f)
494 return NULL;
495 if (!(is_simple(e) || is_just_unknown(e)) ||
496 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800497 nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 " scalar values");
499 }
500 if (is_just_unknown(e) || is_just_unknown(f))
501 e = unknown_expr();
502 else
503 e = scalarvect(reloc_value(e) & reloc_value(f));
H. Peter Anvin76690a12002-04-30 20:52:49 +0000504 }
505 return e;
506}
507
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508static expr *expr3(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000509{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000510 expr *e, *f;
511
512 e = expr4(critical);
513 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000514 return NULL;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000515
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 while (i == TOKEN_SHL || i == TOKEN_SHR) {
517 int j = i;
518 i = scan(scpriv, tokval);
519 f = expr4(critical);
520 if (!f)
521 return NULL;
522 if (!(is_simple(e) || is_just_unknown(e)) ||
523 !(is_simple(f) || is_just_unknown(f))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800524 nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 " scalar values");
526 } else if (is_just_unknown(e) || is_just_unknown(f)) {
527 e = unknown_expr();
528 } else
529 switch (j) {
530 case TOKEN_SHL:
531 e = scalarvect(reloc_value(e) << reloc_value(f));
532 break;
533 case TOKEN_SHR:
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700534 e = scalarvect(((uint64_t)reloc_value(e)) >>
H. Peter Anvine2c80182005-01-15 22:15:51 +0000535 reloc_value(f));
536 break;
537 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000538 }
539 return e;
540}
541
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542static expr *expr4(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000543{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000544 expr *e, *f;
545
546 e = expr5(critical);
547 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000548 return NULL;
549 while (i == '+' || i == '-') {
550 int j = i;
551 i = scan(scpriv, tokval);
552 f = expr5(critical);
553 if (!f)
554 return NULL;
555 switch (j) {
556 case '+':
557 e = add_vectors(e, f);
558 break;
559 case '-':
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700560 e = add_vectors(e, scalar_mult(f, -1L, false));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 break;
562 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000563 }
564 return e;
565}
566
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567static expr *expr5(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000568{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000569 expr *e, *f;
570
571 e = expr6(critical);
572 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000574 while (i == '*' || i == '/' || i == '%' ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000575 i == TOKEN_SDIV || i == TOKEN_SMOD) {
576 int j = i;
577 i = scan(scpriv, tokval);
578 f = expr6(critical);
579 if (!f)
580 return NULL;
581 if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
582 !(is_simple(f) || is_just_unknown(f)))) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800583 nasm_error(ERR_NONFATAL, "division operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 " scalar values");
585 return NULL;
586 }
587 if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800588 nasm_error(ERR_NONFATAL, "division by zero");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 return NULL;
590 }
591 switch (j) {
592 case '*':
593 if (is_simple(e))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700594 e = scalar_mult(f, reloc_value(e), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 else if (is_simple(f))
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700596 e = scalar_mult(e, reloc_value(f), true);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 else if (is_just_unknown(e) && is_just_unknown(f))
598 e = unknown_expr();
599 else {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800600 nasm_error(ERR_NONFATAL, "unable to multiply two "
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 "non-scalar objects");
602 return NULL;
603 }
604 break;
605 case '/':
606 if (is_just_unknown(e) || is_just_unknown(f))
607 e = unknown_expr();
608 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700609 e = scalarvect(((uint64_t)reloc_value(e)) /
610 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000611 break;
612 case '%':
613 if (is_just_unknown(e) || is_just_unknown(f))
614 e = unknown_expr();
615 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700616 e = scalarvect(((uint64_t)reloc_value(e)) %
617 ((uint64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 break;
619 case TOKEN_SDIV:
620 if (is_just_unknown(e) || is_just_unknown(f))
621 e = unknown_expr();
622 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700623 e = scalarvect(((int64_t)reloc_value(e)) /
624 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 break;
626 case TOKEN_SMOD:
627 if (is_just_unknown(e) || is_just_unknown(f))
628 e = unknown_expr();
629 else
Keith Kaniosa5fc6462007-10-13 07:09:22 -0700630 e = scalarvect(((int64_t)reloc_value(e)) %
631 ((int64_t)reloc_value(f)));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 break;
633 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000634 }
635 return e;
636}
637
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700638static expr *eval_floatize(enum floatize type)
639{
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300640 uint8_t result[16], *p; /* Up to 128 bits */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700641 static const struct {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300642 int bytes, start, len;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700643 } formats[] = {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300644 { 1, 0, 1 }, /* FLOAT_8 */
645 { 2, 0, 2 }, /* FLOAT_16 */
646 { 4, 0, 4 }, /* FLOAT_32 */
647 { 8, 0, 8 }, /* FLOAT_64 */
648 { 10, 0, 8 }, /* FLOAT_80M */
649 { 10, 8, 2 }, /* FLOAT_80E */
650 { 16, 0, 8 }, /* FLOAT_128L */
651 { 16, 8, 8 }, /* FLOAT_128H */
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700652 };
653 int sign = 1;
654 int64_t val;
655 int j;
H. Peter Anvin70653092007-10-19 14:42:29 -0700656
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700657 i = scan(scpriv, tokval);
658 if (i != '(') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800659 nasm_error(ERR_NONFATAL, "expecting `('");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300660 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700661 }
662 i = scan(scpriv, tokval);
663 if (i == '-' || i == '+') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300664 sign = (i == '-') ? -1 : 1;
665 i = scan(scpriv, tokval);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700666 }
667 if (i != TOKEN_FLOAT) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800668 nasm_error(ERR_NONFATAL, "expecting floating-point number");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300669 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700670 }
H. Peter Anvin130736c2016-02-17 20:27:41 -0800671 if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300672 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700673 i = scan(scpriv, tokval);
674 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800675 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300676 return NULL;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700677 }
678
679 p = result+formats[type].start+formats[type].len;
680 val = 0;
681 for (j = formats[type].len; j; j--) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300682 p--;
683 val = (val << 8) + *p;
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700684 }
685
686 begintemp();
687 addtotemp(EXPR_SIMPLE, val);
688
689 i = scan(scpriv, tokval);
690 return finishtemp();
691}
692
H. Peter Anvin9c749102008-06-14 21:08:38 -0700693static expr *eval_strfunc(enum strfunc type)
694{
695 char *string;
696 size_t string_len;
697 int64_t val;
698 bool parens, rn_warn;
699
Victor van den Elzenc7deefa2008-06-25 11:41:40 +0200700 parens = false;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700701 i = scan(scpriv, tokval);
702 if (i == '(') {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300703 parens = true;
704 i = scan(scpriv, tokval);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700705 }
706 if (i != TOKEN_STR) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800707 nasm_error(ERR_NONFATAL, "expecting string");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300708 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700709 }
710 string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300711 &string, type);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700712 if (string_len == (size_t)-1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800713 nasm_error(ERR_NONFATAL, "invalid string for transform");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300714 return NULL;
H. Peter Anvin9c749102008-06-14 21:08:38 -0700715 }
716
717 val = readstrnum(string, string_len, &rn_warn);
718 if (parens) {
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300719 i = scan(scpriv, tokval);
720 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800721 nasm_error(ERR_NONFATAL, "expecting `)'");
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300722 return NULL;
723 }
H. Peter Anvin9c749102008-06-14 21:08:38 -0700724 }
725
726 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800727 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin9c749102008-06-14 21:08:38 -0700728
729 begintemp();
730 addtotemp(EXPR_SIMPLE, val);
731
732 i = scan(scpriv, tokval);
733 return finishtemp();
734}
735
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700736static int64_t eval_ifunc(int64_t val, enum ifunc func)
737{
738 int errtype;
739 uint64_t uval = (uint64_t)val;
740 int64_t rv;
741
742 switch (func) {
743 case IFUNC_ILOG2E:
744 case IFUNC_ILOG2W:
745 errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
Cyrill Gorcunov71ba1f02013-02-18 01:38:11 +0400746
747 if (!is_power2(uval))
H. Peter Anvin130736c2016-02-17 20:27:41 -0800748 nasm_error(errtype, "ilog2 argument is not a power of two");
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700749 /* fall through */
750 case IFUNC_ILOG2F:
751 rv = ilog2_64(uval);
752 break;
753
754 case IFUNC_ILOG2C:
755 rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
756 break;
757
758 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800759 nasm_error(ERR_PANIC, "invalid IFUNC token %d", func);
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700760 rv = 0;
761 break;
762 }
763
764 return rv;
765}
766
H. Peter Anvine2c80182005-01-15 22:15:51 +0000767static expr *expr6(int critical)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000768{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000769 int32_t type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000770 expr *e;
Charles Crayne4e8563d2007-11-05 17:19:32 -0800771 int32_t label_seg;
772 int64_t label_ofs;
H. Peter Anvin11627042008-06-09 20:45:19 -0700773 int64_t tmpval;
774 bool rn_warn;
Charles Crayned60059e2008-03-12 22:39:03 -0700775 char *scope;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000776
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700777 switch (i) {
778 case '-':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 i = scan(scpriv, tokval);
780 e = expr6(critical);
781 if (!e)
782 return NULL;
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700783 return scalar_mult(e, -1L, false);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700784
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700785 case '+':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 i = scan(scpriv, tokval);
787 return expr6(critical);
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700788
789 case '~':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 i = scan(scpriv, tokval);
791 e = expr6(critical);
792 if (!e)
793 return NULL;
794 if (is_just_unknown(e))
795 return unknown_expr();
796 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800797 nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 " scalar values");
799 return NULL;
800 }
801 return scalarvect(~reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700802
803 case '!':
Chuck Craynecb9bc212007-05-02 04:21:26 +0000804 i = scan(scpriv, tokval);
805 e = expr6(critical);
806 if (!e)
807 return NULL;
808 if (is_just_unknown(e))
809 return unknown_expr();
810 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800811 nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
Chuck Craynecb9bc212007-05-02 04:21:26 +0000812 " scalar values");
813 return NULL;
814 }
815 return scalarvect(!reloc_value(e));
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700816
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700817 case TOKEN_IFUNC:
818 {
819 enum ifunc func = tokval->t_integer;
820 i = scan(scpriv, tokval);
821 e = expr6(critical);
822 if (!e)
823 return NULL;
824 if (is_just_unknown(e))
825 return unknown_expr();
826 else if (!is_simple(e)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800827 nasm_error(ERR_NONFATAL, "function may only be applied to"
H. Peter Anvin290b4cb2012-05-31 10:25:37 -0700828 " scalar values");
829 return NULL;
830 }
831 return scalarvect(eval_ifunc(reloc_value(e), func));
832 }
833
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700834 case TOKEN_SEG:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 i = scan(scpriv, tokval);
836 e = expr6(critical);
837 if (!e)
838 return NULL;
839 e = segment_part(e);
840 if (!e)
841 return NULL;
842 if (is_unknown(e) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800843 nasm_error(ERR_NONFATAL, "unable to determine segment base");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 return NULL;
845 }
846 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700847
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700848 case TOKEN_FLOATIZE:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300849 return eval_floatize(tokval->t_integer);
H. Peter Anvindc467ba2007-09-24 12:30:54 -0700850
H. Peter Anvin9c749102008-06-14 21:08:38 -0700851 case TOKEN_STRFUNC:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300852 return eval_strfunc(tokval->t_integer);
H. Peter Anvin9c749102008-06-14 21:08:38 -0700853
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700854 case '(':
H. Peter Anvine2c80182005-01-15 22:15:51 +0000855 i = scan(scpriv, tokval);
856 e = bexpr(critical);
857 if (!e)
858 return NULL;
859 if (i != ')') {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800860 nasm_error(ERR_NONFATAL, "expecting `)'");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 return NULL;
862 }
863 i = scan(scpriv, tokval);
864 return e;
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700865
866 case TOKEN_NUM:
H. Peter Anvin11627042008-06-09 20:45:19 -0700867 case TOKEN_STR:
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700868 case TOKEN_REG:
869 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300870 case TOKEN_INSN: /* Opcodes that occur here are really labels */
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700871 case TOKEN_HERE:
872 case TOKEN_BASE:
Jin Kyu Song72018a22013-08-05 20:46:18 -0700873 case TOKEN_DECORATOR:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000874 begintemp();
875 switch (i) {
876 case TOKEN_NUM:
877 addtotemp(EXPR_SIMPLE, tokval->t_integer);
878 break;
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300879 case TOKEN_STR:
880 tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
881 if (rn_warn)
H. Peter Anvin130736c2016-02-17 20:27:41 -0800882 nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
H. Peter Anvin11627042008-06-09 20:45:19 -0700883 addtotemp(EXPR_SIMPLE, tmpval);
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300884 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000885 case TOKEN_REG:
886 addtotemp(tokval->t_integer, 1L);
887 if (hint && hint->type == EAH_NOHINT)
888 hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
889 break;
890 case TOKEN_ID:
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300891 case TOKEN_INSN:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000892 case TOKEN_HERE:
893 case TOKEN_BASE:
894 /*
895 * If !location->known, this indicates that no
896 * symbol, Here or Base references are valid because we
897 * are in preprocess-only mode.
898 */
899 if (!location->known) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800900 nasm_error(ERR_NONFATAL,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000901 "%s not supported in preprocess-only mode",
Cyrill Gorcunovcfbcddf2009-10-31 20:05:32 +0300902 (i == TOKEN_HERE ? "`$'" :
903 i == TOKEN_BASE ? "`$$'" :
904 "symbol references"));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 addtotemp(EXPR_UNKNOWN, 1L);
906 break;
907 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000908
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
910 if (i == TOKEN_BASE) {
911 label_seg = in_abs_seg ? abs_seg : location->segment;
912 label_ofs = 0;
913 } else if (i == TOKEN_HERE) {
914 label_seg = in_abs_seg ? abs_seg : location->segment;
915 label_ofs = in_abs_seg ? abs_offset : location->offset;
916 } else {
917 if (!labelfunc(tokval->t_charptr, &label_seg, &label_ofs)) {
Charles Crayned60059e2008-03-12 22:39:03 -0700918 scope = local_scope(tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000919 if (critical == 2) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800920 nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
Charles Crayned60059e2008-03-12 22:39:03 -0700921 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 return NULL;
923 } else if (critical == 1) {
H. Peter Anvin130736c2016-02-17 20:27:41 -0800924 nasm_error(ERR_NONFATAL,
Charles Crayned60059e2008-03-12 22:39:03 -0700925 "symbol `%s%s' not defined before use",
926 scope,tokval->t_charptr);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 return NULL;
928 } else {
929 if (opflags)
Cyrill Gorcunove2457c72010-09-10 01:02:12 +0400930 *opflags |= OPFLAG_FORWARD;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 type = EXPR_UNKNOWN;
932 label_seg = NO_SEG;
933 label_ofs = 1;
934 }
935 }
936 if (opflags && is_extern(tokval->t_charptr))
937 *opflags |= OPFLAG_EXTERN;
938 }
939 addtotemp(type, label_ofs);
940 if (label_seg != NO_SEG)
941 addtotemp(EXPR_SEGBASE + label_seg, 1L);
942 break;
Jin Kyu Song72018a22013-08-05 20:46:18 -0700943 case TOKEN_DECORATOR:
944 addtotemp(EXPR_RDSAE, tokval->t_integer);
945 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 }
947 i = scan(scpriv, tokval);
948 return finishtemp();
H. Peter Anvin5f77c032007-09-24 10:51:07 -0700949
950 default:
H. Peter Anvin130736c2016-02-17 20:27:41 -0800951 nasm_error(ERR_NONFATAL, "expression syntax error");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000953 }
954}
955
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956void eval_global_info(struct ofmt *output, lfunc lookup_label,
H. Peter Anvin12e46512007-10-03 21:30:57 -0700957 struct location * locp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000958{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000959 outfmt = output;
960 labelfunc = lookup_label;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000961 location = locp;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000962}
963
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
H. Peter Anvin130736c2016-02-17 20:27:41 -0800965 int *fwref, int critical, struct eval_hints *hints)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000966{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000967 expr *e;
968 expr *f = NULL;
969
970 hint = hints;
971 if (hint)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 hint->type = EAH_NOHINT;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000973
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000974 if (critical & CRITICAL) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 critical &= ~CRITICAL;
976 bexpr = rexp0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000977 } else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 bexpr = expr0;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000979
980 scan = sc;
981 scpriv = scprivate;
982 tokval = tv;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000983 opflags = fwref;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000984
985 if (tokval->t_type == TOKEN_INVALID)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 i = scan(scpriv, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000987 else
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 i = tokval->t_type;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000989
Keith Kaniosb7a89542007-04-12 02:40:54 +0000990 while (ntempexprs) /* initialize temporary storage */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 nasm_free(tempexprs[--ntempexprs]);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000992
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 e = bexpr(critical);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000994 if (!e)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000996
997 if (i == TOKEN_WRT) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 i = scan(scpriv, tokval); /* eat the WRT */
999 f = expr6(critical);
1000 if (!f)
1001 return NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001002 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001003 e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
H. Peter Anvin76690a12002-04-30 20:52:49 +00001004 if (f) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 expr *g;
1006 if (is_just_unknown(f))
1007 g = unknown_expr();
1008 else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001009 int64_t value;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 begintemp();
1011 if (!is_reloc(f)) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001012 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 return NULL;
1014 }
1015 value = reloc_seg(f);
1016 if (value == NO_SEG)
1017 value = reloc_value(f) | SEG_ABS;
1018 else if (!(value & SEG_ABS) && !(value % 2) && critical) {
H. Peter Anvin130736c2016-02-17 20:27:41 -08001019 nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 return NULL;
1021 }
1022 addtotemp(EXPR_WRT, value);
1023 g = finishtemp();
1024 }
1025 e = add_vectors(e, g);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001026 }
1027 return e;
1028}