1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 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.
18 # Rapid type analysis on the AST
20 # Rapid type analysis is an analyse that aproximates the set of live classes
21 # and the set of live methods starting from the entry point of the program.
22 # These two sets are interdependant and computed together.
23 # It is quite efficient but the type set is global and pollutes each call site.
24 module rapid_type_analysis
27 private import explain_assert_api
29 import csv
# for live_types_to_csv
30 private import ordered_tree
# for live_methods_to_tree
32 private import more_collections
34 redef class ModelBuilder
35 # Performs a rapid-type-analysis on the program associated with `mainmodule`.
36 fun do_rapid_type_analysis
(mainmodule
: MModule): RapidTypeAnalysis
38 var analysis
= new RapidTypeAnalysis(self, mainmodule
)
41 if toolcontext
.opt_log
.value
then
42 var basename
= toolcontext
.log_directory
/ mainmodule
.name
43 analysis
.live_methods_to_tree
.write_to_file
(basename
+ ".rta_methods.txt")
44 analysis
.live_types_to_csv
.write_to_file
(basename
+ ".rta_types.csv")
51 # RapidTypeAnalysis looks for alive rapid types in application.
52 # The entry point of the analysis is the mainmodule of the application.
53 class RapidTypeAnalysis
54 # The modelbuilder used to get the AST.
55 var modelbuilder
: ModelBuilder
57 # The main module of the analysis.
58 # Used to perform types operations.
59 var mainmodule
: MModule
61 # The pool to live types.
62 # During the analysis, new types are added and combined with
63 # live_methods to determine new methoddefs to visit
64 var live_types
= new HashSet[MClassType]
66 # The pool of unresolved live types
67 # They are globally resolved at the end of the analaysis
68 var live_open_types
= new HashSet[MClassType]
70 # Live (instantiated) classes.
71 var live_classes
= new HashSet[MClass]
73 # The pool of types used to perform type checks (isa and as).
74 var live_cast_types
= new HashSet[MType]
76 # The pool of undesolved types used to perform type checks (isa and as).
77 # They are globally resolved at the end of the analaysis
78 var live_open_cast_types
= new HashSet[MType]
80 # Live method definitions.
81 var live_methoddefs
= new HashSet[MMethodDef]
84 var live_methods
= new HashSet[MMethod]
87 # Those with a live method definitions (see `live_methoddefs`)
88 var live_mmodules
= new HashSet[MModule]
91 var live_callsites
= new HashSet[CallSite]
93 private var live_targets_cache
= new HashMap2[MType, MProperty, Set[MMethodDef]]
95 # The live targets of a specific callsite.
96 fun live_targets
(callsite
: CallSite): Set[MMethodDef]
98 var mtype
= callsite
.recv
99 var anchor
= callsite
.anchor
100 if anchor
!= null then mtype
= mtype
.anchor_to
(callsite
.mmodule
, anchor
)
101 mtype
= mtype
.undecorate
102 if mtype
isa MClassType then mtype
= mtype
.mclass
.intro
.bound_mtype
103 var mproperty
= callsite
.mproperty
104 var res
= live_targets_cache
[mtype
, mproperty
]
105 if res
!= null then return res
106 res
= new ArraySet[MMethodDef]
107 live_targets_cache
[mtype
, mproperty
] = res
109 for c
in live_classes
do
110 var tc
= c
.intro
.bound_mtype
111 if not tc
.is_subtype
(mainmodule
, null, mtype
) then continue
112 var d
= mproperty
.lookup_first_definition
(mainmodule
, tc
)
119 # Live call-to-super.
120 var live_super_sends
= new HashSet[MMethodDef]
122 # Return a ready-to-save CSV document objet that agregates informations about live types.
123 # Each discovered type is listed in a line, with its status: resolution, liveness, cast-liveness.
124 # Note: types are listed in an alphanumeric order to improve human reading.
125 fun live_types_to_csv
: CsvDocument
127 # Gather all kind of type
128 var typeset
= new HashSet[MType]
129 typeset
.add_all
(live_types
)
130 typeset
.add_all
(live_open_types
)
131 typeset
.add_all
(live_cast_types
)
132 typeset
.add_all
(live_open_cast_types
)
133 var types
= typeset
.to_a
134 (new CachedAlphaComparator).sort
(types
)
135 var res
= new CsvDocument
137 res
.header
= ["Type", "Resolution", "Liveness", "Cast-liveness"]
140 if t
.need_anchor
then reso
= "OPEN " else reso
= "CLOSED"
142 if t
isa MClassType and (live_types
.has
(t
) or live_open_types
.has
(t
)) then live
= "LIVE" else live
= "DEAD"
144 if live_cast_types
.has
(t
) or live_open_cast_types
.has
(t
) then cast
= "CAST LIVE" else cast
= "CAST DEAD"
145 res
.add_record
(t
, reso
, live
, cast
)
150 # Return a ready-to-save OrderedTree object that agregates infomration about live methods.
151 # Note: methods are listed in an alphanumeric order to improve human reading.
152 fun live_methods_to_tree
: OrderedTree[Object]
154 var tree
= new OrderedTree[Object]
155 for x
in live_methods
do
158 for z
in x
.mpropdefs
do
160 if live_methoddefs
.has
(z
) then
162 if live_super_sends
.has
(z
) then
163 tree
.add
(zn
, zn
+ "(super)")
165 else if live_super_sends
.has
(z
) then
166 tree
.add
(xn
, zn
+ "(super)")
170 tree
.sort_with
(alpha_comparator
)
174 # Methods that are still candidate to the try_send
175 private var totry_methods
= new HashSet[MMethod]
177 # Methods that are are no more candidate to the try_send
178 private var totry_methods_to_remove
= new Array[MMethod]
180 # Methods that are or were candidate to the try_send
181 # Used to ensure that try_send is only used once
182 private var try_methods
= new HashSet[MMethod]
184 # The method definitions that remain to visit
185 private var todo
= new List[MMethodDef]
187 private fun force_alive
(classname
: String)
189 var classes
= self.modelbuilder
.model
.get_mclasses_by_name
(classname
)
190 if classes
!= null then for c
in classes
do self.add_new
(c
.mclass_type
, c
.mclass_type
)
193 # Run the analysis until all visitable method definitions are visited.
196 var maintype
= mainmodule
.sys_type
197 if maintype
== null then return # No entry point
198 add_new
(maintype
, maintype
)
199 var initprop
= mainmodule
.try_get_primitive_method
("init", maintype
.mclass
)
200 if initprop
!= null then
201 add_send
(maintype
, initprop
)
203 var mainprop
= mainmodule
.try_get_primitive_method
("run", maintype
.mclass
) or else
204 mainmodule
.try_get_primitive_method
("main", maintype
.mclass
)
205 if mainprop
!= null then
206 add_send
(maintype
, mainprop
)
209 var finalizable_type
= mainmodule
.finalizable_type
210 if finalizable_type
!= null then
211 var finalize_meth
= mainmodule
.try_get_primitive_method
("finalize", finalizable_type
.mclass
)
212 if finalize_meth
!= null then add_send
(finalizable_type
, finalize_meth
)
215 # Force primitive types
219 force_alive
("Pointer")
224 force_alive
("UInt16")
226 force_alive
("UInt32")
228 while not todo
.is_empty
do
229 var mmethoddef
= todo
.shift
230 var mmeth
= mmethoddef
.mproperty
231 var msignature
= mmethoddef
.msignature
232 if msignature
== null then continue # Skip broken method
234 #print "# visit {mmethoddef}"
235 var v
= new RapidTypeVisitor(self, mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
)
237 var vararg_rank
= msignature
.vararg_rank
238 if vararg_rank
> -1 then
239 var node
= self.modelbuilder
.mpropdef2node
(mmethoddef
)
240 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
241 #elttype = elttype.anchor_to(self.mainmodule, v.receiver)
242 var vararg
= self.mainmodule
.array_type
(elttype
)
244 var native
= self.mainmodule
.native_array_type
(elttype
)
246 v
.add_monomorphic_send
(vararg
, self.modelbuilder
.force_get_primitive_method
(node
, "with_native", vararg
.mclass
, self.mainmodule
))
249 # TODO? new_msignature
251 var osig
= mmeth
.intro
.msignature
.as(not null)
252 for i
in [0..sig
.arity
[ do
253 var origtype
= osig
.mparameters
[i
].mtype
254 if not origtype
.need_anchor
then continue # skip non covariant stuff
255 var paramtype
= sig
.mparameters
[i
].mtype
259 if mmethoddef
.is_abstract
then continue
261 var npropdef
= modelbuilder
.mpropdef2node
(mmethoddef
)
263 if npropdef
isa AClassdef then
264 if mmethoddef
.mproperty
.is_root_init
then
265 if not mmethoddef
.is_intro
then
266 self.add_super_send
(v
.receiver
, mmethoddef
)
269 npropdef
.debug
"cannot RTA {mmethoddef}"
273 else if mmethoddef
.constant_value
!= null then
274 # Make the return type live
275 v
.add_type
(msignature
.return_mtype
.as(MClassType))
277 else if npropdef
== null then
281 if npropdef
isa AMethPropdef then
282 var auto_super_inits
= npropdef
.auto_super_inits
283 if auto_super_inits
!= null then
284 for auto_super_init
in auto_super_inits
do
285 v
.add_callsite
(auto_super_init
)
288 if npropdef
.auto_super_call
then
289 self.add_super_send
(v
.receiver
, mmethoddef
)
293 if mmethoddef
.is_intern
or mmethoddef
.is_extern
then
294 # UGLY: We force the "instantation" of the concrete return type if any
295 var ret
= msignature
.return_mtype
296 if ret
!= null and ret
isa MClassType and ret
.mclass
.kind
!= abstract_kind
and ret
.mclass
.kind
!= interface_kind
then
301 v
.enter_visit
(npropdef
)
304 #print "MMethod {live_methods.length}: {live_methods.join(", ")}"
305 #print "MMethodDef {live_methoddefs.length}: {live_methoddefs.join(", ")}"
307 #print "open MType {live_open_types.length}: {live_open_types.join(", ")}"
308 var todo_types
= new List[MClassType]
309 todo_types
.add_all
(live_types
)
310 while not todo_types
.is_empty
do
311 var t
= todo_types
.shift
312 for ot
in live_open_types
do
314 if not ot
.can_resolve_for
(t
, t
, mainmodule
) then continue
315 var rt
= ot
.anchor_to
(mainmodule
, t
)
316 if live_types
.has
(rt
) then continue
317 if not rt
.is_legal_in
(mainmodule
) then continue
318 if not check_depth
(rt
) then continue
319 #print "{ot}/{t} -> {rt}"
321 # unshift means a deep-first visit.
322 # So that the `check_depth` limit is reached sooner.
323 todo_types
.unshift
(rt
)
326 #print "MType {live_types.length}: {live_types.join(", ")}"
328 #print "open cast MType {live_open_cast_types.length}: {live_open_cast_types.join(", ")}"
329 for ot
in live_open_cast_types
do
330 #print "live_open_cast_type: {ot}"
331 for t
in live_types
do
332 if not ot
.can_resolve_for
(t
, t
, mainmodule
) then continue
333 var rt
= ot
.anchor_to
(mainmodule
, t
)
334 if not rt
.is_legal_in
(mainmodule
) then continue
335 live_cast_types
.add
(rt
)
336 #print " {ot}/{t} -> {rt}"
339 #print "cast MType {live_cast_types.length}: {live_cast_types.join(", ")}"
342 private fun check_depth
(mtype
: MClassType): Bool
346 self.modelbuilder
.toolcontext
.fatal_error
(null, "Fatal Error: limitation in the rapidtype analysis engine: a type depth of {d} is too important, the problematic type is `{mtype}`.")
352 fun add_new
(recv
: MClassType, mtype
: MClassType)
354 assert not recv
.need_anchor
355 if mtype
.need_anchor
then
356 if live_open_types
.has
(mtype
) then return
357 live_open_types
.add
(mtype
)
359 if live_types
.has
(mtype
) then return
360 live_types
.add
(mtype
)
363 var mclass
= mtype
.mclass
364 if live_classes
.has
(mclass
) then return
365 live_classes
.add
(mclass
)
367 for p
in totry_methods
do try_send
(mtype
, p
)
368 for p
in live_super_sends
do try_super_send
(mtype
, p
)
370 # Remove cleared ones
371 for p
in totry_methods_to_remove
do totry_methods
.remove
(p
)
372 totry_methods_to_remove
.clear
374 var bound_mtype
= mtype
.anchor_to
(mainmodule
, recv
)
375 for cd
in bound_mtype
.collect_mclassdefs
(mainmodule
)
377 for npropdef
in modelbuilder
.collect_attr_propdef
(cd
) do
378 if not npropdef
.has_value
then continue
380 var mpropdef
= npropdef
.mreadpropdef
.as(not null)
381 var v
= new RapidTypeVisitor(self, bound_mtype
, mpropdef
)
382 v
.enter_visit
(npropdef
.n_expr
)
383 v
.enter_visit
(npropdef
.n_block
)
389 fun add_cast
(mtype
: MType)
391 if mtype
.need_anchor
then
392 live_open_cast_types
.add
(mtype
)
394 live_cast_types
.add
(mtype
)
398 fun try_send
(recv
: MClassType, mproperty
: MMethod)
400 recv
= recv
.mclass
.intro
.bound_mtype
401 if not recv
.has_mproperty
(mainmodule
, mproperty
) then return
402 var d
= mproperty
.lookup_first_definition
(mainmodule
, recv
)
406 fun add_call
(mpropdef
: MMethodDef)
408 if live_methoddefs
.has
(mpropdef
) then return
409 live_methoddefs
.add
(mpropdef
)
410 live_mmodules
.add
(mpropdef
.mclassdef
.mmodule
)
413 var mproperty
= mpropdef
.mproperty
414 if mproperty
.mpropdefs
.length
<= 1 then return
415 # If all definitions of a method are live, we can remove the definition of the totry set
416 for d
in mproperty
.mpropdefs
do
417 if not live_methoddefs
.has
(d
) then return
419 #print "full property: {mpropdef.mproperty} for {mpropdef.mproperty.mpropdefs.length} definitions"
420 totry_methods_to_remove
.add
(mpropdef
.mproperty
)
423 fun add_send
(recv
: MType, mproperty
: MMethod)
425 if try_methods
.has
(mproperty
) then return
426 #print "new prop: {mproperty}"
427 live_methods
.add
(mproperty
)
428 try_methods
.add
(mproperty
)
429 if mproperty
.mpropdefs
.length
== 1 then
430 # If there is only one definition, just add the definition and do not try again the property
431 var d
= mproperty
.mpropdefs
.first
435 # Else, the property is potentially called with various reciever
436 # So just try the methods with existing receiver and register it for future receiver
437 totry_methods
.add
(mproperty
)
438 for c
in live_classes
do
439 try_send
(c
.intro
.bound_mtype
, mproperty
)
443 fun try_super_send
(recv
: MClassType, mpropdef
: MMethodDef)
445 recv
= recv
.mclass
.intro
.bound_mtype
446 if not recv
.collect_mclassdefs
(mainmodule
).has
(mpropdef
.mclassdef
) then return
447 var d
= mpropdef
.lookup_next_definition
(mainmodule
, recv
)
451 fun add_super_send
(recv
: MType, mpropdef
: MMethodDef)
453 assert mpropdef
.has_supercall
454 if live_super_sends
.has
(mpropdef
) then return
455 #print "new super prop: {mpropdef}"
456 live_super_sends
.add
(mpropdef
)
457 for c
in live_classes
do
458 try_super_send
(c
.intro
.bound_mtype
, mpropdef
)
463 class RapidTypeVisitor
466 var analysis
: RapidTypeAnalysis
467 var receiver
: MClassType
468 var mpropdef
: MPropDef
472 assert not receiver
.need_anchor
478 if n
.mtype
!= null or n
.is_typed
then
479 n
.accept_rapid_type_visitor
(self)
480 var implicit_cast_to
= n
.implicit_cast_to
481 if implicit_cast_to
!= null then self.add_cast_type
(implicit_cast_to
)
484 n
.accept_rapid_type_visitor
(self)
487 # RTA does not enter in AAnnotations
488 if not n
isa AAnnotations then
493 fun cleanup_type
(mtype
: MType): nullable MClassType
495 mtype
= mtype
.anchor_to
(self.analysis
.mainmodule
, self.receiver
)
496 if mtype
isa MNullType then return null
497 mtype
= mtype
.undecorate
498 assert mtype
isa MClassType
499 assert not mtype
.need_anchor
503 fun get_method
(recv
: MType, name
: String): MMethod
505 var mtype
= cleanup_type
(recv
)
507 return self.analysis
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, mtype
.mclass
, self.analysis
.mainmodule
)
510 fun add_type
(mtype
: MClassType) do analysis
.add_new
(receiver
, mtype
)
512 fun add_monomorphic_send
(mtype
: MType, mproperty
: MMethod)
514 analysis
.live_methods
.add
(mproperty
)
515 analysis
.try_send
(mtype
.as(MClassType), mproperty
)
518 fun add_send
(mtype
: MType, mproperty
: MMethod) do analysis
.add_send
(mtype
, mproperty
)
520 fun add_cast_type
(mtype
: MType) do analysis
.add_cast
(mtype
)
522 fun add_callsite
(callsite
: nullable CallSite) do if callsite
!= null then
523 for m
in callsite
.mpropdef
.initializers
do
524 if m
isa MMethod then
525 analysis
.add_send
(callsite
.recv
, m
)
528 analysis
.add_send
(callsite
.recv
, callsite
.mproperty
)
529 analysis
.live_callsites
.add
(callsite
)
536 private fun accept_rapid_type_visitor
(v
: RapidTypeVisitor)
542 # Make the `mtype` of the expression live
543 # Used by literals and instantiations
544 fun allocate_mtype
(v
: RapidTypeVisitor)
546 var mtype
= self.mtype
547 if not mtype
isa MClassType then return
548 v
.add_type
(self.mtype
.as(MClassType))
552 redef class AIntegerExpr
553 redef fun accept_rapid_type_visitor
(v
)
559 redef class AFloatExpr
560 redef fun accept_rapid_type_visitor
(v
)
566 redef class ACharExpr
567 redef fun accept_rapid_type_visitor
(v
)
573 redef class AArrayExpr
574 redef fun accept_rapid_type_visitor
(v
)
576 var mtype
= self.mtype
.as(MClassType)
578 var native
= v
.analysis
.mainmodule
.native_array_type
(mtype
.arguments
.first
)
580 mtype
= v
.cleanup_type
(mtype
).as(not null)
581 var prop
= v
.get_method
(mtype
, "with_native")
582 v
.add_monomorphic_send
(mtype
, prop
)
583 v
.add_callsite
(with_capacity_callsite
)
584 v
.add_callsite
(push_callsite
)
588 redef class AStringFormExpr
589 redef fun accept_rapid_type_visitor
(v
)
591 var native
= v
.analysis
.mainmodule
.c_string_type
593 var prop
= v
.get_method
(native
, "to_s_unsafe")
594 v
.add_monomorphic_send
(native
, prop
)
595 v
.add_callsite
(to_re
)
596 v
.add_callsite
(ignore_case
)
597 v
.add_callsite
(newline
)
598 v
.add_callsite
(extended
)
599 v
.add_callsite
(to_bytes_with_copy
)
603 redef class ASuperstringExpr
604 redef fun accept_rapid_type_visitor
(v
)
606 var mmodule
= v
.analysis
.mainmodule
607 var object_type
= mmodule
.string_type
608 var arraytype
= mmodule
.array_type
(object_type
)
609 v
.add_type
(arraytype
)
610 var nattype
= mmodule
.native_array_type
(object_type
)
612 var prop
= v
.get_method
(arraytype
, "join")
613 v
.add_monomorphic_send
(arraytype
, prop
)
614 var prop2
= v
.get_method
(arraytype
, "with_native")
615 v
.add_monomorphic_send
(arraytype
, prop2
)
616 v
.add_monomorphic_send
(nattype
, v
.get_method
(nattype
, "native_to_s"))
620 redef class ACrangeExpr
621 redef fun accept_rapid_type_visitor
(v
)
623 var mtype
= self.mtype
624 if not mtype
isa MClassType then return
626 v
.add_callsite
(init_callsite
)
630 redef class AOrangeExpr
631 redef fun accept_rapid_type_visitor
(v
)
633 var mtype
= self.mtype
634 if not mtype
isa MClassType then return
636 v
.add_callsite
(init_callsite
)
640 redef class ATrueExpr
641 redef fun accept_rapid_type_visitor
(v
)
647 redef class AFalseExpr
648 redef fun accept_rapid_type_visitor
(v
)
655 redef fun accept_rapid_type_visitor
(v
)
657 var cast_type
= self.cast_type
658 if cast_type
== null then return
659 v
.add_cast_type
(cast_type
)
663 redef class AAsCastExpr
664 redef fun accept_rapid_type_visitor
(v
)
666 var mtype
= self.mtype
667 if mtype
== null then return
668 v
.add_cast_type
(mtype
)
672 redef class AAssertExpr
673 redef fun accept_rapid_type_visitor
(v
)
675 if can_explain_assert
(v
.analysis
.modelbuilder
) then
676 var str
= explain_assert_str
677 if str
!= null then str
.accept_rapid_type_visitor
(v
)
681 # Does `modelbuilder` know the classes to build a superstring to explain a failed assert?
682 private fun can_explain_assert
(modelbuilder
: ModelBuilder): Bool
684 var nas
= modelbuilder
.model
.get_mclasses_by_name
("NativeArray")
685 if nas
== null then return false
687 nas
= modelbuilder
.model
.get_mclasses_by_name
("Array")
688 if nas
== null or nas
.is_empty
then return false
690 nas
= modelbuilder
.model
.get_mclasses_by_name
("String")
691 if nas
== null or nas
.is_empty
then return false
697 redef class ASendExpr
698 redef fun accept_rapid_type_visitor
(v
)
700 v
.add_callsite
(callsite
)
705 redef class ASendReassignFormExpr
706 redef fun accept_rapid_type_visitor
(v
)
708 v
.add_callsite
(callsite
)
709 v
.add_callsite
(reassign_callsite
)
710 v
.add_callsite
(write_callsite
)
714 redef class AVarReassignExpr
715 redef fun accept_rapid_type_visitor
(v
)
717 v
.add_callsite
(reassign_callsite
)
721 redef class AAttrReassignExpr
722 redef fun accept_rapid_type_visitor
(v
)
724 v
.add_callsite
(reassign_callsite
)
728 redef class ASuperExpr
729 redef fun accept_rapid_type_visitor
(v
)
731 var callsite
= self.callsite
732 if callsite
!= null then
733 v
.add_callsite
(callsite
)
737 v
.analysis
.add_super_send
(v
.receiver
, mpropdef
.as(not null))
741 redef class AForGroup
742 redef fun accept_rapid_type_visitor
(v
)
744 v
.add_callsite
(self.method_iterator
)
745 v
.add_callsite
(self.method_is_ok
)
746 if self.variables
.length
== 1 then
747 v
.add_callsite
(self.method_item
)
748 else if self.variables
.length
== 2 then
749 v
.add_callsite
(self.method_key
)
750 v
.add_callsite
(self.method_item
)
754 v
.add_callsite
(self.method_next
)
755 var mf
= self.method_finish
756 if mf
!= null then v
.add_callsite
(mf
)
761 redef fun accept_rapid_type_visitor
(v
)
763 var mtype
= self.recvtype
.as(not null)
765 v
.add_callsite
(callsite
)