1 /*
   2  * Copyright (c) 2001, 2004, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.interpreter;
  26 
  27 import sun.jvm.hotspot.oops.*;
  28 import sun.jvm.hotspot.utilities.*;
  29 
  30 public class Bytecode {
  31   Method method;
  32   int bci;
  33   static final int jintSize = 4;
  34   static final String spaces = " ";
  35   static final String comma  = ", ";
  36 
  37   Bytecode(Method method, int bci) {
  38     this.method = method;
  39     this.bci    = bci;
  40   }
  41 
  42   // Address computation
  43   // NOTE: assumes that the start of the method's bytecodes is 4-byte aligned
  44   int alignedOffset(int offset) {
  45     return Bits.roundTo(bci + offset, jintSize) - bci;
  46   }
  47 
  48   int javaSignedWordAt(int offset) {
  49     return method.getBytecodeIntArg(bci + offset);
  50   }
  51 
  52   short javaShortAt(int offset) {
  53     return method.getBytecodeShortArg(bci + offset);
  54   }
  55 
  56   byte javaByteAt(int offset) {
  57     return method.getBytecodeByteArg(bci + offset);
  58   }
  59 
  60   public Method method() { return method; }
  61   public int    bci()    { return bci;    }
  62 
  63   // hotspot byte code
  64   public int code() {
  65     return Bytecodes.codeAt(method(), bci());
  66   }
  67 
  68   // jvm byte code
  69   public int javaCode() {
  70     return Bytecodes.javaCode(code());
  71   }
  72 
  73   public String getBytecodeName() {
  74     return Bytecodes.name(code());
  75   }
  76 
  77   public String getJavaBytecodeName() {
  78     return Bytecodes.name(javaCode());
  79   }
  80 
  81   public int getLength() {
  82     return Bytecodes.lengthAt(method(), bci());
  83   }
  84 
  85   public int getJavaLength() {
  86     return Bytecodes.javaLengthAt(method(), bci());
  87   }
  88 
  89   public String toString() {
  90     StringBuffer buf = new StringBuffer(getJavaBytecodeName());
  91     if (code() != javaCode()) {
  92        buf.append(spaces);
  93        buf.append('[');
  94        buf.append(getBytecodeName());
  95        buf.append(']');
  96     }
  97     return buf.toString();
  98   }
  99 }