icode: method inlining moves from IRoutine to ICodeBuilder
[nit.git] / src / icode / icode_tools.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Tools to manipulate intermediace nit code representation
18 import icode_builder
19
20 # A simple visitor to visit icode structures
21 class ICodeVisitor
22 # Called when a iregister is read in a icode
23 fun visit_iregister_read(ic: ICode, r: IRegister) do end
24
25 # Called when a iregister is wrote in a icode
26 fun visit_iregister_write(ic: ICode, r: IRegister) do end
27
28 # The current icode iterator.
29 # Can be used to insert_before, used to change the item or deleted
30 readable var _current_icode: nullable ListIterator[ICode] = null
31
32 # Called when a icode is visited
33 # Automatically visits iregisters and sub-icodes
34 fun visit_icode(ic: nullable ICode)
35 do
36 if ic == null then return
37 if ic isa ISeq then
38 var old_icode = _current_icode
39 var cur = ic.icodes.iterator
40 while cur.is_ok do
41 _current_icode = cur
42 var ic2 = cur.item
43 visit_icode(ic2)
44 cur.next
45 end
46 _current_icode = old_icode
47 else if ic isa IIf then
48 visit_iregister_read(ic, ic.expr)
49 visit_icode(ic.then_seq)
50 visit_icode(ic.else_seq)
51 else if ic isa IOnce then
52 visit_icode(ic.body)
53 else if ic isa ICode1 then
54 visit_iregister_read(ic, ic.expr)
55 else if ic isa ICode2 then
56 visit_iregister_read(ic, ic.expr1)
57 visit_iregister_read(ic, ic.expr2)
58 else if ic isa ICodeN then
59 for e in ic.exprs do
60 visit_iregister_read(ic, e)
61 end
62 var closdefs = ic.closure_defs
63 if ic isa IClosCall then
64 visit_icode(ic.break_seq)
65 end
66 if closdefs != null then
67 visit_closure_defs(closdefs)
68 end
69 end
70 var r = ic.result
71 if r != null then visit_iregister_write(ic, r)
72 end
73
74 # Called when closure definitions are visited
75 # Automatically visits each closure definition
76 fun visit_closure_defs(closdefs: Collection[nullable IClosureDef])
77 do
78 for e in closdefs do
79 if e != null then
80 visit_iroutine(e)
81 end
82 end
83 end
84
85 # Called when an iroutine is visited
86 # Automatically visits the body
87 # Warning: parameters of result registers are not visited
88 fun visit_iroutine(ir: IRoutine)
89 do
90 visit_icode(ir.body)
91 end
92 end
93
94 redef class ICodeBuilder
95 # Inline an iroutine in the current icode sequence
96 fun inline_routine(routine: IRoutine, args: Sequence[IRegister]): nullable IRegister
97 do
98 var d = new ICodeDupContext
99 assert args.length == routine.params.length
100 for i in [0..args.length[ do
101 # FIXME The following assumes that params are readonly.
102 # The alternative is safe but add one move :/
103 d._registers[routine.params[i]] = args[i]
104 #seq.icodes.add(new IMove(d.dup_ireg(routine.params[i]), args[i]))
105 end
106 seq.icodes.add(routine.body.dup_with(d))
107 var r = routine.result
108 if r != null then r = d.dup_ireg(r)
109 return r
110 end
111 end
112
113 # This class stores reference to allow correct duplication of icodes
114 private class ICodeDupContext
115 # Duplicate one register
116 # Subsequent invocation will return the same register
117 fun dup_ireg(r: IRegister): IRegister
118 do
119 var rs = _registers
120 if rs.has_key(r) then
121 return rs[r]
122 else
123 var r2 = new IRegister(r.stype)
124 rs[r] = r2
125 return r2
126 end
127 end
128
129 # Duplicate a bunch of registers
130 # Subsequent invocation will return the same registers
131 fun dup_iregs(regs: Sequence[IRegister]): Sequence[IRegister]
132 do
133 var a = new Array[IRegister].with_capacity(regs.length)
134 for r in regs do
135 a.add(dup_ireg(r))
136 end
137 return a
138 end
139
140 # The associoation between old_seq and new_seq
141 # Directly used by the IEscape
142 var _seqs: Map[ISeq, ISeq] = new HashMap[ISeq, ISeq]
143
144 # The assocation between old_ireg and new_ireg
145 # Used by dup_ireg
146 var _registers: Map[IRegister, IRegister] = new HashMap[IRegister, IRegister]
147 end
148
149 redef class ICode
150 # Duplicate the current icode (generic part)
151 private fun dup_with(d: ICodeDupContext): ICode
152 do
153 var c = inner_dup_with(d)
154 var r = result
155 if r != null then c.result = d.dup_ireg(r)
156 c.location = location
157 return c
158 end
159
160 # Duplicate the current icode (specific part)
161 private fun inner_dup_with(d: ICodeDupContext): ICode is abstract
162 end
163
164 redef class ISeq
165 redef fun inner_dup_with(d)
166 do
167 var c2 = new ISeq
168 dup_seq_to(d, c2)
169 return c2
170 end
171
172 # Duplicate each icode and store them in dest
173 # Note: dest must be empty and not modified afted duplication or IEscapes may be wrongly duplicated
174 private fun dup_seq_to(d: ICodeDupContext, dest: ISeq)
175 do
176 d._seqs[self] = dest
177 for c in icodes do
178 dest.icodes.add(c.dup_with(d))
179 end
180 end
181 end
182
183 redef class ILoop
184 redef fun inner_dup_with(d)
185 do
186 var c2 = new ILoop
187 dup_seq_to(d, c2)
188 return c2
189 end
190 end
191
192 redef class IIf
193 redef fun inner_dup_with(d)
194 do
195 var c2 = new IIf(d.dup_ireg(expr))
196 then_seq.dup_seq_to(d, c2.then_seq)
197 else_seq.dup_seq_to(d, c2.else_seq)
198 return c2
199 end
200 end
201
202 redef class IEscape
203 redef fun inner_dup_with(d)
204 do
205 if d._seqs.has_key(seq) then
206 # Jump to a duplicated sequence
207 return new IEscape(d._seqs[seq])
208 else
209 # Jump to an englobing unduplicated sequence
210 return new IEscape(seq)
211 end
212 end
213 end
214
215 redef class IAbort
216 redef fun inner_dup_with(d)
217 do
218 return new IAbort(texts, module_location)
219 end
220 end
221
222 redef class ICall
223 redef fun inner_dup_with(d)
224 do
225 return new ICall(property, d.dup_iregs(exprs))
226 end
227 end
228
229 redef class ISuper
230 redef fun inner_dup_with(d)
231 do
232 return new ISuper(property, d.dup_iregs(exprs))
233 end
234 end
235
236 redef class INew
237 redef fun inner_dup_with(d)
238 do
239 return new INew(stype, property, d.dup_iregs(exprs))
240 end
241 end
242
243 redef class IClosCall
244 redef fun inner_dup_with(d)
245 do
246 var c2 = new IClosCall(closure_decl, d.dup_iregs(exprs))
247 var bs = break_seq
248 if bs != null then
249 var bs2 = new ISeq
250 c2.break_seq = bs2
251 bs.dup_seq_to(d, bs2)
252 end
253 return c2
254 end
255 end
256
257 redef class INative
258 redef fun inner_dup_with(d)
259 do
260 var c2 = new INative(code, d.dup_iregs(exprs))
261 c2.is_pure = is_pure
262 return c2
263 end
264 end
265
266 redef class IMove
267 redef fun inner_dup_with(d)
268 do
269 return new IMove(d.dup_ireg(result.as(not null)), d.dup_ireg(expr))
270 end
271 end
272
273 redef class IAttrRead
274 redef fun inner_dup_with(d)
275 do
276 return new IAttrRead(property, d.dup_ireg(expr))
277 end
278 end
279
280 redef class IAttrWrite
281 redef fun inner_dup_with(d)
282 do
283 return new IAttrWrite(property, d.dup_ireg(expr1), d.dup_ireg(expr2))
284 end
285 end
286
287 redef class IAttrIsset
288 redef fun inner_dup_with(d)
289 do
290 return new IAttrIsset(property, d.dup_ireg(expr))
291 end
292 end
293
294 redef class ITypeCheck
295 redef fun inner_dup_with(d)
296 do
297 return new ITypeCheck(d.dup_ireg(expr), stype)
298 end
299 end
300
301 redef class IIs
302 redef fun inner_dup_with(d)
303 do
304 return new IIs(d.dup_ireg(expr1), d.dup_ireg(expr2))
305 end
306 end
307
308 redef class INot
309 redef fun inner_dup_with(d)
310 do
311 return new INot(d.dup_ireg(expr))
312 end
313 end
314
315 redef class IOnce
316 redef fun inner_dup_with(d)
317 do
318 var c2 = new IOnce
319 body.dup_seq_to(d, c2.body)
320 return c2
321 end
322 end
323
324 redef class IHasClos
325 redef fun inner_dup_with(d)
326 do
327 return new IHasClos(closure_decl)
328 end
329 end