forked from meinicke/VarexJ
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStackHandler.java
More file actions
985 lines (857 loc) · 25 KB
/
StackHandler.java
File metadata and controls
985 lines (857 loc) · 25 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
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
package gov.nasa.jpf.vm.va;
import gov.nasa.jpf.vm.MJIEnv;
import gov.nasa.jpf.vm.Types;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import cmu.conditional.BiFunction;
import cmu.conditional.ChoiceFactory;
import cmu.conditional.Conditional;
import cmu.conditional.Function;
import cmu.conditional.One;
import cmu.conditional.VoidBiFunction;
import de.fosd.typechef.featureexpr.FeatureExpr;
import de.fosd.typechef.featureexpr.FeatureExprFactory;
/**
* Stack implementation where locals are separated from stack.<br>
* Locals: Conditional[]<br>
* Stack: Conditional -Stack-
*
* @author Jens Meinicke
*
*/
public class StackHandler implements Cloneable, IStackHandler {
enum Type {
INT, FLOAT, LONG, DOUBLE
}
enum StackInstruction {
DUP_X1, DUP2_X2, DUP2_X1, DUP2, DUP, DUP_X2, SWAP
}
/** Locals are directly accessed with index **/
private Conditional<Entry>[] locals;
public Conditional<Stack> stack;
public int length = 0;
public FeatureExpr stackCTX;
//*** StackHandlerTracker class object***//
public StackHandlerTracker c = new StackHandlerTracker();
//static LinkedList<StackHandlerTracker> q = new LinkedList<StackHandlerTracker>();
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#getStackWidth()
*/
@Override
public int getStackWidth() {
return stack.toList().size();
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#toString()
*/
@Override
public String toString() {
StringBuilder string = new StringBuilder();
string.append("Locals: [");
for (int i = 0; i < locals.length; i++) {
if (locals[i] == null) {
string.append("null");
} else {
string.append(locals[i]);
}
string.append(" ");
}
string.append("] \nStack: ");
string.append(stack);
return string.toString();
}
private static final One<Entry> nullValue = new One<>(new Entry(MJIEnv.NULL, false));
@SuppressWarnings("unchecked")
public StackHandler(FeatureExpr ctx, int nLocals, int nOperands) {
if (ctx == null) {
throw new RuntimeException("CTX == NULL");
}
length = nLocals + nOperands;
locals = new Conditional[nLocals];
Arrays.fill(locals, nullValue);
stack = new One<>(new Stack(nOperands, this.c));
stackCTX = ctx;
//q.addLast(this.c);
}
@SuppressWarnings("unchecked")
public StackHandler() {
stack = new One<>(new Stack(0, this.c));
locals = new Conditional[0];
stackCTX = FeatureExprFactory.True();
}
@Override
public FeatureExpr getCtx() {
return stackCTX;
}
@Override
public void setCtx(FeatureExpr ctx) {
stackCTX = ctx;
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#clone()
*/
@Override
@SuppressWarnings("unchecked")
public StackHandler clone() {
StackHandler clone = new StackHandler();
// clone.setCtx(stackCTX); // TODO ThreadStopTest.testStopRunning() fails
clone.length = length;
clone.locals = new Conditional[locals.length];
for (int i = 0; i < locals.length; i++) {
Conditional<Entry> local = locals[i];
if (local != null) {
clone.locals[i] = local.map(CopyEntry);
}
}
clone.stack = stack.map(CopyStack);
return clone;
}
private static final Function<Entry, Entry> CopyEntry = new Function<Entry, Entry>() {
@Override
public Entry apply(final Entry entry) {
return entry.copy();
}
};
private static final Function<Stack, Stack> CopyStack = new Function<Stack, Stack>() {
@Override
public Stack apply(final Stack stack) {
return stack.copy();
}
};
/*
* ############################################################
* Handling of local variables
* ############################################################
*/
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#pushLocal(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public void pushLocal(final FeatureExpr ctx, final int index) {
Conditional<Entry> value = locals[index];
if (value == null) {
value = new One<>(new Entry(MJIEnv.NULL, false));
}
value.mapf(ctx, new VoidBiFunction<FeatureExpr, Entry>() {
@Override
public void apply(final FeatureExpr ctx, final Entry entry) {
push(ctx, entry.value, entry.isRef);
}
});
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#pushLongLocal(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public void pushLongLocal(FeatureExpr ctx, int index) {
Conditional<Entry> value = locals[index];
if (value == null) {
value = new One<>(new Entry(0, false));
}
value.mapf(ctx, new VoidBiFunction<FeatureExpr, Entry>() {
@Override
public void apply(final FeatureExpr ctx, final Entry entry) {
push(ctx, entry.value, false);
}
});
value = locals[index + 1];
if (value == null) {
value = new One<>(new Entry(0, false));
}
value.mapf(ctx, new VoidBiFunction<FeatureExpr, Entry>() {
@Override
public void apply(final FeatureExpr ctx, final Entry entry) {
push(ctx, entry.value, false);
}
});
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#storeOperand(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public void storeOperand(final FeatureExpr ctx, final int index) {
if (Conditional.isTautology(ctx)) {
locals[index] = popEntry(ctx, true);
} else {
if (locals[index] == null) {
locals[index] = new One<>(new Entry(MJIEnv.NULL, false));
}
locals[index] = ChoiceFactory.create(ctx, popEntry(ctx, true), locals[index]).simplify();
}
}
private Conditional<Entry> popEntry(FeatureExpr ctx, final boolean copyRef) {
Conditional<Entry> result = stack.simplify(ctx).mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<Entry>>() {
@Override
public Conditional<Entry> apply(final FeatureExpr f, final Stack s) {
Stack clone = s.copy();
boolean ref = copyRef ? clone.isRef(0) : false;
int res = clone.pop();
if (stackCTX.equivalentTo(f)) {
stack = new One<>(clone);
} else {
stack = ChoiceFactory.create(f, new One<>(clone), stack);
}
return new One<>(new Entry(res, ref));
}
}).simplify();
stack = stack.simplify();
return result;
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#storeLongOperand(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public void storeLongOperand(final FeatureExpr ctx, final int index) {
stack.mapf(ctx, new VoidBiFunction<FeatureExpr, Stack>() {
@Override
public void apply(final FeatureExpr f, final Stack stack) {
if (Conditional.isContradiction(f)) {
return;
}
locals[index + 1] = ChoiceFactory.create(f, popEntry(f, false), locals[index + 1]);
locals[index] = ChoiceFactory.create(f, popEntry(f, false), locals[index]);
}
});
locals[index] = locals[index].simplify();
locals[index + 1] = locals[index + 1].simplify();
stack = stack.simplify();
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#setLocal(de.fosd.typechef.featureexpr.FeatureExpr, int, cmu.conditional.Conditional, boolean)
*/
@Override
public void setLocal(FeatureExpr ctx, final int index, final Conditional<Integer> value, final boolean isRef) {
value.mapf(ctx, new VoidBiFunction<FeatureExpr, Integer>() {
@Override
public void apply(final FeatureExpr x, final Integer value) {
setLocal(x, index, value, isRef);
}
});
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#setLocal(de.fosd.typechef.featureexpr.FeatureExpr, int, int, boolean)
*/
@Override
public void setLocal(final FeatureExpr ctx, final int index, final int value, final boolean isRef) {
if (Conditional.isTautology(ctx)) {
locals[index] = new One<>(new Entry(value, isRef));
} else {
if (locals[index] == null) {
locals[index] = new One<>(new Entry(0, false));
}
locals[index] = ChoiceFactory.create(ctx, new One<>(new Entry(value, isRef)), locals[index]).simplify();
}
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#getLocal(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public Conditional<Integer> getLocal(FeatureExpr ctx, final int index) {
if (index < 0) {
return new One<>(-1);
}
if (index < locals.length) {
if (locals[index] == null) {
return One.MJIEnvNULL;
}
return locals[index].simplify(ctx).map(GetLocal).simplifyValues();
} else {
final int i = index - locals.length;
return stack.map(new Function<Stack, Integer>() {
@Override
public Integer apply(final Stack stack) {
return stack.get(i);
}
});
}
}
@Override
public Object getLocal(int index) {
return locals[index].simplify(getCtx());
}
private static final Function<Entry, Integer> GetLocal = new Function<Entry, Integer>() {
@Override
public Integer apply(final Entry x) {
if (x == null) {
return MJIEnv.NULL;
}
return x.value;
}
};
// TODO change to conditional
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#isRefLocal(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public boolean isRefLocal(FeatureExpr ctx, final int index) {
if (index < 0) {
return false;
}
if (index < locals.length) {
if (locals[index] == null) {
return false;
}
// TODO check calls of isRefLocal
for (boolean b : locals[index].simplify(ctx).map(IsRefLocal).toList()) {
if (b) {
return true;
}
}
return false;
// return locals[index].simplify(ctx).map(new IsRefLocal()).simplifyValues().getValue();
} else {
final int i = index - locals.length;
return stack.simplify(ctx).map(new Function<Stack, Boolean>() {
@Override
public Boolean apply(final Stack stack) {
return stack.isRefIndex(i);
}
}).simplifyValues().getValue();
}
}
private static final Function<Entry, Boolean> IsRefLocal = new Function<Entry, Boolean>() {
@Override
public Boolean apply(final Entry y) {
return y.isRef;
}
};
/*
* #######################################################
* Handling of the stack
* ########################################################
*/
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#push(de.fosd.typechef.featureexpr.FeatureExpr, T)
*/
@Override
public <T> void push(final FeatureExpr ctx, final T value) {
push(ctx, value, false);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#push(de.fosd.typechef.featureexpr.FeatureExpr, java.lang.Object, boolean)
*/
@Override
@SuppressWarnings("unchecked")
public void push(final FeatureExpr ctx, final Object value, final boolean isRef) {
if (value instanceof Conditional) {
((Conditional<Object>) value).mapf(ctx, new VoidBiFunction<FeatureExpr, Object>() {
@Override
public void apply(final FeatureExpr ctx, final Object value) {
push(ctx, value, isRef);
}
});
return;
}
stack = stack.mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<Stack>>() {
@Override
public Conditional<Stack> apply(final FeatureExpr f, final Stack stack) {
if (Conditional.isContradiction(f)) {
return new One<>(stack);
}
Stack clone = stack.copy();
if (value instanceof Integer) {
clone.push((Integer) value, isRef);
} else if (value instanceof Long) {
long v = ((Long) value).longValue();
clone.push((int) (v >> 32), isRef);
clone.push((int) v, isRef);
} else if (value instanceof Double) {
long v = Double.doubleToLongBits((Double) value);
clone.push((int) (v >> 32), isRef);
clone.push((int) v, isRef);
} else if (value instanceof Float) {
clone.push(Float.floatToIntBits((Float) value), isRef);
} else if (value instanceof Byte) {
clone.push(((Byte)value).intValue(), isRef);
} else if (value instanceof Short) {
clone.push((int)(Short)value, isRef);
} else if (value == null) {
clone.push(MJIEnv.NULL, isRef);
} else {
throw new RuntimeException(value + " of type " + value.getClass() + " cannot be pushed to the stack.");
}
if (stackCTX.equivalentTo(f)) {
return new One<>(clone);
}
return ChoiceFactory.create(ctx, new One<>(clone), new One<>(stack));
}
}).simplify();
c.push(this, ctx, value, isRef);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#pop(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public Conditional<Integer> pop(final FeatureExpr ctx) {
return pop(ctx, Type.INT);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#popLong(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public Conditional<Long> popLong(final FeatureExpr ctx) {
return pop(ctx, Type.LONG);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#popDouble(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public Conditional<Double> popDouble(final FeatureExpr ctx) {
return pop(ctx, Type.DOUBLE);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#popFloat(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public Conditional<Float> popFloat(final FeatureExpr ctx) {
return pop(ctx, Type.FLOAT);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#pop(de.fosd.typechef.featureexpr.FeatureExpr, gov.nasa.jpf.vm.StackHandler.Type)
*/
@Override
public <T> Conditional<T> pop(final FeatureExpr ctx, final Type t) {
Conditional<T> result = stack.simplify(ctx).mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<T>>() {
@SuppressWarnings("unchecked")
@Override
public Conditional<T> apply(final FeatureExpr f, final Stack s) {
Stack clone = s.copy();
Number res;
final int lo = clone.pop();
switch (t) {
case INT:
res = Integer.valueOf(lo);
break;
case DOUBLE:
res = Types.intsToDouble(lo, clone.pop());
break;
case FLOAT:
res = Types.intToFloat(lo);
break;
case LONG:
res = Types.intsToLong(lo, clone.pop());
break;
default:
return null;
}
if (stackCTX.equivalentTo(f)) {
stack = new One<>(clone);
} else {
stack = ChoiceFactory.create(f, new One<>(clone), stack);
}
return (Conditional<T>) new One<>(res);
}
}).simplifyValues();
stack = stack.simplify();
c.pop(this, ctx,0);
return result;
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#pop(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public void pop(FeatureExpr ctx, final int n) {
stack = stack.mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<Stack>>() {
@Override
public Conditional<Stack> apply(final FeatureExpr f, final Stack s) {
if (Conditional.isContradiction(f)) {
return new One<>(s);
}
Stack clone = s.copy();
for (int i = n; i > 0; i--) {
clone.pop();
}
if (Conditional.isTautology(f)) {
return new One<>(clone);
}
return ChoiceFactory.create(f, new One<>(clone), new One<>(s));
}
}).simplify();
c.pop(this, ctx, n);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#peek(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public Conditional<Integer> peek(FeatureExpr ctx) {
return peek(ctx, 0);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#peek(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public Conditional<Integer> peek(FeatureExpr ctx, final int offset) {
return peek(ctx, offset, Type.INT);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#peekDouble(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public Conditional<Double> peekDouble(FeatureExpr ctx, final int offset) {
return peek(ctx, offset, Type.DOUBLE);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#peekLong(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public Conditional<Long> peekLong(FeatureExpr ctx, final int offset) {
return peek(ctx, offset, Type.LONG);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#peekFloat(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public Conditional<Float> peekFloat(FeatureExpr ctx, final int offset) {
return peek(ctx, offset, Type.FLOAT);
}
private <T> Conditional<T> peek(FeatureExpr ctx, final int offset, final Type t) {
return stack.simplify(ctx).map(new Function<Stack, T>() {
@SuppressWarnings("unchecked")
@Override
public T apply(final Stack stack) {
switch (t) {
case DOUBLE:
return (T) (Double) Types.intsToDouble(stack.peek(offset), stack.peek(offset + 1));
case FLOAT:
return (T) (Float) Types.intToFloat(stack.peek(offset));
case INT:
return (T) stack.peek(offset);
case LONG:
return (T) (Long) Types.intsToLong(stack.peek(offset), stack.peek(offset + 1));
default:
return null;
}
}
}).simplifyValues();
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#isRef(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public boolean isRef(final FeatureExpr ctx, final int offset) {// change to Conditional<Boolean>
return stack.simplify(ctx).map(new Function<Stack, Boolean>() {
@Override
public Boolean apply(final Stack y) {
return y.isRef(offset);
}
}).simplifyValues().getValue();
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#set(de.fosd.typechef.featureexpr.FeatureExpr, int, int, boolean)
*/
@Override
public void set(final FeatureExpr ctx, final int offset, final int value, final boolean isRef) {
stack = stack.mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<Stack>>() {
@Override
public Conditional<Stack> apply(FeatureExpr f, Stack stack) {
if (f.isContradiction()) {
return new One<>(stack);
}
Stack clone = stack.copy();
clone.set(offset, value, isRef);
if (f.isTautology()) {
return new One<>(clone);
}
return ChoiceFactory.create(ctx, new One<>(clone), new One<>(stack));
}
}).simplify();
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#getTop()
*/
@Override
public Conditional<Integer> getTop() {
return stack.map(GetTop);
}
private static final Function<Stack, Integer> GetTop = new Function<Stack, Integer>() {
@Override
public Integer apply(final Stack y) {
return y.top;
}
};
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#setTop(de.fosd.typechef.featureexpr.FeatureExpr, int)
*/
@Override
public void setTop(final FeatureExpr ctx, final int i) {
stack = stack.mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<Stack>>() {
@Override
public Conditional<Stack> apply(final FeatureExpr f, final Stack stack) {
if (Conditional.isContradiction(f)) {
return new One<>(stack);
}
Stack clone = stack.copy();
clone.top = i;
if (ctx.equals(f)) {
return new One<>(clone);
}
if (Conditional.isTautology(f)) {
return new One<>(clone);
}
return ChoiceFactory.create(ctx, new One<>(clone), new One<>(stack));
}
}).simplify();
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#clear(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void clear(final FeatureExpr ctx) {
stack = stack.mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<Stack>>() {
@Override
public Conditional<Stack> apply(final FeatureExpr f, final Stack stack) {
if (Conditional.isContradiction(f)) {
return new One<>(stack);
}
Stack clone = stack.copy();
clone.clear();
if (Conditional.isTautology(f)) {
return new One<>(clone);
}
return ChoiceFactory.create(ctx, new One<>(clone), new One<>(stack));
}
}).simplify();
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#getSlots()
*/
@Override
public int[] getSlots() {
return getSlots(FeatureExprFactory.True());
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#getSlots(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public int[] getSlots(FeatureExpr ctx) {
int[] slots = new int[length];
int i = 0;
for (Conditional<Entry> l : locals) {
if (l == null) {
slots[i++] = MJIEnv.NULL;
continue;
}
slots[i++] = l.simplify(ctx).getValue(true).value;
}
for (int o : stack.simplify(ctx).getValue(true).getSlots()) {
slots[i++] = o;
}
return slots;
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (!(o instanceof StackHandler)) {
return false;
}
StackHandler other = (StackHandler) o;
for (int i = 0; i < locals.length; i++) {
Conditional<Entry> l1 = locals[i];
Conditional<Entry> l2 = other.locals[i];
if (l1 == l2) {
continue;
}
if (l1 == null) {
return false;
}
if (!l1.equals(l2)) {
return false;
}
}
if (!other.stack.equals(stack)) {
return false;
}
return true;
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#hashCode()
*/
@Override
public int hashCode() {
throw new RuntimeException("hashCode not designed");
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#hasAnyRef(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public boolean hasAnyRef(FeatureExpr ctx) {
for (Conditional<Entry> local : locals) {
if (local == null) {
continue;
}
for (Entry entry : local.simplify(ctx).toList()) {
if (entry.isRef) {
return true;
}
}
}
for (Stack s : stack.simplify(ctx).toList()) {
if (s.hasAnyRef()) {
return true;
}
}
return false;
}
/*
* Stack Instructions
*/
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#dup_x1(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void dup_x1(final FeatureExpr ctx) {
function(ctx, StackInstruction.DUP_X1);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#dup2_x2(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void dup2_x2(final FeatureExpr ctx) {
function(ctx, StackInstruction.DUP2_X2);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#dup2_x1(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void dup2_x1(final FeatureExpr ctx) {
function(ctx, StackInstruction.DUP2_X1);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#dup2(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void dup2(final FeatureExpr ctx) {
function(ctx, StackInstruction.DUP2);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#dup(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void dup(final FeatureExpr ctx) {
function(ctx, StackInstruction.DUP);
c.dup(this, ctx);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#dup_x2(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void dup_x2(final FeatureExpr ctx) {
function(ctx, StackInstruction.DUP_X2);
}
/* (non-Javadoc)
* @see gov.nasa.jpf.vm.IStackHandler#swap(de.fosd.typechef.featureexpr.FeatureExpr)
*/
@Override
public void swap(final FeatureExpr ctx) {
function(ctx, StackInstruction.SWAP);
}
void function(final FeatureExpr ctx, final StackInstruction instruction) {
stack = stack.mapf(ctx, new BiFunction<FeatureExpr, Stack, Conditional<Stack>>() {
@Override
public Conditional<Stack> apply(final FeatureExpr f, final Stack stack) {
if (Conditional.isContradiction(f)) {
return new One<>(stack);
}
Stack clone = stack.copy();
switch (instruction) {
case DUP_X1:
clone.dup_x1();
break;
case DUP2_X2:
clone.dup2_x2();
break;
case DUP:
clone.dup();
break;
case DUP2:
clone.dup2();
break;
case DUP2_X1:
clone.dup2_x1();
break;
case DUP_X2:
clone.dup_x2();
break;
case SWAP:
clone.swap();
break;
default:
throw new RuntimeException(instruction + "not supported");
}
if (Conditional.isTautology(f)) {
return new One<>(clone);
}
if (stackCTX.equivalentTo(f)) {
return new One<>(clone);
}
return ChoiceFactory.create(ctx, new One<>(clone), new One<>(stack));
}
}).simplify();
}
@Override
public int getLength() {
return length;
}
@Override
public Conditional<Stack> getStack() {
return stack;
}
@Override
public Set<Integer> getAllReferences() {
Set<Integer> references = new HashSet<>();
for (Conditional<Entry> cl : locals) {
for (Entry l: cl.toList()) {
if (l.isRef) {
references.add(l.value);
}
}
}
for (Stack s: stack.toList()) {
references.addAll(s.getReferences());
}
return references;
}
@Override
public int getLocalWidth() {
int width = -locals.length;
for (Conditional<Entry> local : locals) {
width += local.simplify(getCtx()).toMap().size();
}
return width;
}
@Override
public String getMaxLocal() {
StringBuilder builder = new StringBuilder();
for (Conditional<Entry> local : locals) {
int size = local.simplify(getCtx()).toMap().size();
builder.append(local.simplify(getCtx()));
builder.append(":");
builder.append(size);
builder.append('\n');
}
return builder.toString();
}
@Override
public void IINC(FeatureExpr ctx, int index, final int increment) {
locals[index] = locals[index].mapf(ctx, new BiFunction<FeatureExpr, Entry, Conditional<Entry>>() {
@Override
public Conditional<Entry> apply(FeatureExpr ctx, Entry y) {
if (Conditional.isContradiction(ctx)) {
return new One<>(y);
}
Entry copy = new Entry(y.value + increment, y.isRef);
if (Conditional.isTautology(ctx)) {
return new One<>(copy);
}
return ChoiceFactory.create(ctx, new One<>(copy), new One<>(y));
}
}).simplify();
}
}