From 818fbd9c567b907031c44961e4299ce1f9059be6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 24 May 2022 00:10:56 -0700 Subject: stage2: string literal interning This is a temporary addition to stage2 in order to match stage1 behavior, however the end-game once the lang spec is settled will be to use a global InternPool for comptime memoized objects, making this behavior consistent across all types, not only string literals. Or, we might decide to not guarantee string literals to have equal comptime pointers, in which case this commit can be reverted. --- src/codegen.zig | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/codegen.zig') diff --git a/src/codegen.zig b/src/codegen.zig index def69d952f..bd556baa5e 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -203,11 +203,23 @@ pub fn generateSymbol( }, .Array => switch (typed_value.val.tag()) { .bytes => { - const payload = typed_value.val.castTag(.bytes).?; + const bytes = typed_value.val.castTag(.bytes).?.data; const len = @intCast(usize, typed_value.ty.arrayLenIncludingSentinel()); // The bytes payload already includes the sentinel, if any try code.ensureUnusedCapacity(len); - code.appendSliceAssumeCapacity(payload.data[0..len]); + code.appendSliceAssumeCapacity(bytes[0..len]); + return Result{ .appended = {} }; + }, + .str_lit => { + const str_lit = typed_value.val.castTag(.str_lit).?.data; + const mod = bin_file.options.module.?; + const bytes = mod.string_literal_bytes.items[str_lit.index..][0..str_lit.len]; + try code.ensureUnusedCapacity(bytes.len + 1); + code.appendSliceAssumeCapacity(bytes); + if (typed_value.ty.sentinel()) |sent_val| { + const byte = @intCast(u8, sent_val.toUnsignedInt(target)); + code.appendAssumeCapacity(byte); + } return Result{ .appended = {} }; }, .aggregate => { -- cgit v1.2.3