API: removed Inbox and Outbox classes, replaced with isInbox(), isOutbox()
[fleet.git] / src / edu / berkeley / fleet / api / Instruction.java
1 package edu.berkeley.fleet.api;
2
3 public abstract class Instruction {
4
5     public static class Kill extends Instruction {
6
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;
12             this.count=count;
13             this.killOnlyStandingInstructions = killOnlyStandingInstructions;
14         }
15         public String toString() { return (count>1 ? "["+count+"] " : "") + "kill"; }
16
17     }
18
19     public static class Executable extends Instruction {
20
21         public final BenkoBox    benkoBox;
22         public final Destination dest;
23         public final int         count;
24
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;
31
32         /** count=0 denotes a standing move */
33         public Executable(BenkoBox    benkoBox,
34                           Destination dest,
35                           int         count,
36                           boolean     tokenIn,
37                           boolean     dataIn,
38                           boolean     latch,
39                           boolean     dataOut,
40                           boolean     tokenOut,
41                           boolean     recycle) {
42             this.benkoBox = benkoBox;
43             this.dest = dest;
44             this.count = count;
45             this.tokenIn = tokenIn;
46             this.dataIn = dataIn;
47             this.latch = latch;
48             this.dataOut = dataOut;
49             this.tokenOut = tokenOut;
50             this.recycle = recycle;
51             if (count < 0)
52                 throw new RuntimeException("count field of an instruction must be >=0");
53         }
54
55         public boolean isStanding() {
56             return count==0;
57         }
58
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);
63         }
64
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 ? ")" : "] ";
72             }
73             boolean needcomma = false;
74             if (tokenIn)           { ret += (needcomma ? ", " : "") + "wait";    needcomma = true; }
75             if (dataIn && latch)  {
76                 if (benkoBox.isInbox())
77                     ret += (needcomma ? ", " : "") + "receive";
78                 else
79                     ret += (needcomma ? ", " : "") + "take";
80                 needcomma = true;
81             }
82             if (dataIn && !latch)  { ret += (needcomma ? ", " : "") + "dismiss"; needcomma = true; }
83             if (dataOut)  {
84                 if (benkoBox.isInbox() || dest==null)
85                     ret += (needcomma ? ", " : "") + "deliver";
86                 else
87                     ret += (needcomma ? ", " : "") + "sendto "+dest;
88                 needcomma = true;
89             }
90             if (tokenOut) { ret += (needcomma ? ", " : "") + "notify "+dest; needcomma = true; }
91             return ret;
92         }
93
94     }
95
96     public static class Literal extends Instruction {
97         public final Destination dest;
98         protected Literal(Destination dest) { this.dest = dest; }
99
100         public static class Absolute extends Literal {
101             public final long value;
102             public Absolute(Destination dest, long value) { super(dest); this.value = value; }
103             public String toString() {
104                 return value + ": sendto " + dest;
105             }
106         }
107
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(Destination 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;
116             }
117         }
118
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(Destination 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;
129             }
130         }
131     }
132 }