-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpp.py
executable file
·72 lines (64 loc) · 2.26 KB
/
pp.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""The pre-processor of the extended instruction."""
import sys
import os
max_width = 16
def padspace(s, width):
return s + ' ' * (width - len(s))
def binary(name, ty, args, op0, op1, funct3):
def do_operand(res, arg, args, ty):
if arg in args:
if arg != 'imm':
res.append(arg)
else:
imm_ty = {'U': 'imm20', 'I': 'imm12', 'S': 'imm12hi imm12lo'}
res.append(imm_ty[ty])
else:
rule = {'I': {'rs1': '19..15=0', 'rs2': '', 'rd': '11..7=0', 'imm': '31..20=0'},
'S': {'rs1': '19..15=0', 'rs2': '24..20=0', 'rd': '', 'imm': '31..25=0 11..7=0'},
'U': {'rs1': '', 'rs2': '', 'rd': '11..7=0', 'imm': '31..12=0'}}
res.append(rule[ty][arg])
res = [name]
args = args.split(',')
do_operand(res, 'rs1', args, ty)
do_operand(res, 'rs2', args, ty)
do_operand(res, 'imm', args, ty)
res.append('14..12=%s' % funct3)
do_operand(res, 'rd', args, ty)
res.append('6..5=%s' % op0)
res.append('4..2=%s' % op1)
res.append('1..0=3')
return ' '.join(res)
def asmtext(name, ty, args, op0, op1, funct3):
args = args.split(',')
operands = []
if 'rd' in args:
operands.append('d')
if 'rs1' in args:
operands.append('s')
if 'rs2' in args:
operands.append('t')
if 'imm' in args:
imm_ty = {'U': 'u', 'I': 'j', 'S': 'q'}
operands.append(imm_ty[ty])
res = []
res.append(padspace('"%s"' % name, 16))
res.append(["INSN_CLASS_I", '{"I", 0}'][compat_mode])
res.append(padspace('"%s"' % ','.join(operands), 8))
res.append(padspace('MATCH_' + name.upper(), 24))
res.append(padspace('MASK_' + name.upper(), 24))
return '{%s, 0, %s, %s, %s, %s, match_opcode, 0},' % tuple(res)
args = sys.argv
compat_mode = os.getenv('COMPAT') is not None
with open(args[1], 'r') as ext, \
open(args[2], 'w') as afile, \
open(args[3], 'w') as bfile:
for raw in ext.readlines():
raw = raw.strip()
if '#' in raw:
raw = raw[:raw.index('#')]
if not raw:
continue
raw = [i for i in raw.split() if i]
afile.write(binary(*raw) + '\n')
bfile.write(asmtext(*raw) + '\n')