Skip to content

Commit d0962df

Browse files
authored
[mypyc] Add a few native int helper irbuilder methods (#19423)
I will use these in a follow-up PR. These simplify IR construction a bit. Only the most common operations have helpers for now, but we can add more later if it seems useful.
1 parent 095df17 commit d0962df

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

mypyc/irbuild/ll_builder.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,33 @@ def float_mod(self, lhs: Value, rhs: Value, line: int) -> Value:
21042104
def compare_floats(self, lhs: Value, rhs: Value, op: int, line: int) -> Value:
21052105
return self.add(FloatComparisonOp(lhs, rhs, op, line))
21062106

2107+
def int_add(self, lhs: Value, rhs: Value | int) -> Value:
2108+
"""Helper to add two native integers.
2109+
2110+
The result has the type of lhs.
2111+
"""
2112+
if isinstance(rhs, int):
2113+
rhs = Integer(rhs, lhs.type)
2114+
return self.int_op(lhs.type, lhs, rhs, IntOp.ADD, line=-1)
2115+
2116+
def int_sub(self, lhs: Value, rhs: Value | int) -> Value:
2117+
"""Helper to subtract a native integer from another one.
2118+
2119+
The result has the type of lhs.
2120+
"""
2121+
if isinstance(rhs, int):
2122+
rhs = Integer(rhs, lhs.type)
2123+
return self.int_op(lhs.type, lhs, rhs, IntOp.SUB, line=-1)
2124+
2125+
def int_mul(self, lhs: Value, rhs: Value | int) -> Value:
2126+
"""Helper to multiply two native integers.
2127+
2128+
The result has the type of lhs.
2129+
"""
2130+
if isinstance(rhs, int):
2131+
rhs = Integer(rhs, lhs.type)
2132+
return self.int_op(lhs.type, lhs, rhs, IntOp.MUL, line=-1)
2133+
21072134
def fixed_width_int_op(
21082135
self, type: RPrimitive, lhs: Value, rhs: Value, op: int, line: int
21092136
) -> Value:

0 commit comments

Comments
 (0)