summaryrefslogtreecommitdiff
path: root/BCT/BytecodeTranslator/Phone/PhoneMethodInliningTraverser.cs
blob: 86d696ec1e938ef0ee61ded2ce653472edfa1adb (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
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;

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

    public override void Visit(IEnumerable<IModule> modules) {
      base.Visit(modules);
      firstPassDone = true;
    }

    public override void Visit(IMethodDefinition method) {
      if (iterMethodsToInline.Contains(method) || (!firstPassDone && phoneHelper.mustInlineMethod(method))) {
        PhoneMethodInliningCodeTraverser codeTraverser= new PhoneMethodInliningCodeTraverser();
        codeTraverser.Visit(method);
        foreach (IMethodDefinition newMethodDef in codeTraverser.getMethodsFound()) {
          if (!methodsToInline.Contains(newMethodDef))
            iterMethodsToInline.Add(newMethodDef);
        }
        methodsToInline.Add(method);
        iterMethodsToInline.Remove(method);
      }
    }

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

    public bool isFinished() {
      return !iterMethodsToInline.Any();
    }
  }

  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;
    }
  }
}