authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-24 20:37:09+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-25 20:41:15+02:00
log7062c8a8865bbd2fb8181b579da552295cd68e6a
treeafa5a38081c75c63599a1642a78266b347b1b5fd
parent0942bf73c90eabf87d0ca965b50beb0fd9a8fc8c

stage2: comptime slice of pointer to hardcoded address


3 files changed, 19 insertions(+), 1 deletions(-)

src/Sema.zig+6-1
......@@ -8001,7 +8001,12 @@ fn analyzePtrArithmetic(
80018001 const offset_int = offset_val.toUnsignedInt();
80028002 if (ptr_val.getUnsignedInt()) |addr| {
80038003 const target = sema.mod.getTarget();
8004 const elem_ty = ptr_ty.childType();
8004 const ptr_child_ty = ptr_ty.childType();
8005 const elem_ty = if (ptr_ty.isSinglePointer() and ptr_child_ty.zigTypeTag() == .Array)
8006 ptr_child_ty.childType()
8007 else
8008 ptr_child_ty;
8009
80058010 const elem_size = elem_ty.abiSize(target);
80068011 const new_addr = switch (air_tag) {
80078012 .ptr_add => addr + elem_size * offset_int,
test/behavior.zig+1
......@@ -65,6 +65,7 @@ test {
6565 if (builtin.zig_is_stage2) {
6666 // When all comptime_memory.zig tests pass, #9646 can be closed.
6767 // _ = @import("behavior/comptime_memory.zig");
68 _ = @import("behavior/slice_stage2.zig");
6869 } else {
6970 _ = @import("behavior/align_stage1.zig");
7071 _ = @import("behavior/alignof.zig");
test/behavior/slice_stage2.zig created+12
......@@ -0,0 +1,12 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4const x = @intToPtr([*]i32, 0x1000)[0..0x500];
5const y = x[0x100..];
6test "compile time slice of pointer to hard coded address" {
7 try expect(@ptrToInt(x) == 0x1000);
8 try expect(x.len == 0x500);
9
10 try expect(@ptrToInt(y) == 0x1400);
11 try expect(y.len == 0x400);
12}