1 /*
  2  * Copyright (c) 1998, 2024, 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 // FORMS.CPP - Definitions for ADL Parser Forms Classes
 26 #include "adlc.hpp"
 27 
 28 //==============================Register Allocation============================
 29 int RegisterForm::_reg_ctr = 0;
 30 
 31 //------------------------------RegisterForm-----------------------------------
 32 // Constructor
 33 RegisterForm::RegisterForm()
 34   : _current_ac(nullptr),
 35     _regDef(cmpstr,hashstr, Form::arena),
 36     _regClass(cmpstr,hashstr, Form::arena),
 37     _allocClass(cmpstr,hashstr, Form::arena) {
 38 }
 39 RegisterForm::~RegisterForm() {
 40 }
 41 
 42 // record a new register definition
 43 void RegisterForm::addRegDef(char *name, char *callingConv, char *c_conv,
 44                              char *idealtype, char *encoding, char* concrete) {
 45   RegDef *regDef = new RegDef(name, callingConv, c_conv, idealtype, encoding, concrete);
 46   _rdefs.addName(name);
 47   _regDef.Insert(name,regDef);
 48 }
 49 
 50 // record a new register class
 51 template <typename T>
 52 T* RegisterForm::addRegClass(const char* className) {
 53   T* regClass = new T(className);
 54   _rclasses.addName(className);
 55   _regClass.Insert(className, regClass);
 56   return regClass;
 57 }
 58 
 59 // Explicit instantiation for all supported register classes.
 60 template RegClass* RegisterForm::addRegClass<RegClass>(const char* className);
 61 template CodeSnippetRegClass* RegisterForm::addRegClass<CodeSnippetRegClass>(const char* className);
 62 template ConditionalRegClass* RegisterForm::addRegClass<ConditionalRegClass>(const char* className);
 63 
 64 // record a new register class
 65 AllocClass *RegisterForm::addAllocClass(char *className) {
 66   AllocClass *allocClass = new AllocClass(className);
 67   _aclasses.addName(className);
 68   _allocClass.Insert(className,allocClass);
 69   return allocClass;
 70 }
 71 
 72 // Called after parsing the Register block.  Record the register class
 73 // for spill-slots/regs.
 74 void RegisterForm::addSpillRegClass() {
 75   // Stack slots start at the next available even register number.
 76   _reg_ctr = (_reg_ctr+7) & ~7;
 77   const char *rc_name = "stack_slots";
 78   RegClass* reg_class = new RegClass(rc_name);
 79   reg_class->set_stack_version(true);
 80   _rclasses.addName(rc_name);
 81   _regClass.Insert(rc_name,reg_class);
 82 }
 83 
 84 // Called after parsing the Register block.  Record the register class
 85 // for operands which are overwritten after matching.
 86 void RegisterForm::addDynamicRegClass() {
 87   const char *rc_name = "dynamic";
 88   RegClass* reg_class = new RegClass(rc_name);
 89   reg_class->set_stack_version(false);
 90   _rclasses.addName(rc_name);
 91   _regClass.Insert(rc_name,reg_class);
 92 }
 93 
 94 // Provide iteration over all register definitions
 95 // in the order used by the register allocator
 96 void        RegisterForm::reset_RegDefs() {
 97   _current_ac = nullptr;
 98   _aclasses.reset();
 99 }
100 
101 RegDef     *RegisterForm::iter_RegDefs() {
102   // Check if we need to get the next AllocClass
103   if ( _current_ac == nullptr ) {
104     const char *ac_name = _aclasses.iter();
105     if( ac_name == nullptr )   return nullptr;   // No more allocation classes
106     _current_ac = (AllocClass*)_allocClass[ac_name];
107     _current_ac->_regDefs.reset();
108     assert( _current_ac != nullptr, "Name must match an allocation class");
109   }
110 
111   const char *rd_name = _current_ac->_regDefs.iter();
112   if( rd_name == nullptr ) {
113     // At end of this allocation class, check the next
114     _current_ac = nullptr;
115     return iter_RegDefs();
116   }
117   RegDef *reg_def = (RegDef*)_current_ac->_regDef[rd_name];
118   assert( reg_def != nullptr, "Name must match a register definition");
119   return reg_def;
120 }
121 
122 // return the register definition with name 'regName'
123 RegDef *RegisterForm::getRegDef(const char *regName) {
124   RegDef *regDef = (RegDef*)_regDef[regName];
125   return  regDef;
126 }
127 
128 // return the register class with name 'className'
129 RegClass *RegisterForm::getRegClass(const char *className) {
130   RegClass *regClass = (RegClass*)_regClass[className];
131   return    regClass;
132 }
133 
134 
135 // Check that register classes are compatible with chunks
136 bool   RegisterForm::verify() {
137   bool valid = true;
138 
139   // Verify Register Classes
140   // check that each register class contains registers from one chunk
141   const char *rc_name = nullptr;
142   _rclasses.reset();
143   while ( (rc_name = _rclasses.iter()) != nullptr ) {
144     // Check the chunk value for all registers in this class
145     RegClass *reg_class = getRegClass(rc_name);
146     assert( reg_class != nullptr, "InternalError() no matching register class");
147   } // end of RegClasses
148 
149   // Verify that every register has been placed into an allocation class
150   RegDef *reg_def = nullptr;
151   reset_RegDefs();
152   uint  num_register_zero = 0;
153   while ( (reg_def = iter_RegDefs()) != nullptr ) {
154     if( reg_def->register_num() == 0 )  ++num_register_zero;
155   }
156   if( num_register_zero > 1 ) {
157     fprintf(stderr,
158             "ERROR: More than one register has been assigned register-number 0.\n"
159             "Probably because a register has not been entered into an allocation class.\n");
160   }
161 
162   return  valid;
163 }
164 
165 // Compute RegMask size
166 int RegisterForm::RegMask_Size() {
167   // Need at least this many words
168   int words_for_regs = (_reg_ctr + 31)>>5;
169   // The array of Register Mask bits should be large enough to cover
170   // all the machine registers and all parameters that need to be passed
171   // on the stack (stack registers) up to some interesting limit.  Methods
172   // that need more parameters will NOT be compiled.  On Intel, the limit
173   // is something like 90+ parameters.
174   // Add a few (3 words == 96 bits) for incoming & outgoing arguments to calls.
175   // Round up to the next doubleword size.
176   return (words_for_regs + 3 + 1) & ~1;
177 }
178 
179 void RegisterForm::dump() {                  // Debug printer
180   output(stderr);
181 }
182 
183 void RegisterForm::output(FILE *fp) {          // Write info to output files
184   const char *name;
185   fprintf(fp,"\n");
186   fprintf(fp,"-------------------- Dump RegisterForm --------------------\n");
187   for(_rdefs.reset(); (name = _rdefs.iter()) != nullptr;) {
188     ((RegDef*)_regDef[name])->output(fp);
189   }
190   fprintf(fp,"\n");
191   for (_rclasses.reset(); (name = _rclasses.iter()) != nullptr;) {
192     ((RegClass*)_regClass[name])->output(fp);
193   }
194   fprintf(fp,"\n");
195   for (_aclasses.reset(); (name = _aclasses.iter()) != nullptr;) {
196     ((AllocClass*)_allocClass[name])->output(fp);
197   }
198   fprintf(fp,"-------------------- end  RegisterForm --------------------\n");
199 }
200 
201 void RegisterForm::forms_do(FormClosure *f) {
202   const char *name = nullptr;
203   if (_current_ac) f->do_form(_current_ac);
204   for(_rdefs.reset(); (name = _rdefs.iter()) != nullptr;) {
205     f->do_form((RegDef*)_regDef[name]);
206   }
207   for (_rclasses.reset(); (name = _rclasses.iter()) != nullptr;) {
208     f->do_form((RegClass*)_regClass[name]);
209   }
210   for (_aclasses.reset(); (name = _aclasses.iter()) != nullptr;) {
211     f->do_form((AllocClass*)_allocClass[name]);
212   }
213 }
214 
215 //------------------------------RegDef-----------------------------------------
216 // Constructor
217 RegDef::RegDef(char *regname, char *callconv, char *c_conv, char * idealtype, char * encode, char * concrete)
218   : _regname(regname), _callconv(callconv), _c_conv(c_conv),
219     _idealtype(idealtype),
220     _register_encode(encode),
221     _concrete(concrete),
222     _register_num(0) {
223 
224   // AdlChunk and register mask are determined by the register number
225   // _register_num is set when registers are added to an allocation class
226 }
227 RegDef::~RegDef() {                      // Destructor
228 }
229 
230 void RegDef::set_register_num(uint32 register_num) {
231   _register_num      = register_num;
232 }
233 
234 // Bit pattern used for generating machine code
235 const char* RegDef::register_encode() const {
236   return _register_encode;
237 }
238 
239 // Register number used in machine-independent code
240 uint32 RegDef::register_num()    const {
241   return _register_num;
242 }
243 
244 void RegDef::dump() {
245   output(stderr);
246 }
247 
248 void RegDef::output(FILE *fp) {         // Write info to output files
249   fprintf(fp,"RegDef: %s (%s) encode as %s  using number %d\n",
250           _regname, (_callconv?_callconv:""), _register_encode, _register_num);
251   fprintf(fp,"\n");
252 }
253 
254 
255 //------------------------------RegClass---------------------------------------
256 // Construct a register class into which registers will be inserted
257 RegClass::RegClass(const char* classid) : _stack_or_reg(false), _classid(classid), _regDef(cmpstr, hashstr, Form::arena) {
258 }
259 
260 RegClass::~RegClass() {
261 }
262 
263 // record a register in this class
264 void RegClass::addReg(RegDef *regDef) {
265   _regDefs.addName(regDef->_regname);
266   _regDef.Insert((void*)regDef->_regname, regDef);
267 }
268 
269 // Number of registers in class
270 uint RegClass::size() const {
271   return _regDef.Size();
272 }
273 
274 const RegDef *RegClass::get_RegDef(const char *rd_name) const {
275   return  (const RegDef*)_regDef[rd_name];
276 }
277 
278 void RegClass::reset() {
279   _regDefs.reset();
280 }
281 
282 const char *RegClass::rd_name_iter() {
283   return _regDefs.iter();
284 }
285 
286 RegDef *RegClass::RegDef_iter() {
287   const char *rd_name  = rd_name_iter();
288   RegDef     *reg_def  = rd_name ? (RegDef*)_regDef[rd_name] : nullptr;
289   return      reg_def;
290 }
291 
292 const RegDef* RegClass::find_first_elem() {
293   const RegDef* first = nullptr;
294   const RegDef* def = nullptr;
295 
296   reset();
297   while ((def = RegDef_iter()) != nullptr) {
298     if (first == nullptr || def->register_num() < first->register_num()) {
299       first = def;
300     }
301   }
302 
303   assert(first != nullptr, "empty mask?");
304   return first;;
305 }
306 
307 // Collect all the registers in this register-word.  One bit per register.
308 int RegClass::regs_in_word( int wordnum, bool stack_also ) {
309   int         word = 0;
310   const char *name;
311   for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
312     int rnum = ((RegDef*)_regDef[name])->register_num();
313     if( (rnum >> 5) == wordnum )
314       word |= (1 << (rnum & 31));
315   }
316   if( stack_also ) {
317     // Now also collect stack bits
318     for( int i = 0; i < 32; i++ )
319       if( wordnum*32+i >= RegisterForm::_reg_ctr )
320         word |= (1 << i);
321   }
322 
323   return word;
324 }
325 
326 void RegClass::dump() {
327   output(stderr);
328 }
329 
330 void RegClass::output(FILE *fp) {           // Write info to output files
331   fprintf(fp,"RegClass: %s\n",_classid);
332   const char *name;
333   for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
334     ((RegDef*)_regDef[name])->output(fp);
335   }
336   fprintf(fp,"--- done with entries for reg_class %s\n\n",_classid);
337 }
338 
339 void RegClass::forms_do(FormClosure *f) {
340   const char *name = nullptr;
341   for( _regDefs.reset(); (name = _regDefs.iter()) != nullptr; ) {
342     f->do_form((RegDef*)_regDef[name]);
343   }
344 }
345 
346 void RegClass::declare_register_masks(FILE* fp) {
347   const char* prefix = "";
348   const char* rc_name_to_upper = toUpper(_classid);
349   fprintf(fp, "extern const RegMask _%s%s_mask;\n", prefix,  rc_name_to_upper);
350   fprintf(fp, "inline const RegMask &%s%s_mask() { return _%s%s_mask; }\n", prefix, rc_name_to_upper, prefix, rc_name_to_upper);
351   if (_stack_or_reg) {
352     fprintf(fp, "extern const RegMask _%sSTACK_OR_%s_mask;\n", prefix, rc_name_to_upper);
353     fprintf(fp, "inline const RegMask &%sSTACK_OR_%s_mask() { return _%sSTACK_OR_%s_mask; }\n", prefix, rc_name_to_upper, prefix, rc_name_to_upper);
354   }
355   delete[] rc_name_to_upper;
356 }
357 
358 void RegClass::build_register_masks(FILE* fp) {
359   int len = RegisterForm::RegMask_Size();
360   const char *prefix = "";
361   const char* rc_name_to_upper = toUpper(_classid);
362   fprintf(fp, "const RegMask _%s%s_mask(", prefix, rc_name_to_upper);
363 
364   int i;
365   for(i = 0; i < len - 1; i++) {
366     fprintf(fp," 0x%x,", regs_in_word(i, false));
367   }
368   fprintf(fp," 0x%x );\n", regs_in_word(i, false));
369 
370   if (_stack_or_reg) {
371     fprintf(fp, "const RegMask _%sSTACK_OR_%s_mask(", prefix, rc_name_to_upper);
372     for(i = 0; i < len - 1; i++) {
373       fprintf(fp," 0x%x,", regs_in_word(i, true));
374     }
375     fprintf(fp," 0x%x );\n", regs_in_word(i, true));
376   }
377   delete[] rc_name_to_upper;
378 }
379 
380 //------------------------------CodeSnippetRegClass---------------------------
381 CodeSnippetRegClass::CodeSnippetRegClass(const char* classid) : RegClass(classid), _code_snippet(nullptr) {
382 }
383 
384 CodeSnippetRegClass::~CodeSnippetRegClass() {
385   delete _code_snippet;
386 }
387 
388 void CodeSnippetRegClass::declare_register_masks(FILE* fp) {
389   const char* prefix = "";
390   const char* rc_name_to_upper = toUpper(_classid);
391   fprintf(fp, "inline const RegMask &%s%s_mask() { %s }\n", prefix, rc_name_to_upper, _code_snippet);
392   delete[] rc_name_to_upper;
393 }
394 
395 //------------------------------ConditionalRegClass---------------------------
396 ConditionalRegClass::ConditionalRegClass(const char *classid) : RegClass(classid), _condition_code(nullptr) {
397     _rclasses[0] = nullptr;
398     _rclasses[1] = nullptr;
399 }
400 
401 ConditionalRegClass::~ConditionalRegClass() {
402   delete _condition_code;
403 }
404 
405 void ConditionalRegClass::declare_register_masks(FILE* fp) {
406   const char* prefix = "";
407   const char* rc_name_to_upper = toUpper(_classid);
408   const char* rclass_0_to_upper = toUpper(_rclasses[0]->_classid);
409   const char* rclass_1_to_upper = toUpper(_rclasses[1]->_classid);
410   fprintf(fp, "inline const RegMask &%s%s_mask() {"
411               " return (%s) ?"
412               " %s%s_mask() :"
413               " %s%s_mask(); }\n",
414               prefix, rc_name_to_upper,
415               _condition_code,
416               prefix, rclass_0_to_upper,
417               prefix, rclass_1_to_upper);
418   if (_stack_or_reg) {
419     fprintf(fp, "inline const RegMask &%sSTACK_OR_%s_mask() {"
420                   " return (%s) ?"
421                   " %sSTACK_OR_%s_mask() :"
422                   " %sSTACK_OR_%s_mask(); }\n",
423                   prefix, rc_name_to_upper,
424                   _condition_code,
425                   prefix, rclass_0_to_upper,
426                   prefix, rclass_1_to_upper);
427   }
428   delete[] rc_name_to_upper;
429   delete[] rclass_0_to_upper;
430   delete[] rclass_1_to_upper;
431   return;
432 }
433 
434 //------------------------------AllocClass-------------------------------------
435 AllocClass::AllocClass(char *classid) : _classid(classid), _regDef(cmpstr,hashstr, Form::arena) {
436 }
437 
438 // record a register in this class
439 void AllocClass::addReg(RegDef *regDef) {
440   assert( regDef != nullptr, "Can not add a null to an allocation class");
441   regDef->set_register_num( RegisterForm::_reg_ctr++ );
442   // Add regDef to this allocation class
443   _regDefs.addName(regDef->_regname);
444   _regDef.Insert((void*)regDef->_regname, regDef);
445 }
446 
447 void AllocClass::dump() {
448   output(stderr);
449 }
450 
451 void AllocClass::output(FILE *fp) {       // Write info to output files
452   fprintf(fp,"AllocClass: %s \n",_classid);
453   const char *name;
454   for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
455     ((RegDef*)_regDef[name])->output(fp);
456   }
457   fprintf(fp,"--- done with entries for alloc_class %s\n\n",_classid);
458 }
459 
460 void AllocClass::forms_do(FormClosure* f) {
461   const char *name;
462   for(_regDefs.reset(); (name = _regDefs.iter()) != nullptr;) {
463     f->do_form((RegDef*)_regDef[name]);
464   }
465   return;
466 }
467 
468 //==============================Frame Handling=================================
469 //------------------------------FrameForm--------------------------------------
470 FrameForm::FrameForm() {
471   _sync_stack_slots = nullptr;
472   _inline_cache_reg = nullptr;
473   _interpreter_frame_pointer_reg = nullptr;
474   _cisc_spilling_operand_name = nullptr;
475   _frame_pointer = nullptr;
476   _c_frame_pointer = nullptr;
477   _alignment = nullptr;
478   _return_addr_loc = false;
479   _c_return_addr_loc = false;
480   _return_addr = nullptr;
481   _c_return_addr = nullptr;
482   _varargs_C_out_slots_killed = nullptr;
483   _return_value = nullptr;
484   _c_return_value = nullptr;
485 }
486 
487 FrameForm::~FrameForm() {
488 }
489 
490 void FrameForm::dump() {
491   output(stderr);
492 }
493 
494 void FrameForm::output(FILE *fp) {           // Write info to output files
495   fprintf(fp,"\nFrame:\n");
496 }
497 
498 //==============================Scheduling=====================================
499 //------------------------------PipelineForm-----------------------------------
500 PipelineForm::PipelineForm()
501   :  _reslist               ()
502   ,  _resdict               (cmpstr, hashstr, Form::arena)
503   ,  _classdict             (cmpstr, hashstr, Form::arena)
504   ,  _rescount              (0)
505   ,  _maxcycleused          (0)
506   ,  _stages                ()
507   ,  _stagecnt              (0)
508   ,  _classlist             ()
509   ,  _classcnt              (0)
510   ,  _noplist               ()
511   ,  _nopcnt                (0)
512   ,  _variableSizeInstrs    (false)
513   ,  _branchHasDelaySlot    (false)
514   ,  _maxInstrsPerBundle    (0)
515   ,  _maxBundlesPerCycle    (1)
516   ,  _instrUnitSize         (0)
517   ,  _bundleUnitSize        (0)
518   ,  _instrFetchUnitSize    (0)
519   ,  _instrFetchUnits       (0) {
520 }
521 PipelineForm::~PipelineForm() {
522 }
523 
524 void PipelineForm::dump() {
525   output(stderr);
526 }
527 
528 void PipelineForm::output(FILE *fp) {           // Write info to output files
529   const char *res;
530   const char *stage;
531   const char *cls;
532   const char *nop;
533   int count = 0;
534 
535   fprintf(fp,"\nPipeline:");
536   if (_variableSizeInstrs)
537     if (_instrUnitSize > 0)
538       fprintf(fp," variable-sized instructions in %d byte units", _instrUnitSize);
539     else
540       fprintf(fp," variable-sized instructions");
541   else
542     if (_instrUnitSize > 0)
543       fprintf(fp," fixed-sized instructions of %d bytes", _instrUnitSize);
544     else if (_bundleUnitSize > 0)
545       fprintf(fp," fixed-sized bundles of %d bytes", _bundleUnitSize);
546     else
547       fprintf(fp," fixed-sized instructions");
548   if (_branchHasDelaySlot)
549     fprintf(fp,", branch has delay slot");
550   if (_maxInstrsPerBundle > 0)
551     fprintf(fp,", max of %d instruction%s in parallel",
552       _maxInstrsPerBundle, _maxInstrsPerBundle > 1 ? "s" : "");
553   if (_maxBundlesPerCycle > 0)
554     fprintf(fp,", max of %d bundle%s in parallel",
555       _maxBundlesPerCycle, _maxBundlesPerCycle > 1 ? "s" : "");
556   if (_instrFetchUnitSize > 0 && _instrFetchUnits)
557     fprintf(fp, ", fetch %d x % d bytes per cycle", _instrFetchUnits, _instrFetchUnitSize);
558 
559   fprintf(fp,"\nResource:");
560   for ( _reslist.reset(); (res = _reslist.iter()) != nullptr; )
561     fprintf(fp," %s(0x%08x)", res, _resdict[res]->is_resource()->mask());
562   fprintf(fp,"\n");
563 
564   fprintf(fp,"\nDescription:\n");
565   for ( _stages.reset(); (stage = _stages.iter()) != nullptr; )
566     fprintf(fp," %s(%d)", stage, count++);
567   fprintf(fp,"\n");
568 
569   fprintf(fp,"\nClasses:\n");
570   for ( _classlist.reset(); (cls = _classlist.iter()) != nullptr; )
571     _classdict[cls]->is_pipeclass()->output(fp);
572 
573   fprintf(fp,"\nNop Instructions:");
574   for ( _noplist.reset(); (nop = _noplist.iter()) != nullptr; )
575     fprintf(fp, " \"%s\"", nop);
576   fprintf(fp,"\n");
577 }
578 
579 
580 //------------------------------ResourceForm-----------------------------------
581 ResourceForm::ResourceForm(unsigned resmask)
582 : _resmask(resmask) {
583 }
584 ResourceForm::~ResourceForm() {
585 }
586 
587 ResourceForm  *ResourceForm::is_resource() const {
588   return (ResourceForm *)(this);
589 }
590 
591 void ResourceForm::dump() {
592   output(stderr);
593 }
594 
595 void ResourceForm::output(FILE *fp) {          // Write info to output files
596   fprintf(fp, "resource: 0x%08x;\n", mask());
597 }
598 
599 
600 //------------------------------PipeClassOperandForm----------------------------------
601 
602 void PipeClassOperandForm::dump() {
603   output(stderr);
604 }
605 
606 void PipeClassOperandForm::output(FILE *fp) {         // Write info to output files
607   fprintf(stderr,"PipeClassOperandForm: %s", _stage);
608   fflush(stderr);
609   if (_more_instrs > 0)
610     fprintf(stderr,"+%d", _more_instrs);
611   fprintf(stderr," (%s)\n", _iswrite ? "write" : "read");
612   fflush(stderr);
613   fprintf(fp,"PipeClassOperandForm: %s", _stage);
614   if (_more_instrs > 0)
615     fprintf(fp,"+%d", _more_instrs);
616   fprintf(fp," (%s)\n", _iswrite ? "write" : "read");
617 }
618 
619 
620 //------------------------------PipeClassResourceForm----------------------------------
621 
622 void PipeClassResourceForm::dump() {
623   output(stderr);
624 }
625 
626 void PipeClassResourceForm::output(FILE *fp) {         // Write info to output files
627   fprintf(fp,"PipeClassResourceForm: %s at stage %s for %d cycles\n",
628      _resource, _stage, _cycles);
629 }
630 
631 
632 //------------------------------PipeClassForm----------------------------------
633 PipeClassForm::PipeClassForm(const char *id, int num)
634   : _ident(id)
635   , _num(num)
636   , _localNames(cmpstr, hashstr, Form::arena)
637   , _localUsage(cmpstr, hashstr, Form::arena)
638   , _has_fixed_latency(0)
639   , _fixed_latency(0)
640   , _instruction_count(0)
641   , _has_multiple_bundles(false)
642   , _has_branch_delay_slot(false)
643   , _force_serialization(false)
644   , _may_have_no_code(false) {
645 }
646 
647 PipeClassForm::~PipeClassForm() {
648 }
649 
650 PipeClassForm  *PipeClassForm::is_pipeclass() const {
651   return (PipeClassForm *)(this);
652 }
653 
654 void PipeClassForm::dump() {
655   output(stderr);
656 }
657 
658 void PipeClassForm::output(FILE *fp) {         // Write info to output files
659   fprintf(fp,"PipeClassForm: #%03d", _num);
660   if (_ident)
661      fprintf(fp," \"%s\":", _ident);
662   if (_has_fixed_latency)
663      fprintf(fp," latency %d", _fixed_latency);
664   if (_force_serialization)
665      fprintf(fp, ", force serialization");
666   if (_may_have_no_code)
667      fprintf(fp, ", may have no code");
668   fprintf(fp, ", %d instruction%s\n", InstructionCount(), InstructionCount() != 1 ? "s" : "");
669 }
670 
671 
672 //==============================Peephole Optimization==========================
673 int Peephole::_peephole_counter = 0;
674 //------------------------------Peephole---------------------------------------
675 Peephole::Peephole() : _predicate(nullptr), _match(nullptr), _procedure(nullptr),
676                        _constraint(nullptr), _replace(nullptr), _next(nullptr) {
677   _peephole_number = _peephole_counter++;
678 }
679 Peephole::~Peephole() {
680 }
681 
682 // Append a peephole rule with the same root instruction
683 void Peephole::append_peephole(Peephole *next_peephole) {
684   if( _next == nullptr ) {
685     _next = next_peephole;
686   } else {
687     _next->append_peephole( next_peephole );
688   }
689 }
690 
691 // Add a predicate to this peephole rule
692 void Peephole::add_predicate(PeepPredicate* predicate) {
693   assert( _predicate == nullptr, "fatal()" );
694   _predicate = predicate;
695 }
696 
697 // Store the components of this peephole rule
698 void Peephole::add_match(PeepMatch *match) {
699   assert( _match == nullptr, "fatal()" );
700   _match = match;
701 }
702 
703 // Add a procedure to this peephole rule
704 void Peephole::add_procedure(PeepProcedure* procedure) {
705   assert( _procedure == nullptr, "fatal()" );
706   _procedure = procedure;
707 }
708 
709 void Peephole::append_constraint(PeepConstraint *next_constraint) {
710   if( _constraint == nullptr ) {
711     _constraint = next_constraint;
712   } else {
713     _constraint->append( next_constraint );
714   }
715 }
716 
717 void Peephole::add_replace(PeepReplace *replace) {
718   assert( _replace == nullptr, "fatal()" );
719   _replace = replace;
720 }
721 
722 // class Peephole accessor methods are in the declaration.
723 
724 
725 void Peephole::dump() {
726   output(stderr);
727 }
728 
729 void Peephole::output(FILE *fp) {         // Write info to output files
730   fprintf(fp,"Peephole:\n");
731   if( _match != nullptr )       _match->output(fp);
732   if( _constraint != nullptr )  _constraint->output(fp);
733   if( _replace != nullptr )     _replace->output(fp);
734   // Output the next entry
735   if( _next ) _next->output(fp);
736 }
737 
738 void Peephole::forms_do(FormClosure *f) {
739   if (_predicate) f->do_form(_predicate);
740   if (_match) f->do_form(_match);
741   if (_procedure) f->do_form(_procedure);
742   if (_constraint) f->do_form(_constraint);
743   if (_replace) f->do_form(_replace);
744   return;
745 }
746 
747 //----------------------------PeepPredicate------------------------------------
748 PeepPredicate::PeepPredicate(const char* rule) : _rule(rule) {
749 }
750 PeepPredicate::~PeepPredicate() {
751 }
752 
753 const char* PeepPredicate::rule() const {
754   return _rule;
755 }
756 
757 void PeepPredicate::dump() {
758   output(stderr);
759 }
760 
761 void PeepPredicate::output(FILE* fp) {
762   fprintf(fp, "PeepPredicate\n");
763 }
764 
765 //------------------------------PeepMatch--------------------------------------
766 PeepMatch::PeepMatch(char *rule) : _max_position(0), _rule(rule) {
767 }
768 PeepMatch::~PeepMatch() {
769 }
770 
771 // Insert info into the match-rule
772 void  PeepMatch::add_instruction(int parent, int position, const char *name,
773                                  int input) {
774   if( position > _max_position ) _max_position = position;
775 
776   _parent.addName((char*) (intptr_t) parent);
777   _position.addName((char*) (intptr_t) position);
778   _instrs.addName(name);
779   _input.addName((char*) (intptr_t) input);
780 }
781 
782 // Access info about instructions in the peep-match rule
783 int   PeepMatch::max_position() {
784   return _max_position;
785 }
786 
787 const char *PeepMatch::instruction_name(int position) {
788   return _instrs.name(position);
789 }
790 
791 // Iterate through all info on matched instructions
792 void  PeepMatch::reset() {
793   _parent.reset();
794   _position.reset();
795   _instrs.reset();
796   _input.reset();
797 }
798 
799 void  PeepMatch::next_instruction(int &parent, int &position, const char* &name, int &input) {
800   parent   = (int) (intptr_t) _parent.iter();
801   position = (int) (intptr_t) _position.iter();
802   name     = _instrs.iter();
803   input    = (int) (intptr_t) _input.iter();
804 }
805 
806 // 'true' if current position in iteration is a placeholder, not matched.
807 bool  PeepMatch::is_placeholder() {
808   return _instrs.current_is_signal();
809 }
810 
811 
812 void PeepMatch::dump() {
813   output(stderr);
814 }
815 
816 void PeepMatch::output(FILE *fp) {        // Write info to output files
817   fprintf(fp,"PeepMatch:\n");
818 }
819 
820 //----------------------------PeepProcedure------------------------------------
821 PeepProcedure::PeepProcedure(const char* name) : _name(name) {
822 }
823 PeepProcedure::~PeepProcedure() {
824 }
825 
826 const char* PeepProcedure::name() const {
827   return _name;
828 }
829 
830 void PeepProcedure::dump() {
831   output(stderr);
832 }
833 
834 void PeepProcedure::output(FILE* fp) {
835   fprintf(fp, "PeepProcedure\n");
836 }
837 
838 //------------------------------PeepConstraint---------------------------------
839 PeepConstraint::PeepConstraint(int left_inst,  char* left_op, char* relation,
840                                int right_inst, char* right_op)
841   : _left_inst(left_inst), _left_op(left_op), _relation(relation),
842     _right_inst(right_inst), _right_op(right_op), _next(nullptr) {}
843 PeepConstraint::~PeepConstraint() {
844 }
845 
846 // Check if constraints use instruction at position
847 bool PeepConstraint::constrains_instruction(int position) {
848   // Check local instruction constraints
849   if( _left_inst  == position ) return true;
850   if( _right_inst == position ) return true;
851 
852   // Check remaining constraints in list
853   if( _next == nullptr )  return false;
854   else                 return _next->constrains_instruction(position);
855 }
856 
857 // Add another constraint
858 void PeepConstraint::append(PeepConstraint *next_constraint) {
859   if( _next == nullptr ) {
860     _next = next_constraint;
861   } else {
862     _next->append( next_constraint );
863   }
864 }
865 
866 // Access the next constraint in the list
867 PeepConstraint *PeepConstraint::next() {
868   return _next;
869 }
870 
871 
872 void PeepConstraint::dump() {
873   output(stderr);
874 }
875 
876 void PeepConstraint::output(FILE *fp) {   // Write info to output files
877   fprintf(fp,"PeepConstraint:\n");
878 }
879 
880 //------------------------------PeepReplace------------------------------------
881 PeepReplace::PeepReplace(char *rule) : _rule(rule) {
882 }
883 PeepReplace::~PeepReplace() {
884 }
885 
886 // Add contents of peepreplace
887 void  PeepReplace::add_instruction(char *root) {
888   _instruction.addName(root);
889   _operand_inst_num.add_signal();
890   _operand_op_name.add_signal();
891 }
892 void  PeepReplace::add_operand( int inst_num, char *inst_operand ) {
893   _instruction.add_signal();
894   _operand_inst_num.addName((char*) (intptr_t) inst_num);
895   _operand_op_name.addName(inst_operand);
896 }
897 
898 // Access contents of peepreplace
899 void  PeepReplace::reset() {
900   _instruction.reset();
901   _operand_inst_num.reset();
902   _operand_op_name.reset();
903 }
904 void  PeepReplace::next_instruction(const char* &inst){
905   inst                     = _instruction.iter();
906   int         inst_num     = (int) (intptr_t) _operand_inst_num.iter();
907   const char* inst_operand = _operand_op_name.iter();
908 }
909 void  PeepReplace::next_operand(int &inst_num, const char* &inst_operand) {
910   const char* inst = _instruction.iter();
911   inst_num         = (int) (intptr_t) _operand_inst_num.iter();
912   inst_operand     = _operand_op_name.iter();
913 }
914 
915 
916 
917 void PeepReplace::dump() {
918   output(stderr);
919 }
920 
921 void PeepReplace::output(FILE *fp) {      // Write info to output files
922   fprintf(fp,"PeepReplace:\n");
923 }