Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/asm/LocalVariableTable.java
38918 views
/*1* Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.asm;2627import sun.tools.java.*;28import java.io.IOException;29import java.io.DataOutputStream;3031/**32* This class is used to assemble the local variable table.33*34* WARNING: The contents of this source file are not part of any35* supported API. Code that depends on them does so at its own risk:36* they are subject to change or removal without notice.37*38* @author Arthur van Hoff39*/40final41class LocalVariableTable {42LocalVariable locals[] = new LocalVariable[8];43int len;4445/**46* Define a new local variable. Merge entries where possible.47*/48void define(MemberDefinition field, int slot, int from, int to) {49if (from >= to) {50return;51}52for (int i = 0 ; i < len ; i++) {53if ((locals[i].field == field) && (locals[i].slot == slot) &&54(from <= locals[i].to) && (to >= locals[i].from)) {55locals[i].from = Math.min(locals[i].from, from);56locals[i].to = Math.max(locals[i].to, to);57return;58}59}60if (len == locals.length) {61LocalVariable newlocals[] = new LocalVariable[len * 2];62System.arraycopy(locals, 0, newlocals, 0, len);63locals = newlocals;64}65locals[len++] = new LocalVariable(field, slot, from, to);66}6768/**69* Trim overlapping local ranges. Java forbids shadowing of70* locals in nested scopes, but non-nested scopes may still declare71* locals with the same name. Because local variable ranges are72* computed using flow analysis as part of assembly, it isn't73* possible to simply make sure variable ranges end where the74* enclosing lexical scope ends. This method makes sure that75* variables with the same name don't overlap, giving priority to76* fields with higher slot numbers that should have appeared later77* in the source.78*/79private void trim_ranges() {80for (int i=0; i<len; i++) {81for (int j=i+1; j<len; j++) {82if ((locals[i].field.getName()==locals[j].field.getName())83&& (locals[i].from <= locals[j].to)84&& (locals[i].to >= locals[j].from)) {85// At this point we know that both ranges are86// the same name and there is also overlap or they abut87if (locals[i].slot < locals[j].slot) {88if (locals[i].from < locals[j].from) {89locals[i].to = Math.min(locals[i].to, locals[j].from);90} else {91// We've detected two local variables with the92// same name, and the one with the greater slot93// number starts before the other. This order94// reversal may happen with locals with the same95// name declared in both a try body and an96// associated catch clause. This is rare, and97// we give up.98}99} else if (locals[i].slot > locals[j].slot) {100if (locals[i].from > locals[j].from) {101locals[j].to = Math.min(locals[j].to, locals[i].from);102} else {103// Same situation as above; just give up.104}105} else {106// This case can happen if there are two variables107// with the same name and slot numbers, and ranges108// that abut. AFAIK the only way this can occur109// is with multiple static initializers. Punt.110}111}112}113}114}115116/**117* Write out the data.118*/119void write(Environment env, DataOutputStream out, ConstantPool tab) throws IOException {120trim_ranges();121out.writeShort(len);122for (int i = 0 ; i < len ; i++) {123//System.out.println("pc=" + locals[i].from + ", len=" + (locals[i].to - locals[i].from) + ", nm=" + locals[i].field.getName() + ", slot=" + locals[i].slot);124out.writeShort(locals[i].from);125out.writeShort(locals[i].to - locals[i].from);126out.writeShort(tab.index(locals[i].field.getName().toString()));127out.writeShort(tab.index(locals[i].field.getType().getTypeSignature()));128out.writeShort(locals[i].slot);129}130}131}132133134