-
Notifications
You must be signed in to change notification settings - Fork 0
/
goose.arch
386 lines (283 loc) · 13.1 KB
/
goose.arch
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
/*
<==============================O
| GOOSE ARCHITECTURE v4.0 |
| __ |
| >(' ) |
| )/ ~ |
| /(____/\ |
| / ) |
| \ ` =~~/ |
| `-----| |
| - - - ~~^ - - |
+------------------------------+
| ARCHITECTURE DEFINITION |
+------------------------------+
Word length: 16 bits
Class: Stack virtual machine
Memory regions: Code, constants, data, stack
Илья Бураков, вариант 6
*/
architecture Goose {
registers:
storage STACK_STORAGE[32]; /* A little-endian physical storage to serve for stack maintaining purposes: */
/* [0 .. SSZ .. 15 ][ 16 .. CSZ .. 31] */
storage SYSTEM_STORAGE[16]; /* A little-endian physical storage for system essential data: */
/* [0 .. IP .. 15] */
view SSZ = STACK_STORAGE[0..15]; /* A view for the current Stack Size storage */
view CSZ = STACK_STORAGE[16..31]; /* A view for the subroutine caller's stack Size storage at the entry point */
/* For "main" or "root" subroutine it's -1 */
view IP = SYSTEM_STORAGE[0..15]; /* A view for the current instruction's pointer storage */
memory:
range mCode[0x0000..0xFFFF] {
cell = 8;
endianess = little-endian;
granularity = 0;
}
range mConsts[0x0000..0xFFFF] {
cell = 8;
endianess = little-endian;
granularity = 0;
}
range mData[0x0000..0xFFFF] {
cell = 8;
endianess = little-endian;
granularity = 0;
}
range mStack[0x0000..0xFFFF] {
cell = 8;
endianess = little-endian;
granularity = 0;
}
instructions:
/* ---------------- TYPE ENCODINGS ---------------- */
encode imm8 field = immediate[8] data; /* generic data stored in 1 byte */
encode imm16 field = immediate[16] data; /* generic data stored in 2 bytes */
encode imm32 field = immediate[32] data; /* generic data stored in 4 bytes */
encode imm64 field = immediate[64] data; /* generic data stored in 8 bytes */
encode ofs16 field = immediate[16] offset; /* memory offset (maximum is 0xFFFF, what is the size of any memory range) */
/* BITS OF PARTS OF INSTRUCTION OPCODES DEFINITION */
/* For advanced description see ISA.txt */
encode efGroup field = cases {
system = 000,
stack = 111,
flowctrl = 101,
jmpif = 100,
arithmetic = 011,
input = 001,
output = 010
};
//<type: b/w/d/q>
encode efValType field = cases {
bt = 00,
wt = 01,
dt = 10,
qt = 11
};
encode esValType sequence = alternatives {
bt = { efValType.bt },
wt = { efValType.wt },
dt = { efValType.dt },
qt = { efValType.qt }
};
encode esValTypeAndNum sequence = alternatives {
bval = { efValType.bt, imm8 as num },
wval = { efValType.wt, imm16 as num },
dval = { efValType.dt, imm32 as num },
qval = { efValType.qt, imm64 as num }
};
encode esValTypeAndOfs sequence = alternatives {
bval = { efValType.bt, ofs16 as ofs },
wval = { efValType.wt, ofs16 as ofs },
dval = { efValType.dt, ofs16 as ofs },
qval = { efValType.qt, ofs16 as ofs }
};
encode efOfs field = cases {
deref = 0,
pop = 1
};
encode numOr0 sequence = alternatives {
defined = { imm8 as num },
default = { 0000 0000 }
};
/* -------------- STACK INSTRUCTIONS -------------- */
instruction push_plain = { efGroup.stack, 0, 1, 0, sequence esValTypeAndNum };
instruction push_by_ofs = { efGroup.stack, 0, 0, efOfs.deref, sequence esValTypeAndOfs };
instruction push_by_ofs_pop = { efGroup.stack, 0, 0, efOfs.pop, sequence esValType };
instruction pop_discard = { 1100 0010 };
instruction pop_by_ofs = { efGroup.stack, 1, 0, efOfs.deref, sequence esValTypeAndOfs};
instruction pop_by_ofs_pop = { efGroup.stack, 1, 0, efOfs.pop, sequence esValType};
instruction sav_by_ofs = { efGroup.stack, 1, 1, efOfs.deref, sequence esValTypeAndOfs};
instruction sav_by_ofs_pop = { efGroup.stack, 1, 1, efOfs.pop, sequence esValType};
instruction swp = { 1100 0000 };
instruction dpl = { 1100 0001, sequence numOr1 };
/* ----------- FLOW CONTROL INSTRUCTIONS ---------- */
encode esZero sequence = alternatives {
twoOpds = { 0 },
withZero = { 1 }
};
encode esComparator sequence = alternatives {
greater = { 00 },
greaterequal = { 01 },
less = { 10 },
lessequal = { 11 }
};
// Opcode construction may get really messy here and further.
// Check ISA.txt for resolving each field's meaning.
instruction jmp_if_plain = { efGroup.jmpif, 1, 0, sequence esZero, sequence esComparator, ofs16 as targetLabel };
instruction jmp_if_deref = { efGroup.jmpif, 0, efOfs.deref, sequence esZero, sequence esComparator, ofs16 as targetOfsVarPtr };
instruction jmp_if_pop = { efGroup.jmpif, 0, efOfs.pop, sequence esZero, sequence esComparator };
instruction jmp_if_equal_plain = { efGroup.flowctrl, 0 0, 1, 0, sequence esZero, ofs16 as targetLabel };
instruction jmp_if_equal_deref = { efGroup.flowctrl, 0 0, 0, efOfs.deref, sequence esZero, ofs16 as targetOfsVarPtr };
instruction jmp_if_equal_pop = { efGroup.flowctrl, 0 0, 0, efOfs.pop, sequence esZero };
instruction jmp_plain = { efGroup.flowctrl, 0 10, 1, 0, ofs16 as targetLabel };
instruction jmp_deref = { efGroup.flowctrl, 0 10, 0, efOfs.deref, ofs16 as targetOfsVarPtr };
instruction jmp_pop = { efGroup.flowctrl, 0 10, 0, efOfs.pop };
encode numOr0AndOfs sequence = alternatives {
num_not_defined = { 0000 0000, ofs16 as ofs },
defined = { imm8 as num, ofs16 as ofs }
};
encode numOr1 sequence = alternatives {
defined = { imm8 as num },
default = { 0000 0001 }
};
instruction call = { efGroup.flowctrl, 0 1100, sequence numOr0AndOfs }; // numOr0AndOfs(num as argsNumber, ofs as subroutineLabel) };
instruction ret = { efGroup.flowctrl, 0 1101, sequence numOr0 };
/* ------------ ARITHMETIC INSTRUCTIONS ----------- */
instruction add = { efGroup.arithmetic, 0 0000 };
instruction sub = { efGroup.arithmetic, 0 0001 };
instruction inc = { efGroup.arithmetic, 0 0010 };
instruction dec = { efGroup.arithmetic, 0 0011 };
instruction mlt = { efGroup.arithmetic, 0 0100 };
instruction div = { efGroup.arithmetic, 0 0101 };
instruction mod = { efGroup.arithmetic, 0 0110 };
/* ------------- BITWISE INSTRUCTIONS ------------- */
instruction band = { efGroup.arithmetic, 1 0111 };
instruction bor = { efGroup.arithmetic, 1 1000 };
instruction bxor = { efGroup.arithmetic, 1 1001 };
/* ----------- INPUT/OUTPUT INSTRUCTIONS ---------- */
encode efMode field = cases {
in = 0,
out = 1
};
instruction inp_deref = { efGroup.input, 0, 0, 1, sequence esValTypeAndOfs };
instruction inp_deref_char = { efGroup.input, 0, 1, 1, 00, ofs16 as ofs};
instruction inp_pop = { efGroup.input, 0, 0, 0, sequence esValType };
instruction inp_pop_char = { efGroup.input, 0, 1, 0, 00 };
instruction inp_stack = { efGroup.input, 1, 0, 0, sequence esValType };
instruction inp_stack_char = { efGroup.input, 1, 1, 000 };
instruction out_deref = { efGroup.output, 0, 0, 1, sequence esValTypeAndOfs };
instruction out_deref_char = { efGroup.output, 0, 1, 1, 00, ofs16 as ofs };
instruction out_pop = { efGroup.output, 0, 0, 0, sequence esValType };
instruction out_pop_char = { efGroup.output, 0, 1, 0, 00 };
instruction out_stack = { efGroup.output, 1, 0, 0, sequence esValType };
instruction out_stack_char = { efGroup.output, 1, 1, 000 };
/* -------------- OTHER INSTRUCTIONS -------------- */
instruction nop = { efGroup.system, 0 0000 };
instruction brk = { efGroup.system, 0 0001 };
instruction stop = { efGroup.system, 0 0010, sequence numOr0 };
instruction stop_ecstack = { efGroup.system, 0 0011 };
mnemonics:
/* --------- ARGUMENT FORMAT DEFINITIONS ---------- */
format arg_plain is "{1}";
format arg_dereference is "*{1}";
format arg_pop is "*(pop)";
format argValType of () "b" when bt,
() "w" when wt,
() "d" when dt,
() "q" when qt;
format argPlainNumAndVal of (num) "b {1}" when bval,
(num) "w {1}" when wval,
(num) "d {1}" when dval,
(num) "q {1}" when qval;
format argDerefOfsAndVal of (ofs) "b *{1}" when bval,
(ofs) "w *{1}" when wval,
(ofs) "d *{1}" when dval,
(ofs) "q *{1}" when qval;
format argPopOfsAndVal of () "b *(pop)" when bt,
() "w *(pop)" when wt,
() "d *(pop)" when dt,
() "q *(pop)" when qt;
format argCountDef1 of () when default,
(num) "{1}" when defined;
/* ------------ FOR STACK INSTRUCTIONS ------------- */
mnemonic push for push_plain (...) argPlainNumAndVal,
for push_by_ofs (...) argDerefOfsAndVal,
for push_by_ofs_pop (...) argPopOfsAndVal;
mnemonic pop for pop_by_ofs (...) argDerefOfsAndVal,
for pop_by_ofs_pop (...) argPopOfsAndVal,
for pop_discard ();
mnemonic sav for sav_by_ofs (...) argDerefOfsAndVal,
for sav_by_ofs_pop (...) argPopOfsAndVal;
mnemonic swp ();
mnemonic dpl (...) argCountDef1;
/* --------- FOR FLOW CONTROL INSTRUCTIONS --------- */
mnemonic jmp for jmp_plain (targetLabel) arg_plain,
for jmp_deref (targetOfsVarPtr) arg_dereference,
for jmp_pop () arg_pop;
mnemonic jez for jmp_if_equal_plain (targetLabel) arg_plain when withZero,
for jmp_if_equal_deref (targetOfsVarPtr) arg_dereference when withZero,
for jmp_if_equal_pop () arg_pop when withZero;
mnemonic je for jmp_if_equal_plain (targetLabel) arg_plain when twoOpds,
for jmp_if_equal_deref (targetOfsVarPtr) arg_dereference when twoOpds,
for jmp_if_equal_pop () arg_pop when twoOpds;
mnemonic jl for jmp_if_plain (targetLabel) arg_plain when twoOpds and less,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when twoOpds and less,
for jmp_if_pop () arg_pop when twoOpds and less;
mnemonic jlz for jmp_if_plain (targetLabel) arg_plain when withZero and less,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when withZero and less,
for jmp_if_pop () arg_pop when withZero and less;
mnemonic jg for jmp_if_plain (targetLabel) arg_plain when twoOpds and greater,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when twoOpds and greater,
for jmp_if_pop () arg_pop when twoOpds and greater;
mnemonic jgz for jmp_if_plain (targetLabel) arg_plain when withZero and greater,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when withZero and greater,
for jmp_if_pop () arg_pop when withZero and greater;
mnemonic jle for jmp_if_plain (targetLabel) arg_plain when twoOpds and lessequal,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when twoOpds and lessequal,
for jmp_if_pop () arg_pop when twoOpds and lessequal;
mnemonic jlez for jmp_if_plain (targetLabel) arg_plain when withZero and lessequal,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when withZero and lessequal,
for jmp_if_pop () arg_pop when withZero and lessequal;
mnemonic jge for jmp_if_plain (targetLabel) arg_plain when twoOpds and greaterequal,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when twoOpds and greaterequal,
for jmp_if_pop () arg_pop when twoOpds and greaterequal;
mnemonic jgez for jmp_if_plain (targetLabel) arg_plain when withZero and greaterequal,
for jmp_if_deref (targetOfsVarPtr) arg_dereference when withZero and greaterequal,
for jmp_if_pop () arg_pop when withZero and greaterequal;
mnemonic call (ofs) arg_plain when num_not_defined,
(ofs, num) "{1} {2}" when defined; // call with n args put onto subroutine's stack
mnemonic ret () when default,
(num) arg_plain when defined; // return with n top vals put onto caller's stack ("returned")
/* ---------- FOR ARITHMETIC INSTRUCTIONS ---------- */
mnemonic add();
mnemonic sub();
mnemonic inc();
mnemonic dec();
mnemonic mlt();
mnemonic div();
mnemonic mod();
/* ----------- FOR BITWISE INSTRUCTIONS ----------- */
mnemonic band();
mnemonic bor();
mnemonic bxor();
/* --------- FOR INPUT/OUTPUT INSTRUCTIONS -------- */
mnemonic inp for inp_deref (...) argDerefOfsAndVal,
for inp_deref_char (ofs) "c *{1}",
for inp_pop (...) argPopOfsAndVal,
for inp_pop_char () "c *(pop)",
for inp_stack (...) argValType,
for inp_stack_char () "c";
mnemonic out for out_deref (...) argDerefOfsAndVal,
for out_deref_char (ofs) "c *{1}",
for out_pop (...) argPopOfsAndVal,
for out_pop_char () "c *(pop)",
for out_stack (...) argValType,
for out_stack_char () "c";
/* ----------- FOR OTHER INSTRUCTIONS ------------- */
mnemonic nop ();
mnemonic stop (num) arg_plain when defined,
() when default,
for stop_ecstack () "*(pop)";
// cpy: copy value from constants to data
mnemonic brk ();
}