263c40ba9059cf702da754a34acb1cedee8b5360
[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 BenkoBox 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                           BenkoBox 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 += "[";
69                 if (count>1) ret += count;
70                 if (count==0) ret += "*";
71                 if (recycle) ret += "r";
72                 ret += "] ";
73             }
74             boolean needcomma = false;
75             if (tokenIn)           { ret += (needcomma ? ", " : "") + "wait";    needcomma = true; }
76             if (dataIn && latch)   { ret += (needcomma ? ", " : "") + "take";    needcomma = true; }
77             if (dataIn && !latch)  { ret += (needcomma ? ", " : "") + "discard"; needcomma = true; }
78             if (dataOut)  {
79                 if (benkoBox instanceof BenkoBox.Inbox || dest==null)
80                     ret += (needcomma ? ", " : "") + "deliver";
81                 else
82                     ret += (needcomma ? ", " : "") + "sendto "+dest;
83                 needcomma = true;
84             }
85             if (tokenOut) { ret += (needcomma ? ", " : "") + "ack "+dest; needcomma = true; }
86             return ret;
87         }
88
89     }
90
91     public static class Literal extends Instruction {
92         public final BenkoBox dest;
93         protected Literal(BenkoBox dest) { this.dest = dest; }
94
95         public static class Absolute extends Literal {
96             public final long value;
97             public Absolute(BenkoBox dest, long value) { super(dest); this.value = value; }
98             public String toString() {
99                 return value + ": sendto " + dest;
100             }
101         }
102
103         public static class Relative extends Literal {
104             /** value transmitted will be offset plus the address from which this instruction was loaded */
105             public final long offset;
106             public Relative(BenkoBox dest, long offset) { super(dest); this.offset = offset; }
107             public String toString() {
108                 String off = ""+offset;
109                 if (offset > 0) off = "+"+off;
110                 return "(@"+offset+"): sendto " + dest;
111             }
112         }
113
114         public static class CodeBagDescriptor extends Literal {
115             /** address of CBD, relative to address that this instruction was loaded from */
116             public final long offset;
117             public final long size;
118             public CodeBagDescriptor(BenkoBox dest, long offset, long size) {
119                 super(dest); this.offset = offset; this.size = size; }
120             public String toString() {
121                 String off = ""+offset;
122                 if (offset > 0) off = "+"+off;
123                 return "(@"+off+":"+size+"): sendto " + dest;
124             }
125         }
126     }
127 }