Skip to content

Commit 39fe615

Browse files
authored
Add a primitive for id (mypyc/mypyc#684)
1 parent 3a900f9 commit 39fe615

File tree

4 files changed

+79
-94
lines changed

4 files changed

+79
-94
lines changed

mypyc/lib-rt/CPy.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ static inline bool CPyTagged_IsLe(CPyTagged left, CPyTagged right) {
624624
}
625625
}
626626

627+
static CPyTagged CPyTagged_Id(PyObject *o) {
628+
return CPyTagged_FromSsize_t((Py_ssize_t)o);
629+
}
630+
627631
static PyObject *CPyList_GetItemUnsafe(PyObject *list, CPyTagged index) {
628632
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
629633
PyObject *result = PyList_GET_ITEM(list, n);

mypyc/ops_misc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
emit=name_emit('Py_NotImplemented'),
4949
is_borrowed=True)
5050

51+
func_op(name='builtins.id',
52+
arg_types=[object_rprimitive],
53+
result_type=int_rprimitive,
54+
error_kind=ERR_NEVER,
55+
emit=call_emit('CPyTagged_Id'))
56+
5157
iter_op = func_op(name='builtins.iter',
5258
arg_types=[object_rprimitive],
5359
result_type=object_rprimitive,

test-data/analysis.test

Lines changed: 65 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -511,104 +511,85 @@ def lol(x: object) -> int:
511511
return st + 1
512512
[out]
513513
def lol(x):
514-
x, r0 :: object
515-
r1 :: str
516-
r2, r3 :: object
517-
r4, st :: int
518-
r5 :: tuple[object, object, object]
519-
r6 :: object
520-
r7 :: str
521-
r8 :: object
522-
r9 :: bool
514+
x :: object
515+
r0, st :: int
516+
r1 :: tuple[object, object, object]
517+
r2 :: object
518+
r3 :: str
519+
r4 :: object
520+
r5 :: bool
521+
r6 :: short_int
522+
r7 :: int
523+
r8, r9 :: bool
523524
r10 :: short_int
524-
r11 :: int
525-
r12, r13 :: bool
526-
r14 :: short_int
527-
r15, r16 :: int
525+
r11, r12 :: int
528526
L0:
529527
L1:
530-
r0 = builtins.module :: static
531-
r1 = unicode_1 :: static ('id')
532-
r2 = getattr r0, r1
533-
if is_error(r2) goto L5 (error at lol:3) else goto L2
528+
r0 = id x :: object
529+
st = r0
530+
goto L10
534531
L2:
535-
r3 = py_call(r2, x)
536-
if is_error(r3) goto L5 (error at lol:3) else goto L3
532+
r1 = error_catch
533+
r2 = builtins.module :: static
534+
r3 = unicode_1 :: static ('Exception')
535+
r4 = getattr r2, r3
536+
if is_error(r4) goto L8 (error at lol:4) else goto L3
537537
L3:
538-
r4 = unbox(int, r3)
539-
if is_error(r4) goto L5 (error at lol:3) else goto L4
538+
r5 = exc_matches r4
539+
if r5 goto L4 else goto L5 :: bool
540540
L4:
541-
st = r4
542-
goto L13
541+
r6 = 1
542+
r7 = -r6 :: int
543+
restore_exc_info r1
544+
return r7
543545
L5:
544-
r5 = error_catch
545-
r6 = builtins.module :: static
546-
r7 = unicode_2 :: static ('Exception')
547-
r8 = getattr r6, r7
548-
if is_error(r8) goto L11 (error at lol:4) else goto L6
546+
reraise_exc; r8 = 0
547+
if not r8 goto L8 else goto L6 :: bool
549548
L6:
550-
r9 = exc_matches r8
551-
if r9 goto L7 else goto L8 :: bool
549+
unreachable
552550
L7:
553-
r10 = 1
554-
r11 = -r10 :: int
555-
restore_exc_info r5
556-
return r11
551+
restore_exc_info r1
552+
goto L10
557553
L8:
558-
reraise_exc; r12 = 0
559-
if not r12 goto L11 else goto L9 :: bool
554+
restore_exc_info r1
555+
r9 = keep_propagating
556+
if not r9 goto L11 else goto L9 :: bool
560557
L9:
561558
unreachable
562559
L10:
563-
restore_exc_info r5
564-
goto L13
560+
r10 = 1
561+
r11 = st + r10 :: int
562+
return r11
565563
L11:
566-
restore_exc_info r5
567-
r13 = keep_propagating
568-
if not r13 goto L14 else goto L12 :: bool
569-
L12:
570-
unreachable
571-
L13:
572-
r14 = 1
573-
r15 = st + r14 :: int
574-
return r15
575-
L14:
576-
r16 = <error> :: int
577-
return r16
564+
r12 = <error> :: int
565+
return r12
578566
(0, 0) {x} {x}
579-
(1, 0) {x} {r0, x}
580-
(1, 1) {r0, x} {r0, r1, x}
581-
(1, 2) {r0, r1, x} {r2, x}
582-
(1, 3) {r2, x} {r2, x}
583-
(2, 0) {r2, x} {r3}
584-
(2, 1) {r3} {r3}
585-
(3, 0) {r3} {r4}
586-
(3, 1) {r4} {r4}
587-
(4, 0) {r4} {st}
588-
(4, 1) {st} {st}
589-
(5, 0) {} {r5}
590-
(5, 1) {r5} {r5, r6}
591-
(5, 2) {r5, r6} {r5, r6, r7}
592-
(5, 3) {r5, r6, r7} {r5, r8}
593-
(5, 4) {r5, r8} {r5, r8}
594-
(6, 0) {r5, r8} {r5, r9}
595-
(6, 1) {r5, r9} {r5}
596-
(7, 0) {r5} {r10, r5}
597-
(7, 1) {r10, r5} {r11, r5}
598-
(7, 2) {r11, r5} {r11}
599-
(7, 3) {r11} {}
600-
(8, 0) {r5} {r12, r5}
601-
(8, 1) {r12, r5} {r5}
567+
(1, 0) {x} {r0}
568+
(1, 1) {r0} {st}
569+
(1, 2) {st} {st}
570+
(2, 0) {} {r1}
571+
(2, 1) {r1} {r1, r2}
572+
(2, 2) {r1, r2} {r1, r2, r3}
573+
(2, 3) {r1, r2, r3} {r1, r4}
574+
(2, 4) {r1, r4} {r1, r4}
575+
(3, 0) {r1, r4} {r1, r5}
576+
(3, 1) {r1, r5} {r1}
577+
(4, 0) {r1} {r1, r6}
578+
(4, 1) {r1, r6} {r1, r7}
579+
(4, 2) {r1, r7} {r7}
580+
(4, 3) {r7} {}
581+
(5, 0) {r1} {r1, r8}
582+
(5, 1) {r1, r8} {r1}
583+
(6, 0) {} {}
584+
(7, 0) {r1, st} {st}
585+
(7, 1) {st} {st}
586+
(8, 0) {r1} {}
587+
(8, 1) {} {r9}
588+
(8, 2) {r9} {}
602589
(9, 0) {} {}
603-
(10, 0) {r5, st} {st}
604-
(10, 1) {st} {st}
605-
(11, 0) {r5} {}
606-
(11, 1) {} {r13}
607-
(11, 2) {r13} {}
608-
(12, 0) {} {}
609-
(13, 0) {st} {r14, st}
610-
(13, 1) {r14, st} {r15}
611-
(13, 2) {r15} {}
612-
(14, 0) {} {r16}
613-
(14, 1) {r16} {}
590+
(10, 0) {st} {r10, st}
591+
(10, 1) {r10, st} {r11}
592+
(10, 2) {r11} {}
593+
(11, 0) {} {r12}
594+
(11, 1) {r12} {}
614595

test-data/genops-classes.test

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,11 @@ L0:
262262
return r1
263263
def C.foo(self, x):
264264
self :: C
265-
x, r0 :: object
266-
r1 :: str
267-
r2, r3 :: object
268-
r4 :: int
265+
x :: object
266+
r0 :: int
269267
L0:
270-
r0 = builtins.module :: static
271-
r1 = unicode_5 :: static ('id')
272-
r2 = getattr r0, r1
273-
r3 = py_call(r2, x)
274-
r4 = unbox(int, r3)
275-
return r4
268+
r0 = id x :: object
269+
return r0
276270
def C.foo__B_glue(self, x):
277271
self :: C
278272
x :: object

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