var _pass: Int = 0
var _icb: ICodeBuilder
+ var _current_inlining: Array[IRoutine]
+
redef fun visit_icode(ic)
do
if ic isa ICall then
var m = ic.property
- if m.iroutine != null and ic.is_inlinable then
- var icb = _icb
- var ir = m.iroutine.as(not null)
- var seq = new ISeq
- var old_seq = icb.seq
- icb.seq = seq
- current_icode.insert_before(seq)
- var e = icb.inline_routine(ir, ic.exprs, ic.closure_defs)
- var r = ic.result
- if r != null then
- assert e != null
- current_icode.insert_before(new IMove(r, e))
+ var ir = m.iroutine
+ if ir != null and ic.is_inlinable then
+ if _current_inlining.has(ir) then
+ # We cannot inline ir
+ # FIXME: what we want is a static call
+ else
+ var icb = _icb
+ _current_inlining.push(ir)
+ var seq = new ISeq
+ var old_seq = icb.seq
+ icb.seq = seq
+ current_icode.insert_before(seq)
+ var e = icb.inline_routine(ir, ic.exprs, ic.closure_defs)
+ var r = ic.result
+ if r != null then
+ assert e != null
+ current_icode.insert_before(new IMove(r, e))
+ end
+ current_icode.delete
+ icb.seq = old_seq
+ visit_icode(seq)
+ _current_inlining.pop
end
- current_icode.delete
- icb.seq = old_seq
- visit_icode(seq)
end
end
super
init(m: MMModule, r: IRoutine)
do
+ _current_inlining = [r]
_icb = new ICodeBuilder(m, r)
end
end
end
redef class ICodeBuilder
+ # IRoutine currently inlining
+ # Used to avoid recursive inlining
+ var _current_inlining: Array[IRoutine] = new Array[IRoutine]
+
+ # Return false if routine can be saflely inlined
+ fun is_currently_inlining_routine(routine: IRoutine): Bool
+ do
+ return routine == iroutine or _current_inlining.has(routine)
+ end
+
# Inline an iroutine in the current icode sequence
+ # Require not is_currently_inlining
fun inline_routine(routine: IRoutine, args: Sequence[IRegister], closdefs: nullable Sequence[nullable IClosureDef]): nullable IRegister
do
+ assert not is_currently_inlining_routine(routine)
+ _current_inlining.add(routine)
var d = new ICodeDupContext(self)
assert args.length == routine.params.length
var closdecls = routine.closure_decls
# Process inlining
routine.body.dup_with(d)
+ _current_inlining.pop
return res
end
end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import kernel
+
+interface Inline__
+ fun foo
+ !f
+ do
+ 0.output
+ f
+ 2.output
+ end
+
+ fun bar
+ !b do 11.output
+ do
+ 10.output
+ b
+ 12.output
+ end
+end
+
+class A
+special Inline__
+end
+
+fun test1
+do
+ var a: A = new A
+ 5.output
+ a.bar !b do
+ 15.output
+ a.bar
+ end
+end
+
+fun test2
+do
+ var a: A = new A
+ a.foo !f do
+ 5.output
+ a.bar !b do
+ 15.output
+ a.bar
+ end
+ end
+end
+
+test1
+test2
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import kernel
+
+interface Inline__
+ fun foo do bar.output
+ fun bar: Int = 2
+ fun baz is abstract
+end
+
+class A
+special Inline__
+ redef fun bar do return 20
+ redef fun baz do bar.output
+end
+
+var a: A = new A
+a.foo
+a.bar.output
+a.baz
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import kernel
+
+interface Inline__
+ fun foo(i: Int)
+ do
+ 'F'.output
+ i.output
+ if i > 0 then
+ bar(i-1)
+ end
+ end
+ fun bar(i: Int)
+ do
+ 'B'.output
+ i.output
+ if i > 0 then
+ foo(i-1)
+ end
+ end
+end
+
+class A
+special Inline__
+ redef fun foo(i: Int)
+ do
+ 'A'.output
+ i.output
+ if i > 0 then
+ bar(i-1)
+ end
+ end
+end
+
+var a: A = new A
+a.foo(10)
+a.bar(10)
--- /dev/null
+5
+10
+15
+10
+11
+12
+12
+0
+5
+10
+15
+10
+11
+12
+12
+2
--- /dev/null
+A10
+B9
+F8
+B7
+F6
+B5
+F4
+B3
+A2
+B1
+F0
+B10
+F9
+B8
+F7
+B6
+A5
+B4
+F3
+B2
+F1
+B0