From 69ddfbfe9e647b1a445be8c99f5c703b985c54c4 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 20 May 2008 03:20:09 +0000 Subject: [PATCH] Fix ExecutionEngine's constant code to work properly when structs and arrays will become first-class types. llvm-svn: 51293 --- llvm/lib/ExecutionEngine/ExecutionEngine.cpp | 37 ++++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/llvm/lib/ExecutionEngine/ExecutionEngine.cpp b/llvm/lib/ExecutionEngine/ExecutionEngine.cpp index 8bd3b1127a95..535f4ac90f70 100644 --- a/llvm/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/llvm/lib/ExecutionEngine/ExecutionEngine.cpp @@ -812,35 +812,26 @@ void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { } else if (isa(Init)) { memset(Addr, 0, (size_t)getTargetData()->getABITypeSize(Init->getType())); return; + } else if (const ConstantArray *CPA = dyn_cast(Init)) { + unsigned ElementSize = + getTargetData()->getABITypeSize(CPA->getType()->getElementType()); + for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) + InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); + return; + } else if (const ConstantStruct *CPS = dyn_cast(Init)) { + const StructLayout *SL = + getTargetData()->getStructLayout(cast(CPS->getType())); + for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) + InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); + return; } else if (Init->getType()->isFirstClassType()) { GenericValue Val = getConstantValue(Init); StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); return; } - switch (Init->getType()->getTypeID()) { - case Type::ArrayTyID: { - const ConstantArray *CPA = cast(Init); - unsigned ElementSize = - getTargetData()->getABITypeSize(CPA->getType()->getElementType()); - for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) - InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); - return; - } - - case Type::StructTyID: { - const ConstantStruct *CPS = cast(Init); - const StructLayout *SL = - getTargetData()->getStructLayout(cast(CPS->getType())); - for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) - InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); - return; - } - - default: - cerr << "Bad Type: " << *Init->getType() << "\n"; - assert(0 && "Unknown constant type to initialize memory with!"); - } + cerr << "Bad Type: " << *Init->getType() << "\n"; + assert(0 && "Unknown constant type to initialize memory with!"); } /// EmitGlobals - Emit all of the global variables to memory, storing their