Skip to content

Commit 481f60c

Browse files
aykevldeadprogram
authored andcommitted
interp: add support for reading a pointer tag
This is necessary to get #3691 working.
1 parent cf39db3 commit 481f60c

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

interp/interpreter.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,14 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
746746
// src/strings/builder.go in the Go source tree. This is
747747
// the identity operator, so we can return the input.
748748
locals[inst.localIndex] = lhs
749+
} else if inst.opcode == llvm.And && rhs.Uint() < 8 {
750+
// This is probably part of a pattern to get the lower bits
751+
// of a pointer for pointer tagging, like this:
752+
// uintptr(unsafe.Pointer(t)) & 0b11
753+
// We can actually support this easily by ANDing with the
754+
// pointer offset.
755+
result := uint64(lhsPtr.offset()) & rhs.Uint()
756+
locals[inst.localIndex] = makeLiteralInt(result, int(lhs.len(r)*8))
749757
} else {
750758
// Catch-all for weird operations that should just be done
751759
// at runtime.

interp/testdata/consteval.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target triple = "x86_64--linux"
44
@intToPtrResult = global i8 0
55
@ptrToIntResult = global i8 0
66
@icmpResult = global i8 0
7+
@pointerTagResult = global i64 0
78
@someArray = internal global {i16, i8, i8} zeroinitializer
89
@someArrayPointer = global ptr zeroinitializer
910

@@ -17,6 +18,7 @@ define internal void @main.init() {
1718
call void @testPtrToInt()
1819
call void @testConstGEP()
1920
call void @testICmp()
21+
call void @testPointerTag()
2022
ret void
2123
}
2224

@@ -63,3 +65,9 @@ unequal:
6365
ret void
6466
ret void
6567
}
68+
69+
define internal void @testPointerTag() {
70+
%val = and i64 ptrtoint (ptr getelementptr inbounds (i8, ptr @someArray, i32 2) to i64), 3
71+
store i64 %val, ptr @pointerTagResult
72+
ret void
73+
}

interp/testdata/consteval.out.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target triple = "x86_64--linux"
44
@intToPtrResult = local_unnamed_addr global i8 2
55
@ptrToIntResult = local_unnamed_addr global i8 2
66
@icmpResult = local_unnamed_addr global i8 2
7+
@pointerTagResult = local_unnamed_addr global i64 2
78
@someArray = internal global { i16, i8, i8 } zeroinitializer
89
@someArrayPointer = local_unnamed_addr global ptr getelementptr inbounds ({ i16, i8, i8 }, ptr @someArray, i64 0, i32 1)
910

0 commit comments

Comments
 (0)