-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathint_ops.py
More file actions
331 lines (282 loc) · 10 KB
/
int_ops.py
File metadata and controls
331 lines (282 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""Arbitrary-precision integer primitive ops.
These mostly operate on (usually) unboxed integers that use a tagged pointer
representation (CPyTagged) and correspond to the Python 'int' type.
See also the documentation for mypyc.rtypes.int_rprimitive.
Use mypyc.ir.ops.IntOp for operations on fixed-width/C integers.
"""
from __future__ import annotations
from typing import NamedTuple
from mypyc.ir.ops import (
ERR_ALWAYS,
ERR_MAGIC,
ERR_MAGIC_OVERLAPPING,
ERR_NEVER,
ComparisonOp,
PrimitiveDescription,
)
from mypyc.ir.rtypes import (
RType,
bit_rprimitive,
bool_rprimitive,
c_pyssize_t_rprimitive,
float_rprimitive,
int16_rprimitive,
int32_rprimitive,
int64_rprimitive,
int_rprimitive,
object_rprimitive,
str_rprimitive,
void_rtype,
)
from mypyc.primitives.registry import (
CFunctionDescription,
binary_op,
custom_op,
function_op,
load_address_op,
unary_op,
)
# Constructors for builtins.int and native int types have the same behavior. In
# interpreted mode, native int types are just aliases to 'int'.
for int_name in (
"builtins.int",
"mypy_extensions.i64",
"mypy_extensions.i32",
"mypy_extensions.i16",
"mypy_extensions.u8",
):
# These int constructors produce object_rprimitives that then need to be unboxed
# I guess unboxing ourselves would save a check and branch though?
# Get the type object for 'builtins.int' or a native int type.
# For ordinary calls to int() we use a load_address to the type.
# Native ints don't have a separate type object -- we just use 'builtins.int'.
load_address_op(name=int_name, type=object_rprimitive, src="PyLong_Type")
# int(float). We could do a bit better directly.
function_op(
name=int_name,
arg_types=[float_rprimitive],
return_type=int_rprimitive,
c_function_name="CPyTagged_FromFloat",
error_kind=ERR_MAGIC,
)
# int(string)
function_op(
name=int_name,
arg_types=[str_rprimitive],
return_type=object_rprimitive,
c_function_name="CPyLong_FromStr",
error_kind=ERR_MAGIC,
)
# int(string, base)
function_op(
name=int_name,
arg_types=[str_rprimitive, int_rprimitive],
return_type=object_rprimitive,
c_function_name="CPyLong_FromStrWithBase",
error_kind=ERR_MAGIC,
)
# str(int)
int_to_str_op = function_op(
name="builtins.str",
arg_types=[int_rprimitive],
return_type=str_rprimitive,
c_function_name="CPyTagged_Str",
error_kind=ERR_MAGIC,
priority=2,
)
# We need a specialization for str on bools also since the int one is wrong...
function_op(
name="builtins.str",
arg_types=[bool_rprimitive],
return_type=str_rprimitive,
c_function_name="CPyBool_Str",
error_kind=ERR_MAGIC,
priority=3,
)
def int_binary_primitive(
op: str, primitive_name: str, return_type: RType = int_rprimitive, error_kind: int = ERR_NEVER
) -> PrimitiveDescription:
return binary_op(
name=op,
arg_types=[int_rprimitive, int_rprimitive],
return_type=return_type,
primitive_name=primitive_name,
error_kind=error_kind,
)
int_eq = int_binary_primitive(op="==", primitive_name="int_eq", return_type=bit_rprimitive)
int_ne = int_binary_primitive(op="!=", primitive_name="int_ne", return_type=bit_rprimitive)
int_lt = int_binary_primitive(op="<", primitive_name="int_lt", return_type=bit_rprimitive)
int_le = int_binary_primitive(op="<=", primitive_name="int_le", return_type=bit_rprimitive)
int_gt = int_binary_primitive(op=">", primitive_name="int_gt", return_type=bit_rprimitive)
int_ge = int_binary_primitive(op=">=", primitive_name="int_ge", return_type=bit_rprimitive)
def int_binary_op(
name: str,
c_function_name: str,
return_type: RType = int_rprimitive,
error_kind: int = ERR_NEVER,
) -> None:
binary_op(
name=name,
arg_types=[int_rprimitive, int_rprimitive],
return_type=return_type,
c_function_name=c_function_name,
error_kind=error_kind,
)
# Binary, unary and augmented assignment operations that operate on CPyTagged ints
# are implemented as C functions.
int_binary_op("+", "CPyTagged_Add")
int_binary_op("-", "CPyTagged_Subtract")
int_binary_op("*", "CPyTagged_Multiply")
int_binary_op("&", "CPyTagged_And")
int_binary_op("|", "CPyTagged_Or")
int_binary_op("^", "CPyTagged_Xor")
# Divide and remainder we honestly propagate errors from because they
# can raise ZeroDivisionError
int_binary_op("//", "CPyTagged_FloorDivide", error_kind=ERR_MAGIC)
int_binary_op("%", "CPyTagged_Remainder", error_kind=ERR_MAGIC)
# Negative shift counts raise an exception
int_binary_op(">>", "CPyTagged_Rshift", error_kind=ERR_MAGIC)
int_binary_op("<<", "CPyTagged_Lshift", error_kind=ERR_MAGIC)
int_binary_op(
"/", "CPyTagged_TrueDivide", return_type=float_rprimitive, error_kind=ERR_MAGIC_OVERLAPPING
)
# This should work because assignment operators are parsed differently
# and the code in irbuild that handles it does the assignment
# regardless of whether or not the operator works in place anyway.
int_binary_op("+=", "CPyTagged_Add")
int_binary_op("-=", "CPyTagged_Subtract")
int_binary_op("*=", "CPyTagged_Multiply")
int_binary_op("&=", "CPyTagged_And")
int_binary_op("|=", "CPyTagged_Or")
int_binary_op("^=", "CPyTagged_Xor")
int_binary_op("//=", "CPyTagged_FloorDivide", error_kind=ERR_MAGIC)
int_binary_op("%=", "CPyTagged_Remainder", error_kind=ERR_MAGIC)
int_binary_op(">>=", "CPyTagged_Rshift", error_kind=ERR_MAGIC)
int_binary_op("<<=", "CPyTagged_Lshift", error_kind=ERR_MAGIC)
def int_unary_op(name: str, c_function_name: str) -> CFunctionDescription:
return unary_op(
name=name,
arg_type=int_rprimitive,
return_type=int_rprimitive,
c_function_name=c_function_name,
error_kind=ERR_NEVER,
)
int_neg_op = int_unary_op("-", "CPyTagged_Negate")
int_invert_op = int_unary_op("~", "CPyTagged_Invert")
# Primitives related to integer comparison operations:
# Description for building int comparison ops
#
# Fields:
# binary_op_variant: identify which IntOp to use when operands are short integers
# c_func_description: the C function to call when operands are tagged integers
# c_func_negated: whether to negate the C function call's result
# c_func_swap_operands: whether to swap lhs and rhs when call the function
class IntComparisonOpDescription(NamedTuple):
binary_op_variant: int
c_func_description: CFunctionDescription
c_func_negated: bool
c_func_swap_operands: bool
# Equals operation on two boxed tagged integers
int_equal_ = custom_op(
arg_types=[int_rprimitive, int_rprimitive],
return_type=bit_rprimitive,
c_function_name="CPyTagged_IsEq_",
error_kind=ERR_NEVER,
)
# Less than operation on two boxed tagged integers
int_less_than_ = custom_op(
arg_types=[int_rprimitive, int_rprimitive],
return_type=bit_rprimitive,
c_function_name="CPyTagged_IsLt_",
error_kind=ERR_NEVER,
)
# Provide mapping from textual op to short int's op variant and boxed int's description.
# Note that these are not complete implementations and require extra IR.
int_comparison_op_mapping: dict[str, IntComparisonOpDescription] = {
"==": IntComparisonOpDescription(ComparisonOp.EQ, int_equal_, False, False),
"!=": IntComparisonOpDescription(ComparisonOp.NEQ, int_equal_, True, False),
"<": IntComparisonOpDescription(ComparisonOp.SLT, int_less_than_, False, False),
"<=": IntComparisonOpDescription(ComparisonOp.SLE, int_less_than_, True, True),
">": IntComparisonOpDescription(ComparisonOp.SGT, int_less_than_, False, True),
">=": IntComparisonOpDescription(ComparisonOp.SGE, int_less_than_, True, False),
}
int64_divide_op = custom_op(
arg_types=[int64_rprimitive, int64_rprimitive],
return_type=int64_rprimitive,
c_function_name="CPyInt64_Divide",
error_kind=ERR_MAGIC_OVERLAPPING,
)
int64_mod_op = custom_op(
arg_types=[int64_rprimitive, int64_rprimitive],
return_type=int64_rprimitive,
c_function_name="CPyInt64_Remainder",
error_kind=ERR_MAGIC_OVERLAPPING,
)
int32_divide_op = custom_op(
arg_types=[int32_rprimitive, int32_rprimitive],
return_type=int32_rprimitive,
c_function_name="CPyInt32_Divide",
error_kind=ERR_MAGIC_OVERLAPPING,
)
int32_mod_op = custom_op(
arg_types=[int32_rprimitive, int32_rprimitive],
return_type=int32_rprimitive,
c_function_name="CPyInt32_Remainder",
error_kind=ERR_MAGIC_OVERLAPPING,
)
int16_divide_op = custom_op(
arg_types=[int16_rprimitive, int16_rprimitive],
return_type=int16_rprimitive,
c_function_name="CPyInt16_Divide",
error_kind=ERR_MAGIC_OVERLAPPING,
)
int16_mod_op = custom_op(
arg_types=[int16_rprimitive, int16_rprimitive],
return_type=int16_rprimitive,
c_function_name="CPyInt16_Remainder",
error_kind=ERR_MAGIC_OVERLAPPING,
)
# Convert tagged int (as PyObject *) to i64
int_to_int64_op = custom_op(
arg_types=[object_rprimitive],
return_type=int64_rprimitive,
c_function_name="CPyLong_AsInt64",
error_kind=ERR_MAGIC_OVERLAPPING,
)
ssize_t_to_int_op = custom_op(
arg_types=[c_pyssize_t_rprimitive],
return_type=int_rprimitive,
c_function_name="CPyTagged_FromSsize_t",
error_kind=ERR_MAGIC,
)
int64_to_int_op = custom_op(
arg_types=[int64_rprimitive],
return_type=int_rprimitive,
c_function_name="CPyTagged_FromInt64",
error_kind=ERR_MAGIC,
)
# Convert tagged int (as PyObject *) to i32
int_to_int32_op = custom_op(
arg_types=[object_rprimitive],
return_type=int32_rprimitive,
c_function_name="CPyLong_AsInt32",
error_kind=ERR_MAGIC_OVERLAPPING,
)
int32_overflow = custom_op(
arg_types=[],
return_type=void_rtype,
c_function_name="CPyInt32_Overflow",
error_kind=ERR_ALWAYS,
)
int16_overflow = custom_op(
arg_types=[],
return_type=void_rtype,
c_function_name="CPyInt16_Overflow",
error_kind=ERR_ALWAYS,
)
uint8_overflow = custom_op(
arg_types=[],
return_type=void_rtype,
c_function_name="CPyUInt8_Overflow",
error_kind=ERR_ALWAYS,
)