| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "alu.h" |
| 21 |
#include <stdlib.h> |
| 22 |
#include <math.h> |
| 23 |
#include <string.h> |
| 24 |
#include "platform.h" |
| 25 |
#include "dynabuf.h" |
| 26 |
#include "encoding.h" |
| 27 |
#include "global.h" |
| 28 |
#include "input.h" |
| 29 |
#include "output.h" |
| 30 |
#include "section.h" |
| 31 |
#include "symbol.h" |
| 32 |
#include "tree.h" |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#define ERRORMSG_INITIALSIZE 256 // ad hoc |
| 38 |
#define FUNCTION_INITIALSIZE 16 // enough for "is_string" |
| 39 |
#define HALF_INITIAL_STACK_SIZE 8 |
| 40 |
static const char exception_div_by_zero[] = "Division by zero."; |
| 41 |
static const char exception_no_value[] = "No value given."; |
| 42 |
static const char exception_paren_open[] = "Too many '('."; |
| 43 |
static const char exception_not_number[] = "Expression did not return a number."; |
| 44 |
static const char exception_float_to_int[]= "Converted to integer for binary logic operator."; |
| 45 |
static const char exception_lengthnot1[] = "String length is not 1."; |
| 46 |
|
| 47 |
enum op_group { |
| 48 |
OPGROUP_SPECIAL, |
| 49 |
OPGROUP_MONADIC, |
| 50 |
OPGROUP_DYADIC |
| 51 |
}; |
| 52 |
enum op_id { |
| 53 |
|
| 54 |
OPID_TERMINATOR, |
| 55 |
OPID_START_EXPRESSION, |
| 56 |
OPID_SUBEXPR_PAREN, |
| 57 |
OPID_START_LIST, |
| 58 |
OPID_SUBEXPR_BRACKET, |
| 59 |
|
| 60 |
OPID_NOT, |
| 61 |
OPID_NEGATE, |
| 62 |
OPID_LOWBYTEOF, |
| 63 |
OPID_HIGHBYTEOF, |
| 64 |
OPID_BANKBYTEOF, |
| 65 |
OPID_ADDRESS, |
| 66 |
OPID_INT, |
| 67 |
OPID_FLOAT, |
| 68 |
OPID_SIN, |
| 69 |
OPID_COS, |
| 70 |
OPID_TAN, |
| 71 |
OPID_ARCSIN, |
| 72 |
OPID_ARCCOS, |
| 73 |
OPID_ARCTAN, |
| 74 |
OPID_LEN, |
| 75 |
OPID_ISNUMBER, |
| 76 |
OPID_ISLIST, |
| 77 |
OPID_ISSTRING, |
| 78 |
OPID_DEC, |
| 79 |
OPID_HEX, |
| 80 |
|
| 81 |
|
| 82 |
OPID_POWEROF, |
| 83 |
OPID_MULTIPLY, |
| 84 |
OPID_DIVIDE, |
| 85 |
OPID_INTDIV, |
| 86 |
OPID_MODULO, |
| 87 |
OPID_SHIFTLEFT, |
| 88 |
OPID_ASR, |
| 89 |
OPID_LSR, |
| 90 |
OPID_ADD, |
| 91 |
OPID_SUBTRACT, |
| 92 |
OPID_EQUALS, |
| 93 |
OPID_LESSOREQUAL, |
| 94 |
OPID_LESSTHAN, |
| 95 |
OPID_GREATEROREQUAL, |
| 96 |
OPID_GREATERTHAN, |
| 97 |
OPID_NOTEQUAL, |
| 98 |
OPID_AND, |
| 99 |
OPID_OR, |
| 100 |
OPID_EOR, |
| 101 |
OPID_XOR, |
| 102 |
OPID_LIST_APPEND, |
| 103 |
OPID_ATINDEX, |
| 104 |
}; |
| 105 |
struct op { |
| 106 |
int priority; |
| 107 |
enum op_group group; |
| 108 |
enum op_id id; |
| 109 |
const char *text_version; |
| 110 |
}; |
| 111 |
static struct op ops_terminating_char = {0, OPGROUP_SPECIAL, OPID_TERMINATOR, "end of expression" }; |
| 112 |
static struct op ops_start_expression = {2, OPGROUP_SPECIAL, OPID_START_EXPRESSION, "start of expression" }; |
| 113 |
static struct op ops_subexpr_paren = {4, OPGROUP_SPECIAL, OPID_SUBEXPR_PAREN, "left parenthesis" }; |
| 114 |
static struct op ops_start_list = {6, OPGROUP_SPECIAL, OPID_START_LIST, "start list" }; |
| 115 |
static struct op ops_subexpr_bracket = {8, OPGROUP_SPECIAL, OPID_SUBEXPR_BRACKET, "open index" }; |
| 116 |
static struct op ops_list_append = {14, OPGROUP_DYADIC, OPID_LIST_APPEND, "append to list" }; |
| 117 |
static struct op ops_or = {16, OPGROUP_DYADIC, OPID_OR, "logical or" }; |
| 118 |
static struct op ops_eor = {18, OPGROUP_DYADIC, OPID_EOR, "exclusive or" }; |
| 119 |
static struct op ops_xor = {18, OPGROUP_DYADIC, OPID_XOR, "exclusive or" }; |
| 120 |
static struct op ops_and = {20, OPGROUP_DYADIC, OPID_AND, "logical and" }; |
| 121 |
static struct op ops_equals = {22, OPGROUP_DYADIC, OPID_EQUALS, "test for equality" }; |
| 122 |
static struct op ops_not_equal = {24, OPGROUP_DYADIC, OPID_NOTEQUAL, "test for inequality" }; |
| 123 |
|
| 124 |
static struct op ops_less_or_equal = {26, OPGROUP_DYADIC, OPID_LESSOREQUAL, "less than or equal" }; |
| 125 |
static struct op ops_less_than = {26, OPGROUP_DYADIC, OPID_LESSTHAN, "less than" }; |
| 126 |
static struct op ops_greater_or_equal = {26, OPGROUP_DYADIC, OPID_GREATEROREQUAL, "greater than or equal" }; |
| 127 |
static struct op ops_greater_than = {26, OPGROUP_DYADIC, OPID_GREATERTHAN, "greater than" }; |
| 128 |
|
| 129 |
static struct op ops_low_byte_of = {28, OPGROUP_MONADIC, OPID_LOWBYTEOF, "low byte of" }; |
| 130 |
static struct op ops_high_byte_of = {28, OPGROUP_MONADIC, OPID_HIGHBYTEOF, "high byte of" }; |
| 131 |
static struct op ops_bank_byte_of = {28, OPGROUP_MONADIC, OPID_BANKBYTEOF, "bank byte of" }; |
| 132 |
|
| 133 |
static struct op ops_shift_left = {30, OPGROUP_DYADIC, OPID_SHIFTLEFT, "shift left" }; |
| 134 |
static struct op ops_asr = {30, OPGROUP_DYADIC, OPID_ASR, "arithmetic shift right" }; |
| 135 |
static struct op ops_lsr = {30, OPGROUP_DYADIC, OPID_LSR, "logical shift right" }; |
| 136 |
|
| 137 |
static struct op ops_add = {32, OPGROUP_DYADIC, OPID_ADD, "addition" }; |
| 138 |
static struct op ops_subtract = {32, OPGROUP_DYADIC, OPID_SUBTRACT, "subtraction" }; |
| 139 |
|
| 140 |
static struct op ops_multiply = {34, OPGROUP_DYADIC, OPID_MULTIPLY, "multiplication" }; |
| 141 |
static struct op ops_divide = {34, OPGROUP_DYADIC, OPID_DIVIDE, "division" }; |
| 142 |
static struct op ops_intdiv = {34, OPGROUP_DYADIC, OPID_INTDIV, "integer division" }; |
| 143 |
static struct op ops_modulo = {34, OPGROUP_DYADIC, OPID_MODULO, "modulo" }; |
| 144 |
|
| 145 |
static struct op ops_negate = {36, OPGROUP_MONADIC, OPID_NEGATE, "negation" }; |
| 146 |
#define PRIO_POWEROF 37 // the single right-associative operator, so this gets checked explicitly |
| 147 |
static struct op ops_powerof = {PRIO_POWEROF, OPGROUP_DYADIC, OPID_POWEROF, "power of" }; |
| 148 |
static struct op ops_not = {38, OPGROUP_MONADIC, OPID_NOT, "logical not" }; |
| 149 |
static struct op ops_atindex = {40, OPGROUP_DYADIC, OPID_ATINDEX, "indexing" }; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static struct op ops_addr = {42, OPGROUP_MONADIC, OPID_ADDRESS, "address()" }; |
| 156 |
static struct op ops_int = {42, OPGROUP_MONADIC, OPID_INT, "int()" }; |
| 157 |
static struct op ops_float = {42, OPGROUP_MONADIC, OPID_FLOAT, "float()" }; |
| 158 |
static struct op ops_sin = {42, OPGROUP_MONADIC, OPID_SIN, "sin()" }; |
| 159 |
static struct op ops_cos = {42, OPGROUP_MONADIC, OPID_COS, "cos()" }; |
| 160 |
static struct op ops_tan = {42, OPGROUP_MONADIC, OPID_TAN, "tan()" }; |
| 161 |
static struct op ops_arcsin = {42, OPGROUP_MONADIC, OPID_ARCSIN, "arcsin()" }; |
| 162 |
static struct op ops_arccos = {42, OPGROUP_MONADIC, OPID_ARCCOS, "arccos()" }; |
| 163 |
static struct op ops_arctan = {42, OPGROUP_MONADIC, OPID_ARCTAN, "arctan()" }; |
| 164 |
static struct op ops_len = {42, OPGROUP_MONADIC, OPID_LEN, "len()" }; |
| 165 |
static struct op ops_isnumber = {42, OPGROUP_MONADIC, OPID_ISNUMBER, "is_number()" }; |
| 166 |
static struct op ops_islist = {42, OPGROUP_MONADIC, OPID_ISLIST, "is_list()" }; |
| 167 |
static struct op ops_isstring = {42, OPGROUP_MONADIC, OPID_ISSTRING, "is_string()" }; |
| 168 |
static struct op ops_dec = {42, OPGROUP_MONADIC, OPID_DEC, "dec()" }; |
| 169 |
static struct op ops_hex = {42, OPGROUP_MONADIC, OPID_HEX, "hex()" }; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static STRUCT_DYNABUF_REF(errormsg_dyna_buf, ERRORMSG_INITIALSIZE); |
| 174 |
static STRUCT_DYNABUF_REF(function_dyna_buf, FUNCTION_INITIALSIZE); |
| 175 |
|
| 176 |
static struct op **op_stack = NULL; |
| 177 |
static int opstack_size = HALF_INITIAL_STACK_SIZE; |
| 178 |
static int op_sp; |
| 179 |
|
| 180 |
static struct object *arg_stack = NULL; |
| 181 |
static int argstack_size = HALF_INITIAL_STACK_SIZE; |
| 182 |
static int arg_sp; |
| 183 |
enum alu_state { |
| 184 |
STATE_EXPECT_ARG_OR_MONADIC_OP, |
| 185 |
STATE_EXPECT_DYADIC_OP, |
| 186 |
STATE_MAX_GO_ON, |
| 187 |
STATE_ERROR, |
| 188 |
STATE_END |
| 189 |
}; |
| 190 |
static enum alu_state alu_state; |
| 191 |
|
| 192 |
static struct ronode op_tree[] = { |
| 193 |
PREDEF_START, |
| 194 |
PREDEFNODE("asr", &ops_asr), |
| 195 |
PREDEFNODE("lsr", &ops_lsr), |
| 196 |
PREDEFNODE("asl", &ops_shift_left), |
| 197 |
PREDEFNODE("lsl", &ops_shift_left), |
| 198 |
PREDEFNODE("div", &ops_intdiv), |
| 199 |
PREDEFNODE("mod", &ops_modulo), |
| 200 |
PREDEFNODE("and", &ops_and), |
| 201 |
PREDEFNODE("or", &ops_or), |
| 202 |
PREDEFNODE("eor", &ops_eor), |
| 203 |
PREDEF_END("xor", &ops_xor), |
| 204 |
|
| 205 |
}; |
| 206 |
static struct ronode function_tree[] = { |
| 207 |
PREDEF_START, |
| 208 |
PREDEFNODE("addr", &ops_addr), |
| 209 |
PREDEFNODE("address", &ops_addr), |
| 210 |
PREDEFNODE("int", &ops_int), |
| 211 |
PREDEFNODE("float", &ops_float), |
| 212 |
PREDEFNODE("len", &ops_len), |
| 213 |
PREDEFNODE("is_number", &ops_isnumber), |
| 214 |
PREDEFNODE("is_list", &ops_islist), |
| 215 |
PREDEFNODE("is_string", &ops_isstring), |
| 216 |
PREDEFNODE("dec", &ops_dec), |
| 217 |
PREDEFNODE("hex", &ops_hex), |
| 218 |
PREDEFNODE("arcsin", &ops_arcsin), |
| 219 |
PREDEFNODE("arccos", &ops_arccos), |
| 220 |
PREDEFNODE("arctan", &ops_arctan), |
| 221 |
PREDEFNODE("sin", &ops_sin), |
| 222 |
PREDEFNODE("cos", &ops_cos), |
| 223 |
PREDEF_END("tan", &ops_tan), |
| 224 |
|
| 225 |
}; |
| 226 |
|
| 227 |
#define PUSH_OP(x) \ |
| 228 |
do { \ |
| 229 |
op_stack[op_sp] = (x); \ |
| 230 |
if (++op_sp >= opstack_size) \ |
| 231 |
enlarge_operator_stack(); \ |
| 232 |
} while (0) |
| 233 |
|
| 234 |
#define PUSH_INT_ARG(i, f, r) \ |
| 235 |
do { \ |
| 236 |
arg_stack[arg_sp].type = &type_number; \ |
| 237 |
arg_stack[arg_sp].u.number.ntype = NUMTYPE_INT; \ |
| 238 |
arg_stack[arg_sp].u.number.flags = (f); \ |
| 239 |
arg_stack[arg_sp].u.number.val.intval = (i); \ |
| 240 |
arg_stack[arg_sp++].u.number.addr_refs = (r); \ |
| 241 |
} while (0) |
| 242 |
#define PUSH_FP_ARG(fp, f) \ |
| 243 |
do { \ |
| 244 |
arg_stack[arg_sp].type = &type_number; \ |
| 245 |
arg_stack[arg_sp].u.number.ntype = NUMTYPE_FLOAT;\ |
| 246 |
arg_stack[arg_sp].u.number.flags = (f); \ |
| 247 |
arg_stack[arg_sp].u.number.val.fpval = (fp); \ |
| 248 |
arg_stack[arg_sp++].u.number.addr_refs = 0; \ |
| 249 |
} while (0) |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
static void enlarge_operator_stack(void) |
| 254 |
{ |
| 255 |
opstack_size *= 2; |
| 256 |
|
| 257 |
op_stack = realloc(op_stack, opstack_size * sizeof(*op_stack)); |
| 258 |
if (op_stack == NULL) |
| 259 |
throw_serious_error(exception_no_memory_left); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
static void enlarge_argument_stack(void) |
| 265 |
{ |
| 266 |
argstack_size *= 2; |
| 267 |
|
| 268 |
arg_stack = realloc(arg_stack, argstack_size * sizeof(*arg_stack)); |
| 269 |
if (arg_stack == NULL) |
| 270 |
throw_serious_error(exception_no_memory_left); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static intval_t my_pow(intval_t mantissa, intval_t exponent) |
| 279 |
{ |
| 280 |
intval_t result = 1; |
| 281 |
|
| 282 |
while (exponent) { |
| 283 |
|
| 284 |
if (exponent & 1) |
| 285 |
result *= mantissa; |
| 286 |
|
| 287 |
mantissa *= mantissa; |
| 288 |
exponent >>= 1; |
| 289 |
} |
| 290 |
return result; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
static intval_t my_asr(intval_t left, intval_t right) |
| 296 |
{ |
| 297 |
|
| 298 |
|
| 299 |
if (left >= 0) |
| 300 |
return left >> right; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
return ~((~left) >> right); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
static void is_not_defined(struct symbol *symbol, char *name, size_t length) |
| 317 |
{ |
| 318 |
if (!pass.flags.complain_about_undefined) |
| 319 |
return; |
| 320 |
|
| 321 |
|
| 322 |
if (symbol->has_been_reported) |
| 323 |
return; |
| 324 |
|
| 325 |
symbol->has_been_reported = TRUE; |
| 326 |
|
| 327 |
dynabuf_clear(errormsg_dyna_buf); |
| 328 |
dynabuf_add_string(errormsg_dyna_buf, "Symbol not defined ("); |
| 329 |
length += errormsg_dyna_buf->size; |
| 330 |
|
| 331 |
dynabuf_add_string(errormsg_dyna_buf, name); |
| 332 |
if (errormsg_dyna_buf->size < length) { |
| 333 |
BUG("IllegalSymbolNameLength", errormsg_dyna_buf->size - length); |
| 334 |
} else { |
| 335 |
errormsg_dyna_buf->size = length; |
| 336 |
} |
| 337 |
dynabuf_add_string(errormsg_dyna_buf, ")."); |
| 338 |
dynabuf_append(errormsg_dyna_buf, '\0'); |
| 339 |
throw_error(errormsg_dyna_buf->buffer); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
static struct symbol *get_symbol_value(scope_t scope, size_t name_length, boolean check_pass_number) |
| 352 |
{ |
| 353 |
struct symbol *symbol; |
| 354 |
struct object *arg; |
| 355 |
|
| 356 |
symbol = symbol_find(scope); |
| 357 |
symbol->has_been_read = TRUE; |
| 358 |
if (symbol->object.type == NULL) { |
| 359 |
|
| 360 |
symbol->object.type = &type_number; |
| 361 |
symbol->object.u.number.ntype = NUMTYPE_UNDEFINED; |
| 362 |
symbol->object.u.number.flags = NUMBER_EVER_UNDEFINED; |
| 363 |
symbol->object.u.number.addr_refs = 0; |
| 364 |
} else { |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if (check_pass_number && (symbol->pass_number != pass.number)) |
| 374 |
symbol->object.u.number.ntype = NUMTYPE_UNDEFINED; |
| 375 |
} |
| 376 |
|
| 377 |
arg = &arg_stack[arg_sp++]; |
| 378 |
*arg = symbol->object; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
if ((arg->type == &type_number) && (arg->u.number.ntype == NUMTYPE_UNDEFINED)) |
| 385 |
is_not_defined(symbol, GLOBALDYNABUF_CURRENT, name_length); |
| 386 |
|
| 387 |
return symbol; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
static void parse_program_counter(void) |
| 393 |
{ |
| 394 |
struct number pc; |
| 395 |
struct object *arg; |
| 396 |
|
| 397 |
GetByte(); |
| 398 |
programcounter_read_asterisk(&pc); |
| 399 |
|
| 400 |
arg = &arg_stack[arg_sp++]; |
| 401 |
arg->type = &type_number; |
| 402 |
arg->u.number = pc; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
void string_prepare_string(struct object *self, int len) |
| 409 |
{ |
| 410 |
self->type = &type_string; |
| 411 |
self->u.string = safe_malloc(sizeof(*(self->u.string)) + len); |
| 412 |
self->u.string->payload[len] = 0; |
| 413 |
self->u.string->length = len; |
| 414 |
self->u.string->refs = 1; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
static void parse_quoted(char closing_quote) |
| 419 |
{ |
| 420 |
boolean is_string; |
| 421 |
intval_t value; |
| 422 |
|
| 423 |
if (input_read_string_literal(closing_quote)) |
| 424 |
goto fail; |
| 425 |
|
| 426 |
if (config.dialect >= V0_97__BACKSLASH_ESCAPING) { |
| 427 |
|
| 428 |
is_string = (closing_quote == '"'); |
| 429 |
} else { |
| 430 |
|
| 431 |
is_string = FALSE; |
| 432 |
} |
| 433 |
|
| 434 |
if (is_string) { |
| 435 |
string_prepare_string(&arg_stack[arg_sp], GlobalDynaBuf->size); |
| 436 |
memcpy(arg_stack[arg_sp].u.string->payload, GLOBALDYNABUF_CURRENT, GlobalDynaBuf->size); |
| 437 |
++arg_sp; |
| 438 |
} else { |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
if (GlobalDynaBuf->size == 0) { |
| 443 |
throw_error("There is no character in quotes."); |
| 444 |
goto fail; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
if (GlobalDynaBuf->size != 1) |
| 449 |
throw_error("There's more than one character."); |
| 450 |
|
| 451 |
value = encoding_encode_char(GLOBALDYNABUF_CURRENT[0]); |
| 452 |
PUSH_INT_ARG(value, 0, 0); |
| 453 |
} |
| 454 |
|
| 455 |
return; |
| 456 |
|
| 457 |
fail: |
| 458 |
PUSH_INT_ARG(0, 0, 0); |
| 459 |
alu_state = STATE_ERROR; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
static void parse_binary_literal(void) |
| 467 |
{ |
| 468 |
intval_t value = 0; |
| 469 |
bits flags = 0; |
| 470 |
int digits = -1; |
| 471 |
|
| 472 |
for (;;) { |
| 473 |
++digits; |
| 474 |
switch (GetByte()) { |
| 475 |
case '0': |
| 476 |
case '.': |
| 477 |
value <<= 1; |
| 478 |
continue; |
| 479 |
case '1': |
| 480 |
case '#': |
| 481 |
value = (value << 1) | 1; |
| 482 |
continue; |
| 483 |
} |
| 484 |
break; |
| 485 |
} |
| 486 |
if (!digits) { |
| 487 |
throw_error("Binary literal without any digits."); |
| 488 |
} |
| 489 |
if (digits & config.warn_bin_mask) { |
| 490 |
throw_finalpass_warning("Binary literal with strange number of digits."); |
| 491 |
} |
| 492 |
|
| 493 |
if (config.honor_leading_zeroes) { |
| 494 |
if (digits > 8) { |
| 495 |
if (digits > 16) { |
| 496 |
if (value < 65536) |
| 497 |
flags |= NUMBER_FORCES_24; |
| 498 |
} else { |
| 499 |
if (value < 256) |
| 500 |
flags |= NUMBER_FORCES_16; |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
PUSH_INT_ARG(value, flags, 0); |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
static void parse_hex_literal(void) |
| 513 |
{ |
| 514 |
char byte; |
| 515 |
int digits = -1; |
| 516 |
bits flags = 0; |
| 517 |
intval_t value = 0; |
| 518 |
|
| 519 |
for (;;) { |
| 520 |
++digits; |
| 521 |
byte = GetByte(); |
| 522 |
|
| 523 |
if ((byte >= '0') && (byte <= '9')) { |
| 524 |
value = (value << 4) + (byte - '0'); |
| 525 |
continue; |
| 526 |
} |
| 527 |
if ((byte >= 'a') && (byte <= 'f')) { |
| 528 |
value = (value << 4) + (byte - 'a') + 10; |
| 529 |
continue; |
| 530 |
} |
| 531 |
if ((byte >= 'A') && (byte <= 'F')) { |
| 532 |
value = (value << 4) + (byte - 'A') + 10; |
| 533 |
continue; |
| 534 |
} |
| 535 |
break; |
| 536 |
} |
| 537 |
if (!digits) |
| 538 |
throw_error("Hex literal without any digits."); |
| 539 |
|
| 540 |
if (config.honor_leading_zeroes) { |
| 541 |
if (digits > 2) { |
| 542 |
if (digits > 4) { |
| 543 |
if (value < 65536) |
| 544 |
flags |= NUMBER_FORCES_24; |
| 545 |
} else { |
| 546 |
if (value < 256) |
| 547 |
flags |= NUMBER_FORCES_16; |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
PUSH_INT_ARG(value, flags, 0); |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
static void parse_frac_part(int integer_part) |
| 558 |
{ |
| 559 |
double denominator = 1, |
| 560 |
fpval = integer_part; |
| 561 |
|
| 562 |
|
| 563 |
while ((GotByte >= '0') && (GotByte <= '9')) { |
| 564 |
fpval = 10 * fpval + (GotByte & 15); |
| 565 |
denominator *= 10; |
| 566 |
GetByte(); |
| 567 |
} |
| 568 |
|
| 569 |
PUSH_FP_ARG(fpval / denominator, 0); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
static void parse_number_literal(void) |
| 588 |
{ |
| 589 |
intval_t intval = (GotByte & 15); |
| 590 |
|
| 591 |
GetByte(); |
| 592 |
|
| 593 |
if (intval == 0) { |
| 594 |
if (GotByte == 'b') { |
| 595 |
parse_binary_literal(); |
| 596 |
return; |
| 597 |
} |
| 598 |
if (GotByte == 'x') { |
| 599 |
parse_hex_literal(); |
| 600 |
return; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
while ((GotByte >= '0') && (GotByte <= '9')) { |
| 605 |
intval = 10 * intval + (GotByte & 15); |
| 606 |
GetByte(); |
| 607 |
} |
| 608 |
|
| 609 |
if (GotByte == '.') { |
| 610 |
|
| 611 |
GetByte(); |
| 612 |
parse_frac_part(intval); |
| 613 |
} else { |
| 614 |
PUSH_INT_ARG(intval, 0, 0); |
| 615 |
} |
| 616 |
|
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static void parse_octal_literal(void) |
| 623 |
{ |
| 624 |
intval_t value = 0; |
| 625 |
bits flags = 0; |
| 626 |
int digits = 0; |
| 627 |
|
| 628 |
while ((GotByte >= '0') && (GotByte <= '7')) { |
| 629 |
value = (value << 3) + (GotByte & 7); |
| 630 |
++digits; |
| 631 |
GetByte(); |
| 632 |
} |
| 633 |
|
| 634 |
if (config.honor_leading_zeroes) { |
| 635 |
if (digits > 3) { |
| 636 |
if (digits > 6) { |
| 637 |
if (value < 65536) |
| 638 |
flags |= NUMBER_FORCES_24; |
| 639 |
} else { |
| 640 |
if (value < 256) |
| 641 |
flags |= NUMBER_FORCES_16; |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
PUSH_INT_ARG(value, flags, 0); |
| 646 |
|
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
static void parse_function_call(void) |
| 652 |
{ |
| 653 |
void *node_body; |
| 654 |
|
| 655 |
|
| 656 |
dynabuf_to_lower(function_dyna_buf, GlobalDynaBuf); |
| 657 |
|
| 658 |
if (tree_easy_scan(function_tree, &node_body, function_dyna_buf)) { |
| 659 |
PUSH_OP((struct op *) node_body); |
| 660 |
} else { |
| 661 |
throw_error("Unknown function."); |
| 662 |
alu_state = STATE_ERROR; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
static void list_init_list(struct object *self) |
| 669 |
{ |
| 670 |
self->type = &type_list; |
| 671 |
self->u.listhead = safe_malloc(sizeof(*(self->u.listhead))); |
| 672 |
self->u.listhead->next = self->u.listhead; |
| 673 |
self->u.listhead->prev = self->u.listhead; |
| 674 |
self->u.listhead->u.listinfo.length = 0; |
| 675 |
self->u.listhead->u.listinfo.refs = 1; |
| 676 |
} |
| 677 |
|
| 678 |
static void list_append_object(struct listitem *head, const struct object *obj) |
| 679 |
{ |
| 680 |
struct listitem *item; |
| 681 |
|
| 682 |
item = safe_malloc(sizeof(*item)); |
| 683 |
item->u.payload = *obj; |
| 684 |
item->next = head; |
| 685 |
item->prev = head->prev; |
| 686 |
item->next->prev = item; |
| 687 |
item->prev->next = item; |
| 688 |
head->u.listinfo.length++; |
| 689 |
} |
| 690 |
|
| 691 |
static void list_append_list(struct listitem *selfhead, struct listitem *otherhead) |
| 692 |
{ |
| 693 |
struct listitem *item; |
| 694 |
|
| 695 |
if (selfhead == otherhead) |
| 696 |
BUG("ExtendingListWithItself", 0); |
| 697 |
item = otherhead->next; |
| 698 |
while (item != otherhead) { |
| 699 |
list_append_object(selfhead, &item->u.payload); |
| 700 |
item = item->next; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
static int parse_octal_or_unpseudo(void) |
| 708 |
{ |
| 709 |
unsigned int unpseudo_count = 1; |
| 710 |
struct pseudopc *context; |
| 711 |
struct symbol *symbol; |
| 712 |
scope_t scope; |
| 713 |
|
| 714 |
|
| 715 |
while (GetByte() == '&') |
| 716 |
++unpseudo_count; |
| 717 |
|
| 718 |
|
| 719 |
if ((unpseudo_count == 1) && (GotByte >= '0') & (GotByte <= '7')) { |
| 720 |
parse_octal_literal(); |
| 721 |
return 0; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
if (GotByte == '*') { |
| 726 |
|
| 727 |
parse_program_counter(); |
| 728 |
context = pseudopc_get_context(); |
| 729 |
} else if (BYTE_STARTS_KEYWORD(GotByte) || (GotByte == LOCAL_PREFIX) || (GotByte == CHEAP_PREFIX)) { |
| 730 |
|
| 731 |
if (input_read_scope_and_symbol_name(&scope)) |
| 732 |
return 1; |
| 733 |
|
| 734 |
symbol = get_symbol_value(scope, GlobalDynaBuf->size - 1, FALSE); |
| 735 |
if (symbol->object.type != &type_number) { |
| 736 |
throw_error("Un-pseudopc operator '&' can only be applied to number symbols."); |
| 737 |
return 0; |
| 738 |
} |
| 739 |
context = symbol->pseudopc; |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
} else { |
| 744 |
throw_error("Expected octal value or symbol name after '&'."); |
| 745 |
return 1; |
| 746 |
} |
| 747 |
|
| 748 |
pseudopc_unpseudo(&arg_stack[arg_sp - 1].u.number, context, unpseudo_count); |
| 749 |
return 0; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
static void eor_is_obsolete(void) |
| 757 |
{ |
| 758 |
if (config.dialect >= V0_98__PATHS_AND_SYMBOLCHANGE) |
| 759 |
throw_error("the \"EOR\" operator is obsolete; use \"XOR\" instead."); |
| 760 |
else |
| 761 |
throw_finalpass_warning("\"EOR\" is deprecated; use \"XOR\" instead."); |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
#define PREVIOUS_ARGUMENT (arg_stack[arg_sp - 2]) |
| 770 |
#define NEWEST_ARGUMENT (arg_stack[arg_sp - 1]) |
| 771 |
#define PREVIOUS_OPERATOR (op_stack[op_sp - 2]) |
| 772 |
#define NEWEST_OPERATOR (op_stack[op_sp - 1]) |
| 773 |
static void handle_special_operator(struct expression *expression, enum op_id previous) |
| 774 |
{ |
| 775 |
|
| 776 |
switch (previous) { |
| 777 |
case OPID_START_EXPRESSION: |
| 778 |
alu_state = STATE_END; |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
PREVIOUS_OPERATOR = NEWEST_OPERATOR; |
| 783 |
--op_sp; |
| 784 |
break; |
| 785 |
case OPID_SUBEXPR_PAREN: |
| 786 |
expression->is_parenthesized = TRUE; |
| 787 |
if (GotByte == ')') { |
| 788 |
|
| 789 |
GetByte(); |
| 790 |
op_sp -= 2; |
| 791 |
alu_state = STATE_EXPECT_DYADIC_OP; |
| 792 |
} else { |
| 793 |
|
| 794 |
++(expression->open_parentheses); |
| 795 |
|
| 796 |
PREVIOUS_OPERATOR = NEWEST_OPERATOR; |
| 797 |
--op_sp; |
| 798 |
} |
| 799 |
break; |
| 800 |
case OPID_START_LIST: |
| 801 |
if (GotByte == ',') { |
| 802 |
GetByte(); |
| 803 |
NEWEST_OPERATOR = &ops_list_append; |
| 804 |
} else if (GotByte == ']') { |
| 805 |
GetByte(); |
| 806 |
op_sp -= 2; |
| 807 |
alu_state = STATE_EXPECT_DYADIC_OP; |
| 808 |
} else { |
| 809 |
|
| 810 |
throw_error("Unterminated list."); |
| 811 |
alu_state = STATE_ERROR; |
| 812 |
|
| 813 |
PREVIOUS_OPERATOR = NEWEST_OPERATOR; |
| 814 |
--op_sp; |
| 815 |
} |
| 816 |
break; |
| 817 |
case OPID_SUBEXPR_BRACKET: |
| 818 |
if (GotByte == ']') { |
| 819 |
GetByte(); |
| 820 |
op_sp -= 2; |
| 821 |
alu_state = STATE_EXPECT_DYADIC_OP; |
| 822 |
} else { |
| 823 |
|
| 824 |
throw_error("Unterminated index spec."); |
| 825 |
alu_state = STATE_ERROR; |
| 826 |
|
| 827 |
PREVIOUS_OPERATOR = NEWEST_OPERATOR; |
| 828 |
--op_sp; |
| 829 |
} |
| 830 |
break; |
| 831 |
default: |
| 832 |
BUG("IllegalOperatorId", previous); |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
static void push_dyadic_and_check(struct expression *expression, struct op *op) |
| 840 |
{ |
| 841 |
PUSH_OP(op); |
| 842 |
if (alu_state < STATE_MAX_GO_ON) |
| 843 |
alu_state = STATE_EXPECT_ARG_OR_MONADIC_OP; |
| 844 |
while (alu_state == STATE_EXPECT_ARG_OR_MONADIC_OP) { |
| 845 |
|
| 846 |
|
| 847 |
if (op_sp < 2) |
| 848 |
return; |
| 849 |
|
| 850 |
|
| 851 |
if (PREVIOUS_OPERATOR->priority < NEWEST_OPERATOR->priority) |
| 852 |
return; |
| 853 |
|
| 854 |
|
| 855 |
if ((PREVIOUS_OPERATOR->priority == NEWEST_OPERATOR->priority) |
| 856 |
&& (NEWEST_OPERATOR->priority == PRIO_POWEROF)) { |
| 857 |
if (config.dialect >= V0_94_6__RIGHT_ASSOC_POWER) { |
| 858 |
return; |
| 859 |
} else { |
| 860 |
|
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
switch (PREVIOUS_OPERATOR->group) { |
| 866 |
case OPGROUP_MONADIC: |
| 867 |
|
| 868 |
if (arg_sp < 1) |
| 869 |
BUG("ArgStackEmpty", arg_sp); |
| 870 |
NEWEST_ARGUMENT.type->monadic_op(&NEWEST_ARGUMENT, PREVIOUS_OPERATOR); |
| 871 |
expression->is_parenthesized = FALSE; |
| 872 |
|
| 873 |
PREVIOUS_OPERATOR = NEWEST_OPERATOR; |
| 874 |
--op_sp; |
| 875 |
break; |
| 876 |
case OPGROUP_DYADIC: |
| 877 |
|
| 878 |
if (arg_sp < 2) |
| 879 |
BUG("NotEnoughArgs", arg_sp); |
| 880 |
PREVIOUS_ARGUMENT.type->dyadic_op(&PREVIOUS_ARGUMENT, PREVIOUS_OPERATOR, &NEWEST_ARGUMENT); |
| 881 |
expression->is_parenthesized = FALSE; |
| 882 |
|
| 883 |
PREVIOUS_OPERATOR = NEWEST_OPERATOR; |
| 884 |
--op_sp; |
| 885 |
--arg_sp; |
| 886 |
break; |
| 887 |
case OPGROUP_SPECIAL: |
| 888 |
|
| 889 |
if (NEWEST_OPERATOR->id != OPID_TERMINATOR) |
| 890 |
BUG("StrangeOperator", NEWEST_OPERATOR->id); |
| 891 |
handle_special_operator(expression, PREVIOUS_OPERATOR->id); |
| 892 |
|
| 893 |
break; |
| 894 |
default: |
| 895 |
BUG("IllegalOperatorGroup", PREVIOUS_OPERATOR->group); |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
static boolean expect_argument_or_monadic_operator(struct expression *expression) |
| 905 |
{ |
| 906 |
struct op *op; |
| 907 |
int ugly_length_kluge; |
| 908 |
boolean perform_negation; |
| 909 |
scope_t scope; |
| 910 |
|
| 911 |
SKIPSPACE(); |
| 912 |
switch (GotByte) { |
| 913 |
case '+': |
| 914 |
|
| 915 |
dynabuf_clear(GlobalDynaBuf); |
| 916 |
do { |
| 917 |
DYNABUF_APPEND(GlobalDynaBuf, '+'); |
| 918 |
} while (GetByte() == '+'); |
| 919 |
ugly_length_kluge = GlobalDynaBuf->size; |
| 920 |
symbol_fix_forward_anon_name(FALSE); |
| 921 |
get_symbol_value(section_now->local_scope, ugly_length_kluge, FALSE); |
| 922 |
goto now_expect_dyadic_op; |
| 923 |
|
| 924 |
case '-': |
| 925 |
|
| 926 |
perform_negation = FALSE; |
| 927 |
dynabuf_clear(GlobalDynaBuf); |
| 928 |
do { |
| 929 |
DYNABUF_APPEND(GlobalDynaBuf, '-'); |
| 930 |
perform_negation = !perform_negation; |
| 931 |
} while (GetByte() == '-'); |
| 932 |
SKIPSPACE(); |
| 933 |
if (BYTE_FOLLOWS_ANON(GotByte)) { |
| 934 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 935 |
get_symbol_value(section_now->local_scope, GlobalDynaBuf->size - 1, TRUE); |
| 936 |
goto now_expect_dyadic_op; |
| 937 |
} |
| 938 |
|
| 939 |
if (perform_negation) |
| 940 |
PUSH_OP(&ops_negate); |
| 941 |
|
| 942 |
break; |
| 943 |
|
| 944 |
case '!': |
| 945 |
op = &ops_not; |
| 946 |
goto get_byte_and_push_monadic; |
| 947 |
|
| 948 |
case '<': |
| 949 |
op = &ops_low_byte_of; |
| 950 |
goto get_byte_and_push_monadic; |
| 951 |
|
| 952 |
case '>': |
| 953 |
op = &ops_high_byte_of; |
| 954 |
goto get_byte_and_push_monadic; |
| 955 |
|
| 956 |
case '^': |
| 957 |
op = &ops_bank_byte_of; |
| 958 |
goto get_byte_and_push_monadic; |
| 959 |
|
| 960 |
|
| 961 |
case '[': |
| 962 |
list_init_list(&arg_stack[arg_sp++]); |
| 963 |
NEXTANDSKIPSPACE(); |
| 964 |
if (GotByte == ']') { |
| 965 |
|
| 966 |
GetByte(); |
| 967 |
goto now_expect_dyadic_op; |
| 968 |
|
| 969 |
} else { |
| 970 |
|
| 971 |
PUSH_OP(&ops_start_list); |
| 972 |
push_dyadic_and_check(expression, &ops_list_append); |
| 973 |
|
| 974 |
} |
| 975 |
break; |
| 976 |
|
| 977 |
case '(': |
| 978 |
op = &ops_subexpr_paren; |
| 979 |
goto get_byte_and_push_monadic; |
| 980 |
|
| 981 |
|
| 982 |
case '"': |
| 983 |
case '\'': |
| 984 |
|
| 985 |
parse_quoted(GotByte); |
| 986 |
|
| 987 |
goto now_expect_dyadic_op; |
| 988 |
|
| 989 |
case '%': |
| 990 |
parse_binary_literal(); |
| 991 |
goto now_expect_dyadic_op; |
| 992 |
|
| 993 |
case '&': |
| 994 |
if (parse_octal_or_unpseudo() == 0) |
| 995 |
goto now_expect_dyadic_op; |
| 996 |
|
| 997 |
|
| 998 |
alu_state = STATE_ERROR; |
| 999 |
break; |
| 1000 |
case '$': |
| 1001 |
parse_hex_literal(); |
| 1002 |
|
| 1003 |
goto now_expect_dyadic_op; |
| 1004 |
|
| 1005 |
case '*': |
| 1006 |
parse_program_counter(); |
| 1007 |
|
| 1008 |
goto now_expect_dyadic_op; |
| 1009 |
|
| 1010 |
|
| 1011 |
case '.': |
| 1012 |
GetByte(); |
| 1013 |
|
| 1014 |
if ((GotByte >= '0') && (GotByte <= '9')) { |
| 1015 |
parse_frac_part(0); |
| 1016 |
goto now_expect_dyadic_op; |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
if (input_read_scope_and_symbol_name_KLUGED(&scope) == 0) { |
| 1021 |
get_symbol_value(scope, GlobalDynaBuf->size - 1, FALSE); |
| 1022 |
goto now_expect_dyadic_op; |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
alu_state = STATE_ERROR; |
| 1027 |
break; |
| 1028 |
case CHEAP_PREFIX: |
| 1029 |
|
| 1030 |
if (input_read_scope_and_symbol_name(&scope) == 0) { |
| 1031 |
get_symbol_value(scope, GlobalDynaBuf->size - 1, FALSE); |
| 1032 |
goto now_expect_dyadic_op; |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
alu_state = STATE_ERROR; |
| 1037 |
break; |
| 1038 |
|
| 1039 |
default: |
| 1040 |
if ((GotByte >= '0') && (GotByte <= '9')) { |
| 1041 |
parse_number_literal(); |
| 1042 |
|
| 1043 |
goto now_expect_dyadic_op; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (BYTE_STARTS_KEYWORD(GotByte)) { |
| 1047 |
register int length; |
| 1048 |
|
| 1049 |
|
| 1050 |
length = parser_read_keyword(); |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
if ((length == 3) |
| 1055 |
&& ((GlobalDynaBuf->buffer[0] | 32) == 'n') |
| 1056 |
&& ((GlobalDynaBuf->buffer[1] | 32) == 'o') |
| 1057 |
&& ((GlobalDynaBuf->buffer[2] | 32) == 't')) { |
| 1058 |
PUSH_OP(&ops_not); |
| 1059 |
|
| 1060 |
} else { |
| 1061 |
if (GotByte == '(') { |
| 1062 |
parse_function_call(); |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
} else { |
| 1071 |
get_symbol_value(SCOPE_GLOBAL, GlobalDynaBuf->size - 1, FALSE); |
| 1072 |
goto now_expect_dyadic_op; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} else { |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
PUSH_INT_ARG(0, 0, 0); |
| 1080 |
if (op_stack[op_sp - 1] == &ops_start_expression) { |
| 1081 |
push_dyadic_and_check(expression, &ops_terminating_char); |
| 1082 |
} else { |
| 1083 |
throw_error(exception_syntax); |
| 1084 |
alu_state = STATE_ERROR; |
| 1085 |
} |
| 1086 |
return FALSE; |
| 1087 |
} |
| 1088 |
break; |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
get_byte_and_push_monadic: |
| 1093 |
GetByte(); |
| 1094 |
PUSH_OP(op); |
| 1095 |
|
| 1096 |
break; |
| 1097 |
|
| 1098 |
now_expect_dyadic_op: |
| 1099 |
|
| 1100 |
if (alu_state < STATE_MAX_GO_ON) |
| 1101 |
alu_state = STATE_EXPECT_DYADIC_OP; |
| 1102 |
break; |
| 1103 |
} |
| 1104 |
|
| 1105 |
return TRUE; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
static void expect_dyadic_operator(struct expression *expression) |
| 1111 |
{ |
| 1112 |
void *node_body; |
| 1113 |
struct op *op; |
| 1114 |
|
| 1115 |
SKIPSPACE(); |
| 1116 |
switch (GotByte) { |
| 1117 |
|
| 1118 |
case '^': |
| 1119 |
op = &ops_powerof; |
| 1120 |
goto get_byte_and_push_dyadic; |
| 1121 |
|
| 1122 |
case '+': |
| 1123 |
op = &ops_add; |
| 1124 |
goto get_byte_and_push_dyadic; |
| 1125 |
|
| 1126 |
case '-': |
| 1127 |
op = &ops_subtract; |
| 1128 |
goto get_byte_and_push_dyadic; |
| 1129 |
|
| 1130 |
case '*': |
| 1131 |
op = &ops_multiply; |
| 1132 |
goto get_byte_and_push_dyadic; |
| 1133 |
|
| 1134 |
case '/': |
| 1135 |
op = &ops_divide; |
| 1136 |
goto get_byte_and_push_dyadic; |
| 1137 |
|
| 1138 |
case '%': |
| 1139 |
op = &ops_modulo; |
| 1140 |
goto get_byte_and_push_dyadic; |
| 1141 |
|
| 1142 |
case '&': |
| 1143 |
op = &ops_and; |
| 1144 |
goto get_byte_and_push_dyadic; |
| 1145 |
|
| 1146 |
case '|': |
| 1147 |
op = &ops_or; |
| 1148 |
goto get_byte_and_push_dyadic; |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
case '=': |
| 1156 |
op = &ops_equals; |
| 1157 |
|
| 1158 |
if (GetByte() == '=') { |
| 1159 |
GetByte(); |
| 1160 |
} else { |
| 1161 |
|
| 1162 |
} |
| 1163 |
goto push_dyadic_op; |
| 1164 |
|
| 1165 |
case '[': |
| 1166 |
GetByte(); |
| 1167 |
|
| 1168 |
|
| 1169 |
push_dyadic_and_check(expression, &ops_atindex); |
| 1170 |
|
| 1171 |
PUSH_OP(&ops_subexpr_bracket); |
| 1172 |
return; |
| 1173 |
|
| 1174 |
|
| 1175 |
case '!': |
| 1176 |
GetByte(); |
| 1177 |
if (parser_expect('=')) { |
| 1178 |
op = &ops_not_equal; |
| 1179 |
goto push_dyadic_op; |
| 1180 |
} |
| 1181 |
|
| 1182 |
alu_state = STATE_ERROR; |
| 1183 |
break; |
| 1184 |
case '<': |
| 1185 |
switch (GetByte()) { |
| 1186 |
case '=': |
| 1187 |
op = &ops_less_or_equal; |
| 1188 |
goto get_byte_and_push_dyadic; |
| 1189 |
|
| 1190 |
case '<': |
| 1191 |
op = &ops_shift_left; |
| 1192 |
goto get_byte_and_push_dyadic; |
| 1193 |
|
| 1194 |
case '>': |
| 1195 |
op = &ops_not_equal; |
| 1196 |
goto get_byte_and_push_dyadic; |
| 1197 |
|
| 1198 |
default: |
| 1199 |
op = &ops_less_than; |
| 1200 |
goto push_dyadic_op; |
| 1201 |
|
| 1202 |
} |
| 1203 |
|
| 1204 |
case '>': |
| 1205 |
switch (GetByte()) { |
| 1206 |
case '=': |
| 1207 |
op = &ops_greater_or_equal; |
| 1208 |
goto get_byte_and_push_dyadic; |
| 1209 |
|
| 1210 |
case '<': |
| 1211 |
op = &ops_not_equal; |
| 1212 |
goto get_byte_and_push_dyadic; |
| 1213 |
|
| 1214 |
case '>': |
| 1215 |
op = &ops_asr; |
| 1216 |
if (GetByte() != '>') |
| 1217 |
goto push_dyadic_op; |
| 1218 |
|
| 1219 |
op = &ops_lsr; |
| 1220 |
goto get_byte_and_push_dyadic; |
| 1221 |
|
| 1222 |
default: |
| 1223 |
op = &ops_greater_than; |
| 1224 |
goto push_dyadic_op; |
| 1225 |
|
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
default: |
| 1230 |
|
| 1231 |
if (BYTE_STARTS_KEYWORD(GotByte)) { |
| 1232 |
parser_read_and_lower_keyword(); |
| 1233 |
|
| 1234 |
|
| 1235 |
if (tree_easy_scan(op_tree, &node_body, GlobalDynaBuf)) { |
| 1236 |
op = node_body; |
| 1237 |
goto push_dyadic_op; |
| 1238 |
} |
| 1239 |
|
| 1240 |
throw_error("Unknown operator."); |
| 1241 |
alu_state = STATE_ERROR; |
| 1242 |
|
| 1243 |
} else { |
| 1244 |
|
| 1245 |
op = &ops_terminating_char; |
| 1246 |
goto push_dyadic_op; |
| 1247 |
} |
| 1248 |
} |
| 1249 |
|
| 1250 |
return; |
| 1251 |
|
| 1252 |
|
| 1253 |
get_byte_and_push_dyadic: |
| 1254 |
GetByte(); |
| 1255 |
push_dyadic_op: |
| 1256 |
push_dyadic_and_check(expression, op); |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
static void unsupported_operation(const struct object *optional, const struct op *op, const struct object *arg) |
| 1262 |
{ |
| 1263 |
if (optional) { |
| 1264 |
if (op->group != OPGROUP_DYADIC) |
| 1265 |
BUG("OperatorIsNotDyadic", op->id); |
| 1266 |
} else { |
| 1267 |
if (op->group != OPGROUP_MONADIC) |
| 1268 |
BUG("OperatorIsNotMonadic", op->id); |
| 1269 |
} |
| 1270 |
dynabuf_clear(errormsg_dyna_buf); |
| 1271 |
dynabuf_add_string(errormsg_dyna_buf, "Operation not supported: Cannot apply \""); |
| 1272 |
dynabuf_add_string(errormsg_dyna_buf, op->text_version); |
| 1273 |
dynabuf_add_string(errormsg_dyna_buf, "\" to \""); |
| 1274 |
if (optional) { |
| 1275 |
dynabuf_add_string(errormsg_dyna_buf, optional->type->name); |
| 1276 |
dynabuf_add_string(errormsg_dyna_buf, "\" and \""); |
| 1277 |
} |
| 1278 |
dynabuf_add_string(errormsg_dyna_buf, arg->type->name); |
| 1279 |
dynabuf_add_string(errormsg_dyna_buf, "\"."); |
| 1280 |
dynabuf_append(errormsg_dyna_buf, '\0'); |
| 1281 |
throw_error(errormsg_dyna_buf->buffer); |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
static void int_create_byte(struct object *self, intval_t byte) |
| 1291 |
{ |
| 1292 |
self->type = &type_number; |
| 1293 |
self->u.number.ntype = NUMTYPE_INT; |
| 1294 |
self->u.number.flags = 0; |
| 1295 |
self->u.number.val.intval = byte; |
| 1296 |
self->u.number.addr_refs = 0; |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
inline static void int_to_float(struct object *self) |
| 1302 |
{ |
| 1303 |
self->u.number.ntype = NUMTYPE_FLOAT; |
| 1304 |
self->u.number.val.fpval = self->u.number.val.intval; |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
inline static void float_to_int(struct object *self) |
| 1310 |
{ |
| 1311 |
self->u.number.ntype = NUMTYPE_INT; |
| 1312 |
self->u.number.val.intval = self->u.number.val.fpval; |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
static void list_to_item(struct object *self, int index) |
| 1318 |
{ |
| 1319 |
struct listitem *item; |
| 1320 |
|
| 1321 |
item = self->u.listhead->next; |
| 1322 |
while (index) { |
| 1323 |
item = item->next; |
| 1324 |
--index; |
| 1325 |
} |
| 1326 |
self->u.listhead->u.listinfo.refs--; |
| 1327 |
*self = item->u.payload; |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
static void string_to_byte(struct object *self, int index) |
| 1333 |
{ |
| 1334 |
intval_t byte; |
| 1335 |
|
| 1336 |
byte = encoding_encode_char(self->u.string->payload[index]); |
| 1337 |
self->u.string->refs--; |
| 1338 |
int_create_byte(self, byte); |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
static boolean number_is_defined(const struct object *self) |
| 1344 |
{ |
| 1345 |
return self->u.number.ntype != NUMTYPE_UNDEFINED; |
| 1346 |
} |
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
static boolean list_is_defined(const struct object *self) |
| 1351 |
{ |
| 1352 |
struct listitem *item; |
| 1353 |
|
| 1354 |
|
| 1355 |
item = self->u.listhead->next; |
| 1356 |
while (item != self->u.listhead) { |
| 1357 |
if (!(item->u.payload.type->is_defined(&item->u.payload))) |
| 1358 |
return FALSE; |
| 1359 |
|
| 1360 |
item = item->next; |
| 1361 |
} |
| 1362 |
|
| 1363 |
return TRUE; |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
static boolean object_return_true(const struct object *self) |
| 1369 |
{ |
| 1370 |
return TRUE; |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
static boolean number_differs(const struct object *self, const struct object *other) |
| 1377 |
{ |
| 1378 |
if (self->u.number.ntype == NUMTYPE_UNDEFINED) |
| 1379 |
return FALSE; |
| 1380 |
|
| 1381 |
if (other->u.number.ntype == NUMTYPE_UNDEFINED) |
| 1382 |
return FALSE; |
| 1383 |
|
| 1384 |
if (self->u.number.ntype == NUMTYPE_INT) { |
| 1385 |
if (other->u.number.ntype == NUMTYPE_INT) |
| 1386 |
return self->u.number.val.intval != other->u.number.val.intval; |
| 1387 |
else |
| 1388 |
return self->u.number.val.intval != other->u.number.val.fpval; |
| 1389 |
} else { |
| 1390 |
if (other->u.number.ntype == NUMTYPE_INT) |
| 1391 |
return self->u.number.val.fpval != other->u.number.val.intval; |
| 1392 |
else |
| 1393 |
return self->u.number.val.fpval != other->u.number.val.fpval; |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
static boolean list_differs(const struct object *self, const struct object *other) |
| 1399 |
{ |
| 1400 |
struct listitem *arthur, |
| 1401 |
*ford; |
| 1402 |
|
| 1403 |
if (self->u.listhead->u.listinfo.length != other->u.listhead->u.listinfo.length) |
| 1404 |
return TRUE; |
| 1405 |
|
| 1406 |
|
| 1407 |
arthur = self->u.listhead->next; |
| 1408 |
ford = other->u.listhead->next; |
| 1409 |
while (arthur != self->u.listhead) { |
| 1410 |
if (ford == other->u.listhead) |
| 1411 |
BUG("ListLengthError", 0); |
| 1412 |
if (arthur->u.payload.type != ford->u.payload.type) |
| 1413 |
return TRUE; |
| 1414 |
|
| 1415 |
if (arthur->u.payload.type->differs(&arthur->u.payload, &ford->u.payload)) |
| 1416 |
return TRUE; |
| 1417 |
|
| 1418 |
arthur = arthur->next; |
| 1419 |
ford = ford->next; |
| 1420 |
} |
| 1421 |
if (ford != other->u.listhead) |
| 1422 |
BUG("ListLengthError", 1); |
| 1423 |
return FALSE; |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
static boolean string_differs(const struct object *self, const struct object *other) |
| 1428 |
{ |
| 1429 |
if (self->u.string->length != other->u.string->length) |
| 1430 |
return TRUE; |
| 1431 |
|
| 1432 |
return !!memcmp(self->u.string->payload, other->u.string->payload, self->u.string->length); |
| 1433 |
} |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
static boolean number_assign(struct object *self, const struct object *new_value, boolean accept_change) |
| 1438 |
{ |
| 1439 |
bits own_flags = self->u.number.flags, |
| 1440 |
other_flags = new_value->u.number.flags; |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
boolean redefined = FALSE; |
| 1445 |
|
| 1446 |
|
| 1447 |
if (accept_change) { |
| 1448 |
self->u.number.ntype = NUMTYPE_UNDEFINED; |
| 1449 |
own_flags &= ~(NUMBER_FITS_BYTE); |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
if (self->u.number.ntype != NUMTYPE_UNDEFINED) { |
| 1454 |
|
| 1455 |
|
| 1456 |
if (number_differs(self, new_value)) |
| 1457 |
redefined = TRUE; |
| 1458 |
} |
| 1459 |
|
| 1460 |
*self = *new_value; |
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
if ((own_flags & NUMBER_FORCEBITS) == 0) |
| 1466 |
own_flags = (own_flags & ~NUMBER_FORCEBITS) | (other_flags & NUMBER_FORCEBITS); |
| 1467 |
|
| 1468 |
|
| 1469 |
if ((own_flags & (NUMBER_EVER_UNDEFINED | NUMBER_FITS_BYTE)) == NUMBER_EVER_UNDEFINED) |
| 1470 |
other_flags &= ~NUMBER_FITS_BYTE; |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
own_flags |= other_flags & (NUMBER_FITS_BYTE | NUMBER_EVER_UNDEFINED); |
| 1476 |
|
| 1477 |
self->u.number.flags = own_flags; |
| 1478 |
return redefined; |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
static boolean list_assign(struct object *self, const struct object *new_value, boolean accept_change) |
| 1485 |
{ |
| 1486 |
boolean redefined = FALSE; |
| 1487 |
|
| 1488 |
if ((!accept_change) && list_differs(self, new_value)) |
| 1489 |
redefined = TRUE; |
| 1490 |
|
| 1491 |
*self = *new_value; |
| 1492 |
return redefined; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
static boolean string_assign(struct object *self, const struct object *new_value, boolean accept_change) |
| 1498 |
{ |
| 1499 |
boolean redefined = FALSE; |
| 1500 |
|
| 1501 |
if ((!accept_change) && string_differs(self, new_value)) |
| 1502 |
redefined = TRUE; |
| 1503 |
|
| 1504 |
*self = *new_value; |
| 1505 |
return redefined; |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
static void undef_handle_monadic_operator(struct object *self, const struct op *op) |
| 1512 |
{ |
| 1513 |
switch (op->id) { |
| 1514 |
case OPID_INT: |
| 1515 |
case OPID_FLOAT: |
| 1516 |
self->u.number.addr_refs = 0; |
| 1517 |
break; |
| 1518 |
case OPID_SIN: |
| 1519 |
case OPID_COS: |
| 1520 |
case OPID_TAN: |
| 1521 |
case OPID_ARCSIN: |
| 1522 |
case OPID_ARCCOS: |
| 1523 |
case OPID_ARCTAN: |
| 1524 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1525 |
self->u.number.addr_refs = 0; |
| 1526 |
break; |
| 1527 |
case OPID_NOT: |
| 1528 |
case OPID_NEGATE: |
| 1529 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1530 |
self->u.number.addr_refs = -(self->u.number.addr_refs); |
| 1531 |
break; |
| 1532 |
case OPID_LOWBYTEOF: |
| 1533 |
case OPID_HIGHBYTEOF: |
| 1534 |
case OPID_BANKBYTEOF: |
| 1535 |
self->u.number.flags |= NUMBER_FITS_BYTE; |
| 1536 |
self->u.number.flags &= ~NUMBER_FORCEBITS; |
| 1537 |
self->u.number.addr_refs = 0; |
| 1538 |
break; |
| 1539 |
case OPID_DEC: |
| 1540 |
case OPID_HEX: |
| 1541 |
|
| 1542 |
|
| 1543 |
string_prepare_string(self, 0); |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
++pass.counters.undefineds; |
| 1548 |
break; |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
default: |
| 1553 |
unsupported_operation(NULL, op, self); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
#define NUMBUFSIZE 64 // large enough(tm) even for 64bit systems |
| 1561 |
static void int_to_string(struct object *self, const char formatstring[]) |
| 1562 |
{ |
| 1563 |
char buffer[NUMBUFSIZE]; |
| 1564 |
int length; |
| 1565 |
|
| 1566 |
#if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L |
| 1567 |
length = snprintf(buffer, NUMBUFSIZE, formatstring, (long) self->u.number.val.intval); |
| 1568 |
#else |
| 1569 |
length = sprintf(buffer, formatstring, (long) self->u.number.val.intval); |
| 1570 |
#endif |
| 1571 |
string_prepare_string(self, length); |
| 1572 |
|
| 1573 |
memcpy(self->u.string->payload, buffer, length); |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
static void float_handle_monadic_operator(struct object *self, const struct op *op); |
| 1578 |
|
| 1579 |
|
| 1580 |
static void int_handle_monadic_operator(struct object *self, const struct op *op) |
| 1581 |
{ |
| 1582 |
int refs = 0; |
| 1583 |
|
| 1584 |
switch (op->id) { |
| 1585 |
case OPID_INT: |
| 1586 |
break; |
| 1587 |
case OPID_FLOAT: |
| 1588 |
int_to_float(self); |
| 1589 |
break; |
| 1590 |
case OPID_SIN: |
| 1591 |
case OPID_COS: |
| 1592 |
case OPID_TAN: |
| 1593 |
case OPID_ARCSIN: |
| 1594 |
case OPID_ARCCOS: |
| 1595 |
case OPID_ARCTAN: |
| 1596 |
|
| 1597 |
int_to_float(self); |
| 1598 |
float_handle_monadic_operator(self, op); |
| 1599 |
return; |
| 1600 |
|
| 1601 |
case OPID_NOT: |
| 1602 |
self->u.number.val.intval = ~(self->u.number.val.intval); |
| 1603 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1604 |
refs = -(self->u.number.addr_refs); |
| 1605 |
break; |
| 1606 |
case OPID_NEGATE: |
| 1607 |
self->u.number.val.intval = -(self->u.number.val.intval); |
| 1608 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1609 |
refs = -(self->u.number.addr_refs); |
| 1610 |
break; |
| 1611 |
case OPID_LOWBYTEOF: |
| 1612 |
self->u.number.val.intval = (self->u.number.val.intval) & 255; |
| 1613 |
self->u.number.flags |= NUMBER_FITS_BYTE; |
| 1614 |
self->u.number.flags &= ~NUMBER_FORCEBITS; |
| 1615 |
break; |
| 1616 |
case OPID_HIGHBYTEOF: |
| 1617 |
self->u.number.val.intval = ((self->u.number.val.intval) >> 8) & 255; |
| 1618 |
self->u.number.flags |= NUMBER_FITS_BYTE; |
| 1619 |
self->u.number.flags &= ~NUMBER_FORCEBITS; |
| 1620 |
break; |
| 1621 |
case OPID_BANKBYTEOF: |
| 1622 |
self->u.number.val.intval = ((self->u.number.val.intval) >> 16) & 255; |
| 1623 |
self->u.number.flags |= NUMBER_FITS_BYTE; |
| 1624 |
self->u.number.flags &= ~NUMBER_FORCEBITS; |
| 1625 |
break; |
| 1626 |
case OPID_DEC: |
| 1627 |
int_to_string(self, "%ld"); |
| 1628 |
break; |
| 1629 |
case OPID_HEX: |
| 1630 |
int_to_string(self, "%lx"); |
| 1631 |
break; |
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
default: |
| 1636 |
unsupported_operation(NULL, op, self); |
| 1637 |
} |
| 1638 |
self->u.number.addr_refs = refs; |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
static void float_ranged_fn(double (*fn)(double), struct object *self) |
| 1645 |
{ |
| 1646 |
if ((self->u.number.val.fpval >= -1) && (self->u.number.val.fpval <= 1)) { |
| 1647 |
self->u.number.val.fpval = fn(self->u.number.val.fpval); |
| 1648 |
} else { |
| 1649 |
throw_error("Argument out of range."); |
| 1650 |
self->u.number.val.fpval = 0; |
| 1651 |
} |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
static void float_handle_monadic_operator(struct object *self, const struct op *op) |
| 1657 |
{ |
| 1658 |
int refs = 0; |
| 1659 |
|
| 1660 |
switch (op->id) { |
| 1661 |
case OPID_INT: |
| 1662 |
float_to_int(self); |
| 1663 |
break; |
| 1664 |
case OPID_FLOAT: |
| 1665 |
break; |
| 1666 |
case OPID_SIN: |
| 1667 |
self->u.number.val.fpval = sin(self->u.number.val.fpval); |
| 1668 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1669 |
break; |
| 1670 |
case OPID_COS: |
| 1671 |
self->u.number.val.fpval = cos(self->u.number.val.fpval); |
| 1672 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1673 |
break; |
| 1674 |
case OPID_TAN: |
| 1675 |
self->u.number.val.fpval = tan(self->u.number.val.fpval); |
| 1676 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1677 |
break; |
| 1678 |
case OPID_ARCSIN: |
| 1679 |
float_ranged_fn(asin, self); |
| 1680 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1681 |
break; |
| 1682 |
case OPID_ARCCOS: |
| 1683 |
float_ranged_fn(acos, self); |
| 1684 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1685 |
break; |
| 1686 |
case OPID_ARCTAN: |
| 1687 |
self->u.number.val.fpval = atan(self->u.number.val.fpval); |
| 1688 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1689 |
break; |
| 1690 |
case OPID_NEGATE: |
| 1691 |
self->u.number.val.fpval = -(self->u.number.val.fpval); |
| 1692 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1693 |
refs = -(self->u.number.addr_refs); |
| 1694 |
break; |
| 1695 |
case OPID_NOT: |
| 1696 |
case OPID_LOWBYTEOF: |
| 1697 |
case OPID_HIGHBYTEOF: |
| 1698 |
case OPID_BANKBYTEOF: |
| 1699 |
|
| 1700 |
float_to_int(self); |
| 1701 |
int_handle_monadic_operator(self, op); |
| 1702 |
return; |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
default: |
| 1708 |
unsupported_operation(NULL, op, self); |
| 1709 |
} |
| 1710 |
self->u.number.addr_refs = refs; |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
static void number_handle_monadic_operator(struct object *self, const struct op *op) |
| 1716 |
{ |
| 1717 |
|
| 1718 |
switch (op->id) { |
| 1719 |
case OPID_ADDRESS: |
| 1720 |
self->u.number.addr_refs = 1; |
| 1721 |
return; |
| 1722 |
|
| 1723 |
case OPID_ISNUMBER: |
| 1724 |
int_create_byte(self, TRUE); |
| 1725 |
return; |
| 1726 |
|
| 1727 |
case OPID_ISLIST: |
| 1728 |
case OPID_ISSTRING: |
| 1729 |
int_create_byte(self, FALSE); |
| 1730 |
return; |
| 1731 |
|
| 1732 |
default: |
| 1733 |
break; |
| 1734 |
} |
| 1735 |
|
| 1736 |
switch (self->u.number.ntype) { |
| 1737 |
case NUMTYPE_UNDEFINED: |
| 1738 |
undef_handle_monadic_operator(self, op); |
| 1739 |
break; |
| 1740 |
case NUMTYPE_INT: |
| 1741 |
int_handle_monadic_operator(self, op); |
| 1742 |
break; |
| 1743 |
case NUMTYPE_FLOAT: |
| 1744 |
float_handle_monadic_operator(self, op); |
| 1745 |
break; |
| 1746 |
default: |
| 1747 |
BUG("IllegalNumberType1", self->u.number.ntype); |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
static void list_handle_monadic_operator(struct object *self, const struct op *op) |
| 1754 |
{ |
| 1755 |
int length; |
| 1756 |
|
| 1757 |
switch (op->id) { |
| 1758 |
case OPID_LEN: |
| 1759 |
length = self->u.listhead->u.listinfo.length; |
| 1760 |
self->u.listhead->u.listinfo.refs--; |
| 1761 |
self->type = &type_number; |
| 1762 |
self->u.number.ntype = NUMTYPE_INT; |
| 1763 |
self->u.number.flags = 0; |
| 1764 |
self->u.number.val.intval = length; |
| 1765 |
self->u.number.addr_refs = 0; |
| 1766 |
break; |
| 1767 |
case OPID_ISLIST: |
| 1768 |
int_create_byte(self, TRUE); |
| 1769 |
break; |
| 1770 |
case OPID_ISNUMBER: |
| 1771 |
case OPID_ISSTRING: |
| 1772 |
int_create_byte(self, FALSE); |
| 1773 |
break; |
| 1774 |
default: |
| 1775 |
unsupported_operation(NULL, op, self); |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
static void string_handle_monadic_operator(struct object *self, const struct op *op) |
| 1782 |
{ |
| 1783 |
int length; |
| 1784 |
|
| 1785 |
switch (op->id) { |
| 1786 |
case OPID_LEN: |
| 1787 |
length = self->u.string->length; |
| 1788 |
self->u.string->refs--; |
| 1789 |
self->type = &type_number; |
| 1790 |
self->u.number.ntype = NUMTYPE_INT; |
| 1791 |
self->u.number.flags = 0; |
| 1792 |
self->u.number.val.intval = length; |
| 1793 |
self->u.number.addr_refs = 0; |
| 1794 |
break; |
| 1795 |
case OPID_ISNUMBER: |
| 1796 |
case OPID_ISLIST: |
| 1797 |
int_create_byte(self, FALSE); |
| 1798 |
break; |
| 1799 |
case OPID_ISSTRING: |
| 1800 |
int_create_byte(self, TRUE); |
| 1801 |
break; |
| 1802 |
default: |
| 1803 |
unsupported_operation(NULL, op, self); |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
static void intfloat_fix_result_after_comparison(struct object *self, const struct object *other, intval_t result) |
| 1811 |
{ |
| 1812 |
bits flags; |
| 1813 |
|
| 1814 |
self->type = &type_number; |
| 1815 |
self->u.number.ntype = NUMTYPE_INT; |
| 1816 |
self->u.number.val.intval = result; |
| 1817 |
self->u.number.addr_refs = 0; |
| 1818 |
flags = (self->u.number.flags | other->u.number.flags) & NUMBER_EVER_UNDEFINED; |
| 1819 |
flags |= NUMBER_FITS_BYTE; |
| 1820 |
|
| 1821 |
self->u.number.flags = flags; |
| 1822 |
} |
| 1823 |
|
| 1824 |
static void intfloat_fix_result_after_dyadic(struct object *self, const struct object *other) |
| 1825 |
{ |
| 1826 |
self->u.number.flags |= other->u.number.flags & (NUMBER_EVER_UNDEFINED | NUMBER_FORCEBITS); |
| 1827 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1828 |
} |
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
static void undef_handle_dyadic_operator(struct object *self, const struct op *op, struct object *other) |
| 1834 |
{ |
| 1835 |
int refs = 0; |
| 1836 |
|
| 1837 |
switch (op->id) { |
| 1838 |
case OPID_POWEROF: |
| 1839 |
case OPID_MULTIPLY: |
| 1840 |
case OPID_DIVIDE: |
| 1841 |
case OPID_INTDIV: |
| 1842 |
case OPID_MODULO: |
| 1843 |
case OPID_SHIFTLEFT: |
| 1844 |
case OPID_ASR: |
| 1845 |
case OPID_LSR: |
| 1846 |
break; |
| 1847 |
|
| 1848 |
case OPID_SUBTRACT: |
| 1849 |
refs = self->u.number.addr_refs - other->u.number.addr_refs; |
| 1850 |
break; |
| 1851 |
|
| 1852 |
case OPID_LESSOREQUAL: |
| 1853 |
case OPID_LESSTHAN: |
| 1854 |
case OPID_GREATEROREQUAL: |
| 1855 |
case OPID_GREATERTHAN: |
| 1856 |
case OPID_NOTEQUAL: |
| 1857 |
case OPID_EQUALS: |
| 1858 |
|
| 1859 |
self->u.number.flags |= NUMBER_FITS_BYTE; |
| 1860 |
self->u.number.flags &= ~NUMBER_FORCEBITS; |
| 1861 |
goto shared; |
| 1862 |
|
| 1863 |
case OPID_EOR: |
| 1864 |
eor_is_obsolete(); |
| 1865 |
|
| 1866 |
case OPID_XOR: |
| 1867 |
case OPID_AND: |
| 1868 |
case OPID_OR: |
| 1869 |
case OPID_ADD: |
| 1870 |
refs = self->u.number.addr_refs + other->u.number.addr_refs; |
| 1871 |
break; |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
default: |
| 1876 |
unsupported_operation(self, op, other); |
| 1877 |
return; |
| 1878 |
} |
| 1879 |
|
| 1880 |
self->u.number.flags |= (other->u.number.flags & NUMBER_FORCEBITS); |
| 1881 |
self->u.number.flags &= ~NUMBER_FITS_BYTE; |
| 1882 |
shared: |
| 1883 |
self->u.number.flags |= (other->u.number.flags & NUMBER_EVER_UNDEFINED); |
| 1884 |
self->u.number.ntype = NUMTYPE_UNDEFINED; |
| 1885 |
self->u.number.addr_refs = refs; |
| 1886 |
} |
| 1887 |
|
| 1888 |
|
| 1889 |
static void float_handle_dyadic_operator(struct object *self, const struct op *op, struct object *other); |
| 1890 |
|
| 1891 |
|
| 1892 |
static void int_handle_dyadic_operator(struct object *self, const struct op *op, struct object *other) |
| 1893 |
{ |
| 1894 |
int refs = 0; |
| 1895 |
|
| 1896 |
|
| 1897 |
if (other->u.number.ntype == NUMTYPE_INT) { |
| 1898 |
|
| 1899 |
} else if (other->u.number.ntype == NUMTYPE_FLOAT) { |
| 1900 |
|
| 1901 |
switch (op->id) { |
| 1902 |
case OPID_POWEROF: |
| 1903 |
case OPID_MULTIPLY: |
| 1904 |
case OPID_DIVIDE: |
| 1905 |
case OPID_INTDIV: |
| 1906 |
case OPID_ADD: |
| 1907 |
case OPID_SUBTRACT: |
| 1908 |
case OPID_EQUALS: |
| 1909 |
case OPID_LESSOREQUAL: |
| 1910 |
case OPID_LESSTHAN: |
| 1911 |
case OPID_GREATEROREQUAL: |
| 1912 |
case OPID_GREATERTHAN: |
| 1913 |
case OPID_NOTEQUAL: |
| 1914 |
|
| 1915 |
int_to_float(self); |
| 1916 |
float_handle_dyadic_operator(self, op, other); |
| 1917 |
return; |
| 1918 |
|
| 1919 |
case OPID_LSR: |
| 1920 |
case OPID_AND: |
| 1921 |
case OPID_OR: |
| 1922 |
case OPID_EOR: |
| 1923 |
case OPID_XOR: |
| 1924 |
|
| 1925 |
throw_finalpass_warning(exception_float_to_int); |
| 1926 |
|
| 1927 |
case OPID_MODULO: |
| 1928 |
case OPID_SHIFTLEFT: |
| 1929 |
case OPID_ASR: |
| 1930 |
|
| 1931 |
float_to_int(other); |
| 1932 |
break; |
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
default: |
| 1937 |
unsupported_operation(self, op, other); |
| 1938 |
return; |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
} else { |
| 1944 |
unsupported_operation(self, op, other); |
| 1945 |
return; |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
if (other->u.number.ntype != NUMTYPE_INT) |
| 1950 |
BUG("SecondArgIsNotAnInt", op->id); |
| 1951 |
|
| 1952 |
|
| 1953 |
switch (op->id) { |
| 1954 |
case OPID_POWEROF: |
| 1955 |
if (other->u.number.val.intval >= 0) { |
| 1956 |
self->u.number.val.intval = my_pow(self->u.number.val.intval, other->u.number.val.intval); |
| 1957 |
} else { |
| 1958 |
throw_error("Exponent is negative."); |
| 1959 |
self->u.number.val.intval = 0; |
| 1960 |
} |
| 1961 |
break; |
| 1962 |
case OPID_MULTIPLY: |
| 1963 |
self->u.number.val.intval *= other->u.number.val.intval; |
| 1964 |
break; |
| 1965 |
case OPID_DIVIDE: |
| 1966 |
case OPID_INTDIV: |
| 1967 |
if (other->u.number.val.intval) { |
| 1968 |
self->u.number.val.intval /= other->u.number.val.intval; |
| 1969 |
break; |
| 1970 |
} |
| 1971 |
|
| 1972 |
|
| 1973 |
case OPID_MODULO: |
| 1974 |
if (other->u.number.val.intval) { |
| 1975 |
self->u.number.val.intval %= other->u.number.val.intval; |
| 1976 |
} else { |
| 1977 |
throw_error(exception_div_by_zero); |
| 1978 |
self->u.number.val.intval = 0; |
| 1979 |
} |
| 1980 |
break; |
| 1981 |
case OPID_ADD: |
| 1982 |
self->u.number.val.intval += other->u.number.val.intval; |
| 1983 |
refs = self->u.number.addr_refs + other->u.number.addr_refs; |
| 1984 |
break; |
| 1985 |
case OPID_SUBTRACT: |
| 1986 |
self->u.number.val.intval -= other->u.number.val.intval; |
| 1987 |
refs = self->u.number.addr_refs - other->u.number.addr_refs; |
| 1988 |
break; |
| 1989 |
case OPID_SHIFTLEFT: |
| 1990 |
self->u.number.val.intval <<= other->u.number.val.intval; |
| 1991 |
break; |
| 1992 |
case OPID_ASR: |
| 1993 |
self->u.number.val.intval = my_asr(self->u.number.val.intval, other->u.number.val.intval); |
| 1994 |
break; |
| 1995 |
case OPID_LSR: |
| 1996 |
self->u.number.val.intval = ((uintval_t) (self->u.number.val.intval)) >> other->u.number.val.intval; |
| 1997 |
break; |
| 1998 |
case OPID_LESSOREQUAL: |
| 1999 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.intval <= other->u.number.val.intval); |
| 2000 |
return; |
| 2001 |
|
| 2002 |
case OPID_LESSTHAN: |
| 2003 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.intval < other->u.number.val.intval); |
| 2004 |
return; |
| 2005 |
|
| 2006 |
case OPID_GREATEROREQUAL: |
| 2007 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.intval >= other->u.number.val.intval); |
| 2008 |
return; |
| 2009 |
|
| 2010 |
case OPID_GREATERTHAN: |
| 2011 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.intval > other->u.number.val.intval); |
| 2012 |
return; |
| 2013 |
|
| 2014 |
case OPID_NOTEQUAL: |
| 2015 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.intval != other->u.number.val.intval); |
| 2016 |
return; |
| 2017 |
|
| 2018 |
case OPID_EQUALS: |
| 2019 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.intval == other->u.number.val.intval); |
| 2020 |
return; |
| 2021 |
|
| 2022 |
case OPID_AND: |
| 2023 |
self->u.number.val.intval &= other->u.number.val.intval; |
| 2024 |
refs = self->u.number.addr_refs + other->u.number.addr_refs; |
| 2025 |
break; |
| 2026 |
case OPID_OR: |
| 2027 |
self->u.number.val.intval |= other->u.number.val.intval; |
| 2028 |
refs = self->u.number.addr_refs + other->u.number.addr_refs; |
| 2029 |
break; |
| 2030 |
case OPID_EOR: |
| 2031 |
eor_is_obsolete(); |
| 2032 |
|
| 2033 |
case OPID_XOR: |
| 2034 |
self->u.number.val.intval ^= other->u.number.val.intval; |
| 2035 |
refs = self->u.number.addr_refs + other->u.number.addr_refs; |
| 2036 |
break; |
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
default: |
| 2041 |
unsupported_operation(self, op, other); |
| 2042 |
return; |
| 2043 |
} |
| 2044 |
|
| 2045 |
self->u.number.addr_refs = refs; |
| 2046 |
intfloat_fix_result_after_dyadic(self, other); |
| 2047 |
} |
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
static void float_handle_dyadic_operator(struct object *self, const struct op *op, struct object *other) |
| 2052 |
{ |
| 2053 |
int refs = 0; |
| 2054 |
|
| 2055 |
|
| 2056 |
if (other->u.number.ntype == NUMTYPE_FLOAT) { |
| 2057 |
|
| 2058 |
} else if (other->u.number.ntype == NUMTYPE_INT) { |
| 2059 |
|
| 2060 |
switch (op->id) { |
| 2061 |
|
| 2062 |
case OPID_POWEROF: |
| 2063 |
case OPID_MULTIPLY: |
| 2064 |
case OPID_DIVIDE: |
| 2065 |
case OPID_INTDIV: |
| 2066 |
case OPID_ADD: |
| 2067 |
case OPID_SUBTRACT: |
| 2068 |
case OPID_LESSOREQUAL: |
| 2069 |
case OPID_LESSTHAN: |
| 2070 |
case OPID_GREATEROREQUAL: |
| 2071 |
case OPID_GREATERTHAN: |
| 2072 |
case OPID_NOTEQUAL: |
| 2073 |
case OPID_EQUALS: |
| 2074 |
|
| 2075 |
int_to_float(other); |
| 2076 |
break; |
| 2077 |
|
| 2078 |
case OPID_MODULO: |
| 2079 |
case OPID_LSR: |
| 2080 |
case OPID_AND: |
| 2081 |
case OPID_OR: |
| 2082 |
case OPID_EOR: |
| 2083 |
case OPID_XOR: |
| 2084 |
|
| 2085 |
case OPID_SHIFTLEFT: |
| 2086 |
case OPID_ASR: |
| 2087 |
break; |
| 2088 |
|
| 2089 |
|
| 2090 |
|
| 2091 |
default: |
| 2092 |
unsupported_operation(self, op, other); |
| 2093 |
return; |
| 2094 |
} |
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
} else { |
| 2099 |
unsupported_operation(self, op, other); |
| 2100 |
return; |
| 2101 |
} |
| 2102 |
|
| 2103 |
switch (op->id) { |
| 2104 |
case OPID_POWEROF: |
| 2105 |
self->u.number.val.fpval = pow(self->u.number.val.fpval, other->u.number.val.fpval); |
| 2106 |
break; |
| 2107 |
case OPID_MULTIPLY: |
| 2108 |
self->u.number.val.fpval *= other->u.number.val.fpval; |
| 2109 |
break; |
| 2110 |
case OPID_DIVIDE: |
| 2111 |
if (other->u.number.val.fpval) { |
| 2112 |
self->u.number.val.fpval /= other->u.number.val.fpval; |
| 2113 |
} else { |
| 2114 |
throw_error(exception_div_by_zero); |
| 2115 |
self->u.number.val.fpval = 0; |
| 2116 |
} |
| 2117 |
break; |
| 2118 |
case OPID_INTDIV: |
| 2119 |
if (other->u.number.val.fpval) { |
| 2120 |
self->u.number.val.intval = self->u.number.val.fpval / other->u.number.val.fpval; |
| 2121 |
} else { |
| 2122 |
throw_error(exception_div_by_zero); |
| 2123 |
self->u.number.val.intval = 0; |
| 2124 |
} |
| 2125 |
self->u.number.ntype = NUMTYPE_INT; |
| 2126 |
break; |
| 2127 |
case OPID_LSR: |
| 2128 |
case OPID_AND: |
| 2129 |
case OPID_OR: |
| 2130 |
case OPID_EOR: |
| 2131 |
case OPID_XOR: |
| 2132 |
throw_finalpass_warning(exception_float_to_int); |
| 2133 |
|
| 2134 |
case OPID_MODULO: |
| 2135 |
float_to_int(self); |
| 2136 |
|
| 2137 |
int_handle_dyadic_operator(self, op, other); |
| 2138 |
return; |
| 2139 |
|
| 2140 |
case OPID_ADD: |
| 2141 |
self->u.number.val.fpval += other->u.number.val.fpval; |
| 2142 |
refs = self->u.number.addr_refs + other->u.number.addr_refs; |
| 2143 |
break; |
| 2144 |
case OPID_SUBTRACT: |
| 2145 |
self->u.number.val.fpval -= other->u.number.val.fpval; |
| 2146 |
refs = self->u.number.addr_refs - other->u.number.addr_refs; |
| 2147 |
break; |
| 2148 |
case OPID_SHIFTLEFT: |
| 2149 |
if (other->u.number.ntype == NUMTYPE_FLOAT) |
| 2150 |
float_to_int(other); |
| 2151 |
self->u.number.val.fpval *= pow(2.0, other->u.number.val.intval); |
| 2152 |
break; |
| 2153 |
case OPID_ASR: |
| 2154 |
if (other->u.number.ntype == NUMTYPE_FLOAT) |
| 2155 |
float_to_int(other); |
| 2156 |
self->u.number.val.fpval /= (1 << other->u.number.val.intval); |
| 2157 |
break; |
| 2158 |
case OPID_LESSOREQUAL: |
| 2159 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.fpval <= other->u.number.val.fpval); |
| 2160 |
return; |
| 2161 |
|
| 2162 |
case OPID_LESSTHAN: |
| 2163 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.fpval < other->u.number.val.fpval); |
| 2164 |
return; |
| 2165 |
|
| 2166 |
case OPID_GREATEROREQUAL: |
| 2167 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.fpval >= other->u.number.val.fpval); |
| 2168 |
return; |
| 2169 |
|
| 2170 |
case OPID_GREATERTHAN: |
| 2171 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.fpval > other->u.number.val.fpval); |
| 2172 |
return; |
| 2173 |
|
| 2174 |
case OPID_NOTEQUAL: |
| 2175 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.fpval != other->u.number.val.fpval); |
| 2176 |
return; |
| 2177 |
|
| 2178 |
case OPID_EQUALS: |
| 2179 |
intfloat_fix_result_after_comparison(self, other, self->u.number.val.fpval == other->u.number.val.fpval); |
| 2180 |
return; |
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
default: |
| 2186 |
unsupported_operation(self, op, other); |
| 2187 |
return; |
| 2188 |
} |
| 2189 |
|
| 2190 |
self->u.number.addr_refs = refs; |
| 2191 |
intfloat_fix_result_after_dyadic(self, other); |
| 2192 |
} |
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
static void number_handle_dyadic_operator(struct object *self, const struct op *op, struct object *other) |
| 2197 |
{ |
| 2198 |
|
| 2199 |
if (other->type != &type_number) { |
| 2200 |
unsupported_operation(self, op, other); |
| 2201 |
return; |
| 2202 |
} |
| 2203 |
|
| 2204 |
if ((self->u.number.ntype == NUMTYPE_UNDEFINED) |
| 2205 |
|| (other->u.number.ntype == NUMTYPE_UNDEFINED)) |
| 2206 |
undef_handle_dyadic_operator(self, op, other); |
| 2207 |
else if (self->u.number.ntype == NUMTYPE_INT) |
| 2208 |
int_handle_dyadic_operator(self, op, other); |
| 2209 |
else if (self->u.number.ntype == NUMTYPE_FLOAT) |
| 2210 |
float_handle_dyadic_operator(self, op, other); |
| 2211 |
else |
| 2212 |
BUG("IllegalNumberType2", self->u.number.ntype); |
| 2213 |
} |
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
static int get_valid_index(int *target, int length, const struct object *self, const struct op *op, struct object *other) |
| 2219 |
{ |
| 2220 |
int index; |
| 2221 |
|
| 2222 |
if (other->type != &type_number) { |
| 2223 |
unsupported_operation(self, op, other); |
| 2224 |
return 1; |
| 2225 |
} |
| 2226 |
if (other->u.number.ntype == NUMTYPE_UNDEFINED) { |
| 2227 |
throw_error("Index is undefined."); |
| 2228 |
return 1; |
| 2229 |
} |
| 2230 |
if (other->u.number.ntype == NUMTYPE_FLOAT) |
| 2231 |
float_to_int(other); |
| 2232 |
if (other->u.number.ntype != NUMTYPE_INT) |
| 2233 |
BUG("IllegalNumberType3", other->u.number.ntype); |
| 2234 |
|
| 2235 |
index = other->u.number.val.intval; |
| 2236 |
|
| 2237 |
if (index < 0) |
| 2238 |
index += length; |
| 2239 |
if ((index < 0) || (index >= length)) { |
| 2240 |
throw_error("Index out of range."); |
| 2241 |
return 1; |
| 2242 |
} |
| 2243 |
*target = index; |
| 2244 |
return 0; |
| 2245 |
} |
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
static void list_handle_dyadic_operator(struct object *self, const struct op *op, struct object *other) |
| 2250 |
{ |
| 2251 |
struct listitem *item; |
| 2252 |
int length; |
| 2253 |
int index; |
| 2254 |
|
| 2255 |
length = self->u.listhead->u.listinfo.length; |
| 2256 |
switch (op->id) { |
| 2257 |
case OPID_LIST_APPEND: |
| 2258 |
list_append_object(self->u.listhead, other); |
| 2259 |
|
| 2260 |
return; |
| 2261 |
|
| 2262 |
case OPID_ATINDEX: |
| 2263 |
if (get_valid_index(&index, length, self, op, other)) |
| 2264 |
return; |
| 2265 |
|
| 2266 |
list_to_item(self, index); |
| 2267 |
return; |
| 2268 |
|
| 2269 |
case OPID_ADD: |
| 2270 |
if (other->type != &type_list) |
| 2271 |
break; |
| 2272 |
item = self->u.listhead; |
| 2273 |
list_init_list(self); |
| 2274 |
list_append_list(self->u.listhead, item); |
| 2275 |
item->u.listinfo.refs--; |
| 2276 |
item = other->u.listhead; |
| 2277 |
list_append_list(self->u.listhead, item); |
| 2278 |
item->u.listinfo.refs--; |
| 2279 |
return; |
| 2280 |
|
| 2281 |
case OPID_EQUALS: |
| 2282 |
if (other->type != &type_list) |
| 2283 |
break; |
| 2284 |
int_create_byte(self, !list_differs(self, other)); |
| 2285 |
|
| 2286 |
return; |
| 2287 |
|
| 2288 |
case OPID_NOTEQUAL: |
| 2289 |
if (other->type != &type_list) |
| 2290 |
break; |
| 2291 |
int_create_byte(self, list_differs(self, other)); |
| 2292 |
|
| 2293 |
return; |
| 2294 |
|
| 2295 |
|
| 2296 |
default: |
| 2297 |
break; |
| 2298 |
} |
| 2299 |
unsupported_operation(self, op, other); |
| 2300 |
} |
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
static void string_handle_dyadic_operator(struct object *self, const struct op *op, struct object *other) |
| 2305 |
{ |
| 2306 |
int length; |
| 2307 |
int index; |
| 2308 |
struct string *arthur, |
| 2309 |
*ford; |
| 2310 |
|
| 2311 |
length = self->u.string->length; |
| 2312 |
switch (op->id) { |
| 2313 |
case OPID_ATINDEX: |
| 2314 |
if (get_valid_index(&index, length, self, op, other)) |
| 2315 |
return; |
| 2316 |
|
| 2317 |
string_to_byte(self, index); |
| 2318 |
return; |
| 2319 |
|
| 2320 |
case OPID_ADD: |
| 2321 |
if (other->type != &type_string) |
| 2322 |
break; |
| 2323 |
arthur = self->u.string; |
| 2324 |
ford = other->u.string; |
| 2325 |
string_prepare_string(self, arthur->length + ford->length); |
| 2326 |
memcpy(self->u.string->payload, arthur->payload, arthur->length); |
| 2327 |
memcpy(self->u.string->payload + arthur->length, ford->payload, ford->length); |
| 2328 |
arthur->refs--; |
| 2329 |
ford->refs--; |
| 2330 |
return; |
| 2331 |
|
| 2332 |
case OPID_EQUALS: |
| 2333 |
if (other->type != &type_string) |
| 2334 |
break; |
| 2335 |
arthur = self->u.string; |
| 2336 |
ford = other->u.string; |
| 2337 |
int_create_byte(self, !string_differs(self, other)); |
| 2338 |
arthur->refs--; |
| 2339 |
ford->refs--; |
| 2340 |
return; |
| 2341 |
|
| 2342 |
case OPID_NOTEQUAL: |
| 2343 |
if (other->type != &type_string) |
| 2344 |
break; |
| 2345 |
arthur = self->u.string; |
| 2346 |
ford = other->u.string; |
| 2347 |
int_create_byte(self, string_differs(self, other)); |
| 2348 |
arthur->refs--; |
| 2349 |
ford->refs--; |
| 2350 |
return; |
| 2351 |
|
| 2352 |
|
| 2353 |
default: |
| 2354 |
break; |
| 2355 |
} |
| 2356 |
unsupported_operation(self, op, other); |
| 2357 |
} |
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
static void number_fix_result(struct object *self) |
| 2362 |
{ |
| 2363 |
|
| 2364 |
if (self->u.number.flags & NUMBER_FORCES_24) |
| 2365 |
self->u.number.flags &= ~(NUMBER_FORCES_16 | NUMBER_FORCES_8); |
| 2366 |
else if (self->u.number.flags & NUMBER_FORCES_16) |
| 2367 |
self->u.number.flags &= ~NUMBER_FORCES_8; |
| 2368 |
} |
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
static void object_no_op(struct object *self) |
| 2373 |
{ |
| 2374 |
} |
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
static void number_print(const struct object *self, struct dynabuf *db) |
| 2380 |
{ |
| 2381 |
char buffer[NUMBUFSIZE]; |
| 2382 |
|
| 2383 |
if (self->u.number.ntype == NUMTYPE_UNDEFINED) { |
| 2384 |
dynabuf_add_string(db, "<UNDEFINED NUMBER>"); |
| 2385 |
} else if (self->u.number.ntype == NUMTYPE_INT) { |
| 2386 |
#if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L |
| 2387 |
snprintf(buffer, NUMBUFSIZE, "%ld (0x%lx)", (long) self->u.number.val.intval, (long) self->u.number.val.intval); |
| 2388 |
#else |
| 2389 |
sprintf(buffer, "%ld (0x%lx)", (long) self->u.number.val.intval, (long) self->u.number.val.intval); |
| 2390 |
#endif |
| 2391 |
dynabuf_add_string(db, buffer); |
| 2392 |
} else if (self->u.number.ntype == NUMTYPE_FLOAT) { |
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
sprintf(buffer, "%.30g", self->u.number.val.fpval); |
| 2397 |
dynabuf_add_string(db, buffer); |
| 2398 |
} else { |
| 2399 |
BUG("IllegalNumberType5", self->u.number.ntype); |
| 2400 |
} |
| 2401 |
} |
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
static void list_print(const struct object *self, struct dynabuf *db) |
| 2406 |
{ |
| 2407 |
struct listitem *item; |
| 2408 |
int length; |
| 2409 |
struct object *obj; |
| 2410 |
const char *prefix = ""; |
| 2411 |
|
| 2412 |
dynabuf_append(db, '['); |
| 2413 |
length = self->u.listhead->u.listinfo.length; |
| 2414 |
item = self->u.listhead->next; |
| 2415 |
while (length--) { |
| 2416 |
obj = &item->u.payload; |
| 2417 |
dynabuf_add_string(db, prefix); |
| 2418 |
obj->type->print(obj, db); |
| 2419 |
item = item->next; |
| 2420 |
prefix = ", "; |
| 2421 |
} |
| 2422 |
dynabuf_append(db, ']'); |
| 2423 |
} |
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
static void string_print(const struct object *self, struct dynabuf *db) |
| 2428 |
{ |
| 2429 |
dynabuf_add_string(db, self->u.string->payload); |
| 2430 |
} |
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
static int has_no_length(const struct object *self) |
| 2435 |
{ |
| 2436 |
return -1; |
| 2437 |
} |
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
static int list_get_length(const struct object *self) |
| 2442 |
{ |
| 2443 |
return self->u.listhead->u.listinfo.length; |
| 2444 |
} |
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
static int string_get_length(const struct object *self) |
| 2449 |
{ |
| 2450 |
return self->u.string->length; |
| 2451 |
} |
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
static void cannot_be_indexed(const struct object *self, struct object *target, int index) |
| 2456 |
{ |
| 2457 |
BUG("TriedToIndexNumber", index); |
| 2458 |
} |
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
static void list_at(const struct object *self, struct object *target, int index) |
| 2463 |
{ |
| 2464 |
*target = *self; |
| 2465 |
list_to_item(target, index); |
| 2466 |
} |
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
static void string_at(const struct object *self, struct object *target, int index) |
| 2471 |
{ |
| 2472 |
*target = *self; |
| 2473 |
string_to_byte(target, index); |
| 2474 |
} |
| 2475 |
|
| 2476 |
|
| 2477 |
struct type type_number = { |
| 2478 |
"number", |
| 2479 |
number_is_defined, |
| 2480 |
number_differs, |
| 2481 |
number_assign, |
| 2482 |
number_handle_monadic_operator, |
| 2483 |
number_handle_dyadic_operator, |
| 2484 |
number_fix_result, |
| 2485 |
number_print, |
| 2486 |
has_no_length, |
| 2487 |
cannot_be_indexed |
| 2488 |
}; |
| 2489 |
struct type type_list = { |
| 2490 |
"list", |
| 2491 |
list_is_defined, |
| 2492 |
list_differs, |
| 2493 |
list_assign, |
| 2494 |
list_handle_monadic_operator, |
| 2495 |
list_handle_dyadic_operator, |
| 2496 |
object_no_op, |
| 2497 |
list_print, |
| 2498 |
list_get_length, |
| 2499 |
list_at |
| 2500 |
}; |
| 2501 |
struct type type_string = { |
| 2502 |
"string", |
| 2503 |
object_return_true, |
| 2504 |
string_differs, |
| 2505 |
string_assign, |
| 2506 |
string_handle_monadic_operator, |
| 2507 |
string_handle_dyadic_operator, |
| 2508 |
object_no_op, |
| 2509 |
string_print, |
| 2510 |
string_get_length, |
| 2511 |
string_at |
| 2512 |
}; |
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
static int parse_expression(struct expression *expression) |
| 2518 |
{ |
| 2519 |
struct object *result = &expression->result; |
| 2520 |
|
| 2521 |
|
| 2522 |
if (arg_stack == NULL) |
| 2523 |
enlarge_argument_stack(); |
| 2524 |
if (op_stack == NULL) |
| 2525 |
enlarge_operator_stack(); |
| 2526 |
|
| 2527 |
|
| 2528 |
expression->is_empty = TRUE; |
| 2529 |
expression->open_parentheses = 0; |
| 2530 |
expression->is_parenthesized = FALSE; |
| 2531 |
|
| 2532 |
|
| 2533 |
op_sp = 0; |
| 2534 |
arg_sp = 0; |
| 2535 |
|
| 2536 |
PUSH_OP(&ops_start_expression); |
| 2537 |
alu_state = STATE_EXPECT_ARG_OR_MONADIC_OP; |
| 2538 |
do { |
| 2539 |
|
| 2540 |
if (arg_sp >= argstack_size) |
| 2541 |
enlarge_argument_stack(); |
| 2542 |
|
| 2543 |
switch (alu_state) { |
| 2544 |
case STATE_EXPECT_ARG_OR_MONADIC_OP: |
| 2545 |
if (expect_argument_or_monadic_operator(expression)) |
| 2546 |
expression->is_empty = FALSE; |
| 2547 |
break; |
| 2548 |
case STATE_EXPECT_DYADIC_OP: |
| 2549 |
expect_dyadic_operator(expression); |
| 2550 |
break; |
| 2551 |
case STATE_MAX_GO_ON: |
| 2552 |
case STATE_ERROR: |
| 2553 |
case STATE_END: |
| 2554 |
break; |
| 2555 |
} |
| 2556 |
} while (alu_state < STATE_MAX_GO_ON); |
| 2557 |
|
| 2558 |
if (alu_state == STATE_END) { |
| 2559 |
|
| 2560 |
if (arg_sp != 1) |
| 2561 |
BUG("ArgStackNotEmpty", arg_sp); |
| 2562 |
if (op_sp != 1) |
| 2563 |
BUG("OperatorStackNotEmpty", op_sp); |
| 2564 |
|
| 2565 |
*result = arg_stack[0]; |
| 2566 |
|
| 2567 |
|
| 2568 |
if (expression->is_empty) { |
| 2569 |
result->type = &type_number; |
| 2570 |
result->u.number.ntype = NUMTYPE_UNDEFINED; |
| 2571 |
result->u.number.flags = NUMBER_EVER_UNDEFINED; |
| 2572 |
result->u.number.addr_refs = 0; |
| 2573 |
} else { |
| 2574 |
|
| 2575 |
if (!(result->type->is_defined(result))) { |
| 2576 |
|
| 2577 |
++pass.counters.undefineds; |
| 2578 |
} |
| 2579 |
} |
| 2580 |
|
| 2581 |
result->type->fix_result(result); |
| 2582 |
return 0; |
| 2583 |
} else { |
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
result->type = &type_number; |
| 2588 |
result->u.number.ntype = NUMTYPE_UNDEFINED; |
| 2589 |
result->u.number.flags = 0; |
| 2590 |
|
| 2591 |
result->u.number.addr_refs = 0; |
| 2592 |
|
| 2593 |
parser_skip_remainder(); |
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
return 1; |
| 2598 |
} |
| 2599 |
} |
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
void ALU_any_int(intval_t *target) |
| 2609 |
{ |
| 2610 |
struct expression expression; |
| 2611 |
|
| 2612 |
parse_expression(&expression); |
| 2613 |
if (expression.open_parentheses) |
| 2614 |
throw_error(exception_paren_open); |
| 2615 |
if (expression.is_empty) |
| 2616 |
throw_error(exception_no_value); |
| 2617 |
if (expression.result.type == &type_number) { |
| 2618 |
if (expression.result.u.number.ntype == NUMTYPE_UNDEFINED) |
| 2619 |
*target = 0; |
| 2620 |
else if (expression.result.u.number.ntype == NUMTYPE_INT) |
| 2621 |
*target = expression.result.u.number.val.intval; |
| 2622 |
else if (expression.result.u.number.ntype == NUMTYPE_FLOAT) |
| 2623 |
*target = expression.result.u.number.val.fpval; |
| 2624 |
else |
| 2625 |
BUG("IllegalNumberType6", expression.result.u.number.ntype); |
| 2626 |
} else if (expression.result.type == &type_string) { |
| 2627 |
|
| 2628 |
|
| 2629 |
if (expression.result.u.string->length != 1) { |
| 2630 |
throw_error(exception_lengthnot1); |
| 2631 |
} else { |
| 2632 |
|
| 2633 |
} |
| 2634 |
string_to_byte(&(expression.result), 0); |
| 2635 |
*target = expression.result.u.number.val.intval; |
| 2636 |
} else { |
| 2637 |
*target = 0; |
| 2638 |
throw_error(exception_not_number); |
| 2639 |
} |
| 2640 |
} |
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
void ALU_defined_int(struct number *intresult) |
| 2652 |
{ |
| 2653 |
struct expression expression; |
| 2654 |
boolean buf = pass.flags.complain_about_undefined; |
| 2655 |
|
| 2656 |
pass.flags.complain_about_undefined = TRUE; |
| 2657 |
parse_expression(&expression); |
| 2658 |
pass.flags.complain_about_undefined = buf; |
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
if (expression.open_parentheses) |
| 2668 |
throw_error(exception_paren_open); |
| 2669 |
if (expression.is_empty) |
| 2670 |
throw_serious_error(exception_no_value); |
| 2671 |
if (expression.result.type == &type_number) { |
| 2672 |
if (expression.result.u.number.ntype == NUMTYPE_UNDEFINED) { |
| 2673 |
throw_serious_error("Value not defined."); |
| 2674 |
expression.result.u.number.val.intval = 0; |
| 2675 |
} else if (expression.result.u.number.ntype == NUMTYPE_INT) { |
| 2676 |
|
| 2677 |
} else if (expression.result.u.number.ntype == NUMTYPE_FLOAT) { |
| 2678 |
float_to_int(&expression.result); |
| 2679 |
} else { |
| 2680 |
BUG("IllegalNumberType7", expression.result.u.number.ntype); |
| 2681 |
} |
| 2682 |
} else if (expression.result.type == &type_string) { |
| 2683 |
|
| 2684 |
|
| 2685 |
if (expression.result.u.string->length != 1) { |
| 2686 |
throw_error(exception_lengthnot1); |
| 2687 |
} else { |
| 2688 |
|
| 2689 |
} |
| 2690 |
string_to_byte(&(expression.result), 0); |
| 2691 |
} else { |
| 2692 |
throw_serious_error(exception_not_number); |
| 2693 |
} |
| 2694 |
*intresult = expression.result.u.number; |
| 2695 |
} |
| 2696 |
|
| 2697 |
|
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
void ALU_addrmode_int(struct expression *expression, int paren) |
| 2707 |
{ |
| 2708 |
parse_expression(expression); |
| 2709 |
if (expression->result.type == &type_number) { |
| 2710 |
|
| 2711 |
if (expression->result.u.number.ntype == NUMTYPE_FLOAT) |
| 2712 |
float_to_int(&(expression->result)); |
| 2713 |
else if (expression->result.u.number.ntype == NUMTYPE_UNDEFINED) |
| 2714 |
expression->result.u.number.val.intval = 0; |
| 2715 |
} else if (expression->result.type == &type_string) { |
| 2716 |
|
| 2717 |
|
| 2718 |
if (expression->result.u.string->length != 1) { |
| 2719 |
throw_error(exception_lengthnot1); |
| 2720 |
} else { |
| 2721 |
|
| 2722 |
} |
| 2723 |
string_to_byte(&(expression->result), 0); |
| 2724 |
} else { |
| 2725 |
throw_error(exception_not_number); |
| 2726 |
} |
| 2727 |
if (expression->open_parentheses > paren) { |
| 2728 |
expression->open_parentheses = 0; |
| 2729 |
throw_error(exception_paren_open); |
| 2730 |
} |
| 2731 |
if (expression->is_empty) |
| 2732 |
throw_error(exception_no_value); |
| 2733 |
} |
| 2734 |
|
| 2735 |
|
| 2736 |
|
| 2737 |
|
| 2738 |
|
| 2739 |
|
| 2740 |
|
| 2741 |
|
| 2742 |
void ALU_any_result(struct object *result) |
| 2743 |
{ |
| 2744 |
struct expression expression; |
| 2745 |
|
| 2746 |
parse_expression(&expression); |
| 2747 |
*result = expression.result; |
| 2748 |
if (expression.open_parentheses) |
| 2749 |
throw_error(exception_paren_open); |
| 2750 |
if (expression.is_empty) |
| 2751 |
throw_error(exception_no_value); |
| 2752 |
} |
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 2771 |
|
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 2789 |
|
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|