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.
89 lines
2.9 KiB
89 lines
2.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
|
|
#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("func");
|
|
auto builder = IRStmtBuilder::create(nullptr, module);
|
|
SysYF::Ptr<Type> Int32Type = Type::get_int32_type(module);
|
|
|
|
// add函数
|
|
std::vector<SysYF::Ptr<Type>> Ints(2, Int32Type);
|
|
auto addFunTY = FunctionType::create(Int32Type, Ints);
|
|
auto addFun = Function::create(addFunTY,
|
|
"add", module);
|
|
|
|
auto bb = BasicBlock::create(module, "entry", addFun);
|
|
builder->set_insert_point(bb);
|
|
auto retAlloca = builder->create_alloca(Int32Type); // 分配返回值
|
|
auto aAlloca = builder->create_alloca(Int32Type); // 分配传入参数 a和b
|
|
auto bAlloca = builder->create_alloca(Int32Type);
|
|
|
|
std::vector<SysYF::Ptr<Value>> args; // 获取climbStairs函数的形参,通过Function中的iterator
|
|
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 aLoad = builder->create_load(aAlloca);
|
|
auto bLoad = builder->create_load(bAlloca);
|
|
|
|
auto sum = builder->create_iadd(aLoad, bLoad);
|
|
auto diff = builder->create_isub(sum, CONST_INT(1));
|
|
|
|
builder->create_store(diff, retAlloca);
|
|
auto retLoad = builder->create_load(retAlloca);
|
|
builder->create_ret(retLoad);
|
|
|
|
|
|
// main函数
|
|
auto mainFun = Function::create(FunctionType::create(Int32Type, {}),
|
|
"main", module);
|
|
bb = BasicBlock::create(module, "entry", mainFun);
|
|
builder->set_insert_point(bb);
|
|
|
|
retAlloca = builder->create_alloca(Int32Type); // 分配返回值
|
|
aAlloca = builder->create_alloca(Int32Type); // 分配传入参数 a和b
|
|
bAlloca = builder->create_alloca(Int32Type);
|
|
auto cAlloca = builder->create_alloca(Int32Type);
|
|
|
|
builder->create_store(CONST_INT(3), aAlloca);
|
|
builder->create_store(CONST_INT(2), bAlloca);
|
|
builder->create_store(CONST_INT(5), cAlloca);
|
|
|
|
aLoad = builder->create_load(aAlloca);
|
|
bLoad = builder->create_load(bAlloca);
|
|
auto output = builder->create_call(addFun, {aLoad, bLoad});
|
|
|
|
auto cLoad = builder->create_load(cAlloca);
|
|
sum = builder->create_iadd(cLoad, output);
|
|
|
|
builder->create_store(sum, retAlloca);
|
|
retLoad = builder->create_load(retAlloca);
|
|
builder->create_ret(retLoad);
|
|
|
|
std::cout << module->print();
|
|
return 0;
|
|
}
|