1 /*
   2  * Copyright (c) 1998, 2005, 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 // A HandlerTableEntry describes an individual entry of a subtable
  26 // of ExceptionHandlerTable. An entry consists of a pair(bci, pco),
  27 // where bci is the exception handler bci, and pco is the pc offset
  28 // relative to the nmethod code start for the compiled exception
  29 // handler corresponding to the (interpreted) exception handler
  30 // starting at bci.
  31 //
  32 // The first HandlerTableEntry of each subtable holds the length
  33 // and catch_pco for the subtable (the length is the number of
  34 // subtable entries w/o header).
  35 
  36 class HandlerTableEntry {
  37  private:
  38   int _bci;
  39   int _pco;
  40   int _scope_depth;
  41 
  42  public:
  43   HandlerTableEntry(int bci, int pco, int scope_depth) {
  44     assert( 0 <= pco, "pco must be positive");
  45     assert( 0 <= scope_depth, "scope_depth must be positive");
  46     _bci = bci;
  47     _pco = pco;
  48     _scope_depth = scope_depth;
  49   }
  50 
  51   int len() const { return _bci; } // for entry at subtable begin
  52   int bci() const { return _bci; }
  53   int pco() const { return _pco; }
  54   int scope_depth() const { return _scope_depth; }
  55 };
  56 
  57 
  58 // An ExceptionHandlerTable is an abstraction over a list of subtables
  59 // of exception handlers for CatchNodes. Each subtable has a one-entry
  60 // header holding length and catch_pco of the subtable, followed
  61 // by 'length' entries for each exception handler that can be reached
  62 // from the corresponding CatchNode. The catch_pco is the pc offset of
  63 // the CatchNode in the corresponding nmethod. Empty subtables are dis-
  64 // carded.
  65 //
  66 // Structure of the table:
  67 //
  68 // table    = { subtable }.
  69 // subtable = header entry { entry }.
  70 // header   = a pair (number of subtable entries, catch pc offset, [unused])
  71 // entry    = a pair (handler bci, handler pc offset, scope depth)
  72 //
  73 // An ExceptionHandlerTable can be created from scratch, in which case
  74 // it is possible to add subtables. It can also be created from an
  75 // nmethod (for lookup purposes) in which case the table cannot be
  76 // modified.
  77 
  78 class nmethod;
  79 class ExceptionHandlerTable VALUE_OBJ_CLASS_SPEC {
  80  private:
  81   HandlerTableEntry* _table;    // the table
  82   int                _length;   // the current length of the table
  83   int                _size;     // the number of allocated entries
  84   ReallocMark        _nesting;  // assertion check for reallocations
  85 
  86   // add the entry & grow the table if needed
  87   void add_entry(HandlerTableEntry entry);
  88   HandlerTableEntry* subtable_for(int catch_pco) const;
  89 
  90  public:
  91   // (compile-time) construction within compiler
  92   ExceptionHandlerTable(int initial_size = 8);
  93 
  94   // (run-time) construction from nmethod
  95   ExceptionHandlerTable(const nmethod* nm);
  96 
  97   // (compile-time) add entries
  98   void add_subtable(
  99     int                 catch_pco, // the pc offset for the CatchNode
 100     GrowableArray<intptr_t>* handler_bcis, // the exception handler entry point bcis
 101     GrowableArray<intptr_t>* scope_depths_from_top_scope,
 102                                            // if representing exception handlers in multiple
 103                                            // inlined scopes, indicates which scope relative to
 104                                            // the youngest/innermost one in which we are performing
 105                                            // the lookup; zero (or null GrowableArray) indicates
 106                                            // innermost scope
 107     GrowableArray<intptr_t>* handler_pcos  // pc offsets for the compiled handlers
 108   );
 109 
 110   // nmethod support
 111   int  size_in_bytes() const { return round_to(_length * sizeof(HandlerTableEntry), oopSize); }
 112   void copy_to(nmethod* nm);
 113 
 114   // lookup
 115   HandlerTableEntry* entry_for(int catch_pco, int handler_bci, int scope_depth) const;
 116 
 117   // debugging
 118   void print_subtable(HandlerTableEntry* t) const;
 119   void print() const;
 120   void print_subtable_for(int catch_pco) const;
 121 };
 122 
 123 
 124 // ----------------------------------------------------------------------------
 125 // Implicit null exception tables.  Maps an exception PC offset to a
 126 // continuation PC offset.  During construction it's a variable sized
 127 // array with a max size and current length.  When stored inside an
 128 // nmethod a zero length table takes no space.  This is detected by
 129 // nul_chk_table_size() == 0.  Otherwise the table has a length word
 130 // followed by pairs of <excp-offset, const-offset>.
 131 
 132 // Use 32-bit representation for offsets
 133 typedef  uint              implicit_null_entry;
 134 
 135 class ImplicitExceptionTable VALUE_OBJ_CLASS_SPEC {
 136   uint _size;
 137   uint _len;
 138   implicit_null_entry *_data;
 139   implicit_null_entry *adr( uint idx ) const { return &_data[2*idx]; }
 140   ReallocMark          _nesting;  // assertion check for reallocations
 141 public:
 142   ImplicitExceptionTable( ) :  _data(0), _size(0), _len(0) { }
 143   // (run-time) construction from nmethod
 144   ImplicitExceptionTable( const nmethod *nm );
 145 
 146   void set_size( uint size );
 147   void append( uint exec_off, uint cont_off );
 148   uint at( uint exec_off ) const;
 149 
 150   uint len() const { return _len; }
 151   int size_in_bytes() const { return len() == 0 ? 0 : ((2 * len() + 1) * sizeof(implicit_null_entry)); }
 152 
 153   void copy_to(nmethod* nm);
 154   void print(address base) const;
 155   void verify(nmethod *nm) const;
 156 };