1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 // Definitions for Error Flags 26 #define WARN 0 27 #define SYNERR 1 28 #define SEMERR 2 29 #define INTERNAL_ERR 3 30 31 // Minimal declarations for include files 32 class OutputMap; 33 class ProductionState; 34 class Expr; 35 36 // STRUCTURE FOR HANDLING INPUT AND OUTPUT FILES 37 typedef BufferedFile ADLFILE; 38 39 //---------------------------ChainList----------------------------------------- 40 class ChainList { 41 NameList _name; 42 NameList _cost; 43 NameList _rule; 44 45 public: 46 void insert(const char *name, const char *cost, const char *rule); 47 bool search(const char *name); 48 49 void reset(); 50 bool iter(const char * &name, const char * &cost, const char * &rule); 51 52 void dump(); 53 void output(FILE *fp); 54 55 ChainList(); 56 ~ChainList(); 57 }; 58 59 //---------------------------MatchList----------------------------------------- 60 class MatchList { 61 private: 62 MatchList *_next; 63 Predicate *_pred; // Predicate which applies to this match rule 64 const char *_cost; 65 66 public: 67 const char *_opcode; 68 const char *_resultStr; 69 const char *_lchild; 70 const char *_rchild; 71 72 MatchList(MatchList *nxt, Predicate *prd): _next(nxt), _pred(prd), _cost(NULL){ 73 _resultStr = _lchild = _rchild = _opcode = NULL; } 74 75 MatchList(MatchList *nxt, Predicate *prd, const char *cost, 76 const char *opcode, const char *resultStr, const char *lchild, 77 const char *rchild) 78 : _next(nxt), _pred(prd), _cost(cost), _opcode(opcode), 79 _resultStr(resultStr), _lchild(lchild), _rchild(rchild) { } 80 81 MatchList *get_next(void) { return _next; } 82 char *get_pred(void) { return (_pred?_pred->_pred:NULL); } 83 Predicate *get_pred_obj(void) { return _pred; } 84 const char *get_cost(void) { return _cost == NULL ? "0" :_cost; } 85 bool search(const char *opc, const char *res, const char *lch, 86 const char *rch, Predicate *pr); 87 88 void dump(); 89 void output(FILE *fp); 90 }; 91 92 //---------------------------ArchDesc------------------------------------------ 93 class ArchDesc { 94 private: 95 FormDict _globalNames; // Global names 96 Dict _idealIndex; // Map ideal names to index in enumeration 97 ExprDict _globalDefs; // Global definitions, #defines 98 int _internalOpCounter; // Internal Operand Counter 99 100 FormList _header; // List of Source Code Forms for hpp file 101 FormList _pre_header; // ditto for the very top of the hpp file 102 FormList _source; // List of Source Code Forms for output 103 FormList _instructions; // List of Instruction Forms for output 104 FormList _machnodes; // List of Node Classes (special for pipelining) 105 FormList _operands; // List of Operand Forms for output 106 FormList _opclass; // List of Operand Class Forms for output 107 FormList _attributes; // List of Attribute Forms for parsing 108 RegisterForm *_register; // Only one Register Form allowed 109 FrameForm *_frame; // Describe stack-frame layout 110 EncodeForm *_encode; // Only one Encode Form allowed 111 PipelineForm *_pipeline; // Pipeline Form for output 112 113 bool _has_match_rule[_last_opcode]; // found AD rule for ideal node in <arch>.ad 114 115 MatchList *_mlistab[_last_opcode]; // Array of MatchLists 116 117 // The Architecture Description identifies which user-defined operand can be used 118 // to access [stack_pointer + offset] 119 OperandForm *_cisc_spill_operand; 120 121 // Methods for outputting the DFA 122 void gen_match(FILE *fp, MatchList &mlist, ProductionState &status, Dict &operands_chained_from); 123 void chain_rule(FILE *fp, const char *indent, const char *ideal, 124 const Expr *icost, const char *irule, 125 Dict &operands_chained_from, ProductionState &status); 126 void expand_opclass(FILE *fp, const char *indent, const Expr *cost, 127 const char *result_type, ProductionState &status); 128 Expr *calc_cost(FILE *fp, const char *spaces, MatchList &mList, ProductionState &status); 129 void prune_matchlist(Dict &minimize, MatchList &mlist); 130 131 // Helper function that outputs code to generate an instruction in MachNodeGenerator 132 void buildMachNode(FILE *fp_cpp, InstructForm *inst, const char *indent); 133 134 public: 135 ArchDesc(); 136 ~ArchDesc(); 137 138 // Option flags which control miscellaneous behaviors throughout the code 139 int _TotalLines; // Line Counter 140 int _no_output; // Flag to disable output of DFA, etc. 141 int _quiet_mode; // Do not output banner messages, etc. 142 int _disable_warnings; // Do not output warning messages 143 int _dfa_debug; // Debug Flag for generated DFA 144 int _dfa_small; // Debug Flag for generated DFA 145 int _adl_debug; // Debug Flag for ADLC 146 int _adlocation_debug; // Debug Flag to use ad file locations 147 bool _cisc_spill_debug; // Debug Flag to see cisc-spill-instructions 148 bool _short_branch_debug; // Debug Flag to see short branch instructions 149 150 // Error/Warning Counts 151 int _syntax_errs; // Count of syntax errors 152 int _semantic_errs; // Count of semantic errors 153 int _warnings; // Count warnings 154 int _internal_errs; // Count of internal errors 155 156 // Accessor for private data. 157 void has_match_rule(int opc, const bool b) { _has_match_rule[opc] = b; } 158 159 // I/O Files 160 ADLFILE _ADL_file; // Input Architecture Description File 161 // Machine dependent files, built from architecture definition 162 ADLFILE _DFA_file; // File for definition of Matcher::DFA 163 ADLFILE _HPP_file; // File for ArchNode class declarations 164 ADLFILE _CPP_file; // File for ArchNode class defintions 165 ADLFILE _CPP_CLONE_file; // File for MachNode/Oper clone defintions 166 ADLFILE _CPP_EXPAND_file; // File for MachNode expand methods 167 ADLFILE _CPP_FORMAT_file; // File for MachNode/Oper format defintions 168 ADLFILE _CPP_GEN_file; // File for MachNode/Oper generator methods 169 ADLFILE _CPP_MISC_file; // File for miscellaneous MachNode/Oper tables & methods 170 ADLFILE _CPP_PEEPHOLE_file; // File for MachNode peephole methods 171 ADLFILE _CPP_PIPELINE_file; // File for MachNode pipeline defintions 172 ADLFILE _VM_file; // File for constants needed in VM code 173 ADLFILE _bug_file; // DFA debugging file 174 175 // I/O helper methods 176 int open_file(bool required, ADLFILE & adf, const char *action); 177 void close_file(int delete_out, ADLFILE & adf); 178 int open_files(void); 179 void close_files(int delete_out); 180 181 Dict _chainRules; // Maps user operand names to ChainRules 182 Dict _internalOps; // Maps match strings to internal operand names 183 NameList _internalOpNames; // List internal operand names 184 Dict _internalMatch; // Map internal name to its MatchNode 185 186 NameList _preproc_list; // Preprocessor flag names 187 FormDict _preproc_table;// Preprocessor flag bindings 188 char* get_preproc_def(const char* flag); 189 void set_preproc_def(const char* flag, const char* def); 190 191 FormDict& globalNames() {return _globalNames;} // map global names to forms 192 void initKeywords(FormDict& globals); // Add keywords to global name table 193 194 ExprDict& globalDefs() {return _globalDefs;} // map global names to expressions 195 196 OperandForm *constructOperand(const char *ident, bool ideal_only); 197 void initBaseOpTypes(); // Import predefined base types. 198 199 void addForm(PreHeaderForm *ptr); // Add objects to pre-header list 200 void addForm(HeaderForm *ptr); // Add objects to header list 201 void addForm(SourceForm *ptr); // Add objects to source list 202 void addForm(EncodeForm *ptr); // Add objects to encode list 203 void addForm(InstructForm *ptr); // Add objects to the instruct list 204 void addForm(OperandForm *ptr); // Add objects to the operand list 205 void addForm(OpClassForm *ptr); // Add objects to the opclasss list 206 void addForm(AttributeForm *ptr); // Add objects to the attributes list 207 void addForm(RegisterForm *ptr); // Add objects to the register list 208 void addForm(FrameForm *ptr); // Add objects to the frame list 209 void addForm(PipelineForm *ptr); // Add objects to the pipeline list 210 void addForm(MachNodeForm *ptr); // Add objects to the machnode list 211 212 int operandFormCount(); // Count number of OperandForms defined 213 int opclassFormCount(); // Count number of OpClassForms defined 214 int instructFormCount(); // Count number of InstructForms defined 215 216 inline void getForm(EncodeForm **ptr) { *ptr = _encode; } 217 218 bool verify(); 219 void dump(); 220 221 // Helper utility that gets MatchList components from inside MatchRule 222 void check_optype(MatchRule *mrule); 223 void build_chain_rule(OperandForm *oper); 224 void add_chain_rule_entry(const char *src, const char *cost, 225 const char *result); 226 const char *getMatchListIndex(MatchRule &mrule); 227 void generateMatchLists(); // Build MatchList array and populate it 228 void inspectOperands(); // Build MatchLists for all operands 229 void inspectOpClasses(); // Build MatchLists for all operands 230 void inspectInstructions(); // Build MatchLists for all operands 231 void buildDFA(FILE *fp); // Driver for constructing the DFA 232 void gen_dfa_state_body(FILE *fp, Dict &minmize, ProductionState &status, Dict &chained, int i); // Driver for constructing the DFA state bodies 233 234 // Helper utilities to generate reduction maps for internal operands 235 const char *reduceLeft (char *internalName); 236 const char *reduceRight(char *internalName); 237 238 // Build enumerations, (1) dense operand index, (2) operands and opcodes 239 const char *machOperEnum(const char *opName); // create dense index names using static function 240 static const char *getMachOperEnum(const char *opName);// create dense index name 241 void buildMachOperEnum(FILE *fp_hpp);// dense enumeration for operands 242 void buildMachOpcodesEnum(FILE *fp_hpp);// enumeration for MachOpers & MachNodes 243 244 // Helper utilities to generate Register Masks 245 RegisterForm *get_registers() { return _register; } 246 const char *reg_mask(OperandForm &opForm); 247 const char *reg_mask(InstructForm &instForm); 248 const char *reg_class_to_reg_mask(const char *reg_class); 249 char *stack_or_reg_mask(OperandForm &opForm); // name of cisc_spillable version 250 // This register class should also generate a stack_or_reg_mask 251 void set_stack_or_reg(const char *reg_class_name); // for cisc-spillable reg classes 252 // Generate an enumeration of register mask names and the RegMask objects. 253 void declare_register_masks(FILE *fp_cpp); 254 void build_register_masks(FILE *fp_cpp); 255 // Generate enumeration of machine register numbers 256 void buildMachRegisterNumbers(FILE *fp_hpp); 257 // Generate enumeration of machine register encodings 258 void buildMachRegisterEncodes(FILE *fp_hpp); 259 // Generate Regsiter Size Array 260 void declareRegSizes(FILE *fp_hpp); 261 // Generate Pipeline Class information 262 void declare_pipe_classes(FILE *fp_hpp); 263 // Generate Pipeline definitions 264 void build_pipeline_enums(FILE *fp_cpp); 265 // Generate Pipeline Class information 266 void build_pipe_classes(FILE *fp_cpp); 267 268 // Declare and define mappings from rules to result and input types 269 void build_map(OutputMap &map); 270 void buildReduceMaps(FILE *fp_hpp, FILE *fp_cpp); 271 // build flags for signaling that our machine needs this instruction cloned 272 void buildMustCloneMap(FILE *fp_hpp, FILE *fp_cpp); 273 274 // output SUN copyright info 275 void addSunCopyright(char* legal, int size, FILE *fp); 276 // output #include declarations for machine specific files 277 void machineDependentIncludes(ADLFILE &adlfile); 278 // Output C preprocessor code to verify the backend compilation environment. 279 void addPreprocessorChecks(FILE *fp); 280 // Output C source and header (source_hpp) blocks. 281 void addPreHeaderBlocks(FILE *fp_hpp); 282 void addHeaderBlocks(FILE *fp_hpp); 283 void addSourceBlocks(FILE *fp_cpp); 284 void generate_adlc_verification(FILE *fp_cpp); 285 286 // output declaration of class State 287 void defineStateClass(FILE *fp); 288 289 // Generator for MachOper objects given integer type 290 void buildMachOperGenerator(FILE *fp_cpp); 291 // Generator for MachNode objects given integer type 292 void buildMachNodeGenerator(FILE *fp_cpp); 293 294 // Generator for Expand methods for instructions with expand rules 295 void defineExpand (FILE *fp, InstructForm *node); 296 // Generator for Peephole methods for instructions with peephole rules 297 void definePeephole (FILE *fp, InstructForm *node); 298 // Generator for Size methods for instructions 299 void defineSize (FILE *fp, InstructForm &node); 300 301 public: 302 // Generator for EvalConstantValue methods for instructions 303 void defineEvalConstant(FILE *fp, InstructForm &node); 304 // Generator for Emit methods for instructions 305 void defineEmit (FILE *fp, InstructForm &node); 306 307 // Define a MachOper encode method 308 void define_oper_interface(FILE *fp, OperandForm &oper, FormDict &globals, 309 const char *name, const char *encoding); 310 311 // Methods to construct the MachNode class hierarchy 312 // Return the type signature for the ideal operation 313 const char *getIdealType(const char *idealOp); 314 // Declare and define the classes derived from MachOper and MachNode 315 void declareClasses(FILE *fp_hpp); 316 void defineClasses(FILE *fp_cpp); 317 318 // Emit an ADLC message 319 void internal_err( const char *fmt, ...); 320 void syntax_err ( int lineno, const char *fmt, ...); 321 int emit_msg(int quiet, int flag, int linenum, const char *fmt, 322 va_list args); 323 324 // Generator for has_match_rule methods 325 void buildInstructMatchCheck(FILE *fp_cpp) const; 326 327 // Generator for Frame Methods 328 void buildFrameMethods(FILE *fp_cpp); 329 330 // Generate CISC_spilling oracle and MachNode::cisc_spill() methods 331 void build_cisc_spill_instructions(FILE *fp_hpp, FILE *fp_cpp); 332 void identify_cisc_spill_instructions(); 333 void identify_short_branches(); 334 void identify_unique_operands(); 335 void set_cisc_spill_operand(OperandForm *opForm) { _cisc_spill_operand = opForm; } 336 OperandForm *cisc_spill_operand() { return _cisc_spill_operand; } 337 bool can_cisc_spill() { return _cisc_spill_operand != NULL; } 338 339 340 protected: 341 // build MatchList from MatchRule 342 void buildMatchList(MatchRule *mrule, const char *resultStr, 343 const char *rootOp, Predicate *pred, const char *cost); 344 345 void buildMList(MatchNode *node, const char *rootOp, const char *resultOp, 346 Predicate *pred, const char *cost); 347 348 friend class ADLParser; 349 350 }; 351 352 353 // -------------------------------- maps ------------------------------------ 354 355 // Base class for generating a mapping from rule number to value. 356 // Used with ArchDesc::build_map() for all maps except "enum MachOperands" 357 // A derived class defines the appropriate output for a specific mapping. 358 class OutputMap { 359 protected: 360 FILE *_hpp; 361 FILE *_cpp; 362 FormDict &_globals; 363 ArchDesc &_AD; 364 public: 365 OutputMap (FILE *decl_file, FILE *def_file, FormDict &globals, ArchDesc &AD) 366 : _hpp(decl_file), _cpp(def_file), _globals(globals), _AD(AD) {}; 367 // Access files used by this routine 368 FILE *decl_file() { return _hpp; } 369 FILE *def_file() { return _cpp; } 370 // Positions in iteration that derived class will be told about 371 enum position { BEGIN_OPERANDS, 372 BEGIN_OPCLASSES, 373 BEGIN_INTERNALS, 374 BEGIN_INSTRUCTIONS, 375 BEGIN_INST_CHAIN_RULES, 376 END_INST_CHAIN_RULES, 377 BEGIN_REMATERIALIZE, 378 END_REMATERIALIZE, 379 END_INSTRUCTIONS 380 }; 381 // Output routines specific to the derived class 382 virtual void declaration() {} 383 virtual void definition() {} 384 virtual void closing() { fprintf(_cpp, "};\n"); } 385 virtual void map(OperandForm &oper) { } 386 virtual void map(OpClassForm &opc) { } 387 virtual void map(char *internal_name) { } 388 // Allow enum-MachOperands to turn-off instructions 389 virtual bool do_instructions() { return true; } 390 virtual void map(InstructForm &inst) { } 391 // Allow derived class to output name and position specific info 392 virtual void record_position(OutputMap::position place, int index) {} 393 };