Files
ziran/cmd/compiler_text.zi
T
waoziandClaude Opus 5.5 931fed4c86 Run ZX natively by default and integrate the finished assembly lanes
One commit, because the parts below only build together.

Allocator: an interval needed at the current instruction is never
evicted, and the allocated-program verifier checks that every operand sits
where the allocation says.

AArch64 encoder: compiler_aarch64.zi encodes allocated machine IR with the
shared ARM byte helpers, maps the function-address relocation,
sign-extends signed 8- and 16-bit loads and rejects what it cannot encode
with a named diagnostic. It has no AOT ELF writer yet.

Native default with checked pointers and no fallback: on linux-x86_64,
`ziran run app.zx`, `zi2zx run`, the embedding API's Auto and the Kryon
players run a bundle's verified program as native code in W^X memory.
Stray and freed pointers, double free, bounds and read-only stores trap. A
native gap is a named preflight error, nothing falls back to the engine,
and `--backend engine` picks it explicitly. zx2native uses the same
preflight. Native code exchanges module state for hot reload, checks the
stack, zeroes fresh locals, spends fuel at the engine's checkpoints and
returns records, arrays and strings from host calls. `zi2zx --debug` source
origins name lines in native traps and `zx2native --emit map`. The host
value API drops its VM prefix (HostValue, HOST_*). A call with a record
result loads the hidden result address into rax only after its stack
arguments are stored, and an indirect target stays in r10: rax could hold
a stack argument (Kryon's text-field clip guard arrived as an address) or
the target (indirect calls returning records crashed).

Self-hosting ports: compiler_backend_process, compiler_backend_cache,
compiler_native_names and compiler_runtime_emit replace the zir_c_build,
zir_c_cache and zir_c_process C, with checked bootstrap C and Makefile
rules. zir_c.toolchain again reports "compiling OUT failed:" with the head
of the compiler output and a truncation marker. The parallel scheduler no
longer stalls on a held compiler after reaping a finished job, and poll or
read errors now fail the build. A fresh BUILD_DIR (Kryon's runtime build)
builds: generated files have pattern rules, objects wait for the headers
they include, and engine flags are per object instead of leaking into zi2c.

Legacy removals F1-F10 and N1-N4: the duplicate LirX64 encoder and its
spec test, zir_c_*, zir_runtime and zir_native, nine zero-reference spec
tests, vec_capacity, the zx_metadata and native_cache_names benchmarks,
build-android-host.sh and two shell wrappers are deleted; zx2native is
machine-only; `zi2zx` rejects `--opt` on ZX input; the bootstrap is
regenerated. ASSEMBLY docs, the legacy-removal plan, the milestones and the
remaining-work plan record the current position.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 14:04:48 -03:00

381 lines
14 KiB
Ziran

// Shared compiler text rules. Buffers belong to the caller; this module uses
// no native imports, so its behavior can also be checked on the portable engine.
SourceRange :: struct { begin: s64; end: s64 }
SourcePart :: struct { begin: s64; end: s64; next: s64 }
DecodedLiteral :: struct { valid: bool; count: s64 }
FormatStep :: struct { valid: bool; argument: bool; present: bool; next: s64 }
LiteralBits :: struct { valid: bool; value: u64 }
// Bounded byte edit distance used by name diagnostics. An exceeded limit
// returns limit + 1; invalid limits return 128. Compiler names are <128 bytes.
NameDistance :: (a: string, b: string, limit: s32) -> s32 {
if limit < 0 || limit >= 128 { return 128 }
if a.count >= 128 || b.count >= 128 ||
a.count - b.count > limit || b.count - a.count > limit { return limit + 1 }
previous: [128]s32
current: [128]s32
for j: 0..b.count { previous[j] = cast(s32)j }
i: s64 = 1
while i <= a.count {
best := cast(s32)i
current[0] = best
j: s64 = 1
while j <= b.count {
substitute := previous[j - 1] + cast(s32)(a[i - 1] != b[j - 1])
remove := previous[j] + 1
insert := current[j - 1] + 1
value := substitute
if remove < value { value = remove }
if insert < value { value = insert }
current[j] = value
if value < best { best = value }
j += 1
}
if best > limit { return limit + 1 }
for j: 0..b.count { previous[j] = current[j] }
i += 1
}
distance := previous[b.count]
if distance > limit { return limit + 1 }
return distance
}
SpaceByte :: (byte: u8) -> bool { return byte == 32 || (byte >= 9 && byte <= 13) }
IdentifierByte :: (byte: u8) -> bool {
return (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122) ||
(byte >= 48 && byte <= 57) || byte == 95
}
SkipSpace :: (source: string, at: s64, inline_only: bool) -> s64 {
while at < source.count {
byte := source[at]
if inline_only {
if byte != 32 && byte != 9 { break }
} else if !SpaceByte(byte) { break }
at += 1
}
return at
}
TrimRange :: (source: string) -> SourceRange {
begin := SkipSpace(source, 0, false)
end := source.count
while end > begin && SpaceByte(source[end - 1]) { end -= 1 }
return .{begin, end}
}
// An unsigned integer literal as the language spells it: decimal, 0x hex,
// 0b binary or a leading-0 octal, with _ separators after the prefix, up to
// the full u64 range. All of SOURCE must be that literal.
IntegerLiteral :: (source: string) -> LiteralBits {
if source.count == 0 || source[0] < #char "0" || source[0] > #char "9" { return .{} }
base: u64 = 10
at: s64 = 0
if source[0] == #char "0" && source.count > 1 {
base = 8
at = 1
if source[1] == #char "x" || source[1] == #char "X" { base = 16; at = 2 }
else if source[1] == #char "b" || source[1] == #char "B" { base = 2; at = 2 }
}
digits := false
value: u64 = 0
while at < source.count {
byte := source[at]
at += 1
if byte == #char "_" { continue }
digit := HexDigit(byte)
if digit < 0 || cast(u64)digit >= base { return .{} }
if value > (~cast(u64)0 - cast(u64)digit) / base { return .{} }
value = value * base + cast(u64)digit
digits = true
}
if !digits && base != 8 { return .{} }
return .{true, value}
}
BlockHeaderEnd :: (source: string) -> s64 {
end := source.count
while end > 0 && SpaceByte(source[end - 1]) { end -= 1 }
if end > 0 && source[end - 1] == #char "{" { end -= 1 }
while end > 0 && SpaceByte(source[end - 1]) { end -= 1 }
return end
}
CamelIdentifier :: (source: string, output: []u8) -> s64 {
if output.count == 0 { return 0 }
used: s64 = 0
upper := true
for byte: source {
if used + 1 >= output.count { break }
if (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122) ||
(byte >= 48 && byte <= 57) {
if upper && byte >= 97 && byte <= 122 { byte -= 32 }
output[used] = byte
used += 1
upper = false
} else { upper = true }
}
if used == 0 && used + 1 < output.count { output[used] = #char "X"; used += 1 }
if used > 0 && output[0] >= 48 && output[0] <= 57 && used + 1 < output.count {
index := used
while index > 0 { output[index] = output[index - 1]; index -= 1 }
output[0] = #char "M"
used += 1
}
output[used] = 0
return used
}
GoFieldIdentifier :: (source: string, output: []u8) -> s64 {
used := CamelIdentifier(source, output)
replacement := ""
{
name := TextView(output[0:used])
if name == "Id" { replacement = "ID" }
else if name == "FocusId" { replacement = "FocusID" }
else if name == "MenuId" { replacement = "MenuID" }
else if name == "SelectedId" { replacement = "SelectedID" }
else if name == "ActivatedId" { replacement = "ActivatedID" }
else if name == "CanonicalUrl" { replacement = "CanonicalURL" }
}
if replacement.count == 0 { return used }
for index: 0..used - 1 { output[index] = replacement[index] }
return used
}
EscapeCString :: (source: string, output: []u8) -> s64 {
if output.count == 0 { return 0 }
used: s64 = 0
for byte: source {
if used + 1 >= output.count { break }
escaped: [4]u8
count := EscapeByte(byte, true, escaped[:])
for index: 0..count - 1 {
if used + 1 < output.count { output[used] = escaped[index]; used += 1 }
}
}
output[used] = 0
return used
}
// next is -1 at the end. Commas inside quotes or nested delimiters stay in
// the part; unterminated quotes stop at the input boundary.
NextSourcePart :: (source: string, at: s64) -> SourcePart {
if at < 0 || at > source.count { return .{0, 0, -1} }
start := at
depth: s32 = 0
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at) }
else if byte == #char "," && depth == 0 { break }
else {
if byte == #char "(" || byte == #char "[" || byte == #char "{" { depth += 1 }
else if byte == #char ")" || byte == #char "]" || byte == #char "}" {
if depth > 0 { depth -= 1 }
}
at += 1
}
}
end := at
next: s64 = -1
if at < source.count { next = at + 1 }
return .{start, end, next}
}
TopLevelAssignment :: (source: string) -> s64 {
at: s64 = 0
depth: s32 = 0
while at < source.count {
byte := source[at]
if byte == #char "\"" || byte == #char "'" { at = AfterQuote(source, at) }
else {
if byte == #char "(" || byte == #char "[" || byte == #char "{" { depth += 1 }
else if byte == #char ")" || byte == #char "]" || byte == #char "}" {
if depth > 0 { depth -= 1 }
} else if byte == #char "=" && depth == 0 && Byte(source, at + 1) != #char "=" &&
(at == 0 || (source[at - 1] != #char "=" && source[at - 1] != #char "!" &&
source[at - 1] != #char "<" && source[at - 1] != #char ">")) { return at }
at += 1
}
}
return -1
}
// Escaped bytes and literal bytes pass through the same UTF-8 validator.
// A failed decode may write a prefix, but never reports a successful count.
DecodeString :: (source: string, output: []u8) -> DecodedLiteral {
if source.count < 2 || source[0] != #char "\"" || source[source.count - 1] != #char "\"" {
return .{false, 0}
}
used: s64 = 0
remaining: s32 = 0
scalar: u32 = 0
minimum: u32 = 0
at: s64 = 1
while at + 1 < source.count {
value := cast(u32)source[at]
unicode_escape := false
if value == #char "\\" {
at += 1
if at + 1 >= source.count { return .{false, 0} }
value = source[at]
escapes := "0abfnrtv\\\""
values: [10]u8 = .{0, 7, 8, 12, 10, 13, 9, 11, 92, 34}
found := false
for index: 0..escapes.count - 1 {
if value == escapes[index] { value = values[index]; found = true; break }
}
if !found {
if value != #char "x" && value != #char "u" && value != #char "U" {
return .{false, 0}
}
unicode_escape = value != #char "x"
digits: s32 = 2
if value == #char "u" { digits = 4 }
else if value == #char "U" { digits = 8 }
value = 0
for index: 0..digits - 1 {
at += 1
if at + 1 >= source.count { return .{false, 0} }
digit := HexDigit(source[at])
if digit < 0 { return .{false, 0} }
value = value * 16 + cast(u32)digit
}
if value > 0x10ffff || (value >= 0xd800 && value <= 0xdfff) { return .{false, 0} }
}
} else if value == #char "\"" { return .{false, 0} }
bytes: [4]u8
bytes[0] = cast(u8)value
count: s32 = 1
if unicode_escape && value >= 128 {
if value < 0x800 {
bytes[0] = cast(u8)(0xc0 | (value >> 6))
bytes[1] = cast(u8)(0x80 | (value & 63))
count = 2
} else if value < 0x10000 {
bytes[0] = cast(u8)(0xe0 | (value >> 12))
bytes[1] = cast(u8)(0x80 | ((value >> 6) & 63))
bytes[2] = cast(u8)(0x80 | (value & 63))
count = 3
} else {
bytes[0] = cast(u8)(0xf0 | (value >> 18))
bytes[1] = cast(u8)(0x80 | ((value >> 12) & 63))
bytes[2] = cast(u8)(0x80 | ((value >> 6) & 63))
bytes[3] = cast(u8)(0x80 | (value & 63))
count = 4
}
}
for index: 0..count - 1 {
byte := bytes[index]
if remaining > 0 {
if (byte & 0xc0) != 0x80 { return .{false, 0} }
scalar = (scalar << 6) | cast(u32)(byte & 63)
remaining -= 1
if remaining == 0 && (scalar < minimum || scalar > 0x10ffff ||
(scalar >= 0xd800 && scalar <= 0xdfff)) { return .{false, 0} }
} else if byte >= 128 {
if byte >= 0xc2 && byte <= 0xdf { remaining = 1; scalar = byte & 31; minimum = 0x80 }
else if byte >= 0xe0 && byte <= 0xef { remaining = 2; scalar = byte & 15; minimum = 0x800 }
else if byte >= 0xf0 && byte <= 0xf4 { remaining = 3; scalar = byte & 7; minimum = 0x10000 }
else { return .{false, 0} }
}
if used >= output.count { return .{false, 0} }
output[used] = byte
used += 1
}
at += 1
}
if remaining != 0 { return .{false, 0} }
return .{true, used}
}
// Decode a format once, then call this until present is false. Each literal
// is re-encoded for the ordinary checked string path of every backend.
NextPrintPiece :: (bytes: []u8, at: s64, output: []u8) -> FormatStep {
if at < 0 || at > bytes.count { return .{false, false, false, at} }
if at == bytes.count { return .{true, false, false, at} }
if bytes[at] == #char "%" && (at + 1 >= bytes.count || bytes[at + 1] != #char "%") {
if output.count > 0 { output[0] = 0 }
return .{true, true, true, at + 1}
}
if output.count < 3 { return .{false, false, false, at} }
output[0] = #char "\""
used: s64 = 1
while at < bytes.count {
byte := bytes[at]
if byte == #char "%" {
if at + 1 >= bytes.count || bytes[at + 1] != #char "%" { break }
at += 1
}
escaped: [4]u8
count := EscapeByte(byte, false, escaped[:])
if used + count + 2 > output.count { return .{false, false, false, at} }
for index: 0..count - 1 { output[used] = escaped[index]; used += 1 }
at += 1
}
output[used] = #char "\""
output[used + 1] = 0
return .{true, false, true, at}
}
OperatorProcedure :: (op: string) -> string {
for index: 0..15 { if op == Operators[index] { return Procedures[index] } }
return ""
}
OperatorTokenLength :: (source: string) -> s64 {
for index: 0..15 {
op := Operators[index]
if source.count >= op.count && source[0:op.count] == op { return op.count }
}
return 0
}
ProcedureOperator :: (name: string) -> string {
for index: 0..15 { if name == Procedures[index] { return Operators[index] } }
return ""
}
// The value of hex digit BYTE, or -1.
HexDigit :: (byte: u8) -> s32 {
if byte >= 48 && byte <= 57 { return cast(s32)byte - 48 }
if byte >= 97 && byte <= 102 { return cast(s32)byte - 97 + 10 }
if byte >= 65 && byte <= 70 { return cast(s32)byte - 65 + 10 }
return -1
}
#scope_file
Operators: [16]string = .{"==", "!=", "<=", ">=", "<<", ">>", "+", "-", "*", "/", "%", "<", ">", "&", "|", "^"};
Procedures: [16]string = .{"operator_equal", "operator_not_equal", "operator_less_equal", "operator_greater_equal", "operator_shift_left", "operator_shift_right", "operator_add", "operator_subtract", "operator_multiply", "operator_divide", "operator_modulo", "operator_less", "operator_greater", "operator_bit_and", "operator_bit_or", "operator_bit_xor"};
Byte :: (source: string, at: s64) -> u8 {
if at < 0 || at >= source.count { return 0 }
return source[at]
}
AfterQuote :: (source: string, at: s64) -> s64 {
quote := source[at]
at += 1
while at < source.count {
byte := source[at]
at += 1
if byte == quote { break }
if byte == #char "\\" && at < source.count { at += 1 }
}
return at
}
EscapeByte :: (byte: u8, c_string: bool, output: []u8) -> s64 {
output[0] = #char "\\"
if byte == #char "\"" || byte == #char "\\" { output[1] = byte; return 2 }
if byte == 10 { output[1] = #char "n"; return 2 }
if byte == 9 { output[1] = #char "t"; return 2 }
if byte == 13 && c_string { output[1] = #char "r"; return 2 }
if byte < 32 || byte == 127 {
hex := "0123456789abcdef"
output[1] = #char "x"
output[2] = hex[byte >> 4]
output[3] = hex[byte & 15]
return 4
}
output[0] = byte
return 1
}