src/share/vm/oops/constMethod.cpp

changeset 4037
da91efe96a93
parent 3917
8150fa46d2ed
child 4245
4735d2c84362
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/oops/constMethod.cpp	Sat Sep 01 13:25:18 2012 -0400
     1.3 @@ -0,0 +1,303 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "interpreter/interpreter.hpp"
    1.30 +#include "memory/gcLocker.hpp"
    1.31 +#include "memory/metadataFactory.hpp"
    1.32 +#include "oops/constMethod.hpp"
    1.33 +#include "oops/method.hpp"
    1.34 +
    1.35 +// Static initialization
    1.36 +const u2 ConstMethod::MAX_IDNUM   = 0xFFFE;
    1.37 +const u2 ConstMethod::UNSET_IDNUM = 0xFFFF;
    1.38 +
    1.39 +ConstMethod* ConstMethod::allocate(ClassLoaderData* loader_data,
    1.40 +                                            int byte_code_size,
    1.41 +                                            int compressed_line_number_size,
    1.42 +                                            int localvariable_table_length,
    1.43 +                                            int exception_table_length,
    1.44 +                                            int checked_exceptions_length,
    1.45 +                                            TRAPS) {
    1.46 +  int size = ConstMethod::size(byte_code_size,
    1.47 +                                      compressed_line_number_size,
    1.48 +                                      localvariable_table_length,
    1.49 +                                      exception_table_length,
    1.50 +                                      checked_exceptions_length);
    1.51 +  return new (loader_data, size, true, THREAD) ConstMethod(
    1.52 +                       byte_code_size, compressed_line_number_size,
    1.53 +                       localvariable_table_length, exception_table_length,
    1.54 +                       checked_exceptions_length, size);
    1.55 +}
    1.56 +
    1.57 +ConstMethod::ConstMethod(int byte_code_size,
    1.58 +                                       int compressed_line_number_size,
    1.59 +                                       int localvariable_table_length,
    1.60 +                                       int exception_table_length,
    1.61 +                                       int checked_exceptions_length,
    1.62 +                                       int size) {
    1.63 +
    1.64 +  No_Safepoint_Verifier no_safepoint;
    1.65 +  set_interpreter_kind(Interpreter::invalid);
    1.66 +  init_fingerprint();
    1.67 +  set_constants(NULL);
    1.68 +  set_stackmap_data(NULL);
    1.69 +  set_code_size(byte_code_size);
    1.70 +  set_constMethod_size(size);
    1.71 +  set_inlined_tables_length(checked_exceptions_length,
    1.72 +                            compressed_line_number_size,
    1.73 +                            localvariable_table_length,
    1.74 +                            exception_table_length);
    1.75 +  assert(this->size() == size, "wrong size for object");
    1.76 +}
    1.77 +
    1.78 +
    1.79 +// Deallocate metadata fields associated with ConstMethod*
    1.80 +void ConstMethod::deallocate_contents(ClassLoaderData* loader_data) {
    1.81 +  set_interpreter_kind(Interpreter::invalid);
    1.82 +  if (stackmap_data() != NULL) {
    1.83 +    MetadataFactory::free_array<u1>(loader_data, stackmap_data());
    1.84 +  }
    1.85 +  set_stackmap_data(NULL);
    1.86 +}
    1.87 +
    1.88 +// How big must this constMethodObject be?
    1.89 +
    1.90 +int ConstMethod::size(int code_size,
    1.91 +                                    int compressed_line_number_size,
    1.92 +                                    int local_variable_table_length,
    1.93 +                                    int exception_table_length,
    1.94 +                                    int checked_exceptions_length) {
    1.95 +  int extra_bytes = code_size;
    1.96 +  if (compressed_line_number_size > 0) {
    1.97 +    extra_bytes += compressed_line_number_size;
    1.98 +  }
    1.99 +  if (checked_exceptions_length > 0) {
   1.100 +    extra_bytes += sizeof(u2);
   1.101 +    extra_bytes += checked_exceptions_length * sizeof(CheckedExceptionElement);
   1.102 +  }
   1.103 +  if (local_variable_table_length > 0) {
   1.104 +    extra_bytes += sizeof(u2);
   1.105 +    extra_bytes +=
   1.106 +              local_variable_table_length * sizeof(LocalVariableTableElement);
   1.107 +  }
   1.108 +  if (exception_table_length > 0) {
   1.109 +    extra_bytes += sizeof(u2);
   1.110 +    extra_bytes += exception_table_length * sizeof(ExceptionTableElement);
   1.111 +  }
   1.112 +  int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord;
   1.113 +  return align_object_size(header_size() + extra_words);
   1.114 +}
   1.115 +
   1.116 +Method* ConstMethod::method() const {
   1.117 +    return InstanceKlass::cast(_constants->pool_holder())->method_with_idnum(
   1.118 +                               _method_idnum);
   1.119 +  }
   1.120 +
   1.121 +// linenumber table - note that length is unknown until decompression,
   1.122 +// see class CompressedLineNumberReadStream.
   1.123 +
   1.124 +u_char* ConstMethod::compressed_linenumber_table() const {
   1.125 +  // Located immediately following the bytecodes.
   1.126 +  assert(has_linenumber_table(), "called only if table is present");
   1.127 +  return code_end();
   1.128 +}
   1.129 +
   1.130 +u2* ConstMethod::checked_exceptions_length_addr() const {
   1.131 +  // Located at the end of the constMethod.
   1.132 +  assert(has_checked_exceptions(), "called only if table is present");
   1.133 +  return last_u2_element();
   1.134 +}
   1.135 +
   1.136 +u2* ConstMethod::exception_table_length_addr() const {
   1.137 +  assert(has_exception_handler(), "called only if table is present");
   1.138 +  if (has_checked_exceptions()) {
   1.139 +    // If checked_exception present, locate immediately before them.
   1.140 +    return (u2*) checked_exceptions_start() - 1;
   1.141 +  } else {
   1.142 +    // Else, the exception table is at the end of the constMethod.
   1.143 +    return last_u2_element();
   1.144 +  }
   1.145 +}
   1.146 +
   1.147 +u2* ConstMethod::localvariable_table_length_addr() const {
   1.148 +  assert(has_localvariable_table(), "called only if table is present");
   1.149 +  if (has_exception_handler()) {
   1.150 +    // If exception_table present, locate immediately before them.
   1.151 +    return (u2*) exception_table_start() - 1;
   1.152 +  } else {
   1.153 +    if (has_checked_exceptions()) {
   1.154 +      // If checked_exception present, locate immediately before them.
   1.155 +      return (u2*) checked_exceptions_start() - 1;
   1.156 +    } else {
   1.157 +      // Else, the linenumber table is at the end of the constMethod.
   1.158 +      return last_u2_element();
   1.159 +    }
   1.160 +  }
   1.161 +}
   1.162 +
   1.163 +
   1.164 +// Update the flags to indicate the presence of these optional fields.
   1.165 +void ConstMethod::set_inlined_tables_length(
   1.166 +                                              int checked_exceptions_len,
   1.167 +                                              int compressed_line_number_size,
   1.168 +                                              int localvariable_table_len,
   1.169 +                                              int exception_table_len) {
   1.170 +  // Must be done in the order below, otherwise length_addr accessors
   1.171 +  // will not work. Only set bit in header if length is positive.
   1.172 +  assert(_flags == 0, "Error");
   1.173 +  if (compressed_line_number_size > 0) {
   1.174 +    _flags |= _has_linenumber_table;
   1.175 +  }
   1.176 +  if (checked_exceptions_len > 0) {
   1.177 +    _flags |= _has_checked_exceptions;
   1.178 +    *(checked_exceptions_length_addr()) = checked_exceptions_len;
   1.179 +  }
   1.180 +  if (exception_table_len > 0) {
   1.181 +    _flags |= _has_exception_table;
   1.182 +    *(exception_table_length_addr()) = exception_table_len;
   1.183 +  }
   1.184 +  if (localvariable_table_len > 0) {
   1.185 +    _flags |= _has_localvariable_table;
   1.186 +    *(localvariable_table_length_addr()) = localvariable_table_len;
   1.187 +  }
   1.188 +}
   1.189 +
   1.190 +
   1.191 +int ConstMethod::checked_exceptions_length() const {
   1.192 +  return has_checked_exceptions() ? *(checked_exceptions_length_addr()) : 0;
   1.193 +}
   1.194 +
   1.195 +
   1.196 +CheckedExceptionElement* ConstMethod::checked_exceptions_start() const {
   1.197 +  u2* addr = checked_exceptions_length_addr();
   1.198 +  u2 length = *addr;
   1.199 +  assert(length > 0, "should only be called if table is present");
   1.200 +  addr -= length * sizeof(CheckedExceptionElement) / sizeof(u2);
   1.201 +  return (CheckedExceptionElement*) addr;
   1.202 +}
   1.203 +
   1.204 +
   1.205 +int ConstMethod::localvariable_table_length() const {
   1.206 +  return has_localvariable_table() ? *(localvariable_table_length_addr()) : 0;
   1.207 +}
   1.208 +
   1.209 +
   1.210 +LocalVariableTableElement* ConstMethod::localvariable_table_start() const {
   1.211 +  u2* addr = localvariable_table_length_addr();
   1.212 +  u2 length = *addr;
   1.213 +  assert(length > 0, "should only be called if table is present");
   1.214 +  addr -= length * sizeof(LocalVariableTableElement) / sizeof(u2);
   1.215 +  return (LocalVariableTableElement*) addr;
   1.216 +}
   1.217 +
   1.218 +int ConstMethod::exception_table_length() const {
   1.219 +  return has_exception_handler() ? *(exception_table_length_addr()) : 0;
   1.220 +}
   1.221 +
   1.222 +ExceptionTableElement* ConstMethod::exception_table_start() const {
   1.223 +  u2* addr = exception_table_length_addr();
   1.224 +  u2 length = *addr;
   1.225 +  assert(length > 0, "should only be called if table is present");
   1.226 +  addr -= length * sizeof(ExceptionTableElement) / sizeof(u2);
   1.227 +  return (ExceptionTableElement*)addr;
   1.228 +}
   1.229 +
   1.230 +
   1.231 +// Printing
   1.232 +
   1.233 +void ConstMethod::print_on(outputStream* st) const {
   1.234 +  ResourceMark rm;
   1.235 +  assert(is_constMethod(), "must be constMethod");
   1.236 +  st->print_cr(internal_name());
   1.237 +  st->print(" - method:       " INTPTR_FORMAT " ", (address)method());
   1.238 +  method()->print_value_on(st); st->cr();
   1.239 +  if (has_stackmap_table()) {
   1.240 +    st->print(" - stackmap data:       ");
   1.241 +    stackmap_data()->print_value_on(st);
   1.242 +    st->cr();
   1.243 +  }
   1.244 +}
   1.245 +
   1.246 +// Short version of printing ConstMethod* - just print the name of the
   1.247 +// method it belongs to.
   1.248 +void ConstMethod::print_value_on(outputStream* st) const {
   1.249 +  assert(is_constMethod(), "must be constMethod");
   1.250 +  st->print(" const part of method " );
   1.251 +  method()->print_value_on(st);
   1.252 +}
   1.253 +
   1.254 +
   1.255 +// Verification
   1.256 +
   1.257 +void ConstMethod::verify_on(outputStream* st) {
   1.258 +  guarantee(is_constMethod(), "object must be constMethod");
   1.259 +  guarantee(is_metadata(), err_msg("Should be metadata " PTR_FORMAT, this));
   1.260 +
   1.261 +  // Verification can occur during oop construction before the method or
   1.262 +  // other fields have been initialized.
   1.263 +  guarantee(is_metadata(), err_msg("Should be metadata " PTR_FORMAT, this));
   1.264 +  guarantee(method()->is_method(), "should be method");
   1.265 +
   1.266 +  address m_end = (address)((oop*) this + size());
   1.267 +  address compressed_table_start = code_end();
   1.268 +  guarantee(compressed_table_start <= m_end, "invalid method layout");
   1.269 +  address compressed_table_end = compressed_table_start;
   1.270 +  // Verify line number table
   1.271 +  if (has_linenumber_table()) {
   1.272 +    CompressedLineNumberReadStream stream(compressed_linenumber_table());
   1.273 +    while (stream.read_pair()) {
   1.274 +      guarantee(stream.bci() >= 0 && stream.bci() <= code_size(), "invalid bci in line number table");
   1.275 +    }
   1.276 +    compressed_table_end += stream.position();
   1.277 +  }
   1.278 +  guarantee(compressed_table_end <= m_end, "invalid method layout");
   1.279 +  // Verify checked exceptions, exception table and local variable tables
   1.280 +  if (has_checked_exceptions()) {
   1.281 +    u2* addr = checked_exceptions_length_addr();
   1.282 +    guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
   1.283 +  }
   1.284 +  if (has_exception_handler()) {
   1.285 +    u2* addr = exception_table_length_addr();
   1.286 +     guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
   1.287 +  }
   1.288 +  if (has_localvariable_table()) {
   1.289 +    u2* addr = localvariable_table_length_addr();
   1.290 +    guarantee(*addr > 0 && (address) addr >= compressed_table_end && (address) addr < m_end, "invalid method layout");
   1.291 +  }
   1.292 +  // Check compressed_table_end relative to uncompressed_table_start
   1.293 +  u2* uncompressed_table_start;
   1.294 +  if (has_localvariable_table()) {
   1.295 +    uncompressed_table_start = (u2*) localvariable_table_start();
   1.296 +  } else if (has_exception_handler()) {
   1.297 +    uncompressed_table_start = (u2*) exception_table_start();
   1.298 +  } else if (has_checked_exceptions()) {
   1.299 +      uncompressed_table_start = (u2*) checked_exceptions_start();
   1.300 +  } else {
   1.301 +      uncompressed_table_start = (u2*) m_end;
   1.302 +  }
   1.303 +  int gap = (intptr_t) uncompressed_table_start - (intptr_t) compressed_table_end;
   1.304 +  int max_gap = align_object_size(1)*BytesPerWord;
   1.305 +  guarantee(gap >= 0 && gap < max_gap, "invalid method layout");
   1.306 +}

mercurial