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 of instructions too painful
59 for notparallel
in ['csr', 'lui', 'c_j', 'wfi', 'auipc',
60 'dret', 'uret', 'mret', 'sret']:
61 if notparallel
in fname
:
64 isintfloat
= 0x0 + floatmask
<< len(allints
)
65 with
open(fname
) as f
:
67 for pattern
in patterns
:
71 if pattern
.startswith('R') and x
!= 0 and f
[x
-1] == 'F':
72 # botch-job/hack: RS1 also matches against FRS1 (etc.)
73 # check letter before match: if "F", skip it.
75 if pattern
.startswith('R') and x
!= 0 and f
[x
-1] == '_':
76 # RS1 also matches against RVC_RS1 (etc.)
77 # check letter before match: if "_", skip it.
79 if pattern
.startswith('FR') and x
!= 0 and f
[x
-1] == '_':
80 # RS1 also matches against RVC_FRS1 (etc.)
81 # check letter before match: if "_", skip it.
83 if 'RVC_' in pattern
and f
[x
+len(pattern
)] == 'S':
84 # RVC_RS2S also matches against RVC_RS2 (etc.)
85 # check letter at end of match: if "S", skip it.
88 if p
.startswith('WRITE_'):
90 if pattern
in allints
:
91 idx
= allints
.index(pattern
)
92 isintfloat
+= 1 << idx
93 if pattern
in allfloats
:
94 idx
= allfloats
.index(pattern
)
95 isintfloat
&= ~
(1 << (idx
+len(allints
)))
96 res
.append('#define USING_REG_%s' % p
)
99 res
.append('#define REGS_PATTERN 0x%x' % isintfloat
)
100 return '\n'.join(res
)
102 if __name__
== '__main__':
104 for (fname
, insn
) in files
:
105 regsname
= "regs_%s.h" % insn
106 regsname
= os
.path
.join(insns_dir
, regsname
)
107 with
open(regsname
, "w") as f
:
108 f
.write(find_registers(fname
))