-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathstructures.js
238 lines (232 loc) · 8.56 KB
/
structures.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
const {
Enum: { CompilerTypeKind: { PARAMETRIZABLE } }
} = require('../../utils')
module.exports = {
Read: {
'array': [PARAMETRIZABLE, (compiler, array) => {
let code = ''
if (array.countType) {
code += 'const { value: count, size: countSize } = ' + compiler.callType(array.countType) + '\n'
} else if (array.count) {
code += 'const count = ' + array.count + '\n'
code += 'const countSize = 0\n'
} else {
throw new Error('Array must contain either count or countType')
}
code += 'if (count > 0xffffff) throw new Error("array size is abnormally large, not reading: " + count)\n'
code += 'const data = []\n'
code += 'let size = countSize\n'
code += 'for (let i = 0; i < count; i++) {\n'
code += ' const elem = ' + compiler.callType(array.type, 'offset + size') + '\n'
code += ' data.push(elem.value)\n'
code += ' size += elem.size\n'
code += '}\n'
code += 'return new Result(data, size)'
return compiler.wrapCode(code)
}],
'count': [PARAMETRIZABLE, (compiler, type) => {
let code = 'return ' + compiler.callType(type.type)
return compiler.wrapCode(code)
}],
'container': [PARAMETRIZABLE, (compiler, values) => {
values = containerInlining(values)
let code = ''
let offsetExpr = 'offset'
let names = []
for (const i in values) {
const { type, name, anon } = values[i]
let trueName
let sizeName
if (type instanceof Array && type[0] === 'bitfield' && anon) {
const subnames = []
for (const { name } of type[1]) {
const trueName = compiler.getField(name)
subnames.push(trueName)
if (name === trueName) names.push(name)
else names.push(`${name}: ${trueName}`)
}
trueName = '{' + subnames.join(', ') + '}'
sizeName = `anon${i}Size`
} else {
trueName = compiler.getField(name)
sizeName = `${trueName}Size`
if (name === trueName) names.push(name)
else names.push(`${name}: ${trueName}`)
}
code += `const { value: ${trueName}, size: ${sizeName} } = ` + compiler.callType(type, offsetExpr) + '\n'
offsetExpr += ` + ${sizeName}`
}
const sizes = offsetExpr.split(' + ')
sizes.shift()
if (sizes.length === 0) sizes.push('0')
code += 'return new Result({ ' + names.join(', ') + ' }, ' + sizes.join(' + ') + ')'
return compiler.wrapCode(code)
}]
},
Write: {
'array': [PARAMETRIZABLE, (compiler, array) => {
let code = ''
if (array.countType) {
code += 'offset = ' + compiler.callType('value.length', array.countType) + '\n'
} else if (array.count === null) {
throw new Error('Array must contain either count or countType')
}
code += 'for (let i = 0; i < value.length; i++) {\n'
code += ' offset = ' + compiler.callType('value[i]', array.type) + '\n'
code += '}\n'
code += 'return offset'
return compiler.wrapCode(code)
}],
'count': [PARAMETRIZABLE, (compiler, type) => {
let code = 'return ' + compiler.callType('value', type.type)
return compiler.wrapCode(code)
}],
'container': [PARAMETRIZABLE, (compiler, values) => {
values = containerInlining(values)
let code = ''
for (const i in values) {
const { type, name, anon } = values[i]
let trueName
if (type instanceof Array && type[0] === 'bitfield' && anon) {
const names = []
for (const { name } of type[1]) {
const trueName = compiler.getField(name)
code += `const ${trueName} = value.${name}\n`
if (name === trueName) names.push(name)
else names.push(`${name}: ${trueName}`)
}
trueName = '{' + names.join(', ') + '}'
} else {
trueName = compiler.getField(name)
code += `const ${trueName} = value.${name}\n`
}
code += `offset = ` + compiler.callType(trueName, type) + '\n'
}
code += 'return offset'
return compiler.wrapCode(code)
}]
},
SizeOf: {
'array': [PARAMETRIZABLE, (compiler, array) => {
let code = ''
if (array.countType) {
code += 'let size = ' + compiler.callType('value.length', array.countType) + '\n'
} else if (array.count) {
code += 'let size = 0\n'
} else {
throw new Error('Array must contain either count or countType')
}
if (!isNaN(compiler.callType('value[i]', array.type))) {
code += 'size += value.length * ' + compiler.callType('value[i]', array.type) + '\n'
} else {
code += 'for (let i = 0; i < value.length; i++) {\n'
code += ' size += ' + compiler.callType('value[i]', array.type) + '\n'
code += '}\n'
}
code += 'return size'
return compiler.wrapCode(code)
}],
'count': [PARAMETRIZABLE, (compiler, type) => {
let code = 'return ' + compiler.callType('value', type.type)
return compiler.wrapCode(code)
}],
'container': [PARAMETRIZABLE, (compiler, values) => {
values = containerInlining(values)
let code = 'let size = 0\n'
for (const i in values) {
const { type, name, anon } = values[i]
let trueName
if (type instanceof Array && type[0] === 'bitfield' && anon) {
const names = []
for (const { name } of type[1]) {
const trueName = compiler.getField(name)
code += `const ${trueName} = value.${name}\n`
if (name === trueName) names.push(name)
else names.push(`${name}: ${trueName}`)
}
trueName = '{' + names.join(', ') + '}'
} else {
trueName = compiler.getField(name)
code += `const ${trueName} = value.${name}\n`
}
code += `size += ` + compiler.callType(trueName, type) + '\n'
}
code += 'return size'
return compiler.wrapCode(code)
}]
}
}
function containerInlining (values) {
// Inlining (support only 1 level)
const newValues = []
for (const i in values) {
const { type, anon } = values[i]
if (anon && !(type instanceof Array && type[0] === 'bitfield')) {
if (type instanceof Array && type[0] === 'container') {
for (const j in type[1]) newValues.push(type[1][j])
} else if (type instanceof Array && type[0] === 'switch') {
const theSwitch = type[1]
const valueSet = new Set()
// search for containers and build a set of possible values
for (const field in theSwitch.fields) {
if (theSwitch.fields[field] instanceof Array && theSwitch.fields[field][0] === 'container') {
for (const j in theSwitch.fields[field][1]) {
const item = theSwitch.fields[field][1][j]
valueSet.add(item.name)
}
}
}
if (theSwitch.default instanceof Array && theSwitch.default[0] === 'container') {
for (const j in theSwitch.default[1]) {
const item = theSwitch.default[1][j]
valueSet.add(item.name)
}
}
// For each value create a switch
for (const name of valueSet.keys()) {
const fields = {}
let theDefault = theSwitch.default
if (theDefault instanceof Array && theDefault[0] === 'container') {
for (const j in theDefault[1]) {
const item = theDefault[1][j]
if (item.name === name) {
theDefault = item.type
break
}
}
}
for (const field in theSwitch.fields) {
if (theSwitch.fields[field] instanceof Array && theSwitch.fields[field][0] === 'container') {
for (const j in theSwitch.fields[field][1]) {
const item = theSwitch.fields[field][1][j]
if (item.name === name) {
fields[field] = theSwitch.fields[field][1][j].type
break
}
}
} else {
fields[field] = theSwitch.fields[field]
}
}
if (!theDefault) {
theDefault = 'void'
}
newValues.push({
name,
type: ['switch', {
compareTo: theSwitch.compareTo,
compareToValue: theSwitch.compareToValue,
default: theDefault,
fields
}]
})
}
} else {
throw new Error('Cannot inline anonymous type: ' + type)
}
} else {
newValues.push(values[i])
}
}
return newValues
}