Skip to content

Commit

Permalink
[mypyc] Introduce low level integer type (#8955)
Browse files Browse the repository at this point in the history
closes mypyc/mypyc#735

This PR introduces a c_int_rprimitive RType which represents a low level, plain 
integer (corresponds to C's Py_ssize_t). It also allows LoadInt to select its rtype 
to generate tagged/plain integer code accordingly.
  • Loading branch information
TH3CHARLie authored Jun 5, 2020
1 parent 3adb2e9 commit d98ba8e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 5 additions & 2 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
BasicBlock, Value, MethodCall, PrimitiveOp, EmitterInterface, Unreachable, NAMESPACE_STATIC,
NAMESPACE_TYPE, NAMESPACE_MODULE, RaiseStandardError, CallC
)
from mypyc.ir.rtypes import RType, RTuple
from mypyc.ir.rtypes import RType, RTuple, is_c_int_rprimitive
from mypyc.ir.func_ir import FuncIR, FuncDecl, FUNC_STATICMETHOD, FUNC_CLASSMETHOD
from mypyc.ir.class_ir import ClassIR

Expand Down Expand Up @@ -179,7 +179,10 @@ def visit_assign(self, op: Assign) -> None:

def visit_load_int(self, op: LoadInt) -> None:
dest = self.reg(op)
self.emit_line('%s = %d;' % (dest, op.value * 2))
if is_c_int_rprimitive(op.type):
self.emit_line('%s = %d;' % (dest, op.value))
else:
self.emit_line('%s = %d;' % (dest, op.value * 2))

def visit_load_error_value(self, op: LoadErrorValue) -> None:
if isinstance(op.type, RTuple):
Expand Down
4 changes: 2 additions & 2 deletions mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,10 @@ class LoadInt(RegisterOp):

error_kind = ERR_NEVER

def __init__(self, value: int, line: int = -1) -> None:
def __init__(self, value: int, line: int = -1, rtype: RType = short_int_rprimitive) -> None:
super().__init__(line)
self.value = value
self.type = short_int_rprimitive
self.type = rtype

def sources(self) -> List[Value]:
return []
Expand Down
10 changes: 9 additions & 1 deletion mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(self,
self.is_unboxed = is_unboxed
self._ctype = ctype
self.is_refcounted = is_refcounted
if ctype == 'CPyTagged':
if ctype in ('CPyTagged', 'Py_ssize_t'):
self.c_undefined = 'CPY_INT_TAG'
elif ctype == 'PyObject *':
# Boxed types use the null pointer as the error value.
Expand Down Expand Up @@ -234,6 +234,10 @@ def __repr__(self) -> str:
short_int_rprimitive = RPrimitive('short_int', is_unboxed=True, is_refcounted=False,
ctype='CPyTagged') # type: Final

# low level integer (corresponds to C's 'int').
c_int_rprimitive = RPrimitive('c_int', is_unboxed=True, is_refcounted=False,
ctype='Py_ssize_t') # type: Final

# Floats are represent as 'float' PyObject * values. (In the future
# we'll likely switch to a more efficient, unboxed representation.)
float_rprimitive = RPrimitive('builtins.float', is_unboxed=False,
Expand Down Expand Up @@ -275,6 +279,10 @@ def is_short_int_rprimitive(rtype: RType) -> bool:
return rtype is short_int_rprimitive


def is_c_int_rprimitive(rtype: RType) -> bool:
return rtype is c_int_rprimitive


def is_float_rprimitive(rtype: RType) -> bool:
return isinstance(rtype, RPrimitive) and rtype.name == 'builtins.float'

Expand Down
4 changes: 3 additions & 1 deletion mypyc/test/test_emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from mypyc.ir.rtypes import (
RTuple, RInstance, int_rprimitive, bool_rprimitive, list_rprimitive,
dict_rprimitive, object_rprimitive
dict_rprimitive, object_rprimitive, c_int_rprimitive
)
from mypyc.ir.func_ir import FuncIR, FuncDecl, RuntimeArg, FuncSignature
from mypyc.ir.class_ir import ClassIR
Expand Down Expand Up @@ -70,6 +70,8 @@ def test_return(self) -> None:
def test_load_int(self) -> None:
self.assert_emit(LoadInt(5),
"cpy_r_r0 = 10;")
self.assert_emit(LoadInt(5, -1, c_int_rprimitive),
"cpy_r_r00 = 5;")

def test_tuple_get(self) -> None:
self.assert_emit(TupleGet(self.t, 1, 0), 'cpy_r_r0 = cpy_r_t.f1;')
Expand Down

0 comments on commit d98ba8e

Please sign in to comment.