diff options
| author | _ov4 <ov4@ov4.org> | 2026-07-26 17:42:08 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-07-26 17:42:08 +0000 |
| commit | a44f1246dbe24fc813a212a5ef3f5811b1d6402a (patch) | |
| tree | f0894af7daa0bac56471c4dae684ec87588c055d /lib/CodeGen/CodeGen.cc | |
| parent | d65c296a0ad8d335aa076aab48a8b198fa934a2d (diff) | |
feat: whole arrays (and records) as FUNCTION/PROCEDURE parameters
- Parameter syntax lands end-to-end: ARRAY OF T (rank 1, bounds travel
with the argument) and ARRAY[l:u,...] OF T (any rank, literal bounds as
a checked contract). Element type and rank must match exactly.
- Flattened ABI: each rank-R array parameter becomes a data pointer plus
kBoundArgsPerDim i64 bounds per dimension. The callee rebuilds
ArrayMetadata from those bounds (ArrayHandler::bindArrayParameter via a
binder callback injected by CodeGen, so FunctionGen stays ignorant of
the array subsystem); the checked-indexing pipeline is reused unchanged.
- BYVAL (the CIE default) copies the whole buffer at callee entry
(malloc+memcpy, never freed — consistent with the string policy) so
callee writes stay invisible; BYREF binds the caller's buffer.
- Bounded-parameter contract: compile-time comparison when the caller's
bounds are constants, else a runtime [Fatal] check
(RuntimeCheck::emitArrayArgBoundsCheck).
- marshalCallArgs gains the array branch and a call-site line number
(CallStmtAST now carries one); whole-array-to-scalar keeps a clear
error, FUNCTION RETURNS ARRAY is a dedicated parse error.
- Verified: BYREF fill/sort-style mutation, BYVAL copy invisibility,
2-D bounded params, ARRAY OF <record> with field writes through the
parameter, bounds-contract mismatches at compile time and run time.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/CodeGen.cc')
| -rw-r--r-- | lib/CodeGen/CodeGen.cc | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index 62a6f1e..64e1b9f 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -90,6 +90,15 @@ CodeGen::CodeGen() { StrConvHandler = std::make_unique<StringConversionHandler>(*TheContext, *Builder, *TheModule); Files = std::make_unique<FileHandler>(*TheContext, *Builder, *TheModule, *RuntimeChecker); + // Array parameters are bound into ArrayHandler through this inversion so + // FunctionGen never sees the array subsystem. + FuncGen->setArrayParamBinder( + [this](const ParamDecl &P, Value *DataPtr, + const std::vector<Value*> &LBs, const std::vector<Value*> &UBs) { + Arrays->bindArrayParameter(P.Name, P.TypeName, DataPtr, LBs, UBs, + /*MakeCopy=*/P.Mode == PassMode::ByVal, *this); + }); + SetupExternalFunctions(); } @@ -714,7 +723,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { } std::vector<Value*> Args; - if (!marshalCallArgs(Name, Call->getArgs(), Args)) return nullptr; + if (!marshalCallArgs(Name, Call->getArgs(), Call->getLine(), Args)) return nullptr; return FuncGen->emitCallExpr(Call, Args); } @@ -740,6 +749,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { // BYVAL parameters are evaluated and coerced to the declared parameter type. bool CodeGen::marshalCallArgs(const std::string &Callee, const std::vector<std::unique_ptr<ExprAST>> &ArgExprs, + int Line, std::vector<Value*> &Out) { const FuncSig *Sig = FuncGen->getSignature(Callee); if (!Sig) { @@ -752,17 +762,26 @@ bool CodeGen::marshalCallArgs(const std::string &Callee, } for (size_t i = 0; i < ArgExprs.size(); ++i) { - const std::string &TypeName = Sig->Params[i].first; - bool IsByRef = Sig->Params[i].second; + const ParamSig &P = Sig->Params[i]; + const std::string &TypeName = P.TypeName; + bool IsByRef = P.IsByRef; ExprAST *ArgExpr = ArgExprs[i].get(); - // Parameters can never be arrays, so a whole-array argument is always - // a user error (and would smash the data pointer if let through). + if (P.IsArray) { + if (!Arrays->emitArrayArgument(ArgExpr, P.TypeName, P.Rank, P.DeclaredBounds, + Callee, Line, *this, Out)) { + return false; + } + continue; + } + + // A whole-array argument against a scalar parameter is always a user + // error (and would smash the data pointer if let through). if (auto *Var = dynamic_cast<VariableExprAST*>(ArgExpr)) { const SymbolInfo *Info = getSymbolInfo(Var->getName()); if (Info && Info->IsArray) { - reportError("Cannot pass whole array %s to %s; pass an element like %s[i]", - Var->getName().c_str(), Callee.c_str(), Var->getName().c_str()); + reportError("Cannot pass whole array %s to %s; the parameter is not an array (declare it ARRAY OF %s)", + Var->getName().c_str(), Callee.c_str(), TypeName.c_str()); return false; } } @@ -959,7 +978,7 @@ void CodeGen::emitStmt(StmtAST *Stmt) { if (auto *Call = dynamic_cast<CallStmtAST*>(Stmt)) { std::vector<Value*> Args; - if (!marshalCallArgs(Call->getCallee(), Call->getArgs(), Args)) return; + if (!marshalCallArgs(Call->getCallee(), Call->getArgs(), Call->getLine(), Args)) return; FuncGen->emitCallStmt(Call, Args); return; } |
