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