-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathriscv-filter.py
executable file
·36 lines (28 loc) · 1021 Bytes
/
riscv-filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
import sys
import tempfile
import subprocess
def main():
fh_in = sys.stdin
fh_out = sys.stdout
while True:
l = fh_in.readline()
if not l:
return 0
if "x" in l:
fh_out.write(l)
fh_out.flush()
continue
obj_temp = tempfile.NamedTemporaryFile(delete=False, mode='w')
with tempfile.NamedTemporaryFile(delete=False, mode='w') as asm_temp:
asm_temp.write(".word 0x%s\n" % l)
asm_temp.flush()
subprocess.run(["riscv64-elf-as", "-march=rv32i", "-o", obj_temp.name, asm_temp.name])
result = subprocess.run(["riscv64-elf-objdump", "-d", obj_temp.name], capture_output=True)
lastline = result.stdout.splitlines()[-1]
chunks = lastline.decode().split('\t')
opcodes = " ".join(chunks[2:])
fh_out.write("%s\n" % opcodes)
fh_out.flush()
if __name__ == '__main__':
sys.exit(main())