summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/backend/icode/ICodes.scala
blob: 10f0c6ee00b21c3481375a4d32b253c8fb0e055b (plain) (blame)
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
/* NSC -- new scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author  Martin Odersky
 */

package scala.tools.nsc
package backend
package icode

import java.io.PrintWriter
import analysis.{ Liveness, ReachingDefinitions }
import scala.tools.nsc.symtab.classfile.ICodeReader
import scala.reflect.io.AbstractFile

/** Glue together ICode parts.
 *
 *  @author Iulian Dragos
 */
abstract class ICodes extends AnyRef
                                 with Members
                                 with BasicBlocks
                                 with Opcodes
                                 with TypeStacks
                                 with TypeKinds
                                 with ExceptionHandlers
                                 with Primitives
                                 with Linearizers
                                 with Printers
                                 with Repository
{
  val global: Global
  import global.{ log, definitions, settings, perRunCaches, devWarning }

  /** The ICode representation of classes */
  val classes = perRunCaches.newMap[global.Symbol, IClass]()

  /** Debugging flag */
  def shouldCheckIcode = settings.check contains global.genicode.phaseName
  def checkerDebug(msg: String) = if (shouldCheckIcode && global.settings.debug) println(msg)

  /** The ICode linearizer. */
  val linearizer: Linearizer = settings.Xlinearizer.value match {
    case "rpo"    => new ReversePostOrderLinearizer()
    case "dfs"    => new DepthFirstLinerizer()
    case "normal" => new NormalLinearizer()
    case "dump"   => new DumpLinearizer()
    case x        => global.abort("Unknown linearizer: " + x)
  }

  def newTextPrinter() =
    new TextPrinter(new PrintWriter(Console.out, true), new DumpLinearizer)

  /** Have to be careful because dump calls around, possibly
   *  re-entering methods which initiated the dump (like foreach
   *  in BasicBlocks) which leads to the icode output olympics.
   */
  private var alreadyDumping = false

  /** Print all classes and basic blocks. Used for debugging. */

  def dumpClassesAndAbort(msg: String): Nothing = {
    if (alreadyDumping) global.abort(msg)
    else alreadyDumping = true

    Console.println(msg)
    val printer = newTextPrinter()
    classes.values foreach printer.printClass
    global.abort(msg)
  }

  def dumpMethodAndAbort(m: IMethod, msg: String): Nothing = {
    Console.println("Fatal bug in inlinerwhile traversing " + m + ": " + msg)
    m.dump()
    global.abort("" + m)
  }
  def dumpMethodAndAbort(m: IMethod, b: BasicBlock): Nothing =
    dumpMethodAndAbort(m, "found open block " + b + " " + b.flagsString)

  def checkValid(m: IMethod) {
    // always slightly dicey to iterate over mutable structures
    m foreachBlock { b =>
      if (!b.closed) {
        // Something is leaving open/empty blocks around (see SI-4840) so
        // let's not kill the deal unless it's nonempty.
        if (b.isEmpty) {
          devWarning(s"Found open but empty block while inlining $m: removing from block list.")
          m.code removeBlock b
        }
        else dumpMethodAndAbort(m, b)
      }
    }
  }

  object liveness extends Liveness {
    val global: ICodes.this.global.type = ICodes.this.global
  }

  object reachingDefinitions extends ReachingDefinitions {
    val global: ICodes.this.global.type = ICodes.this.global
  }

  lazy val AnyRefReference: TypeKind    = REFERENCE(definitions.AnyRefClass)
  lazy val BoxedUnitReference: TypeKind = REFERENCE(definitions.BoxedUnitClass)
  lazy val NothingReference: TypeKind   = REFERENCE(definitions.NothingClass)
  lazy val NullReference: TypeKind      = REFERENCE(definitions.NullClass)
  lazy val ObjectReference: TypeKind    = REFERENCE(definitions.ObjectClass)
  lazy val StringReference: TypeKind    = REFERENCE(definitions.StringClass)

  object icodeReader extends ICodeReader {
    lazy val global: ICodes.this.global.type = ICodes.this.global
    import global._
    def lookupMemberAtTyperPhaseIfPossible(sym: Symbol, name: Name): Symbol =
      global.loaders.lookupMemberAtTyperPhaseIfPossible(sym, name)
    lazy val symbolTable: global.type = global
    lazy val loaders: global.loaders.type = global.loaders

    def classFileLookup: util.ClassFileLookup[AbstractFile] = global.classPath
  }

  /** A phase which works on icode. */
  abstract class ICodePhase(prev: Phase) extends global.GlobalPhase(prev) {
    override def erasedTypes = true
    override def apply(unit: global.CompilationUnit): Unit =
      unit.icode foreach apply

    def apply(cls: global.icodes.IClass): Unit
  }
}