| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/tcgen.cpp,v 1.4 1999/07/11 00:46:58 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | tcgen.cpp - TADS 3 Compiler code generator support classes |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 05/09/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "t3std.h" |
| 27 | #include "os.h" |
| 28 | #include "tcglob.h" |
| 29 | #include "tcgen.h" |
| 30 | #include "vmerr.h" |
| 31 | #include "tcerrnum.h" |
| 32 | #include "tctok.h" |
| 33 | #include "tcprs.h" |
| 34 | #include "tcmain.h" |
| 35 | #include "vmfile.h" |
| 36 | #include "tctarg.h" |
| 37 | |
| 38 | |
| 39 | /* ------------------------------------------------------------------------ */ |
| 40 | /* |
| 41 | * Data/Code Stream Parser-Allocated Object |
| 42 | */ |
| 43 | |
| 44 | /* |
| 45 | * allocate via a parser memory allocator |
| 46 | */ |
| 47 | void *CTcCSPrsAllocObj::operator new(size_t siz, CTcPrsMem *allocator) |
| 48 | { |
| 49 | /* allocate via the allocator */ |
| 50 | return allocator->alloc(siz); |
| 51 | } |
| 52 | |
| 53 | /* ------------------------------------------------------------------------ */ |
| 54 | /* |
| 55 | * Data Stream |
| 56 | */ |
| 57 | |
| 58 | /* |
| 59 | * initialize |
| 60 | */ |
| 61 | CTcDataStream::CTcDataStream(char stream_id) |
| 62 | { |
| 63 | /* remember my ID */ |
| 64 | stream_id_ = stream_id; |
| 65 | |
| 66 | /* nothing is allocated yet */ |
| 67 | ofs_ = 0; |
| 68 | obj_file_start_ofs_ = 0; |
| 69 | pages_ = 0; |
| 70 | page_slots_ = 0; |
| 71 | page_cnt_ = 0; |
| 72 | page_cur_ = 0; |
| 73 | rem_ = 0; |
| 74 | wp_ = 0; |
| 75 | |
| 76 | /* we have no anchors yet */ |
| 77 | first_anchor_ = last_anchor_ = 0; |
| 78 | |
| 79 | /* create our parser memory allocator */ |
| 80 | allocator_ = new CTcPrsMem(); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * delete |
| 85 | */ |
| 86 | CTcDataStream::~CTcDataStream() |
| 87 | { |
| 88 | size_t i; |
| 89 | |
| 90 | /* delete the page slots if we allocated any */ |
| 91 | for (i = 0 ; i < page_cnt_ ; ++i) |
| 92 | t3free(pages_[i]); |
| 93 | |
| 94 | /* delete the page slot array if we allocated it */ |
| 95 | if (pages_ != 0) |
| 96 | t3free(pages_); |
| 97 | |
| 98 | /* delete our label/fixup allocator */ |
| 99 | delete allocator_; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Reset |
| 104 | */ |
| 105 | void CTcDataStream::reset() |
| 106 | { |
| 107 | /* move the write pointer back to the start */ |
| 108 | ofs_ = 0; |
| 109 | obj_file_start_ofs_ = 0; |
| 110 | |
| 111 | /* back to the first page */ |
| 112 | page_cur_ = 0; |
| 113 | |
| 114 | /* set up to write to the first page, if we have any pages at all */ |
| 115 | if (pages_ != 0) |
| 116 | { |
| 117 | /* we have all of the first page available again */ |
| 118 | wp_ = calc_addr(0); |
| 119 | rem_ = TCCS_PAGE_SIZE; |
| 120 | } |
| 121 | |
| 122 | /* reset the allocator */ |
| 123 | allocator_->reset(); |
| 124 | |
| 125 | /* |
| 126 | * forget all of the anchors (no need to delete them explicitly - |
| 127 | * they were allocated from our allocator pool, which we've reset to |
| 128 | * completely discard everything it contained) |
| 129 | */ |
| 130 | first_anchor_ = last_anchor_ = 0; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Decrement the write offset |
| 135 | */ |
| 136 | void CTcDataStream::dec_ofs(int amount) |
| 137 | { |
| 138 | /* adjust the offset */ |
| 139 | ofs_ -= amount; |
| 140 | |
| 141 | /* |
| 142 | * calculate the new page we're on, since this may take us to a |
| 143 | * different page |
| 144 | */ |
| 145 | page_cur_ = ofs_ / TCCS_PAGE_SIZE; |
| 146 | |
| 147 | /* calculate the remaining size in this page */ |
| 148 | rem_ = TCCS_PAGE_SIZE - (ofs_ % TCCS_PAGE_SIZE); |
| 149 | |
| 150 | /* calculate the current write pointer */ |
| 151 | wp_ = calc_addr(ofs_); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Get a pointer to a block at a given offset and a given length. |
| 156 | */ |
| 157 | const char *CTcDataStream::get_block_ptr(ulong ofs, |
| 158 | ulong requested_len, |
| 159 | ulong *available_len) |
| 160 | { |
| 161 | size_t page_rem; |
| 162 | |
| 163 | /* |
| 164 | * determine how much is left on the page containing the offset |
| 165 | * after the given offset |
| 166 | */ |
| 167 | page_rem = TCCS_PAGE_SIZE - (ofs % TCCS_PAGE_SIZE); |
| 168 | |
| 169 | /* |
| 170 | * if the amount remaining on the page is greater than the request |
| 171 | * length, the available length is the entire request; otherwise, |
| 172 | * the available length is the amount remaining on the page |
| 173 | */ |
| 174 | if (page_rem >= requested_len) |
| 175 | *available_len = requested_len; |
| 176 | else |
| 177 | *available_len = page_rem; |
| 178 | |
| 179 | /* return the address at this offset */ |
| 180 | return calc_addr(ofs); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* |
| 185 | * Write bytes to the stream at an earlier offset |
| 186 | */ |
| 187 | void CTcDataStream::write_at(ulong ofs, const char *buf, size_t len) |
| 188 | { |
| 189 | /* if we're writing to the current offset, use the normal writer */ |
| 190 | if (ofs == ofs_) |
| 191 | write(buf, len); |
| 192 | |
| 193 | /* |
| 194 | * log an internal error, and skip writing anything, if the desired |
| 195 | * range of offsets has not been previously written |
| 196 | */ |
| 197 | if (ofs + len > ofs_) |
| 198 | G_tok->throw_internal_error(TCERR_WRITEAT_PAST_END); |
| 199 | |
| 200 | /* write the data to each page it spans */ |
| 201 | while (len != 0) |
| 202 | { |
| 203 | size_t cur; |
| 204 | |
| 205 | /* |
| 206 | * determine how much is left on the page containing the current |
| 207 | * starting offset |
| 208 | */ |
| 209 | cur = TCCS_PAGE_SIZE - (ofs % TCCS_PAGE_SIZE); |
| 210 | |
| 211 | /* |
| 212 | * figure out how much we can copy - copy the whole remaining |
| 213 | * size, but no more than the amount remaining on this page |
| 214 | */ |
| 215 | if (cur > len) |
| 216 | cur = len; |
| 217 | |
| 218 | /* copy the data */ |
| 219 | memcpy(calc_addr(ofs), buf, cur); |
| 220 | |
| 221 | /* advance past this chunk */ |
| 222 | len -= cur; |
| 223 | ofs += cur; |
| 224 | buf += cur; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Copy a chunk of the stream to the given buffer |
| 230 | */ |
| 231 | void CTcDataStream::copy_to_buf(char *buf, ulong start_ofs, ulong len) |
| 232 | { |
| 233 | /* read the data from each page that the block spans */ |
| 234 | while (len != 0) |
| 235 | { |
| 236 | size_t cur; |
| 237 | |
| 238 | /* |
| 239 | * determine how much is left on the page containing the current |
| 240 | * starting offset |
| 241 | */ |
| 242 | cur = TCCS_PAGE_SIZE - (start_ofs % TCCS_PAGE_SIZE); |
| 243 | |
| 244 | /* |
| 245 | * figure out how much we can copy - copy the whole remaining |
| 246 | * size, but no more than the amount remaining on this page |
| 247 | */ |
| 248 | if (cur > len) |
| 249 | cur = (size_t)len; |
| 250 | |
| 251 | /* copy the data */ |
| 252 | memcpy(buf, calc_addr(start_ofs), cur); |
| 253 | |
| 254 | /* advance past this chunk */ |
| 255 | len -= cur; |
| 256 | start_ofs += cur; |
| 257 | buf += cur; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Reserve space |
| 263 | */ |
| 264 | ulong CTcDataStream::reserve(size_t len) |
| 265 | { |
| 266 | ulong ret; |
| 267 | |
| 268 | /* we'll always return the offset current before the call */ |
| 269 | ret = ofs_; |
| 270 | |
| 271 | /* if we have space on the current page, it's easy */ |
| 272 | if (len <= rem_) |
| 273 | { |
| 274 | /* advance the output pointers */ |
| 275 | ofs_ += len; |
| 276 | wp_ += len; |
| 277 | rem_ -= len; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | /* keep going until we satisfy the request */ |
| 282 | do |
| 283 | { |
| 284 | size_t cur; |
| 285 | |
| 286 | /* if necessary, allocate more memory */ |
| 287 | if (rem_ == 0) |
| 288 | alloc_page(); |
| 289 | |
| 290 | /* limit this chunk to the space remaining on the current page */ |
| 291 | cur = len; |
| 292 | if (cur > rem_) |
| 293 | cur = rem_; |
| 294 | |
| 295 | /* skip past this chunk */ |
| 296 | ofs_ += cur; |
| 297 | wp_ += cur; |
| 298 | rem_ -= cur; |
| 299 | len -= cur; |
| 300 | |
| 301 | } while (len != 0); |
| 302 | } |
| 303 | |
| 304 | /* return the starting offset */ |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Append data from another stream. The source stream is permanently |
| 310 | * moved to the new stream, destroying the original stream. |
| 311 | */ |
| 312 | void CTcDataStream::append_stream(CTcDataStream *stream) |
| 313 | { |
| 314 | ulong rem; |
| 315 | ulong ofs; |
| 316 | ulong start_ofs; |
| 317 | CTcStreamAnchor *anchor; |
| 318 | CTcStreamAnchor *nxt; |
| 319 | |
| 320 | /* remember the starting offset of the copy in my stream */ |
| 321 | start_ofs = get_ofs(); |
| 322 | |
| 323 | /* copy all data from the other stream */ |
| 324 | for (ofs = 0, rem = stream->get_ofs() ; rem != 0 ; ) |
| 325 | { |
| 326 | ulong request; |
| 327 | const char *ptr; |
| 328 | ulong actual; |
| 329 | |
| 330 | /* |
| 331 | * request as much as possible from the other stream, up to the |
| 332 | * remaining length or 64k, whichever is smaller |
| 333 | */ |
| 334 | request = 65535; |
| 335 | if (rem < request) |
| 336 | request = rem; |
| 337 | |
| 338 | /* get the chunk from the source stream */ |
| 339 | ptr = stream->get_block_ptr(ofs, request, &actual); |
| 340 | |
| 341 | /* |
| 342 | * write this chunk (which we know is less than 64k and can thus |
| 343 | * be safely cast to size_t, even on 16-bit machines) |
| 344 | */ |
| 345 | write(ptr, (size_t)actual); |
| 346 | |
| 347 | /* advance our counters */ |
| 348 | rem -= actual; |
| 349 | ofs += actual; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Now copy all of the anchors from the source stream to our stream. |
| 354 | * This will ensure that fixups in the other stream have |
| 355 | * corresponding fixups in this stream. Note that we must adjust |
| 356 | * the offset of each copied anchor by the offset of the start of |
| 357 | * the copied data in our stream. |
| 358 | */ |
| 359 | for (anchor = stream->get_first_anchor() ; anchor != 0 ; anchor = nxt) |
| 360 | { |
| 361 | /* |
| 362 | * remember the old link to the next anchor, since we're going |
| 363 | * to move the anchor to my list and thus forget about its |
| 364 | * position in the old list |
| 365 | */ |
| 366 | nxt = anchor->nxt_; |
| 367 | |
| 368 | /* adjust the anchor's offset */ |
| 369 | anchor->ofs_ += start_ofs; |
| 370 | |
| 371 | /* unlink the anchor from its old stream */ |
| 372 | anchor->nxt_ = 0; |
| 373 | |
| 374 | /* link it in to my anchor list */ |
| 375 | if (last_anchor_ != 0) |
| 376 | last_anchor_->nxt_ = anchor; |
| 377 | else |
| 378 | first_anchor_ = anchor; |
| 379 | last_anchor_ = anchor; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Write bytes to the stream |
| 385 | */ |
| 386 | void CTcDataStream::write(const char *buf, size_t len) |
| 387 | { |
| 388 | /* |
| 389 | * if possible, write it in one go (this is for efficiency, so that |
| 390 | * we can avoid making a few comparisons in the most common case) |
| 391 | */ |
| 392 | if (len <= rem_) |
| 393 | { |
| 394 | /* write the data */ |
| 395 | memcpy(wp_, buf, len); |
| 396 | |
| 397 | /* advance the output pointers */ |
| 398 | ofs_ += len; |
| 399 | wp_ += len; |
| 400 | rem_ -= len; |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | /* keep going until we satisfy the request */ |
| 405 | do |
| 406 | { |
| 407 | size_t cur; |
| 408 | |
| 409 | /* if necessary, allocate more memory */ |
| 410 | if (rem_ == 0) |
| 411 | alloc_page(); |
| 412 | |
| 413 | /* limit this chunk to the space remaining on the current page */ |
| 414 | cur = len; |
| 415 | if (cur > rem_) |
| 416 | cur = rem_; |
| 417 | |
| 418 | /* copy it to the page */ |
| 419 | memcpy(wp_, buf, cur); |
| 420 | |
| 421 | /* skip past the space written in the destination */ |
| 422 | ofs_ += cur; |
| 423 | wp_ += cur; |
| 424 | rem_ -= cur; |
| 425 | |
| 426 | /* advance past the space in the source */ |
| 427 | buf += cur; |
| 428 | len -= cur; |
| 429 | |
| 430 | } while (len != 0); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * allocate a new page |
| 436 | */ |
| 437 | void CTcDataStream::alloc_page() |
| 438 | { |
| 439 | /* |
| 440 | * if we're coming back to a page that was previously allocated, we |
| 441 | * need merely re-establish the existing page |
| 442 | */ |
| 443 | if (page_cur_ + 1 < page_cnt_) |
| 444 | { |
| 445 | /* move to the next page */ |
| 446 | ++page_cur_; |
| 447 | |
| 448 | /* start writing at the start of the page */ |
| 449 | wp_ = pages_[page_cur_]; |
| 450 | rem_ = TCCS_PAGE_SIZE; |
| 451 | |
| 452 | /* we're done */ |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * if we don't have room for a new page in the page array, expand |
| 458 | * the page array |
| 459 | */ |
| 460 | if (page_cnt_ >= page_slots_) |
| 461 | { |
| 462 | /* increase the page slot count */ |
| 463 | page_slots_ += 100; |
| 464 | |
| 465 | /* allocate or reallocate the page array */ |
| 466 | if (pages_ == 0) |
| 467 | pages_ = (char **)t3malloc(page_slots_ * sizeof(pages_[0])); |
| 468 | else |
| 469 | pages_ = (char **)t3realloc(pages_, |
| 470 | page_slots_ * sizeof(pages_[0])); |
| 471 | |
| 472 | /* if that failed, throw an error */ |
| 473 | if (pages_ == 0) |
| 474 | err_throw(TCERR_CODEGEN_NO_MEM); |
| 475 | } |
| 476 | |
| 477 | /* allocate the new page */ |
| 478 | pages_[page_cnt_] = (char *)t3malloc(TCCS_PAGE_SIZE); |
| 479 | |
| 480 | /* throw an error if we couldn't allocate the page */ |
| 481 | if (pages_[page_cnt_] == 0) |
| 482 | err_throw(TCERR_CODEGEN_NO_MEM); |
| 483 | |
| 484 | /* start writing at the start of the new page */ |
| 485 | wp_ = pages_[page_cnt_]; |
| 486 | |
| 487 | /* the entire page is free */ |
| 488 | rem_ = TCCS_PAGE_SIZE; |
| 489 | |
| 490 | /* make the new page the current page */ |
| 491 | page_cur_ = page_cnt_; |
| 492 | |
| 493 | /* count the new page */ |
| 494 | ++page_cnt_; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Add an absolute fixup for this stream at the current write offset. |
| 499 | */ |
| 500 | void CTcDataStream::add_abs_fixup(CTcAbsFixup **list_head) |
| 501 | { |
| 502 | /* add the fixup to the list at my current write location */ |
| 503 | CTcAbsFixup::add_abs_fixup(list_head, this, get_ofs()); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | /* |
| 508 | * Add an anchor at the current offset. |
| 509 | */ |
| 510 | CTcStreamAnchor *CTcDataStream::add_anchor(CTcSymbol *owner_sym, |
| 511 | CTcAbsFixup **fixup_list_head, |
| 512 | ulong ofs) |
| 513 | { |
| 514 | CTcStreamAnchor *anchor; |
| 515 | |
| 516 | /* allocate the anchor, giving it our current offset */ |
| 517 | anchor = new (allocator_) CTcStreamAnchor(owner_sym, |
| 518 | fixup_list_head, ofs); |
| 519 | |
| 520 | /* append it to our list */ |
| 521 | if (last_anchor_ != 0) |
| 522 | last_anchor_->nxt_ = anchor; |
| 523 | else |
| 524 | first_anchor_ = anchor; |
| 525 | last_anchor_ = anchor; |
| 526 | |
| 527 | /* return the new anchor */ |
| 528 | return anchor; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Find an anchor with the given stream offset |
| 533 | */ |
| 534 | CTcStreamAnchor *CTcDataStream::find_anchor(ulong ofs) const |
| 535 | { |
| 536 | CTcStreamAnchor *cur; |
| 537 | |
| 538 | /* scan the anchor list */ |
| 539 | for (cur = first_anchor_ ; cur != 0 ; cur = cur->nxt_) |
| 540 | { |
| 541 | /* if this one has the desired offset, return it */ |
| 542 | if (cur->get_ofs() == ofs) |
| 543 | return cur; |
| 544 | } |
| 545 | |
| 546 | /* didn't find it */ |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Write an object ID |
| 552 | */ |
| 553 | void CTcDataStream::write_obj_id(ulong obj_id) |
| 554 | { |
| 555 | /* |
| 556 | * if there's an object ID fixup list, and this is a valid object |
| 557 | * reference (not a 'nil' reference), add this reference |
| 558 | */ |
| 559 | if (G_keep_objfixups && obj_id != TCTARG_INVALID_OBJ) |
| 560 | CTcIdFixup::add_fixup(&G_objfixup, this, get_ofs(), obj_id); |
| 561 | |
| 562 | /* write the ID */ |
| 563 | write4(obj_id); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Write an object ID self-reference |
| 568 | */ |
| 569 | void CTcDataStream::write_obj_id_selfref(CTcSymObj *obj_sym) |
| 570 | { |
| 571 | /* |
| 572 | * Add a fixup list entry to the symbol. This type of reference |
| 573 | * must be kept with the symbol rather than in the global list, |
| 574 | * because we must apply this type of fixup each time we renumber |
| 575 | * the symbol. |
| 576 | */ |
| 577 | obj_sym->add_self_ref_fixup(this, get_ofs()); |
| 578 | |
| 579 | /* write the ID to the stream */ |
| 580 | write4(obj_sym->get_obj_id()); |
| 581 | } |
| 582 | |
| 583 | |
| 584 | /* |
| 585 | * Write a property ID |
| 586 | */ |
| 587 | void CTcDataStream::write_prop_id(uint prop_id) |
| 588 | { |
| 589 | /* if there's an object ID fixup list, add this reference */ |
| 590 | if (G_keep_propfixups) |
| 591 | CTcIdFixup::add_fixup(&G_propfixup, this, get_ofs(), prop_id); |
| 592 | |
| 593 | /* write the ID */ |
| 594 | write2(prop_id); |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Write an enumerator ID |
| 599 | */ |
| 600 | void CTcDataStream::write_enum_id(ulong enum_id) |
| 601 | { |
| 602 | /* if there's a fixup list, add this reference */ |
| 603 | if (G_keep_enumfixups) |
| 604 | CTcIdFixup::add_fixup(&G_enumfixup, this, get_ofs(), enum_id); |
| 605 | |
| 606 | /* write the ID */ |
| 607 | write4(enum_id); |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * Given a stream ID, get the stream |
| 612 | */ |
| 613 | CTcDataStream *CTcDataStream:: |
| 614 | get_stream_from_id(char stream_id, const textchar_t *obj_fname) |
| 615 | { |
| 616 | switch(stream_id) |
| 617 | { |
| 618 | case TCGEN_DATA_STREAM: |
| 619 | return G_ds; |
| 620 | |
| 621 | case TCGEN_CODE_STREAM: |
| 622 | return G_cs_main; |
| 623 | |
| 624 | case TCGEN_STATIC_CODE_STREAM: |
| 625 | return G_cs_static; |
| 626 | |
| 627 | case TCGEN_OBJECT_STREAM: |
| 628 | return G_os; |
| 629 | |
| 630 | case TCGEN_ICMOD_STREAM: |
| 631 | return G_icmod_stream; |
| 632 | |
| 633 | case TCGEN_BIGNUM_STREAM: |
| 634 | return G_bignum_stream; |
| 635 | |
| 636 | case TCGEN_REXPAT_STREAM: |
| 637 | return G_rexpat_stream; |
| 638 | |
| 639 | case TCGEN_STATIC_INIT_ID_STREAM: |
| 640 | return G_static_init_id_stream; |
| 641 | |
| 642 | case TCGEN_LCL_VAR_STREAM: |
| 643 | return G_lcl_stream; |
| 644 | |
| 645 | default: |
| 646 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 647 | TCERR_OBJFILE_INVAL_STREAM_ID, obj_fname); |
| 648 | return 0; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | |
| 653 | /* ------------------------------------------------------------------------ */ |
| 654 | /* |
| 655 | * Code Stream |
| 656 | */ |
| 657 | |
| 658 | /* |
| 659 | * create the code stream |
| 660 | */ |
| 661 | CTcCodeStream::CTcCodeStream(char stream_id) |
| 662 | : CTcDataStream(stream_id) |
| 663 | { |
| 664 | /* no switch yet */ |
| 665 | cur_switch_ = 0; |
| 666 | |
| 667 | /* no enclosing statement yet */ |
| 668 | enclosing_ = 0; |
| 669 | |
| 670 | /* no code body being generated yet */ |
| 671 | code_body_ = 0; |
| 672 | |
| 673 | /* start writing at offset zero */ |
| 674 | ofs_ = 0; |
| 675 | |
| 676 | /* no symbol tables yet */ |
| 677 | symtab_ = 0; |
| 678 | goto_symtab_ = 0; |
| 679 | |
| 680 | /* no labels yet */ |
| 681 | active_lbl_ = 0; |
| 682 | free_lbl_ = 0; |
| 683 | |
| 684 | /* no fixups yet */ |
| 685 | free_fixup_ = 0; |
| 686 | |
| 687 | /* allocate an initial set of line record pages */ |
| 688 | line_pages_alloc_ = 0; |
| 689 | line_pages_ = 0; |
| 690 | alloc_line_pages(5); |
| 691 | |
| 692 | /* no line records in use yet */ |
| 693 | line_cnt_ = 0; |
| 694 | |
| 695 | /* no local frame yet */ |
| 696 | cur_frame_ = 0; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | /* |
| 701 | * destroy the code stream |
| 702 | */ |
| 703 | CTcCodeStream::~CTcCodeStream() |
| 704 | { |
| 705 | size_t i; |
| 706 | |
| 707 | /* release all active labels */ |
| 708 | release_labels(); |
| 709 | |
| 710 | /* delete the line records pages */ |
| 711 | for (i = 0 ; i < line_pages_alloc_ ; ++i) |
| 712 | t3free(line_pages_[i]); |
| 713 | |
| 714 | /* delete the master list of pages */ |
| 715 | t3free(line_pages_); |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Set the current local frame |
| 720 | */ |
| 721 | CTcPrsSymtab *CTcCodeStream::set_local_frame(CTcPrsSymtab *symtab) |
| 722 | { |
| 723 | /* remember the original local frame, so we can return it later */ |
| 724 | CTcPrsSymtab *old_frame = cur_frame_; |
| 725 | |
| 726 | /* add the current byte code location to the outgoing frame */ |
| 727 | if (old_frame != 0) |
| 728 | old_frame->add_to_range(ofs_ - method_ofs_); |
| 729 | |
| 730 | /* remember the current frame */ |
| 731 | cur_frame_ = symtab; |
| 732 | |
| 733 | /* add it to the local frame list for the method if necessary */ |
| 734 | add_local_frame(symtab); |
| 735 | |
| 736 | /* add the current byte code location to the incoming frame */ |
| 737 | symtab->add_to_range(ofs_ - method_ofs_); |
| 738 | |
| 739 | /* return the original local frame */ |
| 740 | return old_frame; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | /* |
| 745 | * Reset |
| 746 | */ |
| 747 | void CTcCodeStream::reset() |
| 748 | { |
| 749 | /* inherit default */ |
| 750 | CTcDataStream::reset(); |
| 751 | |
| 752 | /* clear the line records */ |
| 753 | clear_line_recs(); |
| 754 | |
| 755 | /* |
| 756 | * forget all of the labels and fixups - they're allocated from the |
| 757 | * allocator pool, which we've completely reset now |
| 758 | */ |
| 759 | free_lbl_ = active_lbl_ = 0; |
| 760 | free_fixup_ = 0; |
| 761 | |
| 762 | /* forget all of the symbol tables */ |
| 763 | symtab_ = 0; |
| 764 | goto_symtab_ = 0; |
| 765 | |
| 766 | /* forget the frame list */ |
| 767 | frame_head_ = frame_tail_ = 0; |
| 768 | cur_frame_ = 0; |
| 769 | frame_cnt_ = 0; |
| 770 | |
| 771 | /* forget all of the statement settings */ |
| 772 | cur_switch_ = 0; |
| 773 | enclosing_ = 0; |
| 774 | code_body_ = 0; |
| 775 | |
| 776 | /* presume 'self' is not available */ |
| 777 | self_available_ = FALSE; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Allocate line record pages |
| 782 | */ |
| 783 | void CTcCodeStream::alloc_line_pages(size_t number_to_add) |
| 784 | { |
| 785 | size_t siz; |
| 786 | size_t i; |
| 787 | |
| 788 | /* create or expand the master page array */ |
| 789 | siz = (line_pages_alloc_ + number_to_add) * sizeof(tcgen_line_page_t *); |
| 790 | if (line_pages_ == 0) |
| 791 | line_pages_ = (tcgen_line_page_t **)t3malloc(siz); |
| 792 | else |
| 793 | line_pages_ = (tcgen_line_page_t **)t3realloc(line_pages_, siz); |
| 794 | |
| 795 | /* allocate the new pages */ |
| 796 | for (i = line_pages_alloc_ ; i < line_pages_alloc_ + number_to_add ; ++i) |
| 797 | { |
| 798 | /* allocate this page */ |
| 799 | line_pages_[i] = (tcgen_line_page_t *) |
| 800 | t3malloc(sizeof(tcgen_line_page_t)); |
| 801 | } |
| 802 | |
| 803 | /* remember the new allocation */ |
| 804 | line_pages_alloc_ += number_to_add; |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Allocate a new label object |
| 809 | */ |
| 810 | CTcCodeLabel *CTcCodeStream::alloc_label() |
| 811 | { |
| 812 | CTcCodeLabel *ret; |
| 813 | |
| 814 | /* if there's anything in the free list, use it */ |
| 815 | if (free_lbl_ != 0) |
| 816 | { |
| 817 | /* take the first one off the free list */ |
| 818 | ret = free_lbl_; |
| 819 | |
| 820 | /* unlink it from the list */ |
| 821 | free_lbl_ = free_lbl_->nxt; |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | /* allocate a new label */ |
| 826 | ret = new (allocator_) CTcCodeLabel; |
| 827 | |
| 828 | /* throw an error if allocation failed */ |
| 829 | if (ret == 0) |
| 830 | err_throw(TCERR_CODEGEN_NO_MEM); |
| 831 | } |
| 832 | |
| 833 | /* add the label to the active list */ |
| 834 | ret->nxt = active_lbl_; |
| 835 | active_lbl_ = ret; |
| 836 | |
| 837 | /* return the allocated label */ |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Allocate a new fixup object |
| 843 | */ |
| 844 | CTcLabelFixup *CTcCodeStream::alloc_fixup() |
| 845 | { |
| 846 | CTcLabelFixup *ret; |
| 847 | |
| 848 | /* if there's anything in the free list, use it */ |
| 849 | if (free_fixup_ != 0) |
| 850 | { |
| 851 | /* take the first one off the free list */ |
| 852 | ret = free_fixup_; |
| 853 | |
| 854 | /* unlink it from the list */ |
| 855 | free_fixup_ = free_fixup_->nxt; |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | /* allocate a new fixup */ |
| 860 | ret = new (allocator_) CTcLabelFixup; |
| 861 | |
| 862 | /* throw an error if allocation failed */ |
| 863 | if (ret == 0) |
| 864 | err_throw(TCERR_CODEGEN_NO_MEM); |
| 865 | } |
| 866 | |
| 867 | /* return the allocated fixup */ |
| 868 | return ret; |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * Release all active labels. If any labels are undefined, log an |
| 873 | * internal error. |
| 874 | */ |
| 875 | void CTcCodeStream::release_labels() |
| 876 | { |
| 877 | int err_cnt; |
| 878 | |
| 879 | /* we haven't found any errors yet */ |
| 880 | err_cnt = 0; |
| 881 | |
| 882 | /* run through the list of active labels */ |
| 883 | while (active_lbl_ != 0) |
| 884 | { |
| 885 | CTcCodeLabel *lbl; |
| 886 | |
| 887 | /* pull this label off of the active list */ |
| 888 | lbl = active_lbl_; |
| 889 | active_lbl_ = active_lbl_->nxt; |
| 890 | |
| 891 | /* put this label on the free list */ |
| 892 | lbl->nxt = free_lbl_; |
| 893 | free_lbl_ = lbl; |
| 894 | |
| 895 | /* check for unresolved fixups */ |
| 896 | while (lbl->fhead != 0) |
| 897 | { |
| 898 | CTcLabelFixup *fixup; |
| 899 | |
| 900 | /* pull this fixup off of the active list */ |
| 901 | fixup = lbl->fhead; |
| 902 | lbl->fhead = lbl->fhead->nxt; |
| 903 | |
| 904 | /* put this fixup on the free list */ |
| 905 | fixup->nxt = free_fixup_; |
| 906 | free_fixup_ = fixup; |
| 907 | |
| 908 | /* count the unresolved label */ |
| 909 | ++err_cnt; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * if we found any unresolved fixups, log the error; there's not |
| 915 | * much point in logging each error individually, since this is an |
| 916 | * internal compiler error that the user can't do anything about, |
| 917 | * but at least give the user a count for compiler diagnostic |
| 918 | * purposes |
| 919 | */ |
| 920 | if (err_cnt != 0) |
| 921 | G_tcmain->log_error(0, 0, TC_SEV_INTERNAL, |
| 922 | TCERR_UNRES_TMP_FIXUP, err_cnt); |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Allocate a new label at the current code offset |
| 927 | */ |
| 928 | CTcCodeLabel *CTcCodeStream::new_label_here() |
| 929 | { |
| 930 | CTcCodeLabel *lbl; |
| 931 | |
| 932 | /* allocate a new label */ |
| 933 | lbl = alloc_label(); |
| 934 | |
| 935 | /* set the label's location to the current write position */ |
| 936 | lbl->ofs = ofs_; |
| 937 | lbl->is_known = TRUE; |
| 938 | |
| 939 | /* return the new label */ |
| 940 | return lbl; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Allocate a new forward-reference label |
| 945 | */ |
| 946 | CTcCodeLabel *CTcCodeStream::new_label_fwd() |
| 947 | { |
| 948 | CTcCodeLabel *lbl; |
| 949 | |
| 950 | /* allocate a new label */ |
| 951 | lbl = alloc_label(); |
| 952 | |
| 953 | /* the label's location is not yet known */ |
| 954 | lbl->ofs = 0; |
| 955 | lbl->is_known = FALSE; |
| 956 | |
| 957 | /* return the new label */ |
| 958 | return lbl; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Define the position of a label, resolving any fixups associated with |
| 963 | * the label. |
| 964 | */ |
| 965 | void CTcCodeStream::def_label_pos(CTcCodeLabel *lbl) |
| 966 | { |
| 967 | /* set the label's position */ |
| 968 | lbl->ofs = ofs_; |
| 969 | lbl->is_known = TRUE; |
| 970 | |
| 971 | /* resolve each fixup */ |
| 972 | while (lbl->fhead != 0) |
| 973 | { |
| 974 | CTcLabelFixup *fixup; |
| 975 | long diff; |
| 976 | char buf[4]; |
| 977 | |
| 978 | /* pull this fixup off of the active list */ |
| 979 | fixup = lbl->fhead; |
| 980 | lbl->fhead = lbl->fhead->nxt; |
| 981 | |
| 982 | /* |
| 983 | * calculate the offset from the fixup position to the label |
| 984 | * position, applying the bias to the fixup position |
| 985 | */ |
| 986 | diff = lbl->ofs - (fixup->ofs + fixup->bias); |
| 987 | |
| 988 | /* convert the offset to the correct format and write it out */ |
| 989 | if (fixup->is_long) |
| 990 | { |
| 991 | /* write an INT4 offset value */ |
| 992 | oswp4(buf, diff); |
| 993 | write_at(fixup->ofs, buf, 4); |
| 994 | } |
| 995 | else |
| 996 | { |
| 997 | /* write an INT2 offset value */ |
| 998 | oswp2s(buf, diff); |
| 999 | write_at(fixup->ofs, buf, 2); |
| 1000 | } |
| 1001 | |
| 1002 | /* add this fixup to the free list, since we're finished with it */ |
| 1003 | fixup->nxt = free_fixup_; |
| 1004 | free_fixup_ = fixup; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * Determine if a label has a fixup at a particular offset |
| 1010 | */ |
| 1011 | int CTcCodeStream::has_fixup_at_ofs(CTcCodeLabel *lbl, ulong ofs) |
| 1012 | { |
| 1013 | CTcLabelFixup *fixup; |
| 1014 | |
| 1015 | /* scan for a match */ |
| 1016 | for (fixup = lbl->fhead ; fixup != 0 ; fixup = fixup->nxt) |
| 1017 | { |
| 1018 | /* if this is a match, indicate success */ |
| 1019 | if (fixup->ofs == ofs) |
| 1020 | return TRUE; |
| 1021 | } |
| 1022 | |
| 1023 | /* we didn't find a match */ |
| 1024 | return FALSE; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Remove a label's fixup at a particular offset |
| 1029 | */ |
| 1030 | void CTcCodeStream::remove_fixup_at_ofs(CTcCodeLabel *lbl, ulong ofs) |
| 1031 | { |
| 1032 | CTcLabelFixup *fixup; |
| 1033 | CTcLabelFixup *prv; |
| 1034 | CTcLabelFixup *nxt; |
| 1035 | |
| 1036 | /* scan for a match */ |
| 1037 | for (prv = 0, fixup = lbl->fhead ; fixup != 0 ; prv = fixup, fixup = nxt) |
| 1038 | { |
| 1039 | /* remember the next one */ |
| 1040 | nxt = fixup->nxt; |
| 1041 | |
| 1042 | /* if this is a match, remove it */ |
| 1043 | if (fixup->ofs == ofs) |
| 1044 | { |
| 1045 | /* unlink this fixup from the list */ |
| 1046 | if (prv == 0) |
| 1047 | lbl->fhead = nxt; |
| 1048 | else |
| 1049 | prv->nxt = nxt; |
| 1050 | |
| 1051 | /* move it to the free list */ |
| 1052 | fixup->nxt = free_fixup_; |
| 1053 | free_fixup_ = fixup; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | * Write an offset value to the given label |
| 1060 | */ |
| 1061 | void CTcCodeStream::write_ofs(CTcCodeLabel *lbl, int bias, int is_long) |
| 1062 | { |
| 1063 | /* if the label is known, write it; otherwise, generate a fixup */ |
| 1064 | if (lbl->is_known) |
| 1065 | { |
| 1066 | long diff; |
| 1067 | |
| 1068 | /* |
| 1069 | * calculate the branch offset from the current position, |
| 1070 | * applying the bias to the current position |
| 1071 | */ |
| 1072 | diff = lbl->ofs - (ofs_ + bias); |
| 1073 | |
| 1074 | /* convert the offset to the correct format and write it out */ |
| 1075 | if (is_long) |
| 1076 | write4(diff); |
| 1077 | else |
| 1078 | write2(diff); |
| 1079 | } |
| 1080 | else |
| 1081 | { |
| 1082 | CTcLabelFixup *fixup; |
| 1083 | |
| 1084 | /* allocate a fixup */ |
| 1085 | fixup = alloc_fixup(); |
| 1086 | |
| 1087 | /* set up the fixup data */ |
| 1088 | fixup->ofs = ofs_; |
| 1089 | fixup->bias = bias; |
| 1090 | fixup->is_long = is_long; |
| 1091 | |
| 1092 | /* link the fixup into the label's fixup list */ |
| 1093 | fixup->nxt = lbl->fhead; |
| 1094 | lbl->fhead = fixup; |
| 1095 | |
| 1096 | /* write a placeholder to the code stream */ |
| 1097 | if (is_long) |
| 1098 | write4(0); |
| 1099 | else |
| 1100 | write2(0); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * Add a new line record at the current code offset |
| 1106 | */ |
| 1107 | void CTcCodeStream::add_line_rec(CTcTokFileDesc *file, long linenum) |
| 1108 | { |
| 1109 | /* if there's no file descriptor, there's nothing to add */ |
| 1110 | if (file == 0) |
| 1111 | return; |
| 1112 | |
| 1113 | /* compute the current offset, relative to the start of the method */ |
| 1114 | ulong cur_ofs = G_cs->get_ofs() - method_ofs_; |
| 1115 | |
| 1116 | /* presume we won't re-use the previous record */ |
| 1117 | int reuse = FALSE; |
| 1118 | |
| 1119 | /* |
| 1120 | * If we haven't added any code since the previous line record, |
| 1121 | * overwrite the previous record - it doesn't refer to any |
| 1122 | * executable code, so it's an unnecessary record. Similarly, if |
| 1123 | * the previous record is at the same source position, don't add a |
| 1124 | * separate line record for it, since the debugger won't be able to |
| 1125 | * treat the two lines separately. |
| 1126 | */ |
| 1127 | tcgen_line_t *rec; |
| 1128 | if (line_cnt_ > 0) |
| 1129 | { |
| 1130 | /* get the previous record */ |
| 1131 | rec = get_line_rec(line_cnt_ - 1); |
| 1132 | |
| 1133 | /* |
| 1134 | * if it refers to the same code offset, replace the old record |
| 1135 | * with one at this location |
| 1136 | */ |
| 1137 | if (rec->ofs == cur_ofs) |
| 1138 | reuse = TRUE; |
| 1139 | |
| 1140 | /* |
| 1141 | * if it has the identical source file location, don't bother |
| 1142 | * adding a new record at all |
| 1143 | */ |
| 1144 | if (rec->source_id == file->get_index() |
| 1145 | && rec->source_line == linenum) |
| 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | /* if we're not re-using the previous record, allocate a new one */ |
| 1150 | if (!reuse) |
| 1151 | { |
| 1152 | /* |
| 1153 | * we need a new record - if we've used all the allocated space, |
| 1154 | * allocate more |
| 1155 | */ |
| 1156 | if (line_cnt_ >= line_pages_alloc_ * TCGEN_LINE_PAGE_SIZE) |
| 1157 | alloc_line_pages(5); |
| 1158 | |
| 1159 | /* get a pointer to the next available entry */ |
| 1160 | rec = get_line_rec(line_cnt_); |
| 1161 | |
| 1162 | /* count the new record */ |
| 1163 | ++line_cnt_; |
| 1164 | } |
| 1165 | |
| 1166 | /* store the code offset relative to the start of the current method */ |
| 1167 | rec->ofs = cur_ofs; |
| 1168 | |
| 1169 | /* store the file information */ |
| 1170 | rec->source_id = file->get_index(); |
| 1171 | rec->source_line = linenum; |
| 1172 | |
| 1173 | /* store the frame information */ |
| 1174 | rec->frame = cur_frame_; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Get the nth line record |
| 1179 | */ |
| 1180 | tcgen_line_t *CTcCodeStream::get_line_rec(size_t n) |
| 1181 | { |
| 1182 | return &(line_pages_[n / TCGEN_LINE_PAGE_SIZE] |
| 1183 | ->lines[n % TCGEN_LINE_PAGE_SIZE]); |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * Add a frame to the list of local frames in the method |
| 1188 | */ |
| 1189 | void CTcCodeStream::add_local_frame(CTcPrsSymtab *symtab) |
| 1190 | { |
| 1191 | /* |
| 1192 | * If this is the global symbol table, or it's null, or it's already |
| 1193 | * in a list, ignore it. Note that we can tell if the item is in a |
| 1194 | * list by checking its index value - a value of zero is never a |
| 1195 | * valid index and thus indicates that the item isn't in a list yet. |
| 1196 | */ |
| 1197 | if (symtab == G_prs->get_global_symtab() |
| 1198 | || symtab == 0 |
| 1199 | || symtab->get_list_index() != 0) |
| 1200 | return; |
| 1201 | |
| 1202 | /* link the frame in at the tail of our list */ |
| 1203 | symtab->set_list_next(0); |
| 1204 | if (frame_tail_ == 0) |
| 1205 | frame_head_ = symtab; |
| 1206 | else |
| 1207 | frame_tail_->set_list_next(symtab); |
| 1208 | frame_tail_ = symtab; |
| 1209 | |
| 1210 | /* count the new entry in the list */ |
| 1211 | ++frame_cnt_; |
| 1212 | |
| 1213 | /* |
| 1214 | * Set this frame's index in the list. Note that we've already |
| 1215 | * incremented the index value, so the first frame in the list will |
| 1216 | * have index 1, as is required. |
| 1217 | */ |
| 1218 | symtab->set_list_index(frame_cnt_); |
| 1219 | } |
| 1220 | |
| 1221 | /* ------------------------------------------------------------------------ */ |
| 1222 | /* |
| 1223 | * Data stream anchor |
| 1224 | */ |
| 1225 | |
| 1226 | /* |
| 1227 | * Get the length. We can deduce the length by subtracting our offset |
| 1228 | * from the next item's offset, or, if we're the last item, from the |
| 1229 | * length of the stream. |
| 1230 | */ |
| 1231 | ulong CTcStreamAnchor::get_len(CTcDataStream *ds) const |
| 1232 | { |
| 1233 | if (nxt_ != 0) |
| 1234 | { |
| 1235 | /* |
| 1236 | * there's another item after me - my length is the difference |
| 1237 | * between the next item's offset and my offset |
| 1238 | */ |
| 1239 | return (nxt_->ofs_ - ofs_); |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | /* I'm the last item - my length is whatever is left in the stream */ |
| 1244 | return (ds->get_ofs() - ofs_); |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * Set the finaly absoluate address, and apply fixups. The code |
| 1250 | * generator must invoke this during the link phase once this object's |
| 1251 | * final address is known. |
| 1252 | */ |
| 1253 | void CTcStreamAnchor::set_addr(ulong addr) |
| 1254 | { |
| 1255 | /* remember my address */ |
| 1256 | addr_ = addr; |
| 1257 | |
| 1258 | /* apply all outstanding fixups for this object */ |
| 1259 | CTcAbsFixup::fix_abs_fixup(*fixup_list_head_, addr); |
| 1260 | } |
| 1261 | |
| 1262 | /* ------------------------------------------------------------------------ */ |
| 1263 | /* |
| 1264 | * Absolute Fixup Object |
| 1265 | */ |
| 1266 | |
| 1267 | /* |
| 1268 | * Add an absolute fixup at the current stream location to a given fixup |
| 1269 | * list. |
| 1270 | */ |
| 1271 | void CTcAbsFixup::add_abs_fixup(CTcAbsFixup **list_head, |
| 1272 | CTcDataStream *ds, ulong ofs) |
| 1273 | { |
| 1274 | CTcAbsFixup *fixup; |
| 1275 | |
| 1276 | /* |
| 1277 | * create the fixup object - allocate it out of our fixup allocator |
| 1278 | * pool, since this fixup object has the same attributes (small and |
| 1279 | * long-lived) as other fixup objects |
| 1280 | */ |
| 1281 | fixup = (CTcAbsFixup *)G_prsmem->alloc(sizeof(CTcAbsFixup)); |
| 1282 | |
| 1283 | /* set the fixup location to the current offset */ |
| 1284 | fixup->ds = ds; |
| 1285 | fixup->ofs = ofs; |
| 1286 | |
| 1287 | /* link it in to the caller's list */ |
| 1288 | fixup->nxt = *list_head; |
| 1289 | *list_head = fixup; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * Fix up a fix-up list |
| 1294 | */ |
| 1295 | void CTcAbsFixup::fix_abs_fixup(CTcAbsFixup *list_head, ulong final_ofs) |
| 1296 | { |
| 1297 | CTcAbsFixup *cur; |
| 1298 | |
| 1299 | /* scan the list and fix up each entry */ |
| 1300 | for (cur = list_head ; cur != 0 ; cur = cur->nxt) |
| 1301 | { |
| 1302 | /* |
| 1303 | * fix this entry by writing the final offset in UINT4 format to |
| 1304 | * the target stream at the target offset |
| 1305 | */ |
| 1306 | cur->ds->write4_at(cur->ofs, final_ofs); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /* ------------------------------------------------------------------------ */ |
| 1311 | /* |
| 1312 | * Object/property ID fixups |
| 1313 | */ |
| 1314 | |
| 1315 | /* |
| 1316 | * add a fixup to a list |
| 1317 | */ |
| 1318 | void CTcIdFixup::add_fixup(CTcIdFixup **list_head, class CTcDataStream *ds, |
| 1319 | ulong ofs, ulong id) |
| 1320 | { |
| 1321 | CTcIdFixup *fixup; |
| 1322 | |
| 1323 | /* create the new fixup object */ |
| 1324 | fixup = new (G_prsmem) CTcIdFixup(ds, ofs, id); |
| 1325 | |
| 1326 | /* link it in at the head of the list */ |
| 1327 | fixup->nxt_ = *list_head; |
| 1328 | *list_head = fixup; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * Apply this fixup |
| 1333 | */ |
| 1334 | void CTcIdFixup::apply_fixup(ulong final_id, size_t siz) |
| 1335 | { |
| 1336 | /* write the fixup */ |
| 1337 | if (siz == 2) |
| 1338 | ds_->write2_at(ofs_, (uint)final_id); |
| 1339 | else |
| 1340 | ds_->write4_at(ofs_, final_id); |
| 1341 | } |
| 1342 |