DFG: placed isGlobalInitializer to FunctionSymbol

This commit is contained in:
Igor Chevdar
2018-01-30 13:54:16 +03:00
parent e3b6152a1e
commit 0629513fd9
4 changed files with 57 additions and 42 deletions
@@ -96,8 +96,8 @@ internal class CallGraphBuilder(val context: Context,
val rootSet = if (hasMain) {
listOf(symbolTable.mapFunction(findMainEntryPoint(context)!!).resolved()) +
moduleDFG.functions
.filter { it.value.isGlobalInitializer }
.map { it.key }
.filter { it.isGlobalInitializer }
} else {
moduleDFG.functions.keys.filterIsInstance<DataFlowIR.FunctionSymbol.Public>()
}
@@ -454,7 +454,6 @@ internal class ModuleDFGBuilder(val context: Context, val irModule: IrModuleFrag
return DataFlowIR.Function(
symbol = symbolTable.mapFunction(descriptor),
isGlobalInitializer = descriptor is PropertyDescriptor,
numberOfParameters = templateParameters.size + if (descriptor.isSuspend) 1 else 0,
body = DataFlowIR.FunctionBody(allNodes.distinct().toList(), returnsNode, throwsNode)
)
@@ -278,39 +278,45 @@ internal object DFGSerializer {
}
}
class ExternalFunctionSymbol(val hash: Long, val numberOfParameters: Int, val escapes: Int?, val pointsTo: IntArray?, val name: String?) {
class ExternalFunctionSymbol(val hash: Long, val numberOfParameters: Int, val isGlobalInitializer: Boolean,
val escapes: Int?, val pointsTo: IntArray?, val name: String?) {
constructor(data: ArraySlice) : this(data.readLong(), data.readInt(), data.readNullableInt(),
constructor(data: ArraySlice) : this(data.readLong(), data.readInt(), data.readBoolean(), data.readNullableInt(),
data.readNullable { readIntArray() }, data.readNullableString())
fun write(result: ArraySlice) {
result.writeLong(hash)
result.writeInt(numberOfParameters)
result.writeBoolean(isGlobalInitializer)
result.writeNullableInt(escapes)
result.writeNullable(pointsTo) { writeIntArray(it) }
result.writeNullableString(name)
}
}
class PublicFunctionSymbol(val hash: Long, val numberOfParameters: Int, val index: Int, val name: String?) {
class PublicFunctionSymbol(val hash: Long, val numberOfParameters: Int, val isGlobalInitializer: Boolean,
val index: Int, val name: String?) {
constructor(data: ArraySlice) : this(data.readLong(), data.readInt(), data.readInt(), data.readNullableString())
constructor(data: ArraySlice) : this(data.readLong(), data.readInt(), data.readBoolean(), data.readInt(),
data.readNullableString())
fun write(result: ArraySlice) {
result.writeLong(hash)
result.writeInt(numberOfParameters)
result.writeBoolean(isGlobalInitializer)
result.writeInt(index)
result.writeNullableString(name)
}
}
class PrivateFunctionSymbol(val index: Int, val numberOfParameters: Int, val name: String?) {
class PrivateFunctionSymbol(val index: Int, val numberOfParameters: Int, val isGlobalInitializer: Boolean, val name: String?) {
constructor(data: ArraySlice) : this(data.readInt(), data.readInt(), data.readNullableString())
constructor(data: ArraySlice) : this(data.readInt(), data.readInt(), data.readBoolean(), data.readNullableString())
fun write(result: ArraySlice) {
result.writeInt(index)
result.writeInt(numberOfParameters)
result.writeBoolean(isGlobalInitializer)
result.writeNullableString(name)
}
}
@@ -332,14 +338,14 @@ internal object DFGSerializer {
}
companion object {
fun external(hash: Long, numberOfParameters: Int, escapes: Int?, pointsTo: IntArray?, name: String?) =
FunctionSymbol(ExternalFunctionSymbol(hash, numberOfParameters, escapes, pointsTo, name), null, null)
fun external(hash: Long, numberOfParameters: Int, isGlobalInitializer: Boolean, escapes: Int?, pointsTo: IntArray?, name: String?) =
FunctionSymbol(ExternalFunctionSymbol(hash, numberOfParameters, isGlobalInitializer, escapes, pointsTo, name), null, null)
fun public(hash: Long, numberOfParameters: Int, index: Int, name: String?) =
FunctionSymbol(null, PublicFunctionSymbol(hash, numberOfParameters, index, name), null)
fun public(hash: Long, numberOfParameters: Int, isGlobalInitializer: Boolean, index: Int, name: String?) =
FunctionSymbol(null, PublicFunctionSymbol(hash, numberOfParameters, isGlobalInitializer, index, name), null)
fun private(index: Int, numberOfParameters: Int, name: String?) =
FunctionSymbol(null, null, PrivateFunctionSymbol(index, numberOfParameters, name))
fun private(index: Int, numberOfParameters: Int, isGlobalInitializer: Boolean, name: String?) =
FunctionSymbol(null, null, PrivateFunctionSymbol(index, numberOfParameters, isGlobalInitializer, name))
fun read(data: ArraySlice): FunctionSymbol {
val tag = data.readByte().toInt()
@@ -657,14 +663,12 @@ internal object DFGSerializer {
}
}
class Function(val symbol: Int, val isGlobalInitializer: Boolean,
val numberOfParameters: Int, val body: FunctionBody) {
class Function(val symbol: Int, val numberOfParameters: Int, val body: FunctionBody) {
constructor(data: ArraySlice) : this(data.readInt(), data.readBoolean(), data.readInt(), FunctionBody(data))
constructor(data: ArraySlice) : this(data.readInt(), data.readInt(), FunctionBody(data))
fun write(result: ArraySlice) {
result.writeInt(symbol)
result.writeBoolean(isGlobalInitializer)
result.writeInt(numberOfParameters)
body.write(result)
}
@@ -728,16 +732,20 @@ internal object DFGSerializer {
.map {
val functionSymbol = it.key
val numberOfParameters = functionSymbol.numberOfParameters
val isGlobalInitializer = functionSymbol.isGlobalInitializer
val name = functionSymbol.name
when (functionSymbol) {
is DataFlowIR.FunctionSymbol.External ->
FunctionSymbol.external(functionSymbol.hash, numberOfParameters, functionSymbol.escapes, functionSymbol.pointsTo, name)
FunctionSymbol.external(functionSymbol.hash, numberOfParameters, isGlobalInitializer,
functionSymbol.escapes, functionSymbol.pointsTo, name)
is DataFlowIR.FunctionSymbol.Public ->
FunctionSymbol.public(functionSymbol.hash, numberOfParameters, functionSymbol.symbolTableIndex, name)
FunctionSymbol.public(functionSymbol.hash, numberOfParameters,
isGlobalInitializer, functionSymbol.symbolTableIndex, name)
is DataFlowIR.FunctionSymbol.Private ->
FunctionSymbol.private(functionSymbol.symbolTableIndex, numberOfParameters, name)
FunctionSymbol.private(functionSymbol.symbolTableIndex, numberOfParameters,
isGlobalInitializer, name)
else -> error("Unknown function symbol $functionSymbol")
}
@@ -808,7 +816,6 @@ internal object DFGSerializer {
.toTypedArray()
Function(
functionSymbolMap[function.symbol]!!,
function.isGlobalInitializer,
function.numberOfParameters,
FunctionBody(nodes, nodeMap[body.returns]!!, nodeMap[body.throws]!!)
)
@@ -879,13 +886,15 @@ internal object DFGSerializer {
val private = it.private
when {
external != null ->
DataFlowIR.FunctionSymbol.External(external.hash, external.numberOfParameters, external.escapes, external.pointsTo, external.name)
DataFlowIR.FunctionSymbol.External(external.hash, external.numberOfParameters,
external.isGlobalInitializer, external.escapes, external.pointsTo, external.name)
public != null -> {
val symbolTableIndex = public.index
if (symbolTableIndex >= 0)
++module.numberOfFunctions
DataFlowIR.FunctionSymbol.Public(public.hash, public.numberOfParameters, module, symbolTableIndex, public.name).also {
DataFlowIR.FunctionSymbol.Public(public.hash, public.numberOfParameters, module,
symbolTableIndex, public.isGlobalInitializer, public.name).also {
publicFunctionsMap.put(it.hash, it)
}
}
@@ -894,7 +903,8 @@ internal object DFGSerializer {
val symbolTableIndex = private!!.index
if (symbolTableIndex >= 0)
++module.numberOfFunctions
DataFlowIR.FunctionSymbol.Private(privateFunIndex++, private.numberOfParameters, module, symbolTableIndex, private.name)
DataFlowIR.FunctionSymbol.Private(privateFunIndex++, private.numberOfParameters, module,
symbolTableIndex, private.isGlobalInitializer, private.name)
}
}
}
@@ -1090,7 +1100,7 @@ internal object DFGSerializer {
moduleDataFlowGraph.functions.forEach {
val symbol = functionSymbols[it.symbol]
functions.put(symbol, DataFlowIR.Function(symbol, it.isGlobalInitializer, it.numberOfParameters, deserializeBody(it.body)))
functions.put(symbol, DataFlowIR.Function(symbol, it.numberOfParameters, deserializeBody(it.body)))
}
}
}
@@ -114,9 +114,11 @@ internal object DataFlowIR {
var numberOfFunctions = 0
}
abstract class FunctionSymbol(val numberOfParameters: Int, val name: String?) {
class External(val hash: Long, numberOfParameters: Int, val escapes: Int?, val pointsTo: IntArray?, name: String? = null)
: FunctionSymbol(numberOfParameters, name) {
abstract class FunctionSymbol(val numberOfParameters: Int, val isGlobalInitializer: Boolean, val name: String?) {
class External(val hash: Long, numberOfParameters: Int, isGlobalInitializer: Boolean,
val escapes: Int?, val pointsTo: IntArray?, name: String? = null)
: FunctionSymbol(numberOfParameters, isGlobalInitializer, name) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is External) return false
@@ -133,11 +135,13 @@ internal object DataFlowIR {
}
}
abstract class Declared(numberOfParameters: Int, val module: Module, val symbolTableIndex: Int, name: String?)
: FunctionSymbol(numberOfParameters, name)
abstract class Declared(numberOfParameters: Int, val module: Module, val symbolTableIndex: Int,
isGlobalInitializer: Boolean, name: String?)
: FunctionSymbol(numberOfParameters, isGlobalInitializer, name)
class Public(val hash: Long, numberOfParameters: Int, module: Module, symbolTableIndex: Int, name: String? = null)
: Declared(numberOfParameters, module, symbolTableIndex, name) {
class Public(val hash: Long, numberOfParameters: Int, module: Module, symbolTableIndex: Int,
isGlobalInitializer: Boolean, name: String? = null)
: Declared(numberOfParameters, module, symbolTableIndex, isGlobalInitializer, name) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Public) return false
@@ -154,8 +158,9 @@ internal object DataFlowIR {
}
}
class Private(val index: Int, numberOfParameters: Int, module: Module, symbolTableIndex: Int, name: String? = null)
: Declared(numberOfParameters, module, symbolTableIndex, name) {
class Private(val index: Int, numberOfParameters: Int, module: Module, symbolTableIndex: Int,
isGlobalInitializer: Boolean, name: String? = null)
: Declared(numberOfParameters, module, symbolTableIndex, isGlobalInitializer, name) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Private) return false
@@ -229,11 +234,9 @@ internal object DataFlowIR {
class FunctionBody(val nodes: List<Node>, val returns: Node.Variable, val throws: Node.Variable)
class Function(val symbol: FunctionSymbol,
val isGlobalInitializer: Boolean,
val numberOfParameters: Int,
val body: FunctionBody) {
fun debugOutput() {
println("FUNCTION $symbol")
println("Params: $numberOfParameters")
@@ -511,8 +514,11 @@ internal object DataFlowIR {
fun mapFunction(descriptor: CallableDescriptor) = descriptor.original.let {
functionMap.getOrPut(it) {
when (it) {
is PropertyDescriptor ->
FunctionSymbol.Private(privateFunIndex++, 0, module, -1, takeName { "${it.symbolName}_init" })
is PropertyDescriptor -> {
// A global property initializer.
assert (it.dispatchReceiverParameter == null) { "All local properties initializers should've been lowered" }
FunctionSymbol.Private(privateFunIndex++, 0, module, -1, true, takeName { "${it.symbolName}_init" })
}
is FunctionDescriptor -> {
val name = if (it.isExported()) it.symbolName else it.internalName
@@ -524,7 +530,7 @@ internal object DataFlowIR {
val escapesBitMask = (escapesAnnotation?.allValueArguments?.get(escapesWhoDescriptor.name) as? ConstantValue<Int>)?.value
@Suppress("UNCHECKED_CAST")
val pointsToBitMask = (pointsToAnnotation?.allValueArguments?.get(pointsToOnWhomDescriptor.name) as? ConstantValue<List<IntValue>>)?.value
FunctionSymbol.External(name.localHash.value, numberOfParameters, escapesBitMask,
FunctionSymbol.External(name.localHash.value, numberOfParameters, false, escapesBitMask,
pointsToBitMask?.let { it.map { it.value }.toIntArray() }, takeName { name })
} else {
val isAbstract = it.modality == Modality.ABSTRACT
@@ -536,9 +542,9 @@ internal object DataFlowIR {
++module.numberOfFunctions
val symbolTableIndex = if (!placeToFunctionsTable) -1 else couldBeCalledVirtuallyIndex++
if (it.isExported())
FunctionSymbol.Public(name.localHash.value, numberOfParameters, module, symbolTableIndex, takeName { name })
FunctionSymbol.Public(name.localHash.value, numberOfParameters, module, symbolTableIndex, false, takeName { name })
else
FunctionSymbol.Private(privateFunIndex++, numberOfParameters, module, symbolTableIndex, takeName { name })
FunctionSymbol.Private(privateFunIndex++, numberOfParameters, module, symbolTableIndex, false, takeName { name })
}
}