added dataOutDest
[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  dataOutDest;
31         public final boolean  dataOut;
32         public final boolean  tokenOut;
33         public final boolean  requeue;
34
35         /** count=0 denotes a standing move */
36         public Executable(BenkoBox    benkoBox,
37                           Destination dest,
38                           int         count,
39                           boolean     tokenIn,
40                           boolean     dataIn,
41                           boolean     latch,
42                           boolean     dataOutDest,
43                           boolean     dataOut,
44                           boolean     tokenOut,
45                           boolean     requeue) {
46             this.benkoBox = benkoBox;
47             this.dest = dest;
48             this.count = count;
49             this.tokenIn = tokenIn;
50             this.dataIn = dataIn;
51             this.latch = latch;
52             this.dataOutDest = dataOutDest;
53             this.dataOut = dataOut;
54             this.tokenOut = tokenOut;
55             this.requeue = requeue;
56             if (count < 0)
57                 throw new RuntimeException("count field of an instruction must be >=0");
58         }
59
60         public boolean isStanding() {
61             return count==0;
62         }
63
64         public Instruction.Executable decrementCount() {
65             if (count==1) return null;
66             return new Executable(benkoBox, dest, count==0 ? 0 : count-1,
67                                   tokenIn, dataIn, latch, dataOutDest, dataOut, tokenOut, requeue);
68         }
69
70         public String toString() {
71             String ret = benkoBox.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 ? ")" : "] ";
77             }
78             boolean needcomma = false;
79             if (tokenIn)           { ret += (needcomma ? ", " : "") + "wait";    needcomma = true; }
80             if (dataIn && latch)  {
81                 if (benkoBox.isInbox())
82                     ret += (needcomma ? ", " : "") + "receive";
83                 else
84                     ret += (needcomma ? ", " : "") + "take";
85                 needcomma = true;
86             }
87             if (dataIn && !latch)  { ret += (needcomma ? ", " : "") + "dismiss"; needcomma = true; }
88             if (dataOut)  {
89                 if (benkoBox.isInbox() || dest==null)
90                     ret += (needcomma ? ", " : "") + "deliver";
91                 else
92                     ret += (needcomma ? ", " : "") + "sendto "+dest;
93                 needcomma = true;
94             }
95             if (tokenOut) { ret += (needcomma ? ", " : "") + "notify "+dest; needcomma = true; }
96             return ret;
97         }
98
99     }
100
101     public static class Literal extends Instruction {
102         public final Destination dest;
103         protected Literal(Destination dest) { this.dest = dest; }
104
105         public static class Absolute extends Literal {
106             public final long value;
107             public Absolute(Destination dest, long value) { super(dest); this.value = value; }
108             public String toString() {
109                 return value + ": sendto " + dest;
110             }
111         }
112
113         public static class Relative extends Literal {
114             /** value transmitted will be offset plus the address from which this instruction was loaded */
115             public final long offset;
116             public Relative(Destination dest, long offset) { super(dest); this.offset = offset; }
117             public String toString() {
118                 String off = ""+offset;
119                 if (offset > 0) off = "+"+off;
120                 return "(@"+offset+"): sendto " + dest;
121             }
122         }
123
124         public static class CodeBagDescriptor extends Literal {
125             /** address of CBD, relative to address that this instruction was loaded from */
126             public final long offset;
127             public final long size;
128             public CodeBagDescriptor(Destination dest, long offset, long size) {
129                 super(dest); this.offset = offset; this.size = size; }
130             public String toString() {
131                 String off = ""+offset;
132                 if (offset > 0) off = "+"+off;
133                 return "(CBD @"+off+"+"+size+"): sendto " + dest;
134             }
135         }
136     }
137 }