Debian Codegraph @

rpython/translator/c/test/test_database.py

raw symbols SCIP
1 import sys
2 from rpython.rtyper.lltypesystem.lltype import *
3 from rpython.translator.translator import TranslationContext
4 from rpython.translator.c.database import LowLevelDatabase
5 from rpython.flowspace.model import Constant, Variable, SpaceOperation
6 from rpython.flowspace.model import Block, Link, FunctionGraph
7 from rpython.rtyper.lltypesystem.lltype import getfunctionptr
8 from rpython.rtyper.lltypesystem.rffi import (VOIDP, INT_real, INT, CArrayPtr,
9 CONST_CCHARP, CONST_VOIDP)
10
11
12 def dump_on_stdout(database):
13 print '/*********************************/'
14 structdeflist = database.getstructdeflist()
15 for node in structdeflist:
16 for line in node.definition():
17 print line
18 print
19 for node in database.globalcontainers():
20 for line in node.forward_declaration():
21 print line
22 for node in database.globalcontainers():
23 print
24 for line in node.implementation():
25 print line
26
27
28 def test_primitive():
29 db = LowLevelDatabase()
30 if is_emulated_long:
31 assert db.get(5) == '5LL'
32 else:
33 assert db.get(5) == '5L'
34 assert db.get(True) == '1'
35
36 def test_struct():
37 db = LowLevelDatabase()
38 pfx = db.namespace.global_prefix + 'g_'
39 S = GcStruct('test', ('x', Signed))
40 s = malloc(S)
41 s.x = 42
42 assert db.get(s).startswith('(&'+pfx)
43 assert db.containernodes.keys() == [s._obj]
44 assert db.structdefnodes.keys() == [S]
45
46 def test_inlined_struct():
47 db = LowLevelDatabase()
48 pfx = db.namespace.global_prefix + 'g_'
49 S = GcStruct('test', ('x', Struct('subtest', ('y', Signed))))
50 s = malloc(S)
51 s.x.y = 42
52 assert db.get(s).startswith('(&'+pfx)
53 assert db.containernodes.keys() == [s._obj]
54 db.complete()
55 assert len(db.structdefnodes) == 2
56 assert S in db.structdefnodes
57 assert S.x in db.structdefnodes
58
59 def test_complete():
60 db = LowLevelDatabase()
61 pfx = db.namespace.global_prefix + 'g_'
62 T = GcStruct('subtest', ('y', Signed))
63 S = GcStruct('test', ('x', Ptr(T)))
64 s = malloc(S)
65 s.x = malloc(T)
66 s.x.y = 42
67 assert db.get(s).startswith('(&'+pfx)
68 assert db.containernodes.keys() == [s._obj]
69 db.complete()
70 assert len(db.containernodes) == 2
71 assert s._obj in db.containernodes
72 assert s.x._obj in db.containernodes
73 assert len(db.structdefnodes) == 2
74 assert S in db.structdefnodes
75 assert S.x.TO in db.structdefnodes
76
77 def test_codegen():
78 db = LowLevelDatabase()
79 U = Struct('inlined', ('z', Signed))
80 T = Struct('subtest', ('y', Signed))
81 S = Struct('test', ('x', Ptr(T)), ('u', U), ('p', Ptr(U)))
82 s = malloc(S, immortal=True)
83 s.x = malloc(T, immortal=True)
84 s.x.y = 42
85 s.u.z = -100
86 s.p = s.u
87 db.get(s)
88 db.complete()
89 dump_on_stdout(db)
90
91 def test_codegen_2():
92 db = LowLevelDatabase()
93 A = GcArray(('x', Signed))
94 S = GcStruct('test', ('aptr', Ptr(A)))
95 a = malloc(A, 3)
96 a[0].x = 100
97 a[1].x = 101
98 a[2].x = 102
99 s = malloc(S)
100 s.aptr = a
101 db.get(s)
102 db.complete()
103 dump_on_stdout(db)
104
105 def test_codegen_3():
106 db = LowLevelDatabase()
107 A = Struct('varsizedstuff', ('x', Signed), ('y', Array(('i', Signed))))
108 S = Struct('test', ('aptr', Ptr(A)),
109 ('anitem', Ptr(A.y.OF)),
110 ('anarray', Ptr(A.y)))
111 a = malloc(A, 3, immortal=True)
112 a.x = 99
113 a.y[0].i = 100
114 a.y[1].i = 101
115 a.y[2].i = 102
116 s = malloc(S, immortal=True)
117 s.aptr = a
118 s.anitem = a.y[1]
119 s.anarray = a.y
120 db.get(s)
121 db.complete()
122 dump_on_stdout(db)
123
124 def test_func_simple():
125 # -------------------- flowgraph building --------------------
126 # def f(x):
127 # return x+1
128 x = Variable("x")
129 x.concretetype = Signed
130 result = Variable("result")
131 result.concretetype = Signed
132 one = Constant(1)
133 one.concretetype = Signed
134 op = SpaceOperation("int_add", [x, one], result)
135 block = Block([x])
136 graph = FunctionGraph("f", block)
137 block.operations.append(op)
138 block.closeblock(Link([result], graph.returnblock))
139 graph.getreturnvar().concretetype = Signed
140 # -------------------- end --------------------
141
142 F = FuncType([Signed], Signed)
143 f = functionptr(F, "f", graph=graph)
144 db = LowLevelDatabase()
145 db.get(f)
146 db.complete()
147 dump_on_stdout(db)
148
149 S = GcStruct('testing', ('fptr', Ptr(F)))
150 s = malloc(S)
151 s.fptr = f
152 db = LowLevelDatabase()
153 db.get(s)
154 db.complete()
155 dump_on_stdout(db)
156
157 # ____________________________________________________________
158
159 def makegraph(func, argtypes):
160 t = TranslationContext()
161 t.buildannotator().build_types(func, [int])
162 t.buildrtyper().specialize()
163 bk = t.annotator.bookkeeper
164 graph = bk.getdesc(func).getuniquegraph()
165 return t, graph
166
167 def test_function_call():
168 def g(x, y):
169 return x-y
170 def f(x):
171 return g(1, x)
172 t, graph = makegraph(f, [int])
173
174 F = FuncType([Signed], Signed)
175 f = functionptr(F, "f", graph=graph)
176 db = LowLevelDatabase(t, exctransformer=t.getexceptiontransformer())
177 db.get(f)
178 db.complete()
179 dump_on_stdout(db)
180
181
182 def test_malloc():
183 S = GcStruct('testing', ('x', Signed), ('y', Signed))
184 def ll_f(x):
185 p = malloc(S)
186 p.x = x
187 p.y = x+1
188 return p.x * p.y
189 t, graph = makegraph(ll_f, [int])
190
191 db = LowLevelDatabase(t, exctransformer=t.getexceptiontransformer())
192 db.get(getfunctionptr(graph))
193 db.complete()
194 dump_on_stdout(db)
195
196 def test_multiple_malloc():
197 S1 = GcStruct('testing1', ('x', Signed), ('y', Signed))
198 S = GcStruct('testing', ('ptr1', Ptr(S1)),
199 ('ptr2', Ptr(S1)),
200 ('z', Signed))
201 def ll_f(x):
202 ptr1 = malloc(S1)
203 ptr1.x = x
204 ptr2 = malloc(S1)
205 ptr2.x = x+1
206 s = malloc(S)
207 s.ptr1 = ptr1
208 s.ptr2 = ptr2
209 return s.ptr1.x * s.ptr2.x
210 t, graph = makegraph(ll_f, [int])
211
212 db = LowLevelDatabase(t, exctransformer=t.getexceptiontransformer())
213 db.get(getfunctionptr(graph))
214 db.complete()
215 dump_on_stdout(db)
216
217 def test_array_of_char():
218 A = GcArray(Char)
219 a = malloc(A, 11)
220 for i, c in zip(range(11), 'hello world'):
221 a[i] = c
222 db = LowLevelDatabase()
223 db.get(a)
224 db.complete()
225 dump_on_stdout(db)
226
227 def test_voidp():
228 A = VOIDP
229 db = LowLevelDatabase()
230 assert db.gettype(A) == "void *@"
231
232 def test_intlong_unique():
233 A = INT_real
234 B = Signed
235 db = LowLevelDatabase()
236 assert db.gettype(A) == "int @"
237 assert db.gettype(B) == "Signed @"
238
239
240 def test_recursive_struct():
241 S = GcForwardReference()
242 S.become(GcStruct('testing', ('p', Ptr(S))))
243 p = malloc(S)
244 p.p = p
245 db = LowLevelDatabase()
246 db.get(p)
247 db.complete()
248 dump_on_stdout(db)
249
250 def test_typedef():
251 A = Typedef(Signed, 'test4')
252 db = LowLevelDatabase()
253 assert db.gettype(A) == "test4 @"
254
255 PA = CArrayPtr(A)
256 assert db.gettype(PA) == "test4 *@"
257
258 F = FuncType((A,), A)
259 assert db.gettype(F) == "test4 (@)(test4)"
260
261 def test_const_types():
262 db = LowLevelDatabase()
263
264 assert db.gettype(CONST_CCHARP) == "char const *@"
265 assert db.gettype(CONST_VOIDP) == "const void *@"
266
267 CONST_CCHARPP = Ptr(Array(CONST_CCHARP, hints={'nolength': True, 'render_as_const': True}))
268 tp = db.gettype(CONST_CCHARPP)
269 assert tp == "char const *const *@"
270