aboutsummaryrefslogtreecommitdiffstats
path: root/format.py
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2021-05-24 16:21:14 +0000
committers-ol <s+removethis@s-ol.nu>2021-05-24 16:21:14 +0000
commitacfaab0c3ffe04e7382e7095d9a19c86d80c3a66 (patch)
treebc33043d70ae9d5a4aab4655efbbe7f52f020a1f /format.py
parentsupport offsets on labels (diff)
downloadsubv-acfaab0c3ffe04e7382e7095d9a19c86d80c3a66.tar.gz
subv-acfaab0c3ffe04e7382e7095d9a19c86d80c3a66.zip
turn bits into a class
Diffstat (limited to 'format.py')
-rwxr-xr-xformat.py50
1 files changed, 26 insertions, 24 deletions
diff --git a/format.py b/format.py
index 9e0ead0..58b7708 100755
--- a/format.py
+++ b/format.py
@@ -63,6 +63,8 @@ main:
6f/7 00/5 main[19:12]/off8 main[11:11]/off1 main[10:1]/off10 main[20:20]/off1
"""
+from operator import __and__
+from functools import reduce
import subv
import bits
@@ -80,7 +82,7 @@ def ref_slice(ref, hi, lo):
}
def default_slice(val, hi, lo):
- """ add a default slice spec to labels if missing,
+ """ add a default slice spec to labels if missing.
>>> default_slice(('label', 'imm12'), 12, 1)
('label[12:1]', 'imm12')
@@ -99,8 +101,8 @@ def default_slice(val, hi, lo):
"""
parsed = subv.parse_reference(val)
if 'hi' not in parsed:
- parsed['hi'] = hi
- parsed['lo'] = lo
+ parsed['hi'] = hi
+ parsed['lo'] = lo
ref = subv.format_reference(parsed)
p_size = parsed['hi'] - parsed['lo'] + 1
@@ -126,8 +128,8 @@ def slice_bit_or_ref(val, hi, lo):
...
AssertionError: cant slice label w/o bounds
"""
- if not isinstance(val[0], str):
- return bits.slice(val, hi, lo)
+ if isinstance(val, bits.Bitfield):
+ return val[hi:lo]
ref = subv.parse_reference(val)
assert 'hi' in ref, "cant slice label w/o bounds"
@@ -148,13 +150,13 @@ def pack_u(instr):
""" verify & pack U-type instructions.
>>> pack_u([(0x37, 'lui'), (5, 'rd', 't0'), (0x10010, 'imm20')])
- [(55, 7), (5, 5), (65552, 20)]
+ [Bitfield(0x37, 7), Bitfield(0x5, 5), Bitfield(0x10010, 20)]
>>> pack_u([(0x37, 'lui'), (5, 'rd', 't0'), ('pos', 'imm20')])
- [(55, 7), (5, 5), ('pos[31:12]', 'imm20')]
+ [Bitfield(0x37, 7), Bitfield(0x5, 5), ('pos[31:12]', 'imm20')]
>>> pack_u([(0x37, 'lui'), (5, 'rd', 't0'), ('pos[19:0]', 'imm20')])
- [(55, 7), (5, 5), ('pos[19:0]', 'imm20')]
+ [Bitfield(0x37, 7), Bitfield(0x5, 5), ('pos[19:0]', 'imm20')]
>>> pack_u([(0x37, 'lui'), (5, 'rd', 't0'), ('pos[31:0]', 'imm20')])
Traceback (most recent call last):
@@ -174,11 +176,11 @@ def pack_u(instr):
def pack_i(instr):
""" verify & pack I-type instructions.
- >>> pack_i([(0x13, 'opi'), (0, 'subop', 'add'), (6, 'rd', 't1'), (0, 'rs', 'x0'), (101, 'imm12')])
- [(19, 7), (6, 5), (0, 3), (0, 5), (101, 12)]
+ >>> reduce(__and__, pack_i([(0x13, 'opi'), (0, 'subop', 'add'), (6, 'rd', 't1'), (0, 'rs', 'x0'), (101, 'imm12')]))
+ Bitfield(0x26600065, 32)
>>> pack_i([(0x13, 'opi'), (0, 'subop', 'add'), (6, 'rd', 't1'), (0, 'rs', 'x0'), ('label', 'imm12')])
- [(19, 7), (6, 5), (0, 3), (0, 5), ('label[11:0]', 'imm12')]
+ [Bitfield(0x13, 7), Bitfield(0x6, 5), Bitfield(0x0, 3), Bitfield(0x0, 5), ('label[11:0]', 'imm12')]
"""
(op, sub, rd, rs, imm) = instr
op = bits.u(subv.untag(op), 7)
@@ -195,11 +197,11 @@ def pack_i(instr):
def pack_s(instr):
""" verify & pack S-type instructions.
- >>> pack_s([(0x23, 'store'), (2, 'subop', 'word'), (5, 'rs', 't0'), (6, 'rs', 't1'), (0, 'off12')])
- [(35, 7), (0, 5), (2, 3), (5, 5), (6, 5), (0, 7)]
+ >>> reduce(__and__, pack_s([(0x23, 'store'), (2, 'subop', 'word'), (5, 'rs', 't0'), (6, 'rs', 't1'), (0, 'off12')]))
+ Bitfield(0x46045300, 32)
>>> pack_s([(0x23, 'store'), (2, 'subop', 'word'), (5, 'rs', 't0'), (6, 'rs', 't1'), ('home', 'off12')])
- [(35, 7), ('home[4:0]', 'off5'), (2, 3), (5, 5), (6, 5), ('home[11:5]', 'off7')]
+ [Bitfield(0x23, 7), ('home[4:0]', 'off5'), Bitfield(0x2, 3), Bitfield(0x5, 5), Bitfield(0x6, 5), ('home[11:5]', 'off7')]
"""
(op, sub, rs1, rs2, imm) = instr
op = bits.u(subv.untag(op), 7)
@@ -219,14 +221,14 @@ def pack_s(instr):
def pack_j(instr):
""" verify & pack J-type instructions.
- >>> pack_j([(0x6f, 'jal'), (0, 'rd', 'x0'), (0, 'off20')])
- [(111, 7), (0, 5), (0, 8), (0, 1), (0, 10), (0, 1)]
+ >>> reduce(__and__, pack_j([(0x6f, 'jal'), (0, 'rd', 'x0'), (0, 'off20')]))
+ Bitfield(0xde000000, 32)
- >>> pack_j([(0x6f, 'jal'), (0, 'rd', 'x0'), (-2, 'off20')])
- [(111, 7), (0, 5), (255, 8), (1, 1), (1022, 10), (1, 1)]
+ >>> reduce(__and__, pack_j([(0x6f, 'jal'), (0, 'rd', 'x0'), (-2, 'off20')]))
+ Bitfield(0xde0ffffd, 32)
>>> pack_j([(0x6f, 'jal'), (2, 'rd', 'x2'), ('home', 'off20')])
- [(111, 7), (2, 5), ('home[19:12]', 'off8'), ('home[11:11]', 'off1'), ('home[10:1]', 'off10'), ('home[20:20]', 'off1')]
+ [Bitfield(0x6f, 7), Bitfield(0x2, 5), ('home[19:12]', 'off8'), ('home[11:11]', 'off1'), ('home[10:1]', 'off10'), ('home[20:20]', 'off1')]
"""
(op, rd, imm) = instr
op = bits.u(subv.untag(op), 7)
@@ -247,14 +249,14 @@ def pack_j(instr):
def pack_b(instr):
""" verify & pack B-type instructions.
- >>> pack_b([(0x63, 'branch'), (0, 'subop', '=='), (6, 'rs'), (0, 'rs'), (0, 'off12')])
- [(99, 7), (0, 1), (0, 4), (0, 3), (6, 5), (0, 5), (0, 6), (0, 1)]
+ >>> reduce(__and__, pack_b([(0x63, 'branch'), (0, 'subop', '=='), (6, 'rs'), (0, 'rs'), (0, 'off12')]))
+ Bitfield(0xc6006000, 32)
- >>> pack_b([(0x63, 'branch'), (0, 'subop', '=='), (6, 'rs'), (0, 'rs'), (-2, 'off12')])
- [(99, 7), (1, 1), (14, 4), (0, 3), (6, 5), (0, 5), (63, 6), (1, 1)]
+ >>> reduce(__and__, pack_b([(0x63, 'branch'), (0, 'subop', '=='), (6, 'rs'), (0, 'rs'), (-2, 'off12')]))
+ Bitfield(0xc7e0607f, 32)
>>> pack_b([(0x63, 'branch'), (0, 'subop', '=='), (6, 'rs'), (0, 'rs'), ('home', 'off12')])
- [(99, 7), ('home[11:11]', 'off1'), ('home[4:1]', 'off4'), (0, 3), (6, 5), (0, 5), ('home[10:5]', 'off6'), ('home[12:12]', 'off1')]
+ [Bitfield(0x63, 7), ('home[11:11]', 'off1'), ('home[4:1]', 'off4'), Bitfield(0x0, 3), Bitfield(0x6, 5), Bitfield(0x0, 5), ('home[10:5]', 'off6'), ('home[12:12]', 'off1')]
"""
(op, sub, rs1, rs2, imm) = instr
op = bits.u(subv.untag(op), 7)