-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_llvm.cpp
275 lines (249 loc) · 9.65 KB
/
test_llvm.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include "llvm/llvm.h"
#include "common/test.h"
#include "common/debug.h"
int main(int argc, char **argv)
{
// test llvm type to string
{
TEST_ASSERT_STR_EQ(MLLVM::str(MLLVM::LLVM_i1), "i1");
TEST_ASSERT_STR_EQ(MLLVM::str(MLLVM::LLVM_i32), "i32");
TEST_ASSERT_STR_EQ(MLLVM::str(MLLVM::LLVM_double), "double");
TEST_ASSERT_STR_EQ(MLLVM::str(MLLVM::LLVM_ptr), "ptr");
TEST_ASSERT_STR_EQ(MLLVM::str(MLLVM::LLVM_void), "void");
}
// test gen comment
{
MLLVM::LLVM_Context context;
context.gen_comment("test comment");
std::string result = context.llvm_instructions->back();
std::string expected = "; test comment";
TEST_ASSERT_STR_EQ(result, expected);
}
// test name generation
{
MLLVM::LLVM_Context context;
std::string result = context.gen_name("var");
std::string expected = "%var0";
TEST_ASSERT_STR_EQ(result, expected);
result = context.gen_name("var");
expected = "%var1";
TEST_ASSERT_STR_EQ(result, expected);
}
// reset name counter
{
MLLVM::LLVM_Context context;
context.reset_name_counter();
std::string result = context.gen_name("var");
std::string expected = "%var0";
TEST_ASSERT_STR_EQ(result, expected);
}
// test alloca instruction
{
MLLVM::LLVM_Context context;
std::string llvm_value = "%var0";
context.gen_alloc_inst(llvm_value, MLLVM::LLVM_i32);
std::string result = context.llvm_instructions->back();
;
std::string expected = "%var0 = alloca i32";
TEST_ASSERT_STR_EQ(result, expected);
}
// test store instruction
{
MLLVM::LLVM_Context context;
std::string llvm_src = "10";
std::string llvm_dst = "%var0";
context.gen_store_inst(llvm_src, llvm_dst, MLLVM::LLVM_i32);
std::string result = context.llvm_instructions->back();
std::string expected = "store i32 10, ptr %var0";
TEST_ASSERT_STR_EQ(result, expected);
}
// test load instruction
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_src = "%var_pointer";
context.gen_load_inst(llvm_src, llvm_dst, MLLVM::LLVM_i32);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = load i32, ptr %var_pointer";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm type definition
{
MLLVM::LLVM_Context context;
std::string llvm_type = "%struct.type";
std::vector<std::string> llvm_elements = {"i32", "i32"};
context.gen_define_type(llvm_type, llvm_elements);
std::string result = context.llvm_instructions->back();
std::string expected = "%struct.type = type { i32, i32 }";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm access field in type
{
MLLVM::LLVM_Context context;
std::string llvm_type = "%struct.A";
std::string llvm_field = "%field";
std::string llvm_struct_pointer = "%obj_a";
int offset = 0;
context.gen_offset_field_in_type(
llvm_field,
llvm_type,
llvm_struct_pointer,
offset);
std::string result = context.llvm_instructions->back();
std::string expected = "%field = getelementptr %struct.A, ptr %obj_a, i32 0, i32 0";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm global constant variable
{
MLLVM::LLVM_Context context;
std::string llvm_var_name = "var_name";
std::string llvm_type = "i32";
std::string llvm_value = "10";
context.gen_global_const_var(llvm_var_name, llvm_type, llvm_value);
std::string result = context.global_def->back();
std::string expected = "@var_name = constant i32 10";
TEST_ASSERT_STR_EQ(result, expected);
}
// test declare function instruction
{
MLLVM::LLVM_Context context;
std::string llvm_func_name = "func_name";
std::string llvm_return_type = "i32";
std::vector<std::string> llvm_args = {"i32", "i32"};
context.gen_declare_func(llvm_func_name, llvm_return_type, llvm_args);
std::string result = context.llvm_instructions->back();
std::string expected = "declare i32 @func_name(i32, i32)";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm function definition start
{
MLLVM::LLVM_Context context;
std::string llvm_func_name = "func_name";
std::string llvm_return_type = "i32";
std::vector<std::string> llvm_args = {"i32 %x", "i32 %x"};
context.gen_define_func_start(llvm_func_name, llvm_return_type, llvm_args);
std::string result = context.llvm_instructions->back();
std::string expected = "define i32 @func_name(i32 %x, i32 %x) {";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm function definition end
{
MLLVM::LLVM_Context context;
context.gen_define_func_end();
std::string result = context.llvm_instructions->back();
std::string expected = "}";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm label
{
MLLVM::LLVM_Context context;
std::string label = "entry";
context.gen_label(label);
std::string result = context.llvm_instructions->back();
std::string expected = "entry:";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm gen func call
{
MLLVM::LLVM_Context context;
std::string llvm_return_value = "%var";
std::string llvm_func_name = "func_name";
std::string llvm_return_type = "i32";
std::vector<std::pair<std::string, std::string>> llvm_args = {{"i32", "10"}, {"i32", "20"}};
context.gen_call_inst(llvm_return_value, llvm_func_name, llvm_return_type, llvm_args);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = call i32 @func_name(i32 10, i32 20)";
TEST_ASSERT_STR_EQ(result, expected);
}
/********** test math operations **********/
// test llvm add instruction int
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_add_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_i32);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = add i32 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm add instruction double
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_add_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_double);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = fadd double 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm sub instruction int
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_sub_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_i32);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = sub i32 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm sub instruction double
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_sub_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_double);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = fsub double 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm mul instruction int
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_mul_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_i32);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = mul i32 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm mul instruction double
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_mul_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_double);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = fmul double 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm div instruction int
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_div_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_i32);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = sdiv i32 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
// test llvm div instruction double
{
MLLVM::LLVM_Context context;
std::string llvm_dst = "%var";
std::string llvm_op1 = "10";
std::string llvm_op2 = "20";
context.gen_div_inst(llvm_dst, llvm_op1, llvm_op2, MLLVM::LLVM_double);
std::string result = context.llvm_instructions->back();
std::string expected = "%var = fdiv double 10, 20";
TEST_ASSERT_STR_EQ(result, expected);
}
TEST_PASS();
}