From a44f1246dbe24fc813a212a5ef3f5811b1d6402a Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 26 Jul 2026 17:42:08 +0000 Subject: feat: whole arrays (and records) as FUNCTION/PROCEDURE parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 with field writes through the parameter, bounds-contract mismatches at compile time and run time. Co-Authored-By: Claude Fable 5 --- include/cps/ArrayHandler.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'include/cps/ArrayHandler.h') diff --git a/include/cps/ArrayHandler.h b/include/cps/ArrayHandler.h index e4ccc51..f555ee9 100644 --- a/include/cps/ArrayHandler.h +++ b/include/cps/ArrayHandler.h @@ -40,6 +40,13 @@ class ArrayHandler { llvm::Value *getArrayBasePointer(const std::string &Name); llvm::Value *getElementPointer(const std::string &Name, llvm::Value *Offset); const ArrayMetadata *getMetadata(const std::string &Name) const; + // Shared bounds math: DimSize_i = (Upper_i - Lower_i) + 1, row-major + // multipliers back-to-front, TotalElements = product of the sizes. + void computeDimsAndMultipliers(const std::string &Name, + const std::vector &Lows, + const std::vector &Highs, + std::vector &Multipliers, + llvm::Value *&TotalElements); bool emitCheckedIndices(const std::string &Name, const std::vector> &IndexExprs, int Line, @@ -83,6 +90,30 @@ public: // Swap in a fresh table (function bodies get their own scope, mirroring // the Symbols save/clear/restore discipline); returns the previous table. std::map exchangeTable(std::map NewTable); + + // Callee side of an array parameter: rebuild metadata from the incoming + // data pointer + per-dim bounds. MakeCopy (BYVAL) mallocs and memcpys the + // whole buffer so callee writes stay invisible to the caller. + void bindArrayParameter(const std::string &Name, + const std::string &ElemTypeName, + llvm::Value *DataPtr, + const std::vector &LBs, + const std::vector &UBs, + bool MakeCopy, + CodeGen &CG); + + // Call side: validate a whole-array argument against the declared + // parameter (element type and rank exactly; bounded params check their + // bounds at compile time when constant, else at run time) and append the + // data pointer plus per-dim bounds to Out. + bool emitArrayArgument(ExprAST *ArgExpr, + const std::string &ElemTypeName, + int Rank, + const std::vector> &DeclaredBounds, + const std::string &Callee, + int Line, + CodeGen &CG, + std::vector &Out); }; } // namespace cps -- cgit v1.3.1-4-g156e