076f125a6e9254453c997a8920c128b8bd976a4a
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2009 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Tools to manipulate intermediace nit code representation
20 # A simple visitor to visit icode structures
22 # Called when a iregister is read in a icode
23 fun visit_iregister_read
(ic
: ICode, r
: IRegister) do end
25 # Called when a iregister is wrote in a icode
26 fun visit_iregister_write
(ic
: ICode, r
: IRegister) do end
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
32 # Called when a icode is visited
33 # Automatically visits iregisters and sub-icodes
34 fun visit_icode
(ic
: nullable ICode)
36 if ic
== null then return
38 var old_icode
= _current_icode
39 var cur
= ic
.icodes
.iterator
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
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
60 visit_iregister_read
(ic
, e
)
62 var closdefs
= ic
.closure_defs
63 if ic
isa IClosCall then
64 visit_icode
(ic
.break_seq
)
66 if closdefs
!= null then
67 visit_closure_defs
(closdefs
)
71 if r
!= null then visit_iregister_write
(ic
, r
)
74 # Called when closure definitions are visited
75 # Automatically visits each closure definition
76 fun visit_closure_defs
(closdefs
: Collection[nullable IClosureDef])
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)
94 redef class ICodeBuilder
95 # Inline an iroutine in the current icode sequence
96 fun inline_routine
(routine
: IRoutine, args
: Sequence[IRegister], closdefs
: nullable Sequence[nullable IClosureDef]): nullable IRegister
98 var d
= new ICodeDupContext(self)
99 assert args
.length
== routine
.params
.length
100 var closdecls
= routine
.closure_decls
101 var cdefsa
= if closdefs
!= null then closdefs
.length
else 0
102 var cdeclsa
= if closdecls
!= null then closdecls
.length
else 0
103 assert cdefsa
<= cdeclsa
105 # Fill register duplicate association
106 var dico
= d
._registers
107 var res
= routine
.result
109 var res2
= new_register
(res
.stype
)
113 for reg
in routine
.registers
do
114 assert not dico
.has_key
(reg
)
115 dico
[reg
] = new_register
(reg
.stype
)
117 for i
in [0..args
.length
[ do
118 # FIXME The following assumes that params are readonly.
119 # The alternative is safe but add one move :/
120 dico
[routine
.params
[i
]] = args
[i
]
121 #seq.icodes.add(new IMove(dico[routine.params[i]]), args[i]))
124 # Fill closure association
125 if closdecls
!= null then
126 var cdico
= d
._closures
127 for i
in [0..cdefsa
[ do
128 cdico
[closdecls
[i
]] = closdefs
[i
]
130 for i
in [cdefsa
..cdeclsa
[ do
131 cdico
[closdecls
[i
]] = null
136 routine
.body
.dup_with
(d
)
141 # This class stores reference to allow correct duplication of icodes
142 private class ICodeDupContext
143 # Return the correct register
144 # * a duplicate of the local register 'r' of the inlined iroutine
145 # * 'r' else (it is a register of the caller iroutine)
146 fun dup_ireg
(r
: IRegister): IRegister
149 if rs
.has_key
(r
) then
156 # Return a correct bunch of registers
157 fun dup_iregs
(regs
: Sequence[IRegister]): Sequence[IRegister]
159 var a
= new Array[IRegister].with_capacity
(regs
.length
)
166 # The associoation between old_seq and new_seq
167 # Directly used by the IEscape
168 var _seqs
: Map[ISeq, ISeq] = new HashMap[ISeq, ISeq]
170 # The assocation between old_ireg and new_ireg
172 var _registers
: Map[IRegister, IRegister] = new HashMap[IRegister, IRegister]
174 # The association between a closure_decl and its closure_def (if any)
175 var _closures
: Map[IClosureDecl, nullable IClosureDef] = new ArrayMap[IClosureDecl, nullable IClosureDef]
177 # The current code builder
178 var _icb
: ICodeBuilder
180 init(icb
: ICodeBuilder)
187 # Duplicate the current icode in the icode builder of the ICodeDupContext
188 private fun dup_with
(d
: ICodeDupContext)
190 var c
= inner_dup_with
(d
)
191 if self isa ICodeN then
193 c
.closure_defs
= closure_defs
196 if r
!= null then c
.result
= d
.dup_ireg
(r
)
197 c
.location
= location
198 d
._icb
.seq
.icodes
.add
(c
)
201 # Simle partial duplication of the current icode
202 private fun inner_dup_with
(d
: ICodeDupContext): ICode is abstract
206 redef fun inner_dup_with
(d
)
213 # Duplicate each icode and store them in dest
214 # Note: dest must be empty and not modified afted duplication or IEscapes may be wrongly duplicated
215 private fun dup_seq_to
(d
: ICodeDupContext, dest
: ISeq)
217 var old_seq
= d
._icb
.seq
228 redef fun inner_dup_with
(d
)
237 redef fun inner_dup_with
(d
)
239 var c2
= new IIf(d
.dup_ireg
(expr
))
240 then_seq
.dup_seq_to
(d
, c2
.then_seq
)
241 else_seq
.dup_seq_to
(d
, c2
.else_seq
)
247 redef fun inner_dup_with
(d
)
249 if d
._seqs
.has_key
(seq
) then
250 # Jump to a duplicated sequence
251 return new IEscape(d
._seqs
[seq
])
253 # Jump to an englobing unduplicated sequence
254 return new IEscape(seq
)
260 redef fun inner_dup_with
(d
)
262 return new IAbort(texts
, module_location
)
267 redef fun inner_dup_with
(d
)
269 return new ICall(property
, d
.dup_iregs
(exprs
))
274 redef fun inner_dup_with
(d
)
276 return new ISuper(property
, d
.dup_iregs
(exprs
))
281 redef fun inner_dup_with
(d
)
283 return new INew(stype
, property
, d
.dup_iregs
(exprs
))
287 redef class IClosCall
288 redef fun dup_with
(d
)
290 var closdef
= d
._closures
[closure_decl
]
291 if closdef
== null then
292 # Default is already guarded and inlined
295 # Break sequence cannot be inlined :/
296 assert break_seq
== null
297 var res
= d
._icb
.inline_routine
(closdef
, d
.dup_iregs
(exprs
), null)
298 if result
!= null then
300 d
._icb
.stmt
(new IMove(d
.dup_ireg
(result
.as(not null)), res
))
306 redef fun inner_dup_with
(d
)
308 var c2
= new INative(code
, d
.dup_iregs
(exprs
))
315 redef fun inner_dup_with
(d
)
317 return new IMove(d
.dup_ireg
(result
.as(not null)), d
.dup_ireg
(expr
))
321 redef class IAttrRead
322 redef fun inner_dup_with
(d
)
324 return new IAttrRead(property
, d
.dup_ireg
(expr
))
328 redef class IAttrWrite
329 redef fun inner_dup_with
(d
)
331 return new IAttrWrite(property
, d
.dup_ireg
(expr1
), d
.dup_ireg
(expr2
))
335 redef class IAttrIsset
336 redef fun inner_dup_with
(d
)
338 return new IAttrIsset(property
, d
.dup_ireg
(expr
))
342 redef class ITypeCheck
343 redef fun inner_dup_with
(d
)
345 return new ITypeCheck(d
.dup_ireg
(expr
), stype
)
350 redef fun inner_dup_with
(d
)
352 return new IIs(d
.dup_ireg
(expr1
), d
.dup_ireg
(expr2
))
357 redef fun inner_dup_with
(d
)
359 return new INot(d
.dup_ireg
(expr
))
364 redef fun inner_dup_with
(d
)
367 body
.dup_seq_to
(d
, c2
.body
)
373 redef fun inner_dup_with
(d
)
375 var closdef
= d
._closures
[closure_decl
]
377 if closdef
!= null then
378 res
= d
._icb
.lit_true_reg
380 res
= d
._icb
.lit_false_reg
382 return new IMove(d
.dup_ireg
(result
.as(not null)), res
)