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