Debian Codegraph @

terps/tads/tads3/tcprsfil.cpp

raw symbols SCIP
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 tcprssym.cpp - TADS 3 Compiler Parser, symbol file operations
15 Function
16 This parser module contains code that manages symbol and object files.
17 These routines aren't needed in interpreter builds with dynamic compilation
18 ("eval()" support), so we isolate them out for separate linkage.
19 Notes
20
21 Modified
22 04/30/99 MJRoberts - Creation
23 */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include "os.h"
31 #include "t3std.h"
32 #include "tcprs.h"
33 #include "tctarg.h"
34 #include "tcgen.h"
35 #include "vmhash.h"
36 #include "tcmain.h"
37 #include "vmfile.h"
38 #include "tctok.h"
39
40
41 /* ------------------------------------------------------------------------ */
42 /*
43 * main parser symbol and object file operations
44 */
45
46 /*
47 * Add a symbol loaded from an object file
48 */
49 void CTcParser::add_sym_from_obj_file(uint idx, class CTcSymbol *sym)
50 {
51 /*
52 * add the entry to the object file index list - adjust from the
53 * 1-based index used in the file to an array index
54 */
55 obj_sym_list_[idx - 1] = sym;
56 }
57
58 /*
59 * Get an object file symbol, ensuring that it's an object symbol
60 */
61 CTcSymObj *CTcParser::get_objfile_objsym(uint idx)
62 {
63 CTcSymObj *sym;
64
65 /* get the object based on the index */
66 sym = (CTcSymObj *)get_objfile_sym(idx);
67
68 /* make sure it's an object - if it isn't, return null */
69 if (sym == 0 || sym->get_type() != TC_SYM_OBJ)
70 return 0;
71
72 /* it checks out - return it */
73 return sym;
74 }
75
76
77 /*
78 * Add a dictionary symbol loaded from an object file
79 */
80 void CTcParser::add_dict_from_obj_file(CTcSymObj *sym)
81 {
82 CTcDictEntry *entry;
83
84 /* get the current entry, if any */
85 entry = get_dict_entry(sym);
86
87 /* if there's no current entry, create a new one */
88 if (entry == 0)
89 {
90 /* create the entry */
91 entry = create_dict_entry(sym);
92 }
93
94 /* add the entry to the object file index list */
95 obj_dict_list_[obj_file_dict_idx_++] = entry;
96 }
97
98
99
100 /* ------------------------------------------------------------------------ */
101 /*
102 * Base symbol class
103 */
104
105 /*
106 * Write to a symbol file.
107 */
108 int CTcSymbolBase::write_to_sym_file(CVmFile *fp)
109 {
110 /* do the basic writing */
111 return write_to_file_gen(fp);
112 }
113
114 /*
115 * Write to a file. This is a generic base routine that can be used for
116 * writing to a symbol or object file.
117 */
118 int CTcSymbolBase::write_to_file_gen(CVmFile *fp)
119 {
120 /* write my type */
121 fp->write_uint2((int)get_type());
122
123 /* write my name */
124 return write_name_to_file(fp);
125 }
126
127 /*
128 * Write the symbol name to a file
129 */
130 int CTcSymbolBase::write_name_to_file(CVmFile *fp)
131 {
132 /* write the length of my symbol name, followed by the symbol name */
133 fp->write_uint2((int)get_sym_len());
134
135 /* write the symbol string */
136 fp->write_bytes(get_sym(), get_sym_len());
137
138 /* we wrote the symbol */
139 return TRUE;
140 }
141
142 /*
143 * Read a symbol from a symbol file
144 */
145 CTcSymbol *CTcSymbolBase::read_from_sym_file(CVmFile *fp)
146 {
147 /*
148 * read the type - this is the one thing we know is always present for
149 * every symbol (the rest of the data might vary per subclass)
150 */
151 tc_symtype_t typ = (tc_symtype_t)fp->read_uint2();
152
153 /* create the object based on the type */
154 switch(typ)
155 {
156 case TC_SYM_FUNC:
157 return CTcSymFunc::read_from_sym_file(fp);
158
159 case TC_SYM_OBJ:
160 return CTcSymObj::read_from_sym_file(fp);
161
162 case TC_SYM_PROP:
163 return CTcSymProp::read_from_sym_file(fp);
164
165 case TC_SYM_ENUM:
166 return CTcSymEnum::read_from_sym_file(fp);
167
168 case TC_SYM_METACLASS:
169 return CTcSymMetaclass::read_from_sym_file(fp);
170
171 default:
172 /* other types should not be in a symbol file */
173 G_tcmain->log_error(0, 0, TC_SEV_ERROR, TCERR_SYMEXP_INV_TYPE);
174 return 0;
175 }
176 }
177
178 /*
179 * Read the basic information from the symbol file
180 */
181 const char *CTcSymbolBase::base_read_from_sym_file(CVmFile *fp)
182 {
183 char buf[TOK_SYM_MAX_LEN + 1];
184
185 /* read, null-terminate, and return the string */
186 return CTcParser::read_len_prefix_str(fp, buf, sizeof(buf), 0,
187 TCERR_SYMEXP_SYM_TOO_LONG);
188 }
189
190 /*
191 * Write to an object file.
192 */
193 int CTcSymbolBase::write_to_obj_file(CVmFile *fp)
194 {
195 /* do the basic writing */
196 return write_to_file_gen(fp);
197 }
198
199
200 /* ------------------------------------------------------------------------ */
201 /*
202 * Function symbols
203 */
204
205
206 /*
207 * Write to a symbol file
208 */
209 int CTcSymFuncBase::write_to_sym_file(CVmFile *fp)
210 {
211 char buf[7];
212 CTcSymFunc *cur;
213 int ext_modify;
214
215 /* scan for the bottom of our modify stack */
216 for (cur = get_mod_base() ; cur != 0 && cur->get_mod_base() != 0 ;
217 cur = cur->get_mod_base()) ;
218
219 /* we modify an external if the bottom of our modify stack is external */
220 ext_modify = (cur != 0 && cur->is_extern());
221
222 /*
223 * If we're external, don't bother writing to the file - if we're
224 * importing a function, we don't want to export it as well. Note that
225 * a function that is replacing or modifying an external function is
226 * fundamentally external itself, because the function must be defined
227 * in another file to be replaceable/modifiable.
228 *
229 * As an exception, if this is a multi-method base symbol, and a
230 * multi-method with this name is defined in this file, export it even
231 * though it's technically an extern symbol. We don't export most
232 * extern symbols because we count on the definer to export them, but
233 * in the case of multi-method base symbols, there is no definer - the
234 * base symbol is basically a placeholder to be filled in by the
235 * linker. So *someone* has to export these. The logical place to
236 * export them is from any file that defines a multi-method based on
237 * the base symbol.
238 */
239 if ((is_extern_ || ext_replace_ || ext_modify) && !mm_def_)
240 return FALSE;
241
242 /* inherit default */
243 CTcSymbol::write_to_sym_file(fp);
244
245 /* write our argument count, varargs flag, and return value flag */
246 oswp2(buf, argc_);
247 oswp2(buf + 2, opt_argc_);
248 buf[4] = (varargs_ != 0);
249 buf[5] = (has_retval_ != 0);
250 buf[6] = (is_multimethod_ ? 1 : 0)
251 | (is_multimethod_base_ ? 2 : 0);
252 fp->write_bytes(buf, 7);
253
254 /* we wrote the symbol */
255 return TRUE;
256 }
257
258 /*
259 * Read from a symbol file
260 */
261 CTcSymbol *CTcSymFuncBase::read_from_sym_file(CVmFile *fp)
262 {
263 char symbuf[4096];
264 const char *sym;
265 char info[7];
266 int argc;
267 int opt_argc;
268 int varargs;
269 int has_retval;
270 int is_multimethod, is_multimethod_base;
271
272 /*
273 * Read the symbol name. Use a custom reader instead of the base
274 * reader, because function symbols can be quite long, due to
275 * multimethod name decoration.
276 */
277 if ((sym = CTcParser::read_len_prefix_str(
278 fp, symbuf, sizeof(symbuf), 0, TCERR_SYMEXP_SYM_TOO_LONG)) == 0)
279 return 0;
280
281 /*
282 * read the argument & optional argument count, varargs flag, and
283 * return value flag
284 */
285 fp->read_bytes(info, 7);
286 argc = osrp2(info);
287 opt_argc = osrp2(info + 2);
288 varargs = (info[4] != 0);
289 has_retval = (info[5] != 0);
290 is_multimethod = ((info[6] & 1) != 0);
291 is_multimethod_base = ((info[6] & 2) != 0);
292
293 /* create and return the new symbol */
294 return new CTcSymFunc(sym, strlen(sym), FALSE, argc, opt_argc,
295 varargs, has_retval,
296 is_multimethod, is_multimethod_base, TRUE, TRUE);
297 }
298
299 /*
300 * Write to an object file
301 */
302 int CTcSymFuncBase::write_to_obj_file(CVmFile *fp)
303 {
304 char buf[12];
305 CTcSymFunc *cur;
306 CTcSymFunc *last_mod;
307 int mod_body_cnt;
308 int ext_modify;
309
310 /*
311 * If it's external, and we have no fixups, don't bother writing it to
312 * the object file. If there are no fixups, we don't have any
313 * references to the function, hence there's no need to include it in
314 * the object file.
315 */
316 if (is_extern_ && fixups_ == 0)
317 return FALSE;
318
319 /*
320 * If we have a modified base function, scan down the chain of modified
321 * bases until we reach the last one. If it's external, we need to
322 * note this, and we need to store the fixup list for the external
323 * symbol so that we can explicitly link it to the imported symbol at
324 * link time.
325 */
326 for (mod_body_cnt = 0, last_mod = 0, cur = get_mod_base() ; cur != 0 ;
327 last_mod = cur, cur = cur->get_mod_base())
328 {
329 /* if this one has an associated code body, count it */
330 if (cur->get_code_body() != 0 && !cur->get_code_body()->is_replaced())
331 ++mod_body_cnt;
332 }
333
334 /* we modify an external if the last in the modify chain is external */
335 ext_modify = (last_mod != 0 && last_mod->is_extern());
336
337 /* inherit default */
338 CTcSymbol::write_to_obj_file(fp);
339
340 /*
341 * write our argument count, varargs flag, return value, extern flags,
342 * and the number of our modified base functions with code bodies
343 */
344 oswp2(buf, argc_);
345 oswp2(buf + 2, opt_argc_);
346 buf[4] = (varargs_ != 0);
347 buf[5] = (has_retval_ != 0);
348 buf[6] = (is_extern_ != 0);
349 buf[7] = (ext_replace_ != 0);
350 buf[8] = (ext_modify != 0);
351 buf[9] = (is_multimethod_ ? 1 : 0)
352 | (is_multimethod_base_ ? 2 : 0);
353 oswp2(buf + 10, mod_body_cnt);
354 fp->write_bytes(buf, 12);
355
356 /* if we modify an external, save its fixup list */
357 if (ext_modify)
358 CTcAbsFixup::write_fixup_list_to_object_file(fp, last_mod->fixups_);
359
360 /* write the code stream offsets of the modified base function bodies */
361 for (cur = get_mod_base() ; cur != 0 ; cur = cur->get_mod_base())
362 {
363 /* if this one has a code body, write its code stream offset */
364 if (cur->get_code_body() != 0)
365 fp->write_uint4(cur->get_anchor()->get_ofs());
366 }
367
368 /*
369 * If we're defined as external, write our fixup list. Since this
370 * is an external symbol, it will have no anchor in the code stream,
371 * hence we need to write our fixup list with the symbol and not
372 * with the anchor.
373 */
374 if (is_extern_)
375 CTcAbsFixup::write_fixup_list_to_object_file(fp, fixups_);
376
377 /* we wrote the symbol */
378 return TRUE;
379 }
380
381 /* ------------------------------------------------------------------------ */
382 /*
383 * Object symbols
384 */
385
386 /*
387 * Write to a symbol file
388 */
389 int CTcSymObjBase::write_to_sym_file(CVmFile *fp)
390 {
391 int result;
392
393 /*
394 * If we're external, don't bother writing to the file - if we're
395 * importing an object, we don't want to export it as well. If it's
396 * modified, don't write it either, because modified symbols cannot be
397 * referenced directly by name (the symbol for a modified object is a
398 * fake symbol anyway). In addition, don't write the symbol if it's a
399 * 'modify' or 'replace' definition that applies to an external base
400 * object - instead, we'll pick up the symbol from the other symbol
401 * file with the original definition.
402 */
403 if (is_extern_ || modified_ || ext_modify_ || ext_replace_)
404 return FALSE;
405
406 /* inherit default */
407 result = CTcSymbol::write_to_sym_file(fp);
408
409 /* if that was successful, write additional object-type-specific data */
410 if (result)
411 {
412 /* write the metaclass ID */
413 fp->write_uint2((int)metaclass_);
414
415 /* if it's of metaclass tads-object, write superclass information */
416 if (metaclass_ == TC_META_TADSOBJ)
417 {
418 char c;
419 size_t cnt;
420 CTPNSuperclass *sc;
421
422 /*
423 * set up our flags: indicate whether or not we're explicitly
424 * based on the root object class, and if we're a 'class'
425 * object
426 */
427 c = ((sc_is_root() ? 1 : 0)
428 | (is_class() ? 2 : 0)
429 | (is_transient() ? 4 : 0));
430 fp->write_bytes(&c, 1);
431
432 /* count the declared superclasses */
433 for (cnt = 0, sc = sc_name_head_ ; sc != 0 ;
434 sc = sc->nxt_, ++cnt) ;
435
436 /*
437 * write the number of declared superclasses followed by the
438 * names of the superclasses
439 */
440 fp->write_uint2(cnt);
441 for (sc = sc_name_head_ ; sc != 0 ; sc = sc->nxt_)
442 {
443 /* write the counted-length identifier */
444 fp->write_uint2(sc->get_sym_len());
445 fp->write_bytes(sc->get_sym_txt(), sc->get_sym_len());
446 }
447 }
448 }
449
450 /* return the result */
451 return result;
452 }
453
454 /*
455 * Read from a symbol file
456 */
457 CTcSymbol *CTcSymObjBase::read_from_sym_file(CVmFile *fp)
458 {
459 const char *txt;
460 tc_metaclass_t meta;
461 CTcSymObj *sym;
462 char c;
463 size_t cnt;
464 size_t i;
465
466 /* read the symbol name */
467 if ((txt = base_read_from_sym_file(fp)) == 0)
468 return 0;
469
470 /* read the metaclass ID */
471 meta = (tc_metaclass_t)fp->read_uint2();
472
473 /*
474 * If it's a dictionary object, check to see if it's already defined -
475 * a dictionary object can be exported from multiple modules without
476 * error, since dictionaries are shared across modules.
477 *
478 * The same applies to grammar productions, since a grammar production
479 * can be implicitly created in multiple files.
480 */
481 if (meta == TC_META_DICT || meta == TC_META_GRAMPROD)
482 {
483 /* look for a previous instance of the symbol */
484 CTcSymbol *old_sym =
485 G_prs->get_global_symtab()->find(txt, strlen(txt));
486 if (old_sym != 0
487 && old_sym->get_type() == TC_SYM_OBJ
488 && ((CTcSymObj *)old_sym)->get_metaclass() == meta)
489 {
490 /*
491 * the dictionary is already in the symbol table - return the
492 * existing one, since there's no conflict with importing the
493 * dictionary from multiple places
494 */
495 return old_sym;
496 }
497 }
498
499 /* create the new symbol */
500 sym = new CTcSymObj(txt, strlen(txt), FALSE, G_cg->new_obj_id(),
501 TRUE, meta, 0);
502
503 /* if the metaclass is tads-object, read additional information */
504 if (meta == TC_META_TADSOBJ)
505 {
506 /* read the root-object-superclass flag and the class-object flag */
507 fp->read_bytes(&c, 1);
508 sym->set_sc_is_root((c & 1) != 0);
509 sym->set_is_class((c & 2) != 0);
510 if ((c & 4) != 0)
511 sym->set_transient();
512
513 /* read the number of superclasses, and read the superclass names */
514 cnt = fp->read_uint2();
515 for (i = 0 ; i < cnt ; ++i)
516 {
517 char buf[TOK_SYM_MAX_LEN + 1];
518 const char *sc_txt;
519 size_t sc_len;
520
521 /* read the symbol */
522 sc_txt = CTcParser::read_len_prefix_str(
523 fp, buf, sizeof(buf), &sc_len, TCERR_SYMEXP_SYM_TOO_LONG);
524
525 /* add the superclass list entry to the symbol */
526 sym->add_sc_name_entry(sc_txt, sc_len);
527 }
528 }
529
530 /* return the symbol */
531 return sym;
532 }
533
534 /*
535 * Write to a object file
536 */
537 int CTcSymObjBase::write_to_obj_file(CVmFile *fp)
538 {
539 /*
540 * If the object is external and has never been referenced, don't
541 * bother writing it.
542 *
543 * In addition, if the object is marked as modified, don't write it.
544 * We write modified base objects specially, because we must control
545 * the order in which a modified base object is written relative its
546 * modifying object.
547 */
548 if ((is_extern_ && !ref_) || modified_)
549 return FALSE;
550
551 /* if the object has already been written, don't write it again */
552 if (written_to_obj_)
553 {
554 /*
555 * if we've never been counted in the object file before, we must
556 * have been written indirectly in the course of writing another
557 * symbol - in this case, return true to indicate that we are in
558 * the file, even though we're not actually writing anything now
559 */
560 if (!counted_in_obj_)
561 {
562 /* we've now been counted in the object file */
563 counted_in_obj_ = TRUE;
564
565 /* indicate that we have been written */
566 return TRUE;
567 }
568 else
569 {
570 /* we've already been written and counted - don't write again */
571 return FALSE;
572 }
573 }
574
575 /* do the main part of the writing */
576 return write_to_obj_file_main(fp);
577 }
578
579 /*
580 * Write the object symbol to an object file. This main routine does most
581 * of the actual work, once we've decided that we're actually going to
582 * write the symbol.
583 */
584 int CTcSymObjBase::write_to_obj_file_main(CVmFile *fp)
585 {
586 /* take the next object file index */
587 set_obj_file_idx(G_prs->get_next_obj_file_sym_idx());
588
589 /*
590 * if I have a dictionary object, make sure it's in the object file
591 * before I am - we need to be able to reference the object during
592 * load, so it has to be written before me
593 */
594 if (dict_ != 0)
595 dict_->write_sym_to_obj_file(fp);
596
597 /*
598 * if I'm not anonymous, write the basic header information for the
599 * symbol (don't do this for anonymous objects, since they don't have a
600 * name to write)
601 */
602 if (!anon_)
603 write_to_file_gen(fp);
604
605 /*
606 * write my object ID, so that we can translate from the local
607 * numbering system in the object file to the new numbering system in
608 * the image file
609 */
610 char buf[32];
611 oswp4(buf, obj_id_);
612
613 /* write the flags */
614 buf[4] = (is_extern_ != 0);
615 buf[5] = (ext_replace_ != 0);
616 buf[6] = (modified_ != 0);
617 buf[7] = (mod_base_sym_ != 0);
618 buf[8] = (ext_modify_ != 0);
619 buf[9] = (obj_stm_ != 0 && obj_stm_->is_class());
620 buf[10] = (transient_ != 0);
621
622 /* add the metaclass type */
623 oswp2(buf + 11, (int)metaclass_);
624
625 /* add the dictionary's object file index, if we have one */
626 if (dict_ != 0)
627 oswp2(buf + 13, dict_->get_obj_idx());
628 else
629 oswp2(buf + 13, 0);
630
631 /*
632 * add my object file index (we store this to eliminate any dependency
633 * on the load order - this allows us to write other symbols
634 * recursively without worrying about exactly where the recursion
635 * occurs relative to assigning the file index)
636 */
637 oswp2(buf + 15, get_obj_file_idx());
638
639 /* write the data to the file */
640 fp->write_bytes(buf, 17);
641
642 /* if we're not external, write our stream address */
643 if (!is_extern_)
644 fp->write_uint4(stream_ofs_);
645
646 /* if we're modifying another object, store some extra information */
647 if (mod_base_sym_ != 0)
648 {
649 /*
650 * Write our list of properties to be deleted from base objects at
651 * link time. First, count the properties in the list.
652 */
653 CTcObjPropDel *delprop;
654 int cnt;
655 for (cnt = 0, delprop = first_del_prop_ ; delprop != 0 ;
656 ++cnt, delprop = delprop->nxt_) ;
657
658 /* write the count */
659 fp->write_uint2(cnt);
660
661 /* write the deleted property list */
662 for (delprop = first_del_prop_ ; delprop != 0 ;
663 delprop = delprop->nxt_)
664 {
665 /*
666 * write out this property symbol (we write the symbol rather
667 * than the ID, because when we load the object file, we'll
668 * need to adjust the ID to new global numbering system in the
669 * image file; the easiest way to do this is to write the
670 * symbol and look it up at load time)
671 */
672 fp->write_uint2(delprop->prop_sym_->get_sym_len());
673 fp->write_bytes(delprop->prop_sym_->get_sym(),
674 delprop->prop_sym_->get_sym_len());
675 }
676 }
677
678 /* write our self-reference fixup list */
679 CTcIdFixup::write_to_object_file(fp, fixups_);
680
681 /*
682 * If this is a modifying object, we must write the entire chain of
683 * modified base objects immediately after this object. When we're
684 * reading the symbol table, this ensures that we can read each
685 * modified base object recursively as we read its modifiers, which is
686 * necessary so that we can build up the same modification chain on
687 * loading the object file.
688 */
689 if (mod_base_sym_ != 0)
690 {
691 /* write the main part of the definition */
692 mod_base_sym_->write_to_obj_file_main(fp);
693 }
694
695 /* mark the object as written to the file */
696 written_to_obj_ = TRUE;
697
698 /* written */
699 return TRUE;
700 }
701
702 /*
703 * Write a modified object to an object file
704 */
705 int CTcSymObjBase::write_to_obj_file_as_modified(class CVmFile *fp)
706 {
707 return write_to_obj_file_main(fp);
708 }
709
710
711 /*
712 * Write cross-references to the object file
713 */
714 int CTcSymObjBase::write_refs_to_obj_file(CVmFile *fp)
715 {
716 CTPNSuperclass *sc;
717 uint cnt;
718 long cnt_pos;
719 long end_pos;
720 CTcVocabEntry *voc;
721
722 /*
723 * if this symbol wasn't written to the object file in the first place,
724 * we obviously don't want to include any extra data for it
725 */
726 if (!written_to_obj_)
727 return FALSE;
728
729 /* write my symbol index */
730 fp->write_uint4(get_obj_file_idx());
731
732 /* write a placeholder superclass count */
733 cnt_pos = fp->get_pos();
734 fp->write_uint2(0);
735
736 /* write my superclass list */
737 for (sc = (obj_stm_ != 0 ? obj_stm_->get_first_sc() : 0), cnt = 0 ;
738 sc != 0 ; sc = sc->nxt_)
739 {
740 CTcSymObj *sym;
741
742 /* look up this superclass symbol */
743 sym = (CTcSymObj *)sc->get_sym();
744 if (sym != 0 && sym->get_type() == TC_SYM_OBJ)
745 {
746 /* write the superclass symbol index */
747 fp->write_uint4(sym->get_obj_file_idx());
748
749 /* count it */
750 ++cnt;
751 }
752 }
753
754 /* go back and write the superclass count */
755 end_pos = fp->get_pos();
756 fp->set_pos(cnt_pos);
757 fp->write_uint2(cnt);
758 fp->set_pos(end_pos);
759
760 /* count my vocabulary words */
761 for (cnt = 0, voc = vocab_ ; voc != 0 ; ++cnt, voc = voc->nxt_) ;
762
763 /* write my vocabulary words */
764 fp->write_uint2(cnt);
765 for (voc = vocab_ ; voc != 0 ; voc = voc->nxt_)
766 {
767 /* write the text of the word */
768 fp->write_uint2(voc->len_);
769 fp->write_bytes(voc->txt_, voc->len_);
770
771 /* write the property ID */
772 fp->write_uint2(voc->prop_);
773 }
774
775 /* indicate that we wrote the symbol */
776 return TRUE;
777 }
778
779 /*
780 * Load references from the object file
781 */
782 void CTcSymObjBase::load_refs_from_obj_file(CVmFile *fp, const char *,
783 tctarg_obj_id_t *,
784 tctarg_prop_id_t *prop_xlat)
785 {
786 uint i;
787 uint cnt;
788 CTcObjScEntry *sc_tail;
789
790 /* read the superclass count */
791 cnt = fp->read_uint2();
792
793 /* read the superclass list */
794 for (sc_tail = 0, i = 0 ; i < cnt ; ++i)
795 {
796 ulong idx;
797 CTcSymObj *sym;
798 CTcObjScEntry *sc;
799
800 /* read the next index */
801 idx = fp->read_uint4();
802
803 /* get the symbol */
804 sym = (CTcSymObj *)G_prs->get_objfile_sym(idx);
805 if (sym->get_type() != TC_SYM_OBJ)
806 sym = 0;
807
808 /* create a new list entry */
809 sc = new (G_prsmem) CTcObjScEntry(sym);
810
811 /* link it in at the end of the my superclass list */
812 if (sc_tail != 0)
813 sc_tail->nxt_ = sc;
814 else
815 sc_ = sc;
816
817 /* this is now the last entry in my superclass list */
818 sc_tail = sc;
819 }
820
821 /* load the vocabulary words */
822 cnt = fp->read_uint2();
823 for (i = 0 ; i < cnt ; ++i)
824 {
825 size_t len;
826 char *txt;
827 tctarg_prop_id_t prop;
828
829 /* read the length of this word's text */
830 len = fp->read_uint2();
831
832 /* allocate parser memory for the word's text */
833 txt = (char *)G_prsmem->alloc(len);
834
835 /* read the word into the allocated text buffer */
836 fp->read_bytes(txt, len);
837
838 /* read the property */
839 prop = (tctarg_prop_id_t)fp->read_uint2();
840
841 /* translate the property to the new numbering system */
842 prop = prop_xlat[prop];
843
844 /* add the word to our vocabulary */
845 add_vocab_word(txt, len, prop);
846 }
847 }
848
849 /* ------------------------------------------------------------------------ */
850 /*
851 * Property symbols
852 */
853
854 /*
855 * Write to a symbol file
856 */
857 int CTcSymPropBase::write_to_sym_file(CVmFile *fp)
858 {
859 /* inherit the default handling */
860 int ret = CTcSymbol::write_to_sym_file(fp);
861 if (ret)
862 {
863 /* encode and write our flags */
864 char flags = 0;
865 if (ref_)
866 flags |= 1;
867 if (vocab_)
868 flags |= 2;
869 if (weak_)
870 flags |= 4;
871
872 fp->write_byte(flags);
873 }
874
875 return ret;
876 }
877
878 /*
879 * Read from a symbol file
880 */
881 CTcSymbol *CTcSymPropBase::read_from_sym_file(CVmFile *fp)
882 {
883 /* read the symbol name */
884 const char *sym;
885 if ((sym = base_read_from_sym_file(fp)) == 0)
886 return 0;
887
888 /* read and decode the flags of interest */
889 char flags = fp->read_byte();
890 int weak = flags & 4;
891
892 /*
893 * If this property is already defined, this is a harmless redefinition
894 * - every symbol file can define the same property without any
895 * problem. Indicate the harmless redefinition by returning the
896 * original symbol.
897 */
898 CTcSymbol *old_entry = G_prs->get_global_symtab()->find(sym, strlen(sym));
899 if (old_entry != 0 && old_entry->get_type() == TC_SYM_PROP)
900 return old_entry;
901
902 /*
903 * if there's an old entry of a different type, and our entry is weak,
904 * the other symbol overrides our property definition
905 */
906 if (old_entry != 0 && weak)
907 return old_entry;
908
909 /* create and return the new symbol */
910 CTcSymProp *prop_entry =
911 new CTcSymProp(sym, strlen(sym), FALSE, G_cg->new_prop_id());
912
913 /* if our definition is weak, record that */
914 if (weak)
915 prop_entry->set_weak(TRUE);
916
917 /* return the new entry */
918 return prop_entry;
919 }
920
921 /*
922 * Write to an object file
923 */
924 int CTcSymPropBase::write_to_obj_file(CVmFile *fp)
925 {
926 /*
927 * If the property has never been referenced, don't bother writing it.
928 * We must have picked up the definition from an external symbol set we
929 * loaded but have no references of our own to the property.
930 */
931 if (!ref_)
932 return FALSE;
933
934 /* inherit default */
935 CTcSymbol::write_to_obj_file(fp);
936
937 /*
938 * write my local property ID value - when we load the object file,
939 * we'll need to figure out the translation from our original numbering
940 * system to the new numbering system used in the final image file
941 */
942 fp->write_uint4((ulong)prop_);
943
944 /* written */
945 return TRUE;
946 }
947
948
949 /* ------------------------------------------------------------------------ */
950 /*
951 * Enumerator symbol
952 */
953
954
955 /*
956 * Write to a symbol file
957 */
958 int CTcSymEnumBase::write_to_sym_file(CVmFile *fp)
959 {
960 int result;
961 char buf[32];
962
963 /* inherit default */
964 result = CTcSymbol::write_to_sym_file(fp);
965
966 /* write the 'token' flag */
967 if (result)
968 {
969 /* clear the flags */
970 buf[0] = 0;
971
972 /* set the 'token' flag if appropriate */
973 if (is_token_)
974 buf[0] |= 1;
975
976 /* write the flags */
977 fp->write_bytes(buf, 1);
978 }
979
980 /* return the result */
981 return result;
982 }
983
984 /*
985 * Read from a symbol file
986 */
987 CTcSymbol *CTcSymEnumBase::read_from_sym_file(CVmFile *fp)
988 {
989 const char *sym;
990 CTcSymEnum *old_entry;
991 char buf[32];
992 int is_token;
993
994 /* read the symbol name */
995 if ((sym = base_read_from_sym_file(fp)) == 0)
996 return 0;
997
998 /* read the 'token' flag */
999 fp->read_bytes(buf, 1);
1000 is_token = ((buf[0] & 1) != 0);
1001
1002 /*
1003 * If this enumerator is already defined, this is a harmless
1004 * redefinition - every symbol file can define the same enumerator
1005 * without any problem. Indicate the harmless redefinition by
1006 * returning the original symbol.
1007 */
1008 old_entry = (CTcSymEnum *)
1009 G_prs->get_global_symtab()->find(sym, strlen(sym));
1010 if (old_entry != 0 && old_entry->get_type() == TC_SYM_ENUM)
1011 {
1012 /* if this is a 'token' enum, mark the old entry as such */
1013 if (is_token)
1014 old_entry->set_is_token(TRUE);
1015
1016 /* return the original entry */
1017 return old_entry;
1018 }
1019
1020 /* create and return the new symbol */
1021 return new CTcSymEnum(sym, strlen(sym), FALSE,
1022 G_prs->new_enum_id(), is_token);
1023 }
1024
1025 /*
1026 * Write to an object file
1027 */
1028 int CTcSymEnumBase::write_to_obj_file(CVmFile *fp)
1029 {
1030 char buf[32];
1031
1032 /*
1033 * If the enumerator has never been referenced, don't bother writing
1034 * it. We must have picked up the definition from an external symbol
1035 * set we loaded but have no references of our own to the enumerator.
1036 */
1037 if (!ref_)
1038 return FALSE;
1039
1040 /* inherit default */
1041 CTcSymbol::write_to_obj_file(fp);
1042
1043 /*
1044 * write my local enumerator ID value - when we load the object file,
1045 * we'll need to figure out the translation from our original numbering
1046 * system to the new numbering system used in the image file
1047 */
1048 fp->write_uint4((ulong)enum_id_);
1049
1050 /* clear the flags */
1051 buf[0] = 0;
1052
1053 /* set the 'token' flag if appropriate */
1054 if (is_token_)
1055 buf[0] |= 1;
1056
1057 /* write the flags */
1058 fp->write_bytes(buf, 1);
1059
1060 /* written */
1061 return TRUE;
1062 }
1063
1064 /* ------------------------------------------------------------------------ */
1065 /*
1066 * Built-in function symbol base
1067 */
1068
1069 /*
1070 * Write to a object file
1071 */
1072 int CTcSymBifBase::write_to_obj_file(CVmFile *fp)
1073 {
1074 char buf[10];
1075
1076 /* inherit default */
1077 CTcSymbol::write_to_obj_file(fp);
1078
1079 /* write the varargs and return value flags */
1080 buf[0] = (varargs_ != 0);
1081 buf[1] = (has_retval_ != 0);
1082
1083 /* write the argument count information */
1084 oswp2(buf+2, min_argc_);
1085 oswp2(buf+4, max_argc_);
1086
1087 /*
1088 * write the function set ID and index - these are required to match
1089 * those used in all other object files that make up a single image
1090 * file
1091 */
1092 oswp2(buf+6, func_set_id_);
1093 oswp2(buf+8, func_idx_);
1094 fp->write_bytes(buf, 10);
1095
1096 /* written */
1097 return TRUE;
1098 }
1099
1100 /* ------------------------------------------------------------------------ */
1101 /*
1102 * Metaclass
1103 */
1104
1105 /*
1106 * write some additional data to the object file
1107 */
1108 int CTcSymMetaclassBase::write_to_obj_file(class CVmFile *fp)
1109 {
1110 CTcSymMetaProp *cur;
1111 char buf[16];
1112
1113 /* if this is an unreferenced external symbol, don't write it */
1114 if (ext_ && !ref_)
1115 return FALSE;
1116
1117 /* inherit default */
1118 CTcSymbol::write_to_obj_file(fp);
1119
1120 /* write my metaclass index, class object ID, and property count */
1121 fp->write_uint2(meta_idx_);
1122 fp->write_uint4(class_obj_);
1123 fp->write_uint2(prop_cnt_);
1124
1125 /* write my property symbol list */
1126 for (cur = prop_head_ ; cur != 0 ; cur = cur->nxt_)
1127 {
1128 /* write this symbol name */
1129 fp->write_uint2(cur->prop_->get_sym_len());
1130 fp->write_bytes(cur->prop_->get_sym(), cur->prop_->get_sym_len());
1131
1132 /* set up the flags */
1133 buf[0] = 0;
1134 if (cur->is_static_)
1135 buf[0] |= 1;
1136
1137 /* write the flags */
1138 fp->write_bytes(buf, 1);
1139 }
1140
1141 /* write our modifying object flag */
1142 buf[0] = (mod_obj_ != 0);
1143 fp->write_bytes(buf, 1);
1144
1145 /* if we have a modifier object chain, write it out */
1146 if (mod_obj_ != 0)
1147 mod_obj_->write_to_obj_file_as_modified(fp);
1148
1149 /* written */
1150 return TRUE;
1151 }
1152
1153 /*
1154 * Write to a symbol file
1155 */
1156 int CTcSymMetaclassBase::write_to_sym_file(class CVmFile *fp)
1157 {
1158 /* inherit the base class handling to write the symbol */
1159 CTcSymbol::write_to_sym_file(fp);
1160
1161 /* add the metaclass ID string */
1162 const char *id = G_cg->get_meta_name(meta_idx_);
1163 size_t idlen = strlen(id);
1164 fp->write_byte((char)idlen);
1165 fp->write_bytes(id, idlen);
1166
1167 /* success */
1168 return TRUE;
1169 }
1170
1171 /*
1172 * Read from a symbol file
1173 */
1174 CTcSymbol *CTcSymMetaclassBase::read_from_sym_file(class CVmFile *fp)
1175 {
1176 /* read the symbol name */
1177 const char *txt;
1178 if ((txt = base_read_from_sym_file(fp)) == 0)
1179 return 0;
1180
1181 /* read the metaclass ID string */
1182 char id[256];
1183 size_t idlen = (uchar)fp->read_byte();
1184 fp->read_bytes(id, idlen);
1185
1186 /* if the symbol is already defined, return the existing copy */
1187 CTcSymbol *old_sym = G_prs->get_global_symtab()->find(txt, strlen(txt));
1188 if (old_sym != 0 && old_sym->get_type() == TC_SYM_METACLASS)
1189 return old_sym;
1190
1191 /* create a new external metaclass symbol */
1192 CTcSymMetaclass *sym = new CTcSymMetaclass(
1193 txt, strlen(txt), FALSE, 0, G_cg->new_obj_id());
1194
1195 /* mark it as external */
1196 sym->set_ext(TRUE);
1197
1198 /* add it to the metaclass table */
1199 int idx = G_cg->find_or_add_meta(id, idlen, sym);
1200
1201 /* set the index in the symbol */
1202 sym->set_meta_idx(idx);
1203
1204 /* return the new symbol */
1205 return sym;
1206 }