Debian Codegraph @

rpython/rtyper/callparse.py

raw symbols SCIP
1 from rpython.annotator.argument import ArgumentsForTranslation, ArgErr
2 from rpython.annotator import model as annmodel
3 from rpython.rtyper import rtuple
4 from rpython.rtyper.error import TyperError
5 from rpython.rtyper.lltypesystem import lltype
6
7 class ArgumentsForRtype(ArgumentsForTranslation):
8 def newtuple(self, items):
9 return NewTupleHolder(items)
10
11 def unpackiterable(self, it):
12 assert it.is_tuple()
13 items = it.items()
14 return list(items)
15
16 def getrinputs(rtyper, graph):
17 """Return the list of reprs of the input arguments to the 'graph'."""
18 return [rtyper.bindingrepr(v) for v in graph.getargs()]
19
20 def getrresult(rtyper, graph):
21 """Return the repr of the result variable of the 'graph'."""
22 if graph.getreturnvar().annotation is not None:
23 return rtyper.bindingrepr(graph.getreturnvar())
24 else:
25 return lltype.Void
26
27 def getsig(rtyper, graph):
28 """Return the complete 'signature' of the graph."""
29 return (graph.signature,
30 graph.defaults,
31 getrinputs(rtyper, graph),
32 getrresult(rtyper, graph))
33
34 def callparse(rtyper, graph, hop, r_self=None):
35 """Parse the arguments of 'hop' when calling the given 'graph'.
36 """
37 rinputs = getrinputs(rtyper, graph)
38 def args_h(start):
39 return [VarHolder(i, hop.args_s[i])
40 for i in range(start, hop.nb_args)]
41 if r_self is None:
42 start = 1
43 else:
44 start = 0
45 rinputs[0] = r_self
46 opname = hop.spaceop.opname
47 if opname == "simple_call":
48 arguments = ArgumentsForRtype(args_h(start))
49 elif opname == "call_args":
50 arguments = ArgumentsForRtype.fromshape(
51 hop.args_s[start].const, # shape
52 args_h(start+1))
53 # parse the arguments according to the function we are calling
54 signature = graph.signature
55 defs_h = []
56 if graph.defaults:
57 for x in graph.defaults:
58 defs_h.append(ConstHolder(x))
59 try:
60 holders = arguments.match_signature(signature, defs_h)
61 except ArgErr as e:
62 raise TyperError("signature mismatch: %s: %s" % (
63 graph.name, e.getmsg()))
64
65 assert len(holders) == len(rinputs), "argument parsing mismatch"
66 vlist = []
67 for h,r in zip(holders, rinputs):
68 v = h.emit(r, hop)
69 vlist.append(v)
70 return vlist
71
72
73 class Holder(object):
74
75 def is_tuple(self):
76 return False
77
78 def emit(self, repr, hop):
79 try:
80 cache = self._cache
81 except AttributeError:
82 cache = self._cache = {}
83 try:
84 return cache[repr]
85 except KeyError:
86 v = self._emit(repr, hop)
87 cache[repr] = v
88 return v
89
90
91 class VarHolder(Holder):
92
93 def __init__(self, num, s_obj):
94 self.num = num
95 self.s_obj = s_obj
96
97 def is_tuple(self):
98 return isinstance(self.s_obj, annmodel.SomeTuple)
99
100 def items(self):
101 assert self.is_tuple()
102 n = len(self.s_obj.items)
103 return tuple([ItemHolder(self, i) for i in range(n)])
104
105 def _emit(self, repr, hop):
106 return hop.inputarg(repr, arg=self.num)
107
108 def access(self, hop):
109 repr = hop.args_r[self.num]
110 return repr, self.emit(repr, hop)
111
112 class ConstHolder(Holder):
113 def __init__(self, value):
114 self.value = value
115
116 def is_tuple(self):
117 return type(self.value) is tuple
118
119 def items(self):
120 assert self.is_tuple()
121 return self.value
122
123 def _emit(self, repr, hop):
124 return hop.inputconst(repr, self.value)
125
126
127 class NewTupleHolder(Holder):
128 def __new__(cls, holders):
129 for h in holders:
130 if not isinstance(h, ItemHolder) or not h.holder == holders[0].holder:
131 break
132 else:
133 if 0 < len(holders) == len(holders[0].holder.items()):
134 return holders[0].holder
135 inst = Holder.__new__(cls)
136 inst.holders = tuple(holders)
137 return inst
138
139 def is_tuple(self):
140 return True
141
142 def items(self):
143 return self.holders
144
145 def _emit(self, repr, hop):
146 assert isinstance(repr, rtuple.TupleRepr)
147 tupleitems_v = []
148 for h in self.holders:
149 v = h.emit(repr.items_r[len(tupleitems_v)], hop)
150 tupleitems_v.append(v)
151 vtuple = repr.newtuple(hop.llops, repr, tupleitems_v)
152 return vtuple
153
154
155 class ItemHolder(Holder):
156 def __init__(self, holder, index):
157 self.holder = holder
158 self.index = index
159
160 def _emit(self, repr, hop):
161 index = self.index
162 r_tup, v_tuple = self.holder.access(hop)
163 v = r_tup.getitem_internal(hop, v_tuple, index)
164 return hop.llops.convertvar(v, r_tup.items_r[index], repr)