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
|
//
// SubroutineBase.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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.CodeContracts.Static.AST;
using Mono.CodeContracts.Static.Analysis;
using Mono.CodeContracts.Static.ControlFlow.Blocks;
using Mono.CodeContracts.Static.ControlFlow.Subroutines.Builders;
using Mono.CodeContracts.Static.DataStructures;
using Mono.CodeContracts.Static.Providers;
namespace Mono.CodeContracts.Static.ControlFlow.Subroutines {
abstract class SubroutineBase<Label> : Subroutine, IGraph<CFGBlock, Dummy>, IStackInfo, IEdgeSubroutineAdaptor {
private const int UnusedBlockIndex = Int32.MaxValue - 1;
protected readonly Label StartLabel;
public readonly SubroutineFacade SubroutineFacade;
private readonly Dictionary<Pair<CFGBlock, CFGBlock>, Sequence<Pair<EdgeTag, Subroutine>>> edge_subroutines
= new Dictionary<Pair<CFGBlock, CFGBlock>, Sequence<Pair<EdgeTag, Subroutine>>> ();
private readonly BlockWithLabels<Label> entry;
private readonly BlockWithLabels<Label> entry_after_requires;
private readonly BlockWithLabels<Label> exception_exit;
private readonly BlockWithLabels<Label> exit;
private readonly List<Edge<CFGBlock, EdgeTag>> successors = new List<Edge<CFGBlock, EdgeTag>> ();
protected int BlockIdGenerator;
protected Dictionary<Label, BlockWithLabels<Label>> LabelsThatStartBlocks = new Dictionary<Label, BlockWithLabels<Label>> ();
private CFGBlock[] blocks;
private DepthFirst.Visitor<CFGBlock, Dummy> edge_info;
private EdgeMap<EdgeTag> predecessor_edges;
private EdgeMap<EdgeTag> successor_edges;
protected SubroutineBase (SubroutineFacade subroutineFacade)
{
this.SubroutineFacade = subroutineFacade;
this.entry = new EntryBlock<Label> (this, ref this.BlockIdGenerator);
this.exit = new EntryExitBlock<Label> (this, ref this.BlockIdGenerator);
this.exception_exit = new CatchFilterEntryBlock<Label> (this, ref this.BlockIdGenerator);
}
protected SubroutineBase (SubroutineFacade facade,
Label startLabel, SubroutineBuilder<Label> builder)
: this (facade)
{
this.StartLabel = startLabel;
Builder = builder;
CodeProvider = builder.CodeProvider;
this.entry_after_requires = GetTargetBlock (startLabel);
AddSuccessor (this.entry, EdgeTag.Entry, this.entry_after_requires);
}
public override bool HasContextDependentStackDepth
{
get { return true; }
}
public override bool HasReturnValue
{
get { return false; }
}
public override int StackDelta
{
get { return 0; }
}
protected SubroutineBuilder<Label> Builder { get; set; }
public override DepthFirst.Visitor<CFGBlock, Dummy> EdgeInfo
{
get { return this.edge_info; }
}
public ICodeProvider<Label> CodeProvider { get; private set; }
public override int BlockCount
{
get { return this.blocks.Length; }
}
public override IEnumerable<CFGBlock> Blocks
{
get { return this.blocks; }
}
public override string Name
{
get { return "SR" + Id; }
}
public override EdgeMap<EdgeTag> SuccessorEdges
{
get { return this.successor_edges; }
}
public override EdgeMap<EdgeTag> PredecessorEdges
{
get
{
if (this.predecessor_edges == null)
this.predecessor_edges = this.successor_edges.Reverse ();
return this.predecessor_edges;
}
}
#region Main Blocks
public override CFGBlock Entry
{
get { return this.entry; }
}
public override CFGBlock EntryAfterRequires
{
get
{
if (this.entry_after_requires != null)
return this.entry_after_requires;
return Entry;
}
}
public override CFGBlock Exit
{
get { return this.exit; }
}
public override CFGBlock ExceptionExit
{
get { return this.exception_exit; }
}
#endregion
#region IEdgeSubroutineAdaptor Members
public Sequence<Pair<EdgeTag, Subroutine>> GetOrdinaryEdgeSubroutinesInternal (CFGBlock from, CFGBlock to, Sequence<Edge<CFGBlock, EdgeTag>> context)
{
Sequence<Pair<EdgeTag, Subroutine>> list;
this.edge_subroutines.TryGetValue (new Pair<CFGBlock, CFGBlock> (from, to), out list);
if (list != null && context != null)
list = list.Where (FilterRecursiveContracts (to, context));
return list;
}
#endregion
#region IGraph<CFGBlock,Dummy> Members
public IEnumerable<CFGBlock> Nodes
{
get { return this.blocks; }
}
public virtual IEnumerable<Pair<Dummy, CFGBlock>> Successors (CFGBlock node)
{
foreach (var pair in this.successor_edges [node])
yield return new Pair<Dummy, CFGBlock> (Dummy.Value, pair.Value);
if (node != this.exception_exit)
yield return new Pair<Dummy, CFGBlock> (Dummy.Value, this.exception_exit);
}
#endregion
#region IStackInfo Members
bool IStackInfo.IsCallOnThis (APC apc)
{
return false;
}
#endregion
public override IEnumerable<CFGBlock> SuccessorBlocks (CFGBlock block)
{
return SuccessorEdges [block].Select (it => it.Value);
}
public override bool HasSingleSuccessor (APC point, out APC ifFound)
{
if (point.Index < point.Block.Count) {
ifFound = new APC (point.Block, point.Index + 1, point.SubroutineContext);
return true;
}
if (IsSubroutineEnd (point.Block)) {
if (point.SubroutineContext == null) {
ifFound = APC.Dummy;
return false;
}
ifFound = ComputeSubroutineContinuation (point);
return true;
}
BlockWithLabels<Label> onlyOne = null;
foreach (BlockWithLabels<Label> successor in point.Block.Subroutine.SuccessorBlocks (point.Block)) {
if (onlyOne == null)
onlyOne = successor;
else {
ifFound = APC.Dummy;
return false;
}
}
if (onlyOne != null) {
ifFound = ComputeTargetFinallyContext (point, onlyOne);
return true;
}
ifFound = APC.Dummy;
return false;
}
public override bool HasSinglePredecessor (APC point, out APC ifFound)
{
if (point.Index > 0) {
ifFound = new APC (point.Block, point.Index - 1, point.SubroutineContext);
return true;
}
if (IsSubroutineStart (point.Block)) {
if (point.SubroutineContext == null) {
ifFound = APC.Dummy;
return false;
}
bool hasSinglePredecessor;
ifFound = ComputeSubroutinePreContinuation (point, out hasSinglePredecessor);
return hasSinglePredecessor;
}
CFGBlock onlyOne = null;
foreach (CFGBlock predecessor in point.Block.Subroutine.PredecessorBlocks (point.Block)) {
if (onlyOne != null) {
ifFound = APC.Dummy;
return false;
}
onlyOne = predecessor;
}
if (onlyOne == null) {
ifFound = APC.Dummy;
return false;
}
Sequence<Pair<EdgeTag, Subroutine>> list = EdgeSubroutinesOuterToInner (onlyOne, point.Block, point.SubroutineContext);
if (list.IsEmpty ()) {
ifFound = APC.ForEnd (onlyOne, point.SubroutineContext);
return true;
}
var edge = new Edge<CFGBlock, EdgeTag> (onlyOne, point.Block, list.Head.Key);
Subroutine sub = list.Head.Value;
ifFound = APC.ForEnd (sub.Exit, point.SubroutineContext.Cons (edge));
return true;
}
private APC ComputeSubroutinePreContinuation (APC point, out bool hasSinglePredecessor)
{
Edge<CFGBlock, EdgeTag> head = point.SubroutineContext.Head;
bool isExceptionHandlerEdge;
Sequence<Edge<CFGBlock, EdgeTag>> tail = point.SubroutineContext.Tail;
Sequence<Pair<EdgeTag, Subroutine>> flist = EdgeSubroutinesOuterToInner (head.From, head.To, out isExceptionHandlerEdge, tail);
while (flist.Head.Value != this)
flist = flist.Tail;
if (flist.Tail.IsEmpty ()) {
if (isExceptionHandlerEdge && head.From.Count > 1) {
hasSinglePredecessor = false;
return APC.Dummy;
}
hasSinglePredecessor = true;
return APC.ForEnd (head.From, tail);
}
Pair<EdgeTag, Subroutine> first = flist.Tail.Head;
Subroutine sub = first.Value;
hasSinglePredecessor = true;
return APC.ForEnd (sub.Exit, point.SubroutineContext.Cons (new Edge<CFGBlock, EdgeTag> (head.From, head.To, first.Key)));
}
private APC ComputeSubroutineContinuation (APC point)
{
Edge<CFGBlock, EdgeTag> head = point.SubroutineContext.Head;
Sequence<Edge<CFGBlock, EdgeTag>> tail = point.SubroutineContext.Tail;
Sequence<Pair<EdgeTag, Subroutine>> outerToInner = EdgeSubroutinesOuterToInner (head.From, head.To, tail);
if (outerToInner.Head.Value == this)
return new APC (head.To, 0, tail);
while (outerToInner.Tail.Head.Value != this)
outerToInner = outerToInner.Tail;
return new APC (outerToInner.Head.Value.Entry, 0, tail.Cons (new Edge<CFGBlock, EdgeTag> (head.From, head.To, outerToInner.Head.Key)));
}
public override IEnumerable<CFGBlock> PredecessorBlocks (CFGBlock block)
{
return PredecessorEdges [block].Select (it => it.Value);
}
public override bool IsJoinPoint (CFGBlock block)
{
if (IsCatchFilterHeader (block) || IsSubroutineStart (block) || IsSubroutineEnd (block))
return true;
return PredecessorEdges [block].Count > 1;
}
public override bool IsSubroutineEnd (CFGBlock block)
{
return block == this.exit || block == this.exception_exit;
}
public override bool IsSubroutineStart (CFGBlock block)
{
return block == this.entry;
}
public override bool IsSplitPoint (CFGBlock block)
{
if (IsSubroutineStart (block) || IsSubroutineEnd (block))
return true;
return SuccessorEdges [block].Count > 1;
}
public override bool IsCatchFilterHeader (CFGBlock block)
{
return block is CatchFilterEntryBlock<Label>;
}
public void AddSuccessor (CFGBlock from, EdgeTag tag, CFGBlock to)
{
AddNormalControlFlowEdge (this.successors, from, tag, to);
}
private void AddNormalControlFlowEdge (List<Edge<CFGBlock, EdgeTag>> succs, CFGBlock from, EdgeTag tag, CFGBlock to)
{
succs.Add (new Edge<CFGBlock, EdgeTag> (from, to, tag));
}
public virtual void AddReturnBlock (BlockWithLabels<Label> block)
{
}
public BlockWithLabels<Label> GetTargetBlock (Label label)
{
return GetBlock (label);
}
public BlockWithLabels<Label> GetBlock (Label label)
{
IMetaDataProvider metadataDecoder = this.SubroutineFacade.MetaDataProvider;
BlockWithLabels<Label> block;
if (!this.LabelsThatStartBlocks.TryGetValue (label, out block)) {
Pair<Method, bool> methodVirtualPair;
Method constructor;
if (Builder == null)
throw new InvalidOperationException ("Builder must be not null");
if (Builder.IsMethodCallSite (label, out methodVirtualPair)) {
int parametersCount = metadataDecoder.Parameters (methodVirtualPair.Key).Count;
block = new MethodCallBlock<Label> (methodVirtualPair.Key, this, ref this.BlockIdGenerator, parametersCount, methodVirtualPair.Value);
} else if (Builder.IsNewObjSite (label, out constructor)) {
int parametersCount = metadataDecoder.Parameters (constructor).Count;
block = new NewObjCallBlock<Label> (constructor, parametersCount, this, ref this.BlockIdGenerator);
} else
block = NewBlock ();
if (Builder.IsTargetLabel (label))
this.LabelsThatStartBlocks.Add (label, block);
}
return block;
}
public virtual BlockWithLabels<Label> NewBlock ()
{
return new BlockWithLabels<Label> (this, ref this.BlockIdGenerator);
}
public AssumeBlock<Label> NewAssumeBlock (Label pc, EdgeTag tag)
{
return new AssumeBlock<Label> (this, pc, tag, ref this.BlockIdGenerator);
}
public override sealed void AddEdgeSubroutine (CFGBlock from, CFGBlock to, Subroutine subroutine, EdgeTag tag)
{
if (subroutine == null)
return;
var key = Pair.From (from, to);
Sequence<Pair<EdgeTag, Subroutine>> list;
this.edge_subroutines.TryGetValue (key, out list);
this.edge_subroutines[key] = list.Cons (Pair.From (tag, subroutine));
}
public override IEnumerable<APC> Successors (APC pc)
{
APC singleNext;
if (HasSingleSuccessor (pc, out singleNext))
yield return singleNext;
else {
foreach (CFGBlock block in pc.Block.Subroutine.SuccessorBlocks (pc.Block))
yield return pc.Block.Subroutine.ComputeTargetFinallyContext (pc, block);
}
}
public override IEnumerable<APC> Predecessors (APC pc)
{
if (pc.Index > 0)
yield return new APC (pc.Block, pc.Index - 1, pc.SubroutineContext);
else if (IsSubroutineStart (pc.Block)) {
if (!pc.SubroutineContext.IsEmpty ()) {
foreach (APC apc in ComputeSubroutinePreContinuation (pc))
yield return apc;
}
} else {
foreach (CFGBlock block in pc.Block.Subroutine.PredecessorBlocks (pc.Block)) {
Sequence<Pair<EdgeTag, Subroutine>> diffs = EdgeSubroutinesOuterToInner (block, pc.Block, pc.SubroutineContext);
if (diffs.IsEmpty ())
yield return APC.ForEnd (block, pc.SubroutineContext);
else {
Subroutine sub = diffs.Head.Value;
var edge = new Edge<CFGBlock, EdgeTag> (block, pc.Block, diffs.Head.Key);
yield return APC.ForEnd (sub.Exit, pc.SubroutineContext.Cons (edge));
}
}
}
}
private IEnumerable<APC> ComputeSubroutinePreContinuation (APC point)
{
Edge<CFGBlock, EdgeTag> edge = point.SubroutineContext.Head;
Sequence<Edge<CFGBlock, EdgeTag>> tail = point.SubroutineContext.Tail;
bool isHandlerEdge;
Sequence<Pair<EdgeTag, Subroutine>> diffs = EdgeSubroutinesOuterToInner (edge.From, edge.To, out isHandlerEdge, tail);
while (diffs.Head.Value != this)
diffs = diffs.Tail;
if (diffs.Tail == null) {
if (isHandlerEdge) {
for (int i = 0; i < edge.From.Count; i++)
yield return new APC (edge.From, i, tail);
} else
yield return APC.ForEnd (edge.From, tail);
} else {
Pair<EdgeTag, Subroutine> first = diffs.Tail.Head;
Subroutine nextSubroutine = first.Value;
yield return APC.ForEnd (nextSubroutine.Exit, point.SubroutineContext.Cons (new Edge<CFGBlock, EdgeTag> (edge.From, edge.To, first.Key)));
}
}
public override APC ComputeTargetFinallyContext (APC pc, CFGBlock succ)
{
Sequence<Pair<EdgeTag, Subroutine>> list = EdgeSubroutinesOuterToInner (pc.Block, succ, pc.SubroutineContext);
if (list.IsEmpty ())
return new APC (succ, 0, pc.SubroutineContext);
Pair<EdgeTag, Subroutine> last = list.Last ();
return new APC (last.Value.Entry, 0, pc.SubroutineContext.Cons (new Edge<CFGBlock, EdgeTag> (pc.Block, succ, last.Key)));
}
private Sequence<Pair<EdgeTag, Subroutine>> EdgeSubroutinesOuterToInner (CFGBlock from, CFGBlock succ, Sequence<Edge<CFGBlock, EdgeTag>> subroutineContext)
{
bool isExceptionHandlerEdge;
return EdgeSubroutinesOuterToInner (from, succ, out isExceptionHandlerEdge, subroutineContext);
}
public override Sequence<Pair<EdgeTag, Subroutine>> EdgeSubroutinesOuterToInner (CFGBlock from, CFGBlock succ, out bool isExceptionHandlerEdge, Sequence<Edge<CFGBlock, EdgeTag>> context)
{
if (from.Subroutine != this)
return from.Subroutine.EdgeSubroutinesOuterToInner (from, succ, out isExceptionHandlerEdge, context);
isExceptionHandlerEdge = IsCatchFilterHeader (succ);
return GetOrdinaryEdgeSubroutines (from, succ, context);
}
public override Sequence<Pair<EdgeTag, Subroutine>> GetOrdinaryEdgeSubroutines (CFGBlock from, CFGBlock to, Sequence<Edge<CFGBlock, EdgeTag>> context)
{
IMetaDataProvider metadataDecoder = this.SubroutineFacade.MetaDataProvider;
var apc = new APC (to, 0, context);
DecoratorHelper.Push (this);
try {
Sequence<Pair<EdgeTag, Subroutine>> list = DecoratorHelper.Dispatch<IEdgeSubroutineAdaptor> (this).GetOrdinaryEdgeSubroutinesInternal (from, to, context);
if (apc.InsideContract) {
if (context != null && !list.IsEmpty ()) {
Method calledMethod;
bool isNewObj;
bool isVirtual;
if (@from.IsMethodCallBlock (out calledMethod, out isNewObj, out isVirtual) && isVirtual && ((IStackInfo) this).IsCallOnThis (new APC (@from, 0, null))) {
TypeNode type = metadataDecoder.DeclaringType (calledMethod);
do {
if (context.Head.Tag.Is (EdgeTag.InheritedMask) || context.Head.Tag.Is (EdgeTag.ExtraMask) || context.Head.Tag.Is (EdgeTag.OldMask))
context = context.Tail;
else {
Method calledMethod2;
bool isNewObj2;
bool isVirtual2;
if (context.Head.Tag.Is (EdgeTag.AfterMask) && context.Head.From.IsMethodCallBlock (out calledMethod2, out isNewObj2, out isVirtual2)) {
TypeNode sub = metadataDecoder.DeclaringType (calledMethod2);
if (metadataDecoder.DerivesFrom (sub, type))
type = sub;
if (!DecoratorHelper.Dispatch<IStackInfo> (this).IsCallOnThis (new APC (context.Head.From, 0, null)))
break;
} else if (context.Head.Tag.Is (EdgeTag.BeforeMask) && context.Head.To.IsMethodCallBlock (out calledMethod2, out isNewObj2, out isVirtual2)) {
TypeNode sub = metadataDecoder.DeclaringType (calledMethod2);
if (metadataDecoder.DerivesFrom (sub, type))
type = sub;
if (!DecoratorHelper.Dispatch<IStackInfo> (this).IsCallOnThis (new APC (context.Head.To, 0, null)))
break;
} else if (context.Head.Tag == EdgeTag.Exit) {
var methodInfo = context.Head.From.Subroutine as IMethodInfo;
if (methodInfo != null) {
TypeNode sub = metadataDecoder.DeclaringType (methodInfo.Method);
if (metadataDecoder.DerivesFrom (sub, type))
type = sub;
}
break;
} else {
if (context.Head.Tag != EdgeTag.Entry)
return list;
var methodInfo = context.Head.From.Subroutine as IMethodInfo;
if (methodInfo != null) {
TypeNode sub = metadataDecoder.DeclaringType (methodInfo.Method);
if (metadataDecoder.DerivesFrom (sub, type))
type = sub;
}
break;
}
context = context.Tail;
}
} while (!context.IsEmpty ());
Method implementingMethod;
if (!metadataDecoder.Equal (type, metadataDecoder.DeclaringType (calledMethod)) &&
metadataDecoder.TryGetImplementingMethod (type, calledMethod, out implementingMethod))
list = SpecializedEnsures (list, this.SubroutineFacade.GetEnsures (calledMethod), this.SubroutineFacade.GetEnsures (implementingMethod));
}
}
} else {
Method calledMethod;
bool isNewObj;
bool isVirtual;
if (@from.IsMethodCallBlock (out calledMethod, out isNewObj, out isVirtual)) {
if (DecoratorHelper.Dispatch<IStackInfo> (this).IsCallOnThis (new APC (@from, 0, null))) {
var methodInfo = @from.Subroutine as IMethodInfo;
if (methodInfo != null) {
TypeNode bestType = metadataDecoder.DeclaringType (methodInfo.Method);
Method implementingMethod;
if (isVirtual && metadataDecoder.TryGetImplementingMethod (bestType, calledMethod, out implementingMethod))
list = SpecializedEnsures (list, this.SubroutineFacade.GetEnsures (calledMethod), this.SubroutineFacade.GetEnsures (implementingMethod));
list = InsertInvariant (@from, list, calledMethod, ref bestType, context);
}
}
}
}
return list;
} finally {
DecoratorHelper.Pop ();
}
}
private Sequence<Pair<EdgeTag, Subroutine>> InsertInvariant (CFGBlock from, Sequence<Pair<EdgeTag, Subroutine>> list,
Method calledMethod, ref TypeNode type,
Sequence<Edge<CFGBlock, EdgeTag>> context)
{
IMetaDataProvider metadataDecoder = this.SubroutineFacade.MetaDataProvider;
Property property;
if (metadataDecoder.IsPropertySetter (calledMethod, out property)
&& (metadataDecoder.IsAutoPropertyMember (calledMethod) || WithinConstructor (from, context)))
return list;
if (metadataDecoder.IsConstructor (calledMethod))
type = metadataDecoder.DeclaringType (calledMethod);
Subroutine invariant = this.SubroutineFacade.GetInvariant (type);
if (invariant != null) {
var methodCallBlock = from as MethodCallBlock<Label>;
if (methodCallBlock != null) {
EdgeTag first = methodCallBlock.IsNewObj ? EdgeTag.AfterNewObj : EdgeTag.AfterCall;
return list.Cons (new Pair<EdgeTag, Subroutine> (first, invariant));
}
}
return list;
}
private bool WithinConstructor (CFGBlock current, Sequence<Edge<CFGBlock, EdgeTag>> context)
{
return new APC (current, 0, context).InsideConstructor;
}
private Sequence<Pair<EdgeTag, Subroutine>> SpecializedEnsures (Sequence<Pair<EdgeTag, Subroutine>> subroutines,
Subroutine toReplace, Subroutine specializedEnsures)
{
return subroutines.Select (pair => new Pair<EdgeTag, Subroutine> (pair.Key, pair.Value == toReplace ? specializedEnsures : pair.Value));
}
private static Predicate<Pair<EdgeTag, Subroutine>> FilterRecursiveContracts (CFGBlock from, Sequence<Edge<CFGBlock, EdgeTag>> context)
{
return (candidate) => {
Subroutine sub = candidate.Value;
if (!sub.IsContract)
return true;
if (sub == @from.Subroutine)
return false;
if (context.Any (ctx => sub == ctx.From.Subroutine))
return false;
return true;
};
}
public abstract override void Initialize ();
public virtual void Commit ()
{
PostProcessBlocks ();
}
protected void PostProcessBlocks ()
{
var blockStack = new Stack<CFGBlock> ();
this.successor_edges = new EdgeMap<EdgeTag> (this.successors);
this.edge_info = new DepthFirst.Visitor<CFGBlock, Dummy> (this, null, (block) => blockStack.Push (block), null);
this.edge_info.VisitSubGraphNonRecursive (this.exception_exit);
this.edge_info.VisitSubGraphNonRecursive (this.exit);
this.edge_info.VisitSubGraphNonRecursive (this.entry);
foreach (var successorEdge in this.successor_edges) {
int idGen = UnusedBlockIndex;
successorEdge.From.Renumber (ref idGen);
}
int idGen1 = 0;
foreach (CFGBlock cfgBlock in blockStack)
cfgBlock.Renumber (ref idGen1);
SuccessorEdges.Filter ((e) => e.From.Index != UnusedBlockIndex);
this.predecessor_edges = this.successor_edges.Reverse ();
int finishTime = 0;
var visitor = new DepthFirst.Visitor<CFGBlock, EdgeTag> (this.predecessor_edges, null, block => block.ReversePostOrderIndex = finishTime++, null);
visitor.VisitSubGraphNonRecursive (this.exit);
foreach (CFGBlock node in blockStack)
visitor.VisitSubGraphNonRecursive (node);
SuccessorEdges.Resort ();
this.blocks = blockStack.ToArray ();
this.LabelsThatStartBlocks = null;
Builder = null;
}
public override IEnumerable<Subroutine> UsedSubroutines (HashSet<int> alreadySeen)
{
foreach (var list in this.edge_subroutines.Values) {
foreach (var pair in list.AsEnumerable ()) {
Subroutine sub = pair.Value;
if (!alreadySeen.Contains (sub.Id)) {
alreadySeen.Add (sub.Id);
yield return sub;
}
}
}
}
public override IEnumerable<CFGBlock> ExceptionHandlers<Data, TType> (CFGBlock block, Subroutine innerSubroutine,
Data data, IHandlerFilter<Data> handlerPredicate)
{
yield return this.exception_exit;
}
public override void Print (TextWriter tw, ILPrinter<APC> printer, Func<CFGBlock,
IEnumerable<Sequence<Edge<CFGBlock, EdgeTag>>>> contextLookup,
Sequence<Edge<CFGBlock, EdgeTag>> context,
HashSet<Pair<Subroutine, Sequence<Edge<CFGBlock, EdgeTag>>>> printed)
{
var element = new Pair<Subroutine, Sequence<Edge<CFGBlock, EdgeTag>>> (this, context);
if (printed.Contains (element))
return;
printed.Add (element);
var subs = new HashSet<Subroutine> ();
var methodInfo = this as IMethodInfo;
string method = (methodInfo != null) ? String.Format ("({0})", this.SubroutineFacade.MetaDataProvider.FullName (methodInfo.Method)) : null;
tw.WriteLine ("Subroutine SR{0} {1} {2}", Id, Kind, method);
tw.WriteLine ("-------------");
foreach (BlockWithLabels<Label> block in this.blocks) {
tw.Write ("Block {0} ({1})", block.Index, block.ReversePostOrderIndex);
if (this.edge_info.DepthFirstInfo (block).TargetOfBackEdge)
tw.WriteLine (" (target of backedge)");
else if (IsJoinPoint (block))
tw.WriteLine (" (join point)");
else
tw.WriteLine ();
tw.Write (" Predecessors: ");
foreach (var edge in block.Subroutine.PredecessorEdges [block])
tw.Write ("({0}, {1}) ", edge.Key, edge.Value.Index);
tw.WriteLine ();
PrintHandlers (tw, block);
tw.WriteLine (" Code:");
foreach (APC apc in block.APCs ())
printer (apc, " ", tw);
tw.Write (" Successors: ");
foreach (var edge in block.Subroutine.SuccessorEdges [block]) {
tw.Write ("({0}, {1}", edge.Key, edge.Value.Index);
if (this.edge_info.IsBackEdge (block, Dummy.Value, edge.Value))
tw.Write (" BE");
for (Sequence<Pair<EdgeTag, Subroutine>> list = GetOrdinaryEdgeSubroutines (block, edge.Value, context); list != null; list = list.Tail) {
subs.Add (list.Head.Value);
tw.Write (" SR{0}({1})", list.Head.Value.Id, list.Head.Key);
}
tw.Write (") ");
}
tw.WriteLine ();
}
PrintReferencedSubroutines (tw, subs, printer, contextLookup, context, printed);
}
protected virtual void PrintReferencedSubroutines (TextWriter tw, HashSet<Subroutine> subs, ILPrinter<APC> printer,
Func<CFGBlock, IEnumerable<Sequence<Edge<CFGBlock, EdgeTag>>>> contextLookup,
Sequence<Edge<CFGBlock, EdgeTag>> context,
HashSet<Pair<Subroutine, Sequence<Edge<CFGBlock, EdgeTag>>>> printed)
{
foreach (Subroutine subroutine in subs) {
if (contextLookup == null)
subroutine.Print (tw, printer, contextLookup, context, printed);
else {
foreach (var ctx in contextLookup (subroutine.Entry))
subroutine.Print (tw, printer, contextLookup, ctx, printed);
}
}
}
protected virtual void PrintHandlers (TextWriter tw, BlockWithLabels<Label> block)
{
tw.Write (" Handlers: ");
if (block != ExceptionExit)
tw.Write ("{0} ", ExceptionExit.Index);
tw.WriteLine ();
}
public int GetILOffset (Label label)
{
return CodeProvider.GetILOffset (label);
}
}
}
|