Skip to content

Commit

Permalink
new way of handling scope/vars
Browse files Browse the repository at this point in the history
  • Loading branch information
drom committed Mar 12, 2023
1 parent f9d7687 commit 5c5126c
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 97 deletions.
128 changes: 59 additions & 69 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ const generate = (cb) => {
Object.keys(properties).map(key => p.property(properties[key], key));

const {
scopeIdentifierSpan,
// scopeIdentifierSpan,
varSizeSpan, varIdSpan, varNameSpan,
idSpan,
commandSpan,
timeSpan
} = `
scopeIdentifierSpan
varSizeSpan varIdSpan varNameSpan
idSpan
commandSpan
Expand All @@ -73,10 +72,12 @@ const generate = (cb) => {
.trim().split(/\s+/)
.reduce((res, n) => Object.assign(res, {[n]: p.span(p.code.span(n))}), {});

// scopeIdentifierSpan

const {
declaration,
scopeType, scopeTypeEnd,
scopeIdentifier, scopeIdentifierEnd,
// scopeType, scopeTypeEnd,
// scopeIdentifier, scopeIdentifierEnd,
varType, varTypeEnd,
varSize, varSizeEnd,
varId, varIdEnd,
Expand All @@ -89,8 +90,6 @@ const generate = (cb) => {
simulationId
} = `
declaration
scopeType scopeTypeEnd
scopeIdentifier scopeIdentifierEnd
varType varTypeEnd
varSize varSizeEnd
varId varIdEnd
Expand All @@ -105,6 +104,9 @@ const generate = (cb) => {
.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({
Expand Down Expand Up @@ -134,56 +136,50 @@ const generate = (cb) => {

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('$comment $date $timescale $upscope $version'),
// .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

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'));

scopeIdentifier
.match(spaces, scopeIdentifier)
.otherwise(scopeIdentifierSpan.start(scopeIdentifierEnd));

scopeIdentifierEnd
.match(spaces, scopeIdentifierSpan.end(inDeclaration))
.skipTo(scopeIdentifierEnd);

// $var

varType
.match(spaces, varType)
.otherwise(varTypeEnd);

// $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,
Expand All @@ -207,29 +203,23 @@ const generate = (cb) => {
}, p.invoke(p.code.store('type'), varSize))
.otherwise(p.error(3, 'Expected var type'));

varSize
.match(spaces, varSize)
.otherwise(varSizeSpan.start(varSizeEnd));
// $var reg 3 ( r_reg [2:0] $end
// ^

varSizeEnd
.match(spaces, varSizeSpan.end(varId))
.skipTo(varSizeEnd);
varSize.match(spaces, varSize).otherwise(varSizeSpan.start(varSizeEnd));
varSizeEnd.match(spaces, varSizeSpan.end(varId)).skipTo(varSizeEnd);

varId
.match(spaces, varId)
.otherwise(varIdSpan.start(varIdEnd));
// $var reg 3 ( r_reg [2:0] $end
// ^

varIdEnd
.match(spaces, varIdSpan.end(varName))
.skipTo(varIdEnd);
varId.match(spaces, varId).otherwise(varIdSpan.start(varIdEnd));
varIdEnd.match(spaces, varIdSpan.end(varName)).skipTo(varIdEnd);

varName
.match(spaces, varName)
.otherwise(varNameSpan.start(varNameEnd));
// $var reg 3 ( r_reg [2:0] $end
// ^^^^^

varNameEnd
.match(spaces, varNameSpan.end(inDeclaration))
.skipTo(varNameEnd);
varName.match(spaces, varName).otherwise(varNameSpan.start(varNameEnd));
varNameEnd.match('$end', commandSpan.end(varNameSpan.end(declaration))).skipTo(varNameEnd);

// $end

Expand Down
72 changes: 72 additions & 0 deletions lib/command-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const handleScope = (info, str) => {
const [type, name] = str.split(/\s+/);
const ero = {kind: 'scope', type, name, body: []};
const current = info.stack[info.stack.length - 1];
current.body.push(ero);
info.stack.push(ero);
// console.log(ero);
};

const handleUpScope = (info /* , str */) => {
info.stack.pop();
// console.log(['upscope', str]);
};

const handleVar = (info, str) => {
// reg 3 ( r_reg [2:0]
// 0 1 2 3+
const eroj = str.split(/\s+/);
const ero = {
kind: 'var',
type: eroj[0],
size: parseInt(eroj[1]),
link: eroj[2],
name: eroj.slice(3).join('')
};
{
const m = ero.name.match('^(?<name>\\w+)\\[' + (ero.size - 1) + ':0]$');
if (m) {
ero.name = m.groups.name;
}
}
const current = info.stack[info.stack.length - 1];
current.body.push(ero);
// console.log(ero);
};

const commandHandler = (info, cmd, str) => {
str = str.trim();
switch(cmd) {
case 1:
info.comment = str;
// console.log(['comment', str]);
break;
case 2:
info.date = str;
// console.log(['date', str]);
break;
case 3:
handleScope(info, str);
break;
case 4:
info.timescale = str;
// console.log(['timescale', str]);
break;
case 5:
handleUpScope(info, str);
break;
case 6:
handleVar(info, str);
break;
case 7:
info.version = str;
// console.log(['version', str]);
break;
default:
console.log([cmd, str]);
}
};

module.exports = commandHandler;
24 changes: 17 additions & 7 deletions lib/web-vcd-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const EventEmitter = require('events').EventEmitter;

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

const commandHandler = require('./command-handler.js');

// function _waitForStart(mod) {
// return new Promise((resolve)=>{
// mod.addOnPostRun(resolve);
Expand Down Expand Up @@ -50,6 +52,7 @@ const bindCWrap = (c, wasm) => {
};

const getWrapper = wasm => {
// console.log(wasm);

const c = {};

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



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

Expand Down Expand Up @@ -159,16 +163,23 @@ const getWrapper = wasm => {
break;
// path to path (any type)
case 3:
tmp = dotProp.get(boundInfo, getString(v0, v1));
// console.log(`for ${getString(v0, v1)} got ${tmp}, set to ${prop}`);
tmp = getString(v0, v1)
.split(',')
.map(e => dotProp.get(boundInfo, e));
if (tmp.length === 1) {
dotProp.set(boundInfo, prop, tmp[0]);
break;
}
dotProp.set(boundInfo, prop, tmp);
break;
// create empty object at path
case 4:
// console.log(`${prop} is new {}`);
dotProp.set(boundInfo, prop, {});
break;

case 5:
commandHandler(boundInfo, v0, prop);
break;
default: throw new Error();
}

Expand Down Expand Up @@ -221,9 +232,8 @@ module.exports = async wasm => {
// console.log('getWrapper', lib);
await lib.start();
// console.log('vcd wasm srarted');

const wires = {};
const info = {stack: [wires], wires: wires};
const wires = {kind: 'scope', type: '.', name: '.', body: []};
const info = {stack: [wires], wires};

const s = new stream.Writable();

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

s._write = function (chunk, encoding, callback) {
// console.log(cxt, info);
// console.log('chunk:', chunk.length);
const err = lib.execute(cxt, lifemit, triemit2, info, chunk);
if (err) {
console.log(err);
Expand Down
6 changes: 3 additions & 3 deletions out/vcd.js

Large diffs are not rendered by default.

Binary file modified out/vcd.wasm
Binary file not shown.
Loading

0 comments on commit 5c5126c

Please sign in to comment.