Skip to content

[clang][bytecode] Refactor Program::createGlobalString #125467

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
Feb 3, 2025
Merged
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
26 changes: 13 additions & 13 deletions clang/lib/AST/ByteCode/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const void *Program::getNativePointer(unsigned Idx) {
unsigned Program::createGlobalString(const StringLiteral *S, const Expr *Base) {
const size_t CharWidth = S->getCharByteWidth();
const size_t BitWidth = CharWidth * Ctx.getCharBit();
unsigned StringLength = S->getLength();

PrimType CharType;
switch (CharWidth) {
Expand All @@ -55,15 +56,15 @@ unsigned Program::createGlobalString(const StringLiteral *S, const Expr *Base) {
Base = S;

// Create a descriptor for the string.
Descriptor *Desc = allocateDescriptor(Base, CharType, Descriptor::GlobalMD,
S->getLength() + 1,
/*isConst=*/true,
/*isTemporary=*/false,
/*isMutable=*/false);
Descriptor *Desc =
allocateDescriptor(Base, CharType, Descriptor::GlobalMD, StringLength + 1,
/*isConst=*/true,
/*isTemporary=*/false,
/*isMutable=*/false);

// Allocate storage for the string.
// The byte length does not include the null terminator.
unsigned I = Globals.size();
unsigned GlobalIndex = Globals.size();
unsigned Sz = Desc->getAllocSize();
auto *G = new (Allocator, Sz) Global(Ctx.getEvalID(), Desc, /*isStatic=*/true,
/*isExtern=*/false);
Expand All @@ -74,33 +75,32 @@ unsigned Program::createGlobalString(const StringLiteral *S, const Expr *Base) {

// Construct the string in storage.
const Pointer Ptr(G->block());
for (unsigned I = 0, N = S->getLength(); I <= N; ++I) {
Pointer Field = Ptr.atIndex(I).narrow();
const uint32_t CodePoint = I == N ? 0 : S->getCodeUnit(I);
for (unsigned I = 0; I <= StringLength; ++I) {
Pointer Field = Ptr.atIndex(I);
const uint32_t CodePoint = I == StringLength ? 0 : S->getCodeUnit(I);
switch (CharType) {
case PT_Sint8: {
using T = PrimConv<PT_Sint8>::T;
Field.deref<T>() = T::from(CodePoint, BitWidth);
Field.initialize();
break;
}
case PT_Uint16: {
using T = PrimConv<PT_Uint16>::T;
Field.deref<T>() = T::from(CodePoint, BitWidth);
Field.initialize();
break;
}
case PT_Uint32: {
using T = PrimConv<PT_Uint32>::T;
Field.deref<T>() = T::from(CodePoint, BitWidth);
Field.initialize();
break;
}
default:
llvm_unreachable("unsupported character type");
}
}
return I;
Ptr.initialize();

return GlobalIndex;
}

Pointer Program::getPtrGlobal(unsigned Idx) const {
Expand Down