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