-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend-utils.ts
230 lines (214 loc) · 7.29 KB
/
backend-utils.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
import debug from './util/debug';
import { Type } from './types';
import { StringLiteralData, Backend, Variable } from './api';
import { Statement } from './threeAddressCode/Statement';
import { Statement as TargetStatement } from './targetCode/Statement';
import { Program } from './threeAddressCode/Program';
import { RegisterAgnosticTargetInfo, TargetInfo, TargetRegisters } from './TargetInfo';
import { toTarget } from './targetCode/Function';
import { StackUsage, stackUsageToString } from './targetCode/StackUsage';
import { Register } from './threeAddressCode/Register';
import join from './util/join';
import mipsBackend from './backends/mips';
import jsBackend from './backends/js';
import cBackend from './backends/c';
import x64Backend from './backends/x64';
export const preceedingWhitespace = <TargetRegister>(
tas: TargetStatement<TargetRegister>
): string => {
switch (tas.kind) {
case 'label':
return '';
case 'functionLabel':
return '\n\n';
default:
return ' ';
}
};
export type CompiledExpression<T> = {
prepare: T[];
execute: T[];
cleanup: T[];
};
export type CompiledAssignment<T> = {
prepare: T[];
execute: T[];
cleanup: T[];
};
export type CompiledProgram<T> = {
prepare: T[];
execute: T[];
cleanup: T[];
};
type ExpressionCompiler<T> = (expressions: T[][]) => T[];
export const compileExpression = <T>(
subExpressions: CompiledExpression<T>[],
expressionCompiler: ExpressionCompiler<T>
): CompiledExpression<T> => ({
prepare: subExpressions.map(input => input.prepare).flat(),
execute: expressionCompiler(subExpressions.map(input => input.execute)),
cleanup: subExpressions
.reverse()
.map(input => input.cleanup)
.flat(),
});
export const stringLiteralName = ({ id, value }: StringLiteralData) =>
`string_literal_${id}_${value.replace(/[^a-zA-Z]/g, '')}`;
export type RegisterAssignment<TargetRegister> = {
registerMap: { [key: string]: TargetRegister };
spilled: string[];
};
export const saveFunctionCallResult = <TargetRegister>(
destination: Register | null,
getRegister: (r: Register) => TargetRegister,
registers: TargetRegisters<TargetRegister>
): TargetStatement<TargetRegister>[] => {
if (!destination) {
return [];
}
return [
{
kind: 'move',
from: registers.functionResult,
to: getRegister(destination),
why: 'save result',
},
];
};
export type TranslatedFunction = {
name?: string; // Only main may not have a name
instructions: string[];
stackUsage: StackUsage<string>; // Done because the whole point of TranslatedFunction is to not template on the register type. TODO: don't make register type a template
};
export type Executable = {
main: TranslatedFunction;
functions: TranslatedFunction[];
};
const functionToString = (
commentChar: string,
{ name, instructions, stackUsage }: TranslatedFunction
): string => {
if (!name) debug('no name here');
return `
${name}: ${commentChar} stack: ${stackUsageToString(stackUsage)}
${join(instructions, '\n')}`;
};
export const executableToString = (
commentChar: string,
{ main, functions }: Executable
): string => {
// Main needs to be first for MARS, which just executes from the top of the file
return `
${functionToString(commentChar, main)}
${join(
functions.map(f => functionToString(commentChar, f)),
'\n'
)}`;
};
export const makeExecutable = <TargetRegister>(
{ functions, main }: Program,
{ syscallNumbers }: RegisterAgnosticTargetInfo,
targetRegisterInfo: TargetInfo<TargetRegister>,
translator,
includeCleanup: boolean
): Executable => {
if (!main) throw debug('no main');
const targetFunctions: any = [];
functions.forEach((f, name) =>
targetFunctions.push(
toTarget({
name,
threeAddressFunction: f,
targetInfo: targetRegisterInfo,
finalCleanup: [{ kind: 'return', why: 'The Final Return!' }],
isMain: false,
})
)
);
const targetMain = toTarget({
name: 'builtin_main',
threeAddressFunction: main,
targetInfo: targetRegisterInfo,
finalCleanup: [
// TODO: push/pop exit code is jank and should be removed.
{
kind: 'push',
register: targetRegisterInfo.registers.functionResult,
why: "Need to save exit code so it isn't clobbber by free_globals/verify_no_leaks",
},
...(includeCleanup
? [
{
kind: 'callByName' as 'callByName',
function: 'free_globals',
why: 'free_globals',
},
{
kind: 'callByName' as 'callByName',
function: 'verify_no_leaks',
why: 'verify_no_leaks',
},
]
: []),
{
kind: 'pop' as 'pop',
register: targetRegisterInfo.registers.syscallArgument[0],
why: 'restore exit code',
},
{
kind: 'loadImmediate' as 'loadImmediate',
destination: targetRegisterInfo.registers.syscallSelectAndResult,
value: syscallNumbers.exit,
why: 'prepare to exit',
},
{ kind: 'syscall' as 'syscall', why: 'exit' },
],
isMain: true,
});
return {
main: {
instructions: targetMain.instructions.map(translator).flat() as any,
stackUsage: targetMain.stackUsage as any,
},
functions: targetFunctions.map(({ name, instructions, stackUsage }) => ({
name,
stackUsage,
instructions: instructions.map(translator).flat(),
})) as any,
};
};
// TODO: Move map to outside?
export const freeGlobalsInstructions = (
globals: Variable[],
makeTemporary,
globalNameMap
): Statement[] => {
const instructions: Statement[] = globals
.filter(declaration => ['String', 'List'].includes((declaration.type as Type).type.kind))
.map(declaration => {
const globalStringAddress = makeTemporary('gobalStringAddress');
return [
{
kind: 'loadGlobal',
from: globalNameMap[declaration.name].newName,
to: globalStringAddress,
why: 'Load global string so we can free it',
},
{
kind: 'callByName',
function: 'my_free',
arguments: [globalStringAddress],
destination: null,
why: 'Free global string at end of program',
},
];
})
.flat() as any;
instructions.push({
kind: 'return' as 'return',
register: new Register('dummyReturn'),
why: 'Need to not have an empty function, otherwise verifyingOverlappingJoin fails. TODO: fix that.',
});
return instructions;
};
export const backends: Backend[] = [mipsBackend, jsBackend, cBackend, x64Backend];