Debian Codegraph @

rpython/jit/backend/llgraph/support.py

raw symbols SCIP
1
2 from rpython.jit.codewriter import longlong
3 from rpython.jit.metainterp.support import adr2int, ptr2int
4 from rpython.jit.metainterp.history import getkind
5
6 from rpython.rlib.rarithmetic import r_longlong, r_ulonglong, r_uint
7 from rpython.rtyper.lltypesystem import lltype, rffi, llmemory
8
9 IS_32_BIT = r_ulonglong is not r_uint
10
11 kind2TYPE = {
12 'i': lltype.Signed,
13 'f': lltype.Float,
14 'L': lltype.SignedLongLong,
15 'S': lltype.SingleFloat,
16 'v': lltype.Void,
17 }
18
19 def cast_to_int(x):
20 TP = lltype.typeOf(x)
21 if isinstance(TP, lltype.Ptr):
22 return ptr2int(x)
23 if TP == llmemory.Address:
24 return adr2int(x)
25 if TP is lltype.SingleFloat:
26 return longlong.singlefloat2int(x)
27 return lltype.cast_primitive(lltype.Signed, x)
28
29 def cast_to_ptr(x):
30 assert isinstance(lltype.typeOf(x), lltype.Ptr)
31 return lltype.cast_opaque_ptr(llmemory.GCREF, x)
32
33 def cast_to_floatstorage(x):
34 if isinstance(x, float):
35 return longlong.getfloatstorage(x) # common case
36 if IS_32_BIT:
37 assert longlong.supports_longlong
38 if isinstance(x, r_longlong):
39 return x
40 if isinstance(x, r_ulonglong):
41 return rffi.cast(lltype.SignedLongLong, x)
42 raise TypeError(type(x))
43
44 def cast_result(TP, x):
45 kind = getkind(TP)
46 if kind == 'int':
47 return cast_to_int(x)
48 elif kind == 'ref':
49 return cast_to_ptr(x)
50 elif kind == 'float':
51 return cast_to_floatstorage(x)
52 else:
53 assert kind == 'void'
54 assert x is None
55 return None
56
57 def cast_from_floatstorage(TYPE, x):
58 assert isinstance(x, longlong.r_float_storage)
59 if TYPE is lltype.Float:
60 return longlong.getrealfloat(x)
61 if longlong.is_longlong(TYPE):
62 return rffi.cast(TYPE, x)
63 raise TypeError(TYPE)
64
65 def cast_from_int(TYPE, x):
66 if isinstance(TYPE, lltype.Ptr):
67 if isinstance(x, (int, long, llmemory.AddressAsInt)):
68 x = llmemory.cast_int_to_adr(x)
69 try: # pom pom pom
70 return llmemory.cast_adr_to_ptr(x, TYPE)
71 except Exception:
72 # assume that we want a "C-style" cast, without typechecking the value
73 return rffi.cast(TYPE, x)
74 elif TYPE == llmemory.Address:
75 if isinstance(x, (int, long, llmemory.AddressAsInt)):
76 x = llmemory.cast_int_to_adr(x)
77 assert lltype.typeOf(x) == llmemory.Address
78 return x
79 elif TYPE is lltype.SingleFloat:
80 assert lltype.typeOf(x) is lltype.Signed
81 return longlong.int2singlefloat(x)
82 else:
83 if lltype.typeOf(x) == llmemory.Address:
84 x = adr2int(x)
85 return lltype.cast_primitive(TYPE, x)
86
87 def cast_from_ptr(TYPE, x):
88 if lltype.typeOf(x) == TYPE:
89 return x
90 return lltype.cast_opaque_ptr(TYPE, x)
91
92 def cast_arg(TP, x):
93 kind = getkind(TP)
94 if kind == 'int':
95 return cast_from_int(TP, x)
96 elif kind == 'ref':
97 return cast_from_ptr(TP, x)
98 else:
99 assert kind == 'float'
100 return cast_from_floatstorage(TP, x)
101
102 def cast_call_args(ARGS, args_i, args_r, args_f, args_in_order=None):
103 argsiter_i = iter(args_i or [])
104 argsiter_r = iter(args_r or [])
105 argsiter_f = iter(args_f or [])
106 if args_in_order is not None:
107 orderiter = iter(args_in_order)
108 args = []
109 for TYPE in ARGS:
110 if TYPE is lltype.Void:
111 x = None
112 else:
113 if isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'gc':
114 if args_in_order is not None:
115 n = orderiter.next()
116 assert n == 'r'
117 x = argsiter_r.next()
118 x = cast_from_ptr(TYPE, x)
119 elif TYPE is lltype.Float or longlong.is_longlong(TYPE):
120 if args_in_order is not None:
121 n = orderiter.next()
122 assert n == 'f'
123 x = argsiter_f.next()
124 x = cast_from_floatstorage(TYPE, x)
125 else:
126 if args_in_order is not None:
127 n = orderiter.next()
128 assert n == 'i'
129 x = argsiter_i.next()
130 x = cast_from_int(TYPE, x)
131 args.append(x)
132 assert list(argsiter_i) == []
133 assert list(argsiter_r) == []
134 assert list(argsiter_f) == []
135 return args
136
137 def cast_call_args_in_order(ARGS, args):
138 call_args = []
139 i = 0
140 for ARG in ARGS:
141 kind = getkind(ARG)
142 if kind == 'int':
143 n = cast_from_int(ARG, args[i])
144 i += 1
145 elif kind == 'ref':
146 n = cast_from_ptr(ARG, args[i])
147 i += 1
148 elif kind == 'float':
149 n = cast_from_floatstorage(ARG, args[i])
150 i += 1
151 elif kind == 'void':
152 n = None
153 else:
154 raise AssertionError(kind)
155 call_args.append(n)
156 assert i == len(args)
157 return call_args
158
159 def addr_add_bytes(addr, ofs):
160 if (isinstance(ofs, int) and
161 getattr(addr.adr.ptr._TYPE.TO, 'OF', None) == lltype.Char):
162 return addr + ofs
163 ptr = rffi.cast(rffi.CCHARP, addr.adr)
164 ptr = lltype.direct_ptradd(ptr, ofs)
165 return cast_to_int(ptr)