updated to AM14, AM15
[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 Kill(BenkoBox benkoBox, int count) { this.benkoBox=benkoBox; this.count=count; }
10         public String toString() { return (count>1 ? "["+count+"] " : "") + "kill"; }
11
12     }
13
14     public static class Executable extends Instruction {
15
16         public final BenkoBox benkoBox;
17         public final BenkoBox dest;
18         public final int      count;
19
20         public final boolean  tokenIn;
21         public final boolean  dataIn;
22         public final boolean  latch;
23         public final boolean  dataOut;
24         public final boolean  tokenOut;
25         public final boolean  recycle;
26
27         /** count=0 denotes a standing move */
28         public Executable(BenkoBox benkoBox,
29                           BenkoBox dest,
30                           int      count,
31                           boolean  tokenIn,
32                           boolean  dataIn,
33                           boolean  latch,
34                           boolean  dataOut,
35                           boolean  tokenOut,
36                           boolean  recycle) {
37             this.benkoBox = benkoBox;
38             this.dest = dest;
39             this.count = count;
40             this.tokenIn = tokenIn;
41             this.dataIn = dataIn;
42             this.latch = latch;
43             this.dataOut = dataOut;
44             this.tokenOut = tokenOut;
45             this.recycle = recycle;
46             if (count < 0)
47                 throw new RuntimeException("count field of an instruction must be >=0");
48         }
49
50         public Instruction.Executable decrementCount() {
51             if (count==1) return null;
52             return new Executable(benkoBox, dest, count==0 ? 0 : count-0, tokenIn, dataIn, latch, dataOut, tokenOut, recycle);
53         }
54
55         public String toString() {
56             String ret = "";
57             if (count==0 || count>1 || recycle) {
58                 ret += "[";
59                 if (count>1) ret += count;
60                 if (count==0) ret += "*";
61                 if (recycle) ret += "r";
62                 ret += "] ";
63             }
64             boolean needcomma = false;
65             if (tokenIn)           { ret += (needcomma ? ", " : "") + "wait";    needcomma = true; }
66             if (dataIn && latch)   { ret += (needcomma ? ", " : "") + "take";    needcomma = true; }
67             if (dataIn && !latch)  { ret += (needcomma ? ", " : "") + "discard"; needcomma = true; }
68             if (dataOut)  {
69                 if (benkoBox instanceof BenkoBox.Inbox || dest==null)
70                     ret += (needcomma ? ", " : "") + "deliver";
71                 else
72                     ret += (needcomma ? ", " : "") + "sendto "+dest;
73                 needcomma = true;
74             }
75             if (tokenOut) { ret += (needcomma ? ", " : "") + "ack "+dest; needcomma = true; }
76             return ret;
77         }
78
79     }
80
81     public static class Literal extends Instruction {
82         public final BenkoBox dest;
83         protected Literal(BenkoBox dest) { this.dest = dest; }
84
85         public static class Absolute extends Literal {
86             public final long value;
87             public Absolute(BenkoBox dest, long value) { super(dest); this.value = value; }
88             public String toString() { return value + ": sendto " + dest; }
89         }
90
91         public static class Relative extends Literal {
92             /** value transmitted will be offset plus the address from which this instruction was loaded */
93             public final long offset;
94             public Relative(BenkoBox dest, long offset) { super(dest); this.offset = offset; }
95             // FIXME: not final form!
96             public String toString() { return "(relative "+offset+"): sendto " + dest; }
97         }
98
99         public static class CodeBagDescriptor extends Literal {
100             /** address of CBD, relative to address that this instruction was loaded from */
101             public final long offset;
102             public final long size;
103             public CodeBagDescriptor(BenkoBox dest, long offset, long size) {
104                 super(dest); this.offset = offset; this.size = size; }
105             // FIXME: not final form!
106             public String toString() { return "(CBD "+offset+":"+size+"): sendto " + dest; }
107         }
108     }
109 }