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;
14 if (killOnlyStandingInstructions && count !=1)
15 throw new RuntimeException("currently, kill* must have count=1");
17 public String toString() { return (count>1 ? "["+count+"] " : "") + "kill" + (killOnlyStandingInstructions ? "*" : ""); }
21 public static class Executable extends Instruction {
23 public final BenkoBox benkoBox;
24 public final Destination dest;
25 public final int count;
27 public final boolean tokenIn;
28 public final boolean dataIn;
29 public final boolean latch;
30 public final boolean dataOut;
31 public final boolean tokenOut;
32 public final boolean requeue;
34 /** count=0 denotes a standing move */
35 public Executable(BenkoBox benkoBox,
44 this.benkoBox = benkoBox;
47 this.tokenIn = tokenIn;
50 this.dataOut = dataOut;
51 this.tokenOut = tokenOut;
52 this.requeue = requeue;
54 throw new RuntimeException("count field of an instruction must be >=0");
57 public boolean isStanding() {
61 public Instruction.Executable decrementCount() {
62 if (count==1) return null;
63 return new Executable(benkoBox, dest, count==0 ? 0 : count-1,
64 tokenIn, dataIn, latch, dataOut, tokenOut, requeue);
67 public String toString() {
68 String ret = benkoBox.toString() + ": ";
69 if (count==0 || count>1 || requeue) {
70 ret += requeue ? "(" : "[";
71 if (count>1) ret += count;
72 if (count==0) ret += "*";
73 ret += requeue ? ")" : "] ";
75 boolean needcomma = false;
76 if (tokenIn) { ret += (needcomma ? ", " : "") + "wait"; needcomma = true; }
77 if (dataIn && latch) {
78 if (benkoBox.isInbox())
79 ret += (needcomma ? ", " : "") + "receive";
81 ret += (needcomma ? ", " : "") + "take";
84 if (dataIn && !latch) { ret += (needcomma ? ", " : "") + "dismiss"; needcomma = true; }
86 if (benkoBox.isInbox() || dest==null)
87 ret += (needcomma ? ", " : "") + "deliver";
89 ret += (needcomma ? ", " : "") + "sendto "+dest;
92 if (tokenOut) { ret += (needcomma ? ", " : "") + "notify "+dest; needcomma = true; }
98 public static class Literal extends Instruction {
99 public final Destination dest;
100 protected Literal(Destination dest) { this.dest = dest; }
102 public static class Absolute extends Literal {
103 public final long value;
104 public Absolute(Destination dest, long value) { super(dest); this.value = value; }
105 public String toString() {
106 return value + ": sendto " + dest;
110 public static class Relative extends Literal {
111 /** value transmitted will be offset plus the address from which this instruction was loaded */
112 public final long offset;
113 public Relative(Destination dest, long offset) { super(dest); this.offset = offset; }
114 public String toString() {
115 String off = ""+offset;
116 if (offset > 0) off = "+"+off;
117 return "(@"+offset+"): sendto " + dest;
121 public static class CodeBagDescriptor extends Literal {
122 /** address of CBD, relative to address that this instruction was loaded from */
123 public final long offset;
124 public final long size;
125 public CodeBagDescriptor(Destination dest, long offset, long size) {
126 super(dest); this.offset = offset; this.size = size; }
127 public String toString() {
128 String off = ""+offset;
129 if (offset > 0) off = "+"+off;
130 return "(CBD @"+off+"+"+size+"): sendto " + dest;