blob: f1ae8abd216c52f91f68e4c7a6daa7fb04bfd6c0 (
plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
(.module:
[lux (#- int char)
[abstract
[monad (#+ do)]]
[control
["." function]
["p" parser]]
[data
["." maybe]
["." error]
[text
format]
[collection
["." list ("#/." functor)]]]
["." host (#+ import: do-to)]
[macro
["." code]
["s" syntax (#+ syntax:)]]
[tool
[compiler
[phase (#+ Operation)]]]]
["." // (#+ Primitive Inst)
["." type]])
## [Host]
(import: #long java/lang/Object)
(import: #long java/lang/String)
(syntax: (declare {codes (p.many s.local-identifier)})
(|> codes
(list/map (function (_ code) (` ((~' #static) (~ (code.local-identifier code)) (~' int)))))
wrap))
(`` (import: org/objectweb/asm/Opcodes
(#static NOP int)
## Conversion
(~~ (declare D2F D2I D2L
F2D F2I F2L
I2B I2C I2D I2F I2L I2S
L2D L2F L2I))
## Primitive
(~~ (declare T_BOOLEAN T_CHAR T_FLOAT T_DOUBLE
T_BYTE T_SHORT T_INT T_LONG))
## Class
(~~ (declare CHECKCAST NEW INSTANCEOF))
## Stack
(~~ (declare DUP DUP_X1 DUP_X2
DUP2 DUP2_X1 DUP2_X2
POP POP2
SWAP))
## Jump
(~~ (declare IF_ICMPEQ IF_ICMPGT IF_ICMPLT IF_ACMPEQ IFNULL
IFEQ IFNE IFLT IFLE IFGT IFGE
GOTO))
(#static ACONST_NULL int)
## Var
(~~ (declare ILOAD LLOAD DLOAD ALOAD
ISTORE LSTORE ASTORE))
## Arithmetic
(~~ (declare IADD ISUB IMUL IDIV IREM
LADD LSUB LMUL LDIV LREM LCMP
FADD FSUB FMUL FDIV FREM FCMPG FCMPL
DADD DSUB DMUL DDIV DREM DCMPG DCMPL))
## Bit-wise
(~~ (declare IAND IOR IXOR ISHL ISHR IUSHR
LAND LOR LXOR LSHL LSHR LUSHR))
## Array
(~~ (declare ARRAYLENGTH NEWARRAY ANEWARRAY
AALOAD AASTORE
BALOAD BASTORE
SALOAD SASTORE
IALOAD IASTORE
LALOAD LASTORE
FALOAD FASTORE
DALOAD DASTORE
CALOAD CASTORE))
## Member
(~~ (declare GETSTATIC PUTSTATIC GETFIELD PUTFIELD
INVOKESTATIC INVOKESPECIAL INVOKEVIRTUAL INVOKEINTERFACE))
(#static ATHROW int)
## Concurrency
(~~ (declare MONITORENTER MONITOREXIT))
## Return
(~~ (declare RETURN IRETURN LRETURN DRETURN ARETURN))
))
(import: org/objectweb/asm/Label
(new []))
(import: org/objectweb/asm/MethodVisitor
(visitCode [] void)
(visitMaxs [int int] void)
(visitEnd [] void)
(visitInsn [int] void)
(visitLdcInsn [Object] void)
(visitFieldInsn [int String String String] void)
(visitTypeInsn [int String] void)
(visitVarInsn [int int] void)
(visitIntInsn [int int] void)
(visitMethodInsn [int String String String boolean] void)
(visitLabel [Label] void)
(visitJumpInsn [int Label] void)
(visitTryCatchBlock [Label Label Label String] void)
(visitTableSwitchInsn [int int Label (Array Label)] void)
)
## [Insts]
(def: #export make-label
(All [s] (Operation s Label))
(function (_ state)
(#error.Success [state (Label::new)])))
(def: #export (with-label action)
(-> (-> Label Inst) Inst)
(action (Label::new)))
(template [<name> <type> <prepare>]
[(def: #export (<name> value)
(-> <type> Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitLdcInsn (<prepare> value)))))]
[boolean Bit function.identity]
[int Int host.long-to-int]
[long Int function.identity]
[double Frac function.identity]
[char Nat (|>> .int host.long-to-int host.int-to-char)]
[string Text function.identity]
)
(syntax: (prefix {base s.local-identifier})
(wrap (list (` ((~ (code.local-identifier (format "Opcodes::" base))))))))
(def: #export NULL
Inst
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitInsn (prefix ACONST_NULL)))))
(template [<name>]
[(def: #export <name>
Inst
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitInsn (prefix <name>)))))]
[NOP]
## Stack
[DUP] [DUP_X1] [DUP_X2] [DUP2] [DUP2_X1] [DUP2_X2]
[POP] [POP2]
[SWAP]
## Conversions
[D2F] [D2I] [D2L]
[F2D] [F2I] [F2L]
[I2B] [I2C] [I2D] [I2F] [I2L] [I2S]
[L2D] [L2F] [L2I]
## Integer arithmetic
[IADD] [ISUB] [IMUL] [IDIV] [IREM]
## Integer bitwise
[IAND] [IOR] [IXOR] [ISHL] [ISHR] [IUSHR]
## Long arithmetic
[LADD] [LSUB] [LMUL] [LDIV] [LREM]
[LCMP]
## Long bitwise
[LAND] [LOR] [LXOR] [LSHL] [LSHR] [LUSHR]
## Float arithmetic
[FADD] [FSUB] [FMUL] [FDIV] [FREM] [FCMPG] [FCMPL]
## Double arithmetic
[DADD] [DSUB] [DMUL] [DDIV] [DREM]
[DCMPG] [DCMPL]
## Array
[ARRAYLENGTH]
[AALOAD] [AASTORE]
[BALOAD] [BASTORE]
[SALOAD] [SASTORE]
[IALOAD] [IASTORE]
[LALOAD] [LASTORE]
[FALOAD] [FASTORE]
[DALOAD] [DASTORE]
[CALOAD] [CASTORE]
## Exceptions
[ATHROW]
## Concurrency
[MONITORENTER] [MONITOREXIT]
## Return
[RETURN] [IRETURN] [LRETURN] [DRETURN] [ARETURN]
)
(template [<name>]
[(def: #export (<name> register)
(-> Nat Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitVarInsn (prefix <name>) (.int register)))))]
[ILOAD] [LLOAD] [DLOAD] [ALOAD]
[ISTORE] [LSTORE] [ASTORE]
)
(template [<name> <inst>]
[(def: #export (<name> class field type)
(-> Text Text //.Type Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitFieldInsn (<inst>) (type.binary-name class) field (type.descriptor type)))))]
[GETSTATIC Opcodes::GETSTATIC]
[PUTSTATIC Opcodes::PUTSTATIC]
[PUTFIELD Opcodes::PUTFIELD]
[GETFIELD Opcodes::GETFIELD]
)
(template [<name> <inst>]
[(def: #export (<name> class)
(-> Text Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitTypeInsn (<inst>) (type.binary-name class)))))]
[CHECKCAST Opcodes::CHECKCAST]
[NEW Opcodes::NEW]
[INSTANCEOF Opcodes::INSTANCEOF]
[ANEWARRAY Opcodes::ANEWARRAY]
)
(def: #export (NEWARRAY type)
(-> Primitive Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitIntInsn (Opcodes::NEWARRAY)
(case type
#//.Boolean (Opcodes::T_BOOLEAN)
#//.Byte (Opcodes::T_BYTE)
#//.Short (Opcodes::T_SHORT)
#//.Int (Opcodes::T_INT)
#//.Long (Opcodes::T_LONG)
#//.Float (Opcodes::T_FLOAT)
#//.Double (Opcodes::T_DOUBLE)
#//.Char (Opcodes::T_CHAR))))))
(template [<name> <inst>]
[(def: #export (<name> class method-name method-signature interface?)
(-> Text Text //.Method Bit Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitMethodInsn (<inst>) (type.binary-name class) method-name (type.method-descriptor method-signature) interface?))))]
[INVOKESTATIC Opcodes::INVOKESTATIC]
[INVOKEVIRTUAL Opcodes::INVOKEVIRTUAL]
[INVOKESPECIAL Opcodes::INVOKESPECIAL]
[INVOKEINTERFACE Opcodes::INVOKEINTERFACE]
)
(template [<name>]
[(def: #export (<name> @where)
(-> //.Label Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitJumpInsn (prefix <name>) @where))))]
[IF_ICMPEQ] [IF_ICMPGT] [IF_ICMPLT] [IF_ACMPEQ] [IFNULL]
[IFEQ] [IFNE] [IFLT] [IFLE] [IFGT] [IFGE]
[GOTO]
)
(def: #export (TABLESWITCH min max default labels)
(-> Int Int //.Label (List //.Label) Inst)
(function (_ visitor)
(let [num-labels (list.size labels)
labels-array (host.array Label num-labels)
_ (loop [idx 0]
(if (n/< num-labels idx)
(exec (host.array-write idx
(maybe.assume (list.nth idx labels))
labels-array)
(recur (inc idx)))
[]))]
(do-to visitor
(MethodVisitor::visitTableSwitchInsn min max default labels-array)))))
(def: #export (try @from @to @handler exception)
(-> //.Label //.Label //.Label Text Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitTryCatchBlock @from @to @handler (type.binary-name exception)))))
(def: #export (label @label)
(-> //.Label Inst)
(function (_ visitor)
(do-to visitor
(MethodVisitor::visitLabel @label))))
(def: #export (array type)
(-> //.Type Inst)
(case type
(#//.Primitive prim)
(NEWARRAY prim)
(#//.Generic generic)
(let [elem-class (case generic
(#//.Class class params)
(type.binary-name class)
_
(type.binary-name "java.lang.Object"))]
(ANEWARRAY elem-class))
_
(ANEWARRAY (type.descriptor type))))
(def: (primitive-wrapper type)
(-> Primitive Text)
(case type
#//.Boolean "java.lang.Boolean"
#//.Byte "java.lang.Byte"
#//.Short "java.lang.Short"
#//.Int "java.lang.Integer"
#//.Long "java.lang.Long"
#//.Float "java.lang.Float"
#//.Double "java.lang.Double"
#//.Char "java.lang.Character"))
(def: (primitive-unwrap type)
(-> Primitive Text)
(case type
#//.Boolean "booleanValue"
#//.Byte "byteValue"
#//.Short "shortValue"
#//.Int "intValue"
#//.Long "longValue"
#//.Float "floatValue"
#//.Double "doubleValue"
#//.Char "charValue"))
(def: #export (wrap type)
(-> Primitive Inst)
(let [class (primitive-wrapper type)]
(|>> (INVOKESTATIC class "valueOf"
(type.method (list (#//.Primitive type))
(#.Some (type.class class (list)))
(list))
#0))))
(def: #export (unwrap type)
(-> Primitive Inst)
(let [class (primitive-wrapper type)]
(|>> (CHECKCAST class)
(INVOKEVIRTUAL class (primitive-unwrap type)
(type.method (list) (#.Some (#//.Primitive type)) (list))
#0))))
(def: #export (fuse insts)
(-> (List Inst) Inst)
(case insts
#.Nil
function.identity
(#.Cons singleton #.Nil)
singleton
(#.Cons head tail)
(function.compose (fuse tail) head)))
|