| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/TCPRSIMG.CPP,v 1.1 1999/07/11 00:46:53 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 | tcprsimg.cpp - TADS 3 Compiler Parser - image writing functions |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 04/30/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <stdio.h> |
| 26 | #include <assert.h> |
| 27 | |
| 28 | #include "os.h" |
| 29 | #include "t3std.h" |
| 30 | #include "tcprs.h" |
| 31 | #include "tctarg.h" |
| 32 | #include "tcgen.h" |
| 33 | #include "vmhash.h" |
| 34 | #include "tcmain.h" |
| 35 | #include "vmfile.h" |
| 36 | #include "tctok.h" |
| 37 | |
| 38 | |
| 39 | /* ------------------------------------------------------------------------ */ |
| 40 | /* |
| 41 | * Read an object file and load the global symbol table |
| 42 | */ |
| 43 | int CTcParser::load_object_file(class CVmFile *fp, const textchar_t *fname, |
| 44 | tctarg_obj_id_t *obj_xlat, |
| 45 | tctarg_prop_id_t *prop_xlat, |
| 46 | ulong *enum_xlat) |
| 47 | { |
| 48 | ulong sym_cnt; |
| 49 | ulong dict_cnt; |
| 50 | ulong i; |
| 51 | ulong anon_cnt; |
| 52 | ulong nonsym_cnt; |
| 53 | ulong prod_cnt; |
| 54 | ulong exp_cnt; |
| 55 | |
| 56 | /* read the number of symbol index entries */ |
| 57 | sym_cnt = fp->read_uint4(); |
| 58 | if (sym_cnt != 0) |
| 59 | { |
| 60 | /* allocate space for the symbol index list */ |
| 61 | obj_sym_list_ = (CTcSymbol **) |
| 62 | t3malloc(sym_cnt * sizeof(obj_sym_list_[0])); |
| 63 | |
| 64 | /* the list is empty so far */ |
| 65 | obj_file_sym_idx_ = 0; |
| 66 | } |
| 67 | |
| 68 | /* read the number of dictionary symbols */ |
| 69 | dict_cnt = fp->read_uint4(); |
| 70 | |
| 71 | /* if there are any symbols, read them */ |
| 72 | if (dict_cnt != 0) |
| 73 | { |
| 74 | /* allocate space for the dictionary index list */ |
| 75 | obj_dict_list_ = (CTcDictEntry **) |
| 76 | t3malloc(dict_cnt * sizeof(obj_dict_list_[0])); |
| 77 | |
| 78 | /* nothing in the list yet */ |
| 79 | obj_file_dict_idx_ = 0; |
| 80 | } |
| 81 | |
| 82 | /* read the number of symbols in the file */ |
| 83 | sym_cnt = fp->read_uint4(); |
| 84 | |
| 85 | /* read the symbols */ |
| 86 | for (i = 0 ; i < sym_cnt ; ++i) |
| 87 | { |
| 88 | /* load a symbol */ |
| 89 | if (CTcSymbol::load_from_obj_file(fp, fname, |
| 90 | obj_xlat, prop_xlat, enum_xlat)) |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | /* read the number of anonymous object symbols */ |
| 95 | anon_cnt = fp->read_uint4(); |
| 96 | |
| 97 | /* read the anonymous object symbols */ |
| 98 | for (i = 0 ; i < anon_cnt ; ++i) |
| 99 | { |
| 100 | /* load the next anonymous object symbol */ |
| 101 | if (CTcSymObj::load_from_obj_file(fp, fname, obj_xlat, TRUE)) |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | /* read the non-symbol object ID's */ |
| 106 | nonsym_cnt = fp->read_uint4(); |
| 107 | for (i = 0 ; i < nonsym_cnt ; ++i) |
| 108 | { |
| 109 | tctarg_obj_id_t id; |
| 110 | |
| 111 | /* read the next non-symbol object ID */ |
| 112 | id = (tctarg_obj_id_t)fp->read_uint4(); |
| 113 | |
| 114 | /* |
| 115 | * allocate a new ID for the object, and set the translation |
| 116 | * table for the new ID - this will ensure that references to |
| 117 | * this non-symbol object are properly fixed up |
| 118 | */ |
| 119 | obj_xlat[id] = G_cg->new_obj_id(); |
| 120 | } |
| 121 | |
| 122 | /* read the number of symbol cross-reference sections in the file */ |
| 123 | sym_cnt = fp->read_uint4(); |
| 124 | |
| 125 | /* read the symbol cross-references */ |
| 126 | for (i = 0 ; i < sym_cnt ; ++i) |
| 127 | { |
| 128 | ulong idx; |
| 129 | CTcSymbol *sym; |
| 130 | |
| 131 | /* read the symbol index */ |
| 132 | idx = fp->read_uint4(); |
| 133 | |
| 134 | /* get the symbol from the index list */ |
| 135 | sym = get_objfile_sym(idx); |
| 136 | |
| 137 | /* load the symbol's reference information */ |
| 138 | sym->load_refs_from_obj_file(fp, fname, obj_xlat, prop_xlat); |
| 139 | } |
| 140 | |
| 141 | /* read the number of anonymous object cross-references */ |
| 142 | anon_cnt = fp->read_uint4(); |
| 143 | |
| 144 | /* read the anonymous object cross-references */ |
| 145 | for (i = 0 ; i < anon_cnt ; ++i) |
| 146 | { |
| 147 | ulong idx; |
| 148 | CTcSymbol *sym; |
| 149 | |
| 150 | /* read the symbol index */ |
| 151 | idx = fp->read_uint4(); |
| 152 | |
| 153 | /* get the symbol from the index list */ |
| 154 | sym = get_objfile_sym(idx); |
| 155 | |
| 156 | /* load the symbol's reference information */ |
| 157 | sym->load_refs_from_obj_file(fp, fname, obj_xlat, prop_xlat); |
| 158 | } |
| 159 | |
| 160 | /* read the master grammar rule count */ |
| 161 | prod_cnt = fp->read_uint4(); |
| 162 | |
| 163 | /* read the master grammar rule list */ |
| 164 | for (i = 0 ; i < prod_cnt ; ++i) |
| 165 | { |
| 166 | /* read the next grammar production */ |
| 167 | CTcGramProdEntry::load_from_obj_file(fp, prop_xlat, enum_xlat, 0); |
| 168 | } |
| 169 | |
| 170 | /* read the number of named grammar rules */ |
| 171 | prod_cnt = fp->read_uint4(); |
| 172 | |
| 173 | /* read the private grammar rules */ |
| 174 | for (i = 0 ; i < prod_cnt ; ++i) |
| 175 | { |
| 176 | CTcSymObj *match_sym; |
| 177 | |
| 178 | /* read the match object defining the rule */ |
| 179 | match_sym = get_objfile_objsym(fp->read_uint4()); |
| 180 | |
| 181 | /* read the private rule list */ |
| 182 | CTcGramProdEntry::load_from_obj_file( |
| 183 | fp, prop_xlat, enum_xlat, match_sym); |
| 184 | } |
| 185 | |
| 186 | /* read the export symbol list */ |
| 187 | exp_cnt = fp->read_uint4(); |
| 188 | for (i = 0 ; i < exp_cnt ; ++i) |
| 189 | { |
| 190 | CTcPrsExport *exp; |
| 191 | |
| 192 | /* read the next entry */ |
| 193 | exp = CTcPrsExport::read_from_obj_file(fp); |
| 194 | |
| 195 | /* if that failed, the whole load fails */ |
| 196 | if (exp == 0) |
| 197 | return 1; |
| 198 | |
| 199 | /* add it to our list */ |
| 200 | add_export_to_list(exp); |
| 201 | } |
| 202 | |
| 203 | /* done with the symbol index list - free it */ |
| 204 | if (obj_sym_list_ != 0) |
| 205 | { |
| 206 | /* free it and forget it */ |
| 207 | t3free(obj_sym_list_); |
| 208 | obj_sym_list_ = 0; |
| 209 | } |
| 210 | |
| 211 | /* done with the dictionary index list - free it */ |
| 212 | if (obj_dict_list_ != 0) |
| 213 | { |
| 214 | /* free the memory and forget it */ |
| 215 | t3free(obj_dict_list_); |
| 216 | obj_dict_list_ = 0; |
| 217 | } |
| 218 | |
| 219 | /* success */ |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* ------------------------------------------------------------------------ */ |
| 225 | /* |
| 226 | * Generate code and write the image file |
| 227 | */ |
| 228 | void CTPNStmProg::build_image(class CVmFile *image_fp, uchar xor_mask, |
| 229 | const char tool_data[4]) |
| 230 | { |
| 231 | /* generate code */ |
| 232 | if (gen_code_for_build()) |
| 233 | return; |
| 234 | |
| 235 | /* scan the symbol table for unresolved external references */ |
| 236 | if (G_prs->check_unresolved_externs()) |
| 237 | return; |
| 238 | |
| 239 | /* |
| 240 | * Finally, our task of constructing the program is complete. All |
| 241 | * that remains is to write the image file. Tell the code generator |
| 242 | * to begin the process. |
| 243 | */ |
| 244 | G_cg->write_to_image(image_fp, xor_mask, tool_data); |
| 245 | } |
| 246 | |
| 247 | /* ------------------------------------------------------------------------ */ |
| 248 | /* |
| 249 | * Generate code and write the object file |
| 250 | */ |
| 251 | void CTPNStmProg::build_object_file(class CVmFile *object_fp, |
| 252 | class CTcMake *make_obj) |
| 253 | { |
| 254 | /* generate code */ |
| 255 | if (gen_code_for_build()) |
| 256 | return; |
| 257 | |
| 258 | /* |
| 259 | * Finally, our task of constructing the program is complete. All |
| 260 | * that remains is to write the image file. Tell the code generator |
| 261 | * to begin the process. |
| 262 | */ |
| 263 | G_cg->write_to_object_file(object_fp, make_obj); |
| 264 | } |
| 265 | |
| 266 | /* ------------------------------------------------------------------------ */ |
| 267 | /* |
| 268 | * Generate code for a build, in preparation for writing an image file |
| 269 | * or an object file. |
| 270 | */ |
| 271 | int CTPNStmProg::gen_code_for_build() |
| 272 | { |
| 273 | /* notify the tokenizer that parsing is done */ |
| 274 | G_tok->parsing_done(); |
| 275 | |
| 276 | /* notify the code generator that we're finished parsing */ |
| 277 | G_cg->parsing_done(); |
| 278 | |
| 279 | /* set the global symbol table in the code streams */ |
| 280 | G_cs_main->set_symtab(G_prs->get_global_symtab()); |
| 281 | G_cs_static->set_symtab(G_prs->get_global_symtab()); |
| 282 | |
| 283 | /* generate code for the entire program */ |
| 284 | gen_code(TRUE, TRUE); |
| 285 | |
| 286 | /* |
| 287 | * if we encountered any errors generating code, don't bother |
| 288 | * writing the image |
| 289 | */ |
| 290 | if (G_tcmain->get_error_count() != 0) |
| 291 | return 1; |
| 292 | |
| 293 | /* return success */ |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* ------------------------------------------------------------------------ */ |
| 298 | /* |
| 299 | * Check for unresolved external symbols. Logs an error for each |
| 300 | * unresolved external. |
| 301 | */ |
| 302 | int CTcParser::check_unresolved_externs() |
| 303 | { |
| 304 | int errcnt; |
| 305 | |
| 306 | /* generate any synthesized code */ |
| 307 | G_cg->build_synthesized_code(); |
| 308 | |
| 309 | /* note the previous error count */ |
| 310 | errcnt = G_tcmain->get_error_count(); |
| 311 | |
| 312 | /* enumerate the entries with our unresolved check callback */ |
| 313 | get_global_symtab()->enum_entries(&enum_sym_extref, this); |
| 314 | |
| 315 | /* |
| 316 | * if the error count increased, we logged errors for unresolved |
| 317 | * symbols |
| 318 | */ |
| 319 | return (G_tcmain->get_error_count() > errcnt); |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Enumeration callback - check for unresolved external references. For |
| 324 | * each object or function still marked "external," we'll log an error. |
| 325 | */ |
| 326 | void CTcParser::enum_sym_extref(void *, CTcSymbol *sym) |
| 327 | { |
| 328 | /* if it's an external symbol, log an error */ |
| 329 | if (sym->is_unresolved_extern()) |
| 330 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_UNRESOLVED_EXTERN, |
| 331 | (int)sym->get_sym_len(), sym->get_sym()); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /* ------------------------------------------------------------------------ */ |
| 336 | /* |
| 337 | * Build dictionaries. We go through all objects and insert their |
| 338 | * vocabulary words into their dictionaries. |
| 339 | */ |
| 340 | void CTcParser::build_dictionaries() |
| 341 | { |
| 342 | CTcDictEntry *dict; |
| 343 | CTcSymObj *sym; |
| 344 | |
| 345 | /* |
| 346 | * enumerate our symbols to insert dictionary words - this will |
| 347 | * populate each dictionary's hash table with a complete list of the |
| 348 | * words and object associations for the dictionary |
| 349 | */ |
| 350 | get_global_symtab()->enum_entries(&enum_sym_dict, this); |
| 351 | |
| 352 | /* do the same for the anonymous objects */ |
| 353 | for (sym = anon_obj_head_ ; sym != 0 ; sym = (CTcSymObj *)sym->nxt_) |
| 354 | sym->build_dictionary(); |
| 355 | |
| 356 | /* generate the object stream for each dictionary */ |
| 357 | for (dict = dict_head_ ; dict != 0 ; dict = dict->get_next()) |
| 358 | { |
| 359 | /* generate the code (static data, actually) for this dictionary */ |
| 360 | G_cg->gen_code_for_dict(dict); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * enumeration callback - build dictionaries |
| 366 | */ |
| 367 | void CTcParser::enum_sym_dict(void *, CTcSymbol *sym) |
| 368 | { |
| 369 | /* tell this symbol to build its dictionary entries */ |
| 370 | sym->build_dictionary(); |
| 371 | } |
| 372 | |
| 373 | /* ------------------------------------------------------------------------ */ |
| 374 | /* |
| 375 | * Build grammar productions |
| 376 | */ |
| 377 | void CTcParser::build_grammar_productions() |
| 378 | { |
| 379 | CTcGramProdEntry *entry; |
| 380 | |
| 381 | /* |
| 382 | * First, run through the symbol table and merge all of the private |
| 383 | * grammar rules into the master grammar rule list. Since we've |
| 384 | * finished linking, we've already applied all modify/replace |
| 385 | * overrides, hence each symbol table entry referring to an object |
| 386 | * will contain its final private grammar rule list. So, we can |
| 387 | * safely merge the private lists into the master lists at this point, |
| 388 | * since no more modifications to private lists are possible. |
| 389 | */ |
| 390 | get_global_symtab()->enum_entries(&build_grammar_cb, this); |
| 391 | |
| 392 | /* |
| 393 | * iterate over the master list of productions and generate the image |
| 394 | * data for each one |
| 395 | */ |
| 396 | for (entry = gramprod_head_ ; entry != 0 ; entry = entry->get_next()) |
| 397 | { |
| 398 | /* build this entry */ |
| 399 | G_cg->gen_code_for_gramprod(entry); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Symbol table enumeration callback - merge match object private grammar |
| 405 | * rules into the master grammar rule list. |
| 406 | */ |
| 407 | void CTcParser::build_grammar_cb(void *, CTcSymbol *sym) |
| 408 | { |
| 409 | /* if this is an object, merge its private grammar list */ |
| 410 | if (sym->get_type() == TC_SYM_OBJ) |
| 411 | ((CTcSymObj *)sym)->merge_grammar_entry(); |
| 412 | } |
| 413 | |
| 414 | /* ------------------------------------------------------------------------ */ |
| 415 | /* |
| 416 | * Apply self-reference object ID fixups. This traverses the symbol |
| 417 | * table and applies each object's list of fixups. This can be called |
| 418 | * once after loading all object files. |
| 419 | */ |
| 420 | void CTcParser::apply_internal_fixups() |
| 421 | { |
| 422 | CTcSymObj *anon_obj; |
| 423 | |
| 424 | /* enumerate the entries with our callback */ |
| 425 | get_global_symtab()->enum_entries(&enum_sym_internal_fixup, this); |
| 426 | |
| 427 | /* apply internal fixups to our anonymous objects */ |
| 428 | for (anon_obj = anon_obj_head_ ; anon_obj != 0 ; |
| 429 | anon_obj = (CTcSymObj *)anon_obj->nxt_) |
| 430 | { |
| 431 | /* apply internal fixups to this symbol */ |
| 432 | anon_obj->apply_internal_fixups(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Enumeration callback - apply internal ID fixups |
| 438 | */ |
| 439 | void CTcParser::enum_sym_internal_fixup(void *, CTcSymbol *sym) |
| 440 | { |
| 441 | /* apply its self-reference fixups */ |
| 442 | sym->apply_internal_fixups(); |
| 443 | } |
| 444 | |
| 445 | /* ------------------------------------------------------------------------ */ |
| 446 | /* |
| 447 | * Basic symbol class - image/object file functions |
| 448 | */ |
| 449 | |
| 450 | /* |
| 451 | * Read a symbol from an object file |
| 452 | */ |
| 453 | int CTcSymbolBase::load_from_obj_file(CVmFile *fp, const textchar_t *fname, |
| 454 | tctarg_obj_id_t *obj_xlat, |
| 455 | tctarg_prop_id_t *prop_xlat, |
| 456 | ulong *enum_xlat) |
| 457 | { |
| 458 | tc_symtype_t typ; |
| 459 | |
| 460 | /* |
| 461 | * read the type - this is the one thing we know is always present |
| 462 | * for every symbol (the rest of the data might vary per subclass) |
| 463 | */ |
| 464 | typ = (tc_symtype_t)fp->read_uint2(); |
| 465 | |
| 466 | /* create the object based on the type */ |
| 467 | switch(typ) |
| 468 | { |
| 469 | case TC_SYM_FUNC: |
| 470 | return CTcSymFunc::load_from_obj_file(fp, fname); |
| 471 | |
| 472 | case TC_SYM_OBJ: |
| 473 | return CTcSymObj::load_from_obj_file(fp, fname, obj_xlat, FALSE); |
| 474 | |
| 475 | case TC_SYM_PROP: |
| 476 | return CTcSymProp::load_from_obj_file(fp, fname, prop_xlat); |
| 477 | |
| 478 | case TC_SYM_ENUM: |
| 479 | return CTcSymEnum::load_from_obj_file(fp, fname, enum_xlat); |
| 480 | |
| 481 | case TC_SYM_BIF: |
| 482 | return CTcSymBif::load_from_obj_file(fp, fname); |
| 483 | |
| 484 | case TC_SYM_METACLASS: |
| 485 | return CTcSymMetaclass::load_from_obj_file(fp, fname, obj_xlat); |
| 486 | |
| 487 | default: |
| 488 | /* other types should not be in an object file */ |
| 489 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_INV_TYPE); |
| 490 | |
| 491 | /* return an error indication */ |
| 492 | return 1; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Log a conflict with another symbol from an object file |
| 498 | */ |
| 499 | void CTcSymbolBase::log_objfile_conflict(const textchar_t *fname, |
| 500 | tc_symtype_t new_type) const |
| 501 | { |
| 502 | static const textchar_t *type_name[] = |
| 503 | { |
| 504 | "unknown", "function", "object", "property", "local", |
| 505 | "parameter", "intrinsic function", "native function", "code label", |
| 506 | "intrinsic class", "enum" |
| 507 | }; |
| 508 | |
| 509 | /* |
| 510 | * if the types differ, log an error indicating the different types; |
| 511 | * otherwise, simply log an error indicating the redefinition |
| 512 | */ |
| 513 | if (new_type != get_type()) |
| 514 | { |
| 515 | /* the types differ - show the two types */ |
| 516 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_REDEF_SYM_TYPE, |
| 517 | (int)get_sym_len(), get_sym(), |
| 518 | type_name[get_type()], type_name[new_type], |
| 519 | fname); |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | /* the types are the same */ |
| 524 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_REDEF_SYM, |
| 525 | (int)get_sym_len(), get_sym(), |
| 526 | type_name[new_type], fname); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* ------------------------------------------------------------------------ */ |
| 532 | /* |
| 533 | * Function Symbol subclass - image/object file functions |
| 534 | */ |
| 535 | |
| 536 | /* |
| 537 | * Load from an object file |
| 538 | */ |
| 539 | int CTcSymFuncBase::load_from_obj_file(CVmFile *fp, |
| 540 | const textchar_t *fname) |
| 541 | { |
| 542 | char txtbuf[4096]; |
| 543 | const char *txt; |
| 544 | size_t len; |
| 545 | char buf[12]; |
| 546 | int is_extern; |
| 547 | int ext_replace; |
| 548 | int ext_modify; |
| 549 | int has_retval; |
| 550 | int varargs; |
| 551 | int argc; |
| 552 | int opt_argc; |
| 553 | int is_multimethod, is_multimethod_base; |
| 554 | int mod_base_cnt; |
| 555 | CTcSymFunc *sym; |
| 556 | |
| 557 | /* |
| 558 | * Read the symbol name. Use a custom reader instead of the base |
| 559 | * reader, because function symbols can be quite long, due to |
| 560 | * multimethod name decoration. |
| 561 | */ |
| 562 | if ((txt = CTcParser::read_len_prefix_str( |
| 563 | fp, txtbuf, sizeof(txtbuf), 0, TCERR_SYMEXP_SYM_TOO_LONG)) == 0) |
| 564 | return 1; |
| 565 | len = strlen(txt); |
| 566 | |
| 567 | /* read our extra data */ |
| 568 | fp->read_bytes(buf, 12); |
| 569 | argc = osrp2(buf); |
| 570 | opt_argc = osrp2(buf + 2); |
| 571 | varargs = buf[4]; |
| 572 | has_retval = buf[5]; |
| 573 | is_extern = buf[6]; |
| 574 | ext_replace = buf[7]; |
| 575 | ext_modify = buf[8]; |
| 576 | is_multimethod = (buf[9] & 1) != 0; |
| 577 | is_multimethod_base = (buf[9] & 2) != 0; |
| 578 | mod_base_cnt = osrp2(buf + 10); |
| 579 | |
| 580 | /* look up any existing symbol */ |
| 581 | sym = (CTcSymFunc *)G_prs->get_global_symtab()->find(txt, len); |
| 582 | |
| 583 | /* |
| 584 | * If this symbol is already defined, make sure the original |
| 585 | * definition is a function, and make sure that it's only defined |
| 586 | * (not referenced as external) once. If it's not defined, define |
| 587 | * it anew. |
| 588 | */ |
| 589 | if (sym == 0) |
| 590 | { |
| 591 | /* |
| 592 | * It's not defined yet - create the new definition and add it |
| 593 | * to the symbol table. |
| 594 | */ |
| 595 | sym = new CTcSymFunc(txt, len, FALSE, argc, opt_argc, varargs, |
| 596 | has_retval, is_multimethod, is_multimethod_base, |
| 597 | is_extern, TRUE); |
| 598 | G_prs->get_global_symtab()->add_entry(sym); |
| 599 | |
| 600 | /* it's an error if we're replacing a previously undefined function */ |
| 601 | if (ext_replace || ext_modify) |
| 602 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 603 | TCERR_OBJFILE_REPFUNC_BEFORE_ORIG, |
| 604 | (int)len, txt, fname); |
| 605 | } |
| 606 | else if (sym->get_type() != TC_SYM_FUNC |
| 607 | || (!sym->is_extern() |
| 608 | && !is_extern && !ext_replace && !ext_modify)) |
| 609 | { |
| 610 | /* |
| 611 | * It's already defined, but it's not a function, or this is a |
| 612 | * non-extern/replaced definition and the symbol is already |
| 613 | * defined non-extern - log a symbol type conflict error. |
| 614 | */ |
| 615 | sym->log_objfile_conflict(fname, TC_SYM_FUNC); |
| 616 | |
| 617 | /* |
| 618 | * proceed despite the error, since this is merely a symbol |
| 619 | * conflict and not a file corruption - create a fake symbol to |
| 620 | * hold the information, so that we can read the data and thus |
| 621 | * keep in sync with the file, but don't bother adding the fake |
| 622 | * symbol object to the symbol table |
| 623 | */ |
| 624 | sym = new CTcSymFunc(txt, len, FALSE, argc, opt_argc, varargs, |
| 625 | has_retval, is_multimethod, is_multimethod_base, |
| 626 | is_extern, TRUE); |
| 627 | } |
| 628 | else if (sym->get_argc() != argc |
| 629 | || sym->is_varargs() != varargs |
| 630 | || sym->has_retval() != has_retval) |
| 631 | { |
| 632 | /* the symbol has an incompatible definition - log the error */ |
| 633 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_FUNC_INCOMPAT, |
| 634 | (int)len, txt, fname); |
| 635 | } |
| 636 | else if (sym->is_multimethod() != is_multimethod |
| 637 | || sym->is_multimethod_base() != is_multimethod_base) |
| 638 | { |
| 639 | /* the multi-method status conflicts */ |
| 640 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_MMFUNC_INCOMPAT, |
| 641 | (int)len, txt, fname); |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * if this is a non-extern definition, we now have the object |
| 646 | * defined -- remove the 'extern' flag from the symbol table entry |
| 647 | * in this case |
| 648 | */ |
| 649 | if (!is_extern) |
| 650 | { |
| 651 | /* mark the symbol as defined */ |
| 652 | sym->set_extern(FALSE); |
| 653 | |
| 654 | /* |
| 655 | * if we're replacing it, delete the original; if we're modifying |
| 656 | * it, chain the original into our modify list |
| 657 | */ |
| 658 | if (ext_replace) |
| 659 | { |
| 660 | int i; |
| 661 | |
| 662 | /* |
| 663 | * mark the previous code anchor as obsolete so that we |
| 664 | * don't write its code to the image file |
| 665 | */ |
| 666 | if (sym->get_anchor() != 0) |
| 667 | sym->get_anchor()->set_replaced(TRUE); |
| 668 | |
| 669 | /* |
| 670 | * Mark all of the modified base function code offsets as |
| 671 | * replaced as well. |
| 672 | */ |
| 673 | for (i = 0 ; i < sym->get_mod_base_offset_count() ; ++i) |
| 674 | { |
| 675 | CTcStreamAnchor *anchor; |
| 676 | |
| 677 | /* get the anchor for this offset */ |
| 678 | anchor = G_cs->find_anchor(sym->get_mod_base_offset(i)); |
| 679 | |
| 680 | /* mark it as replaced */ |
| 681 | if (anchor != 0) |
| 682 | anchor->set_replaced(TRUE); |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * We can now forget everything in the modify base list, as |
| 687 | * everything in the list is being replaced and is thus no |
| 688 | * longer relevant. |
| 689 | */ |
| 690 | sym->clear_mod_base_offsets(); |
| 691 | } |
| 692 | else if (ext_modify) |
| 693 | { |
| 694 | /* |
| 695 | * We're modifying an external symbol. The anchor to the code |
| 696 | * stream object that we previously loaded is actually the |
| 697 | * anchor to the modified base object, not to the new meaning |
| 698 | * of the symbol, so detach the anchor from our symbol. |
| 699 | */ |
| 700 | sym->get_anchor()->detach_from_symbol(); |
| 701 | |
| 702 | /* |
| 703 | * The object file has a fixup list for references to the |
| 704 | * external base object that we're modifying. In other words, |
| 705 | * these are external references from the object file we're |
| 706 | * loading to the now-nameless code stream object that we're |
| 707 | * replacing, which is the code stream object at our anchor. |
| 708 | * So, load those fixups into the anchor's new internal fixup |
| 709 | * list. It's important to note that these aren't references |
| 710 | * to this symbol - they're specifically references to the |
| 711 | * modified base code stream object. |
| 712 | */ |
| 713 | CTcAbsFixup::load_fixup_list_from_object_file( |
| 714 | fp, fname, sym->get_anchor()->fixup_list_head_); |
| 715 | |
| 716 | /* |
| 717 | * Add the old code stream anchor to the list of modified base |
| 718 | * offsets for the function. The function we're reading from |
| 719 | * the object file modifies this as a base function, so we need |
| 720 | * to add this to the list of modified base functions. |
| 721 | */ |
| 722 | sym->add_mod_base_offset(sym->get_anchor()->get_ofs()); |
| 723 | |
| 724 | /* |
| 725 | * Complete the dissociation from the anchor by forgetting the |
| 726 | * anchor in the symbol. This will allow the code stream |
| 727 | * object that's associated with this symbol in the current |
| 728 | * file to take over the anchor duty for this symbol, which |
| 729 | * will ensure that all fixups that reference this symbol will |
| 730 | * be resolved to the new code stream object. |
| 731 | */ |
| 732 | sym->set_anchor(0); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Read the list of modified base function offsets. Each entry is a |
| 738 | * code stream offset, so adjust each using the base code stream offset |
| 739 | * for this object file. |
| 740 | */ |
| 741 | for ( ; mod_base_cnt != 0 ; --mod_base_cnt) |
| 742 | { |
| 743 | int i; |
| 744 | |
| 745 | /* read them */ |
| 746 | for (i = 0 ; i < mod_base_cnt ; ++i) |
| 747 | { |
| 748 | /* read the offset, adjusting to the object file start position */ |
| 749 | ulong ofs = fp->read_uint4() + G_cs->get_object_file_start_ofs(); |
| 750 | |
| 751 | /* append this item */ |
| 752 | sym->add_mod_base_offset(ofs); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | /* if it's extern, load the fixup list */ |
| 757 | if (is_extern) |
| 758 | { |
| 759 | /* |
| 760 | * This is an external reference, so we must load our fixup |
| 761 | * list, adding it to any fixup list that already exists with |
| 762 | * the symbol. |
| 763 | */ |
| 764 | CTcAbsFixup:: |
| 765 | load_fixup_list_from_object_file(fp, fname, &sym->fixups_); |
| 766 | } |
| 767 | |
| 768 | /* success */ |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /* ------------------------------------------------------------------------ */ |
| 773 | /* |
| 774 | * object symbol entry base - image/object file functions |
| 775 | */ |
| 776 | |
| 777 | /* |
| 778 | * Load from an object file |
| 779 | */ |
| 780 | int CTcSymObjBase::load_from_obj_file(CVmFile *fp, |
| 781 | const textchar_t *fname, |
| 782 | tctarg_obj_id_t *obj_xlat, |
| 783 | int anon) |
| 784 | { |
| 785 | /* |
| 786 | * do the main loading - if it fails to return a symbol, return |
| 787 | * failure (i.e., non-zero) |
| 788 | */ |
| 789 | return (load_from_obj_file_main(fp, fname, obj_xlat, 0, 0, anon) == 0); |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Load a modified base object from an object file |
| 794 | */ |
| 795 | CTcSymObj *CTcSymObjBase:: |
| 796 | load_from_obj_file_modbase(class CVmFile *fp, const textchar_t *fname, |
| 797 | tctarg_obj_id_t *obj_xlat, |
| 798 | const textchar_t *mod_name, |
| 799 | size_t mod_name_len, int anon) |
| 800 | { |
| 801 | /* skip the type prefix - we know it's an object */ |
| 802 | fp->read_uint2(); |
| 803 | |
| 804 | /* load the object and return the symbol */ |
| 805 | return load_from_obj_file_main(fp, fname, obj_xlat, |
| 806 | mod_name, mod_name_len, anon); |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | * Load from an object file. This main routine does most of the work, |
| 811 | * and returns the loaded symbol. |
| 812 | * |
| 813 | * 'mod_name' is the primary symbol name for a stack of 'modify' |
| 814 | * objects. Each of the objects in a 'modify' stack, except for the |
| 815 | * topmost (i.e., last defined) object, has a fake symbol name, since |
| 816 | * the program can't refer directly to the base object once modified. |
| 817 | * However, while loading, we must know the actual name for the entire |
| 818 | * stack, so that we can link the bottom of the stack in this object |
| 819 | * file to the top of the stack in another object file if the bottom of |
| 820 | * our stack is declared external (i.e., this object file's source code |
| 821 | * used 'modify' with an external object). If we're loading a top-level |
| 822 | * object, not a modified object, 'mod_name' should be null. |
| 823 | */ |
| 824 | CTcSymObj *CTcSymObjBase:: |
| 825 | load_from_obj_file_main(CVmFile *fp, const textchar_t *fname, |
| 826 | tctarg_obj_id_t *obj_xlat, |
| 827 | const textchar_t *mod_name, size_t mod_name_len, |
| 828 | int anon) |
| 829 | { |
| 830 | /* presume we won't have to use a fake symbol */ |
| 831 | int use_fake_sym = FALSE; |
| 832 | |
| 833 | /* presume we won't be able to read a stream offset */ |
| 834 | int stream_ofs_valid = FALSE; |
| 835 | ulong stream_ofs = 0; |
| 836 | |
| 837 | /* read the symbol name information if it's not anonymous */ |
| 838 | const char *txt; |
| 839 | if (!anon) |
| 840 | { |
| 841 | /* read the symbol name */ |
| 842 | if ((txt = base_read_from_sym_file(fp)) == 0) |
| 843 | return 0; |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | /* use ".anon" as our symbol name placeholder */ |
| 848 | txt = ".anon"; |
| 849 | } |
| 850 | |
| 851 | /* get the symbol len */ |
| 852 | size_t len = strlen(txt); |
| 853 | |
| 854 | /* read our extra data */ |
| 855 | char buf[32]; |
| 856 | fp->read_bytes(buf, 17); |
| 857 | ulong id = t3rp4u(buf); |
| 858 | int is_extern = buf[4]; |
| 859 | int ext_replace_flag = buf[5]; |
| 860 | int modified_flag = buf[6]; |
| 861 | int modify_flag = buf[7]; |
| 862 | int ext_modify_flag = buf[8]; |
| 863 | int class_flag = buf[9]; |
| 864 | int trans_flag = buf[10]; |
| 865 | tc_metaclass_t meta = (tc_metaclass_t)osrp2(buf + 11); |
| 866 | uint dict_idx = osrp2(buf + 13); |
| 867 | uint obj_file_idx = osrp2(buf + 15); |
| 868 | |
| 869 | /* |
| 870 | * if we're not external, read our stream offset, and adjust for the |
| 871 | * object stream base in the object file |
| 872 | */ |
| 873 | if (!is_extern) |
| 874 | { |
| 875 | /* get the appropriate stream */ |
| 876 | CTcDataStream *stream = get_stream_from_meta(meta); |
| 877 | |
| 878 | /* read the relative stream offset */ |
| 879 | stream_ofs = fp->read_uint4(); |
| 880 | |
| 881 | /* |
| 882 | * Ensure the stream offset was actually valid. It must be valid |
| 883 | * unless the object has no stream (for example, dictionary and |
| 884 | * grammar production objects are not generated until link time, |
| 885 | * hence they don't have to have - indeed, can't have - valid |
| 886 | * stream offsets when we're loading an object file). |
| 887 | */ |
| 888 | assert(stream_ofs != 0xffffffff || stream == 0); |
| 889 | |
| 890 | /* determine if it's valid */ |
| 891 | if (stream_ofs != 0xffffffff) |
| 892 | { |
| 893 | /* adjust it relative to this object file's stream base */ |
| 894 | stream_ofs += stream->get_object_file_start_ofs(); |
| 895 | |
| 896 | /* note that it's valid */ |
| 897 | stream_ofs_valid = TRUE; |
| 898 | } |
| 899 | else |
| 900 | { |
| 901 | /* the stream offset is not valid */ |
| 902 | stream_ofs_valid = FALSE; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /* we have no deleted properties yet */ |
| 907 | CTcObjPropDel *del_prop_head = 0; |
| 908 | |
| 909 | /* if this is a 'modify' object, read some additional data */ |
| 910 | if (modify_flag) |
| 911 | { |
| 912 | uint cnt; |
| 913 | |
| 914 | /* read the deleted property list */ |
| 915 | for (cnt = fp->read_uint2() ; cnt != 0 ; --cnt) |
| 916 | { |
| 917 | const char *prop_name; |
| 918 | CTcSymProp *prop_sym; |
| 919 | |
| 920 | /* read the symbol name from the file */ |
| 921 | prop_name = base_read_from_sym_file(fp); |
| 922 | if (prop_name == 0) |
| 923 | return 0; |
| 924 | |
| 925 | /* |
| 926 | * find the property symbol, or define it if it's not |
| 927 | * already defined as a property |
| 928 | */ |
| 929 | prop_sym = (CTcSymProp *)G_prs->get_global_symtab() |
| 930 | ->find_or_def_prop(prop_name, strlen(prop_name), |
| 931 | FALSE); |
| 932 | |
| 933 | /* make sure it's a property */ |
| 934 | if (prop_sym->get_type() != TC_SYM_PROP) |
| 935 | { |
| 936 | /* it's not a property - log the conflict */ |
| 937 | prop_sym->log_objfile_conflict(fname, TC_SYM_PROP); |
| 938 | } |
| 939 | else |
| 940 | { |
| 941 | /* add the entry to my list */ |
| 942 | add_del_prop_to_list(&del_prop_head, prop_sym); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /* read the self-reference fixup list */ |
| 948 | CTcIdFixup *fixups = 0; |
| 949 | CTcIdFixup::load_object_file(fp, 0, 0, TCGEN_XLAT_OBJ, |
| 950 | 4, fname, &fixups); |
| 951 | |
| 952 | /* |
| 953 | * if this is a 'modify' object, load the base object - this is the |
| 954 | * original version of the object, which this object modifies |
| 955 | */ |
| 956 | CTcSymObj *mod_base_sym = 0; |
| 957 | if (modify_flag) |
| 958 | { |
| 959 | /* |
| 960 | * Load the base object - pass the top-level object's name |
| 961 | * (which is our own name if the caller didn't pass an enclosing |
| 962 | * top-level object to us). Note that we must read, and can |
| 963 | * immediately discard, the type data in the object file - we |
| 964 | * know that the base symbol is going to be an object, since we |
| 965 | * always write it out at this specific place in the file, but |
| 966 | * we will have written the type information anyway; thus, we |
| 967 | * don't need the type information, but we must at least skip it |
| 968 | * in the file. |
| 969 | */ |
| 970 | mod_base_sym = |
| 971 | load_from_obj_file_modbase(fp, fname, obj_xlat, |
| 972 | mod_name != 0 ? mod_name : txt, |
| 973 | mod_name != 0 ? mod_name_len : len, |
| 974 | FALSE); |
| 975 | |
| 976 | /* if that failed, return failure */ |
| 977 | if (mod_base_sym == 0) |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * If this is a 'modifed extern' symbol, it's just a placeholder to |
| 983 | * connect the bottom object in the stack of modified objects in |
| 984 | * this file with the top object in another object file. |
| 985 | */ |
| 986 | CTcSymObj *sym = 0; |
| 987 | if (is_extern && modified_flag) |
| 988 | { |
| 989 | /* |
| 990 | * We're modifying an external object. This must be the bottom |
| 991 | * object in the stack for this object file, and serves as a |
| 992 | * placeholder for the top object in a stack in another object |
| 993 | * file. We must find the object with the name of our top-level |
| 994 | * object (not the fake name for this modified base object, but |
| 995 | * the real name for the top-level object, because the symbol |
| 996 | * we're modifying in the other file is the top object in its |
| 997 | * stack, if any). So, look up the symbol in the other file, |
| 998 | * which must already be loaded. |
| 999 | */ |
| 1000 | sym = (CTcSymObj *) |
| 1001 | G_prs->get_global_symtab()->find(mod_name, mod_name_len); |
| 1002 | |
| 1003 | /* |
| 1004 | * If the original base symbol wasn't an object of metaclass |
| 1005 | * "TADS Object", we can't modify it. |
| 1006 | */ |
| 1007 | if (sym != 0 |
| 1008 | && (sym->get_type() != TC_SYM_OBJ |
| 1009 | || sym->get_metaclass() != TC_META_TADSOBJ)) |
| 1010 | { |
| 1011 | /* log an error */ |
| 1012 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 1013 | TCERR_OBJFILE_CANNOT_MOD_OR_REP_TYPE, |
| 1014 | (int)sym->get_sym_len(), sym->get_sym(), |
| 1015 | fname); |
| 1016 | |
| 1017 | /* forget the symbol */ |
| 1018 | sym = 0; |
| 1019 | } |
| 1020 | |
| 1021 | /* create a synthesized object to hold the original definition */ |
| 1022 | CTcSymObj *mod_sym = synthesize_modified_obj_sym(FALSE); |
| 1023 | |
| 1024 | /* transfer data to the new fake symbol */ |
| 1025 | if (sym != 0) |
| 1026 | { |
| 1027 | /* |
| 1028 | * 'sym' has the original version of the object from the |
| 1029 | * other object file - the original object file must be |
| 1030 | * loaded before an object file that modifies a symbol it |
| 1031 | * exports, so 'sym' will definitely be present in this case |
| 1032 | * (it's an error - undefined external symbol - that we will |
| 1033 | * have already caught if it's not defined). We want to |
| 1034 | * hijack 'sym' for our own use, since 'modify' replaces the |
| 1035 | * symbol's meaning with the new object data. |
| 1036 | * |
| 1037 | * Transfer the self-reference fixup list from the original |
| 1038 | * version of the object to the new synthesized object -- |
| 1039 | * all of the self-references must now refer to the |
| 1040 | * renumbered object. |
| 1041 | * |
| 1042 | * This is really all we need to do to renumber the object. |
| 1043 | * By moving the self-fixup list to the new fake object, we |
| 1044 | * ensure that the original object will use its new number, |
| 1045 | * which leaves the original number for our use in the new, |
| 1046 | * modifying object (i.e., the one we're loading now). Note |
| 1047 | * that we'll replace the self-fixup list for this symbol |
| 1048 | * with the fixup list of the modifying symbol, below. |
| 1049 | */ |
| 1050 | mod_sym->set_fixups(sym->get_fixups()); |
| 1051 | |
| 1052 | /* |
| 1053 | * Give the modified fake symbol the original pre-modified |
| 1054 | * object data stream. The fake symbol owns the |
| 1055 | * pre-modified data stream because it's the pre-modified |
| 1056 | * object. |
| 1057 | */ |
| 1058 | mod_sym->set_stream_ofs(sym->get_stream_ofs()); |
| 1059 | |
| 1060 | /* |
| 1061 | * transfer the 'modify' base symbol from the original |
| 1062 | * version of this symbol to the new fake version |
| 1063 | */ |
| 1064 | mod_sym->set_mod_base_sym(sym->get_mod_base_sym()); |
| 1065 | |
| 1066 | /* transfer the property deletion list */ |
| 1067 | mod_sym->set_del_prop_head(sym->get_first_del_prop()); |
| 1068 | sym->set_del_prop_head(0); |
| 1069 | |
| 1070 | /* |
| 1071 | * mark the original object as a 'class' object - it might |
| 1072 | * have been compiled as a normal instance in its own |
| 1073 | * translation unit, but it's now a class because it's the |
| 1074 | * base class for this link-time 'modify' |
| 1075 | */ |
| 1076 | mod_sym->mark_compiled_as_class(); |
| 1077 | |
| 1078 | /* transfer the dictionary to the base symbol */ |
| 1079 | mod_sym->set_dict(sym->get_dict()); |
| 1080 | |
| 1081 | /* copy the class flag to the base symbol */ |
| 1082 | mod_sym->set_is_class(sym->is_class()); |
| 1083 | |
| 1084 | /* set our class flag to the one from the original symbol */ |
| 1085 | class_flag = sym->is_class(); |
| 1086 | |
| 1087 | /* |
| 1088 | * transfer the superclass list from the original symbol to |
| 1089 | * the modified base symbol |
| 1090 | */ |
| 1091 | mod_sym->set_sc_head(sym->get_sc_head()); |
| 1092 | sym->set_sc_head(0); |
| 1093 | |
| 1094 | /* transfer the vocabulary list to the modified base symbol */ |
| 1095 | mod_sym->set_vocab_head(sym->get_vocab_head()); |
| 1096 | sym->set_vocab_head(0); |
| 1097 | } |
| 1098 | |
| 1099 | /* do the remaining loading into the synthesized placeholder */ |
| 1100 | sym = mod_sym; |
| 1101 | } |
| 1102 | else if (modified_flag) |
| 1103 | { |
| 1104 | /* |
| 1105 | * The symbol was modified, so the name is fake. Because the |
| 1106 | * name is tied to the object ID, which can change between the |
| 1107 | * the time of writing the object file and now, when we're |
| 1108 | * loading the object file, we must synthesize a new fake name |
| 1109 | * in the context of the loaded object file. The name is based |
| 1110 | * on the object number, which is why it must be re-synthesized |
| 1111 | * - the object number in this scheme can be different than the |
| 1112 | * original object number in the object file. |
| 1113 | */ |
| 1114 | sym = synthesize_modified_obj_sym(FALSE); |
| 1115 | |
| 1116 | /* set the appropriate metaclass */ |
| 1117 | sym->set_metaclass(meta); |
| 1118 | } |
| 1119 | else if (anon) |
| 1120 | { |
| 1121 | /* |
| 1122 | * we will definitely not find a previous entry for an anonymous |
| 1123 | * symbol, because there's no name to look up |
| 1124 | */ |
| 1125 | sym = 0; |
| 1126 | } |
| 1127 | else |
| 1128 | { |
| 1129 | /* |
| 1130 | * normal object - look up a previous definition of the symbol |
| 1131 | * in the global symbol table |
| 1132 | */ |
| 1133 | sym = (CTcSymObj *)G_prs->get_global_symtab()->find(txt, len); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * If this symbol is already defined, make sure the original |
| 1138 | * definition is an object, and make sure that it's only defined |
| 1139 | * (not referenced as external) once. If it's not defined, define |
| 1140 | * it anew. |
| 1141 | */ |
| 1142 | if (sym != 0 && sym->get_type() != TC_SYM_OBJ) |
| 1143 | { |
| 1144 | /* |
| 1145 | * It's already defined, but it's not an object - log a symbol |
| 1146 | * type conflict error |
| 1147 | */ |
| 1148 | sym->log_objfile_conflict(fname, TC_SYM_OBJ); |
| 1149 | |
| 1150 | /* |
| 1151 | * proceed despite the error, since this is merely a symbol |
| 1152 | * conflict and not a file corruption - create a fake symbol to |
| 1153 | * hold the data of the original symbol so we can continue |
| 1154 | * loading |
| 1155 | */ |
| 1156 | sym = 0; |
| 1157 | use_fake_sym = TRUE; |
| 1158 | } |
| 1159 | else if ((ext_replace_flag || ext_modify_flag) |
| 1160 | && sym != 0 && sym->get_metaclass() != TC_META_TADSOBJ) |
| 1161 | { |
| 1162 | /* cannot modify or replace anything but an ordinary object */ |
| 1163 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 1164 | TCERR_OBJFILE_CANNOT_MOD_OR_REP_TYPE, |
| 1165 | (int)sym->get_sym_len(), sym->get_sym(), |
| 1166 | fname); |
| 1167 | |
| 1168 | /* forget that we're doing a replacement */ |
| 1169 | ext_replace_flag = ext_modify_flag = FALSE; |
| 1170 | } |
| 1171 | else if (sym != 0 |
| 1172 | && (sym->get_metaclass() == TC_META_DICT |
| 1173 | || sym->get_metaclass() == TC_META_GRAMPROD)) |
| 1174 | { |
| 1175 | /* |
| 1176 | * If this is a dictionary or grammar production object, and the |
| 1177 | * original definition was of the same metaclass, allow the |
| 1178 | * multiple definitions without conflict - just treat the new |
| 1179 | * definition as external. These object types don't require a |
| 1180 | * primary definition - every time such an object is defined, |
| 1181 | * it's a definition, but the same definition can appear in |
| 1182 | * multiple object files without conflict. Simply act as though |
| 1183 | * this new declaration is extern after all in this case. |
| 1184 | */ |
| 1185 | if (meta == sym->get_metaclass()) |
| 1186 | { |
| 1187 | /* |
| 1188 | * it's another one of the same type - allow it without |
| 1189 | * conflict; act as though this new definition is external |
| 1190 | */ |
| 1191 | if (!sym->is_extern()) |
| 1192 | is_extern = FALSE; |
| 1193 | } |
| 1194 | else |
| 1195 | { |
| 1196 | /* the other one's of a different type - log a conflict */ |
| 1197 | sym->log_objfile_conflict(fname, TC_SYM_OBJ); |
| 1198 | |
| 1199 | /* proceed with a fake symbol */ |
| 1200 | sym = 0; |
| 1201 | use_fake_sym = TRUE; |
| 1202 | } |
| 1203 | } |
| 1204 | else if ((ext_replace_flag || ext_modify_flag) |
| 1205 | && (sym == 0 || sym->is_extern())) |
| 1206 | { |
| 1207 | /* |
| 1208 | * This symbol isn't defined yet, or is only defined as an |
| 1209 | * external, but the new symbol is marked as 'replace' or |
| 1210 | * 'modify' - it's an error, because the original version of an |
| 1211 | * object must always be loaded before the replaced or modified |
| 1212 | * version |
| 1213 | */ |
| 1214 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 1215 | TCERR_OBJFILE_MODREPOBJ_BEFORE_ORIG, |
| 1216 | (int)len, txt, fname); |
| 1217 | |
| 1218 | /* forget the symbol */ |
| 1219 | sym = 0; |
| 1220 | |
| 1221 | /* not replacing anything after all */ |
| 1222 | ext_replace_flag = ext_modify_flag = FALSE; |
| 1223 | } |
| 1224 | else if (sym != 0 |
| 1225 | && !sym->is_extern() |
| 1226 | && !(is_extern || ext_modify_flag || ext_replace_flag |
| 1227 | || modified_flag)) |
| 1228 | { |
| 1229 | /* |
| 1230 | * the symbol was already defined, and this is a new actual |
| 1231 | * definition (not external, and not replace or modify) -- this |
| 1232 | * is an error because it means the same object is defined more |
| 1233 | * than once |
| 1234 | */ |
| 1235 | sym->log_objfile_conflict(fname, TC_SYM_OBJ); |
| 1236 | |
| 1237 | /* |
| 1238 | * proceed despite the error, since this is merely a symbol |
| 1239 | * conflict and not a file corruption - create a fake symbol to |
| 1240 | * hold the data of the original symbol so we can continue |
| 1241 | * loading |
| 1242 | */ |
| 1243 | sym = 0; |
| 1244 | use_fake_sym = TRUE; |
| 1245 | } |
| 1246 | else if (sym != 0 && meta != sym->get_metaclass()) |
| 1247 | { |
| 1248 | /* |
| 1249 | * the new symbol and the old symbol have different metaclasses |
| 1250 | * - it's a conflict |
| 1251 | */ |
| 1252 | sym->log_objfile_conflict(fname, TC_SYM_OBJ); |
| 1253 | |
| 1254 | /* proceed with a fake symbol */ |
| 1255 | sym = 0; |
| 1256 | use_fake_sym = TRUE; |
| 1257 | } |
| 1258 | |
| 1259 | /* create the object if necessary */ |
| 1260 | if (sym == 0) |
| 1261 | { |
| 1262 | /* |
| 1263 | * The symbol isn't defined yet - create the new definition and |
| 1264 | * add it to the symbol table. Allocate a new object ID for the |
| 1265 | * symbol in the normal fashion. |
| 1266 | */ |
| 1267 | sym = new CTcSymObj(txt, len, FALSE, G_cg->new_obj_id(), |
| 1268 | is_extern, meta, 0); |
| 1269 | |
| 1270 | /* |
| 1271 | * if we're using a fake symbol, don't bother adding the symbol |
| 1272 | * to the symbol table, since its only function is to allow us |
| 1273 | * to finish reading the object file data (we won't actually try |
| 1274 | * to link when using a fake symbol, since this always means |
| 1275 | * that an error has made linking impossible; we'll proceed |
| 1276 | * anyway so that we catch any other errors that remain to be |
| 1277 | * found) |
| 1278 | * |
| 1279 | * similarly, don't add the symbol if it's anonymous |
| 1280 | */ |
| 1281 | if (!use_fake_sym && !anon) |
| 1282 | G_prs->get_global_symtab()->add_entry(sym); |
| 1283 | |
| 1284 | /* if it's anonymous, add it to the anonymous symbol list */ |
| 1285 | if (anon) |
| 1286 | G_prs->add_anon_obj(sym); |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * If we're replacing the object, tell the code generator to get rid |
| 1291 | * of the old object definition in the object stream -- delete the |
| 1292 | * definition at the symbol's old stream offset. |
| 1293 | */ |
| 1294 | if (ext_replace_flag) |
| 1295 | G_cg->notify_replace_object(sym->get_stream_ofs()); |
| 1296 | |
| 1297 | /* |
| 1298 | * If this is a non-extern definition, we now have the object |
| 1299 | * defined -- remove the 'extern' flag from the symbol table entry, |
| 1300 | * and set the symbol's data to the data we just read. Do not |
| 1301 | * transfer data to the symbol if this is an extern, since we want |
| 1302 | * to use the existing data from the originally loaded object. |
| 1303 | */ |
| 1304 | if (!is_extern) |
| 1305 | { |
| 1306 | /* clear the external flag */ |
| 1307 | sym->set_extern(FALSE); |
| 1308 | |
| 1309 | /* set the object's stream offset, if we read one */ |
| 1310 | if (stream_ofs_valid) |
| 1311 | sym->set_stream_ofs(stream_ofs); |
| 1312 | |
| 1313 | /* set the base 'modify' symbol if this symbol modifies another */ |
| 1314 | if (mod_base_sym != 0) |
| 1315 | sym->set_mod_base_sym(mod_base_sym); |
| 1316 | |
| 1317 | /* set the new symbol's fixup list */ |
| 1318 | sym->set_fixups(fixups); |
| 1319 | |
| 1320 | /* set the new symbol's deleted property list */ |
| 1321 | sym->set_del_prop_head(del_prop_head); |
| 1322 | |
| 1323 | /* |
| 1324 | * set the symbol's class flag - only add the class flag, |
| 1325 | * because we might have already set the class flag for this |
| 1326 | * symbol based on the external definition |
| 1327 | */ |
| 1328 | if (class_flag) |
| 1329 | sym->set_is_class(class_flag); |
| 1330 | } |
| 1331 | |
| 1332 | /* add this symbol to the load file object index list */ |
| 1333 | G_prs->add_sym_from_obj_file(obj_file_idx, sym); |
| 1334 | |
| 1335 | /* set the dictionary, if one was specified */ |
| 1336 | if (dict_idx != 0) |
| 1337 | sym->set_dict(G_prs->get_obj_dict(dict_idx)); |
| 1338 | |
| 1339 | /* |
| 1340 | * if this is a dictionary symbol, add it to the dictionary fixup |
| 1341 | * list |
| 1342 | */ |
| 1343 | if (meta == TC_META_DICT) |
| 1344 | G_prs->add_dict_from_obj_file(sym); |
| 1345 | |
| 1346 | /* set the transient flag */ |
| 1347 | if (trans_flag) |
| 1348 | sym->set_transient(); |
| 1349 | |
| 1350 | /* |
| 1351 | * Set the translation table entry for the symbol. We know the |
| 1352 | * original ID local to the object file, and we know the new global |
| 1353 | * object ID. |
| 1354 | */ |
| 1355 | obj_xlat[id] = sym->get_obj_id(); |
| 1356 | |
| 1357 | /* success */ |
| 1358 | return sym; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * Apply our self-reference fixups |
| 1363 | */ |
| 1364 | void CTcSymObjBase::apply_internal_fixups() |
| 1365 | { |
| 1366 | /* run through our list and apply each fixup */ |
| 1367 | for (CTcIdFixup *fixup = fixups_ ; fixup != 0 ; fixup = fixup->nxt_) |
| 1368 | fixup->apply_fixup(obj_id_, 4); |
| 1369 | |
| 1370 | /* |
| 1371 | * If we're a 'modify' object, and we were based at compile-time on |
| 1372 | * an object external to the translation unit in which this modified |
| 1373 | * version of the object was defined, we'll have a property deletion |
| 1374 | * list to be applied at link time. Now is the time - go through |
| 1375 | * our list and delete each property in each of our 'modify' base |
| 1376 | * classes. Don't delete the properties in our own object, |
| 1377 | * obviously - just in our modified base classes. |
| 1378 | */ |
| 1379 | for (CTcSymObj *mod_base = mod_base_sym_ ; mod_base != 0 ; |
| 1380 | mod_base = mod_base->get_mod_base_sym()) |
| 1381 | { |
| 1382 | /* delete each property in our deletion list in this base class */ |
| 1383 | for (CTcObjPropDel *entry = first_del_prop_ ; entry != 0 ; |
| 1384 | entry = entry->nxt_) |
| 1385 | { |
| 1386 | /* delete this property from the base object */ |
| 1387 | mod_base->delete_prop_from_mod_base(entry->prop_sym_->get_prop()); |
| 1388 | |
| 1389 | /* remove it from the base object's vocabulary list */ |
| 1390 | mod_base->delete_vocab_prop(entry->prop_sym_->get_prop()); |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Merge my private grammar rules into the master rule list for the |
| 1397 | * associated grammar production object. |
| 1398 | */ |
| 1399 | void CTcSymObjBase::merge_grammar_entry() |
| 1400 | { |
| 1401 | /* if I don't have a grammar list, there's nothing to do */ |
| 1402 | if (grammar_entry_ == 0) |
| 1403 | return; |
| 1404 | |
| 1405 | /* get the grammar production object my rules are associated with */ |
| 1406 | CTcSymObj *prod_sym = grammar_entry_->get_prod_sym(); |
| 1407 | |
| 1408 | /* get the master list for the production */ |
| 1409 | CTcGramProdEntry *master_entry = G_prs->get_gramprod_entry(prod_sym); |
| 1410 | |
| 1411 | /* move the alternatives from my private list to the master list */ |
| 1412 | grammar_entry_->move_alts_to(master_entry); |
| 1413 | } |
| 1414 | |
| 1415 | |
| 1416 | /* ------------------------------------------------------------------------ */ |
| 1417 | /* |
| 1418 | * metaclass symbol base - image/object file functions |
| 1419 | */ |
| 1420 | |
| 1421 | /* |
| 1422 | * load from an object file |
| 1423 | */ |
| 1424 | int CTcSymMetaclassBase:: |
| 1425 | load_from_obj_file(CVmFile *fp, const textchar_t *fname, |
| 1426 | tctarg_obj_id_t *obj_xlat) |
| 1427 | { |
| 1428 | const char *txt; |
| 1429 | size_t len; |
| 1430 | int meta_idx; |
| 1431 | int prop_cnt; |
| 1432 | CTcSymMetaclass *sym; |
| 1433 | char buf[TOK_SYM_MAX_LEN + 1]; |
| 1434 | CTcSymMetaProp *prop; |
| 1435 | int was_defined; |
| 1436 | tctarg_obj_id_t class_obj; |
| 1437 | |
| 1438 | /* read the symbol name */ |
| 1439 | if ((txt = base_read_from_sym_file(fp)) == 0) |
| 1440 | return 1; |
| 1441 | len = strlen(txt); |
| 1442 | |
| 1443 | /* read the metaclass index, class object ID, and property count */ |
| 1444 | fp->read_bytes(buf, 8); |
| 1445 | meta_idx = osrp2(buf); |
| 1446 | class_obj = t3rp4u(buf + 2); |
| 1447 | prop_cnt = osrp2(buf + 6); |
| 1448 | |
| 1449 | /* check for a previous definition */ |
| 1450 | sym = (CTcSymMetaclass *)G_prs->get_global_symtab()->find(txt, len); |
| 1451 | if (sym == 0) |
| 1452 | { |
| 1453 | /* it's not defined yet - create the new definition */ |
| 1454 | sym = new CTcSymMetaclass(txt, len, FALSE, meta_idx, |
| 1455 | G_cg->new_obj_id()); |
| 1456 | G_prs->get_global_symtab()->add_entry(sym); |
| 1457 | |
| 1458 | /* note that it wasn't yet defined */ |
| 1459 | was_defined = FALSE; |
| 1460 | |
| 1461 | /* set the metaclass symbol pointer in the dependency table */ |
| 1462 | G_cg->set_meta_sym(meta_idx, sym); |
| 1463 | } |
| 1464 | else if (sym->get_type() != TC_SYM_METACLASS) |
| 1465 | { |
| 1466 | /* log a conflict */ |
| 1467 | sym->log_objfile_conflict(fname, TC_SYM_METACLASS); |
| 1468 | |
| 1469 | /* forget the symbol */ |
| 1470 | sym = 0; |
| 1471 | was_defined = FALSE; |
| 1472 | } |
| 1473 | else |
| 1474 | { |
| 1475 | /* if the metaclass index doesn't match, it's an error */ |
| 1476 | if (sym->get_meta_idx() != meta_idx) |
| 1477 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 1478 | TCERR_OBJFILE_METACLASS_IDX_CONFLICT, |
| 1479 | (int)len, txt, fname); |
| 1480 | |
| 1481 | /* note that it was previously defined */ |
| 1482 | was_defined = TRUE; |
| 1483 | |
| 1484 | /* start with the first property */ |
| 1485 | prop = sym->get_prop_head(); |
| 1486 | } |
| 1487 | |
| 1488 | /* set the ID translation for the class object */ |
| 1489 | if (sym != 0) |
| 1490 | obj_xlat[class_obj] = sym->get_class_obj(); |
| 1491 | |
| 1492 | /* read the property names */ |
| 1493 | for ( ; prop_cnt != 0 ; --prop_cnt) |
| 1494 | { |
| 1495 | int is_static; |
| 1496 | |
| 1497 | /* read the property symbol name */ |
| 1498 | if ((txt = base_read_from_sym_file(fp)) == 0) |
| 1499 | return 1; |
| 1500 | len = strlen(txt); |
| 1501 | |
| 1502 | /* read the flags */ |
| 1503 | fp->read_bytes(buf, 1); |
| 1504 | is_static = ((buf[0] & 1) != 0); |
| 1505 | |
| 1506 | /* check what we're doing */ |
| 1507 | if (sym == 0) |
| 1508 | { |
| 1509 | /* |
| 1510 | * we have a conflict, so we're just scanning the names to |
| 1511 | * keep in sync with the file - ignore it |
| 1512 | */ |
| 1513 | } |
| 1514 | else if (was_defined) |
| 1515 | { |
| 1516 | /* |
| 1517 | * the metaclass was previously defined - simply check to |
| 1518 | * ensure that this property matches the corresponding |
| 1519 | * property (by list position) in the original definition |
| 1520 | */ |
| 1521 | if (prop == 0) |
| 1522 | { |
| 1523 | /* |
| 1524 | * we're past the end of the original definition's |
| 1525 | * property list - this is okay, as we can simply add |
| 1526 | * the properties in the new list (which must be a more |
| 1527 | * recent definition than the original one) |
| 1528 | */ |
| 1529 | sym->add_prop(txt, len, fname, is_static); |
| 1530 | } |
| 1531 | else if (prop->prop_->get_sym_len() != len |
| 1532 | || memcmp(prop->prop_->get_sym(), txt, len) != 0) |
| 1533 | { |
| 1534 | /* this one doesn't match - it's an error */ |
| 1535 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, |
| 1536 | TCERR_OBJFILE_METACLASS_PROP_CONFLICT, |
| 1537 | (int)len, txt, |
| 1538 | (int)prop->prop_->get_sym_len(), |
| 1539 | prop->prop_->get_sym(), fname); |
| 1540 | } |
| 1541 | |
| 1542 | /* move on to the next property in the list */ |
| 1543 | if (prop != 0) |
| 1544 | prop = prop->nxt_; |
| 1545 | } |
| 1546 | else |
| 1547 | { |
| 1548 | /* |
| 1549 | * we're defining the metaclass anew - add this property to |
| 1550 | * the metaclass's property list |
| 1551 | */ |
| 1552 | sym->add_prop(txt, len, fname, is_static); |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | /* read our modifier object flag */ |
| 1557 | fp->read_bytes(buf, 1); |
| 1558 | if (buf[0] != 0) |
| 1559 | { |
| 1560 | /* laod the new object */ |
| 1561 | CTcSymObj *mod_obj; |
| 1562 | |
| 1563 | /* we have a modification object - load it */ |
| 1564 | mod_obj = CTcSymObj::load_from_obj_file_modbase( |
| 1565 | fp, fname, obj_xlat, 0, 0, FALSE); |
| 1566 | |
| 1567 | /* |
| 1568 | * if the metaclass already has a modification object, then the |
| 1569 | * bottom of the chain we just loaded modifies the top of the |
| 1570 | * existing chain |
| 1571 | */ |
| 1572 | if (sym->get_mod_obj() != 0) |
| 1573 | { |
| 1574 | CTcSymObj *obj; |
| 1575 | CTcSymObj *prv; |
| 1576 | |
| 1577 | /* |
| 1578 | * Set the bottom of the new chain to point to the top of |
| 1579 | * the existing chain. The bottom object in each object |
| 1580 | * file's modification chain is always a dummy root object; |
| 1581 | * we'll thus find the second to last object in the new |
| 1582 | * chain, and replace the pointer to its dummy root |
| 1583 | * superclass with a pointer to the top of the |
| 1584 | * previously-loaded chain that we're modifying. |
| 1585 | */ |
| 1586 | |
| 1587 | /* find the second-to-last object in the new chain */ |
| 1588 | for (prv = 0, obj = mod_obj ; |
| 1589 | obj != 0 && obj->get_mod_base_sym() != 0 ; |
| 1590 | prv = obj, obj = obj->get_mod_base_sym()) ; |
| 1591 | |
| 1592 | /* |
| 1593 | * if we found the second-to-last object, set up the link |
| 1594 | * back into the old chain |
| 1595 | */ |
| 1596 | if (prv != 0) |
| 1597 | prv->set_mod_base_sym(sym->get_mod_obj()); |
| 1598 | } |
| 1599 | |
| 1600 | /* point the metaclass to the modification object */ |
| 1601 | sym->set_mod_obj(mod_obj); |
| 1602 | } |
| 1603 | |
| 1604 | /* return success - the file appears well-formed */ |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | /* ------------------------------------------------------------------------ */ |
| 1610 | /* |
| 1611 | * property symbol entry base - image/object file functions |
| 1612 | */ |
| 1613 | |
| 1614 | /* |
| 1615 | * Load from an object file |
| 1616 | */ |
| 1617 | int CTcSymPropBase::load_from_obj_file(class CVmFile *fp, |
| 1618 | const textchar_t *fname, |
| 1619 | tctarg_prop_id_t *prop_xlat) |
| 1620 | { |
| 1621 | /* read the symbol name information */ |
| 1622 | const char *txt; |
| 1623 | if ((txt = base_read_from_sym_file(fp)) == 0) |
| 1624 | return 1; |
| 1625 | size_t len = strlen(txt); |
| 1626 | |
| 1627 | /* read our property ID */ |
| 1628 | ulong id = fp->read_uint4(); |
| 1629 | |
| 1630 | /* |
| 1631 | * If this symbol is already defined, make sure the original |
| 1632 | * definition is a property. If it's not defined, define it anew. |
| 1633 | */ |
| 1634 | CTcSymProp *sym = (CTcSymProp *)G_prs->get_global_symtab()->find(txt, len); |
| 1635 | if (sym == 0) |
| 1636 | { |
| 1637 | /* |
| 1638 | * It's not defined yet - create the new definition and add it |
| 1639 | * to the symbol table. Allocate a new property ID for the |
| 1640 | * symbol in the normal fashion. |
| 1641 | */ |
| 1642 | sym = new CTcSymProp(txt, len, FALSE, G_cg->new_prop_id()); |
| 1643 | G_prs->get_global_symtab()->add_entry(sym); |
| 1644 | } |
| 1645 | else if (sym->get_type() != TC_SYM_PROP) |
| 1646 | { |
| 1647 | /* |
| 1648 | * It's not already defined as a property - log a symbol type |
| 1649 | * conflict error |
| 1650 | */ |
| 1651 | sym->log_objfile_conflict(fname, TC_SYM_PROP); |
| 1652 | |
| 1653 | /* |
| 1654 | * proceed despite the error, since this is merely a symbol |
| 1655 | * conflict and not a file corruption |
| 1656 | */ |
| 1657 | return 0; |
| 1658 | } |
| 1659 | |
| 1660 | /* |
| 1661 | * Set the translation table entry for the symbol. We know the |
| 1662 | * original ID local to the object file, and we know the new global |
| 1663 | * property ID. |
| 1664 | */ |
| 1665 | prop_xlat[id] = sym->get_prop(); |
| 1666 | |
| 1667 | /* success */ |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | /* ------------------------------------------------------------------------ */ |
| 1672 | /* |
| 1673 | * enumerator symbol entry base - image/object file functions |
| 1674 | */ |
| 1675 | |
| 1676 | /* |
| 1677 | * Load from an object file |
| 1678 | */ |
| 1679 | int CTcSymEnumBase::load_from_obj_file(class CVmFile *fp, |
| 1680 | const textchar_t *fname, |
| 1681 | ulong *enum_xlat) |
| 1682 | { |
| 1683 | const char *txt; |
| 1684 | size_t len; |
| 1685 | ulong id; |
| 1686 | CTcSymEnum *sym; |
| 1687 | char buf[32]; |
| 1688 | int is_token; |
| 1689 | |
| 1690 | /* read the symbol name information */ |
| 1691 | if ((txt = base_read_from_sym_file(fp)) == 0) |
| 1692 | return 1; |
| 1693 | len = strlen(txt); |
| 1694 | |
| 1695 | /* read our enumerator ID */ |
| 1696 | id = fp->read_uint4(); |
| 1697 | |
| 1698 | /* read our flags */ |
| 1699 | fp->read_bytes(buf, 1); |
| 1700 | |
| 1701 | /* get the 'token' flag */ |
| 1702 | is_token = ((buf[0] & 1) != 0); |
| 1703 | |
| 1704 | /* |
| 1705 | * If this symbol is already defined, make sure the original |
| 1706 | * definition is an enum. If it's not defined, define it anew. |
| 1707 | */ |
| 1708 | sym = (CTcSymEnum *)G_prs->get_global_symtab()->find(txt, len); |
| 1709 | if (sym == 0) |
| 1710 | { |
| 1711 | /* |
| 1712 | * It's not defined yet - create the new definition and add it |
| 1713 | * to the symbol table. Allocate a new enumerator ID for the |
| 1714 | * symbol in the normal fashion. |
| 1715 | */ |
| 1716 | sym = new CTcSymEnum(txt, len, FALSE, G_prs->new_enum_id(), is_token); |
| 1717 | G_prs->get_global_symtab()->add_entry(sym); |
| 1718 | } |
| 1719 | else if (sym->get_type() != TC_SYM_ENUM) |
| 1720 | { |
| 1721 | /* |
| 1722 | * It's not already defined as an enumerator - log a symbol type |
| 1723 | * conflict error |
| 1724 | */ |
| 1725 | sym->log_objfile_conflict(fname, TC_SYM_ENUM); |
| 1726 | |
| 1727 | /* |
| 1728 | * proceed despite the error, since this is merely a symbol |
| 1729 | * conflict and not a file corruption |
| 1730 | */ |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | /* |
| 1735 | * Set the translation table entry for the symbol. We know the |
| 1736 | * original ID local to the object file, and we know the new global |
| 1737 | * enum ID. |
| 1738 | */ |
| 1739 | enum_xlat[id] = sym->get_enum_id(); |
| 1740 | |
| 1741 | /* success */ |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | /* ------------------------------------------------------------------------ */ |
| 1746 | /* |
| 1747 | * Built-in function symbol base - image/object file functions |
| 1748 | */ |
| 1749 | |
| 1750 | /* |
| 1751 | * load from an object file |
| 1752 | */ |
| 1753 | int CTcSymBifBase::load_from_obj_file(class CVmFile *fp, |
| 1754 | const textchar_t *fname) |
| 1755 | { |
| 1756 | const char *txt; |
| 1757 | size_t len; |
| 1758 | CTcSymBif *sym; |
| 1759 | char buf[10]; |
| 1760 | ushort func_set_id; |
| 1761 | ushort func_idx; |
| 1762 | int has_retval; |
| 1763 | int min_argc; |
| 1764 | int max_argc; |
| 1765 | int varargs; |
| 1766 | |
| 1767 | /* read the symbol name information */ |
| 1768 | if ((txt = base_read_from_sym_file(fp)) == 0) |
| 1769 | return 1; |
| 1770 | len = strlen(txt); |
| 1771 | |
| 1772 | /* read our additional information */ |
| 1773 | fp->read_bytes(buf, 10); |
| 1774 | varargs = buf[0]; |
| 1775 | has_retval = buf[1]; |
| 1776 | min_argc = osrp2(buf+2); |
| 1777 | max_argc = osrp2(buf+4); |
| 1778 | func_set_id = osrp2(buf+6); |
| 1779 | func_idx = osrp2(buf+8); |
| 1780 | |
| 1781 | /* |
| 1782 | * If this symbol is already defined, make sure the new definition |
| 1783 | * matches the original definition - built-in function sets must be |
| 1784 | * identical in all object files loaded. If it's not already |
| 1785 | * defined, add it now. |
| 1786 | */ |
| 1787 | sym = (CTcSymBif *)G_prs->get_global_symtab()->find(txt, len); |
| 1788 | if (sym == 0) |
| 1789 | { |
| 1790 | /* |
| 1791 | * it's not defined yet - create the new definition and add it |
| 1792 | * to the symbol table |
| 1793 | */ |
| 1794 | sym = new CTcSymBif(txt, len, FALSE, func_set_id, func_idx, |
| 1795 | has_retval, min_argc, max_argc, varargs); |
| 1796 | G_prs->get_global_symtab()->add_entry(sym); |
| 1797 | } |
| 1798 | else if (sym->get_type() != TC_SYM_BIF) |
| 1799 | { |
| 1800 | /* log the error */ |
| 1801 | sym->log_objfile_conflict(fname, TC_SYM_BIF); |
| 1802 | } |
| 1803 | else if (sym->get_func_set_id() != func_set_id |
| 1804 | || sym->get_func_idx() != func_idx |
| 1805 | || sym->get_min_argc() != min_argc |
| 1806 | || sym->get_max_argc() != max_argc |
| 1807 | || sym->is_varargs() != varargs |
| 1808 | || sym->has_retval() != has_retval) |
| 1809 | { |
| 1810 | /* |
| 1811 | * this function is already defined but has different settings |
| 1812 | * -- we cannot reconcile the different usages of the function, |
| 1813 | * so this is an error |
| 1814 | */ |
| 1815 | G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_OBJFILE_BIF_INCOMPAT, |
| 1816 | (int)len, txt, fname); |
| 1817 | } |
| 1818 | else |
| 1819 | { |
| 1820 | /* |
| 1821 | * everything about the symbol matches - there's no need to |
| 1822 | * redefine the symbol, since it's already set up exactly as we |
| 1823 | * need it to be |
| 1824 | */ |
| 1825 | } |
| 1826 | |
| 1827 | /* continue reading the file */ |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | /* ------------------------------------------------------------------------ */ |
| 1832 | /* |
| 1833 | * Grammar production list entry |
| 1834 | */ |
| 1835 | |
| 1836 | /* |
| 1837 | * load from an object file |
| 1838 | */ |
| 1839 | void CTcGramProdEntry::load_from_obj_file( |
| 1840 | CVmFile *fp, const tctarg_prop_id_t *prop_xlat, const ulong *enum_xlat, |
| 1841 | CTcSymObj *private_owner) |
| 1842 | { |
| 1843 | uint idx; |
| 1844 | ulong cnt; |
| 1845 | CTcSymObj *obj; |
| 1846 | CTcGramProdEntry *prod; |
| 1847 | ulong flags; |
| 1848 | |
| 1849 | /* |
| 1850 | * read the object file index of the production object, and get the |
| 1851 | * production object |
| 1852 | */ |
| 1853 | idx = fp->read_uint4(); |
| 1854 | obj = G_prs->get_objfile_objsym(idx); |
| 1855 | |
| 1856 | /* declare the production object */ |
| 1857 | prod = G_prs->declare_gramprod(obj->get_sym(), obj->get_sym_len()); |
| 1858 | |
| 1859 | /* if we have a private owner, create a private rule list */ |
| 1860 | if (private_owner != 0) |
| 1861 | prod = private_owner->create_grammar_entry( |
| 1862 | obj->get_sym(), obj->get_sym_len()); |
| 1863 | |
| 1864 | /* read the flags */ |
| 1865 | flags = fp->read_uint4(); |
| 1866 | |
| 1867 | /* set the explicitly-declared flag if appropriate */ |
| 1868 | if (flags & 1) |
| 1869 | prod->set_declared(TRUE); |
| 1870 | |
| 1871 | /* read the alternative count */ |
| 1872 | cnt = fp->read_uint4(); |
| 1873 | |
| 1874 | /* read the alternatives */ |
| 1875 | for ( ; cnt != 0 ; --cnt) |
| 1876 | { |
| 1877 | CTcGramProdAlt *alt; |
| 1878 | |
| 1879 | /* read an alternative */ |
| 1880 | alt = CTcGramProdAlt::load_from_obj_file(fp, prop_xlat, enum_xlat); |
| 1881 | |
| 1882 | /* add it to the production's list */ |
| 1883 | if (prod != 0) |
| 1884 | prod->add_alt(alt); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | |
| 1889 | /* ------------------------------------------------------------------------ */ |
| 1890 | /* |
| 1891 | * Grammar production alternative |
| 1892 | */ |
| 1893 | |
| 1894 | /* |
| 1895 | * load from an object file |
| 1896 | */ |
| 1897 | CTcGramProdAlt *CTcGramProdAlt:: |
| 1898 | load_from_obj_file(CVmFile *fp, const tctarg_prop_id_t *prop_xlat, |
| 1899 | const ulong *enum_xlat) |
| 1900 | { |
| 1901 | uint idx; |
| 1902 | ulong cnt; |
| 1903 | CTcSymObj *obj; |
| 1904 | CTcGramProdAlt *alt; |
| 1905 | CTcDictEntry *dict; |
| 1906 | int score; |
| 1907 | int badness; |
| 1908 | |
| 1909 | /* read my score and badness */ |
| 1910 | score = fp->read_int2(); |
| 1911 | badness = fp->read_int2(); |
| 1912 | |
| 1913 | /* read my processor object index, and get the associated object */ |
| 1914 | idx = fp->read_uint4(); |
| 1915 | obj = G_prs->get_objfile_objsym(idx); |
| 1916 | |
| 1917 | /* read my dictionary object index, and get the associated entry */ |
| 1918 | idx = fp->read_uint4(); |
| 1919 | dict = G_prs->get_obj_dict(idx); |
| 1920 | |
| 1921 | /* create the alternative object */ |
| 1922 | alt = new (G_prsmem) CTcGramProdAlt(obj, dict); |
| 1923 | |
| 1924 | /* set the score badness */ |
| 1925 | alt->set_score(score); |
| 1926 | alt->set_badness(badness); |
| 1927 | |
| 1928 | /* read the number of tokens */ |
| 1929 | cnt = fp->read_uint4(); |
| 1930 | |
| 1931 | /* read the tokens */ |
| 1932 | for ( ; cnt != 0 ; --cnt) |
| 1933 | { |
| 1934 | CTcGramProdTok *tok; |
| 1935 | |
| 1936 | /* read a token */ |
| 1937 | tok = CTcGramProdTok::load_from_obj_file(fp, prop_xlat, enum_xlat); |
| 1938 | |
| 1939 | /* add it to the alternative's list */ |
| 1940 | alt->add_tok(tok); |
| 1941 | } |
| 1942 | |
| 1943 | /* return the alternative */ |
| 1944 | return alt; |
| 1945 | } |
| 1946 | |
| 1947 | |
| 1948 | /* ------------------------------------------------------------------------ */ |
| 1949 | /* |
| 1950 | * Grammar production token |
| 1951 | */ |
| 1952 | |
| 1953 | /* |
| 1954 | * load from an object file |
| 1955 | */ |
| 1956 | CTcGramProdTok *CTcGramProdTok:: |
| 1957 | load_from_obj_file(CVmFile *fp, const tctarg_prop_id_t *prop_xlat, |
| 1958 | const ulong *enum_xlat) |
| 1959 | { |
| 1960 | CTcGramProdTok *tok; |
| 1961 | CTcSymObj *obj; |
| 1962 | tcgram_tok_type typ; |
| 1963 | tctarg_prop_id_t prop; |
| 1964 | size_t len; |
| 1965 | char *txt; |
| 1966 | uint idx; |
| 1967 | ulong enum_id; |
| 1968 | size_t i; |
| 1969 | |
| 1970 | /* create a new token */ |
| 1971 | tok = new (G_prsmem) CTcGramProdTok(); |
| 1972 | |
| 1973 | /* read the type */ |
| 1974 | typ = (tcgram_tok_type)fp->read_int2(); |
| 1975 | |
| 1976 | /* read the data, which depends on the type */ |
| 1977 | switch(typ) |
| 1978 | { |
| 1979 | case TCGRAM_PROD: |
| 1980 | /* read the production object's object file index */ |
| 1981 | idx = fp->read_uint4(); |
| 1982 | |
| 1983 | /* translate it to an object */ |
| 1984 | obj = G_prs->get_objfile_objsym(idx); |
| 1985 | |
| 1986 | /* set the production object in the token */ |
| 1987 | tok->set_match_prod(obj); |
| 1988 | break; |
| 1989 | |
| 1990 | case TCGRAM_TOKEN_TYPE: |
| 1991 | /* read the token ID, translating to the new enum numbering */ |
| 1992 | enum_id = enum_xlat[fp->read_uint4()]; |
| 1993 | |
| 1994 | /* set the token-type match */ |
| 1995 | tok->set_match_token_type(enum_id); |
| 1996 | break; |
| 1997 | |
| 1998 | case TCGRAM_PART_OF_SPEECH: |
| 1999 | /* read the property ID, translating to the new numbering system */ |
| 2000 | prop = prop_xlat[fp->read_int2()]; |
| 2001 | |
| 2002 | /* set the part of speech in the token */ |
| 2003 | tok->set_match_part_of_speech(prop); |
| 2004 | break; |
| 2005 | |
| 2006 | case TCGRAM_PART_OF_SPEECH_LIST: |
| 2007 | /* read the list length */ |
| 2008 | len = (size_t)fp->read_int2(); |
| 2009 | |
| 2010 | /* set the type */ |
| 2011 | tok->set_match_part_list(); |
| 2012 | |
| 2013 | /* read each element and add it to the list */ |
| 2014 | for (i = 0 ; i < len ; ++i) |
| 2015 | tok->add_match_part_ele(prop_xlat[fp->read_int2()]); |
| 2016 | |
| 2017 | /* done */ |
| 2018 | break; |
| 2019 | |
| 2020 | case TCGRAM_LITERAL: |
| 2021 | /* read the length of the string */ |
| 2022 | len = (size_t)fp->read_int2(); |
| 2023 | |
| 2024 | /* allocate parser memory to hold the text */ |
| 2025 | txt = (char *)G_prsmem->alloc(len); |
| 2026 | |
| 2027 | /* read the text of the literal */ |
| 2028 | fp->read_bytes(txt, len); |
| 2029 | |
| 2030 | /* set the literal in the token */ |
| 2031 | tok->set_match_literal(txt, len); |
| 2032 | break; |
| 2033 | |
| 2034 | case TCGRAM_STAR: |
| 2035 | /* there's no additional data */ |
| 2036 | tok->set_match_star(); |
| 2037 | break; |
| 2038 | |
| 2039 | case TCGRAM_UNKNOWN: |
| 2040 | /* no extra data to read */ |
| 2041 | break; |
| 2042 | } |
| 2043 | |
| 2044 | /* read and set the property association */ |
| 2045 | tok->set_prop_assoc(prop_xlat[fp->read_int2()]); |
| 2046 | |
| 2047 | /* return the token */ |
| 2048 | return tok; |
| 2049 | } |