New source mapping

This commit is contained in:
Michael Bogdanov
2016-04-18 17:56:08 +03:00
parent cb615c557a
commit ff820bddc0
18 changed files with 575 additions and 18 deletions
@@ -24,6 +24,8 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.backend.common.CodegenUtil;
import org.jetbrains.kotlin.codegen.context.*;
import org.jetbrains.kotlin.codegen.inline.*;
import org.jetbrains.kotlin.codegen.inline2.*;
import org.jetbrains.kotlin.codegen.inline2.DefaultSourceMapper;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
import org.jetbrains.kotlin.descriptors.*;
@@ -557,7 +559,7 @@ public abstract class MemberCodegen<T extends KtElement/* TODO: & JetDeclaration
@NotNull
public SourceMapper getOrCreateSourceMapper() {
if (sourceMapper == null) {
sourceMapper = new DefaultSourceMapper(SourceInfo.Companion.createInfo(element, getClassName()), null);
sourceMapper = new DefaultSourceMapper(SourceInfo.Companion.createInfo(element, getClassName()));
}
return sourceMapper;
}
@@ -27,6 +27,8 @@ import org.jetbrains.kotlin.backend.common.CodegenUtil;
import org.jetbrains.kotlin.builtins.BuiltInsPackageFragment;
import org.jetbrains.kotlin.codegen.*;
import org.jetbrains.kotlin.codegen.context.*;
import org.jetbrains.kotlin.codegen.inline2.*;
import org.jetbrains.kotlin.codegen.inline2.NestedSourceMapper;
import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicArrayConstructorsKt;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
@@ -22,6 +22,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.codegen.ClosureCodegen;
import org.jetbrains.kotlin.codegen.StackValue;
import org.jetbrains.kotlin.codegen.inline2.*;
import org.jetbrains.kotlin.codegen.inline2.InlineLambdaSourceMapper;
import org.jetbrains.kotlin.codegen.inline2.NestedSourceMapper;
import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods;
import org.jetbrains.kotlin.codegen.optimization.MandatoryMethodTransformer;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen.inline
import gnu.trove.TIntIntHashMap
import org.jetbrains.kotlin.codegen.ClassBuilder
import org.jetbrains.kotlin.codegen.SourceInfo
import org.jetbrains.kotlin.codegen.inline2.LineNumberToMap
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.MethodVisitor
import java.util.*
@@ -121,6 +122,7 @@ open class InlineLambdaSourceMapper(
interface SourceMapper {
val resultMappings: List<FileMapping>
val parent: SourceMapper?
get() = null
open fun visitSource(name: String, path: String) {
throw UnsupportedOperationException("fail")
@@ -134,6 +136,10 @@ interface SourceMapper {
throw UnsupportedOperationException("fail")
}
open fun visitLineNumber(iv: MethodVisitor, destLineNumber: Int, start: Label, source: LineNumberToMap): Int {
throw UnsupportedOperationException("fail")
}
open fun endMapping() {
parent?.visitOrigin()
}
@@ -145,8 +151,8 @@ interface SourceMapper {
}
}
fun createFromSmap(smap: SMAP): DefaultSourceMapper {
val sourceMapper = DefaultSourceMapper(smap.sourceInfo, null)
fun createFromSmap(smap: SMAP): SourceMapper {
val sourceMapper = org.jetbrains.kotlin.codegen.inline2.DefaultSourceMapper(smap.sourceInfo)
smap.fileMappings.asSequence()
//default one mapped through sourceInfo
.filterNot { it == smap.default }
@@ -239,8 +245,6 @@ class SMAP(val fileMappings: List<FileMapping>) {
val sourceInfo: SourceInfo
init {
//val defaultMapping = default.lineMappings.single()
//temporary workaround for KT-11478 ("Couldn't inline method call" error)
val defaultMapping = default.lineMappings.first()
sourceInfo = SourceInfo(default.name, default.path, defaultMapping.source + defaultMapping.range - 1)
}
@@ -0,0 +1,213 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen.inline2
import gnu.trove.TIntIntHashMap
import org.jetbrains.kotlin.codegen.SourceInfo
import org.jetbrains.kotlin.codegen.inline.FileMapping
import org.jetbrains.kotlin.codegen.inline.RangeMapping
import org.jetbrains.kotlin.codegen.inline.SMAPAndMethodNode
import org.jetbrains.kotlin.codegen.inline.SourceMapper
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.MethodVisitor
import java.util.*
open class LineNumberToMap(val lineNumberToMap: Int, val source: Int, val name: String, val path: String)
open class NestedSourceMapper(
override val parent: SourceMapper, val ranges: List<RangeMapping>, sourceInfo: SourceInfo
) : DefaultSourceMapper(sourceInfo) {
val visited = TIntIntHashMap()
override fun visitLineNumber(iv: MethodVisitor, lineNumber: Int, start: Label) {
val mappedLineNumber = visited.get(lineNumber)
if (mappedLineNumber > 0) {
iv.visitLineNumber(mappedLineNumber, start)
} else {
val findMappingIfExists = findMappingIfExists(lineNumber)!!
val sourceLineNumber = findMappingIfExists.map(lineNumber)
val visitLineNumber = parent.visitLineNumber(iv, lineNumber, start, LineNumberToMap(lineNumber, sourceLineNumber, findMappingIfExists.parent!!.name, findMappingIfExists.parent!!.path))
if (visitLineNumber > 0) {
visited.put(lineNumber, visitLineNumber)
}
}
}
fun findMappingIfExists(lineNumber: Int): RangeMapping? {
val index = ranges.binarySearch(RangeMapping(lineNumber, lineNumber, 1), Comparator {
value, key ->
if (key.dest in value) 0 else RangeMapping.Comparator.compare(value, key)
})
return if (index < 0) null else ranges[index];
}
}
open class InlineLambdaSourceMapper(
parent: SourceMapper, smap: SMAPAndMethodNode
) : NestedSourceMapper(parent, smap.ranges, smap.classSMAP.sourceInfo) {
init {
assert(smap.ranges.isNotEmpty()) {
"Mapping ragnes should be present in inlined lambda: ${smap.node}"
}
}
override fun visitLineNumber(iv: MethodVisitor, lineNumber: Int, start: Label) {
val mapping = findMappingIfExists(lineNumber)!!
if (mapping == ranges.firstOrNull()) { //TODO rewrite
//don't remap origin lambda line numbers
iv.visitLineNumber(lineNumber, start)
}
else {
super.visitLineNumber(iv, lineNumber, start)
}
}
}
open class DefaultSourceMapper(val sourceInfo: SourceInfo): SourceMapper {
protected var maxUsedValue: Int = sourceInfo.linesInFile
var lastVisited: RawFileMapping? = null
private var lastMappedWithChanges: RawFileMapping? = null
private var fileMappings: LinkedHashMap<String, RawFileMapping> = linkedMapOf()
protected val origin: RawFileMapping
init {
val name = sourceInfo.source
val path = sourceInfo.pathOrCleanFQN
origin = RawFileMapping(name, path)
origin.initRange(1, maxUsedValue)
fileMappings.put(createKey(name, path), origin)
lastVisited = origin
}
private fun createKey(name: String, path: String) = "$name#$path"
override val resultMappings: List<FileMapping>
get() = fileMappings.values.map { it.toFileMapping() }
override fun visitSource(name: String, path: String) {
lastVisited = fileMappings.getOrPut(createKey(name, path)) { RawFileMapping(name, path) }
}
override fun visitOrigin() {
lastVisited = origin
}
override fun visitLineNumber(iv: MethodVisitor, lineNumber: Int, start: Label) {
if (lineNumber < 0) {
//no source information, so just skip this linenumber
return
}
//TODO add assertion that mapping exists
//val sourceLineNumber = createMapping(lineNumberToMap)
val sourceLineNumber = lineNumber
assert(lineNumber == sourceLineNumber)
iv.visitLineNumber(lineNumber, start)
}
override fun visitLineNumber(iv: MethodVisitor, destLineNumber: Int, start: Label, source: LineNumberToMap): Int {
if (source.source < 0) {
//no source information, so just skip this linenumber
return -1
}
visitSource(source.name, source.path)
val mappedLineIndex = createMapping(source.source)
iv.visitLineNumber(mappedLineIndex, start)
return mappedLineIndex
}
protected fun createMapping(lineNumber: Int): Int {
val fileMapping = lastVisited!!
//val mappedLineIndex = fileMapping.mapLine(lineNumber, maxUsedValue, lastMappedWithChanges == lastVisited)
val mappedLineIndex = fileMapping.mapNewLineNumber(lineNumber, maxUsedValue, lastMappedWithChanges == lastVisited)
if (mappedLineIndex > maxUsedValue) {
lastMappedWithChanges = fileMapping
maxUsedValue = mappedLineIndex
}
return mappedLineIndex
}
}
class RawFileMapping(val name: String, val path: String) {
private val lineMappings = TIntIntHashMap()
private val rangeMappings = arrayListOf<RangeMapping>()
private var lastMappedWithNewIndex = -1000
fun toFileMapping() =
FileMapping(name, path).apply {
for (range in rangeMappings) {
addRangeMapping(range)
}
}
fun initRange(start: Int, end: Int) {
assert(lineMappings.isEmpty) { "initRange should only be called for empty mapping" }
for (index in start..end) {
lineMappings.put(index, index)
}
rangeMappings.add(RangeMapping(start, start, end - start + 1))
lastMappedWithNewIndex = end
}
fun mapLine(source: Int, currentIndex: Int, isLastMapped: Boolean): Int {
var dest = lineMappings[source]
if (dest == 0) { // line numbers are 1-based, so 0 is ok to indicate missing value
dest = mapNewLineNumber(source, currentIndex, isLastMapped)
}
return dest
}
fun mapNewLineNumber(source: Int, currentIndex: Int, isLastMapped: Boolean): Int {
val dest: Int
val rangeMapping: RangeMapping
if (rangeMappings.isNotEmpty() && isLastMapped && couldFoldInRange(lastMappedWithNewIndex, source)) {
rangeMapping = rangeMappings.last()
rangeMapping.range += source - lastMappedWithNewIndex
dest = lineMappings[lastMappedWithNewIndex] + source - lastMappedWithNewIndex
}
else {
dest = currentIndex + 1
rangeMapping = RangeMapping(source, dest)
rangeMappings.add(rangeMapping)
}
lineMappings.put(source, dest)
lastMappedWithNewIndex = source
return dest
}
fun mapNewInterval(source: Int, dest: Int, range: Int) {
val rangeMapping = RangeMapping(source, dest, range)
rangeMappings.add(rangeMapping)
(source..(source + range - 1)).forEach {
lineMappings.put(source, dest)
}
}
private fun couldFoldInRange(first: Int, second: Int): Boolean {
//TODO
val delta = second - first
return delta > 0 && delta <= 10
}
}
@@ -35,7 +35,7 @@ private fun createLineNumberSequence(node: MethodNode, classSMAP: SMAP): Sequenc
if (key.dest in value) 0 else RangeMapping.Comparator.compare(value, key)
})
if (index < 0) {
error("Unmapped label in inlined function $node ${lineNumber.line}")
error("Unmapped line number in inlined function ${node.name}:${lineNumber.line}")
}
LabelAndMapping(lineNumber, classSMAP.intervals[index])
}