From f5e6f11dc057d4cef59642efbcb502e002c13977 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 22 Oct 2026 03:30:26 +0000 Subject: refactor(codegen): narrow FunctionGen's call and type helpers Make getLLVMType and the link-name emitCall overload private, inline the single-use GenerateCall helper into that overload, and drop the argument setName calls in createArgumentAllocas that repeat the names emitPrototype already assigned. The undefined-function diagnostic stays: a link name can be shadowed by another module global (e.g. FUNCTION stderr). Co-Authored-By: Claude Opus 5.5 --- include/cps/FunctionGen.h | 5 ++--- lib/CodeGen/FunctionGen.cc | 18 ++++-------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/include/cps/FunctionGen.h b/include/cps/FunctionGen.h index 7bf7eca..95579c4 100644 --- a/include/cps/FunctionGen.h +++ b/include/cps/FunctionGen.h @@ -85,6 +85,8 @@ private: std::function RegisterCopy; void createArgumentAllocas(llvm::Function *F, const std::vector &Params, bool IsMethod); + llvm::Type *getLLVMType(const std::string &TypeName); + llvm::Value *emitCall(const std::string &Callee, const std::vector &Args); public: FunctionGen(llvm::LLVMContext &C, @@ -96,8 +98,6 @@ public: bool &HadErrorFlag, int &Line) : Context(C), Module(M), Builder(B), Types(TS), Exceptions(EH), Symbols(Sym), HadError(HadErrorFlag), DiagnosticLine(Line) {} - llvm::Type *getLLVMType(const std::string &TypeName); - void setArrayParamBinder(ArrayParamBinder Binder) { BindArrayParam = std::move(Binder); } void setCopyRegistrar(std::function Registrar) { @@ -117,7 +117,6 @@ public: llvm::Function *emitFunctionDef(FunctionDefAST *FuncAST, const std::function &StmtEmitter, unsigned ScopeID); - llvm::Value *emitCall(const std::string &Callee, const std::vector &Args); llvm::Value *emitCall(const FuncSig &Sig, const std::vector &Args); void emitReturn(llvm::Value *RetVal); diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc index 9c9d0f5..4a084d7 100644 --- a/lib/CodeGen/FunctionGen.cc +++ b/lib/CodeGen/FunctionGen.cc @@ -57,7 +57,6 @@ void FunctionGen::createArgumentAllocas(Function *F, const std::vectorsetName(P.Name); if (P.IsArray) { // Data pointer + kBoundArgsPerDim bounds per dimension; binding @@ -66,10 +65,8 @@ void FunctionGen::createArgumentAllocas(Function *F, const std::vectorsetName(P.Name + kLowerBoundSuffix + std::to_string(D)); Value *UB = &(*AI); ++AI; - UB->setName(P.Name + kUpperBoundSuffix + std::to_string(D)); LBs.push_back(LB); UBs.push_back(UB); } @@ -438,14 +435,11 @@ Function *FunctionGen::emitFunctionDef(FunctionDefAST *FuncAST, return TheFunction; } -static Value *GenerateCall(llvm::Module &Module, - ExceptionHandler &Exceptions, - const std::string &CalleeName, - const std::vector &Args, - bool &HadError, int DiagnosticLine) { - Function *CalleeF = Module.getFunction(CalleeName); +llvm::Value *FunctionGen::emitCall(const std::string &Callee, const std::vector &Args) { + // A link name can be taken by another module global (e.g. FUNCTION stderr). + Function *CalleeF = Module.getFunction(Callee); if (!CalleeF) { - sourceError(DiagnosticLine, "Call to undefined function %s", CalleeName.c_str()); + sourceError(DiagnosticLine, "Call to undefined function %s", Callee.c_str()); HadError = true; return nullptr; } @@ -457,10 +451,6 @@ static Value *GenerateCall(llvm::Module &Module, return Exceptions.emitCall(CalleeF, Args, "calltmp"); } -llvm::Value *FunctionGen::emitCall(const std::string &Callee, const std::vector &Args) { - return GenerateCall(Module, Exceptions, Callee, Args, HadError, DiagnosticLine); -} - llvm::Value *FunctionGen::emitCall(const FuncSig &Sig, const std::vector &Args) { if (BeforeCall) BeforeCall(Sig); if (!Sig.IsExternal) return emitCall(Sig.LinkName, Args); -- cgit v1.3.1-4-g156e