From 4a25b279d8f6c662fa8152cfe42e038d1ea06901 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 26 Jul 2026 12:07:51 +0000 Subject: refactor: keep pseudocode signatures, single call-marshalling path emitPrototype now records each FUNCTION/PROCEDURE's pseudocode signature (FuncSig: return type + per-param type/BYREF flag). CodeGen:: marshalCallArgs replaces the two verbatim-duplicated marshalling blocks and dispatches on the declared signature instead of inferring BYREF from pointer-typed LLVM params: BYVAL STRING params now receive the string data pointer (no more runtime garbage), and BYVAL args are coerced to the declared type (INTEGER->REAL args no longer emit invalid IR). Top-level prototypes are pre-registered in compile(), so calls may precede definitions; GenerateCall no longer fabricates i64 externs for unknown names - an unregistered callee is a compile error. OUTPUT reports an error when it cannot infer the expression type instead of printing with the INTEGER format. Co-Authored-By: Claude Fable 5 --- lib/CodeGen/FunctionGen.cc | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'lib/CodeGen/FunctionGen.cc') diff --git a/lib/CodeGen/FunctionGen.cc b/lib/CodeGen/FunctionGen.cc index 0a22278..57454b1 100644 --- a/lib/CodeGen/FunctionGen.cc +++ b/lib/CodeGen/FunctionGen.cc @@ -41,7 +41,15 @@ void FunctionGen::createArgumentAllocas(Function *F, const std::vectorsecond; +} + Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { + FuncSig Sig; + Sig.ReturnTypeName = Proto->getReturnType(); + std::vector ArgTypes; for (const auto &Arg : Proto->getArgs()) { Type *T = getLLVMType(std::get<1>(Arg)); @@ -49,7 +57,9 @@ Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { T = T->getPointerTo(); } ArgTypes.push_back(T); + Sig.Params.emplace_back(std::get<1>(Arg), std::get<2>(Arg)); } + Signatures[Proto->getName()] = std::move(Sig); Type *RetType = getLLVMType(Proto->getReturnType()); FunctionType *FT = FunctionType::get(RetType, ArgTypes, false); @@ -112,20 +122,12 @@ Function *FunctionGen::emitFunctionDef(FunctionDefAST *FuncAST, static Value *GenerateCall(llvm::Module &Module, llvm::IRBuilder<> &Builder, - llvm::LLVMContext &Context, const std::string &CalleeName, const std::vector &Args, bool &HadError) { Function *CalleeF = Module.getFunction(CalleeName); if (!CalleeF) { - std::vector ArgTypes; - for (auto *Val : Args) ArgTypes.push_back(Val->getType()); - FunctionType *FT = FunctionType::get(Type::getInt64Ty(Context), ArgTypes, false); - CalleeF = Function::Create(FT, Function::ExternalLinkage, CalleeName, &Module); - } - - if (CalleeF->arg_size() != Args.size()) { - fprintf(stderr, "Error: Incorrect # arguments passed to %s\n", CalleeName.c_str()); + fprintf(stderr, "Error: Call to undefined function %s\n", CalleeName.c_str()); HadError = true; return nullptr; } @@ -138,11 +140,11 @@ static Value *GenerateCall(llvm::Module &Module, } llvm::Value *FunctionGen::emitCallExpr(CallExprAST *Call, const std::vector &Args) { - return GenerateCall(Module, Builder, Context, Call->getCallee(), Args, HadError); + return GenerateCall(Module, Builder, Call->getCallee(), Args, HadError); } void FunctionGen::emitCallStmt(CallStmtAST *Call, const std::vector &Args) { - GenerateCall(Module, Builder, Context, Call->getCallee(), Args, HadError); + GenerateCall(Module, Builder, Call->getCallee(), Args, HadError); } void FunctionGen::emitReturn(ReturnStmtAST *Ret, llvm::Value *RetVal) { -- cgit v1.3.1-4-g156e