gcc config
[prop.git] / prop-src / rwgen.h
blobc08959392f4a26edec3f80423850d995098d68c8
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // This file describes the interface of the rewriting compiler.
4 //
5 ///////////////////////////////////////////////////////////////////////////////
6 #ifndef rewriting_compiler_h
7 #define rewriting_compiler_h
9 #include "matchcom.h"
10 #include "classdef.h"
11 #include "ast.h"
12 #include "hashtab.h"
13 #include <AD/automata/treegram.h>
15 class FunctorMap;
16 class TRS;
18 ///////////////////////////////////////////////////////////////////////////////
20 // Class to represent a rewrite class
22 ///////////////////////////////////////////////////////////////////////////////
23 class RewriteClass : public ClassDefinition
24 { RewriteClass(const RewriteClass&);
25 void operator = (const RewriteClass&);
27 HashTable index_info;
28 public:
29 RewriteClass(Id, Protocols, Inherits, TyQual, Decls);
30 ~RewriteClass();
31 virtual void gen_class_interface (CodeGen&);
34 ///////////////////////////////////////////////////////////////////////////////
36 // Class to represent indexing information
38 ///////////////////////////////////////////////////////////////////////////////
39 class RewriteIndexing : public Loc
41 RewriteIndexing(const RewriteIndexing&);
42 void operator = (const RewriteIndexing&);
43 public:
44 Ty ty; // type in question
45 Id name; // name of index
46 Bool external; // external index?
47 public:
48 RewriteIndexing(Ty, Id, Bool);
49 ~RewriteIndexing();
52 ///////////////////////////////////////////////////////////////////////////////
54 // The rewriting compiler is inherited from the pattern matcher.
56 ///////////////////////////////////////////////////////////////////////////////
57 class RewritingCompiler : virtual public CodeGen,
58 virtual public MatchCompiler
60 RewritingCompiler(const RewritingCompiler&); // no copy constructor
61 void operator = (const RewritingCompiler&); // no assignment
63 private:
65 friend class RewriteClass;
66 friend class RewriteMix;
67 friend class TRS;
69 Decls rewriters; // outstanding rewriting declarations.
70 FunctorMap * Fmap; // functormap;
71 RewriteIndexings indices; // current set of indices
72 TRS * trs; // for partial evaluation
73 static HashTable rewrite_classes; // all the rewrite classes declared
75 public:
76 ////////////////////////////////////////////////////////////////////////////
77 // Constructor and destructor
78 ////////////////////////////////////////////////////////////////////////////
79 RewritingCompiler();
80 virtual ~RewritingCompiler();
82 typedef TreeGrammar::Functor Functor;
84 protected:
85 ////////////////////////////////////////////////////////////////////////////
86 // Methods for compiling rewriting
87 ////////////////////////////////////////////////////////////////////////////
89 // generate a set of rewrite rules
90 void gen_rewrite (Id, RewriteIndexings, MatchRules);
92 // generate a rewrite statement
93 void gen_rewriting(Protocols, Exp, Exp,RewriteIndexings,MatchRules,TyQual);
95 // generate a set of outstanding rewrite statements
96 void gen_rewriters();
98 // generate a replacement statement
99 void gen_replacement (Exp, MatchRuleInfo::RewritingMode);
101 // generate a cut/replacement statement
102 void gen_cutreplacement (Exp, MatchRuleInfo::RewritingMode);
104 // generate a failrewrite statement
105 void gen_failrewrite (MatchRuleInfo::RewritingMode);
107 virtual void optimize_rewrite () = 0;
109 private:
110 ////////////////////////////////////////////////////////////////////////////
111 // Methods for compiling a bottomup tree pattern matcher
112 ////////////////////////////////////////////////////////////////////////////
113 void gen_static_rewriter (FunctorMap&);
114 void gen_dynamic_rewriter (FunctorMap&);
115 void generate_tables (FunctorMap&);
117 ////////////////////////////////////////////////////////////////////////////
118 // Methods for generating static labelers
119 ////////////////////////////////////////////////////////////////////////////
120 void generate_static_labelers(FunctorMap&);
121 void gen_literal_labeler (FunctorMap&, Ty);
122 void gen_datatype_labeler (FunctorMap&, Ty);
124 void gen_before_rules (FunctorMap&, Ty);
125 void gen_preorder_rules (FunctorMap&, Ty);
126 void gen_postorder_rules (FunctorMap&, Ty);
127 void gen_topdown_rules (FunctorMap&, Ty);
128 void gen_auxiliary_rules (FunctorMap&, Ty,MatchRuleInfo::RewritingMode);
130 void gen_bottomup_traversals (FunctorMap&, Ty);
131 void gen_bottomup_traversals (FunctorMap&, Functor, int, Cons[], Ty);
132 void gen_one_traversal (FunctorMap&, Functor, Cons, Ty);
133 void gen_component_traversal (FunctorMap&, Functor, Cons, Ty);
134 void gen_single_component_traversal (FunctorMap&,Cons,Ty);
135 void gen_tuple_component_traversal (FunctorMap&,Cons,int,Tys);
136 void gen_record_component_traversal (FunctorMap&,Cons,int,Bool [],Ids,Tys);
138 ////////////////////////////////////////////////////////////////////////////
139 // Methods for generating dynamic labelers
140 ////////////////////////////////////////////////////////////////////////////
141 void generate_dynamic_labelers (FunctorMap&);
142 void generate_state_record (FunctorMap&);
143 void generate_accept_rules_tables (FunctorMap&);
144 void generate_closures (FunctorMap&);
145 void gen_closure (FunctorMap&, Id, MatchRules);
146 void gen_dynamic_datatype_labeler (FunctorMap&, Ty);
147 void gen_dynamic_traversals (FunctorMap&, Ty);
148 void gen_dynamic_traversals (FunctorMap&, int, Cons[], Ty);
149 void gen_one_dynamic_traversal (FunctorMap&, Cons, Ty);
150 void gen_component_dynamic_traversal (FunctorMap&, Cons, Ty);
151 void gen_single_component_dynamic_traversal (FunctorMap&,Cons,Ty);
152 void gen_tuple_component_dynamic_traversal (FunctorMap&,Cons,int,Tys);
153 void gen_record_component_dynamic_traversal (FunctorMap&,Cons,int,Bool [],Ids,Tys);
154 virtual void gen_treecost_match (Match, const BitSet *, MatchRules);
155 virtual void gen_treelabel_match (Match, Ty, Ty, int);
157 ////////////////////////////////////////////////////////////////////////////
158 // Methods for generating actions.
159 ////////////////////////////////////////////////////////////////////////////
160 void gen_action (FunctorMap&, Ty);
161 void gen_cost_labeler_action (FunctorMap&, Ty, MatchRules, Bool);
162 void gen_costless_guarded_labeler_action (FunctorMap&, MatchRules);
163 void gen_costless_guardless_labeler_action (FunctorMap&, MatchRules);
164 void gen_cost_rewriter_action (FunctorMap&, MatchRules);
165 void gen_costless_guarded_rewriter_action (FunctorMap&, MatchRules);
166 void gen_costless_guardless_rewriter_action (FunctorMap&, MatchRules);
168 ////////////////////////////////////////////////////////////////////////////
169 // Methods for generating reducers.
170 ////////////////////////////////////////////////////////////////////////////
171 void generate_reducers (FunctorMap&);
172 void gen_datatype_reducers (FunctorMap&);
173 void gen_datatype_reducer (FunctorMap&, Ty);
174 int gen_pattern_traversal (FunctorMap&, Id lhs, Pat, int);
175 int gen_pattern_traversal (FunctorMap&, Id lhs, Pats, int);
176 int gen_pattern_traversal (FunctorMap&, Id lhs, LabPats, int);
178 ////////////////////////////////////////////////////////////////////////////
179 // Methods to add and lookup a rewrite class
180 ////////////////////////////////////////////////////////////////////////////
181 friend class FunctorMap;
182 static void add_rewrite_class (RewriteClass *);
183 static RewriteClass * lookup_rewrite_class(Id);
184 static Protocols lookup_protocols(Id);
185 static TyQual lookup_qual(Id);
187 ////////////////////////////////////////////////////////////////////////////
188 // Methods for indices
189 ////////////////////////////////////////////////////////////////////////////
190 const RewriteIndexing * get_index(Ty) const;
191 Bool compute_index(Ty, Id& name, Bool& external) const;
192 Bool has_index(Ty) const;
193 Id gen_get_rewrite_state(Ty, Exp);
194 void gen_get_rewrite_state(Ty, Id);
195 void gen_set_rewrite_state_and_rule(Ty, Id);
196 void gen_get_rewrite_rule(Ty, Id);
198 ////////////////////////////////////////////////////////////////////////////
199 // Methods for tracing
200 ////////////////////////////////////////////////////////////////////////////
201 void gen_trace_macro (Id, FunctorMap&);
204 extern Id redex_name(Ty);
206 #endif