| 1 | // |
| 2 | // ACME - a crossassembler for producing 6502/65c02/65816 code. |
| 3 | // Copyright (C) 1998-2006 Marco Baye |
| 4 | // Have a look at "acme.c" for further info |
| 5 | // |
| 6 | // Arithmetic/logic unit |
| 7 | // 11 Oct 006 Improved float reading in parse_decimal_value() |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <math.h> // only for experimental fp support |
| 11 | #include "platform.h" // done first in case "inline" is redefined |
| 12 | #include "alu.h" |
| 13 | #include "cpu.h" |
| 14 | #include "dynabuf.h" |
| 15 | #include "encoding.h" |
| 16 | #include "global.h" |
| 17 | #include "input.h" |
| 18 | #include "label.h" |
| 19 | #include "section.h" |
| 20 | #include "tree.h" |
| 21 | |
| 22 | |
| 23 | // Constants |
| 24 | |
| 25 | #define FUNCION_DYNABUF_INITIALSIZE 8 // enough for "arctan" |
| 26 | #define HALF_INITIAL_STACK_SIZE 8 |
| 27 | static const char exception_div_by_zero[] = "Division by zero."; |
| 28 | static const char exception_no_value[] = "No value given."; |
| 29 | static const char exception_paren_open[] = "Too many '('."; |
| 30 | static const char exception_undefined[] = "Value not defined."; |
| 31 | #define s_or (s_eor+1) // Yes, I know I'm sick |
| 32 | #define s_xor (s_scrxor+3) // Yes, I know I'm sick |
| 33 | static const char s_arcsin[] = "arcsin"; |
| 34 | #define s_sin (s_arcsin+3) // Yes, I know I'm sick |
| 35 | static const char s_arccos[] = "arccos"; |
| 36 | #define s_cos (s_arccos+3) // Yes, I know I'm sick |
| 37 | static const char s_arctan[] = "arctan"; |
| 38 | #define s_tan (s_arctan+3) // Yes, I know I'm sick |
| 39 | |
| 40 | // Operator handles (FIXME - use function pointers instead? or too slow?) |
| 41 | enum op_handle_t { |
| 42 | // special values (pseudo operators) |
| 43 | OPHANDLE_END, // "reached end of expression" |
| 44 | OPHANDLE_RETURN, // "return value to caller" |
| 45 | // functions |
| 46 | OPHANDLE_INT, // int(v) |
| 47 | OPHANDLE_FLOAT, // float(v) |
| 48 | OPHANDLE_SIN, // sin(v) |
| 49 | OPHANDLE_COS, // cos(v) |
| 50 | OPHANDLE_TAN, // tan(v) |
| 51 | OPHANDLE_ARCSIN, // arcsin(v) |
| 52 | OPHANDLE_ARCCOS, // arccos(v) |
| 53 | OPHANDLE_ARCTAN, // arctan(v) |
| 54 | // monadic operators |
| 55 | OPHANDLE_OPENING, // (v '(', starts subexpression |
| 56 | OPHANDLE_NOT, // !v NOT v bit-wise NOT |
| 57 | OPHANDLE_NEGATE, // -v Negate |
| 58 | OPHANDLE_LOWBYTEOF, // <v Lowbyte of |
| 59 | OPHANDLE_HIGHBYTEOF, // >v Highbyte of |
| 60 | OPHANDLE_BANKBYTEOF, // ^v Bankbyte of |
| 61 | // dyadic operators |
| 62 | OPHANDLE_CLOSING, // v) ')', ends subexpression |
| 63 | OPHANDLE_POWEROF, // v^w |
| 64 | OPHANDLE_MULTIPLY, // v*w |
| 65 | OPHANDLE_DIVIDE, // v/w (Integer) Division |
| 66 | OPHANDLE_INTDIV, // v/w v DIV w Integer Division |
| 67 | OPHANDLE_MODULO, // v%w v MOD w Remainder |
| 68 | OPHANDLE_SL, // v<<w v ASL w v LSL w Shift left |
| 69 | OPHANDLE_ASR, // v>>w v ASR w Arithmetic shift right |
| 70 | OPHANDLE_LSR, // v>>>w v LSR w Logical shift right |
| 71 | OPHANDLE_ADD, // v+w |
| 72 | OPHANDLE_SUBTRACT, // v-w |
| 73 | OPHANDLE_EQUALS, // v=w |
| 74 | OPHANDLE_LE, // v<=w |
| 75 | OPHANDLE_LESSTHAN, // v< w |
| 76 | OPHANDLE_GE, // v>=w |
| 77 | OPHANDLE_GREATERTHAN, // v> w |
| 78 | OPHANDLE_NOTEQUAL, // v!=w v<>w v><w |
| 79 | OPHANDLE_AND, // v&w v AND w |
| 80 | OPHANDLE_OR, // v|w v OR w |
| 81 | OPHANDLE_EOR, // v EOR w v XOR w (remove) |
| 82 | OPHANDLE_XOR, // v XOR w |
| 83 | }; |
| 84 | struct operator_t { |
| 85 | enum op_handle_t handle; |
| 86 | int priority; |
| 87 | }; |
| 88 | typedef struct operator_t op_t; |
| 89 | |
| 90 | // operator structs (only hold handle and priority value) |
| 91 | static op_t ops_end = {OPHANDLE_END, 0}; // special |
| 92 | static op_t ops_return = {OPHANDLE_RETURN, 1}; // special |
| 93 | static op_t ops_closing = {OPHANDLE_CLOSING, 2}; // dyadic |
| 94 | static op_t ops_opening = {OPHANDLE_OPENING, 3}; // monadic |
| 95 | static op_t ops_or = {OPHANDLE_OR, 4}; // dyadic |
| 96 | static op_t ops_eor = {OPHANDLE_EOR, 5}; // (remove) |
| 97 | static op_t ops_xor = {OPHANDLE_XOR, 5}; // dyadic |
| 98 | static op_t ops_and = {OPHANDLE_AND, 6}; // dyadic |
| 99 | static op_t ops_equals = {OPHANDLE_EQUALS, 7}; // dyadic |
| 100 | static op_t ops_notequal = {OPHANDLE_NOTEQUAL, 8}; // dyadic |
| 101 | // same priority for all comparison operators |
| 102 | static op_t ops_le = {OPHANDLE_LE, 9}; // dyadic |
| 103 | static op_t ops_lessthan = {OPHANDLE_LESSTHAN, 9}; // dyadic |
| 104 | static op_t ops_ge = {OPHANDLE_GE, 9}; // dyadic |
| 105 | static op_t ops_greaterthan = {OPHANDLE_GREATERTHAN, 9}; // dyadic |
| 106 | // same priority for all byte extraction operators |
| 107 | static op_t ops_lowbyteof = {OPHANDLE_LOWBYTEOF, 10}; // monadic |
| 108 | static op_t ops_highbyteof = {OPHANDLE_HIGHBYTEOF, 10}; // monadic |
| 109 | static op_t ops_bankbyteof = {OPHANDLE_BANKBYTEOF, 10}; // monadic |
| 110 | // same priority for all shift operators |
| 111 | static op_t ops_sl = {OPHANDLE_SL, 11}; // dyadic |
| 112 | static op_t ops_asr = {OPHANDLE_ASR, 11}; // dyadic |
| 113 | static op_t ops_lsr = {OPHANDLE_LSR, 11}; // dyadic |
| 114 | // same priority for "+" and "-" |
| 115 | static op_t ops_add = {OPHANDLE_ADD, 12}; // dyadic |
| 116 | static op_t ops_subtract = {OPHANDLE_SUBTRACT, 12}; // dyadic |
| 117 | // same priority for "*", "/" and "%" |
| 118 | static op_t ops_multiply = {OPHANDLE_MULTIPLY, 13}; // dyadic |
| 119 | static op_t ops_divide = {OPHANDLE_DIVIDE, 13}; // dyadic |
| 120 | static op_t ops_intdiv = {OPHANDLE_INTDIV, 13}; // dyadic |
| 121 | static op_t ops_modulo = {OPHANDLE_MODULO, 13}; // dyadic |
| 122 | // highest "real" priorities |
| 123 | static op_t ops_negate = {OPHANDLE_NEGATE, 14}; // monadic |
| 124 | static op_t ops_powerof = {OPHANDLE_POWEROF, 15}; // dyadic |
| 125 | static op_t ops_not = {OPHANDLE_NOT, 16}; // monadic |
| 126 | // function calls act as if they were monadic operators |
| 127 | static op_t ops_int = {OPHANDLE_INT, 17}; // function |
| 128 | static op_t ops_float = {OPHANDLE_FLOAT, 17}; // function |
| 129 | static op_t ops_sin = {OPHANDLE_SIN, 17}; // function |
| 130 | static op_t ops_cos = {OPHANDLE_COS, 17}; // function |
| 131 | static op_t ops_tan = {OPHANDLE_TAN, 17}; // function |
| 132 | static op_t ops_arcsin = {OPHANDLE_ARCSIN, 17}; // function |
| 133 | static op_t ops_arccos = {OPHANDLE_ARCCOS, 17}; // function |
| 134 | static op_t ops_arctan = {OPHANDLE_ARCTAN, 17}; // function |
| 135 | |
| 136 | |
| 137 | // Variables |
| 138 | |
| 139 | static dynabuf_t* function_dyna_buf; // dynamic buffer for fn names |
| 140 | static op_t** operator_stack = NULL; |
| 141 | static int operator_stk_size = HALF_INITIAL_STACK_SIZE; |
| 142 | static int operator_sp; // operator stack pointer |
| 143 | static struct result_t* operand_stack = NULL; // flags and value |
| 144 | static int operand_stk_size = HALF_INITIAL_STACK_SIZE; |
| 145 | static int operand_sp; // value stack pointer |
| 146 | static int indirect_flag; // Flag for indirect addressing |
| 147 | // (indicated by useless parentheses) |
| 148 | // Contains either 0 or MVALUE_INDIRECT |
| 149 | enum alu_state_t { |
| 150 | STATE_EXPECT_OPERAND_OR_MONADIC_OPERATOR, |
| 151 | STATE_EXPECT_DYADIC_OPERATOR, |
| 152 | STATE_TRY_TO_REDUCE_STACKS, |
| 153 | STATE_MAX_GO_ON, // "border value" to find the stoppers: |
| 154 | STATE_ERROR, // error has occured |
| 155 | STATE_END, // standard end |
| 156 | }; |
| 157 | static enum alu_state_t alu_state; // deterministic finite automaton |
| 158 | // predefined stuff |
| 159 | static node_t* operator_tree = NULL;// tree to hold operators |
| 160 | static node_t operator_list[] = { |
| 161 | PREDEFNODE(s_asr, &ops_asr), |
| 162 | PREDEFNODE(s_lsr, &ops_lsr), |
| 163 | PREDEFNODE(s_asl, &ops_sl), |
| 164 | PREDEFNODE("lsl", &ops_sl), |
| 165 | PREDEFNODE("div", &ops_intdiv), |
| 166 | PREDEFNODE("mod", &ops_modulo), |
| 167 | PREDEFNODE(s_and, &ops_and), |
| 168 | PREDEFNODE(s_or, &ops_or), |
| 169 | PREDEFNODE(s_eor, &ops_eor), // remove sometime |
| 170 | PREDEFLAST(s_xor, &ops_xor), |
| 171 | // ^^^^ this marks the last element |
| 172 | }; |
| 173 | static node_t* function_tree = NULL;// tree to hold functions |
| 174 | static node_t function_list[] = { |
| 175 | PREDEFNODE("int", &ops_int), |
| 176 | PREDEFNODE("float", &ops_float), |
| 177 | PREDEFNODE(s_arcsin, &ops_arcsin), |
| 178 | PREDEFNODE(s_arccos, &ops_arccos), |
| 179 | PREDEFNODE(s_arctan, &ops_arctan), |
| 180 | PREDEFNODE(s_sin, &ops_sin), |
| 181 | PREDEFNODE(s_cos, &ops_cos), |
| 182 | PREDEFLAST(s_tan, &ops_tan), |
| 183 | // ^^^^ this marks the last element |
| 184 | }; |
| 185 | |
| 186 | #define LEFT_FLAGS (operand_stack[operand_sp-2].flags) |
| 187 | #define RIGHT_FLAGS (operand_stack[operand_sp-1].flags) |
| 188 | #define LEFT_INTVAL (operand_stack[operand_sp-2].val.intval) |
| 189 | #define RIGHT_INTVAL (operand_stack[operand_sp-1].val.intval) |
| 190 | #define LEFT_FPVAL (operand_stack[operand_sp-2].val.fpval) |
| 191 | #define RIGHT_FPVAL (operand_stack[operand_sp-1].val.fpval) |
| 192 | |
| 193 | #define PUSH_OPERATOR(x) operator_stack[operator_sp++] = (x) |
| 194 | |
| 195 | #define PUSH_INTOPERAND(i, f) do {\ |
| 196 | operand_stack[operand_sp].flags = (f);\ |
| 197 | operand_stack[operand_sp++].val.intval = (i);\ |
| 198 | } while(0) |
| 199 | #define PUSH_FPOPERAND(fp, f) do {\ |
| 200 | operand_stack[operand_sp].flags = (f) | MVALUE_IS_FP;\ |
| 201 | operand_stack[operand_sp++].val.fpval = (fp);\ |
| 202 | } while(0) |
| 203 | |
| 204 | |
| 205 | // Functions |
| 206 | |
| 207 | // Handle "NeedValue" type errors: problems that may be solved by performing |
| 208 | // further passes. This function only counts, it will not show the errors to |
| 209 | // the user. |
| 210 | static void just_count(void) { |
| 211 | pass_undefined_count++; |
| 212 | } |
| 213 | |
| 214 | // Handle "NeedValue" type errors: problems that may be solved by performing |
| 215 | // further passes. This function counts these errors and shows them to the user. |
| 216 | static void count_and_throw(void) { |
| 217 | pass_undefined_count++; |
| 218 | Throw_error(exception_undefined); |
| 219 | } |
| 220 | |
| 221 | // Function pointer for "result is undefined" type errors. |
| 222 | static void (*result_is_undefined)(void) = just_count; |
| 223 | |
| 224 | // Activate error output for "value undefined" |
| 225 | void ALU_throw_errors(void) { |
| 226 | result_is_undefined = count_and_throw; |
| 227 | } |
| 228 | |
| 229 | // Enlarge operator stack |
| 230 | static void enlarge_operator_stack(void) { |
| 231 | operator_stk_size *= 2; |
| 232 | operator_stack = |
| 233 | realloc(operator_stack, operator_stk_size * sizeof(op_t*)); |
| 234 | if(operator_stack == NULL) |
| 235 | Throw_serious_error(exception_no_memory_left); |
| 236 | } |
| 237 | |
| 238 | // Enlarge operand stack |
| 239 | static void enlarge_operand_stack(void) { |
| 240 | operand_stk_size *= 2; |
| 241 | operand_stack = |
| 242 | realloc(operand_stack, operand_stk_size * sizeof(struct result_t)); |
| 243 | if(operand_stack == NULL) |
| 244 | Throw_serious_error(exception_no_memory_left); |
| 245 | } |
| 246 | |
| 247 | // create dynamic buffer, operator/function trees and operator/operand stacks |
| 248 | void ALU_init(void) { |
| 249 | function_dyna_buf = DynaBuf_create(FUNCION_DYNABUF_INITIALSIZE); |
| 250 | Tree_add_table(&operator_tree, operator_list); |
| 251 | Tree_add_table(&function_tree, function_list); |
| 252 | enlarge_operator_stack(); |
| 253 | enlarge_operand_stack(); |
| 254 | } |
| 255 | |
| 256 | // not-so-braindead algorithm for calculating "to the power of" function for |
| 257 | // integer operands. |
| 258 | // my_pow(whatever, 0) returns 1. my_pow(0, whatever_but_zero) returns 0. |
| 259 | static inline intval_t my_pow(intval_t mantissa, intval_t exponent) { |
| 260 | intval_t result = 1; |
| 261 | |
| 262 | while(exponent) { |
| 263 | // handle exponent's lowmost bit |
| 264 | if(exponent & 1) |
| 265 | result *= mantissa; |
| 266 | // square the mantissa, halve the exponent |
| 267 | mantissa *= mantissa; |
| 268 | exponent >>= 1; |
| 269 | } |
| 270 | return(result); |
| 271 | } |
| 272 | |
| 273 | // arithmetic shift right (works even if C compiler does not support it) |
| 274 | static inline intval_t my_asr(intval_t left, intval_t right) { |
| 275 | intval_t result = left >> right; // perform shift right |
| 276 | |
| 277 | // if left operand >= 0, then ASR and LSR are equivalent, so ok |
| 278 | // if result < 0, then ASR has been performed, so ok |
| 279 | if((left >= 0) || (result < 0)) |
| 280 | return(result); |
| 281 | // otherwise, correct result and return that |
| 282 | return(result | (-1L << (8 * sizeof(intval_t) - right))); |
| 283 | } |
| 284 | |
| 285 | // Lookup (and create, if necessary) label tree item and return its value. |
| 286 | // DynaBuf holds the label's name and "zone" its zone. |
| 287 | // This function is not allowed to change DynaBuf because that's where the |
| 288 | // label name is stored! |
| 289 | static void get_label_value(zone_t zone) { |
| 290 | label_t* label; |
| 291 | |
| 292 | // If the label gets created now, mark it as unsure |
| 293 | label = Label_find(zone, MVALUE_UNSURE); |
| 294 | // in first pass, count usage |
| 295 | if(pass_count == 0) |
| 296 | label->usage++; |
| 297 | // push operand, regardless of whether int or float |
| 298 | operand_stack[operand_sp] = label->result; |
| 299 | operand_stack[operand_sp++].flags |= MVALUE_EXISTS; |
| 300 | } |
| 301 | |
| 302 | // Parse quoted character. |
| 303 | // The character will be converted using the current encoding. |
| 304 | static inline void parse_quoted_character(char closing_quote) { |
| 305 | intval_t value; |
| 306 | |
| 307 | // read character to parse - make sure not at end of statement |
| 308 | if(GetQuotedByte() == CHAR_EOS) |
| 309 | return; |
| 310 | // on empty string, complain |
| 311 | if(GotByte == closing_quote) { |
| 312 | Throw_error(exception_missing_string); |
| 313 | Input_skip_remainder(); |
| 314 | return; |
| 315 | } |
| 316 | // parse character |
| 317 | value = (intval_t) Encoding_encode_char(GotByte); |
| 318 | // Read closing quote (hopefully) |
| 319 | if(GetQuotedByte() == closing_quote) |
| 320 | GetByte();// If length == 1, proceed with next byte |
| 321 | else |
| 322 | if(GotByte) { |
| 323 | // If longer than one character |
| 324 | Throw_error("There's more than one character."); |
| 325 | Input_skip_remainder(); |
| 326 | } |
| 327 | PUSH_INTOPERAND(value, MVALUE_GIVEN | MVALUE_ISBYTE); |
| 328 | // Now GotByte = char following closing quote (or CHAR_EOS on error) |
| 329 | } |
| 330 | |
| 331 | // Parse hexadecimal value. It accepts "0" to "9", "a" to "f" and "A" to "F". |
| 332 | // Capital letters will be converted to lowercase letters using the flagtable. |
| 333 | // The current value is stored as soon as a character is read that is none of |
| 334 | // those given above. |
| 335 | static void parse_hexadecimal_value(void) {// Now GotByte = "$" or "x" |
| 336 | char byte; |
| 337 | bool go_on; // continue loop flag |
| 338 | int digits = -1, // digit counter |
| 339 | flags = MVALUE_GIVEN; |
| 340 | intval_t value = 0; |
| 341 | |
| 342 | do { |
| 343 | digits++; |
| 344 | go_on = FALSE; |
| 345 | byte = GetByte(); |
| 346 | // first, convert "A-F" to "a-f" |
| 347 | byte |= (BYTEFLAGS(byte) & BYTEIS_UPCASE); |
| 348 | // if digit, add digit value |
| 349 | if((byte >= '0') && (byte <= '9')) { |
| 350 | value = (value << 4) + (byte - '0'); |
| 351 | go_on = TRUE;// keep going |
| 352 | } |
| 353 | // if legal ("a-f") character, add character value |
| 354 | if((byte >= 'a') && (byte <= 'f')) { |
| 355 | value = (value << 4) + (byte - 'a') + 10; |
| 356 | go_on = TRUE;// keep going |
| 357 | } |
| 358 | } while(go_on); |
| 359 | // set force bits |
| 360 | if(digits > 2) { |
| 361 | if(digits > 4) { |
| 362 | if(value < 65536) |
| 363 | flags |= MVALUE_FORCE24; |
| 364 | } else { |
| 365 | if(value < 256) |
| 366 | flags |= MVALUE_FORCE16; |
| 367 | } |
| 368 | } |
| 369 | PUSH_INTOPERAND(value, flags); |
| 370 | // Now GotByte = non-hexadecimal char |
| 371 | } |
| 372 | |
| 373 | // Parse a decimal value. As decimal values don't use any prefixes, this |
| 374 | // function expects the first digit to be read already. If the first two digits |
| 375 | // are "0x", this function branches to the one for parsing hexadecimal values. |
| 376 | // This function accepts '0' through '9' and one dot ('.') as the decimal |
| 377 | // point. The current value is stored as soon as a character is read that is |
| 378 | // none of those given above. Float usage is only activated when a decimal |
| 379 | // point has been found, so don't expect "100000000000000000000" to work. |
| 380 | static inline void parse_decimal_value(void) {// Now GotByte = first digit |
| 381 | double fpval, |
| 382 | denominator; |
| 383 | intval_t intval = (GotByte & 15);// this works. it's ASCII. |
| 384 | |
| 385 | GetByte(); |
| 386 | if((intval == 0) && (GotByte == 'x')) |
| 387 | return(parse_hexadecimal_value()); |
| 388 | // parse digits until no more |
| 389 | while((GotByte >= '0') && (GotByte <= '9')) { |
| 390 | intval *= 10; |
| 391 | intval += (GotByte & 15);// this works. it's ASCII. |
| 392 | GetByte(); |
| 393 | } |
| 394 | // check whether it's a float |
| 395 | if(GotByte == '.') { |
| 396 | // read fractional part and store as fp value |
| 397 | GetByte(); |
| 398 | fpval = intval; |
| 399 | denominator = 1; |
| 400 | // parse digits until no more |
| 401 | while((GotByte >= '0') && (GotByte <= '9')) { |
| 402 | fpval *= 10; |
| 403 | fpval += (GotByte & 15);// this works. it' ASCII. |
| 404 | denominator *= 10; |
| 405 | GetByte(); |
| 406 | } |
| 407 | fpval /= denominator; |
| 408 | PUSH_FPOPERAND(fpval, MVALUE_GIVEN); |
| 409 | } else { |
| 410 | // integer |
| 411 | PUSH_INTOPERAND(intval, MVALUE_GIVEN); |
| 412 | } |
| 413 | // Now GotByte = non-decimal char |
| 414 | } |
| 415 | |
| 416 | // Parse octal value. It accepts "0" to "7". The current value is stored as |
| 417 | // soon as a character is read that is none of those given above. |
| 418 | static inline void parse_octal_value(void) {// Now GotByte = "&" |
| 419 | intval_t value = 0; |
| 420 | int flags = MVALUE_GIVEN, |
| 421 | digits = 0; // digit counter |
| 422 | |
| 423 | GetByte(); |
| 424 | while((GotByte >= '0') && (GotByte <= '7')) { |
| 425 | value = (value << 3) + (GotByte & 7);// this works. it's ASCII. |
| 426 | digits++; |
| 427 | GetByte(); |
| 428 | } |
| 429 | // set force bits |
| 430 | if(digits > 3) { |
| 431 | if(digits > 6) { |
| 432 | if(value < 65536) |
| 433 | flags |= MVALUE_FORCE24; |
| 434 | } else { |
| 435 | if(value < 256) |
| 436 | flags |= MVALUE_FORCE16; |
| 437 | } |
| 438 | } |
| 439 | PUSH_INTOPERAND(value, flags); |
| 440 | // Now GotByte = non-octal char |
| 441 | } |
| 442 | |
| 443 | // Parse binary value. Apart from '0' and '1', it also accepts the characters |
| 444 | // '.' and '#', this is much more readable. The current value is stored as soon |
| 445 | // as a character is read that is none of those given above. |
| 446 | static inline void parse_binary_value(void) {// Now GotByte = "%" |
| 447 | intval_t value = 0; |
| 448 | bool go_on = TRUE; // continue loop flag |
| 449 | int flags = MVALUE_GIVEN, |
| 450 | digits = -1; // digit counter |
| 451 | |
| 452 | do { |
| 453 | digits++; |
| 454 | switch(GetByte()) { |
| 455 | |
| 456 | case '0': |
| 457 | case '.': |
| 458 | value <<= 1; |
| 459 | break; |
| 460 | |
| 461 | case '1': |
| 462 | case '#': |
| 463 | value = (value << 1) | 1; |
| 464 | break; |
| 465 | |
| 466 | default: |
| 467 | go_on = FALSE; |
| 468 | } |
| 469 | } while(go_on); |
| 470 | // set force bits |
| 471 | if(digits > 8) { |
| 472 | if(digits > 16) { |
| 473 | if(value < 65536) |
| 474 | flags |= MVALUE_FORCE24; |
| 475 | } else { |
| 476 | if(value < 256) |
| 477 | flags |= MVALUE_FORCE16; |
| 478 | } |
| 479 | } |
| 480 | PUSH_INTOPERAND(value, flags); |
| 481 | // Now GotByte = non-binary char |
| 482 | } |
| 483 | |
| 484 | // Parse function call (sin(), cos(), arctan(), ...) |
| 485 | static inline void parse_function_call(void) { |
| 486 | void* node_body; |
| 487 | |
| 488 | // make lower case version of name in local dynamic buffer |
| 489 | DynaBuf_to_lower(function_dyna_buf, GlobalDynaBuf); |
| 490 | // search for tree item |
| 491 | if(Tree_easy_scan(function_tree, &node_body, function_dyna_buf)) |
| 492 | PUSH_OPERATOR((op_t*) node_body); |
| 493 | else |
| 494 | Throw_error("Unknown function."); |
| 495 | } |
| 496 | |
| 497 | // Expect operand or monadic operator (hopefully inlined) |
| 498 | static inline void expect_operand_or_monadic_operator(void) { |
| 499 | op_t* operator; |
| 500 | bool perform_negation; |
| 501 | |
| 502 | SKIPSPACE(); |
| 503 | switch(GotByte) { |
| 504 | |
| 505 | case '+':// anonymous forward label |
| 506 | // count plus signs to build name of anonymous label |
| 507 | DYNABUF_CLEAR(GlobalDynaBuf); |
| 508 | do |
| 509 | DYNABUF_APPEND(GlobalDynaBuf, '+'); |
| 510 | while(GetByte() == '+'); |
| 511 | Label_fix_forward_name(); |
| 512 | get_label_value(Section_now->zone); |
| 513 | goto now_expect_dyadic; |
| 514 | |
| 515 | case '-':// NEGATION operator or anonymous backward label |
| 516 | // count minus signs in case it's an anonymous backward label |
| 517 | perform_negation = FALSE; |
| 518 | DYNABUF_CLEAR(GlobalDynaBuf); |
| 519 | do { |
| 520 | DYNABUF_APPEND(GlobalDynaBuf, '-'); |
| 521 | perform_negation = !perform_negation; |
| 522 | } while(GetByte() == '-'); |
| 523 | SKIPSPACE(); |
| 524 | if(BYTEFLAGS(GotByte) & FOLLOWS_ANON) { |
| 525 | DynaBuf_append(GlobalDynaBuf, '\0'); |
| 526 | get_label_value(Section_now->zone); |
| 527 | goto now_expect_dyadic; |
| 528 | } // goto means we don't need an "else {" here |
| 529 | if(perform_negation) |
| 530 | PUSH_OPERATOR(&ops_negate); |
| 531 | // State doesn't change |
| 532 | break; |
| 533 | |
| 534 | // Real monadic operators (state doesn't change, still ExpectMonadic) |
| 535 | |
| 536 | case '!':// NOT operator |
| 537 | operator = &ops_not; |
| 538 | goto get_byte_and_push_monadic; |
| 539 | |
| 540 | case '<':// LOWBYTE operator |
| 541 | operator = &ops_lowbyteof; |
| 542 | goto get_byte_and_push_monadic; |
| 543 | |
| 544 | case '>':// HIGHBYTE operator |
| 545 | operator = &ops_highbyteof; |
| 546 | goto get_byte_and_push_monadic; |
| 547 | |
| 548 | case '^':// BANKBYTE operator |
| 549 | operator = &ops_bankbyteof; |
| 550 | goto get_byte_and_push_monadic; |
| 551 | |
| 552 | // Faked monadic operators |
| 553 | |
| 554 | case '(':// left parenthesis |
| 555 | operator = &ops_opening; |
| 556 | goto get_byte_and_push_monadic; |
| 557 | |
| 558 | case ')':// right parenthesis |
| 559 | // this makes "()" also throw a syntax error |
| 560 | Throw_error(exception_syntax); |
| 561 | alu_state = STATE_ERROR; |
| 562 | break; |
| 563 | |
| 564 | // Operands (values, state changes to ExpectDyadic) |
| 565 | |
| 566 | // Quoted character |
| 567 | case '"': |
| 568 | case '\'': |
| 569 | // Character will be converted using current encoding |
| 570 | parse_quoted_character(GotByte); |
| 571 | // Now GotByte = char following closing quote |
| 572 | goto now_expect_dyadic; |
| 573 | |
| 574 | // Binary value |
| 575 | case '%': |
| 576 | parse_binary_value();// Now GotByte = non-binary char |
| 577 | goto now_expect_dyadic; |
| 578 | |
| 579 | // Octal value |
| 580 | case '&': |
| 581 | parse_octal_value();// Now GotByte = non-octal char |
| 582 | goto now_expect_dyadic; |
| 583 | |
| 584 | // Hexadecimal value |
| 585 | case '$': |
| 586 | parse_hexadecimal_value(); |
| 587 | // Now GotByte = non-hexadecimal char |
| 588 | goto now_expect_dyadic; |
| 589 | |
| 590 | // Program counter |
| 591 | case '*': |
| 592 | GetByte();// proceed with next char |
| 593 | PUSH_INTOPERAND(CPU_pc.intval, CPU_pc.flags | MVALUE_EXISTS); |
| 594 | // Now GotByte = char after closing quote |
| 595 | goto now_expect_dyadic; |
| 596 | |
| 597 | // Local label |
| 598 | case '.': |
| 599 | GetByte();// start after '.' |
| 600 | if(Input_read_keyword()) { |
| 601 | // Now GotByte = illegal char |
| 602 | get_label_value(Section_now->zone); |
| 603 | goto now_expect_dyadic; |
| 604 | } // goto means we don't need an "else {" here |
| 605 | alu_state = STATE_ERROR; |
| 606 | break; |
| 607 | |
| 608 | // Decimal values and global labels |
| 609 | default:// (all other characters) |
| 610 | if((GotByte >= '0') && (GotByte <= '9')) { |
| 611 | parse_decimal_value(); |
| 612 | // Now GotByte = non-decimal char |
| 613 | goto now_expect_dyadic; |
| 614 | } // goto means we don't need an "else {" here |
| 615 | if(BYTEFLAGS(GotByte) & STARTS_KEYWORD) { |
| 616 | register int length; |
| 617 | // Read global label (or "NOT") |
| 618 | length = Input_read_keyword(); |
| 619 | // Now GotByte = illegal char |
| 620 | // Check for NOT. Okay, it's hardcoded, |
| 621 | // but so what? Sue me... |
| 622 | if((length == 3) |
| 623 | && ((GlobalDynaBuf->buffer[0] | 32) == 'n') |
| 624 | && ((GlobalDynaBuf->buffer[1] | 32) == 'o') |
| 625 | && ((GlobalDynaBuf->buffer[2] | 32) == 't')) { |
| 626 | PUSH_OPERATOR(&ops_not); |
| 627 | // state doesn't change |
| 628 | } else { |
| 629 | if(GotByte == '(') |
| 630 | parse_function_call(); |
| 631 | else { |
| 632 | get_label_value(ZONE_GLOBAL); |
| 633 | goto now_expect_dyadic; |
| 634 | } |
| 635 | } |
| 636 | } else { |
| 637 | // illegal character read - so don't go on |
| 638 | PUSH_INTOPERAND(0, 0); |
| 639 | // push pseudo value, EXISTS flag is clear |
| 640 | if(operator_stack[operator_sp-1] == &ops_return) { |
| 641 | PUSH_OPERATOR(&ops_end); |
| 642 | alu_state = STATE_TRY_TO_REDUCE_STACKS; |
| 643 | } else { |
| 644 | Throw_error(exception_syntax); |
| 645 | alu_state = STATE_ERROR; |
| 646 | } |
| 647 | } |
| 648 | break; |
| 649 | |
| 650 | // no other possibilities, so here are the shared endings |
| 651 | |
| 652 | get_byte_and_push_monadic: |
| 653 | GetByte(); |
| 654 | PUSH_OPERATOR(operator); |
| 655 | // State doesn't change |
| 656 | break; |
| 657 | |
| 658 | now_expect_dyadic: |
| 659 | alu_state = STATE_EXPECT_DYADIC_OPERATOR; |
| 660 | break; |
| 661 | |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // Expect dyadic operator (hopefully inlined) |
| 666 | static inline void expect_dyadic_operator(void) { |
| 667 | void* node_body; |
| 668 | op_t* operator; |
| 669 | |
| 670 | SKIPSPACE(); |
| 671 | switch(GotByte) { |
| 672 | |
| 673 | // Single-character dyadic operators |
| 674 | |
| 675 | case '^':// "to the power of" |
| 676 | operator = &ops_powerof; |
| 677 | goto get_byte_and_push_dyadic; |
| 678 | |
| 679 | case '+':// add |
| 680 | operator = &ops_add; |
| 681 | goto get_byte_and_push_dyadic; |
| 682 | |
| 683 | case '-':// subtract |
| 684 | operator = &ops_subtract; |
| 685 | goto get_byte_and_push_dyadic; |
| 686 | |
| 687 | case '*':// multiply |
| 688 | operator = &ops_multiply; |
| 689 | goto get_byte_and_push_dyadic; |
| 690 | |
| 691 | case '/':// divide |
| 692 | operator = &ops_divide; |
| 693 | goto get_byte_and_push_dyadic; |
| 694 | |
| 695 | case '%':// modulo |
| 696 | operator = &ops_modulo; |
| 697 | goto get_byte_and_push_dyadic; |
| 698 | |
| 699 | case '&':// bitwise AND |
| 700 | operator = &ops_and; |
| 701 | goto get_byte_and_push_dyadic; |
| 702 | |
| 703 | case '|':// bitwise OR |
| 704 | operator = &ops_or; |
| 705 | goto get_byte_and_push_dyadic; |
| 706 | |
| 707 | // This part is commented out because there is no XOR character defined |
| 708 | // case ???:// bitwise exclusive OR |
| 709 | // operator = &ops_xor; |
| 710 | // goto get_byte_and_push_dyadic; |
| 711 | |
| 712 | case '=':// is equal |
| 713 | operator = &ops_equals; |
| 714 | goto get_byte_and_push_dyadic; |
| 715 | |
| 716 | case ')':// closing parenthesis |
| 717 | operator = &ops_closing; |
| 718 | goto get_byte_and_push_dyadic; |
| 719 | |
| 720 | // Multi-character dyadic operators |
| 721 | |
| 722 | // "!=" |
| 723 | case '!': |
| 724 | if(GetByte() == '=') { |
| 725 | operator = &ops_notequal; |
| 726 | goto get_byte_and_push_dyadic; |
| 727 | } // goto means we don't need an "else {" here |
| 728 | Throw_error(exception_syntax); |
| 729 | alu_state = STATE_ERROR; |
| 730 | break; |
| 731 | |
| 732 | // "<", "<=", "<<" and "<>" |
| 733 | case '<': |
| 734 | switch(GetByte()) { |
| 735 | |
| 736 | case '=':// "<=", less or equal |
| 737 | operator = &ops_le; |
| 738 | goto get_byte_and_push_dyadic; |
| 739 | |
| 740 | case '<':// "<<", shift left |
| 741 | operator = &ops_sl; |
| 742 | goto get_byte_and_push_dyadic; |
| 743 | |
| 744 | case '>':// "<>", not equal |
| 745 | operator = &ops_notequal; |
| 746 | goto get_byte_and_push_dyadic; |
| 747 | |
| 748 | default:// "<", less than |
| 749 | operator = &ops_lessthan; |
| 750 | goto push_dyadic; |
| 751 | } |
| 752 | break; |
| 753 | |
| 754 | // ">", ">=", ">>", ">>>" and "><" |
| 755 | case '>': |
| 756 | switch(GetByte()) { |
| 757 | |
| 758 | case '=':// ">=", greater or equal |
| 759 | operator = &ops_ge; |
| 760 | goto get_byte_and_push_dyadic; |
| 761 | |
| 762 | case '<':// "><", not equal |
| 763 | operator = &ops_notequal; |
| 764 | goto get_byte_and_push_dyadic; |
| 765 | |
| 766 | case '>':// ">>" or ">>>", shift right |
| 767 | operator = &ops_asr;// arithmetic shift right |
| 768 | if(GetByte() != '>') |
| 769 | goto push_dyadic; |
| 770 | operator = &ops_lsr;// logical shift right |
| 771 | goto get_byte_and_push_dyadic; |
| 772 | |
| 773 | default:// ">", greater than |
| 774 | operator = &ops_greaterthan; |
| 775 | goto push_dyadic; |
| 776 | } |
| 777 | break; |
| 778 | |
| 779 | // end of expression or text version of dyadic operator |
| 780 | default: |
| 781 | // check string version of operators |
| 782 | if(BYTEFLAGS(GotByte) & STARTS_KEYWORD) { |
| 783 | Input_read_and_lower_keyword(); |
| 784 | // Now GotByte = illegal char |
| 785 | // search for tree item |
| 786 | if(Tree_easy_scan(operator_tree, &node_body, GlobalDynaBuf)) { |
| 787 | operator = node_body; |
| 788 | goto push_dyadic; |
| 789 | } |
| 790 | // goto means we don't need an "else {" here |
| 791 | Throw_error("Unknown operator."); |
| 792 | alu_state = STATE_ERROR; |
| 793 | } else { |
| 794 | operator = &ops_end; |
| 795 | goto push_dyadic; |
| 796 | } |
| 797 | break; |
| 798 | |
| 799 | // no other possibilities, so here are the shared endings |
| 800 | |
| 801 | get_byte_and_push_dyadic: |
| 802 | GetByte(); |
| 803 | push_dyadic: |
| 804 | PUSH_OPERATOR(operator); |
| 805 | alu_state = STATE_TRY_TO_REDUCE_STACKS; |
| 806 | break; |
| 807 | |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // call C's sin/cos/tan function |
| 812 | static void perform_fp(double (*fn)(double)) { |
| 813 | if((RIGHT_FLAGS & MVALUE_IS_FP) == 0) { |
| 814 | RIGHT_FPVAL = RIGHT_INTVAL; |
| 815 | RIGHT_FLAGS |= MVALUE_IS_FP; |
| 816 | } |
| 817 | RIGHT_FPVAL = fn(RIGHT_FPVAL); |
| 818 | } |
| 819 | |
| 820 | // make sure arg is in [-1, 1] range before calling function |
| 821 | static void perform_ranged_fp(double (*fn)(double)) { |
| 822 | if((RIGHT_FLAGS & MVALUE_IS_FP) == 0) { |
| 823 | RIGHT_FPVAL = RIGHT_INTVAL; |
| 824 | RIGHT_FLAGS |= MVALUE_IS_FP; |
| 825 | } |
| 826 | if((RIGHT_FPVAL >= -1) && (RIGHT_FPVAL <= 1)) |
| 827 | RIGHT_FPVAL = fn(RIGHT_FPVAL); |
| 828 | else { |
| 829 | if(RIGHT_FLAGS & MVALUE_DEFINED) |
| 830 | Throw_error("Argument out of range."); |
| 831 | RIGHT_FPVAL = 0; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // convert right-hand value from fp to int |
| 836 | static void right_fp_to_int() { |
| 837 | RIGHT_INTVAL = RIGHT_FPVAL; |
| 838 | RIGHT_FLAGS &= ~MVALUE_IS_FP; |
| 839 | } |
| 840 | |
| 841 | // check both left-hand and right-hand values. if float, convert to int. |
| 842 | // in first pass, throw warning |
| 843 | static void both_ensure_int(bool warn) { |
| 844 | if(LEFT_FLAGS & MVALUE_IS_FP) { |
| 845 | LEFT_INTVAL = LEFT_FPVAL; |
| 846 | LEFT_FLAGS &= ~MVALUE_IS_FP; |
| 847 | } |
| 848 | if(RIGHT_FLAGS & MVALUE_IS_FP) { |
| 849 | RIGHT_INTVAL = RIGHT_FPVAL; |
| 850 | RIGHT_FLAGS &= ~MVALUE_IS_FP; |
| 851 | } |
| 852 | Throw_first_pass_warning("Converted to integer for binary logic operator."); |
| 853 | } |
| 854 | |
| 855 | // check both left-hand and right-hand values. if int, convert to float. |
| 856 | static void both_ensure_fp() { |
| 857 | if((LEFT_FLAGS & MVALUE_IS_FP) == 0) { |
| 858 | LEFT_FPVAL = LEFT_INTVAL; |
| 859 | LEFT_FLAGS |= MVALUE_IS_FP; |
| 860 | } |
| 861 | if((RIGHT_FLAGS & MVALUE_IS_FP) == 0) { |
| 862 | RIGHT_FPVAL = RIGHT_INTVAL; |
| 863 | RIGHT_FLAGS |= MVALUE_IS_FP; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // make sure both values are float, but mark left one as int (will become one) |
| 868 | static void ensure_int_from_fp() { |
| 869 | both_ensure_fp(); |
| 870 | LEFT_FLAGS &= ~MVALUE_IS_FP; |
| 871 | } |
| 872 | |
| 873 | // Try to reduce stacks by performing high-priority operations |
| 874 | static inline void try_to_reduce_stacks(int* open_parentheses) { |
| 875 | if(operator_sp < 2) { |
| 876 | alu_state = STATE_EXPECT_OPERAND_OR_MONADIC_OPERATOR; |
| 877 | return; |
| 878 | } |
| 879 | if(operator_stack[operator_sp-2]->priority < operator_stack[operator_sp-1]->priority) { |
| 880 | alu_state = STATE_EXPECT_OPERAND_OR_MONADIC_OPERATOR; |
| 881 | return; |
| 882 | } |
| 883 | switch(operator_stack[operator_sp-2]->handle) { |
| 884 | |
| 885 | // special (pseudo) operators |
| 886 | |
| 887 | case OPHANDLE_RETURN: |
| 888 | // don't touch indirect_flag; needed for INDIRECT flag |
| 889 | operator_sp--;// decrement operator stack pointer |
| 890 | alu_state = STATE_END; |
| 891 | break; |
| 892 | |
| 893 | case OPHANDLE_OPENING: |
| 894 | indirect_flag = MVALUE_INDIRECT;// parentheses found |
| 895 | switch(operator_stack[operator_sp-1]->handle) { |
| 896 | |
| 897 | case OPHANDLE_CLOSING:// matching parentheses |
| 898 | operator_sp -= 2;// remove both of them |
| 899 | alu_state = STATE_EXPECT_DYADIC_OPERATOR; |
| 900 | break; |
| 901 | |
| 902 | case OPHANDLE_END:// unmatched parenthesis |
| 903 | (*open_parentheses)++;// count |
| 904 | goto RNTLObutDontTouchIndirectFlag; |
| 905 | |
| 906 | default: |
| 907 | Bug_found("StrangeParenthesis", operator_stack[operator_sp-1]->handle); |
| 908 | } |
| 909 | break; |
| 910 | |
| 911 | case OPHANDLE_CLOSING: |
| 912 | Throw_error("Too many ')'."); |
| 913 | goto remove_next_to_last_operator; |
| 914 | |
| 915 | // functions |
| 916 | |
| 917 | case OPHANDLE_INT: |
| 918 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 919 | right_fp_to_int(); |
| 920 | goto remove_next_to_last_operator; |
| 921 | |
| 922 | case OPHANDLE_FLOAT: |
| 923 | // convert right-hand value from int to fp |
| 924 | if((RIGHT_FLAGS & MVALUE_IS_FP) == 0) { |
| 925 | RIGHT_FPVAL = RIGHT_INTVAL; |
| 926 | RIGHT_FLAGS |= MVALUE_IS_FP; |
| 927 | } |
| 928 | goto remove_next_to_last_operator; |
| 929 | |
| 930 | case OPHANDLE_SIN: |
| 931 | perform_fp(sin); |
| 932 | goto remove_next_to_last_operator; |
| 933 | |
| 934 | case OPHANDLE_COS: |
| 935 | perform_fp(cos); |
| 936 | goto remove_next_to_last_operator; |
| 937 | |
| 938 | case OPHANDLE_TAN: |
| 939 | perform_fp(tan); |
| 940 | goto remove_next_to_last_operator; |
| 941 | |
| 942 | case OPHANDLE_ARCSIN: |
| 943 | perform_ranged_fp(asin); |
| 944 | goto remove_next_to_last_operator; |
| 945 | |
| 946 | case OPHANDLE_ARCCOS: |
| 947 | perform_ranged_fp(acos); |
| 948 | goto remove_next_to_last_operator; |
| 949 | |
| 950 | case OPHANDLE_ARCTAN: |
| 951 | perform_fp(atan); |
| 952 | goto remove_next_to_last_operator; |
| 953 | |
| 954 | // monadic operators |
| 955 | |
| 956 | case OPHANDLE_NOT: |
| 957 | // different operations for fp and int |
| 958 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 959 | right_fp_to_int(); |
| 960 | RIGHT_INTVAL = ~(RIGHT_INTVAL); |
| 961 | RIGHT_FLAGS &= ~MVALUE_ISBYTE; |
| 962 | goto remove_next_to_last_operator; |
| 963 | |
| 964 | case OPHANDLE_NEGATE: |
| 965 | // different operations for fp and int |
| 966 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 967 | RIGHT_FPVAL = -(RIGHT_FPVAL); |
| 968 | else |
| 969 | RIGHT_INTVAL = -(RIGHT_INTVAL); |
| 970 | RIGHT_FLAGS &= ~MVALUE_ISBYTE; |
| 971 | goto remove_next_to_last_operator; |
| 972 | |
| 973 | case OPHANDLE_LOWBYTEOF: |
| 974 | // fp becomes int |
| 975 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 976 | right_fp_to_int(); |
| 977 | RIGHT_INTVAL = (RIGHT_INTVAL) & 255; |
| 978 | RIGHT_FLAGS |= MVALUE_ISBYTE; |
| 979 | RIGHT_FLAGS &= ~MVALUE_FORCEBITS; |
| 980 | goto remove_next_to_last_operator; |
| 981 | |
| 982 | case OPHANDLE_HIGHBYTEOF: |
| 983 | // fp becomes int |
| 984 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 985 | right_fp_to_int(); |
| 986 | RIGHT_INTVAL = ((RIGHT_INTVAL) >> 8) & 255; |
| 987 | RIGHT_FLAGS |= MVALUE_ISBYTE; |
| 988 | RIGHT_FLAGS &= ~MVALUE_FORCEBITS; |
| 989 | goto remove_next_to_last_operator; |
| 990 | |
| 991 | case OPHANDLE_BANKBYTEOF: |
| 992 | // fp becomes int |
| 993 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 994 | right_fp_to_int(); |
| 995 | RIGHT_INTVAL = ((RIGHT_INTVAL) >> 16) & 255; |
| 996 | RIGHT_FLAGS |= MVALUE_ISBYTE; |
| 997 | RIGHT_FLAGS &= ~MVALUE_FORCEBITS; |
| 998 | goto remove_next_to_last_operator; |
| 999 | |
| 1000 | // dyadic operators |
| 1001 | |
| 1002 | case OPHANDLE_POWEROF: |
| 1003 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1004 | both_ensure_fp(); |
| 1005 | LEFT_FPVAL = pow(LEFT_FPVAL, RIGHT_FPVAL); |
| 1006 | goto handle_flags_and_dec_stacks; |
| 1007 | } |
| 1008 | if(RIGHT_INTVAL >= 0) |
| 1009 | LEFT_INTVAL = my_pow(LEFT_INTVAL, RIGHT_INTVAL); |
| 1010 | else { |
| 1011 | if(RIGHT_FLAGS & MVALUE_DEFINED) |
| 1012 | Throw_error("Exponent is negative."); |
| 1013 | LEFT_INTVAL = 0; |
| 1014 | } |
| 1015 | goto handle_flags_and_dec_stacks; |
| 1016 | |
| 1017 | case OPHANDLE_MULTIPLY: |
| 1018 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1019 | both_ensure_fp(); |
| 1020 | LEFT_FPVAL *= RIGHT_FPVAL; |
| 1021 | } else |
| 1022 | LEFT_INTVAL *= RIGHT_INTVAL; |
| 1023 | goto handle_flags_and_dec_stacks; |
| 1024 | |
| 1025 | case OPHANDLE_DIVIDE: |
| 1026 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1027 | both_ensure_fp(); |
| 1028 | if(RIGHT_FPVAL) |
| 1029 | LEFT_FPVAL /= RIGHT_FPVAL; |
| 1030 | else { |
| 1031 | if(RIGHT_FLAGS & MVALUE_DEFINED) |
| 1032 | Throw_error(exception_div_by_zero); |
| 1033 | LEFT_FPVAL = 0; |
| 1034 | } |
| 1035 | } else { |
| 1036 | if(RIGHT_INTVAL) |
| 1037 | LEFT_INTVAL /= RIGHT_INTVAL; |
| 1038 | else { |
| 1039 | if(RIGHT_FLAGS & MVALUE_DEFINED) |
| 1040 | Throw_error(exception_div_by_zero); |
| 1041 | LEFT_INTVAL = 0; |
| 1042 | } |
| 1043 | } |
| 1044 | goto handle_flags_and_dec_stacks; |
| 1045 | |
| 1046 | case OPHANDLE_INTDIV: |
| 1047 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1048 | both_ensure_fp(); |
| 1049 | if(RIGHT_FPVAL) |
| 1050 | LEFT_INTVAL = LEFT_FPVAL / RIGHT_FPVAL; |
| 1051 | else { |
| 1052 | if(RIGHT_FLAGS & MVALUE_DEFINED) |
| 1053 | Throw_error(exception_div_by_zero); |
| 1054 | LEFT_INTVAL = 0; |
| 1055 | } |
| 1056 | LEFT_FLAGS &= ~MVALUE_IS_FP; |
| 1057 | } else { |
| 1058 | if(RIGHT_INTVAL) |
| 1059 | LEFT_INTVAL /= RIGHT_INTVAL; |
| 1060 | else { |
| 1061 | if(RIGHT_FLAGS & MVALUE_DEFINED) |
| 1062 | Throw_error(exception_div_by_zero); |
| 1063 | LEFT_INTVAL = 0; |
| 1064 | } |
| 1065 | } |
| 1066 | goto handle_flags_and_dec_stacks; |
| 1067 | |
| 1068 | case OPHANDLE_MODULO: |
| 1069 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) |
| 1070 | both_ensure_int(FALSE); |
| 1071 | if(RIGHT_INTVAL) |
| 1072 | LEFT_INTVAL %= RIGHT_INTVAL; |
| 1073 | else { |
| 1074 | if(RIGHT_FLAGS & MVALUE_DEFINED) |
| 1075 | Throw_error(exception_div_by_zero); |
| 1076 | LEFT_INTVAL = 0; |
| 1077 | } |
| 1078 | goto handle_flags_and_dec_stacks; |
| 1079 | |
| 1080 | case OPHANDLE_ADD: |
| 1081 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1082 | both_ensure_fp(); |
| 1083 | LEFT_FPVAL += RIGHT_FPVAL; |
| 1084 | } else |
| 1085 | LEFT_INTVAL += RIGHT_INTVAL; |
| 1086 | goto handle_flags_and_dec_stacks; |
| 1087 | |
| 1088 | case OPHANDLE_SUBTRACT: |
| 1089 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1090 | both_ensure_fp(); |
| 1091 | LEFT_FPVAL -= RIGHT_FPVAL; |
| 1092 | } else |
| 1093 | LEFT_INTVAL -= RIGHT_INTVAL; |
| 1094 | goto handle_flags_and_dec_stacks; |
| 1095 | |
| 1096 | case OPHANDLE_SL: |
| 1097 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 1098 | right_fp_to_int(); |
| 1099 | if(LEFT_FLAGS & MVALUE_IS_FP) |
| 1100 | LEFT_FPVAL *= (1 << RIGHT_INTVAL); |
| 1101 | else |
| 1102 | LEFT_INTVAL <<= RIGHT_INTVAL; |
| 1103 | goto handle_flags_and_dec_stacks; |
| 1104 | |
| 1105 | case OPHANDLE_ASR: |
| 1106 | if(RIGHT_FLAGS & MVALUE_IS_FP) |
| 1107 | right_fp_to_int(); |
| 1108 | if(LEFT_FLAGS & MVALUE_IS_FP) |
| 1109 | LEFT_FPVAL /= (1 << RIGHT_INTVAL); |
| 1110 | else |
| 1111 | LEFT_INTVAL = my_asr(LEFT_INTVAL, RIGHT_INTVAL); |
| 1112 | goto handle_flags_and_dec_stacks; |
| 1113 | |
| 1114 | case OPHANDLE_LSR: |
| 1115 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) |
| 1116 | both_ensure_int(TRUE); |
| 1117 | LEFT_INTVAL = ((uintval_t) LEFT_INTVAL) >> RIGHT_INTVAL; |
| 1118 | goto handle_flags_and_dec_stacks; |
| 1119 | |
| 1120 | case OPHANDLE_LE: |
| 1121 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1122 | ensure_int_from_fp(); |
| 1123 | LEFT_INTVAL = (LEFT_FPVAL <= RIGHT_FPVAL); |
| 1124 | } else |
| 1125 | LEFT_INTVAL = (LEFT_INTVAL <= RIGHT_INTVAL); |
| 1126 | goto handle_flags_and_dec_stacks; |
| 1127 | |
| 1128 | case OPHANDLE_LESSTHAN: |
| 1129 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1130 | ensure_int_from_fp(); |
| 1131 | LEFT_INTVAL = (LEFT_FPVAL < RIGHT_FPVAL); |
| 1132 | } else |
| 1133 | LEFT_INTVAL = (LEFT_INTVAL < RIGHT_INTVAL); |
| 1134 | goto handle_flags_and_dec_stacks; |
| 1135 | |
| 1136 | case OPHANDLE_GE: |
| 1137 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1138 | ensure_int_from_fp(); |
| 1139 | LEFT_INTVAL = (LEFT_FPVAL >= RIGHT_FPVAL); |
| 1140 | } else |
| 1141 | LEFT_INTVAL = (LEFT_INTVAL >= RIGHT_INTVAL); |
| 1142 | goto handle_flags_and_dec_stacks; |
| 1143 | |
| 1144 | case OPHANDLE_GREATERTHAN: |
| 1145 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1146 | ensure_int_from_fp(); |
| 1147 | LEFT_INTVAL = (LEFT_FPVAL > RIGHT_FPVAL); |
| 1148 | } else |
| 1149 | LEFT_INTVAL = (LEFT_INTVAL > RIGHT_INTVAL); |
| 1150 | goto handle_flags_and_dec_stacks; |
| 1151 | |
| 1152 | case OPHANDLE_NOTEQUAL: |
| 1153 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1154 | ensure_int_from_fp(); |
| 1155 | LEFT_INTVAL = (LEFT_FPVAL != RIGHT_FPVAL); |
| 1156 | } else |
| 1157 | LEFT_INTVAL = (LEFT_INTVAL != RIGHT_INTVAL); |
| 1158 | goto handle_flags_and_dec_stacks; |
| 1159 | |
| 1160 | case OPHANDLE_EQUALS: |
| 1161 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) { |
| 1162 | ensure_int_from_fp(); |
| 1163 | LEFT_INTVAL = (LEFT_FPVAL == RIGHT_FPVAL); |
| 1164 | } else |
| 1165 | LEFT_INTVAL = (LEFT_INTVAL == RIGHT_INTVAL); |
| 1166 | goto handle_flags_and_dec_stacks; |
| 1167 | |
| 1168 | case OPHANDLE_AND: |
| 1169 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) |
| 1170 | both_ensure_int(TRUE); |
| 1171 | LEFT_INTVAL &= RIGHT_INTVAL; |
| 1172 | goto handle_flags_and_dec_stacks; |
| 1173 | |
| 1174 | case OPHANDLE_EOR: |
| 1175 | Throw_first_pass_warning("\"EOR\" is deprecated; use \"XOR\" instead."); |
| 1176 | /*FALLTHROUGH*/ |
| 1177 | |
| 1178 | case OPHANDLE_XOR: |
| 1179 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) |
| 1180 | both_ensure_int(TRUE); |
| 1181 | LEFT_INTVAL ^= RIGHT_INTVAL; |
| 1182 | goto handle_flags_and_dec_stacks; |
| 1183 | |
| 1184 | case OPHANDLE_OR: |
| 1185 | if((RIGHT_FLAGS | LEFT_FLAGS) & MVALUE_IS_FP) |
| 1186 | both_ensure_int(TRUE); |
| 1187 | LEFT_INTVAL |= RIGHT_INTVAL; |
| 1188 | goto handle_flags_and_dec_stacks; |
| 1189 | |
| 1190 | default: |
| 1191 | Bug_found("IllegalOperatorHandle", operator_stack[operator_sp-2]->handle); |
| 1192 | break; |
| 1193 | |
| 1194 | // no other possibilities, so here are the shared endings |
| 1195 | |
| 1196 | // entry point for dyadic operators |
| 1197 | handle_flags_and_dec_stacks: |
| 1198 | // Handle flags and decrement value stack pointer |
| 1199 | // "OR" EXISTS, UNSURE and FORCEBIT flags |
| 1200 | LEFT_FLAGS |= RIGHT_FLAGS & |
| 1201 | (MVALUE_EXISTS|MVALUE_UNSURE|MVALUE_FORCEBITS); |
| 1202 | // "AND" DEFINED flag |
| 1203 | LEFT_FLAGS &= (RIGHT_FLAGS | ~MVALUE_DEFINED); |
| 1204 | LEFT_FLAGS &= ~MVALUE_ISBYTE;// clear ISBYTE flag |
| 1205 | operand_sp--; |
| 1206 | /*FALLTHROUGH*/ |
| 1207 | |
| 1208 | // entry point for monadic operators |
| 1209 | remove_next_to_last_operator: |
| 1210 | // toplevel operation was something other than parentheses |
| 1211 | indirect_flag = 0; |
| 1212 | /*FALLTHROUGH*/ |
| 1213 | |
| 1214 | // entry point for '(' operator (has set indirect_flag, so don't clear now) |
| 1215 | RNTLObutDontTouchIndirectFlag: |
| 1216 | // Remove operator and shift down next one |
| 1217 | operator_stack[operator_sp-2] = operator_stack[operator_sp-1]; |
| 1218 | operator_sp--;// decrement operator stack pointer |
| 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | // The core of it. Returns number of parentheses left open. |
| 1224 | // FIXME - make state machine using function pointers? or too slow? |
| 1225 | static int parse_expression(result_t* result) { |
| 1226 | int open_parentheses = 0; |
| 1227 | |
| 1228 | operator_sp = 0; // operator stack pointer |
| 1229 | operand_sp = 0; // value stack pointer |
| 1230 | // begin by reading value (or monadic operator) |
| 1231 | alu_state = STATE_EXPECT_OPERAND_OR_MONADIC_OPERATOR; |
| 1232 | indirect_flag = 0; // Contains either 0 or MVALUE_INDIRECT |
| 1233 | PUSH_OPERATOR(&ops_return); |
| 1234 | do { |
| 1235 | // check stack sizes. enlarge if needed |
| 1236 | if(operator_sp >= operator_stk_size) |
| 1237 | enlarge_operator_stack(); |
| 1238 | if(operand_sp >= operand_stk_size) |
| 1239 | enlarge_operand_stack(); |
| 1240 | switch(alu_state) { |
| 1241 | |
| 1242 | case STATE_EXPECT_OPERAND_OR_MONADIC_OPERATOR: |
| 1243 | expect_operand_or_monadic_operator(); |
| 1244 | break; |
| 1245 | |
| 1246 | case STATE_EXPECT_DYADIC_OPERATOR: |
| 1247 | expect_dyadic_operator(); |
| 1248 | break;// no fallthrough; state might |
| 1249 | // have been changed to END or ERROR |
| 1250 | |
| 1251 | case STATE_TRY_TO_REDUCE_STACKS: |
| 1252 | try_to_reduce_stacks(&open_parentheses); |
| 1253 | break; |
| 1254 | |
| 1255 | case STATE_MAX_GO_ON: // suppress |
| 1256 | case STATE_ERROR: // compiler |
| 1257 | case STATE_END: // warnings |
| 1258 | break; |
| 1259 | |
| 1260 | } |
| 1261 | } while(alu_state < STATE_MAX_GO_ON); |
| 1262 | // done. check state. |
| 1263 | if(alu_state == STATE_END) { |
| 1264 | // check for bugs |
| 1265 | if(operand_sp != 1) |
| 1266 | Bug_found("OperandStackNotEmpty", operand_sp); |
| 1267 | if(operator_sp != 1) |
| 1268 | Bug_found("OperatorStackNotEmpty", operator_sp); |
| 1269 | // copy result |
| 1270 | *result = operand_stack[0]; |
| 1271 | result->flags |= indirect_flag; // OR indirect flag |
| 1272 | // only allow *one* force bit |
| 1273 | if(result->flags & MVALUE_FORCE24) |
| 1274 | result->flags &= ~(MVALUE_FORCE16 | MVALUE_FORCE08); |
| 1275 | else if(result->flags & MVALUE_FORCE16) |
| 1276 | result->flags &= ~MVALUE_FORCE08; |
| 1277 | // if there was nothing to parse, mark as undefined |
| 1278 | // (so ALU_defined_int() can react) |
| 1279 | if((result->flags & MVALUE_EXISTS) == 0) |
| 1280 | result->flags &= ~MVALUE_DEFINED; |
| 1281 | // do some checks depending on int/float |
| 1282 | if(result->flags & MVALUE_IS_FP) { |
| 1283 | /*float*/ // if undefined, return zero |
| 1284 | if((result->flags & MVALUE_DEFINED) == 0) |
| 1285 | result->val.fpval = 0; |
| 1286 | // if value is sure, check to set ISBYTE |
| 1287 | else if(((result->flags & MVALUE_UNSURE) == 0) |
| 1288 | && (result->val.fpval <= 255.0) |
| 1289 | && (result->val.fpval >= -128.0)) |
| 1290 | result->flags |= MVALUE_ISBYTE; |
| 1291 | } else { |
| 1292 | /*int*/ // if undefined, return zero |
| 1293 | if((result->flags & MVALUE_DEFINED) == 0) |
| 1294 | result->val.intval = 0; |
| 1295 | // if value is sure, check to set ISBYTE |
| 1296 | else if(((result->flags & MVALUE_UNSURE) == 0) |
| 1297 | && (result->val.intval <= 255) |
| 1298 | && (result->val.intval >= -128)) |
| 1299 | result->flags |= MVALUE_ISBYTE; |
| 1300 | } |
| 1301 | } else { |
| 1302 | // State is STATE_ERROR. But actually, nobody cares. |
| 1303 | // ...errors have already been reported anyway. :) |
| 1304 | } |
| 1305 | // return number of open (unmatched) parentheses |
| 1306 | return(open_parentheses); |
| 1307 | } |
| 1308 | |
| 1309 | // These functions handle numerical expressions. There are operators for |
| 1310 | // arithmetic, logic, shift and comparison operations. |
| 1311 | // There are several different ways to call the core function: |
| 1312 | // intval_t ALU_any_int(void); |
| 1313 | // returns int value (0 if result was undefined) |
| 1314 | // intval_t ALU_defined_int(void); |
| 1315 | // returns int value |
| 1316 | // if result was undefined, serious error is thrown |
| 1317 | // void ALU_int_result(result_int_t*); |
| 1318 | // stores int value and flags (floats are transformed to int) |
| 1319 | // void ALU_any_result(result_t*); |
| 1320 | // stores value and flags (result may be either int or float) |
| 1321 | // int ALU_liberal_int(result_int_t*); |
| 1322 | // stores int value and flags. allows one '(' too many (for x- |
| 1323 | // indirect addressing). returns number of additional '(' (1 or 0). |
| 1324 | // bool ALU_optional_defined_int(intval_t*); |
| 1325 | // stores int value if given. Returns whether stored. |
| 1326 | // Throws error if undefined. |
| 1327 | |
| 1328 | // return int value (if result is undefined, returns zero) |
| 1329 | // It the result's "exists" flag is clear (=empty expression), it throws an |
| 1330 | // error. |
| 1331 | // If the result's "defined" flag is clear, result_is_undefined() is called. |
| 1332 | intval_t ALU_any_int(void) { |
| 1333 | result_t result; |
| 1334 | |
| 1335 | if(parse_expression(&result)) |
| 1336 | Throw_error(exception_paren_open); |
| 1337 | if((result.flags & MVALUE_EXISTS) == 0) |
| 1338 | Throw_error(exception_no_value); |
| 1339 | else if((result.flags & MVALUE_DEFINED) == 0) |
| 1340 | result_is_undefined(); |
| 1341 | if(result.flags & MVALUE_IS_FP) |
| 1342 | return(result.val.fpval); |
| 1343 | else |
| 1344 | return(result.val.intval); |
| 1345 | } |
| 1346 | |
| 1347 | // return int value (if result is undefined, serious error is thrown) |
| 1348 | intval_t ALU_defined_int(void) { |
| 1349 | result_t result; |
| 1350 | |
| 1351 | if(parse_expression(&result)) |
| 1352 | Throw_error(exception_paren_open); |
| 1353 | if((result.flags & MVALUE_DEFINED) == 0) |
| 1354 | Throw_serious_error(exception_undefined); |
| 1355 | if(result.flags & MVALUE_IS_FP) |
| 1356 | return(result.val.fpval); |
| 1357 | else |
| 1358 | return(result.val.intval); |
| 1359 | } |
| 1360 | |
| 1361 | // Store int value if given. Returns whether stored. Throws error if undefined. |
| 1362 | // This function needs either a defined value or no expression at all. So |
| 1363 | // empty expressions are accepted, but undefined ones are not. |
| 1364 | // If the result's "defined" flag is clear and the "exists" flag is set, it |
| 1365 | // throws a serious error and therefore stops assembly. |
| 1366 | bool ALU_optional_defined_int(intval_t* target) { |
| 1367 | result_t result; |
| 1368 | |
| 1369 | if(parse_expression(&result)) |
| 1370 | Throw_error(exception_paren_open); |
| 1371 | if((result.flags & MVALUE_GIVEN) == MVALUE_EXISTS) |
| 1372 | Throw_serious_error(exception_undefined); |
| 1373 | if((result.flags & MVALUE_EXISTS) == 0) |
| 1374 | return(FALSE); |
| 1375 | // something was given, so store |
| 1376 | if(result.flags & MVALUE_IS_FP) |
| 1377 | *target = result.val.fpval; |
| 1378 | else |
| 1379 | *target = result.val.intval; |
| 1380 | return(TRUE); |
| 1381 | } |
| 1382 | |
| 1383 | // Store int value and flags (floats are transformed to int) |
| 1384 | // It the result's "exists" flag is clear (=empty expression), it throws an |
| 1385 | // error. |
| 1386 | // If the result's "defined" flag is clear, result_is_undefined() is called. |
| 1387 | void ALU_int_result(result_int_t* intresult) { |
| 1388 | result_t result; |
| 1389 | |
| 1390 | if(parse_expression(&result)) |
| 1391 | Throw_error(exception_paren_open); |
| 1392 | if((result.flags & MVALUE_EXISTS) == 0) |
| 1393 | Throw_error(exception_no_value); |
| 1394 | else if((result.flags & MVALUE_DEFINED) == 0) |
| 1395 | result_is_undefined(); |
| 1396 | if(result.flags & MVALUE_IS_FP) { |
| 1397 | intresult->intval = result.val.fpval; |
| 1398 | intresult->flags = result.flags & ~MVALUE_IS_FP; |
| 1399 | } else { |
| 1400 | intresult->intval = result.val.intval; |
| 1401 | intresult->flags = result.flags; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | // Store int value and flags. |
| 1406 | // This function allows for one '(' too many. Needed when parsing indirect |
| 1407 | // addressing modes where internal indices have to be possible. Returns number |
| 1408 | // of parentheses still open (either 0 or 1). |
| 1409 | int ALU_liberal_int(result_int_t* intresult) { |
| 1410 | result_t result; |
| 1411 | int parentheses_still_open; |
| 1412 | |
| 1413 | parentheses_still_open = parse_expression(&result); |
| 1414 | if(parentheses_still_open > 1) { |
| 1415 | parentheses_still_open = 0; |
| 1416 | Throw_error(exception_paren_open); |
| 1417 | } |
| 1418 | if((result.flags & MVALUE_EXISTS) |
| 1419 | && ((result.flags & MVALUE_DEFINED) == 0)) |
| 1420 | result_is_undefined(); |
| 1421 | if(result.flags & MVALUE_IS_FP) { |
| 1422 | intresult->intval = result.val.fpval; |
| 1423 | intresult->flags = result.flags & ~MVALUE_IS_FP; |
| 1424 | } else { |
| 1425 | intresult->intval = result.val.intval; |
| 1426 | intresult->flags = result.flags; |
| 1427 | } |
| 1428 | return(parentheses_still_open); |
| 1429 | } |
| 1430 | |
| 1431 | // Store value and flags (result may be either int or float) |
| 1432 | // It the result's "exists" flag is clear (=empty expression), it throws an |
| 1433 | // error. |
| 1434 | // If the result's "defined" flag is clear, result_is_undefined() is called. |
| 1435 | void ALU_any_result(result_t* result) { |
| 1436 | if(parse_expression(result)) |
| 1437 | Throw_error(exception_paren_open); |
| 1438 | if((result->flags & MVALUE_EXISTS) == 0) |
| 1439 | Throw_error(exception_no_value); |
| 1440 | else if((result->flags & MVALUE_DEFINED) == 0) |
| 1441 | result_is_undefined(); |
| 1442 | } |