Mercurial > projects > ldc
annotate tools/binding/llvm/executionengine.d @ 1621:fb2e6707ad17
Merge DMD r314+r315: bugzilla 2029 Typesafe variadic functions don't...
Both DMD revisions are for fixing bugzilla 2029 (Typesafe variadic
functions don't work in CTFE).
The DMD r314 commit message is:
bugzilla 2029 (Typesafe variadic functions don't work in CTFE
The DMD r315 commit message is:
bugzilla 2029 - try again
---
dmd/constfold.c | 11 ++++-
dmd/declaration.c | 21 +++++++++-
dmd/declaration.h | 10 ++++-
dmd/expression.c | 1 +
dmd/interpret.c | 111 +++++++++++++++++++++++++++++++++++++++++++++--------
dmd/mars.h | 2 +-
dmd/mtype.c | 2 +-
7 files changed, 135 insertions(+), 23 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:22 -0300 |
parents | 4ff9ab0d472c |
children |
rev | line source |
---|---|
1273
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
1 // Written in the D programming language by Frits van Bommel 2008 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
2 // Binding of llvm.c.ExecutionEngine for D. |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
3 // |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
4 // This file is distributed under the University of Illinois Open Source |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
5 // License. See LICENSE.TXT for details. |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
6 // |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
7 module llvm.executionengine; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
8 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
9 import llvm.c.Core; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
10 import llvm.c.ExecutionEngine; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
11 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
12 import llvm.llvm; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
13 import llvm.util; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
14 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
15 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
16 class GenericValue |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
17 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
18 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
19 private LLVMGenericValueRef value; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
20 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
21 private this(LLVMGenericValueRef v) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
22 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
23 value = v; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
24 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
25 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
26 void dispose() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
27 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
28 LLVMDisposeGenericValue(value); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
29 value = null; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
30 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
31 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
32 ~this() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
33 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
34 dispose(); // safe because value isn't on the GC heap and isn't exposed. |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
35 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
36 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
37 static GenericValue GetS(IntegerType ty, long N) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
38 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
39 return new GenericValue(LLVMCreateGenericValueOfInt(ty.ll, N, true)); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
40 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
41 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
42 static GenericValue GetU(IntegerType ty, ulong N) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
43 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
44 return new GenericValue(LLVMCreateGenericValueOfInt(ty.ll, N, false)); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
45 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
46 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
47 static GenericValue GetP(void* P) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
48 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
49 return new GenericValue(LLVMCreateGenericValueOfPointer(P)); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
50 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
51 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
52 static GenericValue GetF(RealType ty, double N) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
53 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
54 return new GenericValue(LLVMCreateGenericValueOfFloat(ty.ll, N)); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
55 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
56 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
57 uint intWidth() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
58 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
59 return LLVMGenericValueIntWidth(value); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
60 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
61 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
62 ulong toUInt() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
63 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
64 return LLVMGenericValueToInt(value, false); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
65 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
66 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
67 long toSInt() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
68 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
69 return LLVMGenericValueToInt(value, true); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
70 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
71 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
72 void* toPointer() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
73 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
74 return LLVMGenericValueToPointer(value); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
75 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
76 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
77 double toFloat(RealType ty) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
78 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
79 return LLVMGenericValueToFloat(ty.ll, value); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
80 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
81 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
82 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
83 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
84 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
85 class ExecutionEngine |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
86 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
87 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
88 private LLVMExecutionEngineRef ee; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
89 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
90 private this(LLVMExecutionEngineRef ee) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
91 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
92 this.ee = ee; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
93 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
94 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
95 static ExecutionEngine Create(ModuleProvider mp) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
96 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
97 LLVMExecutionEngineRef ee; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
98 char* err; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
99 if (LLVMCreateExecutionEngine(&ee, mp.ll, &err)) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
100 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
101 auto errmsg = from_stringz(err).dup; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
102 LLVMDisposeMessage(err); |
1274
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
103 if (errmsg.length == 0) |
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
104 errmsg = "Error creating execution engine"; |
1273
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
105 throw new LLVMException(errmsg); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
106 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
107 return new ExecutionEngine(ee); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
108 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
109 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
110 static ExecutionEngine CreateInterpreter(ModuleProvider mp) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
111 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
112 LLVMExecutionEngineRef ee; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
113 char* err; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
114 if (LLVMCreateInterpreter(&ee, mp.ll, &err)) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
115 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
116 auto errmsg = from_stringz(err).dup; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
117 LLVMDisposeMessage(err); |
1274
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
118 if (errmsg.length == 0) |
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
119 errmsg = "Error creating interpreter"; |
1273
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
120 throw new LLVMException(errmsg); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
121 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
122 return new ExecutionEngine(ee); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
123 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
124 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
125 static ExecutionEngine CreateJIT(ModuleProvider mp) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
126 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
127 LLVMExecutionEngineRef ee; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
128 char* err; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
129 if (LLVMCreateJITCompiler(&ee, mp.ll, &err)) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
130 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
131 auto errmsg = from_stringz(err).dup; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
132 LLVMDisposeMessage(err); |
1274
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
133 if (errmsg.length == 0) |
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
134 errmsg = "Error creating JIT"; |
1273
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
135 throw new LLVMException(errmsg); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
136 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
137 return new ExecutionEngine(ee); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
138 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
139 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
140 void dispose() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
141 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
142 LLVMDisposeExecutionEngine(ee); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
143 ee = null; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
144 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
145 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
146 ~this() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
147 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
148 dispose(); // safe because ee isn't on the GC heap and isn't exposed. |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
149 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
150 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
151 void runStaticConstructors() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
152 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
153 LLVMRunStaticConstructors(ee); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
154 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
155 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
156 void runStaticDestructors() |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
157 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
158 LLVMRunStaticDestructors(ee); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
159 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
160 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
161 int runAsMain(Function f, char[][] args = null, char[][] env = null) { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
162 auto argv = new char*[args.length]; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
163 foreach (size_t idx, ref arg; args) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
164 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
165 argv[idx] = to_stringz(arg); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
166 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
167 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
168 auto envp = new char*[env.length + 1]; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
169 foreach (size_t idx, ref envvar ; env) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
170 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
171 envp[idx] = to_stringz(envvar); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
172 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
173 envp[$-1] = null; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
174 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
175 return LLVMRunFunctionAsMain(ee, f.value, argv.length, argv.ptr, envp.ptr); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
176 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
177 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
178 GenericValue run(Function f, GenericValue[] args = null) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
179 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
180 auto cargs = new LLVMGenericValueRef[args.length]; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
181 foreach (size_t idx, ref arg ; args) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
182 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
183 cargs[idx] = arg.value; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
184 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
185 |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
186 auto result = LLVMRunFunction(ee, f.value, cargs.length, cargs.ptr); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
187 return new GenericValue(result); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
188 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
189 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
190 void freeMachineCodeForFunction(Function f) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
191 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
192 LLVMFreeMachineCodeForFunction(ee, f.value); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
193 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
194 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
195 void addModuleProvider(ModuleProvider mp) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
196 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
197 LLVMAddModuleProvider(ee, mp.ll); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
198 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
199 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
200 Module removeModuleProvider(ModuleProvider mp) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
201 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
202 LLVMModuleRef mod; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
203 char* err; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
204 if (LLVMRemoveModuleProvider(ee, mp.ll, &mod, &err)) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
205 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
206 auto errmsg = from_stringz(err).dup; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
207 LLVMDisposeMessage(err); |
1274
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
208 if (errmsg.length == 0) |
4ff9ab0d472c
Check in some old changes to the binding, from before I had commit access.
Frits van Bommel <fvbommel wxs.nl>
parents:
1273
diff
changeset
|
209 errmsg = "Error removing ModuleProvider from ExecutionEngine"; |
1273
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
210 throw new LLVMException(errmsg); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
211 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
212 return Module.GetExisting(mod); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
213 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
214 /// |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
215 Function findFunction(char[] name) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
216 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
217 LLVMValueRef fn; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
218 if (LLVMFindFunction(ee, to_stringz(name), &fn)) |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
219 { |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
220 return null; |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
221 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
222 return new Function(fn, getTypeOf(fn)); |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
223 } |
1ba61de8796b
Committing LLVM binding for D as it currently exists in the SVN repository.
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
224 } |