#include "BasicBlock.h" #include "Constant.h" #include "Function.h" #include "IRStmtBuilder.h" #include "Module.h" #include "Type.h" #include #include #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("assign_gen"); auto builder = IRStmtBuilder::create(nullptr, module); SysYF::Ptr Int32Type = Type::get_int32_type(module); SysYF::Ptr FloatType = Type::get_float_type(module); auto mainFunctype=FunctionType::create(Int32Type,{}); auto mainFunc=Function::create(mainFunctype,"main",module); auto bb=BasicBlock::create(module,"entry",mainFunc); builder->set_insert_point(bb); auto retAlloca=builder->create_alloca(Int32Type); auto bAlloca=builder->create_alloca(FloatType); builder->create_store(CONST_FP(1.8),bAlloca);//float b=1.8 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);//int a[2]={2} auto a0load=builder->create_load(a0gep); auto a0tofloat=builder->create_sitofp(a0load,FloatType); auto bload=builder->create_load(bAlloca); auto fmul=builder->create_fmul(a0tofloat,bload); auto fmultoint=builder->create_fptosi(fmul,Int32Type); auto a1gep=builder->create_gep(aAlloca,{CONST_INT(0),CONST_INT(1)}); builder->create_store(fmultoint,a1gep);//a[1]=a[0]*b auto a1load=builder->create_load(a1gep); builder->create_store(a1load,retAlloca); auto retLoad=builder->create_load(retAlloca); builder->create_ret(retLoad); std::cout<print(); return 0; }