-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinstruction_def.hpp
429 lines (369 loc) · 12.9 KB
/
instruction_def.hpp
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* @file instruction_def.hpp
* @author paul
* @date 27.05.20
* Declaration of the instructions and the corresponding concept.
*/
#ifndef TEMPLATE_CPU_INSTRUCTION_DEF_HPP
#define TEMPLATE_CPU_INSTRUCTION_DEF_HPP
#include "config.hpp"
/**
* Addition Instruction: the register res is set to the value of the register a plus the value of register b
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct Add {};
/**
* Addition with immediate Instruction: the register res is set to the value of the register a plus the value b
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct AddI {};
/**
* Subtraction Instruction: the register res is set to the value of the register a minus the register b
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct Sub {};
/**
* Subtraction with immediate Instruction: the register res is set to the value of the register a minus the value b
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct SubI {};
/**
* Multiplication Instruction: the register res is set to the value of the register a times the register b
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct Mul {};
/**
* Multiplication with immediate Instruction: the register res is set to the value of the register a times the value b
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct MulI {};
/**
* Division Instruction: the register res is set to the value of the register a divided the register b.
* If the value of the register b is zero a static_assert is triggered and the execution is stopped.
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct Div {};
/**
* Division with immediate Instruction: the register res is set to the value of the register a divided by the value b.
* If the b is zero a static_assert is triggered and the execution is stopped.
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct DivI {};
/**
* Binary-And Instruction: the register res is set to the value of the binary and of the registers a and b.
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct And {};
/**
* Binary-And with immediate Instruction: the register res is set to the value of the binary and of the register a and
* the value b.
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct AndI {};
/**
* Binary-Or Instruction: the register res is set to the value of the binary or of the registers a and b.
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct Or {};
/**
* Binary-Or with immediate Instruction: the register res is set to the value of the binary or of the register a and
* the value b.
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct OrI {};
/**
* Binary-Exclusive-Or Instruction: the register res is set to the value of the binary xor of the registers a and b.
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct XOr {};
/**
* Binary-Exclusive Or with immediate Instruction: the register res is set to the value of the binary or of the register
* a and the value b.
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct XOrI {};
/**
* Less Instruction: the register res is set to 1 of the value in register a is less than the one in register b.
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct Less {};
/**
* Less with immediate Instruction: the register res is set to 1 of the value in register a is less than the value b.
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct LessI {};
/**
* Greater Instruction: the register res is set to 1 of the value in register a is greater than the one in register b.
* @tparam res result register
* @tparam a register of first operand
* @tparam b register of second operand
*/
template<Register res, Register a, Register b>
struct Greater {};
/**
* Less with immediate Instruction: the register res is set to 1 of the value in register a is greater than the value b.
* @tparam T the type of b
* @tparam res the result register
* @tparam a register of first operand
* @tparam b value of the second operand
*/
template<typename T, Register res, Register a, T b>
struct GreaterI {};
/**
* Jump Instruction: sets the program counter to the value in register reg
* @tparam reg contains the next program counter
*/
template<Register reg>
struct Jump {};
/**
* Jump with immediate Instruction: jumps to a fixed program counter address given by val
* @tparam T the type of val
* @tparam val the next program counter.
*/
template<typename T, T val>
struct JumpI{};
/**
* Branch on equal Instruction: jumps if the values in register a and b are equal.
* @tparam a the first operand of the comparison
* @tparam b the second operand of the comparison
* @tparam target the register containing the program counter value at which to jump if the values are equal.
*/
template<Register a, Register b, Register target>
struct BranchEq {};
/**
* Branch on equal with immediate Instruction: jumps if the values in register a and the value b are equal.
* @tparam T the type of the target program counter
* @tparam a the first operand of the comparison
* @tparam b the second operand of the comparison
* @tparam target the program counter value at which to jump if the values are equal.
*/
template<typename T, Register a, Register b, T target>
struct BranchEqI {};
/**
* Branch on not-equal Instruction: jumps if the values in register a and b are not equal.
* @tparam a the first operand of the comparison
* @tparam b the second operand of the comparison
* @tparam target the register containing the program counter value at which to jump if the values are not equal.
*/
template<Register a, Register b, Register target>
struct BranchNEq {};
/**
* Branch on not-equal with immediate Instruction: jumps if the values in register a and the value b are not equal.
* @tparam T the type of the target program counter
* @tparam a the first operand of the comparison
* @tparam b the second operand of the comparison
* @tparam target the program counter value at which to jump if the values are not equal.
*/
template<typename T, Register a, Register b, T target>
struct BranchNEqI {};
/**
* Store Instruction: Stores the value from a register into memory.
* @tparam addr_reg the register containing the address to write to
* @tparam reg the register containing the value to write
*/
template<Register addr_reg, Register reg>
struct Store {};
/**
* Store with immediate Instruction: Stores a value from register into memory.
* @tparam addr the address at which to write
* @tparam reg the register containing the value
*/
template<mem_ptr_type addr, Register reg>
struct StoreI {};
/**
* Load Instruction: Loads a value from memory into a register.
* @tparam reg the register in which to write
* @tparam addr_reg the register containing the address to read from
*/
template<Register reg, Register addr_reg>
struct Load {};
/**
* Load with immediate Instruction: Loads a value from memory into a register.
* @tparam reg the register in which to write
* @tparam addr_reg the address to read from
*/
template<Register reg, mem_ptr_type addr>
struct LoadI {};
/**
* Struct used for defining if a type is an Instruction.
* @tparam T the type to check
*/
template<typename T>
struct is_instruction {
static constexpr bool val = false;
};
template<Register res, Register a, Register b>
struct is_instruction<Add<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<AddI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<Sub<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<SubI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<Mul<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<MulI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<Div<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<DivI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<And<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<AndI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<Or<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<OrI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<XOr<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<XOrI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<Less<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<LessI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register res, Register a, Register b>
struct is_instruction<Greater<res, a, b>> {
static constexpr bool val = true;
};
template<typename T, Register res, Register a, T b>
struct is_instruction<GreaterI<T, res, a, b>> {
static constexpr bool val = true;
};
template<Register a, Register b, Register target>
struct is_instruction<BranchEq<a, b, target>> {
static constexpr bool val = true;
};
template<typename T, Register a, Register b, T target>
struct is_instruction<BranchEqI<T, a, b, target>> {
static constexpr bool val = true;
};
template<Register a, Register b, Register target>
struct is_instruction<BranchNEq<a, b, target>> {
static constexpr bool val = true;
};
template<typename T, Register a, Register b, T target>
struct is_instruction<BranchNEqI<T, a, b, target>> {
static constexpr bool val = true;
};
template<mem_ptr_type addr, Register reg>
struct is_instruction<StoreI<addr, reg>> {
static constexpr bool val = true;
};
template<Register reg, mem_ptr_type addr>
struct is_instruction<LoadI<reg, addr>> {
static constexpr bool val = true;
};
template<Register addr, Register reg>
struct is_instruction<Store<addr, reg>> {
static constexpr bool val = true;
};
template<Register reg, Register addr>
struct is_instruction<Load<reg, addr>> {
static constexpr bool val = true;
};
template<Register reg>
struct is_instruction<Jump<reg>> {
static constexpr bool val = true;
};
template<typename T, T reg>
struct is_instruction<JumpI<T, reg>> {
static constexpr bool val = true;
};
/**
* Concept declaring an Instruction, that is one of the types listed above.
* @tparam T the type to check
*/
template<typename T>
concept Instruction = is_instruction<T>::val;
#endif //TEMPLATE_CPU_INSTRUCTION_DEF_HPP