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 | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 75 | static int 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 | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 378 | i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) { |
| 379 | int j = i; |
| 380 | i = scan(scpriv, tokval); |
| 381 | f = expr0(critical); |
| 382 | if (!f) |
| 383 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 384 | |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 385 | e = add_vectors(e, scalar_mult(f, -1L, false)); |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 386 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 387 | switch (j) { |
| 388 | case TOKEN_EQ: |
| 389 | case TOKEN_NE: |
| 390 | if (is_unknown(e)) |
| 391 | v = -1; /* means unknown */ |
| 392 | else if (!is_really_simple(e) || reloc_value(e) != 0) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 393 | v = (j == TOKEN_NE); /* unequal, so return true if NE */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 394 | else |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 395 | v = (j == TOKEN_EQ); /* equal, so return true if EQ */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 396 | break; |
| 397 | default: |
| 398 | if (is_unknown(e)) |
| 399 | v = -1; /* means unknown */ |
| 400 | else if (!is_really_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 401 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 402 | "`%s': operands differ by a non-scalar", |
| 403 | (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j == |
| 404 | TOKEN_GE ? ">=" : ">")); |
| 405 | v = 0; /* must set it to _something_ */ |
| 406 | } else { |
Cyrill Gorcunov | 9f135ed | 2010-11-06 23:04:12 +0300 | [diff] [blame] | 407 | int64_t vv = reloc_value(e); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 408 | if (vv == 0) |
| 409 | v = (j == TOKEN_LE || j == TOKEN_GE); |
| 410 | else if (vv > 0) |
| 411 | v = (j == TOKEN_GE || j == TOKEN_GT); |
| 412 | else /* vv < 0 */ |
| 413 | v = (j == TOKEN_LE || j == TOKEN_LT); |
| 414 | } |
| 415 | break; |
| 416 | } |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 417 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 418 | if (v == -1) |
| 419 | e = unknown_expr(); |
| 420 | else |
| 421 | e = scalarvect(v); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 422 | } |
| 423 | return e; |
| 424 | } |
| 425 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 426 | static expr *expr0(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 427 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 428 | expr *e, *f; |
| 429 | |
| 430 | e = expr1(critical); |
| 431 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 432 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 433 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 434 | while (i == '|') { |
| 435 | i = scan(scpriv, tokval); |
| 436 | f = expr1(critical); |
| 437 | if (!f) |
| 438 | return NULL; |
| 439 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 440 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 441 | nasm_error(ERR_NONFATAL, "`|' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 442 | " scalar values"); |
| 443 | } |
| 444 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 445 | e = unknown_expr(); |
| 446 | else |
| 447 | e = scalarvect(reloc_value(e) | reloc_value(f)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 448 | } |
| 449 | return e; |
| 450 | } |
| 451 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 452 | static expr *expr1(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 453 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 454 | expr *e, *f; |
| 455 | |
| 456 | e = expr2(critical); |
| 457 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 458 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 459 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 460 | while (i == '^') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 461 | i = scan(scpriv, tokval); |
| 462 | f = expr2(critical); |
| 463 | if (!f) |
| 464 | return NULL; |
| 465 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 466 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 467 | nasm_error(ERR_NONFATAL, "`^' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 468 | " scalar values"); |
| 469 | } |
| 470 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 471 | e = unknown_expr(); |
| 472 | else |
| 473 | e = scalarvect(reloc_value(e) ^ reloc_value(f)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 474 | } |
| 475 | return e; |
| 476 | } |
| 477 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 478 | static expr *expr2(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 479 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 480 | expr *e, *f; |
| 481 | |
| 482 | e = expr3(critical); |
| 483 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 484 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 485 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 486 | while (i == '&') { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 487 | i = scan(scpriv, tokval); |
| 488 | f = expr3(critical); |
| 489 | if (!f) |
| 490 | return NULL; |
| 491 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 492 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 493 | nasm_error(ERR_NONFATAL, "`&' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 494 | " scalar values"); |
| 495 | } |
| 496 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 497 | e = unknown_expr(); |
| 498 | else |
| 499 | e = scalarvect(reloc_value(e) & reloc_value(f)); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 500 | } |
| 501 | return e; |
| 502 | } |
| 503 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 504 | static expr *expr3(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 505 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 506 | expr *e, *f; |
| 507 | |
| 508 | e = expr4(critical); |
| 509 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 510 | return NULL; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 511 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 512 | while (i == TOKEN_SHL || i == TOKEN_SHR) { |
| 513 | int j = i; |
| 514 | i = scan(scpriv, tokval); |
| 515 | f = expr4(critical); |
| 516 | if (!f) |
| 517 | return NULL; |
| 518 | if (!(is_simple(e) || is_just_unknown(e)) || |
| 519 | !(is_simple(f) || is_just_unknown(f))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 520 | nasm_error(ERR_NONFATAL, "shift operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 521 | " scalar values"); |
| 522 | } else if (is_just_unknown(e) || is_just_unknown(f)) { |
| 523 | e = unknown_expr(); |
| 524 | } else |
| 525 | switch (j) { |
| 526 | case TOKEN_SHL: |
| 527 | e = scalarvect(reloc_value(e) << reloc_value(f)); |
| 528 | break; |
| 529 | case TOKEN_SHR: |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 530 | e = scalarvect(((uint64_t)reloc_value(e)) >> |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 531 | reloc_value(f)); |
| 532 | break; |
| 533 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 534 | } |
| 535 | return e; |
| 536 | } |
| 537 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 538 | static expr *expr4(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 539 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 540 | expr *e, *f; |
| 541 | |
| 542 | e = expr5(critical); |
| 543 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 544 | return NULL; |
| 545 | while (i == '+' || i == '-') { |
| 546 | int j = i; |
| 547 | i = scan(scpriv, tokval); |
| 548 | f = expr5(critical); |
| 549 | if (!f) |
| 550 | return NULL; |
| 551 | switch (j) { |
| 552 | case '+': |
| 553 | e = add_vectors(e, f); |
| 554 | break; |
| 555 | case '-': |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 556 | e = add_vectors(e, scalar_mult(f, -1L, false)); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 557 | break; |
| 558 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 559 | } |
| 560 | return e; |
| 561 | } |
| 562 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 563 | static expr *expr5(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 564 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 565 | expr *e, *f; |
| 566 | |
| 567 | e = expr6(critical); |
| 568 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 569 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 570 | while (i == '*' || i == '/' || i == '%' || |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 571 | i == TOKEN_SDIV || i == TOKEN_SMOD) { |
| 572 | int j = i; |
| 573 | i = scan(scpriv, tokval); |
| 574 | f = expr6(critical); |
| 575 | if (!f) |
| 576 | return NULL; |
| 577 | if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) || |
| 578 | !(is_simple(f) || is_just_unknown(f)))) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 579 | nasm_error(ERR_NONFATAL, "division operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 580 | " scalar values"); |
| 581 | return NULL; |
| 582 | } |
| 583 | if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 584 | nasm_error(ERR_NONFATAL, "division by zero"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 585 | return NULL; |
| 586 | } |
| 587 | switch (j) { |
| 588 | case '*': |
| 589 | if (is_simple(e)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 590 | e = scalar_mult(f, reloc_value(e), true); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 591 | else if (is_simple(f)) |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 592 | e = scalar_mult(e, reloc_value(f), true); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 593 | else if (is_just_unknown(e) && is_just_unknown(f)) |
| 594 | e = unknown_expr(); |
| 595 | else { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 596 | nasm_error(ERR_NONFATAL, "unable to multiply two " |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 597 | "non-scalar objects"); |
| 598 | return NULL; |
| 599 | } |
| 600 | break; |
| 601 | case '/': |
| 602 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 603 | e = unknown_expr(); |
| 604 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 605 | e = scalarvect(((uint64_t)reloc_value(e)) / |
| 606 | ((uint64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 607 | break; |
| 608 | case '%': |
| 609 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 610 | e = unknown_expr(); |
| 611 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 612 | e = scalarvect(((uint64_t)reloc_value(e)) % |
| 613 | ((uint64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 614 | break; |
| 615 | case TOKEN_SDIV: |
| 616 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 617 | e = unknown_expr(); |
| 618 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 619 | e = scalarvect(((int64_t)reloc_value(e)) / |
| 620 | ((int64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 621 | break; |
| 622 | case TOKEN_SMOD: |
| 623 | if (is_just_unknown(e) || is_just_unknown(f)) |
| 624 | e = unknown_expr(); |
| 625 | else |
Keith Kanios | a5fc646 | 2007-10-13 07:09:22 -0700 | [diff] [blame] | 626 | e = scalarvect(((int64_t)reloc_value(e)) % |
| 627 | ((int64_t)reloc_value(f))); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 628 | break; |
| 629 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 630 | } |
| 631 | return e; |
| 632 | } |
| 633 | |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 634 | static expr *eval_floatize(enum floatize type) |
| 635 | { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 636 | uint8_t result[16], *p; /* Up to 128 bits */ |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 637 | static const struct { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 638 | int bytes, start, len; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 639 | } formats[] = { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 640 | { 1, 0, 1 }, /* FLOAT_8 */ |
| 641 | { 2, 0, 2 }, /* FLOAT_16 */ |
| 642 | { 4, 0, 4 }, /* FLOAT_32 */ |
| 643 | { 8, 0, 8 }, /* FLOAT_64 */ |
| 644 | { 10, 0, 8 }, /* FLOAT_80M */ |
| 645 | { 10, 8, 2 }, /* FLOAT_80E */ |
| 646 | { 16, 0, 8 }, /* FLOAT_128L */ |
| 647 | { 16, 8, 8 }, /* FLOAT_128H */ |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 648 | }; |
| 649 | int sign = 1; |
| 650 | int64_t val; |
| 651 | int j; |
H. Peter Anvin | 7065309 | 2007-10-19 14:42:29 -0700 | [diff] [blame] | 652 | |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 653 | i = scan(scpriv, tokval); |
| 654 | if (i != '(') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 655 | nasm_error(ERR_NONFATAL, "expecting `('"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 656 | return NULL; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 657 | } |
| 658 | i = scan(scpriv, tokval); |
| 659 | if (i == '-' || i == '+') { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 660 | sign = (i == '-') ? -1 : 1; |
| 661 | i = scan(scpriv, tokval); |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 662 | } |
| 663 | if (i != TOKEN_FLOAT) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 664 | nasm_error(ERR_NONFATAL, "expecting floating-point number"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 665 | return NULL; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 666 | } |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 667 | if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes)) |
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 | i = scan(scpriv, tokval); |
| 670 | if (i != ')') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 671 | nasm_error(ERR_NONFATAL, "expecting `)'"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 672 | return NULL; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | p = result+formats[type].start+formats[type].len; |
| 676 | val = 0; |
| 677 | for (j = formats[type].len; j; j--) { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 678 | p--; |
| 679 | val = (val << 8) + *p; |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | begintemp(); |
| 683 | addtotemp(EXPR_SIMPLE, val); |
| 684 | |
| 685 | i = scan(scpriv, tokval); |
| 686 | return finishtemp(); |
| 687 | } |
| 688 | |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 689 | static expr *eval_strfunc(enum strfunc type) |
| 690 | { |
| 691 | char *string; |
| 692 | size_t string_len; |
| 693 | int64_t val; |
| 694 | bool parens, rn_warn; |
| 695 | |
Victor van den Elzen | c7deefa | 2008-06-25 11:41:40 +0200 | [diff] [blame] | 696 | parens = false; |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 697 | i = scan(scpriv, tokval); |
| 698 | if (i == '(') { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 699 | parens = true; |
| 700 | i = scan(scpriv, tokval); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 701 | } |
| 702 | if (i != TOKEN_STR) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 703 | nasm_error(ERR_NONFATAL, "expecting string"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 704 | return NULL; |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 705 | } |
| 706 | string_len = string_transform(tokval->t_charptr, tokval->t_inttwo, |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 707 | &string, type); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 708 | if (string_len == (size_t)-1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 709 | nasm_error(ERR_NONFATAL, "invalid string for transform"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 710 | return NULL; |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | val = readstrnum(string, string_len, &rn_warn); |
| 714 | if (parens) { |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 715 | i = scan(scpriv, tokval); |
| 716 | if (i != ')') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 717 | nasm_error(ERR_NONFATAL, "expecting `)'"); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 718 | return NULL; |
| 719 | } |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | if (rn_warn) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 723 | nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long"); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 724 | |
| 725 | begintemp(); |
| 726 | addtotemp(EXPR_SIMPLE, val); |
| 727 | |
| 728 | i = scan(scpriv, tokval); |
| 729 | return finishtemp(); |
| 730 | } |
| 731 | |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 732 | static int64_t eval_ifunc(int64_t val, enum ifunc func) |
| 733 | { |
| 734 | int errtype; |
| 735 | uint64_t uval = (uint64_t)val; |
| 736 | int64_t rv; |
| 737 | |
| 738 | switch (func) { |
| 739 | case IFUNC_ILOG2E: |
| 740 | case IFUNC_ILOG2W: |
| 741 | errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING; |
Cyrill Gorcunov | 71ba1f0 | 2013-02-18 01:38:11 +0400 | [diff] [blame] | 742 | |
| 743 | if (!is_power2(uval)) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 744 | nasm_error(errtype, "ilog2 argument is not a power of two"); |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 745 | /* fall through */ |
| 746 | case IFUNC_ILOG2F: |
| 747 | rv = ilog2_64(uval); |
| 748 | break; |
| 749 | |
| 750 | case IFUNC_ILOG2C: |
| 751 | rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1; |
| 752 | break; |
| 753 | |
| 754 | default: |
H. Peter Anvin | d6d1b65 | 2016-03-03 14:36:01 -0800 | [diff] [blame] | 755 | nasm_panic(0, "invalid IFUNC token %d", func); |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 756 | rv = 0; |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | return rv; |
| 761 | } |
| 762 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 763 | static expr *expr6(int critical) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 764 | { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 765 | int32_t type; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 766 | expr *e; |
Charles Crayne | 4e8563d | 2007-11-05 17:19:32 -0800 | [diff] [blame] | 767 | int32_t label_seg; |
| 768 | int64_t label_ofs; |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 769 | int64_t tmpval; |
| 770 | bool rn_warn; |
H. Peter Anvin | 9857807 | 2018-06-01 18:02:54 -0700 | [diff] [blame] | 771 | const char *scope; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 772 | |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 773 | if (++deadman > nasm_limit[LIMIT_EVAL]) { |
| 774 | nasm_error(ERR_NONFATAL, "expression too long"); |
| 775 | return NULL; |
| 776 | } |
| 777 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 778 | switch (i) { |
| 779 | case '-': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 780 | i = scan(scpriv, tokval); |
| 781 | e = expr6(critical); |
| 782 | if (!e) |
| 783 | return NULL; |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 784 | return scalar_mult(e, -1L, false); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 785 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 786 | case '+': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 787 | i = scan(scpriv, tokval); |
| 788 | return expr6(critical); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 789 | |
| 790 | case '~': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 791 | i = scan(scpriv, tokval); |
| 792 | e = expr6(critical); |
| 793 | if (!e) |
| 794 | return NULL; |
| 795 | if (is_just_unknown(e)) |
| 796 | return unknown_expr(); |
| 797 | else if (!is_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 798 | nasm_error(ERR_NONFATAL, "`~' operator may only be applied to" |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 799 | " scalar values"); |
| 800 | return NULL; |
| 801 | } |
| 802 | return scalarvect(~reloc_value(e)); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 803 | |
| 804 | case '!': |
Chuck Crayne | cb9bc21 | 2007-05-02 04:21:26 +0000 | [diff] [blame] | 805 | i = scan(scpriv, tokval); |
| 806 | e = expr6(critical); |
| 807 | if (!e) |
| 808 | return NULL; |
| 809 | if (is_just_unknown(e)) |
| 810 | return unknown_expr(); |
| 811 | else if (!is_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 812 | nasm_error(ERR_NONFATAL, "`!' operator may only be applied to" |
Chuck Crayne | cb9bc21 | 2007-05-02 04:21:26 +0000 | [diff] [blame] | 813 | " scalar values"); |
| 814 | return NULL; |
| 815 | } |
| 816 | return scalarvect(!reloc_value(e)); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 817 | |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 818 | case TOKEN_IFUNC: |
| 819 | { |
| 820 | enum ifunc func = tokval->t_integer; |
| 821 | i = scan(scpriv, tokval); |
| 822 | e = expr6(critical); |
| 823 | if (!e) |
| 824 | return NULL; |
| 825 | if (is_just_unknown(e)) |
| 826 | return unknown_expr(); |
| 827 | else if (!is_simple(e)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 828 | nasm_error(ERR_NONFATAL, "function may only be applied to" |
H. Peter Anvin | 290b4cb | 2012-05-31 10:25:37 -0700 | [diff] [blame] | 829 | " scalar values"); |
| 830 | return NULL; |
| 831 | } |
| 832 | return scalarvect(eval_ifunc(reloc_value(e), func)); |
| 833 | } |
| 834 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 835 | case TOKEN_SEG: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 836 | i = scan(scpriv, tokval); |
| 837 | e = expr6(critical); |
| 838 | if (!e) |
| 839 | return NULL; |
| 840 | e = segment_part(e); |
| 841 | if (!e) |
| 842 | return NULL; |
| 843 | if (is_unknown(e) && critical) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 844 | nasm_error(ERR_NONFATAL, "unable to determine segment base"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 845 | return NULL; |
| 846 | } |
| 847 | return e; |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 848 | |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 849 | case TOKEN_FLOATIZE: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 850 | return eval_floatize(tokval->t_integer); |
H. Peter Anvin | dc467ba | 2007-09-24 12:30:54 -0700 | [diff] [blame] | 851 | |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 852 | case TOKEN_STRFUNC: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 853 | return eval_strfunc(tokval->t_integer); |
H. Peter Anvin | 9c74910 | 2008-06-14 21:08:38 -0700 | [diff] [blame] | 854 | |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 855 | case '(': |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 856 | i = scan(scpriv, tokval); |
| 857 | e = bexpr(critical); |
| 858 | if (!e) |
| 859 | return NULL; |
| 860 | if (i != ')') { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 861 | nasm_error(ERR_NONFATAL, "expecting `)'"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 862 | return NULL; |
| 863 | } |
| 864 | i = scan(scpriv, tokval); |
| 865 | return e; |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 866 | |
| 867 | case TOKEN_NUM: |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 868 | case TOKEN_STR: |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 869 | case TOKEN_REG: |
| 870 | case TOKEN_ID: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 871 | case TOKEN_INSN: /* Opcodes that occur here are really labels */ |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 872 | case TOKEN_HERE: |
| 873 | case TOKEN_BASE: |
Jin Kyu Song | 72018a2 | 2013-08-05 20:46:18 -0700 | [diff] [blame] | 874 | case TOKEN_DECORATOR: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 875 | begintemp(); |
| 876 | switch (i) { |
| 877 | case TOKEN_NUM: |
| 878 | addtotemp(EXPR_SIMPLE, tokval->t_integer); |
| 879 | break; |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 880 | case TOKEN_STR: |
| 881 | tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn); |
| 882 | if (rn_warn) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 883 | nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long"); |
H. Peter Anvin | 1162704 | 2008-06-09 20:45:19 -0700 | [diff] [blame] | 884 | addtotemp(EXPR_SIMPLE, tmpval); |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 885 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 886 | case TOKEN_REG: |
| 887 | addtotemp(tokval->t_integer, 1L); |
| 888 | if (hint && hint->type == EAH_NOHINT) |
| 889 | hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE; |
| 890 | break; |
| 891 | case TOKEN_ID: |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 892 | case TOKEN_INSN: |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 893 | case TOKEN_HERE: |
| 894 | case TOKEN_BASE: |
| 895 | /* |
H. Peter Anvin | cd7893d | 2016-02-18 01:25:46 -0800 | [diff] [blame] | 896 | * If !location.known, this indicates that no |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 897 | * symbol, Here or Base references are valid because we |
| 898 | * are in preprocess-only mode. |
| 899 | */ |
H. Peter Anvin | cd7893d | 2016-02-18 01:25:46 -0800 | [diff] [blame] | 900 | if (!location.known) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 901 | nasm_error(ERR_NONFATAL, |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 902 | "%s not supported in preprocess-only mode", |
Cyrill Gorcunov | cfbcddf | 2009-10-31 20:05:32 +0300 | [diff] [blame] | 903 | (i == TOKEN_HERE ? "`$'" : |
| 904 | i == TOKEN_BASE ? "`$$'" : |
| 905 | "symbol references")); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 906 | addtotemp(EXPR_UNKNOWN, 1L); |
| 907 | break; |
| 908 | } |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 909 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 910 | type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */ |
| 911 | if (i == TOKEN_BASE) { |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 912 | label_seg = in_absolute ? absolute.segment : location.segment; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 913 | label_ofs = 0; |
| 914 | } else if (i == TOKEN_HERE) { |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 915 | label_seg = in_absolute ? absolute.segment : location.segment; |
| 916 | label_ofs = in_absolute ? absolute.offset : location.offset; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 917 | } else { |
H. Peter Anvin | cd7893d | 2016-02-18 01:25:46 -0800 | [diff] [blame] | 918 | if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) { |
Charles Crayne | d60059e | 2008-03-12 22:39:03 -0700 | [diff] [blame] | 919 | scope = local_scope(tokval->t_charptr); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 920 | if (critical == 2) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 921 | nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined", |
Charles Crayne | d60059e | 2008-03-12 22:39:03 -0700 | [diff] [blame] | 922 | scope,tokval->t_charptr); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 923 | return NULL; |
| 924 | } else if (critical == 1) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 925 | nasm_error(ERR_NONFATAL, |
Charles Crayne | d60059e | 2008-03-12 22:39:03 -0700 | [diff] [blame] | 926 | "symbol `%s%s' not defined before use", |
| 927 | scope,tokval->t_charptr); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 928 | return NULL; |
| 929 | } else { |
| 930 | if (opflags) |
Cyrill Gorcunov | e2457c7 | 2010-09-10 01:02:12 +0400 | [diff] [blame] | 931 | *opflags |= OPFLAG_FORWARD; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 932 | type = EXPR_UNKNOWN; |
| 933 | label_seg = NO_SEG; |
| 934 | label_ofs = 1; |
| 935 | } |
| 936 | } |
| 937 | if (opflags && is_extern(tokval->t_charptr)) |
| 938 | *opflags |= OPFLAG_EXTERN; |
| 939 | } |
| 940 | addtotemp(type, label_ofs); |
| 941 | if (label_seg != NO_SEG) |
| 942 | addtotemp(EXPR_SEGBASE + label_seg, 1L); |
| 943 | break; |
Jin Kyu Song | 72018a2 | 2013-08-05 20:46:18 -0700 | [diff] [blame] | 944 | case TOKEN_DECORATOR: |
| 945 | addtotemp(EXPR_RDSAE, tokval->t_integer); |
| 946 | break; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 947 | } |
| 948 | i = scan(scpriv, tokval); |
| 949 | return finishtemp(); |
H. Peter Anvin | 5f77c03 | 2007-09-24 10:51:07 -0700 | [diff] [blame] | 950 | |
| 951 | default: |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 952 | nasm_error(ERR_NONFATAL, "expression syntax error"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 953 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 957 | expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv, |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 958 | int *fwref, int critical, struct eval_hints *hints) |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 959 | { |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 960 | expr *e; |
| 961 | expr *f = NULL; |
| 962 | |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 963 | deadman = 0; |
| 964 | |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 965 | hint = hints; |
| 966 | if (hint) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 967 | hint->type = EAH_NOHINT; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 968 | |
H. Peter Anvin | af535c1 | 2002-04-30 20:59:21 +0000 | [diff] [blame] | 969 | if (critical & CRITICAL) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 970 | critical &= ~CRITICAL; |
| 971 | bexpr = rexp0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 972 | } else |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 973 | bexpr = expr0; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 974 | |
| 975 | scan = sc; |
| 976 | scpriv = scprivate; |
| 977 | tokval = tv; |
H. Peter Anvin | eba20a7 | 2002-04-30 20:53:55 +0000 | [diff] [blame] | 978 | opflags = fwref; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 979 | |
| 980 | if (tokval->t_type == TOKEN_INVALID) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 981 | i = scan(scpriv, tokval); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 982 | else |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 983 | i = tokval->t_type; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 984 | |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 985 | while (ntempexprs) /* initialize temporary storage */ |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 986 | nasm_free(tempexprs[--ntempexprs]); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 987 | |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 988 | e = bexpr(critical); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 989 | if (!e) |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 990 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 991 | |
| 992 | if (i == TOKEN_WRT) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 993 | i = scan(scpriv, tokval); /* eat the WRT */ |
| 994 | f = expr6(critical); |
| 995 | if (!f) |
| 996 | return NULL; |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 997 | } |
H. Peter Anvin | 6867acc | 2007-10-10 14:58:45 -0700 | [diff] [blame] | 998 | e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */ |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 999 | if (f) { |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1000 | expr *g; |
| 1001 | if (is_just_unknown(f)) |
| 1002 | g = unknown_expr(); |
| 1003 | else { |
Keith Kanios | b7a8954 | 2007-04-12 02:40:54 +0000 | [diff] [blame] | 1004 | int64_t value; |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1005 | begintemp(); |
| 1006 | if (!is_reloc(f)) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1007 | nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1008 | return NULL; |
| 1009 | } |
| 1010 | value = reloc_seg(f); |
| 1011 | if (value == NO_SEG) |
| 1012 | value = reloc_value(f) | SEG_ABS; |
| 1013 | else if (!(value & SEG_ABS) && !(value % 2) && critical) { |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 1014 | nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT"); |
H. Peter Anvin | e2c8018 | 2005-01-15 22:15:51 +0000 | [diff] [blame] | 1015 | return NULL; |
| 1016 | } |
| 1017 | addtotemp(EXPR_WRT, value); |
| 1018 | g = finishtemp(); |
| 1019 | } |
| 1020 | e = add_vectors(e, g); |
H. Peter Anvin | 76690a1 | 2002-04-30 20:52:49 +0000 | [diff] [blame] | 1021 | } |
| 1022 | return e; |
| 1023 | } |