/
BeamTypeAnalysis.java
2625 lines (2198 loc) · 67.4 KB
/
BeamTypeAnalysis.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This file is part of Erjang - A JVM-based Erlang VM
*
* Copyright (c) 2009 by Trifork
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package erjang.beam.analysis;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.objectweb.asm.Type;
import erjang.EAtom;
import erjang.EBig;
import erjang.EBinMatchState;
import erjang.EBinary;
import erjang.EBitString;
import erjang.ECons;
import erjang.EDouble;
import erjang.EFun;
import erjang.EInteger;
import erjang.EList;
import erjang.EString;
import erjang.ENil;
import erjang.ENumber;
import erjang.EObject;
import erjang.EPID;
import erjang.EPort;
import erjang.ERT;
import erjang.ERef;
import erjang.ESeq;
import erjang.ESmall;
import erjang.ETuple;
import erjang.ETuple2;
import erjang.beam.Arg;
import erjang.beam.BIFUtil;
import erjang.beam.BeamCodeBlock;
import erjang.beam.BeamFunction;
import erjang.beam.BeamInstruction;
import erjang.beam.BeamOpcode;
import erjang.beam.BeamExceptionHandler;
import erjang.beam.BlockVisitor;
import erjang.beam.BlockVisitor2;
import erjang.beam.BuiltInFunction;
import erjang.beam.ExtFunc;
import erjang.beam.FunctionAdapter;
import erjang.beam.FunctionVisitor;
import erjang.beam.FunctionVisitor2;
import erjang.beam.ModuleAdapter;
import erjang.beam.ModuleVisitor;
import erjang.beam.Arg.Kind;
import erjang.beam.repr.Insn;
import erjang.beam.repr.ExtFun;
import erjang.beam.repr.Operands;
import erjang.beam.repr.Operands.Int;
import erjang.beam.repr.Operands.XReg;
import static erjang.beam.repr.Operands.SourceOperand;
import static erjang.beam.repr.Operands.DestinationOperand;
import static erjang.beam.CodeAtoms.*;
public class BeamTypeAnalysis extends ModuleAdapter {
static final Logger log = Logger.getLogger("erjang.beam");
/**
*
*/
public BeamTypeAnalysis(ModuleVisitor mv) {
super(mv);
}
static final Type ESMALL_TYPE = Type.getType(ESmall.class);
static final Type EBIG_TYPE = Type.getType(EBig.class);
static final Type EINTEGER_TYPE = Type.getType(EInteger.class);
static final Type ENUMBER_TYPE = Type.getType(ENumber.class);
static final Type EOBJECT_TYPE = Type.getType(EObject.class);
static final Type EDOUBLE_TYPE = Type.getType(EDouble.class);
static final Type ENIL_TYPE = Type.getType(ENil.class);
static final Type EATOM_TYPE = Type.getType(EAtom.class);
static final Type ETUPLE_TYPE = Type.getType(ETuple.class);
static final Type EBINARY_TYPE = Type.getType(EBinary.class);
static final Type EBITSTRING_TYPE = Type.getType(EBitString.class);
static final Type ECONS_TYPE = Type.getType(ECons.class);
static final Type ESEQ_TYPE = Type.getType(ESeq.class);
static final Type ELIST_TYPE = Type.getType(EList.class);
static final Type EFUN_TYPE = Type.getType(EFun.class);
static final Type EPID_TYPE = Type.getType(EPID.class);
static final Type EPORT_TYPE = Type.getType(EPort.class);
static final Type EREFERENCE_TYPE = Type.getType(ERef.class);
static final Type EMATCHSTATE_TYPE = Type.getType(EBinMatchState.class);
static final EAtom am_plus = EAtom.intern("+");
static final EAtom am_minus = EAtom.intern("-");
private static final ETuple X0_REG = ETuple.make(new EObject[] { X_ATOM,
new ESmall(0) });
private EAtom moduleName;
private List<FV> functions = new ArrayList<FV>();
@Override
public void declareFunction(EAtom fun, int arity, int label) {
/* ignore */
}
@Override
public FunctionVisitor visitFunction(EAtom name, int arity, int startLabel) {
FV f = new FV(super.visitFunction(name, arity, startLabel), name,
arity, startLabel);
functions.add(f);
return f;
}
class FV extends FunctionAdapter implements BeamFunction, TypeMap.XRegMarker {
BasicBlock makeBasicBlock(int label, int index) {
assert ((label & 0xffff) == label);
assert ((index & 0xffff) == index);
int bbk = (label << 16) | index;
BasicBlock bb = bbs.get(bbk);
if (bb == null) {
bbs.put(bbk, bb = new BasicBlock(label, index));
}
return bb;
}
TreeMap<Integer, BasicBlock> bbs = new TreeMap<Integer, BasicBlock>();
/** Maps from label to list of exception handler labels. */
TreeMap<Integer, List<BeamExceptionHandler>> blocks_with_ambiguous_exh = new TreeMap<Integer, List<BeamExceptionHandler>>();
void live_analysis() {
Integer[] all = bbs.keySet().toArray(new Integer[bbs.size()]);
Arrays.sort(all);
boolean change = false;
int iter = 0;
do {
iter += 1;
for (int n = all.length - 1; n >= 0; n--) {
BasicBlock bb = bbs.get(all[n]);
TreeSet<Integer> inq = bb.in;
TreeSet<Integer> outq = bb.out;
TreeSet<Integer> in_n = new TreeSet<Integer>();
in_n.addAll(outq);
in_n.removeAll(bb.kill);
in_n.addAll(bb.use);
bb.in = in_n;
TreeSet<Integer> out_n = new TreeSet<Integer>();
for (BasicBlock s : bb.succ) {
TreeSet<Integer> in_s = s.in;
if (in_s != null) {
out_n.addAll(in_s);
}
}
bb.out = out_n;
change = (!inq.equals(in_n)) || (!outq.equals(out_n));
}
} while (change);
// log.finer("live analysis for " + name + "/" + arity
// + " completed in " + iter + " iterations.");
}
Map<Integer, LabeledBlock> lbs = new TreeMap<Integer, LabeledBlock>();
private final EAtom name;
private final int arity;
private final int startLabel;
private SortedSet<LabeledBlock> needs_analyze = new TreeSet<LabeledBlock>(
new Comparator<LabeledBlock>() {
@Override
public int compare(LabeledBlock o1, LabeledBlock o2) {
return o2.block_label - o1.block_label;
}
});
public int max_stack;
public HashSet<Integer> all_xregs = new HashSet();
public int max_freg;
private boolean is_tail_recursive;
public FV(FunctionVisitor fv, EAtom name, int arity, int startLabel) {
super(fv);
this.name = name;
this.arity = arity;
this.startLabel = startLabel;
}
@Override
public void visitEnd() {
LabeledBlock lb = lbs.get(startLabel);
lb.merge_from(this.make_initial());
try {
while (!needs_analyze.isEmpty()) {
lb = needs_analyze.first();
needs_analyze.remove(lb);
lb.analyze();
}
} catch (RuntimeException t) {
dump();
throw t;
} catch (Error t) {
dump();
throw t;
}
// woo!
// live_analysis();
SortedSet<Integer> labels = new TreeSet<Integer>();
labels.addAll(lbs.keySet());
for (int i : labels) {
lb = lbs.get(i);
if (lb.initial == null) continue;
ExceptionHandler e = lb.initial.exh;
if (e == null) continue;
List<BeamExceptionHandler> ambi = e.ambiguousities();
if (ambi != null) {
blocks_with_ambiguous_exh.put(i,ambi);
}
}
boolean has_unreachable_code = false;
for (int i : labels) {
lb = lbs.get(i);
if (lb.isDeadCode()) {
if (lb.insns.get(0).opcode() == BeamOpcode.func_info) {
// ignore this
} else {
log.fine("UNREACHABLE " + lb.block_label);
has_unreachable_code = true;
}
}
}
if (has_unreachable_code) {
this.dump();
}
function_visit_end();
}
private void function_visit_end() {
if (fv instanceof FunctionVisitor2) {
((FunctionVisitor2) fv).visitMaxs((Collection<Integer>) this.all_xregs ,
this.max_stack, this.max_freg, this.is_tail_recursive);
}
for (LabeledBlock block : this.lbs.values()) {
ExceptionHandler block_exh =
block.initial==null? null : block.initial.exh;
assert(blocks_with_ambiguous_exh.containsKey(block.block_label) == (block_exh != null && block_exh.ambiguousities() != null));
if (block_exh != null && block_exh.ambiguousities() != null) {
// Handle block with ambiguous exception handler:
List<BeamExceptionHandler> ambi = blocks_with_ambiguous_exh.get(block.block_label);
for (BeamExceptionHandler e : ambi) {
int ext_label = extendedLabel(block.block_label, e);
function_visit_end_aux(block, ext_label, e);
}
} else {
int ext_label = extendedLabel(block.block_label, block_exh);
function_visit_end_aux(block, ext_label, block_exh);
}
}
super.fv.visitEnd();
}
private void function_visit_end_aux(LabeledBlock block, int ext_label, BeamExceptionHandler exh) {
BlockVisitor vis = super.fv.visitLabeledBlock(ext_label);
try {
block.accept(vis, exh);
} catch (Error e) {
dump();
throw e;
}
}
private void dump() {
if (log.isLoggable(Level.FINER)) {
log.finer("DUMPING " + name + "/" + arity);
for (Map.Entry<Integer, LabeledBlock> ent : lbs.entrySet()) {
ent.getValue().dump();
}
}
}
@Override
public BlockVisitor visitLabeledBlock(int label) {
return get_lb(label, true);
}
private TypeMap make_initial() {
TypeMap res = new TypeMap(makeBasicBlock(startLabel, 0));
for (int i = 0; i < arity; i++) {
res = res.setx(i, EOBJECT_TYPE, this);
}
return res;
}
private LabeledBlock get_lb(int label, boolean create) {
if (lbs.containsKey(label)) {
LabeledBlock res = lbs.get(label);
return res;
} else if (create) {
// System.out.println("creating " + name + "/" + arity + ":" +
// label);
LabeledBlock res = new LabeledBlock(label);
lbs.put(label, res);
return res;
} else {
return null;
}
}
int extendedLabel(int label, BeamExceptionHandler exh) {
assert ((label &~ 0xffff) == 0);
int extLabel = label;
if (blocks_with_ambiguous_exh.containsKey(label)) {
// Add info about exception context:
if (exh != null) {
assert(! blocks_with_ambiguous_exh.containsKey(exh.getHandlerLabel()));
int handlerLabel = exh.getHandlerLabel();
assert ((handlerLabel &~ 0xffff) == 0);
extLabel |= (handlerLabel << 16);
} // else high 16 bits are 0.
}
return extLabel;
}
public void mark_xreg_as_used(int reg) {
all_xregs.add(reg);
}
private void update_max_regs(TypeMap current) {
max_stack = Math.max(max_stack, current.stacksize);
max_freg = Math.max(max_freg, current.max_freg());
/* As for the X registers, we wish to track their usage
* individually (rather than just the number of the
* highest one used).
* This set could be updated here, but it is far more
* efficient to do it elsewhere - see TypeMap.setx().
*/
}
class LabeledBlock implements BlockVisitor, BeamCodeBlock {
private final int block_label;
TypeMap initial;
boolean last = false;
TypeMap[] map;
public LabeledBlock(int label) {
this.block_label = label;
initial = null;
}
/**
* @param vis
*/
public void accept(BlockVisitor vis, BeamExceptionHandler exh) {
try {
if (!isDeadCode()) {
if (vis instanceof BlockVisitor2) {
accept_2((BlockVisitor2) vis, exh);
} else {
accept_1(vis);
}
}
} finally {
vis.visitEnd();
}
}
private void accept_1(BlockVisitor vis) {
throw new erjang.NotImplemented();
/*
for (int i = 0; i < insns.size(); i++) {
Insn insn = insns.get(i);
BeamOpcode opcode = insn.opcode();
vis.visitInsn(opcode, insn);
}
*/
}
private void accept_2(BlockVisitor2 vis, BeamExceptionHandler exh) {
int tuple_pos = 0;
Arg tuple_reg = null;
vis.visitBegin(exh);
for (int insn_idx = 0; insn_idx < insns.size(); insn_idx++) {
Insn insn_ = insns.get(insn_idx);
BeamOpcode opcode = insn_.opcode();
TypeMap type_map = this.map[insn_idx];
switch (opcode) {
case func_info: {
Insn.AAI insn = (Insn.AAI)insn_;
// log.finer("go: " + insn);
vis.visitInsn(opcode, insn.getExtFun());
break;
}
case fconv:
case fmove:
case move: {
Insn.SD insn = (Insn.SD)insn_;
Arg src = src_arg(insn_idx, insn.src);
Arg dest = dest_arg(insn_idx, insn.dest);
if (dest.kind != Kind.F) {
if (src.kind == Kind.F) {
dest = new Arg(dest, EDOUBLE_TYPE);
} else {
dest = new Arg(dest, src.type);
}
} else {
// arg2.kind == F
}
vis.visitInsn(opcode, src, dest);
break;
}
case put_string: {
Insn.ByD insn = (Insn.ByD)insn_;
Arg src = src_arg(insn_idx, insn.bin);
Arg dest = dest_arg(insn_idx, insn.dest);
// Arg arg1 = decode_arg(insn_idx, insn.elm(3));
// Arg arg2 = decode_out_arg(insn_idx, insn.elm(4));
dest = new Arg(dest, ESEQ_TYPE);
vis.visitInsn(BeamOpcode.move, src, dest);
break;
}
case fadd:
case fsub:
case fmul:
case fdiv:
{
Insn.LSSD insn = (Insn.LSSD)insn_;
EAtom name = opcode.symbol;
int failLabel = decode_labelref(insn.label, type_map.exh);
Arg[] in = new Arg[] {src_arg(insn_idx, insn.src1),
src_arg(insn_idx, insn.src2)};
Type[] inTypes = new Type[] {in[0].type,
in[1].type};
Arg out = dest_arg(insn_idx, insn.dest);
BuiltInFunction bif = BIFUtil.getMethod("erlang", name.getName(), inTypes,
failLabel != 0, true);
vis.visitInsn(opcode, failLabel, in, out, bif);
break;
}
case bif0:
case bif1:
case bif2:
{
Insn.Bif insn = (Insn.Bif)insn_;
EAtom name = insn.ext_fun.fun;
int failLabel = decode_labelref(insn.label, type_map.exh);
SourceOperand[] srcs = insn.args;
Arg[] in = src_args(insn_idx, srcs);
Arg out = dest_arg(insn_idx, insn.dest);
BuiltInFunction bif = BIFUtil.getMethod("erlang", name.getName(),
parmTypes(type_map, srcs),
failLabel != 0, true);
vis.visitInsn(opcode, failLabel, in, out, bif);
break;
}
case gc_bif1:
case gc_bif2:
{
Insn.GcBif insn = (Insn.GcBif)insn_;
EAtom name = insn.ext_fun.fun;
int failLabel = decode_labelref(insn.label, type_map.exh);
SourceOperand[] srcs = insn.args;
Arg[] in = src_args(insn_idx, srcs);
Arg out = dest_arg(insn_idx, insn.dest);
// special case for X+1, 1+X, X-1.
Int lop = null, rop = null;
if (srcs.length==2
&& (((name==am_plus || name == am_minus) && (rop=srcs[1].testInt()) != null && rop.equals(1))
|| (name==am_plus && (lop=srcs[0].testInt()) != null && lop.equals(1))))
{
if (name == am_plus) {
Arg src = (lop == null) ? in[0] : in[1];
vis.visitIncrement(src, out);
break;
} else if (name == am_minus) {
Arg src = in[0];
vis.visitDecrement(src, out);
break;
}
}
BuiltInFunction bif = BIFUtil.getMethod("erlang", name.getName(),
parmTypes(type_map, srcs),
failLabel != 0, true);
vis.visitInsn(opcode, failLabel, in, out, bif);
break;
}
case is_tuple: {
if (insn_idx+1 < insns.size()) {
Insn next_insn = insns.get(insn_idx+1);
if (next_insn.opcode() == BeamOpcode.test_arity) {
int this_fail = decode_labelref(((Insn.L)insn_).label, this.map[insn_idx].exh);
int next_fail = decode_labelref(((Insn.L)next_insn).label, this.map[insn_idx+1].exh);
if (this_fail == next_fail) {
Arg this_arg = src_arg(insn_idx, ((Insn.LD)insn_).dest);
Arg next_arg = src_arg(insn_idx+1, ((Insn.LD)next_insn).dest);
if (this_arg.equals(next_arg)) {
// SKIP THIS INSTRUCTION!
break;
}
}
}
}
}
// Tests:
// LS:
case is_integer:
case is_float:
case is_number:
case is_atom:
case is_pid:
case is_reference:
case is_port:
case is_nil:
case is_binary:
case is_list:
case is_nonempty_list:
case is_function:
case is_boolean:
case is_bitstr:
// LSI:
case test_arity:
case bs_test_tail2:
case bs_test_unit:
// LSS:
case is_lt:
case is_ge:
case is_eq:
case is_ne:
case is_eq_exact:
case is_ne_exact:
case is_function2:
// LSBi:
case bs_match_string:
// LSII:
case bs_skip_utf8:
case bs_skip_utf16:
case bs_skip_utf32:
// LSIID:
case bs_start_match2:
case bs_get_utf8:
case bs_get_utf16:
case bs_get_utf32:
// LSSII:
case bs_skip_bits2:
// LSISIID:
case bs_get_integer2:
case bs_get_float2:
case bs_get_binary2:
accept_2_test(vis, (Insn.L)insn_, insn_idx);
break;
case K_return:
vis.visitInsn(opcode, new Arg(Arg.Kind.X, 0,
type_map.getx(0)));
break;
case allocate_heap_zero:
case allocate_zero: {
Insn.I insn = (Insn.I)insn_;
int depth = type_map.stacksize;
int count = insn.i1;
Arg[] ys = new Arg[count];
for (int i = 0; i < count; i++) {
ys[i] = new Arg(Arg.Kind.Y, depth + i, null);
}
vis.visitInsn(opcode, (Arg[]) ys);
break;
}
case test_heap:
break;
case fclearerror:
case fcheckerror:
break;
case recv_mark:
case recv_set:
break;
case call_ext_last:
case call_ext_only:
do_call(vis, insn_idx, (Insn.I)insn_, true, true);
break;
case call_ext:
do_call(vis, insn_idx, (Insn.I)insn_, false, true);
if (is_exceptional_call(insn_)) {
vis.visitUnreachablePoint();
}
break;
case call:
do_call(vis, insn_idx, (Insn.I)insn_, false, false);
break;
case call_last:
case call_only:
do_call(vis, insn_idx, (Insn.I)insn_, true, false);
break;
case apply_last:
case apply: {
Insn.I insn = (Insn.I) insn_;
Arg[] args = new Arg[2 + insn.i1];
for (int i = 0; i < args.length; i++) {
args[i] = new Arg(Arg.Kind.X, i, map[insn_idx]
.getx(i));
}
vis.visitInsn(opcode, args);
break;
}
case make_fun2: {
Insn.F insn = (Insn.F) insn_;
ExtFun efun = insn.anon_fun.asExtFun();
int numfree = insn.anon_fun.free_vars;
int index = insn.anon_fun.index;
int old_index = insn.anon_fun.old_index;
EBinary uniq = insn.anon_fun.mod_md5;
int old_uniq = insn.anon_fun.old_uniq;
Arg[] free = new Arg[numfree];
for (int i = 0; i < numfree; i++) {
free[i] = new Arg(Arg.Kind.X, i, map[insn_idx]
.getx(i));
}
vis.visitInsn(opcode, efun, free, index, old_index, uniq, old_uniq);
break;
}
case init: {
Insn.D insn = (Insn.D) insn_;
vis.visitInsn(opcode, dest_arg(insn_idx, insn.dest));
break;
}
case put_list: {
Insn.SSD insn = (Insn.SSD) insn_;
Arg[] in = new Arg[] {
src_arg(insn_idx, insn.src1),
src_arg(insn_idx, insn.src2)
};
Arg out = dest_arg(insn_idx, insn.dest);
vis.visitInsn(opcode, in, out);
break;
}
case put_tuple: {
Insn.ID insn = (Insn.ID) insn_;
int arity = insn.i1;
tuple_reg = new Arg(dest_arg(insn_idx, insn.dest),
getTupleType(arity));
vis.visitInsn(opcode, arity, tuple_reg);
tuple_pos = 1;
break;
}
case put: {
Insn.S insn = (Insn.S) insn_;
Arg val = src_arg(insn_idx, insn.src);
vis.visitInsn(opcode, val, tuple_reg, tuple_pos++);
break;
}
case set_tuple_element: {
Insn.SDI insn = (Insn.SDI) insn_;
Arg in = src_arg(insn_idx, insn.src);
Arg out = src_arg(insn_idx, insn.dest);
int idx = insn.i;
vis.visitInsn(opcode, in, out, idx);
break;
}
case allocate_heap:
case allocate:
case deallocate: {
// need to zero out refs?
break;
}
case select_tuple_arity: {
Insn.Select insn = (Insn.Select) insn_;
int failLabel = decode_labelref(insn.defaultLabel,
type_map.exh);
Arg in = src_arg(insn_idx, insn.src);
Operands.SelectList jumpTable = insn.jumpTable;
int len = jumpTable.size();
int[] arities = new int[len];
int[] targets = new int[len];
for (int i=0; i<len; i++) {
Operands.Operand value = jumpTable.getValue(i);
Operands.Label target = jumpTable.getLabel(i);
arities[i] = value.asCodeInt().value;
targets[i] = decode_labelref(target, type_map.exh);
}
vis.visitSelectTuple(in, failLabel, arities, targets);
break;
}
case select_val: {
Insn.Select insn = (Insn.Select) insn_;
int failLabel = decode_labelref(insn.defaultLabel,
type_map.exh);
Arg in = src_arg(insn_idx, insn.src);
Operands.SelectList jumpTable = insn.jumpTable;
int len = jumpTable.size();
Arg[] values = new Arg[len];
int[] targets = new int[len];
for (int i=0; i<len; i++) {
Operands.Operand value = jumpTable.getValue(i);
Operands.Label target = jumpTable.getLabel(i);
values[i] = arg(value.asLiteral());
targets[i] = decode_labelref(target, type_map.exh);
}
vis.visitSelectValue(in, failLabel, values, targets);
break;
}
case get_tuple_element: {
Insn.SID insn = (Insn.SID) insn_;
Arg in = src_arg(insn_idx, insn.src);
int idx = insn.i;
Arg out = dest_arg(insn_idx, insn.dest);
vis.visitInsn(opcode, in, out, idx);
break;
}
case jump: {
Insn.L insn = (Insn.L) insn_;
vis.visitJump(decode_labelref(insn.label, type_map.exh));
break;
}
case on_load: // ignore
case trim:
break;
case get_list: {
Insn.SDD insn = (Insn.SDD) insn_;
vis.visitInsn(opcode, new Arg[] {
src_arg(insn_idx, insn.src),
dest_arg(insn_idx, insn.dest1),
dest_arg(insn_idx, insn.dest2) });
break;
}
case try_case_end:
case badmatch:
case case_end: {
Insn.S insn = (Insn.S) insn_;
vis.visitInsn(opcode, src_arg(insn_idx, insn.src));
break;
}
case if_end:
vis.visitInsn(opcode);
break;
case send: {
vis.visitInsn(opcode,
new Arg[] { new Arg(Arg.Kind.X, 0),
new Arg(Arg.Kind.X, 1) });
break;
}
case K_try:
case K_catch: {
Insn.YL insn = (Insn.YL) insn_;
TypeMap type_map_after = this.map[insn_idx+1];
vis.visitCatchBlockStart(opcode,
decode_labelref(insn.label, type_map.exh),
src_arg(insn_idx, insn.y),
type_map_after.exh);
break;
}
case raise: {
Insn.SS insn = (Insn.SS) insn_;
Arg[] in = {src_arg(insn_idx, insn.src1),
src_arg(insn_idx, insn.src2) };
Arg ex = new Arg(Arg.Kind.X, 0);
int failLabel = 0;
// Half of the args are constants...
vis.visitInsn(opcode, failLabel, in, ex);
break;
}
case try_end:
case try_case:
case catch_end: {
Insn.Y insn = (Insn.Y) insn_;
vis.visitCatchBlockEnd(opcode,
src_arg(insn_idx, insn.y),
type_map.exh);
break;
}
case loop_rec: { /* loop receive */
Insn.LD insn = (Insn.LD) insn_;
vis.visitReceive(opcode,
decode_labelref(insn.label, type_map.exh),
dest_arg(insn_idx, insn.dest));
break;
}
case remove_message:
case timeout:
vis.visitInsn(opcode);
break;
case loop_rec_end:
case wait: {
Insn.L insn = (Insn.L) insn_;
vis.visitInsn(opcode, decode_labelref(insn.label, type_map.exh), null);
break;
}
case wait_timeout: {
Insn.LS insn = (Insn.LS) insn_;
vis.visitInsn(opcode,
decode_labelref(insn.label, type_map.exh),
src_arg(insn_idx, insn.src));
break;
}
case call_fun: {
Insn.I insn = (Insn.I) insn_;
int nargs = insn.i1;
Arg[] args = new Arg[nargs + 1];
for (int i = 0; i < args.length; i++) {
args[i] = new Arg(Arg.Kind.X, i, map[insn_idx]
.getx(i));
}
vis.visitInsn(opcode, args,
new Arg(Arg.Kind.X, 0, null));
break;
}
// {bs_add,{f,0},[{x,3},{x,4},1],{x,3}}
case bs_add: {
Insn.LSSID insn = (Insn.LSSID) insn_;
vis.visitBSAdd(src_arg(insn_idx, insn.src1),
src_arg(insn_idx, insn.src2),
insn.i3,
dest_arg(insn_idx, insn.dest));
break;
}
case bs_context_to_binary: {
Insn.D insn = (Insn.D) insn_;
vis.visitBS(opcode, dest_arg(insn_idx, insn.dest), null);
// do nothing for now
break;
}
case bs_restore2:
case bs_save2: {
Insn.DI insn = (Insn.DI) insn_;
vis.visitBS(opcode, src_arg(insn_idx, insn.dest),
//TODO: streamline - change API
insn.i2 == -1
? new Arg(EAtom.intern("start"))
: new Arg(new ESmall(insn.i2))
);
break;
}
case bs_init_writable: {
Arg size = src_arg(insn_idx, new Operands.XReg(0));
Arg dest = dest_arg(new Operands.XReg(0));
vis.visitInitWritable(size, dest);
break;
}
case bs_init2:
case bs_init_bits: {
Insn.LSIIID insn = (Insn.LSIIID) insn_;
Arg size = src_arg(insn_idx, insn.src2);
int flags = insn.i5;
Arg out = dest_arg(insn_idx, insn.dest);
boolean unit_is_bits = (opcode == BeamOpcode.bs_init_bits);
vis.visitInitBitString(size, flags, out,
unit_is_bits);
break;
}
case bs_put_string: {
Insn.By insn = (Insn.By) insn_;
Arg str = arg(insn.bin);
vis.visitBitStringPut(opcode, str, null,-1,-1);
break;
}
case bs_put_binary:
case bs_put_integer:
case bs_put_float:
{
Insn.LSIIS insn = (Insn.LSIIS) insn_;