-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.hpp
217 lines (209 loc) · 6.07 KB
/
parser.hpp
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
#ifndef __parser__
#define __parser__
#include <unordered_map>
#include <string>
#include <functional>
#include <vector>
#include <algorithm>
#include <fstream>
#include <stdexcept>
#include <exception>
#include <iostream>
#include <boost/tokenizer.hpp>
#include "requirements.hpp"
#include "pipeline.hpp"
#include "configuration.hpp"
using namespace std;
struct Parser
{
vector< struct Command* > parametric_commands;
vector<vector<int>> parametrs;
std:: unordered_map<string,vector<int>> indices;
std:: unordered_map<string,vector<string>> stage_names;
struct Pipeline *pipeline;
std::unordered_map<std::string, int> registerMap, address;
std::vector<std::vector<std::string>> commands;
std::vector<int> commandCount;
Parser(std::ifstream &file, int question)
{
for (int i = 0; i < 32; ++i)
registerMap["$" + std::to_string(i)] = i;
registerMap["$zero"] = 0;
registerMap["$at"] = 1;
registerMap["$v0"] = 2;
registerMap["$v1"] = 3;
for (int i = 0; i < 4; ++i)
registerMap["$a" + std::to_string(i)] = i + 4;
for (int i = 0; i < 8; ++i)
registerMap["$t" + std::to_string(i)] = i + 8, registerMap["$s" + std::to_string(i)] = i + 16;
registerMap["$t8"] = 24;
registerMap["$t9"] = 25;
registerMap["$k0"] = 26;
registerMap["$k1"] = 27;
registerMap["$gp"] = 28;
registerMap["$sp"] = 29;
registerMap["$s8"] = 30;
registerMap["$ra"] = 31;
struct Configuration* config = new Configuration(question);
parametrs = config->parametrs;
indices = config->indices;
stage_names = config->stage_names;
pipeline = config->pipeline;
constructCommands(file);
commandCount.assign(commands.size(), 0);
}
struct Command* define(std::vector<string> command){
bool in1=parametrs[0][0]==1;
int in2=parametrs[0][1];
vector<int> in4=indices[command[0]];
vector<int> in5;
vector<string> command_type1={"add","sub","and","or","slt","mul","and","or"}, command_type2={"addi","andi","ori","sll","srl"};
vector<string> command_type3={"beq","bne"}, command_type4={"j","jal"};
int in9;
std::vector<std::string> subsetVec;
std::copy_if(command.begin(), command.end(), std::back_inserter(subsetVec),
[](const std::string& s) {
return s.find("$") != std::string::npos;
}
);
if(std::find(command_type1.begin(),command_type1.end(),command[0])!=command_type1.end()){
in5={registerMap[subsetVec[0]],registerMap[subsetVec[1]],registerMap[subsetVec[2]]};
in9=-1;
}
else if(std::find(command_type2.begin(),command_type2.end(),command[0])!=command_type2.end()){
in5={registerMap[subsetVec[0]],registerMap[subsetVec[1]],-1};
in9=stoi(command[3]);
}
else if(command[0]=="lw"){
in5={registerMap[subsetVec[0]],registerMap[subsetVec[1]],-1};
if(std::find(command[2].begin(),command[2].end(),'$')==command[2].end()){
in9=stoi(command[2]);
}
else{
in9=0;
}
}
else if(command[0]=="sw"){
in5={-1,registerMap[subsetVec[0]],registerMap[subsetVec[1]]};
if(std::find(command[2].begin(),command[2].end(),'$')==command[2].end()){
in9=stoi(command[2]);
}
else{
in9=0;
}
}
else if(std::find(command_type3.begin(),command_type3.end(),command[0])!=command_type3.end()){
in5={-1,registerMap[subsetVec[0]],registerMap[subsetVec[1]]};
in9=-1;
}
else if(std::find(command_type4.begin(),command_type4.end(),command[0])!=command_type4.end()){
in5={-1,-1,-1};
in9=-1;
}
else{
std:: cerr <<"error in command type"<<std::endl;
}
vector<string> in6=stage_names[command[0]];
vector<int> in3;
unordered_map<string, int> stagemap;
for (int i = 0; i < (int)in6.size(); i++) {
stagemap[in6[i]] = i;
}
for(int i=0;i<in6.size();i++){
in3.push_back(parametrs[1][stagemap[in6[i]]]);
}
string in7=command[0];
struct Command* cmd = new Command(in1,in2,in3,in4,in5,in6,in7,0,in9);
return cmd;
}
void parseCommand(std::string line)
{
// strip until before the comment begins
line = line.substr(0, line.find('#'));
std::vector<std::string> command;
boost::tokenizer<boost::char_separator<char>> tokens(line, boost::char_separator<char>(", \t()"));
for (auto &s : tokens){
command.push_back(s);
}
// empty line or a comment only line
if (command.empty()){
return;
}
else if (command.size() == 1)
{
std::string label = command[0].back() == ':' ? command[0].substr(0, command[0].size() - 1) : "?";
if (address.find(label) == address.end()){
address[label] = commands.size();
}
else{
address[label] = -1;
}
command.clear();
}
else if (command[0].back() == ':')
{
std::string label = command[0].substr(0, command[0].size() - 1);
if (address.find(label) == address.end()){
address[label] = commands.size();
}
else{
address[label] = -1;
}
command = std::vector<std::string>(command.begin() + 1, command.end());
}
else if (command[0].find(':') != std::string::npos)
{
int idx = command[0].find(':');
std::string label = command[0].substr(0, idx);
if (address.find(label) == address.end()){
address[label] = commands.size();
}
else{
address[label] = -1;
}
command[0] = command[0].substr(idx + 1);
}
else if (command[1][0] == ':')
{
if (address.find(command[0]) == address.end()){
address[command[0]] = commands.size();
}
else{
address[command[0]] = -1;
}
command[1] = command[1].substr(1);
if (command[1] == ""){
command.erase(command.begin(), command.begin() + 2);
}
else{
command.erase(command.begin(), command.begin() + 1);
}
}
if (command.empty()){
return;
}
if (command.size() > 4){
for (int i = 4; i < (int)command.size(); ++i){
command[3] += " " + command[i];
}
}
command.resize(4);
commands.push_back(command);
struct Command* parametric= define(command);
parametric_commands.push_back(parametric);
}
void print_commands()
{
for(int i=0;i<(int)commands.size();i++){
parametric_commands[i]->print_command();
}
}
void constructCommands(std::ifstream &file)
{
std::string line;
while (getline(file, line))
parseCommand(line);
file.close();
}
};
#endif