Mercurial > projects > ldc
annotate gen/runtime.c @ 68:c4b3f5d2cd9b trunk
[svn r72] Calling a nested function that is not a delegate was not working.
author | lindquist |
---|---|
date | Sun, 28 Oct 2007 03:14:29 +0100 |
parents | a9d29e9f1fed |
children |
rev | line source |
---|---|
1 | 1 #include <cassert> |
2 | |
52 | 3 #include "gen/llvm.h" |
1 | 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 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
11 #include "gen/runtime.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
12 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
13 |
1 | 14 static llvm::Module* M = NULL; |
15 static bool runtime_failed = false; | |
16 | |
52 | 17 ////////////////////////////////////////////////////////////////////////////////////////////////// |
18 | |
1 | 19 bool LLVM_D_InitRuntime() |
20 { | |
21 Logger::println("*** Loading D runtime ***"); | |
22 LOG_SCOPE; | |
23 | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
4
diff
changeset
|
24 if (!global.params.runtimeImppath) { |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
4
diff
changeset
|
25 error("You must set the runtime import path with -E"); |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
4
diff
changeset
|
26 fatal(); |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
4
diff
changeset
|
27 } |
1 | 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 } | |
52 | 47 |
1 | 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 | |
52 | 60 ////////////////////////////////////////////////////////////////////////////////////////////////// |
61 | |
1 | 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 } | |
52 | 71 |
1 | 72 if (!M) { |
73 assert(!runtime_failed); | |
74 LLVM_D_InitRuntime(); | |
75 } | |
52 | 76 |
1 | 77 llvm::Function* fn = M->getFunction(name); |
40 | 78 if (!fn) { |
79 error("Runtime function '%s' was not found", name); | |
80 fatal(); | |
81 //return NULL; | |
82 } | |
52 | 83 |
1 | 84 const llvm::FunctionType* fnty = fn->getFunctionType(); |
85 return llvm::cast<llvm::Function>(target->getOrInsertFunction(name, fnty)); | |
86 } | |
40 | 87 |
52 | 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 | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
95 llvm::GlobalVariable* gv = target->getNamedGlobal(name); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
96 if (gv) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
97 return gv; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
98 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
99 |
52 | 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 } |