-
Notifications
You must be signed in to change notification settings - Fork 0
/
produceProgramInfo.ts
186 lines (172 loc) · 6.61 KB
/
produceProgramInfo.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
import debug from './util/debug';
import { Type } from './types';
import { parseProgram as parseTacProgram } from './threeAddressCode/Program';
import {
mallocWithMmap,
printWithPrintRuntimeFunction,
readIntDirect,
} from './threeAddressCode/runtime';
import { tokenSpecs, MplToken, MplAst } from './grammar';
import writeTempFile from './util/writeTempFile';
import { lex, Token, LexError } from './parser-lib/lex';
import { parseMpl, compile } from './frontend';
import { FrontendOutput, ExecutionResult, CompilationResult } from './api';
import ParseError from './parser-lib/ParseError';
import join from './util/join';
import { toString as typeToString } from './types';
import { astToString } from './ast';
import { Program, toString as programToString } from './threeAddressCode/Program';
import { makeTargetProgram } from './threeAddressCode/generator';
import { backends } from './backend-utils';
import { interpretProgram, createInitialState } from './interpreter';
type BackendResult = {
name: string;
compilationResult: CompilationResult;
executionResults: ExecutionResult[];
};
type ProgramInfo = {
tokens: Token<MplToken>[];
ast: MplAst;
threeAddressCode: string;
threeAddressRoundTrip: Program | LexError | ParseError[];
frontendOutput: FrontendOutput;
backendResults: BackendResult[];
interpreterResults: ExecutionResult; // TODO: merge with backendResults maybe?
structure: string;
};
type RequestedInfo = {
includeExecutionResult: boolean;
buildBinaries: boolean;
skipBackends?: string[];
skipExecutors?: string[];
};
export default async (
source: string,
stdin: string,
{ includeExecutionResult, skipBackends, skipExecutors, buildBinaries }: RequestedInfo
): Promise<
ProgramInfo | LexError | { parseErrors: ParseError[] } | { typeErrors: TypeError[] }
> => {
const tokens = lex(tokenSpecs, source);
if ('kind' in tokens) {
return tokens;
}
const ast = parseMpl(tokens);
if (Array.isArray(ast)) {
return { parseErrors: ast };
}
const frontendOutput = compile(source);
if (
'parseErrors' in frontendOutput ||
'typeErrors' in frontendOutput ||
'kind' in frontendOutput ||
'internalError' in frontendOutput
) {
return frontendOutput as any;
}
let structure = '';
structure += 'Functions:\n';
frontendOutput.functions.forEach(f => {
structure += `-> ${f.name}(${join(
f.parameters.map(p =>
'namedType' in p.type ? p.type.namedType : typeToString(p.type)
),
', '
)})\n`;
f.statements.forEach(statement => {
structure += `---> ${astToString(statement)}\n`;
});
});
structure += 'Program:\n';
structure += '-> Globals:\n';
frontendOutput.globalDeclarations.forEach(declaration => {
structure += `---> ${(declaration.type as Type).type.kind} ${declaration.name}\n`;
});
structure += '-> Statements:\n';
if (Array.isArray(frontendOutput.program)) {
throw debug("Produce Program Info doesn't support modules.");
}
frontendOutput.program.statements.forEach(statement => {
structure += `---> ${astToString(statement)}\n`;
});
// Make three address code with random alignment, bytesInWord, and malloc/print impl. TODO: This is jank. Maybe three address code should abstract over platform stuff?
const threeAddressCode = makeTargetProgram({
backendInputs: frontendOutput,
targetInfo: {
bytesInWord: 4,
syscallNumbers: {},
functionImpls: {
mallocImpl: mallocWithMmap(4),
printImpl: printWithPrintRuntimeFunction(4),
readIntImpl: readIntDirect(4),
},
},
});
const interpreterResults = interpretProgram(
threeAddressCode,
[],
createInitialState(threeAddressCode)
);
// Do a roundtrip on three address code to string and back to check the parser for that
const stringForm = programToString(threeAddressCode);
const roundTripParsed = parseTacProgram(stringForm);
const stdinFile = await writeTempFile(stdin, 'stdin', 'txt');
let backendResults: BackendResult[] = [];
if (buildBinaries) {
backendResults = await Promise.all(
backends.map(async ({ name, compile: compileFn, finishCompilation, executors }) => {
const targetSource = await compileFn(frontendOutput);
if ('error' in targetSource) {
return {
name,
compilationResult: targetSource,
executionResults: [{ error: 'Compilation Failed', executorName: 'N/A' }],
};
}
const compilationResult = await finishCompilation(
targetSource.target,
targetSource.tac
);
// TODO: better way to report these specific errors. Probably muck with the type of ExecutionResult.
if ('error' in compilationResult) {
return {
name,
compilationResult,
executionResults: [{ error: 'Compilation failed', executorName: 'N/A' }],
};
} else if (!includeExecutionResult || (skipBackends || []).includes(name)) {
return {
name,
compilationResult,
executionResults: [{ error: 'Not requested', executorName: 'N/A' }],
};
} else {
const executionResults = (
await Promise.all(
executors.map(async ({ execute, name: executorName }) => {
if ((skipExecutors || []).includes(executorName)) {
return;
}
return await execute(
compilationResult.binaryFile.path,
stdinFile.path
);
})
)
).filter(Boolean) as any;
return { name, compilationResult, executionResults };
}
})
);
}
return {
tokens,
ast,
frontendOutput,
structure,
threeAddressCode: stringForm,
threeAddressRoundTrip: roundTripParsed as any,
backendResults,
interpreterResults,
};
};