-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.cpp
192 lines (145 loc) · 4.35 KB
/
Function.cpp
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
#include <iostream>
using namespace std;
#include <sstream>
#include "Register.hh"
#include "symboltable.hh"
#include "codegeneration.hh"
#include "IntermediateCode.hh"
#include "ast.hh"
#include "Function.hh"
Function :: Function(DataType d, string s, int a){
returnType=d;
name=s;
lineno=a;
}
Function::Function(){
returnType=INT;
name="";
lineno=0;
}
Function :: ~Function(){
}
string Function :: getFunctionName(){
return name;
}
void Function :: setfunctionname(string s){
name=s;
}
void Function :: setdatatype(DataType d){
returnType=d;
}
void Function :: setLocalList(SymbolTable & s){
localSymbolTable=s;
}
SymbolTable Function :: getLocalList(){
return localSymbolTable;
}
void Function :: setAstList(list<Ast *> & l){
statementList=l;
}
DataType Function :: getReturnType(){
return returnType;
}
void Function :: setReturnType(DataType d){
returnType=d;
}
SymbolTableEntry & Function :: getSymbolTableEntry(string s){
return localSymbolTable.getSymbolTableEntry(s);
/* list<SymbolTableEntry*> varTable=localSymbolTable.varTable;
list<SymbolTableEntry*>::iterator it;
for(it=varTable.begin();it!=varTable.end();it++){
if((*it)->getVariableName()== s)
return *it;
}
*/
}
void Function :: print(ostream & f){
f<<"DATATYPE:"<<((returnType)==INT?"INT ":" ")<<" NAME: "<<name;
f<<"LINENO: "<<lineno<<endl;
f<<"Local Declarations"<<endl;
printSymbolTable(f);
/*
f<<"AST"<<endl;
for(Ast* i:statementList){
(i)->print(f);
}
*/
}
void Function :: printSymbolTable(ostream & f){
localSymbolTable.print(f);
}
list<Ast*> Function :: getAstlist(){
return statementList;
}
void Function :: generateIntermediateCode(){
// cout<<"begin ic_function.cpp"<<endl;
localSymbolTable.assignOffsetsToSymbols();
for(auto i = statementList.begin();i!=statementList.end();i++){
IntermediateCodeForAst icfa = (*i)->generateIntermediateCode();
// cout<<"__AST__"<<endl;
iCode.insert(iCode.begin(),icfa.getICode().begin(),icfa.getICode().end());
}
//cout<<"exit ic_function.cpp"<<endl;
}
void Function :: printIntermediateCode(ostream & o){
list<Quadruple* >::reverse_iterator i;
for (i = iCode.rbegin(); i != iCode.rend(); i++){
// cout<<"Print InterCode"<<endl;
(*i)->print(o);
}
}
/*
void Function :: generateTargetCode(){
cout<<"begin tg_function.cpp"<<endl;
localSymbolTable.assignOffsetsToSymbols();
//list<Instruction *> :: iterator it;
for(auto i = statementList.begin();i!=statementList.end();i++){
TargetCodeForAst tcfa = (*i)->generateTargetCode();
cout<<"--- Ast ---"<<endl;
instLlist.insert(instLlist.begin(),tcfa.getInstlist().begin(),tcfa.getInstlist().end());
}
cout<<"exit tg_function.cpp"<<endl;
}
*/
void Function::printPrologue(ostream & file_buffer)
{
stringstream prologue;
prologue << "\n\
.text \t\t\t# The .text assembler directive indicates\n\
.globl " << name << "\t\t# The following is the code (as oppose to data)\n";
prologue << name << ":\t\t\t\t# .globl makes main know to the \n\t\t\t\t# outside of the program\
# Prologue begins \n\
sw $ra, 0($sp)\t\t# Save the return address\n\
sw $fp, -4($sp)\t\t# Save the frame pointer\n\
sub $fp, $sp, 8\t\t# Update the frame pointer\n";
int size = localSymbolTable.size();
size = -size;
prologue << "\n\tsub $sp, $sp, " << (size + 8) << "\t\t# Make space for the locals\n";
prologue << "# Prologue ends\n\n";
file_buffer << prologue.str();
}
void Function::printEpilogue(ostream & file_buffer)
{
stringstream epilogue;
int size = localSymbolTable.size();
size = -size;
epilogue << "\n# Epilogue Begins\nepilogue_" << name << ":\n\tadd $sp, $sp, " << (size+8) <<endl;
epilogue << "\tlw $fp, -4($sp) \n\tlw $ra, 0($sp) \n\t";
epilogue << "jr $31\t\t# Jump back to the called procedure\n# Epilogue Ends\n\n";
file_buffer << epilogue.str();
}
/*
void Function::printTargetCode(ostream & file_buffer)
{
printPrologue(file_buffer);
// cout<<"Printed prologue"<<endl;
list<Instruction *>::reverse_iterator i;
for (i = instLlist.rbegin(); i != instLlist.rend(); i++){
// cout<<"Print Instruction"<<endl;
(*i)->print(file_buffer);
}
printEpilogue(file_buffer);
}
*/
//../tinyCC -symtab test.tc
//-tokens next cat filename