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.
93 lines
3.5 KiB
93 lines
3.5 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("sysYF func");
|
|
auto builder = IRStmtBuilder::create(nullptr, module);
|
|
SysYF::Ptr<Type> Int32Type = Type::get_int32_type(module);
|
|
auto zero_initializer = ConstantZero::create(Int32Type, module);
|
|
|
|
// int add(int a,int b){
|
|
// return (a+b-1);
|
|
// }
|
|
auto addFunTy = FunctionType::create(Int32Type, {Int32Type, Int32Type});//add函数有两个参数
|
|
auto addFun = Function::create(addFunTy, "add", module);//创建add函数
|
|
|
|
auto BB = BasicBlock::create(module, "entry", addFun);//创建一个名为entry的基本块
|
|
builder->set_insert_point(BB); // 一个BB的开始,将当前插入指令点的位置设在bb
|
|
auto retAlloca = builder->create_alloca(Int32Type); // 在内存中分配返回值的位置
|
|
auto aAlloca = builder->create_alloca(Int32Type);//在内存中分配a的位置
|
|
auto bAlloca = builder->create_alloca(Int32Type);//在内存中分配b的位置
|
|
|
|
std::vector<SysYF::Ptr<Value>> args;
|
|
for (auto arg = addFun->arg_begin(); arg != addFun->arg_end(); arg++) {
|
|
args.push_back(*arg); // * 号运算符是从迭代器中取出迭代器当前指向的元素
|
|
}
|
|
|
|
builder->create_store(args[0], aAlloca);
|
|
builder->create_store(args[1], bAlloca);
|
|
|
|
auto retBB = BasicBlock::create(module, "retBB", addFun);
|
|
|
|
//a+b
|
|
auto aLoad = builder->create_load(aAlloca);
|
|
auto bLoad = builder->create_load(bAlloca);
|
|
auto aAddb = builder->create_iadd(aLoad, bLoad);
|
|
//a+b-1
|
|
auto sub1 = builder->create_isub(aAddb, CONST_INT(1));
|
|
//return a+b-1
|
|
builder->create_store(sub1, retAlloca);
|
|
builder->create_br(retBB);
|
|
|
|
builder->set_insert_point(retBB);
|
|
auto retLoad = builder->create_load(retAlloca);
|
|
builder->create_ret(retLoad);
|
|
|
|
//main()
|
|
auto mainFunTy = FunctionType::create(Int32Type, {});
|
|
auto mainFun = Function::create(mainFunTy, "main", module);
|
|
|
|
auto mainBB = BasicBlock::create(module, "entry", mainFun);
|
|
builder->set_insert_point(mainBB);
|
|
retAlloca = builder->create_alloca(Int32Type);
|
|
aAlloca = builder->create_alloca(Int32Type);//int a
|
|
bAlloca = builder->create_alloca(Int32Type);//int b
|
|
auto cAlloca = builder->create_alloca(Int32Type);//int c
|
|
builder->create_store(CONST_INT(3), aAlloca);//a=3
|
|
builder->create_store(CONST_INT(2), bAlloca);//b=2
|
|
builder->create_store(CONST_INT(5), cAlloca);//c=5
|
|
aLoad = builder->create_load(aAlloca);//a
|
|
bLoad = builder->create_load(bAlloca);//b
|
|
auto cLoad = builder->create_load(cAlloca);//c
|
|
auto callAdd = builder->create_call(addFun, {aLoad, bLoad});//add(a,b)
|
|
auto cAddret = builder->create_iadd(cLoad, callAdd);//c+add(a,b)
|
|
builder->create_store(cAddret, retAlloca);//ret=c+add(a,b)
|
|
//return
|
|
retLoad = builder->create_load(retAlloca);
|
|
builder->create_ret(retLoad);
|
|
|
|
std::cout << module->print();
|
|
return 0;
|
|
}
|