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