|
|
#include "BasicBlock.h"
|
|
|
#include "Constant.h"
|
|
|
#include "Function.h"
|
|
|
#include "IRStmtBuilder.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::create(num, module)
|
|
|
|
|
|
#define CONST_FP(num) \
|
|
|
ConstantFloat::create(num, module) // 得到常数值的表示,方便后面多次用到
|
|
|
|
|
|
using namespace SysYF::IR;
|
|
|
|
|
|
int main() {
|
|
|
auto module = Module::create("if_test.sy"); // module name是if_test
|
|
|
auto builder = IRStmtBuilder::create(nullptr, module);
|
|
|
SysYF::Ptr<Type> Int32Type = Type::get_int32_type(module);
|
|
|
|
|
|
// 生成全局变量a, b
|
|
|
auto zero_initializer = ConstantZero::create(Int32Type, module);
|
|
|
auto a = GlobalVariable::create("a", module, Int32Type, false, zero_initializer);
|
|
|
auto b = GlobalVariable::create("b", module, Int32Type, false, zero_initializer);
|
|
|
|
|
|
// 生成main函数
|
|
|
auto mainFun = Function::create(FunctionType::create(Int32Type, {}),
|
|
|
"main", module);
|
|
|
// 生成一个基本块并进入
|
|
|
auto entry = BasicBlock::create(module, "entry", mainFun);
|
|
|
builder->set_insert_point(entry);
|
|
|
|
|
|
// 给b,a赋值0,3
|
|
|
builder->create_store(CONST_INT(0), b);
|
|
|
builder->create_store(CONST_INT(3), a);
|
|
|
|
|
|
// 设置三个BasicBlock
|
|
|
auto while_cond = BasicBlock::create(module, "while_cond", mainFun);
|
|
|
auto while_body = BasicBlock::create(module, "while_body", mainFun);
|
|
|
auto while_end = BasicBlock::create(module, "while_end", mainFun);
|
|
|
|
|
|
// 跳转while_cond
|
|
|
builder->create_br(while_cond);
|
|
|
|
|
|
// while_cond
|
|
|
builder->set_insert_point(while_cond);
|
|
|
// 把a加载到寄存器
|
|
|
auto aReg = builder->create_load(a);
|
|
|
// 创造一个比较语句,就是说a>0
|
|
|
auto icmp = builder->create_icmp_gt(aReg, CONST_INT(0)); // n和4的比较
|
|
|
// 创建一个分支br
|
|
|
builder->create_cond_br(icmp, while_body, while_end);
|
|
|
|
|
|
// while_body
|
|
|
builder->set_insert_point(while_body);
|
|
|
// b = b + 1
|
|
|
auto aReg2 = builder->create_load(a);
|
|
|
auto bReg2 = builder->create_load(b);
|
|
|
auto abReg = builder->create_iadd(aReg2, bReg2);
|
|
|
builder->create_store(abReg, b);
|
|
|
// a = a - 1,此时可以优化编译流程,不重新加载a
|
|
|
auto aReg3 = builder->create_isub(aReg2, CONST_INT(1));
|
|
|
builder->create_store(aReg3, a);
|
|
|
// 跳转
|
|
|
builder->create_br(while_cond);
|
|
|
|
|
|
// while_body
|
|
|
builder->set_insert_point(while_end);
|
|
|
// 构建假的返回
|
|
|
auto bReg3 = builder->create_load(b);
|
|
|
builder->create_ret(bReg3);
|
|
|
|
|
|
std::cout << module->print();
|
|
|
return 0;
|
|
|
}
|