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