klass.hpp

Go to the documentation of this file.
00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.57 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 // A Klass is the the part of the klassOop that provides:
00025 //  1: language level class object (method dictionary etc.)
00026 //  2: provide vm dispatch behavior for the object
00027 // Both functions are combined into one C++ class. The toplevel class "Klass"
00028 // implements purpose 1 whereas all subclasses provide extra virtual functions 
00029 // for purpose 2.
00030 
00031 // One reason for the oop/klass dichotomy in the implementation is
00032 // that we don't want a C++ vtbl pointer in every object.  Thus,
00033 // normal oops don't have any virtual functions.  Instead, they
00034 // forward all "virtual" functions to their klass, which does have
00035 // a vtbl and does the C++ dispatch depending on the object's
00036 // actual type.  (See oop.inline.h for some of the forwarding code.)
00037 // ALL FUNCTIONS IMPLEMENTING THIS DISPATCH ARE PREFIXED WITH "oop_"!
00038 
00039 // Microsoft C++ 2.0 always forces the vtbl at offset 0.
00040 const int vtbl_position = 0; 
00041 
00042 //  Klass layout:
00043 //    [vtbl                 ]
00044 //    [non_indexable_size   ]  
00045 //    [has_untagged_contents]  can be avoided if prototype is stored.
00046 //    [classVars            ]  class variables copied from mixin
00047 //    [methods              ]  customized methods from the mixin
00048 //    [superKlass           ]  super
00049 //    [mixin                ]  the mixin for the class
00050 
00051 class Klass : ValueObj {
00052  protected:
00053   smiOop        _non_indexable_size;
00054   smiOop        _has_untagged_contents;
00055   objArrayOop   _classVars;
00056   objArrayOop   _methods;
00057   klassOop      _superKlass;
00058   mixinOop      _mixin;
00059 
00060  public:
00061   friend klassOop as_klassOop(void* p) { return klassOop(as_memOop(p)); }
00062 
00063   // Returns the enclosing klassOop
00064   klassOop as_klassOop() const {
00065    // see klassOop.hpp for layout.
00066    return (klassOop) (((char*) this) - sizeof(memOopDesc) + Mem_Tag);
00067   }
00068 
00069   smi non_indexable_size() const {
00070      return _non_indexable_size->value();  }
00071   void set_non_indexable_size(smi size) {
00072      _non_indexable_size = as_smiOop(size); }
00073 
00074   bool has_untagged_contents() const  { return _has_untagged_contents == smiOop_one; }
00075   void set_untagged_contents(bool v)  { _has_untagged_contents = v ? smiOop_one : smiOop_zero; }
00076 
00077   objArrayOop classVars()       const { return _classVars;  }
00078   void  set_classVars(objArrayOop c)  { STORE_OOP(&_classVars, c); }
00079 
00080   objArrayOop methods()         const { return _methods;  }
00081   void set_methods(objArrayOop m)     { STORE_OOP(&_methods, m); }
00082  
00083   klassOop superKlass() const         { return _superKlass;  }
00084   void set_superKlass(klassOop super) { STORE_OOP(&_superKlass, super); }
00085 
00086   mixinOop mixin() const              { return _mixin;  }
00087   void set_mixin(mixinOop m)          { STORE_OOP(&_mixin, m); }
00088 
00089   // Tells whether here is a super class
00090   bool has_superKlass() const { return oop(superKlass()) != nilObj; }
00091 
00092  public:  
00093   int            number_of_methods()   const;        // Returns the number of methods in this class. 
00094   methodOop      method_at(int index)  const;        // Returns the method at index.
00095   void           add_method(methodOop method);       // Adds or overwrites with method.
00096   methodOop      remove_method_at(int index);        // Removes method at index and returns the removed method.
00097 
00098   int            number_of_classVars()   const;      // Returns the number of class variables. 
00099   associationOop classVar_at(int index)  const;      // Returns the class variable at index.
00100   void           add_classVar(associationOop assoc); // Adds or overwrites class variable.
00101   associationOop remove_classVar_at(int index);      // Removes class variable at index and returns the removed association.
00102   bool           includes_classVar(symbolOop name);  // Tells whether the name is present
00103 
00104   // virtual pointer value
00105   int    vtbl_value() const           { return ((int*) this)[vtbl_position]; }
00106   void set_vtbl_value(int vtbl) {
00107     assert(vtbl % 4 == 0, "VTBL should be aligned");
00108     ((int*) this)[vtbl_position] = vtbl; }
00109 
00110   void bootstrap_klass_part_one(bootstrap* bs);
00111   void bootstrap_klass_part_two(bootstrap* bs);
00112 
00113  public:  
00114    // After reading the snapshot the klass has to be fixed e.g. vtbl initialized!
00115   void fixup_after_snapshot_read();  // must not be virtual; vtbl not fixed yet
00116 
00117   // allocation operations
00118   virtual bool can_inline_allocation() const { return false; }
00119   // If this returns true, the compiler may inline the allocation code.
00120   // This means that the actual definition of allocate() is ignored!!
00121   // Fix this compare member function pointers (9/9-1994)
00122 
00123   // Reflective properties
00124   virtual bool can_have_instance_variables() const { return false; }
00125   virtual bool can_be_subclassed()           const { return false; }
00126           bool is_specialized_class()        const;
00127 
00128   // Tells whether this is a named class
00129   bool is_named_class() const;
00130 
00131   // allocation operations
00132   int size() const {  return sizeof(Klass)/sizeof(oop); }
00133  
00134   virtual oop allocateObject();
00135   virtual oop allocateObjectSize(int size);
00136 
00137   enum Format { // Format of a vm klass
00138     no_klass,
00139     mem_klass,
00140       association_klass,
00141       blockClosure_klass,
00142       byteArray_klass,
00143         symbol_klass,
00144       context_klass,
00145       doubleByteArray_klass,
00146       doubleValueArray_klass,
00147       double_klass,
00148       klass_klass,
00149       method_klass,
00150       mixin_klass,
00151       objArray_klass,
00152         weakArray_klass,
00153     process_klass,
00154     vframe_klass,
00155     proxy_klass,
00156     smi_klass,
00157     special_klass
00158   };
00159 
00160   // format
00161   virtual Format format() { return no_klass; }
00162 
00163   static Format format_from_symbol(symbolOop format);
00164   static char*  name_from_format(Format format);
00165 
00166   // Tells whether the two klass have same layout (format and instance variables)
00167   bool has_same_layout_as(klassOop klass);
00168 
00169   // creates invocation
00170   virtual klassOop create_subclass(mixinOop mixin, Format format);
00171 
00172  protected:
00173   static klassOop create_generic_class(klassOop super_class, mixinOop mixin, int vtbl);
00174 
00175  public:
00176 
00177   virtual char* name() const { return ""; }
00178   void  print_klass();
00179   void print_name_on(outputStream* st);
00180 
00181   // Methods
00182   inline methodOop local_lookup(symbolOop selector);
00183   methodOop lookup(symbolOop selector);
00184   bool is_method_holder_for(methodOop method);
00185   klassOop lookup_method_holder_for(methodOop method);
00186 
00187   // Reflective operation
00188   void flush_methods();
00189 
00190   // Class variables
00191   associationOop local_lookup_class_var(symbolOop name);
00192   associationOop lookup_class_var(symbolOop name);
00193 
00194   // Instance variables
00195 
00196   // Returns the word offset for an instance variable.
00197   // -1 is returned if the search failed.
00198   int lookup_inst_var(symbolOop name) const;
00199 
00200   // Returns the name of the instance variable at offset.
00201   // NULL is returned if the search failed.
00202   symbolOop inst_var_name_at(int offset) const;
00203 
00204   // Compute the number of instance variables based on the mixin,
00205   int number_of_instance_variables() const;
00206 
00207   // Schema change support
00208   void mark_for_schema_change();
00209   bool is_marked_for_schema_change();
00210 
00211   void initialize();
00212 
00213  // ALL FUNCTIONS BELOW THIS POINT ARE DISPATCHED FROM AN OOP
00214  // These functions describe behavior for the oop not the KLASS.
00215  public:
00216   // actual oop size of obj in memory
00217   virtual int oop_size(oop obj) const { return non_indexable_size(); }
00218 
00219   // Returns the header size for an instance of this klass
00220   virtual int oop_header_size() const { return 0; }
00221 
00222   // memory operations
00223   virtual bool oop_verify(oop obj);
00224 
00225   virtual int  oop_scavenge_contents(oop obj);
00226   virtual int  oop_scavenge_tenured_contents(oop obj);
00227   virtual void oop_follow_contents(oop obj);
00228 
00229   // type testing operations
00230   virtual bool oop_is_smi()              const { return false; }
00231   virtual bool oop_is_double()           const { return false; }
00232   virtual bool oop_is_block()            const { return false; }
00233   virtual bool oop_is_byteArray()        const { return false; }
00234   virtual bool oop_is_doubleByteArray()  const { return false; }
00235   virtual bool oop_is_doubleValueArray() const { return false; }
00236   virtual bool oop_is_symbol()           const { return false; }
00237   virtual bool oop_is_objArray()         const { return false; }
00238   virtual bool oop_is_weakArray()        const { return false; }
00239   virtual bool oop_is_klass()            const { return false; }
00240   virtual bool oop_is_process()          const { return false; }
00241   virtual bool oop_is_vframe()           const { return false; }
00242   virtual bool oop_is_method()           const { return false; }
00243   virtual bool oop_is_proxy()            const { return false; }
00244   virtual bool oop_is_mixin()            const { return false; }
00245   virtual bool oop_is_association()      const { return false; }
00246   virtual bool oop_is_context()          const { return false; }
00247   virtual bool oop_is_message()          const { return false; }
00248   virtual bool oop_is_indexable()        const { return false; }
00249   
00250   // Dispatched primitives
00251   virtual oop oop_primitive_allocate(oop obj);
00252   virtual oop oop_primitive_allocate_size(oop obj, int size);
00253   virtual oop oop_shallow_copy(oop obj, bool tenured);
00254 
00255   // printing operations
00256   virtual void oop_print_on      (oop obj, outputStream* st);
00257   virtual void oop_short_print_on(oop obj, outputStream* st);
00258   virtual void oop_print_value_on(oop obj, outputStream* st);
00259   
00260   // iterators
00261   virtual void oop_oop_iterate(oop obj, OopClosure* blk);
00262   virtual void oop_layout_iterate(oop obj, ObjectLayoutClosure* blk); 
00263 
00264   friend klassKlass;
00265 };

Generated on Mon Oct 9 13:37:12 2006 for Strongtalk VM by  doxygen 1.4.7