1 /* 2 * Copyright (c) 1997, 2007, 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 chain_rule_c(FILE *fp, char *indent, char *ideal, char *irule); // %%%%% TODO: remove this 127 void expand_opclass(FILE *fp, const char *indent, const Expr *cost, 128 const char *result_type, ProductionState &status); 129 Expr *calc_cost(FILE *fp, const char *spaces, MatchList &mList, ProductionState &status); 130 void prune_matchlist(Dict &minimize, MatchList &mlist); 131 132 // Helper function that outputs code to generate an instruction in MachNodeGenerator 133 void buildMachNode(FILE *fp_cpp, InstructForm *inst, const char *indent); 134 135 public: 136 ArchDesc(); 137 ~ArchDesc(); 138 139 // Option flags which control miscellaneous behaviors throughout the code 140 int _TotalLines; // Line Counter 141 int _no_output; // Flag to disable output of DFA, etc. 142 int _quiet_mode; // Do not output banner messages, etc. 143 int _disable_warnings; // Do not output warning messages 144 int _dfa_debug; // Debug Flag for generated DFA 145 int _dfa_small; // Debug Flag for generated DFA 146 int _adl_debug; // Debug Flag for ADLC 147 int _adlocation_debug; // Debug Flag to use ad file locations 148 bool _cisc_spill_debug; // Debug Flag to see cisc-spill-instructions 149 bool _short_branch_debug; // Debug Flag to see short branch instructions 150 151 // Error/Warning Counts 152 int _syntax_errs; // Count of syntax errors 153 int _semantic_errs; // Count of semantic errors 154 int _warnings; // Count warnings 155 int _internal_errs; // Count of internal errors 156 157 // Accessor for private data. 158 void has_match_rule(int opc, const bool b) { _has_match_rule[opc] = b; } 159 160 // I/O Files 161 ADLFILE _ADL_file; // Input Architecture Description File 162 // Machine dependent files, built from architecture definition 163 ADLFILE _DFA_file; // File for definition of Matcher::DFA 164 ADLFILE _HPP_file; // File for ArchNode class declarations 165 ADLFILE _CPP_file; // File for ArchNode class defintions 166 ADLFILE _CPP_CLONE_file; // File for MachNode/Oper clone defintions 167 ADLFILE _CPP_EXPAND_file; // File for MachNode expand methods 168 ADLFILE _CPP_FORMAT_file; // File for MachNode/Oper format defintions 169 ADLFILE _CPP_GEN_file; // File for MachNode/Oper generator methods 170 ADLFILE _CPP_MISC_file; // File for miscellaneous MachNode/Oper tables & methods 171 ADLFILE _CPP_PEEPHOLE_file; // File for MachNode peephole methods 172 ADLFILE _CPP_PIPELINE_file; // File for MachNode pipeline defintions 173 ADLFILE _VM_file; // File for constants needed in VM code 174 ADLFILE _bug_file; // DFA debugging file 175 176 // I/O helper methods 177 int open_file(bool required, ADLFILE & adf, const char *action); 178 void close_file(int delete_out, ADLFILE & adf); 179 int open_files(void); 180 void close_files(int delete_out); 181 182 Dict _chainRules; // Maps user operand names to ChainRules 183 Dict _internalOps; // Maps match strings to internal operand names 184 NameList _internalOpNames; // List internal operand names 185 Dict _internalMatch; // Map internal name to its MatchNode 186 187 NameList _preproc_list; // Preprocessor flag names 188 FormDict _preproc_table;// Preprocessor flag bindings 189 char* get_preproc_def(const char* flag); 190 void set_preproc_def(const char* flag, const char* def); 191 192 FormDict& globalNames() {return _globalNames;} // map global names to forms 193 void initKeywords(FormDict& globals); // Add keywords to global name table 194 195 ExprDict& globalDefs() {return _globalDefs;} // map global names to expressions 196 197 OperandForm *constructOperand(const char *ident, bool ideal_only); 198 void initBaseOpTypes(); // Import predefined base types. 199 200 void addForm(PreHeaderForm *ptr); // Add objects to pre-header list 201 void addForm(HeaderForm *ptr); // Add objects to header list 202 void addForm(SourceForm *ptr); // Add objects to source list 203 void addForm(EncodeForm *ptr); // Add objects to encode list 204 void addForm(InstructForm *ptr); // Add objects to the instruct list 205 void addForm(OperandForm *ptr); // Add objects to the operand list 206 void addForm(OpClassForm *ptr); // Add objects to the opclasss list 207 void addForm(AttributeForm *ptr); // Add objects to the attributes list 208 void addForm(RegisterForm *ptr); // Add objects to the register list 209 void addForm(FrameForm *ptr); // Add objects to the frame list 210 void addForm(PipelineForm *ptr); // Add objects to the pipeline list 211 void addForm(MachNodeForm *ptr); // Add objects to the machnode list 212 213 int operandFormCount(); // Count number of OperandForms defined 214 int opclassFormCount(); // Count number of OpClassForms defined 215 int instructFormCount(); // Count number of InstructForms defined 216 217 inline void getForm(EncodeForm **ptr) { *ptr = _encode; } 218 219 bool verify(); 220 void dump(); 221 222 // Helper utility that gets MatchList components from inside MatchRule 223 void check_optype(MatchRule *mrule); 224 void build_chain_rule(OperandForm *oper); 225 void add_chain_rule_entry(const char *src, const char *cost, 226 const char *result); 227 const char *getMatchListIndex(MatchRule &mrule); 228 void generateMatchLists(); // Build MatchList array and populate it 229 void inspectOperands(); // Build MatchLists for all operands 230 void inspectOpClasses(); // Build MatchLists for all operands 231 void inspectInstructions(); // Build MatchLists for all operands 232 void buildDFA(FILE *fp); // Driver for constructing the DFA 233 void gen_dfa_state_body(FILE *fp, Dict &minmize, ProductionState &status, Dict &chained, int i); // Driver for constructing the DFA state bodies 234 235 // Helper utilities to generate reduction maps for internal operands 236 const char *reduceLeft (char *internalName); 237 const char *reduceRight(char *internalName); 238 239 // Build enumerations, (1) dense operand index, (2) operands and opcodes 240 const char *machOperEnum(const char *opName); // create dense index names using static function 241 static const char *getMachOperEnum(const char *opName);// create dense index name 242 void buildMachOperEnum(FILE *fp_hpp);// dense enumeration for operands 243 void buildMachOpcodesEnum(FILE *fp_hpp);// enumeration for MachOpers & MachNodes 244 245 // Helper utilities to generate Register Masks 246 RegisterForm *get_registers() { return _register; } 247 const char *reg_mask(OperandForm &opForm); 248 const char *reg_mask(InstructForm &instForm); 249 const char *reg_class_to_reg_mask(const char *reg_class); 250 char *stack_or_reg_mask(OperandForm &opForm); // name of cisc_spillable version 251 // This register class should also generate a stack_or_reg_mask 252 void set_stack_or_reg(const char *reg_class_name); // for cisc-spillable reg classes 253 // Generate an enumeration of register mask names and the RegMask objects. 254 void declare_register_masks(FILE *fp_cpp); 255 void build_register_masks(FILE *fp_cpp); 256 // Generate enumeration of machine register numbers 257 void buildMachRegisterNumbers(FILE *fp_hpp); 258 // Generate enumeration of machine register encodings 259 void buildMachRegisterEncodes(FILE *fp_hpp); 260 // Generate Regsiter Size Array 261 void declareRegSizes(FILE *fp_hpp); 262 // Generate Pipeline Class information 263 void declare_pipe_classes(FILE *fp_hpp); 264 // Generate Pipeline definitions 265 void build_pipeline_enums(FILE *fp_cpp); 266 // Generate Pipeline Class information 267 void build_pipe_classes(FILE *fp_cpp); 268 269 // Declare and define mappings from rules to result and input types 270 void build_map(OutputMap &map); 271 void buildReduceMaps(FILE *fp_hpp, FILE *fp_cpp); 272 // build flags for signaling that our machine needs this instruction cloned 273 void buildMustCloneMap(FILE *fp_hpp, FILE *fp_cpp); 274 275 // output SUN copyright info 276 void addSunCopyright(char* legal, int size, FILE *fp); 277 // output #include declarations for machine specific files 278 void machineDependentIncludes(ADLFILE &adlfile); 279 // Output C preprocessor code to verify the backend compilation environment. 280 void addPreprocessorChecks(FILE *fp); 281 // Output C source and header (source_hpp) blocks. 282 void addPreHeaderBlocks(FILE *fp_hpp); 283 void addHeaderBlocks(FILE *fp_hpp); 284 void addSourceBlocks(FILE *fp_cpp); 285 void generate_adlc_verification(FILE *fp_cpp); 286 287 // output declaration of class State 288 void defineStateClass(FILE *fp); 289 290 // Generator for MachOper objects given integer type 291 void buildMachOperGenerator(FILE *fp_cpp); 292 // Generator for MachNode objects given integer type 293 void buildMachNodeGenerator(FILE *fp_cpp); 294 295 // Generator for Expand methods for instructions with expand rules 296 void defineExpand(FILE *fp, InstructForm *node); 297 // Generator for Peephole methods for instructions with peephole rules 298 void definePeephole(FILE *fp, InstructForm *node); 299 // Generator for Size methods for instructions 300 void defineSize(FILE *fp, InstructForm &node); 301 // Generator for Emit methods for instructions 302 void defineEmit(FILE *fp, InstructForm &node); 303 // Define a MachOper encode method 304 void define_oper_interface(FILE *fp, OperandForm &oper, FormDict &globals, 305 const char *name, const char *encoding); 306 307 // Methods to construct the MachNode class hierarchy 308 // Return the type signature for the ideal operation 309 const char *getIdealType(const char *idealOp); 310 // Declare and define the classes derived from MachOper and MachNode 311 void declareClasses(FILE *fp_hpp); 312 void defineClasses(FILE *fp_cpp); 313 314 // Emit an ADLC message 315 void internal_err( const char *fmt, ...); 316 void syntax_err ( int lineno, const char *fmt, ...); 317 int emit_msg(int quiet, int flag, int linenum, const char *fmt, 318 va_list args); 319 320 // Generator for has_match_rule methods 321 void buildInstructMatchCheck(FILE *fp_cpp) const; 322 323 // Generator for Frame Methods 324 void buildFrameMethods(FILE *fp_cpp); 325 326 // Generate CISC_spilling oracle and MachNode::cisc_spill() methods 327 void build_cisc_spill_instructions(FILE *fp_hpp, FILE *fp_cpp); 328 void identify_cisc_spill_instructions(); 329 void identify_short_branches(); 330 void identify_unique_operands(); 331 void set_cisc_spill_operand(OperandForm *opForm) { _cisc_spill_operand = opForm; } 332 OperandForm *cisc_spill_operand() { return _cisc_spill_operand; } 333 bool can_cisc_spill() { return _cisc_spill_operand != NULL; } 334 335 336 protected: 337 // build MatchList from MatchRule 338 void buildMatchList(MatchRule *mrule, const char *resultStr, 339 const char *rootOp, Predicate *pred, const char *cost); 340 341 void buildMList(MatchNode *node, const char *rootOp, const char *resultOp, 342 Predicate *pred, const char *cost); 343 344 friend class ADLParser; 345 346 }; 347 348 349 // -------------------------------- maps ------------------------------------ 350 351 // Base class for generating a mapping from rule number to value. 352 // Used with ArchDesc::build_map() for all maps except "enum MachOperands" 353 // A derived class defines the appropriate output for a specific mapping. 354 class OutputMap { 355 protected: 356 FILE *_hpp; 357 FILE *_cpp; 358 FormDict &_globals; 359 ArchDesc &_AD; 360 public: 361 OutputMap (FILE *decl_file, FILE *def_file, FormDict &globals, ArchDesc &AD) 362 : _hpp(decl_file), _cpp(def_file), _globals(globals), _AD(AD) {}; 363 // Access files used by this routine 364 FILE *decl_file() { return _hpp; } 365 FILE *def_file() { return _cpp; } 366 // Positions in iteration that derived class will be told about 367 enum position { BEGIN_OPERANDS, 368 BEGIN_OPCLASSES, 369 BEGIN_INTERNALS, 370 BEGIN_INSTRUCTIONS, 371 BEGIN_INST_CHAIN_RULES, 372 END_INST_CHAIN_RULES, 373 BEGIN_REMATERIALIZE, 374 END_REMATERIALIZE, 375 END_INSTRUCTIONS 376 }; 377 // Output routines specific to the derived class 378 virtual void declaration() {} 379 virtual void definition() {} 380 virtual void closing() { fprintf(_cpp, "};\n"); } 381 virtual void map(OperandForm &oper) { } 382 virtual void map(OpClassForm &opc) { } 383 virtual void map(char *internal_name) { } 384 // Allow enum-MachOperands to turn-off instructions 385 virtual bool do_instructions() { return true; } 386 virtual void map(InstructForm &inst) { } 387 // Allow derived class to output name and position specific info 388 virtual void record_position(OutputMap::position place, int index) {} 389 };