-
Notifications
You must be signed in to change notification settings - Fork 130
/
CStruct.java
259 lines (227 loc) · 10.9 KB
/
CStruct.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package org.perl6.nqp.sixmodel.reprs;
import java.util.List;
import java.util.ArrayList;
import com.sun.jna.Structure;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.perl6.nqp.sixmodel.REPR;
import org.perl6.nqp.sixmodel.SerializationReader;
import org.perl6.nqp.sixmodel.SixModelObject;
import org.perl6.nqp.sixmodel.STable;
import org.perl6.nqp.sixmodel.StorageSpec;
import org.perl6.nqp.sixmodel.TypeObject;
import org.perl6.nqp.sixmodel.reprs.CStructREPRData.AttrInfo;
import org.perl6.nqp.sixmodel.reprs.NativeCall.ArgType;
import org.perl6.nqp.runtime.ExceptionHandling;
import org.perl6.nqp.runtime.Ops;
import org.perl6.nqp.runtime.ThreadContext;
public class CStruct extends REPR {
public SixModelObject type_object_for(ThreadContext tc, SixModelObject HOW) {
STable st = new STable(this, HOW);
SixModelObject obj = new TypeObject();
obj.st = st;
st.WHAT = obj;
return st.WHAT;
}
public void compose(ThreadContext tc, STable st, SixModelObject repr_info_hash) {
SixModelObject repr_info = repr_info_hash.at_key_boxed(tc, "attribute");
CStructREPRData repr_data = new CStructREPRData();
long mroLength = repr_info.elems(tc);
List<AttrInfo> attrInfos = new ArrayList<AttrInfo>();
for (long i = mroLength - 1; i >= 0; i--) {
SixModelObject entry = repr_info.at_pos_boxed(tc, i);
SixModelObject attrs = entry.at_pos_boxed(tc, 1);
long parents = entry.at_pos_boxed(tc, 2).elems(tc);
if (parents <= 1) {
long numAttrs = attrs.elems(tc);
for (long j = 0; j < numAttrs; j++) {
SixModelObject attrHash = attrs.at_pos_boxed(tc, j);
AttrInfo info = new AttrInfo();
info.name = attrHash.at_key_boxed(tc, "name").get_str(tc);
info.type = attrHash.at_key_boxed(tc, "type");
info.inlined = (short)attrHash.at_key_boxed(tc, "inlined").get_int(tc);
StorageSpec spec = info.type.st.REPR.get_storage_spec(tc, info.type.st);
info.bits = spec.bits;
repr_data.fieldTypes.put(info.name, info);
if (info.type == null) {
ExceptionHandling.dieInternal(tc, "CStruct representation requires the types of all attributes to be specified");
}
attrInfos.add(info);
}
}
else {
ExceptionHandling.dieInternal(tc, "CStruct representation does not support multiple inheritance");
}
}
/* XXX: We could generate the structure class lazily the first time we
* allocate an object, rather than upfront. Not sure if that's
* necessary though. */
st.REPRData = repr_data;
generateStructClass(tc, st, attrInfos);
}
public SixModelObject allocate(ThreadContext tc, STable st) {
/* TODO: Die if someone tries to allocate a CStruct before it's been
* composed. */
CStructInstance obj = new CStructInstance();
CStructREPRData repr_data = (CStructREPRData) st.REPRData;
obj.st = st;
try {
obj.storage = (Structure) repr_data.structureClass.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return obj;
}
public SixModelObject deserialize_stub(ThreadContext tc, STable st) {
/* This REPR can't be serialized. */
ExceptionHandling.dieInternal(tc, "Can't deserialize_stub a CStruct object.");
return null;
}
public void deserialize_finish(ThreadContext tc, STable st, SerializationReader reader, SixModelObject obj) {
ExceptionHandling.dieInternal(tc, "Can't deserialize_finish a CStruct object.");
}
private static long typeId = 0;
private void generateStructClass(ThreadContext tc, STable st, List<AttrInfo> fields) {
CStructREPRData reprData = (CStructREPRData) st.REPRData;
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
String className = "__CStruct__" + typeId++;
int attributes = fields.size();
if (attributes == 0) {
ExceptionHandling.dieInternal(tc,
"Class " + Ops.typeName(st.WHAT, tc) + " has no attributes, which is illegal with the CStruct representation.");
}
// public $className extends com.sun.jna.Structure implements com.sun.jna.Structure.ByReference { ... }
cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, className, null, "com/sun/jna/Structure", null);
// private static List<String> fieldOrder;
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, "fieldOrder", "Ljava/util/List;",
"Ljava.util.List<Ljava.lang.String;>;", null);
fv.visitEnd();
for (AttrInfo info: fields) {
String type = typeDescriptor(tc, info);
/* Indirect referenced structs need to be handled as pointers here, since the default of
* structs and unions is to inline. */
if (info.inlined == 0
&& (info.argType == ArgType.CSTRUCT || info.argType == ArgType.CPPSTRUCT || info.argType == ArgType.CUNION))
fv = cw.visitField(Opcodes.ACC_PUBLIC, info.name, "Lcom/sun/jna/Pointer;", null, null);
else
fv = cw.visitField(Opcodes.ACC_PUBLIC, info.name, type, null, null);
fv.visitEnd();
}
// static { fieldOrder = new ArrayList(); fieldOrder.add(field1); ... }
MethodVisitor staticVisitor = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
staticVisitor.visitCode();
staticVisitor.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList"); // Construct new object.
staticVisitor.visitInsn(Opcodes.DUP);
staticVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V"); // Invoke the constructor.
staticVisitor.visitFieldInsn(Opcodes.PUTSTATIC, className, "fieldOrder", "Ljava/util/List;");
for (int i = 0; i < attributes; i++) {
staticVisitor.visitFieldInsn(Opcodes.GETSTATIC, className, "fieldOrder", "Ljava/util/List;");
staticVisitor.visitLdcInsn(fields.get(i).name);
staticVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
staticVisitor.visitInsn(Opcodes.POP);
}
staticVisitor.visitInsn(Opcodes.RETURN);
staticVisitor.visitMaxs(2, 0);
staticVisitor.visitEnd();
// public List<String> getFieldOrder() { return fieldOrder; }
MethodVisitor gfoVisitor = cw.visitMethod(Opcodes.ACC_PUBLIC, "getFieldOrder", "()Ljava/util/List;", "()Ljava/util/List<Ljava/lang/String;>;", null);
gfoVisitor.visitCode();
gfoVisitor.visitFieldInsn(Opcodes.GETSTATIC, className, "fieldOrder", "Ljava/util/List;");
gfoVisitor.visitInsn(Opcodes.ARETURN);
gfoVisitor.visitMaxs(1, 1);
gfoVisitor.visitEnd();
// Add nullary constructor calling superclass.
MethodVisitor constructor = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
constructor.visitCode();
constructor.visitVarInsn(Opcodes.ALOAD, 0);
constructor.visitMethodInsn(Opcodes.INVOKESPECIAL,
"com/sun/jna/Structure", "<init>", "()V");
constructor.visitInsn(Opcodes.RETURN);
constructor.visitMaxs(1, 1);
constructor.visitEnd();
cw.visitEnd();
byte[] compiled = cw.toByteArray();
reprData.structureClass = tc.gc.byteClassLoader.defineClass(className, compiled);
}
private String typeDescriptor(ThreadContext tc, AttrInfo info) {
REPR repr = info.type.st.REPR;
StorageSpec spec = repr.get_storage_spec(tc, info.type.st);
info.bits = spec.bits;
if (spec.inlineable == StorageSpec.INLINED && spec.boxed_primitive == StorageSpec.BP_INT) {
if (spec.bits == 8) {
info.argType = ArgType.CHAR;
return "B";
}
else if (spec.bits == 16) {
info.argType = ArgType.SHORT;
return "S";
}
else if (spec.bits == 32) {
info.argType = ArgType.INT;
return "I";
}
else if (spec.bits == 64) {
info.argType = ArgType.LONG;
return "J";
}
else {
ExceptionHandling.dieInternal(tc, "CStruct representation only handles 8, 16, 32 and 64 bit ints");
return null;
}
}
else if (spec.inlineable == StorageSpec.INLINED && spec.boxed_primitive == StorageSpec.BP_NUM) {
if (spec.bits == 32) {
info.argType = ArgType.FLOAT;
return "F";
}
else if (spec.bits == 64) {
info.argType = ArgType.DOUBLE;
return "D";
}
else {
ExceptionHandling.dieInternal(tc, "CStruct representation only handles 32 and 64 bit nums");
return null;
}
}
else if ((spec.can_box & StorageSpec.CAN_BOX_STR) != 0) {
info.argType = ArgType.UTF8STR;
return "Ljava/lang/String;";
}
else if (repr instanceof CArray) {
info.argType = ArgType.CARRAY;
return "Lcom/sun/jna/Pointer;";
}
else if (repr instanceof CPointer) {
info.argType = ArgType.CPOINTER;
return "Lcom/sun/jna/Pointer;";
}
else if (repr instanceof CUnion) {
info.argType = ArgType.CUNION;
Class c = ((CUnionREPRData) info.type.st.REPRData).structureClass;
return Type.getDescriptor(c);
}
else if (repr instanceof CPPStruct) {
info.argType = ArgType.CPPSTRUCT;
Class c = ((CPPStructREPRData) info.type.st.REPRData).structureClass;
return Type.getDescriptor(c);
}
else if (repr instanceof CStruct) {
info.argType = ArgType.CSTRUCT;
Class c = ((CStructREPRData) info.type.st.REPRData).structureClass;
/* When we hit a struct in an attribute that is not composed yet, we most likely
* have hit a struct of our own kind. */
if (c == null)
return "L__CStruct__" + typeId + ";";
return Type.getDescriptor(c);
}
else {
ExceptionHandling.dieInternal(tc, "CStruct representation only handles int, num, CArray, CPointer, CStruct, CPPStruct and CUnion");
return null;
}
}
}