Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/javap/CodeWriter.java
58461 views
/*1* Copyright (c) 2007, 2020, 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 com.sun.tools.javap;2627import java.util.ArrayList;28import java.util.List;2930import com.sun.tools.classfile.AccessFlags;31import com.sun.tools.classfile.Code_attribute;32import com.sun.tools.classfile.ConstantPool;33import com.sun.tools.classfile.ConstantPoolException;34import com.sun.tools.classfile.DescriptorException;35import com.sun.tools.classfile.Instruction;36import com.sun.tools.classfile.Instruction.TypeKind;37import com.sun.tools.classfile.Method;3839/*40* Write the contents of a Code attribute.41*42* <p><b>This is NOT part of any supported API.43* If you write code that depends on this, you do so at your own risk.44* This code and its internal interfaces are subject to change or45* deletion without notice.</b>46*/47public class CodeWriter extends BasicWriter {48public static CodeWriter instance(Context context) {49CodeWriter instance = context.get(CodeWriter.class);50if (instance == null)51instance = new CodeWriter(context);52return instance;53}5455protected CodeWriter(Context context) {56super(context);57context.put(CodeWriter.class, this);58attrWriter = AttributeWriter.instance(context);59classWriter = ClassWriter.instance(context);60constantWriter = ConstantWriter.instance(context);61sourceWriter = SourceWriter.instance(context);62tryBlockWriter = TryBlockWriter.instance(context);63stackMapWriter = StackMapWriter.instance(context);64localVariableTableWriter = LocalVariableTableWriter.instance(context);65localVariableTypeTableWriter = LocalVariableTypeTableWriter.instance(context);66typeAnnotationWriter = TypeAnnotationWriter.instance(context);67options = Options.instance(context);68}6970void write(Code_attribute attr, ConstantPool constant_pool) {71println("Code:");72indent(+1);73writeVerboseHeader(attr, constant_pool);74writeInstrs(attr);75writeExceptionTable(attr);76attrWriter.write(attr, attr.attributes, constant_pool);77indent(-1);78}7980public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) {81Method method = classWriter.getMethod();82String argCount;83try {84int n = method.descriptor.getParameterCount(constant_pool);85if (!method.access_flags.is(AccessFlags.ACC_STATIC))86++n; // for 'this'87argCount = Integer.toString(n);88} catch (ConstantPoolException e) {89argCount = report(e);90} catch (DescriptorException e) {91argCount = report(e);92}9394println("stack=" + attr.max_stack +95", locals=" + attr.max_locals +96", args_size=" + argCount);9798}99100public void writeInstrs(Code_attribute attr) {101List<InstructionDetailWriter> detailWriters = getDetailWriters(attr);102103for (Instruction instr: attr.getInstructions()) {104try {105for (InstructionDetailWriter w: detailWriters)106w.writeDetails(instr);107writeInstr(instr);108} catch (ArrayIndexOutOfBoundsException | IllegalStateException e) {109println(report("error at or after byte " + instr.getPC()));110break;111}112}113114for (InstructionDetailWriter w: detailWriters)115w.flush();116}117118public void writeInstr(Instruction instr) {119print(String.format("%4d: %-13s ", instr.getPC(), instr.getMnemonic()));120// compute the number of indentations for the body of multi-line instructions121// This is 6 (the width of "%4d: "), divided by the width of each indentation level,122// and rounded up to the next integer.123int indentWidth = options.indentWidth;124int indent = (6 + indentWidth - 1) / indentWidth;125instr.accept(instructionPrinter, indent);126println();127}128// where129Instruction.KindVisitor<Void,Integer> instructionPrinter =130new Instruction.KindVisitor<>() {131132public Void visitNoOperands(Instruction instr, Integer indent) {133return null;134}135136public Void visitArrayType(Instruction instr, TypeKind kind, Integer indent) {137print(" " + kind.name);138return null;139}140141public Void visitBranch(Instruction instr, int offset, Integer indent) {142print((instr.getPC() + offset));143return null;144}145146public Void visitConstantPoolRef(Instruction instr, int index, Integer indent) {147print("#" + index);148tab();149print("// ");150printConstant(index);151return null;152}153154public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Integer indent) {155print("#" + index + ", " + value);156tab();157print("// ");158printConstant(index);159return null;160}161162public Void visitLocal(Instruction instr, int index, Integer indent) {163print(index);164return null;165}166167public Void visitLocalAndValue(Instruction instr, int index, int value, Integer indent) {168print(index + ", " + value);169return null;170}171172public Void visitLookupSwitch(Instruction instr,173int default_, int npairs, int[] matches, int[] offsets, Integer indent) {174int pc = instr.getPC();175print("{ // " + npairs);176indent(indent);177for (int i = 0; i < npairs; i++) {178print(String.format("%n%12d: %d", matches[i], (pc + offsets[i])));179}180print("\n default: " + (pc + default_) + "\n}");181indent(-indent);182return null;183}184185public Void visitTableSwitch(Instruction instr,186int default_, int low, int high, int[] offsets, Integer indent) {187int pc = instr.getPC();188print("{ // " + low + " to " + high);189indent(indent);190for (int i = 0; i < offsets.length; i++) {191print(String.format("%n%12d: %d", (low + i), (pc + offsets[i])));192}193print("\n default: " + (pc + default_) + "\n}");194indent(-indent);195return null;196}197198public Void visitValue(Instruction instr, int value, Integer indent) {199print(value);200return null;201}202203public Void visitUnknown(Instruction instr, Integer indent) {204return null;205}206};207208209public void writeExceptionTable(Code_attribute attr) {210if (attr.exception_table_length > 0) {211println("Exception table:");212indent(+1);213println(" from to target type");214for (int i = 0; i < attr.exception_table.length; i++) {215Code_attribute.Exception_data handler = attr.exception_table[i];216print(String.format(" %5d %5d %5d",217handler.start_pc, handler.end_pc, handler.handler_pc));218print(" ");219int catch_type = handler.catch_type;220if (catch_type == 0) {221println("any");222} else {223print("Class ");224println(constantWriter.stringValue(catch_type));225}226}227indent(-1);228}229230}231232private void printConstant(int index) {233constantWriter.write(index);234}235236private List<InstructionDetailWriter> getDetailWriters(Code_attribute attr) {237List<InstructionDetailWriter> detailWriters = new ArrayList<>();238if (options.details.contains(InstructionDetailWriter.Kind.SOURCE)) {239sourceWriter.reset(classWriter.getClassFile(), attr);240if (sourceWriter.hasSource())241detailWriters.add(sourceWriter);242else243println("(Source code not available)");244}245246if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VARS)) {247localVariableTableWriter.reset(attr);248detailWriters.add(localVariableTableWriter);249}250251if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VAR_TYPES)) {252localVariableTypeTableWriter.reset(attr);253detailWriters.add(localVariableTypeTableWriter);254}255256if (options.details.contains(InstructionDetailWriter.Kind.STACKMAPS)) {257stackMapWriter.reset(attr);258stackMapWriter.writeInitialDetails();259detailWriters.add(stackMapWriter);260}261262if (options.details.contains(InstructionDetailWriter.Kind.TRY_BLOCKS)) {263tryBlockWriter.reset(attr);264detailWriters.add(tryBlockWriter);265}266267if (options.details.contains(InstructionDetailWriter.Kind.TYPE_ANNOS)) {268typeAnnotationWriter.reset(attr);269detailWriters.add(typeAnnotationWriter);270}271272return detailWriters;273}274275private AttributeWriter attrWriter;276private ClassWriter classWriter;277private ConstantWriter constantWriter;278private LocalVariableTableWriter localVariableTableWriter;279private LocalVariableTypeTableWriter localVariableTypeTableWriter;280private TypeAnnotationWriter typeAnnotationWriter;281private SourceWriter sourceWriter;282private StackMapWriter stackMapWriter;283private TryBlockWriter tryBlockWriter;284private Options options;285}286287288