From 6971fa83d78502235dbb1aa0f9bea25e74ab9efa Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Mon, 13 Apr 2015 12:30:41 +0100 Subject: [PATCH] SourceDebugExtension support for #156 --- gradle.properties | 2 +- .../asm/launch/MixinBootstrap.java | 2 +- .../transformer/MixinApplicatorStandard.java | 7 +- .../mixin/transformer/MixinTargetContext.java | 19 ++ .../mixin/transformer/TargetClassContext.java | 15 + .../asm/mixin/transformer/meta/SourceMap.java | 276 ++++++++++++++++++ .../org/spongepowered/asm/util/ASMHelper.java | 21 ++ 7 files changed, 339 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/transformer/meta/SourceMap.java diff --git a/gradle.properties b/gradle.properties index 71821148..099b9855 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.6.2 +buildVersion=0.6.3 buildType=SNAPSHOT diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 96a747cd..2d2a2b5d 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -62,7 +62,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.6.2"; + public static final String VERSION = "0.6.3"; // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java index 7f0fc8e4..35f8bc0c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicatorStandard.java @@ -264,7 +264,8 @@ class MixinApplicatorStandard { throw new InvalidMixinException(current, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst applying the mixin class: " + ex.getMessage(), ex); } - + + this.applySourceMap(this.context); this.context.processDebugTasks(); } @@ -900,6 +901,10 @@ class MixinApplicatorStandard { } } + protected void applySourceMap(TargetClassContext context) { + this.targetClass.sourceDebug = context.getSourceMap().toString(); + } + /** * Check constraints in annotations on the specified mixin method * diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index 94d82066..8748a17e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -64,6 +64,7 @@ import org.spongepowered.asm.mixin.transformer.meta.MixinMerged; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError; import org.spongepowered.asm.obfuscation.RemapperChain; +import org.spongepowered.asm.mixin.transformer.meta.SourceMap.File; import org.spongepowered.asm.util.ASMHelper; import org.spongepowered.asm.util.Constants; @@ -147,6 +148,11 @@ public class MixinTargetContext implements IReferenceMapperContext { */ private final boolean detachedSuper; + /** + * SourceMap stratum + */ + private final File stratum; + /** * Minimum class version required to apply this mixin, target class will be * upgraded if the version is below this value @@ -166,6 +172,7 @@ public class MixinTargetContext implements IReferenceMapperContext { this.classNode = classNode; this.targetClass = context; this.targetClassInfo = ClassInfo.forName(this.targetClass.getName()); + this.stratum = context.getSourceMap().addFile(this.classNode, mixin.getName()); this.inheritsFromMixin = mixin.getClassInfo().hasMixinInHierarchy() || this.targetClassInfo.hasMixinTargetInHierarchy(); this.detachedSuper = !this.classNode.superName.equals(this.targetClass.getClassNode().superName); this.sessionId = context.getSessionId(); @@ -289,6 +296,15 @@ public class MixinTargetContext implements IReferenceMapperContext { return this.targetClassInfo; } + /** + * Get the SourceMap stratum for this mixin + * + * @return stratum + */ + public File getStratum() { + return this.stratum; + } + /** * Get the minimum required class version for this mixin */ @@ -356,6 +372,9 @@ public class MixinTargetContext implements IReferenceMapperContext { public void transformMethod(MethodNode method) { this.validateMethod(method); this.transformDescriptor(method); + + // Offset line numbers per the stratum + this.stratum.applyOffset(method); AbstractInsnNode lastInsn = null; for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java index f423b65c..17d9e9de 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java @@ -38,6 +38,7 @@ import org.spongepowered.asm.mixin.Debug; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.injection.struct.Target; +import org.spongepowered.asm.mixin.transformer.meta.SourceMap; import org.spongepowered.asm.util.ASMHelper; /** @@ -64,6 +65,11 @@ class TargetClassContext { * Target class metadata */ private final ClassInfo classInfo; + + /** + * Source map that is generated for target class + */ + private final SourceMap sourceMap; /** * Mixins to apply @@ -98,6 +104,8 @@ class TargetClassContext { this.classNode = classNode; this.classInfo = ClassInfo.fromClassNode(classNode); this.mixins = mixins; + this.sourceMap = new SourceMap(classNode.sourceFile); + this.sourceMap.addFile(this.classNode); } @Override @@ -169,6 +177,13 @@ class TargetClassContext { return this.mixins; } + /** + * Get the source map that is generated for the target class + */ + public SourceMap getSourceMap() { + return this.sourceMap; + } + MethodNode findAliasedMethod(Deque aliases, String desc) { String alias = aliases.poll(); if (alias == null) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/meta/SourceMap.java b/src/main/java/org/spongepowered/asm/mixin/transformer/meta/SourceMap.java new file mode 100644 index 00000000..1aad1511 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/meta/SourceMap.java @@ -0,0 +1,276 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin.transformer.meta; + +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.spongepowered.asm.lib.tree.AbstractInsnNode; +import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.lib.tree.LineNumberNode; +import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.util.ASMHelper; + +/** + * Structure which contains information about a SourceDebugExtension SMAP + */ +public class SourceMap { + + private static final String NEWLINE = "\n"; + + /** + * Defines a source code file within a source map stratum + */ + public static class File { + + /** + * File index in stratum + */ + public final int id; + + /** + * The base line offset for this stratum, line numbers in the output + * will be offset by this amount from their originals + */ + public final int lineOffset; + + /** + * The size of this stratum (number of lines) + */ + public final int size; + + /** + * Actual source file name to include in the smap + */ + public final String sourceFileName; + + /** + * Full path to the source file + */ + public final String sourceFilePath; + + /** + * Create a new SMAP Stratum + * + * @param lineOffset line offset + * @param size total lines + * @param sourceFileName source file name + */ + public File(int id, int lineOffset, int size, String sourceFileName) { + this(id, lineOffset, size, sourceFileName, null); + } + + /** + * Create a new SMAP Stratum + * + * @param lineOffset line offset + * @param size total lines + * @param sourceFileName source file name + * @param sourceFilePath source path + */ + public File(int id, int lineOffset, int size, String sourceFileName, String sourceFilePath) { + this.id = id; + this.lineOffset = lineOffset; + this.size = size; + this.sourceFileName = sourceFileName; + this.sourceFilePath = sourceFilePath; + } + + /** + * Offset the line numbers in the target class node by the base + * lineoffset for this stratum + * + * @param classNode class to operate upon + */ + public void applyOffset(ClassNode classNode) { + for (MethodNode method : classNode.methods) { + this.applyOffset(method); + } + } + + /** + * Offset the line numbers in the target method node by the base + * lineoffset for this stratum + * + * @param method method to operate upon + */ + public void applyOffset(MethodNode method) { + for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { + AbstractInsnNode node = iter.next(); + if (node instanceof LineNumberNode) { + ((LineNumberNode)node).line += this.lineOffset - 1; + } + } + } + + void appendFile(StringBuilder sb) { + if (this.sourceFilePath != null) { + sb.append("+ ").append(this.id).append(" ").append(this.sourceFileName).append(SourceMap.NEWLINE); + sb.append(this.sourceFilePath).append(SourceMap.NEWLINE); + } else { + sb.append(this.id).append(" ").append(this.sourceFileName).append(SourceMap.NEWLINE); + } + } + + public void appendLines(StringBuilder sb) { + sb.append("1#").append(this.id) // Map line number 1 (onwards) in file number + .append(",").append(this.size) // repeated times (eg. lines 1 to ) + .append(":").append(this.lineOffset) // to output line number lineOffset in the output file + .append(SourceMap.NEWLINE); + } + + } + + /** + * Defines a source code stratum within the source map + */ + static class Stratum { + + /** + * Stratum name + */ + public final String name; + + private final Map files = new LinkedHashMap(); + + public Stratum(String name) { + this.name = name; + } + + public File addFile(String fileName, int lineOffset, int size, String sourceFileName, String sourceFilePath) { + File file = this.files.get(fileName); + if (file == null) { + file = new File(this.files.size() + 1, lineOffset, size, sourceFileName, sourceFilePath); + this.files.put(fileName, file); + } + return file; + } + + void appendTo(StringBuilder sb) { + sb.append("*S ").append(this.name).append(SourceMap.NEWLINE); + + sb.append("*F").append(SourceMap.NEWLINE); + for (File file : this.files.values()) { + file.appendFile(sb); + } + + sb.append("*L").append(SourceMap.NEWLINE); + for (File file : this.files.values()) { + file.appendLines(sb); + } + } + + } + + /** + * Original source file name + */ + private final String sourceFile; + + /** + * SMAP strata + */ + private final Map strata = new LinkedHashMap(); + + private int nextLineOffset = 1; + + private String defaultStratum = "Mixin"; + + public SourceMap(String sourceFile) { + this.sourceFile = sourceFile; + } + + /** + * Get the original source file + */ + public String getSourceFile() { + return this.sourceFile; + } + + /** + * Get the generated source file + */ + public String getPseudoGeneratedSourceFile() { + return this.sourceFile.replace(".java", "$mixin.java"); + } + + public File addFile(ClassNode classNode) { + return this.addFile(this.defaultStratum, classNode); + } + + public File addFile(String stratumName, ClassNode classNode) { + return this.addFile(stratumName, classNode, classNode.sourceFile); + } + + public File addFile(ClassNode classNode, String name) { + return this.addFile(this.defaultStratum, classNode, name); + } + + public File addFile(String stratumName, ClassNode classNode, String name) { + return this.addFile(stratumName, name, classNode.sourceFile, classNode.name + ".java", ASMHelper.getMaxLineNumber(classNode, 500, 50)); + } + + public File addFile(String fileName, String sourceFileName, String sourceFilePath, int size) { + return this.addFile(this.defaultStratum, fileName, sourceFileName, sourceFilePath, size); + } + + public File addFile(String stratumName, String fileName, String sourceFileName, String sourceFilePath, int size) { + Stratum stratum = this.strata.get(stratumName); + if (stratum == null) { + stratum = new Stratum(stratumName); + this.strata.put(stratumName, stratum); + } + + File file = stratum.addFile(fileName, this.nextLineOffset, size, sourceFileName, sourceFilePath); + this.nextLineOffset += size; + return file; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + this.appendTo(sb); + return sb.toString(); + } + + private void appendTo(StringBuilder sb) { + // SMAP header + sb.append("SMAP").append(SourceMap.NEWLINE); + + // Source file + sb.append(this.getSourceFile()).append(SourceMap.NEWLINE); + + // Default stratum + sb.append(this.defaultStratum).append(SourceMap.NEWLINE); + for (Stratum stratum : this.strata.values()) { + stratum.appendTo(sb); + } + + // End + sb.append("*E").append(SourceMap.NEWLINE); + } + +} diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index 1743f589..5b0ac8f2 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -860,4 +860,25 @@ public class ASMHelper { return (field.access & flag) == flag; } + /** + * Compute the largest line number found in the specified class + * + * @param classNode Class to inspect + * @param min minimum value to return + * @param pad amount to pad at the end of files + * @return computed max + */ + public static int getMaxLineNumber(ClassNode classNode, int min, int pad) { + int max = 0; + for (MethodNode method : classNode.methods) { + for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { + AbstractInsnNode insn = iter.next(); + if (insn instanceof LineNumberNode) { + max = Math.max(max, ((LineNumberNode)insn).line); + } + } + } + return Math.max(min, max + pad); + } + }