2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2009-11-01 01:05:32 +08:00
|
|
|
*
|
2018-06-13 04:50:37 +08:00
|
|
|
* Copyright 1996-2018 The NASM Authors - All Rights Reserved
|
2009-06-29 08:13:04 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:52:49 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2009-11-01 01:05:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* eval.c expression evaluator for the Netwide Assembler
|
2002-05-01 04:52:49 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2018-12-28 04:37:25 +08:00
|
|
|
#include "nctype.h"
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
2017-09-28 04:34:42 +08:00
|
|
|
#include "ilog2.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "error.h"
|
2002-05-01 04:52:49 +08:00
|
|
|
#include "eval.h"
|
2002-05-01 04:53:55 +08:00
|
|
|
#include "labels.h"
|
2007-09-25 03:30:54 +08:00
|
|
|
#include "float.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "assemble.h"
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
#define TEMPEXPRS_DELTA 128
|
|
|
|
#define TEMPEXPR_DELTA 8
|
|
|
|
|
2018-11-29 02:27:30 +08:00
|
|
|
static scanner scanfunc; /* Address of scanner routine */
|
|
|
|
static void *scpriv; /* Scanner private pointer */
|
2002-05-01 04:53:55 +08:00
|
|
|
|
|
|
|
static expr **tempexprs = NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
static int ntempexprs;
|
|
|
|
static int tempexprs_size = 0;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static expr *tempexpr;
|
|
|
|
static int ntempexpr;
|
|
|
|
static int tempexpr_size;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static struct tokenval *tokval; /* The current token */
|
2018-11-29 02:33:16 +08:00
|
|
|
static int tt; /* The t_type of tokval */
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2018-12-19 03:12:46 +08:00
|
|
|
static bool critical;
|
2002-05-01 04:53:55 +08:00
|
|
|
static int *opflags;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
static struct eval_hints *hint;
|
2018-06-16 08:51:39 +08:00
|
|
|
static int64_t deadman;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2002-05-27 03:49:41 +08:00
|
|
|
|
2002-05-01 04:53:55 +08:00
|
|
|
/*
|
|
|
|
* Unimportant cleanup is done to avoid confusing people who are trying
|
|
|
|
* to debug real memory leaks
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
void eval_cleanup(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
|
|
|
while (ntempexprs)
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(tempexprs[--ntempexprs]);
|
|
|
|
nasm_free(tempexprs);
|
2002-05-01 04:53:55 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* Construct a temporary expression.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
static void begintemp(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
tempexpr = NULL;
|
|
|
|
tempexpr_size = ntempexpr = 0;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static void addtotemp(int32_t type, int64_t value)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
while (ntempexpr >= tempexpr_size) {
|
2005-01-16 06:15:51 +08:00
|
|
|
tempexpr_size += TEMPEXPR_DELTA;
|
|
|
|
tempexpr = nasm_realloc(tempexpr,
|
|
|
|
tempexpr_size * sizeof(*tempexpr));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
tempexpr[ntempexpr].type = type;
|
|
|
|
tempexpr[ntempexpr++].value = value;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static expr *finishtemp(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
addtotemp(0L, 0L); /* terminate */
|
2002-05-01 04:52:49 +08:00
|
|
|
while (ntempexprs >= tempexprs_size) {
|
2005-01-16 06:15:51 +08:00
|
|
|
tempexprs_size += TEMPEXPRS_DELTA;
|
|
|
|
tempexprs = nasm_realloc(tempexprs,
|
|
|
|
tempexprs_size * sizeof(*tempexprs));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return tempexprs[ntempexprs++] = tempexpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add two vector datatypes. We have some bizarre behaviour on far-
|
|
|
|
* absolute segment types: we preserve them during addition _only_
|
|
|
|
* if one of the segments is a truly pure scalar.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
static expr *add_vectors(expr * p, expr * q)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
int preserve;
|
|
|
|
|
|
|
|
preserve = is_really_simple(p) || is_really_simple(q);
|
|
|
|
|
|
|
|
begintemp();
|
|
|
|
|
|
|
|
while (p->type && q->type &&
|
2005-01-16 06:15:51 +08:00
|
|
|
p->type < EXPR_SEGBASE + SEG_ABS &&
|
|
|
|
q->type < EXPR_SEGBASE + SEG_ABS) {
|
|
|
|
int lasttype;
|
|
|
|
|
|
|
|
if (p->type > q->type) {
|
|
|
|
addtotemp(q->type, q->value);
|
|
|
|
lasttype = q++->type;
|
|
|
|
} else if (p->type < q->type) {
|
|
|
|
addtotemp(p->type, p->value);
|
|
|
|
lasttype = p++->type;
|
|
|
|
} else { /* *p and *q have same type */
|
2007-10-13 22:09:22 +08:00
|
|
|
int64_t sum = p->value + q->value;
|
2013-12-11 08:24:45 +08:00
|
|
|
if (sum) {
|
2005-01-16 06:15:51 +08:00
|
|
|
addtotemp(p->type, sum);
|
2013-12-11 08:24:45 +08:00
|
|
|
if (hint)
|
|
|
|
hint->type = EAH_SUMMED;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
lasttype = p->type;
|
|
|
|
p++, q++;
|
|
|
|
}
|
|
|
|
if (lasttype == EXPR_UNKNOWN) {
|
|
|
|
return finishtemp();
|
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
|
|
|
|
addtotemp(p->type, p->value);
|
|
|
|
p++;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
|
|
|
|
addtotemp(q->type, q->value);
|
|
|
|
q++;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return finishtemp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multiply a vector by a scalar. Strip far-absolute segment part
|
|
|
|
* if present.
|
|
|
|
*
|
|
|
|
* Explicit treatment of UNKNOWN is not required in this routine,
|
|
|
|
* since it will silently do the Right Thing anyway.
|
|
|
|
*
|
|
|
|
* If `affect_hints' is set, we also change the hint type to
|
|
|
|
* NOTBASE if a MAKEBASE hint points at a register being
|
|
|
|
* multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
|
|
|
|
* as the base register.
|
|
|
|
*/
|
2007-10-13 22:09:22 +08:00
|
|
|
static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *p = vect;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
|
|
|
|
p->value = scalar * (p->value);
|
|
|
|
if (hint && hint->type == EAH_MAKEBASE &&
|
|
|
|
p->type == hint->base && affect_hints)
|
|
|
|
hint->type = EAH_NOTBASE;
|
|
|
|
p++;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
p->type = 0;
|
|
|
|
|
|
|
|
return vect;
|
|
|
|
}
|
|
|
|
|
2007-10-13 22:09:22 +08:00
|
|
|
static expr *scalarvect(int64_t scalar)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
begintemp();
|
|
|
|
addtotemp(EXPR_SIMPLE, scalar);
|
|
|
|
return finishtemp();
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
static expr *unknown_expr(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
begintemp();
|
|
|
|
addtotemp(EXPR_UNKNOWN, 1L);
|
|
|
|
return finishtemp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The SEG operator: calculate the segment part of a relocatable
|
|
|
|
* value. Return NULL, as usual, if an error occurs. Report the
|
|
|
|
* error too.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
static expr *segment_part(expr * e)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t seg;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
if (is_unknown(e))
|
2005-01-16 06:15:51 +08:00
|
|
|
return unknown_expr();
|
2002-05-01 04:52:49 +08:00
|
|
|
|
|
|
|
if (!is_reloc(e)) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("cannot apply SEG to a non-relocatable value");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
seg = reloc_seg(e);
|
|
|
|
if (seg == NO_SEG) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("cannot apply SEG to a non-relocatable value");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:52:49 +08:00
|
|
|
} else if (seg & SEG_ABS) {
|
2005-01-16 06:15:51 +08:00
|
|
|
return scalarvect(seg & ~SEG_ABS);
|
2002-05-01 04:52:49 +08:00
|
|
|
} else if (seg & 1) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("SEG applied to something which"
|
|
|
|
" is already a segment base");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
} else {
|
2016-02-18 17:18:50 +08:00
|
|
|
int32_t base = ofmt->segbase(seg + 1);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
begintemp();
|
|
|
|
addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
|
|
|
|
1L);
|
|
|
|
return finishtemp();
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursive-descent parser. Called with a single boolean operand,
|
2007-10-11 05:58:45 +08:00
|
|
|
* which is true if the evaluation is critical (i.e. unresolved
|
2018-11-29 02:33:16 +08:00
|
|
|
* symbols are an error condition). Must update the global `tt' to
|
2002-05-01 04:52:49 +08:00
|
|
|
* reflect the token after the parsed string. May return NULL.
|
|
|
|
*
|
|
|
|
* evaluate() should report its own errors: on return it is assumed
|
|
|
|
* that if NULL has been returned, the error has already been
|
|
|
|
* reported.
|
2018-11-29 02:07:19 +08:00
|
|
|
*
|
2002-05-01 04:52:49 +08:00
|
|
|
*/
|
|
|
|
|
2018-11-29 02:27:30 +08:00
|
|
|
/*
|
|
|
|
* Wrapper function around the scanner
|
|
|
|
*/
|
|
|
|
static int scan(void)
|
|
|
|
{
|
2018-11-29 02:33:16 +08:00
|
|
|
return tt = scanfunc(scpriv, tokval);
|
2018-11-29 02:27:30 +08:00
|
|
|
}
|
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
/*
|
|
|
|
* Grammar parsed is:
|
|
|
|
*
|
|
|
|
* expr : bexpr [ WRT expr6 ]
|
2018-11-29 05:13:16 +08:00
|
|
|
* bexpr : cexpr
|
|
|
|
* cexpr : rexp0 [ {?} bexpr {:} cexpr ]
|
2002-05-01 04:52:49 +08:00
|
|
|
* rexp0 : rexp1 [ {||} rexp1...]
|
|
|
|
* rexp1 : rexp2 [ {^^} rexp2...]
|
|
|
|
* rexp2 : rexp3 [ {&&} rexp3...]
|
2018-11-29 05:13:16 +08:00
|
|
|
* rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=,<=>} expr0... ]
|
2002-05-01 04:52:49 +08:00
|
|
|
* expr0 : expr1 [ {|} expr1...]
|
|
|
|
* expr1 : expr2 [ {^} expr2...]
|
|
|
|
* expr2 : expr3 [ {&} expr3...]
|
2018-11-29 02:07:19 +08:00
|
|
|
* expr3 : expr4 [ {<<,>>,<<<,>>>} expr4...]
|
2002-05-01 04:52:49 +08:00
|
|
|
* expr4 : expr5 [ {+,-} expr5...]
|
|
|
|
* expr5 : expr6 [ {*,/,%,//,%%} expr6...]
|
2012-06-01 01:25:37 +08:00
|
|
|
* expr6 : { ~,+,-,IFUNC,SEG } expr6
|
2002-05-01 04:52:49 +08:00
|
|
|
* | (bexpr)
|
|
|
|
* | symbol
|
|
|
|
* | $
|
|
|
|
* | number
|
|
|
|
*/
|
|
|
|
|
2018-11-29 05:13:16 +08:00
|
|
|
static expr *cexpr(void);
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *rexp0(void), *rexp1(void), *rexp2(void), *rexp3(void);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr0(void), *expr1(void), *expr2(void), *expr3(void);
|
|
|
|
static expr *expr4(void), *expr5(void), *expr6(void);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-11-29 02:07:19 +08:00
|
|
|
/* This inline is a placeholder for the root of the basic expression */
|
2018-11-29 02:19:50 +08:00
|
|
|
static inline expr *bexpr(void)
|
2018-11-29 02:07:19 +08:00
|
|
|
{
|
2018-11-29 05:13:16 +08:00
|
|
|
return cexpr();
|
|
|
|
}
|
|
|
|
|
|
|
|
static expr *cexpr(void)
|
|
|
|
{
|
|
|
|
expr *e, *f, *g;
|
|
|
|
|
|
|
|
e = rexp0();
|
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
|
2018-12-14 16:50:34 +08:00
|
|
|
if (tt == TOKEN_QMARK) {
|
2018-11-29 05:13:16 +08:00
|
|
|
scan();
|
|
|
|
f = bexpr();
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (tt != ':') {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`?' without matching `:'");
|
2018-11-29 05:13:16 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
scan();
|
|
|
|
g = cexpr();
|
|
|
|
if (!g)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (is_simple(e)) {
|
|
|
|
e = reloc_value(e) ? f : g;
|
|
|
|
} else if (is_just_unknown(e)) {
|
|
|
|
e = unknown_expr();
|
|
|
|
} else {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("the left-hand side of `?' must be "
|
|
|
|
"a scalar value");
|
2018-11-29 05:13:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
2018-11-29 02:07:19 +08:00
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *rexp0(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = rexp1();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == TOKEN_DBL_OR) {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = rexp1();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (!(is_simple(e) || is_just_unknown(e)) ||
|
|
|
|
!(is_simple(f) || is_just_unknown(f))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`|' operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *rexp1(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = rexp2();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == TOKEN_DBL_XOR) {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = rexp2();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (!(is_simple(e) || is_just_unknown(e)) ||
|
|
|
|
!(is_simple(f) || is_just_unknown(f))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`^' operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *rexp2(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = rexp3();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == TOKEN_DBL_AND) {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = rexp3();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (!(is_simple(e) || is_just_unknown(e)) ||
|
|
|
|
!(is_simple(f) || is_just_unknown(f))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`&' operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *rexp3(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
2007-10-13 22:09:22 +08:00
|
|
|
int64_t v;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr0();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == TOKEN_EQ || tt == TOKEN_LT || tt == TOKEN_GT ||
|
|
|
|
tt == TOKEN_NE || tt == TOKEN_LE || tt == TOKEN_GE ||
|
|
|
|
tt == TOKEN_LEG) {
|
|
|
|
int tto = tt;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr0();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
e = add_vectors(e, scalar_mult(f, -1L, false));
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
switch (tto) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case TOKEN_EQ:
|
|
|
|
case TOKEN_NE:
|
|
|
|
if (is_unknown(e))
|
|
|
|
v = -1; /* means unknown */
|
|
|
|
else if (!is_really_simple(e) || reloc_value(e) != 0)
|
2018-11-29 02:33:16 +08:00
|
|
|
v = (tto == TOKEN_NE); /* unequal, so return true if NE */
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2018-11-29 02:33:16 +08:00
|
|
|
v = (tto == TOKEN_EQ); /* equal, so return true if EQ */
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (is_unknown(e))
|
|
|
|
v = -1; /* means unknown */
|
|
|
|
else if (!is_really_simple(e)) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`%s': operands differ by a non-scalar",
|
|
|
|
(tto == TOKEN_LE ? "<=" :
|
|
|
|
tto == TOKEN_LT ? "<" :
|
|
|
|
tto == TOKEN_GE ? ">=" :
|
|
|
|
tto == TOKEN_GT ? ">" :
|
|
|
|
tto == TOKEN_LEG ? "<=>" :
|
|
|
|
"<internal error>"));
|
2005-01-16 06:15:51 +08:00
|
|
|
v = 0; /* must set it to _something_ */
|
|
|
|
} else {
|
2010-11-07 04:04:12 +08:00
|
|
|
int64_t vv = reloc_value(e);
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tto == TOKEN_LEG)
|
2018-06-16 09:46:11 +08:00
|
|
|
v = (vv < 0) ? -1 : (vv > 0) ? 1 : 0;
|
|
|
|
else if (vv == 0)
|
2018-11-29 02:33:16 +08:00
|
|
|
v = (tto == TOKEN_LE || tto == TOKEN_GE);
|
2005-01-16 06:15:51 +08:00
|
|
|
else if (vv > 0)
|
2018-11-29 02:33:16 +08:00
|
|
|
v = (tto == TOKEN_GE || tto == TOKEN_GT);
|
2005-01-16 06:15:51 +08:00
|
|
|
else /* vv < 0 */
|
2018-11-29 02:33:16 +08:00
|
|
|
v = (tto == TOKEN_LE || tto == TOKEN_LT);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v == -1)
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
|
|
|
e = scalarvect(v);
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr0(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr1();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == '|') {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr1();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (!(is_simple(e) || is_just_unknown(e)) ||
|
|
|
|
!(is_simple(f) || is_just_unknown(f))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`|' operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
|
|
|
e = scalarvect(reloc_value(e) | reloc_value(f));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr1(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr2();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == '^') {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr2();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (!(is_simple(e) || is_just_unknown(e)) ||
|
|
|
|
!(is_simple(f) || is_just_unknown(f))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`^' operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
|
|
|
e = scalarvect(reloc_value(e) ^ reloc_value(f));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr2(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr3();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == '&') {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr3();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (!(is_simple(e) || is_just_unknown(e)) ||
|
|
|
|
!(is_simple(f) || is_just_unknown(f))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`&' operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
|
|
|
e = scalarvect(reloc_value(e) & reloc_value(f));
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr3(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr4();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == TOKEN_SHL || tt == TOKEN_SHR || tt == TOKEN_SAR) {
|
|
|
|
int tto = tt;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr4();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
if (!(is_simple(e) || is_just_unknown(e)) ||
|
|
|
|
!(is_simple(f) || is_just_unknown(f))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("shift operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
} else if (is_just_unknown(e) || is_just_unknown(f)) {
|
|
|
|
e = unknown_expr();
|
2018-06-16 09:37:32 +08:00
|
|
|
} else {
|
2018-11-29 02:33:16 +08:00
|
|
|
switch (tto) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case TOKEN_SHL:
|
|
|
|
e = scalarvect(reloc_value(e) << reloc_value(f));
|
|
|
|
break;
|
|
|
|
case TOKEN_SHR:
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect(((uint64_t)reloc_value(e)) >>
|
2005-01-16 06:15:51 +08:00
|
|
|
reloc_value(f));
|
|
|
|
break;
|
2018-06-16 09:37:32 +08:00
|
|
|
case TOKEN_SAR:
|
|
|
|
e = scalarvect(((int64_t)reloc_value(e)) >>
|
|
|
|
reloc_value(f));
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2018-06-16 09:37:32 +08:00
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr4(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr5();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == '+' || tt == '-') {
|
|
|
|
int tto = tt;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr5();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
2018-11-29 02:33:16 +08:00
|
|
|
switch (tto) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case '+':
|
|
|
|
e = add_vectors(e, f);
|
|
|
|
break;
|
|
|
|
case '-':
|
2007-10-11 05:58:45 +08:00
|
|
|
e = add_vectors(e, scalar_mult(f, -1L, false));
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr5(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e, *f;
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr6();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2018-11-29 02:33:16 +08:00
|
|
|
while (tt == '*' || tt == '/' || tt == '%' ||
|
|
|
|
tt == TOKEN_SDIV || tt == TOKEN_SMOD) {
|
|
|
|
int tto = tt;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr6();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tto != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
|
2005-01-16 06:15:51 +08:00
|
|
|
!(is_simple(f) || is_just_unknown(f)))) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("division operator may only be applied to"
|
|
|
|
" scalar values");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tto != '*' && !is_just_unknown(f) && reloc_value(f) == 0) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("division by zero");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-11-29 02:33:16 +08:00
|
|
|
switch (tto) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case '*':
|
|
|
|
if (is_simple(e))
|
2007-10-11 05:58:45 +08:00
|
|
|
e = scalar_mult(f, reloc_value(e), true);
|
2005-01-16 06:15:51 +08:00
|
|
|
else if (is_simple(f))
|
2007-10-11 05:58:45 +08:00
|
|
|
e = scalar_mult(e, reloc_value(f), true);
|
2005-01-16 06:15:51 +08:00
|
|
|
else if (is_just_unknown(e) && is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("unable to multiply two "
|
|
|
|
"non-scalar objects");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect(((uint64_t)reloc_value(e)) /
|
|
|
|
((uint64_t)reloc_value(f)));
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect(((uint64_t)reloc_value(e)) %
|
|
|
|
((uint64_t)reloc_value(f)));
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case TOKEN_SDIV:
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect(((int64_t)reloc_value(e)) /
|
|
|
|
((int64_t)reloc_value(f)));
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case TOKEN_SMOD:
|
|
|
|
if (is_just_unknown(e) || is_just_unknown(f))
|
|
|
|
e = unknown_expr();
|
|
|
|
else
|
2007-10-13 22:09:22 +08:00
|
|
|
e = scalarvect(((int64_t)reloc_value(e)) %
|
|
|
|
((int64_t)reloc_value(f)));
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
static expr *eval_floatize(enum floatize type)
|
|
|
|
{
|
2009-11-01 01:05:32 +08:00
|
|
|
uint8_t result[16], *p; /* Up to 128 bits */
|
2007-09-25 03:30:54 +08:00
|
|
|
static const struct {
|
2009-11-01 01:05:32 +08:00
|
|
|
int bytes, start, len;
|
2007-09-25 03:30:54 +08:00
|
|
|
} formats[] = {
|
2009-11-01 01:05:32 +08:00
|
|
|
{ 1, 0, 1 }, /* FLOAT_8 */
|
|
|
|
{ 2, 0, 2 }, /* FLOAT_16 */
|
|
|
|
{ 4, 0, 4 }, /* FLOAT_32 */
|
|
|
|
{ 8, 0, 8 }, /* FLOAT_64 */
|
|
|
|
{ 10, 0, 8 }, /* FLOAT_80M */
|
|
|
|
{ 10, 8, 2 }, /* FLOAT_80E */
|
|
|
|
{ 16, 0, 8 }, /* FLOAT_128L */
|
|
|
|
{ 16, 8, 8 }, /* FLOAT_128H */
|
2007-09-25 03:30:54 +08:00
|
|
|
};
|
|
|
|
int sign = 1;
|
|
|
|
int64_t val;
|
2018-11-29 02:33:16 +08:00
|
|
|
int i;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt != '(') {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("expecting `('");
|
2009-11-01 01:05:32 +08:00
|
|
|
return NULL;
|
2007-09-25 03:30:54 +08:00
|
|
|
}
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt == '-' || tt == '+') {
|
|
|
|
sign = (tt == '-') ? -1 : 1;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2007-09-25 03:30:54 +08:00
|
|
|
}
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt != TOKEN_FLOAT) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("expecting floating-point number");
|
2009-11-01 01:05:32 +08:00
|
|
|
return NULL;
|
2007-09-25 03:30:54 +08:00
|
|
|
}
|
2016-02-18 12:27:41 +08:00
|
|
|
if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
|
2009-11-01 01:05:32 +08:00
|
|
|
return NULL;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt != ')') {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("expecting `)'");
|
2009-11-01 01:05:32 +08:00
|
|
|
return NULL;
|
2007-09-25 03:30:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p = result+formats[type].start+formats[type].len;
|
|
|
|
val = 0;
|
2018-11-29 02:33:16 +08:00
|
|
|
for (i = formats[type].len; i; i--) {
|
2009-11-01 01:05:32 +08:00
|
|
|
p--;
|
|
|
|
val = (val << 8) + *p;
|
2007-09-25 03:30:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
begintemp();
|
|
|
|
addtotemp(EXPR_SIMPLE, val);
|
|
|
|
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2007-09-25 03:30:54 +08:00
|
|
|
return finishtemp();
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:17:16 +08:00
|
|
|
static expr *eval_strfunc(enum strfunc type, const char *name)
|
2008-06-15 12:08:38 +08:00
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
size_t string_len;
|
|
|
|
int64_t val;
|
|
|
|
bool parens, rn_warn;
|
|
|
|
|
2008-06-25 17:41:40 +08:00
|
|
|
parens = false;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt == '(') {
|
2009-11-01 01:05:32 +08:00
|
|
|
parens = true;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2008-06-15 12:08:38 +08:00
|
|
|
}
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt != TOKEN_STR) {
|
2019-06-07 08:17:16 +08:00
|
|
|
nasm_nonfatal("expecting string as argument to %s", name);
|
2009-11-01 01:05:32 +08:00
|
|
|
return NULL;
|
2008-06-15 12:08:38 +08:00
|
|
|
}
|
|
|
|
string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
|
2009-11-01 01:05:32 +08:00
|
|
|
&string, type);
|
2008-06-15 12:08:38 +08:00
|
|
|
if (string_len == (size_t)-1) {
|
2019-06-07 08:17:16 +08:00
|
|
|
nasm_nonfatal("invalid input string to %s", name);
|
2009-11-01 01:05:32 +08:00
|
|
|
return NULL;
|
2008-06-15 12:08:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
val = readstrnum(string, string_len, &rn_warn);
|
|
|
|
if (parens) {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt != ')') {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("expecting `)'");
|
2009-11-01 01:05:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-06-15 12:08:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rn_warn)
|
2018-12-19 04:25:11 +08:00
|
|
|
nasm_warn(WARN_OTHER, "character constant too long");
|
2008-06-15 12:08:38 +08:00
|
|
|
|
|
|
|
begintemp();
|
|
|
|
addtotemp(EXPR_SIMPLE, val);
|
|
|
|
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2008-06-15 12:08:38 +08:00
|
|
|
return finishtemp();
|
|
|
|
}
|
|
|
|
|
2012-06-01 01:25:37 +08:00
|
|
|
static int64_t eval_ifunc(int64_t val, enum ifunc func)
|
|
|
|
{
|
|
|
|
uint64_t uval = (uint64_t)val;
|
|
|
|
int64_t rv;
|
|
|
|
|
|
|
|
switch (func) {
|
|
|
|
case IFUNC_ILOG2E:
|
|
|
|
case IFUNC_ILOG2W:
|
2013-02-18 05:38:11 +08:00
|
|
|
if (!is_power2(uval))
|
2018-12-15 05:44:35 +08:00
|
|
|
nasm_error((func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING|WARN_OTHER,
|
2018-12-02 00:54:15 +08:00
|
|
|
"ilog2 argument is not a power of two");
|
2012-06-01 01:25:37 +08:00
|
|
|
/* fall through */
|
|
|
|
case IFUNC_ILOG2F:
|
|
|
|
rv = ilog2_64(uval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IFUNC_ILOG2C:
|
|
|
|
rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-06-16 09:20:17 +08:00
|
|
|
nasm_panic("invalid IFUNC token %d", func);
|
2012-06-01 01:25:37 +08:00
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
static expr *expr6(void)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t type;
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e;
|
2007-11-06 09:19:32 +08:00
|
|
|
int32_t label_seg;
|
|
|
|
int64_t label_ofs;
|
2008-06-10 11:45:19 +08:00
|
|
|
int64_t tmpval;
|
|
|
|
bool rn_warn;
|
2018-06-02 09:02:54 +08:00
|
|
|
const char *scope;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-06-13 04:50:37 +08:00
|
|
|
if (++deadman > nasm_limit[LIMIT_EVAL]) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("expression too long");
|
2018-06-13 04:50:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
switch (tt) {
|
2007-09-25 01:51:07 +08:00
|
|
|
case '-':
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr6();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!e)
|
|
|
|
return NULL;
|
2007-10-11 05:58:45 +08:00
|
|
|
return scalar_mult(e, -1L, false);
|
2007-09-25 01:51:07 +08:00
|
|
|
|
|
|
|
case '+':
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
return expr6();
|
2007-09-25 01:51:07 +08:00
|
|
|
|
|
|
|
case '~':
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr6();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
if (is_just_unknown(e))
|
|
|
|
return unknown_expr();
|
|
|
|
else if (!is_simple(e)) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`~' operator may only be applied to"
|
2005-01-16 06:15:51 +08:00
|
|
|
" scalar values");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return scalarvect(~reloc_value(e));
|
2007-09-25 01:51:07 +08:00
|
|
|
|
|
|
|
case '!':
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr6();
|
2007-05-02 12:21:26 +08:00
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
if (is_just_unknown(e))
|
|
|
|
return unknown_expr();
|
|
|
|
else if (!is_simple(e)) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("`!' operator may only be applied to"
|
2007-05-02 12:21:26 +08:00
|
|
|
" scalar values");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return scalarvect(!reloc_value(e));
|
2007-09-25 01:51:07 +08:00
|
|
|
|
2012-06-01 01:25:37 +08:00
|
|
|
case TOKEN_IFUNC:
|
|
|
|
{
|
|
|
|
enum ifunc func = tokval->t_integer;
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr6();
|
2012-06-01 01:25:37 +08:00
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
if (is_just_unknown(e))
|
|
|
|
return unknown_expr();
|
|
|
|
else if (!is_simple(e)) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("function may only be applied to"
|
2012-06-01 01:25:37 +08:00
|
|
|
" scalar values");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return scalarvect(eval_ifunc(reloc_value(e), func));
|
|
|
|
}
|
|
|
|
|
2007-09-25 01:51:07 +08:00
|
|
|
case TOKEN_SEG:
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
e = expr6();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
e = segment_part(e);
|
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
if (is_unknown(e) && critical) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("unable to determine segment base");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return e;
|
2007-09-25 01:51:07 +08:00
|
|
|
|
2007-09-25 03:30:54 +08:00
|
|
|
case TOKEN_FLOATIZE:
|
2009-11-01 01:05:32 +08:00
|
|
|
return eval_floatize(tokval->t_integer);
|
2007-09-25 03:30:54 +08:00
|
|
|
|
2008-06-15 12:08:38 +08:00
|
|
|
case TOKEN_STRFUNC:
|
2019-06-07 08:17:16 +08:00
|
|
|
return eval_strfunc(tokval->t_integer, tokval->t_charptr);
|
2008-06-15 12:08:38 +08:00
|
|
|
|
2007-09-25 01:51:07 +08:00
|
|
|
case '(':
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2018-11-29 02:19:50 +08:00
|
|
|
e = bexpr();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!e)
|
|
|
|
return NULL;
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt != ')') {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("expecting `)'");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2005-01-16 06:15:51 +08:00
|
|
|
return e;
|
2007-09-25 01:51:07 +08:00
|
|
|
|
|
|
|
case TOKEN_NUM:
|
2008-06-10 11:45:19 +08:00
|
|
|
case TOKEN_STR:
|
2007-09-25 01:51:07 +08:00
|
|
|
case TOKEN_REG:
|
|
|
|
case TOKEN_ID:
|
2009-11-01 01:05:32 +08:00
|
|
|
case TOKEN_INSN: /* Opcodes that occur here are really labels */
|
2007-09-25 01:51:07 +08:00
|
|
|
case TOKEN_HERE:
|
|
|
|
case TOKEN_BASE:
|
2013-08-06 11:46:18 +08:00
|
|
|
case TOKEN_DECORATOR:
|
2005-01-16 06:15:51 +08:00
|
|
|
begintemp();
|
2018-11-29 02:33:16 +08:00
|
|
|
switch (tt) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case TOKEN_NUM:
|
|
|
|
addtotemp(EXPR_SIMPLE, tokval->t_integer);
|
|
|
|
break;
|
2009-11-01 01:05:32 +08:00
|
|
|
case TOKEN_STR:
|
|
|
|
tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
|
|
|
|
if (rn_warn)
|
2018-12-19 04:25:11 +08:00
|
|
|
nasm_warn(WARN_OTHER, "character constant too long");
|
2008-06-10 11:45:19 +08:00
|
|
|
addtotemp(EXPR_SIMPLE, tmpval);
|
2009-11-01 01:05:32 +08:00
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
case TOKEN_REG:
|
|
|
|
addtotemp(tokval->t_integer, 1L);
|
|
|
|
if (hint && hint->type == EAH_NOHINT)
|
|
|
|
hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
|
|
|
|
break;
|
|
|
|
case TOKEN_ID:
|
2009-11-01 01:05:32 +08:00
|
|
|
case TOKEN_INSN:
|
2005-01-16 06:15:51 +08:00
|
|
|
case TOKEN_HERE:
|
|
|
|
case TOKEN_BASE:
|
|
|
|
/*
|
2016-02-18 17:25:46 +08:00
|
|
|
* If !location.known, this indicates that no
|
2005-01-16 06:15:51 +08:00
|
|
|
* symbol, Here or Base references are valid because we
|
|
|
|
* are in preprocess-only mode.
|
|
|
|
*/
|
2016-02-18 17:25:46 +08:00
|
|
|
if (!location.known) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("%s not supported in preprocess-only mode",
|
|
|
|
(tt == TOKEN_HERE ? "`$'" :
|
|
|
|
tt == TOKEN_BASE ? "`$$'" :
|
|
|
|
"symbol references"));
|
2005-01-16 06:15:51 +08:00
|
|
|
addtotemp(EXPR_UNKNOWN, 1L);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt == TOKEN_BASE) {
|
2017-03-08 11:23:03 +08:00
|
|
|
label_seg = in_absolute ? absolute.segment : location.segment;
|
2005-01-16 06:15:51 +08:00
|
|
|
label_ofs = 0;
|
2018-11-29 02:33:16 +08:00
|
|
|
} else if (tt == TOKEN_HERE) {
|
2017-03-08 11:23:03 +08:00
|
|
|
label_seg = in_absolute ? absolute.segment : location.segment;
|
|
|
|
label_ofs = in_absolute ? absolute.offset : location.offset;
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
2016-02-18 17:25:46 +08:00
|
|
|
if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
|
2008-03-13 13:39:03 +08:00
|
|
|
scope = local_scope(tokval->t_charptr);
|
2018-12-19 03:12:46 +08:00
|
|
|
if (critical) {
|
|
|
|
nasm_nonfatal("symbol `%s%s' not defined%s",
|
|
|
|
scope,tokval->t_charptr,
|
|
|
|
pass_first() ? " before use" : "");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-12-19 03:12:46 +08:00
|
|
|
if (opflags)
|
|
|
|
*opflags |= OPFLAG_FORWARD;
|
|
|
|
type = EXPR_UNKNOWN;
|
|
|
|
label_seg = NO_SEG;
|
|
|
|
label_ofs = 1;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
if (opflags && is_extern(tokval->t_charptr))
|
|
|
|
*opflags |= OPFLAG_EXTERN;
|
|
|
|
}
|
|
|
|
addtotemp(type, label_ofs);
|
|
|
|
if (label_seg != NO_SEG)
|
|
|
|
addtotemp(EXPR_SEGBASE + label_seg, 1L);
|
|
|
|
break;
|
2013-08-06 11:46:18 +08:00
|
|
|
case TOKEN_DECORATOR:
|
|
|
|
addtotemp(EXPR_RDSAE, tokval->t_integer);
|
|
|
|
break;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2018-11-29 02:27:30 +08:00
|
|
|
scan();
|
2005-01-16 06:15:51 +08:00
|
|
|
return finishtemp();
|
2007-09-25 01:51:07 +08:00
|
|
|
|
|
|
|
default:
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("expression syntax error");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
|
2018-12-19 03:12:46 +08:00
|
|
|
int *fwref, bool crit, struct eval_hints *hints)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:52:49 +08:00
|
|
|
expr *e;
|
|
|
|
expr *f = NULL;
|
|
|
|
|
2018-06-13 04:50:37 +08:00
|
|
|
deadman = 0;
|
2018-11-29 05:13:16 +08:00
|
|
|
|
2002-05-01 04:52:49 +08:00
|
|
|
hint = hints;
|
|
|
|
if (hint)
|
2005-01-16 06:15:51 +08:00
|
|
|
hint->type = EAH_NOHINT;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-12-19 03:12:46 +08:00
|
|
|
critical = crit;
|
2018-11-29 02:27:30 +08:00
|
|
|
scanfunc = sc;
|
2002-05-01 04:52:49 +08:00
|
|
|
scpriv = scprivate;
|
|
|
|
tokval = tv;
|
2002-05-01 04:53:55 +08:00
|
|
|
opflags = fwref;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
while (ntempexprs) /* initialize temporary storage */
|
2005-01-16 06:15:51 +08:00
|
|
|
nasm_free(tempexprs[--ntempexprs]);
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-11-29 05:13:16 +08:00
|
|
|
tt = tokval->t_type;
|
|
|
|
if (tt == TOKEN_INVALID)
|
|
|
|
scan();
|
|
|
|
|
2018-11-29 02:19:50 +08:00
|
|
|
e = bexpr();
|
2002-05-01 04:52:49 +08:00
|
|
|
if (!e)
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
2002-05-01 04:52:49 +08:00
|
|
|
|
2018-11-29 02:33:16 +08:00
|
|
|
if (tt == TOKEN_WRT) {
|
2018-11-29 02:27:30 +08:00
|
|
|
scan(); /* eat the WRT */
|
2018-11-29 02:19:50 +08:00
|
|
|
f = expr6();
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f)
|
|
|
|
return NULL;
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
2007-10-11 05:58:45 +08:00
|
|
|
e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
|
2002-05-01 04:52:49 +08:00
|
|
|
if (f) {
|
2005-01-16 06:15:51 +08:00
|
|
|
expr *g;
|
|
|
|
if (is_just_unknown(f))
|
|
|
|
g = unknown_expr();
|
|
|
|
else {
|
2007-04-12 10:40:54 +08:00
|
|
|
int64_t value;
|
2005-01-16 06:15:51 +08:00
|
|
|
begintemp();
|
|
|
|
if (!is_reloc(f)) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("invalid right-hand operand to WRT");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
value = reloc_seg(f);
|
|
|
|
if (value == NO_SEG)
|
|
|
|
value = reloc_value(f) | SEG_ABS;
|
|
|
|
else if (!(value & SEG_ABS) && !(value % 2) && critical) {
|
2018-12-02 00:54:15 +08:00
|
|
|
nasm_nonfatal("invalid right-hand operand to WRT");
|
2005-01-16 06:15:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
addtotemp(EXPR_WRT, value);
|
|
|
|
g = finishtemp();
|
|
|
|
}
|
|
|
|
e = add_vectors(e, g);
|
2002-05-01 04:52:49 +08:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|