diff gen/arrays.cpp @ 1328:c78fd2d30da1

Changed array slice copying to call a runtime function when assertions or array bound checks are enabled instead of just doing a memcpy. This makes sure an exception is thrown if the copy is invalid (ie. different lengths or overlap). Fixes ticket #283 . Rebuilding the runtime is necessary.
author Tomas Lindquist Olsen <tomas.l.olsen gmail com>
date Sun, 10 May 2009 02:23:05 +0200
parents ad41053c336e
children 16d9afa22df4
line wrap: on
line diff
--- a/gen/arrays.cpp	Sun May 10 00:50:35 2009 +0200
+++ b/gen/arrays.cpp	Sun May 10 02:23:05 2009 +0200
@@ -16,6 +16,8 @@
 #include "gen/dvalue.h"
 #include "ir/irmodule.h"
 
+#include "gen/cl_options.h"
+
 //////////////////////////////////////////////////////////////////////////////////////////
 
 const LLStructType* DtoArrayType(Type* arrayTy)
@@ -334,7 +336,7 @@
     assert(e->len != 0);
     const LLType* t = e->ptr->getType()->getContainedType(0);
     sz = gIR->ir->CreateMul(DtoConstSize_t(getTypePaddedSize(t)), e->len, "tmp");
-    return e->ptr;
+    return DtoBitCast(e->ptr, getVoidPtrType());
 }
 
 void DtoArrayCopySlices(DSliceValue* dst, DSliceValue* src)
@@ -345,7 +347,15 @@
     LLValue* dstarr = get_slice_ptr(dst,sz1);
     LLValue* srcarr = get_slice_ptr(src,sz2);
 
-    DtoMemCpy(dstarr, srcarr, sz1);
+    if (global.params.useAssert || global.params.useArrayBounds)
+    {
+        LLValue* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_array_slice_copy");
+        gIR->CreateCallOrInvoke4(fn, dstarr, sz1, srcarr, sz2);
+    }
+    else
+    {
+        DtoMemCpy(dstarr, srcarr, sz1);
+    }
 }
 
 void DtoArrayCopyToSlice(DSliceValue* dst, DValue* src)
@@ -354,9 +364,17 @@
 
     LLValue* sz1;
     LLValue* dstarr = get_slice_ptr(dst,sz1);
-    LLValue* srcarr = DtoArrayPtr(src);
+    LLValue* srcarr = DtoBitCast(DtoArrayPtr(src), getVoidPtrType());
 
-    DtoMemCpy(dstarr, srcarr, sz1);
+    if (global.params.useAssert || global.params.useArrayBounds)
+    {
+        LLValue* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_array_slice_copy");
+        gIR->CreateCallOrInvoke4(fn, dstarr, sz1, srcarr, DtoArrayLen(src));
+    }
+    else
+    {
+        DtoMemCpy(dstarr, srcarr, sz1);
+    }
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////