diff options
| author | _ov4 <ov4@ov4.org> | 2026-07-26 12:07:51 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-07-26 12:07:51 +0000 |
| commit | 4a25b279d8f6c662fa8152cfe42e038d1ea06901 (patch) | |
| tree | 58223f2db133a10827e0a6d0c18c33f04bbc2527 /lib/CodeGen/FunctionGen.cc | |
| parent | efc86b5c7625cf4c7074319f42f68807a25bde9d (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/FunctionGen.cc')
| -rw-r--r-- | lib/CodeGen/FunctionGen.cc | 24 |
1 files changed, 13 insertions, 11 deletions
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::vector<std::tupl } } +const FuncSig *FunctionGen::getSignature(const std::string &Name) const { + auto It = Signatures.find(Name); + return It == Signatures.end() ? nullptr : &It->second; +} + Function *FunctionGen::emitPrototype(PrototypeAST *Proto) { + FuncSig Sig; + Sig.ReturnTypeName = Proto->getReturnType(); + std::vector<Type*> 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<llvm::Value*> &Args, bool &HadError) { Function *CalleeF = Module.getFunction(CalleeName); if (!CalleeF) { - std::vector<Type*> 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<llvm::Value*> &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<llvm::Value*> &Args) { - GenerateCall(Module, Builder, Context, Call->getCallee(), Args, HadError); + GenerateCall(Module, Builder, Call->getCallee(), Args, HadError); } void FunctionGen::emitReturn(ReturnStmtAST *Ret, llvm::Value *RetVal) { |
