You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.9 KiB
53 lines
1.9 KiB
#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_gen");
|
|
auto builder = IRStmtBuilder::create(nullptr, module);
|
|
SysYF::Ptr<Type> Int32Type = Type::get_int32_type(module);
|
|
auto zero_initializer=ConstantZero::create(Int32Type,module);
|
|
auto a=GlobalVariable::create("a",module,Int32Type,false,zero_initializer);
|
|
auto mainFunc=Function::create(FunctionType::create(Int32Type,{}),"main",module);
|
|
auto bb=BasicBlock::create(module,"entry",mainFunc);
|
|
builder->set_insert_point(bb);
|
|
auto retAlloca=builder->create_alloca(Int32Type);
|
|
builder->create_store(CONST_INT(10),a);//a=10
|
|
auto aload=builder->create_load(a);
|
|
auto cmp=builder->create_icmp_gt(aload,CONST_INT(0));
|
|
auto truebb=BasicBlock::create(module,"if_true",mainFunc);
|
|
auto falsebb=BasicBlock::create(module,"if_false",mainFunc);
|
|
builder->create_cond_br(cmp,truebb,falsebb);
|
|
builder->set_insert_point(truebb);//true
|
|
aload=builder->create_load(a);
|
|
builder->create_store(aload,retAlloca);
|
|
auto retLoad=builder->create_load(retAlloca);
|
|
builder->create_ret(retLoad);
|
|
builder->set_insert_point(falsebb);
|
|
builder->create_store(CONST_INT(0),retAlloca);
|
|
retLoad=builder->create_load(retAlloca);
|
|
builder->create_ret(retLoad);
|
|
std::cout<<module->print();
|
|
return 0;
|
|
}
|