diff --git a/asm/ffi.py b/asm/ffi.py index cb30ce9..39d9800 100644 --- a/asm/ffi.py +++ b/asm/ffi.py @@ -45,7 +45,7 @@ class CEnv(Structure): ("prglen", c_size_t) ] -class ExecutionEnvironment: +class Environment: def __init__(self, reglen=32, lablen=32, stklen=4096, prglen=4096): cenv = CEnv() cenv.reglen = reglen diff --git a/asm/test.py b/asm/test.py index 640f3bf..ba6d125 100644 --- a/asm/test.py +++ b/asm/test.py @@ -58,7 +58,7 @@ class PushTest(unittest.TestCase): p.parse_asm_line("PUSH 6") b = p() - ex = ffi.ExecutionEnvironment() + ex = ffi.Environment() self.assertEqual(ex.load(b), ffi.CompileRet.OK) self.assertEqual(ex.cenv.prgend, 2) @@ -78,6 +78,60 @@ class PushTest(unittest.TestCase): self.assertEqual(ex.cenv.prg[1].w[1], 0) self.assertEqual(ex.cenv.prg[1].w[2], 0) +class PopTest(unittest.TestCase): + def test_compile_pop_reg(self): + p = Program() + p.parse_asm_line("pop r9") + b = p() + self.assertEqual(b, b'\x02\xC2\x89\x00') + ex = ffi.Environment() + self.assertEqual(ex.load(b), ffi.CompileRet.OK) + self.assertEqual(ex.cenv.prgend, 1) + + self.assertEqual(ex.cenv.prg[0].opcode, 2) + self.assertEqual(ex.cenv.prg[0].w_flags[0], 1) + self.assertEqual(ex.cenv.prg[0].w_flags[1], 0) + self.assertEqual(ex.cenv.prg[0].w_flags[2], 0) + self.assertEqual(ex.cenv.prg[0].w[0], 9) + self.assertEqual(ex.cenv.prg[0].w[1], 0) + self.assertEqual(ex.cenv.prg[0].w[2], 0) + + def test_compile_throw_pop_literal(self): + p = Program() + ex = ffi.Environment() + with self.assertRaises(TypecheckException) as cm: + p.parse_asm_line("pop 6") + self.assertEqual(cm.exception.argtype, ArgType.REG) + self.assertEqual(cm.exception.sarg, '6') + self.assertEqual(cm.exception.i, 0) + self.assertEqual(cm.exception.opcode, 2) + + def test_compile_throw_pop_label(self): + p = Program() + ex = ffi.Environment() + with self.assertRaises(TypecheckException) as cm: + p.parse_asm_line("pop l9") + self.assertEqual(cm.exception.argtype, ArgType.REG) + self.assertEqual(cm.exception.sarg, 'l9') + self.assertEqual(cm.exception.i, 0) + self.assertEqual(cm.exception.opcode, 2) + + def test_compile_throw_argument_overflow(self): + p = Program() + with self.assertRaises(TypecheckLenException) as cm: + p.parse_asm_line("push r1 r2") + self.assertEqual(cm.exception.opcode, 1) + self.assertEqual(cm.exception.insargs, ["r1", "r2"]) + self.assertEqual(cm.exception.argtypelen, 1) + + def test_catch_typecheck_argument_underflow(self): + p = Program() + with self.assertRaises(TypecheckLenException) as cm: + p.parse_asm_line("push") + self.assertEqual(cm.exception.opcode, 2) + self.assertEqual(cm.exception.insargs, []) + self.assertEqual(cm.exception.argtypelen, 1) + class ProgramTest(unittest.TestCase): def test_two(self): p = Program()