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
|
//
// OldScanStateMachine.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2011 Alexander Chebaturkin
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using Mono.CodeContracts.Static.AST;
using Mono.CodeContracts.Static.AST.Visitors;
using Mono.CodeContracts.Static.ControlFlow.Blocks;
using Mono.CodeContracts.Static.DataStructures;
namespace Mono.CodeContracts.Static.ControlFlow.Subroutines {
class OldScanStateMachine<Label> : ILVisitorBase<Label, Dummy, Dummy, int, bool>,
IAggregateVisitor<Label, int, bool> {
private readonly Dictionary<CFGBlock, ScanState> block_start_state = new Dictionary<CFGBlock, ScanState> ();
private readonly EnsuresSubroutine<Label> subroutine;
private EnsuresBlock<Label> current_block;
private TypeNode next_end_old_type;
private ScanState state;
public OldScanStateMachine (EnsuresSubroutine<Label> ensuresSubroutine)
{
this.subroutine = ensuresSubroutine;
this.state = ScanState.OutsideOld;
}
#region Overrides of ILVisitorBase<Label,Local,Parameter,Method,Field,Type,Dummy,Dummy,int,bool>
public override bool DefaultVisit (Label pc, int index)
{
if (this.state != ScanState.InsertingOld)
return this.current_block.UsesOverriding;
this.state = ScanState.OutsideOld;
this.current_block.EndOldWithoutInstruction (this.subroutine.SubroutineFacade.MetaDataProvider.ManagedPointer (this.next_end_old_type));
return true;
}
#endregion
#region Implementation of ICodeQuery<Label,Local,Parameter,Method,Field,Type,int,bool>
public bool Aggregate (Label pc, Label aggregateStart, bool canBeTargetOfBranch, int data)
{
return Nop (pc, data);
}
#endregion
#region IAggregateVisitor<Label,int,bool> Members
public override bool Nop (Label pc, int data)
{
return this.current_block.UsesOverriding;
}
public override bool BeginOld (Label pc, Label matchingEnd, int data)
{
this.state = ScanState.InsideOld;
return this.current_block.UsesOverriding;
}
public override bool EndOld (Label pc, Label matchingEnd, TypeNode type, Dummy dest, Dummy source, int data)
{
this.state = ScanState.OutsideOld;
return this.current_block.UsesOverriding;
}
public override bool LoadField (Label pc, Field field, Dummy dest, Dummy obj, int data)
{
if (this.state != ScanState.InsertingOld)
return this.current_block.UsesOverriding;
this.state = ScanState.OutsideOld;
this.current_block.EndOld (data, this.subroutine.SubroutineFacade.MetaDataProvider.FieldType (field));
return false;
}
public override bool LoadFieldAddress (Label pc, Field field, Dummy dest, Dummy obj, int data)
{
if (this.state == ScanState.InsertingOld)
this.next_end_old_type = this.subroutine.SubroutineFacade.MetaDataProvider.FieldType (field);
return this.current_block.UsesOverriding;
}
public override bool Call<TypeList, ArgList> (Label pc, Method method, bool virt, TypeList extraVarargs, Dummy dest, ArgList args, int data)
{
return false;
}
#endregion
public void HandlePotentialCallBlock (MethodCallBlock<Label> block, EnsuresBlock<Label> priorBlock)
{
if (block == null || this.state != ScanState.InsertingOld)
return;
int count = this.subroutine.SubroutineFacade.MetaDataProvider.Parameters (block.CalledMethod).Count;
if (!this.subroutine.SubroutineFacade.MetaDataProvider.IsStatic (block.CalledMethod))
++count;
if (count > 1) {
this.state = ScanState.OutsideOld;
TypeNode mp = this.subroutine.SubroutineFacade.MetaDataProvider.ManagedPointer (this.next_end_old_type);
priorBlock.EndOldWithoutInstruction (mp);
} else {
this.state = ScanState.InsertingOldAfterCall;
this.next_end_old_type = this.subroutine.SubroutineFacade.MetaDataProvider.ReturnType (block.CalledMethod);
}
}
public void StartBlock (EnsuresBlock<Label> block)
{
if (!this.block_start_state.TryGetValue (block, out this.state)) {
this.state = ScanState.OutsideOld;
this.block_start_state.Add (block, this.state);
}
if (this.state == ScanState.InsertingOld)
block.StartOverridingLabels ();
if (this.state == ScanState.InsertingOldAfterCall) {
block.StartOverridingLabels ();
block.EndOldWithoutInstruction (this.next_end_old_type);
this.state = ScanState.OutsideOld;
}
this.current_block = block;
}
public void SetStartState (CFGBlock succ)
{
ScanState sstate;
if (!this.block_start_state.TryGetValue (succ, out sstate))
this.block_start_state.Add (succ, this.state);
}
#region Nested type: ScanState
private enum ScanState {
OutsideOld,
InsideOld,
InsertingOld,
InsertingOldAfterCall
}
#endregion
}
}
|