use of suite.instructionexecutor.InstructionUtil.Activation in project suite by stupidsing.
the class FunInstructionExecutor method handle.
@Override
protected void handle(Exec exec, Instruction insn) {
Activation current = exec.current;
Frame frame = current.frame;
Object[] regs = frame != null ? frame.registers : null;
Object[] ds = exec.stack;
int dsp = exec.sp;
Node n0, n1, result;
Data<?> data;
switch(insn.insn) {
case CALLINTRINSIC_:
data = (Data<?>) ds[--dsp];
List<Node> ps = new ArrayList<>(3);
for (int i = 1; i < insn.op1; i++) ps.add((Node) ds[--dsp]);
result = Data.<Intrinsic>get(data).invoke(intrinsicCallback, ps);
break;
case COMPARE_______:
n0 = (Node) ds[--dsp];
n1 = (Node) ds[--dsp];
result = Int.of(comparer.compare(n0, n1));
break;
case CONSLIST______:
n0 = (Node) ds[--dsp];
n1 = (Node) ds[--dsp];
result = Tree.of(TermOp.OR____, n0, n1);
break;
case CONSPAIR______:
n0 = (Node) ds[--dsp];
n1 = (Node) ds[--dsp];
result = Tree.of(TermOp.AND___, n0, n1);
break;
case DATACHARS_____:
result = new Data<>(To.chars(((Str) regs[insn.op1]).value));
break;
case GETINTRINSIC__:
Atom atom = (Atom) ds[--dsp];
String intrinsicName = atom.name.split("!")[1];
result = new Data<>(Intrinsics.intrinsics.get(intrinsicName));
break;
case HEAD__________:
result = Tree.decompose((Node) ds[--dsp]).getLeft();
break;
case ISCONS________:
result = atom(Tree.decompose((Node) ds[--dsp]) != null);
break;
case TAIL__________:
result = Tree.decompose((Node) ds[--dsp]).getRight();
break;
default:
result = Fail.t("unknown instruction " + insn);
}
exec.sp = dsp;
regs[insn.op0] = result;
}
use of suite.instructionexecutor.InstructionUtil.Activation 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.Activation 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