This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cgen.cc
1856 lines (1579 loc) · 51.9 KB
/
cgen.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//**************************************************************
//
// Code generator SKELETON
//
// Read the comments carefully. Make sure to
// initialize the base class tags in
// `CgenClassTable::CgenClassTable'
//
// Add the label for the dispatch tables to
// `IntEntry::code_def'
// `StringEntry::code_def'
// `BoolConst::code_def'
//
// Add code to emit everyting else that is needed
// in `CgenClassTable::code'
//
//
// The files as provided will produce code to begin the code
// segments, declare globals, and emit constants. You must
// fill in the rest.
//
//**************************************************************
#include "cgen.h"
#include "cgen_gc.h"
extern void emit_string_constant(ostream& str, char *s);
extern int cgen_debug;
//
// Three symbols from the semantic analyzer (semant.cc) are used.
// If e : No_type, then no code is generated for e.
// Special code is generated for new SELF_TYPE.
// The name "self" also generates code different from other references.
//
//////////////////////////////////////////////////////////////////////
//
// Symbols
//
// For convenience, a large number of symbols are predefined here.
// These symbols include the primitive type and method names, as well
// as fixed names used by the runtime system.
//
//////////////////////////////////////////////////////////////////////
Symbol
arg,
arg2,
Bool,
concat,
cool_abort,
copy,
Int,
in_int,
in_string,
IO,
length,
Main,
main_meth,
No_class,
No_type,
Object,
out_int,
out_string,
prim_slot,
self,
SELF_TYPE,
Str,
str_field,
substr,
type_name,
val;
//
// Initializing the predefined symbols.
//
static void initialize_constants(void)
{
arg = idtable.add_string("arg");
arg2 = idtable.add_string("arg2");
Bool = idtable.add_string("Bool");
concat = idtable.add_string("concat");
cool_abort = idtable.add_string("abort");
copy = idtable.add_string("copy");
Int = idtable.add_string("Int");
in_int = idtable.add_string("in_int");
in_string = idtable.add_string("in_string");
IO = idtable.add_string("IO");
length = idtable.add_string("length");
Main = idtable.add_string("Main");
main_meth = idtable.add_string("main");
// _no_class is a symbol that can't be the name of any
// user-defined class.
No_class = idtable.add_string("_no_class");
No_type = idtable.add_string("_no_type");
Object = idtable.add_string("Object");
out_int = idtable.add_string("out_int");
out_string = idtable.add_string("out_string");
prim_slot = idtable.add_string("_prim_slot");
self = idtable.add_string("self");
SELF_TYPE = idtable.add_string("SELF_TYPE");
Str = idtable.add_string("String");
str_field = idtable.add_string("_str_field");
substr = idtable.add_string("substr");
type_name = idtable.add_string("type_name");
val = idtable.add_string("_val");
}
static char *gc_init_names[] =
{ "_NoGC_Init", "_GenGC_Init", "_ScnGC_Init" };
static char *gc_collect_names[] =
{ "_NoGC_Collect", "_GenGC_Collect", "_ScnGC_Collect" };
// BoolConst is a class that implements code generation for operations
// on the two booleans, which are given global names here.
BoolConst falsebool(FALSE);
BoolConst truebool(TRUE);
//*********************************************************
//
// Define method for code generation
//
// This is the method called by the compiler driver
// `cgtest.cc'. cgen takes an `ostream' to which the assembly will be
// emmitted, and it passes this and the class list of the
// code generator tree to the constructor for `CgenClassTable'.
// That constructor performs all of the work of the code
// generator.
//
//*********************************************************
void program_class::cgen(ostream &os)
{
// spim wants comments to start with '#'
os << "# start of generated code\n";
initialize_constants();
CgenClassTable *codegen_classtable = new CgenClassTable(classes,os);
os << "\n# end of generated code\n";
}
//////////////////////////////////////////////////////////////////////////////
//
// emit_* procedures
//
// emit_X writes code for operation "X" to the output stream.
// There is an emit_X for each opcode X, as well as emit_ functions
// for generating names according to the naming conventions (see emit.h)
// and calls to support functions defined in the trap handler.
//
// Register names and addresses are passed as strings. See `emit.h'
// for symbolic names you can use to refer to the strings.
//
//////////////////////////////////////////////////////////////////////////////
static void emit_load(char *dest_reg, int offset, char *source_reg, ostream& s)
{
s << LW << dest_reg << " " << offset * WORD_SIZE << "(" << source_reg << ")"
<< endl;
}
static void emit_store(char *source_reg, int offset, char *dest_reg, ostream& s)
{
s << SW << source_reg << " " << offset * WORD_SIZE << "(" << dest_reg << ")"
<< endl;
}
static void emit_load_imm(char *dest_reg, int val, ostream& s)
{ s << LI << dest_reg << " " << val << endl; }
static void emit_load_address(char *dest_reg, char *address, ostream& s)
{ s << LA << dest_reg << " " << address << endl; }
static void emit_partial_load_address(char *dest_reg, ostream& s)
{ s << LA << dest_reg << " "; }
static void emit_load_bool(char *dest, const BoolConst& b, ostream& s)
{
emit_partial_load_address(dest,s);
b.code_ref(s);
s << endl;
}
static void emit_load_string(char *dest, StringEntry *str, ostream& s)
{
emit_partial_load_address(dest,s);
str->code_ref(s);
s << endl;
}
static void emit_load_int(char *dest, IntEntry *i, ostream& s)
{
emit_partial_load_address(dest,s);
i->code_ref(s);
s << endl;
}
static void emit_move(char *dest_reg, char *source_reg, ostream& s)
{ s << MOVE << dest_reg << " " << source_reg << endl; }
static void emit_neg(char *dest, char *src1, ostream& s)
{ s << NEG << dest << " " << src1 << endl; }
static void emit_comp(char *dest, char *src, ostream& s)
{ s << NOR << dest << " " << src << " " << ZERO << endl; }
static void emit_add(char *dest, char *src1, char *src2, ostream& s)
{ s << ADD << dest << " " << src1 << " " << src2 << endl; }
static void emit_addu(char *dest, char *src1, char *src2, ostream& s)
{ s << ADDU << dest << " " << src1 << " " << src2 << endl; }
static void emit_addiu(char *dest, char *src1, int imm, ostream& s)
{ s << ADDIU << dest << " " << src1 << " " << imm << endl; }
static void emit_div(char *dest, char *src1, char *src2, ostream& s)
{ s << DIV << dest << " " << src1 << " " << src2 << endl; }
static void emit_mul(char *dest, char *src1, char *src2, ostream& s)
{ s << MUL << dest << " " << src1 << " " << src2 << endl; }
static void emit_sub(char *dest, char *src1, char *src2, ostream& s)
{ s << SUB << dest << " " << src1 << " " << src2 << endl; }
static void emit_sll(char *dest, char *src1, int num, ostream& s)
{ s << SLL << dest << " " << src1 << " " << num << endl; }
static void emit_jalr(char *dest, ostream& s)
{ s << JALR << "\t" << dest << endl; }
static void emit_jal(char *address,ostream &s)
{ s << JAL << address << endl; }
static void emit_return(ostream& s)
{ s << RET << endl; }
static void emit_dispatch_abort(ostream& s)
{ s << JAL << DISPATCH_ABORT << endl; }
static void emit_equality_test(ostream& s)
{ s << JAL << EQUALITY_TEST << endl; }
static void emit_case_abort(ostream& s)
{ s << JAL << _CASE_ABORT << endl; }
static void emit_case_abort2(ostream& s)
{ s << JAL << _CASE_ABORT2 << endl; }
static void emit_gc_assign(ostream& s)
{ s << JAL << "_GenGC_Assign" << endl; }
static void emit_disptable_ref(Symbol sym, ostream& s)
{ s << sym << DISPTAB_SUFFIX; }
static void emit_init_ref(Symbol sym, ostream& s)
{ s << sym << CLASSINIT_SUFFIX; }
static void emit_label_ref(int l, ostream &s)
{ s << "label" << l; }
static void emit_protobj_ref(Symbol sym, ostream& s)
{ s << sym << PROTOBJ_SUFFIX; }
static void emit_method_ref(Symbol classname, Symbol methodname, ostream& s)
{ s << classname << METHOD_SEP << methodname; }
static void emit_label_def(int l, ostream &s)
{
emit_label_ref(l,s);
s << ":" << endl;
}
static void emit_beqz(char *source, int label, ostream &s)
{
s << BEQZ << source << " ";
emit_label_ref(label,s);
s << endl;
}
static void emit_beq(char *src1, char *src2, int label, ostream &s)
{
s << BEQ << src1 << " " << src2 << " ";
emit_label_ref(label,s);
s << endl;
}
static void emit_bne(char *src1, char *src2, int label, ostream &s)
{
s << BNE << src1 << " " << src2 << " ";
emit_label_ref(label,s);
s << endl;
}
static void emit_bleq(char *src1, char *src2, int label, ostream &s)
{
s << BLEQ << src1 << " " << src2 << " ";
emit_label_ref(label,s);
s << endl;
}
static void emit_blt(char *src1, char *src2, int label, ostream &s)
{
s << BLT << src1 << " " << src2 << " ";
emit_label_ref(label,s);
s << endl;
}
static void emit_blti(char *src1, int imm, int label, ostream &s)
{
s << BLT << src1 << " " << imm << " ";
emit_label_ref(label,s);
s << endl;
}
static void emit_bgti(char *src1, int imm, int label, ostream &s)
{
s << BGT << src1 << " " << imm << " ";
emit_label_ref(label,s);
s << endl;
}
static void emit_branch(int l, ostream& s)
{
s << BRANCH;
emit_label_ref(l,s);
s << endl;
}
//
// Push a register on the stack. The stack grows towards smaller addresses.
//
static void emit_push(char *reg, ostream& str)
{
emit_store(reg,0,SP,str);
emit_addiu(SP,SP,-4,str);
}
//
// Fetch the integer value in an Int object.
// Emits code to fetch the integer value of the Integer object pointed
// to by register source into the register dest
//
static void emit_fetch_int(char *dest, char *source, ostream& s)
{ emit_load(dest, DEFAULT_OBJFIELDS, source, s); }
//
// Emits code to store the integer value contained in register source
// into the Integer object pointed to by dest.
//
static void emit_store_int(char *source, char *dest, ostream& s)
{ emit_store(source, DEFAULT_OBJFIELDS, dest, s); }
static void emit_test_collector(ostream &s)
{
emit_push(ACC, s);
emit_move(ACC, SP, s); // stack end
emit_move(A1, ZERO, s); // allocate nothing
s << JAL << gc_collect_names[cgen_Memmgr] << endl;
emit_addiu(SP,SP,4,s);
emit_load(ACC,0,SP,s);
}
static void emit_gc_check(char *source, ostream &s)
{
if (source != (char*)A1) emit_move(A1, source, s);
s << JAL << "_gc_check" << endl;
}
///////////////////////////////////////////////////////////////////////////////
//
// coding strings, ints, and booleans
//
// Cool has three kinds of constants: strings, ints, and booleans.
// This section defines code generation for each type.
//
// All string constants are listed in the global "stringtable" and have
// type StringEntry. StringEntry methods are defined both for String
// constant definitions and references.
//
// All integer constants are listed in the global "inttable" and have
// type IntEntry. IntEntry methods are defined for Int
// constant definitions and references.
//
// Since there are only two Bool values, there is no need for a table.
// The two booleans are represented by instances of the class BoolConst,
// which defines the definition and reference methods for Bools.
//
///////////////////////////////////////////////////////////////////////////////
//
// Strings
//
void StringEntry::code_ref(ostream& s)
{
s << STRCONST_PREFIX << index;
}
//
// Emit code for a constant String.
// You should fill in the code naming the dispatch table.
//
void StringEntry::code_def(ostream& s, int stringclasstag)
{
IntEntryP lensym = inttable.add_int(len);
// Add -1 eye catcher
s << WORD << "-1" << endl;
code_ref(s); s << LABEL // label
<< WORD << stringclasstag << endl // tag
<< WORD << (DEFAULT_OBJFIELDS + STRING_SLOTS + (len+4)/4) << endl // size
<< WORD;
/***** Add dispatch information for class String ******/
emit_disptable_ref(Str, s);
s << endl; // dispatch table
s << WORD; lensym->code_ref(s); s << endl; // string length
emit_string_constant(s,str); // ascii string
s << ALIGN; // align to word
}
//
// StrTable::code_string
// Generate a string object definition for every string constant in the
// stringtable.
//
void StrTable::code_string_table(ostream& s, int stringclasstag)
{
for (List<StringEntry> *l = tbl; l; l = l->tl())
l->hd()->code_def(s,stringclasstag);
}
//
// Ints
//
void IntEntry::code_ref(ostream &s)
{
s << INTCONST_PREFIX << index;
}
//
// Emit code for a constant Integer.
// You should fill in the code naming the dispatch table.
//
void IntEntry::code_def(ostream &s, int intclasstag)
{
// Add -1 eye catcher
s << WORD << "-1" << endl;
code_ref(s); s << LABEL // label
<< WORD << intclasstag << endl // class tag
<< WORD << (DEFAULT_OBJFIELDS + INT_SLOTS) << endl // object size
<< WORD;
/***** Add dispatch information for class Int ******/
emit_disptable_ref(Int, s);
s << endl; // dispatch table
s << WORD << str << endl; // integer value
}
//
// IntTable::code_string_table
// Generate an Int object definition for every Int constant in the
// inttable.
//
void IntTable::code_string_table(ostream &s, int intclasstag)
{
for (List<IntEntry> *l = tbl; l; l = l->tl())
l->hd()->code_def(s,intclasstag);
}
//
// Bools
//
BoolConst::BoolConst(int i) : val(i) { assert(i == 0 || i == 1); }
void BoolConst::code_ref(ostream& s) const
{
s << BOOLCONST_PREFIX << val;
}
//
// Emit code for a constant Bool.
// You should fill in the code naming the dispatch table.
//
void BoolConst::code_def(ostream& s, int boolclasstag)
{
// Add -1 eye catcher
s << WORD << "-1" << endl;
code_ref(s); s << LABEL // label
<< WORD << boolclasstag << endl // class tag
<< WORD << (DEFAULT_OBJFIELDS + BOOL_SLOTS) << endl // object size
<< WORD;
/***** Add dispatch information for class Bool ******/
emit_disptable_ref(Bool, s);
s << endl; // dispatch table
s << WORD << val << endl; // value (0 or 1)
}
//////////////////////////////////////////////////////////////////////////////
//
// CgenClassTable methods
//
//////////////////////////////////////////////////////////////////////////////
//***************************************************
//
// Emit code to start the .data segment and to
// declare the global names.
//
//***************************************************
void CgenClassTable::code_global_data()
{
Symbol main = idtable.lookup_string(MAINNAME);
Symbol string = idtable.lookup_string(STRINGNAME);
Symbol integer = idtable.lookup_string(INTNAME);
Symbol boolc = idtable.lookup_string(BOOLNAME);
str << "\t.data\n" << ALIGN;
//
// The following global names must be defined first.
//
str << GLOBAL << CLASSNAMETAB << endl;
str << GLOBAL; emit_protobj_ref(main,str); str << endl;
str << GLOBAL; emit_protobj_ref(integer,str); str << endl;
str << GLOBAL; emit_protobj_ref(string,str); str << endl;
str << GLOBAL; falsebool.code_ref(str); str << endl;
str << GLOBAL; truebool.code_ref(str); str << endl;
str << GLOBAL << INTTAG << endl;
str << GLOBAL << BOOLTAG << endl;
str << GLOBAL << STRINGTAG << endl;
//
// We also need to know the tag of the Int, String, and Bool classes
// during code generation.
//
str << INTTAG << LABEL
<< WORD << intclasstag << endl;
str << BOOLTAG << LABEL
<< WORD << boolclasstag << endl;
str << STRINGTAG << LABEL
<< WORD << stringclasstag << endl;
}
//***************************************************
//
// Emit code to start the .text segment and to
// declare the global names.
//
//***************************************************
void CgenClassTable::code_global_text()
{
str << GLOBAL << HEAP_START << endl
<< HEAP_START << LABEL
<< WORD << 0 << endl
<< "\t.text" << endl
<< GLOBAL;
emit_init_ref(idtable.add_string("Main"), str);
str << endl << GLOBAL;
emit_init_ref(idtable.add_string("Int"),str);
str << endl << GLOBAL;
emit_init_ref(idtable.add_string("String"),str);
str << endl << GLOBAL;
emit_init_ref(idtable.add_string("Bool"),str);
str << endl << GLOBAL;
emit_method_ref(idtable.add_string("Main"), idtable.add_string("main"), str);
str << endl;
str << GLOBAL << DISPATCH_ABORT << endl;
str << GLOBAL << _CASE_ABORT << endl;
str << GLOBAL << _CASE_ABORT2 << endl;
}
void CgenClassTable::code_bools(int boolclasstag)
{
falsebool.code_def(str,boolclasstag);
truebool.code_def(str,boolclasstag);
}
void CgenClassTable::code_select_gc()
{
//
// Generate GC choice constants (pointers to GC functions)
//
str << GLOBAL << "_MemMgr_INITIALIZER" << endl;
str << "_MemMgr_INITIALIZER:" << endl;
str << WORD << gc_init_names[cgen_Memmgr] << endl;
str << GLOBAL << "_MemMgr_COLLECTOR" << endl;
str << "_MemMgr_COLLECTOR:" << endl;
str << WORD << gc_collect_names[cgen_Memmgr] << endl;
str << GLOBAL << "_MemMgr_TEST" << endl;
str << "_MemMgr_TEST:" << endl;
str << WORD << (cgen_Memmgr_Test == GC_TEST) << endl;
}
//********************************************************
//
// Emit code to reserve space for and initialize all of
// the constants. Class names should have been added to
// the string table (in the supplied code, is is done
// during the construction of the inheritance graph), and
// code for emitting string constants as a side effect adds
// the string's length to the integer table. The constants
// are emmitted by running through the stringtable and inttable
// and producing code for each entry.
//
//********************************************************
void CgenClassTable::code_constants()
{
//
// Add constants that are required by the code generator.
//
stringtable.add_string("");
inttable.add_string("0");
stringtable.code_string_table(str,stringclasstag);
inttable.code_string_table(str,intclasstag);
code_bools(boolclasstag);
}
CgenClassTable::CgenClassTable(Classes classes, ostream& s) :
nds(NULL), str(s), next(0), label(0)
{
stringclasstag = next_class_tag() /* Change to your String class tag here */;
intclasstag = next_class_tag() /* Change to your Int class tag here */;
boolclasstag = next_class_tag() /* Change to your Bool class tag here */;
enterscope();
if (cgen_debug) cout << "Building CgenClassTable" << endl;
install_basic_classes();
install_classes(classes);
build_inheritance_tree();
code();
exitscope();
}
void CgenClassTable::install_basic_classes()
{
// The tree package uses these globals to annotate the classes built below.
//curr_lineno = 0;
Symbol filename = stringtable.add_string("<basic class>");
//
// A few special class names are installed in the lookup table but not
// the class list. Thus, these classes exist, but are not part of the
// inheritance hierarchy.
// No_class serves as the parent of Object and the other special classes.
// SELF_TYPE is the self class; it cannot be redefined or inherited.
// prim_slot is a class known to the code generator.
//
addid(No_class,
new CgenNode(class_(No_class,No_class,nil_Features(),filename),
Basic,this));
addid(SELF_TYPE,
new CgenNode(class_(SELF_TYPE,No_class,nil_Features(),filename),
Basic,this));
addid(prim_slot,
new CgenNode(class_(prim_slot,No_class,nil_Features(),filename),
Basic,this));
//
// The Object class has no parent class. Its methods are
// cool_abort() : Object aborts the program
// type_name() : Str returns a string representation of class name
// copy() : SELF_TYPE returns a copy of the object
//
// There is no need for method bodies in the basic classes---these
// are already built in to the runtime system.
//
install_class(
new CgenNode(
class_(Object,
No_class,
append_Features(
append_Features(
single_Features(method(cool_abort, nil_Formals(), Object, no_expr())),
single_Features(method(type_name, nil_Formals(), Str, no_expr()))),
single_Features(method(copy, nil_Formals(), SELF_TYPE, no_expr()))),
filename),
Basic,this));
//
// The IO class inherits from Object. Its methods are
// out_string(Str) : SELF_TYPE writes a string to the output
// out_int(Int) : SELF_TYPE " an int " " "
// in_string() : Str reads a string from the input
// in_int() : Int " an int " " "
//
install_class(
new CgenNode(
class_(IO,
Object,
append_Features(
append_Features(
append_Features(
single_Features(method(out_string, single_Formals(formal(arg, Str)),
SELF_TYPE, no_expr())),
single_Features(method(out_int, single_Formals(formal(arg, Int)),
SELF_TYPE, no_expr()))),
single_Features(method(in_string, nil_Formals(), Str, no_expr()))),
single_Features(method(in_int, nil_Formals(), Int, no_expr()))),
filename),
Basic,this));
//
// The Int class has no methods and only a single attribute, the
// "val" for the integer.
//
install_class(
new CgenNode(
class_(Int,
Object,
single_Features(attr(val, prim_slot, no_expr())),
filename),
Basic,this,intclasstag));
//
// Bool also has only the "val" slot.
//
install_class(
new CgenNode(
class_(Bool, Object, single_Features(attr(val, prim_slot, no_expr())),filename),
Basic,this,boolclasstag));
//
// The class Str has a number of slots and operations:
// val ???
// str_field the string itself
// length() : Int length of the string
// concat(arg: Str) : Str string concatenation
// substr(arg: Int, arg2: Int): Str substring
//
install_class(
new CgenNode(
class_(Str,
Object,
append_Features(
append_Features(
append_Features(
append_Features(
single_Features(attr(val, Int, no_expr())),
single_Features(attr(str_field, prim_slot, no_expr()))),
single_Features(method(length, nil_Formals(), Int, no_expr()))),
single_Features(method(concat,
single_Formals(formal(arg, Str)),
Str,
no_expr()))),
single_Features(method(substr,
append_Formals(single_Formals(formal(arg, Int)),
single_Formals(formal(arg2, Int))),
Str,
no_expr()))),
filename),
Basic,this,stringclasstag));
}
// CgenClassTable::install_class
// CgenClassTable::install_classes
//
// install_classes enters a list of classes in the symbol table.
//
void CgenClassTable::install_class(CgenNodeP nd)
{
Symbol name = nd->get_name();
if (probe(name))
{
return;
}
// The class name is legal, so add it to the list of classes
// and the symbol table.
nds = new List<CgenNode>(nd,nds);
addid(name,nd);
}
void CgenClassTable::install_classes(Classes cs)
{
for(int i = cs->first(); cs->more(i); i = cs->next(i))
install_class(new CgenNode(cs->nth(i),NotBasic,this));
}
//
// CgenClassTable::build_inheritance_tree
//
void CgenClassTable::build_inheritance_tree()
{
for(List<CgenNode> *l = nds; l; l = l->tl())
set_relations(l->hd());
}
//
// CgenClassTable::set_relations
//
// Takes a CgenNode and locates its, and its parent's, inheritance nodes
// via the class table. Parent and child pointers are added as appropriate.
//
void CgenClassTable::set_relations(CgenNodeP nd)
{
CgenNode *parent_node = probe(nd->get_parent());
nd->set_parentnd(parent_node);
parent_node->add_child(nd);
}
void CgenNode::add_child(CgenNodeP n)
{
children = new List<CgenNode>(n,children);
}
void CgenNode::set_parentnd(CgenNodeP p)
{
assert(parentnd == NULL);
assert(p != NULL);
parentnd = p;
}
void CgenClassTable::code()
{
if (cgen_debug) cout << "coding global data" << endl;
code_global_data();
if (cgen_debug) cout << "choosing gc" << endl;
code_select_gc();
if (cgen_debug) cout << "coding constants" << endl;
code_constants();
// Add your code to emit
// - prototype objects
// - class_nameTab
// - dispatch tables
//
if (cgen_debug) cout << "analyze layout for each class" << endl;
analyze_layout();
if (cgen_debug) cout << "coding prototype objects" << endl;
code_prototype_objects();
if (cgen_debug) cout << "coding class_nameTab" << endl;
code_class_nameTab();
if (cgen_debug) cout << "coding class_objTab" << endl;
code_class_objTab();
if (cgen_debug) cout << "coding global text" << endl;
code_global_text();
// Add your code to emit
// - object initializer
// - the class methods
// - etc...
if (cgen_debug) cout << "coding object initializer" << endl;
code_objects_initializer();
if (cgen_debug) cout << "coding class methods" << endl;
code_class_methods();
}
void CgenClassTable::analyze_layout() {
analyze_layout(root());
}
void CgenClassTable::analyze_layout(CgenNodeP nd) {
DeclareP method;
for (int i = nd->features->first(); nd->features->more(i); i = nd->features->next(i)) {
Feature feat = nd->features->nth(i);
feat->layout(nd);
}
emit_disptable_ref(nd->get_name(), str); str << LABEL;
for (DeclareP method: nd->get_method_slot_to_declare()) {
str << WORD; method->code_ref(str); str << endl;
}
for (List<CgenNode> *l = nd->get_children(); l; l = l->tl()) {
CgenNodeP child = l->hd();
child->set_attr_slot_to_declare(nd->get_attr_slot_to_declare());
child->set_attr_table(nd->get_attr_table());
child->set_method_slot_to_declare(nd->get_method_slot_to_declare());
child->set_method_table(nd->get_method_table());
analyze_layout(l->hd());
}
}
void CgenClassTable::code_prototype_objects() {
code_prototype_objects(root());
}
void CgenClassTable::code_prototype_objects(CgenNodeP nd) {
Symbol type = nd->get_name();
if (type == Int || type == Bool || type == Str) {
nd->code_def(str);
} else {
nd->code_def(str, next_class_tag());
}
for (List<CgenNode> *l = nd->get_children(); l; l = l->tl()) {
code_prototype_objects(l->hd());
}
}
void CgenClassTable::code_class_nameTab() {
str << GLOBAL << CLASSNAMETAB << endl;
str << CLASSNAMETAB << LABEL;
code_class_nameTab(root());
}
void CgenClassTable::code_class_nameTab(CgenNodeP nd) {
str << WORD;
stringtable.lookup_string(nd->get_name()->get_string())->code_ref(str);
str << endl;
for (List<CgenNode> *l = nd->get_children(); l; l = l->tl()) {
code_class_nameTab(l->hd());
}
}
void CgenClassTable::code_class_objTab() {
str << GLOBAL << CLASSOBJTAB << endl;
str << CLASSOBJTAB << LABEL;
code_class_objTab(root());
}
void CgenClassTable::code_class_objTab(CgenNodeP nd) {
str << WORD; emit_protobj_ref(nd->get_name(), str); str << endl;
str << WORD; emit_init_ref(nd->get_name(), str); str << endl;
for (List<CgenNode> *l = nd->get_children(); l; l = l->tl()) {
code_class_objTab(l->hd());
}
}
void CgenClassTable::code_objects_initializer() {
code_objects_initializer(root());
}
void CgenClassTable::code_objects_initializer(CgenNodeP nd) {
nd->code_init(str);
for (List<CgenNode> *l = nd->get_children(); l; l = l->tl()) {
code_objects_initializer(l->hd());
}
}
void CgenClassTable::code_class_methods() {
code_class_methods(root());
}
void CgenClassTable::code_class_methods(CgenNodeP nd) {
nd->code_methods(str);
for (List<CgenNode> *l = nd->get_children(); l; l = l->tl()) {
code_class_methods(l->hd());
}
}
CgenNodeP CgenClassTable::root()
{
return probe(Object);
}
int CgenClassTable::next_class_tag() {
return next++;
}
int CgenClassTable::next_label() {
return label++;
}
///////////////////////////////////////////////////////////////////////
//
// CgenNode methods
//
///////////////////////////////////////////////////////////////////////
CgenNode::CgenNode(Class_ nd, Basicness bstatus, CgenClassTableP ct) :
class__class((const class__class &) *nd),
parentnd(NULL),
children(NULL),
basic_status(bstatus),
class_table(ct),
env(new Environment(class_table, this))