public class InstructionExecutor extends java.lang.Object implements AsmVisitor<State,XVMException>
The main entry point is the execute()
function.
Here is a self-contained example of compiling and executing a While program.
import java.util.List;
import edu.polytechnique.mjava.ast.TProcDef;
import edu.polytechnique.xvm.exec.InstructionExecutor;
import edu.polytechnique.xvm.exec.State;
import edu.polytechnique.xvm.exec.XVMException;
public class CompileAndExecuteProgram {
private static String PATH = "example.wil";
public static void main(String[] args) throws Exception {
if (args.length == 1) PATH = args[0];
List<TProcDef<AbstractExpr, AbstractInstruction>> prg =
ParseAndType.parseAndTypeProgramFromFile(PATH);
ProgramCodeGen cg = new ProgramCodeGen();
// Compile the program using your CodeGen/ProgramCodeGen implementations
cg.codegen(prg);
// (Optional) print out the generated XVM
System.out.print(cg.cg);
// Run the XVM. The third argument of -1 means that the interpreter
// can run as long as you want.
State xvmState = new State();
xvmState.setOutputHandler(System.out::println);
InstructionExecutor.execute(xvmState, cg.cg, -1);
}
}State| Modifier and Type | Field and Description |
|---|---|
java.util.List<AsmInstruction> |
instructions |
java.util.Map<java.lang.String,java.lang.Integer> |
labels |
public final java.util.List<AsmInstruction> instructions
public final java.util.Map<java.lang.String,java.lang.Integer> labels
public void run(State state, int tickBudget) throws XVMException
XVMExceptionpublic static <CodeGen> void execute(State state, CodeGen cg, int tickBudget) throws XVMException
This is generic on the CodeGen implementation as long as it provides two fields:
List<AsmInstruction> instructionsMap<String, Integer> labelsstate - The initial state to run fromtickBudget - The number of ticks to run for (negative means infinite)XVMExceptionpublic static <CodeGen> State execute(CodeGen cg, int tickBudget) throws XVMException
execute(State, CodeGen, int),
except it creates the initial state and returns it at the end.XVMExceptionpublic XVMException visit(ADD asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(SUB asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(MULT asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(DIV asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(EQ asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(LT asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(AND asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(OR asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(NOT asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(GTO asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(GSB asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(RET asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(GTZ asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(PUSH asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(POP asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(PRT asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(PXR asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(PRX asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(FETCH asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(READ asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(WRITE asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(ALLOC asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(CREAD asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(CWRITE asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(RFR asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(WFR asm, State state)
visit in interface AsmVisitor<State,XVMException>public XVMException visit(STOP asm, State state)
visit in interface AsmVisitor<State,XVMException>