duke@435: /* duke@435: * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // output_c.cpp - Class CPP file output routines for architecture definition duke@435: duke@435: #include "adlc.hpp" duke@435: duke@435: // Utilities to characterize effect statements duke@435: static bool is_def(int usedef) { duke@435: switch(usedef) { duke@435: case Component::DEF: duke@435: case Component::USE_DEF: return true; break; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: static bool is_use(int usedef) { duke@435: switch(usedef) { duke@435: case Component::USE: duke@435: case Component::USE_DEF: duke@435: case Component::USE_KILL: return true; break; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: static bool is_kill(int usedef) { duke@435: switch(usedef) { duke@435: case Component::KILL: duke@435: case Component::USE_KILL: return true; break; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: // Define an array containing the machine register names, strings. duke@435: static void defineRegNames(FILE *fp, RegisterForm *registers) { duke@435: if (registers) { duke@435: fprintf(fp,"\n"); duke@435: fprintf(fp,"// An array of character pointers to machine register names.\n"); duke@435: fprintf(fp,"const char *Matcher::regName[REG_COUNT] = {\n"); duke@435: duke@435: // Output the register name for each register in the allocation classes duke@435: RegDef *reg_def = NULL; duke@435: RegDef *next = NULL; duke@435: registers->reset_RegDefs(); duke@435: for( reg_def = registers->iter_RegDefs(); reg_def != NULL; reg_def = next ) { duke@435: next = registers->iter_RegDefs(); duke@435: const char *comma = (next != NULL) ? "," : " // no trailing comma"; duke@435: fprintf(fp," \"%s\"%s\n", duke@435: reg_def->_regname, comma ); duke@435: } duke@435: duke@435: // Finish defining enumeration duke@435: fprintf(fp,"};\n"); duke@435: duke@435: fprintf(fp,"\n"); duke@435: fprintf(fp,"// An array of character pointers to machine register names.\n"); duke@435: fprintf(fp,"const VMReg OptoReg::opto2vm[REG_COUNT] = {\n"); duke@435: reg_def = NULL; duke@435: next = NULL; duke@435: registers->reset_RegDefs(); duke@435: for( reg_def = registers->iter_RegDefs(); reg_def != NULL; reg_def = next ) { duke@435: next = registers->iter_RegDefs(); duke@435: const char *comma = (next != NULL) ? "," : " // no trailing comma"; duke@435: fprintf(fp,"\t%s%s\n", reg_def->_concrete, comma ); duke@435: } duke@435: // Finish defining array duke@435: fprintf(fp,"\t};\n"); duke@435: fprintf(fp,"\n"); duke@435: duke@435: fprintf(fp," OptoReg::Name OptoReg::vm2opto[ConcreteRegisterImpl::number_of_registers];\n"); duke@435: duke@435: } duke@435: } duke@435: duke@435: // Define an array containing the machine register encoding values duke@435: static void defineRegEncodes(FILE *fp, RegisterForm *registers) { duke@435: if (registers) { duke@435: fprintf(fp,"\n"); duke@435: fprintf(fp,"// An array of the machine register encode values\n"); duke@435: fprintf(fp,"const unsigned char Matcher::_regEncode[REG_COUNT] = {\n"); duke@435: duke@435: // Output the register encoding for each register in the allocation classes duke@435: RegDef *reg_def = NULL; duke@435: RegDef *next = NULL; duke@435: registers->reset_RegDefs(); duke@435: for( reg_def = registers->iter_RegDefs(); reg_def != NULL; reg_def = next ) { duke@435: next = registers->iter_RegDefs(); duke@435: const char* register_encode = reg_def->register_encode(); duke@435: const char *comma = (next != NULL) ? "," : " // no trailing comma"; duke@435: int encval; duke@435: if (!ADLParser::is_int_token(register_encode, encval)) { duke@435: fprintf(fp," %s%s // %s\n", duke@435: register_encode, comma, reg_def->_regname ); duke@435: } else { duke@435: // Output known constants in hex char format (backward compatibility). duke@435: assert(encval < 256, "Exceeded supported width for register encoding"); duke@435: fprintf(fp," (unsigned char)'\\x%X'%s // %s\n", duke@435: encval, comma, reg_def->_regname ); duke@435: } duke@435: } duke@435: // Finish defining enumeration duke@435: fprintf(fp,"};\n"); duke@435: duke@435: } // Done defining array duke@435: } duke@435: duke@435: // Output an enumeration of register class names duke@435: static void defineRegClassEnum(FILE *fp, RegisterForm *registers) { duke@435: if (registers) { duke@435: // Output an enumeration of register class names duke@435: fprintf(fp,"\n"); duke@435: fprintf(fp,"// Enumeration of register class names\n"); duke@435: fprintf(fp, "enum machRegisterClass {\n"); duke@435: registers->_rclasses.reset(); duke@435: for( const char *class_name = NULL; duke@435: (class_name = registers->_rclasses.iter()) != NULL; ) { duke@435: fprintf(fp," %s,\n", toUpper( class_name )); duke@435: } duke@435: // Finish defining enumeration duke@435: fprintf(fp, " _last_Mach_Reg_Class\n"); duke@435: fprintf(fp, "};\n"); duke@435: } duke@435: } duke@435: duke@435: // Declare an enumeration of user-defined register classes duke@435: // and a list of register masks, one for each class. duke@435: void ArchDesc::declare_register_masks(FILE *fp_hpp) { duke@435: const char *rc_name; duke@435: duke@435: if( _register ) { duke@435: // Build enumeration of user-defined register classes. duke@435: defineRegClassEnum(fp_hpp, _register); duke@435: duke@435: // Generate a list of register masks, one for each class. duke@435: fprintf(fp_hpp,"\n"); duke@435: fprintf(fp_hpp,"// Register masks, one for each register class.\n"); duke@435: _register->_rclasses.reset(); duke@435: for( rc_name = NULL; duke@435: (rc_name = _register->_rclasses.iter()) != NULL; ) { duke@435: const char *prefix = ""; duke@435: RegClass *reg_class = _register->getRegClass(rc_name); duke@435: assert( reg_class, "Using an undefined register class"); duke@435: duke@435: int len = RegisterForm::RegMask_Size(); duke@435: fprintf(fp_hpp, "extern const RegMask %s%s_mask;\n", prefix, toUpper( rc_name ) ); duke@435: duke@435: if( reg_class->_stack_or_reg ) { duke@435: fprintf(fp_hpp, "extern const RegMask %sSTACK_OR_%s_mask;\n", prefix, toUpper( rc_name ) ); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Generate an enumeration of user-defined register classes duke@435: // and a list of register masks, one for each class. duke@435: void ArchDesc::build_register_masks(FILE *fp_cpp) { duke@435: const char *rc_name; duke@435: duke@435: if( _register ) { duke@435: // Generate a list of register masks, one for each class. duke@435: fprintf(fp_cpp,"\n"); duke@435: fprintf(fp_cpp,"// Register masks, one for each register class.\n"); duke@435: _register->_rclasses.reset(); duke@435: for( rc_name = NULL; duke@435: (rc_name = _register->_rclasses.iter()) != NULL; ) { duke@435: const char *prefix = ""; duke@435: RegClass *reg_class = _register->getRegClass(rc_name); duke@435: assert( reg_class, "Using an undefined register class"); duke@435: duke@435: int len = RegisterForm::RegMask_Size(); duke@435: fprintf(fp_cpp, "const RegMask %s%s_mask(", prefix, toUpper( rc_name ) ); duke@435: { int i; duke@435: for( i = 0; i < len-1; i++ ) duke@435: fprintf(fp_cpp," 0x%x,",reg_class->regs_in_word(i,false)); duke@435: fprintf(fp_cpp," 0x%x );\n",reg_class->regs_in_word(i,false)); duke@435: } duke@435: duke@435: if( reg_class->_stack_or_reg ) { duke@435: int i; duke@435: fprintf(fp_cpp, "const RegMask %sSTACK_OR_%s_mask(", prefix, toUpper( rc_name ) ); duke@435: for( i = 0; i < len-1; i++ ) duke@435: fprintf(fp_cpp," 0x%x,",reg_class->regs_in_word(i,true)); duke@435: fprintf(fp_cpp," 0x%x );\n",reg_class->regs_in_word(i,true)); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Compute an index for an array in the pipeline_reads_NNN arrays duke@435: static int pipeline_reads_initializer(FILE *fp_cpp, NameList &pipeline_reads, PipeClassForm *pipeclass) duke@435: { duke@435: int templen = 1; duke@435: int paramcount = 0; duke@435: const char *paramname; duke@435: duke@435: if (pipeclass->_parameters.count() == 0) duke@435: return -1; duke@435: duke@435: pipeclass->_parameters.reset(); duke@435: paramname = pipeclass->_parameters.iter(); duke@435: const PipeClassOperandForm *pipeopnd = duke@435: (const PipeClassOperandForm *)pipeclass->_localUsage[paramname]; duke@435: if (pipeopnd && !pipeopnd->isWrite() && strcmp(pipeopnd->_stage, "Universal")) duke@435: pipeclass->_parameters.reset(); duke@435: duke@435: while ( (paramname = pipeclass->_parameters.iter()) != NULL ) { duke@435: const PipeClassOperandForm *pipeopnd = duke@435: (const PipeClassOperandForm *)pipeclass->_localUsage[paramname]; duke@435: duke@435: if (pipeopnd) duke@435: templen += 10 + (int)strlen(pipeopnd->_stage); duke@435: else duke@435: templen += 19; duke@435: duke@435: paramcount++; duke@435: } duke@435: duke@435: // See if the count is zero duke@435: if (paramcount == 0) { duke@435: return -1; duke@435: } duke@435: duke@435: char *operand_stages = new char [templen]; duke@435: operand_stages[0] = 0; duke@435: int i = 0; duke@435: templen = 0; duke@435: duke@435: pipeclass->_parameters.reset(); duke@435: paramname = pipeclass->_parameters.iter(); duke@435: pipeopnd = (const PipeClassOperandForm *)pipeclass->_localUsage[paramname]; duke@435: if (pipeopnd && !pipeopnd->isWrite() && strcmp(pipeopnd->_stage, "Universal")) duke@435: pipeclass->_parameters.reset(); duke@435: duke@435: while ( (paramname = pipeclass->_parameters.iter()) != NULL ) { duke@435: const PipeClassOperandForm *pipeopnd = duke@435: (const PipeClassOperandForm *)pipeclass->_localUsage[paramname]; duke@435: templen += sprintf(&operand_stages[templen], " stage_%s%c\n", duke@435: pipeopnd ? pipeopnd->_stage : "undefined", duke@435: (++i < paramcount ? ',' : ' ') ); duke@435: } duke@435: duke@435: // See if the same string is in the table duke@435: int ndx = pipeline_reads.index(operand_stages); duke@435: duke@435: // No, add it to the table duke@435: if (ndx < 0) { duke@435: pipeline_reads.addName(operand_stages); duke@435: ndx = pipeline_reads.index(operand_stages); duke@435: duke@435: fprintf(fp_cpp, "static const enum machPipelineStages pipeline_reads_%03d[%d] = {\n%s};\n\n", duke@435: ndx+1, paramcount, operand_stages); duke@435: } duke@435: else duke@435: delete [] operand_stages; duke@435: duke@435: return (ndx); duke@435: } duke@435: duke@435: // Compute an index for an array in the pipeline_res_stages_NNN arrays duke@435: static int pipeline_res_stages_initializer( duke@435: FILE *fp_cpp, duke@435: PipelineForm *pipeline, duke@435: NameList &pipeline_res_stages, duke@435: PipeClassForm *pipeclass) duke@435: { duke@435: const PipeClassResourceForm *piperesource; duke@435: int * res_stages = new int [pipeline->_rescount]; duke@435: int i; duke@435: duke@435: for (i = 0; i < pipeline->_rescount; i++) duke@435: res_stages[i] = 0; duke@435: duke@435: for (pipeclass->_resUsage.reset(); duke@435: (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) { duke@435: int used_mask = pipeline->_resdict[piperesource->_resource]->is_resource()->mask(); duke@435: for (i = 0; i < pipeline->_rescount; i++) duke@435: if ((1 << i) & used_mask) { duke@435: int stage = pipeline->_stages.index(piperesource->_stage); duke@435: if (res_stages[i] < stage+1) duke@435: res_stages[i] = stage+1; duke@435: } duke@435: } duke@435: duke@435: // Compute the length needed for the resource list duke@435: int commentlen = 0; duke@435: int max_stage = 0; duke@435: for (i = 0; i < pipeline->_rescount; i++) { duke@435: if (res_stages[i] == 0) { duke@435: if (max_stage < 9) duke@435: max_stage = 9; duke@435: } duke@435: else { duke@435: int stagelen = (int)strlen(pipeline->_stages.name(res_stages[i]-1)); duke@435: if (max_stage < stagelen) duke@435: max_stage = stagelen; duke@435: } duke@435: duke@435: commentlen += (int)strlen(pipeline->_reslist.name(i)); duke@435: } duke@435: duke@435: int templen = 1 + commentlen + pipeline->_rescount * (max_stage + 14); duke@435: duke@435: // Allocate space for the resource list duke@435: char * resource_stages = new char [templen]; duke@435: duke@435: templen = 0; duke@435: for (i = 0; i < pipeline->_rescount; i++) { duke@435: const char * const resname = duke@435: res_stages[i] == 0 ? "undefined" : pipeline->_stages.name(res_stages[i]-1); duke@435: duke@435: templen += sprintf(&resource_stages[templen], " stage_%s%-*s // %s\n", duke@435: resname, max_stage - (int)strlen(resname) + 1, duke@435: (i < pipeline->_rescount-1) ? "," : "", duke@435: pipeline->_reslist.name(i)); duke@435: } duke@435: duke@435: // See if the same string is in the table duke@435: int ndx = pipeline_res_stages.index(resource_stages); duke@435: duke@435: // No, add it to the table duke@435: if (ndx < 0) { duke@435: pipeline_res_stages.addName(resource_stages); duke@435: ndx = pipeline_res_stages.index(resource_stages); duke@435: duke@435: fprintf(fp_cpp, "static const enum machPipelineStages pipeline_res_stages_%03d[%d] = {\n%s};\n\n", duke@435: ndx+1, pipeline->_rescount, resource_stages); duke@435: } duke@435: else duke@435: delete [] resource_stages; duke@435: duke@435: delete [] res_stages; duke@435: duke@435: return (ndx); duke@435: } duke@435: duke@435: // Compute an index for an array in the pipeline_res_cycles_NNN arrays duke@435: static int pipeline_res_cycles_initializer( duke@435: FILE *fp_cpp, duke@435: PipelineForm *pipeline, duke@435: NameList &pipeline_res_cycles, duke@435: PipeClassForm *pipeclass) duke@435: { duke@435: const PipeClassResourceForm *piperesource; duke@435: int * res_cycles = new int [pipeline->_rescount]; duke@435: int i; duke@435: duke@435: for (i = 0; i < pipeline->_rescount; i++) duke@435: res_cycles[i] = 0; duke@435: duke@435: for (pipeclass->_resUsage.reset(); duke@435: (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) { duke@435: int used_mask = pipeline->_resdict[piperesource->_resource]->is_resource()->mask(); duke@435: for (i = 0; i < pipeline->_rescount; i++) duke@435: if ((1 << i) & used_mask) { duke@435: int cycles = piperesource->_cycles; duke@435: if (res_cycles[i] < cycles) duke@435: res_cycles[i] = cycles; duke@435: } duke@435: } duke@435: duke@435: // Pre-compute the string length duke@435: int templen; duke@435: int cyclelen = 0, commentlen = 0; duke@435: int max_cycles = 0; duke@435: char temp[32]; duke@435: duke@435: for (i = 0; i < pipeline->_rescount; i++) { duke@435: if (max_cycles < res_cycles[i]) duke@435: max_cycles = res_cycles[i]; duke@435: templen = sprintf(temp, "%d", res_cycles[i]); duke@435: if (cyclelen < templen) duke@435: cyclelen = templen; duke@435: commentlen += (int)strlen(pipeline->_reslist.name(i)); duke@435: } duke@435: duke@435: templen = 1 + commentlen + (cyclelen + 8) * pipeline->_rescount; duke@435: duke@435: // Allocate space for the resource list duke@435: char * resource_cycles = new char [templen]; duke@435: duke@435: templen = 0; duke@435: duke@435: for (i = 0; i < pipeline->_rescount; i++) { duke@435: templen += sprintf(&resource_cycles[templen], " %*d%c // %s\n", duke@435: cyclelen, res_cycles[i], (i < pipeline->_rescount-1) ? ',' : ' ', pipeline->_reslist.name(i)); duke@435: } duke@435: duke@435: // See if the same string is in the table duke@435: int ndx = pipeline_res_cycles.index(resource_cycles); duke@435: duke@435: // No, add it to the table duke@435: if (ndx < 0) { duke@435: pipeline_res_cycles.addName(resource_cycles); duke@435: ndx = pipeline_res_cycles.index(resource_cycles); duke@435: duke@435: fprintf(fp_cpp, "static const uint pipeline_res_cycles_%03d[%d] = {\n%s};\n\n", duke@435: ndx+1, pipeline->_rescount, resource_cycles); duke@435: } duke@435: else duke@435: delete [] resource_cycles; duke@435: duke@435: delete [] res_cycles; duke@435: duke@435: return (ndx); duke@435: } duke@435: duke@435: //typedef unsigned long long uint64_t; duke@435: duke@435: // Compute an index for an array in the pipeline_res_mask_NNN arrays duke@435: static int pipeline_res_mask_initializer( duke@435: FILE *fp_cpp, duke@435: PipelineForm *pipeline, duke@435: NameList &pipeline_res_mask, duke@435: NameList &pipeline_res_args, duke@435: PipeClassForm *pipeclass) duke@435: { duke@435: const PipeClassResourceForm *piperesource; duke@435: const uint rescount = pipeline->_rescount; duke@435: const uint maxcycleused = pipeline->_maxcycleused; duke@435: const uint cyclemasksize = (maxcycleused + 31) >> 5; duke@435: duke@435: int i, j; duke@435: int element_count = 0; duke@435: uint *res_mask = new uint [cyclemasksize]; duke@435: uint resources_used = 0; duke@435: uint resources_used_exclusively = 0; duke@435: duke@435: for (pipeclass->_resUsage.reset(); duke@435: (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) duke@435: element_count++; duke@435: duke@435: // Pre-compute the string length duke@435: int templen; duke@435: int commentlen = 0; duke@435: int max_cycles = 0; duke@435: duke@435: int cyclelen = ((maxcycleused + 3) >> 2); duke@435: int masklen = (rescount + 3) >> 2; duke@435: duke@435: int cycledigit = 0; duke@435: for (i = maxcycleused; i > 0; i /= 10) duke@435: cycledigit++; duke@435: duke@435: int maskdigit = 0; duke@435: for (i = rescount; i > 0; i /= 10) duke@435: maskdigit++; duke@435: duke@435: static const char * pipeline_use_cycle_mask = "Pipeline_Use_Cycle_Mask"; duke@435: static const char * pipeline_use_element = "Pipeline_Use_Element"; duke@435: duke@435: templen = 1 + duke@435: (int)(strlen(pipeline_use_cycle_mask) + (int)strlen(pipeline_use_element) + duke@435: (cyclemasksize * 12) + masklen + (cycledigit * 2) + 30) * element_count; duke@435: duke@435: // Allocate space for the resource list duke@435: char * resource_mask = new char [templen]; duke@435: char * last_comma = NULL; duke@435: duke@435: templen = 0; duke@435: duke@435: for (pipeclass->_resUsage.reset(); duke@435: (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) { duke@435: int used_mask = pipeline->_resdict[piperesource->_resource]->is_resource()->mask(); duke@435: duke@435: if (!used_mask) duke@435: fprintf(stderr, "*** used_mask is 0 ***\n"); duke@435: duke@435: resources_used |= used_mask; duke@435: duke@435: uint lb, ub; duke@435: duke@435: for (lb = 0; (used_mask & (1 << lb)) == 0; lb++); duke@435: for (ub = 31; (used_mask & (1 << ub)) == 0; ub--); duke@435: duke@435: if (lb == ub) duke@435: resources_used_exclusively |= used_mask; duke@435: duke@435: int formatlen = duke@435: sprintf(&resource_mask[templen], " %s(0x%0*x, %*d, %*d, %s %s(", duke@435: pipeline_use_element, duke@435: masklen, used_mask, duke@435: cycledigit, lb, cycledigit, ub, duke@435: ((used_mask & (used_mask-1)) != 0) ? "true, " : "false,", duke@435: pipeline_use_cycle_mask); duke@435: duke@435: templen += formatlen; duke@435: duke@435: memset(res_mask, 0, cyclemasksize * sizeof(uint)); duke@435: duke@435: int cycles = piperesource->_cycles; duke@435: uint stage = pipeline->_stages.index(piperesource->_stage); duke@435: uint upper_limit = stage+cycles-1; duke@435: uint lower_limit = stage-1; duke@435: uint upper_idx = upper_limit >> 5; duke@435: uint lower_idx = lower_limit >> 5; duke@435: uint upper_position = upper_limit & 0x1f; duke@435: uint lower_position = lower_limit & 0x1f; duke@435: duke@435: uint mask = (((uint)1) << upper_position) - 1; duke@435: duke@435: while ( upper_idx > lower_idx ) { duke@435: res_mask[upper_idx--] |= mask; duke@435: mask = (uint)-1; duke@435: } duke@435: duke@435: mask -= (((uint)1) << lower_position) - 1; duke@435: res_mask[upper_idx] |= mask; duke@435: duke@435: for (j = cyclemasksize-1; j >= 0; j--) { duke@435: formatlen = duke@435: sprintf(&resource_mask[templen], "0x%08x%s", res_mask[j], j > 0 ? ", " : ""); duke@435: templen += formatlen; duke@435: } duke@435: duke@435: resource_mask[templen++] = ')'; duke@435: resource_mask[templen++] = ')'; duke@435: last_comma = &resource_mask[templen]; duke@435: resource_mask[templen++] = ','; duke@435: resource_mask[templen++] = '\n'; duke@435: } duke@435: duke@435: resource_mask[templen] = 0; duke@435: if (last_comma) duke@435: last_comma[0] = ' '; duke@435: duke@435: // See if the same string is in the table duke@435: int ndx = pipeline_res_mask.index(resource_mask); duke@435: duke@435: // No, add it to the table duke@435: if (ndx < 0) { duke@435: pipeline_res_mask.addName(resource_mask); duke@435: ndx = pipeline_res_mask.index(resource_mask); duke@435: duke@435: if (strlen(resource_mask) > 0) duke@435: fprintf(fp_cpp, "static const Pipeline_Use_Element pipeline_res_mask_%03d[%d] = {\n%s};\n\n", duke@435: ndx+1, element_count, resource_mask); duke@435: duke@435: char * args = new char [9 + 2*masklen + maskdigit]; duke@435: duke@435: sprintf(args, "0x%0*x, 0x%0*x, %*d", duke@435: masklen, resources_used, duke@435: masklen, resources_used_exclusively, duke@435: maskdigit, element_count); duke@435: duke@435: pipeline_res_args.addName(args); duke@435: } duke@435: else duke@435: delete [] resource_mask; duke@435: duke@435: delete [] res_mask; duke@435: //delete [] res_masks; duke@435: duke@435: return (ndx); duke@435: } duke@435: duke@435: void ArchDesc::build_pipe_classes(FILE *fp_cpp) { duke@435: const char *classname; duke@435: const char *resourcename; duke@435: int resourcenamelen = 0; duke@435: NameList pipeline_reads; duke@435: NameList pipeline_res_stages; duke@435: NameList pipeline_res_cycles; duke@435: NameList pipeline_res_masks; duke@435: NameList pipeline_res_args; duke@435: const int default_latency = 1; duke@435: const int non_operand_latency = 0; duke@435: const int node_latency = 0; duke@435: duke@435: if (!_pipeline) { duke@435: fprintf(fp_cpp, "uint Node::latency(uint i) const {\n"); duke@435: fprintf(fp_cpp, " // assert(false, \"pipeline functionality is not defined\");\n"); duke@435: fprintf(fp_cpp, " return %d;\n", non_operand_latency); duke@435: fprintf(fp_cpp, "}\n"); duke@435: return; duke@435: } duke@435: duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "//------------------Pipeline Methods-----------------------------------------\n"); duke@435: fprintf(fp_cpp, "#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, "const char * Pipeline::stageName(uint s) {\n"); duke@435: fprintf(fp_cpp, " static const char * const _stage_names[] = {\n"); duke@435: fprintf(fp_cpp, " \"undefined\""); duke@435: duke@435: for (int s = 0; s < _pipeline->_stagecnt; s++) duke@435: fprintf(fp_cpp, ", \"%s\"", _pipeline->_stages.name(s)); duke@435: duke@435: fprintf(fp_cpp, "\n };\n\n"); duke@435: fprintf(fp_cpp, " return (s <= %d ? _stage_names[s] : \"???\");\n", duke@435: _pipeline->_stagecnt); duke@435: fprintf(fp_cpp, "}\n"); duke@435: fprintf(fp_cpp, "#endif\n\n"); duke@435: duke@435: fprintf(fp_cpp, "uint Pipeline::functional_unit_latency(uint start, const Pipeline *pred) const {\n"); duke@435: fprintf(fp_cpp, " // See if the functional units overlap\n"); duke@435: #if 0 duke@435: fprintf(fp_cpp, "\n#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, " if (TraceOptoOutput) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"# functional_unit_latency: start == %%d, this->exclusively == 0x%%03x, pred->exclusively == 0x%%03x\\n\", start, resourcesUsedExclusively(), pred->resourcesUsedExclusively());\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "#endif\n\n"); duke@435: #endif duke@435: fprintf(fp_cpp, " uint mask = resourcesUsedExclusively() & pred->resourcesUsedExclusively();\n"); duke@435: fprintf(fp_cpp, " if (mask == 0)\n return (start);\n\n"); duke@435: #if 0 duke@435: fprintf(fp_cpp, "\n#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, " if (TraceOptoOutput) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"# functional_unit_latency: mask == 0x%%x\\n\", mask);\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "#endif\n\n"); duke@435: #endif duke@435: fprintf(fp_cpp, " for (uint i = 0; i < pred->resourceUseCount(); i++) {\n"); duke@435: fprintf(fp_cpp, " const Pipeline_Use_Element *predUse = pred->resourceUseElement(i);\n"); duke@435: fprintf(fp_cpp, " if (predUse->multiple())\n"); duke@435: fprintf(fp_cpp, " continue;\n\n"); duke@435: fprintf(fp_cpp, " for (uint j = 0; j < resourceUseCount(); j++) {\n"); duke@435: fprintf(fp_cpp, " const Pipeline_Use_Element *currUse = resourceUseElement(j);\n"); duke@435: fprintf(fp_cpp, " if (currUse->multiple())\n"); duke@435: fprintf(fp_cpp, " continue;\n\n"); duke@435: fprintf(fp_cpp, " if (predUse->used() & currUse->used()) {\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask x = predUse->mask();\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask y = currUse->mask();\n\n"); duke@435: fprintf(fp_cpp, " for ( y <<= start; x.overlaps(y); start++ )\n"); duke@435: fprintf(fp_cpp, " y <<= 1;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n\n"); duke@435: fprintf(fp_cpp, " // There is the potential for overlap\n"); duke@435: fprintf(fp_cpp, " return (start);\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: fprintf(fp_cpp, "// The following two routines assume that the root Pipeline_Use entity\n"); duke@435: fprintf(fp_cpp, "// consists of exactly 1 element for each functional unit\n"); duke@435: fprintf(fp_cpp, "// start is relative to the current cycle; used for latency-based info\n"); duke@435: fprintf(fp_cpp, "uint Pipeline_Use::full_latency(uint delay, const Pipeline_Use &pred) const {\n"); duke@435: fprintf(fp_cpp, " for (uint i = 0; i < pred._count; i++) {\n"); duke@435: fprintf(fp_cpp, " const Pipeline_Use_Element *predUse = pred.element(i);\n"); duke@435: fprintf(fp_cpp, " if (predUse->_multiple) {\n"); duke@435: fprintf(fp_cpp, " uint min_delay = %d;\n", duke@435: _pipeline->_maxcycleused+1); duke@435: fprintf(fp_cpp, " // Multiple possible functional units, choose first unused one\n"); duke@435: fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n"); duke@435: fprintf(fp_cpp, " const Pipeline_Use_Element *currUse = element(j);\n"); duke@435: fprintf(fp_cpp, " uint curr_delay = delay;\n"); duke@435: fprintf(fp_cpp, " if (predUse->_used & currUse->_used) {\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask x = predUse->_mask;\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask y = currUse->_mask;\n\n"); duke@435: fprintf(fp_cpp, " for ( y <<= curr_delay; x.overlaps(y); curr_delay++ )\n"); duke@435: fprintf(fp_cpp, " y <<= 1;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " if (min_delay > curr_delay)\n min_delay = curr_delay;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " if (delay < min_delay)\n delay = min_delay;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " else {\n"); duke@435: fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n"); duke@435: fprintf(fp_cpp, " const Pipeline_Use_Element *currUse = element(j);\n"); duke@435: fprintf(fp_cpp, " if (predUse->_used & currUse->_used) {\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask x = predUse->_mask;\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask y = currUse->_mask;\n\n"); duke@435: fprintf(fp_cpp, " for ( y <<= delay; x.overlaps(y); delay++ )\n"); duke@435: fprintf(fp_cpp, " y <<= 1;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n\n"); duke@435: fprintf(fp_cpp, " return (delay);\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: fprintf(fp_cpp, "void Pipeline_Use::add_usage(const Pipeline_Use &pred) {\n"); duke@435: fprintf(fp_cpp, " for (uint i = 0; i < pred._count; i++) {\n"); duke@435: fprintf(fp_cpp, " const Pipeline_Use_Element *predUse = pred.element(i);\n"); duke@435: fprintf(fp_cpp, " if (predUse->_multiple) {\n"); duke@435: fprintf(fp_cpp, " // Multiple possible functional units, choose first unused one\n"); duke@435: fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Element *currUse = element(j);\n"); duke@435: fprintf(fp_cpp, " if ( !predUse->_mask.overlaps(currUse->_mask) ) {\n"); duke@435: fprintf(fp_cpp, " currUse->_used |= (1 << j);\n"); duke@435: fprintf(fp_cpp, " _resources_used |= (1 << j);\n"); duke@435: fprintf(fp_cpp, " currUse->_mask.Or(predUse->_mask);\n"); duke@435: fprintf(fp_cpp, " break;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " else {\n"); duke@435: fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n"); duke@435: fprintf(fp_cpp, " Pipeline_Use_Element *currUse = element(j);\n"); duke@435: fprintf(fp_cpp, " currUse->_used |= (1 << j);\n"); duke@435: fprintf(fp_cpp, " _resources_used |= (1 << j);\n"); duke@435: fprintf(fp_cpp, " currUse->_mask.Or(predUse->_mask);\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: duke@435: fprintf(fp_cpp, "uint Pipeline::operand_latency(uint opnd, const Pipeline *pred) const {\n"); duke@435: fprintf(fp_cpp, " int const default_latency = 1;\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: #if 0 duke@435: fprintf(fp_cpp, "#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, " if (TraceOptoOutput) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"# operand_latency(%%d), _read_stage_count = %%d\\n\", opnd, _read_stage_count);\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "#endif\n\n"); duke@435: #endif duke@435: fprintf(fp_cpp, " assert(this, \"NULL pipeline info\")\n"); duke@435: fprintf(fp_cpp, " assert(pred, \"NULL predecessor pipline info\")\n\n"); duke@435: fprintf(fp_cpp, " if (pred->hasFixedLatency())\n return (pred->fixedLatency());\n\n"); duke@435: fprintf(fp_cpp, " // If this is not an operand, then assume a dependence with 0 latency\n"); duke@435: fprintf(fp_cpp, " if (opnd > _read_stage_count)\n return (0);\n\n"); duke@435: fprintf(fp_cpp, " uint writeStage = pred->_write_stage;\n"); duke@435: fprintf(fp_cpp, " uint readStage = _read_stages[opnd-1];\n"); duke@435: #if 0 duke@435: fprintf(fp_cpp, "\n#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, " if (TraceOptoOutput) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"# operand_latency: writeStage=%%s readStage=%%s, opnd=%%d\\n\", stageName(writeStage), stageName(readStage), opnd);\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "#endif\n\n"); duke@435: #endif duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, " if (writeStage == stage_undefined || readStage == stage_undefined)\n"); duke@435: fprintf(fp_cpp, " return (default_latency);\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, " int delta = writeStage - readStage;\n"); duke@435: fprintf(fp_cpp, " if (delta < 0) delta = 0;\n\n"); duke@435: #if 0 duke@435: fprintf(fp_cpp, "\n#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, " if (TraceOptoOutput) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"# operand_latency: delta=%%d\\n\", delta);\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "#endif\n\n"); duke@435: #endif duke@435: fprintf(fp_cpp, " return (delta);\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: duke@435: if (!_pipeline) duke@435: /* Do Nothing */; duke@435: duke@435: else if (_pipeline->_maxcycleused <= duke@435: #ifdef SPARC duke@435: 64 duke@435: #else duke@435: 32 duke@435: #endif duke@435: ) { duke@435: fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator&(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n"); duke@435: fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask(in1._mask & in2._mask);\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator|(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n"); duke@435: fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask(in1._mask | in2._mask);\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: } duke@435: else { duke@435: uint l; duke@435: uint masklen = (_pipeline->_maxcycleused + 31) >> 5; duke@435: fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator&(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n"); duke@435: fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask("); duke@435: for (l = 1; l <= masklen; l++) duke@435: fprintf(fp_cpp, "in1._mask%d & in2._mask%d%s\n", l, l, l < masklen ? ", " : ""); duke@435: fprintf(fp_cpp, ");\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator|(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n"); duke@435: fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask("); duke@435: for (l = 1; l <= masklen; l++) duke@435: fprintf(fp_cpp, "in1._mask%d | in2._mask%d%s", l, l, l < masklen ? ", " : ""); duke@435: fprintf(fp_cpp, ");\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: fprintf(fp_cpp, "void Pipeline_Use_Cycle_Mask::Or(const Pipeline_Use_Cycle_Mask &in2) {\n "); duke@435: for (l = 1; l <= masklen; l++) duke@435: fprintf(fp_cpp, " _mask%d |= in2._mask%d;", l, l); duke@435: fprintf(fp_cpp, "\n}\n\n"); duke@435: } duke@435: duke@435: /* Get the length of all the resource names */ duke@435: for (_pipeline->_reslist.reset(), resourcenamelen = 0; duke@435: (resourcename = _pipeline->_reslist.iter()) != NULL; duke@435: resourcenamelen += (int)strlen(resourcename)); duke@435: duke@435: // Create the pipeline class description duke@435: duke@435: fprintf(fp_cpp, "static const Pipeline pipeline_class_Zero_Instructions(0, 0, true, 0, 0, false, false, false, false, NULL, NULL, NULL, Pipeline_Use(0, 0, 0, NULL));\n\n"); duke@435: fprintf(fp_cpp, "static const Pipeline pipeline_class_Unknown_Instructions(0, 0, true, 0, 0, false, true, true, false, NULL, NULL, NULL, Pipeline_Use(0, 0, 0, NULL));\n\n"); duke@435: duke@435: fprintf(fp_cpp, "const Pipeline_Use_Element Pipeline_Use::elaborated_elements[%d] = {\n", _pipeline->_rescount); duke@435: for (int i1 = 0; i1 < _pipeline->_rescount; i1++) { duke@435: fprintf(fp_cpp, " Pipeline_Use_Element(0, %d, %d, false, Pipeline_Use_Cycle_Mask(", i1, i1); duke@435: uint masklen = (_pipeline->_maxcycleused + 31) >> 5; duke@435: for (int i2 = masklen-1; i2 >= 0; i2--) duke@435: fprintf(fp_cpp, "0%s", i2 > 0 ? ", " : ""); duke@435: fprintf(fp_cpp, "))%s\n", i1 < (_pipeline->_rescount-1) ? "," : ""); duke@435: } duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: duke@435: fprintf(fp_cpp, "const Pipeline_Use Pipeline_Use::elaborated_use(0, 0, %d, (Pipeline_Use_Element *)&elaborated_elements[0]);\n\n", duke@435: _pipeline->_rescount); duke@435: duke@435: for (_pipeline->_classlist.reset(); (classname = _pipeline->_classlist.iter()) != NULL; ) { duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "// Pipeline Class \"%s\"\n", classname); duke@435: PipeClassForm *pipeclass = _pipeline->_classdict[classname]->is_pipeclass(); duke@435: int maxWriteStage = -1; duke@435: int maxMoreInstrs = 0; duke@435: int paramcount = 0; duke@435: int i = 0; duke@435: const char *paramname; duke@435: int resource_count = (_pipeline->_rescount + 3) >> 2; duke@435: duke@435: // Scan the operands, looking for last output stage and number of inputs duke@435: for (pipeclass->_parameters.reset(); (paramname = pipeclass->_parameters.iter()) != NULL; ) { duke@435: const PipeClassOperandForm *pipeopnd = duke@435: (const PipeClassOperandForm *)pipeclass->_localUsage[paramname]; duke@435: if (pipeopnd) { duke@435: if (pipeopnd->_iswrite) { duke@435: int stagenum = _pipeline->_stages.index(pipeopnd->_stage); duke@435: int moreinsts = pipeopnd->_more_instrs; duke@435: if ((maxWriteStage+maxMoreInstrs) < (stagenum+moreinsts)) { duke@435: maxWriteStage = stagenum; duke@435: maxMoreInstrs = moreinsts; duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (i++ > 0 || (pipeopnd && !pipeopnd->isWrite())) duke@435: paramcount++; duke@435: } duke@435: duke@435: // Create the list of stages for the operands that are read duke@435: // Note that we will build a NameList to reduce the number of copies duke@435: duke@435: int pipeline_reads_index = pipeline_reads_initializer(fp_cpp, pipeline_reads, pipeclass); duke@435: duke@435: int pipeline_res_stages_index = pipeline_res_stages_initializer( duke@435: fp_cpp, _pipeline, pipeline_res_stages, pipeclass); duke@435: duke@435: int pipeline_res_cycles_index = pipeline_res_cycles_initializer( duke@435: fp_cpp, _pipeline, pipeline_res_cycles, pipeclass); duke@435: duke@435: int pipeline_res_mask_index = pipeline_res_mask_initializer( duke@435: fp_cpp, _pipeline, pipeline_res_masks, pipeline_res_args, pipeclass); duke@435: duke@435: #if 0 duke@435: // Process the Resources duke@435: const PipeClassResourceForm *piperesource; duke@435: duke@435: unsigned resources_used = 0; duke@435: unsigned exclusive_resources_used = 0; duke@435: unsigned resource_groups = 0; duke@435: for (pipeclass->_resUsage.reset(); duke@435: (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) { duke@435: int used_mask = _pipeline->_resdict[piperesource->_resource]->is_resource()->mask(); duke@435: if (used_mask) duke@435: resource_groups++; duke@435: resources_used |= used_mask; duke@435: if ((used_mask & (used_mask-1)) == 0) duke@435: exclusive_resources_used |= used_mask; duke@435: } duke@435: duke@435: if (resource_groups > 0) { duke@435: fprintf(fp_cpp, "static const uint pipeline_res_or_masks_%03d[%d] = {", duke@435: pipeclass->_num, resource_groups); duke@435: for (pipeclass->_resUsage.reset(), i = 1; duke@435: (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; duke@435: i++ ) { duke@435: int used_mask = _pipeline->_resdict[piperesource->_resource]->is_resource()->mask(); duke@435: if (used_mask) { duke@435: fprintf(fp_cpp, " 0x%0*x%c", resource_count, used_mask, i < (int)resource_groups ? ',' : ' '); duke@435: } duke@435: } duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: } duke@435: #endif duke@435: duke@435: // Create the pipeline class description duke@435: fprintf(fp_cpp, "static const Pipeline pipeline_class_%03d(", duke@435: pipeclass->_num); duke@435: if (maxWriteStage < 0) duke@435: fprintf(fp_cpp, "(uint)stage_undefined"); duke@435: else if (maxMoreInstrs == 0) duke@435: fprintf(fp_cpp, "(uint)stage_%s", _pipeline->_stages.name(maxWriteStage)); duke@435: else duke@435: fprintf(fp_cpp, "((uint)stage_%s)+%d", _pipeline->_stages.name(maxWriteStage), maxMoreInstrs); duke@435: fprintf(fp_cpp, ", %d, %s, %d, %d, %s, %s, %s, %s,\n", duke@435: paramcount, duke@435: pipeclass->hasFixedLatency() ? "true" : "false", duke@435: pipeclass->fixedLatency(), duke@435: pipeclass->InstructionCount(), duke@435: pipeclass->hasBranchDelay() ? "true" : "false", duke@435: pipeclass->hasMultipleBundles() ? "true" : "false", duke@435: pipeclass->forceSerialization() ? "true" : "false", duke@435: pipeclass->mayHaveNoCode() ? "true" : "false" ); duke@435: if (paramcount > 0) { duke@435: fprintf(fp_cpp, "\n (enum machPipelineStages * const) pipeline_reads_%03d,\n ", duke@435: pipeline_reads_index+1); duke@435: } duke@435: else duke@435: fprintf(fp_cpp, " NULL,"); duke@435: fprintf(fp_cpp, " (enum machPipelineStages * const) pipeline_res_stages_%03d,\n", duke@435: pipeline_res_stages_index+1); duke@435: fprintf(fp_cpp, " (uint * const) pipeline_res_cycles_%03d,\n", duke@435: pipeline_res_cycles_index+1); duke@435: fprintf(fp_cpp, " Pipeline_Use(%s, (Pipeline_Use_Element *)", duke@435: pipeline_res_args.name(pipeline_res_mask_index)); duke@435: if (strlen(pipeline_res_masks.name(pipeline_res_mask_index)) > 0) duke@435: fprintf(fp_cpp, "&pipeline_res_mask_%03d[0]", duke@435: pipeline_res_mask_index+1); duke@435: else duke@435: fprintf(fp_cpp, "NULL"); duke@435: fprintf(fp_cpp, "));\n"); duke@435: } duke@435: duke@435: // Generate the Node::latency method if _pipeline defined duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "//------------------Inter-Instruction Latency--------------------------------\n"); duke@435: fprintf(fp_cpp, "uint Node::latency(uint i) {\n"); duke@435: if (_pipeline) { duke@435: #if 0 duke@435: fprintf(fp_cpp, "#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, " if (TraceOptoOutput) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"# %%4d->latency(%%d)\\n\", _idx, i);\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "#endif\n"); duke@435: #endif duke@435: fprintf(fp_cpp, " uint j;\n"); duke@435: fprintf(fp_cpp, " // verify in legal range for inputs\n"); duke@435: fprintf(fp_cpp, " assert(i < len(), \"index not in range\");\n\n"); duke@435: fprintf(fp_cpp, " // verify input is not null\n"); duke@435: fprintf(fp_cpp, " Node *pred = in(i);\n"); duke@435: fprintf(fp_cpp, " if (!pred)\n return %d;\n\n", duke@435: non_operand_latency); duke@435: fprintf(fp_cpp, " if (pred->is_Proj())\n pred = pred->in(0);\n\n"); duke@435: fprintf(fp_cpp, " // if either node does not have pipeline info, use default\n"); duke@435: fprintf(fp_cpp, " const Pipeline *predpipe = pred->pipeline();\n"); duke@435: fprintf(fp_cpp, " assert(predpipe, \"no predecessor pipeline info\");\n\n"); duke@435: fprintf(fp_cpp, " if (predpipe->hasFixedLatency())\n return predpipe->fixedLatency();\n\n"); duke@435: fprintf(fp_cpp, " const Pipeline *currpipe = pipeline();\n"); duke@435: fprintf(fp_cpp, " assert(currpipe, \"no pipeline info\");\n\n"); duke@435: fprintf(fp_cpp, " if (!is_Mach())\n return %d;\n\n", duke@435: node_latency); duke@435: fprintf(fp_cpp, " const MachNode *m = as_Mach();\n"); duke@435: fprintf(fp_cpp, " j = m->oper_input_base();\n"); duke@435: fprintf(fp_cpp, " if (i < j)\n return currpipe->functional_unit_latency(%d, predpipe);\n\n", duke@435: non_operand_latency); duke@435: fprintf(fp_cpp, " // determine which operand this is in\n"); duke@435: fprintf(fp_cpp, " uint n = m->num_opnds();\n"); duke@435: fprintf(fp_cpp, " int delta = %d;\n\n", duke@435: non_operand_latency); duke@435: fprintf(fp_cpp, " uint k;\n"); duke@435: fprintf(fp_cpp, " for (k = 1; k < n; k++) {\n"); duke@435: fprintf(fp_cpp, " j += m->_opnds[k]->num_edges();\n"); duke@435: fprintf(fp_cpp, " if (i < j)\n"); duke@435: fprintf(fp_cpp, " break;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, " if (k < n)\n"); duke@435: fprintf(fp_cpp, " delta = currpipe->operand_latency(k,predpipe);\n\n"); duke@435: fprintf(fp_cpp, " return currpipe->functional_unit_latency(delta, predpipe);\n"); duke@435: } duke@435: else { duke@435: fprintf(fp_cpp, " // assert(false, \"pipeline functionality is not defined\");\n"); duke@435: fprintf(fp_cpp, " return %d;\n", duke@435: non_operand_latency); duke@435: } duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: duke@435: // Output the list of nop nodes duke@435: fprintf(fp_cpp, "// Descriptions for emitting different functional unit nops\n"); duke@435: const char *nop; duke@435: int nopcnt = 0; duke@435: for ( _pipeline->_noplist.reset(); (nop = _pipeline->_noplist.iter()) != NULL; nopcnt++ ); duke@435: duke@435: fprintf(fp_cpp, "void Bundle::initialize_nops(MachNode * nop_list[%d], Compile *C) {\n", nopcnt); duke@435: int i = 0; duke@435: for ( _pipeline->_noplist.reset(); (nop = _pipeline->_noplist.iter()) != NULL; i++ ) { duke@435: fprintf(fp_cpp, " nop_list[%d] = (MachNode *) new (C) %sNode();\n", i, nop); duke@435: } duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: fprintf(fp_cpp, "#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, "void Bundle::dump() const {\n"); duke@435: fprintf(fp_cpp, " static const char * bundle_flags[] = {\n"); duke@435: fprintf(fp_cpp, " \"\",\n"); duke@435: fprintf(fp_cpp, " \"use nop delay\",\n"); duke@435: fprintf(fp_cpp, " \"use unconditional delay\",\n"); duke@435: fprintf(fp_cpp, " \"use conditional delay\",\n"); duke@435: fprintf(fp_cpp, " \"used in conditional delay\",\n"); duke@435: fprintf(fp_cpp, " \"used in unconditional delay\",\n"); duke@435: fprintf(fp_cpp, " \"used in all conditional delays\",\n"); duke@435: fprintf(fp_cpp, " };\n\n"); duke@435: duke@435: fprintf(fp_cpp, " static const char *resource_names[%d] = {", _pipeline->_rescount); duke@435: for (i = 0; i < _pipeline->_rescount; i++) duke@435: fprintf(fp_cpp, " \"%s\"%c", _pipeline->_reslist.name(i), i < _pipeline->_rescount-1 ? ',' : ' '); duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: duke@435: // See if the same string is in the table duke@435: fprintf(fp_cpp, " bool needs_comma = false;\n\n"); duke@435: fprintf(fp_cpp, " if (_flags) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"%%s\", bundle_flags[_flags]);\n"); duke@435: fprintf(fp_cpp, " needs_comma = true;\n"); duke@435: fprintf(fp_cpp, " };\n"); duke@435: fprintf(fp_cpp, " if (instr_count()) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"%%s%%d instr%%s\", needs_comma ? \", \" : \"\", instr_count(), instr_count() != 1 ? \"s\" : \"\");\n"); duke@435: fprintf(fp_cpp, " needs_comma = true;\n"); duke@435: fprintf(fp_cpp, " };\n"); duke@435: fprintf(fp_cpp, " uint r = resources_used();\n"); duke@435: fprintf(fp_cpp, " if (r) {\n"); duke@435: fprintf(fp_cpp, " tty->print(\"%%sresource%%s:\", needs_comma ? \", \" : \"\", (r & (r-1)) != 0 ? \"s\" : \"\");\n"); duke@435: fprintf(fp_cpp, " for (uint i = 0; i < %d; i++)\n", _pipeline->_rescount); duke@435: fprintf(fp_cpp, " if ((r & (1 << i)) != 0)\n"); duke@435: fprintf(fp_cpp, " tty->print(\" %%s\", resource_names[i]);\n"); duke@435: fprintf(fp_cpp, " needs_comma = true;\n"); duke@435: fprintf(fp_cpp, " };\n"); duke@435: fprintf(fp_cpp, " tty->print(\"\\n\");\n"); duke@435: fprintf(fp_cpp, "}\n"); duke@435: fprintf(fp_cpp, "#endif\n"); duke@435: } duke@435: duke@435: // --------------------------------------------------------------------------- duke@435: //------------------------------Utilities to build Instruction Classes-------- duke@435: // --------------------------------------------------------------------------- duke@435: duke@435: static void defineOut_RegMask(FILE *fp, const char *node, const char *regMask) { duke@435: fprintf(fp,"const RegMask &%sNode::out_RegMask() const { return (%s); }\n", duke@435: node, regMask); duke@435: } duke@435: duke@435: // Scan the peepmatch and output a test for each instruction duke@435: static void check_peepmatch_instruction_tree(FILE *fp, PeepMatch *pmatch, PeepConstraint *pconstraint) { duke@435: intptr_t parent = -1; duke@435: intptr_t inst_position = 0; duke@435: const char *inst_name = NULL; duke@435: intptr_t input = 0; duke@435: fprintf(fp, " // Check instruction sub-tree\n"); duke@435: pmatch->reset(); duke@435: for( pmatch->next_instruction( parent, inst_position, inst_name, input ); duke@435: inst_name != NULL; duke@435: pmatch->next_instruction( parent, inst_position, inst_name, input ) ) { duke@435: // If this is not a placeholder duke@435: if( ! pmatch->is_placeholder() ) { duke@435: // Define temporaries 'inst#', based on parent and parent's input index duke@435: if( parent != -1 ) { // root was initialized duke@435: fprintf(fp, " inst%ld = inst%ld->in(%ld);\n", duke@435: inst_position, parent, input); duke@435: } duke@435: duke@435: // When not the root duke@435: // Test we have the correct instruction by comparing the rule duke@435: if( parent != -1 ) { duke@435: fprintf(fp, " matches = matches && ( inst%ld->rule() == %s_rule );", duke@435: inst_position, inst_name); duke@435: } duke@435: } else { duke@435: // Check that user did not try to constrain a placeholder duke@435: assert( ! pconstraint->constrains_instruction(inst_position), duke@435: "fatal(): Can not constrain a placeholder instruction"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: static void print_block_index(FILE *fp, intptr_t inst_position) { duke@435: assert( inst_position >= 0, "Instruction number less than zero"); duke@435: fprintf(fp, "block_index"); duke@435: if( inst_position != 0 ) { duke@435: fprintf(fp, " - %ld", inst_position); duke@435: } duke@435: } duke@435: duke@435: // Scan the peepmatch and output a test for each instruction duke@435: static void check_peepmatch_instruction_sequence(FILE *fp, PeepMatch *pmatch, PeepConstraint *pconstraint) { duke@435: intptr_t parent = -1; duke@435: intptr_t inst_position = 0; duke@435: const char *inst_name = NULL; duke@435: intptr_t input = 0; duke@435: fprintf(fp, " // Check instruction sub-tree\n"); duke@435: pmatch->reset(); duke@435: for( pmatch->next_instruction( parent, inst_position, inst_name, input ); duke@435: inst_name != NULL; duke@435: pmatch->next_instruction( parent, inst_position, inst_name, input ) ) { duke@435: // If this is not a placeholder duke@435: if( ! pmatch->is_placeholder() ) { duke@435: // Define temporaries 'inst#', based on parent and parent's input index duke@435: if( parent != -1 ) { // root was initialized duke@435: fprintf(fp, " // Identify previous instruction if inside this block\n"); duke@435: fprintf(fp, " if( "); duke@435: print_block_index(fp, inst_position); duke@435: fprintf(fp, " > 0 ) {\n Node *n = block->_nodes.at("); duke@435: print_block_index(fp, inst_position); duke@435: fprintf(fp, ");\n inst%ld = (n->is_Mach()) ? ", inst_position); duke@435: fprintf(fp, "n->as_Mach() : NULL;\n }\n"); duke@435: } duke@435: duke@435: // When not the root duke@435: // Test we have the correct instruction by comparing the rule. duke@435: if( parent != -1 ) { duke@435: fprintf(fp, " matches = matches && (inst%ld != NULL) && (inst%ld->rule() == %s_rule);\n", duke@435: inst_position, inst_position, inst_name); duke@435: } duke@435: } else { duke@435: // Check that user did not try to constrain a placeholder duke@435: assert( ! pconstraint->constrains_instruction(inst_position), duke@435: "fatal(): Can not constrain a placeholder instruction"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Build mapping for register indices, num_edges to input duke@435: static void build_instruction_index_mapping( FILE *fp, FormDict &globals, PeepMatch *pmatch ) { duke@435: intptr_t parent = -1; duke@435: intptr_t inst_position = 0; duke@435: const char *inst_name = NULL; duke@435: intptr_t input = 0; duke@435: fprintf(fp, " // Build map to register info\n"); duke@435: pmatch->reset(); duke@435: for( pmatch->next_instruction( parent, inst_position, inst_name, input ); duke@435: inst_name != NULL; duke@435: pmatch->next_instruction( parent, inst_position, inst_name, input ) ) { duke@435: // If this is not a placeholder duke@435: if( ! pmatch->is_placeholder() ) { duke@435: // Define temporaries 'inst#', based on self's inst_position duke@435: InstructForm *inst = globals[inst_name]->is_instruction(); duke@435: if( inst != NULL ) { duke@435: char inst_prefix[] = "instXXXX_"; duke@435: sprintf(inst_prefix, "inst%ld_", inst_position); duke@435: char receiver[] = "instXXXX->"; duke@435: sprintf(receiver, "inst%ld->", inst_position); duke@435: inst->index_temps( fp, globals, inst_prefix, receiver ); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Generate tests for the constraints duke@435: static void check_peepconstraints(FILE *fp, FormDict &globals, PeepMatch *pmatch, PeepConstraint *pconstraint) { duke@435: fprintf(fp, "\n"); duke@435: fprintf(fp, " // Check constraints on sub-tree-leaves\n"); duke@435: duke@435: // Build mapping from num_edges to local variables duke@435: build_instruction_index_mapping( fp, globals, pmatch ); duke@435: duke@435: // Build constraint tests duke@435: if( pconstraint != NULL ) { duke@435: fprintf(fp, " matches = matches &&"); duke@435: bool first_constraint = true; duke@435: while( pconstraint != NULL ) { duke@435: // indentation and connecting '&&' duke@435: const char *indentation = " "; duke@435: fprintf(fp, "\n%s%s", indentation, (!first_constraint ? "&& " : " ")); duke@435: duke@435: // Only have '==' relation implemented duke@435: if( strcmp(pconstraint->_relation,"==") != 0 ) { duke@435: assert( false, "Unimplemented()" ); duke@435: } duke@435: duke@435: // LEFT duke@435: intptr_t left_index = pconstraint->_left_inst; duke@435: const char *left_op = pconstraint->_left_op; duke@435: // Access info on the instructions whose operands are compared duke@435: InstructForm *inst_left = globals[pmatch->instruction_name(left_index)]->is_instruction(); duke@435: assert( inst_left, "Parser should guaranty this is an instruction"); duke@435: int left_op_base = inst_left->oper_input_base(globals); duke@435: // Access info on the operands being compared duke@435: int left_op_index = inst_left->operand_position(left_op, Component::USE); duke@435: if( left_op_index == -1 ) { duke@435: left_op_index = inst_left->operand_position(left_op, Component::DEF); duke@435: if( left_op_index == -1 ) { duke@435: left_op_index = inst_left->operand_position(left_op, Component::USE_DEF); duke@435: } duke@435: } duke@435: assert( left_op_index != NameList::Not_in_list, "Did not find operand in instruction"); duke@435: ComponentList components_left = inst_left->_components; duke@435: const char *left_comp_type = components_left.at(left_op_index)->_type; duke@435: OpClassForm *left_opclass = globals[left_comp_type]->is_opclass(); duke@435: Form::InterfaceType left_interface_type = left_opclass->interface_type(globals); duke@435: duke@435: duke@435: // RIGHT duke@435: int right_op_index = -1; duke@435: intptr_t right_index = pconstraint->_right_inst; duke@435: const char *right_op = pconstraint->_right_op; duke@435: if( right_index != -1 ) { // Match operand duke@435: // Access info on the instructions whose operands are compared duke@435: InstructForm *inst_right = globals[pmatch->instruction_name(right_index)]->is_instruction(); duke@435: assert( inst_right, "Parser should guaranty this is an instruction"); duke@435: int right_op_base = inst_right->oper_input_base(globals); duke@435: // Access info on the operands being compared duke@435: right_op_index = inst_right->operand_position(right_op, Component::USE); duke@435: if( right_op_index == -1 ) { duke@435: right_op_index = inst_right->operand_position(right_op, Component::DEF); duke@435: if( right_op_index == -1 ) { duke@435: right_op_index = inst_right->operand_position(right_op, Component::USE_DEF); duke@435: } duke@435: } duke@435: assert( right_op_index != NameList::Not_in_list, "Did not find operand in instruction"); duke@435: ComponentList components_right = inst_right->_components; duke@435: const char *right_comp_type = components_right.at(right_op_index)->_type; duke@435: OpClassForm *right_opclass = globals[right_comp_type]->is_opclass(); duke@435: Form::InterfaceType right_interface_type = right_opclass->interface_type(globals); duke@435: assert( right_interface_type == left_interface_type, "Both must be same interface"); duke@435: duke@435: } else { // Else match register duke@435: // assert( false, "should be a register" ); duke@435: } duke@435: duke@435: // duke@435: // Check for equivalence duke@435: // duke@435: // fprintf(fp, "phase->eqv( "); duke@435: // fprintf(fp, "inst%d->in(%d+%d) /* %s */, inst%d->in(%d+%d) /* %s */", duke@435: // left_index, left_op_base, left_op_index, left_op, duke@435: // right_index, right_op_base, right_op_index, right_op ); duke@435: // fprintf(fp, ")"); duke@435: // duke@435: switch( left_interface_type ) { duke@435: case Form::register_interface: { duke@435: // Check that they are allocated to the same register duke@435: // Need parameter for index position if not result operand duke@435: char left_reg_index[] = ",instXXXX_idxXXXX"; duke@435: if( left_op_index != 0 ) { duke@435: assert( (left_index <= 9999) && (left_op_index <= 9999), "exceed string size"); duke@435: // Must have index into operands duke@435: sprintf(left_reg_index,",inst%d_idx%d", left_index, left_op_index); duke@435: } else { duke@435: strcpy(left_reg_index, ""); duke@435: } duke@435: fprintf(fp, "(inst%d->_opnds[%d]->reg(ra_,inst%d%s) /* %d.%s */", duke@435: left_index, left_op_index, left_index, left_reg_index, left_index, left_op ); duke@435: fprintf(fp, " == "); duke@435: duke@435: if( right_index != -1 ) { duke@435: char right_reg_index[18] = ",instXXXX_idxXXXX"; duke@435: if( right_op_index != 0 ) { duke@435: assert( (right_index <= 9999) && (right_op_index <= 9999), "exceed string size"); duke@435: // Must have index into operands duke@435: sprintf(right_reg_index,",inst%d_idx%d", right_index, right_op_index); duke@435: } else { duke@435: strcpy(right_reg_index, ""); duke@435: } duke@435: fprintf(fp, "/* %d.%s */ inst%d->_opnds[%d]->reg(ra_,inst%d%s)", duke@435: right_index, right_op, right_index, right_op_index, right_index, right_reg_index ); duke@435: } else { duke@435: fprintf(fp, "%s_enc", right_op ); duke@435: } duke@435: fprintf(fp,")"); duke@435: break; duke@435: } duke@435: case Form::constant_interface: { duke@435: // Compare the '->constant()' values duke@435: fprintf(fp, "(inst%d->_opnds[%d]->constant() /* %d.%s */", duke@435: left_index, left_op_index, left_index, left_op ); duke@435: fprintf(fp, " == "); duke@435: fprintf(fp, "/* %d.%s */ inst%d->_opnds[%d]->constant())", duke@435: right_index, right_op, right_index, right_op_index ); duke@435: break; duke@435: } duke@435: case Form::memory_interface: { duke@435: // Compare 'base', 'index', 'scale', and 'disp' duke@435: // base duke@435: fprintf(fp, "( \n"); duke@435: fprintf(fp, " (inst%d->_opnds[%d]->base(ra_,inst%d,inst%d_idx%d) /* %d.%s$$base */", duke@435: left_index, left_op_index, left_index, left_index, left_op_index, left_index, left_op ); duke@435: fprintf(fp, " == "); duke@435: fprintf(fp, "/* %d.%s$$base */ inst%d->_opnds[%d]->base(ra_,inst%d,inst%d_idx%d)) &&\n", duke@435: right_index, right_op, right_index, right_op_index, right_index, right_index, right_op_index ); duke@435: // index duke@435: fprintf(fp, " (inst%d->_opnds[%d]->index(ra_,inst%d,inst%d_idx%d) /* %d.%s$$index */", duke@435: left_index, left_op_index, left_index, left_index, left_op_index, left_index, left_op ); duke@435: fprintf(fp, " == "); duke@435: fprintf(fp, "/* %d.%s$$index */ inst%d->_opnds[%d]->index(ra_,inst%d,inst%d_idx%d)) &&\n", duke@435: right_index, right_op, right_index, right_op_index, right_index, right_index, right_op_index ); duke@435: // scale duke@435: fprintf(fp, " (inst%d->_opnds[%d]->scale() /* %d.%s$$scale */", duke@435: left_index, left_op_index, left_index, left_op ); duke@435: fprintf(fp, " == "); duke@435: fprintf(fp, "/* %d.%s$$scale */ inst%d->_opnds[%d]->scale()) &&\n", duke@435: right_index, right_op, right_index, right_op_index ); duke@435: // disp duke@435: fprintf(fp, " (inst%d->_opnds[%d]->disp(ra_,inst%d,inst%d_idx%d) /* %d.%s$$disp */", duke@435: left_index, left_op_index, left_index, left_index, left_op_index, left_index, left_op ); duke@435: fprintf(fp, " == "); duke@435: fprintf(fp, "/* %d.%s$$disp */ inst%d->_opnds[%d]->disp(ra_,inst%d,inst%d_idx%d))\n", duke@435: right_index, right_op, right_index, right_op_index, right_index, right_index, right_op_index ); duke@435: fprintf(fp, ") \n"); duke@435: break; duke@435: } duke@435: case Form::conditional_interface: { duke@435: // Compare the condition code being tested duke@435: assert( false, "Unimplemented()" ); duke@435: break; duke@435: } duke@435: default: { duke@435: assert( false, "ShouldNotReachHere()" ); duke@435: break; duke@435: } duke@435: } duke@435: duke@435: // Advance to next constraint duke@435: pconstraint = pconstraint->next(); duke@435: first_constraint = false; duke@435: } duke@435: duke@435: fprintf(fp, ";\n"); duke@435: } duke@435: } duke@435: duke@435: // // EXPERIMENTAL -- TEMPORARY code duke@435: // static Form::DataType get_operand_type(FormDict &globals, InstructForm *instr, const char *op_name ) { duke@435: // int op_index = instr->operand_position(op_name, Component::USE); duke@435: // if( op_index == -1 ) { duke@435: // op_index = instr->operand_position(op_name, Component::DEF); duke@435: // if( op_index == -1 ) { duke@435: // op_index = instr->operand_position(op_name, Component::USE_DEF); duke@435: // } duke@435: // } duke@435: // assert( op_index != NameList::Not_in_list, "Did not find operand in instruction"); duke@435: // duke@435: // ComponentList components_right = instr->_components; duke@435: // char *right_comp_type = components_right.at(op_index)->_type; duke@435: // OpClassForm *right_opclass = globals[right_comp_type]->is_opclass(); duke@435: // Form::InterfaceType right_interface_type = right_opclass->interface_type(globals); duke@435: // duke@435: // return; duke@435: // } duke@435: duke@435: // Construct the new sub-tree duke@435: static void generate_peepreplace( FILE *fp, FormDict &globals, PeepMatch *pmatch, PeepConstraint *pconstraint, PeepReplace *preplace, int max_position ) { duke@435: fprintf(fp, " // IF instructions and constraints matched\n"); duke@435: fprintf(fp, " if( matches ) {\n"); duke@435: fprintf(fp, " // generate the new sub-tree\n"); duke@435: fprintf(fp, " assert( true, \"Debug stopping point\");\n"); duke@435: if( preplace != NULL ) { duke@435: // Get the root of the new sub-tree duke@435: const char *root_inst = NULL; duke@435: preplace->next_instruction(root_inst); duke@435: InstructForm *root_form = globals[root_inst]->is_instruction(); duke@435: assert( root_form != NULL, "Replacement instruction was not previously defined"); duke@435: fprintf(fp, " %sNode *root = new (C) %sNode();\n", root_inst, root_inst); duke@435: duke@435: intptr_t inst_num; duke@435: const char *op_name; duke@435: int opnds_index = 0; // define result operand duke@435: // Then install the use-operands for the new sub-tree duke@435: // preplace->reset(); // reset breaks iteration duke@435: for( preplace->next_operand( inst_num, op_name ); duke@435: op_name != NULL; duke@435: preplace->next_operand( inst_num, op_name ) ) { duke@435: InstructForm *inst_form; duke@435: inst_form = globals[pmatch->instruction_name(inst_num)]->is_instruction(); duke@435: assert( inst_form, "Parser should guaranty this is an instruction"); duke@435: int op_base = inst_form->oper_input_base(globals); duke@435: int inst_op_num = inst_form->operand_position(op_name, Component::USE); duke@435: if( inst_op_num == NameList::Not_in_list ) duke@435: inst_op_num = inst_form->operand_position(op_name, Component::USE_DEF); duke@435: assert( inst_op_num != NameList::Not_in_list, "Did not find operand as USE"); duke@435: // find the name of the OperandForm from the local name duke@435: const Form *form = inst_form->_localNames[op_name]; duke@435: OperandForm *op_form = form->is_operand(); duke@435: if( opnds_index == 0 ) { duke@435: // Initial setup of new instruction duke@435: fprintf(fp, " // ----- Initial setup -----\n"); duke@435: // duke@435: // Add control edge for this node duke@435: fprintf(fp, " root->add_req(_in[0]); // control edge\n"); duke@435: // Add unmatched edges from root of match tree duke@435: int op_base = root_form->oper_input_base(globals); duke@435: for( int unmatched_edge = 1; unmatched_edge < op_base; ++unmatched_edge ) { duke@435: fprintf(fp, " root->add_req(inst%ld->in(%d)); // unmatched ideal edge\n", duke@435: inst_num, unmatched_edge); duke@435: } duke@435: // If new instruction captures bottom type duke@435: if( root_form->captures_bottom_type() ) { duke@435: // Get bottom type from instruction whose result we are replacing duke@435: fprintf(fp, " root->_bottom_type = inst%ld->bottom_type();\n", inst_num); duke@435: } duke@435: // Define result register and result operand duke@435: fprintf(fp, " ra_->add_reference(root, inst%ld);\n", inst_num); duke@435: fprintf(fp, " ra_->set_oop (root, ra_->is_oop(inst%ld));\n", inst_num); duke@435: fprintf(fp, " ra_->set_pair(root->_idx, ra_->get_reg_second(inst%ld), ra_->get_reg_first(inst%ld));\n", inst_num, inst_num); duke@435: fprintf(fp, " root->_opnds[0] = inst%ld->_opnds[0]->clone(C); // result\n", inst_num); duke@435: fprintf(fp, " // ----- Done with initial setup -----\n"); duke@435: } else { duke@435: if( (op_form == NULL) || (op_form->is_base_constant(globals) == Form::none) ) { duke@435: // Do not have ideal edges for constants after matching duke@435: fprintf(fp, " for( unsigned x%d = inst%ld_idx%d; x%d < inst%ld_idx%d; x%d++ )\n", duke@435: inst_op_num, inst_num, inst_op_num, duke@435: inst_op_num, inst_num, inst_op_num+1, inst_op_num ); duke@435: fprintf(fp, " root->add_req( inst%ld->in(x%d) );\n", duke@435: inst_num, inst_op_num ); duke@435: } else { duke@435: fprintf(fp, " // no ideal edge for constants after matching\n"); duke@435: } duke@435: fprintf(fp, " root->_opnds[%d] = inst%ld->_opnds[%d]->clone(C);\n", duke@435: opnds_index, inst_num, inst_op_num ); duke@435: } duke@435: ++opnds_index; duke@435: } duke@435: }else { duke@435: // Replacing subtree with empty-tree duke@435: assert( false, "ShouldNotReachHere();"); duke@435: } duke@435: duke@435: // Return the new sub-tree duke@435: fprintf(fp, " deleted = %d;\n", max_position+1 /*zero to one based*/); duke@435: fprintf(fp, " return root; // return new root;\n"); duke@435: fprintf(fp, " }\n"); duke@435: } duke@435: duke@435: duke@435: // Define the Peephole method for an instruction node duke@435: void ArchDesc::definePeephole(FILE *fp, InstructForm *node) { duke@435: // Generate Peephole function header duke@435: fprintf(fp, "MachNode *%sNode::peephole( Block *block, int block_index, PhaseRegAlloc *ra_, int &deleted, Compile* C ) {\n", node->_ident); duke@435: fprintf(fp, " bool matches = true;\n"); duke@435: duke@435: // Identify the maximum instruction position, duke@435: // generate temporaries that hold current instruction duke@435: // duke@435: // MachNode *inst0 = NULL; duke@435: // ... duke@435: // MachNode *instMAX = NULL; duke@435: // duke@435: int max_position = 0; duke@435: Peephole *peep; duke@435: for( peep = node->peepholes(); peep != NULL; peep = peep->next() ) { duke@435: PeepMatch *pmatch = peep->match(); duke@435: assert( pmatch != NULL, "fatal(), missing peepmatch rule"); duke@435: if( max_position < pmatch->max_position() ) max_position = pmatch->max_position(); duke@435: } duke@435: for( int i = 0; i <= max_position; ++i ) { duke@435: if( i == 0 ) { duke@435: fprintf(fp, " MachNode *inst0 = this;\n", i); duke@435: } else { duke@435: fprintf(fp, " MachNode *inst%d = NULL;\n", i); duke@435: } duke@435: } duke@435: duke@435: // For each peephole rule in architecture description duke@435: // Construct a test for the desired instruction sub-tree duke@435: // then check the constraints duke@435: // If these match, Generate the new subtree duke@435: for( peep = node->peepholes(); peep != NULL; peep = peep->next() ) { duke@435: int peephole_number = peep->peephole_number(); duke@435: PeepMatch *pmatch = peep->match(); duke@435: PeepConstraint *pconstraint = peep->constraints(); duke@435: PeepReplace *preplace = peep->replacement(); duke@435: duke@435: // Root of this peephole is the current MachNode duke@435: assert( true, // %%name?%% strcmp( node->_ident, pmatch->name(0) ) == 0, duke@435: "root of PeepMatch does not match instruction"); duke@435: duke@435: // Make each peephole rule individually selectable duke@435: fprintf(fp, " if( (OptoPeepholeAt == -1) || (OptoPeepholeAt==%d) ) {\n", peephole_number); duke@435: fprintf(fp, " matches = true;\n"); duke@435: // Scan the peepmatch and output a test for each instruction duke@435: check_peepmatch_instruction_sequence( fp, pmatch, pconstraint ); duke@435: duke@435: // Check constraints and build replacement inside scope duke@435: fprintf(fp, " // If instruction subtree matches\n"); duke@435: fprintf(fp, " if( matches ) {\n"); duke@435: duke@435: // Generate tests for the constraints duke@435: check_peepconstraints( fp, _globalNames, pmatch, pconstraint ); duke@435: duke@435: // Construct the new sub-tree duke@435: generate_peepreplace( fp, _globalNames, pmatch, pconstraint, preplace, max_position ); duke@435: duke@435: // End of scope for this peephole's constraints duke@435: fprintf(fp, " }\n"); duke@435: // Closing brace '}' to make each peephole rule individually selectable duke@435: fprintf(fp, " } // end of peephole rule #%d\n", peephole_number); duke@435: fprintf(fp, "\n"); duke@435: } duke@435: duke@435: fprintf(fp, " return NULL; // No peephole rules matched\n"); duke@435: fprintf(fp, "}\n"); duke@435: fprintf(fp, "\n"); duke@435: } duke@435: duke@435: // Define the Expand method for an instruction node duke@435: void ArchDesc::defineExpand(FILE *fp, InstructForm *node) { duke@435: unsigned cnt = 0; // Count nodes we have expand into duke@435: unsigned i; duke@435: duke@435: // Generate Expand function header duke@435: fprintf(fp,"MachNode *%sNode::Expand(State *state, Node_List &proj_list) {\n", node->_ident); duke@435: fprintf(fp,"Compile* C = Compile::current();\n"); duke@435: // Generate expand code duke@435: if( node->expands() ) { duke@435: const char *opid; duke@435: int new_pos, exp_pos; duke@435: const char *new_id = NULL; duke@435: const Form *frm = NULL; duke@435: InstructForm *new_inst = NULL; duke@435: OperandForm *new_oper = NULL; duke@435: unsigned numo = node->num_opnds() + duke@435: node->_exprule->_newopers.count(); duke@435: duke@435: // If necessary, generate any operands created in expand rule duke@435: if (node->_exprule->_newopers.count()) { duke@435: for(node->_exprule->_newopers.reset(); duke@435: (new_id = node->_exprule->_newopers.iter()) != NULL; cnt++) { duke@435: frm = node->_localNames[new_id]; duke@435: assert(frm, "Invalid entry in new operands list of expand rule"); duke@435: new_oper = frm->is_operand(); duke@435: char *tmp = (char *)node->_exprule->_newopconst[new_id]; duke@435: if (tmp == NULL) { duke@435: fprintf(fp," MachOper *op%d = new (C) %sOper();\n", duke@435: cnt, new_oper->_ident); duke@435: } duke@435: else { duke@435: fprintf(fp," MachOper *op%d = new (C) %sOper(%s);\n", duke@435: cnt, new_oper->_ident, tmp); duke@435: } duke@435: } duke@435: } duke@435: cnt = 0; duke@435: // Generate the temps to use for DAG building duke@435: for(i = 0; i < numo; i++) { duke@435: if (i < node->num_opnds()) { duke@435: fprintf(fp," MachNode *tmp%d = this;\n", i); duke@435: } duke@435: else { duke@435: fprintf(fp," MachNode *tmp%d = NULL;\n", i); duke@435: } duke@435: } duke@435: // Build mapping from num_edges to local variables duke@435: fprintf(fp," unsigned num0 = 0;\n"); duke@435: for( i = 1; i < node->num_opnds(); i++ ) { duke@435: fprintf(fp," unsigned num%d = opnd_array(%d)->num_edges();\n",i,i); duke@435: } duke@435: duke@435: // Build a mapping from operand index to input edges duke@435: fprintf(fp," unsigned idx0 = oper_input_base();\n"); duke@435: for( i = 0; i < node->num_opnds(); i++ ) { duke@435: fprintf(fp," unsigned idx%d = idx%d + num%d;\n", duke@435: i+1,i,i); duke@435: } duke@435: duke@435: // Declare variable to hold root of expansion duke@435: fprintf(fp," MachNode *result = NULL;\n"); duke@435: duke@435: // Iterate over the instructions 'node' expands into duke@435: ExpandRule *expand = node->_exprule; duke@435: NameAndList *expand_instr = NULL; duke@435: for(expand->reset_instructions(); duke@435: (expand_instr = expand->iter_instructions()) != NULL; cnt++) { duke@435: new_id = expand_instr->name(); duke@435: duke@435: InstructForm* expand_instruction = (InstructForm*)globalAD->globalNames()[new_id]; duke@435: if (expand_instruction->has_temps()) { duke@435: globalAD->syntax_err(node->_linenum, "In %s: expand rules using instructs with TEMPs aren't supported: %s", duke@435: node->_ident, new_id); duke@435: } duke@435: duke@435: // Build the node for the instruction duke@435: fprintf(fp,"\n %sNode *n%d = new (C) %sNode();\n", new_id, cnt, new_id); duke@435: // Add control edge for this node duke@435: fprintf(fp," n%d->add_req(_in[0]);\n", cnt); duke@435: // Build the operand for the value this node defines. duke@435: Form *form = (Form*)_globalNames[new_id]; duke@435: assert( form, "'new_id' must be a defined form name"); duke@435: // Grab the InstructForm for the new instruction duke@435: new_inst = form->is_instruction(); duke@435: assert( new_inst, "'new_id' must be an instruction name"); duke@435: if( node->is_ideal_if() && new_inst->is_ideal_if() ) { duke@435: fprintf(fp, " ((MachIfNode*)n%d)->_prob = _prob;\n",cnt); duke@435: fprintf(fp, " ((MachIfNode*)n%d)->_fcnt = _fcnt;\n",cnt); duke@435: } duke@435: duke@435: if( node->is_ideal_fastlock() && new_inst->is_ideal_fastlock() ) { duke@435: fprintf(fp, " ((MachFastLockNode*)n%d)->_counters = _counters;\n",cnt); duke@435: } duke@435: duke@435: const char *resultOper = new_inst->reduce_result(); duke@435: fprintf(fp," n%d->set_opnd_array(0, state->MachOperGenerator( %s, C ));\n", duke@435: cnt, machOperEnum(resultOper)); duke@435: duke@435: // get the formal operand NameList duke@435: NameList *formal_lst = &new_inst->_parameters; duke@435: formal_lst->reset(); duke@435: duke@435: // Handle any memory operand duke@435: int memory_operand = new_inst->memory_operand(_globalNames); duke@435: if( memory_operand != InstructForm::NO_MEMORY_OPERAND ) { duke@435: int node_mem_op = node->memory_operand(_globalNames); duke@435: assert( node_mem_op != InstructForm::NO_MEMORY_OPERAND, duke@435: "expand rule member needs memory but top-level inst doesn't have any" ); duke@435: // Copy memory edge duke@435: fprintf(fp," n%d->add_req(_in[1]);\t// Add memory edge\n", cnt); duke@435: } duke@435: duke@435: // Iterate over the new instruction's operands duke@435: for( expand_instr->reset(); (opid = expand_instr->iter()) != NULL; ) { duke@435: // Use 'parameter' at current position in list of new instruction's formals duke@435: // instead of 'opid' when looking up info internal to new_inst duke@435: const char *parameter = formal_lst->iter(); duke@435: // Check for an operand which is created in the expand rule duke@435: if ((exp_pos = node->_exprule->_newopers.index(opid)) != -1) { duke@435: new_pos = new_inst->operand_position(parameter,Component::USE); duke@435: exp_pos += node->num_opnds(); duke@435: // If there is no use of the created operand, just skip it duke@435: if (new_pos != -1) { duke@435: //Copy the operand from the original made above duke@435: fprintf(fp," n%d->set_opnd_array(%d, op%d->clone(C)); // %s\n", duke@435: cnt, new_pos, exp_pos-node->num_opnds(), opid); duke@435: // Check for who defines this operand & add edge if needed duke@435: fprintf(fp," if(tmp%d != NULL)\n", exp_pos); duke@435: fprintf(fp," n%d->add_req(tmp%d);\n", cnt, exp_pos); duke@435: } duke@435: } duke@435: else { duke@435: // Use operand name to get an index into instruction component list duke@435: // ins = (InstructForm *) _globalNames[new_id]; duke@435: exp_pos = node->operand_position_format(opid); duke@435: assert(exp_pos != -1, "Bad expand rule"); duke@435: duke@435: new_pos = new_inst->operand_position(parameter,Component::USE); duke@435: if (new_pos != -1) { duke@435: // Copy the operand from the ExpandNode to the new node duke@435: fprintf(fp," n%d->set_opnd_array(%d, opnd_array(%d)->clone(C)); // %s\n", duke@435: cnt, new_pos, exp_pos, opid); duke@435: // For each operand add appropriate input edges by looking at tmp's duke@435: fprintf(fp," if(tmp%d == this) {\n", exp_pos); duke@435: // Grab corresponding edges from ExpandNode and insert them here duke@435: fprintf(fp," for(unsigned i = 0; i < num%d; i++) {\n", exp_pos); duke@435: fprintf(fp," n%d->add_req(_in[i + idx%d]);\n", cnt, exp_pos); duke@435: fprintf(fp," }\n"); duke@435: fprintf(fp," }\n"); duke@435: // This value is generated by one of the new instructions duke@435: fprintf(fp," else n%d->add_req(tmp%d);\n", cnt, exp_pos); duke@435: } duke@435: } duke@435: duke@435: // Update the DAG tmp's for values defined by this instruction duke@435: int new_def_pos = new_inst->operand_position(parameter,Component::DEF); duke@435: Effect *eform = (Effect *)new_inst->_effects[parameter]; duke@435: // If this operand is a definition in either an effects rule duke@435: // or a match rule duke@435: if((eform) && (is_def(eform->_use_def))) { duke@435: // Update the temp associated with this operand duke@435: fprintf(fp," tmp%d = n%d;\n", exp_pos, cnt); duke@435: } duke@435: else if( new_def_pos != -1 ) { duke@435: // Instruction defines a value but user did not declare it duke@435: // in the 'effect' clause duke@435: fprintf(fp," tmp%d = n%d;\n", exp_pos, cnt); duke@435: } duke@435: } // done iterating over a new instruction's operands duke@435: duke@435: // Invoke Expand() for the newly created instruction. duke@435: fprintf(fp," result = n%d->Expand( state, proj_list );\n", cnt); duke@435: assert( !new_inst->expands(), "Do not have complete support for recursive expansion"); duke@435: } // done iterating over new instructions duke@435: fprintf(fp,"\n"); duke@435: } // done generating expand rule duke@435: duke@435: else if( node->_matrule != NULL ) { duke@435: // Remove duplicated operands and inputs which use the same name. duke@435: // Seach through match operands for the same name usage. duke@435: uint cur_num_opnds = node->num_opnds(); duke@435: if( cur_num_opnds > 1 && cur_num_opnds != node->num_unique_opnds() ) { duke@435: Component *comp = NULL; duke@435: // Build mapping from num_edges to local variables duke@435: fprintf(fp," unsigned num0 = 0;\n"); duke@435: for( i = 1; i < cur_num_opnds; i++ ) { duke@435: fprintf(fp," unsigned num%d = opnd_array(%d)->num_edges();\n",i,i); duke@435: } duke@435: // Build a mapping from operand index to input edges duke@435: fprintf(fp," unsigned idx0 = oper_input_base();\n"); duke@435: for( i = 0; i < cur_num_opnds; i++ ) { duke@435: fprintf(fp," unsigned idx%d = idx%d + num%d;\n", duke@435: i+1,i,i); duke@435: } duke@435: duke@435: uint new_num_opnds = 1; duke@435: node->_components.reset(); duke@435: // Skip first unique operands. duke@435: for( i = 1; i < cur_num_opnds; i++ ) { duke@435: comp = node->_components.iter(); duke@435: if( (int)i != node->unique_opnds_idx(i) ) { duke@435: break; duke@435: } duke@435: new_num_opnds++; duke@435: } duke@435: // Replace not unique operands with next unique operands. duke@435: for( ; i < cur_num_opnds; i++ ) { duke@435: comp = node->_components.iter(); duke@435: int j = node->unique_opnds_idx(i); duke@435: // unique_opnds_idx(i) is unique if unique_opnds_idx(j) is not unique. duke@435: if( j != node->unique_opnds_idx(j) ) { duke@435: fprintf(fp," set_opnd_array(%d, opnd_array(%d)->clone(C)); // %s\n", duke@435: new_num_opnds, i, comp->_name); duke@435: // delete not unique edges here duke@435: fprintf(fp," for(unsigned i = 0; i < num%d; i++) {\n", i); duke@435: fprintf(fp," set_req(i + idx%d, _in[i + idx%d]);\n", new_num_opnds, i); duke@435: fprintf(fp," }\n"); duke@435: fprintf(fp," num%d = num%d;\n", new_num_opnds, i); duke@435: fprintf(fp," idx%d = idx%d + num%d;\n", new_num_opnds+1, new_num_opnds, new_num_opnds); duke@435: new_num_opnds++; duke@435: } duke@435: } duke@435: // delete the rest of edges duke@435: fprintf(fp," for(int i = idx%d - 1; i >= (int)idx%d; i--) {\n", cur_num_opnds, new_num_opnds); duke@435: fprintf(fp," del_req(i);\n", i); duke@435: fprintf(fp," }\n"); duke@435: fprintf(fp," _num_opnds = %d;\n", new_num_opnds); duke@435: } duke@435: } duke@435: duke@435: duke@435: // Generate projections for instruction's additional DEFs and KILLs duke@435: if( ! node->expands() && (node->needs_projections() || node->has_temps())) { duke@435: // Get string representing the MachNode that projections point at duke@435: const char *machNode = "this"; duke@435: // Generate the projections duke@435: fprintf(fp," // Add projection edges for additional defs or kills\n"); duke@435: duke@435: // Examine each component to see if it is a DEF or KILL duke@435: node->_components.reset(); duke@435: // Skip the first component, if already handled as (SET dst (...)) duke@435: Component *comp = NULL; duke@435: // For kills, the choice of projection numbers is arbitrary duke@435: int proj_no = 1; duke@435: bool declared_def = false; duke@435: bool declared_kill = false; duke@435: duke@435: while( (comp = node->_components.iter()) != NULL ) { duke@435: // Lookup register class associated with operand type duke@435: Form *form = (Form*)_globalNames[comp->_type]; duke@435: assert( form, "component type must be a defined form"); duke@435: OperandForm *op = form->is_operand(); duke@435: duke@435: if (comp->is(Component::TEMP)) { duke@435: fprintf(fp, " // TEMP %s\n", comp->_name); duke@435: if (!declared_def) { duke@435: // Define the variable "def" to hold new MachProjNodes duke@435: fprintf(fp, " MachTempNode *def;\n"); duke@435: declared_def = true; duke@435: } duke@435: if (op && op->_interface && op->_interface->is_RegInterface()) { duke@435: fprintf(fp," def = new (C) MachTempNode(state->MachOperGenerator( %s, C ));\n", duke@435: machOperEnum(op->_ident)); duke@435: fprintf(fp," add_req(def);\n"); duke@435: int idx = node->operand_position_format(comp->_name); duke@435: fprintf(fp," set_opnd_array(%d, state->MachOperGenerator( %s, C ));\n", duke@435: idx, machOperEnum(op->_ident)); duke@435: } else { duke@435: assert(false, "can't have temps which aren't registers"); duke@435: } duke@435: } else if (comp->isa(Component::KILL)) { duke@435: fprintf(fp, " // DEF/KILL %s\n", comp->_name); duke@435: duke@435: if (!declared_kill) { duke@435: // Define the variable "kill" to hold new MachProjNodes duke@435: fprintf(fp, " MachProjNode *kill;\n"); duke@435: declared_kill = true; duke@435: } duke@435: duke@435: assert( op, "Support additional KILLS for base operands"); duke@435: const char *regmask = reg_mask(*op); duke@435: const char *ideal_type = op->ideal_type(_globalNames, _register); duke@435: duke@435: if (!op->is_bound_register()) { duke@435: syntax_err(node->_linenum, "In %s only bound registers can be killed: %s %s\n", duke@435: node->_ident, comp->_type, comp->_name); duke@435: } duke@435: duke@435: fprintf(fp," kill = "); duke@435: fprintf(fp,"new (C, 1) MachProjNode( %s, %d, (%s), Op_%s );\n", duke@435: machNode, proj_no++, regmask, ideal_type); duke@435: fprintf(fp," proj_list.push(kill);\n"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: fprintf(fp,"\n"); duke@435: if( node->expands() ) { duke@435: fprintf(fp," return result;\n",cnt-1); duke@435: } else { duke@435: fprintf(fp," return this;\n"); duke@435: } duke@435: fprintf(fp,"}\n"); duke@435: fprintf(fp,"\n"); duke@435: } duke@435: duke@435: duke@435: //------------------------------Emit Routines---------------------------------- duke@435: // Special classes and routines for defining node emit routines which output duke@435: // target specific instruction object encodings. duke@435: // Define the ___Node::emit() routine duke@435: // duke@435: // (1) void ___Node::emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const { duke@435: // (2) // ... encoding defined by user duke@435: // (3) duke@435: // (4) } duke@435: // duke@435: duke@435: class DefineEmitState { duke@435: private: duke@435: enum reloc_format { RELOC_NONE = -1, duke@435: RELOC_IMMEDIATE = 0, duke@435: RELOC_DISP = 1, duke@435: RELOC_CALL_DISP = 2 }; duke@435: enum literal_status{ LITERAL_NOT_SEEN = 0, duke@435: LITERAL_SEEN = 1, duke@435: LITERAL_ACCESSED = 2, duke@435: LITERAL_OUTPUT = 3 }; duke@435: // Temporaries that describe current operand duke@435: bool _cleared; duke@435: OpClassForm *_opclass; duke@435: OperandForm *_operand; duke@435: int _operand_idx; duke@435: const char *_local_name; duke@435: const char *_operand_name; duke@435: bool _doing_disp; duke@435: bool _doing_constant; duke@435: Form::DataType _constant_type; duke@435: DefineEmitState::literal_status _constant_status; duke@435: DefineEmitState::literal_status _reg_status; duke@435: bool _doing_emit8; duke@435: bool _doing_emit_d32; duke@435: bool _doing_emit_d16; duke@435: bool _doing_emit_hi; duke@435: bool _doing_emit_lo; duke@435: bool _may_reloc; duke@435: bool _must_reloc; duke@435: reloc_format _reloc_form; duke@435: const char * _reloc_type; duke@435: bool _processing_noninput; duke@435: duke@435: NameList _strings_to_emit; duke@435: duke@435: // Stable state, set by constructor duke@435: ArchDesc &_AD; duke@435: FILE *_fp; duke@435: EncClass &_encoding; duke@435: InsEncode &_ins_encode; duke@435: InstructForm &_inst; duke@435: duke@435: public: duke@435: DefineEmitState(FILE *fp, ArchDesc &AD, EncClass &encoding, duke@435: InsEncode &ins_encode, InstructForm &inst) duke@435: : _AD(AD), _fp(fp), _encoding(encoding), _ins_encode(ins_encode), _inst(inst) { duke@435: clear(); duke@435: } duke@435: duke@435: void clear() { duke@435: _cleared = true; duke@435: _opclass = NULL; duke@435: _operand = NULL; duke@435: _operand_idx = 0; duke@435: _local_name = ""; duke@435: _operand_name = ""; duke@435: _doing_disp = false; duke@435: _doing_constant= false; duke@435: _constant_type = Form::none; duke@435: _constant_status = LITERAL_NOT_SEEN; duke@435: _reg_status = LITERAL_NOT_SEEN; duke@435: _doing_emit8 = false; duke@435: _doing_emit_d32= false; duke@435: _doing_emit_d16= false; duke@435: _doing_emit_hi = false; duke@435: _doing_emit_lo = false; duke@435: _may_reloc = false; duke@435: _must_reloc = false; duke@435: _reloc_form = RELOC_NONE; duke@435: _reloc_type = AdlcVMDeps::none_reloc_type(); duke@435: _strings_to_emit.clear(); duke@435: } duke@435: duke@435: // Track necessary state when identifying a replacement variable duke@435: void update_state(const char *rep_var) { duke@435: // A replacement variable or one of its subfields duke@435: // Obtain replacement variable from list duke@435: if ( (*rep_var) != '$' ) { duke@435: // A replacement variable, '$' prefix duke@435: // check_rep_var( rep_var ); duke@435: if ( Opcode::as_opcode_type(rep_var) != Opcode::NOT_AN_OPCODE ) { duke@435: // No state needed. duke@435: assert( _opclass == NULL, duke@435: "'primary', 'secondary' and 'tertiary' don't follow operand."); duke@435: } else { duke@435: // Lookup its position in parameter list duke@435: int param_no = _encoding.rep_var_index(rep_var); duke@435: if ( param_no == -1 ) { duke@435: _AD.syntax_err( _encoding._linenum, duke@435: "Replacement variable %s not found in enc_class %s.\n", duke@435: rep_var, _encoding._name); duke@435: } duke@435: duke@435: // Lookup the corresponding ins_encode parameter duke@435: const char *inst_rep_var = _ins_encode.rep_var_name(_inst, param_no); duke@435: if (inst_rep_var == NULL) { duke@435: _AD.syntax_err( _ins_encode._linenum, duke@435: "Parameter %s not passed to enc_class %s from instruct %s.\n", duke@435: rep_var, _encoding._name, _inst._ident); duke@435: } duke@435: duke@435: // Check if instruction's actual parameter is a local name in the instruction duke@435: const Form *local = _inst._localNames[inst_rep_var]; duke@435: OpClassForm *opc = (local != NULL) ? local->is_opclass() : NULL; duke@435: // Note: assert removed to allow constant and symbolic parameters duke@435: // assert( opc, "replacement variable was not found in local names"); duke@435: // Lookup the index position iff the replacement variable is a localName duke@435: int idx = (opc != NULL) ? _inst.operand_position_format(inst_rep_var) : -1; duke@435: duke@435: if ( idx != -1 ) { duke@435: // This is a local in the instruction duke@435: // Update local state info. duke@435: _opclass = opc; duke@435: _operand_idx = idx; duke@435: _local_name = rep_var; duke@435: _operand_name = inst_rep_var; duke@435: duke@435: // !!!!! duke@435: // Do not support consecutive operands. duke@435: assert( _operand == NULL, "Unimplemented()"); duke@435: _operand = opc->is_operand(); duke@435: } duke@435: else if( ADLParser::is_literal_constant(inst_rep_var) ) { duke@435: // Instruction provided a constant expression duke@435: // Check later that encoding specifies $$$constant to resolve as constant duke@435: _constant_status = LITERAL_SEEN; duke@435: } duke@435: else if( Opcode::as_opcode_type(inst_rep_var) != Opcode::NOT_AN_OPCODE ) { duke@435: // Instruction provided an opcode: "primary", "secondary", "tertiary" duke@435: // Check later that encoding specifies $$$constant to resolve as constant duke@435: _constant_status = LITERAL_SEEN; duke@435: } duke@435: else if((_AD.get_registers() != NULL ) && (_AD.get_registers()->getRegDef(inst_rep_var) != NULL)) { duke@435: // Instruction provided a literal register name for this parameter duke@435: // Check that encoding specifies $$$reg to resolve.as register. duke@435: _reg_status = LITERAL_SEEN; duke@435: } duke@435: else { duke@435: // Check for unimplemented functionality before hard failure duke@435: assert( strcmp(opc->_ident,"label")==0, "Unimplemented() Label"); duke@435: assert( false, "ShouldNotReachHere()"); duke@435: } duke@435: } // done checking which operand this is. duke@435: } else { duke@435: // duke@435: // A subfield variable, '$$' prefix duke@435: // Check for fields that may require relocation information. duke@435: // Then check that literal register parameters are accessed with 'reg' or 'constant' duke@435: // duke@435: if ( strcmp(rep_var,"$disp") == 0 ) { duke@435: _doing_disp = true; duke@435: assert( _opclass, "Must use operand or operand class before '$disp'"); duke@435: if( _operand == NULL ) { duke@435: // Only have an operand class, generate run-time check for relocation duke@435: _may_reloc = true; duke@435: _reloc_form = RELOC_DISP; duke@435: _reloc_type = AdlcVMDeps::oop_reloc_type(); duke@435: } else { duke@435: // Do precise check on operand: is it a ConP or not duke@435: // duke@435: // Check interface for value of displacement duke@435: assert( ( _operand->_interface != NULL ), duke@435: "$disp can only follow memory interface operand"); duke@435: MemInterface *mem_interface= _operand->_interface->is_MemInterface(); duke@435: assert( mem_interface != NULL, duke@435: "$disp can only follow memory interface operand"); duke@435: const char *disp = mem_interface->_disp; duke@435: duke@435: if( disp != NULL && (*disp == '$') ) { duke@435: // MemInterface::disp contains a replacement variable, duke@435: // Check if this matches a ConP duke@435: // duke@435: // Lookup replacement variable, in operand's component list duke@435: const char *rep_var_name = disp + 1; // Skip '$' duke@435: const Component *comp = _operand->_components.search(rep_var_name); duke@435: assert( comp != NULL,"Replacement variable not found in components"); duke@435: const char *type = comp->_type; duke@435: // Lookup operand form for replacement variable's type duke@435: const Form *form = _AD.globalNames()[type]; duke@435: assert( form != NULL, "Replacement variable's type not found"); duke@435: OperandForm *op = form->is_operand(); duke@435: assert( op, "Attempting to emit a non-register or non-constant"); duke@435: // Check if this is a constant duke@435: if (op->_matrule && op->_matrule->is_base_constant(_AD.globalNames())) { duke@435: // Check which constant this name maps to: _c0, _c1, ..., _cn duke@435: // const int idx = _operand.constant_position(_AD.globalNames(), comp); duke@435: // assert( idx != -1, "Constant component not found in operand"); duke@435: Form::DataType dtype = op->is_base_constant(_AD.globalNames()); duke@435: if ( dtype == Form::idealP ) { duke@435: _may_reloc = true; duke@435: // No longer true that idealP is always an oop duke@435: _reloc_form = RELOC_DISP; duke@435: _reloc_type = AdlcVMDeps::oop_reloc_type(); duke@435: } duke@435: } duke@435: duke@435: else if( _operand->is_user_name_for_sReg() != Form::none ) { duke@435: // The only non-constant allowed access to disp is an operand sRegX in a stackSlotX duke@435: assert( op->ideal_to_sReg_type(type) != Form::none, "StackSlots access displacements using 'sRegs'"); duke@435: _may_reloc = false; duke@435: } else { duke@435: assert( false, "fatal(); Only stackSlots can access a non-constant using 'disp'"); duke@435: } duke@435: } duke@435: } // finished with precise check of operand for relocation. duke@435: } // finished with subfield variable duke@435: else if ( strcmp(rep_var,"$constant") == 0 ) { duke@435: _doing_constant = true; duke@435: if ( _constant_status == LITERAL_NOT_SEEN ) { duke@435: // Check operand for type of constant duke@435: assert( _operand, "Must use operand before '$$constant'"); duke@435: Form::DataType dtype = _operand->is_base_constant(_AD.globalNames()); duke@435: _constant_type = dtype; duke@435: if ( dtype == Form::idealP ) { duke@435: _may_reloc = true; duke@435: // No longer true that idealP is always an oop duke@435: // // _must_reloc = true; duke@435: _reloc_form = RELOC_IMMEDIATE; duke@435: _reloc_type = AdlcVMDeps::oop_reloc_type(); duke@435: } else { duke@435: // No relocation information needed duke@435: } duke@435: } else { duke@435: // User-provided literals may not require relocation information !!!!! duke@435: assert( _constant_status == LITERAL_SEEN, "Must know we are processing a user-provided literal"); duke@435: } duke@435: } duke@435: else if ( strcmp(rep_var,"$label") == 0 ) { duke@435: // Calls containing labels require relocation duke@435: if ( _inst.is_ideal_call() ) { duke@435: _may_reloc = true; duke@435: // !!!!! !!!!! duke@435: _reloc_type = AdlcVMDeps::none_reloc_type(); duke@435: } duke@435: } duke@435: duke@435: // literal register parameter must be accessed as a 'reg' field. duke@435: if ( _reg_status != LITERAL_NOT_SEEN ) { duke@435: assert( _reg_status == LITERAL_SEEN, "Must have seen register literal before now"); duke@435: if (strcmp(rep_var,"$reg") == 0 || reg_conversion(rep_var) != NULL) { duke@435: _reg_status = LITERAL_ACCESSED; duke@435: } else { duke@435: assert( false, "invalid access to literal register parameter"); duke@435: } duke@435: } duke@435: // literal constant parameters must be accessed as a 'constant' field duke@435: if ( _constant_status != LITERAL_NOT_SEEN ) { duke@435: assert( _constant_status == LITERAL_SEEN, "Must have seen constant literal before now"); duke@435: if( strcmp(rep_var,"$constant") == 0 ) { duke@435: _constant_status = LITERAL_ACCESSED; duke@435: } else { duke@435: assert( false, "invalid access to literal constant parameter"); duke@435: } duke@435: } duke@435: } // end replacement and/or subfield duke@435: duke@435: } duke@435: duke@435: void add_rep_var(const char *rep_var) { duke@435: // Handle subfield and replacement variables. duke@435: if ( ( *rep_var == '$' ) && ( *(rep_var+1) == '$' ) ) { duke@435: // Check for emit prefix, '$$emit32' duke@435: assert( _cleared, "Can not nest $$$emit32"); duke@435: if ( strcmp(rep_var,"$$emit32") == 0 ) { duke@435: _doing_emit_d32 = true; duke@435: } duke@435: else if ( strcmp(rep_var,"$$emit16") == 0 ) { duke@435: _doing_emit_d16 = true; duke@435: } duke@435: else if ( strcmp(rep_var,"$$emit_hi") == 0 ) { duke@435: _doing_emit_hi = true; duke@435: } duke@435: else if ( strcmp(rep_var,"$$emit_lo") == 0 ) { duke@435: _doing_emit_lo = true; duke@435: } duke@435: else if ( strcmp(rep_var,"$$emit8") == 0 ) { duke@435: _doing_emit8 = true; duke@435: } duke@435: else { duke@435: _AD.syntax_err(_encoding._linenum, "Unsupported $$operation '%s'\n",rep_var); duke@435: assert( false, "fatal();"); duke@435: } duke@435: } duke@435: else { duke@435: // Update state for replacement variables duke@435: update_state( rep_var ); duke@435: _strings_to_emit.addName(rep_var); duke@435: } duke@435: _cleared = false; duke@435: } duke@435: duke@435: void emit_replacement() { duke@435: // A replacement variable or one of its subfields duke@435: // Obtain replacement variable from list duke@435: // const char *ec_rep_var = encoding->_rep_vars.iter(); duke@435: const char *rep_var; duke@435: _strings_to_emit.reset(); duke@435: while ( (rep_var = _strings_to_emit.iter()) != NULL ) { duke@435: duke@435: if ( (*rep_var) == '$' ) { duke@435: // A subfield variable, '$$' prefix duke@435: emit_field( rep_var ); duke@435: } else { duke@435: // A replacement variable, '$' prefix duke@435: emit_rep_var( rep_var ); duke@435: } // end replacement and/or subfield duke@435: } duke@435: } duke@435: duke@435: void emit_reloc_type(const char* type) { duke@435: fprintf(_fp, "%s", type) duke@435: ; duke@435: } duke@435: duke@435: duke@435: void gen_emit_x_reloc(const char *d32_lo_hi ) { duke@435: fprintf(_fp,"emit_%s_reloc(cbuf, ", d32_lo_hi ); duke@435: emit_replacement(); fprintf(_fp,", "); duke@435: emit_reloc_type( _reloc_type ); fprintf(_fp,", "); duke@435: fprintf(_fp, "%d", _reloc_form);fprintf(_fp, ");"); duke@435: } duke@435: duke@435: duke@435: void emit() { duke@435: // duke@435: // "emit_d32_reloc(" or "emit_hi_reloc" or "emit_lo_reloc" duke@435: // duke@435: // Emit the function name when generating an emit function duke@435: if ( _doing_emit_d32 || _doing_emit_hi || _doing_emit_lo ) { duke@435: const char *d32_hi_lo = _doing_emit_d32 ? "d32" : (_doing_emit_hi ? "hi" : "lo"); duke@435: // In general, relocatable isn't known at compiler compile time. duke@435: // Check results of prior scan duke@435: if ( ! _may_reloc ) { duke@435: // Definitely don't need relocation information duke@435: fprintf( _fp, "emit_%s(cbuf, ", d32_hi_lo ); duke@435: emit_replacement(); fprintf(_fp, ")"); duke@435: } duke@435: else if ( _must_reloc ) { duke@435: // Must emit relocation information duke@435: gen_emit_x_reloc( d32_hi_lo ); duke@435: } duke@435: else { duke@435: // Emit RUNTIME CHECK to see if value needs relocation info duke@435: // If emitting a relocatable address, use 'emit_d32_reloc' duke@435: const char *disp_constant = _doing_disp ? "disp" : _doing_constant ? "constant" : "INVALID"; duke@435: assert( (_doing_disp || _doing_constant) duke@435: && !(_doing_disp && _doing_constant), duke@435: "Must be emitting either a displacement or a constant"); duke@435: fprintf(_fp,"\n"); duke@435: fprintf(_fp,"if ( opnd_array(%d)->%s_is_oop() ) {\n", duke@435: _operand_idx, disp_constant); duke@435: fprintf(_fp," "); duke@435: gen_emit_x_reloc( d32_hi_lo ); fprintf(_fp,"\n"); duke@435: fprintf(_fp,"} else {\n"); duke@435: fprintf(_fp," emit_%s(cbuf, ", d32_hi_lo); duke@435: emit_replacement(); fprintf(_fp, ");\n"); fprintf(_fp,"}"); duke@435: } duke@435: } duke@435: else if ( _doing_emit_d16 ) { duke@435: // Relocation of 16-bit values is not supported duke@435: fprintf(_fp,"emit_d16(cbuf, "); duke@435: emit_replacement(); fprintf(_fp, ")"); duke@435: // No relocation done for 16-bit values duke@435: } duke@435: else if ( _doing_emit8 ) { duke@435: // Relocation of 8-bit values is not supported duke@435: fprintf(_fp,"emit_d8(cbuf, "); duke@435: emit_replacement(); fprintf(_fp, ")"); duke@435: // No relocation done for 8-bit values duke@435: } duke@435: else { duke@435: // Not an emit# command, just output the replacement string. duke@435: emit_replacement(); duke@435: } duke@435: duke@435: // Get ready for next state collection. duke@435: clear(); duke@435: } duke@435: duke@435: private: duke@435: duke@435: // recognizes names which represent MacroAssembler register types duke@435: // and return the conversion function to build them from OptoReg duke@435: const char* reg_conversion(const char* rep_var) { duke@435: if (strcmp(rep_var,"$Register") == 0) return "as_Register"; duke@435: if (strcmp(rep_var,"$FloatRegister") == 0) return "as_FloatRegister"; duke@435: #if defined(IA32) || defined(AMD64) duke@435: if (strcmp(rep_var,"$XMMRegister") == 0) return "as_XMMRegister"; duke@435: #endif duke@435: return NULL; duke@435: } duke@435: duke@435: void emit_field(const char *rep_var) { duke@435: const char* reg_convert = reg_conversion(rep_var); duke@435: duke@435: // A subfield variable, '$$subfield' duke@435: if ( strcmp(rep_var, "$reg") == 0 || reg_convert != NULL) { duke@435: // $reg form or the $Register MacroAssembler type conversions duke@435: assert( _operand_idx != -1, duke@435: "Must use this subfield after operand"); duke@435: if( _reg_status == LITERAL_NOT_SEEN ) { duke@435: if (_processing_noninput) { duke@435: const Form *local = _inst._localNames[_operand_name]; duke@435: OperandForm *oper = local->is_operand(); duke@435: const RegDef* first = oper->get_RegClass()->find_first_elem(); duke@435: if (reg_convert != NULL) { duke@435: fprintf(_fp, "%s(%s_enc)", reg_convert, first->_regname); duke@435: } else { duke@435: fprintf(_fp, "%s_enc", first->_regname); duke@435: } duke@435: } else { duke@435: fprintf(_fp,"->%s(ra_,this", reg_convert != NULL ? reg_convert : "reg"); duke@435: // Add parameter for index position, if not result operand duke@435: if( _operand_idx != 0 ) fprintf(_fp,",idx%d", _operand_idx); duke@435: fprintf(_fp,")"); duke@435: } duke@435: } else { duke@435: assert( _reg_status == LITERAL_OUTPUT, "should have output register literal in emit_rep_var"); duke@435: // Register literal has already been sent to output file, nothing more needed duke@435: } duke@435: } duke@435: else if ( strcmp(rep_var,"$base") == 0 ) { duke@435: assert( _operand_idx != -1, duke@435: "Must use this subfield after operand"); duke@435: assert( ! _may_reloc, "UnImplemented()"); duke@435: fprintf(_fp,"->base(ra_,this,idx%d)", _operand_idx); duke@435: } duke@435: else if ( strcmp(rep_var,"$index") == 0 ) { duke@435: assert( _operand_idx != -1, duke@435: "Must use this subfield after operand"); duke@435: assert( ! _may_reloc, "UnImplemented()"); duke@435: fprintf(_fp,"->index(ra_,this,idx%d)", _operand_idx); duke@435: } duke@435: else if ( strcmp(rep_var,"$scale") == 0 ) { duke@435: assert( ! _may_reloc, "UnImplemented()"); duke@435: fprintf(_fp,"->scale()"); duke@435: } duke@435: else if ( strcmp(rep_var,"$cmpcode") == 0 ) { duke@435: assert( ! _may_reloc, "UnImplemented()"); duke@435: fprintf(_fp,"->ccode()"); duke@435: } duke@435: else if ( strcmp(rep_var,"$constant") == 0 ) { duke@435: if( _constant_status == LITERAL_NOT_SEEN ) { duke@435: if ( _constant_type == Form::idealD ) { duke@435: fprintf(_fp,"->constantD()"); duke@435: } else if ( _constant_type == Form::idealF ) { duke@435: fprintf(_fp,"->constantF()"); duke@435: } else if ( _constant_type == Form::idealL ) { duke@435: fprintf(_fp,"->constantL()"); duke@435: } else { duke@435: fprintf(_fp,"->constant()"); duke@435: } duke@435: } else { duke@435: assert( _constant_status == LITERAL_OUTPUT, "should have output constant literal in emit_rep_var"); duke@435: // Cosntant literal has already been sent to output file, nothing more needed duke@435: } duke@435: } duke@435: else if ( strcmp(rep_var,"$disp") == 0 ) { duke@435: Form::DataType stack_type = _operand ? _operand->is_user_name_for_sReg() : Form::none; duke@435: if( _operand && _operand_idx==0 && stack_type != Form::none ) { duke@435: fprintf(_fp,"->disp(ra_,this,0)"); duke@435: } else { duke@435: fprintf(_fp,"->disp(ra_,this,idx%d)", _operand_idx); duke@435: } duke@435: } duke@435: else if ( strcmp(rep_var,"$label") == 0 ) { duke@435: fprintf(_fp,"->label()"); duke@435: } duke@435: else if ( strcmp(rep_var,"$method") == 0 ) { duke@435: fprintf(_fp,"->method()"); duke@435: } duke@435: else { duke@435: printf("emit_field: %s\n",rep_var); duke@435: assert( false, "UnImplemented()"); duke@435: } duke@435: } duke@435: duke@435: duke@435: void emit_rep_var(const char *rep_var) { duke@435: _processing_noninput = false; duke@435: // A replacement variable, originally '$' duke@435: if ( Opcode::as_opcode_type(rep_var) != Opcode::NOT_AN_OPCODE ) { duke@435: _inst._opcode->print_opcode(_fp, Opcode::as_opcode_type(rep_var) ); duke@435: } duke@435: else { duke@435: // Lookup its position in parameter list duke@435: int param_no = _encoding.rep_var_index(rep_var); duke@435: if ( param_no == -1 ) { duke@435: _AD.syntax_err( _encoding._linenum, duke@435: "Replacement variable %s not found in enc_class %s.\n", duke@435: rep_var, _encoding._name); duke@435: } duke@435: // Lookup the corresponding ins_encode parameter duke@435: const char *inst_rep_var = _ins_encode.rep_var_name(_inst, param_no); duke@435: duke@435: // Check if instruction's actual parameter is a local name in the instruction duke@435: const Form *local = _inst._localNames[inst_rep_var]; duke@435: OpClassForm *opc = (local != NULL) ? local->is_opclass() : NULL; duke@435: // Note: assert removed to allow constant and symbolic parameters duke@435: // assert( opc, "replacement variable was not found in local names"); duke@435: // Lookup the index position iff the replacement variable is a localName duke@435: int idx = (opc != NULL) ? _inst.operand_position_format(inst_rep_var) : -1; duke@435: if( idx != -1 ) { duke@435: if (_inst.is_noninput_operand(idx)) { duke@435: // This operand isn't a normal input so printing it is done duke@435: // specially. duke@435: _processing_noninput = true; duke@435: } else { duke@435: // Output the emit code for this operand duke@435: fprintf(_fp,"opnd_array(%d)",idx); duke@435: } duke@435: assert( _operand == opc->is_operand(), duke@435: "Previous emit $operand does not match current"); duke@435: } duke@435: else if( ADLParser::is_literal_constant(inst_rep_var) ) { duke@435: // else check if it is a constant expression duke@435: // Removed following assert to allow primitive C types as arguments to encodings duke@435: // assert( _constant_status == LITERAL_ACCESSED, "Must be processing a literal constant parameter"); duke@435: fprintf(_fp,"(%s)", inst_rep_var); duke@435: _constant_status = LITERAL_OUTPUT; duke@435: } duke@435: else if( Opcode::as_opcode_type(inst_rep_var) != Opcode::NOT_AN_OPCODE ) { duke@435: // else check if "primary", "secondary", "tertiary" duke@435: assert( _constant_status == LITERAL_ACCESSED, "Must be processing a literal constant parameter"); duke@435: _inst._opcode->print_opcode(_fp, Opcode::as_opcode_type(inst_rep_var) ); duke@435: _constant_status = LITERAL_OUTPUT; duke@435: } duke@435: else if((_AD.get_registers() != NULL ) && (_AD.get_registers()->getRegDef(inst_rep_var) != NULL)) { duke@435: // Instruction provided a literal register name for this parameter duke@435: // Check that encoding specifies $$$reg to resolve.as register. duke@435: assert( _reg_status == LITERAL_ACCESSED, "Must be processing a literal register parameter"); duke@435: fprintf(_fp,"(%s_enc)", inst_rep_var); duke@435: _reg_status = LITERAL_OUTPUT; duke@435: } duke@435: else { duke@435: // Check for unimplemented functionality before hard failure duke@435: assert( strcmp(opc->_ident,"label")==0, "Unimplemented() Label"); duke@435: assert( false, "ShouldNotReachHere()"); duke@435: } duke@435: // all done duke@435: } duke@435: } duke@435: duke@435: }; // end class DefineEmitState duke@435: duke@435: duke@435: void ArchDesc::defineSize(FILE *fp, InstructForm &inst) { duke@435: duke@435: //(1) duke@435: // Output instruction's emit prototype duke@435: fprintf(fp,"uint %sNode::size(PhaseRegAlloc *ra_) const {\n", duke@435: inst._ident); duke@435: duke@435: //(2) duke@435: // Print the size duke@435: fprintf(fp, " return (VerifyOops ? MachNode::size(ra_) : %s);\n", inst._size); duke@435: duke@435: // (3) and (4) duke@435: fprintf(fp,"}\n"); duke@435: } duke@435: duke@435: void ArchDesc::defineEmit(FILE *fp, InstructForm &inst) { duke@435: InsEncode *ins_encode = inst._insencode; duke@435: duke@435: // (1) duke@435: // Output instruction's emit prototype duke@435: fprintf(fp,"void %sNode::emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const {\n", duke@435: inst._ident); duke@435: duke@435: // If user did not define an encode section, duke@435: // provide stub that does not generate any machine code. duke@435: if( (_encode == NULL) || (ins_encode == NULL) ) { duke@435: fprintf(fp, " // User did not define an encode section.\n"); duke@435: fprintf(fp,"}\n"); duke@435: return; duke@435: } duke@435: duke@435: // Save current instruction's starting address (helps with relocation). duke@435: fprintf( fp, " cbuf.set_inst_mark();\n"); duke@435: duke@435: // // // idx0 is only needed for syntactic purposes and only by "storeSSI" duke@435: // fprintf( fp, " unsigned idx0 = 0;\n"); duke@435: duke@435: // Output each operand's offset into the array of registers. duke@435: inst.index_temps( fp, _globalNames ); duke@435: duke@435: // Output this instruction's encodings duke@435: const char *ec_name; duke@435: bool user_defined = false; duke@435: ins_encode->reset(); duke@435: while ( (ec_name = ins_encode->encode_class_iter()) != NULL ) { duke@435: fprintf(fp, " {"); duke@435: // Output user-defined encoding duke@435: user_defined = true; duke@435: duke@435: const char *ec_code = NULL; duke@435: const char *ec_rep_var = NULL; duke@435: EncClass *encoding = _encode->encClass(ec_name); duke@435: if (encoding == NULL) { duke@435: fprintf(stderr, "User did not define contents of this encode_class: %s\n", ec_name); duke@435: abort(); duke@435: } duke@435: duke@435: if (ins_encode->current_encoding_num_args() != encoding->num_args()) { duke@435: globalAD->syntax_err(ins_encode->_linenum, "In %s: passing %d arguments to %s but expecting %d", duke@435: inst._ident, ins_encode->current_encoding_num_args(), duke@435: ec_name, encoding->num_args()); duke@435: } duke@435: duke@435: DefineEmitState pending(fp, *this, *encoding, *ins_encode, inst ); duke@435: encoding->_code.reset(); duke@435: encoding->_rep_vars.reset(); duke@435: // Process list of user-defined strings, duke@435: // and occurrences of replacement variables. duke@435: // Replacement Vars are pushed into a list and then output duke@435: while ( (ec_code = encoding->_code.iter()) != NULL ) { duke@435: if ( ! encoding->_code.is_signal( ec_code ) ) { duke@435: // Emit pending code duke@435: pending.emit(); duke@435: pending.clear(); duke@435: // Emit this code section duke@435: fprintf(fp,"%s", ec_code); duke@435: } else { duke@435: // A replacement variable or one of its subfields duke@435: // Obtain replacement variable from list duke@435: ec_rep_var = encoding->_rep_vars.iter(); duke@435: pending.add_rep_var(ec_rep_var); duke@435: } duke@435: } duke@435: // Emit pending code duke@435: pending.emit(); duke@435: pending.clear(); duke@435: fprintf(fp, "}\n"); duke@435: } // end while instruction's encodings duke@435: duke@435: // Check if user stated which encoding to user duke@435: if ( user_defined == false ) { duke@435: fprintf(fp, " // User did not define which encode class to use.\n"); duke@435: } duke@435: duke@435: // (3) and (4) duke@435: fprintf(fp,"}\n"); duke@435: } duke@435: duke@435: // --------------------------------------------------------------------------- duke@435: //--------Utilities to build MachOper and MachNode derived Classes------------ duke@435: // --------------------------------------------------------------------------- duke@435: duke@435: //------------------------------Utilities to build Operand Classes------------ duke@435: static void defineIn_RegMask(FILE *fp, FormDict &globals, OperandForm &oper) { duke@435: uint num_edges = oper.num_edges(globals); duke@435: if( num_edges != 0 ) { duke@435: // Method header duke@435: fprintf(fp, "const RegMask *%sOper::in_RegMask(int index) const {\n", duke@435: oper._ident); duke@435: duke@435: // Assert that the index is in range. duke@435: fprintf(fp, " assert(0 <= index && index < %d, \"index out of range\");\n", duke@435: num_edges); duke@435: duke@435: // Figure out if all RegMasks are the same. duke@435: const char* first_reg_class = oper.in_reg_class(0, globals); duke@435: bool all_same = true; duke@435: assert(first_reg_class != NULL, "did not find register mask"); duke@435: duke@435: for (uint index = 1; all_same && index < num_edges; index++) { duke@435: const char* some_reg_class = oper.in_reg_class(index, globals); duke@435: assert(some_reg_class != NULL, "did not find register mask"); duke@435: if (strcmp(first_reg_class, some_reg_class) != 0) { duke@435: all_same = false; duke@435: } duke@435: } duke@435: duke@435: if (all_same) { duke@435: // Return the sole RegMask. duke@435: if (strcmp(first_reg_class, "stack_slots") == 0) { duke@435: fprintf(fp," return &(Compile::current()->FIRST_STACK_mask());\n"); duke@435: } else { duke@435: fprintf(fp," return &%s_mask;\n", toUpper(first_reg_class)); duke@435: } duke@435: } else { duke@435: // Build a switch statement to return the desired mask. duke@435: fprintf(fp," switch (index) {\n"); duke@435: duke@435: for (uint index = 0; index < num_edges; index++) { duke@435: const char *reg_class = oper.in_reg_class(index, globals); duke@435: assert(reg_class != NULL, "did not find register mask"); duke@435: if( !strcmp(reg_class, "stack_slots") ) { duke@435: fprintf(fp, " case %d: return &(Compile::current()->FIRST_STACK_mask());\n", index); duke@435: } else { duke@435: fprintf(fp, " case %d: return &%s_mask;\n", index, toUpper(reg_class)); duke@435: } duke@435: } duke@435: fprintf(fp," }\n"); duke@435: fprintf(fp," ShouldNotReachHere();\n"); duke@435: fprintf(fp," return NULL;\n"); duke@435: } duke@435: duke@435: // Method close duke@435: fprintf(fp, "}\n\n"); duke@435: } duke@435: } duke@435: duke@435: // generate code to create a clone for a class derived from MachOper duke@435: // duke@435: // (0) MachOper *MachOperXOper::clone(Compile* C) const { duke@435: // (1) return new (C) MachXOper( _ccode, _c0, _c1, ..., _cn); duke@435: // (2) } duke@435: // duke@435: static void defineClone(FILE *fp, FormDict &globalNames, OperandForm &oper) { duke@435: fprintf(fp,"MachOper *%sOper::clone(Compile* C) const {\n", oper._ident); duke@435: // Check for constants that need to be copied over duke@435: const int num_consts = oper.num_consts(globalNames); duke@435: const bool is_ideal_bool = oper.is_ideal_bool(); duke@435: if( (num_consts > 0) ) { duke@435: fprintf(fp," return new (C) %sOper(", oper._ident); duke@435: // generate parameters for constants duke@435: int i = 0; duke@435: fprintf(fp,"_c%d", i); duke@435: for( i = 1; i < num_consts; ++i) { duke@435: fprintf(fp,", _c%d", i); duke@435: } duke@435: // finish line (1) duke@435: fprintf(fp,");\n"); duke@435: } duke@435: else { duke@435: assert( num_consts == 0, "Currently support zero or one constant per operand clone function"); duke@435: fprintf(fp," return new (C) %sOper();\n", oper._ident); duke@435: } duke@435: // finish method duke@435: fprintf(fp,"}\n"); duke@435: } duke@435: duke@435: static void define_hash(FILE *fp, char *operand) { duke@435: fprintf(fp,"uint %sOper::hash() const { return 5; }\n", operand); duke@435: } duke@435: duke@435: static void define_cmp(FILE *fp, char *operand) { duke@435: fprintf(fp,"uint %sOper::cmp( const MachOper &oper ) const { return opcode() == oper.opcode(); }\n", operand); duke@435: } duke@435: duke@435: duke@435: // Helper functions for bug 4796752, abstracted with minimal modification duke@435: // from define_oper_interface() duke@435: OperandForm *rep_var_to_operand(const char *encoding, OperandForm &oper, FormDict &globals) { duke@435: OperandForm *op = NULL; duke@435: // Check for replacement variable duke@435: if( *encoding == '$' ) { duke@435: // Replacement variable duke@435: const char *rep_var = encoding + 1; duke@435: // Lookup replacement variable, rep_var, in operand's component list duke@435: const Component *comp = oper._components.search(rep_var); duke@435: assert( comp != NULL, "Replacement variable not found in components"); duke@435: // Lookup operand form for replacement variable's type duke@435: const char *type = comp->_type; duke@435: Form *form = (Form*)globals[type]; duke@435: assert( form != NULL, "Replacement variable's type not found"); duke@435: op = form->is_operand(); duke@435: assert( op, "Attempting to emit a non-register or non-constant"); duke@435: } duke@435: duke@435: return op; duke@435: } duke@435: duke@435: int rep_var_to_constant_index(const char *encoding, OperandForm &oper, FormDict &globals) { duke@435: int idx = -1; duke@435: // Check for replacement variable duke@435: if( *encoding == '$' ) { duke@435: // Replacement variable duke@435: const char *rep_var = encoding + 1; duke@435: // Lookup replacement variable, rep_var, in operand's component list duke@435: const Component *comp = oper._components.search(rep_var); duke@435: assert( comp != NULL, "Replacement variable not found in components"); duke@435: // Lookup operand form for replacement variable's type duke@435: const char *type = comp->_type; duke@435: Form *form = (Form*)globals[type]; duke@435: assert( form != NULL, "Replacement variable's type not found"); duke@435: OperandForm *op = form->is_operand(); duke@435: assert( op, "Attempting to emit a non-register or non-constant"); duke@435: // Check that this is a constant and find constant's index: duke@435: if (op->_matrule && op->_matrule->is_base_constant(globals)) { duke@435: idx = oper.constant_position(globals, comp); duke@435: } duke@435: } duke@435: duke@435: return idx; duke@435: } duke@435: duke@435: bool is_regI(const char *encoding, OperandForm &oper, FormDict &globals ) { duke@435: bool is_regI = false; duke@435: duke@435: OperandForm *op = rep_var_to_operand(encoding, oper, globals); duke@435: if( op != NULL ) { duke@435: // Check that this is a register duke@435: if ( (op->_matrule && op->_matrule->is_base_register(globals)) ) { duke@435: // Register duke@435: const char* ideal = op->ideal_type(globals); duke@435: is_regI = (ideal && (op->ideal_to_Reg_type(ideal) == Form::idealI)); duke@435: } duke@435: } duke@435: duke@435: return is_regI; duke@435: } duke@435: duke@435: bool is_conP(const char *encoding, OperandForm &oper, FormDict &globals ) { duke@435: bool is_conP = false; duke@435: duke@435: OperandForm *op = rep_var_to_operand(encoding, oper, globals); duke@435: if( op != NULL ) { duke@435: // Check that this is a constant pointer duke@435: if (op->_matrule && op->_matrule->is_base_constant(globals)) { duke@435: // Constant duke@435: Form::DataType dtype = op->is_base_constant(globals); duke@435: is_conP = (dtype == Form::idealP); duke@435: } duke@435: } duke@435: duke@435: return is_conP; duke@435: } duke@435: duke@435: duke@435: // Define a MachOper interface methods duke@435: void ArchDesc::define_oper_interface(FILE *fp, OperandForm &oper, FormDict &globals, duke@435: const char *name, const char *encoding) { duke@435: bool emit_position = false; duke@435: int position = -1; duke@435: duke@435: fprintf(fp," virtual int %s", name); duke@435: // Generate access method for base, index, scale, disp, ... duke@435: if( (strcmp(name,"base") == 0) || (strcmp(name,"index") == 0) ) { duke@435: fprintf(fp,"(PhaseRegAlloc *ra_, const Node *node, int idx) const { \n"); duke@435: emit_position = true; duke@435: } else if ( (strcmp(name,"disp") == 0) ) { duke@435: fprintf(fp,"(PhaseRegAlloc *ra_, const Node *node, int idx) const { \n"); duke@435: } else { duke@435: fprintf(fp,"() const { "); duke@435: } duke@435: duke@435: // Check for hexadecimal value OR replacement variable duke@435: if( *encoding == '$' ) { duke@435: // Replacement variable duke@435: const char *rep_var = encoding + 1; duke@435: fprintf(fp,"// Replacement variable: %s\n", encoding+1); duke@435: // Lookup replacement variable, rep_var, in operand's component list duke@435: const Component *comp = oper._components.search(rep_var); duke@435: assert( comp != NULL, "Replacement variable not found in components"); duke@435: // Lookup operand form for replacement variable's type duke@435: const char *type = comp->_type; duke@435: Form *form = (Form*)globals[type]; duke@435: assert( form != NULL, "Replacement variable's type not found"); duke@435: OperandForm *op = form->is_operand(); duke@435: assert( op, "Attempting to emit a non-register or non-constant"); duke@435: // Check that this is a register or a constant and generate code: duke@435: if ( (op->_matrule && op->_matrule->is_base_register(globals)) ) { duke@435: // Register duke@435: int idx_offset = oper.register_position( globals, rep_var); duke@435: position = idx_offset; duke@435: fprintf(fp," return (int)ra_->get_encode(node->in(idx"); duke@435: if ( idx_offset > 0 ) fprintf(fp, "+%d",idx_offset); duke@435: fprintf(fp,"));\n"); duke@435: } else if ( op->ideal_to_sReg_type(op->_ident) != Form::none ) { duke@435: // StackSlot for an sReg comes either from input node or from self, when idx==0 duke@435: fprintf(fp," if( idx != 0 ) {\n"); duke@435: fprintf(fp," // Access register number for input operand\n"); duke@435: fprintf(fp," return ra_->reg2offset(ra_->get_reg_first(node->in(idx)));/* sReg */\n"); duke@435: fprintf(fp," }\n"); duke@435: fprintf(fp," // Access register number from myself\n"); duke@435: fprintf(fp," return ra_->reg2offset(ra_->get_reg_first(node));/* sReg */\n"); duke@435: } else if (op->_matrule && op->_matrule->is_base_constant(globals)) { duke@435: // Constant duke@435: // Check which constant this name maps to: _c0, _c1, ..., _cn duke@435: const int idx = oper.constant_position(globals, comp); duke@435: assert( idx != -1, "Constant component not found in operand"); duke@435: // Output code for this constant, type dependent. duke@435: fprintf(fp," return (int)" ); duke@435: oper.access_constant(fp, globals, (uint)idx /* , const_type */); duke@435: fprintf(fp,";\n"); duke@435: } else { duke@435: assert( false, "Attempting to emit a non-register or non-constant"); duke@435: } duke@435: } duke@435: else if( *encoding == '0' && *(encoding+1) == 'x' ) { duke@435: // Hex value duke@435: fprintf(fp,"return %s;", encoding); duke@435: } else { duke@435: assert( false, "Do not support octal or decimal encode constants"); duke@435: } duke@435: fprintf(fp," }\n"); duke@435: duke@435: if( emit_position && (position != -1) && (oper.num_edges(globals) > 0) ) { duke@435: fprintf(fp," virtual int %s_position() const { return %d; }\n", name, position); duke@435: MemInterface *mem_interface = oper._interface->is_MemInterface(); duke@435: const char *base = mem_interface->_base; duke@435: const char *disp = mem_interface->_disp; duke@435: if( emit_position && (strcmp(name,"base") == 0) duke@435: && base != NULL && is_regI(base, oper, globals) duke@435: && disp != NULL && is_conP(disp, oper, globals) ) { duke@435: // Found a memory access using a constant pointer for a displacement duke@435: // and a base register containing an integer offset. duke@435: // In this case the base and disp are reversed with respect to what duke@435: // is expected by MachNode::get_base_and_disp() and MachNode::adr_type(). duke@435: // Provide a non-NULL return for disp_as_type() that will allow adr_type() duke@435: // to correctly compute the access type for alias analysis. duke@435: // duke@435: // See BugId 4796752, operand indOffset32X in i486.ad duke@435: int idx = rep_var_to_constant_index(disp, oper, globals); duke@435: fprintf(fp," virtual const TypePtr *disp_as_type() const { return _c%d; }\n", idx); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // duke@435: // Construct the method to copy _idx, inputs and operands to new node. duke@435: static void define_fill_new_machnode(bool used, FILE *fp_cpp) { duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "// Copy _idx, inputs and operands to new node\n"); duke@435: fprintf(fp_cpp, "void MachNode::fill_new_machnode( MachNode* node, Compile* C) const {\n"); duke@435: if( !used ) { duke@435: fprintf(fp_cpp, " // This architecture does not have cisc or short branch instructions\n"); duke@435: fprintf(fp_cpp, " ShouldNotCallThis();\n"); duke@435: fprintf(fp_cpp, "}\n"); duke@435: } else { duke@435: // New node must use same node index for access through allocator's tables duke@435: fprintf(fp_cpp, " // New node must use same node index\n"); duke@435: fprintf(fp_cpp, " node->set_idx( _idx );\n"); duke@435: // Copy machine-independent inputs duke@435: fprintf(fp_cpp, " // Copy machine-independent inputs\n"); duke@435: fprintf(fp_cpp, " for( uint j = 0; j < req(); j++ ) {\n"); duke@435: fprintf(fp_cpp, " node->add_req(in(j));\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: // Copy machine operands to new MachNode duke@435: fprintf(fp_cpp, " // Copy my operands, except for cisc position\n"); duke@435: fprintf(fp_cpp, " int nopnds = num_opnds();\n"); duke@435: fprintf(fp_cpp, " assert( node->num_opnds() == (uint)nopnds, \"Must have same number of operands\");\n"); duke@435: fprintf(fp_cpp, " MachOper **to = node->_opnds;\n"); duke@435: fprintf(fp_cpp, " for( int i = 0; i < nopnds; i++ ) {\n"); duke@435: fprintf(fp_cpp, " if( i != cisc_operand() ) \n"); duke@435: fprintf(fp_cpp, " to[i] = _opnds[i]->clone(C);\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: fprintf(fp_cpp, "}\n"); duke@435: } duke@435: fprintf(fp_cpp, "\n"); duke@435: } duke@435: duke@435: //------------------------------defineClasses---------------------------------- duke@435: // Define members of MachNode and MachOper classes based on duke@435: // operand and instruction lists duke@435: void ArchDesc::defineClasses(FILE *fp) { duke@435: duke@435: // Define the contents of an array containing the machine register names duke@435: defineRegNames(fp, _register); duke@435: // Define an array containing the machine register encoding values duke@435: defineRegEncodes(fp, _register); duke@435: // Generate an enumeration of user-defined register classes duke@435: // and a list of register masks, one for each class. duke@435: // Only define the RegMask value objects in the expand file. duke@435: // Declare each as an extern const RegMask ...; in ad_.hpp duke@435: declare_register_masks(_HPP_file._fp); duke@435: // build_register_masks(fp); duke@435: build_register_masks(_CPP_EXPAND_file._fp); duke@435: // Define the pipe_classes duke@435: build_pipe_classes(_CPP_PIPELINE_file._fp); duke@435: duke@435: // Generate Machine Classes for each operand defined in AD file duke@435: fprintf(fp,"\n"); duke@435: fprintf(fp,"\n"); duke@435: fprintf(fp,"//------------------Define classes derived from MachOper---------------------\n"); duke@435: // Iterate through all operands duke@435: _operands.reset(); duke@435: OperandForm *oper; duke@435: for( ; (oper = (OperandForm*)_operands.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( oper->ideal_only() ) continue; duke@435: // !!!!! duke@435: // The declaration of labelOper is in machine-independent file: machnode duke@435: if ( strcmp(oper->_ident,"label") == 0 ) { duke@435: defineIn_RegMask(_CPP_MISC_file._fp, _globalNames, *oper); duke@435: duke@435: fprintf(fp,"MachOper *%sOper::clone(Compile* C) const {\n", oper->_ident); duke@435: fprintf(fp," return new (C) %sOper(_label, _block_num);\n", oper->_ident); duke@435: fprintf(fp,"}\n"); duke@435: duke@435: fprintf(fp,"uint %sOper::opcode() const { return %s; }\n", duke@435: oper->_ident, machOperEnum(oper->_ident)); duke@435: // // Currently all XXXOper::Hash() methods are identical (990820) duke@435: // define_hash(fp, oper->_ident); duke@435: // // Currently all XXXOper::Cmp() methods are identical (990820) duke@435: // define_cmp(fp, oper->_ident); duke@435: fprintf(fp,"\n"); duke@435: duke@435: continue; duke@435: } duke@435: duke@435: // The declaration of methodOper is in machine-independent file: machnode duke@435: if ( strcmp(oper->_ident,"method") == 0 ) { duke@435: defineIn_RegMask(_CPP_MISC_file._fp, _globalNames, *oper); duke@435: duke@435: fprintf(fp,"MachOper *%sOper::clone(Compile* C) const {\n", oper->_ident); duke@435: fprintf(fp," return new (C) %sOper(_method);\n", oper->_ident); duke@435: fprintf(fp,"}\n"); duke@435: duke@435: fprintf(fp,"uint %sOper::opcode() const { return %s; }\n", duke@435: oper->_ident, machOperEnum(oper->_ident)); duke@435: // // Currently all XXXOper::Hash() methods are identical (990820) duke@435: // define_hash(fp, oper->_ident); duke@435: // // Currently all XXXOper::Cmp() methods are identical (990820) duke@435: // define_cmp(fp, oper->_ident); duke@435: fprintf(fp,"\n"); duke@435: duke@435: continue; duke@435: } duke@435: duke@435: defineIn_RegMask(fp, _globalNames, *oper); duke@435: defineClone(_CPP_CLONE_file._fp, _globalNames, *oper); duke@435: // // Currently all XXXOper::Hash() methods are identical (990820) duke@435: // define_hash(fp, oper->_ident); duke@435: // // Currently all XXXOper::Cmp() methods are identical (990820) duke@435: // define_cmp(fp, oper->_ident); duke@435: duke@435: // side-call to generate output that used to be in the header file: duke@435: extern void gen_oper_format(FILE *fp, FormDict &globals, OperandForm &oper, bool for_c_file); duke@435: gen_oper_format(_CPP_FORMAT_file._fp, _globalNames, *oper, true); duke@435: duke@435: } duke@435: duke@435: duke@435: // Generate Machine Classes for each instruction defined in AD file duke@435: fprintf(fp,"//------------------Define members for classes derived from MachNode----------\n"); duke@435: // Output the definitions for out_RegMask() // & kill_RegMask() duke@435: _instructions.reset(); duke@435: InstructForm *instr; duke@435: MachNodeForm *machnode; duke@435: for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( instr->ideal_only() ) continue; duke@435: duke@435: defineOut_RegMask(_CPP_MISC_file._fp, instr->_ident, reg_mask(*instr)); duke@435: } duke@435: duke@435: bool used = false; duke@435: // Output the definitions for expand rules & peephole rules duke@435: _instructions.reset(); duke@435: for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( instr->ideal_only() ) continue; duke@435: // If there are multiple defs/kills, or an explicit expand rule, build rule duke@435: if( instr->expands() || instr->needs_projections() || duke@435: instr->has_temps() || duke@435: instr->_matrule != NULL && duke@435: instr->num_opnds() != instr->num_unique_opnds() ) duke@435: defineExpand(_CPP_EXPAND_file._fp, instr); duke@435: // If there is an explicit peephole rule, build it duke@435: if ( instr->peepholes() ) duke@435: definePeephole(_CPP_PEEPHOLE_file._fp, instr); duke@435: duke@435: // Output code to convert to the cisc version, if applicable duke@435: used |= instr->define_cisc_version(*this, fp); duke@435: duke@435: // Output code to convert to the short branch version, if applicable duke@435: used |= instr->define_short_branch_methods(fp); duke@435: } duke@435: duke@435: // Construct the method called by cisc_version() to copy inputs and operands. duke@435: define_fill_new_machnode(used, fp); duke@435: duke@435: // Output the definitions for labels duke@435: _instructions.reset(); duke@435: while( (instr = (InstructForm*)_instructions.iter()) != NULL ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( instr->ideal_only() ) continue; duke@435: duke@435: // Access the fields for operand Label duke@435: int label_position = instr->label_position(); duke@435: if( label_position != -1 ) { duke@435: // Set the label duke@435: fprintf(fp,"void %sNode::label_set( Label& label, uint block_num ) {\n", instr->_ident); duke@435: fprintf(fp," labelOper* oper = (labelOper*)(opnd_array(%d));\n", duke@435: label_position ); duke@435: fprintf(fp," oper->_label = &label;\n"); duke@435: fprintf(fp," oper->_block_num = block_num;\n"); duke@435: fprintf(fp,"}\n"); duke@435: } duke@435: } duke@435: duke@435: // Output the definitions for methods duke@435: _instructions.reset(); duke@435: while( (instr = (InstructForm*)_instructions.iter()) != NULL ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( instr->ideal_only() ) continue; duke@435: duke@435: // Access the fields for operand Label duke@435: int method_position = instr->method_position(); duke@435: if( method_position != -1 ) { duke@435: // Access the method's address duke@435: fprintf(fp,"void %sNode::method_set( intptr_t method ) {\n", instr->_ident); duke@435: fprintf(fp," ((methodOper*)opnd_array(%d))->_method = method;\n", duke@435: method_position ); duke@435: fprintf(fp,"}\n"); duke@435: fprintf(fp,"\n"); duke@435: } duke@435: } duke@435: duke@435: // Define this instruction's number of relocation entries, base is '0' duke@435: _instructions.reset(); duke@435: while( (instr = (InstructForm*)_instructions.iter()) != NULL ) { duke@435: // Output the definition for number of relocation entries duke@435: uint reloc_size = instr->reloc(_globalNames); duke@435: if ( reloc_size != 0 ) { duke@435: fprintf(fp,"int %sNode::reloc() const {\n", instr->_ident); duke@435: fprintf(fp, " return %d;\n", reloc_size ); duke@435: fprintf(fp,"}\n"); duke@435: fprintf(fp,"\n"); duke@435: } duke@435: } duke@435: fprintf(fp,"\n"); duke@435: duke@435: // Output the definitions for code generation duke@435: // duke@435: // address ___Node::emit(address ptr, PhaseRegAlloc *ra_) const { duke@435: // // ... encoding defined by user duke@435: // return ptr; duke@435: // } duke@435: // duke@435: _instructions.reset(); duke@435: for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( instr->ideal_only() ) continue; duke@435: duke@435: if (instr->_insencode) defineEmit(fp, *instr); duke@435: if (instr->_size) defineSize(fp, *instr); duke@435: duke@435: // side-call to generate output that used to be in the header file: duke@435: extern void gen_inst_format(FILE *fp, FormDict &globals, InstructForm &oper, bool for_c_file); duke@435: gen_inst_format(_CPP_FORMAT_file._fp, _globalNames, *instr, true); duke@435: } duke@435: duke@435: // Output the definitions for alias analysis duke@435: _instructions.reset(); duke@435: for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( instr->ideal_only() ) continue; duke@435: duke@435: // Analyze machine instructions that either USE or DEF memory. duke@435: int memory_operand = instr->memory_operand(_globalNames); duke@435: // Some guys kill all of memory duke@435: if ( instr->is_wide_memory_kill(_globalNames) ) { duke@435: memory_operand = InstructForm::MANY_MEMORY_OPERANDS; duke@435: } duke@435: duke@435: if ( memory_operand != InstructForm::NO_MEMORY_OPERAND ) { duke@435: if( memory_operand == InstructForm::MANY_MEMORY_OPERANDS ) { duke@435: fprintf(fp,"const TypePtr *%sNode::adr_type() const { return TypePtr::BOTTOM; }\n", instr->_ident); duke@435: fprintf(fp,"const MachOper* %sNode::memory_operand() const { return (MachOper*)-1; }\n", instr->_ident); duke@435: } else { duke@435: fprintf(fp,"const MachOper* %sNode::memory_operand() const { return _opnds[%d]; }\n", instr->_ident, memory_operand); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Get the length of the longest identifier duke@435: int max_ident_len = 0; duke@435: _instructions.reset(); duke@435: duke@435: for ( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: if (instr->_ins_pipe && _pipeline->_classlist.search(instr->_ins_pipe)) { duke@435: int ident_len = (int)strlen(instr->_ident); duke@435: if( max_ident_len < ident_len ) duke@435: max_ident_len = ident_len; duke@435: } duke@435: } duke@435: duke@435: // Emit specifically for Node(s) duke@435: fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline_class() { return %s; }\n", duke@435: max_ident_len, "Node", _pipeline ? "(&pipeline_class_Zero_Instructions)" : "NULL"); duke@435: fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline() const { return %s; }\n", duke@435: max_ident_len, "Node", _pipeline ? "(&pipeline_class_Zero_Instructions)" : "NULL"); duke@435: fprintf(_CPP_PIPELINE_file._fp, "\n"); duke@435: duke@435: fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline_class() { return %s; }\n", duke@435: max_ident_len, "MachNode", _pipeline ? "(&pipeline_class_Unknown_Instructions)" : "NULL"); duke@435: fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline() const { return pipeline_class(); }\n", duke@435: max_ident_len, "MachNode"); duke@435: fprintf(_CPP_PIPELINE_file._fp, "\n"); duke@435: duke@435: // Output the definitions for machine node specific pipeline data duke@435: _machnodes.reset(); duke@435: duke@435: for ( ; (machnode = (MachNodeForm*)_machnodes.iter()) != NULL; ) { duke@435: fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %sNode::pipeline() const { return (&pipeline_class_%03d); }\n", duke@435: machnode->_ident, ((class PipeClassForm *)_pipeline->_classdict[machnode->_machnode_pipe])->_num); duke@435: } duke@435: duke@435: fprintf(_CPP_PIPELINE_file._fp, "\n"); duke@435: duke@435: // Output the definitions for instruction pipeline static data references duke@435: _instructions.reset(); duke@435: duke@435: for ( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: if (instr->_ins_pipe && _pipeline->_classlist.search(instr->_ins_pipe)) { duke@435: fprintf(_CPP_PIPELINE_file._fp, "\n"); duke@435: fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*sNode::pipeline_class() { return (&pipeline_class_%03d); }\n", duke@435: max_ident_len, instr->_ident, ((class PipeClassForm *)_pipeline->_classdict[instr->_ins_pipe])->_num); duke@435: fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*sNode::pipeline() const { return (&pipeline_class_%03d); }\n", duke@435: max_ident_len, instr->_ident, ((class PipeClassForm *)_pipeline->_classdict[instr->_ins_pipe])->_num); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: // -------------------------------- maps ------------------------------------ duke@435: duke@435: // Information needed to generate the ReduceOp mapping for the DFA duke@435: class OutputReduceOp : public OutputMap { duke@435: public: duke@435: OutputReduceOp(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD) duke@435: : OutputMap(hpp, cpp, globals, AD) {}; duke@435: duke@435: void declaration() { fprintf(_hpp, "extern const int reduceOp[];\n"); } duke@435: void definition() { fprintf(_cpp, "const int reduceOp[] = {\n"); } duke@435: void closing() { fprintf(_cpp, " 0 // no trailing comma\n"); duke@435: OutputMap::closing(); duke@435: } duke@435: void map(OpClassForm &opc) { duke@435: const char *reduce = opc._ident; duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: void map(OperandForm &oper) { duke@435: // Most operands without match rules, e.g. eFlagsReg, do not have a result operand duke@435: const char *reduce = (oper._matrule ? oper.reduce_result() : NULL); duke@435: // operand stackSlot does not have a match rule, but produces a stackSlot duke@435: if( oper.is_user_name_for_sReg() != Form::none ) reduce = oper.reduce_result(); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: void map(InstructForm &inst) { duke@435: const char *reduce = (inst._matrule ? inst.reduce_result() : NULL); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: void map(char *reduce) { duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: }; duke@435: duke@435: // Information needed to generate the LeftOp mapping for the DFA duke@435: class OutputLeftOp : public OutputMap { duke@435: public: duke@435: OutputLeftOp(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD) duke@435: : OutputMap(hpp, cpp, globals, AD) {}; duke@435: duke@435: void declaration() { fprintf(_hpp, "extern const int leftOp[];\n"); } duke@435: void definition() { fprintf(_cpp, "const int leftOp[] = {\n"); } duke@435: void closing() { fprintf(_cpp, " 0 // no trailing comma\n"); duke@435: OutputMap::closing(); duke@435: } duke@435: void map(OpClassForm &opc) { fprintf(_cpp, " 0"); } duke@435: void map(OperandForm &oper) { duke@435: const char *reduce = oper.reduce_left(_globals); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: void map(char *name) { duke@435: const char *reduce = _AD.reduceLeft(name); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: void map(InstructForm &inst) { duke@435: const char *reduce = inst.reduce_left(_globals); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Information needed to generate the RightOp mapping for the DFA duke@435: class OutputRightOp : public OutputMap { duke@435: public: duke@435: OutputRightOp(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD) duke@435: : OutputMap(hpp, cpp, globals, AD) {}; duke@435: duke@435: void declaration() { fprintf(_hpp, "extern const int rightOp[];\n"); } duke@435: void definition() { fprintf(_cpp, "const int rightOp[] = {\n"); } duke@435: void closing() { fprintf(_cpp, " 0 // no trailing comma\n"); duke@435: OutputMap::closing(); duke@435: } duke@435: void map(OpClassForm &opc) { fprintf(_cpp, " 0"); } duke@435: void map(OperandForm &oper) { duke@435: const char *reduce = oper.reduce_right(_globals); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: void map(char *name) { duke@435: const char *reduce = _AD.reduceRight(name); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: void map(InstructForm &inst) { duke@435: const char *reduce = inst.reduce_right(_globals); duke@435: if( reduce ) fprintf(_cpp, " %s_rule", reduce); duke@435: else fprintf(_cpp, " 0"); duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Information needed to generate the Rule names for the DFA duke@435: class OutputRuleName : public OutputMap { duke@435: public: duke@435: OutputRuleName(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD) duke@435: : OutputMap(hpp, cpp, globals, AD) {}; duke@435: duke@435: void declaration() { fprintf(_hpp, "extern const char *ruleName[];\n"); } duke@435: void definition() { fprintf(_cpp, "const char *ruleName[] = {\n"); } duke@435: void closing() { fprintf(_cpp, " \"no trailing comma\"\n"); duke@435: OutputMap::closing(); duke@435: } duke@435: void map(OpClassForm &opc) { fprintf(_cpp, " \"%s\"", _AD.machOperEnum(opc._ident) ); } duke@435: void map(OperandForm &oper) { fprintf(_cpp, " \"%s\"", _AD.machOperEnum(oper._ident) ); } duke@435: void map(char *name) { fprintf(_cpp, " \"%s\"", name ? name : "0"); } duke@435: void map(InstructForm &inst){ fprintf(_cpp, " \"%s\"", inst._ident ? inst._ident : "0"); } duke@435: }; duke@435: duke@435: duke@435: // Information needed to generate the swallowed mapping for the DFA duke@435: class OutputSwallowed : public OutputMap { duke@435: public: duke@435: OutputSwallowed(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD) duke@435: : OutputMap(hpp, cpp, globals, AD) {}; duke@435: duke@435: void declaration() { fprintf(_hpp, "extern const bool swallowed[];\n"); } duke@435: void definition() { fprintf(_cpp, "const bool swallowed[] = {\n"); } duke@435: void closing() { fprintf(_cpp, " false // no trailing comma\n"); duke@435: OutputMap::closing(); duke@435: } duke@435: void map(OperandForm &oper) { // Generate the entry for this opcode duke@435: const char *swallowed = oper.swallowed(_globals) ? "true" : "false"; duke@435: fprintf(_cpp, " %s", swallowed); duke@435: } duke@435: void map(OpClassForm &opc) { fprintf(_cpp, " false"); } duke@435: void map(char *name) { fprintf(_cpp, " false"); } duke@435: void map(InstructForm &inst){ fprintf(_cpp, " false"); } duke@435: }; duke@435: duke@435: duke@435: // Information needed to generate the decision array for instruction chain rule duke@435: class OutputInstChainRule : public OutputMap { duke@435: public: duke@435: OutputInstChainRule(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD) duke@435: : OutputMap(hpp, cpp, globals, AD) {}; duke@435: duke@435: void declaration() { fprintf(_hpp, "extern const bool instruction_chain_rule[];\n"); } duke@435: void definition() { fprintf(_cpp, "const bool instruction_chain_rule[] = {\n"); } duke@435: void closing() { fprintf(_cpp, " false // no trailing comma\n"); duke@435: OutputMap::closing(); duke@435: } duke@435: void map(OpClassForm &opc) { fprintf(_cpp, " false"); } duke@435: void map(OperandForm &oper) { fprintf(_cpp, " false"); } duke@435: void map(char *name) { fprintf(_cpp, " false"); } duke@435: void map(InstructForm &inst) { // Check for simple chain rule duke@435: const char *chain = inst.is_simple_chain_rule(_globals) ? "true" : "false"; duke@435: fprintf(_cpp, " %s", chain); duke@435: } duke@435: }; duke@435: duke@435: duke@435: //---------------------------build_map------------------------------------ duke@435: // Build mapping from enumeration for densely packed operands duke@435: // TO result and child types. duke@435: void ArchDesc::build_map(OutputMap &map) { duke@435: FILE *fp_hpp = map.decl_file(); duke@435: FILE *fp_cpp = map.def_file(); duke@435: int idx = 0; duke@435: OperandForm *op; duke@435: OpClassForm *opc; duke@435: InstructForm *inst; duke@435: duke@435: // Construct this mapping duke@435: map.declaration(); duke@435: fprintf(fp_cpp,"\n"); duke@435: map.definition(); duke@435: duke@435: // Output the mapping for operands duke@435: map.record_position(OutputMap::BEGIN_OPERANDS, idx ); duke@435: _operands.reset(); duke@435: for(; (op = (OperandForm*)_operands.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( op->ideal_only() ) continue; duke@435: duke@435: // Generate the entry for this opcode duke@435: map.map(*op); fprintf(fp_cpp, ", // %d\n", idx); duke@435: ++idx; duke@435: }; duke@435: fprintf(fp_cpp, " // last operand\n"); duke@435: duke@435: // Place all user-defined operand classes into the mapping duke@435: map.record_position(OutputMap::BEGIN_OPCLASSES, idx ); duke@435: _opclass.reset(); duke@435: for(; (opc = (OpClassForm*)_opclass.iter()) != NULL; ) { duke@435: map.map(*opc); fprintf(fp_cpp, ", // %d\n", idx); duke@435: ++idx; duke@435: }; duke@435: fprintf(fp_cpp, " // last operand class\n"); duke@435: duke@435: // Place all internally defined operands into the mapping duke@435: map.record_position(OutputMap::BEGIN_INTERNALS, idx ); duke@435: _internalOpNames.reset(); duke@435: char *name = NULL; duke@435: for(; (name = (char *)_internalOpNames.iter()) != NULL; ) { duke@435: map.map(name); fprintf(fp_cpp, ", // %d\n", idx); duke@435: ++idx; duke@435: }; duke@435: fprintf(fp_cpp, " // last internally defined operand\n"); duke@435: duke@435: // Place all user-defined instructions into the mapping duke@435: if( map.do_instructions() ) { duke@435: map.record_position(OutputMap::BEGIN_INSTRUCTIONS, idx ); duke@435: // Output all simple instruction chain rules first duke@435: map.record_position(OutputMap::BEGIN_INST_CHAIN_RULES, idx ); duke@435: { duke@435: _instructions.reset(); duke@435: for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( inst->ideal_only() ) continue; duke@435: if ( ! inst->is_simple_chain_rule(_globalNames) ) continue; duke@435: if ( inst->rematerialize(_globalNames, get_registers()) ) continue; duke@435: duke@435: map.map(*inst); fprintf(fp_cpp, ", // %d\n", idx); duke@435: ++idx; duke@435: }; duke@435: map.record_position(OutputMap::BEGIN_REMATERIALIZE, idx ); duke@435: _instructions.reset(); duke@435: for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( inst->ideal_only() ) continue; duke@435: if ( ! inst->is_simple_chain_rule(_globalNames) ) continue; duke@435: if ( ! inst->rematerialize(_globalNames, get_registers()) ) continue; duke@435: duke@435: map.map(*inst); fprintf(fp_cpp, ", // %d\n", idx); duke@435: ++idx; duke@435: }; duke@435: map.record_position(OutputMap::END_INST_CHAIN_RULES, idx ); duke@435: } duke@435: // Output all instructions that are NOT simple chain rules duke@435: { duke@435: _instructions.reset(); duke@435: for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( inst->ideal_only() ) continue; duke@435: if ( inst->is_simple_chain_rule(_globalNames) ) continue; duke@435: if ( ! inst->rematerialize(_globalNames, get_registers()) ) continue; duke@435: duke@435: map.map(*inst); fprintf(fp_cpp, ", // %d\n", idx); duke@435: ++idx; duke@435: }; duke@435: map.record_position(OutputMap::END_REMATERIALIZE, idx ); duke@435: _instructions.reset(); duke@435: for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( inst->ideal_only() ) continue; duke@435: if ( inst->is_simple_chain_rule(_globalNames) ) continue; duke@435: if ( inst->rematerialize(_globalNames, get_registers()) ) continue; duke@435: duke@435: map.map(*inst); fprintf(fp_cpp, ", // %d\n", idx); duke@435: ++idx; duke@435: }; duke@435: } duke@435: fprintf(fp_cpp, " // last instruction\n"); duke@435: map.record_position(OutputMap::END_INSTRUCTIONS, idx ); duke@435: } duke@435: // Finish defining table duke@435: map.closing(); duke@435: }; duke@435: duke@435: duke@435: // Helper function for buildReduceMaps duke@435: char reg_save_policy(const char *calling_convention) { duke@435: char callconv; duke@435: duke@435: if (!strcmp(calling_convention, "NS")) callconv = 'N'; duke@435: else if (!strcmp(calling_convention, "SOE")) callconv = 'E'; duke@435: else if (!strcmp(calling_convention, "SOC")) callconv = 'C'; duke@435: else if (!strcmp(calling_convention, "AS")) callconv = 'A'; duke@435: else callconv = 'Z'; duke@435: duke@435: return callconv; duke@435: } duke@435: duke@435: //---------------------------generate_assertion_checks------------------- duke@435: void ArchDesc::generate_adlc_verification(FILE *fp_cpp) { duke@435: fprintf(fp_cpp, "\n"); duke@435: duke@435: fprintf(fp_cpp, "#ifndef PRODUCT\n"); duke@435: fprintf(fp_cpp, "void Compile::adlc_verification() {\n"); duke@435: globalDefs().print_asserts(fp_cpp); duke@435: fprintf(fp_cpp, "}\n"); duke@435: fprintf(fp_cpp, "#endif\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: } duke@435: duke@435: //---------------------------addSourceBlocks----------------------------- duke@435: void ArchDesc::addSourceBlocks(FILE *fp_cpp) { duke@435: if (_source.count() > 0) duke@435: _source.output(fp_cpp); duke@435: duke@435: generate_adlc_verification(fp_cpp); duke@435: } duke@435: //---------------------------addHeaderBlocks----------------------------- duke@435: void ArchDesc::addHeaderBlocks(FILE *fp_hpp) { duke@435: if (_header.count() > 0) duke@435: _header.output(fp_hpp); duke@435: } duke@435: //-------------------------addPreHeaderBlocks---------------------------- duke@435: void ArchDesc::addPreHeaderBlocks(FILE *fp_hpp) { duke@435: // Output #defines from definition block duke@435: globalDefs().print_defines(fp_hpp); duke@435: duke@435: if (_pre_header.count() > 0) duke@435: _pre_header.output(fp_hpp); duke@435: } duke@435: duke@435: //---------------------------buildReduceMaps----------------------------- duke@435: // Build mapping from enumeration for densely packed operands duke@435: // TO result and child types. duke@435: void ArchDesc::buildReduceMaps(FILE *fp_hpp, FILE *fp_cpp) { duke@435: RegDef *rdef; duke@435: RegDef *next; duke@435: duke@435: // The emit bodies currently require functions defined in the source block. duke@435: duke@435: // Build external declarations for mappings duke@435: fprintf(fp_hpp, "\n"); duke@435: fprintf(fp_hpp, "extern const char register_save_policy[];\n"); duke@435: fprintf(fp_hpp, "extern const char c_reg_save_policy[];\n"); duke@435: fprintf(fp_hpp, "extern const int register_save_type[];\n"); duke@435: fprintf(fp_hpp, "\n"); duke@435: duke@435: // Construct Save-Policy array duke@435: fprintf(fp_cpp, "// Map from machine-independent register number to register_save_policy\n"); duke@435: fprintf(fp_cpp, "const char register_save_policy[] = {\n"); duke@435: _register->reset_RegDefs(); duke@435: for( rdef = _register->iter_RegDefs(); rdef != NULL; rdef = next ) { duke@435: next = _register->iter_RegDefs(); duke@435: char policy = reg_save_policy(rdef->_callconv); duke@435: const char *comma = (next != NULL) ? "," : " // no trailing comma"; duke@435: fprintf(fp_cpp, " '%c'%s\n", policy, comma); duke@435: } duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: duke@435: // Construct Native Save-Policy array duke@435: fprintf(fp_cpp, "// Map from machine-independent register number to c_reg_save_policy\n"); duke@435: fprintf(fp_cpp, "const char c_reg_save_policy[] = {\n"); duke@435: _register->reset_RegDefs(); duke@435: for( rdef = _register->iter_RegDefs(); rdef != NULL; rdef = next ) { duke@435: next = _register->iter_RegDefs(); duke@435: char policy = reg_save_policy(rdef->_c_conv); duke@435: const char *comma = (next != NULL) ? "," : " // no trailing comma"; duke@435: fprintf(fp_cpp, " '%c'%s\n", policy, comma); duke@435: } duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: duke@435: // Construct Register Save Type array duke@435: fprintf(fp_cpp, "// Map from machine-independent register number to register_save_type\n"); duke@435: fprintf(fp_cpp, "const int register_save_type[] = {\n"); duke@435: _register->reset_RegDefs(); duke@435: for( rdef = _register->iter_RegDefs(); rdef != NULL; rdef = next ) { duke@435: next = _register->iter_RegDefs(); duke@435: const char *comma = (next != NULL) ? "," : " // no trailing comma"; duke@435: fprintf(fp_cpp, " %s%s\n", rdef->_idealtype, comma); duke@435: } duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: duke@435: // Construct the table for reduceOp duke@435: OutputReduceOp output_reduce_op(fp_hpp, fp_cpp, _globalNames, *this); duke@435: build_map(output_reduce_op); duke@435: // Construct the table for leftOp duke@435: OutputLeftOp output_left_op(fp_hpp, fp_cpp, _globalNames, *this); duke@435: build_map(output_left_op); duke@435: // Construct the table for rightOp duke@435: OutputRightOp output_right_op(fp_hpp, fp_cpp, _globalNames, *this); duke@435: build_map(output_right_op); duke@435: // Construct the table of rule names duke@435: OutputRuleName output_rule_name(fp_hpp, fp_cpp, _globalNames, *this); duke@435: build_map(output_rule_name); duke@435: // Construct the boolean table for subsumed operands duke@435: OutputSwallowed output_swallowed(fp_hpp, fp_cpp, _globalNames, *this); duke@435: build_map(output_swallowed); duke@435: // // // Preserve in case we decide to use this table instead of another duke@435: //// Construct the boolean table for instruction chain rules duke@435: //OutputInstChainRule output_inst_chain(fp_hpp, fp_cpp, _globalNames, *this); duke@435: //build_map(output_inst_chain); duke@435: duke@435: } duke@435: duke@435: duke@435: //---------------------------buildMachOperGenerator--------------------------- duke@435: duke@435: // Recurse through match tree, building path through corresponding state tree, duke@435: // Until we reach the constant we are looking for. duke@435: static void path_to_constant(FILE *fp, FormDict &globals, duke@435: MatchNode *mnode, uint idx) { duke@435: if ( ! mnode) return; duke@435: duke@435: unsigned position = 0; duke@435: const char *result = NULL; duke@435: const char *name = NULL; duke@435: const char *optype = NULL; duke@435: duke@435: // Base Case: access constant in ideal node linked to current state node duke@435: // Each type of constant has its own access function duke@435: if ( (mnode->_lChild == NULL) && (mnode->_rChild == NULL) duke@435: && mnode->base_operand(position, globals, result, name, optype) ) { duke@435: if ( strcmp(optype,"ConI") == 0 ) { duke@435: fprintf(fp, "_leaf->get_int()"); duke@435: } else if ( (strcmp(optype,"ConP") == 0) ) { duke@435: fprintf(fp, "_leaf->bottom_type()->is_ptr()"); duke@435: } else if ( (strcmp(optype,"ConF") == 0) ) { duke@435: fprintf(fp, "_leaf->getf()"); duke@435: } else if ( (strcmp(optype,"ConD") == 0) ) { duke@435: fprintf(fp, "_leaf->getd()"); duke@435: } else if ( (strcmp(optype,"ConL") == 0) ) { duke@435: fprintf(fp, "_leaf->get_long()"); duke@435: } else if ( (strcmp(optype,"Con")==0) ) { duke@435: // !!!!! - Update if adding a machine-independent constant type duke@435: fprintf(fp, "_leaf->get_int()"); duke@435: assert( false, "Unsupported constant type, pointer or indefinite"); duke@435: } else if ( (strcmp(optype,"Bool") == 0) ) { duke@435: fprintf(fp, "_leaf->as_Bool()->_test._test"); duke@435: } else { duke@435: assert( false, "Unsupported constant type"); duke@435: } duke@435: return; duke@435: } duke@435: duke@435: // If constant is in left child, build path and recurse duke@435: uint lConsts = (mnode->_lChild) ? (mnode->_lChild->num_consts(globals) ) : 0; duke@435: uint rConsts = (mnode->_rChild) ? (mnode->_rChild->num_consts(globals) ) : 0; duke@435: if ( (mnode->_lChild) && (lConsts > idx) ) { duke@435: fprintf(fp, "_kids[0]->"); duke@435: path_to_constant(fp, globals, mnode->_lChild, idx); duke@435: return; duke@435: } duke@435: // If constant is in right child, build path and recurse duke@435: if ( (mnode->_rChild) && (rConsts > (idx - lConsts) ) ) { duke@435: idx = idx - lConsts; duke@435: fprintf(fp, "_kids[1]->"); duke@435: path_to_constant(fp, globals, mnode->_rChild, idx); duke@435: return; duke@435: } duke@435: assert( false, "ShouldNotReachHere()"); duke@435: } duke@435: duke@435: // Generate code that is executed when generating a specific Machine Operand duke@435: static void genMachOperCase(FILE *fp, FormDict &globalNames, ArchDesc &AD, duke@435: OperandForm &op) { duke@435: const char *opName = op._ident; duke@435: const char *opEnumName = AD.machOperEnum(opName); duke@435: uint num_consts = op.num_consts(globalNames); duke@435: duke@435: // Generate the case statement for this opcode duke@435: fprintf(fp, " case %s:", opEnumName); duke@435: fprintf(fp, "\n return new (C) %sOper(", opName); duke@435: // Access parameters for constructor from the stat object duke@435: // duke@435: // Build access to condition code value duke@435: if ( (num_consts > 0) ) { duke@435: uint i = 0; duke@435: path_to_constant(fp, globalNames, op._matrule, i); duke@435: for ( i = 1; i < num_consts; ++i ) { duke@435: fprintf(fp, ", "); duke@435: path_to_constant(fp, globalNames, op._matrule, i); duke@435: } duke@435: } duke@435: fprintf(fp, " );\n"); duke@435: } duke@435: duke@435: duke@435: // Build switch to invoke "new" MachNode or MachOper duke@435: void ArchDesc::buildMachOperGenerator(FILE *fp_cpp) { duke@435: int idx = 0; duke@435: duke@435: // Build switch to invoke 'new' for a specific MachOper duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, duke@435: "//------------------------- MachOper Generator ---------------\n"); duke@435: fprintf(fp_cpp, duke@435: "// A switch statement on the dense-packed user-defined type system\n" duke@435: "// that invokes 'new' on the corresponding class constructor.\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "MachOper *State::MachOperGenerator"); duke@435: fprintf(fp_cpp, "(int opcode, Compile* C)"); duke@435: fprintf(fp_cpp, "{\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, " switch(opcode) {\n"); duke@435: duke@435: // Place all user-defined operands into the mapping duke@435: _operands.reset(); duke@435: int opIndex = 0; duke@435: OperandForm *op; duke@435: for( ; (op = (OperandForm*)_operands.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( op->ideal_only() ) continue; duke@435: duke@435: genMachOperCase(fp_cpp, _globalNames, *this, *op); duke@435: }; duke@435: duke@435: // Do not iterate over operand classes for the operand generator!!! duke@435: duke@435: // Place all internal operands into the mapping duke@435: _internalOpNames.reset(); duke@435: const char *iopn; duke@435: for( ; (iopn = _internalOpNames.iter()) != NULL; ) { duke@435: const char *opEnumName = machOperEnum(iopn); duke@435: // Generate the case statement for this opcode duke@435: fprintf(fp_cpp, " case %s:", opEnumName); duke@435: fprintf(fp_cpp, " return NULL;\n"); duke@435: }; duke@435: duke@435: // Generate the default case for switch(opcode) duke@435: fprintf(fp_cpp, " \n"); duke@435: fprintf(fp_cpp, " default:\n"); duke@435: fprintf(fp_cpp, " fprintf(stderr, \"Default MachOper Generator invoked for: \\n\");\n"); duke@435: fprintf(fp_cpp, " fprintf(stderr, \" opcode = %cd\\n\", opcode);\n", '%'); duke@435: fprintf(fp_cpp, " break;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: duke@435: // Generate the closing for method Matcher::MachOperGenerator duke@435: fprintf(fp_cpp, " return NULL;\n"); duke@435: fprintf(fp_cpp, "};\n"); duke@435: } duke@435: duke@435: duke@435: //---------------------------buildMachNode------------------------------------- duke@435: // Build a new MachNode, for MachNodeGenerator or cisc-spilling duke@435: void ArchDesc::buildMachNode(FILE *fp_cpp, InstructForm *inst, const char *indent) { duke@435: const char *opType = NULL; duke@435: const char *opClass = inst->_ident; duke@435: duke@435: // Create the MachNode object duke@435: fprintf(fp_cpp, "%s %sNode *node = new (C) %sNode();\n",indent, opClass,opClass); duke@435: duke@435: if ( (inst->num_post_match_opnds() != 0) ) { duke@435: // Instruction that contains operands which are not in match rule. duke@435: // duke@435: // Check if the first post-match component may be an interesting def duke@435: bool dont_care = false; duke@435: ComponentList &comp_list = inst->_components; duke@435: Component *comp = NULL; duke@435: comp_list.reset(); duke@435: if ( comp_list.match_iter() != NULL ) dont_care = true; duke@435: duke@435: // Insert operands that are not in match-rule. duke@435: // Only insert a DEF if the do_care flag is set duke@435: comp_list.reset(); duke@435: while ( comp = comp_list.post_match_iter() ) { duke@435: // Check if we don't care about DEFs or KILLs that are not USEs duke@435: if ( dont_care && (! comp->isa(Component::USE)) ) { duke@435: continue; duke@435: } duke@435: dont_care = true; duke@435: // For each operand not in the match rule, call MachOperGenerator duke@435: // with the enum for the opcode that needs to be built duke@435: // and the node just built, the parent of the operand. duke@435: ComponentList clist = inst->_components; duke@435: int index = clist.operand_position(comp->_name, comp->_usedef); duke@435: const char *opcode = machOperEnum(comp->_type); duke@435: const char *parent = "node"; duke@435: fprintf(fp_cpp, "%s node->set_opnd_array(%d, ", indent, index); duke@435: fprintf(fp_cpp, "MachOperGenerator(%s, C));\n", opcode); duke@435: } duke@435: } duke@435: else if ( inst->is_chain_of_constant(_globalNames, opType) ) { duke@435: // An instruction that chains from a constant! duke@435: // In this case, we need to subsume the constant into the node duke@435: // at operand position, oper_input_base(). duke@435: // duke@435: // Fill in the constant duke@435: fprintf(fp_cpp, "%s node->_opnd_array[%d] = ", indent, duke@435: inst->oper_input_base(_globalNames)); duke@435: // ##### duke@435: // Check for multiple constants and then fill them in. duke@435: // Just like MachOperGenerator duke@435: const char *opName = inst->_matrule->_rChild->_opType; duke@435: fprintf(fp_cpp, "new (C) %sOper(", opName); duke@435: // Grab operand form duke@435: OperandForm *op = (_globalNames[opName])->is_operand(); duke@435: // Look up the number of constants duke@435: uint num_consts = op->num_consts(_globalNames); duke@435: if ( (num_consts > 0) ) { duke@435: uint i = 0; duke@435: path_to_constant(fp_cpp, _globalNames, op->_matrule, i); duke@435: for ( i = 1; i < num_consts; ++i ) { duke@435: fprintf(fp_cpp, ", "); duke@435: path_to_constant(fp_cpp, _globalNames, op->_matrule, i); duke@435: } duke@435: } duke@435: fprintf(fp_cpp, " );\n"); duke@435: // ##### duke@435: } duke@435: duke@435: // Fill in the bottom_type where requested duke@435: if ( inst->captures_bottom_type() ) { duke@435: fprintf(fp_cpp, "%s node->_bottom_type = _leaf->bottom_type();\n", indent); duke@435: } duke@435: if( inst->is_ideal_if() ) { duke@435: fprintf(fp_cpp, "%s node->_prob = _leaf->as_If()->_prob;\n", indent); duke@435: fprintf(fp_cpp, "%s node->_fcnt = _leaf->as_If()->_fcnt;\n", indent); duke@435: } duke@435: if( inst->is_ideal_fastlock() ) { duke@435: fprintf(fp_cpp, "%s node->_counters = _leaf->as_FastLock()->counters();\n", indent); duke@435: } duke@435: duke@435: } duke@435: duke@435: //---------------------------declare_cisc_version------------------------------ duke@435: // Build CISC version of this instruction duke@435: void InstructForm::declare_cisc_version(ArchDesc &AD, FILE *fp_hpp) { duke@435: if( AD.can_cisc_spill() ) { duke@435: InstructForm *inst_cisc = cisc_spill_alternate(); duke@435: if (inst_cisc != NULL) { duke@435: fprintf(fp_hpp, " virtual int cisc_operand() const { return %d; }\n", cisc_spill_operand()); duke@435: fprintf(fp_hpp, " virtual MachNode *cisc_version(int offset, Compile* C);\n"); duke@435: fprintf(fp_hpp, " virtual void use_cisc_RegMask();\n"); duke@435: fprintf(fp_hpp, " virtual const RegMask *cisc_RegMask() const { return _cisc_RegMask; }\n"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: //---------------------------define_cisc_version------------------------------- duke@435: // Build CISC version of this instruction duke@435: bool InstructForm::define_cisc_version(ArchDesc &AD, FILE *fp_cpp) { duke@435: InstructForm *inst_cisc = this->cisc_spill_alternate(); duke@435: if( AD.can_cisc_spill() && (inst_cisc != NULL) ) { duke@435: const char *name = inst_cisc->_ident; duke@435: assert( inst_cisc->num_opnds() == this->num_opnds(), "Must have same number of operands"); duke@435: OperandForm *cisc_oper = AD.cisc_spill_operand(); duke@435: assert( cisc_oper != NULL, "insanity check"); duke@435: const char *cisc_oper_name = cisc_oper->_ident; duke@435: assert( cisc_oper_name != NULL, "insanity check"); duke@435: // duke@435: // Set the correct reg_mask_or_stack for the cisc operand duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "void %sNode::use_cisc_RegMask() {\n", this->_ident); duke@435: // Lookup the correct reg_mask_or_stack duke@435: const char *reg_mask_name = cisc_reg_mask_name(); duke@435: fprintf(fp_cpp, " _cisc_RegMask = &STACK_OR_%s;\n", reg_mask_name); duke@435: fprintf(fp_cpp, "}\n"); duke@435: // duke@435: // Construct CISC version of this instruction duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "// Build CISC version of this instruction\n"); duke@435: fprintf(fp_cpp, "MachNode *%sNode::cisc_version( int offset, Compile* C ) {\n", this->_ident); duke@435: // Create the MachNode object duke@435: fprintf(fp_cpp, " %sNode *node = new (C) %sNode();\n", name, name); duke@435: // Fill in the bottom_type where requested duke@435: if ( this->captures_bottom_type() ) { duke@435: fprintf(fp_cpp, " node->_bottom_type = bottom_type();\n"); duke@435: } duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, " // Copy _idx, inputs and operands to new node\n"); duke@435: fprintf(fp_cpp, " fill_new_machnode(node, C);\n"); duke@435: // Construct operand to access [stack_pointer + offset] duke@435: fprintf(fp_cpp, " // Construct operand to access [stack_pointer + offset]\n"); duke@435: fprintf(fp_cpp, " node->set_opnd_array(cisc_operand(), new (C) %sOper(offset));\n", cisc_oper_name); duke@435: fprintf(fp_cpp, "\n"); duke@435: duke@435: // Return result and exit scope duke@435: fprintf(fp_cpp, " return node;\n"); duke@435: fprintf(fp_cpp, "}\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: //---------------------------declare_short_branch_methods---------------------- duke@435: // Build prototypes for short branch methods duke@435: void InstructForm::declare_short_branch_methods(FILE *fp_hpp) { duke@435: if (has_short_branch_form()) { duke@435: fprintf(fp_hpp, " virtual MachNode *short_branch_version(Compile* C);\n"); duke@435: } duke@435: } duke@435: duke@435: //---------------------------define_short_branch_methods----------------------- duke@435: // Build definitions for short branch methods duke@435: bool InstructForm::define_short_branch_methods(FILE *fp_cpp) { duke@435: if (has_short_branch_form()) { duke@435: InstructForm *short_branch = short_branch_form(); duke@435: const char *name = short_branch->_ident; duke@435: duke@435: // Construct short_branch_version() method. duke@435: fprintf(fp_cpp, "// Build short branch version of this instruction\n"); duke@435: fprintf(fp_cpp, "MachNode *%sNode::short_branch_version(Compile* C) {\n", this->_ident); duke@435: // Create the MachNode object duke@435: fprintf(fp_cpp, " %sNode *node = new (C) %sNode();\n", name, name); duke@435: if( is_ideal_if() ) { duke@435: fprintf(fp_cpp, " node->_prob = _prob;\n"); duke@435: fprintf(fp_cpp, " node->_fcnt = _fcnt;\n"); duke@435: } duke@435: // Fill in the bottom_type where requested duke@435: if ( this->captures_bottom_type() ) { duke@435: fprintf(fp_cpp, " node->_bottom_type = bottom_type();\n"); duke@435: } duke@435: duke@435: fprintf(fp_cpp, "\n"); duke@435: // Short branch version must use same node index for access duke@435: // through allocator's tables duke@435: fprintf(fp_cpp, " // Copy _idx, inputs and operands to new node\n"); duke@435: fprintf(fp_cpp, " fill_new_machnode(node, C);\n"); duke@435: duke@435: // Return result and exit scope duke@435: fprintf(fp_cpp, " return node;\n"); duke@435: fprintf(fp_cpp, "}\n"); duke@435: fprintf(fp_cpp,"\n"); duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: duke@435: //---------------------------buildMachNodeGenerator---------------------------- duke@435: // Build switch to invoke appropriate "new" MachNode for an opcode duke@435: void ArchDesc::buildMachNodeGenerator(FILE *fp_cpp) { duke@435: duke@435: // Build switch to invoke 'new' for a specific MachNode duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, duke@435: "//------------------------- MachNode Generator ---------------\n"); duke@435: fprintf(fp_cpp, duke@435: "// A switch statement on the dense-packed user-defined type system\n" duke@435: "// that invokes 'new' on the corresponding class constructor.\n"); duke@435: fprintf(fp_cpp, "\n"); duke@435: fprintf(fp_cpp, "MachNode *State::MachNodeGenerator"); duke@435: fprintf(fp_cpp, "(int opcode, Compile* C)"); duke@435: fprintf(fp_cpp, "{\n"); duke@435: fprintf(fp_cpp, " switch(opcode) {\n"); duke@435: duke@435: // Provide constructor for all user-defined instructions duke@435: _instructions.reset(); duke@435: int opIndex = operandFormCount(); duke@435: InstructForm *inst; duke@435: for( ; (inst = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure that matrule is defined. duke@435: if ( inst->_matrule == NULL ) continue; duke@435: duke@435: int opcode = opIndex++; duke@435: const char *opClass = inst->_ident; duke@435: char *opType = NULL; duke@435: duke@435: // Generate the case statement for this instruction duke@435: fprintf(fp_cpp, " case %s_rule:", opClass); duke@435: duke@435: // Start local scope duke@435: fprintf(fp_cpp, " {\n"); duke@435: // Generate code to construct the new MachNode duke@435: buildMachNode(fp_cpp, inst, " "); duke@435: // Return result and exit scope duke@435: fprintf(fp_cpp, " return node;\n"); duke@435: fprintf(fp_cpp, " }\n"); duke@435: } duke@435: duke@435: // Generate the default case for switch(opcode) duke@435: fprintf(fp_cpp, " \n"); duke@435: fprintf(fp_cpp, " default:\n"); duke@435: fprintf(fp_cpp, " fprintf(stderr, \"Default MachNode Generator invoked for: \\n\");\n"); duke@435: fprintf(fp_cpp, " fprintf(stderr, \" opcode = %cd\\n\", opcode);\n", '%'); duke@435: fprintf(fp_cpp, " break;\n"); duke@435: fprintf(fp_cpp, " };\n"); duke@435: duke@435: // Generate the closing for method Matcher::MachNodeGenerator duke@435: fprintf(fp_cpp, " return NULL;\n"); duke@435: fprintf(fp_cpp, "}\n"); duke@435: } duke@435: duke@435: duke@435: //---------------------------buildInstructMatchCheck-------------------------- duke@435: // Output the method to Matcher which checks whether or not a specific duke@435: // instruction has a matching rule for the host architecture. duke@435: void ArchDesc::buildInstructMatchCheck(FILE *fp_cpp) const { duke@435: fprintf(fp_cpp, "\n\n"); duke@435: fprintf(fp_cpp, "const bool Matcher::has_match_rule(int opcode) {\n"); duke@435: fprintf(fp_cpp, " assert(_last_machine_leaf < opcode && opcode < _last_opcode, \"opcode in range\");\n"); duke@435: fprintf(fp_cpp, " return _hasMatchRule[opcode];\n"); duke@435: fprintf(fp_cpp, "}\n\n"); duke@435: duke@435: fprintf(fp_cpp, "const bool Matcher::_hasMatchRule[_last_opcode] = {\n"); duke@435: int i; duke@435: for (i = 0; i < _last_opcode - 1; i++) { duke@435: fprintf(fp_cpp, " %-5s, // %s\n", duke@435: _has_match_rule[i] ? "true" : "false", duke@435: NodeClassNames[i]); duke@435: } duke@435: fprintf(fp_cpp, " %-5s // %s\n", duke@435: _has_match_rule[i] ? "true" : "false", duke@435: NodeClassNames[i]); duke@435: fprintf(fp_cpp, "};\n"); duke@435: } duke@435: duke@435: //---------------------------buildFrameMethods--------------------------------- duke@435: // Output the methods to Matcher which specify frame behavior duke@435: void ArchDesc::buildFrameMethods(FILE *fp_cpp) { duke@435: fprintf(fp_cpp,"\n\n"); duke@435: // Stack Direction duke@435: fprintf(fp_cpp,"bool Matcher::stack_direction() const { return %s; }\n\n", duke@435: _frame->_direction ? "true" : "false"); duke@435: // Sync Stack Slots duke@435: fprintf(fp_cpp,"int Compile::sync_stack_slots() const { return %s; }\n\n", duke@435: _frame->_sync_stack_slots); duke@435: // Java Stack Alignment duke@435: fprintf(fp_cpp,"uint Matcher::stack_alignment_in_bytes() { return %s; }\n\n", duke@435: _frame->_alignment); duke@435: // Java Return Address Location duke@435: fprintf(fp_cpp,"OptoReg::Name Matcher::return_addr() const {"); duke@435: if (_frame->_return_addr_loc) { duke@435: fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n", duke@435: _frame->_return_addr); duke@435: } duke@435: else { duke@435: fprintf(fp_cpp," return OptoReg::stack2reg(%s); }\n\n", duke@435: _frame->_return_addr); duke@435: } duke@435: // Java Stack Slot Preservation duke@435: fprintf(fp_cpp,"uint Compile::in_preserve_stack_slots() "); duke@435: fprintf(fp_cpp,"{ return %s; }\n\n", _frame->_in_preserve_slots); duke@435: // Top Of Stack Slot Preservation, for both Java and C duke@435: fprintf(fp_cpp,"uint Compile::out_preserve_stack_slots() "); duke@435: fprintf(fp_cpp,"{ return SharedRuntime::out_preserve_stack_slots(); }\n\n"); duke@435: // varargs C out slots killed duke@435: fprintf(fp_cpp,"uint Compile::varargs_C_out_slots_killed() const "); duke@435: fprintf(fp_cpp,"{ return %s; }\n\n", _frame->_varargs_C_out_slots_killed); duke@435: // Java Argument Position duke@435: fprintf(fp_cpp,"void Matcher::calling_convention(BasicType *sig_bt, VMRegPair *regs, uint length, bool is_outgoing) {\n"); duke@435: fprintf(fp_cpp,"%s\n", _frame->_calling_convention); duke@435: fprintf(fp_cpp,"}\n\n"); duke@435: // Native Argument Position duke@435: fprintf(fp_cpp,"void Matcher::c_calling_convention(BasicType *sig_bt, VMRegPair *regs, uint length) {\n"); duke@435: fprintf(fp_cpp,"%s\n", _frame->_c_calling_convention); duke@435: fprintf(fp_cpp,"}\n\n"); duke@435: // Java Return Value Location duke@435: fprintf(fp_cpp,"OptoRegPair Matcher::return_value(int ideal_reg, bool is_outgoing) {\n"); duke@435: fprintf(fp_cpp,"%s\n", _frame->_return_value); duke@435: fprintf(fp_cpp,"}\n\n"); duke@435: // Native Return Value Location duke@435: fprintf(fp_cpp,"OptoRegPair Matcher::c_return_value(int ideal_reg, bool is_outgoing) {\n"); duke@435: fprintf(fp_cpp,"%s\n", _frame->_c_return_value); duke@435: fprintf(fp_cpp,"}\n\n"); duke@435: duke@435: // Inline Cache Register, mask definition, and encoding duke@435: fprintf(fp_cpp,"OptoReg::Name Matcher::inline_cache_reg() {"); duke@435: fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n", duke@435: _frame->_inline_cache_reg); duke@435: fprintf(fp_cpp,"const RegMask &Matcher::inline_cache_reg_mask() {"); duke@435: fprintf(fp_cpp," return INLINE_CACHE_REG_mask; }\n\n"); duke@435: fprintf(fp_cpp,"int Matcher::inline_cache_reg_encode() {"); duke@435: fprintf(fp_cpp," return _regEncode[inline_cache_reg()]; }\n\n"); duke@435: duke@435: // Interpreter's Method Oop Register, mask definition, and encoding duke@435: fprintf(fp_cpp,"OptoReg::Name Matcher::interpreter_method_oop_reg() {"); duke@435: fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n", duke@435: _frame->_interpreter_method_oop_reg); duke@435: fprintf(fp_cpp,"const RegMask &Matcher::interpreter_method_oop_reg_mask() {"); duke@435: fprintf(fp_cpp," return INTERPRETER_METHOD_OOP_REG_mask; }\n\n"); duke@435: fprintf(fp_cpp,"int Matcher::interpreter_method_oop_reg_encode() {"); duke@435: fprintf(fp_cpp," return _regEncode[interpreter_method_oop_reg()]; }\n\n"); duke@435: duke@435: // Interpreter's Frame Pointer Register, mask definition, and encoding duke@435: fprintf(fp_cpp,"OptoReg::Name Matcher::interpreter_frame_pointer_reg() {"); duke@435: if (_frame->_interpreter_frame_pointer_reg == NULL) duke@435: fprintf(fp_cpp," return OptoReg::Bad; }\n\n"); duke@435: else duke@435: fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n", duke@435: _frame->_interpreter_frame_pointer_reg); duke@435: fprintf(fp_cpp,"const RegMask &Matcher::interpreter_frame_pointer_reg_mask() {"); duke@435: if (_frame->_interpreter_frame_pointer_reg == NULL) duke@435: fprintf(fp_cpp," static RegMask dummy; return dummy; }\n\n"); duke@435: else duke@435: fprintf(fp_cpp," return INTERPRETER_FRAME_POINTER_REG_mask; }\n\n"); duke@435: duke@435: // Frame Pointer definition duke@435: /* CNC - I can not contemplate having a different frame pointer between duke@435: Java and native code; makes my head hurt to think about it. duke@435: fprintf(fp_cpp,"OptoReg::Name Matcher::frame_pointer() const {"); duke@435: fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n", duke@435: _frame->_frame_pointer); duke@435: */ duke@435: // (Native) Frame Pointer definition duke@435: fprintf(fp_cpp,"OptoReg::Name Matcher::c_frame_pointer() const {"); duke@435: fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n", duke@435: _frame->_frame_pointer); duke@435: duke@435: // Number of callee-save + always-save registers for calling convention duke@435: fprintf(fp_cpp, "// Number of callee-save + always-save registers\n"); duke@435: fprintf(fp_cpp, "int Matcher::number_of_saved_registers() {\n"); duke@435: RegDef *rdef; duke@435: int nof_saved_registers = 0; duke@435: _register->reset_RegDefs(); duke@435: while( (rdef = _register->iter_RegDefs()) != NULL ) { duke@435: if( !strcmp(rdef->_callconv, "SOE") || !strcmp(rdef->_callconv, "AS") ) duke@435: ++nof_saved_registers; duke@435: } duke@435: fprintf(fp_cpp, " return %d;\n", nof_saved_registers); duke@435: fprintf(fp_cpp, "};\n\n"); duke@435: } duke@435: duke@435: duke@435: duke@435: duke@435: static int PrintAdlcCisc = 0; duke@435: //---------------------------identify_cisc_spilling---------------------------- duke@435: // Get info for the CISC_oracle and MachNode::cisc_version() duke@435: void ArchDesc::identify_cisc_spill_instructions() { duke@435: duke@435: // Find the user-defined operand for cisc-spilling duke@435: if( _frame->_cisc_spilling_operand_name != NULL ) { duke@435: const Form *form = _globalNames[_frame->_cisc_spilling_operand_name]; duke@435: OperandForm *oper = form ? form->is_operand() : NULL; duke@435: // Verify the user's suggestion duke@435: if( oper != NULL ) { duke@435: // Ensure that match field is defined. duke@435: if ( oper->_matrule != NULL ) { duke@435: MatchRule &mrule = *oper->_matrule; duke@435: if( strcmp(mrule._opType,"AddP") == 0 ) { duke@435: MatchNode *left = mrule._lChild; duke@435: MatchNode *right= mrule._rChild; duke@435: if( left != NULL && right != NULL ) { duke@435: const Form *left_op = _globalNames[left->_opType]->is_operand(); duke@435: const Form *right_op = _globalNames[right->_opType]->is_operand(); duke@435: if( (left_op != NULL && right_op != NULL) duke@435: && (left_op->interface_type(_globalNames) == Form::register_interface) duke@435: && (right_op->interface_type(_globalNames) == Form::constant_interface) ) { duke@435: // Successfully verified operand duke@435: set_cisc_spill_operand( oper ); duke@435: if( _cisc_spill_debug ) { duke@435: fprintf(stderr, "\n\nVerified CISC-spill operand %s\n\n", oper->_ident); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: if( cisc_spill_operand() != NULL ) { duke@435: // N^2 comparison of instructions looking for a cisc-spilling version duke@435: _instructions.reset(); duke@435: InstructForm *instr; duke@435: for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure that match field is defined. duke@435: if ( instr->_matrule == NULL ) continue; duke@435: duke@435: MatchRule &mrule = *instr->_matrule; duke@435: Predicate *pred = instr->build_predicate(); duke@435: duke@435: // Grab the machine type of the operand duke@435: const char *rootOp = instr->_ident; duke@435: mrule._machType = rootOp; duke@435: duke@435: // Find result type for match duke@435: const char *result = instr->reduce_result(); duke@435: duke@435: if( PrintAdlcCisc ) fprintf(stderr, " new instruction %s \n", instr->_ident ? instr->_ident : " "); duke@435: bool found_cisc_alternate = false; duke@435: _instructions.reset2(); duke@435: InstructForm *instr2; duke@435: for( ; !found_cisc_alternate && (instr2 = (InstructForm*)_instructions.iter2()) != NULL; ) { duke@435: // Ensure that match field is defined. duke@435: if( PrintAdlcCisc ) fprintf(stderr, " instr2 == %s \n", instr2->_ident ? instr2->_ident : " "); duke@435: if ( instr2->_matrule != NULL duke@435: && (instr != instr2 ) // Skip self duke@435: && (instr2->reduce_result() != NULL) // want same result duke@435: && (strcmp(result, instr2->reduce_result()) == 0)) { duke@435: MatchRule &mrule2 = *instr2->_matrule; duke@435: Predicate *pred2 = instr2->build_predicate(); duke@435: found_cisc_alternate = instr->cisc_spills_to(*this, instr2); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: //---------------------------build_cisc_spilling------------------------------- duke@435: // Get info for the CISC_oracle and MachNode::cisc_version() duke@435: void ArchDesc::build_cisc_spill_instructions(FILE *fp_hpp, FILE *fp_cpp) { duke@435: // Output the table for cisc spilling duke@435: fprintf(fp_cpp, "// The following instructions can cisc-spill\n"); duke@435: _instructions.reset(); duke@435: InstructForm *inst = NULL; duke@435: for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) { duke@435: // Ensure this is a machine-world instruction duke@435: if ( inst->ideal_only() ) continue; duke@435: const char *inst_name = inst->_ident; duke@435: int operand = inst->cisc_spill_operand(); duke@435: if( operand != AdlcVMDeps::Not_cisc_spillable ) { duke@435: InstructForm *inst2 = inst->cisc_spill_alternate(); duke@435: fprintf(fp_cpp, "// %s can cisc-spill operand %d to %s\n", inst->_ident, operand, inst2->_ident); duke@435: } duke@435: } duke@435: fprintf(fp_cpp, "\n\n"); duke@435: } duke@435: duke@435: //---------------------------identify_short_branches---------------------------- duke@435: // Get info for our short branch replacement oracle. duke@435: void ArchDesc::identify_short_branches() { duke@435: // Walk over all instructions, checking to see if they match a short duke@435: // branching alternate. duke@435: _instructions.reset(); duke@435: InstructForm *instr; duke@435: while( (instr = (InstructForm*)_instructions.iter()) != NULL ) { duke@435: // The instruction must have a match rule. duke@435: if (instr->_matrule != NULL && duke@435: instr->is_short_branch()) { duke@435: duke@435: _instructions.reset2(); duke@435: InstructForm *instr2; duke@435: while( (instr2 = (InstructForm*)_instructions.iter2()) != NULL ) { duke@435: instr2->check_branch_variant(*this, instr); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: //---------------------------identify_unique_operands--------------------------- duke@435: // Identify unique operands. duke@435: void ArchDesc::identify_unique_operands() { duke@435: // Walk over all instructions. duke@435: _instructions.reset(); duke@435: InstructForm *instr; duke@435: while( (instr = (InstructForm*)_instructions.iter()) != NULL ) { duke@435: // Ensure this is a machine-world instruction duke@435: if (!instr->ideal_only()) { duke@435: instr->set_unique_opnds(); duke@435: } duke@435: } duke@435: }