annotate src/share/vm/adlc/dfa.cpp @ 17467:55fb97c4c58d hs25-b65

8029233: Update copyright year to match last edit in jdk8 hotspot repository for 2013 Summary: Copyright year updated for files modified during 2013 Reviewed-by: twisti, iveresov
author mikael
date Tue, 24 Dec 2013 11:48:39 -0800
parents 573cf206e381
children 4ca6dc0799b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17467
55fb97c4c58d 8029233: Update copyright year to match last edit in jdk8 hotspot repository for 2013
mikael
parents: 8878
diff changeset
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 628
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 628
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 628
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // DFA.CPP - Method definitions for outputting the matcher DFA from ADLC
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #include "adlc.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 //---------------------------Switches for debugging output---------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
29 static bool debug_output = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 static bool debug_output1 = false; // top level chain rules
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 //---------------------------Access to internals of class State----------------
a61af66fc99e Initial load
duke
parents:
diff changeset
33 static const char *sLeft = "_kids[0]";
a61af66fc99e Initial load
duke
parents:
diff changeset
34 static const char *sRight = "_kids[1]";
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 //---------------------------DFA productions-----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
37 static const char *dfa_production = "DFA_PRODUCTION";
a61af66fc99e Initial load
duke
parents:
diff changeset
38 static const char *dfa_production_set_valid = "DFA_PRODUCTION__SET_VALID";
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 //---------------------------Production State----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
41 static const char *knownInvalid = "knownInvalid"; // The result does NOT have a rule defined
a61af66fc99e Initial load
duke
parents:
diff changeset
42 static const char *knownValid = "knownValid"; // The result must be produced by a rule
a61af66fc99e Initial load
duke
parents:
diff changeset
43 static const char *unknownValid = "unknownValid"; // Unknown (probably due to a child or predicate constraint)
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 static const char *noConstraint = "noConstraint"; // No constraints seen so far
a61af66fc99e Initial load
duke
parents:
diff changeset
46 static const char *hasConstraint = "hasConstraint"; // Within the first constraint
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 //------------------------------Production------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // Track the status of productions for a particular result
a61af66fc99e Initial load
duke
parents:
diff changeset
51 class Production {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
53 const char *_result;
a61af66fc99e Initial load
duke
parents:
diff changeset
54 const char *_constraint;
a61af66fc99e Initial load
duke
parents:
diff changeset
55 const char *_valid;
a61af66fc99e Initial load
duke
parents:
diff changeset
56 Expr *_cost_lb; // Cost lower bound for this production
a61af66fc99e Initial load
duke
parents:
diff changeset
57 Expr *_cost_ub; // Cost upper bound for this production
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
60 Production(const char *result, const char *constraint, const char *valid);
a61af66fc99e Initial load
duke
parents:
diff changeset
61 ~Production() {};
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 void initialize(); // reset to be an empty container
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 const char *valid() const { return _valid; }
a61af66fc99e Initial load
duke
parents:
diff changeset
66 Expr *cost_lb() const { return (Expr *)_cost_lb; }
a61af66fc99e Initial load
duke
parents:
diff changeset
67 Expr *cost_ub() const { return (Expr *)_cost_ub; }
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 void print();
a61af66fc99e Initial load
duke
parents:
diff changeset
70 };
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72
a61af66fc99e Initial load
duke
parents:
diff changeset
73 //------------------------------ProductionState--------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
74 // Track the status of all production rule results
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // Reset for each root opcode (e.g., Op_RegI, Op_AddI, ...)
a61af66fc99e Initial load
duke
parents:
diff changeset
76 class ProductionState {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
78 Dict _production; // map result of production, char*, to information or NULL
a61af66fc99e Initial load
duke
parents:
diff changeset
79 const char *_constraint;
a61af66fc99e Initial load
duke
parents:
diff changeset
80
a61af66fc99e Initial load
duke
parents:
diff changeset
81 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // cmpstr does string comparisions. hashstr computes a key.
a61af66fc99e Initial load
duke
parents:
diff changeset
83 ProductionState(Arena *arena) : _production(cmpstr, hashstr, arena) { initialize(); };
a61af66fc99e Initial load
duke
parents:
diff changeset
84 ~ProductionState() { };
a61af66fc99e Initial load
duke
parents:
diff changeset
85
a61af66fc99e Initial load
duke
parents:
diff changeset
86 void initialize(); // reset local and dictionary state
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 const char *constraint();
a61af66fc99e Initial load
duke
parents:
diff changeset
89 void set_constraint(const char *constraint); // currently working inside of constraints
a61af66fc99e Initial load
duke
parents:
diff changeset
90
a61af66fc99e Initial load
duke
parents:
diff changeset
91 const char *valid(const char *result); // unknownValid, or status for this production
a61af66fc99e Initial load
duke
parents:
diff changeset
92 void set_valid(const char *result); // if not constrained, set status to knownValid
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 Expr *cost_lb(const char *result);
a61af66fc99e Initial load
duke
parents:
diff changeset
95 Expr *cost_ub(const char *result);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 void set_cost_bounds(const char *result, const Expr *cost, bool has_state_check, bool has_cost_check);
a61af66fc99e Initial load
duke
parents:
diff changeset
97
a61af66fc99e Initial load
duke
parents:
diff changeset
98 // Return the Production associated with the result,
a61af66fc99e Initial load
duke
parents:
diff changeset
99 // or create a new Production and insert it into the dictionary.
a61af66fc99e Initial load
duke
parents:
diff changeset
100 Production *getProduction(const char *result);
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 void print();
a61af66fc99e Initial load
duke
parents:
diff changeset
103
a61af66fc99e Initial load
duke
parents:
diff changeset
104 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
105 // Disable public use of constructor, copy-ctor, ...
a61af66fc99e Initial load
duke
parents:
diff changeset
106 ProductionState( ) : _production(cmpstr, hashstr, Form::arena) { assert( false, "NotImplemented"); };
a61af66fc99e Initial load
duke
parents:
diff changeset
107 ProductionState( const ProductionState & ) : _production(cmpstr, hashstr, Form::arena) { assert( false, "NotImplemented"); }; // Deep-copy
a61af66fc99e Initial load
duke
parents:
diff changeset
108 };
a61af66fc99e Initial load
duke
parents:
diff changeset
109
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 //---------------------------Helper Functions----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // cost_check template:
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // 1) if (STATE__NOT_YET_VALID(EBXREGI) || _cost[EBXREGI] > c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 // 2) DFA_PRODUCTION__SET_VALID(EBXREGI, cmovI_memu_rule, c)
a61af66fc99e Initial load
duke
parents:
diff changeset
115 // 3) }
a61af66fc99e Initial load
duke
parents:
diff changeset
116 //
a61af66fc99e Initial load
duke
parents:
diff changeset
117 static void cost_check(FILE *fp, const char *spaces,
a61af66fc99e Initial load
duke
parents:
diff changeset
118 const char *arrayIdx, const Expr *cost, const char *rule, ProductionState &status) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 bool state_check = false; // true if this production needs to check validity
a61af66fc99e Initial load
duke
parents:
diff changeset
120 bool cost_check = false; // true if this production needs to check cost
a61af66fc99e Initial load
duke
parents:
diff changeset
121 bool cost_is_above_upper_bound = false; // true if this production is unnecessary due to high cost
a61af66fc99e Initial load
duke
parents:
diff changeset
122 bool cost_is_below_lower_bound = false; // true if this production replaces a higher cost production
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // Get information about this production
a61af66fc99e Initial load
duke
parents:
diff changeset
125 const Expr *previous_ub = status.cost_ub(arrayIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 if( !previous_ub->is_unknown() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
127 if( previous_ub->less_than_or_equal(cost) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 cost_is_above_upper_bound = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
129 if( debug_output ) { fprintf(fp, "// Previous rule with lower cost than: %s === %s_rule costs %s\n", arrayIdx, rule, cost->as_string()); }
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132
a61af66fc99e Initial load
duke
parents:
diff changeset
133 const Expr *previous_lb = status.cost_lb(arrayIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
134 if( !previous_lb->is_unknown() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 if( cost->less_than_or_equal(previous_lb) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 cost_is_below_lower_bound = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
137 if( debug_output ) { fprintf(fp, "// Previous rule with higher cost\n"); }
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // line 1)
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // Check for validity and compare to other match costs
a61af66fc99e Initial load
duke
parents:
diff changeset
143 const char *validity_check = status.valid(arrayIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
144 if( validity_check == unknownValid ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 fprintf(fp, "%sif (STATE__NOT_YET_VALID(%s) || _cost[%s] > %s) {\n", spaces, arrayIdx, arrayIdx, cost->as_string());
a61af66fc99e Initial load
duke
parents:
diff changeset
146 state_check = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
147 cost_check = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149 else if( validity_check == knownInvalid ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
150 if( debug_output ) { fprintf(fp, "%s// %s KNOWN_INVALID \n", spaces, arrayIdx); }
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 else if( validity_check == knownValid ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 if( cost_is_above_upper_bound ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 // production cost is known to be too high.
a61af66fc99e Initial load
duke
parents:
diff changeset
155 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
156 } else if( cost_is_below_lower_bound ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 // production will unconditionally overwrite a previous production that had higher cost
a61af66fc99e Initial load
duke
parents:
diff changeset
158 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 fprintf(fp, "%sif ( /* %s KNOWN_VALID || */ _cost[%s] > %s) {\n", spaces, arrayIdx, arrayIdx, cost->as_string());
a61af66fc99e Initial load
duke
parents:
diff changeset
160 cost_check = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
161 }
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163
a61af66fc99e Initial load
duke
parents:
diff changeset
164 // line 2)
a61af66fc99e Initial load
duke
parents:
diff changeset
165 // no need to set State vector if our state is knownValid
a61af66fc99e Initial load
duke
parents:
diff changeset
166 const char *production = (validity_check == knownValid) ? dfa_production : dfa_production_set_valid;
a61af66fc99e Initial load
duke
parents:
diff changeset
167 fprintf(fp, "%s %s(%s, %s_rule, %s)", spaces, production, arrayIdx, rule, cost->as_string() );
a61af66fc99e Initial load
duke
parents:
diff changeset
168 if( validity_check == knownValid ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 if( cost_is_below_lower_bound ) { fprintf(fp, "\t // overwrites higher cost rule"); }
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173 // line 3)
a61af66fc99e Initial load
duke
parents:
diff changeset
174 if( cost_check || state_check ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
175 fprintf(fp, "%s}\n", spaces);
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }
a61af66fc99e Initial load
duke
parents:
diff changeset
177
a61af66fc99e Initial load
duke
parents:
diff changeset
178 status.set_cost_bounds(arrayIdx, cost, state_check, cost_check);
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 // Update ProductionState
a61af66fc99e Initial load
duke
parents:
diff changeset
181 if( validity_check != knownValid ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
182 // set State vector if not previously known
a61af66fc99e Initial load
duke
parents:
diff changeset
183 status.set_valid(arrayIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 //---------------------------child_test----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
189 // Example:
a61af66fc99e Initial load
duke
parents:
diff changeset
190 // STATE__VALID_CHILD(_kids[0], FOO) && STATE__VALID_CHILD(_kids[1], BAR)
a61af66fc99e Initial load
duke
parents:
diff changeset
191 // Macro equivalent to: _kids[0]->valid(FOO) && _kids[1]->valid(BAR)
a61af66fc99e Initial load
duke
parents:
diff changeset
192 //
a61af66fc99e Initial load
duke
parents:
diff changeset
193 static void child_test(FILE *fp, MatchList &mList) {
8878
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
194 if (mList._lchild) { // If left child, check it
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
195 const char* lchild_to_upper = ArchDesc::getMachOperEnum(mList._lchild);
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
196 fprintf(fp, "STATE__VALID_CHILD(_kids[0], %s)", lchild_to_upper);
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
197 delete[] lchild_to_upper;
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
198 }
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
199 if (mList._lchild && mList._rchild) { // If both, add the "&&"
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
200 fprintf(fp, " && ");
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
201 }
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
202 if (mList._rchild) { // If right child, check it
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
203 const char* rchild_to_upper = ArchDesc::getMachOperEnum(mList._rchild);
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
204 fprintf(fp, "STATE__VALID_CHILD(_kids[1], %s)", rchild_to_upper);
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
205 delete[] rchild_to_upper;
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
206 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
207 }
a61af66fc99e Initial load
duke
parents:
diff changeset
208
a61af66fc99e Initial load
duke
parents:
diff changeset
209 //---------------------------calc_cost-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
210 // Example:
a61af66fc99e Initial load
duke
parents:
diff changeset
211 // unsigned int c = _kids[0]->_cost[FOO] + _kids[1]->_cost[BAR] + 5;
a61af66fc99e Initial load
duke
parents:
diff changeset
212 //
a61af66fc99e Initial load
duke
parents:
diff changeset
213 Expr *ArchDesc::calc_cost(FILE *fp, const char *spaces, MatchList &mList, ProductionState &status) {
a61af66fc99e Initial load
duke
parents:
diff changeset
214 fprintf(fp, "%sunsigned int c = ", spaces);
a61af66fc99e Initial load
duke
parents:
diff changeset
215 Expr *c = new Expr("0");
8878
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
216 if (mList._lchild) { // If left child, add it in
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
217 const char* lchild_to_upper = ArchDesc::getMachOperEnum(mList._lchild);
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
218 sprintf(Expr::buffer(), "_kids[0]->_cost[%s]", lchild_to_upper);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
219 c->add(Expr::buffer());
8878
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
220 delete[] lchild_to_upper;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
221 }
8878
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
222 if (mList._rchild) { // If right child, add it in
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
223 const char* rchild_to_upper = ArchDesc::getMachOperEnum(mList._rchild);
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
224 sprintf(Expr::buffer(), "_kids[1]->_cost[%s]", rchild_to_upper);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
225 c->add(Expr::buffer());
8878
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
226 delete[] rchild_to_upper;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
227 }
a61af66fc99e Initial load
duke
parents:
diff changeset
228 // Add in cost of this rule
a61af66fc99e Initial load
duke
parents:
diff changeset
229 const char *mList_cost = mList.get_cost();
a61af66fc99e Initial load
duke
parents:
diff changeset
230 c->add(mList_cost, *this);
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 fprintf(fp, "%s;\n", c->as_string());
a61af66fc99e Initial load
duke
parents:
diff changeset
233 c->set_external_name("c");
a61af66fc99e Initial load
duke
parents:
diff changeset
234 return c;
a61af66fc99e Initial load
duke
parents:
diff changeset
235 }
a61af66fc99e Initial load
duke
parents:
diff changeset
236
a61af66fc99e Initial load
duke
parents:
diff changeset
237
a61af66fc99e Initial load
duke
parents:
diff changeset
238 //---------------------------gen_match-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
239 void ArchDesc::gen_match(FILE *fp, MatchList &mList, ProductionState &status, Dict &operands_chained_from) {
a61af66fc99e Initial load
duke
parents:
diff changeset
240 const char *spaces4 = " ";
a61af66fc99e Initial load
duke
parents:
diff changeset
241 const char *spaces6 = " ";
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 fprintf(fp, "%s", spaces4);
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // Only generate child tests if this is not a leaf node
a61af66fc99e Initial load
duke
parents:
diff changeset
245 bool has_child_constraints = mList._lchild || mList._rchild;
8878
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
246 const char *predicate_test = mList.get_pred();
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
247 if (has_child_constraints || predicate_test) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
248 // Open the child-and-predicate-test braces
a61af66fc99e Initial load
duke
parents:
diff changeset
249 fprintf(fp, "if( ");
a61af66fc99e Initial load
duke
parents:
diff changeset
250 status.set_constraint(hasConstraint);
a61af66fc99e Initial load
duke
parents:
diff changeset
251 child_test(fp, mList);
a61af66fc99e Initial load
duke
parents:
diff changeset
252 // Only generate predicate test if one exists for this match
8878
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
253 if (predicate_test) {
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
254 if (has_child_constraints) {
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
255 fprintf(fp," &&\n");
573cf206e381 8006014: Memory leak in hotspot/src/share/vm/adlc/dfa.cpp
neliasso
parents: 1972
diff changeset
256 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
257 fprintf(fp, "%s %s", spaces6, predicate_test);
a61af66fc99e Initial load
duke
parents:
diff changeset
258 }
a61af66fc99e Initial load
duke
parents:
diff changeset
259 // End of outer tests
a61af66fc99e Initial load
duke
parents:
diff changeset
260 fprintf(fp," ) ");
a61af66fc99e Initial load
duke
parents:
diff changeset
261 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
262 // No child or predicate test needed
a61af66fc99e Initial load
duke
parents:
diff changeset
263 status.set_constraint(noConstraint);
a61af66fc99e Initial load
duke
parents:
diff changeset
264 }
a61af66fc99e Initial load
duke
parents:
diff changeset
265
a61af66fc99e Initial load
duke
parents:
diff changeset
266 // End of outer tests
a61af66fc99e Initial load
duke
parents:
diff changeset
267 fprintf(fp,"{\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
268
a61af66fc99e Initial load
duke
parents:
diff changeset
269 // Calculate cost of this match
a61af66fc99e Initial load
duke
parents:
diff changeset
270 const Expr *cost = calc_cost(fp, spaces6, mList, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
271 // Check against other match costs, and update cost & rule vectors
a61af66fc99e Initial load
duke
parents:
diff changeset
272 cost_check(fp, spaces6, ArchDesc::getMachOperEnum(mList._resultStr), cost, mList._opcode, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
273
a61af66fc99e Initial load
duke
parents:
diff changeset
274 // If this is a member of an operand class, update the class cost & rule
a61af66fc99e Initial load
duke
parents:
diff changeset
275 expand_opclass( fp, spaces6, cost, mList._resultStr, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
276
a61af66fc99e Initial load
duke
parents:
diff changeset
277 // Check if this rule should be used to generate the chains as well.
a61af66fc99e Initial load
duke
parents:
diff changeset
278 const char *rule = /* set rule to "Invalid" for internal operands */
a61af66fc99e Initial load
duke
parents:
diff changeset
279 strcmp(mList._opcode,mList._resultStr) ? mList._opcode : "Invalid";
a61af66fc99e Initial load
duke
parents:
diff changeset
280
a61af66fc99e Initial load
duke
parents:
diff changeset
281 // If this rule produces an operand which has associated chain rules,
a61af66fc99e Initial load
duke
parents:
diff changeset
282 // update the operands with the chain rule + this rule cost & this rule.
a61af66fc99e Initial load
duke
parents:
diff changeset
283 chain_rule(fp, spaces6, mList._resultStr, cost, rule, operands_chained_from, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
284
a61af66fc99e Initial load
duke
parents:
diff changeset
285 // Close the child-and-predicate-test braces
a61af66fc99e Initial load
duke
parents:
diff changeset
286 fprintf(fp, " }\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
287
a61af66fc99e Initial load
duke
parents:
diff changeset
288 }
a61af66fc99e Initial load
duke
parents:
diff changeset
289
a61af66fc99e Initial load
duke
parents:
diff changeset
290
a61af66fc99e Initial load
duke
parents:
diff changeset
291 //---------------------------expand_opclass------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
292 // Chain from one result_type to all other members of its operand class
a61af66fc99e Initial load
duke
parents:
diff changeset
293 void ArchDesc::expand_opclass(FILE *fp, const char *indent, const Expr *cost,
a61af66fc99e Initial load
duke
parents:
diff changeset
294 const char *result_type, ProductionState &status) {
a61af66fc99e Initial load
duke
parents:
diff changeset
295 const Form *form = _globalNames[result_type];
a61af66fc99e Initial load
duke
parents:
diff changeset
296 OperandForm *op = form ? form->is_operand() : NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
297 if( op && op->_classes.count() > 0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 if( debug_output ) { fprintf(fp, "// expand operand classes for operand: %s \n", (char *)op->_ident ); } // %%%%% Explanation
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // Iterate through all operand classes which include this operand
a61af66fc99e Initial load
duke
parents:
diff changeset
300 op->_classes.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
301 const char *oclass;
a61af66fc99e Initial load
duke
parents:
diff changeset
302 // Expr *cCost = new Expr(cost);
a61af66fc99e Initial load
duke
parents:
diff changeset
303 while( (oclass = op->_classes.iter()) != NULL )
a61af66fc99e Initial load
duke
parents:
diff changeset
304 // Check against other match costs, and update cost & rule vectors
a61af66fc99e Initial load
duke
parents:
diff changeset
305 cost_check(fp, indent, ArchDesc::getMachOperEnum(oclass), cost, result_type, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307 }
a61af66fc99e Initial load
duke
parents:
diff changeset
308
a61af66fc99e Initial load
duke
parents:
diff changeset
309 //---------------------------chain_rule----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
310 // Starting at 'operand', check if we know how to automatically generate other results
a61af66fc99e Initial load
duke
parents:
diff changeset
311 void ArchDesc::chain_rule(FILE *fp, const char *indent, const char *operand,
a61af66fc99e Initial load
duke
parents:
diff changeset
312 const Expr *icost, const char *irule, Dict &operands_chained_from, ProductionState &status) {
a61af66fc99e Initial load
duke
parents:
diff changeset
313
a61af66fc99e Initial load
duke
parents:
diff changeset
314 // Check if we have already generated chains from this starting point
a61af66fc99e Initial load
duke
parents:
diff changeset
315 if( operands_chained_from[operand] != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
317 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
318 operands_chained_from.Insert( operand, operand);
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320 if( debug_output ) { fprintf(fp, "// chain rules starting from: %s and %s \n", (char *)operand, (char *)irule); } // %%%%% Explanation
a61af66fc99e Initial load
duke
parents:
diff changeset
321
a61af66fc99e Initial load
duke
parents:
diff changeset
322 ChainList *lst = (ChainList *)_chainRules[operand];
a61af66fc99e Initial load
duke
parents:
diff changeset
323 if (lst) {
a61af66fc99e Initial load
duke
parents:
diff changeset
324 // printf("\nChain from <%s> at cost #%s\n",operand, icost ? icost : "_");
a61af66fc99e Initial load
duke
parents:
diff changeset
325 const char *result, *cost, *rule;
a61af66fc99e Initial load
duke
parents:
diff changeset
326 for(lst->reset(); (lst->iter(result,cost,rule)) == true; ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 // Do not generate operands that are already available
a61af66fc99e Initial load
duke
parents:
diff changeset
328 if( operands_chained_from[result] != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
330 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
331 // Compute the cost for previous match + chain_rule_cost
a61af66fc99e Initial load
duke
parents:
diff changeset
332 // total_cost = icost + cost;
a61af66fc99e Initial load
duke
parents:
diff changeset
333 Expr *total_cost = icost->clone(); // icost + cost
a61af66fc99e Initial load
duke
parents:
diff changeset
334 total_cost->add(cost, *this);
a61af66fc99e Initial load
duke
parents:
diff changeset
335
a61af66fc99e Initial load
duke
parents:
diff changeset
336 // Check for transitive chain rules
a61af66fc99e Initial load
duke
parents:
diff changeset
337 Form *form = (Form *)_globalNames[rule];
a61af66fc99e Initial load
duke
parents:
diff changeset
338 if ( ! form->is_instruction()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
339 // printf(" result=%s cost=%s rule=%s\n", result, total_cost, rule);
a61af66fc99e Initial load
duke
parents:
diff changeset
340 // Check against other match costs, and update cost & rule vectors
a61af66fc99e Initial load
duke
parents:
diff changeset
341 const char *reduce_rule = strcmp(irule,"Invalid") ? irule : rule;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 cost_check(fp, indent, ArchDesc::getMachOperEnum(result), total_cost, reduce_rule, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
343 chain_rule(fp, indent, result, total_cost, irule, operands_chained_from, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
344 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
345 // printf(" result=%s cost=%s rule=%s\n", result, total_cost, rule);
a61af66fc99e Initial load
duke
parents:
diff changeset
346 // Check against other match costs, and update cost & rule vectors
a61af66fc99e Initial load
duke
parents:
diff changeset
347 cost_check(fp, indent, ArchDesc::getMachOperEnum(result), total_cost, rule, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
348 chain_rule(fp, indent, result, total_cost, rule, operands_chained_from, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
349 }
a61af66fc99e Initial load
duke
parents:
diff changeset
350
a61af66fc99e Initial load
duke
parents:
diff changeset
351 // If this is a member of an operand class, update class cost & rule
a61af66fc99e Initial load
duke
parents:
diff changeset
352 expand_opclass( fp, indent, total_cost, result, status );
a61af66fc99e Initial load
duke
parents:
diff changeset
353 }
a61af66fc99e Initial load
duke
parents:
diff changeset
354 }
a61af66fc99e Initial load
duke
parents:
diff changeset
355 }
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357
a61af66fc99e Initial load
duke
parents:
diff changeset
358 //---------------------------prune_matchlist-----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
359 // Check for duplicate entries in a matchlist, and prune out the higher cost
a61af66fc99e Initial load
duke
parents:
diff changeset
360 // entry.
a61af66fc99e Initial load
duke
parents:
diff changeset
361 void ArchDesc::prune_matchlist(Dict &minimize, MatchList &mlist) {
a61af66fc99e Initial load
duke
parents:
diff changeset
362
a61af66fc99e Initial load
duke
parents:
diff changeset
363 }
a61af66fc99e Initial load
duke
parents:
diff changeset
364
a61af66fc99e Initial load
duke
parents:
diff changeset
365 //---------------------------buildDFA------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
366 // DFA is a large switch with case statements for each ideal opcode encountered
a61af66fc99e Initial load
duke
parents:
diff changeset
367 // in any match rule in the ad file. Each case has a series of if's to handle
a61af66fc99e Initial load
duke
parents:
diff changeset
368 // the match or fail decisions. The matches test the cost function of that
a61af66fc99e Initial load
duke
parents:
diff changeset
369 // rule, and prune any cases which are higher cost for the same reduction.
a61af66fc99e Initial load
duke
parents:
diff changeset
370 // In order to generate the DFA we walk the table of ideal opcode/MatchList
a61af66fc99e Initial load
duke
parents:
diff changeset
371 // pairs generated by the ADLC front end to build the contents of the case
a61af66fc99e Initial load
duke
parents:
diff changeset
372 // statements (a series of if statements).
a61af66fc99e Initial load
duke
parents:
diff changeset
373 void ArchDesc::buildDFA(FILE* fp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
374 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
375 // Remember operands that are the starting points for chain rules.
a61af66fc99e Initial load
duke
parents:
diff changeset
376 // Prevent cycles by checking if we have already generated chain.
a61af66fc99e Initial load
duke
parents:
diff changeset
377 Dict operands_chained_from(cmpstr, hashstr, Form::arena);
a61af66fc99e Initial load
duke
parents:
diff changeset
378
a61af66fc99e Initial load
duke
parents:
diff changeset
379 // Hash inputs to match rules so that final DFA contains only one entry for
a61af66fc99e Initial load
duke
parents:
diff changeset
380 // each match pattern which is the low cost entry.
a61af66fc99e Initial load
duke
parents:
diff changeset
381 Dict minimize(cmpstr, hashstr, Form::arena);
a61af66fc99e Initial load
duke
parents:
diff changeset
382
a61af66fc99e Initial load
duke
parents:
diff changeset
383 // Track status of dfa for each resulting production
a61af66fc99e Initial load
duke
parents:
diff changeset
384 // reset for each ideal root.
a61af66fc99e Initial load
duke
parents:
diff changeset
385 ProductionState status(Form::arena);
a61af66fc99e Initial load
duke
parents:
diff changeset
386
a61af66fc99e Initial load
duke
parents:
diff changeset
387 // Output the start of the DFA method into the output file
a61af66fc99e Initial load
duke
parents:
diff changeset
388
a61af66fc99e Initial load
duke
parents:
diff changeset
389 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
390 fprintf(fp, "//------------------------- Source -----------------------------------------\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
391 // Do not put random source code into the DFA.
a61af66fc99e Initial load
duke
parents:
diff changeset
392 // If there are constants which need sharing, put them in "source_hpp" forms.
a61af66fc99e Initial load
duke
parents:
diff changeset
393 // _source.output(fp);
a61af66fc99e Initial load
duke
parents:
diff changeset
394 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
395 fprintf(fp, "//------------------------- Attributes -------------------------------------\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
396 _attributes.output(fp);
a61af66fc99e Initial load
duke
parents:
diff changeset
397 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
398 fprintf(fp, "//------------------------- Macros -----------------------------------------\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
399 // #define DFA_PRODUCTION(result, rule, cost)\
a61af66fc99e Initial load
duke
parents:
diff changeset
400 // _cost[ (result) ] = cost; _rule[ (result) ] = rule;
a61af66fc99e Initial load
duke
parents:
diff changeset
401 fprintf(fp, "#define %s(result, rule, cost)\\\n", dfa_production);
a61af66fc99e Initial load
duke
parents:
diff changeset
402 fprintf(fp, " _cost[ (result) ] = cost; _rule[ (result) ] = rule;\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
403 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
404
a61af66fc99e Initial load
duke
parents:
diff changeset
405 // #define DFA_PRODUCTION__SET_VALID(result, rule, cost)\
a61af66fc99e Initial load
duke
parents:
diff changeset
406 // DFA_PRODUCTION( (result), (rule), (cost) ); STATE__SET_VALID( (result) );
a61af66fc99e Initial load
duke
parents:
diff changeset
407 fprintf(fp, "#define %s(result, rule, cost)\\\n", dfa_production_set_valid);
a61af66fc99e Initial load
duke
parents:
diff changeset
408 fprintf(fp, " %s( (result), (rule), (cost) ); STATE__SET_VALID( (result) );\n", dfa_production);
a61af66fc99e Initial load
duke
parents:
diff changeset
409 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
410
a61af66fc99e Initial load
duke
parents:
diff changeset
411 fprintf(fp, "//------------------------- DFA --------------------------------------------\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
412
a61af66fc99e Initial load
duke
parents:
diff changeset
413 fprintf(fp,
a61af66fc99e Initial load
duke
parents:
diff changeset
414 "// DFA is a large switch with case statements for each ideal opcode encountered\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
415 "// in any match rule in the ad file. Each case has a series of if's to handle\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
416 "// the match or fail decisions. The matches test the cost function of that\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
417 "// rule, and prune any cases which are higher cost for the same reduction.\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
418 "// In order to generate the DFA we walk the table of ideal opcode/MatchList\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
419 "// pairs generated by the ADLC front end to build the contents of the case\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
420 "// statements (a series of if statements).\n"
a61af66fc99e Initial load
duke
parents:
diff changeset
421 );
a61af66fc99e Initial load
duke
parents:
diff changeset
422 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
423 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
424 if (_dfa_small) {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 // Now build the individual routines just like the switch entries in large version
a61af66fc99e Initial load
duke
parents:
diff changeset
426 // Iterate over the table of MatchLists, start at first valid opcode of 1
a61af66fc99e Initial load
duke
parents:
diff changeset
427 for (i = 1; i < _last_opcode; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
428 if (_mlistab[i] == NULL) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
429 // Generate the routine header statement for this opcode
a61af66fc99e Initial load
duke
parents:
diff changeset
430 fprintf(fp, "void State::_sub_Op_%s(const Node *n){\n", NodeClassNames[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
431 // Generate body. Shared for both inline and out-of-line version
a61af66fc99e Initial load
duke
parents:
diff changeset
432 gen_dfa_state_body(fp, minimize, status, operands_chained_from, i);
a61af66fc99e Initial load
duke
parents:
diff changeset
433 // End of routine
a61af66fc99e Initial load
duke
parents:
diff changeset
434 fprintf(fp, "}\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
435 }
a61af66fc99e Initial load
duke
parents:
diff changeset
436 }
a61af66fc99e Initial load
duke
parents:
diff changeset
437 fprintf(fp, "bool State::DFA");
a61af66fc99e Initial load
duke
parents:
diff changeset
438 fprintf(fp, "(int opcode, const Node *n) {\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
439 fprintf(fp, " switch(opcode) {\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
440
a61af66fc99e Initial load
duke
parents:
diff changeset
441 // Iterate over the table of MatchLists, start at first valid opcode of 1
a61af66fc99e Initial load
duke
parents:
diff changeset
442 for (i = 1; i < _last_opcode; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
443 if (_mlistab[i] == NULL) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
444 // Generate the case statement for this opcode
a61af66fc99e Initial load
duke
parents:
diff changeset
445 if (_dfa_small) {
a61af66fc99e Initial load
duke
parents:
diff changeset
446 fprintf(fp, " case Op_%s: { _sub_Op_%s(n);\n", NodeClassNames[i], NodeClassNames[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
447 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
448 fprintf(fp, " case Op_%s: {\n", NodeClassNames[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
449 // Walk the list, compacting it
a61af66fc99e Initial load
duke
parents:
diff changeset
450 gen_dfa_state_body(fp, minimize, status, operands_chained_from, i);
a61af66fc99e Initial load
duke
parents:
diff changeset
451 }
a61af66fc99e Initial load
duke
parents:
diff changeset
452 // Print the "break"
a61af66fc99e Initial load
duke
parents:
diff changeset
453 fprintf(fp, " break;\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
454 fprintf(fp, " }\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
455 }
a61af66fc99e Initial load
duke
parents:
diff changeset
456
a61af66fc99e Initial load
duke
parents:
diff changeset
457 // Generate the default case for switch(opcode)
a61af66fc99e Initial load
duke
parents:
diff changeset
458 fprintf(fp, " \n");
a61af66fc99e Initial load
duke
parents:
diff changeset
459 fprintf(fp, " default:\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
460 fprintf(fp, " tty->print(\"Default case invoked for: \\n\");\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
461 fprintf(fp, " tty->print(\" opcode = %cd, \\\"%cs\\\"\\n\", opcode, NodeClassNames[opcode]);\n", '%', '%');
a61af66fc99e Initial load
duke
parents:
diff changeset
462 fprintf(fp, " return false;\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
463 fprintf(fp, " }\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
464
a61af66fc99e Initial load
duke
parents:
diff changeset
465 // Return status, indicating a successful match.
a61af66fc99e Initial load
duke
parents:
diff changeset
466 fprintf(fp, " return true;\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
467 // Generate the closing brace for method Matcher::DFA
a61af66fc99e Initial load
duke
parents:
diff changeset
468 fprintf(fp, "}\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
469 Expr::check_buffers();
a61af66fc99e Initial load
duke
parents:
diff changeset
470 }
a61af66fc99e Initial load
duke
parents:
diff changeset
471
a61af66fc99e Initial load
duke
parents:
diff changeset
472
a61af66fc99e Initial load
duke
parents:
diff changeset
473 class dfa_shared_preds {
475
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
474 enum { count = 4 };
0
a61af66fc99e Initial load
duke
parents:
diff changeset
475
a61af66fc99e Initial load
duke
parents:
diff changeset
476 static bool _found[count];
a61af66fc99e Initial load
duke
parents:
diff changeset
477 static const char* _type [count];
a61af66fc99e Initial load
duke
parents:
diff changeset
478 static const char* _var [count];
a61af66fc99e Initial load
duke
parents:
diff changeset
479 static const char* _pred [count];
a61af66fc99e Initial load
duke
parents:
diff changeset
480
a61af66fc99e Initial load
duke
parents:
diff changeset
481 static void check_index(int index) { assert( 0 <= index && index < count, "Invalid index"); }
a61af66fc99e Initial load
duke
parents:
diff changeset
482
a61af66fc99e Initial load
duke
parents:
diff changeset
483 // Confirm that this is a separate sub-expression.
a61af66fc99e Initial load
duke
parents:
diff changeset
484 // Only need to catch common cases like " ... && shared ..."
a61af66fc99e Initial load
duke
parents:
diff changeset
485 // and avoid hazardous ones like "...->shared"
a61af66fc99e Initial load
duke
parents:
diff changeset
486 static bool valid_loc(char *pred, char *shared) {
a61af66fc99e Initial load
duke
parents:
diff changeset
487 // start of predicate is valid
a61af66fc99e Initial load
duke
parents:
diff changeset
488 if( shared == pred ) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
489
a61af66fc99e Initial load
duke
parents:
diff changeset
490 // Check previous character and recurse if needed
a61af66fc99e Initial load
duke
parents:
diff changeset
491 char *prev = shared - 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
492 char c = *prev;
a61af66fc99e Initial load
duke
parents:
diff changeset
493 switch( c ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
494 case ' ':
475
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
495 case '\n':
0
a61af66fc99e Initial load
duke
parents:
diff changeset
496 return dfa_shared_preds::valid_loc(pred, prev);
a61af66fc99e Initial load
duke
parents:
diff changeset
497 case '!':
a61af66fc99e Initial load
duke
parents:
diff changeset
498 case '(':
a61af66fc99e Initial load
duke
parents:
diff changeset
499 case '<':
a61af66fc99e Initial load
duke
parents:
diff changeset
500 case '=':
a61af66fc99e Initial load
duke
parents:
diff changeset
501 return true;
475
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
502 case '"': // such as: #line 10 "myfile.ad"\n mypredicate
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
503 return true;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
504 case '|':
a61af66fc99e Initial load
duke
parents:
diff changeset
505 if( prev != pred && *(prev-1) == '|' ) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
506 case '&':
a61af66fc99e Initial load
duke
parents:
diff changeset
507 if( prev != pred && *(prev-1) == '&' ) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
508 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
509 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
510 }
a61af66fc99e Initial load
duke
parents:
diff changeset
511
a61af66fc99e Initial load
duke
parents:
diff changeset
512 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
513 }
a61af66fc99e Initial load
duke
parents:
diff changeset
514
a61af66fc99e Initial load
duke
parents:
diff changeset
515 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
516
a61af66fc99e Initial load
duke
parents:
diff changeset
517 static bool found(int index){ check_index(index); return _found[index]; }
a61af66fc99e Initial load
duke
parents:
diff changeset
518 static void set_found(int index, bool val) { check_index(index); _found[index] = val; }
a61af66fc99e Initial load
duke
parents:
diff changeset
519 static void reset_found() {
a61af66fc99e Initial load
duke
parents:
diff changeset
520 for( int i = 0; i < count; ++i ) { _found[i] = false; }
a61af66fc99e Initial load
duke
parents:
diff changeset
521 };
a61af66fc99e Initial load
duke
parents:
diff changeset
522
a61af66fc99e Initial load
duke
parents:
diff changeset
523 static const char* type(int index) { check_index(index); return _type[index]; }
a61af66fc99e Initial load
duke
parents:
diff changeset
524 static const char* var (int index) { check_index(index); return _var [index]; }
a61af66fc99e Initial load
duke
parents:
diff changeset
525 static const char* pred(int index) { check_index(index); return _pred[index]; }
a61af66fc99e Initial load
duke
parents:
diff changeset
526
a61af66fc99e Initial load
duke
parents:
diff changeset
527 // Check each predicate in the MatchList for common sub-expressions
a61af66fc99e Initial load
duke
parents:
diff changeset
528 static void cse_matchlist(MatchList *matchList) {
a61af66fc99e Initial load
duke
parents:
diff changeset
529 for( MatchList *mList = matchList; mList != NULL; mList = mList->get_next() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
530 Predicate* predicate = mList->get_pred_obj();
a61af66fc99e Initial load
duke
parents:
diff changeset
531 char* pred = mList->get_pred();
a61af66fc99e Initial load
duke
parents:
diff changeset
532 if( pred != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
533 for(int index = 0; index < count; ++index ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
534 const char *shared_pred = dfa_shared_preds::pred(index);
a61af66fc99e Initial load
duke
parents:
diff changeset
535 const char *shared_pred_var = dfa_shared_preds::var(index);
a61af66fc99e Initial load
duke
parents:
diff changeset
536 bool result = dfa_shared_preds::cse_predicate(predicate, shared_pred, shared_pred_var);
a61af66fc99e Initial load
duke
parents:
diff changeset
537 if( result ) dfa_shared_preds::set_found(index, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
538 }
a61af66fc99e Initial load
duke
parents:
diff changeset
539 }
a61af66fc99e Initial load
duke
parents:
diff changeset
540 }
a61af66fc99e Initial load
duke
parents:
diff changeset
541 }
a61af66fc99e Initial load
duke
parents:
diff changeset
542
a61af66fc99e Initial load
duke
parents:
diff changeset
543 // If the Predicate contains a common sub-expression, replace the Predicate's
a61af66fc99e Initial load
duke
parents:
diff changeset
544 // string with one that uses the variable name.
a61af66fc99e Initial load
duke
parents:
diff changeset
545 static bool cse_predicate(Predicate* predicate, const char *shared_pred, const char *shared_pred_var) {
a61af66fc99e Initial load
duke
parents:
diff changeset
546 bool result = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
547 char *pred = predicate->_pred;
a61af66fc99e Initial load
duke
parents:
diff changeset
548 if( pred != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
549 char *new_pred = pred;
a61af66fc99e Initial load
duke
parents:
diff changeset
550 for( char *shared_pred_loc = strstr(new_pred, shared_pred);
a61af66fc99e Initial load
duke
parents:
diff changeset
551 shared_pred_loc != NULL && dfa_shared_preds::valid_loc(new_pred,shared_pred_loc);
a61af66fc99e Initial load
duke
parents:
diff changeset
552 shared_pred_loc = strstr(new_pred, shared_pred) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
553 // Do not modify the original predicate string, it is shared
a61af66fc99e Initial load
duke
parents:
diff changeset
554 if( new_pred == pred ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
555 new_pred = strdup(pred);
a61af66fc99e Initial load
duke
parents:
diff changeset
556 shared_pred_loc = strstr(new_pred, shared_pred);
a61af66fc99e Initial load
duke
parents:
diff changeset
557 }
a61af66fc99e Initial load
duke
parents:
diff changeset
558 // Replace shared_pred with variable name
a61af66fc99e Initial load
duke
parents:
diff changeset
559 strncpy(shared_pred_loc, shared_pred_var, strlen(shared_pred_var));
a61af66fc99e Initial load
duke
parents:
diff changeset
560 }
a61af66fc99e Initial load
duke
parents:
diff changeset
561 // Install new predicate
a61af66fc99e Initial load
duke
parents:
diff changeset
562 if( new_pred != pred ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
563 predicate->_pred = new_pred;
a61af66fc99e Initial load
duke
parents:
diff changeset
564 result = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
565 }
a61af66fc99e Initial load
duke
parents:
diff changeset
566 }
a61af66fc99e Initial load
duke
parents:
diff changeset
567 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
568 }
a61af66fc99e Initial load
duke
parents:
diff changeset
569
a61af66fc99e Initial load
duke
parents:
diff changeset
570 // Output the hoisted common sub-expression if we found it in predicates
a61af66fc99e Initial load
duke
parents:
diff changeset
571 static void generate_cse(FILE *fp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
572 for(int j = 0; j < count; ++j ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
573 if( dfa_shared_preds::found(j) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
574 const char *shared_pred_type = dfa_shared_preds::type(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
575 const char *shared_pred_var = dfa_shared_preds::var(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
576 const char *shared_pred = dfa_shared_preds::pred(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
577 fprintf(fp, " %s %s = %s;\n", shared_pred_type, shared_pred_var, shared_pred);
a61af66fc99e Initial load
duke
parents:
diff changeset
578 }
a61af66fc99e Initial load
duke
parents:
diff changeset
579 }
a61af66fc99e Initial load
duke
parents:
diff changeset
580 }
a61af66fc99e Initial load
duke
parents:
diff changeset
581 };
a61af66fc99e Initial load
duke
parents:
diff changeset
582 // shared predicates, _var and _pred entry should be the same length
475
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
583 bool dfa_shared_preds::_found[dfa_shared_preds::count]
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
584 = { false, false, false, false };
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
585 const char* dfa_shared_preds::_type[dfa_shared_preds::count]
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
586 = { "int", "jlong", "intptr_t", "bool" };
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
587 const char* dfa_shared_preds::_var [dfa_shared_preds::count]
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
588 = { "_n_get_int__", "_n_get_long__", "_n_get_intptr_t__", "Compile__current____select_24_bit_instr__" };
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
589 const char* dfa_shared_preds::_pred[dfa_shared_preds::count]
284d0af00d53 6771309: debugging AD files is difficult without #line directives in generated code
jrose
parents: 0
diff changeset
590 = { "n->get_int()", "n->get_long()", "n->get_intptr_t()", "Compile::current()->select_24_bit_instr()" };
0
a61af66fc99e Initial load
duke
parents:
diff changeset
591
a61af66fc99e Initial load
duke
parents:
diff changeset
592
a61af66fc99e Initial load
duke
parents:
diff changeset
593 void ArchDesc::gen_dfa_state_body(FILE* fp, Dict &minimize, ProductionState &status, Dict &operands_chained_from, int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
594 // Start the body of each Op_XXX sub-dfa with a clean state.
a61af66fc99e Initial load
duke
parents:
diff changeset
595 status.initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
596
a61af66fc99e Initial load
duke
parents:
diff changeset
597 // Walk the list, compacting it
a61af66fc99e Initial load
duke
parents:
diff changeset
598 MatchList* mList = _mlistab[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
599 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
600 // Hash each entry using inputs as key and pointer as data.
a61af66fc99e Initial load
duke
parents:
diff changeset
601 // If there is already an entry, keep the one with lower cost, and
a61af66fc99e Initial load
duke
parents:
diff changeset
602 // remove the other one from the list.
a61af66fc99e Initial load
duke
parents:
diff changeset
603 prune_matchlist(minimize, *mList);
a61af66fc99e Initial load
duke
parents:
diff changeset
604 // Iterate
a61af66fc99e Initial load
duke
parents:
diff changeset
605 mList = mList->get_next();
a61af66fc99e Initial load
duke
parents:
diff changeset
606 } while(mList != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
607
a61af66fc99e Initial load
duke
parents:
diff changeset
608 // Hoist previously specified common sub-expressions out of predicates
a61af66fc99e Initial load
duke
parents:
diff changeset
609 dfa_shared_preds::reset_found();
a61af66fc99e Initial load
duke
parents:
diff changeset
610 dfa_shared_preds::cse_matchlist(_mlistab[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
611 dfa_shared_preds::generate_cse(fp);
a61af66fc99e Initial load
duke
parents:
diff changeset
612
a61af66fc99e Initial load
duke
parents:
diff changeset
613 mList = _mlistab[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
614
a61af66fc99e Initial load
duke
parents:
diff changeset
615 // Walk the list again, generating code
a61af66fc99e Initial load
duke
parents:
diff changeset
616 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
617 // Each match can generate its own chains
a61af66fc99e Initial load
duke
parents:
diff changeset
618 operands_chained_from.Clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
619 gen_match(fp, *mList, status, operands_chained_from);
a61af66fc99e Initial load
duke
parents:
diff changeset
620 mList = mList->get_next();
a61af66fc99e Initial load
duke
parents:
diff changeset
621 } while(mList != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
622 // Fill in any chain rules which add instructions
a61af66fc99e Initial load
duke
parents:
diff changeset
623 // These can generate their own chains as well.
a61af66fc99e Initial load
duke
parents:
diff changeset
624 operands_chained_from.Clear(); //
a61af66fc99e Initial load
duke
parents:
diff changeset
625 if( debug_output1 ) { fprintf(fp, "// top level chain rules for: %s \n", (char *)NodeClassNames[i]); } // %%%%% Explanation
a61af66fc99e Initial load
duke
parents:
diff changeset
626 const Expr *zeroCost = new Expr("0");
a61af66fc99e Initial load
duke
parents:
diff changeset
627 chain_rule(fp, " ", (char *)NodeClassNames[i], zeroCost, "Invalid",
a61af66fc99e Initial load
duke
parents:
diff changeset
628 operands_chained_from, status);
a61af66fc99e Initial load
duke
parents:
diff changeset
629 }
a61af66fc99e Initial load
duke
parents:
diff changeset
630
a61af66fc99e Initial load
duke
parents:
diff changeset
631
a61af66fc99e Initial load
duke
parents:
diff changeset
632
a61af66fc99e Initial load
duke
parents:
diff changeset
633 //------------------------------Expr------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
634 Expr *Expr::_unknown_expr = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
635 char Expr::string_buffer[STRING_BUFFER_LENGTH];
a61af66fc99e Initial load
duke
parents:
diff changeset
636 char Expr::external_buffer[STRING_BUFFER_LENGTH];
a61af66fc99e Initial load
duke
parents:
diff changeset
637 bool Expr::_init_buffers = Expr::init_buffers();
a61af66fc99e Initial load
duke
parents:
diff changeset
638
a61af66fc99e Initial load
duke
parents:
diff changeset
639 Expr::Expr() {
a61af66fc99e Initial load
duke
parents:
diff changeset
640 _external_name = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
641 _expr = "Invalid_Expr";
a61af66fc99e Initial load
duke
parents:
diff changeset
642 _min_value = Expr::Max;
a61af66fc99e Initial load
duke
parents:
diff changeset
643 _max_value = Expr::Zero;
a61af66fc99e Initial load
duke
parents:
diff changeset
644 }
a61af66fc99e Initial load
duke
parents:
diff changeset
645 Expr::Expr(const char *cost) {
a61af66fc99e Initial load
duke
parents:
diff changeset
646 _external_name = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
647
a61af66fc99e Initial load
duke
parents:
diff changeset
648 int intval = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
649 if( cost == NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
650 _expr = "0";
a61af66fc99e Initial load
duke
parents:
diff changeset
651 _min_value = Expr::Zero;
a61af66fc99e Initial load
duke
parents:
diff changeset
652 _max_value = Expr::Zero;
a61af66fc99e Initial load
duke
parents:
diff changeset
653 }
a61af66fc99e Initial load
duke
parents:
diff changeset
654 else if( ADLParser::is_int_token(cost, intval) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
655 _expr = cost;
a61af66fc99e Initial load
duke
parents:
diff changeset
656 _min_value = intval;
a61af66fc99e Initial load
duke
parents:
diff changeset
657 _max_value = intval;
a61af66fc99e Initial load
duke
parents:
diff changeset
658 }
a61af66fc99e Initial load
duke
parents:
diff changeset
659 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
660 assert( strcmp(cost,"0") != 0, "Recognize string zero as an int");
a61af66fc99e Initial load
duke
parents:
diff changeset
661 _expr = cost;
a61af66fc99e Initial load
duke
parents:
diff changeset
662 _min_value = Expr::Zero;
a61af66fc99e Initial load
duke
parents:
diff changeset
663 _max_value = Expr::Max;
a61af66fc99e Initial load
duke
parents:
diff changeset
664 }
a61af66fc99e Initial load
duke
parents:
diff changeset
665 }
a61af66fc99e Initial load
duke
parents:
diff changeset
666
a61af66fc99e Initial load
duke
parents:
diff changeset
667 Expr::Expr(const char *name, const char *expression, int min_value, int max_value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
668 _external_name = name;
a61af66fc99e Initial load
duke
parents:
diff changeset
669 _expr = expression ? expression : name;
a61af66fc99e Initial load
duke
parents:
diff changeset
670 _min_value = min_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
671 _max_value = max_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
672 assert(_min_value >= 0 && _min_value <= Expr::Max, "value out of range");
a61af66fc99e Initial load
duke
parents:
diff changeset
673 assert(_max_value >= 0 && _max_value <= Expr::Max, "value out of range");
a61af66fc99e Initial load
duke
parents:
diff changeset
674 }
a61af66fc99e Initial load
duke
parents:
diff changeset
675
a61af66fc99e Initial load
duke
parents:
diff changeset
676 Expr *Expr::clone() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
677 Expr *cost = new Expr();
a61af66fc99e Initial load
duke
parents:
diff changeset
678 cost->_external_name = _external_name;
a61af66fc99e Initial load
duke
parents:
diff changeset
679 cost->_expr = _expr;
a61af66fc99e Initial load
duke
parents:
diff changeset
680 cost->_min_value = _min_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
681 cost->_max_value = _max_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
682
a61af66fc99e Initial load
duke
parents:
diff changeset
683 return cost;
a61af66fc99e Initial load
duke
parents:
diff changeset
684 }
a61af66fc99e Initial load
duke
parents:
diff changeset
685
a61af66fc99e Initial load
duke
parents:
diff changeset
686 void Expr::add(const Expr *c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
687 // Do not update fields until all computation is complete
a61af66fc99e Initial load
duke
parents:
diff changeset
688 const char *external = compute_external(this, c);
a61af66fc99e Initial load
duke
parents:
diff changeset
689 const char *expr = compute_expr(this, c);
a61af66fc99e Initial load
duke
parents:
diff changeset
690 int min_value = compute_min (this, c);
a61af66fc99e Initial load
duke
parents:
diff changeset
691 int max_value = compute_max (this, c);
a61af66fc99e Initial load
duke
parents:
diff changeset
692
a61af66fc99e Initial load
duke
parents:
diff changeset
693 _external_name = external;
a61af66fc99e Initial load
duke
parents:
diff changeset
694 _expr = expr;
a61af66fc99e Initial load
duke
parents:
diff changeset
695 _min_value = min_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
696 _max_value = max_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
697 }
a61af66fc99e Initial load
duke
parents:
diff changeset
698
a61af66fc99e Initial load
duke
parents:
diff changeset
699 void Expr::add(const char *c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
700 Expr *cost = new Expr(c);
a61af66fc99e Initial load
duke
parents:
diff changeset
701 add(cost);
a61af66fc99e Initial load
duke
parents:
diff changeset
702 }
a61af66fc99e Initial load
duke
parents:
diff changeset
703
a61af66fc99e Initial load
duke
parents:
diff changeset
704 void Expr::add(const char *c, ArchDesc &AD) {
a61af66fc99e Initial load
duke
parents:
diff changeset
705 const Expr *e = AD.globalDefs()[c];
a61af66fc99e Initial load
duke
parents:
diff changeset
706 if( e != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
707 // use the value of 'c' defined in <arch>.ad
a61af66fc99e Initial load
duke
parents:
diff changeset
708 add(e);
a61af66fc99e Initial load
duke
parents:
diff changeset
709 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
710 Expr *cost = new Expr(c);
a61af66fc99e Initial load
duke
parents:
diff changeset
711 add(cost);
a61af66fc99e Initial load
duke
parents:
diff changeset
712 }
a61af66fc99e Initial load
duke
parents:
diff changeset
713 }
a61af66fc99e Initial load
duke
parents:
diff changeset
714
a61af66fc99e Initial load
duke
parents:
diff changeset
715 const char *Expr::compute_external(const Expr *c1, const Expr *c2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
716 const char * result = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
717
a61af66fc99e Initial load
duke
parents:
diff changeset
718 // Preserve use of external name which has a zero value
a61af66fc99e Initial load
duke
parents:
diff changeset
719 if( c1->_external_name != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
720 sprintf( string_buffer, "%s", c1->as_string());
a61af66fc99e Initial load
duke
parents:
diff changeset
721 if( !c2->is_zero() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
722 strcat( string_buffer, "+");
a61af66fc99e Initial load
duke
parents:
diff changeset
723 strcat( string_buffer, c2->as_string());
a61af66fc99e Initial load
duke
parents:
diff changeset
724 }
a61af66fc99e Initial load
duke
parents:
diff changeset
725 result = strdup(string_buffer);
a61af66fc99e Initial load
duke
parents:
diff changeset
726 }
a61af66fc99e Initial load
duke
parents:
diff changeset
727 else if( c2->_external_name != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
728 if( !c1->is_zero() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
729 sprintf( string_buffer, "%s", c1->as_string());
a61af66fc99e Initial load
duke
parents:
diff changeset
730 strcat( string_buffer, " + ");
a61af66fc99e Initial load
duke
parents:
diff changeset
731 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
732 string_buffer[0] = '\0';
a61af66fc99e Initial load
duke
parents:
diff changeset
733 }
a61af66fc99e Initial load
duke
parents:
diff changeset
734 strcat( string_buffer, c2->_external_name );
a61af66fc99e Initial load
duke
parents:
diff changeset
735 result = strdup(string_buffer);
a61af66fc99e Initial load
duke
parents:
diff changeset
736 }
a61af66fc99e Initial load
duke
parents:
diff changeset
737 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
738 }
a61af66fc99e Initial load
duke
parents:
diff changeset
739
a61af66fc99e Initial load
duke
parents:
diff changeset
740 const char *Expr::compute_expr(const Expr *c1, const Expr *c2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
741 if( !c1->is_zero() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
742 sprintf( string_buffer, "%s", c1->_expr);
a61af66fc99e Initial load
duke
parents:
diff changeset
743 if( !c2->is_zero() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
744 strcat( string_buffer, "+");
a61af66fc99e Initial load
duke
parents:
diff changeset
745 strcat( string_buffer, c2->_expr);
a61af66fc99e Initial load
duke
parents:
diff changeset
746 }
a61af66fc99e Initial load
duke
parents:
diff changeset
747 }
a61af66fc99e Initial load
duke
parents:
diff changeset
748 else if( !c2->is_zero() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
749 sprintf( string_buffer, "%s", c2->_expr);
a61af66fc99e Initial load
duke
parents:
diff changeset
750 }
a61af66fc99e Initial load
duke
parents:
diff changeset
751 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
752 sprintf( string_buffer, "0");
a61af66fc99e Initial load
duke
parents:
diff changeset
753 }
a61af66fc99e Initial load
duke
parents:
diff changeset
754 char *cost = strdup(string_buffer);
a61af66fc99e Initial load
duke
parents:
diff changeset
755
a61af66fc99e Initial load
duke
parents:
diff changeset
756 return cost;
a61af66fc99e Initial load
duke
parents:
diff changeset
757 }
a61af66fc99e Initial load
duke
parents:
diff changeset
758
a61af66fc99e Initial load
duke
parents:
diff changeset
759 int Expr::compute_min(const Expr *c1, const Expr *c2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
760 int result = c1->_min_value + c2->_min_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
761 assert( result >= 0, "Invalid cost computation");
a61af66fc99e Initial load
duke
parents:
diff changeset
762
a61af66fc99e Initial load
duke
parents:
diff changeset
763 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
764 }
a61af66fc99e Initial load
duke
parents:
diff changeset
765
a61af66fc99e Initial load
duke
parents:
diff changeset
766 int Expr::compute_max(const Expr *c1, const Expr *c2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
767 int result = c1->_max_value + c2->_max_value;
a61af66fc99e Initial load
duke
parents:
diff changeset
768 if( result < 0 ) { // check for overflow
a61af66fc99e Initial load
duke
parents:
diff changeset
769 result = Expr::Max;
a61af66fc99e Initial load
duke
parents:
diff changeset
770 }
a61af66fc99e Initial load
duke
parents:
diff changeset
771
a61af66fc99e Initial load
duke
parents:
diff changeset
772 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
773 }
a61af66fc99e Initial load
duke
parents:
diff changeset
774
a61af66fc99e Initial load
duke
parents:
diff changeset
775 void Expr::print() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
776 if( _external_name != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
777 printf(" %s == (%s) === [%d, %d]\n", _external_name, _expr, _min_value, _max_value);
a61af66fc99e Initial load
duke
parents:
diff changeset
778 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
779 printf(" %s === [%d, %d]\n", _expr, _min_value, _max_value);
a61af66fc99e Initial load
duke
parents:
diff changeset
780 }
a61af66fc99e Initial load
duke
parents:
diff changeset
781 }
a61af66fc99e Initial load
duke
parents:
diff changeset
782
a61af66fc99e Initial load
duke
parents:
diff changeset
783 void Expr::print_define(FILE *fp) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
784 assert( _external_name != NULL, "definition does not have a name");
a61af66fc99e Initial load
duke
parents:
diff changeset
785 assert( _min_value == _max_value, "Expect user definitions to have constant value");
a61af66fc99e Initial load
duke
parents:
diff changeset
786 fprintf(fp, "#define %s (%s) \n", _external_name, _expr);
a61af66fc99e Initial load
duke
parents:
diff changeset
787 fprintf(fp, "// value == %d \n", _min_value);
a61af66fc99e Initial load
duke
parents:
diff changeset
788 }
a61af66fc99e Initial load
duke
parents:
diff changeset
789
a61af66fc99e Initial load
duke
parents:
diff changeset
790 void Expr::print_assert(FILE *fp) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
791 assert( _external_name != NULL, "definition does not have a name");
a61af66fc99e Initial load
duke
parents:
diff changeset
792 assert( _min_value == _max_value, "Expect user definitions to have constant value");
a61af66fc99e Initial load
duke
parents:
diff changeset
793 fprintf(fp, " assert( %s == %d, \"Expect (%s) to equal %d\");\n", _external_name, _min_value, _expr, _min_value);
a61af66fc99e Initial load
duke
parents:
diff changeset
794 }
a61af66fc99e Initial load
duke
parents:
diff changeset
795
a61af66fc99e Initial load
duke
parents:
diff changeset
796 Expr *Expr::get_unknown() {
a61af66fc99e Initial load
duke
parents:
diff changeset
797 if( Expr::_unknown_expr == NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
798 Expr::_unknown_expr = new Expr();
a61af66fc99e Initial load
duke
parents:
diff changeset
799 }
a61af66fc99e Initial load
duke
parents:
diff changeset
800
a61af66fc99e Initial load
duke
parents:
diff changeset
801 return Expr::_unknown_expr;
a61af66fc99e Initial load
duke
parents:
diff changeset
802 }
a61af66fc99e Initial load
duke
parents:
diff changeset
803
a61af66fc99e Initial load
duke
parents:
diff changeset
804 bool Expr::init_buffers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
805 // Fill buffers with 0
a61af66fc99e Initial load
duke
parents:
diff changeset
806 for( int i = 0; i < STRING_BUFFER_LENGTH; ++i ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
807 external_buffer[i] = '\0';
a61af66fc99e Initial load
duke
parents:
diff changeset
808 string_buffer[i] = '\0';
a61af66fc99e Initial load
duke
parents:
diff changeset
809 }
a61af66fc99e Initial load
duke
parents:
diff changeset
810
a61af66fc99e Initial load
duke
parents:
diff changeset
811 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
812 }
a61af66fc99e Initial load
duke
parents:
diff changeset
813
a61af66fc99e Initial load
duke
parents:
diff changeset
814 bool Expr::check_buffers() {
a61af66fc99e Initial load
duke
parents:
diff changeset
815 // returns 'true' if buffer use may have overflowed
a61af66fc99e Initial load
duke
parents:
diff changeset
816 bool ok = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
817 for( int i = STRING_BUFFER_LENGTH - 100; i < STRING_BUFFER_LENGTH; ++i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
818 if( external_buffer[i] != '\0' || string_buffer[i] != '\0' ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
819 ok = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
820 assert( false, "Expr:: Buffer overflow");
a61af66fc99e Initial load
duke
parents:
diff changeset
821 }
a61af66fc99e Initial load
duke
parents:
diff changeset
822 }
a61af66fc99e Initial load
duke
parents:
diff changeset
823
a61af66fc99e Initial load
duke
parents:
diff changeset
824 return ok;
a61af66fc99e Initial load
duke
parents:
diff changeset
825 }
a61af66fc99e Initial load
duke
parents:
diff changeset
826
a61af66fc99e Initial load
duke
parents:
diff changeset
827
a61af66fc99e Initial load
duke
parents:
diff changeset
828 //------------------------------ExprDict---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
829 // Constructor
a61af66fc99e Initial load
duke
parents:
diff changeset
830 ExprDict::ExprDict( CmpKey cmp, Hash hash, Arena *arena )
a61af66fc99e Initial load
duke
parents:
diff changeset
831 : _expr(cmp, hash, arena), _defines() {
a61af66fc99e Initial load
duke
parents:
diff changeset
832 }
a61af66fc99e Initial load
duke
parents:
diff changeset
833 ExprDict::~ExprDict() {
a61af66fc99e Initial load
duke
parents:
diff changeset
834 }
a61af66fc99e Initial load
duke
parents:
diff changeset
835
a61af66fc99e Initial load
duke
parents:
diff changeset
836 // Return # of name-Expr pairs in dict
a61af66fc99e Initial load
duke
parents:
diff changeset
837 int ExprDict::Size(void) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
838 return _expr.Size();
a61af66fc99e Initial load
duke
parents:
diff changeset
839 }
a61af66fc99e Initial load
duke
parents:
diff changeset
840
a61af66fc99e Initial load
duke
parents:
diff changeset
841 // define inserts the given key-value pair into the dictionary,
a61af66fc99e Initial load
duke
parents:
diff changeset
842 // and records the name in order for later output, ...
a61af66fc99e Initial load
duke
parents:
diff changeset
843 const Expr *ExprDict::define(const char *name, Expr *expr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
844 const Expr *old_expr = (*this)[name];
a61af66fc99e Initial load
duke
parents:
diff changeset
845 assert(old_expr == NULL, "Implementation does not support redefinition");
a61af66fc99e Initial load
duke
parents:
diff changeset
846
a61af66fc99e Initial load
duke
parents:
diff changeset
847 _expr.Insert(name, expr);
a61af66fc99e Initial load
duke
parents:
diff changeset
848 _defines.addName(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
849
a61af66fc99e Initial load
duke
parents:
diff changeset
850 return old_expr;
a61af66fc99e Initial load
duke
parents:
diff changeset
851 }
a61af66fc99e Initial load
duke
parents:
diff changeset
852
a61af66fc99e Initial load
duke
parents:
diff changeset
853 // Insert inserts the given key-value pair into the dictionary. The prior
a61af66fc99e Initial load
duke
parents:
diff changeset
854 // value of the key is returned; NULL if the key was not previously defined.
a61af66fc99e Initial load
duke
parents:
diff changeset
855 const Expr *ExprDict::Insert(const char *name, Expr *expr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
856 return (Expr*)_expr.Insert((void*)name, (void*)expr);
a61af66fc99e Initial load
duke
parents:
diff changeset
857 }
a61af66fc99e Initial load
duke
parents:
diff changeset
858
a61af66fc99e Initial load
duke
parents:
diff changeset
859 // Finds the value of a given key; or NULL if not found.
a61af66fc99e Initial load
duke
parents:
diff changeset
860 // The dictionary is NOT changed.
a61af66fc99e Initial load
duke
parents:
diff changeset
861 const Expr *ExprDict::operator [](const char *name) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
862 return (Expr*)_expr[name];
a61af66fc99e Initial load
duke
parents:
diff changeset
863 }
a61af66fc99e Initial load
duke
parents:
diff changeset
864
a61af66fc99e Initial load
duke
parents:
diff changeset
865 void ExprDict::print_defines(FILE *fp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
866 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
867 const char *name = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
868 for( _defines.reset(); (name = _defines.iter()) != NULL; ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
869 const Expr *expr = (const Expr*)_expr[name];
a61af66fc99e Initial load
duke
parents:
diff changeset
870 assert( expr != NULL, "name in ExprDict without matching Expr in dictionary");
a61af66fc99e Initial load
duke
parents:
diff changeset
871 expr->print_define(fp);
a61af66fc99e Initial load
duke
parents:
diff changeset
872 }
a61af66fc99e Initial load
duke
parents:
diff changeset
873 }
a61af66fc99e Initial load
duke
parents:
diff changeset
874 void ExprDict::print_asserts(FILE *fp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
875 fprintf(fp, "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
876 fprintf(fp, " // Following assertions generated from definition section\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
877 const char *name = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
878 for( _defines.reset(); (name = _defines.iter()) != NULL; ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
879 const Expr *expr = (const Expr*)_expr[name];
a61af66fc99e Initial load
duke
parents:
diff changeset
880 assert( expr != NULL, "name in ExprDict without matching Expr in dictionary");
a61af66fc99e Initial load
duke
parents:
diff changeset
881 expr->print_assert(fp);
a61af66fc99e Initial load
duke
parents:
diff changeset
882 }
a61af66fc99e Initial load
duke
parents:
diff changeset
883 }
a61af66fc99e Initial load
duke
parents:
diff changeset
884
a61af66fc99e Initial load
duke
parents:
diff changeset
885 // Print out the dictionary contents as key-value pairs
603
dbbe28fc66b5 6778669: Patch from Red Hat -- fixes compilation errors
twisti
parents: 475
diff changeset
886 static void dumpekey(const void* key) { fprintf(stdout, "%s", (char*) key); }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
887 static void dumpexpr(const void* expr) { fflush(stdout); ((Expr*)expr)->print(); }
a61af66fc99e Initial load
duke
parents:
diff changeset
888
a61af66fc99e Initial load
duke
parents:
diff changeset
889 void ExprDict::dump() {
a61af66fc99e Initial load
duke
parents:
diff changeset
890 _expr.print(dumpekey, dumpexpr);
a61af66fc99e Initial load
duke
parents:
diff changeset
891 }
a61af66fc99e Initial load
duke
parents:
diff changeset
892
a61af66fc99e Initial load
duke
parents:
diff changeset
893
a61af66fc99e Initial load
duke
parents:
diff changeset
894 //------------------------------ExprDict::private------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
895 // Disable public use of constructor, copy-ctor, operator =, operator ==
a61af66fc99e Initial load
duke
parents:
diff changeset
896 ExprDict::ExprDict( ) : _expr(cmpkey,hashkey), _defines() {
a61af66fc99e Initial load
duke
parents:
diff changeset
897 assert( false, "NotImplemented");
a61af66fc99e Initial load
duke
parents:
diff changeset
898 }
a61af66fc99e Initial load
duke
parents:
diff changeset
899 ExprDict::ExprDict( const ExprDict & ) : _expr(cmpkey,hashkey), _defines() {
a61af66fc99e Initial load
duke
parents:
diff changeset
900 assert( false, "NotImplemented");
a61af66fc99e Initial load
duke
parents:
diff changeset
901 }
a61af66fc99e Initial load
duke
parents:
diff changeset
902 ExprDict &ExprDict::operator =( const ExprDict &rhs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
903 assert( false, "NotImplemented");
a61af66fc99e Initial load
duke
parents:
diff changeset
904 _expr = rhs._expr;
a61af66fc99e Initial load
duke
parents:
diff changeset
905 return *this;
a61af66fc99e Initial load
duke
parents:
diff changeset
906 }
a61af66fc99e Initial load
duke
parents:
diff changeset
907 // == compares two dictionaries; they must have the same keys (their keys
a61af66fc99e Initial load
duke
parents:
diff changeset
908 // must match using CmpKey) and they must have the same values (pointer
a61af66fc99e Initial load
duke
parents:
diff changeset
909 // comparison). If so 1 is returned, if not 0 is returned.
a61af66fc99e Initial load
duke
parents:
diff changeset
910 bool ExprDict::operator ==(const ExprDict &d) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
911 assert( false, "NotImplemented");
a61af66fc99e Initial load
duke
parents:
diff changeset
912 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
913 }
a61af66fc99e Initial load
duke
parents:
diff changeset
914
a61af66fc99e Initial load
duke
parents:
diff changeset
915
a61af66fc99e Initial load
duke
parents:
diff changeset
916 //------------------------------Production-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
917 Production::Production(const char *result, const char *constraint, const char *valid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
918 initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
919 _result = result;
a61af66fc99e Initial load
duke
parents:
diff changeset
920 _constraint = constraint;
a61af66fc99e Initial load
duke
parents:
diff changeset
921 _valid = valid;
a61af66fc99e Initial load
duke
parents:
diff changeset
922 }
a61af66fc99e Initial load
duke
parents:
diff changeset
923
a61af66fc99e Initial load
duke
parents:
diff changeset
924 void Production::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
925 _result = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
926 _constraint = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
927 _valid = knownInvalid;
a61af66fc99e Initial load
duke
parents:
diff changeset
928 _cost_lb = Expr::get_unknown();
a61af66fc99e Initial load
duke
parents:
diff changeset
929 _cost_ub = Expr::get_unknown();
a61af66fc99e Initial load
duke
parents:
diff changeset
930 }
a61af66fc99e Initial load
duke
parents:
diff changeset
931
a61af66fc99e Initial load
duke
parents:
diff changeset
932 void Production::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
933 printf("%s", (_result == NULL ? "NULL" : _result ) );
a61af66fc99e Initial load
duke
parents:
diff changeset
934 printf("%s", (_constraint == NULL ? "NULL" : _constraint ) );
a61af66fc99e Initial load
duke
parents:
diff changeset
935 printf("%s", (_valid == NULL ? "NULL" : _valid ) );
a61af66fc99e Initial load
duke
parents:
diff changeset
936 _cost_lb->print();
a61af66fc99e Initial load
duke
parents:
diff changeset
937 _cost_ub->print();
a61af66fc99e Initial load
duke
parents:
diff changeset
938 }
a61af66fc99e Initial load
duke
parents:
diff changeset
939
a61af66fc99e Initial load
duke
parents:
diff changeset
940
a61af66fc99e Initial load
duke
parents:
diff changeset
941 //------------------------------ProductionState--------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
942 void ProductionState::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
943 _constraint = noConstraint;
a61af66fc99e Initial load
duke
parents:
diff changeset
944
a61af66fc99e Initial load
duke
parents:
diff changeset
945 // reset each Production currently in the dictionary
a61af66fc99e Initial load
duke
parents:
diff changeset
946 DictI iter( &_production );
a61af66fc99e Initial load
duke
parents:
diff changeset
947 const void *x, *y = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
948 for( ; iter.test(); ++iter) {
a61af66fc99e Initial load
duke
parents:
diff changeset
949 x = iter._key;
a61af66fc99e Initial load
duke
parents:
diff changeset
950 y = iter._value;
a61af66fc99e Initial load
duke
parents:
diff changeset
951 Production *p = (Production*)y;
a61af66fc99e Initial load
duke
parents:
diff changeset
952 if( p != NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
953 p->initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
954 }
a61af66fc99e Initial load
duke
parents:
diff changeset
955 }
a61af66fc99e Initial load
duke
parents:
diff changeset
956 }
a61af66fc99e Initial load
duke
parents:
diff changeset
957
a61af66fc99e Initial load
duke
parents:
diff changeset
958 Production *ProductionState::getProduction(const char *result) {
a61af66fc99e Initial load
duke
parents:
diff changeset
959 Production *p = (Production *)_production[result];
a61af66fc99e Initial load
duke
parents:
diff changeset
960 if( p == NULL ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
961 p = new Production(result, _constraint, knownInvalid);
a61af66fc99e Initial load
duke
parents:
diff changeset
962 _production.Insert(result, p);
a61af66fc99e Initial load
duke
parents:
diff changeset
963 }
a61af66fc99e Initial load
duke
parents:
diff changeset
964
a61af66fc99e Initial load
duke
parents:
diff changeset
965 return p;
a61af66fc99e Initial load
duke
parents:
diff changeset
966 }
a61af66fc99e Initial load
duke
parents:
diff changeset
967
a61af66fc99e Initial load
duke
parents:
diff changeset
968 void ProductionState::set_constraint(const char *constraint) {
a61af66fc99e Initial load
duke
parents:
diff changeset
969 _constraint = constraint;
a61af66fc99e Initial load
duke
parents:
diff changeset
970 }
a61af66fc99e Initial load
duke
parents:
diff changeset
971
a61af66fc99e Initial load
duke
parents:
diff changeset
972 const char *ProductionState::valid(const char *result) {
a61af66fc99e Initial load
duke
parents:
diff changeset
973 return getProduction(result)->valid();
a61af66fc99e Initial load
duke
parents:
diff changeset
974 }
a61af66fc99e Initial load
duke
parents:
diff changeset
975
a61af66fc99e Initial load
duke
parents:
diff changeset
976 void ProductionState::set_valid(const char *result) {
a61af66fc99e Initial load
duke
parents:
diff changeset
977 Production *p = getProduction(result);
a61af66fc99e Initial load
duke
parents:
diff changeset
978
a61af66fc99e Initial load
duke
parents:
diff changeset
979 // Update valid as allowed by current constraints
a61af66fc99e Initial load
duke
parents:
diff changeset
980 if( _constraint == noConstraint ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
981 p->_valid = knownValid;
a61af66fc99e Initial load
duke
parents:
diff changeset
982 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
983 if( p->_valid != knownValid ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
984 p->_valid = unknownValid;
a61af66fc99e Initial load
duke
parents:
diff changeset
985 }
a61af66fc99e Initial load
duke
parents:
diff changeset
986 }
a61af66fc99e Initial load
duke
parents:
diff changeset
987 }
a61af66fc99e Initial load
duke
parents:
diff changeset
988
a61af66fc99e Initial load
duke
parents:
diff changeset
989 Expr *ProductionState::cost_lb(const char *result) {
a61af66fc99e Initial load
duke
parents:
diff changeset
990 return getProduction(result)->cost_lb();
a61af66fc99e Initial load
duke
parents:
diff changeset
991 }
a61af66fc99e Initial load
duke
parents:
diff changeset
992
a61af66fc99e Initial load
duke
parents:
diff changeset
993 Expr *ProductionState::cost_ub(const char *result) {
a61af66fc99e Initial load
duke
parents:
diff changeset
994 return getProduction(result)->cost_ub();
a61af66fc99e Initial load
duke
parents:
diff changeset
995 }
a61af66fc99e Initial load
duke
parents:
diff changeset
996
a61af66fc99e Initial load
duke
parents:
diff changeset
997 void ProductionState::set_cost_bounds(const char *result, const Expr *cost, bool has_state_check, bool has_cost_check) {
a61af66fc99e Initial load
duke
parents:
diff changeset
998 Production *p = getProduction(result);
a61af66fc99e Initial load
duke
parents:
diff changeset
999
a61af66fc99e Initial load
duke
parents:
diff changeset
1000 if( p->_valid == knownInvalid ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1001 // Our cost bounds are not unknown, just not defined.
a61af66fc99e Initial load
duke
parents:
diff changeset
1002 p->_cost_lb = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1003 p->_cost_ub = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1004 } else if (has_state_check || _constraint != noConstraint) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1005 // The production is protected by a condition, so
a61af66fc99e Initial load
duke
parents:
diff changeset
1006 // the cost bounds may expand.
a61af66fc99e Initial load
duke
parents:
diff changeset
1007 // _cost_lb = min(cost, _cost_lb)
a61af66fc99e Initial load
duke
parents:
diff changeset
1008 if( cost->less_than_or_equal(p->_cost_lb) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1009 p->_cost_lb = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1010 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1011 // _cost_ub = max(cost, _cost_ub)
a61af66fc99e Initial load
duke
parents:
diff changeset
1012 if( p->_cost_ub->less_than_or_equal(cost) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1013 p->_cost_ub = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1014 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1015 } else if (has_cost_check) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1016 // The production has no condition check, but does
a61af66fc99e Initial load
duke
parents:
diff changeset
1017 // have a cost check that could reduce the upper
a61af66fc99e Initial load
duke
parents:
diff changeset
1018 // and/or lower bound.
a61af66fc99e Initial load
duke
parents:
diff changeset
1019 // _cost_lb = min(cost, _cost_lb)
a61af66fc99e Initial load
duke
parents:
diff changeset
1020 if( cost->less_than_or_equal(p->_cost_lb) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1021 p->_cost_lb = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1022 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1023 // _cost_ub = min(cost, _cost_ub)
a61af66fc99e Initial load
duke
parents:
diff changeset
1024 if( cost->less_than_or_equal(p->_cost_ub) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1025 p->_cost_ub = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1026 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1027 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1028 // The costs are unconditionally set.
a61af66fc99e Initial load
duke
parents:
diff changeset
1029 p->_cost_lb = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1030 p->_cost_ub = cost->clone();
a61af66fc99e Initial load
duke
parents:
diff changeset
1031 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1032
a61af66fc99e Initial load
duke
parents:
diff changeset
1033 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1034
a61af66fc99e Initial load
duke
parents:
diff changeset
1035 // Print out the dictionary contents as key-value pairs
603
dbbe28fc66b5 6778669: Patch from Red Hat -- fixes compilation errors
twisti
parents: 475
diff changeset
1036 static void print_key (const void* key) { fprintf(stdout, "%s", (char*) key); }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1037 static void print_production(const void* production) { fflush(stdout); ((Production*)production)->print(); }
a61af66fc99e Initial load
duke
parents:
diff changeset
1038
a61af66fc99e Initial load
duke
parents:
diff changeset
1039 void ProductionState::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1040 _production.print(print_key, print_production);
a61af66fc99e Initial load
duke
parents:
diff changeset
1041 }