1 /*
   2  * Copyright 1999-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 // ciField
  26 //
  27 // This class represents the result of a field lookup in the VM.
  28 // The lookup may not succeed, in which case the information in
  29 // the ciField will be incomplete.
  30 class ciField : public ResourceObj {
  31   CI_PACKAGE_ACCESS
  32   friend class ciEnv;
  33   friend class ciInstanceKlass;
  34   friend class NonStaticFieldFiller;
  35 
  36 private:
  37   ciFlags          _flags;
  38   ciInstanceKlass* _holder;
  39   ciSymbol*        _name;
  40   ciSymbol*        _signature;
  41   ciType*          _type;
  42   int              _offset;
  43   bool             _is_constant;
  44   ciInstanceKlass* _known_to_link_with;
  45   ciConstant       _constant_value;
  46 
  47   // Used for will_link
  48   int              _cp_index;
  49 
  50   ciType* compute_type();
  51   ciType* compute_type_impl();
  52 
  53   ciField(ciInstanceKlass* klass, int index);
  54   ciField(fieldDescriptor* fd);
  55 
  56   // shared constructor code
  57   void initialize_from(fieldDescriptor* fd);
  58 
  59   // The implementation of the print method.
  60   void print_impl(outputStream* st);
  61 
  62 public:
  63   ciFlags flags() { return _flags; }
  64 
  65   // Of which klass is this field a member?
  66   //
  67   // Usage note: the declared holder of a field is the class
  68   // referenced by name in the bytecodes.  The canonical holder
  69   // is the most general class which holds the field.  This
  70   // method returns the canonical holder.  The declared holder
  71   // can be accessed via a method in ciBytecodeStream.
  72   //
  73   // Ex.
  74   //     class A {
  75   //       public int f = 7;
  76   //     }
  77   //     class B extends A {
  78   //       public void test() {
  79   //         System.out.println(f);
  80   //       }
  81   //     }
  82   //
  83   //   A java compiler is permitted to compile the access to
  84   //   field f as:
  85   //
  86   //     getfield B.f
  87   //
  88   //   In that case the declared holder of f would be B and
  89   //   the canonical holder of f would be A.
  90   ciInstanceKlass* holder() { return _holder; }
  91 
  92   // Name of this field?
  93   ciSymbol* name() { return _name; }
  94 
  95   // Signature of this field?
  96   ciSymbol* signature() { return _signature; }
  97 
  98   // Of what type is this field?
  99   ciType* type() { return (_type == NULL) ? compute_type() : _type; }
 100 
 101   // How is this field actually stored in memory?
 102   BasicType layout_type() { return type2field[(_type == NULL) ? T_OBJECT : _type->basic_type()]; }
 103 
 104   // How big is this field in memory?
 105   int size_in_bytes() { return type2aelembytes(layout_type()); }
 106 
 107   // What is the offset of this field?
 108   int offset() {
 109     assert(_offset >= 1, "illegal call to offset()");
 110     return _offset;
 111   }
 112 
 113   // Same question, explicit units.  (Fields are aligned to the byte level.)
 114   int offset_in_bytes() {
 115     return offset();
 116   }
 117 
 118   // Is this field shared?
 119   bool is_shared() {
 120     // non-static fields of shared holders are cached
 121     return _holder->is_shared() && !is_static();
 122   }
 123 
 124   // Is this field a constant?
 125   //
 126   // Clarification: A field is considered constant if:
 127   //   1. The field is both static and final
 128   //   2. The canonical holder of the field has undergone
 129   //      static initialization.
 130   //   3. If the field is an object or array, then the oop
 131   //      in question is allocated in perm space.
 132   //   4. The field is not one of the special static/final
 133   //      non-constant fields.  These are java.lang.System.in
 134   //      and java.lang.System.out.  Abomination.
 135   //
 136   // Note: the check for case 4 is not yet implemented.
 137   bool is_constant() { return _is_constant; }
 138 
 139   // Get the constant value of this field.
 140   ciConstant constant_value() {
 141     assert(is_static() && is_constant(), "illegal call to constant_value()");
 142     return _constant_value;
 143   }
 144 
 145   // Get the constant value of non-static final field in the given
 146   // object.
 147   ciConstant constant_value_of(ciObject* object) {
 148     assert(!is_static() && is_constant(), "only if field is non-static constant");
 149     assert(object->is_instance(), "must be instance");
 150     return object->as_instance()->field_value(this);
 151   }
 152 
 153   // Check for link time errors.  Accessing a field from a
 154   // certain class via a certain bytecode may or may not be legal.
 155   // This call checks to see if an exception may be raised by
 156   // an access of this field.
 157   //
 158   // Usage note: if the same field is accessed multiple times
 159   // in the same compilation, will_link will need to be checked
 160   // at each point of access.
 161   bool will_link(ciInstanceKlass* accessing_klass,
 162                  Bytecodes::Code bc);
 163 
 164   // Java access flags
 165   bool is_public      () { return flags().is_public(); }
 166   bool is_private     () { return flags().is_private(); }
 167   bool is_protected   () { return flags().is_protected(); }
 168   bool is_static      () { return flags().is_static(); }
 169   bool is_final       () { return flags().is_final(); }
 170   bool is_volatile    () { return flags().is_volatile(); }
 171   bool is_transient   () { return flags().is_transient(); }
 172 
 173   // Debugging output
 174   void print();
 175   void print_name_on(outputStream* st);
 176 };