From 8bbd8f96bbc14fec922214e59787ab83bc06b068 Mon Sep 17 00:00:00 2001 From: zhm <1978583449@qq.com> Date: Wed, 13 May 2026 17:05:01 +0800 Subject: [PATCH] Fix starttime/stoptime function name and add line number parameter --- src/irgen/IRGenExp.cpp | 12 ++++++++++-- src/irgen/IRGenFunc.cpp | 10 ++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/irgen/IRGenExp.cpp b/src/irgen/IRGenExp.cpp index b7d35a72..2f94f941 100644 --- a/src/irgen/IRGenExp.cpp +++ b/src/irgen/IRGenExp.cpp @@ -655,9 +655,17 @@ std::any IRGenImpl::visitUnaryExp(SysYParser::UnaryExpContext* ctx) { args.push_back(EvalExpr(*exp)); } } + + if (callee_name == "starttime" || callee_name == "stoptime") { + int lineno = ctx->getStart()->getLine(); + args.push_back(static_cast(builder_.CreateConstInt(lineno))); + } + if (args.size() != func_it->second->GetParams().size()) { - throw std::runtime_error( - FormatError("irgen", "函数参数个数不匹配: " + callee_name)); + if (callee_name != "starttime" && callee_name != "stoptime") { + throw std::runtime_error( + FormatError("irgen", "函数参数个数不匹配: " + callee_name)); + } } for (size_t i = 0; i < args.size(); ++i) { args[i] = CastValueTo(args[i], func_it->second->GetParams()[i]->GetType()); diff --git a/src/irgen/IRGenFunc.cpp b/src/irgen/IRGenFunc.cpp index 3401c50c..9c6c1342 100644 --- a/src/irgen/IRGenFunc.cpp +++ b/src/irgen/IRGenFunc.cpp @@ -119,10 +119,12 @@ std::any IRGenImpl::visitCompUnit(SysYParser::CompUnitContext* ctx) { auto* putch = module_.CreateFunction("putch", ir::Type::GetVoidType(), true); putch->AddParam("%arg.x", ir::Type::GetInt32Type()); function_map_["putch"] = putch; - function_map_["starttime"] = - module_.CreateFunction("starttime", ir::Type::GetVoidType(), true); - function_map_["stoptime"] = - module_.CreateFunction("stoptime", ir::Type::GetVoidType(), true); + auto* sysy_starttime = module_.CreateFunction("_sysy_starttime", ir::Type::GetVoidType(), true); + sysy_starttime->AddParam("%arg.lineno", ir::Type::GetInt32Type()); + function_map_["starttime"] = sysy_starttime; + auto* sysy_stoptime = module_.CreateFunction("_sysy_stoptime", ir::Type::GetVoidType(), true); + sysy_stoptime->AddParam("%arg.lineno", ir::Type::GetInt32Type()); + function_map_["stoptime"] = sysy_stoptime; SysYParser::FuncDefContext* main_func = nullptr; for (auto* func : funcs) {