Debian Codegraph @

terps/tads/tads3/tct3prg.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/TCT3STM.CPP,v 1.1 1999/07/11 00:46:57 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 tct3stm.cpp - TADS 3 Compiler - T3 VM Code Generator - program-level classes
15 Function
16 Generate code for the T3 VM. This file contains program-level classes,
17 in order to segregate the code generation classes required for the full
18 compiler from those required for subsets that require only expression
19 parsing (such as debuggers). The program-level classes are for statements
20 at the outer level, outside of executable code: object definitions,
21 dictionary declarations, etc. This also includes code to read and
22 write object files.
23 Notes
24
25 Modified
26 05/08/99 MJRoberts - Creation
27 */
28
29 #include <stdio.h>
30
31 #include "t3std.h"
32 #include "os.h"
33 #include "tcprs.h"
34 #include "tct3.h"
35 #include "tcgen.h"
36 #include "vmtype.h"
37 #include "vmwrtimg.h"
38 #include "vmfile.h"
39 #include "tcmain.h"
40 #include "tcerr.h"
41
42
43
44 /* ------------------------------------------------------------------------ */
45 /*
46 * Object Definition Statement
47 */
48
49 /*
50 * given the offset of the start of an object in the compiled object
51 * stream, get the offset of the first property
52 */
53 ulong CTPNStmObject::get_stream_first_prop_ofs(CTcDataStream *stream,
54 ulong obj_ofs)
55 {
56 /*
57 * the property table follows the superclass table, which follows
58 * the tads-object header; the superclass table contains 4 bytes per
59 * superclass (we can obtain the superclass count from the stream
60 * data)
61 */
62 return obj_ofs + TCT3_TADSOBJ_SC_OFS
63 + (get_stream_sc_cnt(stream, obj_ofs) * 4);
64 }
65
66 /*
67 * given the offset of the start of an object in the compiled object
68 * stream, get the offset of the property at the given index
69 */
70 ulong CTPNStmObject::get_stream_prop_ofs(CTcDataStream *stream,
71 ulong obj_ofs, uint idx)
72 {
73 /*
74 * calculate the offset to the selected property from the start of
75 * the property table
76 */
77 return get_stream_first_prop_ofs(stream, obj_ofs)
78 + (TCT3_TADSOBJ_PROP_SIZE * idx);
79 }
80
81 /*
82 * given the offset of the start of an object in the compiled object
83 * stream, get the number of properties in the stream data
84 */
85 uint CTPNStmObject::get_stream_prop_cnt(CTcDataStream *stream,
86 ulong obj_ofs)
87 {
88 /* the property count is at offset 2 in the tads-object header */
89 return stream->readu2_at(obj_ofs + TCT3_TADSOBJ_HEADER_OFS + 2);
90 }
91
92 /*
93 * given the offset of the start of an object in the compiled object
94 * stream, set the number of properties in the stream data, adjusting
95 * the data size in the metaclass header to match
96 */
97 size_t CTPNStmObject::set_stream_prop_cnt(CTcDataStream *stream,
98 ulong obj_ofs, uint prop_cnt)
99 {
100 size_t data_size;
101
102 /* the property count is at offset 2 in the tads-object header */
103 stream->write2_at(obj_ofs + TCT3_TADSOBJ_HEADER_OFS + 2, prop_cnt);
104
105 /*
106 * calculate the new data size to store in the metaclass header --
107 * this is the size of the tads-object header, plus the size of the
108 * superclass table (4 bytes per superclass), plus the size of the
109 * property table
110 */
111 data_size = TCT3_TADSOBJ_HEADER_SIZE
112 + (get_stream_sc_cnt(stream, obj_ofs) * 4)
113 + (prop_cnt * TCT3_TADSOBJ_PROP_SIZE);
114
115 /* write the data size to the metaclass header (it's at offset 4) */
116 stream->write2_at(obj_ofs + TCT3_META_HEADER_OFS + 4, data_size);
117
118 /* return the new data size */
119 return data_size;
120 }
121
122 /*
123 * Get the object flags from an object in a compiled stream
124 */
125 uint CTPNStmObject::get_stream_obj_flags(CTcDataStream *stream,
126 ulong obj_ofs)
127 {
128 /* the flags are at offset 4 in the tads-object header */
129 return stream->read2_at(obj_ofs + TCT3_TADSOBJ_HEADER_OFS + 4);
130 }
131
132 /*
133 * Set the object flags in an object in a compiled stream
134 */
135 void CTPNStmObject::set_stream_obj_flags(CTcDataStream *stream,
136 ulong obj_ofs, uint flags)
137 {
138 /*
139 * write the new flags - they're at offset 4 in the tads-object
140 * header
141 */
142 stream->write2_at(obj_ofs + TCT3_TADSOBJ_HEADER_OFS + 4, flags);
143 }
144
145 /*
146 * given the offset of the start of an object in the compiled object
147 * stream, get the number of superclasses in the stream data
148 */
149 uint CTPNStmObject::get_stream_sc_cnt(CTcDataStream *stream,
150 ulong obj_ofs)
151 {
152 /* the superclass count is at offset 0 in the tads-object header */
153 return stream->readu2_at(obj_ofs + TCT3_TADSOBJ_HEADER_OFS + 0);
154 }
155
156 /*
157 * given the stream offset of the start of an object in the compiled
158 * object stream, change a superclass object ID
159 */
160 void CTPNStmObject::set_stream_sc(CTcDataStream *stream, ulong obj_ofs,
161 uint sc_idx, vm_obj_id_t new_sc)
162 {
163 /*
164 * set the superclass - it's at offset 6 in the object data, plus
165 * four bytes (UINT4) per index slot
166 */
167 stream->write2_at(obj_ofs + TCT3_TADSOBJ_HEADER_OFS + 6 + (sc_idx * 4),
168 new_sc);
169 }
170
171 /*
172 * given the offset of the start of an object in the compiled object
173 * stream, get the property ID of the property at a given index in the
174 * property table
175 */
176 vm_prop_id_t CTPNStmObject::get_stream_prop_id(CTcDataStream *stream,
177 ulong obj_ofs, uint prop_idx)
178 {
179 ulong prop_ofs;
180
181 /* get the property's data offset */
182 prop_ofs = get_stream_prop_ofs(stream, obj_ofs, prop_idx);
183
184 /* read the property ID - it's at offset 0 in the property data */
185 return (vm_prop_id_t)stream->readu2_at(prop_ofs);
186 }
187
188 /*
189 * given the offset of the start of an object in the compiled object
190 * stream, get the datatype of the property at the given index in the
191 * property table
192 */
193 vm_datatype_t CTPNStmObject::
194 get_stream_prop_type(CTcDataStream *stream, ulong obj_ofs, uint prop_idx)
195 {
196 ulong dh_ofs;
197
198 /* get the property's data holder offset */
199 dh_ofs = get_stream_prop_val_ofs(stream, obj_ofs, prop_idx);
200
201 /* the type is the first byte of the serialized data holder */
202 return (vm_datatype_t)stream->get_byte_at(dh_ofs);
203 }
204
205 /*
206 * given the offset of the start of an object in the compiled object
207 * stream, get the stream offset of the serialized DATAHOLDER structure
208 * for the property at the given index in the property table
209 */
210 ulong CTPNStmObject::get_stream_prop_val_ofs(CTcDataStream *stream,
211 ulong obj_ofs, uint prop_idx)
212 {
213 ulong prop_ofs;
214
215 /* get the property's data offset */
216 prop_ofs = get_stream_prop_ofs(stream, obj_ofs, prop_idx);
217
218 /* the data holder immediately follows the (UINT2) property ID */
219 return prop_ofs + 2;
220 }
221
222
223 /*
224 * given the offset of the start of an object in the compiled object
225 * stream, get the property ID of the property at a given index in the
226 * property table
227 */
228 void CTPNStmObject::set_stream_prop_id(CTcDataStream *stream,
229 ulong obj_ofs, uint prop_idx,
230 vm_prop_id_t new_id)
231 {
232 ulong prop_ofs;
233
234 /* get the property's data offset */
235 prop_ofs = get_stream_prop_ofs(stream, obj_ofs, prop_idx);
236
237 /* set the data */
238 stream->write2_at(prop_ofs, (uint)new_id);
239 }
240