More unification work.

This commit is contained in:
kai
2012-07-29 16:13:52 +02:00
parent 10a0bdf891
commit bf2aaaca84
2 changed files with 79 additions and 4 deletions

View File

@@ -439,6 +439,23 @@ LLConstant* ComplexExp::toConstElem(IRState* p)
//////////////////////////////////////////////////////////////////////////////////////////
#if LDC_LLVM_VER >= 301
template <typename T>
static inline LLConstant* toConstantArray(LLType* ct, LLArrayType* at, T* str, size_t len, bool nullterm = true)
{
std::vector<LLConstant*> vals;
vals.reserve(len+1);
for (size_t i = 0; i < len; ++i) {
vals.push_back(LLConstantInt::get(ct, str[i], false));
}
if (nullterm)
vals.push_back(LLConstantInt::get(ct, 0, false));
return LLConstantArray::get(at, vals);
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////
DValue* StringExp::toElem(IRState* p)
{
Logger::print("StringExp::toElem: %s @ %s\n", toChars(), type->toChars());
@@ -452,6 +469,7 @@ DValue* StringExp::toElem(IRState* p)
LLArrayType* at = LLArrayType::get(ct,len+1);
LLConstant* _init;
#if LDC_LLVM_VER == 300
if (cty->size() == 1) {
uint8_t* str = (uint8_t*)string;
llvm::StringRef cont((char*)str, len);
@@ -479,6 +497,22 @@ DValue* StringExp::toElem(IRState* p)
}
else
assert(0);
#else
switch (cty->size())
{
default:
llvm_unreachable("Unknown char type");
case 1:
_init = toConstantArray(ct, at, static_cast<uint8_t *>(string), len);
break;
case 2:
_init = toConstantArray(ct, at, static_cast<uint16_t *>(string), len);
break;
case 4:
_init = toConstantArray(ct, at, static_cast<uint32_t *>(string), len);
break;
}
#endif
llvm::GlobalValue::LinkageTypes _linkage = llvm::GlobalValue::InternalLinkage;
if (Logger::enabled())
@@ -523,6 +557,7 @@ LLConstant* StringExp::toConstElem(IRState* p)
LLArrayType* at = LLArrayType::get(ct,endlen);
LLConstant* _init;
#if LDC_LLVM_VER == 300
if (cty->size() == 1) {
uint8_t* str = (uint8_t*)string;
llvm::StringRef cont((char*)str, len);
@@ -552,6 +587,22 @@ LLConstant* StringExp::toConstElem(IRState* p)
}
else
assert(0);
#else
switch (cty->size())
{
default:
llvm_unreachable("Unknown char type");
case 1:
_init = toConstantArray(ct, at, static_cast<uint8_t *>(string), len, nullterm);
break;
case 2:
_init = toConstantArray(ct, at, static_cast<uint16_t *>(string), len, nullterm);
break;
case 4:
_init = toConstantArray(ct, at, static_cast<uint32_t *>(string), len, nullterm);
break;
}
#endif
if (t->ty == Tsarray)
{

View File

@@ -34,7 +34,11 @@ bool DtoIsPassedByRef(Type* type)
return (t == Tstruct || t == Tsarray);
}
#if LDC_LLVM_VER == 300
unsigned DtoShouldExtend(Type* type)
#else
llvm::Attributes DtoShouldExtend(Type* type)
#endif
{
type = type->toBasetype();
if (type->isintegral())
@@ -479,7 +483,7 @@ void DtoMemSet(LLValue* dst, LLValue* val, LLValue* nbytes)
LLType *Tys[2] ={VoidPtrTy, intTy};
llvm::Function* fn = llvm::Intrinsic::getDeclaration(gIR->module,
llvm::Intrinsic::memset, llvm::makeArrayRef(Tys, 2));
gIR->ir->CreateCall5(fn, dst, val, nbytes, DtoConstUint(1), DtoConstBool(false), "");
}
@@ -502,7 +506,7 @@ void DtoMemCpy(LLValue* dst, LLValue* src, LLValue* nbytes, unsigned align)
LLType *Tys[3] ={VoidPtrTy, VoidPtrTy, intTy};
llvm::Function* fn = llvm::Intrinsic::getDeclaration(gIR->module,
llvm::Intrinsic::memcpy, llvm::makeArrayRef(Tys, 3));
gIR->ir->CreateCall5(fn, dst, src, nbytes, DtoConstUint(align), DtoConstBool(false), "");
}
@@ -605,12 +609,27 @@ LLConstant* DtoConstFP(Type* t, longdouble value)
//////////////////////////////////////////////////////////////////////////////////////////
#if LDC_LLVM_VER >= 301
static inline LLConstant* toConstString(llvm::LLVMContext& ctx, const llvm::StringRef& str)
{
LLSmallVector<uint8_t, 64> vals;
vals.append(str.begin(), str.end());
vals.push_back(0);
return llvm::ConstantDataArray::get(ctx, vals);
}
#endif
LLConstant* DtoConstString(const char* str)
{
#if LDC_LLVM_VER == 300
llvm::StringRef s(str?str:"");
LLConstant* init = LLConstantArray::get(gIR->context(), s, true);
#else
llvm::StringRef s(str ? str : "");
LLConstant* init = toConstString(gIR->context(), s);
#endif
llvm::GlobalVariable* gvar = new llvm::GlobalVariable(
*gIR->module, init->getType(), true,llvm::GlobalValue::InternalLinkage, init, ".str");
*gIR->module, init->getType(), true, llvm::GlobalValue::InternalLinkage, init, ".str");
LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
return DtoConstSlice(
DtoConstSize_t(s.size()),
@@ -620,10 +639,15 @@ LLConstant* DtoConstString(const char* str)
}
LLConstant* DtoConstStringPtr(const char* str, const char* section)
{
#if LDC_LLVM_VER == 300
llvm::StringRef s(str);
LLConstant* init = LLConstantArray::get(gIR->context(), s, true);
#else
llvm::StringRef s(str);
LLConstant* init = toConstString(gIR->context(), s);
#endif
llvm::GlobalVariable* gvar = new llvm::GlobalVariable(
*gIR->module, init->getType(), true,llvm::GlobalValue::InternalLinkage, init, ".str");
*gIR->module, init->getType(), true, llvm::GlobalValue::InternalLinkage, init, ".str");
if (section) gvar->setSection(section);
LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
return llvm::ConstantExpr::getGetElementPtr(gvar, idxs, true);