1 /*
   2  * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // output_h.cpp - Class HPP file output routines for architecture definition
  26 #include "adlc.hpp"
  27 
  28 // The comment delimiter used in format statements after assembler instructions.
  29 #if defined(PPC64)
  30 #define commentSeperator "\t//"
  31 #else
  32 #define commentSeperator "!"
  33 #endif
  34 
  35 // Generate the #define that describes the number of registers.
  36 static void defineRegCount(FILE *fp, RegisterForm *registers) {
  37   if (registers) {
  38     int regCount =  AdlcVMDeps::Physical + registers->_rdefs.count();
  39     fprintf(fp,"\n");
  40     fprintf(fp,"// the number of reserved registers + machine registers.\n");
  41     fprintf(fp,"#define REG_COUNT    %d\n", regCount);
  42   }
  43 }
  44 
  45 // Output enumeration of machine register numbers
  46 // (1)
  47 // // Enumerate machine registers starting after reserved regs.
  48 // // in the order of occurrence in the register block.
  49 // enum MachRegisterNumbers {
  50 //   EAX_num = 0,
  51 //   ...
  52 //   _last_Mach_Reg
  53 // }
  54 void ArchDesc::buildMachRegisterNumbers(FILE *fp_hpp) {
  55   if (_register) {
  56     RegDef *reg_def = nullptr;
  57 
  58     // Output a #define for the number of machine registers
  59     defineRegCount(fp_hpp, _register);
  60 
  61     // Count all the Save_On_Entry and Always_Save registers
  62     int    saved_on_entry = 0;
  63     int  c_saved_on_entry = 0;
  64     _register->reset_RegDefs();
  65     while( (reg_def = _register->iter_RegDefs()) != nullptr ) {
  66       if( strcmp(reg_def->_callconv,"SOE") == 0 ||
  67           strcmp(reg_def->_callconv,"AS")  == 0 )  ++saved_on_entry;
  68       if( strcmp(reg_def->_c_conv,"SOE") == 0 ||
  69           strcmp(reg_def->_c_conv,"AS")  == 0 )  ++c_saved_on_entry;
  70     }
  71     fprintf(fp_hpp, "\n");
  72     fprintf(fp_hpp, "// the number of save_on_entry + always_saved registers.\n");
  73     fprintf(fp_hpp, "#define MAX_SAVED_ON_ENTRY_REG_COUNT    %d\n",   max(saved_on_entry,c_saved_on_entry));
  74     fprintf(fp_hpp, "#define     SAVED_ON_ENTRY_REG_COUNT    %d\n",   saved_on_entry);
  75     fprintf(fp_hpp, "#define   C_SAVED_ON_ENTRY_REG_COUNT    %d\n", c_saved_on_entry);
  76 
  77     // (1)
  78     // Build definition for enumeration of register numbers
  79     fprintf(fp_hpp, "\n");
  80     fprintf(fp_hpp, "// Enumerate machine register numbers starting after reserved regs.\n");
  81     fprintf(fp_hpp, "// in the order of occurrence in the register block.\n");
  82     fprintf(fp_hpp, "enum MachRegisterNumbers {\n");
  83 
  84     // Output the register number for each register in the allocation classes
  85     _register->reset_RegDefs();
  86     int i = 0;
  87     while( (reg_def = _register->iter_RegDefs()) != nullptr ) {
  88       fprintf(fp_hpp,"  %s_num,", reg_def->_regname);
  89       for (int j = 0; j < 20-(int)strlen(reg_def->_regname); j++) fprintf(fp_hpp, " ");
  90       fprintf(fp_hpp," // enum %3d, regnum %3d, reg encode %3s\n",
  91               i++,
  92               reg_def->register_num(),
  93               reg_def->register_encode());
  94     }
  95     // Finish defining enumeration
  96     fprintf(fp_hpp, "  _last_Mach_Reg            // %d\n", i);
  97     fprintf(fp_hpp, "};\n");
  98   }
  99 
 100   fprintf(fp_hpp, "\n// Size of register-mask in ints\n");
 101   fprintf(fp_hpp, "#define RM_SIZE_IN_INTS %d\n", RegisterForm::RegMask_Size());
 102   fprintf(fp_hpp, "// Minimum size of register-mask in ints\n");
 103   fprintf(fp_hpp, "#define RM_SIZE_IN_INTS_MIN %d\n", RegisterForm::words_for_regs());
 104   fprintf(fp_hpp, "// Unroll factor for loops over the data in a RegMask\n");
 105   fprintf(fp_hpp, "#define FORALL_BODY ");
 106   int len = RegisterForm::RegMask_Size();
 107   for( int i = 0; i < len; i++ )
 108     fprintf(fp_hpp, "BODY(%d) ",i);
 109   fprintf(fp_hpp, "\n\n");
 110 
 111   fprintf(fp_hpp,"class RegMask;\n");
 112   // All RegMasks are declared "extern const ..." in ad_<arch>.hpp
 113   // fprintf(fp_hpp,"extern RegMask STACK_OR_STACK_SLOTS_mask;\n\n");
 114 }
 115 
 116 
 117 // Output enumeration of machine register encodings
 118 // (2)
 119 // // Enumerate machine registers starting after reserved regs.
 120 // // in the order of occurrence in the alloc_class(es).
 121 // enum MachRegisterEncodes {
 122 //   EAX_enc = 0x00,
 123 //   ...
 124 // }
 125 void ArchDesc::buildMachRegisterEncodes(FILE *fp_hpp) {
 126   if (_register) {
 127     RegDef *reg_def = nullptr;
 128     RegDef *reg_def_next = nullptr;
 129 
 130     // (2)
 131     // Build definition for enumeration of encode values
 132     fprintf(fp_hpp, "\n");
 133     fprintf(fp_hpp, "// Enumerate machine registers starting after reserved regs.\n");
 134     fprintf(fp_hpp, "// in the order of occurrence in the alloc_class(es).\n");
 135     fprintf(fp_hpp, "enum MachRegisterEncodes {\n");
 136 
 137     // Find max enum string length.
 138     size_t maxlen = 0;
 139     _register->reset_RegDefs();
 140     reg_def = _register->iter_RegDefs();
 141     while (reg_def != nullptr) {
 142       size_t len = strlen(reg_def->_regname);
 143       if (len > maxlen) maxlen = len;
 144       reg_def = _register->iter_RegDefs();
 145     }
 146 
 147     // Output the register encoding for each register in the allocation classes
 148     _register->reset_RegDefs();
 149     reg_def_next = _register->iter_RegDefs();
 150     while( (reg_def = reg_def_next) != nullptr ) {
 151       reg_def_next = _register->iter_RegDefs();
 152       fprintf(fp_hpp,"  %s_enc", reg_def->_regname);
 153       for (size_t i = strlen(reg_def->_regname); i < maxlen; i++) fprintf(fp_hpp, " ");
 154       fprintf(fp_hpp," = %3s%s\n", reg_def->register_encode(), reg_def_next == nullptr? "" : "," );
 155     }
 156     // Finish defining enumeration
 157     fprintf(fp_hpp, "};\n");
 158 
 159   } // Done with register form
 160 }
 161 
 162 
 163 // Declare an array containing the machine register names, strings.
 164 static void declareRegNames(FILE *fp, RegisterForm *registers) {
 165   if (registers) {
 166 //    fprintf(fp,"\n");
 167 //    fprintf(fp,"// An array of character pointers to machine register names.\n");
 168 //    fprintf(fp,"extern const char *regName[];\n");
 169   }
 170 }
 171 
 172 // Declare an array containing the machine register sizes in 32-bit words.
 173 void ArchDesc::declareRegSizes(FILE *fp) {
 174 // regSize[] is not used
 175 }
 176 
 177 // Declare an array containing the machine register encoding values
 178 static void declareRegEncodes(FILE *fp, RegisterForm *registers) {
 179   if (registers) {
 180     // // //
 181     // fprintf(fp,"\n");
 182     // fprintf(fp,"// An array containing the machine register encode values\n");
 183     // fprintf(fp,"extern const char  regEncode[];\n");
 184   }
 185 }
 186 
 187 
 188 // ---------------------------------------------------------------------------
 189 //------------------------------Utilities to build Instruction Classes--------
 190 // ---------------------------------------------------------------------------
 191 static void out_RegMask(FILE *fp) {
 192   fprintf(fp,"  virtual const RegMask &out_RegMask() const;\n");
 193 }
 194 
 195 // ---------------------------------------------------------------------------
 196 //--------Utilities to build MachOper and MachNode derived Classes------------
 197 // ---------------------------------------------------------------------------
 198 
 199 //------------------------------Utilities to build Operand Classes------------
 200 static void in_RegMask(FILE *fp) {
 201   fprintf(fp,"  virtual const RegMask *in_RegMask(int index) const;\n");
 202 }
 203 
 204 static void declareConstStorage(FILE *fp, FormDict &globals, OperandForm *oper) {
 205   int i = 0;
 206   Component *comp;
 207 
 208   if (oper->num_consts(globals) == 0) return;
 209   // Iterate over the component list looking for constants
 210   oper->_components.reset();
 211   if ((comp = oper->_components.iter()) == nullptr) {
 212     assert(oper->num_consts(globals) == 1, "Bad component list detected.\n");
 213     const char *type = oper->ideal_type(globals);
 214     if (!strcmp(type, "ConI")) {
 215       if (i > 0) fprintf(fp,", ");
 216       fprintf(fp,"  int32_t        _c%d;\n", i);
 217     }
 218     else if (!strcmp(type, "ConP")) {
 219       if (i > 0) fprintf(fp,", ");
 220       fprintf(fp,"  const TypePtr *_c%d;\n", i);
 221     }
 222     else if (!strcmp(type, "ConN")) {
 223       if (i > 0) fprintf(fp,", ");
 224       fprintf(fp,"  const TypeNarrowOop *_c%d;\n", i);
 225     }
 226     else if (!strcmp(type, "ConNKlass")) {
 227       if (i > 0) fprintf(fp,", ");
 228       fprintf(fp,"  const TypeNarrowKlass *_c%d;\n", i);
 229     }
 230     else if (!strcmp(type, "ConL")) {
 231       if (i > 0) fprintf(fp,", ");
 232       fprintf(fp,"  jlong          _c%d;\n", i);
 233     }
 234     else if (!strcmp(type, "ConF")) {
 235       if (i > 0) fprintf(fp,", ");
 236       fprintf(fp,"  jfloat         _c%d;\n", i);
 237     }
 238     else if (!strcmp(type, "ConH")) {
 239       if (i > 0) fprintf(fp,", ");
 240       fprintf(fp,"  jshort        _c%d;\n", i);
 241     }
 242     else if (!strcmp(type, "ConD")) {
 243       if (i > 0) fprintf(fp,", ");
 244       fprintf(fp,"  jdouble        _c%d;\n", i);
 245     }
 246     else if (!strcmp(type, "Bool")) {
 247       fprintf(fp,"private:\n");
 248       fprintf(fp,"  BoolTest::mask _c%d;\n", i);
 249       fprintf(fp,"public:\n");
 250     }
 251     else {
 252       assert(0, "Non-constant operand lacks component list.");
 253     }
 254   } // end if null
 255   else {
 256     oper->_components.reset();
 257     while ((comp = oper->_components.iter()) != nullptr) {
 258       if (!strcmp(comp->base_type(globals), "ConI")) {
 259         fprintf(fp,"  jint             _c%d;\n", i);
 260         i++;
 261       }
 262       else if (!strcmp(comp->base_type(globals), "ConP")) {
 263         fprintf(fp,"  const TypePtr *_c%d;\n", i);
 264         i++;
 265       }
 266       else if (!strcmp(comp->base_type(globals), "ConN")) {
 267         fprintf(fp,"  const TypePtr *_c%d;\n", i);
 268         i++;
 269       }
 270       else if (!strcmp(comp->base_type(globals), "ConNKlass")) {
 271         fprintf(fp,"  const TypePtr *_c%d;\n", i);
 272         i++;
 273       }
 274       else if (!strcmp(comp->base_type(globals), "ConL")) {
 275         fprintf(fp,"  jlong            _c%d;\n", i);
 276         i++;
 277       }
 278       else if (!strcmp(comp->base_type(globals), "ConH")) {
 279         fprintf(fp,"  jshort            _c%d;\n", i);
 280         i++;
 281       }
 282       else if (!strcmp(comp->base_type(globals), "ConF")) {
 283         fprintf(fp,"  jfloat           _c%d;\n", i);
 284         i++;
 285       }
 286       else if (!strcmp(comp->base_type(globals), "ConD")) {
 287         fprintf(fp,"  jdouble          _c%d;\n", i);
 288         i++;
 289       }
 290     }
 291   }
 292 }
 293 
 294 // Declare constructor.
 295 // Parameters start with condition code, then all other constants
 296 //
 297 // (0) public:
 298 // (1)  MachXOper(int32 ccode, int32 c0, int32 c1, ..., int32 cn)
 299 // (2)     : _ccode(ccode), _c0(c0), _c1(c1), ..., _cn(cn) { }
 300 //
 301 static void defineConstructor(FILE *fp, const char *name, uint num_consts,
 302                               ComponentList &lst, bool is_ideal_bool,
 303                               Form::DataType constant_type, FormDict &globals) {
 304   fprintf(fp,"public:\n");
 305   // generate line (1)
 306   fprintf(fp,"  %sOper(", name);
 307   if( num_consts == 0 ) {
 308     fprintf(fp,") {}\n");
 309     return;
 310   }
 311 
 312   // generate parameters for constants
 313   uint i = 0;
 314   Component *comp;
 315   lst.reset();
 316   if ((comp = lst.iter()) == nullptr) {
 317     assert(num_consts == 1, "Bad component list detected.\n");
 318     switch( constant_type ) {
 319     case Form::idealI : {
 320       fprintf(fp,is_ideal_bool ? "BoolTest::mask c%d" : "int32_t c%d", i);
 321       break;
 322     }
 323     case Form::idealN :      { fprintf(fp,"const TypeNarrowOop *c%d", i); break; }
 324     case Form::idealNKlass : { fprintf(fp,"const TypeNarrowKlass *c%d", i); break; }
 325     case Form::idealP :      { fprintf(fp,"const TypePtr *c%d", i); break; }
 326     case Form::idealL :      { fprintf(fp,"jlong c%d", i);   break;        }
 327     case Form::idealH :      { fprintf(fp,"jshort c%d", i);   break;        }
 328     case Form::idealF :      { fprintf(fp,"jfloat c%d", i);  break;        }
 329     case Form::idealD :      { fprintf(fp,"jdouble c%d", i); break;        }
 330     default:
 331       assert(!is_ideal_bool, "Non-constant operand lacks component list.");
 332       break;
 333     }
 334   } // end if null
 335   else {
 336     lst.reset();
 337     while((comp = lst.iter()) != nullptr) {
 338       if (!strcmp(comp->base_type(globals), "ConI")) {
 339         if (i > 0) fprintf(fp,", ");
 340         fprintf(fp,"int32_t c%d", i);
 341         i++;
 342       }
 343       else if (!strcmp(comp->base_type(globals), "ConP")) {
 344         if (i > 0) fprintf(fp,", ");
 345         fprintf(fp,"const TypePtr *c%d", i);
 346         i++;
 347       }
 348       else if (!strcmp(comp->base_type(globals), "ConN")) {
 349         if (i > 0) fprintf(fp,", ");
 350         fprintf(fp,"const TypePtr *c%d", i);
 351         i++;
 352       }
 353       else if (!strcmp(comp->base_type(globals), "ConNKlass")) {
 354         if (i > 0) fprintf(fp,", ");
 355         fprintf(fp,"const TypePtr *c%d", i);
 356         i++;
 357       }
 358       else if (!strcmp(comp->base_type(globals), "ConL")) {
 359         if (i > 0) fprintf(fp,", ");
 360         fprintf(fp,"jlong c%d", i);
 361         i++;
 362       }
 363       else if (!strcmp(comp->base_type(globals), "ConF")) {
 364         if (i > 0) fprintf(fp,", ");
 365         fprintf(fp,"jfloat c%d", i);
 366         i++;
 367       }
 368       else if (!strcmp(comp->base_type(globals), "ConD")) {
 369         if (i > 0) fprintf(fp,", ");
 370         fprintf(fp,"jdouble c%d", i);
 371         i++;
 372       }
 373       else if (!strcmp(comp->base_type(globals), "Bool")) {
 374         if (i > 0) fprintf(fp,", ");
 375         fprintf(fp,"BoolTest::mask c%d", i);
 376         i++;
 377       }
 378     }
 379   }
 380   // finish line (1) and start line (2)
 381   fprintf(fp,")  : ");
 382   // generate initializers for constants
 383   i = 0;
 384   fprintf(fp,"_c%d(c%d)", i, i);
 385   for( i = 1; i < num_consts; ++i) {
 386     fprintf(fp,", _c%d(c%d)", i, i);
 387   }
 388   // The body for the constructor is empty
 389   fprintf(fp," {}\n");
 390 }
 391 
 392 // ---------------------------------------------------------------------------
 393 // Utilities to generate format rules for machine operands and instructions
 394 // ---------------------------------------------------------------------------
 395 
 396 // Generate the format rule for condition codes
 397 static void defineCCodeDump(OperandForm* oper, FILE *fp, int i) {
 398   assert(oper != nullptr, "what");
 399   CondInterface* cond = oper->_interface->is_CondInterface();
 400   fprintf(fp, "       if( _c%d == BoolTest::eq ) st->print_raw(\"%s\");\n",i,cond->_equal_format);
 401   fprintf(fp, "  else if( _c%d == BoolTest::ne ) st->print_raw(\"%s\");\n",i,cond->_not_equal_format);
 402   fprintf(fp, "  else if( _c%d == BoolTest::le ) st->print_raw(\"%s\");\n",i,cond->_less_equal_format);
 403   fprintf(fp, "  else if( _c%d == BoolTest::ge ) st->print_raw(\"%s\");\n",i,cond->_greater_equal_format);
 404   fprintf(fp, "  else if( _c%d == BoolTest::lt ) st->print_raw(\"%s\");\n",i,cond->_less_format);
 405   fprintf(fp, "  else if( _c%d == BoolTest::gt ) st->print_raw(\"%s\");\n",i,cond->_greater_format);
 406   fprintf(fp, "  else if( _c%d == BoolTest::overflow ) st->print_raw(\"%s\");\n",i,cond->_overflow_format);
 407   fprintf(fp, "  else if( _c%d == BoolTest::no_overflow ) st->print_raw(\"%s\");\n",i,cond->_no_overflow_format);
 408 }
 409 
 410 // Output code that dumps constant values, increment "i" if type is constant
 411 static uint dump_spec_constant(FILE *fp, const char *ideal_type, uint i, OperandForm* oper) {
 412   if (!strcmp(ideal_type, "ConI")) {
 413     fprintf(fp,"   st->print(\"#%%d\", _c%d);\n", i);
 414     fprintf(fp,"   st->print(\"/0x%%08x\", _c%d);\n", i);
 415     ++i;
 416   }
 417   else if (!strcmp(ideal_type, "ConH")) {
 418     fprintf(fp,"   st->print(\"#%%d\", _c%d);\n", i);
 419     fprintf(fp,"   st->print(\"/0x%%08x\", _c%d);\n", i);
 420     ++i;
 421   }
 422   else if (!strcmp(ideal_type, "ConP")) {
 423     fprintf(fp,"    _c%d->dump_on(st);\n", i);
 424     ++i;
 425   }
 426   else if (!strcmp(ideal_type, "ConN")) {
 427     fprintf(fp,"    _c%d->dump_on(st);\n", i);
 428     ++i;
 429   }
 430   else if (!strcmp(ideal_type, "ConNKlass")) {
 431     fprintf(fp,"    _c%d->dump_on(st);\n", i);
 432     ++i;
 433   }
 434   else if (!strcmp(ideal_type, "ConL")) {
 435     fprintf(fp,"    st->print(\"#\" INT64_FORMAT, (int64_t)_c%d);\n", i);
 436     fprintf(fp,"    st->print(\"/\" UINT64_FORMAT_X_0, (uint64_t)_c%d);\n", i);
 437     ++i;
 438   }
 439   else if (!strcmp(ideal_type, "ConF")) {
 440     fprintf(fp,"    st->print(\"#%%f\", _c%d);\n", i);
 441     fprintf(fp,"    jint _c%di = JavaValue(_c%d).get_jint();\n", i, i);
 442     fprintf(fp,"    st->print(\"/0x%%x/\", _c%di);\n", i);
 443     ++i;
 444   }
 445   else if (!strcmp(ideal_type, "ConD")) {
 446     fprintf(fp,"    st->print(\"#%%f\", _c%d);\n", i);
 447     fprintf(fp,"    jlong _c%dl = JavaValue(_c%d).get_jlong();\n", i, i);
 448     fprintf(fp,"    st->print(\"/\" UINT64_FORMAT_X_0, (uint64_t)_c%dl);\n", i);
 449     ++i;
 450   }
 451   else if (!strcmp(ideal_type, "Bool")) {
 452     defineCCodeDump(oper, fp,i);
 453     ++i;
 454   }
 455 
 456   return i;
 457 }
 458 
 459 // Generate the format rule for an operand
 460 void gen_oper_format(FILE *fp, FormDict &globals, OperandForm &oper, bool for_c_file = false) {
 461   if (!for_c_file) {
 462     // invoked after output #ifndef PRODUCT to ad_<arch>.hpp
 463     // compile the bodies separately, to cut down on recompilations
 464     fprintf(fp,"  virtual void           int_format(PhaseRegAlloc *ra, const MachNode *node, outputStream *st) const;\n");
 465     fprintf(fp,"  virtual void           ext_format(PhaseRegAlloc *ra, const MachNode *node, int idx, outputStream *st) const;\n");
 466     return;
 467   }
 468 
 469   // Local pointer indicates remaining part of format rule
 470   int idx = 0;                   // position of operand in match rule
 471 
 472   // Generate internal format function, used when stored locally
 473   fprintf(fp, "\n#ifndef PRODUCT\n");
 474   fprintf(fp,"void %sOper::int_format(PhaseRegAlloc *ra, const MachNode *node, outputStream *st) const {\n", oper._ident);
 475   // Generate the user-defined portion of the format
 476   if (oper._format) {
 477     if ( oper._format->_strings.count() != 0 ) {
 478       // No initialization code for int_format
 479 
 480       // Build the format from the entries in strings and rep_vars
 481       const char  *string  = nullptr;
 482       oper._format->_rep_vars.reset();
 483       oper._format->_strings.reset();
 484       while ( (string = oper._format->_strings.iter()) != nullptr ) {
 485 
 486         // Check if this is a standard string or a replacement variable
 487         if ( string != NameList::_signal ) {
 488           // Normal string
 489           // Pass through to st->print
 490           fprintf(fp,"  st->print_raw(\"%s\");\n", string);
 491         } else {
 492           // Replacement variable
 493           const char *rep_var = oper._format->_rep_vars.iter();
 494           // Check that it is a local name, and an operand
 495           const Form* form = oper._localNames[rep_var];
 496           if (form == nullptr) {
 497             globalAD->syntax_err(oper._linenum,
 498                                  "\'%s\' not found in format for %s\n", rep_var, oper._ident);
 499             assert(form, "replacement variable was not found in local names");
 500           }
 501           OperandForm *op      = form->is_operand();
 502           // Get index if register or constant
 503           if ( op->_matrule && op->_matrule->is_base_register(globals) ) {
 504             idx  = oper.register_position( globals, rep_var);
 505           }
 506           else if (op->_matrule && op->_matrule->is_base_constant(globals)) {
 507             idx  = oper.constant_position( globals, rep_var);
 508           } else {
 509             idx = 0;
 510           }
 511 
 512           // output invocation of "$..."s format function
 513           if ( op != nullptr ) op->int_format(fp, globals, idx);
 514 
 515           if ( idx == -1 ) {
 516             fprintf(stderr,
 517                     "Using a name, %s, that isn't in match rule\n", rep_var);
 518             assert( strcmp(op->_ident,"label")==0, "Unimplemented");
 519           }
 520         } // Done with a replacement variable
 521       } // Done with all format strings
 522     } else {
 523       // Default formats for base operands (RegI, RegP, ConI, ConP, ...)
 524       oper.int_format(fp, globals, 0);
 525     }
 526 
 527   } else { // oper._format == null
 528     // Provide a few special case formats where the AD writer cannot.
 529     if ( strcmp(oper._ident,"Universe")==0 ) {
 530       fprintf(fp, "  st->print(\"$$univ\");\n");
 531     }
 532     // labelOper::int_format is defined in ad_<...>.cpp
 533   }
 534   // ALWAYS! Provide a special case output for condition codes.
 535   if( oper.is_ideal_bool() ) {
 536     defineCCodeDump(&oper, fp,0);
 537   }
 538   fprintf(fp,"}\n");
 539 
 540   // Generate external format function, when data is stored externally
 541   fprintf(fp,"void %sOper::ext_format(PhaseRegAlloc *ra, const MachNode *node, int idx, outputStream *st) const {\n", oper._ident);
 542   // Generate the user-defined portion of the format
 543   if (oper._format) {
 544     if ( oper._format->_strings.count() != 0 ) {
 545 
 546       // Check for a replacement string "$..."
 547       if ( oper._format->_rep_vars.count() != 0 ) {
 548         // Initialization code for ext_format
 549       }
 550 
 551       // Build the format from the entries in strings and rep_vars
 552       const char  *string  = nullptr;
 553       oper._format->_rep_vars.reset();
 554       oper._format->_strings.reset();
 555       while ( (string = oper._format->_strings.iter()) != nullptr ) {
 556 
 557         // Check if this is a standard string or a replacement variable
 558         if ( string != NameList::_signal ) {
 559           // Normal string
 560           // Pass through to st->print
 561           fprintf(fp,"  st->print_raw(\"%s\");\n", string);
 562         } else {
 563           // Replacement variable
 564           const char *rep_var = oper._format->_rep_vars.iter();
 565          // Check that it is a local name, and an operand
 566           const Form* form = oper._localNames[rep_var];
 567           if (form == nullptr) {
 568             globalAD->syntax_err(oper._linenum,
 569                                  "\'%s\' not found in format for %s\n", rep_var, oper._ident);
 570             assert(form, "replacement variable was not found in local names");
 571           }
 572           OperandForm *op      = form->is_operand();
 573           // Get index if register or constant
 574           if ( op->_matrule && op->_matrule->is_base_register(globals) ) {
 575             idx  = oper.register_position( globals, rep_var);
 576           }
 577           else if (op->_matrule && op->_matrule->is_base_constant(globals)) {
 578             idx  = oper.constant_position( globals, rep_var);
 579           } else {
 580             idx = 0;
 581           }
 582           // output invocation of "$..."s format function
 583           if ( op != nullptr )   op->ext_format(fp, globals, idx);
 584 
 585           // Lookup the index position of the replacement variable
 586           idx      = oper._components.operand_position_format(rep_var, &oper);
 587           if ( idx == -1 ) {
 588             fprintf(stderr,
 589                     "Using a name, %s, that isn't in match rule\n", rep_var);
 590             assert( strcmp(op->_ident,"label")==0, "Unimplemented");
 591           }
 592         } // Done with a replacement variable
 593       } // Done with all format strings
 594 
 595     } else {
 596       // Default formats for base operands (RegI, RegP, ConI, ConP, ...)
 597       oper.ext_format(fp, globals, 0);
 598     }
 599   } else { // oper._format is null
 600     // Provide a few special case formats where the AD writer cannot.
 601     if ( strcmp(oper._ident,"Universe")==0 ) {
 602       fprintf(fp, "  st->print(\"$$univ\");\n");
 603     }
 604     // labelOper::ext_format is defined in ad_<...>.cpp
 605   }
 606   // ALWAYS! Provide a special case output for condition codes.
 607   if( oper.is_ideal_bool() ) {
 608     defineCCodeDump(&oper, fp,0);
 609   }
 610   fprintf(fp, "}\n");
 611   fprintf(fp, "#endif\n");
 612 }
 613 
 614 
 615 // Generate the format rule for an instruction
 616 void gen_inst_format(FILE *fp, FormDict &globals, InstructForm &inst, bool for_c_file = false) {
 617   if (!for_c_file) {
 618     // compile the bodies separately, to cut down on recompilations
 619     // #ifndef PRODUCT region generated by caller
 620     fprintf(fp,"  virtual void           format(PhaseRegAlloc *ra, outputStream *st) const;\n");
 621     return;
 622   }
 623 
 624   // Define the format function
 625   fprintf(fp, "#ifndef PRODUCT\n");
 626   fprintf(fp, "void %sNode::format(PhaseRegAlloc *ra, outputStream *st) const {\n", inst._ident);
 627 
 628   // Generate the user-defined portion of the format
 629   if( inst._format ) {
 630     // If there are replacement variables,
 631     // Generate index values needed for determining the operand position
 632     if( inst._format->_rep_vars.count() )
 633       inst.index_temps(fp, globals);
 634 
 635     // Build the format from the entries in strings and rep_vars
 636     const char  *string  = nullptr;
 637     inst._format->_rep_vars.reset();
 638     inst._format->_strings.reset();
 639     while( (string = inst._format->_strings.iter()) != nullptr ) {
 640       fprintf(fp,"  ");
 641       // Check if this is a standard string or a replacement variable
 642       if( string == NameList::_signal ) { // Replacement variable
 643         const char* rep_var =  inst._format->_rep_vars.iter();
 644         inst.rep_var_format( fp, rep_var);
 645       } else if( string == NameList::_signal3 ) { // Replacement variable in raw text
 646         const char* rep_var =  inst._format->_rep_vars.iter();
 647         const Form *form   = inst._localNames[rep_var];
 648         if (form == nullptr) {
 649           fprintf(stderr, "unknown replacement variable in format statement: '%s'\n", rep_var);
 650           assert(false, "ShouldNotReachHere()");
 651         }
 652         OpClassForm *opc   = form->is_opclass();
 653         assert( opc, "replacement variable was not found in local names");
 654         // Lookup the index position of the replacement variable
 655         int idx  = inst.operand_position_format(rep_var);
 656         if ( idx == -1 ) {
 657           assert( strcmp(opc->_ident,"label")==0, "Unimplemented");
 658           assert( false, "ShouldNotReachHere()");
 659         }
 660 
 661         if (inst.is_noninput_operand(idx)) {
 662           assert( false, "ShouldNotReachHere()");
 663         } else {
 664           // Output the format call for this operand
 665           fprintf(fp,"opnd_array(%d)",idx);
 666         }
 667         rep_var =  inst._format->_rep_vars.iter();
 668         inst._format->_strings.iter();
 669         if ( strcmp(rep_var,"$constant") == 0 && opc->is_operand()) {
 670           Form::DataType constant_type = form->is_operand()->is_base_constant(globals);
 671           if ( constant_type == Form::idealD ) {
 672             fprintf(fp,"->constantD()");
 673           } else if ( constant_type == Form::idealF ) {
 674             fprintf(fp,"->constantF()");
 675           } else if ( constant_type == Form::idealL ) {
 676             fprintf(fp,"->constantL()");
 677           } else {
 678             fprintf(fp,"->constant()");
 679           }
 680         } else if ( strcmp(rep_var,"$cmpcode") == 0) {
 681             fprintf(fp,"->ccode()");
 682         } else {
 683           assert( false, "ShouldNotReachHere()");
 684         }
 685       } else if( string == NameList::_signal2 ) // Raw program text
 686         fputs(inst._format->_strings.iter(), fp);
 687       else
 688         fprintf(fp,"st->print_raw(\"%s\");\n", string);
 689     } // Done with all format strings
 690   } // Done generating the user-defined portion of the format
 691 
 692   // Add call debug info automatically
 693   Form::CallType call_type = inst.is_ideal_call();
 694   if( call_type != Form::invalid_type ) {
 695     switch( call_type ) {
 696     case Form::JAVA_DYNAMIC:
 697       fprintf(fp,"  _method->print_short_name(st);\n");
 698       break;
 699     case Form::JAVA_STATIC:
 700       fprintf(fp,"  if( _method ) _method->print_short_name(st);\n");
 701       fprintf(fp,"  else st->print(\" wrapper for: %%s\", _name);\n");
 702       fprintf(fp,"  if( !_method ) dump_trap_args(st);\n");
 703       break;
 704     case Form::JAVA_COMPILED:
 705     case Form::JAVA_INTERP:
 706       break;
 707     case Form::JAVA_RUNTIME:
 708     case Form::JAVA_LEAF:
 709     case Form::JAVA_NATIVE:
 710       fprintf(fp,"  st->print(\" %%s\", _name);");
 711       break;
 712     default:
 713       assert(0,"ShouldNotReachHere");
 714     }
 715     fprintf(fp,  "  st->cr();\n" );
 716     fprintf(fp,  "  if (_jvms) _jvms->format(ra, this, st); else st->print_cr(\"        No JVM State Info\");\n" );
 717     fprintf(fp,  "  st->print(\"        # \");\n" );
 718     fprintf(fp,  "  if( _jvms && _oop_map ) _oop_map->print_on(st);\n");
 719   }
 720   else if(inst.is_ideal_safepoint()) {
 721     fprintf(fp,  "  st->print_raw(\"\");\n" );
 722     fprintf(fp,  "  if (_jvms) _jvms->format(ra, this, st); else st->print_cr(\"        No JVM State Info\");\n" );
 723     fprintf(fp,  "  st->print(\"        # \");\n" );
 724     fprintf(fp,  "  if( _jvms && _oop_map ) _oop_map->print_on(st);\n");
 725   }
 726   else if( inst.is_ideal_if() ) {
 727     fprintf(fp,  "  st->print(\"  P=%%f C=%%f\",_prob,_fcnt);\n" );
 728   }
 729   else if( inst.is_ideal_mem() ) {
 730     // Print out the field name if available to improve readability
 731     fprintf(fp,  "  if (ra->C->alias_type(adr_type())->field() != nullptr) {\n");
 732     fprintf(fp,  "    ciField* f = ra->C->alias_type(adr_type())->field();\n");
 733     fprintf(fp,  "    st->print(\" %s Field: \");\n", commentSeperator);
 734     fprintf(fp,  "    if (f->is_volatile())\n");
 735     fprintf(fp,  "      st->print(\"volatile \");\n");
 736     fprintf(fp,  "    f->holder()->name()->print_symbol_on(st);\n");
 737     fprintf(fp,  "    st->print(\".\");\n");
 738     fprintf(fp,  "    f->name()->print_symbol_on(st);\n");
 739     fprintf(fp,  "    if (f->is_constant())\n");
 740     fprintf(fp,  "      st->print(\" (constant)\");\n");
 741     fprintf(fp,  "  } else {\n");
 742     // Make sure 'Volatile' gets printed out
 743     fprintf(fp,  "    if (ra->C->alias_type(adr_type())->is_volatile())\n");
 744     fprintf(fp,  "      st->print(\" volatile!\");\n");
 745     fprintf(fp,  "  }\n");
 746   }
 747 
 748   // Complete the definition of the format function
 749   fprintf(fp, "}\n#endif\n");
 750 }
 751 
 752 void ArchDesc::declare_pipe_classes(FILE *fp_hpp) {
 753   if (!_pipeline)
 754     return;
 755 
 756   fprintf(fp_hpp, "\n");
 757   fprintf(fp_hpp, "// Pipeline_Use_Cycle_Mask Class\n");
 758   fprintf(fp_hpp, "class Pipeline_Use_Cycle_Mask {\n");
 759 
 760   if (_pipeline->_maxcycleused <= 32) {
 761     fprintf(fp_hpp, "protected:\n");
 762     fprintf(fp_hpp, "  uint32_t _mask;\n\n");
 763     fprintf(fp_hpp, "public:\n");
 764     fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask() : _mask(0) {}\n\n");
 765     fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask(uint32_t mask) : _mask(mask) {}\n\n");
 766     fprintf(fp_hpp, "  bool overlaps(const Pipeline_Use_Cycle_Mask &in2) const {\n");
 767     fprintf(fp_hpp, "    return ((_mask & in2._mask) != 0);\n");
 768     fprintf(fp_hpp, "  }\n\n");
 769     fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask& operator<<=(int n) {\n");
 770     fprintf(fp_hpp, "    _mask <<= (n < 32) ? n : 31;\n");
 771     fprintf(fp_hpp, "    return *this;\n");
 772     fprintf(fp_hpp, "  }\n\n");
 773     fprintf(fp_hpp, "  void Or(const Pipeline_Use_Cycle_Mask &in2) {\n");
 774     fprintf(fp_hpp, "    _mask |= in2._mask;\n");
 775     fprintf(fp_hpp, "  }\n\n");
 776     fprintf(fp_hpp, "  friend Pipeline_Use_Cycle_Mask operator&(const Pipeline_Use_Cycle_Mask &, const Pipeline_Use_Cycle_Mask &);\n");
 777     fprintf(fp_hpp, "  friend Pipeline_Use_Cycle_Mask operator|(const Pipeline_Use_Cycle_Mask &, const Pipeline_Use_Cycle_Mask &);\n\n");
 778   }
 779   else {
 780     fprintf(fp_hpp, "protected:\n");
 781     uint masklen = (_pipeline->_maxcycleused + 31) >> 5;
 782     uint l;
 783     fprintf(fp_hpp, "  uint32_t ");
 784     for (l = 1; l <= masklen; l++)
 785       fprintf(fp_hpp, "_mask%d%s", l, l < masklen ? ", " : ";\n\n");
 786     fprintf(fp_hpp, "public:\n");
 787     fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask() : ");
 788     for (l = 1; l <= masklen; l++)
 789       fprintf(fp_hpp, "_mask%d(0)%s", l, l < masklen ? ", " : " {}\n\n");
 790     fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask(");
 791     for (l = 1; l <= masklen; l++)
 792       fprintf(fp_hpp, "uint32_t mask%d%s", l, l < masklen ? ", " : ") : ");
 793     for (l = 1; l <= masklen; l++)
 794       fprintf(fp_hpp, "_mask%d(mask%d)%s", l, l, l < masklen ? ", " : " {}\n\n");
 795 
 796     fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask intersect(const Pipeline_Use_Cycle_Mask &in2) {\n");
 797     fprintf(fp_hpp, "    Pipeline_Use_Cycle_Mask out;\n");
 798     for (l = 1; l <= masklen; l++)
 799       fprintf(fp_hpp, "    out._mask%d = _mask%d & in2._mask%d;\n", l, l, l);
 800     fprintf(fp_hpp, "    return out;\n");
 801     fprintf(fp_hpp, "  }\n\n");
 802     fprintf(fp_hpp, "  bool overlaps(const Pipeline_Use_Cycle_Mask &in2) const {\n");
 803     fprintf(fp_hpp, "    return ");
 804     for (l = 1; l <= masklen; l++)
 805       fprintf(fp_hpp, "((_mask%d & in2._mask%d) != 0)%s", l, l, l < masklen ? " || " : "");
 806     fprintf(fp_hpp, ";\n");
 807     fprintf(fp_hpp, "  }\n\n");
 808     fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask& operator<<=(int n) {\n");
 809     fprintf(fp_hpp, "    if (n >= 32)\n");
 810     fprintf(fp_hpp, "      do {\n       ");
 811     for (l = masklen; l > 1; l--)
 812       fprintf(fp_hpp, " _mask%d = _mask%d;", l, l-1);
 813     fprintf(fp_hpp, " _mask%d = 0;\n", 1);
 814     fprintf(fp_hpp, "      } while ((n -= 32) >= 32);\n\n");
 815     fprintf(fp_hpp, "    if (n > 0) {\n");
 816     fprintf(fp_hpp, "      uint m = 32 - n;\n");
 817     fprintf(fp_hpp, "      uint32_t mask = (1 << n) - 1;\n");
 818     fprintf(fp_hpp, "      uint32_t temp%d = mask & (_mask%d >> m); _mask%d <<= n;\n", 2, 1, 1);
 819     for (l = 2; l < masklen; l++) {
 820       fprintf(fp_hpp, "      uint32_t temp%d = mask & (_mask%d >> m); _mask%d <<= n; _mask%d |= temp%d;\n", l+1, l, l, l, l);
 821     }
 822     fprintf(fp_hpp, "      _mask%d <<= n; _mask%d |= temp%d;\n", masklen, masklen, masklen);
 823     fprintf(fp_hpp, "    }\n");
 824 
 825     fprintf(fp_hpp, "    return *this;\n");
 826     fprintf(fp_hpp, "  }\n\n");
 827     fprintf(fp_hpp, "  void Or(const Pipeline_Use_Cycle_Mask &);\n\n");
 828     fprintf(fp_hpp, "  friend Pipeline_Use_Cycle_Mask operator&(const Pipeline_Use_Cycle_Mask &, const Pipeline_Use_Cycle_Mask &);\n");
 829     fprintf(fp_hpp, "  friend Pipeline_Use_Cycle_Mask operator|(const Pipeline_Use_Cycle_Mask &, const Pipeline_Use_Cycle_Mask &);\n\n");
 830   }
 831 
 832   fprintf(fp_hpp, "  friend class Pipeline_Use;\n\n");
 833   fprintf(fp_hpp, "  friend class Pipeline_Use_Element;\n\n");
 834   fprintf(fp_hpp, "};\n\n");
 835 
 836   uint rescount = 0;
 837   const char *resource;
 838 
 839   for (_pipeline->_reslist.reset(); (resource = _pipeline->_reslist.iter()) != nullptr;) {
 840       if (_pipeline->_resdict[resource]->is_resource()->is_discrete()) {
 841         rescount++;
 842       }
 843     }
 844 
 845   fprintf(fp_hpp, "// Pipeline_Use_Element Class\n");
 846   fprintf(fp_hpp, "class Pipeline_Use_Element {\n");
 847   fprintf(fp_hpp, "protected:\n");
 848   fprintf(fp_hpp, "  // Mask of used functional units\n");
 849   fprintf(fp_hpp, "  uint _used;\n\n");
 850   fprintf(fp_hpp, "  // Lower and upper bound of functional unit number range\n");
 851   fprintf(fp_hpp, "  uint _lb, _ub;\n\n");
 852   fprintf(fp_hpp, "  // Indicates multiple functionals units available\n");
 853   fprintf(fp_hpp, "  bool _multiple;\n\n");
 854   fprintf(fp_hpp, "  // Mask of specific used cycles\n");
 855   fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask _mask;\n\n");
 856   fprintf(fp_hpp, "public:\n");
 857   fprintf(fp_hpp, "  Pipeline_Use_Element() {}\n\n");
 858   fprintf(fp_hpp, "  Pipeline_Use_Element(uint used, uint lb, uint ub, bool multiple, Pipeline_Use_Cycle_Mask mask)\n");
 859   fprintf(fp_hpp, "  : _used(used), _lb(lb), _ub(ub), _multiple(multiple), _mask(mask) {}\n\n");
 860   fprintf(fp_hpp, "  uint used() const { return _used; }\n\n");
 861   fprintf(fp_hpp, "  uint lowerBound() const { return _lb; }\n\n");
 862   fprintf(fp_hpp, "  uint upperBound() const { return _ub; }\n\n");
 863   fprintf(fp_hpp, "  bool multiple() const { return _multiple; }\n\n");
 864   fprintf(fp_hpp, "  Pipeline_Use_Cycle_Mask mask() const { return _mask; }\n\n");
 865   fprintf(fp_hpp, "  bool overlaps(const Pipeline_Use_Element &in2) const {\n");
 866   fprintf(fp_hpp, "    return ((_used & in2._used) != 0 && _mask.overlaps(in2._mask));\n");
 867   fprintf(fp_hpp, "  }\n\n");
 868   fprintf(fp_hpp, "  void step(uint cycles) {\n");
 869   fprintf(fp_hpp, "    _used = 0;\n");
 870   fprintf(fp_hpp, "    _mask <<= cycles;\n");
 871   fprintf(fp_hpp, "  }\n\n");
 872   fprintf(fp_hpp, "  friend class Pipeline_Use;\n");
 873   fprintf(fp_hpp, "};\n\n");
 874 
 875   fprintf(fp_hpp, "// Pipeline_Use Class\n");
 876   fprintf(fp_hpp, "class Pipeline_Use {\n");
 877   fprintf(fp_hpp, "protected:\n");
 878   fprintf(fp_hpp, "  // These resources can be used\n");
 879   fprintf(fp_hpp, "  uint _resources_used;\n\n");
 880   fprintf(fp_hpp, "  // These resources are used; excludes multiple choice functional units\n");
 881   fprintf(fp_hpp, "  uint _resources_used_exclusively;\n\n");
 882   fprintf(fp_hpp, "  // Number of elements\n");
 883   fprintf(fp_hpp, "  uint _count;\n\n");
 884   fprintf(fp_hpp, "  // This is the array of Pipeline_Use_Elements\n");
 885   fprintf(fp_hpp, "  Pipeline_Use_Element * _elements;\n\n");
 886   fprintf(fp_hpp, "public:\n");
 887   fprintf(fp_hpp, "  Pipeline_Use(uint resources_used, uint resources_used_exclusively, uint count, Pipeline_Use_Element *elements)\n");
 888   fprintf(fp_hpp, "  : _resources_used(resources_used)\n");
 889   fprintf(fp_hpp, "  , _resources_used_exclusively(resources_used_exclusively)\n");
 890   fprintf(fp_hpp, "  , _count(count)\n");
 891   fprintf(fp_hpp, "  , _elements(elements)\n");
 892   fprintf(fp_hpp, "  {}\n\n");
 893   fprintf(fp_hpp, "  uint resourcesUsed() const { return _resources_used; }\n\n");
 894   fprintf(fp_hpp, "  uint resourcesUsedExclusively() const { return _resources_used_exclusively; }\n\n");
 895   fprintf(fp_hpp, "  uint count() const { return _count; }\n\n");
 896   fprintf(fp_hpp, "  Pipeline_Use_Element * element(uint i) const { return &_elements[i]; }\n\n");
 897   fprintf(fp_hpp, "  uint full_latency(uint delay, const Pipeline_Use &pred) const;\n\n");
 898   fprintf(fp_hpp, "  void add_usage(const Pipeline_Use &pred);\n\n");
 899   fprintf(fp_hpp, "  void reset() {\n");
 900   fprintf(fp_hpp, "    _resources_used = _resources_used_exclusively = 0;\n");
 901   fprintf(fp_hpp, "  };\n\n");
 902   fprintf(fp_hpp, "  void step(uint cycles) {\n");
 903   fprintf(fp_hpp, "    reset();\n");
 904   fprintf(fp_hpp, "    for (uint i = 0; i < %d; i++)\n",
 905     rescount);
 906   fprintf(fp_hpp, "      (&_elements[i])->step(cycles);\n");
 907   fprintf(fp_hpp, "  };\n\n");
 908   fprintf(fp_hpp, "  static const Pipeline_Use         elaborated_use;\n");
 909   fprintf(fp_hpp, "  static const Pipeline_Use_Element elaborated_elements[%d];\n\n",
 910     rescount);
 911   fprintf(fp_hpp, "  friend class Pipeline;\n");
 912   fprintf(fp_hpp, "};\n\n");
 913 
 914   fprintf(fp_hpp, "// Pipeline Class\n");
 915   fprintf(fp_hpp, "class Pipeline {\n");
 916   fprintf(fp_hpp, "public:\n");
 917 
 918   fprintf(fp_hpp, "  static bool enabled() { return %s; }\n\n",
 919     _pipeline ? "true" : "false" );
 920 
 921   assert( _pipeline->_maxInstrsPerBundle &&
 922         ( _pipeline->_instrUnitSize || _pipeline->_bundleUnitSize) &&
 923           _pipeline->_instrFetchUnitSize &&
 924           _pipeline->_instrFetchUnits,
 925     "unspecified pipeline architecture units");
 926 
 927   uint unitSize = _pipeline->_instrUnitSize ? _pipeline->_instrUnitSize : _pipeline->_bundleUnitSize;
 928 
 929   fprintf(fp_hpp, "  enum {\n");
 930   fprintf(fp_hpp, "    _variable_size_instructions = %d,\n",
 931     _pipeline->_variableSizeInstrs ? 1 : 0);
 932   fprintf(fp_hpp, "    _fixed_size_instructions = %d,\n",
 933     _pipeline->_variableSizeInstrs ? 0 : 1);
 934   fprintf(fp_hpp, "    _max_instrs_per_bundle = %d,\n",
 935     _pipeline->_maxInstrsPerBundle);
 936   fprintf(fp_hpp, "    _max_bundles_per_cycle = %d,\n",
 937     _pipeline->_maxBundlesPerCycle);
 938   fprintf(fp_hpp, "    _max_instrs_per_cycle = %d\n",
 939     _pipeline->_maxBundlesPerCycle * _pipeline->_maxInstrsPerBundle);
 940   fprintf(fp_hpp, "  };\n\n");
 941 
 942   fprintf(fp_hpp, "  static bool instr_has_unit_size() { return %s; }\n\n",
 943     _pipeline->_instrUnitSize != 0 ? "true" : "false" );
 944   if( _pipeline->_bundleUnitSize != 0 )
 945     if( _pipeline->_instrUnitSize != 0 )
 946       fprintf(fp_hpp, "// Individual Instructions may be bundled together by the hardware\n\n");
 947     else
 948       fprintf(fp_hpp, "// Instructions exist only in bundles\n\n");
 949   else
 950     fprintf(fp_hpp, "// Bundling is not supported\n\n");
 951   if( _pipeline->_instrUnitSize != 0 )
 952     fprintf(fp_hpp, "  // Size of an instruction\n");
 953   else
 954     fprintf(fp_hpp, "  // Size of an individual instruction does not exist - unsupported\n");
 955   fprintf(fp_hpp, "  static uint instr_unit_size() {");
 956   if( _pipeline->_instrUnitSize == 0 )
 957     fprintf(fp_hpp, " assert( false, \"Instructions are only in bundles\" );");
 958   fprintf(fp_hpp, " return %d; };\n\n", _pipeline->_instrUnitSize);
 959 
 960   if( _pipeline->_bundleUnitSize != 0 )
 961     fprintf(fp_hpp, "  // Size of a bundle\n");
 962   else
 963     fprintf(fp_hpp, "  // Bundles do not exist - unsupported\n");
 964   fprintf(fp_hpp, "  static uint bundle_unit_size() {");
 965   if( _pipeline->_bundleUnitSize == 0 )
 966     fprintf(fp_hpp, " assert( false, \"Bundles are not supported\" );");
 967   fprintf(fp_hpp, " return %d; };\n\n", _pipeline->_bundleUnitSize);
 968 
 969   fprintf(fp_hpp, "  static bool requires_bundling() { return %s; }\n\n",
 970     _pipeline->_bundleUnitSize != 0 && _pipeline->_instrUnitSize == 0 ? "true" : "false" );
 971 
 972   fprintf(fp_hpp, "private:\n");
 973   fprintf(fp_hpp, "  Pipeline();  // Not a legal constructor\n");
 974   fprintf(fp_hpp, "\n");
 975   fprintf(fp_hpp, "  const unsigned char                   _read_stage_count;\n");
 976   fprintf(fp_hpp, "  const unsigned char                   _write_stage;\n");
 977   fprintf(fp_hpp, "  const unsigned char                   _fixed_latency;\n");
 978   fprintf(fp_hpp, "  const unsigned char                   _instruction_count;\n");
 979   fprintf(fp_hpp, "  const bool                            _has_fixed_latency;\n");
 980   fprintf(fp_hpp, "  const bool                            _has_multiple_bundles;\n");
 981   fprintf(fp_hpp, "  const bool                            _force_serialization;\n");
 982   fprintf(fp_hpp, "  const bool                            _may_have_no_code;\n");
 983   fprintf(fp_hpp, "  const enum machPipelineStages * const _read_stages;\n");
 984   fprintf(fp_hpp, "  const enum machPipelineStages * const _resource_stage;\n");
 985   fprintf(fp_hpp, "  const uint                    * const _resource_cycles;\n");
 986   fprintf(fp_hpp, "  const Pipeline_Use                    _resource_use;\n");
 987   fprintf(fp_hpp, "\n");
 988   fprintf(fp_hpp, "public:\n");
 989   fprintf(fp_hpp, "  Pipeline(uint                            write_stage,\n");
 990   fprintf(fp_hpp, "           uint                            count,\n");
 991   fprintf(fp_hpp, "           bool                            has_fixed_latency,\n");
 992   fprintf(fp_hpp, "           uint                            fixed_latency,\n");
 993   fprintf(fp_hpp, "           uint                            instruction_count,\n");
 994   fprintf(fp_hpp, "           bool                            has_multiple_bundles,\n");
 995   fprintf(fp_hpp, "           bool                            force_serialization,\n");
 996   fprintf(fp_hpp, "           bool                            may_have_no_code,\n");
 997   fprintf(fp_hpp, "           enum machPipelineStages * const dst,\n");
 998   fprintf(fp_hpp, "           enum machPipelineStages * const stage,\n");
 999   fprintf(fp_hpp, "           uint                    * const cycles,\n");
1000   fprintf(fp_hpp, "           Pipeline_Use                    resource_use)\n");
1001   fprintf(fp_hpp, "  : _read_stage_count(count)\n");
1002   fprintf(fp_hpp, "  , _write_stage(write_stage)\n");
1003   fprintf(fp_hpp, "  , _fixed_latency(fixed_latency)\n");
1004   fprintf(fp_hpp, "  , _instruction_count(instruction_count)\n");
1005   fprintf(fp_hpp, "  , _has_fixed_latency(has_fixed_latency)\n");
1006   fprintf(fp_hpp, "  , _has_multiple_bundles(has_multiple_bundles)\n");
1007   fprintf(fp_hpp, "  , _force_serialization(force_serialization)\n");
1008   fprintf(fp_hpp, "  , _may_have_no_code(may_have_no_code)\n");
1009   fprintf(fp_hpp, "  , _read_stages(dst)\n");
1010   fprintf(fp_hpp, "  , _resource_stage(stage)\n");
1011   fprintf(fp_hpp, "  , _resource_cycles(cycles)\n");
1012   fprintf(fp_hpp, "  , _resource_use(resource_use)\n");
1013   fprintf(fp_hpp, "  {};\n");
1014   fprintf(fp_hpp, "\n");
1015   fprintf(fp_hpp, "  uint writeStage() const {\n");
1016   fprintf(fp_hpp, "    return (_write_stage);\n");
1017   fprintf(fp_hpp, "  }\n");
1018   fprintf(fp_hpp, "\n");
1019   fprintf(fp_hpp, "  enum machPipelineStages readStage(int ndx) const {\n");
1020   fprintf(fp_hpp, "    return (ndx < _read_stage_count ? _read_stages[ndx] : stage_undefined);");
1021   fprintf(fp_hpp, "  }\n\n");
1022   fprintf(fp_hpp, "  uint resourcesUsed() const {\n");
1023   fprintf(fp_hpp, "    return _resource_use.resourcesUsed();\n  }\n\n");
1024   fprintf(fp_hpp, "  uint resourcesUsedExclusively() const {\n");
1025   fprintf(fp_hpp, "    return _resource_use.resourcesUsedExclusively();\n  }\n\n");
1026   fprintf(fp_hpp, "  bool hasFixedLatency() const {\n");
1027   fprintf(fp_hpp, "    return (_has_fixed_latency);\n  }\n\n");
1028   fprintf(fp_hpp, "  uint fixedLatency() const {\n");
1029   fprintf(fp_hpp, "    return (_fixed_latency);\n  }\n\n");
1030   fprintf(fp_hpp, "  uint functional_unit_latency(uint start, const Pipeline *pred) const;\n\n");
1031   fprintf(fp_hpp, "  uint operand_latency(uint opnd, const Pipeline *pred) const;\n\n");
1032   fprintf(fp_hpp, "  const Pipeline_Use& resourceUse() const {\n");
1033   fprintf(fp_hpp, "    return (_resource_use); }\n\n");
1034   fprintf(fp_hpp, "  const Pipeline_Use_Element * resourceUseElement(uint i) const {\n");
1035   fprintf(fp_hpp, "    return (&_resource_use._elements[i]); }\n\n");
1036   fprintf(fp_hpp, "  uint resourceUseCount() const {\n");
1037   fprintf(fp_hpp, "    return (_resource_use._count); }\n\n");
1038   fprintf(fp_hpp, "  uint instructionCount() const {\n");
1039   fprintf(fp_hpp, "    return (_instruction_count); }\n\n");
1040   fprintf(fp_hpp, "  bool hasMultipleBundles() const {\n");
1041   fprintf(fp_hpp, "    return (_has_multiple_bundles); }\n\n");
1042   fprintf(fp_hpp, "  bool forceSerialization() const {\n");
1043   fprintf(fp_hpp, "    return (_force_serialization); }\n\n");
1044   fprintf(fp_hpp, "  bool mayHaveNoCode() const {\n");
1045   fprintf(fp_hpp, "    return (_may_have_no_code); }\n\n");
1046   fprintf(fp_hpp, "//const Pipeline_Use_Cycle_Mask& resourceUseMask(int resource) const {\n");
1047   fprintf(fp_hpp, "//  return (_resource_use_masks[resource]); }\n\n");
1048   fprintf(fp_hpp, "\n#ifndef PRODUCT\n");
1049   fprintf(fp_hpp, "  static const char * stageName(uint i);\n");
1050   fprintf(fp_hpp, "#endif\n");
1051   fprintf(fp_hpp, "};\n\n");
1052 
1053   fprintf(fp_hpp, "// Bundle class\n");
1054   fprintf(fp_hpp, "class Bundle {\n");
1055 
1056   uint mshift = 0;
1057   for (uint msize = _pipeline->_maxInstrsPerBundle * _pipeline->_maxBundlesPerCycle; msize != 0; msize >>= 1)
1058     mshift++;
1059 
1060   uint rshift = rescount;
1061 
1062   fprintf(fp_hpp, "protected:\n");
1063   fprintf(fp_hpp, "  uint _starts_bundle  : 1,\n");
1064   fprintf(fp_hpp, "       _instr_count    : %d,\n",   mshift);
1065   fprintf(fp_hpp, "       _resources_used : %d;\n",   rshift);
1066   fprintf(fp_hpp, "public:\n");
1067   fprintf(fp_hpp, "  Bundle() : _starts_bundle(0), _instr_count(0), _resources_used(0) {}\n\n");
1068   fprintf(fp_hpp, "  void set_instr_count(uint i) { _instr_count  = i; }\n");
1069   fprintf(fp_hpp, "  void set_resources_used(uint i) { _resources_used   = i; }\n");
1070   fprintf(fp_hpp, "  void set_starts_bundle() { _starts_bundle = true; }\n");
1071 
1072   fprintf(fp_hpp, "  uint instr_count() const { return (_instr_count); }\n");
1073   fprintf(fp_hpp, "  uint resources_used() const { return (_resources_used); }\n");
1074   fprintf(fp_hpp, "  bool starts_bundle() const { return (_starts_bundle != 0); }\n");
1075 
1076   fprintf(fp_hpp, "#ifndef PRODUCT\n");
1077   fprintf(fp_hpp, "  void dump(outputStream *st = tty) const;\n");
1078   fprintf(fp_hpp, "#endif\n");
1079   fprintf(fp_hpp, "};\n\n");
1080 
1081 //  const char *classname;
1082 //  for (_pipeline->_classlist.reset(); (classname = _pipeline->_classlist.iter()) != nullptr; ) {
1083 //    PipeClassForm *pipeclass = _pipeline->_classdict[classname]->is_pipeclass();
1084 //    fprintf(fp_hpp, "// Pipeline Class Instance for \"%s\"\n", classname);
1085 //  }
1086 }
1087 
1088 //------------------------------declareClasses---------------------------------
1089 // Construct the class hierarchy of MachNode classes from the instruction &
1090 // operand lists
1091 void ArchDesc::declareClasses(FILE *fp) {
1092 
1093   // Declare an array containing the machine register names, strings.
1094   declareRegNames(fp, _register);
1095 
1096   // Declare an array containing the machine register encoding values
1097   declareRegEncodes(fp, _register);
1098 
1099   // Generate declarations for the total number of operands
1100   fprintf(fp,"\n");
1101   fprintf(fp,"// Total number of operands defined in architecture definition\n");
1102   int num_operands = 0;
1103   OperandForm *op;
1104   for (_operands.reset(); (op = (OperandForm*)_operands.iter()) != nullptr; ) {
1105     // Ensure this is a machine-world instruction
1106     if (op->ideal_only()) continue;
1107 
1108     ++num_operands;
1109   }
1110   int first_operand_class = num_operands;
1111   OpClassForm *opc;
1112   for (_opclass.reset(); (opc = (OpClassForm*)_opclass.iter()) != nullptr; ) {
1113     // Ensure this is a machine-world instruction
1114     if (opc->ideal_only()) continue;
1115 
1116     ++num_operands;
1117   }
1118   fprintf(fp,"#define FIRST_OPERAND_CLASS   %d\n", first_operand_class);
1119   fprintf(fp,"#define NUM_OPERANDS          %d\n", num_operands);
1120   fprintf(fp,"\n");
1121   // Generate declarations for the total number of instructions
1122   fprintf(fp,"// Total number of instructions defined in architecture definition\n");
1123   fprintf(fp,"#define NUM_INSTRUCTIONS   %d\n",instructFormCount());
1124 
1125 
1126   // Generate Machine Classes for each operand defined in AD file
1127   fprintf(fp,"\n");
1128   fprintf(fp,"//----------------------------Declare classes derived from MachOper----------\n");
1129   // Iterate through all operands
1130   _operands.reset();
1131   OperandForm *oper;
1132   for( ; (oper = (OperandForm*)_operands.iter()) != nullptr;) {
1133     // Ensure this is a machine-world instruction
1134     if (oper->ideal_only() ) continue;
1135     // The declaration of labelOper is in machine-independent file: machnode
1136     if ( strcmp(oper->_ident,"label")  == 0 ) continue;
1137     // The declaration of methodOper is in machine-independent file: machnode
1138     if ( strcmp(oper->_ident,"method") == 0 ) continue;
1139 
1140     // Build class definition for this operand
1141     fprintf(fp,"\n");
1142     fprintf(fp,"class %sOper : public MachOper { \n",oper->_ident);
1143     fprintf(fp,"private:\n");
1144     // Operand definitions that depend upon number of input edges
1145     {
1146       uint num_edges = oper->num_edges(_globalNames);
1147       if( num_edges != 1 ) { // Use MachOper::num_edges() {return 1;}
1148         fprintf(fp,"  virtual uint           num_edges() const { return %d; }\n",
1149               num_edges );
1150       }
1151       if( num_edges > 0 ) {
1152         in_RegMask(fp);
1153       }
1154     }
1155 
1156     // Support storing constants inside the MachOper
1157     declareConstStorage(fp,_globalNames,oper);
1158 
1159     // Support storage of the condition codes
1160     if( oper->is_ideal_bool() ) {
1161       fprintf(fp,"  virtual int ccode() const { \n");
1162       fprintf(fp,"    switch (_c0) {\n");
1163       fprintf(fp,"    case  BoolTest::eq : return equal();\n");
1164       fprintf(fp,"    case  BoolTest::gt : return greater();\n");
1165       fprintf(fp,"    case  BoolTest::lt : return less();\n");
1166       fprintf(fp,"    case  BoolTest::ne : return not_equal();\n");
1167       fprintf(fp,"    case  BoolTest::le : return less_equal();\n");
1168       fprintf(fp,"    case  BoolTest::ge : return greater_equal();\n");
1169       fprintf(fp,"    case  BoolTest::overflow : return overflow();\n");
1170       fprintf(fp,"    case  BoolTest::no_overflow: return no_overflow();\n");
1171       fprintf(fp,"    default : ShouldNotReachHere(); return 0;\n");
1172       fprintf(fp,"    }\n");
1173       fprintf(fp,"  };\n");
1174     }
1175 
1176     // Support storage of the condition codes
1177     if( oper->is_ideal_bool() ) {
1178       fprintf(fp,"  virtual void negate() { \n");
1179       fprintf(fp,"    _c0 = (BoolTest::mask)((int)_c0^0x4); \n");
1180       fprintf(fp,"  };\n");
1181     }
1182 
1183     // Declare constructor.
1184     // Parameters start with condition code, then all other constants
1185     //
1186     // (1)  MachXOper(int32 ccode, int32 c0, int32 c1, ..., int32 cn)
1187     // (2)     : _ccode(ccode), _c0(c0), _c1(c1), ..., _cn(cn) { }
1188     //
1189     Form::DataType constant_type = oper->simple_type(_globalNames);
1190     defineConstructor(fp, oper->_ident, oper->num_consts(_globalNames),
1191                       oper->_components, oper->is_ideal_bool(),
1192                       constant_type, _globalNames);
1193 
1194     // Clone function
1195     fprintf(fp,"  virtual MachOper      *clone() const;\n");
1196 
1197     // Support setting a spill offset into a constant operand.
1198     // We only support setting an 'int' offset, while in the
1199     // LP64 build spill offsets are added with an AddP which
1200     // requires a long constant.  Thus we don't support spilling
1201     // in frames larger than 4Gig.
1202     if( oper->has_conI(_globalNames) ||
1203         oper->has_conL(_globalNames) )
1204       fprintf(fp, "  virtual void set_con( jint c0 ) { _c0 = c0; }\n");
1205 
1206     // virtual functions for encoding and format
1207     //    fprintf(fp,"  virtual void           encode()   const {\n    %s }\n",
1208     //            (oper->_encrule)?(oper->_encrule->_encrule):"");
1209     // Check the interface type, and generate the correct query functions
1210     // encoding queries based upon MEMORY_INTER, REG_INTER, CONST_INTER.
1211 
1212     fprintf(fp,"  virtual uint           opcode() const { return %s; }\n",
1213             machOperEnum(oper->_ident));
1214 
1215     // virtual function to look up ideal return type of machine instruction
1216     //
1217     // (1)  virtual const Type    *type() const { return .....; }
1218     //
1219     if ((oper->_matrule) && (oper->_matrule->_lChild == nullptr) &&
1220         (oper->_matrule->_rChild == nullptr)) {
1221       unsigned int position = 0;
1222       const char  *opret, *opname, *optype;
1223       oper->_matrule->base_operand(position,_globalNames,opret,opname,optype);
1224       fprintf(fp,"  virtual const Type    *type() const {");
1225       const char *type = getIdealType(optype);
1226       if( type != nullptr ) {
1227         Form::DataType data_type = oper->is_base_constant(_globalNames);
1228         // Check if we are an ideal pointer type
1229         if( data_type == Form::idealP || data_type == Form::idealN || data_type == Form::idealNKlass ) {
1230           // Return the ideal type we already have: <TypePtr *>
1231           fprintf(fp," return _c0;");
1232         } else {
1233           // Return the appropriate bottom type
1234           fprintf(fp," return %s;", getIdealType(optype));
1235         }
1236       } else {
1237         fprintf(fp," ShouldNotCallThis(); return Type::BOTTOM;");
1238       }
1239       fprintf(fp," }\n");
1240     } else {
1241       // Check for user-defined stack slots, based upon sRegX
1242       Form::DataType data_type = oper->is_user_name_for_sReg();
1243       if( data_type != Form::none ){
1244         const char *type = nullptr;
1245         switch( data_type ) {
1246         case Form::idealI: type = "TypeInt::INT";   break;
1247         case Form::idealP: type = "TypePtr::BOTTOM";break;
1248         case Form::idealF: type = "Type::FLOAT";    break;
1249         case Form::idealD: type = "Type::DOUBLE";   break;
1250         case Form::idealL: type = "TypeLong::LONG"; break;
1251         case Form::idealH: type = "Type::HALF_FLOAT"; break;
1252         case Form::none: // fall through
1253         default:
1254           assert( false, "No support for this type of stackSlot");
1255         }
1256         fprintf(fp,"  virtual const Type    *type() const { return %s; } // stackSlotX\n", type);
1257       }
1258     }
1259 
1260 
1261     //
1262     // virtual functions for defining the encoding interface.
1263     //
1264     // Access the linearized ideal register mask,
1265     // map to physical register encoding
1266     if ( oper->_matrule && oper->_matrule->is_base_register(_globalNames) ) {
1267       // Just use the default virtual 'reg' call
1268     } else if ( oper->ideal_to_sReg_type(oper->_ident) != Form::none ) {
1269       // Special handling for operand 'sReg', a Stack Slot Register.
1270       // Map linearized ideal register mask to stack slot number
1271       fprintf(fp,"  virtual int            reg(PhaseRegAlloc *ra_, const Node *node) const {\n");
1272       fprintf(fp,"    return (int)OptoReg::reg2stack(ra_->get_reg_first(node));/* sReg */\n");
1273       fprintf(fp,"  }\n");
1274       fprintf(fp,"  virtual int            reg(PhaseRegAlloc *ra_, const Node *node, int idx) const {\n");
1275       fprintf(fp,"    return (int)OptoReg::reg2stack(ra_->get_reg_first(node->in(idx)));/* sReg */\n");
1276       fprintf(fp,"  }\n");
1277     }
1278 
1279     // Output the operand specific access functions used by an enc_class
1280     // These are only defined when we want to override the default virtual func
1281     if (oper->_interface != nullptr) {
1282       fprintf(fp,"\n");
1283       // Check if it is a Memory Interface
1284       if ( oper->_interface->is_MemInterface() != nullptr ) {
1285         MemInterface *mem_interface = oper->_interface->is_MemInterface();
1286         const char *base = mem_interface->_base;
1287         if( base != nullptr ) {
1288           define_oper_interface(fp, *oper, _globalNames, "base", base);
1289         }
1290         char *index = mem_interface->_index;
1291         if( index != nullptr ) {
1292           define_oper_interface(fp, *oper, _globalNames, "index", index);
1293         }
1294         const char *scale = mem_interface->_scale;
1295         if( scale != nullptr ) {
1296           define_oper_interface(fp, *oper, _globalNames, "scale", scale);
1297         }
1298         const char *disp = mem_interface->_disp;
1299         if( disp != nullptr ) {
1300           define_oper_interface(fp, *oper, _globalNames, "disp", disp);
1301           oper->disp_is_oop(fp, _globalNames);
1302         }
1303         if( oper->stack_slots_only(_globalNames) ) {
1304           // should not call this:
1305           fprintf(fp,"  virtual int       constant_disp() const { return Type::OffsetBot; }");
1306         } else if ( disp != nullptr ) {
1307           define_oper_interface(fp, *oper, _globalNames, "constant_disp", disp);
1308         }
1309       } // end Memory Interface
1310       // Check if it is a Conditional Interface
1311       else if (oper->_interface->is_CondInterface() != nullptr) {
1312         CondInterface *cInterface = oper->_interface->is_CondInterface();
1313         const char *equal = cInterface->_equal;
1314         if( equal != nullptr ) {
1315           define_oper_interface(fp, *oper, _globalNames, "equal", equal);
1316         }
1317         const char *not_equal = cInterface->_not_equal;
1318         if( not_equal != nullptr ) {
1319           define_oper_interface(fp, *oper, _globalNames, "not_equal", not_equal);
1320         }
1321         const char *less = cInterface->_less;
1322         if( less != nullptr ) {
1323           define_oper_interface(fp, *oper, _globalNames, "less", less);
1324         }
1325         const char *greater_equal = cInterface->_greater_equal;
1326         if( greater_equal != nullptr ) {
1327           define_oper_interface(fp, *oper, _globalNames, "greater_equal", greater_equal);
1328         }
1329         const char *less_equal = cInterface->_less_equal;
1330         if( less_equal != nullptr ) {
1331           define_oper_interface(fp, *oper, _globalNames, "less_equal", less_equal);
1332         }
1333         const char *greater = cInterface->_greater;
1334         if( greater != nullptr ) {
1335           define_oper_interface(fp, *oper, _globalNames, "greater", greater);
1336         }
1337         const char *overflow = cInterface->_overflow;
1338         if( overflow != nullptr ) {
1339           define_oper_interface(fp, *oper, _globalNames, "overflow", overflow);
1340         }
1341         const char *no_overflow = cInterface->_no_overflow;
1342         if( no_overflow != nullptr ) {
1343           define_oper_interface(fp, *oper, _globalNames, "no_overflow", no_overflow);
1344         }
1345       } // end Conditional Interface
1346       // Check if it is a Constant Interface
1347       else if (oper->_interface->is_ConstInterface() != nullptr ) {
1348         assert( oper->num_consts(_globalNames) == 1,
1349                 "Must have one constant when using CONST_INTER encoding");
1350         if (!strcmp(oper->ideal_type(_globalNames), "ConI")) {
1351           // Access the locally stored constant
1352           fprintf(fp,"  virtual intptr_t       constant() const {");
1353           fprintf(fp,   " return (intptr_t)_c0;");
1354           fprintf(fp,"  }\n");
1355         }
1356         else if (!strcmp(oper->ideal_type(_globalNames), "ConP")) {
1357           // Access the locally stored constant
1358           fprintf(fp,"  virtual intptr_t       constant() const {");
1359           fprintf(fp,   " return _c0->get_con();");
1360           fprintf(fp, " }\n");
1361           // Generate query to determine if this pointer is an oop
1362           fprintf(fp,"  virtual relocInfo::relocType           constant_reloc() const {");
1363           fprintf(fp,   " return _c0->reloc();");
1364           fprintf(fp, " }\n");
1365         }
1366         else if (!strcmp(oper->ideal_type(_globalNames), "ConN")) {
1367           // Access the locally stored constant
1368           fprintf(fp,"  virtual intptr_t       constant() const {");
1369           fprintf(fp,   " return _c0->get_ptrtype()->get_con();");
1370           fprintf(fp, " }\n");
1371           // Generate query to determine if this pointer is an oop
1372           fprintf(fp,"  virtual relocInfo::relocType           constant_reloc() const {");
1373           fprintf(fp,   " return _c0->get_ptrtype()->reloc();");
1374           fprintf(fp, " }\n");
1375         }
1376         else if (!strcmp(oper->ideal_type(_globalNames), "ConNKlass")) {
1377           // Access the locally stored constant
1378           fprintf(fp,"  virtual intptr_t       constant() const {");
1379           fprintf(fp,   " return _c0->get_ptrtype()->get_con();");
1380           fprintf(fp, " }\n");
1381           // Generate query to determine if this pointer is an oop
1382           fprintf(fp,"  virtual relocInfo::relocType           constant_reloc() const {");
1383           fprintf(fp,   " return _c0->get_ptrtype()->reloc();");
1384           fprintf(fp, " }\n");
1385         }
1386         else if (!strcmp(oper->ideal_type(_globalNames), "ConL")) {
1387           fprintf(fp,"  virtual intptr_t       constant() const {");
1388           // We don't support addressing modes with > 4Gig offsets.
1389           // Truncate to int.
1390           fprintf(fp,   "  return (intptr_t)_c0;");
1391           fprintf(fp, " }\n");
1392           fprintf(fp,"  virtual jlong          constantL() const {");
1393           fprintf(fp,   " return _c0;");
1394           fprintf(fp, " }\n");
1395         }
1396         else if (!strcmp(oper->ideal_type(_globalNames), "ConH")) {
1397           fprintf(fp,"  virtual intptr_t       constant() const {");
1398           fprintf(fp,   " ShouldNotReachHere(); return 0; ");
1399           fprintf(fp, " }\n");
1400           fprintf(fp,"  virtual jshort         constantH() const {");
1401           fprintf(fp,   " return (jshort)_c0;");
1402           fprintf(fp, " }\n");
1403         }
1404         else if (!strcmp(oper->ideal_type(_globalNames), "ConF")) {
1405           fprintf(fp,"  virtual intptr_t       constant() const {");
1406           fprintf(fp,   " ShouldNotReachHere(); return 0; ");
1407           fprintf(fp, " }\n");
1408           fprintf(fp,"  virtual jfloat         constantF() const {");
1409           fprintf(fp,   " return (jfloat)_c0;");
1410           fprintf(fp, " }\n");
1411         }
1412         else if (!strcmp(oper->ideal_type(_globalNames), "ConD")) {
1413           fprintf(fp,"  virtual intptr_t       constant() const {");
1414           fprintf(fp,   " ShouldNotReachHere(); return 0; ");
1415           fprintf(fp, " }\n");
1416           fprintf(fp,"  virtual jdouble        constantD() const {");
1417           fprintf(fp,   " return _c0;");
1418           fprintf(fp, " }\n");
1419         }
1420       }
1421       else if (oper->_interface->is_RegInterface() != nullptr) {
1422         // make sure that a fixed format string isn't used for an
1423         // operand which might be assigned to multiple registers.
1424         // Otherwise the opto assembly output could be misleading.
1425         if (oper->_format->_strings.count() != 0 && !oper->is_bound_register()) {
1426           syntax_err(oper->_linenum,
1427                      "Only bound registers can have fixed formats: %s\n",
1428                      oper->_ident);
1429         }
1430       }
1431       else {
1432         assert( false, "ShouldNotReachHere();");
1433       }
1434     }
1435 
1436     fprintf(fp,"\n");
1437     // // Currently all XXXOper::hash() methods are identical (990820)
1438     // declare_hash(fp);
1439     // // Currently all XXXOper::Cmp() methods are identical (990820)
1440     // declare_cmp(fp);
1441 
1442     // Do not place dump_spec() and Name() into PRODUCT code
1443     // int_format and ext_format are not needed in PRODUCT code either
1444     fprintf(fp, "#ifndef PRODUCT\n");
1445 
1446     // Declare int_format() and ext_format()
1447     gen_oper_format(fp, _globalNames, *oper);
1448 
1449     // Machine independent print functionality for debugging
1450     // IF we have constants, create a dump_spec function for the derived class
1451     //
1452     // (1)  virtual void           dump_spec() const {
1453     // (2)    st->print("#%d", _c#);        // Constant != ConP
1454     //  OR    _c#->dump_on(st);             // Type ConP
1455     //  ...
1456     // (3)  }
1457     uint num_consts = oper->num_consts(_globalNames);
1458     if( num_consts > 0 ) {
1459       // line (1)
1460       fprintf(fp, "  virtual void           dump_spec(outputStream *st) const {\n");
1461       // generate format string for st->print
1462       // Iterate over the component list & spit out the right thing
1463       uint i = 0;
1464       const char *type = oper->ideal_type(_globalNames);
1465       Component  *comp;
1466       oper->_components.reset();
1467       if ((comp = oper->_components.iter()) == nullptr) {
1468         assert(num_consts == 1, "Bad component list detected.\n");
1469         i = dump_spec_constant( fp, type, i, oper );
1470         // Check that type actually matched
1471         assert( i != 0, "Non-constant operand lacks component list.");
1472       } // end if null
1473       else {
1474         // line (2)
1475         // dump all components
1476         oper->_components.reset();
1477         while((comp = oper->_components.iter()) != nullptr) {
1478           type = comp->base_type(_globalNames);
1479           i = dump_spec_constant( fp, type, i, nullptr );
1480         }
1481       }
1482       // finish line (3)
1483       fprintf(fp,"  }\n");
1484     }
1485 
1486     fprintf(fp,"  virtual const char    *Name() const { return \"%s\";}\n",
1487             oper->_ident);
1488 
1489     fprintf(fp,"#endif\n");
1490 
1491     // Close definition of this XxxMachOper
1492     fprintf(fp,"};\n");
1493   }
1494 
1495 
1496   // Generate Machine Classes for each instruction defined in AD file
1497   fprintf(fp,"\n");
1498   fprintf(fp,"//----------------------------Declare classes for Pipelines-----------------\n");
1499   declare_pipe_classes(fp);
1500 
1501   // Generate Machine Classes for each instruction defined in AD file
1502   fprintf(fp,"\n");
1503   fprintf(fp,"//----------------------------Declare classes derived from MachNode----------\n");
1504   _instructions.reset();
1505   InstructForm *instr;
1506   for( ; (instr = (InstructForm*)_instructions.iter()) != nullptr; ) {
1507     // Ensure this is a machine-world instruction
1508     if ( instr->ideal_only() ) continue;
1509 
1510     // Build class definition for this instruction
1511     fprintf(fp,"\n");
1512     fprintf(fp,"class %sNode : public %s { \n",
1513             instr->_ident, instr->mach_base_class(_globalNames) );
1514     fprintf(fp,"private:\n");
1515     fprintf(fp,"  MachOper *_opnd_array[%d];\n", instr->num_opnds() );
1516     if ( instr->is_ideal_jump() ) {
1517       fprintf(fp, "  GrowableArray<Label*> _index2label;\n");
1518     }
1519 
1520     fprintf(fp, "public:\n");
1521 
1522     Attribute *att = instr->_attribs;
1523     // Fields of the node specified in the ad file.
1524     while (att != nullptr) {
1525       if (strncmp(att->_ident, "ins_field_", 10) == 0) {
1526         const char *field_name = att->_ident+10;
1527         const char *field_type = att->_val;
1528         fprintf(fp, "  %s _%s;\n", field_type, field_name);
1529       }
1530       att = (Attribute *)att->_next;
1531     }
1532 
1533     fprintf(fp,"  MachOper *opnd_array(uint operand_index) const {\n");
1534     fprintf(fp,"    assert(operand_index < _num_opnds, \"invalid _opnd_array index\");\n");
1535     fprintf(fp,"    return _opnd_array[operand_index];\n");
1536     fprintf(fp,"  }\n");
1537     fprintf(fp,"  void      set_opnd_array(uint operand_index, MachOper *operand) {\n");
1538     fprintf(fp,"    assert(operand_index < _num_opnds, \"invalid _opnd_array index\");\n");
1539     fprintf(fp,"    _opnd_array[operand_index] = operand;\n");
1540     fprintf(fp,"  }\n");
1541     fprintf(fp,"  virtual uint           rule() const { return %s_rule; }\n",
1542             instr->_ident);
1543     fprintf(fp,"private:\n");
1544     if ( instr->is_ideal_jump() ) {
1545       fprintf(fp,"  virtual void           add_case_label(int index_num, Label* blockLabel) {\n");
1546       fprintf(fp,"    _index2label.at_put_grow(index_num, blockLabel);\n");
1547       fprintf(fp,"  }\n");
1548     }
1549     if( can_cisc_spill() && (instr->cisc_spill_alternate() != nullptr) ) {
1550       fprintf(fp,"  const RegMask  *_cisc_RegMask;\n");
1551     }
1552 
1553     out_RegMask(fp);                      // output register mask
1554 
1555     // If this instruction contains a labelOper
1556     // Declare Node::methods that set operand Label's contents
1557     int label_position = instr->label_position();
1558     if( label_position != -1 ) {
1559       // Set/Save the label, stored in labelOper::_branch_label
1560       fprintf(fp,"  virtual void           label_set( Label* label, uint block_num );\n");
1561       fprintf(fp,"  virtual void           save_label( Label** label, uint* block_num );\n");
1562     }
1563 
1564     // If this instruction contains a methodOper
1565     // Declare Node::methods that set operand method's contents
1566     int method_position = instr->method_position();
1567     if( method_position != -1 ) {
1568       // Set the address method, stored in methodOper::_method
1569       fprintf(fp,"  virtual void           method_set( intptr_t method );\n");
1570     }
1571 
1572     // virtual functions for attributes
1573     //
1574     // Each instruction attribute results in a virtual call of same name.
1575     // The ins_cost is not handled here.
1576     Attribute *attr = instr->_attribs;
1577     Attribute *avoid_back_to_back_attr = nullptr;
1578     while (attr != nullptr) {
1579       if (strcmp (attr->_ident, "ins_is_TrapBasedCheckNode") == 0) {
1580         fprintf(fp, "  virtual bool           is_TrapBasedCheckNode() const { return %s; }\n", attr->_val);
1581       } else if (strcmp (attr->_ident, "ins_is_late_expanded_null_check_candidate") == 0) {
1582         fprintf(fp, "  virtual bool           is_late_expanded_null_check_candidate() const { return %s; }\n", attr->_val);
1583       } else if (strcmp (attr->_ident, "ins_cost") != 0 &&
1584           strncmp(attr->_ident, "ins_field_", 10) != 0 &&
1585           // Must match function in node.hpp: return type bool, no prefix "ins_".
1586           strcmp (attr->_ident, "ins_is_TrapBasedCheckNode") != 0 &&
1587           strcmp (attr->_ident, "ins_short_branch") != 0) {
1588         fprintf(fp, "  virtual int            %s() const { return %s; }\n", attr->_ident, attr->_val);
1589       }
1590       if (strcmp(attr->_ident, "ins_avoid_back_to_back") == 0) {
1591         avoid_back_to_back_attr = attr;
1592       }
1593       attr = (Attribute *)attr->_next;
1594     }
1595 
1596     // virtual functions for encode and format
1597 
1598     // Virtual function for evaluating the constant.
1599     if (instr->is_mach_constant()) {
1600       fprintf(fp,"  virtual void           eval_constant(Compile* C);\n");
1601     }
1602 
1603     // Output the opcode function and the encode function here using the
1604     // encoding class information in the _insencode slot.
1605     if ( instr->_insencode ) {
1606       if (instr->postalloc_expands()) {
1607         fprintf(fp,"  virtual bool           requires_postalloc_expand() const { return true; }\n");
1608         fprintf(fp,"  virtual void           postalloc_expand(GrowableArray <Node *> *nodes, PhaseRegAlloc *ra_);\n");
1609       } else {
1610         fprintf(fp,"  virtual void           emit(C2_MacroAssembler *masm, PhaseRegAlloc *ra_) const;\n");
1611       }
1612     }
1613 
1614     // virtual function for getting the size of an instruction
1615     if ( instr->_size ) {
1616       fprintf(fp,"  virtual uint           size(PhaseRegAlloc *ra_) const;\n");
1617     }
1618 
1619     // Return the top-level ideal opcode.
1620     // Use MachNode::ideal_Opcode() for nodes based on MachNode class
1621     // if the ideal_Opcode == Op_Node.
1622     if ( strcmp("Node", instr->ideal_Opcode(_globalNames)) != 0 ||
1623          strcmp("MachNode", instr->mach_base_class(_globalNames)) != 0 ) {
1624       fprintf(fp,"  virtual int            ideal_Opcode() const { return Op_%s; }\n",
1625             instr->ideal_Opcode(_globalNames) );
1626     }
1627 
1628     if (instr->needs_constant_base() &&
1629         !instr->is_mach_constant()) {  // These inherit the function from MachConstantNode.
1630       fprintf(fp,"  virtual uint           mach_constant_base_node_input() const { ");
1631       if (instr->is_ideal_call() != Form::invalid_type &&
1632           instr->is_ideal_call() != Form::JAVA_LEAF) {
1633         // MachConstantBase goes behind arguments, but before jvms.
1634         fprintf(fp,"assert(tf() && tf()->domain(), \"\"); return tf()->domain()->cnt();");
1635       } else {
1636         fprintf(fp,"return req()-1;");
1637       }
1638       fprintf(fp," }\n");
1639     }
1640 
1641     // Allow machine-independent optimization, invert the sense of the IF test
1642     if( instr->is_ideal_if() ) {
1643       fprintf(fp,"  virtual void           negate() { \n");
1644       // Identify which operand contains the negate(able) ideal condition code
1645       int   idx = 0;
1646       instr->_components.reset();
1647       for( Component *comp; (comp = instr->_components.iter()) != nullptr; ) {
1648         // Check that component is an operand
1649         Form *form = (Form*)_globalNames[comp->_type];
1650         OperandForm *opForm = form ? form->is_operand() : nullptr;
1651         if( opForm == nullptr ) continue;
1652 
1653         // Lookup the position of the operand in the instruction.
1654         if( opForm->is_ideal_bool() ) {
1655           idx = instr->operand_position(comp->_name, comp->_usedef);
1656           assert( idx != NameList::Not_in_list, "Did not find component in list that contained it.");
1657           break;
1658         }
1659       }
1660       fprintf(fp,"    opnd_array(%d)->negate();\n", idx);
1661       fprintf(fp,"    _prob = 1.0f - _prob;\n");
1662       fprintf(fp,"  };\n");
1663     }
1664 
1665 
1666     // Identify which input register matches the input register.
1667     uint  matching_input = instr->two_address(_globalNames);
1668 
1669     // Generate the method if it returns != 0 otherwise use MachNode::two_adr()
1670     if( matching_input != 0 ) {
1671       fprintf(fp,"  virtual uint           two_adr() const  ");
1672       fprintf(fp,"{ return oper_input_base()");
1673       for( uint i = 2; i <= matching_input; i++ )
1674         fprintf(fp," + opnd_array(%d)->num_edges()",i-1);
1675       fprintf(fp,"; }\n");
1676     }
1677 
1678     // Declare cisc_version, if applicable
1679     //   MachNode *cisc_version( int offset /* ,... */ );
1680     instr->declare_cisc_version(*this, fp);
1681 
1682     // If there is an explicit peephole rule, build it
1683     if ( instr->peepholes() != nullptr ) {
1684       fprintf(fp,"  virtual int            peephole(Block* block, int block_index, PhaseCFG* cfg_, PhaseRegAlloc* ra_);\n");
1685     }
1686 
1687     // Output the declaration for number of relocation entries
1688     if ( instr->reloc(_globalNames) != 0 ) {
1689       fprintf(fp,"  virtual int            reloc() const;\n");
1690     }
1691 
1692     if (instr->alignment() != 1) {
1693       fprintf(fp,"  virtual int            alignment_required() const { return %d; }\n", instr->alignment());
1694       fprintf(fp,"  virtual int            compute_padding(int current_offset) const;\n");
1695     }
1696 
1697     // Starting point for inputs matcher wants.
1698     // Use MachNode::oper_input_base() for nodes based on MachNode class
1699     // if the base == 1.
1700     if ( instr->oper_input_base(_globalNames) != 1 ||
1701          strcmp("MachNode", instr->mach_base_class(_globalNames)) != 0 ) {
1702       fprintf(fp,"  virtual uint           oper_input_base() const { return %d; }\n",
1703             instr->oper_input_base(_globalNames));
1704     }
1705 
1706     // Make the constructor and following methods 'public:'
1707     fprintf(fp,"public:\n");
1708 
1709     // Constructor
1710     if ( instr->is_ideal_jump() ) {
1711       fprintf(fp,"  %sNode() : _index2label(MinJumpTableSize*2) { ", instr->_ident);
1712     } else {
1713       fprintf(fp,"  %sNode() { ", instr->_ident);
1714       if( can_cisc_spill() && (instr->cisc_spill_alternate() != nullptr) ) {
1715         fprintf(fp,"_cisc_RegMask = nullptr; ");
1716       }
1717     }
1718 
1719     fprintf(fp," _num_opnds = %d; _opnds = _opnd_array; ", instr->num_opnds());
1720 
1721     bool node_flags_set = false;
1722     // flag: if this instruction matches an ideal 'Copy*' node
1723     if ( instr->is_ideal_copy() != 0 ) {
1724       fprintf(fp,"init_flags(Flag_is_Copy");
1725       node_flags_set = true;
1726     }
1727 
1728     // Is an instruction is a constant?  If so, get its type
1729     Form::DataType  data_type;
1730     const char     *opType = nullptr;
1731     const char     *result = nullptr;
1732     data_type    = instr->is_chain_of_constant(_globalNames, opType, result);
1733     // Check if this instruction is a constant
1734     if ( data_type != Form::none ) {
1735       if ( node_flags_set ) {
1736         fprintf(fp," | Flag_is_Con");
1737       } else {
1738         fprintf(fp,"init_flags(Flag_is_Con");
1739         node_flags_set = true;
1740       }
1741     }
1742 
1743     // flag: if this instruction is cisc alternate
1744     if ( can_cisc_spill() && instr->is_cisc_alternate() ) {
1745       if ( node_flags_set ) {
1746         fprintf(fp," | Flag_is_cisc_alternate");
1747       } else {
1748         fprintf(fp,"init_flags(Flag_is_cisc_alternate");
1749         node_flags_set = true;
1750       }
1751     }
1752 
1753     // flag: if this instruction has short branch form
1754     if ( instr->has_short_branch_form() ) {
1755       if ( node_flags_set ) {
1756         fprintf(fp," | Flag_may_be_short_branch");
1757       } else {
1758         fprintf(fp,"init_flags(Flag_may_be_short_branch");
1759         node_flags_set = true;
1760       }
1761     }
1762 
1763     // flag: if this instruction should not be generated back to back.
1764     if (avoid_back_to_back_attr != nullptr) {
1765       if (node_flags_set) {
1766         fprintf(fp," | (%s)", avoid_back_to_back_attr->_val);
1767       } else {
1768         fprintf(fp,"init_flags((%s)", avoid_back_to_back_attr->_val);
1769         node_flags_set = true;
1770       }
1771     }
1772 
1773     // Check if machine instructions that USE memory, but do not DEF memory,
1774     // depend upon a node that defines memory in machine-independent graph.
1775     if ( instr->needs_anti_dependence_check(_globalNames) ) {
1776       if ( node_flags_set ) {
1777         fprintf(fp," | Flag_needs_anti_dependence_check");
1778       } else {
1779         fprintf(fp,"init_flags(Flag_needs_anti_dependence_check");
1780         node_flags_set = true;
1781       }
1782     }
1783 
1784     // flag: if this instruction is implemented with a call
1785     if ( instr->_has_call ) {
1786       if ( node_flags_set ) {
1787         fprintf(fp," | Flag_has_call");
1788       } else {
1789         fprintf(fp,"init_flags(Flag_has_call");
1790         node_flags_set = true;
1791       }
1792     }
1793 
1794     if ( node_flags_set ) {
1795       fprintf(fp,"); ");
1796     }
1797 
1798     fprintf(fp,"}\n");
1799 
1800     // size_of, used by base class's clone to obtain the correct size.
1801     fprintf(fp,"  virtual uint           size_of() const {");
1802     fprintf(fp,   " return sizeof(%sNode);", instr->_ident);
1803     fprintf(fp, " }\n");
1804 
1805     // Virtual methods which are only generated to override base class
1806     if( instr->expands() || instr->needs_projections() ||
1807         instr->has_temps() ||
1808         instr->is_mach_constant() ||
1809         instr->needs_constant_base() ||
1810         (instr->_matrule != nullptr &&
1811          instr->num_opnds() != instr->num_unique_opnds()) ) {
1812       fprintf(fp,"  virtual MachNode      *Expand(State *state, Node_List &proj_list, Node* mem);\n");
1813     }
1814 
1815     if (instr->is_pinned(_globalNames)) {
1816       fprintf(fp,"  virtual bool           pinned() const { return ");
1817       if (instr->is_parm(_globalNames)) {
1818         fprintf(fp,"_in[0]->pinned();");
1819       } else {
1820         fprintf(fp,"true;");
1821       }
1822       fprintf(fp," }\n");
1823     }
1824     if (instr->is_projection(_globalNames)) {
1825       fprintf(fp,"  virtual const Node *is_block_proj() const { return this; }\n");
1826     }
1827     if ( instr->num_post_match_opnds() != 0
1828          || instr->is_chain_of_constant(_globalNames) ) {
1829       fprintf(fp,"  friend MachNode *State::MachNodeGenerator(int opcode);\n");
1830     }
1831     if ( instr->rematerialize(_globalNames, get_registers()) ) {
1832       fprintf(fp,"  // Rematerialize %s\n", instr->_ident);
1833     }
1834 
1835     // Declare short branch methods, if applicable
1836     instr->declare_short_branch_methods(fp);
1837 
1838     // See if there is an "ins_pipe" declaration for this instruction
1839     if (instr->_ins_pipe) {
1840       fprintf(fp,"  static  const Pipeline *pipeline_class();\n");
1841       fprintf(fp,"  virtual const Pipeline *pipeline() const;\n");
1842     }
1843 
1844     // Generate virtual function for MachNodeX::bottom_type when necessary
1845     //
1846     // Note on accuracy:  Pointer-types of machine nodes need to be accurate,
1847     // or else alias analysis on the matched graph may produce bad code.
1848     // Moreover, the aliasing decisions made on machine-node graph must be
1849     // no less accurate than those made on the ideal graph, or else the graph
1850     // may fail to schedule.  (Reason:  Memory ops which are reordered in
1851     // the ideal graph might look interdependent in the machine graph,
1852     // thereby removing degrees of scheduling freedom that the optimizer
1853     // assumed would be available.)
1854     //
1855     // %%% We should handle many of these cases with an explicit ADL clause:
1856     // instruct foo() %{ ... bottom_type(TypeRawPtr::BOTTOM); ... %}
1857     if( data_type != Form::none ) {
1858       // A constant's bottom_type returns a Type containing its constant value
1859 
1860       // !!!!!
1861       // Convert all ints, floats, ... to machine-independent TypeXs
1862       // as is done for pointers
1863       //
1864       // Construct appropriate constant type containing the constant value.
1865       fprintf(fp,"  virtual const class Type *bottom_type() const {\n");
1866       switch( data_type ) {
1867       case Form::idealI:
1868         fprintf(fp,"    return  TypeInt::make(opnd_array(1)->constant());\n");
1869         break;
1870       case Form::idealP:
1871       case Form::idealN:
1872       case Form::idealNKlass:
1873         fprintf(fp,"    return  opnd_array(1)->type();\n");
1874         break;
1875       case Form::idealD:
1876         fprintf(fp,"    return  TypeD::make(opnd_array(1)->constantD());\n");
1877         break;
1878       case Form::idealH:
1879         fprintf(fp,"    return  TypeH::make(opnd_array(1)->constantH());\n");
1880         break;
1881       case Form::idealF:
1882         fprintf(fp,"    return  TypeF::make(opnd_array(1)->constantF());\n");
1883         break;
1884       case Form::idealL:
1885         fprintf(fp,"    return  TypeLong::make(opnd_array(1)->constantL());\n");
1886         break;
1887       default:
1888         assert( false, "Unimplemented()" );
1889         break;
1890       }
1891       fprintf(fp,"  };\n");
1892     }
1893 /*    else if ( instr->_matrule && instr->_matrule->_rChild &&
1894         (  strcmp("ConvF2I",instr->_matrule->_rChild->_opType)==0
1895         || strcmp("ConvD2I",instr->_matrule->_rChild->_opType)==0 ) ) {
1896       // !!!!! !!!!!
1897       // Provide explicit bottom type for conversions to int
1898       // On Intel the result operand is a stackSlot, untyped.
1899       fprintf(fp,"  virtual const class Type *bottom_type() const {");
1900       fprintf(fp,   " return  TypeInt::INT;");
1901       fprintf(fp, " };\n");
1902     }*/
1903     else if( instr->is_ideal_copy() &&
1904               !strcmp(instr->_matrule->_lChild->_opType,"stackSlotP") ) {
1905       // !!!!!
1906       // Special hack for ideal Copy of pointer.  Bottom type is oop or not depending on input.
1907       fprintf(fp,"  const Type            *bottom_type() const { return in(1)->bottom_type(); } // Copy?\n");
1908     }
1909     else if( instr->is_ideal_loadPC() ) {
1910       // LoadPCNode provides the return address of a call to native code.
1911       // Define its bottom type to be TypeRawPtr::BOTTOM instead of TypePtr::BOTTOM
1912       // since it is a pointer to an internal VM location and must have a zero offset.
1913       // Allocation detects derived pointers, in part, by their non-zero offsets.
1914       fprintf(fp,"  const Type            *bottom_type() const { return TypeRawPtr::BOTTOM; } // LoadPC?\n");
1915     }
1916     else if( instr->is_ideal_box() ) {
1917       // BoxNode provides the address of a stack slot.
1918       // Define its bottom type to be TypeRawPtr::BOTTOM instead of TypePtr::BOTTOM
1919       // This prevents raise_above_anti_dependences from complaining. It will
1920       // complain if it sees that the pointer base is TypePtr::BOTTOM since
1921       // it doesn't understand what that might alias.
1922       fprintf(fp,"  const Type            *bottom_type() const { return TypeRawPtr::BOTTOM; } // Box?\n");
1923     }
1924     else if (instr->_matrule && instr->_matrule->_rChild &&
1925               (!strcmp(instr->_matrule->_rChild->_opType,"CMoveP") || !strcmp(instr->_matrule->_rChild->_opType,"CMoveN")) ) {
1926       int offset = 1;
1927       // Special special hack to see if the Cmp? has been incorporated in the conditional move
1928       MatchNode *rl = instr->_matrule->_rChild->_lChild;
1929       if (rl && !strcmp(rl->_opType, "Binary") && rl->_rChild && strncmp(rl->_rChild->_opType, "Cmp", 3) == 0) {
1930         offset = 2;
1931         fprintf(fp,"  const Type            *bottom_type() const { if (req() == 3) return in(2)->bottom_type();\n\tconst Type *t = in(oper_input_base()+%d)->bottom_type(); return (req() <= oper_input_base()+%d) ? t : t->meet(in(oper_input_base()+%d)->bottom_type()); } // %s\n",
1932         offset, offset+1, offset+1, instr->_matrule->_rChild->_opType);
1933       } else {
1934         // Special hack for ideal CMove; ideal type depends on inputs
1935         fprintf(fp,"  const Type            *bottom_type() const { const Type *t = in(oper_input_base()+%d)->bottom_type(); return (req() <= oper_input_base()+%d) ? t : t->meet(in(oper_input_base()+%d)->bottom_type()); } // %s\n",
1936         offset, offset+1, offset+1, instr->_matrule->_rChild->_opType);
1937       }
1938     }
1939     else if (instr->is_tls_instruction()) {
1940       // Special hack for tlsLoadP
1941       fprintf(fp,"  const Type            *bottom_type() const { return TypeRawPtr::BOTTOM; } // tlsLoadP\n");
1942     }
1943     else if ( instr->is_ideal_if() ) {
1944       fprintf(fp,"  const Type            *bottom_type() const { return TypeTuple::IFBOTH; } // matched IfNode\n");
1945     }
1946     else if ( instr->is_ideal_membar() ) {
1947       fprintf(fp,"  const Type            *bottom_type() const { return TypeTuple::MEMBAR; } // matched MemBar\n");
1948     }
1949 
1950     // Check where 'ideal_type' must be customized
1951     /*
1952     if ( instr->_matrule && instr->_matrule->_rChild &&
1953         (  strcmp("ConvF2I",instr->_matrule->_rChild->_opType)==0
1954         || strcmp("ConvD2I",instr->_matrule->_rChild->_opType)==0 ) ) {
1955       fprintf(fp,"  virtual uint           ideal_reg() const { return Compile::current()->matcher()->base2reg[Type::Int]; }\n");
1956     }*/
1957 
1958     // Analyze machine instructions that either USE or DEF memory.
1959     int memory_operand = instr->memory_operand(_globalNames);
1960     if ( memory_operand != InstructForm::NO_MEMORY_OPERAND ) {
1961       if( memory_operand == InstructForm::MANY_MEMORY_OPERANDS ) {
1962         fprintf(fp,"  virtual const TypePtr *adr_type() const;\n");
1963       }
1964       fprintf(fp,"  virtual const MachOper *memory_operand() const;\n");
1965     }
1966 
1967     fprintf(fp, "#ifndef PRODUCT\n");
1968 
1969     // virtual function for generating the user's assembler output
1970     gen_inst_format(fp, _globalNames,*instr);
1971 
1972     // Machine independent print functionality for debugging
1973     fprintf(fp,"  virtual const char    *Name() const { return \"%s\";}\n",
1974             instr->_ident);
1975 
1976     fprintf(fp, "#endif\n");
1977 
1978     // Close definition of this XxxMachNode
1979     fprintf(fp,"};\n");
1980   };
1981 
1982 }
1983 
1984 void ArchDesc::defineStateClass(FILE *fp) {
1985   static const char *state__valid    = "_rule[index] & 0x1";
1986 
1987   fprintf(fp,"\n");
1988   fprintf(fp,"// MACROS to inline and constant fold State::valid(index)...\n");
1989   fprintf(fp,"// when given a constant 'index' in dfa_<arch>.cpp\n");
1990   fprintf(fp,"#define STATE__NOT_YET_VALID(index) ");
1991   fprintf(fp,"  ( (%s) == 0 )\n", state__valid);
1992   fprintf(fp,"\n");
1993   fprintf(fp,"#define STATE__VALID_CHILD(state,index) ");
1994   fprintf(fp,"  ( state && (state->%s) )\n", state__valid);
1995   fprintf(fp,"\n");
1996   fprintf(fp,
1997           "//---------------------------State-------------------------------------------\n");
1998   fprintf(fp,"// State contains an integral cost vector, indexed by machine operand opcodes,\n");
1999   fprintf(fp,"// a rule vector consisting of machine operand/instruction opcodes, and also\n");
2000   fprintf(fp,"// indexed by machine operand opcodes, pointers to the children in the label\n");
2001   fprintf(fp,"// tree generated by the Label routines in ideal nodes (currently limited to\n");
2002   fprintf(fp,"// two for convenience, but this could change).\n");
2003   fprintf(fp,"class State : public ArenaObj {\n");
2004   fprintf(fp,"private:\n");
2005   fprintf(fp,"  unsigned int _cost[_LAST_MACH_OPER];  // Costs, indexed by operand opcodes\n");
2006   fprintf(fp,"  uint16_t     _rule[_LAST_MACH_OPER];  // Rule and validity, indexed by operand opcodes\n");
2007   fprintf(fp,"                                        // Lowest bit encodes validity\n");
2008 
2009   fprintf(fp,"public:\n");
2010   fprintf(fp,"  int    _id;                           // State identifier\n");
2011   fprintf(fp,"  Node  *_leaf;                         // Ideal (non-machine-node) leaf of match tree\n");
2012   fprintf(fp,"  State *_kids[2];                      // Children of state node in label tree\n");
2013   fprintf(fp,"\n");
2014   fprintf(fp,"  State(void);\n");
2015   fprintf(fp,"  DEBUG_ONLY( ~State(void); )\n");
2016   fprintf(fp,"\n");
2017   fprintf(fp,"  // Methods created by ADLC and invoked by Reduce\n");
2018   fprintf(fp,"  MachOper *MachOperGenerator(int opcode);\n");
2019   fprintf(fp,"  MachNode *MachNodeGenerator(int opcode);\n");
2020   fprintf(fp,"\n");
2021   fprintf(fp,"  // Assign a state to a node, definition of method produced by ADLC\n");
2022   fprintf(fp,"  bool DFA( int opcode, const Node *ideal );\n");
2023   fprintf(fp,"\n");
2024   fprintf(fp,"  bool valid(uint index) {\n");
2025   fprintf(fp,"    return %s;\n", state__valid);
2026   fprintf(fp,"  }\n");
2027   fprintf(fp,"  unsigned int rule(uint index) {\n");
2028   fprintf(fp,"    return _rule[index] >> 1;\n");
2029   fprintf(fp,"  }\n");
2030   fprintf(fp,"  unsigned int cost(uint index) {\n");
2031   fprintf(fp,"    return _cost[index];\n");
2032   fprintf(fp,"  }\n");
2033   fprintf(fp,"\n");
2034   fprintf(fp,"#ifndef PRODUCT\n");
2035   fprintf(fp,"  void dump();                // Debugging prints\n");
2036   fprintf(fp,"  void dump(int depth);\n");
2037   fprintf(fp,"#endif\n");
2038   if (_dfa_small) {
2039     // Generate the routine name we'll need
2040     for (int i = 1; i < _last_opcode; i++) {
2041       if (_mlistab[i] == nullptr) continue;
2042       fprintf(fp, "  void  _sub_Op_%s(const Node *n);\n", NodeClassNames[i]);
2043     }
2044   }
2045   fprintf(fp,"};\n");
2046   fprintf(fp,"\n");
2047   fprintf(fp,"\n");
2048 
2049 }
2050 
2051 
2052 //---------------------------buildMachOperEnum---------------------------------
2053 // Build enumeration for densely packed operands.
2054 // This enumeration is used to index into the arrays in the State objects
2055 // that indicate cost and a successful rule match.
2056 
2057 // Information needed to generate the ReduceOp mapping for the DFA
2058 class OutputMachOperands : public OutputMap {
2059 public:
2060   OutputMachOperands(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
2061     : OutputMap(hpp, cpp, globals, AD, "MachOperands") {};
2062 
2063   void declaration() { }
2064   void definition()  { fprintf(_cpp, "enum MachOperands {\n"); }
2065   void closing()     { fprintf(_cpp, "  _LAST_MACH_OPER\n");
2066                        OutputMap::closing();
2067   }
2068   void map(OpClassForm &opc)  {
2069     const char* opc_ident_to_upper = _AD.machOperEnum(opc._ident);
2070     fprintf(_cpp, "  %s", opc_ident_to_upper);
2071     delete[] opc_ident_to_upper;
2072   }
2073   void map(OperandForm &oper) {
2074     const char* oper_ident_to_upper = _AD.machOperEnum(oper._ident);
2075     fprintf(_cpp, "  %s", oper_ident_to_upper);
2076     delete[] oper_ident_to_upper;
2077   }
2078   void map(char *name) {
2079     const char* name_to_upper = _AD.machOperEnum(name);
2080     fprintf(_cpp, "  %s", name_to_upper);
2081     delete[] name_to_upper;
2082   }
2083 
2084   bool do_instructions()      { return false; }
2085   void map(InstructForm &inst){ assert( false, "ShouldNotCallThis()"); }
2086 };
2087 
2088 
2089 void ArchDesc::buildMachOperEnum(FILE *fp_hpp) {
2090   // Construct the table for MachOpcodes
2091   OutputMachOperands output_mach_operands(fp_hpp, fp_hpp, _globalNames, *this);
2092   build_map(output_mach_operands);
2093 }
2094 
2095 
2096 //---------------------------buildMachEnum----------------------------------
2097 // Build enumeration for all MachOpers and all MachNodes
2098 
2099 // Information needed to generate the ReduceOp mapping for the DFA
2100 class OutputMachOpcodes : public OutputMap {
2101   int begin_inst_chain_rule;
2102   int end_inst_chain_rule;
2103   int begin_rematerialize;
2104   int end_rematerialize;
2105   int end_instructions;
2106 public:
2107   OutputMachOpcodes(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
2108     : OutputMap(hpp, cpp, globals, AD, "MachOpcodes"),
2109       begin_inst_chain_rule(-1), end_inst_chain_rule(-1),
2110       begin_rematerialize(-1), end_rematerialize(-1),
2111       end_instructions(-1)
2112   {};
2113 
2114   void declaration() { }
2115   void definition()  { fprintf(_cpp, "enum MachOpcodes {\n"); }
2116   void closing()     {
2117     if( begin_inst_chain_rule != -1 )
2118       fprintf(_cpp, "  _BEGIN_INST_CHAIN_RULE = %d,\n", begin_inst_chain_rule);
2119     if( end_inst_chain_rule   != -1 )
2120       fprintf(_cpp, "  _END_INST_CHAIN_RULE  = %d,\n", end_inst_chain_rule);
2121     if( begin_rematerialize   != -1 )
2122       fprintf(_cpp, "  _BEGIN_REMATERIALIZE   = %d,\n", begin_rematerialize);
2123     if( end_rematerialize     != -1 )
2124       fprintf(_cpp, "  _END_REMATERIALIZE    = %d,\n", end_rematerialize);
2125     // always execute since do_instructions() is true, and avoids trailing comma
2126     fprintf(_cpp, "  _last_Mach_Node  = %d \n",  end_instructions);
2127     OutputMap::closing();
2128   }
2129   void map(OpClassForm &opc)  { fprintf(_cpp, "  %s_rule", opc._ident ); }
2130   void map(OperandForm &oper) { fprintf(_cpp, "  %s_rule", oper._ident ); }
2131   void map(char        *name) { if (name) fprintf(_cpp, "  %s_rule", name);
2132                                 else      fprintf(_cpp, "  0"); }
2133   void map(InstructForm &inst) {fprintf(_cpp, "  %s_rule", inst._ident ); }
2134 
2135   void record_position(OutputMap::position place, int idx ) {
2136     switch(place) {
2137     case OutputMap::BEGIN_INST_CHAIN_RULES :
2138       begin_inst_chain_rule = idx;
2139       break;
2140     case OutputMap::END_INST_CHAIN_RULES :
2141       end_inst_chain_rule   = idx;
2142       break;
2143     case OutputMap::BEGIN_REMATERIALIZE :
2144       begin_rematerialize   = idx;
2145       break;
2146     case OutputMap::END_REMATERIALIZE :
2147       end_rematerialize     = idx;
2148       break;
2149     case OutputMap::END_INSTRUCTIONS :
2150       end_instructions      = idx;
2151       break;
2152     default:
2153       break;
2154     }
2155   }
2156 };
2157 
2158 
2159 void ArchDesc::buildMachOpcodesEnum(FILE *fp_hpp) {
2160   // Construct the table for MachOpcodes
2161   OutputMachOpcodes output_mach_opcodes(fp_hpp, fp_hpp, _globalNames, *this);
2162   build_map(output_mach_opcodes);
2163 }
2164 
2165 
2166 // Generate an enumeration of the pipeline states, and both
2167 // the functional units (resources) and the masks for
2168 // specifying resources
2169 void ArchDesc::build_pipeline_enums(FILE *fp_hpp) {
2170   int stagelen = (int)strlen("undefined");
2171   int stagenum = 0;
2172 
2173   if (_pipeline) {              // Find max enum string length
2174     const char *stage;
2175     for ( _pipeline->_stages.reset(); (stage = _pipeline->_stages.iter()) != nullptr; ) {
2176       int len = (int)strlen(stage);
2177       if (stagelen < len) stagelen = len;
2178     }
2179   }
2180 
2181   // Generate a list of stages
2182   fprintf(fp_hpp, "\n");
2183   fprintf(fp_hpp, "// Pipeline Stages\n");
2184   fprintf(fp_hpp, "enum machPipelineStages {\n");
2185   fprintf(fp_hpp, "   stage_%-*s = 0,\n", stagelen, "undefined");
2186 
2187   if( _pipeline ) {
2188     const char *stage;
2189     for ( _pipeline->_stages.reset(); (stage = _pipeline->_stages.iter()) != nullptr; )
2190       fprintf(fp_hpp, "   stage_%-*s = %d,\n", stagelen, stage, ++stagenum);
2191   }
2192 
2193   fprintf(fp_hpp, "   stage_%-*s = %d\n", stagelen, "count", stagenum);
2194   fprintf(fp_hpp, "};\n");
2195 
2196   fprintf(fp_hpp, "\n");
2197   fprintf(fp_hpp, "// Pipeline Resources\n");
2198   fprintf(fp_hpp, "enum machPipelineResources {\n");
2199   int rescount = 0;
2200 
2201   if( _pipeline ) {
2202     const char *resource;
2203     int reslen = 0;
2204 
2205     // Generate a list of resources, and masks
2206     for ( _pipeline->_reslist.reset(); (resource = _pipeline->_reslist.iter()) != nullptr; ) {
2207       int len = (int)strlen(resource);
2208       if (reslen < len)
2209         reslen = len;
2210     }
2211 
2212     for ( _pipeline->_reslist.reset(); (resource = _pipeline->_reslist.iter()) != nullptr; ) {
2213       const ResourceForm *resform = _pipeline->_resdict[resource]->is_resource();
2214       if (resform->is_discrete()) {
2215         fprintf(fp_hpp, "   resource_%-*s = %d,\n", reslen, resource, rescount++);
2216       }
2217     }
2218     fprintf(fp_hpp, "\n");
2219     for ( _pipeline->_reslist.reset(); (resource = _pipeline->_reslist.iter()) != nullptr; ) {
2220       const ResourceForm *resform = _pipeline->_resdict[resource]->is_resource();
2221       fprintf(fp_hpp, "   res_mask_%-*s = 0x%08x,\n", reslen, resource, resform->mask());
2222     }
2223     fprintf(fp_hpp, "\n");
2224   }
2225   fprintf(fp_hpp, "   resource_count = %d\n", rescount);
2226   fprintf(fp_hpp, "};\n");
2227 }