fix(std): render integer formatter extremes — i64::MIN and unsigned all-ones [F0.8]
Resolves issue 0090. The `{}` integer formatter mis-rendered both ends of
the 64-bit range:
- `int_to_string` computed the magnitude as `0 - n`, which overflows for
`s64::MIN` (its magnitude is unrepresentable as a positive s64) — the
value stayed negative, the digit loop ran zero times, so only `-`
printed. It now extracts digits straight from `n` (per-digit
`|n % 10|`, `n` truncating toward zero), never negating MIN.
- `any_to_string`'s `case int:` formatted every integer as s64, so a u64
all-ones value printed as `-1`. There was no `uint` type-category to
distinguish signedness. Added an additive `type_is_unsigned(T)`
reflection builtin (static fold + dynamic interp/LLVM paths, mirroring
`type_name`), backed by the new `TypeTable.isUnsignedInt` predicate, and
a `uint_to_string` formatter (unsigned decimal via long-division over
four 16-bit limbs). `case int:` routes through `type_is_unsigned(type)`.
The 16-bit-limb split is factored into a shared `decompose_u16x4`, now
reused by `int_to_hex_string` (no second unsigned-math routine).
Regression: examples/0046-basic-int-formatter-extremes pins both extremes
plus a width spread; unit tests cover `isUnsignedInt`. Docs (specs.md
representation note, readme std API) updated for unsigned/extreme `{}`
behavior. IR snapshots refreshed for the two new std functions.
This commit is contained in:
@@ -60,6 +60,36 @@ pub const Reflection = struct {
|
||||
return global;
|
||||
}
|
||||
|
||||
/// Lazy global `[N x i1]` indexed by `TypeId.index()`: 1 where the type is
|
||||
/// an unsigned integer. Built on the first dynamic `type_is_unsigned(t)`
|
||||
/// call site; the runtime arm GEPs in at the boxed TypeId and loads the bit.
|
||||
/// Derives every entry from `TypeTable.isUnsignedInt` — the single
|
||||
/// signedness source-of-truth, so no per-index magic lives in the emitter.
|
||||
pub fn getOrBuildTypeIsUnsignedArray(self: Reflection) c.LLVMValueRef {
|
||||
if (self.e.type_is_unsigned_array) |g| return g;
|
||||
|
||||
const n: u32 = @intCast(self.e.ir_mod.types.infos.items.len);
|
||||
var field_vals = std.ArrayList(c.LLVMValueRef).empty;
|
||||
defer field_vals.deinit(self.e.alloc);
|
||||
var i: u32 = 0;
|
||||
while (i < n) : (i += 1) {
|
||||
const tid = TypeId.fromIndex(i);
|
||||
const bit: u64 = if (self.e.ir_mod.types.isUnsignedInt(tid)) 1 else 0;
|
||||
field_vals.append(self.e.alloc, c.LLVMConstInt(self.e.cached_i1, bit, 0)) catch unreachable;
|
||||
}
|
||||
|
||||
const arr_ty = c.LLVMArrayType(self.e.cached_i1, n);
|
||||
const arr_init = c.LLVMConstArray(self.e.cached_i1, field_vals.items.ptr, n);
|
||||
const global = c.LLVMAddGlobal(self.e.llvm_module, arr_ty, "__sx_type_is_unsigned");
|
||||
c.LLVMSetInitializer(global, arr_init);
|
||||
c.LLVMSetGlobalConstant(global, 1);
|
||||
c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);
|
||||
|
||||
self.e.type_is_unsigned_array = global;
|
||||
self.e.type_is_unsigned_array_len = n;
|
||||
return global;
|
||||
}
|
||||
|
||||
/// Build (or return cached) a global constant array of {ptr, i64} string values
|
||||
/// for the field names of a struct type.
|
||||
pub fn getOrBuildFieldNameArray(self: Reflection, struct_type: TypeId) c.LLVMValueRef {
|
||||
|
||||
Reference in New Issue
Block a user