From c357c964710ff19b93c691f3f5379ee2fbdf0e93 Mon Sep 17 00:00:00 2001 From: Tomas Lindquist Olsen Date: Thu, 4 Oct 2007 10:57:26 +0200 Subject: [PATCH] [svn r30] * Fixed static function-local variables. * Fixed CondExp - bool ? true : false --- gen/toir.c | 28 +++++++++++++++------------- gen/toobj.c | 10 +++++++--- test/staticvars.d | 13 +++++++++++++ 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 test/staticvars.d diff --git a/gen/toir.c b/gen/toir.c index 55abf9bc..043e9d48 100644 --- a/gen/toir.c +++ b/gen/toir.c @@ -46,19 +46,22 @@ elem* DeclarationExp::toElem(IRState* p) { Logger::println("VarDeclaration"); - // handle const - // TODO probably not correct - bool isconst = (vd->storage_class & STCconst) != 0; + if (vd->isDataseg()) + { + vd->toObjFile(); + } + else + { + // allocate storage on the stack + Logger::println("vdtype = %s", vd->type->toChars()); + const llvm::Type* lltype = LLVM_DtoType(vd->type); + llvm::AllocaInst* allocainst = new llvm::AllocaInst(lltype, vd->toChars(), p->topallocapoint()); + //allocainst->setAlignment(vd->type->alignsize()); // TODO + vd->llvmValue = allocainst; + // e->val = really needed?? - // allocate storage on the stack - Logger::println("vdtype = %s", vd->type->toChars()); - const llvm::Type* lltype = LLVM_DtoType(vd->type); - llvm::AllocaInst* allocainst = new llvm::AllocaInst(lltype, vd->toChars(), p->topallocapoint()); - //allocainst->setAlignment(vd->type->alignsize()); // TODO - vd->llvmValue = allocainst; - // e->val = really needed?? - - LLVM_DtoInitializer(vd->type, vd->init); + LLVM_DtoInitializer(vd->type, vd->init); + } } // struct declaration else if (StructDeclaration* s = declaration->isStructDeclaration()) @@ -2209,7 +2212,6 @@ elem* CondExp::toElem(IRState* p) p->scope() = IRScope(condtrue, condfalse); elem* u = e1->toElem(p); - Logger::cout() << *u->val << '|' << *resval << '\n'; \ new llvm::StoreInst(u->getValue(),resval,p->scopebb()); new llvm::BranchInst(condend,p->scopebb()); delete u; diff --git a/gen/toobj.c b/gen/toobj.c index d684d7b0..772f714a 100644 --- a/gen/toobj.c +++ b/gen/toobj.c @@ -505,9 +505,13 @@ void VarDeclaration::toObjFile() if (isDataseg()) { bool _isconst = isConst(); - if (!_isconst) - _isconst = (storage_class & STCconst) ? true : false; // doesn't seem to work ): - llvm::GlobalValue::LinkageTypes _linkage = LLVM_DtoLinkage(protection, storage_class); + + llvm::GlobalValue::LinkageTypes _linkage; + if (parent->isFuncDeclaration()) + _linkage = llvm::GlobalValue::InternalLinkage; + else + _linkage = LLVM_DtoLinkage(protection, storage_class); + const llvm::Type* _type = LLVM_DtoType(type); assert(_type); diff --git a/test/staticvars.d b/test/staticvars.d new file mode 100644 index 00000000..0a901a02 --- /dev/null +++ b/test/staticvars.d @@ -0,0 +1,13 @@ +module staticvars; + +int func() +{ + static int i; + return i++; +} + +void main() +{ + assert(func() == 0); + assert(func() == 1); +}