Commit graph

435 commits

Author SHA1 Message Date
Andrew Haines 511d0bc901 chore(codegen): reserve FSystemDefsEmitted (phase 1 of 6c-J)
No-op reservation for the AppendUnit System-unit (TObject /
TCustomAttribute) emission landing in phase 2.  This commit only:

- Adds FSystemDefsEmitted: Boolean to TCodeGenQBE.
- Initializes it to False in the constructor.

Nothing reads or writes it yet.  Splitting the work this way lets
a stage-1 binary built from this commit understand the symbol
*before* the next commit starts checking it from four call sites
(AppendUnit + the three Emit*Defs).  Without this phase, a partial
intermediate state emits duplicate System-unit defs and the
resulting binary can't compile its own next iteration —
chicken-and-egg observed earlier in this session and documented in
memory:project_unit_as_toplevel.md.

Pre-commit gate:
  - mcp compile: OK
  - bootstrap binary updated cleanly
2026-06-03 19:34:11 +01:00
Andrew Haines 7db947ccb0 build(runtime): drop 7 build-driver shims, use unit-as-top-level
project_unit_as_toplevel landed last commit — blaise now accepts a
unit directly as the top-level source, with --output producing a
standalone .o (with embedded .bif).  This collapses each RTL unit's
4-step pipeline (driver shim → emit-ir → sed strip → qbe → cc -c)
into a single invocation.

Deleted (7 driver shims, mechanical wrappers):
  blaise_arc_build_driver.pas
  blaise_exc_build_driver.pas
  blaise_float_build_driver.pas
  blaise_mem_build_driver.pas
  blaise_str_build_driver.pas
  blaise_thread_build_driver.pas
  blaise_weak_build_driver.pas

Makefile rule pattern shrinks from:

  $(OBJ_DIR)/X.ssa: $(PAS_SRC_DIR)/X.pas $(PAS_SRC_DIR)/X_build_driver.pas
      $(BLAISE) --source $(PAS_SRC_DIR)/X_build_driver.pas \
                --unit-path $(PAS_SRC_DIR) --emit-ir \
      | sed '/^# Generated by Blaise/,$$d' > $@
  $(OBJ_DIR)/X.s: $(OBJ_DIR)/X.ssa
      $(QBE) -o $@ $<
  $(OBJ_DIR)/X.o: $(OBJ_DIR)/X.s
      $(CC) -c -o $@ $<

to:

  $(OBJ_DIR)/X.o: $(PAS_SRC_DIR)/X.pas
      $(BLAISE) --source $< --unit-path $(PAS_SRC_DIR) --output $@

Carve-out: rtl.platform.posix keeps the build-driver + sed pipeline.
That .o is currently the sole anchor of typeinfo_TObject /
vtable_TObject in blaise_rtl.a — they're emitted by the "compile
as program" path because TObject is reachable from the implicit
System unit there, but unit-mode codegen only emits the unit's own
classes (TRtlPlatform / TRtlPlatformPosix) and leaves TObject as
an undefined extern.  Gating System-unit emission on reachability
inside unit-mode codegen is the follow-up that would let this last
carve-out collapse too.

Each new .o also carries an embedded .bif (6c-G), so the resulting
blaise_rtl.a + objcopy --dump-section can recover the per-unit
interface — useful for future tooling.

Pre-commit gate:
  - mcp compile: OK
  - TestRunner: 2249 tests, 2 pre-existing failures only
  - fixpoint.sh: FIXPOINT_OK at stage-3/stage-4
2026-06-03 19:34:11 +01:00
Andrew Haines dfabf0c2d8 feat(parser,driver): unit-as-top-level + --skip-dep-codegen
blaise --source <unit>.pas now parses and compiles a Blaise unit
directly, producing a relocatable .o that exports the unit's
interface-section routines.

Parser:
  TParser.IsUnitTopLevel — peeks the primed first token, True
  when it's tkUnit.

Semantic:
  TSemanticAnalyser.GetSymbolTable read-accessor — codegen needs
  the analyser's symbol table in unit-mode where no TProgram
  exists to hand it off.

Driver (Blaise.pas):
  - Parse phase forks on IsUnitTopLevel: TopUnit (TUnit) or
    Prog (TProgram), with IsUnitMode tracking which arm took.
  - Semantic phase uses TopUnit.UsedUnits for dep loading and
    AnalyseUnitForExport(TopUnit) for the top-level pass.
  - Codegen forks on IsUnitMode: AppendUnit(TopUnit) only, no
    AppendProgram, so no @main lands in the IR.
  - New CompileUnitToObject runs qbe → .s → cc -c → .o, stopping
    at the object file (no link, no RTL).

  - --skip-dep-codegen omits the AppendUnit pass for dep units —
    cross-unit call sites become unresolved externs.  Caller
    must supply pre-built dep object files at link time.

Together these are the minimum primitives for separate
compilation.  The .bif-in-.o pipeline that consumes them lands
in a later feature.
2026-06-03 19:34:11 +01:00
Graeme Geldenhuys 6cb2012bd9 feat(native): M7e complete — open arrays and dynamic arrays
Open-array parameters: two-register ABI (data ptr + high index).
  - BuildFrame: IntIdx2 tracks register slots (open arrays consume 2).
  - EmitFunctionDef prologue: spills both data-ptr and high-index regs.
  - EmitCall: open-array args expand to (data_ptr, high) push pair.
  - EmitOpenArrayLiteral: allocates stack storage for [a,b,c] literals,
    fills elements, returns bytes allocated for post-call cleanup.
  - Static-array-to-open-array coercion: leaq base + compile-time high.
  - Open-array forwarding: pass through from caller's _var_N/_var_N_high.

Intrinsics for all array flavours:
  - High(A): open array → _high slot; static → compile-time HighBound;
    dynamic → _DynArrayLength - 1; string → StringLength - 1.
  - Low(A): static → LowBound; all others → 0.
  - Length(A): static → compile-time count; open → _high + 1;
    dynamic → _DynArrayLength; string → StringLength.

Dynamic arrays:
  - tyDynArray added to IntByteSize (8 bytes, pointer-sized slot).
  - Global var registration extended to include tyDynArray.
  - A[I] read: EmitExprToEax path for tyDynArray subscript.
  - A[I] := V write: TStaticSubscriptAssign handles tyDynArray.
  - SetLength(A, N): calls _DynArraySetLength(ptr, n, elemsize),
    stores new data ptr back into the variable slot.

8 new AssertRunsOnBoth e2e tests: inline literal sum, High/Low/Length,
static-to-open coercion (length, sum, nested pass-through), dynamic
array SetLength+access and Length/High. 2420 tests pass.
2026-06-03 19:00:32 +01:00
Graeme Geldenhuys f4f7dbb767 feat(native): M7d complete — exception handling parity with QBE backend
try/finally: _PushExcFrame + _blaise_setjmp dispatch; normal path pops frame
and runs finally body; exception path captures exc, pops frame, runs finally,
re-raises via _Reraise.

try/except: typed handlers matched via _IsInstance (exception in %r15), bare
catch-all body, else body for unmatched exceptions, re-raise when no handler
matches.

raise: evaluates object expression into %rdi and calls _Raise; bare raise
retrieves _CurrentException into %rdi and calls _Reraise.

Exit/Break/Continue through active try frames call EmitExcUnwind, which pops
each frame and emits the finally body inline (try/except frames have nil in
FFinallyStack so only the pop is emitted).

Exception frame allocation:
- Functions: CountTryStmts pre-allocates 512-byte, 16-byte-aligned slots
  in BuildFrame (negative %rbp offsets, _exc_frame_N names).
- Program main: FProgExcFrameCount counts try stmts; 512-byte .data globals
  (_exc_frame_N) emitted in EmitDataSection.

FFinallyStack uses TList<TCompoundStmt> for index-aligned nil-safe access
(TObjectList.Delete unconditionally calls _ClassRelease, which crashes on nil).

10 new AssertRunsOnBoth e2e tests covering: bare try/finally, nested finally,
exit-through-finally, break-through-finally, bare try/except, typed handler,
subclass match, bare-raise propagation, else body, exit-through-nested-finally.

2412 tests pass; FIXPOINT_OK.
2026-06-03 18:33:25 +01:00
Graeme Geldenhuys 2e7876cb9f feat(native): M7c complete — string operations parity with QBE backend
String literals emitted as immortal ARC blobs in .rodata (__sN labels);
leaq __sN+12(%rip) loads the data pointer convention. String assignment
uses _StringAddRef/_StringRelease ARC. WriteLn(S) routes to _SysWriteStr.
Concatenation (boAdd on tyString) calls _StringConcat. Subscript S[I]
calls _OrdAt. Built-ins: Length, Pos, Copy, UpperCase, LowerCase, Trim,
SameText, IntToStr, StrToInt, PChar (identity), string()→_StringFromPChar.
Format(Fmt,...) builds a 16-byte-per-slot args array on the stack and
calls _StringFormatN. Delete/SetLength use ARC reassignment. String
params and return values are 8-byte pointers. String locals released in
the function epilogue. 19 new AssertRunsOnBoth e2e tests.
2402 tests pass; FIXPOINT_OK.
2026-06-03 16:42:24 +01:00
Graeme Geldenhuys da57f0a00c chore: remove progress-native-backend.txt from tracking; add to .gitignore 2026-06-03 14:38:13 +01:00
Graeme Geldenhuys d6c98c1459 feat(native): M7b complete — class system, method dispatch, method pointers
Implements the full class system for the native x86_64 backend:

Data section:
- EmitClassSection emits .data vtables (typeinfo ptr + virtual method ptrs),
  typeinfo blocks (8-quad: parent, itab, class-name ptr, method table, size,
  field-cleanup fn, vtable, attrs), class-name strings (ARC header + ASCII
  null-terminated), and method tables (count + name-ptr/code pairs).
- EmitFieldCleanupFn emits _FieldCleanup_<Class> stubs in .text.
- NativeMangle strips the leading $ from vtable ImplName entries (StrAt/
  StrCopyTail) to match the QBE backend mangling convention.
- EmitClassNameString is idempotent (FClassNameEmitted dict) so MethodAddress
  and EmitClassSection can both reference the same label without duplicating it.

Code section:
- EmitClassMethods iterates AProg.Block.TypeDecls (not ProcDecls) so that
  class method bodies in CD.Methods (post LinkClassMethodImpls) are emitted.
- Constructor calls: _ClassAlloc(size, cleanup) -> wire vtable ptr -> call ctor body.
- Class variable assignment: _ClassAddRef(new), load old, _ClassRelease(old), store new.
- Class field access (read/write) through Self pointer via leaq + field offset.
- MethodAddress resolves to a _MethodAddress(instance, name_ptr) RTL call using
  the class-name string +12 (ASCII data) offset.

Method-pointer (of-object) calls:
- EmitMethodPtrCall: leaq slot->%rcx; movq 0(%rcx)->%r10 (Code);
  movq 8(%rcx)->%r11 (Data); args into %rsi/%rdx/...; movq %r11,%rdi; callq *%r10.
- Method-pointer cast assignment (TFoo(M)) copies 16 bytes via memcpy.
- tyProcedural IsMethodPtr globals now emit two .quad 0 entries (16 bytes) in
  EmitDataSection; tyClass globals registered and emitted as .quad 0 (8 bytes).

Also adds --emit-asm flag to the compiler CLI:
- Prints native assembly to stdout (analogous to --emit-ir for QBE IR).
- Implies --backend native; skips the cc linking step.
- --output is not required when --emit-asm is set.

TestRun_Native_IndirectCall_MethodPtr promoted from Ignore to AssertRunsOnBoth.
2383 tests pass.
2026-06-03 14:27:09 +01:00
Graeme Geldenhuys 23ee55e0ca docs(progress): mark M7a complete — sret record-returning functions done; M7b (class system) scoped 2026-06-03 14:02:11 +01:00
Graeme Geldenhuys bb1cfb7676 feat(codegen): native backend M7a — record-returning functions (sret)
Implement the sret calling convention for functions/procedures that
return a record type, matching the QBE backend's hidden-first-pointer
approach:

Callee side (EmitFunctionDef):
- FSretFunc flag set in BuildFrame when ResolvedReturnType.Kind = tyRecord
- Result slot becomes an 8-byte pointer slot (nil type = pointer-size)
- Prologue spills the hidden sret %rdi into the Result slot (IntIdx starts
  at 1 so normal params continue at %rsi, %rdx, ...)
- No Result initialisation (caller's buffer is already zeroed by caller)
- Epilogue emits plain ret (no return value in %rax/%xmm0)

Field writes through Result (TFieldAssignment with RecordName='Result'):
- Load the sret pointer from the Result slot into %rcx
- Write through %rcx + field offset

Caller side (EmitSretCall):
- leaq dest → %r10 before arg evaluation (survives clobbers)
- Call memset(%r10, 0, TotalSize) to zero the destination buffer
- Reload %r10 after memset (caller-saves may be clobbered)
- Evaluate normal args and push; pop into %rsi/%rdx/... (index 1+)
- movq %r10, %rdi to place sret pointer as hidden first arg
- callq function

TAssignment detection: when LHS is a record and RHS is a record-returning
TFuncCallExpr, dispatch to EmitSretCall instead of EmitExprToEax.

TestRun_Native_RecordReturnFunction promoted from Ignore to AssertRunsOnBoth.
2383 tests pass; FIXPOINT_OK.
2026-06-03 13:57:14 +01:00
Graeme Geldenhuys 07b73bf420 docs(progress): mark M6 complete — float parity in native backend 2026-06-03 13:38:02 +01:00
Graeme Geldenhuys c09664dd76 feat(codegen): native backend M6 — float (Double/Single) parity
Double and Single variables, literals, arithmetic, comparisons,
WriteLn output, value parameters, and function return now work in
the native x86_64 backend, matching QBE output on all test cases.

Implementation details:
- EmitExprToXmm0: evaluates float expressions into %xmm0.  Binary ops
  use the stack (subq/movsd) to save left operand, evaluate right into
  %xmm0, then pop left into %xmm1 and combine via addsd/subsd/mulsd/
  divsd (boSlash, not boDiv, for `/`).
- EmitLoadFloat / EmitStoreFloat: movsd/movss between memory and %xmm0.
- Float globals: .double 0.0 / .float 0.0 in .data section; registered
  in EmitProgram alongside integer globals.
- Float literals: stored as .double/.float constants in .rodata, loaded
  via RIP-relative movsd/movss.  Label prefix .LF to avoid collision
  with loop labels.
- Single coercion: when LHS is Single and literal is Double (all
  TFloatLiterals default to tyDouble), cvtsd2ss converts before store.
- Float comparisons: EmitCondBranch detects float operands and emits
  ucomisd/ucomiss + conditional jump directly (CF/ZF flags).
- EmitFunctionDef prologue: spills float params from %xmm0/%xmm1/...
  (tracked by XmmIdx counter, independent of integer IntIdx counter).
  Result slot initialised with xorpd/xorps; epilogue loads Result into
  %xmm0 for float return types.
- EmitCall: detects mixed/float param calls; pre-allocates N×8 stack
  slots, evaluates args left-to-right into slots, then loads into
  separate int/xmm register sets.  Pure-integer calls use the existing
  push/pop strategy unchanged.
- EmitWrite: checks tyDouble/tySingle before integer fallthrough;
  evaluates via EmitExprToXmm0 and dispatches to _SysWriteDouble /
  _SysWriteSingle with fd in %edi, value in %xmm0.

8 new e2e tests (all AssertRunsOnBoth): DoubleGlobal, DoubleLocal,
DoubleArithmetic, DoubleComparison, DoubleWriteLn, SingleGlobal,
DoubleFuncParam, DoubleFuncReturn.  2383 tests pass; FIXPOINT_OK.
2026-06-03 13:37:43 +01:00
Graeme Geldenhuys f4baabb9ce fix(codegen): WriteLn(Double/Single) crashed QBE with type mismatch
EmitWrite had no tyDouble/tySingle case; floats fell through to
_SysWriteInt with a d-typed temp, which QBE rejected as an invalid
argument type.

Add _SysWriteDouble and _SysWriteSingle RTL stubs in rtl.platform.pas
and rtl.platform.posix.pas (using the existing _DoubleToStr/_SingleToStr
functions from blaise_float.pas).  EmitWrite now dispatches to these
stubs so WriteLn(someDouble) and WriteLn(someSingle) compile and run
correctly without requiring DoubleToStr at the call site.

Two new e2e tests (TestRun_WriteLn_Double_Direct, TestRun_WriteLn_Single_Direct)
verify direct float output; 2375 tests pass.
2026-06-03 13:22:10 +01:00
Graeme Geldenhuys 613f326fa1 test(native): M5 complete — method-pointer call deferred to M7 with placeholder
Method-pointer calls (of object) depend on class allocation and field
access not yet in the native backend; deferred to M7 alongside full class
support.  Added TestRun_Native_IndirectCall_MethodPtr as a placeholder:
QBE path verified via CompileAndRunWithRTL, native path Ignored with a
clear TODO M7 comment.  When M7 lands, replace Ignore with AssertRunsOnBoth.

M5 is now complete for the integer-family subset.
2026-06-03 13:01:46 +01:00
Graeme Geldenhuys dd30d4d7cd test(native): add M7 placeholder for record-returning functions
QBE path is verified and asserted; native path is Ignored with a
TODO M7 comment until sret/aggregate return is implemented.  When M7
lands, replace Ignore with AssertRunsOnBoth.
2026-06-03 12:55:23 +01:00
Graeme Geldenhuys e802c0f072 feat(codegen): native backend M4 — static array element read/write
Implement TStaticSubscriptAssign (write A[I] := V) and static array
element reads (TStringSubscriptExpr with tyStaticArray inner) for
integer-family elements.  Handles zero and non-zero LowBound, local
frame slots and global RIP-relative storage.

Element address: base + (Index − LowBound) × ElementType.RawSize,
computed via imulq/addq.  TIdentExpr with tyStaticArray returns the
array's base address via leaq (not a scalar load) so subscript
expressions can use it as a base pointer.

Frame and data section:
- AddSlot uses AType.RawSize (rounded to 8) for tyStaticArray, matching
  the existing record handling.
- EmitDataSection emits .skip N / .balign 8 for global array vars.
- EmitProgram registers array-typed globals alongside integer and record
  globals.

Tests: 3 new e2e tests run on both backends via AssertRunsOnBoth —
StaticArrayGlobal, StaticArrayLocal, StaticArrayNonZeroLow.

M4 is now complete.
2026-06-03 12:34:32 +01:00
Graeme Geldenhuys d2fbb80911 feat(codegen): native backend M4 — record field read/write
Implement TFieldAssignment (write) and TFieldAccessExpr (read) for
integer-family fields of record-typed locals and globals.

Layout:
- AddSlot now allocates TotalSize bytes (rounded to the next 8-byte
  boundary) for record-typed frame slots, not a fixed 8.
- EmitDataSection emits .skip N / .balign 8 for global record vars.
- EmitProgram registers record-typed globals alongside integer globals.

Field address computation: base address + FieldInfo.Offset.  When
offset = 0 the base operand (VarOperand or name(%rip)) is used directly;
non-zero offsets leaq the base into %rcx and then use N(%rcx).

IntByteSize already returns 8 for pointer-width types (tyProcedural etc)
from the previous commit; no change needed there.

Tests: 3 new e2e tests run on both backends via AssertRunsOnBoth —
RecordGlobal (program-level record var), RecordLocal (record local inside
a function), RecordParam (record fields passed as scalar arguments).
2026-06-03 12:28:11 +01:00
Graeme Geldenhuys c419e054b5 feat(codegen): native backend M5 — bare indirect (procedural-type) calls
Support calling through a procedural-type variable (function/procedure
pointer):

  @FuncName in expression context: leaq funcname(%rip), %rax (TAddrOfExpr
  with a tyProcedural inner TIdentExpr).

  EmitCallIndirect: loads the pointer from the variable slot into %r10
  (caller-saved, not clobbered by arg evaluation in %rax), pushes args
  left-to-right, pops into SysV arg registers, then callq *%r10.  Wired
  into TProcCall.IsIndirectCall (statement) and TFuncCallExpr.IsIndirectCall
  (expression).

  IntByteSize: extended to return 8 for pointer-width types (tyProcedural,
  tyPointer, tyClass, tyString, tyPChar, tyMetaClass) — previously fell
  through to the default 4, causing proc-pointer globals to be stored/loaded
  as .long/.movl, silently truncating the 64-bit code address.

Tests: 2 new e2e tests (BareProc, BareFunc) run on both backends via
AssertRunsOnBoth.  Method pointers (of object) deferred pending
TFieldAssignment support.
2026-06-03 12:09:46 +01:00
Graeme Geldenhuys f47849a2aa feat(codegen): native backend M5 — >6 integer arguments via stack passing
SysV AMD64 ABI: args 0-5 in rdi/rsi/rdx/rcx/r8/r9; args 6-7 on the stack
at +16(%rbp)/+24(%rbp) in the callee (right-to-left push order so arg6 is
closest to the frame).

Caller (EmitCall): evaluates all args left-to-right and pushes them. For >6
args, pops the stack args (highest index first) into %r10/%r11 (caller-saved
scratch), pops reg args into their registers, then re-pushes the stack args in
ABI order (argN-1 deepest, arg6 on top at callq). Cleans up with addq after
the call. Supports up to 8 total args; more raises a clear error.

Callee (BuildFrame): params 0-5 get negative %rbp slots (spilled from regs in
the prologue as before); params 6-7 get positive %rbp offsets (+16, +24) so
VarOperand emits N(%rbp) for them — no spill needed, the caller already placed
them there. AddSlot now stores offsets as negative integers; VarOperand checks
the sign to emit -N(%rbp) for locals vs N(%rbp) for stack params.

Tests: 2 new e2e tests (SevenArgs, EightArgs) run through both QBE and native
backends via AssertRunsOnBoth.
2026-06-03 11:56:13 +01:00
Graeme Geldenhuys 682f4e0121 refactor(native): use diamond operator at all generic constructor call sites
Replace explicit type argument repetition with <> now that the feature is
available.  Five sites in TX86_64Backend: FDataGlobals, FBreakLabels,
FContinueLabels (in Create) and FFrame, FFrameTypes (in BuildFrame).
2026-06-03 11:44:27 +01:00
Graeme Geldenhuys d4f9f9e4a3 feat(lang): diamond operator — infer generic type args from LHS in assignments
TFoo<>.Create on the RHS of an assignment infers all type arguments from
the declared type of the LHS variable, eliminating the redundant repetition:

  var S: TStack<string>;
  S := TStack<>.Create;          { was: TStack<string>.Create }

  var D: TDictionary<string, Integer>;
  D := TDictionary<>.Create;     { infers both K and V }

Works for any number of type parameters.

Implementation:
- Parser: detects IDENT tkNotEquals DOT (the lexer folds '<>' into a single
  tkNotEquals token) in expression context and stores 'TFoo<>' as the sentinel
  RecordName in TFieldAccessExpr.
- Semantic: ResolveDiamond() in AnalyseAssignment replaces the '<>' sentinel
  with the full concrete type name from the resolved LHS type, before the
  normal constructor-call analysis proceeds.

Docs: grammar.ebnf and language-rationale.adoc updated.

Tests: 6 IR-level tests in cp.test.generics (parser sentinel, semantic
inference for 1 and 2 type params, IR identity with explicit form);
2 E2E tests in cp.test.e2e.misc (single-arg and two-arg, compile+run).
2026-06-03 11:29:29 +01:00
Graeme Geldenhuys 8e4608380b refactor(native): replace TStringList stacks/globals with proper generic types
Use semantically correct collection types now that the Pointer→class ARC
fix is in place:

- FBreakLabels/FContinueLabels: TStringList used as LIFO stacks (Add/Delete-
  last/Strings[Count-1]) → TStack<string> with Push/Pop/Peek.

- FDataGlobals + FGlobalTypes (parallel TStringList + TDictionary): merged
  into a single TOrderedDictionary<string, TTypeDesc>.  ContainsKey replaces
  IndexOf, Keys[I] replaces Strings[I], and TryGetValue works directly on the
  one map.  Insertion order is preserved for .data section emission.
2026-06-03 11:05:48 +01:00
Graeme Geldenhuys dcd70a7176 fix(arc): emit _ClassAddRef when assigning Pointer to class-typed variable
When a Pointer-typed expression (e.g. from TStringList.Objects[]) is
assigned to a class-typed ARC-managed local, the codegen previously fell
through to a plain scalar store — emitting no _ClassAddRef.  The variable
then received a spurious _ClassRelease at scope exit, imbalancing the
refcount.

This was the root cause of the 3rd-generic-instantiation linker error:
FindGeneric returns TObject from FGenerics.Objects[] (Pointer-typed), so
each call to InstantiateGenericInterface emitted one extra _ClassRelease
on the template object.  After two calls the template was destroyed; the
third call received a dangling pointer.

Fix: add a branch in EmitAssignment for
  ResolvedLhsType.Kind = tyClass AND Expr.ResolvedType.Kind = tyPointer
that mirrors the existing tyClass→tyClass path (retain new, release old,
store), always emitting _ClassAddRef since a raw Pointer value never
carries a pre-existing owned reference.

Also remove the debug WriteLn trace left in TGenericInterfaceDef.Destroy.

Tests added:
- cp.test.arc: IR-level check that Pointer→class assign emits _ClassAddRef
- cp.test.e2e.arc: three instantiations of the same generic class all
  compile, link, and produce correct output
2026-06-03 10:55:18 +01:00
Graeme Geldenhuys f5f99ab78a feat(codegen): native backend M5 — break/continue/exit + test upgrades
Add break, continue, and exit statement support to the native x86_64
backend.  FBreakLabels/FContinueLabels stacks are pushed before each
loop body and popped after; break jumps to the loop-end label, continue
to the increment (for) or condition (while/repeat) label.  Exit jumps
to FExitLabel at the function epilogue; Exit(Value) uses the semantic
pass's synthesised ResultAssign.

Upgrade six existing QBE-only e2e tests to AssertRunsOnBoth: ForBreak,
ExitFromFunction, ConstInt, ConstNeg, DefaultParam, TypeCastIntByte.
Four new native e2e tests: ForBreak, WhileContinue, ExitFromFunction,
ExitValueShorthand.

2351 tests pass; FIXPOINT_OK.
2026-06-03 09:56:52 +01:00
Graeme Geldenhuys dc7680fe93 feat(codegen): native backend M5 — var/out parameter support
Var/out params pass the caller's address via leaq (locals/globals);
the param slot holds a pointer (always movq spill); reads dereference
through the pointer, writes store through it.  Pass-through (var param
forwarded to another var param) loads the pointer from the slot.

Uses AST IsVarParam flags from the semantic analyser — simpler and
consistent with the QBE backend.  Wider-int var params (Int64, Byte)
dereference at the declared type's width.

Existing QBE-only e2e tests (VarParamSwap, ConstParam) upgraded to
AssertRunsOnBoth.  Four new native e2e tests: VarParamSwap,
VarParamPassThrough, VarParamWiderInt, OutParam.

2347 tests pass; FIXPOINT_OK.
2026-06-03 09:23:27 +01:00
Graeme Geldenhuys 2d509cf764 tooling: rolling-bootstrap script for development-checkout builds
Add scripts/rolling-bootstrap.sh to rebuild the self-hosting chain from
the last release binary up to the checked-out revision. A released binary
can only compile source up to its own feature set, so between releases it
can no longer cold-bootstrap master directly — the moment a feature is
taught to the parser in one commit and used in the runtime/compiler in a
later commit, the gap opens. The script replays the chain commit-by-commit
(each step built by the previous step's binary), carrying the binary
forward, and installs the result as releases/v<version>-pre/.

It runs in a throwaway git worktree, so the live tree (including
uncommitted changes) is untouched; it passes QBE= and BLAISE= into the
runtime Makefile and uses the main repo's built QBE; and it smoke-tests
each step. A failed step names the exact commit (BOOTSTRAP_BROKEN),
making it usable as a CI check that the two-step discipline holds.

Document the prerequisite (place the last release binary under
releases/vX.Y.Z/, which is gitignored) and usage in scripts/BOOTSTRAP.adoc,
with a pointer from README.adoc.
2026-06-03 08:05:16 +01:00
Graeme Geldenhuys 6711a6cfdd fix: green the test suite — array-const link collisions and TComponent ARC
Three fixes that were causing six e2e failures:

Local array consts collided at link time. Function/block/unit-level
`const X: array[...] = (...)` were emitted as `export data $X` with no
mangling. The RTL's own `Days` const (rtl.platform.posix.pas) then
clashed with any user program declaring a `Days` const
(`multiple definition of 'Days'`). Local array consts now use a mangled,
file-local label (`__bac_N_Name`): the mangled name is set in semantic
(AnalyseArrayConstDecls -> ResolvedQbeName / ConstArrayQbe), carried to
the bare-ident read path via TIdentExpr.ConstArraySymbol, and emitted as
non-exported `data` so two separately-compiled objects cannot clash.
Class/record consts keep their exported, type-qualified label.

TComponent fought ARC and crashed. The destructor force-Freed children
it held only as raw pointers, while a caller's variable still referenced
them — a use-after-free at scope exit, surfacing as infinite
Destroy->RemoveComponent->ClassRelease recursion. Ownership is now honest
with ARC: FOwner is [Unretained]; AddComponent takes a strong ref via
_ClassAddRef; Destroy releases the owner's hold rather than Freeing, so a
child the caller still holds survives and an unheld child is destroyed;
RemoveComponent only unlinks the slot (the dying child's ref already
reached zero).

Deflaked TestRun_Thread_Terminate_Flag. The worker could finish via its
`I > 1000` break before the main thread's Terminate landed, racing the
printed flag between 0 and 1. The loop now exits only via the terminate
flag (with a high safety cap), so FTerminated is deterministically True.

Unit tests in cp.test.constants.pas updated to assert the mangled,
non-exported label.
2026-06-03 01:49:00 +01:00
Graeme Geldenhuys 576f60d807 fix(stdlib): TStringList.Text/LoadFromFile preserve verbatim lines
SplitIntoList — the sole backend for TStringList.SetText and LoadFromFile —
trimmed leading and trailing spaces from each split segment. That is correct
for CSV-style splitting but wrong for line-splitting: a TStringList must store
each line verbatim, including indentation.

Removed the trimming so each segment is added as Copy(S, Start, Len) untouched.
SplitIntoList has exactly one caller (SetText), so no trimming-dependent code
is affected.

This also fixes a latent test-infrastructure bug: the threaded test runner
parses each subprocess suite's stdout via TStringList.Text and identifies
failure-detail lines by their two-space indentation. With indentation stripped,
those lines never matched, AddFailure was never called, and the merged result
reported zero failures — so the plain summary printed OK even when [Threaded]
suites failed. With lines preserved, the summary now correctly reports failures.

Regression test: TestRun_TextSet_PreservesLeadingWhitespace asserts on raw
stdout so the spaces inside [] are checked without round-tripping through Text.

Self-hosting fixpoint verified clean.
2026-06-03 00:40:00 +01:00
Graeme Geldenhuys 99b7761bf4 refactor: use Exit(X) shorthand for Result-then-Exit early returns
Now that Exit(X) is supported (0f63626), replace the historical
'Result := X; Exit;' workaround with the Exit(X) function-result shorthand
across compiler, runtime, and stdlib (305 sites).

Only same-indentation pairs are merged: a 'Result := X;' immediately followed
by an 'Exit;' at the same indentation level. Sites where the trailing 'Exit;'
is less-indented than the assignment are left untouched — there the Exit
belongs to an enclosing for/else/if block, not to the assignment, so merging
would change control flow (e.g. the xor/and numeric-result branch in
uSemantic falling through to the Boolean-operand check). 14 such sites are
deliberately not converted.

Three converted sites sit inside a real try/finally (uSemantic overload/
generic resolution); each try body already contained bare Exit; statements
and its finally body is a plain local-list Free, so control flow is unchanged.
A new e2e test (TestRun_ExitValueThroughFinally) confirms Exit(X) from inside
a try/finally runs the finally body and returns the value.

Self-hosting fixpoint verified clean (stage-2 IR == stage-3 IR).
2026-06-03 00:33:50 +01:00
Graeme Geldenhuys 0f63626397 feat(lang): Exit(Value) function-result shorthand
Inside a function, Exit(X) now assigns X to Result and returns, matching
Delphi/FPC:

  function Classify(n: Integer): Integer;
  begin
    if n < 0 then Exit(-1);
    if n = 0 then Exit(0);
    Result := 1
  end;

Pipeline:
- AST: TExitStmt gains Value (the parsed X) and ResultAssign (a
  synthesised 'Result := X' built by semantic). CloneStmt copies them.
- Parser: the tkExit branch parses an optional (Expr) after Exit.
- Semantic: Exit(X) is valid only inside a function (Result in scope);
  it is rewritten into a 'Result := X' TAssignment that is analysed like
  any assignment, so it inherits return-type compatibility checking and
  the widening / ARC handling for string and class returns. Exit(X) in a
  procedure, or with a type-incompatible X, is a clear error.
- Codegen: emits the synthesised assignment (via EmitAssignment) before
  the normal exit jump; CollectAddressTakenStmt walks it too. Bare Exit
  is unchanged.

Tests: 5 IR/semantic cases in cp.test.flowjumps (parse attaches value;
function OK; procedure + type-mismatch errors; codegen stores Result
then jumps) and an e2e in cp.test.e2e.controlflow covering int and
string (ARC) returns plus fall-through. Grammar (ExitStmt) and
language-rationale updated. Full suite 2341 tests pass; fixpoint clean.
2026-06-03 00:06:52 +01:00
Graeme Geldenhuys 703e44dfa1 feat(semantic): accept set literals as 'set of' arguments
A bracket literal can now be passed directly where a `set of <enum>`
parameter is expected, e.g. Configure([optA, optC]) or Report([]) — no
named intermediate variable needed.

Previously such a call failed overload resolution: a bare [a, b] is
analysed (lacking set context) as an open-array of the enum, which does
not match the set parameter. Now:

- ArgMatchScore matches a TArrayLiteralExpr argument against a set
  parameter — non-empty when the members share the param's base enum,
  and the empty [] against any set — checked before the nil-arg bail so
  [] (which has no resolved type) is handled.
- AnalyseArrayLiteralExpr defers an empty [] to a nil type instead of
  erroring outright; it is only meaningful with a set target.
- After overload resolution, RetypeSetLiteralArgs re-points each matched
  set-literal argument's ResolvedType at the parameter's set type, so
  codegen emits the bitmask (EmitArrayLiteralExpr dispatches on tySet).
- CheckTypesMatch gained a nil-actual guard (a clean "no value type"
  error instead of a segfault on a stray []), and the assignment path
  rejects an empty [] assigned to a non-set LHS.

Fixes a second, latent bug exposed by passing sets by value: a `set of`
value parameter of <=32 members (QBE type w) was spilled in the prologue
with storel, which QBE rejects. Added a tySet case to both param-spill
sites (storew for w sets, storel for l sets).

Tests: 6 IR/semantic cases in cp.test.sets (arg OK / empty / wrong-enum
/ empty-non-set-assign fail, bitmask fold, w-width param spill) and an
e2e in cp.test.e2e.misc. language-rationale updated. Full suite 2335
tests pass; fixpoint clean.
2026-06-02 20:04:17 +01:00
Graeme Geldenhuys 62165dac3d feat(const): set-valued constants (const X = [a, b])
Allow a const declaration to take a set literal on its RHS, e.g.

  const
    Primary       = [cRed, cBlue];        // type inferred: set of TColor
    Both: TDirSet = [dNorth, dEast];      // type annotated
    None: TDirSet = [];                   // empty set

Parser: a tkLBracket branch in ParseConstBlock parses the member
identifier list (or empty []) onto new TConstDecl.IsSet / SetElements.

Semantic: set consts are resolved in the second constant pass
(AnalyseArrayConstDecls), after AnalyseTypeDecls has registered the enum
members. AnalyseSetConstDecl resolves each member to its enum ordinal,
ORs (1 shl ord) into the bitmask, and registers the const with a tySet
type — the declared set type when annotated (members checked against its
base enum), otherwise the inferred 'set of <Enum>' (found-or-created and
defined globally). Members must share one enum; a non-enum member or an
empty unannotated set is a clear error.

CheckTypesMatch now treats two tySet types over the same base enum as the
same type, so an inferred 'set of TDir' const assigns to a TDirSet
variable — set values are structural, not nominal.

Codegen needs no change: a set const is an integer bitmask, emitted by
the existing constant-ident path.

Tests: 9 IR/semantic cases in cp.test.sets (parse, inferred/annotated/
empty OK, mixed-enum / non-enum / empty-unannotated failures, bitmask
fold), plus an e2e in cp.test.e2e.misc. Grammar (ConstRhs) and
language-rationale updated. Full suite 2328 tests pass; fixpoint clean.
2026-06-02 19:48:27 +01:00
Graeme Geldenhuys 3bf5fad1ae test(e2e): run native suite through both QBE and native backends
Add a both-backend e2e harness so a single test exercises both code
generators against one expected value — the native backend's correctness
model is parity with QBE on the same source, so this is the natural way
to test it.

- TBackend = (beQBE, beNative); CompileAndRunOn(backend, ...) is the
  shared compile+run core. CompileAndRun and CompileAndRunNative are now
  thin wrappers over it (one path instead of three near-duplicates).
- AssertRunsOnBoth(Src, ExpectedOut, ExpectedCode) compiles+runs on both
  backends and asserts each matches, tagging failures [qbe] / [native]
  so a one-backend regression is unambiguous.
- The native suite's 20 tests now call AssertRunsOnBoth, so each runs on
  QBE and native (was native-only).

A per-method [Backends(...)] attribute would read better but needs
method-level attribute RTTI (a layout-changing compiler feature Blaise
lacks); AssertRunsOnBoth gives the same per-method granularity today with
no compiler change. Two compiler gaps found and logged in bugs.txt: a
set-valued const, and passing a set literal to a 'set of <enum>'
parameter — both worked around by not using a set-typed API.

Full suite 2318 tests pass; fixpoint clean (test-only change, IR
unaffected).
2026-06-02 19:34:57 +01:00
Graeme Geldenhuys 577b3fc160 chore: remove FPC conditional-compilation directives
Blaise cannot be compiled with FPC, so every {$IFDEF FPC} block was dead
code. Remove them across the compiler:

- uPasTokeniser: collapse the {$IFDEF FPC}/{$ELSE} PosOrd/PosSubstr shims
  to the Blaise (0-based) branch only.
- uSemantic, uParser: drop {$IFDEF FPC}...Free{$ENDIF} manual frees (Blaise
  is ARC; the frees were FPC-only and never compiled under Blaise).
- uConfig: drop the stray {$mode objfpc}{$H+} directive — no other unit
  carries one.

No behavioural change: the removed blocks emitted nothing under Blaise.
Full suite 2318 tests pass; fixpoint IR byte-identical (FIXPOINT_OK).
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 6b5c2b2ce4 refactor(codegen): native backend slot maps use TTypeDesc, drop encoding
With the generic-template lifetime bug fixed, the native x86_64 backend
can again hold a second TDictionary instantiation in its own unit, so
the per-slot type maps revert from the small-integer encoding workaround
back to a plain TDictionary<string,TTypeDesc>.

Removes EncodeIntType/DecodedSize/DecodedUnsigned and the *Code helper
variants; EmitLoadVar/EmitStoreVar/EmitSpillArg/AddGlobal and the frame/
global slot maps now carry TTypeDesc directly, and width/signedness is
read straight off the type via IntByteSize/IsUnsignedInt at each use.
Behaviour is unchanged — same instructions emitted; this is a clarity
cleanup now that the encoding is no longer needed.

Full suite 2318 tests pass; fixpoint clean (verified with a stage-1 that
carries the generics fix; the local v0.10.0-pre reference binary was
refreshed to that fixpoint-verified binary so the default bootstrap
stays self-consistent).
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys d1d89125bf fix(semantic): retain generic templates so repeat instantiation works
A second distinct instantiation of a generic whose template inherits
from a generic interface (e.g. TDictionary<K,V> = class(IMap<K,V>)) was
silently mis-wired: it got no TObject parent and no vtable, so its
$vtable_... symbol was referenced from its typeinfo but never emitted,
and linking failed with an undefined-reference error. Plain generic
classes and single instantiations were unaffected.

Root cause: TSymbolTable.FGenerics (base name -> template AST node) was
non-owning. The IMap template AST node was freed during the first
instantiation and its heap slot reused by an unrelated object; the next
FindGeneric('IMap') returned that dangling slot, the `is
TGenericInterfaceDef` test failed, InstantiateGenericInterface bailed,
the cloned class's parent name was left as 'IMap<...>' instead of moving
to ImplementsNames, and the parent/vtable wiring no-op'd.

Fix: retain a strong reference to every registered template in a new
owning list (FGenericTemplates) so templates outlive any single
instantiation. RegisterGeneric adds the template there in addition to
the FGenerics name index.

Regression test: cp.test.e2e.collections2 compiles a unit holding two
distinct TDictionary instantiations and asserts both link and run. Full
suite 2318 tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 563ce0b274 fix(codegen): print unsigned 32-bit values as unsigned in WriteLn
WriteLn of a Cardinal/UInt32 (or Word) value above 2^31 printed a
negative signed wrap because both backends routed it through the signed
32-bit _SysWriteInt runtime routine (e.g. Cardinal 3000000000 printed as
-1294967296).

Route unsigned 32-bit arguments (tyUInt32, tyWord) through the existing
_SysWriteUInt64 routine instead, zero-extending the value to 64 bits
first. Byte/Boolean/Enum stay on the signed 32-bit writer: their value
range is always non-negative there, matching prior behaviour. Int64 and
UInt64 paths are unchanged.

Both the QBE backend (extuw before the call) and the native x86_64
backend (the value is already zero-extended in %rax) get the fix, keeping
them in lockstep so the native backend's QBE-parity oracle still holds.

Adds an e2e test in each suite (cp.test.e2e.misc and cp.test.e2e.native)
asserting Cardinal 3000000000 prints as 3000000000. Full suite 2317
tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 7a077ab72c feat(codegen): native backend M5 — wider integer family + conversions
Extend the x86_64 native backend beyond 32-bit Integer to the full
integer family (Byte, Word, SmallInt, Int64, UInt32, UInt64, Boolean,
Enum) as program globals, function locals, value parameters and return
values, plus explicit type-cast conversions.

Integer values now flow 64-bit-extended in %rax: loads sign/zero-extend
by width and signedness (movsbq/movzbq/movswq/movzwq/movslq/movl/movq),
stores re-narrow through the matching register sub-view (%al/%ax/%eax/
%rax), and arithmetic/comparison run in 64-bit (addq/subq/imulq, and
idivq+cqto for signed div/mod, divq with a zeroed %rdx for unsigned).
Explicit casts Byte(X)/Word(X)/Int64(X) lower to a truncate-then-extend.

WriteLn now dispatches _SysWriteInt / _SysWriteInt64 / _SysWriteUInt64
by argument width and signedness, matching the QBE backend oracle.
Frame slots are 8-byte-aligned and sized by type; program globals emit
.byte/.word/.long/.quad to match.

Each slot's width+signedness is encoded as a small Integer so the unit
keeps a single TDictionary<string,Integer> instantiation; a second
distinct instantiation of the same generic in one unit currently drops
a vtable definition (logged in bugs.txt, along with the unsupported
Exit(Value) shorthand and broken nested-procedure emission found while
implementing this).

Five new e2e tests cover wider-int globals, Int64 arithmetic, wider-int
params/return, type-cast conversions and signedness/wraparound. Full
suite 2315 tests pass; fixpoint clean (--emit-ir is unaffected, it
always uses the QBE backend).
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys d1efe5888f refactor(codegen): use TDictionary for the native backend frame map
Replace the TStringList-with-Objects[]-as-offset frame map in the x86_64
backend with TDictionary<string, Integer> from generics.collections. The frame
is a name->offset map looked up on every ident read and assignment, so a
dictionary is the correct container for the access pattern, and it removes the
TObject(PtrUInt(...)) cast hack.

This was blocked until the preceding fix: TDictionary implements IMap<K,V>, and
a generic class implementing a generic interface inside a unit previously failed
to link. With that fixed, the compiler can use TDictionary internally.

Verified: compiler bootstraps with TDictionary used in its own unit; 2310 tests
pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 1ac1e6e536 fix(codegen): emit generic interface-instance typeinfo in multi-unit builds
A generic class implementing a generic interface (e.g. TDictionary<K,V> ->
IMap<K,V>) failed to link when instantiated inside a unit rather than the main
program:

  undefined reference to `typeinfo_IMap_string_Integer'
  (referenced by impllist_TDictionary_string_Integer)

The program codegen path emits generic interface-instance typeinfo blocks from
AProg.GenericIntfInstances (EmitInterfaceDefs), but AppendUnit emitted the
generic-class-instance impllist — which references that typeinfo — without ever
emitting the typeinfo itself for the unit's GenericIntfInstances. Single-program
use worked; the unit path left a dangling symbol.

AppendUnit now emits `data $typeinfo_<InstName> = { l 0 }` for each
AUnit.GenericIntfInstances, mirroring the program path.

Adds a CompileAndRunWithUnit e2e helper (writes a user unit to the scratch dir
so the unit, not the program, is the compilation unit) and a regression test
TE2EIMapTests.TestRun_IMap_GenericDictInUnit_LinksAndRuns that instantiates
TDictionary<string,Integer> inside a unit and runs it.

Verified: 2310 tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 23af526bbb feat(codegen): native backend M5 — user functions, parameters, recursion
Add a stack-frame model and direct procedure/function calls to the x86_64
backend:

- Frame model: each function's params, Result, and local vars get a 4-byte
  -N(%rbp) slot, recorded in FFrame (name -> offset). VarOperand picks frame-
  slot addressing for locals and RIP-relative for program globals; variable
  reads, assignments, and the for-loop variable all route through it, so loops
  and assignments work correctly inside functions.

- EmitFunctionDef: standard prologue, reserve a 16-aligned frame, spill the
  incoming SysV argument registers (edi/esi/edx/ecx/r8d/r9d) into the param
  slots, run the body, load Result into %eax for a function, epilogue. Layout
  matches the FPC -O- reference (checked against 3.2.2 and 3.3.1) and the QBE
  backend.

- EmitCall: integer value arguments evaluated left-to-right and pushed, then
  popped into the argument registers (so a complex argument cannot clobber an
  already-set register); call; result in %eax. Wired into both expression
  position (TFuncCallExpr) and statement position (TProcCall).

Scope: integer value parameters and integer/void return. Recursion works
(verified with factorial). Indirect calls, var/out parameters, conversions,
more than six arguments, and non-Integer parameter/return types fail loudly
and are left for follow-up.

Container note: the frame map wants TDictionary<string,Integer>, but a generic
class implementing a generic interface (TDictionary -> IMap<K,V>) currently
fails to link when instantiated inside a unit (open multi-unit typeinfo bug,
see bugs.txt). Since this backend is part of the multi-unit compiler build, it
uses a TStringList for now with a TODO to switch once that bug is fixed.

Output matches the QBE backend on functions, multi-argument calls, calls in
expressions, nested calls, recursion, and for loops over a local variable.

Verified: 2309 tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 3772213f61 feat(codegen): native backend M4 — integer variables and for loops
Add program-level integer variables and the for loop to the x86_64 backend:

- Variables: a program's top-level var block is lowered to RIP-relative .data
  globals, the same model the QBE backend uses. EmitExprToEax reads an integer
  ident as `movl name(%rip), %eax` (named constants fold to an immediate);
  TAssignment stores `movl %eax, name(%rip)`. EmitDataSection collects every
  slot and emits one .data definition each — named program variables are
  exported, hidden compiler-generated slots (.L-prefixed) stay file-local.
  Declared integer vars are registered up front so unused declarations still
  get a slot (matching QBE).

- for loop: the end expression is evaluated once into a hidden global slot
  (Pascal semantics), then cond (i <= end / i >= end for downto) - body -
  increment/decrement - loop. Supports to/downto and nesting.

Output matches the QBE backend on sum-over-for, downto, nested for, the
for-end-evaluated-once rule, and counter-driven while/repeat loops.

Native e2e tests added: vars + for, downto + nested for, counter loops, and
for-end-evaluated-once.

Verified: 2306 tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 3c76343969 feat(codegen): native backend M3 — if/while/repeat control flow
Add control-flow lowering to the x86_64 backend:

- Comparison operators (= <> < > <= >=) in EmitExprToEax: cmpl %ecx, %eax
  followed by the matching setcc and movzbl, yielding a 0/1 in %eax.
- EmitCondBranch: evaluate a condition to %eax, testl, jne true-label, jmp
  false-label.
- NewLabel: unique .L-prefixed local labels.
- if/else, while, and repeat..until in EmitStmt, plus TCompoundStmt block
  flattening. Loop/branch shapes mirror the QBE backend (while: cond-body-cond;
  repeat: body then branch true->end / false->body).

Output matches the QBE backend on if/else, all six comparison operators,
nested if, while (false condition), and repeat (single iteration).

`for` is deferred to M4: it needs a loop variable, which requires local
variable storage (the M4 milestone).

Native e2e tests added: if/else, comparisons + nested if, while/repeat.

Verified: 2302 tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 6afe35cb2e feat(codegen): native backend M2 — integer arithmetic and Write/WriteLn
Add expression and statement lowering to the x86_64 backend:

- EmitExprToEax: integer literals and the binary operators + - * div mod.
  Evaluates left into %eax, pushes it, evaluates right into %eax, pops left
  into %ecx, and combines (addl/subl/imull; cltd+idivl for div, with %edx for
  mod). The push/pop pairs are balanced within each expression, so %rsp stays
  16-byte aligned at every call site. Correct for arbitrary nesting with no
  register allocator.
- EmitStmt / EmitWrite: Write and WriteLn of integer arguments, lowered to
  _SysWriteInt(fd=1, value) per argument plus a trailing _SysWriteNewline for
  WriteLn — the same runtime contract the QBE backend uses.
- EmitProgram now lowers the program body statements between _SetArgs and the
  return-0 epilogue.

Output matches the QBE backend exactly on arithmetic, integer div/mod, nested
parenthesised expressions, operator precedence, negative results, and Write
without a newline.

Native e2e tests added (cp.test.e2e.native): int arithmetic + WriteLn, div/mod
+ nesting, and Write-without-newline.

Verified: 2299 tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 4d41707c48 feat(codegen): native backend M1 — empty program compiles and runs
TX86_64Backend.EmitProgram emits the program entry function in AT&T assembly:
the $main(argc, argv) prologue, the $_SetArgs runtime call, and a return-0
epilogue — mirroring the QBE backend's $main shape. TCodeGenNative.Generate
drives the backend and returns the assembly text; the driver links it via cc
(CompileToNativeDirect) with no QBE step.

`program P; begin end.` now compiles and runs via --backend native and exits 0
with no qbe invocation (verified by strace: only cc is spawned; objdump
confirms main is the native-emitted code).

The backend accumulates assembly in a TStringBuilder (stdlib) rather than a
TStringList: emission is append-only and read once at the end, so a single
growable buffer avoids per-line heap strings and the O(N^2) cost of a final
concat — the same approach the QBE backend's TIRBuffer uses.

Adds CompileAndRunNative to the e2e base (lowers via TCodeGenNative, links,
runs) and a native e2e suite (cp.test.e2e.native) with the M1 empty-program
test. Correctness oracle is parity with the QBE path.

Verified: 2296 tests pass; fixpoint clean.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys d64656c5b5 feat(codegen): scaffold native backend (--backend native / --target)
Wire up the native code-generation backend skeleton (Phase 5 of
docs/toolchain-independence.adoc), gated behind a new --backend flag with
QBE remaining the default.

New units:
- blaise.codegen.target: TTargetDesc (OS, CPU) + PtrSize, ParseTargetName,
  TargetName, HostTarget. Targets accepted: linux-{x86_64,i386,arm64},
  freebsd-{x86_64,arm64}, windows-x86_64, macos-arm64 (only linux-x86_64 has
  a backend so far). All pointer-size logic routes through PtrSize so 32-bit
  targets flip by one value.
- uToolchain: env -> $PATH -> fallback resolution of as/cc/qbe + the RTL
  archive, ported and trimmed from a contributor LLVM branch (LLVM-specific
  slots dropped). POSIX-host path conventions; no RTL change required.
- blaise.codegen.native.backend: TNativeBackend abstract base (own unit to
  avoid a cycle with the concrete backends).
- blaise.codegen.native: TCodeGenNative (implements ICodeGen, will walk the
  AST) + CreateNativeBackend factory.
- blaise.codegen.native.x86_64: TX86_64Backend shell registered with the
  factory.

Driver (Blaise.pas): --backend qbe|native and --target <os>-<cpu> parsing
with validation; --emit-ir always uses TCodeGenQBE (fixpoint protection);
native output writes assembly text and links via CompileToNativeDirect using
the same cc line as the QBE path. Code emission is not yet implemented, so a
native compile fails loudly with a clear per-target message.

Also fix a second pre-existing codegen bug surfaced by passing TTargetDesc as
a var/out parameter: whole-record assignment to a var/out record parameter
(T := L / T := Func) had no case in EmitAssign's IsVarParam branch and fell
through to a single-word store, corrupting the record. Added a tyRecord case
that copies into the dereferenced caller address (sret for a record-returning
call, else EmitRecordCopy).

Regression tests (e2e, since both bugs were link/runtime-level):
- TestRun_Record_AssignToVarParam (records)
- TestRun_Interface_GlobalNil_LinksAndRuns (classes2)

Verified: 2295 tests pass; fixpoint clean (stage-2 IR == stage-3 IR).
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 4eb3b8a3c2 refactor(codegen): introduce ICodeGen interface for backend dispatch
Extract the codegen pass contract (Generate/GenerateUnit/SetSymbolTable/
SetDebugMode/AppendUnit/AppendProgram/GetOutput) into an ICodeGen interface
in the new uCodeGen unit. TCodeGenQBE now implements it, and the Blaise.pas
driver holds an ICodeGen and runs the codegen sequence once polymorphically
rather than via a per-backend branch. This is the seam the upcoming native
backend (TCodeGenNative) plugs into.

ICodeGen is an interface rather than an abstract base class: Blaise objects
are fully ARC-managed, so an interface carries no lifetime penalty, and the
QBE-text and (future) machine-code backends share no implementation that a
base class could host. The driver's codegen object is now ARC-released at
scope exit; the manual Free is removed.

Also fix a pre-existing codegen bug surfaced by making the driver's codegen
variable interface-typed: assigning nil to an interface-typed GLOBAL emitted
a single-slot `storew, $Name` against a symbol that has no data definition
(interface globals use the $Name_obj/$Name_itab fat-pointer pair), causing an
undefined-reference link error. EmitAssign now has a dedicated interface-nil
case clearing both slots with correct ARC release; interface locals were
already correct.

Verified: 2293 tests pass; fixpoint clean (stage-2 IR == stage-3 IR);
valgrind reports no invalid free from the ARC interface transition.
2026-06-02 18:58:58 +01:00
Graeme Geldenhuys 0055f4b480 Status update 2026-06-02 17:31:02 +01:00
Graeme Geldenhuys 7efae6d6e0 docs(benchmark): add QBE 1.3 measurement entry
Records allocator microbenchmarks and compile-step timing after
updating the vendored QBE from v1.2 to v1.3.  The compile step
(QBE translating IR to machine code) is 3.5% faster in the stage-2
binary assembled by QBE 1.3, attributed to the new GVN/GCM passes.
2026-06-02 17:17:54 +01:00
Graeme Geldenhuys c9dde987f0 vendor: update QBE from v1.2 to v1.3
QBE 1.3 (released 2026-06-01) adds Windows AMD64 ABI support
(amd64_win target), improved PIC via extern symbol constants,
GVN/GCM optimisation passes, if-conversion, and various
correctness and performance fixes.

All 2293 compiler tests pass against the new QBE binary.
2026-06-02 16:54:51 +01:00