Skip to content

Commit c9d6f22

Browse files
committed
4'sdのようなコードを解釈できないため修整。lexer内のtoken解釈をする順番を入れ替えただけ。
casex文についてcase文と同様に解釈する機能の追加 上記確認テストの追加。
1 parent 80deaed commit c9d6f22

File tree

6 files changed

+207
-43
lines changed

6 files changed

+207
-43
lines changed

pyverilog/testcode/casex.v

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module TOP(CLK, RST, LED);
2+
input CLK, RST;
3+
output [7:0] LED;
4+
reg [7:0] cnt;
5+
always @(posedge CLK) begin
6+
if(RST) begin
7+
cnt <= 0;
8+
end else begin
9+
casex(cnt)
10+
'b00: begin
11+
cnt <= cnt + 1;
12+
end
13+
'b1x: begin
14+
cnt <= 0;
15+
end
16+
default: begin
17+
cnt <= cnt + 1;
18+
end
19+
endcase
20+
end
21+
end
22+
assign LED = cnt;
23+
endmodule
24+

pyverilog/testcode/signed.v

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//`default_nettype none
2+
3+
module TOP(CLK, RST);
4+
input CLK, RST;
5+
reg [7:0] cnt;
6+
7+
8+
always @(posedge CLK or negedge RST) begin
9+
if(RST) begin
10+
cnt <= 'd0;
11+
end else begin
12+
cnt <= cnt + 1'sd1;
13+
end
14+
end
15+
16+
17+
endmodule
18+

pyverilog/testcode/test_sd.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#-------------------------------------------------------------------------------
2+
# test_sd.py
3+
#
4+
# Lexical analyzer
5+
#
6+
# Copyright (C) 2015, ryosuke fukatani
7+
# License: Apache 2.0
8+
#-------------------------------------------------------------------------------
9+
10+
11+
import sys
12+
import os
13+
import subprocess
14+
15+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) )
16+
17+
from pyverilog.dataflow.dataflow_analyzer import *
18+
import unittest
19+
20+
21+
class TestSequenceFunctions(unittest.TestCase):
22+
def setUp(self):
23+
path_clone = sys.path
24+
pop_target = []
25+
for i,path in enumerate(path_clone):
26+
if path == 'C:\\Python27\\lib\\site-packages\\pyverilog-0.9.0-py2.7.egg':
27+
pop_target.append(i)
28+
for i in reversed(pop_target):
29+
sys.path.pop(i)
30+
reload(pyverilog.dataflow.dataflow_analyzer)
31+
32+
def test_signed(self):
33+
terms, binddict = self.dataflow_wrapper("signed.v")
34+
self.assertEqual(binddict.values()[0][0].tostr(),
35+
"(Bind dest:TOP.cnt tree:(Branch Cond:(Terminal TOP.RST) True:(IntConst 'd0) False:(Operator Plus Next:(Terminal TOP.cnt),(IntConst 1'sd1))))")
36+
37+
def test_casex(self):
38+
self.dataflow_wrapper("casex.v")
39+
40+
def dataflow_wrapper(self,code_file):
41+
42+
from optparse import OptionParser
43+
44+
optparser = OptionParser()
45+
optparser.add_option("-v","--version",action="store_true",dest="showversion",
46+
default=False,help="Show the version")
47+
optparser.add_option("-I","--include",dest="include",action="append",
48+
default=[],help="Include path")
49+
optparser.add_option("-D",dest="define",action="append",
50+
default=[],help="Macro Definition")
51+
optparser.add_option("-t","--top",dest="topmodule",
52+
default="TOP",help="Top module, Default=TOP")
53+
optparser.add_option("--nobind",action="store_true",dest="nobind",
54+
default=False,help="No binding traversal, Default=False")
55+
optparser.add_option("--noreorder",action="store_true",dest="noreorder",
56+
default=False,help="No reordering of binding dataflow, Default=False")
57+
58+
filelist = {code_file}
59+
options = optparser.get_default_values()
60+
61+
62+
for f in filelist:
63+
if not os.path.exists(f): raise IOError("file not found: " + f)
64+
65+
verilogdataflowanalyzer = VerilogDataflowAnalyzer(filelist, options.topmodule,
66+
noreorder=options.noreorder,
67+
nobind=options.nobind,
68+
preprocess_include=options.include,
69+
preprocess_define=options.define)
70+
verilogdataflowanalyzer.generate()
71+
72+
directives = verilogdataflowanalyzer.get_directives()
73+
print('Directive:')
74+
for dr in directives:
75+
print(dr)
76+
77+
instances = verilogdataflowanalyzer.getInstances()
78+
print('Instance:')
79+
for ins in instances:
80+
print(ins)
81+
82+
if options.nobind:
83+
print('Signal:')
84+
signals = verilogdataflowanalyzer.getSignals()
85+
for sig in signals:
86+
print(sig)
87+
88+
print('Const:')
89+
consts = verilogdataflowanalyzer.getConsts()
90+
for con in consts:
91+
print(con)
92+
93+
else:
94+
terms = verilogdataflowanalyzer.getTerms()
95+
print('Term:')
96+
for tk, tv in sorted(terms.items(), key=lambda x:len(x[0])):
97+
print(tv.tostr())
98+
99+
binddict = verilogdataflowanalyzer.getBinddict()
100+
print('Bind:')
101+
for bk, bv in sorted(binddict.items(), key=lambda x:len(x[0])):
102+
for bvi in bv:
103+
print(bvi.tostr())
104+
105+
return terms, binddict
106+
107+
if __name__ == '__main__':
108+
unittest.main()

pyverilog/vparser/ast.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#-------------------------------------------------------------------------------
22
# ast.py
3-
#
3+
#
44
# Verilog HDL AST classes with Pyverilog
55
#
66
# Copyright (C) 2013, Shinya Takamaeda-Yamazaki
7+
# edited by ryosuke fukatani
78
# License: Apache 2.0
89
#-------------------------------------------------------------------------------
910

@@ -47,7 +48,7 @@ def __hash__(self):
4748
s = hash(tuple([getattr(self, a) for a in self.attr_names]))
4849
c = hash(self.children())
4950
return hash((s, c))
50-
51+
5152
################################################################################
5253
class Source(Node):
5354
attr_names = ('name',)
@@ -171,7 +172,7 @@ def children(self):
171172
nodelist = []
172173
if self.width: nodelist.append(self.width)
173174
return tuple(nodelist)
174-
175+
175176
class Input(Variable): pass
176177
class Output(Variable): pass
177178
class Inout(Variable): pass
@@ -347,11 +348,11 @@ class Power(Operator): pass
347348
class Times(Operator): pass
348349
class Divide(Operator): pass
349350
class Mod(Operator): pass
350-
################################################################################
351+
################################################################################
351352
# Level 3
352353
class Plus(Operator): pass
353354
class Minus(Operator): pass
354-
################################################################################
355+
################################################################################
355356
# Level 4
356357
class Sll(Operator): pass
357358
class Srl(Operator): pass
@@ -510,6 +511,8 @@ def children(self):
510511
if self.caselist: nodelist.extend(self.caselist)
511512
return tuple(nodelist)
512513

514+
class CasexStatement(CaseStatement): pass
515+
513516
class Case(Node):
514517
attr_names = ()
515518
def __init__(self, cond, statement):
@@ -540,7 +543,7 @@ def children(self):
540543
if self.statement: nodelist.append(self.statement)
541544
return tuple(nodelist)
542545

543-
class EventStatement(Node):
546+
class EventStatement(Node):
544547
attr_names = ()
545548
def __init__(self, senslist):
546549
self.senslist = senslist
@@ -589,7 +592,7 @@ def children(self):
589592
if self.parameterlist: nodelist.extend(self.parameterlist)
590593
if self.instances: nodelist.extend(self.instances)
591594
return tuple(nodelist)
592-
595+
593596
class Instance(Node):
594597
attr_names = ('name', 'module')
595598
def __init__(self, module, name, portlist, parameterlist, array=None):

pyverilog/vparser/lexer.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#-------------------------------------------------------------------------------
22
# lexer.py
3-
#
3+
#
44
# Lexical analyzer
55
#
66
# Copyright (C) 2013, Shinya Takamaeda-Yamazaki
7+
#
8+
# edited by ryosuke fukatani
9+
#
710
# License: Apache 2.0
811
#-------------------------------------------------------------------------------
912

@@ -45,9 +48,9 @@ def token(self):
4548
'MODULE', 'ENDMODULE', 'BEGIN', 'END', 'GENERATE', 'ENDGENERATE', 'GENVAR',
4649
'FUNCTION', 'ENDFUNCTION', 'TASK', 'ENDTASK',
4750
'INPUT', 'INOUT', 'OUTPUT', 'TRI', 'REG', 'WIRE', 'INTEGER', 'REAL', 'SIGNED',
48-
'PARAMETER', 'LOCALPARAM',
51+
'PARAMETER', 'LOCALPARAM',
4952
'ASSIGN', 'ALWAYS', 'SENS_OR', 'POSEDGE', 'NEGEDGE', 'INITIAL',
50-
'IF', 'ELSE', 'FOR', 'WHILE', 'CASE', 'ENDCASE', 'DEFAULT',
53+
'IF', 'ELSE', 'FOR', 'WHILE', 'CASE', 'CASEX', 'ENDCASE', 'DEFAULT',
5154
'WAIT', 'FOREVER', 'DISABLE', 'FORK', 'JOIN',
5255
)
5356

@@ -62,7 +65,7 @@ def token(self):
6265
'PLUS','MINUS','POWER','TIMES','DIVIDE','MOD',
6366
'NOT', 'OR', 'NOR', 'AND', 'NAND', 'XOR', 'XNOR',
6467
'LOR', 'LAND', 'LNOT',
65-
'LSHIFTA', 'RSHIFTA', 'LSHIFT', 'RSHIFT',
68+
'LSHIFTA', 'RSHIFTA', 'LSHIFT', 'RSHIFT',
6669
'LT', 'GT', 'LE', 'GE', 'EQ', 'NE', 'EQL', 'NEL',
6770
'COND', # ?
6871
'EQUALS',
@@ -78,7 +81,7 @@ def token(self):
7881
'INTNUMBER_OCT', 'SIGNED_INTNUMBER_OCT',
7982
'INTNUMBER_BIN', 'SIGNED_INTNUMBER_BIN',
8083
'LPAREN','RPAREN', 'LBRACKET', 'RBRACKET', 'LBRACE', 'RBRACE',
81-
'DELAY', 'DOLLER',
84+
'DELAY', 'DOLLER',
8285
)
8386

8487
skipped = (
@@ -188,7 +191,7 @@ def t_COMMENTOUT(self, t):
188191
octal_escape = r"""([0-7]{1,3})"""
189192
hex_escape = r"""(x[0-9a-fA-F]+)"""
190193
escape_sequence = r"""(\\("""+simple_escape+'|'+octal_escape+'|'+hex_escape+'))'
191-
string_char = r"""([^"\\\n]|"""+escape_sequence+')'
194+
string_char = r"""([^"\\\n]|"""+escape_sequence+')'
192195
string_literal = '"'+string_char+'*"'
193196

194197
identifier = r"""(([a-zA-Z_])([a-zA-Z_0-9$])*)|((\\\S)(\S)*)"""
@@ -201,38 +204,38 @@ def t_STRING_LITERAL(self, t):
201204
def t_FLOATNUMBER(self, t):
202205
return t
203206

204-
@TOKEN(bin_number)
205-
def t_INTNUMBER_BIN(self, t):
206-
return t
207-
208207
@TOKEN(signed_bin_number)
209208
def t_SIGNED_INTNUMBER_BIN(self, t):
210209
return t
211210

212-
@TOKEN(octal_number)
213-
def t_INTNUMBER_OCT(self, t):
211+
@TOKEN(bin_number)
212+
def t_INTNUMBER_BIN(self, t):
214213
return t
215214

216215
@TOKEN(signed_octal_number)
217216
def t_SIGNED_INTNUMBER_OCT(self, t):
218217
return t
219218

220-
@TOKEN(hex_number)
221-
def t_INTNUMBER_HEX(self, t):
222-
return t
219+
@TOKEN(octal_number)
220+
def t_INTNUMBER_OCT(self, t):
221+
return t
223222

224223
@TOKEN(signed_hex_number)
225224
def t_SIGNED_INTNUMBER_HEX(self, t):
226225
return t
227226

228-
@TOKEN(decimal_number)
229-
def t_INTNUMBER_DEC(self, t):
230-
return t
227+
@TOKEN(hex_number)
228+
def t_INTNUMBER_HEX(self, t):
229+
return t
231230

232231
@TOKEN(signed_decimal_number)
233232
def t_SIGNED_INTNUMBER_DEC(self, t):
234233
return t
235234

235+
@TOKEN(decimal_number)
236+
def t_INTNUMBER_DEC(self, t):
237+
return t
238+
236239
@TOKEN(identifier)
237240
def t_ID(self, t):
238241
t.type = self.reserved.get(t.value, 'ID')
@@ -258,7 +261,7 @@ def _find_tok_column(self, token):
258261
if self.lexer.lexdata[i] == '\n': break
259262
i -= 1
260263
return (token.lexpos - i) + 1
261-
264+
262265
def _make_tok_location(self, token):
263266
return (token.lineno, self._find_tok_column(token))
264267

0 commit comments

Comments
 (0)