A `forward;`-declared routine in a program's declaration section swallowed the
following separate implementation (and everything up to the program's `end.`)
as its own body, producing "Expected ';' but got '.'".
Root cause: in ParseMethodDecl, when ACanHaveNestedProcs is true (the
standalone-proc context used for program/unit blocks), a bare
`procedure`/`function` keyword after the signature triggers body parsing so a
nested sub-routine is absorbed into the enclosing routine. A forward decl has
no body, so the keyword that follows it is the SEPARATE implementation — but
the body-trigger fired anyway and consumed it.
Fix: track a local IsForward flag (set when the `forward` directive is seen)
and suppress the body-parse trigger for it, exactly as IsExternal already does.
Mutually-recursive routines declared with forward now parse and run.
Tests: a parser test asserts the forward decl and its implementation are two
separate ProcDecls (forward has no body, impl has one); a dual-backend e2e
test runs mutually-recursive IsEven/IsOdd and checks the computed parity.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite green
on both the QBE-built and native-built runners (3693 tests).
`type TByte = 0..255;` failed to parse — ParseTypeDecl had no case for an
integer-literal subrange, so the RHS fell through to the generic "expected
record/class/..." error.
Blaise does not range-check, so a named subrange is treated as an alias to
the narrowest STANDARD integer type that holds both bounds (0..255 -> Byte,
-10..10 -> SmallInt, etc.). This keeps record/array element layout correct
(TByte is byte-sized) while the value behaves as an ordinary integer. Two
parser helpers do the work: SubrangeAhead (lookahead: IntLit.. or -IntLit..)
and ParseIntegerSubrangeBaseType (parse lo..hi, pick the base type, reject a
descending range). Note Blaise has no 8-bit signed alias, so a signed
subrange that would fit in ShortInt widens to SmallInt.
Only integer-literal bounds form a named type; identifier/enum-bounded
subranges (TLow..THigh, red..blue) are intentionally not handled here (they
are ambiguous as a named-type form).
Tests: parser tests (named subrange, negative bounds, descending-is-error)
and dual-backend e2e tests (named subrange runs; subrange as a record field
and array element with a negative range). grammar.ebnf gains the
IntSubrange rule.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
green on both the QBE-built and native-built runners (3691 tests).
TMoney previously hard-wired banker's rounding (rmHalfEven) at its single
normalisation point. The underlying TDecimal already supports all eight
TRoundingMode values plus custom IRoundingStrategy, so expose that through
TMoney: every constructor and rounding-sensitive operation gains overloads
taking an explicit TRoundingMode or an IRoundingStrategy.
MoneyFromStr('1.005','USD') -> 1.00 (banker's, unchanged default)
MoneyFromStr('1.005','USD', rmHalfUp) -> 1.01
C := A.Add(B, rmCeiling);
M := MoneyFromStr('9.999','USD', myCashRoundingStrategy);
Overloaded: MoneyFromStr, MoneyFromDecimal, Add, Subtract, Multiply (each
gains a TRoundingMode form and an IRoundingStrategy form). MoneyFromInt,
MoneyZero, MultiplyInt and Negate are exact (no fraction introduced) and
need no mode. The existing no-mode calls are unchanged and keep banker's
rounding, so this is fully backward-compatible.
Internally a single MakeMoneyS(strategy) core does the normalisation; the
mode form delegates via StandardRounding(Mode) and the default via
rmHalfEven — one rounding code path.
Tests: 5 IR/semantic tests and 8 dual-backend e2e tests covering the mode
overloads (HalfUp/Down/Ceiling), the custom-strategy overload, the
default-stays-banker's guarantee, and rounding on Add/Multiply/constructors.
Docs: language-rationale.adoc updated to record that rounding is swappable
per call (default banker's), not hard-wired.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
green on both the QBE-built and native-built runners (3686 tests).
EmitSretCall (the sret path for free-function record returns) pushed one
stack slot per LOGICAL argument, so an interface argument — a fat pointer
that occupies TWO integer-register slots (obj + itab) — had its second slot
dropped. The arguments landed in the wrong registers and the stack was
left unbalanced, crashing. This is the free-function sibling of the method
fix in 077f13b (EmitMethodSretCall).
Fix: in EmitSretCall's <=5-slot register path, push interface args as two
slots (obj then itab, via EmitMethodArgPush for the direct case and the
saved fat pointer for the hoisted akIntfConsume case), track bytes pushed so
the hoisted-value reloads stay correct, and pop one register per SLOT. The
register-vs-spill guard now uses a new SretUserSlots helper that counts
interface params as two. The >5-slot overflow path raises a precise error
for the (not-yet-supported, untested) interface-in-overflow combination
rather than silently mis-placing registers.
Surfaced by Numerics.Money's rounding overloads: MoneyFromStr(amount,
currency, IRoundingStrategy) is a free function returning a record (TMoney)
with an interface argument, which hit this path.
Regression tests in cp.test.e2e.recordret.pas cover a free function
returning an sret record with an interface arg, with and without a leading
scalar arg, via AssertRunsOnAll (QBE + native parity).
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
green on both the QBE-built and native-built runners (3686 tests).
TMoney is a thin value-type wrapper layering an ISO-4217 currency tag onto
an exact TDecimal amount. The numeric core (TDecimal) stays currency- and
locale-agnostic; currency policy lives entirely in this wrapper, matching
Moneta / money-gem / rusty-money.
Design:
- Currency is an upper-cased ISO-4217 string code; the set is open (unknown
codes are accepted at the fallback minor-unit scale of 2).
- A built-in registry gives each currency its default scale (JPY 0, USD 2,
KWD 3, fallback 2); every TMoney is normalised to its currency's scale on
construction and after every operation, using banker's rounding.
- Cross-currency Add/Subtract/Compare raise EMoneyMismatch (no implicit
conversion); Equals is total (False, not raise, across currencies).
- Immutable value semantics, mirroring TDecimal.
API: free-function constructors (MoneyFromStr / MoneyFromDecimal /
MoneyFromInt / MoneyZero), record methods (Amount, CurrencyCode, IsZero,
Sign, Add, Subtract, Negate, Multiply, MultiplyInt, Compare, Equals,
AmountString, ToString), and the CurrencyScale registry function.
Tests:
- cp.test.numerics.money.pas: 22 IR/semantic tests (resolution, IR shape,
type errors) via TUnitLoader.
- cp.test.e2e.numerics.money.pas: 24 dual-backend e2e tests
(CompileAndRunWithRTL) covering construction + per-currency normalisation,
banker's rounding, case-folding, arithmetic, mismatch raising,
Compare/Equals/IsZero/Sign, the registry, and a realistic invoice flow.
Docs: language-rationale.adoc gains "Currency Amounts — TMoney Wraps
TDecimal, Currency Is a String Tag" (decision + alternatives);
future-improvements.adoc marks Numerics.Money as implemented.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
green on both the QBE-built and native-built runners (3671 tests).
EmitRecordCopy copied every non-managed scalar field with loadw/storew (or
loadl/storel), ignoring the field's actual width. A sub-word field —
Boolean or Byte (1 byte), SmallInt or Word (2 bytes) — was therefore
stored with a 4-byte storew that overran into the following field. When a
Boolean sat immediately before a managed (string / class / dynarray) field,
the wide store clobbered the low bytes of that pointer; the subsequent
_StringRelease / _DynArrayRelease then dereferenced a garbage address and
crashed.
This is exactly TDecimal's layout (FNegative/FInflated Booleans ahead of a
carrier with a managed field), so it blocked Numerics.Money on QBE:
MakeMoney rounds into a TDecimal local and stores it into TMoney.FAmount,
copying the record by value. The native backend was always width-correct.
Fix: route scalar field copies through LoadInstrFor/StoreInstrFor (the same
width-correct helpers already used for element and variable loads), and copy
inline aggregate fields (static array / jumbo-set bitmap) with memcpy.
Regression test in cp.test.e2e.records.pas reproduces the TDecimal-shaped
layout (Boolean flags before a string field, copied through an sret Result
field) and asserts QBE/native parity via AssertRunsOnAll.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
green on both the QBE-built and native-built runners (3625 tests).
A record-returning method that takes an interface parameter crashed (sret
return) or produced garbage (register return) on the native backend. An
interface is a fat pointer (obj + itab) occupying TWO integer-register
slots, but EmitMethodSretCall popped one register per LOGICAL argument
instead of per ABI slot. The interface's second slot was never consumed:
the user arguments landed in the wrong registers, and in the sret case a
stack slot was left unpopped, unbalancing the frame and corrupting return
addresses (the observed 0x1 / 0xffffffffffffffff backtrace).
Fix: in both EmitMethodSretCall register-passing paths (register-return
record and sret-return record, <=4-arg form), pop CountArgSlots(Params)
registers — counting interface params as two — into the integer registers
after sret/Self, rather than ACall.Args.Count. The <=4 register-path
guard is likewise changed to gate on slot count (UserSlots + 2 <= 6), so
an interface arg cannot silently overflow the register file.
This is the root cause of the TDecimal.Divide / RoundTo / SetScale native
crash: each takes a `const IRoundingStrategy` (interface) and returns a
record. With the fix, the full division/rounding battery — all eight
rounding modes, banker's rounding, custom IRoundingStrategy, rmUnnecessary,
the arbitrary-precision inflated path, and div-by-zero — runs identically
on native and QBE.
Tests:
- cp.test.e2e.recordret.pas: three new regression tests for an interface
argument to a record-returning method (sret return, register return,
and a scalar-before-interface ordering check), via AssertRunsOnAll.
- cp.test.e2e.numerics.decimal.pas: the 14 Divide/RoundTo/SetScale/float
e2e tests that were CompileAndRunWithRTLQBEOnly now run dual-backend
(CompileAndRunWithRTL), giving native parity coverage for the whole
decimal arithmetic surface.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
green on both the QBE-built and native-built test runners (3624 tests).
A method call whose receiver is itself a record-returning call —
A.Plus(B).Val() — crashed on the native backend. The receiver
expression was lowered with EmitExprToEax, leaving the record VALUE
(the register-return payload, or the first bytes of the sret buffer) in
%rax, which was then used directly as the Self POINTER. Dereferencing
that garbage segfaulted. QBE handled the chained form correctly, so the
dual-backend e2e harness flagged it as a native/QBE parity divergence.
Fix: when a record method's receiver is a record-returning call,
materialise the call result into a stack buffer and pass its ADDRESS as
Self. The buffer address is carried in callee-saved %rbx so it survives
the argument push/pop sequence; both are freed after the call. Applied
to both EmitMethodCallExpr paths (<=6 and >6 arg slots) and all four
receiver sites in EmitMethodSretCall.
For the sret path, a forwarded %rsp-relative destination (the nested
chain A.Plus(B).Plus(A), where EmitRecordCallSretAt forwards '(%rsp)')
is resolved to an absolute pointer in callee-saved %r14 BEFORE the
receiver-materialisation prologue moves %rsp, so the destination no
longer drifts.
Regression tests in cp.test.e2e.recordret.pas cover the register-return
chain, the sret-record chain, the >5-arg overflow path, the nested
double chain, and the managed-field (TDecimal-shaped) record — all via
AssertRunsOnAll (QBE + native parity).
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full
suite green on both the QBE-built and native-built test runners
(3621 tests).
A single exact base-10 decimal type for financial / exact-decimal use,
replacing the historical Currency/Comp/Extended proliferation with one type.
- Construction: DecFromInt/Int64/Str, plus a safe/exact float split
(DecFromFloat takes the shortest decimal — 0.1 stays 0.1 — while the
dangerous binary-exact path is the explicitly-named DecFromFloatExact).
- Value semantics + value-based equality: 2.0 = 2.00 with a consistent hash,
so it is safe as a dictionary key (unlike Java BigDecimal).
- Arithmetic: Add/Subtract (scale = max), Multiply (scale = sum), Negate, Abs,
arbitrary precision via a decimal-digit magnitude (compact Int64 fast path
inflating on overflow).
- Division + rounding: a layered design — a TRoundingMode enum (8 modes,
banker's default) over an IRoundingStrategy interface users can implement
for custom rounding. Division always carries an explicit scale + mode.
- Formatting: ToString/ToPlainString never use scientific notation;
StripTrailingZeros keeps integer zeros (600 stays 600, never 6E+2).
- Conversions out: ToDouble (lossy), ToInt64 (truncating).
IR tests (32) + e2e tests (46). Add/Subtract/Multiply and the value-semantics
run dual-backend; Divide/RoundTo and float conversion run QBE-only pending
native codegen fixes (logic verified on QBE; see bugs.txt).
Compiling a UNIT (top source is a `unit`, so the pipeline runs in unit-mode and
Prog stays nil) via the default incremental path segfaulted: the incremental
worker setup unconditionally read `Prog.SymbolTable` to seed each dep worker,
dereferencing nil. In unit-mode the symbol table comes from the semantic pass,
not a program node. This is exactly the invocation the runtime Makefile drives
(`blaise --source X.pas --output X.o` per RTL unit), so it broke `make` in
runtime/ once incremental became the default; it was a latent pre-existing bug
in the opt-in incremental path (reproduces at de0ea5d with --incremental).
Fix: seed the worker symbol table from Semantic.GetSymbolTable() when Prog is
nil, matching the non-incremental unit-mode path.
Also pin the runtime Makefile to --no-incremental. That Makefile is itself a
hand-managed separate-compilation system (one object per unit + an explicit
archive list); the compiler's incremental mode would additionally write
per-dependency side-effect objects and skip inlining a used unit's bodies, but
those side-effect objects are not in the archive list — so a cross-unit symbol
(e.g. typeinfo_TRtlPlatform, referenced by the derived TRtlPlatformPosix in a
different unit) was left undefined at link. Whole-program per unit keeps each
unit object self-contained.
Regression test (cp.test.e2e.sepcompile.pas): compile a unit that uses another
unit in unit-mode via the default incremental path, then build and run a
program over the emitted object.
A float (Double/Single) by-value argument passed to a function or method that
returns a record with a managed field (dynamic array / string, i.e. the true
sret return path) was mis-handled by the native backend: every argument was
materialised via EmitExprToEax, the integer-only emitter. A float LITERAL hit
"unsupported expression form TFloatLiteral", and a float VARIABLE would have
been routed through %rax into an integer argument register instead of an %xmm
register — wrong per the SysV ABI. QBE compiled these correctly, so it was a
native/QBE parity divergence.
This blocked DecFromFloat / DecFromFloatExact (TDecimal has a dynamic-array
field) and any MoneyFromFloat-style constructor in Numerics.Money.
Fix: both EmitSretCall (free functions) and EmitMethodSretCall (methods) now
detect a float-typed by-value argument and, when present, evaluate arguments
into stack slots and load them into registers per the SysV ABI — integer args
into the integer argument registers (after %rdi=sret and, for methods,
%rsi=Self), float args into %xmm0.. with an independent counter, and %al set to
the vector-register count. The pure-integer path is unchanged, so the common
case (and native self-compile) is byte-for-byte identical.
Adds three e2e regression tests (cp.test.e2e.recordret.pas), run on both
backends via AssertRunsOnAll: single float arg to a managed-record-returning
function, two float args, and a single float arg to a managed-record-returning
method.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; the new tests
pass on both the QBE and native arms.
Incremental compilation (per-unit .o emission with embedded .bif) is now the
default for every build; --no-incremental opts out and builds a single
whole-program object. The old --incremental opt-in flag is removed.
Fix a latent incremental-mode bug the flip exposed: in incremental /
--skip-dep-codegen mode the dependency unit BODIES are compiled into their own
objects and AppendUnit is skipped for them in the main program codegen. But
AppendUnit was also where a unit's initialization section got registered into
the init-call list, so the program startup ($main) emitted no
`call <Unit>_init` for any dep — every dependency's initialization section
silently never ran. This affected BOTH backends and was why a native-built
TestRunner (built incrementally by default) registered zero tests: each
cp.test.* unit's `initialization`/RegisterTest never executed.
Fix: add ICodeGen.NoteDepInitUnit(name, hasInit), implemented on the QBE
backend and the native backend (virtual on TNativeBackend, overridden by the
x86_64 backend), which records a dep unit's init symbol in dependency order
without emitting its body. The driver calls it for each skipped dep in
incremental mode, so $main emits the calls and they resolve against the
<Unit>_init symbols exported by the per-unit objects. Verified across the full
matrix (native/qbe x incremental/--no-incremental), including chained
multi-unit init ordering (a dep that uses another dep sees the other's init
having run first).
Guard the incremental block so it never runs in --emit-ir / --emit-asm /
--dump-ast modes (those produce stdout text and no objects); this keeps
fixpoint.sh, fixpoint-native.sh and rolling-bootstrap.sh — all of which use
--emit-ir/--emit-asm — unaffected.
Tests: replace the --incremental e2e test with one that exercises the default
incremental path (no flag), plus a new --no-incremental test asserting the
whole-program path leaves no per-unit .o behind.
Remove the eight TestCodegen_Win64_* record-return IR tests and the GenIRWin64
helper. They pinned aspirational QBE Win64-ABI IR for a target the native
backend does not support (only linux-x86_64 is implemented) and that is being
deprecated along with QBE. The native backend miscompiled the in-process
GTarget-global Win64 path these tests drove; rather than carry IR tests for an
unsupported, unrunnable, soon-to-be-removed target, they are dropped. Native
Win64 will be addressed when Windows support is actually built.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
3534 tests / 0 failures on both the QBE-built and native-built test runners.
Phase 3 of the native-default drive — the locally-verifiable items (CI and
rolling-bootstrap changes are deferred to Phase 4, where they become correct
once v0.12.0 is the rolling-bootstrap anchor):
- Help text now marks the backends explicitly: "qbe (deprecated) | native
(default)". QBE is retained as the e2e parity oracle and QBE-fixpoint guard;
removal is a later release.
- fixpoint-native-internal.sh now drives the FULL internal pipeline (internal
assembler + internal linker) against an all-external (gcc assemble + gcc link)
reference, instead of only differing on the assembler. Both internal tools are
now the default toolchain, so the conformance guard must exercise both. Header
comments updated to reflect that it guards the internal linker too and why it
stays a differential probe (the internal assembler buffers the whole object in
memory and OOMs on the compiler's own ~631k-line .s).
- language-rationale.adoc records the decision: native backend + internal
assembler + internal linker as the default toolchain (no external tools beyond
the C runtime startup objects), why QBE is kept-but-deprecated, and the
per-invocation escape hatches (--assembler external, --linker external,
--backend qbe, --incremental).
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
3541 tests / 0 failures on both the QBE-built and native-built test runners.
The native dynamic-array SetLength lowering loaded the array's current data
pointer into %rdi (the first argument to _DynArraySetLength) BEFORE evaluating
the new-length expression. When that expression itself emitted a call — most
commonly SetLength(Result, Length(A)), where Length(A) lowers to a
_DynArrayLength call — the call clobbered %rdi, so _DynArraySetLength resized
the wrong array (the length-expression's operand) instead of the intended one.
The visible symptom: a function taking a dynamic-array parameter and returning
a dynamic array had its Result aliased to the first array parameter's buffer.
SetLength(Result, Length(A)) resized A instead of Result, every Result[I] write
was lost, and the caller got back the first argument unchanged. QBE was correct
throughout; this was a native-only parity divergence.
Fix: evaluate the length expression into %esi first, then load the array
pointer into %rdi immediately before the call — matching the safe ordering the
string and field-receiver SetLength paths already used.
Adds five e2e regression tests (cp.test.e2e.dynarray.pas) covering the result-
from-param-length case, the const-array-param subtract, global-from-global
length, local-from-local length, and length-from-a-user-function-call. All run
on both backends via AssertRunsOnAll; the native arm would have failed before
this fix. This closes a test-coverage gap: no existing test exercised a
SetLength whose length argument emitted a call.
The internal assembler had two TLS-related bugs that caused undefined
symbols in .o files compiled from runtime units using threadvars:
1. sym@tpoff(%reg) operands: the parser included '@tpoff' as part of
the symbol name instead of recognising it as a relocation modifier.
Fixed by stopping symbol-name accumulation at '@', detecting the
'tpoff' suffix, and setting a TpOff flag that emits R_X86_64_TPOFF32.
2. %fs:0 (thread base read): the parser fell through to symbol parsing
and created an undefined reference to literal "0". Fixed by checking
for a numeric displacement before attempting symbol parsing in the
%fs: handler.
Also fix the --help text to show 'native (default)' instead of
'qbe (default)' now that the native backend is the default.
The native backend now uses --assembler internal and --linker internal
by default when the user does not explicitly pass these flags. This
eliminates the dependency on external tools (gcc/as/ld) for native
backend compilation.
Also fixes a linker bug where writing to a currently-executing binary
on Linux would fail with EStreamError — the linker now calls
DeleteFile before creating the output file.
The internal assembler had two bugs in .ascii directive parsing:
1. Comment stripping treated '#' as a comment start even inside quoted
strings, silently truncating string literals containing '#'. This
corrupted .rodata for any program with '#' in string constants
(e.g. QBE IR format strings like "# String literals").
2. The \xHH hex escape syntax was not handled. Bytes encoded as \xE2
etc. were emitted as literal ASCII 'xE2' (3 bytes) instead of the
single byte 0xE2, corrupting UTF-8 string data.
Both bugs were invisible when using the external assembler (gcc) and
only manifested when --assembler internal was used on large programs
with non-ASCII string literals or '#' characters in strings.
Four tests exercise the full compiler-driver pipeline with
--backend native --assembler internal --linker internal:
HelloWorld, StringOps (Format/UpperCase/IntToStr), exception
handling (raise + try/except), and virtual method dispatch.
These catch regressions in TLS relocation, CRT discovery,
duplicate-symbol handling, and PIE emission.
Add --linker internal|external option to the native backend driver,
routing the link step through TLinker instead of shelling out to cc.
FindCrtObjects discovers CRT startup objects (Scrt1.o, crti.o, crtn.o,
crtbeginS.o, crtendS.o) on Debian and RedHat layouts.
Fix SHF_TLS constant from $200 to the correct ELF spec value $400 in
both the reader and writer — the wrong value caused .tbss sections to
be placed twice (once in the normal NOBITS loop, once in the TLS
section), producing doubled negative TPOFF32 offsets and segfaults.
Fix FTlsAddr to use the post-alignment address from PlaceSection rather
than the pre-alignment cursor, which caused off-by-padding TPOFF values.
Change duplicate global symbol handling from error to first-wins
semantics (matching ld behaviour), needed because the main program
object and archive members may both define system runtime functions.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK,
3532 tests pass on both QBE-built and native-built test runners.
Adds CodePointToString(CP: Integer): string, the inverse of CodePointAt /
CodePointFromByteIndex. It encodes a Unicode codepoint (0..U+10FFFF) as
its 1..4 byte UTF-8 sequence, providing the codepoint-aware replacement for
the Char(n) cast used in other Pascal dialects. Chr(n) only writes a single
raw byte, which is invalid for codepoints above 127; CodePointToString emits
the correct multi-byte form. Out-of-range values yield an empty string.
E2E tests cover single-byte ASCII, the two-byte 0xC2 0xB1 encoding of U+00B1,
a round-trip across all four UTF-8 length classes, and out-of-range handling.
The native x86-64 backend is now the default (bkNative). QBE remains
available via --backend qbe. Several codegen bugs that were invisible
while QBE was the default are fixed in this commit:
Native backend fixes:
- EmitLeaqGlobal helper: threadvar globals now emit the correct
fs:0 + @tpoff TLS sequence instead of bare %rip-relative leaq.
Fixes static array write (FreeLists), string subscript assignment,
EmitExprAddr, record memcpy, method-ptr cast, and open-array push
paths that all used raw leaq %s(%rip).
- EmitIncDec: unified local/global paths through VarOperand so
threadvar scalar Inc/Dec (e.g. LargeFreeCount) gets TLS addressing.
- EmitIncDec FAE.Base: Inc(P^.Field) where P is a pointer now
evaluates the base expression instead of leaq on the stack slot.
- FinalizeEmit: data section (.data/.bss/.rodata) now emits once at
the end of unit compilation via FinalizeEmit, not inside each
EmitUnit call. Fixes duplicate/missing string literals in sep-compile.
- EmitFunctionDef AExported: implementation-only helpers in monolithic
mode stay file-local (.globl suppressed) to avoid symbol collisions
with the RTL archive.
QBE backend fixes:
- AppendUnit typeinfo: export prefix added to data $typeinfo_TObject,
$typeinfo_TCustomAttribute, $vtable_TObject, $vtable_TCustomAttribute
so native-built RTL can resolve them.
- FSuppressSystemDefs: decoupled from FExportAll so CreateUnitCodeGen
can export all symbols without suppressing system defs.
Infrastructure:
- runtime/Makefile: BLAISE_FLAGS variable for passing extra compiler
flags (e.g. --backend qbe) from build scripts.
- fixpoint.sh: make clean before RTL rebuild to pick up backend change.
Flip the compiler's default --backend from qbe to native. Native has full
feature parity with QBE (the v0.11.0 cycle's 116 fixes plus the dual-backend
e2e harness brought them level), self-hosts, and gives no external codegen
dependency, source-level debugging, and a path to cross-compilation — so it
is the right default to exercise everywhere.
QBE stays fully supported via --backend qbe and remains the cold-bootstrap
root: --emit-ir still routes to QBE regardless of the default (PickTopDriver
ignores --backend for emit modes), so scripts/rolling-bootstrap.sh,
fixpoint.sh, and CI — all of which pass --emit-ir explicitly — are unaffected
by this change. The only behaviour change is that a plain
'blaise --source X --output Y' (and a plain 'make' in runtime/) now uses the
native backend.
Flipping the default surfaced and fixed a real native gap first (the
unit-to-.o duplicate-system-defs link collision, previous commit); the full
suite is green with the TestRunner built by BOTH a QBE-built and a
native-built compiler, and all three fixpoints pass.
Compiling a unit to a standalone .o with the native backend
(blaise --source U.pas --output U.o) emitted the built-in system
definitions — typeinfo_/vtable_/_FieldCleanup_ for TObject and
TCustomAttribute — into the unit .o. Linking that .o against a consumer
program (which emits its own copies) failed with
'multiple definition of vtable_TCustomAttribute'.
Two gaps fed this:
- The unit-as-top-level path in Blaise.pas built its codegen via
Driver.CreateCodeGen (the program path, separate-compile = False) instead
of Driver.CreateUnitCodeGen (separate-compile = True), so the backend's
FSeparateCompile gate (which already suppresses the system defs) never
engaged. Switched the IsUnitMode branch to CreateUnitCodeGen, with a
CreateCodeGen fallback for any backend lacking a unit codegen.
- TCodeGenNative.GenerateUnit did not propagate FSeparateCompile to the
backend (AppendUnit already did); added the missing propagation for
defensive correctness.
The QBE backend was unaffected (its unit path already suppresses these).
Adds TestNativeBackend_RoundTrip_NoDuplicateSystemDefs in
cp.test.e2e.sepcompile, which pins --backend native so the guard holds
regardless of the compiler's default backend.
EmitByteRhsToEax lowered a single-character string-literal RHS via
Ord(TStringLiteral(AExpr).Value[0]). That subscript-then-Ord idiom is
miscompiled by the self-hosted native stage: a native-built compiler baked a
pointer-sized garbage immediate (movl $0xd291f9ec) instead of the byte, so
S[i] := 'c' wrote junk. It only surfaced in CI, where the test runner is
built by the native-bootstrapped -pre binary; local verification used the
QBE-built fixpoint binary, which lowers the idiom correctly, so the bug was
invisible.
Use StrAt(Value, 0) — the OrdAt-backed byte-read helper already used
elsewhere in this backend and stage-stable — instead of Ord(Value[0]).
Matches the QBE backend, which uses StrAt and was never affected.
Verified by building the test runner with a native-built compiler (the CI
configuration): full suite passes; all three fixpoints clean.
S[I] := <byte> was rejected by the semantic pass ('is not a static array
or dynamic array') for any string — local, global, or var-param — on both
backends. It is now supported as the symmetric counterpart of the S[I]
read: an in-place byte store into a 0-based UTF-8 string.
Because strings are reference-counted and literals are immortal (stored in
read-only memory; the native backend put them in .rodata, so a naive store
segfaulted, while QBE silently mutated the shared literal), the write
performs copy-on-write. New RTL helper _StringUnique(S) returns S when it
is uniquely owned (rc=1) and otherwise allocates a fresh rc=1 copy, releases
the old reference, and returns the copy. Both backends emit
_StringUnique -> write the result back to the slot -> storeb, so the slot
keeps exactly one owned reference and mutating one alias never disturbs
another (Delphi/FPC UniqueString semantics).
The RHS accepts a numeric ordinal, Chr(n), or a single-character literal
(the byte-shaped forms used in place of a Char type). PChar subscript
writes keep their existing in-place path (raw pointer, no ARC header).
Dual-backend e2e tests (write, copy-on-write aliasing + literal reuse,
var-param) in cp.test.e2e.stringops. Updates language-rationale.adoc
(writable subscript + COW) and grammar.ebnf (string as a subscript-assign
base).
$hex, %binary and &octal integer literals were rejected inside an
array-constant initialiser — 'array[0..1] of Int64 = ($1, $2)' failed
with 'Cannot resolve array-const element' while the scalar form '= $1'
worked. The parser stores array elements verbatim (prefix and all), and
ResolveConstArrayElem only recognised decimal digits, so any radix-prefixed
element fell through to an identifier lookup and errored.
Fold radix-prefixed elements (with optional leading sign) through
ParseIntLiteral — the same canonical parser the rest of the compiler uses
for scalar and typed consts — so all four radixes, plus underscore digit
separators, behave identically inside an array initialiser. Keeps FPC's
&-octal (the project prefers FPC syntax where FPC and Delphi diverge; the
lexer already tokenised all four radixes).
Documents the radix-prefix rule in language-rationale.adoc.
Implement the Phase B internal-linker pipeline (TLinker in
blaise.linker.elf) on top of the Phase A section merger:
- Symbol resolution: build the global symbol table from merged
sections (STB_GLOBAL over STB_WEAK; two globals = duplicate error),
define the five linker-synthesised symbols (_GLOBAL_OFFSET_TABLE_,
__bss_start, _edata, _end, __TMC_END__), resolve weak-undefined
references to 0 and raise on strong-undefined.
- Static relocations: patch R_X86_64_PC32 / R_X86_64_PLT32 as S+A-P
(PLT32 against an internal symbol relaxes to the direct form, no
PLT). Reject the dynamic-only forms (R_X86_64_64, GOT/TLS) and
absolute 32-bit symbol references with a diagnostic — deferred to
Phase C.
- Layout + emission: assign virtual addresses at a fixed base
(0x400000) in an executable run (.text + .rodata, sharing the first
page with the headers) and a writable run (.data + .bss) on a fresh
page; write a non-PIE ET_EXEC with two PT_LOAD program headers, the
relocated section payloads, and a section-header table for tooling;
mark the file executable.
Platform/arch-variable values (Is64, OSABI, EMachine, BaseAddr,
PageSize) live in TLinkTarget rather than being hard-coded, so FreeBSD
and a future i386 ELF target are a new target record and a PE/Mach-O
container would be a sibling writer behind the same symbol/relocation
core.
The linker is not wired into the compiler driver — it is a standalone
unit exercised only by tests; all backends still link via cc. Driver
integration behind --linker internal is Phase D.
Tests: TLinkerTests (symbol resolution, PC32 cross-object displacement,
R_X86_64_64 rejection, ELF-header/entry-point structure) and
TLinkerE2ETests (hand-written write+exit syscall object linked
internally to an ET_EXEC, executed, asserted on stdout + exit code;
readelf -a reports a clean statically-linked x86-64 executable).
Two native-backend codegen constraints were worked around in this unit:
no string-element assignment through a var-string parameter (append-
style byte building + PChar-local in-place writes), and no capturing
nested functions (private methods instead).
An `inherited Method(args)` whose Self plus arguments occupy more than the
six System V integer argument registers aborted native codegen with
"System V integer arg register index 6 out of range". EmitInheritedCallSeq
only implemented the all-in-registers case: it pushed every argument and
popped each into a register, walking past the six available once Self + the
args exceeded six slots. There was no stack-spill path like the regular
method-call and constructor emitters already have.
Give EmitInheritedCallSeq the same >6-slot handling: when Self + args exceed
six register slots, build the six-register prefix plus an overflow region in
one frame, place Self in slot 0, evaluate each argument into its slot, load
registers 0..5, drop the prefix so the overflow sits at the callee's
[rsp+0..], then static-dispatch to the parent. Up to six register slots
keeps the existing push/pop path. QBE was already correct.
This is independent of the call position and of metaclass dispatch — any
inherited call with seven or more register slots (e.g. a method or
constructor with six+ scalar/aggregate args calling its parent) was
affected.
Adds an e2e regression (cp.test.e2e.classes2) exercising an inherited call
with Self + 7 integer args (8 register slots), asserting correct dispatch
and result on every backend.
(cherry picked from commit b1fc1285ff3984eedaf6a68d58e2d568f1e4c793)
Calling a constructor through a metaclass-typed variable as a statement,
with the result discarded — `C.Create(args);` where `C: class of TBase` —
compiled cleanly but segfaulted at runtime on both the QBE and native
x86-64 backends. The same call in expression position (`o := C.Create();`)
worked.
The semantic pass flags the statement node with IsConstructorCall and
IsMetaclassDispatch, but only the expression-position lowering acted on
them (allocating via _ClassCreate and dispatching the ctor through the new
instance's vtable). The statement-position emitters had no such branch, so
the call fell through to the ordinary virtual-method path: it loaded the
metaclass value (a typeinfo pointer) as if it were the receiver object,
read a bogus vtable from the typeinfo's first word, and called a garbage
slot — crashing.
Add the metaclass-constructor branch to both statement emitters
(TCodeGenQBE.EmitMethodCall and TX86_64Backend.EmitMethodCallStmt): load
the metaclass, allocate via _ClassCreate, marshal the args, dispatch the
ctor indirectly through vtable[(slot+1)*8], then release the freshly built
(refcount 1) instance so the discarded result is freed rather than leaked.
A non-metaclass constructor as a statement (TFoo.Create();) is still
rejected earlier by the semantic pass as 'not a variable', so only the
metaclass shape reaches the new branch.
Adds an e2e regression (cp.test.e2e.classes2) covering zero-arg and
multi-arg dispatch and asserting the instance is freed (destructor count).
(cherry picked from commit 3f719213a838ab886b5db74847e7bef8894c404a)
Accept an anonymous integer subrange as a set base type, e.g.
'set of 0..255' or 'set of 1..10', in both the inline (var/param/field)
and 'type T = set of L..H' declaration positions. The subrange lowers to
the same bitmap machinery as 'set of Byte': the bitmap is sized to H+1
bits and member ordinals are the integer values themselves. Bounds may be
integer literals, named constants, or constant expressions; the lower
bound must be >= 0, the upper <= 255, and the range ascending.
Also fix 'set of Boolean' so a Boolean operand is accepted directly on
either side of 'in' and as the second argument of Include/Exclude, without
an intervening Ord().
Parser handles both 'set of' paths (ParseTypeName and ParseSetDef);
semantic resolution adds ResolveSubrangeSetType for on-demand and
type-decl paths. Adds dual-backend e2e tests and semantic unit tests.
Updates grammar.ebnf and language-rationale.adoc per the language-decision
rule, and removes the now-implemented item from future-improvements.adoc.
The KI-3 ARC triple-release bug — multiple `on E: TYPE do` handlers in one
except block colliding on a single QBE slot — is fixed, so the workaround of
giving each handler a distinct variable name can be reverted. Handler vars are
now the natural `E` again in:
- blaise.testing.pas: the runner's 4-handler dispatch block
(EAF/EIT/E/ETO -> E) — the exact KI-3 scenario, in the test framework's
own outcome classifier.
- cp.test.attributes.pas: ETO -> E.
- cp.test.sets.pas: ESE/EEx -> E.
(There were no {$IFDEF FPC} blocks left to remove — the KI-2 .Free guards and
all other FPC conditionals were already gone from the source tree as part of
the FPC-independence migration; verified by a full-tree directive scan.)
All three fixpoints green; 3501 tests pass, including the framework's own
FAIL/IGNORED/ERROR outcome paths.
An empty bracket literal [] passed to a plain open-array parameter
(`array of T`) failed overload resolution with "No matching overload" on both
backends. A non-empty literal works because AnalyseArrayLiteralExpr infers the
element type and types it `array of <elem>`; an empty [] has nothing to infer
from and was left untyped (nil), so it matched neither the open-array param (nil
arg type) nor the existing set / array-of-const special cases.
Two changes in uSemantic:
* ArgMatchScore now scores an empty TArrayLiteralExpr as an exact (2) match
against any tyOpenArray parameter — an empty open array is valid for any
element type — placed before the nil-arg bail so overload selection accepts it.
* RetypeSetLiteralArgs pins the empty []'s ResolvedType to the chosen formal's
open-array type, so codegen emits a zero-length open array (data=nil, high=-1)
of the right element type.
Overload disambiguation is preserved ([] picks the open-array overload; a scalar
argument still picks the scalar overload), and two equally-matching open-array
overloads still raise a clean "ambiguous overload" error. Works for
`array of string`, `array of Integer`, and `array of const`.
This was the last open item in native-testrunner-investigation.txt; the native
TestRunner now passes the full suite unfiltered. Adds dual-backend e2e tests.
All three fixpoints green; 3501 tests pass.
A multi-dimensional array whose dimensions are enum types — array[TEnum, TEnum]
or array[TEnum, 0..1] — failed to parse with "Expected '..' but got ','". Only
the single-dimension form array[TEnum] was handled; every dimension in the
comma-separated multi-dim path was forced through the lo..hi range parser.
Both the const-array dimension parser (ReadConstArrayDim) and the type-name
parser (ParseTypeName) now recognise an enum-type dimension — a bare identifier
not followed by '..' — and encode it as the range 0 .. @TEnum, reusing the
existing single-dimension '@TEnum' convention. ResolveArrayBound resolves the
'@TEnum' high-bound marker to the enum's last ordinal (member count - 1). This
fixes const, type and var declarations in any dimension position and mix
(enum+range, range+enum, 3-D enum+range+enum).
Adds dual-backend e2e coverage: 2-D enum/enum, mixed dims in both orders,
type+var form, and a 3-D mixed case. All three fixpoints green; 3499 tests pass.
A typed set constant accepted only enum-member identifiers — `const C: TByteSet =
[1, 2, 3]` and `[1..3]` both failed with "Expected set member identifier". The
const-decl set path (a separate, more limited parser than the expression set
literal) is extended to accept:
* integer-literal members (1, 2, 3),
* inclusive ranges `lo..hi` (each endpoint an integer literal, enum member, or
named constant), expanded to all ordinals in range,
* and ordinal base types (set of Byte / Boolean), not just enums.
Parser stores each member verbatim (a range as 'lo..hi'); the semantic pass
(AnalyseSetConstDecl) resolves and expands them via a new ResolveSetMemberOrd
helper, integrating with the existing small-mask and jumbo byte-bitmap const
machinery. Enum-member const sets (the original form) still work.
Adds dual-backend e2e coverage including edge cases: mixed range+literal, a jumbo
(>64-member) set with a range, the empty set, descending-range rejection, and an
enum-set regression. All three fixpoints green; 3495 tests pass.
An array constant with Boolean (or enum, or named-constant) elements emitted the
identifiers verbatim — `(False, True, ...)` became symbol references, failing at
link time on native ("undefined reference to `False'") and in QBE ("unknown
keyword False"). And even once folded, every non-string element was emitted at a
fixed 4-byte stride that did not match the element's actual read width, so a
Boolean array read all-zero.
Two fixes:
* Semantic (AnalyseArrayConstDecls): a new ResolveConstArrayElem folds each
bare-identifier element to its numeric value once the element type is known —
Boolean True/False to 1/0, enum members and named integer/boolean constants to
their ordinal/value. Numeric/float literals pass through. An unresolved
identifier is now a clear semantic error instead of a link failure.
* Codegen (both backends): array-const element data is emitted at the element's
real width — .byte/.word/.long/.quad on native, b/h/w/l in QBE — instead of a
fixed .long/w. Built-in scalar names are mapped directly so the width is
correct even when the codegen has no symbol table set (the e2e in-process path).
Adds dual-backend e2e coverage including edge cases: Byte/Word/Int64 widths,
negative integer pass-through, named-const elements, and a Boolean array indexed
by an enum. All three fixpoints green; 3490 tests pass.
The native backend previously stubbed GenerateUnit and left
SupportsIncremental/SupportsWarmCache False, so --incremental fell back to QBE.
Native now drives the per-unit parallel pipeline itself:
* TCodeGenNative.GenerateUnit emits a single unit in isolation (new
TNativeBackend.GenerateUnit: clear buffer, EmitUnit, return assembly).
* The native driver overrides SupportsIncremental/SupportsWarmCache (True),
CreateUnitCodeGen (separate-compile mode), and LowerToObject (assemble the
unit .s to .o via the internal assembler or `cc -c -x assembler`).
* Separate-compilation mode (FSeparateCompile): unit objects omit the
once-per-program TObject/TCustomAttribute system defs — the program object
provides them — and emit their own file-local string-literal blobs, so a unit
method referencing a literal links cleanly. The system typeinfo/vtable symbols
(typeinfo_TObject, vtable_TObject, and TCustomAttribute) are now .globl so unit
objects can reference the program's single definition.
Works with both the external (`cc -c`) and internal assemblers, and the warm
cache (a second build reuses the per-unit .o). Adds a sepcompile e2e test
building a multi-unit class program with --backend native --incremental.
All three fixpoints green; 3485 tests pass.
A static array of interfaces (Arr: array[0..2] of IGreet) was unusable on the
native backend in three ways, all now fixed:
* Store (Arr[I] := V): the element is a contiguous 16-byte fat pointer, but the
generic static-subscript store wrote only the 8-byte obj and left the itab
garbage. Now computes the element address and delegates to
EmitInterfaceToFieldSlotsAt (obj+itab with ARC for nil/class/interface).
* Read as RHS (G := Arr[I]): EmitInterfaceAssign gained an interface-subscript
source case that copies obj+itab with ARC.
* Method dispatch (Arr[I].M()): EmitInterfaceCall gained a subscript-receiver
case that loads obj/itab from the element address.
A shared EmitIntfStaticElemAddr helper computes the element fat-pointer address
(base + (idx-LowBound)*16), reused by all three paths and by the interface-store
source. EmitInterfaceToFieldSlotsAt also learned an interface-subscript source
(Arr[J] := Arr[I]). Native now matches QBE exactly on the full test
(11/22/22/11/11). TestRun_StaticArrayOfInterface_FatPointer is dual-backend.
(A pre-existing scope-exit leak of interface array elements affects BOTH backends
identically — logged separately in bugs.txt, not a parity gap.)
All three fixpoints green; 3484 tests pass.
Show(MakeFoo(42)), where MakeFoo returns an interface, handed the borrowing
parameter an owned (+1) fat pointer that was never released after the call —
--debug reported one leaked instance on native (QBE already released it).
Added an akIntfConsume hoist kind: EmitArgHoist drives the interface-returning
call in the pre-pass and saves the owned fat pointer (itab then obj, obj on
top); the argument reload pushes both halves as the two interface arg slots; and
EmitHoistEpilogue releases the obj after the call (mirrors akStrConsume). The
2-slot reload is wired into both EmitArgPush and EmitCall's inline argument loop.
--debug leak report is now clean on both backends; two leakcheck tests (QBE +
native) are added. All three fixpoints green; 3484 tests pass.
DateAddDays(MakeDate(...), 5) segfaulted on native while the non-nested form
worked. The args arrived correct — the fault was a stack-alignment bug. The
argument hoist for a record-returning-call argument allocated a 16-byte-aligned
sret buffer (RecArgBufBytes) and then pushed an 8-byte pointer to it, a 24-byte
contribution that left %rsp 8 bytes off 16-alignment. The misalignment was
harmless until the callee chain reached an aligned SSE access — here libm's
mktime (via DateUtils _TimeJoin) executing `movdqa`, which faults on a
misaligned address.
EmitArgHoist now reserves a full 16-byte slot for the saved pointer (subq $8 +
pushq) so the hoist region stays a multiple of 16. The pad sits above the saved
pointer, so reload offsets are unchanged. This is the shared hoist, so it fixes
the alignment for every record-call-argument site, not just DateAddDays.
The DateAddDays e2e test is re-enabled on both backends; a self-contained
regression (record-call result as a record-by-value arg feeding an SSE op) is
added to the gaps suite. All three fixpoints green; 3482 tests pass.
A class-level const array (const Days: array[0..6] of string = (...)) read via
an instance, T.Days[I], returned an empty string on native. The semantic pass
folds the subscript into the field-access node — IsConstant=True with
ConstArraySymbol (the global data label), ConstArrayType (the static-array type)
and PropIndexExpr (the index) — but native's EmitExprToEax IsConstant branch only
handled scalar string/int consts and fell through to an empty string literal.
Added the class-const-array element path: compute
base + (idx - LowBound) * ElemSize and load the element by type (float / string
pointer / narrowed integer), plus a bare-reference case returning the label
address. Mirrors the QBE EmitExpr ConstArraySymbol path. The range- and
enum-indexed class-const-array e2e tests are converted to AssertRunsOnAll.
(Static-array-of-interface element store/read/dispatch remains a separate, larger
follow-up — tracked in bugs.txt; that test stays QBE-only meanwhile.)
All three fixpoints green; 3481 tests pass.
Native lowered Round() with cvtsd2si/cvtss2si, which honour the FPU rounding
mode (round-half-to-even / banker's): Round(2.5)=2, Round(-2.5)=-2. QBE — and
Delphi/FPC — round half away from zero via C99 round(): Round(2.5)=3,
Round(-2.5)=-3. Native now calls round()/roundf() first, then truncates the
integral result, matching QBE exactly. The two Round e2e tests are converted to
AssertRunsOnAll.
All three fixpoints green; 3481 tests pass.
The native backend raised "TMethodCallExpr has no ResolvedMethod" for the two
TObject built-in methods that the semantic pass flags rather than resolving to a
declared method:
* InheritsFrom(C): load the receiver's typeinfo (vtable[0] for a class instance),
evaluate the class-of argument to a typeinfo pointer, and call
_InheritsFrom(self_ti, arg_ti) → Boolean.
* ToString: virtual dispatch through vtable slot 2 (offset 16: typeinfo, Destroy,
ToString), returning the string in %rax — the default returns the class name.
A small EmitMethodReceiverToRax helper loads the instance pointer (expression,
named local/global, or implicit Self). The 7 InheritsFrom tests and the default
ToString test in cp.test.e2e.classes2 are converted to AssertRunsOnAll, pinning
both builtins on QBE and native.
All three fixpoints green; 3481 tests pass.
StrToDouble was lowered only in the integer EmitExprToEax path: it called
_StrToDouble (which returns the Double in %xmm0 per the System V ABI) and left
%rax untouched, so a float assignment (D := StrToDouble(S)) read %rax — a
leftover pointer — and cvtsi2sd'd it into garbage. Native printed values like
95693543141392 instead of the parsed number. Added StrToDouble to the float
emitter (EmitFloatBuiltin) so the result is consumed from %xmm0.
The 7 StrToDouble e2e tests are converted to AssertRunsOnAll, pinning the fix on
both backends. The blanket dual-backend flip of the inline CompileAndRun helper
is documented as deliberately reverted: it breaks on non-deterministic programs
(GetProcessID) and several genuine native gaps remain open (InheritsFrom/ToString
builtins, Round .5-boundary, class const-array, interface-field-assignment RHS,
EmitSretCall record-value arg) — all logged in bugs.txt for individual fixes.
Suites migrate to AssertRunsOnAll as those gaps close.
All three fixpoints green; 3481 tests pass.
Show(MakeFoo(42)), where MakeFoo returns an interface, previously raised
"unsupported interface argument expression" on the native backend. EmitMethodArgPush
now handles a TFuncCallExpr/TMethodCallExpr argument of interface type: it lowers
the call via EmitIntfSretCall/EmitIntfSretMethodCall (which leaves the fat pointer
at (%rsp)), loads obj+itab, drops the sret buffer, and pushes the pair in arg-slot
order. Both backends now print 42. Adds a dual-backend e2e test.
Known follow-up (tracked): the owned (+1) reference of the call result is not yet
released after the call, so --debug reports a single leaked instance for this
specific borrowed-interface-call-result shape. The feature is output-correct; the
ARC release will route through EmitArgHoist (akIntfConsume) like const-string
consume args. All three fixpoints green; 3481 tests pass.
Native backend gaps surfaced by making the RTL/stdlib e2e suites dual-backend:
* 3-arg Supports(Obj, IFoo, OutVar): the native EmitExprToEax only handled the
2-arg boolean form, so the out-var was never populated or registered — a local
out-var read garbage and a global one produced undefined _obj/_itab symbols at
link time. Implemented the 3-arg form (GetItab + ARC retain/release + global
registration), mirroring the QBE EmitSupportsExpr path.
* e2e harness: CompileAndRunWithRTL now compiles and runs on BOTH backends and
asserts parity, instead of QBE only. This brings sysutils, collections,
streams, strutils, tstringlist, tcomponent, threading, arc and the rest of the
RTL/stdlib suites onto the native backend for the first time. New helpers
AssertRTLRunsOnAll/On/One and CompileAndRunWithRTLOn back this; the QBE-only
CompileAndRunWithRTLDebug now delegates to the dual-backend *On implementation.
One pre-existing native bug remains (EmitSretCall mishandles a record-by-value
argument that is itself a record-returning call result, e.g.
DateAddDays(MakeDate(...), 5) — the v0.11.0 release crashes too). That single
case is pinned to QBE via CompileAndRunWithRTLQBEOnly with a tracked TODO; every
other RTL/stdlib e2e now runs on both backends.
All three fixpoints green; 3480 tests pass.
Two related native x86-64 bugs prevented an interface-returning (sret) method
from being called with more register-eligible arguments than the ABI passes in
registers:
1. Caller side: the three sret call emitters (EmitIntfSretCall,
EmitIntfSretMethodCall, EmitClassIntfSretMethodCall) popped every pushed arg
slot into a System V integer register and raised once the index exceeded 5,
instead of spilling the overflow to the stack. A shared EmitSretRegArgs helper
now loads the register-mapped slots and relocates the overflow slots to the
stack in ABI order, returning the byte count the caller reclaims after the
call.
2. Callee side: BuildFrame classified FSretFunc only when allocating the Result
slot, which runs AFTER the parameter-register accounting loop. The loop
therefore read a stale FSretFunc (left over from the previous function) and,
for an sret-returning class/interface method (hidden buffer in %rdi AND Self
in %rsi — two registers), mis-placed the first stack-passed parameter as the
last register parameter. The sret classification is hoisted above the param
loop, and the register count now adds the sret buffer and Self independently.
Both backends now agree (IFoo.Make(1..6) returns 21). Adds three dual-backend
e2e tests. All three fixpoints green; 3480 tests pass.
When two units each privately declared the same C function via
`external name '...'` in their implementation sections, the second unit failed
with "Undeclared function" at the call site.
Root cause: FProcIndex is global across all compiled units, but impl-to-forward
matching used a bare FProcIndex.IndexOf. The second unit's external matched the
FIRST unit's already-indexed decl, so it was treated as that unit's
implementation and was never defined into its own scope.
Fixes:
- Restrict every impl-to-forward match (and the "interface function has no
implementation" checks) to the current unit via a new IndexOfProcInUnit
helper that filters on OwningUnit. The overload-signature loops gain the same
OwningUnit guard.
- When an impl pairs with its forward decl, carry the forward's OwningUnit onto
the impl so the later "has no implementation" scan still finds it.
- Collapse the false "ambiguous overload" that the two same-named externals
produced in the global overload group: SameExternalDecl treats two external
decls with the same link name and arity as one function.
This also clears the obstacle that forced the float-const fix to use only
_DoubleToStr; uSemantic and blaise.assembler.x86_64 may now both declare
_StrToDouble safely.
Regression test: TestDuplicateExternalAcrossUnits_Compiles in
cp.test.e2e.sepcompile (two units declare `external name 'strlen'`, one
invocation, compile + run + assert stdout).
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
3477 tests, 0 failures with the QBE fixpoint binary.
RawDoubleToStr formatted folded float constants via libc snprintf, declared
in Pascal with a fixed Double parameter. snprintf is genuinely variadic; the
SysV x86-64 ABI requires %al to hold the XMM-register count for variadic calls,
and neither backend emits that setup (QBE only for its '...' syntax; the native
EmitCall never sets %al). With %al garbage, glibc could read the double from
the wrong place and emit a wrong string -- an environment-dependent
miscompilation of folded float constants that passed locally but failed on the
CI runner (TestConstExpr_FloatParens_Folds expected d_9; TestConstExpr_
NegativeFloat_Folds expected d_-3).
Replace the variadic snprintf call with the RTL's pure-Pascal _DoubleToStr
(FormatFloat(V,15) == %.15g), which is ABI-safe. strtod is not variadic and is
kept as-is.
Add two e2e regression tests (TestRun_FloatConstFold_Parens /
_NegativeMul) that compile AND run folded-float programs and assert stdout.
The IR-only tests could not catch a host-ABI-dependent fold.
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite
3476 tests, 0 failures with the QBE fixpoint binary.
Self-hosting fixpoint verified on 371,292 lines of QBE IR (QBE, native,
and internal-assembler fixpoints all green). This cycle landed 17 language
features and 30 bug fixes: floating-point constant folding, named-constant
and enum-indexed static-array bounds, generic methods, default array
properties, set-of-ordinal types, case-label ranges, and broad codegen and
ARC hardening across both backends. 3474 tests passing.
When a variable or typed constant declares a Double/Single type but the
initialiser expression is pure-integer (e.g. `var Y: Double = 2 * 3`),
the semantic pass now promotes the folded integer value to a float string
so the QBE backend emits a valid `d_<value>` literal instead of an empty
`d_`.
Static-array declarations in var and type sections now accept an enum type
as the index: array[TColor] of Integer. The array is sized 0..N-1 where
N is the enum member count. Subscript access uses enum ordinal values —
no codegen changes were needed.
Parser: detects array[Ident] (ident followed by ']' with no '..') and
encodes it as 'array[@TEnum] of T' in the type-name string.
Semantic: a new path in FindTypeOrInstantiate resolves the '@' marker,
looks up the enum type, reads Members.Count, and creates a standard
TStaticArrayTypeDesc with bounds 0..Count-1.
This completes the enum-indexed array story — const arrays already
supported this form; var/type declarations now match.
Tests: 4 unit tests + 2 E2E tests (both backends).
Grammar and language rationale updated.
Static-array bounds now accept named integer constants and compile-time
integer expressions, not only integer literals. All of these are valid:
const N = 10;
type TBuf = array[0..N-1] of Byte;
var A: array[0..N] of Integer;
const Days: array[0..N-1] of string = (...);
Parser: ReadConstBoundText collects tokens forming a bound expression
(integers, identifiers, arithmetic operators, parentheses) into a string
embedded in the type name.
Semantic: ResolveArrayBound resolves the bound text — plain integers via
StrToInt, named constants via symbol-table lookup, expressions via
mini-parse + EvalConstIntExpr. The canonical type name always uses
resolved integer values for cache consistency.
Both the var/type declaration path (FindTypeOrInstantiate) and the
const-array path (BuildConstArrayType / ReadConstArrayDim) are updated.
Tests: 5 unit tests + 3 E2E tests (both backends).
Grammar and language rationale updated.
Constant declarations now accept arithmetic expressions involving float
literals, named float constants, and the '/' operator. The semantic pass
detects float-containing expressions and folds them to a single Double
value at compile time, storing the result as a string — the same format
used for bare float literals.
The '/' operator always yields a float result even with integer operands,
matching Delphi/FPC semantics (e.g. const X = 10 / 4 produces 2.5).
Parser: extended ConstRhsStartsIntExpr to detect float-led and
slash-containing expressions; added tkSlash to IsConstExprOp.
Semantic: added IsFloatConstExpr (detects float leaves or '/' usage) and
EvalConstFloatExpr (folds via libc strtod/snprintf to avoid a known
self-hosting ABI issue with the Blaise StrToDouble built-in).
Tests: 8 IR unit tests + 4 E2E tests (both backends).
Grammar and language rationale updated.
Widen set base types from enum-only to also accept Byte (256-bit jumbo)
and Boolean (2-bit small). Set literals accept integer literals and
range syntax [lo..hi], expanded at compile time via EvalConstIntExpr.
Include/Exclude and the in operator accept numeric arguments for ordinal
base types. Both QBE and native backends handle TIntLiteral elements in
set mask computation.
15 new IR unit tests and 4 new E2E tests (both backends via
AssertRunsOnAll). All 3444 tests pass; FIXPOINT_OK,
NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK.
When a named type alias (e.g. TIntHolder = THolder<Integer>) appeared
in the type section before the out-of-line method implementations
(procedure THolder<T>.SetVal), InstantiateGenericRecord cloned the
template methods with Body=nil because LinkGenericClassMethodImpls had
not yet transferred the implementation bodies. Codegen skipped the
bodiless methods, producing undefined-reference link errors.
Add RepairEarlyGenericInstances which runs after LinkGenericClassMethodImpls
in all three analysis paths (AnalyseUnit, AnalyseUnitForExport,
AnalyseBlock). It iterates existing generic record and class instances,
finds methods still missing a body, clones from the now-populated
template, and analyses the repaired methods with correct type-param
scope bindings.
The statement parser's deref-then-field path (p^.field) unconditionally
expected ':=' after consuming the field name, failing when the field
is an array that needs subscripting. Now check for '[' before ':='
to handle p^.da[0] := value, matching the existing r.da[0] := value
path.
Functions returning a static array >8 bytes now use the sret ABI on
both QBE and native x86-64 backends: the caller passes the destination
buffer as a hidden first argument, the callee writes through it, and
returns void. Previously only the first 8 bytes survived (QBE
segfaulted, native rejected the function outright).
Key changes:
- QBE: IsRecordCall, signature, prologue, epilogue, and assignment
dispatch all extended to handle tyStaticArray alongside tyRecord
- Native: allowed return type guard, BuildFrame FSretFunc, sret
call-site dispatch, and subscript-assign Result indirection all
extended for tyStaticArray
A `const A: TArr = (10, 20, 30)` where TArr is a named alias for a
static array type failed to parse as an array constant. The parser
now detects the value-list shape via lookahead when CD.TypeName is set,
and the semantic pass resolves the named type to its underlying
TStaticArrayTypeDesc to extract bounds and element type.
The tyBoolean branch in EmitExpr intercepted boXor (and boAnd/boOr)
before the generic integer path could handle them. The else clause
in the comparison-operator switch defaulted to ceqw (compare-equal),
which is the exact opposite of xor: True xor True produced True
instead of False.
Added boAnd/boOr/boXor cases to the tyBoolean integer-comparison
switch so they emit the correct bitwise operations. The native
backend was already correct (no separate tyBoolean interception).
Fixes GitHub #123.
3411 tests pass. All three fixpoints verified.
Add GetItem/SetItem methods and an Items[Key: K] default property to
both TDictionary<K,V> and TOrderedDictionary<K,V>. This enables the
idiomatic d[key] syntax for reads and writes:
D['one'] := 1;
WriteLn(D['one']);
GetItem halts on missing key (consistent with index-out-of-bounds in
TList). SetItem delegates to Add (add-or-update semantics).
The compiler already had full default property infrastructure (parser,
AST, semantic, both codegens) — only the stdlib declarations were
missing. This is the first non-Integer-typed default property in the
codebase, exercising the generic index type path end-to-end.
Tests: 2 IR codegen tests (GetItem/SetItem emission) + 3 e2e tests
on both backends (integer keys, string keys, update-via-bracket).
3407 tests pass. All three fixpoints verified.
Three native backend bugs found and fixed by the IR-emit audit:
1. `sar` operator raised ENativeCodeGenError — added boSar -> sarq.
2. UInt64 comparison used signed setl/setg/setle/setge — max UInt64
compared as negative. Fixed: unsigned types and pointer-like types
now emit setb/seta/setbe/setae.
3. Mixed Single/Double float comparison in EmitCondBranch and
EmitExprToEax determined IsS (single-precision) by checking only
the left operand. A Single compared to a Double literal loaded
double-width data but compared with ucomiss. Fixed: require BOTH
operands to be Single for IsS, and add EmitXmm0WidthAdjust calls.
New e2e test files (all both-backend via AssertRunsOnAll):
- cp.test.e2e.external (10): FFI name binding, narrow-int return
masking, Single param/return, record-by-value, name alias
- cp.test.e2e.genericintfs (7): generic interfaces, two type params,
IEqualityComparer/IComparer, string type arg, polymorphic dispatch
- cp.test.e2e.gaps (15): packed records, sar/shr, UInt64 operations,
[Unretained] back-references, generic records, published RTTI
MethodAddress, multi-arg WriteLn
3402 tests pass. All three fixpoints verified.
Constructors are now auto-slotted into the vtable by the semantic pass.
When a constructor is called through a metaclass-typed variable
(C.Create(args)), the compiler emits _ClassCreate for allocation
followed by a vtable-indirect call to the most-derived constructor
body. Direct calls (TFoo.Create) remain fully static.
Both backends (QBE and native x86-64) emit correct dispatch for:
- MetaclassVar.Create(args) syntax
- ClassCreate(MetaclassVar, args) builtin
- Zero-arg and multi-arg constructor signatures
This is a layout-changing commit (adds constructor vtable slots).
Verified: FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK,
3370 tests pass.
The native x86-64 backend corrupted other arguments when a SCALAR FIELD of a
record-returning call result was read inline as a call argument — e.g.
Check('vals', 1, MakeR().A); // 'vals' lost on native; QBE was fine
Reading MakeR().A went through EmitExprToEax, which materialises the call's
sret buffer (a stack subq) and never frees it (the frame epilogue
reclaims it for a standalone expression). Inside the argument push/pop
sequence that stray allocation shifted %rsp, so an already-pushed argument
(typically the const-string) was reloaded from the wrong slot and lost —
crashing later in _StringAddRef when the callee retained the garbage pointer.
Fix: in the chained-field-read path, when the base is a record-returning call
and the field is scalar, allocate the sret buffer, read the field out of it,
then FREE the buffer so the expression nets zero stack change and the argument
protocol stays balanced. Record/static-array fields are excluded — they
return an address INTO the buffer, whose lifetime must outlive the read.
Regression test (both backends) in cp.test.e2e.recordret; the inline
HostTarget().OS assertion in cp.test.toolchain (previously worked around) now
exercises the fixed path directly.
FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite OK (3363).
--emit-ir prints QBE IR and --emit-asm prints native assembly; PickTopDriver
routes each output mode to the backend that produces it, ignoring --backend.
That silent override is acceptable for the default backend, but when the user
EXPLICITLY passes a --backend that cannot produce the requested output it was
silently honoured anyway — e.g. '--backend native --emit-ir' quietly emitted
QBE IR, ignoring the requested native backend.
Track whether --backend was given explicitly (TFrontEndOpts.BackendExplicit)
and, after backend selection, reject the incompatible combinations:
--backend native --emit-ir -> error (use --emit-asm)
--backend qbe --emit-asm -> error (use --emit-ir)
The default-backend cases are unchanged: --emit-ir with no --backend still
resolves to QBE, and --backend qbe --emit-ir / --backend native --emit-asm
remain valid. This matches the driver-owns-its-flags principle of the option
refactor — a backend-specific output mode belongs to its backend.
Tests in cp.test.cli cover both rejections and both still-valid paths.
FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK; full suite OK (3362).
The TCLIContractTests and TInternalAsmE2ETests suites resolve their
compiler binary from BLAISE_QBE_COMPILER, falling back to the
/tmp/fp_blaise{3,2} fixpoint binaries. Those only exist after a local
fixpoint.sh run, so in CI both suites silently Ignore'd ~24 tests while
the run still reported green (25 ignored in CI vs 1 locally).
Two changes:
- bootstrap.yml: set BLAISE_QBE_COMPILER to the bootstrapped -pre
binary so both suites actually run in CI.
- CompilerAvailable() in each suite now prints a one-time StdErr note
when it skips, so a missing toolchain is loud instead of a silent
green with hidden ignores.
Generalise external-tool lookup so a cross/host build finds the right
binaries:
- target: HostTarget detects the host OS from ParamStr(0) (a '.exe' suffix
means Windows, incl. under wine) instead of assuming linux; add
ExecutableExtension(target).
- uToolchain: TToolSpec (ordered candidate basenames, a cross-triple
prefix, and a HostTool flag) + ResolveSpec resolve a tool by env
override, then $BLAISE_TOOLCHAIN_PREFIX + name, then the cross prefix on
$PATH, then bare name — each probe appending the host exe extension.
HostTool distinguishes host tools (run anywhere, target via flags) from
target tools (must be the target's own binary; no host fallback when
cross). TrySingleName now uses the real host extension.
- driver: TBackendDriver.DescribeTools(target) declares a backend's tools
(base = the shared linker: cc/clang or the mingw cross-linker for a
Windows target); ToolPath resolves one by name; LinkViaToolchain links
through it. Self.DescribeTools for virtual dispatch.
Native sret codegen fixes (folded in so this commit bootstraps): the
toolchain code is the first in the compiler's own source to build a record
via a function whose body does SetLength on a dynamic-array Result field,
and to pass a record-returning method result directly as a const-record
argument. Both miscompiled on the native x86-64 backend (QBE was correct),
so a native-compiled compiler could not bootstrap at this commit:
- EmitLValueSlotAddr: an l-value field slot on a local record used a raw
leaq, but the sret-function Result slot holds a POINTER — route through
EmitLocalRecordBase so the pointer is loaded before adding the field
offset (fixes SetLength(Result.DynField, N)).
- EmitArgHoist: a record-returning method-call argument was materialised
via EmitExprToEax, which has no sret path for method calls — allocate the
buffer and drive EmitMethodSretCall directly, mirroring the function path.
Tests:
- cp.test.toolchain: ResolveSpec resolution order, host-tool vs target-tool
cross-suppression, fallback naming, and the host/extension helpers.
- cp.test.e2e.recordret: regressions for both native sret fixes (both
backends).
- cp.test.codegen: native backend also emits the _BlaiseInit startup call
(the existing assertion only covered QBE).
A third, pre-existing native sret bug (sret-call result + const-string arg
in the same call) surfaced while writing the toolchain tests; it is
independent of this work and logged in bugs.txt.
Backend-neutral feature; native fixes are x86-64-only.
A unit's initialization section only runs when that unit is compiled
into the program's own translation unit; units linked from a prebuilt
archive never get their _init called. blaise_weak relies on its
initialization section to set up the weak-reference table mutex, so for
archive builds that setup never happened. A zero-filled mutex object is
a valid unlocked mutex on some platforms, which kept this latent — but
that is not guaranteed everywhere.
Add a _BlaiseInit RTL entry point that performs the one-time setup
(currently the weak-table mutex, guarded to stay idempotent) and have
each backend's $main emit a call to it right after _SetArgs, before any
user code runs. The mutex is now initialised explicitly instead of by
relying on a zero-filled object happening to be valid.
Regression: cp.test.codegen TestMain_CallsBlaiseInit asserts the call.
Replace the FPC/Delphi replace surface (ReplaceStr, ReplaceText, and the
proposed StringReplace + TReplaceFlags set) with two self-explanatory
functions:
Replace(S, Old, New) — replaces the FIRST occurrence
ReplaceAll(S, Old, New) — replaces EVERY occurrence
Both are case-sensitive and literal (non-pattern). This sheds the legacy
redundancy: ReplaceStr/ReplaceText were two names for one operation split by
a 'String vs Text' distinction that is not obvious, and Delphi's StringReplace
encodes the same two booleans (all-vs-first, sensitive-vs-insensitive) as an
awkward flag-set. Modern languages (Go, Python, Java, Rust) use a plain
first/all split with no case flag.
Case-insensitive replace is expressed by lower-casing the inputs with the
built-in LowerCase/UpperCase before calling Replace/ReplaceAll; a
case-*preserving* insensitive replace and pattern/regex matching are deferred
and recorded in docs/future-improvements.adoc.
The internal worker keeps the existing TStringBuilder-based loop; it gains a
FirstOnly flag and drops the now-unused case-fold path.
No callers of the removed functions existed anywhere in the tree. IR/semantic
and e2e tests updated to the new names; both fixpoints green (FIXPOINT_OK,
NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK); full suite OK (3346 tests).
Integer div and mod by zero previously trapped in hardware (SIGFPE),
uncatchable even inside try/except — a single bad divisor terminated the
process. Both backends now emit a divisor-zero guard before each integer
idiv that raises a catchable EDivByZero exception via the normal exception
machinery, matching standard Pascal/Delphi semantics.
- SysUtils: add EDivByZero = class(Exception) and the _RaiseDivByZero helper
that raises EDivByZero('Division by zero').
- QBE + native x86-64: emit a divisor==0 check before integer div/rem
(32- and 64-bit); on zero, call SysUtils__RaiseDivByZero (which longjmps).
- The guard is emitted only when SysUtils is in scope (EDivByZero resolves
through the symbol table); without SysUtils, division traps as before.
This mirrors Delphi's placement of EDivByZero in System.SysUtils and keeps
the always-linked runtime free of stdlib class machinery.
- Tests: in-process e2e proves the guard does not disturb normal division on
both backends; shell-out cli tests (real compiler + linked stdlib) prove
catchable div/mod-by-zero on both backends.
- Rationale recorded in language-rationale.adoc.
All three fixpoints green (FIXPOINT_OK, NATIVE_FIXPOINT_OK,
NATIVE_INTERNAL_OK); full suite OK (3345 tests).
Relational comparison of strings was unhandled in both backends — only =
and <> were special-cased. The operands fell through to the integer
comparison path:
- QBE emitted a comparison that aborted QBE itself
(amd64/isel.c: selcmp: Assertion `k != Kw' failed).
- Native silently compared the string POINTERS, giving wrong results
(e.g. 'b' > 'a' returned False).
Both backends now route <, >, <=, >= through the existing _StringCompare
RTL helper (strcmp-like: <0 / 0 / >0) and compare its result against 0
with the matching signed condition.
Verified the full truth table ('abc' < 'abd', 'b' > 'a', <=/>= equal
cases, empty-string ordering) on both backends. Adds
TE2EStringOpsTests.TestRun_StringRelational_Order.
An implicit-Self method call in statement position (an unqualified
`Sink(...)` inside a method body) used a register-only fast path that
pushed every argument and popped it into SysVArgRegs64[I+1] — with no
check that the arguments fit the 6 System V integer registers. Self
takes %rdi, leaving 5 registers for arguments, so a call with 6+ value
args (Self + 6 = 7 slots) indexed SysVArgRegs64[6], one past the
[0..5] array. The out-of-bounds read returned an adjacent data-section
value (a garbage string pointer) that was later concatenated, crashing
_StringConcat — which is exactly how the native self-compile segfaulted.
(This was the "latent native self-compile crash" logged in bugs.txt. It
surfaced when a property-accessor helper shifted codegen enough to route
a 6+-slot implicit-Self call through this path; the workaround there
sidestepped it, but the bug was in the call-emission path itself.)
The implicit-Self statement-call path now guards the register fast path
with `CountArgSlots + 1 <= 6` and, when the slots overflow, spills the
surplus to the stack — mirroring the correct handling already present in
EmitMethodCallStmt for qualified method calls.
Also adds SysVArg64(), a bounds-checked accessor for the System V
integer-arg registers; every dynamic SysVArgRegs64[expr] read now goes
through it, so any future over-allocation raises a clear codegen error
("System V integer arg register index N out of range") instead of
silently reading past the array and miscompiling.
Verified: the pre-release segfaults native-compiling the minimal repro
(implicit-Self method, 6 args); the fix compiles and runs it correctly
(21) on both backends, and the full native self-compile succeeds. Adds
TE2ENativeTests.TestRun_Native_ImplicitSelfMethod_{Six,Seven}Args.
Both FNV-1a hash helpers carried over FPC's 1-based string indexing,
which is wrong in Blaise's 0-based world: each loop skipped the first
byte (index 0) and read one byte past the end (index Length).
- uDebugOPDF.FNV1a32 (32-bit; OPDF type IDs)
- uUnitInterfaceIO.ContentHashFnv1a64 (64-bit; .bif source hash)
Both now use 'for B in S' byte iteration, which is bounds-safe and is
also the natural iteration for FNV-1a (defined over the byte stream).
Output validated against a reference FNV-1a32 implementation.
No layout/ABI impact: OPDF type IDs are opaque internal tokens, and the
.bif SourceHash is a staleness cache key. Existing .bif caches will
mismatch once and trigger a one-time recompile, which uUnitLoader
already handles.
Two related set/enum fixes from shaking the sets cluster.
1. Set subset (<=) and superset (>=) operators, e.g. `if s <= t`:
- Semantic: <= and >= on two sets yield Boolean (alongside =, <>).
- Codegen both backends: non-jumbo sets test (s and not t)=0 for <=
(operands swapped for >=); jumbo sets call a new RTL _SetSubset
(added to blaise_set.pas) which returns whether every bit of A is in B.
Completes the set-operator suite (+, -, *, in, =, <>, <=, >= now all work).
2. Reject a variable that shadows a visible enum member (e.g. `var c` when
an enum member `C` is visible — Pascal is case-insensitive). Such a
shadow silently retargeted the member in a set literal `[A, C, D]` to the
variable: QBE errored with "Set literal element 'c' is not a constant",
and — worse — the native backend produced a WRONG bitmask with no error
(the member's bit was dropped). This surfaced as a corrupt for-in over a
set when the loop variable collided with a member. Now rejected at the
declaration with a clear message, mirroring the existing type-name-shadow
rule (FPC/Delphi allow the shadow; Blaise does not).
Verified subset/superset truth tables and for-in over a set (no shadow) on
both backends, and that the shadow is now a compile error. Adds
TE2ESetOpsTests.TestRun_Set_SubsetSuperset and
TSemanticTests.TestVarDecl_ShadowsEnumMember_RaisesError. Rationale's
"Variable Names May Not Shadow Type Names" section extended to enum members.
Two standard-Pascal ordinal features that were missing, found while
shaking the sets/enums/case cluster:
1. case-label ranges — `2..4:` as a case label:
case i of
0, 1: ...;
2..4: ...; // previously: Parse error "Expected ':' but got '..'"
else ...;
end;
The parser now accepts `lo..hi` per case label (reusing TSetRangeExpr to
represent the range, as set literals already do). Semantic checks both
bounds against the selector type; codegen matches a range with a
(sel >= lo) and (sel <= hi) test on both backends. Singles and ranges may
be mixed in one branch (`5, 7..9:`), and enum ranges work (`Red..Yellow`).
Ranges are rejected for a string selector.
2. Succ(x)/Pred(x) — the next/previous value of an ordinal (enum or integer
family); previously "Undeclared function 'Succ'". The result keeps the
argument's type, so Succ(enumValue) is still that enum. Lowered to +1 / -1
on the ordinal value on both backends.
Verified case ranges (int + enum, mixed with singles, with else) and
Succ/Pred (integer + enum, nested, type-preserved) on both backends. Adds
five dual-backend e2e tests in cp.test.e2e.controlflow.pas.
Completes generic methods: the out-of-line implementation form now parses
and links, alongside the inline-body form added previously.
type TUtil = class
function Pick<T>(cond: Boolean; a, b: T): T; // declaration
end;
function TUtil.Pick<T>(cond: Boolean; a, b: T): T; // out-of-line body
begin if cond then Result := a else Result := b end;
- Parser: a method-level <T> appearing AFTER the qualified name
(Owner.Method<T>) is now parsed into TypeParams. (The <T> before the dot
remains the owner's type params, as for TList<T>.Add.)
- Semantic: LinkClassMethodImpls detects a generic-method impl
(TypeParams <> nil) and transfers its body onto the in-class
generic-method template, instead of trying to resolve its T-typed params.
Also fixes a real hole found via the out-of-line probe: generic-method
call arguments were not type-checked against the parameters (the path
bypasses ResolveMethodOverload), so e.g. passing a string for a Boolean
parameter compiled silently. The call site now validates argument count
and types against the monomorphised signature.
Verified out-of-line impl (string + Integer instantiations) on both
backends, and that a mismatched argument is now rejected. Adds
TE2EGenericsTests.TestRun_GenericMethod_OutOfLineImpl. Grammar and
language-rationale updated to drop the inline-only caveat.
A class or record method may now declare its own type parameters,
independent of the enclosing type, and is monomorphised per call site:
type TUtil = class
function Pick<T>(cond: Boolean; a, b: T): T;
begin if cond then Result := a else Result := b end;
end;
...
u.Pick<Integer>(True, 7, 9); // -> 7
u.Pick<string>(False, 'a', 'b'); // -> 'b'
Each distinct set of explicit type arguments produces one concrete body
named <Owner>_<Method>_<Args> (e.g. TUtil_Pick_Integer); the implicit
Self is preserved, so a generic method can read the receiver's fields and
call its other methods. This is distinct from (and composes with) methods
of a generic CLASS.
Implementation mirrors the existing generic free-function machinery:
- Parser: a method-call folds an explicit <...> type-arg list into the
method name (Pick<Integer>), using the same two-token '<' lookahead as
generic free-function calls.
- Semantic: generic-method templates (TypeParams <> nil) are registered
by Owner.Method and skipped from signature/vtable/body analysis;
InstantiateGenericMethod clones the template, substitutes the type
params, keeps OwnerTypeName + Self, analyses via AnalyseMethodDecl, and
records a TGenericMethodInstance (deduplicated per owner+args). The
call site resolves obj.M<T>(...) to the instance.
- Codegen (both backends): GenericMethodInstances are emitted as ordinary
methods (Self param + mangled ResolvedQbeName).
Verified pick/echo, two distinct instantiations (Integer + string), use
of a Self field, and two type parameters on both backends. Adds IR tests
(TGenericFuncTests.TestCodegen_GenericMethod_{Body,Call}Emitted) and
dual-backend e2e tests (TE2EGenericsTests.TestRun_GenericMethod_*).
Grammar and language-rationale updated.
Limitation (logged in bugs.txt): only the inline-body declaration form is
supported; the out-of-line function TOwner.Method<T>(...) form is not
yet parsed.
TList<T> now exposes a `default` array property, so elements can be read
and written with the familiar subscript syntax instead of .Get(i):
L[0] := 100; // L.SetItem(0, 100)
WriteLn(L[0] + L[1]); // L.Get(i)
Adds TList<T>.SetItem (a pointer-target store, so the compiler's ARC
discipline releases the old element and retains the new for managed T)
and the property Items[AIndex]: T read Get write SetItem; default.
Also fixes the generic-instance clone path in the semantic analyser: it
copied every property-decl field except IsDefault, so a default property
declared on a generic template (TList<T>) lost its default flag on
instantiation and List[i] did not resolve. The clone now carries
IsDefault.
Verified List[i] read, write, and polymorphic element dispatch
(List[i].Area through a base-typed element) on both backends. Adds
TE2ETListTests.TestRun_TList_DefaultProperty_{ReadWrite,Polymorphic}.
Note: this makes the stdlib depend on the `default` directive (added in
the previous commit), so the pre-release bootstrap binary is refreshed to
a default-aware stage-2 in a follow-up.
Adds the `default` directive on an indexed property, enabling subscript
sugar on the object itself:
property Items[I: Integer]: T read Get write Put; default;
...
V[0] := 10; // lowers to V.Put(0, 10)
WriteLn(V[0]); // lowers to V.Get(0)
This is the mechanism behind the familiar List[i] syntax and was a real
foundational gap (the directive did not even parse — "Expected ':'").
Implementation:
- Parser: accept the trailing `default;` directive after a property
declaration; set TPropertyDecl.IsDefault.
- AST / symbol table: IsDefault on TPropertyDecl and TPropertyInfo;
TRecordTypeDesc.FindDefaultProperty walks the inheritance chain.
- Semantic: Obj[I] read (AnalyseStringSubscriptExpr) synthesises the
default property's field access and reuses the indexed-property read
path; Obj[I] := V write (AnalyseStaticSubscriptAssign) records the
setter on the TStaticSubscriptAssign node.
- Codegen (both backends): the write path emits the setter call via the
existing PropAccessorTarget / EmitPropAccessorCallNative helpers, so it
honours virtual/override on the accessor; the read path delegates the
TStringSubscriptExpr to its folded property-read field access.
- Cross-unit: IsDefault is serialised in the .bif interface so a default
property declared in one unit keeps its subscript sugar elsewhere.
Verified read, write, string-element, inherited, and cross-unit cases on
both backends. Adds IR tests (TPropertyTests.TestCodegen_DefaultProperty_
{Read,Write}) and dual-backend e2e tests (TE2EPropertyTests.TestRun_
DefaultProperty_{ReadWrite,StringElement,Inherited}). Grammar and
language-rationale updated; the two new TStaticSubscriptAssign fields are
marked safe in bif-coverage.status.
A generic class that implements a non-generic interface could not be
assigned to an interface variable:
IVal = interface function Get: Integer; end;
TBox<T> = class(IVal) ... function Get: Integer; ... end;
iv: IVal := TBox<Integer>.Create(77); // expected IVal, got TBox<Integer>
The first heritage entry of class(X) is parsed into ParentName whether X
is a class or an interface. The generic-instance clone path only moved
ParentName to ImplementsNames (so AddImplements would be called) when X
was a GENERIC interface — its name contained '<'. A plain interface name
like IVal failed that test, stayed in ParentName, and the instance's
implements list ended up empty, so class -> interface compatibility
rejected the assignment.
The move now fires for any ParentName that resolves to tyInterface,
generic or not, mirroring the non-generic class path. Verified a generic
class used through its interface, including a method with arguments, on
both backends. Adds TE2EGenericsTests.TestRun_GenericClassImplements
Interface and _MethodArgs.
A class implementing a derived interface could not be narrowed directly
to a base interface:
IDog = interface(IAnimal) ... end;
TDog = class(IDog) ... end;
a: IAnimal := TDog.Create; // link error: undefined itab_TDog_IAnimal
The class only emitted the itab for the interface it directly named
(itab_TDog_IDog), so assigning the instance to a base-interface variable
or parameter referenced an itab that was never generated. (The type
check itself was fixed in 37690de; this is the codegen half.)
EmitInterfaceDefs on both backends now expands each implemented interface
to its full parent chain, deduplicates, and emits an itab plus an
impllist entry for every ancestor. An ancestor's interface methods are a
prefix of the descendant's itab layout, so the same implementation
pointers apply — the base itab is just the leading slots.
Verified class-instance -> base-interface variable and parameter on both
backends. Adds TE2EInterfaceTests.TestRun_ClassImplDerived_To{BaseVar,
BaseParam}.
A value of a derived interface was not accepted where a base interface
was expected:
IAnimal = interface function Name: string; end;
IDog = interface(IAnimal) function Bark: string; end;
d: IDog; a: IAnimal;
a := d; // rejected: "expected IAnimal but got IDog"
Describe(d); // (a: IAnimal) -> "No matching overload"
CheckTypesMatch treated the two interfaces as unrelated. It now walks the
TInterfaceTypeDesc.Parent chain (new helper InterfaceInheritsFrom):
- interface -> base interface: assignable when the expected interface is
on the actual interface's parent chain (IDog is-a IAnimal).
- class -> interface: a class that implements a DESCENDANT of the expected
interface also satisfies it.
ArgMatchScore picks this up automatically through its CheckTypesMatch
fall-through, so parameter passing and overload resolution work too.
Verified derived->base assignment, derived-arg->base-param, and a
three-level interface chain (IC -> IA) on both backends. Adds
TE2EInterfaceTests.TestRun_{DerivedInterface_ToBaseVar,
DerivedInterface_ToBaseParam,ThreeLevelInterfaceChain}.
Note: assigning a class INSTANCE that implements a derived interface
directly to a base-interface variable still needs an itab for the base
that is not yet emitted (logged in bugs.txt); the interface->interface
copy path used by these tests does not need it.
A non-generic class could not inherit from a generic-class instance:
TBox<T> = class ... end;
TIntBox = class(TBox<Integer>) ... end; // rejected, then bad IR
Two bugs:
1. Semantic: AnalyseTypeDecls assumed any generic name in the first
heritage slot was an interface — it cast FindTypeOrInstantiate's
result to TInterfaceTypeDesc and moved it to the implements list, so a
generic CLASS parent was rejected as "Unknown interface 'TBox<Integer>'
in implements list". It now classifies the instantiated descriptor: a
tyInterface result is an implements entry, anything else stays as the
parent class for normal class-parent resolution.
2. QBE codegen: the inheriting class's typeinfo referenced its parent as
$typeinfo_TBox<Integer> via ClassSymName (which does not mangle <>),
but the generic instance's typeinfo is defined under the QBEMangle'd
symbol $typeinfo_TBox_Integer — producing invalid QBE IR ("invalid
character <"). The parent reference now uses QBEMangle for a generic
instance, matching its definition. The native backend already mangled
correctly and only needed the semantic fix.
Verified inherited method + field access and a virtual method declared on
the generic base and overridden in the derived class, on both backends.
Adds TE2EGenericsTests.TestRun_InheritFromGenericInstance_{MethodAndField,
VirtualOverride} and a note in docs/language-rationale.adoc.
Accessing a class const through the class type (TThing.MaxCount, not an
instance) was broken differently on each backend:
- Native: EmitExprToEax had no IsConstant case for TFieldAccessExpr, so
the access raised "unsupported expression form TFieldAccessExpr".
- QBE: the string class-const path emitted
`$_StringRetain(l $%tN)` — a non-existent RTL symbol, with the literal
temp wrongly double-prefixed by `$` (invalid IR). This path had never
worked; it was the only reference to the phantom _StringRetain.
Native now emits the inlined constant (movabsq for ordinals,
EmitStrLitAddr for strings), mirroring the IDENT-const path. QBE returns
the string literal's data-pointer temp directly, exactly like a plain
string literal (the consumer manages ARC). Accessing the same const via
an instance (t.MaxCount) already worked on both.
Adds TE2EClasses2Tests.TestRun_ClassConst_{IntViaType,StringViaType,
ViaInstanceAndType} (dual-backend); the string test also concatenates the
const to exercise ARC.
A derived class that declared an `overload` method with the same name as
an overload inherited from its base SHADOWED the inherited variants
instead of merging with them:
TBase = class
function F(x: Integer): string; overload;
end;
TDerived = class(TBase)
function F(s: string): string; overload;
end;
d.F('a'); // ok
d.F(5); // was rejected: "expected string but got Integer"
Two causes, both fixed:
1. ResolveMethodOverload stopped walking the inheritance chain as soon
as the first class declaring the name yielded any arity match, so the
base overloads were never considered. It now unions the overload
groups up the parent chain, stopping only when a level declares the
name WITHOUT `overload` — a non-overload method hides inherited ones
(the conventional redeclaration-hides rule), an `overload` method
extends the set (Delphi semantics).
2. The variable-receiver method-call path used FindMethodDecl (first
match in the chain) and never invoked overload resolution at all. It
now analyses the arguments and calls ResolveMethodOverload, falling
back to FindMethodDecl for the no-overload case. (Arguments must be
analysed before resolution so candidates can be scored against their
resolved types.)
Adds TE2EInheritTests.TestRun_OverloadMergeAcrossInheritance (dual
backend) and an "Overload sets merge across inheritance" subsection to
docs/language-rationale.adoc.
A property whose getter or setter is declared `virtual` did not dispatch
through the vtable: reading or writing the property through a base-typed
variable holding a derived instance called the BASE accessor, not the
override. A direct b.GetVal() call dispatched correctly, but b.Val (the
property over the same virtual getter) did not.
TBase = class
function GetVal: Integer; virtual; begin Result := 1; end;
property Val: Integer read GetVal;
end;
TDerived = class(TBase)
function GetVal: Integer; override; begin Result := 99; end;
end;
b: TBase := TDerived.Create;
WriteLn(b.Val); // was 1, now 99
The property read/write lowering always emitted a static call to the
accessor's declaring class. Now the semantic pass records the accessor's
vtable slot on the AST node (PropAccessorVSlot, -1 when the accessor is
not virtual), and codegen dispatches through the vtable when the slot is
>= 0, exactly as a direct method call does.
QBE: PropAccessorTarget computes the call target — emitting the vptr+slot
loads and returning the function-pointer temp for a virtual accessor, or
the static mangled symbol otherwise — and each call site emits its own
`call <target>(...)`. (A single emit-and-return helper was tried first
but tripped a latent native-backend miscompile on the self-compile; see
bugs.txt. The target-string shape avoids it.) Native:
EmitPropAccessorCallNative dispatches through the vtable or statically.
Covers getter and setter, on both backends. Adds
TE2EInheritTests.TestRun_VirtualProperty{Getter,Setter}_Dispatches
(dual-backend). The two new AST fields are marked `safe` in
bif-coverage.status (semantic-set, not serialised).
A child class that inherits a method-backed property failed to link:
Tbase = class
property classValue: Integer read getValue write setValue;
end;
Tchild = class(Tbase) ... end;
child.classValue := 12; // -> undefined reference to Tchild_setValue
writeln(child.classValue); // -> undefined reference to Tchild_getValue
Property accessors are mangled as <owner>_<method>. The read/write
lowering set PropOwnerType to the static variable's class (Tchild), but
the getter/setter symbols are only emitted for the class that declares
them (Tbase). The call referenced Tchild_getValue / Tchild_setValue,
which never exist, so linking failed on both backends.
Adds PropAccessorOwner, which walks the inheritance chain from the
variable's class upward and returns the class that actually declares the
accessor (falling back to the static type when none is found, leaving
the non-inherited case unchanged). All property read and write lowering
sites — direct, qualified-base, implicit-Self, and indexed-property-on-
field-type — now resolve PropOwnerType through it.
Regression test TestRun_InheritedProperty_AccessorsResolveToParent in
cp.test.e2e.classes2.pas compiles + runs the issue's program on both
backends and asserts stdout '12'.
Fix approach contributed via the issue thread by andrewd207; extended to
cover the indexed-property-on-field-type accessor sites as well.
cp.test.inherit.pas asserted only on generated QBE IR substrings and
never fed the IR to a backend, so virtual dispatch, inherited calls,
multi-level field layout, and destructor chaining were never proven to
run correctly — exactly the kind of gap where plausible-looking IR can
still produce wrong runtime behaviour.
Adds cp.test.e2e.inherit.pas with nine tests that compile + run on BOTH
the QBE and native x86-64 backends and assert on stdout:
- three-level virtual override reached through a base-typed variable
- inherited call inside an override
- constructor chain via inherited Create
- polymorphic dispatch through a base-typed array
- is / as operators with downcast
- virtual dispatch from inside a base constructor (template method)
- four-level field inheritance (offset accumulation)
- double dispatch (override -> inherited -> virtual)
- virtual destructor chain via inherited Destroy
All nine pass on both backends. No compiler source changed.
A chained subscript WRITE on a dynamic array of dynamic arrays —
m[i][j] := v — was rejected at semantic time with "Multi-dimensional
subscript base must be a static array". Only a static-array base was
accepted in the chained-assign path; reading via an intermediate row
variable (row := m[i]; row[j] := v) worked, but the direct write did
not, making jagged 2-D+ dynamic arrays awkward to fill.
Semantic: AnalyseStaticSubscriptAssign now accepts a tyDynArray base in
the BaseExpr (chained) path and derives the element type from
TDynArrayTypeDesc.
Codegen (both backends): when the chained-write base resolves to a
dynamic array, evaluate BaseExpr (m[i]) to the inner array's data
pointer and index into it — heap indirection per level — instead of the
inline-offset static-array path. QBE adds a BaseExpr branch in the
dynarray block of EmitStaticSubscriptAssign. Native stashes the base
pointer below the spilled value on the stack and reloads it at the
base-resolve step, with matching cleanup on every Exit path (float,
record, string/class, and scalar).
Verified 2-D int, 2-D string (with overwrite), and 3-D int nesting on
both backends. Adds three dual-backend e2e tests in
cp.test.e2e.dynarray.pas and a "Dynamic-array nesting (jagged arrays)"
subsection to docs/language-rationale.adoc.
A dynamic-array variable passed where an open-array parameter was
expected was rejected with "No matching overload", and even once the
semantic check accepted it the codegen emitted the wrong (data, high)
pair: it loaded a `<name>_high` slot that only exists for static-array
open-array temporaries.
Semantic pass: CheckTypesMatch and ArgMatchScore now treat tyDynArray
as assignment-compatible with a tyOpenArray parameter when the element
types match.
Codegen (both backends): when a tyDynArray argument is coerced to an
open-array parameter, pass the data pointer plus (runtime length - 1)
as the high index, where the length comes from _DynArrayLength at run
time rather than a compile-time static bound. QBE: OpenArrayArgFragment
gains a dynarray case (using distinct temps for the w-length and the
l-high computation to keep the IR well-typed). Native: the three
open-array argument emission paths (EmitMethodArgPush plus the two
call-site push/slot handlers) each gain a tyDynArray case.
Adds five dual-backend e2e tests in cp.test.e2e.openarray.pas covering
sum, High/Length, empty array, array-of-string, and pass-to-nested.
SetLength on an array element was rejected:
var m: array of array of Integer;
SetLength(m, 2); SetLength(m[0], 3); -> "First argument of 'SetLength'
must be an assignable variable"
This blocked ragged 2-D dynamic arrays (resizing each inner row). The
SetLength first-argument check accepted only a variable or field, not an
array-element l-value.
Semantic: the check now routes through the shared IsVarArgLValue helper, which
also accepts an array-element subscript (the type check already requires the
element to be a string or dynamic array). Codegen: QBE's EmitDynArraySetLength
already used EmitLValueAddr (extended for subscripts in the var-arg fix);
native's EmitLValueSlotAddr now handles a TStringSubscriptExpr element slot
(its address is what @m[i] computes).
Found by the e2e dynamic-array hardening sweep. New file
cp.test.e2e.dynarray.pas (8 tests, all run on BOTH backends): SetLength fill,
Length/High, grow-preserves, array-of-string, reference semantics, return by
value, ragged 2-D via SetLength(m[i], n), fill-inner-via-row.
(Two further dynarray gaps found and logged for follow-up: m[i][j] := v write
on dyn-of-dyn, and open-array param + dynarray arg compatibility.)
All three fixpoints + full suite (3283 tests) pass.
Adds four record e2e tests from the hardening sweep, all run on BOTH backends:
record method mutating Self, record-return-field used inline (Make(V).X),
record-with-static-array-field deep copy (y := x then y.A[i] must not alias x),
and a nested-record method call.
No compiler change — every record case probed (value-copy semantics, by-value
params, return-by-value, nested copy, string-field ARC, static-array-field
deep copy, methods) was already correct on both backends; this locks in the
proof.
Test-only; full suite 3275 tests green.
Adds five pointer e2e tests run on BOTH backends (AssertRunsOnAll), from the
hardening sweep's pointer probing: pointer-to-record-field write, pointer
parameter, pointer equality, pointer-to-array-element, and a GetMem-built
linked list traversal.
No compiler change — pointer codegen was already correct on both backends for
every case probed; this just locks in the proof. (Separately, New/Dispose are
not Blaise builtins — a language-design question logged in bugs.txt, not a bug.)
Test-only; full suite 3271 tests green. Compiler unchanged since 64160a0, so
the three fixpoints from that commit still hold.
Passing an array element to a var/out parameter was rejected:
procedure Swap(var A, B: Integer); ...
Swap(a[0], a[1]); -> "var argument 1 of 'Swap' must be a variable"
An array element is a legitimate addressable l-value (Swap(a[i], a[j]) is
idiomatic Pascal); only a plain variable, field access, or pointer deref were
accepted.
Semantic: the three general var-param l-value checks now route through a shared
IsVarArgLValue helper that also accepts a TStringSubscriptExpr over an array
base (static / dynamic / open array). String and PChar subscripts (a char by
value) are still rejected, as are literals and expressions. The stricter
Delete/SetLength first-argument checks are unchanged.
Codegen: EmitLValueAddr (QBE) and EmitVarArgAddrToRax (native) now handle a
TStringSubscriptExpr actual by reusing the existing element-address logic (via
a transient TAddrOfExpr), so the element's address is passed by reference.
Found by the e2e var/out parameter hardening sweep. New file
cp.test.e2e.varparams.pas (11 tests, all run on BOTH backends): var int/swap/
string, out params, var record, nested var call, var class field, and array
element (static/dynamic/string) as a var actual incl. Swap(a[0], a[1]).
All three fixpoints + full suite (3266 tests) pass.
Reading a static-array field of the current object via implicit Self inside a
method (FD[i], no explicit Self.) segfaulted on QBE:
type TC = class FD: array[0..4] of Integer;
function At(i: Integer): Integer; begin Result := FD[i] end; end;
The implicit-Self bare-field path in EmitExpr returned the field's storage
ADDRESS only for tyRecord; every other field kind was loaded as a value. A
static array lives inline in the object, so loading `FD` dereferenced the
array's first 8 bytes as a pointer; the subsequent FD[i] indexing then ran off
that garbage pointer. Native handled it; only QBE was broken.
Now the address is returned for any aggregate-address type (record, static
array, jumbo set) via IsAggregateAddrType, so `FD` yields &FD and FD[i]
indexes the inline array correctly. This also fixes indexed properties backed
by a static-array field (the common Items[I] pattern), which is how the bug
first surfaced.
Found by the e2e properties hardening sweep. New file
cp.test.e2e.properties.pas (11 tests, all run on BOTH backends): property
read/write, getter/setter, read-only, string, default value, inherited,
indexed; static-array field read/write/sum in a method; static-array-of-record
field.
All three fixpoints + full suite (3255 tests) pass.
Passing a class value to an interface-typed parameter (implicit
class->interface narrowing at the call site) produced a QBE link error,
referencing a nonexistent $Name_obj symbol:
function Describe(N: INamed): string; ...
Describe(t) // t: TThing implementing INamed
-> undefined reference to `t_obj'
The native backend handled it; only QBE was broken — the first
backend-divergence bug of the e2e hardening sweep.
EmitInterfaceExprPair's TIdentExpr branch assumed the source was
interface-typed (split obj/itab slots, addressed as Name_obj/Name_itab).
For a class-typed source there is just a single class pointer, so it emitted
a reference to the non-existent Name_obj. Now a class-typed expression
narrowed to an interface is handled directly: the value is the obj pointer,
paired with the static $itab_<Class>_<Interface> (mirroring the existing
class->interface assignment path). The target interface type is threaded
through InterfaceArgFragment/EmitInterfaceExprPair from the param type at the
call sites that lacked an inline class check.
Covers class var (global + local), constructor result, and procedure
interface params. Found by the e2e interfaces hardening sweep. New file
cp.test.e2e.interfaces.pas (8 tests, all run on BOTH backends).
All three fixpoints + full suite (3244 tests) pass.
In a generic method/function, a local variable whose name matched the type
parameter (case-insensitively) was wrongly rejected:
function R: T; var t: T; begin t := V; Result := t end;
-> "Duplicate identifier 't' — a type with this name is already visible"
During instantiation each type parameter is registered as a skType alias
(T = Integer) so the body resolves, and the shadow-a-type-name check (#102)
then saw the local `t` as shadowing the type `T`. But a type PARAMETER is a
placeholder, not a user-declared type the programmer is shadowing, so a local
sharing its name is legitimate.
Tracks the type-parameter names currently in scope (FActiveTypeParams, pushed
around instantiated class/record body analysis) and exempts them from the
shadow check. A genuine var-vs-type clash (var TFoo vs type TFoo) is still
rejected — verified.
Found by the e2e generics hardening sweep. Regression:
TE2EGenericsTests.TestRun_GenericClass_LocalNamedLikeTypeParam and
TestRun_GenericRecord_LocalNamedLikeTypeParam, both backends. The #102
shadow-type tests (TSemanticTests) still pass.
All three fixpoints + full suite (3236 tests) pass.
A generic free function with a local variable of the type parameter failed:
function Echo<T>(X: T): T; var tmp: T; begin tmp := X; Result := tmp end;
-> "Semantic error: Unknown type 'T'"
Signature substitution (params + return) was applied during instantiation, but
the cloned body's local var declarations still referenced the bare type
parameter, so AnalyseStandaloneDecl saw `var tmp: T` with T unresolved.
InstantiateGenericFunc now substitutes the type parameter in the cloned body's
TVarDecl.TypeName entries (via SubstTypeParam, which already handles T, ^T, and
nested SomeName<T>), so a local of type T (or ^T, or List<T>) resolves to the
concrete type. Generic class/record method bodies already substituted their
locals; this brings free functions in line.
Found by the e2e generics hardening sweep. New file cp.test.e2e.generics.pas
(10 tests, all run on BOTH backends): generic funcs incl. typed locals,
paramless+typed-local-return, two typed locals; generic classes/records;
method typed locals; two type params; distinct instantiations; nesting.
All three fixpoints + full suite (3234 tests) pass.
TList<TList<Integer>> and TBox<TPair<Integer, string>> failed to parse:
each type argument was read as a single bare identifier, with no recursion
into a nested <...>, so a nested '<' produced "Expected '>' but got '<'"
(type position) or "Expected '.' or '(' after generic type arguments"
(constructor/expression position).
Both type-argument parse sites now recurse through ParseTypeName, so a type
argument may itself be a generic specialisation, to arbitrary depth
(TList<TList<TList<Integer>>> works). The expression-position heuristic also
accepts tkLessThan as the lookahead-2 token (the first arg being itself
generic). The comparison-operator disambiguation (a < b, (a < b) and (b < c))
is unaffected.
Found by the e2e generics hardening sweep. Regression:
TE2EMiscTests.TestRun_NestedGenericTypeArgs (TBox<TBox<Integer>>, both
backends). docs/grammar.ebnf TypeArgList rule updated to recurse via
GenericName.
All three fixpoints + full suite (3224 tests) pass.
A statement beginning with '(' was rejected ("Expected statement"), so a
parenthesised cast could not be an assignment TARGET:
(a as TB).FX := 42; -> Parse error
Reading the same expression worked, and the hard-cast target TB(a).FX := 42
worked, so only this statement-parser entry point was missing.
ParseStmt now handles a leading '(' by parsing the parenthesised expression
and requiring a '.Field := Expr' suffix, building a TFieldAssignment whose
receiver is that expression (ObjExpr) — the same AST + semantic + codegen path
already used for element-field writes (a[i].F := v). No AST/semantic/codegen
change was needed; the gap was purely the parser.
Found by the e2e test-hardening sweep of the inheritance cluster. Regression:
TE2EMiscTests.TestRun_ParenCastAsAssignmentTarget, run on BOTH backends.
docs/grammar.ebnf FieldAssignment rule updated.
All three fixpoints + full suite (3223 tests) pass.
`inherited` previously parsed only as a statement, so calling an inherited
FUNCTION and using its result failed to parse:
Result := inherited Value() + 100; -> "Parse error: Expected expression"
This blocked the normal OOP pattern of an overriding function extending its
parent's result.
Adds the expression form alongside the existing statement form:
* uAST: new TInheritedCallExpr (sibling of TInheritedCallStmt) + CloneExpr case.
* uParser: tkInherited handled in ParseFactor (primary expression).
* uSemantic: AnalyseInheritedCallExpr resolves to the parent method (must be a
non-void function) and sets ResolvedType to the return type.
* Codegen: static (non-virtual) call to the parent slot, result returned as a
value. QBE EmitInheritedCallExpr; native EmitInheritedCallSeq (shared by the
statement and expression forms) leaves the result in %rax/%xmm0.
* uUnitInterfaceIO: 'inhc' encode/decode so inherited-expr in an inline method
body round-trips through the .bif unit-interface cache; bif-coverage.status
marks the two fields serialise (bif-coverage OK).
* docs/grammar.ebnf + docs/language-rationale.adoc updated (same commit).
Found by the e2e test-hardening sweep of the inheritance feature cluster.
Regression: TE2EMiscTests.TestRun_InheritedFunctionCall_InExpression covers a
value-returning inherited call and an inherited call with an argument, run on
BOTH backends via AssertRunsOnAll.
All three fixpoints (FIXPOINT_OK, NATIVE_FIXPOINT_OK, NATIVE_INTERNAL_OK) and
the full suite (3222 tests) pass.
Assembling the compiler's own object with --assembler internal needed ~53 GB
of peak memory and OOM-killed. Root cause: blaise.elfwriter built byte data by
string concatenation (X := X + ...), which reallocates and copies the whole
buffer on every append — O(n^2). The dominant cost was Finish() building the
.strtab and .symtab one element at a time across 32914 symbols; the section
bodies (~2 MB .text) and the final image assembly had the same shape.
Introduces TByteBuf, a capacity-doubling array-of-Byte buffer (amortized O(1)
append, bulk memcpy for byte<->string and buf<->buf), and routes the section
storage, the strtab/symtab/rela tables, and the final ELF image assembly
through it. TElfWriterSection.Data (string) becomes Bytes+Count (array of
Byte) — also the natural shape for the future internal linker, which needs
indexed byte access into section bodies.
Result: the internal assembler builds the whole compiler in ~5 s using
~740 MB peak RSS (was OOM at 54 GB) — a 73x memory reduction. Note: this is
allocation churn, not a leak; ARC was freeing the old buffers correctly (the
--debug leak tracker shows no new leaks), but the O(n) reallocation volume
blew the RSS high-water mark.
The internal assembler now self-hosts on both backends (the QBE-compiled path
relies on the @(class-field array)[idx] address fix in the previous commit).
Both fixpoints, the internal-assembler conformance guard, and the full suite
(3221 tests) pass.
TCodeGenQBE.EmitAddrOfExpr's @Rec.Arr[I] branch (TFieldAccessExpr with
IsArrayAccess) computed the receiver base for Base / IsVarParam / plain-local
records but was MISSING the IsClassAccess and IsImplicitSelf cases. For a
class instance variable it fell through to VarRef(RecordName) = $Obj, i.e. the
ADDRESS of the variable's slot, instead of loadl-ing $Obj to get the instance
pointer. So @Obj.Arr[I] emitted "add $Obj, off" and produced a garbage element
address that crashed when the pointer was actually used (e.g. passed to an
external memcpy).
Adds the IsClassAccess case (load the slot to get the instance pointer; load
twice for a var-param class) and the IsImplicitSelf case, mirroring the
canonical read-path receiver ladder. This is the QBE analogue of the native
EmitLocalRecordBase fix.
Regression: TE2EMiscTests.TestRun_AddrOfClassFieldDynArrayElem_LoadsInstance
writes through @B.Data[idx] via memcpy and reads it back; AssertRunsOnAll runs
it on BOTH backends.
Both fixpoints + the internal-assembler conformance guard + the full suite
(3221 tests) pass.
Adds the driver option contract from docs/backend-options-design.adoc
(Steps 2-5), so a backend owns parsing, describing, and validating its own
private flags and the common parser stays backend-agnostic.
Four virtual methods on TBackendDriver, with inert base defaults:
* AcceptOption — the parser offers an unrecognised flag to the active
driver (Chain of Responsibility); typed accept/reject result.
* DescribeOptions — the driver contributes its --help lines (via the new
shared FormatFlagLine helper, so column layout is owned in one place).
* ValidateOptions — post-parse, full-context validity rules (Template
Method seam).
* ClaimsEmitIR — retires PickTopDriver's hard-coded bkQBE for --emit-ir.
Parser integration is one-pass with a deferred drain: unknown flags are
collected during the loop and offered to the driver only after --backend
resolves it, so argument order never matters and the value-skip logic is
not duplicated across passes.
First real adopter on master: the native driver takes ownership of
--assembler internal|external. The inline --assembler arm is removed from
ParseArgs; the flag now flows through the drain into Native.AcceptOption.
Eng-review addenda folded in:
* ValidateOptions runs UNCONDITIONALLY, above the stdout-mode toolchain
skip (so flag-combination rules fire even with --emit-ir).
* A backend-private flag passed to the wrong backend is now a clear hard
error (intentional behaviour change: --backend qbe --assembler internal
was previously silently accepted-and-ignored).
* Pending entries carry {flag, lookahead} with consumed-index bookkeeping
so a flag's value token is not re-reported as unknown.
Tests: cp.test.driver.pas (TBackendDriverContractTests, 11 unit tests on
the real registered singletons) and cp.test.cli.pas (CLI E2E: internal
accepted, bogus rejected, wrong-backend rejected, bad value still rejected
under --emit-ir, --assembler listed in --help).
Both fixpoints, the internal-assembler conformance guard
(NATIVE_INTERNAL_OK), and the full suite (3220 tests) pass.
The native backend's field-access receiver ladder, when the record base is
a NAMED LOCAL record, emitted 'leaq <slot>' to get the record's address.
That is correct for an ordinary stack value record, but WRONG for the
Result of an sret (record-returning) function: there the frame slot holds
the caller's buffer POINTER, not the record itself, so the address must be
loaded with 'movq'. Reading Result.<field> on the RHS (e.g.
'Result.Imm := Result.Disp' in the internal assembler's ParseOperand) thus
dereferenced the wrong location and produced garbage.
The receiver ladder was duplicated across 12 local-leaf sites, each missing
the sret-Result case (the implicit-Result analogue of the implicit-Self
field-access symmetry rule). This centralises the local-record base
computation into one EmitLocalRecordBase(AName, AReg) helper that makes the
leaq-vs-movq decision once, and routes all 12 leaf sites through it.
The bug was latent: the wrong read happened to land on a harmless stack slot
under the prior register/stack allocation, so existing code worked by
accident. Any change that shifts global layout (e.g. growing a vtable by
one slot) re-allocated registers and exposed it as corrupted output from the
--assembler internal path. Neither fixpoint exercises the internal
assembler, so it surfaced only via TInternalAsmE2ETests once a layout change
armed it.
Both fixpoints (FIXPOINT_OK, NATIVE_FIXPOINT_OK) and the full suite
(3204 tests, incl. TInternalAsmE2ETests) pass.
ParseArgs changes from fifteen 'out' parameters to two caller-constructed,
populated objects:
* TFrontEndOpts (new unit blaise.frontend.opts) carries front-end-only
state no backend driver reads: SourceFile, OutputFile, SearchPaths,
EmitIfaceDir, Incremental, UnitCacheDir, DumpAST, SkipDepCodegen, plus
the output-mode policy flags EmitIR/EmitAsm and the Backend selection
input. It lives in its own unit (not blaise.codegen.driver, the
backend-facing abstraction) so front-end state stays out of the
backend opts bag.
* TBackendOpts (existing) carries only the cross-cutting knobs a driver
reads: Target, OPDFEnabled, DebugMode, UseInternalAsm (and OPDFAsmFile,
bound later during OPDF emit). EmitAsm is removed from TBackendOpts —
no driver method read it; it is a front-end selection flag.
The main body seeds its working locals from the two objects right after
the call, so the large downstream body reads them unchanged. The parser
now populates one pair of objects instead of fifteen by-reference returns.
Behaviour-preserving. Step 1 of docs/backend-options-design.adoc.
Both fixpoints (FIXPOINT_OK, NATIVE_FIXPOINT_OK) and the full suite
(3204 tests) pass.
PasBuild v1.9.0 ships the native TBlaiseBackend (driving Blaise via
--source/--output and probing the version via --help), which was the
agreed trigger to retire the FPC-style argument path.
Deletes ParseFPCArgs, IsFPCStyleInvocation, HandleFPCInfoQuery, the
main-body 'if IsFPCStyleInvocation()' branch, and the now-dead
uStrCompat import (~120 lines, all confined to Blaise.pas). This
leaves a single argument parser (ParseArgs) for the backend-options
refactor to build on, and eliminates the hand-maintained option-default
block in the FPC branch that was a recurring source of
uninitialised-option bugs.
Adds cp.test.cli.pas (TCLIContractTests) with a CLI smoke test:
--help works, a normal --source/--output compile works, and an -iV
probe no longer returns FPC's 3.2.2 (now a rejected unknown flag).
Step 0 of docs/backend-options-design.adoc.
Both fixpoints (FIXPOINT_OK, NATIVE_FIXPOINT_OK) and the full suite
(3204 tests) pass.
A set literal element may now be a constant range:
e := [Red..Blue]; { {Red, Green, Blue} }
e := [m0, m2..m4, m7]; { ranges mix with single members }
The parser builds a transient TSetRangeExpr for each lo..hi element; the
semantic pass (AnalyseSetLiteralExpr) expands it into the individual member
idents before any other consumer runs, so overload resolution, the bitmask
folder, the jumbo-set path, and both code generators see an ordinary member
list and need no range-specific logic.
Both bounds must be compile-time constants of the set's base type. A
reversed constant range [Blue..Red] is a compile-time error rather than a
silently empty set — matching Blaise's preference for rejecting
confusing-but-legal constructs over FPC's empty-set-with-warning behaviour.
Variable bounds [lo..hi] are rejected ("set range bound must be a
constant"); runtime-variable ranges are deferred.
TSetRangeExpr is wired into the .bif serialiser and the AST cloner so a
generic template body containing an unexpanded range round-trips through
separate compilation (verified by hand: a generic returning [m1..m3]
compiled to .o, then instantiated from the .bif with source hidden).
Set base types remain enumerations; the issue's set-of-byte example needs
ordinal-base set types, tracked separately in docs/future-improvements.adoc.
Range expansion is base-type-agnostic, so ranges will work for those
automatically once they land.
Docs: grammar.ebnf SetElement rule; language-rationale.adoc decision +
alternatives. bif-coverage.status regenerated for the new node.
FIXPOINT_OK + NATIVE_FIXPOINT_OK; full suite OK (3201 tests, 11 new).
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).
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).
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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).
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.
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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.
- 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).
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.
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).
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.
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
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.
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.
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.
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).
pdr now resolves the ASLR slide correctly (load base from the
binary's offset-0 mapping), so the -no-pie guard in both native link
paths is no longer needed. Debug binaries are position-independent
again, matching the platform default.
Verified under live ASLR: breakpoints, var-param drilldown, captured
vars, dynamic arrays, TList<T> inspection, callstack and stepping all
work against PIE binaries.
Remove the 'PIE (ASLR) support in PDR' section from
future-improvements.adoc — implemented.
OPDF/native debugging:
- uDebugFacts: TDbgVar.Indirect — the frame slot holds the value's
ADDRESS rather than the value (var/out params, captured outer locals).
- uDebugOPDF: emit LocationExpr=3 'RBP-relative indirect' for such
slots; mangle generic instance names in class/property records
(vtable_TList_Integer — raw 'TList<Integer>' broke the assembler);
set OPDF_FLAG_DYNARRAY_LEN32 in the header so debuggers read the
[refcount:Int32][length:Int32] dynamic-array header correctly.
- native: DbgMarkParams marks var/out params and _cap_ capture slots
indirect; captured vars are typed from the enclosing declaration
(OuterVarType via FDbgOuterDecl).
Verified with pdr: var-param record AND class field drilldown inside
the callee, captured-var inspection in nested procedures, dynamic
array printing and subscripting, TList<T> instance inspection.
Codegen bugs uncovered by the debug probes, fixed in both backends:
- var-param class field read/write used a single deref: the slot holds
the caller variable's address, so code treated that address as the
instance pointer — reads returned garbage and writes corrupted the
caller's frame. All field-access leaf sites now load twice when
IsClassAccess and IsVarParam (QBE: EmitInstancePtr/EmitLValueAddr/
EmitFieldAssignment/EmitExpr leaves; native: the parallel
RecordName-based receiver loads).
- QBE nested-proc signatures emitted a double comma when a proc has
captured vars AND regular params (l %_cap_X, , w %_par_K).
- native: assignments to captured string/dynarray/class/record/float
vars wrote to a global of the same name instead of through the
capture pointer; captured float reads did the same.
- native: 'var D: Double' params were classified as xmm args on both
sides of a call — the pointer arrived in an xmm register and the
callee stored through garbage. Var-param floats now travel in
integer registers like every other by-ref param.
New e2e tests: TestRun_VarParamClass_FieldReadWrite,
TestRun_NestedProc_CaptureAndParams, TestRun_VarParamFloat_CapturedFloat.
Three gaps between the OPDF data and a realistic multi-unit program:
- Type records for unit-defined classes referenced bare symbols
(vtable_TStringList, TStringList_SetText) while the emitted symbols
carry the owning-unit prefix — any program using TStringList failed
to LINK under --debug-opdf. New MangledClassSym mirrors the
codegen's ClassSymName allowlist (System / rtl.* / blaise_* /
program-scope stay bare) for VMTAddress and property accessor
addresses.
- Self was recorded untyped (its slot is allocated as a raw pointer),
so 'print Self.FField' failed. DbgMarkParams now types Self as the
owning class — pdr dereferences class-typed variables, so instance
field drilldown works. The unit-less driver path now passes the
program's symbol table to the backend (the analyser, which the
table's uses-chain provider points at, is still alive there; the
in-test fallback variant dangled and was reverted).
- Value record parameters present their '_data' shadow slot (the
callee's inline copy) as the parameter instead of the raw ABI
pointer slot — record fields are inline at known offsets, which is
exactly what field access needs.
Verified with pdr against a program using classes+sysutils units:
print P.X / P.Y (record param fields), Self.FCount / Self.FName
(instance fields, string rendering), Msg (local string) all correct.
Suite: 2955 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
The OPDF emitter was a standalone AST walk that GUESSED runtime facts:
local offsets ignored parameter slots and native slot sizing, every line
record pointed at the function entry, and HighPC was a next-function
approximation. The native backend owns all of those exactly — so it now
produces them.
New uDebugFacts unit: under --debug-opdf the native backend records, per
emitted function, the assembly symbol (LowPC), an end label after the
final ret (exact HighPC), every frame slot with its real %rbp offset
(parameters flagged with var/const, internal bookkeeping slots
filtered), and a .Ldbg_N local label per statement with its source
line/column. TOPDFEmitter gains a facts mode that emits scopes, params
(plus a locatable recLocalVar per parameter), locals and per-STATEMENT
recLineInfo from those facts; without facts (QBE backend — QBE assigns
frames and addresses itself) it keeps the approximate AST walk.
The driver appends the OPDF section to the SAME assembly file for the
native backend: local labels resolve within one object, so nothing needs
exporting. Debug links pass -no-pie (OPDF stores absolute addresses;
the pdr loader does not yet apply a PIE slide).
Verified end to end with the pdr reference debugger against a native
binary: 'break file.pas:line' hits mid-function, 'step' advances one
source line at a time, 'print <param>/<local>' reads correct values via
the real RBP offsets, and 'callstack' shows named frames with lines
(TThing_Bump at dbg.pas:11 <- main at dbg.pas:25). None of these worked
before. Normal builds are byte-identical (labels only under OPDF mode;
both fixpoints clean).
Tests: facts-mode emitter assertions (exact HighPC, real offsets,
statement-label line records) + native backend facts collection
(.Ldbg labels, param/local offsets, label-free normal builds).
Suite: 2954 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
Three OPDF emitter gaps from the debugger-feedback backlog:
- Class and record METHODS (destructors included) never got
recFunctionScope records: after semantic analysis the implementation
bodies live on the type declarations' method lists, while
EmitFunctionScopes only walked Block.ProcDecls (whose impl entries are
body-less matching stubs). The walk now gathers both sources; method
records carry the qualified label (TThing_Bump) as the record name —
the symbol a debugger resolves for break-by-function-name — and as
LowPC. Plain functions are unchanged (label = name).
- Under --debug-opdf every function symbol is now exported (ExportPrefix
and EmitFuncDef honour FOpdfMode): the .opdf companion is assembled as
a separate object whose scope records reference function labels, so
local symbols failed to link the moment a method got a scope record.
Same rationale as the existing VTableDataPrefix export.
- Generic-instance vtables bypassed VTableDataPrefix and stayed local
under --debug-opdf.
Verified end to end: a program with a virtual-method class compiles,
links and runs with --debug-opdf (previously the documented blocker).
Still open (bugs.txt): per-statement line records need codegen-emitted
statement labels — the line table remains function-start granularity.
Tests: TestOPDF_MethodScope_Emitted + TestOPDF_DestructorScope_Emitted.
Suite: 2950 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
obj.Field := MakeClass() leaked one reference per store: class-returning
functions and methods uniformly transfer +1 (the callee retains into
Result and never releases it — verified for constructor results, locals,
globals, fields and chained calls on both backends), but the class-field
store sites retained unconditionally on top of that transfer.
The ExprOwnsRef guard is now applied to every class-field store:
QBE EmitFieldAssignment, native TFieldAssignment (ObjExpr, sret-Result,
RecordName branches) and the native implicit-Self assignment — matching
the long-guarded string/dyn-array paths and QBE's implicit-Self path.
The historical blocker (a stage-2 compiler built with this guard
corrupted its own heap) was a TAIL of pre-existing over-releases that
the tighter refcounts exposed. Found and fixed with a temporary
negative-refcount abort in _ClassRelease:
- Typed exception handlers injected one synthetic local PER 'on E:'
clause: two same-named handlers double-allocated the slot and emitted
one epilogue release each, while only the matched body bound (+1) —
the exception's refcount went negative on scope exit. The synthetic
local is now deduplicated by name (uSemantic).
- Handler re-binding leaked the previous binding: the shared slot is now
released before each bind (both backends).
Verified shapes: ExprOwnsRef=True can only be user functions/methods/
getters/indirect calls — inlining excludes class returns and no external
returns a class, so every owning report is a genuine +1.
Compiler self-compile leak count drops 14 -> 6 objects. Tests:
TestDebug_ClassFieldFromCall_NoLeak + TestDebug_MultiHandlerVar_
NoOverRelease (leak-tracker asserted, both backends). Suite: 2948 OK on
working and fixpoint binaries (stage-2); FIXPOINT_OK; NATIVE_FIXPOINT_OK;
full native TestRunner OK.
The FTypeXxx caches (FTypeInteger, FTypeString, ...) held an extra
retained reference on top of FAllTypes' owning reference — NewType
returns +1 and the strong field store retained again, so every builtin
TTypeDesc ran one refcount high for the whole compilation.
With the [Unretained] store lowering in place (the owned +1 from the
NewType call is released at the assignment site), the caches are now
plain borrowed views into the type pool, which matches the ownership
story: FAllTypes owns the single reference, everything else borrows.
A previous attempt at this marking (pre-dating the Unretained
temp-release lowering) regressed leak counts; with the prerequisite in
place the leak report is unchanged (14 residual objects, none of them
type-pool related) and all gates pass on a stage-2 rebuild.
Suite: 2946 OK (stage-2 binary); FIXPOINT_OK; NATIVE_FIXPOINT_OK;
full natively-compiled TestRunner OK (2946).
Resolves the TStringList.Objects / TList<T> retention question from the
bug backlog as an explicit design decision (language-rationale,
'Collection Ownership'):
- TList<T>/TStack<T>/TQueue<T> managed elements ARE retained on store —
this already works (generic stores lower to ARC-aware pointer writes);
TE2ETListTests.TestRun_TList_ClassElements_RetainedAcrossScope now
pins it on both backends (object survives its creating scope).
- TStringList.Objects[] stays NON-OWNING by design: the API type is
Pointer and the integer-cast idiom (TObject(PtrUInt(N)), used in 27
places in the compiler itself) makes blind retention impossible.
Convention documented at the declaration and in the rationale.
- Known limitation recorded: generic Clear/Destroy do not yet release
remaining managed elements (needs a Default(T)-style zero-store);
tracked in the leak backlog.
Suite: 2946 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
Two native miscompiles, found chasing the generic-template in-memory
bif round-trip crash (TIfaceIOTests.TestRoundTrip_Generic*_TemplateParams):
1. .Free() on an l-value receiver expression (Def.ClassDef.Free()) only
released — the slot was not nilled (the QBE lowering stores 0). The
stale pointer aliased the next same-size allocation, so the following
ARC field store double-released the NEW object; its owned lists died
while still referenced (ReadGenericClassPayload's Free+Create+assign
dance left ClassDef.Fields freed → nil deref in the test). Field and
ident receivers now release AND nil via EmitLValueSlotAddr; other
expression receivers keep the release-only behaviour.
2. A record-returning call assigned into an array element
(t[i] := FLexer.Next()) evaluated the RHS via EmitExprToEax, which
cannot pass the hidden sret pointer — the callee wrote its Result
record through garbage (TLexerTests crashed; the suite had never run
natively because the round-trip crash aborted earlier). Both
subscript-assign branches now sret record-returning calls directly
into the element after releasing the old element's managed fields.
The FULL natively-compiled TestRunner passes for the first time:
2944 tests, 0 failures (previously aborted at TIfaceIOTests).
Tests: TNativeConstArgTests.TestFree_FieldReceiver_NilsSlot (asm-level)
+ TE2ERecordsTests.TestRun_RecordCallResult_IntoArrayElement (both
backends). Suite: 2945 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
H.S.Note(); failed semantic analysis with "Class 'IShape' has no method
'Note'" — AnalyseMethodCall's receiver-expression path accepted
interface-typed receivers in its kind check but then resolved the method
against TRecordTypeDesc unconditionally. Interface receivers now get
their own branch (mirror of the implicit-Self interface path): validate
against the interface's method list and mark the call for itab dispatch.
The QBE backend already lowered the shape; the native EmitMethodCallStmt
site additionally failed to pass ObjExpr to EmitInterfaceCall, emitting
bogus _obj/_itab operands from the empty ObjectName — it now forwards
the receiver expression like EmitMethodCallExpr does.
Test: TE2EClasses2Tests.TestRun_InterfaceFieldCall_StatementPosition
(both backends). Suite: 2943 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
Open-array arguments were lowered correctly only at standalone
procedure/function call sites. Method calls (statement and expression
paths, static and virtual) and constructor calls emitted just the data
pointer — the callee read its (data, high) pair shifted, so High(A) and
the elements were garbage (F.Sum([10, 20, 30]) printed junk; the
statement form printed a wrong count).
New OpenArrayArgFragment helper emits the 'l data, l high' pair for
inline array literals, static-array coercions, and open-array parameter
forwarding; the four method/constructor argument loops now use it.
The native backend already handled all these shapes.
Test: TE2EOpenArrayTests.TestRun_OpenArray_MethodAndCtorParams —
constructor, function and procedure (statement) open-array params on a
class, both backends. Suite: 2942 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
A function-of-object variable called in EXPRESSION position went through
EmitCallIndirect, which loads only the Code pointer and passes the first
user argument in %rdi — Data (Self) from offset +8 of the TMethod block
was never loaded and the argument registers were not shifted. The
STATEMENT path already dispatched on IsMethodPtr to EmitMethodPtrCall;
the expression path now does the same.
Repro: any 'function(N: Integer): Integer of object' variable call in an
expression — QBE was correct, native segfaulted.
Test: TE2EMiscTests.TestRun_FunctionOfObject_IndirectCall (both
backends). Suite: 2941 OK; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
Interfaces may declare properties (FPC/Delphi parity):
IValued = interface
function GetValue(): Integer;
procedure SetValue(AValue: Integer);
property Value: Integer read GetValue write SetValue;
end;
Accessors must be methods of the interface or an inherited parent
(interfaces have no fields), validated at registration. I.Value reads
lower to the existing zero-arg getter itab dispatch; I.Value := X
lowers to the setter dispatch with X as the single argument — pure
compile-time sugar, no itab slots, no layout change. Child interfaces
see inherited properties via the parent chain. Wired end to end:
parser, TInterfaceTypeDef.Properties (AST + clone), TInterfaceTypeDesc
property registry, semantic read/write resolution, both backends, .bif
serialisation and import registration. v1 limits (recorded in
language-rationale): plain interface-typed receivers; no indexed/
default array properties.
Two pre-existing linking bugs surfaced by the dogfood program and are
fixed alongside:
- Property accessor names written in a different case than the method
declaration (read getValue for GetValue) produced unresolved symbols —
accessor names are now normalised to the declared casing at
registration (classes and interfaces, compile and import paths).
- A program-level class implementing an interface failed to link
whenever the program had a uses clause: program-scope methods carry
bare symbol names (uSemantic.CurrentUnitPrefix) but itabs and
property-setter call sites prefixed them with the program name via
Sym.OwningUnit. ClassUnitPrefix (QBE) / ClassSymName (native) now
skip the prefix for program-owned classes (new FProgramName field).
Tests: 7 in cp.test.interfaces (parse, registration, accessor
validation, read-only enforcement, inheritance, IR dispatch), bif
round-trip in cp.test.unitinterface, 2 e2e suites on both backends in
cp.test.e2e.classes2 (interface read/write incl. compound assignment
and inherited dispatch; case-mismatch + uses regression). Suite: 2940
OK on working and fixpoint binaries; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
Sin(12) and Tanh(I) now compile: the float compiler builtins (Sqrt,
Ceil/Floor/Round/Trunc, Ln/Log2/Log10, Power, the trig family, IsNaN/
IsInfinite) accept integer-family arguments and widen them to Double —
matching FPC/Delphi and Blaise's own implicit int→float assignment
rule. The trig builtins return Double for integer arguments (still
Single→Single / Double→Double for float arguments). Power previously
type-checked nothing and emitted invalid IR for integer (and Single)
arguments; it now validates and coerces both to double.
Double(I) / Single(I) typecasts were lowered as bit copies: QBE emitted
an integer temp into 'stored' (rejected by qbe), native stored an
integer register — so the Tanh(Single(I)) workaround failed too. Float
casts now emit real conversions on both backends (swtof/sltof/ultof/
uwtof + exts/truncd on QBE; cvtsi2sd/ss + cvtss2sd/cvtsd2ss natively),
including float→float width changes.
Fixing this exposed two pre-existing native float-width bugs: a Single
RHS assigned to a Double variable was stored without cvtss2sd, and
mixed-width binary operands (s * 1000 — integer literals are emitted as
.double constants) ran the Single binary path at the wrong width.
Added EmitXmm0WidthAdjust and applied it at assignment and to both
binary operands.
Decision recorded in docs/language-rationale.adoc (Implicit
Integer→Float Widening). Tests: 8 IR/semantic tests in cp.test.math
(the two RejectInteger tests inverted to acceptance) + 2 e2e tests on
both backends in cp.test.e2e.math. Suite: 2930 OK on working and
fixpoint binaries; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
Array-typed FIELDS were second-class citizens for element access; this
lands the full variation family on both backends:
- Semantic: r.A[i] := v dropped the subscript from the LHS type — the
parser stores it in TFieldAssignment.PropIndexExpr (the indexed-
property slot) and semantic ignored it for real fields, demanding the
whole array type on the RHS. A subscript on a real dyn/static-array
field is now an ELEMENT write (new semantic-set IsElemWrite flag);
both backends emit the element store with the standard ARC and
record-copy rules.
- Parser: c.N.A[i] := v failed with "Expected 'end' but got '['" —
the chained L-value walker now accepts a terminating Field[idx] :=,
and the subscript-chain path accepts arr[i].A[j] := v (subscript
directly over another subscript stays a clear parse error).
- Bare implicit-Self: A[i] := v inside a method raised "Undeclared
variable 'A'" — TStaticSubscriptAssign now resolves array-typed
fields of Self (IsImplicitSelf + ImplicitFieldInfo).
- SetLength(r.A, n): QBE refused ("first argument must be a
variable"); native silently emitted NO code for field receivers and
mis-stored through var-param receivers. QBE routes through
EmitLValueAddr; native gains EmitLValueSlotAddr covering field,
var-param and implicit-Self receivers for dyn-array and string
SetLength.
- Read side: c.A[i] through a class variable computed the element base
as if c were an inline record (missing object-pointer load) and
segfaulted; implicit-Self bases had the same gap; native missed
chained reads (c.N.A[i]) entirely. All base shapes are handled in
the IsArrayAccess read paths of both backends now.
bif-coverage.status regenerated for the new semantic-set AST fields
(safe). Tests: 4 IR tests (cp.test.dynarray) + 6 e2e tests on both
backends (cp.test.e2e.records) covering record/class/implicit-Self
receivers, nested chains, static-array fields and string-element ARC.
Suite: 2922 OK on working and fixpoint binaries; FIXPOINT_OK (single
round); NATIVE_FIXPOINT_OK.
Dynamic arrays of records were broken in three interlocking ways:
- Parser: a[i].Field := v (and a[i].Method, chained a[i].F.G := v) on
the statement LHS raised "Expected ':=' but got '.'" — the subscript
statement branch only accepted ':=' directly after ']'. It now
builds a subscript-rooted postfix chain ending in a TFieldAssignment
(via ObjExpr) or TMethodCallStmt (via ObjExpr). grammar.ebnf gains
SubscriptFieldAssign / SubscriptMethodCall rules.
- Both backends: a[i] := r stored the ADDRESS of r into the element
instead of copying the record, and element reads loaded the first
8 bytes of the element as if it were a pointer. The two bugs masked
each other (elements aliased r — the TElfSection workaround comment
in uElfObject.pas documents the symptom). Record-element subscript
reads now yield the element address (dyn/open/static arrays) and
writes do an ARC-aware fieldwise copy: EmitRecordCopy on QBE,
retain-src/release-dest/memcpy on native. Native static arrays of
records had the same read/write bug and are fixed too.
- QBE backend: Exit inside the SECOND (or later) try block of a
function skipped _PopExcFrame, leaving a stale g_exc_top that
corrupted later raises/pops. EmitTryFinallyStmt/EmitTryExceptStmt
emitted normal and exception paths sequentially but decremented the
codegen-time FExcDepth on both (net -1 per try statement). Ported
the native backend's rebalancing (restore depth before emitting the
exception path). This is the likely root cause of the historical
'avoid bare Exit inside try' convention.
Tests: 3 IR tests (cp.test.dynarray), 4 e2e tests on both backends
(cp.test.e2e.records), IR pop-count + e2e regression for the exc-frame
bug (cp.test.exceptions, cp.test.e2e.exceptions). Suite: 2912 OK on
working and fixpoint binaries; FIXPOINT_OK; NATIVE_FIXPOINT_OK.
Port the QBE backend's shape-aware const-string argument convention to
the native x86-64 backend: const params of any kind no longer get the
callee entry-retain/exit-release pair; the caller protects string
arguments by shape (borrowed / consume / pin — see
blaise.codegen.arcshapes.TConstArgMode). Both backends now share one
convention, which also makes native calls into the QBE-compiled RTL
consistent for const params.
The pin slots live in a unified pre-call hoist region (EmitArgHoist)
shared with open-array literal blocks and — new — the sret buffers of
record-returning call arguments. Every call path emits the same
region + post-call epilogue (string releases, record-temp field
releases, reclaim) with the call's return registers preserved:
direct calls, method calls (both strategies), constructor calls,
interface dispatch (var flags from TInterfaceTypeDesc mark positions
to skip; shape protection is applied to every string-typed value arg
since const-ness is invisible there), proc-pointer and method-pointer
calls (TProcParamInfo const flags), inherited calls, and the three
sret call paths (dest pointer saved below the region so %rsp-relative
dest operands cannot drift).
The address-taken walkers move to the shared unit
blaise.codegen.arcshapes (QBE IR output is byte-identical); the native
backend rebuilds its borrowed-argument blocklist per function from
them, mirroring the QBE backend's FConstArgUnsafe.
Bugs found by the new tests and the natively-compiled TestRunner,
fixed here (native backend, all pre-existing):
- record-returning calls as arguments interleaved their sret buffer
between pushed argument slots (garbage args); the hoist fixes this
and EmitSretArgReleases is gone
- callee prologue copied record/static-array value params via memcpy
before spilling later argument registers (memcpy clobbered them);
spills now run in two phases
- >6-slot method calls forwarded var/out params by slot address
instead of the held pointer (no ParamMode check)
- TStringLiteral ignored IsCharCoerce: S[I] = ',' compared the
literal's data pointer against the byte
- EmitNarrowToType truncated pointer-shaped values (class casts) to
32 bits
- IntByteSize defaulted nil/unmapped kinds to 4 bytes (QBE uses 'l');
forward-class fields loaded half a pointer
- EmitCallIndirect/EmitMethodPtrCall loaded the target into %r10/%r11
before argument evaluation (caller-saved clobber window)
- EmitCall's push/pop loop used a stale ParamType from the counting
loop
Tests: cp.test.nativeconstarg (12 asm-level shape tests incl. the
callee-pair flip) and cp.test.e2e.constarg (21 runtime parity tests on
both backends). Full suite 2903 OK on the working and fixpoint
binaries; FIXPOINT_OK; NATIVE_FIXPOINT_OK; natively-compiled
TestRunner green except two pre-existing generic-template round-trip
failures (bugs.txt).
With the default-argument statement bug fixed, the borrowed-local
elision blocked in 91f44ec is sound and enabled: a const-string argument
that is a plain local or by-value/const parameter variable emits no
caller pin at all — the frame's own reference outlives the call.
Exclusions guard the aliasing edges: address-taken locals (explicit @,
passed to var/out params), locals captured by nested procedures (both
via a per-function blocklist rebuilt in EmitVarAllocs), and calls whose
signature has a var/out string param (F(L, L) lets the callee release
L's buffer through the alias). Nine IR contract tests cover every
shape, including the new exclusion guards.
Tail wins on the remaining profile:
- QBEMangle: fast path returns the input unchanged when it contains no
mangled characters (1.3M calls were rebuilding clean names one concat
per character).
- TStringList.FindSorted: compares the probe slot in place through the
CompareStr/CompareText builtins instead of copying it into a local
and dispatching through Compare — several ARC ops per binary-search
step on the hottest lookup path.
- _StringFormatN: the sizing pass uses a pure DecimalWidth count
instead of rendering every %d argument twice.
Compiler self-compile: 0.78 s -> 0.60 s wall, 8.60G -> 6.45G
instructions. Verified through two self-hosted generations, gen-2
native emission, both fixpoints and the full suite (2870 tests).
AnalyseMethodCall (TMethodCallStmt) never called AppendDefaultArgs, so a
method-call statement that omitted defaulted parameters was emitted with
fewer arguments than the callee's parameter list — the callee then read
whatever the missing slots happened to contain. For parameters past the
six SysV integer registers that meant caller-frame stack junk: the
native backend's own EmitInterfaceCall (defaulted 7th param AObjExpr)
read garbage instead of nil, and only survived because the old register
allocation upstream happened to leave benign values there. Expression
calls, proc-call statements and implicit-Self calls already appended
defaults; the three resolution sites in AnalyseMethodCall now do too
(AppendDefaultArgs is idempotent).
Pinned by cp.test.defaultargs.pas: explicit-receiver statement,
seventh-slot default, implicit-Self statement, and proc statement.
Const-string args were pinned (AddRef before the call, Release after)
regardless of shape. ConstArgMode now classifies them: string literals
and named consts are immortal and emit no ARC ops at all; +1 owned
temps (function/method/getter returns) emit a single post-call Release
that consumes the transferred reference — previously these leaked one
buffer per call (the pin pair netted to zero and EmitOwnedArgReleases
deliberately skipped const-string params); every other shape keeps the
pin. The hot TStringList lookup signatures (Compare, KeyEquals,
HashOf, Find, FindSorted, IndexOf) become const.
Borrowed elision for plain local variables — the larger win — is
deliberately NOT enabled: it is sound at the ARC level, but removing
the balanced pin pairs changes caller register allocation and exposes a
latent use-before-def of a callee-saved register in the native
backend's EmitInterfaceCall (a second-generation compiler then reads
the caller's leftover %r13 as an AST pointer and crashes). The full
repro and bisect trail are recorded in the bug log; the camPin branch
carries a pointer to it.
Verified through two self-hosted generations plus both fixpoints;
contract pinned by 7 IR tests in cp.test.constarg.pas.
The generic keyed containers used documented linear key scans. They now
build the same lazy open-addressing index as TStringList: threshold 16,
kept at <= 50% load, maintained on Add, invalidated by Remove/Clear/
Destroy (storage stays insertion-ordered, so TOrderedDictionary indexed
access is unchanged).
Hashing an arbitrary key type works through monomorphisation: the
generic bodies call GCHashOf(Key), which resolves per instantiation
against new overloads (string hashes its bytes FNV-1a, matching the
content semantics of '=' on strings; Integer/Int64/Pointer use a
multiplicative mix). Instantiating a keyed container with a type that
has no GCHashOf overload is a compile-time error; the threshold const
lives in the interface because generic bodies are analysed at their
instantiation site.
Neutral on the compiler self-compile (its dictionaries are small); this
is stdlib quality for user programs. Contract pinned by 7 new
in-process tests (cp.test.maphash.pas) written against the linear
implementation first.
Every standalone call resolved overloads by scanning the whole
FProcIndex with SameText (twice: IndexOf + ResolveStandaloneOverload),
and every method call scanned FMethodIndex once per inheritance hop
(2.35% of the compile after the TStringList hash landed). FProcGroups/
FMethodGroups now map each key to a TObjectList holding every decl
registered under it, so resolution, the registration-time duplicate
check and impl-signature matching fetch all candidates with one hashed
IndexOf. FProcIndex.Objects[] rewrites go through
ReplaceProcIndexObject so the groups see iface->impl swaps.
The groups are additionally held in an owning TObjectList keep-alive:
TStringList.Objects stores raw, non-retained pointers, so a group whose
only strong reference was a local would be released at the creating
routine's exit and its memory recycled (manifested as a crash resolving
Exception.Create during self-compile). TObjectList itself retains on
Add and releases on Put/Clear/Destroy. The non-retaining behaviour of
TStringList.Objects and generic TList<T> class elements is recorded in
the bug log.
Compiler self-compile: 0.91 s -> 0.80 s, 10.5G -> 9.2G instructions.
Profiling the compiler self-compile (--emit-ir, callgrind) showed 86% of
all instructions inside linear scans of five unsorted TStringLists
(FUnitSymbols 68%, FMethodIndex 12%, FStrLits 4%, FUnitIfaces 2%);
TStringList.Find averaged 145 Compare calls per lookup, and each
iteration paid six ARC ops through Compare's by-value string params.
Unsorted lists with >= 16 entries now build a lazy open-addressing hash
index on first Find: FNV-1a over the bytes, case-folded when the list is
case-insensitive, kept at <= 50% load so an empty slot terminates every
probe. Duplicate keys keep the first-added index, preserving the
IndexOf contract that overload registration relies on. Appends update
the table in place; Delete/Insert/Put/Clear/CustomSort and the
Sorted/CaseSensitive setters invalidate it for lazy rebuild. Sorted
lists keep using binary search; small lists keep the linear scan.
Compiler self-compile: 4.75 s -> 0.91 s wall (5.2x), 71.6G -> 10.5G
instructions (6.8x). Behaviour contract pinned by 12 new in-process
tests in cp.test.stringlistfind.pas, written against the old linear
implementation first.
Five native-backend fixes found while bringing up the natively-compiled
TestRunner:
- Bare class-name expressions (IsMetaclassRef) now emit the typeinfo
address (leaq typeinfo_<T>(%rip)), matching the QBE backend's
'copy $typeinfo_<T>'. Previously they fell through to a global
variable load of an undefined symbol, breaking RegisterTest(TFoo).
Also: tyMetaClass and tySet accepted in the function param/return
type guards, and ForceDirectories added to the TProcCall handler.
- ClassCreate: the constructor symbol now goes through
MethodEmitNameNative (unit-prefixed mangling) instead of raw
OwnerTypeName_Create, and the _ClassCreate result is kept in %rbx
across the constructor call — constructors are plain procedures and
do not return Self in %rax.
- Class-name string blobs (__cn_*) now carry the bare class name as
content while keeping the unit-prefixed symbol name. Previously the
prefixed name was embedded, so ClassName/TestClassName returned
'unit_TClass' and the runner's --suite filters never matched.
- Const string params now get the callee-side entry-retain/exit-release
pair. _StringConcat returns an rc=0 transient; QBE pins it caller
side (EnsureConstStringRef), the native backend had no protection, so
any retaining callee further down the chain freed the transient while
still in use — heap corruption that crashed the semantic pass on
'class of' expressions.
- Static-array locals are memset to zero on entry, mirroring QBE's
EmitVarAllocs. Element assignment into arrays of managed types
releases the old element; without the memset that release read stack
garbage (TProcess slots in RunFilteredTests).
- Open-array literal arguments are hoisted before the argument pushes
(HoistOALitArgs + BeginCallArgs/PushCallArg/EndCallArgs). The old
in-loop EmitOpenArrayLiteral subq'd its element block in the middle
of the push sequence, so the popq loop that loads the argument
registers popped from inside the block — Outer('gcc', ['-o', B, A])
received AExe='-o'. FMethodArgExtraStack is gone (its only writer
was the in-loop literal emission, and it was reset by nested call
sites mid-argument-evaluation).
Three new e2e tests cover metaclass values on both backends (including
the multi-unit RegisterTest pattern) and an open-array literal that is
not the first argument.
The native backend rejected MethodAddress calls where the second
argument was not a string literal. Evaluate the expression normally
and pass the result (already a data pointer) to _MethodAddress, matching
the QBE backend.
Also convert remaining old-style single-quote concatenation source
constants in e2e tests to triple-quote text blocks for readability.
EmitExprToEax uses %rcx as a scratch register for binary expressions,
but the open-array, dynamic-array, static-array, and PChar element
access paths all saved the base address in %rcx before calling
EmitExprToEax for the index expression. A computed index like [I + 1]
would clobber %rcx, causing the element address computation to
dereference a garbage pointer.
Use pushq/popq to protect the base address across the EmitExprToEax
call, matching the pattern already used by the string-subscript path.
Same fix applied to the @Array[I] address-of paths.
This was the last blocker for the native fixpoint — the native-compiled
compiler can now fully recompile itself with --backend native and
produce identical assembly at stage-2 and stage-3.
The native backend's EmitWrite hardcoded fd=1 for all _SysWrite* calls,
ignoring the StdErr file argument. WriteLn(StdErr, 'msg') emitted the
integer 2 as a literal followed by the message on stdout instead of
routing to file descriptor 2.
Mirror the QBE backend's StdErr detection: if the first argument is the
StdErr identifier, set fd=2 and skip it in the argument loop.
Three related bugs in the native x86-64 backend where record data was
written to the pointer slot (leaq = address-of-slot) rather than through
the pointer (movq = load-pointer-then-deref):
1. EmitSretCall / EmitMethodSretCall: when the LHS of a record-returning
function call is a var/out parameter, pass ASretIsIndirect=True so the
memset and reg-return capture target the caller's buffer, not the
local stack slot holding the pointer.
2. EmitRecordRegReturnCapture: honour a new AIsIndirect flag — emit
movq (load pointer) instead of leaq (address-of-slot) for all
classification shapes (rcInt1..rcSSEInt).
3. EmitExprToEax for TIdentExpr with tyRecord/tyStaticArray: when the
identifier is a const/value record parameter (ParamMode = pmRecordValue
or pmStaticArrayValue), the frame slot holds a pointer to the data
copy, not inline data — emit movq instead of leaq.
Bug 1+2 caused out-param record assignments to overwrite the pointer
with the return value. Bug 3 caused const-record field assignments
(e.g. FTarget := ATarget in SetTarget) to copy a stack address into the
field instead of the record data. Both crashed the native-compiled
compiler when using --backend native.
Added E2E test: TestRun_Native_RecordSret_OutParam.
Replace the hardcoded two-backend AssertRunsOnBoth with a set-driven
design that scales to any number of backends:
- TBackends = set of TBackend; AllBackends constant
- AssertRunsOnAll: iterates AllBackends (replaces AssertRunsOnBoth)
- AssertRunsOn(ABackends: TBackends; ...): run on a specific subset
- BackendName(TBackend): string for assertion labels
When a new backend (e.g. LLVM) is added to TBackend and AllBackends,
all 232 AssertRunsOnAll call sites automatically exercise it.
Align the QBE backend unit names with the dotted naming convention
already used by the native backend (blaise.codegen.native.*).
uCodeGen.pas → blaise.codegen.pas
uCodeGenQBE.pas → blaise.codegen.qbe.pas
Updated all uses clauses (~70 test files, main compiler units),
documentation references (README + 5 docs/*.adoc files), and
comment references in runtime/stdlib.
EmitMethodDef and EmitFuncDef called ClassifyRecordReturn four times on
the same return type (signature, declaration, prologue, epilogue).
Classify once into a local RC variable and pass it to each helper.
The per-call-site classification in EmitRecordReturnCallSite remains
unchanged — it classifies a different type (the callee's) each time.
FIXPOINT_OK, 2831 tests pass.
Implement SysV AMD64 ABI register-return classification in the native
x86-64 backend, matching the QBE backend's record-return support.
Small POD records (≤16 bytes, no managed fields) now return in registers
(rax/rdx for INTEGER, xmm0/xmm1 for SSE) instead of always using the
hidden sret pointer. Classification covers all seven SysV shapes:
rcInt1, rcInt2, rcSSE1, rcSSE2, rcIntSSE, rcSSEInt, and rcWin64Agg.
Changes:
- Move TRecReturnClass enum to uCodeGen.pas (shared by both backends)
- Add classification helpers to TNativeBackend (IsRecordManagedClean,
IsRecordAllIntegerLeaves, ClassifyRecordReturn, etc.)
- BuildFrame: reg-return records allocate Result as a local buffer
(not an sret pointer slot)
- EmitRecordReturnEpilogue: load Result buffer into return registers
- EmitRecordRegReturnCapture: store return registers into dest buffer
- EmitSretCall/EmitMethodSretCall: dispatch through reg-return path
when callee qualifies
- EmitFieldAddrToRcx: new helper for computing record/class field
addresses (needed by float field reads)
- EmitExprToXmm0: handle TFieldAccessExpr and TIntLiteral
- Fix float field assignment in TFieldAssignment (was using integer
load/store path for Single/Double fields)
- Fix Single argument passing (add cvtsd2ss when Double→Single
conversion needed)
Also adds 3 missing QBE E2E tests (nested-record, two-Single, managed-
field sret) and 9 native E2E tests covering all register-return shapes.
FIXPOINT_OK, 2831 tests pass.
Classify record returns per SysV x86-64 §3.2.3 instead of using sret
unconditionally. Small POD records (1-16 bytes, integer and/or float
leaves) now travel in registers (rax/rdx, xmm0/xmm1), matching how C
compilers return small structs. Records with managed fields (string,
class, interface, dynarray — checked recursively) always stay on sret.
Win64 delegates to QBE's -t amd64_win ABI pass via uniform aggregate
type declarations.
Based on Andrew Haines' qbe_record_byval_return branch, rebased onto
master and adapted. Added FlattenRecordToAggLetters to correctly
classify nested-record field types in QBE aggregate declarations.
39 IR tests (cp.test.recordret) + 12 E2E tests (cp.test.e2e.recordret).
2819 tests, FIXPOINT_OK.
Track string and dynamic-array buffer leaks in --debug builds.
Strings are registered in _StringAddRef (on the 0→1 transition)
and removed in _StringRelease; likewise for _DynArrayAddRef/Release.
The leak report prints "string" or "dynarray" as the type tag
instead of a class name.
Sentinel-based type tags (GLTTagString=Pointer(2), GLTTagDynArray=
Pointer(3)) distinguish buffer entries from class entries in the
hash map. The report reads the correct refcount header offset
for each type (12-byte string header, 8-byte dynarray header,
CLASS_HDR for classes).
LT_BUCKETS grows from 1024 to 4096 to accommodate the higher
entry count when strings/dynarrays are tracked. Report wording
changes from "object(s) not released" to "leak(s) not released".
2768 tests pass (2 new e2e tests), FIXPOINT_OK.
Extend _LeakTrackerRegister from 2 to 4 args (UserPtr, ClassName,
UnitName, Line) so the --debug leak report prints where each leaked
object was allocated. Both QBE and native backends now emit the
extra unit-name string literal and AST line number at every
_ClassAlloc site. Leak report output changes from:
- TBox (rc=1)
to:
- TBox (rc=1) at LeakSite:14
Runtime: bucket size grows from 16 to 32 bytes; LTInsert stores
unit-name ptr + line; _LeakTrackerReport prints the " at unit:line"
suffix.
Native backend: plumb FDebugMode from TCodeGenNative through
TNativeBackend; emit _LeakTrackerEnable in $main; emit
_LeakTrackerRegister at both constructor-call paths.
QBE backend: add FCurrentUnitName, set in Generate/GenerateUnit/
AppendUnit/AppendProgram; extend both _LeakTrackerRegister call
sites with EmitStrLit(FCurrentUnitName) + node.Line.
2766 tests pass (2 new e2e tests), FIXPOINT_OK.
When a record-returning function call is passed directly as an argument
to another call (e.g. Consume(MakeRec('hello'))), the caller allocates
an sret buffer on the stack. After the outer call returns, the managed
fields (strings, classes, dyn-arrays, interfaces) inside that buffer
must be released — otherwise they leak.
The QBE backend already handled this via EmitOwnedArgReleases; the
native backend lacked the equivalent. Add EmitSretArgReleases which
iterates args in reverse, computes each sret buffer's stack offset,
and calls EmitRecordFieldReleases to release every managed leaf.
Wire it into all call emission paths: EmitCall, EmitCallIndirect,
EmitMethodCallStmt, EmitMethodCallExpr, EmitInheritedCall,
EmitInterfaceCall, EmitInterfaceFieldCall, EmitMethodPtrCall, and
the implicit-self inline call sites.
Weak interface variables (both backends):
- Native: EmitInterfaceAssign routes [Weak] interface locals/globals
through _WeakAssign (no refcount) instead of _ClassAddRef/_ClassRelease.
Scope-exit cleanup emits _WeakClear for both interface and class weak
locals. EmitGlobalReleases checks IsWeakGlobal for program globals.
- QBE: already had full weak support (no changes needed).
Type-cast of interface variable (both backends):
- TVal(S) where S is an interface var must load from S_obj (the obj
half of the fat pointer), not bare S (which doesn't exist as a symbol).
QBE: EmitExpr type-cast path loads from VarRef_obj.
Native: EmitExprToEax type-cast path uses IntfObjOperand.
Previously crashed at link time with "undefined reference to S".
Two new e2e tests: IntfFieldFromFunc (sret into class field) and
WeakInterfaceVar (weak assignment + scope cleanup + destruction ordering).
Both backends now handle storing the result of an interface-returning
function into a class field:
H.F := MakeIntf(42); { explicit field assignment }
F := MakeIntf(42); { implicit-Self field inside a method }
QBE backend: EmitInterfaceToFieldSlots gains an IsInterfaceCall check
that allocates a 16-byte sret buffer, calls EmitRecordCallSret, loads
obj+itab from the buffer, releases the old field obj, and stores new —
no extra AddRef since the callee already retains into the sret buffer.
Native backend: both EmitInterfaceAssign (implicit-Self path) and
EmitInterfaceToFieldSlotsAt (explicit field path) gain TFuncCallExpr
branches that call EmitIntfSretCall, release the old obj from the
field, and copy obj+itab from the stack buffer into the field slots.
Previously both backends raised an error for this combination — the
sret convention for interface returns (commit c8ce6f7) only covered
local/global variable targets.
Both QBE and native backends now support functions/methods that return
an interface type. The return uses sret (struct-return) convention:
the caller allocates a 16-byte buffer (obj+itab), passes its address as
a hidden first argument, and the callee writes both fat-pointer slots
into the buffer.
QBE backend:
- Function signature adds hidden l %_par__sret, function becomes void
- Result_obj aliases sret+0, Result_itab aliases sret+8
- Epilogue is plain ret (no return value)
- Caller-side: alloc 16-byte buffer, EmitRecordCallSret, load obj+itab
- New IsInterfaceCall() helper and assignment handler for the sret path
Native backend:
- BuildFrame marks interface returns as FSretFunc (same as records)
- EmitIntfSretCall: allocates stack buffer, passes as %rdi, calls func
- EmitInterfaceAssign: new sret-Result path dereferences the Result
pointer to write obj+itab into the caller's buffer
- Assignment handler loads obj+itab from sret buffer after the call
Previously, functions returning interfaces crashed the QBE backend
(undeclared split-slot names in IR) and were unsupported in native.