1 /*
   2  * Copyright (c) 1997, 2019, 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 #ifndef SHARE_CODE_LOCATION_HPP
  26 #define SHARE_CODE_LOCATION_HPP
  27 
  28 #include "asm/assembler.hpp"
  29 #include "code/vmreg.hpp"
  30 
  31 class DebugInfoReadStream;
  32 class DebugInfoWriteStream;
  33 
  34 // A Location describes a concrete machine variable location
  35 // (such as integer or floating point register or a stack-held
  36 // variable). Used when generating debug-information for nmethods.
  37 //
  38 // Encoding:
  39 //
  40 // bits (use low bits for best compression):
  41 //  Type:   [3..0]
  42 //  Where:  [4]
  43 //  Offset: [31..5]
  44 
  45 class Location {
  46   friend class VMStructs;
  47  public:
  48   enum Where {
  49     on_stack,
  50     in_register
  51   };
  52 
  53   enum Type {
  54     invalid,                    // Invalid location
  55     normal,                     // Ints, floats, double halves
  56     oop,                        // Oop (please GC me!)
  57     int_in_long,                // Integer held in long register
  58     lng,                        // Long held in one register
  59     float_in_dbl,               // Float held in double register
  60     dbl,                        // Double held in one register
  61     vector,                     // Vector in one register
  62     addr,                       // JSR return address
  63     narrowoop                   // Narrow Oop (please GC me!)
  64   };
  65 
  66 
  67  private:
  68   enum {
  69     TYPE_MASK    = (juint) 0x0F,
  70     TYPE_SHIFT   = 0,
  71     WHERE_MASK   = (juint) 0x10,
  72     WHERE_SHIFT  = 4,
  73     OFFSET_MASK  = (juint) 0xFFFFFFE0,
  74     OFFSET_SHIFT = 5
  75   };
  76 
  77   juint _value;
  78 
  79   // Create a bit-packed Location
  80   Location(Where where_, Type type_, unsigned offset_) {
  81     set(where_, type_, offset_);
  82     assert( where () == where_ , "" );
  83     assert( type  () == type_  , "" );
  84     assert( offset() == offset_, "" );
  85   }
  86 
  87   inline void set(Where where_, Type type_, unsigned offset_) {
  88     _value = (juint) ((where_  << WHERE_SHIFT) |
  89                       (type_   << TYPE_SHIFT)  |
  90                       ((offset_ << OFFSET_SHIFT) & OFFSET_MASK));
  91   }
  92 
  93  public:
  94 
  95   // Stack location Factory.  Offset is 4-byte aligned; remove low bits
  96   static Location new_stk_loc( Type t, int offset ) { return Location(on_stack,t,offset>>LogBytesPerInt); }
  97   // Register location Factory
  98   static Location new_reg_loc( Type t, VMReg reg ) { return Location(in_register, t, reg->value()); }
  99   // Default constructor
 100   Location() { set(on_stack,invalid,0); }
 101 
 102   // Bit field accessors
 103   Where where()  const { return (Where)       ((_value & WHERE_MASK)  >> WHERE_SHIFT);}
 104   Type  type()   const { return (Type)        ((_value & TYPE_MASK)   >> TYPE_SHIFT); }
 105   unsigned offset() const { return (unsigned) ((_value & OFFSET_MASK) >> OFFSET_SHIFT); }
 106 
 107   // Accessors
 108   bool is_register() const    { return where() == in_register; }
 109   bool is_stack() const       { return where() == on_stack;    }
 110 
 111   int stack_offset() const    { assert(where() == on_stack,    "wrong Where"); return offset()<<LogBytesPerInt; }
 112   int register_number() const { assert(where() == in_register, "wrong Where"); return offset()   ; }
 113 
 114   VMReg reg() const { assert(where() == in_register, "wrong Where"); return VMRegImpl::as_VMReg(offset())   ; }
 115 
 116   // Printing
 117   void print_on(outputStream* st) const;
 118 
 119   // Serialization of debugging information
 120   Location(DebugInfoReadStream* stream);
 121   void write_on(DebugInfoWriteStream* stream);
 122 
 123   // check
 124   static bool legal_offset_in_bytes(int offset_in_bytes);
 125 };
 126 
 127 #endif // SHARE_CODE_LOCATION_HPP