-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
152 lines (126 loc) · 4.1 KB
/
main.c
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
/*
* main.c
*/
#include <stdio.h>
#include "util.h"
#include "symbol.h"
#include "types.h"
#include "absyn.h"
#include "errormsg.h"
#include "temp.h" /* needed by translate.h */
#include "tree.h" /* needed by frame.h */
#include "assem.h"
#include "frame.h" /* needed by translate.h and printfrags prototype */
#include "translate.h"
#include "env.h"
#include "semant.h" /* function prototype for transProg */
#include "canon.h"
#include "prabsyn.h"
#include "printtree.h"
#include "escape.h"
#include "parse.h"
#include "codegen.h"
#include "regalloc.h"
//---------------------
#include "graph.h"
#include "flowgraph.h"
#include "liveness.h"
extern bool anyErrors;
//------debug-----------
void showInstrInfo(FILE *out, void *inst) {
AS_print(out, (AS_instr) inst, Temp_layerMap(F_tempMap(), Temp_name()));
}
void showTempInfo(FILE *out, void *t) {
fprintf(out, "%s\n", Temp_look(Temp_layerMap(F_tempMap(), Temp_name()), (Temp_temp) t));
}
//----------------------
/*Mark: Lab6: complete the function doProc
* 1. initialize the F_tempMap
* 2. initialize the register lists (for register allocation)
* 3. do register allocation
* 4. output (print) the assembly code of each function
* Uncommenting the following printf can help you debugging.*/
/* print the assembly language instructions to filename.s */
static void doProc(FILE *out, FILE *logg, F_frame frame, T_stm body) {
AS_proc proc;
T_stmList stmList;
AS_instrList iList;
struct C_block blo;
// fprintf(logg, "doProc for function %s:\n", S_name(F_name(frame)));
stmList = C_linearize(body); /* 8 */
blo = C_basicBlocks(stmList);
stmList = C_traceSchedule(blo);
// printStmList(logg, stmList);
// fprintf(logg,"-------====trace=====-----\n");
iList = F_codegen(frame, stmList); /* 9 */
iList = F_procEntryExit2(iList);
// AS_printInstrList(logg, iList, Temp_layerMap(F_tempMap(), Temp_name()));
struct RA_result ra = RA_regAlloc(frame, iList); /* 11 */
//Part of TA's implementation. Just for reference.
AS_rewrite(ra.il, Temp_layerMap(F_tempMap(), ra.coloring));
proc = F_procEntryExit3(frame, ra.il);
fprintf(out, "%s", proc->prolog);
AS_printInstrList(out, proc->body, ra.coloring); // Assem tree to machine language
fprintf(out, "%s", proc->epilog);
}
void doStr(FILE *out, Temp_label label, string str) {
fprintf(out, "\t.section .rodata\n");
fprintf(out, ".%s:\n", S_name(label));
int length = *(int *) str;
fprintf(out, "\t.int %d\n", length);
//it may contains zeros in the middle of string. To keep this work, we need to print all the charactors instead of using fprintf(str)
fprintf(out, "\t.string \"");
int i = 0;
str = str + 4;
char c;
for (; i < length; i++) {
c = str[i];
if (c == '\n') {
fprintf(out, "\\n");
} else if (c == '\t') {
fprintf(out, "\\t");
} else {
fprintf(out, "%c", c);
}
}
fprintf(out, "\"\n");
//fprintf(out, ".string \"%s\"\n", str);
}
int main(int argc, string *argv) {
A_exp absyn_root;
// S_table base_env, base_tenv;
F_fragList frags;
char outfile[100];
char logfile[100];
FILE *out = stdout;
FILE *logg = stdout;
if (argc == 2) {
absyn_root = parse(argv[1]);
if (!absyn_root)
return 1;
#if 0
pr_exp(out, absyn_root, 0); /* print absyn data structure */
fprintf(out, "\n");
#endif
//Lab 6: escape analysis
//If you have implemented escape analysis, uncomment this
Esc_findEscape(absyn_root); /* set varDec's escape field */
frags = SEM_transProg(absyn_root);
if (anyErrors) return 1; /* don't continue */
/* convert the filename */
sprintf(outfile, "%s.s", argv[1]);
sprintf(logfile, "%s.log", argv[1]);
out = fopen(outfile, "w");
logg = fopen(logfile, "w");
/* Chapter 8, 9, 10, 11 & 12 */
for (; frags; frags = frags->tail)
if (frags->head->kind == F_procFrag) {
doProc(out, logg, frags->head->u.proc.frame, frags->head->u.proc.body);
} else if (frags->head->kind == F_stringFrag)
doStr(out, frags->head->u.stringg.label, frags->head->u.stringg.str);
fclose(out);
fclose(logg);
}
EM_error(0, "usage: tiger file.tig");
return 1;
}