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