package scala.tools.nsc
package backend
package icode
import scala.tools.nsc.ast._
import scala.collection.{ mutable, immutable }
import mutable.ListBuffer
trait Linearizers {
self: ICodes =>
import opcodes._
abstract class Linearizer {
def linearize(c: IMethod): List[BasicBlock]
def linearizeAt(c: IMethod, start: BasicBlock): List[BasicBlock]
}
class NormalLinearizer extends Linearizer with WorklistAlgorithm {
type Elem = BasicBlock
val worklist: WList = new mutable.Stack()
var blocks: List[BasicBlock] = Nil
def linearize(m: IMethod): List[BasicBlock] = {
val b = m.code.startBlock;
blocks = Nil;
run {
worklist pushAll (m.exh map (_.startBlock));
worklist.push(b);
}
blocks.reverse;
}
def linearizeAt(m: IMethod, start: BasicBlock): List[BasicBlock] = {
blocks = Nil
worklist.clear()
linearize(start)
}
def linearize(startBlock: BasicBlock): List[BasicBlock] = {
run( { worklist.push(startBlock); } );
blocks.reverse;
}
def processElement(b: BasicBlock) =
if (b.nonEmpty) {
add(b);
b.lastInstruction match {
case JUMP(whereto) =>
add(whereto);
case CJUMP(success, failure, _, _) =>
add(success);
add(failure);
case CZJUMP(success, failure, _, _) =>
add(success);
add(failure);
case SWITCH(_, labels) =>
add(labels);
case RETURN(_) => ();
case THROW(clasz) => ();
}
}
def dequeue: Elem = worklist.pop;
def add(b: BasicBlock) {
if (blocks.contains(b))
()
else {
blocks = b :: blocks;
worklist push b;
}
}
def add(bs: List[BasicBlock]): Unit = bs foreach add;
}
class DepthFirstLinerizer extends Linearizer {
var blocks: List[BasicBlock] = Nil;
def linearize(m: IMethod): List[BasicBlock] = {
blocks = Nil;
dfs(m.code.startBlock);
m.exh foreach (b => dfs(b.startBlock));
blocks.reverse
}
def linearizeAt(m: IMethod, start: BasicBlock): List[BasicBlock] = {
blocks = Nil
dfs(start)
blocks.reverse
}
def dfs(b: BasicBlock): Unit =
if (b.nonEmpty && add(b))
b.successors foreach dfs;
def add(b: BasicBlock): Boolean =
!(blocks contains b) && {
blocks = b :: blocks;
true
}
}
class ReversePostOrderLinearizer extends Linearizer {
var blocks: List[BasicBlock] = Nil
val visited = new mutable.HashSet[BasicBlock]
val added = new mutable.BitSet
def linearize(m: IMethod): List[BasicBlock] = {
blocks = Nil;
visited.clear()
added.clear;
m.exh foreach (b => rpo(b.startBlock));
rpo(m.code.startBlock);
if (m.code.startBlock.predecessors eq Nil)
blocks
else
m.code.startBlock :: (blocks.filterNot(_ == m.code.startBlock))
}
def linearizeAt(m: IMethod, start: BasicBlock): List[BasicBlock] = {
blocks = Nil
visited.clear()
added.clear()
rpo(start)
blocks
}
def rpo(b: BasicBlock): Unit =
if (b.nonEmpty && !visited(b)) {
visited += b;
b.successors foreach rpo
add(b)
}
def add(b: BasicBlock) =
if (!added(b.label)) {
added += b.label
blocks = b :: blocks;
}
}
class DumpLinearizer extends Linearizer {
def linearize(m: IMethod): List[BasicBlock] = m.code.blocks.toList
def linearizeAt(m: IMethod, start: BasicBlock): List[BasicBlock] = sys.error("not implemented")
}
class MSILLinearizer extends Linearizer {
val normalLinearizer = new NormalLinearizer()
def linearize(m: IMethod): List[BasicBlock] = {
val handlersByCovered = m.exh.groupBy(_.covered)
def size(covered: collection.immutable.Set[BasicBlock]) = {
val hs = handlersByCovered(covered)
covered.size + (hs :\ 0)((h, s) => h.blocks.length + s)
}
val tryBlocks = handlersByCovered.keys.toList sortBy size
var result = normalLinearizer.linearize(m)
val frozen = mutable.HashSet[BasicBlock](result.head)
for (tryBlock <- tryBlocks) {
result = groupBlocks(m, result, handlersByCovered(tryBlock), frozen)
}
result
}
def groupBlocks(method: IMethod, blocks: List[BasicBlock], handlers: List[ExceptionHandler], frozen: mutable.HashSet[BasicBlock]) = {
assert(blocks.head == method.code.startBlock, method)
val beforeAndTry = new ListBuffer[BasicBlock]()
val catches = handlers map (_ => new ListBuffer[BasicBlock]())
val after = new ListBuffer[BasicBlock]()
var beforeTry = true
val head = handlers.head
for (b <- blocks) {
if (head covers b) {
beforeTry = false
beforeAndTry += b
} else {
val handlerIndex = handlers.indexWhere(_.blocks.contains(b))
if (handlerIndex >= 0) {
catches(handlerIndex) += b
} else if (beforeTry) {
beforeAndTry += b
} else {
after += b
}
}
}
(catches, handlers).zipped foreach { (lb, handler) =>
lb -= handler.startBlock
handler.startBlock +=: lb
}
var firstAfter: Option[BasicBlock] = None
var blks = head.covered.toList :: handlers.map(_.blocks)
while (firstAfter.isEmpty && !blks.isEmpty) {
val b = blks.head
blks = blks.tail
val leaving = leavingBlocks(b)
if (!leaving.isEmpty) {
assert(leaving.size <= 1, leaving)
firstAfter = Some(leaving.head)
}
}
if (firstAfter.isDefined) {
val b = firstAfter.get
if (frozen(b)) {
assert(after contains b, b +", "+ method)
} else {
frozen += b
if (beforeAndTry contains b) {
beforeAndTry -= b
} else {
assert(after contains b, after)
after -= b
}
b +=: after
}
}
for (lb <- catches) { beforeAndTry ++= lb }
beforeAndTry ++= after
beforeAndTry.toList
}
private def leavingBlocks(blocks: List[BasicBlock]) = {
val res = new mutable.HashSet[BasicBlock]()
for (b <- blocks; s <- b.directSuccessors; if (!blocks.contains(s)))
res += s
res
}
def linearizeAt(m: IMethod, start: BasicBlock): List[BasicBlock] = {
sys.error("not implemented")
}
}
}