aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/llvm.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-05-24 15:32:12 -0400
committerGitHub <noreply@github.com>2022-05-24 15:32:12 -0400
commit60f0acd9b9c2bced47ba1c214460f34b73738f95 (patch)
tree7cc1ba371eab54fb84d5f56a67c3dab0d035bf7a /src/codegen/llvm.zig
parent8171972cbb477672ee1a99d953df4aaecb744a0c (diff)
parent953e2778d4402cf85b99061ef9bdb89aa3e5f2db (diff)
downloadzig-60f0acd9b9c2bced47ba1c214460f34b73738f95.tar.gz
zig-60f0acd9b9c2bced47ba1c214460f34b73738f95.zip
Merge pull request #11706 from ziglang/string-literal-interning
stage2: string literal interning
Diffstat (limited to 'src/codegen/llvm.zig')
-rw-r--r--src/codegen/llvm.zig32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
index 6de001e5fd..ef33f39f55 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -2936,9 +2936,39 @@ pub const DeclGen = struct {
return dg.context.constString(
bytes.ptr,
@intCast(c_uint, tv.ty.arrayLenIncludingSentinel()),
- .True, // don't null terminate. bytes has the sentinel, if any.
+ .True, // Don't null terminate. Bytes has the sentinel, if any.
);
},
+ .str_lit => {
+ const str_lit = tv.val.castTag(.str_lit).?.data;
+ const bytes = dg.module.string_literal_bytes.items[str_lit.index..][0..str_lit.len];
+ if (tv.ty.sentinel()) |sent_val| {
+ const byte = @intCast(u8, sent_val.toUnsignedInt(target));
+ if (byte == 0 and bytes.len > 0) {
+ return dg.context.constString(
+ bytes.ptr,
+ @intCast(c_uint, bytes.len),
+ .False, // Yes, null terminate.
+ );
+ }
+ var array = std.ArrayList(u8).init(dg.gpa);
+ defer array.deinit();
+ try array.ensureUnusedCapacity(bytes.len + 1);
+ array.appendSliceAssumeCapacity(bytes);
+ array.appendAssumeCapacity(byte);
+ return dg.context.constString(
+ array.items.ptr,
+ @intCast(c_uint, array.items.len),
+ .True, // Don't null terminate.
+ );
+ } else {
+ return dg.context.constString(
+ bytes.ptr,
+ @intCast(c_uint, bytes.len),
+ .True, // Don't null terminate. `bytes` has the sentinel, if any.
+ );
+ }
+ },
.aggregate => {
const elem_vals = tv.val.castTag(.aggregate).?.data;
const elem_ty = tv.ty.elemType();