Debian Codegraph @

rpython/rtyper/lltypesystem/test/test_lloperation.py

raw symbols SCIP
1 import py
2 from rpython.rtyper.lltypesystem.lloperation import LL_OPERATIONS, llop, void
3 from rpython.rtyper.lltypesystem import lltype, opimpl
4 from rpython.rtyper.llinterp import LLFrame
5 from rpython.rtyper.test.test_llinterp import interpret
6 from rpython.rtyper import rclass
7 from rpython.rlib.rarithmetic import LONGLONG_MASK, r_longlong, r_ulonglong
8
9 LL_INTERP_OPERATIONS = [name[3:] for name in LLFrame.__dict__.keys()
10 if name.startswith('op_')]
11
12 # ____________________________________________________________
13
14 def test_canfold_opimpl_complete():
15 for opname, llop in LL_OPERATIONS.items():
16 assert opname == llop.opname
17 if llop.canfold:
18 func = opimpl.get_op_impl(opname)
19 assert callable(func)
20
21 def test_llop_fold():
22 assert llop.int_add(lltype.Signed, 10, 2) == 12
23 assert llop.int_add(lltype.Signed, -6, -7) == -13
24 S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
25 s1 = lltype.malloc(S1)
26 s1.x = 123
27 assert llop.getfield(lltype.Signed, s1, 'x') == 123
28 S2 = lltype.GcStruct('S2', ('x', lltype.Signed))
29 s2 = lltype.malloc(S2)
30 s2.x = 123
31 py.test.raises(TypeError, "llop.getfield(lltype.Signed, s2, 'x')")
32
33 def test_llop_interp():
34 from rpython.rtyper.annlowlevel import LowLevelAnnotatorPolicy
35 def llf(x, y):
36 return llop.int_add(lltype.Signed, x, y)
37 res = interpret(llf, [5, 7], policy=LowLevelAnnotatorPolicy())
38 assert res == 12
39
40 def test_llop_with_voids_interp():
41 from rpython.rtyper.annlowlevel import LowLevelAnnotatorPolicy
42 S = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
43 name_y = void('y')
44 def llf():
45 s = lltype.malloc(S)
46 llop.bare_setfield(lltype.Void, s, void('x'), 3)
47 llop.bare_setfield(lltype.Void, s, name_y, 2)
48 return s.x + s.y
49 res = interpret(llf, [], policy=LowLevelAnnotatorPolicy())
50 assert res == 5
51
52 def test_is_pure():
53 from rpython.flowspace.model import Variable, Constant
54 assert llop.bool_not.is_pure([Variable()])
55 assert llop.debug_assert.is_pure([Variable()])
56 assert not llop.int_add_ovf.is_pure([Variable(), Variable()])
57 #
58 S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
59 v_s1 = Variable()
60 v_s1.concretetype = lltype.Ptr(S1)
61 assert not llop.setfield.is_pure([v_s1, Constant('x'), Variable()])
62 assert not llop.getfield.is_pure([v_s1, Constant('y')])
63 #
64 A1 = lltype.GcArray(lltype.Signed)
65 v_a1 = Variable()
66 v_a1.concretetype = lltype.Ptr(A1)
67 assert not llop.setarrayitem.is_pure([v_a1, Variable(), Variable()])
68 assert not llop.getarrayitem.is_pure([v_a1, Variable()])
69 assert llop.getarraysize.is_pure([v_a1])
70 #
71 S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
72 hints={'immutable': True})
73 v_s2 = Variable()
74 v_s2.concretetype = lltype.Ptr(S2)
75 assert not llop.setfield.is_pure([v_s2, Constant('x'), Variable()])
76 assert llop.getfield.is_pure([v_s2, Constant('y')])
77 #
78 A2 = lltype.GcArray(lltype.Signed, hints={'immutable': True})
79 v_a2 = Variable()
80 v_a2.concretetype = lltype.Ptr(A2)
81 assert not llop.setarrayitem.is_pure([v_a2, Variable(), Variable()])
82 assert llop.getarrayitem.is_pure([v_a2, Variable()])
83 assert llop.getarraysize.is_pure([v_a2])
84 #
85 for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE,
86 rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE,
87 rclass.IR_QUASIIMMUTABLE_ARRAY]:
88 accessor = rclass.FieldListAccessor()
89 S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
90 hints={'immutable_fields': accessor})
91 accessor.initialize(S3, {'x': kind})
92 v_s3 = Variable()
93 v_s3.concretetype = lltype.Ptr(S3)
94 assert not llop.setfield.is_pure([v_s3, Constant('x'), Variable()])
95 assert not llop.setfield.is_pure([v_s3, Constant('y'), Variable()])
96 assert llop.getfield.is_pure([v_s3, Constant('x')]) is kind
97 assert not llop.getfield.is_pure([v_s3, Constant('y')])
98
99 def test_getfield_pure():
100 S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
101 S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
102 hints={'immutable': True})
103 accessor = rclass.FieldListAccessor()
104 #
105 s1 = lltype.malloc(S1); s1.x = 45
106 py.test.raises(TypeError, llop.getfield, lltype.Signed, s1, 'x')
107 s2 = lltype.malloc(S2); s2.x = 45
108 assert llop.getfield(lltype.Signed, s2, 'x') == 45
109 #
110 py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s1, 'x')
111 assert llop.getinteriorfield(lltype.Signed, s2, 'x') == 45
112 #
113 for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE,
114 rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE,
115 rclass.IR_QUASIIMMUTABLE_ARRAY]:
116 #
117 S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
118 hints={'immutable_fields': accessor})
119 accessor.initialize(S3, {'x': kind})
120 s3 = lltype.malloc(S3); s3.x = 46; s3.y = 47
121 if kind in [rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY]:
122 assert llop.getfield(lltype.Signed, s3, 'x') == 46
123 assert llop.getinteriorfield(lltype.Signed, s3, 'x') == 46
124 else:
125 py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'x')
126 py.test.raises(TypeError, llop.getinteriorfield,
127 lltype.Signed, s3, 'x')
128 py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'y')
129 py.test.raises(TypeError, llop.getinteriorfield,
130 lltype.Signed, s3, 'y')
131
132 def test_cast_float_to_ulonglong():
133 f = 12350000000000000000.0
134 py.test.raises(OverflowError, r_longlong, f)
135 r_longlong(f / 2) # does not raise OverflowError
136 #
137 x = llop.cast_float_to_ulonglong(lltype.UnsignedLongLong, f)
138 assert x == r_ulonglong(f)
139
140 # ___________________________________________________________________________
141 # This tests that the LLInterpreter and the LL_OPERATIONS tables are in sync.
142
143 def test_table_complete():
144 for opname in LL_INTERP_OPERATIONS:
145 assert opname in LL_OPERATIONS
146
147 def test_llinterp_complete():
148 for opname, llop in LL_OPERATIONS.items():
149 if llop.canrun:
150 continue
151 assert opname in LL_INTERP_OPERATIONS