This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackendTest.cpp
153 lines (132 loc) · 6.03 KB
/
backendTest.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
152
153
#include <iostream>
#include "Instruction.h"
#include "IRTypes.h"
#include "IRTranslator.h"
#include "RegisterAllocationRefactor.h"
#include "NoRegisterAllocation.h"
#include "PeepholeOptimization.h"
using Instruction::ARMv7_Register;
using Instruction::BranchInstruction;
using namespace Instruction;
using namespace Instruction::Operands;
using namespace Instruction::Utilities::Abbr;
bool isDebug = true;
int optLevel = 2;
void testASM() {
std::cout << "Hello, World!" << std::endl;
Instruction::Operands::RegisterList list;
list.emplaceRegister(Instruction::ARMv7_Register::PC, Instruction::ARMv7_Register::R0,
Instruction::ARMv7_Register::R1);
auto add1 = Instruction::AdditionInstruction(Instruction::Operands::Register(Instruction::R1),
Instruction::Operands::Register(Instruction::PC),
Instruction::Operands::FlexibleOperand(
Instruction::Operands::Register(Instruction::R1),
Instruction::Operands::ImmediateNumber<8>(10),
Instruction::Operands::ImmediateNumber<5>(30)));
auto br1 = BranchInstruction(BL, "__FL1");
auto cmp1 = ComparisonInstruction(CMP, Register(R0), Register(LR));
std::cout << PushInstruction(list) << std::endl;
std::cout << add1 << std::endl;
std::cout << AdditionInstruction(Register(R1), Register(R2), imm12(-50)) << std::endl;
std::cout << br1 << std::endl;
std::cout << cmp1 << std::endl;
std::cout << AdditionInstruction(r0, Register(PC), Register(LR)) << std::endl;
std::cout << BitInstruction(AND, r0, Register(PC), Register(LR), true) << std::endl;
std::cout << LabelInstruction(Operands::Label("s")) << std::endl;
std::cout << LabelInstruction("__FL1") << std::endl;
std::cout << AdditionInstruction(r1, r2, r0) << std::endl;
std::cout << BranchInstruction(B, Label(".")) << std::endl;
std::cout << offset8(-1099) << std::endl;
InstructionStream is;
is << ComparisonInstruction(CMP, r0, pc) << BranchInstruction(B, Label("__next_gb"));
is << std::move(br1);
is << AdditionInstruction(pc, r1, FlexibleOperand(r2, imm8(10), imm5(3), true));
for (const auto &var : is)
std::cout << *var << std::endl;
}
void runner(const IntermediateRepresentation::IRProgram &irProgram) {
auto &&translator = Backend::Translator::Translator<Backend::RegisterAllocation::NoRegisterAllocation, Backend::Translator::availableRegister>(
irProgram);
try {
auto ins = translator.doTranslation();
puts("");
puts("[ASM]");
for (const auto &var : ins)
std::cout << *var << std::endl;
puts("");
puts("[ASM]");
auto res = DoScan(ins);
for (const auto &var : res)
std::cout << *var << std::endl;
} catch (const std::runtime_error &e) {
std::cerr << "Fatal error: " << e.what() << std::endl;
exit(-1);
}
}
void testCase1() {
using namespace IntermediateRepresentation;
IRProgram program;
Function function("Test");
IRArray glb_arr("glb_arr_tst", 10);
glb_arr.addData(5, 0xafff);
auto a = IROperand(i32, "a"), b = IROperand(i32, "b"),
c = IROperand(i32, "c"), d = IROperand(i32, "d"),
e = IROperand(i32, "e"), f = IROperand(i32, "f"),
glb = IROperand(i32, "glb_tst"), glb_str = IROperand(str, "glb_str");
program.setGlobal({
Statement{GLB_VAR, i32, glb},
Statement{GLB_VAR, str, glb_str, IROperand("%d %d %d\\n")}
});
program.setGlobalArrays({glb_arr});
auto genImm = [](int imm) {
return IROperand(i32, imm);
};
function.setParameters({
a, b, c, d
});
//
// function << Statement { ADD, i32, a, b, a };
// function << Statement { SUB, i32, b, c, a };
// function << Statement { MOD, i32, d, a, b };
function << Statement{CALL, i32, d, IROperand("printf"), glb_str, a, d, b, c};
function << Statement{RETURN, i32, c};
program.setFunctions({function});
std::cout << program.toString() << std::endl;
runner(program);
}
int main() {
IntermediateRepresentation::IRProgram irProgram;
IntermediateRepresentation::Function function("Test");
IntermediateRepresentation::Statement statement(IntermediateRepresentation::ADD, IntermediateRepresentation::i32,
IntermediateRepresentation::IROperand(
IntermediateRepresentation::i32, 32));
using namespace IntermediateRepresentation;
auto &&translator = Backend::Translator::Translator<Backend::RegisterAllocation::ColourAllocatorRewrite, Backend::Translator::availableRegister>(
irProgram);
/*
* mov %a, 5
* mov %b, 3
* div %c, %a, %b
* return %c
* */
function.insert(Statement(MOV, i32, IROperand(i32, "a"), IROperand(i32, 5)));
function << Statement{MOV, i32, IROperand(i32, "b"), IROperand(i32, 3)};
function << Statement{DIV, i32, IROperand(i32, "c"), IROperand(i32, "a"), IROperand(i32, "b")};
function << Statement{RETURN, i32, IROperand(i32, "c")};
irProgram.insert(function);
testCase1();
// std::cout << irProgram.toString();
// auto &&translator = Backend::Translator::Translator<Backend::RegisterAllocation::ColourAllocator, Backend::Translator::availableRegister>(irProgram);
// try {
// auto &&ins = translator.doTranslation();
// puts("");
// puts("[ASM]");
// for (const auto& var : ins)
// std::cout << *var << std::endl;
// } catch (std::exception e) {
// std::cerr << "Fatal error: " << e.what() << std::endl;
// exit(-1);
// }
// testASM();
return 0;
}