Commit graph

720 commits

Author SHA1 Message Date
Graeme Geldenhuys 4e90165b73 feat(semantic): reject variables that shadow a type name (#102)
A variable declaration sharing its identifier with any visible type name
compiled without complaint — e.g. an Iface interface and an iface variable
in the same program (Pascal is case-insensitive, so these are one
identifier). The variable silently shadows the type, which is confusing
and almost always a mistake.

Blaise already rejected var-vs-var, var-vs-const and type-vs-type clashes;
var-vs-type slipped through because type decls are registered in the
block's enclosing scope (so they outlive the block scope) while var decls
are registered one scope deeper, hiding the type from FTable.Define's
duplicate check. AnalyseVarDecls now looks up the name in the type
namespace (FTable.FindType, which honours uses-chain visibility) and
rejects any match.

The rule is stricter than FPC mode objfpc, which permits shadowing a
built-in or outer-scope type (var Integer: Int64 compiles in FPC). Blaise
rejects the whole class — same-block, outer-scope, imported, or built-in —
to eliminate the confusion rather than carry FPC's footgun. Recorded in
docs/language-rationale.adoc with the alternatives considered.

FIXPOINT_OK + NATIVE_FIXPOINT_OK; full suite OK (3190 tests, 5 new).
2026-06-15 16:38:19 +01:00
Graeme Geldenhuys bf63d7419f fix(codegen): three bugs compiling units with generics + float code (#107)
Compiling the issue's waveCalcs unit surfaced three independent codegen
bugs, two of which the reporter saw directly and a third uncovered while
verifying the fix on the native backend.

1. QBE: a generic FUNCTION declared in a unit but never instantiated was
   code-generated as a template — its T-typed locals have no resolved
   type, so codegen raised "Variable 'MaxValue' has no resolved type —
   semantic pass required". EmitStandaloneDefs skipped templates, but the
   unit-emission path (GenerateUnit/AppendUnit -> EmitFuncDef) did not.
   Guard centrally in EmitFuncDef: skip decls with TypeParams <> nil; only
   concrete instances (from GenericFuncInstances) are emitted.

2. native: a float comparison nested inside a short-circuit and/or — e.g.
   (P > 1.0) or (P < -1.0) — reaches EmitExprToEax rather than
   EmitCondBranch. The integer comparison path there emits its operands via
   EmitExprToEax, which has no TFloatLiteral handler, so it raised
   "unsupported expression form TFloatLiteral". Add a float-comparison
   branch to EmitExprToEax that materialises the 0/1 result via ucomisd +
   setcc, mirroring EmitCondBranch.

3. native: a local float const (const TwoPi = 6.28; inside a function) was
   lowered as a load from a symbol named after the const, which is never
   emitted — link error "undefined reference to TwoPi". The const has no
   storage; inline its value from ConstString via .rodata in
   EmitExprToXmm0, as a TFloatLiteral already does.

Regression tests:
- TestUninstantiatedGenericFunc_InUnit_Compiles (sepcompile e2e)
- TestRun_Native_FloatCompareInOrAnd (runs on both backends)
- TestRun_Native_LocalFloatConst (runs on both backends)

FIXPOINT_OK + NATIVE_FIXPOINT_OK; full suite OK (3185 tests).
2026-06-15 16:22:29 +01:00
Graeme Geldenhuys 267e8db702 fix(rtl): WriteDecimal mishandles Low(Int64), printing only '-'
WriteDecimal negated N before extracting digits. For Low(Int64) =
-9223372036854775808 the negation overflows (no positive counterpart in
two's complement) and leaves the value negative, so the 'while AbsN > 0'
loop ran zero times and only the sign was emitted.

Extract digits while keeping the value negative — the negative range
reaches Low(Int64) — and negate each remainder. Also fixes
Low(Integer), which routes through the same function.

Adds e2e regression TestRun_Int64_MinValue.
2026-06-15 16:05:37 +01:00
Graeme Geldenhuys fbb5a675aa feat(format): float Format() support on the native x86-64 backend
Mirror the QBE backend's tag-2 float handling in EmitFormatCall: a float
argument stores tag 2, evaluates to %xmm0 (widening a Single to Double), and
bit-copies the 64-bit pattern into the value slot via movq — the native
equivalent of QBE's 'cast'.

E2E tests (cp.test.e2e.stringops) exercise %f default/precision/width (both
justifications), %e, %g, and a float interleaved with int+string args, all via
AssertRunsOnAll so they run on both the QBE and native backends.
2026-06-15 00:58:43 +01:00
Graeme Geldenhuys 7c26598bb8 feat(format): float support in Format() — %f/%e/%g with width/precision (QBE)
Format() previously handled only %d, %s and %%; any float directive rendered
verbatim, and on the QBE backend a float argument failed to compile because the
double value was stored with 'storel <d-temp>', which QBE rejects.

RTL (blaise_float.pas): add _FormatFloatSpec(V, Spec, Prec) — a precision-aware
renderer for %f/%F, %e/%E and %g/%G built on the existing Grisu1 digit
generator, with round-half-up at the precision boundary (including all-nines
carry) and printf-style exponent formatting.

RTL (blaise_str.pas): rewrite _StringFormatN to parse the full
%[-][width][.prec]<conv> specifier syntax, render each argument (int/string/
float) into a growable buffer, and apply field-width space padding. A new
arg tag (2) carries the IEEE-754 binary64 bit pattern for float values.

QBE codegen: float Format arguments now emit tag 2 and reinterpret the double
bits to an integer via 'cast' before storel, fixing the compile error.

Tests: IR unit tests assert tag-2 emission and the bit cast. E2E coverage and
the native backend follow in the next commit.
2026-06-15 00:58:37 +01:00
Graeme Geldenhuys d3dbebbe85 test(sets): extract set-of e2e tests to cp.test.e2e.sets, run on both backends
The general 'set of' operation e2e tests (Include/Exclude, in, union/intersect,
valued constant, literal argument, equality, for-in, and the 33..64-member
Set64 boundary cases) lived in cp.test.e2e.misc and ran on QBE only via
CompileAndRun. They test named-set operator semantics, distinct from the
inline-set syntax covered by cp.test.e2e.inlineset, so they move to a new
purpose-named unit cp.test.e2e.sets (TE2ESetOpsTests) paralleling the IR-level
cp.test.sets — and are promoted to AssertRunsOnAll so each now runs on both the
QBE and native backends, closing a native-coverage gap.

Pure relocation + both-backend promotion: full suite unchanged at 3172 tests
(11 set tests moved misc -> sets). README test count updated 2768 -> 3172.
2026-06-15 00:34:39 +01:00
Graeme Geldenhuys fa853e0eb2 feat(sets): jumbo sets — set of enum up to 256 members
Extend set of <enum> from a 64-member cap to 256, Java-EnumSet style. Sets
of 64 members or fewer keep the existing single-register bitmask (QBE w/l);
sets of 65..256 members ('jumbo') become an inline byte-array bitmap of
ceil(N/8) bytes, treated as a value aggregate (passed by reference, returned
via sret, memset/memcpy), with operations performed by new RTL helpers.

Representation (uSymbolTable): TSetTypeDesc.IsJumbo (BitCount > 64) and
RawByteSize; RawSize/ByteSize/AllocAlign sized accordingly. TSymbol gains
ConstSetBytes/ConstSetQbe for jumbo constants (can't fit an Int64 mask).

RTL: new runtime/src/main/pascal/blaise_set.pas — _SetIn/_SetInclude/
_SetExclude/_SetUnion/_SetInter/_SetDiff/_SetEqual/_SetCopy over byte-array
bitmaps (overlap-safe). Wired into runtime/Makefile.

Semantic: the four >64 caps become >256. AnalyseSetConstDecl folds a jumbo
const to a byte array. The anonymous set for 'X in [a,b,c]' is sized to the
largest listed ordinal (when constant), not the full enum — keeping the
common low-ordinal membership test (incl. the compiler's own TokenKind
tests) on the fast register path. This also fixes a latent miscompile: the
old fixed-l representation silently dropped any listed ordinal >= 64.

Codegen (both backends): jumbo branches for literal, in, +/-/*, =/<>,
Include/Exclude, for-in, assignment, params (pmJumboSetValue ABI), and sret
returns. The register 'in' gained a range guard for literal-sized sets.
Native reserves two 32-byte scratch slots per frame (and .bss in main) for
set-op/literal result buffers. QBE adds IsAggregateAddrType so jumbo sets
ride the record/static-array address paths and are never promoted.

OPDF: no format change — recSet SizeInBytes (1 byte) already covers <=32.

Verified: FIXPOINT_OK and NATIVE_FIXPOINT_OK; full suite (3172 tests) green
built by the stage-2 binary, the QBE fixpoint binary, and the native
fixpoint binary; bif-coverage OK. Tests: cp.test.jumboset (12 IR) and
cp.test.e2e.jumboset (6 e2e, both backends via AssertRunsOnAll); the
>256-member rejection test in cp.test.sets updated. Docs: grammar.ebnf and
language-rationale set-type sections updated for the 256 cap and literal
sizing.

Closes the design discussion behind #81.
2026-06-14 23:23:19 +01:00
Graeme Geldenhuys 2708a6209e feat(const): compile-time integer constant expressions (#96)
A const declaration may now take a compile-time integer expression on the
right-hand side, not just a single literal:

  const A = 2 * 3;            // 6
        B = 2 + 3 * 4;        // 14 (precedence)
        C = (2 + 3) * 4;      // 20 (parentheses)
        D = 100 div 7;        // 14
        E = Base * 2 + 1;     // references a prior const

Previously only single literals and a flat, single-precedence bit-op chain
(a or b or c) were accepted; 2 * 3 failed with "Expected ';' but got '*'"
and (2 * 3) with "Expected numeric or string constant".

Parser: when the const RHS starts an integer expression (a leading '(', an
integer literal or leading-minus literal followed by a binary operator, or an
ident followed by a numeric operator), parse it with the existing
full-precedence ParseExpr into a normal expression AST stored on
TConstDecl.IntValueExpr. A bare literal keeps the fast IntVal path; '+' on an
ident is left to the string-concat path for backward compatibility.

Semantic: EvalConstIntExpr recursively folds the AST to an Int64, resolving
named-constant references against the symbol table. Folding in the semantic
pass (after all consts are registered) is what makes forward references work
regardless of declaration order. Wired into both the const-block and the
initialised-variable (var G = expr) fold sites. Operator precedence and
grouping come from the AST shape, superseding the precedence-unaware bit-op
token chain.

Scope: folds to integer. Float constant arithmetic and using a const as a
static-array bound remain separate, pre-existing limitations (noted in the
rationale).

Tests: eight IR/fold cases in cp.test.constants (multiply, parens, precedence,
div/mod, named-ref, unary minus, mixed arith+bitwise); one e2e case on both
backends in cp.test.e2e.misc. Docs: grammar.ebnf gains the ConstIntExpr rules;
language-rationale gains a Constant Expressions section.
2026-06-13 19:14:14 +01:00
Graeme Geldenhuys 9a511a22e2 feat(const): multi-dimensional array constants
Range-indexed array constants now support multiple dimensions in both the
comma form (array[0..1, 0..2] of Integer) and the equivalent nested form
(array[0..1] of array[0..1] of Integer), with nested initialiser groups
((1,2,3),(4,5,6)). Previously the const-declaration parser had its own
single-dimension array-type and value-list parsers, so any multi-dim const
failed at the type (Expected ']' but got ',') or the nested value group.

Parser: ParseConstArrayType walks one-or-more 'array[...] of' headers,
reading comma-separated ranges per header and recursing on a nested 'array';
each dimension's bounds go to CD.ArrayDimLows/ArrayDimHighs. The value
parser is now recursive (ParseConstArrayGroup/ParseConstArrayScalar),
flattening nested groups into ArrayElements in row-major order. Dim-0 bounds
mirror onto the legacy ArrayLowBound/ArrayHighBound so the single-dim path
is unchanged.

Semantic: BuildConstArrayType builds the nested static-array type
innermost-first and validates the flat element count equals the product of
dimension extents. Shared by the program/unit and class-const sites.

Codegen: the QBE emitter already lays a flat row-major blob, so it needs no
change for integer/string elements. The native emitter now drills through
nested static-array types to the innermost scalar to pick the element
directive.

Tests: parse/semantic/IR in cp.test.constants; e2e (comma, nested, 3-D) on
both backends via AssertRunsOnAll in cp.test.e2e.staticarray. Docs:
grammar.ebnf gains ConstArrayType/ConstArrayValue rules; language-rationale
updated (the stale 'enum index only' constraint corrected to document
range and multi-dimensional const arrays).
2026-06-13 18:48:28 +01:00
Graeme Geldenhuys 5fa44b33a5 fix(codegen): float-typed array elements on both backends
Storing a Double or Single into a dynamic- or static-array element was
miscompiled. The QBE backend's element-store selected storel/storew for
any non-integer element, so a double element got 'storel %d-temp' and
QBE rejected it (invalid type for first operand in storel). The native
backend evaluated the RHS through the integer path (EmitExprToEax),
which raised 'unsupported expression form TFloatLiteral', and even past
that would have stored from a GPR rather than %xmm0; the float element
READ had the matching gap (unsupported float expression form
TStringSubscriptExpr).

QBE: add tyDouble->stored / tySingle->stores to both the dyn-array and
static-array element-store cases, and coerce the value temp to the
element width via CoerceArg (a 1.5 literal is a 'd' temp; a Single slot
needs truncd first).

Native: in the element store, evaluate float RHS via EmitExprToXmm0,
width-adjust to the element type, spill through %rax (x86 has no push for
xmm), then reload and store with movsd/movss. Add float element-read
cases to EmitExprToXmm0 mirroring the integer subscript address maths.

Tests: three e2e cases in cp.test.e2e.staticarray (dyn double, dyn
single, static double) run on both backends via AssertRunsOnAll.
2026-06-13 18:07:42 +01:00
Graeme Geldenhuys 12ebcbe09f feat(parser): clear diagnostics for packed/bitpacked array
FPC's bitpacked array (bit-level packing) and packed array have no
Blaise equivalent. Previously bitpacked fell through to tkIdent and
produced a confusing "Expected ';'" error, while packed array gave a
generic "may only precede 'record'" message.

Catch both in the type-declaration RHS and emit an actionable error
that points users at the supported idiom (set of for packed boolean
flags). packed record is unaffected.

Adds five tests to cp.test.packedrecord: parse-error coverage for both
keywords plus message-content assertions that the error names the
keyword and suggests set of.
2026-06-13 17:56:22 +01:00
Graeme Geldenhuys f830a9f1ac feat(params): array of const (heterogeneous variadic parameters)
A parameter declared 'array of const' accepts a single call-site bracket list
of mixed-type values, boxed into an array of the intrinsic record TVarRec and
passed via the existing open-array ABI:

  procedure Log(args: array of const);
  ...
  Log([42, 'hi', 3.5, True]);

This is the one loosely-typed-passing mechanism Blaise adopts (see the
rationale section); untyped params, varargs, and Variant remain omitted.

- RTL/builtins: TVarRec is registered as a compiler-intrinsic record
  { VType: Byte; VValue: Pointer } (16-byte layout, mirroring TMethod), with
  vt* discriminant constants - all available with no uses clause, matching
  Delphi's auto-available System.TVarRec.  Blaise has no record variant parts,
  so the callee reads each element by reinterpret-casting the single VValue
  slot (Integer(v.VValue), string(PChar(v.VValue)), PDouble(v.VValue)^, ...).
- Parser: 'array of const' parses as an open array whose element is TVarRec.
- Semantic: a heterogeneous bracket literal is typed 'array of TVarRec' rather
  than rejected; overload resolution binds it (and homogeneous / empty
  literals) to an array-of-const formal; retyping runs for proc, func, and
  method calls.
- Codegen (both backends): EmitConstArrayLiteral builds one 16-byte TVarRec
  per element, tagging by inferred type.  Borrow semantics (FPC) - strings and
  objects are stored without AddRef.  Doubles are heap-boxed via _BlaiseGetMem
  (vtExtended holds a PDouble) since a double does not fit the pointer slot.
- Native: also fixes a pre-existing gap - reading a float through a pointer
  deref (PDouble^) in EmitExprToXmm0 - needed for vtExtended read-back.

Tests: cp.test.arrayofconst (parser/semantic/IR) and cp.test.e2e.arrayofconst
(compile + run on both backends, including value read-back, empty/homogeneous
lists, and string-variable borrow).  Grammar and language rationale documented.
2026-06-13 17:37:59 +01:00
Graeme Geldenhuys 28bd5cdb38 docs(rationale): position on loosely-typed parameter passing
Record Blaise's stance on the four conflated "pass a loosely-typed value"
features: untyped const/var parameters, array of const, varargs, and Variant.

Blaise adopts only array of const - the one mechanism overloads and generics
cannot replace (arbitrary-length, mixed-type argument lists, as used by
Format-style APIs); it is a compiler-bounded, per-element-tagged call construct
rather than a free-floating dynamic value.

Untyped parameters are omitted on footgun grounds: they implicitly take an
address and erase the type, whereas a typed pointer (PByte / ^T) does the same
job explicitly and keeps type information for indexing/field access. varargs is
omitted as C-interop-only and untagged; Variant is omitted in favour of
overloads, generics, and explicit tagged unions that keep checking at compile
time.
2026-06-13 15:29:20 +01:00
Graeme Geldenhuys 54d7d2a573 feat(types): inline set types in any type position
A set type may now be written inline anywhere a type is expected (var,
parameter, record field, ...), not only in a named type declaration.  The
element type may be a named enumeration or an anonymous enumeration written
in place:

  var
    Days:  set of TWeekday;       // named enum element
    Flags: set of (fA, fB, fC);   // anonymous enum element

Implementation resolves an inline set entirely from its canonical type-name
string, with no parser-side state:
- Parser: ParseTypeName gains a 'set of' branch.  A named element yields
  'set of <Name>'; an anonymous element is encoded verbatim as
  'set of (a,b,c)' (ParseAnonEnumName), self-contained in the type string.
- Semantic: FindTypeOrInstantiate recognises the 'set of ' prefix and builds
  the TSetTypeDesc on demand (as it already does for 'array of'/'^'/'class
  of').  For the anonymous form, SynthAnonEnum synthesises an enum type from
  the encoded member list - registering each member as an enum constant - and
  reuses an identical inline enum if its members are already defined.  Set
  types compare structurally, so the synthetic enum name need only be unique.

Encoding the enum in the type string rather than carrying parser state makes
the feature work uniformly in every type position with no declaration-ordering
or ownership concerns.

Tests: cp.test.inlineset (parser/semantic) and cp.test.e2e.inlineset (compile
+ run on both backends).  Grammar and language rationale documented.
2026-06-13 13:45:13 +01:00
Graeme Geldenhuys ba51a53365 feat(vars): initialised global variables (var G: T = value)
A global variable declaration may now carry an initialiser whose value is
folded at compile time and emitted into the data section, so the variable
holds its initial value before the program body runs (matches FPC/Delphi):

  var
    G: Integer = 42;
    S: string = 'hello';
    A: array[0..2] of Integer = (10, 20, 30);

Implementation reuses the typed-constant pipeline end to end:
- Parser: the const value scanner is factored into ParseConstValue, shared by
  const declarations and the new var-initialiser path.  TVarDecl carries an
  owned InitConst: TConstDecl (TConstDecl moved ahead of TVarDecl so no
  forward declaration is needed).
- Semantic: AnalyseVarInitializer folds the value and type-checks it against
  the declared type; array initialisers derive element type and bounds from
  the resolved static-array type and mint a data label.
- Codegen: EmitGlobalVarInit (QBE) and EmitGlobalInitData (native) emit the
  folded value as a typed data slot - a single field for scalars/strings (a
  string global points at an immortal static header __sN + 12) and an inline
  element list for arrays.  Covers program- and unit-level globals on both
  backends.

Restrictions, each rejected with a clear diagnostic: global scope only (no
local initialisers), a single name per initialised declaration, and arrays
only for aggregates (record and inline-set initialisers are deferred - no
record-constant machinery yet, and a set initialiser would clash with the
const symbol the set folder defines).

Tests: cp.test.varinit (IR + parser/semantic) and cp.test.e2e.varinit
(compile+run on both backends).  Grammar and language rationale documented.
2026-06-13 13:29:43 +01:00
Graeme Geldenhuys 42f48c9ece fix(codegen): Inc/Dec on a captured outer-scope variable
A nested procedure that did Inc(Counter)/Dec(Counter) on a captured outer
local failed to compile: the captured value lives behind the '_cap_<Name>'
pointer slot (which holds the var's address), but Inc/Dec fell through to the
plain local/global path and referenced a non-existent '%_var_Counter' /
'Counter' symbol. QBE rejected it ("invalid type for first operand in
loadsw"); native linking failed ("undefined reference to Counter").

Assignment to a captured var already used the _cap_ indirection; Inc/Dec did
not. Add a captured-var branch to both backends that loads/modifies/stores
through the _cap_ pointer (QBE: loadw/storew via %_cap_Name; native: move the
_cap_ pointer into %rdx and use the existing addr-based inc/dec path). Covers
the w and l element types and the optional step argument.

Adds e2e TestRun_IncDec_CapturedVar. Full suite 3088, both fixpoints OK.

Discovered while migrating the OPDF debugger's nested-scope test
(test_12_teaser_demo) to the Blaise suite.
2026-06-13 12:29:05 +01:00
Graeme Geldenhuys 7567356e84 fix(opdf): emit Boolean and real constants correctly
Constant debug records mishandled two kinds, surfaced while debugging const
values in pdr:

- Boolean (const Enabled = True): True/False are plain identifiers with no
  dedicated token, so the const parser fell into the generic ident-as-string
  path and emitted an empty STRING constant. Recognise True/False as a Boolean
  ordinal (value 1/0) and tag it with TypeName 'Boolean' so the debug emitter
  points the constant at the Boolean primitive's TypeID — pdr then renders
  'True'/'False' rather than a raw 1/0.

- Real (const PiApprox = 3.14159): IsFloat constants fell into the ordinal
  branch and emitted IntVal (always 0). Emit them as ckReal (kind 2, already
  supported by the debugger) with the IEEE-754 Double via GAS .double.

EmitConstants now branches IsFloat -> ckReal, IsString -> ckString, else
ckOrd (with a typed TypeID when TypeName is set). Adds TestOPDF_Constant_-
BooleanTyped and TestOPDF_Constant_Real. Full suite green (3085), both
fixpoints OK.
2026-06-13 12:11:14 +01:00
Graeme Geldenhuys ccd109ce77 feat(arrays): support multi-dimensional static arrays
Add multi-dimensional static array syntax, both the comma form
(array[0..1, 0..2] of Integer; A[i, j]) and the equivalent nested/chained
form (array[0..1] of array[0..2] of Integer; A[i][j]).  The comma forms are
syntactic sugar: the parser desugars array[a, b] of T into the nested
array[a] of array[b] of T, and A[i, j] into chained subscripts A[i][j], so
the two notations are fully interchangeable in every position.

Layers:
- uParser: comma loops in ParseTypeName and subscript reads; the statement
  LHS now lowers A[i, j] := v and A[i][j] := v to a TStaticSubscriptAssign
  carrying a new BaseExpr (the inner-array address expression).  The previous
  "chained base not yet supported" rejection is removed.
- uAST: TStaticSubscriptAssign.BaseExpr (owned); wired into CloneStmt and the
  .bif encoder/decoder (uUnitInterfaceIO); bif-coverage status updated.
- uSemantic: BaseExpr branch resolves the inner static-array type and checks
  the index and value element type.
- Codegen (both backends): a nested static-array element now evaluates to its
  inline address (mirroring record/interface elements) so a further subscript
  indexes into it; the static-subscript store reads its base from BaseExpr
  when set.  Nested arrays are a flat row-major contiguous block.
- OPDF: no new record needed - each dimension emits one recArray whose element
  points at the next inner recArray; pdr follows the chain and renders the
  value as a true multi-dimensional structure.

Tests: IR unit tests (cp.test.staticarray), e2e tests on both backends via
AssertRunsOnAll (cp.test.e2e.staticarray), and a nested-recArray OPDF test
(cp.test.opdf).  Grammar and language rationale documented.
2026-06-13 11:56:38 +01:00
Graeme Geldenhuys 7b8116310f feat(opdf): emit open-array debug info (ArrayKind=2 + companion slot)
Open-array parameters were emitted to the OPDF companion file as dynamic
arrays (IsDynamic=1), so the debugger read their length from the heap
header at data-4. An open array has no header — it is a (data ptr, high
index) pair on the stack — so this read returned garbage (Length=32765).

Model open arrays as a distinct kind:

- TDbgVar gains IsOpenArray + HighRbpOffset; DbgMarkParams records the
  '_high' companion slot offset from the frame map.
- The recArray byte previously named IsDynamic now carries ArrayKind
  (0=static, 1=dynamic, 2=open-array); open arrays emit ArrayKind=2.
- recLocalVar for an open array uses LocationExpr=5 and appends the
  companion '_high' slot offset as a trailing SmallInt.
- Open arrays get a distinct canonical name ('open array of T') so they
  no longer alias a same-element dynamic array's TypeID.
- Parameter and facts-path variable type records are now emitted; the
  anonymous open-array type previously had no recArray written at all.

The debugger side (length read from the companion slot) lives in the
opdebugger repo. FPC's dbgopdf.pas has no open-array concept; this is a
Blaise extension to the OPDF format.
2026-06-13 01:06:16 +01:00
Graeme Geldenhuys 6ba1bc9b78 fix(semantic): reject void/procedure-call results as WriteLn/Write arguments
The previous fix caught procedural-typed variables passed directly to
WriteLn, but missed the case of calling a procedure variable and passing
the (non-existent) result: WriteLn(proce(2,3)).  A procedure call
resolves to tyVoid, not nil, so the nil check didn't fire.  The codegen
emitted an empty QBE operand ('w ') causing QBE to reject the IR.

Extend the Write/WriteLn arg check to also reject tyVoid, covering both:
  - WriteLn(proce)      — procedural variable (tyProcedural)
  - WriteLn(proce(a,b)) — call to a procedure variable (tyVoid)

Adds a semantic unit test for the procedure-call-result case.
2026-06-12 18:12:48 +01:00
Graeme Geldenhuys b3024facc0 fix(semantic): reject procedural/record/class types as WriteLn/Write arguments
WriteLn/Write had no type-check on their argument list — a procedural
variable (or any other unprintable type: record, class, interface, array)
was silently accepted by the semantic pass and handed to the codegen,
which emitted it as an integer (the raw code pointer).  The native backend
printed the address; QBE rejected the IR with "invalid argument".

Add a post-analyse check in the Write/WriteLn branch: after resolving each
argument's type, reject tyProcedural, tyRecord, tyClass, tyInterface,
tyMetaClass, tyStaticArray, tyDynArray, tyOpenArray, and tyPointer with
a clear error message matching FPC's behaviour.

Adds one semantic unit test covering the procedural-variable case.
2026-06-12 17:46:56 +01:00
Graeme Geldenhuys 1b0afaf3d3 fix(test): derive blaise binary path from TestRunner location, not cwd
BlaisePath() was using GetCurrentDir() as the project root fallback,
then appending 'compiler/target/blaise'.  This worked when the TestRunner
was invoked from the project root, but broke when PasBuild cd'd into
compiler/target/ before running ./TestRunner — the path doubled up to
.../compiler/target/compiler/target/blaise.

Fix by deriving the path from ExtractFilePath(ParamStr(0)), which always
points to the directory containing the TestRunner binary regardless of
the caller's working directory.  The BLAISE_PROJECT_ROOT env var override
is preserved unchanged.
2026-06-12 16:59:15 +01:00
Graeme Geldenhuys 75834d393d fix(semantic): out-of-line methods now link for generic records
LinkGenericClassMethodImpls only handled TGenericTypeDef (class), but
TGenericRecordDef is registered in the same generic registry. When an
out-of-line method implementation was written for a generic record
(function MyRec<T>.Foo), the lookup succeeded but the cast to
TGenericTypeDef followed by .ClassDef access was wrong for records,
producing a spurious "Generic type not found" error.

Fix: check the runtime type and dispatch to .ClassDef.Methods for
classes or .RecordDef.Methods for records. Also broadened the
"not declared in generic class" error message to "generic type".

Tests: two new cases in TGenericRecordTests cover the semantic link
and codegen emission for out-of-line generic record methods.
2026-06-12 16:45:33 +01:00
Graeme Geldenhuys 512cbfef98 test(zero-init): use S=[] directly in SetLocal now that semantic crash is fixed
The SrcZeroInit_SetLocal source previously used `fA in S` to probe
set zero-initialisation, working around the S=[] semantic-pass crash
(now fixed).  Update it to use S=[] directly — both more idiomatic
and exercises the fixed equality path in the same test run.
2026-06-12 16:29:57 +01:00
Graeme Geldenhuys 75db9584dc fix(semantic): set equality S=[] and S=[lit] no longer crash
AnalyseBinaryExpr crashed with a nil-deref when one operand was a set
type (tySet) and the other was an empty array literal [] — because
AnalyseArrayLiteralExpr returns nil for an empty literal (no element
type to infer), and the set-dispatch guard read .Kind off that nil
pointer.

Fix: before the set-dispatch guard, coerce any array literal on either
side (empty or non-empty) to the set type of the opposite operand,
mirroring the existing pattern for `x in [A, B, C]`.  This also
enables `S = [fA, fB]` without requiring an explicit cast.

The native-backend set-variable crash documented in bugs.txt was
already fixed in HEAD (the release binary produced a codegen error, but
the current compiler handles it correctly).

Tests added:
- TSetTests.TestSemantic_Set_EqualityEmptyLiteral_OK — S=[] must not crash
- TSetTests.TestSemantic_Set_EqualityLiteral_OK — S=[fA,fB] accepted
- TSetTests.TestCodegen_Set_EqualityEmptyLiteralEmitsCeqw — IR check
- TSetTests.TestCodegen_Set_EqualityLiteralEmitsCeqw — IR check
- TE2EMiscTests.TestRun_Set_EqualityWithLiteral — compile+run assertion
- TE2ENativeTests.TestRun_ZeroInit_SetLocal now runs on both backends
2026-06-12 16:21:25 +01:00
Graeme Geldenhuys a2d263e0e8 feat(lang): guarantee zero-initialisation of all variables
Blaise now guarantees zero-initialisation of every variable as a language
semantic — local variables, globals, record fields, static-array elements,
threadvars, and Result.  The QBE backend already satisfied this; the native
x86-64 backend was the only gap (scalar locals were uninitialised on stack).

Native backend changes (blaise.codegen.native.x86_64.pas):
- Replaced the ARC-only zero-init loop in EmitFunctionDef with an
  exhaustive case over all TTypeKind values, covering every scalar
  type (integer family, float, boolean, pointer, enum, set, procedural)
  in addition to the already-handled managed types.  An else-raise clause
  ensures any future new type kind is caught at compile time rather than
  silently skipped.
- Fixed AddSlot to allocate 16 bytes for method-pointer locals (Code +
  Data slots), matching the 16-byte allocation the QBE backend already
  used.  Previously only 8 bytes were reserved, which would corrupt an
  adjacent frame slot if a local method-pointer was written.

10 new E2E tests run on both backends via AssertRunsOnAll, using a
Dirty() helper that pre-fills the stack with 0xDEADBEEF to prove zero-init
comes from the prologue and not from lucky stack layout:
  TestRun_ZeroInit_ScalarIntegers, FloatLocals, BooleanAndChar,
  PointerLocals, EnumLocal, SetLocal (QBE-only — native crashes on sets,
  pre-existing bug), RecordWithMixedFields, StaticArray, ThreadVar,
  GlobalVars.

Documented in docs/language-rationale.adoc (decision, alternatives
rejected, implementation notes, future noinit/definite-assignment roadmap).
2026-06-12 16:09:02 +01:00
Graeme Geldenhuys d182b4ab7e fix(debug): attribute generic-instance allocation sites to the defining unit
The --debug leak tracker reports allocation sites as '<unit>:<line>'.
For objects allocated inside generic method bodies the two halves of
that pair came from different sources: the line number is the cloned
template AST's Line field (which refers to the template's source file),
but the unit name was FCurrentUnitName — the unit or program being
emitted when the generic instance's method bodies were generated, i.e.
the INSTANTIATING unit.  A TListEnumerator<Integer> created inside
TList<T>.GetEnumerator (generics.collections.pas:316) was therefore
reported as 'P:316' for a 33-line program P.

Fix: record the declaring unit on TGenericTypeDef when the template is
registered (both the direct semantic path and the .bif import path),
copy it onto TGenericInstance at instantiation, and have both backends
temporarily switch FCurrentUnitName to GI.DefUnitName around the
emission of generic-instance method bodies, restoring it afterwards.
The report now reads 'Generics.Collections:316'.

The new DefUnitName fields are populated by the semantic pass at
runtime and are intentionally not serialised; bif-coverage passes
unchanged.  Generic record and generic function instances still carry
the old mismatch and are tracked separately.

New e2e tests (QBE + native): TestDebug_GenericAllocSite_ReportsDefiningUnit
and TestDebug_GenericAllocSite_ReportsDefiningUnit_Native.
2026-06-12 15:16:45 +01:00
Graeme Geldenhuys 1dfc693fe5 fix(codegen): for-in over enumerators — promoted loop var and leaked enumerator
Two bugs in the enumerator-protocol for-in lowering, found via a user
program iterating a TList<Integer>:

- QBE: the mem2reg promotion scan did not treat the for-in loop
  variable as a store target, so an otherwise-unaddressed scalar loop
  var was promoted to an SSA temp while the lowering wrote the element
  with storew %t, %_var_<name> — invalid IR ('invalid type for second
  operand ... in storew').  The loop variable now always keeps a stack
  slot.

- Both backends: GetEnumerator's result (an owned +1 constructor
  return) was re-retained when stored into the synthetic enumerator
  slot, while the function epilogue releases the slot exactly once —
  leaking one enumerator per loop (rc=1 in every --debug leak report).
  The owned reference is now transferred into the slot.

e2e leak-check tests run the same list-summing loop under --debug on
both backends and assert clean output with no leak report.
2026-06-12 15:05:43 +01:00
Graeme Geldenhuys eabf20ae7f refactor(driver): driver-aware worker; registry-driven flags and pre-flight
TCompileWorker now carries Driver + Opts and builds its per-unit
codegen through Driver.CreateUnitCodeGen, names its temp IR file with
the driver's extension, and lowers through the same driver.  A driver
that claims SupportsIncremental but returns a nil unit codegen fails
with a clear error instead of crashing.  The incremental dispatcher
prefers the top-program backend's driver and falls back to QBE when
the backend has no per-unit emission yet — the only remaining backend
comparison outside PickTopDriver.

--backend validation and the --help backend line are now driven by the
driver registry (ParseBackendName / RegisteredBackendNames), so adding
a backend no longer touches the flag parser.  The toolchain pre-flight
runs once through Driver.CheckToolchain before the front-end; stdout-
only modes (--emit-ir / --emit-asm / --dump-ast) skip it since they
need no external tools.

Blaise.pas no longer references TCodeGenQBE/TCodeGenNative anywhere;
unused locals from the pre-driver pipeline are removed.

Architecture follows Andrew Haines' unify_backend_interface proposal.
2026-06-12 14:05:38 +01:00
Graeme Geldenhuys 543f495f48 refactor(driver): lowering and linking through the backend drivers
Move the IR-to-object and IR-to-binary pipelines behind TBackendDriver
(architecture per Andrew Haines' unify_backend_interface proposal):

* LowerToObject — QBE driver runs qbe -> .s -> cc -c -> .o; the base
  class default fails loudly for drivers without per-unit emission.
* LinkProgram — QBE driver lowers via qbe then links; the native driver
  links its assembly directly, or assembles in-process
  (AssembleToObject) and links the .o when --assembler internal is
  selected.  The --assembler policy now lives inside the native driver
  instead of Blaise.pas.
* LinkViaToolchain — shared protected link line (input, OPDF sidecar,
  prebuilt dep objects, RTL archive, -lm, -lpthread) resolved through
  uToolchain, so BLAISE_QBE/BLAISE_LINKER/BLAISE_RTL overrides now apply
  uniformly; the unit-object path previously hard-coded 'qbe'/'cc'.
* RunProcess moves to blaise.codegen.driver (used from worker threads).

Blaise.pas drops CompileToNative, CompileToNativeDirect, LinkObjectFile,
FindRTL, and the backend branch in the output dispatch; the single
shared path writes the IR with the driver's file extension and calls
Driver.LinkProgram.  Behaviour deltas: the native link now also receives
auto-discovered prebuilt dep objects (previously dropped on the native
path), and link failures report 'link error (exit N)' uniformly.
2026-06-12 14:01:48 +01:00
Graeme Geldenhuys 5603767298 refactor(driver): introduce TBackendDriver; route codegen construction
Add blaise.codegen.driver with the abstract TBackendDriver base class
(virtual Kind/Name/IRFileExt/SupportsIncremental/SupportsWarmCache/
CheckToolchain/CreateCodeGen/CreateUnitCodeGen), the TBackendOpts flag
bag, a fixed-array registry keyed by backend kind, and PickTopDriver —
the single backend-selection policy decision (--emit-ir forces QBE,
--emit-asm implies native, otherwise --backend).

blaise.codegen.qbe.driver and blaise.codegen.native.driver register
class singletons at unit initialization; they are ARC-managed globals
released by the program-exit release pass.

Blaise.pas now builds one TBackendOpts up front, resolves the driver
once via PickTopDriver, and constructs the code generator through
Driver.CreateCodeGen — the backend if/else around TCodeGenQBE /
TCodeGenNative construction is gone.

The architecture follows Andrew Haines' unify_backend_interface
proposal; full credit to Andrew for the driver/registry design.  This
tree uses an abstract class with virtual methods instead of an
interface so shared behaviour can live in the base class.
2026-06-12 13:55:01 +01:00
Graeme Geldenhuys 06517bf1d3 fix(qbe): dispatch record/interface-returning virtual methods via vtable
EmitRecordCallSret always emitted a direct call to the declaring class's
method symbol, even for virtual methods.  Overrides never ran on the
record/interface sret return path, and calling a virtual-abstract method
(which has no emitted body) produced an undefined-symbol link error.

Add SretMethodCallTarget: when VTableSlot >= 0 load the vptr from the
instance and the function pointer from the vtable slot (slot 0 is
typeinfo, so method N lives at (N+1)*8), otherwise keep the static
symbol.  Applied to all three receiver shapes in EmitRecordCallSret
(implicit-Self call, class-receiver method call, zero-arg field-access
call).

E2E coverage: TestRun_Native_IntfFromClassMethod now exercises an
override returning an interface; new TestRun_Native_RecReturnVirtualOverride
covers the record-returning case on both backends.
2026-06-12 13:53:25 +01:00
Graeme Geldenhuys 1ba78cf90b fix(native): support class-receiver method calls returning interfaces
IntfVar := ClassObj.Method() failed with 'unsupported interface-field
assignment RHS' on the native backend (the QBE backend already handled
it).  EmitInterfaceAssign only covered itab-dispatch receivers and plain
function calls in its sret branches.

Add EmitClassIntfSretMethodCall — sret buffer as hidden first arg
(%rdi), receiver in %rsi, static or vtable dispatch from the receiver's
class — and wire it into all three EmitInterfaceAssign regions
(implicit-Self field, sret Result/var-param, local/global LHS).  Calls
with more than four user argument slots fail loudly until needed.

Covered by TE2ENativeTests.TestRun_Native_IntfFromClassMethod, which
exercises every LHS shape plus virtual dispatch on both backends.
2026-06-12 13:44:19 +01:00
Graeme Geldenhuys f0f888f2c5 fix(codegen): interface-dispatch ABI — record args, var/out params, call receivers, discarded returns
Four itab-dispatch ABI gaps reported from Andrew Haines'
unify_backend_interface branch, fixed on both backends:

- Record by const/value through interface dispatch (QBE) passed the
  record as 'w <addr>' — truncating to the low 8 bytes and shifting
  every later argument.  Dispatch sites now use the same :_ffi_<Name>
  aggregate ABI as direct calls.  (Native already passed the address.)
- var/out parameters through interface dispatch loaded the VALUE
  instead of passing the slot address.  Root causes: a 1-based loop
  over the 0-based var-flag string in MethodParamIsVar (parameter 0's
  flag was never seen), the QBE statement path ignoring var flags, and
  the native push loops never emitting addresses.  Strings and
  dynarrays covered.
- TFuncCallExpr receivers (GetDriver(x).Info()) previously raised
  fail-loud.  The receiver call is sret-evaluated into a temporary fat
  pair; the owned +1 obj is released right after the consuming call
  (QBE: pending-release list flushed by every call emitter; native:
  pair kept above the hoist region, released preserving result regs).
- Discarded interface-returning itab calls in statement position
  clobbered memory through the register the callee expects to hold the
  sret buffer (QBE: missing buffer; native: receiver passed where the
  buffer belongs, shifting Self).  Statement calls now get a throwaway
  sret buffer and release the returned obj.  Semantic records the itab
  return type on TMethodCallStmt (bif-coverage clean).

e2e tests run on both backends (cp.test.e2e.imap); IR tests pin the
aggregate ABI, slot-address passing, and the discard-sret+release
sequence (cp.test.interfaces).
2026-06-12 13:27:00 +01:00
Graeme Geldenhuys c20ddd549d fix(codegen): interface nil-compare and interface-method sret returns
Two interface-dispatch bugs surfaced by Andrew Haines'
unify_backend_interface branch, fixed on both backends:

1. Interface value reads through a bare identifier (nil compares,
   most visibly 'if Result = nil' inside an interface-returning
   function) loaded a non-existent single slot.  Interface idents now
   load the obj half of the fat pointer: locals via the _obj slot,
   globals via the Name_obj label, Result via the sret buffer
   pointer.  QBE additionally treats tyInterface as a pointer kind in
   comparisons (ceql/cnel).  The native backend previously compared
   the sret buffer ADDRESS against nil — always false, silently.

2. V := Intf.Method(...) where Method returns an interface emitted a
   single-slot store against the split-slot local (invalid QBE IR /
   wrong native code).  Itab-dispatched calls returning interfaces now
   route through the sret convention like plain function calls: QBE
   grows EmitIntfSretDispatch; native grows EmitIntfSretMethodCall,
   wired into every interface-assignment LHS shape.  Ownership follows
   the existing convention (callee AddRefs into the sret buffer, the
   caller takes the owned +1 pair).

Also fixes a latent bug found while testing: assigning an
interface-typed Result to a global stored the sret buffer pointer as
the obj half (native PushIntfIdentPair now dereferences correctly).

Still open, fail-loud (recorded in bugs.txt): TFuncCallExpr receivers
of interface calls, and discarded interface-returning itab calls in
statement position on the QBE path.

IR tests in cp.test.interfaces; e2e tests in cp.test.e2e.imap run the
registry-pattern acceptance program on both backends.
2026-06-12 12:50:19 +01:00
Andrew Haines 81c5804ae4 fix(codegen): static-array element read/write of interface uses fat pointer
Ported from Andrew Haines' unify_backend_interface branch (97b4d6a9).

Both halves of the static-array element path missed the interface
fat-pointer layout: Arr[I] := IFaceVal fell through to the generic
element store, which picked storew against the 16-byte slot and left
itab uninitialised; F := Arr[I] emitted a single-slot load and a store
against the bare global name (undefined reference at link time).  The
element slot is now treated as the contiguous obj+itab pair on both
paths, with class->element stores resolving the itab by name and
nil stores releasing the prior object.

Two additions to the original patch:
- The class->element store transfers ownership when the RHS already
  owns +1 (ExprOwnsRef guard, mirroring EmitAssignment's class->iface
  branch) instead of an unconditional _ClassAddRef that leaked one
  reference per constructor-RHS store.
- The new TStringSubscriptExpr case in EmitInterfaceExprPair is
  restricted to STATIC array bases: dynamic-array subscripts return a
  loaded value, not an address, and keep hitting the fail-loud error
  until they get their own handling.

Tests: the two IR tests from the original patch (read assertion updated
to the contiguous global fat-pointer layout that landed after Andrew's
base), plus an e2e test (TestRun_StaticArrayOfInterface_FatPointer)
covering store, dispatch through Arr[I], element-to-var copy,
element-to-element copy, and nil store at runtime.
2026-06-12 12:13:54 +01:00
Graeme Geldenhuys 1babf0c9a5 feat(linker): section merger (completes Phase A)
TSectionMerger in the new blaise.linker.elf unit concatenates
like-named allocatable sections across input objects, padding each
contribution to its declared alignment, and records a placement
(merged section + offset) per input section — the basis for symbol
and relocation rebasing in Phase B.

SHT_NOBITS contributions advance the merged size without emitting
bytes; mixing NOBITS and PROGBITS under one name is an error.
Bookkeeping sections (symtab, strtab, rela, .note.GNU-stack,
.comment) are skipped — the linker rebuilds those itself — while
non-alloc .opdf.* debug sections are kept for the OPDF pass-through.

Tests cover text concatenation with placement offsets, alignment
padding between contributions, .bss size accumulation, and the
bookkeeping-section skip list.
2026-06-12 10:13:16 +01:00
Graeme Geldenhuys 5415815f8d feat(linker): ELF object and ar-archive reader (Phase A)
First step of the internal-linker plan
(docs/internal-linker-design.adoc): blaise.elfreader parses ELF64
little-endian ET_REL x86-64 objects — section headers with contents,
the symbol table, and RELA relocation entries — and !<arch> static
archives with GNU long-name table support (blaise_rtl.a carries
member names beyond the 15-character ar limit).

Parsed entities are heap objects rather than records to sidestep the
known dynamic-array-of-record element-assignment hazard.  The archive
API fills a caller-owned TList (generic function return types are not
supported yet).

TElfReaderTests covers: section bytes and flags from the internal
assembler's output, global function symbols, .quad relocations with
addends, NOBITS sections, bad-magic rejection for both formats, a
synthetic archive exercising the GNU long-name table and member
padding, and a sweep over every member of the real blaise_rtl.a.
2026-06-12 10:10:21 +01:00
Graeme Geldenhuys 62a9b02870 fix(codegen): string-field subscripts are 0-based on both backends
Rec.Field[N] / Obj.Data[N] where the field is string-typed
(IsCharAccess) read the wrong byte:

- QBE backend: leftover 1-based lowering subtracted 1 from the index —
  every read was off by one.  The receiver base also ignored the leaf
  shape (always loaded the slot as a pointer), which mis-addressed
  inline record receivers.  The branch now uses the same receiver
  ladder as array-field subscripts and indexes 0-based.
- Native backend: had no IsCharAccess handling at all — the expression
  fell through to the plain field-read path and returned the string
  POINTER bytes instead of the subscripted character.  Added the
  missing branch (receiver ladder, load data pointer, movzbq at the
  index).

Pinned by TestRun_Native_StringFieldCharRead, which runs on both
backends and covers record, class, expression-index, and
implicit-Self receivers.  Found while parsing ELF symbol tables with
Ord(Sec.Data[Off + 4]) in the new linker reader.
2026-06-12 10:10:09 +01:00
Graeme Geldenhuys ff5c481ec0 test: add assembler/ELF writer tests and internal linker design doc
- TAsmEncodingTests: byte-level regression tests against AssembleToBytes
  (bare (%reg) operands, .quad symbol relocations, TLS prefix ordering,
  PC32 addend with trailing immediates, branch relocations,
  .note.GNU-stack presence, REX.X for extended index registers, imm16
  width, unknown-directive/duplicate-label errors, line-numbered
  diagnostics).
- TElfWriterTests: ELF header fields, section append/align/offset
  tracking, symbol definition and lookup, BSS reservation.
- TInternalAsmE2ETests: compile->link->run through the compiler CLI
  with --backend native --assembler internal, including class virtual
  dispatch (vtable .quad relocations) and float arithmetic (SSE
  spills).  A compile failure now Fails the test rather than being
  masked as a missing toolchain.
- docs/internal-linker-design.adoc: design for the next
  toolchain-independence phase — an internal ELF linker built into the
  blaise binary (PIE output, eager binding, .rela.dyn/R_X86_64_RELATIVE,
  TLS, CRT discovery, FreeBSD/macOS outlook, OPDF pass-through).
2026-06-12 09:55:44 +01:00
Graeme Geldenhuys 83d40490fc feat: add self-assembler for native x86-64 backend (--assembler internal)
Route A of the toolchain-independence plan: a built-in assembler that
parses the restricted AT&T subset the native backend emits and encodes
it into ELF relocatable objects, replacing the shell-out to GNU as.

New units:
- blaise.elfwriter: ET_REL ELF64 writer (.text/.data/.rodata/.bss/
  .tbss, .symtab/.strtab, .rela.* with RELA addends).  Always emits an
  empty .note.GNU-stack section so linked binaries get a non-executable
  stack, matching GNU as output.
- blaise.assembler.x86_64: two-pass assembler covering the backend's
  full mnemonic set (ALU, MOV family incl. extensions, SSE scalar
  ops, branches, calls, TLS) and directives.

Correctness points carried by the encoder:
- bare (%reg), disp(%base,%index,scale) and RIP-relative operands;
- .quad/.long <symbol>[+disp] emit R_X86_64_64/_32 relocations
  (vtables, typeinfo, class-name records);
- the FS segment prefix is prepended ahead of REX for TLS operands;
- PC32 addends account for trailing immediate bytes (addend -8 for
  movq $imm, sym(%rip));
- branches to external/other-section labels emit PLT32 relocations;
- REX.X is set for r8-r15 index registers on every mem-operand path;
- 16-bit moves emit imm16, not imm32.

Fail-loudly policy: unknown directives, duplicate labels, and
unsupported operand forms raise EAssembler with the source line number
and raw line text; Blaise.pas reports them as proper diagnostics.

The textual .s remains the canonical artefact; --assembler external
(the default) still shells out to GNU as.
2026-06-12 09:55:34 +01:00
Graeme Geldenhuys da2bb5a9a9 fix(native): var/out argument addresses and record-typed field assignment
Two wrong-code bugs in the x86-64 native backend, found while running
the internal assembler inside the native-compiled TestRunner:

1. var/out arguments passed to sret (record-returning) function calls
   and indirect calls loaded the variable's VALUE (movslq) instead of
   its ADDRESS (leaq), handing the callee a garbage pointer.  The same
   inline emission logic was duplicated across seven call paths, four
   of which silently pushed a stale %rax for any non-identifier var
   argument (e.g. Result.Field in an sret function).  All paths now
   share a single EmitVarArgAddrToRax helper which also handles
   field-access arguments (including fields of the sret Result) and
   fails loudly on unsupported forms instead of emitting garbage.

2. Assigning a record value into a record-typed field of the sret
   Result (Result.Op1 := T) fell through to the scalar store path,
   writing the source record's ADDRESS as an 8-byte field value.  A
   record-to-record-field assignment now performs the full ARC-aware
   copy: retain source managed fields, release destination fields,
   then memcpy of the record size.

Both bugs are pinned by new e2e tests that run on the QBE and native
backends (TestRun_Native_SretCall_VarParamArg,
TestRun_Native_SretResult_NestedRecordFieldAssign).
2026-06-12 09:55:20 +01:00
Graeme Geldenhuys eeadc177a1 fix: default all option variables in the FPC-style invocation branch
ParseFPCArgs only assigns SourceFile, OutputFile, SearchPaths and
OPDFEnabled; the FPC-style branch then defaulted just EmitIR/EmitAsm,
leaving DumpAST, DebugMode, Backend, Target, SkipDepCodegen,
EmitIfaceDir, Incremental and UnitCacheDir uninitialised — several of
which are read unconditionally later (UnitCacheDir, Backend, DumpAST).
Locals are not zero-initialised; the path only behaved because main's
frame lands on freshly mapped zero pages.

Diagnosed by Andrew Haines: a flow analysis flagged DumpAST as
potentially used before assignment, and the FPC-style branch is the
incoming path that proves the warning a true positive.
2026-06-12 00:55:16 +01:00
Graeme Geldenhuys 1a5a881d6c feat(semantic): reserve module names as identifiers
The program/unit's own name and the names of directly used units can
no longer be redeclared by top-level declarations, matching FPC and
Delphi (Duplicate identifier / E2004).  Previously the module name was
stored on the AST node and never entered the symbol table, so
'program P; var P: Integer;' compiled by accident of omission.

The semantic pass plants skModule marker symbols in the scopes that
receive top-level declarations (global + program-block scope for
programs, unit scope for units); the ordinary duplicate check on
TSymbolTable.Define then rejects redeclarations.  Lookup treats a
marker hit as unresolvable, so the reserved name is not a value and
inner scopes can still shadow it, as in FPC.  The const paths get an
explicit marker check because their Define-failure branch otherwise
tolerates the clash silently (cross-unit const shadowing).

One deliberate divergence: FPC accepts a procedure named after the
program (an accident of its overload machinery); Blaise rejects all
declaration forms uniformly.

Grammar is unchanged — this is a name-resolution rule, not syntax —
so grammar.ebnf is untouched; the decision is recorded in
language-rationale.adoc.  44 test programs named 'program P' that also
declared an identifier P are renamed to 'program Prg'.

Closes #84
2026-06-11 23:51:11 +01:00
Graeme Geldenhuys b9aae4abf5 dateutils: fixes documentation reference in header comment. 2026-06-11 23:12:30 +01:00
Graeme Geldenhuys b776ceb5dd fix(codegen): fail loudly on IsArrayAccess fall-through in both backends
The array-field-subscript read path (Rec.Arr[I]) in EmitExpr dispatches
on the field's type kind — dynarray, static array, open array — with no
final else.  A field kind outside those three would fall through with
Result unassigned (QBE: garbage temp name) or a stale %rax (native:
silent wrong value).

Unreachable today: the semantic pass only sets IsArrayAccess for those
three kinds.  But the invariant lives in a different unit, so add an
explicit raise in both backends to turn any future mismatch into a
compile-time internal error instead of silently wrong code.
2026-06-11 23:06:54 +01:00
Graeme Geldenhuys 35f8ff5970 Revert defaulting the compiler to Native backend.
My local setting accidently got comitted in ac0acc7bdd.
2026-06-11 22:53:26 +01:00
Graeme Geldenhuys 840b053812 fix: var parameters of dynamic-array, static-array, PChar and interface types
Diagnosis credit: Andrew Haines (issue #89) correctly identified that
the subscript-assign path loads through a var/out parameter's slot only
once — the slot holds the ADDRESS of the caller's variable, so element
writes landed in the wrong memory and corrupted the heap.  His patch
covered the QBE dyn-array and class cases; the class half had already
landed independently (248dccf).  This implements the remaining cases
across BOTH backends in the current tree.

Subscript writes through var/out params (TStaticSubscriptAssign gains
IsVarParam, set by the semantic pass; bif-coverage status updated):
- dynamic arrays: one extra dereference to reach the data pointer
  (writes were lost and stray stores corrupted the heap; SetLength and
  element reads already worked),
- static arrays: load the array address from the slot instead of
  offsetting the slot itself (QBE wrote into the parameter slot region;
  the native ident READ also produced garbage — pmVar now treated like
  the other by-ref param modes),
- PChar: extra dereference before the byte store (writes were lost).

Interface var/out parameters (previously did not even compile: QBE
emitted loads from a non-existent %_var_G_obj; native mis-spilled the
single incoming pointer as a two-register fat pointer):
- interface variables now occupy ONE contiguous 16-byte fat-pointer
  block (obj at +0, itab at +8).  QBE locals: a single alloc8 16 with
  the _itab name derived at +8; QBE globals: a single 16-byte data item
  $Name_obj with the itab half addressed as $Name_obj + 8 (the separate
  $Name_itab item could not be guaranteed adjacent).  Native locals and
  globals were already contiguous.
- new IntfObjAddr/IntfItabAddr helpers route every QBE obj/itab access
  (assignment variants incl. weak, dispatch, expr-pair reads, as-out
  binding); var/out params dereference the slot first.
- native: var-param-aware receiver load in EmitInterfaceCall, var-param
  LHS in EmitInterfaceAssign (shares the sret-Result pointer path),
  interface globals usable as var args (leaq Name_obj), and the call
  slot counter treats a var interface arg as ONE pointer slot (it was
  counted as two, desynchronising the argument register pops).

IR assertions updated for the new global fat-pointer layout.  New e2e
tests: TestRun_VarParamDynArray_WriteAndGrow,
TestRun_VarParamStaticArray_PChar,
TestRun_VarParamInterface_DispatchAndReassign.

Closes #89.
2026-06-11 18:50:50 +01:00
Graeme Geldenhuys 4f2246cc57 fix(debug): per-unit source files in OPDF line records
Every recLineInfo record carried the program's main source file, so
'break unit.pas:NN' failed with 'No code found' for any line inside a
unit, and callstack frames attributed unit code to the program file
(found trying to break inside kanban.ui.pas in the kanban tool).

TDbgFunc gains a SourceFile field; the native backend stamps it from
TUnit.SourceFile while emitting each unit (empty for the main program,
which keeps the emitter's own fallback).  The facts-mode line emission
writes each function's own file into its records.  The recLineInfo
format already carries a per-record filename and pdr matches by
basename, so no format or debugger change is needed.

Verified: breakpoints by unit-file:line resolve and fire, parameters
print at the stop, and callstacks name the correct file per frame.
2026-06-11 17:56:23 +01:00
Graeme Geldenhuys ac0acc7bdd feat(native): build blaise-compiler with the native backend by default
compiler/project.xml now passes --backend native --debug-opdf for the
blaise-compiler module, so local builds of the compiler and TestRunner
exercise the native x86-64 backend end-to-end and are debuggable with
pdr.  The shipped default backend remains QBE.

Compiling the compiler itself natively (never done before) exposed two
pre-existing native codegen bugs, both fixed:

- Local dynamic-array variables were not zero-initialised.  SetLength
  reads the old data pointer and the epilogue releases it, so stack
  garbage in the slot corrupted the heap — the unit→.o iface-embed
  path crashed in the allocator.  Dyn-array locals now start nil like
  string/class/record locals.  New e2e test
  TestRun_Native_LocalDynArray_DirtyStack pins this with a
  dirty-stack helper.

- P[I] := Chr(N) stored the low byte of the _Chr-allocated STRING
  POINTER instead of N: the native backend lacked the QBE backend's
  EmitByteRhs short-circuit.  Added EmitByteRhsToEax (Chr folds to its
  argument, single-char literals to their ordinal) and applied it at
  every byte-sized store site (PChar subscript, dyn/static array
  byte elements, P^ byte writes, byte array-field elements).  This
  corrupted every ELF header patch in uElfObject, producing .o files
  with garbage e_shoff/e_shnum.

With both fixes the full separate-compilation round trip works under
the all-native toolchain and the complete suite passes (2959 tests)
with a natively-built compiler and TestRunner.

uDebugOPDF: interface-typed globals are two labels (Name_obj/_itab);
recGlobalVar now references Name_obj so --debug-opdf links for
programs with interface globals (e.g. the compiler's own CG).
2026-06-11 17:32:25 +01:00