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.

114 lines
3.7 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 code"); // module name是什么无关紧要
auto builder = IRStmtBuilder::create(nullptr, module);
SysYF::Ptr<Type> Int32Type = Type::get_int32_type(module);
// add(a, b)
// 函数参数类型的vector
std::vector<SysYF::Ptr<Type>> Ints(2, Int32Type);
//通过返回值类型与参数类型列表得到函数类型
auto addFunTy = FunctionType::create(Int32Type, Ints);
// 由函数类型得到函数
auto addFun = Function::create(addFunTy,
"add", module);
// BB的名字在生成中无所谓,但是可以方便阅读
auto bb = BasicBlock::create(module, "entry", addFun);
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; // 获取add函数的形参,通过Function中的iterator
for (auto arg = addFun->arg_begin(); arg != addFun->arg_end(); arg++) {
args.push_back(*arg); // * 号运算符是从迭代器中取出迭代器当前指向的元素
}
builder->create_store(args[0], aAlloca); // store参数a
builder->create_store(args[1], bAlloca); // store参数b
auto aLoad = builder->create_load(aAlloca); // load参数a
auto bLoad = builder->create_load(bAlloca); // load参数b
// a+b
auto add = builder->create_iadd(aLoad, bLoad);
// a+b-1
auto sub = builder->create_isub(add, CONST_INT(1));
builder->create_store(sub, retAlloca);
auto retLoad = builder->create_load(retAlloca);
builder->create_ret(retLoad);
builder->create_ret(retLoad);
// main函数
auto mainFun = Function::create(FunctionType::create(Int32Type, {}),
"main", module);
bb = BasicBlock::create(module, "entry", mainFun);
// BasicBlock的名字在生成中无所谓,但是可以方便阅读
builder->set_insert_point(bb);
retAlloca = builder->create_alloca(Int32Type);
// int a;
auto a = builder->create_alloca(Int32Type);
// int b;
auto b = builder->create_alloca(Int32Type);
// int c;
auto c = builder->create_alloca(Int32Type);
// a = 3;
builder->create_store(CONST_INT(3), a);
// b = 2;
builder->create_store(CONST_INT(2), b);
// c = 5;
builder->create_store(CONST_INT(5), c);
// tmp = add(a, b);
aLoad = builder->create_load(a);
bLoad = builder->create_load(b);
auto tmp = builder->create_call(addFun, {aLoad, bLoad});
// ret = c + tmp;
auto cLoad = builder->create_load(c);
auto ret = builder->create_iadd(cLoad, tmp);
builder->create_store(ret, retAlloca);
retLoad = builder->create_load(retAlloca);
builder->create_ret(retLoad);
std::cout << module->print();
return 0;
}