Mercurial > projects > ldc
annotate gen/runtime.c @ 40:8b0e809563df trunk
[svn r44] Lots of bug fixes.
New array literal support
New array ~= operator support (for single element)
New with statement support
More...
author | lindquist |
---|---|
date | Fri, 19 Oct 2007 07:43:21 +0200 |
parents | 3cfcb944304e |
children | 0c77619e803b |
rev | line source |
---|---|
1 | 1 #include <cassert> |
2 | |
3 #include "llvm/Module.h" | |
4 #include "llvm/Bitcode/ReaderWriter.h" | |
5 #include "llvm/Support/MemoryBuffer.h" | |
6 | |
7 #include "root.h" | |
8 #include "mars.h" | |
9 | |
4
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
10 #include "gen/runtime.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
11 #include "gen/logger.h" |
e116aa1488e6
[svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents:
1
diff
changeset
|
12 |
1 | 13 static llvm::Module* M = NULL; |
14 static bool runtime_failed = false; | |
15 | |
16 bool LLVM_D_InitRuntime() | |
17 { | |
18 Logger::println("*** Loading D runtime ***"); | |
19 LOG_SCOPE; | |
20 | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
4
diff
changeset
|
21 if (!global.params.runtimeImppath) { |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
4
diff
changeset
|
22 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
|
23 fatal(); |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
4
diff
changeset
|
24 } |
1 | 25 std::string filename(global.params.runtimeImppath); |
26 filename.append("/llvmdcore.bc"); | |
27 llvm::MemoryBuffer* buffer = llvm::MemoryBuffer::getFile(filename.c_str(), filename.length()); | |
28 if (!buffer) { | |
29 Logger::println("Failed to load runtime library from disk"); | |
30 runtime_failed = true; | |
31 return false; | |
32 } | |
33 | |
34 std::string errstr; | |
35 bool retval = false; | |
36 M = llvm::ParseBitcodeFile(buffer, &errstr); | |
37 if (M) { | |
38 retval = true; | |
39 } | |
40 else { | |
41 Logger::println("Failed to load runtime: %s", errstr.c_str()); | |
42 runtime_failed = true; | |
43 } | |
44 | |
45 delete buffer; | |
46 return retval; | |
47 } | |
48 | |
49 void LLVM_D_FreeRuntime() | |
50 { | |
51 if (M) { | |
52 Logger::println("*** Freeing D runtime ***"); | |
53 delete M; | |
54 } | |
55 } | |
56 | |
57 llvm::Function* LLVM_D_GetRuntimeFunction(llvm::Module* target, const char* name) | |
58 { | |
59 // 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 :) | |
61 | |
62 if (global.params.noruntime) { | |
63 error("No implicit runtime calls allowed with -noruntime option enabled"); | |
64 fatal(); | |
65 } | |
66 | |
67 if (!M) { | |
68 assert(!runtime_failed); | |
69 LLVM_D_InitRuntime(); | |
70 } | |
71 | |
72 llvm::Function* fn = M->getFunction(name); | |
40 | 73 if (!fn) { |
74 error("Runtime function '%s' was not found", name); | |
75 fatal(); | |
76 //return NULL; | |
77 } | |
1 | 78 |
79 const llvm::FunctionType* fnty = fn->getFunctionType(); | |
80 return llvm::cast<llvm::Function>(target->getOrInsertFunction(name, fnty)); | |
81 } | |
40 | 82 |