Commit graph

369 commits

Author SHA1 Message Date
Graeme Geldenhuys 61264da0bf feat(lang): add [Unretained] non-owning reference attribute
[Unretained] is an unsafe non-owning class/interface reference (like Swift's
unowned(unsafe)): no reference counting and no weak-table registration.  The
field assignment is a plain pointer store and field cleanup is a no-op for it.
Use only when the referent is guaranteed to outlive the field.

This complements [Weak] (safe, auto-nils on referent free, has weak-registry
cost).  The two are mutually exclusive on a declaration.

Pipeline:
- semantic: HasUnretainedAttribute + resolution on field decls, with
  class/interface-only and not-both-with-Weak checks; IsUnretained propagated
  to TFieldInfo.
- codegen: EmitFieldAssignment and the implicit-Self field path emit a bare
  storel for unretained class fields; EmitFieldCleanupFn skips them.

Apply [Unretained] to the compiler's own non-owning class fields — every
field documented "not owned" across uAST (ResolvedType, ResolvedMethod,
ResolvedDecl, FieldInfo back-pointers, …) and uSymbolTable (type-desc
references, FParent back-pointers, NextOverload, …).  These pointed into
the symbol table's type pool / the AST tree, which the codegen was
needlessly ARC-managing — inflating shared TTypeDesc refcounts.  Marking them
unretained de-inflates those (TTypeDesc dropped from rc=4/6 toward rc=2) at
zero per-assignment cost.

Tests: 5 new cases in cp.test.weakref.pas (semantic rejection on non-class
and on [Weak]+[Unretained]; codegen asserts no addref on store, no
_WeakAssign, no release/clear in field cleanup).  Docs: language-rationale
gains a [Weak] vs [Unretained] section with a comparison table.

Fixpoint clean; 2269 tests pass.
2026-06-02 08:41:12 +01:00
Graeme Geldenhuys 4fe8858103 fix(codegen): run finally blocks on non-local exit (Exit/Break/Continue)
A non-local exit out of a try/finally only popped the exception frame and
jumped to the function/loop exit — it never ran the finally body.  So
`try ... Exit ... finally CleanUp end` silently skipped CleanUp, and the
unbalanced frame could later crash in _PopExcFrame.

Track active try/finally bodies in FFinallyStack, aligned 1:1 with the
FExcDepth exception frames (try/except pushes nil to keep the indices in
step).  EmitExcUnwind now, for each frame it crosses, pops the frame and —
when that frame is a try/finally — emits its finally body inline, innermost
first.  This mirrors the finally emission already done on the normal and
exception paths in EmitTryFinallyStmt.

Tests: IR test asserts the finally body is emitted on the exit path
(3 occurrences: normal, exception, exit); three e2e tests cover Exit through
one finally, Exit through nested finallys (innermost first), and Break out of
a loop through a finally.  Fixpoint clean; 2264 tests pass.

Note: the compiler's own source still avoids `Exit` inside `try/finally`
because the current bootstrap release binary predates this fix and would
miscompile it.  Once a release binary carrying this fix is cut, the
free-before-Exit workarounds in EmitMethodCall and the method-call-expr path
can be replaced with proper try/finally.
2026-06-02 01:11:19 +01:00
Graeme Geldenhuys ddde3c8621 fix(arc): wire owned-arg-temp release into remaining call paths
Extend EmitOwnedArgReleases coverage to the method-call statement (both the
arbitrary-receiver ObjExpr path and the named-receiver path), the method-call
expression path, and the standalone function-call expression path.  Each now
tracks value-argument temps in a parallel list and releases the +1-owned ones
after the call, matching the constructor and procedure paths.

Avoided `Exit` inside `try/finally`: that shape miscompiles today — the Exit
branch fails to pop the exception frame and segfaults in _PopExcFrame at
runtime (logged in bugs.txt).  Free the temp list explicitly before each Exit
instead; leaking it on the fatal-exception path is acceptable.

Stage-2 self-compiles, fixpoint clean, 2260 tests pass.
2026-06-02 00:58:15 +01:00
Graeme Geldenhuys dc2ab8fe37 refactor(arc): drop redundant owned-field Free from 50 AST destructors
Swift-style ARC continued: convert every AST destructor whose body only
released owned class fields into an inherited-only destructor.  ARC field
cleanup already releases those fields; the manual Free calls merely zeroed
the slots first, so removing them is behaviour-neutral (fixpoint clean,
stage-2 self-compiles, 2260 tests pass).

TMethodDecl is deliberately left unchanged: it frees Body conditionally
(`if OwnBody then Body.Free`) because cloned generic method stubs share a
body they do not own.  That is genuine conditional ownership, not redundant
cleanup, and converting it would let field cleanup release a borrowed Body.
Tracked as a follow-up: model Body as a properly refcounted shared reference
and retire the OwnBody flag.
2026-06-02 00:50:29 +01:00
Graeme Geldenhuys dfe04a10d6 refactor(arc): drop redundant owned-field Free from symbol-table destructors
Swift-style ARC: a destructor is for side-effects, not for releasing owned
storage.  ARC field cleanup already releases every owned class/string field
(verified in the generated _FieldCleanup_TScope: it releases FSymbols and
FKeys, and _WeakClears the [Weak] FParent).  The manual FKeys.Free /
FSymbols.Free in TScope.Destroy (and the equivalents in TSymbolTable and
TSymbol) duplicated that work — they zeroed the slot so field cleanup's
release became a no-op, netting one release either way.

Remove the redundant frees, leaving the destructors as inherited-only.
Behaviour-neutral (IR-confirmed equivalent); fixpoint clean; 2260 tests pass.
This establishes the pattern; remaining destructors convert separately.
2026-06-02 00:48:41 +01:00
Graeme Geldenhuys 804d8a127e fix(arc): release +1-owned temporaries passed as value arguments
A function/property/method return passed directly as a value argument
(e.g. TScope.Create(CurrentScope), where CurrentScope reads through
GetCurrentScope and returns +1) was never released by the caller.  The
callee takes ownership via its parameter-entry AddRef and releases at scope
exit, so the caller's +1 temporary leaked once per such call.

This was the dominant source of compiler self-compilation leaks: every
PushScope does TScope.Create(CurrentScope), leaking one reference on the
parent scope.  The global scope accumulated a reference per builtin symbol,
so it never reached refcount zero and its entire subtree (scopes, symbols,
their lists) leaked.

Add EmitOwnedArgReleases: after a call, release any value-argument temp
whose expression ExprOwnsRef.  Wire it into the constructor-with-args path
and the user-defined procedure path, tracking value-arg temps in a parallel
list ('' for var/open-array/interface args that are not owned value temps).

Measured: compiling a trivial program dropped from 329 leaked objects to 30.
Fixpoint clean; 2260 tests pass.
2026-06-02 00:43:01 +01:00
Graeme Geldenhuys 5e45c50503 fix(compiler): exit --emit-ir path through normal cleanup, not Halt(0)
The --emit-ir branch in main wrote the IR and immediately called Halt(0),
which lowers to libc exit().  libc exit() runs atexit handlers (so the leak
report still prints) but unwinds no Pascal stack frame, so the main block's
scope-exit ARC releases never run — any local not freed in the finally block
leaked silently, and the leak tracker could not see it.

Restructure so --emit-ir writes the IR and falls through to the end of the
program, letting the main block's scope-exit cleanup release its locals
before the process returns normally.  The native-compile path is now the
else branch.  IR output is byte-identical (verified: fixpoint clean,
2260 tests pass).
2026-06-02 00:31:08 +01:00
Graeme Geldenhuys 5b10bdab7b fix(arc): release +1-owned receiver temporaries after method calls
When a method is invoked on a receiver that is itself a function or
method-backed property return (e.g. CurrentScope.Define(Sym), where
CurrentScope reads through GetCurrentScope), the receiver evaluates to a
+1-owned temporary.  The call consumed the value but nothing released it,
so each such call leaked one reference on the returned object.

ExprOwnsRef now recognises method-backed property reads (PropRead with a
non-empty ReadMethod return +1; field-backed reads do not).  Both the
method-call statement path (EmitMethodCall) and the method-call expression
path (EmitExpr) now release the receiver temporary after the call when
ExprOwnsRef(ObjExpr) holds.

Measured effect: compiling a trivial program drove TScope's leaked
refcount from 129 down to 1 — the per-symbol CurrentScope.Define temporary
is no longer leaked.
2026-06-02 00:10:33 +01:00
Graeme Geldenhuys 857551c7b9 fix(arc): honour [Weak] on implicit-Self class field assignments
The implicit-Self field-store path in EmitAssignment (bare field name like
FParent := X) emitted the strong AddRef/Release pattern unconditionally,
ignoring the field's [Weak] attribute.  Only the explicit-receiver path
(EmitFieldAssignment) checked IsWeak.  As a result a [Weak] back-pointer
assigned via a bare name still took a strong reference, over-retaining the
target.

Route weak class fields through _WeakAssign in the implicit-Self path too,
mirroring EmitFieldAssignment.  Mark TScope.FParent [Weak] — a parent scope
outlives its children, so the back-pointer must not retain it.
2026-06-02 00:03:19 +01:00
Graeme Geldenhuys ebe131da15 feat(debug): show refcount per object in leak report
Each leaked object line now reports its refcount: `- ClassName (rc=N)`.
This distinguishes the two leak modes at a glance:

  rc=1  reference created but never released (dangling owner)
  rc>1  over-retain — an AddRef without a matching Release (double-AddRef)

Reads the refcount from the class header at UserPtr - CLASS_HDR.  Adds a
WriteSignedInt helper since immortal objects report rc=-1.
2026-06-01 23:55:51 +01:00
Graeme Geldenhuys 043602a0a4 fix(arc): eliminate double-AddRef on function-return class assignments
Three fixes that work together to correct ARC refcount accounting:

1. _ClassFree now delegates to _ClassRelease instead of bypassing ARC
   with a raw _BlaiseFreeMem.  This lets users call .Free (out of habit)
   or rely on ARC — both paths go through the same refcount logic.

2. ExprOwnsRef helper in codegen suppresses the call-site _ClassAddRef
   when the RHS expression already owns a +1 reference (function/method
   return values).  Covers TFuncCallExpr (with ResolvedDecl guard to
   exclude type casts), TMethodCallExpr, TFieldAccessExpr.IsMethodCall,
   and TIdentExpr.IsNoArgFuncCall / IsImplicitSelfMethod.

3. Allow Pointer() and PtrUInt() casts on tyString arguments, needed by
   rtl.platform.posix for Pointer(APath) where APath is a string.

The double-AddRef bug inflated refcounts on every class-returning
function call: the callee's Result := X emitted AddRef (correct — the
Result slot is not released at scope exit), and the caller's assignment
emitted a second AddRef (incorrect — the +1 already transferred).
2026-06-01 23:49:39 +01:00
Graeme Geldenhuys 89c8e49ed8 fix(arc): remove double _ClassAddRef in constructor-with-args codegen
When TFoo.Create(args) was compiled via the TMethodCallExpr constructor
path, the codegen emitted _ClassAddRef immediately after _ClassAlloc.
EmitAssignment then added a second _ClassAddRef for the receiving slot,
leaving refcount=2 after the first assignment — so the object could
never reach refcount=0 and was permanently leaked.

The TFieldAccessExpr no-arg constructor path (TFoo.Create without parens)
was already correct: no AddRef in the constructor path, leaving the
single AddRef entirely to EmitAssignment.  The TMethodCallExpr path now
follows the same contract.

Root cause: the constructor path was treating _ClassAddRef as "this
object now exists with refcount 1", but _ClassAlloc already returns
refcount=0, and the assignment site is the sole owner of the retain.

Tests:
- TClassTests.TestCodegen_Constructor_WithArgs_ExactlyOneAddRef: asserts
  exactly one _ClassAddRef in the IR for a constructor-with-args call.
- TE2ELeakCheckTests.TestDebug_ConstructorWithArgs_NoDoubleAddRef: e2e
  test compiles a program with TBox.Create(42) under --debug and asserts
  no leak report is emitted at exit.

2260 tests pass; fixpoint verified (stage-3/stage-4).
2026-06-01 16:52:48 +01:00
Graeme Geldenhuys a231c7d043 feat(debug): add --debug leak reporter + Pointer/PtrUInt cast pairs
Runtime leak tracker (blaise_arc.pas):
- Open-addressing hash map (1024 buckets, 16 bytes each) tracks every
  live class instance; zero overhead when disabled.
- _LeakTrackerEnable() activates tracking and registers _LeakTrackerReport
  via atexit; called from $main when --debug is passed.
- _LeakTrackerRegister(UserPtr, ClassName) inserts into the map; called
  from codegen at every constructor site (both TFieldAccessExpr and
  TMethodCallExpr paths).
- _ClassRelease unregisters on refcount-zero free via LTDelete.
- Report written to stderr lists count + class name of each survivor.

Compiler (--debug flag):
- New --debug CLI flag sets FDebugMode on TCodeGenQBE; emits
  call $_LeakTrackerEnable() in EmitMainHeader after unit init calls,
  and call $_LeakTrackerRegister(...) after every _ClassAlloc site.
- --debug-opdf remains independent (OPDF only, no leak tracking).
- SetDebugMode(Bool) added to TCodeGenQBE public API for e2e tests.

Pointer/PtrUInt cast pairs (semantic + codegen):
- Pointer(intOrPtrExpr) validated in AnalyseFuncCallExpr; accepts
  integer, pointer, class, metaclass, procedural, and nil sources.
- PtrUInt(intOrPtrExpr) same validation; resolves to tyUInt64.
- Codegen: w-to-l widening uses extuw for Pointer/PChar targets
  (zero-extend, not sign-extend) to preserve address semantics.
- Four new unit tests in cp.test.pointers.pas.

E2E test infrastructure:
- CompileAndRunWithRTLDebug added to TE2ETestCase base for in-process
  debug-mode compilation without recursive overload ambiguity.
- cp.test.e2e.leakcheck.pas: 5 tests covering no-leak silence,
  single leak, multiple leaks, reference cycles, and debug-off.

2258 tests pass; fixpoint verified.
2026-06-01 16:37:02 +01:00
Graeme Geldenhuys 506ada7916 chore: remove dead {$IFDEF FPC} blocks, --cache-dir, and migrate_full.py
The compiler has been self-hosting since v0.7.0 and FPC is no longer
part of the toolchain. Remove the last FPC-conditional code paths:

- Blaise.pas: drop CacheKeyForFile, LoadCachedIR, StoreCachedIR and
  both {$IFDEF FPC} whole-program IR cache blocks; remove --cache-dir
  flag and CacheDir/UnitIR/UnitName/UnitPath locals; collapse the
  ReadProcessChunk FPC/Blaise fork to the Blaise implementation only;
  drop the poUsePipes/poStderrToOutPut Options block.

- tools/migrate_full.py: deleted — the Python migration analyser is no
  longer needed now that the toolchain is fully Blaise-native.
2026-06-01 02:07:44 +01:00
Graeme Geldenhuys 8136fcaff7 perf(stdlib): rewrite GetText and GetCommaText to avoid O(N²) concatenation
Both methods accumulated a result string with repeated `Result := Result + …`
inside a loop — each iteration allocates a fresh buffer and copies the entire
accumulator forward, quadratic in total content size.

Rewrite both using TStringBuilder (geometric buffer growth, single ToString
at the end).  On a self-compile via --output the GetText hot path drops from
~17 s to ~1.7 s (~10× speedup); GetCommaText gets the same fix proactively.

Add TestRun_TextGet_ManyLines: 500-line list with mixed empty/non-empty entries,
spot-checks total length and key substrings after the rewrite.
2026-06-01 00:11:39 +01:00
Graeme Geldenhuys e549026c61 fix(semantic): implicit Self member shadows unit-level symbol
Inside a class method, an unqualified reference X (call OR bare ident)
used to bind to a unit-level X of the same name even when the
enclosing class declared X as a method / field / property.  Standard
Delphi/FPC resolution order is:

  1. Local variables / parameters / var-parameters
  2. Implicit Self.member (incl. inherited via class chain)
  3. Unit-level proc / function (program-level + uses-clause exports)

AnalyseProcCall, AnalyseFuncCall, and the TIdentExpr branch all queried
FTable first and only fell back to the implicit-Self lookup when the
global was nil.  So a 'uses strutils' in scope used to bind unqualified
CountOccurrences inside a class method to strutils's version, and a
bare 'Tag' inside a class method bound to a program-level 'Tag'
function even when the enclosing class had its own Tag.

Fixed at all three resolution sites: when FTable returns a local
var/parameter the call stays as an indirect call through it; otherwise
the implicit-Self check now runs before falling back to the unit-level
binding.

Tests pin the priority order and the explicit-qualifier escape hatches.

Contributed by: Andrew Haines <andrewd207@aol.com>
2026-05-31 18:11:37 +01:00
Graeme Geldenhuys d1c55fd0f6 fix(semantic): nested procs with same name in different outer procs no longer ambiguous
Two standalone procedures each containing a nested procedure named 'Inner'
triggered "Ambiguous overload of 'Inner'" because AnalyseStandaloneDecls
was adding all nested proc declarations to the global FProcIndex regardless
of nesting depth.

Fix: skip FProcIndex registration when FCurrentEnclosingDecl != nil (i.e.
we are inside a proc body).  Nested procs are resolved via the scoped symbol
table only; the global index is for top-level standalone procs.

AnalyseProcCall gains an early-exit branch for nested procs: when the
symbol is found in scope (skProcedure/skFunction with Decl.EnclosingDecl != nil)
but absent from FProcIndex, resolve directly from Sym.Decl.

Regression test added to cp.test.procs.pas.

Closes bug https://github.com/graemeg/blaise/issues/59
2026-05-31 17:59:46 +01:00
Graeme Geldenhuys bccb8dcf54 feat(parser,semantic,codegen): implement nested procedures with captured-var support
Nested procedures (a procedure declared inside another procedure's local
declaration section) now compile and run correctly.

Parser: ParseMethodDecl gains an ACanHaveNestedProcs flag (default False)
so that a bare procedure/function keyword after a standalone proc's header
correctly triggers body parsing.  The flag is only set True from
ParseStandaloneDecl; class-body and interface-method declarations
are unaffected.

Semantic: AnalyseStandaloneDecl tracks the FCurrentEnclosingDecl chain
and sets EnclosingDecl on each nested proc.  CollectCaptures walks the
nested proc's body AST (iterative BFS) to find all references to variables
declared in the directly enclosing proc's local block.  Those variables are
recorded in CapturedVars on TMethodDecl.

Codegen: EmitFuncDef recursively emits nested procs before the outer
function, assigning each a mangled QBE name (OuterName_InnerName).
Captured variables are passed as implicit leading pointer parameters
(l %_cap_X), keeping the outer frame's stack slot alive.  EmitVarAllocs
treats captured vars as address-taken so they always get stack slots.
TIdentExpr reads and TAssignment writes to captured vars go directly
through the %_cap_X pointer without an extra dereference.

Tests added: two IR unit tests (nested proc emitted before outer;
captured var passed by pointer) and one E2E test confirming the nested
proc mutates and the outer proc sees the updated value.  2243 tests pass;
fixpoint OK.

Closes: https://github.com/graemeg/blaise/issues/58
2026-05-31 17:45:20 +01:00
Graeme Geldenhuys 5d6abdac61 fix(semantic,codegen): allow direct invocation of proc-typed class fields
Calling a method-pointer field directly — `F.Handler;` or `F.Handler()` —
previously failed with "Class 'TFoo' has no method 'Handler'" because
AnalyseMethodCall went straight to ResolveMethodOverload without first
checking whether the name resolved to a procedural-typed field.

Fix: before the ResolveMethodOverload/error path, detect a tyProcedural
field on the receiver class and set IsProcFieldCall on the TMethodCallStmt.
EmitMethodCall then loads Code from the field slot (Self+Offset) and Data
from slot+8, emitting the same indirect (Code,Data) ABI used everywhere
else for method-pointer dispatch.

New fields on TMethodCallStmt: IsProcFieldCall, ProcFieldInfo,
ResolvedProcType.  No AST node is needed for TMethodCallExpr yet (the
expression form is a separate gap).

Closes: https://github.com/graemeg/blaise/issues/57

Tests added: IR unit test asserting indirect-call emission; E2E test
compiling and running both `F.Handler;` and `F.Handler();` forms.
2240 tests pass; fixpoint OK.
2026-05-31 16:44:55 +01:00
Graeme Geldenhuys 8b524340ce fix(codegen): storel for proc-pointer static-array slots; add Expr()() postfix call
Two related bugs found while testing procedural types as open-array elements:

1. Static-array element stores for tyProcedural used 'storew' (32-bit) instead
   of 'storel' (64-bit), truncating the function pointer and causing a segfault
   at the indirect call site.  Fixed by adding tyProcedural to the storel branch
   in the static-array element-store path in EmitArraySubscriptStmt.

2. Calling through an array subscript expression (Fns[I]()) was rejected by the
   parser with "Expected 'end' but got '('".  The postfix chaining loop only
   handled '.', '[', and '^'; it did not recognise '(' as a postfix call on a
   non-identifier expression.

   Fixed by introducing TIndirectFuncCallExpr (callee is an arbitrary TASTExpr),
   extending the postfix loop in ParseFactor to emit it when '(' follows any
   expression, and adding the corresponding semantic analysis and codegen paths.
   The codegen uses the callee value directly as the call target — no extra loadl,
   since the subscript load already yields the function pointer value.

Adds E2E test TestRun_OpenArray_ProcType_CallEach covering a static array of
TIntFn passed as an open-array parameter and called element-by-element via the
direct Fns[I]() syntax.
2026-05-22 17:10:02 +01:00
Graeme Geldenhuys a208906601 docs: update future-improvements.adoc removing completed items. 2026-05-22 16:57:19 +01:00
Graeme Geldenhuys ca622e258a fix(codegen): load class pointer before offsetting in var-param L-value
EmitLValueAddr handled the leaf TFieldAccessExpr form (no .Base) only
for inline-record and var-record-param cases.  For a class field --
where the variable's slot stores a pointer to the heap object -- the
base was emitted as VarRef(...), so a var-param call site emitted
`add &N, offset` instead of `add (loadl N), offset`.  Reads through
the var pointer hit the variable's own storage and the write never
reached the field.

EmitInstancePtr already had the symmetric branch (Fld.IsClassAccess ->
load through VarRef); add the missing case to EmitLValueAddr so calls
like `Fill(N.Value, N.IsBig)` reach the heap object.

Regression coverage:
  * TVarParamTests.TestCodegen_VarParam_ClassFieldLeaf_LoadsObjectPointer
    asserts the IR contains `loadl $N` and no bare `add $N, ...`.
  * TE2EClasses2Tests.TestRun_VarParam_ClassFields_WritebackVisible
    compiles and runs the documented reproducer and checks stdout
    shows 4096 / 1 instead of 0 / 0.
2026-05-22 16:02:34 +01:00
Graeme Geldenhuys 7dbb981665 build(runtime): enable pipefail so compiler errors fail the build
The Pascal RTL recipes pipe $(BLAISE) --emit-ir through sed to strip
the program section before writing the .ssa file.  Without pipefail,
sed's exit code masks a failing compiler invocation: the recipe
succeeds with an empty .ssa, QBE emits an empty .s, gcc archives an
empty .o, and the first symptom is a downstream link failure with
"no symbols".

Set SHELL=/bin/bash and .SHELLFLAGS=-o pipefail -c at the top of the
Makefile so every recipe inherits pipefail.  Verified by injecting a
parse error into a build driver: make now exits with Error 1 and
prints the compiler's diagnostic, instead of silently producing an
empty archive member.
2026-05-22 15:39:08 +01:00
Graeme Geldenhuys b7b3d9000f test(lexer): regression tests for UTF-8 bytes inside comments
Lock in the current behaviour: high UTF-8 bytes (2-, 3-, and 4-byte
sequences) inside brace, paren-star, and line comments are skipped
cleanly without disturbing the token stream that follows.
2026-05-22 15:33:38 +01:00
Graeme Geldenhuys aeb3e4ae8d feat(lang): extend High/Low to ordinal types; targeted float error
High and Low previously accepted only arrays and strings.  They now
also accept any ordinal type — Integer, Int64, UInt32, UInt64,
SmallInt, Word, Byte, Boolean, and enums — as either a type name or
an expression.  The result type matches the argument type so
High(Int64) round-trips through 64-bit code paths without truncation.
Bounds are folded at compile time to a literal QBE copy.

Floating-point arguments now produce a targeted error message
("not defined for floating-point types; use MaxDouble/MinDouble or
Math.Infinity") instead of the generic "must be an array or string".

docs/language-rationale.adoc records the decision; docs/grammar.ebnf
is updated to reflect the broadened intrinsic signatures.
2026-05-22 14:40:06 +01:00
Graeme Geldenhuys ae07c7476c fix(semantic): handle SizeOf(expression), not only SizeOf(TypeName)
The SizeOf handler only set ResolvedType on its argument when the
argument was a type-name identifier. For a variable or any other typed
expression, the argument's ResolvedType stayed nil and codegen later
crashed dereferencing it for ByteSize. Now non-type arguments are run
through AnalyseExpr so their ResolvedType is populated; the codegen's
compile-time fold to a literal byte size still applies.

Fixes #54.
2026-05-22 14:15:20 +01:00
Graeme Geldenhuys 2b501cdbde fix(lang): treat / as real division, distinct from div
The parser previously mapped both `/` and `div` to a single `boDiv` op,
so `Integer / Integer` was evaluated as integer division and returned
an Integer. `Trunc(Y / X)` and `Round(Y / X)` with Integer operands
were therefore rejected with "requires a float argument", and
`Round(7 / 2)` would have silently produced 3 instead of 4.

Introduce a separate `boSlash` AST op for `/`. Semantic gives it a
float result type unconditionally (Single only when both operands are
Single, Double otherwise); codegen reuses the existing float
arithmetic path. As a follow-on, `div` now rejects float operands
explicitly instead of accepting them silently.

Includes IR tests, e2e tests, and a rationale section documenting the
`/` vs `div` split.
2026-05-22 13:59:33 +01:00
Graeme Geldenhuys 34297e8317 feat(lang): add sar arithmetic-right-shift operator
`shr` stays logical (zero-fill) on all integer types, matching
Delphi/FPC semantics. The new `sar` keyword emits QBE's arithmetic
shift, preserving the sign bit on signed operands.

Closes BUG-003 (previously: signed Int64 `shr` silently discarded
the sign).  Resolved by adding a new operator instead of changing
`shr` semantics, so existing code ported from FPC/Delphi continues
to behave identically.
2026-05-22 13:40:25 +01:00
Graeme Geldenhuys e236e7cbda fix(generics): emit support data for generic classes declared in a unit
A generic class declared in a unit's interface section and instantiated
by code in that same unit failed to link with undefined references to
$_FieldCleanup_<mangled>, $vtable_<mangled>, $typeinfo_<mangled> and
the cloned constructor body.  Two coordinated gaps caused this:

* AppendUnit (the live unit codegen path) never iterated
  AUnit.GenericInstances, so no typeinfo/vtable/cleanup/method-body
  data was emitted for instances registered against the unit rather
  than the program.  Mirrored the program path's emission for each
  generic instance, including itab/impllist for instances that
  implement interfaces.

* LinkGenericClassMethodImpls ran after interface-section globals
  were analysed.  Generic instances clone the template's
  Methods.Body at instantiation time, so any FindTypeOrInstantiate
  call triggered by an interface-section global var produced an
  instance with nil bodies — codegen then emitted nothing for those
  methods.  Moved the linking step to run immediately after
  AnalyseTypeDecls(IntfBlock), before any potential instantiation,
  in both AnalyseUnit and AnalyseUnitForExport.

Added four IR tests in cp.test.generics.pas with a new GenCombinedIR
helper that exercises the real driver path (AnalyseUnitForExport +
Analyse(Prog) + AppendUnit + AppendProgram).  The test source uses a
separated impl-section body so the ordering requirement is also
covered.

Closes #40.
2026-05-22 12:38:18 +01:00
Graeme Geldenhuys e732c71cd1 feat(types): add packed record qualifier
Introduces the `packed record ... end` syntax.  Field layout in a
packed record skips natural-alignment padding between fields and
skips the record's tail padding, so SizeOf equals the cumulative
byte size of the fields.  ARC-managed field types (string, class,
interface, dynamic array) keep their natural 8-byte alignment so
that _StringRelease / _ClassRelease etc. can keep using aligned
64-bit loads through the field pointer.

`packed` is only legal directly before `record`.  `packed class`
and `packed array` are parse errors — neither has a meaningful
implementation in Blaise's heap-allocated class model nor in its
existing tightly-strided array layout.

Implementation:
  - Lexer: tkPacked token, PACKED keyword
  - AST: TRecordTypeDef.IsPacked, propagated through CloneTypeDef
  - Parser: optional PACKED prefix before RECORD; rejects other
    forms with a clear error message
  - uSymbolTable: TRecordTypeDesc.IsPacked + new FieldAlign helper;
    AddField / PackedSize / TotalSize / MaxAlign honour it
  - uSemantic: propagates IsPacked from def to desc in pass 1
  - OPDF: no change — emits whatever TotalSize reports

Tests: 11 IR-level + 2 e2e in cp.test.packedrecord.pas, plus the
TTokenKind audit bumped from 82 to 83.

Grammar and language-rationale updated.
2026-05-22 11:31:42 +01:00
Graeme Geldenhuys 7e613c6853 fix(codegen): use narrow load/store for implicit-Self Byte/Word/SmallInt fields
The two implicit-Self field paths (assignment to bare Field inside a
method, and reading a bare Field name) hardcoded storew/loadw whenever
QbeTypeOf was 'w'.  After SizeOf(Byte)=1 was introduced — and now with
SmallInt / Word at 2 bytes — that over-writes (or over-reads) the
adjacent fields.  Symptom: setting four Byte fields A..D in sequence
left A reading as 0x04030201 instead of 1.

Route both sites through StoreInstrFor / LoadInstrFor, matching the
explicit obj.Field codepath that already calls the helpers.  No change
for Integer / UInt32 / Enum / Boolean / Set (still storew / loadw); the
helpers select storeb / loadub for Byte and storeh / loadsh / loaduh
for the new 16-bit types.

Two e2e tests pin the regression:
  - TestRun_ImplicitSelf_ByteFields_NoBleed (4 adjacent Byte fields)
  - TestRun_ImplicitSelf_SmallIntWord_Fields (SmallInt + Word)
2026-05-22 08:58:16 +01:00
Graeme Geldenhuys 5d092cafee feat(types): add SmallInt / Word 16-bit integer types
Introduces tySmallInt and tyWord first-class types alongside the
existing Integer / Int64 / Byte / UInt32 / UInt64 family.  Storage is
2 bytes (storeh / loadsh / loaduh) but values are widened to QBE 'w'
in registers, matching the Byte pattern.  Int16 and UInt16 are
accepted as aliases.

Implicit widening into Integer, Int64, UInt32 and UInt64 is permitted;
all 16-bit values fit losslessly in those wider types.

Tests: 11 IR-level + 5 e2e in cp.test.smallint_word.pas.

Grammar and language-rationale updated to remove the "deferred" note
on SmallInt/Word.
2026-05-22 08:51:21 +01:00
Graeme Geldenhuys b43a999f80 feat(types): add UInt64 / QWord type
Adds a real 64-bit unsigned integer type with two equivalent names:
UInt64 (Delphi style, matches the existing Int64) and QWord (FPC
style).  PtrUInt now aliases UInt64 too — it's the natural pointer-
sized unsigned on 64-bit.

Language semantics:
- Arithmetic on UInt64 uses udiv/urem; add/sub/mul/and/or/xor/shl/shr
  are bit-identical to their signed counterparts.
- Comparisons use unsigned QBE ops (cultl, cugtl, ...).
- Int64 <-> UInt64 mixing requires an explicit cast; the two types are
  not implicitly convertible in either direction.
- Decimal/hex literals in the (2^63, 2^64-1) range are typed as
  UInt64.  Smaller literals stay Integer/Int64.
- SizeOf(UInt64) = SizeOf(QWord) = 8.

Runtime:
- New _UInt64ToStr in blaise_str.pas plus a WriteDecimalU helper that
  uses UInt64 arithmetic.
- SysWriteUInt64 added to the platform abstraction and implemented in
  the POSIX layer.  WriteLn(UInt64) routes through it.
- IntToStr(UInt64) routes to _UInt64ToStr; explicit UInt64ToStr is
  also exposed as a builtin.

Bootstrap notes:
- Older release binaries cannot compile the runtime any more because
  rtl.platform.pas declares SysWriteUInt64.  A stage-2 rebuild from a
  fresh stage-1 is required after this commit on any worktree with an
  older stage-1, per CLAUDE.md.
- The parser stages literal Value through local var-params rather than
  writing directly to the new TIntLiteral.IsUInt64 field via class-field
  out-params.  Working around a stage-1 codegen bug where var-param
  calls that target a class field silently fail to write back.

Docs:
- docs/grammar.ebnf: built-in type list expanded with UInt64/QWord,
  integer-literal typing rules documented.
- docs/language-rationale.adoc: integer types table updated with the
  unsigned variants, Int64<->UInt64 strict conversion rule explained.

Tests:
- 15 new tests in cp.test.uint64.pas covering symbol-table
  registration, codegen instruction picking (udiv/urem/cultl/cugtl),
  literal-range typing, and e2e round-trips.
- Full suite: 2151 tests pass (up from 2132).
- Fixpoint clean at stage-3/stage-4 (expected: type-system change).
2026-05-22 08:08:35 +01:00
Graeme Geldenhuys a899c937a2 feat(testing): accept multiple --suite filters
Previously --suite could only be passed once; subsequent occurrences
silently overwrote earlier ones, making it hard to run a curated subset
of tests in one invocation.  The runner now collects every --suite
value into a filter list and runs a test if any filter matches.

Each --suite value may also be a comma-delimited list, so

  TestRunner --suite TA --suite TB.m
  TestRunner --suite TA,TB.m

are equivalent.  A filter with no '.' matches any method in that class;
a filter of the form Class.Method pins one specific method.  Empty
entries and surrounding whitespace are ignored.  An empty filter list
runs everything (unchanged default).

The threaded-subprocess fan-out is skipped when filters are supplied —
matching tests run in-process, which is what users want when narrowing
a debug session.

Helpers SplitSuiteSpec, AppendSuiteFilter, and MatchesFilters are now
exposed for direct unit testing in cp.test.runner_filters.pas (13
tests).
2026-05-21 23:27:21 +01:00
Graeme Geldenhuys 525f36e769 fix(codegen): pack Byte/Boolean fields at 1-byte stride
SizeOf(Byte) reported 4 instead of 1, and Byte/Boolean fields inside
records were laid out at 4-byte stride.  Both broke struct interop with
external libraries and produced surprising SizeOf results.

Changes:
- TTypeDesc.ByteSize and AllocAlign return 1 for tyByte / tyBoolean.
- TRecordTypeDesc gains PackedSize (cumulative offset, no tail pad) and
  TotalSize now tail-pads records (but not classes) up to MaxAlign.
  MaxAlign defaults to 1 so all-Byte records report their natural size.
- AddField/PackedSize align each field to its AllocAlign so a Byte
  followed by an Integer correctly pads the Integer to offset 4.
- Codegen gains LoadInstrFor/StoreInstrFor helpers and uses loadub/storeb
  on byte-sized field accesses to avoid over-reading/over-writing into
  adjacent fields.  Open-array element reads use the same helper.

Pointer fields are now correctly aligned to 8 inside records (previously
they could land on a 4-byte boundary after an Integer field).  The
TNode-with-vptr+Integer+pointer test moves from size 20 to size 24 to
reflect this — the old size was wrong on strict-alignment targets.

Tail-padding is intentionally skipped for class instances so InstanceSize
matches FPC/Delphi semantics (classes are never directly stored in
arrays — only references to them are).

Tests:
- TestSemantic_FourByteRecord_TotalSizeIs4
- TestSemantic_ByteThenInteger_AlignsInteger
- TestSemantic_ByteFieldOffsets_Are0123
- TestCodegen_SizeOfByte_Is1
- TestCodegen_SizeOfFourByteRecord_Is4
- TestRun_Record_FourByteFields_PackedAndRoundTrip
- TestRun_Record_ByteThenInteger_RoundTrip
2026-05-21 23:17:10 +01:00
Graeme Geldenhuys 80488bf0a4 test(bench): record TList<TObject> scan speedup from ARC elision
After commit ffe09b3 (nil-slot _ClassRelease elision), TList<TObject>
hand-rolled scan x 1000 went 131 ms -> 90 ms (~30% improvement) on
the same hardware as the previous entry.  Smaller wins on sequential
and random Get for the same workload.
2026-05-21 17:28:44 +01:00
Graeme Geldenhuys a6c87e33ea chore(clean): remove all old FPC compiler directives we don't need in Blaise
There were still from the days when FPC compiled Blaise's tests.
2026-05-21 17:23:38 +01:00
Graeme Geldenhuys ffe09b395f perf(codegen): elide _ClassRelease on provably-nil slots
When the LHS of a class-typed assignment is a local slot that has not
yet been written within the function entry block, EmitVarAllocs has
just zeroed it via `storel 0, %_var_X`.  Releasing nil is a no-op at
runtime but still pays a function call + ret per assignment.  Skip
the load+release entirely in this case; only the addref+store
remain.

Tracking is conservative:
 * `FArcSlotWritten` is cleared at every function start.
 * `ArcSlotIsNil(X)` is True only while we are still in the @start
   block and X has not been written.
 * Any new label (branch/loop/case body) ends the elision window for
   all slots; that prevents leaking a value written along a branch we
   did not see.
 * Globals, var-params and mem2reg-promoted locals are excluded.

Impact on `tests/bench_lists.pas` (Ryzen 7 5800X):

  TList<TObject> hand-rolled scan x 1000:  131 ms -> 90 ms  (~30%)
  Sequential Get(i) x 1_000_000:             6 ms ->  5 ms
  Random      Get(i) x 1_000_000:           13 ms -> 12 ms

The IR for `TList<T>.Get` now shows a single AddRef call instead of
the prior AddRef+Release pair on the Result slot.

Adds three IR tests in cp.test.arc covering: first-store elision,
second-store still-releases, and post-branch still-releases.
All 2112 tests pass; fixpoint clean at stage-3/stage-4.
2026-05-21 17:17:03 +01:00
Graeme Geldenhuys 90b15afce4 feat(stdlib): add TList<T>.IndexOf
Linear scan over FData returning the index of the first equal element
or -1 when not found.  Matches the semantics of TSet<T>.IndexOf which
the rest of the unit already exposes, and saves callers from writing
the hand-rolled scan that bench_lists.pas demonstrates.

Adds two e2e tests covering the integer/found and string/not-found
paths.
2026-05-21 15:23:35 +01:00
Graeme Geldenhuys 07b9b8ddbd test(bench): merge bench_strings + bench_objects into bench_lists
The multi-instance generic miscompile that forced the two-program
split is fixed (b6736d3), so the four workloads now run in a single
program: TStringList, TList<String>, TObjectList, TList<TObject>.

Add a fresh results entry on the post-fix compiler; timings are
within noise of the split run on the same hardware.
2026-05-21 14:37:22 +01:00
Graeme Geldenhuys b6736d3925 fix(semantic): clone method bodies per generic instance
Generic instance method bodies were shared via OwnBody=False, so a
single TBlock AST was annotated by the semantic analyser once for each
instance in turn.  The Resolved* annotations on the shared body ended
up reflecting whichever instance was analysed last, causing call
targets in earlier instances to point at later instances' helpers —
e.g. TList<String>.Add would emit `call $TList_TObject_Grow` and skip
$_StringAddRef on the value parameter.

Add CloneBlock/CloneStmt/CloneExpr in uAST.pas covering all node types
that can appear inside a method body (~26 expr/stmt forms plus block-
level Var/Const/Type/Proc decls).  Annotations are deliberately not
copied — each instance re-runs semantic analysis on its own AST.
Wire the clone into InstantiateGeneric and InstantiateGenericFunction
so each instance owns its body.

Adds TestCodegen_Generic_TwoInstances_MethodCallsResolveToOwnInstance
to lock in: with TBox<Integer> and TBox<String> in one program, the
emitted TBox_Integer_Init body must contain
`call \$TBox_Integer_SetValue` and the TBox_String_Init body must
contain `call \$TBox_String_SetValue`.  Previously both bodies called
the String instance's helper.

Verified: 2107 tests pass; fixpoint clean; the two-instance benchmark
scenario (TList<String> + TList<TObject> in one program) now compiles
and runs correctly.
2026-05-21 14:30:24 +01:00
Graeme Geldenhuys 39136547f5 fix(semantic): allow two instances of the same generic class
A program that instantiated the same generic class with two different
concrete arguments (e.g. TList<String> and TList<TObject>) failed at
semantic analysis with 'Type mismatch: expected ''^T'' but got
''^TObject'''.  Two compounding causes:

- FindTypeOrInstantiate cached compound types ('^T', 'array of T',
  'class of T', 'array[lo..hi] of T') under the literal input string.
  When 'T' was a scope-bound type parameter the cached entry pinned
  the first instance's resolution ('^String'), so a second
  instantiation that bound T to a different type re-used the wrong
  entry.  Key these caches by the canonical element-type name
  ('^String', 'array of TObject', ...) so each (constructor + element)
  pair has its own slot.

- AnalyseMethodCallExpr mutated AExpr.ObjectName to the resolved class
  name ('TFoo<T>' -> 'TFoo<String>') on first analysis.  Because
  cloned generic methods share the body AST (NewMDecl.OwnBody := False
  in InstantiateGeneric), the mutation was visible to the next
  instance's analysis and made it look up the wrong concrete class.
  Only normalise casing when the lookup hit the original spelling;
  otherwise keep the template form and pass the resolved name to
  overload resolution via a local.

A separate codegen issue still affects per-instance method body
emission when the body is shared — each function is emitted but the
shared body's call-target annotations come from whichever instance
was analysed last, so e.g. TList<String>.Add can end up calling
TList<TObject>.Grow.  Tracked separately; needs per-instance body
cloning to fix properly.
2026-05-21 11:16:37 +01:00
Graeme Geldenhuys db84dc8b3f test(bench): TStringList vs TList<String>, TObjectList vs TList<TObject>
Micro-benchmark comparing the legacy list types against the generic
list equivalents introduced for self-hosting.  Logs the median of two
runs to tests/bench_lists_results.txt for trend tracking — append a
new dated section after each meaningful runtime or codegen change.

Phases per container: bulk insert (1M), sequential read, random read
(1M), for..in iteration (strings only), and indexed lookup (IndexOf or
hand-rolled scan).  Initial run on AMD Ryzen 7 5800X shows TList<String>
beating TStringList across the board, while TObjectList still beats
TList<TObject> on writes and on IndexOf — the latter by ~8x due to
direct pointer-scan vs. one Get call per element.
2026-05-21 10:53:13 +01:00
Graeme Geldenhuys 93358ed08a fix(codegen): retain/release class refs stored through typed pointer
EmitPointerWrite emitted retain+release for tyString but had no parallel
case for tyClass.  Storing a class reference through a typed pointer
('PItem^ := SomeObj' where PItem: ^TItem) therefore left the slot
without a strong reference, so the object was freed as soon as the
original holder dropped its ref — subsequent reads returned dangling
memory.

This surfaced when TList<TObject>.Add stored items through its 'FData:
^T' backing buffer: every Get returned garbage once the caller reused
the local variable that had supplied the value.  TList<String> worked
because the string path was already in place.

Add the symmetric tyClass branch.
2026-05-21 10:50:56 +01:00
Graeme Geldenhuys ed7323a618 feat(stdlib): add TListEnumerator<T> + TList<T>.GetEnumerator
Wires up for..in on TList<T> from generics.collections.  Two compiler
fixes were required:

- uSemantic: TClassName<T>.Method(args) inside a generic method body
  failed to resolve because AnalyseMethodCallExpr looked up the literal
  name with the unsubstituted type parameter.  Now mirrors the existing
  field-access path: if the receiver name contains '<' and lookup fails,
  apply ResolveScopeBoundTypeParams + FindTypeOrInstantiate before
  re-trying the lookup.

- uCodeGenQBE: constructor-with-args path emitted
  $_FieldCleanup_TListEnumerator<String> with literal angle brackets
  which QBE rejects.  Mangle the class name like the no-arg path
  already does.
2026-05-21 10:25:19 +01:00
Graeme Geldenhuys 6352c6fcd0 fix(semantic): wire up parent class on generic instances
InstantiateGeneric did not assign RT.Parent, CopyVTableFrom parent, or
copy parent fields onto the synthesised TRecordTypeDesc. With no parent
vtable copied in, RT.HasVTable returned false for every generic
instance, so the codegen skipped emitting $vtable_<T> while the
typeinfo emitter still referenced it — producing an unresolved symbol
at link time for any program that used TList<X>, TStack<X>, TQueue<X>,
TSet<X>, TDictionary<K,V>, etc. from generics.collections.

Mirror the regular class-resolution path in AnalyseProgramTypes: look
up the explicit parent (or fall back to TObject when no parent name is
given), set RT.Parent, CopyVTableFrom the parent so Destroy/ToString
slots are inherited, and copy the parent's fields so FindField sees
them. The order is preserved — parent-wiring runs before the vtable
slot pre-pass which runs before field resolution, so TotalSize
correctly accounts for the 8-byte vptr.

Add cp.test.e2e.tlist.pas — the first e2e coverage of a stdlib
generic class. IR-only tests would not have caught this because the
link step is what failed.
2026-05-21 10:01:28 +01:00
Graeme Geldenhuys 8cced6c91f fix(semantic): walk parent chain when resolving inherited properties
FindProperty and FindIndexedProperty searched only the current class's
own Properties list, so a subclass could not see properties declared on
its ancestors. Inherited fields work because parent fields are copied
into the child during class resolution, but properties are not copied —
they must be discovered via inheritance walking, as FindMethodDecl
already does.

Fixes #45.
2026-05-21 09:44:24 +01:00
Graeme Geldenhuys 1b531e051f fix(semantic): register var decls before analysing method bodies
Class methods in a program could not access program-level globals because
AnalyseBlock analysed method bodies before pushing the block scope and
registering its var declarations. Units were unaffected because
AnalyseUnit already registers impl-section vars first.

Closes #43.
2026-05-21 09:30:33 +01:00
Andrew Haines 56014894d3 fix(semantic): use overload resolution for constructor calls
Constructor calls like TFoo.Create(arg) were using FindMethodDecl, which
returns the first MethodDecl indexed for the TypeName.MethodName key.
With overloaded constructors that share a name (e.g. one-arg Create vs
two-arg Create), the picked candidate may have a higher arity than the
call site.  AppendDefaultArgs then fails on the unfilled parameters
with "No default value for parameter 'X' of 'Create'" — even though a
matching lower-arity overload exists.

Reproduces with a class declaring Create(A, B) before Create(A): the
two-arg overload is indexed first and chosen for a one-arg call site.

Replace FindMethodDecl with ResolveMethodOverload at the constructor
call site; fall back to FindMethodDecl when the resolver returns nil
(extern/RTL constructors not registered in FMethodIndex).

Adds IR tests in cp.test.overload.pas and an E2E test in
cp.test.e2e.classes2.pas that exercise the declaration-order-sensitive
case.

Original contribution from Andrew Haines <andrewd207@aol.com>
(https://github.com/andrewd207/blaise_llvm/commit/c2a3bbed).
2026-05-21 08:48:48 +01:00
Graeme Geldenhuys 2b3840265a fix(codegen): ARC retain/release on array element writes
Subscript assignment into managed-element arrays (string or class)
emitted only a raw store, leaking the new value's refcount and dropping
the old value's release. After two writes to the same slot, the first
value was leaked and the program ran with an unbalanced refcount.

EmitStaticSubscriptAssign now emits the standard retain-new / release-
old pair before the store, for both the dynamic-array branch and the
static-array branch.

Regression coverage in cp.test.staticarray:
- TestCodegen_StaticArray_StringWrite_EmitsARC
- TestCodegen_StaticArray_ClassWrite_EmitsARC
- TestCodegen_DynArray_StringWrite_EmitsARC
- TestCodegen_DynArray_ClassWrite_EmitsARC

Original contribution from Andrew Haines (andrewd207).
2026-05-21 08:36:15 +01:00