1 package com.tapina.robe.runtime;
2
3 import java.io.Writer;
4 import java.io.IOException;
5
6 /***
7 * Created by IntelliJ IDEA.
8 * User: gareth
9 * Date: Aug 21, 2003
10 * Time: 8:00:02 AM
11 */
12 public abstract class Instruction {
13 public static int count = 0;
14 private final Condition condition;
15
16 public Instruction(Condition condition) {
17 this.condition = condition;
18 }
19
20 /***
21 * @param environment
22 * @return true if the pipeline should be flushed (i.e. branch has occurred)
23 */
24 public final boolean checkAndExecute(Environment environment) {
25 count++;
26 if (isConditionSatisfied(environment)) {
27 return execute(environment);
28 } else {
29 return false;
30 }
31 }
32
33 /***
34 * Try to avoid having much in the way of conditionals in this method - it should execute straight through
35 * and be as easily optimised/inlined as possible for speed. Do all your ifs and buts in the decoder or
36 * the constructor to this class.
37 * @param environment
38 * @return true if the pipeline should be flushed (i.e. branch has occurred)
39 */
40 protected abstract boolean execute(Environment environment);
41
42 private boolean isConditionSatisfied(Environment environment) {
43 return condition.isSatisfied(environment.getCpu());
44 }
45
46 protected final void dumpCondition(Writer out) throws IOException {
47 if (condition != Condition.AL) {
48 out.write(" if (");
49 out.write(condition.toJavaExpression());
50 out.write(") ");
51 }
52 }
53
54 public final void dumpJavaSource(Writer out) throws IOException {
55 out.write(" // " + toString() + "\n");
56
57 dumpCondition(out);
58 out.write("{\n");
59 dumpJavaSourceUnconditional(out);
60 out.write("}\n");
61 }
62
63 public abstract void dumpJavaSourceUnconditional(Writer out) throws IOException;
64 }