EmitExpr for TBinaryExpr now checks for boAnd/boOr first and emits
short-circuit branches (jnz to sc_rhs/sc_end labels) before evaluating
either operand. The eager `and` instruction previously emitted was
safe for bitwise use but caused a null-pointer crash when the compiler
compiled itself: TSymbolTable.FindType evaluated Sym.Kind even when Sym
was nil, because the code generator did not short-circuit the guard
`(Sym <> nil) and (Sym.Kind = skTypeAlias)`.
Stage-3 now produces byte-identical IR to stage-2 (fixpoint reached).
1. ImplicitSelfField ARC: emit _ClassAddRef/_ClassRelease (or
_StringAddRef/_StringRelease) around storel when assigning to a
class- or string-typed implicit-Self field. Without this, the
TCodeGenQBE.FLines StringList was freed at Generate() exit, so
GetOutput returned garbage and stage-3 produced empty output.
2. UnescapeString: rewrite to handle both 'quoted' strings and #N
decimal char-code literals (the old version only handled
single-quoted forms, so #10 → empty string and GetText joined
all lines without newlines).
3. Chr builtin: add to symbol table, semantic pass, and codegen
(emits _Chr(w arg) → l result), required by the new UnescapeString.
4. AnalyseFieldAccess: set ResolvedMethod on parameterless constructor
TFieldAccessExpr nodes so the codegen can emit the Create body call
(companion to the EmitExpr IsConstructorCall fix from the previous
commit).
Add the full structure-return (sret) convention to the hand source so
stage-2 generates correct IR for functions/methods that return tyRecord:
- EmitMethodDef/EmitFuncDef: declare record-returning functions as void
with a hidden first param `l %_par__sret`; initialise %_var_Result
from it; emit bare `ret` at exit.
- IsRecordCall / EmitRecordCallSret: detect record-returning call
expressions (TFuncCallExpr, TMethodCallExpr, TFieldAccessExpr) and
emit them with the sret address as the first argument. Handles the
IsImplicitSelf path with correct Self+offset pointer arithmetic.
- EmitRecordCopy / EmitRecordReleaseFields: field-by-field ARC-correct
copy and release helpers for record types.
- EmitAssignment tyRecord branch: for record-typed lhs, either call
EmitRecordCallSret (when the rhs is a call) or EmitRecordCopy.
- ImplicitSelfField tyRecord branch in EmitAssignment: same logic for
assignments to record fields reached via implicit Self.
- TIdentExpr IsImplicitSelf tyRecord fix: return the field address
rather than loading through it (callers need the address for copies).
- TIdentExpr local/global record variable fix: return VarRef address
directly instead of an extra loadl.
All fixes mirror what the real multi-file source already had; this
brings the hand source to parity for record-return code generation.
- TSymbol gains ConstString field; AnalyseConstDecls stores CD.StrVal
- TIdentExpr gains ConstString and NoArgFuncDecl fields
- AnalyseIdent propagates ConstString to identifier exprs
- EmitExpr: string constants emit 'l copy $__sN' not 'w copy 0'
- IsNoArgFuncCall now set unconditionally for any skFunction with a
return type; NoArgFuncDecl is looked up from FProcIndex so codegen
can emit a proper call $FuncName() for user-defined zero-arg functions
- EmitExpr no-arg call path forwards NoArgFuncDecl as ResolvedDecl so
EmitFuncCall finds the declaration instead of falling through to
builtin dispatch and raising ECodeGenError
- TFieldAccessExpr IsMethodCall/PropRead/IsClassAccess paths use
VarRef(RecordName, IsGlobal) instead of hardcoded %%_var_ prefix,
fixing global-variable receivers such as Source.GetText
When EmitExpr generates code for A + B + C (string concat), the
intermediate temp T1 = A + B (from _StringConcat) starts with
refcnt=0. Before this fix that temp was silently abandoned:
calling _StringRelease(T1) with refcnt=0 would decrement to -1
(the IMMORTAL sentinel), so the string was never freed and the
compiler slowly exhausted all available memory when processing
large source files.
The correct pattern is to temporarily own the intermediate before
the second concat and release it after, so _StringConcat has a
chance to copy its bytes first:
_StringAddRef(T1) ; 0 -> 1 (temporary ownership)
T2 = _StringConcat(T1, C) ; copies T1's bytes into T2
_StringRelease(T1) ; 1 -> 0 -> freed
The same ownership pattern is applied to the right operand when
it is itself an unowned string expression (TBinaryExpr, TFuncCallExpr,
or TMethodCallExpr).
Also mirrors EmitMethodCall var-param forwarding fix (IsVarParam
check for both ObjExpr path and main path) and the previously-landed
EmitVarArgAddr, IsGlobal, vtable-in-ctor-with-args fixes to the hand
source (tests/blaise-compiler.pas).
With this fix stage-2 (blaise-compiler compiled by itself) compiles
the full hand source in 17s using 23MB RSS instead of hitting the OOM
killer at 47GB.
TMethodCallExpr.IsConstructorCall (TypeName.Create with args) was missing
the storel $vtable_X instruction after _ClassAlloc. The TFieldAccessExpr
path (no-arg Create) already had it. The missing store left vtable pointer
as zero, causing _IsInstance to crash with null vtable dereference during
type analysis of TTypeDesc subclasses.
Also adds virtual Destroy to TTypeDesc in the hand source so TTypeDesc and
its subclasses (TRecordTypeDesc, TEnumTypeDesc, TInterfaceTypeDesc) get
vtable slots, making HasVTable return true and field offsets correct.
Regression tests added for both the no-arg and with-arg constructor vtable
store paths.
The hand-written self-hosting TObjectList stored bare Pointers and
took no ARC ref when adding. In loops like
while Check(tkIdent) do
begin
CD := TConstDecl.Create;
...
ABlock.ConstDecls.Add(CD);
end;
reassigning CD on the next iteration released the prior CD (its only
strong ref) and the list's pointer dangled. When a later allocation
reused that heap slot, the list silently contained the wrong object,
and semantic analysis crashed in TSymbol.Create reading CD.Name from
an unrelated object's memory.
Add/Put/Delete/Clear/Destroy now AddRef on insert and Release on
remove or destroy, gated on FOwnsObjects so the semantics match the
real compiler's TObjectList. Extract continues to transfer the ref
to the caller without release.
Lets stage-2 compile the hand source far enough to hit the next bug
(currently a nil Self in TRecordTypeDesc.AddVTableSlot during
AnalyseTypeDecls) — more hand-source polish to follow.
The blaise compiler now compiles its own source (tests/blaise-compiler.pas)
into a second-stage binary that successfully compiles and runs a hello-world
program. All 834 compiler tests still pass.
Codegen and RTL fixes that unblocked self-hosting:
- String literal interning (uCodeGenQBE): set FStrLits.CaseSensitive := True.
FPC's TStringList.IndexOf is case-insensitive by default, so distinct
literals like 'WRITELN' and 'WriteLn' collapsed to the same label.
- ARC on implicit Self.Field assignments (uCodeGenQBE): EmitAssignment's
implicit-Self branch did a raw storel for class/string fields. Fresh
_ClassAlloc'ed objects start at refcnt 0, so without the addref they were
freed by later local releases. Now addrefs new value and releases old.
- #nn character literals (uLexer): UnescapeString handled only 'text'.
Extended to decode #nn decimal literals and concatenated forms like
'abc'#13#10'def'. Local OrdAt helper lets the body parse under FPC and
the self-hosted compiler.
- Chr() builtin: registered in uSymbolTable, handled in uSemantic, emitted
in uCodeGenQBE as _Chr, implemented in rtl/blaise_str.c.
- LF line separator (Classes.pas): TStringList.GetText used #13#10; QBE
rejects CR in its input. Changed to #10.
Self-hosting source (tests/blaise-compiler.pas) — needs a virtual Destroy
on TASTNode and Exception (post-TObject-stripping they became root classes
without a vtable, breaking `is` checks and exception message layout).
TAST* descendants chain `override`.
Migration tooling: add tools/migrate_full.py — the script that generates
tests/blaise-compiler.pas by flattening the eight compiler units and RTL
Classes unit into a single self-contained Pascal source. The postprocess()
step injects virtual Destroy on TASTNode / TTypeDesc and strips `override`
from non-vtable root classes.
Implicit Self: bare field names inside methods (e.g. FPos := FPos + 1)
now work without explicit Self. prefix. TIdentExpr.IsImplicitSelf /
ImplicitFieldInfo flags set by AnalyseExpr when lookup fails but a class
field matches; TAssignment.ImplicitSelfField set by AnalyseAssignment.
Codegen emits loadl %_var_Self + offset for reads, storew/storel through
Self + offset for writes. Required for migrating the compiler source which
was written in FPC style without explicit Self.
OrdAt(s, i): integer RTL function — returns ASCII ordinal of character at
1-based position i. Replaces FSource[FPos] (array indexing) in the
migrated uPasTokeniser.
Migration script (tests/blaise-compiler.pas): initial work-in-progress
self-hosting source. Script handles: const→class body stripping, inline
qualifier removal, inline-var removal, Exit(expr) expansion, shr/shl
conversion, constructor/destructor→procedure, access modifier stripping,
keyword array → TStringList/InitKeywords, FSource[expr] → OrdAt(FSource, expr),
char-range in [...] → integer comparisons, repeat...until → while True.
834 tests pass.
Constructor calls with arguments: TMethodCallExpr.IsConstructorCall flag;
AnalyseMethodCallExpr handles TypeName.Create(args) → allocate via
_ClassAlloc then call user-defined Create method; EmitCaseStmt generates
dispatch, EmitExpr handles the new constructor path.
const block: TConstDecl AST node; TBlock.ConstDecls list; ParseConstBlock;
AnalyseConstDecls registers each constant as skConstant in the symbol table.
tests/blaise-compiler.pas: initial self-hosting source stub — program header,
CHR_* character constants, Exception hierarchy, and Classes.pas RTL content.
The file is built incrementally; this commit has sections 1-2.
834 tests pass.
const: TConstDecl AST node; ParseConstBlock (integer and string literal values);
AnalyseConstDecls (registers each as skConstant in symbol table). Enables
CHR_* character constants and dupAccept/dupIgnore/dupError in self-hosting source.
ParseBlock: add tkConst to the multi-section loop.
Blaise now supports: case, enum, const, multi-type/var blocks, all file I/O
builtins — the complete language surface needed for the source migration.
834 tests pass.
New builtins: CompareStr, CompareText (→ _StringCompare/_StringCompareText),
ZeroMem (→ memset), _ClassAddRef/_ClassRelease for manual ARC management.
EmitPointerWrite now emits retain/release when the base type is tyString,
enabling ARC-correct string storage in ^string arrays. ZeroMem is used to
zero-initialise newly grown string slots so the "release old" half of ARC
never sees uninitialised memory.
Classes.pas: TObjectList (^Pointer array, no ARC overhead) and TStringList
(^string + ^Pointer parallel arrays, sorted binary search via CompareText,
complete Delete/Insert/Clear with correct ARC). Generics.Collections and
phase3_milestone updated to zero-init new array slots, fixing previously
hidden memory safety issues revealed by the EmitPointerWrite ARC fix.
785 tests pass, zero Valgrind errors.
Compiler:
- TRecordTypeDesc gains HasDestroyMethod boolean property, set by the
semantic analyser whenever a class (regular or generic instantiation)
declares a 'Destroy' method.
- EmitFieldCleanupFn emits a call to $<TypeName>_Destroy before the
ARC field-release loop when HasDestroyMethod is set, giving classes a
hook to free raw resources (malloc buffers etc.) at refcount zero.
RTL (Generics.Collections):
- TList<T> and TDictionary<K,V> replace 'procedure Free' with
'procedure Destroy'. Destroy frees the internal raw buffers (FData /
FKeys / FValues) and nils them, making re-entrant calls safe.
Free is no longer user-defined on these classes so List.Free uses
the built-in ARC release path (_ClassRelease + slot nil-out).
tests/phase3_milestone.pas:
- TIntList and TStrIntDict migrated to Destroy (removed FreeMem(Self)
which was unsafe under universal ARC on TObject).
Tests (718 total, 0 failures):
- cp.test.arc: 3 new IR tests for Destroy dispatch in field cleanup fn
(non-generic, absent-on-class-without-Destroy, generic instantiation).
- cp.test.e2e: 4 new end-to-end / valgrind tests — ClassDestroy frees
buffer, TList ARC lifecycle, Phase3Milestone stdout and valgrind-clean.
The exception frame was allocated as alloc16 32 (32 × 16 = 512 bytes on paper,
but QBE's alloc16 N means N items of 16 bytes: so 32 × 16 = 512 is actually
correct). Wait - re-reading: alloc16 N in QBE allocates N bytes aligned to 16.
So alloc16 32 was only 32 bytes — far less than sizeof(BlaiseExcFrame) which
needs ~216 bytes on Linux x86_64 (200-byte jmp_buf + two pointer fields).
setjmp writes its full jmp_buf into the undersized slot, silently corrupting
whatever sat above it on the stack: saved registers, local variables, and the
virtual method pointer loaded for virtual dispatch. Any try block followed by
a virtual method call in expression position crashed with a bad function pointer.
Fix: alloc16 32 → alloc16 512 in both EmitTryFinallyStmt and EmitTryExceptStmt,
matching the RTL contract documented in blaise_exc.c lines 8-13.
Two new regression tests assert alloc16 512 appears in emitted IR for both
try forms. 628 tests pass (was 626).
Also adds tests/phase2_milestone.pas: linked list with virtual dispatch,
inheritance, try/finally, and 'is' type test — zero valgrind leaks.
Updates design.adoc Phase 3 status table to reflect current implementation.
Four bugs fixed on the path to the milestone:
1. Non-generic class fields resolved with FindType instead of
FindTypeOrInstantiate — typed pointer fields (^Integer, ^string) in
concrete classes failed with 'Unknown type'.
2. Integer/Boolean/Int64 local variables not zero-initialised — QBE
validator rejected 'slot read but never stored to' for variables
written only through var-param pointers.
3. Class method var-param signature wrong — EmitMethodDef emitted the
param type (w for Integer) rather than 'l' (pointer) for var params;
EmitParamAllocs spilled the pointer with storew instead of storel.
4. Method call sites did not pass addresses for var-param arguments —
EmitMethodCallExpr always called EmitExpr (value load) rather than
forwarding %_var_Name (address) for IsVarParam params.
Milestone result (valgrind --leak-check=full):
7 allocs, 7 frees, 0 bytes in use at exit, ERROR SUMMARY: 0 errors
Symbol table:
- Added tyClass kind and NewClassType factory; TRecordTypeDesc now
accepts an optional kind parameter so records and classes share the
same descriptor with distinct semantics.
Semantic analyser:
- AnalyseTypeDecls runs before PushScope so type symbols land in global
scope and survive PopScope.
- Handles TRecordTypeDef and TClassTypeDef; sets IsConstructorCall on
TypeName.Create expressions and IsClassAccess on class-variable field
access and assignment nodes.
Code generator (QBE IR):
- Class variables: 8-byte pointer slot, zeroed on entry.
- Constructor calls: malloc(sizeof fields), store pointer.
- Class field access/write: load pointer, add field offset, load/store.
- ARC string assignment: AddRef new value, Release old value, then store.
- Block exit: Release every string variable in scope (EmitStringCleanup).
Lexer / Parser / AST:
- Added tkClass keyword and ParseClassDef; FieldDecl parsing refactored
into ParseFieldDecl(AFields) shared by both record and class.
- AST gains TClassTypeDef, and IsConstructorCall/IsClassAccess flags on
TFieldAccessExpr and TFieldAssignment.
RTL:
- blaise_arc.c: Phase 2 no-op stubs for _StringAddRef / _StringRelease.
- rtl/Makefile: builds blaise_rtl.a; `make install` copies it next to
the compiler binary for automatic discovery by FindRTL.
- Blaise.pas driver: FindRTL checks BLAISE_RTL env var then binary dir;
links the RTL archive when present.
Tests:
- 180 unit tests (records ×23, classes ×22, ARC ×9, codegen ×9, …).
- 4 end-to-end integration tests in tests/integration/test_arc_strings.sh
covering string assignment, two-var cleanup, reassignment cycle, and
empty-program linkage.
Docs:
- design.adoc updated with Phase 2 implementation status table.
- docs/grammar.ebnf: new authoritative EBNF grammar for the Blaise
language as implemented, including ARC semantic annotations.