Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

automaton.h

Go to the documentation of this file.
00001 //
00002 // File: automaton.h
00003 //
00004 // Purpose: Implements the many-to-one rule automaton class used to match insn patterns
00005 //          in the LEGO DDG.
00006 //
00007 // Author: Mark C. Toburen
00008 //
00009 // History:
00010 //    Created - 7/30/01
00011 //
00012 
00013 #ifndef _AUTOMATON_H_
00014 #define _AUTOMATON_H_
00015 
00016 #include "typedefs.h"
00017 #include "succ_node.h"
00018 #include "belong.h"
00019 #include "tree_node.h"
00020 
00021 class Automaton {
00022 private:
00023    map<string, TreeNode> ParseTree;
00024 
00025 public:
00026 
00027    // default constructor
00028    Automaton () {}
00029 
00030    // constructor
00031    Automaton (map<string, vector<insn> > rules) {
00032       TreeIter find, end;
00033       RuleIter rule;
00034       InsnVecIter vec;
00035       TreeNode *tnode;
00036 
00037       // Build the parse tree.
00038       for (rule = rules.begin(); rule != rules.end(); rule++) {
00039 
00040          // skip the blank rule that results from the last '/' in the rules file
00041          if (rule->first == "") continue;
00042          //cout << rule->first << endl;
00043 
00044          // For each op in the rule:
00045          //   1. If the op hasn't been inserted into the ParseTree map
00046          //      a. create a new TreeNode for this op
00047          //      b. insert the TreeNode into the ParseTree map with the op name as its key
00048          //   2. If the op exists in the ParseTree
00049          //      a. insert a new "belong_to" entry for this op & rule
00050          for (vec = (rule->second).begin() + 1; vec != (rule->second).end(); vec++) {
00051 
00052             // See if the current op in the rule is in the ParseTree
00053             find = ParseTree.find(vec->GetName());
00054             end = ParseTree.end();
00055             if (find == end) {
00056                //cerr << ">> Automaton: inserting op: " << vec->GetName() << endl;
00057                // Create a new TreeNode and insert it into the ParseTree.
00058                tnode = new TreeNode(rule, vec);
00059                ParseTree.insert(pair<string, TreeNode>(vec->GetName(), *tnode));
00060             } else {
00061                // insert a new "belong_to" entry for this op & rule
00062                (find->second).InsertBelong(rule, vec);
00063             }
00064          }
00065       }
00066       SetSuccessorParents();
00067    }
00068 
00069    // set the parent parse tree node of all the successors in the tree
00070    void SetSuccessorParents () {
00071       TreeIter titer, find, end = ParseTree.end();
00072       BelongIter biter;
00073       SuccNode *succ;
00074 
00075       for (titer = ParseTree.begin(); titer != ParseTree.end(); titer++) {
00076          for (biter = (titer->second).GetBelongTo().begin(); biter != (titer->second).GetBelongTo().end(); biter++) {
00077             if (!(succ = (biter->second).GetSuccessor()))
00078                continue;   // this op has no successor for this rule
00079             find = ParseTree.find(succ->GetOp().GetName());
00080             if (find == end)
00081                cerr << ">> SetSuccessorParents: cannot find successor op in ParseTree." << endl;
00082             else {
00083                succ->SetParentNode(&(find->second));
00084             }
00085          }
00086       }
00087    }
00088 
00089    // search the parse tree for a start node with name OPNAME
00090    TreeNode *FindStartOp (string opname) {
00091       TreeIter find;
00092       BelongIter biter;
00093 
00094       find = ParseTree.find(opname);
00095       if (find != ParseTree.end()) {
00096          for (biter = (find->second).GetBelongTo().begin(); biter != (find->second).GetBelongTo().end(); biter++) {
00097             if ((biter->second).IsStartOp())
00098                return &(find->second);
00099             continue;
00100          }
00101       } else
00102          return NULL;
00103    }
00104 
00105    void PrintAutomaton () {
00106       TreeIter titer;
00107       BelongIter biter;
00108       SuccNode *succ;
00109 
00110       cout << "Printing automaton ... " << endl;
00111       for (titer = ParseTree.begin(); titer != ParseTree.end(); titer++) {
00112          for (biter = (titer->second).GetBelongTo().begin(); biter != (titer->second).GetBelongTo().end(); biter++) {
00113             cout << biter->first << "\t" << (titer->second).GetOp().GetName() << "\t" << (biter->second).IsStartOp();
00114             if ((biter->second).IsStartOp() == true)
00115                cout << "\t" << (biter->second).GetMacroInsn().GetName();
00116             succ = (biter->second).GetSuccessor();
00117             if (succ != NULL)
00118                cout << "\t" << succ->GetOp().GetName() << "(" << succ->GetParentNode()->GetOp().GetName() << ")\t" << succ->IsLeafNode();
00119             cout << endl;
00120          }
00121       }
00122       cout << "done." << endl;
00123    }
00124 };
00125 
00126 #endif // _AUTOMATON_H_

Generated on Mon Jul 21 20:23:59 2003 for TINKER LEGO DOC by doxygen 1.3.2