API: inert changes (cleanup, visibility, toString())
[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             if (killOnlyStandingInstructions && count !=1)
15                 throw new RuntimeException("currently, kill* must have count=1");
16         }
17         public String toString() { return (count>1 ? "["+count+"] " : "") + "kill" + (killOnlyStandingInstructions ? "*" : ""); }
18
19     }
20
21     public static class Executable extends Instruction {
22
23         public final BenkoBox    benkoBox;
24         public final Destination dest;
25         public final int         count;
26
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  recycle;
33
34         /** count=0 denotes a standing move */
35         public Executable(BenkoBox    benkoBox,
36                           Destination dest,
37                           int         count,
38                           boolean     tokenIn,
39                           boolean     dataIn,
40                           boolean     latch,
41                           boolean     dataOut,
42                           boolean     tokenOut,
43                           boolean     recycle) {
44             this.benkoBox = benkoBox;
45             this.dest = dest;
46             this.count = count;
47             this.tokenIn = tokenIn;
48             this.dataIn = dataIn;
49             this.latch = latch;
50             this.dataOut = dataOut;
51             this.tokenOut = tokenOut;
52             this.recycle = recycle;
53             if (count < 0)
54                 throw new RuntimeException("count field of an instruction must be >=0");
55         }
56
57         public boolean isStanding() {
58             return count==0;
59         }
60
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, recycle);
65         }
66
67         public String toString() {
68             String ret = benkoBox.toString() + ": ";
69             if (count==0 || count>1 || recycle) {
70                 ret += recycle ? "(" : "[";
71                 if (count>1) ret += count;
72                 if (count==0) ret += "*";
73                 ret += recycle ? ")" : "] ";
74             }
75             boolean needcomma = false;
76             if (tokenIn)           { ret += (needcomma ? ", " : "") + "wait";    needcomma = true; }
77             if (dataIn && latch)  {
78                 if (benkoBox.isInbox())
79                     ret += (needcomma ? ", " : "") + "receive";
80                 else
81                     ret += (needcomma ? ", " : "") + "take";
82                 needcomma = true;
83             }
84             if (dataIn && !latch)  { ret += (needcomma ? ", " : "") + "dismiss"; needcomma = true; }
85             if (dataOut)  {
86                 if (benkoBox.isInbox() || dest==null)
87                     ret += (needcomma ? ", " : "") + "deliver";
88                 else
89                     ret += (needcomma ? ", " : "") + "sendto "+dest;
90                 needcomma = true;
91             }
92             if (tokenOut) { ret += (needcomma ? ", " : "") + "notify "+dest; needcomma = true; }
93             return ret;
94         }
95
96     }
97
98     public static class Literal extends Instruction {
99         public final Destination dest;
100         protected Literal(Destination dest) { this.dest = dest; }
101
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;
107             }
108         }
109
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;
118             }
119         }
120
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;
131             }
132         }
133     }
134 }