-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbp.cpp
More file actions
116 lines (97 loc) · 3.48 KB
/
bp.cpp
File metadata and controls
116 lines (97 loc) · 3.48 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
#include "bp.hpp"
#include <iostream>
#include <sstream>
#include <vector>
#include "ralloc.hpp"
#include "symbolTable.hpp"
extern int yylineno;
using namespace std;
bool replace(string& str, const string& from, const string& to, const BranchLabelIndex index);
CodeBuffer::CodeBuffer() : buffer(), globalDefs() {}
CodeBuffer& CodeBuffer::instance() {
static CodeBuffer inst; // only instance
return inst;
}
string CodeBuffer::genLabel(const string& prefix) {
std::stringstream label;
if (prefix != "") {
label << prefix << "_label_";
} else {
label << "label_";
}
label << buffer.size();
std::string ret(label.str());
label << ":";
emit("br label %" + ret, true);
emit(label.str());
return ret;
}
extern SymbolTable symbolTable;
int CodeBuffer::emit(const string& s, bool canSkip) {
int indentationDepth = symbolTable.getCurrentScopeDepth();
string indentationStr(indentationDepth, '\t');
// Skip both this and prev emit start with br
if (canSkip and s.substr(0, 9) == "br label " and (buffer.back().find("br ") != string::npos or buffer.back().find("ret ") != string::npos)) {
return -1;
}
buffer.push_back(indentationStr + s + " ; " + to_string(yylineno));
return buffer.size() - 1;
}
void CodeBuffer::bpatch(vector<pair<int, BranchLabelIndex>>& address_list, const std::string& label) {
for (vector<pair<int, BranchLabelIndex>>::const_iterator i = address_list.begin(); i != address_list.end(); i++) {
int address = (*i).first;
if (address == -1) {
continue;
}
BranchLabelIndex labelIndex = (*i).second;
replace(buffer[address], "@", "%" + label, labelIndex);
}
address_list.clear();
}
void CodeBuffer::bpatch(pair<int, BranchLabelIndex> pair, const std::string& label) {
int address = pair.first;
BranchLabelIndex labelIndex = pair.second;
replace(buffer[address], "@", "%" + label, labelIndex);
}
void CodeBuffer::printCodeBuffer() {
for (std::vector<string>::const_iterator it = buffer.begin(); it != buffer.end(); ++it) {
// Check if "label @" in in *it
if (it->find("label @") == string::npos) {
cout << *it << endl;
} else {
cout << "; DEBUG: removed> " << *it << endl;
}
}
}
vector<pair<int, BranchLabelIndex>> CodeBuffer::makelist(pair<int, BranchLabelIndex> item) {
vector<pair<int, BranchLabelIndex>> newList;
newList.push_back(item);
return newList;
}
vector<pair<int, BranchLabelIndex>> CodeBuffer::merge(const vector<pair<int, BranchLabelIndex>>& l1, const vector<pair<int, BranchLabelIndex>>& l2) {
vector<pair<int, BranchLabelIndex>> newList(l1.begin(), l1.end());
newList.insert(newList.end(), l2.begin(), l2.end());
return newList;
}
// ******** 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;
}
}
// ******** Helper Methods ********** //
bool replace(string& str, const string& from, const string& to, const BranchLabelIndex index) {
size_t pos;
if (index == SECOND) {
pos = str.find_last_of(from);
} else { // FIRST
pos = str.find_first_of(from);
}
if (pos == string::npos)
return false;
str.replace(pos, from.length(), to);
return true;
}