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