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