# It is quite efficient but the type set is global and pollutes each call site.
module rapid_type_analysis
-import model
-import modelbuilder
-import typing
-import auto_super_init
+import semantize
-import csv # for live_types_to_csv
-import ordered_tree # for live_methods_to_tree
+private import csv # for live_types_to_csv
+private import ordered_tree # for live_methods_to_tree
+
+private import more_collections
redef class ModelBuilder
+ # Performs a rapid-type-analysis on the program associated with `mainmodule`.
fun do_rapid_type_analysis(mainmodule: MModule): RapidTypeAnalysis
do
var analysis = new RapidTypeAnalysis(self, mainmodule)
analysis.run_analysis
+
+ if toolcontext.opt_log.value then
+ var basename = toolcontext.log_directory / mainmodule.name
+ analysis.live_methods_to_tree.write_to_file(basename + ".rta_methods.txt")
+ analysis.live_types_to_csv.write_to_file(basename + ".rta_types.csv")
+ end
+
return analysis
end
end
# Live methods.
var live_methods = new HashSet[MMethod]
+ # Live mmodules.
+ # Those with a live method definitions (see `live_methoddefs`)
+ var live_mmodules = new HashSet[MModule]
+
+ # Live callsites.
+ var live_callsites = new HashSet[CallSite]
+
+ private var live_targets_cache = new HashMap2[MType, MProperty, Set[MMethodDef]]
+
+ # The live targets of a specific callsite.
+ fun live_targets(callsite: CallSite): Set[MMethodDef]
+ do
+ var mtype = callsite.recv
+ var anchor = callsite.anchor
+ if anchor != null then mtype = mtype.anchor_to(callsite.mmodule, anchor)
+ mtype = mtype.undecorate
+ if mtype isa MClassType then mtype = mtype.mclass.intro.bound_mtype
+ var mproperty = callsite.mproperty
+ var res = live_targets_cache[mtype, mproperty]
+ if res != null then return res
+ res = new ArraySet[MMethodDef]
+ live_targets_cache[mtype, mproperty] = res
+
+ for c in live_classes do
+ var tc = c.intro.bound_mtype
+ if not tc.is_subtype(mainmodule, null, mtype) then continue
+ var d = mproperty.lookup_first_definition(mainmodule, tc)
+ res.add d
+ end
+
+ return res
+ end
+
# Live call-to-super.
var live_super_sends = new HashSet[MMethodDef]
# Return a ready-to-save CSV document objet that agregates informations about live types.
# Each discovered type is listed in a line, with its status: resolution, liveness, cast-liveness.
# Note: types are listed in an alphanumeric order to improve human reading.
- fun live_types_to_csv: CSVDocument
+ fun live_types_to_csv: CsvDocument
do
# Gather all kind of type
var typeset = new HashSet[MType]
typeset.add_all(live_open_cast_types)
var types = typeset.to_a
(new CachedAlphaComparator).sort(types)
- var res = new CSVDocument
+ var res = new CsvDocument
+ res.format = new CsvFormat('"', ';', "\n")
res.header = ["Type", "Resolution", "Liveness", "Cast-liveness"]
for t in types do
var reso
if t isa MClassType and (live_types.has(t) or live_open_types.has(t)) then live = "LIVE" else live = "DEAD"
var cast
if live_cast_types.has(t) or live_open_cast_types.has(t) then cast = "CAST LIVE" else cast = "CAST DEAD"
- res.add_line(t, reso, live, cast)
+ res.add_record(t, reso, live, cast)
end
return res
end
return tree
end
- # Methods that are are still candidate to the try_send
+ # Methods that are still candidate to the try_send
private var totry_methods = new HashSet[MMethod]
+ # Methods that are are no more candidate to the try_send
+ private var totry_methods_to_remove = new Array[MMethod]
+
+ # Methods that are or were candidate to the try_send
+ # Used to ensure that try_send is only used once
+ private var try_methods = new HashSet[MMethod]
+
# The method definitions that remain to visit
private var todo = new List[MMethodDef]
if initprop != null then
add_send(maintype, initprop)
end
- var mainprop = mainmodule.try_get_primitive_method("main", maintype.mclass)
+ var mainprop = mainmodule.try_get_primitive_method("run", maintype.mclass) or else
+ mainmodule.try_get_primitive_method("main", maintype.mclass)
if mainprop != null then
add_send(maintype, mainprop)
end
+ var finalizable_type = mainmodule.finalizable_type
+ if finalizable_type != null then
+ var finalize_meth = mainmodule.try_get_primitive_method("finalize", finalizable_type.mclass)
+ if finalize_meth != null then add_send(finalizable_type, finalize_meth)
+ end
+
# Force primitive types
force_alive("Bool")
- force_alive("Int")
force_alive("Float")
force_alive("Char")
+ force_alive("Pointer")
+ force_alive("Byte")
+ force_alive("Int")
+ force_alive("Int8")
+ force_alive("Int16")
+ force_alive("UInt16")
+ force_alive("Int32")
+ force_alive("UInt32")
while not todo.is_empty do
var mmethoddef = todo.shift
+ var mmeth = mmethoddef.mproperty
+ var msignature = mmethoddef.msignature
+ if msignature == null then continue # Skip broken method
+
#print "# visit {mmethoddef}"
var v = new RapidTypeVisitor(self, mmethoddef.mclassdef.bound_mtype, mmethoddef)
- var vararg_rank = mmethoddef.msignature.vararg_rank
+ var vararg_rank = msignature.vararg_rank
if vararg_rank > -1 then
- var node = self.modelbuilder.mpropdef2npropdef[mmethoddef]
- var elttype = mmethoddef.msignature.mparameters[vararg_rank].mtype
+ var node = self.modelbuilder.mpropdef2node(mmethoddef)
+ var elttype = msignature.mparameters[vararg_rank].mtype
#elttype = elttype.anchor_to(self.mainmodule, v.receiver)
- var vararg = self.mainmodule.get_primitive_class("Array").get_mtype([elttype])
+ var vararg = self.mainmodule.array_type(elttype)
v.add_type(vararg)
- var native = self.mainmodule.get_primitive_class("NativeArray").get_mtype([elttype])
+ var native = self.mainmodule.native_array_type(elttype)
v.add_type(native)
v.add_monomorphic_send(vararg, self.modelbuilder.force_get_primitive_method(node, "with_native", vararg.mclass, self.mainmodule))
end
-
- for i in [0..mmethoddef.msignature.arity[ do
- var origtype = mmethoddef.mproperty.intro.msignature.mparameters[i].mtype
+ # TODO? new_msignature
+ var sig = msignature
+ var osig = mmeth.intro.msignature.as(not null)
+ for i in [0..sig.arity[ do
+ var origtype = osig.mparameters[i].mtype
if not origtype.need_anchor then continue # skip non covariant stuff
- var paramtype = mmethoddef.msignature.mparameters[i].mtype
- #paramtype = v.cleanup_type(paramtype).as(not null)
+ var paramtype = sig.mparameters[i].mtype
add_cast(paramtype)
end
- if not modelbuilder.mpropdef2npropdef.has_key(mmethoddef) then
- # It is an init for a class?
- if mmethoddef.mproperty.name == "init" then
- var nclassdef = self.modelbuilder.mclassdef2nclassdef[mmethoddef.mclassdef]
- var super_inits = nclassdef.super_inits
- if super_inits != null then
- #assert args.length == 1
- for su in super_inits do
- v.add_monomorphic_send(v.receiver, su)
- end
- end
+ var npropdef = modelbuilder.mpropdef2node(mmethoddef)
+
+ if npropdef isa AClassdef then
+ # It is an init for a class
+ assert mmethoddef == npropdef.mfree_init
- else
- abort
+ if mmethoddef.mproperty.is_root_init and not mmethoddef.is_intro then
+ self.add_super_send(v.receiver, mmethoddef)
end
continue
+ else if mmethoddef.constant_value != null then
+ # Make the return type live
+ v.add_type(msignature.return_mtype.as(MClassType))
+ continue
+ else if npropdef == null then
+ abort
end
- var npropdef = modelbuilder.mpropdef2npropdef[mmethoddef]
-
- if npropdef isa AConcreteMethPropdef then
+ if npropdef isa AMethPropdef then
var auto_super_inits = npropdef.auto_super_inits
if auto_super_inits != null then
for auto_super_init in auto_super_inits do
v.add_callsite(auto_super_init)
end
end
- else if npropdef isa AInternMethPropdef or
- (npropdef isa AExternMethPropdef and npropdef.n_extern != null) then
+ if npropdef.auto_super_call then
+ self.add_super_send(v.receiver, mmethoddef)
+ end
+ end
+
+ if mmethoddef.is_intern or mmethoddef.is_extern then
# UGLY: We force the "instantation" of the concrete return type if any
- var ret = mmethoddef.msignature.return_mtype
+ var ret = msignature.return_mtype
if ret != null and ret isa MClassType and ret.mclass.kind != abstract_kind and ret.mclass.kind != interface_kind then
v.add_type(ret)
end
- else if npropdef isa AExternMethPropdef then
- var nclassdef = npropdef.parent.as(AClassdef)
- v.enter_visit(npropdef)
- else if npropdef isa AExternInitPropdef then
- v.add_type(v.receiver)
- else
-
end
v.enter_visit(npropdef)
if not ot.can_resolve_for(t, t, mainmodule) then continue
var rt = ot.anchor_to(mainmodule, t)
if live_types.has(rt) then continue
+ if not check_depth(rt) then continue
#print "{ot}/{t} -> {rt}"
live_types.add(rt)
todo_types.add(rt)
- check_depth(rt)
end
end
#print "MType {live_types.length}: {live_types.join(", ")}"
#print "cast MType {live_cast_types.length}: {live_cast_types.join(", ")}"
end
- private fun check_depth(mtype: MClassType)
+ private fun check_depth(mtype: MClassType): Bool
do
var d = mtype.length
if d > 255 then
- 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}.")
+ 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}`.")
+ return false
end
+ return true
end
fun add_new(recv: MClassType, mtype: MClassType)
for p in totry_methods do try_send(mtype, p)
for p in live_super_sends do try_super_send(mtype, p)
+ # Remove cleared ones
+ for p in totry_methods_to_remove do totry_methods.remove(p)
+ totry_methods_to_remove.clear
+
var bound_mtype = mtype.anchor_to(mainmodule, recv)
for cd in bound_mtype.collect_mclassdefs(mainmodule)
do
- if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
- var nclassdef = self.modelbuilder.mclassdef2nclassdef[cd]
- for npropdef in nclassdef.n_propdefs do
- if not npropdef isa AAttrPropdef then continue
- var nexpr = npropdef.n_expr
- if nexpr == null then continue
- var mpropdef = npropdef.mpropdef.as(not null)
+ for npropdef in modelbuilder.collect_attr_propdef(cd) do
+ if not npropdef.has_value then continue
+
+ var mpropdef = npropdef.mreadpropdef.as(not null)
var v = new RapidTypeVisitor(self, bound_mtype, mpropdef)
- v.enter_visit(nexpr)
+ v.enter_visit(npropdef.n_expr)
+ v.enter_visit(npropdef.n_block)
end
end
do
if live_methoddefs.has(mpropdef) then return
live_methoddefs.add(mpropdef)
+ live_mmodules.add(mpropdef.mclassdef.mmodule)
todo.add(mpropdef)
var mproperty = mpropdef.mproperty
if mproperty.mpropdefs.length <= 1 then return
# If all definitions of a method are live, we can remove the definition of the totry set
for d in mproperty.mpropdefs do
- if d.is_abstract then continue
if not live_methoddefs.has(d) then return
end
#print "full property: {mpropdef.mproperty} for {mpropdef.mproperty.mpropdefs.length} definitions"
- totry_methods.remove(mpropdef.mproperty)
+ totry_methods_to_remove.add(mpropdef.mproperty)
end
fun add_send(recv: MType, mproperty: MMethod)
do
- if live_methods.has(mproperty) then return
+ if try_methods.has(mproperty) then return
#print "new prop: {mproperty}"
live_methods.add(mproperty)
+ try_methods.add(mproperty)
if mproperty.mpropdefs.length == 1 then
# If there is only one definition, just add the definition and do not try again the property
var d = mproperty.mpropdefs.first
fun add_super_send(recv: MType, mpropdef: MMethodDef)
do
+ assert mpropdef.has_supercall
if live_super_sends.has(mpropdef) then return
#print "new super prop: {mpropdef}"
live_super_sends.add(mpropdef)
- for t in live_types do
- try_super_send(t, mpropdef)
+ for c in live_classes do
+ try_super_send(c.intro.bound_mtype, mpropdef)
end
end
end
var receiver: MClassType
var mpropdef: MPropDef
- init(analysis: RapidTypeAnalysis, receiver: MClassType, mpropdef: MPropDef)
+ init
do
- self.analysis = analysis
- self.receiver = receiver
- self.mpropdef = mpropdef
assert not receiver.need_anchor
end
redef fun visit(n)
do
- n.accept_rapid_type_visitor(self)
if n isa AExpr then
- var implicit_cast_to = n.implicit_cast_to
- if implicit_cast_to != null then self.add_cast_type(implicit_cast_to)
+ if n.mtype != null or n.is_typed then
+ n.accept_rapid_type_visitor(self)
+ var implicit_cast_to = n.implicit_cast_to
+ if implicit_cast_to != null then self.add_cast_type(implicit_cast_to)
+ end
+ else
+ n.accept_rapid_type_visitor(self)
end
# RTA does not enter in AAnnotations
do
mtype = mtype.anchor_to(self.analysis.mainmodule, self.receiver)
if mtype isa MNullType then return null
- if mtype isa MNullableType then mtype = mtype.mtype
+ mtype = mtype.undecorate
assert mtype isa MClassType
assert not mtype.need_anchor
return mtype
end
- fun get_class(name: String): MClass
- do
- return analysis.mainmodule.get_primitive_class(name)
- end
-
fun get_method(recv: MType, name: String): MMethod
do
var mtype = cleanup_type(recv)
fun add_type(mtype: MClassType) do analysis.add_new(receiver, mtype)
- fun add_monomorphic_send(mtype: MType, mproperty: MMethod) do analysis.try_send(mtype.as(MClassType), mproperty)
+ fun add_monomorphic_send(mtype: MType, mproperty: MMethod)
+ do
+ analysis.live_methods.add(mproperty)
+ analysis.try_send(mtype.as(MClassType), mproperty)
+ end
fun add_send(mtype: MType, mproperty: MMethod) do analysis.add_send(mtype, mproperty)
fun add_cast_type(mtype: MType) do analysis.add_cast(mtype)
- fun add_callsite(callsite: nullable CallSite) do if callsite != null then analysis.add_send(callsite.recv, callsite.mproperty)
+ fun add_callsite(callsite: nullable CallSite) do if callsite != null then
+ for m in callsite.mpropdef.initializers do
+ if m isa MMethod then
+ analysis.add_send(callsite.recv, m)
+ end
+ end
+ analysis.add_send(callsite.recv, callsite.mproperty)
+ analysis.live_callsites.add(callsite)
+ end
end
###
end
end
-redef class AIntExpr
- redef fun accept_rapid_type_visitor(v)
+redef class AExpr
+ # Make the `mtype` of the expression live
+ # Used by literals and instantiations
+ fun allocate_mtype(v: RapidTypeVisitor)
do
+ var mtype = self.mtype
+ if not mtype isa MClassType then return
v.add_type(self.mtype.as(MClassType))
end
end
+redef class AIntegerExpr
+ redef fun accept_rapid_type_visitor(v)
+ do
+ allocate_mtype(v)
+ end
+end
+
redef class AFloatExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class ACharExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
do
var mtype = self.mtype.as(MClassType)
v.add_type(mtype)
- var native = v.analysis.mainmodule.get_primitive_class("NativeArray").get_mtype([mtype.arguments.first])
+ var native = v.analysis.mainmodule.native_array_type(mtype.arguments.first)
v.add_type(native)
mtype = v.cleanup_type(mtype).as(not null)
var prop = v.get_method(mtype, "with_native")
v.add_monomorphic_send(mtype, prop)
+ v.add_callsite(with_capacity_callsite)
+ v.add_callsite(push_callsite)
end
end
redef class AStringFormExpr
redef fun accept_rapid_type_visitor(v)
do
- var native = v.get_class("NativeString").mclass_type
+ var native = v.analysis.mainmodule.native_string_type
v.add_type(native)
- var prop = v.get_method(native, "to_s_with_length")
+ var prop = v.get_method(native, "to_s_full")
v.add_monomorphic_send(native, prop)
end
end
redef class ASuperstringExpr
redef fun accept_rapid_type_visitor(v)
do
- var arraytype = v.get_class("Array").get_mtype([v.get_class("Object").mclass_type])
+ var mmodule = v.analysis.mainmodule
+ var object_type = mmodule.string_type
+ var arraytype = mmodule.array_type(object_type)
v.add_type(arraytype)
- v.add_type(v.get_class("NativeArray").get_mtype([v.get_class("Object").mclass_type]))
+ var nattype = mmodule.native_array_type(object_type)
+ v.add_type(nattype)
var prop = v.get_method(arraytype, "join")
v.add_monomorphic_send(arraytype, prop)
var prop2 = v.get_method(arraytype, "with_native")
v.add_monomorphic_send(arraytype, prop2)
+ v.add_monomorphic_send(nattype, v.get_method(nattype, "native_to_s"))
end
end
redef class ACrangeExpr
redef fun accept_rapid_type_visitor(v)
do
- var mtype = self.mtype.as(MClassType)
+ var mtype = self.mtype
+ if not mtype isa MClassType then return
v.add_type(mtype)
- var prop = v.get_method(mtype, "init")
- v.add_monomorphic_send(mtype, prop)
+ v.add_callsite(init_callsite)
end
end
redef class AOrangeExpr
redef fun accept_rapid_type_visitor(v)
do
- var mtype = self.mtype.as(MClassType)
+ var mtype = self.mtype
+ if not mtype isa MClassType then return
v.add_type(mtype)
- var prop = v.get_method(mtype, "without_last")
- v.add_monomorphic_send(mtype, prop)
+ v.add_callsite(init_callsite)
end
end
redef class ATrueExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class AFalseExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_type(self.mtype.as(MClassType))
+ allocate_mtype(v)
end
end
redef class AIsaExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_cast_type(self.cast_type.as(not null))
+ var cast_type = self.cast_type
+ if cast_type == null then return
+ v.add_cast_type(cast_type)
end
end
redef class AAsCastExpr
redef fun accept_rapid_type_visitor(v)
do
- v.add_cast_type(self.mtype.as(not null))
+ var mtype = self.mtype
+ if mtype == null then return
+ v.add_cast_type(mtype)
end
end
return
end
- v.analysis.add_super_send(v.receiver, v.mpropdef.as(MMethodDef))
+ v.analysis.add_super_send(v.receiver, mpropdef.as(not null))
end
end
-redef class AForExpr
+redef class AForGroup
redef fun accept_rapid_type_visitor(v)
do
- var recvtype = self.n_expr.mtype.as(not null)
- var colltype = self.coltype.as(not null)
- var itmeth = v.get_method(colltype, "iterator")
- v.add_send(recvtype, itmeth)
- var iteratortype = itmeth.intro.msignature.return_mtype.as(MClassType).mclass.intro.bound_mtype
- var objtype = v.get_class("Object").mclass_type
- v.add_send(objtype, v.get_method(iteratortype, "is_ok"))
+ v.add_callsite(self.method_iterator)
+ v.add_callsite(self.method_is_ok)
if self.variables.length == 1 then
- v.add_send(objtype, v.get_method(iteratortype, "item"))
+ v.add_callsite(self.method_item)
else if self.variables.length == 2 then
- v.add_send(objtype, v.get_method(iteratortype, "key"))
- v.add_send(objtype, v.get_method(iteratortype, "item"))
+ v.add_callsite(self.method_key)
+ v.add_callsite(self.method_item)
else
abort
end
- v.add_send(objtype, v.get_method(iteratortype, "next"))
+ v.add_callsite(self.method_next)
+ var mf = self.method_finish
+ if mf != null then v.add_callsite(mf)
end
end
redef class ANewExpr
redef fun accept_rapid_type_visitor(v)
do
- var mtype = self.mtype.as(MClassType)
+ var mtype = self.recvtype.as(not null)
v.add_type(mtype)
v.add_callsite(callsite)
end