00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #ifndef _HLLCODE_H_
00024 #define _HLLCODE_H_
00025 
00026 #include <iostream>
00027 #include <vector>
00028 #include <assert.h>
00029 #include <statement.h>      
00030 
00031 class BasicBlock;
00032 typedef BasicBlock *PBB;
00033 class Exp;
00034 class UserProc;
00035 class Proc;
00036 class Type;
00037 class Signature;
00038 class Assign;
00039 class LocationSet;
00040 class CallStatement;
00041 
00042 class ReturnStatement;
00043 
00044 class HLLCode {
00045 protected:
00046         UserProc *m_proc;       
00047 
00048 public:
00049         
00050                 HLLCode() { }
00051                 HLLCode(UserProc *p) : m_proc(p) { }
00052 
00053     
00054 virtual         ~HLLCode() { }
00055 
00056     
00057 virtual void    reset() {
00058         }
00059 
00060         
00061         UserProc *getProc() { return m_proc; }
00062         void    setProc(UserProc *p) { m_proc = p; }
00063 
00064         
00065 
00066 
00067 
00068         
00069 virtual void    AddPretestedLoopHeader(int indLevel, Exp *cond) = 0;
00070 virtual void    AddPretestedLoopEnd(int indLevel) = 0;
00071 
00072         
00073 virtual void    AddEndlessLoopHeader(int indLevel) = 0;
00074 virtual void    AddEndlessLoopEnd(int indLevel) = 0;
00075 
00076         
00077 virtual void    AddPosttestedLoopHeader(int indLevel) = 0;
00078 virtual void    AddPosttestedLoopEnd(int indLevel, Exp *cond) = 0;
00079 
00080         
00081 virtual void    AddCaseCondHeader(int indLevel, Exp *cond) = 0;
00082 virtual void    AddCaseCondOption(int indLevel, Exp *opt) = 0;
00083 virtual void    AddCaseCondOptionEnd(int indLevel) = 0;
00084 virtual void    AddCaseCondElse(int indLevel) = 0;
00085 virtual void    AddCaseCondEnd(int indLevel) = 0;
00086 
00087         
00088 virtual void    AddIfCondHeader(int indLevel, Exp *cond) = 0;
00089 virtual void    AddIfCondEnd(int indLevel) = 0;
00090 
00091         
00092 virtual void    AddIfElseCondHeader(int indLevel, Exp *cond) = 0;
00093 virtual void    AddIfElseCondOption(int indLevel) = 0;
00094 virtual void    AddIfElseCondEnd(int indLevel) = 0;
00095 
00096         
00097 virtual void    AddGoto(int indLevel, int ord) = 0;
00098 virtual void    AddBreak(int indLevel) = 0;
00099 virtual void    AddContinue(int indLevel) = 0;
00100 
00101         
00102 virtual void    AddLabel(int indLevel, int ord) = 0;
00103 virtual void    RemoveLabel(int ord) = 0;
00104 virtual void    RemoveUnusedLabels(int maxOrd) = 0;
00105 
00106         
00107 virtual void    AddAssignmentStatement(int indLevel, Assign *s) = 0;
00108 virtual void    AddCallStatement(int indLevel, Proc *proc, const char *name, StatementList &args,
00109                     StatementList* results) = 0;
00110 virtual void    AddIndCallStatement(int indLevel, Exp *exp, StatementList& args, StatementList* results) = 0;
00111 virtual void    AddReturnStatement(int indLevel, StatementList* rets) = 0;
00112 
00113         
00114 virtual void    AddProcStart(UserProc* proc) = 0;
00115 virtual void    AddProcEnd() = 0;
00116 virtual void    AddLocal(const char *name, Type *type, bool last = false) = 0;
00117 virtual void    AddGlobal(const char *name, Type *type, Exp *init = NULL) = 0;
00118 virtual void    AddPrototype(UserProc* proc) = 0;
00119 
00120     
00121 virtual void    AddLineComment(char* cmt) = 0;
00122     
00123         
00124 
00125 
00126 virtual void    print(std::ostream &os) = 0;
00127 };      
00128 
00129 class SyntaxNode {
00130 protected:
00131         PBB     pbb;
00132         int     nodenum;
00133         int     score;
00134         SyntaxNode *correspond; 
00135         bool    notGoto;
00136         int     depth;
00137 
00138 public:
00139                 SyntaxNode();
00140 virtual         ~SyntaxNode();
00141 
00142 virtual bool    isBlock() { return false; }
00143 virtual bool    isGoto();
00144 virtual bool    isBranch();
00145 
00146 virtual void    ignoreGoto() { };
00147 
00148 virtual int     getNumber() { return nodenum; }
00149 
00150         PBB     getBB() { return pbb; }
00151         void    setBB(PBB bb) { pbb = bb; }
00152 
00153 virtual int     getNumOutEdges() = 0;
00154 virtual SyntaxNode *getOutEdge(SyntaxNode *root, int n) = 0;
00155 virtual bool    endsWithGoto() = 0;
00156 virtual bool    startsWith(SyntaxNode *node) { return this == node; }
00157 
00158 virtual SyntaxNode *getEnclosingLoop(SyntaxNode *pFor, 
00159                                          SyntaxNode *cur = NULL) = 0;
00160 
00161         int     getScore();
00162         void    addToScore(int n) { score = getScore() + n; }
00163         void    setDepth(int n) { depth = n; }
00164         int     getDepth() { return depth; }
00165 
00166 virtual SyntaxNode *clone() = 0;
00167 virtual SyntaxNode *replace(SyntaxNode *from, SyntaxNode *to) = 0;
00168         SyntaxNode *getCorrespond() { return correspond; }
00169 
00170 virtual SyntaxNode *findNodeFor(PBB bb) = 0;
00171 virtual void    printAST(SyntaxNode *root, std::ostream &os) = 0;
00172 virtual int     evaluate(SyntaxNode *root) = 0;
00173 virtual void    addSuccessors(SyntaxNode *root, std::vector<SyntaxNode*> &successors) { }
00174 };      
00175 
00176 class BlockSyntaxNode : public SyntaxNode {
00177 private:
00178     std::vector<SyntaxNode*> statements;
00179 
00180 public:
00181     BlockSyntaxNode();
00182     virtual ~BlockSyntaxNode();
00183 
00184     virtual bool isBlock() { return pbb == NULL; }
00185 
00186     virtual void ignoreGoto() {
00187         if (pbb) notGoto = true;
00188         else if (statements.size() > 0)
00189             statements[statements.size()-1]->ignoreGoto();
00190     }
00191 
00192     int getNumStatements() { 
00193         return pbb ? 0 : statements.size(); 
00194     }
00195     SyntaxNode *getStatement(int n) {
00196         assert(pbb == NULL);
00197         return statements[n]; 
00198     }
00199     void prependStatement(SyntaxNode *n) {
00200         assert(pbb == NULL);
00201         statements.resize(statements.size()+1);
00202         for (int i = statements.size()-1; i > 0;  i--)
00203             statements[i] = statements[i-1];
00204         statements[0] = n;
00205     }
00206     void addStatement(SyntaxNode *n) { 
00207         assert(pbb == NULL);
00208         statements.push_back(n); 
00209     }
00210     void setStatement(int i, SyntaxNode *n) { 
00211         assert(pbb == NULL);
00212         statements[i] = n; 
00213     }
00214 
00215 virtual int getNumOutEdges();
00216 virtual SyntaxNode *getOutEdge(SyntaxNode *root, int n);
00217 virtual bool endsWithGoto() {
00218             if (pbb) return isGoto();
00219             bool last = false;
00220             if (statements.size() > 0)
00221                 last = statements[statements.size()-1]->endsWithGoto();
00222             return last;
00223         }
00224 virtual bool    startsWith(SyntaxNode *node) { 
00225             return this == node || (statements.size() > 0 && statements[0]->startsWith(node)); 
00226         }
00227 virtual SyntaxNode *getEnclosingLoop(SyntaxNode *pFor, SyntaxNode *cur = NULL) {
00228             if (this == pFor) return cur;
00229             for (unsigned i = 0; i < statements.size(); i++) {
00230                 SyntaxNode *n = statements[i]->getEnclosingLoop(pFor, cur);
00231                 if (n) return n;
00232             }
00233             return NULL;
00234         }
00235 
00236 virtual SyntaxNode *clone();
00237 virtual SyntaxNode *replace(SyntaxNode *from, SyntaxNode *to);
00238 
00239 virtual SyntaxNode *findNodeFor(PBB bb);
00240 virtual void    printAST(SyntaxNode *root, std::ostream &os);
00241 virtual int     evaluate(SyntaxNode *root);
00242 virtual void    addSuccessors(SyntaxNode *root, std::vector<SyntaxNode*> &successors);
00243 };      
00244 
00245 class IfThenSyntaxNode : public SyntaxNode {
00246 protected:
00247         SyntaxNode *pThen;
00248         Exp     *cond;
00249 
00250 public:
00251                 IfThenSyntaxNode();
00252 virtual         ~IfThenSyntaxNode();
00253 
00254 virtual bool    isGoto() { return false; }
00255 virtual bool    isBranch() { return false; }
00256 
00257 virtual int     getNumOutEdges() {
00258         return 1;
00259     }
00260 virtual SyntaxNode *getOutEdge(SyntaxNode *root, int n);
00261 virtual bool    endsWithGoto() { return false; }
00262 
00263 virtual SyntaxNode *clone();
00264 virtual SyntaxNode *replace(SyntaxNode *from, SyntaxNode *to);
00265 
00266 virtual SyntaxNode *getEnclosingLoop(SyntaxNode *pFor, SyntaxNode *cur = NULL) {
00267             if (this == pFor) return cur;
00268             return pThen->getEnclosingLoop(pFor, cur);
00269         }
00270 
00271         void    setCond(Exp *e) { cond = e; }
00272         Exp     *getCond() { return cond; }
00273         void    setThen(SyntaxNode *n) { pThen = n; }
00274 
00275 virtual SyntaxNode *findNodeFor(PBB bb);
00276 virtual void    printAST(SyntaxNode *root, std::ostream &os);
00277 virtual int     evaluate(SyntaxNode *root);
00278 virtual void    addSuccessors(SyntaxNode *root, std::vector<SyntaxNode*> &successors);
00279 };      
00280 
00281 class IfThenElseSyntaxNode : public SyntaxNode {
00282 protected:
00283         SyntaxNode *pThen;
00284         SyntaxNode *pElse;
00285         Exp     *cond;
00286 
00287 public:
00288                 IfThenElseSyntaxNode();
00289 virtual         ~IfThenElseSyntaxNode();
00290 virtual bool    isGoto() { return false; }
00291 virtual bool    isBranch() { return false; }
00292 
00293 virtual int     getNumOutEdges() {
00294             return 1;
00295         }
00296 virtual SyntaxNode *getOutEdge(SyntaxNode *root, int n) {
00297             SyntaxNode *o = pThen->getOutEdge(root, 0);
00298             assert(o == pElse->getOutEdge(root, 0));
00299             return o;
00300         }
00301 virtual bool    endsWithGoto() { return false; }
00302 
00303 virtual SyntaxNode *getEnclosingLoop(SyntaxNode *pFor, SyntaxNode *cur = NULL) {
00304             if (this == pFor) return cur;
00305             SyntaxNode *n = pThen->getEnclosingLoop(pFor, cur);
00306             if (n) return n;
00307             return pElse->getEnclosingLoop(pFor, cur);
00308         }
00309 
00310 
00311 virtual SyntaxNode *clone();
00312 virtual SyntaxNode *replace(SyntaxNode *from, SyntaxNode *to);
00313 
00314         void    setCond(Exp *e) { cond = e; }
00315         void    setThen(SyntaxNode *n) { pThen = n; }
00316         void    setElse(SyntaxNode *n) { pElse = n; }
00317 
00318 virtual SyntaxNode *findNodeFor(PBB bb);
00319 virtual void printAST(SyntaxNode *root, std::ostream &os);
00320 virtual int evaluate(SyntaxNode *root);
00321 virtual void addSuccessors(SyntaxNode *root,
00322                                std::vector<SyntaxNode*> &successors);
00323 };      
00324 
00325 class PretestedLoopSyntaxNode : public SyntaxNode {
00326 protected:
00327         SyntaxNode *pBody;
00328         Exp     *cond;
00329 
00330 public:
00331                 PretestedLoopSyntaxNode();
00332 virtual         ~PretestedLoopSyntaxNode();
00333 virtual bool    isGoto() { return false; }
00334 virtual bool    isBranch() { return false; }
00335 
00336 virtual int     getNumOutEdges() {
00337         return 1;
00338     }
00339 virtual SyntaxNode *getOutEdge(SyntaxNode *root, int n);
00340 virtual bool    endsWithGoto() { return false; }
00341 virtual SyntaxNode *getEnclosingLoop(SyntaxNode *pFor, SyntaxNode *cur = NULL) {
00342             if (this == pFor) return cur;
00343             return pBody->getEnclosingLoop(pFor, this);
00344         }
00345 
00346 virtual SyntaxNode *clone();
00347 virtual SyntaxNode *replace(SyntaxNode *from, SyntaxNode *to);
00348 
00349         void    setCond(Exp *e) { cond = e; }
00350         void    setBody(SyntaxNode *n) { pBody = n; }
00351 
00352 virtual SyntaxNode *findNodeFor(PBB bb);
00353 virtual void    printAST(SyntaxNode *root, std::ostream &os);
00354 virtual int     evaluate(SyntaxNode *root);
00355 virtual void    addSuccessors(SyntaxNode *root, std::vector<SyntaxNode*> &successors);
00356 };      
00357 
00358 class PostTestedLoopSyntaxNode : public SyntaxNode {
00359 protected:
00360         SyntaxNode *pBody;
00361         Exp     *cond;
00362 
00363 public:
00364                     PostTestedLoopSyntaxNode();
00365 virtual ~PostTestedLoopSyntaxNode();
00366 virtual bool    isGoto() { return false; }
00367 virtual bool    isBranch() { return false; }
00368 
00369 virtual int     getNumOutEdges() {
00370             return 1;
00371         }
00372 virtual SyntaxNode *getOutEdge(SyntaxNode *root, int n);
00373 virtual bool    endsWithGoto() { return false; }
00374 virtual SyntaxNode *getEnclosingLoop(SyntaxNode *pFor, SyntaxNode *cur = NULL) {
00375             if (this == pFor) return cur;
00376             return pBody->getEnclosingLoop(pFor, this);
00377         }
00378 
00379 virtual SyntaxNode *clone();
00380 virtual SyntaxNode *replace(SyntaxNode *from, SyntaxNode *to);
00381 
00382         void    setCond(Exp *e) { cond = e; }
00383         void    setBody(SyntaxNode *n) { pBody = n; }
00384 
00385 virtual SyntaxNode *findNodeFor(PBB bb);
00386 virtual void    printAST(SyntaxNode *root, std::ostream &os);
00387 virtual int     evaluate(SyntaxNode *root);
00388 virtual void    addSuccessors(SyntaxNode *root, std::vector<SyntaxNode*> &successors);
00389 };      
00390 
00391 class InfiniteLoopSyntaxNode : public SyntaxNode {
00392 protected:
00393         SyntaxNode *pBody;
00394 
00395 public:
00396                 InfiniteLoopSyntaxNode();
00397 virtual         ~InfiniteLoopSyntaxNode();
00398 virtual bool    isGoto() { return false; }
00399 virtual bool    isBranch() { return false; }
00400 
00401 virtual int getNumOutEdges() {
00402             return 0;
00403         }
00404 virtual SyntaxNode *getOutEdge(SyntaxNode *root, int n) {
00405             return NULL;
00406         }
00407 virtual bool endsWithGoto() { return false; }
00408 virtual SyntaxNode *getEnclosingLoop(SyntaxNode *pFor, SyntaxNode *cur = NULL) {
00409             if (this == pFor) return cur;
00410             return pBody->getEnclosingLoop(pFor, this);
00411         }
00412 
00413 virtual SyntaxNode *clone();
00414 virtual SyntaxNode *replace(SyntaxNode *from, SyntaxNode *to);
00415 
00416         void    setBody(SyntaxNode *n) { pBody = n; }
00417 
00418 virtual SyntaxNode *findNodeFor(PBB bb);
00419 virtual void    printAST(SyntaxNode *root, std::ostream &os);
00420 virtual int     evaluate(SyntaxNode *root);
00421 virtual void    addSuccessors(SyntaxNode *root, std::vector<SyntaxNode*> &successors);
00422 };      
00423 
00424 #endif
00425