This repository was archived by the owner on Aug 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (145 loc) · 3.54 KB
/
Copy pathmain.cpp
File metadata and controls
163 lines (145 loc) · 3.54 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
#include <iostream>
#include <string>
#include <vector>
#include "src/MipsTarget/MipsTarget.h"
#include "src/CompilerFrontend/Lexxer.h"
#include "src/CompilerFrontend/parser.h"
#include <cmath>
#include <functional>
#include <filesystem>
#include "src/LLVMBackend/LLVMTarget.h"
using namespace std;
namespace fs = std::filesystem;
#pragma endregion
void handle_cli(char *argv[], int argc)
{
if (argc == 1)
{
return;
}
char *file = argv[1];
vector<Tokens> a = lex(readFile(file));
cout << "lexxed sucessfully" << endl;
for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "-token") == 0)
{
printList(a);
}
}
vector<unique_ptr<FunctionNode>> c = move(parse(a));
cout << "parsed successfully" << endl;
string file1 = "";
for (int i = 0; i < argc; i++)
{
if (strcmp(argv[i], "-o") == 0)
{
file1 = argv[i + 1];
}
}
gen_mips_target(move(c), file1);
cout << "compiled successfully" << endl;
}
void llvmtest()
{
vector<Tokens> a = lex(readFile("test.lk"));
// printList(a);
vector<unique_ptr<FunctionNode>> c = move(parse(a));
gen_LLVM(move(c), "");
}
int branchnym = 0;
int registers = -1;
string allocate_reg()
{
registers++;
if (registers >= 10)
{
registers = 0;
}
return "$t" + to_string(registers);
}
string false_branch = "";
string true_branch = "";
int num_ors = 0;
// expiremental
string traverse(unique_ptr<Node> op, string &global_string, int isLoop = 0)
{
if (op == nullptr)
{
return "";
}
if (instanceof <IntegerNode *>(op.get()))
{
IntegerNode *pd = dynamic_cast<IntegerNode *>(op.get());
string reg = allocate_reg();
int num = stoi(pd->num) * OFFSET;
global_string += "li " + reg + "," + to_string(num) + "\n";
return reg;
}
if (instanceof <FloatNode *>(op.get()))
{
FloatNode *pd = dynamic_cast<FloatNode *>(op.get());
string reg = allocate_reg();
int num = stoi(pd->num);
global_string += "li " + reg + "," + to_string(num) + "\n";
return reg;
}
if (instanceof <BoolExpressionNode *>(op.get()))
{
BoolExpressionNode *pd = dynamic_cast<BoolExpressionNode *>(op.get());
false_branch = to_string(branchnym + 1);
true_branch = to_string(branchnym);
if (pd->op.value().id == type::OR)
{
string reg0 = traverse(move(pd->left), global_string, !isLoop);
string reg1 = traverse(move(pd->right), global_string);
return "";
}
else if (pd->op.value().id == type::AND)
{
string reg0 = traverse(move(pd->left), global_string);
string reg1 = traverse(move(pd->right), global_string);
return "";
}
string reg0 = traverse(move(pd->left), global_string);
string reg1 = traverse(move(pd->right), global_string);
if (pd->op.value().id == type::BOOL_EQ)
{
if (isLoop == 1)
{
global_string += "beq " + reg0 + "," + reg1 + "," + "L" + true_branch + "\n";
}
else
{
global_string += "bne " + reg0 + "," + reg1 + "," + "L" + false_branch + "\n";
}
return "";
}
}
return "";
}
int main(int argc, char *argv[])
{
// llvmtest();
// int a[8] = {1,2};
// int b[7] = {2,1};
// b = a;
handle_cli(argv, argc);
// vector<Tokens> a = lex(readFile("test.lk"));
// unique_ptr<Node> b = test(a);
// string global_string = ".data \n .text \n main: \n";
// traverse(move(b), global_string);
// string dirname = "src/MipsTarget/MipsTargetASM/";
// fs::create_directories(dirname);
//
// string filename = "out.s";
//
//
// ofstream outfile(dirname + filename);
// global_string += "li $v0,10 \n syscall ";
// cout<<"numors: " << num_ors << endl;
// cout << global_string << endl;
// outfile.close();
// gen_LLVM();
return 0;
}