diff --git a/Student/task1/ll/if_hand.ll b/Student/task1/ll/if_hand.ll index e69de29..601ddd9 100644 --- a/Student/task1/ll/if_hand.ll +++ b/Student/task1/ll/if_hand.ll @@ -0,0 +1,11 @@ +@a = common global i32 0, align 4 +define i32 @main() #0 { + store i32 10, i32* @a, align 4 + %1 = load i32, i32* @a, align 4 + %2 = icmp sgt i32 %1, 0 + br i1 %2, label %3, label %4 +3: + ret i32 %1 +4: + ret i32 0 +} \ No newline at end of file diff --git a/Student/task2/cpp/assign_gen.cpp b/Student/task2/cpp/assign_gen.cpp index c30d8f1..4100493 100644 --- a/Student/task2/cpp/assign_gen.cpp +++ b/Student/task2/cpp/assign_gen.cpp @@ -1,3 +1,66 @@ -int main(){ +#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("Assign code"); + auto builder = IRStmtBuilder::create(nullptr, module); + SysYF::Ptr Int32Type = Type::get_int32_type(module); + SysYF::Ptr FloatType = Type::get_float_type(module); + + // main function + auto mainFun = Function::create(FunctionType::create(Int32Type, {}), + "main", module); + auto bb = BasicBlock::create(module, "entry", mainFun); + builder->set_insert_point(bb); + auto retAlloca = builder->create_alloca(Int32Type); + + // b = 1.8 + auto bAlloca = builder->create_alloca(FloatType); + builder->create_store(CONST_FP(1.8), bAlloca); + + // a[2] = {2} + 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); + + // a[0] * b + auto a0Load = builder->create_load(a0Gep); + auto a0Float = builder->create_sitofp(a0Load, FloatType); + auto bLoad = builder->create_load(FloatType, bAlloca); + auto a1res = builder->create_fmul(a0Float, bLoad); + + // store to a[1] + auto a1res_int = builder->create_fptosi(a1res, Int32Type); + auto a1Gep = builder->create_gep(aAlloca, {CONST_INT(0), CONST_INT(1)}); + builder->create_store(a1res_int, a1Gep); + + // ret + builder->create_store(a1res_int, retAlloca); + auto retLoad = builder->create_load(retAlloca); + builder->create_ret(retLoad); + + std::cout << module->print(); return 0; } diff --git a/Student/task2/cpp/if_gen.cpp b/Student/task2/cpp/if_gen.cpp index c30d8f1..208bc6e 100644 --- a/Student/task2/cpp/if_gen.cpp +++ b/Student/task2/cpp/if_gen.cpp @@ -1,3 +1,61 @@ -int main(){ +#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("If code"); + auto builder = IRStmtBuilder::create(nullptr, module); + SysYF::Ptr Int32Type = Type::get_int32_type(module); + SysYF::Ptr FloatType = Type::get_float_type(module); + + auto zero_initializer = ConstantZero::create(Int32Type, module); + auto a = GlobalVariable::create("a", module, Int32Type, false, zero_initializer); + + // main function + auto mainFun = Function::create(FunctionType::create(Int32Type, {}), + "main", module); + auto bb = BasicBlock::create(module, "entry", mainFun); + builder->set_insert_point(bb); + auto retAlloca = builder->create_alloca(Int32Type); + + // store 10 to a + builder->create_store(CONST_INT(10), a); + auto aValue = builder->create_load(a); + + // if(a > 0) + auto icmp = builder->create_icmp_gt(aValue, CONST_INT(0)); + auto trueBB = BasicBlock::create(module, "trueBB_if", mainFun); // true分支 + auto falseBB = BasicBlock::create(module, "falseBB_if", mainFun); // false分支 + builder->create_cond_br(icmp, trueBB, falseBB); // 条件BR + + // if true return a + builder->set_insert_point(trueBB); + builder->create_ret(aValue); + + // if false return 0 + builder->set_insert_point(falseBB); + builder->create_ret(CONST_INT(0)); + + std::cout << module->print(); return 0; }