-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
151 lines (139 loc) · 4.48 KB
/
main.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
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
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
#include "parsing/tokenize.h"
#include "parsing/parse.h"
#include "mipsCompiler/MipsCompiler.h"
int main(int argc, char** argv) {
/*
-o = output file
-type = type of output (s, mem)
-r a b ... = run, print variables a, b, ... at end
*/
std::string output_file;
std::vector<std::string> files;
std::vector<std::string> runVars;
std::string output_type = "s";
bool run = false;
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "-o") {
output_file = argv[++i];
} else if (std::string(argv[i]) == "-r") {
run = true;
i++;
for (; i < argc; i++) {
runVars.emplace_back(argv[i]);
}
}
else if (std::string(argv[i]) == "-type"){
output_type = argv[++i];
}
else {
files.emplace_back(argv[i]);
}
}
if (files.empty()) {
std::cerr << "No input files" << std::endl;
return 1;
}
if (output_file.empty()) {
std::cerr << "No output file" << std::endl;
return 1;
}
std::vector<Token*> tokens;
for (const std::string& file : files) {
std::ifstream in(file);
if (!in.is_open()) {
std::cerr << "Could not open file " << file << std::endl;
return 1;
}
std::string line;
std::string file_text = "";
while (std::getline(in, line)) {
file_text += line + "\n";
}
std::vector<Token*> newTokens = tokenize(file_text);
tokens.insert(tokens.end(), newTokens.begin(), newTokens.end());
}
TokenIterator tokens_iter(tokens);
Scope scope(nullptr);
std::vector<Token*> ast = parse(tokens_iter, &scope);
sort_ast(&ast, &scope);
MipsBuilder builder;
builder.addInstruction(new InstrAddi(29, 29, 2047), "");
VariableTracker tracker(&builder);
BreakScope breakScope;
compile_instructions(&breakScope, ast, &builder, &tracker);
for (Token* token : ast) {
delete token;
}
int mem_loc = tracker.get_mem_offset();
builder.prependInstruction(new InstrAddi(28, 0, mem_loc));
builder.simplify();
builder.linkLabels();
// save to file
if (output_type == "s") {
std::ofstream out(output_file);
if (!out.is_open()) {
std::cerr << "Could not open output file " << output_file << std::endl;
return 1;
}
std::string assembly_code = builder.export_str();
out << assembly_code;
}
else if (output_type == "mem") {
std::ofstream out(output_file, std::ios::binary);
if (!out.is_open()) {
std::cerr << "Could not open output file " << output_file << std::endl;
return 1;
}
std::vector<uint32_t> mem = builder.export_mem();
for (uint32_t word : mem) {
std::bitset<32> bits(word);
out.write(bits.to_string().c_str(), 32);
out << std::endl;
}
if (mem.size() < 4096){
for (int i = 0; i < 4096 - mem.size(); i++) {
out << "00000000000000000000000000000000" << std::endl;
}
}
}
else if (output_type == "numbers"){
std::vector<uint32_t> mem = builder.export_mem();
std::ofstream out(output_file);
if (!out.is_open()) {
std::cerr << "Could not open output file " << output_file << std::endl;
return 1;
}
out << "{";
for (int i = 0; i < mem.size(); i++) {
out << mem[i];
if (i < mem.size() - 1) out << ", ";
}
out << "}";
}
else {
std::cerr << "Invalid output type " << output_type << std::endl;
return 1;
}
if (run){
std::vector<Instruction*> instructions = builder.getInstructions();
MipsRunner runner(2048, instructions.data(), instructions.size());
int num_cycles = runner.run(50000);
printf("Ran for %d cycles\n", num_cycles);
for (const std::string& var : runVars){
uint8_t reg = tracker.getReg(var, false);
int mem = tracker.get_mem_addr(var);
if (mem <= 0) mem = -mem;
else mem -= 1;
printf("%s = %d (reg %d) or %d (mem %d)\n",
var.c_str(),
runner.get_reg(reg),
reg,
runner.get_mem(mem),
mem);
}
}
}