lang: array-typed '::' consts as immutable globals (PLAN-CONST-AGG step 1)
K : [4]s64 : .[...] and the untyped A :: .[1, 2, 3] register as is_const globals: one storage, reads GEP it, dead-global elimination drops unused ones, source-aware reads come free via selectGlobalAuthor. - registerConstArrayGlobal (scanDecls pass 2): typed via the annotation (array-ness + dimension/count checked), untyped via element-type unification — all ints s64; ANY float promotes the element type to f64 with ints converting exactly; bool/string homogeneous; a non-numeric mix or non-inferable element asks for an annotation. - constExprValue converts int elements into float destinations exactly (the int+float promotion rule, element-wise). - emitGlobals marks is_const globals LLVMSetGlobalConstant — also flips the comptime-backed #run globals and __sx_default_context to 'constant' (37 pinned IR snapshots regenerated; runtime unchanged). - Element shapes: nested arrays, struct elements, strings, bools. Non-constant elements / dim mismatch / mixed types diagnose loudly. Examples: 0177 (feature matrix incl. @K reads through *[4]s64 — needs the 0117 fix), 1159/1160/1161 (diagnostics), 0837 repointed to values.
This commit is contained in:
@@ -962,6 +962,8 @@ pub const LLVMEmitter = struct {
|
||||
c.LLVMSetInitializer(llvm_global, c.LLVMConstNull(llvm_ty));
|
||||
}
|
||||
|
||||
if (global.is_const) c.LLVMSetGlobalConstant(llvm_global, 1);
|
||||
|
||||
self.global_map.put(@intCast(i), llvm_global) catch {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1587,6 +1587,8 @@ pub const Lowering = struct {
|
||||
pub const fnPtrTypeWantsCtx = lower_decl.fnPtrTypeWantsCtx;
|
||||
pub const scanDecls = lower_decl.scanDecls;
|
||||
pub const registerTypedModuleConst = lower_decl.registerTypedModuleConst;
|
||||
pub const registerConstArrayGlobal = lower_decl.registerConstArrayGlobal;
|
||||
pub const inferConstArrayType = lower_decl.inferConstArrayType;
|
||||
pub const typedConstInitFits = lower_decl.typedConstInitFits;
|
||||
pub const constExprInitFits = lower_decl.constExprInitFits;
|
||||
pub const registerTopLevelGlobal = lower_decl.registerTopLevelGlobal;
|
||||
|
||||
@@ -50,7 +50,12 @@ pub fn constArrayLiteral(self: *Lowering, elements: []const *const Node, array_t
|
||||
/// expression is not constant-foldable here.
|
||||
pub fn constExprValue(self: *Lowering, expr: *const Node, expected_ty: TypeId) ?inst_mod.ConstantValue {
|
||||
return switch (expr.data) {
|
||||
.int_literal => |il| .{ .int = il.value },
|
||||
// An int element in a FLOAT destination converts exactly (the
|
||||
// int+float promotion rule, element-wise — `[2]f64 : .[1, 2.5]`).
|
||||
.int_literal => |il| if (isFloat(expected_ty))
|
||||
.{ .float = @floatFromInt(il.value) }
|
||||
else
|
||||
.{ .int = il.value },
|
||||
.bool_literal => |bl| .{ .boolean = bl.value },
|
||||
// A float into an INTEGER destination follows the implicit
|
||||
// narrowing rule: an integral float folds to its int, a
|
||||
@@ -71,7 +76,10 @@ pub fn constExprValue(self: *Lowering, expr: *const Node, expected_ty: TypeId) ?
|
||||
.null_literal => .null_val,
|
||||
.unary_op => |uo| switch (uo.op) {
|
||||
.negate => switch (uo.operand.data) {
|
||||
.int_literal => |il| .{ .int = -il.value },
|
||||
.int_literal => |il| if (isFloat(expected_ty))
|
||||
.{ .float = @floatFromInt(-il.value) }
|
||||
else
|
||||
.{ .int = -il.value },
|
||||
.float_literal => |fl| .{ .float = -fl.value },
|
||||
else => null,
|
||||
},
|
||||
|
||||
@@ -778,7 +778,10 @@ pub fn scanDecls(self: *Lowering, decls: []const *const Node) void {
|
||||
self.setCurrentSourceFile(decl.source_file);
|
||||
switch (decl.data) {
|
||||
.var_decl => self.registerTopLevelGlobal(&decl.data.var_decl),
|
||||
.const_decl => |cd| self.registerTypedModuleConst(&cd),
|
||||
.const_decl => |cd| if (cd.value.data == .array_literal)
|
||||
self.registerConstArrayGlobal(&cd)
|
||||
else
|
||||
self.registerTypedModuleConst(&cd),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
@@ -924,6 +927,95 @@ pub fn constExprInitFits(self: *Lowering, init_ty: TypeId, dst_ty: TypeId) bool
|
||||
return init_ty == dst_ty;
|
||||
}
|
||||
|
||||
/// Register an array-typed `::` constant (`K : [4]s64 : .[...]`, or the
|
||||
/// untyped `A :: .[1, 2, 3]`) as an IMMUTABLE module global: one storage,
|
||||
/// reads GEP it, the emitter marks it LLVMSetGlobalConstant, dead-global
|
||||
/// elimination drops it when unused. Source-aware reads come for free via
|
||||
/// `selectGlobalAuthor` (the per-source partition is written here).
|
||||
pub fn registerConstArrayGlobal(self: *Lowering, cd: *const ast.ConstDecl) void {
|
||||
const al = &cd.value.data.array_literal;
|
||||
const arr_ty: TypeId = blk: {
|
||||
if (cd.type_annotation) |ta| {
|
||||
const t = self.resolveType(ta);
|
||||
if (t == .unresolved) return; // annotation already diagnosed
|
||||
if (t.isBuiltin() or self.module.types.get(t) != .array) {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, cd.value.span, "constant '{s}' has an array-literal initializer but its annotation is not an array type", .{cd.name});
|
||||
return;
|
||||
}
|
||||
const dim = self.module.types.get(t).array.length;
|
||||
if (dim != al.elements.len) {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, cd.value.span, "constant '{s}' declares [{d}] elements but its initializer has {d}", .{ cd.name, dim, al.elements.len });
|
||||
return;
|
||||
}
|
||||
break :blk t;
|
||||
}
|
||||
break :blk self.inferConstArrayType(cd.name, al.elements, cd.value.span) orelse return;
|
||||
};
|
||||
const init_val = self.constArrayLiteral(al.elements, arr_ty) orelse {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, cd.value.span, "constant '{s}' must be initialized by compile-time constant elements", .{cd.name});
|
||||
return;
|
||||
};
|
||||
const name_id = self.module.types.internString(cd.name);
|
||||
const gid = self.module.addGlobal(.{
|
||||
.name = name_id,
|
||||
.ty = arr_ty,
|
||||
.init_val = init_val,
|
||||
.is_const = true,
|
||||
});
|
||||
self.putGlobal(self.current_source_file, cd.name, .{ .id = gid, .ty = arr_ty });
|
||||
}
|
||||
|
||||
/// Infer `[N]T` for an untyped array-literal constant. Element types unify:
|
||||
/// all ints → s64; ANY float promotes the element type to f64 (ints convert
|
||||
/// exactly — the int+float promotion rule for consts, element-wise); bool /
|
||||
/// string homogeneous only. A non-numeric mix or a non-inferable element
|
||||
/// shape (nested aggregate, enum literal, named const) asks for an
|
||||
/// annotation rather than guessing.
|
||||
pub fn inferConstArrayType(self: *Lowering, name: []const u8, elements: []const *const Node, span: ast.Span) ?TypeId {
|
||||
if (elements.len == 0) {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, span, "constant '{s}' is an empty array literal — annotate the type (e.g. `{s} : [0]s64 : .[]`)", .{ name, name });
|
||||
return null;
|
||||
}
|
||||
var elem_ty: ?TypeId = null;
|
||||
for (elements) |e| {
|
||||
const leaf: ?TypeId = switch (e.data) {
|
||||
.int_literal => .s64,
|
||||
.float_literal => .f64,
|
||||
.bool_literal => .bool,
|
||||
.string_literal => .string,
|
||||
.unary_op => |uo| if (uo.op == .negate) switch (uo.operand.data) {
|
||||
.int_literal => .s64,
|
||||
.float_literal => .f64,
|
||||
else => null,
|
||||
} else null,
|
||||
else => null,
|
||||
};
|
||||
const lt = leaf orelse {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, e.span, "cannot infer the element type of constant '{s}' from this element — annotate the array type", .{name});
|
||||
return null;
|
||||
};
|
||||
if (elem_ty) |prev| {
|
||||
if (prev == lt) continue;
|
||||
// Numeric mix promotes to the float element type.
|
||||
const numeric_pair = (prev == .s64 and lt == .f64) or (prev == .f64 and lt == .s64);
|
||||
if (numeric_pair) {
|
||||
elem_ty = .f64;
|
||||
continue;
|
||||
}
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, e.span, "constant '{s}' mixes incompatible element types — annotate the array type", .{name});
|
||||
return null;
|
||||
}
|
||||
elem_ty = lt;
|
||||
}
|
||||
return self.module.types.arrayOf(elem_ty.?, @intCast(elements.len));
|
||||
}
|
||||
|
||||
/// Register a top-level mutable global (e.g., `context : Context = ---;`).
|
||||
/// Run AFTER `resolveForwardIdentifierAliases` so a forward identifier alias
|
||||
/// in the type annotation (`A :: B; B :: s32; g : A = 7;`) resolves to its
|
||||
|
||||
Reference in New Issue
Block a user