-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcompilation.ts
More file actions
337 lines (279 loc) · 8.47 KB
/
compilation.ts
File metadata and controls
337 lines (279 loc) · 8.47 KB
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
import * as assert from 'assert';
import * as frontend from 'llparse-frontend';
import {
CONTAINER_KEY, STATE_ERROR,
ARG_STATE, ARG_POS, ARG_ENDPOS,
VAR_MATCH,
STATE_PREFIX, LABEL_PREFIX, BLOB_PREFIX,
} from './constants';
import { Code } from './code';
import { Node } from './node';
import { Transform } from './transform';
import { MatchSequence } from './helpers/match-sequence';
// Number of hex words per line of blob declaration
const BLOB_GROUP_SIZE = 11;
type WrappedNode = frontend.IWrap<frontend.node.Node>;
interface IBlob {
readonly alignment: number | undefined;
readonly buffer: Buffer;
readonly name: string;
}
// TODO(indutny): deduplicate
export interface ICompilationOptions {
readonly debug?: string;
}
// TODO(indutny): deduplicate
export interface ICompilationProperty {
readonly name: string;
readonly ty: string;
}
export class Compilation {
private readonly stateMap: Map<string, ReadonlyArray<string>> = new Map();
private readonly blobs: Map<Buffer, IBlob> = new Map();
private readonly codeMap: Map<string, Code<frontend.code.Code>> = new Map();
private readonly matchSequence:
Map<string, MatchSequence> = new Map();
private readonly resumptionTargets: Set<string> = new Set();
constructor(public readonly prefix: string,
private readonly properties: ReadonlyArray<ICompilationProperty>,
resumptionTargets: ReadonlySet<WrappedNode>,
private readonly options: ICompilationOptions) {
for (const node of resumptionTargets) {
this.resumptionTargets.add(STATE_PREFIX + node.ref.id.name);
}
}
private buildStateEnum(out: string[]): void {
out.push('enum llparse_state_e {');
out.push(` ${STATE_ERROR},`);
for (const stateName of this.stateMap.keys()) {
if (this.resumptionTargets.has(stateName)) {
out.push(` ${stateName},`);
}
}
out.push('};');
out.push('typedef enum llparse_state_e llparse_state_t;');
}
private buildBlobs(out: string[]): void {
if (this.blobs.size === 0) {
return;
}
for (const blob of this.blobs.values()) {
const buffer = blob.buffer;
let align = '';
if (blob.alignment) {
align = ` ALIGN(${blob.alignment})`;
}
if (blob.alignment) {
out.push('#ifdef __SSE4_2__');
}
out.push(`static const unsigned char${align} ${blob.name}[] = {`);
for (let i = 0; i < buffer.length; i += BLOB_GROUP_SIZE) {
const limit = Math.min(buffer.length, i + BLOB_GROUP_SIZE);
const hex: string[] = [];
for (let j = i; j < limit; j++) {
const value = buffer[j];
assert(value !== undefined);
hex.push(this.toChar(value));
}
let line = ' ' + hex.join(', ');
if (limit !== buffer.length) {
line += ',';
}
out.push(line);
}
out.push(`};`);
if (blob.alignment) {
out.push('#endif /* __SSE4_2__ */');
}
}
out.push('');
}
private buildMatchSequence(out: string[]): void {
if (this.matchSequence.size === 0) {
return;
}
MatchSequence.buildGlobals(out);
out.push('');
for (const match of this.matchSequence.values()) {
match.build(this, out);
out.push('');
}
}
public reserveSpans(spans: ReadonlyArray<frontend.SpanField>): void {
for (const span of spans) {
for (const callback of span.callbacks) {
this.buildCode(this.unwrapCode(callback));
}
}
}
public debug(out: string[], message: string): void {
if (this.options.debug === undefined) {
return;
}
const args = [
this.stateArg(),
`(const char*) ${this.posArg()}`,
`(const char*) ${this.endPosArg()}`,
];
out.push(`${this.options.debug}(${args.join(', ')},`);
out.push(` ${this.cstring(message)});`);
}
public buildGlobals(out: string[]): void {
if (this.options.debug !== undefined) {
out.push(`void ${this.options.debug}(`);
out.push(` ${this.prefix}_t* s, const char* p, const char* endp,`);
out.push(' const char* msg);');
}
this.buildBlobs(out);
this.buildMatchSequence(out);
this.buildStateEnum(out);
for (const code of this.codeMap.values()) {
out.push('');
code.build(this, out);
}
}
public buildResumptionStates(out: string[]): void {
this.stateMap.forEach((lines, name) => {
if (!this.resumptionTargets.has(name)) {
return;
}
out.push(`case ${name}:`);
out.push(`${LABEL_PREFIX}${name}: {`);
lines.forEach((line) => out.push(` ${line}`));
out.push(' UNREACHABLE;');
out.push('}');
});
}
public buildInternalStates(out: string[]): void {
this.stateMap.forEach((lines, name) => {
if (this.resumptionTargets.has(name)) {
return;
}
out.push(`${LABEL_PREFIX}${name}: {`);
lines.forEach((line) => out.push(` ${line}`));
out.push(' UNREACHABLE;');
out.push('}');
});
}
public addState(state: string, lines: ReadonlyArray<string>): void {
assert(!this.stateMap.has(state));
this.stateMap.set(state, lines);
}
public buildCode(code: Code<frontend.code.Code>): string {
if (this.codeMap.has(code.ref.name)) {
assert.strictEqual(this.codeMap.get(code.ref.name)!, code,
`Code name conflict for "${code.ref.name}"`);
} else {
this.codeMap.set(code.ref.name, code);
}
return code.ref.name;
}
public getFieldType(field: string): string {
for (const property of this.properties) {
if (property.name === field) {
return property.ty;
}
}
throw new Error(`Field "${field}" not found`);
}
// Helpers
public unwrapCode(code: frontend.IWrap<frontend.code.Code>)
: Code<frontend.code.Code> {
const container = code as frontend.ContainerWrap<frontend.code.Code>;
return container.get(CONTAINER_KEY);
}
public unwrapNode(node: WrappedNode): Node<frontend.node.Node> {
const container = node as frontend.ContainerWrap<frontend.node.Node>;
return container.get(CONTAINER_KEY);
}
public unwrapTransform(node: frontend.IWrap<frontend.transform.Transform>)
: Transform<frontend.transform.Transform> {
const container =
node as frontend.ContainerWrap<frontend.transform.Transform>;
return container.get(CONTAINER_KEY);
}
public indent(out: string[], lines: ReadonlyArray<string>, pad: string) {
for (const line of lines) {
out.push(`${pad}${line}`);
}
}
// MatchSequence cache
public getMatchSequence(
transform: frontend.IWrap<frontend.transform.Transform>, select: Buffer)
: string {
const wrap = this.unwrapTransform(transform);
let res: MatchSequence;
if (this.matchSequence.has(wrap.ref.name)) {
res = this.matchSequence.get(wrap.ref.name)!;
} else {
res = new MatchSequence(wrap);
this.matchSequence.set(wrap.ref.name, res);
}
return res.getName();
}
// Arguments
public stateArg(): string {
return ARG_STATE;
}
public posArg(): string {
return ARG_POS;
}
public endPosArg(): string {
return ARG_ENDPOS;
}
public matchVar(): string {
return VAR_MATCH;
}
// State fields
public indexField(): string {
return this.stateField('_index');
}
public currentField(): string {
return this.stateField('_current');
}
public errorField(): string {
return this.stateField('error');
}
public reasonField(): string {
return this.stateField('reason');
}
public errorPosField(): string {
return this.stateField('error_pos');
}
public spanPosField(index: number): string {
return this.stateField(`_span_pos${index}`);
}
public spanCbField(index: number): string {
return this.stateField(`_span_cb${index}`);
}
public stateField(name: string): string {
return `${this.stateArg()}->${name}`;
}
// Globals
public cstring(value: string): string {
return JSON.stringify(value);
}
public blob(value: Buffer, alignment?: number): string {
if (this.blobs.has(value)) {
return this.blobs.get(value)!.name;
}
const res = BLOB_PREFIX + this.blobs.size;
this.blobs.set(value, {
alignment,
buffer: value,
name: res,
});
return res;
}
public toChar(value: number): string {
const ch = String.fromCharCode(value);
// `'`, `\`
if (value === 0x27 || value === 0x5c) {
return `'\\${ch}'`;
} else if (value >= 0x20 && value <= 0x7e) {
return `'${ch}'`;
} else {
return `0x${value.toString(16)}`;
}
}
}