1 package edu.berkeley.fleet.api;
3 public abstract class Instruction {
5 public final Pump pump;
6 public Instruction(Pump pump) { this.pump = pump; }
7 public String toString() { return pump+": "; }
9 public static abstract class CountingInstruction extends Instruction {
10 public final int count;
11 public CountingInstruction(Pump pump, int count) {
15 public boolean isStanding() { return count==0; }
16 public abstract boolean isRequeueing();
17 public String toString() { return super.toString()+(count==1?"":(isRequeueing()?("("+(count==0?"*":(count+""))+")"):("["+(count==0?"*":(count+""))+"]")))+" "; }
20 public static class UnClog extends Instruction {
21 public UnClog(Pump pump) { super(pump); }
22 public String toString() { return super.toString() + "unclog;"; }
25 public static class Clog extends Executable {
26 public Clog(Pump pump) { super(pump, 1); }
27 public String toString() { return super.toString() + "clog;"; }
28 public Instruction.Executable decrementCount() { return null; }
29 public boolean isRequeueing() { return false; }
32 public static class Kill extends CountingInstruction {
33 public Kill(Pump pump, int count) { super(pump, count); }
34 public boolean isRequeueing() { return false; }
35 public String toString() { return super.toString() + "kill;"; }
38 public static abstract class Executable extends CountingInstruction {
39 public Executable(Pump pump, int count) { super(pump, count); }
40 public abstract Instruction.Executable decrementCount();
43 public static class Move extends Executable {
44 public final Destination dest;
46 public final boolean tokenIn;
47 public final boolean dataIn;
48 public final boolean latch;
49 public final boolean dataOutDest;
50 public final boolean dataOut;
51 public final boolean tokenOut;
52 public final boolean requeue;
53 public final boolean ignoreUntilLast;
55 public boolean isRequeueing() { return requeue; }
57 /** count=0 denotes a standing move */
58 public Move(Pump pump,
68 boolean ignoreUntilLast) {
71 this.tokenIn = tokenIn;
74 this.dataOutDest = dataOutDest;
75 this.dataOut = dataOut;
76 this.tokenOut = tokenOut;
77 this.requeue = requeue;
78 this.ignoreUntilLast = ignoreUntilLast;
80 throw new RuntimeException("count field of an instruction must be >=0");
81 if (pump.isInbox() && tokenIn && dataIn)
82 throw new RuntimeException("cannot have both \"wait\" and \"take\"/\"recieve\" on an inbox: " + this);
83 if (pump.isOutbox() && tokenOut && dataOut)
84 throw new RuntimeException("cannot have both \"sendto\" and \"notify\" on an outbox: " + this);
86 throw new RuntimeException("cannot have latch bit set without dataIn bit: " + this);
89 public Instruction.Executable decrementCount() {
90 if (count==1) return null;
91 return new Move(pump, dest, count==0 ? 0 : count-1,
92 tokenIn, dataIn, latch, dataOutDest, dataOut, tokenOut, requeue, ignoreUntilLast);
95 public String toString() {
97 String ret = super.toString();
98 boolean needcomma = false;
99 if (tokenIn) { ret += (needcomma ? ", " : "") + "wait"; needcomma = true; }
100 if (dataIn && latch) {
102 ret += (needcomma ? ", " : "") + "receive";
104 ret += (needcomma ? ", " : "") + "take";
107 if (dataIn && !latch) { ret += (needcomma ? ", " : "") + "dismiss"; needcomma = true; }
109 if (pump.isInbox() || dest==null)
110 ret += (needcomma ? ", " : "") + "deliver";
112 ret += (needcomma ? ", " : "") + "sendto "+dest;
115 if (tokenOut) { ret += (needcomma ? ", " : "") + "notify "+dest; needcomma = true; }
121 public static class LocalLiteral extends Executable {
122 public final long literal;
123 public boolean isRequeueing() { return true; }
124 public LocalLiteral(Pump pump, long literal, int count) {
126 this.literal = literal;
128 public Instruction.Executable decrementCount() {
129 if (count==1) return null;
130 return new LocalLiteral(pump, literal, count-1);
134 public static class CodeBagDescriptor extends Instruction {
135 /** address of CBD, relative to address that this instruction was loaded from */
136 public final long offset;
137 public final long size;
138 public CodeBagDescriptor(Pump pump, long offset, long size) {
140 this.offset = offset;
143 public String toString() {
144 String off = ""+offset;
145 if (offset > 0) off = "+"+off;
146 return "(CBD @"+off+"+"+size+"): sendto " + pump;