twisti@2047: /* stefank@2314: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. twisti@2047: * Copyright 2008, 2009 Red Hat, Inc. twisti@2047: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. twisti@2047: * twisti@2047: * This code is free software; you can redistribute it and/or modify it twisti@2047: * under the terms of the GNU General Public License version 2 only, as twisti@2047: * published by the Free Software Foundation. twisti@2047: * twisti@2047: * This code is distributed in the hope that it will be useful, but WITHOUT twisti@2047: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or twisti@2047: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License twisti@2047: * version 2 for more details (a copy is included in the LICENSE file that twisti@2047: * accompanied this code). twisti@2047: * twisti@2047: * You should have received a copy of the GNU General Public License version twisti@2047: * 2 along with this work; if not, write to the Free Software Foundation, twisti@2047: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. twisti@2047: * twisti@2047: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA twisti@2047: * or visit www.oracle.com if you need additional information or have any twisti@2047: * questions. twisti@2047: * twisti@2047: */ twisti@2047: stefank@2314: #ifndef SHARE_VM_SHARK_SHARKBLOCK_HPP stefank@2314: #define SHARE_VM_SHARK_SHARKBLOCK_HPP stefank@2314: stefank@2314: #include "ci/ciMethod.hpp" stefank@2314: #include "ci/ciStreams.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "shark/llvmHeaders.hpp" stefank@2314: #include "shark/sharkBuilder.hpp" stefank@2314: #include "shark/sharkConstant.hpp" stefank@2314: #include "shark/sharkInvariants.hpp" stefank@2314: #include "shark/sharkState.hpp" stefank@2314: #include "shark/sharkValue.hpp" stefank@2314: #include "utilities/debug.hpp" stefank@2314: twisti@2047: class SharkState; twisti@2047: twisti@2047: class SharkBlock : public SharkTargetInvariants { twisti@2047: protected: twisti@2047: SharkBlock(const SharkTargetInvariants* parent) twisti@2047: : SharkTargetInvariants(parent), twisti@2047: _iter(target()), twisti@2047: _current_state(NULL) {} twisti@2047: twisti@2047: SharkBlock(const SharkCompileInvariants* parent, ciMethod* target) twisti@2047: : SharkTargetInvariants(parent, target), twisti@2047: _iter(target), twisti@2047: _current_state(NULL) {} twisti@2047: twisti@2047: private: twisti@2047: ciBytecodeStream _iter; twisti@2047: SharkState* _current_state; twisti@2047: twisti@2047: public: twisti@2047: ciBytecodeStream* iter() { twisti@2047: return &_iter; twisti@2047: } twisti@2047: Bytecodes::Code bc() { twisti@2047: return iter()->cur_bc(); twisti@2047: } twisti@2047: int bci() { twisti@2047: return iter()->cur_bci(); twisti@2047: } twisti@2047: twisti@2047: // Entry state twisti@2047: protected: twisti@2047: virtual SharkState* entry_state(); twisti@2047: twisti@2047: // Current state twisti@2047: private: twisti@2047: SharkState* initial_current_state(); twisti@2047: twisti@2047: public: twisti@2047: SharkState* current_state() { twisti@2047: if (_current_state == NULL) twisti@2047: set_current_state(initial_current_state()); twisti@2047: return _current_state; twisti@2047: } twisti@2047: twisti@2047: protected: twisti@2047: void set_current_state(SharkState* current_state) { twisti@2047: _current_state = current_state; twisti@2047: } twisti@2047: twisti@2047: // Local variables twisti@2047: protected: twisti@2047: SharkValue* local(int index) { twisti@2047: SharkValue *value = current_state()->local(index); twisti@2047: assert(value != NULL, "shouldn't be"); twisti@2047: assert(value->is_one_word() || twisti@2047: (index + 1 < max_locals() && twisti@2047: current_state()->local(index + 1) == NULL), "should be"); twisti@2047: return value; twisti@2047: } twisti@2047: void set_local(int index, SharkValue* value) { twisti@2047: assert(value != NULL, "shouldn't be"); twisti@2047: current_state()->set_local(index, value); twisti@2047: if (value->is_two_word()) twisti@2047: current_state()->set_local(index + 1, NULL); twisti@2047: } twisti@2047: twisti@2047: // Expression stack (raw) twisti@2047: protected: twisti@2047: void xpush(SharkValue* value) { twisti@2047: current_state()->push(value); twisti@2047: } twisti@2047: SharkValue* xpop() { twisti@2047: return current_state()->pop(); twisti@2047: } twisti@2047: SharkValue* xstack(int slot) { twisti@2047: SharkValue *value = current_state()->stack(slot); twisti@2047: assert(value != NULL, "shouldn't be"); twisti@2047: assert(value->is_one_word() || twisti@2047: (slot > 0 && twisti@2047: current_state()->stack(slot - 1) == NULL), "should be"); twisti@2047: return value; twisti@2047: } twisti@2047: int xstack_depth() { twisti@2047: return current_state()->stack_depth(); twisti@2047: } twisti@2047: twisti@2047: // Expression stack (cooked) twisti@2047: protected: twisti@2047: void push(SharkValue* value) { twisti@2047: assert(value != NULL, "shouldn't be"); twisti@2047: xpush(value); twisti@2047: if (value->is_two_word()) twisti@2047: xpush(NULL); twisti@2047: } twisti@2047: SharkValue* pop() { twisti@2047: int size = current_state()->stack(0) == NULL ? 2 : 1; twisti@2047: if (size == 2) twisti@2047: xpop(); twisti@2047: SharkValue *value = xpop(); twisti@2047: assert(value && value->size() == size, "should be"); twisti@2047: return value; twisti@2047: } twisti@2047: SharkValue* pop_result(BasicType type) { twisti@2047: SharkValue *result = pop(); twisti@2047: twisti@2047: #ifdef ASSERT twisti@2047: switch (result->basic_type()) { twisti@2047: case T_BOOLEAN: twisti@2047: case T_BYTE: twisti@2047: case T_CHAR: twisti@2047: case T_SHORT: twisti@2047: assert(type == T_INT, "type mismatch"); twisti@2047: break; twisti@2047: twisti@2047: case T_ARRAY: twisti@2047: assert(type == T_OBJECT, "type mismatch"); twisti@2047: break; twisti@2047: twisti@2047: default: twisti@2047: assert(result->basic_type() == type, "type mismatch"); twisti@2047: } twisti@2047: #endif // ASSERT twisti@2047: twisti@2047: return result; twisti@2047: } twisti@2047: twisti@2047: // Code generation twisti@2047: public: twisti@2047: virtual void emit_IR(); twisti@2047: twisti@2047: protected: twisti@2047: void parse_bytecode(int start, int limit); twisti@2047: twisti@2047: // Helpers twisti@2047: protected: twisti@2047: virtual void do_zero_check(SharkValue* value); twisti@2047: twisti@2047: // Zero checking twisti@2047: protected: twisti@2047: void check_null(SharkValue* object) { twisti@2047: zero_check(object); twisti@2047: } twisti@2047: void check_divide_by_zero(SharkValue* value) { twisti@2047: zero_check(value); twisti@2047: } twisti@2047: private: twisti@2047: void zero_check(SharkValue* value) { twisti@2047: if (!value->zero_checked()) twisti@2047: do_zero_check(value); twisti@2047: } twisti@2047: twisti@2047: // Safepoints twisti@2047: protected: twisti@2047: virtual void maybe_add_backedge_safepoint(); twisti@2047: twisti@2047: // Traps twisti@2047: protected: twisti@2047: virtual bool has_trap(); twisti@2047: virtual int trap_request(); twisti@2047: virtual int trap_bci(); twisti@2047: virtual void do_trap(int trap_request); twisti@2047: twisti@2047: // arraylength twisti@2047: protected: twisti@2047: virtual void do_arraylength(); twisti@2047: twisti@2047: // *aload and *astore twisti@2047: protected: twisti@2047: virtual void do_aload(BasicType basic_type); twisti@2047: virtual void do_astore(BasicType basic_type); twisti@2047: twisti@2047: // *div and *rem twisti@2047: private: twisti@2047: void do_idiv() { twisti@2047: do_div_or_rem(false, false); twisti@2047: } twisti@2047: void do_irem() { twisti@2047: do_div_or_rem(false, true); twisti@2047: } twisti@2047: void do_ldiv() { twisti@2047: do_div_or_rem(true, false); twisti@2047: } twisti@2047: void do_lrem() { twisti@2047: do_div_or_rem(true, true); twisti@2047: } twisti@2047: void do_div_or_rem(bool is_long, bool is_rem); twisti@2047: twisti@2047: // get* and put* twisti@2047: private: twisti@2047: void do_getstatic() { twisti@2047: do_field_access(true, false); twisti@2047: } twisti@2047: void do_getfield() { twisti@2047: do_field_access(true, true); twisti@2047: } twisti@2047: void do_putstatic() { twisti@2047: do_field_access(false, false); twisti@2047: } twisti@2047: void do_putfield() { twisti@2047: do_field_access(false, true); twisti@2047: } twisti@2047: void do_field_access(bool is_get, bool is_field); twisti@2047: twisti@2047: // lcmp and [fd]cmp[lg] twisti@2047: private: twisti@2047: void do_lcmp(); twisti@2047: void do_fcmp(bool is_double, bool unordered_is_greater); twisti@2047: twisti@2047: // *return and athrow twisti@2047: protected: twisti@2047: virtual void do_return(BasicType type); twisti@2047: virtual void do_athrow(); twisti@2047: twisti@2047: // goto* twisti@2047: protected: twisti@2047: virtual void do_goto(); twisti@2047: twisti@2047: // jsr* and ret twisti@2047: protected: twisti@2047: virtual void do_jsr(); twisti@2047: virtual void do_ret(); twisti@2047: twisti@2047: // if* twisti@2047: protected: twisti@2047: virtual void do_if(llvm::ICmpInst::Predicate p, SharkValue* b, SharkValue* a); twisti@2047: twisti@2047: // *switch twisti@2047: protected: twisti@2047: int switch_default_dest(); twisti@2047: int switch_table_length(); twisti@2047: int switch_key(int i); twisti@2047: int switch_dest(int i); twisti@2047: twisti@2047: virtual void do_switch(); twisti@2047: twisti@2047: // invoke* twisti@2047: protected: twisti@2047: virtual void do_call(); twisti@2047: twisti@2047: // checkcast and instanceof twisti@2047: protected: twisti@2047: virtual void do_instance_check(); twisti@2047: virtual bool maybe_do_instanceof_if(); twisti@2047: twisti@2047: // new and *newarray twisti@2047: protected: twisti@2047: virtual void do_new(); twisti@2047: virtual void do_newarray(); twisti@2047: virtual void do_anewarray(); twisti@2047: virtual void do_multianewarray(); twisti@2047: twisti@2047: // monitorenter and monitorexit twisti@2047: protected: twisti@2047: virtual void do_monitorenter(); twisti@2047: virtual void do_monitorexit(); twisti@2047: }; stefank@2314: stefank@2314: #endif // SHARE_VM_SHARK_SHARKBLOCK_HPP