Debian Codegraph @

rpython/translator/c/test/test_symbolic.py

raw symbols SCIP
1 from rpython.translator.interactive import Translation
2 from rpython.translator.c.test.test_genc import compile
3 from rpython.conftest import option
4 from rpython.rtyper.lltypesystem import llmemory, lltype
5 from rpython.rlib.objectmodel import ComputedIntSymbolic
6
7 def test_offsetof():
8 STRUCT = lltype.GcStruct("s", ("x", lltype.Signed), ("y", lltype.Signed))
9 offsetx = llmemory.offsetof(STRUCT, 'x')
10 offsety = llmemory.offsetof(STRUCT, 'y')
11 def f():
12 s = lltype.malloc(STRUCT)
13 s.x = 1
14 adr = llmemory.cast_ptr_to_adr(s)
15 result = (adr + offsetx).signed[0]
16 (adr + offsety).signed[0] = 2
17 return result * 10 + s.y
18 fn = compile(f, [])
19 res = fn()
20 assert res == 12
21
22 def test_offsetof_nogc():
23 ARR = lltype.Array(lltype.Signed)
24 offsetx = llmemory.arraylengthoffset(ARR)
25 offsety = llmemory.itemoffsetof(ARR, 0)
26 ARR2 = lltype.GcArray(lltype.Signed)
27 offsetx2 = llmemory.arraylengthoffset(ARR2)
28 offsety2 = llmemory.itemoffsetof(ARR2, 0)
29
30 def f():
31 a = lltype.malloc(ARR, 5, flavor='raw')
32 a2 = lltype.malloc(ARR2, 6, flavor='raw')
33 a2[0] = 1
34 a[0] = 3
35 adr = llmemory.cast_ptr_to_adr(a)
36 adr2 = llmemory.cast_ptr_to_adr(a2)
37 return ((adr + offsetx).signed[0] * 1000 +
38 (adr + offsety).signed[0] * 100 +
39 (adr2 + offsetx2).signed[0] * 10 + (adr2 + offsety2).signed[0])
40
41 fn = compile(f, [])
42 res = fn()
43 assert res == 5361
44
45 def test_sizeof_array_with_no_length():
46 A = lltype.Array(lltype.Signed, hints={'nolength': True})
47 arraysize = llmemory.sizeof(A, 10)
48 signedsize = llmemory.sizeof(lltype.Signed)
49 def f():
50 return arraysize-signedsize*10
51 fn = compile(f, [])
52 res = fn()
53 assert res == 0
54
55 def test_itemoffsetof():
56 ARRAY = lltype.GcArray(lltype.Signed)
57 itemoffsets = [llmemory.itemoffsetof(ARRAY, i) for i in range(5)]
58 def f():
59 a = lltype.malloc(ARRAY, 5)
60 adr = llmemory.cast_ptr_to_adr(a)
61 result = 0
62 for i in range(5):
63 a[i] = i + 1
64 for i in range(5):
65 result = result * 10 + (adr + itemoffsets[i]).signed[0]
66 for i in range(5):
67 (adr + itemoffsets[i]).signed[0] = i
68 for i in range(5):
69 result = 10 * result + a[i]
70 return result
71 fn = compile(f, [])
72 res = fn()
73 assert res == 1234501234
74
75 def test_itemoffsetof_fixedsizearray():
76 ARRAY = lltype.FixedSizeArray(lltype.Signed, 5)
77 itemoffsets = [llmemory.itemoffsetof(ARRAY, i) for i in range(5)]
78 a = lltype.malloc(ARRAY, immortal=True)
79 def f():
80 adr = llmemory.cast_ptr_to_adr(a)
81 result = 0
82 for i in range(5):
83 a[i] = i + 1
84 for i in range(5):
85 result = result * 10 + (adr + itemoffsets[i]).signed[0]
86 for i in range(5):
87 (adr + itemoffsets[i]).signed[0] = i
88 for i in range(5):
89 result = 10 * result + a[i]
90 return result
91 fn = compile(f, [])
92 res = fn()
93 assert res == 1234501234
94
95 def test_sizeof_constsize_struct():
96 # _not_ a GcStruct, since we want to raw_malloc it
97 STRUCT = lltype.Struct("s", ("x", lltype.Signed), ("y", lltype.Signed))
98 STRUCTPTR = lltype.Ptr(STRUCT)
99 sizeofs = llmemory.sizeof(STRUCT)
100 offsety = llmemory.offsetof(STRUCT, 'y')
101 def f():
102 adr = llmemory.raw_malloc(sizeofs)
103 s = llmemory.cast_adr_to_ptr(adr, STRUCTPTR)
104 s.y = 5 # does not crash
105 result = (adr + offsety).signed[0] * 10 + int(offsety < sizeofs)
106 llmemory.raw_free(adr)
107 return result
108 fn = compile(f, [])
109 res = fn()
110 assert res == 51
111
112 def test_computed_int_symbolic():
113 too_early = True
114 def compute_fn():
115 assert not too_early
116 return 7
117 k = ComputedIntSymbolic(compute_fn)
118 def f(ignored):
119 return k*6
120
121 t = Translation(f)
122 t.rtype()
123 if option.view:
124 t.view()
125 too_early = False
126 fn = compile(f, [int])
127 res = fn(0)
128 assert res == 42