Nearly CC
An educational compiler skeleton
loopvars.h
1 #ifndef LOOPVARS_H
2 #define LOOPVARS_H
3 
4 #include <utility>
5 #include <map>
6 #include <vector>
7 #include "ast_visitor.h"
8 class Symbol;
9 
10 // Produce a ranking of local variables in a function definition by estimating
11 // how frequently they are used, with the idea that the more deeply nested
12 // the loop, the more run-time use can be expected. This visitor should
13 // be applied to a single function-definition node.
14 //
15 // This ranking can be used later to assign callee-saved registers to
16 // some local variables
17 class LoopVars : public ASTVisitor {
18 public:
19  typedef std::pair<int, Symbol *> RankedVar;
20 
21 private:
22  int m_depth;
23  std::map<Symbol *, RankedVar> m_ranked_var_map;
24 
25 public:
26  LoopVars();
27  virtual ~LoopVars();
28 
29  std::vector<RankedVar> get_ranked_vars() const;
30 
31  virtual void visit_while_statement(Node *n);
32  virtual void visit_do_while_statement(Node *n);
33  virtual void visit_for_statement(Node *n);
34  virtual void visit_variable_ref(Node *n);
35 };
36 
37 #endif // LOOPVARS_H
Base class for AST visitors.
Definition: ast_visitor.h:7
Definition: loopvars.h:17
virtual void visit_variable_ref(Node *n)
Visit a Node with the AST_VARIABLE_REF tag value.
Definition: loopvars.cpp:77
virtual void visit_while_statement(Node *n)
Visit a Node with the AST_WHILE_STATEMENT tag value.
Definition: loopvars.cpp:46
virtual void visit_for_statement(Node *n)
Visit a Node with the AST_FOR_STATEMENT tag value.
Definition: loopvars.cpp:66
virtual void visit_do_while_statement(Node *n)
Visit a Node with the AST_DO_WHILE_STATEMENT tag value.
Definition: loopvars.cpp:56
Definition: node.h:35
Definition: symbol.h:28