-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.js
executable file
·311 lines (268 loc) · 7.82 KB
/
build.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const cp = require('child_process');
const llparse = require('llparse');
const gyp = cb => {
console.log('build');
const proc = cp.spawn('node-gyp', ['configure', 'build']);
proc.stderr.on('data', data => {
console.error(data.toString());
});
proc.on('close', (cb || (() => {
console.log('done');
})));
};
const objection = lut => arg => arg.split(/\s+/).reduce((res, key) => {
if (lut[key] === undefined) {
throw new Error(key);
}
res[key] = lut[key];
return res;
}, {});
const properties = {
command: 'i8',
type: 'i8',
size: 'i32',
time: 'i64', // current simulation time
trigger: 'ptr',
triee: 'ptr', // trigger event emitter
lifee: 'ptr', // life cycle event emmiter
info: 'ptr',
value: 'ptr', // value of the signal on change event
mask: 'ptr', // mask (x, z) of the signal on change event
digitCount: 'i32',
maskCount: 'i32',
tmpStr: 'ptr',
timeStampStr: 'ptr',
idStr: 'ptr',
tmpStr2: 'ptr',
stackPointer: 'i32',
id: 'ptr',
napi_env: 'ptr'
};
const spaces = [' ', '\n', '\r', '\t'];
const lineSpaces = [' ', '\t'];
const generate = (cb) => {
// const llparseDot = require('llparse-dot');
const prj = 'vcd_parser';
const p = new llparse.LLParse(prj);
Object.keys(properties).map(key => p.property(properties[key], key));
const {
// scopeIdentifierSpan,
varSizeSpan, varIdSpan, varNameSpan,
idSpan,
commandSpan,
timeSpan
} = `
varSizeSpan varIdSpan varNameSpan
idSpan
commandSpan
timeSpan
`
.trim().split(/\s+/)
.reduce((res, n) => Object.assign(res, {[n]: p.span(p.code.span(n))}), {});
// scopeIdentifierSpan
const {
declaration,
// scopeType, scopeTypeEnd,
// scopeIdentifier, scopeIdentifierEnd,
varType, varTypeEnd,
varSize, varSizeEnd,
varId, varIdEnd,
varName, varNameEnd,
inDeclaration,
simulation,
inSimulation,
simulationTime,
simulationVector, simulationVectorEnd, simulationVectorRecovery,
simulationId
} = `
declaration
varType varTypeEnd
varSize varSizeEnd
varId varIdEnd
varName varNameEnd
inDeclaration
simulation
inSimulation
simulationTime
simulationVector simulationVectorEnd simulationVectorRecovery
simulationId
`
.trim().split(/\s+/)
.reduce((res, n) => Object.assign(res, {[n]: p.node(n)}), {});
// scopeType scopeTypeEnd
// scopeIdentifier scopeIdentifierEnd
const enddefinitions = p.node('inDeclarationEnd');
const cmd = objection({
$comment: 1,
$date: 2,
$scope: 3,
$timescale: 4,
$upscope: 5,
$var: 6,
$version: 7,
$enddefinitions: 8,
$dumpall: 9,
$dumpoff: 10,
$dumpon: 11,
$dumpvars: 12,
'#': 13,
'0': 14, '1': 15,
x: 16, X: 17,
z: 18, Z: 19,
u: 20, U: 21, // VHDL states
w: 22, W: 23,
l: 24, L: 25,
h: 26, H: 27,
'-': 28,
b: 30, B: 31, r: 32, R: 33
});
declaration
.match(spaces, declaration)
// .select(cmd('$scope'),
// p.invoke(p.code.store('command'), commandSpan.start(scopeType)))
// .select(cmd('$var'),
// p.invoke(p.code.store('command'), commandSpan.start(varType)))
.select(cmd('$scope $var $upscope $comment $date $timescale $version'),
p.invoke(p.code.store('command'), commandSpan.start(inDeclaration)))
.select(cmd('$enddefinitions'),
p.invoke(p.code.store('command'), commandSpan.start(enddefinitions)))
.otherwise(p.error(1, 'Expected declaration command'));
// $scope module clkdiv2n_tb $end
// ^^^^^^
// scopeType.match(spaces, scopeType).otherwise(scopeTypeEnd);
// scopeTypeEnd
// .select(
// {
// module: 0,
// task: 1,
// function: 2,
// begin: 3,
// fork: 4,
// // extra scopes from Verilator
// generate: 5,
// struct: 6,
// union: 7,
// class: 8,
// interface: 9,
// package: 10,
// program: 11
// },
// p.invoke(p.code.store('type'), scopeIdentifier))
// .otherwise(p.error(2, 'Expected scope type'));
// $scope module clkdiv2n_tb $end
// ^^^^^^^^^^^
// scopeIdentifier.match(spaces, scopeIdentifier).otherwise(scopeIdentifierSpan.start(scopeIdentifierEnd));
// scopeIdentifierEnd.match(spaces, scopeIdentifierSpan.end(inDeclaration)).skipTo(scopeIdentifierEnd);
// $var reg 3 ( r_reg [2:0] $end
// ^^^
varType.match(spaces, varType).otherwise(varTypeEnd);
varTypeEnd
.select({
event: 1,
integer: 2,
parameter: 3,
real: 4,
realtime: 5,
reg: 6,
supply0: 7,
supply1: 8,
time: 9,
tri: 10,
triand: 11,
trior: 12,
trireg: 13,
tri0: 14,
tri1: 15,
wand: 16,
wire: 17,
wor: 18
}, p.invoke(p.code.store('type'), varSize))
.otherwise(p.error(3, 'Expected var type'));
// $var reg 3 ( r_reg [2:0] $end
// ^
varSize.match(spaces, varSize).otherwise(varSizeSpan.start(varSizeEnd));
varSizeEnd.match(spaces, varSizeSpan.end(varId)).skipTo(varSizeEnd);
// $var reg 3 ( r_reg [2:0] $end
// ^
varId.match(spaces, varId).otherwise(varIdSpan.start(varIdEnd));
varIdEnd.match(spaces, varIdSpan.end(varName)).skipTo(varIdEnd);
// $var reg 3 ( r_reg [2:0] $end
// ^^^^^
varName.match(spaces, varName).otherwise(varNameSpan.start(varNameEnd));
varNameEnd.match('$end', commandSpan.end(varNameSpan.end(declaration))).skipTo(varNameEnd);
// $end
inDeclaration
.match('$end', commandSpan.end(declaration))
.skipTo(inDeclaration);
enddefinitions
.match('$end', commandSpan.end(simulation))
.skipTo(enddefinitions);
simulation
.match([' ', '\r', '\n', '\t', '$dumpvars', '$end'], simulation)
.select(cmd('$dumpall $dumpoff $dumpon $comment'),
p.invoke(p.code.store('command'), commandSpan.start(inSimulation)))
.select(cmd('#'),
p.invoke(p.code.store('command'), timeSpan.start(simulationTime)))
.select(cmd('0 1 x X z Z u U w W l L h H -'),
p.invoke(p.code.store('command'), idSpan.start(simulationId)))
.select(cmd('b B r R'),
p.invoke(p.code.store('command'), simulationVector))
.otherwise(p.error(4, 'Expected simulation command'));
inSimulation
.match('$end', commandSpan.end(simulation))
.skipTo(inSimulation);
simulationTime
.match(spaces, timeSpan.end(p.invoke(p.code.span('onTime'), simulation)))
.skipTo(simulationTime);
simulationVector
.select(
{
0: 0,
1: 1,
x: 2, X: 2,
z: 3, Z: 3,
u: 3, U: 3, // VHDL states
w: 3, W: 3,
l: 3, L: 3,
h: 3, H: 3,
'-': 3
},
p.invoke(
// p.code.mulAdd('value', {base: 2, signed: false}),
p.code.value('onDigit'),
{1: p.error(5, 'Content-Length overflow')},
simulationVector
)
)
.otherwise(simulationVectorEnd);
simulationVectorEnd
.match(lineSpaces, idSpan.start(simulationId))
.skipTo(simulationVectorRecovery);
simulationVectorRecovery
.select(
{
'\n': 1, '\r': 1
},
p.invoke(
p.code.value('onRecover'),
{1: p.error(6, 'recover')},
simulation
)
)
.skipTo(simulationVectorRecovery);
simulationId
.match(spaces, idSpan.end(p.invoke(p.code.span('onId'), simulation)))
.skipTo(simulationId);
const artifacts = p.build(declaration);
fs.writeFileSync(prj + '.h', artifacts.header);
// fs.writeFileSync('verilog_preprocessor.bc', artifacts.bitcode);
fs.writeFileSync(prj + '.c', artifacts.c);
// const dot = new llparseDot.Dot();
// fs.writeFileSync(prj + '.dot', dot.build(declaration));
cb();
};
generate(gyp);
/* eslint camelcase: 0 */