-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint.cpp
More file actions
executable file
·372 lines (363 loc) · 11.6 KB
/
Copy pathPrint.cpp
File metadata and controls
executable file
·372 lines (363 loc) · 11.6 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
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
/*
Print.cpp
This file defines the function used to print code in llvm format
*/
#include "Print.h"
#include "ANTNode.h"
#include "y.tab.h"
//#include <fstream>
//#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <list>
using namespace std;
string ReadFunc = "readFunc";
string WriteFunc = "writeFunc";
string InitFunc = "initFunc";
string retLabel = "Ret";
int regNum = 1;
int label = 0;
int errorCount = 0;
list<string>LabelList;
/************************************************************************/
/* label&temp */
/************************************************************************/
string getTempReg()
{
char buffer[15];
sprintf(buffer,".%d",++regNum);//For convenience, we use %.num as temp instead of %num
return "%"+string(buffer);
}
void resetRegAndLabel()
{
regNum = 0;
label = 0;
}
string *addLabel( string s )
{
char buffer[15];
sprintf(buffer,".%d",++label);
LabelList.push_back(s+buffer);
return &LabelList.back();
}
void printLabel( string *labelP )
{
llvmout << '\n' << *labelP << ":\n";
}
/************************************************************************/
/* Print Definition */
/************************************************************************/
void printGloIntDef(string addr )
{
llvmout << addr << " = global i32 0, align 4\n";
}
void printLocalIntDef(string addr)
{
llvmout << " " << addr << " = alloca i32, align 4\n";
}
void printStrVarDef(string addr, string type, bool glo )
{
if( glo == true )
llvmout << addr << " = global " << type << " zeroinitializer, align 4\n";
else
llvmout << " " << addr << " = alloca" << type << ", align 4\n";
}
void printIntArrayDef( string addr, vector<int> *lP, bool glo )
{
//cout << "## In printIntArrayDef()\n";
/* if( glo == true )
llvmout << addr << " = global [" << l
<< " x i32 ] zeroinitializer, align 4\n";
else
llvmout << " " << addr << " = alloca [" << l
<< " x i32 ], align 4\n";*/
if( glo == true ) llvmout << addr << " = global ";
else llvmout << addr << " = alloca ";
for( int j = 0; j < lP->size(); ++j )
llvmout << "[ " << lP->at(j) << " x ";
llvmout << "i32";
for( int j = 0; j < lP->size(); ++j )
llvmout << " ]";
if( glo == true ) llvmout << " zeroinitializer, align 4\n";
else llvmout << ", align 4\n";
}
void printStrArrayDef( string addr, vector<int> *lP, string type, bool glo )
{
/*if( glo == true )
llvmout << addr << " = global [" << l
<< " x " << type << " ] zeroinitializer, align 4\n";
else
llvmout << " " << addr << " = alloca [" << l
<< " x " << type << " ], align 4\n";*/
if( glo == true ) llvmout << addr << " = global ";
else llvmout << addr << " = alloca ";
for( int j = 0; j < lP->size(); ++j )
llvmout << "[ " << lP->at(j) << " x ";
llvmout << type;
for( int j = 0; j < lP->size(); ++j )
llvmout << " ]";
if( glo == true ) llvmout << " zeroinitializer, align 4\n";
else llvmout << ", align 4\n";
}
/************************************************************************/
/* load & store */
/************************************************************************/
void printLoad( string reg, string addr, string t)
{
llvmout << " " << reg << " = load " << t << "* " << addr
<< ", align 4\n";
}
void printStore( string addr, string reg, string t )
{
llvmout << " " << "store " << t << ' ' << reg
<< ", " << t << "* " << addr
<< ", align 4\n\n";
}
void printStore( string addr, int i, string t )
{
llvmout << " " << "store " << t << ' ' << i
<< ", " << t << "* " << addr
<< ", align 4\n";
}
/************************************************************************/
/* getelementptr */
/************************************************************************/
//$$ Need modify
void printGetElePtr_iA( string left, string addr, vector<int> *lP, int i )
{
llvmout << " " << left << " = getelementptr inbounds ";
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << "[ " << lP->at(j) << " x ";
}
llvmout << "i32";
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << " ]";
}
llvmout << "* " << addr << ",";
if( !lP->empty() && lP->front() != -1 )
llvmout << " i32 0,";
llvmout << " i32 " << i << '\n';
}
void printGetElePtr_iA( string left, string addr, vector<int> *lP, string i )
{
llvmout << " " << left << " = getelementptr inbounds ";
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << "[ " << lP->at(j) << " x ";
}
llvmout << "i32";
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << " ]";
}
llvmout << "* " << addr << ",";
if( !lP->empty() && lP->front() != -1 )
llvmout << " i32 0,";
llvmout << " i32 " << i << '\n';
}
void printGetElePtr_sA(string left, string addr, string type, vector<int> *lP, int i )
{
llvmout << " " << left << " = getelementptr inbounds ";
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << "[ " << lP->at(j) << " x ";
}
llvmout << type;
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << " ]";
}
llvmout << "* " << addr << ", i32 0, i32 " << i << '\n';
}
void printGetElePtr_sA(string left, string addr, string type, vector<int> *lP, string i )
{
llvmout << " " << left << " = getelementptr inbounds ";
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << "[ " << lP->at(j) << " x ";
}
llvmout << type;
for( int j = 0; j < lP->size(); ++j )
{
if( lP->at(j) == -1 ) continue;
llvmout << " ]";
}
llvmout << "* " << addr << ", i32 0, i32 " << i << '\n';
}
void printGetElePtr_st( string left, string addr, string type, int i )
{
llvmout << " " << left << " = getelementptr inbounds "
<< type << "* "
<< addr << ", i32 0, i32 " << i << '\n';
}
/************************************************************************/
/* bitcast&memcpy */
/************************************************************************/
void printBitCast(string dest, string source, string type)
{
llvmout << " " << dest << " = bitcast " << type << "* " << source << " to i8*\n";
}
void printMemCpy(string dest, string source, int l)
{
llvmout << "call void @llvm.memcpy.p0i8.p0i8.i32( i8* " << dest
<< ", i8* " << source << ", i32 " << l*4 << ", i32 4, i1 false)\n";
}
/************************************************************************/
/* operation */
/************************************************************************/
const char* getOp(int op)
{
switch(op)
{
case ADD: return "add nsw";
case SUB: return "sub nsw";
case MUL: return "mul nsw";
case DIV: return "sdiv";
case MOD: return "srem";
case SL: return "shl";
case SR: return "ashr";
case BAND: return "and";
case BXOR: return "xor";
case BOR: return "or";
default:
printErrorMessage(-1,"Operator","Undefined operator");
return "ERROR";
}
}
const char* getReOp( int re )
{
switch(re)
{
case EQ:return "eq";
case NE:return "ne";
case GT:return "sgt";
case GE:return "sge";
case LT:return "slt";
case LE:return "sle";
default:
printErrorMessage(-1,"Operator","Undefined comparasion condition");
return "ERROR";
}
}
void printOp(string dest, string sL, string sR, int op )
{
llvmout << " " << dest << " = " << getOp(op) << " i32 "
<< sL << ", " << sR << '\n';
}
void printOp(string dest, int i, string sR, int op )
{
llvmout << " " << dest << " = " << getOp(op) << " i32 "
<< i << ", " << sR << '\n';
}
void printOp(string dest, string sL, int i, int op )
{
llvmout << " " << dest << " = " << getOp(op) << " i32 "
<< sL << ", " << i << '\n';
}
/************************************************************************/
/* icmp&xor&zext */
/************************************************************************/
void printICmp( string dest, string sL, string sR, int cond )
{
llvmout << " " << dest << " = icmp " << getReOp(cond)
<< " i32 " << sL << ", " << sR << "\n";
}
void printICmp( string dest, string sL, int i, int cond )
{
llvmout << " " << dest << " = icmp " << getReOp(cond)
<< " i32 " << sL << ", " << i << "\n";
}
void printICmp( string dest, int i, string sR, int cond )
{
llvmout << " " << dest << " = icmp " << getReOp(cond)
<< " i32 " << i << ", " << sR << "\n";
}
void printXOr( string dest, string sL, string sR )/*i1*/
{
llvmout << " " << dest << " = xor i1 " << sL
<< ", " << sR << "\n";
}
void printZext( string dest, string s )
{
llvmout << " " << dest << " = zext i1 " << s << " to i32\n";
}
/************************************************************************/
/* branch */
/************************************************************************/
void printCondBr( string cond, string *trueP, string *falseP)
{
llvmout << " " << "br i1 " << cond << ", label %"
<< *trueP << ", label %" << *falseP << '\n';
//Handle the basic blocks number
//getTempReg();
}
void printBr( string *labelP )
{
llvmout << " " << "br label %" << *labelP << '\n';
//Handle the basic blocks number
//getTempReg();
}
/************************************************************************/
/* Read&Write */
/************************************************************************/
void printReadDef()
{
llvmout << "@.strRead = private unnamed_addr constant [3 x i8] "
<< "c\"%d\\00\", align 1\n";
llvmout << "declare i32 @scanf(i8*, ...)\n";
llvmout << "define i32 @" << ReadFunc << "() {\n";
llvmout << " %a = alloca i32, align 4\n";
llvmout << " %1 = call i32 (i8*, ...)* @scanf(i8* getelementptr inbounds "
<< "([3 x i8]* @.strRead, i32 0, i32 0), i32* %a)\n";
llvmout << " %2 = load i32* %a, align 4\n";
llvmout << " ret i32 %2\n";
llvmout << "}\n\n";
}
void printWriteDef()
{
llvmout << "@.strWrite = private unnamed_addr constant [4 x i8] "
<< "c\"%d\\0A\\00\", align 1\n";
llvmout << "declare i32 @printf(i8*, ...)\n";
llvmout << "define void @" << WriteFunc << "(i32 %a) {\n";
llvmout << " %1 = alloca i32, align 4\n";
llvmout << " store i32 %a, i32* %1, align 4\n";
llvmout << " %2 = load i32* %1, align 4\n";
llvmout << " %3 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds "
<< "([4 x i8]* @.strWrite, i32 0, i32 0), i32 %2)\n";
llvmout << " ret void\n";
llvmout << "}\n\n";
}
void printRead(string dest)
{
llvmout << " " << dest << " = call i32 @" << ReadFunc << "()\n";
}
void printWrite(string source)
{
llvmout << " " << "call void @"<< WriteFunc << "(i32 " << source << ")\n";
}
void printWrite(int i)
{
llvmout << " " << "call void @"<< WriteFunc << "(i32 " << i << ")\n";
}
void printHeadOfMain()
{
llvmout << " call void @" << InitFunc << "()\n\n";
}
void printErrorMessage(int lineno,string postion, string messages)
{
cerr << "Error[" << (++errorCount) << "][line " << lineno << "] in " << postion << " : " << messages << '\n';
}
void printMemCpyDef()
{
llvmout << "\ndeclare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind\n";
}