CompileUnit.java revision 1007:4258ccc2eb8a
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
26package jdk.nashorn.internal.codegen;
27
28import java.util.Set;
29import java.util.TreeSet;
30
31/**
32 * Used to track split class compilation.
33 */
34public final class CompileUnit implements Comparable<CompileUnit> {
35    /** Current class name */
36    private final String className;
37
38    /** Current class generator */
39    private ClassEmitter classEmitter;
40
41    private long weight;
42
43    private Class<?> clazz;
44
45    private boolean isUsed;
46
47    CompileUnit(final String className, final ClassEmitter classEmitter, final long initialWeight) {
48        this.className    = className;
49        this.weight       = initialWeight;
50        this.classEmitter = classEmitter;
51    }
52
53    static Set<CompileUnit> createCompileUnitSet() {
54        return new TreeSet<>();
55    }
56
57    /**
58     * Check if this compile unit is used
59     * @return true if tagged as in use - i.e active code that needs to be generated
60     */
61    public boolean isUsed() {
62        return isUsed;
63    }
64
65    /**
66     * Tag this compile unit as used
67     */
68    public void setUsed() {
69        this.isUsed = true;
70    }
71
72    /**
73     * Return the class that contains the code for this unit, null if not
74     * generated yet
75     *
76     * @return class with compile unit code
77     */
78    public Class<?> getCode() {
79        return clazz;
80    }
81
82    /**
83     * Set class when it exists. Only accessible from compiler
84     * @param clazz class with code for this compile unit
85     */
86    void setCode(final Class<?> clazz) {
87        clazz.getClass(); // null check
88        this.clazz = clazz;
89        // Revisit this - refactor to avoid null-ed out non-final fields
90        // null out emitter
91        this.classEmitter = null;
92    }
93
94    /**
95     * Add weight to this compile unit
96     * @param w weight to add
97     */
98    void addWeight(final long w) {
99        this.weight += w;
100    }
101
102    /**
103     * Get the current weight of the compile unit.
104     * @return the unit's weight
105     */
106    long getWeight() {
107        return weight;
108    }
109
110    /**
111     * Check if this compile unit can hold {@code weight} more units of weight
112     * @param w weight to check if can be added
113     * @return true if weight fits in this compile unit
114     */
115    public boolean canHold(final long w) {
116        return (this.weight + w) < Splitter.SPLIT_THRESHOLD;
117    }
118
119    /**
120     * Get the class emitter for this compile unit
121     * @return class emitter
122     */
123    public ClassEmitter getClassEmitter() {
124        return classEmitter;
125    }
126
127    /**
128     * Get the class name for this compile unit
129     * @return the class name
130     */
131    public String getUnitClassName() {
132        return className;
133    }
134
135    private static String shortName(final String name) {
136        return name.lastIndexOf('/') == -1 ? name : name.substring(name.lastIndexOf('/') + 1);
137    }
138
139    @Override
140    public String toString() {
141        return "[CompileUnit className=" + shortName(className) + " weight=" + weight + '/' + Splitter.SPLIT_THRESHOLD + ']';
142    }
143
144    @Override
145    public int compareTo(final CompileUnit o) {
146        return className.compareTo(o.className);
147    }
148}
149