Files
sx/src/backend/llvm/reflection.zig
agra 8ac6c573e8 fix: comptime field reflection on tuples/arrays/vectors (issue 0195)
`field_count` / `field_name` were broken on every non-struct/enum aggregate:
`field_count(Tuple(i64, bool))` silently returned 0 (a missing `.tuple` arm in
the count switches), and `field_name(tuple/array/vector, i)` SEGFAULTED — the
LLVM backend built a zero-length `[0 x string]` name array for those kinds while
sizing the runtime GEP at the (often non-zero) member count, so the indexed load
ran past the array.

Root cause was three+ parallel switches that each had to know how to count an
aggregate's members, and disagreed: `field_count` lowering and `memberCount` had
struct/union/tagged_union/enum/array/vector but no `.tuple`; the backend's
`field_name_get` build + GEP sizing had neither `.tuple` nor `.array`/`.vector`.

Fix:
- add the `.tuple` arm to `field_count` lowering (src/ir/lower/call.zig) and
  `TypeTable.memberCount` (src/ir/types.zig; this also backs the COMPILER-API
  `type_field_count` VM reader).
- unify the LLVM backend onto the single source of truth: both
  `getOrBuildFieldNameArray` (reflection.zig) and `emitFieldNameGet`'s GEP sizing
  (ops.zig) now derive from `memberCount` / `memberName`, so the name-array
  length and the GEP array type can never diverge again — for any kind. A member
  with no name (positional-tuple / array / vector element) reflects as "" (one
  slot per member, always in-bounds); named-tuple elements recover their labels.

The array/vector clone was surfaced by adversarial review of the tuple-only fix.

Regression: examples/comptime/0646-comptime-field-reflect-tuple-array.sx exercises
field_count/field_name/field_type over struct, enum, positional + named tuple,
array, and vector. Full suite green (818/0). Unblocks the `race` synthesis, which
must reflect a named tuple's labels + element types.
2026-06-26 12:28:09 +03:00

233 lines
12 KiB
Zig

const std = @import("std");
const llvm = @import("../../llvm_api.zig");
const c = llvm.c;
const errors = @import("../../errors.zig");
const emit = @import("../../ir/emit_llvm.zig");
const ir_inst = @import("../../ir/inst.zig");
const ir_types = @import("../../ir/types.zig");
const LLVMEmitter = emit.LLVMEmitter;
const Inst = ir_inst.Inst;
const TypeId = ir_types.TypeId;
const StringId = ir_types.StringId;
/// Reflection metadata + trace-frame emission (architecture phase A7.2),
/// extracted from `LLVMEmitter`. A backend `*LLVMEmitter` facade (field `e`):
/// the type/field/tag reflection NAME-ARRAY builders (memoized into
/// `type_name_array`/`field_name_arrays`/`tag_name_array` on `LLVMEmitter`) and
/// the error-trace `Frame` builders. Reads cached LLVM handles / the IR type
/// table / the module via `self.e.*`; the memoizing composite getters
/// (`getStringStructType`/`getFrameStructType`) + `emitFieldValueGet` stay on
/// `LLVMEmitter`. Entry points are reached via `self.reflection()`.
pub const Reflection = struct {
e: *LLVMEmitter,
/// Lazy global `[N x string]` indexed by `TypeId.index()`, holding each
/// type's display name. Built on the first dynamic `type_name(t)` call site.
pub fn getOrBuildTypeNameArray(self: Reflection) c.LLVMValueRef {
if (self.e.type_name_array) |g| return g;
const n: u32 = @intCast(self.e.ir_mod.types.infos.items.len);
const string_ty = self.e.getStringStructType();
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 name_str = self.e.ir_mod.types.formatTypeName(self.e.alloc, tid);
const str_z = self.e.alloc.dupeZ(u8, name_str) catch unreachable;
defer self.e.alloc.free(str_z);
const global_str = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(name_str.len + 1)), "tn.str");
c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(name_str.len + 1), 1));
c.LLVMSetGlobalConstant(global_str, 1);
c.LLVMSetLinkage(global_str, c.LLVMPrivateLinkage);
const len_val = c.LLVMConstInt(self.e.cached_i64, name_str.len, 0);
var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
const const_struct = c.LLVMConstStructInContext(self.e.context, &struct_fields, 2, 0);
field_vals.append(self.e.alloc, const_struct) catch unreachable;
}
const arr_ty = c.LLVMArrayType(string_ty, n);
const arr_init = c.LLVMConstArray(string_ty, field_vals.items.ptr, n);
const global = c.LLVMAddGlobal(self.e.llvm_module, arr_ty, "__sx_type_names");
c.LLVMSetInitializer(global, arr_init);
c.LLVMSetGlobalConstant(global, 1);
c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);
self.e.type_name_array = global;
self.e.type_name_array_len = n;
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 {
if (self.e.field_name_arrays.get(struct_type.index())) |g| return g;
// Collect one name StringId per member, driven by the SINGLE source of
// truth `memberCount`/`memberName` (types.zig) — NOT a per-kind switch
// here. This guarantees the array length always matches `emitFieldNameGet`'s
// GEP sizing (which also derives from `memberCount`), so a kind covered by
// one but not the other can never reappear (that mismatch was issue 0195:
// tuples/arrays counted N members but built a zero-length name array → an
// out-of-bounds GEP → segfault). A member with no name (positional tuple
// element, array/vector element) yields `.empty` → "", keeping one slot
// per member so `field_name(T, i)` is always in-bounds.
const n_members: i64 = self.e.ir_mod.types.memberCount(struct_type) orelse 0;
var name_ids = std.ArrayList(StringId).empty;
defer name_ids.deinit(self.e.alloc);
var mi: i64 = 0;
while (mi < n_members) : (mi += 1) {
const nid: StringId = self.e.ir_mod.types.memberName(struct_type, mi) orelse .empty;
name_ids.append(self.e.alloc, nid) catch unreachable;
}
const string_ty = self.e.getStringStructType();
const n: u32 = @intCast(name_ids.items.len);
// Build constant initializer: [N x {ptr, i64}]
var field_vals = std.ArrayList(c.LLVMValueRef).empty;
defer field_vals.deinit(self.e.alloc);
for (name_ids.items) |name_id| {
const name_str = self.e.ir_mod.types.getString(name_id);
const str_z = self.e.alloc.dupeZ(u8, name_str) catch unreachable;
defer self.e.alloc.free(str_z);
const global_str = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(name_str.len + 1)), "fld.str");
c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(name_str.len + 1), 1));
c.LLVMSetGlobalConstant(global_str, 1);
c.LLVMSetLinkage(global_str, c.LLVMPrivateLinkage);
// Build fat pointer {ptr, len} as constant struct
const len_val = c.LLVMConstInt(self.e.cached_i64, name_str.len, 0);
var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
const const_struct = c.LLVMConstStructInContext(self.e.context, &struct_fields, 2, 0);
field_vals.append(self.e.alloc, const_struct) catch unreachable;
}
// Create global array [N x {ptr, i64}]
const array_ty = c.LLVMArrayType(string_ty, n);
const array_init = c.LLVMConstArray(string_ty, field_vals.items.ptr, n);
const global = c.LLVMAddGlobal(self.e.llvm_module, array_ty, "field_names");
c.LLVMSetInitializer(global, array_init);
c.LLVMSetGlobalConstant(global, 1);
c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);
self.e.field_name_arrays.put(struct_type.index(), global) catch unreachable;
return global;
}
/// The always-linked tag-name table: a `[N x {ptr, i64}]` global of tag
/// names indexed by global tag id (the `TagRegistry` namespace; slot 0 is
/// the reserved "" no-error name). `error_tag_name_get` GEPs into it at the
/// runtime tag id. Built once per module. Always emitted (not trace-gated)
/// so `{}` interpolation of an error tag works even in release builds.
pub fn getOrBuildTagNameArray(self: Reflection) c.LLVMValueRef {
if (self.e.tag_name_array) |g| return g;
const string_ty = self.e.getStringStructType();
const names = self.e.ir_mod.types.tags.names.items;
var field_vals = std.ArrayList(c.LLVMValueRef).empty;
defer field_vals.deinit(self.e.alloc);
for (names) |name_str| {
const str_z = self.e.alloc.dupeZ(u8, name_str) catch unreachable;
defer self.e.alloc.free(str_z);
const global_str = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(name_str.len + 1)), "tag.str");
c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(name_str.len + 1), 1));
c.LLVMSetGlobalConstant(global_str, 1);
c.LLVMSetLinkage(global_str, c.LLVMPrivateLinkage);
const len_val = c.LLVMConstInt(self.e.cached_i64, name_str.len, 0);
var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
const const_struct = c.LLVMConstStructInContext(self.e.context, &struct_fields, 2, 0);
field_vals.append(self.e.alloc, const_struct) catch unreachable;
}
const n: u32 = @intCast(names.len);
const array_ty = c.LLVMArrayType(string_ty, n);
const array_init = c.LLVMConstArray(string_ty, field_vals.items.ptr, n);
const global = c.LLVMAddGlobal(self.e.llvm_module, array_ty, "tag_names");
c.LLVMSetInitializer(global, array_init);
c.LLVMSetGlobalConstant(global, 1);
c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);
self.e.tag_name_array = global;
return global;
}
/// An interned constant sx `string` (`{ ptr, i64 }`) of the cached string
/// struct type, backed by a private NUL-terminated data global. Cached by
/// content so a path/name shared by many push sites is emitted once.
fn buildStringConst(self: Reflection, s: []const u8) c.LLVMValueRef {
if (self.e.frame_str_cache.get(s)) |v| return v;
const str_z = self.e.alloc.dupeZ(u8, s) catch unreachable;
defer self.e.alloc.free(str_z);
const data = c.LLVMAddGlobal(self.e.llvm_module, c.LLVMArrayType(self.e.cached_i8, @intCast(s.len + 1)), "frame.str");
c.LLVMSetInitializer(data, c.LLVMConstStringInContext(self.e.context, str_z.ptr, @intCast(s.len + 1), 1));
c.LLVMSetGlobalConstant(data, 1);
c.LLVMSetLinkage(data, c.LLVMPrivateLinkage);
c.LLVMSetUnnamedAddress(data, c.LLVMGlobalUnnamedAddr);
var fields = [_]c.LLVMValueRef{ data, c.LLVMConstInt(self.e.cached_i64, s.len, 0) };
const str_const = c.LLVMConstNamedStruct(self.e.getStringStructType(), &fields, 2);
const key = self.e.alloc.dupe(u8, s) catch return str_const;
self.e.frame_str_cache.put(key, str_const) catch self.e.alloc.free(key);
return str_const;
}
/// Build the interned `Frame` global for a `.trace_frame` push site and
/// return its address as `i64` (the value `sx_trace_push` stores). Resolves
/// the instruction's span + current function to `{file,line,col,func}`. The
/// file is shown as its basename so trace output is machine-independent
/// (the harness passes absolute paths); full paths live in DWARF.
pub fn emitTraceFrame(self: Reflection, instruction: *const Inst) c.LLVMValueRef {
const file = std.fs.path.basename(self.e.current_func_file);
const src = self.e.sourceForFile(self.e.current_func_file);
const loc = errors.SourceLoc.compute(src, instruction.span.start);
const func_name = self.e.ir_mod.types.getString(self.e.ir_mod.functions.items[self.e.current_func_idx].name);
var fields = [_]c.LLVMValueRef{
self.buildStringConst(file),
c.LLVMConstInt(self.e.cached_i32, loc.line, 0),
c.LLVMConstInt(self.e.cached_i32, loc.col, 0),
self.buildStringConst(func_name),
self.buildStringConst(errors.lineAt(src, instruction.span.start)),
};
const frame_ty = self.e.getFrameStructType();
const frame_const = c.LLVMConstNamedStruct(frame_ty, &fields, 5);
const g = c.LLVMAddGlobal(self.e.llvm_module, frame_ty, "trace.frame");
c.LLVMSetInitializer(g, frame_const);
c.LLVMSetGlobalConstant(g, 1);
c.LLVMSetLinkage(g, c.LLVMPrivateLinkage);
return c.LLVMConstPtrToInt(g, self.e.cached_i64);
}
};