1 package edu.berkeley.fleet.api;
3 public abstract class Instruction {
5 public static class Kill extends Instruction {
7 public final Pump pump;
8 public final int count;
9 public final boolean killOnlyStandingInstructions;
10 public Kill(Pump pump, int count, boolean killOnlyStandingInstructions) {
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 Pump pump;
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 dataOutDest;
31 public final boolean dataOut;
32 public final boolean tokenOut;
33 public final boolean requeue;
35 /** count=0 denotes a standing move */
36 public Executable(Pump pump,
49 this.tokenIn = tokenIn;
52 this.dataOutDest = dataOutDest;
53 this.dataOut = dataOut;
54 this.tokenOut = tokenOut;
55 this.requeue = requeue;
57 throw new RuntimeException("count field of an instruction must be >=0");
60 public boolean isStanding() {
64 public Instruction.Executable decrementCount() {
65 if (count==1) return null;
66 return new Executable(pump, dest, count==0 ? 0 : count-1,
67 tokenIn, dataIn, latch, dataOutDest, dataOut, tokenOut, requeue);
70 public String toString() {
71 String ret = pump.toString() + ": ";
72 if (count==0 || count>1 || requeue) {
73 ret += requeue ? "(" : "[";
74 if (count>1) ret += count;
75 if (count==0) ret += "*";
76 ret += requeue ? ")" : "] ";
78 boolean needcomma = false;
79 if (tokenIn) { ret += (needcomma ? ", " : "") + "wait"; needcomma = true; }
80 if (dataIn && latch) {
82 ret += (needcomma ? ", " : "") + "receive";
84 ret += (needcomma ? ", " : "") + "take";
87 if (dataIn && !latch) { ret += (needcomma ? ", " : "") + "dismiss"; needcomma = true; }
89 if (pump.isInbox() || dest==null)
90 ret += (needcomma ? ", " : "") + "deliver";
92 ret += (needcomma ? ", " : "") + "sendto "+dest;
95 if (tokenOut) { ret += (needcomma ? ", " : "") + "notify "+dest; needcomma = true; }
101 public static class LocalLiteral extends Instruction {
102 public final Pump pump;
103 public final long literal;
104 public LocalLiteral(Pump pump, long literal) {
106 this.literal = literal;
110 public static class Literal extends Instruction {
111 public final Destination dest;
112 protected Literal(Destination dest) { this.dest = dest; }
114 public static class Absolute extends Literal {
115 public final long value;
116 public Absolute(Destination dest, long value) { super(dest); this.value = value; }
117 public String toString() {
118 return value + ": sendto " + dest;
122 public static class Relative extends Literal {
123 /** value transmitted will be offset plus the address from which this instruction was loaded */
124 public final long offset;
125 public Relative(Destination dest, long offset) { super(dest); this.offset = offset; }
126 public String toString() {
127 String off = ""+offset;
128 if (offset > 0) off = "+"+off;
129 return "(@"+offset+"): sendto " + dest;
134 public static class CodeBagDescriptor extends Instruction {
135 /** address of CBD, relative to address that this instruction was loaded from */
136 public final Pump pump;
137 public final long offset;
138 public final long size;
139 public CodeBagDescriptor(Pump pump, long offset, long size) {
141 this.offset = offset;
144 public String toString() {
145 String off = ""+offset;
146 if (offset > 0) off = "+"+off;
147 return "(CBD @"+off+"+"+size+"): sendto " + pump;