Commit graph

807 commits

Author SHA1 Message Date
Graeme Geldenhuys fa106ceeee feat(sets): support integer-subrange set base (set of 0..255) and Boolean operands
Accept an anonymous integer subrange as a set base type, e.g.
'set of 0..255' or 'set of 1..10', in both the inline (var/param/field)
and 'type T = set of L..H' declaration positions. The subrange lowers to
the same bitmap machinery as 'set of Byte': the bitmap is sized to H+1
bits and member ordinals are the integer values themselves. Bounds may be
integer literals, named constants, or constant expressions; the lower
bound must be >= 0, the upper <= 255, and the range ascending.

Also fix 'set of Boolean' so a Boolean operand is accepted directly on
either side of 'in' and as the second argument of Include/Exclude, without
an intervening Ord().

Parser handles both 'set of' paths (ParseTypeName and ParseSetDef);
semantic resolution adds ResolveSubrangeSetType for on-demand and
type-decl paths. Adds dual-backend e2e tests and semantic unit tests.
Updates grammar.ebnf and language-rationale.adoc per the language-decision
rule, and removes the now-implemented item from future-improvements.adoc.
2026-06-18 13:25:22 +01:00
Graeme Geldenhuys f107b0dabc docs: narrow the ordinal-set future-improvements entry to the unimplemented part
`set of byte` and `set of Boolean` are implemented (issue #105) — named ordinal
base types lower to a bit set (byte uses the 256-bit jumbo path), with working
membership/Include/Exclude and [lo..hi] range literals on both backends. The
section is rewritten to record that, leaving only the genuinely-remaining work:

* the anonymous integer-subrange base form `set of 0..255` / `set of 1..10`,
  still rejected at parse time ("Expected enum type or '(' after 'set of'");
* a minor `set of Boolean` follow-on where `True in s` is rejected by the `in`
  type-check (Ord(b) in s works).
2026-06-18 13:00:04 +01:00
Graeme Geldenhuys bc0485285f cleanup: revert KI-3 exception-handler var renames to plain E
The KI-3 ARC triple-release bug — multiple `on E: TYPE do` handlers in one
except block colliding on a single QBE slot — is fixed, so the workaround of
giving each handler a distinct variable name can be reverted. Handler vars are
now the natural `E` again in:
  - blaise.testing.pas: the runner's 4-handler dispatch block
    (EAF/EIT/E/ETO -> E) — the exact KI-3 scenario, in the test framework's
    own outcome classifier.
  - cp.test.attributes.pas: ETO -> E.
  - cp.test.sets.pas: ESE/EEx -> E.

(There were no {$IFDEF FPC} blocks left to remove — the KI-2 .Free guards and
all other FPC conditionals were already gone from the source tree as part of
the FPC-independence migration; verified by a full-tree directive scan.)

All three fixpoints green; 3501 tests pass, including the framework's own
FAIL/IGNORED/ERROR outcome paths.
2026-06-18 12:17:06 +01:00
Graeme Geldenhuys 1ef226369d fix(semantic): accept empty open-array literal [] as an open-array argument
An empty bracket literal [] passed to a plain open-array parameter
(`array of T`) failed overload resolution with "No matching overload" on both
backends. A non-empty literal works because AnalyseArrayLiteralExpr infers the
element type and types it `array of <elem>`; an empty [] has nothing to infer
from and was left untyped (nil), so it matched neither the open-array param (nil
arg type) nor the existing set / array-of-const special cases.

Two changes in uSemantic:
* ArgMatchScore now scores an empty TArrayLiteralExpr as an exact (2) match
  against any tyOpenArray parameter — an empty open array is valid for any
  element type — placed before the nil-arg bail so overload selection accepts it.
* RetypeSetLiteralArgs pins the empty []'s ResolvedType to the chosen formal's
  open-array type, so codegen emits a zero-length open array (data=nil, high=-1)
  of the right element type.

Overload disambiguation is preserved ([] picks the open-array overload; a scalar
argument still picks the scalar overload), and two equally-matching open-array
overloads still raise a clean "ambiguous overload" error. Works for
`array of string`, `array of Integer`, and `array of const`.

This was the last open item in native-testrunner-investigation.txt; the native
TestRunner now passes the full suite unfiltered. Adds dual-backend e2e tests.
All three fixpoints green; 3501 tests pass.
2026-06-18 11:53:51 +01:00
Graeme Geldenhuys 937146a3ac fix(arrays): enum-indexed multi-dimensional arrays (#128)
A multi-dimensional array whose dimensions are enum types — array[TEnum, TEnum]
or array[TEnum, 0..1] — failed to parse with "Expected '..' but got ','". Only
the single-dimension form array[TEnum] was handled; every dimension in the
comma-separated multi-dim path was forced through the lo..hi range parser.

Both the const-array dimension parser (ReadConstArrayDim) and the type-name
parser (ParseTypeName) now recognise an enum-type dimension — a bare identifier
not followed by '..' — and encode it as the range 0 .. @TEnum, reusing the
existing single-dimension '@TEnum' convention. ResolveArrayBound resolves the
'@TEnum' high-bound marker to the enum's last ordinal (member count - 1). This
fixes const, type and var declarations in any dimension position and mix
(enum+range, range+enum, 3-D enum+range+enum).

Adds dual-backend e2e coverage: 2-D enum/enum, mixed dims in both orders,
type+var form, and a 3-D mixed case. All three fixpoints green; 3499 tests pass.
2026-06-18 11:25:15 +01:00
Graeme Geldenhuys 517ed26f5d feat(const): integer literals and ranges in const-decl set literals
A typed set constant accepted only enum-member identifiers — `const C: TByteSet =
[1, 2, 3]` and `[1..3]` both failed with "Expected set member identifier". The
const-decl set path (a separate, more limited parser than the expression set
literal) is extended to accept:

* integer-literal members (1, 2, 3),
* inclusive ranges `lo..hi` (each endpoint an integer literal, enum member, or
  named constant), expanded to all ordinals in range,
* and ordinal base types (set of Byte / Boolean), not just enums.

Parser stores each member verbatim (a range as 'lo..hi'); the semantic pass
(AnalyseSetConstDecl) resolves and expands them via a new ResolveSetMemberOrd
helper, integrating with the existing small-mask and jumbo byte-bitmap const
machinery. Enum-member const sets (the original form) still work.

Adds dual-backend e2e coverage including edge cases: mixed range+literal, a jumbo
(>64-member) set with a range, the empty set, descending-range rejection, and an
enum-set regression. All three fixpoints green; 3495 tests pass.
2026-06-18 11:08:00 +01:00
Graeme Geldenhuys 6289a5235e fix: fold Boolean/enum/named-const array-const elements + emit correct width
An array constant with Boolean (or enum, or named-constant) elements emitted the
identifiers verbatim — `(False, True, ...)` became symbol references, failing at
link time on native ("undefined reference to `False'") and in QBE ("unknown
keyword False"). And even once folded, every non-string element was emitted at a
fixed 4-byte stride that did not match the element's actual read width, so a
Boolean array read all-zero.

Two fixes:
* Semantic (AnalyseArrayConstDecls): a new ResolveConstArrayElem folds each
  bare-identifier element to its numeric value once the element type is known —
  Boolean True/False to 1/0, enum members and named integer/boolean constants to
  their ordinal/value. Numeric/float literals pass through. An unresolved
  identifier is now a clear semantic error instead of a link failure.
* Codegen (both backends): array-const element data is emitted at the element's
  real width — .byte/.word/.long/.quad on native, b/h/w/l in QBE — instead of a
  fixed .long/w. Built-in scalar names are mapped directly so the width is
  correct even when the codegen has no symbol table set (the e2e in-process path).

Adds dual-backend e2e coverage including edge cases: Byte/Word/Int64 widths,
negative integer pass-through, named-const elements, and a Boolean array indexed
by an enum. All three fixpoints green; 3490 tests pass.
2026-06-18 10:50:34 +01:00
Graeme Geldenhuys 734030932d feat(native): single-unit compilation + incremental/warm-cache builds
The native backend previously stubbed GenerateUnit and left
SupportsIncremental/SupportsWarmCache False, so --incremental fell back to QBE.
Native now drives the per-unit parallel pipeline itself:

* TCodeGenNative.GenerateUnit emits a single unit in isolation (new
  TNativeBackend.GenerateUnit: clear buffer, EmitUnit, return assembly).
* The native driver overrides SupportsIncremental/SupportsWarmCache (True),
  CreateUnitCodeGen (separate-compile mode), and LowerToObject (assemble the
  unit .s to .o via the internal assembler or `cc -c -x assembler`).
* Separate-compilation mode (FSeparateCompile): unit objects omit the
  once-per-program TObject/TCustomAttribute system defs — the program object
  provides them — and emit their own file-local string-literal blobs, so a unit
  method referencing a literal links cleanly. The system typeinfo/vtable symbols
  (typeinfo_TObject, vtable_TObject, and TCustomAttribute) are now .globl so unit
  objects can reference the program's single definition.

Works with both the external (`cc -c`) and internal assemblers, and the warm
cache (a second build reuses the per-unit .o). Adds a sepcompile e2e test
building a multi-unit class program with --backend native --incremental.

All three fixpoints green; 3485 tests pass.
2026-06-18 09:30:37 +01:00
Graeme Geldenhuys ebe7057bb7 feat(native): static-array-of-interface store, read, and dispatch
A static array of interfaces (Arr: array[0..2] of IGreet) was unusable on the
native backend in three ways, all now fixed:

* Store (Arr[I] := V): the element is a contiguous 16-byte fat pointer, but the
  generic static-subscript store wrote only the 8-byte obj and left the itab
  garbage. Now computes the element address and delegates to
  EmitInterfaceToFieldSlotsAt (obj+itab with ARC for nil/class/interface).
* Read as RHS (G := Arr[I]): EmitInterfaceAssign gained an interface-subscript
  source case that copies obj+itab with ARC.
* Method dispatch (Arr[I].M()): EmitInterfaceCall gained a subscript-receiver
  case that loads obj/itab from the element address.

A shared EmitIntfStaticElemAddr helper computes the element fat-pointer address
(base + (idx-LowBound)*16), reused by all three paths and by the interface-store
source. EmitInterfaceToFieldSlotsAt also learned an interface-subscript source
(Arr[J] := Arr[I]). Native now matches QBE exactly on the full test
(11/22/22/11/11). TestRun_StaticArrayOfInterface_FatPointer is dual-backend.

(A pre-existing scope-exit leak of interface array elements affects BOTH backends
identically — logged separately in bugs.txt, not a parity gap.)

All three fixpoints green; 3484 tests pass.
2026-06-18 09:07:52 +01:00
Graeme Geldenhuys a6378b39e2 fix(native): release the owned ref of an interface-call-result argument
Show(MakeFoo(42)), where MakeFoo returns an interface, handed the borrowing
parameter an owned (+1) fat pointer that was never released after the call —
--debug reported one leaked instance on native (QBE already released it).

Added an akIntfConsume hoist kind: EmitArgHoist drives the interface-returning
call in the pre-pass and saves the owned fat pointer (itab then obj, obj on
top); the argument reload pushes both halves as the two interface arg slots; and
EmitHoistEpilogue releases the obj after the call (mirrors akStrConsume). The
2-slot reload is wired into both EmitArgPush and EmitCall's inline argument loop.

--debug leak report is now clean on both backends; two leakcheck tests (QBE +
native) are added. All three fixpoints green; 3484 tests pass.
2026-06-18 08:55:58 +01:00
Graeme Geldenhuys 2d8497f2b3 fix(native): keep %rsp 16-aligned in the record-call argument hoist
DateAddDays(MakeDate(...), 5) segfaulted on native while the non-nested form
worked. The args arrived correct — the fault was a stack-alignment bug. The
argument hoist for a record-returning-call argument allocated a 16-byte-aligned
sret buffer (RecArgBufBytes) and then pushed an 8-byte pointer to it, a 24-byte
contribution that left %rsp 8 bytes off 16-alignment. The misalignment was
harmless until the callee chain reached an aligned SSE access — here libm's
mktime (via DateUtils _TimeJoin) executing `movdqa`, which faults on a
misaligned address.

EmitArgHoist now reserves a full 16-byte slot for the saved pointer (subq $8 +
pushq) so the hoist region stays a multiple of 16. The pad sits above the saved
pointer, so reload offsets are unchanged. This is the shared hoist, so it fixes
the alignment for every record-call-argument site, not just DateAddDays.

The DateAddDays e2e test is re-enabled on both backends; a self-contained
regression (record-call result as a record-by-value arg feeding an SSE op) is
added to the gaps suite. All three fixpoints green; 3482 tests pass.
2026-06-18 08:44:54 +01:00
Graeme Geldenhuys 28a4093244 fix(native): class-level const array element access (T.Arr[I])
A class-level const array (const Days: array[0..6] of string = (...)) read via
an instance, T.Days[I], returned an empty string on native. The semantic pass
folds the subscript into the field-access node — IsConstant=True with
ConstArraySymbol (the global data label), ConstArrayType (the static-array type)
and PropIndexExpr (the index) — but native's EmitExprToEax IsConstant branch only
handled scalar string/int consts and fell through to an empty string literal.

Added the class-const-array element path: compute
base + (idx - LowBound) * ElemSize and load the element by type (float / string
pointer / narrowed integer), plus a bare-reference case returning the label
address. Mirrors the QBE EmitExpr ConstArraySymbol path. The range- and
enum-indexed class-const-array e2e tests are converted to AssertRunsOnAll.

(Static-array-of-interface element store/read/dispatch remains a separate, larger
follow-up — tracked in bugs.txt; that test stays QBE-only meanwhile.)

All three fixpoints green; 3481 tests pass.
2026-06-18 08:12:22 +01:00
Graeme Geldenhuys 5c1293f268 fix(native): Round() rounds half-away-from-zero to match QBE/Delphi
Native lowered Round() with cvtsd2si/cvtss2si, which honour the FPU rounding
mode (round-half-to-even / banker's): Round(2.5)=2, Round(-2.5)=-2. QBE — and
Delphi/FPC — round half away from zero via C99 round(): Round(2.5)=3,
Round(-2.5)=-3. Native now calls round()/roundf() first, then truncates the
integral result, matching QBE exactly. The two Round e2e tests are converted to
AssertRunsOnAll.

All three fixpoints green; 3481 tests pass.
2026-06-18 07:57:59 +01:00
Graeme Geldenhuys 92e09e517f feat(native): TObject.InheritsFrom and default ToString builtins
The native backend raised "TMethodCallExpr has no ResolvedMethod" for the two
TObject built-in methods that the semantic pass flags rather than resolving to a
declared method:

* InheritsFrom(C): load the receiver's typeinfo (vtable[0] for a class instance),
  evaluate the class-of argument to a typeinfo pointer, and call
  _InheritsFrom(self_ti, arg_ti) → Boolean.
* ToString: virtual dispatch through vtable slot 2 (offset 16: typeinfo, Destroy,
  ToString), returning the string in %rax — the default returns the class name.

A small EmitMethodReceiverToRax helper loads the instance pointer (expression,
named local/global, or implicit Self). The 7 InheritsFrom tests and the default
ToString test in cp.test.e2e.classes2 are converted to AssertRunsOnAll, pinning
both builtins on QBE and native.

All three fixpoints green; 3481 tests pass.
2026-06-18 07:50:43 +01:00
Graeme Geldenhuys 161d8e4102 fix(native): StrToDouble returns its Double in %xmm0, not %rax
StrToDouble was lowered only in the integer EmitExprToEax path: it called
_StrToDouble (which returns the Double in %xmm0 per the System V ABI) and left
%rax untouched, so a float assignment (D := StrToDouble(S)) read %rax — a
leftover pointer — and cvtsi2sd'd it into garbage. Native printed values like
95693543141392 instead of the parsed number. Added StrToDouble to the float
emitter (EmitFloatBuiltin) so the result is consumed from %xmm0.

The 7 StrToDouble e2e tests are converted to AssertRunsOnAll, pinning the fix on
both backends. The blanket dual-backend flip of the inline CompileAndRun helper
is documented as deliberately reverted: it breaks on non-deterministic programs
(GetProcessID) and several genuine native gaps remain open (InheritsFrom/ToString
builtins, Round .5-boundary, class const-array, interface-field-assignment RHS,
EmitSretCall record-value arg) — all logged in bugs.txt for individual fixes.
Suites migrate to AssertRunsOnAll as those gaps close.

All three fixpoints green; 3481 tests pass.
2026-06-18 02:56:13 +01:00
Graeme Geldenhuys 5dd6a3d8c9 feat(native): pass an interface-returning call result as an argument
Show(MakeFoo(42)), where MakeFoo returns an interface, previously raised
"unsupported interface argument expression" on the native backend. EmitMethodArgPush
now handles a TFuncCallExpr/TMethodCallExpr argument of interface type: it lowers
the call via EmitIntfSretCall/EmitIntfSretMethodCall (which leaves the fat pointer
at (%rsp)), loads obj+itab, drops the sret buffer, and pushes the pair in arg-slot
order. Both backends now print 42. Adds a dual-backend e2e test.

Known follow-up (tracked): the owned (+1) reference of the call result is not yet
released after the call, so --debug reports a single leaked instance for this
specific borrowed-interface-call-result shape. The feature is output-correct; the
ARC release will route through EmitArgHoist (akIntfConsume) like const-string
consume args. All three fixpoints green; 3481 tests pass.
2026-06-18 02:33:14 +01:00
Graeme Geldenhuys 44a2bd93fb feat(native): 3-arg Supports out-var; run RTL/stdlib e2e on both backends
Native backend gaps surfaced by making the RTL/stdlib e2e suites dual-backend:

* 3-arg Supports(Obj, IFoo, OutVar): the native EmitExprToEax only handled the
  2-arg boolean form, so the out-var was never populated or registered — a local
  out-var read garbage and a global one produced undefined _obj/_itab symbols at
  link time. Implemented the 3-arg form (GetItab + ARC retain/release + global
  registration), mirroring the QBE EmitSupportsExpr path.

* e2e harness: CompileAndRunWithRTL now compiles and runs on BOTH backends and
  asserts parity, instead of QBE only. This brings sysutils, collections,
  streams, strutils, tstringlist, tcomponent, threading, arc and the rest of the
  RTL/stdlib suites onto the native backend for the first time. New helpers
  AssertRTLRunsOnAll/On/One and CompileAndRunWithRTLOn back this; the QBE-only
  CompileAndRunWithRTLDebug now delegates to the dual-backend *On implementation.

One pre-existing native bug remains (EmitSretCall mishandles a record-by-value
argument that is itself a record-returning call result, e.g.
DateAddDays(MakeDate(...), 5) — the v0.11.0 release crashes too). That single
case is pinned to QBE via CompileAndRunWithRTLQBEOnly with a tracked TODO; every
other RTL/stdlib e2e now runs on both backends.

All three fixpoints green; 3480 tests pass.
2026-06-18 02:23:17 +01:00
Graeme Geldenhuys f39e7e1553 fix(native): interface-sret method calls with >5 args + sret frame layout
Two related native x86-64 bugs prevented an interface-returning (sret) method
from being called with more register-eligible arguments than the ABI passes in
registers:

1. Caller side: the three sret call emitters (EmitIntfSretCall,
   EmitIntfSretMethodCall, EmitClassIntfSretMethodCall) popped every pushed arg
   slot into a System V integer register and raised once the index exceeded 5,
   instead of spilling the overflow to the stack. A shared EmitSretRegArgs helper
   now loads the register-mapped slots and relocates the overflow slots to the
   stack in ABI order, returning the byte count the caller reclaims after the
   call.

2. Callee side: BuildFrame classified FSretFunc only when allocating the Result
   slot, which runs AFTER the parameter-register accounting loop. The loop
   therefore read a stale FSretFunc (left over from the previous function) and,
   for an sret-returning class/interface method (hidden buffer in %rdi AND Self
   in %rsi — two registers), mis-placed the first stack-passed parameter as the
   last register parameter. The sret classification is hoisted above the param
   loop, and the register count now adds the sret buffer and Self independently.

Both backends now agree (IFoo.Make(1..6) returns 21). Adds three dual-backend
e2e tests. All three fixpoints green; 3480 tests pass.
2026-06-18 02:02:46 +01:00
Graeme Geldenhuys 66ff63e73d fix(semantic): resolve same external in two units (was "Undeclared function")
When two units each privately declared the same C function via
`external name '...'` in their implementation sections, the second unit failed
with "Undeclared function" at the call site.

Root cause: FProcIndex is global across all compiled units, but impl-to-forward
matching used a bare FProcIndex.IndexOf.  The second unit's external matched the
FIRST unit's already-indexed decl, so it was treated as that unit's
implementation and was never defined into its own scope.

Fixes:
- Restrict every impl-to-forward match (and the "interface function has no
  implementation" checks) to the current unit via a new IndexOfProcInUnit
  helper that filters on OwningUnit.  The overload-signature loops gain the same
  OwningUnit guard.
- When an impl pairs with its forward decl, carry the forward's OwningUnit onto
  the impl so the later "has no implementation" scan still finds it.
- Collapse the false "ambiguous overload" that the two same-named externals
  produced in the global overload group: SameExternalDecl treats two external
  decls with the same link name and arity as one function.

This also clears the obstacle that forced the float-const fix to use only
_DoubleToStr; uSemantic and blaise.assembler.x86_64 may now both declare
_StrToDouble safely.

Regression test: TestDuplicateExternalAcrossUnits_Compiles in
cp.test.e2e.sepcompile (two units declare `external name 'strlen'`, one
invocation, compile + run + assert stdout).

Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
3477 tests, 0 failures with the QBE fixpoint binary.
2026-06-18 01:30:21 +01:00
Graeme Geldenhuys 9844b38e56 fix(const): use ABI-safe RTL helper for float-const folding (was libc snprintf)
RawDoubleToStr formatted folded float constants via libc snprintf, declared
in Pascal with a fixed Double parameter.  snprintf is genuinely variadic; the
SysV x86-64 ABI requires %al to hold the XMM-register count for variadic calls,
and neither backend emits that setup (QBE only for its '...' syntax; the native
EmitCall never sets %al).  With %al garbage, glibc could read the double from
the wrong place and emit a wrong string -- an environment-dependent
miscompilation of folded float constants that passed locally but failed on the
CI runner (TestConstExpr_FloatParens_Folds expected d_9; TestConstExpr_
NegativeFloat_Folds expected d_-3).

Replace the variadic snprintf call with the RTL's pure-Pascal _DoubleToStr
(FormatFloat(V,15) == %.15g), which is ABI-safe.  strtod is not variadic and is
kept as-is.

Add two e2e regression tests (TestRun_FloatConstFold_Parens /
_NegativeMul) that compile AND run folded-float programs and assert stdout.
The IR-only tests could not catch a host-ABI-dependent fold.

Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
3476 tests, 0 failures with the QBE fixpoint binary.
2026-06-18 01:17:57 +01:00
Graeme Geldenhuys d72a7d9d6f chore: begin v0.12.0-dev cycle 2026-06-17 23:25:12 +01:00
Graeme Geldenhuys 87531495a2 docs: update test count to 3474 for v0.11.0 2026-06-17 23:22:43 +01:00
Graeme Geldenhuys 7a9a92aca5 release: v0.11.0
Self-hosting fixpoint verified on 371,292 lines of QBE IR (QBE, native,
and internal-assembler fixpoints all green). This cycle landed 17 language
features and 30 bug fixes: floating-point constant folding, named-constant
and enum-indexed static-array bounds, generic methods, default array
properties, set-of-ordinal types, case-label ranges, and broad codegen and
ARC hardening across both backends. 3474 tests passing.
2026-06-17 23:22:20 +01:00
Graeme Geldenhuys c79c7e015a fix(const): promote integer initialisers to float for Double/Single vars and typed consts
When a variable or typed constant declares a Double/Single type but the
initialiser expression is pure-integer (e.g. `var Y: Double = 2 * 3`),
the semantic pass now promotes the folded integer value to a float string
so the QBE backend emits a valid `d_<value>` literal instead of an empty
`d_`.
2026-06-17 22:58:29 +01:00
Graeme Geldenhuys 8c7f5c9cbc feat(arrays): enum-indexed static arrays in var/type declarations (#114)
Static-array declarations in var and type sections now accept an enum type
as the index: array[TColor] of Integer.  The array is sized 0..N-1 where
N is the enum member count.  Subscript access uses enum ordinal values —
no codegen changes were needed.

Parser: detects array[Ident] (ident followed by ']' with no '..') and
encodes it as 'array[@TEnum] of T' in the type-name string.

Semantic: a new path in FindTypeOrInstantiate resolves the '@' marker,
looks up the enum type, reads Members.Count, and creates a standard
TStaticArrayTypeDesc with bounds 0..Count-1.

This completes the enum-indexed array story — const arrays already
supported this form; var/type declarations now match.

Tests: 4 unit tests + 2 E2E tests (both backends).
Grammar and language rationale updated.
2026-06-17 19:16:12 +01:00
Graeme Geldenhuys 2dcfe8e196 feat(arrays): accept named constants and expressions as static-array bounds (#109)
Static-array bounds now accept named integer constants and compile-time
integer expressions, not only integer literals.  All of these are valid:

  const N = 10;
  type TBuf = array[0..N-1] of Byte;
  var A: array[0..N] of Integer;
  const Days: array[0..N-1] of string = (...);

Parser: ReadConstBoundText collects tokens forming a bound expression
(integers, identifiers, arithmetic operators, parentheses) into a string
embedded in the type name.

Semantic: ResolveArrayBound resolves the bound text — plain integers via
StrToInt, named constants via symbol-table lookup, expressions via
mini-parse + EvalConstIntExpr.  The canonical type name always uses
resolved integer values for cache consistency.

Both the var/type declaration path (FindTypeOrInstantiate) and the
const-array path (BuildConstArrayType / ReadConstArrayDim) are updated.

Tests: 5 unit tests + 3 E2E tests (both backends).
Grammar and language rationale updated.
2026-06-17 19:07:26 +01:00
Graeme Geldenhuys 38c77c8a93 feat(const): fold floating-point constant expressions at compile time (#108)
Constant declarations now accept arithmetic expressions involving float
literals, named float constants, and the '/' operator.  The semantic pass
detects float-containing expressions and folds them to a single Double
value at compile time, storing the result as a string — the same format
used for bare float literals.

The '/' operator always yields a float result even with integer operands,
matching Delphi/FPC semantics (e.g. const X = 10 / 4 produces 2.5).

Parser: extended ConstRhsStartsIntExpr to detect float-led and
slash-containing expressions; added tkSlash to IsConstExprOp.

Semantic: added IsFloatConstExpr (detects float leaves or '/' usage) and
EvalConstFloatExpr (folds via libc strtod/snprintf to avoid a known
self-hosting ABI issue with the Blaise StrToDouble built-in).

Tests: 8 IR unit tests + 4 E2E tests (both backends).
Grammar and language rationale updated.
2026-06-17 18:48:04 +01:00
Graeme Geldenhuys f6d488bca4 feat(sets): support ordinal-based set types — set of Byte / Boolean (#105)
Widen set base types from enum-only to also accept Byte (256-bit jumbo)
and Boolean (2-bit small).  Set literals accept integer literals and
range syntax [lo..hi], expanded at compile time via EvalConstIntExpr.
Include/Exclude and the in operator accept numeric arguments for ordinal
base types.  Both QBE and native backends handle TIntLiteral elements in
set mask computation.

15 new IR unit tests and 4 new E2E tests (both backends via
AssertRunsOnAll).  All 3444 tests pass; FIXPOINT_OK,
NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK.
2026-06-17 18:21:56 +01:00
Graeme Geldenhuys b0499aa454 fix(semantic): repair generic instance methods when type alias precedes impl (#124)
When a named type alias (e.g. TIntHolder = THolder<Integer>) appeared
in the type section before the out-of-line method implementations
(procedure THolder<T>.SetVal), InstantiateGenericRecord cloned the
template methods with Body=nil because LinkGenericClassMethodImpls had
not yet transferred the implementation bodies. Codegen skipped the
bodiless methods, producing undefined-reference link errors.

Add RepairEarlyGenericInstances which runs after LinkGenericClassMethodImpls
in all three analysis paths (AnalyseUnit, AnalyseUnitForExport,
AnalyseBlock). It iterates existing generic record and class instances,
finds methods still missing a body, clones from the now-populated
template, and analyses the repaired methods with correct type-param
scope bindings.
2026-06-17 16:51:26 +01:00
Graeme Geldenhuys 0af9f9f7bf fix(parser): allow p^.field[index] := value subscript after deref (#118)
The statement parser's deref-then-field path (p^.field) unconditionally
expected ':=' after consuming the field name, failing when the field
is an array that needs subscripting.  Now check for '[' before ':='
to handle p^.da[0] := value, matching the existing r.da[0] := value
path.
2026-06-17 15:58:02 +01:00
Graeme Geldenhuys cdd1d0cbe2 fix(codegen): return fixed-size static array by value via sret (#112)
Functions returning a static array >8 bytes now use the sret ABI on
both QBE and native x86-64 backends: the caller passes the destination
buffer as a hidden first argument, the callee writes through it, and
returns void.  Previously only the first 8 bytes survived (QBE
segfaulted, native rejected the function outright).

Key changes:
- QBE: IsRecordCall, signature, prologue, epilogue, and assignment
  dispatch all extended to handle tyStaticArray alongside tyRecord
- Native: allowed return type guard, BuildFrame FSretFunc, sret
  call-site dispatch, and subscript-assign Result indirection all
  extended for tyStaticArray
2026-06-17 15:50:22 +01:00
Graeme Geldenhuys d4a066aa93 fix(parser+semantic): typed array const with named type alias (#113)
A `const A: TArr = (10, 20, 30)` where TArr is a named alias for a
static array type failed to parse as an array constant.  The parser
now detects the value-list shape via lookahead when CD.TypeName is set,
and the semantic pass resolves the named type to its underlying
TStaticArrayTypeDesc to extract bounds and element type.
2026-06-17 15:33:32 +01:00
Graeme Geldenhuys 69e0ff6baa fix(codegen): Boolean xor emitted as ceqw instead of xor in QBE backend
The tyBoolean branch in EmitExpr intercepted boXor (and boAnd/boOr)
before the generic integer path could handle them.  The else clause
in the comparison-operator switch defaulted to ceqw (compare-equal),
which is the exact opposite of xor: True xor True produced True
instead of False.

Added boAnd/boOr/boXor cases to the tyBoolean integer-comparison
switch so they emit the correct bitwise operations.  The native
backend was already correct (no separate tyBoolean interception).

Fixes GitHub #123.

3411 tests pass.  All three fixpoints verified.
2026-06-17 15:16:59 +01:00
Graeme Geldenhuys a82269f48c feat(stdlib): add Items default property to TDictionary and TOrderedDictionary
Add GetItem/SetItem methods and an Items[Key: K] default property to
both TDictionary<K,V> and TOrderedDictionary<K,V>.  This enables the
idiomatic d[key] syntax for reads and writes:

  D['one'] := 1;
  WriteLn(D['one']);

GetItem halts on missing key (consistent with index-out-of-bounds in
TList).  SetItem delegates to Add (add-or-update semantics).

The compiler already had full default property infrastructure (parser,
AST, semantic, both codegens) — only the stdlib declarations were
missing.  This is the first non-Integer-typed default property in the
codebase, exercising the generic index type path end-to-end.

Tests: 2 IR codegen tests (GetItem/SetItem emission) + 3 e2e tests
on both backends (integer keys, string keys, update-via-bracket).
3407 tests pass.  All three fixpoints verified.
2026-06-17 15:05:02 +01:00
Graeme Geldenhuys 2b745eaeb2 fix(codegen): native sar, unsigned compare, mixed-float comparison + 32 e2e tests
Three native backend bugs found and fixed by the IR-emit audit:

1. `sar` operator raised ENativeCodeGenError — added boSar -> sarq.
2. UInt64 comparison used signed setl/setg/setle/setge — max UInt64
   compared as negative. Fixed: unsigned types and pointer-like types
   now emit setb/seta/setbe/setae.
3. Mixed Single/Double float comparison in EmitCondBranch and
   EmitExprToEax determined IsS (single-precision) by checking only
   the left operand. A Single compared to a Double literal loaded
   double-width data but compared with ucomiss. Fixed: require BOTH
   operands to be Single for IsS, and add EmitXmm0WidthAdjust calls.

New e2e test files (all both-backend via AssertRunsOnAll):
- cp.test.e2e.external (10): FFI name binding, narrow-int return
  masking, Single param/return, record-by-value, name alias
- cp.test.e2e.genericintfs (7): generic interfaces, two type params,
  IEqualityComparer/IComparer, string type arg, polymorphic dispatch
- cp.test.e2e.gaps (15): packed records, sar/shr, UInt64 operations,
  [Unretained] back-references, generic records, published RTTI
  MethodAddress, multi-arg WriteLn

3402 tests pass. All three fixpoints verified.
2026-06-17 14:13:24 +01:00
Graeme Geldenhuys 4e0e0a90a9 refactor(testing): use Cls.Create() instead of ClassCreate() builtin
Dogfood the new metaclass constructor dispatch syntax in the test
runner — the primary real-world consumer of metaclass construction.
2026-06-17 13:32:53 +01:00
Graeme Geldenhuys 215082afc3 feat(codegen): implicit-virtual constructor dispatch via metaclass
Constructors are now auto-slotted into the vtable by the semantic pass.
When a constructor is called through a metaclass-typed variable
(C.Create(args)), the compiler emits _ClassCreate for allocation
followed by a vtable-indirect call to the most-derived constructor
body. Direct calls (TFoo.Create) remain fully static.

Both backends (QBE and native x86-64) emit correct dispatch for:
- MetaclassVar.Create(args) syntax
- ClassCreate(MetaclassVar, args) builtin
- Zero-arg and multi-arg constructor signatures

This is a layout-changing commit (adds constructor vtable slots).
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK,
3370 tests pass.
2026-06-17 10:07:47 +01:00
Graeme Geldenhuys 1753b5d2a8 fix(codegen): native scalar field of a record-call result as a call argument
The native x86-64 backend corrupted other arguments when a SCALAR FIELD of a
record-returning call result was read inline as a call argument — e.g.

    Check('vals', 1, MakeR().A);     // 'vals' lost on native; QBE was fine

Reading MakeR().A went through EmitExprToEax, which materialises the call's
sret buffer (a stack subq) and never frees it (the frame epilogue
reclaims it for a standalone expression).  Inside the argument push/pop
sequence that stray allocation shifted %rsp, so an already-pushed argument
(typically the const-string) was reloaded from the wrong slot and lost —
crashing later in _StringAddRef when the callee retained the garbage pointer.

Fix: in the chained-field-read path, when the base is a record-returning call
and the field is scalar, allocate the sret buffer, read the field out of it,
then FREE the buffer so the expression nets zero stack change and the argument
protocol stays balanced.  Record/static-array fields are excluded — they
return an address INTO the buffer, whose lifetime must outlive the read.

Regression test (both backends) in cp.test.e2e.recordret; the inline
HostTarget().OS assertion in cp.test.toolchain (previously worked around) now
exercises the fixed path directly.

FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite OK (3363).
2026-06-17 08:34:16 +01:00
Graeme Geldenhuys c266a8ee74 feat(cli): reject --emit-ir/--emit-asm with an incompatible explicit backend
--emit-ir prints QBE IR and --emit-asm prints native assembly; PickTopDriver
routes each output mode to the backend that produces it, ignoring --backend.
That silent override is acceptable for the default backend, but when the user
EXPLICITLY passes a --backend that cannot produce the requested output it was
silently honoured anyway — e.g. '--backend native --emit-ir' quietly emitted
QBE IR, ignoring the requested native backend.

Track whether --backend was given explicitly (TFrontEndOpts.BackendExplicit)
and, after backend selection, reject the incompatible combinations:

  --backend native --emit-ir   -> error (use --emit-asm)
  --backend qbe    --emit-asm  -> error (use --emit-ir)

The default-backend cases are unchanged: --emit-ir with no --backend still
resolves to QBE, and --backend qbe --emit-ir / --backend native --emit-asm
remain valid.  This matches the driver-owns-its-flags principle of the option
refactor — a backend-specific output mode belongs to its backend.

Tests in cp.test.cli cover both rejections and both still-valid paths.
FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite OK (3362).
2026-06-17 08:27:54 +01:00
Graeme Geldenhuys 88db7f3b23 ci: run CLI-contract & internal-assembler tests; surface skips
The TCLIContractTests and TInternalAsmE2ETests suites resolve their
compiler binary from BLAISE_QBE_COMPILER, falling back to the
/tmp/fp_blaise{3,2} fixpoint binaries.  Those only exist after a local
fixpoint.sh run, so in CI both suites silently Ignore'd ~24 tests while
the run still reported green (25 ignored in CI vs 1 locally).

Two changes:
  - bootstrap.yml: set BLAISE_QBE_COMPILER to the bootstrapped -pre
    binary so both suites actually run in CI.
  - CompilerAvailable() in each suite now prints a one-time StdErr note
    when it skips, so a missing toolchain is loud instead of a silent
    green with hidden ignores.
2026-06-17 08:13:50 +01:00
Andrew Haines a0e0fd567b feat(toolchain): host-aware tool resolution with spec + prefix env
Generalise external-tool lookup so a cross/host build finds the right
binaries:

- target: HostTarget detects the host OS from ParamStr(0) (a '.exe' suffix
  means Windows, incl. under wine) instead of assuming linux; add
  ExecutableExtension(target).
- uToolchain: TToolSpec (ordered candidate basenames, a cross-triple
  prefix, and a HostTool flag) + ResolveSpec resolve a tool by env
  override, then $BLAISE_TOOLCHAIN_PREFIX + name, then the cross prefix on
  $PATH, then bare name — each probe appending the host exe extension.
  HostTool distinguishes host tools (run anywhere, target via flags) from
  target tools (must be the target's own binary; no host fallback when
  cross). TrySingleName now uses the real host extension.
- driver: TBackendDriver.DescribeTools(target) declares a backend's tools
  (base = the shared linker: cc/clang or the mingw cross-linker for a
  Windows target); ToolPath resolves one by name; LinkViaToolchain links
  through it. Self.DescribeTools for virtual dispatch.

Native sret codegen fixes (folded in so this commit bootstraps): the
toolchain code is the first in the compiler's own source to build a record
via a function whose body does SetLength on a dynamic-array Result field,
and to pass a record-returning method result directly as a const-record
argument.  Both miscompiled on the native x86-64 backend (QBE was correct),
so a native-compiled compiler could not bootstrap at this commit:

- EmitLValueSlotAddr: an l-value field slot on a local record used a raw
  leaq, but the sret-function Result slot holds a POINTER — route through
  EmitLocalRecordBase so the pointer is loaded before adding the field
  offset (fixes SetLength(Result.DynField, N)).
- EmitArgHoist: a record-returning method-call argument was materialised
  via EmitExprToEax, which has no sret path for method calls — allocate the
  buffer and drive EmitMethodSretCall directly, mirroring the function path.

Tests:
- cp.test.toolchain: ResolveSpec resolution order, host-tool vs target-tool
  cross-suppression, fallback naming, and the host/extension helpers.
- cp.test.e2e.recordret: regressions for both native sret fixes (both
  backends).
- cp.test.codegen: native backend also emits the _BlaiseInit startup call
  (the existing assertion only covered QBE).

A third, pre-existing native sret bug (sret-call result + const-string arg
in the same call) surfaced while writing the toolchain tests; it is
independent of this work and logged in bugs.txt.

Backend-neutral feature; native fixes are x86-64-only.
2026-06-17 00:44:35 +01:00
Andrew Haines 4e61cb1e8f fix(rtl): run one-time RTL setup via a _BlaiseInit startup hook
A unit's initialization section only runs when that unit is compiled
into the program's own translation unit; units linked from a prebuilt
archive never get their _init called.  blaise_weak relies on its
initialization section to set up the weak-reference table mutex, so for
archive builds that setup never happened.  A zero-filled mutex object is
a valid unlocked mutex on some platforms, which kept this latent — but
that is not guaranteed everywhere.

Add a _BlaiseInit RTL entry point that performs the one-time setup
(currently the weak-table mutex, guarded to stay idempotent) and have
each backend's $main emit a call to it right after _SetArgs, before any
user code runs.  The mutex is now initialised explicitly instead of by
relying on a zero-filled object happening to be valid.

Regression: cp.test.codegen TestMain_CallsBlaiseInit asserts the call.
2026-06-17 00:10:53 +01:00
Graeme Geldenhuys 2f54707a63 feat(strutils): clean literal replace API — Replace (first) + ReplaceAll
Replace the FPC/Delphi replace surface (ReplaceStr, ReplaceText, and the
proposed StringReplace + TReplaceFlags set) with two self-explanatory
functions:

  Replace(S, Old, New)    — replaces the FIRST occurrence
  ReplaceAll(S, Old, New) — replaces EVERY occurrence

Both are case-sensitive and literal (non-pattern).  This sheds the legacy
redundancy: ReplaceStr/ReplaceText were two names for one operation split by
a 'String vs Text' distinction that is not obvious, and Delphi's StringReplace
encodes the same two booleans (all-vs-first, sensitive-vs-insensitive) as an
awkward flag-set.  Modern languages (Go, Python, Java, Rust) use a plain
first/all split with no case flag.

Case-insensitive replace is expressed by lower-casing the inputs with the
built-in LowerCase/UpperCase before calling Replace/ReplaceAll; a
case-*preserving* insensitive replace and pattern/regex matching are deferred
and recorded in docs/future-improvements.adoc.

The internal worker keeps the existing TStringBuilder-based loop; it gains a
FirstOnly flag and drops the now-unused case-fold path.

No callers of the removed functions existed anywhere in the tree.  IR/semantic
and e2e tests updated to the new names; both fixpoints green (FIXPOINT_OK,
NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK); full suite OK (3346 tests).
2026-06-16 23:49:30 +01:00
Graeme Geldenhuys 712263abd4 feat(rtl): integer div/mod by zero raises catchable EDivByZero
Integer div and mod by zero previously trapped in hardware (SIGFPE),
uncatchable even inside try/except — a single bad divisor terminated the
process.  Both backends now emit a divisor-zero guard before each integer
idiv that raises a catchable EDivByZero exception via the normal exception
machinery, matching standard Pascal/Delphi semantics.

- SysUtils: add EDivByZero = class(Exception) and the _RaiseDivByZero helper
  that raises EDivByZero('Division by zero').
- QBE + native x86-64: emit a divisor==0 check before integer div/rem
  (32- and 64-bit); on zero, call SysUtils__RaiseDivByZero (which longjmps).
- The guard is emitted only when SysUtils is in scope (EDivByZero resolves
  through the symbol table); without SysUtils, division traps as before.
  This mirrors Delphi's placement of EDivByZero in System.SysUtils and keeps
  the always-linked runtime free of stdlib class machinery.
- Tests: in-process e2e proves the guard does not disturb normal division on
  both backends; shell-out cli tests (real compiler + linked stdlib) prove
  catchable div/mod-by-zero on both backends.
- Rationale recorded in language-rationale.adoc.

All three fixpoints green (FIXPOINT_OK, NATIVE_FIXPOINT_OK,
NATIVE_INTERNAL_OK); full suite OK (3345 tests).
2026-06-16 23:04:56 +01:00
Graeme Geldenhuys 5a15d4d15e fix(codegen): string relational operators (< > <= >=)
Relational comparison of strings was unhandled in both backends — only =
and <> were special-cased. The operands fell through to the integer
comparison path:

- QBE emitted a comparison that aborted QBE itself
  (amd64/isel.c: selcmp: Assertion `k != Kw' failed).
- Native silently compared the string POINTERS, giving wrong results
  (e.g. 'b' > 'a' returned False).

Both backends now route <, >, <=, >= through the existing _StringCompare
RTL helper (strcmp-like: <0 / 0 / >0) and compare its result against 0
with the matching signed condition.

Verified the full truth table ('abc' < 'abd', 'b' > 'a', <=/>= equal
cases, empty-string ordering) on both backends. Adds
TE2EStringOpsTests.TestRun_StringRelational_Order.
2026-06-16 20:04:55 +01:00
Graeme Geldenhuys 6c088024ec fix(native): spill implicit-Self method calls with >6 register slots
An implicit-Self method call in statement position (an unqualified
`Sink(...)` inside a method body) used a register-only fast path that
pushed every argument and popped it into SysVArgRegs64[I+1] — with no
check that the arguments fit the 6 System V integer registers. Self
takes %rdi, leaving 5 registers for arguments, so a call with 6+ value
args (Self + 6 = 7 slots) indexed SysVArgRegs64[6], one past the
[0..5] array. The out-of-bounds read returned an adjacent data-section
value (a garbage string pointer) that was later concatenated, crashing
_StringConcat — which is exactly how the native self-compile segfaulted.

(This was the "latent native self-compile crash" logged in bugs.txt. It
surfaced when a property-accessor helper shifted codegen enough to route
a 6+-slot implicit-Self call through this path; the workaround there
sidestepped it, but the bug was in the call-emission path itself.)

The implicit-Self statement-call path now guards the register fast path
with `CountArgSlots + 1 <= 6` and, when the slots overflow, spills the
surplus to the stack — mirroring the correct handling already present in
EmitMethodCallStmt for qualified method calls.

Also adds SysVArg64(), a bounds-checked accessor for the System V
integer-arg registers; every dynamic SysVArgRegs64[expr] read now goes
through it, so any future over-allocation raises a clear codegen error
("System V integer arg register index N out of range") instead of
silently reading past the array and miscompiling.

Verified: the pre-release segfaults native-compiling the minimal repro
(implicit-Self method, 6 args); the fix compiles and runs it correctly
(21) on both backends, and the full native self-compile succeeds. Adds
TE2ENativeTests.TestRun_Native_ImplicitSelfMethod_{Six,Seven}Args.
2026-06-16 19:39:03 +01:00
Graeme Geldenhuys d15501af7b fix: correct 0-based string iteration in FNV-1a hash loops
Both FNV-1a hash helpers carried over FPC's 1-based string indexing,
which is wrong in Blaise's 0-based world: each loop skipped the first
byte (index 0) and read one byte past the end (index Length).

  - uDebugOPDF.FNV1a32          (32-bit; OPDF type IDs)
  - uUnitInterfaceIO.ContentHashFnv1a64 (64-bit; .bif source hash)

Both now use 'for B in S' byte iteration, which is bounds-safe and is
also the natural iteration for FNV-1a (defined over the byte stream).
Output validated against a reference FNV-1a32 implementation.

No layout/ABI impact: OPDF type IDs are opaque internal tokens, and the
.bif SourceHash is a staleness cache key. Existing .bif caches will
mismatch once and trigger a one-time recompile, which uUnitLoader
already handles.
2026-06-16 18:50:56 +01:00
Graeme Geldenhuys 908f3503b9 docs: add CI build status badge to README 2026-06-16 18:19:10 +01:00
Graeme Geldenhuys ea8dc01434 ci: bump deprecated actions to Node 24 majors
actions/checkout v4->v6, actions/cache v4->v5, actions/upload-artifact
v4->v7. Silences the Node.js 20 deprecation warning; cache format is
compatible across these majors so incremental resume still works.
2026-06-16 18:09:03 +01:00
Graeme Geldenhuys 85e2f28b0c ci: expose vendored QBE to the compiler via BLAISE_QBE
The compiler shells out to 'qbe' and resolves it via the BLAISE_QBE env
var, then PATH (uToolchain.ResolveQBE). The QBE= make variable passed by
rolling-bootstrap.sh only covers the script's own direct qbe calls, not
the compiler's internal invocation when building the RTL. On the runner
qbe is built at vendor/qbe/qbe but not on PATH, so the RTL build failed
with 'qbe error (exit 127)' on the very first replay step.

Set BLAISE_QBE (and prepend vendor/qbe to PATH) after building QBE so all
subsequent steps can find it.
2026-06-16 17:33:25 +01:00