Skip to content

Commit a3b5933

Browse files
authored
[mypyc] Translate more primitive ops to CallC (python#9014)
Related issue: mypyc/mypyc#734 Mainly translate primitive ops that use call_emit and with their descriptions used elsewhere. Int ops are intentionally left out because they make a lot of changes to IR tests, therefore making them in a separate PR.
1 parent d3edd60 commit a3b5933

17 files changed

+83
-79
lines changed

mypyc/irbuild/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def process_iterator_tuple_assignment(self,
517517
self.activate_block(ok_block)
518518

519519
for litem in reversed(post_star_vals):
520-
ritem = self.primitive_op(list_pop_last, [iter_list], line)
520+
ritem = self.call_c(list_pop_last, [iter_list], line)
521521
self.assign(litem, ritem, line)
522522

523523
# Assign the starred value

mypyc/irbuild/expression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def transform_tuple_expr(builder: IRBuilder, expr: TupleExpr) -> Value:
463463
def _visit_tuple_display(builder: IRBuilder, expr: TupleExpr) -> Value:
464464
"""Create a list, then turn it into a tuple."""
465465
val_as_list = _visit_list_display(builder, expr.items, expr.line)
466-
return builder.primitive_op(list_tuple_op, [val_as_list], expr.line)
466+
return builder.call_c(list_tuple_op, [val_as_list], expr.line)
467467

468468

469469
def transform_dict_expr(builder: IRBuilder, expr: DictExpr) -> Value:

mypyc/irbuild/ll_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def py_call(self,
270270
pos_args_list = self.primitive_op(new_list_op, pos_arg_values, line)
271271
for star_arg_value in star_arg_values:
272272
self.primitive_op(list_extend_op, [pos_args_list, star_arg_value], line)
273-
pos_args_tuple = self.primitive_op(list_tuple_op, [pos_args_list], line)
273+
pos_args_tuple = self.call_c(list_tuple_op, [pos_args_list], line)
274274

275275
kw_args_dict = self.make_dict(kw_arg_key_value_pairs, line)
276276

mypyc/primitives/dict_ops.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mypyc.primitives.registry import (
1212
name_ref_op, method_op, binary_op, func_op, custom_op,
1313
simple_emit, negative_int_emit, call_emit, call_negative_bool_emit,
14-
name_emit, c_custom_op, c_method_op
14+
name_emit, c_custom_op, c_method_op, c_function_op
1515
)
1616

1717

@@ -103,25 +103,24 @@
103103
return_type=dict_rprimitive,
104104
c_function_name='CPyDict_Build',
105105
error_kind=ERR_MAGIC,
106-
var_arg_type=object_rprimitive,)
106+
var_arg_type=object_rprimitive)
107107

108108
# Construct a dictionary from another dictionary.
109-
func_op(
109+
c_function_op(
110110
name='builtins.dict',
111111
arg_types=[dict_rprimitive],
112-
result_type=dict_rprimitive,
112+
return_type=dict_rprimitive,
113+
c_function_name='PyDict_Copy',
113114
error_kind=ERR_MAGIC,
114-
emit=call_emit('PyDict_Copy'),
115115
priority=2)
116116

117117
# Generic one-argument dict constructor: dict(obj)
118-
119-
func_op(
118+
c_function_op(
120119
name='builtins.dict',
121120
arg_types=[object_rprimitive],
122-
result_type=dict_rprimitive,
123-
error_kind=ERR_MAGIC,
124-
emit=call_emit('CPyDict_FromAny'))
121+
return_type=dict_rprimitive,
122+
c_function_name='CPyDict_FromAny',
123+
error_kind=ERR_MAGIC)
125124

126125
# dict.keys()
127126
c_method_op(

mypyc/primitives/list_ops.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None:
4949

5050

5151
# list[index] (for an integer index)
52-
list_get_item_op = method_op(
52+
list_get_item_op = c_method_op(
5353
name='__getitem__',
5454
arg_types=[list_rprimitive, int_rprimitive],
55-
result_type=object_rprimitive,
56-
error_kind=ERR_MAGIC,
57-
emit=call_emit('CPyList_GetItem'))
55+
return_type=object_rprimitive,
56+
c_function_name='CPyList_GetItem',
57+
error_kind=ERR_MAGIC)
5858

5959
# Version with no int bounds check for when it is known to be short
60-
method_op(
60+
c_method_op(
6161
name='__getitem__',
6262
arg_types=[list_rprimitive, short_int_rprimitive],
63-
result_type=object_rprimitive,
63+
return_type=object_rprimitive,
64+
c_function_name='CPyList_GetItemShort',
6465
error_kind=ERR_MAGIC,
65-
emit=call_emit('CPyList_GetItemShort'),
6666
priority=2)
6767

6868
# This is unsafe because it assumes that the index is a non-negative short integer
@@ -76,13 +76,13 @@ def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None:
7676
emit=call_emit('CPyList_GetItemUnsafe'))
7777

7878
# list[index] = obj
79-
list_set_item_op = method_op(
79+
list_set_item_op = c_method_op(
8080
name='__setitem__',
8181
arg_types=[list_rprimitive, int_rprimitive, object_rprimitive],
82-
steals=[False, False, True],
83-
result_type=bool_rprimitive,
82+
return_type=bool_rprimitive,
83+
c_function_name='CPyList_SetItem',
8484
error_kind=ERR_FALSE,
85-
emit=call_emit('CPyList_SetItem'))
85+
steals=[False, False, True])
8686

8787
# list.append(obj)
8888
list_append_op = method_op(
@@ -101,20 +101,20 @@ def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None:
101101
emit=call_emit('CPyList_Extend'))
102102

103103
# list.pop()
104-
list_pop_last = method_op(
104+
list_pop_last = c_method_op(
105105
name='pop',
106106
arg_types=[list_rprimitive],
107-
result_type=object_rprimitive,
108-
error_kind=ERR_MAGIC,
109-
emit=call_emit('CPyList_PopLast'))
107+
return_type=object_rprimitive,
108+
c_function_name='CPyList_PopLast',
109+
error_kind=ERR_MAGIC)
110110

111111
# list.pop(index)
112-
list_pop = method_op(
112+
list_pop = c_method_op(
113113
name='pop',
114114
arg_types=[list_rprimitive, int_rprimitive],
115-
result_type=object_rprimitive,
116-
error_kind=ERR_MAGIC,
117-
emit=call_emit('CPyList_Pop'))
115+
return_type=object_rprimitive,
116+
c_function_name='CPyList_Pop',
117+
error_kind=ERR_MAGIC)
118118

119119
# list.count(obj)
120120
c_method_op(

mypyc/primitives/tuple_ops.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
EmitterInterface, ERR_NEVER, ERR_MAGIC
1111
)
1212
from mypyc.ir.rtypes import tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive
13-
from mypyc.primitives.registry import func_op, method_op, custom_op, call_emit, simple_emit
13+
from mypyc.primitives.registry import (
14+
func_op, c_method_op, custom_op, simple_emit, c_function_op
15+
)
1416

1517

1618
# tuple[index] (for an int index)
17-
tuple_get_item_op = method_op(
19+
tuple_get_item_op = c_method_op(
1820
name='__getitem__',
1921
arg_types=[tuple_rprimitive, int_rprimitive],
20-
result_type=object_rprimitive,
21-
error_kind=ERR_MAGIC,
22-
emit=call_emit('CPySequenceTuple_GetItem'))
23-
22+
return_type=object_rprimitive,
23+
c_function_name='CPySequenceTuple_GetItem',
24+
error_kind=ERR_MAGIC)
2425

2526
# Construct a boxed tuple from items: (item1, item2, ...)
2627
new_tuple_op = custom_op(
@@ -49,18 +50,18 @@ def emit_len(emitter: EmitterInterface, args: List[str], dest: str) -> None:
4950
emit=emit_len)
5051

5152
# Construct tuple from a list.
52-
list_tuple_op = func_op(
53+
list_tuple_op = c_function_op(
5354
name='builtins.tuple',
5455
arg_types=[list_rprimitive],
55-
result_type=tuple_rprimitive,
56+
return_type=tuple_rprimitive,
57+
c_function_name='PyList_AsTuple',
5658
error_kind=ERR_MAGIC,
57-
emit=call_emit('PyList_AsTuple'),
5859
priority=2)
5960

6061
# Construct tuple from an arbitrary (iterable) object.
61-
func_op(
62+
c_function_op(
6263
name='builtins.tuple',
6364
arg_types=[object_rprimitive],
64-
result_type=tuple_rprimitive,
65-
error_kind=ERR_MAGIC,
66-
emit=call_emit('PySequence_Tuple'))
65+
return_type=tuple_rprimitive,
66+
c_function_name='PySequence_Tuple',
67+
error_kind=ERR_MAGIC)

mypyc/test-data/exceptions.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def f(x):
1414
r2, r3 :: int
1515
L0:
1616
r0 = 0
17-
r1 = x[r0] :: list
17+
r1 = CPyList_GetItemShort(x, r0)
1818
if is_error(r1) goto L3 (error at f:3) else goto L1
1919
L1:
2020
r2 = unbox(int, r1)
@@ -51,7 +51,7 @@ L1:
5151
r2 = None
5252
inc_ref z :: int
5353
r3 = box(int, z)
54-
r4 = x.__setitem__(y, r3) :: list
54+
r4 = CPyList_SetItem(x, y, r3)
5555
if not r4 goto L3 (error at f:4) else goto L2 :: bool
5656
L2:
5757
r5 = None
@@ -144,7 +144,7 @@ L1:
144144
r2 = i < l :: int
145145
if r2 goto L2 else goto L7 :: bool
146146
L2:
147-
r3 = a[i] :: list
147+
r3 = CPyList_GetItem(a, i)
148148
if is_error(r3) goto L8 (error at sum:6) else goto L3
149149
L3:
150150
r4 = unbox(int, r3)

mypyc/test-data/irbuild-any.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ L0:
116116
r5 = a.__setitem__(r3, r4) :: object
117117
r6 = box(int, n)
118118
r7 = l.__setitem__(a, r6) :: object
119-
r8 = l.__setitem__(n, a) :: list
119+
r8 = CPyList_SetItem(l, n, a)
120120
r9 = box(int, n)
121121
r10 = [a, r9]
122122
r11 = None

mypyc/test-data/irbuild-basic.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ L0:
754754
r6 = (r4, r5)
755755
r7 = 0
756756
r8 = box(tuple[int, int], r6)
757-
r9 = a.__setitem__(r7, r8) :: list
757+
r9 = CPyList_SetItem(a, r7, r8)
758758
r10 = True
759759
r11 = box(bool, r10)
760760
y = r11
@@ -1633,7 +1633,7 @@ L0:
16331633
r7 = []
16341634
r8 = box(tuple[int, int, int], r3)
16351635
r9 = r7.extend(r8) :: list
1636-
r10 = tuple r7 :: list
1636+
r10 = PyList_AsTuple(r7)
16371637
r11 = PyDict_New()
16381638
r12 = py_call_with_kwargs(r6, r10, r11)
16391639
r13 = unbox(tuple[int, int, int], r12)
@@ -1662,7 +1662,7 @@ L0:
16621662
r8 = [r7]
16631663
r9 = box(tuple[int, int], r3)
16641664
r10 = r8.extend(r9) :: list
1665-
r11 = tuple r8 :: list
1665+
r11 = PyList_AsTuple(r8)
16661666
r12 = PyDict_New()
16671667
r13 = py_call_with_kwargs(r6, r11, r12)
16681668
r14 = unbox(tuple[int, int, int], r13)
@@ -2387,7 +2387,7 @@ def g(a, b, c):
23872387
r2, r3 :: int
23882388
L0:
23892389
r0 = a.__getitem__(c)
2390-
r1 = b[c] :: list
2390+
r1 = CPyList_GetItem(b, c)
23912391
r2 = unbox(int, r1)
23922392
r3 = r0 + r2 :: int
23932393
return r3
@@ -3261,7 +3261,7 @@ L1:
32613261
unreachable
32623262
L2:
32633263
r2 = 0
3264-
r3 = r0[r2] :: list
3264+
r3 = CPyList_GetItemShort(r0, r2)
32653265
r4 = unbox(int, r3)
32663266
return r4
32673267

mypyc/test-data/irbuild-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ L0:
6060
r3 = [c]
6161
a = r3
6262
r4 = 0
63-
r5 = a[r4] :: list
63+
r5 = CPyList_GetItemShort(a, r4)
6464
r6 = cast(__main__.C, r5)
6565
d = r6
6666
r7 = d.x

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy