1 # Based on GardenSnake - a parser generator demonstration program
2 # GardenSnake was released into the Public Domain by Andrew Dalke.
4 # Portions of this work are derived from Python's Grammar definition
5 # and may be covered under the Python copyright and license
7 # Andrew Dalke / Dalke Scientific Software, LLC
8 # 30 August 2006 / Cape Town, South Africa
10 # Modifications for inclusion in PLY distribution
12 from pprint
import pprint
14 from ply
import lex
, yacc
18 from soc
.decoder
.power_decoder
import create_pdecode
19 from nmigen
.back
.pysim
import Simulator
, Delay
20 from nmigen
import Module
, Signal
22 from soc
.decoder
.pseudo
.parser
import GardenSnakeCompiler
23 from soc
.decoder
.selectable_int
import SelectableInt
, selectconcat
25 ####### Test code #######
30 index <- (RS)[8*i:8*i+7]
31 RA <- [0]*56 || perm[0:7]
36 if index < 64 then index <- 0
47 index <- (RS)[8*i:8*i+7]
52 RA <- [0]*56|| perm[0:7]
58 if (RS)[63-n] = 0b1 then
68 else if a > EXTS(SI) then
77 in_range <- ((x | y) &
79 in_range <- (x + y) - (a + b)
90 l
.append(1 if (num
& (1<<i
)) else 0)
100 def __init__(self
, sd
, regfile
):
103 self
.regfile
= regfile
105 self
[i
] = SelectableInt(0, 64)
107 def set_form(self
, form
):
110 def ___getitem__(self
, attr
):
111 print ("GPR getitem", attr
)
112 getform
= self
.sd
.sigforms
[self
.form
]
113 rnum
= getattr(getform
, attr
)
114 print ("GPR get", rnum
, rnum
, dir(rnum
))
116 print (l
[0]._as
_const
())
118 #print (x, x.value, dir(x))
119 #print (x.value, dir(x.value))
121 return self
.regfile
[rnum
]
124 def convert_to_python(pcode
):
126 gsc
= GardenSnakeCompiler()
128 tree
= gsc
.compile(pcode
, mode
="exec", filename
="string")
129 tree
= ast
.fix_missing_locations(tree
)
130 return astor
.to_source(tree
)
135 gsc
= GardenSnakeCompiler()
137 # XXX unused! see GPR instead
141 gsc
.gpr
= GPR(gsc
.parser
.sd
, gsc
.regfile
)
143 _compile
= gsc
.compile
145 tree
= _compile(code
, mode
="single", filename
="string")
146 tree
= ast
.fix_missing_locations(tree
)
147 print ( ast
.dump(tree
) )
150 print (astor
.dump_tree(tree
))
152 source
= astor
.to_source(tree
)
157 # Set up the GardenSnake run-time environment
160 print ("-->", " ".join(map(str,args
)))
162 from soc
.decoder
.helpers
import (EXTS64
, EXTZ64
, ROTL64
, ROTL32
, MASK
,)
168 d
["SelectableInt"] = SelectableInt
169 d
["concat"] = selectconcat
173 gsc
.gpr
.set_form(form
)
174 getform
= gsc
.parser
.sd
.sigforms
[form
]._asdict
()
175 #print ("getform", form)
176 #for k, f in getform.items():
180 compiled_code
= compile(source
, mode
="exec", filename
="<string>")
184 instruction
= Signal(32)
186 m
.submodules
.decode
= decode
= gsc
.parser
.sd
187 comb
+= decode
.raw_opcode_in
.eq(instruction
)
194 print("0x{:X}".format(ins
& 0xffffffff))
196 # ask the decoder to decode this binary data (endian'd)
197 yield decode
.bigendian
.eq(0) # little / big?
198 yield instruction
.eq(ins
) # raw binary instr.
201 # read regs, drop them into dict for function
202 for rname
in gsc
.parser
.uninit_regs
:
203 d
[rname
] = SelectableInt(0, 64) # uninitialised (to zero)
204 print ("uninitialised", rname
, get_reg_hex(d
[rname
]))
206 for rname
in gsc
.parser
.read_regs
:
207 regidx
= yield getattr(decode
.sigforms
['X'], rname
)
208 d
[rname
] = gsc
.gpr
[regidx
]
209 print ("read reg", rname
, regidx
, get_reg_hex(d
[rname
]))
211 exec (compiled_code
, d
)
216 print (decode
.sigforms
['X'])
217 x
= yield decode
.sigforms
['X'].RS
218 ra
= yield decode
.sigforms
['X'].RA
219 print ("RA", ra
, d
['RA'])
222 for wname
in gsc
.parser
.write_regs
:
224 print ("write regs", wname
, d
[wname
], reg
)
226 gsc
.gpr
[regidx
] = d
[wname
]
228 sim
.add_process(process
)
229 with sim
.write_vcd("simulator.vcd", "simulator.gtkw",
230 traces
=decode
.ports()):
233 for i
in range(len(gsc
.gpr
)):
234 print ("regfile", i
, get_reg_hex(gsc
.gpr
[i
]))
237 if __name__
== '__main__':