-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathVariable.java
157 lines (139 loc) · 3.43 KB
/
Variable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.*;
class Variable {
String name;
Integer address; // compiled storage address
Object value; // initialized value
Map<String, Variable> variables;
List<String> args = new ArrayList<String>();
public Variable(String line, Map<String, Variable> variables) {
// name [(arg [, arg2, arg3 ... ] ) ]
this.variables = variables;
Cursor cursor = new Cursor(line);
name = cursor.parseVarName();
if (cursor.at('(')) {
Cursor subcursor = cursor.parseParens();
while(true) {
args.add(subcursor.parseExpression());
if (subcursor.done()) { break; }
subcursor.expect(',');
}
}
if (!variables.containsKey(name)) {
variables.put(name, this);
}
else if (variables.get(name).args.size() != args.size()) {
throw new Error("Inconsistent variable subscripting.");
}
else if (args.size() > 3) {
throw new Error("Variables may have no more than three subscripts.");
}
// these temporary assignments will probably be
// blown away by a dimension statement later:
if (args.size() == 0) { value = 0; }
else if (args.size() == 1) { value = new int[1]; }
else if (args.size() == 2) { value = new int[1][1]; }
else if (args.size() == 3) { value = new int[1][1][1]; }
}
public void emitStorage(MakoRom rom) {
if (address != null) { return; }
address = rom.size();
rom.label(name, address);
if (value instanceof Integer) {
rom.add((Integer)value, MakoRom.Type.Data);
}
else if (value instanceof int[][][]) {
for(int[][] m : (int[][][])value) {
for(int[] r : m) {
for(int i : r) {
rom.add(i, MakoRom.Type.Array);
}
}
}
}
else if (value instanceof int[][]) {
for(int[] r : (int[][])value) {
for(int i : r) {
rom.add(i, MakoRom.Type.Array);
}
}
}
else if (value instanceof int[]) {
for(int i : (int[])value) {
rom.add(i, MakoRom.Type.Array);
}
}
}
public void emit(MakoRom rom) {
// make sure the base address of this variable
// reference points to the canonical instance.
if (variables.get(name) != this) {
address = variables.get(name).address;
value = variables.get(name).value;
}
if (value instanceof Integer) {
rom.addConst(address);
}
else if (value instanceof int[][][]) {
int[][][] v3 = (int[][][])value;
new Expression(
String.format("((%s) x %d) + ((%s) x %d) + (%s) + %s",
args.get(0),
v3[0].length * v3[0][0].length,
args.get(1),
v3[0][0].length,
args.get(2),
address
),
variables
).emit(rom);
/*
args.get(0).emit(rom);
rom.addConst(v3[0].length * v3[0][0].length);
rom.addMul();
args.get(1).emit(rom);
rom.addConst(v3[0][0].length);
rom.addMul();
rom.addAdd();
args.get(2).emit(rom);
rom.addAdd();
rom.addConst(address);
rom.addAdd();
*/
}
else if (value instanceof int[][]) {
int[][] v2 = (int[][])value;
new Expression(
String.format("((%s) x %s) + (%s) + %s",
args.get(0),
v2[0].length,
args.get(1),
address
),
variables
).emit(rom);
/*
args.get(0).emit(rom);
rom.addConst(v2[0].length);
rom.addMul();
args.get(1).emit(rom);
rom.addAdd();
rom.addConst(address);
rom.addAdd();
*/
}
else if (value instanceof int[]) {
new Expression(
String.format("(%s) + %s",
args.get(0),
address
),
variables
).emit(rom);
/*
args.get(0).emit(rom);
rom.addConst(address);
rom.addAdd();
*/
}
}
}