Skip to content

Commit 5c5126c

Browse files
committed
new way of handling scope/vars
1 parent f9d7687 commit 5c5126c

File tree

8 files changed

+192
-97
lines changed

8 files changed

+192
-97
lines changed

bin/build.js

+59-69
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ const generate = (cb) => {
5858
Object.keys(properties).map(key => p.property(properties[key], key));
5959

6060
const {
61-
scopeIdentifierSpan,
61+
// scopeIdentifierSpan,
6262
varSizeSpan, varIdSpan, varNameSpan,
6363
idSpan,
6464
commandSpan,
6565
timeSpan
6666
} = `
67-
scopeIdentifierSpan
6867
varSizeSpan varIdSpan varNameSpan
6968
idSpan
7069
commandSpan
@@ -73,10 +72,12 @@ const generate = (cb) => {
7372
.trim().split(/\s+/)
7473
.reduce((res, n) => Object.assign(res, {[n]: p.span(p.code.span(n))}), {});
7574

75+
// scopeIdentifierSpan
76+
7677
const {
7778
declaration,
78-
scopeType, scopeTypeEnd,
79-
scopeIdentifier, scopeIdentifierEnd,
79+
// scopeType, scopeTypeEnd,
80+
// scopeIdentifier, scopeIdentifierEnd,
8081
varType, varTypeEnd,
8182
varSize, varSizeEnd,
8283
varId, varIdEnd,
@@ -89,8 +90,6 @@ const generate = (cb) => {
8990
simulationId
9091
} = `
9192
declaration
92-
scopeType scopeTypeEnd
93-
scopeIdentifier scopeIdentifierEnd
9493
varType varTypeEnd
9594
varSize varSizeEnd
9695
varId varIdEnd
@@ -105,6 +104,9 @@ const generate = (cb) => {
105104
.trim().split(/\s+/)
106105
.reduce((res, n) => Object.assign(res, {[n]: p.node(n)}), {});
107106

107+
// scopeType scopeTypeEnd
108+
// scopeIdentifier scopeIdentifierEnd
109+
108110
const enddefinitions = p.node('inDeclarationEnd');
109111

110112
const cmd = objection({
@@ -134,56 +136,50 @@ const generate = (cb) => {
134136

135137
declaration
136138
.match(spaces, declaration)
137-
.select(cmd('$scope'),
138-
p.invoke(p.code.store('command'), commandSpan.start(scopeType)))
139-
.select(cmd('$var'),
140-
p.invoke(p.code.store('command'), commandSpan.start(varType)))
141-
.select(cmd('$comment $date $timescale $upscope $version'),
139+
// .select(cmd('$scope'),
140+
// p.invoke(p.code.store('command'), commandSpan.start(scopeType)))
141+
// .select(cmd('$var'),
142+
// p.invoke(p.code.store('command'), commandSpan.start(varType)))
143+
.select(cmd('$scope $var $upscope $comment $date $timescale $version'),
142144
p.invoke(p.code.store('command'), commandSpan.start(inDeclaration)))
143145
.select(cmd('$enddefinitions'),
144146
p.invoke(p.code.store('command'), commandSpan.start(enddefinitions)))
145147
.otherwise(p.error(1, 'Expected declaration command'));
146148

147-
// $scope
148-
149-
scopeType
150-
.match(spaces, scopeType)
151-
.otherwise(scopeTypeEnd);
152-
153-
scopeTypeEnd
154-
.select(
155-
{
156-
module: 0,
157-
task: 1,
158-
function: 2,
159-
begin: 3,
160-
fork: 4,
161-
// extra scopes from Verilator
162-
generate: 5,
163-
struct: 6,
164-
union: 7,
165-
class: 8,
166-
interface: 9,
167-
package: 10,
168-
program: 11
169-
},
170-
p.invoke(p.code.store('type'), scopeIdentifier))
171-
.otherwise(p.error(2, 'Expected scope type'));
172-
173-
scopeIdentifier
174-
.match(spaces, scopeIdentifier)
175-
.otherwise(scopeIdentifierSpan.start(scopeIdentifierEnd));
176-
177-
scopeIdentifierEnd
178-
.match(spaces, scopeIdentifierSpan.end(inDeclaration))
179-
.skipTo(scopeIdentifierEnd);
180-
181-
// $var
182-
183-
varType
184-
.match(spaces, varType)
185-
.otherwise(varTypeEnd);
186-
149+
// $scope module clkdiv2n_tb $end
150+
// ^^^^^^
151+
152+
// scopeType.match(spaces, scopeType).otherwise(scopeTypeEnd);
153+
// scopeTypeEnd
154+
// .select(
155+
// {
156+
// module: 0,
157+
// task: 1,
158+
// function: 2,
159+
// begin: 3,
160+
// fork: 4,
161+
// // extra scopes from Verilator
162+
// generate: 5,
163+
// struct: 6,
164+
// union: 7,
165+
// class: 8,
166+
// interface: 9,
167+
// package: 10,
168+
// program: 11
169+
// },
170+
// p.invoke(p.code.store('type'), scopeIdentifier))
171+
// .otherwise(p.error(2, 'Expected scope type'));
172+
173+
// $scope module clkdiv2n_tb $end
174+
// ^^^^^^^^^^^
175+
176+
// scopeIdentifier.match(spaces, scopeIdentifier).otherwise(scopeIdentifierSpan.start(scopeIdentifierEnd));
177+
// scopeIdentifierEnd.match(spaces, scopeIdentifierSpan.end(inDeclaration)).skipTo(scopeIdentifierEnd);
178+
179+
// $var reg 3 ( r_reg [2:0] $end
180+
// ^^^
181+
182+
varType.match(spaces, varType).otherwise(varTypeEnd);
187183
varTypeEnd
188184
.select({
189185
event: 1,
@@ -207,29 +203,23 @@ const generate = (cb) => {
207203
}, p.invoke(p.code.store('type'), varSize))
208204
.otherwise(p.error(3, 'Expected var type'));
209205

210-
varSize
211-
.match(spaces, varSize)
212-
.otherwise(varSizeSpan.start(varSizeEnd));
206+
// $var reg 3 ( r_reg [2:0] $end
207+
// ^
213208

214-
varSizeEnd
215-
.match(spaces, varSizeSpan.end(varId))
216-
.skipTo(varSizeEnd);
209+
varSize.match(spaces, varSize).otherwise(varSizeSpan.start(varSizeEnd));
210+
varSizeEnd.match(spaces, varSizeSpan.end(varId)).skipTo(varSizeEnd);
217211

218-
varId
219-
.match(spaces, varId)
220-
.otherwise(varIdSpan.start(varIdEnd));
212+
// $var reg 3 ( r_reg [2:0] $end
213+
// ^
221214

222-
varIdEnd
223-
.match(spaces, varIdSpan.end(varName))
224-
.skipTo(varIdEnd);
215+
varId.match(spaces, varId).otherwise(varIdSpan.start(varIdEnd));
216+
varIdEnd.match(spaces, varIdSpan.end(varName)).skipTo(varIdEnd);
225217

226-
varName
227-
.match(spaces, varName)
228-
.otherwise(varNameSpan.start(varNameEnd));
218+
// $var reg 3 ( r_reg [2:0] $end
219+
// ^^^^^
229220

230-
varNameEnd
231-
.match(spaces, varNameSpan.end(inDeclaration))
232-
.skipTo(varNameEnd);
221+
varName.match(spaces, varName).otherwise(varNameSpan.start(varNameEnd));
222+
varNameEnd.match('$end', commandSpan.end(varNameSpan.end(declaration))).skipTo(varNameEnd);
233223

234224
// $end
235225

lib/command-handler.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
const handleScope = (info, str) => {
4+
const [type, name] = str.split(/\s+/);
5+
const ero = {kind: 'scope', type, name, body: []};
6+
const current = info.stack[info.stack.length - 1];
7+
current.body.push(ero);
8+
info.stack.push(ero);
9+
// console.log(ero);
10+
};
11+
12+
const handleUpScope = (info /* , str */) => {
13+
info.stack.pop();
14+
// console.log(['upscope', str]);
15+
};
16+
17+
const handleVar = (info, str) => {
18+
// reg 3 ( r_reg [2:0]
19+
// 0 1 2 3+
20+
const eroj = str.split(/\s+/);
21+
const ero = {
22+
kind: 'var',
23+
type: eroj[0],
24+
size: parseInt(eroj[1]),
25+
link: eroj[2],
26+
name: eroj.slice(3).join('')
27+
};
28+
{
29+
const m = ero.name.match('^(?<name>\\w+)\\[' + (ero.size - 1) + ':0]$');
30+
if (m) {
31+
ero.name = m.groups.name;
32+
}
33+
}
34+
const current = info.stack[info.stack.length - 1];
35+
current.body.push(ero);
36+
// console.log(ero);
37+
};
38+
39+
const commandHandler = (info, cmd, str) => {
40+
str = str.trim();
41+
switch(cmd) {
42+
case 1:
43+
info.comment = str;
44+
// console.log(['comment', str]);
45+
break;
46+
case 2:
47+
info.date = str;
48+
// console.log(['date', str]);
49+
break;
50+
case 3:
51+
handleScope(info, str);
52+
break;
53+
case 4:
54+
info.timescale = str;
55+
// console.log(['timescale', str]);
56+
break;
57+
case 5:
58+
handleUpScope(info, str);
59+
break;
60+
case 6:
61+
handleVar(info, str);
62+
break;
63+
case 7:
64+
info.version = str;
65+
// console.log(['version', str]);
66+
break;
67+
default:
68+
console.log([cmd, str]);
69+
}
70+
};
71+
72+
module.exports = commandHandler;

lib/web-vcd-parser.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const EventEmitter = require('events').EventEmitter;
55

66
const dotProp = require('dot-prop');
77

8+
const commandHandler = require('./command-handler.js');
9+
810
// function _waitForStart(mod) {
911
// return new Promise((resolve)=>{
1012
// mod.addOnPostRun(resolve);
@@ -50,6 +52,7 @@ const bindCWrap = (c, wasm) => {
5052
};
5153

5254
const getWrapper = wasm => {
55+
// console.log(wasm);
5356

5457
const c = {};
5558

@@ -115,6 +118,7 @@ const getWrapper = wasm => {
115118
// console.log({name, time, command, valueWords});
116119

117120

121+
118122
// const view0 = wasm.HEAPU8.subarray(value, value+(valueWords*8));
119123
// const view1 = wasm.HEAPU8.subarray(mask, mask+(valueWords*8));
120124

@@ -159,16 +163,23 @@ const getWrapper = wasm => {
159163
break;
160164
// path to path (any type)
161165
case 3:
162-
tmp = dotProp.get(boundInfo, getString(v0, v1));
163-
// console.log(`for ${getString(v0, v1)} got ${tmp}, set to ${prop}`);
166+
tmp = getString(v0, v1)
167+
.split(',')
168+
.map(e => dotProp.get(boundInfo, e));
169+
if (tmp.length === 1) {
170+
dotProp.set(boundInfo, prop, tmp[0]);
171+
break;
172+
}
164173
dotProp.set(boundInfo, prop, tmp);
165174
break;
166175
// create empty object at path
167176
case 4:
168177
// console.log(`${prop} is new {}`);
169178
dotProp.set(boundInfo, prop, {});
170179
break;
171-
180+
case 5:
181+
commandHandler(boundInfo, v0, prop);
182+
break;
172183
default: throw new Error();
173184
}
174185

@@ -221,9 +232,8 @@ module.exports = async wasm => {
221232
// console.log('getWrapper', lib);
222233
await lib.start();
223234
// console.log('vcd wasm srarted');
224-
225-
const wires = {};
226-
const info = {stack: [wires], wires: wires};
235+
const wires = {kind: 'scope', type: '.', name: '.', body: []};
236+
const info = {stack: [wires], wires};
227237

228238
const s = new stream.Writable();
229239

@@ -245,7 +255,7 @@ module.exports = async wasm => {
245255
const cxt = lib.init(lifemit, triemit, info);
246256

247257
s._write = function (chunk, encoding, callback) {
248-
// console.log(cxt, info);
258+
// console.log('chunk:', chunk.length);
249259
const err = lib.execute(cxt, lifemit, triemit2, info, chunk);
250260
if (err) {
251261
console.log(err);

out/vcd.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/vcd.wasm

-4.37 KB
Binary file not shown.

0 commit comments

Comments
 (0)