-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg.cpp
57 lines (49 loc) · 1.23 KB
/
cg.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
#include "cg.hpp"
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;
CodeBuffer::CodeBuffer() : buffer(), globalDefs() {}
CodeBuffer &CodeBuffer::instance() {
static CodeBuffer inst;//only instance
return inst;
}
string CodeBuffer::freshLabel(){
std::stringstream label;
label << "label_" << ++labels_num;
return label.str();
}
string CodeBuffer::freshVar(){
std::stringstream var;
var << "%t" << ++vars_num;
return var.str();
}
int CodeBuffer::emit(const string &s, const string &comment){
bool debug = true;
int incline = 50;
if(comment.length() == 0 || !debug){
buffer.push_back(s);
}
else{
buffer.push_back(s + std::string(max(incline-(long)s.length(),(long)3), ' ') + "; " + comment);
}
return buffer.size() - 1;
}
void CodeBuffer::printCodeBuffer(){
for (std::vector<string>::const_iterator it = buffer.begin(); it != buffer.end(); ++it)
{
cout << *it << endl;
}
}
// ******** Methods to handle the global section ********** //
void CodeBuffer::emitGlobal(const std::string& dataLine)
{
globalDefs.push_back(dataLine);
}
void CodeBuffer::printGlobalBuffer()
{
for (vector<string>::const_iterator it = globalDefs.begin(); it != globalDefs.end(); ++it)
{
cout << *it << endl;
}
}