The tools are renamed after their input and output: zi2zx lowers source or ZIR to executable ZX, zx2native turns ZX into a native ELF, zx2wasm into a wasm32 module, and zi2c, zi2go and zi2zir keep their roles under cmd/ folders of the same names. zi2asm and the cmd/zir-* folders are gone, and tests/tool_names.sh keeps the old names out. zx2native keeps the direct encoder as its default; the machine pipeline replaces it once it reaches parity. The embedding API can run a verified bundle as x86-64 machine code in memory. Code is written to RW pages, relocated, then switched to RX, never RWX, and each instance gets its own data (cmd/zir/zir_native_memory.c, zir_host_native.c). Backend selection is BundleBackendPolicy: Engine and Native are strict, and Auto chooses by platform only, never by whether one program fits. A native gap is a named error, never a switch to the engine. Desktop x86-64 Auto stays on the engine until the native backend is complete. cmd/compiler_machine_image.zi adds the target-neutral machine image contract. The machine verifiers reject unknown relocation kinds, out-of-range symbols and addends, non-host external calls, native-width mismatches and missing stack or fuel facts. The machine pipeline lowers and encodes more of LIR. Docs, site pages, Kryon's build references and the benchmarks use the new names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
450 lines
15 KiB
Ziran
450 lines
15 KiB
Ziran
Diagnostics :: #import "diagnostics"
|
|
#import "byte_text_linux"
|
|
#import "c_string"
|
|
#import "file_linux"
|
|
#import "text"
|
|
#import "package_process"
|
|
|
|
#scope_export
|
|
libc :: #system_library "libc";
|
|
PkgMallocRaw :: (size: u64) -> *u8 #foreign libc "malloc";
|
|
PkgFreeRaw :: (memory: *u8) #foreign libc "free";
|
|
PkgCallocRaw :: (count: u64, size: u64) -> *u8 #foreign libc "calloc";
|
|
PkgGetenvRaw :: (name: *u8) -> *u8 #foreign libc "getenv";
|
|
PkgGetcwdRaw :: (buffer: *u8, size: u64) -> *u8 #foreign libc "getcwd";
|
|
PkgRealpathRaw :: (path: *u8, resolved: *u8) -> *u8 #foreign libc "realpath";
|
|
PkgWriteRaw :: (fd: s32, bytes: *u8, count: u64) -> s64 #foreign libc "write";
|
|
#if #defined(__APPLE__) {
|
|
// libSystem provides both; macOS does not ship OpenSSL's libcrypto.
|
|
PkgDigestRaw :: (input: *u8, size: u32, digest: *u8) -> *u8 #foreign libc "CC_SHA256";
|
|
PkgRandomFillRaw :: (output: *u8, size: u64) #foreign libc "arc4random_buf";
|
|
PkgSha256Raw :: (input: *u8, size: u64, digest: *u8) -> *u8 {
|
|
if size > cast(u64)4294967295 { return null }
|
|
return PkgDigestRaw(input, cast(u32)size, digest)
|
|
}
|
|
PkgRandomRaw :: (output: *u8, size: s32) -> s32 {
|
|
if size < 0 { return 0 }
|
|
PkgRandomFillRaw(output, cast(u64)size)
|
|
return 1
|
|
}
|
|
} else {
|
|
crypto :: #system_library "crypto";
|
|
PkgSha256Raw :: (input: *u8, size: u64, digest: *u8) -> *u8 #foreign crypto "SHA256";
|
|
PkgRandomRaw :: (output: *u8, size: s32) -> s32 #foreign crypto "RAND_bytes";
|
|
}
|
|
|
|
PkgReadResult :: struct {
|
|
text: string
|
|
valid: bool
|
|
}
|
|
|
|
PkgProcessResult :: struct {
|
|
text: string
|
|
code: s32
|
|
}
|
|
|
|
PkgSetDiagnosticFormat :: (arguments: []string) {
|
|
Diagnostics.SetFormat("text")
|
|
if PkgGetenv("ZIRAN_DIAGNOSTICS") == "json" { Diagnostics.SetFormat("json") }
|
|
for argument: arguments {
|
|
if argument == "--" { break }
|
|
if argument == "--diagnostics=json" { Diagnostics.SetFormat("json") }
|
|
if argument == "--diagnostics=text" { Diagnostics.SetFormat("text") }
|
|
}
|
|
}
|
|
|
|
#program_export
|
|
PkgAlloc :: (size: u64) -> *u8 { return PkgMallocRaw(size) }
|
|
|
|
#program_export
|
|
PkgAllocZero :: (size: u64) -> *u8 { return PkgCallocRaw(1, size) }
|
|
|
|
PkgCopy :: (target: []u8, at: s64, value: string) -> s64 {
|
|
if at < 0 || at + cast(s64)value.count > cast(s64)target.count { return -1 }
|
|
index: s64 = 0
|
|
while index < cast(s64)value.count {
|
|
target[at + index] = value[index]
|
|
index += 1
|
|
}
|
|
return at + cast(s64)value.count
|
|
}
|
|
|
|
PkgJoin :: (left: string, right: string) -> string {
|
|
total: u64 = cast(u64)left.count + cast(u64)right.count
|
|
if total > 16777216 { return "" }
|
|
memory: *u8 = PkgMallocRaw(total + 1)
|
|
if memory == null { return "" }
|
|
buffer: []u8 = BytesFromPointer(memory, total + 1)
|
|
at: s64 = PkgCopy(buffer, 0, left)
|
|
at = PkgCopy(buffer, at, right)
|
|
buffer[at] = cast(u8)0
|
|
return TextFromBytes(buffer[0:at])
|
|
}
|
|
|
|
PkgJoin3 :: (first: string, second: string, third: string) -> string {
|
|
return PkgJoin(PkgJoin(first, second), third)
|
|
}
|
|
|
|
PkgGetenv :: (name: string) -> string {
|
|
buffer: [256]u8
|
|
if !CopyCString(name, buffer[:]) { return "" }
|
|
return TextFromCString(PkgGetenvRaw(*buffer[0]))
|
|
}
|
|
|
|
PkgCanonical :: (path: string) -> string {
|
|
buffer: [4096]u8
|
|
if !CopyCString(path, buffer[:]) { return "" }
|
|
return TextFromCString(PkgRealpathRaw(*buffer[0], null))
|
|
}
|
|
|
|
PkgCwd :: () -> string {
|
|
memory: *u8 = PkgMallocRaw(4096)
|
|
if memory == null || PkgGetcwdRaw(memory, 4096) == null { return "" }
|
|
return TextFromCString(memory)
|
|
}
|
|
|
|
PkgLauncherDirectory :: (launcher: string) -> string {
|
|
path: string = PkgCanonical(launcher)
|
|
if path.count == 0 { return "" }
|
|
end: s64 = cast(s64)path.count - 1
|
|
while end > 0 && path[end] != cast(u8)47 { end -= 1 }
|
|
return PkgJoin("", path[0:end])
|
|
}
|
|
|
|
PkgProjectRoot :: () -> string {
|
|
current: string = PkgCwd()
|
|
while current.count > 0 {
|
|
if PathExists(PkgJoin(current, "/ziran.toml")) { return current }
|
|
if current == "/" { break }
|
|
last: s64 = cast(s64)current.count - 1
|
|
while last > 0 && current[last] != cast(u8)47 { last -= 1 }
|
|
if last == 0 { current = "/" }
|
|
else { current = current[0:last] }
|
|
}
|
|
return ""
|
|
}
|
|
|
|
PkgRead :: (path: string) -> PkgReadResult { return PkgReadLimited(path, 16777216) }
|
|
|
|
PkgReadLimited :: (path: string, limit: s64) -> PkgReadResult {
|
|
result: PkgReadResult
|
|
file: FileHandle = OpenRead(path)
|
|
if !file.valid { return result }
|
|
size: s64 = FileSize(file)
|
|
if size < 0 || size > limit {
|
|
CloseFile(file)
|
|
return result
|
|
}
|
|
memory: *u8 = PkgMallocRaw(cast(u64)size + 1)
|
|
if memory == null {
|
|
CloseFile(file)
|
|
return result
|
|
}
|
|
bytes: []u8 = BytesFromPointer(memory, cast(u64)size + 1)
|
|
if size > 0 && ReadAt(file, bytes[0:size], 0) != size {
|
|
CloseFile(file)
|
|
return result
|
|
}
|
|
CloseFile(file)
|
|
bytes[size] = cast(u8)0
|
|
result.text = TextFromBytes(bytes[0:size])
|
|
result.valid = true
|
|
return result
|
|
}
|
|
|
|
PkgWrite :: (path: string, content: string) -> bool {
|
|
file: FileHandle = OpenReplace(path)
|
|
if !file.valid { return false }
|
|
valid: bool = WriteTextAt(file, content, 0) == cast(s64)content.count
|
|
if valid { valid = SyncFile(file) }
|
|
CloseFile(file)
|
|
return valid
|
|
}
|
|
|
|
// Readers see either complete version. Every writer owns an exclusive private
|
|
// temporary beside the destination; concurrent writers never share a .tmp.
|
|
PkgWriteAtomic :: (path: string, content: string) -> bool {
|
|
random: [16]u8; hex: [32]u8; digits := "0123456789abcdef"
|
|
if PkgRandomRaw(*random[0], 16) != 1 { return false }
|
|
for i: 0..15 {
|
|
hex[i * 2] = digits[cast(s32)(random[i] >> cast(u8)4)]
|
|
hex[i * 2 + 1] = digits[cast(s32)(random[i] & cast(u8)15)]
|
|
}
|
|
temporary := PkgJoin3(path, ".tmp.", TextFromBytes(hex[:]))
|
|
file := OpenExclusivePrivate(temporary)
|
|
if !file.valid { return false }
|
|
valid := WriteTextAt(file, content, 0) == cast(s64)content.count
|
|
if valid { valid = SyncFile(file) }
|
|
closed := CloseFile(file)
|
|
if valid && closed { valid = RenamePath(temporary, path) }
|
|
else { valid = false }
|
|
if !valid { unused RemoveFile(temporary) }
|
|
return valid
|
|
}
|
|
|
|
// Names a private, unpredictable sibling for a short-lived compiler stage.
|
|
// The producer still creates the file atomically; the caller removes it after
|
|
// the next stage, whether that stage succeeds or fails.
|
|
PkgTemporarySibling :: (path: string, extension: string) -> string {
|
|
random: [16]u8
|
|
hex: [32]u8
|
|
digits := "0123456789abcdef"
|
|
if PkgRandomRaw(*random[0], 16) != 1 { return "" }
|
|
for index: 0..15 {
|
|
hex[index * 2] = digits[cast(s32)(random[index] >> cast(u8)4)]
|
|
hex[index * 2 + 1] = digits[cast(s32)(random[index] & cast(u8)15)]
|
|
}
|
|
return PkgJoin3(PkgJoin3(path, ".tmp.", TextFromBytes(hex[:])), extension, "")
|
|
}
|
|
|
|
PkgMkdirs :: (path: string) -> bool {
|
|
if path.count == 0 || path.count > 4095 { return false }
|
|
index: s64 = 1
|
|
while index < cast(s64)path.count {
|
|
if path[index] == cast(u8)47 {
|
|
if !CreateDirectory(path[0:index]) { return false }
|
|
}
|
|
index += 1
|
|
}
|
|
return CreateDirectory(path)
|
|
}
|
|
|
|
PkgTrim :: (value: string) -> string {
|
|
start: s64 = 0
|
|
finish: s64 = cast(s64)value.count
|
|
while start < finish && (value[start] == cast(u8)32 ||
|
|
value[start] == cast(u8)9 || value[start] == cast(u8)10 ||
|
|
value[start] == cast(u8)13) { start += 1 }
|
|
while finish > start && (value[finish - 1] == cast(u8)32 ||
|
|
value[finish - 1] == cast(u8)9 || value[finish - 1] == cast(u8)10 ||
|
|
value[finish - 1] == cast(u8)13) { finish -= 1 }
|
|
return value[start:finish]
|
|
}
|
|
|
|
PkgFoldEqual :: (left: string, right: string) -> bool {
|
|
if left.count != right.count { return false }
|
|
index: s64 = 0
|
|
while index < cast(s64)left.count {
|
|
if LowerASCII(left[index]) != LowerASCII(right[index]) { return false }
|
|
index += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
PkgLexLess :: (left: string, right: string) -> bool {
|
|
index: s64 = 0
|
|
while index < cast(s64)left.count && index < cast(s64)right.count {
|
|
if left[index] < right[index] { return true }
|
|
if left[index] > right[index] { return false }
|
|
index += 1
|
|
}
|
|
return left.count < right.count
|
|
}
|
|
|
|
PkgEndsWith :: (value: string, suffix: string) -> bool {
|
|
if suffix.count > value.count { return false }
|
|
return value[cast(s64)value.count - cast(s64)suffix.count:] == suffix
|
|
}
|
|
|
|
PkgRun :: (arguments: []string, cwd: string, names: []string,
|
|
values: []string) -> s32 {
|
|
empty: [0]u8
|
|
code := RunProcess(arguments, cwd, names, values, empty[:])
|
|
if code == 127 { PkgFailCode("package.process", PkgJoin("command exited with status 127: ", arguments[0])) }
|
|
else if code < 0 { PkgFailCode("package.process", "cannot start command") }
|
|
return code
|
|
}
|
|
|
|
// An empty value means "unset" in RunProcess.
|
|
// Git writes trace output to stderr, which captures share with stdout. Clear
|
|
// every trace switch so an inherited GIT_TRACE cannot make a clean cache entry
|
|
// look modified or garble a ref listing.
|
|
PkgQuietGitEnv :: (names: []string, values: []string) -> s32 {
|
|
quiet: [12]string
|
|
quiet[0] = "GIT_TRACE"
|
|
quiet[1] = "GIT_TRACE2"
|
|
quiet[2] = "GIT_TRACE2_EVENT"
|
|
quiet[3] = "GIT_TRACE2_PERF"
|
|
quiet[4] = "GIT_TRACE_SETUP"
|
|
quiet[5] = "GIT_TRACE_PACKET"
|
|
quiet[6] = "GIT_TRACE_CURL"
|
|
quiet[7] = "GIT_TRACE_PERFORMANCE"
|
|
quiet[8] = "GIT_TRACE_PACK_ACCESS"
|
|
quiet[9] = "GIT_TRACE_SHALLOW"
|
|
quiet[10] = "GIT_TRACE_REFS"
|
|
quiet[11] = "GIT_CURL_VERBOSE"
|
|
index: s32 = 0
|
|
while index < 12 {
|
|
names[index] = quiet[index]
|
|
values[index] = ""
|
|
index += 1
|
|
}
|
|
return 12
|
|
}
|
|
|
|
PkgCaptureRaw :: (arguments: []string, cwd: string) -> PkgProcessResult {
|
|
result: PkgProcessResult
|
|
names: [12]string
|
|
values: [12]string
|
|
count: s32 = PkgQuietGitEnv(names[:], values[:])
|
|
memory: *u8 = PkgMallocRaw(1048576)
|
|
if memory == null { result.code = -1; return result }
|
|
output: []u8 = BytesFromPointer(memory, 1048576)
|
|
result.code = RunProcess(arguments, cwd, names[0:count], values[0:count], output)
|
|
result.text = TextFromCString(memory)
|
|
return result
|
|
}
|
|
|
|
PkgCaptureEnv :: (arguments: []string, cwd: string, names: []string,
|
|
values: []string) -> PkgProcessResult {
|
|
result: PkgProcessResult
|
|
memory: *u8 = PkgMallocRaw(1048576)
|
|
if memory == null { result.code = -1; return result }
|
|
output: []u8 = BytesFromPointer(memory, 1048576)
|
|
result.code = RunProcess(arguments, cwd, names, values, output)
|
|
result.text = PkgTrim(TextFromCString(memory))
|
|
return result
|
|
}
|
|
|
|
PkgCapture :: (arguments: []string, cwd: string) -> PkgProcessResult {
|
|
result: PkgProcessResult = PkgCaptureRaw(arguments, cwd)
|
|
result.text = PkgTrim(result.text)
|
|
return result
|
|
}
|
|
|
|
// Runs a build step whose log belongs on stderr, so a command's stdout
|
|
// carries only its result; `root=$(ziran pkg path ziran)` stays one line.
|
|
// The build runs apart from any make that started ziran: an inherited
|
|
// MAKEFLAGS would carry its variables, such as a sanitizer build's flags,
|
|
// and a jobserver the child cannot reach.
|
|
PkgRunToStderr :: (arguments: []string, cwd: string) -> s32 {
|
|
// make also exports command-line variables such as SANITIZE_FLAGS.
|
|
names: [4]string
|
|
values: [4]string
|
|
names[0] = "MAKEFLAGS"
|
|
names[1] = "MFLAGS"
|
|
names[2] = "MAKELEVEL"
|
|
names[3] = "SANITIZE_FLAGS"
|
|
values[0] = ""
|
|
values[1] = ""
|
|
values[2] = ""
|
|
values[3] = ""
|
|
result: PkgProcessResult = PkgCaptureEnv(arguments, cwd, names[:], values[:])
|
|
if Diagnostics.IsJson() {
|
|
if result.code != 0 { PkgFailCode("package.tool", result.text) }
|
|
} else { PkgPrint(result.text, true) }
|
|
return result.code
|
|
}
|
|
|
|
PkgPrint :: (value: string, error: bool) -> void {
|
|
if value.count == 0 { return }
|
|
fd: s32 = 1
|
|
if error { fd = 2 }
|
|
bytes: [4096]u8
|
|
index: s64 = 0
|
|
while index < cast(s64)value.count {
|
|
count: s64 = cast(s64)value.count - index
|
|
if count > 4096 { count = 4096 }
|
|
part: s64 = 0
|
|
while part < count {
|
|
bytes[part] = value[index + part]
|
|
part += 1
|
|
}
|
|
written: s64 = PkgWriteRaw(fd, *bytes[0], cast(u64)count)
|
|
if written <= 0 { return }
|
|
index += written
|
|
}
|
|
}
|
|
|
|
PkgJsonString :: (value: string) -> void { Diagnostics.Quote(value, false) }
|
|
|
|
PkgFail :: (message: string) -> s32 {
|
|
return PkgFailCode("package.command", message)
|
|
}
|
|
|
|
// Set once a specific failure is printed, so callers do not add a generic one.
|
|
pkg_failure_reported: bool;
|
|
|
|
PkgFailCode :: (code: string, message: string) -> s32 {
|
|
pkg_failure_reported = true
|
|
if Diagnostics.IsJson() {
|
|
Diagnostics.Emit(code, message, "")
|
|
} else {
|
|
PkgPrint("ziran: ", true)
|
|
PkgPrint(message, true)
|
|
PkgPrint("\n", true)
|
|
}
|
|
return 2
|
|
}
|
|
|
|
PkgShowToolFailure :: (output: string) {
|
|
if Diagnostics.IsJson() { PkgFailCode("package.tool", output) }
|
|
else { PkgPrint(output, true); PkgPrint("\n", true) }
|
|
}
|
|
|
|
PkgDigest :: (value: string) -> string {
|
|
digest: [32]u8
|
|
memory := PkgAlloc(cast(u64)value.count + 1)
|
|
if memory == null { return "" }
|
|
bytes := BytesFromPointer(memory, cast(u64)value.count + 1)
|
|
PkgCopy(bytes, 0, value)
|
|
hashed := PkgSha256Raw(*bytes[0], cast(u64)value.count, *digest[0]) != null
|
|
PkgFreeRaw(memory)
|
|
if !hashed { return "" }
|
|
alphabet := "0123456789abcdef"
|
|
output: [64]u8
|
|
for index: 0..31 {
|
|
output[index * 2] = alphabet[cast(s32)(digest[index] >> cast(u8)4)]
|
|
output[index * 2 + 1] = alphabet[cast(s32)(digest[index] & cast(u8)15)]
|
|
}
|
|
return PkgJoin("", TextFromBytes(output[:]))
|
|
}
|
|
|
|
PkgSourceId :: (url: string, commit: string) -> string {
|
|
input: string = PkgJoin3(url, "\n", commit)
|
|
digest: [32]u8
|
|
memory: *u8 = PkgAlloc(cast(u64)input.count)
|
|
if input.count == 0 || memory == null { return "" }
|
|
bytes: []u8 = BytesFromPointer(memory, cast(u64)input.count)
|
|
if PkgCopy(bytes, 0, input) < 0 || PkgSha256Raw(*bytes[0],
|
|
cast(u64)input.count, *digest[0]) == null { return "" }
|
|
alphabet: string = "0123456789abcdef"
|
|
result: [18]u8
|
|
result[0] = cast(u8)112
|
|
index: s32 = 0
|
|
while index < 8 {
|
|
result[index * 2 + 1] = alphabet[cast(s32)(digest[index] >> cast(u8)4)]
|
|
result[index * 2 + 2] = alphabet[cast(s32)(digest[index] & cast(u8)15)]
|
|
index += 1
|
|
}
|
|
return PkgJoin("", TextFromBytes(result[0:17]))
|
|
}
|
|
|
|
// Command-lifetime arena for variable-sized root and argument collections.
|
|
// Each request is bounded by the input size and checked byte arithmetic.
|
|
PkgStringBlock :: struct { next: *PkgStringBlock; data: *u8 }
|
|
PkgStringArena: *PkgStringBlock;
|
|
PkgStringSlice :: struct { data: *string; count: s64 }
|
|
PkgStrings :: (count: s64) -> []string {
|
|
result: []string
|
|
if count <= 0 || count > 9223372036854775807 / size_of(string) { return result }
|
|
block := cast(*PkgStringBlock)PkgAllocZero(cast(u64)size_of(PkgStringBlock))
|
|
if block == null { return result }
|
|
block.data = PkgAllocZero(cast(u64)(count * size_of(string)))
|
|
if block.data == null { PkgFreeRaw(cast(*u8)block); return result }
|
|
block.next = PkgStringArena
|
|
PkgStringArena = block
|
|
descriptor := cast(*PkgStringSlice)(*result)
|
|
descriptor.data = cast(*string)block.data
|
|
descriptor.count = count
|
|
return result
|
|
}
|
|
PkgReleaseStringArena :: () {
|
|
while PkgStringArena != null {
|
|
block := PkgStringArena
|
|
PkgStringArena = block.next
|
|
PkgFreeRaw(block.data)
|
|
PkgFreeRaw(cast(*u8)block)
|
|
}
|
|
}
|