e897d63944df3d8eb7debc4daf06e2c2107674f8
2 # Copyright (C) 2018 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
4 """ identify registers used in riscv/insns/*.h and create code
5 that can be used in spike at runtime
7 the design of spike assumes that once an opcode is identified,
8 the role of decoding the instruction is implicitly rolled into
9 and included inside the function that emulates that opcode.
11 however there may be circumstances where the behaviour of an
12 instruction has to change depending on "tags" associated with
13 the registers (security extensions, simple-v extension).
15 therefore this code walks the instruction implementations
16 in riscv/insns/*.h looking for register usage patterns.
17 the resultant table can be used *prior* to the emulation,
18 without having to manually maintain such a table.
24 insns_dir
= "./riscv/insns"
26 if len(sys
.argv
) == 2:
27 fullfname
= sys
.argv
[1]
28 pth
, fname
= os
.path
.split(fullfname
)
30 return [(fullfname
, insn
)]
33 for fname
in os
.listdir(insns_dir
):
34 if not fname
.endswith(".h"):
36 if fname
.startswith("regs_"):
39 res
.append((os
.path
.join(insns_dir
, fname
), insn
))
42 cintpatterns
= [ 'WRITE_RVC_RS1S', 'WRITE_RVC_RS2S',
43 'RVC_RS1', 'RVC_RS2', 'RVC_RS1S', 'RVC_RS2S', ]
44 cfloatpatterns
= [ 'WRITE_RVC_FRS2S', 'RVC_FRS2 ', 'RVC_FRS2S ']
45 intpatterns
= ['WRITE_RD' , 'RS1', 'RS2', 'RS3']
46 floatpatterns
= ['WRITE_FRD', 'FRS1', 'FRS2', 'FRS3']
47 patterns
= intpatterns
+ floatpatterns
48 patterns
+= cintpatterns
49 patterns
+= cfloatpatterns
51 allfloats
= floatpatterns
+ cfloatpatterns
52 floatmask
= (1<<len(allfloats
)-1)
53 allints
= intpatterns
+ cintpatterns
[2:]
55 skip
= '#define USING_NOREGS\n' \
56 '#define REGS_PATTERN 0x0\n'
57 def find_registers(fname
):
58 # HACK! macro-skipping csr* instructions too painful
59 if 'csr' in fname
or 'lui' in fname
:
62 isintfloat
= 0x0 + floatmask
<< len(allints
)
63 with
open(fname
) as f
:
65 for pattern
in patterns
:
69 if pattern
.startswith('R') and x
!= 0 and f
[x
-1] == 'F':
70 # botch-job/hack: RS1 also matches against FRS1 (etc.)
71 # check letter before match: if "F", skip it.
73 if pattern
.startswith('R') and x
!= 0 and f
[x
-1] == '_':
74 # RS1 also matches against RVC_RS1 (etc.)
75 # check letter before match: if "_", skip it.
77 if pattern
.startswith('FR') and x
!= 0 and f
[x
-1] == '_':
78 # RS1 also matches against RVC_FRS1 (etc.)
79 # check letter before match: if "_", skip it.
81 if 'RVC_' in pattern
and f
[x
+len(pattern
)] == 'S':
82 # RVC_RS2S also matches against RVC_RS2 (etc.)
83 # check letter at end of match: if "S", skip it.
86 if p
.startswith('WRITE_'):
88 if pattern
in allints
:
89 idx
= allints
.index(pattern
)
90 isintfloat
+= 1 << idx
91 if pattern
in allfloats
:
92 idx
= allfloats
.index(pattern
)
93 isintfloat
&= ~
(1 << (idx
+len(allints
)))
94 res
.append('#define USING_REG_%s' % p
)
97 res
.append('#define REGS_PATTERN 0x%x' % isintfloat
)
100 if __name__
== '__main__':
102 for (fname
, insn
) in files
:
103 regsname
= "regs_%s.h" % insn
104 regsname
= os
.path
.join(insns_dir
, regsname
)
105 with
open(regsname
, "w") as f
:
106 f
.write(find_registers(fname
))