exp.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2002-2006, Trent Waddington and Mike Van Emmerik
00003  */
00004 /*==============================================================================
00005  * FILE:       exp.h
00006  * OVERVIEW:   Provides the definition for the Exp class and its subclasses.
00007  *============================================================================*/
00008 /*
00009  * $Revision: 1.139 $   // 1.119.2.11
00010  *
00011  * 05 Apr 02 - Mike: Created
00012  * 05 Apr 02 - Mike: Added clone(), copy constructors
00013  * 08 Apr 02 - Mike: Added isFlagCall(), Terminal subclass
00014  * 10 Apr 02 - Mike: Search and replace
00015  * 29 Apr 02 - Mike: TypedExp takes Type& and Exp* in opposite order; consistent
00016  * 10 May 02 - Mike: Added refSubExp1 etc
00017  * 21 May 02 - Mike: Mods for gcc 3.1
00018  * 02 Aug 04 - Mike: Removed PhiExp (PhiAssign replaces it) 
00019  * 05 Aug 04 - Mike: Removed the withUses/withDF parameter from print() funcs
00020  */
00021 
00022 #ifndef __EXP_H_
00023 #define __EXP_H_
00024 
00025 /* Main class hierarchy:    Exp (abstract)
00026                       _____/ | \
00027                      /       |  \
00028                   Unary    Const Terminal
00029      TypedExp____/  |   \         \
00030       FlagDef___/ Binary Location  TypeVal
00031        RefExp__/    |
00032                  Ternary
00033 */
00034 
00035 #include <iostream>
00036 #include <fstream>      // For ostream, cout etc
00037 #include <stdio.h>      // For sprintf
00038 #include <list>
00039 #include <vector>
00040 #include <set>
00041 #include <assert.h>
00042 #include "operator.h"   // Declares the OPER enum
00043 #include "types.h"      // For ADDRESS, etc
00044 #include "type.h"       // The Type class for typed expressions
00045 //#include "statement.h"    // For StmtSet etc
00046 #include "exphelp.h"
00047 #include "memo.h"
00048 
00049 class UseSet;
00050 class DefSet;
00051 class RTL;              // For class FlagDef
00052 class Statement;
00053 class BasicBlock;
00054 class LocationSet;
00055 class StatementSet;
00056 class TypeVal;
00057 class ExpVisitor;
00058 class ExpModifier;
00059 class XMLProgParser;
00060 class Proc;
00061 class UserProc;
00062 typedef BasicBlock* PBB;
00063 
00064 #define DEBUG_BUFSIZE   5000        // Size of the debug print buffer
00065 
00066 
00067 /*==============================================================================
00068  * Exp is an expression class, though it will probably be used to hold many other things (e.g. perhaps transformations).
00069  * It is a standard tree representation. Exp itself is abstract. A special class Const is used for constants. Unary,
00070  * Binary, and Ternary hold 1, 2, and 3 subexpressions respectively. For efficiency of representation, these have to be
00071  * separate classes, derived from Exp.
00072  *============================================================================*/
00073 
00074 // Class Exp is abstract. However, the constructor can be called from the constructors of derived classes, and virtual
00075 // functions not overridden by derived classes can be called
00076 class Exp {
00077 protected:
00078         OPER        op;            // The operator (e.g. opPlus)
00079 
00080         unsigned    lexBegin, lexEnd;
00081 
00082         // Constructor, with ID
00083                     Exp(OPER op) : op(op) {}
00084 
00085 public:
00086         // Virtual destructor
00087 virtual             ~Exp() {}
00088 
00089         // Return the operator. Note: I'd like to make this protected, but then subclasses don't seem to be able to use
00090         // it (at least, for subexpressions)
00091         OPER        getOper() const {return op;}
00092         void        setOper(OPER x) {op = x;}     // A few simplifications use this
00093 
00094         void        setLexBegin(unsigned int n) { lexBegin = n; }
00095         void        setLexEnd(unsigned int n) { lexEnd = n; }
00096         unsigned    getLexBegin() { return lexBegin; }
00097         unsigned    getLexEnd() { return lexEnd; }
00098 
00099         // Print the expression to the given stream
00100 virtual void        print(std::ostream& os, bool html = false) = 0;
00101         // Print with <type>
00102         void        printt(std::ostream& os = std::cout);
00103         void        printAsHL(std::ostream& os = std::cout); // Print with v[5] as v5
00104         char*       prints();       // Print to string (for debugging and logging)
00105         void        dump();         // Print to standard error (for debugging)
00106         // Recursive print: don't want parens at the top level
00107 virtual void printr(std::ostream& os, bool html = false) { print(os, html);}       // But most classes want standard
00108         // For debugging: print in indented hex. In gdb: "p x->printx(0)"
00109 virtual void        printx(int ind) = 0;
00110 
00111         // Display as a dotty graph
00112         void        createDotFile(char* name);
00113 virtual void        appendDotFile(std::ofstream& os) = 0;
00114 
00115         // Clone (make copy of self that can be deleted without affecting self)
00116 virtual Exp*        clone() = 0;
00117 
00118         // Comparison
00119 // Type sensitive equality
00120 virtual bool        operator==(const Exp& o) const = 0;
00121 // Type sensitive less than
00122 virtual bool        operator< (const Exp& o)  const = 0;
00123 // Type insensitive less than. Class TypedExp overrides
00124 virtual bool        operator<<(const Exp& o) const
00125                         {return (*this < o);}
00126 // Comparison ignoring subscripts
00127 virtual bool        operator*=(Exp& o) = 0;
00128 
00129 // Return the number of subexpressions. This is only needed in rare cases.
00130 // Could use polymorphism for all those cases, but this is easier
00131 virtual int getArity() {return 0;}      // Overridden for Unary, Binary, etc
00132 
00133         //  //  //  //  //  //  //
00134         //   Enquiry functions  //
00135         //  //  //  //  //  //  //
00136 
00137         // True if this is a call to a flag function
00138         bool        isFlagCall() {return op == opFlagCall;}
00139         // True if this represents one of the abstract flags locations, int or float
00140         bool        isFlags() {return op == opFlags || op == opFflags;}
00141         // True if is one of the main 4 flags
00142         bool        isMainFlag() {return op >= opZF && op <= opOF;}
00143         // True if this is a register location
00144         bool        isRegOf() {return op == opRegOf;}
00145         // True if this is a register location with a constant index
00146         bool        isRegOfK();
00147         // True if this is a specific numeric register
00148         bool        isRegN(int n);
00149         // True if this is a memory location (any memory nesting depth)
00150         bool        isMemOf() {return op == opMemOf;}
00151         // True if this is an address of
00152         bool        isAddrOf() {return op == opAddrOf;}
00153         // True if this is an array expression
00154         bool        isArrayIndex() {return op == opArrayIndex;}
00155         // True if this is a struct member access
00156         bool        isMemberOf() {return op == opMemberAccess;}
00157         // True if this is a temporary. Note some old code still has r[tmp]
00158         bool        isTemp();
00159         // True if this is the anull Terminal (anulls next instruction)
00160         bool        isAnull() {return op == opAnull;}
00161         // True if this is the Nil Terminal (terminates lists; "NOP" expression)
00162         bool        isNil() {return op == opNil;}
00163         // True if this is %pc
00164         bool        isPC() {return op == opPC;}
00165         // True if is %afp, %afp+k, %afp-k, or a[m[<any of these]]
00166         bool        isAfpTerm();
00167         // True if is int const
00168         bool        isIntConst() {return op == opIntConst;}
00169         // True if is string const
00170         bool        isStrConst() {return op == opStrConst;}
00171         // Get string constant even if mangled
00172         char*       getAnyStrConst();
00173         // True if is flt point const
00174         bool        isFltConst() {return op == opFltConst;}
00175         // True if inteter or string constant
00176         bool        isConst() {return op == opIntConst || op == opStrConst;}
00177         // True if is a post-var expression (var_op' in SSL file)
00178         bool        isPostVar() {return op == opPostVar;}
00179         // True if this is an opSize (size case; deprecated)
00180         bool        isSizeCast() {return op == opSize;}
00181         // True if this is a subscripted expression (SSA)
00182         bool        isSubscript() {return op == opSubscript;}
00183         // True if this is a phi assignmnet (SSA)
00184 //      bool        isPhi() {return op == opPhi;}
00185         // True if this is a local variable
00186         bool        isLocal() {return op == opLocal;}
00187         // True if this is a global variable
00188         bool        isGlobal() {return op == opGlobal;}
00189         // True if this is a typeof
00190         bool        isTypeOf() {return op == opTypeOf;}
00191         // Get the index for this var
00192         int         getVarIndex();
00193         // True if this is a terminal
00194 virtual bool        isTerminal() { return false; }
00195         // True if this is the constant "true"
00196         bool        isTrue() {return op == opTrue;}
00197         // True if this is the constant "false"
00198         bool        isFalse() {return op == opFalse;}
00199         // True if this is a disjunction, i.e. x or y
00200         bool        isDisjunction() {return op == opOr;}
00201         // True if this is a conjunction, i.e. x and y
00202         bool        isConjunction() {return op == opAnd;}
00203         // True if this is a boolean constant
00204         bool        isBoolConst() {return op == opTrue || op == opFalse;}
00205         // True if this is an equality (== or !=)
00206         bool        isEquality() {return op == opEquals /*|| op == opNotEqual*/;}
00207         // True if this is a comparison
00208         bool        isComparison() { return  op == opEquals || op == opNotEqual ||
00209                                              op == opGtr || op == opLess ||
00210                                              op == opGtrUns || op == opLessUns ||
00211                                              op == opGtrEq || op == opLessEq ||
00212                                              op == opGtrEqUns || op == opLessEqUns; }
00213         // True if this is a TypeVal
00214         bool        isTypeVal() { return op == opTypeVal;}
00215         // True if this is a machine feature
00216         bool        isMachFtr() {return op == opMachFtr;}
00217         // True if this is a parameter. Note: opParam has two meanings: a SSL parameter, or a function parameter
00218         bool        isParam() {return op == opParam;}
00219         
00220         // True if this is a location
00221         bool        isLocation() { return   op == opMemOf || op == opRegOf ||
00222                                             op == opGlobal || op == opLocal ||
00223                                             op == opParam; }
00224 
00225         // True if this is a typed expression
00226         bool        isTypedExp() { return op == opTypedExp;}
00227 
00228 
00229         // FIXME: are these used?
00230         // Matches this expression to the pattern, if successful returns a list of variable bindings, otherwise returns
00231         // NULL
00232 virtual Exp         *match(Exp *pattern);
00233 
00234         // match a string pattern
00235 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00236 
00237             //  //  //  //  //  //  //
00238             //  Search and Replace  //
00239             //  //  //  //  //  //  //
00240         
00241         // Search for Exp *search in this Exp. If found, return true and return a ptr to the matching expression in
00242         // result (useful with wildcards).
00243 virtual bool        search(Exp* search, Exp*& result);
00244 
00245         // Search for Exp search in this Exp. For each found, add a ptr to the matching expression in result (useful
00246         // with wildcards).   Does NOT clear result on entry
00247         bool        searchAll(Exp* search, std::list<Exp*>& result);
00248 
00249         // Search this Exp for *search; if found, replace with *replace
00250         Exp*        searchReplace (Exp* search, Exp* replace, bool& change);
00251 
00252         // Search *pSrc for *search; for all occurrences, replace with *replace
00253         Exp*        searchReplaceAll(Exp* search, Exp* replace, bool& change, bool once = false);
00254 
00255         // Mostly not for public use. Search for subexpression matches.
00256 static  void        doSearch(Exp* search, Exp*& pSrc, std::list<Exp**>& li, bool once);
00257 
00258         // As above.
00259 virtual void        doSearchChildren(Exp* search, std::list<Exp**>& li, bool once);
00260 
00261         /// Propagate all possible assignments to components of this expression.
00262         Exp*        propagateAll();
00263         Exp*        propagateAllRpt(bool& changed);     // As above, but keep propagating until no change
00264 
00265         //  //  //  //  //  //  //
00266         //    Sub expressions   //
00267         //  //  //  //  //  //  //
00268     
00269         // These are here so we can (optionally) prevent code clutter.
00270         // Using a *Exp (that is known to be a Binary* say), you can just directly call getSubExp2.
00271         // However, you can still choose to cast from Exp* to Binary* etc. and avoid the virtual call
00272 virtual Exp*        getSubExp1() {return 0;}
00273 virtual Exp*        getSubExp2() {return 0;}
00274 virtual Exp*        getSubExp3() {return 0;}
00275 virtual Exp*&       refSubExp1();
00276 virtual Exp*&       refSubExp2();
00277 virtual Exp*&       refSubExp3();
00278 virtual void        setSubExp1(Exp* e) {};
00279 virtual void        setSubExp2(Exp* e) {};
00280 virtual void        setSubExp3(Exp* e) {};
00281 
00282         // Get the complexity depth. Basically, add one for each unary, binary, or ternary
00283         int         getComplexityDepth(UserProc* proc);
00284         // Get memory depth. Add one for each m[]
00285         int         getMemDepth();
00286 
00287         //  //  //  //  //  //  //
00288         //  Guarded assignment  //
00289         //  //  //  //  //  //  //
00290         Exp*        getGuard();         // Get the guard expression, or 0 if not
00291     
00292         //  //  //  //  //  //  //  //  //
00293         //  Expression Simplification   //
00294         //  //  //  //  //  //  //  //  //
00295     
00296         void        partitionTerms(std::list<Exp*>& positives, std::list<Exp*>& negatives, std::vector<int>& integers,
00297                         bool negate);
00298 virtual Exp*        simplifyArith() {return this;}
00299 static  Exp*        Accumulate(std::list<Exp*> exprs);
00300         // Simplify the expression
00301         Exp*        simplify();
00302 virtual Exp*        polySimplify(bool& bMod) {bMod = false; return this;}
00303         // Just the address simplification a[ m[ any ]]
00304 virtual Exp*        simplifyAddr() {return this;}
00305 virtual Exp*        simplifyConstraint() {return this;}
00306         Exp*        fixSuccessor();     // succ(r2) -> r3
00307         // Kill any zero fill, sign extend, or truncates
00308         Exp*        killFill();
00309 
00310         // Do the work of finding used locations. If memOnly set, only look inside m[...]
00311         void        addUsedLocs(LocationSet& used, bool memOnly = false);
00312 
00313         Exp         *removeSubscripts(bool& allZero);
00314 
00315         // Get number of definitions (statements this expression depends on)
00316 virtual int         getNumRefs() {return 0;}
00317 
00318         // Convert from SSA form, where this is not subscripted (but defined at statement d)
00319         // Needs the UserProc for the symbol map
00320         Exp*        fromSSAleft(UserProc* proc, Statement* d);
00321 
00322         // Generate constraints for this Exp. NOTE: The behaviour is a bit different depending on whether or not
00323         // parameter result is a type constant or a type variable.
00324         // If the constraint is always satisfied, return true
00325         // If the constraint can never be satisfied, return false
00326         // Example: this is opMinus and result is <int>, constraints are:
00327         //   sub1 = <int> and sub2 = <int> or
00328         //   sub1 = <ptr> and sub2 = <ptr>
00329         // Example: this is opMinus and result is Tr (typeOf r), constraints are:
00330         //   sub1 = <int> and sub2 = <int> and Tr = <int> or
00331         //   sub1 = <ptr> and sub2 = <ptr> and Tr = <int> or
00332         //   sub1 = <ptr> and sub2 = <int> and Tr = <ptr>
00333 virtual Exp*        genConstraints(Exp* result);
00334 
00335         // Visitation
00336         // Note: best to have accept() as pure virtual, so you don't forget to implement it for new subclasses of Exp
00337 virtual bool        accept(ExpVisitor* v) = 0;
00338 virtual Exp*        accept(ExpModifier* v) = 0;
00339         void        fixLocationProc(UserProc* p);
00340         UserProc*   findProc();
00341         // Set or clear the constant subscripts
00342         void        setConscripts(int n, bool bClear);
00343         Exp*        stripSizes();           // Strip all size casts
00344         // Subscript all e in this Exp with statement def:
00345         Exp*        expSubscriptVar(Exp* e, Statement* def /*, Cfg* cfg */ );
00346         // Subscript all e in this Exp with 0 (implicit assignments)
00347         Exp*        expSubscriptValNull(Exp* e /*, Cfg* cfg */);
00348         // Subscript all locations in this expression with their implicit assignments
00349         Exp*        expSubscriptAllNull(/*Cfg* cfg*/);
00350         // Perform call bypass and simple (assignment only) propagation to this exp
00351         // Note: can change this, so often need to clone before calling
00352         Exp*        bypass();
00353         void        bypassComp();                   // As above, but only the xxx of m[xxx]
00354         bool        containsFlags();                // Check if this exp contains any flag calls
00355         bool        containsBadMemof(UserProc* p);  // Check if this Exp contains a bare (non subscripted) memof
00356         bool        containsMemof(UserProc* proc);  // Check of this Exp contains any memof at all. Not used.
00357 
00358         // Data flow based type analysis (implemented in type/dfa.cpp)
00359         // Pull type information up the expression tree
00360 virtual Type*       ascendType() {assert(0); return 0;}
00361         // Push type information down the expression tree
00362 virtual void        descendType(Type* parentType, bool& ch, Statement* s) {assert(0);}
00363 
00364 protected:
00365         friend class XMLProgParser;
00366 };      // class Exp
00367 
00368 // Not part of the Exp class, but logically belongs with it:
00369 std::ostream& operator<<(std::ostream& os, Exp* p);  // Print the Exp poited to by p
00370 
00371 /*==============================================================================
00372  * Const is a subclass of Exp, and holds either an integer, floating point, string, or address constant
00373  *============================================================================*/
00374 class Const : public Exp {
00375         union {
00376             int     i;          // Integer
00377             // Note: although we have i and a as unions, both often use the same operator (opIntConst).
00378             // There is no opCodeAddr any more.
00379             ADDRESS a;          // void* conflated with unsigned int: needs fixing
00380             QWord   ll;         // 64 bit integer
00381             double  d;          // Double precision float
00382             char*   p;          // Pointer to string
00383                                 // Don't store string: function could be renamed
00384             Proc*   pp;         // Pointer to function
00385         } u;
00386         int         conscript;  // like a subscript for constants
00387         Type*       type;       // Constants need types during type analysis
00388 public:
00389         // Special constructors overloaded for the various constants
00390                     Const(int i);
00391                     Const(QWord ll);
00392                     Const(ADDRESS a);
00393                     Const(double d);
00394                     Const(char* p);
00395                     Const(Proc* p);
00396         // Copy constructor
00397                     Const(Const& o);
00398             
00399         // Nothing to destruct: Don't deallocate the string passed to constructor
00400 
00401         // Clone
00402 virtual Exp*        clone();
00403 
00404         // Compare
00405 virtual bool        operator==(const Exp& o) const;
00406 virtual bool        operator< (const Exp& o) const;
00407 virtual bool        operator*=(Exp& o);
00408 
00409         // Get the constant
00410         int         getInt() {return u.i;}
00411         QWord       getLong(){return u.ll;}
00412         double      getFlt() {return u.d;}
00413         char*       getStr() {return u.p;}
00414         ADDRESS     getAddr() {return u.a;}
00415         const char* getFuncName();
00416 
00417         // Set the constant
00418         void        setInt(int i)       {u.i = i;}
00419         void        setLong(QWord ll)   {u.ll = ll;}
00420         void        setFlt(double d)    {u.d = d;}
00421         void        setStr(char* p)     {u.p = p;}
00422         void        setAddr(ADDRESS a)  {u.a = a;}
00423 
00424         // Get and set the type
00425         Type*       getType() { return type; }
00426         void        setType(Type* ty) { type = ty; }
00427 
00428 virtual void        print(std::ostream& os, bool html = false);
00429         // Print "recursive" (extra parens not wanted at outer levels)
00430         void        printNoQuotes(std::ostream& os);
00431 virtual void        printx(int ind);
00432  
00433 
00434 virtual void        appendDotFile(std::ofstream& of);
00435 virtual Exp*        genConstraints(Exp* restrictTo);
00436 
00437         // Visitation
00438 virtual bool        accept(ExpVisitor* v);
00439 virtual Exp*        accept(ExpModifier* v);
00440 
00441 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00442 
00443         int         getConscript() {return conscript;}
00444         void        setConscript(int cs) {conscript = cs;}
00445 
00446 virtual Type*       ascendType();
00447 virtual void        descendType(Type* parentType, bool& ch, Statement* s);
00448 
00449 protected:
00450         friend class XMLProgParser;
00451 };      // class Const
00452 
00453 /*==============================================================================
00454  * Terminal is a subclass of Exp, and holds special zero arity items such as opFlags (abstract flags register)
00455  *============================================================================*/
00456 class Terminal : public Exp {
00457 public:
00458         // Constructors
00459                     Terminal(OPER op);
00460                     Terminal(Terminal& o);      // Copy constructor
00461 
00462         // Clone
00463 virtual Exp*    clone();
00464 
00465         // Compare
00466 virtual bool        operator==(const Exp& o) const;
00467 virtual bool        operator< (const Exp& o) const;
00468 virtual bool        operator*=(Exp& o);
00469 
00470 virtual void        print(std::ostream& os, bool html = false);
00471 virtual void        appendDotFile(std::ofstream& of);
00472 virtual void        printx(int ind);
00473 
00474 virtual bool        isTerminal() { return true; }
00475 
00476         // Visitation
00477 virtual bool        accept(ExpVisitor* v);
00478 virtual Exp*        accept(ExpModifier* v);
00479 
00480 virtual Type*       ascendType();
00481 virtual void        descendType(Type* parentType, bool& ch, Statement* s);
00482 
00483 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00484 
00485 protected:
00486         friend class XMLProgParser;
00487 };      // class Terminal
00488 
00489 /*==============================================================================
00490  * Unary is a subclass of Exp, holding one subexpression
00491  *============================================================================*/
00492 class Unary : public Exp {
00493 protected:
00494         Exp*            subExp1;    // One subexpression pointer
00495 
00496         // Constructor, with just ID
00497                     Unary(OPER op);
00498 public:
00499         // Constructor, with ID and subexpression
00500                     Unary(OPER op, Exp* e);
00501         // Copy constructor
00502                     Unary(Unary& o);
00503 
00504         // Clone
00505 virtual Exp*        clone();
00506 
00507         // Compare
00508 virtual bool        operator==(const Exp& o) const;
00509 virtual bool        operator< (const Exp& o) const;
00510 virtual bool        operator*=(Exp& o);
00511 
00512         // Destructor
00513 virtual             ~Unary();
00514 
00515         // Arity
00516 virtual int         getArity() {return 1;}
00517 
00518         // Print
00519 virtual void        print(std::ostream& os, bool html = false);
00520 virtual void        appendDotFile(std::ofstream& of);
00521 virtual void        printx(int ind);
00522 
00523         // Set first subexpression
00524         void        setSubExp1(Exp* e);
00525         void        setSubExp1ND(Exp* e) {subExp1 = e;}
00526         // Get first subexpression
00527         Exp*        getSubExp1();
00528         // Get a reference to subexpression 1
00529         Exp*&       refSubExp1();
00530 
00531 virtual Exp*        match(Exp *pattern); 
00532 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00533         
00534         // Search children
00535         void            doSearchChildren(Exp* search, std::list<Exp**>& li, bool once);
00536 
00537         // Do the work of simplifying this expression
00538 virtual Exp*        polySimplify(bool& bMod);
00539         Exp*        simplifyArith();
00540         Exp*        simplifyAddr();
00541 virtual Exp*        simplifyConstraint();
00542 
00543         // Type analysis
00544 virtual Exp*        genConstraints(Exp* restrictTo);
00545 
00546         // Visitation
00547 virtual bool        accept(ExpVisitor* v);
00548 virtual Exp*        accept(ExpModifier* v);
00549 
00550 virtual Type*       ascendType();
00551 virtual void        descendType(Type* parentType, bool& ch, Statement* s);
00552 
00553 protected:
00554         friend class XMLProgParser;
00555 };  // class Unary
00556 
00557 /*==============================================================================
00558  * Binary is a subclass of Unary, holding two subexpressions
00559  *============================================================================*/
00560 class Binary : public Unary {
00561 protected:
00562         Exp*            subExp2;    // Second subexpression pointer
00563 
00564         // Constructor, with ID
00565                     Binary(OPER op);
00566 
00567 public:
00568         // Constructor, with ID and subexpressions
00569                     Binary(OPER op, Exp* e1, Exp* e2);
00570         // Copy constructor
00571                     Binary(Binary& o);
00572 
00573         // Clone
00574 virtual Exp* clone();
00575 
00576         // Compare
00577 virtual bool         operator==(const Exp& o) const ;
00578 virtual bool         operator< (const Exp& o) const ;
00579 virtual bool         operator*=(Exp& o);
00580 
00581         // Destructor
00582 virtual             ~Binary();
00583 
00584         // Arity
00585         int             getArity() {return 2;}
00586 
00587         // Print
00588 virtual void         print(std::ostream& os, bool html = false);
00589 virtual void         printr(std::ostream& os, bool html = false);
00590 virtual void         appendDotFile(std::ofstream& of);
00591 virtual void         printx(int ind);
00592 
00593         // Set second subexpression
00594         void        setSubExp2(Exp* e);
00595         // Get second subexpression
00596         Exp*        getSubExp2();
00597         // Commute the two operands
00598         void        commute();
00599         // Get a reference to subexpression 2
00600         Exp*&       refSubExp2();
00601 
00602 virtual Exp*        match(Exp *pattern); 
00603 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00604 
00605         // Search children
00606         void        doSearchChildren(Exp* search, std::list<Exp**>& li, bool once);
00607 
00608         // Do the work of simplifying this expression
00609 virtual Exp*        polySimplify(bool& bMod);
00610         Exp*        simplifyArith();
00611         Exp*        simplifyAddr();
00612 virtual Exp*        simplifyConstraint();
00613 
00614         // Type analysis
00615 virtual Exp*        genConstraints(Exp* restrictTo);
00616 
00617         // Visitation
00618 virtual bool        accept(ExpVisitor* v);
00619 virtual Exp*        accept(ExpModifier* v);
00620 
00621 virtual Type*       ascendType();
00622 virtual void        descendType(Type* parentType, bool& ch, Statement* s);
00623 
00624 private:
00625         Exp*        constrainSub(TypeVal* typeVal1, TypeVal* typeVal2);
00626 
00627 protected:
00628         friend class XMLProgParser;
00629 };  // class Binary
00630 
00631 /*==============================================================================
00632  * Ternary is a subclass of Binary, holding three subexpressions
00633  *============================================================================*/
00634 class Ternary : public Binary {
00635         Exp*        subExp3;    // Third subexpression pointer
00636 
00637         // Constructor, with operator
00638                     Ternary(OPER op);
00639 
00640 public:
00641         // Constructor, with operator and subexpressions
00642                     Ternary(OPER op, Exp* e1, Exp* e2, Exp* e3);
00643         // Copy constructor
00644                     Ternary(Ternary& o);
00645 
00646         // Clone
00647 virtual Exp*        clone();
00648 
00649         // Compare
00650 virtual bool         operator==(const Exp& o) const ;
00651 virtual bool         operator< (const Exp& o) const ;
00652 virtual bool         operator*=(Exp& o);
00653 
00654         // Destructor
00655 virtual             ~Ternary();
00656 
00657         // Arity
00658         int             getArity() {return 3;}
00659 
00660         // Print
00661 virtual void         print(std::ostream& os, bool html = false);
00662 virtual void         printr(std::ostream& os, bool html = false);
00663 virtual void         appendDotFile(std::ofstream& of);
00664 virtual void         printx(int ind);
00665 
00666         // Set third subexpression
00667         void        setSubExp3(Exp* e);
00668         // Get third subexpression
00669         Exp*        getSubExp3();
00670         // Get a reference to subexpression 3
00671         Exp*&       refSubExp3();
00672 
00673         // Search children
00674         void        doSearchChildren(Exp* search, std::list<Exp**>& li, bool once);
00675 
00676 virtual Exp*        polySimplify(bool& bMod);
00677         Exp*        simplifyArith();
00678         Exp*        simplifyAddr();
00679 
00680         // Type analysis
00681 virtual Exp* genConstraints(Exp* restrictTo);
00682 
00683         // Visitation
00684 virtual bool    accept(ExpVisitor* v);
00685 virtual Exp*    accept(ExpModifier* v);
00686 
00687 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00688 
00689 virtual Type*   ascendType();
00690 virtual void    descendType(Type* parentType, bool& ch, Statement* s);
00691 
00692 protected:
00693         friend class XMLProgParser;
00694 };  // class Ternary
00695 
00696 /*==============================================================================
00697  * TypedExp is a subclass of Unary, holding one subexpression and a Type
00698  *============================================================================*/
00699 class TypedExp : public Unary {
00700         Type            *type;
00701 public:
00702         // Constructor
00703                         TypedExp();
00704         // Constructor, subexpression
00705                         TypedExp(Exp* e1);
00706         // Constructor, type, and subexpression.
00707         // A rare const parameter allows the common case of providing a temporary,
00708         // e.g. foo = new TypedExp(Type(INTEGER), ...);
00709                         TypedExp(Type* ty, Exp* e1);
00710         // Copy constructor
00711                         TypedExp(TypedExp& o);
00712 
00713         // Clone
00714 virtual Exp* clone();
00715 
00716         // Compare
00717 virtual bool        operator==(const Exp& o) const;
00718 virtual bool        operator< (const Exp& o) const;
00719 virtual bool        operator<<(const Exp& o) const;
00720 virtual bool        operator*=(Exp& o);
00721 
00722 
00723 virtual void        print(std::ostream& os, bool html = false);
00724 virtual void        appendDotFile(std::ofstream& of);
00725 virtual void        printx(int ind);
00726 
00727         // Get and set the type
00728 virtual Type*       getType() {return type;}
00729 virtual void        setType(Type* ty) {type = ty;}
00730 
00731         // polySimplify
00732 virtual Exp*        polySimplify(bool& bMod);
00733 
00734         // Visitation
00735 virtual bool        accept(ExpVisitor* v);
00736 virtual Exp*        accept(ExpModifier* v);
00737 
00738 virtual Type*       ascendType();
00739 virtual void        descendType(Type* parentType, bool& ch, Statement* s);
00740 
00741 protected:
00742         friend class XMLProgParser;
00743 };      // class TypedExp
00744 
00745 /*==============================================================================
00746  * FlagDef is a subclass of Unary, and holds a list of parameters (in the subexpression), and a pointer to an RTL
00747  *============================================================================*/
00748 class FlagDef : public Unary {
00749         RTL*        rtl;
00750 public:
00751                     FlagDef(Exp* params, RTL* rtl);     // Constructor
00752 virtual             ~FlagDef();                         // Destructor
00753 virtual void        appendDotFile(std::ofstream& of);
00754         RTL*        getRtl() { return rtl; }
00755         void        setRtl(RTL* r) { rtl = r; }
00756 
00757         // Visitation
00758 virtual bool        accept(ExpVisitor* v);
00759 virtual Exp*        accept(ExpModifier* v);
00760 
00761 protected:
00762         friend class XMLProgParser;
00763 };      // class FlagDef
00764 
00765 /*==============================================================================
00766  * RefExp is a subclass of Unary, holding an ordinary Exp pointer, and a pointer to a defining statement (could be a
00767  * phi assignment).  This is used for subscripting SSA variables. Example:
00768  *   m[1000] becomes m[1000]{3} if defined at statement 3
00769  * The integer is really a pointer to the definig statement, printed as the statement number for compactness.
00770  *============================================================================*/
00771 class RefExp : public Unary {
00772         Statement*  def;                // The defining statement
00773 
00774 public:
00775         // Constructor with expression (e) and statement defining it (def)
00776                     RefExp(Exp* e, Statement* def);
00777         //RefExp(Exp* e);
00778         //RefExp(RefExp& o);
00779 virtual Exp*        clone();
00780 virtual bool        operator==(const Exp& o) const;
00781 virtual bool        operator< (const Exp& o) const;
00782 virtual bool        operator*=(Exp& o);
00783 
00784 virtual void        print(std::ostream& os, bool html = false);
00785 virtual void        printx(int ind);
00786 //virtual int       getNumRefs() {return 1;}
00787         Statement*  getDef() {return def;}      // Ugh was called getRef()
00788         Exp*        addSubscript(Statement* def) {this->def = def; return this;}
00789         void        setDef(Statement* def) {this->def = def;}
00790 virtual Exp*        genConstraints(Exp* restrictTo);
00791         bool        references(Statement* s) {return def == s;}
00792 virtual Exp*        polySimplify(bool& bMod);
00793 virtual Exp         *match(Exp *pattern);
00794 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00795 
00796         // Before type analysis, implicit definitions are NULL.  During and after TA, they point to an implicit
00797         // assignment statement.  Don't implement here, since it would require #including of statement.h
00798         bool        isImplicitDef();
00799 
00800         // Visitation
00801 virtual bool        accept(ExpVisitor* v);
00802 virtual Exp*        accept(ExpModifier* v);
00803 
00804 virtual Type*       ascendType();
00805 virtual void        descendType(Type* parentType, bool& ch, Statement* s);
00806 
00807 protected:
00808                     RefExp() : Unary(opSubscript), def(NULL) { }
00809         friend class XMLProgParser;
00810 };      // class RefExp
00811 
00812 
00813 /*==============================================================================
00814 class TypeVal. Just a Terminal with a Type. Used for type values in constraints
00815 ==============================================================================*/
00816 class TypeVal : public Terminal {
00817         Type*       val;
00818 
00819 public:
00820                     TypeVal(Type* ty);
00821                     ~TypeVal();
00822 
00823 virtual Type*       getType() {return val;}
00824 virtual void        setType(Type* t) {val = t;}
00825 virtual Exp*        clone();
00826 virtual bool        operator==(const Exp& o) const;
00827 virtual bool        operator< (const Exp& o) const;
00828 virtual bool        operator*=(Exp& o);
00829 virtual void        print(std::ostream& os, bool html = false);
00830 virtual void        printx(int ind);
00831 virtual Exp*        genConstraints(Exp* restrictTo) {
00832                         assert(0); return NULL;} // Should not be constraining constraints
00833 //virtual Exp       *match(Exp *pattern);
00834 
00835         // Visitation
00836 virtual bool        accept(ExpVisitor* v);
00837 virtual Exp*        accept(ExpModifier* v);
00838 
00839 protected:
00840         friend class XMLProgParser;
00841 };  // class TypeVal
00842 
00843 class Location : public Unary {
00844 protected:
00845         UserProc    *proc;
00846 
00847 public:
00848         // Constructor with ID, subexpression, and UserProc*
00849                     Location(OPER op, Exp* e, UserProc *proc);
00850         // Copy constructor
00851                     Location(Location& o);
00852         // Custom constructor
00853 static Location*    regOf(int r) {return new Location(opRegOf, new Const(r), NULL);}
00854 static Location*    regOf(Exp *e) {return new Location(opRegOf, e, NULL);}
00855 static Location*    memOf(Exp *e, UserProc* p = NULL) {return new Location(opMemOf, e, p);}
00856 static Location*    tempOf(Exp* e) {return new Location(opTemp, e, NULL);}
00857 static Location*    global(const char *nam, UserProc *p) {
00858                         return new Location(opGlobal, new Const((char*)nam), p);}
00859 static Location*    local(const char *nam, UserProc *p);
00860 static Location*    param(const char *nam, UserProc *p = NULL) {
00861                         return new Location(opParam, new Const((char*)nam), p);}
00862         // Clone
00863 virtual Exp*        clone();
00864 
00865         void        setProc(UserProc *p) { proc = p; }
00866         UserProc    *getProc() { return proc; }
00867 
00868 virtual Exp*        polySimplify(bool& bMod);
00869 virtual void        getDefinitions(LocationSet& defs);
00870 
00871         // Visitation
00872 virtual bool        accept(ExpVisitor* v);
00873 virtual Exp*        accept(ExpModifier* v);
00874 virtual bool        match(const char *pattern, std::map<std::string, Exp*> &bindings);
00875 
00876 protected:
00877         friend class XMLProgParser;
00878                     Location(OPER op) : Unary(op), proc(NULL) { }
00879 };  // class Location
00880     
00881 #endif // __EXP_H__

Generated on Tue Sep 19 21:18:23 2006 for Boomerang by  doxygen 1.4.6