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.

51 lines
1.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
#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");
auto builder = IRStmtBuilder::create(nullptr, module);
SysYF::Ptr<Type> Int32Type = Type::get_int32_type(module);
SysYF::Ptr<Type> FloatType = Type::get_float_type(module);
SysYF::Ptr<Type> ArrayType = Type::get_array_type(Int32Type, 2);
auto mainFun = Function::create(FunctionType::create(Int32Type, {}),
"main", module);
auto bb = BasicBlock::create(module, "entry", mainFun);
builder->set_insert_point(bb);
auto bAlloca = builder->create_alloca(FloatType);
auto aAlloca = builder->create_alloca(ArrayType);
builder->create_store(CONST_FP(1.8), bAlloca);
auto bLoad = builder->create_load(bAlloca);
auto a0Gep = builder->create_gep(aAlloca, {CONST_INT(0), CONST_INT(0)});
builder->create_store(CONST_INT(2), a0Gep);
auto a0Load = builder->create_load(a0Gep);
auto a0siToFp = builder->create_sitofp(a0Load, FloatType);
auto fmul = builder->create_fmul(a0siToFp, bLoad);
auto FpToSi = builder->create_fptosi(fmul, Int32Type);
auto a1Gep = builder->create_gep(aAlloca, {CONST_INT(0), CONST_INT(1)});
builder->create_store(FpToSi, a1Gep);
auto a1Load = builder->create_load(a1Gep);
builder->create_ret(a1Load);
std::cout << module->print();
return 0;
}