-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtab-print.cc
198 lines (150 loc) · 6.21 KB
/
symtab-print.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
/*********************************************************************************************
cfglp : A CFG Language Processor
--------------------------------
About:
Implemented by Uday Khedker (http://www.cse.iitb.ac.in/~uday) for
the courses cs302+cs306: Language Processors (theory and lab) at
IIT Bombay. Release date Jan 6, 2013. Copyrights reserved by Uday
Khedker. This implemenation has been made available purely for
academic purposes without any warranty of any kind.
Please see the README file for some details. A doxygen generated
documentation can be found at http://www.cse.iitb.ac.in/~uday/cfglp
***********************************************************************************************/
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <list>
using namespace std;
#include "common-classes.hh"
#include "evaluate.hh"
#include "reg-alloc.hh"
#include "symtab.hh"
#include "ast.hh"
#include "support.hh"
#include "program.hh"
void sym_Entry_for_Int_Var::print_Sym_Entry_Details(ostream * sym_fp)
{
string k_mesg = "The entity type of name " + name + " should have been variable";
CHECK_INVARIANT (en_type == entity_Var, k_mesg)
const string en_type_name = "VAR";
if(var_t == real)
{
string t_mesg = "The type of entity_Var " + name + " should have been int_Val";
CHECK_INVARIANT (type == int_Val, t_mesg)
const string type_name = "INT";
*sym_fp << " Name: " << name << " Type: " << type_name << " Entity Type: " << en_type_name;
if (start_offset == end_offset)
*sym_fp << " (No offset assigned yet)\n";
else
*sym_fp << " Start Offset: " << start_offset << " End Offset: " << end_offset << "\n";
}
}
void sym_List::print_Sym_Node_List(ostream * sym_fp)
{
map<string, sym_Entry_Ptr>::iterator i;
for (i = sym_list.begin(); i != sym_list.end(); i++)
i->second->print_Sym_Entry_Details(sym_fp);
}
void sym_Entry_for_Proc::print_Sym_Entry_Details(ostream * sym_fp)
{
string mesg = "Wrong value of entity type field for a symbol entry representing a procedure " + name;
CHECK_INVARIANT (en_type == entity_Proc, mesg)
const string en_type_name = "FUNC";
const string type_name = (type == int_Val)? "INT" : "VOID";
*sym_fp << " Name: " << name << " Type: " << type_name << " Entity Type: " << en_type_name << "\n";
}
void sym_List::print_Sym_Node_List_for_Evaluation(ostream * sym_fp)
{
map<string, sym_Entry_Ptr>::iterator i;
for (i = sym_list.begin(); i != sym_list.end(); i++)
{
/* Check if the sym list entry is a name and then print it */
sym_Entry_Ptr se_P = i->second;
if (se_P->get_Entity_Type() == entity_Var)
se_P->print_Sym_Entry_Eval_Details(sym_fp);
}
}
void sym_Entry_for_Int_Var::print_Sym_Entry_Eval_Details(ostream * sym_fp)
{
string k_mesg = "The entity type of name " + name + " should have been variable";
CHECK_INVARIANT (en_type == entity_Var, k_mesg)
const string en_type_name = "VAR";
string t_mesg = "The type of entity_Var " + name + " should have been int_Val";
CHECK_INVARIANT (type == int_Val, t_mesg)
const string type_name = "INT";
stringstream ss;
if (undefined)
ss << "Undefined";
else
ss << value;
if(var_t == real) {
*sym_fp << " Name: " << name << " Value " << ss.str() << "\n";
}
}
void sym_Entry_for_Float_Var::print_Sym_Entry_Eval_Details(ostream * sym_fp)
{
string k_mesg = "The entity type of name " + name + " should have been variable";
CHECK_INVARIANT (en_type == entity_Var, k_mesg)
const string en_type_name = "VAR";
string t_mesg = "The type of entity_Var " + name + " should have been int_Val";
CHECK_INVARIANT (type == float_Val, t_mesg)
const string type_name = "FLOAT";
stringstream ss;
if (undefined)
ss << "Undefined";
else
ss << value;
if(var_t == real) {
*sym_fp << " Name: " << name << " Value " << ss.str() << "\n";
}
}
/**************************** functions for generating assembly ******************/
void sym_List::print_Sym_for_Assembly(ostream * sym_fp)
{
map<string, sym_Entry_Ptr>::iterator i;
for (i = sym_list.begin(); i != sym_list.end(); i++)
{
/* Check if the sym list entry is a name and then print it */
sym_Entry_Ptr se_P = i->second;
if (se_P->get_Entity_Type() == entity_Var)
se_P->print_Sym_for_Assembly(sym_fp);
}
}
void sym_Entry_for_Int_Var::print_Sym_for_Assembly(ostream * sym_fp)
{
string k_mesg = "The entity type of name " + name + " should have been variable";
CHECK_INVARIANT (en_type == entity_Var, k_mesg)
const string en_type_name = "VAR";
string t_mesg = "The type of entity_Var " + name + " should have been int_Val";
CHECK_INVARIANT (type == int_Val, t_mesg)
const string type_name = "INT";
CHECK_INVARIANT(sym_scope == is_Global, "Global scope value expected")
*sym_fp << name << ":\t.word 0\n";
}
void sym_Entry::print_Sym_for_Assembly(ostream * sym_fp)
{
CHECK_INVARIANT (SHOULD_NOT_REACH, "Symbol does not qualify as global data in assembly output")
}
/*
------------------------------
new implementation
*/
void sym_Entry_for_Float_Var::print_Sym_Entry_Details(ostream * sym_fp)
{
//print nothing for arti_Var, exp_Var
string k_mesg = "The entity type of name " + name + " should have been variable";
CHECK_INVARIANT (en_type == entity_Var, k_mesg)
const string en_type_name = "VAR";
if(var_t == real)
{
string t_mesg = "The type of entity_Var " + name + " should have been int_Val";
CHECK_INVARIANT (type == float_Val, t_mesg)
const string type_name = "FLOAT";
*sym_fp << " Name: " << name << " Type: " << type_name << " Entity Type: " << en_type_name;
if (start_offset == end_offset)
*sym_fp << " (No offset assigned yet)\n";
else
*sym_fp << " Start Offset: " << start_offset << " End Offset: " << end_offset << "\n";
}
}