diff gen/functions.cpp @ 156:ccd07d9f2ce9 trunk

[svn r172] moving all IR state previously stored in Dsymbol into IrDsymbol and a Dsymbol->IrDsymbol map
author ChristianK
date Thu, 01 May 2008 13:05:53 +0200
parents 7f92f477ff53
children 5c17f81fc1c1
line wrap: on
line diff
--- a/gen/functions.cpp	Tue Apr 29 21:33:50 2008 +0200
+++ b/gen/functions.cpp	Thu May 01 13:05:53 2008 +0200
@@ -74,10 +74,10 @@
         ClassDeclaration* ti = Type::typeinfo;
         ti->toObjFile();
         DtoForceConstInitDsymbol(ti);
-        assert(ti->irStruct->constInit);
+        assert(gIR->irDsymbol[ti].irStruct->constInit);
         std::vector<const llvm::Type*> types;
         types.push_back(DtoSize_t());
-        types.push_back(getPtrToType(getPtrToType(ti->irStruct->constInit->getType())));
+        types.push_back(getPtrToType(getPtrToType(gIR->irDsymbol[ti].irStruct->constInit->getType())));
         const llvm::Type* t1 = llvm::StructType::get(types);
         paramvec.push_back(getPtrToType(t1));
         paramvec.push_back(getPtrToType(llvm::Type::Int8Ty));
@@ -232,7 +232,7 @@
     llvm::Function* func = llvm::dyn_cast<llvm::Function>(fn);
     assert(func);
     assert(func->isIntrinsic());
-    gIR->irFunc[fdecl]->func = func;
+    gIR->irDsymbol[fdecl].irFunc->func = func;
     return func;
 }
 
@@ -251,8 +251,8 @@
             return;
     }
 
-    if (fdecl->llvmResolved) return;
-    fdecl->llvmResolved = true;
+    if (gIR->irDsymbol[fdecl].resolved) return;
+    gIR->irDsymbol[fdecl].resolved = true;
 
     Logger::println("DtoResolveFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars());
     LOG_SCOPE;
@@ -272,9 +272,9 @@
         {
             Logger::println("magic va_arg found");
             fdecl->llvmInternal = LLVMva_arg;
-            fdecl->llvmDeclared = true;
-            fdecl->llvmInitialized = true;
-            fdecl->llvmDefined = true;
+            gIR->irDsymbol[fdecl].declared = true;
+            gIR->irDsymbol[fdecl].initialized = true;
+            gIR->irDsymbol[fdecl].defined = true;
             return; // this gets mapped to an instruction so a declaration makes no sence
         }
         else if (tempdecl->llvmInternal == LLVMva_start)
@@ -295,8 +295,8 @@
 
 void DtoDeclareFunction(FuncDeclaration* fdecl)
 {
-    if (fdecl->llvmDeclared) return;
-    fdecl->llvmDeclared = true;
+    if (gIR->irDsymbol[fdecl].declared) return;
+    gIR->irDsymbol[fdecl].declared = true;
 
     Logger::println("DtoDeclareFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars());
     LOG_SCOPE;
@@ -311,9 +311,9 @@
 
     if (fdecl->runTimeHack) {
         Logger::println("runtime hack func chars: %s", fdecl->toChars());
-        if (gIR->irFunc.find(fdecl) == gIR->irFunc.end()) {
-            gIR->irFunc[fdecl] = new IrFunction(fdecl);
-            gIR->irFunc[fdecl]->func = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars());
+        if (!gIR->irDsymbol[fdecl].irFunc) {
+            gIR->irDsymbol[fdecl].irFunc = new IrFunction(fdecl);
+            gIR->irDsymbol[fdecl].irFunc->func = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars());
         }
         return;
     }
@@ -330,8 +330,8 @@
     else if (fdecl->llvmInternal == LLVMva_start)
         declareOnly = true;
 
-    if (gIR->irFunc.find(fdecl) == gIR->irFunc.end()) {
-        gIR->irFunc[fdecl] = new IrFunction(fdecl);
+    if (!gIR->irDsymbol[fdecl].irFunc) {
+        gIR->irDsymbol[fdecl].irFunc = new IrFunction(fdecl);
     }
 
     // mangled name
@@ -358,7 +358,7 @@
         assert(func->getFunctionType() == functype);
 
     // add func to IRFunc
-    gIR->irFunc[fdecl]->func = func;
+    gIR->irDsymbol[fdecl].irFunc->func = func;
 
     // calling convention
     if (!vafunc && fdecl->llvmInternal != LLVMintrinsic)
@@ -366,7 +366,7 @@
     else // fall back to C, it should be the right thing to do
         func->setCallingConv(llvm::CallingConv::C);
 
-    gIR->irFunc[fdecl]->func = func;
+    gIR->irDsymbol[fdecl].irFunc->func = func;
     assert(llvm::isa<llvm::FunctionType>(f->llvmType->get()));
 
     // main
@@ -388,22 +388,22 @@
     int k = 0;
     if (f->llvmRetInPtr) {
         iarg->setName("retval");
-        gIR->irFunc[fdecl]->retArg = iarg;
+        gIR->irDsymbol[fdecl].irFunc->retArg = iarg;
         ++iarg;
     }
     if (f->llvmUsesThis) {
         iarg->setName("this");
-        gIR->irFunc[fdecl]->thisVar = iarg;
-        assert(gIR->irFunc[fdecl]->thisVar);
+        gIR->irDsymbol[fdecl].irFunc->thisVar = iarg;
+        assert(gIR->irDsymbol[fdecl].irFunc->thisVar);
         ++iarg;
     }
 
     if (f->linkage == LINKd && f->varargs == 1) {
         iarg->setName("_arguments");
-        gIR->irFunc[fdecl]->_arguments = iarg;
+        gIR->irDsymbol[fdecl].irFunc->_arguments = iarg;
         ++iarg;
         iarg->setName("_argptr");
-        gIR->irFunc[fdecl]->_argptr = iarg;
+        gIR->irDsymbol[fdecl].irFunc->_argptr = iarg;
         ++iarg;
     }
 
@@ -414,15 +414,15 @@
         //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident);
         if (arg && arg->ident != 0) {
             if (arg->vardecl) {
-                if (arg->vardecl->irLocal)
+                if (gIR->irDsymbol[arg->vardecl].irLocal)
                 {
-                    Logger::cout() << "WTF!?!: " << *arg->vardecl->irLocal->value << '\n';
+                    Logger::cout() << "WTF!?!: " << *gIR->irDsymbol[arg->vardecl].irLocal->value << '\n';
                 }
-                assert(!arg->vardecl->irLocal);
-                assert(!arg->vardecl->irGlobal);
-                assert(!arg->vardecl->irField);
-                arg->vardecl->irLocal = new IrLocal(arg->vardecl);
-                arg->vardecl->irLocal->value = iarg;
+                assert(!gIR->irDsymbol[arg->vardecl].irLocal);
+                assert(!gIR->irDsymbol[arg->vardecl].irGlobal);
+                assert(!gIR->irDsymbol[arg->vardecl].irField);
+                gIR->irDsymbol[arg->vardecl].irLocal = new IrLocal(arg->vardecl);
+                gIR->irDsymbol[arg->vardecl].irLocal->value = iarg;
             }
             iarg->setName(arg->ident->toChars());
         }
@@ -446,10 +446,10 @@
 
 void DtoDefineFunc(FuncDeclaration* fd)
 {
-    if (fd->llvmDefined) return;
-    fd->llvmDefined = true;
+    if (gIR->irDsymbol[fd].defined) return;
+    gIR->irDsymbol[fd].defined = true;
 
-    assert(fd->llvmDeclared);
+    assert(gIR->irDsymbol[fd].declared);
 
     Logger::println("DtoDefineFunc(%s): %s", fd->toPrettyChars(), fd->loc.toChars());
     LOG_SCOPE;
@@ -457,27 +457,27 @@
     // debug info
     if (global.params.symdebug) {
         Module* mo = fd->getModule();
-        gIR->irFunc[fd]->dwarfSubProg = DtoDwarfSubProgram(fd, DtoDwarfCompileUnit(mo));
+        gIR->irDsymbol[fd].irFunc->dwarfSubProg = DtoDwarfSubProgram(fd, DtoDwarfCompileUnit(mo));
     }
 
     Type* t = DtoDType(fd->type);
     TypeFunction* f = (TypeFunction*)t;
     assert(f->llvmType);
 
-    llvm::Function* func = gIR->irFunc[fd]->func;
+    llvm::Function* func = gIR->irDsymbol[fd].irFunc->func;
     const llvm::FunctionType* functype = func->getFunctionType();
 
     // only members of the current module or template instances maybe be defined
     if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent))
     {
-        fd->llvmDModule = gIR->dmodule;
+        gIR->irDsymbol[fd].DModule = gIR->dmodule;
 
         // function definition
         if (fd->fbody != 0)
         {
             Logger::println("Doing function body for: %s", fd->toChars());
-            assert(gIR->irFunc.count(fd) != 0);
-            gIR->functions.push_back(gIR->irFunc[fd]);
+            assert(gIR->irDsymbol[fd].irFunc);
+            gIR->functions.push_back(gIR->irDsymbol[fd].irFunc);
 
             if (fd->isMain())
                 gIR->emitMain = true;
@@ -495,8 +495,8 @@
                 // need result variable? (not nested)
                 if (fd->vresult && !fd->vresult->nestedref) {
                     Logger::println("non-nested vresult value");
-                    fd->vresult->irLocal = new IrLocal(fd->vresult);
-                    fd->vresult->irLocal->value = new llvm::AllocaInst(DtoType(fd->vresult->type),"function_vresult",allocaPoint);
+                    gIR->irDsymbol[fd->vresult].irLocal = new IrLocal(fd->vresult);
+                    gIR->irDsymbol[fd->vresult].irLocal->value = new llvm::AllocaInst(DtoType(fd->vresult->type),"function_vresult",allocaPoint);
                 }
 
                 // give arguments storage
@@ -507,14 +507,14 @@
                         VarDeclaration* vd = arg->vardecl;
                         if (!vd->needsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type))
                             continue;
-                        llvm::Value* a = vd->irLocal->value;
+                        llvm::Value* a = gIR->irDsymbol[vd].irLocal->value;
                         assert(a);
                         std::string s(a->getName());
                         Logger::println("giving argument '%s' storage", s.c_str());
                         s.append("_storage");
                         llvm::Value* v = new llvm::AllocaInst(a->getType(),s,allocaPoint);
                         gIR->ir->CreateStore(a,v);
-                        vd->irLocal->value = v;
+                        gIR->irDsymbol[vd].irLocal->value = v;
                     }
                     else {
                         Logger::attention(fd->loc, "some unknown argument: %s", arg ? arg->toChars() : 0);
@@ -527,7 +527,7 @@
                 llvm::Value* parentNested = NULL;
                 if (FuncDeclaration* fd2 = fd->toParent2()->isFuncDeclaration()) {
                     if (!fd->isStatic()) // huh?
-                        parentNested = gIR->irFunc[fd2]->nestedVar;
+                        parentNested = gIR->irDsymbol[fd2].irFunc->nestedVar;
                 }
 
                 // need result variable? (nested)
@@ -547,16 +547,16 @@
                     for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) {
                         VarDeclaration* vd = *i;
                         Logger::println("referenced nested variable %s", vd->toChars());
-                        if (!vd->irLocal)
-                            vd->irLocal = new IrLocal(vd);
-                        vd->irLocal->nestedIndex = j++;
+                        if (!gIR->irDsymbol[vd].irLocal)
+                            gIR->irDsymbol[vd].irLocal = new IrLocal(vd);
+                        gIR->irDsymbol[vd].irLocal->nestedIndex = j++;
                         if (vd->isParameter()) {
-                            if (!vd->irLocal->value) {
+                            if (!gIR->irDsymbol[vd].irLocal->value) {
                                 assert(vd == fd->vthis);
-                                vd->irLocal->value = gIR->irFunc[fd]->thisVar;
+                                gIR->irDsymbol[vd].irLocal->value = gIR->irDsymbol[fd].irFunc->thisVar;
                             }
-                            assert(vd->irLocal->value);
-                            nestTypes.push_back(vd->irLocal->value->getType());
+                            assert(gIR->irDsymbol[vd].irLocal->value);
+                            nestTypes.push_back(gIR->irDsymbol[vd].irLocal->value->getType());
                         }
                         else {
                             nestTypes.push_back(DtoType(vd->type));
@@ -564,18 +564,18 @@
                     }
                     const llvm::StructType* nestSType = llvm::StructType::get(nestTypes);
                     Logger::cout() << "nested var struct has type:" << *nestSType << '\n';
-                    gIR->irFunc[fd]->nestedVar = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint);
+                    gIR->irDsymbol[fd].irFunc->nestedVar = new llvm::AllocaInst(nestSType,"nestedvars",allocaPoint);
                     if (parentNested) {
-                        assert(gIR->irFunc[fd]->thisVar);
-                        llvm::Value* ptr = gIR->ir->CreateBitCast(gIR->irFunc[fd]->thisVar, parentNested->getType(), "tmp");
-                        gIR->ir->CreateStore(ptr, DtoGEPi(gIR->irFunc[fd]->nestedVar, 0,0, "tmp"));
+                        assert(gIR->irDsymbol[fd].irFunc->thisVar);
+                        llvm::Value* ptr = gIR->ir->CreateBitCast(gIR->irDsymbol[fd].irFunc->thisVar, parentNested->getType(), "tmp");
+                        gIR->ir->CreateStore(ptr, DtoGEPi(gIR->irDsymbol[fd].irFunc->nestedVar, 0,0, "tmp"));
                     }
                     for (std::set<VarDeclaration*>::iterator i=fd->nestedVars.begin(); i!=fd->nestedVars.end(); ++i) {
                         VarDeclaration* vd = *i;
                         if (vd->isParameter()) {
-                            assert(vd->irLocal);
-                            gIR->ir->CreateStore(vd->irLocal->value, DtoGEPi(gIR->irFunc[fd]->nestedVar, 0, vd->irLocal->nestedIndex, "tmp"));
-                            vd->irLocal->value = gIR->irFunc[fd]->nestedVar;
+                            assert(gIR->irDsymbol[vd].irLocal);
+                            gIR->ir->CreateStore(gIR->irDsymbol[vd].irLocal->value, DtoGEPi(gIR->irDsymbol[fd].irFunc->nestedVar, 0, gIR->irDsymbol[vd].irLocal->nestedIndex, "tmp"));
+                            gIR->irDsymbol[vd].irLocal->value = gIR->irDsymbol[fd].irFunc->nestedVar;
                         }
                     }
                 }
@@ -583,9 +583,9 @@
                 // copy _argptr to a memory location
                 if (f->linkage == LINKd && f->varargs == 1)
                 {
-                    llvm::Value* argptrmem = new llvm::AllocaInst(gIR->irFunc[fd]->_argptr->getType(), "_argptrmem", gIR->topallocapoint());
-                    new llvm::StoreInst(gIR->irFunc[fd]->_argptr, argptrmem, gIR->scopebb());
-                    gIR->irFunc[fd]->_argptr = argptrmem;
+                    llvm::Value* argptrmem = new llvm::AllocaInst(gIR->irDsymbol[fd].irFunc->_argptr->getType(), "_argptrmem", gIR->topallocapoint());
+                    new llvm::StoreInst(gIR->irDsymbol[fd].irFunc->_argptr, argptrmem, gIR->scopebb());
+                    gIR->irDsymbol[fd].irFunc->_argptr = argptrmem;
                 }
 
                 // output function body