@Test
  public void cytron() throws IOException {
    IRBody block = parseCytron();
    System.out.println(block);

    ControlFlowGraph cfg = new ControlFlowGraph(block);
    List<BasicBlock> bb = cfg.getLiveBasicBlocks();

    System.out.println(cfg);
    cfg.dumpGraph();

    // see Figure 5 in
    // http://www.cs.utexas.edu/~pingali/CS380C/2010/papers/ssaCytron.pdf

    assertThat(cfg.getSuccessors(bb.get(0)), itemsEqualTo(bb.get(1), cfg.getExit()));
    assertThat(cfg.getSuccessors(bb.get(1)), itemsEqualTo(bb.get(2)));
    assertThat(cfg.getSuccessors(bb.get(2)), itemsEqualTo(bb.get(3), bb.get(7)));
    assertThat(cfg.getSuccessors(bb.get(3)), itemsEqualTo(bb.get(4), bb.get(5)));
    assertThat(cfg.getSuccessors(bb.get(4)), itemsEqualTo(bb.get(6)));
    assertThat(cfg.getSuccessors(bb.get(5)), itemsEqualTo(bb.get(6)));
    assertThat(cfg.getSuccessors(bb.get(6)), itemsEqualTo(bb.get(8)));
    assertThat(cfg.getSuccessors(bb.get(7)), itemsEqualTo(bb.get(8)));
    assertThat(cfg.getSuccessors(bb.get(8)), itemsEqualTo(bb.get(9)));
    assertThat(cfg.getSuccessors(bb.get(9)), itemsEqualTo(bb.get(10), bb.get(11)));
    assertThat(cfg.getSuccessors(bb.get(10)), itemsEqualTo(bb.get(11)));
    assertThat(cfg.getSuccessors(bb.get(11)), itemsEqualTo(bb.get(9), bb.get(12)));
    assertThat(cfg.getSuccessors(bb.get(12)), itemsEqualTo(bb.get(13), bb.get(2)));
  }
Esempio n. 2
0
  public void writeInline(EmitContext emitContext, InstructionAdapter mv) {

    // Last check for assumption violations
    types.verifyFunctionAssumptions(runtimeState);

    Label exitLabel = new Label();

    for (BasicBlock basicBlock : cfg.getBasicBlocks()) {
      if (basicBlock != cfg.getEntry() && basicBlock != cfg.getExit()) {
        for (IRLabel label : basicBlock.getLabels()) {
          mv.visitLabel(emitContext.getAsmLabel(label));
        }
        for (Statement stmt : basicBlock.getStatements()) {
          try {
            if (stmt instanceof ReturnStatement) {
              // Instead of returning, just push the return value on the stack
              // and jump to the exit point for the function.
              stmt.getRHS().load(emitContext, mv);
              mv.goTo(exitLabel);

            } else {
              stmt.emit(emitContext, mv);
            }
          } catch (NotCompilableException e) {
            throw e;
          } catch (Exception e) {
            throw new InternalCompilerException("Exception compiling statement " + stmt, e);
          }
        }
      }
    }
    mv.mark(exitLabel);
  }