Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/adlc/main.cpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324// MAIN.CPP - Entry point for the Architecture Description Language Compiler25#include "adlc.hpp"2627//------------------------------Prototypes-------------------------------------28static void usage(ArchDesc& AD); // Print usage message and exit29static char *strip_ext(char *fname); // Strip off name extension30static char *base_plus_suffix(const char* base, const char *suffix);// New concatenated string31static int get_legal_text(FileBuff &fbuf, char **legal_text); // Get pointer to legal text3233ArchDesc* globalAD = NULL; // global reference to Architecture Description object3435const char* get_basename(const char* filename) {36const char *basename = filename;37const char *cp;38for (cp = basename; *cp; cp++) {39if (*cp == '/') {40basename = cp+1;41}42}43return basename;44}4546//------------------------------main-------------------------------------------47int main(int argc, char *argv[])48{49ArchDesc AD; // Architecture Description object50globalAD = &AD;5152// ResourceMark mark;53ADLParser *ADL_Parse; // ADL Parser object to parse AD file5455// Check for proper arguments56if( argc == 1 ) usage(AD); // No arguments? Then print usage5758// Read command line arguments and file names59for( int i = 1; i < argc; i++ ) { // For all arguments60register char *s = argv[i]; // Get option/filename6162if( *s++ == '-' ) { // It's a flag? (not a filename)63if( !*s ) { // Stand-alone `-' means stdin64//********** INSERT CODE HERE **********65} else while (*s != '\0') { // While have flags on option66switch (*s++) { // Handle flag67case 'd': // Debug flag68AD._dfa_debug += 1; // Set Debug Flag69break;70case 'g': // Debug ad location flag71AD._adlocation_debug += 1; // Set Debug ad location Flag72break;73case 'o': // No Output Flag74AD._no_output ^= 1; // Toggle no_output flag75break;76case 'q': // Quiet Mode Flag77AD._quiet_mode ^= 1; // Toggle quiet_mode flag78break;79case 'w': // Disable Warnings Flag80AD._disable_warnings ^= 1; // Toggle disable_warnings flag81break;82case 'T': // Option to make DFA as many subroutine calls.83AD._dfa_small += 1; // Set Mode Flag84break;85case 'c': { // Set C++ Output file name86AD._CPP_file._name = s;87const char *base = strip_ext(strdup(s));88AD._CPP_CLONE_file._name = base_plus_suffix(base,"_clone.cpp");89AD._CPP_EXPAND_file._name = base_plus_suffix(base,"_expand.cpp");90AD._CPP_FORMAT_file._name = base_plus_suffix(base,"_format.cpp");91AD._CPP_GEN_file._name = base_plus_suffix(base,"_gen.cpp");92AD._CPP_MISC_file._name = base_plus_suffix(base,"_misc.cpp");93AD._CPP_PEEPHOLE_file._name = base_plus_suffix(base,"_peephole.cpp");94AD._CPP_PIPELINE_file._name = base_plus_suffix(base,"_pipeline.cpp");95s += strlen(s);96break;97}98case 'h': // Set C++ Output file name99AD._HPP_file._name = s; s += strlen(s);100break;101case 'v': // Set C++ Output file name102AD._VM_file._name = s; s += strlen(s);103break;104case 'a': // Set C++ Output file name105AD._DFA_file._name = s;106AD._bug_file._name = s;107s += strlen(s);108break;109case '#': // Special internal debug flag110AD._adl_debug++; // Increment internal debug level111break;112case 's': // Output which instructions are cisc-spillable113AD._cisc_spill_debug = true;114break;115case 'D': // Flag Definition116{117char* flag = s;118s += strlen(s);119char* def = strchr(flag, '=');120if (def == NULL) def = (char*)"1";121else *def++ = '\0';122AD.set_preproc_def(flag, def);123}124break;125case 'U': // Flag Un-Definition126{127char* flag = s;128s += strlen(s);129AD.set_preproc_def(flag, NULL);130}131break;132default: // Unknown option133usage(AD); // So print usage and exit134} // End of switch on options...135} // End of while have options...136137} else { // Not an option; must be a filename138AD._ADL_file._name = argv[i]; // Set the input filename139140// // Files for storage, based on input file name141const char *base = strip_ext(strdup(argv[i]));142char *temp = base_plus_suffix("dfa_",base);143AD._DFA_file._name = base_plus_suffix(temp,".cpp");144delete temp;145temp = base_plus_suffix("ad_",base);146AD._CPP_file._name = base_plus_suffix(temp,".cpp");147AD._CPP_CLONE_file._name = base_plus_suffix(temp,"_clone.cpp");148AD._CPP_EXPAND_file._name = base_plus_suffix(temp,"_expand.cpp");149AD._CPP_FORMAT_file._name = base_plus_suffix(temp,"_format.cpp");150AD._CPP_GEN_file._name = base_plus_suffix(temp,"_gen.cpp");151AD._CPP_MISC_file._name = base_plus_suffix(temp,"_misc.cpp");152AD._CPP_PEEPHOLE_file._name = base_plus_suffix(temp,"_peephole.cpp");153AD._CPP_PIPELINE_file._name = base_plus_suffix(temp,"_pipeline.cpp");154AD._HPP_file._name = base_plus_suffix(temp,".hpp");155delete temp;156temp = base_plus_suffix("adGlobals_",base);157AD._VM_file._name = base_plus_suffix(temp,".hpp");158delete temp;159temp = base_plus_suffix("bugs_",base);160AD._bug_file._name = base_plus_suffix(temp,".out");161delete temp;162} // End of files vs options...163} // End of while have command line arguments164165// Open files used to store the matcher and its components166if (AD.open_files() == 0) return 1; // Open all input/output files167168// Build the File Buffer, Parse the input, & Generate Code169FileBuff ADL_Buf(&AD._ADL_file, AD); // Create a file buffer for input file170171// Get pointer to legal text at the beginning of AD file.172// It will be used in generated ad files.173char* legal_text;174int legal_sz = get_legal_text(ADL_Buf, &legal_text);175176ADL_Parse = new ADLParser(ADL_Buf, AD); // Create a parser to parse the buffer177ADL_Parse->parse(); // Parse buffer & build description lists178179if( AD._dfa_debug >= 1 ) { // For higher debug settings, print dump180AD.dump();181}182183delete ADL_Parse; // Delete parser184185// Verify that the results of the parse are consistent186AD.verify();187188// Prepare to generate the result files:189AD.generateMatchLists();190AD.identify_unique_operands();191AD.identify_cisc_spill_instructions();192AD.identify_short_branches();193// Make sure every file starts with a copyright:194AD.addSunCopyright(legal_text, legal_sz, AD._HPP_file._fp); // .hpp195AD.addSunCopyright(legal_text, legal_sz, AD._CPP_file._fp); // .cpp196AD.addSunCopyright(legal_text, legal_sz, AD._CPP_CLONE_file._fp); // .cpp197AD.addSunCopyright(legal_text, legal_sz, AD._CPP_EXPAND_file._fp); // .cpp198AD.addSunCopyright(legal_text, legal_sz, AD._CPP_FORMAT_file._fp); // .cpp199AD.addSunCopyright(legal_text, legal_sz, AD._CPP_GEN_file._fp); // .cpp200AD.addSunCopyright(legal_text, legal_sz, AD._CPP_MISC_file._fp); // .cpp201AD.addSunCopyright(legal_text, legal_sz, AD._CPP_PEEPHOLE_file._fp); // .cpp202AD.addSunCopyright(legal_text, legal_sz, AD._CPP_PIPELINE_file._fp); // .cpp203AD.addSunCopyright(legal_text, legal_sz, AD._VM_file._fp); // .hpp204AD.addSunCopyright(legal_text, legal_sz, AD._DFA_file._fp); // .cpp205// Add include guards for all .hpp files206AD.addIncludeGuardStart(AD._HPP_file, "GENERATED_ADFILES_AD_HPP"); // .hpp207AD.addIncludeGuardStart(AD._VM_file, "GENERATED_ADFILES_ADGLOBALS_HPP"); // .hpp208// Add includes209AD.addInclude(AD._CPP_file, "precompiled.hpp");210AD.addInclude(AD._CPP_file, "adfiles", get_basename(AD._VM_file._name));211AD.addInclude(AD._CPP_file, "adfiles", get_basename(AD._HPP_file._name));212AD.addInclude(AD._CPP_file, "memory/allocation.inline.hpp");213AD.addInclude(AD._CPP_file, "asm/macroAssembler.inline.hpp");214AD.addInclude(AD._CPP_file, "code/compiledIC.hpp");215AD.addInclude(AD._CPP_file, "code/vmreg.hpp");216AD.addInclude(AD._CPP_file, "gc_interface/collectedHeap.inline.hpp");217AD.addInclude(AD._CPP_file, "oops/compiledICHolder.hpp");218AD.addInclude(AD._CPP_file, "oops/markOop.hpp");219AD.addInclude(AD._CPP_file, "oops/method.hpp");220AD.addInclude(AD._CPP_file, "oops/oop.inline.hpp");221AD.addInclude(AD._CPP_file, "oops/oop.inline2.hpp");222AD.addInclude(AD._CPP_file, "opto/cfgnode.hpp");223AD.addInclude(AD._CPP_file, "opto/locknode.hpp");224AD.addInclude(AD._CPP_file, "opto/opcodes.hpp");225AD.addInclude(AD._CPP_file, "opto/regalloc.hpp");226AD.addInclude(AD._CPP_file, "opto/regmask.hpp");227AD.addInclude(AD._CPP_file, "opto/runtime.hpp");228AD.addInclude(AD._CPP_file, "runtime/biasedLocking.hpp");229AD.addInclude(AD._CPP_file, "runtime/sharedRuntime.hpp");230AD.addInclude(AD._CPP_file, "runtime/stubRoutines.hpp");231AD.addInclude(AD._CPP_file, "utilities/growableArray.hpp");232#ifdef TARGET_ARCH_x86233AD.addInclude(AD._CPP_file, "nativeInst_x86.hpp");234AD.addInclude(AD._CPP_file, "vmreg_x86.inline.hpp");235#endif236#ifdef TARGET_ARCH_aarch32237AD.addInclude(AD._CPP_file, "nativeInst_aarch32.hpp");238AD.addInclude(AD._CPP_file, "vmreg_aarch32.inline.hpp");239#endif240#ifdef TARGET_ARCH_aarch64241AD.addInclude(AD._CPP_file, "assembler_aarch64.inline.hpp");242AD.addInclude(AD._CPP_file, "nativeInst_aarch64.hpp");243AD.addInclude(AD._CPP_file, "vmreg_aarch64.inline.hpp");244#endif245#ifdef TARGET_ARCH_sparc246AD.addInclude(AD._CPP_file, "nativeInst_sparc.hpp");247AD.addInclude(AD._CPP_file, "vmreg_sparc.inline.hpp");248#endif249#ifdef TARGET_ARCH_arm250AD.addInclude(AD._CPP_file, "nativeInst_arm.hpp");251AD.addInclude(AD._CPP_file, "vmreg_arm.inline.hpp");252#endif253#ifdef TARGET_ARCH_ppc254AD.addInclude(AD._CPP_file, "assembler_ppc.inline.hpp");255AD.addInclude(AD._CPP_file, "nativeInst_ppc.hpp");256AD.addInclude(AD._CPP_file, "vmreg_ppc.inline.hpp");257#endif258AD.addInclude(AD._HPP_file, "memory/allocation.hpp");259AD.addInclude(AD._HPP_file, "opto/machnode.hpp");260AD.addInclude(AD._HPP_file, "opto/node.hpp");261AD.addInclude(AD._HPP_file, "opto/regalloc.hpp");262AD.addInclude(AD._HPP_file, "opto/subnode.hpp");263AD.addInclude(AD._HPP_file, "opto/vectornode.hpp");264AD.addInclude(AD._CPP_CLONE_file, "precompiled.hpp");265AD.addInclude(AD._CPP_CLONE_file, "adfiles", get_basename(AD._HPP_file._name));266AD.addInclude(AD._CPP_EXPAND_file, "precompiled.hpp");267AD.addInclude(AD._CPP_EXPAND_file, "adfiles", get_basename(AD._HPP_file._name));268AD.addInclude(AD._CPP_FORMAT_file, "precompiled.hpp");269AD.addInclude(AD._CPP_FORMAT_file, "adfiles", get_basename(AD._HPP_file._name));270AD.addInclude(AD._CPP_GEN_file, "precompiled.hpp");271AD.addInclude(AD._CPP_GEN_file, "adfiles", get_basename(AD._HPP_file._name));272AD.addInclude(AD._CPP_GEN_file, "opto/cfgnode.hpp");273AD.addInclude(AD._CPP_GEN_file, "opto/locknode.hpp");274AD.addInclude(AD._CPP_MISC_file, "precompiled.hpp");275AD.addInclude(AD._CPP_MISC_file, "adfiles", get_basename(AD._HPP_file._name));276AD.addInclude(AD._CPP_PEEPHOLE_file, "precompiled.hpp");277AD.addInclude(AD._CPP_PEEPHOLE_file, "adfiles", get_basename(AD._HPP_file._name));278AD.addInclude(AD._CPP_PIPELINE_file, "precompiled.hpp");279AD.addInclude(AD._CPP_PIPELINE_file, "adfiles", get_basename(AD._HPP_file._name));280AD.addInclude(AD._DFA_file, "precompiled.hpp");281AD.addInclude(AD._DFA_file, "adfiles", get_basename(AD._HPP_file._name));282AD.addInclude(AD._DFA_file, "opto/cfgnode.hpp"); // Use PROB_MAX in predicate.283AD.addInclude(AD._DFA_file, "opto/matcher.hpp");284AD.addInclude(AD._DFA_file, "opto/opcodes.hpp");285// Make sure each .cpp file starts with include lines:286// files declaring and defining generators for Mach* Objects (hpp,cpp)287// Generate the result files:288// enumerations, class definitions, object generators, and the DFA289// file containing enumeration of machine operands & instructions (hpp)290AD.addPreHeaderBlocks(AD._HPP_file._fp); // .hpp291AD.buildMachOperEnum(AD._HPP_file._fp); // .hpp292AD.buildMachOpcodesEnum(AD._HPP_file._fp); // .hpp293AD.buildMachRegisterNumbers(AD._VM_file._fp); // VM file294AD.buildMachRegisterEncodes(AD._HPP_file._fp); // .hpp file295AD.declareRegSizes(AD._HPP_file._fp); // .hpp296AD.build_pipeline_enums(AD._HPP_file._fp); // .hpp297// output definition of class "State"298AD.defineStateClass(AD._HPP_file._fp); // .hpp299// file declaring the Mach* classes derived from MachOper and MachNode300AD.declareClasses(AD._HPP_file._fp);301// declare and define maps: in the .hpp and .cpp files respectively302AD.addSourceBlocks(AD._CPP_file._fp); // .cpp303AD.addHeaderBlocks(AD._HPP_file._fp); // .hpp304AD.buildReduceMaps(AD._HPP_file._fp, AD._CPP_file._fp);305AD.buildMustCloneMap(AD._HPP_file._fp, AD._CPP_file._fp);306// build CISC_spilling oracle and MachNode::cisc_spill() methods307AD.build_cisc_spill_instructions(AD._HPP_file._fp, AD._CPP_file._fp);308// define methods for machine dependent State, MachOper, and MachNode classes309AD.defineClasses(AD._CPP_file._fp);310AD.buildMachOperGenerator(AD._CPP_GEN_file._fp);// .cpp311AD.buildMachNodeGenerator(AD._CPP_GEN_file._fp);// .cpp312// define methods for machine dependent instruction matching313AD.buildInstructMatchCheck(AD._CPP_file._fp); // .cpp314// define methods for machine dependent frame management315AD.buildFrameMethods(AD._CPP_file._fp); // .cpp316AD.generate_needs_clone_jvms(AD._CPP_file._fp);317318// do this last:319AD.addPreprocessorChecks(AD._CPP_file._fp); // .cpp320AD.addPreprocessorChecks(AD._CPP_CLONE_file._fp); // .cpp321AD.addPreprocessorChecks(AD._CPP_EXPAND_file._fp); // .cpp322AD.addPreprocessorChecks(AD._CPP_FORMAT_file._fp); // .cpp323AD.addPreprocessorChecks(AD._CPP_GEN_file._fp); // .cpp324AD.addPreprocessorChecks(AD._CPP_MISC_file._fp); // .cpp325AD.addPreprocessorChecks(AD._CPP_PEEPHOLE_file._fp); // .cpp326AD.addPreprocessorChecks(AD._CPP_PIPELINE_file._fp); // .cpp327328// define the finite automata that selects lowest cost production329AD.buildDFA(AD._DFA_file._fp);330// Add include guards for all .hpp files331AD.addIncludeGuardEnd(AD._HPP_file, "GENERATED_ADFILES_AD_HPP"); // .hpp332AD.addIncludeGuardEnd(AD._VM_file, "GENERATED_ADFILES_ADGLOBALS_HPP"); // .hpp333334AD.close_files(0); // Close all input/output files335336// Final printout and statistics337// cout << program;338339if( AD._dfa_debug & 2 ) { // For higher debug settings, print timing info340// Timer t_stop;341// Timer t_total = t_stop - t_start; // Total running time342// cerr << "\n---Architecture Description Totals---\n";343// cerr << ", Total lines: " << TotalLines;344// float l = TotalLines;345// cerr << "\nTotal Compilation Time: " << t_total << "\n";346// float ft = (float)t_total;347// if( ft > 0.0 ) fprintf(stderr,"Lines/sec: %#5.2f\n", l/ft);348}349return (AD._syntax_errs + AD._semantic_errs + AD._internal_errs); // Bye Bye!!350}351352//------------------------------usage------------------------------------------353static void usage(ArchDesc& AD)354{355printf("Architecture Description Language Compiler\n\n");356printf("Usage: adlc [-doqwTs] [-#]* [-D<FLAG>[=<DEF>]] [-U<FLAG>] [-c<CPP_FILE_NAME>] [-h<HPP_FILE_NAME>] [-a<DFA_FILE_NAME>] [-v<GLOBALS_FILE_NAME>] <ADL_FILE_NAME>\n");357printf(" d produce DFA debugging info\n");358printf(" o no output produced, syntax and semantic checking only\n");359printf(" q quiet mode, supresses all non-essential messages\n");360printf(" w suppress warning messages\n");361printf(" T make DFA as many subroutine calls\n");362printf(" s output which instructions are cisc-spillable\n");363printf(" D define preprocessor symbol\n");364printf(" U undefine preprocessor symbol\n");365printf(" c specify CPP file name (default: %s)\n", AD._CPP_file._name);366printf(" h specify HPP file name (default: %s)\n", AD._HPP_file._name);367printf(" a specify DFA output file name\n");368printf(" v specify adGlobals output file name\n");369printf(" # increment ADL debug level\n");370printf("\n");371}372373//------------------------------open_file------------------------------------374int ArchDesc::open_file(bool required, ADLFILE & ADF, const char *action)375{376if (required &&377(ADF._fp = fopen(ADF._name, action)) == NULL) {378printf("ERROR: Cannot open file for %s: %s\n", action, ADF._name);379close_files(1);380return 0;381}382return 1;383}384385//------------------------------open_files-------------------------------------386int ArchDesc::open_files(void)387{388if (_ADL_file._name == NULL)389{ printf("ERROR: No ADL input file specified\n"); return 0; }390391if (!open_file(true , _ADL_file, "r")) { return 0; }392if (!open_file(!_no_output, _DFA_file, "w")) { return 0; }393if (!open_file(!_no_output, _HPP_file, "w")) { return 0; }394if (!open_file(!_no_output, _CPP_file, "w")) { return 0; }395if (!open_file(!_no_output, _CPP_CLONE_file, "w")) { return 0; }396if (!open_file(!_no_output, _CPP_EXPAND_file, "w")) { return 0; }397if (!open_file(!_no_output, _CPP_FORMAT_file, "w")) { return 0; }398if (!open_file(!_no_output, _CPP_GEN_file, "w")) { return 0; }399if (!open_file(!_no_output, _CPP_MISC_file, "w")) { return 0; }400if (!open_file(!_no_output, _CPP_PEEPHOLE_file, "w")) { return 0; }401if (!open_file(!_no_output, _CPP_PIPELINE_file, "w")) { return 0; }402if (!open_file(!_no_output, _VM_file , "w")) { return 0; }403if (!open_file(_dfa_debug != 0, _bug_file, "w")) { return 0; }404405return 1;406}407408//------------------------------close_file------------------------------------409void ArchDesc::close_file(int delete_out, ADLFILE& ADF)410{411if (ADF._fp) {412fclose(ADF._fp);413if (delete_out) remove(ADF._name);414}415}416417//------------------------------close_files------------------------------------418void ArchDesc::close_files(int delete_out)419{420if (_ADL_file._fp) fclose(_ADL_file._fp);421422close_file(delete_out, _CPP_file);423close_file(delete_out, _CPP_CLONE_file);424close_file(delete_out, _CPP_EXPAND_file);425close_file(delete_out, _CPP_FORMAT_file);426close_file(delete_out, _CPP_GEN_file);427close_file(delete_out, _CPP_MISC_file);428close_file(delete_out, _CPP_PEEPHOLE_file);429close_file(delete_out, _CPP_PIPELINE_file);430close_file(delete_out, _HPP_file);431close_file(delete_out, _DFA_file);432close_file(delete_out, _bug_file);433434if (!_quiet_mode) {435printf("\n");436if (_no_output || delete_out) {437if (_ADL_file._name) printf("%s: ", _ADL_file._name);438printf("No output produced");439}440else {441if (_ADL_file._name) printf("%s --> ", _ADL_file._name);442printf("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s",443_CPP_file._name,444_CPP_CLONE_file._name,445_CPP_EXPAND_file._name,446_CPP_FORMAT_file._name,447_CPP_GEN_file._name,448_CPP_MISC_file._name,449_CPP_PEEPHOLE_file._name,450_CPP_PIPELINE_file._name,451_HPP_file._name,452_DFA_file._name);453}454printf("\n");455}456}457458//------------------------------strip_ext--------------------------------------459static char *strip_ext(char *fname)460{461char *ep;462463if (fname) {464ep = fname + strlen(fname) - 1; // start at last character and look for '.'465while (ep >= fname && *ep != '.') --ep;466if (*ep == '.') *ep = '\0'; // truncate string at '.'467}468return fname;469}470471//------------------------------base_plus_suffix-------------------------------472// New concatenated string473static char *base_plus_suffix(const char* base, const char *suffix)474{475int len = (int)strlen(base) + (int)strlen(suffix) + 1;476477char* fname = new char[len];478sprintf(fname,"%s%s",base,suffix);479return fname;480}481482//------------------------------get_legal_text---------------------------------483// Get pointer to legal text at the beginning of AD file.484// This code assumes that a legal text starts at the beginning of .ad files,485// is commented by "//" at each line and ends with empty line.486//487int get_legal_text(FileBuff &fbuf, char **legal_text)488{489char* legal_start = fbuf.get_line();490assert(legal_start[0] == '/' && legal_start[1] == '/', "Incorrect header of AD file");491char* legal_end = fbuf.get_line();492assert(strncmp(legal_end, "// Copyright", 12) == 0, "Incorrect header of AD file");493while(legal_end[0] == '/' && legal_end[1] == '/') {494legal_end = fbuf.get_line();495}496*legal_text = legal_start;497return (int) (legal_end - legal_start);498}499500// VS2005 has its own definition, identical to this one.501#if !defined(_WIN32) || defined(_WIN64) || _MSC_VER < 1400502void *operator new( size_t size, int, const char *, int ) throw() {503return ::operator new( size );504}505#endif506507508