aboutsummaryrefslogtreecommitdiffstats
path: root/format.py
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2021-05-25 15:14:24 +0000
committers-ol <s+removethis@s-ol.nu>2021-05-25 15:14:24 +0000
commit661a79240c8bafb3c99f3080c546689d025738f0 (patch)
tree3168b7acd14a8d627ca941bdd5a81c1d17268078 /format.py
parentclose to working AUIPC+JALR jump (diff)
downloadsubv-661a79240c8bafb3c99f3080c546689d025738f0.tar.gz
subv-661a79240c8bafb3c99f3080c546689d025738f0.zip
apply "black" formatting
Diffstat (limited to 'format.py')
-rwxr-xr-xformat.py117
1 files changed, 67 insertions, 50 deletions
diff --git a/format.py b/format.py
index 5776da8..62c35b2 100755
--- a/format.py
+++ b/format.py
@@ -66,24 +66,29 @@ main:
import subv
import bits
+
def _test_format(words):
- return ' '.join(map(str, words))
+ return " ".join(map(str, words))
+
def ref_slice(ref, hi, lo):
if hi < lo:
raise ValueError("cant slice reverse range")
- elif hi >= ref['size']:
- raise ValueError("cant slice [{}:{}] from {} bit value".format(hi, lo, ref['size']))
+ elif hi >= ref["size"]:
+ raise ValueError(
+ "cant slice [{}:{}] from {} bit value".format(hi, lo, ref["size"])
+ )
return {
**ref,
- 'hi': hi,
- 'lo': lo,
- 'size': hi - lo + 1,
+ "hi": hi,
+ "lo": lo,
+ "size": hi - lo + 1,
}
+
def sign_trunc(val, size):
- """ truncate and sign-shrink a number literal.
+ """truncate and sign-shrink a number literal.
>>> sign_trunc(bits.i(4, 32), 8)
Bitfield(0x4, 8)
@@ -94,12 +99,13 @@ def sign_trunc(val, size):
>>> sign_trunc(bits.i(-0x11ff, 32), 8)
Bitfield(0x81, 8)
"""
- sign = val[val.size-1]
- rest = val[size-2:0]
+ sign = val[val.size - 1]
+ rest = val[size - 2 : 0]
return sign & rest
+
def pack_u(instr):
- """ verify & pack U-type instructions.
+ """verify & pack U-type instructions.
>>> _test_format(pack_u([(0x37, 'lui'), (5, 'rd', 't0'), (0x10010, 'imm20')]))
'37/7 05/5 10010/20'
@@ -117,16 +123,17 @@ def pack_u(instr):
"""
(op, rd, imm) = instr
op = bits.u(subv.untag(op), 7)
- rd = bits.u(subv.untag(rd, 'rd'), 5)
+ rd = bits.u(subv.untag(rd, "rd"), 5)
if subv.is_reference(imm):
imm = bits.ref(imm, slice(31, 12))
else:
- imm = bits.i(subv.untag(imm, 'imm20'), 20)
+ imm = bits.i(subv.untag(imm, "imm20"), 20)
return [op, rd, imm]
+
def pack_i(instr):
- """ verify & pack I-type instructions.
+ """verify & pack I-type instructions.
>>> _test_format(pack_i([(0x13, 'opi'), (0, 'subop', 'add'), (6, 'rd', 't1'), (0, 'rs', 'x0'), (0x65, 'imm12')]))
'13/7 06/5 0/3 00/5 065/12'
@@ -136,22 +143,23 @@ def pack_i(instr):
"""
(op, sub, rd, rs, imm) = instr
op_tag = op[1]
- op = bits.u(subv.untag(op), 7)
- sub = bits.u(subv.untag(sub, 'subop'), 3)
- rd = bits.u(subv.untag(rd, 'rd'), 5)
- rs = bits.u(subv.untag(rs, 'rs'), 5)
+ op = bits.u(subv.untag(op), 7)
+ sub = bits.u(subv.untag(sub, "subop"), 3)
+ rd = bits.u(subv.untag(rd, "rd"), 5)
+ rs = bits.u(subv.untag(rs, "rs"), 5)
if subv.is_reference(imm):
- if op_tag == 'jalr':
+ if op_tag == "jalr":
imm = bits.ref(imm, slice(12, 1))
else:
imm = bits.ref(imm, slice(11, 0))
else:
- imm = bits.i(subv.untag(imm, 'imm12'), 12)
+ imm = bits.i(subv.untag(imm, "imm12"), 12)
return [op, rd, sub, rs, imm]
+
def pack_s(instr):
- """ verify & pack S-type instructions.
+ """verify & pack S-type instructions.
>>> _test_format(pack_s([(0x23, 'store'), (2, 'subop', 'word'), (5, 'rs', 't0'), (6, 'rs', 't1'), (0, 'off12')]))
'23/7 00/5 2/3 05/5 06/5 00/7'
@@ -160,22 +168,23 @@ def pack_s(instr):
'23/7 home/off12/[4:0] 2/3 05/5 06/5 home/off12/[11:5]'
"""
(op, sub, rs1, rs2, imm) = instr
- op = bits.u(subv.untag(op), 7)
- sub = bits.u(subv.untag(sub, 'subop'), 3)
- rs1 = bits.u(subv.untag(rs1, 'rs'), 5)
- rs2 = bits.u(subv.untag(rs2, 'rs'), 5)
+ op = bits.u(subv.untag(op), 7)
+ sub = bits.u(subv.untag(sub, "subop"), 3)
+ rs1 = bits.u(subv.untag(rs1, "rs"), 5)
+ rs2 = bits.u(subv.untag(rs2, "rs"), 5)
if subv.is_reference(imm):
imm = bits.ref(imm, slice(11, 0))
else:
- imm = bits.i(subv.untag(imm, 'off12'), 12)
+ imm = bits.i(subv.untag(imm, "off12"), 12)
imm_lo = imm[4:0]
imm_hi = imm[11:5]
return [op, imm_lo, sub, rs1, rs2, imm_hi]
+
def pack_j(instr):
- """ verify & pack J-type instructions.
+ """verify & pack J-type instructions.
>>> _test_format(pack_j([(0x6f, 'jal'), (0, 'rd', 'x0'), (0, 'off20')]))
'6f/7 00/5 00/8 0/1 000/10 0/1'
@@ -188,12 +197,12 @@ def pack_j(instr):
"""
(op, rd, imm) = instr
op = bits.u(subv.untag(op), 7)
- rd = bits.u(subv.untag(rd, 'rd'), 5)
+ rd = bits.u(subv.untag(rd, "rd"), 5)
if subv.is_reference(imm):
imm = bits.ref(imm, slice(20, 1))
else:
- imm = bits.i(subv.untag(imm, 'off20'), 20)
+ imm = bits.i(subv.untag(imm, "off20"), 20)
imm_lo = imm[9:0]
imm_11 = imm[10]
@@ -202,8 +211,9 @@ def pack_j(instr):
return [op, rd, imm_hi, imm_11, imm_lo, imm_20]
+
def pack_b(instr):
- """ verify & pack B-type instructions.
+ """verify & pack B-type instructions.
>>> _test_format(pack_b([(0x63, 'branch'), (0, 'subop', '=='), (6, 'rs'), (0, 'rs'), (0, 'off12')]))
'63/7 0/1 0/4 0/3 06/5 00/5 00/6 0/1'
@@ -215,15 +225,15 @@ def pack_b(instr):
'63/7 home/off12/[11:11] home/off12/[4:1] 0/3 06/5 00/5 home/off12/[10:5] home/off12/[12:12]'
"""
(op, sub, rs1, rs2, imm) = instr
- op = bits.u(subv.untag(op), 7)
- sub = bits.u(subv.untag(sub, 'subop'), 3)
- rs1 = bits.u(subv.untag(rs1, 'rs'), 5)
- rs2 = bits.u(subv.untag(rs2, 'rs'), 5)
+ op = bits.u(subv.untag(op), 7)
+ sub = bits.u(subv.untag(sub, "subop"), 3)
+ rs1 = bits.u(subv.untag(rs1, "rs"), 5)
+ rs2 = bits.u(subv.untag(rs2, "rs"), 5)
if subv.is_reference(imm):
imm = bits.ref(imm, slice(12, 1))
else:
- imm = bits.i(subv.untag(imm, 'off12'), 12)
+ imm = bits.i(subv.untag(imm, "off12"), 12)
imm_lo = imm[3:0]
imm_md = imm[9:4]
@@ -232,39 +242,46 @@ def pack_b(instr):
return [op, imm_11, imm_lo, sub, rs1, rs2, imm_md, imm_12]
+
instr_map = {
# 'opr': (pack_r, 0x33),
- 'load': (pack_i, 0x03),
- 'opi': (pack_i, 0x13),
- 'jalr': (pack_i, 0x67),
- 'store': (pack_s, 0x23),
- 'branch': (pack_b, 0x63),
- 'lui': (pack_u, 0x37),
- 'auipc': (pack_u, 0x17),
- 'jal': (pack_j, 0x6f),
+ "load": (pack_i, 0x03),
+ "opi": (pack_i, 0x13),
+ "jalr": (pack_i, 0x67),
+ "store": (pack_s, 0x23),
+ "branch": (pack_b, 0x63),
+ "lui": (pack_u, 0x37),
+ "auipc": (pack_u, 0x17),
+ "jal": (pack_j, 0x6F),
}
+
@subv.with_parsed_lines
def format(iter):
for segment, line in iter:
- if line['type'] == 'instr' and segment == 'code':
- op = line['instr'][0]
- assert len(op) == 2, 'instruction without op label: {}'.format(op)
+ if line["type"] == "instr" and segment == "code":
+ op = line["instr"][0]
+ assert len(op) == 2, "instruction without op label: {}".format(op)
(op, label) = op
if label not in instr_map:
raise ValueError("unknown instruction label: {}".format(label))
(formatter, expected) = instr_map[label]
if op != expected:
- raise ValueError("opcode {} doesn't match label {} (expected {})"
- .format(op, label, expected))
+ raise ValueError(
+ "opcode {} doesn't match label {} (expected {})".format(
+ op, label, expected
+ )
+ )
- line['instr'] = formatter(line['instr'])
+ line["instr"] = formatter(line["instr"])
line = yield subv.format(line)
else:
- line = yield line['raw']
+ line = yield line["raw"]
+
-if __name__ == '__main__':
+if __name__ == "__main__":
import sys
+
for line in format(sys.stdin):
print(line)