Debian Codegraph @

terps/tads/tads3/tcgen.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/tcgen.h,v 1.4 1999/07/11 00:46:58 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 tcgen.h - TADS 3 Compiler code generator support classes
12 Function
13
14 Notes
15
16 Modified
17 05/08/99 MJRoberts - Creation
18 */
19
20 #ifndef TCGEN_H
21 #define TCGEN_H
22
23 #include "t3std.h"
24 #include "os.h"
25
26 #include "tctargty.h"
27
28 /* ------------------------------------------------------------------------ */
29 /*
30 * Stream ID's - an ID is associated with each data stream, and is used
31 * to identify the stream in an object file. These ID's are stored in
32 * object files, so any changes will render old object files obsolete
33 * and therefore require changing the object file signature's version
34 * number.
35 */
36
37 /* the constant pool data stream */
38 const char TCGEN_DATA_STREAM = 1;
39
40 /* the primary code pool data stream */
41 const char TCGEN_CODE_STREAM = 2;
42
43 /* the static object data stream */
44 const char TCGEN_OBJECT_STREAM = 3;
45
46 /*
47 * dictionary object data stream (not stored in an object file, because
48 * dictionaries aren't generated until link time)
49 */
50 const char TCGEN_DICT_STREAM = 4;
51
52 /*
53 * grammar production object data stream (not stored in an object file,
54 * because grammar production objects aren't generated until link time)
55 */
56 const char TCGEN_GRAMPROD_STREAM = 5;
57
58 /* BigNumber data stream */
59 const char TCGEN_BIGNUM_STREAM = 6;
60
61 /* IntrinsicClass data stream */
62 const char TCGEN_INTCLASS_STREAM = 7;
63
64 /* intrinsic class modifier data stream */
65 const char TCGEN_ICMOD_STREAM = 8;
66
67 /* static initializer code stream */
68 const char TCGEN_STATIC_CODE_STREAM = 9;
69
70 /*
71 * static initializer stream - contains a list of obj.prop identifiers
72 * for static initialization
73 */
74 const char TCGEN_STATIC_INIT_ID_STREAM = 10;
75
76 /* local variable name stream - for local variable frame information */
77 const char TCGEN_LCL_VAR_STREAM = 11;
78
79 /* RexPattern data stream */
80 const char TCGEN_REXPAT_STREAM = 12;
81
82
83 /* ------------------------------------------------------------------------ */
84 /*
85 * Data Stream. This object provides an in-memory byte stream.
86 *
87 * The data stream provides essentially a flat 32-bit address space, but
88 * dynamically allocates memory as needed, and works on 16-bit machines.
89 * The write pointer starts at offste zero, and is incremented by one
90 * for each byte written.
91 */
92
93 /*
94 * size in bytes of each page - we'll use a size that will work in
95 * 16-bit architectures
96 */
97 const size_t TCCS_PAGE_SIZE = 65000;
98
99 class CTcDataStream
100 {
101 public:
102 CTcDataStream(char stream_id);
103 virtual ~CTcDataStream();
104
105 /*
106 * get my stream ID - this is assigned during stream creation and is
107 * used to identify the stream in an object file
108 */
109 char get_stream_id() const { return stream_id_; }
110
111 /*
112 * Given a stream ID (TCGEN_xxx_STREAM), get the stream object.
113 * Logs an error if the stream ID is invalid.
114 */
115 static CTcDataStream *get_stream_from_id(char stream_id,
116 const textchar_t *obj_fname);
117
118 /* get the current write pointer offset */
119 ulong get_ofs() const { return ofs_; }
120
121 /*
122 * decrement the write offset - this can be used to remove one or
123 * more bytes from the stream (for peephole optimization, for
124 * example)
125 */
126 void dec_ofs(int amt);
127
128 /*
129 * Get a pointer to a block at a given offset and a given length.
130 * The block might not be returned contiguously, so we return how
131 * much data can be read at the returned pointer in
132 * (*available_len). If (*available_len) on return is less than
133 * requested_len, the caller must make a subsequent call, at (ofs +
134 * *available_len) of length (requested_len - *available_len), to
135 * read the next chunk; this process must be iterated until the
136 * entire request has been satisfied.
137 */
138 const char *get_block_ptr(ulong ofs,
139 ulong requested_len, ulong *available_len);
140
141 /* extract a chunk of the stream into the given buffer */
142 void copy_to_buf(char *buf, ulong start_ofs, ulong len);
143
144 /*
145 * Reserve space, advancing the write pointer without copying any
146 * data. The space written must eventually be written with
147 * write_at(), etc. Returns the offset of the start of the reserved
148 * space (which will always simply be the current offset before the
149 * call).
150 */
151 ulong reserve(size_t len);
152
153 /*
154 * Append data from another stream. The old stream is destroyed by
155 * this operation - the data are physically moved from the old
156 * stream to the new stream. Fixup information is moved along with
157 * the stream data.
158 */
159 void append_stream(CTcDataStream *stream);
160
161 /* write bytes to the stream */
162 void write(char b) { write(&b, 1); }
163 void write(const char *buf, size_t len);
164
165 /* write bytes at an earlier offset */
166 void write_at(ulong ofs, const char *buf, size_t len);
167 void write_at(ulong ofs, char b) { write_at(ofs, &b, 1); }
168
169 /* write a TADS portable UINT2 value */
170 void write2(uint val)
171 {
172 char buf[2];
173
174 /* convert the value to UINT2 format and write it out */
175 oswp2(buf, val);
176 write(buf, 2);
177 }
178
179 /* write a UINT2 at a given offset */
180 void write2_at(ulong ofs, uint val)
181 {
182 char buf[2];
183
184 /* convert the value to UINT2 format and write it out */
185 oswp2(buf, val);
186 write_at(ofs, buf, 2);
187 }
188
189 /* write a TADS portable UINT4 value */
190 void write4(ulong val)
191 {
192 char buf[4];
193
194 /* convert the value to UINT4 format and write it out */
195 oswp4(buf, val);
196 write(buf, 4);
197 }
198
199 /* write a TADS portable UINT4 value at a given offset */
200 void write4_at(ulong ofs, ulong val)
201 {
202 char buf[4];
203
204 /* convert the value to UINT4 format and write it out */
205 oswp4(buf, val);
206 write_at(ofs, buf, 4);
207 }
208
209 /*
210 * Write an object ID at the current offset. If there's a global
211 * object ID fixup list, we'll add this reference to the list.
212 */
213 void write_obj_id(ulong obj_id);
214
215 /*
216 * Write an object ID self-reference. This must be used when a
217 * modification of this object that assigns it a new object ID (not
218 * due to linking, but due to explicit replacement with the 'modify'
219 * statement) requires that this reference to the ID be changed to
220 * match the new object ID rather than referring to the replacement
221 * object.
222 */
223 void write_obj_id_selfref(class CTcSymObj *obj_sym);
224
225 /*
226 * Write a property ID at the current offset. If there's a global
227 * property ID fixup list, we'll add this reference to the list.
228 */
229 void write_prop_id(uint prop_id);
230
231 /* write an enum ID at the current offset, keeping fixups if needed */
232 void write_enum_id(ulong enum_id);
233
234 /* get the byte at the given offset */
235 char get_byte_at(ulong ofs)
236 {
237 return *calc_addr(ofs);
238 }
239
240 /* read an INT2 value at the given offst */
241 int read2_at(ulong ofs)
242 {
243 char buf[2];
244
245 /* read the two bytes */
246 buf[0] = get_byte_at(ofs);
247 buf[1] = get_byte_at(ofs + 1);
248 return osrp2s(buf);
249 }
250
251 /* read a UINT2 value at the given offset */
252 uint readu2_at(ulong ofs)
253 {
254 char buf[2];
255
256 /* read the two bytes */
257 buf[0] = get_byte_at(ofs);
258 buf[1] = get_byte_at(ofs + 1);
259 return osrp2(buf);
260 }
261
262 /* read an INT4 value at the given offset */
263 int read4_at(ulong ofs)
264 {
265 char buf[4];
266
267 /* read the four bytes */
268 buf[0] = get_byte_at(ofs);
269 buf[1] = get_byte_at(ofs + 1);
270 buf[2] = get_byte_at(ofs + 2);
271 buf[3] = get_byte_at(ofs + 3);
272 return osrp4s(buf);
273 }
274
275 /* read a UINT4 value at the given offset */
276 uint readu4_at(ulong ofs)
277 {
278 char buf[4];
279
280 /* read the four bytes */
281 buf[0] = get_byte_at(ofs);
282 buf[1] = get_byte_at(ofs + 1);
283 buf[2] = get_byte_at(ofs + 2);
284 buf[3] = get_byte_at(ofs + 3);
285 return t3rp4u(buf);
286 }
287
288 /*
289 * Add an absolute fixup at the current stream location to a given
290 * absolute fixup list. We'll create a new fixup object, record our
291 * current offset in the fixup so that we come back and fix this
292 * location, and add the fixup object to the given list.
293 */
294 void add_abs_fixup(struct CTcAbsFixup **list_head);
295
296 /* get the head of my list of anchors */
297 struct CTcStreamAnchor *get_first_anchor()
298 const { return first_anchor_; }
299
300 /*
301 * Add an anchor at the current offset. This should be used each
302 * time an atomic item is written to the stream.
303 *
304 * If the fixup list head pointer is not null, we will store fixups
305 * for the anchor in the given list. Otherwise, we'll provide an
306 * internal list head in the anchor object. Objects that are
307 * reachable through multiple references (such as an object
308 * associated with a symbol table entry) must typically keep their
309 * own fixup lists, because the fixup list might be needed before
310 * and after the data stream object is created. Objects that can
311 * only be reached through a single reference, which is created only
312 * when the data stream object is created, can use the internal
313 * fixup list instead.
314 *
315 * If an external fixup list is provided, the owning symbol table
316 * entry must be provided. This is necessary so that, when writing
317 * the anchor to an object file, we can also store a reference to
318 * the symbol that owns the list; this allows the association to be
319 * restored when the object file is loaded.
320 */
321 struct CTcStreamAnchor *add_anchor(class CTcSymbol *owner_sym,
322 struct CTcAbsFixup **fixup_list_head)
323 {
324 return add_anchor(owner_sym, fixup_list_head, get_ofs());
325 }
326
327 /* add an anchor at the given offset */
328 struct CTcStreamAnchor *add_anchor(class CTcSymbol *owner_sym,
329 struct CTcAbsFixup **fixup_list_head,
330 ulong ofs);
331
332 /* find an anchor at the given offset */
333 struct CTcStreamAnchor *find_anchor(ulong ofs) const;
334
335 /*
336 * Write the stream to an object file. Writes the stream data and
337 * the list of anchors and their fixups. Every fixup should be
338 * reachable from an anchor, and all of the anchors are in our
339 * anchor list.
340 */
341 void write_to_object_file(class CVmFile *fp);
342
343 /*
344 * Load an object file
345 */
346 void load_object_file(class CVmFile *fp, const textchar_t *obj_fname);
347
348 /*
349 * Get/set the object file starting offset - this is the base offset
350 * of the stream for the current object. Because streams can refer
351 * to one another, the object file loader must set the starting
352 * offset for all streams before reading the first stream for an
353 * object file. The 'set' call simply sets the starting offset to
354 * the current offset; this won't be affected by subsequent loading
355 * into the stream, so we have a stable base address for the object
356 * file data in the stream.
357 */
358 ulong get_object_file_start_ofs() const { return obj_file_start_ofs_; }
359 void set_object_file_start_ofs() { obj_file_start_ofs_ = ofs_; }
360
361 /*
362 * Reset - discard all data in the stream
363 */
364 virtual void reset();
365
366 protected:
367 /* allocate a new page */
368 void alloc_page();
369
370 /*
371 * Calculate the memory address of a given byte, given the byte's
372 * offset. The caller must be sure that the offset is less than the
373 * current write position, since this routine does not allocate new
374 * memory.
375 */
376 char *calc_addr(ulong ofs) const
377 {
378 /*
379 * get the page number by dividing the offset by the page size;
380 * get the offset in the page from the remainder of the same
381 * division
382 */
383 return pages_[ofs / TCCS_PAGE_SIZE] + (ofs % TCCS_PAGE_SIZE);
384 }
385
386 /* current write offset */
387 ulong ofs_;
388
389 /* start offset of the stream for the current object file's data */
390 ulong obj_file_start_ofs_;
391
392 /* current write pointer */
393 char *wp_;
394
395 /* space remaining on the current page */
396 size_t rem_;
397
398 /* current page */
399 size_t page_cur_;
400
401 /* array of pages */
402 char **pages_;
403
404 /* number of page slots */
405 size_t page_slots_;
406
407 /* number of pages allocated */
408 size_t page_cnt_;
409
410 /* head and tail of my list of anchors */
411 struct CTcStreamAnchor *first_anchor_;
412 struct CTcStreamAnchor *last_anchor_;
413
414 /*
415 * parser memory allocator - we use this for allocating label and
416 * fixup objects; these objects fit the characteristics used for the
417 * parser memory allocator, so we can get better memory management
418 * efficiency by using this allocator
419 */
420 class CTcPrsMem *allocator_;
421
422 /* my stream ID, for identification in the object file */
423 char stream_id_;
424 };
425
426 /* ------------------------------------------------------------------------ */
427 /*
428 * Debugging line record. This record stores information on one
429 * executable line of source code, associating the byte-code location of
430 * the code with the source file ID and line number, plus the frame
431 * identifier (which gives the local scope in effect for the line of
432 * code).
433 */
434 struct tcgen_line_t
435 {
436 /*
437 * Byte-code offset of the first instruction for this source line.
438 * This is expressed as an offset from the start of the method
439 * header, which means that this value is relative and thus doesn't
440 * need any adjustment for linking or any other changes to the
441 * absolute location where the code is stored.
442 */
443 uint ofs;
444
445 /*
446 * Source file ID and line number. The source file ID is the index
447 * of the file descriptor in the tokenizer's master source file
448 * list, so it must be adjusted when multiple object files are
449 * linked together for the fact that the file descriptor indices can
450 * change.
451 */
452 int source_id;
453 long source_line;
454
455 /*
456 * frame - this is the local scope frame for this line, which
457 * specifies the local symbol table in effect for the line of code
458 */
459 class CTcPrsSymtab *frame;
460 };
461
462 /* number of line records per page */
463 const size_t TCGEN_LINE_PAGE_SIZE = 1024;
464
465 /*
466 * Debugging line record page. We allocate blocks of line records for
467 * memory efficiency; this is a page of line numbers.
468 */
469 struct tcgen_line_page_t
470 {
471 /* underlying line records */
472 tcgen_line_t lines[TCGEN_LINE_PAGE_SIZE];
473 };
474
475
476 /* ------------------------------------------------------------------------ */
477 /*
478 * Code Stream. This object provides a place for code generators to
479 * store byte code. A code stream is an extension of a byte stream,
480 * with support for symbol table access; enclosing switch, loop, and
481 * 'try' block tracking; forward-reference labels; and relative jump
482 * generation.
483 */
484
485 /* code stream class */
486 class CTcCodeStream: public CTcDataStream
487 {
488 public:
489 CTcCodeStream(char stream_id);
490 ~CTcCodeStream();
491
492 /*
493 * Temporary label operations. A temporary label can be used to
494 * generate forward or reverse jumps.
495 */
496
497 /* allocate a new label at the current write offset */
498 struct CTcCodeLabel *new_label_here();
499
500 /*
501 * allocate a forward-reference label; the position will be defined
502 * later via the def_label_pos() method
503 */
504 struct CTcCodeLabel *new_label_fwd();
505
506 /*
507 * define the position of a label allocated as a forward reference
508 * to be the current write offset
509 */
510 void def_label_pos(struct CTcCodeLabel *lbl);
511
512 /*
513 * Remove a fixup at a particular location for a label. This can be
514 * used if an instruction is being removed (for optimization, for
515 * example).
516 */
517 void remove_fixup_at_ofs(struct CTcCodeLabel *lbl, ulong ofs);
518
519 /*
520 * Check a label's fixup list to see if it has a fixup at a
521 * particular code stream offset. Returns true if there is such a
522 * fixup, false if not.
523 */
524 int has_fixup_at_ofs(struct CTcCodeLabel *lbl, ulong ofs);
525
526 /*
527 * Release all active labels. Logs an internal error if any labels
528 * are still forward-declared.
529 */
530 void release_labels();
531
532 /*
533 * Get the current enclosing statement. This is used to find the
534 * target of a 'break' or 'continue', and is also used to invoke
535 * 'finally' blocks when leaving a nested block.
536 */
537 class CTPNStmEnclosing *get_enclosing() const { return enclosing_; }
538
539 /*
540 * Set the enclosing statement. During code generation, each time
541 * an enclosing statement is encountered, it should be set as the
542 * current enclosing statement for the duration of its code
543 * generation, so that its subnodes can find it. This routine
544 * returns the previous enclosing statement so that it can be
545 * restored later.
546 */
547 class CTPNStmEnclosing *set_enclosing(CTPNStmEnclosing *stm)
548 {
549 CTPNStmEnclosing *old_stm;
550
551 /* save the old one */
552 old_stm = enclosing_;
553
554 /* set the new one */
555 enclosing_ = stm;
556
557 /* return the old one */
558 return old_stm;
559 }
560
561 /*
562 * Set the current code body
563 */
564 class CTPNCodeBody *set_code_body(CTPNCodeBody *cb)
565 {
566 CTPNCodeBody *old_cb;
567
568 /* save the old one */
569 old_cb = code_body_;
570
571 /* set the new one */
572 code_body_ = cb;
573
574 /* return the old one */
575 return old_cb;
576 }
577
578 /* get the current code body being generated */
579 class CTPNCodeBody *get_code_body() const { return code_body_; }
580
581 /*
582 * Write the relative offset to the given label as a 2- or 4-byte
583 * value in TADS portable INT2 or INT4 format. The 'bias' value
584 * gives the offset from the current write pointer of the source
585 * position; the relative value written is thus:
586 *
587 * (label - (current + bias))
588 */
589 void write_ofs2(struct CTcCodeLabel *lbl, int bias)
590 { write_ofs(lbl, bias, FALSE); }
591 void write_ofs4(struct CTcCodeLabel *lbl, int bias)
592 { write_ofs(lbl, bias, TRUE); }
593
594 /*
595 * Set the enclosing "switch" statement node, returning the previous
596 * one to allow for later restoration
597 */
598 class CTPNStmSwitch *set_switch(class CTPNStmSwitch *sw)
599 {
600 class CTPNStmSwitch *old_sw;
601
602 /* remember the current switch node for a moment */
603 old_sw = cur_switch_;
604
605 /* set the new switch */
606 cur_switch_ = sw;
607
608 /* return the previous one */
609 return old_sw;
610 }
611
612 /* get the current switch */
613 class CTPNStmSwitch *get_switch() const { return cur_switch_; }
614
615 /* get the active symbol table */
616 class CTcPrsSymtab *get_symtab() const { return symtab_; }
617
618 /* get the active 'goto' symbol table */
619 class CTcPrsSymtab *get_goto_symtab() const { return goto_symtab_; }
620
621 /* set the active symbol table */
622 void set_symtab(class CTcPrsSymtab *symtab) { symtab_ = symtab; }
623
624 /* set the active 'goto' symbol table */
625 void set_goto_symtab(class CTcPrsSymtab *symtab)
626 { goto_symtab_ = symtab; }
627
628 /*
629 * get/self 'self' availability - if we're generating code for a
630 * stand-alone function, 'self' is not available; if we're
631 * generating code for an object method, 'self' is available
632 */
633 int is_self_available() const { return self_available_; }
634 void set_self_available(int f) { self_available_ = f; }
635
636 /* add a debugging line record at the current byte-code offset */
637 void add_line_rec(class CTcTokFileDesc *file, long linenum);
638
639 /* clear all line records */
640 void clear_line_recs() { line_cnt_ = 0; }
641
642 /* get the number of line records */
643 size_t get_line_rec_count() const { return line_cnt_; }
644
645 /* get the nth line record */
646 struct tcgen_line_t *get_line_rec(size_t n);
647
648 /* set the starting offset of the current method */
649 void set_method_ofs(ulong ofs) { method_ofs_ = ofs; }
650
651 /* clear the list of local frames in the current method */
652 void clear_local_frames()
653 {
654 /* clear the list */
655 frame_head_ = frame_tail_ = 0;
656
657 /* reset the count */
658 frame_cnt_ = 0;
659 }
660
661 /*
662 * Set the current local frame. This will add the frame to the
663 * master list of frames for the current method if it's not already
664 * there, and will establish the frame as the current local frame.
665 * Returns the previous local frame, so that the caller can restore
666 * the enclosing frame when leaving a nested frame.
667 */
668 class CTcPrsSymtab *set_local_frame(class CTcPrsSymtab *symtab);
669
670 /* get the local frame count for this method */
671 size_t get_frame_count() const { return frame_cnt_; }
672
673 /* get the head of the frame list for the method */
674 class CTcPrsSymtab *get_first_frame() const { return frame_head_; }
675
676 /* reset */
677 virtual void reset();
678
679 protected:
680 /*
681 * add a frame to the list of local frames; does nothing if the
682 * frame is already in the list for this method
683 */
684 void add_local_frame(class CTcPrsSymtab *symtab);
685
686 /* allocate additional line record pages */
687 void alloc_line_pages(size_t number_to_add);
688
689 /* allocate a label object */
690 struct CTcCodeLabel *alloc_label();
691
692 /* allocate a fixup object */
693 struct CTcLabelFixup *alloc_fixup();
694
695 /*
696 * Write an offset to the given label. If is_long is true, we'll
697 * write an INT4 offset; otherwise we'll write an INT2 offset value.
698 */
699 void write_ofs(struct CTcCodeLabel *lbl, int bias, int is_long);
700
701 /* head of list of active temporary labels */
702 struct CTcCodeLabel *active_lbl_;
703
704 /* head of list of free temporary label objects */
705 struct CTcCodeLabel *free_lbl_;
706
707 /* head of list of free label fixup objects */
708 struct CTcLabelFixup *free_fixup_;
709
710 /* current symbol table */
711 class CTcPrsSymtab *symtab_;
712
713 /* current 'goto' symbol table */
714 class CTcPrsSymtab *goto_symtab_;
715
716 /* current "switch" statement */
717 class CTPNStmSwitch *cur_switch_;
718
719 /* current enclosing statement */
720 class CTPNStmEnclosing *enclosing_;
721
722 /* current code body being generated */
723 class CTPNCodeBody *code_body_;
724
725 /* flag: 'self' is available in current code body */
726 unsigned int self_available_ : 1;
727
728 /* array of line record pages */
729 tcgen_line_page_t **line_pages_;
730 size_t line_pages_alloc_;
731
732 /* number of line records actually used */
733 size_t line_cnt_;
734
735 /* starting offst of current method */
736 ulong method_ofs_;
737
738 /* head and tail of list of local frames for the current method */
739 class CTcPrsSymtab *frame_head_;
740 class CTcPrsSymtab *frame_tail_;
741
742 /* number of frames in the local method list so far */
743 size_t frame_cnt_;
744
745 /* currently active local frame */
746 class CTcPrsSymtab *cur_frame_;
747 };
748
749 /* ------------------------------------------------------------------------ */
750 /*
751 * Code Stream Parser-Allocated Object - this is a base class for
752 * objects allocated by a CTcPrsMem allocator.
753 */
754 struct CTcCSPrsAllocObj
755 {
756 /* allocate via the parser allocator */
757 void *operator new(size_t siz, class CTcPrsMem *allocator);
758 };
759
760 /* ------------------------------------------------------------------------ */
761 /*
762 * Code label
763 */
764 struct CTcCodeLabel: CTcCSPrsAllocObj
765 {
766 CTcCodeLabel()
767 {
768 /* clear members */
769 nxt = 0;
770 ofs = 0;
771 fhead = 0;
772 is_known = FALSE;
773 }
774
775 /* next code label */
776 CTcCodeLabel *nxt;
777
778 /* offset of the code associated with the label */
779 ulong ofs;
780
781 /*
782 * head of list of fixups for this label; this will always be null
783 * once the label's offset is known, since we will resolve any
784 * existing fixups as soon as the label is defined and will add no
785 * further fixups after it's known, since we can generate correct
786 * offsets directly
787 */
788 struct CTcLabelFixup *fhead;
789
790 /* flag: true -> offset is known */
791 uint is_known : 1;
792 };
793
794 /*
795 * Code label fixup. Each time we generate a jump to a code label that
796 * hasn't been defined yet, we'll generate a fixup and attach it to the
797 * label. The fixup records the location of the jump; as soon as the
798 * label is defined, we'll go through all of the fixup records
799 * associated with the label, and fill in the correct jump offset.
800 */
801 struct CTcLabelFixup: CTcCSPrsAllocObj
802 {
803 CTcLabelFixup()
804 {
805 /* clear members */
806 nxt = 0;
807 ofs = 0;
808 bias = 0;
809 is_long = FALSE;
810 }
811
812 /* next fixup in same list */
813 CTcLabelFixup *nxt;
814
815 /* code offset of the jump in need of fixing */
816 ulong ofs;
817
818 /*
819 * bias to apply to jump offset (the value we will write is:
820 *
821 * (target - (ofs + bias))
822 */
823 int bias;
824
825 /* long jump - true -> INT4 offset, false -> INT2 offset */
826 uint is_long : 1;
827 };
828
829
830 /* ------------------------------------------------------------------------ */
831 /*
832 * Absolute Fixup. Each time we generate a reference to an offset in a
833 * stream, we must save the location so that we can go back and fix up
834 * the offset after the final image file layout configuration. We can't
835 * know until we've generated the entire program what the final location
836 * of any stream is, because we won't know how large we're going to make
837 * the pool pages until we've generated the whole program.
838 *
839 * Each absolute fixup is stored in a list anchored in the object that
840 * owns the object in the stream to which the fixup refers. During the
841 * image layout configuration phase, we go through the list of all pool
842 * blocks, and figure out where the pool block will go in the image
843 * file; once this is known, we apply all of the fixups for that
844 * block by scanning the block's fixup list.
845 */
846 struct CTcAbsFixup: CTcCSPrsAllocObj
847 {
848 /* next fixup in same list */
849 CTcAbsFixup *nxt;
850
851 /* stream containing the reference */
852 class CTcDataStream *ds;
853
854 /* location in stream 'ds' of the 4-byte pointer value to fix */
855 ulong ofs;
856
857 /*
858 * Add an absolute fixup, at a given offset in a given stream, to a
859 * given list.
860 */
861 static void add_abs_fixup(struct CTcAbsFixup **list_head,
862 CTcDataStream *ds, ulong ofs);
863
864 /*
865 * Fix up a fix-up list, given the final address of the referenced
866 * object. Scans the fix-up list and stores the final address at
867 * each location in the list.
868 */
869 static void fix_abs_fixup(struct CTcAbsFixup *list_head,
870 ulong final_ofs);
871
872 /* write a fixup list to an object file */
873 static void write_fixup_list_to_object_file(class CVmFile *fp,
874 CTcAbsFixup *list_head);
875
876 /* load a fixup list from an object file */
877 static void
878 load_fixup_list_from_object_file(class CVmFile *fp,
879 const textchar_t *obj_fname,
880 CTcAbsFixup **list_head);
881
882 };
883
884 /* ------------------------------------------------------------------------ */
885 /*
886 * ID Fixup. We use this to track a reference to an object ID or
887 * property ID. These fixups are needed when combining object files
888 * that were compiled separately, since we must reconcile the ID name
889 * spaces of the multiple files.
890 */
891
892 /* translation datatypes */
893 enum tcgen_xlat_type
894 {
895 /* object ID's - translation table type is (tctarg_obj_id_t *) */
896 TCGEN_XLAT_OBJ,
897
898 /* property ID's - translation table type is (tctarg_prop_id_t *) */
899 TCGEN_XLAT_PROP,
900
901 /* enum's - translation table type is (ulong *) */
902 TCGEN_XLAT_ENUM
903 };
904
905 /*
906 * ID fixup class
907 */
908 struct CTcIdFixup: CTcCSPrsAllocObj
909 {
910 /* initialize */
911 CTcIdFixup(class CTcDataStream *ds, ulong ofs, ulong id)
912 {
913 /* remember the data */
914 ds_ = ds;
915 ofs_ = ofs;
916 id_ = id;
917
918 /* we're not in a list yet */
919 nxt_ = 0;
920 }
921
922 /* add a fixup to a fixup list */
923 static void add_fixup(CTcIdFixup **list_head, class CTcDataStream *ds,
924 ulong ofs, ulong id);
925
926 /* write a fixup list to an object file */
927 static void write_to_object_file(class CVmFile *fp, CTcIdFixup *head);
928
929 /*
930 * Load an ID fixup list from an object file.
931 *
932 * 'xlat' is the translation array; it consists of elements of the
933 * appropriate type, depending on xlat_type.
934 *
935 * 'xlat_cnt' is the number of elements in the 'xlat' array.
936 *
937 * 'stream_element_size' is the size of the stream data elements
938 * that we're fixing up. This can be 2 for a UINT2 value, or 4 for
939 * a UINT4 value.
940 *
941 * 'fname' is the object filename, which we need for reporting
942 * errors that we encounter in the fixup data.
943 *
944 * If 'fixup_list_head' is provided, it points to the head of a list
945 * that we use to store new fixups. For each fixup we read, we
946 * create a new fixup referring to the same element. This is needed
947 * if the file we're reading will be turned back into another object
948 * file at some point and needs relative object ID information to be
949 * stored.
950 */
951 static void load_object_file(class CVmFile *fp,
952 const void *xlat, ulong xlat_cnt,
953 tcgen_xlat_type xlat_type,
954 size_t stream_element_size,
955 const textchar_t *fname,
956 CTcIdFixup **fixup_list_head);
957
958 /*
959 * apply the fixup, given the final ID to use, and the size of the
960 * data item to write (2 for a UINT2, 4 for a UINT4)
961 */
962 void apply_fixup(ulong final_id, size_t siz);
963
964 /* the stream containing the reference to this object */
965 class CTcDataStream *ds_;
966
967 /* the offset in the stream of the reference */
968 ulong ofs_;
969
970 /*
971 * the local ID - this is the ID that is used locally in the
972 * separate file before it's translated to the final value global to
973 * the combined files
974 */
975 ulong id_;
976
977 /* next fixup in the list */
978 CTcIdFixup *nxt_;
979 };
980
981
982 /* ------------------------------------------------------------------------ */
983 /*
984 * Data Stream Anchor. Each time we add an object to a data stream, we
985 * must add an anchor to the stream. The anchor tracks the indivisible
986 * object and all references (via a fixup list) to the object.
987 */
988 struct CTcStreamAnchor: CTcCSPrsAllocObj
989 {
990 CTcStreamAnchor(class CTcSymbol *fixup_owner_sym,
991 struct CTcAbsFixup **fixup_list_head, ulong stream_ofs)
992 {
993 /* we're not in a list yet */
994 nxt_ = 0;
995
996 /*
997 * if the caller provided an external fixup list head pointer,
998 * use it; otherwise, use our internal fixup list
999 */
1000 if (fixup_list_head != 0)
1001 {
1002 /* use the external fixup list */
1003 fixup_list_head_ = fixup_list_head;
1004
1005 /* remember the owning symbol */
1006 fixup_info_.fixup_owner_sym_ = fixup_owner_sym;
1007 }
1008 else
1009 {
1010 /* use our internal fixup list */
1011 fixup_list_head_ = &fixup_info_.internal_fixup_head_;
1012
1013 /* we have no fixup items in our internal list yet */
1014 fixup_info_.internal_fixup_head_ = 0;
1015 }
1016
1017 /* remember the stream offset */
1018 ofs_ = stream_ofs;
1019
1020 /* our pool address is not yet known */
1021 addr_ = 0;
1022
1023 /* we're not yet replaced */
1024 replaced_ = FALSE;
1025 }
1026
1027 /*
1028 * Detach from our symbol, switching to our internal fixup list. We
1029 * leave any fixups that were previously associated with our symbol
1030 * with the symbol, so this detaches us from any fixups currently
1031 * pointing to me. This is useful for replacing and modifying symbols,
1032 * since it allows the anchor for the original definition to be
1033 * dissociated from the symbol, so that the symbol can be reused with a
1034 * different anchor.
1035 */
1036 void detach_from_symbol()
1037 {
1038 /* switch to our internal fixup list */
1039 fixup_list_head_ = &fixup_info_.internal_fixup_head_;
1040
1041 /* we have nothing in our list yet */
1042 fixup_info_.internal_fixup_head_ = 0;
1043 }
1044
1045 /* get my offset - this is the stream offset, not the final address */
1046 ulong get_ofs() const { return ofs_; }
1047
1048 /*
1049 * Get my final address - valid only after the linking phase (as
1050 * defined and performed by the target-specific code generator).
1051 * The exact meaning of this address is defined by the
1052 * target-specific code generator, and this value is meant for use
1053 * exclusively by the target code generator; the target-independent
1054 * part of the compiler should have no use for this value.
1055 */
1056 ulong get_addr() const { return addr_; }
1057
1058 /*
1059 * Set the address - this should be invoked by the target-specific
1060 * code generator during the link phase when the final address of
1061 * the stream object is known. This will store the address in the
1062 * anchor, and will apply all of the outstanding fixups for the
1063 * object.
1064 */
1065 void set_addr(ulong addr);
1066
1067 /*
1068 * Get the length. We don't separately track the length, because
1069 * the length can be deduced from the offset of the next item (or
1070 * from the length of the stream, when the item is the last item in
1071 * the stream).
1072 */
1073 ulong get_len(class CTcDataStream *ds) const;
1074
1075 /*
1076 * Get the symbol table entry that owns my fixup list, if the fixup
1077 * list is external. Returns null if the fixup list is internal.
1078 */
1079 class CTcSymbol *get_fixup_owner_sym() const
1080 {
1081 return (fixup_list_head_ == &fixup_info_.internal_fixup_head_
1082 ? 0 : fixup_info_.fixup_owner_sym_);
1083 }
1084
1085 /* get/set the 'replaced' flag */
1086 int is_replaced() const { return replaced_; }
1087 void set_replaced(int f) { replaced_ = f; }
1088
1089 /* next anchor in same list */
1090 CTcStreamAnchor *nxt_;
1091
1092 /*
1093 * Pointer to fixup list head for the object - this is a list of the
1094 * fixups for references to this object. Because an object might
1095 * have a fixup list before the object is added to the data stream
1096 * (anything that is reachable through multiple references, such as
1097 * a function in a symbol table, could have such a fixup list),
1098 * we'll set this to refer to the external fixup list. Otherwise,
1099 * we'll set it to point to our own internal fixup list head.
1100 */
1101 struct CTcAbsFixup **fixup_list_head_;
1102
1103 /*
1104 * If we have an external fixup list, we need to store the symbol
1105 * table entry that owns the fixup list; otherwise, we must store
1106 * the fixup list head. Since we only need one or the other,
1107 * compress the storage into a union.
1108 */
1109 union
1110 {
1111 /*
1112 * internal fixup list - this is used if no external fixup list
1113 * exists for the object
1114 */
1115 struct CTcAbsFixup *internal_fixup_head_;
1116
1117 /* owning symbol table entry, if the fixup list is external */
1118 class CTcSymbol *fixup_owner_sym_;
1119 } fixup_info_;
1120
1121 /* offset of this object in the stream */
1122 ulong ofs_;
1123
1124 /*
1125 * final absolute address - this won't be known until the layout of
1126 * the pool or data segment containing this object is calculated,
1127 * which happens during the link phase
1128 */
1129 ulong addr_;
1130
1131 /*
1132 * Flag: this code block has been replaced by another one. This is
1133 * set when the function or property that owns this code is replaced
1134 * by another implementation; when this is set, the code block
1135 * should not be written to the image file.
1136 */
1137 unsigned int replaced_ : 1;
1138 };
1139
1140 /* ------------------------------------------------------------------------ */
1141 /*
1142 * Dynamic compiler interface to the live VM. The compiler uses this to
1143 * access VM resources required during compilation.
1144 */
1145 class CTcVMIfc
1146 {
1147 public:
1148 virtual ~CTcVMIfc() { }
1149
1150 /*
1151 * Add a generated object. This creates a live object in the VM and
1152 * returns its ID.
1153 */
1154 virtual tctarg_obj_id_t new_obj(tctarg_obj_id_t cls) = 0;
1155
1156 /* validate an object ID */
1157 virtual int validate_obj(tctarg_obj_id_t obj) = 0;
1158
1159 /*
1160 * Add a property value to a generated object.
1161 */
1162 virtual void set_prop(tctarg_obj_id_t obj, tctarg_prop_id_t prop,
1163 const class CTcConstVal *val) = 0;
1164
1165 /* validate a property ID */
1166 virtual int validate_prop(tctarg_prop_id_t prop) = 0;
1167
1168 /* validate a built-in function pointer */
1169 virtual int validate_bif(uint set_index, uint func_index) = 0;
1170
1171 /* validate a constant pool address for a string/list */
1172 virtual int validate_pool_str(uint32_t pool_ofs) = 0;
1173 virtual int validate_pool_list(uint32_t pool_ofs) = 0;
1174 };
1175
1176 #endif /* TCGEN_H */
1177