jist.runtime
Class RewriterTraversalContinuableMethods

java.lang.Object
  extended by jist.runtime.ClassTraversal.Empty
      extended by jist.runtime.RewriterTraversalContinuableMethods
All Implemented Interfaces:
ClassTraversal.Visitor

 class RewriterTraversalContinuableMethods
extends ClassTraversal.Empty

Traversal object that modifies continuable methods. Continuable needs to be able to pause and resume their execution at various points: at any blocking entity call, or continuable regular method call. Pausing execution means saving both the frame and the program location into a custom state object. Resuming execution means reversing the process.

 Static analysis:
   - data flow: to determine what the frame (stack and locals) looks like
       at each blocking or continuable execution point
   - state class: create custom state class (subclass of Event.ContinuationFrame) 
     with fields for locals and stack slots at each execution point

 Pausing execution:
  - save frame (locals and stack) to state object before execution and store
    in additional local variable
  - execute method invocation
  - check if we are in save mode
    - if yes, then push frame information using Controller and return 
      immediately (with some dummy return value, if necessary)

 Restoring execution:
   - (at method start) check if we are invoking this method in restore mode
   - if yes, pop frame information from Controller
   - switch on program counter
   - restore frame (locals and stack) slots
   - jump to pause point
 

Since:
JIST1.0
Author:
Rimon Barr <barr+jist@cs.cornell.edu>
See Also:
JistAPI

Field Summary
private  org.apache.bcel.generic.ConstantPoolGen cpg
          class constant pool.
private  RewriterFlow flow
          data flow analysis object.
private  RewriterFlow.FlowInfoMap flowinfo
          data flow analysis result.
private  java.util.Vector frames
          save/restore frames.
private  org.apache.bcel.generic.InstructionFactory ifc
          class instruction factory.
private  java.util.Vector ihs
          save/restore instruction handles.
private  int pc
          program location counter.
private  Rewriter rewriter
          primary rewriter.
 
Constructor Summary
RewriterTraversalContinuableMethods(Rewriter rewriter)
          Create rewriter object that will transform continuable methods to CPS.
 
Method Summary
private static org.apache.bcel.classfile.JavaClass createStateClass(org.apache.bcel.generic.MethodGen mg, int pc, org.apache.bcel.verifier.structurals.Frame f)
          Generate state class for a specific execution point within a method.
 org.apache.bcel.generic.ClassGen doClass(org.apache.bcel.generic.ClassGen cg)
          Upcall to process class before field and method processing.
 void doInstruction(org.apache.bcel.generic.ClassGen cg, org.apache.bcel.generic.MethodGen mg, org.apache.bcel.generic.InstructionHandle ih, org.apache.bcel.generic.Instruction inst)
          Upcall to process instruction. Note that instructions can be inserted and deleted directly using the BCEL API, and therefore this method returns void.
 org.apache.bcel.generic.MethodGen doMethod(org.apache.bcel.generic.ClassGen cg, org.apache.bcel.generic.MethodGen mg)
          Upcall to process method before instruction processing.
 org.apache.bcel.generic.MethodGen doMethodPost(org.apache.bcel.generic.ClassGen cg, org.apache.bcel.generic.MethodGen mg)
          Upcall to process method after instruction processing.
private static org.apache.bcel.verifier.structurals.Frame fixFrame(org.apache.bcel.verifier.structurals.Frame f)
          Return BCEL frame with fewer object types (only relevant ones) in order to simplify frame save/restore code.
private static org.apache.bcel.generic.Type fixType(org.apache.bcel.generic.Type t)
          Helper method for fixFrame to process individual types within Frame object.
static boolean isNotInit(org.apache.bcel.generic.Type t)
          Returns whether Type is Uninitialized.
private static void restoreFrame(org.apache.bcel.generic.MethodGen mg, java.util.Vector frames, java.util.Vector ihs, org.apache.bcel.generic.InstructionFactory ifc)
          Insert instructions to restore an execution frame from a state object.
private static void saveFrame(org.apache.bcel.generic.MethodGen mg, org.apache.bcel.generic.InstructionHandle ih, int pc, org.apache.bcel.verifier.structurals.Frame f, org.apache.bcel.generic.InstructionFactory ifc)
          Insert instructions to save an execution frame to state object.
 
Methods inherited from class jist.runtime.ClassTraversal.Empty
doClassPost, doField
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rewriter

private Rewriter rewriter
primary rewriter.


flow

private RewriterFlow flow
data flow analysis object.


flowinfo

private RewriterFlow.FlowInfoMap flowinfo
data flow analysis result.


cpg

private org.apache.bcel.generic.ConstantPoolGen cpg
class constant pool.


ifc

private org.apache.bcel.generic.InstructionFactory ifc
class instruction factory.


pc

private int pc
program location counter.


frames

private java.util.Vector frames
save/restore frames.


ihs

private java.util.Vector ihs
save/restore instruction handles.

Constructor Detail

RewriterTraversalContinuableMethods

public RewriterTraversalContinuableMethods(Rewriter rewriter)
Create rewriter object that will transform continuable methods to CPS.

Parameters:
rewriter - reference to primary rewriter
Method Detail

doClass

public org.apache.bcel.generic.ClassGen doClass(org.apache.bcel.generic.ClassGen cg)
Upcall to process class before field and method processing.

Specified by:
doClass in interface ClassTraversal.Visitor
Overrides:
doClass in class ClassTraversal.Empty
Parameters:
cg - BCEL class generator object to process
Returns:
processed BCEL class generator object

doMethod

public org.apache.bcel.generic.MethodGen doMethod(org.apache.bcel.generic.ClassGen cg,
                                                  org.apache.bcel.generic.MethodGen mg)
Upcall to process method before instruction processing.

Specified by:
doMethod in interface ClassTraversal.Visitor
Overrides:
doMethod in class ClassTraversal.Empty
Parameters:
cg - BCEL class generator object
mg - BCEL method generator object to process
Returns:
processed BCEL method generator object

doMethodPost

public org.apache.bcel.generic.MethodGen doMethodPost(org.apache.bcel.generic.ClassGen cg,
                                                      org.apache.bcel.generic.MethodGen mg)
Upcall to process method after instruction processing.

Specified by:
doMethodPost in interface ClassTraversal.Visitor
Overrides:
doMethodPost in class ClassTraversal.Empty
Parameters:
cg - BCEL field generator object
mg - BCEL method generator object to process
Returns:
processed BCEL method generator object

doInstruction

public void doInstruction(org.apache.bcel.generic.ClassGen cg,
                          org.apache.bcel.generic.MethodGen mg,
                          org.apache.bcel.generic.InstructionHandle ih,
                          org.apache.bcel.generic.Instruction inst)
                   throws java.lang.ClassNotFoundException
Upcall to process instruction. Note that instructions can be inserted and deleted directly using the BCEL API, and therefore this method returns void.

Specified by:
doInstruction in interface ClassTraversal.Visitor
Overrides:
doInstruction in class ClassTraversal.Empty
Parameters:
cg - BCEL class generator object
mg - BCEL method generator object
ih - BCEL instruction handle object to process
inst - BCEL instruction object to process
Throws:
java.lang.ClassNotFoundException

fixFrame

private static org.apache.bcel.verifier.structurals.Frame fixFrame(org.apache.bcel.verifier.structurals.Frame f)
Return BCEL frame with fewer object types (only relevant ones) in order to simplify frame save/restore code.

Parameters:
f - input frame from data flow
Returns:
frame with fewer object types

fixType

private static org.apache.bcel.generic.Type fixType(org.apache.bcel.generic.Type t)
Helper method for fixFrame to process individual types within Frame object.

Parameters:
t - input type from data flow frame
Returns:
simplified type

isNotInit

public static boolean isNotInit(org.apache.bcel.generic.Type t)
Returns whether Type is Uninitialized.

Parameters:
t - BCEL type
Returns:
whether type is uninitialized

createStateClass

private static org.apache.bcel.classfile.JavaClass createStateClass(org.apache.bcel.generic.MethodGen mg,
                                                                    int pc,
                                                                    org.apache.bcel.verifier.structurals.Frame f)
Generate state class for a specific execution point within a method. The generated classes subclass Event.ContinuationFrame, and have properly types variables called 'local_i' and 'stack_j' for each local variable and stack slot in the given frame. The class is unique names by its method and execution point.

Parameters:
mg - method whose frame is being saved
pc - program location identifier
f - frame to save
Returns:
BCEL class object

saveFrame

private static void saveFrame(org.apache.bcel.generic.MethodGen mg,
                              org.apache.bcel.generic.InstructionHandle ih,
                              int pc,
                              org.apache.bcel.verifier.structurals.Frame f,
                              org.apache.bcel.generic.InstructionFactory ifc)
Insert instructions to save an execution frame to state object.

Parameters:
mg - method to be saved
ih - instruction handle of save point
pc - program location identifier
f - stack and local types
ifc - class instruction factory

restoreFrame

private static void restoreFrame(org.apache.bcel.generic.MethodGen mg,
                                 java.util.Vector frames,
                                 java.util.Vector ihs,
                                 org.apache.bcel.generic.InstructionFactory ifc)
Insert instructions to restore an execution frame from a state object.

Parameters:
mg - method to be restored onto stack
frames - frames of restoration points
ihs - instruction handles of restoration points
ifc - class instruction factory