-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathJITCompiler.java
More file actions
109 lines (86 loc) · 3.6 KB
/
JITCompiler.java
File metadata and controls
109 lines (86 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
*
*/
package org.python.modules.jffi;
import com.kenai.jffi.CallingConvention;
import org.python.core.PyObject;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
/**
*
*/
class JITCompiler {
private final Map<JITSignature, HandleRef>
handles = new HashMap<JITSignature, HandleRef>();
private final Map<Class, JITHandle> classes = Collections.synchronizedMap(new WeakHashMap<Class, JITHandle>());
private final ReferenceQueue referenceQueue = new ReferenceQueue();
private final JITHandle failedHandle = new JITHandle(this,
new JITSignature(NativeType.VOID, new NativeType[0], false, new boolean[0], CallingConvention.DEFAULT, false),
true);
private static class SingletonHolder {
private static final JITCompiler INSTANCE = new JITCompiler();
}
public static JITCompiler getInstance() {
return SingletonHolder.INSTANCE;
}
private static final class HandleRef extends WeakReference<JITHandle> {
JITSignature signature;
public HandleRef(JITHandle handle, JITSignature signature, ReferenceQueue refqueue) {
super(handle, refqueue);
this.signature = signature;
}
}
private void cleanup() {
HandleRef ref;
while ((ref = (HandleRef) referenceQueue.poll()) != null) {
handles.remove(ref.signature);
}
}
JITHandle getHandle(PyObject resultType, PyObject[] parameterTypes, CallingConvention convention, boolean ignoreErrno) {
boolean hasResultConverter = !(resultType instanceof CType.Builtin);
NativeType nativeResultType;
if (resultType instanceof CType.Builtin) {
nativeResultType = ((CType) resultType).getNativeType();
/*
} else if (resultType instanceof MappedType) {
nativeResultType = ((MappedType) resultType).getRealType().getNativeType();
*/
} else {
return failedHandle;
}
NativeType[] nativeParameterTypes = new NativeType[parameterTypes.length];
boolean[] hasParameterConverter = new boolean[parameterTypes.length];
for (int i = 0; i < hasParameterConverter.length; i++) {
CType parameterType = CType.typeOf(parameterTypes[i]);
if (parameterType instanceof CType.Builtin) {
nativeParameterTypes[i] = parameterType.getNativeType();
/*
} else if (parameterType instanceof MappedType) {
nativeParameterTypes[i] = ((MappedType) parameterType).getRealType().getNativeType();
*/
} else {
return failedHandle;
}
hasParameterConverter[i] = !(parameterType instanceof CType.Builtin);
}
JITSignature jitSignature = new JITSignature(nativeResultType, nativeParameterTypes,
hasResultConverter, hasParameterConverter, convention, ignoreErrno);
synchronized (this) {
cleanup();
HandleRef ref = handles.get(jitSignature);
JITHandle handle = ref != null ? ref.get() : null;
if (handle == null) {
handle = new JITHandle(this, jitSignature, false);
handles.put(jitSignature, new HandleRef(handle, jitSignature, referenceQueue));
}
return handle;
}
}
void registerClass(JITHandle handle, Class<? extends Invoker> klass) {
classes.put(klass, handle);
}
}