1 package edu.berkeley.fleet.api;
3 public abstract class Instruction {
5 public static class Kill extends Instruction {
7 public final BenkoBox benkoBox;
8 public final int count;
9 public final boolean killOnlyStandingInstructions;
10 public Kill(BenkoBox benkoBox, int count, boolean killOnlyStandingInstructions) {
11 this.benkoBox=benkoBox;
13 this.killOnlyStandingInstructions = killOnlyStandingInstructions;
15 public String toString() { return (count>1 ? "["+count+"] " : "") + "kill"; }
19 public static class Executable extends Instruction {
21 public final BenkoBox benkoBox;
22 public final BenkoBox dest;
23 public final int count;
25 public final boolean tokenIn;
26 public final boolean dataIn;
27 public final boolean latch;
28 public final boolean dataOut;
29 public final boolean tokenOut;
30 public final boolean recycle;
32 /** count=0 denotes a standing move */
33 public Executable(BenkoBox benkoBox,
42 this.benkoBox = benkoBox;
45 this.tokenIn = tokenIn;
48 this.dataOut = dataOut;
49 this.tokenOut = tokenOut;
50 this.recycle = recycle;
52 throw new RuntimeException("count field of an instruction must be >=0");
55 public boolean isStanding() {
59 public Instruction.Executable decrementCount() {
60 if (count==1) return null;
61 return new Executable(benkoBox, dest, count==0 ? 0 : count-1,
62 tokenIn, dataIn, latch, dataOut, tokenOut, recycle);
65 public String toString() {
66 String ret = benkoBox.toString() + ": ";
67 if (count==0 || count>1 || recycle) {
68 ret += recycle ? "(" : "[";
69 if (count>1) ret += count;
70 if (count==0) ret += "*";
71 ret += recycle ? ")" : "] ";
73 boolean needcomma = false;
74 if (tokenIn) { ret += (needcomma ? ", " : "") + "wait"; needcomma = true; }
75 if (dataIn && latch) {
76 if (benkoBox instanceof BenkoBox.Inbox)
77 ret += (needcomma ? ", " : "") + "receive";
79 ret += (needcomma ? ", " : "") + "take";
82 if (dataIn && !latch) { ret += (needcomma ? ", " : "") + "dismiss"; needcomma = true; }
84 if (benkoBox instanceof BenkoBox.Inbox || dest==null)
85 ret += (needcomma ? ", " : "") + "deliver";
87 ret += (needcomma ? ", " : "") + "sendto "+dest;
90 if (tokenOut) { ret += (needcomma ? ", " : "") + "notify "+dest; needcomma = true; }
96 public static class Literal extends Instruction {
97 public final BenkoBox dest;
98 protected Literal(BenkoBox dest) { this.dest = dest; }
100 public static class Absolute extends Literal {
101 public final long value;
102 public Absolute(BenkoBox dest, long value) { super(dest); this.value = value; }
103 public String toString() {
104 return value + ": sendto " + dest;
108 public static class Relative extends Literal {
109 /** value transmitted will be offset plus the address from which this instruction was loaded */
110 public final long offset;
111 public Relative(BenkoBox dest, long offset) { super(dest); this.offset = offset; }
112 public String toString() {
113 String off = ""+offset;
114 if (offset > 0) off = "+"+off;
115 return "(@"+offset+"): sendto " + dest;
119 public static class CodeBagDescriptor extends Literal {
120 /** address of CBD, relative to address that this instruction was loaded from */
121 public final long offset;
122 public final long size;
123 public CodeBagDescriptor(BenkoBox dest, long offset, long size) {
124 super(dest); this.offset = offset; this.size = size; }
125 public String toString() {
126 String off = ""+offset;
127 if (offset > 0) off = "+"+off;
128 return "(@"+off+":"+size+"): sendto " + dest;