1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.codegen; 27 28 import java.io.Serializable; 29 import java.util.Set; 30 import java.util.TreeSet; 31 import jdk.nashorn.internal.ir.CompileUnitHolder; 32 33 /** 34 * Used to track split class compilation. Note that instances of the class are serializable, but all fields are 35 * transient, making the serialized version of the class only useful for tracking the referential topology of other 36 * AST nodes referencing the same or different compile units. We do want to preserve this topology though as 37 * {@link CompileUnitHolder}s in a deserialized AST will undergo reinitialization. 38 */ 39 public final class CompileUnit implements Comparable<CompileUnit>, Serializable { 40 private static final long serialVersionUID = 1L; 41 42 /** Current class name */ 43 private transient final String className; 44 45 /** Current class generator */ 46 private transient ClassEmitter classEmitter; 47 48 private transient long weight; 49 50 private transient Class<?> clazz; 51 52 private transient boolean isUsed; 53 54 private static int emittedUnitCount; 55 56 CompileUnit(final String className, final ClassEmitter classEmitter, final long initialWeight) { 57 this.className = className; 58 this.weight = initialWeight; 59 this.classEmitter = classEmitter; 60 } 61 62 static Set<CompileUnit> createCompileUnitSet() { 63 return new TreeSet<>(); 64 } 65 66 static void increaseEmitCount() { 67 emittedUnitCount++; 68 } 69 70 /** 71 * Get the amount of emitted compile units so far in the system 72 * @return emitted compile unit count 73 */ 74 public static int getEmittedUnitCount() { 75 return emittedUnitCount; 76 } 77 78 /** 79 * Check if this compile unit is used 80 * @return true if tagged as in use - i.e active code that needs to be generated 81 */ 82 public boolean isUsed() { 83 return isUsed; 84 } 85 86 /** 87 * Check if a compile unit has code, not counting inits and clinits 88 * @return true of if there is "real code" in the compile unit 89 */ 90 public boolean hasCode() { 91 return (classEmitter.getMethodCount() - classEmitter.getInitCount() - classEmitter.getClinitCount()) > 0; 92 } 93 94 /** 95 * Tag this compile unit as used 96 */ 97 public void setUsed() { 98 this.isUsed = true; 99 } 100 101 /** 102 * Return the class that contains the code for this unit, null if not 103 * generated yet 104 * 105 * @return class with compile unit code 106 */ 107 public Class<?> getCode() { 108 return clazz; 109 } 110 111 /** 112 * Set class when it exists. Only accessible from compiler 113 * @param clazz class with code for this compile unit 114 */ 115 void setCode(final Class<?> clazz) { 116 clazz.getClass(); // null check 117 this.clazz = clazz; 118 // Revisit this - refactor to avoid null-ed out non-final fields 119 // null out emitter 120 this.classEmitter = null; 121 } 122 123 /** 124 * Add weight to this compile unit 125 * @param w weight to add 126 */ 127 void addWeight(final long w) { 128 this.weight += w; 129 } 130 131 /** 132 * Check if this compile unit can hold {@code weight} more units of weight 133 * @param w weight to check if can be added 134 * @return true if weight fits in this compile unit 135 */ 136 public boolean canHold(final long w) { 137 return (this.weight + w) < Splitter.SPLIT_THRESHOLD; 138 } 139 140 /** 141 * Get the class emitter for this compile unit 142 * @return class emitter 143 */ 144 public ClassEmitter getClassEmitter() { 145 return classEmitter; 146 } 147 148 /** 149 * Get the class name for this compile unit 150 * @return the class name 151 */ 152 public String getUnitClassName() { 153 return className; 154 } 155 156 private static String shortName(final String name) { 157 return name == null ? null : name.lastIndexOf('/') == -1 ? name : name.substring(name.lastIndexOf('/') + 1); 158 } 159 160 @Override 161 public String toString() { 162 final String methods = classEmitter != null ? classEmitter.getMethodNames().toString() : "<anon>"; 163 return "[CompileUnit className=" + shortName(className) + " weight=" + weight + '/' + Splitter.SPLIT_THRESHOLD + " hasCode=" + methods + ']'; 164 } 165 166 @Override 167 public int compareTo(final CompileUnit o) { 168 return className.compareTo(o.className); 169 } 170 }