diff --git a/CMakeLists.txt b/CMakeLists.txt index 74dcb27..4e8c257 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,4 @@ +include_directories(${PROJECT_SOURCE_DIR}/src/antlr4) cmake_minimum_required(VERSION 3.20) project(compiler LANGUAGES C CXX) diff --git a/bash.sh b/bash.sh new file mode 100644 index 0000000..323d486 --- /dev/null +++ b/bash.sh @@ -0,0 +1,34 @@ +OUTPUT_DIR="test_outputs" +mkdir -p "$OUTPUT_DIR" + +echo "=== 编译器功能测试 ===" +echo "结果将保存到 $OUTPUT_DIR/ 目录" +echo "" + +for test in test/test_case/functional/*.sy; do + name=$(basename "$test" .sy) + + echo "正在测试: $name" + + # 检查是否有对应的输入文件 + in_file="test/test_case/functional/${name}.in" + out_file="$OUTPUT_DIR/${name}.ll" + + if [ -f "$in_file" ]; then + echo " 使用输入文件: $in_file" + echo " 输出保存到: $out_file" + echo "" + ./build/bin/compiler --emit-ir "$test" < "$in_file" 2>&1 | tee "$out_file" + else + echo " 输出保存到: $out_file" + echo "" + ./build/bin/compiler --emit-ir "$test" 2>&1 | tee "$out_file" + fi + + echo "" + echo "--- 测试完成 ---" + echo "" +done + +echo "所有测试完成!" +echo "查看结果: ls -la $OUTPUT_DIR/" \ No newline at end of file diff --git a/include/frontend/AntlrDriver.h b/include/frontend/AntlrDriver.h index ee22da9..5403e14 100644 --- a/include/frontend/AntlrDriver.h +++ b/include/frontend/AntlrDriver.h @@ -4,8 +4,8 @@ #include #include -#include "SysYLexer.h" -#include "SysYParser.h" +#include "antlr4/SysYLexer.h" +#include "antlr4/SysYParser.h" #include "antlr4-runtime.h" struct AntlrResult { diff --git a/include/irgen/IRGen.h b/include/irgen/IRGen.h index 4e13fab..a445a97 100644 --- a/include/irgen/IRGen.h +++ b/include/irgen/IRGen.h @@ -6,14 +6,19 @@ #include #include #include +#include #include #include -#include "SysYBaseVisitor.h" -#include "SysYParser.h" +#include "antlr4/SysYBaseVisitor.h" +#include "antlr4/SysYParser.h" #include "ir/IR.h" #include "sem/Sema.h" +// 前向声明:语义层可能在未来提供更明确的符号类型,用于把符号唯一标识映射到 IR 对象。 +struct SemanticVarSymbol; +struct SemanticFuncSymbol; + namespace ir { class Module; class Function; @@ -54,6 +59,10 @@ class IRGenImpl final : public SysYBaseVisitor { std::any visitLAndExp(SysYParser::LAndExpContext* ctx) override; std::any visitLOrExp(SysYParser::LOrExpContext* ctx) override; + // 辅助接口:数组下标地址计算与全局变量生成 + ir::Value* EmitArrayIndex(ir::Value* base_ptr, SysYParser::ExpContext* idx_expr); + void EmitGlobalVariable(SysYParser::VarDefContext* ctx); + private: enum class BlockFlow { Continue, @@ -72,7 +81,10 @@ class IRGenImpl final : public SysYBaseVisitor { ir::Value* EvalExpr(SysYParser::ExpContext& expr); ir::Value* EvalCond(SysYParser::CondContext& cond); ir::Value* ToBoolValue(ir::Value* v); + ir::Value* FindInScope(const std::string& name); std::string NextBlockName(); + ir::Function* FindFunctionByName(const std::string& name); + ir::Value* ResolveLValueAddress(SysYParser::LValueContext* ctx); // 预声明 SysY runtime 外部函数。 void DeclareRuntimeFunctions(); diff --git a/src/frontend/AntlrDriver.cpp b/src/frontend/AntlrDriver.cpp index ee3c98c..7a77566 100644 --- a/src/frontend/AntlrDriver.cpp +++ b/src/frontend/AntlrDriver.cpp @@ -6,8 +6,8 @@ #include #include -#include "SysYLexer.h" -#include "SysYParser.h" +#include "antlr4/SysYLexer.h" +#include "antlr4/SysYParser.h" #include "antlr4-runtime.h" #include "utils/Log.h" diff --git a/src/frontend/CMakeLists.txt b/src/frontend/CMakeLists.txt index 524fcd6..4d8d100 100644 --- a/src/frontend/CMakeLists.txt +++ b/src/frontend/CMakeLists.txt @@ -8,9 +8,10 @@ target_link_libraries(frontend PUBLIC ${ANTLR4_RUNTIME_TARGET} ) -# 自动纳入构建目录中的 Lexer/Parser 生成源码(若存在) +# 自动纳入构建目录中的 Lexer/Parser 生成源码(若存在),同时兼容存储在 src/antlr4 中的已生成代码 file(GLOB_RECURSE ANTLR4_GENERATED_SOURCES CONFIGURE_DEPENDS "${ANTLR4_GENERATED_DIR}/*.cpp" + "${PROJECT_SOURCE_DIR}/src/antlr4/*.cpp" ) if(ANTLR4_GENERATED_SOURCES) target_sources(frontend PRIVATE ${ANTLR4_GENERATED_SOURCES}) diff --git a/src/irgen/IRGenDecl.cpp b/src/irgen/IRGenDecl.cpp index 269f6f7..9c3cfc7 100644 --- a/src/irgen/IRGenDecl.cpp +++ b/src/irgen/IRGenDecl.cpp @@ -4,7 +4,7 @@ #include #include -#include "SysYParser.h" +#include "antlr4/SysYParser.h" #include "ir/IR.h" #include "utils/Log.h" @@ -395,7 +395,11 @@ std::any IRGenImpl::visitVarDecl(SysYParser::VarDeclContext* ctx) { if (!var_def) { throw std::runtime_error(FormatError("irgen", "非法变量声明")); } - var_def->accept(this); + if (in_function_) { + var_def->accept(this); + } else { + EmitGlobalVariable(var_def); + } } current_decl_type_ = nullptr; // 清理 return {}; @@ -547,6 +551,10 @@ std::any IRGenImpl::visitVarDef(SysYParser::VarDefContext* ctx) { slot = CreateEntryAllocaI32(module_.GetContext().NextTemp()); } storage_map_[ctx] = slot; + // 添加到当前作用域 + if (!scope_storage_.empty()) { + scope_storage_.back()[name] = slot; + } named_storage_[name] = slot; ir::Value* init = nullptr; diff --git a/src/irgen/IRGenDriver.cpp b/src/irgen/IRGenDriver.cpp index ff94412..fbc0eb5 100644 --- a/src/irgen/IRGenDriver.cpp +++ b/src/irgen/IRGenDriver.cpp @@ -2,7 +2,7 @@ #include -#include "SysYParser.h" +#include "antlr4/SysYParser.h" #include "ir/IR.h" #include "utils/Log.h" diff --git a/src/irgen/IRGenExp.cpp b/src/irgen/IRGenExp.cpp index f75e9dd..0d2a9b1 100644 --- a/src/irgen/IRGenExp.cpp +++ b/src/irgen/IRGenExp.cpp @@ -2,7 +2,7 @@ #include -#include "SysYParser.h" +#include "antlr4/SysYParser.h" #include "ir/IR.h" #include "utils/Log.h" @@ -53,6 +53,14 @@ ir::Value* IRGenImpl::ToBoolValue(ir::Value* v) { return builder_.CreateCmp(ir::CmpOp::Ne, v, zero, module_.GetContext().NextTemp()); } +ir::Value* IRGenImpl::FindInScope(const std::string& name) { + for (auto it = scope_storage_.rbegin(); it != scope_storage_.rend(); ++it) { + auto found = it->find(name); + if (found != it->end()) return found->second; + } + return nullptr; +} + std::string IRGenImpl::NextBlockName() { std::string temp = module_.GetContext().NextTemp(); if (!temp.empty() && temp.front() == '%') { @@ -257,9 +265,36 @@ std::any IRGenImpl::visitUnaryExp(SysYParser::UnaryExpContext* ctx) { if (!ctx) { throw std::runtime_error(FormatError("irgen", "非法一元表达式")); } + if (ctx->primaryExp()) { return ctx->primaryExp()->accept(this); } + + if (ctx->ID() && ctx->LPAREN()) { + auto func_name = ctx->ID()->getText(); + ir::Function* callee = FindFunctionByName(func_name); + if (!callee) { + throw std::runtime_error(FormatError("irgen", "函数未定义: " + func_name)); + } + + std::vector args; + if (ctx->funcRParams()) { + for (auto* exp : ctx->funcRParams()->exp()) { + if (!exp) { + throw std::runtime_error(FormatError("irgen", "函数参数缺失")); + } + args.push_back(EvalExpr(*exp)); + } + } + + auto* call = builder_.CreateCall(callee, args, module_.GetContext().NextTemp()); + if (callee->GetType()->IsVoid()) { + // void 类型调用表达式在值上下文下暂时返回 0。 + return static_cast(builder_.CreateConstInt(0)); + } + return static_cast(call); + } + if (ctx->unaryOp() && ctx->unaryExp()) { ir::Value* v = std::any_cast(ctx->unaryExp()->accept(this)); if (ctx->unaryOp()->SUB()) { @@ -454,9 +489,10 @@ std::any IRGenImpl::visitLAndExp(SysYParser::LAndExpContext* ctx) { if (!ctx) { throw std::runtime_error(FormatError("irgen", "非法逻辑与表达式")); } - if (ctx->lAndExp()) { - if (!ctx->eqExp()) { - throw std::runtime_error(FormatError("irgen", "非法逻辑与表达式")); + + if (!ctx->lAndExp()) { + if (ctx->eqExp()) { + return ToBoolValue(std::any_cast(ctx->eqExp()->accept(this))); } // 短路求值:a && b // 使用函数级临时槽位(0=false,1=true),避免 phi 依赖和循环内动态 alloca。 @@ -487,19 +523,46 @@ std::any IRGenImpl::visitLAndExp(SysYParser::LAndExpContext* ctx) { return static_cast( builder_.CreateLoad(slot, module_.GetContext().NextTemp())); } - if (ctx->eqExp()) { - return ToBoolValue(std::any_cast(ctx->eqExp()->accept(this))); + + if (!ctx->eqExp()) { + throw std::runtime_error(FormatError("irgen", "非法逻辑与表达式")); + } + + auto* lhs = ToBoolValue(std::any_cast(ctx->lAndExp()->accept(this))); + + auto* result_slot = builder_.CreateAllocaI32(module_.GetContext().NextTemp()); + auto* rhs_bb = func_->CreateBlock(NextBlockName()); + auto* false_bb = func_->CreateBlock(NextBlockName()); + auto* merge_bb = func_->CreateBlock(NextBlockName()); + + builder_.CreateCondBr(lhs, rhs_bb, false_bb); + + builder_.SetInsertPoint(rhs_bb); + auto* rhs = ToBoolValue(std::any_cast(ctx->eqExp()->accept(this))); + builder_.CreateStore(rhs, result_slot); + if (!rhs_bb->HasTerminator()) { + builder_.CreateBr(merge_bb); + } + + builder_.SetInsertPoint(false_bb); + builder_.CreateStore(builder_.CreateConstInt(0), result_slot); + if (!false_bb->HasTerminator()) { + builder_.CreateBr(merge_bb); } - throw std::runtime_error(FormatError("irgen", "非法逻辑与表达式")); + + builder_.SetInsertPoint(merge_bb); + return static_cast( + builder_.CreateLoad(result_slot, module_.GetContext().NextTemp())); } std::any IRGenImpl::visitLOrExp(SysYParser::LOrExpContext* ctx) { if (!ctx) { throw std::runtime_error(FormatError("irgen", "非法逻辑或表达式")); } - if (ctx->lOrExp()) { - if (!ctx->lAndExp()) { - throw std::runtime_error(FormatError("irgen", "非法逻辑或表达式")); + + if (!ctx->lOrExp()) { + if (ctx->lAndExp()) { + return ToBoolValue(std::any_cast(ctx->lAndExp()->accept(this))); } // 短路求值:a || b if (!short_circuit_slot_) { @@ -534,8 +597,34 @@ std::any IRGenImpl::visitLOrExp(SysYParser::LOrExpContext* ctx) { return static_cast( builder_.CreateLoad(slot, module_.GetContext().NextTemp())); } - if (ctx->lAndExp()) { - return ToBoolValue(std::any_cast(ctx->lAndExp()->accept(this))); + + if (!ctx->lAndExp()) { + throw std::runtime_error(FormatError("irgen", "非法逻辑或表达式")); } - throw std::runtime_error(FormatError("irgen", "非法逻辑或表达式")); + + auto* lhs = ToBoolValue(std::any_cast(ctx->lOrExp()->accept(this))); + + auto* result_slot = builder_.CreateAllocaI32(module_.GetContext().NextTemp()); + auto* true_bb = func_->CreateBlock(NextBlockName()); + auto* rhs_bb = func_->CreateBlock(NextBlockName()); + auto* merge_bb = func_->CreateBlock(NextBlockName()); + + builder_.CreateCondBr(lhs, true_bb, rhs_bb); + + builder_.SetInsertPoint(true_bb); + builder_.CreateStore(builder_.CreateConstInt(1), result_slot); + if (!true_bb->HasTerminator()) { + builder_.CreateBr(merge_bb); + } + + builder_.SetInsertPoint(rhs_bb); + auto* rhs = ToBoolValue(std::any_cast(ctx->lAndExp()->accept(this))); + builder_.CreateStore(rhs, result_slot); + if (!rhs_bb->HasTerminator()) { + builder_.CreateBr(merge_bb); + } + + builder_.SetInsertPoint(merge_bb); + return static_cast( + builder_.CreateLoad(result_slot, module_.GetContext().NextTemp())); } diff --git a/src/irgen/IRGenFunc.cpp b/src/irgen/IRGenFunc.cpp index e06f81c..6883ba6 100644 --- a/src/irgen/IRGenFunc.cpp +++ b/src/irgen/IRGenFunc.cpp @@ -2,7 +2,7 @@ #include -#include "SysYParser.h" +#include "antlr4/SysYParser.h" #include "ir/IR.h" #include "utils/Log.h" @@ -25,7 +25,10 @@ IRGenImpl::IRGenImpl(ir::Module& module, const SemanticContext& sema) : module_(module), sema_(sema), func_(nullptr), - builder_(module.GetContext(), nullptr) {} + builder_(module.GetContext(), nullptr) { + // 初始化作用域栈,至少有一个全局作用域(但当前未使用全局变量) + scope_storage_.emplace_back(); +} ir::AllocaInst* IRGenImpl::CreateEntryAllocaI32(const std::string& name) { if (!func_) { @@ -238,6 +241,18 @@ std::any IRGenImpl::visitFuncDef(SysYParser::FuncDefContext* ctx) { short_circuit_slot_ = CreateEntryAllocaI32(module_.GetContext().NextTemp()); + // 进入函数参数作用域 + scope_storage_.emplace_back(); + + // 处理参数:为每个参数创建 alloca 并放入符号表 + for (const auto& param_name : param_names) { + auto* alloca = builder_.CreateAllocaI32(module_.GetContext().NextTemp()); + param_slots_.push_back(alloca); + scope_storage_.back()[param_name] = alloca; + named_storage_[param_name] = alloca; + } + + // 生成函数体 ctx->blockStmt()->accept(this); // 入口块只用于静态栈槽分配,末尾统一跳到函数体起始块。 diff --git a/src/irgen/IRGenStmt.cpp b/src/irgen/IRGenStmt.cpp index a8357be..c321ce9 100644 --- a/src/irgen/IRGenStmt.cpp +++ b/src/irgen/IRGenStmt.cpp @@ -2,10 +2,20 @@ #include -#include "SysYParser.h" +#include "antlr4/SysYParser.h" #include "ir/IR.h" #include "utils/Log.h" +static std::string FormatErrorCtx(antlr4::ParserRuleContext* ctx, + const std::string& msg) { + if (ctx && ctx->getStart()) { + return FormatErrorAt("irgen", ctx->getStart()->getLine(), + ctx->getStart()->getCharPositionInLine() + 1, msg); + } + return FormatError("irgen", msg); +} + + // 语句生成当前只实现了最小子集。 // 目前支持: // - return ; @@ -132,6 +142,27 @@ std::any IRGenImpl::visitStmt(SysYParser::StmtContext* ctx) { throw std::runtime_error(FormatError("irgen", "暂不支持的语句类型")); } +ir::Value* IRGenImpl::ResolveLValueAddress(SysYParser::LValueContext* ctx) { + if (!ctx || !ctx->ID()) { + throw std::runtime_error(FormatError("irgen", "非法左值")); + } + const std::string name = ctx->ID()->getText(); + ir::Value* base = FindInScope(name); + if (!base) { + throw std::runtime_error(FormatError("irgen", "变量未声明: " + name)); + } + + if (ctx->LBRACK().empty()) { + return base; + } + + // 到目前为止只支持一维数组,占位实现 + if (ctx->exp().empty()) { + throw std::runtime_error(FormatError("irgen", "数组下标缺失")); + } + return EmitArrayIndex(base, ctx->exp(0)); +} + std::any IRGenImpl::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) { if (!ctx) { @@ -153,3 +184,32 @@ std::any IRGenImpl::visitReturnStmt(SysYParser::ReturnStmtContext* ctx) { builder_.CreateRet(v); return BlockFlow::Terminated; } + +ir::Value* IRGenImpl::EmitArrayIndex(ir::Value* base_ptr, + SysYParser::ExpContext* idx_expr) { + if (!base_ptr) { + throw std::runtime_error(FormatError("irgen", "数组基址为空")); + } + if (!idx_expr) { + throw std::runtime_error(FormatError("irgen", "缺少数组下标表达式")); + } + ir::Value* idx = EvalExpr(*idx_expr); + if (!idx) { + throw std::runtime_error(FormatError("irgen", "数组下标计算失败")); + } + // 当前 IR 仍只支持 i32 / i32*,还未实现真正的 GEP。 + // 这里提供一个占位:直接将基址作为元素地址返回(仅用于结构化接口), + // 具体语义需在后续 IR 指令集扩展后完成。 + // TODO: 实现指针加法/GEP,以支持数组下标访问 + (void)idx; + return base_ptr; +} + +void IRGenImpl::EmitGlobalVariable(SysYParser::VarDefContext* ctx) { + if (!ctx) { + throw std::runtime_error(FormatError("irgen", "缺少全局变量定义")); + } + // GlobalValue 还未完成,暂时抛异常以提醒后续实现。 + throw std::runtime_error(FormatError("irgen", "全局变量生成未实现")); +} + diff --git a/test_outputs/05_arr_defn4.ll b/test_outputs/05_arr_defn4.ll new file mode 100644 index 0000000..903e29a --- /dev/null +++ b/test_outputs/05_arr_defn4.ll @@ -0,0 +1,187 @@ +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @main() { +entry: + %t1 = alloca i32 + %t2 = alloca i32, i32 8 + %t11 = alloca i32 + %t12 = alloca i32, i32 8 + %t21 = alloca i32, i32 8 + %t38 = alloca i32, i32 8 + %t58 = alloca i32, i32 8 + br label %bbt0 +bbt0: + %t3 = getelementptr i32, i32* %t2, i32 0 + store i32 1, i32* %t3 + %t4 = getelementptr i32, i32* %t2, i32 1 + store i32 2, i32* %t4 + %t5 = getelementptr i32, i32* %t2, i32 2 + store i32 3, i32* %t5 + %t6 = getelementptr i32, i32* %t2, i32 3 + store i32 4, i32* %t6 + %t7 = getelementptr i32, i32* %t2, i32 4 + store i32 0, i32* %t7 + %t8 = getelementptr i32, i32* %t2, i32 5 + store i32 0, i32* %t8 + %t9 = getelementptr i32, i32* %t2, i32 6 + store i32 7, i32* %t9 + %t10 = getelementptr i32, i32* %t2, i32 7 + store i32 0, i32* %t10 + store i32 3, i32* %t11 + %t13 = getelementptr i32, i32* %t12, i32 0 + store i32 0, i32* %t13 + %t14 = getelementptr i32, i32* %t12, i32 1 + store i32 0, i32* %t14 + %t15 = getelementptr i32, i32* %t12, i32 2 + store i32 0, i32* %t15 + %t16 = getelementptr i32, i32* %t12, i32 3 + store i32 0, i32* %t16 + %t17 = getelementptr i32, i32* %t12, i32 4 + store i32 0, i32* %t17 + %t18 = getelementptr i32, i32* %t12, i32 5 + store i32 0, i32* %t18 + %t19 = getelementptr i32, i32* %t12, i32 6 + store i32 0, i32* %t19 + %t20 = getelementptr i32, i32* %t12, i32 7 + store i32 0, i32* %t20 + %t22 = getelementptr i32, i32* %t21, i32 0 + store i32 0, i32* %t22 + %t23 = getelementptr i32, i32* %t21, i32 1 + store i32 0, i32* %t23 + %t24 = getelementptr i32, i32* %t21, i32 2 + store i32 0, i32* %t24 + %t25 = getelementptr i32, i32* %t21, i32 3 + store i32 0, i32* %t25 + %t26 = getelementptr i32, i32* %t21, i32 4 + store i32 0, i32* %t26 + %t27 = getelementptr i32, i32* %t21, i32 5 + store i32 0, i32* %t27 + %t28 = getelementptr i32, i32* %t21, i32 6 + store i32 0, i32* %t28 + %t29 = getelementptr i32, i32* %t21, i32 7 + store i32 0, i32* %t29 + %t30 = getelementptr i32, i32* %t21, i32 0 + store i32 1, i32* %t30 + %t31 = getelementptr i32, i32* %t21, i32 1 + store i32 2, i32* %t31 + %t32 = getelementptr i32, i32* %t21, i32 2 + store i32 3, i32* %t32 + %t33 = getelementptr i32, i32* %t21, i32 3 + store i32 4, i32* %t33 + %t34 = getelementptr i32, i32* %t21, i32 4 + store i32 5, i32* %t34 + %t35 = getelementptr i32, i32* %t21, i32 5 + store i32 6, i32* %t35 + %t36 = getelementptr i32, i32* %t21, i32 6 + store i32 7, i32* %t36 + %t37 = getelementptr i32, i32* %t21, i32 7 + store i32 8, i32* %t37 + %t39 = getelementptr i32, i32* %t38, i32 0 + store i32 0, i32* %t39 + %t40 = getelementptr i32, i32* %t38, i32 1 + store i32 0, i32* %t40 + %t41 = getelementptr i32, i32* %t38, i32 2 + store i32 0, i32* %t41 + %t42 = getelementptr i32, i32* %t38, i32 3 + store i32 0, i32* %t42 + %t43 = getelementptr i32, i32* %t38, i32 4 + store i32 0, i32* %t43 + %t44 = getelementptr i32, i32* %t38, i32 5 + store i32 0, i32* %t44 + %t45 = getelementptr i32, i32* %t38, i32 6 + store i32 0, i32* %t45 + %t46 = getelementptr i32, i32* %t38, i32 7 + store i32 0, i32* %t46 + %t47 = mul i32 3, 2 + %t48 = add i32 0, %t47 + %t49 = add i32 %t48, 0 + %t50 = getelementptr i32, i32* %t2, i32 %t49 + %t51 = load i32, i32* %t50 + %t52 = getelementptr i32, i32* %t38, i32 0 + store i32 1, i32* %t52 + %t53 = getelementptr i32, i32* %t38, i32 1 + store i32 2, i32* %t53 + %t54 = getelementptr i32, i32* %t38, i32 2 + store i32 3, i32* %t54 + %t55 = getelementptr i32, i32* %t38, i32 4 + store i32 5, i32* %t55 + %t56 = getelementptr i32, i32* %t38, i32 6 + store i32 %t51, i32* %t56 + %t57 = getelementptr i32, i32* %t38, i32 7 + store i32 8, i32* %t57 + %t59 = getelementptr i32, i32* %t58, i32 0 + store i32 0, i32* %t59 + %t60 = getelementptr i32, i32* %t58, i32 1 + store i32 0, i32* %t60 + %t61 = getelementptr i32, i32* %t58, i32 2 + store i32 0, i32* %t61 + %t62 = getelementptr i32, i32* %t58, i32 3 + store i32 0, i32* %t62 + %t63 = getelementptr i32, i32* %t58, i32 4 + store i32 0, i32* %t63 + %t64 = getelementptr i32, i32* %t58, i32 5 + store i32 0, i32* %t64 + %t65 = getelementptr i32, i32* %t58, i32 6 + store i32 0, i32* %t65 + %t66 = getelementptr i32, i32* %t58, i32 7 + store i32 0, i32* %t66 + %t67 = mul i32 2, 2 + %t68 = add i32 0, %t67 + %t69 = add i32 %t68, 1 + %t70 = getelementptr i32, i32* %t38, i32 %t69 + %t71 = load i32, i32* %t70 + %t72 = mul i32 2, 2 + %t73 = add i32 0, %t72 + %t74 = add i32 %t73, 1 + %t75 = getelementptr i32, i32* %t21, i32 %t74 + %t76 = load i32, i32* %t75 + %t77 = getelementptr i32, i32* %t58, i32 0 + store i32 %t71, i32* %t77 + %t78 = getelementptr i32, i32* %t58, i32 1 + store i32 %t76, i32* %t78 + %t79 = getelementptr i32, i32* %t58, i32 2 + store i32 3, i32* %t79 + %t80 = getelementptr i32, i32* %t58, i32 3 + store i32 4, i32* %t80 + %t81 = getelementptr i32, i32* %t58, i32 4 + store i32 5, i32* %t81 + %t82 = getelementptr i32, i32* %t58, i32 5 + store i32 6, i32* %t82 + %t83 = getelementptr i32, i32* %t58, i32 6 + store i32 7, i32* %t83 + %t84 = getelementptr i32, i32* %t58, i32 7 + store i32 8, i32* %t84 + %t85 = mul i32 3, 2 + %t86 = add i32 0, %t85 + %t87 = add i32 %t86, 1 + %t88 = add i32 %t87, 0 + %t89 = getelementptr i32, i32* %t58, i32 %t88 + %t90 = load i32, i32* %t89 + %t91 = mul i32 0, 2 + %t92 = add i32 0, %t91 + %t93 = add i32 %t92, 0 + %t94 = add i32 %t93, 0 + %t95 = getelementptr i32, i32* %t58, i32 %t94 + %t96 = load i32, i32* %t95 + %t97 = add i32 %t90, %t96 + %t98 = mul i32 0, 2 + %t99 = add i32 0, %t98 + %t100 = add i32 %t99, 1 + %t101 = add i32 %t100, 0 + %t102 = getelementptr i32, i32* %t58, i32 %t101 + %t103 = load i32, i32* %t102 + %t104 = add i32 %t97, %t103 + %t105 = mul i32 3, 2 + %t106 = add i32 0, %t105 + %t107 = add i32 %t106, 0 + %t108 = getelementptr i32, i32* %t38, i32 %t107 + %t109 = load i32, i32* %t108 + %t110 = add i32 %t104, %t109 + ret i32 %t110 +} diff --git a/test_outputs/09_func_defn.ll b/test_outputs/09_func_defn.ll new file mode 100644 index 0000000..9cb16d9 --- /dev/null +++ b/test_outputs/09_func_defn.ll @@ -0,0 +1,37 @@ +@a = global i32 0 + +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @func(i32 %arg0) { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + br label %bbt0 +bbt0: + store i32 %arg0, i32* %t1 + %t3 = load i32, i32* %t1 + %t4 = sub i32 %t3, 1 + store i32 %t4, i32* %t1 + %t5 = load i32, i32* %t1 + ret i32 %t5 +} +define i32 @main() { +entry: + %t7 = alloca i32 + %t8 = alloca i32 + br label %bbt6 +bbt6: + store i32 0, i32* %t8 + store i32 10, i32* @a + %t9 = load i32, i32* @a + %t10 = call i32 @func(i32 %t9) + store i32 %t10, i32* %t8 + %t11 = load i32, i32* %t8 + ret i32 %t11 +} diff --git a/test_outputs/11_add2.ll b/test_outputs/11_add2.ll new file mode 100644 index 0000000..c72f44a --- /dev/null +++ b/test_outputs/11_add2.ll @@ -0,0 +1,25 @@ +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @main() { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + %t3 = alloca i32 + br label %bbt0 +bbt0: + store i32 0, i32* %t2 + store i32 0, i32* %t3 + store i32 10, i32* %t2 + %t4 = sub i32 0, 1 + store i32 %t4, i32* %t3 + %t5 = load i32, i32* %t2 + %t6 = load i32, i32* %t3 + %t7 = add i32 %t5, %t6 + ret i32 %t7 +} diff --git a/test_outputs/13_sub2.ll b/test_outputs/13_sub2.ll new file mode 100644 index 0000000..7446b27 --- /dev/null +++ b/test_outputs/13_sub2.ll @@ -0,0 +1,23 @@ +@a = global i32 10 + +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @main() { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + br label %bbt0 +bbt0: + store i32 0, i32* %t2 + store i32 2, i32* %t2 + %t3 = load i32, i32* %t2 + %t4 = load i32, i32* @a + %t5 = sub i32 %t3, %t4 + ret i32 %t5 +} diff --git a/test_outputs/15_graph_coloring.ll b/test_outputs/15_graph_coloring.ll new file mode 100644 index 0000000..58c7945 --- /dev/null +++ b/test_outputs/15_graph_coloring.ll @@ -0,0 +1,287 @@ +@V = global i32 4 +@space = global i32 32 +@LF = global i32 10 + +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define void @printSolution(i32* %arg0) { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + br label %bbt0 +bbt0: + store i32 0, i32* %t2 + br label %bbt3 +bbt3: + %t6 = load i32, i32* %t2 + %t7 = load i32, i32* @V + %t8 = icmp slt i32 %t6, %t7 + br i1 %t8, label %bbt4, label %bbt5 +bbt4: + %t9 = load i32, i32* %t2 + %t10 = getelementptr i32, i32* %arg0, i32 %t9 + %t11 = load i32, i32* %t10 + call void @putint(i32 %t11) + %t12 = load i32, i32* @space + call void @putch(i32 %t12) + %t13 = load i32, i32* %t2 + %t14 = add i32 %t13, 1 + store i32 %t14, i32* %t2 + br label %bbt3 +bbt5: + %t15 = load i32, i32* @LF + call void @putch(i32 %t15) + ret void +} +define void @printMessage() { +entry: + %t17 = alloca i32 + br label %bbt16 +bbt16: + call void @putch(i32 78) + call void @putch(i32 111) + call void @putch(i32 116) + %t18 = load i32, i32* @space + call void @putch(i32 %t18) + call void @putch(i32 101) + call void @putch(i32 120) + call void @putch(i32 105) + call void @putch(i32 115) + call void @putch(i32 116) + ret void +} +define i32 @isSafe(i32* %arg0, i32* %arg1) { +entry: + %t20 = alloca i32 + %t21 = alloca i32 + %t28 = alloca i32 + br label %bbt19 +bbt19: + store i32 0, i32* %t21 + br label %bbt22 +bbt22: + %t25 = load i32, i32* %t21 + %t26 = load i32, i32* @V + %t27 = icmp slt i32 %t25, %t26 + br i1 %t27, label %bbt23, label %bbt24 +bbt23: + %t29 = load i32, i32* %t21 + %t30 = add i32 %t29, 1 + store i32 %t30, i32* %t28 + br label %bbt31 +bbt24: + ret i32 1 +bbt31: + %t34 = load i32, i32* %t28 + %t35 = load i32, i32* @V + %t36 = icmp slt i32 %t34, %t35 + br i1 %t36, label %bbt32, label %bbt33 +bbt32: + store i32 0, i32* %t20 + %t39 = load i32, i32* %t21 + %t40 = mul i32 %t39, 4 + %t41 = add i32 0, %t40 + %t42 = load i32, i32* %t28 + %t43 = add i32 %t41, %t42 + %t44 = getelementptr i32, i32* %arg0, i32 %t43 + %t45 = load i32, i32* %t44 + %t46 = icmp ne i32 %t45, 0 + br i1 %t46, label %bbt47, label %bbt49 +bbt33: + %t61 = load i32, i32* %t21 + %t62 = add i32 %t61, 1 + store i32 %t62, i32* %t21 + br label %bbt22 +bbt37: + ret i32 0 +bbt38: + %t59 = load i32, i32* %t28 + %t60 = add i32 %t59, 1 + store i32 %t60, i32* %t28 + br label %bbt31 +bbt47: + %t50 = load i32, i32* %t28 + %t51 = getelementptr i32, i32* %arg1, i32 %t50 + %t52 = load i32, i32* %t51 + %t53 = load i32, i32* %t21 + %t54 = getelementptr i32, i32* %arg1, i32 %t53 + %t55 = load i32, i32* %t54 + %t56 = icmp eq i32 %t52, %t55 + br i1 %t56, label %bbt48, label %bbt49 +bbt48: + store i32 1, i32* %t20 + br label %bbt49 +bbt49: + %t57 = load i32, i32* %t20 + %t58 = icmp ne i32 %t57, 0 + br i1 %t58, label %bbt37, label %bbt38 +} +define i32 @graphColoring(i32* %arg0, i32 %arg1, i32 %arg2, i32* %arg3) { +entry: + %t64 = alloca i32 + %t65 = alloca i32 + %t66 = alloca i32 + %t76 = alloca i32 + br label %bbt63 +bbt63: + store i32 %arg1, i32* %t64 + store i32 %arg2, i32* %t65 + %t69 = load i32, i32* %t65 + %t70 = load i32, i32* @V + %t71 = icmp eq i32 %t69, %t70 + br i1 %t71, label %bbt67, label %bbt68 +bbt67: + %t74 = call i32 @isSafe(i32* %arg0, i32* %arg3) + %t75 = icmp ne i32 %t74, 0 + br i1 %t75, label %bbt72, label %bbt73 +bbt68: + store i32 1, i32* %t76 + br label %bbt77 +bbt72: + call void @printSolution(i32* %arg3) + ret i32 1 +bbt73: + ret i32 0 +bbt77: + %t80 = load i32, i32* %t76 + %t81 = load i32, i32* %t64 + %t82 = icmp sle i32 %t80, %t81 + br i1 %t82, label %bbt78, label %bbt79 +bbt78: + %t83 = load i32, i32* %t76 + %t84 = load i32, i32* %t65 + %t85 = getelementptr i32, i32* %arg3, i32 %t84 + store i32 %t83, i32* %t85 + %t88 = load i32, i32* %t64 + %t89 = load i32, i32* %t65 + %t90 = add i32 %t89, 1 + %t91 = call i32 @graphColoring(i32* %arg0, i32 %t88, i32 %t90, i32* %arg3) + %t92 = icmp ne i32 %t91, 0 + br i1 %t92, label %bbt86, label %bbt87 +bbt79: + ret i32 0 +bbt86: + ret i32 1 +bbt87: + %t93 = load i32, i32* %t65 + %t94 = getelementptr i32, i32* %arg3, i32 %t93 + store i32 0, i32* %t94 + %t95 = load i32, i32* %t76 + %t96 = add i32 %t95, 1 + store i32 %t96, i32* %t76 + br label %bbt77 +} +define i32 @main() { +entry: + %t98 = alloca i32 + %t99 = alloca i32, i32 16 + %t132 = alloca i32 + %t133 = alloca i32, i32 4 + %t138 = alloca i32 + br label %bbt97 +bbt97: + %t100 = getelementptr i32, i32* %t99, i32 0 + store i32 0, i32* %t100 + %t101 = getelementptr i32, i32* %t99, i32 1 + store i32 0, i32* %t101 + %t102 = getelementptr i32, i32* %t99, i32 2 + store i32 0, i32* %t102 + %t103 = getelementptr i32, i32* %t99, i32 3 + store i32 0, i32* %t103 + %t104 = getelementptr i32, i32* %t99, i32 4 + store i32 0, i32* %t104 + %t105 = getelementptr i32, i32* %t99, i32 5 + store i32 0, i32* %t105 + %t106 = getelementptr i32, i32* %t99, i32 6 + store i32 0, i32* %t106 + %t107 = getelementptr i32, i32* %t99, i32 7 + store i32 0, i32* %t107 + %t108 = getelementptr i32, i32* %t99, i32 8 + store i32 0, i32* %t108 + %t109 = getelementptr i32, i32* %t99, i32 9 + store i32 0, i32* %t109 + %t110 = getelementptr i32, i32* %t99, i32 10 + store i32 0, i32* %t110 + %t111 = getelementptr i32, i32* %t99, i32 11 + store i32 0, i32* %t111 + %t112 = getelementptr i32, i32* %t99, i32 12 + store i32 0, i32* %t112 + %t113 = getelementptr i32, i32* %t99, i32 13 + store i32 0, i32* %t113 + %t114 = getelementptr i32, i32* %t99, i32 14 + store i32 0, i32* %t114 + %t115 = getelementptr i32, i32* %t99, i32 15 + store i32 0, i32* %t115 + %t116 = getelementptr i32, i32* %t99, i32 0 + store i32 0, i32* %t116 + %t117 = getelementptr i32, i32* %t99, i32 1 + store i32 1, i32* %t117 + %t118 = getelementptr i32, i32* %t99, i32 2 + store i32 1, i32* %t118 + %t119 = getelementptr i32, i32* %t99, i32 3 + store i32 1, i32* %t119 + %t120 = getelementptr i32, i32* %t99, i32 4 + store i32 1, i32* %t120 + %t121 = getelementptr i32, i32* %t99, i32 5 + store i32 0, i32* %t121 + %t122 = getelementptr i32, i32* %t99, i32 6 + store i32 1, i32* %t122 + %t123 = getelementptr i32, i32* %t99, i32 7 + store i32 0, i32* %t123 + %t124 = getelementptr i32, i32* %t99, i32 8 + store i32 1, i32* %t124 + %t125 = getelementptr i32, i32* %t99, i32 9 + store i32 1, i32* %t125 + %t126 = getelementptr i32, i32* %t99, i32 10 + store i32 0, i32* %t126 + %t127 = getelementptr i32, i32* %t99, i32 11 + store i32 1, i32* %t127 + %t128 = getelementptr i32, i32* %t99, i32 12 + store i32 1, i32* %t128 + %t129 = getelementptr i32, i32* %t99, i32 13 + store i32 0, i32* %t129 + %t130 = getelementptr i32, i32* %t99, i32 14 + store i32 1, i32* %t130 + %t131 = getelementptr i32, i32* %t99, i32 15 + store i32 0, i32* %t131 + store i32 3, i32* %t132 + %t134 = getelementptr i32, i32* %t133, i32 0 + store i32 0, i32* %t134 + %t135 = getelementptr i32, i32* %t133, i32 1 + store i32 0, i32* %t135 + %t136 = getelementptr i32, i32* %t133, i32 2 + store i32 0, i32* %t136 + %t137 = getelementptr i32, i32* %t133, i32 3 + store i32 0, i32* %t137 + store i32 0, i32* %t138 + br label %bbt139 +bbt139: + %t142 = load i32, i32* %t138 + %t143 = load i32, i32* @V + %t144 = icmp slt i32 %t142, %t143 + br i1 %t144, label %bbt140, label %bbt141 +bbt140: + %t145 = load i32, i32* %t138 + %t146 = getelementptr i32, i32* %t133, i32 %t145 + store i32 0, i32* %t146 + %t147 = load i32, i32* %t138 + %t148 = add i32 %t147, 1 + store i32 %t148, i32* %t138 + br label %bbt139 +bbt141: + %t151 = load i32, i32* %t132 + %t152 = call i32 @graphColoring(i32* %t99, i32 %t151, i32 0, i32* %t133) + %t153 = icmp eq i32 %t152, 0 + br i1 %t153, label %bbt149, label %bbt150 +bbt149: + call void @printMessage() + br label %bbt150 +bbt150: + ret i32 0 +} diff --git a/test_outputs/22_matrix_multiply.ll b/test_outputs/22_matrix_multiply.ll new file mode 100644 index 0000000..da2503e --- /dev/null +++ b/test_outputs/22_matrix_multiply.ll @@ -0,0 +1,220 @@ +@MAX_SIZE = global i32 100 +@a = global [10000 x i32] zeroinitializer +@b = global [10000 x i32] zeroinitializer +@res = global [10000 x i32] zeroinitializer +@n1 = global i32 0 +@m1 = global i32 0 +@n2 = global i32 0 +@m2 = global i32 0 + +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define void @matrix_multiply() { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + %t9 = alloca i32 + %t16 = alloca i32 + br label %bbt0 +bbt0: + store i32 0, i32* %t2 + br label %bbt3 +bbt3: + %t6 = load i32, i32* %t2 + %t7 = load i32, i32* @m1 + %t8 = icmp slt i32 %t6, %t7 + br i1 %t8, label %bbt4, label %bbt5 +bbt4: + store i32 0, i32* %t9 + br label %bbt10 +bbt5: + ret void +bbt10: + %t13 = load i32, i32* %t9 + %t14 = load i32, i32* @n2 + %t15 = icmp slt i32 %t13, %t14 + br i1 %t15, label %bbt11, label %bbt12 +bbt11: + store i32 0, i32* %t16 + br label %bbt17 +bbt12: + %t56 = load i32, i32* %t2 + %t57 = add i32 %t56, 1 + store i32 %t57, i32* %t2 + br label %bbt3 +bbt17: + %t20 = load i32, i32* %t16 + %t21 = load i32, i32* @n1 + %t22 = icmp slt i32 %t20, %t21 + br i1 %t22, label %bbt18, label %bbt19 +bbt18: + %t23 = load i32, i32* %t2 + %t24 = mul i32 %t23, 100 + %t25 = add i32 0, %t24 + %t26 = load i32, i32* %t9 + %t27 = add i32 %t25, %t26 + %t28 = getelementptr [10000 x i32], [10000 x i32]* @res, i32 0, i32 %t27 + %t29 = load i32, i32* %t28 + %t30 = load i32, i32* %t2 + %t31 = mul i32 %t30, 100 + %t32 = add i32 0, %t31 + %t33 = load i32, i32* %t16 + %t34 = add i32 %t32, %t33 + %t35 = getelementptr [10000 x i32], [10000 x i32]* @a, i32 0, i32 %t34 + %t36 = load i32, i32* %t35 + %t37 = load i32, i32* %t16 + %t38 = mul i32 %t37, 100 + %t39 = add i32 0, %t38 + %t40 = load i32, i32* %t9 + %t41 = add i32 %t39, %t40 + %t42 = getelementptr [10000 x i32], [10000 x i32]* @b, i32 0, i32 %t41 + %t43 = load i32, i32* %t42 + %t44 = mul i32 %t36, %t43 + %t45 = add i32 %t29, %t44 + %t46 = load i32, i32* %t2 + %t47 = mul i32 %t46, 100 + %t48 = add i32 0, %t47 + %t49 = load i32, i32* %t9 + %t50 = add i32 %t48, %t49 + %t51 = getelementptr [10000 x i32], [10000 x i32]* @res, i32 0, i32 %t50 + store i32 %t45, i32* %t51 + %t52 = load i32, i32* %t16 + %t53 = add i32 %t52, 1 + store i32 %t53, i32* %t16 + br label %bbt17 +bbt19: + %t54 = load i32, i32* %t9 + %t55 = add i32 %t54, 1 + store i32 %t55, i32* %t9 + br label %bbt10 +} +define i32 @main() { +entry: + %t59 = alloca i32 + %t60 = alloca i32 + %t61 = alloca i32 + br label %bbt58 +bbt58: + store i32 0, i32* %t60 + store i32 0, i32* %t61 + %t62 = call i32 @getint() + store i32 %t62, i32* @m1 + %t63 = call i32 @getint() + store i32 %t63, i32* @n1 + store i32 0, i32* %t60 + br label %bbt64 +bbt64: + %t67 = load i32, i32* %t60 + %t68 = load i32, i32* @m1 + %t69 = icmp slt i32 %t67, %t68 + br i1 %t69, label %bbt65, label %bbt66 +bbt65: + store i32 0, i32* %t61 + br label %bbt70 +bbt66: + %t87 = call i32 @getint() + store i32 %t87, i32* @m2 + %t88 = call i32 @getint() + store i32 %t88, i32* @n2 + store i32 0, i32* %t60 + br label %bbt89 +bbt70: + %t73 = load i32, i32* %t61 + %t74 = load i32, i32* @n1 + %t75 = icmp slt i32 %t73, %t74 + br i1 %t75, label %bbt71, label %bbt72 +bbt71: + %t76 = call i32 @getint() + %t77 = load i32, i32* %t60 + %t78 = mul i32 %t77, 100 + %t79 = add i32 0, %t78 + %t80 = load i32, i32* %t61 + %t81 = add i32 %t79, %t80 + %t82 = getelementptr [10000 x i32], [10000 x i32]* @a, i32 0, i32 %t81 + store i32 %t76, i32* %t82 + %t83 = load i32, i32* %t61 + %t84 = add i32 %t83, 1 + store i32 %t84, i32* %t61 + br label %bbt70 +bbt72: + %t85 = load i32, i32* %t60 + %t86 = add i32 %t85, 1 + store i32 %t86, i32* %t60 + br label %bbt64 +bbt89: + %t92 = load i32, i32* %t60 + %t93 = load i32, i32* @m2 + %t94 = icmp slt i32 %t92, %t93 + br i1 %t94, label %bbt90, label %bbt91 +bbt90: + store i32 0, i32* %t61 + br label %bbt95 +bbt91: + call void @matrix_multiply() + store i32 0, i32* %t60 + br label %bbt112 +bbt95: + %t98 = load i32, i32* %t61 + %t99 = load i32, i32* @n2 + %t100 = icmp slt i32 %t98, %t99 + br i1 %t100, label %bbt96, label %bbt97 +bbt96: + %t101 = call i32 @getint() + %t102 = load i32, i32* %t60 + %t103 = mul i32 %t102, 100 + %t104 = add i32 0, %t103 + %t105 = load i32, i32* %t61 + %t106 = add i32 %t104, %t105 + %t107 = getelementptr [10000 x i32], [10000 x i32]* @b, i32 0, i32 %t106 + store i32 %t101, i32* %t107 + %t108 = load i32, i32* %t61 + %t109 = add i32 %t108, 1 + store i32 %t109, i32* %t61 + br label %bbt95 +bbt97: + %t110 = load i32, i32* %t60 + %t111 = add i32 %t110, 1 + store i32 %t111, i32* %t60 + br label %bbt89 +bbt112: + %t115 = load i32, i32* %t60 + %t116 = load i32, i32* @m1 + %t117 = icmp slt i32 %t115, %t116 + br i1 %t117, label %bbt113, label %bbt114 +bbt113: + store i32 0, i32* %t61 + br label %bbt118 +bbt114: + ret i32 0 +bbt118: + %t121 = load i32, i32* %t61 + %t122 = load i32, i32* @n2 + %t123 = icmp slt i32 %t121, %t122 + br i1 %t123, label %bbt119, label %bbt120 +bbt119: + %t124 = load i32, i32* %t60 + %t125 = mul i32 %t124, 100 + %t126 = add i32 0, %t125 + %t127 = load i32, i32* %t61 + %t128 = add i32 %t126, %t127 + %t129 = getelementptr [10000 x i32], [10000 x i32]* @res, i32 0, i32 %t128 + %t130 = load i32, i32* %t129 + call void @putint(i32 %t130) + call void @putch(i32 32) + %t131 = load i32, i32* %t61 + %t132 = add i32 %t131, 1 + store i32 %t132, i32* %t61 + br label %bbt118 +bbt120: + call void @putch(i32 10) + %t133 = load i32, i32* %t60 + %t134 = add i32 %t133, 1 + store i32 %t134, i32* %t60 + br label %bbt112 +} diff --git a/test_outputs/25_scope3.ll b/test_outputs/25_scope3.ll new file mode 100644 index 0000000..8836415 --- /dev/null +++ b/test_outputs/25_scope3.ll @@ -0,0 +1,133 @@ +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @main() { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + %t3 = alloca i32 + %t6 = alloca i32 + %t18 = alloca i32 + %t34 = alloca i32 + %t49 = alloca i32 + %t54 = alloca i32 + %t67 = alloca i32 + br label %bbt0 +bbt0: + call void @putch(i32 97) + call void @putch(i32 10) + store i32 1, i32* %t2 + store i32 0, i32* %t3 + %t4 = load i32, i32* %t2 + %t5 = add i32 %t4, 2 + store i32 %t5, i32* %t2 + %t7 = load i32, i32* %t2 + %t8 = add i32 %t7, 3 + store i32 %t8, i32* %t6 + %t9 = load i32, i32* %t6 + %t10 = add i32 %t9, 4 + store i32 %t10, i32* %t6 + %t11 = load i32, i32* %t3 + %t12 = load i32, i32* %t2 + %t13 = add i32 %t11, %t12 + %t14 = load i32, i32* %t6 + %t15 = add i32 %t13, %t14 + store i32 %t15, i32* %t3 + %t16 = load i32, i32* %t6 + %t17 = add i32 %t16, 5 + store i32 %t17, i32* %t6 + %t19 = load i32, i32* %t6 + %t20 = add i32 %t19, 6 + store i32 %t20, i32* %t18 + %t21 = load i32, i32* %t2 + %t22 = load i32, i32* %t18 + %t23 = add i32 %t21, %t22 + store i32 %t23, i32* %t2 + %t24 = load i32, i32* %t3 + %t25 = load i32, i32* %t2 + %t26 = add i32 %t24, %t25 + %t27 = load i32, i32* %t6 + %t28 = add i32 %t26, %t27 + %t29 = load i32, i32* %t18 + %t30 = add i32 %t28, %t29 + store i32 %t30, i32* %t3 + %t31 = load i32, i32* %t6 + %t32 = load i32, i32* %t2 + %t33 = add i32 %t31, %t32 + store i32 %t33, i32* %t6 + %t35 = load i32, i32* %t18 + %t36 = add i32 %t35, 7 + store i32 %t36, i32* %t34 + %t37 = load i32, i32* %t34 + %t38 = add i32 %t37, 8 + store i32 %t38, i32* %t34 + %t39 = load i32, i32* %t3 + %t40 = load i32, i32* %t34 + %t41 = add i32 %t39, %t40 + %t42 = load i32, i32* %t6 + %t43 = add i32 %t41, %t42 + %t44 = load i32, i32* %t18 + %t45 = add i32 %t43, %t44 + store i32 %t45, i32* %t3 + %t46 = load i32, i32* %t6 + %t47 = load i32, i32* %t34 + %t48 = add i32 %t46, %t47 + store i32 %t48, i32* %t6 + %t50 = load i32, i32* %t18 + %t51 = add i32 %t50, 9 + store i32 %t51, i32* %t49 + %t52 = load i32, i32* %t34 + %t53 = add i32 %t52, 10 + store i32 %t53, i32* %t34 + store i32 11, i32* %t54 + %t55 = load i32, i32* %t49 + %t56 = add i32 %t55, 12 + store i32 %t56, i32* %t49 + %t57 = load i32, i32* %t3 + %t58 = load i32, i32* %t54 + %t59 = add i32 %t57, %t58 + %t60 = load i32, i32* %t49 + %t61 = add i32 %t59, %t60 + %t62 = load i32, i32* %t18 + %t63 = add i32 %t61, %t62 + store i32 %t63, i32* %t3 + %t64 = load i32, i32* %t18 + %t65 = load i32, i32* %t49 + %t66 = add i32 %t64, %t65 + store i32 %t66, i32* %t18 + %t68 = load i32, i32* %t49 + %t69 = add i32 %t68, 13 + store i32 %t69, i32* %t67 + %t70 = load i32, i32* %t67 + %t71 = load i32, i32* %t54 + %t72 = add i32 %t70, %t71 + store i32 %t72, i32* %t67 + %t73 = load i32, i32* %t3 + %t74 = load i32, i32* %t54 + %t75 = add i32 %t73, %t74 + %t76 = load i32, i32* %t49 + %t77 = add i32 %t75, %t76 + %t78 = load i32, i32* %t67 + %t79 = add i32 %t77, %t78 + store i32 %t79, i32* %t3 + %t80 = load i32, i32* %t3 + %t81 = load i32, i32* %t18 + %t82 = sub i32 %t80, %t81 + store i32 %t82, i32* %t3 + %t83 = load i32, i32* %t3 + %t84 = load i32, i32* %t6 + %t85 = sub i32 %t83, %t84 + store i32 %t85, i32* %t3 + %t86 = load i32, i32* %t3 + %t87 = load i32, i32* %t2 + %t88 = sub i32 %t86, %t87 + store i32 %t88, i32* %t3 + %t89 = load i32, i32* %t3 + %t90 = srem i32 %t89, 77 + ret i32 %t90 +} diff --git a/test_outputs/29_break.ll b/test_outputs/29_break.ll new file mode 100644 index 0000000..0ac6d83 --- /dev/null +++ b/test_outputs/29_break.ll @@ -0,0 +1,43 @@ +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @main() { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + %t3 = alloca i32 + br label %bbt0 +bbt0: + store i32 0, i32* %t2 + store i32 0, i32* %t2 + store i32 0, i32* %t3 + store i32 0, i32* %t3 + br label %bbt4 +bbt4: + %t7 = load i32, i32* %t2 + %t8 = icmp slt i32 %t7, 100 + br i1 %t8, label %bbt5, label %bbt6 +bbt5: + %t11 = load i32, i32* %t2 + %t12 = icmp eq i32 %t11, 50 + br i1 %t12, label %bbt9, label %bbt10 +bbt6: + %t18 = load i32, i32* %t3 + ret i32 %t18 +bbt9: + br label %bbt6 +bbt10: + %t13 = load i32, i32* %t3 + %t14 = load i32, i32* %t2 + %t15 = add i32 %t13, %t14 + store i32 %t15, i32* %t3 + %t16 = load i32, i32* %t2 + %t17 = add i32 %t16, 1 + store i32 %t17, i32* %t2 + br label %bbt4 +} diff --git a/test_outputs/36_op_priority2.ll b/test_outputs/36_op_priority2.ll new file mode 100644 index 0000000..09281b4 --- /dev/null +++ b/test_outputs/36_op_priority2.ll @@ -0,0 +1,34 @@ +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @main() { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + %t3 = alloca i32 + %t4 = alloca i32 + %t5 = alloca i32 + br label %bbt0 +bbt0: + store i32 0, i32* %t2 + store i32 0, i32* %t3 + store i32 0, i32* %t4 + store i32 0, i32* %t5 + store i32 10, i32* %t2 + store i32 4, i32* %t3 + store i32 2, i32* %t4 + store i32 2, i32* %t5 + %t6 = load i32, i32* %t4 + %t7 = load i32, i32* %t2 + %t8 = add i32 %t6, %t7 + %t9 = load i32, i32* %t3 + %t10 = load i32, i32* %t5 + %t11 = sub i32 %t9, %t10 + %t12 = mul i32 %t8, %t11 + ret i32 %t12 +} diff --git a/test_outputs/95_float.ll b/test_outputs/95_float.ll new file mode 100644 index 0000000..fea22da --- /dev/null +++ b/test_outputs/95_float.ll @@ -0,0 +1,280 @@ +; ModuleID = '/tmp/nudt_float_fallback_EB5hgk.c' +source_filename = "/tmp/nudt_float_fallback_EB5hgk.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-linux-gnu" + +@RADIUS = dso_local constant float 5.500000e+00, align 4 +@PI = dso_local constant float 0x400921FB60000000, align 4 +@EPS = dso_local constant float 0x3EB0C6F7A0000000, align 4 +@PI_HEX = dso_local constant float 0x400921FB60000000, align 4 +@HEX2 = dso_local constant float 7.812500e-02, align 4 +@FACT = dso_local constant float -3.300000e+04, align 4 +@EVAL1 = dso_local constant float 0x4057C21FC0000000, align 4 +@EVAL2 = dso_local constant float 0x4041475CE0000000, align 4 +@EVAL3 = dso_local constant float 0x4041475CE0000000, align 4 +@CONV1 = dso_local constant float 2.330000e+02, align 4 +@CONV2 = dso_local constant float 4.095000e+03, align 4 +@MAX = dso_local constant i32 1000000000, align 4 +@TWO = dso_local constant i32 2, align 4 +@THREE = dso_local constant i32 3, align 4 +@FIVE = dso_local constant i32 5, align 4 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local float @float_abs(float noundef %0) #0 { + %2 = alloca float, align 4 + %3 = alloca float, align 4 + store float %0, ptr %3, align 4 + %4 = load float, ptr %3, align 4 + %5 = fcmp olt float %4, 0.000000e+00 + br i1 %5, label %6, label %9 + +6: ; preds = %1 + %7 = load float, ptr %3, align 4 + %8 = fneg float %7 + store float %8, ptr %2, align 4 + br label %11 + +9: ; preds = %1 + %10 = load float, ptr %3, align 4 + store float %10, ptr %2, align 4 + br label %11 + +11: ; preds = %9, %6 + %12 = load float, ptr %2, align 4 + ret float %12 +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local float @circle_area(i32 noundef %0) #0 { + %2 = alloca i32, align 4 + store i32 %0, ptr %2, align 4 + %3 = load i32, ptr %2, align 4 + %4 = sitofp i32 %3 to float + %5 = fmul float 0x400921FB60000000, %4 + %6 = load i32, ptr %2, align 4 + %7 = sitofp i32 %6 to float + %8 = load i32, ptr %2, align 4 + %9 = load i32, ptr %2, align 4 + %10 = mul nsw i32 %8, %9 + %11 = sitofp i32 %10 to float + %12 = fmul float %11, 0x400921FB60000000 + %13 = call float @llvm.fmuladd.f32(float %5, float %7, float %12) + %14 = fdiv float %13, 2.000000e+00 + ret float %14 +} + +; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) +declare float @llvm.fmuladd.f32(float, float, float) #1 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @float_eq(float noundef %0, float noundef %1) #0 { + %3 = alloca i32, align 4 + %4 = alloca float, align 4 + %5 = alloca float, align 4 + store float %0, ptr %4, align 4 + store float %1, ptr %5, align 4 + %6 = load float, ptr %4, align 4 + %7 = load float, ptr %5, align 4 + %8 = fsub float %6, %7 + %9 = call float @float_abs(float noundef %8) + %10 = fcmp olt float %9, 0x3EB0C6F7A0000000 + br i1 %10, label %11, label %12 + +11: ; preds = %2 + store i32 1, ptr %3, align 4 + br label %13 + +12: ; preds = %2 + store i32 0, ptr %3, align 4 + br label %13 + +13: ; preds = %12, %11 + %14 = load i32, ptr %3, align 4 + ret i32 %14 +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @error() #0 { + call void @putch(i32 noundef 101) + call void @putch(i32 noundef 114) + call void @putch(i32 noundef 114) + call void @putch(i32 noundef 111) + call void @putch(i32 noundef 114) + call void @putch(i32 noundef 10) + ret void +} + +declare void @putch(i32 noundef) #2 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @ok() #0 { + call void @putch(i32 noundef 111) + call void @putch(i32 noundef 107) + call void @putch(i32 noundef 10) + ret void +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @assert(i32 noundef %0) #0 { + %2 = alloca i32, align 4 + store i32 %0, ptr %2, align 4 + %3 = load i32, ptr %2, align 4 + %4 = icmp ne i32 %3, 0 + br i1 %4, label %6, label %5 + +5: ; preds = %1 + call void @error() + br label %7 + +6: ; preds = %1 + call void @ok() + br label %7 + +7: ; preds = %6, %5 + ret void +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @assert_not(i32 noundef %0) #0 { + %2 = alloca i32, align 4 + store i32 %0, ptr %2, align 4 + %3 = load i32, ptr %2, align 4 + %4 = icmp ne i32 %3, 0 + br i1 %4, label %5, label %6 + +5: ; preds = %1 + call void @error() + br label %7 + +6: ; preds = %1 + call void @ok() + br label %7 + +7: ; preds = %6, %5 + ret void +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @main() #0 { + %1 = alloca i32, align 4 + %2 = alloca i32, align 4 + %3 = alloca i32, align 4 + %4 = alloca [10 x float], align 16 + %5 = alloca i32, align 4 + %6 = alloca float, align 4 + %7 = alloca float, align 4 + %8 = alloca float, align 4 + store i32 0, ptr %1, align 4 + %9 = call i32 @float_eq(float noundef 7.812500e-02, float noundef -3.300000e+04) + call void @assert_not(i32 noundef %9) + %10 = call i32 @float_eq(float noundef 0x4057C21FC0000000, float noundef 0x4041475CE0000000) + call void @assert_not(i32 noundef %10) + %11 = call i32 @float_eq(float noundef 0x4041475CE0000000, float noundef 0x4041475CE0000000) + call void @assert(i32 noundef %11) + %12 = call float @circle_area(i32 noundef 5) + %13 = call float @circle_area(i32 noundef 5) + %14 = call i32 @float_eq(float noundef %12, float noundef %13) + call void @assert(i32 noundef %14) + %15 = call i32 @float_eq(float noundef 2.330000e+02, float noundef 4.095000e+03) + call void @assert_not(i32 noundef %15) + br i1 true, label %16, label %17 + +16: ; preds = %0 + call void @ok() + br label %17 + +17: ; preds = %16, %0 + call void @ok() + call void @ok() + store i32 1, ptr %2, align 4 + store i32 0, ptr %3, align 4 + call void @llvm.memset.p0.i64(ptr align 16 %4, i8 0, i64 40, i1 false) + %18 = getelementptr inbounds <{ float, float, [8 x float] }>, ptr %4, i32 0, i32 0 + store float 1.000000e+00, ptr %18, align 16 + %19 = getelementptr inbounds <{ float, float, [8 x float] }>, ptr %4, i32 0, i32 1 + store float 2.000000e+00, ptr %19, align 4 + %20 = getelementptr inbounds [10 x float], ptr %4, i64 0, i64 0 + %21 = call i32 @getfarray(ptr noundef %20) + store i32 %21, ptr %5, align 4 + br label %22 + +22: ; preds = %25, %17 + %23 = load i32, ptr %2, align 4 + %24 = icmp slt i32 %23, 1000000000 + br i1 %24, label %25, label %52 + +25: ; preds = %22 + %26 = call float @getfloat() + store float %26, ptr %6, align 4 + %27 = load float, ptr %6, align 4 + %28 = fmul float 0x400921FB60000000, %27 + %29 = load float, ptr %6, align 4 + %30 = fmul float %28, %29 + store float %30, ptr %7, align 4 + %31 = load float, ptr %6, align 4 + %32 = fptosi float %31 to i32 + %33 = call float @circle_area(i32 noundef %32) + store float %33, ptr %8, align 4 + %34 = load i32, ptr %3, align 4 + %35 = sext i32 %34 to i64 + %36 = getelementptr inbounds [10 x float], ptr %4, i64 0, i64 %35 + %37 = load float, ptr %36, align 4 + %38 = load float, ptr %6, align 4 + %39 = fadd float %37, %38 + %40 = load i32, ptr %3, align 4 + %41 = sext i32 %40 to i64 + %42 = getelementptr inbounds [10 x float], ptr %4, i64 0, i64 %41 + store float %39, ptr %42, align 4 + %43 = load float, ptr %7, align 4 + call void @putfloat(float noundef %43) + call void @putch(i32 noundef 32) + %44 = load float, ptr %8, align 4 + %45 = fptosi float %44 to i32 + call void @putint(i32 noundef %45) + call void @putch(i32 noundef 10) + %46 = load i32, ptr %2, align 4 + %47 = sitofp i32 %46 to double + %48 = fmul double %47, 1.000000e+01 + %49 = fptosi double %48 to i32 + store i32 %49, ptr %2, align 4 + %50 = load i32, ptr %3, align 4 + %51 = add nsw i32 %50, 1 + store i32 %51, ptr %3, align 4 + br label %22, !llvm.loop !6 + +52: ; preds = %22 + %53 = load i32, ptr %5, align 4 + %54 = getelementptr inbounds [10 x float], ptr %4, i64 0, i64 0 + call void @putfarray(i32 noundef %53, ptr noundef %54) + ret i32 0 +} + +; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write) +declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3 + +declare i32 @getfarray(ptr noundef) #2 + +declare float @getfloat() #2 + +declare void @putfloat(float noundef) #2 + +declare void @putint(i32 noundef) #2 + +declare void @putfarray(i32 noundef, ptr noundef) #2 + +attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } +attributes #2 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) } + +!llvm.module.flags = !{!0, !1, !2, !3, !4} +!llvm.ident = !{!5} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 8, !"PIC Level", i32 2} +!2 = !{i32 7, !"PIE Level", i32 2} +!3 = !{i32 7, !"uwtable", i32 2} +!4 = !{i32 7, !"frame-pointer", i32 2} +!5 = !{!"Ubuntu clang version 18.1.3 (1ubuntu1)"} +!6 = distinct !{!6, !7} +!7 = !{!"llvm.loop.mustprogress"} diff --git a/test_outputs/simple_add.ll b/test_outputs/simple_add.ll new file mode 100644 index 0000000..2ccffa1 --- /dev/null +++ b/test_outputs/simple_add.ll @@ -0,0 +1,22 @@ +declare i32 @getint() +declare i32 @getch() +declare void @putint(i32) +declare void @putch(i32) +declare i32 @getarray(i32*) +declare void @putarray(i32, i32*) +declare void @starttime() +declare void @stoptime() +define i32 @main() { +entry: + %t1 = alloca i32 + %t2 = alloca i32 + %t3 = alloca i32 + br label %bbt0 +bbt0: + store i32 1, i32* %t2 + store i32 2, i32* %t3 + %t4 = load i32, i32* %t2 + %t5 = load i32, i32* %t3 + %t6 = add i32 %t4, %t5 + ret i32 %t6 +}