From bf2aaaca843760f26c04014b03c2c996395f7fdc Mon Sep 17 00:00:00 2001 From: kai Date: Sun, 29 Jul 2012 16:13:52 +0200 Subject: [PATCH] More unification work. --- gen/toir.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ gen/tollvm.cpp | 32 +++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/gen/toir.cpp b/gen/toir.cpp index 636e319b..43d60008 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -439,6 +439,23 @@ LLConstant* ComplexExp::toConstElem(IRState* p) ////////////////////////////////////////////////////////////////////////////////////////// +#if LDC_LLVM_VER >= 301 +template +static inline LLConstant* toConstantArray(LLType* ct, LLArrayType* at, T* str, size_t len, bool nullterm = true) +{ + std::vector 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(string), len); + break; + case 2: + _init = toConstantArray(ct, at, static_cast(string), len); + break; + case 4: + _init = toConstantArray(ct, at, static_cast(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(string), len, nullterm); + break; + case 2: + _init = toConstantArray(ct, at, static_cast(string), len, nullterm); + break; + case 4: + _init = toConstantArray(ct, at, static_cast(string), len, nullterm); + break; + } +#endif if (t->ty == Tsarray) { diff --git a/gen/tollvm.cpp b/gen/tollvm.cpp index 7223cd87..6cd2a9b9 100644 --- a/gen/tollvm.cpp +++ b/gen/tollvm.cpp @@ -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 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);