-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcscCode.cpp
More file actions
268 lines (227 loc) · 5.47 KB
/
cscCode.cpp
File metadata and controls
268 lines (227 loc) · 5.47 KB
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
/* ____________________________________________________________________________
Semantics and Coding Component Implementation for the Micro Compiler
mcode.cpp
Version 2007
James L. Richards
Last Update: August 28, 2007
Update by M. J. Wolf: January 21,2016
The routines in this unit are based on those provided in the book
"Crafting A Compiler" by Charles N. Fischer and Richard J. LeBlanc, Jr.,
Benjamin Cummings Publishing Co. (1991).
See Section 2.3-2.4, pp. 31-40.
____________________________________________________________________________
*/
#include <iostream>
#include <fstream>
using namespace std;
extern ifstream sourceFile;
extern ofstream outFile, listFile;
#include "cscScan.h" // Scanner class definition
#include "cscCode.h" // CodeGen class definition
extern Scanner scan; // global Scanner object declared in micro.cpp
// *******************
// ** Constructor **
// *******************
CodeGen::CodeGen()
{
maxTemp = 0;
}
// *******************************
// ** Private Member Functions **
// *******************************
void CodeGen::CheckId(const string & s)
{
if (!LookUp(s)) // variable not declared yet
Enter(s);
}
void CodeGen::Enter(const string & s)
{
symbolTable.push_back(s);
}
void CodeGen::ExtractExpr(const ExprRec & e, string& s)
{
string t;
int k, n;
switch (e.kind)
{
case ID_EXPR:
case TEMP_EXPR: // operand form: +k(R15)
s = e.name;
n = 0;
while (symbolTable[n] != s) n++;
k = 2 * n; // offset: 2 bytes per variable
IntToAlpha(k, t);
s = "+" + t + "(R15)";
break;
case LITERAL_EXPR:
IntToAlpha(e.val, t);
s = "#" + t;
}
}
string CodeGen::ExtractOp(const OpRec & o)
{
if (o.op == PLUS)
return "IA ";
else
return "IS ";
}
void CodeGen::Generate(const string & s1, const string & s2, const string & s3)
{
listFile.width(20);
listFile << ' ' << s1;
outFile << s1;
if (s2.length() > 0)
{
listFile << s2;
outFile << s2;
if (s3.length() > 0)
{
listFile << ',' << s3;
outFile << ',' << s3;
}
}
listFile << endl;
outFile << endl;
}
string CodeGen::GetTemp()
{
string s;
static string t;
t = "Temp&";
IntToAlpha(++maxTemp, s);
t += s;
CheckId(t);
return t;
}
void CodeGen::IntToAlpha(int val, string& str)
{
int k;
char temp;
str = "";
if (val == 0) str = "0";
while (val > 0)
{
str.append(1, (char)(val % 10 + (int)'0'));
val /= 10;
}
k = int(str.length());
for (int i = 0; i < k/2; i++)
{
temp = str[i];
str[i] = str[k-i-1];
str[k-i-1] = temp;
}
}
bool CodeGen::LookUp(const string & s)
{
for (unsigned i = 0; i < symbolTable.size(); i++)
if (symbolTable[i] == s)
return true;
return false;
}
// ******************************
// ** Public Member Functions **
// ******************************
void CodeGen::Assign(const ExprRec & target, const ExprRec & source)
{
string s;
ExtractExpr(source, s);
Generate("LD ", "R0", s);
ExtractExpr(target, s);
Generate("STO ", "R0", s);
}
void CodeGen::Finish()
{
string s;
listFile.width(6);
listFile << ++scan.lineNumber << " " << scan.lineBuffer << endl;
Generate("HALT ", "", "");
Generate("LABEL ", "VARS", "");
IntToAlpha(int(2*(symbolTable.size()+1)), s);
Generate("SKIP ", s, "");
outFile.close();
listFile << endl << endl;
listFile << " _____________________________________________\n";
listFile << " <><><><> S Y M B O L T A B L E <><><><>\n"
<< endl;
listFile << " Relative" << endl;
listFile << " Address Identifier" << endl;
listFile << " -------- --------------------------------"
<< endl;
for (unsigned i = 0; i < symbolTable.size(); i++)
{
listFile.width(7);
listFile << 2*i << " " << symbolTable[i] << endl;
}
listFile << " _____________________________________________"
<< endl;
listFile << endl;
listFile << " Normal successful compilation." << endl;
listFile.close();
}
void CodeGen::GenInfix(const ExprRec & e1, const OpRec & op,
const ExprRec & e2, ExprRec& e)
{
string opnd;
if (e1.kind == LITERAL_EXPR && e2.kind == LITERAL_EXPR)
{
e.kind = LITERAL_EXPR;
switch (op.op)
{
case PLUS:
e.val = e1.val + e2.val;
break;
case MINUS:
e.val = e1.val - e2.val;
}
}
else
{
e.kind = TEMP_EXPR;
e.name = GetTemp();
ExtractExpr(e1, opnd);
Generate("LD ", "R0", opnd);
ExtractExpr(e2, opnd);
Generate(ExtractOp(op), "R0", opnd);
ExtractExpr(e, opnd);
Generate("STO ", "R0", opnd);
}
}
void CodeGen::NewLine()
{
Generate("WRNL ", "", "");
}
void CodeGen::ProcessId(ExprRec& e)
{
CheckId(scan.tokenBuffer);
e.kind = ID_EXPR;
e.name = scan.tokenBuffer;
}
void CodeGen::ProcessLiteral(ExprRec& e)
{
e.kind = LITERAL_EXPR;
e.val = atoi(scan.tokenBuffer.data());
}
void CodeGen::ProcessOp(OpRec& o)
{
if (scan.tokenBuffer == "+")
o.op = PLUS;
else
o.op = MINUS;
}
void CodeGen::ReadId(const ExprRec & inVar)
{
string s;
ExtractExpr(inVar, s);
Generate("RDI ", s, "");
}
void CodeGen::Start()
{
Generate("LDA ", "R15", "VARS");
}
void CodeGen::WriteExpr(const ExprRec & outExpr)
{
string s;
ExtractExpr(outExpr, s);
Generate("WRI ", s, "");
}