| author | |
| committer | |
| log | 1ebb7612445c1b0b0d76c73d17f18254e0d9efab |
| tree | 3b1c24935473e23a374f90780864b040515e2042 |
| parent | 8a0a6b7387fcd0017db85de14793abfd6ec7f6e5 |
4 files changed, 53 insertions(+), 0 deletions(-)
src/codegen.zig+8| ... | ... | @@ -854,6 +854,14 @@ pub fn generateSymbol( |
| 854 | 854 | } |
| 855 | 855 | return Result{ .appended = {} }; |
| 856 | 856 | }, |
| 857 | .str_lit => { | |
| 858 | const str_lit = typed_value.val.castTag(.str_lit).?.data; | |
| 859 | const mod = bin_file.options.module.?; | |
| 860 | const bytes = mod.string_literal_bytes.items[str_lit.index..][0..str_lit.len]; | |
| 861 | try code.ensureUnusedCapacity(str_lit.len); | |
| 862 | code.appendSliceAssumeCapacity(bytes); | |
| 863 | return Result{ .appended = {} }; | |
| 864 | }, | |
| 857 | 865 | else => unreachable, |
| 858 | 866 | }, |
| 859 | 867 | else => |t| { |
src/codegen/llvm.zig+26| ... | ... | @@ -3882,6 +3882,32 @@ pub const DeclGen = struct { |
| 3882 | 3882 | @intCast(c_uint, llvm_elems.len), |
| 3883 | 3883 | ); |
| 3884 | 3884 | }, |
| 3885 | .str_lit => { | |
| 3886 | // Note, sentinel is not stored | |
| 3887 | const str_lit = tv.val.castTag(.str_lit).?.data; | |
| 3888 | const bytes = dg.module.string_literal_bytes.items[str_lit.index..][0..str_lit.len]; | |
| 3889 | const vector_len = @intCast(usize, tv.ty.arrayLen()); | |
| 3890 | assert(vector_len == bytes.len); | |
| 3891 | ||
| 3892 | const elem_ty = tv.ty.elemType(); | |
| 3893 | const llvm_elems = try dg.gpa.alloc(*llvm.Value, vector_len); | |
| 3894 | defer dg.gpa.free(llvm_elems); | |
| 3895 | for (llvm_elems) |*elem, i| { | |
| 3896 | var byte_payload: Value.Payload.U64 = .{ | |
| 3897 | .base = .{ .tag = .int_u64 }, | |
| 3898 | .data = bytes[i], | |
| 3899 | }; | |
| 3900 | ||
| 3901 | elem.* = try dg.lowerValue(.{ | |
| 3902 | .ty = elem_ty, | |
| 3903 | .val = Value.initPayload(&byte_payload.base), | |
| 3904 | }); | |
| 3905 | } | |
| 3906 | return llvm.constVector( | |
| 3907 | llvm_elems.ptr, | |
| 3908 | @intCast(c_uint, llvm_elems.len), | |
| 3909 | ); | |
| 3910 | }, | |
| 3885 | 3911 | else => unreachable, |
| 3886 | 3912 | }, |
| 3887 | 3913 |
test/behavior.zig+1| ... | ... | @@ -161,6 +161,7 @@ test { |
| 161 | 161 | _ = @import("behavior/int_div.zig"); |
| 162 | 162 | _ = @import("behavior/inttoptr.zig"); |
| 163 | 163 | _ = @import("behavior/ir_block_deps.zig"); |
| 164 | _ = @import("behavior/lower_strlit_to_vector.zig"); | |
| 164 | 165 | _ = @import("behavior/math.zig"); |
| 165 | 166 | _ = @import("behavior/maximum_minimum.zig"); |
| 166 | 167 | _ = @import("behavior/member_func.zig"); |
test/behavior/lower_strlit_to_vector.zig created+18| ... | ... | @@ -0,0 +1,18 @@ |
| 1 | const std = @import("std"); | |
| 2 | const builtin = @import("builtin"); | |
| 3 | ||
| 4 | test "strlit to vector" { | |
| 5 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO | |
| 6 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 7 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 8 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | |
| 9 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO | |
| 10 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 11 | ||
| 12 | const strlit = "0123456789abcdef0123456789ABCDEF"; | |
| 13 | const vec_from_strlit: @Vector(32, u8) = strlit.*; | |
| 14 | const arr_from_vec = @as([32]u8, vec_from_strlit); | |
| 15 | for (strlit) |c, i| | |
| 16 | try std.testing.expect(c == arr_from_vec[i]); | |
| 17 | try std.testing.expectEqualSlices(u8, strlit, &arr_from_vec); | |
| 18 | } |