src/share/vm/adlc/output_c.cpp

Fri, 20 Dec 2013 13:51:14 +0100

author
goetz
date
Fri, 20 Dec 2013 13:51:14 +0100
changeset 6499
ad3b94907eed
parent 6494
492e67693373
child 6503
a9becfeecd1b
permissions
-rw-r--r--

8030863: PPC64: (part 220): ConstantTableBase for calls between args and jvms
Summary: Add ConstantTableBase node edge after parameters and before jvms. Adapt jvms offsets.
Reviewed-by: kvn

duke@435 1 /*
coleenp@4037 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // output_c.cpp - Class CPP file output routines for architecture definition
duke@435 26
duke@435 27 #include "adlc.hpp"
duke@435 28
duke@435 29 // Utilities to characterize effect statements
duke@435 30 static bool is_def(int usedef) {
duke@435 31 switch(usedef) {
duke@435 32 case Component::DEF:
duke@435 33 case Component::USE_DEF: return true; break;
duke@435 34 }
duke@435 35 return false;
duke@435 36 }
duke@435 37
duke@435 38 static bool is_use(int usedef) {
duke@435 39 switch(usedef) {
duke@435 40 case Component::USE:
duke@435 41 case Component::USE_DEF:
duke@435 42 case Component::USE_KILL: return true; break;
duke@435 43 }
duke@435 44 return false;
duke@435 45 }
duke@435 46
duke@435 47 static bool is_kill(int usedef) {
duke@435 48 switch(usedef) {
duke@435 49 case Component::KILL:
duke@435 50 case Component::USE_KILL: return true; break;
duke@435 51 }
duke@435 52 return false;
duke@435 53 }
duke@435 54
duke@435 55 // Define an array containing the machine register names, strings.
duke@435 56 static void defineRegNames(FILE *fp, RegisterForm *registers) {
duke@435 57 if (registers) {
duke@435 58 fprintf(fp,"\n");
duke@435 59 fprintf(fp,"// An array of character pointers to machine register names.\n");
duke@435 60 fprintf(fp,"const char *Matcher::regName[REG_COUNT] = {\n");
duke@435 61
duke@435 62 // Output the register name for each register in the allocation classes
duke@435 63 RegDef *reg_def = NULL;
duke@435 64 RegDef *next = NULL;
duke@435 65 registers->reset_RegDefs();
neliasso@4906 66 for (reg_def = registers->iter_RegDefs(); reg_def != NULL; reg_def = next) {
duke@435 67 next = registers->iter_RegDefs();
duke@435 68 const char *comma = (next != NULL) ? "," : " // no trailing comma";
neliasso@4906 69 fprintf(fp," \"%s\"%s\n", reg_def->_regname, comma);
duke@435 70 }
duke@435 71
duke@435 72 // Finish defining enumeration
duke@435 73 fprintf(fp,"};\n");
duke@435 74
duke@435 75 fprintf(fp,"\n");
duke@435 76 fprintf(fp,"// An array of character pointers to machine register names.\n");
duke@435 77 fprintf(fp,"const VMReg OptoReg::opto2vm[REG_COUNT] = {\n");
duke@435 78 reg_def = NULL;
duke@435 79 next = NULL;
duke@435 80 registers->reset_RegDefs();
neliasso@4906 81 for (reg_def = registers->iter_RegDefs(); reg_def != NULL; reg_def = next) {
duke@435 82 next = registers->iter_RegDefs();
duke@435 83 const char *comma = (next != NULL) ? "," : " // no trailing comma";
neliasso@4906 84 fprintf(fp,"\t%s%s\n", reg_def->_concrete, comma);
duke@435 85 }
duke@435 86 // Finish defining array
duke@435 87 fprintf(fp,"\t};\n");
duke@435 88 fprintf(fp,"\n");
duke@435 89
duke@435 90 fprintf(fp," OptoReg::Name OptoReg::vm2opto[ConcreteRegisterImpl::number_of_registers];\n");
duke@435 91
duke@435 92 }
duke@435 93 }
duke@435 94
duke@435 95 // Define an array containing the machine register encoding values
duke@435 96 static void defineRegEncodes(FILE *fp, RegisterForm *registers) {
duke@435 97 if (registers) {
duke@435 98 fprintf(fp,"\n");
duke@435 99 fprintf(fp,"// An array of the machine register encode values\n");
duke@435 100 fprintf(fp,"const unsigned char Matcher::_regEncode[REG_COUNT] = {\n");
duke@435 101
duke@435 102 // Output the register encoding for each register in the allocation classes
duke@435 103 RegDef *reg_def = NULL;
duke@435 104 RegDef *next = NULL;
duke@435 105 registers->reset_RegDefs();
neliasso@4906 106 for (reg_def = registers->iter_RegDefs(); reg_def != NULL; reg_def = next) {
duke@435 107 next = registers->iter_RegDefs();
duke@435 108 const char* register_encode = reg_def->register_encode();
duke@435 109 const char *comma = (next != NULL) ? "," : " // no trailing comma";
duke@435 110 int encval;
duke@435 111 if (!ADLParser::is_int_token(register_encode, encval)) {
neliasso@4906 112 fprintf(fp," %s%s // %s\n", register_encode, comma, reg_def->_regname);
duke@435 113 } else {
duke@435 114 // Output known constants in hex char format (backward compatibility).
duke@435 115 assert(encval < 256, "Exceeded supported width for register encoding");
neliasso@4906 116 fprintf(fp," (unsigned char)'\\x%X'%s // %s\n", encval, comma, reg_def->_regname);
duke@435 117 }
duke@435 118 }
duke@435 119 // Finish defining enumeration
duke@435 120 fprintf(fp,"};\n");
duke@435 121
duke@435 122 } // Done defining array
duke@435 123 }
duke@435 124
duke@435 125 // Output an enumeration of register class names
duke@435 126 static void defineRegClassEnum(FILE *fp, RegisterForm *registers) {
duke@435 127 if (registers) {
duke@435 128 // Output an enumeration of register class names
duke@435 129 fprintf(fp,"\n");
duke@435 130 fprintf(fp,"// Enumeration of register class names\n");
duke@435 131 fprintf(fp, "enum machRegisterClass {\n");
duke@435 132 registers->_rclasses.reset();
neliasso@4906 133 for (const char *class_name = NULL; (class_name = registers->_rclasses.iter()) != NULL;) {
neliasso@4906 134 const char * class_name_to_upper = toUpper(class_name);
neliasso@4906 135 fprintf(fp," %s,\n", class_name_to_upper);
neliasso@4906 136 delete[] class_name_to_upper;
duke@435 137 }
duke@435 138 // Finish defining enumeration
duke@435 139 fprintf(fp, " _last_Mach_Reg_Class\n");
duke@435 140 fprintf(fp, "};\n");
duke@435 141 }
duke@435 142 }
duke@435 143
duke@435 144 // Declare an enumeration of user-defined register classes
duke@435 145 // and a list of register masks, one for each class.
duke@435 146 void ArchDesc::declare_register_masks(FILE *fp_hpp) {
duke@435 147 const char *rc_name;
duke@435 148
neliasso@4906 149 if (_register) {
duke@435 150 // Build enumeration of user-defined register classes.
duke@435 151 defineRegClassEnum(fp_hpp, _register);
duke@435 152
duke@435 153 // Generate a list of register masks, one for each class.
duke@435 154 fprintf(fp_hpp,"\n");
duke@435 155 fprintf(fp_hpp,"// Register masks, one for each register class.\n");
duke@435 156 _register->_rclasses.reset();
neliasso@4906 157 for (rc_name = NULL; (rc_name = _register->_rclasses.iter()) != NULL;) {
neliasso@4906 158 const char *prefix = "";
neliasso@4906 159 RegClass *reg_class = _register->getRegClass(rc_name);
neliasso@4906 160 assert(reg_class, "Using an undefined register class");
neliasso@4906 161
neliasso@4906 162 const char* rc_name_to_upper = toUpper(rc_name);
duke@435 163
roland@3317 164 if (reg_class->_user_defined == NULL) {
neliasso@4906 165 fprintf(fp_hpp, "extern const RegMask _%s%s_mask;\n", prefix, rc_name_to_upper);
neliasso@4906 166 fprintf(fp_hpp, "inline const RegMask &%s%s_mask() { return _%s%s_mask; }\n", prefix, rc_name_to_upper, prefix, rc_name_to_upper);
roland@3317 167 } else {
neliasso@4906 168 fprintf(fp_hpp, "inline const RegMask &%s%s_mask() { %s }\n", prefix, rc_name_to_upper, reg_class->_user_defined);
roland@3317 169 }
duke@435 170
neliasso@4906 171 if (reg_class->_stack_or_reg) {
roland@3317 172 assert(reg_class->_user_defined == NULL, "no user defined reg class here");
neliasso@4906 173 fprintf(fp_hpp, "extern const RegMask _%sSTACK_OR_%s_mask;\n", prefix, rc_name_to_upper);
neliasso@4906 174 fprintf(fp_hpp, "inline const RegMask &%sSTACK_OR_%s_mask() { return _%sSTACK_OR_%s_mask; }\n", prefix, rc_name_to_upper, prefix, rc_name_to_upper);
duke@435 175 }
neliasso@4906 176 delete[] rc_name_to_upper;
neliasso@4906 177
duke@435 178 }
duke@435 179 }
duke@435 180 }
duke@435 181
duke@435 182 // Generate an enumeration of user-defined register classes
duke@435 183 // and a list of register masks, one for each class.
duke@435 184 void ArchDesc::build_register_masks(FILE *fp_cpp) {
duke@435 185 const char *rc_name;
duke@435 186
neliasso@4906 187 if (_register) {
duke@435 188 // Generate a list of register masks, one for each class.
duke@435 189 fprintf(fp_cpp,"\n");
duke@435 190 fprintf(fp_cpp,"// Register masks, one for each register class.\n");
duke@435 191 _register->_rclasses.reset();
neliasso@4906 192 for (rc_name = NULL; (rc_name = _register->_rclasses.iter()) != NULL;) {
neliasso@4906 193 const char *prefix = "";
neliasso@4906 194 RegClass *reg_class = _register->getRegClass(rc_name);
neliasso@4906 195 assert(reg_class, "Using an undefined register class");
neliasso@4906 196
neliasso@4906 197 if (reg_class->_user_defined != NULL) {
neliasso@4906 198 continue;
neliasso@4906 199 }
roland@3317 200
duke@435 201 int len = RegisterForm::RegMask_Size();
neliasso@4906 202 const char* rc_name_to_upper = toUpper(rc_name);
neliasso@4906 203 fprintf(fp_cpp, "const RegMask _%s%s_mask(", prefix, rc_name_to_upper);
neliasso@4906 204
neliasso@4906 205 {
neliasso@4906 206 int i;
neliasso@4906 207 for(i = 0; i < len - 1; i++) {
neliasso@4906 208 fprintf(fp_cpp," 0x%x,", reg_class->regs_in_word(i, false));
neliasso@4906 209 }
neliasso@4906 210 fprintf(fp_cpp," 0x%x );\n", reg_class->regs_in_word(i, false));
duke@435 211 }
duke@435 212
neliasso@4906 213 if (reg_class->_stack_or_reg) {
duke@435 214 int i;
neliasso@4906 215 fprintf(fp_cpp, "const RegMask _%sSTACK_OR_%s_mask(", prefix, rc_name_to_upper);
neliasso@4906 216 for(i = 0; i < len - 1; i++) {
neliasso@4906 217 fprintf(fp_cpp," 0x%x,",reg_class->regs_in_word(i, true));
neliasso@4906 218 }
neliasso@4906 219 fprintf(fp_cpp," 0x%x );\n",reg_class->regs_in_word(i, true));
duke@435 220 }
neliasso@4906 221 delete[] rc_name_to_upper;
duke@435 222 }
duke@435 223 }
duke@435 224 }
duke@435 225
duke@435 226 // Compute an index for an array in the pipeline_reads_NNN arrays
duke@435 227 static int pipeline_reads_initializer(FILE *fp_cpp, NameList &pipeline_reads, PipeClassForm *pipeclass)
duke@435 228 {
duke@435 229 int templen = 1;
duke@435 230 int paramcount = 0;
duke@435 231 const char *paramname;
duke@435 232
duke@435 233 if (pipeclass->_parameters.count() == 0)
duke@435 234 return -1;
duke@435 235
duke@435 236 pipeclass->_parameters.reset();
duke@435 237 paramname = pipeclass->_parameters.iter();
duke@435 238 const PipeClassOperandForm *pipeopnd =
duke@435 239 (const PipeClassOperandForm *)pipeclass->_localUsage[paramname];
duke@435 240 if (pipeopnd && !pipeopnd->isWrite() && strcmp(pipeopnd->_stage, "Universal"))
duke@435 241 pipeclass->_parameters.reset();
duke@435 242
duke@435 243 while ( (paramname = pipeclass->_parameters.iter()) != NULL ) {
twisti@1038 244 const PipeClassOperandForm *tmppipeopnd =
duke@435 245 (const PipeClassOperandForm *)pipeclass->_localUsage[paramname];
duke@435 246
twisti@1038 247 if (tmppipeopnd)
twisti@1038 248 templen += 10 + (int)strlen(tmppipeopnd->_stage);
duke@435 249 else
duke@435 250 templen += 19;
duke@435 251
duke@435 252 paramcount++;
duke@435 253 }
duke@435 254
duke@435 255 // See if the count is zero
duke@435 256 if (paramcount == 0) {
duke@435 257 return -1;
duke@435 258 }
duke@435 259
duke@435 260 char *operand_stages = new char [templen];
duke@435 261 operand_stages[0] = 0;
duke@435 262 int i = 0;
duke@435 263 templen = 0;
duke@435 264
duke@435 265 pipeclass->_parameters.reset();
duke@435 266 paramname = pipeclass->_parameters.iter();
duke@435 267 pipeopnd = (const PipeClassOperandForm *)pipeclass->_localUsage[paramname];
duke@435 268 if (pipeopnd && !pipeopnd->isWrite() && strcmp(pipeopnd->_stage, "Universal"))
duke@435 269 pipeclass->_parameters.reset();
duke@435 270
duke@435 271 while ( (paramname = pipeclass->_parameters.iter()) != NULL ) {
twisti@1038 272 const PipeClassOperandForm *tmppipeopnd =
duke@435 273 (const PipeClassOperandForm *)pipeclass->_localUsage[paramname];
duke@435 274 templen += sprintf(&operand_stages[templen], " stage_%s%c\n",
twisti@1038 275 tmppipeopnd ? tmppipeopnd->_stage : "undefined",
duke@435 276 (++i < paramcount ? ',' : ' ') );
duke@435 277 }
duke@435 278
duke@435 279 // See if the same string is in the table
duke@435 280 int ndx = pipeline_reads.index(operand_stages);
duke@435 281
duke@435 282 // No, add it to the table
duke@435 283 if (ndx < 0) {
duke@435 284 pipeline_reads.addName(operand_stages);
duke@435 285 ndx = pipeline_reads.index(operand_stages);
duke@435 286
duke@435 287 fprintf(fp_cpp, "static const enum machPipelineStages pipeline_reads_%03d[%d] = {\n%s};\n\n",
duke@435 288 ndx+1, paramcount, operand_stages);
duke@435 289 }
duke@435 290 else
duke@435 291 delete [] operand_stages;
duke@435 292
duke@435 293 return (ndx);
duke@435 294 }
duke@435 295
duke@435 296 // Compute an index for an array in the pipeline_res_stages_NNN arrays
duke@435 297 static int pipeline_res_stages_initializer(
duke@435 298 FILE *fp_cpp,
duke@435 299 PipelineForm *pipeline,
duke@435 300 NameList &pipeline_res_stages,
duke@435 301 PipeClassForm *pipeclass)
duke@435 302 {
duke@435 303 const PipeClassResourceForm *piperesource;
duke@435 304 int * res_stages = new int [pipeline->_rescount];
duke@435 305 int i;
duke@435 306
duke@435 307 for (i = 0; i < pipeline->_rescount; i++)
duke@435 308 res_stages[i] = 0;
duke@435 309
duke@435 310 for (pipeclass->_resUsage.reset();
duke@435 311 (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) {
duke@435 312 int used_mask = pipeline->_resdict[piperesource->_resource]->is_resource()->mask();
duke@435 313 for (i = 0; i < pipeline->_rescount; i++)
duke@435 314 if ((1 << i) & used_mask) {
duke@435 315 int stage = pipeline->_stages.index(piperesource->_stage);
duke@435 316 if (res_stages[i] < stage+1)
duke@435 317 res_stages[i] = stage+1;
duke@435 318 }
duke@435 319 }
duke@435 320
duke@435 321 // Compute the length needed for the resource list
duke@435 322 int commentlen = 0;
duke@435 323 int max_stage = 0;
duke@435 324 for (i = 0; i < pipeline->_rescount; i++) {
duke@435 325 if (res_stages[i] == 0) {
duke@435 326 if (max_stage < 9)
duke@435 327 max_stage = 9;
duke@435 328 }
duke@435 329 else {
duke@435 330 int stagelen = (int)strlen(pipeline->_stages.name(res_stages[i]-1));
duke@435 331 if (max_stage < stagelen)
duke@435 332 max_stage = stagelen;
duke@435 333 }
duke@435 334
duke@435 335 commentlen += (int)strlen(pipeline->_reslist.name(i));
duke@435 336 }
duke@435 337
duke@435 338 int templen = 1 + commentlen + pipeline->_rescount * (max_stage + 14);
duke@435 339
duke@435 340 // Allocate space for the resource list
duke@435 341 char * resource_stages = new char [templen];
duke@435 342
duke@435 343 templen = 0;
duke@435 344 for (i = 0; i < pipeline->_rescount; i++) {
duke@435 345 const char * const resname =
duke@435 346 res_stages[i] == 0 ? "undefined" : pipeline->_stages.name(res_stages[i]-1);
duke@435 347
duke@435 348 templen += sprintf(&resource_stages[templen], " stage_%s%-*s // %s\n",
duke@435 349 resname, max_stage - (int)strlen(resname) + 1,
duke@435 350 (i < pipeline->_rescount-1) ? "," : "",
duke@435 351 pipeline->_reslist.name(i));
duke@435 352 }
duke@435 353
duke@435 354 // See if the same string is in the table
duke@435 355 int ndx = pipeline_res_stages.index(resource_stages);
duke@435 356
duke@435 357 // No, add it to the table
duke@435 358 if (ndx < 0) {
duke@435 359 pipeline_res_stages.addName(resource_stages);
duke@435 360 ndx = pipeline_res_stages.index(resource_stages);
duke@435 361
duke@435 362 fprintf(fp_cpp, "static const enum machPipelineStages pipeline_res_stages_%03d[%d] = {\n%s};\n\n",
duke@435 363 ndx+1, pipeline->_rescount, resource_stages);
duke@435 364 }
duke@435 365 else
duke@435 366 delete [] resource_stages;
duke@435 367
duke@435 368 delete [] res_stages;
duke@435 369
duke@435 370 return (ndx);
duke@435 371 }
duke@435 372
duke@435 373 // Compute an index for an array in the pipeline_res_cycles_NNN arrays
duke@435 374 static int pipeline_res_cycles_initializer(
duke@435 375 FILE *fp_cpp,
duke@435 376 PipelineForm *pipeline,
duke@435 377 NameList &pipeline_res_cycles,
duke@435 378 PipeClassForm *pipeclass)
duke@435 379 {
duke@435 380 const PipeClassResourceForm *piperesource;
duke@435 381 int * res_cycles = new int [pipeline->_rescount];
duke@435 382 int i;
duke@435 383
duke@435 384 for (i = 0; i < pipeline->_rescount; i++)
duke@435 385 res_cycles[i] = 0;
duke@435 386
duke@435 387 for (pipeclass->_resUsage.reset();
duke@435 388 (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) {
duke@435 389 int used_mask = pipeline->_resdict[piperesource->_resource]->is_resource()->mask();
duke@435 390 for (i = 0; i < pipeline->_rescount; i++)
duke@435 391 if ((1 << i) & used_mask) {
duke@435 392 int cycles = piperesource->_cycles;
duke@435 393 if (res_cycles[i] < cycles)
duke@435 394 res_cycles[i] = cycles;
duke@435 395 }
duke@435 396 }
duke@435 397
duke@435 398 // Pre-compute the string length
duke@435 399 int templen;
duke@435 400 int cyclelen = 0, commentlen = 0;
duke@435 401 int max_cycles = 0;
duke@435 402 char temp[32];
duke@435 403
duke@435 404 for (i = 0; i < pipeline->_rescount; i++) {
duke@435 405 if (max_cycles < res_cycles[i])
duke@435 406 max_cycles = res_cycles[i];
duke@435 407 templen = sprintf(temp, "%d", res_cycles[i]);
duke@435 408 if (cyclelen < templen)
duke@435 409 cyclelen = templen;
duke@435 410 commentlen += (int)strlen(pipeline->_reslist.name(i));
duke@435 411 }
duke@435 412
duke@435 413 templen = 1 + commentlen + (cyclelen + 8) * pipeline->_rescount;
duke@435 414
duke@435 415 // Allocate space for the resource list
duke@435 416 char * resource_cycles = new char [templen];
duke@435 417
duke@435 418 templen = 0;
duke@435 419
duke@435 420 for (i = 0; i < pipeline->_rescount; i++) {
duke@435 421 templen += sprintf(&resource_cycles[templen], " %*d%c // %s\n",
duke@435 422 cyclelen, res_cycles[i], (i < pipeline->_rescount-1) ? ',' : ' ', pipeline->_reslist.name(i));
duke@435 423 }
duke@435 424
duke@435 425 // See if the same string is in the table
duke@435 426 int ndx = pipeline_res_cycles.index(resource_cycles);
duke@435 427
duke@435 428 // No, add it to the table
duke@435 429 if (ndx < 0) {
duke@435 430 pipeline_res_cycles.addName(resource_cycles);
duke@435 431 ndx = pipeline_res_cycles.index(resource_cycles);
duke@435 432
duke@435 433 fprintf(fp_cpp, "static const uint pipeline_res_cycles_%03d[%d] = {\n%s};\n\n",
duke@435 434 ndx+1, pipeline->_rescount, resource_cycles);
duke@435 435 }
duke@435 436 else
duke@435 437 delete [] resource_cycles;
duke@435 438
duke@435 439 delete [] res_cycles;
duke@435 440
duke@435 441 return (ndx);
duke@435 442 }
duke@435 443
duke@435 444 //typedef unsigned long long uint64_t;
duke@435 445
duke@435 446 // Compute an index for an array in the pipeline_res_mask_NNN arrays
duke@435 447 static int pipeline_res_mask_initializer(
duke@435 448 FILE *fp_cpp,
duke@435 449 PipelineForm *pipeline,
duke@435 450 NameList &pipeline_res_mask,
duke@435 451 NameList &pipeline_res_args,
duke@435 452 PipeClassForm *pipeclass)
duke@435 453 {
duke@435 454 const PipeClassResourceForm *piperesource;
duke@435 455 const uint rescount = pipeline->_rescount;
duke@435 456 const uint maxcycleused = pipeline->_maxcycleused;
duke@435 457 const uint cyclemasksize = (maxcycleused + 31) >> 5;
duke@435 458
duke@435 459 int i, j;
duke@435 460 int element_count = 0;
duke@435 461 uint *res_mask = new uint [cyclemasksize];
duke@435 462 uint resources_used = 0;
duke@435 463 uint resources_used_exclusively = 0;
duke@435 464
duke@435 465 for (pipeclass->_resUsage.reset();
twisti@5221 466 (piperesource = (const PipeClassResourceForm*)pipeclass->_resUsage.iter()) != NULL; ) {
duke@435 467 element_count++;
twisti@5221 468 }
duke@435 469
duke@435 470 // Pre-compute the string length
duke@435 471 int templen;
duke@435 472 int commentlen = 0;
duke@435 473 int max_cycles = 0;
duke@435 474
duke@435 475 int cyclelen = ((maxcycleused + 3) >> 2);
duke@435 476 int masklen = (rescount + 3) >> 2;
duke@435 477
duke@435 478 int cycledigit = 0;
duke@435 479 for (i = maxcycleused; i > 0; i /= 10)
duke@435 480 cycledigit++;
duke@435 481
duke@435 482 int maskdigit = 0;
duke@435 483 for (i = rescount; i > 0; i /= 10)
duke@435 484 maskdigit++;
duke@435 485
twisti@5221 486 static const char* pipeline_use_cycle_mask = "Pipeline_Use_Cycle_Mask";
twisti@5221 487 static const char* pipeline_use_element = "Pipeline_Use_Element";
duke@435 488
duke@435 489 templen = 1 +
duke@435 490 (int)(strlen(pipeline_use_cycle_mask) + (int)strlen(pipeline_use_element) +
duke@435 491 (cyclemasksize * 12) + masklen + (cycledigit * 2) + 30) * element_count;
duke@435 492
duke@435 493 // Allocate space for the resource list
duke@435 494 char * resource_mask = new char [templen];
duke@435 495 char * last_comma = NULL;
duke@435 496
duke@435 497 templen = 0;
duke@435 498
duke@435 499 for (pipeclass->_resUsage.reset();
twisti@5221 500 (piperesource = (const PipeClassResourceForm*)pipeclass->_resUsage.iter()) != NULL; ) {
duke@435 501 int used_mask = pipeline->_resdict[piperesource->_resource]->is_resource()->mask();
duke@435 502
twisti@5221 503 if (!used_mask) {
duke@435 504 fprintf(stderr, "*** used_mask is 0 ***\n");
twisti@5221 505 }
duke@435 506
duke@435 507 resources_used |= used_mask;
duke@435 508
duke@435 509 uint lb, ub;
duke@435 510
duke@435 511 for (lb = 0; (used_mask & (1 << lb)) == 0; lb++);
duke@435 512 for (ub = 31; (used_mask & (1 << ub)) == 0; ub--);
duke@435 513
twisti@5221 514 if (lb == ub) {
duke@435 515 resources_used_exclusively |= used_mask;
twisti@5221 516 }
duke@435 517
duke@435 518 int formatlen =
duke@435 519 sprintf(&resource_mask[templen], " %s(0x%0*x, %*d, %*d, %s %s(",
duke@435 520 pipeline_use_element,
duke@435 521 masklen, used_mask,
duke@435 522 cycledigit, lb, cycledigit, ub,
duke@435 523 ((used_mask & (used_mask-1)) != 0) ? "true, " : "false,",
duke@435 524 pipeline_use_cycle_mask);
duke@435 525
duke@435 526 templen += formatlen;
duke@435 527
duke@435 528 memset(res_mask, 0, cyclemasksize * sizeof(uint));
duke@435 529
duke@435 530 int cycles = piperesource->_cycles;
duke@435 531 uint stage = pipeline->_stages.index(piperesource->_stage);
twisti@5221 532 if ((uint)NameList::Not_in_list == stage) {
kvn@4161 533 fprintf(stderr,
kvn@4161 534 "pipeline_res_mask_initializer: "
kvn@4161 535 "semantic error: "
kvn@4161 536 "pipeline stage undeclared: %s\n",
kvn@4161 537 piperesource->_stage);
kvn@4161 538 exit(1);
kvn@4161 539 }
twisti@5221 540 uint upper_limit = stage + cycles - 1;
twisti@5221 541 uint lower_limit = stage - 1;
duke@435 542 uint upper_idx = upper_limit >> 5;
duke@435 543 uint lower_idx = lower_limit >> 5;
duke@435 544 uint upper_position = upper_limit & 0x1f;
duke@435 545 uint lower_position = lower_limit & 0x1f;
duke@435 546
duke@435 547 uint mask = (((uint)1) << upper_position) - 1;
duke@435 548
twisti@5221 549 while (upper_idx > lower_idx) {
duke@435 550 res_mask[upper_idx--] |= mask;
duke@435 551 mask = (uint)-1;
duke@435 552 }
duke@435 553
duke@435 554 mask -= (((uint)1) << lower_position) - 1;
duke@435 555 res_mask[upper_idx] |= mask;
duke@435 556
duke@435 557 for (j = cyclemasksize-1; j >= 0; j--) {
duke@435 558 formatlen =
duke@435 559 sprintf(&resource_mask[templen], "0x%08x%s", res_mask[j], j > 0 ? ", " : "");
duke@435 560 templen += formatlen;
duke@435 561 }
duke@435 562
duke@435 563 resource_mask[templen++] = ')';
duke@435 564 resource_mask[templen++] = ')';
duke@435 565 last_comma = &resource_mask[templen];
duke@435 566 resource_mask[templen++] = ',';
duke@435 567 resource_mask[templen++] = '\n';
duke@435 568 }
duke@435 569
duke@435 570 resource_mask[templen] = 0;
twisti@5221 571 if (last_comma) {
duke@435 572 last_comma[0] = ' ';
twisti@5221 573 }
duke@435 574
duke@435 575 // See if the same string is in the table
duke@435 576 int ndx = pipeline_res_mask.index(resource_mask);
duke@435 577
duke@435 578 // No, add it to the table
duke@435 579 if (ndx < 0) {
duke@435 580 pipeline_res_mask.addName(resource_mask);
duke@435 581 ndx = pipeline_res_mask.index(resource_mask);
duke@435 582
duke@435 583 if (strlen(resource_mask) > 0)
duke@435 584 fprintf(fp_cpp, "static const Pipeline_Use_Element pipeline_res_mask_%03d[%d] = {\n%s};\n\n",
duke@435 585 ndx+1, element_count, resource_mask);
duke@435 586
twisti@5221 587 char* args = new char [9 + 2*masklen + maskdigit];
duke@435 588
duke@435 589 sprintf(args, "0x%0*x, 0x%0*x, %*d",
duke@435 590 masklen, resources_used,
duke@435 591 masklen, resources_used_exclusively,
duke@435 592 maskdigit, element_count);
duke@435 593
duke@435 594 pipeline_res_args.addName(args);
duke@435 595 }
twisti@5221 596 else {
duke@435 597 delete [] resource_mask;
twisti@5221 598 }
duke@435 599
duke@435 600 delete [] res_mask;
duke@435 601 //delete [] res_masks;
duke@435 602
duke@435 603 return (ndx);
duke@435 604 }
duke@435 605
duke@435 606 void ArchDesc::build_pipe_classes(FILE *fp_cpp) {
duke@435 607 const char *classname;
duke@435 608 const char *resourcename;
duke@435 609 int resourcenamelen = 0;
duke@435 610 NameList pipeline_reads;
duke@435 611 NameList pipeline_res_stages;
duke@435 612 NameList pipeline_res_cycles;
duke@435 613 NameList pipeline_res_masks;
duke@435 614 NameList pipeline_res_args;
duke@435 615 const int default_latency = 1;
duke@435 616 const int non_operand_latency = 0;
duke@435 617 const int node_latency = 0;
duke@435 618
duke@435 619 if (!_pipeline) {
duke@435 620 fprintf(fp_cpp, "uint Node::latency(uint i) const {\n");
duke@435 621 fprintf(fp_cpp, " // assert(false, \"pipeline functionality is not defined\");\n");
duke@435 622 fprintf(fp_cpp, " return %d;\n", non_operand_latency);
duke@435 623 fprintf(fp_cpp, "}\n");
duke@435 624 return;
duke@435 625 }
duke@435 626
duke@435 627 fprintf(fp_cpp, "\n");
duke@435 628 fprintf(fp_cpp, "//------------------Pipeline Methods-----------------------------------------\n");
duke@435 629 fprintf(fp_cpp, "#ifndef PRODUCT\n");
duke@435 630 fprintf(fp_cpp, "const char * Pipeline::stageName(uint s) {\n");
duke@435 631 fprintf(fp_cpp, " static const char * const _stage_names[] = {\n");
duke@435 632 fprintf(fp_cpp, " \"undefined\"");
duke@435 633
duke@435 634 for (int s = 0; s < _pipeline->_stagecnt; s++)
duke@435 635 fprintf(fp_cpp, ", \"%s\"", _pipeline->_stages.name(s));
duke@435 636
duke@435 637 fprintf(fp_cpp, "\n };\n\n");
duke@435 638 fprintf(fp_cpp, " return (s <= %d ? _stage_names[s] : \"???\");\n",
duke@435 639 _pipeline->_stagecnt);
duke@435 640 fprintf(fp_cpp, "}\n");
duke@435 641 fprintf(fp_cpp, "#endif\n\n");
duke@435 642
duke@435 643 fprintf(fp_cpp, "uint Pipeline::functional_unit_latency(uint start, const Pipeline *pred) const {\n");
duke@435 644 fprintf(fp_cpp, " // See if the functional units overlap\n");
duke@435 645 #if 0
duke@435 646 fprintf(fp_cpp, "\n#ifndef PRODUCT\n");
duke@435 647 fprintf(fp_cpp, " if (TraceOptoOutput) {\n");
duke@435 648 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 649 fprintf(fp_cpp, " }\n");
duke@435 650 fprintf(fp_cpp, "#endif\n\n");
duke@435 651 #endif
duke@435 652 fprintf(fp_cpp, " uint mask = resourcesUsedExclusively() & pred->resourcesUsedExclusively();\n");
duke@435 653 fprintf(fp_cpp, " if (mask == 0)\n return (start);\n\n");
duke@435 654 #if 0
duke@435 655 fprintf(fp_cpp, "\n#ifndef PRODUCT\n");
duke@435 656 fprintf(fp_cpp, " if (TraceOptoOutput) {\n");
duke@435 657 fprintf(fp_cpp, " tty->print(\"# functional_unit_latency: mask == 0x%%x\\n\", mask);\n");
duke@435 658 fprintf(fp_cpp, " }\n");
duke@435 659 fprintf(fp_cpp, "#endif\n\n");
duke@435 660 #endif
duke@435 661 fprintf(fp_cpp, " for (uint i = 0; i < pred->resourceUseCount(); i++) {\n");
duke@435 662 fprintf(fp_cpp, " const Pipeline_Use_Element *predUse = pred->resourceUseElement(i);\n");
duke@435 663 fprintf(fp_cpp, " if (predUse->multiple())\n");
duke@435 664 fprintf(fp_cpp, " continue;\n\n");
duke@435 665 fprintf(fp_cpp, " for (uint j = 0; j < resourceUseCount(); j++) {\n");
duke@435 666 fprintf(fp_cpp, " const Pipeline_Use_Element *currUse = resourceUseElement(j);\n");
duke@435 667 fprintf(fp_cpp, " if (currUse->multiple())\n");
duke@435 668 fprintf(fp_cpp, " continue;\n\n");
duke@435 669 fprintf(fp_cpp, " if (predUse->used() & currUse->used()) {\n");
duke@435 670 fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask x = predUse->mask();\n");
duke@435 671 fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask y = currUse->mask();\n\n");
duke@435 672 fprintf(fp_cpp, " for ( y <<= start; x.overlaps(y); start++ )\n");
duke@435 673 fprintf(fp_cpp, " y <<= 1;\n");
duke@435 674 fprintf(fp_cpp, " }\n");
duke@435 675 fprintf(fp_cpp, " }\n");
duke@435 676 fprintf(fp_cpp, " }\n\n");
duke@435 677 fprintf(fp_cpp, " // There is the potential for overlap\n");
duke@435 678 fprintf(fp_cpp, " return (start);\n");
duke@435 679 fprintf(fp_cpp, "}\n\n");
duke@435 680 fprintf(fp_cpp, "// The following two routines assume that the root Pipeline_Use entity\n");
duke@435 681 fprintf(fp_cpp, "// consists of exactly 1 element for each functional unit\n");
duke@435 682 fprintf(fp_cpp, "// start is relative to the current cycle; used for latency-based info\n");
duke@435 683 fprintf(fp_cpp, "uint Pipeline_Use::full_latency(uint delay, const Pipeline_Use &pred) const {\n");
duke@435 684 fprintf(fp_cpp, " for (uint i = 0; i < pred._count; i++) {\n");
duke@435 685 fprintf(fp_cpp, " const Pipeline_Use_Element *predUse = pred.element(i);\n");
duke@435 686 fprintf(fp_cpp, " if (predUse->_multiple) {\n");
duke@435 687 fprintf(fp_cpp, " uint min_delay = %d;\n",
duke@435 688 _pipeline->_maxcycleused+1);
duke@435 689 fprintf(fp_cpp, " // Multiple possible functional units, choose first unused one\n");
duke@435 690 fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n");
duke@435 691 fprintf(fp_cpp, " const Pipeline_Use_Element *currUse = element(j);\n");
duke@435 692 fprintf(fp_cpp, " uint curr_delay = delay;\n");
duke@435 693 fprintf(fp_cpp, " if (predUse->_used & currUse->_used) {\n");
duke@435 694 fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask x = predUse->_mask;\n");
duke@435 695 fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask y = currUse->_mask;\n\n");
duke@435 696 fprintf(fp_cpp, " for ( y <<= curr_delay; x.overlaps(y); curr_delay++ )\n");
duke@435 697 fprintf(fp_cpp, " y <<= 1;\n");
duke@435 698 fprintf(fp_cpp, " }\n");
duke@435 699 fprintf(fp_cpp, " if (min_delay > curr_delay)\n min_delay = curr_delay;\n");
duke@435 700 fprintf(fp_cpp, " }\n");
duke@435 701 fprintf(fp_cpp, " if (delay < min_delay)\n delay = min_delay;\n");
duke@435 702 fprintf(fp_cpp, " }\n");
duke@435 703 fprintf(fp_cpp, " else {\n");
duke@435 704 fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n");
duke@435 705 fprintf(fp_cpp, " const Pipeline_Use_Element *currUse = element(j);\n");
duke@435 706 fprintf(fp_cpp, " if (predUse->_used & currUse->_used) {\n");
duke@435 707 fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask x = predUse->_mask;\n");
duke@435 708 fprintf(fp_cpp, " Pipeline_Use_Cycle_Mask y = currUse->_mask;\n\n");
duke@435 709 fprintf(fp_cpp, " for ( y <<= delay; x.overlaps(y); delay++ )\n");
duke@435 710 fprintf(fp_cpp, " y <<= 1;\n");
duke@435 711 fprintf(fp_cpp, " }\n");
duke@435 712 fprintf(fp_cpp, " }\n");
duke@435 713 fprintf(fp_cpp, " }\n");
duke@435 714 fprintf(fp_cpp, " }\n\n");
duke@435 715 fprintf(fp_cpp, " return (delay);\n");
duke@435 716 fprintf(fp_cpp, "}\n\n");
duke@435 717 fprintf(fp_cpp, "void Pipeline_Use::add_usage(const Pipeline_Use &pred) {\n");
duke@435 718 fprintf(fp_cpp, " for (uint i = 0; i < pred._count; i++) {\n");
duke@435 719 fprintf(fp_cpp, " const Pipeline_Use_Element *predUse = pred.element(i);\n");
duke@435 720 fprintf(fp_cpp, " if (predUse->_multiple) {\n");
duke@435 721 fprintf(fp_cpp, " // Multiple possible functional units, choose first unused one\n");
duke@435 722 fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n");
duke@435 723 fprintf(fp_cpp, " Pipeline_Use_Element *currUse = element(j);\n");
duke@435 724 fprintf(fp_cpp, " if ( !predUse->_mask.overlaps(currUse->_mask) ) {\n");
duke@435 725 fprintf(fp_cpp, " currUse->_used |= (1 << j);\n");
duke@435 726 fprintf(fp_cpp, " _resources_used |= (1 << j);\n");
duke@435 727 fprintf(fp_cpp, " currUse->_mask.Or(predUse->_mask);\n");
duke@435 728 fprintf(fp_cpp, " break;\n");
duke@435 729 fprintf(fp_cpp, " }\n");
duke@435 730 fprintf(fp_cpp, " }\n");
duke@435 731 fprintf(fp_cpp, " }\n");
duke@435 732 fprintf(fp_cpp, " else {\n");
duke@435 733 fprintf(fp_cpp, " for (uint j = predUse->_lb; j <= predUse->_ub; j++) {\n");
duke@435 734 fprintf(fp_cpp, " Pipeline_Use_Element *currUse = element(j);\n");
duke@435 735 fprintf(fp_cpp, " currUse->_used |= (1 << j);\n");
duke@435 736 fprintf(fp_cpp, " _resources_used |= (1 << j);\n");
duke@435 737 fprintf(fp_cpp, " currUse->_mask.Or(predUse->_mask);\n");
duke@435 738 fprintf(fp_cpp, " }\n");
duke@435 739 fprintf(fp_cpp, " }\n");
duke@435 740 fprintf(fp_cpp, " }\n");
duke@435 741 fprintf(fp_cpp, "}\n\n");
duke@435 742
duke@435 743 fprintf(fp_cpp, "uint Pipeline::operand_latency(uint opnd, const Pipeline *pred) const {\n");
duke@435 744 fprintf(fp_cpp, " int const default_latency = 1;\n");
duke@435 745 fprintf(fp_cpp, "\n");
duke@435 746 #if 0
duke@435 747 fprintf(fp_cpp, "#ifndef PRODUCT\n");
duke@435 748 fprintf(fp_cpp, " if (TraceOptoOutput) {\n");
duke@435 749 fprintf(fp_cpp, " tty->print(\"# operand_latency(%%d), _read_stage_count = %%d\\n\", opnd, _read_stage_count);\n");
duke@435 750 fprintf(fp_cpp, " }\n");
duke@435 751 fprintf(fp_cpp, "#endif\n\n");
duke@435 752 #endif
jcoomes@1844 753 fprintf(fp_cpp, " assert(this, \"NULL pipeline info\");\n");
jcoomes@1844 754 fprintf(fp_cpp, " assert(pred, \"NULL predecessor pipline info\");\n\n");
duke@435 755 fprintf(fp_cpp, " if (pred->hasFixedLatency())\n return (pred->fixedLatency());\n\n");
duke@435 756 fprintf(fp_cpp, " // If this is not an operand, then assume a dependence with 0 latency\n");
duke@435 757 fprintf(fp_cpp, " if (opnd > _read_stage_count)\n return (0);\n\n");
duke@435 758 fprintf(fp_cpp, " uint writeStage = pred->_write_stage;\n");
duke@435 759 fprintf(fp_cpp, " uint readStage = _read_stages[opnd-1];\n");
duke@435 760 #if 0
duke@435 761 fprintf(fp_cpp, "\n#ifndef PRODUCT\n");
duke@435 762 fprintf(fp_cpp, " if (TraceOptoOutput) {\n");
duke@435 763 fprintf(fp_cpp, " tty->print(\"# operand_latency: writeStage=%%s readStage=%%s, opnd=%%d\\n\", stageName(writeStage), stageName(readStage), opnd);\n");
duke@435 764 fprintf(fp_cpp, " }\n");
duke@435 765 fprintf(fp_cpp, "#endif\n\n");
duke@435 766 #endif
duke@435 767 fprintf(fp_cpp, "\n");
duke@435 768 fprintf(fp_cpp, " if (writeStage == stage_undefined || readStage == stage_undefined)\n");
duke@435 769 fprintf(fp_cpp, " return (default_latency);\n");
duke@435 770 fprintf(fp_cpp, "\n");
duke@435 771 fprintf(fp_cpp, " int delta = writeStage - readStage;\n");
duke@435 772 fprintf(fp_cpp, " if (delta < 0) delta = 0;\n\n");
duke@435 773 #if 0
duke@435 774 fprintf(fp_cpp, "\n#ifndef PRODUCT\n");
duke@435 775 fprintf(fp_cpp, " if (TraceOptoOutput) {\n");
duke@435 776 fprintf(fp_cpp, " tty->print(\"# operand_latency: delta=%%d\\n\", delta);\n");
duke@435 777 fprintf(fp_cpp, " }\n");
duke@435 778 fprintf(fp_cpp, "#endif\n\n");
duke@435 779 #endif
duke@435 780 fprintf(fp_cpp, " return (delta);\n");
duke@435 781 fprintf(fp_cpp, "}\n\n");
duke@435 782
duke@435 783 if (!_pipeline)
duke@435 784 /* Do Nothing */;
duke@435 785
duke@435 786 else if (_pipeline->_maxcycleused <=
duke@435 787 #ifdef SPARC
duke@435 788 64
duke@435 789 #else
duke@435 790 32
duke@435 791 #endif
duke@435 792 ) {
duke@435 793 fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator&(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n");
duke@435 794 fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask(in1._mask & in2._mask);\n");
duke@435 795 fprintf(fp_cpp, "}\n\n");
duke@435 796 fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator|(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n");
duke@435 797 fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask(in1._mask | in2._mask);\n");
duke@435 798 fprintf(fp_cpp, "}\n\n");
duke@435 799 }
duke@435 800 else {
duke@435 801 uint l;
duke@435 802 uint masklen = (_pipeline->_maxcycleused + 31) >> 5;
duke@435 803 fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator&(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n");
duke@435 804 fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask(");
duke@435 805 for (l = 1; l <= masklen; l++)
duke@435 806 fprintf(fp_cpp, "in1._mask%d & in2._mask%d%s\n", l, l, l < masklen ? ", " : "");
duke@435 807 fprintf(fp_cpp, ");\n");
duke@435 808 fprintf(fp_cpp, "}\n\n");
duke@435 809 fprintf(fp_cpp, "Pipeline_Use_Cycle_Mask operator|(const Pipeline_Use_Cycle_Mask &in1, const Pipeline_Use_Cycle_Mask &in2) {\n");
duke@435 810 fprintf(fp_cpp, " return Pipeline_Use_Cycle_Mask(");
duke@435 811 for (l = 1; l <= masklen; l++)
duke@435 812 fprintf(fp_cpp, "in1._mask%d | in2._mask%d%s", l, l, l < masklen ? ", " : "");
duke@435 813 fprintf(fp_cpp, ");\n");
duke@435 814 fprintf(fp_cpp, "}\n\n");
duke@435 815 fprintf(fp_cpp, "void Pipeline_Use_Cycle_Mask::Or(const Pipeline_Use_Cycle_Mask &in2) {\n ");
duke@435 816 for (l = 1; l <= masklen; l++)
duke@435 817 fprintf(fp_cpp, " _mask%d |= in2._mask%d;", l, l);
duke@435 818 fprintf(fp_cpp, "\n}\n\n");
duke@435 819 }
duke@435 820
duke@435 821 /* Get the length of all the resource names */
duke@435 822 for (_pipeline->_reslist.reset(), resourcenamelen = 0;
duke@435 823 (resourcename = _pipeline->_reslist.iter()) != NULL;
duke@435 824 resourcenamelen += (int)strlen(resourcename));
duke@435 825
duke@435 826 // Create the pipeline class description
duke@435 827
duke@435 828 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 829 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 830
duke@435 831 fprintf(fp_cpp, "const Pipeline_Use_Element Pipeline_Use::elaborated_elements[%d] = {\n", _pipeline->_rescount);
duke@435 832 for (int i1 = 0; i1 < _pipeline->_rescount; i1++) {
duke@435 833 fprintf(fp_cpp, " Pipeline_Use_Element(0, %d, %d, false, Pipeline_Use_Cycle_Mask(", i1, i1);
duke@435 834 uint masklen = (_pipeline->_maxcycleused + 31) >> 5;
duke@435 835 for (int i2 = masklen-1; i2 >= 0; i2--)
duke@435 836 fprintf(fp_cpp, "0%s", i2 > 0 ? ", " : "");
duke@435 837 fprintf(fp_cpp, "))%s\n", i1 < (_pipeline->_rescount-1) ? "," : "");
duke@435 838 }
duke@435 839 fprintf(fp_cpp, "};\n\n");
duke@435 840
duke@435 841 fprintf(fp_cpp, "const Pipeline_Use Pipeline_Use::elaborated_use(0, 0, %d, (Pipeline_Use_Element *)&elaborated_elements[0]);\n\n",
duke@435 842 _pipeline->_rescount);
duke@435 843
duke@435 844 for (_pipeline->_classlist.reset(); (classname = _pipeline->_classlist.iter()) != NULL; ) {
duke@435 845 fprintf(fp_cpp, "\n");
duke@435 846 fprintf(fp_cpp, "// Pipeline Class \"%s\"\n", classname);
duke@435 847 PipeClassForm *pipeclass = _pipeline->_classdict[classname]->is_pipeclass();
duke@435 848 int maxWriteStage = -1;
duke@435 849 int maxMoreInstrs = 0;
duke@435 850 int paramcount = 0;
duke@435 851 int i = 0;
duke@435 852 const char *paramname;
duke@435 853 int resource_count = (_pipeline->_rescount + 3) >> 2;
duke@435 854
duke@435 855 // Scan the operands, looking for last output stage and number of inputs
duke@435 856 for (pipeclass->_parameters.reset(); (paramname = pipeclass->_parameters.iter()) != NULL; ) {
duke@435 857 const PipeClassOperandForm *pipeopnd =
duke@435 858 (const PipeClassOperandForm *)pipeclass->_localUsage[paramname];
duke@435 859 if (pipeopnd) {
duke@435 860 if (pipeopnd->_iswrite) {
duke@435 861 int stagenum = _pipeline->_stages.index(pipeopnd->_stage);
duke@435 862 int moreinsts = pipeopnd->_more_instrs;
duke@435 863 if ((maxWriteStage+maxMoreInstrs) < (stagenum+moreinsts)) {
duke@435 864 maxWriteStage = stagenum;
duke@435 865 maxMoreInstrs = moreinsts;
duke@435 866 }
duke@435 867 }
duke@435 868 }
duke@435 869
duke@435 870 if (i++ > 0 || (pipeopnd && !pipeopnd->isWrite()))
duke@435 871 paramcount++;
duke@435 872 }
duke@435 873
duke@435 874 // Create the list of stages for the operands that are read
duke@435 875 // Note that we will build a NameList to reduce the number of copies
duke@435 876
duke@435 877 int pipeline_reads_index = pipeline_reads_initializer(fp_cpp, pipeline_reads, pipeclass);
duke@435 878
duke@435 879 int pipeline_res_stages_index = pipeline_res_stages_initializer(
duke@435 880 fp_cpp, _pipeline, pipeline_res_stages, pipeclass);
duke@435 881
duke@435 882 int pipeline_res_cycles_index = pipeline_res_cycles_initializer(
duke@435 883 fp_cpp, _pipeline, pipeline_res_cycles, pipeclass);
duke@435 884
duke@435 885 int pipeline_res_mask_index = pipeline_res_mask_initializer(
duke@435 886 fp_cpp, _pipeline, pipeline_res_masks, pipeline_res_args, pipeclass);
duke@435 887
duke@435 888 #if 0
duke@435 889 // Process the Resources
duke@435 890 const PipeClassResourceForm *piperesource;
duke@435 891
duke@435 892 unsigned resources_used = 0;
duke@435 893 unsigned exclusive_resources_used = 0;
duke@435 894 unsigned resource_groups = 0;
duke@435 895 for (pipeclass->_resUsage.reset();
duke@435 896 (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL; ) {
duke@435 897 int used_mask = _pipeline->_resdict[piperesource->_resource]->is_resource()->mask();
duke@435 898 if (used_mask)
duke@435 899 resource_groups++;
duke@435 900 resources_used |= used_mask;
duke@435 901 if ((used_mask & (used_mask-1)) == 0)
duke@435 902 exclusive_resources_used |= used_mask;
duke@435 903 }
duke@435 904
duke@435 905 if (resource_groups > 0) {
duke@435 906 fprintf(fp_cpp, "static const uint pipeline_res_or_masks_%03d[%d] = {",
duke@435 907 pipeclass->_num, resource_groups);
duke@435 908 for (pipeclass->_resUsage.reset(), i = 1;
duke@435 909 (piperesource = (const PipeClassResourceForm *)pipeclass->_resUsage.iter()) != NULL;
duke@435 910 i++ ) {
duke@435 911 int used_mask = _pipeline->_resdict[piperesource->_resource]->is_resource()->mask();
duke@435 912 if (used_mask) {
duke@435 913 fprintf(fp_cpp, " 0x%0*x%c", resource_count, used_mask, i < (int)resource_groups ? ',' : ' ');
duke@435 914 }
duke@435 915 }
duke@435 916 fprintf(fp_cpp, "};\n\n");
duke@435 917 }
duke@435 918 #endif
duke@435 919
duke@435 920 // Create the pipeline class description
duke@435 921 fprintf(fp_cpp, "static const Pipeline pipeline_class_%03d(",
duke@435 922 pipeclass->_num);
duke@435 923 if (maxWriteStage < 0)
duke@435 924 fprintf(fp_cpp, "(uint)stage_undefined");
duke@435 925 else if (maxMoreInstrs == 0)
duke@435 926 fprintf(fp_cpp, "(uint)stage_%s", _pipeline->_stages.name(maxWriteStage));
duke@435 927 else
duke@435 928 fprintf(fp_cpp, "((uint)stage_%s)+%d", _pipeline->_stages.name(maxWriteStage), maxMoreInstrs);
duke@435 929 fprintf(fp_cpp, ", %d, %s, %d, %d, %s, %s, %s, %s,\n",
duke@435 930 paramcount,
duke@435 931 pipeclass->hasFixedLatency() ? "true" : "false",
duke@435 932 pipeclass->fixedLatency(),
duke@435 933 pipeclass->InstructionCount(),
duke@435 934 pipeclass->hasBranchDelay() ? "true" : "false",
duke@435 935 pipeclass->hasMultipleBundles() ? "true" : "false",
duke@435 936 pipeclass->forceSerialization() ? "true" : "false",
duke@435 937 pipeclass->mayHaveNoCode() ? "true" : "false" );
duke@435 938 if (paramcount > 0) {
duke@435 939 fprintf(fp_cpp, "\n (enum machPipelineStages * const) pipeline_reads_%03d,\n ",
duke@435 940 pipeline_reads_index+1);
duke@435 941 }
duke@435 942 else
duke@435 943 fprintf(fp_cpp, " NULL,");
duke@435 944 fprintf(fp_cpp, " (enum machPipelineStages * const) pipeline_res_stages_%03d,\n",
duke@435 945 pipeline_res_stages_index+1);
duke@435 946 fprintf(fp_cpp, " (uint * const) pipeline_res_cycles_%03d,\n",
duke@435 947 pipeline_res_cycles_index+1);
duke@435 948 fprintf(fp_cpp, " Pipeline_Use(%s, (Pipeline_Use_Element *)",
duke@435 949 pipeline_res_args.name(pipeline_res_mask_index));
duke@435 950 if (strlen(pipeline_res_masks.name(pipeline_res_mask_index)) > 0)
duke@435 951 fprintf(fp_cpp, "&pipeline_res_mask_%03d[0]",
duke@435 952 pipeline_res_mask_index+1);
duke@435 953 else
duke@435 954 fprintf(fp_cpp, "NULL");
duke@435 955 fprintf(fp_cpp, "));\n");
duke@435 956 }
duke@435 957
duke@435 958 // Generate the Node::latency method if _pipeline defined
duke@435 959 fprintf(fp_cpp, "\n");
duke@435 960 fprintf(fp_cpp, "//------------------Inter-Instruction Latency--------------------------------\n");
duke@435 961 fprintf(fp_cpp, "uint Node::latency(uint i) {\n");
duke@435 962 if (_pipeline) {
duke@435 963 #if 0
duke@435 964 fprintf(fp_cpp, "#ifndef PRODUCT\n");
duke@435 965 fprintf(fp_cpp, " if (TraceOptoOutput) {\n");
duke@435 966 fprintf(fp_cpp, " tty->print(\"# %%4d->latency(%%d)\\n\", _idx, i);\n");
duke@435 967 fprintf(fp_cpp, " }\n");
duke@435 968 fprintf(fp_cpp, "#endif\n");
duke@435 969 #endif
duke@435 970 fprintf(fp_cpp, " uint j;\n");
duke@435 971 fprintf(fp_cpp, " // verify in legal range for inputs\n");
duke@435 972 fprintf(fp_cpp, " assert(i < len(), \"index not in range\");\n\n");
duke@435 973 fprintf(fp_cpp, " // verify input is not null\n");
duke@435 974 fprintf(fp_cpp, " Node *pred = in(i);\n");
duke@435 975 fprintf(fp_cpp, " if (!pred)\n return %d;\n\n",
duke@435 976 non_operand_latency);
duke@435 977 fprintf(fp_cpp, " if (pred->is_Proj())\n pred = pred->in(0);\n\n");
duke@435 978 fprintf(fp_cpp, " // if either node does not have pipeline info, use default\n");
duke@435 979 fprintf(fp_cpp, " const Pipeline *predpipe = pred->pipeline();\n");
duke@435 980 fprintf(fp_cpp, " assert(predpipe, \"no predecessor pipeline info\");\n\n");
duke@435 981 fprintf(fp_cpp, " if (predpipe->hasFixedLatency())\n return predpipe->fixedLatency();\n\n");
duke@435 982 fprintf(fp_cpp, " const Pipeline *currpipe = pipeline();\n");
duke@435 983 fprintf(fp_cpp, " assert(currpipe, \"no pipeline info\");\n\n");
duke@435 984 fprintf(fp_cpp, " if (!is_Mach())\n return %d;\n\n",
duke@435 985 node_latency);
duke@435 986 fprintf(fp_cpp, " const MachNode *m = as_Mach();\n");
duke@435 987 fprintf(fp_cpp, " j = m->oper_input_base();\n");
duke@435 988 fprintf(fp_cpp, " if (i < j)\n return currpipe->functional_unit_latency(%d, predpipe);\n\n",
duke@435 989 non_operand_latency);
duke@435 990 fprintf(fp_cpp, " // determine which operand this is in\n");
duke@435 991 fprintf(fp_cpp, " uint n = m->num_opnds();\n");
duke@435 992 fprintf(fp_cpp, " int delta = %d;\n\n",
duke@435 993 non_operand_latency);
duke@435 994 fprintf(fp_cpp, " uint k;\n");
duke@435 995 fprintf(fp_cpp, " for (k = 1; k < n; k++) {\n");
duke@435 996 fprintf(fp_cpp, " j += m->_opnds[k]->num_edges();\n");
duke@435 997 fprintf(fp_cpp, " if (i < j)\n");
duke@435 998 fprintf(fp_cpp, " break;\n");
duke@435 999 fprintf(fp_cpp, " }\n");
duke@435 1000 fprintf(fp_cpp, " if (k < n)\n");
duke@435 1001 fprintf(fp_cpp, " delta = currpipe->operand_latency(k,predpipe);\n\n");
duke@435 1002 fprintf(fp_cpp, " return currpipe->functional_unit_latency(delta, predpipe);\n");
duke@435 1003 }
duke@435 1004 else {
duke@435 1005 fprintf(fp_cpp, " // assert(false, \"pipeline functionality is not defined\");\n");
duke@435 1006 fprintf(fp_cpp, " return %d;\n",
duke@435 1007 non_operand_latency);
duke@435 1008 }
duke@435 1009 fprintf(fp_cpp, "}\n\n");
duke@435 1010
duke@435 1011 // Output the list of nop nodes
duke@435 1012 fprintf(fp_cpp, "// Descriptions for emitting different functional unit nops\n");
duke@435 1013 const char *nop;
duke@435 1014 int nopcnt = 0;
duke@435 1015 for ( _pipeline->_noplist.reset(); (nop = _pipeline->_noplist.iter()) != NULL; nopcnt++ );
duke@435 1016
duke@435 1017 fprintf(fp_cpp, "void Bundle::initialize_nops(MachNode * nop_list[%d], Compile *C) {\n", nopcnt);
duke@435 1018 int i = 0;
duke@435 1019 for ( _pipeline->_noplist.reset(); (nop = _pipeline->_noplist.iter()) != NULL; i++ ) {
duke@435 1020 fprintf(fp_cpp, " nop_list[%d] = (MachNode *) new (C) %sNode();\n", i, nop);
duke@435 1021 }
duke@435 1022 fprintf(fp_cpp, "};\n\n");
duke@435 1023 fprintf(fp_cpp, "#ifndef PRODUCT\n");
kvn@4161 1024 fprintf(fp_cpp, "void Bundle::dump(outputStream *st) const {\n");
duke@435 1025 fprintf(fp_cpp, " static const char * bundle_flags[] = {\n");
duke@435 1026 fprintf(fp_cpp, " \"\",\n");
duke@435 1027 fprintf(fp_cpp, " \"use nop delay\",\n");
duke@435 1028 fprintf(fp_cpp, " \"use unconditional delay\",\n");
duke@435 1029 fprintf(fp_cpp, " \"use conditional delay\",\n");
duke@435 1030 fprintf(fp_cpp, " \"used in conditional delay\",\n");
duke@435 1031 fprintf(fp_cpp, " \"used in unconditional delay\",\n");
duke@435 1032 fprintf(fp_cpp, " \"used in all conditional delays\",\n");
duke@435 1033 fprintf(fp_cpp, " };\n\n");
duke@435 1034
duke@435 1035 fprintf(fp_cpp, " static const char *resource_names[%d] = {", _pipeline->_rescount);
duke@435 1036 for (i = 0; i < _pipeline->_rescount; i++)
duke@435 1037 fprintf(fp_cpp, " \"%s\"%c", _pipeline->_reslist.name(i), i < _pipeline->_rescount-1 ? ',' : ' ');
duke@435 1038 fprintf(fp_cpp, "};\n\n");
duke@435 1039
duke@435 1040 // See if the same string is in the table
duke@435 1041 fprintf(fp_cpp, " bool needs_comma = false;\n\n");
duke@435 1042 fprintf(fp_cpp, " if (_flags) {\n");
kvn@4161 1043 fprintf(fp_cpp, " st->print(\"%%s\", bundle_flags[_flags]);\n");
duke@435 1044 fprintf(fp_cpp, " needs_comma = true;\n");
duke@435 1045 fprintf(fp_cpp, " };\n");
duke@435 1046 fprintf(fp_cpp, " if (instr_count()) {\n");
kvn@4161 1047 fprintf(fp_cpp, " st->print(\"%%s%%d instr%%s\", needs_comma ? \", \" : \"\", instr_count(), instr_count() != 1 ? \"s\" : \"\");\n");
duke@435 1048 fprintf(fp_cpp, " needs_comma = true;\n");
duke@435 1049 fprintf(fp_cpp, " };\n");
duke@435 1050 fprintf(fp_cpp, " uint r = resources_used();\n");
duke@435 1051 fprintf(fp_cpp, " if (r) {\n");
kvn@4161 1052 fprintf(fp_cpp, " st->print(\"%%sresource%%s:\", needs_comma ? \", \" : \"\", (r & (r-1)) != 0 ? \"s\" : \"\");\n");
duke@435 1053 fprintf(fp_cpp, " for (uint i = 0; i < %d; i++)\n", _pipeline->_rescount);
duke@435 1054 fprintf(fp_cpp, " if ((r & (1 << i)) != 0)\n");
kvn@4161 1055 fprintf(fp_cpp, " st->print(\" %%s\", resource_names[i]);\n");
duke@435 1056 fprintf(fp_cpp, " needs_comma = true;\n");
duke@435 1057 fprintf(fp_cpp, " };\n");
kvn@4161 1058 fprintf(fp_cpp, " st->print(\"\\n\");\n");
duke@435 1059 fprintf(fp_cpp, "}\n");
duke@435 1060 fprintf(fp_cpp, "#endif\n");
duke@435 1061 }
duke@435 1062
duke@435 1063 // ---------------------------------------------------------------------------
duke@435 1064 //------------------------------Utilities to build Instruction Classes--------
duke@435 1065 // ---------------------------------------------------------------------------
duke@435 1066
duke@435 1067 static void defineOut_RegMask(FILE *fp, const char *node, const char *regMask) {
duke@435 1068 fprintf(fp,"const RegMask &%sNode::out_RegMask() const { return (%s); }\n",
duke@435 1069 node, regMask);
duke@435 1070 }
duke@435 1071
twisti@1038 1072 static void print_block_index(FILE *fp, int inst_position) {
duke@435 1073 assert( inst_position >= 0, "Instruction number less than zero");
duke@435 1074 fprintf(fp, "block_index");
duke@435 1075 if( inst_position != 0 ) {
twisti@1038 1076 fprintf(fp, " - %d", inst_position);
duke@435 1077 }
duke@435 1078 }
duke@435 1079
duke@435 1080 // Scan the peepmatch and output a test for each instruction
duke@435 1081 static void check_peepmatch_instruction_sequence(FILE *fp, PeepMatch *pmatch, PeepConstraint *pconstraint) {
twisti@1038 1082 int parent = -1;
twisti@1038 1083 int inst_position = 0;
twisti@1038 1084 const char* inst_name = NULL;
twisti@1038 1085 int input = 0;
duke@435 1086 fprintf(fp, " // Check instruction sub-tree\n");
duke@435 1087 pmatch->reset();
duke@435 1088 for( pmatch->next_instruction( parent, inst_position, inst_name, input );
duke@435 1089 inst_name != NULL;
duke@435 1090 pmatch->next_instruction( parent, inst_position, inst_name, input ) ) {
duke@435 1091 // If this is not a placeholder
duke@435 1092 if( ! pmatch->is_placeholder() ) {
duke@435 1093 // Define temporaries 'inst#', based on parent and parent's input index
duke@435 1094 if( parent != -1 ) { // root was initialized
duke@435 1095 fprintf(fp, " // Identify previous instruction if inside this block\n");
duke@435 1096 fprintf(fp, " if( ");
duke@435 1097 print_block_index(fp, inst_position);
adlertz@5635 1098 fprintf(fp, " > 0 ) {\n Node *n = block->get_node(");
duke@435 1099 print_block_index(fp, inst_position);
twisti@1038 1100 fprintf(fp, ");\n inst%d = (n->is_Mach()) ? ", inst_position);
duke@435 1101 fprintf(fp, "n->as_Mach() : NULL;\n }\n");
duke@435 1102 }
duke@435 1103
duke@435 1104 // When not the root
duke@435 1105 // Test we have the correct instruction by comparing the rule.
duke@435 1106 if( parent != -1 ) {
twisti@1038 1107 fprintf(fp, " matches = matches && (inst%d != NULL) && (inst%d->rule() == %s_rule);\n",
duke@435 1108 inst_position, inst_position, inst_name);
duke@435 1109 }
duke@435 1110 } else {
duke@435 1111 // Check that user did not try to constrain a placeholder
duke@435 1112 assert( ! pconstraint->constrains_instruction(inst_position),
duke@435 1113 "fatal(): Can not constrain a placeholder instruction");
duke@435 1114 }
duke@435 1115 }
duke@435 1116 }
duke@435 1117
duke@435 1118 // Build mapping for register indices, num_edges to input
duke@435 1119 static void build_instruction_index_mapping( FILE *fp, FormDict &globals, PeepMatch *pmatch ) {
twisti@1038 1120 int parent = -1;
twisti@1038 1121 int inst_position = 0;
twisti@1038 1122 const char* inst_name = NULL;
twisti@1038 1123 int input = 0;
duke@435 1124 fprintf(fp, " // Build map to register info\n");
duke@435 1125 pmatch->reset();
duke@435 1126 for( pmatch->next_instruction( parent, inst_position, inst_name, input );
duke@435 1127 inst_name != NULL;
duke@435 1128 pmatch->next_instruction( parent, inst_position, inst_name, input ) ) {
duke@435 1129 // If this is not a placeholder
duke@435 1130 if( ! pmatch->is_placeholder() ) {
duke@435 1131 // Define temporaries 'inst#', based on self's inst_position
duke@435 1132 InstructForm *inst = globals[inst_name]->is_instruction();
duke@435 1133 if( inst != NULL ) {
duke@435 1134 char inst_prefix[] = "instXXXX_";
twisti@1038 1135 sprintf(inst_prefix, "inst%d_", inst_position);
duke@435 1136 char receiver[] = "instXXXX->";
twisti@1038 1137 sprintf(receiver, "inst%d->", inst_position);
duke@435 1138 inst->index_temps( fp, globals, inst_prefix, receiver );
duke@435 1139 }
duke@435 1140 }
duke@435 1141 }
duke@435 1142 }
duke@435 1143
duke@435 1144 // Generate tests for the constraints
duke@435 1145 static void check_peepconstraints(FILE *fp, FormDict &globals, PeepMatch *pmatch, PeepConstraint *pconstraint) {
duke@435 1146 fprintf(fp, "\n");
duke@435 1147 fprintf(fp, " // Check constraints on sub-tree-leaves\n");
duke@435 1148
duke@435 1149 // Build mapping from num_edges to local variables
duke@435 1150 build_instruction_index_mapping( fp, globals, pmatch );
duke@435 1151
duke@435 1152 // Build constraint tests
duke@435 1153 if( pconstraint != NULL ) {
duke@435 1154 fprintf(fp, " matches = matches &&");
duke@435 1155 bool first_constraint = true;
duke@435 1156 while( pconstraint != NULL ) {
duke@435 1157 // indentation and connecting '&&'
duke@435 1158 const char *indentation = " ";
duke@435 1159 fprintf(fp, "\n%s%s", indentation, (!first_constraint ? "&& " : " "));
duke@435 1160
duke@435 1161 // Only have '==' relation implemented
duke@435 1162 if( strcmp(pconstraint->_relation,"==") != 0 ) {
duke@435 1163 assert( false, "Unimplemented()" );
duke@435 1164 }
duke@435 1165
duke@435 1166 // LEFT
twisti@1038 1167 int left_index = pconstraint->_left_inst;
duke@435 1168 const char *left_op = pconstraint->_left_op;
duke@435 1169 // Access info on the instructions whose operands are compared
duke@435 1170 InstructForm *inst_left = globals[pmatch->instruction_name(left_index)]->is_instruction();
duke@435 1171 assert( inst_left, "Parser should guaranty this is an instruction");
duke@435 1172 int left_op_base = inst_left->oper_input_base(globals);
duke@435 1173 // Access info on the operands being compared
duke@435 1174 int left_op_index = inst_left->operand_position(left_op, Component::USE);
duke@435 1175 if( left_op_index == -1 ) {
duke@435 1176 left_op_index = inst_left->operand_position(left_op, Component::DEF);
duke@435 1177 if( left_op_index == -1 ) {
duke@435 1178 left_op_index = inst_left->operand_position(left_op, Component::USE_DEF);
duke@435 1179 }
duke@435 1180 }
duke@435 1181 assert( left_op_index != NameList::Not_in_list, "Did not find operand in instruction");
duke@435 1182 ComponentList components_left = inst_left->_components;
duke@435 1183 const char *left_comp_type = components_left.at(left_op_index)->_type;
duke@435 1184 OpClassForm *left_opclass = globals[left_comp_type]->is_opclass();
duke@435 1185 Form::InterfaceType left_interface_type = left_opclass->interface_type(globals);
duke@435 1186
duke@435 1187
duke@435 1188 // RIGHT
duke@435 1189 int right_op_index = -1;
twisti@1038 1190 int right_index = pconstraint->_right_inst;
duke@435 1191 const char *right_op = pconstraint->_right_op;
duke@435 1192 if( right_index != -1 ) { // Match operand
duke@435 1193 // Access info on the instructions whose operands are compared
duke@435 1194 InstructForm *inst_right = globals[pmatch->instruction_name(right_index)]->is_instruction();
duke@435 1195 assert( inst_right, "Parser should guaranty this is an instruction");
duke@435 1196 int right_op_base = inst_right->oper_input_base(globals);
duke@435 1197 // Access info on the operands being compared
duke@435 1198 right_op_index = inst_right->operand_position(right_op, Component::USE);
duke@435 1199 if( right_op_index == -1 ) {
duke@435 1200 right_op_index = inst_right->operand_position(right_op, Component::DEF);
duke@435 1201 if( right_op_index == -1 ) {
duke@435 1202 right_op_index = inst_right->operand_position(right_op, Component::USE_DEF);
duke@435 1203 }
duke@435 1204 }
duke@435 1205 assert( right_op_index != NameList::Not_in_list, "Did not find operand in instruction");
duke@435 1206 ComponentList components_right = inst_right->_components;
duke@435 1207 const char *right_comp_type = components_right.at(right_op_index)->_type;
duke@435 1208 OpClassForm *right_opclass = globals[right_comp_type]->is_opclass();
duke@435 1209 Form::InterfaceType right_interface_type = right_opclass->interface_type(globals);
duke@435 1210 assert( right_interface_type == left_interface_type, "Both must be same interface");
duke@435 1211
duke@435 1212 } else { // Else match register
duke@435 1213 // assert( false, "should be a register" );
duke@435 1214 }
duke@435 1215
duke@435 1216 //
duke@435 1217 // Check for equivalence
duke@435 1218 //
duke@435 1219 // fprintf(fp, "phase->eqv( ");
duke@435 1220 // fprintf(fp, "inst%d->in(%d+%d) /* %s */, inst%d->in(%d+%d) /* %s */",
duke@435 1221 // left_index, left_op_base, left_op_index, left_op,
duke@435 1222 // right_index, right_op_base, right_op_index, right_op );
duke@435 1223 // fprintf(fp, ")");
duke@435 1224 //
duke@435 1225 switch( left_interface_type ) {
duke@435 1226 case Form::register_interface: {
duke@435 1227 // Check that they are allocated to the same register
duke@435 1228 // Need parameter for index position if not result operand
duke@435 1229 char left_reg_index[] = ",instXXXX_idxXXXX";
duke@435 1230 if( left_op_index != 0 ) {
duke@435 1231 assert( (left_index <= 9999) && (left_op_index <= 9999), "exceed string size");
duke@435 1232 // Must have index into operands
kvn@4161 1233 sprintf(left_reg_index,",inst%d_idx%d", (int)left_index, left_op_index);
duke@435 1234 } else {
duke@435 1235 strcpy(left_reg_index, "");
duke@435 1236 }
duke@435 1237 fprintf(fp, "(inst%d->_opnds[%d]->reg(ra_,inst%d%s) /* %d.%s */",
duke@435 1238 left_index, left_op_index, left_index, left_reg_index, left_index, left_op );
duke@435 1239 fprintf(fp, " == ");
duke@435 1240
duke@435 1241 if( right_index != -1 ) {
duke@435 1242 char right_reg_index[18] = ",instXXXX_idxXXXX";
duke@435 1243 if( right_op_index != 0 ) {
duke@435 1244 assert( (right_index <= 9999) && (right_op_index <= 9999), "exceed string size");
duke@435 1245 // Must have index into operands
kvn@4161 1246 sprintf(right_reg_index,",inst%d_idx%d", (int)right_index, right_op_index);
duke@435 1247 } else {
duke@435 1248 strcpy(right_reg_index, "");
duke@435 1249 }
duke@435 1250 fprintf(fp, "/* %d.%s */ inst%d->_opnds[%d]->reg(ra_,inst%d%s)",
duke@435 1251 right_index, right_op, right_index, right_op_index, right_index, right_reg_index );
duke@435 1252 } else {
duke@435 1253 fprintf(fp, "%s_enc", right_op );
duke@435 1254 }
duke@435 1255 fprintf(fp,")");
duke@435 1256 break;
duke@435 1257 }
duke@435 1258 case Form::constant_interface: {
duke@435 1259 // Compare the '->constant()' values
duke@435 1260 fprintf(fp, "(inst%d->_opnds[%d]->constant() /* %d.%s */",
duke@435 1261 left_index, left_op_index, left_index, left_op );
duke@435 1262 fprintf(fp, " == ");
duke@435 1263 fprintf(fp, "/* %d.%s */ inst%d->_opnds[%d]->constant())",
duke@435 1264 right_index, right_op, right_index, right_op_index );
duke@435 1265 break;
duke@435 1266 }
duke@435 1267 case Form::memory_interface: {
duke@435 1268 // Compare 'base', 'index', 'scale', and 'disp'
duke@435 1269 // base
duke@435 1270 fprintf(fp, "( \n");
duke@435 1271 fprintf(fp, " (inst%d->_opnds[%d]->base(ra_,inst%d,inst%d_idx%d) /* %d.%s$$base */",
duke@435 1272 left_index, left_op_index, left_index, left_index, left_op_index, left_index, left_op );
duke@435 1273 fprintf(fp, " == ");
duke@435 1274 fprintf(fp, "/* %d.%s$$base */ inst%d->_opnds[%d]->base(ra_,inst%d,inst%d_idx%d)) &&\n",
duke@435 1275 right_index, right_op, right_index, right_op_index, right_index, right_index, right_op_index );
duke@435 1276 // index
duke@435 1277 fprintf(fp, " (inst%d->_opnds[%d]->index(ra_,inst%d,inst%d_idx%d) /* %d.%s$$index */",
duke@435 1278 left_index, left_op_index, left_index, left_index, left_op_index, left_index, left_op );
duke@435 1279 fprintf(fp, " == ");
duke@435 1280 fprintf(fp, "/* %d.%s$$index */ inst%d->_opnds[%d]->index(ra_,inst%d,inst%d_idx%d)) &&\n",
duke@435 1281 right_index, right_op, right_index, right_op_index, right_index, right_index, right_op_index );
duke@435 1282 // scale
duke@435 1283 fprintf(fp, " (inst%d->_opnds[%d]->scale() /* %d.%s$$scale */",
duke@435 1284 left_index, left_op_index, left_index, left_op );
duke@435 1285 fprintf(fp, " == ");
duke@435 1286 fprintf(fp, "/* %d.%s$$scale */ inst%d->_opnds[%d]->scale()) &&\n",
duke@435 1287 right_index, right_op, right_index, right_op_index );
duke@435 1288 // disp
duke@435 1289 fprintf(fp, " (inst%d->_opnds[%d]->disp(ra_,inst%d,inst%d_idx%d) /* %d.%s$$disp */",
duke@435 1290 left_index, left_op_index, left_index, left_index, left_op_index, left_index, left_op );
duke@435 1291 fprintf(fp, " == ");
duke@435 1292 fprintf(fp, "/* %d.%s$$disp */ inst%d->_opnds[%d]->disp(ra_,inst%d,inst%d_idx%d))\n",
duke@435 1293 right_index, right_op, right_index, right_op_index, right_index, right_index, right_op_index );
duke@435 1294 fprintf(fp, ") \n");
duke@435 1295 break;
duke@435 1296 }
duke@435 1297 case Form::conditional_interface: {
duke@435 1298 // Compare the condition code being tested
duke@435 1299 assert( false, "Unimplemented()" );
duke@435 1300 break;
duke@435 1301 }
duke@435 1302 default: {
duke@435 1303 assert( false, "ShouldNotReachHere()" );
duke@435 1304 break;
duke@435 1305 }
duke@435 1306 }
duke@435 1307
duke@435 1308 // Advance to next constraint
duke@435 1309 pconstraint = pconstraint->next();
duke@435 1310 first_constraint = false;
duke@435 1311 }
duke@435 1312
duke@435 1313 fprintf(fp, ";\n");
duke@435 1314 }
duke@435 1315 }
duke@435 1316
duke@435 1317 // // EXPERIMENTAL -- TEMPORARY code
duke@435 1318 // static Form::DataType get_operand_type(FormDict &globals, InstructForm *instr, const char *op_name ) {
duke@435 1319 // int op_index = instr->operand_position(op_name, Component::USE);
duke@435 1320 // if( op_index == -1 ) {
duke@435 1321 // op_index = instr->operand_position(op_name, Component::DEF);
duke@435 1322 // if( op_index == -1 ) {
duke@435 1323 // op_index = instr->operand_position(op_name, Component::USE_DEF);
duke@435 1324 // }
duke@435 1325 // }
duke@435 1326 // assert( op_index != NameList::Not_in_list, "Did not find operand in instruction");
duke@435 1327 //
duke@435 1328 // ComponentList components_right = instr->_components;
duke@435 1329 // char *right_comp_type = components_right.at(op_index)->_type;
duke@435 1330 // OpClassForm *right_opclass = globals[right_comp_type]->is_opclass();
duke@435 1331 // Form::InterfaceType right_interface_type = right_opclass->interface_type(globals);
duke@435 1332 //
duke@435 1333 // return;
duke@435 1334 // }
duke@435 1335
duke@435 1336 // Construct the new sub-tree
duke@435 1337 static void generate_peepreplace( FILE *fp, FormDict &globals, PeepMatch *pmatch, PeepConstraint *pconstraint, PeepReplace *preplace, int max_position ) {
duke@435 1338 fprintf(fp, " // IF instructions and constraints matched\n");
duke@435 1339 fprintf(fp, " if( matches ) {\n");
duke@435 1340 fprintf(fp, " // generate the new sub-tree\n");
duke@435 1341 fprintf(fp, " assert( true, \"Debug stopping point\");\n");
duke@435 1342 if( preplace != NULL ) {
duke@435 1343 // Get the root of the new sub-tree
duke@435 1344 const char *root_inst = NULL;
duke@435 1345 preplace->next_instruction(root_inst);
duke@435 1346 InstructForm *root_form = globals[root_inst]->is_instruction();
duke@435 1347 assert( root_form != NULL, "Replacement instruction was not previously defined");
duke@435 1348 fprintf(fp, " %sNode *root = new (C) %sNode();\n", root_inst, root_inst);
duke@435 1349
twisti@1038 1350 int inst_num;
duke@435 1351 const char *op_name;
duke@435 1352 int opnds_index = 0; // define result operand
duke@435 1353 // Then install the use-operands for the new sub-tree
duke@435 1354 // preplace->reset(); // reset breaks iteration
duke@435 1355 for( preplace->next_operand( inst_num, op_name );
duke@435 1356 op_name != NULL;
duke@435 1357 preplace->next_operand( inst_num, op_name ) ) {
duke@435 1358 InstructForm *inst_form;
duke@435 1359 inst_form = globals[pmatch->instruction_name(inst_num)]->is_instruction();
duke@435 1360 assert( inst_form, "Parser should guaranty this is an instruction");
duke@435 1361 int inst_op_num = inst_form->operand_position(op_name, Component::USE);
duke@435 1362 if( inst_op_num == NameList::Not_in_list )
duke@435 1363 inst_op_num = inst_form->operand_position(op_name, Component::USE_DEF);
duke@435 1364 assert( inst_op_num != NameList::Not_in_list, "Did not find operand as USE");
duke@435 1365 // find the name of the OperandForm from the local name
duke@435 1366 const Form *form = inst_form->_localNames[op_name];
duke@435 1367 OperandForm *op_form = form->is_operand();
duke@435 1368 if( opnds_index == 0 ) {
duke@435 1369 // Initial setup of new instruction
duke@435 1370 fprintf(fp, " // ----- Initial setup -----\n");
duke@435 1371 //
duke@435 1372 // Add control edge for this node
duke@435 1373 fprintf(fp, " root->add_req(_in[0]); // control edge\n");
duke@435 1374 // Add unmatched edges from root of match tree
duke@435 1375 int op_base = root_form->oper_input_base(globals);
duke@435 1376 for( int unmatched_edge = 1; unmatched_edge < op_base; ++unmatched_edge ) {
twisti@1038 1377 fprintf(fp, " root->add_req(inst%d->in(%d)); // unmatched ideal edge\n",
duke@435 1378 inst_num, unmatched_edge);
duke@435 1379 }
duke@435 1380 // If new instruction captures bottom type
never@1896 1381 if( root_form->captures_bottom_type(globals) ) {
duke@435 1382 // Get bottom type from instruction whose result we are replacing
twisti@1038 1383 fprintf(fp, " root->_bottom_type = inst%d->bottom_type();\n", inst_num);
duke@435 1384 }
duke@435 1385 // Define result register and result operand
twisti@1038 1386 fprintf(fp, " ra_->add_reference(root, inst%d);\n", inst_num);
twisti@1038 1387 fprintf(fp, " ra_->set_oop (root, ra_->is_oop(inst%d));\n", inst_num);
twisti@1038 1388 fprintf(fp, " ra_->set_pair(root->_idx, ra_->get_reg_second(inst%d), ra_->get_reg_first(inst%d));\n", inst_num, inst_num);
twisti@1038 1389 fprintf(fp, " root->_opnds[0] = inst%d->_opnds[0]->clone(C); // result\n", inst_num);
duke@435 1390 fprintf(fp, " // ----- Done with initial setup -----\n");
duke@435 1391 } else {
duke@435 1392 if( (op_form == NULL) || (op_form->is_base_constant(globals) == Form::none) ) {
duke@435 1393 // Do not have ideal edges for constants after matching
twisti@1038 1394 fprintf(fp, " for( unsigned x%d = inst%d_idx%d; x%d < inst%d_idx%d; x%d++ )\n",
duke@435 1395 inst_op_num, inst_num, inst_op_num,
duke@435 1396 inst_op_num, inst_num, inst_op_num+1, inst_op_num );
twisti@1038 1397 fprintf(fp, " root->add_req( inst%d->in(x%d) );\n",
duke@435 1398 inst_num, inst_op_num );
duke@435 1399 } else {
duke@435 1400 fprintf(fp, " // no ideal edge for constants after matching\n");
duke@435 1401 }
twisti@1038 1402 fprintf(fp, " root->_opnds[%d] = inst%d->_opnds[%d]->clone(C);\n",
duke@435 1403 opnds_index, inst_num, inst_op_num );
duke@435 1404 }
duke@435 1405 ++opnds_index;
duke@435 1406 }
duke@435 1407 }else {
duke@435 1408 // Replacing subtree with empty-tree
duke@435 1409 assert( false, "ShouldNotReachHere();");
duke@435 1410 }
duke@435 1411
duke@435 1412 // Return the new sub-tree
duke@435 1413 fprintf(fp, " deleted = %d;\n", max_position+1 /*zero to one based*/);
duke@435 1414 fprintf(fp, " return root; // return new root;\n");
duke@435 1415 fprintf(fp, " }\n");
duke@435 1416 }
duke@435 1417
duke@435 1418
duke@435 1419 // Define the Peephole method for an instruction node
duke@435 1420 void ArchDesc::definePeephole(FILE *fp, InstructForm *node) {
duke@435 1421 // Generate Peephole function header
duke@435 1422 fprintf(fp, "MachNode *%sNode::peephole( Block *block, int block_index, PhaseRegAlloc *ra_, int &deleted, Compile* C ) {\n", node->_ident);
duke@435 1423 fprintf(fp, " bool matches = true;\n");
duke@435 1424
duke@435 1425 // Identify the maximum instruction position,
duke@435 1426 // generate temporaries that hold current instruction
duke@435 1427 //
duke@435 1428 // MachNode *inst0 = NULL;
duke@435 1429 // ...
duke@435 1430 // MachNode *instMAX = NULL;
duke@435 1431 //
duke@435 1432 int max_position = 0;
duke@435 1433 Peephole *peep;
duke@435 1434 for( peep = node->peepholes(); peep != NULL; peep = peep->next() ) {
duke@435 1435 PeepMatch *pmatch = peep->match();
duke@435 1436 assert( pmatch != NULL, "fatal(), missing peepmatch rule");
duke@435 1437 if( max_position < pmatch->max_position() ) max_position = pmatch->max_position();
duke@435 1438 }
duke@435 1439 for( int i = 0; i <= max_position; ++i ) {
duke@435 1440 if( i == 0 ) {
twisti@1038 1441 fprintf(fp, " MachNode *inst0 = this;\n");
duke@435 1442 } else {
duke@435 1443 fprintf(fp, " MachNode *inst%d = NULL;\n", i);
duke@435 1444 }
duke@435 1445 }
duke@435 1446
duke@435 1447 // For each peephole rule in architecture description
duke@435 1448 // Construct a test for the desired instruction sub-tree
duke@435 1449 // then check the constraints
duke@435 1450 // If these match, Generate the new subtree
duke@435 1451 for( peep = node->peepholes(); peep != NULL; peep = peep->next() ) {
duke@435 1452 int peephole_number = peep->peephole_number();
duke@435 1453 PeepMatch *pmatch = peep->match();
duke@435 1454 PeepConstraint *pconstraint = peep->constraints();
duke@435 1455 PeepReplace *preplace = peep->replacement();
duke@435 1456
duke@435 1457 // Root of this peephole is the current MachNode
duke@435 1458 assert( true, // %%name?%% strcmp( node->_ident, pmatch->name(0) ) == 0,
duke@435 1459 "root of PeepMatch does not match instruction");
duke@435 1460
duke@435 1461 // Make each peephole rule individually selectable
duke@435 1462 fprintf(fp, " if( (OptoPeepholeAt == -1) || (OptoPeepholeAt==%d) ) {\n", peephole_number);
duke@435 1463 fprintf(fp, " matches = true;\n");
duke@435 1464 // Scan the peepmatch and output a test for each instruction
duke@435 1465 check_peepmatch_instruction_sequence( fp, pmatch, pconstraint );
duke@435 1466
duke@435 1467 // Check constraints and build replacement inside scope
duke@435 1468 fprintf(fp, " // If instruction subtree matches\n");
duke@435 1469 fprintf(fp, " if( matches ) {\n");
duke@435 1470
duke@435 1471 // Generate tests for the constraints
duke@435 1472 check_peepconstraints( fp, _globalNames, pmatch, pconstraint );
duke@435 1473
duke@435 1474 // Construct the new sub-tree
duke@435 1475 generate_peepreplace( fp, _globalNames, pmatch, pconstraint, preplace, max_position );
duke@435 1476
duke@435 1477 // End of scope for this peephole's constraints
duke@435 1478 fprintf(fp, " }\n");
duke@435 1479 // Closing brace '}' to make each peephole rule individually selectable
duke@435 1480 fprintf(fp, " } // end of peephole rule #%d\n", peephole_number);
duke@435 1481 fprintf(fp, "\n");
duke@435 1482 }
duke@435 1483
duke@435 1484 fprintf(fp, " return NULL; // No peephole rules matched\n");
duke@435 1485 fprintf(fp, "}\n");
duke@435 1486 fprintf(fp, "\n");
duke@435 1487 }
duke@435 1488
duke@435 1489 // Define the Expand method for an instruction node
duke@435 1490 void ArchDesc::defineExpand(FILE *fp, InstructForm *node) {
duke@435 1491 unsigned cnt = 0; // Count nodes we have expand into
duke@435 1492 unsigned i;
duke@435 1493
duke@435 1494 // Generate Expand function header
twisti@2350 1495 fprintf(fp, "MachNode* %sNode::Expand(State* state, Node_List& proj_list, Node* mem) {\n", node->_ident);
twisti@2350 1496 fprintf(fp, " Compile* C = Compile::current();\n");
duke@435 1497 // Generate expand code
duke@435 1498 if( node->expands() ) {
duke@435 1499 const char *opid;
duke@435 1500 int new_pos, exp_pos;
duke@435 1501 const char *new_id = NULL;
duke@435 1502 const Form *frm = NULL;
duke@435 1503 InstructForm *new_inst = NULL;
duke@435 1504 OperandForm *new_oper = NULL;
duke@435 1505 unsigned numo = node->num_opnds() +
duke@435 1506 node->_exprule->_newopers.count();
duke@435 1507
duke@435 1508 // If necessary, generate any operands created in expand rule
duke@435 1509 if (node->_exprule->_newopers.count()) {
duke@435 1510 for(node->_exprule->_newopers.reset();
duke@435 1511 (new_id = node->_exprule->_newopers.iter()) != NULL; cnt++) {
duke@435 1512 frm = node->_localNames[new_id];
duke@435 1513 assert(frm, "Invalid entry in new operands list of expand rule");
duke@435 1514 new_oper = frm->is_operand();
duke@435 1515 char *tmp = (char *)node->_exprule->_newopconst[new_id];
duke@435 1516 if (tmp == NULL) {
duke@435 1517 fprintf(fp," MachOper *op%d = new (C) %sOper();\n",
duke@435 1518 cnt, new_oper->_ident);
duke@435 1519 }
duke@435 1520 else {
duke@435 1521 fprintf(fp," MachOper *op%d = new (C) %sOper(%s);\n",
duke@435 1522 cnt, new_oper->_ident, tmp);
duke@435 1523 }
duke@435 1524 }
duke@435 1525 }
duke@435 1526 cnt = 0;
duke@435 1527 // Generate the temps to use for DAG building
duke@435 1528 for(i = 0; i < numo; i++) {
duke@435 1529 if (i < node->num_opnds()) {
duke@435 1530 fprintf(fp," MachNode *tmp%d = this;\n", i);
duke@435 1531 }
duke@435 1532 else {
duke@435 1533 fprintf(fp," MachNode *tmp%d = NULL;\n", i);
duke@435 1534 }
duke@435 1535 }
duke@435 1536 // Build mapping from num_edges to local variables
duke@435 1537 fprintf(fp," unsigned num0 = 0;\n");
duke@435 1538 for( i = 1; i < node->num_opnds(); i++ ) {
duke@435 1539 fprintf(fp," unsigned num%d = opnd_array(%d)->num_edges();\n",i,i);
duke@435 1540 }
duke@435 1541
duke@435 1542 // Build a mapping from operand index to input edges
duke@435 1543 fprintf(fp," unsigned idx0 = oper_input_base();\n");
coleenp@548 1544
never@1638 1545 // The order in which the memory input is added to a node is very
coleenp@548 1546 // strange. Store nodes get a memory input before Expand is
never@1638 1547 // called and other nodes get it afterwards or before depending on
never@1638 1548 // match order so oper_input_base is wrong during expansion. This
never@1638 1549 // code adjusts it so that expansion will work correctly.
never@1638 1550 int has_memory_edge = node->_matrule->needs_ideal_memory_edge(_globalNames);
never@1638 1551 if (has_memory_edge) {
never@1638 1552 fprintf(fp," if (mem == (Node*)1) {\n");
never@1638 1553 fprintf(fp," idx0--; // Adjust base because memory edge hasn't been inserted yet\n");
never@1638 1554 fprintf(fp," }\n");
coleenp@548 1555 }
coleenp@548 1556
duke@435 1557 for( i = 0; i < node->num_opnds(); i++ ) {
duke@435 1558 fprintf(fp," unsigned idx%d = idx%d + num%d;\n",
duke@435 1559 i+1,i,i);
duke@435 1560 }
duke@435 1561
duke@435 1562 // Declare variable to hold root of expansion
duke@435 1563 fprintf(fp," MachNode *result = NULL;\n");
duke@435 1564
duke@435 1565 // Iterate over the instructions 'node' expands into
duke@435 1566 ExpandRule *expand = node->_exprule;
duke@435 1567 NameAndList *expand_instr = NULL;
duke@435 1568 for(expand->reset_instructions();
duke@435 1569 (expand_instr = expand->iter_instructions()) != NULL; cnt++) {
duke@435 1570 new_id = expand_instr->name();
duke@435 1571
duke@435 1572 InstructForm* expand_instruction = (InstructForm*)globalAD->globalNames()[new_id];
goetz@6481 1573
goetz@6481 1574 if (!expand_instruction) {
goetz@6481 1575 globalAD->syntax_err(node->_linenum, "In %s: instruction %s used in expand not declared\n",
goetz@6481 1576 node->_ident, new_id);
goetz@6481 1577 continue;
goetz@6481 1578 }
goetz@6481 1579
duke@435 1580 if (expand_instruction->has_temps()) {
duke@435 1581 globalAD->syntax_err(node->_linenum, "In %s: expand rules using instructs with TEMPs aren't supported: %s",
duke@435 1582 node->_ident, new_id);
duke@435 1583 }
duke@435 1584
duke@435 1585 // Build the node for the instruction
duke@435 1586 fprintf(fp,"\n %sNode *n%d = new (C) %sNode();\n", new_id, cnt, new_id);
duke@435 1587 // Add control edge for this node
duke@435 1588 fprintf(fp," n%d->add_req(_in[0]);\n", cnt);
duke@435 1589 // Build the operand for the value this node defines.
duke@435 1590 Form *form = (Form*)_globalNames[new_id];
duke@435 1591 assert( form, "'new_id' must be a defined form name");
duke@435 1592 // Grab the InstructForm for the new instruction
duke@435 1593 new_inst = form->is_instruction();
duke@435 1594 assert( new_inst, "'new_id' must be an instruction name");
duke@435 1595 if( node->is_ideal_if() && new_inst->is_ideal_if() ) {
duke@435 1596 fprintf(fp, " ((MachIfNode*)n%d)->_prob = _prob;\n",cnt);
duke@435 1597 fprintf(fp, " ((MachIfNode*)n%d)->_fcnt = _fcnt;\n",cnt);
duke@435 1598 }
duke@435 1599
duke@435 1600 if( node->is_ideal_fastlock() && new_inst->is_ideal_fastlock() ) {
duke@435 1601 fprintf(fp, " ((MachFastLockNode*)n%d)->_counters = _counters;\n",cnt);
duke@435 1602 }
duke@435 1603
kvn@4113 1604 // Fill in the bottom_type where requested
kvn@4113 1605 if (node->captures_bottom_type(_globalNames) &&
kvn@4113 1606 new_inst->captures_bottom_type(_globalNames)) {
kvn@4113 1607 fprintf(fp, " ((MachTypeNode*)n%d)->_bottom_type = bottom_type();\n", cnt);
kvn@4113 1608 }
kvn@4113 1609
duke@435 1610 const char *resultOper = new_inst->reduce_result();
duke@435 1611 fprintf(fp," n%d->set_opnd_array(0, state->MachOperGenerator( %s, C ));\n",
duke@435 1612 cnt, machOperEnum(resultOper));
duke@435 1613
duke@435 1614 // get the formal operand NameList
duke@435 1615 NameList *formal_lst = &new_inst->_parameters;
duke@435 1616 formal_lst->reset();
duke@435 1617
duke@435 1618 // Handle any memory operand
duke@435 1619 int memory_operand = new_inst->memory_operand(_globalNames);
duke@435 1620 if( memory_operand != InstructForm::NO_MEMORY_OPERAND ) {
duke@435 1621 int node_mem_op = node->memory_operand(_globalNames);
duke@435 1622 assert( node_mem_op != InstructForm::NO_MEMORY_OPERAND,
duke@435 1623 "expand rule member needs memory but top-level inst doesn't have any" );
never@1638 1624 if (has_memory_edge) {
coleenp@548 1625 // Copy memory edge
never@1638 1626 fprintf(fp," if (mem != (Node*)1) {\n");
never@1638 1627 fprintf(fp," n%d->add_req(_in[1]);\t// Add memory edge\n", cnt);
never@1638 1628 fprintf(fp," }\n");
coleenp@548 1629 }
duke@435 1630 }
duke@435 1631
duke@435 1632 // Iterate over the new instruction's operands
never@850 1633 int prev_pos = -1;
duke@435 1634 for( expand_instr->reset(); (opid = expand_instr->iter()) != NULL; ) {
duke@435 1635 // Use 'parameter' at current position in list of new instruction's formals
duke@435 1636 // instead of 'opid' when looking up info internal to new_inst
duke@435 1637 const char *parameter = formal_lst->iter();
goetz@6481 1638 if (!parameter) {
goetz@6481 1639 globalAD->syntax_err(node->_linenum, "Operand %s of expand instruction %s has"
goetz@6481 1640 " no equivalent in new instruction %s.",
goetz@6481 1641 opid, node->_ident, new_inst->_ident);
goetz@6481 1642 assert(0, "Wrong expand");
goetz@6481 1643 }
goetz@6481 1644
duke@435 1645 // Check for an operand which is created in the expand rule
duke@435 1646 if ((exp_pos = node->_exprule->_newopers.index(opid)) != -1) {
duke@435 1647 new_pos = new_inst->operand_position(parameter,Component::USE);
duke@435 1648 exp_pos += node->num_opnds();
duke@435 1649 // If there is no use of the created operand, just skip it
kvn@4161 1650 if (new_pos != NameList::Not_in_list) {
duke@435 1651 //Copy the operand from the original made above
duke@435 1652 fprintf(fp," n%d->set_opnd_array(%d, op%d->clone(C)); // %s\n",
duke@435 1653 cnt, new_pos, exp_pos-node->num_opnds(), opid);
duke@435 1654 // Check for who defines this operand & add edge if needed
duke@435 1655 fprintf(fp," if(tmp%d != NULL)\n", exp_pos);
duke@435 1656 fprintf(fp," n%d->add_req(tmp%d);\n", cnt, exp_pos);
duke@435 1657 }
duke@435 1658 }
duke@435 1659 else {
duke@435 1660 // Use operand name to get an index into instruction component list
duke@435 1661 // ins = (InstructForm *) _globalNames[new_id];
duke@435 1662 exp_pos = node->operand_position_format(opid);
duke@435 1663 assert(exp_pos != -1, "Bad expand rule");
never@850 1664 if (prev_pos > exp_pos && expand_instruction->_matrule != NULL) {
never@850 1665 // For the add_req calls below to work correctly they need
never@850 1666 // to added in the same order that a match would add them.
never@850 1667 // This means that they would need to be in the order of
never@850 1668 // the components list instead of the formal parameters.
never@850 1669 // This is a sort of hidden invariant that previously
never@850 1670 // wasn't checked and could lead to incorrectly
never@850 1671 // constructed nodes.
never@850 1672 syntax_err(node->_linenum, "For expand in %s to work, parameter declaration order in %s must follow matchrule\n",
never@850 1673 node->_ident, new_inst->_ident);
never@850 1674 }
never@850 1675 prev_pos = exp_pos;
duke@435 1676
duke@435 1677 new_pos = new_inst->operand_position(parameter,Component::USE);
duke@435 1678 if (new_pos != -1) {
duke@435 1679 // Copy the operand from the ExpandNode to the new node
duke@435 1680 fprintf(fp," n%d->set_opnd_array(%d, opnd_array(%d)->clone(C)); // %s\n",
duke@435 1681 cnt, new_pos, exp_pos, opid);
duke@435 1682 // For each operand add appropriate input edges by looking at tmp's
duke@435 1683 fprintf(fp," if(tmp%d == this) {\n", exp_pos);
duke@435 1684 // Grab corresponding edges from ExpandNode and insert them here
duke@435 1685 fprintf(fp," for(unsigned i = 0; i < num%d; i++) {\n", exp_pos);
duke@435 1686 fprintf(fp," n%d->add_req(_in[i + idx%d]);\n", cnt, exp_pos);
duke@435 1687 fprintf(fp," }\n");
duke@435 1688 fprintf(fp," }\n");
duke@435 1689 // This value is generated by one of the new instructions
duke@435 1690 fprintf(fp," else n%d->add_req(tmp%d);\n", cnt, exp_pos);
duke@435 1691 }
duke@435 1692 }
duke@435 1693
duke@435 1694 // Update the DAG tmp's for values defined by this instruction
duke@435 1695 int new_def_pos = new_inst->operand_position(parameter,Component::DEF);
duke@435 1696 Effect *eform = (Effect *)new_inst->_effects[parameter];
duke@435 1697 // If this operand is a definition in either an effects rule
duke@435 1698 // or a match rule
duke@435 1699 if((eform) && (is_def(eform->_use_def))) {
duke@435 1700 // Update the temp associated with this operand
duke@435 1701 fprintf(fp," tmp%d = n%d;\n", exp_pos, cnt);
duke@435 1702 }
duke@435 1703 else if( new_def_pos != -1 ) {
duke@435 1704 // Instruction defines a value but user did not declare it
duke@435 1705 // in the 'effect' clause
duke@435 1706 fprintf(fp," tmp%d = n%d;\n", exp_pos, cnt);
duke@435 1707 }
duke@435 1708 } // done iterating over a new instruction's operands
duke@435 1709
duke@435 1710 // Invoke Expand() for the newly created instruction.
never@1638 1711 fprintf(fp," result = n%d->Expand( state, proj_list, mem );\n", cnt);
duke@435 1712 assert( !new_inst->expands(), "Do not have complete support for recursive expansion");
duke@435 1713 } // done iterating over new instructions
duke@435 1714 fprintf(fp,"\n");
duke@435 1715 } // done generating expand rule
duke@435 1716
kvn@2561 1717 // Generate projections for instruction's additional DEFs and KILLs
kvn@2561 1718 if( ! node->expands() && (node->needs_projections() || node->has_temps())) {
kvn@2561 1719 // Get string representing the MachNode that projections point at
kvn@2561 1720 const char *machNode = "this";
kvn@2561 1721 // Generate the projections
kvn@2561 1722 fprintf(fp," // Add projection edges for additional defs or kills\n");
kvn@2561 1723
kvn@2561 1724 // Examine each component to see if it is a DEF or KILL
kvn@2561 1725 node->_components.reset();
kvn@2561 1726 // Skip the first component, if already handled as (SET dst (...))
kvn@2561 1727 Component *comp = NULL;
kvn@2561 1728 // For kills, the choice of projection numbers is arbitrary
kvn@2561 1729 int proj_no = 1;
kvn@2561 1730 bool declared_def = false;
kvn@2561 1731 bool declared_kill = false;
kvn@2561 1732
kvn@2561 1733 while( (comp = node->_components.iter()) != NULL ) {
kvn@2561 1734 // Lookup register class associated with operand type
kvn@2561 1735 Form *form = (Form*)_globalNames[comp->_type];
kvn@2561 1736 assert( form, "component type must be a defined form");
kvn@2561 1737 OperandForm *op = form->is_operand();
kvn@2561 1738
kvn@2561 1739 if (comp->is(Component::TEMP)) {
kvn@2561 1740 fprintf(fp, " // TEMP %s\n", comp->_name);
kvn@2561 1741 if (!declared_def) {
kvn@2561 1742 // Define the variable "def" to hold new MachProjNodes
kvn@2561 1743 fprintf(fp, " MachTempNode *def;\n");
kvn@2561 1744 declared_def = true;
kvn@2561 1745 }
kvn@2561 1746 if (op && op->_interface && op->_interface->is_RegInterface()) {
kvn@2561 1747 fprintf(fp," def = new (C) MachTempNode(state->MachOperGenerator( %s, C ));\n",
kvn@2561 1748 machOperEnum(op->_ident));
kvn@2561 1749 fprintf(fp," add_req(def);\n");
kvn@2561 1750 // The operand for TEMP is already constructed during
kvn@2561 1751 // this mach node construction, see buildMachNode().
kvn@2561 1752 //
kvn@2561 1753 // int idx = node->operand_position_format(comp->_name);
kvn@2561 1754 // fprintf(fp," set_opnd_array(%d, state->MachOperGenerator( %s, C ));\n",
kvn@2561 1755 // idx, machOperEnum(op->_ident));
kvn@2561 1756 } else {
kvn@2561 1757 assert(false, "can't have temps which aren't registers");
kvn@2561 1758 }
kvn@2561 1759 } else if (comp->isa(Component::KILL)) {
kvn@2561 1760 fprintf(fp, " // DEF/KILL %s\n", comp->_name);
kvn@2561 1761
kvn@2561 1762 if (!declared_kill) {
kvn@2561 1763 // Define the variable "kill" to hold new MachProjNodes
kvn@2561 1764 fprintf(fp, " MachProjNode *kill;\n");
kvn@2561 1765 declared_kill = true;
kvn@2561 1766 }
kvn@2561 1767
kvn@2561 1768 assert( op, "Support additional KILLS for base operands");
kvn@2561 1769 const char *regmask = reg_mask(*op);
kvn@2561 1770 const char *ideal_type = op->ideal_type(_globalNames, _register);
kvn@2561 1771
kvn@2561 1772 if (!op->is_bound_register()) {
kvn@2561 1773 syntax_err(node->_linenum, "In %s only bound registers can be killed: %s %s\n",
kvn@2561 1774 node->_ident, comp->_type, comp->_name);
kvn@2561 1775 }
kvn@2561 1776
kvn@2561 1777 fprintf(fp," kill = ");
kvn@4115 1778 fprintf(fp,"new (C) MachProjNode( %s, %d, (%s), Op_%s );\n",
kvn@2561 1779 machNode, proj_no++, regmask, ideal_type);
kvn@2561 1780 fprintf(fp," proj_list.push(kill);\n");
kvn@2561 1781 }
kvn@2561 1782 }
kvn@2561 1783 }
kvn@2561 1784
kvn@2561 1785 if( !node->expands() && node->_matrule != NULL ) {
duke@435 1786 // Remove duplicated operands and inputs which use the same name.
duke@435 1787 // Seach through match operands for the same name usage.
duke@435 1788 uint cur_num_opnds = node->num_opnds();
duke@435 1789 if( cur_num_opnds > 1 && cur_num_opnds != node->num_unique_opnds() ) {
duke@435 1790 Component *comp = NULL;
duke@435 1791 // Build mapping from num_edges to local variables
duke@435 1792 fprintf(fp," unsigned num0 = 0;\n");
duke@435 1793 for( i = 1; i < cur_num_opnds; i++ ) {
kvn@4161 1794 fprintf(fp," unsigned num%d = opnd_array(%d)->num_edges();",i,i);
kvn@4161 1795 fprintf(fp, " \t// %s\n", node->opnd_ident(i));
duke@435 1796 }
duke@435 1797 // Build a mapping from operand index to input edges
duke@435 1798 fprintf(fp," unsigned idx0 = oper_input_base();\n");
duke@435 1799 for( i = 0; i < cur_num_opnds; i++ ) {
duke@435 1800 fprintf(fp," unsigned idx%d = idx%d + num%d;\n",
duke@435 1801 i+1,i,i);
duke@435 1802 }
duke@435 1803
duke@435 1804 uint new_num_opnds = 1;
duke@435 1805 node->_components.reset();
duke@435 1806 // Skip first unique operands.
duke@435 1807 for( i = 1; i < cur_num_opnds; i++ ) {
duke@435 1808 comp = node->_components.iter();
twisti@5221 1809 if (i != node->unique_opnds_idx(i)) {
duke@435 1810 break;
duke@435 1811 }
duke@435 1812 new_num_opnds++;
duke@435 1813 }
duke@435 1814 // Replace not unique operands with next unique operands.
duke@435 1815 for( ; i < cur_num_opnds; i++ ) {
duke@435 1816 comp = node->_components.iter();
twisti@5221 1817 uint j = node->unique_opnds_idx(i);
duke@435 1818 // unique_opnds_idx(i) is unique if unique_opnds_idx(j) is not unique.
duke@435 1819 if( j != node->unique_opnds_idx(j) ) {
duke@435 1820 fprintf(fp," set_opnd_array(%d, opnd_array(%d)->clone(C)); // %s\n",
duke@435 1821 new_num_opnds, i, comp->_name);
duke@435 1822 // delete not unique edges here
duke@435 1823 fprintf(fp," for(unsigned i = 0; i < num%d; i++) {\n", i);
duke@435 1824 fprintf(fp," set_req(i + idx%d, _in[i + idx%d]);\n", new_num_opnds, i);
duke@435 1825 fprintf(fp," }\n");
duke@435 1826 fprintf(fp," num%d = num%d;\n", new_num_opnds, i);
duke@435 1827 fprintf(fp," idx%d = idx%d + num%d;\n", new_num_opnds+1, new_num_opnds, new_num_opnds);
duke@435 1828 new_num_opnds++;
duke@435 1829 }
duke@435 1830 }
duke@435 1831 // delete the rest of edges
duke@435 1832 fprintf(fp," for(int i = idx%d - 1; i >= (int)idx%d; i--) {\n", cur_num_opnds, new_num_opnds);
twisti@1038 1833 fprintf(fp," del_req(i);\n");
duke@435 1834 fprintf(fp," }\n");
duke@435 1835 fprintf(fp," _num_opnds = %d;\n", new_num_opnds);
twisti@1220 1836 assert(new_num_opnds == node->num_unique_opnds(), "what?");
duke@435 1837 }
duke@435 1838 }
duke@435 1839
twisti@2350 1840 // If the node is a MachConstantNode, insert the MachConstantBaseNode edge.
twisti@2350 1841 // NOTE: this edge must be the last input (see MachConstantNode::mach_constant_base_node_input).
goetz@6484 1842 // There are nodes that don't use $constantablebase, but still require that it
goetz@6484 1843 // is an input to the node. Example: divF_reg_immN, Repl32B_imm on x86_64.
goetz@6484 1844 if (node->is_mach_constant() || node->needs_constant_base()) {
goetz@6499 1845 if (node->is_ideal_call() != Form::invalid_type &&
goetz@6499 1846 node->is_ideal_call() != Form::JAVA_LEAF) {
goetz@6499 1847 fprintf(fp, " // MachConstantBaseNode added in matcher.\n");
goetz@6499 1848 _needs_clone_jvms = true;
goetz@6499 1849 } else {
goetz@6499 1850 fprintf(fp, " add_req(C->mach_constant_base_node());\n");
goetz@6499 1851 }
twisti@2350 1852 }
twisti@2350 1853
goetz@6499 1854 fprintf(fp, "\n");
goetz@6499 1855 if (node->expands()) {
goetz@6499 1856 fprintf(fp, " return result;\n");
duke@435 1857 } else {
goetz@6499 1858 fprintf(fp, " return this;\n");
duke@435 1859 }
goetz@6499 1860 fprintf(fp, "}\n");
goetz@6499 1861 fprintf(fp, "\n");
duke@435 1862 }
duke@435 1863
duke@435 1864
duke@435 1865 //------------------------------Emit Routines----------------------------------
duke@435 1866 // Special classes and routines for defining node emit routines which output
duke@435 1867 // target specific instruction object encodings.
duke@435 1868 // Define the ___Node::emit() routine
duke@435 1869 //
duke@435 1870 // (1) void ___Node::emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const {
duke@435 1871 // (2) // ... encoding defined by user
duke@435 1872 // (3)
duke@435 1873 // (4) }
duke@435 1874 //
duke@435 1875
duke@435 1876 class DefineEmitState {
duke@435 1877 private:
duke@435 1878 enum reloc_format { RELOC_NONE = -1,
duke@435 1879 RELOC_IMMEDIATE = 0,
duke@435 1880 RELOC_DISP = 1,
duke@435 1881 RELOC_CALL_DISP = 2 };
duke@435 1882 enum literal_status{ LITERAL_NOT_SEEN = 0,
duke@435 1883 LITERAL_SEEN = 1,
duke@435 1884 LITERAL_ACCESSED = 2,
duke@435 1885 LITERAL_OUTPUT = 3 };
duke@435 1886 // Temporaries that describe current operand
duke@435 1887 bool _cleared;
duke@435 1888 OpClassForm *_opclass;
duke@435 1889 OperandForm *_operand;
duke@435 1890 int _operand_idx;
duke@435 1891 const char *_local_name;
duke@435 1892 const char *_operand_name;
duke@435 1893 bool _doing_disp;
duke@435 1894 bool _doing_constant;
duke@435 1895 Form::DataType _constant_type;
duke@435 1896 DefineEmitState::literal_status _constant_status;
duke@435 1897 DefineEmitState::literal_status _reg_status;
duke@435 1898 bool _doing_emit8;
duke@435 1899 bool _doing_emit_d32;
duke@435 1900 bool _doing_emit_d16;
duke@435 1901 bool _doing_emit_hi;
duke@435 1902 bool _doing_emit_lo;
duke@435 1903 bool _may_reloc;
duke@435 1904 reloc_format _reloc_form;
duke@435 1905 const char * _reloc_type;
duke@435 1906 bool _processing_noninput;
duke@435 1907
duke@435 1908 NameList _strings_to_emit;
duke@435 1909
duke@435 1910 // Stable state, set by constructor
duke@435 1911 ArchDesc &_AD;
duke@435 1912 FILE *_fp;
duke@435 1913 EncClass &_encoding;
duke@435 1914 InsEncode &_ins_encode;
duke@435 1915 InstructForm &_inst;
duke@435 1916
duke@435 1917 public:
duke@435 1918 DefineEmitState(FILE *fp, ArchDesc &AD, EncClass &encoding,
duke@435 1919 InsEncode &ins_encode, InstructForm &inst)
duke@435 1920 : _AD(AD), _fp(fp), _encoding(encoding), _ins_encode(ins_encode), _inst(inst) {
duke@435 1921 clear();
duke@435 1922 }
duke@435 1923
duke@435 1924 void clear() {
duke@435 1925 _cleared = true;
duke@435 1926 _opclass = NULL;
duke@435 1927 _operand = NULL;
duke@435 1928 _operand_idx = 0;
duke@435 1929 _local_name = "";
duke@435 1930 _operand_name = "";
duke@435 1931 _doing_disp = false;
duke@435 1932 _doing_constant= false;
duke@435 1933 _constant_type = Form::none;
duke@435 1934 _constant_status = LITERAL_NOT_SEEN;
duke@435 1935 _reg_status = LITERAL_NOT_SEEN;
duke@435 1936 _doing_emit8 = false;
duke@435 1937 _doing_emit_d32= false;
duke@435 1938 _doing_emit_d16= false;
duke@435 1939 _doing_emit_hi = false;
duke@435 1940 _doing_emit_lo = false;
duke@435 1941 _may_reloc = false;
duke@435 1942 _reloc_form = RELOC_NONE;
duke@435 1943 _reloc_type = AdlcVMDeps::none_reloc_type();
duke@435 1944 _strings_to_emit.clear();
duke@435 1945 }
duke@435 1946
duke@435 1947 // Track necessary state when identifying a replacement variable
kvn@4161 1948 // @arg rep_var: The formal parameter of the encoding.
duke@435 1949 void update_state(const char *rep_var) {
duke@435 1950 // A replacement variable or one of its subfields
duke@435 1951 // Obtain replacement variable from list
duke@435 1952 if ( (*rep_var) != '$' ) {
duke@435 1953 // A replacement variable, '$' prefix
duke@435 1954 // check_rep_var( rep_var );
duke@435 1955 if ( Opcode::as_opcode_type(rep_var) != Opcode::NOT_AN_OPCODE ) {
duke@435 1956 // No state needed.
duke@435 1957 assert( _opclass == NULL,
duke@435 1958 "'primary', 'secondary' and 'tertiary' don't follow operand.");
twisti@2350 1959 }
twisti@2350 1960 else if ((strcmp(rep_var, "constanttablebase") == 0) ||
twisti@2350 1961 (strcmp(rep_var, "constantoffset") == 0) ||
twisti@2350 1962 (strcmp(rep_var, "constantaddress") == 0)) {
goetz@6484 1963 if (!(_inst.is_mach_constant() || _inst.needs_constant_base())) {
twisti@2350 1964 _AD.syntax_err(_encoding._linenum,
goetz@6484 1965 "Replacement variable %s not allowed in instruct %s (only in MachConstantNode or MachCall).\n",
twisti@2350 1966 rep_var, _encoding._name);
twisti@2350 1967 }
twisti@2350 1968 }
twisti@2350 1969 else {
kvn@4161 1970 // Lookup its position in (formal) parameter list of encoding
duke@435 1971 int param_no = _encoding.rep_var_index(rep_var);
duke@435 1972 if ( param_no == -1 ) {
duke@435 1973 _AD.syntax_err( _encoding._linenum,
duke@435 1974 "Replacement variable %s not found in enc_class %s.\n",
duke@435 1975 rep_var, _encoding._name);
duke@435 1976 }
duke@435 1977
duke@435 1978 // Lookup the corresponding ins_encode parameter
kvn@4161 1979 // This is the argument (actual parameter) to the encoding.
duke@435 1980 const char *inst_rep_var = _ins_encode.rep_var_name(_inst, param_no);
duke@435 1981 if (inst_rep_var == NULL) {
duke@435 1982 _AD.syntax_err( _ins_encode._linenum,
duke@435 1983 "Parameter %s not passed to enc_class %s from instruct %s.\n",
duke@435 1984 rep_var, _encoding._name, _inst._ident);
duke@435 1985 }
duke@435 1986
duke@435 1987 // Check if instruction's actual parameter is a local name in the instruction
duke@435 1988 const Form *local = _inst._localNames[inst_rep_var];
duke@435 1989 OpClassForm *opc = (local != NULL) ? local->is_opclass() : NULL;
duke@435 1990 // Note: assert removed to allow constant and symbolic parameters
duke@435 1991 // assert( opc, "replacement variable was not found in local names");
duke@435 1992 // Lookup the index position iff the replacement variable is a localName
duke@435 1993 int idx = (opc != NULL) ? _inst.operand_position_format(inst_rep_var) : -1;
duke@435 1994
duke@435 1995 if ( idx != -1 ) {
duke@435 1996 // This is a local in the instruction
duke@435 1997 // Update local state info.
duke@435 1998 _opclass = opc;
duke@435 1999 _operand_idx = idx;
duke@435 2000 _local_name = rep_var;
duke@435 2001 _operand_name = inst_rep_var;
duke@435 2002
duke@435 2003 // !!!!!
duke@435 2004 // Do not support consecutive operands.
duke@435 2005 assert( _operand == NULL, "Unimplemented()");
duke@435 2006 _operand = opc->is_operand();
duke@435 2007 }
duke@435 2008 else if( ADLParser::is_literal_constant(inst_rep_var) ) {
duke@435 2009 // Instruction provided a constant expression
duke@435 2010 // Check later that encoding specifies $$$constant to resolve as constant
duke@435 2011 _constant_status = LITERAL_SEEN;
duke@435 2012 }
duke@435 2013 else if( Opcode::as_opcode_type(inst_rep_var) != Opcode::NOT_AN_OPCODE ) {
duke@435 2014 // Instruction provided an opcode: "primary", "secondary", "tertiary"
duke@435 2015 // Check later that encoding specifies $$$constant to resolve as constant
duke@435 2016 _constant_status = LITERAL_SEEN;
duke@435 2017 }
duke@435 2018 else if((_AD.get_registers() != NULL ) && (_AD.get_registers()->getRegDef(inst_rep_var) != NULL)) {
duke@435 2019 // Instruction provided a literal register name for this parameter
duke@435 2020 // Check that encoding specifies $$$reg to resolve.as register.
duke@435 2021 _reg_status = LITERAL_SEEN;
duke@435 2022 }
duke@435 2023 else {
duke@435 2024 // Check for unimplemented functionality before hard failure
duke@435 2025 assert( strcmp(opc->_ident,"label")==0, "Unimplemented() Label");
duke@435 2026 assert( false, "ShouldNotReachHere()");
duke@435 2027 }
duke@435 2028 } // done checking which operand this is.
duke@435 2029 } else {
duke@435 2030 //
duke@435 2031 // A subfield variable, '$$' prefix
duke@435 2032 // Check for fields that may require relocation information.
duke@435 2033 // Then check that literal register parameters are accessed with 'reg' or 'constant'
duke@435 2034 //
duke@435 2035 if ( strcmp(rep_var,"$disp") == 0 ) {
duke@435 2036 _doing_disp = true;
duke@435 2037 assert( _opclass, "Must use operand or operand class before '$disp'");
duke@435 2038 if( _operand == NULL ) {
duke@435 2039 // Only have an operand class, generate run-time check for relocation
duke@435 2040 _may_reloc = true;
duke@435 2041 _reloc_form = RELOC_DISP;
duke@435 2042 _reloc_type = AdlcVMDeps::oop_reloc_type();
duke@435 2043 } else {
duke@435 2044 // Do precise check on operand: is it a ConP or not
duke@435 2045 //
duke@435 2046 // Check interface for value of displacement
duke@435 2047 assert( ( _operand->_interface != NULL ),
duke@435 2048 "$disp can only follow memory interface operand");
duke@435 2049 MemInterface *mem_interface= _operand->_interface->is_MemInterface();
duke@435 2050 assert( mem_interface != NULL,
duke@435 2051 "$disp can only follow memory interface operand");
duke@435 2052 const char *disp = mem_interface->_disp;
duke@435 2053
duke@435 2054 if( disp != NULL && (*disp == '$') ) {
duke@435 2055 // MemInterface::disp contains a replacement variable,
duke@435 2056 // Check if this matches a ConP
duke@435 2057 //
duke@435 2058 // Lookup replacement variable, in operand's component list
duke@435 2059 const char *rep_var_name = disp + 1; // Skip '$'
duke@435 2060 const Component *comp = _operand->_components.search(rep_var_name);
duke@435 2061 assert( comp != NULL,"Replacement variable not found in components");
duke@435 2062 const char *type = comp->_type;
duke@435 2063 // Lookup operand form for replacement variable's type
duke@435 2064 const Form *form = _AD.globalNames()[type];
duke@435 2065 assert( form != NULL, "Replacement variable's type not found");
duke@435 2066 OperandForm *op = form->is_operand();
duke@435 2067 assert( op, "Attempting to emit a non-register or non-constant");
duke@435 2068 // Check if this is a constant
duke@435 2069 if (op->_matrule && op->_matrule->is_base_constant(_AD.globalNames())) {
duke@435 2070 // Check which constant this name maps to: _c0, _c1, ..., _cn
duke@435 2071 // const int idx = _operand.constant_position(_AD.globalNames(), comp);
duke@435 2072 // assert( idx != -1, "Constant component not found in operand");
duke@435 2073 Form::DataType dtype = op->is_base_constant(_AD.globalNames());
duke@435 2074 if ( dtype == Form::idealP ) {
duke@435 2075 _may_reloc = true;
duke@435 2076 // No longer true that idealP is always an oop
duke@435 2077 _reloc_form = RELOC_DISP;
duke@435 2078 _reloc_type = AdlcVMDeps::oop_reloc_type();
duke@435 2079 }
duke@435 2080 }
duke@435 2081
duke@435 2082 else if( _operand->is_user_name_for_sReg() != Form::none ) {
duke@435 2083 // The only non-constant allowed access to disp is an operand sRegX in a stackSlotX
duke@435 2084 assert( op->ideal_to_sReg_type(type) != Form::none, "StackSlots access displacements using 'sRegs'");
duke@435 2085 _may_reloc = false;
duke@435 2086 } else {
duke@435 2087 assert( false, "fatal(); Only stackSlots can access a non-constant using 'disp'");
duke@435 2088 }
duke@435 2089 }
duke@435 2090 } // finished with precise check of operand for relocation.
duke@435 2091 } // finished with subfield variable
duke@435 2092 else if ( strcmp(rep_var,"$constant") == 0 ) {
duke@435 2093 _doing_constant = true;
duke@435 2094 if ( _constant_status == LITERAL_NOT_SEEN ) {
duke@435 2095 // Check operand for type of constant
duke@435 2096 assert( _operand, "Must use operand before '$$constant'");
duke@435 2097 Form::DataType dtype = _operand->is_base_constant(_AD.globalNames());
duke@435 2098 _constant_type = dtype;
duke@435 2099 if ( dtype == Form::idealP ) {
duke@435 2100 _may_reloc = true;
duke@435 2101 // No longer true that idealP is always an oop
duke@435 2102 // // _must_reloc = true;
duke@435 2103 _reloc_form = RELOC_IMMEDIATE;
duke@435 2104 _reloc_type = AdlcVMDeps::oop_reloc_type();
duke@435 2105 } else {
duke@435 2106 // No relocation information needed
duke@435 2107 }
duke@435 2108 } else {
duke@435 2109 // User-provided literals may not require relocation information !!!!!
duke@435 2110 assert( _constant_status == LITERAL_SEEN, "Must know we are processing a user-provided literal");
duke@435 2111 }
duke@435 2112 }
duke@435 2113 else if ( strcmp(rep_var,"$label") == 0 ) {
duke@435 2114 // Calls containing labels require relocation
duke@435 2115 if ( _inst.is_ideal_call() ) {
duke@435 2116 _may_reloc = true;
duke@435 2117 // !!!!! !!!!!
duke@435 2118 _reloc_type = AdlcVMDeps::none_reloc_type();
duke@435 2119 }
duke@435 2120 }
duke@435 2121
duke@435 2122 // literal register parameter must be accessed as a 'reg' field.
duke@435 2123 if ( _reg_status != LITERAL_NOT_SEEN ) {
duke@435 2124 assert( _reg_status == LITERAL_SEEN, "Must have seen register literal before now");
duke@435 2125 if (strcmp(rep_var,"$reg") == 0 || reg_conversion(rep_var) != NULL) {
duke@435 2126 _reg_status = LITERAL_ACCESSED;
duke@435 2127 } else {
goetz@6481 2128 _AD.syntax_err(_encoding._linenum,
goetz@6481 2129 "Invalid access to literal register parameter '%s' in %s.\n",
goetz@6481 2130 rep_var, _encoding._name);
duke@435 2131 assert( false, "invalid access to literal register parameter");
duke@435 2132 }
duke@435 2133 }
duke@435 2134 // literal constant parameters must be accessed as a 'constant' field
goetz@6481 2135 if (_constant_status != LITERAL_NOT_SEEN) {
goetz@6481 2136 assert(_constant_status == LITERAL_SEEN, "Must have seen constant literal before now");
goetz@6481 2137 if (strcmp(rep_var,"$constant") == 0) {
goetz@6481 2138 _constant_status = LITERAL_ACCESSED;
duke@435 2139 } else {
goetz@6481 2140 _AD.syntax_err(_encoding._linenum,
goetz@6481 2141 "Invalid access to literal constant parameter '%s' in %s.\n",
goetz@6481 2142 rep_var, _encoding._name);
duke@435 2143 }
duke@435 2144 }
duke@435 2145 } // end replacement and/or subfield
duke@435 2146
duke@435 2147 }
duke@435 2148
duke@435 2149 void add_rep_var(const char *rep_var) {
duke@435 2150 // Handle subfield and replacement variables.
duke@435 2151 if ( ( *rep_var == '$' ) && ( *(rep_var+1) == '$' ) ) {
duke@435 2152 // Check for emit prefix, '$$emit32'
duke@435 2153 assert( _cleared, "Can not nest $$$emit32");
duke@435 2154 if ( strcmp(rep_var,"$$emit32") == 0 ) {
duke@435 2155 _doing_emit_d32 = true;
duke@435 2156 }
duke@435 2157 else if ( strcmp(rep_var,"$$emit16") == 0 ) {
duke@435 2158 _doing_emit_d16 = true;
duke@435 2159 }
duke@435 2160 else if ( strcmp(rep_var,"$$emit_hi") == 0 ) {
duke@435 2161 _doing_emit_hi = true;
duke@435 2162 }
duke@435 2163 else if ( strcmp(rep_var,"$$emit_lo") == 0 ) {
duke@435 2164 _doing_emit_lo = true;
duke@435 2165 }
duke@435 2166 else if ( strcmp(rep_var,"$$emit8") == 0 ) {
duke@435 2167 _doing_emit8 = true;
duke@435 2168 }
duke@435 2169 else {
duke@435 2170 _AD.syntax_err(_encoding._linenum, "Unsupported $$operation '%s'\n",rep_var);
duke@435 2171 assert( false, "fatal();");
duke@435 2172 }
duke@435 2173 }
duke@435 2174 else {
duke@435 2175 // Update state for replacement variables
duke@435 2176 update_state( rep_var );
duke@435 2177 _strings_to_emit.addName(rep_var);
duke@435 2178 }
duke@435 2179 _cleared = false;
duke@435 2180 }
duke@435 2181
duke@435 2182 void emit_replacement() {
duke@435 2183 // A replacement variable or one of its subfields
duke@435 2184 // Obtain replacement variable from list
duke@435 2185 // const char *ec_rep_var = encoding->_rep_vars.iter();
duke@435 2186 const char *rep_var;
duke@435 2187 _strings_to_emit.reset();
duke@435 2188 while ( (rep_var = _strings_to_emit.iter()) != NULL ) {
duke@435 2189
duke@435 2190 if ( (*rep_var) == '$' ) {
duke@435 2191 // A subfield variable, '$$' prefix
duke@435 2192 emit_field( rep_var );
duke@435 2193 } else {
twisti@1059 2194 if (_strings_to_emit.peek() != NULL &&
twisti@1059 2195 strcmp(_strings_to_emit.peek(), "$Address") == 0) {
twisti@1059 2196 fprintf(_fp, "Address::make_raw(");
twisti@1059 2197
twisti@1059 2198 emit_rep_var( rep_var );
twisti@1059 2199 fprintf(_fp,"->base(ra_,this,idx%d), ", _operand_idx);
twisti@1059 2200
twisti@1059 2201 _reg_status = LITERAL_ACCESSED;
twisti@1059 2202 emit_rep_var( rep_var );
twisti@1059 2203 fprintf(_fp,"->index(ra_,this,idx%d), ", _operand_idx);
twisti@1059 2204
twisti@1059 2205 _reg_status = LITERAL_ACCESSED;
twisti@1059 2206 emit_rep_var( rep_var );
twisti@1059 2207 fprintf(_fp,"->scale(), ");
twisti@1059 2208
twisti@1059 2209 _reg_status = LITERAL_ACCESSED;
twisti@1059 2210 emit_rep_var( rep_var );
twisti@1059 2211 Form::DataType stack_type = _operand ? _operand->is_user_name_for_sReg() : Form::none;
twisti@1059 2212 if( _operand && _operand_idx==0 && stack_type != Form::none ) {
twisti@1059 2213 fprintf(_fp,"->disp(ra_,this,0), ");
twisti@1059 2214 } else {
twisti@1059 2215 fprintf(_fp,"->disp(ra_,this,idx%d), ", _operand_idx);
twisti@1059 2216 }
twisti@1059 2217
twisti@1059 2218 _reg_status = LITERAL_ACCESSED;
twisti@1059 2219 emit_rep_var( rep_var );
coleenp@4037 2220 fprintf(_fp,"->disp_reloc())");
twisti@1059 2221
twisti@1059 2222 // skip trailing $Address
twisti@1059 2223 _strings_to_emit.iter();
twisti@1059 2224 } else {
twisti@1059 2225 // A replacement variable, '$' prefix
twisti@1059 2226 const char* next = _strings_to_emit.peek();
twisti@1059 2227 const char* next2 = _strings_to_emit.peek(2);
twisti@1059 2228 if (next != NULL && next2 != NULL && strcmp(next2, "$Register") == 0 &&
twisti@1059 2229 (strcmp(next, "$base") == 0 || strcmp(next, "$index") == 0)) {
twisti@1059 2230 // handle $rev_var$$base$$Register and $rev_var$$index$$Register by
twisti@1059 2231 // producing as_Register(opnd_array(#)->base(ra_,this,idx1)).
twisti@1059 2232 fprintf(_fp, "as_Register(");
twisti@1059 2233 // emit the operand reference
twisti@1059 2234 emit_rep_var( rep_var );
twisti@1059 2235 rep_var = _strings_to_emit.iter();
twisti@1059 2236 assert(strcmp(rep_var, "$base") == 0 || strcmp(rep_var, "$index") == 0, "bad pattern");
twisti@1059 2237 // handle base or index
twisti@1059 2238 emit_field(rep_var);
twisti@1059 2239 rep_var = _strings_to_emit.iter();
twisti@1059 2240 assert(strcmp(rep_var, "$Register") == 0, "bad pattern");
twisti@1059 2241 // close up the parens
twisti@1059 2242 fprintf(_fp, ")");
twisti@1059 2243 } else {
twisti@1059 2244 emit_rep_var( rep_var );
twisti@1059 2245 }
twisti@1059 2246 }
duke@435 2247 } // end replacement and/or subfield
duke@435 2248 }
duke@435 2249 }
duke@435 2250
duke@435 2251 void emit_reloc_type(const char* type) {
duke@435 2252 fprintf(_fp, "%s", type)
duke@435 2253 ;
duke@435 2254 }
duke@435 2255
duke@435 2256
duke@435 2257 void emit() {
duke@435 2258 //
duke@435 2259 // "emit_d32_reloc(" or "emit_hi_reloc" or "emit_lo_reloc"
duke@435 2260 //
duke@435 2261 // Emit the function name when generating an emit function
duke@435 2262 if ( _doing_emit_d32 || _doing_emit_hi || _doing_emit_lo ) {
duke@435 2263 const char *d32_hi_lo = _doing_emit_d32 ? "d32" : (_doing_emit_hi ? "hi" : "lo");
duke@435 2264 // In general, relocatable isn't known at compiler compile time.
duke@435 2265 // Check results of prior scan
duke@435 2266 if ( ! _may_reloc ) {
duke@435 2267 // Definitely don't need relocation information
duke@435 2268 fprintf( _fp, "emit_%s(cbuf, ", d32_hi_lo );
duke@435 2269 emit_replacement(); fprintf(_fp, ")");
duke@435 2270 }
duke@435 2271 else {
duke@435 2272 // Emit RUNTIME CHECK to see if value needs relocation info
duke@435 2273 // If emitting a relocatable address, use 'emit_d32_reloc'
duke@435 2274 const char *disp_constant = _doing_disp ? "disp" : _doing_constant ? "constant" : "INVALID";
duke@435 2275 assert( (_doing_disp || _doing_constant)
duke@435 2276 && !(_doing_disp && _doing_constant),
duke@435 2277 "Must be emitting either a displacement or a constant");
duke@435 2278 fprintf(_fp,"\n");
coleenp@4037 2279 fprintf(_fp,"if ( opnd_array(%d)->%s_reloc() != relocInfo::none ) {\n",
duke@435 2280 _operand_idx, disp_constant);
duke@435 2281 fprintf(_fp," ");
coleenp@4037 2282 fprintf(_fp,"emit_%s_reloc(cbuf, ", d32_hi_lo );
coleenp@4037 2283 emit_replacement(); fprintf(_fp,", ");
coleenp@4037 2284 fprintf(_fp,"opnd_array(%d)->%s_reloc(), ",
coleenp@4037 2285 _operand_idx, disp_constant);
coleenp@4037 2286 fprintf(_fp, "%d", _reloc_form);fprintf(_fp, ");");
coleenp@4037 2287 fprintf(_fp,"\n");
duke@435 2288 fprintf(_fp,"} else {\n");
duke@435 2289 fprintf(_fp," emit_%s(cbuf, ", d32_hi_lo);
duke@435 2290 emit_replacement(); fprintf(_fp, ");\n"); fprintf(_fp,"}");
duke@435 2291 }
duke@435 2292 }
duke@435 2293 else if ( _doing_emit_d16 ) {
duke@435 2294 // Relocation of 16-bit values is not supported
duke@435 2295 fprintf(_fp,"emit_d16(cbuf, ");
duke@435 2296 emit_replacement(); fprintf(_fp, ")");
duke@435 2297 // No relocation done for 16-bit values
duke@435 2298 }
duke@435 2299 else if ( _doing_emit8 ) {
duke@435 2300 // Relocation of 8-bit values is not supported
duke@435 2301 fprintf(_fp,"emit_d8(cbuf, ");
duke@435 2302 emit_replacement(); fprintf(_fp, ")");
duke@435 2303 // No relocation done for 8-bit values
duke@435 2304 }
duke@435 2305 else {
duke@435 2306 // Not an emit# command, just output the replacement string.
duke@435 2307 emit_replacement();
duke@435 2308 }
duke@435 2309
duke@435 2310 // Get ready for next state collection.
duke@435 2311 clear();
duke@435 2312 }
duke@435 2313
duke@435 2314 private:
duke@435 2315
duke@435 2316 // recognizes names which represent MacroAssembler register types
duke@435 2317 // and return the conversion function to build them from OptoReg
duke@435 2318 const char* reg_conversion(const char* rep_var) {
duke@435 2319 if (strcmp(rep_var,"$Register") == 0) return "as_Register";
duke@435 2320 if (strcmp(rep_var,"$FloatRegister") == 0) return "as_FloatRegister";
duke@435 2321 #if defined(IA32) || defined(AMD64)
duke@435 2322 if (strcmp(rep_var,"$XMMRegister") == 0) return "as_XMMRegister";
duke@435 2323 #endif
goetz@6494 2324 if (strcmp(rep_var,"$CondRegister") == 0) return "as_ConditionRegister";
duke@435 2325 return NULL;
duke@435 2326 }
duke@435 2327
duke@435 2328 void emit_field(const char *rep_var) {
duke@435 2329 const char* reg_convert = reg_conversion(rep_var);
duke@435 2330
duke@435 2331 // A subfield variable, '$$subfield'
duke@435 2332 if ( strcmp(rep_var, "$reg") == 0 || reg_convert != NULL) {
duke@435 2333 // $reg form or the $Register MacroAssembler type conversions
duke@435 2334 assert( _operand_idx != -1,
duke@435 2335 "Must use this subfield after operand");
duke@435 2336 if( _reg_status == LITERAL_NOT_SEEN ) {
duke@435 2337 if (_processing_noninput) {
duke@435 2338 const Form *local = _inst._localNames[_operand_name];
duke@435 2339 OperandForm *oper = local->is_operand();
duke@435 2340 const RegDef* first = oper->get_RegClass()->find_first_elem();
duke@435 2341 if (reg_convert != NULL) {
duke@435 2342 fprintf(_fp, "%s(%s_enc)", reg_convert, first->_regname);
duke@435 2343 } else {
duke@435 2344 fprintf(_fp, "%s_enc", first->_regname);
duke@435 2345 }
duke@435 2346 } else {
duke@435 2347 fprintf(_fp,"->%s(ra_,this", reg_convert != NULL ? reg_convert : "reg");
duke@435 2348 // Add parameter for index position, if not result operand
duke@435 2349 if( _operand_idx != 0 ) fprintf(_fp,",idx%d", _operand_idx);
duke@435 2350 fprintf(_fp,")");
kvn@4161 2351 fprintf(_fp, "/* %s */", _operand_name);
duke@435 2352 }
duke@435 2353 } else {
duke@435 2354 assert( _reg_status == LITERAL_OUTPUT, "should have output register literal in emit_rep_var");
duke@435 2355 // Register literal has already been sent to output file, nothing more needed
duke@435 2356 }
duke@435 2357 }
duke@435 2358 else if ( strcmp(rep_var,"$base") == 0 ) {
duke@435 2359 assert( _operand_idx != -1,
duke@435 2360 "Must use this subfield after operand");
duke@435 2361 assert( ! _may_reloc, "UnImplemented()");
duke@435 2362 fprintf(_fp,"->base(ra_,this,idx%d)", _operand_idx);
duke@435 2363 }
duke@435 2364 else if ( strcmp(rep_var,"$index") == 0 ) {
duke@435 2365 assert( _operand_idx != -1,
duke@435 2366 "Must use this subfield after operand");
duke@435 2367 assert( ! _may_reloc, "UnImplemented()");
duke@435 2368 fprintf(_fp,"->index(ra_,this,idx%d)", _operand_idx);
duke@435 2369 }
duke@435 2370 else if ( strcmp(rep_var,"$scale") == 0 ) {
duke@435 2371 assert( ! _may_reloc, "UnImplemented()");
duke@435 2372 fprintf(_fp,"->scale()");
duke@435 2373 }
duke@435 2374 else if ( strcmp(rep_var,"$cmpcode") == 0 ) {
duke@435 2375 assert( ! _may_reloc, "UnImplemented()");
duke@435 2376 fprintf(_fp,"->ccode()");
duke@435 2377 }
duke@435 2378 else if ( strcmp(rep_var,"$constant") == 0 ) {
duke@435 2379 if( _constant_status == LITERAL_NOT_SEEN ) {
duke@435 2380 if ( _constant_type == Form::idealD ) {
duke@435 2381 fprintf(_fp,"->constantD()");
duke@435 2382 } else if ( _constant_type == Form::idealF ) {
duke@435 2383 fprintf(_fp,"->constantF()");
duke@435 2384 } else if ( _constant_type == Form::idealL ) {
duke@435 2385 fprintf(_fp,"->constantL()");
duke@435 2386 } else {
duke@435 2387 fprintf(_fp,"->constant()");
duke@435 2388 }
duke@435 2389 } else {
duke@435 2390 assert( _constant_status == LITERAL_OUTPUT, "should have output constant literal in emit_rep_var");
kvn@4161 2391 // Constant literal has already been sent to output file, nothing more needed
duke@435 2392 }
duke@435 2393 }
duke@435 2394 else if ( strcmp(rep_var,"$disp") == 0 ) {
duke@435 2395 Form::DataType stack_type = _operand ? _operand->is_user_name_for_sReg() : Form::none;
duke@435 2396 if( _operand && _operand_idx==0 && stack_type != Form::none ) {
duke@435 2397 fprintf(_fp,"->disp(ra_,this,0)");
duke@435 2398 } else {
duke@435 2399 fprintf(_fp,"->disp(ra_,this,idx%d)", _operand_idx);
duke@435 2400 }
duke@435 2401 }
duke@435 2402 else if ( strcmp(rep_var,"$label") == 0 ) {
duke@435 2403 fprintf(_fp,"->label()");
duke@435 2404 }
duke@435 2405 else if ( strcmp(rep_var,"$method") == 0 ) {
duke@435 2406 fprintf(_fp,"->method()");
duke@435 2407 }
duke@435 2408 else {
duke@435 2409 printf("emit_field: %s\n",rep_var);
kvn@4161 2410 globalAD->syntax_err(_inst._linenum, "Unknown replacement variable %s in format statement of %s.",
kvn@4161 2411 rep_var, _inst._ident);
duke@435 2412 assert( false, "UnImplemented()");
duke@435 2413 }
duke@435 2414 }
duke@435 2415
duke@435 2416
duke@435 2417 void emit_rep_var(const char *rep_var) {
duke@435 2418 _processing_noninput = false;
duke@435 2419 // A replacement variable, originally '$'
duke@435 2420 if ( Opcode::as_opcode_type(rep_var) != Opcode::NOT_AN_OPCODE ) {
never@850 2421 if (!_inst._opcode->print_opcode(_fp, Opcode::as_opcode_type(rep_var) )) {
never@850 2422 // Missing opcode
never@850 2423 _AD.syntax_err( _inst._linenum,
never@850 2424 "Missing $%s opcode definition in %s, used by encoding %s\n",
never@850 2425 rep_var, _inst._ident, _encoding._name);
never@850 2426 }
duke@435 2427 }
twisti@2350 2428 else if (strcmp(rep_var, "constanttablebase") == 0) {
twisti@2350 2429 fprintf(_fp, "as_Register(ra_->get_encode(in(mach_constant_base_node_input())))");
twisti@2350 2430 }
twisti@2350 2431 else if (strcmp(rep_var, "constantoffset") == 0) {
twisti@2350 2432 fprintf(_fp, "constant_offset()");
twisti@2350 2433 }
twisti@2350 2434 else if (strcmp(rep_var, "constantaddress") == 0) {
twisti@2350 2435 fprintf(_fp, "InternalAddress(__ code()->consts()->start() + constant_offset())");
twisti@2350 2436 }
duke@435 2437 else {
duke@435 2438 // Lookup its position in parameter list
duke@435 2439 int param_no = _encoding.rep_var_index(rep_var);
duke@435 2440 if ( param_no == -1 ) {
duke@435 2441 _AD.syntax_err( _encoding._linenum,
duke@435 2442 "Replacement variable %s not found in enc_class %s.\n",
duke@435 2443 rep_var, _encoding._name);
duke@435 2444 }
duke@435 2445 // Lookup the corresponding ins_encode parameter
duke@435 2446 const char *inst_rep_var = _ins_encode.rep_var_name(_inst, param_no);
duke@435 2447
duke@435 2448 // Check if instruction's actual parameter is a local name in the instruction
duke@435 2449 const Form *local = _inst._localNames[inst_rep_var];
duke@435 2450 OpClassForm *opc = (local != NULL) ? local->is_opclass() : NULL;
duke@435 2451 // Note: assert removed to allow constant and symbolic parameters
duke@435 2452 // assert( opc, "replacement variable was not found in local names");
duke@435 2453 // Lookup the index position iff the replacement variable is a localName
duke@435 2454 int idx = (opc != NULL) ? _inst.operand_position_format(inst_rep_var) : -1;
duke@435 2455 if( idx != -1 ) {
duke@435 2456 if (_inst.is_noninput_operand(idx)) {
duke@435 2457 // This operand isn't a normal input so printing it is done
duke@435 2458 // specially.
duke@435 2459 _processing_noninput = true;
duke@435 2460 } else {
duke@435 2461 // Output the emit code for this operand
duke@435 2462 fprintf(_fp,"opnd_array(%d)",idx);
duke@435 2463 }
duke@435 2464 assert( _operand == opc->is_operand(),
duke@435 2465 "Previous emit $operand does not match current");
duke@435 2466 }
duke@435 2467 else if( ADLParser::is_literal_constant(inst_rep_var) ) {
duke@435 2468 // else check if it is a constant expression
duke@435 2469 // Removed following assert to allow primitive C types as arguments to encodings
duke@435 2470 // assert( _constant_status == LITERAL_ACCESSED, "Must be processing a literal constant parameter");
duke@435 2471 fprintf(_fp,"(%s)", inst_rep_var);
duke@435 2472 _constant_status = LITERAL_OUTPUT;
duke@435 2473 }
duke@435 2474 else if( Opcode::as_opcode_type(inst_rep_var) != Opcode::NOT_AN_OPCODE ) {
duke@435 2475 // else check if "primary", "secondary", "tertiary"
duke@435 2476 assert( _constant_status == LITERAL_ACCESSED, "Must be processing a literal constant parameter");
never@850 2477 if (!_inst._opcode->print_opcode(_fp, Opcode::as_opcode_type(inst_rep_var) )) {
never@850 2478 // Missing opcode
never@850 2479 _AD.syntax_err( _inst._linenum,
never@850 2480 "Missing $%s opcode definition in %s\n",
never@850 2481 rep_var, _inst._ident);
never@850 2482
never@850 2483 }
duke@435 2484 _constant_status = LITERAL_OUTPUT;
duke@435 2485 }
duke@435 2486 else if((_AD.get_registers() != NULL ) && (_AD.get_registers()->getRegDef(inst_rep_var) != NULL)) {
duke@435 2487 // Instruction provided a literal register name for this parameter
duke@435 2488 // Check that encoding specifies $$$reg to resolve.as register.
duke@435 2489 assert( _reg_status == LITERAL_ACCESSED, "Must be processing a literal register parameter");
duke@435 2490 fprintf(_fp,"(%s_enc)", inst_rep_var);
duke@435 2491 _reg_status = LITERAL_OUTPUT;
duke@435 2492 }
duke@435 2493 else {
duke@435 2494 // Check for unimplemented functionality before hard failure
duke@435 2495 assert( strcmp(opc->_ident,"label")==0, "Unimplemented() Label");
duke@435 2496 assert( false, "ShouldNotReachHere()");
duke@435 2497 }
duke@435 2498 // all done
duke@435 2499 }
duke@435 2500 }
duke@435 2501
duke@435 2502 }; // end class DefineEmitState
duke@435 2503
duke@435 2504
duke@435 2505 void ArchDesc::defineSize(FILE *fp, InstructForm &inst) {
duke@435 2506
duke@435 2507 //(1)
duke@435 2508 // Output instruction's emit prototype
kvn@4161 2509 fprintf(fp,"uint %sNode::size(PhaseRegAlloc *ra_) const {\n",
duke@435 2510 inst._ident);
duke@435 2511
kvn@4161 2512 fprintf(fp, " assert(VerifyOops || MachNode::size(ra_) <= %s, \"bad fixed size\");\n", inst._size);
coleenp@548 2513
duke@435 2514 //(2)
duke@435 2515 // Print the size
kvn@4161 2516 fprintf(fp, " return (VerifyOops ? MachNode::size(ra_) : %s);\n", inst._size);
duke@435 2517
duke@435 2518 // (3) and (4)
goetz@6478 2519 fprintf(fp,"}\n\n");
goetz@6478 2520 }
goetz@6478 2521
goetz@6478 2522 // Emit late expand function.
goetz@6478 2523 void ArchDesc::define_postalloc_expand(FILE *fp, InstructForm &inst) {
goetz@6478 2524 InsEncode *ins_encode = inst._insencode;
goetz@6478 2525
goetz@6478 2526 // Output instruction's postalloc_expand prototype.
goetz@6478 2527 fprintf(fp, "void %sNode::postalloc_expand(GrowableArray <Node *> *nodes, PhaseRegAlloc *ra_) {\n",
goetz@6478 2528 inst._ident);
goetz@6478 2529
goetz@6478 2530 assert((_encode != NULL) && (ins_encode != NULL), "You must define an encode section.");
goetz@6478 2531
goetz@6478 2532 // Output each operand's offset into the array of registers.
goetz@6478 2533 inst.index_temps(fp, _globalNames);
goetz@6478 2534
goetz@6478 2535 // Output variables "unsigned idx_<par_name>", Node *n_<par_name> and "MachOpnd *op_<par_name>"
goetz@6478 2536 // for each parameter <par_name> specified in the encoding.
goetz@6478 2537 ins_encode->reset();
goetz@6478 2538 const char *ec_name = ins_encode->encode_class_iter();
goetz@6478 2539 assert(ec_name != NULL, "late expand must specify an encoding");
goetz@6478 2540
goetz@6478 2541 EncClass *encoding = _encode->encClass(ec_name);
goetz@6478 2542 if (encoding == NULL) {
goetz@6478 2543 fprintf(stderr, "User did not define contents of this encode_class: %s\n", ec_name);
goetz@6478 2544 abort();
goetz@6478 2545 }
goetz@6478 2546 if (ins_encode->current_encoding_num_args() != encoding->num_args()) {
goetz@6478 2547 globalAD->syntax_err(ins_encode->_linenum, "In %s: passing %d arguments to %s but expecting %d",
goetz@6478 2548 inst._ident, ins_encode->current_encoding_num_args(),
goetz@6478 2549 ec_name, encoding->num_args());
goetz@6478 2550 }
goetz@6478 2551
goetz@6478 2552 fprintf(fp, " // Access to ins and operands for late expand.\n");
goetz@6478 2553 const int buflen = 2000;
goetz@6478 2554 char idxbuf[buflen]; char *ib = idxbuf; sprintf(ib, "");
goetz@6478 2555 char nbuf [buflen]; char *nb = nbuf; sprintf(nb, "");
goetz@6478 2556 char opbuf [buflen]; char *ob = opbuf; sprintf(ob, "");
goetz@6478 2557
goetz@6478 2558 encoding->_parameter_type.reset();
goetz@6478 2559 encoding->_parameter_name.reset();
goetz@6478 2560 const char *type = encoding->_parameter_type.iter();
goetz@6478 2561 const char *name = encoding->_parameter_name.iter();
goetz@6478 2562 int param_no = 0;
goetz@6478 2563 for (; (type != NULL) && (name != NULL);
goetz@6478 2564 (type = encoding->_parameter_type.iter()), (name = encoding->_parameter_name.iter())) {
goetz@6478 2565 const char* arg_name = ins_encode->rep_var_name(inst, param_no);
goetz@6478 2566 int idx = inst.operand_position_format(arg_name);
goetz@6478 2567 if (strcmp(arg_name, "constanttablebase") == 0) {
goetz@6478 2568 ib += sprintf(ib, " unsigned idx_%-5s = mach_constant_base_node_input(); \t// %s, \t%s\n",
goetz@6478 2569 name, type, arg_name);
goetz@6478 2570 nb += sprintf(nb, " Node *n_%-7s = lookup(idx_%s);\n", name, name);
goetz@6478 2571 // There is no operand for the constanttablebase.
goetz@6478 2572 } else if (inst.is_noninput_operand(idx)) {
goetz@6478 2573 globalAD->syntax_err(inst._linenum,
goetz@6478 2574 "In %s: you can not pass the non-input %s to a late expand encoding.\n",
goetz@6478 2575 inst._ident, arg_name);
goetz@6478 2576 } else {
goetz@6478 2577 ib += sprintf(ib, " unsigned idx_%-5s = idx%d; \t// %s, \t%s\n",
goetz@6478 2578 name, idx, type, arg_name);
goetz@6478 2579 nb += sprintf(nb, " Node *n_%-7s = lookup(idx_%s);\n", name, name);
goetz@6478 2580 ob += sprintf(ob, " %sOper *op_%s = (%sOper *)opnd_array(%d);\n", type, name, type, idx);
goetz@6478 2581 }
goetz@6478 2582 param_no++;
goetz@6478 2583 }
goetz@6478 2584 assert(ib < &idxbuf[buflen-1] && nb < &nbuf[buflen-1] && ob < &opbuf[buflen-1], "buffer overflow");
goetz@6478 2585
goetz@6478 2586 fprintf(fp, "%s", idxbuf);
goetz@6478 2587 fprintf(fp, " Node *n_region = lookup(0);\n");
goetz@6478 2588 fprintf(fp, "%s%s", nbuf, opbuf);
goetz@6478 2589 fprintf(fp, " Compile *C = ra_->C;\n");
goetz@6478 2590
goetz@6478 2591 // Output this instruction's encodings.
goetz@6478 2592 fprintf(fp, " {");
goetz@6478 2593 const char *ec_code = NULL;
goetz@6478 2594 const char *ec_rep_var = NULL;
goetz@6478 2595 assert(encoding == _encode->encClass(ec_name), "");
goetz@6478 2596
goetz@6478 2597 DefineEmitState pending(fp, *this, *encoding, *ins_encode, inst);
goetz@6478 2598 encoding->_code.reset();
goetz@6478 2599 encoding->_rep_vars.reset();
goetz@6478 2600 // Process list of user-defined strings,
goetz@6478 2601 // and occurrences of replacement variables.
goetz@6478 2602 // Replacement Vars are pushed into a list and then output.
goetz@6478 2603 while ((ec_code = encoding->_code.iter()) != NULL) {
goetz@6478 2604 if (! encoding->_code.is_signal(ec_code)) {
goetz@6478 2605 // Emit pending code.
goetz@6478 2606 pending.emit();
goetz@6478 2607 pending.clear();
goetz@6478 2608 // Emit this code section.
goetz@6478 2609 fprintf(fp, "%s", ec_code);
goetz@6478 2610 } else {
goetz@6478 2611 // A replacement variable or one of its subfields.
goetz@6478 2612 // Obtain replacement variable from list.
goetz@6478 2613 ec_rep_var = encoding->_rep_vars.iter();
goetz@6478 2614 pending.add_rep_var(ec_rep_var);
goetz@6478 2615 }
goetz@6478 2616 }
goetz@6478 2617 // Emit pending code.
goetz@6478 2618 pending.emit();
goetz@6478 2619 pending.clear();
goetz@6478 2620 fprintf(fp, " }\n");
goetz@6478 2621
goetz@6478 2622 fprintf(fp, "}\n\n");
goetz@6478 2623
goetz@6478 2624 ec_name = ins_encode->encode_class_iter();
goetz@6478 2625 assert(ec_name == NULL, "Late expand may only have one encoding.");
duke@435 2626 }
duke@435 2627
twisti@2350 2628 // defineEmit -----------------------------------------------------------------
twisti@2350 2629 void ArchDesc::defineEmit(FILE* fp, InstructForm& inst) {
twisti@2350 2630 InsEncode* encode = inst._insencode;
duke@435 2631
duke@435 2632 // (1)
duke@435 2633 // Output instruction's emit prototype
twisti@2350 2634 fprintf(fp, "void %sNode::emit(CodeBuffer& cbuf, PhaseRegAlloc* ra_) const {\n", inst._ident);
duke@435 2635
duke@435 2636 // If user did not define an encode section,
duke@435 2637 // provide stub that does not generate any machine code.
twisti@2350 2638 if( (_encode == NULL) || (encode == NULL) ) {
duke@435 2639 fprintf(fp, " // User did not define an encode section.\n");
twisti@2350 2640 fprintf(fp, "}\n");
duke@435 2641 return;
duke@435 2642 }
duke@435 2643
duke@435 2644 // Save current instruction's starting address (helps with relocation).
twisti@2350 2645 fprintf(fp, " cbuf.set_insts_mark();\n");
twisti@2350 2646
twisti@2350 2647 // For MachConstantNodes which are ideal jump nodes, fill the jump table.
twisti@2350 2648 if (inst.is_mach_constant() && inst.is_ideal_jump()) {
twisti@2350 2649 fprintf(fp, " ra_->C->constant_table().fill_jump_table(cbuf, (MachConstantNode*) this, _index2label);\n");
twisti@2350 2650 }
duke@435 2651
duke@435 2652 // Output each operand's offset into the array of registers.
twisti@2350 2653 inst.index_temps(fp, _globalNames);
duke@435 2654
duke@435 2655 // Output this instruction's encodings
duke@435 2656 const char *ec_name;
duke@435 2657 bool user_defined = false;
twisti@2350 2658 encode->reset();
twisti@2350 2659 while ((ec_name = encode->encode_class_iter()) != NULL) {
twisti@2350 2660 fprintf(fp, " {\n");
duke@435 2661 // Output user-defined encoding
duke@435 2662 user_defined = true;
duke@435 2663
duke@435 2664 const char *ec_code = NULL;
duke@435 2665 const char *ec_rep_var = NULL;
duke@435 2666 EncClass *encoding = _encode->encClass(ec_name);
duke@435 2667 if (encoding == NULL) {
duke@435 2668 fprintf(stderr, "User did not define contents of this encode_class: %s\n", ec_name);
duke@435 2669 abort();
duke@435 2670 }
duke@435 2671
twisti@2350 2672 if (encode->current_encoding_num_args() != encoding->num_args()) {
twisti@2350 2673 globalAD->syntax_err(encode->_linenum, "In %s: passing %d arguments to %s but expecting %d",
twisti@2350 2674 inst._ident, encode->current_encoding_num_args(),
duke@435 2675 ec_name, encoding->num_args());
duke@435 2676 }
duke@435 2677
twisti@2350 2678 DefineEmitState pending(fp, *this, *encoding, *encode, inst);
duke@435 2679 encoding->_code.reset();
duke@435 2680 encoding->_rep_vars.reset();
duke@435 2681 // Process list of user-defined strings,
duke@435 2682 // and occurrences of replacement variables.
duke@435 2683 // Replacement Vars are pushed into a list and then output
twisti@2350 2684 while ((ec_code = encoding->_code.iter()) != NULL) {
twisti@2350 2685 if (!encoding->_code.is_signal(ec_code)) {
duke@435 2686 // Emit pending code
duke@435 2687 pending.emit();
duke@435 2688 pending.clear();
duke@435 2689 // Emit this code section
twisti@2350 2690 fprintf(fp, "%s", ec_code);
duke@435 2691 } else {
duke@435 2692 // A replacement variable or one of its subfields
duke@435 2693 // Obtain replacement variable from list
duke@435 2694 ec_rep_var = encoding->_rep_vars.iter();
duke@435 2695 pending.add_rep_var(ec_rep_var);
duke@435 2696 }
duke@435 2697 }
duke@435 2698 // Emit pending code
duke@435 2699 pending.emit();
duke@435 2700 pending.clear();
twisti@2350 2701 fprintf(fp, " }\n");
duke@435 2702 } // end while instruction's encodings
duke@435 2703
duke@435 2704 // Check if user stated which encoding to user
duke@435 2705 if ( user_defined == false ) {
duke@435 2706 fprintf(fp, " // User did not define which encode class to use.\n");
duke@435 2707 }
duke@435 2708
duke@435 2709 // (3) and (4)
kvn@4161 2710 fprintf(fp, "}\n\n");
twisti@2350 2711 }
twisti@2350 2712
twisti@2350 2713 // defineEvalConstant ---------------------------------------------------------
twisti@2350 2714 void ArchDesc::defineEvalConstant(FILE* fp, InstructForm& inst) {
twisti@2350 2715 InsEncode* encode = inst._constant;
twisti@2350 2716
twisti@2350 2717 // (1)
twisti@2350 2718 // Output instruction's emit prototype
twisti@2350 2719 fprintf(fp, "void %sNode::eval_constant(Compile* C) {\n", inst._ident);
twisti@2350 2720
twisti@3310 2721 // For ideal jump nodes, add a jump-table entry.
twisti@2350 2722 if (inst.is_ideal_jump()) {
twisti@3310 2723 fprintf(fp, " _constant = C->constant_table().add_jump_table(this);\n");
twisti@2350 2724 }
twisti@2350 2725
twisti@2350 2726 // If user did not define an encode section,
twisti@2350 2727 // provide stub that does not generate any machine code.
twisti@2350 2728 if ((_encode == NULL) || (encode == NULL)) {
twisti@2350 2729 fprintf(fp, " // User did not define an encode section.\n");
twisti@2350 2730 fprintf(fp, "}\n");
twisti@2350 2731 return;
twisti@2350 2732 }
twisti@2350 2733
twisti@2350 2734 // Output this instruction's encodings
twisti@2350 2735 const char *ec_name;
twisti@2350 2736 bool user_defined = false;
twisti@2350 2737 encode->reset();
twisti@2350 2738 while ((ec_name = encode->encode_class_iter()) != NULL) {
twisti@2350 2739 fprintf(fp, " {\n");
twisti@2350 2740 // Output user-defined encoding
twisti@2350 2741 user_defined = true;
twisti@2350 2742
twisti@2350 2743 const char *ec_code = NULL;
twisti@2350 2744 const char *ec_rep_var = NULL;
twisti@2350 2745 EncClass *encoding = _encode->encClass(ec_name);
twisti@2350 2746 if (encoding == NULL) {
twisti@2350 2747 fprintf(stderr, "User did not define contents of this encode_class: %s\n", ec_name);
twisti@2350 2748 abort();
twisti@2350 2749 }
twisti@2350 2750
twisti@2350 2751 if (encode->current_encoding_num_args() != encoding->num_args()) {
twisti@2350 2752 globalAD->syntax_err(encode->_linenum, "In %s: passing %d arguments to %s but expecting %d",
twisti@2350 2753 inst._ident, encode->current_encoding_num_args(),
twisti@2350 2754 ec_name, encoding->num_args());
twisti@2350 2755 }
twisti@2350 2756
twisti@2350 2757 DefineEmitState pending(fp, *this, *encoding, *encode, inst);
twisti@2350 2758 encoding->_code.reset();
twisti@2350 2759 encoding->_rep_vars.reset();
twisti@2350 2760 // Process list of user-defined strings,
twisti@2350 2761 // and occurrences of replacement variables.
twisti@2350 2762 // Replacement Vars are pushed into a list and then output
twisti@2350 2763 while ((ec_code = encoding->_code.iter()) != NULL) {
twisti@2350 2764 if (!encoding->_code.is_signal(ec_code)) {
twisti@2350 2765 // Emit pending code
twisti@2350 2766 pending.emit();
twisti@2350 2767 pending.clear();
twisti@2350 2768 // Emit this code section
twisti@2350 2769 fprintf(fp, "%s", ec_code);
twisti@2350 2770 } else {
twisti@2350 2771 // A replacement variable or one of its subfields
twisti@2350 2772 // Obtain replacement variable from list
twisti@2350 2773 ec_rep_var = encoding->_rep_vars.iter();
twisti@2350 2774 pending.add_rep_var(ec_rep_var);
twisti@2350 2775 }
twisti@2350 2776 }
twisti@2350 2777 // Emit pending code
twisti@2350 2778 pending.emit();
twisti@2350 2779 pending.clear();
twisti@2350 2780 fprintf(fp, " }\n");
twisti@2350 2781 } // end while instruction's encodings
twisti@2350 2782
twisti@2350 2783 // Check if user stated which encoding to user
twisti@2350 2784 if (user_defined == false) {
twisti@2350 2785 fprintf(fp, " // User did not define which encode class to use.\n");
twisti@2350 2786 }
twisti@2350 2787
twisti@2350 2788 // (3) and (4)
twisti@2350 2789 fprintf(fp, "}\n");
duke@435 2790 }
duke@435 2791
duke@435 2792 // ---------------------------------------------------------------------------
duke@435 2793 //--------Utilities to build MachOper and MachNode derived Classes------------
duke@435 2794 // ---------------------------------------------------------------------------
duke@435 2795
duke@435 2796 //------------------------------Utilities to build Operand Classes------------
duke@435 2797 static void defineIn_RegMask(FILE *fp, FormDict &globals, OperandForm &oper) {
duke@435 2798 uint num_edges = oper.num_edges(globals);
duke@435 2799 if( num_edges != 0 ) {
duke@435 2800 // Method header
duke@435 2801 fprintf(fp, "const RegMask *%sOper::in_RegMask(int index) const {\n",
duke@435 2802 oper._ident);
duke@435 2803
duke@435 2804 // Assert that the index is in range.
duke@435 2805 fprintf(fp, " assert(0 <= index && index < %d, \"index out of range\");\n",
duke@435 2806 num_edges);
duke@435 2807
duke@435 2808 // Figure out if all RegMasks are the same.
duke@435 2809 const char* first_reg_class = oper.in_reg_class(0, globals);
duke@435 2810 bool all_same = true;
duke@435 2811 assert(first_reg_class != NULL, "did not find register mask");
duke@435 2812
duke@435 2813 for (uint index = 1; all_same && index < num_edges; index++) {
duke@435 2814 const char* some_reg_class = oper.in_reg_class(index, globals);
duke@435 2815 assert(some_reg_class != NULL, "did not find register mask");
duke@435 2816 if (strcmp(first_reg_class, some_reg_class) != 0) {
duke@435 2817 all_same = false;
duke@435 2818 }
duke@435 2819 }
duke@435 2820
duke@435 2821 if (all_same) {
duke@435 2822 // Return the sole RegMask.
duke@435 2823 if (strcmp(first_reg_class, "stack_slots") == 0) {
duke@435 2824 fprintf(fp," return &(Compile::current()->FIRST_STACK_mask());\n");
duke@435 2825 } else {
neliasso@4906 2826 const char* first_reg_class_to_upper = toUpper(first_reg_class);
neliasso@4906 2827 fprintf(fp," return &%s_mask();\n", first_reg_class_to_upper);
neliasso@4906 2828 delete[] first_reg_class_to_upper;
duke@435 2829 }
duke@435 2830 } else {
duke@435 2831 // Build a switch statement to return the desired mask.
duke@435 2832 fprintf(fp," switch (index) {\n");
duke@435 2833
duke@435 2834 for (uint index = 0; index < num_edges; index++) {
duke@435 2835 const char *reg_class = oper.in_reg_class(index, globals);
duke@435 2836 assert(reg_class != NULL, "did not find register mask");
duke@435 2837 if( !strcmp(reg_class, "stack_slots") ) {
duke@435 2838 fprintf(fp, " case %d: return &(Compile::current()->FIRST_STACK_mask());\n", index);
duke@435 2839 } else {
neliasso@4906 2840 const char* reg_class_to_upper = toUpper(reg_class);
neliasso@4906 2841 fprintf(fp, " case %d: return &%s_mask();\n", index, reg_class_to_upper);
neliasso@4906 2842 delete[] reg_class_to_upper;
duke@435 2843 }
duke@435 2844 }
duke@435 2845 fprintf(fp," }\n");
duke@435 2846 fprintf(fp," ShouldNotReachHere();\n");
duke@435 2847 fprintf(fp," return NULL;\n");
duke@435 2848 }
duke@435 2849
duke@435 2850 // Method close
duke@435 2851 fprintf(fp, "}\n\n");
duke@435 2852 }
duke@435 2853 }
duke@435 2854
duke@435 2855 // generate code to create a clone for a class derived from MachOper
duke@435 2856 //
duke@435 2857 // (0) MachOper *MachOperXOper::clone(Compile* C) const {
duke@435 2858 // (1) return new (C) MachXOper( _ccode, _c0, _c1, ..., _cn);
duke@435 2859 // (2) }
duke@435 2860 //
duke@435 2861 static void defineClone(FILE *fp, FormDict &globalNames, OperandForm &oper) {
kvn@4161 2862 fprintf(fp,"MachOper *%sOper::clone(Compile* C) const {\n", oper._ident);
duke@435 2863 // Check for constants that need to be copied over
duke@435 2864 const int num_consts = oper.num_consts(globalNames);
duke@435 2865 const bool is_ideal_bool = oper.is_ideal_bool();
duke@435 2866 if( (num_consts > 0) ) {
kvn@4161 2867 fprintf(fp," return new (C) %sOper(", oper._ident);
duke@435 2868 // generate parameters for constants
duke@435 2869 int i = 0;
duke@435 2870 fprintf(fp,"_c%d", i);
duke@435 2871 for( i = 1; i < num_consts; ++i) {
duke@435 2872 fprintf(fp,", _c%d", i);
duke@435 2873 }
duke@435 2874 // finish line (1)
duke@435 2875 fprintf(fp,");\n");
duke@435 2876 }
duke@435 2877 else {
duke@435 2878 assert( num_consts == 0, "Currently support zero or one constant per operand clone function");
kvn@4161 2879 fprintf(fp," return new (C) %sOper();\n", oper._ident);
duke@435 2880 }
duke@435 2881 // finish method
duke@435 2882 fprintf(fp,"}\n");
duke@435 2883 }
duke@435 2884
duke@435 2885 // Helper functions for bug 4796752, abstracted with minimal modification
duke@435 2886 // from define_oper_interface()
duke@435 2887 OperandForm *rep_var_to_operand(const char *encoding, OperandForm &oper, FormDict &globals) {
duke@435 2888 OperandForm *op = NULL;
duke@435 2889 // Check for replacement variable
duke@435 2890 if( *encoding == '$' ) {
duke@435 2891 // Replacement variable
duke@435 2892 const char *rep_var = encoding + 1;
duke@435 2893 // Lookup replacement variable, rep_var, in operand's component list
duke@435 2894 const Component *comp = oper._components.search(rep_var);
duke@435 2895 assert( comp != NULL, "Replacement variable not found in components");
duke@435 2896 // Lookup operand form for replacement variable's type
duke@435 2897 const char *type = comp->_type;
duke@435 2898 Form *form = (Form*)globals[type];
duke@435 2899 assert( form != NULL, "Replacement variable's type not found");
duke@435 2900 op = form->is_operand();
duke@435 2901 assert( op, "Attempting to emit a non-register or non-constant");
duke@435 2902 }
duke@435 2903
duke@435 2904 return op;
duke@435 2905 }
duke@435 2906
duke@435 2907 int rep_var_to_constant_index(const char *encoding, OperandForm &oper, FormDict &globals) {
duke@435 2908 int idx = -1;
duke@435 2909 // Check for replacement variable
duke@435 2910 if( *encoding == '$' ) {
duke@435 2911 // Replacement variable
duke@435 2912 const char *rep_var = encoding + 1;
duke@435 2913 // Lookup replacement variable, rep_var, in operand's component list
duke@435 2914 const Component *comp = oper._components.search(rep_var);
duke@435 2915 assert( comp != NULL, "Replacement variable not found in components");
duke@435 2916 // Lookup operand form for replacement variable's type
duke@435 2917 const char *type = comp->_type;
duke@435 2918 Form *form = (Form*)globals[type];
duke@435 2919 assert( form != NULL, "Replacement variable's type not found");
duke@435 2920 OperandForm *op = form->is_operand();
duke@435 2921 assert( op, "Attempting to emit a non-register or non-constant");
duke@435 2922 // Check that this is a constant and find constant's index:
duke@435 2923 if (op->_matrule && op->_matrule->is_base_constant(globals)) {
duke@435 2924 idx = oper.constant_position(globals, comp);
duke@435 2925 }
duke@435 2926 }
duke@435 2927
duke@435 2928 return idx;
duke@435 2929 }
duke@435 2930
duke@435 2931 bool is_regI(const char *encoding, OperandForm &oper, FormDict &globals ) {
duke@435 2932 bool is_regI = false;
duke@435 2933
duke@435 2934 OperandForm *op = rep_var_to_operand(encoding, oper, globals);
duke@435 2935 if( op != NULL ) {
duke@435 2936 // Check that this is a register
duke@435 2937 if ( (op->_matrule && op->_matrule->is_base_register(globals)) ) {
duke@435 2938 // Register
duke@435 2939 const char* ideal = op->ideal_type(globals);
duke@435 2940 is_regI = (ideal && (op->ideal_to_Reg_type(ideal) == Form::idealI));
duke@435 2941 }
duke@435 2942 }
duke@435 2943
duke@435 2944 return is_regI;
duke@435 2945 }
duke@435 2946
duke@435 2947 bool is_conP(const char *encoding, OperandForm &oper, FormDict &globals ) {
duke@435 2948 bool is_conP = false;
duke@435 2949
duke@435 2950 OperandForm *op = rep_var_to_operand(encoding, oper, globals);
duke@435 2951 if( op != NULL ) {
duke@435 2952 // Check that this is a constant pointer
duke@435 2953 if (op->_matrule && op->_matrule->is_base_constant(globals)) {
duke@435 2954 // Constant
duke@435 2955 Form::DataType dtype = op->is_base_constant(globals);
duke@435 2956 is_conP = (dtype == Form::idealP);
duke@435 2957 }
duke@435 2958 }
duke@435 2959
duke@435 2960 return is_conP;
duke@435 2961 }
duke@435 2962
duke@435 2963
duke@435 2964 // Define a MachOper interface methods
duke@435 2965 void ArchDesc::define_oper_interface(FILE *fp, OperandForm &oper, FormDict &globals,
duke@435 2966 const char *name, const char *encoding) {
duke@435 2967 bool emit_position = false;
duke@435 2968 int position = -1;
duke@435 2969
duke@435 2970 fprintf(fp," virtual int %s", name);
duke@435 2971 // Generate access method for base, index, scale, disp, ...
duke@435 2972 if( (strcmp(name,"base") == 0) || (strcmp(name,"index") == 0) ) {
duke@435 2973 fprintf(fp,"(PhaseRegAlloc *ra_, const Node *node, int idx) const { \n");
duke@435 2974 emit_position = true;
duke@435 2975 } else if ( (strcmp(name,"disp") == 0) ) {
duke@435 2976 fprintf(fp,"(PhaseRegAlloc *ra_, const Node *node, int idx) const { \n");
duke@435 2977 } else {
goetz@6478 2978 fprintf(fp, "() const {\n");
duke@435 2979 }
duke@435 2980
duke@435 2981 // Check for hexadecimal value OR replacement variable
duke@435 2982 if( *encoding == '$' ) {
duke@435 2983 // Replacement variable
duke@435 2984 const char *rep_var = encoding + 1;
kvn@4161 2985 fprintf(fp," // Replacement variable: %s\n", encoding+1);
duke@435 2986 // Lookup replacement variable, rep_var, in operand's component list
duke@435 2987 const Component *comp = oper._components.search(rep_var);
duke@435 2988 assert( comp != NULL, "Replacement variable not found in components");
duke@435 2989 // Lookup operand form for replacement variable's type
duke@435 2990 const char *type = comp->_type;
duke@435 2991 Form *form = (Form*)globals[type];
duke@435 2992 assert( form != NULL, "Replacement variable's type not found");
duke@435 2993 OperandForm *op = form->is_operand();
duke@435 2994 assert( op, "Attempting to emit a non-register or non-constant");
duke@435 2995 // Check that this is a register or a constant and generate code:
duke@435 2996 if ( (op->_matrule && op->_matrule->is_base_register(globals)) ) {
duke@435 2997 // Register
duke@435 2998 int idx_offset = oper.register_position( globals, rep_var);
duke@435 2999 position = idx_offset;
duke@435 3000 fprintf(fp," return (int)ra_->get_encode(node->in(idx");
duke@435 3001 if ( idx_offset > 0 ) fprintf(fp, "+%d",idx_offset);
duke@435 3002 fprintf(fp,"));\n");
duke@435 3003 } else if ( op->ideal_to_sReg_type(op->_ident) != Form::none ) {
duke@435 3004 // StackSlot for an sReg comes either from input node or from self, when idx==0
duke@435 3005 fprintf(fp," if( idx != 0 ) {\n");
kvn@4161 3006 fprintf(fp," // Access stack offset (register number) for input operand\n");
duke@435 3007 fprintf(fp," return ra_->reg2offset(ra_->get_reg_first(node->in(idx)));/* sReg */\n");
duke@435 3008 fprintf(fp," }\n");
kvn@4161 3009 fprintf(fp," // Access stack offset (register number) from myself\n");
duke@435 3010 fprintf(fp," return ra_->reg2offset(ra_->get_reg_first(node));/* sReg */\n");
duke@435 3011 } else if (op->_matrule && op->_matrule->is_base_constant(globals)) {
duke@435 3012 // Constant
duke@435 3013 // Check which constant this name maps to: _c0, _c1, ..., _cn
duke@435 3014 const int idx = oper.constant_position(globals, comp);
duke@435 3015 assert( idx != -1, "Constant component not found in operand");
duke@435 3016 // Output code for this constant, type dependent.
duke@435 3017 fprintf(fp," return (int)" );
duke@435 3018 oper.access_constant(fp, globals, (uint)idx /* , const_type */);
duke@435 3019 fprintf(fp,";\n");
duke@435 3020 } else {
duke@435 3021 assert( false, "Attempting to emit a non-register or non-constant");
duke@435 3022 }
duke@435 3023 }
duke@435 3024 else if( *encoding == '0' && *(encoding+1) == 'x' ) {
duke@435 3025 // Hex value
kvn@4161 3026 fprintf(fp," return %s;\n", encoding);
duke@435 3027 } else {
goetz@6478 3028 globalAD->syntax_err(oper._linenum, "In operand %s: Do not support this encode constant: '%s' for %s.",
goetz@6478 3029 oper._ident, encoding, name);
duke@435 3030 assert( false, "Do not support octal or decimal encode constants");
duke@435 3031 }
duke@435 3032 fprintf(fp," }\n");
duke@435 3033
duke@435 3034 if( emit_position && (position != -1) && (oper.num_edges(globals) > 0) ) {
duke@435 3035 fprintf(fp," virtual int %s_position() const { return %d; }\n", name, position);
duke@435 3036 MemInterface *mem_interface = oper._interface->is_MemInterface();
duke@435 3037 const char *base = mem_interface->_base;
duke@435 3038 const char *disp = mem_interface->_disp;
duke@435 3039 if( emit_position && (strcmp(name,"base") == 0)
duke@435 3040 && base != NULL && is_regI(base, oper, globals)
duke@435 3041 && disp != NULL && is_conP(disp, oper, globals) ) {
duke@435 3042 // Found a memory access using a constant pointer for a displacement
duke@435 3043 // and a base register containing an integer offset.
duke@435 3044 // In this case the base and disp are reversed with respect to what
duke@435 3045 // is expected by MachNode::get_base_and_disp() and MachNode::adr_type().
duke@435 3046 // Provide a non-NULL return for disp_as_type() that will allow adr_type()
duke@435 3047 // to correctly compute the access type for alias analysis.
duke@435 3048 //
duke@435 3049 // See BugId 4796752, operand indOffset32X in i486.ad
duke@435 3050 int idx = rep_var_to_constant_index(disp, oper, globals);
duke@435 3051 fprintf(fp," virtual const TypePtr *disp_as_type() const { return _c%d; }\n", idx);
duke@435 3052 }
duke@435 3053 }
duke@435 3054 }
duke@435 3055
duke@435 3056 //
duke@435 3057 // Construct the method to copy _idx, inputs and operands to new node.
duke@435 3058 static void define_fill_new_machnode(bool used, FILE *fp_cpp) {
duke@435 3059 fprintf(fp_cpp, "\n");
duke@435 3060 fprintf(fp_cpp, "// Copy _idx, inputs and operands to new node\n");
duke@435 3061 fprintf(fp_cpp, "void MachNode::fill_new_machnode( MachNode* node, Compile* C) const {\n");
duke@435 3062 if( !used ) {
duke@435 3063 fprintf(fp_cpp, " // This architecture does not have cisc or short branch instructions\n");
duke@435 3064 fprintf(fp_cpp, " ShouldNotCallThis();\n");
duke@435 3065 fprintf(fp_cpp, "}\n");
duke@435 3066 } else {
duke@435 3067 // New node must use same node index for access through allocator's tables
duke@435 3068 fprintf(fp_cpp, " // New node must use same node index\n");
duke@435 3069 fprintf(fp_cpp, " node->set_idx( _idx );\n");
duke@435 3070 // Copy machine-independent inputs
duke@435 3071 fprintf(fp_cpp, " // Copy machine-independent inputs\n");
duke@435 3072 fprintf(fp_cpp, " for( uint j = 0; j < req(); j++ ) {\n");
duke@435 3073 fprintf(fp_cpp, " node->add_req(in(j));\n");
duke@435 3074 fprintf(fp_cpp, " }\n");
duke@435 3075 // Copy machine operands to new MachNode
duke@435 3076 fprintf(fp_cpp, " // Copy my operands, except for cisc position\n");
duke@435 3077 fprintf(fp_cpp, " int nopnds = num_opnds();\n");
duke@435 3078 fprintf(fp_cpp, " assert( node->num_opnds() == (uint)nopnds, \"Must have same number of operands\");\n");
duke@435 3079 fprintf(fp_cpp, " MachOper **to = node->_opnds;\n");
duke@435 3080 fprintf(fp_cpp, " for( int i = 0; i < nopnds; i++ ) {\n");
duke@435 3081 fprintf(fp_cpp, " if( i != cisc_operand() ) \n");
duke@435 3082 fprintf(fp_cpp, " to[i] = _opnds[i]->clone(C);\n");
duke@435 3083 fprintf(fp_cpp, " }\n");
duke@435 3084 fprintf(fp_cpp, "}\n");
duke@435 3085 }
duke@435 3086 fprintf(fp_cpp, "\n");
duke@435 3087 }
duke@435 3088
duke@435 3089 //------------------------------defineClasses----------------------------------
duke@435 3090 // Define members of MachNode and MachOper classes based on
duke@435 3091 // operand and instruction lists
duke@435 3092 void ArchDesc::defineClasses(FILE *fp) {
duke@435 3093
duke@435 3094 // Define the contents of an array containing the machine register names
duke@435 3095 defineRegNames(fp, _register);
duke@435 3096 // Define an array containing the machine register encoding values
duke@435 3097 defineRegEncodes(fp, _register);
duke@435 3098 // Generate an enumeration of user-defined register classes
duke@435 3099 // and a list of register masks, one for each class.
duke@435 3100 // Only define the RegMask value objects in the expand file.
duke@435 3101 // Declare each as an extern const RegMask ...; in ad_<arch>.hpp
duke@435 3102 declare_register_masks(_HPP_file._fp);
duke@435 3103 // build_register_masks(fp);
duke@435 3104 build_register_masks(_CPP_EXPAND_file._fp);
duke@435 3105 // Define the pipe_classes
duke@435 3106 build_pipe_classes(_CPP_PIPELINE_file._fp);
duke@435 3107
duke@435 3108 // Generate Machine Classes for each operand defined in AD file
duke@435 3109 fprintf(fp,"\n");
duke@435 3110 fprintf(fp,"\n");
duke@435 3111 fprintf(fp,"//------------------Define classes derived from MachOper---------------------\n");
duke@435 3112 // Iterate through all operands
duke@435 3113 _operands.reset();
duke@435 3114 OperandForm *oper;
duke@435 3115 for( ; (oper = (OperandForm*)_operands.iter()) != NULL; ) {
duke@435 3116 // Ensure this is a machine-world instruction
duke@435 3117 if ( oper->ideal_only() ) continue;
duke@435 3118 // !!!!!
duke@435 3119 // The declaration of labelOper is in machine-independent file: machnode
duke@435 3120 if ( strcmp(oper->_ident,"label") == 0 ) {
duke@435 3121 defineIn_RegMask(_CPP_MISC_file._fp, _globalNames, *oper);
duke@435 3122
duke@435 3123 fprintf(fp,"MachOper *%sOper::clone(Compile* C) const {\n", oper->_ident);
duke@435 3124 fprintf(fp," return new (C) %sOper(_label, _block_num);\n", oper->_ident);
duke@435 3125 fprintf(fp,"}\n");
duke@435 3126
duke@435 3127 fprintf(fp,"uint %sOper::opcode() const { return %s; }\n",
duke@435 3128 oper->_ident, machOperEnum(oper->_ident));
duke@435 3129 // // Currently all XXXOper::Hash() methods are identical (990820)
duke@435 3130 // define_hash(fp, oper->_ident);
duke@435 3131 // // Currently all XXXOper::Cmp() methods are identical (990820)
duke@435 3132 // define_cmp(fp, oper->_ident);
duke@435 3133 fprintf(fp,"\n");
duke@435 3134
duke@435 3135 continue;
duke@435 3136 }
duke@435 3137
duke@435 3138 // The declaration of methodOper is in machine-independent file: machnode
duke@435 3139 if ( strcmp(oper->_ident,"method") == 0 ) {
duke@435 3140 defineIn_RegMask(_CPP_MISC_file._fp, _globalNames, *oper);
duke@435 3141
duke@435 3142 fprintf(fp,"MachOper *%sOper::clone(Compile* C) const {\n", oper->_ident);
duke@435 3143 fprintf(fp," return new (C) %sOper(_method);\n", oper->_ident);
duke@435 3144 fprintf(fp,"}\n");
duke@435 3145
duke@435 3146 fprintf(fp,"uint %sOper::opcode() const { return %s; }\n",
duke@435 3147 oper->_ident, machOperEnum(oper->_ident));
duke@435 3148 // // Currently all XXXOper::Hash() methods are identical (990820)
duke@435 3149 // define_hash(fp, oper->_ident);
duke@435 3150 // // Currently all XXXOper::Cmp() methods are identical (990820)
duke@435 3151 // define_cmp(fp, oper->_ident);
duke@435 3152 fprintf(fp,"\n");
duke@435 3153
duke@435 3154 continue;
duke@435 3155 }
duke@435 3156
duke@435 3157 defineIn_RegMask(fp, _globalNames, *oper);
duke@435 3158 defineClone(_CPP_CLONE_file._fp, _globalNames, *oper);
duke@435 3159 // // Currently all XXXOper::Hash() methods are identical (990820)
duke@435 3160 // define_hash(fp, oper->_ident);
duke@435 3161 // // Currently all XXXOper::Cmp() methods are identical (990820)
duke@435 3162 // define_cmp(fp, oper->_ident);
duke@435 3163
duke@435 3164 // side-call to generate output that used to be in the header file:
duke@435 3165 extern void gen_oper_format(FILE *fp, FormDict &globals, OperandForm &oper, bool for_c_file);
duke@435 3166 gen_oper_format(_CPP_FORMAT_file._fp, _globalNames, *oper, true);
duke@435 3167
duke@435 3168 }
duke@435 3169
duke@435 3170
duke@435 3171 // Generate Machine Classes for each instruction defined in AD file
duke@435 3172 fprintf(fp,"//------------------Define members for classes derived from MachNode----------\n");
duke@435 3173 // Output the definitions for out_RegMask() // & kill_RegMask()
duke@435 3174 _instructions.reset();
duke@435 3175 InstructForm *instr;
duke@435 3176 MachNodeForm *machnode;
duke@435 3177 for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3178 // Ensure this is a machine-world instruction
duke@435 3179 if ( instr->ideal_only() ) continue;
duke@435 3180
duke@435 3181 defineOut_RegMask(_CPP_MISC_file._fp, instr->_ident, reg_mask(*instr));
duke@435 3182 }
duke@435 3183
duke@435 3184 bool used = false;
duke@435 3185 // Output the definitions for expand rules & peephole rules
duke@435 3186 _instructions.reset();
duke@435 3187 for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3188 // Ensure this is a machine-world instruction
duke@435 3189 if ( instr->ideal_only() ) continue;
duke@435 3190 // If there are multiple defs/kills, or an explicit expand rule, build rule
duke@435 3191 if( instr->expands() || instr->needs_projections() ||
duke@435 3192 instr->has_temps() ||
twisti@2350 3193 instr->is_mach_constant() ||
goetz@6484 3194 instr->needs_constant_base() ||
duke@435 3195 instr->_matrule != NULL &&
duke@435 3196 instr->num_opnds() != instr->num_unique_opnds() )
duke@435 3197 defineExpand(_CPP_EXPAND_file._fp, instr);
duke@435 3198 // If there is an explicit peephole rule, build it
duke@435 3199 if ( instr->peepholes() )
duke@435 3200 definePeephole(_CPP_PEEPHOLE_file._fp, instr);
duke@435 3201
duke@435 3202 // Output code to convert to the cisc version, if applicable
duke@435 3203 used |= instr->define_cisc_version(*this, fp);
duke@435 3204
duke@435 3205 // Output code to convert to the short branch version, if applicable
never@1896 3206 used |= instr->define_short_branch_methods(*this, fp);
duke@435 3207 }
duke@435 3208
duke@435 3209 // Construct the method called by cisc_version() to copy inputs and operands.
duke@435 3210 define_fill_new_machnode(used, fp);
duke@435 3211
duke@435 3212 // Output the definitions for labels
duke@435 3213 _instructions.reset();
duke@435 3214 while( (instr = (InstructForm*)_instructions.iter()) != NULL ) {
duke@435 3215 // Ensure this is a machine-world instruction
duke@435 3216 if ( instr->ideal_only() ) continue;
duke@435 3217
duke@435 3218 // Access the fields for operand Label
duke@435 3219 int label_position = instr->label_position();
duke@435 3220 if( label_position != -1 ) {
duke@435 3221 // Set the label
kvn@3037 3222 fprintf(fp,"void %sNode::label_set( Label* label, uint block_num ) {\n", instr->_ident);
duke@435 3223 fprintf(fp," labelOper* oper = (labelOper*)(opnd_array(%d));\n",
duke@435 3224 label_position );
kvn@3037 3225 fprintf(fp," oper->_label = label;\n");
duke@435 3226 fprintf(fp," oper->_block_num = block_num;\n");
duke@435 3227 fprintf(fp,"}\n");
kvn@3051 3228 // Save the label
kvn@3051 3229 fprintf(fp,"void %sNode::save_label( Label** label, uint* block_num ) {\n", instr->_ident);
kvn@3051 3230 fprintf(fp," labelOper* oper = (labelOper*)(opnd_array(%d));\n",
kvn@3051 3231 label_position );
kvn@3051 3232 fprintf(fp," *label = oper->_label;\n");
kvn@3051 3233 fprintf(fp," *block_num = oper->_block_num;\n");
kvn@3051 3234 fprintf(fp,"}\n");
duke@435 3235 }
duke@435 3236 }
duke@435 3237
duke@435 3238 // Output the definitions for methods
duke@435 3239 _instructions.reset();
duke@435 3240 while( (instr = (InstructForm*)_instructions.iter()) != NULL ) {
duke@435 3241 // Ensure this is a machine-world instruction
duke@435 3242 if ( instr->ideal_only() ) continue;
duke@435 3243
duke@435 3244 // Access the fields for operand Label
duke@435 3245 int method_position = instr->method_position();
duke@435 3246 if( method_position != -1 ) {
duke@435 3247 // Access the method's address
duke@435 3248 fprintf(fp,"void %sNode::method_set( intptr_t method ) {\n", instr->_ident);
duke@435 3249 fprintf(fp," ((methodOper*)opnd_array(%d))->_method = method;\n",
duke@435 3250 method_position );
duke@435 3251 fprintf(fp,"}\n");
duke@435 3252 fprintf(fp,"\n");
duke@435 3253 }
duke@435 3254 }
duke@435 3255
duke@435 3256 // Define this instruction's number of relocation entries, base is '0'
duke@435 3257 _instructions.reset();
duke@435 3258 while( (instr = (InstructForm*)_instructions.iter()) != NULL ) {
duke@435 3259 // Output the definition for number of relocation entries
duke@435 3260 uint reloc_size = instr->reloc(_globalNames);
duke@435 3261 if ( reloc_size != 0 ) {
kvn@4161 3262 fprintf(fp,"int %sNode::reloc() const {\n", instr->_ident);
kvn@4161 3263 fprintf(fp," return %d;\n", reloc_size);
duke@435 3264 fprintf(fp,"}\n");
duke@435 3265 fprintf(fp,"\n");
duke@435 3266 }
duke@435 3267 }
duke@435 3268 fprintf(fp,"\n");
duke@435 3269
duke@435 3270 // Output the definitions for code generation
duke@435 3271 //
duke@435 3272 // address ___Node::emit(address ptr, PhaseRegAlloc *ra_) const {
duke@435 3273 // // ... encoding defined by user
duke@435 3274 // return ptr;
duke@435 3275 // }
duke@435 3276 //
duke@435 3277 _instructions.reset();
duke@435 3278 for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3279 // Ensure this is a machine-world instruction
duke@435 3280 if ( instr->ideal_only() ) continue;
duke@435 3281
goetz@6478 3282 if (instr->_insencode) {
goetz@6478 3283 if (instr->postalloc_expands()) {
goetz@6478 3284 // Don't write this to _CPP_EXPAND_file, as the code generated calls C-code
goetz@6478 3285 // from code sections in ad file that is dumped to fp.
goetz@6478 3286 define_postalloc_expand(fp, *instr);
goetz@6478 3287 } else {
goetz@6478 3288 defineEmit(fp, *instr);
goetz@6478 3289 }
goetz@6478 3290 }
twisti@2350 3291 if (instr->is_mach_constant()) defineEvalConstant(fp, *instr);
twisti@2350 3292 if (instr->_size) defineSize (fp, *instr);
duke@435 3293
duke@435 3294 // side-call to generate output that used to be in the header file:
duke@435 3295 extern void gen_inst_format(FILE *fp, FormDict &globals, InstructForm &oper, bool for_c_file);
duke@435 3296 gen_inst_format(_CPP_FORMAT_file._fp, _globalNames, *instr, true);
duke@435 3297 }
duke@435 3298
duke@435 3299 // Output the definitions for alias analysis
duke@435 3300 _instructions.reset();
duke@435 3301 for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3302 // Ensure this is a machine-world instruction
duke@435 3303 if ( instr->ideal_only() ) continue;
duke@435 3304
duke@435 3305 // Analyze machine instructions that either USE or DEF memory.
duke@435 3306 int memory_operand = instr->memory_operand(_globalNames);
duke@435 3307 // Some guys kill all of memory
duke@435 3308 if ( instr->is_wide_memory_kill(_globalNames) ) {
duke@435 3309 memory_operand = InstructForm::MANY_MEMORY_OPERANDS;
duke@435 3310 }
duke@435 3311
duke@435 3312 if ( memory_operand != InstructForm::NO_MEMORY_OPERAND ) {
duke@435 3313 if( memory_operand == InstructForm::MANY_MEMORY_OPERANDS ) {
duke@435 3314 fprintf(fp,"const TypePtr *%sNode::adr_type() const { return TypePtr::BOTTOM; }\n", instr->_ident);
duke@435 3315 fprintf(fp,"const MachOper* %sNode::memory_operand() const { return (MachOper*)-1; }\n", instr->_ident);
duke@435 3316 } else {
duke@435 3317 fprintf(fp,"const MachOper* %sNode::memory_operand() const { return _opnds[%d]; }\n", instr->_ident, memory_operand);
duke@435 3318 }
duke@435 3319 }
duke@435 3320 }
duke@435 3321
duke@435 3322 // Get the length of the longest identifier
duke@435 3323 int max_ident_len = 0;
duke@435 3324 _instructions.reset();
duke@435 3325
duke@435 3326 for ( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3327 if (instr->_ins_pipe && _pipeline->_classlist.search(instr->_ins_pipe)) {
duke@435 3328 int ident_len = (int)strlen(instr->_ident);
duke@435 3329 if( max_ident_len < ident_len )
duke@435 3330 max_ident_len = ident_len;
duke@435 3331 }
duke@435 3332 }
duke@435 3333
duke@435 3334 // Emit specifically for Node(s)
duke@435 3335 fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline_class() { return %s; }\n",
duke@435 3336 max_ident_len, "Node", _pipeline ? "(&pipeline_class_Zero_Instructions)" : "NULL");
duke@435 3337 fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline() const { return %s; }\n",
duke@435 3338 max_ident_len, "Node", _pipeline ? "(&pipeline_class_Zero_Instructions)" : "NULL");
duke@435 3339 fprintf(_CPP_PIPELINE_file._fp, "\n");
duke@435 3340
duke@435 3341 fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline_class() { return %s; }\n",
duke@435 3342 max_ident_len, "MachNode", _pipeline ? "(&pipeline_class_Unknown_Instructions)" : "NULL");
duke@435 3343 fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*s::pipeline() const { return pipeline_class(); }\n",
duke@435 3344 max_ident_len, "MachNode");
duke@435 3345 fprintf(_CPP_PIPELINE_file._fp, "\n");
duke@435 3346
duke@435 3347 // Output the definitions for machine node specific pipeline data
duke@435 3348 _machnodes.reset();
duke@435 3349
duke@435 3350 for ( ; (machnode = (MachNodeForm*)_machnodes.iter()) != NULL; ) {
duke@435 3351 fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %sNode::pipeline() const { return (&pipeline_class_%03d); }\n",
duke@435 3352 machnode->_ident, ((class PipeClassForm *)_pipeline->_classdict[machnode->_machnode_pipe])->_num);
duke@435 3353 }
duke@435 3354
duke@435 3355 fprintf(_CPP_PIPELINE_file._fp, "\n");
duke@435 3356
duke@435 3357 // Output the definitions for instruction pipeline static data references
duke@435 3358 _instructions.reset();
duke@435 3359
duke@435 3360 for ( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3361 if (instr->_ins_pipe && _pipeline->_classlist.search(instr->_ins_pipe)) {
duke@435 3362 fprintf(_CPP_PIPELINE_file._fp, "\n");
duke@435 3363 fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*sNode::pipeline_class() { return (&pipeline_class_%03d); }\n",
duke@435 3364 max_ident_len, instr->_ident, ((class PipeClassForm *)_pipeline->_classdict[instr->_ins_pipe])->_num);
duke@435 3365 fprintf(_CPP_PIPELINE_file._fp, "const Pipeline * %*sNode::pipeline() const { return (&pipeline_class_%03d); }\n",
duke@435 3366 max_ident_len, instr->_ident, ((class PipeClassForm *)_pipeline->_classdict[instr->_ins_pipe])->_num);
duke@435 3367 }
duke@435 3368 }
duke@435 3369 }
duke@435 3370
duke@435 3371
duke@435 3372 // -------------------------------- maps ------------------------------------
duke@435 3373
duke@435 3374 // Information needed to generate the ReduceOp mapping for the DFA
duke@435 3375 class OutputReduceOp : public OutputMap {
duke@435 3376 public:
duke@435 3377 OutputReduceOp(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
kvn@4161 3378 : OutputMap(hpp, cpp, globals, AD, "reduceOp") {};
duke@435 3379
duke@435 3380 void declaration() { fprintf(_hpp, "extern const int reduceOp[];\n"); }
duke@435 3381 void definition() { fprintf(_cpp, "const int reduceOp[] = {\n"); }
duke@435 3382 void closing() { fprintf(_cpp, " 0 // no trailing comma\n");
duke@435 3383 OutputMap::closing();
duke@435 3384 }
duke@435 3385 void map(OpClassForm &opc) {
duke@435 3386 const char *reduce = opc._ident;
duke@435 3387 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3388 else fprintf(_cpp, " 0");
duke@435 3389 }
duke@435 3390 void map(OperandForm &oper) {
duke@435 3391 // Most operands without match rules, e.g. eFlagsReg, do not have a result operand
duke@435 3392 const char *reduce = (oper._matrule ? oper.reduce_result() : NULL);
duke@435 3393 // operand stackSlot does not have a match rule, but produces a stackSlot
duke@435 3394 if( oper.is_user_name_for_sReg() != Form::none ) reduce = oper.reduce_result();
duke@435 3395 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3396 else fprintf(_cpp, " 0");
duke@435 3397 }
duke@435 3398 void map(InstructForm &inst) {
duke@435 3399 const char *reduce = (inst._matrule ? inst.reduce_result() : NULL);
duke@435 3400 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3401 else fprintf(_cpp, " 0");
duke@435 3402 }
duke@435 3403 void map(char *reduce) {
duke@435 3404 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3405 else fprintf(_cpp, " 0");
duke@435 3406 }
duke@435 3407 };
duke@435 3408
duke@435 3409 // Information needed to generate the LeftOp mapping for the DFA
duke@435 3410 class OutputLeftOp : public OutputMap {
duke@435 3411 public:
duke@435 3412 OutputLeftOp(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
kvn@4161 3413 : OutputMap(hpp, cpp, globals, AD, "leftOp") {};
duke@435 3414
duke@435 3415 void declaration() { fprintf(_hpp, "extern const int leftOp[];\n"); }
duke@435 3416 void definition() { fprintf(_cpp, "const int leftOp[] = {\n"); }
duke@435 3417 void closing() { fprintf(_cpp, " 0 // no trailing comma\n");
duke@435 3418 OutputMap::closing();
duke@435 3419 }
duke@435 3420 void map(OpClassForm &opc) { fprintf(_cpp, " 0"); }
duke@435 3421 void map(OperandForm &oper) {
duke@435 3422 const char *reduce = oper.reduce_left(_globals);
duke@435 3423 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3424 else fprintf(_cpp, " 0");
duke@435 3425 }
duke@435 3426 void map(char *name) {
duke@435 3427 const char *reduce = _AD.reduceLeft(name);
duke@435 3428 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3429 else fprintf(_cpp, " 0");
duke@435 3430 }
duke@435 3431 void map(InstructForm &inst) {
duke@435 3432 const char *reduce = inst.reduce_left(_globals);
duke@435 3433 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3434 else fprintf(_cpp, " 0");
duke@435 3435 }
duke@435 3436 };
duke@435 3437
duke@435 3438
duke@435 3439 // Information needed to generate the RightOp mapping for the DFA
duke@435 3440 class OutputRightOp : public OutputMap {
duke@435 3441 public:
duke@435 3442 OutputRightOp(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
kvn@4161 3443 : OutputMap(hpp, cpp, globals, AD, "rightOp") {};
duke@435 3444
duke@435 3445 void declaration() { fprintf(_hpp, "extern const int rightOp[];\n"); }
duke@435 3446 void definition() { fprintf(_cpp, "const int rightOp[] = {\n"); }
duke@435 3447 void closing() { fprintf(_cpp, " 0 // no trailing comma\n");
duke@435 3448 OutputMap::closing();
duke@435 3449 }
duke@435 3450 void map(OpClassForm &opc) { fprintf(_cpp, " 0"); }
duke@435 3451 void map(OperandForm &oper) {
duke@435 3452 const char *reduce = oper.reduce_right(_globals);
duke@435 3453 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3454 else fprintf(_cpp, " 0");
duke@435 3455 }
duke@435 3456 void map(char *name) {
duke@435 3457 const char *reduce = _AD.reduceRight(name);
duke@435 3458 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3459 else fprintf(_cpp, " 0");
duke@435 3460 }
duke@435 3461 void map(InstructForm &inst) {
duke@435 3462 const char *reduce = inst.reduce_right(_globals);
duke@435 3463 if( reduce ) fprintf(_cpp, " %s_rule", reduce);
duke@435 3464 else fprintf(_cpp, " 0");
duke@435 3465 }
duke@435 3466 };
duke@435 3467
duke@435 3468
duke@435 3469 // Information needed to generate the Rule names for the DFA
duke@435 3470 class OutputRuleName : public OutputMap {
duke@435 3471 public:
duke@435 3472 OutputRuleName(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
kvn@4161 3473 : OutputMap(hpp, cpp, globals, AD, "ruleName") {};
duke@435 3474
duke@435 3475 void declaration() { fprintf(_hpp, "extern const char *ruleName[];\n"); }
duke@435 3476 void definition() { fprintf(_cpp, "const char *ruleName[] = {\n"); }
kvn@4161 3477 void closing() { fprintf(_cpp, " \"invalid rule name\" // no trailing comma\n");
duke@435 3478 OutputMap::closing();
duke@435 3479 }
duke@435 3480 void map(OpClassForm &opc) { fprintf(_cpp, " \"%s\"", _AD.machOperEnum(opc._ident) ); }
duke@435 3481 void map(OperandForm &oper) { fprintf(_cpp, " \"%s\"", _AD.machOperEnum(oper._ident) ); }
duke@435 3482 void map(char *name) { fprintf(_cpp, " \"%s\"", name ? name : "0"); }
duke@435 3483 void map(InstructForm &inst){ fprintf(_cpp, " \"%s\"", inst._ident ? inst._ident : "0"); }
duke@435 3484 };
duke@435 3485
duke@435 3486
duke@435 3487 // Information needed to generate the swallowed mapping for the DFA
duke@435 3488 class OutputSwallowed : public OutputMap {
duke@435 3489 public:
duke@435 3490 OutputSwallowed(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
kvn@4161 3491 : OutputMap(hpp, cpp, globals, AD, "swallowed") {};
duke@435 3492
duke@435 3493 void declaration() { fprintf(_hpp, "extern const bool swallowed[];\n"); }
duke@435 3494 void definition() { fprintf(_cpp, "const bool swallowed[] = {\n"); }
duke@435 3495 void closing() { fprintf(_cpp, " false // no trailing comma\n");
duke@435 3496 OutputMap::closing();
duke@435 3497 }
duke@435 3498 void map(OperandForm &oper) { // Generate the entry for this opcode
duke@435 3499 const char *swallowed = oper.swallowed(_globals) ? "true" : "false";
duke@435 3500 fprintf(_cpp, " %s", swallowed);
duke@435 3501 }
duke@435 3502 void map(OpClassForm &opc) { fprintf(_cpp, " false"); }
duke@435 3503 void map(char *name) { fprintf(_cpp, " false"); }
duke@435 3504 void map(InstructForm &inst){ fprintf(_cpp, " false"); }
duke@435 3505 };
duke@435 3506
duke@435 3507
duke@435 3508 // Information needed to generate the decision array for instruction chain rule
duke@435 3509 class OutputInstChainRule : public OutputMap {
duke@435 3510 public:
duke@435 3511 OutputInstChainRule(FILE *hpp, FILE *cpp, FormDict &globals, ArchDesc &AD)
kvn@4161 3512 : OutputMap(hpp, cpp, globals, AD, "instruction_chain_rule") {};
duke@435 3513
duke@435 3514 void declaration() { fprintf(_hpp, "extern const bool instruction_chain_rule[];\n"); }
duke@435 3515 void definition() { fprintf(_cpp, "const bool instruction_chain_rule[] = {\n"); }
duke@435 3516 void closing() { fprintf(_cpp, " false // no trailing comma\n");
duke@435 3517 OutputMap::closing();
duke@435 3518 }
duke@435 3519 void map(OpClassForm &opc) { fprintf(_cpp, " false"); }
duke@435 3520 void map(OperandForm &oper) { fprintf(_cpp, " false"); }
duke@435 3521 void map(char *name) { fprintf(_cpp, " false"); }
duke@435 3522 void map(InstructForm &inst) { // Check for simple chain rule
duke@435 3523 const char *chain = inst.is_simple_chain_rule(_globals) ? "true" : "false";
duke@435 3524 fprintf(_cpp, " %s", chain);
duke@435 3525 }
duke@435 3526 };
duke@435 3527
duke@435 3528
duke@435 3529 //---------------------------build_map------------------------------------
duke@435 3530 // Build mapping from enumeration for densely packed operands
duke@435 3531 // TO result and child types.
duke@435 3532 void ArchDesc::build_map(OutputMap &map) {
duke@435 3533 FILE *fp_hpp = map.decl_file();
duke@435 3534 FILE *fp_cpp = map.def_file();
duke@435 3535 int idx = 0;
duke@435 3536 OperandForm *op;
duke@435 3537 OpClassForm *opc;
duke@435 3538 InstructForm *inst;
duke@435 3539
duke@435 3540 // Construct this mapping
duke@435 3541 map.declaration();
duke@435 3542 fprintf(fp_cpp,"\n");
duke@435 3543 map.definition();
duke@435 3544
duke@435 3545 // Output the mapping for operands
duke@435 3546 map.record_position(OutputMap::BEGIN_OPERANDS, idx );
duke@435 3547 _operands.reset();
duke@435 3548 for(; (op = (OperandForm*)_operands.iter()) != NULL; ) {
duke@435 3549 // Ensure this is a machine-world instruction
duke@435 3550 if ( op->ideal_only() ) continue;
duke@435 3551
duke@435 3552 // Generate the entry for this opcode
kvn@4161 3553 fprintf(fp_cpp, " /* %4d */", idx); map.map(*op); fprintf(fp_cpp, ",\n");
duke@435 3554 ++idx;
duke@435 3555 };
duke@435 3556 fprintf(fp_cpp, " // last operand\n");
duke@435 3557
duke@435 3558 // Place all user-defined operand classes into the mapping
duke@435 3559 map.record_position(OutputMap::BEGIN_OPCLASSES, idx );
duke@435 3560 _opclass.reset();
duke@435 3561 for(; (opc = (OpClassForm*)_opclass.iter()) != NULL; ) {
kvn@4161 3562 fprintf(fp_cpp, " /* %4d */", idx); map.map(*opc); fprintf(fp_cpp, ",\n");
duke@435 3563 ++idx;
duke@435 3564 };
duke@435 3565 fprintf(fp_cpp, " // last operand class\n");
duke@435 3566
duke@435 3567 // Place all internally defined operands into the mapping
duke@435 3568 map.record_position(OutputMap::BEGIN_INTERNALS, idx );
duke@435 3569 _internalOpNames.reset();
duke@435 3570 char *name = NULL;
duke@435 3571 for(; (name = (char *)_internalOpNames.iter()) != NULL; ) {
kvn@4161 3572 fprintf(fp_cpp, " /* %4d */", idx); map.map(name); fprintf(fp_cpp, ",\n");
duke@435 3573 ++idx;
duke@435 3574 };
duke@435 3575 fprintf(fp_cpp, " // last internally defined operand\n");
duke@435 3576
duke@435 3577 // Place all user-defined instructions into the mapping
duke@435 3578 if( map.do_instructions() ) {
duke@435 3579 map.record_position(OutputMap::BEGIN_INSTRUCTIONS, idx );
duke@435 3580 // Output all simple instruction chain rules first
duke@435 3581 map.record_position(OutputMap::BEGIN_INST_CHAIN_RULES, idx );
duke@435 3582 {
duke@435 3583 _instructions.reset();
duke@435 3584 for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3585 // Ensure this is a machine-world instruction
duke@435 3586 if ( inst->ideal_only() ) continue;
duke@435 3587 if ( ! inst->is_simple_chain_rule(_globalNames) ) continue;
duke@435 3588 if ( inst->rematerialize(_globalNames, get_registers()) ) continue;
duke@435 3589
kvn@4161 3590 fprintf(fp_cpp, " /* %4d */", idx); map.map(*inst); fprintf(fp_cpp, ",\n");
duke@435 3591 ++idx;
duke@435 3592 };
duke@435 3593 map.record_position(OutputMap::BEGIN_REMATERIALIZE, idx );
duke@435 3594 _instructions.reset();
duke@435 3595 for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3596 // Ensure this is a machine-world instruction
duke@435 3597 if ( inst->ideal_only() ) continue;
duke@435 3598 if ( ! inst->is_simple_chain_rule(_globalNames) ) continue;
duke@435 3599 if ( ! inst->rematerialize(_globalNames, get_registers()) ) continue;
duke@435 3600
kvn@4161 3601 fprintf(fp_cpp, " /* %4d */", idx); map.map(*inst); fprintf(fp_cpp, ",\n");
duke@435 3602 ++idx;
duke@435 3603 };
duke@435 3604 map.record_position(OutputMap::END_INST_CHAIN_RULES, idx );
duke@435 3605 }
duke@435 3606 // Output all instructions that are NOT simple chain rules
duke@435 3607 {
duke@435 3608 _instructions.reset();
duke@435 3609 for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3610 // Ensure this is a machine-world instruction
duke@435 3611 if ( inst->ideal_only() ) continue;
duke@435 3612 if ( inst->is_simple_chain_rule(_globalNames) ) continue;
duke@435 3613 if ( ! inst->rematerialize(_globalNames, get_registers()) ) continue;
duke@435 3614
kvn@4161 3615 fprintf(fp_cpp, " /* %4d */", idx); map.map(*inst); fprintf(fp_cpp, ",\n");
duke@435 3616 ++idx;
duke@435 3617 };
duke@435 3618 map.record_position(OutputMap::END_REMATERIALIZE, idx );
duke@435 3619 _instructions.reset();
duke@435 3620 for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 3621 // Ensure this is a machine-world instruction
duke@435 3622 if ( inst->ideal_only() ) continue;
duke@435 3623 if ( inst->is_simple_chain_rule(_globalNames) ) continue;
duke@435 3624 if ( inst->rematerialize(_globalNames, get_registers()) ) continue;
duke@435 3625
kvn@4161 3626 fprintf(fp_cpp, " /* %4d */", idx); map.map(*inst); fprintf(fp_cpp, ",\n");
duke@435 3627 ++idx;
duke@435 3628 };
duke@435 3629 }
duke@435 3630 fprintf(fp_cpp, " // last instruction\n");
duke@435 3631 map.record_position(OutputMap::END_INSTRUCTIONS, idx );
duke@435 3632 }
duke@435 3633 // Finish defining table
duke@435 3634 map.closing();
duke@435 3635 };
duke@435 3636
duke@435 3637
duke@435 3638 // Helper function for buildReduceMaps
duke@435 3639 char reg_save_policy(const char *calling_convention) {
duke@435 3640 char callconv;
duke@435 3641
duke@435 3642 if (!strcmp(calling_convention, "NS")) callconv = 'N';
duke@435 3643 else if (!strcmp(calling_convention, "SOE")) callconv = 'E';
duke@435 3644 else if (!strcmp(calling_convention, "SOC")) callconv = 'C';
duke@435 3645 else if (!strcmp(calling_convention, "AS")) callconv = 'A';
duke@435 3646 else callconv = 'Z';
duke@435 3647
duke@435 3648 return callconv;
duke@435 3649 }
duke@435 3650
goetz@6499 3651 void ArchDesc::generate_needs_clone_jvms(FILE *fp_cpp) {
goetz@6499 3652 fprintf(fp_cpp, "bool Compile::needs_clone_jvms() { return %s; }\n\n",
goetz@6499 3653 _needs_clone_jvms ? "true" : "false");
goetz@6499 3654 }
goetz@6499 3655
duke@435 3656 //---------------------------generate_assertion_checks-------------------
duke@435 3657 void ArchDesc::generate_adlc_verification(FILE *fp_cpp) {
duke@435 3658 fprintf(fp_cpp, "\n");
duke@435 3659
duke@435 3660 fprintf(fp_cpp, "#ifndef PRODUCT\n");
duke@435 3661 fprintf(fp_cpp, "void Compile::adlc_verification() {\n");
duke@435 3662 globalDefs().print_asserts(fp_cpp);
duke@435 3663 fprintf(fp_cpp, "}\n");
duke@435 3664 fprintf(fp_cpp, "#endif\n");
duke@435 3665 fprintf(fp_cpp, "\n");
duke@435 3666 }
duke@435 3667
duke@435 3668 //---------------------------addSourceBlocks-----------------------------
duke@435 3669 void ArchDesc::addSourceBlocks(FILE *fp_cpp) {
duke@435 3670 if (_source.count() > 0)
duke@435 3671 _source.output(fp_cpp);
duke@435 3672
duke@435 3673 generate_adlc_verification(fp_cpp);
duke@435 3674 }
duke@435 3675 //---------------------------addHeaderBlocks-----------------------------
duke@435 3676 void ArchDesc::addHeaderBlocks(FILE *fp_hpp) {
duke@435 3677 if (_header.count() > 0)
duke@435 3678 _header.output(fp_hpp);
duke@435 3679 }
duke@435 3680 //-------------------------addPreHeaderBlocks----------------------------
duke@435 3681 void ArchDesc::addPreHeaderBlocks(FILE *fp_hpp) {
duke@435 3682 // Output #defines from definition block
duke@435 3683 globalDefs().print_defines(fp_hpp);
duke@435 3684
duke@435 3685 if (_pre_header.count() > 0)
duke@435 3686 _pre_header.output(fp_hpp);
duke@435 3687 }
duke@435 3688
duke@435 3689 //---------------------------buildReduceMaps-----------------------------
duke@435 3690 // Build mapping from enumeration for densely packed operands
duke@435 3691 // TO result and child types.
duke@435 3692 void ArchDesc::buildReduceMaps(FILE *fp_hpp, FILE *fp_cpp) {
duke@435 3693 RegDef *rdef;
duke@435 3694 RegDef *next;
duke@435 3695
duke@435 3696 // The emit bodies currently require functions defined in the source block.
duke@435 3697
duke@435 3698 // Build external declarations for mappings
duke@435 3699 fprintf(fp_hpp, "\n");
duke@435 3700 fprintf(fp_hpp, "extern const char register_save_policy[];\n");
duke@435 3701 fprintf(fp_hpp, "extern const char c_reg_save_policy[];\n");
duke@435 3702 fprintf(fp_hpp, "extern const int register_save_type[];\n");
duke@435 3703 fprintf(fp_hpp, "\n");
duke@435 3704
duke@435 3705 // Construct Save-Policy array
duke@435 3706 fprintf(fp_cpp, "// Map from machine-independent register number to register_save_policy\n");
duke@435 3707 fprintf(fp_cpp, "const char register_save_policy[] = {\n");
duke@435 3708 _register->reset_RegDefs();
duke@435 3709 for( rdef = _register->iter_RegDefs(); rdef != NULL; rdef = next ) {
duke@435 3710 next = _register->iter_RegDefs();
duke@435 3711 char policy = reg_save_policy(rdef->_callconv);
duke@435 3712 const char *comma = (next != NULL) ? "," : " // no trailing comma";
kvn@4161 3713 fprintf(fp_cpp, " '%c'%s // %s\n", policy, comma, rdef->_regname);
duke@435 3714 }
duke@435 3715 fprintf(fp_cpp, "};\n\n");
duke@435 3716
duke@435 3717 // Construct Native Save-Policy array
duke@435 3718 fprintf(fp_cpp, "// Map from machine-independent register number to c_reg_save_policy\n");
duke@435 3719 fprintf(fp_cpp, "const char c_reg_save_policy[] = {\n");
duke@435 3720 _register->reset_RegDefs();
duke@435 3721 for( rdef = _register->iter_RegDefs(); rdef != NULL; rdef = next ) {
duke@435 3722 next = _register->iter_RegDefs();
duke@435 3723 char policy = reg_save_policy(rdef->_c_conv);
duke@435 3724 const char *comma = (next != NULL) ? "," : " // no trailing comma";
kvn@4161 3725 fprintf(fp_cpp, " '%c'%s // %s\n", policy, comma, rdef->_regname);
duke@435 3726 }
duke@435 3727 fprintf(fp_cpp, "};\n\n");
duke@435 3728
duke@435 3729 // Construct Register Save Type array
duke@435 3730 fprintf(fp_cpp, "// Map from machine-independent register number to register_save_type\n");
duke@435 3731 fprintf(fp_cpp, "const int register_save_type[] = {\n");
duke@435 3732 _register->reset_RegDefs();
duke@435 3733 for( rdef = _register->iter_RegDefs(); rdef != NULL; rdef = next ) {
duke@435 3734 next = _register->iter_RegDefs();
duke@435 3735 const char *comma = (next != NULL) ? "," : " // no trailing comma";
duke@435 3736 fprintf(fp_cpp, " %s%s\n", rdef->_idealtype, comma);
duke@435 3737 }
duke@435 3738 fprintf(fp_cpp, "};\n\n");
duke@435 3739
duke@435 3740 // Construct the table for reduceOp
duke@435 3741 OutputReduceOp output_reduce_op(fp_hpp, fp_cpp, _globalNames, *this);
duke@435 3742 build_map(output_reduce_op);
duke@435 3743 // Construct the table for leftOp
duke@435 3744 OutputLeftOp output_left_op(fp_hpp, fp_cpp, _globalNames, *this);
duke@435 3745 build_map(output_left_op);
duke@435 3746 // Construct the table for rightOp
duke@435 3747 OutputRightOp output_right_op(fp_hpp, fp_cpp, _globalNames, *this);
duke@435 3748 build_map(output_right_op);
duke@435 3749 // Construct the table of rule names
duke@435 3750 OutputRuleName output_rule_name(fp_hpp, fp_cpp, _globalNames, *this);
duke@435 3751 build_map(output_rule_name);
duke@435 3752 // Construct the boolean table for subsumed operands
duke@435 3753 OutputSwallowed output_swallowed(fp_hpp, fp_cpp, _globalNames, *this);
duke@435 3754 build_map(output_swallowed);
duke@435 3755 // // // Preserve in case we decide to use this table instead of another
duke@435 3756 //// Construct the boolean table for instruction chain rules
duke@435 3757 //OutputInstChainRule output_inst_chain(fp_hpp, fp_cpp, _globalNames, *this);
duke@435 3758 //build_map(output_inst_chain);
duke@435 3759
duke@435 3760 }
duke@435 3761
duke@435 3762
duke@435 3763 //---------------------------buildMachOperGenerator---------------------------
duke@435 3764
duke@435 3765 // Recurse through match tree, building path through corresponding state tree,
duke@435 3766 // Until we reach the constant we are looking for.
duke@435 3767 static void path_to_constant(FILE *fp, FormDict &globals,
duke@435 3768 MatchNode *mnode, uint idx) {
duke@435 3769 if ( ! mnode) return;
duke@435 3770
duke@435 3771 unsigned position = 0;
duke@435 3772 const char *result = NULL;
duke@435 3773 const char *name = NULL;
duke@435 3774 const char *optype = NULL;
duke@435 3775
duke@435 3776 // Base Case: access constant in ideal node linked to current state node
duke@435 3777 // Each type of constant has its own access function
duke@435 3778 if ( (mnode->_lChild == NULL) && (mnode->_rChild == NULL)
duke@435 3779 && mnode->base_operand(position, globals, result, name, optype) ) {
duke@435 3780 if ( strcmp(optype,"ConI") == 0 ) {
duke@435 3781 fprintf(fp, "_leaf->get_int()");
duke@435 3782 } else if ( (strcmp(optype,"ConP") == 0) ) {
duke@435 3783 fprintf(fp, "_leaf->bottom_type()->is_ptr()");
coleenp@548 3784 } else if ( (strcmp(optype,"ConN") == 0) ) {
coleenp@548 3785 fprintf(fp, "_leaf->bottom_type()->is_narrowoop()");
roland@4159 3786 } else if ( (strcmp(optype,"ConNKlass") == 0) ) {
roland@4159 3787 fprintf(fp, "_leaf->bottom_type()->is_narrowklass()");
duke@435 3788 } else if ( (strcmp(optype,"ConF") == 0) ) {
duke@435 3789 fprintf(fp, "_leaf->getf()");
duke@435 3790 } else if ( (strcmp(optype,"ConD") == 0) ) {
duke@435 3791 fprintf(fp, "_leaf->getd()");
duke@435 3792 } else if ( (strcmp(optype,"ConL") == 0) ) {
duke@435 3793 fprintf(fp, "_leaf->get_long()");
duke@435 3794 } else if ( (strcmp(optype,"Con")==0) ) {
duke@435 3795 // !!!!! - Update if adding a machine-independent constant type
duke@435 3796 fprintf(fp, "_leaf->get_int()");
duke@435 3797 assert( false, "Unsupported constant type, pointer or indefinite");
duke@435 3798 } else if ( (strcmp(optype,"Bool") == 0) ) {
duke@435 3799 fprintf(fp, "_leaf->as_Bool()->_test._test");
duke@435 3800 } else {
duke@435 3801 assert( false, "Unsupported constant type");
duke@435 3802 }
duke@435 3803 return;
duke@435 3804 }
duke@435 3805
duke@435 3806 // If constant is in left child, build path and recurse
duke@435 3807 uint lConsts = (mnode->_lChild) ? (mnode->_lChild->num_consts(globals) ) : 0;
duke@435 3808 uint rConsts = (mnode->_rChild) ? (mnode->_rChild->num_consts(globals) ) : 0;
duke@435 3809 if ( (mnode->_lChild) && (lConsts > idx) ) {
duke@435 3810 fprintf(fp, "_kids[0]->");
duke@435 3811 path_to_constant(fp, globals, mnode->_lChild, idx);
duke@435 3812 return;
duke@435 3813 }
duke@435 3814 // If constant is in right child, build path and recurse
duke@435 3815 if ( (mnode->_rChild) && (rConsts > (idx - lConsts) ) ) {
duke@435 3816 idx = idx - lConsts;
duke@435 3817 fprintf(fp, "_kids[1]->");
duke@435 3818 path_to_constant(fp, globals, mnode->_rChild, idx);
duke@435 3819 return;
duke@435 3820 }
duke@435 3821 assert( false, "ShouldNotReachHere()");
duke@435 3822 }
duke@435 3823
duke@435 3824 // Generate code that is executed when generating a specific Machine Operand
duke@435 3825 static void genMachOperCase(FILE *fp, FormDict &globalNames, ArchDesc &AD,
duke@435 3826 OperandForm &op) {
duke@435 3827 const char *opName = op._ident;
duke@435 3828 const char *opEnumName = AD.machOperEnum(opName);
duke@435 3829 uint num_consts = op.num_consts(globalNames);
duke@435 3830
duke@435 3831 // Generate the case statement for this opcode
duke@435 3832 fprintf(fp, " case %s:", opEnumName);
duke@435 3833 fprintf(fp, "\n return new (C) %sOper(", opName);
duke@435 3834 // Access parameters for constructor from the stat object
duke@435 3835 //
duke@435 3836 // Build access to condition code value
duke@435 3837 if ( (num_consts > 0) ) {
duke@435 3838 uint i = 0;
duke@435 3839 path_to_constant(fp, globalNames, op._matrule, i);
duke@435 3840 for ( i = 1; i < num_consts; ++i ) {
duke@435 3841 fprintf(fp, ", ");
duke@435 3842 path_to_constant(fp, globalNames, op._matrule, i);
duke@435 3843 }
duke@435 3844 }
duke@435 3845 fprintf(fp, " );\n");
duke@435 3846 }
duke@435 3847
duke@435 3848
duke@435 3849 // Build switch to invoke "new" MachNode or MachOper
duke@435 3850 void ArchDesc::buildMachOperGenerator(FILE *fp_cpp) {
duke@435 3851 int idx = 0;
duke@435 3852
duke@435 3853 // Build switch to invoke 'new' for a specific MachOper
duke@435 3854 fprintf(fp_cpp, "\n");
duke@435 3855 fprintf(fp_cpp, "\n");
duke@435 3856 fprintf(fp_cpp,
duke@435 3857 "//------------------------- MachOper Generator ---------------\n");
duke@435 3858 fprintf(fp_cpp,
duke@435 3859 "// A switch statement on the dense-packed user-defined type system\n"
duke@435 3860 "// that invokes 'new' on the corresponding class constructor.\n");
duke@435 3861 fprintf(fp_cpp, "\n");
duke@435 3862 fprintf(fp_cpp, "MachOper *State::MachOperGenerator");
duke@435 3863 fprintf(fp_cpp, "(int opcode, Compile* C)");
duke@435 3864 fprintf(fp_cpp, "{\n");
duke@435 3865 fprintf(fp_cpp, "\n");
duke@435 3866 fprintf(fp_cpp, " switch(opcode) {\n");
duke@435 3867
duke@435 3868 // Place all user-defined operands into the mapping
duke@435 3869 _operands.reset();
duke@435 3870 int opIndex = 0;
duke@435 3871 OperandForm *op;
duke@435 3872 for( ; (op = (OperandForm*)_operands.iter()) != NULL; ) {
duke@435 3873 // Ensure this is a machine-world instruction
duke@435 3874 if ( op->ideal_only() ) continue;
duke@435 3875
duke@435 3876 genMachOperCase(fp_cpp, _globalNames, *this, *op);
duke@435 3877 };
duke@435 3878
duke@435 3879 // Do not iterate over operand classes for the operand generator!!!
duke@435 3880
duke@435 3881 // Place all internal operands into the mapping
duke@435 3882 _internalOpNames.reset();
duke@435 3883 const char *iopn;
duke@435 3884 for( ; (iopn = _internalOpNames.iter()) != NULL; ) {
duke@435 3885 const char *opEnumName = machOperEnum(iopn);
duke@435 3886 // Generate the case statement for this opcode
duke@435 3887 fprintf(fp_cpp, " case %s:", opEnumName);
duke@435 3888 fprintf(fp_cpp, " return NULL;\n");
duke@435 3889 };
duke@435 3890
duke@435 3891 // Generate the default case for switch(opcode)
duke@435 3892 fprintf(fp_cpp, " \n");
duke@435 3893 fprintf(fp_cpp, " default:\n");
duke@435 3894 fprintf(fp_cpp, " fprintf(stderr, \"Default MachOper Generator invoked for: \\n\");\n");
duke@435 3895 fprintf(fp_cpp, " fprintf(stderr, \" opcode = %cd\\n\", opcode);\n", '%');
duke@435 3896 fprintf(fp_cpp, " break;\n");
duke@435 3897 fprintf(fp_cpp, " }\n");
duke@435 3898
duke@435 3899 // Generate the closing for method Matcher::MachOperGenerator
duke@435 3900 fprintf(fp_cpp, " return NULL;\n");
duke@435 3901 fprintf(fp_cpp, "};\n");
duke@435 3902 }
duke@435 3903
duke@435 3904
duke@435 3905 //---------------------------buildMachNode-------------------------------------
duke@435 3906 // Build a new MachNode, for MachNodeGenerator or cisc-spilling
duke@435 3907 void ArchDesc::buildMachNode(FILE *fp_cpp, InstructForm *inst, const char *indent) {
duke@435 3908 const char *opType = NULL;
duke@435 3909 const char *opClass = inst->_ident;
duke@435 3910
duke@435 3911 // Create the MachNode object
duke@435 3912 fprintf(fp_cpp, "%s %sNode *node = new (C) %sNode();\n",indent, opClass,opClass);
duke@435 3913
duke@435 3914 if ( (inst->num_post_match_opnds() != 0) ) {
duke@435 3915 // Instruction that contains operands which are not in match rule.
duke@435 3916 //
duke@435 3917 // Check if the first post-match component may be an interesting def
duke@435 3918 bool dont_care = false;
duke@435 3919 ComponentList &comp_list = inst->_components;
duke@435 3920 Component *comp = NULL;
duke@435 3921 comp_list.reset();
duke@435 3922 if ( comp_list.match_iter() != NULL ) dont_care = true;
duke@435 3923
duke@435 3924 // Insert operands that are not in match-rule.
duke@435 3925 // Only insert a DEF if the do_care flag is set
duke@435 3926 comp_list.reset();
duke@435 3927 while ( comp = comp_list.post_match_iter() ) {
duke@435 3928 // Check if we don't care about DEFs or KILLs that are not USEs
duke@435 3929 if ( dont_care && (! comp->isa(Component::USE)) ) {
duke@435 3930 continue;
duke@435 3931 }
duke@435 3932 dont_care = true;
duke@435 3933 // For each operand not in the match rule, call MachOperGenerator
kvn@2561 3934 // with the enum for the opcode that needs to be built.
duke@435 3935 ComponentList clist = inst->_components;
kvn@4161 3936 int index = clist.operand_position(comp->_name, comp->_usedef, inst);
duke@435 3937 const char *opcode = machOperEnum(comp->_type);
duke@435 3938 fprintf(fp_cpp, "%s node->set_opnd_array(%d, ", indent, index);
duke@435 3939 fprintf(fp_cpp, "MachOperGenerator(%s, C));\n", opcode);
duke@435 3940 }
duke@435 3941 }
duke@435 3942 else if ( inst->is_chain_of_constant(_globalNames, opType) ) {
duke@435 3943 // An instruction that chains from a constant!
duke@435 3944 // In this case, we need to subsume the constant into the node
duke@435 3945 // at operand position, oper_input_base().
duke@435 3946 //
duke@435 3947 // Fill in the constant
duke@435 3948 fprintf(fp_cpp, "%s node->_opnd_array[%d] = ", indent,
duke@435 3949 inst->oper_input_base(_globalNames));
duke@435 3950 // #####
duke@435 3951 // Check for multiple constants and then fill them in.
duke@435 3952 // Just like MachOperGenerator
duke@435 3953 const char *opName = inst->_matrule->_rChild->_opType;
duke@435 3954 fprintf(fp_cpp, "new (C) %sOper(", opName);
duke@435 3955 // Grab operand form
duke@435 3956 OperandForm *op = (_globalNames[opName])->is_operand();
duke@435 3957 // Look up the number of constants
duke@435 3958 uint num_consts = op->num_consts(_globalNames);
duke@435 3959 if ( (num_consts > 0) ) {
duke@435 3960 uint i = 0;
duke@435 3961 path_to_constant(fp_cpp, _globalNames, op->_matrule, i);
duke@435 3962 for ( i = 1; i < num_consts; ++i ) {
duke@435 3963 fprintf(fp_cpp, ", ");
duke@435 3964 path_to_constant(fp_cpp, _globalNames, op->_matrule, i);
duke@435 3965 }
duke@435 3966 }
duke@435 3967 fprintf(fp_cpp, " );\n");
duke@435 3968 // #####
duke@435 3969 }
duke@435 3970
duke@435 3971 // Fill in the bottom_type where requested
goetz@6484 3972 if (inst->captures_bottom_type(_globalNames)) {
goetz@6484 3973 if (strncmp("MachCall", inst->mach_base_class(_globalNames), strlen("MachCall"))) {
goetz@6484 3974 fprintf(fp_cpp, "%s node->_bottom_type = _leaf->bottom_type();\n", indent);
goetz@6484 3975 }
duke@435 3976 }
duke@435 3977 if( inst->is_ideal_if() ) {
duke@435 3978 fprintf(fp_cpp, "%s node->_prob = _leaf->as_If()->_prob;\n", indent);
duke@435 3979 fprintf(fp_cpp, "%s node->_fcnt = _leaf->as_If()->_fcnt;\n", indent);
duke@435 3980 }
duke@435 3981 if( inst->is_ideal_fastlock() ) {
duke@435 3982 fprintf(fp_cpp, "%s node->_counters = _leaf->as_FastLock()->counters();\n", indent);
duke@435 3983 }
duke@435 3984
duke@435 3985 }
duke@435 3986
duke@435 3987 //---------------------------declare_cisc_version------------------------------
duke@435 3988 // Build CISC version of this instruction
duke@435 3989 void InstructForm::declare_cisc_version(ArchDesc &AD, FILE *fp_hpp) {
duke@435 3990 if( AD.can_cisc_spill() ) {
duke@435 3991 InstructForm *inst_cisc = cisc_spill_alternate();
duke@435 3992 if (inst_cisc != NULL) {
duke@435 3993 fprintf(fp_hpp, " virtual int cisc_operand() const { return %d; }\n", cisc_spill_operand());
duke@435 3994 fprintf(fp_hpp, " virtual MachNode *cisc_version(int offset, Compile* C);\n");
duke@435 3995 fprintf(fp_hpp, " virtual void use_cisc_RegMask();\n");
duke@435 3996 fprintf(fp_hpp, " virtual const RegMask *cisc_RegMask() const { return _cisc_RegMask; }\n");
duke@435 3997 }
duke@435 3998 }
duke@435 3999 }
duke@435 4000
duke@435 4001 //---------------------------define_cisc_version-------------------------------
duke@435 4002 // Build CISC version of this instruction
duke@435 4003 bool InstructForm::define_cisc_version(ArchDesc &AD, FILE *fp_cpp) {
duke@435 4004 InstructForm *inst_cisc = this->cisc_spill_alternate();
duke@435 4005 if( AD.can_cisc_spill() && (inst_cisc != NULL) ) {
duke@435 4006 const char *name = inst_cisc->_ident;
duke@435 4007 assert( inst_cisc->num_opnds() == this->num_opnds(), "Must have same number of operands");
duke@435 4008 OperandForm *cisc_oper = AD.cisc_spill_operand();
duke@435 4009 assert( cisc_oper != NULL, "insanity check");
duke@435 4010 const char *cisc_oper_name = cisc_oper->_ident;
duke@435 4011 assert( cisc_oper_name != NULL, "insanity check");
duke@435 4012 //
duke@435 4013 // Set the correct reg_mask_or_stack for the cisc operand
duke@435 4014 fprintf(fp_cpp, "\n");
duke@435 4015 fprintf(fp_cpp, "void %sNode::use_cisc_RegMask() {\n", this->_ident);
duke@435 4016 // Lookup the correct reg_mask_or_stack
duke@435 4017 const char *reg_mask_name = cisc_reg_mask_name();
duke@435 4018 fprintf(fp_cpp, " _cisc_RegMask = &STACK_OR_%s;\n", reg_mask_name);
duke@435 4019 fprintf(fp_cpp, "}\n");
duke@435 4020 //
duke@435 4021 // Construct CISC version of this instruction
duke@435 4022 fprintf(fp_cpp, "\n");
duke@435 4023 fprintf(fp_cpp, "// Build CISC version of this instruction\n");
duke@435 4024 fprintf(fp_cpp, "MachNode *%sNode::cisc_version( int offset, Compile* C ) {\n", this->_ident);
duke@435 4025 // Create the MachNode object
duke@435 4026 fprintf(fp_cpp, " %sNode *node = new (C) %sNode();\n", name, name);
duke@435 4027 // Fill in the bottom_type where requested
never@1896 4028 if ( this->captures_bottom_type(AD.globalNames()) ) {
duke@435 4029 fprintf(fp_cpp, " node->_bottom_type = bottom_type();\n");
duke@435 4030 }
twisti@1220 4031
twisti@1220 4032 uint cur_num_opnds = num_opnds();
twisti@1220 4033 if (cur_num_opnds > 1 && cur_num_opnds != num_unique_opnds()) {
twisti@1220 4034 fprintf(fp_cpp," node->_num_opnds = %d;\n", num_unique_opnds());
twisti@1220 4035 }
twisti@1220 4036
duke@435 4037 fprintf(fp_cpp, "\n");
duke@435 4038 fprintf(fp_cpp, " // Copy _idx, inputs and operands to new node\n");
duke@435 4039 fprintf(fp_cpp, " fill_new_machnode(node, C);\n");
duke@435 4040 // Construct operand to access [stack_pointer + offset]
duke@435 4041 fprintf(fp_cpp, " // Construct operand to access [stack_pointer + offset]\n");
duke@435 4042 fprintf(fp_cpp, " node->set_opnd_array(cisc_operand(), new (C) %sOper(offset));\n", cisc_oper_name);
duke@435 4043 fprintf(fp_cpp, "\n");
duke@435 4044
duke@435 4045 // Return result and exit scope
duke@435 4046 fprintf(fp_cpp, " return node;\n");
duke@435 4047 fprintf(fp_cpp, "}\n");
duke@435 4048 fprintf(fp_cpp, "\n");
duke@435 4049 return true;
duke@435 4050 }
duke@435 4051 return false;
duke@435 4052 }
duke@435 4053
duke@435 4054 //---------------------------declare_short_branch_methods----------------------
duke@435 4055 // Build prototypes for short branch methods
duke@435 4056 void InstructForm::declare_short_branch_methods(FILE *fp_hpp) {
duke@435 4057 if (has_short_branch_form()) {
duke@435 4058 fprintf(fp_hpp, " virtual MachNode *short_branch_version(Compile* C);\n");
duke@435 4059 }
duke@435 4060 }
duke@435 4061
duke@435 4062 //---------------------------define_short_branch_methods-----------------------
duke@435 4063 // Build definitions for short branch methods
never@1896 4064 bool InstructForm::define_short_branch_methods(ArchDesc &AD, FILE *fp_cpp) {
duke@435 4065 if (has_short_branch_form()) {
duke@435 4066 InstructForm *short_branch = short_branch_form();
duke@435 4067 const char *name = short_branch->_ident;
duke@435 4068
duke@435 4069 // Construct short_branch_version() method.
duke@435 4070 fprintf(fp_cpp, "// Build short branch version of this instruction\n");
duke@435 4071 fprintf(fp_cpp, "MachNode *%sNode::short_branch_version(Compile* C) {\n", this->_ident);
duke@435 4072 // Create the MachNode object
duke@435 4073 fprintf(fp_cpp, " %sNode *node = new (C) %sNode();\n", name, name);
duke@435 4074 if( is_ideal_if() ) {
duke@435 4075 fprintf(fp_cpp, " node->_prob = _prob;\n");
duke@435 4076 fprintf(fp_cpp, " node->_fcnt = _fcnt;\n");
duke@435 4077 }
duke@435 4078 // Fill in the bottom_type where requested
never@1896 4079 if ( this->captures_bottom_type(AD.globalNames()) ) {
duke@435 4080 fprintf(fp_cpp, " node->_bottom_type = bottom_type();\n");
duke@435 4081 }
duke@435 4082
duke@435 4083 fprintf(fp_cpp, "\n");
duke@435 4084 // Short branch version must use same node index for access
duke@435 4085 // through allocator's tables
duke@435 4086 fprintf(fp_cpp, " // Copy _idx, inputs and operands to new node\n");
duke@435 4087 fprintf(fp_cpp, " fill_new_machnode(node, C);\n");
duke@435 4088
duke@435 4089 // Return result and exit scope
duke@435 4090 fprintf(fp_cpp, " return node;\n");
duke@435 4091 fprintf(fp_cpp, "}\n");
duke@435 4092 fprintf(fp_cpp,"\n");
duke@435 4093 return true;
duke@435 4094 }
duke@435 4095 return false;
duke@435 4096 }
duke@435 4097
duke@435 4098
duke@435 4099 //---------------------------buildMachNodeGenerator----------------------------
duke@435 4100 // Build switch to invoke appropriate "new" MachNode for an opcode
duke@435 4101 void ArchDesc::buildMachNodeGenerator(FILE *fp_cpp) {
duke@435 4102
duke@435 4103 // Build switch to invoke 'new' for a specific MachNode
duke@435 4104 fprintf(fp_cpp, "\n");
duke@435 4105 fprintf(fp_cpp, "\n");
duke@435 4106 fprintf(fp_cpp,
duke@435 4107 "//------------------------- MachNode Generator ---------------\n");
duke@435 4108 fprintf(fp_cpp,
duke@435 4109 "// A switch statement on the dense-packed user-defined type system\n"
duke@435 4110 "// that invokes 'new' on the corresponding class constructor.\n");
duke@435 4111 fprintf(fp_cpp, "\n");
duke@435 4112 fprintf(fp_cpp, "MachNode *State::MachNodeGenerator");
duke@435 4113 fprintf(fp_cpp, "(int opcode, Compile* C)");
duke@435 4114 fprintf(fp_cpp, "{\n");
duke@435 4115 fprintf(fp_cpp, " switch(opcode) {\n");
duke@435 4116
duke@435 4117 // Provide constructor for all user-defined instructions
duke@435 4118 _instructions.reset();
duke@435 4119 int opIndex = operandFormCount();
duke@435 4120 InstructForm *inst;
duke@435 4121 for( ; (inst = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 4122 // Ensure that matrule is defined.
duke@435 4123 if ( inst->_matrule == NULL ) continue;
duke@435 4124
duke@435 4125 int opcode = opIndex++;
duke@435 4126 const char *opClass = inst->_ident;
duke@435 4127 char *opType = NULL;
duke@435 4128
duke@435 4129 // Generate the case statement for this instruction
duke@435 4130 fprintf(fp_cpp, " case %s_rule:", opClass);
duke@435 4131
duke@435 4132 // Start local scope
kvn@4161 4133 fprintf(fp_cpp, " {\n");
duke@435 4134 // Generate code to construct the new MachNode
duke@435 4135 buildMachNode(fp_cpp, inst, " ");
duke@435 4136 // Return result and exit scope
duke@435 4137 fprintf(fp_cpp, " return node;\n");
duke@435 4138 fprintf(fp_cpp, " }\n");
duke@435 4139 }
duke@435 4140
duke@435 4141 // Generate the default case for switch(opcode)
duke@435 4142 fprintf(fp_cpp, " \n");
duke@435 4143 fprintf(fp_cpp, " default:\n");
duke@435 4144 fprintf(fp_cpp, " fprintf(stderr, \"Default MachNode Generator invoked for: \\n\");\n");
duke@435 4145 fprintf(fp_cpp, " fprintf(stderr, \" opcode = %cd\\n\", opcode);\n", '%');
duke@435 4146 fprintf(fp_cpp, " break;\n");
duke@435 4147 fprintf(fp_cpp, " };\n");
duke@435 4148
duke@435 4149 // Generate the closing for method Matcher::MachNodeGenerator
duke@435 4150 fprintf(fp_cpp, " return NULL;\n");
duke@435 4151 fprintf(fp_cpp, "}\n");
duke@435 4152 }
duke@435 4153
duke@435 4154
duke@435 4155 //---------------------------buildInstructMatchCheck--------------------------
duke@435 4156 // Output the method to Matcher which checks whether or not a specific
duke@435 4157 // instruction has a matching rule for the host architecture.
duke@435 4158 void ArchDesc::buildInstructMatchCheck(FILE *fp_cpp) const {
duke@435 4159 fprintf(fp_cpp, "\n\n");
duke@435 4160 fprintf(fp_cpp, "const bool Matcher::has_match_rule(int opcode) {\n");
duke@435 4161 fprintf(fp_cpp, " assert(_last_machine_leaf < opcode && opcode < _last_opcode, \"opcode in range\");\n");
duke@435 4162 fprintf(fp_cpp, " return _hasMatchRule[opcode];\n");
duke@435 4163 fprintf(fp_cpp, "}\n\n");
duke@435 4164
duke@435 4165 fprintf(fp_cpp, "const bool Matcher::_hasMatchRule[_last_opcode] = {\n");
duke@435 4166 int i;
duke@435 4167 for (i = 0; i < _last_opcode - 1; i++) {
duke@435 4168 fprintf(fp_cpp, " %-5s, // %s\n",
duke@435 4169 _has_match_rule[i] ? "true" : "false",
duke@435 4170 NodeClassNames[i]);
duke@435 4171 }
duke@435 4172 fprintf(fp_cpp, " %-5s // %s\n",
duke@435 4173 _has_match_rule[i] ? "true" : "false",
duke@435 4174 NodeClassNames[i]);
duke@435 4175 fprintf(fp_cpp, "};\n");
duke@435 4176 }
duke@435 4177
duke@435 4178 //---------------------------buildFrameMethods---------------------------------
duke@435 4179 // Output the methods to Matcher which specify frame behavior
duke@435 4180 void ArchDesc::buildFrameMethods(FILE *fp_cpp) {
duke@435 4181 fprintf(fp_cpp,"\n\n");
duke@435 4182 // Stack Direction
duke@435 4183 fprintf(fp_cpp,"bool Matcher::stack_direction() const { return %s; }\n\n",
duke@435 4184 _frame->_direction ? "true" : "false");
duke@435 4185 // Sync Stack Slots
duke@435 4186 fprintf(fp_cpp,"int Compile::sync_stack_slots() const { return %s; }\n\n",
duke@435 4187 _frame->_sync_stack_slots);
duke@435 4188 // Java Stack Alignment
duke@435 4189 fprintf(fp_cpp,"uint Matcher::stack_alignment_in_bytes() { return %s; }\n\n",
duke@435 4190 _frame->_alignment);
duke@435 4191 // Java Return Address Location
duke@435 4192 fprintf(fp_cpp,"OptoReg::Name Matcher::return_addr() const {");
duke@435 4193 if (_frame->_return_addr_loc) {
duke@435 4194 fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n",
duke@435 4195 _frame->_return_addr);
duke@435 4196 }
duke@435 4197 else {
duke@435 4198 fprintf(fp_cpp," return OptoReg::stack2reg(%s); }\n\n",
duke@435 4199 _frame->_return_addr);
duke@435 4200 }
duke@435 4201 // Java Stack Slot Preservation
duke@435 4202 fprintf(fp_cpp,"uint Compile::in_preserve_stack_slots() ");
duke@435 4203 fprintf(fp_cpp,"{ return %s; }\n\n", _frame->_in_preserve_slots);
duke@435 4204 // Top Of Stack Slot Preservation, for both Java and C
duke@435 4205 fprintf(fp_cpp,"uint Compile::out_preserve_stack_slots() ");
duke@435 4206 fprintf(fp_cpp,"{ return SharedRuntime::out_preserve_stack_slots(); }\n\n");
duke@435 4207 // varargs C out slots killed
duke@435 4208 fprintf(fp_cpp,"uint Compile::varargs_C_out_slots_killed() const ");
duke@435 4209 fprintf(fp_cpp,"{ return %s; }\n\n", _frame->_varargs_C_out_slots_killed);
duke@435 4210 // Java Argument Position
duke@435 4211 fprintf(fp_cpp,"void Matcher::calling_convention(BasicType *sig_bt, VMRegPair *regs, uint length, bool is_outgoing) {\n");
duke@435 4212 fprintf(fp_cpp,"%s\n", _frame->_calling_convention);
duke@435 4213 fprintf(fp_cpp,"}\n\n");
duke@435 4214 // Native Argument Position
duke@435 4215 fprintf(fp_cpp,"void Matcher::c_calling_convention(BasicType *sig_bt, VMRegPair *regs, uint length) {\n");
duke@435 4216 fprintf(fp_cpp,"%s\n", _frame->_c_calling_convention);
duke@435 4217 fprintf(fp_cpp,"}\n\n");
duke@435 4218 // Java Return Value Location
duke@435 4219 fprintf(fp_cpp,"OptoRegPair Matcher::return_value(int ideal_reg, bool is_outgoing) {\n");
duke@435 4220 fprintf(fp_cpp,"%s\n", _frame->_return_value);
duke@435 4221 fprintf(fp_cpp,"}\n\n");
duke@435 4222 // Native Return Value Location
duke@435 4223 fprintf(fp_cpp,"OptoRegPair Matcher::c_return_value(int ideal_reg, bool is_outgoing) {\n");
duke@435 4224 fprintf(fp_cpp,"%s\n", _frame->_c_return_value);
duke@435 4225 fprintf(fp_cpp,"}\n\n");
duke@435 4226
duke@435 4227 // Inline Cache Register, mask definition, and encoding
duke@435 4228 fprintf(fp_cpp,"OptoReg::Name Matcher::inline_cache_reg() {");
duke@435 4229 fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n",
duke@435 4230 _frame->_inline_cache_reg);
duke@435 4231 fprintf(fp_cpp,"int Matcher::inline_cache_reg_encode() {");
duke@435 4232 fprintf(fp_cpp," return _regEncode[inline_cache_reg()]; }\n\n");
duke@435 4233
duke@435 4234 // Interpreter's Method Oop Register, mask definition, and encoding
duke@435 4235 fprintf(fp_cpp,"OptoReg::Name Matcher::interpreter_method_oop_reg() {");
duke@435 4236 fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n",
duke@435 4237 _frame->_interpreter_method_oop_reg);
duke@435 4238 fprintf(fp_cpp,"int Matcher::interpreter_method_oop_reg_encode() {");
duke@435 4239 fprintf(fp_cpp," return _regEncode[interpreter_method_oop_reg()]; }\n\n");
duke@435 4240
duke@435 4241 // Interpreter's Frame Pointer Register, mask definition, and encoding
duke@435 4242 fprintf(fp_cpp,"OptoReg::Name Matcher::interpreter_frame_pointer_reg() {");
duke@435 4243 if (_frame->_interpreter_frame_pointer_reg == NULL)
duke@435 4244 fprintf(fp_cpp," return OptoReg::Bad; }\n\n");
duke@435 4245 else
duke@435 4246 fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n",
duke@435 4247 _frame->_interpreter_frame_pointer_reg);
duke@435 4248
duke@435 4249 // Frame Pointer definition
duke@435 4250 /* CNC - I can not contemplate having a different frame pointer between
duke@435 4251 Java and native code; makes my head hurt to think about it.
duke@435 4252 fprintf(fp_cpp,"OptoReg::Name Matcher::frame_pointer() const {");
duke@435 4253 fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n",
duke@435 4254 _frame->_frame_pointer);
duke@435 4255 */
duke@435 4256 // (Native) Frame Pointer definition
duke@435 4257 fprintf(fp_cpp,"OptoReg::Name Matcher::c_frame_pointer() const {");
duke@435 4258 fprintf(fp_cpp," return OptoReg::Name(%s_num); }\n\n",
duke@435 4259 _frame->_frame_pointer);
duke@435 4260
duke@435 4261 // Number of callee-save + always-save registers for calling convention
duke@435 4262 fprintf(fp_cpp, "// Number of callee-save + always-save registers\n");
duke@435 4263 fprintf(fp_cpp, "int Matcher::number_of_saved_registers() {\n");
duke@435 4264 RegDef *rdef;
duke@435 4265 int nof_saved_registers = 0;
duke@435 4266 _register->reset_RegDefs();
duke@435 4267 while( (rdef = _register->iter_RegDefs()) != NULL ) {
duke@435 4268 if( !strcmp(rdef->_callconv, "SOE") || !strcmp(rdef->_callconv, "AS") )
duke@435 4269 ++nof_saved_registers;
duke@435 4270 }
duke@435 4271 fprintf(fp_cpp, " return %d;\n", nof_saved_registers);
duke@435 4272 fprintf(fp_cpp, "};\n\n");
duke@435 4273 }
duke@435 4274
duke@435 4275
duke@435 4276
duke@435 4277
duke@435 4278 static int PrintAdlcCisc = 0;
duke@435 4279 //---------------------------identify_cisc_spilling----------------------------
duke@435 4280 // Get info for the CISC_oracle and MachNode::cisc_version()
duke@435 4281 void ArchDesc::identify_cisc_spill_instructions() {
duke@435 4282
kvn@4161 4283 if (_frame == NULL)
kvn@4161 4284 return;
kvn@4161 4285
duke@435 4286 // Find the user-defined operand for cisc-spilling
duke@435 4287 if( _frame->_cisc_spilling_operand_name != NULL ) {
duke@435 4288 const Form *form = _globalNames[_frame->_cisc_spilling_operand_name];
duke@435 4289 OperandForm *oper = form ? form->is_operand() : NULL;
duke@435 4290 // Verify the user's suggestion
duke@435 4291 if( oper != NULL ) {
duke@435 4292 // Ensure that match field is defined.
duke@435 4293 if ( oper->_matrule != NULL ) {
duke@435 4294 MatchRule &mrule = *oper->_matrule;
duke@435 4295 if( strcmp(mrule._opType,"AddP") == 0 ) {
duke@435 4296 MatchNode *left = mrule._lChild;
duke@435 4297 MatchNode *right= mrule._rChild;
duke@435 4298 if( left != NULL && right != NULL ) {
duke@435 4299 const Form *left_op = _globalNames[left->_opType]->is_operand();
duke@435 4300 const Form *right_op = _globalNames[right->_opType]->is_operand();
duke@435 4301 if( (left_op != NULL && right_op != NULL)
duke@435 4302 && (left_op->interface_type(_globalNames) == Form::register_interface)
duke@435 4303 && (right_op->interface_type(_globalNames) == Form::constant_interface) ) {
duke@435 4304 // Successfully verified operand
duke@435 4305 set_cisc_spill_operand( oper );
duke@435 4306 if( _cisc_spill_debug ) {
duke@435 4307 fprintf(stderr, "\n\nVerified CISC-spill operand %s\n\n", oper->_ident);
duke@435 4308 }
duke@435 4309 }
duke@435 4310 }
duke@435 4311 }
duke@435 4312 }
duke@435 4313 }
duke@435 4314 }
duke@435 4315
duke@435 4316 if( cisc_spill_operand() != NULL ) {
duke@435 4317 // N^2 comparison of instructions looking for a cisc-spilling version
duke@435 4318 _instructions.reset();
duke@435 4319 InstructForm *instr;
duke@435 4320 for( ; (instr = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 4321 // Ensure that match field is defined.
duke@435 4322 if ( instr->_matrule == NULL ) continue;
duke@435 4323
duke@435 4324 MatchRule &mrule = *instr->_matrule;
duke@435 4325 Predicate *pred = instr->build_predicate();
duke@435 4326
duke@435 4327 // Grab the machine type of the operand
duke@435 4328 const char *rootOp = instr->_ident;
duke@435 4329 mrule._machType = rootOp;
duke@435 4330
duke@435 4331 // Find result type for match
duke@435 4332 const char *result = instr->reduce_result();
duke@435 4333
duke@435 4334 if( PrintAdlcCisc ) fprintf(stderr, " new instruction %s \n", instr->_ident ? instr->_ident : " ");
duke@435 4335 bool found_cisc_alternate = false;
duke@435 4336 _instructions.reset2();
duke@435 4337 InstructForm *instr2;
duke@435 4338 for( ; !found_cisc_alternate && (instr2 = (InstructForm*)_instructions.iter2()) != NULL; ) {
duke@435 4339 // Ensure that match field is defined.
duke@435 4340 if( PrintAdlcCisc ) fprintf(stderr, " instr2 == %s \n", instr2->_ident ? instr2->_ident : " ");
duke@435 4341 if ( instr2->_matrule != NULL
duke@435 4342 && (instr != instr2 ) // Skip self
duke@435 4343 && (instr2->reduce_result() != NULL) // want same result
duke@435 4344 && (strcmp(result, instr2->reduce_result()) == 0)) {
duke@435 4345 MatchRule &mrule2 = *instr2->_matrule;
duke@435 4346 Predicate *pred2 = instr2->build_predicate();
duke@435 4347 found_cisc_alternate = instr->cisc_spills_to(*this, instr2);
duke@435 4348 }
duke@435 4349 }
duke@435 4350 }
duke@435 4351 }
duke@435 4352 }
duke@435 4353
duke@435 4354 //---------------------------build_cisc_spilling-------------------------------
duke@435 4355 // Get info for the CISC_oracle and MachNode::cisc_version()
duke@435 4356 void ArchDesc::build_cisc_spill_instructions(FILE *fp_hpp, FILE *fp_cpp) {
duke@435 4357 // Output the table for cisc spilling
duke@435 4358 fprintf(fp_cpp, "// The following instructions can cisc-spill\n");
duke@435 4359 _instructions.reset();
duke@435 4360 InstructForm *inst = NULL;
duke@435 4361 for(; (inst = (InstructForm*)_instructions.iter()) != NULL; ) {
duke@435 4362 // Ensure this is a machine-world instruction
duke@435 4363 if ( inst->ideal_only() ) continue;
duke@435 4364 const char *inst_name = inst->_ident;
duke@435 4365 int operand = inst->cisc_spill_operand();
duke@435 4366 if( operand != AdlcVMDeps::Not_cisc_spillable ) {
duke@435 4367 InstructForm *inst2 = inst->cisc_spill_alternate();
duke@435 4368 fprintf(fp_cpp, "// %s can cisc-spill operand %d to %s\n", inst->_ident, operand, inst2->_ident);
duke@435 4369 }
duke@435 4370 }
duke@435 4371 fprintf(fp_cpp, "\n\n");
duke@435 4372 }
duke@435 4373
duke@435 4374 //---------------------------identify_short_branches----------------------------
duke@435 4375 // Get info for our short branch replacement oracle.
duke@435 4376 void ArchDesc::identify_short_branches() {
duke@435 4377 // Walk over all instructions, checking to see if they match a short
duke@435 4378 // branching alternate.
duke@435 4379 _instructions.reset();
duke@435 4380 InstructForm *instr;
duke@435 4381 while( (instr = (InstructForm*)_instructions.iter()) != NULL ) {
duke@435 4382 // The instruction must have a match rule.
duke@435 4383 if (instr->_matrule != NULL &&
duke@435 4384 instr->is_short_branch()) {
duke@435 4385
duke@435 4386 _instructions.reset2();
duke@435 4387 InstructForm *instr2;
duke@435 4388 while( (instr2 = (InstructForm*)_instructions.iter2()) != NULL ) {
duke@435 4389 instr2->check_branch_variant(*this, instr);
duke@435 4390 }
duke@435 4391 }
duke@435 4392 }
duke@435 4393 }
duke@435 4394
duke@435 4395
duke@435 4396 //---------------------------identify_unique_operands---------------------------
duke@435 4397 // Identify unique operands.
duke@435 4398 void ArchDesc::identify_unique_operands() {
duke@435 4399 // Walk over all instructions.
duke@435 4400 _instructions.reset();
duke@435 4401 InstructForm *instr;
duke@435 4402 while( (instr = (InstructForm*)_instructions.iter()) != NULL ) {
duke@435 4403 // Ensure this is a machine-world instruction
duke@435 4404 if (!instr->ideal_only()) {
duke@435 4405 instr->set_unique_opnds();
duke@435 4406 }
duke@435 4407 }
duke@435 4408 }

mercurial