duke@435: /* duke@435: * Copyright 2003-2006 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // A StackMapFrame represents one frame in the stack map attribute. duke@435: duke@435: enum { duke@435: FLAG_THIS_UNINIT = 0x01 duke@435: }; duke@435: duke@435: class StackMapFrame : public ResourceObj { duke@435: private: duke@435: int32_t _offset; duke@435: duke@435: // See comment in StackMapTable about _frame_count about why these duke@435: // fields are int32_t instead of u2. duke@435: int32_t _locals_size; // number of valid type elements in _locals duke@435: int32_t _stack_size; // number of valid type elements in _stack duke@435: duke@435: int32_t _max_locals; duke@435: int32_t _max_stack; duke@435: duke@435: u1 _flags; duke@435: VerificationType* _locals; // local variable type array duke@435: VerificationType* _stack; // operand stack type array duke@435: duke@435: ClassVerifier* _verifier; // the verifier verifying this method duke@435: duke@435: public: duke@435: // constructors duke@435: duke@435: // This constructor is used by the type checker to allocate frames duke@435: // in type state, which have _max_locals and _max_stack array elements duke@435: // in _locals and _stack. duke@435: StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* verifier); duke@435: duke@435: // This constructor is used to initialize stackmap frames in stackmap table, duke@435: // which have _locals_size and _stack_size array elements in _locals and _stack. duke@435: StackMapFrame(int32_t offset, duke@435: u1 flags, duke@435: u2 locals_size, duke@435: u2 stack_size, duke@435: u2 max_locals, duke@435: u2 max_stack, duke@435: VerificationType* locals, duke@435: VerificationType* stack, duke@435: ClassVerifier* v) : _offset(offset), _flags(flags), duke@435: _locals_size(locals_size), duke@435: _stack_size(stack_size), duke@435: _max_locals(max_locals), duke@435: _max_stack(max_stack), duke@435: _locals(locals), _stack(stack), duke@435: _verifier(v) { } duke@435: duke@435: inline void set_offset(int32_t offset) { _offset = offset; } duke@435: inline void set_verifier(ClassVerifier* v) { _verifier = v; } duke@435: inline void set_flags(u1 flags) { _flags = flags; } duke@435: inline void set_locals_size(u2 locals_size) { _locals_size = locals_size; } duke@435: inline void set_stack_size(u2 stack_size) { _stack_size = stack_size; } duke@435: inline void clear_stack() { _stack_size = 0; } duke@435: inline int32_t offset() const { return _offset; } duke@435: inline ClassVerifier* verifier() const { return _verifier; } duke@435: inline u1 flags() const { return _flags; } duke@435: inline int32_t locals_size() const { return _locals_size; } duke@435: inline VerificationType* locals() const { return _locals; } duke@435: inline int32_t stack_size() const { return _stack_size; } duke@435: inline VerificationType* stack() const { return _stack; } duke@435: inline int32_t max_locals() const { return _max_locals; } duke@435: inline int32_t max_stack() const { return _max_stack; } duke@435: inline bool flag_this_uninit() const { return _flags & FLAG_THIS_UNINIT; } duke@435: duke@435: // Set locals and stack types to bogus duke@435: inline void reset() { duke@435: int32_t i; duke@435: for (i = 0; i < _max_locals; i++) { duke@435: _locals[i] = VerificationType::bogus_type(); duke@435: } duke@435: for (i = 0; i < _max_stack; i++) { duke@435: _stack[i] = VerificationType::bogus_type(); duke@435: } duke@435: } duke@435: duke@435: // Return a StackMapFrame with the same local variable array and empty stack. duke@435: // Stack array is allocate with unused one element. duke@435: StackMapFrame* frame_in_exception_handler(u1 flags); duke@435: duke@435: // Set local variable type array based on m's signature. duke@435: VerificationType set_locals_from_arg( duke@435: const methodHandle m, VerificationType thisKlass, TRAPS); duke@435: duke@435: // Search local variable type array and stack type array. duke@435: // Return true if an uninitialized object is found. duke@435: bool has_new_object() const; duke@435: duke@435: // Search local variable type array and stack type array. duke@435: // Set every element with type of old_object to new_object. duke@435: void initialize_object( duke@435: VerificationType old_object, VerificationType new_object); duke@435: duke@435: // Copy local variable type array in src into this local variable type array. duke@435: void copy_locals(const StackMapFrame* src); duke@435: duke@435: // Copy stack type array in src into this stack type array. duke@435: void copy_stack(const StackMapFrame* src); duke@435: duke@435: // Return true if this stack map frame is assignable to target. duke@435: bool is_assignable_to(const StackMapFrame* target, TRAPS) const; duke@435: duke@435: // Push type into stack type array. duke@435: inline void push_stack(VerificationType type, TRAPS) { duke@435: assert(!type.is_check(), "Must be a real type"); duke@435: if (_stack_size >= _max_stack) { duke@435: verifier()->verify_error(_offset, "Operand stack overflow"); duke@435: return; duke@435: } duke@435: _stack[_stack_size++] = type; duke@435: } duke@435: duke@435: inline void push_stack_2( duke@435: VerificationType type1, VerificationType type2, TRAPS) { duke@435: assert(type1.is_long() || type1.is_double(), "must be long/double"); duke@435: assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); duke@435: if (_stack_size >= _max_stack - 1) { duke@435: verifier()->verify_error(_offset, "Operand stack overflow"); duke@435: return; duke@435: } duke@435: _stack[_stack_size++] = type1; duke@435: _stack[_stack_size++] = type2; duke@435: } duke@435: duke@435: // Pop and return the top type on stack without verifying. duke@435: inline VerificationType pop_stack(TRAPS) { duke@435: if (_stack_size <= 0) { duke@435: verifier()->verify_error(_offset, "Operand stack underflow"); duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: // Put bogus type to indicate it's no longer valid. duke@435: // Added to make it consistent with the other pop_stack method. duke@435: VerificationType top = _stack[--_stack_size]; duke@435: NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); ) duke@435: return top; duke@435: } duke@435: duke@435: // Pop and return the top type on stack type array after verifying it duke@435: // is assignable to type. duke@435: inline VerificationType pop_stack(VerificationType type, TRAPS) { duke@435: if (_stack_size != 0) { duke@435: VerificationType top = _stack[_stack_size - 1]; duke@435: bool subtype = type.is_assignable_from( duke@435: top, verifier()->current_class(), duke@435: CHECK_(VerificationType::bogus_type())); duke@435: if (subtype) { duke@435: _stack_size --; duke@435: NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); ) duke@435: return top; duke@435: } duke@435: } duke@435: return pop_stack_ex(type, THREAD); duke@435: } duke@435: duke@435: inline void pop_stack_2( duke@435: VerificationType type1, VerificationType type2, TRAPS) { duke@435: assert(type1.is_long2() || type1.is_double2(), "must be long/double"); duke@435: assert(type2.is_long() || type2.is_double(), "must be long/double_2"); duke@435: if (_stack_size >= 2) { duke@435: VerificationType top1 = _stack[_stack_size - 1]; duke@435: bool subtype1 = type1.is_assignable_from( duke@435: top1, verifier()->current_class(), CHECK); duke@435: VerificationType top2 = _stack[_stack_size - 2]; duke@435: bool subtype2 = type2.is_assignable_from( duke@435: top2, verifier()->current_class(), CHECK); duke@435: if (subtype1 && subtype2) { duke@435: _stack_size -= 2; duke@435: NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); ) duke@435: NOT_PRODUCT( _stack[_stack_size+1] = VerificationType::bogus_type(); ) duke@435: return; duke@435: } duke@435: } duke@435: pop_stack_ex(type1, THREAD); duke@435: pop_stack_ex(type2, THREAD); duke@435: } duke@435: duke@435: // Uncommon case that throws exceptions. duke@435: VerificationType pop_stack_ex(VerificationType type, TRAPS); duke@435: duke@435: // Return the type at index in local variable array after verifying duke@435: // it is assignable to type. duke@435: VerificationType get_local(int32_t index, VerificationType type, TRAPS); duke@435: // For long/double. duke@435: void get_local_2( duke@435: int32_t index, VerificationType type1, VerificationType type2, TRAPS); duke@435: duke@435: // Set element at index in local variable array to type. duke@435: void set_local(int32_t index, VerificationType type, TRAPS); duke@435: // For long/double. duke@435: void set_local_2( duke@435: int32_t index, VerificationType type1, VerificationType type2, TRAPS); duke@435: duke@435: // Private auxiliary method used only in is_assignable_to(StackMapFrame). duke@435: // Returns true if src is assignable to target. duke@435: bool is_assignable_to( duke@435: VerificationType* src, VerificationType* target, int32_t len, TRAPS) const; duke@435: duke@435: // Debugging duke@435: void print() const PRODUCT_RETURN; duke@435: };