-
Notifications
You must be signed in to change notification settings - Fork 25
/
sizeof.js
432 lines (395 loc) · 15.3 KB
/
sizeof.js
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Node.js API.
const util = require('util')
const map = require('./map')
// Determine necessary variables.
const { serialize: declare } = require('./declare')
// Format source code maintaining indentation.
const $ = require('programmatic')
// Generate accumulator declaration source.
const Inline = require('./inline')
// Generate required modules and functions.
const required = require('./required')
// Join an array of strings separated by an empty line.
const join = require('./join')
//
// Generate sizeof function for a given packet definition.
//
function generate (packet, { require = null }) {
const { parameters, accumulators } = declare(packet)
const variables = {
register: true
}
const inliner = Inline({
packet, variables, accumulators, parameters,
direction: 'serialize'
})
let $i = -1
// TODO Fold constants, you're doing `$_ += 1; $_ += 2` which won't fold.
function dispatch (path, field) {
switch (field.type) {
case 'structure': {
if (field.fixed) {
return `$start += ${field.bits >>> 3}`
}
return map(dispatch, path, field.fields)
}
break
case 'conditional': {
// TODO Only referenced, right?
if (field.serialize.unconditional) {
return map(dispatch, path, field.serialize.conditions[0].fields)
break
}
const tests = field.serialize.conditions
.filter(condition => condition.test != null)
.map(condition => condition.test)
const invocations = inliner.accumulations(tests)
let ladder = '', keywords = 'if'
for (let i = 0, I = field.serialize.conditions.length; i < I; i++) {
const condition = field.serialize.conditions[i]
const source = map(dispatch, path, condition.fields)
ladder = condition.test != null ? function () {
const registers = field.split ? [ path ] : []
const test = inliner.test(path, condition.test, registers)
return $(`
`, ladder, `${keywords} (`, test, `) {
`, source, `
}
`)
} () : $(`
`, ladder, ` else {
`, source, `
}
`)
keywords = ' else if'
}
// TODO Remove extra line if no invocations.
return $(`
`, invocations, `
`, ladder, `
`)
}
case 'switch': {
if (field.fixed) {
return $(`
$start += ${field.bits >>> 3}
`)
}
const cases = []
for (const when of field.cases) {
cases.push($(`
${when.otherwise ? 'default' : `case ${util.inspect(when.value)}`}:
`, map(dispatch, path, when.fields), `
break
`))
}
const invocations = inliner.accumulations([ field.select ])
return $(`
`, invocations, -1, `
switch (`, inliner.test(path, field.select), `) {
`, join(cases), `
}
`)
}
break
case 'fixed': {
if (field.calculated) {
const element = field.fields[0]
const calculation = inliner.test(path, field.length)
if (element.fixed) {
return $(`
$start += `, calculation, ` * ${element.bits >>> 3}
`)
}
variables.i = true
const i = `$i[${++$i}]`
const source = $(`
for (${i} = 0; ${i} < ${path}.length; ${i}++) {
`, map(dispatch, `${path}[${i}]`, field.fields), `
}
`)
return source
} else if (field.fixed) {
return $(`
$start += ${field.bits >>> 3}
`)
} else {
variables.i = true
const i = `$i[${++$i}]`
return $(`
for (${i} = 0; ${i} < ${path}.length; ${i}++) {
`, map(dispatch, `${path}[${i}]`, field.fields), `
}
`)
$i--
}
}
break
case 'terminated': {
// TODO Use AST rollup `fixed`.
if (field.fields.filter(field => !field.fixed).length == 0) {
const bits = field.fields.reduce((sum, field) => sum + field.bits, 0)
return field.fields[0].type == 'buffer' && !field.fields[0].concat
? $(`
$start += ${path}.reduce((sum, buffer) => sum + buffer.length, 0) +
${field.terminator.length}
`) : $(`
$start += ${bits >>> 3} * ${path}.length + ${field.terminator.length}
`)
}
variables.i = true
$i++
const i = `$i[${$i}]`
const source = $(`
for (${i} = 0; ${i} < ${path}.length; ${i}++) {
`, map(dispatch, `${path}[${i}]`, field.fields), `
}
$start += ${field.terminator.length}
`)
$i--
return source
}
break
case 'lengthEncoded': {
const encoding = function () {
if (field.fields[0].type == 'buffer' && ! field.fields[0].concat) {
variables.length = true
return $(`
$length = ${path}.reduce((sum, buffer) => sum + buffer.length, 0)
`, map(dispatch, '$length', field.encoding), `
`)
}
return map(dispatch, `${path}.length`, field.encoding)
} ()
if (field.fields[0].fixed) {
return field.fields[0].type == 'buffer' && !field.fields[0].concat
? $(`
`, encoding, `
$start += $length
`) : $(`
`, encoding, `
$start += ${field.fields[0].bits >>> 3} * ${path}.length
`)
}
variables.i = true
$i++
const i = `$i[${$i}]`
const source = $(`
`, encoding, `
for (${i} = 0; ${i} < ${path}.length; ${i}++) {
`, map(dispatch, `${path}[${i}]`, field.fields), `
}
`)
$i--
return source
}
case 'accumulator': {
variables.accumulator = true
// TODO Really want to get rid of this step if the final
// calcualtions are not referenced, if the argument is not an
// external argument and if it is not referenced again in the
// parse or serialize or sizeof.
const accumulator = inliner.accumulator(field, name => referenced[name])
const source = field.fixed
? `$start += ${field.bits >>> 3}`
: map(dispatch, path, field.fields)
return $(`
`, accumulator, -1, `
`, source, `
`)
}
break
case 'inline': {
// Here is logic that only runs the inline if the value is
// referenced by one of the conditionals. The idea being that the
// transformations do not matter because the size is fixed. Of
// course, it is not fixed if it an array, and the transformation
// could be transforming an integer to an array. It wasn't obvious
// because in my tests I was transforming a string of characters
// within the ASCII range to a UTF-8 buffer, so that checking the
// length of the string would work to determine the length of the
// buffer, but it would not work for a string of emoji.
//
// TODO What's to do about this is to determine if the nested value
// is fixed length. If so we can skip the transform.
//
// You have a notion that you could separate transformation into a
// separate machine, so that you could run it once and use it for
// both size of and serialization. You can imagine that it would
// create problems somehow, but it would save on doubled assertions,
// once for size of and once for serialization. Also, makes offset
// of difficult.
//
// Could use `({ $_, $offset: { value } }) => {}` to get a running
// offset, then the pre-conversion would not be as bad. We'd do this
// instead of an offset of function.
const inlines = field.before.filter(inline => {
return (
(
inline.positional && inline.arity > 0
) ||
inline.properties.filter(property => {
return referenced[property]
}).length != 0
)
})
if (field.fixed) {
inlines.length = 0
}
variables.stack = true
const inlined = inliner.inline(path, inlines)
const source = map(dispatch, inlined.path, field.fields)
// TODO Exclude if not externally referenced.
return $(`
`, inlined.inlined, -1, `
`, inlined.starts, -1, `
`, source, `
`, -1, inliner.pop(), `
`)
}
break
case 'literal':
case 'integer':
case 'bigint':
return `$start += ${field.bits >>> 3}`
}
}
const references = { accumulators: [], buffered: {} }, referenced = {}
function dependencies (field) {
switch (field.type) {
case 'structure': {
field.fields.map(dependencies)
}
break
case 'conditional': {
field.serialize.conditions.forEach(condition => {
if (condition.test == null) {
return
}
if (
condition.test.properties.filter(property => {
return /^(?:\$start|\$end)$/.test(property)
}).length != 0
) {
buffered = true
}
condition.test.properties.forEach(property => {
if (references.buffered[property]) {
referenced[property] = true
}
})
condition.fields.map(dependencies)
})
}
break
case 'switch': {
if (
field.select.properties.filter(property => {
return /^(?:\$start|\$end)$/.test(property)
}).length != 0
) {
buffered = true
}
field.select.properties.forEach(property => {
if (references.buffered[property]) {
referenced[property] = true
}
})
field.cases.forEach(when => {
when.fields.map(dependencies)
})
}
break
case 'fixed': {
field.fields.map(dependencies)
}
break
case 'terminated': {
field.fields.map(dependencies)
}
break
case 'lengthEncoded': {
field.fields.map(dependencies)
}
break
case 'accumulator': {
field.accumulators.forEach(accumulator => {
references.accumulators.push(accumulator.name)
})
field.fields.map(dependencies)
}
break
case 'inline': {
field.before.forEach(inline => {
if (
inline.properties.filter(property => {
return /^(?:\$start|\$end)$/.test(property)
}).length != 0
) {
references.accumulators.forEach(accumulator => {
if (inline.properties.includes(accumulator)) {
references.buffered[accumulator] = true
}
})
}
})
field.fields.map(dependencies)
}
break
case 'literal': {
field.fields.map(dependencies)
}
break
case 'buffer':
case 'bigint':
case 'integer':
case 'absent':
break
default: {
throw new Error(field.type)
}
break
}
}
dependencies(packet)
const buffered = Object.keys(referenced).length != 0
if (buffered) {
variables.starts = true
}
const source = dispatch(packet.name, packet)
const declarations = {
register: '$start = 0',
i: '$i = []',
length: '$length',
starts: '$starts = []',
accumulator: '$accumulator = {}',
stack: '$$ = []'
}
const lets = Object.keys(declarations)
.filter(key => variables[key])
.map(key => declarations[key])
const requires = required(require)
return $(`
function () {
`, requires, -1, `
return function (${packet.name}) {
let ${lets.join(', ')}
`, source, `
return $start
}
} ()
`)
}
module.exports = function (packets, options = {}) {
const source = packets.map(packet => {
const source = generate(packet, options)
return $(`
${packet.name}: `, source, `
`)
})
return $(`
{
`, source.join(',\n'), `
}
`)
}