comparison gen/arrays.cpp @ 1292:ad41053c336e

Make static int[] a = [1, 2]; a[0] = 4; not segfault by making the array data ptr a non-const global variable.
author Christian Kamm <kamm incasoftware de>
date Sun, 03 May 2009 11:16:54 +0200
parents 0686701178d3
children c78fd2d30da1
comparison
equal deleted inserted replaced
1291:875afb7a93b6 1292:ad41053c336e
315 // if the type is a static array, we're done 315 // if the type is a static array, we're done
316 if (arrty->ty == Tsarray) 316 if (arrty->ty == Tsarray)
317 return constarr; 317 return constarr;
318 318
319 // for dynamic array we need to make a global with the data, so we have a pointer for the dynamic array 319 // for dynamic array we need to make a global with the data, so we have a pointer for the dynamic array
320 LLGlobalVariable* gvar = new LLGlobalVariable(constarr->getType(), true, LLGlobalValue::InternalLinkage, constarr, ".constarray", gIR->module); 320 // Important: don't make the gvar constant, since this const initializer might
321 // be used as an initializer for a static T[] - where modifying contents is allowed.
322 LLGlobalVariable* gvar = new LLGlobalVariable(constarr->getType(), false, LLGlobalValue::InternalLinkage, constarr, ".constarray", gIR->module);
321 LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) }; 323 LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
322 324
323 LLConstant* gep = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2); 325 LLConstant* gep = llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
324 gep = llvm::ConstantExpr::getBitCast(gvar, getPtrToType(llelemty)); 326 gep = llvm::ConstantExpr::getBitCast(gvar, getPtrToType(llelemty));
325 327