use of suite.instructionexecutor.InstructionUtil.Instruction in project suite by stupidsing.
the class InstructionExecutor method yawnThunk_.
private Node yawnThunk_(Thunk thunk0) {
Frame f0 = new Frame(null, 2);
f0.registers[0] = thunk0;
Activation current = new Activation(f0, yawnEntryPoint, null);
Node[] stack = new Node[Suite.stackSize];
int ip = 0, sp = 0;
Node returnValue = null;
Exec exec = new Exec();
exec.stack = stack;
Comparer comparer = comparer();
Tree tree;
while (true) try {
Frame frame = current.frame;
Node[] regs = frame != null ? frame.registers : null;
Instruction insn = instructions[ip = current.ip++];
Thunk thunk;
TermOp op;
int i;
switch(insn.insn) {
case ASSIGNCONST___:
regs[insn.op0] = constantPool.get(insn.op1);
break;
case ASSIGNFRAMEREG:
i = insn.op1;
while (i++ < 0) frame = frame.previous;
regs[insn.op0] = frame.registers[insn.op2];
break;
case ASSIGNINT_____:
regs[insn.op0] = number(insn.op1);
break;
case ASSIGNRESULT__:
regs[insn.op0] = returnValue;
break;
case ASSIGNTHUNK___:
regs[insn.op0] = new Thunk(frame, insn.op1);
break;
case ASSIGNTHUNKRES:
regs[insn.op0] = returnValue;
thunk = (Thunk) regs[insn.op1];
// facilitates garbage collection
thunk.frame = null;
thunk.result = returnValue;
break;
case CALL__________:
current = new Activation(frame, insn.op0, current);
break;
case CALLTHUNK_____:
thunk = (Thunk) regs[insn.op0];
if (thunk.result == null)
current = new Activation(thunk, current);
else
returnValue = thunk.result;
break;
case ENTER_________:
AnalyzedFrame af = analyzer.getFrame(ip);
Frame parent = af.isRequireParent() ? frame : null;
Instruction frameBegin = instructions[af.getFrameBeginIp()];
current.frame = new Frame(parent, frameBegin.op0);
break;
case ERROR_________:
Fail.t("error termination");
case EVALADD_______:
regs[insn.op0] = number(i(regs[insn.op1]) + i(regs[insn.op2]));
break;
case EVALDIV_______:
regs[insn.op0] = number(i(regs[insn.op1]) / i(regs[insn.op2]));
break;
case EVALEQ________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i == 0);
break;
case EVALLE________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i <= 0);
break;
case EVALLT________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i < 0);
break;
case EVALNE________:
i = comparer.compare(regs[insn.op1], regs[insn.op2]);
regs[insn.op0] = atom(i != 0);
break;
case EVALMOD_______:
regs[insn.op0] = number(i(regs[insn.op1]) % i(regs[insn.op2]));
break;
case EVALMUL_______:
regs[insn.op0] = number(i(regs[insn.op1]) * i(regs[insn.op2]));
break;
case EVALSUB_______:
regs[insn.op0] = number(i(regs[insn.op1]) - i(regs[insn.op2]));
break;
case EXIT__________:
return returnValue;
case FORMTREE0_____:
Node left = regs[insn.op0];
Node right = regs[insn.op1];
insn = instructions[current.ip++];
op = TermOp.find(((Atom) constantPool.get(insn.op0)).name);
regs[insn.op1] = Tree.of(op, left, right);
break;
case FRAMEBEGIN____:
case FRAMEEND______:
break;
case IFFALSE_______:
if (regs[insn.op0] != Atom.TRUE)
current.ip = insn.op1;
break;
case IFNOTCONS_____:
if ((tree = Tree.decompose(regs[insn.op0], TermOp.OR____)) != null) {
stack[sp++] = tree.getLeft();
stack[sp++] = tree.getRight();
} else
current.ip = insn.op1;
break;
case IFNOTPAIR_____:
if ((tree = Tree.decompose(regs[insn.op0], TermOp.AND___)) != null) {
stack[sp++] = tree.getLeft();
stack[sp++] = tree.getRight();
} else
current.ip = insn.op1;
break;
case IFNOTEQUALS___:
if (regs[insn.op1] != regs[insn.op2])
current.ip = insn.op0;
break;
case JUMP__________:
current.ip = insn.op0;
break;
case JUMPCLOSURE___:
thunk = (Thunk) regs[insn.op0];
current = current.previous;
if (thunk.result == null)
current = new Activation(thunk, current);
else
returnValue = thunk.result;
break;
case LEAVE_________:
current.frame = current.frame.previous;
break;
case LOGREG________:
LogUtil.info(regs[insn.op0].toString());
break;
case NEWNODE_______:
regs[insn.op0] = new Reference();
break;
case PUSH__________:
stack[sp++] = regs[insn.op0];
break;
case POP___________:
regs[insn.op0] = stack[--sp];
break;
case POPANY________:
--sp;
break;
case REMARK________:
break;
case RETURN________:
current = current.previous;
break;
case SETRESULT_____:
returnValue = regs[insn.op0];
break;
case TOP___________:
regs[insn.op0] = stack[sp + insn.op1];
break;
default:
exec.current = current;
exec.sp = sp;
handle(exec, insn);
current = exec.current;
sp = exec.sp;
}
} catch (Exception ex) {
Fail.t("at IP = " + ip, ex);
}
}
use of suite.instructionexecutor.InstructionUtil.Instruction in project suite by stupidsing.
the class InstructionExtractor method getRegisterNumber.
private int getRegisterNumber(List<Node> rs, int index) {
if (index < rs.size()) {
Node node = rs.get(index).finalNode();
Tree tree;
if (node instanceof Int)
return ((Int) node).number;
else if (node instanceof Reference) {
// transient register
// allocates new register in current local frame
Instruction frameBegin = frameBegins.getFirst();
int registerNumber = frameBegin.op0++;
Binder.bind(node, Int.of(registerNumber), trail);
return registerNumber;
} else if ((tree = Tree.decompose(node, TermOp.COLON_)) != null) {
Node key = tree.getLeft(), value = tree.getRight();
if (key == KEYC)
return allocateInPool(value);
else if (key == KEYL)
return ipByLabelId.get(IdentityKey.of(value));
else if (key == KEYR)
return 0;
}
return Fail.t("cannot parse instruction " + rs.get(0) + " operand " + node);
} else
return 0;
}
use of suite.instructionexecutor.InstructionUtil.Instruction in project suite by stupidsing.
the class InstructionExtractor method extract.
private Instruction extract(List<Node> rs) {
String insnName = ((Atom) rs.get(0)).name;
Insn insn;
if (Objects.equals(insnName, "EVALUATE")) {
Atom atom = (Atom) rs.remove(3);
TermOp operator = TermOp.find(atom.name);
insn = InstructionUtil.getEvalInsn(operator);
} else
insn = InstructionUtil.getInsn(insnName);
if (insn != null) {
Instruction instruction = new //
Instruction(//
insn, //
getRegisterNumber(rs, 1), //
getRegisterNumber(rs, 2), getRegisterNumber(rs, 3));
if (insn == Insn.FRAMEBEGIN____)
frameBegins.push(instruction);
else if (insn == Insn.FRAMEEND______)
frameBegins.pop();
return instruction;
} else
return Fail.t("unknown opcode " + insnName);
}
use of suite.instructionexecutor.InstructionUtil.Instruction in project suite by stupidsing.
the class LazyFunInstructionExecutor method postprocessInstructions.
@Override
protected void postprocessInstructions(List<Instruction> list) {
invokeJavaEntryPoint = list.size();
list.add(new Instruction(Insn.FRAMEBEGIN____, 3, 0, 0));
list.add(new Instruction(Insn.PUSH__________, 0, 0, 0));
list.add(new Instruction(Insn.PUSH__________, 1, 0, 0));
list.add(new Instruction(Insn.CALLINTRINSIC_, 2, 2, 0));
list.add(new Instruction(Insn.SETRESULT_____, 2, 0, 0));
list.add(new Instruction(Insn.RETURN________, 0, 0, 0));
list.add(new Instruction(Insn.FRAMEEND______, 0, 0, 0));
super.postprocessInstructions(list);
}
use of suite.instructionexecutor.InstructionUtil.Instruction in project suite by stupidsing.
the class LogicInstructionExecutor method handle.
@Override
protected void handle(Exec exec, Instruction insn) {
Activation current = exec.current;
Frame frame = current.frame;
Node[] regs = frame != null ? frame.registers : null;
Trail trail = prover.getTrail();
Instruction insn1;
switch(insn.insn) {
case BACKUPCSP_____:
regs[insn.op0] = exec.current.previous;
break;
case BACKUPDSP_____:
regs[insn.op0] = number(exec.sp);
break;
case BIND__________:
if (!Binder.bind(regs[insn.op0], regs[insn.op1], trail))
// fail
current.ip = insn.op2;
break;
case BINDMARK______:
regs[insn.op0] = number(trail.getPointInTime());
break;
case BINDUNDO______:
trail.unwind(i(regs[insn.op0]));
break;
case DECOMPOSETREE0:
Node node = regs[insn.op0].finalNode();
insn1 = getInstructions()[current.ip++];
TermOp op = TermOp.find(((Atom) constantPool.get(insn1.op0)).name);
int rl = insn1.op1;
int rr = insn1.op2;
if (node instanceof Tree) {
Tree tree = (Tree) node;
if (tree.getOperator() == op) {
regs[rl] = tree.getLeft();
regs[rr] = tree.getRight();
} else
current.ip = insn.op1;
} else if (node instanceof Reference) {
Tree tree = Tree.of(op, regs[rl] = new Reference(), regs[rr] = new Reference());
trail.addBind((Reference) node, tree);
} else
current.ip = insn.op1;
break;
case PROVEINTERPRET:
if (!prover.prove(regs[insn.op0]))
current.ip = insn.op1;
break;
case PROVESYS______:
if (!systemPredicates.call(regs[insn.op0]))
current.ip = insn.op1;
break;
case RESTORECSP____:
exec.current.previous = (Activation) regs[insn.op0];
break;
case RESTOREDSP____:
exec.sp = i(regs[insn.op0]);
break;
default:
Fail.t("unknown instruction " + insn);
}
}
Aggregations