Skip to content

Commit dac1829

Browse files
authored
[clang][bytecode] IntPointer::atOffset() should append (#104686)
... to current offset. This breaks other tests which this commit also fixes. Namely, getIndex() should return the integer representation for non-block pointers.
1 parent 07bd3bb commit dac1829

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,17 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
18531853
if (!CheckArray(S, OpPC, Ptr))
18541854
return false;
18551855

1856+
// This is much simpler for integral pointers, so handle them first.
1857+
if (Ptr.isIntegralPointer()) {
1858+
uint64_t V = Ptr.getIntegerRepresentation();
1859+
uint64_t O = static_cast<uint64_t>(Offset) * Ptr.elemSize();
1860+
if constexpr (Op == ArithOp::Add)
1861+
S.Stk.push<Pointer>(V + O, Ptr.asIntPointer().Desc);
1862+
else
1863+
S.Stk.push<Pointer>(V - O, Ptr.asIntPointer().Desc);
1864+
return true;
1865+
}
1866+
18561867
uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
18571868
uint64_t Index;
18581869
if (Ptr.isOnePastEnd())

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,5 +654,5 @@ IntPointer IntPointer::atOffset(const ASTContext &ASTCtx,
654654
uint64_t FieldOffset =
655655
ASTCtx.toCharUnitsFromBits(Layout.getFieldOffset(FieldIndex))
656656
.getQuantity();
657-
return IntPointer{this->Desc, FieldOffset};
657+
return IntPointer{this->Desc, this->Value + FieldOffset};
658658
}

clang/lib/AST/ByteCode/Pointer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ class Pointer {
571571
/// Returns the index into an array.
572572
int64_t getIndex() const {
573573
if (!isBlockPointer())
574-
return 0;
574+
return getIntegerRepresentation();
575575

576576
if (isZero())
577577
return 0;

clang/test/AST/ByteCode/codegen.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s | FileCheck %s
2+
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s -fexperimental-new-constant-interpreter | FileCheck %s
3+
4+
typedef __INTPTR_TYPE__ intptr_t;
5+
6+
const intptr_t Z1 = (intptr_t)(((char*)-1LL) + 1);
7+
// CHECK: @Z1 = constant i64 0
8+
9+
const intptr_t Z2 = (intptr_t)(((char*)1LL) - 1);
10+
// CHECK: @Z2 = constant i64 0
11+
12+
struct A {
13+
char num_fields;
14+
};
15+
struct B {
16+
char a, b[1];
17+
};
18+
const int A = (char *)(&( (struct B *)(16) )->b[0]) - (char *)(16);
19+
// CHECK: @A = constant i32 1

0 commit comments

Comments
 (0)