changeset 683:b411c41a9716

Only allocate the module file name once. Fixes #90.
author Christian Kamm <kamm incasoftware de>
date Sun, 12 Oct 2008 10:35:16 +0200
parents 518b8cc84369
children f1560a529987
files gen/arrays.cpp gen/llvmhelpers.cpp gen/statements.cpp gen/todebug.cpp gen/toobj.cpp ir/irmodule.cpp ir/irmodule.h
diffstat 7 files changed, 29 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/gen/arrays.cpp	Sat Oct 11 22:44:17 2008 +0200
+++ b/gen/arrays.cpp	Sun Oct 12 10:35:16 2008 +0200
@@ -1,6 +1,7 @@
 #include "gen/llvm.h"
 
 #include "mtype.h"
+#include "module.h"
 #include "dsymbol.h"
 #include "aggregate.h"
 #include "declaration.h"
@@ -13,6 +14,7 @@
 #include "gen/runtime.h"
 #include "gen/logger.h"
 #include "gen/dvalue.h"
+#include "ir/irmodule.h"
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
@@ -1072,25 +1074,11 @@
     llvm::AttrListPtr palist;
 
     // file param
-    // FIXME: every array bounds check creates a global for the filename !!!
-    LLConstant* c = DtoConstString(loc.filename);
-
-    llvm::AllocaInst* alloc = gIR->func()->srcfileArg;
-    if (!alloc)
-    {
-        alloc = DtoAlloca(c->getType(), ".srcfile");
-        gIR->func()->srcfileArg = alloc;
-    }
-    LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp");
-    DtoStore(c->getOperand(0), ptr);
-    ptr = DtoGEPi(alloc, 0,1, "tmp");
-    DtoStore(c->getOperand(1), ptr);
-
-    args.push_back(alloc);
+    args.push_back(gIR->dmodule->ir.irModule->fileName);
     palist = palist.addAttr(1, llvm::Attribute::ByVal);
 
     // line param
-    c = DtoConstUint(loc.linnum);
+    LLConstant* c = DtoConstUint(loc.linnum);
     args.push_back(c);
 
     // call
--- a/gen/llvmhelpers.cpp	Sat Oct 11 22:44:17 2008 +0200
+++ b/gen/llvmhelpers.cpp	Sun Oct 12 10:35:16 2008 +0200
@@ -6,6 +6,7 @@
 #include "id.h"
 #include "expression.h"
 #include "template.h"
+#include "module.h"
 
 #include "gen/tollvm.h"
 #include "gen/llvmhelpers.h"
@@ -19,6 +20,7 @@
 #include "gen/functions.h"
 #include "gen/typeinf.h"
 #include "gen/todebug.h"
+#include "ir/irmodule.h"
 
 #include <stack>
 
@@ -110,7 +112,6 @@
 void DtoAssert(Loc* loc, DValue* msg)
 {
     std::vector<LLValue*> args;
-    LLConstant* c;
 
     // func
     const char* fname = msg ? "_d_assert_msg" : "_d_assert";
@@ -120,9 +121,6 @@
     llvm::AttrListPtr palist;
     int idx = 1;
 
-    // FIXME: every assert creates a global for the filename !!!
-    c = DtoConstString(loc->filename);
-
     // msg param
     if (msg)
     {
@@ -131,7 +129,7 @@
             llvm::AllocaInst* alloc = gIR->func()->msgArg;
             if (!alloc)
             {
-                alloc = DtoAlloca(c->getType(), ".assertmsg");
+                alloc = DtoAlloca(DtoArrayType(LLType::Int8Ty), ".assertmsg");
                 DtoSetArray(alloc, DtoArrayLen(s), DtoArrayPtr(s));
                 gIR->func()->msgArg = alloc;
             }
@@ -145,23 +143,12 @@
     }
 
     // file param
-    llvm::AllocaInst* alloc = gIR->func()->srcfileArg;
-    if (!alloc)
-    {
-        alloc = DtoAlloca(c->getType(), ".srcfile");
-        gIR->func()->srcfileArg = alloc;
-    }
-    LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp");
-    DtoStore(c->getOperand(0), ptr);
-    ptr = DtoGEPi(alloc, 0,1, "tmp");
-    DtoStore(c->getOperand(1), ptr);
-
-    args.push_back(alloc);
+    args.push_back(gIR->dmodule->ir.irModule->fileName);
     palist = palist.addAttr(idx++, llvm::Attribute::ByVal);
 
 
     // line param
-    c = DtoConstUint(loc->linnum);
+    LLConstant* c = DtoConstUint(loc->linnum);
     args.push_back(c);
 
     // call
--- a/gen/statements.cpp	Sat Oct 11 22:44:17 2008 +0200
+++ b/gen/statements.cpp	Sun Oct 12 10:35:16 2008 +0200
@@ -16,6 +16,7 @@
 #include "mtype.h"
 #include "hdrgen.h"
 #include "port.h"
+#include "module.h"
 
 #include "gen/irstate.h"
 #include "gen/logger.h"
@@ -27,6 +28,7 @@
 #include "gen/dvalue.h"
 
 #include "ir/irfunction.h"
+#include "ir/irmodule.h"
 #include "ir/irlandingpad.h"
 
 //////////////////////////////////////////////////////////////////////////////
@@ -1270,27 +1272,13 @@
     int idx = 1;
 
     std::vector<LLValue*> args;
-    LLConstant* c;
 
     // file param
-    // FIXME: every use creates a global for the filename !!!
-    c = DtoConstString(loc.filename);
-    llvm::AllocaInst* alloc = gIR->func()->srcfileArg;
-    if (!alloc)
-    {
-        alloc = DtoAlloca(c->getType(), ".srcfile");
-        gIR->func()->srcfileArg = alloc;
-    }
-    LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp");
-    DtoStore(c->getOperand(0), ptr);
-    ptr = DtoGEPi(alloc, 0,1, "tmp");
-    DtoStore(c->getOperand(1), ptr);
-
-    args.push_back(alloc);
+    args.push_back(gIR->dmodule->ir.irModule->fileName);
     palist = palist.addAttr(idx++, llvm::Attribute::ByVal);
 
     // line param
-    c = DtoConstUint(loc.linnum);
+    LLConstant* c = DtoConstUint(loc.linnum);
     args.push_back(c);
 
     // call
--- a/gen/todebug.cpp	Sat Oct 11 22:44:17 2008 +0200
+++ b/gen/todebug.cpp	Sun Oct 12 10:35:16 2008 +0200
@@ -628,7 +628,7 @@
 
     // we might be generating for an import
     if (!m->ir.irModule)
-        m->ir.irModule = new IrModule(m);
+        m->ir.irModule = new IrModule(m, m->srcfile->toChars());
     else if (m->ir.irModule->dwarfCompileUnit)
     {
         if (m->ir.irModule->dwarfCompileUnit->getParent() == gIR->module)
--- a/gen/toobj.cpp	Sat Oct 11 22:44:17 2008 +0200
+++ b/gen/toobj.cpp	Sun Oct 12 10:35:16 2008 +0200
@@ -89,16 +89,17 @@
     IrDsymbol::resetAll();
     IrType::resetAll();
 
-    // module ir state
-    // might already exist via import, just overwrite...
-    this->ir.irModule = new IrModule(this);
-
     // name the module
     std::string mname(toChars());
     if (md != 0)
         mname = md->toChars();
     ir.module = new llvm::Module(mname);
 
+    // module ir state
+    // might already exist via import, just overwrite...
+    //FIXME: is there a good reason for overwriting?
+    this->ir.irModule = new IrModule(this, srcfile->toChars());
+
     // set target stuff
     std::string target_triple(global.params.tt_arch);
     target_triple.append(global.params.tt_os);
--- a/ir/irmodule.cpp	Sat Oct 11 22:44:17 2008 +0200
+++ b/ir/irmodule.cpp	Sun Oct 12 10:35:16 2008 +0200
@@ -1,9 +1,16 @@
 #include "gen/llvm.h"
+#include "gen/tollvm.h"
+#include "gen/irstate.h"
 #include "ir/irmodule.h"
 
-IrModule::IrModule(Module* module)
+IrModule::IrModule(Module* module, const char* srcfilename)
 {
     M = module;
+
+    LLConstant* slice = DtoConstString(srcfilename);
+    fileName = new llvm::GlobalVariable(
+        slice->getType(), true, LLGlobalValue::InternalLinkage, slice, ".modulefilename", gIR->module);
+
     dwarfCompileUnit = NULL;
 }
 
--- a/ir/irmodule.h	Sat Oct 11 22:44:17 2008 +0200
+++ b/ir/irmodule.h	Sun Oct 12 10:35:16 2008 +0200
@@ -7,12 +7,13 @@
 
 struct IrModule : IrBase
 {
-    IrModule(Module* module);
+    IrModule(Module* module, const char* srcfilename);
     virtual ~IrModule();
 
     Module* M;
 
     LLGlobalVariable* dwarfCompileUnit;
+    LLGlobalVariable* fileName;
 };
 
 #endif