55from mypy .test .helpers import assert_string_arrays_equal
66from mypyc .codegen .emit import Emitter , EmitterContext
77from mypyc .codegen .emitfunc import FunctionEmitterVisitor , generate_native_function
8- from mypyc .common import PLATFORM_SIZE
8+ from mypyc .common import HAVE_IMMORTAL , PLATFORM_SIZE
99from mypyc .ir .class_ir import ClassIR
1010from mypyc .ir .func_ir import FuncDecl , FuncIR , FuncSignature , RuntimeArg
1111from mypyc .ir .ops import (
2828 Integer ,
2929 IntOp ,
3030 LoadAddress ,
31+ LoadLiteral ,
3132 LoadMem ,
3233 Op ,
3334 Register ,
5354 int64_rprimitive ,
5455 int_rprimitive ,
5556 list_rprimitive ,
57+ none_rprimitive ,
5658 object_rprimitive ,
5759 pointer_rprimitive ,
5860 short_int_rprimitive ,
@@ -114,6 +116,7 @@ def add_local(name: str, rtype: RType) -> Register:
114116 compute_vtable (ir )
115117 ir .mro = [ir ]
116118 self .r = add_local ("r" , RInstance (ir ))
119+ self .none = add_local ("none" , none_rprimitive )
117120
118121 self .context = EmitterContext (NameGenerator ([["mod" ]]))
119122
@@ -805,9 +808,25 @@ def test_extend(self) -> None:
805808 Extend (a , int_rprimitive , signed = False ), """cpy_r_r0 = (uint32_t)cpy_r_a;"""
806809 )
807810
811+ def test_inc_ref_none (self ) -> None :
812+ b = Box (self .none )
813+ self .assert_emit ([b , IncRef (b )], "" if HAVE_IMMORTAL else "CPy_INCREF(cpy_r_r0);" )
814+
815+ def test_inc_ref_bool (self ) -> None :
816+ b = Box (self .b )
817+ self .assert_emit ([b , IncRef (b )], "" if HAVE_IMMORTAL else "CPy_INCREF(cpy_r_r0);" )
818+
819+ def test_inc_ref_int_literal (self ) -> None :
820+ for x in - 5 , 0 , 1 , 5 , 255 , 256 :
821+ b = LoadLiteral (x , object_rprimitive )
822+ self .assert_emit ([b , IncRef (b )], "" if HAVE_IMMORTAL else "CPy_INCREF(cpy_r_r0);" )
823+ for x in - 6 , 257 :
824+ b = LoadLiteral (x , object_rprimitive )
825+ self .assert_emit ([b , IncRef (b )], "CPy_INCREF(cpy_r_r0);" )
826+
808827 def assert_emit (
809828 self ,
810- op : Op ,
829+ op : Op | list [ Op ] ,
811830 expected : str ,
812831 next_block : BasicBlock | None = None ,
813832 * ,
@@ -816,7 +835,11 @@ def assert_emit(
816835 skip_next : bool = False ,
817836 ) -> None :
818837 block = BasicBlock (0 )
819- block .ops .append (op )
838+ if isinstance (op , Op ):
839+ block .ops .append (op )
840+ else :
841+ block .ops .extend (op )
842+ op = op [- 1 ]
820843 value_names = generate_names_for_ir (self .registers , [block ])
821844 emitter = Emitter (self .context , value_names )
822845 declarations = Emitter (self .context , value_names )
0 commit comments