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/RuntimeCheck.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/RuntimeCheck.cc')
| -rw-r--r-- | lib/CodeGen/RuntimeCheck.cc | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/CodeGen/RuntimeCheck.cc b/lib/CodeGen/RuntimeCheck.cc index e6d4bf8..40304fd 100644 --- a/lib/CodeGen/RuntimeCheck.cc +++ b/lib/CodeGen/RuntimeCheck.cc @@ -27,6 +27,9 @@ void RuntimeCheck::setupExternalFunctions() { OutOfBoundsMsg = Builder.CreateGlobalStringPtr("[Fatal] line %d: Array index out of bounds\n", "err_bounds", 0, &TheModule); EnumRangeMsg = Builder.CreateGlobalStringPtr("[Fatal] line %d: Value out of range for enum %s\n", "err_enum_range", 0, &TheModule); NullDerefMsg = Builder.CreateGlobalStringPtr("[Fatal] line %d: Dereference of an unset pointer\n", "err_null_deref", 0, &TheModule); + ArrayArgBoundsMsg = Builder.CreateGlobalStringPtr( + "[Fatal] line %d: Array argument bounds [%lld:%lld] do not match the declared parameter bounds [%lld:%lld]\n", + "err_arr_arg_bounds", 0, &TheModule); } void RuntimeCheck::emitErrorAndExit(Value *Condition, Value *Msg, int Line) { @@ -86,3 +89,17 @@ void RuntimeCheck::emitNullDerefCheck(Value *Ptr, int Line) { "ptr_is_null"); emitErrorAndExit(IsNull, NullDerefMsg, Line); } + +void RuntimeCheck::emitArrayArgBoundsCheck(Value *ActualLower, Value *ActualUpper, + int64_t DeclaredLower, int64_t DeclaredUpper, + int Line) { + Value *DeclLo = ConstantInt::get(TheContext, APInt(64, static_cast<uint64_t>(DeclaredLower), true)); + Value *DeclHi = ConstantInt::get(TheContext, APInt(64, static_cast<uint64_t>(DeclaredUpper), true)); + Value *LoDiff = Builder.CreateICmpNE(ActualLower, DeclLo, "arg_lo_ne"); + Value *HiDiff = Builder.CreateICmpNE(ActualUpper, DeclHi, "arg_hi_ne"); + Value *Mismatch = Builder.CreateOr(LoDiff, HiDiff, "arg_bounds_ne"); + + emitErrorAndExit(Mismatch, ArrayArgBoundsMsg, + {ConstantInt::get(TheContext, APInt(32, Line)), + ActualLower, ActualUpper, DeclLo, DeclHi}); +} |
