diff options
| author | Robin Voetter <robin@voetter.nl> | 2021-10-21 16:17:21 +0200 |
|---|---|---|
| committer | Robin Voetter <robin@voetter.nl> | 2021-10-21 16:24:18 +0200 |
| commit | 09c7d5aebc4f7fe96a89f93c0cf99cc03977ea5f (patch) | |
| tree | 7877d79040b38f43aea7fc192546b19d07821bc0 /src/value.zig | |
| parent | 84876fec582e13707a809c8136bb1eaf92b5da09 (diff) | |
| download | zig-09c7d5aebc4f7fe96a89f93c0cf99cc03977ea5f.tar.gz zig-09c7d5aebc4f7fe96a89f93c0cf99cc03977ea5f.zip | |
stage2: elemPtr for slices
* Restructure elemPtr a bit
* New AIR instruction: slice_elem_ptr, which returns a pointer to an element of a slice
* Value: adapt elemPtr to work on slices
Diffstat (limited to 'src/value.zig')
| -rw-r--r-- | src/value.zig | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/src/value.zig b/src/value.zig index f1ca384b75..bb904cea30 100644 --- a/src/value.zig +++ b/src/value.zig @@ -114,7 +114,7 @@ pub const Value = extern union { /// This Tag will never be seen by machine codegen backends. It is changed into a /// `decl_ref` when a comptime variable goes out of scope. decl_ref_mut, - /// Pointer to a specific element of an array. + /// Pointer to a specific element of an array, vector or slice. elem_ptr, /// Pointer to a specific field of a struct or union. field_ptr, @@ -1792,17 +1792,23 @@ pub const Value = extern union { /// Returns a pointer to the element value at the index. pub fn elemPtr(self: Value, allocator: *Allocator, index: usize) !Value { - if (self.castTag(.elem_ptr)) |elem_ptr| { - return Tag.elem_ptr.create(allocator, .{ - .array_ptr = elem_ptr.data.array_ptr, - .index = elem_ptr.data.index + index, - }); + switch (self.tag()) { + .elem_ptr => { + const elem_ptr = self.castTag(.elem_ptr).?.data; + return Tag.elem_ptr.create(allocator, .{ + .array_ptr = elem_ptr.array_ptr, + .index = elem_ptr.index + index, + }); + }, + .slice => return Tag.elem_ptr.create(allocator, .{ + .array_ptr = self.castTag(.slice).?.data.ptr, + .index = index, + }), + else => return Tag.elem_ptr.create(allocator, .{ + .array_ptr = self, + .index = index, + }), } - - return Tag.elem_ptr.create(allocator, .{ - .array_ptr = self, - .index = index, - }); } pub fn isUndef(self: Value) bool { |
