comparison gen/runtime.cpp @ 86:fd32135dca3e trunk

[svn r90] Major updates to the gen directory. Redesigned the 'elem' struct. Much more... !!! Lots of bugfixes. Added support for special foreach on strings. Added std.array, std.utf, std.ctype and std.uni to phobos. Changed all the .c files in the gen dir to .cpp (it *is* C++ after all)
author lindquist
date Sat, 03 Nov 2007 14:44:58 +0100
parents gen/runtime.c@a9d29e9f1fed
children 6789050b5ad1
comparison
equal deleted inserted replaced
85:f869c636a113 86:fd32135dca3e
1 #include <cassert>
2
3 #include "gen/llvm.h"
4 #include "llvm/Module.h"
5 #include "llvm/Bitcode/ReaderWriter.h"
6 #include "llvm/Support/MemoryBuffer.h"
7
8 #include "root.h"
9 #include "mars.h"
10
11 #include "gen/runtime.h"
12 #include "gen/logger.h"
13
14 static llvm::Module* M = NULL;
15 static bool runtime_failed = false;
16
17 //////////////////////////////////////////////////////////////////////////////////////////////////
18
19 bool LLVM_D_InitRuntime()
20 {
21 Logger::println("*** Loading D runtime ***");
22 LOG_SCOPE;
23
24 if (!global.params.runtimeImppath) {
25 error("You must set the runtime import path with -E");
26 fatal();
27 }
28 std::string filename(global.params.runtimeImppath);
29 filename.append("/llvmdcore.bc");
30 llvm::MemoryBuffer* buffer = llvm::MemoryBuffer::getFile(filename.c_str(), filename.length());
31 if (!buffer) {
32 Logger::println("Failed to load runtime library from disk");
33 runtime_failed = true;
34 return false;
35 }
36
37 std::string errstr;
38 bool retval = false;
39 M = llvm::ParseBitcodeFile(buffer, &errstr);
40 if (M) {
41 retval = true;
42 }
43 else {
44 Logger::println("Failed to load runtime: %s", errstr.c_str());
45 runtime_failed = true;
46 }
47
48 delete buffer;
49 return retval;
50 }
51
52 void LLVM_D_FreeRuntime()
53 {
54 if (M) {
55 Logger::println("*** Freeing D runtime ***");
56 delete M;
57 }
58 }
59
60 //////////////////////////////////////////////////////////////////////////////////////////////////
61
62 llvm::Function* LLVM_D_GetRuntimeFunction(llvm::Module* target, const char* name)
63 {
64 // TODO maybe check the target module first, to allow overriding the runtime on a pre module basis?
65 // could be done and seems like it could be neat too :)
66
67 if (global.params.noruntime) {
68 error("No implicit runtime calls allowed with -noruntime option enabled");
69 fatal();
70 }
71
72 if (!M) {
73 assert(!runtime_failed);
74 LLVM_D_InitRuntime();
75 }
76
77 llvm::Function* fn = M->getFunction(name);
78 if (!fn) {
79 error("Runtime function '%s' was not found", name);
80 fatal();
81 //return NULL;
82 }
83
84 const llvm::FunctionType* fnty = fn->getFunctionType();
85 return llvm::cast<llvm::Function>(target->getOrInsertFunction(name, fnty));
86 }
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 llvm::GlobalVariable* gv = target->getNamedGlobal(name);
96 if (gv) {
97 return gv;
98 }
99
100 if (global.params.noruntime) {
101 error("No implicit runtime calls allowed with -noruntime option enabled");
102 fatal();
103 }
104
105 if (!M) {
106 assert(!runtime_failed);
107 LLVM_D_InitRuntime();
108 }
109
110 llvm::GlobalVariable* g = M->getNamedGlobal(name);
111 if (!g) {
112 error("Runtime global '%s' was not found", name);
113 fatal();
114 //return NULL;
115 }
116
117 const llvm::PointerType* t = g->getType();
118 return new llvm::GlobalVariable(t->getElementType(),g->isConstant(),g->getLinkage(),NULL,g->getName(),target);
119 }