Mercurial > projects > ldc
annotate gen/runtime.c @ 54:28e99b04a132 trunk
[svn r58] Fixed cond expression resulting in a non-basic type.
Fixed identity expression for dynamic arrays.
Revamped the system to keep track of lvalues and rvalues and their relations.
Typedef declaration now generate the custom typeinfo.
Other bugfixes.
author | lindquist |
---|---|
date | Wed, 24 Oct 2007 01:37:34 +0200 |
parents | 0c77619e803b |
children | a9d29e9f1fed |
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 | |
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 } |