| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "pseudoopcodes.h" |
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include "config.h" |
| 11 |
#include "cpu.h" |
| 12 |
#include "alu.h" |
| 13 |
#include "dynabuf.h" |
| 14 |
#include "encoding.h" |
| 15 |
#include "flow.h" |
| 16 |
#include "input.h" |
| 17 |
#include "macro.h" |
| 18 |
#include "global.h" |
| 19 |
#include "output.h" |
| 20 |
#include "section.h" |
| 21 |
#include "symbol.h" |
| 22 |
#include "tree.h" |
| 23 |
#include "typesystem.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static boolean line_uses_keyword_arguments(void) |
| 29 |
{ |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
return FALSE; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
enum eos { |
| 44 |
SKIP_REMAINDER, |
| 45 |
ENSURE_EOS, |
| 46 |
PARSE_REMAINDER, |
| 47 |
AT_EOS_ANYWAY |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
static const char exception_want_IN_or_comma[] = "Expected ',' or IN keyword after loop variable."; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
static void end_all_pseudopc(void) |
| 56 |
{ |
| 57 |
while (pseudopc_isactive()) |
| 58 |
pseudopc_end(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static enum eos po_initmem(void) |
| 67 |
{ |
| 68 |
struct number intresult; |
| 69 |
|
| 70 |
|
| 71 |
if (pass.number != 1) |
| 72 |
return SKIP_REMAINDER; |
| 73 |
|
| 74 |
|
| 75 |
if (config.mem_init_value != NO_VALUE_GIVEN) { |
| 76 |
throw_warning("Memory already initialised."); |
| 77 |
return SKIP_REMAINDER; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
ALU_defined_int(&intresult); |
| 85 |
if ((intresult.val.intval > 255) || (intresult.val.intval < -128)) |
| 86 |
throw_error(exception_number_out_of_8b_range); |
| 87 |
|
| 88 |
|
| 89 |
config.mem_init_value = intresult.val.intval & 0xff; |
| 90 |
|
| 91 |
return ENSURE_EOS; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
static enum eos po_to(void) |
| 96 |
{ |
| 97 |
enum outfile_format format; |
| 98 |
|
| 99 |
|
| 100 |
if (pass.number != 1) |
| 101 |
return SKIP_REMAINDER; |
| 102 |
|
| 103 |
|
| 104 |
if (config.output_filename) { |
| 105 |
throw_warning("Output file name already chosen."); |
| 106 |
return SKIP_REMAINDER; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
if (input_read_output_filename()) |
| 112 |
return SKIP_REMAINDER; |
| 113 |
|
| 114 |
|
| 115 |
config.output_filename = dynabuf_get_copy(GlobalDynaBuf); |
| 116 |
|
| 117 |
|
| 118 |
if (parser_accept_comma()) { |
| 119 |
|
| 120 |
|
| 121 |
if (parser_read_and_lower_keyword() == 0) |
| 122 |
return SKIP_REMAINDER; |
| 123 |
|
| 124 |
format = outputformat_find(); |
| 125 |
if (format == OUTFILE_FORMAT_UNSPECIFIED) { |
| 126 |
throw_error("Unknown output format."); |
| 127 |
return SKIP_REMAINDER; |
| 128 |
} |
| 129 |
} else { |
| 130 |
|
| 131 |
if (config.dialect >= V0_86__DEPRECATE_REALPC) { |
| 132 |
throw_warning("Used \"!to\" without file format indicator."); |
| 133 |
} else { |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
format = OUTFILE_FORMAT_CBM; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if (config.outfile_format != OUTFILE_FORMAT_UNSPECIFIED) { |
| 142 |
throw_warning("Output file format already chosen."); |
| 143 |
} else { |
| 144 |
config.outfile_format = format; |
| 145 |
} |
| 146 |
|
| 147 |
return ENSURE_EOS; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
static enum eos po_symbollist(void) |
| 152 |
{ |
| 153 |
|
| 154 |
if (pass.number != 1) |
| 155 |
return SKIP_REMAINDER; |
| 156 |
|
| 157 |
|
| 158 |
if (config.symbollist_filename) { |
| 159 |
throw_warning("Symbol list file name already chosen."); |
| 160 |
return SKIP_REMAINDER; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if (input_read_output_filename()) |
| 166 |
return SKIP_REMAINDER; |
| 167 |
|
| 168 |
|
| 169 |
config.symbollist_filename = dynabuf_get_copy(GlobalDynaBuf); |
| 170 |
|
| 171 |
return ENSURE_EOS; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static enum eos po_outfilestart(void) |
| 180 |
{ |
| 181 |
static int last_pass_number = -1; |
| 182 |
|
| 183 |
if ((config.outfile_start != NO_VALUE_GIVEN) |
| 184 |
|| (last_pass_number == pass.number)) { |
| 185 |
throw_pass1_warning("Start of output file already chosen."); |
| 186 |
} else { |
| 187 |
last_pass_number = pass.number; |
| 188 |
outbuf_set_outfile_start(); |
| 189 |
} |
| 190 |
return ENSURE_EOS; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
static enum eos po_outfilelimit(void) |
| 195 |
{ |
| 196 |
static int last_pass_number = -1; |
| 197 |
|
| 198 |
if ((config.outfile_limit != NO_VALUE_GIVEN) |
| 199 |
|| (last_pass_number == pass.number)) { |
| 200 |
throw_pass1_warning("End of output file already chosen."); |
| 201 |
} else { |
| 202 |
last_pass_number = pass.number; |
| 203 |
outbuf_set_outfile_limit(); |
| 204 |
} |
| 205 |
return ENSURE_EOS; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
static enum eos po_xor(void) |
| 215 |
{ |
| 216 |
char old_value; |
| 217 |
intval_t change; |
| 218 |
|
| 219 |
old_value = output_get_xor(); |
| 220 |
ALU_any_int(&change); |
| 221 |
if ((change > 255) || (change < -128)) { |
| 222 |
throw_error(exception_number_out_of_8b_range); |
| 223 |
change = 0; |
| 224 |
} |
| 225 |
output_set_xor(old_value ^ change); |
| 226 |
|
| 227 |
if (parse_optional_block()) |
| 228 |
output_set_xor(old_value); |
| 229 |
return ENSURE_EOS; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
static enum eos iterate(void (*fn)(intval_t)) |
| 235 |
{ |
| 236 |
struct iter_context iter; |
| 237 |
struct object object; |
| 238 |
|
| 239 |
iter.fn = fn; |
| 240 |
iter.accept_long_strings = FALSE; |
| 241 |
iter.stringxor = 0; |
| 242 |
do { |
| 243 |
ALU_any_result(&object); |
| 244 |
output_object(&object, &iter); |
| 245 |
} while (parser_accept_comma()); |
| 246 |
return ENSURE_EOS; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static enum eos po_byte(void) |
| 252 |
{ |
| 253 |
return iterate(output_8); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static enum eos po_16(void) |
| 259 |
{ |
| 260 |
return iterate((cpu_current_type->flags & CPUFLAG_ISBIGENDIAN) ? output_be16 : output_le16); |
| 261 |
} |
| 262 |
|
| 263 |
static enum eos po_be16(void) |
| 264 |
{ |
| 265 |
return iterate(output_be16); |
| 266 |
} |
| 267 |
|
| 268 |
static enum eos po_le16(void) |
| 269 |
{ |
| 270 |
return iterate(output_le16); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
static enum eos po_24(void) |
| 276 |
{ |
| 277 |
return iterate((cpu_current_type->flags & CPUFLAG_ISBIGENDIAN) ? output_be24 : output_le24); |
| 278 |
} |
| 279 |
|
| 280 |
static enum eos po_be24(void) |
| 281 |
{ |
| 282 |
return iterate(output_be24); |
| 283 |
} |
| 284 |
|
| 285 |
static enum eos po_le24(void) |
| 286 |
{ |
| 287 |
return iterate(output_le24); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
static enum eos po_32(void) |
| 293 |
{ |
| 294 |
return iterate((cpu_current_type->flags & CPUFLAG_ISBIGENDIAN) ? output_be32 : output_le32); |
| 295 |
} |
| 296 |
|
| 297 |
static enum eos po_be32(void) |
| 298 |
{ |
| 299 |
return iterate(output_be32); |
| 300 |
} |
| 301 |
|
| 302 |
static enum eos po_le32(void) |
| 303 |
{ |
| 304 |
return iterate(output_le32); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static enum eos po_hex(void) |
| 310 |
{ |
| 311 |
int digits = 0; |
| 312 |
unsigned char byte = 0; |
| 313 |
|
| 314 |
for (;;) { |
| 315 |
if (digits == 2) { |
| 316 |
output_byte(byte); |
| 317 |
digits = 0; |
| 318 |
byte = 0; |
| 319 |
} |
| 320 |
if (GotByte >= '0' && GotByte <= '9') { |
| 321 |
byte = (byte << 4) | (GotByte - '0'); |
| 322 |
++digits; |
| 323 |
GetByte(); |
| 324 |
continue; |
| 325 |
} |
| 326 |
if (GotByte >= 'a' && GotByte <= 'f') { |
| 327 |
byte = (byte << 4) | (GotByte - 'a' + 10); |
| 328 |
++digits; |
| 329 |
GetByte(); |
| 330 |
continue; |
| 331 |
} |
| 332 |
if (GotByte >= 'A' && GotByte <= 'F') { |
| 333 |
byte = (byte << 4) | (GotByte - 'A' + 10); |
| 334 |
++digits; |
| 335 |
GetByte(); |
| 336 |
continue; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
if (digits == 1) { |
| 341 |
throw_error("Hex digits are not given in pairs."); |
| 342 |
return SKIP_REMAINDER; |
| 343 |
} |
| 344 |
switch (GotByte) { |
| 345 |
case ' ': |
| 346 |
case '\t': |
| 347 |
GetByte(); |
| 348 |
continue; |
| 349 |
case CHAR_EOS: |
| 350 |
return AT_EOS_ANYWAY; |
| 351 |
default: |
| 352 |
throw_error(exception_syntax); |
| 353 |
return SKIP_REMAINDER; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static enum eos po_cbm(void) |
| 361 |
{ |
| 362 |
if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) { |
| 363 |
throw_error("\"!cbm\" is obsolete; use \"!ct pet\" instead."); |
| 364 |
} else { |
| 365 |
encoder_current = &encoder_pet; |
| 366 |
throw_finalpass_warning("\"!cbm\" is deprecated; use \"!ct pet\" instead."); |
| 367 |
} |
| 368 |
return ENSURE_EOS; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
static enum eos use_encoding_from_file(void) |
| 374 |
{ |
| 375 |
struct filespecflags flags; |
| 376 |
FILE *stream; |
| 377 |
unsigned char local_table[256], |
| 378 |
*buffered_table; |
| 379 |
const struct encoder *buffered_encoder; |
| 380 |
|
| 381 |
|
| 382 |
if (input_read_input_filename(&flags)) |
| 383 |
return SKIP_REMAINDER; |
| 384 |
|
| 385 |
|
| 386 |
stream = includepaths_open_ro(&flags); |
| 387 |
if (stream) { |
| 388 |
|
| 389 |
if (fread(local_table, sizeof(char), 256, stream) != 256) |
| 390 |
throw_error("Conversion table incomplete."); |
| 391 |
fclose(stream); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
buffered_encoder = encoder_current; |
| 396 |
encoder_current = &encoder_file; |
| 397 |
buffered_table = encoding_loaded_table; |
| 398 |
encoding_loaded_table = local_table; |
| 399 |
|
| 400 |
if (parse_optional_block()) { |
| 401 |
encoder_current = buffered_encoder; |
| 402 |
} else { |
| 403 |
|
| 404 |
|
| 405 |
memcpy(buffered_table, local_table, 256); |
| 406 |
} |
| 407 |
|
| 408 |
encoding_loaded_table = buffered_table; |
| 409 |
return ENSURE_EOS; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
static enum eos use_predefined_encoding(const struct encoder *new_encoder) |
| 415 |
{ |
| 416 |
unsigned char local_table[256], |
| 417 |
*buffered_table; |
| 418 |
const struct encoder *buffered_encoder; |
| 419 |
|
| 420 |
|
| 421 |
buffered_encoder = encoder_current; |
| 422 |
encoder_current = new_encoder; |
| 423 |
buffered_table = encoding_loaded_table; |
| 424 |
encoding_loaded_table = local_table; |
| 425 |
|
| 426 |
if (parse_optional_block()) |
| 427 |
encoder_current = buffered_encoder; |
| 428 |
|
| 429 |
encoding_loaded_table = buffered_table; |
| 430 |
return ENSURE_EOS; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static enum eos po_convtab(void) |
| 436 |
{ |
| 437 |
|
| 438 |
if ((GotByte == '<') || (GotByte == '"')) |
| 439 |
return use_encoding_from_file(); |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
if (parser_read_and_lower_keyword() == 0) |
| 444 |
return SKIP_REMAINDER; |
| 445 |
|
| 446 |
|
| 447 |
if (strcmp(GlobalDynaBuf->buffer, "pet") == 0) |
| 448 |
return use_predefined_encoding(&encoder_pet); |
| 449 |
|
| 450 |
if (strcmp(GlobalDynaBuf->buffer, "raw") == 0) |
| 451 |
return use_predefined_encoding(&encoder_raw); |
| 452 |
|
| 453 |
if (strcmp(GlobalDynaBuf->buffer, "scr") == 0) |
| 454 |
return use_predefined_encoding(&encoder_scr); |
| 455 |
|
| 456 |
if (strcmp(GlobalDynaBuf->buffer, "file") == 0) { |
| 457 |
SKIPSPACE(); |
| 458 |
if (!parser_expect('=')) { |
| 459 |
return SKIP_REMAINDER; |
| 460 |
} |
| 461 |
return use_encoding_from_file(); |
| 462 |
} |
| 463 |
|
| 464 |
throw_error("Unknown encoding."); |
| 465 |
return use_predefined_encoding(encoder_current); |
| 466 |
} |
| 467 |
|
| 468 |
static enum eos encode_string(const struct encoder *inner_encoder, unsigned char xor) |
| 469 |
{ |
| 470 |
const struct encoder *outer_encoder = encoder_current; |
| 471 |
struct iter_context iter; |
| 472 |
struct object object; |
| 473 |
int offset; |
| 474 |
|
| 475 |
iter.fn = output_8; |
| 476 |
iter.accept_long_strings = TRUE; |
| 477 |
iter.stringxor = xor; |
| 478 |
|
| 479 |
encoder_current = inner_encoder; |
| 480 |
|
| 481 |
if (config.dialect >= V0_97__BACKSLASH_ESCAPING) { |
| 482 |
|
| 483 |
do { |
| 484 |
ALU_any_result(&object); |
| 485 |
output_object(&object, &iter); |
| 486 |
} while (parser_accept_comma()); |
| 487 |
} else { |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
do { |
| 493 |
if (GotByte == '"') { |
| 494 |
|
| 495 |
if (input_read_string_literal('"')) |
| 496 |
break; |
| 497 |
|
| 498 |
for (offset = 0; offset < GlobalDynaBuf->size; ++offset) |
| 499 |
output_8(xor ^ encoding_encode_char(GLOBALDYNABUF_CURRENT[offset])); |
| 500 |
} else { |
| 501 |
|
| 502 |
ALU_any_result(&object); |
| 503 |
output_object(&object, &iter); |
| 504 |
} |
| 505 |
} while (parser_accept_comma()); |
| 506 |
} |
| 507 |
|
| 508 |
encoder_current = outer_encoder; |
| 509 |
return ENSURE_EOS; |
| 510 |
} |
| 511 |
|
| 512 |
static enum eos po_text(void) |
| 513 |
{ |
| 514 |
return encode_string(encoder_current, 0); |
| 515 |
} |
| 516 |
|
| 517 |
static enum eos po_raw(void) |
| 518 |
{ |
| 519 |
return encode_string(&encoder_raw, 0); |
| 520 |
} |
| 521 |
|
| 522 |
static enum eos po_pet(void) |
| 523 |
{ |
| 524 |
return encode_string(&encoder_pet, 0); |
| 525 |
} |
| 526 |
|
| 527 |
static enum eos po_scr(void) |
| 528 |
{ |
| 529 |
return encode_string(&encoder_scr, 0); |
| 530 |
} |
| 531 |
|
| 532 |
static enum eos po_scrxor(void) |
| 533 |
{ |
| 534 |
intval_t xor; |
| 535 |
|
| 536 |
ALU_any_int(&xor); |
| 537 |
if (parser_expect(',')) { |
| 538 |
return encode_string(&encoder_scr, xor); |
| 539 |
} |
| 540 |
return SKIP_REMAINDER; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
static void include_file(FILE *stream, intval_t size, intval_t offset) |
| 545 |
{ |
| 546 |
int byte, |
| 547 |
amount; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
if ((size >= 0) && (!pass.flags.generate_output)) { |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
while (size != 0) { |
| 557 |
output_byte(0); |
| 558 |
--size; |
| 559 |
} |
| 560 |
return; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
fseek(stream, offset, SEEK_SET); |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
while (size != 0) { |
| 569 |
byte = getc(stream); |
| 570 |
if (byte == EOF) |
| 571 |
break; |
| 572 |
output_byte(byte); |
| 573 |
--size; |
| 574 |
} |
| 575 |
|
| 576 |
if (size > 0) { |
| 577 |
throw_warning("Padding with zeroes."); |
| 578 |
do { |
| 579 |
output_byte(0); |
| 580 |
} while (--size); |
| 581 |
} |
| 582 |
|
| 583 |
if (config.process_verbosity >= 2) { |
| 584 |
amount = output_get_statement_size(); |
| 585 |
printf("Loaded %d (0x%04x) bytes from file offset %d (0x%04x).\n", |
| 586 |
amount, amount, offset, offset); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
static enum eos po_binary(void) |
| 591 |
{ |
| 592 |
struct filespecflags flags; |
| 593 |
FILE *stream; |
| 594 |
struct number arg; |
| 595 |
intval_t size = NO_VALUE_GIVEN, |
| 596 |
skip = 0; |
| 597 |
|
| 598 |
|
| 599 |
if (input_read_input_filename(&flags)) |
| 600 |
return SKIP_REMAINDER; |
| 601 |
|
| 602 |
|
| 603 |
stream = includepaths_open_ro(&flags); |
| 604 |
if (stream == NULL) |
| 605 |
return SKIP_REMAINDER; |
| 606 |
|
| 607 |
|
| 608 |
if (parser_accept_comma()) { |
| 609 |
if (line_uses_keyword_arguments()) { |
| 610 |
|
| 611 |
|
| 612 |
BUG("KeywordArgsNotYet", 0); |
| 613 |
} else { |
| 614 |
|
| 615 |
if ((GotByte != ',') && (GotByte != CHAR_EOS)) { |
| 616 |
|
| 617 |
ALU_defined_int(&arg); |
| 618 |
size = arg.val.intval; |
| 619 |
|
| 620 |
|
| 621 |
if (size < 0) |
| 622 |
throw_serious_error(exception_negative_size); |
| 623 |
} |
| 624 |
|
| 625 |
if (parser_accept_comma()) { |
| 626 |
|
| 627 |
if (GotByte != CHAR_EOS) { |
| 628 |
|
| 629 |
ALU_defined_int(&arg); |
| 630 |
skip = arg.val.intval; |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
include_file(stream, size, skip); |
| 637 |
fclose(stream); |
| 638 |
return ENSURE_EOS; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
static enum eos po_fill(void) |
| 644 |
{ |
| 645 |
struct number sizeresult; |
| 646 |
intval_t fill = FILLVALUE_FILL; |
| 647 |
|
| 648 |
if (line_uses_keyword_arguments()) { |
| 649 |
|
| 650 |
|
| 651 |
BUG("KeywordArgsNotYet", 1); |
| 652 |
} else { |
| 653 |
|
| 654 |
ALU_defined_int(&sizeresult); |
| 655 |
if (parser_accept_comma()) |
| 656 |
ALU_any_int(&fill); |
| 657 |
} |
| 658 |
|
| 659 |
while (sizeresult.val.intval--) |
| 660 |
output_8(fill); |
| 661 |
return ENSURE_EOS; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
static enum eos po_skip(void) |
| 668 |
{ |
| 669 |
struct number amount; |
| 670 |
|
| 671 |
ALU_defined_int(&amount); |
| 672 |
if (amount.val.intval < 0) |
| 673 |
throw_serious_error(exception_negative_size); |
| 674 |
else |
| 675 |
output_skip(amount.val.intval); |
| 676 |
return ENSURE_EOS; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
static enum eos po_align(void) |
| 686 |
{ |
| 687 |
struct number arg; |
| 688 |
intval_t andresult, |
| 689 |
equalresult, |
| 690 |
fill = cpu_current_type->default_align_value; |
| 691 |
struct number pc; |
| 692 |
|
| 693 |
if (line_uses_keyword_arguments()) { |
| 694 |
|
| 695 |
|
| 696 |
BUG("KeywordArgsNotYet", 2); |
| 697 |
} else { |
| 698 |
|
| 699 |
ALU_defined_int(&arg); |
| 700 |
andresult = arg.val.intval; |
| 701 |
if (parser_expect(',')) { |
| 702 |
|
| 703 |
} |
| 704 |
ALU_defined_int(&arg); |
| 705 |
equalresult = arg.val.intval; |
| 706 |
if (parser_accept_comma()) |
| 707 |
ALU_any_int(&fill); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
programcounter_read_pc(&pc); |
| 712 |
if (pc.ntype == NUMTYPE_UNDEFINED) { |
| 713 |
throw_error(exception_pc_undefined); |
| 714 |
return SKIP_REMAINDER; |
| 715 |
} |
| 716 |
|
| 717 |
while ((pc.val.intval++ & andresult) != equalresult) |
| 718 |
output_8(fill); |
| 719 |
return ENSURE_EOS; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
static void old_offset_assembly(void) |
| 725 |
{ |
| 726 |
if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) { |
| 727 |
|
| 728 |
throw_error("\"!pseudopc/!realpc\" are obsolete; use \"!pseudopc {}\" instead."); |
| 729 |
} else if (config.dialect >= V0_86__DEPRECATE_REALPC) { |
| 730 |
|
| 731 |
throw_finalpass_warning("\"!pseudopc/!realpc\" are deprecated; use \"!pseudopc {}\" instead."); |
| 732 |
} else { |
| 733 |
|
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
static enum eos po_pseudopc(void) |
| 740 |
{ |
| 741 |
struct number new_pc; |
| 742 |
|
| 743 |
|
| 744 |
ALU_defined_int(&new_pc); |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
if (new_pc.val.intval < 0) { |
| 765 |
throw_error("Program counter cannot be negative."); |
| 766 |
new_pc.val.intval = cpu_current_type->dummy_pc; |
| 767 |
} |
| 768 |
pseudopc_start(&new_pc); |
| 769 |
|
| 770 |
if (parse_optional_block()) { |
| 771 |
|
| 772 |
if (pseudopc_isactive()) { |
| 773 |
pseudopc_end(); |
| 774 |
} else { |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
} |
| 784 |
} else { |
| 785 |
old_offset_assembly(); |
| 786 |
} |
| 787 |
return ENSURE_EOS; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
static enum eos po_realpc(void) |
| 793 |
{ |
| 794 |
old_offset_assembly(); |
| 795 |
end_all_pseudopc(); |
| 796 |
return ENSURE_EOS; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
static enum eos po_cpu(void) |
| 803 |
{ |
| 804 |
const struct cpu_type *cpu_buffer = cpu_current_type; |
| 805 |
const struct cpu_type *new_cpu_type; |
| 806 |
|
| 807 |
if (parser_read_and_lower_keyword()) { |
| 808 |
new_cpu_type = cputype_find(); |
| 809 |
if (new_cpu_type) |
| 810 |
cpu_current_type = new_cpu_type; |
| 811 |
else |
| 812 |
throw_error("Unknown processor."); |
| 813 |
} |
| 814 |
|
| 815 |
if (parse_optional_block()) |
| 816 |
cpu_current_type = cpu_buffer; |
| 817 |
return ENSURE_EOS; |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
static enum eos set_register_length(boolean *var, boolean make_long) |
| 824 |
{ |
| 825 |
boolean long_before = *var; |
| 826 |
|
| 827 |
|
| 828 |
vcpu_check_and_set_reg_length(var, make_long); |
| 829 |
|
| 830 |
if (parse_optional_block()) |
| 831 |
vcpu_check_and_set_reg_length(var, long_before); |
| 832 |
return ENSURE_EOS; |
| 833 |
} |
| 834 |
|
| 835 |
static enum eos po_al(void) |
| 836 |
{ |
| 837 |
return set_register_length(&cpu_a_is_long, TRUE); |
| 838 |
} |
| 839 |
|
| 840 |
static enum eos po_as(void) |
| 841 |
{ |
| 842 |
return set_register_length(&cpu_a_is_long, FALSE); |
| 843 |
} |
| 844 |
|
| 845 |
static enum eos po_rl(void) |
| 846 |
{ |
| 847 |
return set_register_length(&cpu_xy_are_long, TRUE); |
| 848 |
} |
| 849 |
|
| 850 |
static enum eos po_rs(void) |
| 851 |
{ |
| 852 |
return set_register_length(&cpu_xy_are_long, FALSE); |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
#if 0 |
| 857 |
|
| 858 |
static enum eos po_enum(void) |
| 859 |
{ |
| 860 |
struct number step; |
| 861 |
|
| 862 |
step.val.intval = 1; |
| 863 |
ALU_defined_int(&step); |
| 864 |
throw_serious_error("Not yet"); |
| 865 |
return ENSURE_EOS; |
| 866 |
} |
| 867 |
#endif |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
static enum eos po_set(void) |
| 872 |
{ |
| 873 |
scope_t scope; |
| 874 |
int force_bit; |
| 875 |
|
| 876 |
if (input_read_scope_and_symbol_name(&scope)) |
| 877 |
return SKIP_REMAINDER; |
| 878 |
|
| 879 |
force_bit = parser_get_force_bit(); |
| 880 |
if (!parser_expect('=')) |
| 881 |
return SKIP_REMAINDER; |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
parse_assignment(scope, force_bit, POWER_CHANGE_VALUE | POWER_CHANGE_OBJTYPE); |
| 890 |
return ENSURE_EOS; |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
static enum eos po_zone(void) |
| 897 |
{ |
| 898 |
struct section entry_values; |
| 899 |
char *new_title; |
| 900 |
int allocated; |
| 901 |
|
| 902 |
|
| 903 |
entry_values = *section_now; |
| 904 |
|
| 905 |
new_title = s_untitled; |
| 906 |
allocated = FALSE; |
| 907 |
|
| 908 |
|
| 909 |
if (BYTE_CONTINUES_KEYWORD(GotByte)) { |
| 910 |
|
| 911 |
|
| 912 |
parser_read_keyword(); |
| 913 |
new_title = dynabuf_get_copy(GlobalDynaBuf); |
| 914 |
allocated = TRUE; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
section_new(section_now, "Subzone", new_title, allocated); |
| 919 |
if (parse_optional_block()) { |
| 920 |
|
| 921 |
section_finalize(section_now); |
| 922 |
*section_now = entry_values; |
| 923 |
} else { |
| 924 |
|
| 925 |
section_finalize(&entry_values); |
| 926 |
section_now->type = "Zone"; |
| 927 |
} |
| 928 |
return ENSURE_EOS; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
static enum eos po_subzone(void) |
| 933 |
{ |
| 934 |
if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) { |
| 935 |
throw_error("\"!subzone {}\" is obsolete; use \"!zone {}\" instead."); |
| 936 |
} else { |
| 937 |
throw_finalpass_warning("\"!subzone {}\" is deprecated; use \"!zone {}\" instead."); |
| 938 |
} |
| 939 |
|
| 940 |
return po_zone(); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
static enum eos po_source(void) |
| 945 |
{ |
| 946 |
struct filespecflags flags; |
| 947 |
FILE *stream; |
| 948 |
const char *eternal_plat_filename; |
| 949 |
|
| 950 |
|
| 951 |
if (input_read_input_filename(&flags)) |
| 952 |
return SKIP_REMAINDER; |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
if (--sanity.source_recursions_left < 0) |
| 957 |
throw_serious_error("Too deeply nested. Recursive \"!source\"?"); |
| 958 |
|
| 959 |
stream = includepaths_open_ro(&flags); |
| 960 |
if (stream) { |
| 961 |
eternal_plat_filename = dynabuf_get_copy(GlobalDynaBuf); |
| 962 |
parse_source_code_file(stream, eternal_plat_filename); |
| 963 |
fclose(stream); |
| 964 |
} |
| 965 |
|
| 966 |
++sanity.source_recursions_left; |
| 967 |
return ENSURE_EOS; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
enum ifmode { |
| 972 |
IFMODE_IF, |
| 973 |
IFMODE_IFDEF, |
| 974 |
IFMODE_IFNDEF, |
| 975 |
IFMODE_ELSE |
| 976 |
}; |
| 977 |
|
| 978 |
static enum eos ifelse(enum ifmode mode) |
| 979 |
{ |
| 980 |
boolean skip_the_rest = FALSE; |
| 981 |
boolean condition_met; |
| 982 |
struct number ifresult; |
| 983 |
|
| 984 |
for (;;) { |
| 985 |
|
| 986 |
switch (mode) { |
| 987 |
case IFMODE_IF: |
| 988 |
if (skip_the_rest) { |
| 989 |
|
| 990 |
|
| 991 |
input_read_statement(CHAR_SOB); |
| 992 |
|
| 993 |
} else { |
| 994 |
|
| 995 |
|
| 996 |
ALU_defined_int(&ifresult); |
| 997 |
condition_met = !!ifresult.val.intval; |
| 998 |
} |
| 999 |
if (GotByte != CHAR_SOB) |
| 1000 |
throw_serious_error(exception_no_left_brace); |
| 1001 |
break; |
| 1002 |
case IFMODE_IFDEF: |
| 1003 |
condition_met = check_ifdef_condition(); |
| 1004 |
break; |
| 1005 |
case IFMODE_IFNDEF: |
| 1006 |
condition_met = !check_ifdef_condition(); |
| 1007 |
break; |
| 1008 |
case IFMODE_ELSE: |
| 1009 |
condition_met = TRUE; |
| 1010 |
break; |
| 1011 |
default: |
| 1012 |
BUG("IllegalIfMode", mode); |
| 1013 |
condition_met = TRUE; |
| 1014 |
} |
| 1015 |
SKIPSPACE(); |
| 1016 |
|
| 1017 |
if (condition_met && !skip_the_rest) { |
| 1018 |
skip_the_rest = TRUE; |
| 1019 |
if (GotByte == CHAR_SOB) { |
| 1020 |
parse_until_eob_or_eof(); |
| 1021 |
|
| 1022 |
if (GotByte != CHAR_EOB) |
| 1023 |
throw_serious_error(exception_no_right_brace); |
| 1024 |
} else { |
| 1025 |
return PARSE_REMAINDER; |
| 1026 |
} |
| 1027 |
} else { |
| 1028 |
if (GotByte == CHAR_SOB) { |
| 1029 |
input_block_skip(); |
| 1030 |
} else { |
| 1031 |
return SKIP_REMAINDER; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
NEXTANDSKIPSPACE(); |
| 1036 |
|
| 1037 |
if (mode == IFMODE_ELSE) { |
| 1038 |
|
| 1039 |
if (GotByte != CHAR_EOS) |
| 1040 |
throw_error("Expected end-of-statement after ELSE block."); |
| 1041 |
return SKIP_REMAINDER; |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
if (GotByte == CHAR_EOS) |
| 1046 |
return AT_EOS_ANYWAY; |
| 1047 |
|
| 1048 |
|
| 1049 |
if (parser_read_and_lower_keyword() == 0) |
| 1050 |
return SKIP_REMAINDER; |
| 1051 |
|
| 1052 |
|
| 1053 |
if (strcmp(GlobalDynaBuf->buffer, "elif") == 0) { |
| 1054 |
mode = IFMODE_IF; |
| 1055 |
continue; |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
if (strcmp(GlobalDynaBuf->buffer, "else")) { |
| 1060 |
throw_error("Expected end-of-statement or ELIF/ELSE keyword after '}'."); |
| 1061 |
return SKIP_REMAINDER; |
| 1062 |
} |
| 1063 |
|
| 1064 |
SKIPSPACE(); |
| 1065 |
if (GotByte == CHAR_SOB) { |
| 1066 |
|
| 1067 |
mode = IFMODE_ELSE; |
| 1068 |
continue; |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
if (parser_read_and_lower_keyword() == 0) |
| 1073 |
return SKIP_REMAINDER; |
| 1074 |
|
| 1075 |
|
| 1076 |
if (strcmp(GlobalDynaBuf->buffer, "if") == 0) { |
| 1077 |
mode = IFMODE_IF; |
| 1078 |
} else if (strcmp(GlobalDynaBuf->buffer, "ifdef") == 0) { |
| 1079 |
mode = IFMODE_IFDEF; |
| 1080 |
} else if (strcmp(GlobalDynaBuf->buffer, "ifndef") == 0) { |
| 1081 |
mode = IFMODE_IFNDEF; |
| 1082 |
} else { |
| 1083 |
throw_error("Expected '{' or IF/IFDEF/IFNDEF keyword after ELSE keyword."); |
| 1084 |
return SKIP_REMAINDER; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
static enum eos po_if(void) |
| 1091 |
{ |
| 1092 |
return ifelse(IFMODE_IF); |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
static enum eos po_ifdef(void) |
| 1098 |
{ |
| 1099 |
return ifelse(IFMODE_IFDEF); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
static enum eos po_ifndef(void) |
| 1105 |
{ |
| 1106 |
return ifelse(IFMODE_IFNDEF); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
static enum eos po_for(void) |
| 1115 |
{ |
| 1116 |
scope_t scope; |
| 1117 |
bits force_bit; |
| 1118 |
struct for_loop loop; |
| 1119 |
struct number intresult; |
| 1120 |
|
| 1121 |
if (input_read_scope_and_symbol_name(&scope)) |
| 1122 |
return SKIP_REMAINDER; |
| 1123 |
|
| 1124 |
|
| 1125 |
force_bit = parser_get_force_bit(); |
| 1126 |
loop.symbol = symbol_find(scope); |
| 1127 |
if (parser_accept_comma()) { |
| 1128 |
|
| 1129 |
loop.u.counter.force_bit = force_bit; |
| 1130 |
ALU_defined_int(&intresult); |
| 1131 |
loop.u.counter.addr_refs = intresult.addr_refs; |
| 1132 |
if (parser_accept_comma()) { |
| 1133 |
|
| 1134 |
loop.algorithm = FORALGO_NEWCOUNT; |
| 1135 |
if (config.dialect >= V0_94_12__NEW_FOR_SYNTAX) { |
| 1136 |
|
| 1137 |
} else { |
| 1138 |
|
| 1139 |
throw_finalpass_warning("Found new \"!for\" syntax."); |
| 1140 |
} |
| 1141 |
loop.u.counter.first = intresult.val.intval; |
| 1142 |
ALU_defined_int(&intresult); |
| 1143 |
|
| 1144 |
if (config.warn_on_type_mismatch |
| 1145 |
&& (intresult.addr_refs != loop.u.counter.addr_refs)) { |
| 1146 |
throw_finalpass_warning("Wrong type for loop's END value - must match type of START value."); |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (loop.u.counter.first <= intresult.val.intval) { |
| 1150 |
|
| 1151 |
loop.iterations_left = 1 + intresult.val.intval - loop.u.counter.first; |
| 1152 |
loop.u.counter.increment = 1; |
| 1153 |
} else { |
| 1154 |
|
| 1155 |
loop.iterations_left = 1 + loop.u.counter.first - intresult.val.intval; |
| 1156 |
loop.u.counter.increment = -1; |
| 1157 |
} |
| 1158 |
} else { |
| 1159 |
|
| 1160 |
loop.algorithm = FORALGO_OLDCOUNT; |
| 1161 |
if (config.dialect >= V0_94_12__NEW_FOR_SYNTAX) { |
| 1162 |
|
| 1163 |
throw_finalpass_warning("Found old \"!for\" syntax."); |
| 1164 |
} |
| 1165 |
if (intresult.val.intval < 0) { |
| 1166 |
throw_serious_error("Loop count is negative."); |
| 1167 |
} |
| 1168 |
|
| 1169 |
loop.u.counter.first = 1; |
| 1170 |
loop.iterations_left = intresult.val.intval; |
| 1171 |
loop.u.counter.increment = 1; |
| 1172 |
} |
| 1173 |
} else { |
| 1174 |
|
| 1175 |
loop.algorithm = FORALGO_ITERATE; |
| 1176 |
|
| 1177 |
if ((GotByte != 'i') && (GotByte != 'I')) { |
| 1178 |
throw_error(exception_want_IN_or_comma); |
| 1179 |
return SKIP_REMAINDER; |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
parser_read_and_lower_keyword(); |
| 1190 |
if (strcmp(GlobalDynaBuf->buffer, "in") != 0) { |
| 1191 |
throw_error(exception_want_IN_or_comma); |
| 1192 |
return SKIP_REMAINDER; |
| 1193 |
} |
| 1194 |
if (force_bit) { |
| 1195 |
throw_error("Force bits can only be given to counters, not when iterating over string/list contents."); |
| 1196 |
return SKIP_REMAINDER; |
| 1197 |
} |
| 1198 |
ALU_any_result(&loop.u.iter.obj); |
| 1199 |
loop.iterations_left = loop.u.iter.obj.type->length(&loop.u.iter.obj); |
| 1200 |
if (loop.iterations_left < 0) { |
| 1201 |
throw_error("Given object is not iterable."); |
| 1202 |
return SKIP_REMAINDER; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
if (GotByte != CHAR_SOB) |
| 1207 |
throw_serious_error(exception_no_left_brace); |
| 1208 |
|
| 1209 |
input_block_getcopy(&loop.block); |
| 1210 |
flow_forloop(&loop); |
| 1211 |
free(loop.block.body); |
| 1212 |
|
| 1213 |
|
| 1214 |
GetByte(); |
| 1215 |
return ENSURE_EOS; |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
static enum eos po_do(void) |
| 1221 |
{ |
| 1222 |
struct do_while loop; |
| 1223 |
|
| 1224 |
|
| 1225 |
SKIPSPACE(); |
| 1226 |
flow_store_doloop_condition(&loop.head_cond, CHAR_SOB); |
| 1227 |
if (GotByte != CHAR_SOB) |
| 1228 |
throw_serious_error(exception_no_left_brace); |
| 1229 |
|
| 1230 |
input_block_getcopy(&loop.block); |
| 1231 |
|
| 1232 |
NEXTANDSKIPSPACE(); |
| 1233 |
|
| 1234 |
flow_store_doloop_condition(&loop.tail_cond, CHAR_EOS); |
| 1235 |
|
| 1236 |
flow_do_while(&loop); |
| 1237 |
|
| 1238 |
free(loop.tail_cond.block.body); |
| 1239 |
free(loop.block.body); |
| 1240 |
free(loop.head_cond.block.body); |
| 1241 |
return AT_EOS_ANYWAY; |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
static enum eos po_while(void) |
| 1247 |
{ |
| 1248 |
struct do_while loop; |
| 1249 |
|
| 1250 |
|
| 1251 |
SKIPSPACE(); |
| 1252 |
flow_store_while_condition(&loop.head_cond); |
| 1253 |
if (GotByte != CHAR_SOB) |
| 1254 |
throw_serious_error(exception_no_left_brace); |
| 1255 |
|
| 1256 |
input_block_getcopy(&loop.block); |
| 1257 |
|
| 1258 |
loop.tail_cond.block.body = NULL; |
| 1259 |
flow_do_while(&loop); |
| 1260 |
|
| 1261 |
free(loop.block.body); |
| 1262 |
free(loop.head_cond.block.body); |
| 1263 |
GetByte(); |
| 1264 |
return ENSURE_EOS; |
| 1265 |
} |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
static enum eos po_macro(void) |
| 1270 |
{ |
| 1271 |
|
| 1272 |
if (pass.number == 1) { |
| 1273 |
macro_parse_definition(); |
| 1274 |
} else { |
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
while (GotByte != CHAR_SOB) |
| 1280 |
GetByte(); |
| 1281 |
input_block_skip(); |
| 1282 |
} |
| 1283 |
GetByte(); |
| 1284 |
return ENSURE_EOS; |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
static enum eos po_address(void) |
| 1346 |
{ |
| 1347 |
boolean buf; |
| 1348 |
|
| 1349 |
buf = parser_change_addr_block_flag(TRUE); |
| 1350 |
if (parse_optional_block()) { |
| 1351 |
|
| 1352 |
parser_change_addr_block_flag(buf); |
| 1353 |
return ENSURE_EOS; |
| 1354 |
} else { |
| 1355 |
|
| 1356 |
parser_change_addr_block_flag(buf); |
| 1357 |
parser_set_addr_prefix(); |
| 1358 |
return PARSE_REMAINDER; |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
static enum eos po_nowarn(void) |
| 1367 |
{ |
| 1368 |
boolean buf; |
| 1369 |
|
| 1370 |
buf = parser_change_nowarn_block_flag(TRUE); |
| 1371 |
if (parse_optional_block()) { |
| 1372 |
|
| 1373 |
parser_change_nowarn_block_flag(buf); |
| 1374 |
return ENSURE_EOS; |
| 1375 |
} else { |
| 1376 |
|
| 1377 |
parser_change_nowarn_block_flag(buf); |
| 1378 |
parser_set_nowarn_prefix(); |
| 1379 |
return PARSE_REMAINDER; |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
static enum eos po_break(void) |
| 1386 |
{ |
| 1387 |
boolean allowed; |
| 1388 |
|
| 1389 |
allowed = parser_allow_break_cont(FALSE); |
| 1390 |
parser_allow_break_cont(allowed); |
| 1391 |
if (allowed) |
| 1392 |
parser_set_shortcut(SHORTCUT_BREAK); |
| 1393 |
else |
| 1394 |
throw_error("!break not within a loop."); |
| 1395 |
return ENSURE_EOS; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
static enum eos po_continue(void) |
| 1400 |
{ |
| 1401 |
boolean allowed; |
| 1402 |
|
| 1403 |
allowed = parser_allow_break_cont(FALSE); |
| 1404 |
parser_allow_break_cont(allowed); |
| 1405 |
if (allowed) |
| 1406 |
parser_set_shortcut(SHORTCUT_CONT); |
| 1407 |
else |
| 1408 |
throw_error("!continue not within a loop."); |
| 1409 |
return ENSURE_EOS; |
| 1410 |
} |
| 1411 |
|
| 1412 |
|
| 1413 |
static enum eos po_return(void) |
| 1414 |
{ |
| 1415 |
boolean allowed; |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
allowed = parser_allow_return(FALSE); |
| 1421 |
parser_allow_return(allowed); |
| 1422 |
if (allowed) |
| 1423 |
parser_set_shortcut(SHORTCUT_RETURN); |
| 1424 |
else |
| 1425 |
throw_error("!return not within a macro."); |
| 1426 |
return ENSURE_EOS; |
| 1427 |
} |
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
static STRUCT_DYNABUF_REF(user_message, 80); |
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
static enum eos throw_src_string(enum debuglevel level, const char prefix[]) |
| 1436 |
{ |
| 1437 |
struct object object; |
| 1438 |
|
| 1439 |
dynabuf_clear(user_message); |
| 1440 |
dynabuf_add_string(user_message, prefix); |
| 1441 |
|
| 1442 |
if (config.dialect >= V0_97__BACKSLASH_ESCAPING) { |
| 1443 |
|
| 1444 |
do { |
| 1445 |
ALU_any_result(&object); |
| 1446 |
object.type->print(&object, user_message); |
| 1447 |
} while (parser_accept_comma()); |
| 1448 |
} else { |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
do { |
| 1454 |
if (GotByte == '"') { |
| 1455 |
|
| 1456 |
if (input_read_string_literal('"')) |
| 1457 |
break; |
| 1458 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 1459 |
dynabuf_add_string(user_message, GLOBALDYNABUF_CURRENT); |
| 1460 |
} else { |
| 1461 |
|
| 1462 |
ALU_any_result(&object); |
| 1463 |
object.type->print(&object, user_message); |
| 1464 |
} |
| 1465 |
} while (parser_accept_comma()); |
| 1466 |
} |
| 1467 |
dynabuf_append(user_message, '\0'); |
| 1468 |
throw_message(level, user_message->buffer, NULL); |
| 1469 |
return ENSURE_EOS; |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
static enum eos po_debug(void) |
| 1474 |
{ |
| 1475 |
struct number debuglevel; |
| 1476 |
|
| 1477 |
ALU_defined_int(&debuglevel); |
| 1478 |
if (!parser_expect(',')) |
| 1479 |
return SKIP_REMAINDER; |
| 1480 |
|
| 1481 |
|
| 1482 |
if (debuglevel.val.intval > config.debuglevel) |
| 1483 |
return SKIP_REMAINDER; |
| 1484 |
|
| 1485 |
return throw_src_string(debuglevel.val.intval, "!debug: "); |
| 1486 |
} |
| 1487 |
|
| 1488 |
|
| 1489 |
static enum eos po_info(void) |
| 1490 |
{ |
| 1491 |
return throw_src_string(DEBUGLEVEL_INFO, "!info: "); |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
static enum eos po_warn(void) |
| 1496 |
{ |
| 1497 |
return throw_src_string(DEBUGLEVEL_WARNING, "!warn: "); |
| 1498 |
} |
| 1499 |
|
| 1500 |
|
| 1501 |
static enum eos po_error(void) |
| 1502 |
{ |
| 1503 |
return throw_src_string(DEBUGLEVEL_ERROR, "!error: "); |
| 1504 |
} |
| 1505 |
|
| 1506 |
|
| 1507 |
static enum eos po_serious(void) |
| 1508 |
{ |
| 1509 |
return throw_src_string(DEBUGLEVEL_SERIOUS, "!serious: "); |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
static enum eos po_endoffile(void) |
| 1516 |
{ |
| 1517 |
parser_ensure_EOS(); |
| 1518 |
input_force_eof(); |
| 1519 |
return AT_EOS_ANYWAY; |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
static struct ronode pseudo_opcode_tree[] = { |
| 1524 |
PREDEF_START, |
| 1525 |
PREDEFNODE("initmem", po_initmem), |
| 1526 |
PREDEFNODE("to", po_to), |
| 1527 |
PREDEFNODE("sl", po_symbollist), |
| 1528 |
PREDEFNODE("symbollist", po_symbollist), |
| 1529 |
PREDEFNODE("outfilestart", po_outfilestart), |
| 1530 |
PREDEFNODE("outfilestop", po_outfilelimit), |
| 1531 |
PREDEFNODE("outfilelimit", po_outfilelimit), |
| 1532 |
PREDEFNODE("xor", po_xor), |
| 1533 |
PREDEFNODE("by", po_byte), |
| 1534 |
PREDEFNODE("byt", po_byte), |
| 1535 |
PREDEFNODE("byte", po_byte), |
| 1536 |
PREDEFNODE("8", po_byte), |
| 1537 |
PREDEFNODE("08", po_byte), |
| 1538 |
PREDEFNODE("wo", po_16), |
| 1539 |
PREDEFNODE("word", po_16), |
| 1540 |
PREDEFNODE("16", po_16), |
| 1541 |
PREDEFNODE("be16", po_be16), |
| 1542 |
PREDEFNODE("le16", po_le16), |
| 1543 |
PREDEFNODE("24", po_24), |
| 1544 |
PREDEFNODE("be24", po_be24), |
| 1545 |
PREDEFNODE("le24", po_le24), |
| 1546 |
PREDEFNODE("32", po_32), |
| 1547 |
PREDEFNODE("be32", po_be32), |
| 1548 |
PREDEFNODE("le32", po_le32), |
| 1549 |
PREDEFNODE("h", po_hex), |
| 1550 |
PREDEFNODE("hex", po_hex), |
| 1551 |
PREDEFNODE("cbm", po_cbm), |
| 1552 |
PREDEFNODE("ct", po_convtab), |
| 1553 |
PREDEFNODE("convtab", po_convtab), |
| 1554 |
PREDEFNODE("tx", po_text), |
| 1555 |
PREDEFNODE("txt", po_text), |
| 1556 |
PREDEFNODE("text", po_text), |
| 1557 |
PREDEFNODE("raw", po_raw), |
| 1558 |
PREDEFNODE("pet", po_pet), |
| 1559 |
PREDEFNODE("scr", po_scr), |
| 1560 |
PREDEFNODE("scrxor", po_scrxor), |
| 1561 |
PREDEFNODE("bin", po_binary), |
| 1562 |
PREDEFNODE("binary", po_binary), |
| 1563 |
PREDEFNODE("fi", po_fill), |
| 1564 |
PREDEFNODE("fill", po_fill), |
| 1565 |
PREDEFNODE("skip", po_skip), |
| 1566 |
PREDEFNODE("align", po_align), |
| 1567 |
PREDEFNODE("pseudopc", po_pseudopc), |
| 1568 |
PREDEFNODE("realpc", po_realpc), |
| 1569 |
PREDEFNODE("cpu", po_cpu), |
| 1570 |
PREDEFNODE("al", po_al), |
| 1571 |
PREDEFNODE("as", po_as), |
| 1572 |
PREDEFNODE("rl", po_rl), |
| 1573 |
PREDEFNODE("rs", po_rs), |
| 1574 |
|
| 1575 |
PREDEFNODE("set", po_set), |
| 1576 |
PREDEFNODE("zn", po_zone), |
| 1577 |
PREDEFNODE("zone", po_zone), |
| 1578 |
PREDEFNODE("sz", po_subzone), |
| 1579 |
PREDEFNODE("subzone", po_subzone), |
| 1580 |
PREDEFNODE("src", po_source), |
| 1581 |
PREDEFNODE("source", po_source), |
| 1582 |
PREDEFNODE("if", po_if), |
| 1583 |
PREDEFNODE("ifdef", po_ifdef), |
| 1584 |
PREDEFNODE("ifndef", po_ifndef), |
| 1585 |
PREDEFNODE("for", po_for), |
| 1586 |
PREDEFNODE("do", po_do), |
| 1587 |
PREDEFNODE("while", po_while), |
| 1588 |
PREDEFNODE("macro", po_macro), |
| 1589 |
|
| 1590 |
|
| 1591 |
PREDEFNODE("addr", po_address), |
| 1592 |
PREDEFNODE("address", po_address), |
| 1593 |
PREDEFNODE("nowarn", po_nowarn), |
| 1594 |
PREDEFNODE("break", po_break), |
| 1595 |
PREDEFNODE("continue", po_continue), |
| 1596 |
PREDEFNODE("return", po_return), |
| 1597 |
PREDEFNODE("debug", po_debug), |
| 1598 |
PREDEFNODE("info", po_info), |
| 1599 |
PREDEFNODE("warn", po_warn), |
| 1600 |
PREDEFNODE("error", po_error), |
| 1601 |
PREDEFNODE("serious", po_serious), |
| 1602 |
PREDEFNODE("eof", po_endoffile), |
| 1603 |
PREDEF_END("endoffile", po_endoffile), |
| 1604 |
|
| 1605 |
}; |
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
void pseudoopcode_parse(void) |
| 1610 |
{ |
| 1611 |
void *node_body; |
| 1612 |
enum eos (*fn)(void), |
| 1613 |
then = SKIP_REMAINDER; |
| 1614 |
|
| 1615 |
GetByte(); |
| 1616 |
|
| 1617 |
if (parser_read_and_lower_keyword()) { |
| 1618 |
|
| 1619 |
if ((tree_easy_scan(pseudo_opcode_tree, &node_body, GlobalDynaBuf)) |
| 1620 |
&& node_body) { |
| 1621 |
fn = (enum eos (*)(void)) node_body; |
| 1622 |
SKIPSPACE(); |
| 1623 |
|
| 1624 |
then = fn(); |
| 1625 |
} else { |
| 1626 |
throw_error("Unknown pseudo opcode."); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
if (then == SKIP_REMAINDER) |
| 1630 |
parser_skip_remainder(); |
| 1631 |
else if (then == ENSURE_EOS) |
| 1632 |
parser_ensure_EOS(); |
| 1633 |
|
| 1634 |
|
| 1635 |
} |
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
void notreallypo_setpc(void) |
| 1641 |
{ |
| 1642 |
bits segment_flags = 0; |
| 1643 |
boolean do_outfilestart = FALSE; |
| 1644 |
struct number intresult; |
| 1645 |
|
| 1646 |
|
| 1647 |
NEXTANDSKIPSPACE(); |
| 1648 |
if (!parser_expect('=')) |
| 1649 |
goto fail; |
| 1650 |
|
| 1651 |
ALU_defined_int(&intresult); |
| 1652 |
|
| 1653 |
while (parser_accept_comma()) { |
| 1654 |
|
| 1655 |
if (parser_read_and_lower_keyword() == 0) |
| 1656 |
goto fail; |
| 1657 |
|
| 1658 |
if (strcmp(GlobalDynaBuf->buffer, "overlay") == 0) { |
| 1659 |
segment_flags |= SEGMENT_FLAG_OVERLAY; |
| 1660 |
} else if (strcmp(GlobalDynaBuf->buffer, "invisible") == 0) { |
| 1661 |
segment_flags |= SEGMENT_FLAG_INVISIBLE; |
| 1662 |
} else if (strcmp(GlobalDynaBuf->buffer, "outfilestart") == 0) { |
| 1663 |
do_outfilestart = TRUE; |
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
} else { |
| 1675 |
throw_error("Unknown \"*=\" segment modifier."); |
| 1676 |
goto fail; |
| 1677 |
} |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
if (pseudopc_isactive()) { |
| 1683 |
if (config.dialect >= V0_94_8__DISABLED_OBSOLETE) { |
| 1684 |
|
| 1685 |
|
| 1686 |
} else if (config.dialect >= V0_93__SHORTER_SETPC_WARNING) { |
| 1687 |
throw_warning("Offset assembly still active at end of segment."); |
| 1688 |
end_all_pseudopc(); |
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
} else { |
| 1694 |
|
| 1695 |
throw_warning("Offset assembly still active at end of segment. Switched it off."); |
| 1696 |
end_all_pseudopc(); |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
if (intresult.val.intval < 0) { |
| 1701 |
throw_error("Program counter cannot be negative."); |
| 1702 |
intresult.val.intval = cpu_current_type->dummy_pc; |
| 1703 |
} |
| 1704 |
programcounter_set(intresult.val.intval, segment_flags); |
| 1705 |
|
| 1706 |
|
| 1707 |
if (do_outfilestart) |
| 1708 |
po_outfilestart(); |
| 1709 |
|
| 1710 |
parser_ensure_EOS(); |
| 1711 |
return; |
| 1712 |
|
| 1713 |
fail: |
| 1714 |
parser_skip_remainder(); |
| 1715 |
} |