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
- fun inline_routine(routine: IRoutine, args: Sequence[IRegister]): nullable IRegister
+ # Require not is_currently_inlining
+ fun inline_routine(routine: IRoutine, args: Sequence[IRegister], closdefs: nullable Sequence[nullable IClosureDef]): nullable IRegister
do
- var d = new ICodeDupContext
+ 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
+ var cdefsa = if closdefs != null then closdefs.length else 0
+ var cdeclsa = if closdecls != null then closdecls.length else 0
+ assert cdefsa <= cdeclsa
# Fill register duplicate association
var dico = d._registers
#seq.icodes.add(new IMove(dico[routine.params[i]]), args[i]))
end
+ # Fill closure association
+ if closdecls != null then
+ var cdico = d._closures
+ for i in [0..cdefsa[ do
+ cdico[closdecls[i]] = closdefs[i]
+ end
+ for i in [cdefsa..cdeclsa[ do
+ cdico[closdecls[i]] = null
+ end
+ end
+
# Process inlining
- seq.icodes.add(routine.body.dup_with(d))
+ routine.body.dup_with(d)
+ _current_inlining.pop
return res
end
end
# The assocation between old_ireg and new_ireg
# Used by dup_ireg
var _registers: Map[IRegister, IRegister] = new HashMap[IRegister, IRegister]
+
+ # The association between a closure_decl and its closure_def (if any)
+ var _closures: Map[IClosureDecl, nullable IClosureDef] = new ArrayMap[IClosureDecl, nullable IClosureDef]
+
+ # The current code builder
+ var _icb: ICodeBuilder
+
+ init(icb: ICodeBuilder)
+ do
+ _icb = icb
+ end
end
redef class ICode
- # Duplicate the current icode (generic part)
- private fun dup_with(d: ICodeDupContext): ICode
+ # Duplicate the current icode in the icode builder of the ICodeDupContext
+ private fun dup_with(d: ICodeDupContext)
do
var c = inner_dup_with(d)
+ if self isa ICodeN then
+ assert c isa ICodeN
+ c.closure_defs = closure_defs
+ end
var r = result
if r != null then c.result = d.dup_ireg(r)
c.location = location
- return c
+ d._icb.seq.icodes.add(c)
end
- # Duplicate the current icode (specific part)
+ # Simle partial duplication of the current icode
private fun inner_dup_with(d: ICodeDupContext): ICode is abstract
end
# Note: dest must be empty and not modified afted duplication or IEscapes may be wrongly duplicated
private fun dup_seq_to(d: ICodeDupContext, dest: ISeq)
do
+ var old_seq = d._icb.seq
+ d._icb.seq = dest
d._seqs[self] = dest
for c in icodes do
- dest.icodes.add(c.dup_with(d))
+ c.dup_with(d)
end
+ d._icb.seq = old_seq
end
end
end
redef class IClosCall
- redef fun inner_dup_with(d)
+ redef fun dup_with(d)
do
- var c2 = new IClosCall(closure_decl, d.dup_iregs(exprs))
- var bs = break_seq
- if bs != null then
- var bs2 = new ISeq
- c2.break_seq = bs2
- bs.dup_seq_to(d, bs2)
+ var closdef = d._closures[closure_decl]
+ if closdef == null then
+ # Default is already guarded and inlined
+ return
+ end
+ # Break sequence cannot be inlined :/
+ assert break_seq == null
+ var res = d._icb.inline_routine(closdef, d.dup_iregs(exprs), null)
+ if result != null then
+ assert res != null
+ d._icb.stmt(new IMove(d.dup_ireg(result.as(not null)), res))
end
- return c2
end
end
redef class IHasClos
redef fun inner_dup_with(d)
do
- return new IHasClos(closure_decl)
+ var closdef = d._closures[closure_decl]
+ var res: IRegister
+ if closdef != null then
+ res = d._icb.lit_true_reg
+ else
+ res = d._icb.lit_false_reg
+ end
+ return new IMove(d.dup_ireg(result.as(not null)), res)
end
end