aboutsummaryrefslogtreecommitdiffstats
path: root/asm
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2023-02-08 04:15:54 +0000
committerGravatar Peter McGoron 2023-02-08 04:15:54 +0000
commit606814c4c079fc51102d4aa69079bbfb67b6f4b0 (patch)
tree5b93e620d5d3d2f88a4d3df81547553cc3c7701f /asm
parentmove tests (diff)
python ffi: test parsing
Diffstat (limited to 'asm')
-rw-r--r--asm/creole.py (renamed from asm/creole_asm.py)0
-rw-r--r--asm/ffi.py29
-rw-r--r--asm/test.py8
3 files changed, 37 insertions, 0 deletions
diff --git a/asm/creole_asm.py b/asm/creole.py
index 8e87b58..8e87b58 100644
--- a/asm/creole_asm.py
+++ b/asm/creole.py
diff --git a/asm/ffi.py b/asm/ffi.py
new file mode 100644
index 0000000..87f6275
--- /dev/null
+++ b/asm/ffi.py
@@ -0,0 +1,29 @@
+from ctypes import *
+dll = CDLL("./libcreole.so")
+
+class CIns(Structure):
+ _fields_ = [("opcode", c_int),
+ ("w_flags", c_ubyte * 3),
+ ("w", c_uint * 3)]
+
+class CReader(Structure):
+ _fields_ = [("p", POINTER(c_ubyte)),
+ ("left", c_size_t)]
+def make_uchar_buf(s):
+ buf = (c_ubyte * len(s))()
+ buf[:] = s[:]
+ return buf
+
+class CParseLineException(Exception):
+ pass
+def parse_line(line):
+ buf = make_uchar_buf(line)
+
+ rd = CReader(buf, len(line))
+ ins = CIns()
+
+ ret = dll.creole_parse_line(byref(ins), byref(rd))
+
+ if ret != 0:
+ raise CParseLineException(ret)
+ return (ins.opcode, list(zip(ins.w_flags, ins.w)))
diff --git a/asm/test.py b/asm/test.py
index e86b508..dc07655 100644
--- a/asm/test.py
+++ b/asm/test.py
@@ -1,5 +1,6 @@
from creole import *
import unittest
+import ffi
class ProgramTest(unittest.TestCase):
def test_oneline(self):
@@ -38,5 +39,12 @@ class ProgramTest(unittest.TestCase):
b = p()
self.assertEqual(b, b_ex)
+ def test_parse_reg(self):
+ p = Program()
+ p.parse_asm_line("push r5")
+ ins = ffi.parse_line(p())
+ self.assertEqual(ins[0], instructions["push"].opcode)
+ self.assertEqual(ins[1][0], (1,5))
+
if __name__ == "__main__":
unittest.main()