Skip to content

Commit e616b05

Browse files
authored
Merge pull request #295 from oveeernight/splitting
Splitting
2 parents 7a70925 + 7b6e6a2 commit e616b05

36 files changed

+1279
-748
lines changed

VSharp.InternalCalls/ByReference.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ module ByReference =
1818
assert(isValueField field)
1919
Memory.ReferenceField state this field
2020

21-
let internal ctor (state : state) (args : term list) : (term * state) list =
21+
let internal ctor (state : state) (args : term list) : term =
2222
assert(List.length args = 3)
2323
let this, ref = List.item 0 args, List.item 2 args
2424
let fieldRef = referenceValueField state this
25-
Memory.Write state fieldRef ref |> List.map (withFst (Nop()))
25+
Memory.Write state fieldRef ref
26+
Nop()
2627

2728
let internal getValue (state : state) (args : term list) : term =
2829
assert(List.length args = 2)

VSharp.InternalCalls/ByReference.fsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module internal ByReference =
99
val isValueField : fieldId -> bool
1010

1111
[<Implements("System.Void System.ByReference`1[T]..ctor(this, T&)")>]
12-
val ctor : state -> term list -> (term * state) list
12+
val ctor : state -> term list -> term
1313

1414
[<Implements("T& System.ByReference`1[T].get_Value(this)")>]
1515
val getValue : state -> term list -> term

VSharp.InternalCalls/Enum.fs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ module internal Enum =
6868
valuesList.Add(TypeUtils.convert v typeof<UInt64> :?> UInt64)
6969
let values = Memory.ObjectToTerm state (valuesList.ToArray()) typeof<UInt64[]>
7070
let valuesCase state k =
71-
Memory.Write state valuesPtr values |> k
71+
Memory.Write state valuesPtr values
72+
List.singleton state |> k
7273
let namesAndValuesCase state k =
7374
let names = Memory.ObjectToTerm state names typeof<string[]>
7475
let namesPtr = Memory.ReadField state namesRef refField
75-
let states = Memory.Write state valuesPtr values
76-
List.collect (fun state -> Memory.Write state namesPtr names) states |> k
76+
Memory.Write state valuesPtr values
77+
Memory.Write state namesPtr names
78+
List.singleton state |> k
7779
let needNames = (Types.Cast getNamesFlag typeof<int>) === (MakeNumber 1)
7880
StatedConditionalExecutionAppend state
7981
(fun state k -> k (needNames, state))

VSharp.InternalCalls/Environment.fs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ module internal Environment =
3636
let dir = Memory.AllocateDefaultClass state t
3737
let fields = Reflection.fieldsOf false t
3838
let nameField = fields |> Array.find (fun (f, _) -> f.name = "_name") |> fst
39-
let states = Memory.WriteClassField state dir nameField name
40-
assert(List.length states = 1)
39+
Memory.WriteClassField state dir nameField name
4140
dir
4241

4342
let FileExists (_ : state) (args : term list) =

VSharp.InternalCalls/IntPtr.fs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ open VSharp.Core
88

99
module IntPtr =
1010

11-
let private intPtrCtor (state : state) this term : (term * state) list =
11+
let private intPtrCtor (state : state) this term : term =
1212
let ptr = MakeIntPtr term
13-
Memory.Write state this ptr |> List.map (withFst (Nop()))
13+
Memory.Write state this ptr
14+
Nop()
15+
1416

15-
let internal intPtrCtorFromInt (state : state) (args : term list) : (term * state) list =
17+
let internal intPtrCtorFromInt (state : state) (args : term list) : term =
1618
assert(List.length args = 2)
1719
let this, intTerm = List.item 0 args, List.item 1 args
1820
intPtrCtor state this intTerm
1921

20-
let internal intPtrCtorFromPtr (state : state) (args : term list) : (term * state) list =
22+
let internal intPtrCtorFromPtr (state : state) (args : term list) : term =
2123
assert(List.length args = 2)
2224
let this, ptrTerm = List.item 0 args, List.item 1 args
2325
intPtrCtor state this ptrTerm
2426

25-
let internal intPtrCtorFromLong (state : state) (args : term list) : (term * state) list =
27+
let internal intPtrCtorFromLong (state : state) (args : term list) : term =
2628
assert(List.length args = 2)
2729
let this, intTerm = List.item 0 args, List.item 1 args
2830
intPtrCtor state this intTerm
@@ -37,21 +39,22 @@ module IntPtr =
3739

3840
module UIntPtr =
3941

40-
let private uintPtrCtor (state : state) this term : (term * state) list =
42+
let private uintPtrCtor (state : state) this term : term =
4143
let ptr = MakeUIntPtr term
42-
Memory.Write state this ptr |> List.map (withFst (Nop()))
44+
Memory.Write state this ptr
45+
Nop()
4346

44-
let internal uintPtrCtorFromUInt (state : state) (args : term list) : (term * state) list =
47+
let internal uintPtrCtorFromUInt (state : state) (args : term list) : term =
4548
assert(List.length args = 2)
4649
let this, intTerm = List.item 0 args, List.item 1 args
4750
uintPtrCtor state this intTerm
4851

49-
let internal uintPtrCtorFromPtr (state : state) (args : term list) : (term * state) list =
52+
let internal uintPtrCtorFromPtr (state : state) (args : term list) : term =
5053
assert(List.length args = 2)
5154
let this, ptrTerm = List.item 0 args, List.item 1 args
5255
uintPtrCtor state this ptrTerm
5356

54-
let internal uintPtrCtorFromULong (state : state) (args : term list) : (term * state) list =
57+
let internal uintPtrCtorFromULong (state : state) (args : term list) : term =
5558
assert(List.length args = 2)
5659
let this, intTerm = List.item 0 args, List.item 1 args
5760
uintPtrCtor state this intTerm

VSharp.InternalCalls/IntPtr.fsi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ open VSharp.Core
77
module internal IntPtr =
88

99
[<Implements("System.Void System.IntPtr..ctor(this, System.Int32)")>]
10-
val intPtrCtorFromInt : state -> term list -> (term * state) list
10+
val intPtrCtorFromInt : state -> term list -> term
1111

1212
[<Implements("System.Void System.IntPtr..ctor(this, System.Void*)")>]
13-
val intPtrCtorFromPtr : state -> term list -> (term * state) list
13+
val intPtrCtorFromPtr : state -> term list -> term
1414

1515
[<Implements("System.Void System.IntPtr..ctor(this, System.Int64)")>]
16-
val intPtrCtorFromLong : state -> term list -> (term * state) list
16+
val intPtrCtorFromLong : state -> term list -> term
1717

1818
[<Implements("System.Void* System.IntPtr.ToPointer(this)")>]
1919
val intPtrToPointer : state -> term list -> term
2020

2121
module internal UIntPtr =
2222

2323
[<Implements("System.Void System.UIntPtr..ctor(this, System.UInt32)")>]
24-
val uintPtrCtorFromUInt : state -> term list -> (term * state) list
24+
val uintPtrCtorFromUInt : state -> term list -> term
2525

2626
[<Implements("System.Void System.UIntPtr..ctor(this, System.Void*)")>]
27-
val uintPtrCtorFromPtr : state -> term list -> (term * state) list
27+
val uintPtrCtorFromPtr : state -> term list -> term
2828

2929
[<Implements("System.Void System.UIntPtr..ctor(this, System.UInt64)")>]
30-
val uintPtrCtorFromULong : state -> term list -> (term * state) list
30+
val uintPtrCtorFromULong : state -> term list -> term
3131

3232
[<Implements("System.Void* System.UIntPtr.ToPointer(this)")>]
3333
val uintPtrToPointer : state -> term list -> term

VSharp.InternalCalls/String.fs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ module internal String =
162162
let char = Memory.ReadStringChar cilState.state this index
163163
cilState.Push char
164164
List.singleton cilState |> k
165-
let arrayLength = Add length (MakeNumber 1)
166-
interpreter.AccessArray getChar cilState arrayLength index id
165+
interpreter.AccessArray getChar cilState length index id
167166

168167
let AllCharsInUInt32AreAscii (_ : state) (_ : term list) =
169168
// TODO: try to not internal call it

VSharp.SILI.Core/API.fs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module API =
8080
let Ref address = Ref address
8181
let Ptr baseAddress typ offset = Ptr baseAddress typ offset
8282
let HeapRef address baseType = HeapRef address baseType
83-
let Union gvs = Union gvs
83+
let Ite iteType = Ite iteType
8484

8585
let True() = True()
8686
let False() = False()
@@ -426,9 +426,9 @@ module API =
426426
let index = indices[0]
427427
t, mul index (TypeUtils.internalSizeOf t |> makeNumber)
428428
Ptr baseAddress sightType (add offset indexOffset)
429-
| Union gvs ->
429+
| Ite iteType ->
430430
let referenceArrayIndex term = ReferenceArrayIndex state term indices valueType
431-
Merging.guardedMap referenceArrayIndex gvs
431+
Merging.guardedMap referenceArrayIndex iteType
432432
| _ -> internalfail $"Referencing array index: expected reference, but got {arrayRef}"
433433

434434
let ReferenceField state reference fieldId =
@@ -468,11 +468,6 @@ module API =
468468
let TryAddressFromRefFork state ref =
469469
CommonTryAddressFromRef state ref true
470470

471-
let private transformBoxedRef ref =
472-
match ref.term with
473-
| HeapRef _ -> HeapReferenceToBoxReference ref
474-
| _ -> ref
475-
476471
let ExtractAddress ref = extractAddress ref
477472
let ExtractPointerOffset ptr = extractPointerOffset ptr
478473

@@ -485,16 +480,17 @@ module API =
485480
let ReadThis state method = state.memory.ReadStackLocation (ThisKey method)
486481
let ReadArgument state parameterInfo = state.memory.ReadStackLocation (ParameterKey parameterInfo)
487482

488-
let CommonReadField reporter state term field =
489-
let doRead target =
490-
match target.term with
491-
| HeapRef _
492-
| Ptr _
493-
| Ref _ -> ReferenceField state target field |> state.memory.Read reporter
494-
| Struct _ -> state.memory.ReadStruct reporter target field
495-
| Combined _ -> state.memory.ReadFieldUnsafe reporter target field
496-
| _ -> internalfail $"Reading field of {term}"
497-
Merging.guardedApply doRead term
483+
let rec CommonReadField reporter state term field =
484+
match term.term with
485+
| HeapRef _
486+
| Ptr _
487+
| Ref _ -> ReferenceField state term field |> state.memory.Read reporter
488+
| Struct _ -> state.memory.ReadStruct reporter term field
489+
| Combined _ -> state.memory.ReadFieldUnsafe reporter term field
490+
| Ite iteType ->
491+
iteType.filter (fun v -> True() <> IsBadRef v)
492+
|> Merging.guardedMap (fun t -> CommonReadField reporter state t field)
493+
| _ -> internalfail $"Reading field of {term}"
498494

499495
let ReadField state term field =
500496
CommonReadField emptyReporter state term field
@@ -522,9 +518,9 @@ module API =
522518
| HeapRef(addr, typ) when state.memory.MostConcreteTypeOfHeapRef addr typ = typeof<string> ->
523519
let addr, arrayType = state.memory.StringArrayInfo addr None
524520
state.memory.ReadArrayIndex addr [index] arrayType
525-
| Union gvs ->
521+
| Ite iteType ->
526522
let readStringChar term = ReadStringChar state term index
527-
Merging.guardedMap readStringChar gvs
523+
Merging.guardedMap readStringChar iteType
528524
| _ -> internalfail $"Reading string char: expected reference, but got {reference}"
529525
let ReadStaticField state typ field = state.memory.ReadStaticField typ field
530526
let ReadDelegate state reference = state.memory.ReadDelegate reference
@@ -538,16 +534,11 @@ module API =
538534

539535
let WriteStackLocation state location value = state.memory.WriteStackLocation location value
540536

541-
let Write state reference value =
542-
let write state reference =
543-
state.memory.Write emptyReporter (transformBoxedRef reference) value
544-
Branching.guardedStatedMap write state reference
537+
let Write state reference value = state.memory.Write emptyReporter reference value
545538

546539
let WriteUnsafe (reporter : IErrorReporter) state reference value =
547-
let write state reference =
548-
reporter.ConfigureState state
549-
state.memory.Write reporter reference value
550-
Branching.guardedStatedMap write state reference
540+
reporter.ConfigureState state
541+
state.memory.Write reporter reference value
551542

552543
let WriteStructField structure field value = writeStruct structure field value
553544

@@ -556,13 +547,17 @@ module API =
556547
writeStruct structure field value
557548

558549
let WriteClassFieldUnsafe (reporter : IErrorReporter) state reference field value =
559-
let write state reference =
560-
reporter.ConfigureState state
561-
match reference.term with
562-
| HeapRef(addr, _) -> state.memory.WriteClassField addr field value
550+
let extractAddress ref =
551+
match ref.term with
552+
| HeapRef(addr, _) -> addr
563553
| _ -> internalfail $"Writing field of class: expected reference, but got {reference}"
564-
state
565-
Branching.guardedStatedMap write state reference
554+
reporter.ConfigureState state
555+
match reference.term with
556+
| Ite iteType ->
557+
let filtered = iteType.filter (fun r -> Pointers.isBadRef r |> isTrue |> not)
558+
filtered.ToDisjunctiveGvs()
559+
|> List.iter (fun (g, r) -> state.memory.GuardedWriteClassField (Some g) (extractAddress r) field value)
560+
| _ -> state.memory.WriteClassField (extractAddress reference) field value
566561

567562
let WriteClassField state reference field value =
568563
WriteClassFieldUnsafe emptyReporter state reference field value
@@ -766,26 +761,26 @@ module API =
766761
let rec ArrayRank state arrayRef =
767762
match arrayRef.term with
768763
| HeapRef(addr, typ) -> state.memory.MostConcreteTypeOfHeapRef addr typ |> TypeUtils.rankOf |> makeNumber
769-
| Union gvs -> Merging.guardedMap (ArrayRank state) gvs
764+
| Ite iteType -> Merging.guardedMap (ArrayRank state) iteType
770765
| _ -> internalfail $"Getting rank of array: expected ref, but got {arrayRef}"
771766

772767
let rec ArrayLengthByDimension state arrayRef index =
773768
match arrayRef.term with
774769
| HeapRef(addr, typ) ->
775770
let memory = state.memory
776771
memory.MostConcreteTypeOfHeapRef addr typ |> symbolicTypeToArrayType |> memory.ReadLength addr index
777-
| Union gvs ->
772+
| Ite iteType ->
778773
let arrayLengthByDimension term = ArrayLengthByDimension state term index
779-
Merging.guardedMap arrayLengthByDimension gvs
774+
Merging.guardedMap arrayLengthByDimension iteType
780775
| _ -> internalfail $"reading array length: expected heap reference, but got {arrayRef}"
781776
let rec ArrayLowerBoundByDimension state arrayRef index =
782777
match arrayRef.term with
783778
| HeapRef(addr, typ) ->
784779
let memory = state.memory
785780
memory.MostConcreteTypeOfHeapRef addr typ |> symbolicTypeToArrayType |> memory.ReadLowerBound addr index
786-
| Union gvs ->
781+
| Ite iteType ->
787782
let arrayLowerBoundByDimension term = ArrayLowerBoundByDimension state term index
788-
Merging.guardedMap arrayLowerBoundByDimension gvs
783+
Merging.guardedMap arrayLowerBoundByDimension iteType
789784
| _ -> internalfail $"reading array lower bound: expected heap reference, but got {arrayRef}"
790785

791786
let rec CountOfArrayElements state arrayRef =
@@ -795,7 +790,7 @@ module API =
795790
let arrayType = memory.MostConcreteTypeOfHeapRef address typ |> symbolicTypeToArrayType
796791
let lens = List.init arrayType.dimension (fun dim -> memory.ReadLength address (makeNumber dim) arrayType)
797792
List.fold mul (makeNumber 1) lens
798-
| Union gvs -> Merging.guardedMap (CountOfArrayElements state) gvs
793+
| Ite iteType -> Merging.guardedMap (CountOfArrayElements state) iteType
799794
| _ -> internalfail $"counting array elements: expected heap reference, but got {arrayRef}"
800795

801796
let StringLength state strRef = state.memory.LengthOfString strRef

VSharp.SILI.Core/API.fsi

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ module API =
5151
val Ref : address -> term
5252
val Ptr : pointerBase -> Type -> term -> term
5353
val HeapRef : heapAddress -> Type -> term
54-
val Union : (term * term) list -> term
55-
54+
val Ite : iteType -> term
5655
val True : unit -> term
5756
val False : unit -> term
5857
val NullRef : Type -> term
@@ -295,14 +294,14 @@ module API =
295294

296295
val InitializeArray : state -> term -> term -> unit
297296

298-
val Write : state -> term -> term -> state list
299-
val WriteUnsafe : IErrorReporter -> state -> term -> term -> state list
297+
val Write : state -> term -> term -> unit
298+
val WriteUnsafe : IErrorReporter -> state -> term -> term -> unit
300299
val WriteStackLocation : state -> stackKey -> term -> unit
301300
val WriteStructField : term -> fieldId -> term -> term
302301
val WriteStructFieldUnsafe : IErrorReporter -> state -> term -> fieldId -> term -> term
303-
val WriteClassFieldUnsafe : IErrorReporter -> state -> term -> fieldId -> term -> state list
304-
val WriteClassField : state -> term -> fieldId -> term -> state list
305-
val WriteArrayIndexUnsafe : IErrorReporter -> state -> term -> term list -> term -> Type option -> state list
302+
val WriteClassFieldUnsafe : IErrorReporter -> state -> term -> fieldId -> term -> unit
303+
val WriteClassField : state -> term -> fieldId -> term -> unit
304+
val WriteArrayIndexUnsafe : IErrorReporter -> state -> term -> term list -> term -> Type option -> unit
306305
val WriteStaticField : state -> Type -> fieldId -> term -> unit
307306

308307
val DefaultOf : Type -> term

0 commit comments

Comments
 (0)