duke@435: /* duke@435: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Definitions for Error Flags duke@435: #define WARN 0 duke@435: #define SYNERR 1 duke@435: #define SEMERR 2 duke@435: #define INTERNAL_ERR 3 duke@435: duke@435: // Minimal declarations for include files duke@435: class OutputMap; duke@435: class ProductionState; duke@435: class Expr; duke@435: duke@435: // STRUCTURE FOR HANDLING INPUT AND OUTPUT FILES duke@435: typedef BufferedFile ADLFILE; duke@435: duke@435: //---------------------------ChainList----------------------------------------- duke@435: class ChainList { duke@435: NameList _name; duke@435: NameList _cost; duke@435: NameList _rule; duke@435: duke@435: public: duke@435: void insert(const char *name, const char *cost, const char *rule); duke@435: bool search(const char *name); duke@435: duke@435: void reset(); duke@435: bool iter(const char * &name, const char * &cost, const char * &rule); duke@435: duke@435: void dump(); duke@435: void output(FILE *fp); duke@435: duke@435: ChainList(); duke@435: ~ChainList(); duke@435: }; duke@435: duke@435: //---------------------------MatchList----------------------------------------- duke@435: class MatchList { duke@435: private: duke@435: MatchList *_next; duke@435: Predicate *_pred; // Predicate which applies to this match rule duke@435: const char *_cost; duke@435: duke@435: public: duke@435: const char *_opcode; duke@435: const char *_resultStr; duke@435: const char *_lchild; duke@435: const char *_rchild; duke@435: duke@435: MatchList(MatchList *nxt, Predicate *prd): _next(nxt), _pred(prd), _cost(NULL){ duke@435: _resultStr = _lchild = _rchild = _opcode = NULL; } duke@435: duke@435: MatchList(MatchList *nxt, Predicate *prd, const char *cost, duke@435: const char *opcode, const char *resultStr, const char *lchild, duke@435: const char *rchild) duke@435: : _next(nxt), _pred(prd), _cost(cost), _opcode(opcode), duke@435: _resultStr(resultStr), _lchild(lchild), _rchild(rchild) { } duke@435: duke@435: MatchList *get_next(void) { return _next; } duke@435: char *get_pred(void) { return (_pred?_pred->_pred:NULL); } duke@435: Predicate *get_pred_obj(void) { return _pred; } duke@435: const char *get_cost(void) { return _cost == NULL ? "0" :_cost; } duke@435: bool search(const char *opc, const char *res, const char *lch, duke@435: const char *rch, Predicate *pr); duke@435: duke@435: void dump(); duke@435: void output(FILE *fp); duke@435: }; duke@435: duke@435: //---------------------------ArchDesc------------------------------------------ duke@435: class ArchDesc { duke@435: private: duke@435: FormDict _globalNames; // Global names duke@435: Dict _idealIndex; // Map ideal names to index in enumeration duke@435: ExprDict _globalDefs; // Global definitions, #defines duke@435: int _internalOpCounter; // Internal Operand Counter duke@435: duke@435: FormList _header; // List of Source Code Forms for hpp file duke@435: FormList _pre_header; // ditto for the very top of the hpp file duke@435: FormList _source; // List of Source Code Forms for output duke@435: FormList _instructions; // List of Instruction Forms for output duke@435: FormList _machnodes; // List of Node Classes (special for pipelining) duke@435: FormList _operands; // List of Operand Forms for output duke@435: FormList _opclass; // List of Operand Class Forms for output duke@435: FormList _attributes; // List of Attribute Forms for parsing duke@435: RegisterForm *_register; // Only one Register Form allowed duke@435: FrameForm *_frame; // Describe stack-frame layout duke@435: EncodeForm *_encode; // Only one Encode Form allowed duke@435: PipelineForm *_pipeline; // Pipeline Form for output duke@435: duke@435: bool _has_match_rule[_last_opcode]; // found AD rule for ideal node in .ad duke@435: duke@435: MatchList *_mlistab[_last_opcode]; // Array of MatchLists duke@435: duke@435: // The Architecture Description identifies which user-defined operand can be used duke@435: // to access [stack_pointer + offset] duke@435: OperandForm *_cisc_spill_operand; duke@435: duke@435: // Methods for outputting the DFA duke@435: void gen_match(FILE *fp, MatchList &mlist, ProductionState &status, Dict &operands_chained_from); duke@435: void chain_rule(FILE *fp, const char *indent, const char *ideal, duke@435: const Expr *icost, const char *irule, duke@435: Dict &operands_chained_from, ProductionState &status); duke@435: void chain_rule_c(FILE *fp, char *indent, char *ideal, char *irule); // %%%%% TODO: remove this duke@435: void expand_opclass(FILE *fp, const char *indent, const Expr *cost, duke@435: const char *result_type, ProductionState &status); duke@435: Expr *calc_cost(FILE *fp, const char *spaces, MatchList &mList, ProductionState &status); duke@435: void prune_matchlist(Dict &minimize, MatchList &mlist); duke@435: duke@435: // Helper function that outputs code to generate an instruction in MachNodeGenerator duke@435: void buildMachNode(FILE *fp_cpp, InstructForm *inst, const char *indent); duke@435: duke@435: public: duke@435: ArchDesc(); duke@435: ~ArchDesc(); duke@435: duke@435: // Option flags which control miscellaneous behaviors throughout the code duke@435: int _TotalLines; // Line Counter duke@435: int _no_output; // Flag to disable output of DFA, etc. duke@435: int _quiet_mode; // Do not output banner messages, etc. duke@435: int _disable_warnings; // Do not output warning messages duke@435: int _dfa_debug; // Debug Flag for generated DFA duke@435: int _dfa_small; // Debug Flag for generated DFA duke@435: int _adl_debug; // Debug Flag for ADLC duke@435: int _adlocation_debug; // Debug Flag to use ad file locations duke@435: bool _cisc_spill_debug; // Debug Flag to see cisc-spill-instructions duke@435: bool _short_branch_debug; // Debug Flag to see short branch instructions duke@435: duke@435: // Error/Warning Counts duke@435: int _syntax_errs; // Count of syntax errors duke@435: int _semantic_errs; // Count of semantic errors duke@435: int _warnings; // Count warnings duke@435: int _internal_errs; // Count of internal errors duke@435: duke@435: // Accessor for private data. duke@435: void has_match_rule(int opc, const bool b) { _has_match_rule[opc] = b; } duke@435: duke@435: // I/O Files duke@435: ADLFILE _ADL_file; // Input Architecture Description File duke@435: // Machine dependent files, built from architecture definition duke@435: ADLFILE _DFA_file; // File for definition of Matcher::DFA duke@435: ADLFILE _HPP_file; // File for ArchNode class declarations duke@435: ADLFILE _CPP_file; // File for ArchNode class defintions duke@435: ADLFILE _CPP_CLONE_file; // File for MachNode/Oper clone defintions duke@435: ADLFILE _CPP_EXPAND_file; // File for MachNode expand methods duke@435: ADLFILE _CPP_FORMAT_file; // File for MachNode/Oper format defintions duke@435: ADLFILE _CPP_GEN_file; // File for MachNode/Oper generator methods duke@435: ADLFILE _CPP_MISC_file; // File for miscellaneous MachNode/Oper tables & methods duke@435: ADLFILE _CPP_PEEPHOLE_file; // File for MachNode peephole methods duke@435: ADLFILE _CPP_PIPELINE_file; // File for MachNode pipeline defintions duke@435: ADLFILE _VM_file; // File for constants needed in VM code duke@435: ADLFILE _bug_file; // DFA debugging file duke@435: duke@435: // I/O helper methods duke@435: int open_file(bool required, ADLFILE & adf, const char *action); duke@435: void close_file(int delete_out, ADLFILE & adf); duke@435: int open_files(void); duke@435: void close_files(int delete_out); duke@435: duke@435: Dict _chainRules; // Maps user operand names to ChainRules duke@435: Dict _internalOps; // Maps match strings to internal operand names duke@435: NameList _internalOpNames; // List internal operand names duke@435: Dict _internalMatch; // Map internal name to its MatchNode duke@435: duke@435: NameList _preproc_list; // Preprocessor flag names duke@435: FormDict _preproc_table;// Preprocessor flag bindings duke@435: char* get_preproc_def(const char* flag); duke@435: void set_preproc_def(const char* flag, const char* def); duke@435: duke@435: FormDict& globalNames() {return _globalNames;} // map global names to forms duke@435: void initKeywords(FormDict& globals); // Add keywords to global name table duke@435: duke@435: ExprDict& globalDefs() {return _globalDefs;} // map global names to expressions duke@435: duke@435: OperandForm *constructOperand(const char *ident, bool ideal_only); duke@435: void initBaseOpTypes(); // Import predefined base types. duke@435: duke@435: void addForm(PreHeaderForm *ptr); // Add objects to pre-header list duke@435: void addForm(HeaderForm *ptr); // Add objects to header list duke@435: void addForm(SourceForm *ptr); // Add objects to source list duke@435: void addForm(EncodeForm *ptr); // Add objects to encode list duke@435: void addForm(InstructForm *ptr); // Add objects to the instruct list duke@435: void addForm(OperandForm *ptr); // Add objects to the operand list duke@435: void addForm(OpClassForm *ptr); // Add objects to the opclasss list duke@435: void addForm(AttributeForm *ptr); // Add objects to the attributes list duke@435: void addForm(RegisterForm *ptr); // Add objects to the register list duke@435: void addForm(FrameForm *ptr); // Add objects to the frame list duke@435: void addForm(PipelineForm *ptr); // Add objects to the pipeline list duke@435: void addForm(MachNodeForm *ptr); // Add objects to the machnode list duke@435: duke@435: int operandFormCount(); // Count number of OperandForms defined duke@435: int opclassFormCount(); // Count number of OpClassForms defined duke@435: int instructFormCount(); // Count number of InstructForms defined duke@435: duke@435: inline void getForm(EncodeForm **ptr) { *ptr = _encode; } duke@435: duke@435: bool verify(); duke@435: void dump(); duke@435: duke@435: // Helper utility that gets MatchList components from inside MatchRule duke@435: void check_optype(MatchRule *mrule); duke@435: void build_chain_rule(OperandForm *oper); duke@435: void add_chain_rule_entry(const char *src, const char *cost, duke@435: const char *result); duke@435: const char *getMatchListIndex(MatchRule &mrule); duke@435: void generateMatchLists(); // Build MatchList array and populate it duke@435: void inspectOperands(); // Build MatchLists for all operands duke@435: void inspectOpClasses(); // Build MatchLists for all operands duke@435: void inspectInstructions(); // Build MatchLists for all operands duke@435: void buildDFA(FILE *fp); // Driver for constructing the DFA duke@435: void gen_dfa_state_body(FILE *fp, Dict &minmize, ProductionState &status, Dict &chained, int i); // Driver for constructing the DFA state bodies duke@435: duke@435: // Helper utilities to generate reduction maps for internal operands duke@435: const char *reduceLeft (char *internalName); duke@435: const char *reduceRight(char *internalName); duke@435: duke@435: // Build enumerations, (1) dense operand index, (2) operands and opcodes duke@435: const char *machOperEnum(const char *opName); // create dense index names using static function duke@435: static const char *getMachOperEnum(const char *opName);// create dense index name duke@435: void buildMachOperEnum(FILE *fp_hpp);// dense enumeration for operands duke@435: void buildMachOpcodesEnum(FILE *fp_hpp);// enumeration for MachOpers & MachNodes duke@435: duke@435: // Helper utilities to generate Register Masks duke@435: RegisterForm *get_registers() { return _register; } duke@435: const char *reg_mask(OperandForm &opForm); duke@435: const char *reg_mask(InstructForm &instForm); duke@435: const char *reg_class_to_reg_mask(const char *reg_class); duke@435: char *stack_or_reg_mask(OperandForm &opForm); // name of cisc_spillable version duke@435: // This register class should also generate a stack_or_reg_mask duke@435: void set_stack_or_reg(const char *reg_class_name); // for cisc-spillable reg classes duke@435: // Generate an enumeration of register mask names and the RegMask objects. duke@435: void declare_register_masks(FILE *fp_cpp); duke@435: void build_register_masks(FILE *fp_cpp); duke@435: // Generate enumeration of machine register numbers duke@435: void buildMachRegisterNumbers(FILE *fp_hpp); duke@435: // Generate enumeration of machine register encodings duke@435: void buildMachRegisterEncodes(FILE *fp_hpp); duke@435: // Generate Regsiter Size Array duke@435: void declareRegSizes(FILE *fp_hpp); duke@435: // Generate Pipeline Class information duke@435: void declare_pipe_classes(FILE *fp_hpp); duke@435: // Generate Pipeline definitions duke@435: void build_pipeline_enums(FILE *fp_cpp); duke@435: // Generate Pipeline Class information duke@435: void build_pipe_classes(FILE *fp_cpp); duke@435: duke@435: // Declare and define mappings from rules to result and input types duke@435: void build_map(OutputMap &map); duke@435: void buildReduceMaps(FILE *fp_hpp, FILE *fp_cpp); duke@435: // build flags for signaling that our machine needs this instruction cloned duke@435: void buildMustCloneMap(FILE *fp_hpp, FILE *fp_cpp); duke@435: duke@435: // output SUN copyright info duke@435: void addSunCopyright(char* legal, int size, FILE *fp); duke@435: // output #include declarations for machine specific files duke@435: void machineDependentIncludes(ADLFILE &adlfile); duke@435: // Output C preprocessor code to verify the backend compilation environment. duke@435: void addPreprocessorChecks(FILE *fp); duke@435: // Output C source and header (source_hpp) blocks. duke@435: void addPreHeaderBlocks(FILE *fp_hpp); duke@435: void addHeaderBlocks(FILE *fp_hpp); duke@435: void addSourceBlocks(FILE *fp_cpp); duke@435: void generate_adlc_verification(FILE *fp_cpp); duke@435: duke@435: // output declaration of class State duke@435: void defineStateClass(FILE *fp); duke@435: duke@435: // Generator for MachOper objects given integer type duke@435: void buildMachOperGenerator(FILE *fp_cpp); duke@435: // Generator for MachNode objects given integer type duke@435: void buildMachNodeGenerator(FILE *fp_cpp); duke@435: duke@435: // Generator for Expand methods for instructions with expand rules duke@435: void defineExpand(FILE *fp, InstructForm *node); duke@435: // Generator for Peephole methods for instructions with peephole rules duke@435: void definePeephole(FILE *fp, InstructForm *node); duke@435: // Generator for Size methods for instructions duke@435: void defineSize(FILE *fp, InstructForm &node); duke@435: // Generator for Emit methods for instructions duke@435: void defineEmit(FILE *fp, InstructForm &node); duke@435: // Define a MachOper encode method duke@435: void define_oper_interface(FILE *fp, OperandForm &oper, FormDict &globals, duke@435: const char *name, const char *encoding); duke@435: duke@435: // Methods to construct the MachNode class hierarchy duke@435: // Return the type signature for the ideal operation duke@435: const char *getIdealType(const char *idealOp); duke@435: // Declare and define the classes derived from MachOper and MachNode duke@435: void declareClasses(FILE *fp_hpp); duke@435: void defineClasses(FILE *fp_cpp); duke@435: duke@435: // Emit an ADLC message duke@435: void internal_err( const char *fmt, ...); duke@435: void syntax_err ( int lineno, const char *fmt, ...); duke@435: int emit_msg(int quiet, int flag, int linenum, const char *fmt, duke@435: va_list args); duke@435: duke@435: // Generator for has_match_rule methods duke@435: void buildInstructMatchCheck(FILE *fp_cpp) const; duke@435: duke@435: // Generator for Frame Methods duke@435: void buildFrameMethods(FILE *fp_cpp); duke@435: duke@435: // Generate CISC_spilling oracle and MachNode::cisc_spill() methods duke@435: void build_cisc_spill_instructions(FILE *fp_hpp, FILE *fp_cpp); duke@435: void identify_cisc_spill_instructions(); duke@435: void identify_short_branches(); duke@435: void identify_unique_operands(); duke@435: void set_cisc_spill_operand(OperandForm *opForm) { _cisc_spill_operand = opForm; } duke@435: OperandForm *cisc_spill_operand() { return _cisc_spill_operand; } duke@435: bool can_cisc_spill() { return _cisc_spill_operand != NULL; } duke@435: duke@435: duke@435: protected: duke@435: // build MatchList from MatchRule duke@435: void buildMatchList(MatchRule *mrule, const char *resultStr, duke@435: const char *rootOp, Predicate *pred, const char *cost); duke@435: duke@435: void buildMList(MatchNode *node, const char *rootOp, const char *resultOp, duke@435: Predicate *pred, const char *cost); duke@435: duke@435: friend class ADLParser; duke@435: duke@435: }; duke@435: duke@435: duke@435: // -------------------------------- maps ------------------------------------ duke@435: duke@435: // Base class for generating a mapping from rule number to value. duke@435: // Used with ArchDesc::build_map() for all maps except "enum MachOperands" duke@435: // A derived class defines the appropriate output for a specific mapping. duke@435: class OutputMap { duke@435: protected: duke@435: FILE *_hpp; duke@435: FILE *_cpp; duke@435: FormDict &_globals; duke@435: ArchDesc &_AD; duke@435: public: duke@435: OutputMap (FILE *decl_file, FILE *def_file, FormDict &globals, ArchDesc &AD) duke@435: : _hpp(decl_file), _cpp(def_file), _globals(globals), _AD(AD) {}; duke@435: // Access files used by this routine duke@435: FILE *decl_file() { return _hpp; } duke@435: FILE *def_file() { return _cpp; } duke@435: // Positions in iteration that derived class will be told about duke@435: enum position { BEGIN_OPERANDS, duke@435: BEGIN_OPCLASSES, duke@435: BEGIN_INTERNALS, duke@435: BEGIN_INSTRUCTIONS, duke@435: BEGIN_INST_CHAIN_RULES, duke@435: END_INST_CHAIN_RULES, duke@435: BEGIN_REMATERIALIZE, duke@435: END_REMATERIALIZE, duke@435: END_INSTRUCTIONS duke@435: }; duke@435: // Output routines specific to the derived class duke@435: virtual void declaration() {} duke@435: virtual void definition() {} duke@435: virtual void closing() { fprintf(_cpp, "};\n"); } duke@435: virtual void map(OperandForm &oper) { } duke@435: virtual void map(OpClassForm &opc) { } duke@435: virtual void map(char *internal_name) { } duke@435: // Allow enum-MachOperands to turn-off instructions duke@435: virtual bool do_instructions() { return true; } duke@435: virtual void map(InstructForm &inst) { } duke@435: // Allow derived class to output name and position specific info duke@435: virtual void record_position(OutputMap::position place, int index) {} duke@435: };