| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "output.h" |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include "cpu.h" |
| 16 |
#include "global.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
static void throwsegmentproblem(const char msg[]) |
| 21 |
{ |
| 22 |
if (config.strict_segments) |
| 23 |
countorthrow_value_error(msg); |
| 24 |
else |
| 25 |
throw_finalpass_warning(msg); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct segment { |
| 34 |
struct segment *next, |
| 35 |
*prev; |
| 36 |
intval_t start, |
| 37 |
length; |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
static void segmentlist_headinit(struct segment *list_head) |
| 42 |
{ |
| 43 |
list_head->next = list_head; |
| 44 |
list_head->prev = list_head; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
static void segmentlist_release(struct segment *list_head) |
| 49 |
{ |
| 50 |
struct segment *next, |
| 51 |
*tmp; |
| 52 |
|
| 53 |
next = list_head->next; |
| 54 |
while (next != list_head) { |
| 55 |
tmp = next; |
| 56 |
next = next->next; |
| 57 |
free(tmp); |
| 58 |
} |
| 59 |
|
| 60 |
segmentlist_headinit(list_head); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static void segmentlist_add(struct segment *list_head, intval_t start, intval_t length) |
| 69 |
{ |
| 70 |
struct segment *new_segment, |
| 71 |
*test_segment = list_head->next; |
| 72 |
|
| 73 |
|
| 74 |
new_segment = safe_malloc(sizeof(*new_segment)); |
| 75 |
new_segment->start = start; |
| 76 |
new_segment->length = length; |
| 77 |
|
| 78 |
list_head->start = start; |
| 79 |
list_head->length = length + 1; |
| 80 |
|
| 81 |
while ((test_segment->start < new_segment->start) |
| 82 |
|| ((test_segment->start == new_segment->start) && (test_segment->length < new_segment->length))) |
| 83 |
test_segment = test_segment->next; |
| 84 |
|
| 85 |
new_segment->next = test_segment; |
| 86 |
new_segment->prev = test_segment->prev; |
| 87 |
new_segment->next->prev = new_segment; |
| 88 |
new_segment->prev->next = new_segment; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
static void segmentlist_check(struct segment *list_head, intval_t new_pc) |
| 93 |
{ |
| 94 |
struct segment *test_segment = list_head->next; |
| 95 |
|
| 96 |
|
| 97 |
list_head->start = new_pc + 1; |
| 98 |
list_head->length = 1; |
| 99 |
|
| 100 |
while (test_segment->start <= new_pc) { |
| 101 |
if ((test_segment->start + test_segment->length) > new_pc) { |
| 102 |
|
| 103 |
throwsegmentproblem("Segment starts inside another one, overwriting it."); |
| 104 |
return; |
| 105 |
} |
| 106 |
test_segment = test_segment->next; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
static intval_t segmentlist_findlimit(struct segment *list_head, intval_t new_pc) |
| 113 |
{ |
| 114 |
struct segment *test_segment = list_head->next; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
list_head->start = new_pc + 1; |
| 120 |
while (test_segment->start <= new_pc) |
| 121 |
test_segment = test_segment->next; |
| 122 |
if (test_segment == list_head) |
| 123 |
return OUTBUF_MAXSIZE; |
| 124 |
else |
| 125 |
return test_segment->start; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
struct output { |
| 134 |
|
| 135 |
char *buffer; |
| 136 |
intval_t write_idx; |
| 137 |
intval_t lowest_written; |
| 138 |
intval_t highest_written; |
| 139 |
intval_t forced_start_idx; |
| 140 |
intval_t forced_limit_idx; |
| 141 |
struct { |
| 142 |
intval_t start; |
| 143 |
intval_t limit; |
| 144 |
bits flags; |
| 145 |
struct segment list_head; |
| 146 |
} segm; |
| 147 |
char xor; |
| 148 |
intval_t needed_bufsize; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
struct pseudopc { |
| 154 |
struct pseudopc *outer; |
| 155 |
intval_t offset; |
| 156 |
}; |
| 157 |
static struct pseudopc outermost_pseudopc_context; |
| 158 |
static struct pseudopc *pseudopc_current_context = &outermost_pseudopc_context; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static struct output default_output; |
| 163 |
static struct output *out = &default_output; |
| 164 |
static intval_t program_counter; |
| 165 |
static int statement_size; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static void report_binary(char value) |
| 175 |
{ |
| 176 |
if (report->bin_used == 0) |
| 177 |
report->bin_address = out->write_idx; |
| 178 |
if (report->bin_used < REPORT_BINBUFSIZE) |
| 179 |
report->bin_buf[report->bin_used++] = value; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static void breached_limit(int offending_offset) |
| 185 |
{ |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if (offending_offset >= OUTBUF_MAXSIZE) |
| 190 |
throw_serious_error("Reached memory limit."); |
| 191 |
throwsegmentproblem("Segment reached another one, overwriting it."); |
| 192 |
|
| 193 |
out->segm.limit = segmentlist_findlimit(&out->segm.list_head, offending_offset); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
void (*output_byte)(intval_t byte); |
| 199 |
|
| 200 |
|
| 201 |
#define PC_NOT_SET (program_counter == NO_VALUE_GIVEN) |
| 202 |
|
| 203 |
|
| 204 |
static void real_output(intval_t byte) |
| 205 |
{ |
| 206 |
|
| 207 |
|
| 208 |
if (out->write_idx >= out->segm.limit) |
| 209 |
breached_limit(out->write_idx); |
| 210 |
|
| 211 |
if (out->write_idx < out->lowest_written) |
| 212 |
out->lowest_written = out->write_idx; |
| 213 |
|
| 214 |
if (out->write_idx > out->highest_written) |
| 215 |
out->highest_written = out->write_idx; |
| 216 |
|
| 217 |
if (report->fd) |
| 218 |
report_binary(byte & 0xff); |
| 219 |
|
| 220 |
if (out->buffer) { |
| 221 |
if (out->write_idx >= out->needed_bufsize) { |
| 222 |
|
| 223 |
throw_serious_error("Output buffer overrun."); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
} |
| 229 |
out->buffer[out->write_idx] = (byte & 0xff) ^ out->xor; |
| 230 |
} |
| 231 |
|
| 232 |
out->write_idx++; |
| 233 |
++statement_size; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static void complain_and_use_dummy_pc(void) |
| 239 |
{ |
| 240 |
throw_error(exception_pc_undefined); |
| 241 |
programcounter_set(cpu_current_type->dummy_pc, 0); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
static void no_output(intval_t byte) |
| 246 |
{ |
| 247 |
complain_and_use_dummy_pc(); |
| 248 |
output_byte(byte); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
void output_skip(int size) |
| 255 |
{ |
| 256 |
if (size < 1) { |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
if (PC_NOT_SET) |
| 266 |
complain_and_use_dummy_pc(); |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
out->write_idx += size; |
| 275 |
statement_size += size; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
void outbuf_set_outfile_start(void) |
| 281 |
{ |
| 282 |
|
| 283 |
if (PC_NOT_SET) |
| 284 |
complain_and_use_dummy_pc(); |
| 285 |
out->forced_start_idx = out->write_idx; |
| 286 |
} |
| 287 |
void outbuf_set_outfile_limit(void) |
| 288 |
{ |
| 289 |
|
| 290 |
if (PC_NOT_SET) |
| 291 |
complain_and_use_dummy_pc(); |
| 292 |
out->forced_limit_idx = out->write_idx; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
void output_init(void) |
| 298 |
{ |
| 299 |
|
| 300 |
segmentlist_headinit(&out->segm.list_head); |
| 301 |
|
| 302 |
out->needed_bufsize = NO_VALUE_GIVEN; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
void output_passinit(void) |
| 307 |
{ |
| 308 |
|
| 309 |
if (pass.flags.generate_output) { |
| 310 |
|
| 311 |
|
| 312 |
if (out->needed_bufsize == NO_VALUE_GIVEN) { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
out->needed_bufsize = 16; |
| 317 |
} |
| 318 |
if (out->needed_bufsize > OUTBUF_MAXSIZE) { |
| 319 |
throw_serious_error("Output buffer size exceeds maximum."); |
| 320 |
} |
| 321 |
|
| 322 |
out->buffer = safe_malloc(out->needed_bufsize); |
| 323 |
|
| 324 |
if (config.mem_init_value == NO_VALUE_GIVEN) { |
| 325 |
memset(out->buffer, 0, out->needed_bufsize); |
| 326 |
} else { |
| 327 |
memset(out->buffer, config.mem_init_value & 0xff, out->needed_bufsize); |
| 328 |
} |
| 329 |
} else { |
| 330 |
out->buffer = NULL; |
| 331 |
} |
| 332 |
|
| 333 |
out->write_idx = NO_VALUE_GIVEN; |
| 334 |
|
| 335 |
out->lowest_written = OUTBUF_MAXSIZE; |
| 336 |
out->highest_written = -1; |
| 337 |
|
| 338 |
out->forced_start_idx = NO_VALUE_GIVEN; |
| 339 |
out->forced_limit_idx = NO_VALUE_GIVEN; |
| 340 |
|
| 341 |
out->segm.start = NO_VALUE_GIVEN; |
| 342 |
out->segm.limit = OUTBUF_MAXSIZE; |
| 343 |
out->segm.flags = 0; |
| 344 |
|
| 345 |
|
| 346 |
segmentlist_release(&out->segm.list_head); |
| 347 |
|
| 348 |
out->xor = 0; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
output_byte = no_output; |
| 354 |
|
| 355 |
|
| 356 |
program_counter = NO_VALUE_GIVEN; |
| 357 |
statement_size = 0; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
outermost_pseudopc_context.outer = NULL; |
| 362 |
outermost_pseudopc_context.offset = 0; |
| 363 |
|
| 364 |
pseudopc_current_context = &outermost_pseudopc_context; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
static void end_segment(void) |
| 371 |
{ |
| 372 |
intval_t amount; |
| 373 |
|
| 374 |
|
| 375 |
if (out->segm.start == NO_VALUE_GIVEN) |
| 376 |
return; |
| 377 |
|
| 378 |
|
| 379 |
if (out->segm.flags & SEGMENT_FLAG_INVISIBLE) |
| 380 |
return; |
| 381 |
|
| 382 |
|
| 383 |
amount = out->write_idx - out->segm.start; |
| 384 |
if (amount == 0) |
| 385 |
return; |
| 386 |
|
| 387 |
|
| 388 |
segmentlist_add(&out->segm.list_head, out->segm.start, amount); |
| 389 |
|
| 390 |
if (pass.flags.is_final_pass && (config.process_verbosity >= 2)) { |
| 391 |
|
| 392 |
printf("Segment size is %d (0x%04x) bytes (0x%04x - 0x%04x exclusive).\n", |
| 393 |
amount, amount, out->segm.start, out->write_idx); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
void output_endofpass(void) |
| 400 |
{ |
| 401 |
intval_t bufsize; |
| 402 |
|
| 403 |
|
| 404 |
end_segment(); |
| 405 |
|
| 406 |
|
| 407 |
if (out->highest_written >= out->lowest_written) { |
| 408 |
bufsize = out->highest_written + 1; |
| 409 |
} else { |
| 410 |
bufsize = NO_VALUE_GIVEN; |
| 411 |
} |
| 412 |
|
| 413 |
if (out->needed_bufsize != bufsize) |
| 414 |
++pass.counters.symbolchanges; |
| 415 |
out->needed_bufsize = bufsize; |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
static void start_segment(intval_t address_change, bits segment_flags) |
| 422 |
{ |
| 423 |
|
| 424 |
end_segment(); |
| 425 |
|
| 426 |
|
| 427 |
out->write_idx = out->write_idx + address_change; |
| 428 |
if (out->write_idx < 0) { |
| 429 |
throw_serious_error("Tried to write to negative address."); |
| 430 |
} else if (out->write_idx >= OUTBUF_MAXSIZE) { |
| 431 |
throw_serious_error("Reached memory limit."); |
| 432 |
} |
| 433 |
out->segm.start = out->write_idx; |
| 434 |
out->segm.flags = segment_flags; |
| 435 |
|
| 436 |
output_byte = real_output; |
| 437 |
|
| 438 |
if (!(segment_flags & SEGMENT_FLAG_OVERLAY)) |
| 439 |
segmentlist_check(&out->segm.list_head, out->segm.start); |
| 440 |
out->segm.limit = segmentlist_findlimit(&out->segm.list_head, out->segm.start); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
char output_get_xor(void) |
| 446 |
{ |
| 447 |
return out->xor; |
| 448 |
} |
| 449 |
void output_set_xor(char xor) |
| 450 |
{ |
| 451 |
out->xor = xor; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
void programcounter_set(intval_t new_pc, bits segment_flags) |
| 460 |
{ |
| 461 |
intval_t pc_change; |
| 462 |
|
| 463 |
pc_change = new_pc - program_counter; |
| 464 |
program_counter = new_pc; |
| 465 |
|
| 466 |
start_segment(pc_change, segment_flags); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
void programcounter_read_pc(struct number *target) |
| 472 |
{ |
| 473 |
|
| 474 |
if (PC_NOT_SET) { |
| 475 |
target->ntype = NUMTYPE_UNDEFINED; |
| 476 |
} else { |
| 477 |
target->ntype = NUMTYPE_INT; |
| 478 |
} |
| 479 |
target->flags = 0; |
| 480 |
target->val.intval = program_counter; |
| 481 |
target->addr_refs = 1; |
| 482 |
} |
| 483 |
|
| 484 |
void programcounter_read_asterisk(struct number *target) |
| 485 |
{ |
| 486 |
programcounter_read_pc(target); |
| 487 |
|
| 488 |
|
| 489 |
if (target->ntype != NUMTYPE_UNDEFINED) |
| 490 |
return; |
| 491 |
|
| 492 |
|
| 493 |
if (pass.flags.complain_about_undefined) |
| 494 |
throw_error("Symbol not defined (*)."); |
| 495 |
else |
| 496 |
++pass.counters.undefineds; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
int output_get_statement_size(void) |
| 506 |
{ |
| 507 |
return statement_size; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
void output_end_statement(void) |
| 513 |
{ |
| 514 |
program_counter += statement_size; |
| 515 |
statement_size = 0; |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
void output_get_result(const char **ptr, intval_t *size, intval_t *loadaddr) |
| 526 |
{ |
| 527 |
intval_t start, |
| 528 |
limit, |
| 529 |
amount; |
| 530 |
|
| 531 |
if (out->buffer == NULL) |
| 532 |
BUG("noOutBuf", 0); |
| 533 |
|
| 534 |
start = out->lowest_written; |
| 535 |
limit = out->highest_written + 1; |
| 536 |
|
| 537 |
if (out->forced_start_idx != NO_VALUE_GIVEN) |
| 538 |
start = out->forced_start_idx; |
| 539 |
if (out->forced_limit_idx != NO_VALUE_GIVEN) |
| 540 |
limit = out->forced_limit_idx; |
| 541 |
|
| 542 |
if (config.outfile_start != NO_VALUE_GIVEN) |
| 543 |
start = config.outfile_start; |
| 544 |
if (config.outfile_limit != NO_VALUE_GIVEN) |
| 545 |
limit = config.outfile_limit; |
| 546 |
|
| 547 |
if (limit <= start) { |
| 548 |
|
| 549 |
start = 0; |
| 550 |
amount = 0; |
| 551 |
|
| 552 |
|
| 553 |
} else { |
| 554 |
amount = limit - start; |
| 555 |
} |
| 556 |
|
| 557 |
*ptr = out->buffer + start; |
| 558 |
*size = amount; |
| 559 |
*loadaddr = start; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
void pseudopc_start(struct number *new_pc) |
| 567 |
{ |
| 568 |
struct pseudopc *new_context; |
| 569 |
|
| 570 |
|
| 571 |
if (PC_NOT_SET) |
| 572 |
complain_and_use_dummy_pc(); |
| 573 |
|
| 574 |
new_context = safe_malloc(sizeof(*new_context)); |
| 575 |
new_context->outer = pseudopc_current_context; |
| 576 |
new_context->offset = new_pc->val.intval - program_counter; |
| 577 |
pseudopc_current_context = new_context; |
| 578 |
program_counter = new_pc->val.intval; |
| 579 |
} |
| 580 |
|
| 581 |
void pseudopc_end(void) |
| 582 |
{ |
| 583 |
program_counter = program_counter - pseudopc_current_context->offset; |
| 584 |
pseudopc_current_context = pseudopc_current_context->outer; |
| 585 |
if (pseudopc_current_context == NULL) |
| 586 |
BUG("PseudoPCContext", 0); |
| 587 |
} |
| 588 |
|
| 589 |
void pseudopc_unpseudo(struct number *target, struct pseudopc *context, unsigned int levels) |
| 590 |
{ |
| 591 |
switch (target->ntype) { |
| 592 |
case NUMTYPE_UNDEFINED: |
| 593 |
return; |
| 594 |
|
| 595 |
|
| 596 |
case NUMTYPE_INT: |
| 597 |
break; |
| 598 |
case NUMTYPE_FLOAT: |
| 599 |
throw_error("Un-pseudopc operator '&' does not work on floats."); |
| 600 |
return; |
| 601 |
default: |
| 602 |
BUG("IllegalNumberType8", target->ntype); |
| 603 |
} |
| 604 |
if (context == NULL) { |
| 605 |
throw_error("Un-pseudopc operator '&' only works on addresses."); |
| 606 |
return; |
| 607 |
} |
| 608 |
while (levels--) { |
| 609 |
if (context == &outermost_pseudopc_context) { |
| 610 |
throw_error("Un-pseudopc operator '&' has no !pseudopc context."); |
| 611 |
return; |
| 612 |
} |
| 613 |
target->val.intval = target->val.intval - context->offset; |
| 614 |
context = context->outer; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
struct pseudopc *pseudopc_get_context(void) |
| 620 |
{ |
| 621 |
return pseudopc_current_context; |
| 622 |
} |
| 623 |
|
| 624 |
int pseudopc_isactive(void) |
| 625 |
{ |
| 626 |
return pseudopc_current_context != &outermost_pseudopc_context; |
| 627 |
} |