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