Skip to content

[clang][bytecode] Implement base casts on integral pointers #108340

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
Sep 12, 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
12 changes: 12 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,12 @@ inline bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off) {

inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
const Pointer &Ptr = S.Stk.peek<Pointer>();

if (!Ptr.isBlockPointer()) {
S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
return true;
}

if (!CheckNull(S, OpPC, Ptr, CSK_Base))
return false;
if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
Expand All @@ -1624,6 +1630,12 @@ inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {

inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off) {
const Pointer &Ptr = S.Stk.pop<Pointer>();

if (!Ptr.isBlockPointer()) {
S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
return true;
}

if (!CheckNull(S, OpPC, Ptr, CSK_Base))
return false;
if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
Expand Down
23 changes: 23 additions & 0 deletions clang/lib/AST/ByteCode/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,26 @@ IntPointer IntPointer::atOffset(const ASTContext &ASTCtx,
.getQuantity();
return IntPointer{this->Desc, this->Value + FieldOffset};
}

IntPointer IntPointer::baseCast(const ASTContext &ASTCtx,
unsigned BaseOffset) const {
const Record *R = Desc->ElemRecord;
const Descriptor *BaseDesc = nullptr;

// This iterates over bases and checks for the proper offset. That's
// potentially slow but this case really shouldn't happen a lot.
for (const Record::Base &B : R->bases()) {
if (B.Offset == BaseOffset) {
BaseDesc = B.Desc;
break;
}
}
assert(BaseDesc);

// Adjust the offset value based on the information from the record layout.
const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(R->getDecl());
CharUnits BaseLayoutOffset =
Layout.getBaseClassOffset(cast<CXXRecordDecl>(BaseDesc->asDecl()));

return {BaseDesc, Value + BaseLayoutOffset.getQuantity()};
}
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct IntPointer {
uint64_t Value;

IntPointer atOffset(const ASTContext &ASTCtx, unsigned Offset) const;
IntPointer baseCast(const ASTContext &ASTCtx, unsigned BaseOffset) const;
};

enum class Storage { Block, Int, Fn };
Expand Down
19 changes: 19 additions & 0 deletions clang/test/AST/ByteCode/const-base-cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck %s
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm %s -o - -fexperimental-new-constant-interpreter | FileCheck %s


/// Slightly adapted to the version from test/CodeGenCXX/.

struct X { int x[12];};
struct A : X { char x, y, z; };
struct B { char y; };
struct C : A,B {};
unsigned char x = ((char*)(X*)(C*)0x1000) - (char*)0x1000;
// CHECK: @x = {{(dso_local )?}}global i8 0

unsigned char y = ((char*)(B*)(C*)0x1000) - (char*)0x1000;
// CHECK: @y = {{(dso_local )?}}global i8 51

unsigned char z = ((char*)(A*)(C*)0x1000) - (char*)0x1000;
// CHECK: @z = {{(dso_local )?}}global i8 0

2 changes: 1 addition & 1 deletion clang/test/CodeGenCXX/const-base-cast.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck %s
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm %s -o - -fexperimental-new-constant-interpreter | FileCheck %s

// Check that the following construct, which is similar to one which occurs
// in Firefox, is folded correctly.
Expand Down
Loading