duke@435: /* trims@1907: * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: // This code has been converted from the 1.1E java virtual machine duke@435: // Thanks to the JavaTopics group for using the code duke@435: duke@435: class ChangeItem; duke@435: duke@435: // Callback object for code relocations duke@435: class RelocatorListener : public StackObj { duke@435: public: duke@435: RelocatorListener() {}; duke@435: virtual void relocated(int bci, int delta, int new_method_size) = 0; duke@435: }; duke@435: duke@435: duke@435: class Relocator : public ResourceObj { duke@435: public: duke@435: Relocator(methodHandle method, RelocatorListener* listener); duke@435: methodHandle insert_space_at(int bci, int space, u_char inst_buffer[], TRAPS); duke@435: duke@435: // Callbacks from ChangeItem's duke@435: bool handle_code_changes(); duke@435: bool handle_widen (int bci, int new_ilen, u_char inst_buffer[]); // handles general instructions duke@435: void push_jump_widen (int bci, int delta, int new_delta); // pushes jumps duke@435: bool handle_jump_widen (int bci, int delta); // handles jumps duke@435: bool handle_switch_pad (int bci, int old_pad, bool is_lookup_switch); // handles table and lookup switches duke@435: duke@435: private: duke@435: unsigned char* _code_array; duke@435: int _code_array_length; duke@435: int _code_length; duke@435: unsigned char* _compressed_line_number_table; duke@435: int _compressed_line_number_table_size; duke@435: methodHandle _method; duke@435: u_char _overwrite[3]; // stores overwritten bytes for shrunken instructions duke@435: duke@435: GrowableArray* _changes; duke@435: duke@435: unsigned char* code_array() const { return _code_array; } duke@435: void set_code_array(unsigned char* array) { _code_array = array; } duke@435: duke@435: int code_length() const { return _code_length; } duke@435: void set_code_length(int length) { _code_length = length; } duke@435: duke@435: int code_array_length() const { return _code_array_length; } duke@435: void set_code_array_length(int length) { _code_array_length = length; } duke@435: duke@435: unsigned char* compressed_line_number_table() const { return _compressed_line_number_table; } duke@435: void set_compressed_line_number_table(unsigned char* table) { _compressed_line_number_table = table; } duke@435: duke@435: int compressed_line_number_table_size() const { return _compressed_line_number_table_size; } duke@435: void set_compressed_line_number_table_size(int size) { _compressed_line_number_table_size = size; } duke@435: duke@435: methodHandle method() const { return _method; } duke@435: void set_method(methodHandle method) { _method = method; } duke@435: duke@435: // This will return a raw bytecode, which is possibly rewritten. duke@435: Bytecodes::Code code_at(int bci) const { return (Bytecodes::Code) code_array()[bci]; } duke@435: void code_at_put(int bci, Bytecodes::Code code) { code_array()[bci] = (char) code; } duke@435: duke@435: // get and set signed integers in the code_array duke@435: inline int int_at(int bci) const { return Bytes::get_Java_u4(&code_array()[bci]); } duke@435: inline void int_at_put(int bci, int value) { Bytes::put_Java_u4(&code_array()[bci], value); } duke@435: duke@435: // get and set signed shorts in the code_array duke@435: inline short short_at(int bci) const { return (short)Bytes::get_Java_u2(&code_array()[bci]); } duke@435: inline void short_at_put(int bci, short value) { Bytes::put_Java_u2((address) &code_array()[bci], value); } duke@435: duke@435: // get the address of in the code_array duke@435: inline char* addr_at(int bci) const { return (char*) &code_array()[bci]; } duke@435: duke@435: int instruction_length_at(int bci) { return Bytecodes::length_at(code_array() + bci); } duke@435: duke@435: // Helper methods duke@435: int align(int n) const { return (n+3) & ~3; } duke@435: int code_slop_pct() const { return 25; } duke@435: bool is_opcode_lookupswitch(Bytecodes::Code bc); duke@435: duke@435: // basic relocation methods duke@435: bool relocate_code (int bci, int ilen, int delta); duke@435: void change_jumps (int break_bci, int delta); duke@435: void change_jump (int bci, int offset, bool is_short, int break_bci, int delta); duke@435: void adjust_exception_table(int bci, int delta); duke@435: void adjust_line_no_table (int bci, int delta); duke@435: void adjust_local_var_table(int bci, int delta); duke@435: int get_orig_switch_pad (int bci, bool is_lookup_switch); duke@435: int rc_instr_len (int bci); duke@435: bool expand_code_array (int delta); duke@435: duke@435: // Callback support duke@435: RelocatorListener *_listener; duke@435: void notify(int bci, int delta, int new_code_length) { duke@435: if (_listener != NULL) duke@435: _listener->relocated(bci, delta, new_code_length); duke@435: } duke@435: };