H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 2 | * |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 6 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 7 | * 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 Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 17 | * |
H. Peter Anvin | 9e6747c | 2009-06-28 17:13:04 -0700 | [diff] [blame] | 18 | * 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 Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
H. Peter Anvin | fe50195 | 2007-10-02 21:53:51 -0700 | [diff] [blame] | 38 | #include "compiler.h" |
| 39 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <stddef.h> |
| 43 | #include <string.h> |
| 44 | #include <ctype.h> |
| 45 | |
| 46 | #include "nasm.h" |
| 47 | #include "nasmlib.h" |
H. Peter Anvin | 0a12606 | 2017-09-27 13:34:42 -0700 | [diff] [blame] | 48 | #include "ilog2.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 49 | #include "error.h" |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 50 | #include "eval.h" |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 51 | #include "labels.h" |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 52 | #include "float.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 53 | #include "assemble.h" |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 54 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 55 | #define TEMPEXPRS_DELTA 128 |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 56 | #define TEMPEXPR_DELTA 8 |
| 57 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 58 | static scanner scan; /* Address of scanner routine */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 59 | |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 60 | static expr **tempexprs = NULL; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 61 | static int ntempexprs; |
| 62 | static int tempexprs_size = 0; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 63 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 64 | static expr *tempexpr; |
| 65 | static int ntempexpr; |
| 66 | static int tempexpr_size; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 67 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 68 | static struct tokenval *tokval; /* The current token */ |
| 69 | static int i; /* The t_type of tokval */ |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 70 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 71 | static void *scpriv; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 72 | static int *opflags; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 73 | |
| 74 | static struct eval_hints *hint; |
H. Peter Anvin | a3d96d0 | 2018-06-15 17:51:39 -0700 | [diff] [blame] | 75 | static int64_t deadman; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 76 | |
H. Peter Anvin | 667dd80 | 2002-05-26 19:49:41 +0000 | [diff] [blame] | 77 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 78 | /* |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 79 | * Unimportant cleanup is done to avoid confusing people who are trying |
| 80 | * to debug real memory leaks |
| 81 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 82 | void eval_cleanup(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 83 | { |
| 84 | while (ntempexprs) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 85 | nasm_free(tempexprs[--ntempexprs]); |
| 86 | nasm_free(tempexprs); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 90 | * Construct a temporary expression. |
| 91 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 92 | static void begintemp(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 93 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 94 | tempexpr = NULL; |
| 95 | tempexpr_size = ntempexpr = 0; |
| 96 | } |
| 97 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 98 | static void addtotemp(int32_t type, int64_t value) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 99 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 100 | while (ntempexpr >= tempexpr_size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 101 | tempexpr_size += TEMPEXPR_DELTA; |
| 102 | tempexpr = nasm_realloc(tempexpr, |
| 103 | tempexpr_size * sizeof(*tempexpr)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 104 | } |
| 105 | tempexpr[ntempexpr].type = type; |
| 106 | tempexpr[ntempexpr++].value = value; |
| 107 | } |
| 108 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 109 | static expr *finishtemp(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 110 | { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 111 | addtotemp(0L, 0L); /* terminate */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 112 | while (ntempexprs >= tempexprs_size) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 113 | tempexprs_size += TEMPEXPRS_DELTA; |
| 114 | tempexprs = nasm_realloc(tempexprs, |
| 115 | tempexprs_size * sizeof(*tempexprs)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 116 | } |
| 117 | return tempexprs[ntempexprs++] = tempexpr; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Add two vector datatypes. We have some bizarre behaviour on far- |
| 122 | * absolute segment types: we preserve them during addition _only_ |
| 123 | * if one of the segments is a truly pure scalar. |
| 124 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 125 | static expr *add_vectors(expr * p, expr * q) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 126 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 127 | int preserve; |
| 128 | |
| 129 | preserve = is_really_simple(p) || is_really_simple(q); |
| 130 | |
| 131 | begintemp(); |
| 132 | |
| 133 | while (p->type && q->type && |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 134 | p->type < EXPR_SEGBASE + SEG_ABS && |
| 135 | q->type < EXPR_SEGBASE + SEG_ABS) { |
| 136 | int lasttype; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 137 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 138 | if (p->type > q->type) { |
| 139 | addtotemp(q->type, q->value); |
| 140 | lasttype = q++->type; |
| 141 | } else if (p->type < q->type) { |
| 142 | addtotemp(p->type, p->value); |
| 143 | lasttype = p++->type; |
| 144 | } else { /* *p and *q have same type */ |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 145 | int64_t sum = p->value + q->value; |
Jin Kyu Song | 4360ba2 | 2013-12-10 16:24:45 -0800 | [diff] [blame] | 146 | if (sum) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 147 | addtotemp(p->type, sum); |
Jin Kyu Song | 4360ba2 | 2013-12-10 16:24:45 -0800 | [diff] [blame] | 148 | if (hint) |
| 149 | hint->type = EAH_SUMMED; |
| 150 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 151 | lasttype = p->type; |
| 152 | p++, q++; |
| 153 | } |
| 154 | if (lasttype == EXPR_UNKNOWN) { |
| 155 | return finishtemp(); |
| 156 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 157 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 158 | while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) { |
| 159 | addtotemp(p->type, p->value); |
| 160 | p++; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 161 | } |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 162 | while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) { |
| 163 | addtotemp(q->type, q->value); |
| 164 | q++; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return finishtemp(); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Multiply a vector by a scalar. Strip far-absolute segment part |
| 172 | * if present. |
| 173 | * |
| 174 | * Explicit treatment of UNKNOWN is not required in this routine, |
| 175 | * since it will silently do the Right Thing anyway. |
| 176 | * |
| 177 | * If `affect_hints' is set, we also change the hint type to |
| 178 | * NOTBASE if a MAKEBASE hint points at a register being |
| 179 | * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX |
| 180 | * as the base register. |
| 181 | */ |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 182 | static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 183 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 184 | expr *p = vect; |
| 185 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 186 | while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) { |
| 187 | p->value = scalar * (p->value); |
| 188 | if (hint && hint->type == EAH_MAKEBASE && |
| 189 | p->type == hint->base && affect_hints) |
| 190 | hint->type = EAH_NOTBASE; |
| 191 | p++; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 192 | } |
| 193 | p->type = 0; |
| 194 | |
| 195 | return vect; |
| 196 | } |
| 197 | |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 198 | static expr *scalarvect(int64_t scalar) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 199 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 200 | begintemp(); |
| 201 | addtotemp(EXPR_SIMPLE, scalar); |
| 202 | return finishtemp(); |
| 203 | } |
| 204 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 205 | static expr *unknown_expr(void) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 206 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 207 | begintemp(); |
| 208 | addtotemp(EXPR_UNKNOWN, 1L); |
| 209 | return finishtemp(); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * The SEG operator: calculate the segment part of a relocatable |
| 214 | * value. Return NULL, as usual, if an error occurs. Report the |
| 215 | * error too. |
| 216 | */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 217 | static expr *segment_part(expr * e) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 218 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 219 | int32_t seg; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 220 | |
| 221 | if (is_unknown(e)) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 222 | return unknown_expr(); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 223 | |
| 224 | if (!is_reloc(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 225 | nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 226 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | seg = reloc_seg(e); |
| 230 | if (seg == NO_SEG) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 231 | nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 232 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 233 | } else if (seg & SEG_ABS) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 234 | return scalarvect(seg & ~SEG_ABS); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 235 | } else if (seg & 1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 236 | nasm_error(ERR_NONFATAL, "SEG applied to something which" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 237 | " is already a segment base"); |
| 238 | return NULL; |
| 239 | } else { |
H. Peter Anvin | 36034ec | 2016-02-18 01:18:50 -0800 | [diff] [blame] | 240 | int32_t base = ofmt->segbase(seg + 1); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 241 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 242 | begintemp(); |
| 243 | addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base), |
| 244 | 1L); |
| 245 | return finishtemp(); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Recursive-descent parser. Called with a single boolean operand, |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 251 | * which is true if the evaluation is critical (i.e. unresolved |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 252 | * symbols are an error condition). Must update the global `i' to |
| 253 | * reflect the token after the parsed string. May return NULL. |
| 254 | * |
| 255 | * evaluate() should report its own errors: on return it is assumed |
| 256 | * that if NULL has been returned, the error has already been |
| 257 | * reported. |
| 258 | */ |
| 259 | |
| 260 | /* |
| 261 | * Grammar parsed is: |
| 262 | * |
| 263 | * expr : bexpr [ WRT expr6 ] |
| 264 | * bexpr : rexp0 or expr0 depending on relative-mode setting |
| 265 | * rexp0 : rexp1 [ {||} rexp1...] |
| 266 | * rexp1 : rexp2 [ {^^} rexp2...] |
| 267 | * rexp2 : rexp3 [ {&&} rexp3...] |
| 268 | * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ] |
| 269 | * expr0 : expr1 [ {|} expr1...] |
| 270 | * expr1 : expr2 [ {^} expr2...] |
| 271 | * expr2 : expr3 [ {&} expr3...] |
| 272 | * expr3 : expr4 [ {<<,>>} expr4...] |
| 273 | * expr4 : expr5 [ {+,-} expr5...] |
| 274 | * expr5 : expr6 [ {*,/,%,//,%%} expr6...] |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 275 | * expr6 : { ~,+,-,IFUNC,SEG } expr6 |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 276 | * | (bexpr) |
| 277 | * | symbol |
| 278 | * | $ |
| 279 | * | number |
| 280 | */ |
| 281 | |
| 282 | static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int); |
| 283 | |
| 284 | static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int); |
| 285 | static expr *expr4(int), *expr5(int), *expr6(int); |
| 286 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 287 | static expr *(*bexpr) (int); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 288 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 289 | static expr *rexp0(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 290 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 291 | expr *e, *f; |
| 292 | |
| 293 | e = rexp1(critical); |
| 294 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 295 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 296 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 297 | while (i == TOKEN_DBL_OR) { |
| 298 | i = scan(scpriv, tokval); |
| 299 | f = rexp1(critical); |
| 300 | if (!f) |
| 301 | return NULL; |
| 302 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 303 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 304 | nasm_error(ERR_NONFATAL, "`|' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 305 | " scalar values"); |
| 306 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 307 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 308 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 309 | e = unknown_expr(); |
| 310 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 311 | e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f))); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 312 | } |
| 313 | return e; |
| 314 | } |
| 315 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 316 | static expr *rexp1(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 317 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 318 | expr *e, *f; |
| 319 | |
| 320 | e = rexp2(critical); |
| 321 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 322 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 323 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 324 | while (i == TOKEN_DBL_XOR) { |
| 325 | i = scan(scpriv, tokval); |
| 326 | f = rexp2(critical); |
| 327 | if (!f) |
| 328 | return NULL; |
| 329 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 330 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 331 | nasm_error(ERR_NONFATAL, "`^' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 332 | " scalar values"); |
| 333 | } |
| 334 | |
| 335 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 336 | e = unknown_expr(); |
| 337 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 338 | e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f))); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 339 | } |
| 340 | return e; |
| 341 | } |
| 342 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 343 | static expr *rexp2(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 344 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 345 | expr *e, *f; |
| 346 | |
| 347 | e = rexp3(critical); |
| 348 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 349 | return NULL; |
| 350 | while (i == TOKEN_DBL_AND) { |
| 351 | i = scan(scpriv, tokval); |
| 352 | f = rexp3(critical); |
| 353 | if (!f) |
| 354 | return NULL; |
| 355 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 356 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 357 | nasm_error(ERR_NONFATAL, "`&' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 358 | " scalar values"); |
| 359 | } |
| 360 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 361 | e = unknown_expr(); |
| 362 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 363 | e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f))); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 364 | } |
| 365 | return e; |
| 366 | } |
| 367 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 368 | static expr *rexp3(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 369 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 370 | expr *e, *f; |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 371 | int64_t v; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 372 | |
| 373 | e = expr0(critical); |
| 374 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 375 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 376 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 377 | while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT || |
H. Peter Anvin | 3bb1dd0 | 2018-06-15 18:46:11 -0700 | [diff] [blame] | 378 | i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE || |
| 379 | i == TOKEN_LEG) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 380 | int j = i; |
| 381 | i = scan(scpriv, tokval); |
| 382 | f = expr0(critical); |
| 383 | if (!f) |
| 384 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 385 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 386 | e = add_vectors(e, scalar_mult(f, -1L, false)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 387 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 388 | switch (j) { |
| 389 | case TOKEN_EQ: |
| 390 | case TOKEN_NE: |
| 391 | if (is_unknown(e)) |
| 392 | v = -1; /* means unknown */ |
| 393 | else if (!is_really_simple(e) || reloc_value(e) != 0) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 394 | v = (j == TOKEN_NE); /* unequal, so return true if NE */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 395 | else |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 396 | v = (j == TOKEN_EQ); /* equal, so return true if EQ */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 397 | break; |
| 398 | default: |
| 399 | if (is_unknown(e)) |
| 400 | v = -1; /* means unknown */ |
| 401 | else if (!is_really_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 402 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 403 | "`%s': operands differ by a non-scalar", |
H. Peter Anvin | 3bb1dd0 | 2018-06-15 18:46:11 -0700 | [diff] [blame] | 404 | (j == TOKEN_LE ? "<=" : |
| 405 | j == TOKEN_LT ? "<" : |
| 406 | j == TOKEN_GE ? ">=" : |
| 407 | j == TOKEN_GT ? ">" : |
| 408 | j == TOKEN_LEG ? "<=>" : |
| 409 | "<internal error>")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 410 | v = 0; /* must set it to _something_ */ |
| 411 | } else { |
Cyrill Gorcunov | 9f135ed | 2010-11-06 23:04:12 +0300 | [diff] [blame] | 412 | int64_t vv = reloc_value(e); |
H. Peter Anvin | 3bb1dd0 | 2018-06-15 18:46:11 -0700 | [diff] [blame] | 413 | if (j == TOKEN_LEG) |
| 414 | v = (vv < 0) ? -1 : (vv > 0) ? 1 : 0; |
| 415 | else if (vv == 0) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 416 | v = (j == TOKEN_LE || j == TOKEN_GE); |
| 417 | else if (vv > 0) |
| 418 | v = (j == TOKEN_GE || j == TOKEN_GT); |
| 419 | else /* vv < 0 */ |
| 420 | v = (j == TOKEN_LE || j == TOKEN_LT); |
| 421 | } |
| 422 | break; |
| 423 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 424 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 425 | if (v == -1) |
| 426 | e = unknown_expr(); |
| 427 | else |
| 428 | e = scalarvect(v); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 429 | } |
| 430 | return e; |
| 431 | } |
| 432 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 433 | static expr *expr0(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 434 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 435 | expr *e, *f; |
| 436 | |
| 437 | e = expr1(critical); |
| 438 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 439 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 440 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 441 | while (i == '|') { |
| 442 | i = scan(scpriv, tokval); |
| 443 | f = expr1(critical); |
| 444 | if (!f) |
| 445 | return NULL; |
| 446 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 447 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 448 | nasm_error(ERR_NONFATAL, "`|' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 449 | " scalar values"); |
| 450 | } |
| 451 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 452 | e = unknown_expr(); |
| 453 | else |
| 454 | e = scalarvect(reloc_value(e) | reloc_value(f)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 455 | } |
| 456 | return e; |
| 457 | } |
| 458 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 459 | static expr *expr1(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 460 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 461 | expr *e, *f; |
| 462 | |
| 463 | e = expr2(critical); |
| 464 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 465 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 466 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 467 | while (i == '^') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 468 | i = scan(scpriv, tokval); |
| 469 | f = expr2(critical); |
| 470 | if (!f) |
| 471 | return NULL; |
| 472 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 473 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 474 | nasm_error(ERR_NONFATAL, "`^' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 475 | " scalar values"); |
| 476 | } |
| 477 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 478 | e = unknown_expr(); |
| 479 | else |
| 480 | e = scalarvect(reloc_value(e) ^ reloc_value(f)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 481 | } |
| 482 | return e; |
| 483 | } |
| 484 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 485 | static expr *expr2(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 486 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 487 | expr *e, *f; |
| 488 | |
| 489 | e = expr3(critical); |
| 490 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 491 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 492 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 493 | while (i == '&') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 494 | i = scan(scpriv, tokval); |
| 495 | f = expr3(critical); |
| 496 | if (!f) |
| 497 | return NULL; |
| 498 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 499 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 500 | nasm_error(ERR_NONFATAL, "`&' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 501 | " scalar values"); |
| 502 | } |
| 503 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 504 | e = unknown_expr(); |
| 505 | else |
| 506 | e = scalarvect(reloc_value(e) & reloc_value(f)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 507 | } |
| 508 | return e; |
| 509 | } |
| 510 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 511 | static expr *expr3(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 512 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 513 | expr *e, *f; |
| 514 | |
| 515 | e = expr4(critical); |
| 516 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 517 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 518 | |
H. Peter Anvin | 94adf7d | 2018-06-15 18:37:32 -0700 | [diff] [blame] | 519 | while (i == TOKEN_SHL || i == TOKEN_SHR || i == TOKEN_SAR) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 520 | int j = i; |
| 521 | i = scan(scpriv, tokval); |
| 522 | f = expr4(critical); |
| 523 | if (!f) |
| 524 | return NULL; |
| 525 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 526 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 527 | nasm_error(ERR_NONFATAL, "shift operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 528 | " scalar values"); |
| 529 | } else if (is_just_unknown(e) || is_just_unknown(f)) { |
| 530 | e = unknown_expr(); |
H. Peter Anvin | 94adf7d | 2018-06-15 18:37:32 -0700 | [diff] [blame] | 531 | } else { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 532 | switch (j) { |
| 533 | case TOKEN_SHL: |
| 534 | e = scalarvect(reloc_value(e) << reloc_value(f)); |
| 535 | break; |
| 536 | case TOKEN_SHR: |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 537 | e = scalarvect(((uint64_t)reloc_value(e)) >> |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 538 | reloc_value(f)); |
| 539 | break; |
H. Peter Anvin | 94adf7d | 2018-06-15 18:37:32 -0700 | [diff] [blame] | 540 | case TOKEN_SAR: |
| 541 | e = scalarvect(((int64_t)reloc_value(e)) >> |
| 542 | reloc_value(f)); |
| 543 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 544 | } |
H. Peter Anvin | 94adf7d | 2018-06-15 18:37:32 -0700 | [diff] [blame] | 545 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 546 | } |
| 547 | return e; |
| 548 | } |
| 549 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 550 | static expr *expr4(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 551 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 552 | expr *e, *f; |
| 553 | |
| 554 | e = expr5(critical); |
| 555 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 556 | return NULL; |
| 557 | while (i == '+' || i == '-') { |
| 558 | int j = i; |
| 559 | i = scan(scpriv, tokval); |
| 560 | f = expr5(critical); |
| 561 | if (!f) |
| 562 | return NULL; |
| 563 | switch (j) { |
| 564 | case '+': |
| 565 | e = add_vectors(e, f); |
| 566 | break; |
| 567 | case '-': |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 568 | e = add_vectors(e, scalar_mult(f, -1L, false)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 569 | break; |
| 570 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 571 | } |
| 572 | return e; |
| 573 | } |
| 574 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 575 | static expr *expr5(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 576 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 577 | expr *e, *f; |
| 578 | |
| 579 | e = expr6(critical); |
| 580 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 581 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 582 | while (i == '*' || i == '/' || i == '%' || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 583 | i == TOKEN_SDIV || i == TOKEN_SMOD) { |
| 584 | int j = i; |
| 585 | i = scan(scpriv, tokval); |
| 586 | f = expr6(critical); |
| 587 | if (!f) |
| 588 | return NULL; |
| 589 | if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) || |
| 590 | !(is_simple(f) || is_just_unknown(f)))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 591 | nasm_error(ERR_NONFATAL, "division operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 592 | " scalar values"); |
| 593 | return NULL; |
| 594 | } |
Cyrill Gorcunov | ceec0d8 | 2018-10-14 01:26:19 +0300 | [diff] [blame] | 595 | if (j != '*' && !is_just_unknown(f) && reloc_value(f) == 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 596 | nasm_error(ERR_NONFATAL, "division by zero"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 597 | return NULL; |
| 598 | } |
| 599 | switch (j) { |
| 600 | case '*': |
| 601 | if (is_simple(e)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 602 | e = scalar_mult(f, reloc_value(e), true); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 603 | else if (is_simple(f)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 604 | e = scalar_mult(e, reloc_value(f), true); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 605 | else if (is_just_unknown(e) && is_just_unknown(f)) |
| 606 | e = unknown_expr(); |
| 607 | else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 608 | nasm_error(ERR_NONFATAL, "unable to multiply two " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 609 | "non-scalar objects"); |
| 610 | return NULL; |
| 611 | } |
| 612 | break; |
| 613 | case '/': |
| 614 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 615 | e = unknown_expr(); |
| 616 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 617 | e = scalarvect(((uint64_t)reloc_value(e)) / |
| 618 | ((uint64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 619 | break; |
| 620 | case '%': |
| 621 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 622 | e = unknown_expr(); |
| 623 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 624 | e = scalarvect(((uint64_t)reloc_value(e)) % |
| 625 | ((uint64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 626 | break; |
| 627 | case TOKEN_SDIV: |
| 628 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 629 | e = unknown_expr(); |
| 630 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 631 | e = scalarvect(((int64_t)reloc_value(e)) / |
| 632 | ((int64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 633 | break; |
| 634 | case TOKEN_SMOD: |
| 635 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 636 | e = unknown_expr(); |
| 637 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 638 | e = scalarvect(((int64_t)reloc_value(e)) % |
| 639 | ((int64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 640 | break; |
| 641 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 642 | } |
| 643 | return e; |
| 644 | } |
| 645 | |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 646 | static expr *eval_floatize(enum floatize type) |
| 647 | { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 648 | uint8_t result[16], *p; /* Up to 128 bits */ |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 649 | static const struct { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 650 | int bytes, start, len; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 651 | } formats[] = { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 652 | { 1, 0, 1 }, /* FLOAT_8 */ |
| 653 | { 2, 0, 2 }, /* FLOAT_16 */ |
| 654 | { 4, 0, 4 }, /* FLOAT_32 */ |
| 655 | { 8, 0, 8 }, /* FLOAT_64 */ |
| 656 | { 10, 0, 8 }, /* FLOAT_80M */ |
| 657 | { 10, 8, 2 }, /* FLOAT_80E */ |
| 658 | { 16, 0, 8 }, /* FLOAT_128L */ |
| 659 | { 16, 8, 8 }, /* FLOAT_128H */ |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 660 | }; |
| 661 | int sign = 1; |
| 662 | int64_t val; |
| 663 | int j; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 664 | |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 665 | i = scan(scpriv, tokval); |
| 666 | if (i != '(') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 667 | nasm_error(ERR_NONFATAL, "expecting `('"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 668 | return NULL; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 669 | } |
| 670 | i = scan(scpriv, tokval); |
| 671 | if (i == '-' || i == '+') { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 672 | sign = (i == '-') ? -1 : 1; |
| 673 | i = scan(scpriv, tokval); |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 674 | } |
| 675 | if (i != TOKEN_FLOAT) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 676 | nasm_error(ERR_NONFATAL, "expecting floating-point number"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 677 | return NULL; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 678 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 679 | if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes)) |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 680 | return NULL; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 681 | i = scan(scpriv, tokval); |
| 682 | if (i != ')') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 683 | nasm_error(ERR_NONFATAL, "expecting `)'"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 684 | return NULL; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | p = result+formats[type].start+formats[type].len; |
| 688 | val = 0; |
| 689 | for (j = formats[type].len; j; j--) { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 690 | p--; |
| 691 | val = (val << 8) + *p; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | begintemp(); |
| 695 | addtotemp(EXPR_SIMPLE, val); |
| 696 | |
| 697 | i = scan(scpriv, tokval); |
| 698 | return finishtemp(); |
| 699 | } |
| 700 | |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 701 | static expr *eval_strfunc(enum strfunc type) |
| 702 | { |
| 703 | char *string; |
| 704 | size_t string_len; |
| 705 | int64_t val; |
| 706 | bool parens, rn_warn; |
| 707 | |
Victor van den Elzen | c7deefa | 2008-06-25 11:41:40 +0200 | [diff] [blame] | 708 | parens = false; |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 709 | i = scan(scpriv, tokval); |
| 710 | if (i == '(') { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 711 | parens = true; |
| 712 | i = scan(scpriv, tokval); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 713 | } |
| 714 | if (i != TOKEN_STR) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 715 | nasm_error(ERR_NONFATAL, "expecting string"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 716 | return NULL; |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 717 | } |
| 718 | string_len = string_transform(tokval->t_charptr, tokval->t_inttwo, |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 719 | &string, type); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 720 | if (string_len == (size_t)-1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 721 | nasm_error(ERR_NONFATAL, "invalid string for transform"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 722 | return NULL; |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | val = readstrnum(string, string_len, &rn_warn); |
| 726 | if (parens) { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 727 | i = scan(scpriv, tokval); |
| 728 | if (i != ')') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 729 | nasm_error(ERR_NONFATAL, "expecting `)'"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 730 | return NULL; |
| 731 | } |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | if (rn_warn) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 735 | nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long"); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 736 | |
| 737 | begintemp(); |
| 738 | addtotemp(EXPR_SIMPLE, val); |
| 739 | |
| 740 | i = scan(scpriv, tokval); |
| 741 | return finishtemp(); |
| 742 | } |
| 743 | |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 744 | static int64_t eval_ifunc(int64_t val, enum ifunc func) |
| 745 | { |
| 746 | int errtype; |
| 747 | uint64_t uval = (uint64_t)val; |
| 748 | int64_t rv; |
| 749 | |
| 750 | switch (func) { |
| 751 | case IFUNC_ILOG2E: |
| 752 | case IFUNC_ILOG2W: |
| 753 | errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING; |
Cyrill Gorcunov | 71ba1f0 | 2013-02-18 01:38:11 +0400 | [diff] [blame] | 754 | |
| 755 | if (!is_power2(uval)) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 756 | nasm_error(errtype, "ilog2 argument is not a power of two"); |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 757 | /* fall through */ |
| 758 | case IFUNC_ILOG2F: |
| 759 | rv = ilog2_64(uval); |
| 760 | break; |
| 761 | |
| 762 | case IFUNC_ILOG2C: |
| 763 | rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1; |
| 764 | break; |
| 765 | |
| 766 | default: |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 767 | nasm_panic("invalid IFUNC token %d", func); |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 768 | rv = 0; |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | return rv; |
| 773 | } |
| 774 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 775 | static expr *expr6(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 776 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 777 | int32_t type; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 778 | expr *e; |
Charles Crayne | 4e8563d | 2007-11-05 17:19:32 -0800 | [diff] [blame] | 779 | int32_t label_seg; |
| 780 | int64_t label_ofs; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 781 | int64_t tmpval; |
| 782 | bool rn_warn; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 783 | const char *scope; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 784 | |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 785 | if (++deadman > nasm_limit[LIMIT_EVAL]) { |
| 786 | nasm_error(ERR_NONFATAL, "expression too long"); |
| 787 | return NULL; |
| 788 | } |
| 789 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 790 | switch (i) { |
| 791 | case '-': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 792 | i = scan(scpriv, tokval); |
| 793 | e = expr6(critical); |
| 794 | if (!e) |
| 795 | return NULL; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 796 | return scalar_mult(e, -1L, false); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 797 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 798 | case '+': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 799 | i = scan(scpriv, tokval); |
| 800 | return expr6(critical); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 801 | |
| 802 | case '~': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 803 | i = scan(scpriv, tokval); |
| 804 | e = expr6(critical); |
| 805 | if (!e) |
| 806 | return NULL; |
| 807 | if (is_just_unknown(e)) |
| 808 | return unknown_expr(); |
| 809 | else if (!is_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 810 | nasm_error(ERR_NONFATAL, "`~' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 811 | " scalar values"); |
| 812 | return NULL; |
| 813 | } |
| 814 | return scalarvect(~reloc_value(e)); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 815 | |
| 816 | case '!': |
Chuck Crayne | cb9bc21 | 2007-05-02 04:21:26 +0000 | [diff] [blame] | 817 | i = scan(scpriv, tokval); |
| 818 | e = expr6(critical); |
| 819 | if (!e) |
| 820 | return NULL; |
| 821 | if (is_just_unknown(e)) |
| 822 | return unknown_expr(); |
| 823 | else if (!is_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 824 | nasm_error(ERR_NONFATAL, "`!' operator may only be applied to" |
Chuck Crayne | cb9bc21 | 2007-05-02 04:21:26 +0000 | [diff] [blame] | 825 | " scalar values"); |
| 826 | return NULL; |
| 827 | } |
| 828 | return scalarvect(!reloc_value(e)); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 829 | |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 830 | case TOKEN_IFUNC: |
| 831 | { |
| 832 | enum ifunc func = tokval->t_integer; |
| 833 | i = scan(scpriv, tokval); |
| 834 | e = expr6(critical); |
| 835 | if (!e) |
| 836 | return NULL; |
| 837 | if (is_just_unknown(e)) |
| 838 | return unknown_expr(); |
| 839 | else if (!is_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 840 | nasm_error(ERR_NONFATAL, "function may only be applied to" |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 841 | " scalar values"); |
| 842 | return NULL; |
| 843 | } |
| 844 | return scalarvect(eval_ifunc(reloc_value(e), func)); |
| 845 | } |
| 846 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 847 | case TOKEN_SEG: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 848 | i = scan(scpriv, tokval); |
| 849 | e = expr6(critical); |
| 850 | if (!e) |
| 851 | return NULL; |
| 852 | e = segment_part(e); |
| 853 | if (!e) |
| 854 | return NULL; |
| 855 | if (is_unknown(e) && critical) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 856 | nasm_error(ERR_NONFATAL, "unable to determine segment base"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 857 | return NULL; |
| 858 | } |
| 859 | return e; |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 860 | |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 861 | case TOKEN_FLOATIZE: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 862 | return eval_floatize(tokval->t_integer); |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 863 | |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 864 | case TOKEN_STRFUNC: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 865 | return eval_strfunc(tokval->t_integer); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 866 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 867 | case '(': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 868 | i = scan(scpriv, tokval); |
| 869 | e = bexpr(critical); |
| 870 | if (!e) |
| 871 | return NULL; |
| 872 | if (i != ')') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 873 | nasm_error(ERR_NONFATAL, "expecting `)'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 874 | return NULL; |
| 875 | } |
| 876 | i = scan(scpriv, tokval); |
| 877 | return e; |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 878 | |
| 879 | case TOKEN_NUM: |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 880 | case TOKEN_STR: |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 881 | case TOKEN_REG: |
| 882 | case TOKEN_ID: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 883 | case TOKEN_INSN: /* Opcodes that occur here are really labels */ |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 884 | case TOKEN_HERE: |
| 885 | case TOKEN_BASE: |
Jin Kyu Song | 72018a2 | 2013-08-05 20:46:18 -0700 | [diff] [blame] | 886 | case TOKEN_DECORATOR: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 887 | begintemp(); |
| 888 | switch (i) { |
| 889 | case TOKEN_NUM: |
| 890 | addtotemp(EXPR_SIMPLE, tokval->t_integer); |
| 891 | break; |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 892 | case TOKEN_STR: |
| 893 | tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn); |
| 894 | if (rn_warn) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 895 | nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long"); |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 896 | addtotemp(EXPR_SIMPLE, tmpval); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 897 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 898 | case TOKEN_REG: |
| 899 | addtotemp(tokval->t_integer, 1L); |
| 900 | if (hint && hint->type == EAH_NOHINT) |
| 901 | hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE; |
| 902 | break; |
| 903 | case TOKEN_ID: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 904 | case TOKEN_INSN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 905 | case TOKEN_HERE: |
| 906 | case TOKEN_BASE: |
| 907 | /* |
H. Peter Anvin | cd7893d | 2016-02-18 01:25:46 -0800 | [diff] [blame] | 908 | * If !location.known, this indicates that no |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 909 | * symbol, Here or Base references are valid because we |
| 910 | * are in preprocess-only mode. |
| 911 | */ |
H. Peter Anvin | cd7893d | 2016-02-18 01:25:46 -0800 | [diff] [blame] | 912 | if (!location.known) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 913 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 914 | "%s not supported in preprocess-only mode", |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 915 | (i == TOKEN_HERE ? "`$'" : |
| 916 | i == TOKEN_BASE ? "`$$'" : |
| 917 | "symbol references")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 918 | addtotemp(EXPR_UNKNOWN, 1L); |
| 919 | break; |
| 920 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 921 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 922 | type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */ |
| 923 | if (i == TOKEN_BASE) { |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 924 | label_seg = in_absolute ? absolute.segment : location.segment; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 925 | label_ofs = 0; |
| 926 | } else if (i == TOKEN_HERE) { |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 927 | label_seg = in_absolute ? absolute.segment : location.segment; |
| 928 | label_ofs = in_absolute ? absolute.offset : location.offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 929 | } else { |
H. Peter Anvin | cd7893d | 2016-02-18 01:25:46 -0800 | [diff] [blame] | 930 | if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) { |
Charles Crayne | d60059e | 2008-03-12 22:39:03 -0700 | [diff] [blame] | 931 | scope = local_scope(tokval->t_charptr); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 932 | if (critical == 2) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 933 | nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined", |
Charles Crayne | d60059e | 2008-03-12 22:39:03 -0700 | [diff] [blame] | 934 | scope,tokval->t_charptr); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 935 | return NULL; |
| 936 | } else if (critical == 1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 937 | nasm_error(ERR_NONFATAL, |
Charles Crayne | d60059e | 2008-03-12 22:39:03 -0700 | [diff] [blame] | 938 | "symbol `%s%s' not defined before use", |
| 939 | scope,tokval->t_charptr); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 940 | return NULL; |
| 941 | } else { |
| 942 | if (opflags) |
Cyrill Gorcunov | e2457c7 | 2010-09-10 01:02:12 +0400 | [diff] [blame] | 943 | *opflags |= OPFLAG_FORWARD; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 944 | type = EXPR_UNKNOWN; |
| 945 | label_seg = NO_SEG; |
| 946 | label_ofs = 1; |
| 947 | } |
| 948 | } |
| 949 | if (opflags && is_extern(tokval->t_charptr)) |
| 950 | *opflags |= OPFLAG_EXTERN; |
| 951 | } |
| 952 | addtotemp(type, label_ofs); |
| 953 | if (label_seg != NO_SEG) |
| 954 | addtotemp(EXPR_SEGBASE + label_seg, 1L); |
| 955 | break; |
Jin Kyu Song | 72018a2 | 2013-08-05 20:46:18 -0700 | [diff] [blame] | 956 | case TOKEN_DECORATOR: |
| 957 | addtotemp(EXPR_RDSAE, tokval->t_integer); |
| 958 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 959 | } |
| 960 | i = scan(scpriv, tokval); |
| 961 | return finishtemp(); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 962 | |
| 963 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 964 | nasm_error(ERR_NONFATAL, "expression syntax error"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 965 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 969 | expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 970 | int *fwref, int critical, struct eval_hints *hints) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 971 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 972 | expr *e; |
| 973 | expr *f = NULL; |
| 974 | |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 975 | deadman = 0; |
| 976 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 977 | hint = hints; |
| 978 | if (hint) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 979 | hint->type = EAH_NOHINT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 980 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 981 | if (critical & CRITICAL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 982 | critical &= ~CRITICAL; |
| 983 | bexpr = rexp0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 984 | } else |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 985 | bexpr = expr0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 986 | |
| 987 | scan = sc; |
| 988 | scpriv = scprivate; |
| 989 | tokval = tv; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 990 | opflags = fwref; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 991 | |
| 992 | if (tokval->t_type == TOKEN_INVALID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 993 | i = scan(scpriv, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 994 | else |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 995 | i = tokval->t_type; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 996 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 997 | while (ntempexprs) /* initialize temporary storage */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 998 | nasm_free(tempexprs[--ntempexprs]); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 999 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1000 | e = bexpr(critical); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1001 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1002 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1003 | |
| 1004 | if (i == TOKEN_WRT) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1005 | i = scan(scpriv, tokval); /* eat the WRT */ |
| 1006 | f = expr6(critical); |
| 1007 | if (!f) |
| 1008 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1009 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 1010 | e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1011 | if (f) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1012 | expr *g; |
| 1013 | if (is_just_unknown(f)) |
| 1014 | g = unknown_expr(); |
| 1015 | else { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 1016 | int64_t value; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1017 | begintemp(); |
| 1018 | if (!is_reloc(f)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1019 | nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1020 | return NULL; |
| 1021 | } |
| 1022 | value = reloc_seg(f); |
| 1023 | if (value == NO_SEG) |
| 1024 | value = reloc_value(f) | SEG_ABS; |
| 1025 | else if (!(value & SEG_ABS) && !(value % 2) && critical) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1026 | nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1027 | return NULL; |
| 1028 | } |
| 1029 | addtotemp(EXPR_WRT, value); |
| 1030 | g = finishtemp(); |
| 1031 | } |
| 1032 | e = add_vectors(e, g); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1033 | } |
| 1034 | return e; |
| 1035 | } |