#include "BasicBlock.h" #include "Constant.h" #include "Function.h" #include "IRStmtBuilder.h" #include "Module.h" #include "Type.h" #include #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("SysY code assign_test"); auto builder = IRStmtBuilder::create(nullptr, module); SysYF::Ptr Int32Type = Type::get_int32_type(module); SysYF::Ptr FloatType = Type::get_float_type(module); auto mainFunTy = FunctionType::create(Int32Type, {}); auto mainFun = Function::create(mainFunTy, "main", module); auto main_entry_bb = BasicBlock::create(module, "main_entry", mainFun); builder->set_insert_point(main_entry_bb); auto retAlloca = builder->create_alloca(Int32Type); auto bAlloca = builder->create_alloca(FloatType); builder->create_store(CONST_FP(1.8), bAlloca); auto arrayType_a = ArrayType::get(Int32Type, 2); auto aAlloca = builder->create_alloca(arrayType_a); 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 a0toFloat = builder->create_sitofp(a0Load, FloatType); auto bLoad = builder->create_load(bAlloca); auto mul = builder->create_fmul(a0toFloat, bLoad); auto multoi32 = builder->create_fptosi(mul, Int32Type); auto a1Gep = builder->create_gep(aAlloca, {CONST_INT(0), CONST_INT(1)}); builder->create_store(multoi32, a1Gep); auto a1Load = builder->create_load(a1Gep); builder->create_store(a1Load, retAlloca); auto retLoad = builder->create_load(retAlloca); builder->create_ret(retLoad); std::cout << module->print(); return 0; }