diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-05-24 00:10:56 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-05-24 01:01:24 -0700 |
| commit | 818fbd9c567b907031c44961e4299ce1f9059be6 (patch) | |
| tree | db185db14c188c6226824b2f865023d9ef7b25e8 /src/codegen.zig | |
| parent | 8171972cbb477672ee1a99d953df4aaecb744a0c (diff) | |
| download | zig-818fbd9c567b907031c44961e4299ce1f9059be6.tar.gz zig-818fbd9c567b907031c44961e4299ce1f9059be6.zip | |
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.
Diffstat (limited to 'src/codegen.zig')
| -rw-r--r-- | src/codegen.zig | 16 |
1 files changed, 14 insertions, 2 deletions
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 => { |
