comparison gen/runtime.c @ 52:0c77619e803b trunk

[svn r56] Initial support for TypeInfo. Enums not work. Several other bugfixes.
author lindquist
date Tue, 23 Oct 2007 05:55:12 +0200
parents 8b0e809563df
children a9d29e9f1fed
comparison
equal deleted inserted replaced
51:61bc1b4ad3c4 52:0c77619e803b
1 #include <cassert> 1 #include <cassert>
2 2
3 #include "gen/llvm.h"
3 #include "llvm/Module.h" 4 #include "llvm/Module.h"
4 #include "llvm/Bitcode/ReaderWriter.h" 5 #include "llvm/Bitcode/ReaderWriter.h"
5 #include "llvm/Support/MemoryBuffer.h" 6 #include "llvm/Support/MemoryBuffer.h"
6 7
7 #include "root.h" 8 #include "root.h"
10 #include "gen/runtime.h" 11 #include "gen/runtime.h"
11 #include "gen/logger.h" 12 #include "gen/logger.h"
12 13
13 static llvm::Module* M = NULL; 14 static llvm::Module* M = NULL;
14 static bool runtime_failed = false; 15 static bool runtime_failed = false;
16
17 //////////////////////////////////////////////////////////////////////////////////////////////////
15 18
16 bool LLVM_D_InitRuntime() 19 bool LLVM_D_InitRuntime()
17 { 20 {
18 Logger::println("*** Loading D runtime ***"); 21 Logger::println("*** Loading D runtime ***");
19 LOG_SCOPE; 22 LOG_SCOPE;
39 } 42 }
40 else { 43 else {
41 Logger::println("Failed to load runtime: %s", errstr.c_str()); 44 Logger::println("Failed to load runtime: %s", errstr.c_str());
42 runtime_failed = true; 45 runtime_failed = true;
43 } 46 }
44 47
45 delete buffer; 48 delete buffer;
46 return retval; 49 return retval;
47 } 50 }
48 51
49 void LLVM_D_FreeRuntime() 52 void LLVM_D_FreeRuntime()
52 Logger::println("*** Freeing D runtime ***"); 55 Logger::println("*** Freeing D runtime ***");
53 delete M; 56 delete M;
54 } 57 }
55 } 58 }
56 59
60 //////////////////////////////////////////////////////////////////////////////////////////////////
61
57 llvm::Function* LLVM_D_GetRuntimeFunction(llvm::Module* target, const char* name) 62 llvm::Function* LLVM_D_GetRuntimeFunction(llvm::Module* target, const char* name)
58 { 63 {
59 // TODO maybe check the target module first, to allow overriding the runtime on a pre module basis? 64 // TODO maybe check the target module first, to allow overriding the runtime on a pre module basis?
60 // could be done and seems like it could be neat too :) 65 // could be done and seems like it could be neat too :)
61 66
62 if (global.params.noruntime) { 67 if (global.params.noruntime) {
63 error("No implicit runtime calls allowed with -noruntime option enabled"); 68 error("No implicit runtime calls allowed with -noruntime option enabled");
64 fatal(); 69 fatal();
65 } 70 }
66 71
67 if (!M) { 72 if (!M) {
68 assert(!runtime_failed); 73 assert(!runtime_failed);
69 LLVM_D_InitRuntime(); 74 LLVM_D_InitRuntime();
70 } 75 }
71 76
72 llvm::Function* fn = M->getFunction(name); 77 llvm::Function* fn = M->getFunction(name);
73 if (!fn) { 78 if (!fn) {
74 error("Runtime function '%s' was not found", name); 79 error("Runtime function '%s' was not found", name);
75 fatal(); 80 fatal();
76 //return NULL; 81 //return NULL;
77 } 82 }
78 83
79 const llvm::FunctionType* fnty = fn->getFunctionType(); 84 const llvm::FunctionType* fnty = fn->getFunctionType();
80 return llvm::cast<llvm::Function>(target->getOrInsertFunction(name, fnty)); 85 return llvm::cast<llvm::Function>(target->getOrInsertFunction(name, fnty));
81 } 86 }
82 87
88 //////////////////////////////////////////////////////////////////////////////////////////////////
89
90 llvm::GlobalVariable* LLVM_D_GetRuntimeGlobal(llvm::Module* target, const char* name)
91 {
92 // TODO maybe check the target module first, to allow overriding the runtime on a pre module basis?
93 // could be done and seems like it could be neat too :)
94
95 if (global.params.noruntime) {
96 error("No implicit runtime calls allowed with -noruntime option enabled");
97 fatal();
98 }
99
100 if (!M) {
101 assert(!runtime_failed);
102 LLVM_D_InitRuntime();
103 }
104
105 llvm::GlobalVariable* g = M->getNamedGlobal(name);
106 if (!g) {
107 error("Runtime global '%s' was not found", name);
108 fatal();
109 //return NULL;
110 }
111
112 const llvm::PointerType* t = g->getType();
113 return new llvm::GlobalVariable(t->getElementType(),g->isConstant(),g->getLinkage(),NULL,g->getName(),target);
114 }