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:
agra
2026-06-05 09:05:37 +03:00
parent 5ef74a15f3
commit 64f77e9779
54 changed files with 36282 additions and 30161 deletions

View File

@@ -1064,6 +1064,27 @@ pub const Ops = struct {
const eq_res = c.LLVMBuildICmp(self.e.builder, c.LLVMIntEQ, a, b, "te.eq");
self.e.mapRef(eq_res);
},
.type_is_unsigned => {
// Dynamic `type_is_unsigned(t)`: extract the TypeId from
// the arg (Any-boxed Type → value field, or bare i64
// index), GEP into the `__sx_type_is_unsigned` table, load
// the i1. Mirrors the `type_name` runtime lookup.
const arg_ref = bi.args[0];
const arg_val = self.e.resolveRef(arg_ref);
const tid_idx = switch (self.e.reflectArgRepr(arg_ref)) {
.unresolved => @panic("type_is_unsigned: reflection arg IR-type unresolved — a type-resolution failure reached LLVM emission without a diagnostic"),
.boxed => c.LLVMBuildExtractValue(self.e.builder, arg_val, 1, "tiu.tid"),
.bare => arg_val,
};
const arr_global = self.e.reflection().getOrBuildTypeIsUnsignedArray();
const arr_len = self.e.type_is_unsigned_array_len;
const arr_ty = c.LLVMArrayType(self.e.cached_i1, arr_len);
const zero = c.LLVMConstInt(self.e.cached_i64, 0, 0);
var indices = [2]c.LLVMValueRef{ zero, tid_idx };
const gep = c.LLVMBuildInBoundsGEP2(self.e.builder, arr_ty, arr_global, &indices, 2, "tiu.gep");
const result = c.LLVMBuildLoad2(self.e.builder, self.e.cached_i1, gep, "tiu.load");
self.e.mapRef(result);
},
.has_impl => {
// Runtime has_impl needs a protocol-map
// snapshot — not wired yet. Silent false for

View File

@@ -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 {