expr.hpp

Go to the documentation of this file.
00001 /* Copyright 1994 - 1996 LongView Technologies L.L.C. $Revision: 1.29 $ */
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 # ifdef DELTA_COMPILER
00025 
00026 // Exprs denote the results of expressions in the compiler.  Their main
00027 // purpose is to annotate a PReg with type information.  
00028 // Another way of looking at Exprs is that they represent values in the compiler,
00029 // whereas PRegs denote locations (names).  Thus, many Exprs may point to the
00030 // same PReg.
00031 
00032 # define BASIC_FLAG_DEF(name, prot)                                           \
00033  protected:                                                                   \
00034     static const int CONC(name,Bit);                                          \
00035  prot                                                                         \
00036  bool CONC(is,name)() const { return flags & CONC(name,Bit) ? true : false; }                 \
00037   void CONC(set,name)(bool b) {                                               \
00038     if (b) flags |= CONC(name,Bit); else flags &= ~CONC(name,Bit); }          \
00039 
00040 # define FLAG_DEF(name)       BASIC_FLAG_DEF(name, public:)
00041 # define PFLAG_DEF(name)      BASIC_FLAG_DEF(name, protected:)
00042 
00043 class Expr : public PrintableResourceObj {      // abstract base class
00044  protected:
00045   PReg* _preg;                          // PReg holding it
00046   Node* _node;                          // defining node or NULL if unknown
00047   InlinedScope* unlikelyScope;          // scope/bci making unknown unlikely
00048   int unlikelyBCI;                      // (only set if isUnknownUnlikely())
00049 
00050  public:
00051   Expr* next;                           // used for splittable MergeExprs
00052   int flags;
00053   
00054   Expr(PReg* p, Node* n);
00055   
00056   virtual bool isUnknownExpr() const            { return false; }
00057   virtual bool isNoResultExpr() const           { return false; }
00058   virtual bool isKlassExpr() const              { return false; }
00059   virtual bool isBlockExpr() const              { return false; }
00060   virtual bool isConstantExpr() const           { return false; }
00061   virtual bool isMergeExpr() const              { return false; }
00062   virtual bool isContextExpr() const            { return false; }
00063   
00064   virtual bool hasKlass() const                 { return false; }
00065   virtual int  nklasses() const                 = 0;    // number of klasses contained in expr
00066   virtual bool really_hasKlass(InlinedScope* s) const { Unused(s);  return hasKlass(); }
00067   virtual klassOop klass() const                { ShouldNotCallThis(); return 0; }
00068   virtual bool hasConstant() const              { return false; }
00069   virtual oop constant() const                  { ShouldNotCallThis(); return 0; }
00070   virtual bool containsUnknown()                = 0;
00071   virtual Expr* makeUnknownUnlikely(InlinedScope* s) {
00072     Unused(s); ShouldNotCallThis(); return 0;}
00073   virtual bool isUnknownUnlikely() const        { return false; }
00074   virtual bool needsStoreCheck() const          { return true; }
00075   
00076   virtual UnknownExpr* findUnknown() const      { return NULL; }
00077   virtual Expr* findKlass(klassOop map) const   { Unused(map);  return NULL; }
00078   
00079   virtual Expr* asReceiver() const;
00080   virtual KlassExpr* asKlassExpr() const        { ShouldNotCallThis(); return NULL; }
00081   virtual ConstantExpr* asConstantExpr() const  { ShouldNotCallThis(); return NULL; }
00082   virtual MergeExpr* asMergeExpr() const        { ShouldNotCallThis(); return NULL; }
00083   
00084   virtual Expr* shallowCopy(PReg* p, Node* n) const     = 0;  // return a shallow copy
00085   virtual Expr* copyWithout(Expr* e) const              = 0;  // return receiver w/o expr case
00086   virtual Expr* mergeWith(Expr* other, Node* n)         = 0;  // return receiver merged with other
00087   virtual Expr* convertToKlass(PReg* p, Node* n) const  = 0;  // convert constants to klasses
00088   virtual bool equals(Expr* other) const                = 0;
00089   
00090   Node*   node() const                          { return _node; }
00091   PReg*   preg() const                          { return _preg; }
00092   void    setNode(Node* n, PReg* p)             { _node = n; _preg = p; }
00093   bool    is_smi() const                        { return hasKlass() && klass() == smiKlassObj; }
00094 
00095   InlinedScope* scope() const;
00096   virtual NameNode* nameNode(bool mustBeLegal = true) const;
00097   virtual void verify() const;
00098 
00099  protected:
00100   void print_helper(char* type);
00101 };
00102 
00103 // an expression whose type is unknown
00104 class UnknownExpr : public Expr {
00105  public:
00106   UnknownExpr(PReg* p, Node* n = NULL, bool u = false) : Expr(p, n) {
00107     setUnlikely(u); }
00108   bool isUnknownExpr() const            { return true; }
00109   bool containsUnknown()                { return true; }
00110   FLAG_DEF(Unlikely);                   // true e.g. if this is the "unknown" branch of a
00111                                         // type-predicted receiver, or result of prim. failure
00112   UnknownExpr* findUnknown() const      { return (UnknownExpr*)this; }
00113   bool isUnknownUnlikely() const        { return isUnlikely(); }
00114   int  nklasses() const                 { return 0; }  
00115   Expr* shallowCopy(PReg* p, Node* n) const;
00116   Expr* copyWithout(Expr* e) const      { return (Expr*)this; }
00117   Expr* mergeWith(Expr* other, Node* n);
00118   Expr* convertToKlass(PReg* p, Node* n) const { return shallowCopy(p, n); };
00119   Expr* makeUnknownUnlikely(InlinedScope* s);
00120   bool equals(Expr* other) const;
00121   void print();
00122 };
00123 
00124 // an expression that has no value, i.e., will never exist at runtime
00125 // example: the return value of a block method that ends with a non-local return
00126 // used mainly for compiler debugging and to avoid generating unreachable code
00127 class NoResultExpr : public Expr {
00128  public:
00129   NoResultExpr(Node* n = NULL);
00130   bool isNoResultExpr() const           { return true; }
00131   int  nklasses() const                 { return 0; }  
00132   bool containsUnknown()                { return false; }
00133   Expr* shallowCopy(PReg* p, Node* n) const;
00134   Expr* copyWithout(Expr* e) const      { return (Expr*)this; }
00135   Expr* mergeWith(Expr* other, Node* n);
00136   Expr* convertToKlass(PReg* p, Node* n) const  { return shallowCopy(p, n); };
00137   bool equals(Expr* other) const;
00138   void print();
00139 };
00140 
00141 
00142 // an expression whose klass is known
00143 class KlassExpr : public Expr {
00144  protected:
00145   klassOop _klass;
00146  public:
00147   KlassExpr(klassOop m, PReg* p, Node* n);
00148   
00149   bool isKlassExpr() const              { return true; }
00150   bool containsUnknown()                { return false; }
00151   
00152   KlassExpr* asKlassExpr() const        { return (KlassExpr*)this; }
00153   bool hasKlass() const                 { return true; }
00154   int  nklasses() const                 { return 1; }  
00155   klassOop klass() const                { return _klass; }
00156   virtual bool needsStoreCheck() const;
00157   Expr* shallowCopy(PReg* p, Node* n) const;
00158   Expr* copyWithout(Expr* e) const;
00159   Expr* mergeWith(Expr* other, Node* n);
00160   Expr* convertToKlass(PReg* p, Node* n) const  { return shallowCopy(p, n); };
00161   Expr* findKlass(klassOop map) const   { return _klass == map ? (Expr*)this : NULL; }
00162   bool equals(Expr* other) const;
00163   void print();
00164   virtual void verify() const;
00165 };
00166 
00167 // a cloned block literal (result of BlockClone node)
00168 class BlockExpr : public KlassExpr {
00169  protected:
00170   InlinedScope* _blockScope;            // block's parent scope
00171  public:
00172   BlockExpr(BlockPReg* p, Node* n);
00173   bool isBlockExpr() const              { return true; }
00174   InlinedScope* blockScope() const      { return _blockScope; }
00175   BlockPReg* preg() const               { return (BlockPReg*)_preg; }
00176   int  nklasses() const                 { return 1; }  
00177   Expr* shallowCopy(PReg* p, Node* n) const;
00178   Expr* mergeWith(Expr* other, Node* n);
00179   bool equals(Expr* other) const;
00180   void print();
00181   virtual void verify() const;
00182 };
00183 
00184 // an expression whose exact runtime value is known
00185 class ConstantExpr : public Expr {
00186   oop _c;
00187  public:
00188   ConstantExpr(oop c, PReg* p, Node* n) : Expr(p, n) { _c = c; }
00189   
00190   bool isConstantExpr() const                   { return true; }
00191   bool containsUnknown()                        { return false; }
00192   bool hasKlass() const                         { return true; }
00193   KlassExpr* asKlassExpr() const;
00194   klassOop klass() const                        { return _c->klass(); }
00195   int  nklasses() const                         { return 1; }  
00196   ConstantExpr* asConstantExpr() const          { return (ConstantExpr*)this; }
00197   bool hasConstant() const                      { return true; }
00198   oop constant() const                          { return _c; }
00199   virtual bool needsStoreCheck() const;
00200   NameNode* nameNode(bool mustBeLegal = true) const;
00201   Expr* shallowCopy(PReg* p, Node* n) const;
00202   Expr* copyWithout(Expr* e) const;
00203   Expr* mergeWith(Expr* other, Node* n);
00204   Expr* convertToKlass(PReg* p, Node* n) const;
00205   Expr* findKlass(klassOop map) const;
00206   bool equals(Expr* other) const;
00207   void print();
00208   virtual void verify() const;
00209 };
00210 
00211 
00212 // an expression that is the result of merging two or more other expressions
00213 // when control flow merges
00214 // example: the result of foo ifTrue: [ 1 ] ifFalse: [ i + j ] could be
00215 // MergeExpr(ConstantExpr(1), KlassExpr(IntegerKlass))
00216 // merge expressions have the following properties:
00217 // - they never have more than MaxPICSize+1 elements
00218 // - they're flat (i.e don't contain other MergeExprs)
00219 
00220 class MergeExpr : public Expr {
00221  public:
00222   GrowableArray<Expr*>* exprs;
00223   
00224   MergeExpr(Expr* e1, Expr* e2, PReg* p, Node* n);
00225   MergeExpr(PReg* p, Node* n);
00226         // A MergeExpr's PReg says where the merged result is (i.e., it may be different from
00227         // the PRegs of the individual expressions); typically, there's an assigmnent from the
00228         // subexpression's PReg to the MergeExpr's PReg just before the control flow merge.
00229         // The node n (if non-NULL) is considered the defining node of the MergeExpr; it is usually
00230         // one of the first nodes after the control flow merge.  It is always legal to pass
00231         // NULL for n, but doing so prevents splitting.
00232   
00233   bool isMergeExpr() const              { return true; }
00234   FLAG_DEF(Splittable);
00235   PFLAG_DEF(UnknownSet);
00236   PFLAG_DEF(ContainingUnknown);
00237  public:
00238   bool containsUnknown();
00239   MergeExpr* asMergeExpr() const        { return (MergeExpr*)this; }
00240   bool hasKlass() const;
00241   int  nklasses() const;  
00242   KlassExpr* asKlassExpr() const;
00243   bool really_hasKlass(InlinedScope* s) const;
00244   klassOop klass() const;
00245   Expr* asReceiver() const;
00246   bool hasConstant() const;
00247   oop constant() const;
00248   Expr* shallowCopy(PReg* p, Node* n) const;
00249   Expr* copyWithout(Expr* e) const;
00250   Expr* mergeWith(Expr* other, Node* n);
00251   Expr* convertToKlass(PReg* p, Node* n) const;
00252   Expr* makeUnknownUnlikely(InlinedScope* s);
00253   bool equals(Expr* other) const;
00254   void print();
00255   virtual void verify() const;
00256   
00257   UnknownExpr* findUnknown() const;    // returns NULL if no expression found
00258   Expr* findKlass(klassOop map) const;
00259   
00260 protected:
00261   void initialize();
00262   void mergeInto(Expr* other, Node* n);
00263   void add(Expr* s);
00264 };
00265 
00266 
00267 // an expression for a context pointer
00268 // used only for compiler debugging; it should never appear where a normal expression is expected
00269 class ContextExpr : public Expr {
00270  public:
00271         ContextExpr(PReg* r);
00272   bool  isContextExpr() const                   { return true; }
00273   bool  containsUnknown()                       { ShouldNotCallThis(); return false; }
00274   int   nklasses() const                        { ShouldNotCallThis(); return 1; }  
00275   Expr* shallowCopy(PReg* p, Node* n) const     { ShouldNotCallThis(); return (Expr*)this; }
00276   Expr* copyWithout(Expr* e) const              { ShouldNotCallThis(); return (Expr*)this; }
00277   Expr* mergeWith(Expr* other, Node* n)         { ShouldNotCallThis(); return (Expr*)this; }
00278   Expr* convertToKlass(PReg* p, Node* n) const  { return shallowCopy(p, n); };
00279   bool  equals(Expr* other) const               { ShouldNotCallThis(); return false; }
00280   void  print();
00281   virtual void verify() const;
00282 };
00283 
00284 
00285 class ExprStack : public GrowableArray<Expr*> {
00286   // an ExprStack simulates the run-time expression stack during compilation
00287   // it keeps track of the live ranges by recording the bci on push/pop
00288  private:
00289   InlinedScope* _scope;                         // scope that generates the pushes and pops
00290  public:
00291   ExprStack(InlinedScope* scope, int size);
00292   void  push(Expr* expr, InlinedScope* currentScope, int bci);
00293   void  push2nd(Expr* expr, InlinedScope* currentScope, int bci); // allows a 2nd expr to be pushed for the same bci
00294   void  assign_top(Expr* expr);
00295   Expr* pop();
00296   void  pop(int nofExprsToPop);
00297   void  print();
00298 };
00299 
00300 # undef BASIC_FLAG_DEF
00301 # undef FLAG_DEF
00302 # undef PFLAG_DEF
00303 
00304 # endif

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