@@ -23,7 +23,7 @@ class ImplementationLimit(Exception):
23
23
})
24
24
25
25
26
- def signed (value ):
26
+ def _signed (value ):
27
27
if isinstance (value , str ):
28
28
return False
29
29
elif isinstance (value , int ):
@@ -34,7 +34,7 @@ def signed(value):
34
34
assert False , "Invalid constant {!r}" .format (value )
35
35
36
36
37
- def const (value ):
37
+ def _const (value ):
38
38
if isinstance (value , str ):
39
39
return "\" {}\" " .format (value .translate (_escape_map ))
40
40
elif isinstance (value , int ):
@@ -44,7 +44,7 @@ def const(value):
44
44
# This code path is only used for Instances, where Verilog-like behavior is desirable.
45
45
# Verilog ensures that integers with unspecified width are 32 bits wide or more.
46
46
width = max (32 , bits_for (value ))
47
- return const (ast .Const (value , width ))
47
+ return _const (ast .Const (value , width ))
48
48
elif isinstance (value , ast .Const ):
49
49
value_twos_compl = value .value & ((1 << value .width ) - 1 )
50
50
return "{}'{:0{}b}" .format (value .width , value_twos_compl , value .width )
@@ -98,7 +98,7 @@ def _append(self, *args, **kwargs):
98
98
class _AttrBuilder :
99
99
def _attribute (self , name , value , * , indent = 0 ):
100
100
self ._append ("{}attribute \\ {} {}\n " ,
101
- " " * indent , name , const (value ))
101
+ " " * indent , name , _const (value ))
102
102
103
103
def _attributes (self , attrs , * , src = None , ** kwargs ):
104
104
for name , value in attrs .items ():
@@ -166,12 +166,12 @@ def cell(self, kind, name=None, params={}, ports={}, attrs={}, src=""):
166
166
if isinstance (value , float ):
167
167
self ._append (" parameter real \\ {} \" {!r}\" \n " ,
168
168
param , value )
169
- elif signed (value ):
169
+ elif _signed (value ):
170
170
self ._append (" parameter signed \\ {} {}\n " ,
171
- param , const (value ))
171
+ param , _const (value ))
172
172
else :
173
173
self ._append (" parameter \\ {} {}\n " ,
174
- param , const (value ))
174
+ param , _const (value ))
175
175
for port , wire in ports .items ():
176
176
self ._append (" connect {} {}\n " , port , wire )
177
177
self ._append (" end\n " )
@@ -270,18 +270,14 @@ def update(self, lhs, rhs):
270
270
self ._append (" update {} {}\n " , lhs , rhs )
271
271
272
272
273
- def src (src_loc ):
273
+ def _src (src_loc ):
274
274
if src_loc is None :
275
275
return None
276
276
file , line = src_loc
277
277
return "{}:{}" .format (file , line )
278
278
279
279
280
- def srcs (src_locs ):
281
- return "|" .join (sorted (filter (lambda x : x , map (src , src_locs ))))
282
-
283
-
284
- class LegalizeValue (Exception ):
280
+ class _LegalizeValue (Exception ):
285
281
def __init__ (self , value , branches , src_loc ):
286
282
self .value = value
287
283
self .branches = list (branches )
@@ -335,10 +331,10 @@ def resolve(self, signal, prefix=None):
335
331
336
332
wire_curr = self .rtlil .wire (width = signal .width , name = wire_name ,
337
333
port_id = port_id , port_kind = port_kind ,
338
- attrs = attrs , src = src (signal .src_loc ))
334
+ attrs = attrs , src = _src (signal .src_loc ))
339
335
if signal in self .driven and self .driven [signal ]:
340
336
wire_next = self .rtlil .wire (width = signal .width , name = wire_curr + "$next" ,
341
- src = src (signal .src_loc ))
337
+ src = _src (signal .src_loc ))
342
338
else :
343
339
wire_next = None
344
340
self .wires [signal ] = (wire_curr , wire_next )
@@ -419,7 +415,7 @@ def on_ArrayProxy(self, value):
419
415
else :
420
416
max_index = 1 << len (value .index )
421
417
max_elem = len (value .elems )
422
- raise LegalizeValue (value .index , range (min (max_index , max_elem )), value .src_loc )
418
+ raise _LegalizeValue (value .index , range (min (max_index , max_elem )), value .src_loc )
423
419
424
420
425
421
class _RHSValueCompiler (_ValueCompiler ):
@@ -454,19 +450,19 @@ def on_value(self, value):
454
450
return super ().on_value (self .s .expand (value ))
455
451
456
452
def on_Const (self , value ):
457
- return const (value )
453
+ return _const (value )
458
454
459
455
def on_AnyConst (self , value ):
460
456
if value in self .s .anys :
461
457
return self .s .anys [value ]
462
458
463
459
res_bits , res_sign = value .shape ()
464
- res = self .s .rtlil .wire (width = res_bits , src = src (value .src_loc ))
460
+ res = self .s .rtlil .wire (width = res_bits , src = _src (value .src_loc ))
465
461
self .s .rtlil .cell ("$anyconst" , ports = {
466
462
"\\ Y" : res ,
467
463
}, params = {
468
464
"WIDTH" : res_bits ,
469
- }, src = src (value .src_loc ))
465
+ }, src = _src (value .src_loc ))
470
466
self .s .anys [value ] = res
471
467
return res
472
468
@@ -475,12 +471,12 @@ def on_AnySeq(self, value):
475
471
return self .s .anys [value ]
476
472
477
473
res_bits , res_sign = value .shape ()
478
- res = self .s .rtlil .wire (width = res_bits , src = src (value .src_loc ))
474
+ res = self .s .rtlil .wire (width = res_bits , src = _src (value .src_loc ))
479
475
self .s .rtlil .cell ("$anyseq" , ports = {
480
476
"\\ Y" : res ,
481
477
}, params = {
482
478
"WIDTH" : res_bits ,
483
- }, src = src (value .src_loc ))
479
+ }, src = _src (value .src_loc ))
484
480
self .s .anys [value ] = res
485
481
return res
486
482
@@ -496,15 +492,15 @@ def on_Operator_unary(self, value):
496
492
497
493
arg_bits , arg_sign = arg .shape ()
498
494
res_bits , res_sign = value .shape ()
499
- res = self .s .rtlil .wire (width = res_bits , src = src (value .src_loc ))
495
+ res = self .s .rtlil .wire (width = res_bits , src = _src (value .src_loc ))
500
496
self .s .rtlil .cell (self .operator_map [(1 , value .operator )], ports = {
501
497
"\\ A" : self (arg ),
502
498
"\\ Y" : res ,
503
499
}, params = {
504
500
"A_SIGNED" : arg_sign ,
505
501
"A_WIDTH" : arg_bits ,
506
502
"Y_WIDTH" : res_bits ,
507
- }, src = src (value .src_loc ))
503
+ }, src = _src (value .src_loc ))
508
504
return res
509
505
510
506
def match_shape (self , value , new_bits , new_sign ):
@@ -515,15 +511,15 @@ def match_shape(self, value, new_bits, new_sign):
515
511
if new_bits <= value_bits :
516
512
return self (ast .Slice (value , 0 , new_bits ))
517
513
518
- res = self .s .rtlil .wire (width = new_bits , src = src (value .src_loc ))
514
+ res = self .s .rtlil .wire (width = new_bits , src = _src (value .src_loc ))
519
515
self .s .rtlil .cell ("$pos" , ports = {
520
516
"\\ A" : self (value ),
521
517
"\\ Y" : res ,
522
518
}, params = {
523
519
"A_SIGNED" : value_sign ,
524
520
"A_WIDTH" : value_bits ,
525
521
"Y_WIDTH" : new_bits ,
526
- }, src = src (value .src_loc ))
522
+ }, src = _src (value .src_loc ))
527
523
return res
528
524
529
525
def on_Operator_binary (self , value ):
@@ -539,7 +535,7 @@ def on_Operator_binary(self, value):
539
535
lhs_wire = self .match_shape (lhs , lhs_bits , lhs_sign )
540
536
rhs_wire = self .match_shape (rhs , rhs_bits , rhs_sign )
541
537
res_bits , res_sign = value .shape ()
542
- res = self .s .rtlil .wire (width = res_bits , src = src (value .src_loc ))
538
+ res = self .s .rtlil .wire (width = res_bits , src = _src (value .src_loc ))
543
539
self .s .rtlil .cell (self .operator_map [(2 , value .operator )], ports = {
544
540
"\\ A" : lhs_wire ,
545
541
"\\ B" : rhs_wire ,
@@ -550,19 +546,19 @@ def on_Operator_binary(self, value):
550
546
"B_SIGNED" : rhs_sign ,
551
547
"B_WIDTH" : rhs_bits ,
552
548
"Y_WIDTH" : res_bits ,
553
- }, src = src (value .src_loc ))
549
+ }, src = _src (value .src_loc ))
554
550
if value .operator in ("//" , "%" ):
555
551
# RTLIL leaves division by zero undefined, but we require it to return zero.
556
552
divmod_res = res
557
- res = self .s .rtlil .wire (width = res_bits , src = src (value .src_loc ))
553
+ res = self .s .rtlil .wire (width = res_bits , src = _src (value .src_loc ))
558
554
self .s .rtlil .cell ("$mux" , ports = {
559
555
"\\ A" : divmod_res ,
560
556
"\\ B" : self (ast .Const (0 , ast .Shape (res_bits , res_sign ))),
561
557
"\\ S" : self (rhs == 0 ),
562
558
"\\ Y" : res ,
563
559
}, params = {
564
560
"WIDTH" : res_bits
565
- }, src = src (value .src_loc ))
561
+ }, src = _src (value .src_loc ))
566
562
return res
567
563
568
564
def on_Operator_mux (self , value ):
@@ -573,15 +569,15 @@ def on_Operator_mux(self, value):
573
569
val1_bits = val0_bits = res_bits = max (val1_bits , val0_bits , res_bits )
574
570
val1_wire = self .match_shape (val1 , val1_bits , val1_sign )
575
571
val0_wire = self .match_shape (val0 , val0_bits , val0_sign )
576
- res = self .s .rtlil .wire (width = res_bits , src = src (value .src_loc ))
572
+ res = self .s .rtlil .wire (width = res_bits , src = _src (value .src_loc ))
577
573
self .s .rtlil .cell ("$mux" , ports = {
578
574
"\\ A" : val0_wire ,
579
575
"\\ B" : val1_wire ,
580
576
"\\ S" : self (sel ),
581
577
"\\ Y" : res ,
582
578
}, params = {
583
579
"WIDTH" : res_bits
584
- }, src = src (value .src_loc ))
580
+ }, src = _src (value .src_loc ))
585
581
return res
586
582
587
583
def on_Operator (self , value ):
@@ -599,7 +595,7 @@ def _prepare_value_for_Slice(self, value):
599
595
if isinstance (value , (ast .Signal , ast .Slice , ast .Cat )):
600
596
sigspec = self (value )
601
597
else :
602
- sigspec = self .s .rtlil .wire (len (value ), src = src (value .src_loc ))
598
+ sigspec = self .s .rtlil .wire (len (value ), src = _src (value .src_loc ))
603
599
self .s .rtlil .connect (sigspec , self (value ))
604
600
return sigspec
605
601
@@ -610,7 +606,7 @@ def on_Part(self, value):
610
606
lhs_bits , lhs_sign = lhs .shape ()
611
607
rhs_bits , rhs_sign = rhs .shape ()
612
608
res_bits , res_sign = value .shape ()
613
- res = self .s .rtlil .wire (width = res_bits , src = src (value .src_loc ))
609
+ res = self .s .rtlil .wire (width = res_bits , src = _src (value .src_loc ))
614
610
# Note: Verilog's x[o+:w] construct produces a $shiftx cell, not a $shift cell.
615
611
# However, nMigen's semantics defines the out-of-range bits to be zero, so it is correct
616
612
# to use a $shift cell here instead, even though it produces less idiomatic Verilog.
@@ -624,7 +620,7 @@ def on_Part(self, value):
624
620
"B_SIGNED" : rhs_sign ,
625
621
"B_WIDTH" : rhs_bits ,
626
622
"Y_WIDTH" : res_bits ,
627
- }, src = src (value .src_loc ))
623
+ }, src = _src (value .src_loc ))
628
624
return res
629
625
630
626
def on_Repl (self , value ):
@@ -681,9 +677,9 @@ def on_Part(self, value):
681
677
# is large (e.g. 32-bit wide), trying to naively legalize it is likely to exhaust
682
678
# system resources.
683
679
max_branches = len (value .value ) // value .stride + 1
684
- raise LegalizeValue (value .offset ,
685
- range (1 << len (value .offset ))[:max_branches ],
686
- value .src_loc )
680
+ raise _LegalizeValue (value .offset ,
681
+ range (1 << len (value .offset ))[:max_branches ],
682
+ value .src_loc )
687
683
688
684
def on_Repl (self , value ):
689
685
raise TypeError # :nocov:
@@ -743,7 +739,7 @@ def on_property(self, stmt):
743
739
self .state .rtlil .cell ("$" + stmt ._kind , ports = {
744
740
"\\ A" : check_wire ,
745
741
"\\ EN" : en_wire ,
746
- }, src = src (stmt .src_loc ))
742
+ }, src = _src (stmt .src_loc ))
747
743
748
744
on_Assert = on_property
749
745
on_Assume = on_property
@@ -764,11 +760,11 @@ def on_Switch(self, stmt):
764
760
# don't cache anything in that case.
765
761
test_sigspec = self .rhs_compiler (stmt .test )
766
762
767
- with self ._case .switch (test_sigspec , src = src (stmt .src_loc )) as switch :
763
+ with self ._case .switch (test_sigspec , src = _src (stmt .src_loc )) as switch :
768
764
for values , stmts in stmt .cases .items ():
769
765
case_attrs = {}
770
766
if values in stmt .case_src_locs :
771
- case_attrs ["src" ] = src (stmt .case_src_locs [values ])
767
+ case_attrs ["src" ] = _src (stmt .case_src_locs [values ])
772
768
if isinstance (stmt .test , ast .Signal ) and stmt .test .decoder :
773
769
decoded_values = []
774
770
for value in values :
@@ -785,9 +781,9 @@ def on_Switch(self, stmt):
785
781
def on_statement (self , stmt ):
786
782
try :
787
783
super ().on_statement (stmt )
788
- except LegalizeValue as legalize :
784
+ except _LegalizeValue as legalize :
789
785
with self ._case .switch (self .rhs_compiler (legalize .value ),
790
- src = src (legalize .src_loc )) as switch :
786
+ src = _src (legalize .src_loc )) as switch :
791
787
shape = legalize .value .shape ()
792
788
tests = ["{:0{}b}" .format (v , shape .width ) for v in legalize .branches ]
793
789
if tests :
0 commit comments