-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mill.h
362 lines (333 loc) · 9.37 KB
/
Mill.h
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Copyright (c) 2019, Thomas DiModica
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the names of the copyright holders nor the names of other
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// If you include this twice, bad things will happen and you deserve it.
#include <map>
#include <deque>
#include <memory>
#include <vector>
#include <cstdio>
class Machine; // Forward declaration for Op.
// Abstract base class for all operations.
class Op
{
public:
virtual void perform(Machine&) = 0;
};
const size_t BELT_SIZE = 64U;
// Enum to define when we are addressing the belt vs when we are using an immediate.
enum BELT_LOCATION
{
B1,
B2,
B3,
B4,
B5,
B6,
B7,
B8,
B9,
B10,
B11,
B12,
B13,
B14,
B15,
B16,
B17,
B18,
B19,
B20,
B21,
B22,
B23,
B24,
B25,
B26,
B27,
B28,
B29,
B30,
B31,
B32,
B33,
B34,
B35,
B36,
B37,
B38,
B39,
B40,
B41,
B42,
B43,
B44,
B45,
B46,
B47,
B48,
B49,
B50,
B51,
B52,
B53,
B54,
B55,
B56,
B57,
B58,
B59,
B60,
B61,
B62,
B63,
B64
};
enum NONE_TYPE
{
NONE_VAL = 1
};
enum NAR_TYPE
{
NAR_VAL = 2
};
enum BELT_TYPE
{
WORD
};
struct BELT_T
{
union
{
int w;
};
int length;
int narflags;
BELT_TYPE type;
BELT_T() : w(0), length(0), narflags(0), type (WORD) { }
BELT_T(int w) : w(w), length(0), narflags(0), type(WORD) { }
BELT_T(NONE_TYPE) : w(0), length(0), narflags(NONE_VAL), type(WORD) { }
BELT_T(NAR_TYPE) : w(0), length(0), narflags(NAR_VAL), type(WORD) { }
};
enum FRAME_TYPE
{
CALL_FRAME, // Associated BFrame
LOOP_FRAME // No associated BFrame
};
class IFrame
{
public:
size_t pc; // Index into the instruction array to the current instruction.
size_t ip; // Index into the operation array to the current operation.
int flags; // The current set flags in the operation gang.
int c; // The number of instructions in the future that those flags will appear.
FRAME_TYPE type;
IFrame() : pc(0U), ip(0U), flags(0), c(0), type(CALL_FRAME) { }
};
class BFrame
{
public:
std::deque<BELT_T> belt;
std::deque<std::deque<BELT_T> > retire;
std::deque<std::vector<std::pair<size_t, BELT_T> > > loads; // Loads have a latency of 3 and are alias safe.
BELT_T getLoc(BELT_LOCATION loc)
{
if (loc > belt.size())
{
return NAR_VAL;
}
else
{
return belt[belt.size() - 1 - loc];
}
}
};
class Machine
{
private:
std::map<std::string, size_t> labels; // The instruction that label refers to.
std::map<std::string, std::vector<size_t*> > prereferences; // References to labels that haven't been defined yet.
public:
std::vector<std::vector<std::unique_ptr<Op> > > instructions;
std::vector<IFrame> stack;
std::vector<BFrame> belt;
bool faultFlag;
std::map<std::string, BELT_T> loads; // BELT_T is location to read. Read is on pickup.
int * memory;
// Builder functions : these all return the machine that is being operated on.
// At run time, these functions compile the genAsm into something that will execute.
// Each function creates an operation and performs instructions.back().push_back().
Machine& add (BELT_LOCATION, BELT_LOCATION);
Machine& add (BELT_LOCATION, BELT_T);
Machine& br (BELT_LOCATION);
Machine& con (BELT_T);
Machine& fault ();
Machine& innertr (BELT_LOCATION pred, int drops, BELT_LOCATION dest);
Machine& load (BELT_LOCATION);
Machine& load (BELT_LOCATION, int latency);
Machine& load (BELT_LOCATION, const std::string& tag);
Machine& pick (BELT_LOCATION pred, BELT_LOCATION);
Machine& pick (BELT_LOCATION pred, BELT_LOCATION, BELT_LOCATION);
Machine& pickup (const std::string& tag);
Machine& recur (BELT_LOCATION pred, BELT_LOCATION, BELT_LOCATION); // Pick that looks at NaR
Machine& refuse (const std::string& tag);
Machine& retnfl (BELT_LOCATION pred);
Machine& store (BELT_LOCATION, BELT_LOCATION);
Machine& sub (BELT_LOCATION, BELT_LOCATION);
Machine& sub (BELT_LOCATION, BELT_T);
// Create the next instruction to be built.
Machine& next()
{
instructions.push_back(std::vector<std::unique_ptr<Op> >());
return *this;
}
// Define a label. Defining a label implicitly calls next().
Machine& label(const std::string&);
// Run the code.
void run()
{
stack.push_back(IFrame());
belt.push_back(BFrame());
faultFlag = false;
while ((stack.back().pc < instructions.size()) && (false == faultFlag))
{
// Note 1 : the instruction is completed in the event of a fault.
// Note 2 : branches work because branches are ALWAYS to labels,
// and labels are ALWAYS preceded by an instructions whose only operation is fault.
// (This prevents un-Mill-like falling off of an EBB.)
while ((stack.back().ip < instructions[stack.back().pc].size()))
{
const std::unique_ptr<Op>& current = instructions[stack.back().pc][stack.back().ip];
++stack.back().ip;
current->perform(*this);
}
stack.back().ip = 0U;
++stack.back().pc;
if ((false == belt.back().loads.empty()) && (false == belt.back().loads[0].empty()))
{
if (true == belt.back().retire.empty())
{
belt.back().retire.push_back(std::deque<BELT_T>());
}
for (auto load : belt.back().loads[0])
{
belt.back().retire.front()[load.first] = memory[load.second.w];
}
}
if (false == belt.back().retire.empty())
{
belt.back().belt.insert(belt.back().belt.end(), belt.back().retire.front().begin(), belt.back().retire.front().end());
belt.back().retire.pop_front();
while (belt.back().belt.size() > BELT_SIZE)
{
belt.back().belt.pop_front();
}
/*
int loca = 1;
for (auto loc : belt.back().belt)
{
std::printf("Belt at %d is %d\n", loca++, loc.w);
}
*/
}
}
}
};
class add_op : public Op
{
public:
BELT_LOCATION lhs, rhs;
void perform(Machine& m)
{
if (true == m.belt.back().retire.empty())
{
m.belt.back().retire.push_back(std::deque<BELT_T>());
}
// The instruction-level polymorphism goes here.
m.belt.back().retire.front().push_back(m.belt.back().getLoc(lhs).w + m.belt.back().getLoc(rhs).w);
}
};
class addi_op : public Op
{
public:
BELT_LOCATION lhs;
BELT_T rhs;
void perform(Machine& m)
{
if (true == m.belt.back().retire.empty())
{
m.belt.back().retire.push_back(std::deque<BELT_T>());
}
// The instruction-level polymorphism goes here.
m.belt.back().retire.front().push_back(m.belt.back().getLoc(lhs).w + rhs.w);
}
};
class con_op : public Op
{
public:
BELT_T value;
void perform(Machine& m)
{
//std::printf("Putting %d on belt...\n", value.w);
if (true == m.belt.back().retire.empty())
{
m.belt.back().retire.push_back(std::deque<BELT_T>());
}
// The instruction-level polymorphism goes here.
m.belt.back().retire.front().push_back(value);
}
};
class store_op : public Op
{
public:
BELT_LOCATION location, value;
void perform(Machine& m)
{
// The instruction-level polymorphism goes here.
int loc = m.belt.back().getLoc(location).w;
int val = m.belt.back().getLoc(value).w;
//std::printf("Storing (%d) %d at (%d) %d...\n", value, val, location, loc);
m.memory[loc] = val;
if (255 == loc)
{
std::putchar(val);
}
}
};
Machine& Machine::con (BELT_T val)
{
std::unique_ptr<con_op> op = std::make_unique<con_op>();
op->value = val;
instructions.back().push_back(std::move(op));
return *this;
}
Machine& Machine::store (BELT_LOCATION loc, BELT_LOCATION val)
{
std::unique_ptr<store_op> op = std::make_unique<store_op>();
op->location = loc;
op->value = val;
instructions.back().push_back(std::move(op));
return *this;
}