dymokomi/luce-base
Source of release 0.46.0
0.46.0 / src / sema / standard_sources.lucb
#==============================================================================================
#
# standard_sources - The standard library carried inside the compiler
#
# DESCRIPTION:
# The standard modules of base.md §16.6 as raw text, each fragment as `src/std` holds
# it, for a checker embedded in another program (`embed.check_root`), which reads no
# standard library from disk. The command itself reads `src/std` (`standard`); this
# copy is written by `tools/embed_std.py`, and the gate fails when the two differ. Do
# not edit this file; edit `src/std` and rerun the tool.
#
#==============================================================================================
# mark: The modules ===========================================================================
## The standard modules in binding order, as `src/std/ORDER` lists them.
pub let modules: str[20] = ["platform", "c", "windows_abi", "float_text", "numerals", "core", "memory", "windows_text", "os", "io", "luce", "atomic", "time", "thread", "sync", "ownership", "strings", "testing", "interop", "debug"]
## Where each module's fragments begin in `paths` and `text`, by its index in `modules`;
## the last entry is the fragment count. `platform` has none: the checker writes it.
pub let firsts: u32[21] = [0, 0, 1, 2, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 22, 31, 32, 47, 48]
## Each fragment's path as positions name it: `std/core.lucb`, `std/strings/search.lucb`.
pub let paths: str[48] = ["std/c.lucb", "std/windows_abi.lucb", "std/float_text/module.lucb", "std/float_text/tables.lucb", "std/float_text/ryu.lucb", "std/numerals.lucb", "std/core.lucb", "std/memory.lucb", "std/windows_text.lucb", "std/os/module.lucb", "std/os/windows.lucb", "std/io.lucb", "std/luce.lucb", "std/atomic.lucb", "std/time.lucb", "std/thread.lucb", "std/sync.lucb", "std/ownership/module.lucb", "std/ownership/pool.lucb", "std/ownership/cycles.lucb", "std/ownership/weak.lucb", "std/ownership/lifecycle.lucb", "std/strings/module.lucb", "std/strings/search.lucb", "std/strings/split.lucb", "std/strings/transform.lucb", "std/strings/replace.lucb", "std/strings/numbers.lucb", "std/strings/float_tables.lucb", "std/strings/floats.lucb", "std/strings/formatting.lucb", "std/testing.lucb", "std/interop/module.lucb", "std/interop/object.lucb", "std/interop/reference.lucb", "std/interop/weak.lucb", "std/interop/lease.lucb", "std/interop/view.lucb", "std/interop/interface.lucb", "std/interop/result.lucb", "std/interop/callback.lucb", "std/interop/connection.lucb", "std/interop/signal.lucb", "std/interop/transfer.lucb", "std/interop/worker_entry.lucb", "std/interop/worker_queue.lucb", "std/interop/worker.lucb", "std/debug.lucb"]
# mark: The sources ===========================================================================
## The text of `src/std/c.lucb`.
let text_0: str = r"""#==============================================================================================
#
# c - C's types and errno
#
# DESCRIPTION:
# C's types live here (base.md §5.2); the checker supplies them, since their widths are
# the target's: `c.int` and the other fixed-width ones are Base types by another name;
# `c.char`, `c.long`, `c.ulong` and `c.wchar` are distinct; `c.va_list` is opaque; `c.str`
# is C text.
#
#==============================================================================================
import platform
extern func mac_errno as "__error"() -> i32*
extern func linux_errno as "__errno_location"() -> i32*
extern func windows_errno as "_errno"() -> i32*
## The calling thread's `errno`: the C library's account of its last failure.
pub func errno() -> i32:
if platform.windows:
return *windows_errno()
if platform.macos:
return *mac_errno()
return *linux_errno()
## Set the calling thread's errno. Clear it immediately before C calls such as
## readdir whose null result can mean either normal completion or failure.
pub func set_errno(value: i32):
if platform.windows:
*windows_errno() = value
elif platform.macos:
*mac_errno() = value
else:
*linux_errno() = value
## `EINTR`: a call a signal interrupted, to be made again. The same number on every host.
pub let interrupted: i32 = 4
## The C standard input stream; the backend supplies it.
pub func stdin() -> void*:
trap("supplied by the backend")
## The C standard output stream; the backend supplies it.
pub func stdout() -> void*:
trap("supplied by the backend")
## The C standard error stream; the backend supplies it.
pub func stderr() -> void*:
trap("supplied by the backend")
"""
## The text of `src/std/windows_abi.lucb`.
let text_1: str = r"""#==============================================================================================
#
# windows_abi - Internal shared Win32 ABI
#
# DESCRIPTION:
# Module-specific handles and records stay with their filesystem, process, input or
# graphics adapter. Public standard APIs never expose these declarations. This module
# cannot depend on allocation.
#
#==============================================================================================
import c
## The CP_UTF8 code page.
pub let utf8_page: u32 = 65001
## MB_ERR_INVALID_CHARS: fail on malformed UTF-8.
pub let reject_invalid_utf8: u32 = 8
## WC_ERR_INVALID_CHARS: fail on malformed UTF-16.
pub let reject_invalid_utf16: u32 = 128
## Win32 SYSTEM_INFO: the page size and processor count.
pub extern struct SystemInfo:
pub architecture: u16
pub reserved: u16
pub page_size: u32
pub minimum_address: void*
pub maximum_address: void*
pub active_processor_mask: usize
pub processor_count: u32
pub processor_type: u32
pub allocation_granularity: u32
pub processor_level: u16
pub processor_revision: u16
## Win32 OVERLAPPED. File locking uses only its zero state; the explicit layout
## keeps packages from duplicating an ABI-sensitive record.
pub extern struct Overlapped:
pub internal: usize
pub internal_high: usize
pub offset: u32
pub offset_high: u32
pub event: void*?
## Win32 MultiByteToWideChar: bytes to UTF-16.
pub extern func MultiByteToWideChar(page: u32, flags: u32, input: c.str, length: i32, output: u16*?, capacity: i32) -> i32
## Win32 WideCharToMultiByte: UTF-16 to bytes.
pub extern func WideCharToMultiByte(page: u32, flags: u32, input: const u16*, length: i32, output: u8*?, capacity: i32, fallback: void*?, used_fallback: void*?) -> i32
## Win32 GetLastError.
pub extern func GetLastError() -> u32
## Win32 SetLastError.
pub extern func SetLastError(error: u32)
## Win32 CloseHandle.
pub extern func CloseHandle(handle: void*) -> i32
## Win32 GetCurrentProcessId.
pub extern func GetCurrentProcessId() -> u32
## Win32 GetCurrentProcess.
pub extern func GetCurrentProcess() -> void*
## Win32 GetSystemInfo.
pub extern func GetSystemInfo(info: SystemInfo*)
## Win32 Sleep, in milliseconds.
pub extern func Sleep(milliseconds: u32)
## Win32 GetTempPathW.
pub extern func GetTempPathW(capacity: u32, output: u16*) -> u32
## Win32 GetModuleHandleW.
pub extern func GetModuleHandleW(name: u16*?) -> void*
## Win32 CreateFileW.
pub extern func CreateFileW(path: u16*, access: u32, share: u32, security: void*?, disposition: u32, flags: u32, template: void*?) -> void*
## Win32 LockFileEx.
pub extern func LockFileEx(handle: void*, flags: u32, reserved: u32, low: u32, high: u32, overlapped: Overlapped*) -> i32
## Win32 GetEnvironmentVariableW.
pub extern func GetEnvironmentVariableW(name: u16*, output: u16*, capacity: u32) -> u32
## Win32 SetEnvironmentVariableW.
pub extern func SetEnvironmentVariableW(name: u16*, value: u16*?) -> i32
## Win32 GetCurrentDirectoryW.
pub extern func GetCurrentDirectoryW(capacity: u32, output: u16*?) -> u32
## Win32 SetCurrentDirectoryW.
pub extern func SetCurrentDirectoryW(path: u16*) -> i32
"""
## The text of `src/std/float_text/module.lucb`.
let text_2: str = r"""#==============================================================================================
#
# float_text - The shortest decimal text of a float
#
# DESCRIPTION:
# The fewest significant digits that read back as the value, of those the closest
# (Ryu: Adams, "Ryu: Fast Float-to-String Conversion", PLDI 2018), each width with its
# own digits, laid out as double-conversion's ToShortest: decimal notation for a decimal
# exponent from -6 to 14, otherwise one digit, the rest after a point, and `e` with the
# exponent. Integer arithmetic only. strings' format_f64 writes this text, and a
# formatted string's float field this text with a `.0` after digits that have no point
# (base.md §14). It comes before `core` and needs nothing else.
#
#==============================================================================================
# mark: Text ===================================================================================
## The shortest text of `value` into `text`, at least 32 bytes: `0.1`, `1e15`, `5e-324`,
## `-0`, `inf`, `-inf`, `nan`. Its length.
pub func write_f64(value: f64, text: u8[]) -> usize:
let bits = value.bits()
let negative = (bits >> 63) != 0
let exponent_bits = (u32)((bits >> 52) & 0x7FF)
let mantissa_bits = bits & 0xFFFFFFFFFFFFF
if exponent_bits == 0x7FF:
return special(text, negative, mantissa_bits != 0)
if exponent_bits == 0 and mantissa_bits == 0:
return copy_word(text, "-0" if negative else "0")
let (digits, exponent) = shortest64(mantissa_bits, exponent_bits)
return layout(text, negative, digits, exponent)
## The f32 counterpart of write_f64: the f32's own shortest digits (`0.1` for 0.1f32).
pub func write_f32(value: f32, text: u8[]) -> usize:
let bits = value.bits()
let negative = (bits >> 31) != 0
let exponent_bits = (bits >> 23) & 0xFF
let mantissa_bits = bits & 0x7FFFFF
if exponent_bits == 0xFF:
return special(text, negative, mantissa_bits != 0)
if exponent_bits == 0 and mantissa_bits == 0:
return copy_word(text, "-0" if negative else "0")
let (digits, exponent) = shortest32(mantissa_bits, exponent_bits)
return layout(text, negative, (u64)digits, exponent)
## A formatted string's field of `value` (base.md §14): write_f64's text, with `.0` after
## digits that have no point or exponent (`5.0`, `-0.0`, `123456789012345.0`). Its length.
pub func display_f64(value: f64, text: u8[]) -> usize:
return with_point(text, write_f64(value, text))
## display_f64 for an f32: its own shortest digits.
pub func display_f32(value: f32, text: u8[]) -> usize:
return with_point(text, write_f32(value, text))
func with_point(text: u8[], length: usize) -> usize:
var at: usize = 0
while at < length:
let byte = text[at]
if byte == '.' or byte == 'e' or byte == 'i' or byte == 'n':
return length
at += 1
text[length] = '.'
text[length + 1] = '0'
return length + 2
func special(text: u8[], negative: bool, nan: bool) -> usize:
if nan:
return copy_word(text, "nan")
return copy_word(text, "-inf" if negative else "inf")
func copy_word(text: u8[], word: str) -> usize:
var at: usize = 0
while at < word.length:
text[at] = word.bytes[at]
at += 1
return word.length
## ToShortest's layout of `digits * 10^exponent` into `text`; its length.
func layout(text: u8[], negative: bool, digits: u64, exponent: i32) -> usize:
var reversed: u8[20] = ---
var count: usize = 0
var rest = digits
while rest > 0:
reversed[count] = (u8)(48 + rest % 10)
count += 1
rest = rest // 10
let point = (i64)exponent + (i64)count
let decimal_exponent = point - 1
var at: usize = 0
if negative:
text[0] = '-'
at = 1
if decimal_exponent >= -6 and decimal_exponent < 15:
if point <= 0:
text[at] = '0'
text[at + 1] = '.'
at += 2
for _ in 0..<(usize)(-point):
text[at] = '0'
at += 1
for index in 0..<count:
text[at] = reversed[count - 1 - index]
at += 1
elif (usize)point >= count:
for index in 0..<count:
text[at] = reversed[count - 1 - index]
at += 1
for _ in 0..<((usize)point - count):
text[at] = '0'
at += 1
else:
for index in 0..<(usize)point:
text[at] = reversed[count - 1 - index]
at += 1
text[at] = '.'
at += 1
for index in (usize)point..<count:
text[at] = reversed[count - 1 - index]
at += 1
return at
text[at] = reversed[count - 1]
at += 1
if count > 1:
text[at] = '.'
at += 1
for index in 1..<count:
text[at] = reversed[count - 1 - index]
at += 1
text[at] = 'e'
at += 1
var power = decimal_exponent
if power < 0:
text[at] = '-'
at += 1
power = -power
var power_digits: u8[8] = ---
var width: usize = 0
while true:
power_digits[width] = (u8)(48 + power % 10)
width += 1
power = power // 10
if power == 0:
break
while width > 0:
width -= 1
text[at] = power_digits[width]
at += 1
return at
"""
## The text of `src/std/float_text/tables.lucb`.
let text_3: str = r"""#==============================================================================================
#
# tables - Ryu's powers of five, for float_text
#
# DESCRIPTION:
# Written by tools/float_tables.py; do not edit.
#
#==============================================================================================
# mark: Powers of five =========================================================================
## 5^i for i in 0..<326 to 125 bits: the low word, then the high word.
let pow5_split: u64[652] = [
0x0000000000000000, 0x1000000000000000, 0x0000000000000000, 0x1400000000000000,
0x0000000000000000, 0x1900000000000000, 0x0000000000000000, 0x1F40000000000000,
0x0000000000000000, 0x1388000000000000, 0x0000000000000000, 0x186A000000000000,
0x0000000000000000, 0x1E84800000000000, 0x0000000000000000, 0x1312D00000000000,
0x0000000000000000, 0x17D7840000000000, 0x0000000000000000, 0x1DCD650000000000,
0x0000000000000000, 0x12A05F2000000000, 0x0000000000000000, 0x174876E800000000,
0x0000000000000000, 0x1D1A94A200000000, 0x0000000000000000, 0x12309CE540000000,
0x0000000000000000, 0x16BCC41E90000000, 0x0000000000000000, 0x1C6BF52634000000,
0x0000000000000000, 0x11C37937E0800000, 0x0000000000000000, 0x16345785D8A00000,
0x0000000000000000, 0x1BC16D674EC80000, 0x0000000000000000, 0x1158E460913D0000,
0x0000000000000000, 0x15AF1D78B58C4000, 0x0000000000000000, 0x1B1AE4D6E2EF5000,
0x0000000000000000, 0x10F0CF064DD59200, 0x0000000000000000, 0x152D02C7E14AF680,
0x0000000000000000, 0x1A784379D99DB420, 0x0000000000000000, 0x108B2A2C28029094,
0x0000000000000000, 0x14ADF4B7320334B9, 0x4000000000000000, 0x19D971E4FE8401E7,
0x8800000000000000, 0x1027E72F1F128130, 0xAA00000000000000, 0x1431E0FAE6D7217C,
0xD480000000000000, 0x193E5939A08CE9DB, 0xC9A0000000000000, 0x1F8DEF8808B02452,
0xBE04000000000000, 0x13B8B5B5056E16B3, 0xAD85000000000000, 0x18A6E32246C99C60,
0xD8E6400000000000, 0x1ED09BEAD87C0378, 0x878FE80000000000, 0x13426172C74D822B,
0x6973E20000000000, 0x1812F9CF7920E2B6, 0x03D0DA8000000000, 0x1E17B84357691B64,
0x8262889000000000, 0x12CED32A16A1B11E, 0x22FB2AB400000000, 0x178287F49C4A1D66,
0xABB9F56100000000, 0x1D6329F1C35CA4BF, 0xCB54395CA0000000, 0x125DFA371A19E6F7,
0xBE2947B3C8000000, 0x16F578C4E0A060B5, 0x2DB399A0BA000000, 0x1CB2D6F618C878E3,
0xFC90400474400000, 0x11EFC659CF7D4B8D, 0x7BB4500591500000, 0x166BB7F0435C9E71,
0xDAA16406F5A40000, 0x1C06A5EC5433C60D, 0xA8A4DE8459868000, 0x118427B3B4A05BC8,
0xD2CE16256FE82000, 0x15E531A0A1C872BA, 0x87819BAECBE22800, 0x1B5E7E08CA3A8F69,
0xF4B1014D3F6D5900, 0x111B0EC57E6499A1, 0x71DD41A08F48AF40, 0x1561D276DDFDC00A,
0x0E549208B31ADB10, 0x1ABA4714957D300D, 0x28F4DB456FF0C8EA, 0x10B46C6CDD6E3E08,
0x33321216CBECFB24, 0x14E1878814C9CD8A, 0xBFFE969C7EE839ED, 0x1A19E96A19FC40EC,
0xF7FF1E21CF512434, 0x105031E2503DA893, 0xF5FEE5AA43256D41, 0x14643E5AE44D12B8,
0x337E9F14D3EEC892, 0x197D4DF19D605767, 0x005E46DA08EA7AB6, 0x1FDCA16E04B86D41,
0xA03AEC4845928CB2, 0x13E9E4E4C2F34448, 0xC849A75A56F72FDE, 0x18E45E1DF3B0155A,
0x7A5C1130ECB4FBD6, 0x1F1D75A5709C1AB1, 0xEC798ABE93F11D65, 0x13726987666190AE,
0xA797ED6E38ED64BF, 0x184F03E93FF9F4DA, 0x517DE8C9C728BDEF, 0x1E62C4E38FF87211,
0xD2EEB17E1C7976B5, 0x12FDBB0E39FB474A, 0x87AA5DDDA397D462, 0x17BD29D1C87A191D,
0xE994F5550C7DC97B, 0x1DAC74463A989F64, 0x11FD195527CE9DED, 0x128BC8ABE49F639F,
0xD67C5FAA71C24568, 0x172EBAD6DDC73C86, 0x8C1B77950E32D6C2, 0x1CFA698C95390BA8,
0x57912ABD28DFC639, 0x121C81F7DD43A749, 0xAD75756C7317B7C8, 0x16A3A275D494911B,
0x98D2D2C78FDDA5BA, 0x1C4C8B1349B9B562, 0x9F83C3BCB9EA8794, 0x11AFD6EC0E14115D,
0x0764B4ABE8652979, 0x161BCCA7119915B5, 0x493DE1D6E27E73D7, 0x1BA2BFD0D5FF5B22,
0x6DC6AD264D8F0866, 0x1145B7E285BF98F5, 0xC938586FE0F2CA80, 0x159725DB272F7F32,
0x7B866E8BD92F7D20, 0x1AFCEF51F0FB5EFF, 0xAD34051767BDAE34, 0x10DE1593369D1B5F,
0x9881065D41AD19C1, 0x15159AF804446237, 0x7EA147F492186032, 0x1A5B01B605557AC5,
0x6F24CCF8DB4F3C1F, 0x1078E111C3556CBB, 0x4AEE003712230B27, 0x14971956342AC7EA,
0xDDA98044D6ABCDF0, 0x19BCDFABC13579E4, 0x0A89F02B062B60B6, 0x10160BCB58C16C2F,
0xCD2C6C35C7B638E4, 0x141B8EBE2EF1C73A, 0x8077874339A3C71D, 0x1922726DBAAE3909,
0xE0956914080CB8E4, 0x1F6B0F092959C74B, 0x6C5D61AC8507F38E, 0x13A2E965B9D81C8F,
0x4774BA17A649F072, 0x188BA3BF284E23B3, 0x1951E89D8FDC6C8F, 0x1EAE8CAEF261ACA0,
0x0FD3316279E9C3D9, 0x132D17ED577D0BE4, 0x13C7FDBB186434CF, 0x17F85DE8AD5C4EDD,
0x58B9FD29DE7D4203, 0x1DF67562D8B36294, 0xB7743E3A2B0E4942, 0x12BA095DC7701D9C,
0xE5514DC8B5D1DB92, 0x17688BB5394C2503, 0xDEA5A13AE3465277, 0x1D42AEA2879F2E44,
0x0B2784C4CE0BF38A, 0x1249AD2594C37CEB, 0xCDF165F6018EF06D, 0x16DC186EF9F45C25,
0x416DBF7381F2AC88, 0x1C931E8AB871732F, 0x88E497A83137ABD5, 0x11DBF316B346E7FD,
0xEB1DBD923D8596CA, 0x1652EFDC6018A1FC, 0x25E52CF6CCE6FC7D, 0x1BE7ABD3781ECA7C,
0x97AF3C1A40105DCE, 0x1170CB642B133E8D, 0xFD9B0B20D0147542, 0x15CCFE3D35D80E30,
0x3D01CDE904199292, 0x1B403DCC834E11BD, 0x462120B1A28FFB9B, 0x1108269FD210CB16,
0xD7A968DE0B33FA82, 0x154A3047C694FDDB, 0xCD93C3158E00F923, 0x1A9CBC59B83A3D52,
0xC07C59ED78C09BB6, 0x10A1F5B813246653, 0xB09B7068D6F0C2A3, 0x14CA732617ED7FE8,
0xDCC24C830CACF34C, 0x19FD0FEF9DE8DFE2, 0xC9F96FD1E7EC180F, 0x103E29F5C2B18BED,
0x3C77CBC661E71E13, 0x144DB473335DEEE9, 0x8B95BEB7FA60E598, 0x1961219000356AA3,
0x6E7B2E65F8F91EFE, 0x1FB969F40042C54C, 0xC50CFCFFBB9BB35F, 0x13D3E2388029BB4F,
0xB6503C3FAA82A037, 0x18C8DAC6A0342A23, 0xA3E44B4F95234844, 0x1EFB1178484134AC,
0xE66EAF11BD360D2B, 0x135CEAEB2D28C0EB, 0xE00A5AD62C839075, 0x183425A5F872F126,
0x980CF18BB7A47493, 0x1E412F0F768FAD70, 0x5F0816F752C6C8DC, 0x12E8BD69AA19CC66,
0xF6CA1CB527787B13, 0x17A2ECC414A03F7F, 0xF47CA3E2715699D7, 0x1D8BA7F519C84F5F,
0xF8CDE66D86D62026, 0x127748F9301D319B, 0xF7016008E88BA830, 0x17151B377C247E02,
0xB4C1B80B22AE923C, 0x1CDA62055B2D9D83, 0x50F91306F5AD1B65, 0x12087D4358FC8272,
0xE53757C8B318623F, 0x168A9C942F3BA30E, 0x9E852DBADFDE7ACF, 0x1C2D43B93B0A8BD2,
0xA3133C94CBEB0CC1, 0x119C4A53C4E69763, 0x8BD80BB9FEE5CFF1, 0x16035CE8B6203D3C,
0xAECE0EA87E9F43EE, 0x1B843422E3A84C8B, 0x4D40C9294F238A75, 0x1132A095CE492FD7,
0x2090FB73A2EC6D12, 0x157F48BB41DB7BCD, 0x68B53A508BA78856, 0x1ADF1AEA12525AC0,
0x417144725748B536, 0x10CB70D24B7378B8, 0x51CD958EED1AE283, 0x14FE4D06DE5056E6,
0xE640FAF2A8619B24, 0x1A3DE04895E46C9F, 0xEFE89CD7A93D00F7, 0x1066AC2D5DAEC3E3,
0xEBE2C40D938C4134, 0x14805738B51A74DC, 0x26DB7510F86F5181, 0x19A06D06E2611214,
0x9849292A9B4592F1, 0x100444244D7CAB4C, 0xBE5B73754216F7AD, 0x1405552D60DBD61F,
0xADF25052929CB598, 0x1906AA78B912CBA7, 0x996EE4673743E2FF, 0x1F485516E7577E91,
0xFFE54EC0828A6DDF, 0x138D352E5096AF1A, 0xBFDEA270A32D0957, 0x18708279E4BC5AE1,
0x2FD64B0CCBF84BAD, 0x1E8CA3185DEB719A, 0x5DE5EEE7FF7B2F4C, 0x1317E5EF3AB32700,
0x755F6AA1FF59FB1F, 0x17DDDF6B095FF0C0, 0x92B7454A7F3079E7, 0x1DD55745CBB7ECF0,
0x5BB28B4E8F7E4C30, 0x12A5568B9F52F416, 0xF29F2E22335DDF3C, 0x174EAC2E8727B11B,
0xEF46F9AAC035570B, 0x1D22573A28F19D62, 0xD58C5C0AB8215667, 0x123576845997025D,
0x4AEF730D6629AC01, 0x16C2D4256FFCC2F5, 0x9DAB4FD0BFB41701, 0x1C73892ECBFBF3B2,
0xA28B11E277D08E60, 0x11C835BD3F7D784F, 0x8B2DD65B15C4B1F9, 0x163A432C8F5CD663,
0x6DF94BF1DB35DE77, 0x1BC8D3F7B3340BFC, 0xC4BBCF772901AB0A, 0x115D847AD000877D,
0x35EAC354F34215CD, 0x15B4E5998400A95D, 0x8365742A30129B40, 0x1B221EFFE500D3B4,
0xD21F689A5E0BA108, 0x10F5535FEF208450, 0x06A742C0F58E894A, 0x1532A837EAE8A565,
0x4851137132F22B9D, 0x1A7F5245E5A2CEBE, 0xED32AC26BFD75B42, 0x108F936BAF85C136,
0xA87F57306FCD3212, 0x14B378469B673184, 0xD29F2CFC8BC07E97, 0x19E056584240FDE5,
0xA3A37C1DD7584F1E, 0x102C35F729689EAF, 0x8C8C5B254D2E62E6, 0x14374374F3C2C65B,
0x6FAF71EEA079FB9F, 0x1945145230B377F2, 0x0B9B4E6A48987A87, 0x1F965966BCE055EF,
0x674111026D5F4C94, 0x13BDF7E0360C35B5, 0xC111554308B71FBA, 0x18AD75D8438F4322,
0x7155AA93CAE4E7A8, 0x1ED8D34E547313EB, 0x26D58A9C5ECF10C9, 0x13478410F4C7EC73,
0xF08AED437682D4FB, 0x1819651531F9E78F, 0xECADA89454238A3A, 0x1E1FBE5A7E786173,
0x73EC895CB4963664, 0x12D3D6F88F0B3CE8, 0x90E7ABB3E1BBC3FD, 0x1788CCB6B2CE0C22,
0x352196A0DA2AB4FD, 0x1D6AFFE45F818F2B, 0x0134FE24885AB11E, 0x1262DFEEBBB0F97B,
0xC1823DADAA715D65, 0x16FB97EA6A9D37D9, 0x31E2CD19150DB4BF, 0x1CBA7DE5054485D0,
0x1F2DC02FAD2890F7, 0x11F48EAF234AD3A2, 0xA6F9303B9872B535, 0x1671B25AEC1D888A,
0x50B77C4A7E8F6282, 0x1C0E1EF1A724EAAD, 0x5272ADAE8F199D91, 0x1188D357087712AC,
0x670F591A32E004F6, 0x15EB082CCA94D757, 0x40D32F60BF980633, 0x1B65CA37FD3A0D2D,
0x4883FD9C77BF03E0, 0x111F9E62FE44483C, 0x5AA4FD0395AEC4D8, 0x156785FBBDD55A4B,
0x314E3C447B1A760E, 0x1AC1677AAD4AB0DE, 0xDED0E5AACCF089C9, 0x10B8E0ACAC4EAE8A,
0x96851F15802CAC3B, 0x14E718D7D7625A2D, 0xFC2666DAE037D74A, 0x1A20DF0DCD3AF0B8,
0x9D980048CC22E68E, 0x10548B68A044D673, 0x84FE005AFF2BA032, 0x1469AE42C8560C10,
0xA63D8071BEF6883E, 0x198419D37A6B8F14, 0xCFCCE08E2EB42A4E, 0x1FE52048590672D9,
0x21E00C58DD309A70, 0x13EF342D37A407C8, 0x2A580F6F147CC10D, 0x18EB0138858D09BA,
0xB4EE134AD99BF150, 0x1F25C186A6F04C28, 0x7114CC0EC80176D2, 0x137798F428562F99,
0xCD59FF127A01D486, 0x18557F31326BBB7F, 0xC0B07ED7188249A8, 0x1E6ADEFD7F06AA5F,
0xD86E4F466F516E09, 0x1302CB5E6F642A7B, 0xCE89E3180B25C98B, 0x17C37E360B3D351A,
0x822C5BDE0DEF3BEE, 0x1DB45DC38E0C8261, 0xF15BB96AC8B58575, 0x1290BA9A38C7D17C,
0x2DB2A7C57AE2E6D2, 0x1734E940C6F9C5DC, 0x391F51B6D99BA086, 0x1D022390F8B83753,
0x03B3931248014454, 0x1221563A9B732294, 0x04A077D6DA019569, 0x16A9ABC9424FEB39,
0x45C895CC9081FAC3, 0x1C5416BB92E3E607, 0x8B9D5D9FDA513CBA, 0x11B48E353BCE6FC4,
0xAE84B507D0E58BE8, 0x1621B1C28AC20BB5, 0x1A25E249C51EEEE3, 0x1BAA1E332D728EA3,
0xF057AD6E1B33554D, 0x114A52DFFC679925, 0x6C6D98C9A2002AA1, 0x159CE797FB817F6F,
0x4788FEFC0A803549, 0x1B04217DFA61DF4B, 0x0CB59F5D8690214E, 0x10E294EEBC7D2B8F,
0xCFE30734E83429A1, 0x151B3A2A6B9C7672, 0x83DBC9022241340A, 0x1A6208B50683940F,
0xB2695DA15568C086, 0x107D457124123C89, 0x1F03B509AAC2F0A7, 0x149C96CD6D16CBAC,
0x26C4A24C1573ACD1, 0x19C3BC80C85C7E97, 0x783AE56F8D684C03, 0x101A55D07D39CF1E,
0x16499ECB70C25F03, 0x1420EB449C8842E6, 0x9BDC067E4CF2F6C4, 0x19292615C3AA539F,
0x82D3081DE02FB476, 0x1F736F9B3494E887, 0xB1C3E512AC1DD0C9, 0x13A825C100DD1154,
0xDE34DE57572544FC, 0x18922F31411455A9, 0x55C215ED2CEE963B, 0x1EB6BAFD91596B14,
0xB5994DB43C151DE5, 0x133234DE7AD7E2EC, 0xE2FFA1214B1A655E, 0x17FEC216198DDBA7,
0xDBBF89699DE0FEB6, 0x1DFE729B9FF15291, 0x2957B5E202AC9F31, 0x12BF07A143F6D39B,
0xF3ADA35A8357C6FE, 0x176EC98994F48881, 0x70990C31242DB8BD, 0x1D4A7BEBFA31AAA2,
0x865FA79EB69C9376, 0x124E8D737C5F0AA5, 0xE7F791866443B854, 0x16E230D05B76CD4E,
0xA1F575E7FD54A669, 0x1C9ABD04725480A2, 0xA53969B0FE54E801, 0x11E0B622C774D065,
0x0E87C41D3DEA2202, 0x1658E3AB7952047F, 0xD229B5248D64AA82, 0x1BEF1C9657A6859E,
0x435A1136D85EEA91, 0x117571DDF6C81383, 0x143095848E76A536, 0x15D2CE55747A1864,
0x193CBAE5B2144E83, 0x1B4781EAD1989E7D, 0x2FC5F4CF8F4CB112, 0x110CB132C2FF630E,
0xBBB77203731FDD56, 0x154FDD7F73BF3BD1, 0x2AA54E844FE7D4AC, 0x1AA3D4DF50AF0AC6,
0xDAA75112B1F0E4EB, 0x10A6650B926D66BB, 0xD15125575E6D1E26, 0x14CFFE4E7708C06A,
0x85A56EAD360865B0, 0x1A03FDE214CAF085, 0x7387652C41C53F8E, 0x10427EAD4CFED653,
0x50693E7752368F71, 0x14531E58A03E8BE8, 0x64838E1526C4334E, 0x1967E5EEC84E2EE2,
0xFDA4719A70754022, 0x1FC1DF6A7A61BA9A, 0xDE86C70086494815, 0x13D92BA28C7D14A0,
0x162878C0A7DB9A1A, 0x18CF768B2F9C59C9, 0x5BB296F0D1D280A1, 0x1F03542DFB83703B,
0x194F9E5683239064, 0x1362149CBD322625, 0x5FA385EC23EC747E, 0x183A99C3EC7EAFAE,
0xF78C67672CE7919D, 0x1E494034E79E5B99, 0x3AB7C0A07C10BB02, 0x12EDC82110C2F940,
0x4965B0C89B14E9C3, 0x17A93A2954F3B790, 0x5BBF1CFAC1DA2433, 0x1D9388B3AA30A574,
0xB957721CB92856A0, 0x127C35704A5E6768, 0xE7AD4EA3E7726C48, 0x171B42CC5CF60142,
0xA198A24CE14F075A, 0x1CE2137F74338193, 0x44FF65700CD16498, 0x120D4C2FA8A030FC,
0x563F3ECC1005BDBE, 0x16909F3B92C83D3B, 0x2BCF0E7F14072D2E, 0x1C34C70A777A4C8A,
0x5B61690F6C847C3D, 0x11A0FC668AAC6FD6, 0xF239C35347A59B4C, 0x16093B802D578BCB,
0xEEC83428198F021F, 0x1B8B8A6038AD6EBE, 0x553D20990FF96153, 0x1137367C236C6537,
0x2A8C68BF53F7B9A8, 0x1585041B2C477E85, 0x752F82EF28F5A812, 0x1AE64521F7595E26,
0x093DB1D57999890B, 0x10CFEB353A97DAD8, 0x0B8D1E4AD7FFEB4E, 0x1503E602893DD18E,
0x8E7065DD8DFFE622, 0x1A44DF832B8D45F1, 0xF9063FAA78BFEFD5, 0x106B0BB1FB384BB6,
0xB747CF9516EFEBCA, 0x1485CE9E7A065EA4, 0xE519C37A5CABE6BD, 0x19A742461887F64D,
0xAF301A2C79EB7036, 0x1008896BCF54F9F0, 0xDAFC20B798664C43, 0x140AABC6C32A386C,
0x11BB28E57E7FDF54, 0x190D56B873F4C688, 0x1629F31EDE1FD72A, 0x1F50AC6690F1F82A,
0x4DDA37F34AD3E67A, 0x13926BC01A973B1A, 0xE150C5F01D88E019, 0x187706B0213D09E0,
0x19A4F76C24EB181F, 0x1E94C85C298C4C59, 0xB0071AA39712EF13, 0x131CFD3999F7AFB7,
0x9C08E14C7CD7AAD8, 0x17E43C8800759BA5, 0x030B199F9C0D958E, 0x1DDD4BAA0093028F,
0x61E6F003C1887D79, 0x12AA4F4A405BE199, 0xBA60AC04B1EA9CD7, 0x1754E31CD072D9FF,
0xA8F8D705DE65440D, 0x1D2A1BE4048F907F, 0xC99B8663AAFF4A88, 0x123A516E82D9BA4F,
0xBC0267FC95BF1D2A, 0x16C8E5CA239028E3, 0xAB0301FBBB2EE474, 0x1C7B1F3CAC74331C,
0xEAE1E13D54FD4EC9, 0x11CCF385EBC89FF1, 0x659A598CAA3CA27B, 0x1640306766BAC7EE,
0xFF00EFEFD4CBCB1A, 0x1BD03C81406979E9, 0x3F6095F5E4FF5EF0, 0x116225D0C841EC32,
0xCF38BB735E3F36AC, 0x15BAAF44FA52673E, 0x8306EA5035CF0457, 0x1B295B1638E7010E,
0x11E4527221A162B6, 0x10F9D8EDE39060A9, 0x565D670EAA09BB64, 0x15384F295C7478D3,
0x2BF4C0D2548C2A3D, 0x1A8662F3B3919708, 0x1B78F88374D79A66, 0x1093FDD8503AFE65,
0x625736A4520D8100, 0x14B8FD4E6449BDFE, 0xFAED044D6690E140, 0x19E73CA1FD5C2D7D,
0xBCD422B0601A8CC8, 0x103085E53E599C6E, 0x6C092B5C78212FFA, 0x143CA75E8DF0038A,
0x070B763396297BF8, 0x194BD136316C046D, 0x48CE53C07BB3DAF6, 0x1F9EC583BDC70588,
0x2D80F4584D5068DA, 0x13C33B72569C6375, 0x78E1316E60A48310, 0x18B40A4EEC437C52,
]
## 2^(bits(5^i) - 1 + 125) / 5^i + 1 for i in 0..<342: the low word, then the high word.
let pow5_inv_split: u64[684] = [
0x0000000000000001, 0x2000000000000000, 0x999999999999999A, 0x1999999999999999,
0x47AE147AE147AE15, 0x147AE147AE147AE1, 0x6C8B4395810624DE, 0x10624DD2F1A9FBE7,
0x7A786C226809D496, 0x1A36E2EB1C432CA5, 0x61F9F01B866E43AB, 0x14F8B588E368F084,
0xB4C7F34938583622, 0x10C6F7A0B5ED8D36, 0x87A6520EC08D236A, 0x1AD7F29ABCAF4857,
0x9FB841A566D74F88, 0x15798EE2308C39DF, 0xE62D01511F12A607, 0x112E0BE826D694B2,
0xD6AE6881CB5109A4, 0x1B7CDFD9D7BDBAB7, 0xDEF1ED34A2A73AEA, 0x15FD7FE17964955F,
0x7F27F0F6E885C8BB, 0x119799812DEA1119, 0x650CB4BE40D60DF8, 0x1C25C268497681C2,
0xEA70909833DE7193, 0x16849B86A12B9B01, 0x21F3A6E0297EC143, 0x1203AF9EE756159B,
0x6985D7CD0F313537, 0x1CD2B297D889BC2B, 0x2137DFD73F5A90F9, 0x170EF54646D49689,
0xE75FE645CC4873FA, 0x12725DD1D243ABA0, 0xA5663D3C7A0D865D, 0x1D83C94FB6D2AC34,
0x511E976394D79EB1, 0x179CA10C9242235D, 0xDA7EDF82DD794BC1, 0x12E3B40A0E9B4F7D,
0x2A6498D1625BAC68, 0x1E392010175EE596, 0xEEB6E0A781E2F053, 0x182DB34012B25144,
0x58924D52CE4F26A9, 0x1357C299A88EA76A, 0x27507BB7B07EA441, 0x1EF2D0F5DA7DD8AA,
0x52A6C95FC0655034, 0x18C240C4AECB13BB, 0x0EEBD44C99EAA690, 0x13CE9A36F23C0FC9,
0xB17953ADC3110A80, 0x1FB0F6BE50601941, 0xC12DDC8B02740867, 0x195A5EFEA6B34767,
0x3424B06F3529A052, 0x14484BFEEBC29F86, 0x901D59F290EE19DB, 0x1039D66589687F9E,
0x4CFBC31DB4B0295F, 0x19F623D5A8A73297, 0x3D9635B15D59BAB2, 0x14C4E977BA1F5BAC,
0x97AB5E277DE16228, 0x109D8792FB4C4956, 0xF2ABC9D8C9689D0D, 0x1A95A5B7F87A0EF0,
0x5BBCA17A3ABA173E, 0x154484932D2E725A, 0xAFCA1AC82EFB45CB, 0x11039D428A8B8EAE,
0xB2DCF7A6B1920945, 0x1B38FB9DAA78E44A, 0xF57D92EBC141A104, 0x15C72FB1552D836E,
0xC46475896767B403, 0x116C262777579C58, 0x6D6D88DBD8A5ECD2, 0x1BE03D0BF225C6F4,
0x8ABE071646EB23DB, 0x164CFDA3281E38C3, 0x6EFE6C11D255B649, 0x11D7314F534B609C,
0xB197134FB6EF8A0E, 0x1C8B821885456760, 0x27AC0F72F8BFA1A5, 0x16D601AD376AB91A,
0xB95672C260994E1E, 0x1244CE242C5560E1, 0xF5571E03CDC21695, 0x1D3AE36D13BBCE35,
0x2AAC18030B01ABAB, 0x17624F8A762FD82B, 0xBBBCE0026F348956, 0x12B50C6EC4F31355,
0x92C7CCD0B1EDA889, 0x1DEE7A4AD4B81EEF, 0xDBD30A408E57BA07, 0x17F1FB6F10934BF2,
0x7CA8D50071DFC806, 0x1327FC58DA0F6FF5, 0xFAA7BB33E9660CD6, 0x1EA6608E29B24CBB,
0x9552FC298784D711, 0x18851A0B548EA3C9, 0xAAA8C9BAD2D0AC0E, 0x139DAE6F76D88307,
0xDDDADC5E1E1AACE3, 0x1F62B0B257C0D1A5, 0x7E48B04B4B488A4F, 0x191BC08EAC9A4151,
0xCB6D59D5D5D3A1D9, 0x141633A556E1CDDA, 0x3C577B1177DC817B, 0x1011C2EAABE7D7E2,
0xC6F25E825960CF2A, 0x19B604AAACA62636, 0x6BF518684780A5BB, 0x14919D5556EB51C5,
0x232A79ED06008496, 0x10747DDDDF22A7D1, 0xD1DD8FE1A3340756, 0x1A53FC9631D10C81,
0xA7E4731AE8F66C45, 0x150FFD44F4A73D34, 0x531D28E253F8569E, 0x10D9976A5D52975D,
0xEB61DB03B98D5762, 0x1AF5BF109550F22E, 0xBC4E48CFC7A445E8, 0x159165A6DDDA5B58,
0x6371D3D96C836B20, 0x11411E1F17E1E2AD, 0x9F1C8628AD9F11CD, 0x1B9B6364F3030448,
0xE5B06B53BE18DB0B, 0x1615E91D8F359D06, 0xEAF3890FCB4715A2, 0x11AB20E472914A6B,
0x44B8DB4C7871BC37, 0x1C45016D841BAA46, 0x03C715D6C6C1635F, 0x169D9ABE03495505,
0x3638DE456BCDE919, 0x1217AEFE69077737, 0x56C163A2461641C1, 0x1CF2B1970E725858,
0xDF011C81D1AB67CE, 0x17288E1271F51379, 0x7F3416CE4155ECA5, 0x1286D80EC190DC61,
0x6520247D3556476E, 0x1DA48CE468E7C702, 0xEA801D30F7783925, 0x17B6D71D20B96C01,
0xBB99B0F3F92CFA84, 0x12F8AC174D612334, 0x5F5C4E532847F739, 0x1E5AACF215683854,
0x7F7D0B75B9D32C2E, 0x18488A5B44536043, 0x9930D5F7C7DC2358, 0x136D3B7C36A919CF,
0x8EB4898C72F9D226, 0x1F152BF9F10E8FB2, 0x722A07A38F2E41B8, 0x18DDBCC7F40BA628,
0xC1BB394FA5BE9AFA, 0x13E497065CD61E86, 0x9C5EC2190930F7F6, 0x1FD424D6FAF030D7,
0x49E56814075A5FF8, 0x197683DF2F268D79, 0x6E51201005E1E660, 0x145ECFE5BF520AC7,
0xF1DA800CD181851A, 0x104BD984990E6F05, 0x4FC400148268D4F5, 0x1A12F5A0F4E3E4D6,
0xD96999AA01ED772B, 0x14DBF7B3F71CB711, 0xADEE1488018AC5BC, 0x10AFF95CC5B09274,
0x497CEDA668DE092C, 0x1AB328946F80EA54, 0x3ACA57B853E4D424, 0x155C2076BF9A5510,
0x623B7960431D7683, 0x1116805EFFAEAA73, 0x9D2BF566D1C8BD9E, 0x1B5733CB32B110B8,
0x7DBCC452416D647F, 0x15DF5CA28EF40D60, 0xCAFD69DB678AB6CC, 0x117F7D4ED8C33DE6,
0xAB2F0FC572778ADF, 0x1BFF2EE48E052FD7, 0x88F273045B92D580, 0x1665BF1D3E6A8CAC,
0xD3F528D049424466, 0x11EAFF4A98553D56, 0xB988414D4203A0A3, 0x1CAB3210F3BB9557,
0x6139CDD76802E6E9, 0x16EF5B40C2FC7779, 0xE761717920025254, 0x125915CD68C9F92D,
0xA568B58E999D5086, 0x1D5B561574765B7C, 0x5120913EE14AA6D2, 0x177C44DDF6C515FD,
0xA74D40FF1AA21F0E, 0x12C9D0B1923744CA, 0x0BAECE64F769CB4A, 0x1E0FB44F50586E11,
0x3C8BD850C5EE3C3B, 0x180C903F7379F1A7, 0xCA0979DA37F1C9C9, 0x133D4032C2C7F485,
0xA9A8C2F6BFE942DB, 0x1EC866B79E0CBA6F, 0x2153CF2BCCBA9BE3, 0x18A0522C7E709526,
0x1AA9728970954982, 0x13B374F06526DDB8, 0xF775840F1A88759D, 0x1F8587E7083E2F8C,
0x5F9136727BA05E17, 0x19379FEC0698260A, 0x1940F85B9619E4DF, 0x142C7FF0054684D5,
0xE100C6AFAB47EA4C, 0x1023998CD1053710, 0xCE67A44C453FDD47, 0x19D28F47B4D524E7,
0xD852E9D69DCCB106, 0x14A8729FC3DDB71F, 0x79DBEE454B0A2738, 0x1086C219697E2C19,
0x295FE3A211A9D859, 0x1A71368F0F30468F, 0xBAB31C81A7BB137A, 0x15275ED8D8F36BA5,
0x6228E39AEC95A92F, 0x10EC4BE0AD8F8951, 0x9D0E38F7E0EF7517, 0x1B13AC9AAF4C0EE8,
0xB0D82D931A592A79, 0x15A956E225D67253, 0x8D79BE0F4847552E, 0x11544581B7DEC1DC,
0x158F967EDA0BBB7C, 0x1BBA08CF8C979C94, 0x77A611FF14D62F97, 0x162E6D72D6DFB076,
0xF951A7FF43DE8C79, 0x11BEBDF578B2F391, 0xC21C3FFED2FDAD8E, 0x1C6463225AB7EC1C,
0x01B0333242648AD8, 0x16B6B5B5155FF017, 0x0159C28E9B83A246, 0x122BC490DDE659AC,
0xCEF604175F3903A3, 0x1D12D41AFCA3C2AC, 0x725E69AC4C2D9C83, 0x17424348CA1C9BBD,
0xF5185489D68AE39C, 0x129B69070816E2FD, 0xEE8D540FBDAB05C6, 0x1DC574D80CF16B2F,
0xBED77672FE226B05, 0x17D12A4670C1228C, 0xFF12C528CB4EBC04, 0x130DBB6B8D674ED6,
0xCB513B74787DF9A0, 0x1E7C5F127BD87E24, 0x090DC929F9FE614D, 0x18637F41FCAD31B7,
0xA0D7D42194CB810A, 0x1382CC34CA2427C5, 0x67BFB9CF5478CE77, 0x1F37AD21436D0C6F,
0x1FCC94A5DD2D71F9, 0x18F9574DCF8A7059, 0x7FD6DD517DBDF4C7, 0x13FAAC3E3FA1F37A,
0xFFBE2EE8C92FEE0B, 0x1FF779FD329CB8C3, 0x6631BF20A0F324D6, 0x1992C7FDC216FA36,
0xB827CC1A1A5C1D78, 0x14756CCB01ABFB5E, 0x935309AE7B7CE460, 0x105DF0A267BCC918,
0x1EEB42B0C594A099, 0x1A2FE76A3F9474F4, 0xE58902270476E6E1, 0x14F31F8832DD2A5C,
0xB7A0CE859D2BEBE7, 0x10C27FA028B0EEB0, 0x59014A6F61DFDFD8, 0x1AD0CC33744E4AB4,
0xE0CDD525E7E64CAD, 0x1573D68F903EA229, 0x4D7177518651D6F1, 0x11297872D9CBB4EE,
0x7BE8BEE8D6E957E8, 0x1B758D848FAC54B0, 0xFCBA3253DF211320, 0x15F7A46A0C89DD59,
0x63C8284318E74280, 0x1192E9EE706E4AAE, 0x060D0D3827D86A66, 0x1C1E43171A4A1117,
0x6B3DA42CECAD21EB, 0x167E9C127B6E7412, 0x88FE1CF0BD574E56, 0x11FEE341FC585CDB,
0x419694B462254A23, 0x1CCB0536608D615F, 0x67ABAA29E81DD4E9, 0x1708D0F84D3DE77F,
0xB95621BB2017DD87, 0x126D73F9D764B932, 0xC223692B668C95A5, 0x1D7BECC2F23AC1EA,
0xCE82BA891ED6DE1D, 0x179657025B6234BB, 0xA53562074BDF1818, 0x12DEAC01E2B4F6FC,
0x3B889CD87964F359, 0x1E3113363787F194, 0xFC6D4A46C783F5E1, 0x18274291C6065ADC,
0x30576E9F06032B1A, 0x13529BA7D19EAF17, 0x1A257DCB3CD1DE90, 0x1EEA92A61C311825,
0x481DFE3C30A7E540, 0x18BBA884E35A79B7, 0xD34B31C9C0865100, 0x13C9539D82AEC7C5,
0x5211E942CDA3B4CD, 0x1FA885C8D117A609, 0x74DB21023E1C90A4, 0x19539E3A40DFB807,
0xF715B401CB4A0D50, 0x1442E4FB67196005, 0xF8DE299B09080AA7, 0x103583FC527AB337,
0x8E304291A80CDDD7, 0x19EF3993B72AB859, 0x3E8D020E200A4B13, 0x14BF6142F8EEF9E1,
0x653D9B3E80083C0F, 0x10991A9BFA58C7E7, 0x6EC8F864000D2CE4, 0x1A8E90F9908E0CA5,
0x8BD3F9E999A423EA, 0x153EDA614071A3B7, 0x3CA994BAE1501CBB, 0x10FF151A99F482F9,
0xC775BAC49BB3612B, 0x1B31BB5DC320D18E, 0xD2C4956A16291A89, 0x15C162B168E70E0B,
0xDBD0778811BA7BA1, 0x11678227871F3E6F, 0x2C80BF401C5D929B, 0x1BD8D03F3E9863E6,
0xBD33CC3349E47549, 0x16470CFF6546B651, 0xCA8FD68F6E505DD4, 0x11D270CC51055EA7,
0x4419574BE3B3C953, 0x1C83E7AD4E6EFDD9, 0x0347790982F63AA9, 0x16CFEC8AA52597E1,
0xCF6C60D468C4FBBA, 0x123FF06EEA847980, 0xE57A34870E07F92A, 0x1D331A4B10D3F59A,
0x512E906C0B399422, 0x175C1508DA432AE2, 0xDA8BA6BCD5C7A9B5, 0x12B010D3E1CF5581,
0x90DF712E22D90F87, 0x1DE6815302E5559C, 0xDA4C5A8B4F140C6C, 0x17EB9AA8CF1DDE16,
0xAEA37BA2A5A9A38A, 0x1322E220A5B17E78, 0x7DD25F6AA2A905A9, 0x1E9E369AA2B59727,
0x97DB7F888220D154, 0x187E92154EF7AC1F, 0x797C6606CE80A777, 0x139874DDD8C6234C,
0x8F2D700AE4010BF1, 0x1F5A549627A36BAD, 0x0C2459A25000D65A, 0x191510781FB5EFBE,
0x701D1481D99A4515, 0x1410D9F9B2F7F2FE, 0xC017439B147B6A77, 0x100D7B2E28C65BFE,
0xCCF205C4ED9243F2, 0x19AF2B7D0E0A2CCA, 0x0A5B37D0BE0E9CC2, 0x148C22CA71A1BD6F,
0x0848F973CB3EE3CE, 0x10701BD527B4978C, 0xDA0E5BEC78649FB0, 0x1A4CF9550C5425AC,
0x7B3EAFF060507FC0, 0x150A6110D6A9B7BD, 0x95CBBFF380406633, 0x10D51A73DEEE2C97,
0xEFAC665266CD7052, 0x1AEE90B964B04758, 0x2623850EB8A459DB, 0x158BA6FAB6F36C47,
0x1E82D0D893B6AE49, 0x113C85955F29236C, 0xFD9E1AF41F8AB075, 0x1B9408EEFEA838AC,
0x97B1AF29B2D559F7, 0x16100725988693BD, 0xAC8E25BAF5777B2C, 0x11A66C1E139EDC97,
0x7A7D092B2258C513, 0x1C3D79C9B8FE2DBF, 0x61FDA0EF4EAD6A76, 0x169794A160CB57CC,
0xE7FE1A590BBDEEC5, 0x1212DD4DE7091309, 0xA6635D5B45FCB13A, 0x1CEAFBAFD80E84DC,
0x851C4AAF6B308DC8, 0x172262F3133ED0B0, 0xD0E36EF2BC26D7D4, 0x1281E8C275CBDA26,
0xB49F17EAC6A48C86, 0x1D9CA79D894629D7, 0x2A18DFEF0550706B, 0x17B08617A104EE46,
0x54E0B3259DD9F389, 0x12F39E794D9D8B6B, 0x87CDEB6F62F65274, 0x1E5297287C2F4578,
0xD30B22BF825EA85D, 0x18421286C9BF6AC6, 0x0F3C1BCC684BB9E4, 0x13680ED23AFF889F,
0x18602C7A4079296D, 0x1F0CE4839198DA98, 0x46B356C833942124, 0x18D71D360E13E213,
0x388F78A029434DB6, 0x13DF4A91A4DCB4DC, 0x5A7F2766A86BAF8A, 0x1FCBAA82A1612160,
0x153285EBB9EFBFA2, 0x196FBB9BB44DB44D, 0xAA8ED189618C994E, 0x145962E2F6A4903D,
0xEED8A7A11AD6E10C, 0x1047824F2BB6D9CA, 0x7E27729B5E249B45, 0x1A0C03B1DF8AF611,
0xFE85F549181D4904, 0x14D6695B193BF80D, 0xCB9E5DD4134AA0D0, 0x10AB877C142FF9A4,
0xDF63C9535211014D, 0x1AAC0BF9B9E65C3A, 0x191CA10F74DA6771, 0x15566FFAFB1EB02F,
0xADB080D92A4852C1, 0x1111F32F2F4BC025, 0x15E7348EAA0D5134, 0x1B4FEB7EB212CD09,
0xAB1F5D3EEE710DC4, 0x15D98932280F0A6D, 0xBC1917658B8DA49D, 0x117AD428200C0857,
0x2CF4F23C127C3A94, 0x1BF7B9D9CCE00D59, 0xF0C3F4FCDB969543, 0x165FC7E170B33DE0,
0x5A365D9716121103, 0x11E6398126F5CB1A, 0x9056FC24F01CE804, 0x1CA38F350B22DE90,
0xD9DF301D8CE3ECD0, 0x16E93F5DA2824BA6, 0xE17F59B13D8323DA, 0x125432B14ECEA2EB,
0x68CBC2B52F38395C, 0x1D53844EE47DD179, 0x53D6355DBF602DE3, 0x177603725064A794,
0xA9782AB165E68B1C, 0x12C4CF8EA6B6EC76, 0x0F26AAB56FD744FA, 0x1E07B27DD78B13F1,
0x3F52222ABFDF6A62, 0x18062864AC6F4327, 0x65DB4E88997F884E, 0x1338205089F29C1F,
0x6FC54A7428CC0D4A, 0x1EC033B40FEA9365, 0x596AA1F68709A43B, 0x1899C2F673220F84,
0xADEEE7F86C07B696, 0x13AE3591F5B4D936, 0x497E3FF3E00C5756, 0x1F7D228322BAF524,
0xD464FFF64CD6AC45, 0x1930E868E89590E9, 0x4383FFF83D7889D1, 0x14272053ED4473EE,
0xCF9CCCC69793A174, 0x101F4D0FF1038FF1, 0x7F6147A425B90252, 0x19CBAE7FE805B31C,
0xCC4DD2E9B7C7350F, 0x14A2F1FFECD15C16, 0x3D0B0F215FD290D9, 0x10825B3323DAB012,
0x61AB4B689950E7C1, 0x1A6A2B85062AB350, 0x4E22A2BA1440B967, 0x1521BC6A6B555C40,
0x0B4EE894DD009453, 0x10E7C9EEBC4449CD, 0x1217DA87C800ED51, 0x1B0C764AC6D3A948,
0xDB46486CA000BDDA, 0x15A391D56BDC876C, 0x490506BD4CCD64AF, 0x114FA7DDEFE39F8A,
0xA8080AC87AE23AB1, 0x1BB2A62FE638FF43, 0x5339A239FBE82EF4, 0x162884F31E93FF69,
0x75C7B4FB2FECF25D, 0x11BA03F5B20FFF87, 0x22D92191E647EA2E, 0x1C5CD322B67FFF3F,
0xB57A8141850654F2, 0x16B0A8E891FFFF65, 0xC4620101373843F5, 0x1226ED86DB3332B7,
0x3A366801F1F39FEE, 0x1D0B15A491EB8459, 0xFB5EB99B27F6198B, 0x173C115074BC69E0,
0x2F7EFAE2865E7AD6, 0x129674405D6387E7, 0xE597F7D0D6FD9156, 0x1DBD86CD6238D971,
0x8479930D78CADAAB, 0x17CAD23DE82D7AC1, 0xD06142712D6F1556, 0x1308A831868AC89A,
0x4D686A4EAF182222, 0x1E74404F3DAADA91, 0xA453883EF279B4E8, 0x185D003F6488AEDA,
0xE9DC6CFF28615D87, 0x137D99CC506D58AE, 0xA960AE650D6895A4, 0x1F2F5C7A1A488DE4,
0xBAB3BEB73DED4483, 0x18F2B061AEA07183, 0x2EF6322C318A9D36, 0x13F559E7BEE6C136,
0xE4BD1D13827761F0, 0x1FEEF63F97D79B89, 0x83CA7DA9352C4E5A, 0x198BF832DFDFAFA1,
0x9CA1FE20F756A515, 0x146FF9C24CB2F2E7, 0x4A1B31B3F9121DAA, 0x1059949B708F28B9,
0x435EB5ECC1B695DD, 0x1A28EDC580E50DF5, 0x35E55E57015EDE4A, 0x14ED8B04671DA4C4,
0xC4B77EAC0118B1D5, 0x10BE08D0527E1D69, 0xA12597799B5AB622, 0x1AC9A7B3B7302F0F,
0x4DB7AC6149155E81, 0x156E1FC2F8F358D9, 0xD7C6238107444B9B, 0x1124E63593F5E0AD,
0x593D059B3ED3AC2B, 0x1B6E3D2286563449, 0xE0FD9E15CBDC89BC, 0x15F1CA820511C36D,
0xB3FE18116FE3A163, 0x118E3B9B37416924, 0x866359B57FD29BD1, 0x1C16C5C525357507,
0xD1E91491330EE30E, 0x16789E3750F790D2, 0x74BA76DA8F3F1C0B, 0x11FA182C40C60D75,
0xEDF72490E531C678, 0x1CC359E067A348BB, 0x8B2C1D40B75B052D, 0x1702AE4D1FB5D3C9,
0x6F567DCD5F7C0424, 0x12688B70E62B0FD4, 0x7EF0C94898C66D06, 0x1D74124E3D11B2ED,
0x98C0A106E09EBD9F, 0x17900EA4FDA7C257, 0x470080D24D4BCAE6, 0x12D9A550CAEC9B79,
0xD800CE1D487944A2, 0x1E29088144ADC58E, 0x1333D8176D2DD082, 0x1820D39A9D57D13F,
0xA8F646792424A6CE, 0x134D76154AACA765, 0x74BD3D8EA03AA47D, 0x1EE25688777AA56F,
0x5D64313EE6955064, 0x18B51206C5FBB78C, 0x4AB68DCBEBAAA6B7, 0x13C40E6BD1962C70,
0x1124161312AAA457, 0x1FA01712E8F0471A, 0xDA8344DC0EEEE9DF, 0x194CDF4253F36C14,
0xE2029D7CD8BF2180, 0x143D7F6843292343, 0x4E687DFD7A328133, 0x103132B9CF541C36,
0x4A40C9959050CEB8, 0x19E851294BB9C6BD, 0x0833D477A6A70BC6, 0x14B9DA876FC7D231,
0xA02976C61EEC096B, 0x1094AED2BFD30E8D, 0x004257A364ACDBDF, 0x1A877E1DFFB81749,
0xCD01DFB5EA23E319, 0x153931B1996012A0, 0x70CE4C91881CB5AE, 0x10FA8E27ADE6754D,
0x1AE3ADB5A69455E2, 0x1B2A7D0C4970BBAF, 0x7BE957C4854377E8, 0x15BB973D078D62F2,
0xC987796A0435F987, 0x1162DF64060AB58E, 0x75A58F1006BCC271, 0x1BD1656CD67788E4,
0xF7B7A5A66BCA3527, 0x16411DF0AB92D3E9, 0x5FC61E1EBCA1C41F, 0x11CDB18D560F0FEE,
0xFFA363646102D365, 0x1C7C4F4889B1B316, 0x32E91C504D9BDC51, 0x16C9D906D48E28DF,
0x8F20E37371497D0E, 0x123B140576D820B2, 0x7E9B0585820F2E7C, 0x1D2B533BF159CDEA,
0xCBAF379E01A5BECA, 0x1755DC2FF447D7EE, 0x0958F94B348498A1, 0x12AB168CC36CACBF,
]
"""
## The text of `src/std/float_text/ryu.lucb`.
let text_4: str = r"""#==============================================================================================
#
# ryu - The shortest digits of a float (float_text)
#
# DESCRIPTION:
# Ryu's d2d and f2s over float_text's tables of powers of five: the fewest decimal
# digits, and their power of ten, that read back as a finite nonzero f64 or f32.
#
#==============================================================================================
# mark: Ryu ====================================================================================
## Ryu's d2d: the shortest decimal digits * 10^exponent of a finite, nonzero f64, given its
## mantissa and exponent fields.
func shortest64(ieee_mantissa: u64, ieee_exponent: u32) -> (u64, i32):
var e2: i32 = 0
var m2: u64 = 0
if ieee_exponent == 0:
e2 = 1 - 1023 - 52 - 2
m2 = ieee_mantissa
else:
e2 = (i32)ieee_exponent - 1023 - 52 - 2
m2 = ((u64)1 << 52) | ieee_mantissa
let accept_bounds = (m2 & 1) == 0
let mv = 4 * m2
let mm_shift: u64 = 1 if ieee_mantissa != 0 or ieee_exponent <= 1 else 0
var vr: u64 = 0
var vp: u64 = 0
var vm: u64 = 0
var e10: i32 = 0
var vm_trailing_zeros = false
var vr_trailing_zeros = false
if e2 >= 0:
let q = log10_pow2(e2) - (1 if e2 > 3 else 0)
e10 = q
let k = 125 + pow5_bits(q) - 1
let i = -e2 + q + k
let row = (usize)q * 2
vr = mul_shift64(4 * m2, pow5_inv_split[row], pow5_inv_split[row + 1], i)
vp = mul_shift64(4 * m2 + 2, pow5_inv_split[row], pow5_inv_split[row + 1], i)
vm = mul_shift64(4 * m2 - 1 - mm_shift, pow5_inv_split[row], pow5_inv_split[row + 1], i)
if q <= 21:
if mv % 5 == 0:
vr_trailing_zeros = pow5_factor(mv) >= (u32)q
elif accept_bounds:
vm_trailing_zeros = pow5_factor(mv - 1 - mm_shift) >= (u32)q
elif pow5_factor(mv + 2) >= (u32)q:
vp -= 1
else:
let q = log10_pow5(-e2) - (1 if -e2 > 1 else 0)
e10 = q + e2
let i = -e2 - q
let k = pow5_bits(i) - 125
let j = q - k
let row = (usize)i * 2
vr = mul_shift64(4 * m2, pow5_split[row], pow5_split[row + 1], j)
vp = mul_shift64(4 * m2 + 2, pow5_split[row], pow5_split[row + 1], j)
vm = mul_shift64(4 * m2 - 1 - mm_shift, pow5_split[row], pow5_split[row + 1], j)
if q <= 1:
vr_trailing_zeros = true
if accept_bounds:
vm_trailing_zeros = mm_shift == 1
else:
vp -= 1
elif q < 63:
vr_trailing_zeros = (mv & (((u64)1 << (u64)q) - 1)) == 0
return trimmed(vr, vp, vm, e10, accept_bounds, vm_trailing_zeros, vr_trailing_zeros, 0)
## Ryu's digit removal, d2d's and f2s's alike: drop digits while the bounds still differ,
## then round on the last digit removed (`start_last`, the one f2s's bounds computation
## already dropped, or 0). Dropping two digits at once leaves their tens digit as the last.
func trimmed(start_vr: u64, start_vp: u64, start_vm: u64, e10: i32, accept_bounds: bool, start_vm_zeros: bool, start_vr_zeros: bool, start_last: u64) -> (u64, i32):
var vr = start_vr
var vp = start_vp
var vm = start_vm
var vm_trailing_zeros = start_vm_zeros
var vr_trailing_zeros = start_vr_zeros
var removed: i32 = 0
var last_removed = start_last
if vm_trailing_zeros or vr_trailing_zeros:
while vp // 10 > vm // 10:
vm_trailing_zeros = vm_trailing_zeros and vm % 10 == 0
vr_trailing_zeros = vr_trailing_zeros and last_removed == 0
last_removed = vr % 10
vr = vr // 10
vp = vp // 10
vm = vm // 10
removed += 1
if vm_trailing_zeros:
while vm % 10 == 0:
vr_trailing_zeros = vr_trailing_zeros and last_removed == 0
last_removed = vr % 10
vr = vr // 10
vp = vp // 10
vm = vm // 10
removed += 1
if vr_trailing_zeros and last_removed == 5 and vr % 2 == 0:
last_removed = 4
let up = (vr == vm and (not accept_bounds or not vm_trailing_zeros)) or last_removed >= 5
return (vr + (1 if up else 0), e10 + removed)
if vp // 100 > vm // 100:
last_removed = (vr % 100) // 10
vr = vr // 100
vp = vp // 100
vm = vm // 100
removed += 2
while vp // 10 > vm // 10:
last_removed = vr % 10
vr = vr // 10
vp = vp // 10
vm = vm // 10
removed += 1
return (vr + (1 if vr == vm or last_removed >= 5 else 0), e10 + removed)
## Ryu's f2s over the same tables: the shortest decimal digits * 10^exponent of a finite,
## nonzero f32.
func shortest32(ieee_mantissa: u32, ieee_exponent: u32) -> (u32, i32):
var e2: i32 = 0
var m2: u32 = 0
if ieee_exponent == 0:
e2 = 1 - 127 - 23 - 2
m2 = ieee_mantissa
else:
e2 = (i32)ieee_exponent - 127 - 23 - 2
m2 = ((u32)1 << 23) | ieee_mantissa
let accept_bounds = (m2 & 1) == 0
let mv = 4 * m2
let mp = 4 * m2 + 2
let mm_shift: u32 = 1 if ieee_mantissa != 0 or ieee_exponent <= 1 else 0
let mm = 4 * m2 - 1 - mm_shift
var vr: u32 = 0
var vp: u32 = 0
var vm: u32 = 0
var e10: i32 = 0
var vm_trailing_zeros = false
var vr_trailing_zeros = false
var last_removed: u32 = 0
if e2 >= 0:
let q = log10_pow2(e2)
e10 = q
let k = 61 + pow5_bits(q) - 1
let i = -e2 + q + k
vr = pow5_inv_div_pow2(mv, q, i)
vp = pow5_inv_div_pow2(mp, q, i)
vm = pow5_inv_div_pow2(mm, q, i)
if q != 0 and (vp - 1) // 10 <= vm // 10:
let l = 61 + pow5_bits(q - 1) - 1
last_removed = pow5_inv_div_pow2(mv, q - 1, -e2 + q - 1 + l) % 10
if q <= 9:
if mv % 5 == 0:
vr_trailing_zeros = pow5_factor((u64)mv) >= (u32)q
elif accept_bounds:
vm_trailing_zeros = pow5_factor((u64)mm) >= (u32)q
elif pow5_factor((u64)mp) >= (u32)q:
vp -= 1
else:
let q = log10_pow5(-e2)
e10 = q + e2
let i = -e2 - q
let k = pow5_bits(i) - 61
var j = q - k
vr = pow5_div_pow2(mv, i, j)
vp = pow5_div_pow2(mp, i, j)
vm = pow5_div_pow2(mm, i, j)
if q != 0 and (vp - 1) // 10 <= vm // 10:
j = q - 1 - (pow5_bits(i + 1) - 61)
last_removed = pow5_div_pow2(mv, i + 1, j) % 10
if q <= 1:
vr_trailing_zeros = true
if accept_bounds:
vm_trailing_zeros = mm_shift == 1
else:
vp -= 1
elif q < 31:
vr_trailing_zeros = (mv & (((u32)1 << (u32)(q - 1)) - 1)) == 0
let (digits, exponent) = trimmed((u64)vr, (u64)vp, (u64)vm, e10, accept_bounds, vm_trailing_zeros, vr_trailing_zeros, (u64)last_removed)
return (u32(digits), exponent)
## floor(log2(5^e)) + 1 for 0 <= e <= 3528.
inline func pow5_bits(e: i32) -> i32:
return (i32)((((u32)e) * 1217359) >> 19) + 1
## floor(log10(2^e)) for 0 <= e <= 1650.
inline func log10_pow2(e: i32) -> i32:
return (i32)((((u32)e) * 78913) >> 18)
## floor(log10(5^e)) for 0 <= e <= 2620.
inline func log10_pow5(e: i32) -> i32:
return (i32)((((u32)e) * 732923) >> 20)
## How many times 5 divides `value` (nonzero).
func pow5_factor(value: u64) -> u32:
var rest = value
var count: u32 = 0
while rest % 5 == 0:
rest = rest // 5
count += 1
return count
## (m * (high:low)) >> j for Ryu's 125-bit multipliers.
inline func mul_shift64(m: u64, low: u64, high: u64, j: i32) -> u64:
var b0_low: u64 = 0
let b0_high = product(m, low, &b0_low)
var b2_low: u64 = 0
var b2_high = product(m, high, &b2_low)
let sum_low = b2_low +% b0_high
if sum_low < b2_low:
b2_high += 1
let shift = (u64)(j - 64)
if shift == 0:
return sum_low
if shift >= 64:
return b2_high >> (shift - 64)
return (b2_high << (64 - shift)) | (sum_low >> shift)
inline func mul_shift32(m: u32, factor: u64, shift: i32) -> u32:
let low = (u64)m * (factor & 0xFFFFFFFF)
let high = (u64)m * (factor >> 32)
let sum = (low >> 32) + high
return (u32)(sum >> (u64)(shift - 32))
inline func pow5_inv_div_pow2(m: u32, q: i32, j: i32) -> u32:
return mul_shift32(m, pow5_inv_split[(usize)q * 2 + 1] + 1, j)
inline func pow5_div_pow2(m: u32, i: i32, j: i32) -> u32:
return mul_shift32(m, pow5_split[(usize)i * 2 + 1], j)
## The 128-bit product a * b: the high word, the low word stored in `low`. Ryu's
## multiplications and strings' float parsing (Lemire's) both take it.
pub inline func product(a: u64, b: u64, low: u64*) -> u64:
let a0 = a & 0xFFFFFFFF
let a1 = a >> 32
let b0 = b & 0xFFFFFFFF
let b1 = b >> 32
let p00 = a0 * b0
let p01 = a0 * b1
let p10 = a1 * b0
let p11 = a1 * b1
let middle = (p00 >> 32) + (p01 & 0xFFFFFFFF) + (p10 & 0xFFFFFFFF)
*low = (middle << 32) | (p00 & 0xFFFFFFFF)
return p11 + (p01 >> 32) + (p10 >> 32) + (middle >> 32)
"""
## The text of `src/std/numerals.lucb`.
let text_5: str = r"""#==============================================================================================
#
# numerals - Numbers and text laid out by a format specification
#
# DESCRIPTION:
# What a formatted string's `{value:spec}` field does (base.md §5.5), as Python's
# `format` does it: the specification read as CPython's
# parse_internal_render_format_spec reads it, a number laid out as calc_number_widths
# and fill_number lay it out, and a float's digits made exactly and rounded half to
# even, as David Gay's dtoa rounds them. The text goes to a `Sink` piece by piece, so
# nothing is allocated and nothing bounds a field but the sink. The compiler reads a
# field's specification with `read` and `misfit` and refuses one that does not fit;
# the program lays the field out with `lay_integer`, `lay_float` or `lay_text`. It is
# the one implementation: Luce lays out its fields and rounds `math.round` with it too,
# wording a misfit in its own terms from `misfit_of` (luce src/support/number_text.lucb),
# and the seed carries a copy of this file. It comes after `float_text` and before
# `core`, and needs nothing else.
#
#==============================================================================================
import float_text
# mark: Specifications =========================================================================
## A specification read: `[[fill]align][sign][z][#][0][width][grouping][.precision][type]`
## (§5.5). A part not written is `0`, `-1` or empty, so a default can still be told apart.
pub struct Spec:
## The fill scalar, as written; empty when none was.
pub var fill: str
## `<`, `>`, `^` or `=`; 0 when none was written.
pub var align: u8
## `+`, `-` or a space; 0 when none was written.
pub var sign: u8
## `z`: a negative zero, after rounding, shows as zero.
pub var no_negative_zero: bool
## `#`: the alternate form, a base's prefix or a point always kept.
pub var alternate: bool
## `0` before the width: zeros fill a number after its sign.
pub var zero: bool
## The least width in scalars; -1 when none was written.
pub var width: i64
## `,` or `_` between groups of digits; 0 when none was written.
pub var grouping: u8
## The digits after the point, or the scalars a text keeps; -1 when none was written.
pub var precision: i64
## The type letter; 0 when none was written.
pub var kind: u8
## A specification read from its text, and what is wrong with it: empty when nothing is.
pub struct Reading:
## The specification, meaningful when `problem` is empty.
pub var spec: Spec
## Why the text is not a specification; empty when it is one.
pub var problem: str
## What a field shows: an integer, a float, or text (a `str`, a `char`, a `bool`).
pub enum Category:
integer
floating
text
func is_align(byte: u8) -> bool:
return byte == '<' or byte == '>' or byte == '^' or byte == '='
## The byte length of the UTF-8 scalar starting with `lead`.
func scalar_bytes(lead: u8) -> usize:
if lead < 0x80:
return 1
if lead < 0xE0:
return 2
if lead < 0xF0:
return 3
return 4
## The scalars of UTF-8 `text`.
pub func scalar_count(text: const u8[]) -> i64:
var n: i64 = 0
for b in text:
if (b & 0xC0) != 0x80:
n += 1
return n
## Digits read from `at` as a width or a precision; how many there were, -1 when too many.
func read_count(b: const u8[], at: usize*, value: i64*) -> i64:
var n: i64 = 0
var total: i64 = 0
while *at < b.length and b[*at] >= '0' and b[*at] <= '9':
total = total * 10 + (i64)(b[*at] - '0')
if total > 1000000000:
return -1
n += 1
*at += 1
*value = total
return n
## Read `text` as CPython's parse_internal_render_format_spec reads a specification.
pub func read(text: str) -> Reading:
var spec = Spec(fill = "", align = 0, sign = 0, no_negative_zero = false, alternate = false, zero = false, width = -1, grouping = 0, precision = -1, kind = 0)
let b = text.bytes
var at: usize = 0
var first: usize = 0
if b.length > 0:
first = scalar_bytes(b[0])
if b.length > first and is_align(b[first]):
spec.fill = (str)b[..<first]
spec.align = b[first]
at = first + 1
elif b.length > 0 and is_align(b[0]):
spec.align = b[0]
at = 1
if at < b.length and (b[at] == '+' or b[at] == '-' or b[at] == ' '):
spec.sign = b[at]
at += 1
if at < b.length and b[at] == 'z':
spec.no_negative_zero = true
at += 1
if at < b.length and b[at] == '#':
spec.alternate = true
at += 1
if spec.fill.length == 0 and at < b.length and b[at] == '0':
spec.zero = true
at += 1
var width: i64 = 0
let digits = read_count(b, &at, &width)
if digits < 0:
return Reading(spec = spec, problem = "its width is too large")
if digits > 0:
spec.width = width
if at < b.length and (b[at] == ',' or b[at] == '_'):
spec.grouping = b[at]
at += 1
if at < b.length and (b[at] == ',' or b[at] == '_'):
return Reading(spec = spec, problem = "it groups digits with both `,` and `_`")
if at < b.length and b[at] == '.':
at += 1
var precision: i64 = 0
let n = read_count(b, &at, &precision)
if n < 0:
return Reading(spec = spec, problem = "its precision is too large")
if n == 0:
return Reading(spec = spec, problem = "a `.` is followed by the precision's digits")
spec.precision = precision
if b.length - at > 1:
return Reading(spec = spec, problem = "it is not `[[fill]align][sign][z][#][0][width][grouping][.precision][type]`")
if at < b.length:
spec.kind = b[at]
return Reading(spec = spec, problem = "")
## Why a specification does not fit a value of its category, for its caller to word.
pub enum Misfit:
## It fits.
fits
## An integer with `e`, `f`, `g`, `%` or their upper cases.
float_type_of_integer
## An integer with a type letter of no number.
integer_type
## An integer with a precision.
integer_precision
## An integer with `z`.
integer_z
## A `c` with a sign.
scalar_sign
## A `c` with `#`.
scalar_alternate
## `,` with `b`, `o`, `x` or `X`.
comma_grouping
## A `c` with `_`.
scalar_grouping
## A float with a type letter of no float.
float_type
## Text with a type letter other than `s`.
text_type
## Text with a sign.
text_sign
## Text with `z`.
text_z
## Text with `#`.
text_alternate
## Text aligned with `=`.
text_after_sign
## Text with grouping.
text_grouping
## Why `spec` does not fit a value of `category`, or `fits` (§5.5).
pub func misfit_of(spec: Spec, category: Category) -> Misfit:
let k = spec.kind
match category:
.integer:
if k == 'e' or k == 'E' or k == 'f' or k == 'F' or k == 'g' or k == 'G' or k == '%':
return Misfit.float_type_of_integer
if k != 0 and k != 'd' and k != 'b' and k != 'o' and k != 'x' and k != 'X' and k != 'c':
return Misfit.integer_type
if spec.precision >= 0:
return Misfit.integer_precision
if spec.no_negative_zero:
return Misfit.integer_z
if k == 'c' and spec.sign != 0:
return Misfit.scalar_sign
if k == 'c' and spec.alternate:
return Misfit.scalar_alternate
if spec.grouping == ',' and k != 0 and k != 'd':
return Misfit.comma_grouping
if spec.grouping == '_' and k == 'c':
return Misfit.scalar_grouping
.floating:
if k != 0 and k != 'e' and k != 'E' and k != 'f' and k != 'F' and k != 'g' and k != 'G' and k != '%':
return Misfit.float_type
.text:
if k != 0 and k != 's':
return Misfit.text_type
if spec.sign != 0:
return Misfit.text_sign
if spec.no_negative_zero:
return Misfit.text_z
if spec.alternate:
return Misfit.text_alternate
if spec.align == '=':
return Misfit.text_after_sign
if spec.grouping != 0:
return Misfit.text_grouping
return Misfit.fits
## What is wrong with `spec` for a value of `category`, in Base's terms: empty when it
## fits (§5.5).
pub func misfit(spec: Spec, category: Category) -> str:
match misfit_of(spec, category):
.fits:
return ""
.float_type_of_integer:
return "its type letter formats a float; convert the integer with `f64(...)` first"
.integer_type:
return "an integer is formatted with `d`, `b`, `o`, `x`, `X` or `c`"
.integer_precision:
return "an integer takes no precision"
.integer_z, .text_z:
return "`z` belongs to a float"
.scalar_sign:
return "`c` takes no sign"
.scalar_alternate:
return "`c` takes no `#`"
.comma_grouping:
return "`,` groups decimal digits; `_` groups `b`, `o`, `x` and `X`"
.scalar_grouping:
return "`c` takes no grouping"
.float_type:
return "a float is formatted with `e`, `E`, `f`, `F`, `g`, `G` or `%`"
.text_type:
return "text is formatted with `s` or no type"
.text_sign:
return "a sign belongs to a number"
.text_alternate:
return "`#` belongs to a number"
.text_after_sign:
return "`=` aligns a number after its sign"
.text_grouping:
return "grouping belongs to a number"
## The fill and alignment a field uses: the written ones, else the defaults, a number to
## the right and text to the left, and `0` before the width filling with zeros after the
## sign (CPython's backwards-compatible special case).
func settle(spec: Spec, numeric: bool) -> Spec:
var out = spec
if spec.zero:
out.fill = "0"
if spec.align == 0 and numeric:
out.align = '='
if out.fill.length == 0:
out.fill = " "
if out.align == 0:
out.align = '>' if numeric else '<'
return out
## The settled specification of `specification`, which the compiler has read and fitted.
func settled(specification: str, numeric: bool) -> Spec:
let reading = read(specification)
if reading.problem.length > 0:
trap("a formatted field's specification is not one; the compiler refuses it")
return settle(reading.spec, numeric)
# mark: Output =================================================================================
## Where laid-out text goes: `write` appends `bytes` to `context`, answering false when
## they did not all fit, which ends the layout.
pub struct Sink:
## What the text is written into, handed to `write`.
pub var context: void*
## Append the bytes; false when they did not all fit.
pub var write: func(void*, const u8[]) -> bool
func put(sink: Sink, bytes: const u8[]) -> bool:
let write = sink.write
return bytes.length == 0 or write(sink.context, bytes)
func put_byte(sink: Sink, byte: u8) -> bool:
let one: u8[1] = [byte]
return put(sink, one)
## `text` `n` times.
func repeat(sink: Sink, text: str, n: i64) -> bool:
var i: i64 = 0
while i < n:
if not put(sink, text.bytes):
return false
i += 1
return true
## `text` with its lower-case letters in upper case when `upper`.
func put_cased(sink: Sink, text: const u8[], upper: bool) -> bool:
if not upper:
return put(sink, text)
for c in text:
if not put_byte(sink, c - 32 if c >= 'a' and c <= 'z' else c):
return false
return true
## Padding before and after `n` scalars to `width`, by the alignment (CPython's calc_padding).
func padding(n: i64, width: i64, align: u8) -> (i64, i64):
let total = width if width > n else n
var left: i64 = 0
if align == '>':
left = total - n
elif align == '^':
left = (total - n) // 2
return (left, total - n - left)
func smaller(a: i64, b: i64) -> i64:
return a if a < b else b
func larger(a: i64, b: i64) -> i64:
return a if a > b else b
# mark: Digits =================================================================================
## Digits, a virtual `0` outside `[0, count)`: an integer's in its base, or the exact
## decimal digits of a float's magnitude, `0.d1d2d3... × 10^point`, at most 767 significant
## digits for any finite f64 and no trailing zero.
pub struct Decimal:
## The digits, as ASCII; the first `count` are meaningful.
pub var digits: u8[800]
## How many digits there are; 0 is zero.
pub var count: usize
## Where the point stands: before `digits[point]`, a negative `point` that many zeros
## earlier.
pub var point: i64
## The digit at virtual index `i`.
pub func at(i: i64) -> u8:
return self.digits[(usize)i] if i >= 0 and i < (i64)self.count else '0'
## The digits of `magnitude` in `base` (2, 8, 10 or 16), lower case.
func integer_digits(magnitude: u64, base: u64) -> Decimal:
var d: Decimal = ---
var reversed: u8[64] = ---
var n: usize = 0
var v = magnitude
while true:
let digit = (u8)(v % base)
reversed[n] = '0' + digit if digit < 10 else 'a' + digit - 10
n += 1
v = v // base
if v == 0:
break
d.count = n
d.point = (i64)n
var i: usize = 0
while i < n:
d.digits[i] = reversed[n - 1 - i]
i += 1
return d
## The digits of |value|, finite: the significand times a power of two, made decimal in
## limbs of 10^9 exactly (multiplying by 2^k, or by 5^k for a negative exponent).
func exact(value: f64) -> Decimal:
var d: Decimal = ---
d.count = 0
d.point = 0
let bits = value.bits()
let exponent_bits = (i64)((bits >> 52) & 0x7FF)
var significand = bits & 0xFFFFFFFFFFFFF
var exponent: i64 = -1074
if exponent_bits != 0:
significand = significand | 0x10000000000000
exponent = exponent_bits - 1075
if significand == 0:
d.digits[0] = '0'
d.count = 1
d.point = 1
return d
var limbs: u64[100] = ---
limbs[0] = significand % 1000000000
limbs[1] = significand // 1000000000
var used: usize = 2 if limbs[1] != 0 else 1
if exponent >= 0:
var left = exponent
while left > 0:
let shift = left if left < 30 else 30
used = multiply(limbs, used, (u64)1 << (u64)shift)
left -= shift
else:
var left = -exponent
while left > 0:
let step = left if left < 13 else 13
var factor: u64 = 1
for _ in 0..<step:
factor *= 5
used = multiply(limbs, used, factor)
left -= step
# the top limb without leading zeros, every other one as nine digits
var top: u8[9] = ---
var n: usize = 0
var high = limbs[used - 1]
while high > 0:
top[n] = '0' + (u8)(high % 10)
high = high // 10
n += 1
while n > 0:
n -= 1
d.digits[d.count] = top[n]
d.count += 1
var i = used - 1
while i > 0:
i -= 1
var limb = limbs[i]
var k: usize = 9
while k > 0:
k -= 1
d.digits[d.count + k] = '0' + (u8)(limb % 10)
limb = limb // 10
d.count += 9
d.point = (i64)d.count + (exponent if exponent < 0 else 0)
trim_zeros(&d)
return d
## `limbs[..<used] *= factor`, factor at most 2^30; the limbs now used.
func multiply(limbs: u64[], used: usize, factor: u64) -> usize:
var carry: u64 = 0
var i: usize = 0
while i < used:
let product = limbs[i] * factor + carry
limbs[i] = product % 1000000000
carry = product // 1000000000
i += 1
var n = used
while carry > 0:
limbs[n] = carry % 1000000000
carry = carry // 1000000000
n += 1
return n
func trim_zeros(d: Decimal*):
while d.count > 1 and d.digits[d.count - 1] == '0':
d.count -= 1
## Keep the first `keep` digits, rounding half to even on the exact value; `keep` may be 0
## or less, which leaves zero (`count` 0) or one digit carried up.
func round_to(d: Decimal*, keep: i64):
if keep >= (i64)d.count:
return
var up = false
if keep >= 0:
let next = d.digits[(usize)keep]
var beyond = false
var i = (usize)keep + 1
while i < d.count:
if d.digits[i] != '0':
beyond = true
i += 1
let odd = keep > 0 and ((d.digits[(usize)keep - 1] - '0') % 2) == 1
up = next > '5' or (next == '5' and (beyond or odd))
if keep <= 0:
d.count = 0
if up:
d.digits[0] = '1'
d.count = 1
d.point += 1
return
d.count = (usize)keep
if up:
var i = d.count
var carried = true
while carried and i > 0:
i -= 1
if d.digits[i] == '9':
d.digits[i] = '0'
else:
d.digits[i] += 1
carried = false
if carried:
# every kept digit was 9: one digit, a 1, a place higher
d.digits[0] = '1'
d.count = 1
d.point += 1
trim_zeros(d)
if d.count == 1 and d.digits[0] == '0':
d.count = 0
## The exact decimal digits of finite |value| rounded half to even to `places` after the
## point (before it, for a negative `places`), as dtoa's mode 3 makes them for Python's
## `round(x, places)`: no trailing zero, zero as `0` at point 1, and no digit at all, at
## point `-places`, when the rounding leaves nothing.
pub func decimal_places(value: f64, places: i64) -> Decimal:
return digits_of(value, 3, places)
## dtoa's mode 2 (`n` significant digits) or mode 3 (`n` digits after the point, any sign)
## of a finite |value|: rounded half to even, no trailing zero, zero as `0` at point 1, and
## nothing left at point `-n` when mode 3 rounds it all away.
func digits_of(value: f64, mode: u8, n: i64) -> Decimal:
var d = exact(value)
if d.count == 1 and d.digits[0] == '0':
return d
if mode == 2:
round_to(&d, n if n > 1 else 1)
else:
round_to(&d, d.point + n)
if d.count == 0:
d.point = -n
return d
# mark: Numbers ================================================================================
## A number to lay out: its sign, a base's prefix, the digits `[first, last)` of `digits`
## with the point before index `point` (shown when `dot`), and what follows them, an
## exponent, `%`, or the scalar a `c` field names.
struct Number:
var negative: bool
var prefix: str
var digits: Decimal
var first: i64
var point: i64
var last: i64
var dot: bool
var rest: u8[8]
var rest_length: usize
## The digits a group of `interval` with zeros reaching `least` scalars make, CPython's
## _PyUnicode_InsertThousandsGrouping: `n` digits, at least as many as reach `least` with a
## separator between each group; no groups when `interval` is 0.
func padded_digits(n: i64, least: i64, interval: i64) -> i64:
var count = n if n > 0 else 1
if interval == 0:
return larger(count, least)
while count + (count - 1) // interval < least:
count += 1
return count
## A number laid out as CPython's calc_number_widths and fill_number lay it out:
## `[padding] sign prefix [padding] grouped-digits [point] rest [padding]`. `spec` is settled.
func lay_number(sink: Sink, n: Number*, spec: Spec, upper: bool) -> bool:
var sign: u8 = 0
if spec.sign == '+':
sign = '-' if n.negative else '+'
elif spec.sign == ' ':
sign = '-' if n.negative else ' '
elif n.negative:
sign = '-'
let rest = n.rest[..<n.rest_length]
let fraction = n.last - n.point if n.last > n.point else 0
let fixed = (1 if sign != 0 else 0) + (i64)n.prefix.length + (1 if n.dot else 0) + fraction + scalar_count(rest)
let least = spec.width - fixed if spec.fill == "0" and spec.align == '=' else 0
let interval: i64 = 0 if spec.grouping == 0 else (4 if spec.kind == 'b' or spec.kind == 'o' or spec.kind == 'x' or spec.kind == 'X' else 3)
let whole = n.point - n.first
# a `c` field has no digits, only the scalar it names
let count = padded_digits(whole, least, interval) if whole > 0 or n.rest_length == 0 or n.dot else 0
let body = count + ((count - 1) // interval if interval > 0 and count > 0 else 0)
let spare = spec.width - (fixed + body)
var before: i64 = 0
var between: i64 = 0
var after: i64 = 0
if spare > 0:
if spec.align == '<':
after = spare
elif spec.align == '^':
before = spare // 2
after = spare - before
elif spec.align == '=':
between = spare
else:
before = spare
if not repeat(sink, spec.fill, before):
return false
if sign != 0 and not put_byte(sink, sign):
return false
if not put_cased(sink, n.prefix.bytes, upper) or not repeat(sink, spec.fill, between):
return false
# the whole part's digits, the leftmost `count - whole` of them padding zeros
var k: i64 = 0
while k < count:
if interval > 0 and k > 0 and (count - k) % interval == 0 and not put_byte(sink, spec.grouping):
return false
let digit = n.digits.at(n.point - count + k) if k >= count - whole else '0'
if not put_byte(sink, digit - 32 if upper and digit >= 'a' and digit <= 'z' else digit):
return false
k += 1
if n.dot and not put_byte(sink, '.'):
return false
var i = n.point
while i < n.last:
if not put_byte(sink, n.digits.at(i)):
return false
i += 1
return put_cased(sink, rest, upper) and repeat(sink, spec.fill, after)
## A number with no digits yet: positive, no prefix, nothing after.
func number(digits: Decimal) -> Number:
return Number(negative = false, prefix = "", digits = digits, first = 0, point = digits.point, last = digits.point, dot = false, rest = [0, 0, 0, 0, 0, 0, 0, 0], rest_length = 0)
## `bytes` after the digits of `n`.
func add_rest(n: Number*, bytes: const u8[]):
for b in bytes:
if n.rest_length < 8:
n.rest[n.rest_length] = b
n.rest_length += 1
## An integer field `{value:spec}`, its sign apart from its magnitude, laid out by
## `specification`. A `c` field of a number that names no Unicode scalar traps.
pub func lay_integer(sink: Sink, negative: bool, magnitude: u64, specification: str) -> bool:
let spec = settled(specification, true)
if spec.kind == 'c':
if not is_scalar(negative, magnitude):
trap("a `c` field's number names no Unicode scalar")
var n = number(integer_digits(0, 10))
n.first = n.point
var buffer: u8[4]
add_rest(&n, encode_scalar((u32)magnitude, buffer))
return lay_number(sink, &n, spec, false)
var base: u64 = 10
var prefix = ""
if spec.kind == 'b':
base = 2
prefix = "0b"
elif spec.kind == 'o':
base = 8
prefix = "0o"
elif spec.kind == 'x' or spec.kind == 'X':
base = 16
prefix = "0x"
var n = number(integer_digits(magnitude, base))
n.negative = negative
if spec.alternate:
n.prefix = prefix
return lay_number(sink, &n, spec, spec.kind == 'X')
## Whether the integer of `negative` and `magnitude` names a Unicode scalar, as a `c`
## field's number must: from 0 to U+10FFFF, not a surrogate. A caller that must not trap
## asks before laying a `c` field out.
pub func is_scalar(negative: bool, magnitude: u64) -> bool:
return not negative and magnitude <= 0x10FFFF and (magnitude < 0xD800 or magnitude > 0xDFFF)
## The UTF-8 of scalar `value` in `buffer`, which holds four bytes.
pub func encode_scalar(value: u32, buffer: u8[]) -> const u8[]:
if value < 0x80:
buffer[0] = (u8)value
return buffer[..<1]
if value < 0x800:
buffer[0] = (u8)(0xC0 | (value >> 6))
buffer[1] = (u8)(0x80 | (value & 0x3F))
return buffer[..<2]
if value < 0x10000:
buffer[0] = (u8)(0xE0 | (value >> 12))
buffer[1] = (u8)(0x80 | ((value >> 6) & 0x3F))
buffer[2] = (u8)(0x80 | (value & 0x3F))
return buffer[..<3]
buffer[0] = (u8)(0xF0 | (value >> 18))
buffer[1] = (u8)(0x80 | ((value >> 12) & 0x3F))
buffer[2] = (u8)(0x80 | ((value >> 6) & 0x3F))
buffer[3] = (u8)(0x80 | (value & 0x3F))
return buffer[..<4]
# mark: Floats =================================================================================
## A float field `{value:spec}` laid out by `specification`; `single` when the
## value is an `f32` (or an `f16`), whose display is its own shortest digits. With neither a
## type nor a precision a field shows the value's display, where Python shows its repr.
pub func lay_float(sink: Sink, value: f64, single: bool, specification: str) -> bool:
let spec = settled(specification, true)
var x = value
var kind = spec.kind
var percent = false
if kind == '%':
x = x * 100.0
kind = 'f'
percent = true
var n = number(integer_digits(0, 10))
if kind == 0 and spec.precision < 0:
displayed(&n, x, single, spec.no_negative_zero)
else:
short(&n, x, kind, spec.precision if spec.precision >= 0 else 6, spec.alternate, spec.no_negative_zero)
if percent:
add_rest(&n, "%".bytes)
return lay_number(sink, &n, spec, kind == 'E' or kind == 'F' or kind == 'G')
## The display of `value` (§14.4) as a number to lay out: its digits up to the point, the
## point, and the rest (more digits, an exponent) as they are.
func displayed(n: Number*, value: f64, single: bool, no_negative_zero: bool):
var shown: u8[40] = ---
let length = float_text.display_f32((f32)value, shown) if single else float_text.display_f64(value, shown)
var at: usize = 0
if shown[0] == '-':
n.negative = not (no_negative_zero and value == 0.0)
at = 1
var d: Decimal = ---
d.count = 0
while at < length and shown[at] >= '0' and shown[at] <= '9':
d.digits[d.count] = shown[at]
d.count += 1
at += 1
d.point = (i64)d.count
if at < length and shown[at] == '.':
at += 1
while at < length and shown[at] >= '0' and shown[at] <= '9':
d.digits[d.count] = shown[at]
d.count += 1
at += 1
n.dot = true
n.digits = d
n.first = 0
n.point = d.point
n.last = (i64)d.count
# `inf`, `nan` and an exponent follow the digits as they are
add_rest(n, shown[at..<length])
## `e`, `f`, `g` (and the upper ones) of `value` with `precision`, as CPython's
## PyOS_double_to_string and format_float_short write them: `-` for a negative value,
## `inf` and `nan` for the specials.
func short(n: Number*, value: f64, kind: u8, precision: i64, alternate: bool, no_negative_zero: bool):
let upper = kind == 'E' or kind == 'F' or kind == 'G'
var code = kind + 32 if upper else kind
var add_dot = false
var p = precision
if code == 0:
# a precision without a type: `g`, a point always kept (CPython's 'r' with one)
code = 'g'
add_dot = true
var negative = value < 0.0 or (value == 0.0 and (value.bits() >> 63) != 0)
if value != value or infinite(value):
n.negative = negative and value == value
n.first = n.point
add_rest(n, "nan".bytes if value != value else "inf".bytes)
return
var mode: u8 = 2
if code == 'e':
p += 1
elif code == 'f':
mode = 3
elif p == 0:
p = 1
let d = digits_of(value, mode, p)
if no_negative_zero and (d.count == 0 or (d.count == 1 and d.digits[0] == '0')):
negative = false
var point = d.point
var end = (i64)d.count
var use_exponent = false
if code == 'e':
use_exponent = true
end = p
elif code == 'f':
end = point + p
else:
if point <= -4 or point > (p - 1 if add_dot else p):
use_exponent = true
if alternate:
end = p
var exponent: i64 = 0
if use_exponent:
exponent = point - 1
point = 1
let start = point - 1 if point <= 0 else 0
if not use_exponent and add_dot:
end = end if end > point else point + 1
else:
end = end if end > point else point
n.negative = negative
n.digits = d
n.first = start
n.point = point
n.last = end
n.dot = point < end or alternate
if use_exponent:
add_rest(n, "e".bytes)
add_rest(n, "-".bytes if exponent < 0 else "+".bytes)
let magnitude = (u64)(-exponent if exponent < 0 else exponent)
let e = integer_digits(magnitude, 10)
if magnitude < 10:
add_rest(n, "0".bytes)
add_rest(n, e.digits[..<e.count])
## Whether `x` is an infinity (a NaN is told apart by `x != x`).
func infinite(x: f64) -> bool:
return x == x and ((x.bits() >> 52) & 0x7FF) == 0x7FF
# mark: Text ===================================================================================
## A text field `{value:spec}`, `shown` being its display, laid out by `specification`: precision keeps that many scalars, and the width pads it (CPython's
## format_string_internal).
pub func lay_text(sink: Sink, shown: str, specification: str) -> bool:
let spec = settled(specification, false)
var view = shown.bytes
var n = scalar_count(view)
if spec.precision >= 0 and n > spec.precision:
view = scalar_prefix(view, spec.precision)
n = spec.precision
let (before, after) = padding(n, spec.width, spec.align)
return repeat(sink, spec.fill, before) and put(sink, view) and repeat(sink, spec.fill, after)
## The first `n` scalars of `bytes`, viewing them.
func scalar_prefix(bytes: const u8[], n: i64) -> const u8[]:
var seen: i64 = 0
var i: usize = 0
while i < bytes.length:
if (bytes[i] & 0xC0) != 0x80:
if seen == n:
return bytes[..<i]
seen += 1
i += 1
return bytes
"""
## The text of `src/std/core.lucb`.
let text_6: str = r"""#==============================================================================================
#
# core - The compiler's runtime support
#
# DESCRIPTION:
# The runtime the compiler emits calls to: startup and thread identity, the `format` and
# f-string buffer, integer and boolean formatting, hashing, and the checked, saturating
# and converting integer arithmetic.
#
#==============================================================================================
from windows_abi import WideCharToMultiByte
import c
import float_text
import platform
import memory
import strings
extern func set_binary_mode as "_setmode"(descriptor: i32, mode: i32) -> i32
extern func GetCurrentThreadId() -> u32
var initial_thread: u32
## Whether the caller is the process's initial thread.
pub func on_initial_thread() -> bool:
if platform.windows:
return initial_thread != 0 and GetCurrentThreadId() == initial_thread
return false
## Standard streams carry language bytes, including LF, without CRT text translation.
pub func process_start():
if platform.windows:
initial_thread = GetCurrentThreadId()
_ = set_binary_mode(0, 32768)
_ = set_binary_mode(1, 32768)
_ = set_binary_mode(2, 32768)
extern func posix_write as "write"(descriptor: i32, buffer: const void*, count: usize) -> isize
extern func windows_write as "_write"(descriptor: i32, buffer: const void*, count: u32) -> i32
func write(descriptor: i32, buffer: const void*, count: usize) -> isize:
if platform.windows:
return (isize)windows_write(descriptor, buffer, (u32)(count if count < 2147483647 else 2147483647))
return posix_write(descriptor, buffer, count)
extern func c_exit as "exit"(code: i32) -> never
extern func malloc(size: usize) -> void*?
extern func realloc(block: void*?, size: usize) -> void*?
extern func c_free as "free"(block: void*?)
extern func memcpy(target: void*, source: const void*, count: usize) -> void*
extern func memmove(target: void*, source: const void*, count: usize) -> void*
extern func memcmp(a: const void*, b: const void*, count: usize) -> i32
extern func strlen(text: c.str) -> usize
## Stop the program with its reason on stderr (§8.7).
pub func trap(message: c.str) -> never:
begin_trap()
write_diagnostic("trap: ".bytes)
write_diagnostic(((str)message).bytes)
write_diagnostic("\n".bytes)
finish_trap()
## `trap: file:line:column: message`: a trap naming the statement it stopped at (§11.5).
pub func trap_at(position: c.str, message: c.str) -> never:
diagnostic_location(position)
write_diagnostic(((str)message).bytes)
write_diagnostic("\n".bytes)
finish_trap()
## Positioned diagnostic with NUL-terminated C strings. Base assertions use
## trap_detail_at so their message spans retain their explicit lengths.
pub func trap_two_at(position: c.str, message: c.str, detail: c.str) -> never:
diagnostic_location(position)
write_diagnostic(((str)message).bytes)
write_diagnostic(": ".bytes)
write_diagnostic(((str)detail).bytes)
write_diagnostic("\n".bytes)
finish_trap()
## Unpositioned diagnostic with NUL-terminated C strings.
pub func trap_two(message: c.str, detail: c.str) -> never:
begin_trap()
write_diagnostic("trap: ".bytes)
write_diagnostic(((str)message).bytes)
write_diagnostic(": ".bytes)
write_diagnostic(((str)detail).bytes)
write_diagnostic("\n".bytes)
finish_trap()
## Fatal diagnostic for a Base string. Its explicit byte length permits slices,
## empty values and embedded NUL; no byte beyond the view is read.
pub func trap_text_at(position: c.str, message: str) -> never:
diagnostic_location(position)
write_diagnostic(message.bytes)
write_diagnostic("\n".bytes)
finish_trap()
## `trap(f"...")` streams its message (`stream_put`), so a message longer than a format
## buffer is whole: the location first, then the message to descriptor 2, then `trap_end`.
pub func trap_begin_at(position: c.str):
diagnostic_location(position)
## The end of a streamed trap message, whose newline the stream wrote: the report, and
## the exit.
pub func trap_end() -> never:
finish_trap()
## Assertion prefix plus a Base string detail, preserving the detail's length.
pub func trap_detail_at(position: c.str, message: c.str, detail: str) -> never:
diagnostic_location(position)
let prefix = (str)message
write_diagnostic(prefix.bytes)
write_diagnostic(": ".bytes)
write_diagnostic(detail.bytes)
write_diagnostic("\n".bytes)
finish_trap()
## A trap at a position given as Base text, none when it is empty: how the value
## language's runtime and its interpreter stop.
pub func trap_located(position: str, message: str) -> never:
begin_trap()
write_diagnostic("trap: ".bytes)
if position.length > 0:
write_diagnostic(position.bytes)
write_diagnostic(": ".bytes)
write_diagnostic(message.bytes)
write_diagnostic("\n".bytes)
finish_trap()
## A trap with the failure that led to it on a second line, ` failed at file:line:column:
## cause`, or ` failed: cause` when it names no position.
pub func trap_located_after(position: str, message: str, cause: str, cause_at: str) -> never:
begin_trap()
write_diagnostic("trap: ".bytes)
if position.length > 0:
write_diagnostic(position.bytes)
write_diagnostic(": ".bytes)
write_diagnostic(message.bytes)
if cause_at.length > 0:
write_diagnostic("\n failed at ".bytes)
write_diagnostic(cause_at.bytes)
write_diagnostic(": ".bytes)
else:
write_diagnostic("\n failed: ".bytes)
write_diagnostic(cause.bytes)
write_diagnostic("\n".bytes)
finish_trap()
## `trap: file:line:column: `, or `trap: ` when the position is empty (a C build's code
## before its first statement).
func diagnostic_location(position: c.str):
begin_trap()
write_diagnostic("trap: ".bytes)
let location = (str)position
if location.length > 0:
write_diagnostic(location.bytes)
write_diagnostic(": ".bytes)
## Panic reporting cannot raise another language error. The diagnostic goes to
## stderr and, when crash reports are on, to the report as well.
func write_diagnostic(data: const u8[]):
write_all(2, data)
if crash_file >= 0:
write_all(crash_file, data)
## Retry interruptions and short writes; stop on a terminal write failure or lack of progress.
func write_all(descriptor: i32, data: const u8[]):
var written: usize = 0
while written < data.length:
let remaining = data.length - written
let count = remaining if remaining < 1073741824 else 1073741824
let result = write(descriptor, data[written..].data, count)
if result < 0 and c.errno() == c.interrupted:
continue
if result <= 0:
return
written += (usize)result
# mark: Crash reports ==========================================================================
extern func posix_open as "open"(path: c.str, flags: i32, ...) -> i32
extern func windows_open as "_open"(path: c.str, flags: i32, ...) -> i32
extern func posix_close as "close"(descriptor: i32) -> i32
extern func windows_close as "_close"(descriptor: i32) -> i32
extern func backtrace(frames: void**, size: i32) -> i32
extern func backtrace_symbols_fd(frames: const void**, size: i32, descriptor: i32)
extern func RtlCaptureStackBackTrace(skip: u32, count: u32, frames: void**, hash: u32*?) -> u16
extern func GetModuleHandleExA(flags: u32, address: const void*, module: void**) -> i32
extern func GetModuleFileNameA(module: void*, name: u8*, size: u32) -> u32
extern func c_quick_exit as "_exit"(code: i32) -> never
extern func dladdr(address: const void*, info: DlInfo*) -> i32
extern func getcontext(context: void*) -> i32
extern func RtlLookupFunctionEntry(pc: u64, image_base: u64*, history: void*?) -> void*?
extern func RtlVirtualUnwind(kind: u32, image_base: u64, pc: u64, entry: void*, context: void*, handler_data: void**, establisher: u64*, pointers: void*?) -> void*?
## `Dl_info`: the module and nearest symbol `dladdr` finds for an address.
extern struct DlInfo:
file: c.str?
base: usize
symbol: c.str?
start: usize
## Where a crash report goes, NUL-terminated, and what it starts with; empty when
## reports are off. `std.crash` fills them; a trap or fatal signal writes them.
var crash_path: u8[1024]
var crash_header: u8[1024]
var crash_header_length: usize = 0
var crash_file: i32 = -1
## Set while a report walks frame pointers: a fault then is the walk's own, at a
## frame it could not read, and the report ends there.
var crash_walking: bool = false
## Where `getcontext` puts the registers of a trap on Linux: more than any
## `ucontext_t` needs, as words so it is aligned.
var crash_registers: usize[1024]
## A Windows fault's CONTEXT, copied so it can be unwound in place: 1232 bytes
## that must start on 16, with room to move them there.
var crash_windows_context: u8[1280]
## The interrupted context of the fatal signal being reported, when there is one.
var crash_context: const void*? = none
## Whether this process has written a report: a later trap, one in a crash handler,
## is added to it rather than replacing it.
var crash_reported: bool = false
## What runs once a report is written, before the process ends (`set_crash_handler`).
var crash_handler: (func(i32) -> unit)? = none
## Turn crash reports on: a trap from now on also writes `header` and the trap
## to the file at `path` (created then, replaced if present), with a native
## stack where the platform gives one. Paths or headers too long are cut.
pub func set_crash_report(path: str, header: str):
let length = path.length if path.length < crash_path.length - 1 else crash_path.length - 1
for index in 0..<length:
crash_path[index] = path.bytes[index]
crash_path[length] = 0
let header_length = header.length if header.length < crash_header.length else crash_header.length
for index in 0..<header_length:
crash_header[index] = header.bytes[index]
crash_header_length = header_length
## Whether crash reports are on.
pub func crash_reports_enabled() -> bool:
return crash_path[0] != 0
## Run `handler` once a crash report is written, before the process ends: with 0 after
## a trap, on the thread that trapped, where the program may still do ordinary work;
## with the signal's number after a fatal signal, inside the signal handler, where only
## async-signal-safe calls are allowed. The process ends when the handler returns. A
## trap inside the handler is added to the report and runs the handler again.
pub func set_crash_handler(handler: (func(i32) -> unit)?):
crash_handler = handler
## Open the report, if reports are on and it is not open yet, and write its header.
## The start of every trap and fatal signal: the running test, if any, named as failed
## (§16.5), then the crash report opened.
func begin_trap():
if test_running.length > 0:
write_all(1, "FAIL ".bytes)
write_all(1, test_running.bytes)
write_all(1, "\n".bytes)
begin_crash_report()
## The end of a trap or fatal signal inside a test: the test counted as failed and the
## tally of the tests run so far printed, since the run ends here (§16.5).
func end_trapped_test():
if test_running.length == 0:
return
test_running = ""
tests_failed += 1
_ = test_summary()
func begin_crash_report():
if crash_file >= 0 or crash_path[0] == 0:
return
let path = (c.str)&crash_path[0]
# write, create, and truncate the first time; append after
if platform.windows:
crash_file = windows_open(path, 1 | 256 | (8 if crash_reported else 512) | 32768, 420)
elif platform.macos:
crash_file = posix_open(path, 1 | 512 | (8 if crash_reported else 1024), 420)
else:
crash_file = posix_open(path, 1 | 64 | (1024 if crash_reported else 512), 420)
if crash_file < 0:
return
if crash_reported:
write_all(crash_file, "\n(and while the program handled that crash)\n".bytes)
else:
write_all(crash_file, crash_header[..<crash_header_length])
crash_reported = true
## Finish a trap: the stack into the report, close it, the crash handler, and exit.
func finish_trap() -> never:
end_trapped_test()
finish_crash_report(trap_registers())
if let handler = crash_handler:
handler(0)
c_exit(1)
## Where a fault left the program: its instruction, its frame pointer and, on
## arm64, its link register; zeros where they are not known.
pub struct FaultRegisters:
## The faulting instruction.
pub var pc: usize = 0
## The frame pointer at the fault: the start of the frame chain.
pub var fp: usize = 0
## The link register (arm64): the caller of a function that made no frame.
pub var lr: usize = 0
## Write the stack into the report and close it: walked from `at` when it has a
## frame pointer, else as the platform's own backtrace gives it.
func finish_crash_report(at: FaultRegisters):
if crash_file < 0:
return
write_all(crash_file, "\nstack:\n".bytes)
var unwound = false
if platform.windows:
if let context = crash_context:
write_windows_unwound(crash_file, context)
unwound = true
if unwound:
close_crash_report()
return
if at.fp != 0:
write_walked_stack(crash_file, at)
elif platform.posix:
# Pointer-sized slots: the addresses backtrace fills in.
var frames: usize[64]
let depth = backtrace((void**)&frames[0], 64)
if depth > 0:
backtrace_symbols_fd((const void**)&frames[0], depth, crash_file)
elif platform.windows:
write_windows_stack(crash_file)
close_crash_report()
func close_crash_report():
if platform.windows:
_ = windows_close(crash_file)
else:
_ = posix_close(crash_file)
crash_file = -1
## A trap's own registers on Linux, where `backtrace` needs unwind tables the
## native backend leaves out of release builds; zeros elsewhere (macOS's
## backtrace follows frame pointers, Windows' the SEH tables).
func trap_registers() -> FaultRegisters:
if platform.linux:
if getcontext((void*)&crash_registers[0]) != 0:
return FaultRegisters()
return context_registers((const void*)&crash_registers[0])
else:
return FaultRegisters()
# mark: Walking frame pointers =================================================================
## The registers in a signal's `ucontext_t` (or `getcontext`'s), or in a Windows
## `CONTEXT` record. Offsets are each platform's C layout.
func context_registers(context: const void*) -> FaultRegisters:
let base = (const u8*)context
if platform.windows:
return FaultRegisters(pc = word_at(base, 248), fp = word_at(base, 160))
elif platform.linux and platform.x86_64:
# uc_mcontext.gregs at 40: RIP is register 16, RBP 10.
return FaultRegisters(pc = word_at(base, 168), fp = word_at(base, 120))
elif platform.macos:
# uc_mcontext is a pointer at 48, to the exception state (16 bytes) and the thread state.
let machine = (const u8*)(const void*)word_at(base, 48)
if platform.arm64:
return FaultRegisters(pc = word_at(machine, 272), fp = word_at(machine, 248), lr = word_at(machine, 256))
else:
return FaultRegisters(pc = word_at(machine, 144), fp = word_at(machine, 64))
else:
return FaultRegisters()
func word_at(base: const u8*, offset: usize) -> usize:
return ((const usize*)(const void*)(base + offset))[0]
## Every frame from `at`: the faulting instruction, the link register on arm64
## (a leaf keeps its caller there), then each saved return address up the
## chain of frame records, which the native backend keeps in every function.
## A record that does not climb the stack ends the walk; one that cannot be
## read faults, and `report_fatal_signal_in` ends the report there.
func write_walked_stack(file: i32, at: FaultRegisters):
var frames: usize[64] = ---
var depth: usize = 0
if at.pc != 0:
frames[0] = at.pc
depth = 1
# The link register is the caller only when the faulting function made no
# frame of its own; otherwise it is a stale return inside it. Marked so.
var link: usize = 64
if at.lr != 0 and at.lr != at.pc:
link = depth
frames[depth] = at.lr
depth += 1
var frame = at.fp
crash_walking = true
while depth < 64 and frame != 0 and frame % 8 == 0:
let record = (const usize*)(const void*)frame
let caller = record[1]
let next = record[0]
if caller == 0:
break
if depth == 0 or caller != frames[depth - 1]:
frames[depth] = caller
depth += 1
if next <= frame or next - frame > 16777216:
break
frame = next
crash_walking = false
for index in 0..<depth:
write_frame(file, index, frames[index], index == link)
## Windows: unwind a copy of the fault's CONTEXT a frame at a time through the
## SEH tables the native backend emits (its frame pointer sits below each
## frame's locals, so there are no frame records to follow). A function with no
## table entry is a leaf, whose return address is at the stack pointer.
func write_windows_unwound(file: i32, context: const void*):
if platform.windows:
let start = (usize)(const void*)&crash_windows_context[0]
let copy = &crash_windows_context[(16 - start % 16) % 16]
_ = memcpy(copy, context, 1232)
var frames: usize[64] = ---
var depth: usize = 0
crash_walking = true
while depth < 64:
let pc = word_at(copy, 248)
let sp = word_at(copy, 152)
if pc == 0:
break
frames[depth] = pc
depth += 1
var base: u64 = 0
if let entry = RtlLookupFunctionEntry((u64)pc, &base, none):
var data: void* = ---
var establisher: u64 = 0
_ = RtlVirtualUnwind(0, base, (u64)pc, entry, (void*)copy, &data, &establisher, none)
else:
put_word(copy, 248, ((const usize*)(const void*)sp)[0])
put_word(copy, 152, sp + 8)
if word_at(copy, 152) <= sp:
break
crash_walking = false
for index in 0..<depth:
write_frame(file, index, frames[index], false)
func put_word(base: u8*, offset: usize, value: usize):
((usize*)(void*)(base + offset))[0] = value
## One frame's line: its number, address, module and offset in it (for
## addr2line or atos), and the nearest symbol where the platform names one.
func write_frame(file: i32, index: usize, address: usize, link: bool):
var line: u8[640] = ---
var used: usize = 0
line[0] = '#'
used = put_decimal(line, 1, index)
line[used] = ' '
if link:
used = put_text(line, used + 1, "lr")
line[used] = ' '
used = put_hex(line, used + 1, address)
line[used] = ' '
used += 1
if platform.windows:
used = put_windows_module(line, used, address)
elif platform.posix:
var info: DlInfo = ---
info.file = none
info.symbol = none
if dladdr((const void*)address, &info) != 0:
if let file_name = info.file:
used = put_base_name(line, used, (str)file_name)
line[used] = '+'
used = put_hex(line, used + 1, address - info.base)
if let symbol = info.symbol:
line[used] = ' '
used = put_text(line, used + 1, (str)symbol)
line[used] = '+'
used = put_hex(line, used + 1, address - info.start)
else:
line[used] = '?'
used += 1
line[used] = '\n'
write_all(file, (const u8[])line[..<used + 1])
## The module holding `address` on Windows, as `name+0xoffset`, into `line` at `used`.
func put_windows_module(line: u8[], used: usize, address: usize) -> usize:
if platform.windows:
var module: void* = ---
var name: u8[260] = ---
# 4: from an address; 2: without changing the module's reference count
if GetModuleHandleExA(6, (const void*)address, (void**)&module) == 0:
line[used] = '?'
return used + 1
let length = GetModuleFileNameA(module, &name[0], 260)
let at = put_base_name(line, used, (str)name[..<length])
line[at] = '+'
return put_hex(line, at + 1, address - (usize)module)
else:
return used
## The last component of `path` into `line` at `used`, cut to fit.
func put_base_name(line: u8[], used: usize, path: str) -> usize:
var start: usize = 0
for index in 0..<path.length:
if path.bytes[index] == '/' or path.bytes[index] == '\\':
start = index + 1
return put_text(line, used, (str)path.bytes[start..])
## `text` into `line` at `used`, leaving room for what follows it.
func put_text(line: u8[], used: usize, text: str) -> usize:
var at = used
for byte in text.bytes:
if at + 40 >= line.length:
break
line[at] = byte
at += 1
return at
## `value` in decimal into `line` at `used`; the new length.
func put_decimal(line: u8[], used: usize, value: usize) -> usize:
var digits: u8[24] = ---
var count: usize = 0
var rest = value
while true:
digits[count] = (u8)('0' + (u8)(rest % 10))
count += 1
rest = rest // 10
if rest == 0:
break
var at = used
while count > 0:
count -= 1
line[at] = digits[count]
at += 1
return at
## The native stack on Windows, a frame a line as `module+0xoffset`, which a debugger or
## addr2line resolves against the build. Nothing here allocates.
func write_windows_stack(file: i32):
# One branch on the platform around everything, which the C backend folds away
# elsewhere: the Windows symbols then appear in no other platform's build.
if platform.windows:
write_all(file, "(the reporter's frames and Windows' exception dispatch come first; the fault is the\n first program frame after ntdll.dll. Offsets are from each module's load address;\n add the image base, 0x140000000 by default for a 64-bit program, for addr2line.)\n".bytes)
var frames: usize[64]
let depth = (usize)RtlCaptureStackBackTrace(0, 64, (void**)&frames[0], none)
for index in 0..<depth:
let address = frames[index]
var line: u8[400] = ---
var used: usize = 0
var module: void* = ---
var base: usize = 0
var name: u8[260] = ---
var length: u32 = 0
# 4: from an address; 2: without changing the module's reference count
if GetModuleHandleExA(6, (const void*)address, (void**)&module) != 0:
base = (usize)module
length = GetModuleFileNameA(module, &name[0], 260)
var start: u32 = 0
for at in 0..<length:
if name[at] == '\\' or name[at] == '/': start = at + 1
for at in start..<length:
if used < 300:
line[used] = name[at]
used += 1
if length == 0:
line[used] = '?'
used += 1
line[used] = '+'
used += 1
used = put_hex(line, used, address - base)
line[used] = '\n'
used += 1
write_all(file, (const u8[])line[..<used])
## `value` as `0x` and lowercase hex digits into `line` at `used`; the new length.
func put_hex(line: u8[], used: usize, value: usize) -> usize:
var at = used
line[at] = '0'
line[at + 1] = 'x'
at += 2
var shift: usize = 60
var started = false
while true:
let digit = (value >> shift) & 15
if digit != 0 or started or shift == 0:
started = true
line[at] = "0123456789abcdef".bytes[digit]
at += 1
if shift == 0:
break
shift -= 4
return at
## A fatal signal: report it as a trap would, then end with the signal's exit
## status. Only async-signal-safe calls are made from here on.
pub func report_fatal_signal(name: str, number: i32) -> never:
report_fatal_signal_in(name, number, none)
## A fatal signal whose handler has the interrupted context (a POSIX
## `ucontext_t`, or a Windows `CONTEXT`): the stack is walked from where the
## fault happened rather than from the handler. A fault while the stack is
## walked ends the report at the frame that could not be read.
pub func report_fatal_signal_in(name: str, number: i32, context: const void*?) -> never:
if crash_walking:
crash_walking = false
if crash_file >= 0:
write_all(crash_file, "(the walk stopped at a frame it could not read)\n".bytes)
close_crash_report()
c_quick_exit(128 + number)
begin_trap()
write_diagnostic("trap: fatal signal ".bytes)
write_diagnostic(name.bytes)
write_diagnostic("\n".bytes)
end_trapped_test()
crash_context = context
if let interrupted = context:
finish_crash_report(context_registers(interrupted))
else:
finish_crash_report(FaultRegisters())
if let handler = crash_handler:
handler(number)
c_quick_exit(128 + number)
## The buffer `format` and f"..." fill: the bytes, their capacity, and the count used.
pub struct FormatBuffer:
## The buffer's bytes.
pub var data: u8*
## The buffer's capacity in bytes.
pub var capacity: usize
## The number of bytes used.
pub var used: usize
## What text past the capacity does: `format_fixed`, it is cut; `format_grows`, the
## text moves to the heap and goes on there; `format_grown`, it is on the heap.
pub var growth: usize = 0
## A buffer of the caller's, as `format` fills: text past its capacity is cut.
pub let format_fixed: usize = 0
## A buffer the compiler made for a formatted argument (§5.5): it starts on the stack, moves
## to the heap when the text outgrows it, and is released once the call it feeds returns.
pub let format_grows: usize = 1
## A growing buffer whose text is on the heap, which `format_release` frees.
pub let format_grown: usize = 2
## Append `count` bytes; 1 when they do not fit: the bytes that fit are kept, and the buffer
## is then over capacity, which `format` reports and `format_finish` marks. A growing
## buffer makes room instead, and fails only when the heap has none.
pub func format_put(b: FormatBuffer*, text: const void*?, count: usize) -> i32:
if b.used > b.capacity:
return 1
if count > b.capacity - b.used and not (b.growth != format_fixed and format_grow(b, b.used + count)):
if let source = text:
if b.capacity > b.used:
_ = memcpy(&b.data[b.used], source, b.capacity - b.used)
b.used = b.capacity +% 1
return 1
if let source = text:
if count > 0:
_ = memcpy(&b.data[b.used], source, count)
b.used += count
return 0
## Make room in a growing buffer for `need` bytes and the NUL `format_finish` puts after
## them, at least doubling it; false when the heap has no room.
func format_grow(b: FormatBuffer*, need: usize) -> bool:
var size = b.capacity * 2
if size <= need:
size = need + 1
if b.growth == format_grown:
let moved = realloc(b.data, size) else return false
b.data = (u8*)moved
else:
let fresh = malloc(size) else return false
_ = memcpy(fresh, b.data, b.used)
b.data = (u8*)fresh
b.growth = format_grown
b.capacity = size
return true
## Free what a growing buffer took from the heap; its text is gone with it.
pub func format_release(b: FormatBuffer*):
if b.growth == format_grown:
c_free(b.data)
b.growth = format_grows
b.capacity = 0
b.used = 0
## Append the decimal `value` to `b`; the bytes written.
pub func format_u64(b: FormatBuffer*, value: u64) -> i32:
var digits: u8[24] = ---
var n: usize = 0
var v = value
if v == 0:
digits[23] = 48
n = 1
while v != 0:
n += 1
digits[24 - n] = (u8)(48 + v % 10)
v = v // 10
return format_put(b, &digits[24 - n], n)
## Append the decimal `value` to `b`; the bytes written.
pub func format_i64(b: FormatBuffer*, value: i64) -> i32:
if value < 0:
let minus = "-"
if format_put(b, minus.bytes.data, 1) != 0:
return 1
return format_u64(b, 0 -% (u64)value)
return format_u64(b, (u64)value)
## A float field (base.md §14): the shortest decimal that reads back as `value`, a `.0`
## after digits with no point (float_text.display_f64); both backends call this.
pub func format_f64(b: FormatBuffer*, value: f64) -> i32:
var text: u8[40] = ---
let n = float_text.display_f64(value, text[0..])
return format_put(b, &text[0], n)
## format_f64 for an f32 (or an f16, widened): the f32's own shortest digits.
pub func format_f32(b: FormatBuffer*, value: f32) -> i32:
var text: u8[40] = ---
let n = float_text.display_f32(value, text[0..])
return format_put(b, &text[0], n)
## Append `true` or `false` to `b`; the bytes written.
pub func format_bool(b: FormatBuffer*, value: bool) -> i32:
let word = "true" if value else "false"
return format_put(b, word.bytes.data, word.length)
## A `char` displays as itself, in UTF-8 (§14.4).
pub func format_char(b: FormatBuffer*, value: char) -> i32:
let cp = u32(value)
var out: u8[4]
var n: usize = 0
if cp < 0x80:
out[0] = u8(cp)
n = 1
elif cp < 0x800:
out[0] = u8(0xC0 | (cp >> 6))
out[1] = u8(0x80 | (cp & 0x3F))
n = 2
elif cp < 0x10000:
out[0] = u8(0xE0 | (cp >> 12))
out[1] = u8(0x80 | ((cp >> 6) & 0x3F))
out[2] = u8(0x80 | (cp & 0x3F))
n = 3
else:
out[0] = u8(0xF0 | (cp >> 18))
out[1] = u8(0x80 | ((cp >> 12) & 0x3F))
out[2] = u8(0x80 | ((cp >> 6) & 0x3F))
out[3] = u8(0x80 | (cp & 0x3F))
n = 4
return format_put(b, (const void*)&out, n)
## The text so far, NUL-terminated when there is room. Text over capacity is cut at a
## character boundary and ends in `...`, so a long f"..." is never silently empty.
pub func format_finish(b: FormatBuffer*) -> str:
if b.used < b.capacity:
b.data[b.used] = 0
if b.used > b.capacity:
if b.capacity < 3:
return ""
var end = b.capacity - 3
while end > 0 and (b.data[end] & 0xC0) == 0x80:
end -= 1
b.data[end] = 46
b.data[end + 1] = 46
b.data[end + 2] = 46
return (str)u8[](b.data, end + 3)
return (str)u8[](b.data, b.used)
## The startup shim's pieces: the argument vector as `str[]` (checked) or `c.str[]`.
pub func arguments(count: i32, vector: c.str*) -> str[]:
let raw = raw_arguments(count, vector)
let n = raw.length
let block = malloc((n if n > 0 else 1) * memory.size_of(str)) else trap("memory.exhausted")
let items = (str*)block
var i: usize = 0
while i < n:
let text = (str)raw[i]
if not utf8_ok(text.bytes.data, text.length):
trap("invalid_utf8")
items[i] = text
i += 1
return str[](items, n)
## The process's arguments as a span of C strings.
pub func raw_arguments(count: i32, vector: c.str*) -> c.str[]:
if platform.windows:
return windows_arguments()
return c.str[](vector, (usize)count)
extern func GetCommandLineW() -> u16*
extern func CommandLineToArgvW(command: u16*, count: i32*) -> u16**?
extern func LocalFree(block: void*) -> void*?
extern func atexit(callback: func() -> unit) -> i32
var windows_argument_vector: c.str[]
func free_windows_arguments():
for text in windows_argument_vector:
c_free((void*)text)
c_free((void*)windows_argument_vector.data)
## The wide command line preserves names the Windows ANSI code page cannot encode.
func windows_arguments() -> c.str[]:
if platform.windows:
if windows_argument_vector.length > 0:
return windows_argument_vector
var count: i32 = 0
let wide = CommandLineToArgvW(GetCommandLineW(), &count) else trap("could not read the Windows command line")
defer _ = LocalFree((void*)wide)
let block = malloc((usize)count * memory.size_of(c.str)) else trap("memory.exhausted")
let items = (c.str*)block
for index in 0..<(usize)count:
let size = WideCharToMultiByte(65001, 128, wide[index], -1, none, 0, none, none)
if size <= 0: trap("the command line contains invalid UTF-16")
let text = malloc((usize)size) else trap("memory.exhausted")
if WideCharToMultiByte(65001, 128, wide[index], -1, (u8*)text, size, none, none) != size:
trap("could not encode the command line as UTF-8")
items[index] = (c.str)text
windows_argument_vector = c.str[](items, (usize)count)
if atexit(free_windows_arguments) != 0:
trap("could not register command line cleanup")
return windows_argument_vector
return c.str[]((c.str*)0, 0)
## The error of the fallible call that failed last on this thread, as the native backends
## pass it: a failing function leaves its error here and returns its flag set, and `try`
## passes the flag up with the error left where it is (§11.2's representation).
pub local var failure: Error
# mark: Messages in flight ==================================================================
#
# `error(code, message)` copies a message that is not a literal here, so the message stays
# readable after the cleanup the failure runs has freed what it was made from (§11.3). The
# room is a stack: a handler that finishes gives back its error's message and everything
# raised after it (`message_handled`), and a failure that leaves a handler takes the place of
# the handled message (`message_raised`'s `replaces`). A message that does not fit above the
# top starts the room again at its beginning, so the room never grows and never allocates.
## The room for the messages of the errors in flight on this thread, and its first free byte.
local var raised_room: u8[16384]
local var raised_top: usize = 0
## The longest message the room keeps: a longer one is cut at a character and ends in `…`.
let raised_most: usize = 4096
## Where `text` starts in this thread's room, or `none` when it lies elsewhere. Only its
## start is read: a message written here lies here whole, and the length an error passes in
## carries its `called_at` above 32 bits (§11.3).
func in_room(text: str) -> usize?:
let base = (usize)&raised_room[0]
let at = (usize)text.bytes.data
if at < base or at > base + raised_room.length:
return none
return at - base
## The message of an error being raised, copied into this thread's room: a copy that lives
## until the handler that catches the error finishes (§11.3). `replaces` is the message of the
## error a handler is passing on a failure from, or empty: its place is reused when it is in
## the room, the new message copied before the old one is given up, so a new message made
## from the old one reads it whole.
pub func message_raised(message: str, replaces: str) -> str:
var length = message.length
var cut = false
if length > raised_most:
length = raised_most - 3
while length > 0 and (message.bytes[length] & 0xC0) == 0x80:
length -= 1
cut = true
let total = length + (3 if cut else 0)
var at = raised_top
if let start = in_room(replaces):
if start <= raised_top and start + total <= raised_room.length:
at = start
if at + total > raised_room.length:
at = 0
let room = u8[](&raised_room[at], total)
if length > 0:
_ = memmove(room.data, message.bytes.data, length)
if cut:
room[length] = 0xE2
room[length + 1] = 0x80
room[length + 2] = 0xA6
raised_top = at + total
return (str)room
## A handler finished with the error whose message is `message`: its place in the room and
## everything raised after it are free again. A message elsewhere, a literal, frees nothing.
pub func message_handled(message: str):
if let start = in_room(message):
if start < raised_top:
raised_top = start
# mark: Where errors were raised ============================================================
#
# An error keeps where it was raised (§11.3), the position text of the statement a trap there
# would name, in the word after its code: the text's distance from `origin_base`, a byte of
# this program's own data, so the `Error` stays three words and a `T!` result does not grow.
# The texts and the byte are in one image, well within 2 GiB of each other; 0 is no position.
# `called_at`, the first call into another package the error came out of, is kept the same
# way in the word above the message's length, which is 32 bits: a new message clears it.
## The byte an error's `at` is counted from: generated code subtracts its address from the
## position text's.
pub var origin_base: u8 = 0
## `failure.at`: the position text `raised` bytes from `origin_base`, `file:line:column`, or
## empty when it is 0.
pub func origin(raised: i32) -> str:
if raised == 0:
return ""
let text = (u8*)(usize)((isize)(usize)&origin_base + (isize)raised)
return (str)(c.str)text
## `failure.called_at`: the position text `crossed` bytes from `origin_base`, or `at` (from
## `raised`) when the error came out of no call into another package (`crossed` is 0).
pub func called(raised: i32, crossed: i32) -> str:
return origin(crossed if crossed != 0 else raised)
## A failed `main`: its error on stderr, `error: file:line:column: message (code n)`, the
## place being where the error was raised, `at`, then ` called at file:line:column` when it
## came out of a call into another package, `called_at`; exit status 1. The code shown is the
## package's own number, `n` of `ErrorCode.package(n)`; the package identity above it (§11.3)
## tells packages apart and means nothing to a reader.
pub func main_failed(code: i32, message: str, raised: i32, crossed: i32) -> i32:
var bytes: u8[1024] = ---
let at = origin(raised)
let number = (u32)code & 0xFFFF
let line = strings.format(bytes, f"error: {at}: {message} (code {number})\n" if at.length > 0 else f"error: {message} (code {number})\n") catch:
return 1
_ = write(2, line.bytes.data, line.length)
let call = origin(crossed)
if call.length > 0 and call != at:
let second = strings.format(bytes, f" called at {call}\n") catch:
return 1
_ = write(2, second.bytes.data, second.length)
return 1
## The failure of a test whose `assert` was false (§16.5).
pub let assert_failed: ErrorCode = ErrorCode.package(60)
## Where the `assert` that failed the running test stands, empty while none has, and the
## room its message is written in, which the failure views until the test is reported.
var assert_position: c.str = ""
var assert_message: u8[1024]
## A false `assert` in a test's own body (§16.5): the test fails with `text`, `assert failed:`
## and the condition, and `detail` after it when there is one, and is reported at `position`,
## the assert's.
pub func test_assert(position: c.str, text: c.str, detail: str, detailed: bool) -> !:
assert_position = position
let condition = (str)text
let message = strings.format(assert_message, f"{condition}: {detail}" if detailed else f"{condition}") catch:
recover condition
error(assert_failed, message)
## The test the runner is running, empty between tests, and the tally so far: a trap or
## fatal signal in a test names it as failed and prints the tally before the run ends.
var test_running: str = ""
var tests_passed: i32 = 0
var tests_failed: i32 = 0
## The runner starts the test named `name`.
pub func test_start(name: str):
test_running = name
## The test runner's report line for the test that ran: `ok`, or `FAIL` with the place and
## the message on the next line, the place being the failed `assert`'s or else the test's
## own, `at` (§16.5).
pub func test_report(failed: bool, at: c.str, message: str):
let place = (str)(assert_position if ((str)assert_position).length > 0 else at)
assert_position = ""
test_result(failed, place, message)
## Report the test that ran and count it: `ok name`, or `FAIL name` and the indented
## `place: message`. Luce's runners report through here too, so every runner prints alike.
pub func test_result(failed: bool, place: str, message: str):
let name = test_running
test_running = ""
if failed:
tests_failed += 1
else:
tests_passed += 1
if not failed:
write_all(1, "ok ".bytes)
write_all(1, name.bytes)
write_all(1, "\n".bytes)
return
write_all(1, "FAIL ".bytes)
write_all(1, name.bytes)
write_all(1, "\n ".bytes)
write_all(1, place.bytes)
write_all(1, ": ".bytes)
write_all(1, message.bytes)
write_all(1, "\n".bytes)
## Print the tally of the tests run, the failures counted when there are any; the process
## exit status. Formats without the heap or `strings`, since a trap may print it.
pub func test_summary() -> i32:
var line: u8[128] = ---
var used = put_decimal(line, 0, (usize)tests_passed)
used = put_text(line, used, " passed\n")
if tests_failed > 0:
used = put_decimal(line, used, (usize)tests_failed)
used = put_text(line, used, " failed\n")
write_all(1, line[..<used])
return 0 if tests_failed == 0 else 1
## `print`: the text and a newline on stdout; `answer()` programs print their number.
pub func print_str(text: str):
_ = write(1, text.bytes.data, text.length)
let newline = "\n"
_ = write(1, newline.bytes.data, 1)
# mark: Streamed text =========================================================================
## `print(f"...")` and `trap(f"...")` fill a format buffer and write it out to `descriptor`
## (1, standard output; 2, the diagnostic) as it fills, so no buffer bounds the text and a
## short line is one write. Append `count` bytes at `data` and leave at least half the
## buffer free, which is room for the fields the compiler displays into it between pieces;
## a field that displays itself longer than that is cut, as `format_finish` marks.
pub func stream_put(b: FormatBuffer*, data: const u8*, count: usize, descriptor: i32):
if b.used > b.capacity or count > b.capacity - b.used:
stream_flush(b, descriptor)
if count >= b.capacity:
stream_write(descriptor, u8[]((u8*)data, count))
return
_ = format_put(b, data, count)
if b.used > b.capacity // 2:
stream_flush(b, descriptor)
## Write out the buffer's text and empty it: the end of a streamed text.
pub func stream_flush(b: FormatBuffer*, descriptor: i32):
if b.used > 0:
stream_write(descriptor, format_finish(b).bytes)
b.used = 0
func stream_write(descriptor: i32, data: const u8[]):
if descriptor == 2:
write_diagnostic(data)
else:
write_all(descriptor, data)
## Print `value` followed by a newline.
pub func print_i64(value: i64):
var bytes: u8[32] = ---
let all: u8[] = bytes
var b = FormatBuffer(data = all.data, capacity = 32, used = 0)
_ = format_i64(&b, value)
_ = write(1, all.data, b.used)
let newline = "\n"
_ = write(1, newline.bytes.data, 1)
## Ordering of text: bytewise, then by length (§7.4).
pub func str_compare(a: str, b: str) -> i32:
let n = a.length if a.length < b.length else b.length
var order: i32 = 0
if n > 0:
order = memcmp(a.bytes.data, b.bytes.data, n)
if order != 0:
return -1 if order < 0 else 1
if a.length < b.length:
return -1
return 1 if a.length > b.length else 0
## The scalar starting at byte `index` of `count` bytes of valid UTF-8, its width left in
## `width`: what `for character in text` steps by (§5.5).
pub func utf8_scalar(text: const u8*, count: usize, index: usize, width: usize*) -> char:
let byte = text[index]
var w: usize = 1
var cp: u32 = (u32)byte
if byte >= 240:
w = 4
cp = (u32)(byte & 7)
elif byte >= 224:
w = 3
cp = (u32)(byte & 15)
elif byte >= 192:
w = 2
cp = (u32)(byte & 31)
if index + w > count:
w = count - index
var k: usize = 1
while k < w:
cp = (cp << 6) | (u32)(text[index + k] & 63)
k += 1
*width = w
return char(cp)
## Whether `count` bytes are well-formed UTF-8: no overlong forms, no surrogates, nothing past U+10FFFF.
pub func utf8_ok(text: const u8*?, count: usize) -> bool:
let s = text else return count == 0
var i: usize = 0
while i < count:
# eight ASCII bytes at a time: source text is mostly ASCII
while i + 8 <= count:
var word: u64 = 0
_ = memcpy(&word, &s[i], 8)
if (word & 0x8080808080808080) != 0:
break
i += 8
if i >= count:
break
let byte = s[i]
var w: usize = 1
var cp: u32 = 0
if byte < 128:
cp = (u32)byte
elif (byte & 224) == 192:
w = 2
cp = (u32)(byte & 31)
elif (byte & 240) == 224:
w = 3
cp = (u32)(byte & 15)
elif (byte & 248) == 240:
w = 4
cp = (u32)(byte & 7)
else:
return false
if i + w > count:
return false
var k: usize = 1
while k < w:
let x = s[i + k]
if (x & 192) != 128:
return false
cp = (cp << 6) | (u32)(x & 63)
k += 1
if w == 2 and cp < 128:
return false
if w == 3 and cp < 2048:
return false
if w == 4 and cp < 65536:
return false
if cp >= 55296 and cp <= 57343:
return false
if cp > 1114111:
return false
i += w
return true
## `hash`: a per-process seed and a multiplicative mixer (§7.4).
var hash_seed_value: u64
var hash_seeded: bool
## A per-process seed for the collection hashers.
# mark: Hashing ===============================================================================
## The process's per-run hash seed, chosen once from the address space.
pub func hash_seed() -> u64:
if not hash_seeded:
hash_seed_value = 11400714819323198485 ^ (u64)(usize)&hash_seed_value
hash_seeded = true
return hash_seed_value
## Mix `x` into the running hash `h`.
pub func hash_mix(h: u64, x: u64) -> u64:
var r = h ^ x
r = r *% 11400714819323198485
r = r ^ (r >> 32)
return r
## Mix `count` bytes at `data` into the running hash `h`.
pub func hash_bytes(h: u64, data: const void*?, count: usize) -> u64:
var r = h
if let p = data:
let bytes = (const u8*)p
var i: usize = 0
while i < count:
r = hash_mix(r, (u64)bytes[i])
i += 1
return hash_mix(r, (u64)count)
## Integer conversions between widths: `bits` of `value` as a 64-bit word.
func mask_of(bits: i32) -> u64:
if bits >= 64:
return 18446744073709551615
return (1 << (u64)bits) - 1
func sign_extend(value: i64, bits: i32) -> i64:
if bits >= 64:
return value
let m = mask_of(bits)
let x = (u64)value & m
if (x & (1 << (u64)(bits - 1))) != 0:
return (i64)(x | ~m)
return (i64)x
func zero_extend(value: u64, bits: i32) -> u64:
return value & mask_of(bits)
func signed_min(bits: i32) -> i64:
if bits >= 64:
return -9223372036854775807 - 1
return 0 -% (1 << (i64)(bits - 1))
func signed_max(bits: i32) -> i64:
if bits >= 64:
return 9223372036854775807
return (1 << (i64)(bits - 1)) - 1
func fits_signed(value: i64, bits: i32) -> bool:
return value >= signed_min(bits) and value <= signed_max(bits)
func fits_unsigned(value: u64, bits: i32) -> bool:
return value <= mask_of(bits)
## The source of an integer conversion as a sign and a magnitude, so a `u64` above
## `i64.max` stays itself.
struct Magnitude:
var negative: bool
var bits: u64
func magnitude(value: u64, from_bits: i32, from_signed: i32) -> Magnitude:
if from_signed != 0:
let v = sign_extend((i64)value, from_bits)
return Magnitude(negative = v < 0, bits = (u64)v)
return Magnitude(negative = false, bits = zero_extend(value, from_bits))
## A checked conversion's result as bits: outside the destination's range traps (§7.5),
## naming the statement at `at`.
func checked_bits(m: Magnitude, to_bits: i32, to_signed: i32, at: c.str) -> u64:
var bad = false
if to_signed != 0:
bad = (i64)m.bits < signed_min(to_bits) if m.negative else m.bits > (u64)signed_max(to_bits)
else:
bad = m.negative or not fits_unsigned(m.bits, to_bits)
if bad:
trap_at(at, "integer conversion out of range")
return m.bits
## `T(x)` between integers (`mode` 0, checked) and `(T)x` (`mode` 1, truncating).
pub func conv_s(value: i64, from_bits: i32, from_signed: i32, to_bits: i32, to_signed: i32, mode: i32, at: c.str) -> i64:
let m = magnitude((u64)value, from_bits, from_signed)
if mode == 0:
return (i64)checked_bits(m, to_bits, to_signed, at)
let bits = m.bits & mask_of(to_bits)
if to_signed != 0:
return sign_extend((i64)bits, to_bits)
return (i64)bits
## Convert an unsigned value between integer widths and signedness; `mode` chooses checked or wrapping.
pub func conv_u(value: u64, from_bits: i32, from_signed: i32, to_bits: i32, to_signed: i32, mode: i32, at: c.str) -> u64:
let m = magnitude(value, from_bits, from_signed)
if mode == 0:
return checked_bits(m, to_bits, to_signed, at)
return zero_extend(m.bits, to_bits)
## A `char` from an integer's bits: a scalar value, or a trap when checked (`mode` 0).
pub func to_char(value: u64, mode: i32, at: c.str) -> u64:
let bad = value > 1114111 or (value >= 55296 and value <= 57343)
if bad and mode == 0:
trap_at(at, "integer conversion out of range")
return value & 4294967295
func is_finite(value: f64) -> bool:
return value - value == 0.0
## `2^(bits-1)` and `2^bits` as floats: exact, where `(f64)signed_max(64)` rounds up to `2^63`.
func float_bound_s(bits: i32) -> f64:
return (f64)((u64)1 << (u64)(bits - 1))
func float_bound_u(bits: i32) -> f64:
return float_bound_s(bits) * 2.0
## Float to integer: checked (`mode` 0) or saturating (`mode` 1); the cast inside is the target's own.
pub func f_to_s(value: f64, bits: i32, mode: i32, at: c.str) -> i64:
let high = float_bound_s(bits)
let low = 0.0 - high
if mode == 0:
if not is_finite(value) or value < low or value >= high:
trap_at(at, "integer conversion out of range")
return (i64)value
if value != value:
return 0
if value <= low:
return signed_min(bits)
if value >= high:
return signed_max(bits)
return (i64)value
## Convert a float to an unsigned integer of `bits`; `mode` checks the range.
pub func f_to_u(value: f64, bits: i32, mode: i32, at: c.str) -> u64:
let high = float_bound_u(bits)
if mode == 0:
if not is_finite(value) or value < 0.0 or value >= high:
trap_at(at, "integer conversion out of range")
return (u64)value
if value != value or value < 0.0:
return 0
if value >= high:
return mask_of(bits)
return (u64)value
## Saturating arithmetic (`+|`, `-|`, `*|`) on `bits`-wide operands in 64-bit words.
func add_overflows_s(a: i64, b: i64, r: i64) -> bool:
return ((a ^ r) & (b ^ r)) < 0
func sub_overflows_s(a: i64, b: i64, r: i64) -> bool:
return ((a ^ b) & (a ^ r)) < 0
func mul_overflows_s(a: i64, b: i64) -> bool:
if a == 0 or b == 0:
return false
if a == -1:
return b == signed_min(64)
if b == -1:
return a == signed_min(64)
return (a *% b) // b != a
## Saturating signed addition at `bits`.
# mark: Saturating and checked arithmetic =====================================================
## Saturating signed addition of `x` and `y` at `bits` bits.
pub func adds_s(x: i64, y: i64, bits: i32) -> i64:
let a = sign_extend(x, bits)
let b = sign_extend(y, bits)
let r = a +% b
if add_overflows_s(a, b, r) or not fits_signed(r, bits):
return signed_min(bits) if a < 0 else signed_max(bits)
return r
## Saturating unsigned addition at `bits`.
pub func adds_u(x: u64, y: u64, bits: i32) -> u64:
let a = zero_extend(x, bits)
let b = zero_extend(y, bits)
let r = a +% b
if r < a or not fits_unsigned(r, bits):
return mask_of(bits)
return r
## Saturating signed subtraction at `bits`.
pub func subs_s(x: i64, y: i64, bits: i32) -> i64:
let a = sign_extend(x, bits)
let b = sign_extend(y, bits)
let r = a -% b
if sub_overflows_s(a, b, r) or not fits_signed(r, bits):
return signed_min(bits) if a < 0 else signed_max(bits)
return r
## Saturating unsigned subtraction at `bits`.
pub func subs_u(x: u64, y: u64, bits: i32) -> u64:
let a = zero_extend(x, bits)
let b = zero_extend(y, bits)
if a < b:
return 0
return a - b
## Saturating signed multiplication at `bits`.
pub func muls_s(x: i64, y: i64, bits: i32) -> i64:
let a = sign_extend(x, bits)
let b = sign_extend(y, bits)
if mul_overflows_s(a, b) or not fits_signed(a *% b, bits):
return signed_min(bits) if (a < 0) != (b < 0) else signed_max(bits)
return a *% b
## Saturating unsigned multiplication at `bits`.
pub func muls_u(x: u64, y: u64, bits: i32) -> u64:
let a = zero_extend(x, bits)
let b = zero_extend(y, bits)
if b != 0 and a > mask_of(bits) // b:
return mask_of(bits)
return zero_extend(a *% b, bits)
## Checked-to-optional arithmetic (`+?`, `-?`, `*?`): the result through `out`, whether it fit.
pub func qadd_s(x: i64, y: i64, bits: i32, out: i64*) -> bool:
let a = sign_extend(x, bits)
let b = sign_extend(y, bits)
let r = a +% b
if add_overflows_s(a, b, r) or not fits_signed(r, bits):
return false
*out = r
return true
## Checked unsigned addition through `out`; whether it fit.
pub func qadd_u(x: u64, y: u64, bits: i32, out: u64*) -> bool:
let a = zero_extend(x, bits)
let b = zero_extend(y, bits)
let r = a +% b
if r < a or not fits_unsigned(r, bits):
return false
*out = r
return true
## Checked signed subtraction through `out`; whether it fit.
pub func qsub_s(x: i64, y: i64, bits: i32, out: i64*) -> bool:
let a = sign_extend(x, bits)
let b = sign_extend(y, bits)
let r = a -% b
if sub_overflows_s(a, b, r) or not fits_signed(r, bits):
return false
*out = r
return true
## Checked unsigned subtraction through `out`; whether it fit.
pub func qsub_u(x: u64, y: u64, bits: i32, out: u64*) -> bool:
let a = zero_extend(x, bits)
let b = zero_extend(y, bits)
if a < b:
return false
*out = a - b
return true
## Checked signed multiplication through `out`; whether it fit.
pub func qmul_s(x: i64, y: i64, bits: i32, out: i64*) -> bool:
let a = sign_extend(x, bits)
let b = sign_extend(y, bits)
if mul_overflows_s(a, b) or not fits_signed(a *% b, bits):
return false
*out = a *% b
return true
## Checked unsigned multiplication through `out`; whether it fit.
pub func qmul_u(x: u64, y: u64, bits: i32, out: u64*) -> bool:
let a = zero_extend(x, bits)
let b = zero_extend(y, bits)
if b != 0 and a > mask_of(bits) // b:
return false
*out = a *% b
return true
## Terminate the process through the minimal runtime layer.
pub func exit(code: i32) -> never:
c_exit(code)
"""
## The text of `src/std/memory.lucb`.
let text_7: str = r"""#==============================================================================================
#
# memory - Allocators and byte primitives
#
# DESCRIPTION:
# The allocators of §12, the thread's current-allocator convention, the diagnostic
# records of an allocation failure, and the byte copy, move, set, read and write helpers.
#
#==============================================================================================
from windows_abi import SystemInfo
from windows_abi import GetSystemInfo
import platform
extern func malloc(size: usize) -> void*?
extern func c_free as "free"(block: void*)
extern func posix_memalign(memptr: void*?*, alignment: usize, size: usize) -> i32
extern func aligned_malloc as "_aligned_malloc"(size: usize, alignment: usize) -> void*?
extern func aligned_free as "_aligned_free"(block: void*)
extern func memcpy(target: void*, source: const void*, count: usize) -> void*
extern func memmove(target: void*, source: const void*, count: usize) -> void*
extern func memset(target: void*, value: i32, count: usize) -> void*
## What every allocator offers: blocks of bytes at an alignment, optionally
## resized in place, and given back (§12.4). Zero alignment means one. A nonempty
## block must meet the requested alignment; an allocator may refuse an unsupported
## alignment. Bump allocators support every positive alignment, not only powers of two.
pub interface Allocator:
func allocate(size: usize, alignment: usize) -> u8[]?
func resize(block: u8[], size: usize) -> bool
func release(block: u8[])
## A bump allocator over a caller's bytes. Only the last block can be resized or
## released; everything goes away together when the buffer does.
pub struct FixedBuffer: Allocator:
var data: u8[]
var used: usize
## An allocator handing out `buffer`'s bytes; only the last block resizes.
pub static func over(buffer: u8[]) -> FixedBuffer:
return FixedBuffer(data = buffer, used = 0)
## Allocate `size` bytes at `alignment`, or none when it cannot.
pub func allocate(size: usize, alignment: usize) -> u8[]?:
if size == 0:
return self.data[..<0]
let align = alignment if alignment > 0 else 1
let start = aligned_offset(self.data, self.used, align) else return none
if start > self.data.length or size > self.data.length - start:
return none
self.used = start + size
return self.data[start..<self.used]
## Try to resize `block` to `size` in place; whether it worked.
pub func resize(block: u8[], size: usize) -> bool:
if size == block.length:
return true
if block.length == 0:
return false
let start = (usize)block.data -? (usize)self.data.data else return false
if start > self.used or block.length != self.used - start:
return false
if start > self.data.length or size > self.data.length - start:
return false
self.used = start + size
return true
## Free `block`.
pub func release(block: u8[]):
if block.length == 0:
return
if diagnostic:
_ = memset(block.data, 221, block.length) # a fixed buffer releases all at once
let start = (usize)block.data - (usize)self.data.data
if start + block.length == self.used:
self.used = start
## Align the address inside a bounded span, without rounding an addition past
## usize's range. The remainder works for arbitrary positive byte alignments.
func aligned_offset(storage: u8[], used: usize, alignment: usize) -> usize?:
if used > storage.length:
return none
let address = (usize)storage.data +? used else return none
let remainder = address % alignment
let padding = 0 if remainder == 0 else alignment - remainder
if padding > storage.length - used:
return none
return used + padding
## The C library's heap.
pub struct CAllocator: Allocator:
var unused: u8
## Allocate `size` bytes at `alignment`, or none when it cannot.
pub func allocate(size: usize, alignment: usize) -> u8[]?:
if size == 0:
return u8[]((u8*)8, 0)
if alignment != 0 and (alignment & (alignment - 1)) != 0:
return none
let align = alignment if alignment > 8 else 8
var p: void*? = none
if platform.windows:
p = aligned_malloc(size, align)
elif posix_memalign(&p, align, size) != 0:
return none
let block = p else return none
return u8[]((u8*)block, size)
## Try to resize `block` to `size` in place; whether it worked.
pub func resize(block: u8[], size: usize) -> bool:
return size <= block.length
## Free `block`.
pub func release(block: u8[]):
if block.length == 0:
return
if not diagnostic:
release_heap(block.data)
return
# the diagnostic profile (§19.4): the block is filled with a pattern and held back, so
# a use after release reads the pattern rather than the next allocation; releasing
# it again, or writing it while it is held, is reported
lock(&records_lock)
if already_held(quarantine, block):
unlock(&records_lock)
trap("double free: a block was released twice")
_ = memset(block.data, (i32)released_fill, block.length)
let slot = quarantine_next % quarantine.length
if let old = quarantine[slot]:
if not still_released(old):
unlock(&records_lock)
trap("use after free: a released block was written after its release")
release_heap(old.data)
quarantine[slot] = block
quarantine_next += 1
unlock(&records_lock)
func release_heap(block: void*):
if platform.windows:
aligned_free(block)
else:
c_free(block)
## Bump allocation over a parent allocator, released all at once by `reset` or `destroy`
## (§12.4, §24.3). One thread at a time.
pub struct Arena: Allocator:
var parent: Allocator
var block: u8[]
var used: usize
## An arena of `capacity` bytes over `parent`, released all at once.
pub static func over(parent: Allocator, capacity: usize) -> Arena!:
let block = parent.allocate(capacity, 16) else error(exhausted, "memory.exhausted")
return Arena(parent = parent, block = block, used = 0)
## Allocate `size` bytes at `alignment`, or none when it cannot.
pub func allocate(size: usize, alignment: usize) -> u8[]?:
if size == 0:
return self.block[..<0]
let align = alignment if alignment > 0 else 1
let start = aligned_offset(self.block, self.used, align) else return none
let end = start +? size else return none
if end > self.block.length:
return none
self.used = end
return self.block[start..<end]
## Try to resize `block` to `size` in place; whether it worked.
pub func resize(block: u8[], size: usize) -> bool:
if size == block.length:
return true
if block.length == 0:
return false
# the last block may grow or shrink in place
let start = (usize)block.data -? (usize)self.block.data else return false
if start > self.used or block.length != self.used - start:
return false
if start > self.block.length or size > self.block.length - start:
return false
self.used = start + size
return true
## Free `block`.
pub func release(block: u8[]):
if diagnostic and block.length > 0:
_ = memset(block.data, 221, block.length) # an arena releases all at once
## What has been handed out since the last reset, and what the arena holds in all.
pub func in_use() -> usize:
return self.used
## The arena's total capacity in bytes.
pub func capacity() -> usize:
return self.block.length
## Forget every allocation; the storage is reused from its start.
pub func reset():
self.used = 0
## Give the storage back to the parent; the arena is empty afterwards.
pub func destroy():
self.parent.release(self.block)
self.block = self.block[..<0]
self.used = 0
## Whole pages from the host, each block its own mapping (§12.4). Safe from several threads.
pub struct PageAllocator: Allocator:
var unused: u8
## Allocate `size` bytes at `alignment`, or none when it cannot.
pub func allocate(size: usize, alignment: usize) -> u8[]?:
if size == 0:
return u8[]((u8*)8, 0)
let page = page_size()
if alignment > page or (alignment != 0 and page % alignment != 0):
return none
let padded = size +? (page - 1) else return none
let rounded = padded // page * page
let mapped = map_pages(rounded) else return none
if (usize)mapped == ~(usize)0:
return none
return u8[]((u8*)mapped, rounded)
## Try to resize `block` to `size` in place; whether it worked.
pub func resize(block: u8[], size: usize) -> bool:
return size <= block.length
## Free `block`.
pub func release(block: u8[]):
if block.length == 0:
return
if not diagnostic:
unmap_pages(block)
return
# the diagnostic profile (§19.4): the mapping is filled and kept for a while, so a
# use after release reads the pattern rather than faulting or reading a new mapping
lock(&records_lock)
if already_held(held_pages, block):
unlock(&records_lock)
trap("double free: pages were released twice")
_ = memset(block.data, (i32)released_fill, block.length)
let slot = held_next % held_pages.length
if let old = held_pages[slot]:
if not still_released(old):
unlock(&records_lock)
trap("use after free: released pages were written after their release")
unmap_pages(old)
held_pages[slot] = block
held_next += 1
unlock(&records_lock)
extern func mmap(address: void*?, length: usize, protection: i32, flags: i32, descriptor: i32, offset: i64) -> void*?
## `MAP_PRIVATE | MAP_ANON`: `MAP_ANON` is 0x1000 on macOS and 0x20 on Linux; the
## protection `PROT_READ | PROT_WRITE` is 3 on both.
let private_anonymous: i32 = 4098 if platform.macos else 34
extern func munmap(address: void*, length: usize) -> i32
extern func getpagesize() -> i32
extern func VirtualAlloc(address: void*?, size: usize, kind: u32, protection: u32) -> void*?
extern func VirtualFree(address: void*, size: usize, kind: u32) -> i32
## The host's memory page size in bytes: the one query `os.page_size` and the threads'
## stack sizes use too.
pub func page_size() -> usize:
if platform.windows:
var info: SystemInfo = ---
GetSystemInfo(&info)
return (usize)info.page_size
return (usize)getpagesize()
func map_pages(size: usize) -> void*?:
if platform.windows:
return VirtualAlloc(none, size, 12288, 4)
return mmap(none, size, 3, private_anonymous, -1, 0)
func unmap_pages(block: u8[]):
if platform.windows:
_ = VirtualFree(block.data, 0, 32768)
else:
_ = munmap(block.data, block.length)
## `--profile diagnostic`: set by the startup shim before `startup` runs.
pub var diagnostic: bool
## An allocation the diagnostic profile recorded (§19.4): the block, its size, and where.
pub struct Site:
## The block's address, or none for a failed request.
pub var address: void*?
## The requested size in bytes.
pub var size: usize
## The function that requested it.
pub var function: str
## The source line of the request.
pub var line: u32
var sites: Site[256]
var site_next: usize
## The diagnostic profile's records are shared by every thread: a short spin lock guards
## each update, on the atomic `@bool` alone, since this module is below `sync`.
var records_lock: @bool
func lock(flag: (@bool)*):
while true:
let (exchanged, observed) = flag.cas(false, true)
_ = observed
if exchanged:
return
func unlock(flag: (@bool)*):
flag.store(false)
## Called by the compiled program after each allocation under `--profile diagnostic`.
## The record keeps the most recent 256 allocations, oldest first: once full, the oldest
## goes and the newest takes the last place.
pub func note_site(address: void*?, size: usize, function: str, line: u32):
lock(&records_lock)
if site_next < sites.length:
sites[site_next] = Site(address = address, size = size, function = function, line = line)
else:
var i: usize = 1
while i < sites.length:
sites[i - 1] = sites[i]
i += 1
sites[sites.length - 1] = Site(address = address, size = size, function = function, line = line)
site_next += 1
unlock(&records_lock)
## The allocations recorded so far, the most recent 256 at most, oldest first. A view of
## the record: read it while no other thread allocates.
pub func allocation_sites() -> const Site[]:
let held = site_next if site_next < sites.length else sites.length
return sites[..<held]
## The released blocks the diagnostic profile still holds, oldest freed first.
var quarantine: (u8[]?)[256]
var quarantine_next: usize
## The released mappings the diagnostic profile still holds, oldest first.
var held_pages: (u8[]?)[8]
var held_next: usize
## The byte a released block is filled with under the diagnostic profile.
let released_fill: u8 = 221
## The byte unwritten storage is filled with under the diagnostic profile: `---` storage,
## `memory.allocate` and `memory.frame`; the compiler writes the same byte, 0xAA.
let unwritten_fill: u8 = 170
## Whether `block` is among the released blocks still `held`: releasing it again is a
## double free.
func already_held(blocks: const (u8[]?)[], block: u8[]) -> bool:
for entry in blocks:
if let kept = entry:
if kept.data == block.data:
return true
return false
## Whether every byte of a held block is still the fill it was given at release: a write
## through a dangling pointer changes one.
func still_released(block: const u8[]) -> bool:
for byte in block:
if byte != released_fill:
return false
return true
## Allocation failed: out of memory.
# mark: Diagnostics and heap ==================================================================
## Allocation failed because memory is exhausted.
pub let exhausted: ErrorCode = ErrorCode.package(1)
## No allocator is current (§12.3).
pub let unset: ErrorCode = ErrorCode.package(2)
var c_allocator: CAllocator
## The current allocator of this thread (§12.3) and the process's initial one.
pub local var allocator: Allocator
## The process's C heap allocator.
pub var heap: Allocator
## Run by the startup shim before `main`, and by `thread.spawn` in a new thread.
pub func startup():
heap = &c_allocator
allocator = heap
## Copy `count` elements from `source` to `target` (no overlap).
# mark: Byte primitives =======================================================================
## Copy `count` elements from `source` into `target`.
pub func copy[T](target: T[], source: const T[], count: usize):
if count > target.length or count > source.length:
trap("index out of bounds")
if count > 0:
_ = memcpy(target.data, source.data, count * size_of(T))
## Copy `count` elements, allowing `source` and `target` to overlap.
pub func move[T](target: T[], source: const T[], count: usize):
if count > target.length or count > source.length:
trap("index out of bounds")
if count > 0:
_ = memmove(target.data, source.data, count * size_of(T))
## Set every byte of `target` to `value`.
pub func set(target: u8[], value: u8):
if target.length > 0:
_ = memset(target.data, (i32)value, target.length)
## Read a `T` from `address`, which may be unaligned.
pub func read[T](address: const void*) -> T:
var value: T = ---
_ = memcpy(&value, address, size_of(T))
return value
## Write `value` to `address`, which may be unaligned.
pub func write[T](address: void*, value: T):
var copy_of = value
_ = memcpy(address, ©_of, size_of(T))
## `count` uninitialised elements of `T` in the frame of the function that calls this, at
## most `most` (a constant; `most * size_of(T)` at most 4096 bytes), freed when that function
## returns (§12.7). The compiler takes the storage at the call, so this body never runs.
pub func frame[T](count: usize, most: usize) -> T[]:
trap("memory.frame is answered where it is called")
## `size` unwritten bytes aligned to `alignment`, from the current allocator: storage a
## structure lays out itself (§12.2), given back with `free`; the diagnostic profile fills
## them with 0xAA (§19.4). Fails with `memory.exhausted`.
pub func allocate(size: usize, alignment: usize) -> u8[]!:
let block = allocator.allocate(size, alignment) else error(exhausted, "memory.exhausted")
if diagnostic:
_ = memset(block.data, (i32)unwritten_fill, block.length)
return block
## `block` with `size` bytes: in place when the current allocator can, else a
## new block with the old bytes copied and the old block released. The bytes past the
## old ones are unwritten, 0xAA under the diagnostic profile.
pub func grow(block: u8[], size: usize) -> u8[]!:
var grown: u8[] = ---
if allocator.resize(block, size):
grown = u8[](block.data, size)
else:
grown = allocator.allocate(size, 8) else error(exhausted, "memory.exhausted")
let keep = block.length if block.length < size else size
if keep > 0:
_ = memcpy(grown.data, block.data, keep)
allocator.release(block)
if diagnostic and size > block.length:
_ = memset(&grown[block.length], (i32)unwritten_fill, size - block.length)
return grown
"""
## The text of `src/std/windows_text.lucb`.
let text_8: str = r"""#==============================================================================================
#
# windows_text - Allocating Windows text conversions
#
# DESCRIPTION:
# Returned storage belongs to the current allocator and includes a NUL terminator.
# Callers select their API's error code.
#
#==============================================================================================
import c
import memory
import windows_abi
## UTF-8 `text` to a NUL-terminated UTF-16 buffer, `invalid` on malformed input.
pub func wide(text: c.str, invalid: ErrorCode) -> u16[]!:
let count = windows_abi.MultiByteToWideChar(windows_abi.utf8_page, windows_abi.reject_invalid_utf8, text, -1, none, 0)
if count <= 0:
error(invalid, "OS text is not valid UTF-8")
let result = try new u16[(usize)count] ---
errdefer free(result)
if windows_abi.MultiByteToWideChar(windows_abi.utf8_page, windows_abi.reject_invalid_utf8, text, -1, result.data, count) != count:
error(invalid, "OS text could not be converted to UTF-16")
return result
## UTF-16 `text` to a NUL-terminated UTF-8 string, `invalid` on malformed input.
pub func narrow(text: const u16[], invalid: ErrorCode) -> str!:
if text.length > 2147483647:
error(invalid, "OS text exceeds the conversion limit")
let count = windows_abi.WideCharToMultiByte(windows_abi.utf8_page, windows_abi.reject_invalid_utf16, text.data, (i32)text.length, none, 0, none, none)
if count == 0 and text.length > 0:
error(invalid, "OS text is not valid UTF-16")
let result = try new u8[(usize)count + 1] ---
errdefer free(result)
if count > 0 and windows_abi.WideCharToMultiByte(windows_abi.utf8_page, windows_abi.reject_invalid_utf16, text.data, (i32)text.length, result.data, count, none, none) != count:
error(invalid, "OS text could not be converted to UTF-8")
result[(usize)count] = 0
return (str)result[..<(usize)count]
"""
## The text of `src/std/os/module.lucb`.
let text_9: str = r"""#==============================================================================================
#
# os - The operating system's view of the process
#
# DESCRIPTION:
# Its environment, working directory, identity and host (§16.6). Text comes back as `str`
# views of C storage that the system owns; copy what must outlive the next call.
#
#==============================================================================================
import c
import memory
import windows_text
import platform
extern func getenv(variable: c.str) -> c.str?
extern func setenv(variable: c.str, value: c.str, overwrite: i32) -> i32
extern func unsetenv(variable: c.str) -> i32
extern func getcwd(buffer: u8*, size: usize) -> c.str?
extern func readlink(path: c.str, buffer: void*, capacity: usize) -> isize
extern func mac_executable_path as "_NSGetExecutablePath"(buffer: u8*, size: u32*) -> i32
extern func chdir(path: c.str) -> i32
extern func getpid() -> i32
extern func getppid() -> i32
extern func gethostname(buffer: u8*, size: usize) -> i32
extern func c_exit as "exit"(code: i32) -> never
extern func sysconf(option: i32) -> i64
extern func getentropy(buffer: void*, count: usize) -> i32
extern func BCryptGenRandom(algorithm: void*?, buffer: u8*, count: u32, flags: u32) -> i32
## Fill bytes from the operating system's cryptographic random generator.
## A failure is reported; weak process IDs or timestamps are never substituted.
pub func random_bytes(buffer: u8[]) -> !:
var offset: usize = 0
while offset < buffer.length:
let count = buffer.length - offset if buffer.length - offset < 256 else 256
var result: i32 = 0
if platform.windows:
result = BCryptGenRandom(none, &buffer[offset], (u32)count, 2)
else:
result = getentropy(&buffer[offset], count)
if result != 0:
error(failed, "operating system randomness is unavailable")
offset += count
## An operating-system call failed.
pub let failed: ErrorCode = ErrorCode.package(12)
## The target, as constants a program branches on: `if os.arm64:`, `if os.linux and
## os.x86_64:` (§19.5); the compiler decides them from `--target`, so the branch a target
## rules out is pruned (§19.6).
pub let arm64: bool = platform.arm64
## Whether the target architecture is x86-64.
pub let x86_64: bool = platform.x86_64
## Whether the target operating system is macOS.
pub let macos: bool = platform.macos
## Whether the target operating system is Linux.
pub let linux: bool = platform.linux
## Whether the target operating system is Windows.
pub let windows: bool = platform.windows
## Whether the target is a POSIX system.
pub let posix: bool = platform.posix
## The width of a pointer on the target, in bits.
pub let pointer_bits: u32 = platform.pointer_bits
## The target triple.
pub let name: str = platform.name
## The instruction-set level the program was compiled for (`--cpu`, §19.5): on x86-64, 1
## to 4 for x86-64-v1 to v4; on arm64, 1 for NEON, 2 for SVE.
pub let cpu_level: u32 = platform.cpu_level
## The instruction-set level of the processor this program runs on, in the same scale:
## what it may dispatch to at run time. On x86-64 `cpuid` answers; on arm64 the level is
## NEON until the system reports SVE.
# mark: Machine and process ===================================================================
## The microarchitecture level (§19.5) the CPU running this process supports.
pub func cpu_level_running() -> u32:
if platform.x86_64:
return x86_level()
return 1
## x86-64: SSE4.2 and POPCNT make v2; AVX2, FMA, and BMI2 make v3; AVX-512F makes v4.
func x86_level() -> u32:
var c1: u32 = 0
var b7: u32 = 0
asm x86_64 (in("eax") 1u32, out("ecx") c1, out("eax") _, out("ebx") _, out("edx") _, options(nostack)):
cpuid
asm x86_64 (in("eax") 7u32, in("ecx") 0u32, out("ebx") b7, out("eax") _, out("ecx") _, out("edx") _, options(nostack)):
cpuid
let sse42 = (c1 & (1 << 20)) != 0
let popcnt = (c1 & (1 << 23)) != 0
let fma = (c1 & (1 << 12)) != 0
# the operating system saves the wide registers only with `OSXSAVE` set
let osxsave = (c1 & (1 << 27)) != 0
let avx2 = (b7 & (1 << 5)) != 0
let bmi2 = (b7 & (1 << 8)) != 0
let avx512f = (b7 & (1 << 16)) != 0
if not (sse42 and popcnt):
return 1
if not (avx2 and fma and bmi2 and osxsave):
return 2
return 4 if avx512f else 3
## `_SC_NPROCESSORS_ONLN`: 58 on macOS, 84 on Linux.
let processors_online: i32 = 58 if platform.macos else 84
## The processors available to this process, at least one.
pub func cpus() -> u32:
if platform.windows:
let count = GetActiveProcessorCount(65535)
return count if count > 0 else 1
let n = sysconf(processors_online)
return (u32)n if n > 0 else 1
## The host's memory page size in bytes.
pub func page_size() -> usize:
return memory.page_size()
## The value of an environment variable, or none when it is not set. Copy a view
## that must survive another env call on this thread or an environment mutation.
pub func env(variable: c.str) -> str?:
if platform.windows:
return windows_env(variable)
let value = getenv(variable) else return none
return (str)value
## Set an environment variable for this process and the ones it starts.
pub func set_env(variable: c.str, value: c.str) -> !:
if platform.windows:
let key = try windows_text.wide(variable, failed)
defer free(key)
let text = try windows_text.wide(value, failed)
defer free(text)
if SetEnvironmentVariableW(key.data, text.data) == 0:
error(failed, "the environment variable cannot be set")
return
if setenv(variable, value, 1) != 0:
error(failed, "the environment variable cannot be set")
## Remove environment variable `variable`.
pub func unset_env(variable: c.str) -> !:
if platform.windows:
let key = try windows_text.wide(variable, failed)
defer free(key)
if SetEnvironmentVariableW(key.data, none) == 0 and GetLastError() != 203:
error(failed, "the environment variable cannot be unset")
return
if unsetenv(variable) != 0:
error(failed, "the environment variable cannot be unset")
## The working directory, in storage from the current allocator.
pub func cwd() -> str!:
if platform.windows:
return try windows_cwd()
let buffer = try new u8[4096] ---
errdefer free(buffer)
let found = getcwd(buffer.data, buffer.length)
if found == none:
error(failed, "the working directory cannot be read")
let path = found else trap("unreachable")
return (str)path
## The path of this program's executable as the loader saw it, in an allocation of the
## current allocator, NUL-terminated for the C calls a path goes to.
pub func executable() -> str!:
if platform.windows:
return try windows_executable()
let buffer = try new u8[4096] ---
errdefer free(buffer)
var count: usize = 0
if platform.macos:
var size: u32 = 4096
if mac_executable_path(buffer.data, &size) != 0:
error(failed, "the executable path cannot be read")
while count < buffer.length and buffer[count] != 0:
count += 1
else:
let read = readlink("/proc/self/exe", buffer.data, buffer.length - 1)
if read < 0:
error(failed, "the executable path cannot be read")
count = (usize)read
buffer[count] = 0
return (str)buffer[..<count]
## Change the process's working directory to `path`.
pub func change_dir(path: c.str) -> !:
if platform.windows:
let text = try windows_text.wide(path, failed)
defer free(text)
if SetCurrentDirectoryW(text.data) == 0:
error(failed, "the directory cannot be entered")
return
if chdir(path) != 0:
error(failed, "the directory cannot be entered")
## The process identifier.
pub func pid() -> i32:
if platform.windows:
return (i32)GetCurrentProcessId()
return getpid()
## The parent process's identifier.
pub func parent_pid() -> i32:
if platform.windows:
return windows_parent_pid()
return getppid()
## The host's name, in storage from the current allocator.
pub func hostname() -> str!:
if platform.windows:
var host_text: u16[256]
var count: u32 = 256
if GetComputerNameW(&host_text[0], &count) == 0:
error(failed, "the host name cannot be read")
return try windows_text.narrow(host_text[..<(usize)count], failed)
let buffer = try new u8[256] ---
errdefer free(buffer)
if gethostname(buffer.data, buffer.length) != 0:
error(failed, "the host name cannot be read")
var n: usize = 0
while n < buffer.length and buffer[n] != 0:
n += 1
return (str)buffer[..<n]
## Stop the process with `code`, running no deferred work.
pub func exit(code: i32) -> never:
c_exit(code)
"""
## The text of `src/std/os/windows.lucb`.
let text_10: str = r"""#==============================================================================================
#
# windows - The os module's native Windows calls
#
# DESCRIPTION:
# Native Windows calls stay behind the portable `os` interface. Public text is UTF-8.
#
#==============================================================================================
from windows_abi import MultiByteToWideChar, WideCharToMultiByte, GetLastError, SetLastError, CloseHandle, GetCurrentProcessId, GetEnvironmentVariableW, SetEnvironmentVariableW, GetCurrentDirectoryW, SetCurrentDirectoryW
extern func GetComputerNameW(output: u16*, capacity: u32*) -> i32
extern func GetModuleFileNameW(module: void*?, output: u16*, capacity: u32) -> u32
extern func GetActiveProcessorCount(group: u16) -> u32
extern func CreateToolhelp32Snapshot(flags: u32, process: u32) -> void*
extern func Process32FirstW(snapshot: void*, entry: WinProcessEntry*) -> i32
extern func Process32NextW(snapshot: void*, entry: WinProcessEntry*) -> i32
extern struct WinProcessEntry:
size: u32
usage: u32
pid: u32
heap: usize
module: u32
threads: u32
parent: u32
priority: i32
flags: u32
executable: u16[260]
# Environment views last until the next env call on this thread. Fixed bounds are
# the Windows environment limit, independent of the caller's current allocator.
local var environment_wide: u16[32768]
local var environment_utf8: u8[131073]
local var environment_key: u16[32768]
func windows_env(variable: c.str) -> str?:
if MultiByteToWideChar(65001, 8, variable, -1, &environment_key[0], 32768) == 0:
return none
SetLastError(0)
let count = GetEnvironmentVariableW(&environment_key[0], &environment_wide[0], 32768)
if count >= 32768 or (count == 0 and GetLastError() != 0):
return none
var size: i32 = 0
if count > 0:
size = WideCharToMultiByte(65001, 128, &environment_wide[0], (i32)count, &environment_utf8[0], 131072, none, none)
if size == 0:
return none
environment_utf8[(usize)size] = 0
return (str)environment_utf8[..<(usize)size]
func windows_cwd() -> str!:
var attempt: usize = 0
while attempt < 8:
let capacity = GetCurrentDirectoryW(0, none)
if capacity == 0:
break
let text = try new u16[(usize)capacity] ---
defer free(text)
let count = GetCurrentDirectoryW(capacity, text.data)
if count == 0:
break
if count < capacity:
return try windows_text.narrow(text[..<(usize)count], failed)
attempt += 1
error(failed, "the working directory cannot be read")
## The module path grows until the call answers a length below the capacity: a full
## buffer is a truncated path.
func windows_executable() -> str!:
var capacity: usize = 260
while capacity <= 32768:
let text = try new u16[capacity] ---
defer free(text)
let count = GetModuleFileNameW(none, text.data, (u32)capacity)
if count == 0:
break
if (usize)count < capacity:
return try windows_text.narrow(text[..<(usize)count], failed)
capacity *= 4
error(failed, "the executable path cannot be read")
func windows_parent_pid() -> i32:
let snapshot = CreateToolhelp32Snapshot(2, 0)
if (usize)snapshot == ~(usize)0:
return 0
defer _ = CloseHandle(snapshot)
var entry: WinProcessEntry
entry.size = (u32)memory.size_of(WinProcessEntry)
var found = Process32FirstW(snapshot, &entry)
let current = GetCurrentProcessId()
while found != 0:
if entry.pid == current:
return (i32)entry.parent
found = Process32NextW(snapshot, &entry)
return 0
"""
## The text of `src/std/io.lucb`.
let text_11: str = r"""#==============================================================================================
#
# io - Byte streams
#
# DESCRIPTION:
# Readers and writers over bytes, buffered wrappers, the standard output and error
# streams, the user's home directory, and the stream error codes.
#
#==============================================================================================
import c
import core
import memory
import platform
import os
import strings
extern func fwrite(data: const void*, size: usize, count: usize, stream: void*) -> usize
extern func fflush(stream: void*?) -> i32
extern func flockfile(stream: void*)
extern func funlockfile(stream: void*)
extern func win_lock_file as "_lock_file"(stream: void*)
extern func win_unlock_file as "_unlock_file"(stream: void*)
extern func posix_read as "read"(descriptor: i32, buffer: void*, count: usize) -> isize
extern func windows_read as "_read"(descriptor: i32, buffer: void*, count: u32) -> i32
func c_read(descriptor: i32, buffer: void*, count: usize) -> isize:
if platform.windows:
return (isize)windows_read(descriptor, buffer, (u32)(count if count < 2147483647 else 2147483647))
return posix_read(descriptor, buffer, count)
## A borrowed byte source. A successful read returns at most the buffer length;
## zero for a nonempty buffer means EOF. Empty reads return zero without consuming
## input. Errors are distinct from EOF. The implementing object owns its lifetime.
pub interface Reader:
func read(buffer: u8[]) -> usize!
## A borrowed byte sink (§14.4). A write may accept fewer bytes than supplied;
## callers needing the whole payload use `write_all`. Empty writes return zero.
## A failure may follow earlier successful writes; the view never owns the sink.
pub interface Writer:
func write(data: const u8[]) -> usize!
## A stream ended before the expected bytes arrived.
pub let unexpected_eof: ErrorCode = ErrorCode.package(14)
## A stream made no progress and cannot continue.
pub let no_progress: ErrorCode = ErrorCode.package(15)
## A read or write was asked for an invalid count.
pub let invalid_count: ErrorCode = ErrorCode.package(16)
## A read or write was given an invalid buffer.
pub let invalid_buffer: ErrorCode = ErrorCode.package(17)
## Fill `buffer`, or fail at EOF or on a source error. Optional `transferred` is
## reset to zero on entry and updated after each successful read, including before
## a later failure. It must not overlap the buffer or the source's state. No call
## reaches the source for an empty buffer. The source remains borrowed and open.
pub func read_exact(source: Reader, buffer: u8[], transferred: usize*? = none) -> !:
var done: usize = 0
if let progress = transferred:
*progress = 0
while done < buffer.length:
let count = try source.read(buffer[done..])
if count > buffer.length - done:
error(invalid_count, "the reader returned more bytes than requested")
if count == 0:
error(unexpected_eof, "the reader ended before the buffer was filled")
done += count
if let progress = transferred:
*progress = done
## Write all bytes, preserving confirmed progress through `transferred` on error.
## The progress pointer must not overlap the data or sink state. A zero-length
## successful write on nonempty input fails with `no_progress` instead of spinning.
## This does not flush or close the borrowed sink. Empty input makes no sink call.
pub func write_all(destination: Writer, data: const u8[], transferred: usize*? = none) -> !:
var done: usize = 0
if let progress = transferred:
*progress = 0
while done < data.length:
let count = try destination.write(data[done..])
if count > data.length - done:
error(invalid_count, "the writer returned more bytes than supplied")
if count == 0:
error(no_progress, "the writer accepted no bytes")
done += count
if let progress = transferred:
*progress = done
## Copy at most `limit` bytes through caller-owned scratch storage; stop earlier
## at EOF. Reads never consume beyond the limit. `transferred` counts confirmed
## destination bytes, including before a failure; source consumption can be ahead
## by one scratch buffer if writing fails. Scratch and progress must not overlap
## either endpoint's state/data or each other. No allocation, flush or close occurs.
pub func copy_limit(source: Reader, destination: Writer, buffer: u8[], limit: usize, transferred: usize*? = none) -> usize!:
var done: usize = 0
if let progress = transferred:
*progress = 0
if limit == 0:
return 0
if buffer.length == 0:
error(invalid_buffer, "copying requires a nonempty scratch buffer")
while done < limit:
let available = limit - done
let requested = buffer.length if buffer.length < available else available
let count = try source.read(buffer[..<requested])
if count > requested:
error(invalid_count, "the reader returned more bytes than requested")
if count == 0:
break
var written: usize = 0
write_all(destination, buffer[..<count], &written) catch failure:
if let progress = transferred:
*progress = done + written
error(failure)
done += written
if let progress = transferred:
*progress = done
return done
## A cursor over borrowed bytes. Copying the reader copies its position, not its
## data. The backing storage must outlive the reader and every read using it.
pub struct SliceReader: Reader:
var data: const u8[]
var position: usize
## A reader over `data`.
pub static func over(data: const u8[]) -> SliceReader:
return SliceReader(data = data, position = 0)
## The number of bytes not yet read.
pub func remaining() -> usize:
return self.data.length - self.position
## Copy up to the buffer length; zero means no bytes remain (or an empty read).
pub func read(buffer: u8[]) -> usize!:
let left = self.remaining()
let count = buffer.length if buffer.length < left else left
memory.move(buffer, self.data[self.position..], count)
self.position += count
return count
## A writer into fixed borrowed storage. Copying it copies the cursor and aliases
## storage. Writes may be short; once full, a nonempty write fails with `full`.
pub struct SliceWriter: Writer:
var data: u8[]
var used: usize
## A writer into `data`.
pub static func over(data: u8[]) -> SliceWriter:
return SliceWriter(data = data, used = 0)
## View of confirmed written bytes, borrowed from the caller's storage.
pub func written() -> const u8[]:
return self.data[..<self.used]
## Write `data`; the number of bytes written.
pub func write(data: const u8[]) -> usize!:
if data.length == 0:
return 0
let left = self.data.length - self.used
if left == 0:
error(full, "the destination buffer is full")
let count = data.length if data.length < left else left
memory.move(self.data[self.used..], data, count)
self.used += count
return count
## Buffered input over a borrowed source and nonempty caller-owned storage. Both
## must outlive this adapter. Storage must not overlap source state or read buffers.
## Construct with `over`; serialize access and do not use copied adapters together.
## Reads may prefetch beyond the requested bytes. The adapter never closes its source.
pub struct BufferedReader: Reader:
var source: Reader
var storage: u8[]
var position: usize
var used: usize
## A buffered reader over `source`, using `storage`.
pub static func over(source: Reader, storage: u8[]) -> BufferedReader!:
if storage.length == 0:
error(invalid_buffer, "buffered input requires nonempty storage")
return BufferedReader(source = source, storage = storage, position = 0, used = 0)
## Unread bytes already fetched. No source operation occurs.
pub func buffered() -> usize:
return self.used - self.position
## Return buffered bytes first, otherwise perform one source read. Empty reads
## do not call the source. EOF is not cached, allowing a growing source to resume.
pub func read(buffer: u8[]) -> usize!:
if buffer.length == 0:
return 0
if self.position == self.used:
let count = try self.source.read(self.storage)
if count > self.storage.length:
error(invalid_count, "the reader returned more bytes than requested")
self.position = 0
self.used = count
let available = self.buffered()
let count = buffer.length if buffer.length < available else available
memory.copy(buffer, self.storage[self.position..], count)
self.position += count
return count
## Buffered output over a borrowed sink and nonempty caller-owned storage. Both
## must outlive this adapter; storage must not overlap sink state or write input.
## Construct with `over`; serialize access and do not use copied adapters together.
## Explicitly flush before discarding the adapter. There is no implicit flush/close.
pub struct BufferedWriter: Writer:
var destination: Writer
var storage: u8[]
var used: usize
## A buffered writer over `destination`, using `storage`.
pub static func over(destination: Writer, storage: u8[]) -> BufferedWriter!:
if storage.length == 0:
error(invalid_buffer, "buffered output requires nonempty storage")
return BufferedWriter(destination = destination, storage = storage, used = 0)
## Bytes accepted by this adapter but not yet confirmed by the destination.
pub func buffered() -> usize:
return self.used
## Drain this buffer, preserving the unconfirmed suffix if the sink fails.
## Retrying does not resend bytes confirmed by earlier successful sink writes.
## This neither flushes downstream buffers nor requests filesystem durability.
pub func flush() -> !:
var written: usize = 0
write_all(self.destination, self.storage[..<self.used], &written) catch failure:
self.used -= written
memory.move(self.storage, self.storage[written..], self.used)
error(failure)
self.used = 0
## Accept up to the remaining capacity. A full buffer is drained before any
## new input is accepted; a failure accepts none of this call's input. Empty
## writes do not drain the buffer. Use write_all to accept an entire payload.
pub func write(data: const u8[]) -> usize!:
if data.length == 0:
return 0
if self.used == self.storage.length:
try self.flush()
let available = self.storage.length - self.used
let count = data.length if data.length < available else available
memory.copy(self.storage[self.used..], data, count)
self.used += count
return count
## A compile-time source position, the value of `luce.location`.
pub struct Location:
## The source file of the location.
pub var file: str
## The source line of the location.
pub var line: u32
## The enclosing function of the location.
pub var function: str
## The stream is closed.
pub let closed: ErrorCode = ErrorCode.package(3)
## The underlying stream operation failed.
pub let failed: ErrorCode = ErrorCode.package(24)
## A non-blocking stream has no data ready.
pub let would_block: ErrorCode = ErrorCode.package(28)
## Borrowed standard output stream. Each nonempty write uses the existing libc
## stream and flushes only that stream, making completed output visible before a
## following print. The libc stream lock serializes each write with other libc
## users; concurrent direct descriptor writes may interleave. No stream is created
## or owned here. A write/flush failure can follow partial output.
pub struct File: Writer:
var descriptor: i32
func stream() -> void*!:
if self.descriptor == 1:
return c.stdout()
if self.descriptor == 2:
return c.stderr()
error(closed, "the standard output stream is unavailable")
## Flush this C stream, without closing it or requesting filesystem durability.
pub func flush() -> !:
let stream = try self.stream()
if fflush(stream) != 0:
error(closed, "the stream could not be flushed")
## Write `data`; the number of bytes written.
pub func write(data: const u8[]) -> usize!:
if data.length == 0:
return 0
let stream = try self.stream()
lock_stream(stream)
defer unlock_stream(stream)
let written = fwrite(data.data, 1, data.length, stream)
let flushed = fflush(stream)
if written != data.length or flushed != 0:
error(closed, "the stream refused the bytes")
return written
func lock_stream(stream: void*):
if platform.windows:
win_lock_file(stream)
else:
flockfile(stream)
func unlock_stream(stream: void*):
if platform.windows:
win_unlock_file(stream)
else:
funlockfile(stream)
var out_file: File = File(descriptor = 1)
var err_file: File = File(descriptor = 2)
## Unbuffered input borrows descriptor zero. It retries interrupted calls, reports
## would-block separately, and never owns or closes the process's descriptor.
struct StandardInput: Reader:
var descriptor: i32
## Read into `buffer`; the number of bytes read.
pub func read(buffer: u8[]) -> usize!:
if buffer.length == 0:
return 0
let requested = 1073741824 if buffer.length > 1073741824 else buffer.length
while true:
let count = c_read(self.descriptor, buffer.data, requested)
if count >= 0:
return (usize)count
let number = c.errno()
if number == c.interrupted:
continue
if number == (35 if platform.macos else 11):
error(would_block, "standard input would block")
if number == 9:
error(closed, "standard input is closed")
error(failed, "standard input could not be read")
var in_file: StandardInput = StandardInput(descriptor = 0)
## Borrowed unbuffered standard input. Reads may block; concurrent readers compete
## for bytes. Do not mix with buffered C stdin reads, which may have prefetched data.
pub func stdin() -> Reader:
return &in_file
## The process's standard output stream.
# mark: Standard streams ======================================================================
## A writer over the process's standard output.
pub func stdout() -> Writer:
return &out_file
## The process's standard error stream.
pub func stderr() -> Writer:
return &err_file
## Explicitly flush C stdout, including bytes written through C interop.
pub func flush_stdout() -> !:
try out_file.flush()
## Explicitly flush C stderr, including bytes written through C interop.
pub func flush_stderr() -> !:
try err_file.flush()
## A fixed writer has no room left.
pub let full: ErrorCode = ErrorCode.package(4)
## The sink a formatted string offers a `Display` value (§14.4): it appends to the buffer
## the compiler is filling, and fails once that buffer is full, as `format` then does.
pub struct FormatSink: Writer:
var buffer: core.FormatBuffer*
## Write `data`; the number of bytes written.
pub func write(data: const u8[]) -> usize!:
if core.format_put(self.buffer, data.data, data.length) != 0:
error(full, "the buffer is full")
return data.length
## The process's directories (§16.6): `from io import path`, then `path.user()`. Each is
## a `str` view of environment storage the system owns, or fresh storage from the current
## allocator where one has to be built.
pub struct path:
var unused: u8
## The user's home directory: USERPROFILE on Windows, HOME on POSIX.
pub static func user() -> str!:
let home = os.env("USERPROFILE" if platform.windows else "HOME") else error(io_path_failed, "no home directory")
return home
## The current user's home directory.
pub static func home() -> str!:
return try path.user()
## Where temporary files go: `$TMPDIR`, else `/tmp`.
pub static func temp() -> str:
if platform.windows:
if let dir = os.env("TEMP"):
if dir.length > 0: return dir
if let dir = os.env("TMP"):
if dir.length > 0: return dir
return "."
let dir = os.env("TMPDIR") else return "/tmp"
return dir
## Where configuration lives: `$XDG_CONFIG_HOME`, else `$HOME/.config`.
pub static func config() -> str!:
if platform.windows:
if let set = os.env("APPDATA"):
if set.length > 0: return set
let home = try path.user()
let buffer = try new u8[home.length + 17] ---
return try strings.format(buffer, f"{home}/AppData/Roaming")
if let set = os.env("XDG_CONFIG_HOME"):
return set
let home = try path.user()
let buffer = try new u8[home.length + 9] ---
return try strings.format(buffer, f"{home}/.config")
## The home directory could not be determined.
pub let io_path_failed: ErrorCode = ErrorCode.package(13)
"""
## The text of `src/std/luce.lucb`.
let text_12: str = r"""#==============================================================================================
#
# luce - The derived interface bounds
#
# DESCRIPTION:
# The derived bounds of §14.4: every type with `==` is Equatable, every hashable one
# Hashable, every ordered one Comparable. Nothing implements them by hand.
#
#==============================================================================================
import io
## Implemented by every type with `==`.
pub interface Equatable:
func equals(other: void*) -> bool
## Implemented by every hashable type: `value.hash()` (§7.4).
pub interface Hashable:
func hash() -> u64
## Implemented by every ordered type.
pub interface Comparable:
func compare(other: void*) -> i64
## The protocols `for` consumes (§8.3, §14.4): a value iterator answers each element
## until `none`; an iterable answers a fresh iterator of its own concrete type.
pub interface Iterator[T]:
func next() -> T?
## A type a `for` loop can walk: it yields an iterator of `T` (§13.3).
pub interface Iterable[T, I: Iterator[T]]:
func iterator() -> I
## A value that writes itself to a sink (§14.4); formatted strings call it for each field.
pub interface Display:
func display(sink: io.Writer) -> !
## The source file of the use site; the backend replaces it (§9.1).
pub let file: str = ""
## The source line of the use site; the backend replaces it (§9.1).
pub let line: u32 = 0
## The enclosing function of the use site; the backend replaces it (§9.1).
pub let function: str = ""
## The use site's file, line and function together (§9.1).
pub let location: io.Location = io.Location(file = "", line = 0, function = "")
"""
## The text of `src/std/atomic.lucb`.
let text_13: str = r"""#==============================================================================================
#
# atomic - Memory orderings and fences for `@T` operations
#
# DESCRIPTION:
# Memory orderings for the operations of `@T` (§15.1).
#
#==============================================================================================
## The memory ordering of an `@T` operation.
pub enum Ordering as u8:
relaxed = 1
acquire = 2
release = 3
acq_rel = 4
seq_cst = 5
## A fence's alone: the compiler moves no memory access across it, and the processor is
## told nothing, what a signal handler sharing data with the code it interrupts needs.
signal = 6
## A standalone fence; the backend supplies the instruction.
pub func fence(order: Ordering):
_ = order
"""
## The text of `src/std/time.lucb`.
let text_14: str = r"""#==============================================================================================
#
# time - Monotonic and wall-clock time
#
# DESCRIPTION:
# A monotonic clock that never goes backwards, and the wall clock as seconds since the
# Unix epoch, over the host's `clock_gettime`.
#
#==============================================================================================
import c
import platform
extern func clock_gettime(clock: i32, out_time: Stamp*) -> i32
extern func c_time as "time"(out_time: void*?) -> i64
## `struct timespec`: `tv_nsec` is a C `long`, four bytes on Windows.
extern struct Stamp:
seconds: i64
nanoseconds: c.long
## `CLOCK_MONOTONIC`: 6 on macOS, 1 on Linux; `CLOCK_REALTIME` is 0 on both.
let monotonic: i32 = 6 if platform.macos else 1
let realtime: i32 = 0
## The clock read into `stamp`: the monotonic one or the wall clock.
func read_clock(monotonic_clock: bool, stamp: Stamp*) -> i32:
return clock_gettime(monotonic if monotonic_clock else realtime, stamp)
## Nanoseconds from an arbitrary origin, never going backwards.
pub func now() -> u64:
var stamp = Stamp(seconds = 0, nanoseconds = 0)
_ = read_clock(true, &stamp)
return (u64)stamp.seconds * 1000000000 + (u64)stamp.nanoseconds
## Seconds since the Unix epoch.
pub func unix() -> i64:
return c_time(none)
## Nanoseconds elapsed since `start`, from `now()`.
pub func since(start: u64) -> u64:
let current = now()
return current - start if current > start else 0
"""
## The text of `src/std/thread.lucb`.
let text_15: str = r"""#==============================================================================================
#
# thread - Native threads
#
# DESCRIPTION:
# Spawning, joining and detaching host threads over pthreads, and on Windows over its
# own threads, with the running thread's identity and yielding and sleeping.
#
#==============================================================================================
from windows_abi import CloseHandle, Sleep
import c
import core
import memory
import platform
import time
import windows_text
extern func pthread_create(thread: void*?*, attributes: void*?, start: func(void*) -> void*?, argument: void*) -> i32
extern func pthread_join(thread: void*, result: void*?*?) -> i32
extern func pthread_detach(thread: void*) -> i32
extern func pthread_self() -> void*
extern func pthread_main_np() -> i32
extern func getpid() -> i32
extern func gettid() -> i32
extern func pthread_attr_init(attributes: void*) -> i32
extern func pthread_attr_setstacksize(attributes: void*, size: usize) -> i32
extern func pthread_attr_destroy(attributes: void*) -> i32
## macOS names the calling thread; Linux names one by handle. Both cap a name at 15 bytes.
extern func mac_setname as "pthread_setname_np"(name: c.str) -> i32
extern func linux_setname as "pthread_setname_np"(thread: void*, name: c.str) -> i32
extern func sched_yield() -> i32
## Windows' threads: a handle each, which joining waits on and closes.
extern func CreateThread(security: void*?, stack: usize, start: func(void*) -> u32, argument: void*, flags: u32, id: u32*?) -> void*?
extern func WaitForSingleObject(handle: void*, milliseconds: u32) -> u32
extern func GetCurrentThread() -> void*
extern func SetThreadDescription(thread: void*, name: const u16*) -> i32
extern func SwitchToThread() -> i32
extern func nanosleep(request: const Duration*, remaining: Duration*?) -> i32
## `struct timespec`: `tv_nsec` is a C `long`, four bytes on Windows.
extern struct Duration:
seconds: i64
nanoseconds: c.long
## A thread operation the operating system refused.
# mark: Threads ===============================================================================
## A thread operation failed.
pub let failed: ErrorCode = ErrorCode.package(5)
## `PTHREAD_STACK_MIN`: 16 KiB on macOS and x86-64 glibc; a smaller stack is raised to it.
let least_stack: usize = 16384
## The stack of a thread spawned without a size: 8 MiB on every host, the main thread's
## on Linux, where macOS gives a new thread 512 KiB and Windows 1 MiB. A program may set
## it before it spawns; `spawn(..., stack = n)` overrides it for one thread.
pub var default_stack: usize = 8388608
## The longest name a thread may carry, with its NUL.
let name_room: usize = 16
## What gives a new thread's `local var`s their initialisers (§6.3), run before its entry.
## The native backend's startup sets it when the program has such initialisers, since its
## thread-local storage starts zeroed; under C, `_Thread_local` storage starts initialised
## and it stays none.
pub var locals_start: (func() -> unit)?
## CreateThread's flag that makes its size the stack's reservation, not its first commit.
let stack_reservation: u32 = 0x00010000
## WaitForSingleObject's wait without end, and its answer when the object is signalled.
let wait_forever: u32 = 0xFFFFFFFF
let signalled: u32 = 0
## A running thread; the zero value is no thread at all, so arrays of handles exist. On
## Windows the id is the thread's handle.
pub struct Handle:
var id: void*?
## Wait for the thread to finish.
pub func join() -> !:
let id = self.id else error(failed, "no thread to join")
if platform.windows:
let waited = WaitForSingleObject(id, wait_forever)
_ = CloseHandle(id)
if waited != signalled:
error(failed, "join")
return
if pthread_join(id, none) != 0:
error(failed, "join")
## Let the thread run on its own; its resources are reclaimed at exit.
pub func detach():
let id = self.id else return
if platform.windows:
_ = CloseHandle(id)
return
_ = pthread_detach(id)
## What a new thread starts with: the entry, its context, its name in a heap block of
## `name_room` bytes, NUL-terminated, and the allocator rule of §15.3.
struct Start:
var entry: func(void*?) -> unit
var context: void*?
var name: u8[]
var name_length: usize
func trampoline(raw: void*) -> void*?:
let start = (Start*)raw
if let initialise = locals_start:
initialise()
memory.allocator = memory.heap
let entry = start.entry
let context = start.context
if start.name_length > 0:
let text = (c.str)(str)start.name[..<start.name_length]
if platform.windows:
name_windows(text)
elif platform.macos:
_ = mac_setname(text)
else:
_ = linux_setname(pthread_self(), text)
release_start(start)
entry(context)
return none
## A Windows thread's entry, which answers a status rather than a pointer.
func windows_trampoline(raw: void*) -> u32:
_ = trampoline(raw)
return 0
## The calling thread's name, as Windows' tools show it: UTF-16, and best effort.
func name_windows(name: c.str):
let wide = windows_text.wide(name, failed) catch failure:
return
defer free(wide)
_ = SetThreadDescription(GetCurrentThread(), wide.data)
## Free what `spawn` handed the new thread: the start record and its copy of the name.
func release_start(start: Start*):
free(start.name) in memory.heap
free(start) in memory.heap
## Start a thread running `entry(context)`. `stack` is its stack in bytes, `default_stack`
## when 0, at least `PTHREAD_STACK_MIN` and whole pages; `name` is what the host's tools
## show for it, its first fifteen bytes.
pub func spawn(entry: func(void*?) -> unit, context: void*?, stack: usize = 0, name: str = "") -> Handle!:
let copy = try new u8[name_room] --- in memory.heap
# on success the thread owns both, and `trampoline` frees them
errdefer free(copy) in memory.heap
let kept = name.length if name.length < name_room - 1 else name_room - 1
memory.copy(copy, name.bytes, kept)
copy[kept] = 0
let start = new Start(entry = entry, context = context, name = copy, name_length = kept) in memory.heap catch failure:
error(memory.exhausted, "memory.exhausted")
errdefer free(start) in memory.heap
let wanted = stack if stack > 0 else default_stack
let page = memory.page_size()
var size = wanted if wanted > least_stack else least_stack
size = (size + page - 1) // page * page
if platform.windows:
let handle = CreateThread(none, size, windows_trampoline, (void*)start, stack_reservation, none) else error(failed, "spawn")
return Handle(id = handle)
var attributes: u64[16]
if pthread_attr_init((void*)&attributes[0]) != 0:
error(failed, "spawn")
if pthread_attr_setstacksize((void*)&attributes[0], size) != 0:
_ = pthread_attr_destroy((void*)&attributes[0])
error(failed, "the stack size is refused")
var id: void*? = none
let created = pthread_create(&id, (void*)&attributes[0], trampoline, (void*)start)
_ = pthread_attr_destroy((void*)&attributes[0])
if created != 0:
error(failed, "spawn")
let thread = id else error(failed, "spawn")
return Handle(id = thread)
## A handle to the calling thread: on Windows its pseudo-handle, which stands for whichever
## thread uses it and which closing leaves alone.
pub func current() -> Handle:
if platform.windows:
return Handle(id = GetCurrentThread())
return Handle(id = pthread_self())
## Whether this is the process's initial thread, for main-thread native resources.
pub func is_main() -> bool:
if platform.windows:
return core.on_initial_thread()
if platform.macos:
return pthread_main_np() != 0
if platform.linux:
return gettid() == getpid()
return false
## A hint to the processor inside a spin loop.
pub func pause():
asm arm64:
yield
asm x86_64:
pause
## Yield the processor to another runnable thread.
pub func yield():
if platform.windows:
_ = SwitchToThread()
return
_ = sched_yield()
## Sleep for at least `milliseconds`: a signal that interrupts the sleep leaves the time
## remaining, which is slept again.
pub func sleep(milliseconds: u64):
if platform.windows:
sleep_windows(milliseconds)
return
var request = Duration(seconds = (i64)(milliseconds // 1000), nanoseconds = (c.long)((i64)(milliseconds % 1000) * 1000000))
var remaining = Duration(seconds = 0, nanoseconds = 0)
while nanosleep(&request, &remaining) != 0 and c.errno() == c.interrupted:
request = remaining
## Windows rounds waits to clock ticks and may wake before the requested interval.
## Recheck a monotonic deadline without changing system-wide timer resolution. A
## finite chunk also avoids narrowing long durations to Sleep's INFINITE sentinel.
func sleep_windows(milliseconds: u64):
if milliseconds == 0:
Sleep(0)
return
let longest_finite_wait: u64 = 0xFFFFFFFE
let start = time.now()
var elapsed: u64 = 0
while elapsed < milliseconds:
let remaining = milliseconds - elapsed
Sleep((u32)(remaining if remaining < longest_finite_wait else longest_finite_wait))
elapsed = time.since(start) // 1000000
"""
## The text of `src/std/sync.lucb`.
let text_16: str = r"""#==============================================================================================
#
# sync - Locks, conditions, once and semaphores
#
# DESCRIPTION:
# Blocking synchronisation over the host's futex-like wait and wake: a mutex, a condition
# variable, a run-once gate, and a counting semaphore.
#
# The host's sleep returns on wakes nothing here sent: Linux and macOS rarely, and
# Windows often when a thread waiting on an address less than 32 bytes away is woken.
# Every sleep below checks its own word again and sleeps again when it has not
# changed, so such a wake costs a context switch and is never seen by a caller.
#
#==============================================================================================
import platform
import time
## macOS: the `ulock` calls libSystem builds its own locks on.
extern func __ulock_wait(operation: u32, address: void*, value: u64, timeout: u32) -> i32
extern func __ulock_wake(operation: u32, address: void*, wake_value: u64) -> i32
## Linux: the futex system call, through `syscall`; the number is the architecture's.
extern func syscall(number: i64, ...) -> i64
extern func WaitOnAddress(address: void*, comparison: u32*, size: usize, milliseconds: u32) -> i32
extern func WakeByAddressSingle(address: void*)
extern func WakeByAddressAll(address: void*)
let compare_and_wait: u32 = 1
let wake_all: u32 = 256
let futex_number: i64 = 98 if platform.arm64 else 202
let futex_wait_private: i32 = 128
let futex_wake_private: i32 = 129
## Sleep in the kernel while the word at `address` still holds `value`; a change
## or a wake returns, and so may nothing at all, so every caller loops.
func sleep_on(address: void*, value: u32):
if platform.windows:
var expected = value
_ = WaitOnAddress(address, &expected, 4, 4294967295)
elif platform.macos:
_ = __ulock_wait(compare_and_wait, address, (u64)value, 0)
else:
let no_timeout: void*? = none
_ = syscall(futex_number, address, futex_wait_private, value, no_timeout)
## `sleep_on`, giving up after about `milliseconds`: a timeout, a change, a wake or nothing
## at all return, so every caller loops against its own deadline.
func sleep_on_for(address: void*, value: u32, milliseconds: u64):
if milliseconds == 0:
return
if platform.windows:
var expected = value
_ = WaitOnAddress(address, &expected, 4, (u32)(milliseconds if milliseconds < 4294967294 else 4294967294))
elif platform.macos:
# microseconds, where 0 would wait for ever
let micros = milliseconds *? 1000 else 4294967295
_ = __ulock_wait(compare_and_wait, address, (u64)value, (u32)(micros if micros < 4294967295 else 4294967295))
else:
var timeout = Timeout(seconds = (i64)(milliseconds // 1000), nanoseconds = (i64)(milliseconds % 1000) * 1000000)
_ = syscall(futex_number, address, futex_wait_private, value, &timeout)
## A relative `timespec` for the futex call.
extern struct Timeout:
seconds: i64
nanoseconds: i64
func wake_one(address: void*):
if platform.windows:
WakeByAddressSingle(address)
elif platform.macos:
_ = __ulock_wake(compare_and_wait, address, 0)
else:
_ = syscall(futex_number, address, futex_wake_private, 1)
func wake_every(address: void*):
if platform.windows:
WakeByAddressAll(address)
elif platform.macos:
_ = __ulock_wake(compare_and_wait | wake_all, address, 0)
else:
_ = syscall(futex_number, address, futex_wake_private, 2147483647)
## A lock whose waiters sleep: zero is unlocked, so `var lock: Mutex` is ready to
## use; one is locked; two is locked with someone asleep on it.
## A mutual-exclusion lock; a waiter sleeps until it is free.
pub struct Mutex:
var state: @u32
## Acquire the lock, blocking until it is free.
pub func lock():
let (taken, seen) = self.state.cas(0, 1, .acquire, .relaxed)
if taken:
return
var current = seen
if current != 2:
current = self.state.swap(2, .acquire)
while current != 0:
sleep_on((void*)&self.state, 2)
# still held with sleepers: not the holder's wake, so sleep again
if self.state.load(.relaxed) == 2:
continue
current = self.state.swap(2, .acquire)
## Release the lock and wake one waiter.
pub func unlock():
if self.state.swap(0, .release) == 2:
wake_one((void*)&self.state)
## Acquire the lock without blocking; whether it was taken.
pub func try_lock() -> bool:
let (taken, seen) = self.state.cas(0, 1, .acquire, .relaxed)
_ = seen
return taken
## A sequence number waiters watch; every signal advances it and wakes.
## A condition variable: waiters sleep until signalled. As with every condition variable,
## a waiter checks its predicate in a loop around `wait`: a signal says that something
## changed, not what, and another thread may have acted on it first.
pub struct Condition:
var sequence: @u32
## Release `mutex`, sleep until signalled, then reacquire it. A wake with no signal
## since the call is not returned; a signal or broadcast meant for another waiter may be.
pub func wait(mutex: Mutex*):
let seen = self.sequence.load(.acquire)
mutex.unlock()
while self.sequence.load(.acquire) == seen:
sleep_on((void*)&self.sequence, seen)
mutex.lock()
## `wait`, giving up after about `milliseconds`; a caller tells a timeout from a signal
## by checking its predicate.
pub func wait_for(mutex: Mutex*, milliseconds: u64):
let seen = self.sequence.load(.acquire)
mutex.unlock()
let start = time.now()
var left = milliseconds
while left > 0 and self.sequence.load(.acquire) == seen:
sleep_on_for((void*)&self.sequence, seen, left)
let waited = time.since(start) // 1000000
left = milliseconds - waited if waited < milliseconds else 0
mutex.lock()
## Wake one waiter.
pub func signal():
_ = self.sequence.add(1, .release)
wake_one((void*)&self.sequence)
## Wake every waiter.
pub func broadcast():
_ = self.sequence.add(1, .release)
wake_every((void*)&self.sequence)
## Runs its function exactly once; later callers wait for the first to finish.
## Runs a function exactly once; later callers wait for the first.
pub struct Once:
var state: @u32
## Run `function` on the first call; later calls wait for it to finish.
pub func run(function: func() -> unit):
let (first, seen) = self.state.cas(0, 1, .acq_rel, .acquire)
_ = seen
if first:
function()
self.state.store(2, .release)
wake_every((void*)&self.state)
return
while self.state.load(.acquire) != 2:
sleep_on((void*)&self.state, 1)
## A counting semaphore whose waiters sleep.
## A counting semaphore whose waiters sleep.
pub struct Semaphore:
var count: @u32
## Take one permit, blocking until one is available.
pub func acquire():
while true:
let n = self.count.load(.acquire)
if n > 0:
let (taken, seen) = self.count.cas(n, n - 1, .acq_rel, .relaxed)
_ = seen
if taken:
return
else:
sleep_on((void*)&self.count, 0)
## Return one permit and wake one waiter.
pub func release():
_ = self.count.add(1, .release)
wake_one((void*)&self.count)
# mark: Cancellation ========================================================================
## A one-way cancellation signal shared by reference. Zero is not requested.
## Keep this object alive until every waiting thread returns; do not copy or reset
## it while shared. Requests use release/acquire synchronization.
pub struct Cancellation:
var requested: @u32
## Request cancellation.
pub func request():
self.requested.store(1, .release)
## Whether cancellation was requested.
pub func is_requested() -> bool:
return self.requested.load(.acquire) != 0
"""
## The text of `src/std/ownership/module.lucb`.
let text_17: str = r"""#==============================================================================================
#
# ownership - Intrusive ownership shared by Base and Luce
#
# DESCRIPTION:
# Intrusive ownership shared by Base libraries and compiled Luce programs. Base callers
# retain and release explicitly; Luce inserts those operations. One header and trace
# protocol cover native/managed cycles on the owning thread. Runtime allocations use the
# system heap; an explicit allocator can be supplied to reserve when its lifetime is
# guaranteed by the native owner.
#
#==============================================================================================
import c
import io
import memory
import core
# mark: Objects ================================================================================
let black: u8 = 0
let gray: u8 = 1
let white: u8 = 2
let purple: u8 = 3
let color_mask: u8 = 3
let buffered: u8 = 4
let dead: u8 = 8
let collecting: u8 = 16
## A literal's text: never counted, never released, never alive at the end.
pub let immortal: u8 = 32
## An object whose `init` failed, while `abandon` releases its fields: a release of it is
## bookkeeping only and the collector never walks into it.
let abandoning: u8 = 64
## Placed in the collector's disposal order (cycles.lucb, `order_disposal`).
let ordered: u8 = 128
## The ownership header every managed object carries, before its fields.
pub struct Object:
## The strong reference count; the object is finalized when it reaches zero.
pub var strong: u32
## The number of weak references watching the object.
pub var weak: u32
## The collector's color and state bits (§the cycle collector).
pub var flags: u8
## The object's class description.
pub var info: Info*
## Allocation identity never repeats, even when an allocator reuses an address.
pub var generation: u64
## The owning context the object belongs to.
pub var context: u64
## The size of the object's allocation in bytes.
pub var allocation_size: usize
## The allocator that made the object, when one was supplied explicitly.
pub var allocator: memory.Allocator?
## What the collector and the destructor need to know about a class.
pub struct Info:
## The class name, for diagnostics and the collector.
pub var name: str
## The object's size in bytes, its fields included.
pub var size: usize
## `deinit`, when the class declares one.
pub var finish: (func(Object*) -> unit)?
## Releases the fields in reverse declaration order, tolerating unassigned ones.
pub var drop: func(Object*) -> unit
## Visits every strong reference the object holds, fields in declaration order.
pub var trace: func(Object*, func(Object*, void*) -> unit, void*) -> unit
## An interface adapter preserves the identity of its concrete owner.
pub var identity: (func(Object*) -> u64)? = none
## No object of this type can be part of a reference cycle (text, a list of numbers, a
## class whose fields are all such): the collector never takes one as a candidate nor
## walks into one, so large acyclic data costs it nothing.
pub var acyclic: bool = false
## `finish` disposes native state that other objects' finishes may still reach (a
## native object's close): in a garbage cycle it runs after every `deinit`, and before
## the finishes of the objects this one refers to, as releasing them in turn would.
pub var disposes: bool = false
## Objects alive: allocated and not yet destroyed.
local var live: usize = 0
## Allocated objects not yet finalized on the calling runtime thread.
pub func live_count() -> usize:
return live
## Where the thread's program is: `file:line:column` of the Luce statement running, set by
## the compiled code before each statement and restored when a function returns; what a
## trap names (§12.4).
pub local var site: str
## The caller's site again, deferred by every function the compiler writes.
pub func restore_site(saved: str):
site = saved
## Stop the program with `trap: file:line:column: message` on standard error and status 1.
pub func stop(message: str) -> never:
core.trap_located(site, message)
## A library trap on behalf of the value language: stops at its running statement when one is
## set, so the trap names the Luce line rather than the library's; returns otherwise, for the
## caller's own `trap` to name its statement.
pub func stop_at_site(message: str):
if site.length > 0:
core.trap_located(site, message)
func color_of(o: Object*) -> u8:
return o.flags & color_mask
func set_color(o: Object*, color: u8):
o.flags = (o.flags & ~color_mask) | color
func has(o: Object*, flag: u8) -> bool:
return (o.flags & flag) != 0
var next_context: @u64 = 1
var next_generation: @u64 = 1
local var thread_context: u64 = 0
local var runtime_depth: u32 = 0
## A unique runtime identity for this thread, independent of recycled OS thread IDs.
pub func context_id() -> u64:
if thread_context == 0:
thread_context = next_context.add(1, .relaxed)
if thread_context == 0:
stop("runtime context identity exhausted")
return thread_context
## Application runtime entry is separate from ordinary manual Base ownership.
## Generated callbacks require entry; native worker adapters establish it internally.
pub func enter():
_ = context_id()
runtime_depth += 1
## Trap when no owning context has been entered.
pub func require_context():
if runtime_depth == 0:
stop("a managed callback needs a runtime entry on this thread")
## Trap when `o` is touched from a thread other than its owner.
pub func check_thread(o: Object*):
if not has(o, immortal) and o.context != context_id():
stop("an object belongs to another runtime thread")
## Trap when `o` has already been finalized.
pub func check_alive(o: Object*):
check_thread(o)
if has(o, dead) or o.strong == 0:
stop("an object has already been destroyed")
## Reserve unpublished storage. The caller supplies a stable descriptor before
## publishing the object and abandons the storage if native initialization fails.
pub func reserve(info: Info*, size: usize, allocator: memory.Allocator? = none) -> Object*!:
if size < memory.size_of(Object):
stop("an object allocation is smaller than its header")
let selected = allocator else memory.heap
let block = selected.allocate(size, 16) else error(memory.exhausted, "memory.exhausted")
memory.set(block, 0)
let o = (Object*)block.data
o.strong = 1
o.info = info
o.context = context_id()
o.generation = next_generation.add(1, .relaxed)
if o.generation == 0:
stop("object allocation identity exhausted")
o.allocation_size = block.length
o.allocator = selected
live += 1
return o
## Allocate an object of class `info` and its header.
pub func allocate(info: Info*) -> Object*:
return allocate_sized(info, info.size)
## Runtime wrapper/text allocation retains Luce's trap-on-exhaustion policy.
pub func allocate_sized(info: Info*, size: usize) -> Object*:
return reserve(info, size) catch failure:
trap("out of memory")
## Add a strong reference to `o` and return it.
pub func retain(o: Object*) -> Object*:
check_alive(o)
if has(o, immortal):
return o
o.strong += 1
set_color(o, black)
return o
## Drop a strong reference to `o`, finalizing it when the count reaches zero. The value
## language's release: it stores before it releases, so the collector may run here.
pub func release(o: Object*):
release_from(o, true)
## `release` for a handle native code holds (`interop`): never runs the collector, which
## could trace a field the caller has released but not yet overwritten (`possible_root`).
pub func release_held(o: Object*):
release_from(o, false)
func release_from(o: Object*, may_collect: bool):
check_thread(o)
if has(o, immortal):
return
if has(o, collecting) or has(o, abandoning):
# a white object's field, released while the collector drops it, or a reference
# back to an object `abandon` is dropping: the count is bookkeeping only, the
# collector or `abandon` frees the object
if o.strong > 0:
o.strong -= 1
return
check_alive(o)
o.strong -= 1
if o.strong == 0:
destroy(o)
elif not o.info.acyclic:
possible_root(o, may_collect)
## `deinit`, then the fields, then the storage unless a weak reference keeps the shell.
func destroy(o: Object*):
set_color(o, black)
if let f = o.info.finish:
o.strong = 1
f(o)
if o.strong != 1:
trap("`deinit` published `self`")
o.strong = 0
o.info.drop(o)
live -= 1
o.flags |= dead
if not has(o, buffered):
release_shell(o)
## The storage goes when nothing, strong or weak or the candidate buffer, refers to it.
func release_shell(o: Object*):
if o.weak == 0 and has(o, dead) and not has(o, buffered):
let allocator = o.allocator else stop("an object has no allocation owner")
let block = u8[]((u8*)o, o.allocation_size)
free(block) in allocator
## Free unpublished storage without invoking the successful object's cleanup.
## The native initializer owns cleanup of its partially acquired resources.
pub func discard_unpublished(o: Object*):
check_alive(o)
if o.strong != 1 or o.weak != 0 or has(o, buffered):
stop("an incomplete object was published")
live -= 1
o.strong = 0
o.flags |= dead
release_shell(o)
## An `init` that failed: the fields assigned so far go, no `deinit` runs. Like `destroy`,
## the object leaves the collector's candidates first, so a collection the drop sets off
## never walks fields already released. What the fields held that refers back to the
## object, a closure over `self` or a child holding its parent, lets go of it in the drop;
## a reference still held after that was published by the failed `init`, which stops the
## program rather than leave it pointing at freed storage. The trap names the `init`'s own
## failure, `cause` raised at `cause_at`, which the publication would otherwise hide.
pub func abandon(o: Object*, cause: str, cause_at: str):
check_alive(o)
set_color(o, black)
o.flags |= abandoning
o.info.drop(o)
o.flags &= ~abandoning
if o.strong != 1:
core.trap_located_after(site, "a failed `init` published `self`", cause, cause_at)
live -= 1
o.strong = 0
o.flags |= dead
if not has(o, buffered):
release_shell(o)
## The allocation identity, or the concrete owner's identity through an adapter.
pub func identity(object: Object*) -> u64:
check_alive(object)
if let resolve = object.info.identity:
return resolve(object)
return object.generation
"""
## The text of `src/std/ownership/pool.lucb`.
let text_18: str = r"""#==============================================================================================
#
# pool - The temporaries pool
#
# DESCRIPTION:
# Owned values handed on as temporaries, released together by the next `drain`; the
# generators emit `mark`/`drain` pairs around a statement's operands.
#
#==============================================================================================
# mark: The temporaries pool ===================================================================
struct Pending:
var object: Object*?
var cell: void*?
var drop: (func(void*) -> unit)?
var size: usize
local var pending: Pending[]
local var pending_count: usize = 0
## The current pool position, to `drain` back to later.
pub func mark() -> usize:
return pending_count
func push_pending(p: Pending):
if pending_count == pending.length:
pending = grow(pending, pending_count, 64)
pending[pending_count] = p
pending_count += 1
## An owned object handed on as a temporary: released by the next `drain`.
pub func pool_object(o: Object*) -> Object*:
push_pending(Pending(object = o, cell = none, drop = none, size = 0))
return o
## An owned value of any type handed on as a temporary; `drop` releases what it holds.
pub func pool_value[T](v: T, drop: func(void*) -> unit) -> T:
let block = memory.heap.allocate(memory.size_of(T), memory.align_of(T)) else trap("out of memory")
memory.write[T](block.data, v)
push_pending(Pending(object = none, cell = block.data, drop = drop, size = memory.size_of(T)))
return v
## Releases every temporary pooled since `mark`, in the order they were pooled.
pub func drain(since: usize):
if pending_count <= since:
return
let count = pending_count
var i = since
while i < count:
let p = pending[i]
if let o = p.object:
release(o)
elif let cell = p.cell:
if let d = p.drop:
d(cell)
release_cell(cell, p.size)
i += 1
# a release pools nothing of its own, so nothing was added above `count`
pending_count = since
## Free a pooled value's block of `size` bytes, which `pool_value` took from the heap.
func release_cell(cell: void*, size: usize):
free(u8[]((u8*)cell, size)) in memory.heap
## `items` in heap storage with room for twice as many (`least` when it had none), its
## first `count` kept and its old storage freed: the growth of the runtime's own lists.
## Out of memory traps, as the bookkeeping has no failure to report into.
func grow[T](items: T[], count: usize, least: usize) -> T[]:
let grown = new T[items.length * 2 if items.length > 0 else least] --- in memory.heap catch failure:
trap("out of memory")
for i in 0..<count:
grown[i] = items[i]
if items.length > 0:
free(items) in memory.heap
return grown
"""
## The text of `src/std/ownership/cycles.lucb`.
let text_19: str = r"""#==============================================================================================
#
# cycles - The cycle collector
#
# DESCRIPTION:
# A synchronous trial-deletion collector for reference cycles on the owning thread,
# run when the candidate set fills at a release of the value language's, explicitly,
# or at the end of a run; never at a release through a native handle (`release_held`). An object whose type is
# acyclic is never a candidate and never walked into: it cannot be part of a cycle,
# and every visitor skipping it keeps the trial counts balanced.
#
#==============================================================================================
# mark: The cycle collector ====================================================================
pub let candidate_limit: usize = 1024
local var roots: Object*[]
local var root_count: usize = 0
local var whites: Object*[]
local var white_count: usize = 0
local var disposals: Object*[]
local var disposal_count: usize = 0
local var collector_running: bool = false
## The candidate count at which a release that may not collect drops the candidates that
## are no longer purple or alive (`purge_roots`), so the buffer grows only with live ones.
local var purge_at: usize = 0
## `LUCE_CYCLE_STRESS` in the environment: 0 not read yet, 1 absent, 2 present.
var stress_state: @u8
extern func stress_getenv as "getenv"(name: c.str) -> c.str?
## Whether `LUCE_CYCLE_STRESS` is set: the collector then runs at every new candidate of a
## release that may collect, so a collection happens at every such release that could leave
## a cycle, in `clear`, `remove`, a `deinit` and a failed `init` too (the stress mode of luce's tools/cycles_fuzz.py). The
## environment is read once; every thread reads the same answer.
func stressed() -> bool:
var state = stress_state.load(.relaxed)
if state == 0:
state = 2 if stress_getenv("LUCE_CYCLE_STRESS") != none else 1
stress_state.store(state, .relaxed)
return state == 2
func push_root(o: Object*):
if root_count == roots.length:
roots = grow(roots, root_count, candidate_limit)
roots[root_count] = o
root_count += 1
## The disposal order lists some of the garbage, so its buffer grows with the garbage's: a
## collection allocates only when the garbage outgrows what came before.
func push_white(o: Object*):
if white_count == whites.length:
whites = grow(whites, white_count, 256)
disposals = grow(disposals, 0, 256)
whites[white_count] = o
white_count += 1
## `o`, released and still held, may be a cycle's root. When the candidates fill, a release
## of the value language's collects (`may_collect`). A release through a native handle never
## does: native code may still hold a traced field whose edge it has just released, as in
## `old.release()` before `self.field = next`, and a collection would take that edge away a
## second time. Its candidates wait for the next release that may collect, or a `collect`.
func possible_root(o: Object*, may_collect: bool):
if color_of(o) != purple:
set_color(o, purple)
if not has(o, buffered):
o.flags |= buffered
push_root(o)
if (root_count >= candidate_limit or stressed()) and not collector_running:
if may_collect:
collect()
elif root_count >= purge_at:
purge_roots()
purge_at = candidate_limit if root_count * 2 < candidate_limit else root_count * 2
## A point where no native code is midway through replacing a traced field, such as the top
## of an event loop's turn: collects when releases through native handles filled the
## candidates (`possible_root`), which they never collect themselves.
pub func safe_point():
if root_count >= candidate_limit and not collector_running:
collect()
## Drop the candidates that are no longer purple or no longer alive, freeing the storage of
## the dead: it touches no object's fields, so any release may.
func purge_roots():
var kept: usize = 0
var i: usize = 0
while i < root_count:
let o = roots[i]
if color_of(o) == purple and o.strong > 0 and not has(o, dead):
roots[kept] = o
kept += 1
else:
o.flags &= ~buffered
release_shell(o)
i += 1
root_count = kept
## An object no walk enters: a literal, an acyclic one, or one `abandon` is dropping.
func skipped(child: Object*) -> bool:
return has(child, immortal) or child.info.acyclic or has(child, abandoning)
func visit_mark_gray(child: Object*, context: void*):
if skipped(child):
return
check_thread(child)
child.strong -= 1
mark_gray(child)
func mark_gray(o: Object*):
if color_of(o) != gray:
set_color(o, gray)
o.info.trace(o, visit_mark_gray, (void*)o)
func visit_scan(child: Object*, context: void*):
if skipped(child):
return
check_thread(child)
scan(child)
func visit_scan_black(child: Object*, context: void*):
if skipped(child):
return
check_thread(child)
child.strong += 1
if color_of(child) != black:
scan_black(child)
func scan(o: Object*):
if color_of(o) == gray:
if o.strong > 0:
scan_black(o)
else:
set_color(o, white)
o.info.trace(o, visit_scan, (void*)o)
func scan_black(o: Object*):
set_color(o, black)
o.info.trace(o, visit_scan_black, (void*)o)
func visit_gather(child: Object*, context: void*):
if skipped(child):
return
check_thread(child)
gather(child)
## The white objects, each once, flagged as being collected (`sort_whites` orders them).
func gather(o: Object*):
if color_of(o) == white and not has(o, collecting):
o.flags |= collecting
push_white(o)
o.info.trace(o, visit_gather, (void*)o)
## Undo the trial deletion of one edge out of a garbage object. `mark_gray` took one count
## for every edge it walked; `scan_black` gave back those out of objects found reachable, but
## an edge out of garbage is still missing, whether it points at more garbage or at a live
## object. Restoring every one lets each field release below be an ordinary release: a live
## object loses exactly the reference the garbage held, where it once lost two.
func visit_restore(child: Object*, context: void*):
if skipped(child):
return
check_thread(child)
child.strong += 1
func push_disposal(o: Object*):
disposals[disposal_count] = o
disposal_count += 1
func visit_order(child: Object*, context: void*):
if skipped(child):
return
order_from(child)
## A depth-first walk of the garbage that lists each disposing object after every one it
## reaches: read backwards, an object is disposed before what it refers to.
func order_from(o: Object*):
if not has(o, collecting) or has(o, ordered):
return
o.flags |= ordered
o.info.trace(o, visit_order, (void*)o)
if o.info.disposes:
push_disposal(o)
## The order the garbage's native objects are disposed in, after every `deinit` has run:
## each before the objects it refers to, as releasing the garbage one reference at a time
## would, so a close that reaches into what it holds finds it open. A cycle has no first
## object, and the walk treats the edge back to an object it is still inside as the one
## released last. It starts at the objects that do not dispose, oldest first, then the
## rest: a native object's references own what they reach, while a cycle is closed by the
## value language's objects and closures, so the walk enters it there.
func order_disposal():
disposal_count = 0
var i: usize = 0
while i < white_count:
if not whites[i].info.disposes:
order_from(whites[i])
i += 1
i = 0
while i < white_count:
order_from(whites[i])
i += 1
func run_finish(o: Object*):
if let f = o.info.finish:
let before = o.strong
o.strong += 1
f(o)
if o.strong > before + 1:
trap("`deinit` published `self`")
# Native disposal may release edges, including an edge to itself.
# Remove only the temporary finalizer reference, preserving those drops.
o.strong -= 1
## The garbage in the order its objects were made, oldest first (their generations): what a
## cycle's deinits and the releases of its fields follow, the same in a compiled program and
## Luce's interpreter whatever let go of what when (a heap sort, in place).
func sort_whites():
var start = white_count // 2
while start > 0:
start -= 1
sift_white(start, white_count)
var end = white_count
while end > 1:
end -= 1
let top = whites[0]
whites[0] = whites[end]
whites[end] = top
sift_white(0, end)
func sift_white(first: usize, end: usize):
var root = first
while root * 2 + 1 < end:
var child = root * 2 + 1
if child + 1 < end and whites[child + 1].generation > whites[child].generation:
child += 1
if whites[root].generation >= whites[child].generation:
return
let held = whites[root]
whites[root] = whites[child]
whites[child] = held
root = child
## Free the garbage: deinits first, then the native objects' disposals, then every object's
## fields, then the storage.
pub func collect():
if collector_running:
return
collector_running = true
purge_at = candidate_limit
# candidates still purple and alive stay; the rest leave the buffer
purge_roots()
var i: usize = 0
while i < root_count:
mark_gray(roots[i])
i += 1
i = 0
while i < root_count:
scan(roots[i])
i += 1
white_count = 0
i = 0
while i < root_count:
gather(roots[i])
i += 1
i = 0
while i < root_count:
roots[i].flags &= ~buffered
i += 1
root_count = 0
sort_whites()
# the counts the trial deletion took for the garbage's edges are restored, to garbage and
# to live objects alike, so that every field release below is an ordinary one; a release
# of a white object then only lowers its count
i = 0
while i < white_count:
whites[i].info.trace(whites[i], visit_restore, (void*)whites[i])
i += 1
# the order is taken before any finish runs, over the garbage as it was found
order_disposal()
i = 0
while i < white_count:
if not whites[i].info.disposes:
run_finish(whites[i])
i += 1
i = disposal_count
while i > 0:
i -= 1
run_finish(disposals[i])
disposal_count = 0
i = 0
while i < white_count:
whites[i].info.drop(whites[i])
i += 1
i = 0
while i < white_count:
let o = whites[i]
live -= 1
o.strong = 0
o.flags = dead
release_shell(o)
i += 1
white_count = 0
collector_running = false
"""
## The text of `src/std/ownership/weak.lucb`.
let text_20: str = r"""#==============================================================================================
#
# weak - Weak references
#
# DESCRIPTION:
# A weak reference watches an object without keeping it alive, and yields it again only
# while it is still alive.
#
#==============================================================================================
# mark: Weak references ========================================================================
## A weak reference: it watches an object without keeping it alive.
pub struct Weak:
## The watched object, or none once it is gone.
pub var target: Object*?
## A weak reference watching `o`.
pub func weak_make(o: Object*) -> Weak:
check_alive(o)
if not has(o, immortal):
o.weak += 1
return Weak(target = o)
## A copy of `w`, counting one more watcher of its object.
pub func weak_copy(w: Weak) -> Weak:
if let o = w.target:
check_thread(o)
if not has(o, immortal):
o.weak += 1
return w
## Drop `w`; the object's shell is freed when its last watcher goes.
pub func weak_drop(w: Weak):
if let o = w.target:
check_thread(o)
if not has(o, immortal):
o.weak -= 1
release_shell(o)
## The object with a fresh strong reference, or none once it is dead.
pub func weak_get(w: Weak) -> Object*?:
if let o = w.target:
check_thread(o)
if not has(o, dead):
return retain(o)
return none
"""
## The text of `src/std/ownership/lifecycle.lucb`.
let text_21: str = r"""#==============================================================================================
#
# lifecycle - The end of a run
#
# DESCRIPTION:
# After `main` and after each worker, the collector runs and anything still alive is a
# defect that stops the program.
#
#==============================================================================================
# mark: The end of a run =======================================================================
import strings
## After `main`: the collector runs, and anything still alive is a defect. The exit status.
pub func finish_run(status: i32) -> i32:
drain(0)
settle()
if live != 0:
var room: u8[96]
let text = strings.format(room, f"runtime: {live} objects alive at exit\n") catch failure:
recover "runtime: objects alive at exit\n"
var e = io.stderr()
_ = e.write(text.bytes) catch failure: recover 0
return 3
release_buffers()
runtime_depth = 0
thread_context = 0
return status
## A worker's end (§14): its heap is its own, and it too must leave nothing alive once
## its arguments and result are released; what it does leave stops the program.
pub func finish_task():
drain(0)
settle()
if live != 0:
var room: u8[96]
let text = strings.format(room, f"runtime: {live} objects alive when a task ended\n") catch failure:
recover "runtime: objects alive when a task ended\n"
var e = io.stderr()
_ = e.write(text.bytes) catch failure: recover 0
core.exit(3)
release_buffers()
runtime_depth = 0
thread_context = 0
## Collecting a cycle drops its members' fields, which can release shared objects the cycle
## held: that buffers fresh candidates and may pool temporaries. During a run the next
## collection absorbs them, but at the end of a run there is no next, so repeat until the
## collector's buffers settle. A bound guards against a fixpoint that never empties.
func settle():
collect()
var guard: usize = 0
while (pending_count > 0 or root_count > 0) and guard < 1000:
drain(0)
collect()
guard += 1
## Empty object counts do not include the collector's and temporaries pool's
## backing allocations. A finished thread must release those too: its TLS roots
## disappear at thread exit, so retaining capacity would leak on every spawn.
func release_buffers():
assert(pending_count == 0 and root_count == 0 and white_count == 0)
if pending.length > 0:
free(pending) in memory.heap
if roots.length > 0:
free(roots) in memory.heap
if whites.length > 0:
free(whites) in memory.heap
if disposals.length > 0:
free(disposals) in memory.heap
var empty_pending: Pending[]
var empty_objects: Object*[]
pending = empty_pending
roots = empty_objects
whites = empty_objects
disposals = empty_objects
"""
## The text of `src/std/strings/module.lucb`.
let text_22: str = r"""#==============================================================================================
#
# strings - Text operations over `str` views
#
# DESCRIPTION:
# Text operations over `str` views; what allocates says so and uses the current allocator,
# and says how the allocation is given back: `release` for a text made here, `free` for
# the borrowed-field arrays returned by split and split_by.
#
#==============================================================================================
import float_text
import io
import memory
import numerals
## An empty separator or needle where one is required.
pub let invalid_separator: ErrorCode = ErrorCode.package(48)
## A result exceeds the caller's byte limit.
pub let output_too_large: ErrorCode = ErrorCode.package(49)
## A radix outside 2 through 36.
pub let invalid_radix: ErrorCode = ErrorCode.package(50)
## The text is not a number in the given radix.
pub let invalid_number: ErrorCode = ErrorCode.package(51)
## The parsed value does not fit its type.
pub let number_out_of_range: ErrorCode = ErrorCode.package(52)
"""
## The text of `src/std/strings/search.lucb`.
let text_23: str = r"""#==============================================================================================
#
# search - Substring search over `str`
#
# DESCRIPTION:
# Prefix, suffix, and forward or reverse substring search, all byte-exact and
# allocation-free.
#
#==============================================================================================
## Whether `text` begins with `prefix`.
pub func starts_with(text: str, prefix: str) -> bool:
if prefix.length > text.length:
return false
return (str)text.bytes[..<prefix.length] == prefix
## Whether `text` ends with `suffix`.
pub func ends_with(text: str, suffix: str) -> bool:
if suffix.length > text.length:
return false
return (str)text.bytes[text.length - suffix.length..] == suffix
## The byte offset of the first exact needle, or none. Empty needles match at
## zero. No allocation or UTF-8 validation occurs; embedded NUL bytes are ordinary
## data. The search takes linear time and constant auxiliary space.
pub func find(text: str, needle: str) -> usize?:
return search_bytes(text.bytes, needle.bytes, false)
## Search at or after a byte offset. An offset past the end returns none, including
## for an empty needle. This does not require a Unicode scalar boundary.
pub func find_from(text: str, needle: str, start: usize) -> usize?:
if start > text.length:
return none
let offset = search_bytes(text.bytes[start..], needle.bytes, false) else return none
return start + offset
## The last exact occurrence, including overlaps. Empty needles match at the end.
## Like find, this uses byte offsets, linear time, constant space and no allocation.
pub func find_last(text: str, needle: str) -> usize?:
let offset = search_bytes(text.bytes, needle.bytes, true) else return none
return text.length - needle.length - offset
## The byte offset of the last `byte` in `text`.
pub func last(text: str, byte: u8) -> usize?:
var i = text.length
while i > 0:
i -= 1
if text.bytes[i] == byte:
return i
return none
## Whether `needle` occurs anywhere in `text`.
pub func contains(text: str, needle: str) -> bool:
return find(text, needle) != none
## Two-way matching splits the needle at a critical factorization. Match its right
## part first, then the left; the period lets repeated prefixes be skipped without
## comparing them again. Reversed indexing gives the same bound for find_last.
func search_bytes(text: const u8[], needle: const u8[], reversed: bool) -> usize?:
if needle.length == 0:
return 0
if needle.length > text.length:
return none
let (forward_split, forward_period) = maximal_suffix(needle, reversed, false)
let (backward_split, backward_period) = maximal_suffix(needle, reversed, true)
var critical = forward_split
var period = forward_period
if backward_split > critical:
critical = backward_split
period = backward_period
var periodic = critical <= needle.length - period
var index: usize = 0
while periodic and index < critical:
if search_byte(needle, index, reversed) != search_byte(needle, period + index, reversed):
periodic = false
index += 1
var remembered: usize = 0
if periodic:
remembered = needle.length - period
else:
let right = needle.length - critical + 1
period = critical if critical > right else right
var position: usize = 0
var matched_prefix: usize = 0
while needle.length <= text.length - position:
var right = critical if critical > matched_prefix else matched_prefix
while right < needle.length and search_byte(needle, right, reversed) == search_byte(text, position + right, reversed):
right += 1
if right < needle.length:
position += right - critical + 1
matched_prefix = 0
continue
var left = critical
while left > matched_prefix and search_byte(needle, left - 1, reversed) == search_byte(text, position + left - 1, reversed):
left -= 1
if left <= matched_prefix:
return position
position += period
matched_prefix = remembered
return none
## Positions represent suffix starts rather than signed -1 sentinels. Every
## advance stays within the needle; no index arithmetic wraps at usize maximum.
func maximal_suffix(needle: const u8[], reversed: bool, opposite_order: bool) -> (usize, usize):
var left: usize = 0
var right: usize = 1
var offset: usize = 0
var period: usize = 1
while right < needle.length and offset < needle.length - right:
let a = search_byte(needle, right + offset, reversed)
let b = search_byte(needle, left + offset, reversed)
if a == b:
if offset + 1 == period:
right += period
offset = 0
else:
offset += 1
elif (a < b) != opposite_order:
right += offset + 1
offset = 0
period = right - left
else:
left = right
right = left + 1
offset = 0
period = 1
return (left, period)
func search_byte(bytes: const u8[], index: usize, reversed: bool) -> u8:
return bytes[bytes.length - index - 1] if reversed else bytes[index]
"""
## The text of `src/std/strings/split.lucb`.
let text_24: str = r"""#==============================================================================================
#
# split - Splitting text into views
#
# DESCRIPTION:
# Split `str` at a byte or a substring into views over the input, once, last, or every
# occurrence.
#
#==============================================================================================
## `text` split at every `separator`, as views into it; the array is an allocation of the
## current allocator, given back with `free`.
pub func split(text: str, separator: u8) -> str[]!:
var count: usize = 1
for b in text.bytes:
if b == separator:
count = count +? 1 else error(memory.exhausted, "the split result is too large")
let parts = try new str[count] ---
var n: usize = 0
var start: usize = 0
var i: usize = 0
let b = text.bytes
while i < b.length:
if b[i] == separator:
parts[n] = (str)b[start..<i]
n += 1
start = i + 1
i += 1
parts[n] = (str)b[start..]
return parts
## Split a borrowed text at a nonempty byte substring. Empty fields, including the
## final one after a trailing separator, are preserved. Zero is an exhausted
## iterator. Keep text and separator storage alive and unchanged while iterating.
pub struct SplitIterator:
var text: str
var separator: str
var position: usize
var remaining: usize
var active: bool
## max_parts=0 has no limit; one returns the entire input as a single field.
## No allocation occurs. An empty separator reports invalid_separator.
pub static func over(text: str, separator: str, max_parts: usize = 0) -> SplitIterator!:
if separator.length == 0:
error(invalid_separator, "a split iterator requires a nonempty separator")
return SplitIterator(text = text, separator = separator, remaining = max_parts, active = true)
## The next borrowed field. An empty field is present; none means exhausted.
pub func next() -> str?:
if not self.active:
return none
if self.remaining == 1:
self.active = false
return (str)self.text.bytes[self.position..]
let found = find_from(self.text, self.separator, self.position)
if let end = found:
let start = self.position
self.position = end + self.separator.length
if self.remaining > 1:
self.remaining -= 1
return (str)self.text.bytes[start..<end]
self.active = false
return (str)self.text.bytes[self.position..]
## An allocated array of borrowed fields with SplitIterator semantics. Free the
## array through the allocator current on entry; its field bytes still borrow text.
## max_parts=0 has no limit. An empty separator reports invalid_separator.
pub func split_by(text: str, separator: str, max_parts: usize = 0) -> str[]!:
let initial = try SplitIterator.over(text, separator, max_parts)
var counter = initial
var count: usize = 0
while counter.next() != none:
count = count +? 1 else error(memory.exhausted, "the split result is too large")
let fields = try new str[count] ---
var iterator = initial
var index: usize = 0
while let field = iterator.next():
fields[index] = field
index += 1
return fields
## Borrow the text before and after the first separator, or none if absent. The
## separator is omitted. An empty separator splits at the start: (empty, text).
pub func split_once(text: str, separator: str) -> (str, str)?:
let position = find(text, separator) else return none
return ((str)text.bytes[..<position], (str)text.bytes[position + separator.length..])
## Borrow the text before and after the last separator, including overlapping
## candidates. An empty separator splits at the end: (text, empty).
pub func split_last(text: str, separator: str) -> (str, str)?:
let position = find_last(text, separator) else return none
return ((str)text.bytes[..<position], (str)text.bytes[position + separator.length..])
"""
## The text of `src/std/strings/transform.lucb`.
let text_25: str = r"""#==============================================================================================
#
# transform - Trimming, joining and case
#
# DESCRIPTION:
# Trim, join, copy and ASCII case changes over `str`, returning fresh owned text.
#
#==============================================================================================
func is_space(byte: u8) -> bool:
return byte == ' ' or byte == '\n' or byte == '\t' or byte == '\r'
## `text` without leading and trailing spaces, tabs, and line ends: a view.
pub func trim(text: str) -> str:
var start: usize = 0
var end = text.length
let b = text.bytes
while start < end and is_space(b[start]):
start += 1
while end > start and is_space(b[end - 1]):
end -= 1
return (str)b[start..<end]
## The pieces joined with `separator` between them, NUL-terminated in the current
## allocator; `release` gives it back.
pub func join(pieces: const str[], separator: str) -> str!:
var total: usize = 0
var i: usize = 0
while i < pieces.length:
total = total +? pieces[i].length else error(memory.exhausted, "the joined text is too large")
if i + 1 < pieces.length:
total = total +? separator.length else error(memory.exhausted, "the joined text is too large")
i += 1
let out = try terminated_buffer(total)
var n: usize = 0
i = 0
while i < pieces.length:
memory.copy(out[n..], pieces[i].bytes, pieces[i].length)
n += pieces[i].length
if i + 1 < pieces.length:
memory.copy(out[n..], separator.bytes, separator.length)
n += separator.length
i += 1
out[n] = 0
return (str)out[..<n]
## A copy of `text` in the current allocator, NUL-terminated so it may be a `c.str`;
## `release` gives it back.
pub func copy(text: str) -> str!:
let out = try terminated_buffer(text.length)
memory.copy(out, text.bytes, text.length)
out[text.length] = 0
return (str)out[..<text.length]
## As much of `text` as fits in `buffer`, copied there and cut at a character's start; the
## result views `buffer`. A caught failure's message is kept past its handler so, or with
## `copy` (§11.3).
pub func copy_into(buffer: u8[], text: str) -> str:
var n = text.length
if n > buffer.length:
n = buffer.length
while n > 0 and (text.bytes[n] & 0xC0) == 0x80:
n -= 1
memory.copy(buffer, text.bytes, n)
return (str)buffer[..<n]
## ASCII case folding; other bytes pass through. NUL-terminated in the current
## allocator; `release` gives the text back.
pub func to_upper(text: str) -> str!:
let out = try terminated_buffer(text.length)
var i: usize = 0
for b in text.bytes:
out[i] = b - 32 if b >= 'a' and b <= 'z' else b
i += 1
out[i] = 0
return (str)out[..<i]
## ASCII lowercase; other bytes pass through unchanged. The result is a
## NUL-terminated current-allocator allocation released with release.
pub func to_lower(text: str) -> str!:
let out = try terminated_buffer(text.length)
var i: usize = 0
for b in text.bytes:
out[i] = b + 32 if b >= 'A' and b <= 'Z' else b
i += 1
out[i] = 0
return (str)out[..<i]
## Give back text from join, copy, replace, to_upper or to_lower: the allocation is
## one byte longer than the text, its NUL.
pub func release(text: str):
free(u8[]((u8*)text.bytes.data, text.length + 1))
## Allocate text storage including its terminator, reporting size overflow as the
## same fallible outcome as an allocation refusal. Callers initialize every byte.
func terminated_buffer(length: usize) -> u8[]!:
let size = length +? 1 else error(memory.exhausted, "the text is too large")
return try new u8[size] ---
"""
## The text of `src/std/strings/replace.lucb`.
let text_26: str = r"""#==============================================================================================
#
# replace - Substring replacement
#
# DESCRIPTION:
# Replace nonoverlapping byte substrings left to right into fresh owned text, with
# optional caps on the count and the result size.
#
#==============================================================================================
## Replace nonoverlapping byte substrings from left to right. Empty needles report
## invalid_separator. max_replacements=0 replaces all matches; max_bytes optionally
## limits result bytes excluding its terminator, reporting output_too_large before
## allocation. A result is always a fresh NUL-terminated current-allocator allocation,
## including unchanged or empty results; release gives it back. Inputs remain borrowed
## and unchanged and may overlap each other. No UTF-8 validation occurs.
pub func replace(text: str, needle: str, replacement: str, max_replacements: usize = 0, max_bytes: usize? = none) -> str!:
if needle.length == 0:
error(invalid_separator, "replacement requires a nonempty needle")
var size = text.length
var count: usize = 0
var position: usize = 0
while max_replacements == 0 or count < max_replacements:
let found = find_from(text, needle, position) else break
if replacement.length >= needle.length:
size = size +? (replacement.length - needle.length) else error(memory.exhausted, "the replacement result is too large")
else:
size -= needle.length - replacement.length
position = found + needle.length
count += 1
if let maximum = max_bytes:
if size > maximum:
error(output_too_large, "the replacement result exceeds its byte limit")
let output = try terminated_buffer(size)
var used: usize = 0
var copied: usize = 0
position = 0
while copied < count:
let found = find_from(text, needle, position) else trap("replacement input changed between scans")
let prefix = found - position
memory.copy(output[used..], text.bytes[position..<found], prefix)
used += prefix
memory.copy(output[used..], replacement.bytes, replacement.length)
used += replacement.length
position = found + needle.length
copied += 1
let tail = text.length - position
memory.copy(output[used..], text.bytes[position..], tail)
used += tail
output[used] = 0
return (str)output[..<used]
"""
## The text of `src/std/strings/numbers.lucb`.
let text_27: str = r"""#==============================================================================================
#
# numbers - Integer formatting and parsing
#
# DESCRIPTION:
# Signed and unsigned integers in radix 2 through 36, ASCII digits only, over borrowed or
# caller-provided storage.
#
#==============================================================================================
## A signed integer in radix 2 through 36. ASCII letters are case-insensitive
## digits; prefixes are not interpreted. Optional +/- and leading zeroes are
## accepted. None reports an invalid radix, invalid byte, missing digit or overflow.
pub func parse_i64(text: str, radix: u32 = 10) -> i64?:
if radix < 2 or radix > 36 or text.length == 0:
return none
var position: usize = 0
var negative = false
if text.bytes[0] == '-' or text.bytes[0] == '+':
negative = text.bytes[0] == '-'
position = 1
if position == text.length:
return none
# Accumulate on the sign's side so the negative endpoint fits without abs.
var value: i64 = 0
while position < text.length:
let digit = integer_digit(text.bytes[position]) else return none
if digit >= radix:
return none
let scaled = value *? (i64)radix else return none
value = (scaled -? (i64)digit if negative else scaled +? (i64)digit) else return none
position += 1
return value
## Unsigned counterpart of parse_i64. A leading plus is accepted; every minus,
## including -0, is rejected. The entire input must fit u64 in the requested radix.
pub func parse_u64(text: str, radix: u32 = 10) -> u64?:
if radix < 2 or radix > 36 or text.length == 0:
return none
var position: usize = 1 if text.bytes[0] == '+' else 0
if position == text.length:
return none
var value: u64 = 0
while position < text.length:
let digit = integer_digit(text.bytes[position]) else return none
if digit >= radix:
return none
let scaled = value *? (u64)radix else return none
value = scaled +? (u64)digit else return none
position += 1
return value
## Canonical lowercase digits, with a minus only for negative values, no prefix,
## padding or terminator. Return a borrowed view of buffer. A 65-byte buffer always
## suffices. Invalid radix or insufficient capacity leaves buffer unchanged.
pub func format_i64(value: i64, buffer: u8[], radix: u32 = 10) -> str!:
let magnitude = (u64)(-(value + 1)) + 1 if value < 0 else (u64)value
return try format_integer(magnitude, value < 0, buffer, radix)
## Unsigned counterpart of format_i64; at most 64 bytes are needed. io.full reports
## insufficient capacity and invalid_radix reports a radix outside 2 through 36.
pub func format_u64(value: u64, buffer: u8[], radix: u32 = 10) -> str!:
return try format_integer(value, false, buffer, radix)
func integer_digit(byte: u8) -> u32?:
if byte >= '0' and byte <= '9':
return (u32)(byte - '0')
if byte >= 'a' and byte <= 'z':
return (u32)(byte - 'a') + 10
if byte >= 'A' and byte <= 'Z':
return (u32)(byte - 'A') + 10
return none
func format_integer(value: u64, negative: bool, buffer: u8[], radix: u32) -> str!:
if radix < 2 or radix > 36:
error(invalid_radix, "an integer radix must be between 2 and 36")
let digits = "0123456789abcdefghijklmnopqrstuvwxyz"
var reversed: u8[65]
var count: usize = 0
var remaining = value
while true:
reversed[count] = digits.bytes[(usize)(remaining % (u64)radix)]
count += 1
remaining //= (u64)radix
if remaining == 0:
break
if negative:
reversed[count] = '-'
count += 1
if buffer.length < count:
error(io.full, "the destination cannot hold the formatted integer")
var position: usize = 0
while position < count:
buffer[position] = reversed[count - position - 1]
position += 1
return (str)buffer[..<count]
"""
## The text of `src/std/strings/float_tables.lucb`.
let text_28: str = r"""#==============================================================================================
#
# float_tables - Eisel-Lemire's powers of ten, for strings' parsing
#
# DESCRIPTION:
# Written by tools/float_tables.py; do not edit.
#
#==============================================================================================
# mark: Parsing ================================================================================
## 10^q for q in -348..=347 as the top 128 bits of its binary expansion, rounded down: the high
## word, then the low word.
let lemire_powers: u64[1392] = [
0xFA8FD5A0081C0288, 0x1732C869CD60E453, 0x9C99E58405118195, 0x0E7FBD42205C8EB4,
0xC3C05EE50655E1FA, 0x521FAC92A873B261, 0xF4B0769E47EB5A78, 0xE6A797B752909EF9,
0x98EE4A22ECF3188B, 0x9028BED2939A635C, 0xBF29DCABA82FDEAE, 0x7432EE873880FC33,
0xEEF453D6923BD65A, 0x113FAA2906A13B3F, 0x9558B4661B6565F8, 0x4AC7CA59A424C507,
0xBAAEE17FA23EBF76, 0x5D79BCF00D2DF649, 0xE95A99DF8ACE6F53, 0xF4D82C2C107973DC,
0x91D8A02BB6C10594, 0x79071B9B8A4BE869, 0xB64EC836A47146F9, 0x9748E2826CDEE284,
0xE3E27A444D8D98B7, 0xFD1B1B2308169B25, 0x8E6D8C6AB0787F72, 0xFE30F0F5E50E20F7,
0xB208EF855C969F4F, 0xBDBD2D335E51A935, 0xDE8B2B66B3BC4723, 0xAD2C788035E61382,
0x8B16FB203055AC76, 0x4C3BCB5021AFCC31, 0xADDCB9E83C6B1793, 0xDF4ABE242A1BBF3D,
0xD953E8624B85DD78, 0xD71D6DAD34A2AF0D, 0x87D4713D6F33AA6B, 0x8672648C40E5AD68,
0xA9C98D8CCB009506, 0x680EFDAF511F18C2, 0xD43BF0EFFDC0BA48, 0x0212BD1B2566DEF2,
0x84A57695FE98746D, 0x014BB630F7604B57, 0xA5CED43B7E3E9188, 0x419EA3BD35385E2D,
0xCF42894A5DCE35EA, 0x52064CAC828675B9, 0x818995CE7AA0E1B2, 0x7343EFEBD1940993,
0xA1EBFB4219491A1F, 0x1014EBE6C5F90BF8, 0xCA66FA129F9B60A6, 0xD41A26E077774EF6,
0xFD00B897478238D0, 0x8920B098955522B4, 0x9E20735E8CB16382, 0x55B46E5F5D5535B0,
0xC5A890362FDDBC62, 0xEB2189F734AA831D, 0xF712B443BBD52B7B, 0xA5E9EC7501D523E4,
0x9A6BB0AA55653B2D, 0x47B233C92125366E, 0xC1069CD4EABE89F8, 0x999EC0BB696E840A,
0xF148440A256E2C76, 0xC00670EA43CA250D, 0x96CD2A865764DBCA, 0x380406926A5E5728,
0xBC807527ED3E12BC, 0xC605083704F5ECF2, 0xEBA09271E88D976B, 0xF7864A44C633682E,
0x93445B8731587EA3, 0x7AB3EE6AFBE0211D, 0xB8157268FDAE9E4C, 0x5960EA05BAD82964,
0xE61ACF033D1A45DF, 0x6FB92487298E33BD, 0x8FD0C16206306BAB, 0xA5D3B6D479F8E056,
0xB3C4F1BA87BC8696, 0x8F48A4899877186C, 0xE0B62E2929ABA83C, 0x331ACDABFE94DE87,
0x8C71DCD9BA0B4925, 0x9FF0C08B7F1D0B14, 0xAF8E5410288E1B6F, 0x07ECF0AE5EE44DD9,
0xDB71E91432B1A24A, 0xC9E82CD9F69D6150, 0x892731AC9FAF056E, 0xBE311C083A225CD2,
0xAB70FE17C79AC6CA, 0x6DBD630A48AAF406, 0xD64D3D9DB981787D, 0x092CBBCCDAD5B108,
0x85F0468293F0EB4E, 0x25BBF56008C58EA5, 0xA76C582338ED2621, 0xAF2AF2B80AF6F24E,
0xD1476E2C07286FAA, 0x1AF5AF660DB4AEE1, 0x82CCA4DB847945CA, 0x50D98D9FC890ED4D,
0xA37FCE126597973C, 0xE50FF107BAB528A0, 0xCC5FC196FEFD7D0C, 0x1E53ED49A96272C8,
0xFF77B1FCBEBCDC4F, 0x25E8E89C13BB0F7A, 0x9FAACF3DF73609B1, 0x77B191618C54E9AC,
0xC795830D75038C1D, 0xD59DF5B9EF6A2417, 0xF97AE3D0D2446F25, 0x4B0573286B44AD1D,
0x9BECCE62836AC577, 0x4EE367F9430AEC32, 0xC2E801FB244576D5, 0x229C41F793CDA73F,
0xF3A20279ED56D48A, 0x6B43527578C1110F, 0x9845418C345644D6, 0x830A13896B78AAA9,
0xBE5691EF416BD60C, 0x23CC986BC656D553, 0xEDEC366B11C6CB8F, 0x2CBFBE86B7EC8AA8,
0x94B3A202EB1C3F39, 0x7BF7D71432F3D6A9, 0xB9E08A83A5E34F07, 0xDAF5CCD93FB0CC53,
0xE858AD248F5C22C9, 0xD1B3400F8F9CFF68, 0x91376C36D99995BE, 0x23100809B9C21FA1,
0xB58547448FFFFB2D, 0xABD40A0C2832A78A, 0xE2E69915B3FFF9F9, 0x16C90C8F323F516C,
0x8DD01FAD907FFC3B, 0xAE3DA7D97F6792E3, 0xB1442798F49FFB4A, 0x99CD11CFDF41779C,
0xDD95317F31C7FA1D, 0x40405643D711D583, 0x8A7D3EEF7F1CFC52, 0x482835EA666B2572,
0xAD1C8EAB5EE43B66, 0xDA3243650005EECF, 0xD863B256369D4A40, 0x90BED43E40076A82,
0x873E4F75E2224E68, 0x5A7744A6E804A291, 0xA90DE3535AAAE202, 0x711515D0A205CB36,
0xD3515C2831559A83, 0x0D5A5B44CA873E03, 0x8412D9991ED58091, 0xE858790AFE9486C2,
0xA5178FFF668AE0B6, 0x626E974DBE39A872, 0xCE5D73FF402D98E3, 0xFB0A3D212DC8128F,
0x80FA687F881C7F8E, 0x7CE66634BC9D0B99, 0xA139029F6A239F72, 0x1C1FFFC1EBC44E80,
0xC987434744AC874E, 0xA327FFB266B56220, 0xFBE9141915D7A922, 0x4BF1FF9F0062BAA8,
0x9D71AC8FADA6C9B5, 0x6F773FC3603DB4A9, 0xC4CE17B399107C22, 0xCB550FB4384D21D3,
0xF6019DA07F549B2B, 0x7E2A53A146606A48, 0x99C102844F94E0FB, 0x2EDA7444CBFC426D,
0xC0314325637A1939, 0xFA911155FEFB5308, 0xF03D93EEBC589F88, 0x793555AB7EBA27CA,
0x96267C7535B763B5, 0x4BC1558B2F3458DE, 0xBBB01B9283253CA2, 0x9EB1AAEDFB016F16,
0xEA9C227723EE8BCB, 0x465E15A979C1CADC, 0x92A1958A7675175F, 0x0BFACD89EC191EC9,
0xB749FAED14125D36, 0xCEF980EC671F667B, 0xE51C79A85916F484, 0x82B7E12780E7401A,
0x8F31CC0937AE58D2, 0xD1B2ECB8B0908810, 0xB2FE3F0B8599EF07, 0x861FA7E6DCB4AA15,
0xDFBDCECE67006AC9, 0x67A791E093E1D49A, 0x8BD6A141006042BD, 0xE0C8BB2C5C6D24E0,
0xAECC49914078536D, 0x58FAE9F773886E18, 0xDA7F5BF590966848, 0xAF39A475506A899E,
0x888F99797A5E012D, 0x6D8406C952429603, 0xAAB37FD7D8F58178, 0xC8E5087BA6D33B83,
0xD5605FCDCF32E1D6, 0xFB1E4A9A90880A64, 0x855C3BE0A17FCD26, 0x5CF2EEA09A55067F,
0xA6B34AD8C9DFC06F, 0xF42FAA48C0EA481E, 0xD0601D8EFC57B08B, 0xF13B94DAF124DA26,
0x823C12795DB6CE57, 0x76C53D08D6B70858, 0xA2CB1717B52481ED, 0x54768C4B0C64CA6E,
0xCB7DDCDDA26DA268, 0xA9942F5DCF7DFD09, 0xFE5D54150B090B02, 0xD3F93B35435D7C4C,
0x9EFA548D26E5A6E1, 0xC47BC5014A1A6DAF, 0xC6B8E9B0709F109A, 0x359AB6419CA1091B,
0xF867241C8CC6D4C0, 0xC30163D203C94B62, 0x9B407691D7FC44F8, 0x79E0DE63425DCF1D,
0xC21094364DFB5636, 0x985915FC12F542E4, 0xF294B943E17A2BC4, 0x3E6F5B7B17B2939D,
0x979CF3CA6CEC5B5A, 0xA705992CEECF9C42, 0xBD8430BD08277231, 0x50C6FF782A838353,
0xECE53CEC4A314EBD, 0xA4F8BF5635246428, 0x940F4613AE5ED136, 0x871B7795E136BE99,
0xB913179899F68584, 0x28E2557B59846E3F, 0xE757DD7EC07426E5, 0x331AEADA2FE589CF,
0x9096EA6F3848984F, 0x3FF0D2C85DEF7621, 0xB4BCA50B065ABE63, 0x0FED077A756B53A9,
0xE1EBCE4DC7F16DFB, 0xD3E8495912C62894, 0x8D3360F09CF6E4BD, 0x64712DD7ABBBD95C,
0xB080392CC4349DEC, 0xBD8D794D96AACFB3, 0xDCA04777F541C567, 0xECF0D7A0FC5583A0,
0x89E42CAAF9491B60, 0xF41686C49DB57244, 0xAC5D37D5B79B6239, 0x311C2875C522CED5,
0xD77485CB25823AC7, 0x7D633293366B828B, 0x86A8D39EF77164BC, 0xAE5DFF9C02033197,
0xA8530886B54DBDEB, 0xD9F57F830283FDFC, 0xD267CAA862A12D66, 0xD072DF63C324FD7B,
0x8380DEA93DA4BC60, 0x4247CB9E59F71E6D, 0xA46116538D0DEB78, 0x52D9BE85F074E608,
0xCD795BE870516656, 0x67902E276C921F8B, 0x806BD9714632DFF6, 0x00BA1CD8A3DB53B6,
0xA086CFCD97BF97F3, 0x80E8A40ECCD228A4, 0xC8A883C0FDAF7DF0, 0x6122CD128006B2CD,
0xFAD2A4B13D1B5D6C, 0x796B805720085F81, 0x9CC3A6EEC6311A63, 0xCBE3303674053BB0,
0xC3F490AA77BD60FC, 0xBEDBFC4411068A9C, 0xF4F1B4D515ACB93B, 0xEE92FB5515482D44,
0x991711052D8BF3C5, 0x751BDD152D4D1C4A, 0xBF5CD54678EEF0B6, 0xD262D45A78A0635D,
0xEF340A98172AACE4, 0x86FB897116C87C34, 0x9580869F0E7AAC0E, 0xD45D35E6AE3D4DA0,
0xBAE0A846D2195712, 0x8974836059CCA109, 0xE998D258869FACD7, 0x2BD1A438703FC94B,
0x91FF83775423CC06, 0x7B6306A34627DDCF, 0xB67F6455292CBF08, 0x1A3BC84C17B1D542,
0xE41F3D6A7377EECA, 0x20CABA5F1D9E4A93, 0x8E938662882AF53E, 0x547EB47B7282EE9C,
0xB23867FB2A35B28D, 0xE99E619A4F23AA43, 0xDEC681F9F4C31F31, 0x6405FA00E2EC94D4,
0x8B3C113C38F9F37E, 0xDE83BC408DD3DD04, 0xAE0B158B4738705E, 0x9624AB50B148D445,
0xD98DDAEE19068C76, 0x3BADD624DD9B0957, 0x87F8A8D4CFA417C9, 0xE54CA5D70A80E5D6,
0xA9F6D30A038D1DBC, 0x5E9FCF4CCD211F4C, 0xD47487CC8470652B, 0x7647C3200069671F,
0x84C8D4DFD2C63F3B, 0x29ECD9F40041E073, 0xA5FB0A17C777CF09, 0xF468107100525890,
0xCF79CC9DB955C2CC, 0x7182148D4066EEB4, 0x81AC1FE293D599BF, 0xC6F14CD848405530,
0xA21727DB38CB002F, 0xB8ADA00E5A506A7C, 0xCA9CF1D206FDC03B, 0xA6D90811F0E4851C,
0xFD442E4688BD304A, 0x908F4A166D1DA663, 0x9E4A9CEC15763E2E, 0x9A598E4E043287FE,
0xC5DD44271AD3CDBA, 0x40EFF1E1853F29FD, 0xF7549530E188C128, 0xD12BEE59E68EF47C,
0x9A94DD3E8CF578B9, 0x82BB74F8301958CE, 0xC13A148E3032D6E7, 0xE36A52363C1FAF01,
0xF18899B1BC3F8CA1, 0xDC44E6C3CB279AC1, 0x96F5600F15A7B7E5, 0x29AB103A5EF8C0B9,
0xBCB2B812DB11A5DE, 0x7415D448F6B6F0E7, 0xEBDF661791D60F56, 0x111B495B3464AD21,
0x936B9FCEBB25C995, 0xCAB10DD900BEEC34, 0xB84687C269EF3BFB, 0x3D5D514F40EEA742,
0xE65829B3046B0AFA, 0x0CB4A5A3112A5112, 0x8FF71A0FE2C2E6DC, 0x47F0E785EABA72AB,
0xB3F4E093DB73A093, 0x59ED216765690F56, 0xE0F218B8D25088B8, 0x306869C13EC3532C,
0x8C974F7383725573, 0x1E414218C73A13FB, 0xAFBD2350644EEACF, 0xE5D1929EF90898FA,
0xDBAC6C247D62A583, 0xDF45F746B74ABF39, 0x894BC396CE5DA772, 0x6B8BBA8C328EB783,
0xAB9EB47C81F5114F, 0x066EA92F3F326564, 0xD686619BA27255A2, 0xC80A537B0EFEFEBD,
0x8613FD0145877585, 0xBD06742CE95F5F36, 0xA798FC4196E952E7, 0x2C48113823B73704,
0xD17F3B51FCA3A7A0, 0xF75A15862CA504C5, 0x82EF85133DE648C4, 0x9A984D73DBE722FB,
0xA3AB66580D5FDAF5, 0xC13E60D0D2E0EBBA, 0xCC963FEE10B7D1B3, 0x318DF905079926A8,
0xFFBBCFE994E5C61F, 0xFDF17746497F7052, 0x9FD561F1FD0F9BD3, 0xFEB6EA8BEDEFA633,
0xC7CABA6E7C5382C8, 0xFE64A52EE96B8FC0, 0xF9BD690A1B68637B, 0x3DFDCE7AA3C673B0,
0x9C1661A651213E2D, 0x06BEA10CA65C084E, 0xC31BFA0FE5698DB8, 0x486E494FCFF30A62,
0xF3E2F893DEC3F126, 0x5A89DBA3C3EFCCFA, 0x986DDB5C6B3A76B7, 0xF89629465A75E01C,
0xBE89523386091465, 0xF6BBB397F1135823, 0xEE2BA6C0678B597F, 0x746AA07DED582E2C,
0x94DB483840B717EF, 0xA8C2A44EB4571CDC, 0xBA121A4650E4DDEB, 0x92F34D62616CE413,
0xE896A0D7E51E1566, 0x77B020BAF9C81D17, 0x915E2486EF32CD60, 0x0ACE1474DC1D122E,
0xB5B5ADA8AAFF80B8, 0x0D819992132456BA, 0xE3231912D5BF60E6, 0x10E1FFF697ED6C69,
0x8DF5EFABC5979C8F, 0xCA8D3FFA1EF463C1, 0xB1736B96B6FD83B3, 0xBD308FF8A6B17CB2,
0xDDD0467C64BCE4A0, 0xAC7CB3F6D05DDBDE, 0x8AA22C0DBEF60EE4, 0x6BCDF07A423AA96B,
0xAD4AB7112EB3929D, 0x86C16C98D2C953C6, 0xD89D64D57A607744, 0xE871C7BF077BA8B7,
0x87625F056C7C4A8B, 0x11471CD764AD4972, 0xA93AF6C6C79B5D2D, 0xD598E40D3DD89BCF,
0xD389B47879823479, 0x4AFF1D108D4EC2C3, 0x843610CB4BF160CB, 0xCEDF722A585139BA,
0xA54394FE1EEDB8FE, 0xC2974EB4EE658828, 0xCE947A3DA6A9273E, 0x733D226229FEEA32,
0x811CCC668829B887, 0x0806357D5A3F525F, 0xA163FF802A3426A8, 0xCA07C2DCB0CF26F7,
0xC9BCFF6034C13052, 0xFC89B393DD02F0B5, 0xFC2C3F3841F17C67, 0xBBAC2078D443ACE2,
0x9D9BA7832936EDC0, 0xD54B944B84AA4C0D, 0xC5029163F384A931, 0x0A9E795E65D4DF11,
0xF64335BCF065D37D, 0x4D4617B5FF4A16D5, 0x99EA0196163FA42E, 0x504BCED1BF8E4E45,
0xC06481FB9BCF8D39, 0xE45EC2862F71E1D6, 0xF07DA27A82C37088, 0x5D767327BB4E5A4C,
0x964E858C91BA2655, 0x3A6A07F8D510F86F, 0xBBE226EFB628AFEA, 0x890489F70A55368B,
0xEADAB0ABA3B2DBE5, 0x2B45AC74CCEA842E, 0x92C8AE6B464FC96F, 0x3B0B8BC90012929D,
0xB77ADA0617E3BBCB, 0x09CE6EBB40173744, 0xE55990879DDCAABD, 0xCC420A6A101D0515,
0x8F57FA54C2A9EAB6, 0x9FA946824A12232D, 0xB32DF8E9F3546564, 0x47939822DC96ABF9,
0xDFF9772470297EBD, 0x59787E2B93BC56F7, 0x8BFBEA76C619EF36, 0x57EB4EDB3C55B65A,
0xAEFAE51477A06B03, 0xEDE622920B6B23F1, 0xDAB99E59958885C4, 0xE95FAB368E45ECED,
0x88B402F7FD75539B, 0x11DBCB0218EBB414, 0xAAE103B5FCD2A881, 0xD652BDC29F26A119,
0xD59944A37C0752A2, 0x4BE76D3346F0495F, 0x857FCAE62D8493A5, 0x6F70A4400C562DDB,
0xA6DFBD9FB8E5B88E, 0xCB4CCD500F6BB952, 0xD097AD07A71F26B2, 0x7E2000A41346A7A7,
0x825ECC24C873782F, 0x8ED400668C0C28C8, 0xA2F67F2DFA90563B, 0x728900802F0F32FA,
0xCBB41EF979346BCA, 0x4F2B40A03AD2FFB9, 0xFEA126B7D78186BC, 0xE2F610C84987BFA8,
0x9F24B832E6B0F436, 0x0DD9CA7D2DF4D7C9, 0xC6EDE63FA05D3143, 0x91503D1C79720DBB,
0xF8A95FCF88747D94, 0x75A44C6397CE912A, 0x9B69DBE1B548CE7C, 0xC986AFBE3EE11ABA,
0xC24452DA229B021B, 0xFBE85BADCE996168, 0xF2D56790AB41C2A2, 0xFAE27299423FB9C3,
0x97C560BA6B0919A5, 0xDCCD879FC967D41A, 0xBDB6B8E905CB600F, 0x5400E987BBC1C920,
0xED246723473E3813, 0x290123E9AAB23B68, 0x9436C0760C86E30B, 0xF9A0B6720AAF6521,
0xB94470938FA89BCE, 0xF808E40E8D5B3E69, 0xE7958CB87392C2C2, 0xB60B1D1230B20E04,
0x90BD77F3483BB9B9, 0xB1C6F22B5E6F48C2, 0xB4ECD5F01A4AA828, 0x1E38AEB6360B1AF3,
0xE2280B6C20DD5232, 0x25C6DA63C38DE1B0, 0x8D590723948A535F, 0x579C487E5A38AD0E,
0xB0AF48EC79ACE837, 0x2D835A9DF0C6D851, 0xDCDB1B2798182244, 0xF8E431456CF88E65,
0x8A08F0F8BF0F156B, 0x1B8E9ECB641B58FF, 0xAC8B2D36EED2DAC5, 0xE272467E3D222F3F,
0xD7ADF884AA879177, 0x5B0ED81DCC6ABB0F, 0x86CCBB52EA94BAEA, 0x98E947129FC2B4E9,
0xA87FEA27A539E9A5, 0x3F2398D747B36224, 0xD29FE4B18E88640E, 0x8EEC7F0D19A03AAD,
0x83A3EEEEF9153E89, 0x1953CF68300424AC, 0xA48CEAAAB75A8E2B, 0x5FA8C3423C052DD7,
0xCDB02555653131B6, 0x3792F412CB06794D, 0x808E17555F3EBF11, 0xE2BBD88BBEE40BD0,
0xA0B19D2AB70E6ED6, 0x5B6ACEAEAE9D0EC4, 0xC8DE047564D20A8B, 0xF245825A5A445275,
0xFB158592BE068D2E, 0xEED6E2F0F0D56712, 0x9CED737BB6C4183D, 0x55464DD69685606B,
0xC428D05AA4751E4C, 0xAA97E14C3C26B886, 0xF53304714D9265DF, 0xD53DD99F4B3066A8,
0x993FE2C6D07B7FAB, 0xE546A8038EFE4029, 0xBF8FDB78849A5F96, 0xDE98520472BDD033,
0xEF73D256A5C0F77C, 0x963E66858F6D4440, 0x95A8637627989AAD, 0xDDE7001379A44AA8,
0xBB127C53B17EC159, 0x5560C018580D5D52, 0xE9D71B689DDE71AF, 0xAAB8F01E6E10B4A6,
0x9226712162AB070D, 0xCAB3961304CA70E8, 0xB6B00D69BB55C8D1, 0x3D607B97C5FD0D22,
0xE45C10C42A2B3B05, 0x8CB89A7DB77C506A, 0x8EB98A7A9A5B04E3, 0x77F3608E92ADB242,
0xB267ED1940F1C61C, 0x55F038B237591ED3, 0xDF01E85F912E37A3, 0x6B6C46DEC52F6688,
0x8B61313BBABCE2C6, 0x2323AC4B3B3DA015, 0xAE397D8AA96C1B77, 0xABEC975E0A0D081A,
0xD9C7DCED53C72255, 0x96E7BD358C904A21, 0x881CEA14545C7575, 0x7E50D64177DA2E54,
0xAA242499697392D2, 0xDDE50BD1D5D0B9E9, 0xD4AD2DBFC3D07787, 0x955E4EC64B44E864,
0x84EC3C97DA624AB4, 0xBD5AF13BEF0B113E, 0xA6274BBDD0FADD61, 0xECB1AD8AEACDD58E,
0xCFB11EAD453994BA, 0x67DE18EDA5814AF2, 0x81CEB32C4B43FCF4, 0x80EACF948770CED7,
0xA2425FF75E14FC31, 0xA1258379A94D028D, 0xCAD2F7F5359A3B3E, 0x096EE45813A04330,
0xFD87B5F28300CA0D, 0x8BCA9D6E188853FC, 0x9E74D1B791E07E48, 0x775EA264CF55347D,
0xC612062576589DDA, 0x95364AFE032A819D, 0xF79687AED3EEC551, 0x3A83DDBD83F52204,
0x9ABE14CD44753B52, 0xC4926A9672793542, 0xC16D9A0095928A27, 0x75B7053C0F178293,
0xF1C90080BAF72CB1, 0x5324C68B12DD6338, 0x971DA05074DA7BEE, 0xD3F6FC16EBCA5E03,
0xBCE5086492111AEA, 0x88F4BB1CA6BCF584, 0xEC1E4A7DB69561A5, 0x2B31E9E3D06C32E5,
0x9392EE8E921D5D07, 0x3AFF322E62439FCF, 0xB877AA3236A4B449, 0x09BEFEB9FAD487C2,
0xE69594BEC44DE15B, 0x4C2EBE687989A9B3, 0x901D7CF73AB0ACD9, 0x0F9D37014BF60A10,
0xB424DC35095CD80F, 0x538484C19EF38C94, 0xE12E13424BB40E13, 0x2865A5F206B06FB9,
0x8CBCCC096F5088CB, 0xF93F87B7442E45D3, 0xAFEBFF0BCB24AAFE, 0xF78F69A51539D748,
0xDBE6FECEBDEDD5BE, 0xB573440E5A884D1B, 0x89705F4136B4A597, 0x31680A88F8953030,
0xABCC77118461CEFC, 0xFDC20D2B36BA7C3D, 0xD6BF94D5E57A42BC, 0x3D32907604691B4C,
0x8637BD05AF6C69B5, 0xA63F9A49C2C1B10F, 0xA7C5AC471B478423, 0x0FCF80DC33721D53,
0xD1B71758E219652B, 0xD3C36113404EA4A8, 0x83126E978D4FDF3B, 0x645A1CAC083126E9,
0xA3D70A3D70A3D70A, 0x3D70A3D70A3D70A3, 0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC,
0x8000000000000000, 0x0000000000000000, 0xA000000000000000, 0x0000000000000000,
0xC800000000000000, 0x0000000000000000, 0xFA00000000000000, 0x0000000000000000,
0x9C40000000000000, 0x0000000000000000, 0xC350000000000000, 0x0000000000000000,
0xF424000000000000, 0x0000000000000000, 0x9896800000000000, 0x0000000000000000,
0xBEBC200000000000, 0x0000000000000000, 0xEE6B280000000000, 0x0000000000000000,
0x9502F90000000000, 0x0000000000000000, 0xBA43B74000000000, 0x0000000000000000,
0xE8D4A51000000000, 0x0000000000000000, 0x9184E72A00000000, 0x0000000000000000,
0xB5E620F480000000, 0x0000000000000000, 0xE35FA931A0000000, 0x0000000000000000,
0x8E1BC9BF04000000, 0x0000000000000000, 0xB1A2BC2EC5000000, 0x0000000000000000,
0xDE0B6B3A76400000, 0x0000000000000000, 0x8AC7230489E80000, 0x0000000000000000,
0xAD78EBC5AC620000, 0x0000000000000000, 0xD8D726B7177A8000, 0x0000000000000000,
0x878678326EAC9000, 0x0000000000000000, 0xA968163F0A57B400, 0x0000000000000000,
0xD3C21BCECCEDA100, 0x0000000000000000, 0x84595161401484A0, 0x0000000000000000,
0xA56FA5B99019A5C8, 0x0000000000000000, 0xCECB8F27F4200F3A, 0x0000000000000000,
0x813F3978F8940984, 0x4000000000000000, 0xA18F07D736B90BE5, 0x5000000000000000,
0xC9F2C9CD04674EDE, 0xA400000000000000, 0xFC6F7C4045812296, 0x4D00000000000000,
0x9DC5ADA82B70B59D, 0xF020000000000000, 0xC5371912364CE305, 0x6C28000000000000,
0xF684DF56C3E01BC6, 0xC732000000000000, 0x9A130B963A6C115C, 0x3C7F400000000000,
0xC097CE7BC90715B3, 0x4B9F100000000000, 0xF0BDC21ABB48DB20, 0x1E86D40000000000,
0x96769950B50D88F4, 0x1314448000000000, 0xBC143FA4E250EB31, 0x17D955A000000000,
0xEB194F8E1AE525FD, 0x5DCFAB0800000000, 0x92EFD1B8D0CF37BE, 0x5AA1CAE500000000,
0xB7ABC627050305AD, 0xF14A3D9E40000000, 0xE596B7B0C643C719, 0x6D9CCD05D0000000,
0x8F7E32CE7BEA5C6F, 0xE4820023A2000000, 0xB35DBF821AE4F38B, 0xDDA2802C8A800000,
0xE0352F62A19E306E, 0xD50B2037AD200000, 0x8C213D9DA502DE45, 0x4526F422CC340000,
0xAF298D050E4395D6, 0x9670B12B7F410000, 0xDAF3F04651D47B4C, 0x3C0CDD765F114000,
0x88D8762BF324CD0F, 0xA5880A69FB6AC800, 0xAB0E93B6EFEE0053, 0x8EEA0D047A457A00,
0xD5D238A4ABE98068, 0x72A4904598D6D880, 0x85A36366EB71F041, 0x47A6DA2B7F864750,
0xA70C3C40A64E6C51, 0x999090B65F67D924, 0xD0CF4B50CFE20765, 0xFFF4B4E3F741CF6D,
0x82818F1281ED449F, 0xBFF8F10E7A8921A4, 0xA321F2D7226895C7, 0xAFF72D52192B6A0D,
0xCBEA6F8CEB02BB39, 0x9BF4F8A69F764490, 0xFEE50B7025C36A08, 0x02F236D04753D5B4,
0x9F4F2726179A2245, 0x01D762422C946590, 0xC722F0EF9D80AAD6, 0x424D3AD2B7B97EF5,
0xF8EBAD2B84E0D58B, 0xD2E0898765A7DEB2, 0x9B934C3B330C8577, 0x63CC55F49F88EB2F,
0xC2781F49FFCFA6D5, 0x3CBF6B71C76B25FB, 0xF316271C7FC3908A, 0x8BEF464E3945EF7A,
0x97EDD871CFDA3A56, 0x97758BF0E3CBB5AC, 0xBDE94E8E43D0C8EC, 0x3D52EEED1CBEA317,
0xED63A231D4C4FB27, 0x4CA7AAA863EE4BDD, 0x945E455F24FB1CF8, 0x8FE8CAA93E74EF6A,
0xB975D6B6EE39E436, 0xB3E2FD538E122B44, 0xE7D34C64A9C85D44, 0x60DBBCA87196B616,
0x90E40FBEEA1D3A4A, 0xBC8955E946FE31CD, 0xB51D13AEA4A488DD, 0x6BABAB6398BDBE41,
0xE264589A4DCDAB14, 0xC696963C7EED2DD1, 0x8D7EB76070A08AEC, 0xFC1E1DE5CF543CA2,
0xB0DE65388CC8ADA8, 0x3B25A55F43294BCB, 0xDD15FE86AFFAD912, 0x49EF0EB713F39EBE,
0x8A2DBF142DFCC7AB, 0x6E3569326C784337, 0xACB92ED9397BF996, 0x49C2C37F07965404,
0xD7E77A8F87DAF7FB, 0xDC33745EC97BE906, 0x86F0AC99B4E8DAFD, 0x69A028BB3DED71A3,
0xA8ACD7C0222311BC, 0xC40832EA0D68CE0C, 0xD2D80DB02AABD62B, 0xF50A3FA490C30190,
0x83C7088E1AAB65DB, 0x792667C6DA79E0FA, 0xA4B8CAB1A1563F52, 0x577001B891185938,
0xCDE6FD5E09ABCF26, 0xED4C0226B55E6F86, 0x80B05E5AC60B6178, 0x544F8158315B05B4,
0xA0DC75F1778E39D6, 0x696361AE3DB1C721, 0xC913936DD571C84C, 0x03BC3A19CD1E38E9,
0xFB5878494ACE3A5F, 0x04AB48A04065C723, 0x9D174B2DCEC0E47B, 0x62EB0D64283F9C76,
0xC45D1DF942711D9A, 0x3BA5D0BD324F8394, 0xF5746577930D6500, 0xCA8F44EC7EE36479,
0x9968BF6ABBE85F20, 0x7E998B13CF4E1ECB, 0xBFC2EF456AE276E8, 0x9E3FEDD8C321A67E,
0xEFB3AB16C59B14A2, 0xC5CFE94EF3EA101E, 0x95D04AEE3B80ECE5, 0xBBA1F1D158724A12,
0xBB445DA9CA61281F, 0x2A8A6E45AE8EDC97, 0xEA1575143CF97226, 0xF52D09D71A3293BD,
0x924D692CA61BE758, 0x593C2626705F9C56, 0xB6E0C377CFA2E12E, 0x6F8B2FB00C77836C,
0xE498F455C38B997A, 0x0B6DFB9C0F956447, 0x8EDF98B59A373FEC, 0x4724BD4189BD5EAC,
0xB2977EE300C50FE7, 0x58EDEC91EC2CB657, 0xDF3D5E9BC0F653E1, 0x2F2967B66737E3ED,
0x8B865B215899F46C, 0xBD79E0D20082EE74, 0xAE67F1E9AEC07187, 0xECD8590680A3AA11,
0xDA01EE641A708DE9, 0xE80E6F4820CC9495, 0x884134FE908658B2, 0x3109058D147FDCDD,
0xAA51823E34A7EEDE, 0xBD4B46F0599FD415, 0xD4E5E2CDC1D1EA96, 0x6C9E18AC7007C91A,
0x850FADC09923329E, 0x03E2CF6BC604DDB0, 0xA6539930BF6BFF45, 0x84DB8346B786151C,
0xCFE87F7CEF46FF16, 0xE612641865679A63, 0x81F14FAE158C5F6E, 0x4FCB7E8F3F60C07E,
0xA26DA3999AEF7749, 0xE3BE5E330F38F09D, 0xCB090C8001AB551C, 0x5CADF5BFD3072CC5,
0xFDCB4FA002162A63, 0x73D9732FC7C8F7F6, 0x9E9F11C4014DDA7E, 0x2867E7FDDCDD9AFA,
0xC646D63501A1511D, 0xB281E1FD541501B8, 0xF7D88BC24209A565, 0x1F225A7CA91A4226,
0x9AE757596946075F, 0x3375788DE9B06958, 0xC1A12D2FC3978937, 0x0052D6B1641C83AE,
0xF209787BB47D6B84, 0xC0678C5DBD23A49A, 0x9745EB4D50CE6332, 0xF840B7BA963646E0,
0xBD176620A501FBFF, 0xB650E5A93BC3D898, 0xEC5D3FA8CE427AFF, 0xA3E51F138AB4CEBE,
0x93BA47C980E98CDF, 0xC66F336C36B10137, 0xB8A8D9BBE123F017, 0xB80B0047445D4184,
0xE6D3102AD96CEC1D, 0xA60DC059157491E5, 0x9043EA1AC7E41392, 0x87C89837AD68DB2F,
0xB454E4A179DD1877, 0x29BABE4598C311FB, 0xE16A1DC9D8545E94, 0xF4296DD6FEF3D67A,
0x8CE2529E2734BB1D, 0x1899E4A65F58660C, 0xB01AE745B101E9E4, 0x5EC05DCFF72E7F8F,
0xDC21A1171D42645D, 0x76707543F4FA1F73, 0x899504AE72497EBA, 0x6A06494A791C53A8,
0xABFA45DA0EDBDE69, 0x0487DB9D17636892, 0xD6F8D7509292D603, 0x45A9D2845D3C42B6,
0x865B86925B9BC5C2, 0x0B8A2392BA45A9B2, 0xA7F26836F282B732, 0x8E6CAC7768D7141E,
0xD1EF0244AF2364FF, 0x3207D795430CD926, 0x8335616AED761F1F, 0x7F44E6BD49E807B8,
0xA402B9C5A8D3A6E7, 0x5F16206C9C6209A6, 0xCD036837130890A1, 0x36DBA887C37A8C0F,
0x802221226BE55A64, 0xC2494954DA2C9789, 0xA02AA96B06DEB0FD, 0xF2DB9BAA10B7BD6C,
0xC83553C5C8965D3D, 0x6F92829494E5ACC7, 0xFA42A8B73ABBF48C, 0xCB772339BA1F17F9,
0x9C69A97284B578D7, 0xFF2A760414536EFB, 0xC38413CF25E2D70D, 0xFEF5138519684ABA,
0xF46518C2EF5B8CD1, 0x7EB258665FC25D69, 0x98BF2F79D5993802, 0xEF2F773FFBD97A61,
0xBEEEFB584AFF8603, 0xAAFB550FFACFD8FA, 0xEEAABA2E5DBF6784, 0x95BA2A53F983CF38,
0x952AB45CFA97A0B2, 0xDD945A747BF26183, 0xBA756174393D88DF, 0x94F971119AEEF9E4,
0xE912B9D1478CEB17, 0x7A37CD5601AAB85D, 0x91ABB422CCB812EE, 0xAC62E055C10AB33A,
0xB616A12B7FE617AA, 0x577B986B314D6009, 0xE39C49765FDF9D94, 0xED5A7E85FDA0B80B,
0x8E41ADE9FBEBC27D, 0x14588F13BE847307, 0xB1D219647AE6B31C, 0x596EB2D8AE258FC8,
0xDE469FBD99A05FE3, 0x6FCA5F8ED9AEF3BB, 0x8AEC23D680043BEE, 0x25DE7BB9480D5854,
0xADA72CCC20054AE9, 0xAF561AA79A10AE6A, 0xD910F7FF28069DA4, 0x1B2BA1518094DA04,
0x87AA9AFF79042286, 0x90FB44D2F05D0842, 0xA99541BF57452B28, 0x353A1607AC744A53,
0xD3FA922F2D1675F2, 0x42889B8997915CE8, 0x847C9B5D7C2E09B7, 0x69956135FEBADA11,
0xA59BC234DB398C25, 0x43FAB9837E699095, 0xCF02B2C21207EF2E, 0x94F967E45E03F4BB,
0x8161AFB94B44F57D, 0x1D1BE0EEBAC278F5, 0xA1BA1BA79E1632DC, 0x6462D92A69731732,
0xCA28A291859BBF93, 0x7D7B8F7503CFDCFE, 0xFCB2CB35E702AF78, 0x5CDA735244C3D43E,
0x9DEFBF01B061ADAB, 0x3A0888136AFA64A7, 0xC56BAEC21C7A1916, 0x088AAA1845B8FDD0,
0xF6C69A72A3989F5B, 0x8AAD549E57273D45, 0x9A3C2087A63F6399, 0x36AC54E2F678864B,
0xC0CB28A98FCF3C7F, 0x84576A1BB416A7DD, 0xF0FDF2D3F3C30B9F, 0x656D44A2A11C51D5,
0x969EB7C47859E743, 0x9F644AE5A4B1B325, 0xBC4665B596706114, 0x873D5D9F0DDE1FEE,
0xEB57FF22FC0C7959, 0xA90CB506D155A7EA, 0x9316FF75DD87CBD8, 0x09A7F12442D588F2,
0xB7DCBF5354E9BECE, 0x0C11ED6D538AEB2F, 0xE5D3EF282A242E81, 0x8F1668C8A86DA5FA,
0x8FA475791A569D10, 0xF96E017D694487BC, 0xB38D92D760EC4455, 0x37C981DCC395A9AC,
0xE070F78D3927556A, 0x85BBE253F47B1417, 0x8C469AB843B89562, 0x93956D7478CCEC8E,
0xAF58416654A6BABB, 0x387AC8D1970027B2, 0xDB2E51BFE9D0696A, 0x06997B05FCC0319E,
0x88FCF317F22241E2, 0x441FECE3BDF81F03, 0xAB3C2FDDEEAAD25A, 0xD527E81CAD7626C3,
0xD60B3BD56A5586F1, 0x8A71E223D8D3B074, 0x85C7056562757456, 0xF6872D5667844E49,
0xA738C6BEBB12D16C, 0xB428F8AC016561DB, 0xD106F86E69D785C7, 0xE13336D701BEBA52,
0x82A45B450226B39C, 0xECC0024661173473, 0xA34D721642B06084, 0x27F002D7F95D0190,
0xCC20CE9BD35C78A5, 0x31EC038DF7B441F4, 0xFF290242C83396CE, 0x7E67047175A15271,
0x9F79A169BD203E41, 0x0F0062C6E984D386, 0xC75809C42C684DD1, 0x52C07B78A3E60868,
0xF92E0C3537826145, 0xA7709A56CCDF8A82, 0x9BBCC7A142B17CCB, 0x88A66076400BB691,
0xC2ABF989935DDBFE, 0x6ACFF893D00EA435, 0xF356F7EBF83552FE, 0x0583F6B8C4124D43,
0x98165AF37B2153DE, 0xC3727A337A8B704A, 0xBE1BF1B059E9A8D6, 0x744F18C0592E4C5C,
0xEDA2EE1C7064130C, 0x1162DEF06F79DF73, 0x9485D4D1C63E8BE7, 0x8ADDCB5645AC2BA8,
0xB9A74A0637CE2EE1, 0x6D953E2BD7173692, 0xE8111C87C5C1BA99, 0xC8FA8DB6CCDD0437,
0x910AB1D4DB9914A0, 0x1D9C9892400A22A2, 0xB54D5E4A127F59C8, 0x2503BEB6D00CAB4B,
0xE2A0B5DC971F303A, 0x2E44AE64840FD61D, 0x8DA471A9DE737E24, 0x5CEAECFED289E5D2,
0xB10D8E1456105DAD, 0x7425A83E872C5F47, 0xDD50F1996B947518, 0xD12F124E28F77719,
0x8A5296FFE33CC92F, 0x82BD6B70D99AAA6F, 0xACE73CBFDC0BFB7B, 0x636CC64D1001550B,
0xD8210BEFD30EFA5A, 0x3C47F7E05401AA4E, 0x8714A775E3E95C78, 0x65ACFAEC34810A71,
0xA8D9D1535CE3B396, 0x7F1839A741A14D0D, 0xD31045A8341CA07C, 0x1EDE48111209A050,
0x83EA2B892091E44D, 0x934AED0AAB460432, 0xA4E4B66B68B65D60, 0xF81DA84D5617853F,
0xCE1DE40642E3F4B9, 0x36251260AB9D668E, 0x80D2AE83E9CE78F3, 0xC1D72B7C6B426019,
0xA1075A24E4421730, 0xB24CF65B8612F81F, 0xC94930AE1D529CFC, 0xDEE033F26797B627,
0xFB9B7CD9A4A7443C, 0x169840EF017DA3B1, 0x9D412E0806E88AA5, 0x8E1F289560EE864E,
0xC491798A08A2AD4E, 0xF1A6F2BAB92A27E2, 0xF5B5D7EC8ACB58A2, 0xAE10AF696774B1DB,
0x9991A6F3D6BF1765, 0xACCA6DA1E0A8EF29, 0xBFF610B0CC6EDD3F, 0x17FD090A58D32AF3,
0xEFF394DCFF8A948E, 0xDDFC4B4CEF07F5B0, 0x95F83D0A1FB69CD9, 0x4ABDAF101564F98E,
0xBB764C4CA7A4440F, 0x9D6D1AD41ABE37F1, 0xEA53DF5FD18D5513, 0x84C86189216DC5ED,
0x92746B9BE2F8552C, 0x32FD3CF5B4E49BB4, 0xB7118682DBB66A77, 0x3FBC8C33221DC2A1,
0xE4D5E82392A40515, 0x0FABAF3FEAA5334A, 0x8F05B1163BA6832D, 0x29CB4D87F2A7400E,
0xB2C71D5BCA9023F8, 0x743E20E9EF511012, 0xDF78E4B2BD342CF6, 0x914DA9246B255416,
0x8BAB8EEFB6409C1A, 0x1AD089B6C2F7548E, 0xAE9672ABA3D0C320, 0xA184AC2473B529B1,
0xDA3C0F568CC4F3E8, 0xC9E5D72D90A2741E, 0x8865899617FB1871, 0x7E2FA67C7A658892,
0xAA7EEBFB9DF9DE8D, 0xDDBB901B98FEEAB7, 0xD51EA6FA85785631, 0x552A74227F3EA565,
0x8533285C936B35DE, 0xD53A88958F87275F, 0xA67FF273B8460356, 0x8A892ABAF368F137,
0xD01FEF10A657842C, 0x2D2B7569B0432D85, 0x8213F56A67F6B29B, 0x9C3B29620E29FC73,
0xA298F2C501F45F42, 0x8349F3BA91B47B8F, 0xCB3F2F7642717713, 0x241C70A936219A73,
0xFE0EFB53D30DD4D7, 0xED238CD383AA0110, 0x9EC95D1463E8A506, 0xF4363804324A40AA,
0xC67BB4597CE2CE48, 0xB143C6053EDCD0D5, 0xF81AA16FDC1B81DA, 0xDD94B7868E94050A,
0x9B10A4E5E9913128, 0xCA7CF2B4191C8326, 0xC1D4CE1F63F57D72, 0xFD1C2F611F63A3F0,
0xF24A01A73CF2DCCF, 0xBC633B39673C8CEC, 0x976E41088617CA01, 0xD5BE0503E085D813,
0xBD49D14AA79DBC82, 0x4B2D8644D8A74E18, 0xEC9C459D51852BA2, 0xDDF8E7D60ED1219E,
0x93E1AB8252F33B45, 0xCABB90E5C942B503, 0xB8DA1662E7B00A17, 0x3D6A751F3B936243,
0xE7109BFBA19C0C9D, 0x0CC512670A783AD4, 0x906A617D450187E2, 0x27FB2B80668B24C5,
0xB484F9DC9641E9DA, 0xB1F9F660802DEDF6, 0xE1A63853BBD26451, 0x5E7873F8A0396973,
0x8D07E33455637EB2, 0xDB0B487B6423E1E8, 0xB049DC016ABC5E5F, 0x91CE1A9A3D2CDA62,
0xDC5C5301C56B75F7, 0x7641A140CC7810FB, 0x89B9B3E11B6329BA, 0xA9E904C87FCB0A9D,
0xAC2820D9623BF429, 0x546345FA9FBDCD44, 0xD732290FBACAF133, 0xA97C177947AD4095,
0x867F59A9D4BED6C0, 0x49ED8EABCCCC485D, 0xA81F301449EE8C70, 0x5C68F256BFFF5A74,
0xD226FC195C6A2F8C, 0x73832EEC6FFF3111, 0x83585D8FD9C25DB7, 0xC831FD53C5FF7EAB,
0xA42E74F3D032F525, 0xBA3E7CA8B77F5E55, 0xCD3A1230C43FB26F, 0x28CE1BD2E55F35EB,
0x80444B5E7AA7CF85, 0x7980D163CF5B81B3, 0xA0555E361951C366, 0xD7E105BCC332621F,
0xC86AB5C39FA63440, 0x8DD9472BF3FEFAA7, 0xFA856334878FC150, 0xB14F98F6F0FEB951,
0x9C935E00D4B9D8D2, 0x6ED1BF9A569F33D3, 0xC3B8358109E84F07, 0x0A862F80EC4700C8,
0xF4A642E14C6262C8, 0xCD27BB612758C0FA, 0x98E7E9CCCFBD7DBD, 0x8038D51CB897789C,
0xBF21E44003ACDD2C, 0xE0470A63E6BD56C3, 0xEEEA5D5004981478, 0x1858CCFCE06CAC74,
0x95527A5202DF0CCB, 0x0F37801E0C43EBC8, 0xBAA718E68396CFFD, 0xD30560258F54E6BA,
0xE950DF20247C83FD, 0x47C6B82EF32A2069, 0x91D28B7416CDD27E, 0x4CDC331D57FA5441,
0xB6472E511C81471D, 0xE0133FE4ADF8E952, 0xE3D8F9E563A198E5, 0x58180FDDD97723A6,
0x8E679C2F5E44FF8F, 0x570F09EAA7EA7648, 0xB201833B35D63F73, 0x2CD2CC6551E513DA,
0xDE81E40A034BCF4F, 0xF8077F7EA65E58D1, 0x8B112E86420F6191, 0xFB04AFAF27FAF782,
0xADD57A27D29339F6, 0x79C5DB9AF1F9B563, 0xD94AD8B1C7380874, 0x18375281AE7822BC,
0x87CEC76F1C830548, 0x8F2293910D0B15B5, 0xA9C2794AE3A3C69A, 0xB2EB3875504DDB22,
0xD433179D9C8CB841, 0x5FA60692A46151EB, 0x849FEEC281D7F328, 0xDBC7C41BA6BCD333,
0xA5C7EA73224DEFF3, 0x12B9B522906C0800, 0xCF39E50FEAE16BEF, 0xD768226B34870A00,
0x81842F29F2CCE375, 0xE6A1158300D46640, 0xA1E53AF46F801C53, 0x60495AE3C1097FD0,
0xCA5E89B18B602368, 0x385BB19CB14BDFC4, 0xFCF62C1DEE382C42, 0x46729E03DD9ED7B5,
0x9E19DB92B4E31BA9, 0x6C07A2C26A8346D1, 0xC5A05277621BE293, 0xC7098B7305241885,
0xF70867153AA2DB38, 0xB8CBEE4FC66D1EA7, 0x9A65406D44A5C903, 0x737F74F1DC043328,
0xC0FE908895CF3B44, 0x505F522E53053FF2, 0xF13E34AABB430A15, 0x647726B9E7C68FEF,
0x96C6E0EAB509E64D, 0x5ECA783430DC19F5, 0xBC789925624C5FE0, 0xB67D16413D132072,
0xEB96BF6EBADF77D8, 0xE41C5BD18C57E88F, 0x933E37A534CBAAE7, 0x8E91B962F7B6F159,
0xB80DC58E81FE95A1, 0x723627BBB5A4ADB0, 0xE61136F2227E3B09, 0xCEC3B1AAA30DD91C,
0x8FCAC257558EE4E6, 0x213A4F0AA5E8A7B1, 0xB3BD72ED2AF29E1F, 0xA988E2CD4F62D19D,
0xE0ACCFA875AF45A7, 0x93EB1B80A33B8605, 0x8C6C01C9498D8B88, 0xBC72F130660533C3,
0xAF87023B9BF0EE6A, 0xEB8FAD7C7F8680B4, 0xDB68C2CA82ED2A05, 0xA67398DB9F6820E1,
0x892179BE91D43A43, 0x88083F8943A1148C, 0xAB69D82E364948D4, 0x6A0A4F6B948959B0,
0xD6444E39C3DB9B09, 0x848CE34679ABB01C, 0x85EAB0E41A6940E5, 0xF2D80E0C0C0B4E11,
0xA7655D1D2103911F, 0x6F8E118F0F0E2195, 0xD13EB46469447567, 0x4B7195F2D2D1A9FB,
]
"""
## The text of `src/std/strings/floats.lucb`.
let text_29: str = r"""#==============================================================================================
#
# floats - Floating-point text: correctly rounded parsing, shortest formatting
#
# DESCRIPTION:
# Decimal text to f64 and f32, correctly rounded to nearest with ties to even, and
# back as the shortest digits that read back to the same value. Parsing takes a
# mantissa and a power of ten that are both exact (up to 2^53 and 10^22) through one
# float multiplication or division (Clinger's fast path, correctly rounded in the
# default rounding mode Luce never changes), and the rest through Eisel-Lemire
# (Lemire, "Number Parsing at a Gigabyte per Second", 2021, in the form of Go's
# strconv) over float_tables' 128-bit powers of ten; what that cannot decide (a tie,
# a subnormal, digits past the 19th that matter) goes to a decimal of up to 800 digits
# scaled by powers of two (strconv's slow path), in integer arithmetic. Formatting is
# float_text's shortest digits (Ryu), in integers. No C library and no locale.
#
#==============================================================================================
# mark: Parsing ================================================================================
## Parse `text`, all of it, as the f64 nearest its value, ties to even: an optional sign, then
## digits with an optional point (`1`, `1.5`, `.5`, `5.`) and an optional exponent (`e` or
## `E`, an optional sign, digits), or `inf`, `infinity` or `nan` in any case. NaN is the
## quiet NaN with the written sign. Fails with invalid_number when `text` is not a number or
## goes on past one (scan_f64 says where it ends), and with number_out_of_range when it
## rounds past the largest finite f64; a smaller magnitude than the least subnormal is a
## signed zero. No locale, whitespace, underscore or hex form is read.
pub func parse_f64(text: str) -> f64!:
let (value, used) = try scan_f64(text.bytes)
if used != text.length:
error(invalid_number, "the text goes on past the number")
return value
## The f32 counterpart of parse_f64, rounded once from the decimal (never through f64).
pub func parse_f32(text: str) -> f32!:
let (value, used) = try scan_f32(text.bytes)
if used != text.length:
error(invalid_number, "the text goes on past the number")
return value
## The number that starts `text`, read as parse_f64 reads one, and how many bytes spell it:
## the longest prefix that is a number (`1e` is the number `1`, one byte). A reader continues
## from there, and names that position when what follows is not what it expects. Fails with
## invalid_number when no number starts `text`, number_out_of_range as parse_f64 does.
pub func scan_f64(text: const u8[]) -> (f64, usize)!:
let number = scan(text)
if number.kind == nothing_kind:
error(invalid_number, "no number starts the text")
let sign: u64 = 0x8000000000000000 if number.negative else 0
if number.kind == infinity_kind:
return (f64.bits(sign | 0x7FF0000000000000), number.used)
if number.kind == nan_kind:
return (f64.bits(sign | 0x7FF8000000000000), number.used)
if number.mantissa == 0:
return (f64.bits(sign), number.used)
if not number.truncated and number.mantissa <= 0x20000000000000 and number.exponent >= -22 and number.exponent <= 22:
# Clinger's fast path: the mantissa and the power of ten are both exact, so one
# correctly rounded multiplication or division is the nearest f64
var exact = (f64)number.mantissa
if number.exponent > 0:
exact = exact * exact_powers[(usize)number.exponent]
elif number.exponent < 0:
exact = exact / exact_powers[(usize)(-number.exponent)]
return (-exact if number.negative else exact, number.used)
let magnitude = number.exponent + number.digits - 1
if magnitude > 309:
error(number_out_of_range, "the number is beyond the largest f64")
if magnitude < -330:
return (f64.bits(sign), number.used)
if let value = lemire64(number.mantissa, number.exponent, number.negative):
# digits past the 19th: the value lies between mantissa and mantissa + 1 units
if not number.truncated:
return (value, number.used)
if let above = lemire64(number.mantissa + 1, number.exponent, number.negative):
if above.bits() == value.bits():
return (value, number.used)
var slow = Decimal()
slow.read(text[..<number.used])
let (bits, overflow) = slow.float_bits(52, 11, -1023)
if overflow:
error(number_out_of_range, "the number is beyond the largest f64")
return (f64.bits(bits | sign), number.used)
## The f32 counterpart of scan_f64.
pub func scan_f32(text: const u8[]) -> (f32, usize)!:
let number = scan(text)
if number.kind == nothing_kind:
error(invalid_number, "no number starts the text")
let sign: u32 = 0x80000000 if number.negative else 0
if number.kind == infinity_kind:
return (f32.bits(sign | 0x7F800000), number.used)
if number.kind == nan_kind:
return (f32.bits(sign | 0x7FC00000), number.used)
if number.mantissa == 0:
return (f32.bits(sign), number.used)
if not number.truncated and number.exponent == 0 and number.mantissa <= 0x1000000:
let exact = (f32)number.mantissa
return (-exact if number.negative else exact, number.used)
let magnitude = number.exponent + number.digits - 1
if magnitude > 39:
error(number_out_of_range, "the number is beyond the largest f32")
if magnitude < -66:
return (f32.bits(sign), number.used)
if let value = lemire32(number.mantissa, number.exponent, number.negative):
if not number.truncated:
return (value, number.used)
if let above = lemire32(number.mantissa + 1, number.exponent, number.negative):
if above.bits() == value.bits():
return (value, number.used)
var slow = Decimal()
slow.read(text[..<number.used])
let (bits, overflow) = slow.float_bits(23, 8, -127)
if overflow:
error(number_out_of_range, "the number is beyond the largest f32")
return (f32.bits((u32)bits | sign), number.used)
## Every power of ten an f64 holds exactly, 10^0 to 10^22.
let exact_powers: f64[23] = [1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0,
100000000.0, 1000000000.0, 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
100000000000000.0, 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0,
1000000000000000000000.0, 10000000000000000000000.0]
let finite_kind: u8 = 0
let infinity_kind: u8 = 1
let nan_kind: u8 = 2
let nothing_kind: u8 = 3
## A number as scanned: `mantissa * 10^exponent` from its first 19 significant digits.
struct Scanned:
var negative: bool = false
var kind: u8 = 0
var mantissa: u64 = 0
var digits: i64 = 0
var exponent: i64 = 0
## Whether a nonzero digit past the 19th was left out of `mantissa`.
var truncated: bool = false
var used: usize = 0
## The number at the start of `text`: its sign, kind, leading digits and length. The digits
## before the point and after it are read by a loop each, as the digits come: a number of
## a usda array or a JSON document costs a compare and a multiply-add a digit.
inline func scan(text: const u8[]) -> Scanned:
var number = Scanned()
let end = text.length
var at: usize = 0
if at < end and (text[at] == '-' or text[at] == '+'):
number.negative = text[at] == '-'
at += 1
if at < end and text[at] > '9':
# a letter: only the special words start with one
if let length = special_word(text[at..]):
number.kind = infinity_kind if length != 3 or ascii_lower(text[at]) == 'i' else nan_kind
number.used = at + length
return number
number.kind = nothing_kind
return number
let whole = at
# leading zeros say nothing
while at < end and text[at] == '0':
at += 1
# counts, not checked: none can pass the text's length
var mantissa: u64 = 0
var digits: usize = 0
var dropped: usize = 0
var scaled: usize = 0
var truncated = false
while at < end:
let digit = text[at] -% '0'
if digit > 9:
break
if digits < 19:
# nineteen digits stay below 2^64
mantissa = mantissa *% 10 +% (u64)digit
digits = digits +% 1
else:
if digit != 0:
truncated = true
dropped = dropped +% 1
at = at +% 1
var seen = at > whole
if at < end and text[at] == '.':
at += 1
let fraction = at
if digits == 0:
# zeros after the point and before the first digit scale what follows
while at < end and text[at] == '0':
at = at +% 1
scaled = at -% fraction
while at < end:
let digit = text[at] -% '0'
if digit > 9:
break
if digits < 19:
mantissa = mantissa *% 10 +% (u64)digit
digits = digits +% 1
scaled = scaled +% 1
elif digit != 0:
truncated = true
at = at +% 1
seen = seen or at > fraction
if not seen:
number.kind = nothing_kind
return number
let exponent = (i64)dropped - (i64)scaled
number.mantissa = mantissa
number.digits = (i64)digits
number.exponent = exponent
number.truncated = truncated
number.used = at
if at < end and (text[at] == 'e' or text[at] == 'E'):
var after = at + 1
var down = false
if after < end and (text[after] == '-' or text[after] == '+'):
down = text[after] == '-'
after += 1
var power: i64 = 0
let first = after
while after < end and text[after] >= '0' and text[after] <= '9':
# beyond any f64 either way; the cap keeps the sum from wrapping
if power < 100000:
power = power * 10 + (i64)(text[after] - '0')
after += 1
if after > first:
number.exponent += -power if down else power
number.used = after
return number
## The length of `inf`, `infinity` or `nan` (any case) at the start of `word`, the longest.
noinline func special_word(word: const u8[]) -> usize?:
if starts_word(word, "infinity"):
return 8
if starts_word(word, "inf") or starts_word(word, "nan"):
return 3
return none
func starts_word(text: const u8[], word: str) -> bool:
if text.length < word.length:
return false
var at: usize = 0
while at < word.length:
if ascii_lower(text[at]) != word.bytes[at]:
return false
at += 1
return true
func ascii_lower(byte: u8) -> u8:
return byte + 32 if byte >= 'A' and byte <= 'Z' else byte
# mark: Eisel-Lemire ===========================================================================
## The decimal exponents lemire_powers covers.
let smallest_power: i64 = -348
let largest_power: i64 = 347
## `mantissa * 10^exponent` as the nearest f64, or none when the 128-bit product cannot
## decide it (a tie, a subnormal, past the range): the slow path decides then.
func lemire64(mantissa: u64, exponent: i64, negative: bool) -> f64?:
if exponent < smallest_power or exponent > largest_power:
return none
let zeros = leading_zeros(mantissa)
let man = mantissa << zeros
var binary = (u64)(((217706 * exponent) >> 16) + 64 + 1023 - (i64)zeros)
let row = (usize)(exponent - smallest_power) * 2
var low: u64 = 0
var high = float_text.product(man, lemire_powers[row], &low)
# the top 55 bits may be off by the truncated low word: widen when unsure
if high & 0x1FF == 0x1FF and low +% man < man:
var extra_low: u64 = 0
let extra_high = float_text.product(man, lemire_powers[row + 1], &extra_low)
var merged_high = high
let merged_low = low +% extra_high
if merged_low < low:
merged_high = merged_high +% 1
if merged_high & 0x1FF == 0x1FF and merged_low +% 1 == 0 and extra_low +% man < man:
return none
high = merged_high
low = merged_low
let top = high >> 63
var result = high >> (top + 9)
binary = binary -% (1 ^ top)
# an exact halfway case needs the whole decimal to break the tie
if low == 0 and high & 0x1FF == 0 and result & 3 == 1:
return none
result = (result + (result & 1)) >> 1
if (result >> 53) > 0:
result >>= 1
binary = binary +% 1
# zero or wrapped: subnormal; 0x7FF or more: infinite
if binary -% 1 >= 0x7FF - 1:
return none
var bits = (binary << 52) | (result & 0xFFFFFFFFFFFFF)
if negative:
bits |= 0x8000000000000000
return f64.bits(bits)
## lemire64 for f32: 24 bits kept of the product's top 64.
func lemire32(mantissa: u64, exponent: i64, negative: bool) -> f32?:
if exponent < smallest_power or exponent > largest_power:
return none
let zeros = leading_zeros(mantissa)
let man = mantissa << zeros
var binary = (u64)(((217706 * exponent) >> 16) + 64 + 127 - (i64)zeros)
let row = (usize)(exponent - smallest_power) * 2
var low: u64 = 0
var high = float_text.product(man, lemire_powers[row], &low)
if high & 0x3FFFFFFFFF == 0x3FFFFFFFFF and low +% man < man:
var extra_low: u64 = 0
let extra_high = float_text.product(man, lemire_powers[row + 1], &extra_low)
var merged_high = high
let merged_low = low +% extra_high
if merged_low < low:
merged_high = merged_high +% 1
if merged_high & 0x3FFFFFFFFF == 0x3FFFFFFFFF and merged_low +% 1 == 0 and extra_low +% man < man:
return none
high = merged_high
low = merged_low
let top = high >> 63
var result = high >> (top + 38)
binary = binary -% (1 ^ top)
if low == 0 and high & 0x3FFFFFFFFF == 0 and result & 3 == 1:
return none
result = (result + (result & 1)) >> 1
if (result >> 24) > 0:
result >>= 1
binary = binary +% 1
if binary -% 1 >= 0xFF - 1:
return none
var bits = (u32)((binary << 23) | (result & 0x7FFFFF))
if negative:
bits |= 0x80000000
return f32.bits(bits)
## Leading zero bits of a nonzero value.
inline func leading_zeros(value: u64) -> u64:
var v = value
var count: u64 = 0
if v & 0xFFFFFFFF00000000 == 0:
count += 32
v <<= 32
if v & 0xFFFF000000000000 == 0:
count += 16
v <<= 16
if v & 0xFF00000000000000 == 0:
count += 8
v <<= 8
if v & 0xF000000000000000 == 0:
count += 4
v <<= 4
if v & 0xC000000000000000 == 0:
count += 2
v <<= 2
if v & 0x8000000000000000 == 0:
count += 1
return count
# mark: The slow path ==========================================================================
## How many digits a Decimal keeps; a nonzero digit past them only marks it truncated.
let decimal_capacity: usize = 800
## The most a Decimal shifts by in one pass: nine times 2^60 and a carry fit a u64.
let largest_shift: u32 = 60
## For a decimal point at d, 10^d < 2^powers_of_two[d]: how far a shift brings it down.
let powers_of_two: i64[9] = [1, 3, 6, 9, 13, 16, 19, 23, 26]
## A decimal `0.d[0]d[1]... * 10^point`, digits 0 through 9, no trailing zeros: exact for
## any text up to 800 significant digits, and scaled by powers of two to find the binary
## value when Eisel-Lemire cannot (strconv's slow path).
struct Decimal:
var digits: u8[800]
var count: usize = 0
var point: i64 = 0
var truncated: bool = false
## The finite number `text` spells, which scan has read already.
func read(text: const u8[]):
var at: usize = 0
if at < text.length and (text[at] == '-' or text[at] == '+'):
at += 1
var after_point = false
while at < text.length:
let byte = text[at]
if byte == '.':
after_point = true
elif byte >= '0' and byte <= '9':
let digit = byte - '0'
if self.count == 0 and digit == 0:
if after_point:
self.point -= 1
else:
if not after_point:
self.point += 1
if self.count < decimal_capacity:
self.digits[self.count] = digit
self.count += 1
elif digit != 0:
self.truncated = true
else:
break
at += 1
if at < text.length and (text[at] == 'e' or text[at] == 'E'):
at += 1
var down = false
if at < text.length and (text[at] == '-' or text[at] == '+'):
down = text[at] == '-'
at += 1
var power: i64 = 0
while at < text.length and text[at] >= '0' and text[at] <= '9':
if power < 100000:
power = power * 10 + (i64)(text[at] - '0')
at += 1
self.point += -power if down else power
self.trim()
## The value's bits in a format of `mantissa_bits` stored mantissa bits, `exponent_bits`
## exponent bits and exponent bias `-bias`, rounded to nearest, ties to even; and
## whether it overflowed to infinity (then the bits are infinity's).
func float_bits(mantissa_bits: u32, exponent_bits: u32, bias: i64) -> (u64, bool):
let largest = ((i64)1 << (i64)exponent_bits) - 1
var exponent: i64 = 0
var mantissa: u64 = 0
var overflow = false
if self.count == 0 or self.point < -330:
exponent = bias
elif self.point > 310:
overflow = true
else:
# into [0.5, 1) by powers of two
while self.point > 0:
let n = 27 if self.point >= 9 else powers_of_two[(usize)self.point]
self.shift(-n)
exponent += n
while self.point < 0 or (self.point == 0 and self.digits[0] < 5):
let n = 27 if -self.point >= 9 else powers_of_two[(usize)(-self.point)]
self.shift(n)
exponent -= n
# the format's range is [1, 2)
exponent -= 1
# below the least normal exponent: a subnormal, with fewer bits
if exponent < bias + 1:
let n = bias + 1 - exponent
self.shift(-n)
exponent += n
if exponent - bias >= largest:
overflow = true
else:
self.shift((i64)mantissa_bits + 1)
mantissa = self.rounded_integer()
# rounding carried into a new bit
if mantissa == (u64)2 << (u64)mantissa_bits:
mantissa >>= 1
exponent += 1
if exponent - bias >= largest:
overflow = true
if mantissa & ((u64)1 << (u64)mantissa_bits) == 0:
exponent = bias
if overflow:
mantissa = 0
exponent = largest + bias
var bits = mantissa & (((u64)1 << (u64)mantissa_bits) - 1)
bits |= (u64)((exponent - bias) & largest) << (u64)mantissa_bits
return (bits, overflow)
## Multiply by 2^k, or divide by 2^-k when k is negative.
func shift(k: i64):
if self.count == 0:
return
var left = k
while left > (i64)largest_shift:
self.shift_left(largest_shift)
left -= (i64)largest_shift
while left < -(i64)largest_shift:
self.shift_right(largest_shift)
left += (i64)largest_shift
if left > 0:
self.shift_left((u32)left)
elif left < 0:
self.shift_right((u32)(-left))
## Multiply by 2^k, k at most largest_shift: the digits times 2^k written right to left.
func shift_left(k: u32):
var scratch: u8[820] = ---
let end = self.count + 20
var write = end
var carry: u64 = 0
var read = self.count
while read > 0:
read -= 1
carry += (u64)self.digits[read] << (u64)k
let quotient = carry // 10
write -= 1
scratch[write] = (u8)(carry - quotient * 10)
carry = quotient
while carry > 0:
let quotient = carry // 10
write -= 1
scratch[write] = (u8)(carry - quotient * 10)
carry = quotient
let produced = end - write
let kept = produced if produced < decimal_capacity else decimal_capacity
var at: usize = 0
while at < produced:
if at < kept:
self.digits[at] = scratch[write + at]
elif scratch[write + at] != 0:
self.truncated = true
at += 1
self.point += (i64)produced - (i64)self.count
self.count = kept
self.trim()
## Divide by 2^k, k at most largest_shift.
func shift_right(k: u32):
var read: usize = 0
var write: usize = 0
var n: u64 = 0
# enough leading digits to cover the first quotient digit
while (n >> (u64)k) == 0:
if read >= self.count:
if n == 0:
self.count = 0
return
while (n >> (u64)k) == 0:
n = n * 10
read += 1
break
n = n * 10 + (u64)self.digits[read]
read += 1
self.point -= (i64)read - 1
let mask = ((u64)1 << (u64)k) - 1
while read < self.count:
let next = self.digits[read]
self.digits[write] = (u8)(n >> (u64)k)
write += 1
n = (n & mask) * 10 + (u64)next
read += 1
while n > 0:
let digit = n >> (u64)k
n &= mask
if write < decimal_capacity:
self.digits[write] = (u8)digit
write += 1
elif digit > 0:
self.truncated = true
n = n * 10
self.count = write
self.trim()
## The integer part, rounded to nearest, ties to even (a truncated tail breaks a tie up).
func rounded_integer() -> u64:
if self.point > 20:
return 0xFFFFFFFFFFFFFFFF
var n: u64 = 0
var at: i64 = 0
while at < self.point and at < (i64)self.count:
n = n * 10 + (u64)self.digits[(usize)at]
at += 1
while at < self.point:
n *= 10
at += 1
if self.rounds_up(self.point):
n += 1
return n
func rounds_up(at: i64) -> bool:
if at < 0 or at >= (i64)self.count:
return false
let digit = self.digits[(usize)at]
if digit == 5 and at + 1 == (i64)self.count:
# exactly half, unless digits were dropped past the capacity
if self.truncated:
return true
return at > 0 and self.digits[(usize)(at - 1)] % 2 == 1
return digit >= 5
func trim():
while self.count > 0 and self.digits[self.count - 1] == 0:
self.count -= 1
if self.count == 0:
self.point = 0
# mark: Formatting =============================================================================
## The shortest decimal text that parse_f64 reads back as `value`, the closest to it of
## those, as double-conversion's ToShortest lays it out: decimal notation for a decimal
## exponent from -6 to 14 (`0.000001`, `123456789012345`, `1.5`), otherwise one digit, the
## rest after a point, and `e` with the exponent (`1e-7`, `1.2345e15`, `5e-324`); `-0`,
## `inf`, `-inf`, `nan` (no sign, no payload). Written into `buffer` (32 bytes always
## suffice), and a view of it; io.full when it does not fit, the buffer unchanged. Neither
## allocates, locale or rounding mode (float_text).
pub func format_f64(value: f64, buffer: u8[]) -> str!:
var text: u8[32] = ---
let length = float_text.write_f64(value, text[0..])
return try deliver(text[..<length], buffer)
## The f32 counterpart of format_f64: the f32's own shortest digits (`0.1` for 0.1f32, not
## the digits of its f64 value).
pub func format_f32(value: f32, buffer: u8[]) -> str!:
var text: u8[32] = ---
let length = float_text.write_f32(value, text[0..])
return try deliver(text[..<length], buffer)
func deliver(text: const u8[], buffer: u8[]) -> str!:
if buffer.length < text.length:
error(io.full, "the destination cannot hold the formatted floating-point value")
memory.copy(buffer, text, text.length)
return (str)buffer[..<text.length]
"""
## The text of `src/std/strings/formatting.lucb`.
let text_30: str = r"""#==============================================================================================
#
# strings/formatting - Formatted text into a caller's bytes, and fields by specification
#
# DESCRIPTION:
# `format(buffer, f"...")` (§5.5), which the compiler lays out straight into the buffer
# where the call is written, and the writers a formatted string's `{value:spec}` field
# becomes: `numerals` lays the value out and the text goes to a `Writer`.
#
#==============================================================================================
# mark: Formatting =============================================================================
## `text` written into `buffer`: the `str` over the bytes written (§5.5). Fails with
## `memory.exhausted` when the text does not fit, the bytes that fit written. The compiler
## writes the text where the call is, so this body never runs.
pub func format(buffer: u8[], text: fmt) -> str!:
trap("strings.format is answered where it is called")
# mark: Fields by specification ================================================================
## A `Writer` as `numerals` writes into it, with the first failure it gave: its code and
## its message, copied, since the handler that caught it gives the original back (§11.3).
struct Through:
var writer: io.Writer
var failed: ErrorCode? = none
var message: u8[256]
var length: usize = 0
## Append `bytes` to the `Through` at `context`; false once the writer has failed.
func write_through(context: void*, bytes: const u8[]) -> bool:
let through = (Through*)context
let written = through.writer.write(bytes) catch failure:
through.failed = failure.code
through.length = copy_into(through.message, failure.message).length
return false
return written == bytes.length
## The writer's failure after a layout that stopped, or a short write's.
func stopped(through: const Through*) -> !:
if let code = through.failed:
error(code, (str)through.message[..<through.length])
error(memory.exhausted, "the writer took fewer bytes than it was given")
## `value` laid out by format specification `spec` (§5.5), written to `sink`: what a
## formatted string's `{value:spec}` field writes. A `spec` that does not read traps.
pub func write_i64(sink: io.Writer, value: i64, spec: str) -> !:
var through = Through(writer = sink)
let negative = value < 0
let magnitude = 0 -% (u64)value if negative else (u64)value
if not numerals.lay_integer(numerals.Sink(context = (void*)&through, write = write_through), negative, magnitude, spec):
try stopped(&through)
## `value` laid out by format specification `spec` (§5.5), written to `sink`.
pub func write_u64(sink: io.Writer, value: u64, spec: str) -> !:
var through = Through(writer = sink)
if not numerals.lay_integer(numerals.Sink(context = (void*)&through, write = write_through), false, value, spec):
try stopped(&through)
## `value` laid out by format specification `spec` (§5.5), written to `sink`.
pub func write_f64(sink: io.Writer, value: f64, spec: str) -> !:
var through = Through(writer = sink)
if not numerals.lay_float(numerals.Sink(context = (void*)&through, write = write_through), value, false, spec):
try stopped(&through)
## `value` laid out by format specification `spec` (§5.5), written to `sink`: with neither
## a type nor a precision, the `f32`'s own shortest digits.
pub func write_f32(sink: io.Writer, value: f32, spec: str) -> !:
var through = Through(writer = sink)
if not numerals.lay_float(numerals.Sink(context = (void*)&through, write = write_through), f64(value), true, spec):
try stopped(&through)
## `text` laid out by format specification `spec` (§5.5), written to `sink`.
pub func write_text(sink: io.Writer, text: str, spec: str) -> !:
var through = Through(writer = sink)
if not numerals.lay_text(numerals.Sink(context = (void*)&through, write = write_through), text, spec):
try stopped(&through)
## `value`, its UTF-8, laid out by format specification `spec` (§5.5), written to `sink`.
pub func write_char(sink: io.Writer, value: char, spec: str) -> !:
var bytes: u8[4]
let cp = u32(value)
var n: usize = 0
if cp < 0x80:
bytes[0] = u8(cp)
n = 1
elif cp < 0x800:
bytes[0] = u8(0xC0 | (cp >> 6))
bytes[1] = u8(0x80 | (cp & 0x3F))
n = 2
elif cp < 0x10000:
bytes[0] = u8(0xE0 | (cp >> 12))
bytes[1] = u8(0x80 | ((cp >> 6) & 0x3F))
bytes[2] = u8(0x80 | (cp & 0x3F))
n = 3
else:
bytes[0] = u8(0xF0 | (cp >> 18))
bytes[1] = u8(0x80 | ((cp >> 12) & 0x3F))
bytes[2] = u8(0x80 | ((cp >> 6) & 0x3F))
bytes[3] = u8(0x80 | (cp & 0x3F))
n = 4
try write_text(sink, (str)bytes[..<n], spec)
## `true` or `false` laid out by format specification `spec` (§5.5), written to `sink`.
pub func write_bool(sink: io.Writer, value: bool, spec: str) -> !:
try write_text(sink, "true" if value else "false", spec)
"""
## The text of `src/std/testing.lucb`.
let text_31: str = r"""#==============================================================================================
#
# testing - What a Base test runs under
#
# DESCRIPTION:
# A fixed-buffer allocator made current for each test, a deterministic seed, and
# assertions that name what differed (§16.5).
#
#==============================================================================================
import luce
import memory
from io import Location
from memory import Allocator, FixedBuffer
var arena_bytes: u8[1048576]
var arena: FixedBuffer
var saved: Allocator
var current_seed: u64 = 1
## Called by the runner before each test.
pub func begin(index: u64):
arena = FixedBuffer.over(arena_bytes)
saved = memory.allocator
memory.allocator = &arena
current_seed = index * 2654435761 + 1
## Called by the runner after each test.
pub func end():
memory.allocator = saved
## A seed that is the same on every run of the same test.
pub func seed() -> u64:
return current_seed
## The next pseudo-random value from the seed, and the seed advanced.
pub func random() -> u64:
var x = current_seed
x = x ^ (x << 13)
x = x ^ (x >> 7)
x = x ^ (x << 17)
current_seed = x
return x
## Trap with `message` when `condition` is false, naming the line of the test that asked
## (`at`, filled in where it is called).
pub func expect(condition: bool, message: str, at: Location = luce.location):
if not condition:
trap(f"{at.file}:{at.line}: {message}")
## Trap, naming both values and the line of the test, when `actual` and `expected` differ.
pub func expect_equal(actual: i64, expected: i64, message: str, at: Location = luce.location):
if actual != expected:
trap(f"{at.file}:{at.line}: {message}: expected {expected}, got {actual}")
## Trap, naming both texts and the line of the test, when they differ.
pub func expect_text(actual: str, expected: str, message: str, at: Location = luce.location):
if actual != expected:
trap(f"{at.file}:{at.line}: {message}: expected `{expected}`, got `{actual}`")
"""
## The text of `src/std/interop/module.lucb`.
let text_32: str = r"""#==============================================================================================
#
# interop - Explicit native ownership shared with managed consumers
#
# DESCRIPTION:
# Explicit native ownership shared with managed consumers. Package types remain
# ordinary structs with init and methods; a Type declaration supplies their
# disposal, trace and affinity contract. Reference values are manual in Base.
#
#==============================================================================================
import memory
import ownership
import thread
import sync
import time
## The carrier or its native value is not valid.
pub let invalid: ErrorCode = ErrorCode.package(101)
## The value was touched from a thread other than its owner.
pub let wrong_thread: ErrorCode = ErrorCode.package(102)
## The lease or scope backing the value has ended.
pub let expired: ErrorCode = ErrorCode.package(103)
## A package's constant declaration of native ownership. Dispose releases native
## resources and strong edges exactly once, including after an explicit close.
## A closeable export binds its public disposal method as the terminal operation.
pub struct Type[T]:
## The type's name.
pub let name: str
## Releases the value's native resources and strong edges, exactly once.
pub let dispose: func(T*) -> unit
## Visits the strong references the value holds, when it holds any.
pub let trace: (func(const T*, func(ownership.Object*, void*) -> unit, void*) -> unit)? = none
## Whether the export binds a public disposal method as its terminal operation.
pub let closeable: bool = false
## Whether the value must live on the process's main thread.
pub let main_thread: bool = false
"""
## The text of `src/std/interop/object.lucb`.
let text_33: str = r"""#==============================================================================================
#
# object - The stable owner shell
#
# DESCRIPTION:
# Shared by every native reference and managed alias; its header participates directly in
# the collector, with no second reference count.
#
#==============================================================================================
## Stable shell shared by every native reference and managed alias. Its header
## participates directly in the shared collector; no second reference count exists.
pub struct Owner[T]:
## The shared collector header.
pub var header: ownership.Object
var information: ownership.Info
var declaration: Type[T]
var native: T*?
var native_allocator: memory.Allocator?
var closed: bool
var disposed: bool
var active: usize
## Whether the owner has not been closed.
pub static func is_open(object: ownership.Object*) -> bool:
ownership.check_alive(object)
return not ((Owner[T]*)object).closed
## Begin a native call guard, retaining the object.
pub static func enter(object: ownership.Object*):
_ = ownership.retain(object)
((Owner[T]*)object).active += 1
## End a native call guard, disposing the owner once closed and idle.
pub static func leave(object: ownership.Object*):
ownership.check_alive(object)
let owner = (Owner[T]*)object
if owner.active == 0:
trap("a native call guard was left twice")
owner.active -= 1
if owner.closed and owner.active == 0:
dispose_owner[T](owner)
ownership.release_held(object)
## The collector's finalizer: close the owner.
pub static func finish_owner(object: ownership.Object*):
close_owner[T]((Owner[T]*)object)
## Free the owner's native storage.
pub static func drop_owner(object: ownership.Object*):
let owner = (Owner[T]*)object
if let native = owner.native:
let allocator = owner.native_allocator else trap("native storage has no allocator")
owner.native = none
free(native) in allocator
## Visit the native value's strong references.
pub static func trace_owner(object: ownership.Object*, visit: func(ownership.Object*, void*) -> unit, context: void*):
let owner = (Owner[T]*)object
if owner.disposed:
return
if let native = owner.native:
if let trace = owner.declaration.trace:
trace(native, visit, context)
func close_owner[T](owner: Owner[T]*):
ownership.check_alive(&owner.header)
if owner.closed:
return
# Publish closure before package cleanup can call back into an alias.
owner.closed = true
if owner.active == 0:
dispose_owner[T](owner)
func dispose_owner[T](owner: Owner[T]*):
if owner.disposed:
return
owner.disposed = true
if let native = owner.native:
owner.declaration.dispose(native)
## Unpublished ownership storage. Reserve before running a native initializer;
## publish only after success, or cancel without running a successful finalizer.
pub struct Reservation[T]:
var owner: Owner[T]*?
## Reserve unpublished storage for `declaration`.
pub func init(declaration: Type[T]) -> !:
if declaration.main_thread and not thread.is_main():
error(wrong_thread, "this native type requires the process main thread")
var information = ownership.Info(name = declaration.name, size = memory.size_of(Owner[T]),
finish = Owner[T].finish_owner, drop = Owner[T].drop_owner,
trace = Owner[T].trace_owner, disposes = true)
let owner = (Owner[T]*)try ownership.reserve(&information, memory.size_of(Owner[T]))
owner.information = information
owner.header.info = &owner.information
owner.declaration = declaration
self.owner = owner
## Publish the native value, yielding a reference and consuming the reservation.
pub func publish(native: T*, allocator: memory.Allocator? = none) -> Reference[T]:
let owner = self.owner else trap("native ownership has already been published or cancelled")
owner.native = native
owner.native_allocator = allocator else memory.allocator
self.owner = none
return Reference[T](owner)
## Discard the reservation without publishing.
pub func cancel():
if let owner = self.owner:
self.owner = none
ownership.discard_unpublished(&owner.header)
"""
## The text of `src/std/interop/reference.lucb`.
let text_34: str = r"""#==============================================================================================
#
# reference - A typed reference carrier
#
# DESCRIPTION:
# Parameters borrow the carrier; clone acquires an additional strong reference.
# Publish/adopt return one owning reference whose Base caller releases explicitly.
#
#==============================================================================================
## A typed reference carrier. Parameters borrow the carrier; clone acquires an
## additional strong reference. Publish/adopt return one owning reference, whose
## Base caller releases explicitly. Copying the struct does not acquire ownership.
pub struct Reference[T]:
## The owner shell this reference keeps alive.
pub let owner: Owner[T]*
## A reference over `owner`, which is already alive.
pub func init(owner: Owner[T]*):
ownership.check_alive(&owner.header)
self.owner = owner
## Adopt `native` into a new owned reference of `declaration`.
pub static func adopt(native: T*, declaration: Type[T], allocator: memory.Allocator? = none) -> Reference[T]!:
let selected = allocator else memory.allocator
var reservation = Reservation[T](declaration) catch failure:
release_native(native, declaration, selected)
error(failure)
return reservation.publish(native, selected)
## Another strong reference to the same value.
pub func clone() -> Reference[T]:
_ = ownership.retain(&self.owner.header)
return Reference[T](self.owner)
## Drop this reference's edge.
pub func release():
ownership.release_held(&self.owner.header)
## The native value, or a closed/uninitialized error.
pub func get() -> T*!:
ownership.check_alive(&self.owner.header)
if self.owner.closed:
error(invalid, "a native object is closed")
return self.owner.native else trap("a native object has not been initialized")
## Infallible access traps; fallible adapters use get to preserve a closed error.
pub func value() -> T*:
return self.get() catch failure:
ownership.stop_at_site(failure.message)
trap(failure.message)
## A stable identity for the value that never repeats.
pub func identity() -> u64:
ownership.check_alive(&self.owner.header)
return self.owner.header.generation
## Whether the value has been closed.
pub func is_closed() -> bool:
ownership.check_alive(&self.owner.header)
return self.owner.closed
## Close the value, if its type is closeable.
pub func close():
ownership.check_alive(&self.owner.header)
if not self.owner.declaration.closeable:
trap("this native type does not support explicit close")
close_owner[T](self.owner)
## A validity lease over the value.
pub func lease() -> Lease!:
_ = try self.get()
return try Lease(&self.owner.header, Owner[T].is_open, Owner[T].enter, Owner[T].leave)
## Validate that `object` is a live reference of this type.
pub static func validate_interface(object: ownership.Object*) -> !:
_ = try Reference[T]((Owner[T]*)object).get()
## The supplied witness must point into this owner's stable native storage.
pub func as_interface[I](native: I) -> Interface[I]:
return Interface[I](&self.owner.header, native, Reference[T].validate_interface,
Owner[T].enter, Owner[T].leave).clone()
## Guards are balanced by generated Luce calls or explicitly by Base callers.
pub func enter():
Owner[T].enter(&self.owner.header)
## End a native call guard on the value.
pub func leave():
Owner[T].leave(&self.owner.header)
## Every retained native edge must be visited by the package's trace callback.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
# Trial deletion temporarily subtracts internal strong references.
ownership.check_thread(&self.owner.header)
visit(&self.owner.header, context)
## Dispose of `native`, a value no reference took, and free it from `allocator`.
func release_native[T](native: T*, declaration: Type[T], allocator: memory.Allocator):
declaration.dispose(native)
free(native) in allocator
"""
## The text of `src/std/interop/weak.lucb`.
let text_35: str = r"""#==============================================================================================
#
# weak - A weak carrier over an owner shell
#
# DESCRIPTION:
# Keeps the owner shell, never the native resource; getting a live reference acquires one
# strong ownership obligation for the Base caller.
#
#==============================================================================================
## A weak carrier keeps the owner shell, never the native resource. Getting a
## live reference acquires one strong ownership obligation for the Base caller.
pub struct WeakReference[T]:
var weak: ownership.Weak
## A weak carrier watching `reference`'s owner.
pub func init(reference: Reference[T]):
self.weak = ownership.weak_make(&reference.owner.header)
## Another weak carrier watching the same owner.
pub func clone() -> WeakReference[T]:
var result = self
result.weak = ownership.weak_copy(self.weak)
return result
## Drop this weak carrier's edge to the owner.
pub func release():
ownership.weak_drop(self.weak)
## A fresh strong reference to the value, or none once it is gone.
pub func get() -> Reference[T]?:
let owner = ownership.weak_get(self.weak) else return none
return Reference[T]((Owner[T]*)owner)
"""
## The text of `src/std/interop/lease.lucb`.
let text_36: str = r"""#==============================================================================================
#
# lease - A one-shot validity scope
#
# DESCRIPTION:
# Tied to an owning native object: keeping the lease alive keeps its checks safe, and
# invalidation never extends the resource's scope.
#
#==============================================================================================
## A one-shot validity scope tied to an owning native object. Keeping a lease
## alive keeps its checks safe; invalidation never extends the resource's scope.
struct LeaseState:
var header: ownership.Object
var owner: ownership.Object*
var open: func(ownership.Object*) -> bool
var begin: func(ownership.Object*) -> unit
var end: func(ownership.Object*) -> unit
var valid: bool
func drop_lease(object: ownership.Object*):
let state = (LeaseState*)object
ownership.release_held(state.owner)
func trace_lease(object: ownership.Object*, visit: func(ownership.Object*, void*) -> unit, context: void*):
visit(((LeaseState*)object).owner, context)
var lease_info: ownership.Info = ownership.Info(name = "native lease", size = memory.size_of(LeaseState),
finish = none, drop = drop_lease, trace = trace_lease)
## A one-shot validity scope over an owning native object.
pub struct Lease:
var state: LeaseState*
var generation: u64
func init(owner: ownership.Object*, open: func(ownership.Object*) -> bool,
begin: func(ownership.Object*) -> unit, end: func(ownership.Object*) -> unit) -> !:
ownership.check_alive(owner)
let state = (LeaseState*)try ownership.reserve(&lease_info, memory.size_of(LeaseState))
state.owner = ownership.retain(owner)
state.open = open
state.begin = begin
state.end = end
state.valid = true
self.state = state
self.generation = state.header.generation
func alive():
ownership.check_alive(&self.state.header)
if self.generation != self.state.header.generation:
trap("a native lease has a stale generation")
## Another edge to the same lease.
pub func clone() -> Lease:
self.alive()
_ = ownership.retain(&self.state.header)
return self
## Drop this lease's edge.
pub func release():
self.alive()
ownership.release_held(&self.state.header)
## Mark the scope invalid; later checks fail.
pub func invalidate():
self.alive()
self.state.valid = false
## Whether the scope is still valid and its owner open.
pub func is_valid() -> bool:
self.alive()
return self.state.valid and self.state.open(self.state.owner)
## Error `expired` when the scope is no longer valid.
pub func check() -> !:
if not self.is_valid():
error(expired, "a native view has expired")
## An active invocation postpones physical disposal of its native owner.
## Operational closure/expiry is still visible immediately to every alias.
pub func enter() -> !:
try self.check()
_ = ownership.retain(&self.state.header)
self.state.begin(self.state.owner)
## End a guarded invocation, releasing the owner.
pub func leave():
self.alive()
self.state.end(self.state.owner)
ownership.release_held(&self.state.header)
## Visit the owner as a strong reference.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
ownership.check_thread(&self.state.header)
visit(&self.state.header, context)
"""
## The text of `src/std/interop/view.lucb`.
let text_37: str = r"""#==============================================================================================
#
# view - An exported borrowed native struct
#
# DESCRIPTION:
# A checked view of a borrowed native struct, never copied into ownership of the resource
# it describes; every operation checks its validity lease.
#
#==============================================================================================
## An explicit export of a borrowed native struct. Its complete native data is a
## view, never copied into ownership of the resource it describes.
pub struct ViewType[T]:
## The view type's name.
pub let name: str
## Whether the view allows mutable access.
pub let mutable: bool = false
## The owner shell backing a checked view.
pub struct ViewOwner[T]:
## The shared collector header.
pub var header: ownership.Object
var information: ownership.Info
var native: T
var declaration: ViewType[T]
var lease: Lease
## Release the view's lease when the owner is dropped.
pub static func drop_view(object: ownership.Object*):
((ViewOwner[T]*)object).lease.release()
## Visit the lease's strong references.
pub static func trace_view(object: ownership.Object*, visit: func(ownership.Object*, void*) -> unit, context: void*):
((ViewOwner[T]*)object).lease.trace(visit, context)
## Manual Base carrier for a checked view. The Luce adapter shares this owner and
## retains it in aliases/bound methods; every operation checks its lease again.
pub struct View[T]:
## The owner shell this view shares.
pub let owner: ViewOwner[T]*
## A view over `owner`, which is already alive.
pub func init(owner: ViewOwner[T]*):
ownership.check_alive(&owner.header)
self.owner = owner
## A view of `native`, valid while `lease` is.
pub static func make(native: T, declaration: ViewType[T], lease: Lease) -> View[T]!:
try lease.check()
var information = ownership.Info(name = declaration.name, size = memory.size_of(ViewOwner[T]),
finish = none, drop = ViewOwner[T].drop_view, trace = ViewOwner[T].trace_view)
let owner = (ViewOwner[T]*)try ownership.reserve(&information, memory.size_of(ViewOwner[T]))
owner.information = information
owner.header.info = &owner.information
owner.native = native
owner.declaration = declaration
owner.lease = lease.clone()
return View[T](owner)
## Another strong reference to the same view.
pub func clone() -> View[T]:
_ = ownership.retain(&self.owner.header)
return self
## Drop this view's edge.
pub func release():
ownership.release_held(&self.owner.header)
## A read-only pointer to the value, after checking the lease.
pub func get() -> const T*!:
ownership.check_alive(&self.owner.header)
try self.owner.lease.check()
return &self.owner.native
## A mutable pointer to the value, when the view is mutable.
pub func get_mut() -> T*!:
_ = try self.get()
if not self.owner.declaration.mutable:
error(invalid, "a native view is read-only")
return &self.owner.native
## The value, trapping if the lease has expired.
pub func value() -> const T*:
return self.get() catch failure:
ownership.stop_at_site(failure.message)
trap(failure.message)
## The mutable value, trapping when unavailable.
pub func value_mut() -> T*:
return self.get_mut() catch failure:
ownership.stop_at_site(failure.message)
trap(failure.message)
## Whether the view's lease is still valid.
pub func is_valid() -> bool:
ownership.check_alive(&self.owner.header)
return self.owner.lease.is_valid()
## Begin a guarded invocation, retaining the owner.
pub func enter() -> !:
ownership.check_alive(&self.owner.header)
try self.owner.lease.enter()
_ = ownership.retain(&self.owner.header)
## End a guarded invocation.
pub func leave():
ownership.check_alive(&self.owner.header)
self.owner.lease.leave()
ownership.release_held(&self.owner.header)
## Validate that `object` is a live view of this type.
pub static func validate_interface(object: ownership.Object*) -> !:
_ = try View[T]((ViewOwner[T]*)object).get()
## Begin a guarded invocation for an interface witness.
pub static func begin_interface(object: ownership.Object*):
View[T]((ViewOwner[T]*)object).enter() catch failure:
ownership.stop_at_site(failure.message)
trap(failure.message)
## End a guarded invocation for an interface witness.
pub static func end_interface(object: ownership.Object*):
View[T]((ViewOwner[T]*)object).leave()
## The witness borrows this view's storage and preserves its validity lease.
pub func as_interface[I](native: I) -> Interface[I]:
return Interface[I](&self.owner.header, native, View[T].validate_interface,
View[T].begin_interface, View[T].end_interface).clone()
## Visit the owner as a strong reference.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
ownership.check_thread(&self.owner.header)
visit(&self.owner.header, context)
"""
## The text of `src/std/interop/interface.lucb`.
let text_38: str = r"""#==============================================================================================
#
# interface - An owned interface witness
#
# DESCRIPTION:
# Pairs an interface witness with explicit ownership and validity; a bare Base interface is
# only a pointer/table view, so this carrier makes retention deliberate.
#
#==============================================================================================
## An interface witness paired with explicit ownership and validity. A bare Base
## interface is only a pointer/table view; this carrier makes retention deliberate.
pub struct Interface[I]:
## The retained object the witness belongs to.
pub let owner: ownership.Object*
let native: I
let validate: (func(ownership.Object*) -> unit!)?
let begin: (func(ownership.Object*) -> unit)?
let end: (func(ownership.Object*) -> unit)?
## Construction borrows. A returned carrier transfers one retained reference;
## use clone when the caller already owns the original reference.
pub func init(owner: ownership.Object*, native: I,
validate: (func(ownership.Object*) -> unit!)? = none,
begin: (func(ownership.Object*) -> unit)? = none,
end: (func(ownership.Object*) -> unit)? = none):
ownership.check_alive(owner)
if (begin == none) != (end == none):
trap("an interface needs paired invocation guards")
self.owner = owner
self.native = native
self.validate = validate
self.begin = begin
self.end = end
## Another edge to the same witness.
pub func clone() -> Interface[I]:
_ = ownership.retain(self.owner)
return self
## Drop this witness's edge to the owner.
pub func release():
ownership.release_held(self.owner)
## The interface value, after checking that the owner is alive and valid.
pub func get() -> I!:
ownership.check_alive(self.owner)
if let validate = self.validate:
try validate(self.owner)
return self.native
## The interface value, trapping if it is invalid.
pub func value() -> I:
return self.get() catch failure:
ownership.stop_at_site(failure.message)
trap(failure.message)
## Begin a guarded invocation, retaining the owner.
pub func enter() -> !:
_ = try self.get()
_ = ownership.retain(self.owner)
if let begin = self.begin:
begin(self.owner)
## End a guarded invocation, releasing the owner.
pub func leave():
ownership.check_alive(self.owner)
if let end = self.end:
end(self.owner)
ownership.release_held(self.owner)
## Visit the owner as a strong reference.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
ownership.check_thread(self.owner)
visit(self.owner, context)
"""
## The text of `src/std/interop/result.lucb`.
let text_39: str = r"""#==============================================================================================
#
# result - Owned native values and outcomes
#
# DESCRIPTION:
# A native value with explicit backing storage, and a callback's success value or failure
# text, each carrying an explicit owner a Base caller releases.
#
#==============================================================================================
## A native value with explicit backing storage. The value may borrow that storage;
## clone retains it, release ends the obligation. Plain/static values need no owner.
pub struct Owned[T]:
## The native value, which may borrow the owner's storage.
pub let value: T
## The object backing the value, or none for a plain value.
pub let owner: ownership.Object*? = none
## Another edge to the value's owner, when it has one.
pub func clone() -> Owned[T]:
if let owner = self.owner:
_ = ownership.retain(owner)
return self
## End this value's obligation to its owner.
pub func release():
if let owner = self.owner:
ownership.release_held(owner)
## Visit the owner as a strong reference.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
if let owner = self.owner:
ownership.check_thread(owner)
visit(owner, context)
let no_error: ErrorCode = ErrorCode.package(0)
## A callback's success value or failure text, each with an explicit owner. A Base
## caller can consume a failure without leaving managed error text pending anywhere.
## get borrows until release; it never transfers ownership of the contained value.
pub struct Outcome[T]:
let result: Owned[T]?
## The failure code, or `package(0)` on success.
pub let code: ErrorCode
let message: Owned[str]?
## A successful outcome carrying `value`.
pub static func success(value: Owned[T]) -> Outcome[T]:
return Outcome[T](value, no_error, none)
## A failed outcome with `code` and `message`.
pub static func failure(code: ErrorCode, message: Owned[str]) -> Outcome[T]:
return Outcome[T](none, code, message)
## Whether the outcome succeeded.
pub func is_success() -> bool:
return self.message == none
## The success value, or the failure as an error.
pub func get() -> T!:
if let message = self.message:
error(self.code, message.value)
return (self.result else trap("an outcome has no result")).value
## A copy of the outcome, retaining its owned value or message.
pub func clone() -> Outcome[T]:
if let message = self.message:
return Outcome[T].failure(self.code, message.clone())
return Outcome[T].success((self.result else trap("an outcome has no result")).clone())
## Release the outcome's owned value or message.
pub func release():
if let result = self.result:
result.release()
if let message = self.message:
message.release()
## Visit the outcome's strong references.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
if let result = self.result:
result.trace(visit, context)
if let message = self.message:
message.trace(visit, context)
func drop_text(object: ownership.Object*):
_ = object
func trace_text(object: ownership.Object*, visit: func(ownership.Object*, void*) -> unit, context: void*):
_ = object
var text_info: ownership.Info = ownership.Info(name = "native owned text", size = 0,
finish = none, drop = drop_text, trace = trace_text)
## Own a copy of text from a temporary buffer or a native error before it expires.
pub func copy_text(value: str) -> Owned[str]!:
let size = memory.size_of(ownership.Object) +? value.length else error(memory.exhausted, "owned text is too large")
let owner = try ownership.reserve(&text_info, size)
let bytes = u8[]((u8*)owner + memory.size_of(ownership.Object), value.length)
memory.copy(bytes, value.bytes, value.length)
return Owned[str]((str)bytes, owner)
## A successful unit outcome, for a callback that reports only success or failure. It saves
## every such callback from spelling `Outcome[unit].success(Owned[unit](()))`.
pub func success_unit() -> Outcome[unit]:
return Outcome[unit].success(Owned[unit](()))
## A failed unit outcome carrying `code` and an owned copy of `message`, the bridge from a
## Base `!` failure to the `Outcome` a callback returns: `expr catch failure: return
## interop.failure_unit(failure.code, failure.message)`. If copying the message itself runs
## out of memory, the outcome reports memory.exhausted with a static note instead of pending
## managed text.
pub func failure_unit(code: ErrorCode, message: str) -> Outcome[unit]:
let text = copy_text(message) catch exhausted:
_ = exhausted
return Outcome[unit].failure(memory.exhausted, Owned[str]("out of memory copying an error message"))
return Outcome[unit].failure(code, text)
test "the unit outcome helpers carry success and copied failure text":
var good = success_unit()
assert(good.is_success())
good.release()
var bad = failure_unit(memory.exhausted, "boom")
assert(not bad.is_success())
bad.get() catch failure:
assert(failure.message == "boom")
bad.release()
"""
## The text of `src/std/interop/callback.lucb`.
let text_40: str = r"""#==============================================================================================
#
# callback - A retained callable carrier
#
# DESCRIPTION:
# Copying the carrier borrows; cloning acquires an edge. Managed closures use their
# existing owner, so captures remain visible to ARC.
#
#==============================================================================================
## A retained callable. Copying this carrier borrows; cloning acquires an edge.
## Managed closures use their existing owner, so captures remain visible to ARC.
pub struct Callback[A, R]:
## The retained object the callable belongs to.
pub let owner: ownership.Object*
## Adapter identity. Call through invoke to retain the owner and check affinity.
pub let entry: func(ownership.Object*, A) -> Outcome[R]
let context: u64
## A callback over `owner`, called through `entry`.
pub func init(owner: ownership.Object*, entry: func(ownership.Object*, A) -> Outcome[R]):
ownership.check_alive(owner)
self.owner = owner
self.entry = entry
self.context = ownership.context_id()
func require_thread():
if self.context != ownership.context_id():
trap("a callback belongs to another runtime thread")
## Another edge to the same callable.
pub func clone() -> Callback[A, R]:
self.require_thread()
_ = ownership.retain(self.owner)
return self
## Drop this callback's edge to the owner.
pub func release():
self.require_thread()
ownership.release_held(self.owner)
## Call the callable with `argument`, retaining the owner across the call.
pub func invoke(argument: A) -> Outcome[R]:
self.require_thread()
_ = ownership.retain(self.owner)
defer ownership.release_held(self.owner)
return self.entry(self.owner, argument)
## Visit the owner as a strong reference.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
self.require_thread()
ownership.check_thread(self.owner)
visit(self.owner, context)
## Native methods receive stable storage under the source owner's call guard.
pub static func bind[T](source: Reference[T], method: func(const T*, A) -> Outcome[R]) -> Callback[A, R]!:
return try BoundCallback[T, A, R].make(source, none, method)
## A callback bound to a method of a native `source` that changes it.
pub static func bind_mutating[T](source: Reference[T], method: func(T*, A) -> Outcome[R]) -> Callback[A, R]!:
return try BoundCallback[T, A, R].make(source, method, none)
struct BoundCallback[T, A, R]:
var header: ownership.Object
var information: ownership.Info
var source: Reference[T]
var method: (func(T*, A) -> Outcome[R])?
var read: (func(const T*, A) -> Outcome[R])?
static func make(source: Reference[T], method: (func(T*, A) -> Outcome[R])?,
read: (func(const T*, A) -> Outcome[R])?) -> Callback[A, R]!:
_ = try source.get()
var information = ownership.Info(name = "bound native callback", size = memory.size_of(BoundCallback[T, A, R]),
finish = none, drop = BoundCallback[T, A, R].drop, trace = BoundCallback[T, A, R].trace)
let bound = (BoundCallback[T, A, R]*)try ownership.reserve(&information, memory.size_of(BoundCallback[T, A, R]))
bound.information = information
bound.header.info = &bound.information
bound.source = source.clone()
bound.method = method
bound.read = read
return Callback[A, R](&bound.header, BoundCallback[T, A, R].invoke)
static func invoke(object: ownership.Object*, argument: A) -> Outcome[R]:
let bound = (BoundCallback[T, A, R]*)object
let native = bound.source.get() catch failure:
return failed_outcome[R](failure.code, failure.message)
bound.source.enter()
defer bound.source.leave()
if let method = bound.method:
return method(native, argument)
return (bound.read else trap("a callback binding has no method"))(native, argument)
static func drop(object: ownership.Object*):
((BoundCallback[T, A, R]*)object).source.release()
static func trace(object: ownership.Object*, visit: func(ownership.Object*, void*) -> unit, context: void*):
((BoundCallback[T, A, R]*)object).source.trace(visit, context)
## Error views may borrow a closing source. Copy before releasing that source;
## allocation failure itself has a static message and needs no further allocation.
func failed_outcome[T](code: ErrorCode, message: str) -> Outcome[T]:
let owned = copy_text(message) catch failure:
return Outcome[T].failure(memory.exhausted, Owned[str]("out of memory copying a callback failure"))
return Outcome[T].failure(code, owned)
"""
## The text of `src/std/interop/connection.lucb`.
let text_41: str = r"""#==============================================================================================
#
# connection - An owning registration token
#
# DESCRIPTION:
# Dropping its last reference disconnects; explicit disconnection stays queryable and
# releases its signal-state edge at once.
#
#==============================================================================================
## Owning registration token. Dropping its last reference disconnects; explicit
## disconnection remains queryable and releases its signal-state edge immediately.
pub struct Connection:
var state: ownership.Object*?
let identifier: u64
let remove: func(ownership.Object*, u64) -> unit
let contains: func(ownership.Object*, u64) -> bool
func init(state: ownership.Object*, identifier: u64,
remove: func(ownership.Object*, u64) -> unit,
contains: func(ownership.Object*, u64) -> bool):
self.state = state
self.identifier = identifier
self.remove = remove
self.contains = contains
_ = ownership.retain(state)
## Remove the registration; idempotent.
pub func disconnect():
let state = self.state else return
self.state = none
self.remove(state, self.identifier)
ownership.release_held(state)
## Whether the registration is still present.
pub func is_connected() -> bool:
let state = self.state else return false
ownership.check_alive(state)
return self.contains(state, self.identifier)
func destroy():
self.disconnect()
func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
if let state = self.state:
ownership.check_thread(state)
visit(state, context)
## The interop type describing a `Connection`.
pub let connection_type: Type[Connection] = Type[Connection](name = "Connection",
dispose = Connection.destroy, trace = Connection.trace)
"""
## The text of `src/std/interop/signal.lucb`.
let text_42: str = r"""#==============================================================================================
#
# signal - A native multicast signal
#
# DESCRIPTION:
# A retained signal that fans an argument out to every connected callback; connecting
# yields a `Connection` token whose last reference disconnects.
#
#==============================================================================================
struct Subscription[A]:
let identifier: u64
let callback: Callback[A, unit]
var next: Subscription[A]*?
struct Delivery[A]:
let identifier: u64
let callback: Callback[A, unit]
struct SignalState[A]:
var header: ownership.Object
var information: ownership.Info
var allocator: memory.Allocator
var first: Subscription[A]*?
var last: Subscription[A]*?
var serial: u64
var count: usize
var closed: bool
static func contains(object: ownership.Object*, identifier: u64) -> bool:
let state = (SignalState[A]*)object
var cursor = state.first
while let entry = cursor:
if entry.identifier == identifier:
return true
cursor = entry.next
return false
static func remove(object: ownership.Object*, identifier: u64):
let state = (SignalState[A]*)object
var previous: Subscription[A]*? = none
var cursor = state.first
while let entry = cursor:
if entry.identifier == identifier:
if let before = previous:
before.next = entry.next
else:
state.first = entry.next
if state.last == entry:
state.last = previous
state.count -= 1
# Detach before releasing: callback destruction can reenter us.
entry.callback.release()
free(entry) in state.allocator
return
previous = entry
cursor = entry.next
static func close(object: ownership.Object*):
let state = (SignalState[A]*)object
state.closed = true
var cursor = state.first
state.first = none
state.last = none
state.count = 0
while let entry = cursor:
cursor = entry.next
entry.callback.release()
free(entry) in state.allocator
static func trace(object: ownership.Object*, visit: func(ownership.Object*, void*) -> unit, context: void*):
var cursor = ((SignalState[A]*)object).first
while let entry = cursor:
entry.callback.trace(visit, context)
cursor = entry.next
## Ordered, thread-bound delivery. Base copies borrow this carrier; clone/release
## manage ownership. Package owners close their signal before releasing it.
# mark: The signal carrier =====================================================================
## A typed, connectable signal: an owned carrier its aliases share by reference count.
pub struct Signal[A]:
let state: SignalState[A]*
## A custom allocator must outlive the signal and its connection aliases.
pub func init(allocator: memory.Allocator? = none) -> !:
let selected = allocator else memory.heap
var information = ownership.Info(name = "signal", size = memory.size_of(SignalState[A]),
finish = none, drop = SignalState[A].close, trace = SignalState[A].trace)
let state = (SignalState[A]*)try ownership.reserve(&information, memory.size_of(SignalState[A]), selected)
state.information = information
state.header.info = &state.information
state.allocator = selected
self.state = state
## Another strong reference to the same signal.
pub func clone() -> Signal[A]:
_ = ownership.retain(&self.state.header)
return self
## Drop this signal's edge.
pub func release():
ownership.release_held(&self.state.header)
## Close the signal; further connects and emits fail.
pub func close():
ownership.check_alive(&self.state.header)
SignalState[A].close(&self.state.header)
## Whether the signal has been closed.
pub func is_closed() -> bool:
ownership.check_alive(&self.state.header)
return self.state.closed
## Visit the signal state as a strong reference.
pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
ownership.check_thread(&self.state.header)
visit(&self.state.header, context)
## Register `callback`, returning a `Connection` token.
pub func connect(callback: Callback[A, unit]) -> Reference[Connection]!:
ownership.check_alive(&self.state.header)
ownership.check_alive(callback.owner)
if self.state.closed:
error(invalid, "a signal is closed")
let identifier = self.state.serial +? 1 else error(invalid, "signal registration identities exhausted")
var reservation = try Reservation[Connection](connection_type)
errdefer reservation.cancel()
let token = reservation.publish(try new Connection(&self.state.header, identifier,
SignalState[A].remove, SignalState[A].contains) in self.state.allocator, self.state.allocator)
errdefer token.release()
let entry = try new Subscription[A](identifier, callback.clone(), none) in self.state.allocator
self.state.serial = identifier
self.state.count += 1
if let last = self.state.last:
last.next = entry
else:
self.state.first = entry
self.state.last = entry
return token
## Deliver `argument` to every connected callback.
pub func emit(argument: A) -> Outcome[unit]:
_ = ownership.retain(&self.state.header)
defer ownership.release_held(&self.state.header)
if self.state.closed:
return Outcome[unit].failure(invalid, Owned[str]("a signal is closed"))
if self.state.count == 0:
return Outcome[unit].success(Owned[unit](()))
let snapshot = new Delivery[A][self.state.count] --- in self.state.allocator catch failure:
return failed_outcome[unit](failure.code, failure.message)
defer free(snapshot) in self.state.allocator
var cursor = self.state.first
var count: usize = 0
while let entry = cursor:
snapshot[count] = Delivery[A](entry.identifier, entry.callback.clone())
count += 1
cursor = entry.next
defer release_deliveries[A](snapshot)
for delivery in snapshot:
if not SignalState[A].contains(&self.state.header, delivery.identifier):
continue
let result = delivery.callback.invoke(argument)
if not result.is_success():
return result
result.release()
return Outcome[unit].success(Owned[unit](()))
func release_deliveries[A](deliveries: const Delivery[A][]):
for delivery in deliveries:
delivery.callback.release()
"""
## The text of `src/std/interop/transfer.lucb`.
let text_43: str = r"""#==============================================================================================
#
# transfer - Uniquely owned cross-thread messages
#
# DESCRIPTION:
# A `Packet` is a uniquely owned message outside either thread's ARC graph; a `Transfer`
# is the deep-copy policy, and a `Reply` carries a worker's result or failure.
#
#==============================================================================================
## A uniquely owned message outside either thread's ARC graph. Copies borrow;
## passing a packet into a queue transfers its release obligation. Its disposer
## must work on either thread and must not access thread-bound owners/allocators.
pub struct Packet[T]:
## The message value.
pub let value: T
let storage: void*? = none
let dispose: (func(void*) -> unit)? = none
## A packet owning `value`, released through `dispose`.
pub func init(value: T, storage: void*? = none, dispose: (func(void*) -> unit)? = none):
self.value = value
self.storage = storage
self.dispose = dispose
## Dispose the packet's storage.
pub func release():
if let storage = self.storage:
(self.dispose else trap("a packet has storage without a disposer"))(storage)
## Deep-copy policy supplied by the native library for a wire value. All borrowed
## members must point into the returned packet or immutable process-lifetime data.
## A policy must never smuggle an ARC owner, checked view or callback across threads.
pub struct Transfer[T]:
## The library's deep-copy of a wire value into a packet.
pub let copy: func(T) -> Packet[T]!
## For pointer-free scalar/record values only. Text/spans need a deep copy.
pub static func plain() -> Transfer[T]:
return Transfer[T](Transfer[T].copy_plain)
static func copy_plain(value: T) -> Packet[T]!:
return Packet[T](value)
struct PacketBytes:
var bytes: u8[]
static func drop(raw: void*):
let storage = (PacketBytes*)raw
free(storage.bytes) in memory.heap
free(storage) in memory.heap
static func copy(value: const u8[]) -> Packet[const u8[]]!:
let bytes = try new u8[value.length] --- in memory.heap
errdefer free(bytes) in memory.heap
memory.copy(bytes, value, value.length)
let storage = try new PacketBytes(bytes) in memory.heap
return Packet[const u8[]](bytes, (void*)storage, PacketBytes.drop)
static func text(value: str) -> Packet[str]!:
let packet = try PacketBytes.copy(value.bytes)
return Packet[str]((str)packet.value, packet.storage, packet.dispose)
## The deep-copy policy for `str`.
pub let text_transfer: Transfer[str] = Transfer[str](PacketBytes.text)
## The deep-copy policy for byte spans.
pub let bytes_transfer: Transfer[const u8[]] = Transfer[const u8[]](PacketBytes.copy)
## A reply owns its successful packet or its copied failure text. get borrows;
## release is required whether the caller handles success or failure.
pub struct Reply[T]:
let result: Packet[T]?
## The failure code, or `package(0)` on success.
pub let code: ErrorCode
let message: Packet[str]?
## A successful reply carrying `value`.
pub static func success(value: Packet[T]) -> Reply[T]:
return Reply[T](value, no_error, none)
## A failed reply with `code` and a copied `message`.
pub static func failure(code: ErrorCode, message: str) -> Reply[T]:
let copy = text_transfer.copy(message) catch failure:
return Reply[T](none, memory.exhausted, Packet[str]("out of memory copying a worker failure"))
return Reply[T](none, code, copy)
## Whether the reply succeeded.
pub func is_success() -> bool:
return self.message == none
## The success value, or the failure as an error.
pub func get() -> T!:
if let message = self.message:
error(self.code, message.value)
return (self.result else trap("a worker reply has no result")).value
## Release the reply's owned value or message.
pub func release():
if let result = self.result:
result.release()
if let message = self.message:
message.release()
"""
## The text of `src/std/interop/worker_entry.lucb`.
let text_44: str = r"""#==============================================================================================
#
# worker_entry - A worker's factory and runtime hooks
#
# DESCRIPTION:
# A named factory that runs once on the new thread and returns its local handler, with no
# retained source-thread state.
#
#==============================================================================================
## A named factory and its runtime hooks, with no retained source-thread state.
## The factory runs exactly once on the new thread and returns its local handler.
## Compilers supply the managed entry adapter; Base factories use native().
pub struct WorkerEntry[C, M, R]:
## The factory's context pointer, borrowed for the worker's life.
pub let context: void*?
## Runs the factory on the new thread, yielding its handler or a failure.
pub let open: func(void*?, C) -> Outcome[Callback[M, R]]
## The runtime hook run as the worker starts.
pub let enter: func() -> unit
## The runtime hook run as the worker ends.
pub let leave: func() -> unit
## A worker entry from a plain Base factory.
pub static func native(factory: func(C) -> Outcome[Callback[M, R]]) -> WorkerEntry[C, M, R]:
return WorkerEntry[C, M, R]((void*)factory, WorkerEntry[C, M, R].open_native,
ownership.enter, ownership.finish_task)
static func open_native(context: void*?, configuration: C) -> Outcome[Callback[M, R]]:
let factory = (func(C) -> Outcome[Callback[M, R]])(context else trap("a worker has no factory"))
return factory(configuration)
local var active_worker_cancellation: sync.Cancellation*? = none
## Native operations called by a worker use this signal for interruptible socket
## waits. The pointer is borrowed on this worker until its factory/handler returns;
## it must not be retained outside the worker's lifetime or reset by application code.
pub func worker_cancellation() -> sync.Cancellation*:
return active_worker_cancellation else trap("this operation requires an application worker")
"""
## The text of `src/std/interop/worker_queue.lucb`.
let text_45: str = r"""#==============================================================================================
#
# worker_queue - A worker's message queue
#
# DESCRIPTION:
# Queue storage protected by the worker's mutex; removing a value transfers ownership
# before waking a producer, and disposal runs outside the lock.
#
#==============================================================================================
## Queue storage is protected by WorkerState's mutex. Removing a value transfers
## ownership before waking a producer; disposal runs outside the queue lock.
struct WorkerQueue[T]:
var slots: (T?)[]
var head: usize = 0
var count: usize = 0
func full() -> bool:
return self.count == self.slots.length
func push(value: T):
assert(not self.full())
self.slots[(self.head + self.count) % self.slots.length] = value
self.count += 1
func pop() -> T:
assert(self.count > 0)
let value = self.slots[self.head] else trap("an occupied worker slot is empty")
self.slots[self.head] = none
self.head = (self.head + 1) % self.slots.length
self.count -= 1
return value
"""
## The text of `src/std/interop/worker.lucb`.
let text_46: str = r"""#==============================================================================================
#
# worker - A managed worker thread
#
# DESCRIPTION:
# A worker runs a factory-built handler on its own thread; messages are posted with
# `send` and their replies delivered through the entry's callback.
#
#==============================================================================================
## The worker has been closed.
pub let worker_closed: ErrorCode = ErrorCode.package(104)
## The worker's queue is full.
pub let worker_busy: ErrorCode = ErrorCode.package(105)
struct WorkerState[C, M, R]:
var mutex: sync.Mutex
var changed: sync.Condition
var cancellation: sync.Cancellation
var ready: bool
var done: bool
var startup_error: Reply[unit]?
var input: WorkerQueue[Packet[M]]
var output: WorkerQueue[Reply[R]]
let configuration: Packet[C]
let entry: WorkerEntry[C, M, R]
let message_transfer: Transfer[M]
let result_transfer: Transfer[R]
var thread: thread.Handle
static func start(entry: WorkerEntry[C, M, R], configuration: C,
config_transfer: Transfer[C], message_transfer: Transfer[M], result_transfer: Transfer[R],
capacity: usize, stack: usize) -> WorkerState[C, M, R]*!:
if capacity == 0:
error(invalid, "a worker queue needs positive capacity")
let config = try config_transfer.copy(configuration)
errdefer config.release()
let input = try new (Packet[M]?)[capacity] --- in memory.heap
errdefer free(input) in memory.heap
let output = try new (Reply[R]?)[capacity] --- in memory.heap
errdefer free(output) in memory.heap
let block = try new WorkerState[C, M, R][1] --- in memory.heap
errdefer free(block) in memory.heap
let state = &block[0]
var mutex: sync.Mutex
var changed: sync.Condition
var cancellation: sync.Cancellation
var worker_thread: thread.Handle
*state = WorkerState[C, M, R](mutex, changed, cancellation, false, false, none,
WorkerQueue[Packet[M]](input), WorkerQueue[Reply[R]](output), config,
entry, message_transfer, result_transfer, worker_thread)
state.thread = try thread.spawn(WorkerState[C, M, R].run, (void*)state, stack)
return state
static func run(raw: void*?):
let state = (WorkerState[C, M, R]*)(raw else trap("a worker has no state"))
state.entry.enter()
active_worker_cancellation = &state.cancellation
let opened = state.entry.open(state.entry.context, state.configuration.value)
let handler = opened.get() catch failure:
let saved = Reply[unit].failure(failure.code, failure.message)
opened.release()
state.configuration.release()
state.mutex.lock()
state.startup_error = saved
state.ready = true
state.changed.broadcast()
state.mutex.unlock()
WorkerState[C, M, R].finish(state)
return
# The Outcome owns the handler until this thread has finished delivery.
state.configuration.release()
state.mutex.lock()
state.ready = true
state.changed.broadcast()
state.mutex.unlock()
WorkerState[C, M, R].deliver(state, handler)
opened.release()
WorkerState[C, M, R].finish(state)
static func finish(state: WorkerState[C, M, R]*):
active_worker_cancellation = none
state.entry.leave()
state.mutex.lock()
state.done = true
state.changed.broadcast()
state.mutex.unlock()
static func deliver(state: WorkerState[C, M, R]*, handler: Callback[M, R]):
while true:
state.mutex.lock()
while state.input.count == 0 and not state.cancellation.is_requested():
state.changed.wait(&state.mutex)
if state.cancellation.is_requested():
state.mutex.unlock()
return
let message = state.input.pop()
state.changed.broadcast()
state.mutex.unlock()
let result = handler.invoke(message.value)
let reply = WorkerState[C, M, R].copy_reply(state, result)
result.release()
message.release()
state.mutex.lock()
while state.output.full() and not state.cancellation.is_requested():
state.changed.wait(&state.mutex)
if state.cancellation.is_requested():
state.mutex.unlock()
reply.release()
return
state.output.push(reply)
state.changed.broadcast()
state.mutex.unlock()
## Under the mutex: the queued reply first, or worker_closed once the worker has
## stopped; none while it runs with nothing queued.
static func take_reply(state: WorkerState[C, M, R]*) -> Reply[R]?!:
if state.cancellation.is_requested() or state.done:
error(worker_closed, "the worker no longer produces replies")
if state.output.count == 0:
return none
let result = state.output.pop()
state.changed.broadcast()
return result
static func copy_reply(state: WorkerState[C, M, R]*, outcome: Outcome[R]) -> Reply[R]:
let value = outcome.get() catch failure:
return Reply[R].failure(failure.code, failure.message)
let copy = state.result_transfer.copy(value) catch failure:
return Reply[R].failure(failure.code, failure.message)
return Reply[R].success(copy)
## A bounded persistent worker. This carrier is uniquely owned; Base copies borrow.
## send/receive/cancel may be called concurrently while the owner keeps it alive.
## close must run after those callers return; it cancels and joins before freeing.
## Luce libraries expose their own owned application-specific wrapper around it.
# mark: The worker handle ======================================================================
## A bounded persistent worker: a uniquely-owned handle over a thread, its queues and cancellation.
pub struct Worker[C, M, R]:
var state: WorkerState[C, M, R]*?
## Start `entry` on a new worker thread with `configuration`, waiting until it is ready.
pub func init(entry: WorkerEntry[C, M, R], configuration: C,
config_transfer: Transfer[C], message_transfer: Transfer[M], result_transfer: Transfer[R],
capacity: usize = 16, stack: usize = 0) -> !:
let state = try WorkerState[C, M, R].start(entry, configuration, config_transfer,
message_transfer, result_transfer, capacity, stack)
self.state = state
state.mutex.lock()
while not state.ready:
state.changed.wait(&state.mutex)
state.mutex.unlock()
if let failure = state.startup_error:
# Copy into the creator's temporary pool before joining/freeing state.
# Native callers scope ownership.mark/drain around error consumption.
let message = (failure.message else trap("a startup failure has no message")).value
let saved = copy_text(message) catch exhausted:
self.close()
error(memory.exhausted, "out of memory copying a worker startup failure")
let code = failure.code
_ = ownership.pool_object(saved.owner else trap("copied text has no owner"))
self.close()
error(code, saved.value)
## Post `message` to the worker, blocking while its input queue is full.
pub func send(message: M) -> !:
try self.dispatch(message, true)
## Nonblocking backpressure: a full input queue returns worker_busy.
pub func try_send(message: M) -> !:
try self.dispatch(message, false)
func dispatch(message: M, wait: bool) -> !:
let state = self.state else error(worker_closed, "the worker is closed")
let packet = try state.message_transfer.copy(message)
errdefer packet.release()
state.mutex.lock()
defer state.mutex.unlock()
while state.input.full() and not state.cancellation.is_requested() and not state.done:
if not wait:
error(worker_busy, "the worker input queue is full")
state.changed.wait(&state.mutex)
if state.cancellation.is_requested() or state.done:
error(worker_closed, "the worker no longer accepts messages")
state.input.push(packet)
state.changed.broadcast()
## The next reply, waiting for it. Replies preserve accepted-message order.
## Cancellation is terminal: queued replies are discarded by close, and blocked
## receivers wake with worker_closed.
pub func receive() -> Reply[R]!:
let state = self.state else error(worker_closed, "the worker is closed")
state.mutex.lock()
defer state.mutex.unlock()
while state.output.count == 0 and not state.cancellation.is_requested() and not state.done:
state.changed.wait(&state.mutex)
return try WorkerState[C, M, R].take_reply(state) else trap("a worker woke with no reply")
## The next reply if one is ready, none if not: a receive that never waits, for a
## caller that polls, as a UI frame does.
pub func try_receive() -> Reply[R]?!:
return try self.receive_within(0)
## The next reply, waiting at most about `milliseconds` for it; none when none came.
pub func receive_within(milliseconds: u64) -> Reply[R]?!:
let state = self.state else error(worker_closed, "the worker is closed")
let deadline = time.now() + milliseconds * 1000000
state.mutex.lock()
defer state.mutex.unlock()
while state.output.count == 0 and not state.cancellation.is_requested() and not state.done:
let now = time.now()
if now >= deadline:
return none
state.changed.wait_for(&state.mutex, (deadline - now + 999999) // 1000000)
return try WorkerState[C, M, R].take_reply(state)
## Ask the worker to stop; cancellation is terminal and wakes blocked callers.
pub func cancel():
let state = self.state else return
state.mutex.lock()
state.cancellation.request()
state.changed.broadcast()
state.mutex.unlock()
## Cancel the worker, join its thread, drain its queues, and free it.
pub func close():
let state = self.state else return
self.cancel()
state.thread.join() catch failure:
trap("a worker could not join its thread")
self.state = none
while state.input.count > 0:
state.input.pop().release()
while state.output.count > 0:
state.output.pop().release()
if let failure = state.startup_error:
failure.release()
free(state.input.slots) in memory.heap
free(state.output.slots) in memory.heap
free(u8[]((u8*)state, memory.size_of(WorkerState[C, M, R]))) in memory.heap
"""
## The text of `src/std/debug.lucb`.
let text_47: str = r"""#==============================================================================================
#
# debug - The in-program source debugger's runtime
#
# DESCRIPTION:
# The hooks a `--debug` build calls on entry, exit and at each statement, and the
# breakpoint console that reads frames by their described layout.
#
#==============================================================================================
import c
import memory
extern func getenv(name: c.str) -> c.str?
extern func c_read as "read"(descriptor: i32, buffer: void*, count: usize) -> isize
extern func c_exit as "exit"(code: i32) -> never
## A frame as the compiler described it under `--debug`: the function's name and
## source, then `count` locals laid out after it, in this record's own layout.
struct Shape:
var name: str
var source: str
var count: u64
struct Local:
var name: str
var kind: str
var offset: u64
var size: u64
struct Activation:
var shape: Shape*?
var frame: void*?
var line: u32
struct Break:
var file: str
var line: u32
var activations: Activation[512]
var depth: usize
var checked: bool
var active: bool
var started: bool
var stepping: bool
var step_over: usize
var breaks: Break[32]
var break_count: usize
var names: u8[4096]
var names_used: usize
var line_bytes: u8[512]
## Called at every function entry and exit of a `--debug` build.
# mark: Debugger hooks ========================================================================
## Enter a described lexical scope for the in-program debugger.
pub func enter(shape: void*):
if depth < activations.length:
activations[depth] = Activation(shape = (Shape*)shape, frame = none, line = 0)
depth += 1
## Pop the current activation from the debugger's shadow stack.
pub func leave():
if depth > 0:
depth -= 1
## Called before every statement: stop when a breakpoint, a step, or the start asks.
pub func at(shape: void*, line: u32, frame: void*):
if not checked:
checked = true
active = getenv("LUCE_DEBUG") != none
if not active or depth == 0 or depth > activations.length:
return
let top = depth - 1
let s = (Shape*)shape
activations[top] = Activation(shape = s, frame = frame, line = line)
var stop = false
if not started:
started = true
stop = true
elif stepping:
stop = true
elif step_over > 0 and depth <= step_over:
stop = true
elif hits(s.source, line):
stop = true
if not stop:
return
stepping = false
step_over = 0
print(f"stopped at {s.source}:{line} in {s.name}")
prompt(top)
func hits(file: str, line: u32) -> bool:
var i: usize = 0
while i < break_count:
let b = breaks[i]
if b.line == line and (b.file == file or ends_with(file, b.file)):
return true
i += 1
return false
func ends_with(text: str, suffix: str) -> bool:
if suffix.length > text.length:
return false
return (str)text.bytes[text.length - suffix.length..] == suffix
## Read commands until one lets the program run.
func prompt(top: usize):
while true:
let line = read_line() else return
let command = first_word(line)
let rest = trim((str)line.bytes[command.length..])
if command == "r" or command == "run" or command == "c" or command == "continue":
return
if command == "s" or command == "step":
stepping = true
return
if command == "n" or command == "next":
step_over = depth
return
if command == "q" or command == "quit":
print("quit")
c_exit(0)
if command == "b" or command == "break":
add_break(rest)
elif command == "p" or command == "print":
show_named(top, rest)
elif command == "locals":
show_all(top)
elif command == "bt" or command == "backtrace":
backtrace()
elif command.length == 0:
continue
else:
print(f"unknown command `{command}`; commands: break FILE:LINE, run, step, next, print NAME, locals, backtrace, quit")
func add_break(spec: str):
var colon: usize = spec.length
var i = spec.length
while i > 0:
i -= 1
if spec.bytes[i] == ':':
colon = i
break
if colon == spec.length:
print("break needs FILE:LINE")
return
let file = (str)spec.bytes[..<colon]
let number = parse_line((str)spec.bytes[colon + 1..])
if number == 0 or break_count == breaks.length or names_used + file.length > names.length:
print("cannot set that breakpoint")
return
memory.copy(names[names_used..], file.bytes, file.length)
let kept = (str)names[names_used..<names_used + file.length]
names_used += file.length
breaks[break_count] = Break(file = kept, line = number)
break_count += 1
print(f"breakpoint {break_count} at {file}:{number}")
func parse_line(text: str) -> u32:
var value: u32 = 0
for byte in text.bytes:
if byte < '0' or byte > '9':
return 0
value = value * 10 + (u32)(byte - '0')
return value
func backtrace():
var i = depth
var index: usize = 0
while i > 0:
i -= 1
let a = activations[i]
if let s = a.shape:
print(f"#{index} {s.name} at {s.source}:{a.line}")
index += 1
func show_all(top: usize):
let a = activations[top]
let s = a.shape else return
var i: u64 = 0
while i < s.count:
show(local_of(s, i), a.frame)
i += 1
func show_named(top: usize, name: str):
let a = activations[top]
let s = a.shape else return
var i: u64 = 0
while i < s.count:
let described = local_of(s, i)
if described.name == name:
show(described, a.frame)
return
i += 1
print(f"no local named `{name}`")
## The locals follow the shape, each `memory.size_of(Local)` bytes.
func local_of(s: Shape*, index: u64) -> Local*:
let base = (usize)s + memory.size_of(Shape) + (usize)index * memory.size_of(Local)
return (Local*)base
## A local's value read from the frame and shown by its type's spelling.
func show(described: Local*, frame: void*?):
let f = frame else return
let address = (void*)((usize)f - (usize)described.offset)
let kind = described.kind
if kind == "i64" or kind == "isize":
print(f"{described.name}: {kind} = {memory.read[i64](address)}")
elif kind == "i32":
print(f"{described.name}: {kind} = {memory.read[i32](address)}")
elif kind == "i16":
print(f"{described.name}: {kind} = {memory.read[i16](address)}")
elif kind == "i8":
print(f"{described.name}: {kind} = {memory.read[i8](address)}")
elif kind == "u64" or kind == "usize":
print(f"{described.name}: {kind} = {memory.read[u64](address)}")
elif kind == "u32" or kind == "char":
print(f"{described.name}: {kind} = {memory.read[u32](address)}")
elif kind == "u16":
print(f"{described.name}: {kind} = {memory.read[u16](address)}")
elif kind == "u8":
print(f"{described.name}: {kind} = {memory.read[u8](address)}")
elif kind == "bool":
print(f"{described.name}: {kind} = {memory.read[bool](address)}")
elif kind == "f64":
print(f"{described.name}: {kind} = {memory.read[f64](address)}")
elif kind == "f32":
print(f"{described.name}: {kind} = {memory.read[f32](address)}")
elif kind == "str":
let text = memory.read[str](address)
print(f"{described.name}: {kind} = \"{text}\"")
elif kind == "c.str":
let text = memory.read[c.str](address)
print(f"{described.name}: {kind} = \"{(str)text}\"")
elif ends_with(kind, "*") or ends_with(kind, "*?"):
print(f"{described.name}: {kind} = 0x{memory.read[usize](address):x}")
else:
var shown: usize = (usize)described.size
if shown > 32:
shown = 32
var text: u8[160]
var used: usize = 0
var i: usize = 0
while i < shown:
let byte = memory.read[u8]((void*)((usize)address + i))
let digits = "0123456789abcdef".bytes
text[used] = digits[(usize)(byte >> 4)]
text[used + 1] = digits[(usize)(byte & 15)]
text[used + 2] = ' '
used += 3
i += 1
print(f"{described.name}: {kind} = {described.size} bytes {(str)text[..<used]}")
func first_word(line: str) -> str:
var i: usize = 0
while i < line.length and line.bytes[i] != ' ':
i += 1
return (str)line.bytes[..<i]
func trim(text: str) -> str:
var start: usize = 0
var end = text.length
while start < end and text.bytes[start] == ' ':
start += 1
while end > start and (text.bytes[end - 1] == ' ' or text.bytes[end - 1] == '\r'):
end -= 1
return (str)text.bytes[start..<end]
## One line from standard input, without its newline; none at the end of input.
func read_line() -> str?:
var used: usize = 0
while used < line_bytes.length:
var byte: u8[1]
let n = c_read(0, &byte[0], 1)
if n <= 0:
if used == 0:
return none
break
if byte[0] == '\n':
break
line_bytes[used] = byte[0]
used += 1
return (str)line_bytes[..<used]
"""
# mark: By index ==============================================================================
## The text of fragment `index`, as `paths` names it.
pub func text(index: usize) -> str:
match index:
0: return text_0
1: return text_1
2: return text_2
3: return text_3
4: return text_4
5: return text_5
6: return text_6
7: return text_7
8: return text_8
9: return text_9
10: return text_10
11: return text_11
12: return text_12
13: return text_13
14: return text_14
15: return text_15
16: return text_16
17: return text_17
18: return text_18
19: return text_19
20: return text_20
21: return text_21
22: return text_22
23: return text_23
24: return text_24
25: return text_25
26: return text_26
27: return text_27
28: return text_28
29: return text_29
30: return text_30
31: return text_31
32: return text_32
33: return text_33
34: return text_34
35: return text_35
36: return text_36
37: return text_37
38: return text_38
39: return text_39
40: return text_40
41: return text_41
42: return text_42
43: return text_43
44: return text_44
45: return text_45
46: return text_46
47: return text_47
_: return ""