Skip to content

[clang][bytecode] IntPointer::atOffset() should append #104686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,17 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
if (!CheckArray(S, OpPC, Ptr))
return false;

// This is much simpler for integral pointers, so handle them first.
if (Ptr.isIntegralPointer()) {
uint64_t V = Ptr.getIntegerRepresentation();
uint64_t O = static_cast<uint64_t>(Offset) * Ptr.elemSize();
if constexpr (Op == ArithOp::Add)
S.Stk.push<Pointer>(V + O, Ptr.asIntPointer().Desc);
else
S.Stk.push<Pointer>(V - O, Ptr.asIntPointer().Desc);
return true;
}

uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
uint64_t Index;
if (Ptr.isOnePastEnd())
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,5 +647,5 @@ IntPointer IntPointer::atOffset(const ASTContext &ASTCtx,
uint64_t FieldOffset =
ASTCtx.toCharUnitsFromBits(Layout.getFieldOffset(FieldIndex))
.getQuantity();
return IntPointer{this->Desc, FieldOffset};
return IntPointer{this->Desc, this->Value + FieldOffset};
}
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ class Pointer {
/// Returns the index into an array.
int64_t getIndex() const {
if (!isBlockPointer())
return 0;
return getIntegerRepresentation();

if (isZero())
return 0;
Expand Down
19 changes: 19 additions & 0 deletions clang/test/AST/ByteCode/codegen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s -fexperimental-new-constant-interpreter | FileCheck %s

typedef __INTPTR_TYPE__ intptr_t;

const intptr_t Z1 = (intptr_t)(((char*)-1LL) + 1);
// CHECK: @Z1 = constant i64 0

const intptr_t Z2 = (intptr_t)(((char*)1LL) - 1);
// CHECK: @Z2 = constant i64 0

struct A {
char num_fields;
};
struct B {
char a, b[1];
};
const int A = (char *)(&( (struct B *)(16) )->b[0]) - (char *)(16);
// CHECK: @A = constant i32 1
Loading