-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.ts
261 lines (230 loc) · 6.65 KB
/
runtime.ts
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
import { MachineProgramCompilation } from "./compiler.ts";
import { dump } from "./memory.ts";
import {
MachineOperation, add, dup, encodeOperation,
exit, peek, push, read, swap, write
} from "./operations.ts";
import { MachineExecutable } from "./system.ts";
export type RuntimeStruct = ReadonlyArray<readonly [string]>;
export type RuntimeEnum = [string, number][];
export const arrayStructure: RuntimeStruct = [
['length'],
];
export const valueStructure = [
['type'],
['value']
] as const;
// The "Runtime Structure" contains global information
// about code state: such as the allocator and
// referenceto to static data blocks
export const runtimeStructure = [
['allocationOffset'],
['lookupOffset'],
['dataOffset'],
['programOffset'],
] as const;
export const runtimeStackStructure = [
['lookupAddress'],
['dataAddress'],
] as const;
const calculateLookup = (blocks: number[][]) => {
let offset = 0;
const offsets: number[] = [];
for (const block of blocks) {
offsets.push(offset);
offset += block.length;
}
return offsets;
};
export const link = (
compilation: MachineProgramCompilation
): MachineExecutable => {
const data = compilation.dataBlocks.flat(1);
const lookup = calculateLookup(compilation.dataBlocks);
const program = compilation.operations.map(encodeOperation).flat(1);
const companionStructures = [
data,
lookup,
program,
];
const companionBytes = companionStructures.flat(1);
const [
dataOffset,
lookupOffset,
programOffset,
] = calculateLookup(companionStructures);
const runtimeBytesLength = getStructLength(runtimeStructure);
const programAddress = data.length + lookup.length + 1;
const runtime = getStructBytes(runtimeStructure, {
programOffset: programOffset - companionBytes.length,
lookupOffset: lookupOffset - companionBytes.length,
dataOffset: dataOffset - companionBytes.length,
allocationOffset: runtimeBytesLength
})
const memory = [
// first byte is "runtime address"
companionBytes.length + 1,
companionBytes,
runtime,
Array.from({ length: 1024 * 1024 }).map(_ => 0),
].flat(1);
return { memory, entry: programAddress }
};
export const getStructFieldIndex = (structure: RuntimeStruct, fieldName: string): number => {
const index = structure.findIndex(([stuctureFieldName]) => stuctureFieldName === fieldName);
if (index === -1)
throw new Error();
return index;
}
/** stack: [] => [length] */
export const encodeTypeLength = (structure: RuntimeStruct): MachineOperation[] => {
return [
push(structure.length)
].flat(1);
};
/** stack: [byteCount, runtimeAddress] => [allocatedAddress] */
export const encodeAllocate = (): MachineOperation[] => {
return [
dup(),
encodeReadStructField(runtimeStructure, 'allocationOffset'),
// [byteCount, runtimeAddress, allocOffset]
dup(),
push(3),
peek(),
add(),
// [byteCount, runtimeAddress, allocOffset, nextAllocOffset]
push(2),
peek(),
encodeWriteStructField(runtimeStructure, 'allocationOffset'),
// [byteCount, runtimeAddress, allocOffset]
add(),
swap(),
encodeDiscard(),
// [allocOffset]
].flat(1);
}
/** stack: [] => [address] */
export const encodeReadData = (runtimeAddress: number, dataIndex: number): MachineOperation[] => {
return [
push(runtimeAddress),
encodeReadStructField(runtimeStructure, 'dataBlocks'),
push(dataIndex),
encodeReadArrayIndex(),
].flat(1);
}
/** stack: [structAddress] => [value] */
export const encodeReadStructField = (structure: RuntimeStruct, fieldName: string): MachineOperation[] => {
const index = getStructFieldIndex(structure, fieldName);
return [
push(index),
add(),
read(),
].flat(1);
}
/** stack: [value, structAddress] => [] */
export const encodeWriteStructField = (structure: RuntimeStruct, fieldName: string): MachineOperation[] => {
const index = getStructFieldIndex(structure, fieldName);
return [
push(index),
add(),
swap(),
write(),
].flat(1);
}
/** stack: [arrayAddress, index] => [value] */
export const encodeReadArrayIndex = (): MachineOperation[] => {
return [
add(),
read(),
].flat(1);
};
/** stack: [value, arrayAddress, index] => [] */
export const encodeWriteArrayIndex = (): MachineOperation[] => {
return [
add(),
swap(),
write(),
]
};
/** stack: [stackOffset] => [value] */
export const encodeReadStackStructureField = (
structure: RuntimeStruct,
fieldName: string
): MachineOperation[] => {
const index = getStructFieldIndex(structure, fieldName);
const length = structure.length - 1;
return [
push(length - index),
add(),
peek(),
].flat(1);
};
/*:: stack: [] => [...structValues] */
export const encodePushStackStructure = (
structure: RuntimeStruct,
structureData: { [key in string]: number }
): MachineOperation[] => {
const bytes = getStructBytes(structure, structureData)
return [
bytes.map(byte => push(byte)),
].flat(1);
}
export const getStructBytes = <T extends RuntimeStruct>(
structure: T,
structureData: { [key in T[number][0]]: number }
): number[] => {
return structure.map(([field]) => structureData[(field as T[number][0])] || 0);
}
export const getStructLength = (structure: RuntimeStruct) => {
return structure.length;
}
/** stack: [programstart] => [runtimestructaddress] */
export const encodeRuntimeInitialization = (): MachineOperation[] => {
return [
dup(), // [programstart, programstart]
read(), // [programstart, runtimestructoffset]
add(),
// [runtimestructaddress]
encodePushStackStruct(runtimeStackStructure, {
lookupAddress: (depth) => [
push(depth),
peek(),
dup(),
encodeReadStructField(runtimeStructure, 'lookupOffset'),
add(),
].flat(1),
dataAddress: (depth) => [
push(depth),
peek(),
dup(),
encodeReadStructField(runtimeStructure, 'dataOffset'),
add(),
].flat(1)
}),
].flat(1);
}
/**:: [] => */
export const encodePushStackStruct = <T extends RuntimeStruct>(
structure: T,
loadField: { [key in T[number][0]]: (depth: number) => MachineOperation[] },
): MachineOperation[] => {
return [
structure
.map(([fieldname], index) =>
loadField[fieldname as T[number][0]](index)
)
.flat(1)
].flat(1);
}
/** stack: [dataIndex] => [dataAddress] */
export const encodeLoadDataAddress = (runtimeDepth: number): MachineOperation[] => {
return [
push(runtimeDepth),
encodeReadStackStructureField(runtimeStackStructure, 'lookupAddress'),
add(),
read(),
push(runtimeDepth),
encodeReadStackStructureField(runtimeStackStructure, 'dataAddress'),
add(),
].flat(1);
};