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