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 Kill(BenkoBox benkoBox, int count) { this.benkoBox=benkoBox; this.count=count; }
10 public String toString() { return (count>1 ? "["+count+"] " : "") + "kill"; }
14 public static class Executable extends Instruction {
16 public final BenkoBox benkoBox;
17 public final BenkoBox dest;
18 public final int count;
20 public final boolean tokenIn;
21 public final boolean dataIn;
22 public final boolean latch;
23 public final boolean dataOut;
24 public final boolean tokenOut;
25 public final boolean recycle;
27 /** count=0 denotes a standing move */
28 public Executable(BenkoBox benkoBox,
37 this.benkoBox = benkoBox;
40 this.tokenIn = tokenIn;
43 this.dataOut = dataOut;
44 this.tokenOut = tokenOut;
45 this.recycle = recycle;
47 throw new RuntimeException("count field of an instruction must be >=0");
50 public Instruction.Executable decrementCount() {
51 if (count==1) return null;
52 return new Executable(benkoBox, dest, count==0 ? 0 : count-1,
53 tokenIn, dataIn, latch, dataOut, tokenOut, recycle);
56 public String toString() {
57 String ret = benkoBox.toString() + ": ";
58 if (count==0 || count>1 || recycle) {
60 if (count>1) ret += count;
61 if (count==0) ret += "*";
62 if (recycle) ret += "r";
65 boolean needcomma = false;
66 if (tokenIn) { ret += (needcomma ? ", " : "") + "wait"; needcomma = true; }
67 if (dataIn && latch) { ret += (needcomma ? ", " : "") + "take"; needcomma = true; }
68 if (dataIn && !latch) { ret += (needcomma ? ", " : "") + "discard"; needcomma = true; }
70 if (benkoBox instanceof BenkoBox.Inbox || dest==null)
71 ret += (needcomma ? ", " : "") + "deliver";
73 ret += (needcomma ? ", " : "") + "sendto "+dest;
76 if (tokenOut) { ret += (needcomma ? ", " : "") + "ack "+dest; needcomma = true; }
82 public static class Literal extends Instruction {
83 public final BenkoBox dest;
84 protected Literal(BenkoBox dest) { this.dest = dest; }
86 public static class Absolute extends Literal {
87 public final long value;
88 public Absolute(BenkoBox dest, long value) { super(dest); this.value = value; }
89 public String toString() {
90 return value + ": sendto " + dest;
94 public static class Relative extends Literal {
95 /** value transmitted will be offset plus the address from which this instruction was loaded */
96 public final long offset;
97 public Relative(BenkoBox dest, long offset) { super(dest); this.offset = offset; }
98 public String toString() {
99 String off = ""+offset;
100 if (offset > 0) off = "+"+off;
101 return "(@"+offset+"): sendto " + dest;
105 public static class CodeBagDescriptor extends Literal {
106 /** address of CBD, relative to address that this instruction was loaded from */
107 public final long offset;
108 public final long size;
109 public CodeBagDescriptor(BenkoBox dest, long offset, long size) {
110 super(dest); this.offset = offset; this.size = size; }
111 public String toString() {
112 String off = ""+offset;
113 if (offset > 0) off = "+"+off;
114 return "(@"+off+":"+size+"): sendto " + dest;