refactor(backend): extract reflection metadata + trace frames into src/backend/llvm/reflection.zig (A7.2 reflection)

Move the type/field/tag reflection name-array builders and the error-trace Frame
builders out of emit_llvm.zig into a Reflection backend *LLVMEmitter facade
(field `e`). Behavior-preserving relocation — self.* -> self.e.* only.

- src/backend/llvm/reflection.zig (Reflection): getOrBuildTypeNameArray /
  getOrBuildFieldNameArray / getOrBuildTagNameArray (pub) + emitTraceFrame (pub)
  + buildStringConst (private trace helper). The memoized state
  (type_name_array(_len) / field_name_arrays / tag_name_array / frame_str_cache)
  stays on LLVMEmitter; the facade reads/writes via self.e.*.
- Routed the 5 call sites through a new reflection() accessor (type_name /
  field_name / error_tag_name builtins, emitFailableMainRet's tag-name lookup,
  and the .trace_frame push).
- Kept in emit_llvm.zig per the A6.1 "emission-heavy stays" precedent:
  getFrameStructType (composite-type getter, widened to pub — emitTraceFrame calls
  it back), emitFieldValueGet (field-value reflection EMISSION, not an array
  builder), emitFailableMainRet. getStringStructType/getAnyStructType already pub.
- No reflection-array layout, trace-Frame field order, or linkage change.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0 (reflection
anchors 0030/0118/0517/0520 + trace anchors 1024/1025/1026 all ok, no churn).
This commit is contained in:
agra
2026-06-03 09:29:27 +03:00
parent f92a743c85
commit 46b874074b
2 changed files with 216 additions and 194 deletions

View File

@@ -0,0 +1,205 @@
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;
}
/// 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;
const info = self.e.ir_mod.types.get(struct_type);
// Collect name StringIds from struct fields, union fields, or enum variants
var name_ids = std.ArrayList(StringId).empty;
defer name_ids.deinit(self.e.alloc);
switch (info) {
.@"struct" => |s| {
for (s.fields) |f| name_ids.append(self.e.alloc, f.name) catch unreachable;
},
.@"union" => |u| {
for (u.fields) |f| name_ids.append(self.e.alloc, f.name) catch unreachable;
},
.tagged_union => |u| {
for (u.fields) |f| name_ids.append(self.e.alloc, f.name) catch unreachable;
},
.@"enum" => |e| {
for (e.variants) |v| name_ids.append(self.e.alloc, v) catch unreachable;
},
else => {},
}
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);
}
};

View File

@@ -13,6 +13,7 @@ const errors = @import("../errors.zig");
const llvm_types = @import("../backend/llvm/types.zig");
const llvm_abi = @import("../backend/llvm/abi.zig");
const llvm_debug = @import("../backend/llvm/debug.zig");
const llvm_reflection = @import("../backend/llvm/reflection.zig");
const ir_inst = @import("inst.zig");
const Ref = ir_inst.Ref;
const Span = ir_inst.Span;
@@ -1880,7 +1881,7 @@ pub const LLVMEmitter = struct {
self.advanceRefCounter();
},
.trace_frame => {
self.mapRef(self.emitTraceFrame(instruction));
self.mapRef(self.reflection().emitTraceFrame(instruction));
},
.trace_resolve => |u| {
// The operand is a `Frame*` stamped in by `.trace_frame` (as
@@ -3264,7 +3265,7 @@ pub const LLVMEmitter = struct {
// Bare i64 (TypeId index).
break :blk arg_val;
};
const arr_global = self.getOrBuildTypeNameArray();
const arr_global = self.reflection().getOrBuildTypeNameArray();
const arr_len = self.type_name_array_len;
const string_ty = self.getStringStructType();
const arr_ty = c.LLVMArrayType(string_ty, arr_len);
@@ -3543,7 +3544,7 @@ pub const LLVMEmitter = struct {
// ── Reflection ops ──────────────────────────────────────
.field_name_get => |fr| {
// Build global string array for this struct's field names, then GEP at runtime index
const global = self.getOrBuildFieldNameArray(fr.struct_type);
const global = self.reflection().getOrBuildFieldNameArray(fr.struct_type);
const idx = self.resolveRef(fr.index);
const string_ty = self.getStringStructType();
// Get struct field count for array type
@@ -3571,7 +3572,7 @@ pub const LLVMEmitter = struct {
// the runtime tag id (the error-set value, a u32). Out-of-range
// ids can't occur — ids come from the same registry the table
// is built from — so no bounds branch is needed.
const global = self.getOrBuildTagNameArray();
const global = self.reflection().getOrBuildTagNameArray();
const tag_raw = self.resolveRef(u.operand);
const idx = c.LLVMBuildZExt(self.builder, tag_raw, self.cached_i64, "etn.idx");
const string_ty = self.getStringStructType();
@@ -4428,6 +4429,10 @@ pub const LLVMEmitter = struct {
return .{ .e = self };
}
fn reflection(self: *LLVMEmitter) llvm_reflection.Reflection {
return .{ .e = self };
}
/// IR-type → LLVM-type lowering lives in `backend/llvm/types.zig`
/// (`TypeLowering`). This stays the facade entry point (~97 callers).
pub fn toLLVMType(self: *LLVMEmitter, ty: TypeId) c.LLVMTypeRef {
@@ -4469,7 +4474,7 @@ pub const LLVMEmitter = struct {
/// The compiled error-trace `Frame` type: `{ string, i32, i32, string }`.
/// Layout must match `Frame` in `trace.sx` and `SxFrame` in `sx_trace.c`.
fn getFrameStructType(self: *LLVMEmitter) c.LLVMTypeRef {
pub fn getFrameStructType(self: *LLVMEmitter) c.LLVMTypeRef {
if (self.frame_struct_type) |t| return t;
const str_ty = self.getStringStructType();
var field_types = [_]c.LLVMTypeRef{
@@ -4483,52 +4488,6 @@ pub const LLVMEmitter = struct {
return self.frame_struct_type.?;
}
/// 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: *LLVMEmitter, s: []const u8) c.LLVMValueRef {
if (self.frame_str_cache.get(s)) |v| return v;
const str_z = self.alloc.dupeZ(u8, s) catch unreachable;
defer self.alloc.free(str_z);
const data = c.LLVMAddGlobal(self.llvm_module, c.LLVMArrayType(self.cached_i8, @intCast(s.len + 1)), "frame.str");
c.LLVMSetInitializer(data, c.LLVMConstStringInContext(self.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.cached_i64, s.len, 0) };
const str_const = c.LLVMConstNamedStruct(self.getStringStructType(), &fields, 2);
const key = self.alloc.dupe(u8, s) catch return str_const;
self.frame_str_cache.put(key, str_const) catch self.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.
fn emitTraceFrame(self: *LLVMEmitter, instruction: *const Inst) c.LLVMValueRef {
const file = std.fs.path.basename(self.current_func_file);
const src = self.sourceForFile(self.current_func_file);
const loc = errors.SourceLoc.compute(src, instruction.span.start);
const func_name = self.ir_mod.types.getString(self.ir_mod.functions.items[self.current_func_idx].name);
var fields = [_]c.LLVMValueRef{
self.buildStringConst(file),
c.LLVMConstInt(self.cached_i32, loc.line, 0),
c.LLVMConstInt(self.cached_i32, loc.col, 0),
self.buildStringConst(func_name),
self.buildStringConst(errors.lineAt(src, instruction.span.start)),
};
const frame_ty = self.getFrameStructType();
const frame_const = c.LLVMConstNamedStruct(frame_ty, &fields, 5);
const g = c.LLVMAddGlobal(self.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.cached_i64);
}
pub fn getAnyStructType(self: *LLVMEmitter) c.LLVMTypeRef {
if (self.any_struct_type) |t| return t;
var field_types = [_]c.LLVMTypeRef{
@@ -4671,110 +4630,6 @@ pub const LLVMEmitter = struct {
self.mapRef(result);
}
// ── Reflection emission helpers ────────────────────────────────
/// Build (or return cached) a global constant array of {ptr, i64}
/// string values indexed by `TypeId.index()`. Lets the dynamic
/// `type_name(t)` builtin look up the type's display name at
/// runtime — `gep arr[tid]; load string`. The array's length is
/// the current `infos.items.len`; new types interned after this
/// is built fall outside the array (the gep would OOB), so
/// callers must build LAZILY after all types are registered.
fn getOrBuildTypeNameArray(self: *LLVMEmitter) c.LLVMValueRef {
if (self.type_name_array) |g| return g;
const n: u32 = @intCast(self.ir_mod.types.infos.items.len);
const string_ty = self.getStringStructType();
var field_vals = std.ArrayList(c.LLVMValueRef).empty;
defer field_vals.deinit(self.alloc);
var i: u32 = 0;
while (i < n) : (i += 1) {
const tid = @import("types.zig").TypeId.fromIndex(i);
const name_str = self.ir_mod.types.formatTypeName(self.alloc, tid);
const str_z = self.alloc.dupeZ(u8, name_str) catch unreachable;
defer self.alloc.free(str_z);
const global_str = c.LLVMAddGlobal(self.llvm_module, c.LLVMArrayType(self.cached_i8, @intCast(name_str.len + 1)), "tn.str");
c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.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.cached_i64, name_str.len, 0);
var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
const const_struct = c.LLVMConstStructInContext(self.context, &struct_fields, 2, 0);
field_vals.append(self.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.llvm_module, arr_ty, "__sx_type_names");
c.LLVMSetInitializer(global, arr_init);
c.LLVMSetGlobalConstant(global, 1);
c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);
self.type_name_array = global;
self.type_name_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.
fn getOrBuildFieldNameArray(self: *LLVMEmitter, struct_type: TypeId) c.LLVMValueRef {
if (self.field_name_arrays.get(struct_type.index())) |g| return g;
const info = self.ir_mod.types.get(struct_type);
// Collect name StringIds from struct fields, union fields, or enum variants
var name_ids = std.ArrayList(StringId).empty;
defer name_ids.deinit(self.alloc);
switch (info) {
.@"struct" => |s| {
for (s.fields) |f| name_ids.append(self.alloc, f.name) catch unreachable;
},
.@"union" => |u| {
for (u.fields) |f| name_ids.append(self.alloc, f.name) catch unreachable;
},
.tagged_union => |u| {
for (u.fields) |f| name_ids.append(self.alloc, f.name) catch unreachable;
},
.@"enum" => |e| {
for (e.variants) |v| name_ids.append(self.alloc, v) catch unreachable;
},
else => {},
}
const string_ty = self.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.alloc);
for (name_ids.items) |name_id| {
const name_str = self.ir_mod.types.getString(name_id);
const str_z = self.alloc.dupeZ(u8, name_str) catch unreachable;
defer self.alloc.free(str_z);
const global_str = c.LLVMAddGlobal(self.llvm_module, c.LLVMArrayType(self.cached_i8, @intCast(name_str.len + 1)), "fld.str");
c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.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.cached_i64, name_str.len, 0);
var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
const const_struct = c.LLVMConstStructInContext(self.context, &struct_fields, 2, 0);
field_vals.append(self.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.llvm_module, array_ty, "field_names");
c.LLVMSetInitializer(global, array_init);
c.LLVMSetGlobalConstant(global, 1);
c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);
self.field_name_arrays.put(struct_type.index(), global) catch unreachable;
return global;
}
/// Failable main entry-point wrapper (ERR E4.2). At the LLVM level main
/// returns i32. `tag_val` is the u32 error tag (0 = "no error"); `value` is
/// the integer value slot for a value-carrying `-> (int, !)` main, or null
@@ -4799,7 +4654,7 @@ pub const LLVMEmitter = struct {
// Error: resolve the tag name, report to stderr, exit 1.
c.LLVMPositionBuilderAtEnd(self.builder, err_bb);
const global = self.getOrBuildTagNameArray();
const global = self.reflection().getOrBuildTagNameArray();
const idx = c.LLVMBuildZExt(self.builder, tag_i32, self.cached_i64, "main.tagidx");
const string_ty = self.getStringStructType();
const n: u32 = @intCast(self.ir_mod.types.tags.names.items.len);
@@ -4822,44 +4677,6 @@ pub const LLVMEmitter = struct {
_ = c.LLVMBuildRet(self.builder, c.LLVMConstInt(self.cached_i32, 1, 0));
}
/// 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.
fn getOrBuildTagNameArray(self: *LLVMEmitter) c.LLVMValueRef {
if (self.tag_name_array) |g| return g;
const string_ty = self.getStringStructType();
const names = self.ir_mod.types.tags.names.items;
var field_vals = std.ArrayList(c.LLVMValueRef).empty;
defer field_vals.deinit(self.alloc);
for (names) |name_str| {
const str_z = self.alloc.dupeZ(u8, name_str) catch unreachable;
defer self.alloc.free(str_z);
const global_str = c.LLVMAddGlobal(self.llvm_module, c.LLVMArrayType(self.cached_i8, @intCast(name_str.len + 1)), "tag.str");
c.LLVMSetInitializer(global_str, c.LLVMConstStringInContext(self.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.cached_i64, name_str.len, 0);
var struct_fields = [2]c.LLVMValueRef{ global_str, len_val };
const const_struct = c.LLVMConstStructInContext(self.context, &struct_fields, 2, 0);
field_vals.append(self.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.llvm_module, array_ty, "tag_names");
c.LLVMSetInitializer(global, array_init);
c.LLVMSetGlobalConstant(global, 1);
c.LLVMSetLinkage(global, c.LLVMPrivateLinkage);
self.tag_name_array = global;
return global;
}
/// Emit field_value_get: switch on runtime index, each case extracts a field and boxes it as Any.
fn emitFieldValueGet(self: *LLVMEmitter, fr: ir_inst.FieldReflect, func_idx: u32) void {
const base_val = self.resolveRef(fr.base);