diff --git a/Student/task2/cpp/assign_gen.cpp b/Student/task2/cpp/assign_gen.cpp index c30d8f1..b7337bf 100644 --- a/Student/task2/cpp/assign_gen.cpp +++ b/Student/task2/cpp/assign_gen.cpp @@ -1,3 +1,54 @@ -int main(){ - return 0; -} +#include "BasicBlock.h" +#include "Constant.h" +#include "Function.h" +#include "IRStmtBuilder.h" +#include "Module.h" +#include "Type.h" + +#include +#include + +#define CONST_INT(num) \ + ConstantInt::create(num, module) + +#define CONST_FP(num) \ + ConstantFloat::create(num, module) // 得到常数值的表示,方便后面多次用到 + +using namespace SysYF::IR; + +int main() +{ + // 创建module和builder + 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); + // main 函数 + auto mainFun = Function::create(FunctionType::create(Int32Type, {}), "main", module); + auto bb = BasicBlock::create(module, "entry", mainFun); + builder->set_insert_point(bb); + // 分配空间和赋初值 + auto ret_Alloca = builder->create_alloca(Int32Type); // int return + auto b_Alloca = builder->create_alloca(FloatType); // float b + auto ArrayType_a = ArrayType::get(Int32Type, 2); // Type of Array a + auto a_Alloca = builder->create_alloca(ArrayType_a); // int a[2] + auto a0GEP = builder->create_gep(a_Alloca, {CONST_INT(0), CONST_INT(0)}); // a[0] pointer + auto a1GEP = builder->create_gep(a_Alloca, {CONST_INT(0), CONST_INT(1)}); // a[1] pointer + + builder->create_store(CONST_FP(1.8), b_Alloca); // b = 1.8 + builder->create_store(CONST_INT(2), a0GEP); // a[0] = 2 + builder->create_store(CONST_INT(0), a1GEP); // a[1] = 0 + // 计算返回值 + auto b_Load = builder->create_load(b_Alloca); // load b + auto a0_Load = builder->create_load(a0GEP); // load a[0] + auto a0_float = builder->create_sitofp(a0_Load, FloatType); // int to float + auto ans_float = builder->create_fmul(b_Load, a0_float); // b * a[0] + auto ans_int = builder->create_fptosi(ans_float, Int32Type); // float to int + builder->create_store(ans_int, a1GEP); // store ans to a[1] + builder->create_store(ans_int, ret_Alloca); // return ans + // return + auto ret_Load = builder->create_load(ret_Alloca); + builder->create_ret(ret_Load); + // 输出IR + std::cout << module->print(); +} \ No newline at end of file diff --git a/Student/task2/cpp/while_gen.cpp b/Student/task2/cpp/while_gen.cpp index c30d8f1..d1d06a1 100644 --- a/Student/task2/cpp/while_gen.cpp +++ b/Student/task2/cpp/while_gen.cpp @@ -1,3 +1,63 @@ -int main(){ - return 0; -} +#include "BasicBlock.h" +#include "Constant.h" +#include "Function.h" +#include "IRStmtBuilder.h" +#include "Module.h" +#include "Type.h" + +#include +#include + +#define CONST_INT(num) \ + ConstantInt::create(num, module) + +#define CONST_FP(num) \ + ConstantFloat::create(num, module) // 得到常数值的表示,方便后面多次用到 + +using namespace SysYF::IR; + +int main() +{ + // 创建module和builder + 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); + // 全局变量 + auto zero_initializer = ConstantZero::create(Int32Type, module); + auto a = GlobalVariable::create("a", module, Int32Type, false, zero_initializer); + auto b = GlobalVariable::create("b", module, Int32Type, false, zero_initializer); + // main 函数 + auto mainFunTy = Function::create(FunctionType::create(Int32Type, {}), "main", module); + auto bb = BasicBlock::create(module, "entry", mainFunTy); + builder->set_insert_point(bb); + // 赋初值 + builder->create_store(CONST_INT(3), a); // a = 3 + builder->create_store(CONST_INT(0), b); // b = 0 + // 建立while循环 + auto condBB = BasicBlock::create(module, "condBB_while", mainFunTy); + auto trueBB = BasicBlock::create(module, "trueBB_while", mainFunTy); + auto falseBB = BasicBlock::create(module, "falseBB_while", mainFunTy); + builder->create_br(condBB); + builder->set_insert_point(condBB); + // while循环条件 + auto a_Load = builder->create_load(a); // load a + auto icmp = builder->create_icmp_gt(a_Load, CONST_INT(0)); + builder->create_cond_br(icmp, trueBB, falseBB); + // while循环体 + builder->set_insert_point(trueBB); + a_Load = builder->create_load(a); // load a + auto b_Load = builder->create_load(b); // load b + auto add = builder->create_iadd(b_Load, a_Load); + auto sub = builder->create_isub(a_Load, CONST_INT(1)); + builder->create_store(add, b); // b = b + a + builder->create_store(sub, a); // a = a - 1 + builder->create_br(condBB); // 跳转到循环条件 + // while循环结束 + builder->set_insert_point(falseBB); + // return + auto ret_Load = builder->create_load(b); + builder->create_ret(ret_Load); + // 输出IR + std::cout << module->print(); +} \ No newline at end of file