jjg@283: /*
jjg@283: * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
jjg@283: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@283: *
jjg@283: * This code is free software; you can redistribute it and/or modify it
jjg@283: * under the terms of the GNU General Public License version 2 only, as
jjg@283: * published by the Free Software Foundation. Sun designates this
jjg@283: * particular file as subject to the "Classpath" exception as provided
jjg@283: * by Sun in the LICENSE file that accompanied this code.
jjg@283: *
jjg@283: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@283: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@283: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@283: * version 2 for more details (a copy is included in the LICENSE file that
jjg@283: * accompanied this code).
jjg@283: *
jjg@283: * You should have received a copy of the GNU General Public License version
jjg@283: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@283: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@283: *
jjg@283: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@283: * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@283: * have any questions.
jjg@283: */
jjg@283:
jjg@283: package com.sun.tools.javap;
jjg@283:
jjg@283: import com.sun.tools.classfile.AccessFlags;
jjg@283: import java.util.HashMap;
jjg@283: import java.util.Map;
jjg@283:
jjg@283: import com.sun.tools.classfile.Attribute;
jjg@283: import com.sun.tools.classfile.Code_attribute;
jjg@283: import com.sun.tools.classfile.ConstantPool;
jjg@283: import com.sun.tools.classfile.ConstantPoolException;
jjg@283: import com.sun.tools.classfile.Descriptor;
jjg@283: import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
jjg@283: import com.sun.tools.classfile.Instruction;
jjg@283: import com.sun.tools.classfile.Method;
jjg@283: import com.sun.tools.classfile.StackMapTable_attribute;
jjg@283: import com.sun.tools.classfile.StackMapTable_attribute.*;
jjg@283:
jjg@283: import static com.sun.tools.classfile.StackMapTable_attribute.verification_type_info.*;
jjg@283:
jjg@283: /**
jjg@283: * Annotate instructions with stack map.
jjg@283: *
jjg@283: *
This is NOT part of any API supported by Sun Microsystems. If
jjg@283: * you write code that depends on this, you do so at your own risk.
jjg@283: * This code and its internal interfaces are subject to change or
jjg@283: * deletion without notice.
jjg@283: */
jjg@283: public class StackMapWriter extends InstructionDetailWriter {
jjg@283: static StackMapWriter instance(Context context) {
jjg@283: StackMapWriter instance = context.get(StackMapWriter.class);
jjg@283: if (instance == null)
jjg@283: instance = new StackMapWriter(context);
jjg@283: return instance;
jjg@283: }
jjg@283:
jjg@283: protected StackMapWriter(Context context) {
jjg@283: super(context);
jjg@283: context.put(StackMapWriter.class, this);
jjg@283: classWriter = ClassWriter.instance(context);
jjg@283: }
jjg@283:
jjg@283: public void reset(Code_attribute attr) {
jjg@283: setStackMap((StackMapTable_attribute) attr.attributes.get(Attribute.StackMapTable));
jjg@283: }
jjg@283:
jjg@283: void setStackMap(StackMapTable_attribute attr) {
jjg@283: if (attr == null) {
jjg@283: map = null;
jjg@283: return;
jjg@283: }
jjg@283:
jjg@283: Method m = classWriter.getMethod();
jjg@283: Descriptor d = m.descriptor;
jjg@283: String[] args;
jjg@283: try {
jjg@283: ConstantPool cp = classWriter.getClassFile().constant_pool;
jjg@283: String argString = d.getParameterTypes(cp);
jjg@283: args = argString.substring(1, argString.length() - 1).split("[, ]+");
jjg@283: } catch (ConstantPoolException e) {
jjg@283: return;
jjg@283: } catch (InvalidDescriptor e) {
jjg@283: return;
jjg@283: }
jjg@283: boolean isStatic = m.access_flags.is(AccessFlags.ACC_STATIC);
jjg@283:
jjg@283: verification_type_info[] initialLocals = new verification_type_info[(isStatic ? 0 : 1) + args.length];
jjg@283: if (!isStatic)
jjg@283: initialLocals[0] = new CustomVerificationTypeInfo("this");
jjg@283: for (int i = 0; i < args.length; i++) {
jjg@283: initialLocals[(isStatic ? 0 : 1) + i] =
jjg@283: new CustomVerificationTypeInfo(args[i].replace(".", "/"));
jjg@283: }
jjg@283:
jjg@283: map = new HashMap();
jjg@283: StackMapBuilder builder = new StackMapBuilder();
jjg@283:
jjg@283: // using -1 as the pc for the initial frame effectively compensates for
jjg@283: // the difference in behavior for the first stack map frame (where the
jjg@283: // pc offset is just offset_delta) compared to subsequent frames (where
jjg@283: // the pc offset is always offset_delta+1).
jjg@283: int pc = -1;
jjg@283:
jjg@283: map.put(pc, new StackMap(initialLocals, empty));
jjg@283:
jjg@283: for (int i = 0; i < attr.entries.length; i++)
jjg@283: pc = attr.entries[i].accept(builder, pc);
jjg@283: }
jjg@283:
jjg@283: public void writeInitialDetails() {
jjg@283: writeDetails(-1);
jjg@283: }
jjg@283:
jjg@283: public void writeDetails(Instruction instr) {
jjg@283: writeDetails(instr.getPC());
jjg@283: }
jjg@283:
jjg@283: private void writeDetails(int pc) {
jjg@283: if (map == null)
jjg@283: return;
jjg@283:
jjg@283: StackMap m = map.get(pc);
jjg@283: if (m != null) {
jjg@283: print("StackMap locals: ", m.locals);
jjg@283: print("StackMap stack: ", m.stack);
jjg@283: }
jjg@283:
jjg@283: }
jjg@283:
jjg@283: void print(String label, verification_type_info[] entries) {
jjg@283: print(label);
jjg@283: for (int i = 0; i < entries.length; i++) {
jjg@283: print(" ");
jjg@283: print(entries[i]);
jjg@283: }
jjg@283: println();
jjg@283: }
jjg@283:
jjg@283: void print(verification_type_info entry) {
jjg@283: if (entry == null) {
jjg@283: print("ERROR");
jjg@283: return;
jjg@283: }
jjg@283:
jjg@283: switch (entry.tag) {
jjg@283: case -1:
jjg@283: print(((CustomVerificationTypeInfo) entry).text);
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Top:
jjg@283: print("top");
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Integer:
jjg@283: print("int");
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Float:
jjg@283: print("float");
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Long:
jjg@283: print("long");
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Double:
jjg@283: print("double");
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Null:
jjg@283: print("null");
jjg@283: break;
jjg@283:
jjg@283: case ITEM_UninitializedThis:
jjg@283: print("uninit_this");
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Object:
jjg@283: try {
jjg@283: ConstantPool cp = classWriter.getClassFile().constant_pool;
jjg@283: ConstantPool.CONSTANT_Class_info class_info = cp.getClassInfo(((Object_variable_info) entry).cpool_index);
jjg@283: print(cp.getUTF8Value(class_info.name_index));
jjg@283: } catch (ConstantPoolException e) {
jjg@283: print("??");
jjg@283: }
jjg@283: break;
jjg@283:
jjg@283: case ITEM_Uninitialized:
jjg@283: print(((Uninitialized_variable_info) entry).offset);
jjg@283: break;
jjg@283: }
jjg@283:
jjg@283: }
jjg@283:
jjg@283: private Map map;
jjg@283: private ClassWriter classWriter;
jjg@283:
jjg@283: class StackMapBuilder
jjg@283: implements StackMapTable_attribute.stack_map_frame.Visitor {
jjg@283:
jjg@283: public Integer visit_same_frame(same_frame frame, Integer pc) {
jjg@283: int new_pc = pc + frame.getOffsetDelta() + 1;
jjg@283: StackMap m = map.get(pc);
jjg@283: assert (m != null);
jjg@283: map.put(new_pc, m);
jjg@283: return new_pc;
jjg@283: }
jjg@283:
jjg@283: public Integer visit_same_locals_1_stack_item_frame(same_locals_1_stack_item_frame frame, Integer pc) {
jjg@283: int new_pc = pc + frame.getOffsetDelta() + 1;
jjg@283: StackMap prev = map.get(pc);
jjg@283: assert (prev != null);
jjg@283: StackMap m = new StackMap(prev.locals, frame.stack);
jjg@283: map.put(new_pc, m);
jjg@283: return new_pc;
jjg@283: }
jjg@283:
jjg@283: public Integer visit_same_locals_1_stack_item_frame_extended(same_locals_1_stack_item_frame_extended frame, Integer pc) {
jjg@283: int new_pc = pc + frame.getOffsetDelta() + 1;
jjg@283: StackMap prev = map.get(pc);
jjg@283: assert (prev != null);
jjg@283: StackMap m = new StackMap(prev.locals, frame.stack);
jjg@283: map.put(new_pc, m);
jjg@283: return new_pc;
jjg@283: }
jjg@283:
jjg@283: public Integer visit_chop_frame(chop_frame frame, Integer pc) {
jjg@283: int new_pc = pc + frame.getOffsetDelta() + 1;
jjg@283: StackMap prev = map.get(pc);
jjg@283: assert (prev != null);
jjg@283: int k = 251 - frame.frame_type;
jjg@283: verification_type_info[] new_locals = new verification_type_info[prev.locals.length - k];
jjg@283: System.arraycopy(prev.locals, 0, new_locals, 0, new_locals.length);
jjg@283: StackMap m = new StackMap(new_locals, empty);
jjg@283: map.put(new_pc, m);
jjg@283: return new_pc;
jjg@283: }
jjg@283:
jjg@283: public Integer visit_same_frame_extended(same_frame_extended frame, Integer pc) {
jjg@283: int new_pc = pc + frame.getOffsetDelta();
jjg@283: StackMap m = map.get(pc);
jjg@283: assert (m != null);
jjg@283: map.put(new_pc, m);
jjg@283: return new_pc;
jjg@283: }
jjg@283:
jjg@283: public Integer visit_append_frame(append_frame frame, Integer pc) {
jjg@283: int new_pc = pc + frame.getOffsetDelta() + 1;
jjg@283: StackMap prev = map.get(pc);
jjg@283: assert (prev != null);
jjg@283: verification_type_info[] new_locals = new verification_type_info[prev.locals.length + frame.locals.length];
jjg@283: System.arraycopy(prev.locals, 0, new_locals, 0, prev.locals.length);
jjg@283: System.arraycopy(frame.locals, 0, new_locals, prev.locals.length, frame.locals.length);
jjg@283: StackMap m = new StackMap(new_locals, empty);
jjg@283: map.put(new_pc, m);
jjg@283: return new_pc;
jjg@283: }
jjg@283:
jjg@283: public Integer visit_full_frame(full_frame frame, Integer pc) {
jjg@283: int new_pc = pc + frame.getOffsetDelta() + 1;
jjg@283: StackMap m = new StackMap(frame.locals, frame.stack);
jjg@283: map.put(new_pc, m);
jjg@283: return new_pc;
jjg@283: }
jjg@283:
jjg@283: }
jjg@283:
jjg@283: class StackMap {
jjg@283: StackMap(verification_type_info[] locals, verification_type_info[] stack) {
jjg@283: this.locals = locals;
jjg@283: this.stack = stack;
jjg@283: }
jjg@283:
jjg@283: private final verification_type_info[] locals;
jjg@283: private final verification_type_info[] stack;
jjg@283: }
jjg@283:
jjg@283: class CustomVerificationTypeInfo extends verification_type_info {
jjg@283: public CustomVerificationTypeInfo(String text) {
jjg@283: super(-1);
jjg@283: this.text = text;
jjg@283: }
jjg@283: private String text;
jjg@283: }
jjg@283:
jjg@283: private final verification_type_info[] empty = { };
jjg@283: }