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