Mercurial > projects > ldc
annotate gen/runtime.cpp @ 109:5ab8e92611f9 trunk
[svn r113] Added initial support for associative arrays (AAs).
Fixed some problems with the string runtime support functions.
Fixed initialization of array of structs.
Fixed slice assignment where LHS is slice but RHS is dynamic array.
Fixed problems with result of assignment expressions.
Fixed foreach problems with key type mismatches.
author | lindquist |
---|---|
date | Wed, 21 Nov 2007 04:13:15 +0100 |
parents | 6789050b5ad1 |
children | 1700239cab2e |
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 if (global.params.noruntime) { | |
65 error("No implicit runtime calls allowed with -noruntime option enabled"); | |
66 fatal(); | |
67 } | |
52 | 68 |
1 | 69 if (!M) { |
70 assert(!runtime_failed); | |
71 LLVM_D_InitRuntime(); | |
72 } | |
52 | 73 |
98
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
86
diff
changeset
|
74 llvm::Function* fn = target->getFunction(name); |
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
86
diff
changeset
|
75 if (fn) |
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
86
diff
changeset
|
76 return fn; |
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
86
diff
changeset
|
77 |
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
86
diff
changeset
|
78 fn = M->getFunction(name); |
40 | 79 if (!fn) { |
98
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
86
diff
changeset
|
80 printf("Runtime function '%s' was not found\n", name); |
6789050b5ad1
[svn r102] Further delayed emission of function bodies to avoid problems with circular-forward-references.
lindquist
parents:
86
diff
changeset
|
81 assert(0); |
40 | 82 //return NULL; |
83 } | |
52 | 84 |
1 | 85 const llvm::FunctionType* fnty = fn->getFunctionType(); |
86 return llvm::cast<llvm::Function>(target->getOrInsertFunction(name, fnty)); | |
87 } | |
40 | 88 |
52 | 89 ////////////////////////////////////////////////////////////////////////////////////////////////// |
90 | |
91 llvm::GlobalVariable* LLVM_D_GetRuntimeGlobal(llvm::Module* target, const char* name) | |
92 { | |
93 // TODO maybe check the target module first, to allow overriding the runtime on a pre module basis? | |
94 // could be done and seems like it could be neat too :) | |
95 | |
57
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
96 llvm::GlobalVariable* gv = target->getNamedGlobal(name); |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
97 if (gv) { |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
98 return gv; |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
99 } |
a9d29e9f1fed
[svn r61] Added support for D-style variadic functions :)
lindquist
parents:
52
diff
changeset
|
100 |
52 | 101 if (global.params.noruntime) { |
102 error("No implicit runtime calls allowed with -noruntime option enabled"); | |
103 fatal(); | |
104 } | |
105 | |
106 if (!M) { | |
107 assert(!runtime_failed); | |
108 LLVM_D_InitRuntime(); | |
109 } | |
110 | |
111 llvm::GlobalVariable* g = M->getNamedGlobal(name); | |
112 if (!g) { | |
113 error("Runtime global '%s' was not found", name); | |
114 fatal(); | |
115 //return NULL; | |
116 } | |
117 | |
118 const llvm::PointerType* t = g->getType(); | |
119 return new llvm::GlobalVariable(t->getElementType(),g->isConstant(),g->getLinkage(),NULL,g->getName(),target); | |
120 } |