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