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 openpower
.decoder
.power_decoder
import create_pdecode
19 from nmigen
.sim
import Simulator
, Delay
20 from nmigen
import Module
, Signal
22 from openpower
.decoder
.pseudo
.parser
import GardenSnakeCompiler
23 from openpower
.decoder
.selectable_int
import SelectableInt
, selectconcat
24 from openpower
.decoder
.isa
.caller
import GPR
, Mem
27 ####### Test code #######
32 index <- (RS)[8*i:8*i+7]
33 RA <- [0]*56 || perm[0:7]
38 if index < 64 then index <- 0
49 index <- (RS)[8*i:8*i+7]
54 RA <- [0]*56|| perm[0:7]
61 if (RS)[63-n] = 0b1 then
71 else if a > EXTS(SI) then
80 in_range <- ((x | y) &
82 in_range <- (x + y) - (a + b)
87 src1 <- EXTZ((RA)[56:63])
89 in_range <- src21lo <= src1 & src1 <= src21hi
103 RT <- (RA) + EXTS(SI || [0]*16)
113 RT <- (load_data[56:63] || load_data[48:55]
114 || load_data[40:47] || load_data[32:39]
115 || load_data[24:31] || load_data[16:23]
116 || load_data[8:15] || load_data[0:7])
127 MEM(EA, 1) <- (RS)[56:63]
133 MEM(EA, 4) <- GPR(r)[32:63]
143 ctr_ok <- BO[2] | ((CTR[M:63] != 0) ^ BO[3])
144 cond_ok <- BO[0] | ¬(CR[BI+32] ^ BO[1])
148 if RA = 0 then EA <- 0
150 if NB = 0 then n <- 32
158 GPR(r)[i:i+7] <- MEM(EA, 1)
160 if i = 64 then i <- 32
181 RT <- 0x0001_a000_0000_0000
219 prod[0:XLEN-1] <- MULS((RA)[XLEN/2:XLEN-1], (RB)[XLEN/2:XLEN-1])
220 RT[XLEN/2:XLEN-1] <- prod[0:(XLEN/2)-1]
221 RT[0:(XLEN/2)-1] <- undefined(prod[0:(XLEN/2)-1])
252 l
.append(1 if (num
& (1 << i
)) else 0)
257 def get_reg_hex(reg
):
258 return hex(reg
.value
)
261 def convert_to_pure_python(pcode
, helper
=False, filename
="string"):
263 gsc
= GardenSnakeCompiler(form
=None, incl_carry
=False, helper
=helper
)
265 tree
= gsc
.compile(pcode
, mode
="exec", filename
=filename
)
266 tree
= ast
.fix_missing_locations(tree
)
267 return astor
.to_source(tree
)
270 def convert_to_python(pcode
, form
, incl_carry
, helper
=False, filename
="string"):
273 gsc
= GardenSnakeCompiler(form
=form
, incl_carry
=incl_carry
, helper
=helper
)
275 tree
= gsc
.compile(pcode
, mode
="exec", filename
=filename
)
276 tree
= ast
.fix_missing_locations(tree
)
277 regsused
= {'read_regs': gsc
.parser
.read_regs
,
278 'write_regs': gsc
.parser
.write_regs
,
279 'uninit_regs': gsc
.parser
.uninit_regs
,
280 'special_regs': gsc
.parser
.special_regs
,
281 'op_fields': gsc
.parser
.op_fields
}
282 return astor
.to_source(tree
), regsused
285 def check_in_gitignore(output_file
):
286 gitignore_file
= os
.path
.join(os
.path
.dirname(output_file
), ".gitignore")
287 base_name
= os
.path
.basename(output_file
)
288 with
open(gitignore_file
, "r") as f
:
289 for l
in f
.readlines():
291 if "/" + base_name
== l
:
294 if base_name
.endswith(".py"):
296 raise ValueError(f
"generated output file not in .gitignore:\n"
297 f
"output file: {output_file}\n"
298 f
".gitignore file: {gitignore_file}")
303 gsc
= GardenSnakeCompiler(debug
=True)
308 gsc
.gpr
= GPR(None, None, None, gsc
.regfile
)
311 _compile
= gsc
.compile
313 tree
= _compile(code
, mode
="single", filename
="string")
314 tree
= ast
.fix_missing_locations(tree
)
315 print(ast
.dump(tree
))
318 print(astor
.dump_tree(tree
))
320 source
= astor
.to_source(tree
)
325 # Set up the GardenSnake run-time environment
328 print("-->", " ".join(map(str, args
)))
330 from openpower
.decoder
.helpers
import (EXTS64
, EXTZ64
, ROTL64
, ROTL32
, MASK
,
331 trunc_div
, trunc_rem
)
337 d
["trunc_div"] = trunc_div
338 d
["trunc_rem"] = trunc_rem
339 d
["SelectableInt"] = SelectableInt
340 d
["concat"] = selectconcat
343 d
["memassign"] = gsc
.mem
.memassign
346 gsc
.gpr
.set_form(form
)
347 getform
= gsc
.parser
.sd
.sigforms
[form
]._asdict
()
348 #print ("getform", form)
349 # for k, f in getform.items():
353 compiled_code
= compile(source
, mode
="exec", filename
="<string>")
357 instruction
= Signal(32)
359 m
.submodules
.decode
= decode
= gsc
.parser
.sd
360 comb
+= decode
.raw_opcode_in
.eq(instruction
)
367 print("0x{:X}".format(ins
& 0xffffffff))
369 # ask the decoder to decode this binary data (endian'd)
370 yield decode
.bigendian
.eq(0) # little / big?
371 yield instruction
.eq(ins
) # raw binary instr.
374 # uninitialised regs, drop them into dict for function
375 for rname
in gsc
.parser
.uninit_regs
:
376 d
[rname
] = SelectableInt(0, 64) # uninitialised (to zero)
377 print("uninitialised", rname
, hex(d
[rname
].value
))
379 # read regs, drop them into dict for function
380 for rname
in gsc
.parser
.read_regs
:
381 regidx
= yield getattr(decode
.sigforms
['X'], rname
)
382 d
[rname
] = gsc
.gpr
[regidx
] # contents of regfile
383 d
["_%s" % rname
] = regidx
# actual register value
384 print("read reg", rname
, regidx
, hex(d
[rname
].value
))
386 exec(compiled_code
, d
) # code gets executed here in dict "d"
389 print(d
.keys()) # shows the variables that may have been created
391 print(decode
.sigforms
['X'])
392 x
= yield decode
.sigforms
['X'].RS
393 ra
= yield decode
.sigforms
['X'].RA
394 rb
= yield decode
.sigforms
['X'].RB
395 print("RA", ra
, d
['RA'])
396 print("RB", rb
, d
['RB'])
399 for wname
in gsc
.parser
.write_regs
:
402 print("write regs", regidx
, wname
, d
[wname
], reg
)
403 gsc
.gpr
[regidx
] = d
[wname
]
405 sim
.add_process(process
)
406 with sim
.write_vcd("simulator.vcd", "simulator.gtkw",
407 traces
=decode
.ports()):
412 for i
in range(0, len(gsc
.mem
.mem
), 16):
415 hexstr
.append("%02x" % gsc
.mem
.mem
[i
+j
])
416 hexstr
= ' '.join(hexstr
)
417 print("mem %4x" % i
, hexstr
)
420 if __name__
== '__main__':