-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcgen.cpp
1342 lines (1114 loc) · 31.9 KB
/
tcgen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/tads3/tcgen.cpp,v 1.4 1999/07/11 00:46:58 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
tcgen.cpp - TADS 3 Compiler code generator support classes
Function
Notes
Modified
05/09/99 MJRoberts - Creation
*/
#include <stdlib.h>
#include <string.h>
#include "t3std.h"
#include "os.h"
#include "tcglob.h"
#include "tcgen.h"
#include "vmerr.h"
#include "tcerrnum.h"
#include "tctok.h"
#include "tcprs.h"
#include "tcmain.h"
#include "vmfile.h"
#include "tctarg.h"
/* ------------------------------------------------------------------------ */
/*
* Data/Code Stream Parser-Allocated Object
*/
/*
* allocate via a parser memory allocator
*/
void *CTcCSPrsAllocObj::operator new(size_t siz, CTcPrsMem *allocator)
{
/* allocate via the allocator */
return allocator->alloc(siz);
}
/* ------------------------------------------------------------------------ */
/*
* Data Stream
*/
/*
* initialize
*/
CTcDataStream::CTcDataStream(char stream_id)
{
/* remember my ID */
stream_id_ = stream_id;
/* nothing is allocated yet */
ofs_ = 0;
obj_file_start_ofs_ = 0;
pages_ = 0;
page_slots_ = 0;
page_cnt_ = 0;
page_cur_ = 0;
rem_ = 0;
wp_ = 0;
/* we have no anchors yet */
first_anchor_ = last_anchor_ = 0;
/* create our parser memory allocator */
allocator_ = new CTcPrsMem();
}
/*
* delete
*/
CTcDataStream::~CTcDataStream()
{
size_t i;
/* delete the page slots if we allocated any */
for (i = 0 ; i < page_cnt_ ; ++i)
t3free(pages_[i]);
/* delete the page slot array if we allocated it */
if (pages_ != 0)
t3free(pages_);
/* delete our label/fixup allocator */
delete allocator_;
}
/*
* Reset
*/
void CTcDataStream::reset()
{
/* move the write pointer back to the start */
ofs_ = 0;
obj_file_start_ofs_ = 0;
/* back to the first page */
page_cur_ = 0;
/* set up to write to the first page, if we have any pages at all */
if (pages_ != 0)
{
/* we have all of the first page available again */
wp_ = calc_addr(0);
rem_ = TCCS_PAGE_SIZE;
}
/* reset the allocator */
allocator_->reset();
/*
* forget all of the anchors (no need to delete them explicitly -
* they were allocated from our allocator pool, which we've reset to
* completely discard everything it contained)
*/
first_anchor_ = last_anchor_ = 0;
}
/*
* Decrement the write offset
*/
void CTcDataStream::dec_ofs(int amount)
{
/* adjust the offset */
ofs_ -= amount;
/*
* calculate the new page we're on, since this may take us to a
* different page
*/
page_cur_ = ofs_ / TCCS_PAGE_SIZE;
/* calculate the remaining size in this page */
rem_ = TCCS_PAGE_SIZE - (ofs_ % TCCS_PAGE_SIZE);
/* calculate the current write pointer */
wp_ = calc_addr(ofs_);
}
/*
* Get a pointer to a block at a given offset and a given length.
*/
const char *CTcDataStream::get_block_ptr(ulong ofs,
ulong requested_len,
ulong *available_len)
{
size_t page_rem;
/*
* determine how much is left on the page containing the offset
* after the given offset
*/
page_rem = TCCS_PAGE_SIZE - (ofs % TCCS_PAGE_SIZE);
/*
* if the amount remaining on the page is greater than the request
* length, the available length is the entire request; otherwise,
* the available length is the amount remaining on the page
*/
if (page_rem >= requested_len)
*available_len = requested_len;
else
*available_len = page_rem;
/* return the address at this offset */
return calc_addr(ofs);
}
/*
* Write bytes to the stream at an earlier offset
*/
void CTcDataStream::write_at(ulong ofs, const char *buf, size_t len)
{
/* if we're writing to the current offset, use the normal writer */
if (ofs == ofs_)
write(buf, len);
/*
* log an internal error, and skip writing anything, if the desired
* range of offsets has not been previously written
*/
if (ofs + len > ofs_)
G_tok->throw_internal_error(TCERR_WRITEAT_PAST_END);
/* write the data to each page it spans */
while (len != 0)
{
size_t cur;
/*
* determine how much is left on the page containing the current
* starting offset
*/
cur = TCCS_PAGE_SIZE - (ofs % TCCS_PAGE_SIZE);
/*
* figure out how much we can copy - copy the whole remaining
* size, but no more than the amount remaining on this page
*/
if (cur > len)
cur = len;
/* copy the data */
memcpy(calc_addr(ofs), buf, cur);
/* advance past this chunk */
len -= cur;
ofs += cur;
buf += cur;
}
}
/*
* Copy a chunk of the stream to the given buffer
*/
void CTcDataStream::copy_to_buf(char *buf, ulong start_ofs, ulong len)
{
/* read the data from each page that the block spans */
while (len != 0)
{
size_t cur;
/*
* determine how much is left on the page containing the current
* starting offset
*/
cur = TCCS_PAGE_SIZE - (start_ofs % TCCS_PAGE_SIZE);
/*
* figure out how much we can copy - copy the whole remaining
* size, but no more than the amount remaining on this page
*/
if (cur > len)
cur = (size_t)len;
/* copy the data */
memcpy(buf, calc_addr(start_ofs), cur);
/* advance past this chunk */
len -= cur;
start_ofs += cur;
buf += cur;
}
}
/*
* Reserve space
*/
ulong CTcDataStream::reserve(size_t len)
{
ulong ret;
/* we'll always return the offset current before the call */
ret = ofs_;
/* if we have space on the current page, it's easy */
if (len <= rem_)
{
/* advance the output pointers */
ofs_ += len;
wp_ += len;
rem_ -= len;
}
else
{
/* keep going until we satisfy the request */
do
{
size_t cur;
/* if necessary, allocate more memory */
if (rem_ == 0)
alloc_page();
/* limit this chunk to the space remaining on the current page */
cur = len;
if (cur > rem_)
cur = rem_;
/* skip past this chunk */
ofs_ += cur;
wp_ += cur;
rem_ -= cur;
len -= cur;
} while (len != 0);
}
/* return the starting offset */
return ret;
}
/*
* Append data from another stream. The source stream is permanently
* moved to the new stream, destroying the original stream.
*/
void CTcDataStream::append_stream(CTcDataStream *stream)
{
ulong rem;
ulong ofs;
ulong start_ofs;
CTcStreamAnchor *anchor;
CTcStreamAnchor *nxt;
/* remember the starting offset of the copy in my stream */
start_ofs = get_ofs();
/* copy all data from the other stream */
for (ofs = 0, rem = stream->get_ofs() ; rem != 0 ; )
{
ulong request;
const char *ptr;
ulong actual;
/*
* request as much as possible from the other stream, up to the
* remaining length or 64k, whichever is smaller
*/
request = 65535;
if (rem < request)
request = rem;
/* get the chunk from the source stream */
ptr = stream->get_block_ptr(ofs, request, &actual);
/*
* write this chunk (which we know is less than 64k and can thus
* be safely cast to size_t, even on 16-bit machines)
*/
write(ptr, (size_t)actual);
/* advance our counters */
rem -= actual;
ofs += actual;
}
/*
* Now copy all of the anchors from the source stream to our stream.
* This will ensure that fixups in the other stream have
* corresponding fixups in this stream. Note that we must adjust
* the offset of each copied anchor by the offset of the start of
* the copied data in our stream.
*/
for (anchor = stream->get_first_anchor() ; anchor != 0 ; anchor = nxt)
{
/*
* remember the old link to the next anchor, since we're going
* to move the anchor to my list and thus forget about its
* position in the old list
*/
nxt = anchor->nxt_;
/* adjust the anchor's offset */
anchor->ofs_ += start_ofs;
/* unlink the anchor from its old stream */
anchor->nxt_ = 0;
/* link it in to my anchor list */
if (last_anchor_ != 0)
last_anchor_->nxt_ = anchor;
else
first_anchor_ = anchor;
last_anchor_ = anchor;
}
}
/*
* Write bytes to the stream
*/
void CTcDataStream::write(const char *buf, size_t len)
{
/*
* if possible, write it in one go (this is for efficiency, so that
* we can avoid making a few comparisons in the most common case)
*/
if (len <= rem_)
{
/* write the data */
memcpy(wp_, buf, len);
/* advance the output pointers */
ofs_ += len;
wp_ += len;
rem_ -= len;
}
else
{
/* keep going until we satisfy the request */
do
{
size_t cur;
/* if necessary, allocate more memory */
if (rem_ == 0)
alloc_page();
/* limit this chunk to the space remaining on the current page */
cur = len;
if (cur > rem_)
cur = rem_;
/* copy it to the page */
memcpy(wp_, buf, cur);
/* skip past the space written in the destination */
ofs_ += cur;
wp_ += cur;
rem_ -= cur;
/* advance past the space in the source */
buf += cur;
len -= cur;
} while (len != 0);
}
}
/*
* allocate a new page
*/
void CTcDataStream::alloc_page()
{
/*
* if we're coming back to a page that was previously allocated, we
* need merely re-establish the existing page
*/
if (page_cur_ + 1 < page_cnt_)
{
/* move to the next page */
++page_cur_;
/* start writing at the start of the page */
wp_ = pages_[page_cur_];
rem_ = TCCS_PAGE_SIZE;
/* we're done */
return;
}
/*
* if we don't have room for a new page in the page array, expand
* the page array
*/
if (page_cnt_ >= page_slots_)
{
/* increase the page slot count */
page_slots_ += 100;
/* allocate or reallocate the page array */
if (pages_ == 0)
pages_ = (char **)t3malloc(page_slots_ * sizeof(pages_[0]));
else
pages_ = (char **)t3realloc(pages_,
page_slots_ * sizeof(pages_[0]));
/* if that failed, throw an error */
if (pages_ == 0)
err_throw(TCERR_CODEGEN_NO_MEM);
}
/* allocate the new page */
pages_[page_cnt_] = (char *)t3malloc(TCCS_PAGE_SIZE);
/* throw an error if we couldn't allocate the page */
if (pages_[page_cnt_] == 0)
err_throw(TCERR_CODEGEN_NO_MEM);
/* start writing at the start of the new page */
wp_ = pages_[page_cnt_];
/* the entire page is free */
rem_ = TCCS_PAGE_SIZE;
/* make the new page the current page */
page_cur_ = page_cnt_;
/* count the new page */
++page_cnt_;
}
/*
* Add an absolute fixup for this stream at the current write offset.
*/
void CTcDataStream::add_abs_fixup(CTcAbsFixup **list_head)
{
/* add the fixup to the list at my current write location */
CTcAbsFixup::add_abs_fixup(list_head, this, get_ofs());
}
/*
* Add an anchor at the current offset.
*/
CTcStreamAnchor *CTcDataStream::add_anchor(CTcSymbol *owner_sym,
CTcAbsFixup **fixup_list_head,
ulong ofs)
{
CTcStreamAnchor *anchor;
/* allocate the anchor, giving it our current offset */
anchor = new (allocator_) CTcStreamAnchor(owner_sym,
fixup_list_head, ofs);
/* append it to our list */
if (last_anchor_ != 0)
last_anchor_->nxt_ = anchor;
else
first_anchor_ = anchor;
last_anchor_ = anchor;
/* return the new anchor */
return anchor;
}
/*
* Find an anchor with the given stream offset
*/
CTcStreamAnchor *CTcDataStream::find_anchor(ulong ofs) const
{
CTcStreamAnchor *cur;
/* scan the anchor list */
for (cur = first_anchor_ ; cur != 0 ; cur = cur->nxt_)
{
/* if this one has the desired offset, return it */
if (cur->get_ofs() == ofs)
return cur;
}
/* didn't find it */
return 0;
}
/*
* Write an object ID
*/
void CTcDataStream::write_obj_id(ulong obj_id)
{
/*
* if there's an object ID fixup list, and this is a valid object
* reference (not a 'nil' reference), add this reference
*/
if (G_keep_objfixups && obj_id != TCTARG_INVALID_OBJ)
CTcIdFixup::add_fixup(&G_objfixup, this, get_ofs(), obj_id);
/* write the ID */
write4(obj_id);
}
/*
* Write an object ID self-reference
*/
void CTcDataStream::write_obj_id_selfref(CTcSymObj *obj_sym)
{
/*
* Add a fixup list entry to the symbol. This type of reference
* must be kept with the symbol rather than in the global list,
* because we must apply this type of fixup each time we renumber
* the symbol.
*/
obj_sym->add_self_ref_fixup(this, get_ofs());
/* write the ID to the stream */
write4(obj_sym->get_obj_id());
}
/*
* Write a property ID
*/
void CTcDataStream::write_prop_id(uint prop_id)
{
/* if there's an object ID fixup list, add this reference */
if (G_keep_propfixups)
CTcIdFixup::add_fixup(&G_propfixup, this, get_ofs(), prop_id);
/* write the ID */
write2(prop_id);
}
/*
* Write an enumerator ID
*/
void CTcDataStream::write_enum_id(ulong enum_id)
{
/* if there's a fixup list, add this reference */
if (G_keep_enumfixups)
CTcIdFixup::add_fixup(&G_enumfixup, this, get_ofs(), enum_id);
/* write the ID */
write4(enum_id);
}
/*
* Given a stream ID, get the stream
*/
CTcDataStream *CTcDataStream::
get_stream_from_id(char stream_id, const textchar_t *obj_fname)
{
switch(stream_id)
{
case TCGEN_DATA_STREAM:
return G_ds;
case TCGEN_CODE_STREAM:
return G_cs_main;
case TCGEN_STATIC_CODE_STREAM:
return G_cs_static;
case TCGEN_OBJECT_STREAM:
return G_os;
case TCGEN_ICMOD_STREAM:
return G_icmod_stream;
case TCGEN_BIGNUM_STREAM:
return G_bignum_stream;
case TCGEN_REXPAT_STREAM:
return G_rexpat_stream;
case TCGEN_STATIC_INIT_ID_STREAM:
return G_static_init_id_stream;
case TCGEN_LCL_VAR_STREAM:
return G_lcl_stream;
default:
G_tcmain->log_error(0, 0, TC_SEV_ERROR,
TCERR_OBJFILE_INVAL_STREAM_ID, obj_fname);
return 0;
}
}
/* ------------------------------------------------------------------------ */
/*
* Code Stream
*/
/*
* create the code stream
*/
CTcCodeStream::CTcCodeStream(char stream_id)
: CTcDataStream(stream_id)
{
/* no switch yet */
cur_switch_ = 0;
/* no enclosing statement yet */
enclosing_ = 0;
/* no code body being generated yet */
code_body_ = 0;
/* start writing at offset zero */
ofs_ = 0;
/* no symbol tables yet */
symtab_ = 0;
goto_symtab_ = 0;
/* no labels yet */
active_lbl_ = 0;
free_lbl_ = 0;
/* no fixups yet */
free_fixup_ = 0;
/* allocate an initial set of line record pages */
line_pages_alloc_ = 0;
line_pages_ = 0;
alloc_line_pages(5);
/* no line records in use yet */
line_cnt_ = 0;
/* no local frame yet */
cur_frame_ = 0;
}
/*
* destroy the code stream
*/
CTcCodeStream::~CTcCodeStream()
{
size_t i;
/* release all active labels */
release_labels();
/* delete the line records pages */
for (i = 0 ; i < line_pages_alloc_ ; ++i)
t3free(line_pages_[i]);
/* delete the master list of pages */
t3free(line_pages_);
}
/*
* Set the current local frame
*/
CTcPrsSymtab *CTcCodeStream::set_local_frame(CTcPrsSymtab *symtab)
{
/* remember the original local frame, so we can return it later */
CTcPrsSymtab *old_frame = cur_frame_;
/* add the current byte code location to the outgoing frame */
if (old_frame != 0)
old_frame->add_to_range(ofs_ - method_ofs_);
/* remember the current frame */
cur_frame_ = symtab;
/* add it to the local frame list for the method if necessary */
add_local_frame(symtab);
/* add the current byte code location to the incoming frame */
symtab->add_to_range(ofs_ - method_ofs_);
/* return the original local frame */
return old_frame;
}
/*
* Reset
*/
void CTcCodeStream::reset()
{
/* inherit default */
CTcDataStream::reset();
/* clear the line records */
clear_line_recs();
/*
* forget all of the labels and fixups - they're allocated from the
* allocator pool, which we've completely reset now
*/
free_lbl_ = active_lbl_ = 0;
free_fixup_ = 0;
/* forget all of the symbol tables */
symtab_ = 0;
goto_symtab_ = 0;
/* forget the frame list */
frame_head_ = frame_tail_ = 0;
cur_frame_ = 0;
frame_cnt_ = 0;
/* forget all of the statement settings */
cur_switch_ = 0;
enclosing_ = 0;
code_body_ = 0;
/* presume 'self' is not available */
self_available_ = FALSE;
}
/*
* Allocate line record pages
*/
void CTcCodeStream::alloc_line_pages(size_t number_to_add)
{
size_t siz;
size_t i;
/* create or expand the master page array */
siz = (line_pages_alloc_ + number_to_add) * sizeof(tcgen_line_page_t *);
if (line_pages_ == 0)
line_pages_ = (tcgen_line_page_t **)t3malloc(siz);
else
line_pages_ = (tcgen_line_page_t **)t3realloc(line_pages_, siz);
/* allocate the new pages */
for (i = line_pages_alloc_ ; i < line_pages_alloc_ + number_to_add ; ++i)
{
/* allocate this page */
line_pages_[i] = (tcgen_line_page_t *)
t3malloc(sizeof(tcgen_line_page_t));
}
/* remember the new allocation */
line_pages_alloc_ += number_to_add;
}
/*
* Allocate a new label object
*/
CTcCodeLabel *CTcCodeStream::alloc_label()
{
CTcCodeLabel *ret;
/* if there's anything in the free list, use it */
if (free_lbl_ != 0)
{
/* take the first one off the free list */
ret = free_lbl_;
/* unlink it from the list */
free_lbl_ = free_lbl_->nxt;
}
else
{
/* allocate a new label */
ret = new (allocator_) CTcCodeLabel;
/* throw an error if allocation failed */
if (ret == 0)
err_throw(TCERR_CODEGEN_NO_MEM);
}
/* add the label to the active list */
ret->nxt = active_lbl_;
active_lbl_ = ret;
/* return the allocated label */
return ret;
}
/*
* Allocate a new fixup object
*/
CTcLabelFixup *CTcCodeStream::alloc_fixup()
{
CTcLabelFixup *ret;
/* if there's anything in the free list, use it */
if (free_fixup_ != 0)
{
/* take the first one off the free list */
ret = free_fixup_;
/* unlink it from the list */
free_fixup_ = free_fixup_->nxt;
}
else
{
/* allocate a new fixup */
ret = new (allocator_) CTcLabelFixup;
/* throw an error if allocation failed */
if (ret == 0)
err_throw(TCERR_CODEGEN_NO_MEM);
}
/* return the allocated fixup */
return ret;
}
/*
* Release all active labels. If any labels are undefined, log an
* internal error.
*/
void CTcCodeStream::release_labels()
{
int err_cnt;
/* we haven't found any errors yet */
err_cnt = 0;
/* run through the list of active labels */
while (active_lbl_ != 0)
{
CTcCodeLabel *lbl;
/* pull this label off of the active list */
lbl = active_lbl_;
active_lbl_ = active_lbl_->nxt;
/* put this label on the free list */
lbl->nxt = free_lbl_;
free_lbl_ = lbl;
/* check for unresolved fixups */
while (lbl->fhead != 0)
{
CTcLabelFixup *fixup;
/* pull this fixup off of the active list */
fixup = lbl->fhead;
lbl->fhead = lbl->fhead->nxt;
/* put this fixup on the free list */
fixup->nxt = free_fixup_;
free_fixup_ = fixup;
/* count the unresolved label */
++err_cnt;
}
}
/*
* if we found any unresolved fixups, log the error; there's not
* much point in logging each error individually, since this is an
* internal compiler error that the user can't do anything about,
* but at least give the user a count for compiler diagnostic
* purposes
*/
if (err_cnt != 0)
G_tcmain->log_error(0, 0, TC_SEV_INTERNAL,
TCERR_UNRES_TMP_FIXUP, err_cnt);
}
/*
* Allocate a new label at the current code offset
*/
CTcCodeLabel *CTcCodeStream::new_label_here()
{
CTcCodeLabel *lbl;
/* allocate a new label */
lbl = alloc_label();
/* set the label's location to the current write position */
lbl->ofs = ofs_;
lbl->is_known = TRUE;
/* return the new label */
return lbl;
}
/*
* Allocate a new forward-reference label
*/
CTcCodeLabel *CTcCodeStream::new_label_fwd()
{
CTcCodeLabel *lbl;
/* allocate a new label */
lbl = alloc_label();
/* the label's location is not yet known */
lbl->ofs = 0;
lbl->is_known = FALSE;
/* return the new label */
return lbl;
}
/*
* Define the position of a label, resolving any fixups associated with
* the label.
*/
void CTcCodeStream::def_label_pos(CTcCodeLabel *lbl)
{
/* set the label's position */
lbl->ofs = ofs_;
lbl->is_known = TRUE;
/* resolve each fixup */
while (lbl->fhead != 0)
{
CTcLabelFixup *fixup;
long diff;
char buf[4];
/* pull this fixup off of the active list */
fixup = lbl->fhead;
lbl->fhead = lbl->fhead->nxt;
/*
* calculate the offset from the fixup position to the label
* position, applying the bias to the fixup position
*/
diff = lbl->ofs - (fixup->ofs + fixup->bias);
/* convert the offset to the correct format and write it out */
if (fixup->is_long)
{
/* write an INT4 offset value */
oswp4(buf, diff);
write_at(fixup->ofs, buf, 4);
}
else
{
/* write an INT2 offset value */
oswp2s(buf, diff);
write_at(fixup->ofs, buf, 2);
}