summaryrefslogtreecommitdiff
path: root/BCT/BytecodeTranslator/Phone/PhoneMethodInliningTraverser.cs
blob: 97935721dad4a93d691c168361772fffbfbd2461 (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Cci;

namespace BytecodeTranslator.Phone {
  public class PhoneMethodInliningMetadataTraverser : BaseMetadataTraverser {
    private HashSet<IMethodDefinition> methodsToInline;
    private HashSet<IMethodDefinition> iterMethodsToInline;
    private PhoneCodeHelper phoneHelper;
    private bool firstPassDone = false;
    private bool changedOnLastPass = false;
    private IAssemblyReference assemblyBeingTranslated;

    public int TotalMethodsCount { get; private set; }
    public int InlinedMethodsCount { get { return methodsToInline.Count(); } }

    public PhoneMethodInliningMetadataTraverser(PhoneCodeHelper phoneHelper) {
      methodsToInline = new HashSet<IMethodDefinition>();
      iterMethodsToInline = new HashSet<IMethodDefinition>();
      this.phoneHelper = phoneHelper;
      TotalMethodsCount = 0;
    }

    public override void Visit(IEnumerable<IModule> modules) {
      foreach (IModule module in modules) {
        assemblyBeingTranslated= module.ContainingAssembly;
        this.Visit(module);
      }
      firstPassDone = true;
    }

    public override void Visit(IMethodDefinition method) {
      if (!firstPassDone)
        TotalMethodsCount++;

      if (iterMethodsToInline.Contains(method) || (!firstPassDone && phoneHelper.mustInlineMethod(method))) {
        PhoneMethodInliningCodeTraverser codeTraverser= new PhoneMethodInliningCodeTraverser();
        codeTraverser.Visit(method);
        foreach (IMethodDefinition newMethodDef in codeTraverser.getMethodsFound()) {
          bool isExtern = this.assemblyBeingTranslated != null &&
                          !TypeHelper.GetDefiningUnitReference(newMethodDef.ContainingType).UnitIdentity.Equals(this.assemblyBeingTranslated.UnitIdentity);
          if (!methodsToInline.Contains(newMethodDef) && !isExtern) {
            iterMethodsToInline.Add(newMethodDef);
            changedOnLastPass = true;
          }
        }
        methodsToInline.Add(method);
        iterMethodsToInline.Remove(method);
      }
    }

    public IEnumerable<IMethodDefinition> getMethodsToInline() {
      return methodsToInline;
    }

    public bool isFinished() {
      return firstPassDone && !changedOnLastPass;
    }

    public void findAllMethodsToInline(List<IModule> modules) {
      while (!isFinished()) {
        changedOnLastPass = false;
        Visit(modules);
      }
    }
  }

  class PhoneMethodInliningCodeTraverser : BaseCodeTraverser {
    private HashSet<IMethodDefinition> foundMethods = new HashSet<IMethodDefinition>();

    public override void Visit(IMethodCall methodCall) {
      foundMethods.Add(methodCall.MethodToCall.ResolvedMethod);
    }

    public IEnumerable<IMethodDefinition> getMethodsFound() {
      return foundMethods;
    }
  }
}