diff options
Diffstat (limited to 'src/jdk/nashorn/internal/codegen/CompileUnit.java')
-rw-r--r-- | src/jdk/nashorn/internal/codegen/CompileUnit.java | 88 |
1 files changed, 67 insertions, 21 deletions
diff --git a/src/jdk/nashorn/internal/codegen/CompileUnit.java b/src/jdk/nashorn/internal/codegen/CompileUnit.java index 73127259..13e9d716 100644 --- a/src/jdk/nashorn/internal/codegen/CompileUnit.java +++ b/src/jdk/nashorn/internal/codegen/CompileUnit.java @@ -25,28 +25,77 @@ package jdk.nashorn.internal.codegen; +import java.io.Serializable; +import java.util.Set; +import java.util.TreeSet; +import jdk.nashorn.internal.ir.CompileUnitHolder; + /** - * Used to track split class compilation. - */ -public class CompileUnit implements Comparable<CompileUnit> { + * Used to track split class compilation. Note that instances of the class are serializable, but all fields are + * transient, making the serialized version of the class only useful for tracking the referential topology of other + * AST nodes referencing the same or different compile units. We do want to preserve this topology though as + * {@link CompileUnitHolder}s in a deserialized AST will undergo reinitialization. + */ +public final class CompileUnit implements Comparable<CompileUnit>, Serializable { + private static final long serialVersionUID = 1L; + /** Current class name */ - private final String className; + private transient final String className; /** Current class generator */ - private ClassEmitter classEmitter; + private transient ClassEmitter classEmitter; - private long weight; + private transient long weight; - private Class<?> clazz; + private transient Class<?> clazz; - CompileUnit(final String className, final ClassEmitter classEmitter) { - this(className, classEmitter, 0L); - } + private transient boolean isUsed; + + private static int emittedUnitCount; CompileUnit(final String className, final ClassEmitter classEmitter, final long initialWeight) { this.className = className; - this.classEmitter = classEmitter; this.weight = initialWeight; + this.classEmitter = classEmitter; + } + + static Set<CompileUnit> createCompileUnitSet() { + return new TreeSet<>(); + } + + static void increaseEmitCount() { + emittedUnitCount++; + } + + /** + * Get the amount of emitted compile units so far in the system + * @return emitted compile unit count + */ + public static int getEmittedUnitCount() { + return emittedUnitCount; + } + + /** + * Check if this compile unit is used + * @return true if tagged as in use - i.e active code that needs to be generated + */ + public boolean isUsed() { + return isUsed; + } + + /** + * Check if a compile unit has code, not counting inits and clinits + * @return true of if there is "real code" in the compile unit + */ + public boolean hasCode() { + return (classEmitter.getMethodCount() - classEmitter.getInitCount() - classEmitter.getClinitCount()) > 0; + } + + /** + * Tag this compile unit as used + */ + public void setUsed() { + this.isUsed = true; } /** @@ -80,14 +129,6 @@ public class CompileUnit implements Comparable<CompileUnit> { } /** - * Get the current weight of the compile unit. - * @return the unit's weight - */ - long getWeight() { - return weight; - } - - /** * Check if this compile unit can hold {@code weight} more units of weight * @param w weight to check if can be added * @return true if weight fits in this compile unit @@ -112,13 +153,18 @@ public class CompileUnit implements Comparable<CompileUnit> { return className; } + private static String shortName(final String name) { + return name == null ? null : name.lastIndexOf('/') == -1 ? name : name.substring(name.lastIndexOf('/') + 1); + } + @Override public String toString() { - return "[classname=" + className + " weight=" + weight + '/' + Splitter.SPLIT_THRESHOLD + ']'; + final String methods = classEmitter != null ? classEmitter.getMethodNames().toString() : "<anon>"; + return "[CompileUnit className=" + shortName(className) + " weight=" + weight + '/' + Splitter.SPLIT_THRESHOLD + " hasCode=" + methods + ']'; } @Override - public int compareTo(CompileUnit o) { + public int compareTo(final CompileUnit o) { return className.compareTo(o.className); } } |