Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created on 10 nov 2009
- */
- package interpreter.api;
- import java.io.PrintStream;
- import java.util.NoSuchElementException;
- import interpreter.api.util.InterpreterStackOverflow;
- import interpreter.api.util.InterpreterYieldTimeout;
- public class ExecutionThread
- {
- public final InterpreterProcess process;
- public final String name;
- private final ExecutionFrame[] frames;
- private int frameIndex;
- public ExecutionThread(InterpreterProcess process, String name, int maxDepth)
- {
- this.process = process;
- this.name = name;
- this.frames = new ExecutionFrame[maxDepth];
- }
- public void dump(PrintStream out)
- {
- out.println("Thread-dump in java-thread \"" + Thread.currentThread().getName() + "\" / step-thread \"" + this.name + "\"");
- out.println(" at " + this.current);
- for (int i = this.frameIndex - 1; i >= 0; i--)
- out.println(" at " + this.frames[i]);
- }
- public boolean reachedFrame(ExecutionFrame find)
- {
- return this.frameIndex == 0 || this.current == find;
- }
- public ExecutionFrame findCallsite(ExecutionFrame frame)
- {
- if (this.current == frame && this.frameIndex != 0)
- return this.frames[this.frameIndex - 1];
- for (int i = 0; i < this.frameIndex; i++)
- {
- if (this.frames[i] == frame)
- {
- if (i == 0)
- return null;
- return this.frames[i - 1];
- }
- }
- throw new NoSuchElementException();
- }
- //
- private boolean yielded = false;
- public void yield()
- {
- if (this.yielded)
- throw new IllegalStateException();
- this.yielded = true;
- }
- //
- public boolean stepUntilYield(ExecutionFrame callsite, int maxSteps) throws Throwable
- {
- for (int i = 0; i < maxSteps; i++)
- {
- if (this.yielded)
- {
- this.yielded = false;
- return true; // continue later
- }
- if (!this.step())
- return false; // end!
- if (this.reachedFrame(callsite))
- return false;
- }
- throw new InterpreterYieldTimeout();
- }
- public boolean stepUntilYield(ExecutionFrame callsite) throws Throwable
- {
- while (true)
- {
- if (this.yielded)
- {
- this.yielded = false;
- return true; // continue later
- }
- if (!this.step())
- return false; // end!
- if (this.reachedFrame(callsite))
- return false;
- }
- }
- public boolean step() throws Throwable
- {
- ExecutionFrame frame = this.currentFrame();
- if (frame.method == null)
- {
- this.popFrame();
- return true;
- }
- if (frame.method.impl != null)
- {
- frame.method.impl.call(frame); // pops frame
- return true;
- }
- return frame.method.stream.step(frame);
- }
- //
- public void vreturn()
- {
- this.popFrame();
- }
- public void ireturn()
- {
- int value = this.currentFrame().popInt();
- this.popFrame().pushInt(value);
- }
- public void lreturn()
- {
- long value = this.currentFrame().popLong();
- this.popFrame().pushLong(value);
- }
- public void freturn()
- {
- float value = this.currentFrame().popFloat();
- this.popFrame().pushFloat(value);
- }
- public void dreturn()
- {
- double value = this.currentFrame().popDouble();
- this.popFrame().pushDouble(value);
- }
- public void areturn()
- {
- int value = this.currentFrame().popReference();
- this.popFrame().pushInt(value);
- }
- //
- private ExecutionFrame current;
- public void pushFrame(ExecutionFrame frame)
- {
- if (this.current != null)
- if (this.frameIndex < this.frames.length)
- this.frames[this.frameIndex++] = this.current;
- else
- throw new InterpreterStackOverflow();
- this.current = frame;
- }
- public ExecutionFrame currentFrame()
- {
- return this.current;
- }
- private ExecutionFrame popFrame()
- {
- return this.current = this.frames[--this.frameIndex];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement