aboutsummaryrefslogtreecommitdiffstats
path: root/pack.py
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-05-28 15:22:11 +0000
committers-ol <s-ol@users.noreply.github.com>2020-05-28 15:22:11 +0000
commit51091f7355a89aab6347523f3e3e53ed328b4dfc (patch)
treec3a11ca809ba075a8e2b37c217c6ed6278905ff2 /pack.py
parenttests, cleanup, verify.py (diff)
downloadsubv-51091f7355a89aab6347523f3e3e53ed328b4dfc.tar.gz
subv-51091f7355a89aab6347523f3e3e53ed328b4dfc.zip
survey, pack
Diffstat (limited to 'pack.py')
-rw-r--r--pack.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/pack.py b/pack.py
new file mode 100644
index 0000000..aba29a3
--- /dev/null
+++ b/pack.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env python3
+from riscv import byteify, bit_concat
+from subx import clean, classify, parse_bits, format_instr
+
+def pack(iter):
+ for line in iter:
+ line = clean(line)
+ if line == '':
+ continue
+
+ type = classify(line)
+
+ if type == 'segment' or type == 'label':
+ yield line
+ else:
+ fields = parse_bits(line)
+ total = bit_concat(*fields)
+
+ if total[1] != 32:
+ raise ValueError("instruction parts do not add up to 32 bit!")
+
+ yield format_instr(byteify(total))
+
+if __name__ == '__main__':
+ import sys
+ for line in pack(sys.stdin):
+ print(line)
+
+import unittest
+class TestE2E(unittest.TestCase):
+ def test_e2e(self):
+ from io import StringIO
+ from textwrap import dedent
+ inp = dedent('''\
+ == code 0x80000000
+ 37/7 05/5 10010/20
+ 13/7 06/5 00/3 00/5 48/12
+ 23/7 00/5 02/3 05/5 06/5 00/7
+ 13/7 06/5 00/3 00/5 65/12
+ 23/7 00/5 02/3 05/5 06/5 00/7
+ 13/7 06/5 00/3 00/5 6c/12
+ 23/7 00/5 02/3 05/5 06/5 00/7
+ 13/7 06/5 00/3 00/5 6c/12
+ 23/7 00/5 02/3 05/5 06/5 00/7
+ 13/7 06/5 00/3 00/5 6f/12
+ 23/7 00/5 02/3 05/5 06/5 00/7
+ 13/7 06/5 00/3 00/5 0a/12
+ 23/7 00/5 02/3 05/5 06/5 00/7
+ 6f/7 00/5 ff/8 01/1 3e6/10 1/1
+ ''')
+
+ out = dedent('''\
+ == code 0x80000000
+ b7 02 01 10
+ 13 03 80 04
+ 23 a0 62 00
+ 13 03 50 06
+ 23 a0 62 00
+ 13 03 c0 06
+ 23 a0 62 00
+ 13 03 c0 06
+ 23 a0 62 00
+ 13 03 f0 06
+ 23 a0 62 00
+ 13 03 a0 00
+ 23 a0 62 00
+ 6f f0 df fc
+ ''')
+
+ got = ''
+ for line in pack(StringIO(inp)):
+ got += line + '\n'
+
+ self.assertEqual(got, out)