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) {
69 if (count>1) ret += count;
70 if (count==0) ret += "*";
71 if (recycle) ret += "r";
74 boolean needcomma = false;
75 if (tokenIn) { ret += (needcomma ? ", " : "") + "wait"; needcomma = true; }
76 if (dataIn && latch) { ret += (needcomma ? ", " : "") + "take"; needcomma = true; }
77 if (dataIn && !latch) { ret += (needcomma ? ", " : "") + "discard"; needcomma = true; }
79 if (benkoBox instanceof BenkoBox.Inbox || dest==null)
80 ret += (needcomma ? ", " : "") + "deliver";
82 ret += (needcomma ? ", " : "") + "sendto "+dest;
85 if (tokenOut) { ret += (needcomma ? ", " : "") + "ack "+dest; needcomma = true; }
91 public static class Literal extends Instruction {
92 public final BenkoBox dest;
93 protected Literal(BenkoBox dest) { this.dest = dest; }
95 public static class Absolute extends Literal {
96 public final long value;
97 public Absolute(BenkoBox dest, long value) { super(dest); this.value = value; }
98 public String toString() {
99 return value + ": sendto " + dest;
103 public static class Relative extends Literal {
104 /** value transmitted will be offset plus the address from which this instruction was loaded */
105 public final long offset;
106 public Relative(BenkoBox dest, long offset) { super(dest); this.offset = offset; }
107 public String toString() {
108 String off = ""+offset;
109 if (offset > 0) off = "+"+off;
110 return "(@"+offset+"): sendto " + dest;
114 public static class CodeBagDescriptor extends Literal {
115 /** address of CBD, relative to address that this instruction was loaded from */
116 public final long offset;
117 public final long size;
118 public CodeBagDescriptor(BenkoBox dest, long offset, long size) {
119 super(dest); this.offset = offset; this.size = size; }
120 public String toString() {
121 String off = ""+offset;
122 if (offset > 0) off = "+"+off;
123 return "(@"+off+":"+size+"): sendto " + dest;