forked from XiaoYaoYouUSTC/Cminusf-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassign_generator.cpp
61 lines (51 loc) · 1.88 KB
/
assign_generator.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
#include "BasicBlock.h"
#include "Constant.h"
#include "Function.h"
#include "IRBuilder.h"
#include "Module.h"
#include "Type.h"
#include <iostream>
#include <memory>
#ifdef DEBUG // 用于调试信息,大家可以在编译过程中通过" -DDEBUG"来开启这一选项
#define DEBUG_OUTPUT std::cout << __LINE__ << std::endl; // 输出行号的简单示例
#else
#define DEBUG_OUTPUT
#endif
#define CONST_INT(num) \
ConstantInt::get(num, module)
#define CONST_FP(num) \
ConstantFP::get(num, module) // 得到常数值的表示,方便后面多次用到
int main() {
auto module = new Module("Cminus code"); // module name是什么无关紧要
auto builder = new IRBuilder(nullptr, module);
Type *Int32Type = Type::get_int32_type(module);
//main function
auto mainFun = Function::create(FunctionType::get(Int32Type,{}),"main",module);
//define the basicblock
auto bb = BasicBlock::create(module,"entry",mainFun);
builder->set_insert_point(bb);
//define return variation
auto retAlloca = builder->create_alloca(Int32Type);
//define a[10]
auto *arrayType = ArrayType::get(Int32Type,10);
auto a = builder->create_alloca(arrayType);
//set the value of return variation
builder->create_store(CONST_INT(0),retAlloca);
//set a[0]=10
auto a0GEP = builder->create_gep(a,{CONST_INT(0),CONST_INT(0)});
builder->create_store(CONST_INT(10),a0GEP);
//count a[0]*2
a0GEP = builder->create_gep(a,{CONST_INT(0),CONST_INT(0)});
auto a0Load = builder->create_load(a0GEP);
auto mul=builder->create_imul(a0Load,CONST_INT(2));
//set a[1]=a[0]*2
auto a1GEP = builder->create_gep(a,{CONST_INT(0),CONST_INT(1)});
builder->create_store(mul,a1GEP);
//return a[1]
a1GEP = builder->create_gep(a,{CONST_INT(0),CONST_INT(1)});
auto a1Load = builder->create_load(a1GEP);
builder->create_ret(a1Load);
std::cout<<module->print();
delete module;
return 0;
}