Path: blob/master/src/hotspot/share/adlc/main.cpp
41145 views
/*1* Copyright (c) 1997, 2021, 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 == '/' || *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 arguments60char *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, "code/codeCache.hpp");214AD.addInclude(AD._CPP_file, "code/compiledIC.hpp");215AD.addInclude(AD._CPP_file, "code/nativeInst.hpp");216AD.addInclude(AD._CPP_file, "code/vmreg.inline.hpp");217AD.addInclude(AD._CPP_file, "gc/shared/collectedHeap.inline.hpp");218AD.addInclude(AD._CPP_file, "oops/compiledICHolder.hpp");219AD.addInclude(AD._CPP_file, "oops/compressedOops.hpp");220AD.addInclude(AD._CPP_file, "oops/markWord.hpp");221AD.addInclude(AD._CPP_file, "oops/method.hpp");222AD.addInclude(AD._CPP_file, "oops/oop.inline.hpp");223AD.addInclude(AD._CPP_file, "opto/c2_MacroAssembler.hpp");224AD.addInclude(AD._CPP_file, "opto/cfgnode.hpp");225AD.addInclude(AD._CPP_file, "opto/intrinsicnode.hpp");226AD.addInclude(AD._CPP_file, "opto/locknode.hpp");227AD.addInclude(AD._CPP_file, "opto/opcodes.hpp");228AD.addInclude(AD._CPP_file, "opto/regalloc.hpp");229AD.addInclude(AD._CPP_file, "opto/regmask.hpp");230AD.addInclude(AD._CPP_file, "opto/runtime.hpp");231AD.addInclude(AD._CPP_file, "runtime/biasedLocking.hpp");232AD.addInclude(AD._CPP_file, "runtime/safepointMechanism.hpp");233AD.addInclude(AD._CPP_file, "runtime/sharedRuntime.hpp");234AD.addInclude(AD._CPP_file, "runtime/stubRoutines.hpp");235AD.addInclude(AD._CPP_file, "utilities/growableArray.hpp");236AD.addInclude(AD._CPP_file, "utilities/powerOfTwo.hpp");237AD.addInclude(AD._HPP_file, "memory/allocation.hpp");238AD.addInclude(AD._HPP_file, "oops/compressedOops.hpp");239AD.addInclude(AD._HPP_file, "code/nativeInst.hpp");240AD.addInclude(AD._HPP_file, "opto/output.hpp");241AD.addInclude(AD._HPP_file, "opto/machnode.hpp");242AD.addInclude(AD._HPP_file, "opto/node.hpp");243AD.addInclude(AD._HPP_file, "opto/regalloc.hpp");244AD.addInclude(AD._HPP_file, "opto/subnode.hpp");245AD.addInclude(AD._HPP_file, "opto/vectornode.hpp");246AD.addInclude(AD._CPP_CLONE_file, "precompiled.hpp");247AD.addInclude(AD._CPP_CLONE_file, "adfiles", get_basename(AD._HPP_file._name));248AD.addInclude(AD._CPP_EXPAND_file, "precompiled.hpp");249AD.addInclude(AD._CPP_EXPAND_file, "adfiles", get_basename(AD._HPP_file._name));250AD.addInclude(AD._CPP_EXPAND_file, "oops/compressedOops.hpp");251AD.addInclude(AD._CPP_FORMAT_file, "precompiled.hpp");252AD.addInclude(AD._CPP_FORMAT_file, "adfiles", get_basename(AD._HPP_file._name));253AD.addInclude(AD._CPP_FORMAT_file, "compiler/oopMap.hpp");254AD.addInclude(AD._CPP_GEN_file, "precompiled.hpp");255AD.addInclude(AD._CPP_GEN_file, "adfiles", get_basename(AD._HPP_file._name));256AD.addInclude(AD._CPP_GEN_file, "opto/cfgnode.hpp");257AD.addInclude(AD._CPP_GEN_file, "opto/locknode.hpp");258AD.addInclude(AD._CPP_GEN_file, "opto/rootnode.hpp");259AD.addInclude(AD._CPP_MISC_file, "precompiled.hpp");260AD.addInclude(AD._CPP_MISC_file, "adfiles", get_basename(AD._HPP_file._name));261AD.addInclude(AD._CPP_PEEPHOLE_file, "precompiled.hpp");262AD.addInclude(AD._CPP_PEEPHOLE_file, "adfiles", get_basename(AD._HPP_file._name));263AD.addInclude(AD._CPP_PIPELINE_file, "precompiled.hpp");264AD.addInclude(AD._CPP_PIPELINE_file, "adfiles", get_basename(AD._HPP_file._name));265AD.addInclude(AD._DFA_file, "precompiled.hpp");266AD.addInclude(AD._DFA_file, "adfiles", get_basename(AD._HPP_file._name));267AD.addInclude(AD._DFA_file, "oops/compressedOops.hpp");268AD.addInclude(AD._DFA_file, "opto/cfgnode.hpp"); // Use PROB_MAX in predicate.269AD.addInclude(AD._DFA_file, "opto/intrinsicnode.hpp");270AD.addInclude(AD._DFA_file, "opto/matcher.hpp");271AD.addInclude(AD._DFA_file, "opto/narrowptrnode.hpp");272AD.addInclude(AD._DFA_file, "opto/opcodes.hpp");273AD.addInclude(AD._DFA_file, "opto/convertnode.hpp");274AD.addInclude(AD._DFA_file, "utilities/powerOfTwo.hpp");275276// Make sure each .cpp file starts with include lines:277// files declaring and defining generators for Mach* Objects (hpp,cpp)278// Generate the result files:279// enumerations, class definitions, object generators, and the DFA280// file containing enumeration of machine operands & instructions (hpp)281AD.addPreHeaderBlocks(AD._HPP_file._fp); // .hpp282AD.buildMachOperEnum(AD._HPP_file._fp); // .hpp283AD.buildMachOpcodesEnum(AD._HPP_file._fp); // .hpp284AD.buildMachRegisterNumbers(AD._VM_file._fp); // VM file285AD.buildMachRegisterEncodes(AD._HPP_file._fp); // .hpp file286AD.declareRegSizes(AD._HPP_file._fp); // .hpp287AD.build_pipeline_enums(AD._HPP_file._fp); // .hpp288// output definition of class "State"289AD.defineStateClass(AD._HPP_file._fp); // .hpp290// file declaring the Mach* classes derived from MachOper and MachNode291AD.declareClasses(AD._HPP_file._fp);292// declare and define maps: in the .hpp and .cpp files respectively293AD.addSourceBlocks(AD._CPP_file._fp); // .cpp294AD.addHeaderBlocks(AD._HPP_file._fp); // .hpp295AD.buildReduceMaps(AD._HPP_file._fp, AD._CPP_file._fp);296AD.buildMustCloneMap(AD._HPP_file._fp, AD._CPP_file._fp);297// build CISC_spilling oracle and MachNode::cisc_spill() methods298AD.build_cisc_spill_instructions(AD._HPP_file._fp, AD._CPP_file._fp);299// define methods for machine dependent State, MachOper, and MachNode classes300AD.defineClasses(AD._CPP_file._fp);301AD.buildMachOperGenerator(AD._CPP_GEN_file._fp);// .cpp302AD.buildMachNodeGenerator(AD._CPP_GEN_file._fp);// .cpp303// define methods for machine dependent instruction matching304AD.buildInstructMatchCheck(AD._CPP_file._fp); // .cpp305// define methods for machine dependent frame management306AD.buildFrameMethods(AD._CPP_file._fp); // .cpp307AD.generate_needs_deep_clone_jvms(AD._CPP_file._fp);308309// do this last:310AD.addPreprocessorChecks(AD._CPP_file._fp); // .cpp311AD.addPreprocessorChecks(AD._CPP_CLONE_file._fp); // .cpp312AD.addPreprocessorChecks(AD._CPP_EXPAND_file._fp); // .cpp313AD.addPreprocessorChecks(AD._CPP_FORMAT_file._fp); // .cpp314AD.addPreprocessorChecks(AD._CPP_GEN_file._fp); // .cpp315AD.addPreprocessorChecks(AD._CPP_MISC_file._fp); // .cpp316AD.addPreprocessorChecks(AD._CPP_PEEPHOLE_file._fp); // .cpp317AD.addPreprocessorChecks(AD._CPP_PIPELINE_file._fp); // .cpp318319// define the finite automata that selects lowest cost production320AD.buildDFA(AD._DFA_file._fp);321// Add include guards for all .hpp files322AD.addIncludeGuardEnd(AD._HPP_file, "GENERATED_ADFILES_AD_HPP"); // .hpp323AD.addIncludeGuardEnd(AD._VM_file, "GENERATED_ADFILES_ADGLOBALS_HPP"); // .hpp324325AD.close_files(0); // Close all input/output files326327// Final printout and statistics328// cout << program;329330if( AD._dfa_debug & 2 ) { // For higher debug settings, print timing info331// Timer t_stop;332// Timer t_total = t_stop - t_start; // Total running time333// cerr << "\n---Architecture Description Totals---\n";334// cerr << ", Total lines: " << TotalLines;335// float l = TotalLines;336// cerr << "\nTotal Compilation Time: " << t_total << "\n";337// float ft = (float)t_total;338// if( ft > 0.0 ) fprintf(stderr,"Lines/sec: %#5.2f\n", l/ft);339}340return (AD._syntax_errs + AD._semantic_errs + AD._internal_errs); // Bye Bye!!341}342343//------------------------------usage------------------------------------------344static void usage(ArchDesc& AD)345{346printf("Architecture Description Language Compiler\n\n");347printf("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");348printf(" d produce DFA debugging info\n");349printf(" o no output produced, syntax and semantic checking only\n");350printf(" q quiet mode, supresses all non-essential messages\n");351printf(" w suppress warning messages\n");352printf(" T make DFA as many subroutine calls\n");353printf(" s output which instructions are cisc-spillable\n");354printf(" D define preprocessor symbol\n");355printf(" U undefine preprocessor symbol\n");356printf(" c specify CPP file name (default: %s)\n", AD._CPP_file._name);357printf(" h specify HPP file name (default: %s)\n", AD._HPP_file._name);358printf(" a specify DFA output file name\n");359printf(" v specify adGlobals output file name\n");360printf(" # increment ADL debug level\n");361printf("\n");362}363364//------------------------------open_file------------------------------------365int ArchDesc::open_file(bool required, ADLFILE & ADF, const char *action)366{367if (required &&368(ADF._fp = fopen(ADF._name, action)) == NULL) {369printf("ERROR: Cannot open file for %s: %s\n", action, ADF._name);370close_files(1);371return 0;372}373return 1;374}375376//------------------------------open_files-------------------------------------377int ArchDesc::open_files(void)378{379if (_ADL_file._name == NULL)380{ printf("ERROR: No ADL input file specified\n"); return 0; }381382if (!open_file(true , _ADL_file, "r")) { return 0; }383if (!open_file(!_no_output, _DFA_file, "w")) { return 0; }384if (!open_file(!_no_output, _HPP_file, "w")) { return 0; }385if (!open_file(!_no_output, _CPP_file, "w")) { return 0; }386if (!open_file(!_no_output, _CPP_CLONE_file, "w")) { return 0; }387if (!open_file(!_no_output, _CPP_EXPAND_file, "w")) { return 0; }388if (!open_file(!_no_output, _CPP_FORMAT_file, "w")) { return 0; }389if (!open_file(!_no_output, _CPP_GEN_file, "w")) { return 0; }390if (!open_file(!_no_output, _CPP_MISC_file, "w")) { return 0; }391if (!open_file(!_no_output, _CPP_PEEPHOLE_file, "w")) { return 0; }392if (!open_file(!_no_output, _CPP_PIPELINE_file, "w")) { return 0; }393if (!open_file(!_no_output, _VM_file , "w")) { return 0; }394if (!open_file(_dfa_debug != 0, _bug_file, "w")) { return 0; }395396return 1;397}398399//------------------------------close_file------------------------------------400void ArchDesc::close_file(int delete_out, ADLFILE& ADF)401{402if (ADF._fp) {403fclose(ADF._fp);404if (delete_out) remove(ADF._name);405}406}407408//------------------------------close_files------------------------------------409void ArchDesc::close_files(int delete_out)410{411if (_ADL_file._fp) fclose(_ADL_file._fp);412413close_file(delete_out, _CPP_file);414close_file(delete_out, _CPP_CLONE_file);415close_file(delete_out, _CPP_EXPAND_file);416close_file(delete_out, _CPP_FORMAT_file);417close_file(delete_out, _CPP_GEN_file);418close_file(delete_out, _CPP_MISC_file);419close_file(delete_out, _CPP_PEEPHOLE_file);420close_file(delete_out, _CPP_PIPELINE_file);421close_file(delete_out, _HPP_file);422close_file(delete_out, _DFA_file);423close_file(delete_out, _bug_file);424425if (!_quiet_mode) {426printf("\n");427if (_no_output || delete_out) {428if (_ADL_file._name) printf("%s: ", _ADL_file._name);429printf("No output produced");430}431else {432if (_ADL_file._name) printf("%s --> ", _ADL_file._name);433printf("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s",434_CPP_file._name,435_CPP_CLONE_file._name,436_CPP_EXPAND_file._name,437_CPP_FORMAT_file._name,438_CPP_GEN_file._name,439_CPP_MISC_file._name,440_CPP_PEEPHOLE_file._name,441_CPP_PIPELINE_file._name,442_HPP_file._name,443_DFA_file._name);444}445printf("\n");446}447}448449//------------------------------strip_ext--------------------------------------450static char *strip_ext(char *fname)451{452char *ep;453454if (fname) {455ep = fname + strlen(fname) - 1; // start at last character and look for '.'456while (ep >= fname && *ep != '.') --ep;457if (*ep == '.') *ep = '\0'; // truncate string at '.'458}459return fname;460}461462//------------------------------base_plus_suffix-------------------------------463// New concatenated string464static char *base_plus_suffix(const char* base, const char *suffix)465{466int len = (int)strlen(base) + (int)strlen(suffix) + 1;467468char* fname = new char[len];469sprintf(fname,"%s%s",base,suffix);470return fname;471}472473//------------------------------get_legal_text---------------------------------474// Get pointer to legal text at the beginning of AD file.475// This code assumes that a legal text starts at the beginning of .ad files,476// is commented by "//" at each line and ends with empty line.477//478int get_legal_text(FileBuff &fbuf, char **legal_text)479{480char* legal_start = fbuf.get_line();481assert(legal_start[0] == '/' && legal_start[1] == '/', "Incorrect header of AD file");482char* legal_end = fbuf.get_line();483assert(strncmp(legal_end, "// Copyright", 12) == 0, "Incorrect header of AD file");484while(legal_end[0] == '/' && legal_end[1] == '/') {485legal_end = fbuf.get_line();486}487*legal_text = legal_start;488return (int) (legal_end - legal_start);489}490491// VS2005 has its own definition, identical to this one.492#if !defined(_WIN32) || defined(_WIN64) || _MSC_VER < 1400493void *operator new( size_t size, int, const char *, int ) throw() {494return ::operator new( size );495}496#endif497498499