icode: do no do recursive inline
[nit.git] / src / analysis / inline_methods.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 # Detect inlinable methods and inline them
18 package inline_methods
19
20 import icode
21
22 private class InlineMethodVisitor
23 special ICodeVisitor
24 var _pass: Int = 0
25 var _icb: ICodeBuilder
26
27 var _current_inlining: Array[IRoutine]
28
29 redef fun visit_icode(ic)
30 do
31 if ic isa ICall then
32 var m = ic.property
33 var ir = m.iroutine
34 if ir != null and ic.is_inlinable then
35 if _current_inlining.has(ir) then
36 # We cannot inline ir
37 # FIXME: what we want is a static call
38 else
39 var icb = _icb
40 _current_inlining.push(ir)
41 var seq = new ISeq
42 var old_seq = icb.seq
43 icb.seq = seq
44 current_icode.insert_before(seq)
45 var e = icb.inline_routine(ir, ic.exprs, ic.closure_defs)
46 var r = ic.result
47 if r != null then
48 assert e != null
49 current_icode.insert_before(new IMove(r, e))
50 end
51 current_icode.delete
52 icb.seq = old_seq
53 visit_icode(seq)
54 _current_inlining.pop
55 end
56 end
57 end
58 super
59 end
60
61 init(m: MMModule, r: IRoutine)
62 do
63 _current_inlining = [r]
64 _icb = new ICodeBuilder(m, r)
65 end
66 end
67
68 redef class ICall
69 fun is_inlinable: Bool
70 do
71 var m = property
72 var mn = m.name
73 var cn = m.local_class.name
74 return (m.is_intern and cn != once ("Object".to_symbol)) or
75 (cn == (once ("Array".to_symbol)) and (mn == (once ("length".to_symbol)) or mn == (once ("[]".to_symbol)))) or
76 (cn == (once ("AbstractArrayRead".to_symbol)) and (mn == (once ("length".to_symbol)) or mn == (once ("[]".to_symbol)))) or
77 (m.global.intro.local_class.name == (once ("Inline__".to_symbol)))
78 end
79 end
80
81 redef class IRoutine
82 fun inline_methods(m: MMModule)
83 do
84 var v = new InlineMethodVisitor(m, self)
85 v.visit_iroutine(self)
86 end
87 end