Fix serious bug in handling constant-valued initializers.

In InitSymbol(), we try to be smart and emit a memcpy when there
are a number of values to store (e.g. for arrays, structs, etc.)

Unfortunately, this wasn't working as desired for bools (i.e. i1 types),
since the SizeOf() call that tried to figure out how many bytes to
copy would return 0 bytes, due to dividing the number of bits to copy
by 8.

Fixes issue #234.
This commit is contained in:
Matt Pharr
2012-04-09 14:23:08 -07:00
parent 8475dc082a
commit 972043c146
2 changed files with 14 additions and 8 deletions

View File

@@ -643,13 +643,17 @@ InitSymbol(llvm::Value *ptr, const Type *symType, Expr *initExpr,
return;
}
llvm::Value *constPtr =
new llvm::GlobalVariable(*m->module, llvmType, true /* const */,
llvm::GlobalValue::InternalLinkage,
constValue, "const_initializer");
llvm::Value *size = g->target.SizeOf(llvmType,
ctx->GetCurrentBasicBlock());
ctx->MemcpyInst(ptr, constPtr, size);
if (Type::IsBasicType(symType))
ctx->StoreInst(constValue, ptr);
else {
llvm::Value *constPtr =
new llvm::GlobalVariable(*m->module, llvmType, true /* const */,
llvm::GlobalValue::InternalLinkage,
constValue, "const_initializer");
llvm::Value *size = g->target.SizeOf(llvmType,
ctx->GetCurrentBasicBlock());
ctx->MemcpyInst(ptr, constPtr, size);
}
return;
}

View File

@@ -518,7 +518,9 @@ Target::SizeOf(LLVM_TYPE_CONST llvm::Type *type,
const llvm::TargetData *td = GetTargetMachine()->getTargetData();
Assert(td != NULL);
uint64_t byteSize = td->getTypeSizeInBits(type) / 8;
uint64_t bitSize = td->getTypeSizeInBits(type);
Assert((bitSize % 8) == 0);
uint64_t byteSize = bitSize / 8;
if (is32Bit || g->opt.force32BitAddressing)
return LLVMInt32((int32_t)byteSize);
else