changeset 1524:b265fb6ce15b

Merge.
author Robert Clipsham <robert@octarineparrot.com>
date Mon, 06 Jul 2009 23:56:11 +0100
parents 833337c65fd3 (current diff) 6766485fb584 (diff)
children d28cd7c45267
files gen/main.cpp gen/runtime.cpp
diffstat 7 files changed, 113 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Mon Jul 06 23:54:02 2009 +0100
+++ b/CMakeLists.txt	Mon Jul 06 23:56:11 2009 +0100
@@ -53,11 +53,9 @@
 	OUTPUT_VARIABLE LLVM_LDFLAGS
 	OUTPUT_STRIP_TRAILING_WHITESPACE
 )
-
-set(EXTRA_LLVM_MODULES "" CACHE STRING "extra llvm components to link in (see llvm-config --components)")
 execute_process(
-	COMMAND ${PERL_EXECUTABLE} ${LLVM_CONFIG} --libfiles bitwriter linker ipo instrumentation backend ${EXTRA_LLVM_MODULES}
-	OUTPUT_VARIABLE LLVM_LIBS
+	COMMAND ${PERL_EXECUTABLE} ${LLVM_CONFIG} --includedir
+	OUTPUT_VARIABLE LLVM_INCLUDEDIR
 	OUTPUT_STRIP_TRAILING_WHITESPACE
 )
 
@@ -133,7 +131,40 @@
 set(DEFAULT_TARGET ${HOST_TARGET} CACHE STRING "default target")
 set(DEFAULT_ALT_TARGET ${HOST_ALT_TARGET} CACHE STRING "default alt target")
 
-include_directories(. ${DMDFE_PATH} ${DMDFE_PATH}/root ${PROJECT_BINARY_DIR}/${DMDFE_PATH} ${PROJECT_BINARY_DIR} ${LLVM_INSTDIR}/include)
+include_directories(. ${DMDFE_PATH} ${DMDFE_PATH}/root ${PROJECT_BINARY_DIR}/${DMDFE_PATH} ${PROJECT_BINARY_DIR} ${LLVM_INCLUDEDIR})
+
+set(EXTRA_LLVM_MODULES "" CACHE STRING "extra llvm components to link in (see llvm-config --components)")
+separate_arguments(EXTRA_LLVM_MODULES)
+execute_process(
+        COMMAND ${PERL_EXECUTABLE} ${LLVM_CONFIG} --libfiles bitwriter linker ipo instrumentation backend ${EXTRA_LLVM_MODULES}
+        OUTPUT_VARIABLE LLVM_LIBS
+        OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+
+# build a define that contains all LLVM targets required and is usable for
+# preprocessor code generation. start with the native target.
+find_path(LLVM_CONFIG_FILE_PATH config.h PATHS ${LLVM_INCLUDEDIR}/llvm/Config ${LLVM_INCLUDEDIR}/Config NO_DEFAULT_PATH)
+if(LLVM_CONFIG_FILE_PATH STREQUAL "LLVM_CONFIG_FILE_PATH-NOTFOUND")
+        message("Couldn't find your llvm Config.h file in ${LLVM_INCLUDEDIR}, no native target will be initialized.")
+else(LLVM_CONFIG_FILE_PATH STREQUAL "LLVM_CONFIG_FILE_PATH-NOTFOUND")
+        file(STRINGS ${LLVM_CONFIG_FILE_PATH}/config.h LLVM_NATIVE_ARCH REGEX "^#define LLVM_NATIVE_ARCH")
+        if(LLVM_NATIVE_ARCH)
+                string(REGEX REPLACE "^#define LLVM_NATIVE_ARCH (.*)Target$" "\\1" LLVM_NATIVE_ARCH ${LLVM_NATIVE_ARCH})
+                message("Found native target ${LLVM_NATIVE_ARCH}")
+                set(LLVM_MODULES_DEFINE "LLVM_TARGET(${LLVM_NATIVE_ARCH})")
+        else(LLVM_NATIVE_ARCH)
+                message("Couldn't find the LLVM_NATIVE_ARCH define in ${LLVM_CONFIG_FILE_PATH}/config.h. Probably you have an older LLVM and can ignore this warning.")
+        endif(LLVM_NATIVE_ARCH)
+endif(LLVM_CONFIG_FILE_PATH STREQUAL "LLVM_CONFIG_FILE_PATH-NOTFOUND")
+# chain the extra target list to the define
+foreach(EXTRA_TARGET ${EXTRA_LLVM_MODULES})
+    set(LLVM_MODULES_DEFINE "${LLVM_MODULES_DEFINE} LLVM_TARGET(${EXTRA_TARGET})")
+endforeach(EXTRA_TARGET)
+set_source_files_properties(
+    ${PROJECT_SOURCE_DIR}/gen/main.cpp PROPERTIES
+    COMPILE_DEFINITIONS LDC_TARGETS=${LLVM_MODULES_DEFINE}
+)
+
 
 file(GLOB FE_SRC ${DMDFE_PATH}/*.c)
 file(GLOB FE_SRC_ROOT ${DMDFE_PATH}/root/*.c)
--- a/gen/arrays.cpp	Mon Jul 06 23:54:02 2009 +0100
+++ b/gen/arrays.cpp	Mon Jul 06 23:56:11 2009 +0100
@@ -398,6 +398,25 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
+static bool isInitialized(Type* et) {
+    // Strip static array types from element type
+    Type* bt = et->toBasetype();
+    while (bt->ty == Tsarray) {
+        et = bt->nextOf();
+        bt = et->toBasetype();
+    }
+    // If it's a typedef with "= void" initializer then don't initialize.
+    if (et->ty == Ttypedef) {
+        Logger::println("Typedef: %s", et->toChars());
+        TypedefDeclaration* tdd = ((TypeTypedef*)et)->sym;
+        if (tdd && tdd->init && tdd->init->isVoidInitializer())
+            return false;
+    }
+    // Otherwise, it's always initialized.
+    return true;
+}
+
+//////////////////////////////////////////////////////////////////////////////////////////
 DSliceValue* DtoNewDynArray(Loc& loc, Type* arrayType, DValue* dim, bool defaultInit)
 {
     Logger::println("DtoNewDynArray : %s", arrayType->toChars());
@@ -411,7 +430,10 @@
     LLValue* arrayLen = dim->getRVal();
 
     // get runtime function
-    bool zeroInit = arrayType->toBasetype()->nextOf()->isZeroInit();
+    Type* eltType = arrayType->toBasetype()->nextOf();
+    if (defaultInit && !isInitialized(eltType))
+        defaultInit = false;
+    bool zeroInit = eltType->isZeroInit();
     const char* fnname = defaultInit ? (zeroInit ? "_d_newarrayT" : "_d_newarrayiT") : "_d_newarrayvT";
     LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);
 
@@ -445,6 +467,8 @@
 
     // get runtime function
     bool zeroInit = vtype->isZeroInit();
+    if (defaultInit && !isInitialized(vtype))
+        defaultInit = false;
     const char* fnname = defaultInit ? (zeroInit ? "_d_newarraymT" : "_d_newarraymiT") : "_d_newarraymvT";
     LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);
 
--- a/gen/llvmhelpers.cpp	Mon Jul 06 23:54:02 2009 +0100
+++ b/gen/llvmhelpers.cpp	Mon Jul 06 23:56:11 2009 +0100
@@ -979,10 +979,12 @@
     else if (AttribDeclaration* a = declaration->isAttribDeclaration())
     {
         Logger::println("AttribDeclaration");
-        if (a->decl)
-            for (int i=0; i < a->decl->dim; ++i)
+        // choose the right set in case this is a conditional declaration
+        Array *d = a->include(NULL, NULL);
+        if (d)
+            for (int i=0; i < d->dim; ++i)
             {
-                DtoDeclarationExp((Dsymbol*)a->decl->data[i]);
+                DtoDeclarationExp((Dsymbol*)d->data[i]);
             }
     }
     // mixin declaration
--- a/gen/main.cpp	Mon Jul 06 23:54:02 2009 +0100
+++ b/gen/main.cpp	Mon Jul 06 23:56:11 2009 +0100
@@ -4,13 +4,20 @@
 // which uses the llvm license
 
 #include "gen/llvm.h"
+#include "gen/llvm-version.h"
 #include "llvm/LinkAllVMCore.h"
 #include "llvm/Linker.h"
+#if LLVM_REV >= 74640
+#include "llvm/LLVMContext.h"
+#endif
 #include "llvm/System/Signals.h"
 #include "llvm/Target/SubtargetFeature.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Target/TargetMachineRegistry.h"
+#if LLVM_REV >= 73610
+#include "llvm/Target/TargetSelect.h"
+#endif
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -397,7 +404,11 @@
     if (global.errors)
         fatal();
 
+#if LLVM_REV >= 74640
+    llvm::Module mod("dummy", llvm::getGlobalContext());
+#else
     llvm::Module mod("dummy");
+#endif
 
     // override triple if needed
     const char* defaultTriple = DEFAULT_TARGET_TRIPLE;
@@ -423,8 +434,17 @@
 
     mod.setTargetTriple(global.params.targetTriple);
 
-    // Allocate target machine.  First, check whether the user has
-    // explicitly specified an architecture to compile for.
+    // Allocate target machine.
+    
+    // first initialize llvm
+#if LLVM_REV >= 73610
+#define LLVM_TARGET(A) LLVMInitialize##A##Target(); LLVMInitialize##A##AsmPrinter();
+// this is defined to be LLVM_TARGET(target name 1) LLVM_TARGET(target name 2) ...
+LDC_TARGETS
+#undef LLVM_TARGET
+#endif
+
+    // Check whether the user has explicitly specified an architecture to compile for.
     if (mArch == 0)
     {
         std::string Err;
@@ -908,7 +928,11 @@
         char* name = m->toChars();
         char* filename = m->objfile->name->str;
         
+#if LLVM_REV >= 74640
+        llvm::Linker linker(name, name, llvm::getGlobalContext());
+#else
         llvm::Linker linker(name, name);
+#endif
         std::string errormsg;
         for (int i = 0; i < llvmModules.size(); i++)
         {
--- a/gen/runtime.cpp	Mon Jul 06 23:54:02 2009 +0100
+++ b/gen/runtime.cpp	Mon Jul 06 23:56:11 2009 +0100
@@ -1,4 +1,8 @@
 #include "gen/llvm.h"
+#include "gen/llvm-version.h"
+#if LLVM_REV >= 74640
+#include "llvm/LLVMContext.h"
+#endif
 #include "llvm/Module.h"
 #include "llvm/Attributes.h"
 #include "llvm/Bitcode/ReaderWriter.h"
@@ -149,7 +153,11 @@
 static void LLVM_D_BuildRuntimeModule()
 {
     Logger::println("building module");
+#if LLVM_REV >= 74640
+    M = new llvm::Module("ldc internal runtime", llvm::getGlobalContext());
+#else
     M = new llvm::Module("ldc internal runtime");
+#endif
 
     Logger::println("building basic types");
     const LLType* voidTy = LLType::VoidTy;
--- a/gen/todebug.cpp	Mon Jul 06 23:54:02 2009 +0100
+++ b/gen/todebug.cpp	Mon Jul 06 23:56:11 2009 +0100
@@ -45,23 +45,6 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////////////
 
-static llvm::DIAnchor getDwarfAnchor(dwarf_constants c)
-{
-    switch (c)
-    {
-    case DW_TAG_compile_unit:
-        return gIR->difactory.GetOrCreateCompileUnitAnchor();
-    case DW_TAG_variable:
-        return gIR->difactory.GetOrCreateGlobalVariableAnchor();
-    case DW_TAG_subprogram:
-        return gIR->difactory.GetOrCreateSubprogramAnchor();
-    default:
-        assert(0);
-    }
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////////////
-
 static const llvm::StructType* getDwarfCompileUnitType() {
     return isaStruct(gIR->module->getTypeByName("llvm.dbg.compile_unit.type"));
 }
@@ -311,7 +294,11 @@
         // set to handle recursive types properly
         gv = emitDwarfGlobalDecl(getDwarfCompositeTypeType(), "llvm.dbg.compositetype");
         // set bogus initializer to satisfy asserts in DICompositeType constructor
-        gv->setInitializer(LLConstant::getNullValue(getDwarfCompositeTypeType()));
+        std::vector<LLConstant*> initvals(11);
+        initvals[0] = DBG_TAG(DW_TAG_structure_type);
+        for (int i = 1; i < initvals.size(); ++i)
+            initvals[i] = LLConstant::getNullValue(getDwarfCompositeTypeType()->getContainedType(i));
+        gv->setInitializer(LLConstantStruct::get(getDwarfCompositeTypeType(), initvals));
         ir->diCompositeType = llvm::DICompositeType(gv);
 
         tag = DW_TAG_structure_type;
--- a/gen/toobj.cpp	Mon Jul 06 23:54:02 2009 +0100
+++ b/gen/toobj.cpp	Mon Jul 06 23:56:11 2009 +0100
@@ -11,8 +11,12 @@
 #include <fstream>
 
 #include "gen/llvm.h"
+#include "gen/llvm-version.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Bitcode/ReaderWriter.h"
+#if LLVM_REV >= 74640
+#include "llvm/LLVMContext.h"
+#endif
 #include "llvm/Module.h"
 #include "llvm/ModuleProvider.h"
 #include "llvm/PassManager.h"
@@ -43,7 +47,6 @@
 #include "gen/functions.h"
 #include "gen/irstate.h"
 #include "gen/llvmhelpers.h"
-#include "gen/llvm-version.h"
 #include "gen/logger.h"
 #include "gen/optimizer.h"
 #include "gen/programs.h"
@@ -94,7 +97,11 @@
 
     // create a new ir state
     // TODO look at making the instance static and moving most functionality into IrModule where it belongs
+#if LLVM_REV >= 74640
+    IRState ir(new llvm::Module(mname, llvm::getGlobalContext()));
+#else
     IRState ir(new llvm::Module(mname));
+#endif
     gIR = &ir;
     ir.dmodule = this;