1 /* 2 * Copyright (c) 1995, 2019, 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 26 27 package sun.tools.java; 28 29 import java.io.*; 30 31 /** 32 * WARNING: The contents of this source file are not part of any 33 * supported API. Code that depends on them does so at its own risk: 34 * they are subject to change or removal without notice. 35 */ 36 public class BinaryCode implements Constants { 37 int maxStack; // maximum stack used by code 38 int maxLocals; // maximum locals used by code 39 BinaryExceptionHandler exceptionHandlers[]; 40 BinaryAttribute atts; // code attributes 41 BinaryConstantPool cpool; // constant pool of the class 42 byte code[]; // the byte code 43 44 /** 45 * Construct the binary code from the code attribute 46 */ 47 48 public 49 BinaryCode(byte data[], BinaryConstantPool cpool, Environment env) { 50 DataInputStream in = new DataInputStream(new ByteArrayInputStream(data)); 51 try { 52 this.cpool = cpool; 53 // JVM 4.7.4 CodeAttribute.max_stack 54 this.maxStack = in.readUnsignedShort(); 55 // JVM 4.7.4 CodeAttribute.max_locals 56 this.maxLocals = in.readUnsignedShort(); 57 // JVM 4.7.4 CodeAttribute.code_length 58 int code_length = in.readInt(); 59 this.code = new byte[code_length]; 60 // JVM 4.7.4 CodeAttribute.code[] 61 in.read(this.code); 62 // JVM 4.7.4 CodeAttribute.exception_table_length 63 int exception_count = in.readUnsignedShort(); 64 this.exceptionHandlers = new BinaryExceptionHandler[exception_count]; 65 for (int i = 0; i < exception_count; i++) { 66 // JVM 4.7.4 CodeAttribute.exception_table.start_pc 67 int start = in.readUnsignedShort(); 68 // JVM 4.7.4 CodeAttribute.exception_table.end_pc 69 int end = in.readUnsignedShort(); 70 // JVM 4.7.4 CodeAttribute.exception_table.handler_pc 71 int handler = in.readUnsignedShort(); 72 // JVM 4.7.4 CodeAttribute.exception_table.catch_type 73 ClassDeclaration xclass = cpool.getDeclaration(env, in.readUnsignedShort()); 74 this.exceptionHandlers[i] = 75 new BinaryExceptionHandler(start, end, handler, xclass); 76 } 77 this.atts = BinaryAttribute.load(in, cpool, ~0); 78 if (in.available() != 0) { 79 System.err.println("Should have exhausted input stream!"); 80 } 81 } catch (IOException e) { 82 throw new CompilerError(e); 83 } 84 } 85 86 87 /** 88 * Accessors 89 */ 90 91 public BinaryExceptionHandler[] getExceptionHandlers() { 92 return exceptionHandlers; 93 } 94 95 public byte[] getCode() { return code; } 96 97 public int getMaxStack() { return maxStack; } 98 99 public int getMaxLocals() { return maxLocals; } 100 101 public BinaryAttribute getAttributes() { return atts; } 102 103 /** 104 * Load a binary class 105 */ 106 public static 107 BinaryCode load(BinaryMember bf, BinaryConstantPool cpool, Environment env) { 108 byte code[] = bf.getAttribute(idCode); 109 return (code != null) ? new BinaryCode(code, cpool, env) : null; 110 } 111 }