@@ -175,9 +175,11 @@ namespace Digitaljs {
175175
176176namespace Yosys {
177177
178- export type BitChar = '0' | '1' | 'x' ;
178+ export const ConstChars = [ "0" , "1" , "x" , "z" ] as const ;
179179
180- export type Bit = number | '0' | '1' | 'x' ;
180+ export type BitChar = ( typeof ConstChars ) [ number ] ;
181+
182+ export type Bit = number | BitChar ;
181183
182184 export type BitVector = Bit [ ] ;
183185
@@ -404,7 +406,7 @@ function yosys_to_digitaljs(data: Yosys.Output, portmaps: Portmaps, options: Con
404406
405407function yosys_to_digitaljs_mod ( name : string , mod : Yosys . Module , portmaps : Portmaps , options : ConvertOptions = { } ) : Digitaljs . Module {
406408 function constbit ( bit : Bit ) {
407- return bit == '0' || bit == '1' || bit == 'x' ;
409+ return ( Yosys . ConstChars as readonly string [ ] ) . includes ( bit . toString ( ) ) ;
408410 }
409411 const nets = new HashMap < Net , NetInfo > ( ) ;
410412 const netnames = new HashMap < Net , string [ ] > ( ) ;
@@ -434,8 +436,6 @@ function yosys_to_digitaljs_mod(name: string, mod: Yosys.Module, portmaps: Portm
434436 const net = get_net ( k ) ;
435437 if ( net . source !== undefined ) {
436438 // multiple sources driving one net, disallowed in digitaljs
437- console . log ( k ) ;
438- console . log ( net ) ;
439439 throw Error ( 'Multiple sources driving net: ' + net . name ) ;
440440 }
441441 net . source = { id : d , port : p } ;
@@ -1170,17 +1170,41 @@ function yosys_to_digitaljs_mod(name: string, mod: Yosys.Module, portmaps: Portm
11701170 return mout ;
11711171}
11721172
1173- function escape_filename ( cmd : string ) : string {
1174- return '"' + cmd . replace ( / ( [ " \s ' $ ` \\ ] ) / g, '\\$1' ) + '"' ;
1173+ function ansi_c_escape_contents ( cmd : string ) : string {
1174+ function func ( ch : string ) {
1175+ if ( ch == '\t' ) return '\\t ';
1176+ if ( ch == '\r' ) return '\\r ';
1177+ if ( ch == '\n' ) return '\\n ';
1178+ return '\\x ' + ch . charCodeAt ( 0 ) . toString ( 16 ) . padStart ( 2 , '0' ) ;
1179+ }
1180+ return cmd . replace ( / ( [ " ' \\ ] ) / g, '\\$1' )
1181+ . replace ( / [ \x00 - \x1F \x7F - \x9F ] / g, func ) ;
1182+ }
1183+
1184+ function ansi_c_escape ( cmd : string ) : string {
1185+ return '"' + ansi_c_escape_contents ( cmd ) + '"' ;
1186+ }
1187+
1188+ function shell_escape_contents ( cmd : string ) : string {
1189+ return cmd . replace ( / ( [ " \r \n $ ` \\ ] ) / g, '\\$1' ) ;
11751190}
1176-
1191+
1192+ function shell_escape ( cmd : string ) : string {
1193+ return '"' + shell_escape_contents ( cmd ) + '"' ;
1194+ }
1195+
1196+ function process_filename ( filename : string ) : string {
1197+ const flags = / \.sv$ / . test ( filename ) ? " -sv" : "" ;
1198+ return "read_verilog " + flags + " " + ansi_c_escape ( filename ) ;
1199+ }
1200+
11771201const verilator_re = / ^ % ( Warning | Error ) [ ^ :] * : ( [ ^ :] * ) :( [ 0 - 9 ] + ) :( [ 0 - 9 ] + ) : ( . * ) $ / ;
11781202
11791203export async function verilator_lint ( filenames : string [ ] , dirname ?: string , options : Options = { } ) : Promise < LintMessage [ ] > {
11801204 try {
11811205 const output : LintMessage [ ] = [ ] ;
11821206 const verilator_result : { stdout : string , stderr : string } = await promisify ( child_process . exec ) (
1183- 'verilator -lint-only -Wall -Wno-DECLFILENAME -Wno-UNOPT -Wno-UNOPTFLAT ' + filenames . map ( escape_filename ) . join ( ' ' ) ,
1207+ 'timeout -k10s 40s verilator -lint-only -Wall -Wno-DECLFILENAME -Wno-UNOPT -Wno-UNOPTFLAT ' + filenames . map ( shell_escape ) . join ( ' ' ) ,
11841208 { maxBuffer : 1000000 , cwd : dirname || null , timeout : options . timeout || 60000 } )
11851209 . catch ( exc => exc ) ;
11861210 for ( const line of verilator_result . stderr . split ( '\n' ) ) {
@@ -1222,8 +1246,9 @@ export async function process(filenames: string[], dirname?: string, options: Op
12221246 const tmpjson = await tmp . tmpName ( { postfix : '.json' } ) ;
12231247 let obj = undefined ;
12241248 const yosys_result : { stdout : string , stderr : string , killed ?: boolean , code ?: number } = await promisify ( child_process . exec ) (
1225- 'yosys -p "hierarchy -auto-top; proc' + optimize_simp + fsmpass + '; memory -nomap; wreduce -memx' +
1226- optimize + '" -o "' + tmpjson + '" ' + filenames . map ( escape_filename ) . join ( ' ' ) ,
1249+ 'timeout -k10s 40s yosys -p "' + shell_escape_contents ( filenames . map ( process_filename ) . join ( '; ' ) ) +
1250+ '; hierarchy -auto-top; proc' + optimize_simp + fsmpass + '; memory -nomap; wreduce -memx' +
1251+ optimize + '" -o "' + tmpjson + '"' ,
12271252 { maxBuffer : 1000000 , cwd : dirname || null , timeout : options . timeout || 60000 } )
12281253 . catch ( exc => exc ) ;
12291254 try {
0 commit comments