From a6acac4b77468845237783a02131493135102efd Mon Sep 17 00:00:00 2001 From: Aliaksei Chapyzhenka Date: Mon, 6 Jan 2025 15:10:45 -0800 Subject: [PATCH] hash table for ids --- bin/build.js | 15 +++++++++------ lib/command-handler.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/bin/build.js b/bin/build.js index ae5f8d5..d19e67a 100755 --- a/bin/build.js +++ b/bin/build.js @@ -25,10 +25,10 @@ const objection = lut => arg => arg.split(/\s+/).reduce((res, key) => { }, {}); const properties = { - command: 'i8', - type: 'i8', + definitions: 'ptr', + output: 'ptr', + id_sum: 'i32', size: 'i32', - time: 'i64', // current simulation time trigger: 'ptr', triee: 'ptr', // trigger event emitter lifee: 'ptr', // life cycle event emmiter @@ -43,13 +43,16 @@ const properties = { tmpStr2: 'ptr', stackPointer: 'i32', id: 'ptr', - napi_env: 'ptr' + napi_env: 'ptr', + time: 'i64', // current simulation time + command: 'i8', + type: 'i8' }; const spaces = [' ', '\n', '\r', '\t']; const lineSpaces = [' ', '\t']; -const generate = (cb) => { +const generate = (/* cb */) => { // const llparseDot = require('llparse-dot'); const prj = 'vcd_parser'; @@ -303,7 +306,7 @@ const generate = (cb) => { // const dot = new llparseDot.Dot(); // fs.writeFileSync(prj + '.dot', dot.build(declaration)); - cb(); + // cb(); }; generate(gyp); diff --git a/lib/command-handler.js b/lib/command-handler.js index 7abc327..d78b394 100644 --- a/lib/command-handler.js +++ b/lib/command-handler.js @@ -14,15 +14,42 @@ const handleUpScope = (info /* , str */) => { // console.log(['upscope', str]); }; +const updateIdTable = ($, link) => { + if ($.ido[link] !== undefined) { // old Id + return; + } + + // polynomial rolling hash function + let hash = 0; + let poly = 1; + for (let i = 0; i < link.length; i++) { + const c = link.charCodeAt(i) - 33 + 1; // ! .. ~ (94 digits) + hash = (hash + poly * c) & 0xfff; + poly = (poly * 97) & 0xfff; // 89, 97 + } + + $.ido[link] = [$.nextId, hash]; + + // add entry to the Hash Table object + if ($.hashTable[hash] === undefined) { + $.hashTable[hash] = {}; + } + + $.hashTable[hash][link] = $.nextId; + $.nextId += 1; +}; + const handleVar = (info, str) => { // reg 3 ( r_reg [2:0] // 0 1 2 3+ const eroj = str.split(/\s+/); + const link = eroj[2]; + updateIdTable(info, link); const ero = { kind: 'var', type: eroj[0], size: parseInt(eroj[1]), - link: eroj[2], + link, name: eroj.slice(3).join('') }; {