#include "BasicBlock.h" #include "Constant.h" #include "Function.h" #include "IRStmtBuilder.h" #include "Module.h" #include "Type.h" #include #include #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 Int32Type = Type::get_int32_type(module); SysYF::Ptr FloatType = Type::get_float_type(module); SysYF::Ptr 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; }