Error if static array is cast to an array such that oldarraysize % newelemsize != 0.

This commit is contained in:
Christian Kamm 2008-07-29 12:32:01 +02:00
parent 99f1cfef36
commit caa61a5523
4 changed files with 23 additions and 15 deletions

View file

@ -672,7 +672,7 @@ DSliceValue* DtoCatArrayElement(Type* type, Expression* exp1, Expression* exp2)
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
// helper for eq and cmp // helper for eq and cmp
static LLValue* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r, bool useti) static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue* r, bool useti)
{ {
Logger::println("comparing arrays"); Logger::println("comparing arrays");
LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, func); LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, func);
@ -689,9 +689,9 @@ static LLValue* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r, bool
if ((l_ty->ty == Tsarray) || (r_ty->ty == Tsarray)) { if ((l_ty->ty == Tsarray) || (r_ty->ty == Tsarray)) {
Type* a_ty = l_ty->next->arrayOf(); Type* a_ty = l_ty->next->arrayOf();
if (l_ty->ty == Tsarray) if (l_ty->ty == Tsarray)
l = DtoCastArray(l, a_ty); l = DtoCastArray(loc, l, a_ty);
if (r_ty->ty == Tsarray) if (r_ty->ty == Tsarray)
r = DtoCastArray(r, a_ty); r = DtoCastArray(loc, r, a_ty);
} }
Logger::println("giving storage"); Logger::println("giving storage");
@ -758,9 +758,9 @@ static LLValue* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r, bool
} }
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r) LLValue* DtoArrayEquals(Loc& loc, TOK op, DValue* l, DValue* r)
{ {
LLValue* res = DtoArrayEqCmp_impl("_adEq", l, r, true); LLValue* res = DtoArrayEqCmp_impl(loc, "_adEq", l, r, true);
res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp"); res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp");
if (op == TOKnotequal) if (op == TOKnotequal)
res = gIR->ir->CreateNot(res, "tmp"); res = gIR->ir->CreateNot(res, "tmp");
@ -769,7 +769,7 @@ LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r)
} }
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r) LLValue* DtoArrayCompare(Loc& loc, TOK op, DValue* l, DValue* r)
{ {
LLValue* res = 0; LLValue* res = 0;
@ -817,9 +817,9 @@ LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r)
{ {
Type* t = DtoDType(DtoDType(l->getType())->next); Type* t = DtoDType(DtoDType(l->getType())->next);
if (t->ty == Tchar) if (t->ty == Tchar)
res = DtoArrayEqCmp_impl("_adCmpChar", l, r, false); res = DtoArrayEqCmp_impl(loc, "_adCmpChar", l, r, false);
else else
res = DtoArrayEqCmp_impl("_adCmp", l, r, true); res = DtoArrayEqCmp_impl(loc, "_adCmp", l, r, true);
res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp"); res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp");
} }
@ -944,7 +944,7 @@ LLValue* DtoArrayPtr(DValue* v)
} }
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
DValue* DtoCastArray(DValue* u, Type* to) DValue* DtoCastArray(Loc& loc, DValue* u, Type* to)
{ {
Logger::println("DtoCastArray"); Logger::println("DtoCastArray");
LOG_SCOPE; LOG_SCOPE;
@ -968,6 +968,7 @@ DValue* DtoCastArray(DValue* u, Type* to)
} }
else if (totype->ty == Tarray) { else if (totype->ty == Tarray) {
Logger::cout() << "to array" << '\n'; Logger::cout() << "to array" << '\n';
const LLType* ptrty = DtoArrayType(totype)->getContainedType(1); const LLType* ptrty = DtoArrayType(totype)->getContainedType(1);
const LLType* ety = DtoTypeNotVoid(fromtype->next); const LLType* ety = DtoTypeNotVoid(fromtype->next);
@ -986,6 +987,13 @@ DValue* DtoCastArray(DValue* u, Type* to)
Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
assert(isaPointer(uval->getType())); assert(isaPointer(uval->getType()));
const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0)); const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0));
if(arrty->getNumElements() % totype->next->size() != 0)
{
error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars());
fatal();
}
rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false); rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false);
rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0)); rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
rval = DtoBitCast(uval, ptrty); rval = DtoBitCast(uval, ptrty);

View file

@ -30,8 +30,8 @@ DSliceValue* DtoCatArrayElement(Type* type, Expression* exp1, Expression* exp2);
void DtoStaticArrayCopy(LLValue* dst, LLValue* src); void DtoStaticArrayCopy(LLValue* dst, LLValue* src);
LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r); LLValue* DtoArrayEquals(Loc& loc, TOK op, DValue* l, DValue* r);
LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r); LLValue* DtoArrayCompare(Loc& loc, TOK op, DValue* l, DValue* r);
LLValue* DtoDynArrayIs(TOK op, DValue* l, DValue* r); LLValue* DtoDynArrayIs(TOK op, DValue* l, DValue* r);
@ -40,6 +40,6 @@ LLValue* DtoArrayCastLength(LLValue* len, const LLType* elemty, const LLType* ne
LLValue* DtoArrayLen(DValue* v); LLValue* DtoArrayLen(DValue* v);
LLValue* DtoArrayPtr(DValue* v); LLValue* DtoArrayPtr(DValue* v);
DValue* DtoCastArray(DValue* val, Type* to); DValue* DtoCastArray(Loc& loc, DValue* val, Type* to);
#endif // LLVMC_GEN_ARRAYS_H #endif // LLVMC_GEN_ARRAYS_H

View file

@ -846,7 +846,7 @@ DValue* DtoCast(Loc& loc, DValue* val, Type* to)
return DtoCastClass(val, to); return DtoCastClass(val, to);
} }
else if (fromtype->ty == Tarray || fromtype->ty == Tsarray) { else if (fromtype->ty == Tarray || fromtype->ty == Tsarray) {
return DtoCastArray(val, to); return DtoCastArray(loc, val, to);
} }
else if (fromtype->ty == Tpointer || fromtype->ty == Tfunction) { else if (fromtype->ty == Tpointer || fromtype->ty == Tfunction) {
return DtoCastPtr(loc, val, to); return DtoCastPtr(loc, val, to);

View file

@ -1309,7 +1309,7 @@ DValue* CmpExp::toElem(IRState* p)
else if (t->ty == Tsarray || t->ty == Tarray) else if (t->ty == Tsarray || t->ty == Tarray)
{ {
Logger::println("static or dynamic array"); Logger::println("static or dynamic array");
eval = DtoArrayCompare(op,l,r); eval = DtoArrayCompare(loc,op,l,r);
} }
else else
{ {
@ -1382,7 +1382,7 @@ DValue* EqualExp::toElem(IRState* p)
else if (t->ty == Tsarray || t->ty == Tarray) else if (t->ty == Tsarray || t->ty == Tarray)
{ {
Logger::println("static or dynamic array"); Logger::println("static or dynamic array");
eval = DtoArrayEquals(op,l,r); eval = DtoArrayEquals(loc,op,l,r);
} }
else if (t->ty == Tdelegate) else if (t->ty == Tdelegate)
{ {