| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | ""; |
| 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 - file operations |
| 15 | Function |
| 16 | This module provides the file operations for the tcgen classes. These |
| 17 | are separated out here because they're not needed in builds that don't read |
| 18 | or write symbol or object files, such as debugger and eval()-equipped |
| 19 | interpreter builds. |
| 20 | Notes |
| 21 | |
| 22 | Modified |
| 23 | 05/09/99 MJRoberts - Creation |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "t3std.h" |
| 31 | #include "os.h" |
| 32 | #include "tcglob.h" |
| 33 | #include "tcgen.h" |
| 34 | #include "vmerr.h" |
| 35 | #include "tcerrnum.h" |
| 36 | #include "tctok.h" |
| 37 | #include "tcprs.h" |
| 38 | #include "tcmain.h" |
| 39 | #include "vmfile.h" |
| 40 | #include "tctarg.h" |
| 41 | |
| 42 | |
| 43 | /* ------------------------------------------------------------------------ */ |
| 44 | /* |
| 45 | * Data streams |
| 46 | */ |
| 47 | |
| 48 | /* |
| 49 | * Write the stream, its anchors, and its fixups to an object file. |
| 50 | */ |
| 51 | void CTcDataStream::write_to_object_file(CVmFile *fp) |
| 52 | { |
| 53 | ulong ofs; |
| 54 | CTcStreamAnchor *anchor; |
| 55 | long cnt; |
| 56 | |
| 57 | /* |
| 58 | * First, write the data stream bytes. Write the length prefix |
| 59 | * followed by the data. Just blast the whole thing out in one huge |
| 60 | * byte stream, one page at a time. |
| 61 | */ |
| 62 | fp->write_uint4(ofs_); |
| 63 | |
| 64 | /* write the data one page at a time */ |
| 65 | for (ofs = 0 ; ofs < ofs_ ; ) |
| 66 | { |
| 67 | size_t cur; |
| 68 | |
| 69 | /* |
| 70 | * write out one whole page, or the balance of the current page if |
| 71 | * we have less than a whole page remaining |
| 72 | */ |
| 73 | cur = TCCS_PAGE_SIZE; |
| 74 | if (ofs + cur > ofs_) |
| 75 | cur = (size_t)(ofs_ - ofs); |
| 76 | |
| 77 | /* write out this chunk */ |
| 78 | fp->write_bytes(calc_addr(ofs), cur); |
| 79 | |
| 80 | /* move to the next page's offset */ |
| 81 | ofs += cur; |
| 82 | } |
| 83 | |
| 84 | /* count the anchors */ |
| 85 | for (cnt = 0, anchor = first_anchor_ ; anchor != 0 ; |
| 86 | anchor = anchor->nxt_, ++cnt) ; |
| 87 | |
| 88 | /* write the count */ |
| 89 | fp->write_uint4(cnt); |
| 90 | |
| 91 | /* |
| 92 | * Write all of the anchors, and all of their fixups. (We have the |
| 93 | * code to write the anchor and fixup information in-line here for |
| 94 | * efficiency - there will normally be a large number of these tiny |
| 95 | * objects, so anything we can do to improve the speed of this loop |
| 96 | * will help quite a lot in the overall write performance.) |
| 97 | */ |
| 98 | for (anchor = first_anchor_ ; anchor != 0 ; anchor = anchor->nxt_) |
| 99 | { |
| 100 | char buf[6]; |
| 101 | |
| 102 | /* write the stream offset */ |
| 103 | oswp4(buf, anchor->get_ofs()); |
| 104 | |
| 105 | /* |
| 106 | * If the anchor has an external fixup list, write the symbol's |
| 107 | * name to the file; otherwise write a zero length to indicate that |
| 108 | * we have an internal fixup list. |
| 109 | */ |
| 110 | if (anchor->get_fixup_owner_sym() == 0) |
| 111 | { |
| 112 | /* no external list - indicate with a zero symbol length */ |
| 113 | oswp2(buf+4, 0); |
| 114 | fp->write_bytes(buf, 6); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | /* external list - write the symbol length and name */ |
| 119 | oswp2(buf+4, anchor->get_fixup_owner_sym()->get_sym_len()); |
| 120 | fp->write_bytes(buf, 6); |
| 121 | fp->write_bytes(anchor->get_fixup_owner_sym()->get_sym(), |
| 122 | anchor->get_fixup_owner_sym()->get_sym_len()); |
| 123 | } |
| 124 | |
| 125 | /* write the fixup list */ |
| 126 | CTcAbsFixup:: |
| 127 | write_fixup_list_to_object_file(fp, *anchor->fixup_list_head_); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Read a stream from an object file |
| 133 | */ |
| 134 | void CTcDataStream::load_object_file(CVmFile *fp, |
| 135 | const textchar_t *fname) |
| 136 | { |
| 137 | ulong stream_len; |
| 138 | ulong rem; |
| 139 | char buf[1024]; |
| 140 | ulong start_ofs; |
| 141 | ulong anchor_cnt; |
| 142 | |
| 143 | /* read the length of the stream */ |
| 144 | stream_len = fp->read_uint4(); |
| 145 | |
| 146 | /* remember my starting offset */ |
| 147 | start_ofs = get_ofs(); |
| 148 | |
| 149 | /* read the stream bytes */ |
| 150 | for (rem = stream_len ; rem != 0 ; ) |
| 151 | { |
| 152 | size_t cur; |
| 153 | |
| 154 | /* read up to a buffer-full, or however much is left */ |
| 155 | cur = sizeof(buf); |
| 156 | if (cur > rem) |
| 157 | cur = rem; |
| 158 | |
| 159 | /* read this chunk */ |
| 160 | fp->read_bytes(buf, cur); |
| 161 | |
| 162 | /* add this chunk to the stream */ |
| 163 | write(buf, cur); |
| 164 | |
| 165 | /* deduct the amount we've read from the amount remaining */ |
| 166 | rem -= cur; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Read the anchors. For each anchor, we must fix up the anchor by |
| 171 | * adding the base address of the stream we just read - the original |
| 172 | * anchor offsets in the object file reflect a base stream offset of |
| 173 | * zero, but we could be loading the stream after a bunch of other data |
| 174 | * have already been loaded into the stream. |
| 175 | * |
| 176 | * First, read the number of anchors, then loop through the anchors and |
| 177 | * read each one. |
| 178 | */ |
| 179 | for (anchor_cnt = fp->read_uint4() ; anchor_cnt != 0 ; |
| 180 | --anchor_cnt) |
| 181 | { |
| 182 | ulong anchor_ofs; |
| 183 | size_t sym_len; |
| 184 | CTcStreamAnchor *anchor; |
| 185 | |
| 186 | /* read this anchor */ |
| 187 | fp->read_bytes(buf, 6); |
| 188 | |
| 189 | /* get the offset, and adjust for the new stream base offset */ |
| 190 | anchor_ofs = t3rp4u(buf) + start_ofs; |
| 191 | |
| 192 | /* get the length of the owning symbol's name, if any */ |
| 193 | sym_len = osrp2(buf+4); |
| 194 | |
| 195 | /* if there's a symbol name, read it */ |
| 196 | if (sym_len != 0) |
| 197 | { |
| 198 | CTcSymbol *owner_sym; |
| 199 | |
| 200 | /* read the symbol name */ |
| 201 | fp->read_bytes(buf, sym_len); |
| 202 | |
| 203 | /* look it up in the global symbol table */ |
| 204 | owner_sym = G_prs->get_global_symtab()->find(buf, sym_len); |
| 205 | if (owner_sym == 0) |
| 206 | { |
| 207 | /* |
| 208 | * the owner symbol doesn't exist - this is an internal |
| 209 | * inconsistency in the object file, because the anchor |
| 210 | * symbol must always be defined in the same file and hence |
| 211 | * should have been loaded already; complain and go on |
| 212 | */ |
| 213 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 214 | TCERR_OBJFILE_INT_SYM_MISSING, |
| 215 | (int)sym_len, buf, fname); |
| 216 | |
| 217 | /* we can't create the anchor */ |
| 218 | anchor = 0; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | /* create the anchor based on the symbol */ |
| 223 | anchor = add_anchor(owner_sym, |
| 224 | owner_sym->get_fixup_list_anchor(), |
| 225 | anchor_ofs); |
| 226 | |
| 227 | /* set the anchor in the symbol */ |
| 228 | owner_sym->set_anchor(anchor); |
| 229 | } |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | /* create the anchor with no external references */ |
| 234 | anchor = add_anchor(0, 0, anchor_ofs); |
| 235 | } |
| 236 | |
| 237 | /* load the fixup list */ |
| 238 | CTcAbsFixup:: |
| 239 | load_fixup_list_from_object_file(fp, fname, |
| 240 | anchor->fixup_list_head_); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /* ------------------------------------------------------------------------ */ |
| 248 | /* |
| 249 | * Absolute address fixups |
| 250 | */ |
| 251 | |
| 252 | /* |
| 253 | * Write a fixup list to an object file |
| 254 | */ |
| 255 | void CTcAbsFixup:: |
| 256 | write_fixup_list_to_object_file(CVmFile *fp, CTcAbsFixup *list_head) |
| 257 | { |
| 258 | int cnt; |
| 259 | CTcAbsFixup *fixup; |
| 260 | |
| 261 | /* count the fixups */ |
| 262 | for (cnt = 0, fixup = list_head ; fixup != 0 ; |
| 263 | fixup = fixup->nxt, ++cnt) ; |
| 264 | |
| 265 | /* write the count */ |
| 266 | fp->write_uint2(cnt); |
| 267 | |
| 268 | /* write the fixup list */ |
| 269 | for (fixup = list_head ; fixup != 0 ; fixup = fixup->nxt) |
| 270 | { |
| 271 | char buf[5]; |
| 272 | |
| 273 | /* write the data stream ID */ |
| 274 | buf[0] = fixup->ds->get_stream_id(); |
| 275 | |
| 276 | /* write the data stream offset of the reference */ |
| 277 | oswp4(buf+1, fixup->ofs); |
| 278 | |
| 279 | /* write the data */ |
| 280 | fp->write_bytes(buf, 5); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Read a fixup list from an object file |
| 286 | */ |
| 287 | void CTcAbsFixup:: |
| 288 | load_fixup_list_from_object_file(CVmFile *fp, |
| 289 | const textchar_t *obj_fname, |
| 290 | CTcAbsFixup **list_head) |
| 291 | { |
| 292 | uint fixup_cnt; |
| 293 | |
| 294 | /* read the fixups */ |
| 295 | for (fixup_cnt = fp->read_uint2() ; fixup_cnt != 0 ; --fixup_cnt) |
| 296 | { |
| 297 | char buf[5]; |
| 298 | char stream_id; |
| 299 | ulong fixup_ofs; |
| 300 | CTcDataStream *stream; |
| 301 | |
| 302 | /* read the fixup data */ |
| 303 | fp->read_bytes(buf, 5); |
| 304 | stream_id = buf[0]; |
| 305 | fixup_ofs = t3rp4u(buf+1); |
| 306 | |
| 307 | /* find the stream for the ID */ |
| 308 | stream = CTcDataStream::get_stream_from_id(stream_id, obj_fname); |
| 309 | |
| 310 | /* if the stream is invalid, ignore this record */ |
| 311 | if (stream == 0) |
| 312 | continue; |
| 313 | |
| 314 | /* |
| 315 | * the fixup offset is relative to the starting offset of the |
| 316 | * stream for the current object file - adjust it accordingly |
| 317 | */ |
| 318 | fixup_ofs += stream->get_object_file_start_ofs(); |
| 319 | |
| 320 | /* create the fixup and add it to the list */ |
| 321 | add_abs_fixup(list_head, stream, fixup_ofs); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* ------------------------------------------------------------------------ */ |
| 326 | /* |
| 327 | * Object/property ID fixups |
| 328 | */ |
| 329 | |
| 330 | /* |
| 331 | * Write a fixup list to an object file |
| 332 | */ |
| 333 | void CTcIdFixup::write_to_object_file(CVmFile *fp, CTcIdFixup *head) |
| 334 | { |
| 335 | ulong cnt; |
| 336 | CTcIdFixup *cur; |
| 337 | |
| 338 | /* count the elements in the list */ |
| 339 | for (cur = head, cnt = 0 ; cur != 0 ; cur = cur->nxt_, ++cnt) ; |
| 340 | |
| 341 | /* write the count */ |
| 342 | fp->write_uint4(cnt); |
| 343 | |
| 344 | /* write the fixups */ |
| 345 | for (cur = head ; cur != 0 ; cur = cur->nxt_) |
| 346 | { |
| 347 | char buf[9]; |
| 348 | |
| 349 | /* prepare a buffer with the data in portable format */ |
| 350 | buf[0] = cur->ds_->get_stream_id(); |
| 351 | oswp4(buf+1, cur->ofs_); |
| 352 | oswp4(buf+5, cur->id_); |
| 353 | |
| 354 | /* write the data */ |
| 355 | fp->write_bytes(buf, 9); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Read an object ID fixup list from an object file |
| 361 | */ |
| 362 | void CTcIdFixup::load_object_file(CVmFile *fp, |
| 363 | const void *xlat, |
| 364 | ulong xlat_cnt, |
| 365 | tcgen_xlat_type xlat_type, |
| 366 | size_t stream_element_size, |
| 367 | const textchar_t *fname, |
| 368 | CTcIdFixup **fixup_list_head) |
| 369 | { |
| 370 | ulong cnt; |
| 371 | |
| 372 | /* read the count, then read the fixups */ |
| 373 | for (cnt = fp->read_uint4() ; cnt != 0 ; --cnt) |
| 374 | { |
| 375 | char buf[9]; |
| 376 | ulong ofs; |
| 377 | ulong old_id; |
| 378 | ulong new_id; |
| 379 | CTcDataStream *stream; |
| 380 | |
| 381 | /* read the next fixup */ |
| 382 | fp->read_bytes(buf, 9); |
| 383 | stream = CTcDataStream::get_stream_from_id(buf[0], fname); |
| 384 | ofs = t3rp4u(buf+1); |
| 385 | old_id = t3rp4u(buf+5); |
| 386 | |
| 387 | /* if the stream was invalid, ignore this record */ |
| 388 | if (stream == 0) |
| 389 | continue; |
| 390 | |
| 391 | /* adjust the offset for the stream's start in this object file */ |
| 392 | ofs += stream->get_object_file_start_ofs(); |
| 393 | |
| 394 | /* apply the fixup if a translation table was provided */ |
| 395 | if (xlat != 0) |
| 396 | { |
| 397 | /* make sure the count is in range - if it's not, ignore it */ |
| 398 | if (old_id >= xlat_cnt) |
| 399 | { |
| 400 | /* note the problem */ |
| 401 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 402 | TCERR_OBJFILE_INVAL_OBJ_ID, fname); |
| 403 | |
| 404 | /* ignore the record */ |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | /* look up the new ID */ |
| 409 | switch(xlat_type) |
| 410 | { |
| 411 | case TCGEN_XLAT_OBJ: |
| 412 | new_id = ((const tctarg_obj_id_t *)xlat)[old_id]; |
| 413 | break; |
| 414 | |
| 415 | case TCGEN_XLAT_PROP: |
| 416 | new_id = ((const tctarg_prop_id_t *)xlat)[old_id]; |
| 417 | break; |
| 418 | |
| 419 | case TCGEN_XLAT_ENUM: |
| 420 | new_id = ((const ulong *)xlat)[old_id]; |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | /* apply the fixup */ |
| 425 | if (stream_element_size == 2) |
| 426 | stream->write2_at(ofs, (uint)new_id); |
| 427 | else |
| 428 | stream->write4_at(ofs, new_id); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | /* use the original ID for now */ |
| 433 | new_id = old_id; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * If we're keeping object fixups in the new file, create a fixup |
| 438 | * for the reference. Note that the fixup is for the new ID, not |
| 439 | * the old ID, because we've already translated the value in the |
| 440 | * stream to the new ID. |
| 441 | */ |
| 442 | if (fixup_list_head != 0) |
| 443 | CTcIdFixup::add_fixup(fixup_list_head, stream, ofs, new_id); |
| 444 | } |
| 445 | } |
| 446 |