1 /* Table of relaxations for Xtensa assembly. 2 Copyright (C) 2003-2014 Free Software Foundation, Inc. 3 4 This file is part of GAS, the GNU Assembler. 5 6 GAS is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3, or (at your option) 9 any later version. 10 11 GAS is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GAS; see the file COPYING. If not, write to 18 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21 /* This file contains the code for generating runtime data structures 22 for relaxation pattern matching from statically specified strings. 23 Each action contains an instruction pattern to match and 24 preconditions for the match as well as an expansion if the pattern 25 matches. The preconditions can specify that two operands are the 26 same or an operand is a specific constant or register. The expansion 27 uses the bound variables from the pattern to specify that specific 28 operands from the pattern should be used in the result. 29 30 The code determines whether the condition applies to a constant or 31 a register depending on the type of the operand. You may get 32 unexpected results if you don't match the rule against the operand 33 type correctly. 34 35 The patterns match a language like: 36 37 INSN_PATTERN ::= INSN_TEMPL ( '|' PRECOND )* ( '?' OPTIONPRED )* 38 INSN_TEMPL ::= OPCODE ' ' [ OPERAND (',' OPERAND)* ] 39 OPCODE ::= id 40 OPERAND ::= CONSTANT | VARIABLE | SPECIALFN '(' VARIABLE ')' 41 SPECIALFN ::= 'HI24S' | 'F32MINUS' | 'LOW8' 42 | 'HI16' | 'LOW16' 43 VARIABLE ::= '%' id 44 PRECOND ::= OPERAND CMPOP OPERAND 45 CMPOP ::= '==' | '!=' 46 OPTIONPRED ::= OPTIONNAME ('+' OPTIONNAME) 47 OPTIONNAME ::= '"' id '"' 48 49 The replacement language 50 INSN_REPL ::= INSN_LABEL_LIT ( ';' INSN_LABEL_LIT )* 51 INSN_LABEL_LIT ::= INSN_TEMPL 52 | 'LABEL' 53 | 'LITERAL' VARIABLE 54 55 The operands in a PRECOND must be constants or variables bound by 56 the INSN_PATTERN. 57 58 The configuration options define a predicate on the availability of 59 options which must be TRUE for this rule to be valid. Examples are 60 requiring "density" for replacements with density instructions, 61 requiring "const16" for replacements that require const16 62 instructions, etc. The names are interpreted by the assembler to a 63 truth value for a particular frag. 64 65 The operands in the INSN_REPL must be constants, variables bound in 66 the associated INSN_PATTERN, special variables that are bound in 67 the INSN_REPL by LABEL or LITERAL definitions, or special value 68 manipulation functions. 69 70 A simple example of a replacement pattern: 71 {"movi.n %as,%imm", "movi %as,%imm"} would convert the narrow 72 movi.n instruction to the wide movi instruction. 73 74 A more complex example of a branch around: 75 {"beqz %as,%label", "bnez %as,%LABEL;j %label;LABEL"} 76 would convert a branch to a negated branch to the following instruction 77 with a jump to the original label. 78 79 An Xtensa-specific example that generates a literal: 80 {"movi %at,%imm", "LITERAL %imm; l32r %at,%LITERAL"} 81 will convert a movi instruction to an l32r of a literal 82 literal defined in the literal pool. 83 84 Even more complex is a conversion of a load with immediate offset 85 to a load of a freshly generated literal, an explicit add and 86 a load with 0 offset. This transformation is only valid, though 87 when the first and second operands are not the same as specified 88 by the "| %at!=%as" precondition clause. 89 {"l32i %at,%as,%imm | %at!=%as", 90 "LITERAL %imm; l32r %at,%LITERAL; add %at,%at,%as; l32i %at,%at,0"} 91 92 There is special case for loop instructions here, but because we do 93 not currently have the ability to represent the difference of two 94 symbols, the conversion requires special code in the assembler to 95 write the operands of the addi/addmi pair representing the 96 difference of the old and new loop end label. */ 97 98 #include "as.h" 99 #include "xtensa-isa.h" 100 #include "xtensa-relax.h" 101 #include <stddef.h> 102 #include "xtensa-config.h" 103 104 #ifndef XCHAL_HAVE_WIDE_BRANCHES 105 #define XCHAL_HAVE_WIDE_BRANCHES 0 106 #endif 107 108 /* Imported from bfd. */ 109 extern xtensa_isa xtensa_default_isa; 110 111 /* The opname_list is a small list of names that we use for opcode and 112 operand variable names to simplify ownership of these commonly used 113 strings. Strings entered in the table can be compared by pointer 114 equality. */ 115 116 typedef struct opname_list_struct opname_list; 117 typedef opname_list opname_e; 118 119 struct opname_list_struct 120 { 121 char *opname; 122 opname_list *next; 123 }; 124 125 static opname_list *local_opnames = NULL; 126 127 128 /* The "opname_map" and its element structure "opname_map_e" are used 129 for binding an operand number to a name or a constant. */ 130 131 typedef struct opname_map_e_struct opname_map_e; 132 typedef struct opname_map_struct opname_map; 133 134 struct opname_map_e_struct 135 { 136 const char *operand_name; /* If null, then use constant_value. */ 137 int operand_num; 138 unsigned constant_value; 139 opname_map_e *next; 140 }; 141 142 struct opname_map_struct 143 { 144 opname_map_e *head; 145 opname_map_e **tail; 146 }; 147 148 /* The "precond_list" and its element structure "precond_e" represents 149 explicit preconditions comparing operand variables and constants. 150 In the "precond_e" structure, a variable is identified by the name 151 in the "opname" field. If that field is NULL, then the operand 152 is the constant in field "opval". */ 153 154 typedef struct precond_e_struct precond_e; 155 typedef struct precond_list_struct precond_list; 156 157 struct precond_e_struct 158 { 159 const char *opname1; 160 unsigned opval1; 161 CmpOp cmpop; 162 const char *opname2; 163 unsigned opval2; 164 precond_e *next; 165 }; 166 167 struct precond_list_struct 168 { 169 precond_e *head; 170 precond_e **tail; 171 }; 172 173 174 /* The insn_templ represents the INSN_TEMPL instruction template. It 175 is an opcode name with a list of operands. These are used for 176 instruction patterns and replacement patterns. */ 177 178 typedef struct insn_templ_struct insn_templ; 179 struct insn_templ_struct 180 { 181 const char *opcode_name; 182 opname_map operand_map; 183 }; 184 185 186 /* The insn_pattern represents an INSN_PATTERN instruction pattern. 187 It is an instruction template with preconditions that specify when 188 it actually matches a given instruction. */ 189 190 typedef struct insn_pattern_struct insn_pattern; 191 struct insn_pattern_struct 192 { 193 insn_templ t; 194 precond_list preconds; 195 ReqOptionList *options; 196 }; 197 198 199 /* The "insn_repl" and associated element structure "insn_repl_e" 200 instruction replacement list is a list of 201 instructions/LITERALS/LABELS with constant operands or operands 202 with names bound to the operand names in the associated pattern. */ 203 204 typedef struct insn_repl_e_struct insn_repl_e; 205 struct insn_repl_e_struct 206 { 207 insn_templ t; 208 insn_repl_e *next; 209 }; 210 211 typedef struct insn_repl_struct insn_repl; 212 struct insn_repl_struct 213 { 214 insn_repl_e *head; 215 insn_repl_e **tail; 216 }; 217 218 219 /* The split_rec is a vector of allocated char * pointers. */ 220 221 typedef struct split_rec_struct split_rec; 222 struct split_rec_struct 223 { 224 char **vec; 225 int count; 226 }; 227 228 /* The "string_pattern_pair" is a set of pairs containing instruction 229 patterns and replacement strings. */ 230 231 typedef struct string_pattern_pair_struct string_pattern_pair; 232 struct string_pattern_pair_struct 233 { 234 const char *pattern; 235 const char *replacement; 236 }; 237 238 239 /* The widen_spec_list is a list of valid substitutions that generate 241 wider representations. These are generally used to specify 242 replacements for instructions whose immediates do not fit their 243 encodings. A valid transition may require multiple steps of 244 one-to-one instruction replacements with a final multiple 245 instruction replacement. As an example, here are the transitions 246 required to replace an 'addi.n' with an 'addi', 'addmi'. 247 248 addi.n a4, 0x1010 249 => addi a4, 0x1010 250 => addmi a4, 0x1010 251 => addmi a4, 0x1000, addi a4, 0x10. 252 253 See the comments in xg_assembly_relax for some important details 254 regarding how these chains must be built. */ 255 256 static string_pattern_pair widen_spec_list[] = 257 { 258 {"add.n %ar,%as,%at ? IsaUseDensityInstruction", "add %ar,%as,%at"}, 259 {"addi.n %ar,%as,%imm ? IsaUseDensityInstruction", "addi %ar,%as,%imm"}, 260 {"beqz.n %as,%label ? IsaUseDensityInstruction", "beqz %as,%label"}, 261 {"bnez.n %as,%label ? IsaUseDensityInstruction", "bnez %as,%label"}, 262 {"l32i.n %at,%as,%imm ? IsaUseDensityInstruction", "l32i %at,%as,%imm"}, 263 {"mov.n %at,%as ? IsaUseDensityInstruction", "or %at,%as,%as"}, 264 {"movi.n %as,%imm ? IsaUseDensityInstruction", "movi %as,%imm"}, 265 {"nop.n ? IsaUseDensityInstruction ? realnop", "nop"}, 266 {"nop.n ? IsaUseDensityInstruction ? no-realnop", "or 1,1,1"}, 267 {"ret.n %as ? IsaUseDensityInstruction", "ret %as"}, 268 {"retw.n %as ? IsaUseDensityInstruction", "retw %as"}, 269 {"s32i.n %at,%as,%imm ? IsaUseDensityInstruction", "s32i %at,%as,%imm"}, 270 {"srli %at,%as,%imm", "extui %at,%as,%imm,F32MINUS(%imm)"}, 271 {"slli %ar,%as,0", "or %ar,%as,%as"}, 272 273 /* Widening with literals or const16. */ 274 {"movi %at,%imm ? IsaUseL32R ", 275 "LITERAL %imm; l32r %at,%LITERAL"}, 276 {"movi %at,%imm ? IsaUseConst16", 277 "const16 %at,HI16U(%imm); const16 %at,LOW16U(%imm)"}, 278 279 {"addi %ar,%as,%imm", "addmi %ar,%as,%imm"}, 280 /* LOW8 is the low 8 bits of the Immed 281 MID8S is the middle 8 bits of the Immed */ 282 {"addmi %ar,%as,%imm", "addmi %ar,%as,HI24S(%imm); addi %ar,%ar,LOW8(%imm)"}, 283 284 /* In the end convert to either an l32r or const16. */ 285 {"addmi %ar,%as,%imm | %ar!=%as ? IsaUseL32R", 286 "LITERAL %imm; l32r %ar,%LITERAL; add %ar,%as,%ar"}, 287 {"addmi %ar,%as,%imm | %ar!=%as ? IsaUseConst16", 288 "const16 %ar,HI16U(%imm); const16 %ar,LOW16U(%imm); add %ar,%as,%ar"}, 289 290 /* Widening the load instructions with too-large immediates */ 291 {"l8ui %at,%as,%imm | %at!=%as ? IsaUseL32R", 292 "LITERAL %imm; l32r %at,%LITERAL; add %at,%at,%as; l8ui %at,%at,0"}, 293 {"l16si %at,%as,%imm | %at!=%as ? IsaUseL32R", 294 "LITERAL %imm; l32r %at,%LITERAL; add %at,%at,%as; l16si %at,%at,0"}, 295 {"l16ui %at,%as,%imm | %at!=%as ? IsaUseL32R", 296 "LITERAL %imm; l32r %at,%LITERAL; add %at,%at,%as; l16ui %at,%at,0"}, 297 {"l32i %at,%as,%imm | %at!=%as ? IsaUseL32R", 298 "LITERAL %imm; l32r %at,%LITERAL; add %at,%at,%as; l32i %at,%at,0"}, 299 300 /* Widening load instructions with const16s. */ 301 {"l8ui %at,%as,%imm | %at!=%as ? IsaUseConst16", 302 "const16 %at,HI16U(%imm); const16 %at,LOW16U(%imm); add %at,%at,%as; l8ui %at,%at,0"}, 303 {"l16si %at,%as,%imm | %at!=%as ? IsaUseConst16", 304 "const16 %at,HI16U(%imm); const16 %at,LOW16U(%imm); add %at,%at,%as; l16si %at,%at,0"}, 305 {"l16ui %at,%as,%imm | %at!=%as ? IsaUseConst16", 306 "const16 %at,HI16U(%imm); const16 %at,LOW16U(%imm); add %at,%at,%as; l16ui %at,%at,0"}, 307 {"l32i %at,%as,%imm | %at!=%as ? IsaUseConst16", 308 "const16 %at,HI16U(%imm); const16 %at,LOW16U(%imm); add %at,%at,%as; l32i %at,%at,0"}, 309 310 /* This is only PART of the loop instruction. In addition, 311 hardcoded into its use is a modification of the final operand in 312 the instruction in bytes 9 and 12. */ 313 {"loop %as,%label | %as!=1 ? IsaUseLoops", 314 "loop %as,%LABEL;" 315 "rsr.lend %as;" /* LEND */ 316 "wsr.lbeg %as;" /* LBEG */ 317 "addi %as, %as, 0;" /* lo8(%label-%LABEL1) */ 318 "addmi %as, %as, 0;" /* mid8(%label-%LABEL1) */ 319 "wsr.lend %as;" 320 "isync;" 321 "rsr.lcount %as;" /* LCOUNT */ 322 "addi %as, %as, 1;" /* density -> addi.n %as, %as, 1 */ 323 "LABEL"}, 324 {"loopgtz %as,%label | %as!=1 ? IsaUseLoops", 325 "beqz %as,%label;" 326 "bltz %as,%label;" 327 "loopgtz %as,%LABEL;" 328 "rsr.lend %as;" /* LEND */ 329 "wsr.lbeg %as;" /* LBEG */ 330 "addi %as, %as, 0;" /* lo8(%label-%LABEL1) */ 331 "addmi %as, %as, 0;" /* mid8(%label-%LABEL1) */ 332 "wsr.lend %as;" 333 "isync;" 334 "rsr.lcount %as;" /* LCOUNT */ 335 "addi %as, %as, 1;" /* density -> addi.n %as, %as, 1 */ 336 "LABEL"}, 337 {"loopnez %as,%label | %as!=1 ? IsaUseLoops", 338 "beqz %as,%label;" 339 "loopnez %as,%LABEL;" 340 "rsr.lend %as;" /* LEND */ 341 "wsr.lbeg %as;" /* LBEG */ 342 "addi %as, %as, 0;" /* lo8(%label-%LABEL1) */ 343 "addmi %as, %as, 0;" /* mid8(%label-%LABEL1) */ 344 "wsr.lend %as;" 345 "isync;" 346 "rsr.lcount %as;" /* LCOUNT */ 347 "addi %as, %as, 1;" /* density -> addi.n %as, %as, 1 */ 348 "LABEL"}, 349 350 /* Relaxing to wide branches. Order is important here. With wide 351 branches, there is more than one correct relaxation for an 352 out-of-range branch. Put the wide branch relaxations first in the 353 table since they are more efficient than the branch-around 354 relaxations. */ 355 356 {"beqz %as,%label ? IsaUseWideBranches", "WIDE.beqz %as,%label"}, 357 {"bnez %as,%label ? IsaUseWideBranches", "WIDE.bnez %as,%label"}, 358 {"bgez %as,%label ? IsaUseWideBranches", "WIDE.bgez %as,%label"}, 359 {"bltz %as,%label ? IsaUseWideBranches", "WIDE.bltz %as,%label"}, 360 {"beqi %as,%imm,%label ? IsaUseWideBranches", "WIDE.beqi %as,%imm,%label"}, 361 {"bnei %as,%imm,%label ? IsaUseWideBranches", "WIDE.bnei %as,%imm,%label"}, 362 {"bgei %as,%imm,%label ? IsaUseWideBranches", "WIDE.bgei %as,%imm,%label"}, 363 {"blti %as,%imm,%label ? IsaUseWideBranches", "WIDE.blti %as,%imm,%label"}, 364 {"bgeui %as,%imm,%label ? IsaUseWideBranches", "WIDE.bgeui %as,%imm,%label"}, 365 {"bltui %as,%imm,%label ? IsaUseWideBranches", "WIDE.bltui %as,%imm,%label"}, 366 {"bbci %as,%imm,%label ? IsaUseWideBranches", "WIDE.bbci %as,%imm,%label"}, 367 {"bbsi %as,%imm,%label ? IsaUseWideBranches", "WIDE.bbsi %as,%imm,%label"}, 368 {"beq %as,%at,%label ? IsaUseWideBranches", "WIDE.beq %as,%at,%label"}, 369 {"bne %as,%at,%label ? IsaUseWideBranches", "WIDE.bne %as,%at,%label"}, 370 {"bge %as,%at,%label ? IsaUseWideBranches", "WIDE.bge %as,%at,%label"}, 371 {"blt %as,%at,%label ? IsaUseWideBranches", "WIDE.blt %as,%at,%label"}, 372 {"bgeu %as,%at,%label ? IsaUseWideBranches", "WIDE.bgeu %as,%at,%label"}, 373 {"bltu %as,%at,%label ? IsaUseWideBranches", "WIDE.bltu %as,%at,%label"}, 374 {"bany %as,%at,%label ? IsaUseWideBranches", "WIDE.bany %as,%at,%label"}, 375 {"bnone %as,%at,%label ? IsaUseWideBranches", "WIDE.bnone %as,%at,%label"}, 376 {"ball %as,%at,%label ? IsaUseWideBranches", "WIDE.ball %as,%at,%label"}, 377 {"bnall %as,%at,%label ? IsaUseWideBranches", "WIDE.bnall %as,%at,%label"}, 378 {"bbc %as,%at,%label ? IsaUseWideBranches", "WIDE.bbc %as,%at,%label"}, 379 {"bbs %as,%at,%label ? IsaUseWideBranches", "WIDE.bbs %as,%at,%label"}, 380 381 /* Widening branch comparisons eq/ne to zero. Prefer relaxing to narrow 382 branches if the density option is available. */ 383 {"beqz %as,%label ? IsaUseDensityInstruction", "bnez.n %as,%LABEL;j %label;LABEL"}, 384 {"bnez %as,%label ? IsaUseDensityInstruction", "beqz.n %as,%LABEL;j %label;LABEL"}, 385 {"beqz %as,%label", "bnez %as,%LABEL;j %label;LABEL"}, 386 {"bnez %as,%label", "beqz %as,%LABEL;j %label;LABEL"}, 387 {"WIDE.beqz %as,%label ? IsaUseDensityInstruction", "bnez.n %as,%LABEL;j %label;LABEL"}, 388 {"WIDE.bnez %as,%label ? IsaUseDensityInstruction", "beqz.n %as,%LABEL;j %label;LABEL"}, 389 {"WIDE.beqz %as,%label", "bnez %as,%LABEL;j %label;LABEL"}, 390 {"WIDE.bnez %as,%label", "beqz %as,%LABEL;j %label;LABEL"}, 391 392 /* Widening expect-taken branches. */ 393 {"beqzt %as,%label ? IsaUsePredictedBranches", "bnez %as,%LABEL;j %label;LABEL"}, 394 {"bnezt %as,%label ? IsaUsePredictedBranches", "beqz %as,%LABEL;j %label;LABEL"}, 395 {"beqt %as,%at,%label ? IsaUsePredictedBranches", "bne %as,%at,%LABEL;j %label;LABEL"}, 396 {"bnet %as,%at,%label ? IsaUsePredictedBranches", "beq %as,%at,%LABEL;j %label;LABEL"}, 397 398 /* Widening branches from the Xtensa boolean option. */ 399 {"bt %bs,%label ? IsaUseBooleans", "bf %bs,%LABEL;j %label;LABEL"}, 400 {"bf %bs,%label ? IsaUseBooleans", "bt %bs,%LABEL;j %label;LABEL"}, 401 402 /* Other branch-around-jump widenings. */ 403 {"bgez %as,%label", "bltz %as,%LABEL;j %label;LABEL"}, 404 {"bltz %as,%label", "bgez %as,%LABEL;j %label;LABEL"}, 405 {"beqi %as,%imm,%label", "bnei %as,%imm,%LABEL;j %label;LABEL"}, 406 {"bnei %as,%imm,%label", "beqi %as,%imm,%LABEL;j %label;LABEL"}, 407 {"bgei %as,%imm,%label", "blti %as,%imm,%LABEL;j %label;LABEL"}, 408 {"blti %as,%imm,%label", "bgei %as,%imm,%LABEL;j %label;LABEL"}, 409 {"bgeui %as,%imm,%label", "bltui %as,%imm,%LABEL;j %label;LABEL"}, 410 {"bltui %as,%imm,%label", "bgeui %as,%imm,%LABEL;j %label;LABEL"}, 411 {"bbci %as,%imm,%label", "bbsi %as,%imm,%LABEL;j %label;LABEL"}, 412 {"bbsi %as,%imm,%label", "bbci %as,%imm,%LABEL;j %label;LABEL"}, 413 {"beq %as,%at,%label", "bne %as,%at,%LABEL;j %label;LABEL"}, 414 {"bne %as,%at,%label", "beq %as,%at,%LABEL;j %label;LABEL"}, 415 {"bge %as,%at,%label", "blt %as,%at,%LABEL;j %label;LABEL"}, 416 {"blt %as,%at,%label", "bge %as,%at,%LABEL;j %label;LABEL"}, 417 {"bgeu %as,%at,%label", "bltu %as,%at,%LABEL;j %label;LABEL"}, 418 {"bltu %as,%at,%label", "bgeu %as,%at,%LABEL;j %label;LABEL"}, 419 {"bany %as,%at,%label", "bnone %as,%at,%LABEL;j %label;LABEL"}, 420 {"bnone %as,%at,%label", "bany %as,%at,%LABEL;j %label;LABEL"}, 421 {"ball %as,%at,%label", "bnall %as,%at,%LABEL;j %label;LABEL"}, 422 {"bnall %as,%at,%label", "ball %as,%at,%LABEL;j %label;LABEL"}, 423 {"bbc %as,%at,%label", "bbs %as,%at,%LABEL;j %label;LABEL"}, 424 {"bbs %as,%at,%label", "bbc %as,%at,%LABEL;j %label;LABEL"}, 425 426 {"WIDE.bgez %as,%label", "bltz %as,%LABEL;j %label;LABEL"}, 427 {"WIDE.bltz %as,%label", "bgez %as,%LABEL;j %label;LABEL"}, 428 {"WIDE.beqi %as,%imm,%label", "bnei %as,%imm,%LABEL;j %label;LABEL"}, 429 {"WIDE.bnei %as,%imm,%label", "beqi %as,%imm,%LABEL;j %label;LABEL"}, 430 {"WIDE.bgei %as,%imm,%label", "blti %as,%imm,%LABEL;j %label;LABEL"}, 431 {"WIDE.blti %as,%imm,%label", "bgei %as,%imm,%LABEL;j %label;LABEL"}, 432 {"WIDE.bgeui %as,%imm,%label", "bltui %as,%imm,%LABEL;j %label;LABEL"}, 433 {"WIDE.bltui %as,%imm,%label", "bgeui %as,%imm,%LABEL;j %label;LABEL"}, 434 {"WIDE.bbci %as,%imm,%label", "bbsi %as,%imm,%LABEL;j %label;LABEL"}, 435 {"WIDE.bbsi %as,%imm,%label", "bbci %as,%imm,%LABEL;j %label;LABEL"}, 436 {"WIDE.beq %as,%at,%label", "bne %as,%at,%LABEL;j %label;LABEL"}, 437 {"WIDE.bne %as,%at,%label", "beq %as,%at,%LABEL;j %label;LABEL"}, 438 {"WIDE.bge %as,%at,%label", "blt %as,%at,%LABEL;j %label;LABEL"}, 439 {"WIDE.blt %as,%at,%label", "bge %as,%at,%LABEL;j %label;LABEL"}, 440 {"WIDE.bgeu %as,%at,%label", "bltu %as,%at,%LABEL;j %label;LABEL"}, 441 {"WIDE.bltu %as,%at,%label", "bgeu %as,%at,%LABEL;j %label;LABEL"}, 442 {"WIDE.bany %as,%at,%label", "bnone %as,%at,%LABEL;j %label;LABEL"}, 443 {"WIDE.bnone %as,%at,%label", "bany %as,%at,%LABEL;j %label;LABEL"}, 444 {"WIDE.ball %as,%at,%label", "bnall %as,%at,%LABEL;j %label;LABEL"}, 445 {"WIDE.bnall %as,%at,%label", "ball %as,%at,%LABEL;j %label;LABEL"}, 446 {"WIDE.bbc %as,%at,%label", "bbs %as,%at,%LABEL;j %label;LABEL"}, 447 {"WIDE.bbs %as,%at,%label", "bbc %as,%at,%LABEL;j %label;LABEL"}, 448 449 /* Expanding calls with literals. */ 450 {"call0 %label,%ar0 ? IsaUseL32R", 451 "LITERAL %label; l32r a0,%LITERAL; callx0 a0,%ar0"}, 452 {"call4 %label,%ar4 ? IsaUseL32R", 453 "LITERAL %label; l32r a4,%LITERAL; callx4 a4,%ar4"}, 454 {"call8 %label,%ar8 ? IsaUseL32R", 455 "LITERAL %label; l32r a8,%LITERAL; callx8 a8,%ar8"}, 456 {"call12 %label,%ar12 ? IsaUseL32R", 457 "LITERAL %label; l32r a12,%LITERAL; callx12 a12,%ar12"}, 458 459 /* Expanding calls with const16. */ 460 {"call0 %label,%ar0 ? IsaUseConst16", 461 "const16 a0,HI16U(%label); const16 a0,LOW16U(%label); callx0 a0,%ar0"}, 462 {"call4 %label,%ar4 ? IsaUseConst16", 463 "const16 a4,HI16U(%label); const16 a4,LOW16U(%label); callx4 a4,%ar4"}, 464 {"call8 %label,%ar8 ? IsaUseConst16", 465 "const16 a8,HI16U(%label); const16 a8,LOW16U(%label); callx8 a8,%ar8"}, 466 {"call12 %label,%ar12 ? IsaUseConst16", 467 "const16 a12,HI16U(%label); const16 a12,LOW16U(%label); callx12 a12,%ar12"}, 468 469 /* Expanding j.l with literals. */ 470 {"j %label ? FREEREG ? IsaUseL32R", 471 "LITERAL %label; l32r FREEREG,%LITERAL; jx FREEREG"}, 472 /* Expanding j.l with const16. */ 473 {"j %label ? FREEREG ? IsaUseConst16", 474 "const16 FREEREG,HI16U(%label); const16 FREEREG,LOW16U(%label); jx FREEREG"}, 475 }; 476 477 #define WIDEN_COUNT (sizeof (widen_spec_list) / sizeof (string_pattern_pair)) 478 479 480 /* The simplify_spec_list specifies simplifying transformations that 481 will reduce the instruction width or otherwise simplify an 482 instruction. These are usually applied before relaxation in the 483 assembler. It is always legal to simplify. Even for "addi as, 0", 484 the "addi.n as, 0" will eventually be widened back to an "addi 0" 485 after the widening table is applied. Note: The usage of this table 486 has changed somewhat so that it is entirely specific to "narrowing" 487 instructions to use the density option. This table is not used at 488 all when the density option is not available. */ 489 490 string_pattern_pair simplify_spec_list[] = 491 { 492 {"add %ar,%as,%at ? IsaUseDensityInstruction", "add.n %ar,%as,%at"}, 493 {"addi.n %ar,%as,0 ? IsaUseDensityInstruction", "mov.n %ar,%as"}, 494 {"addi %ar,%as,0 ? IsaUseDensityInstruction", "mov.n %ar,%as"}, 495 {"addi %ar,%as,%imm ? IsaUseDensityInstruction", "addi.n %ar,%as,%imm"}, 496 {"addmi %ar,%as,%imm ? IsaUseDensityInstruction", "addi.n %ar,%as,%imm"}, 497 {"beqz %as,%label ? IsaUseDensityInstruction", "beqz.n %as,%label"}, 498 {"bnez %as,%label ? IsaUseDensityInstruction", "bnez.n %as,%label"}, 499 {"l32i %at,%as,%imm ? IsaUseDensityInstruction", "l32i.n %at,%as,%imm"}, 500 {"movi %as,%imm ? IsaUseDensityInstruction", "movi.n %as,%imm"}, 501 {"nop ? realnop ? IsaUseDensityInstruction", "nop.n"}, 502 {"or %ar,%as,%at | %ar==%as | %as==%at ? IsaUseDensityInstruction", "nop.n"}, 503 {"or %ar,%as,%at | %ar!=%as | %as==%at ? IsaUseDensityInstruction", "mov.n %ar,%as"}, 504 {"ret %as ? IsaUseDensityInstruction", "ret.n %as"}, 505 {"retw %as ? IsaUseDensityInstruction", "retw.n %as"}, 506 {"s32i %at,%as,%imm ? IsaUseDensityInstruction", "s32i.n %at,%as,%imm"}, 507 {"slli %ar,%as,0 ? IsaUseDensityInstruction", "mov.n %ar,%as"} 508 }; 509 510 #define SIMPLIFY_COUNT \ 511 (sizeof (simplify_spec_list) / sizeof (string_pattern_pair)) 512 513 514 /* Externally visible functions. */ 516 517 extern bfd_boolean xg_has_userdef_op_fn (OpType); 518 extern long xg_apply_userdef_op_fn (OpType, long); 519 520 521 static void 522 append_transition (TransitionTable *tt, 523 xtensa_opcode opcode, 524 TransitionRule *t, 525 transition_cmp_fn cmp) 526 { 527 TransitionList *tl = (TransitionList *) xmalloc (sizeof (TransitionList)); 528 TransitionList *prev; 529 TransitionList **t_p; 530 gas_assert (tt != NULL); 531 gas_assert (opcode < tt->num_opcodes); 532 533 prev = tt->table[opcode]; 534 tl->rule = t; 535 tl->next = NULL; 536 if (prev == NULL) 537 { 538 tt->table[opcode] = tl; 539 return; 540 } 541 542 for (t_p = &tt->table[opcode]; (*t_p) != NULL; t_p = &(*t_p)->next) 543 { 544 if (cmp && cmp (t, (*t_p)->rule) < 0) 545 { 546 /* Insert it here. */ 547 tl->next = *t_p; 548 *t_p = tl; 549 return; 550 } 551 } 552 (*t_p) = tl; 553 } 554 555 556 static void 557 append_condition (TransitionRule *tr, Precondition *cond) 558 { 559 PreconditionList *pl = 560 (PreconditionList *) xmalloc (sizeof (PreconditionList)); 561 PreconditionList *prev = tr->conditions; 562 PreconditionList *nxt; 563 564 pl->precond = cond; 565 pl->next = NULL; 566 if (prev == NULL) 567 { 568 tr->conditions = pl; 569 return; 570 } 571 nxt = prev->next; 572 while (nxt != NULL) 573 { 574 prev = nxt; 575 nxt = nxt->next; 576 } 577 prev->next = pl; 578 } 579 580 581 static void 582 append_value_condition (TransitionRule *tr, 583 CmpOp cmp, 584 unsigned op1, 585 unsigned op2) 586 { 587 Precondition *cond = (Precondition *) xmalloc (sizeof (Precondition)); 588 589 cond->cmp = cmp; 590 cond->op_num = op1; 591 cond->typ = OP_OPERAND; 592 cond->op_data = op2; 593 append_condition (tr, cond); 594 } 595 596 597 static void 598 append_constant_value_condition (TransitionRule *tr, 599 CmpOp cmp, 600 unsigned op1, 601 unsigned cnst) 602 { 603 Precondition *cond = (Precondition *) xmalloc (sizeof (Precondition)); 604 605 cond->cmp = cmp; 606 cond->op_num = op1; 607 cond->typ = OP_CONSTANT; 608 cond->op_data = cnst; 609 append_condition (tr, cond); 610 } 611 612 613 static void 614 append_build_insn (TransitionRule *tr, BuildInstr *bi) 615 { 616 BuildInstr *prev = tr->to_instr; 617 BuildInstr *nxt; 618 619 bi->next = NULL; 620 if (prev == NULL) 621 { 622 tr->to_instr = bi; 623 return; 624 } 625 nxt = prev->next; 626 while (nxt != 0) 627 { 628 prev = nxt; 629 nxt = prev->next; 630 } 631 prev->next = bi; 632 } 633 634 635 static void 636 append_op (BuildInstr *bi, BuildOp *b_op) 637 { 638 BuildOp *prev = bi->ops; 639 BuildOp *nxt; 640 641 if (prev == NULL) 642 { 643 bi->ops = b_op; 644 return; 645 } 646 nxt = prev->next; 647 while (nxt != NULL) 648 { 649 prev = nxt; 650 nxt = nxt->next; 651 } 652 prev->next = b_op; 653 } 654 655 656 static void 657 append_literal_op (BuildInstr *bi, unsigned op1, unsigned src_op) 658 { 659 BuildOp *b_op = (BuildOp *) xmalloc (sizeof (BuildOp)); 660 661 b_op->op_num = op1; 662 b_op->typ = OP_LITERAL; 663 b_op->op_data = src_op; 664 b_op->next = NULL; 665 append_op (bi, b_op); 666 } 667 668 669 static void 670 append_label_op (BuildInstr *bi, unsigned op1) 671 { 672 BuildOp *b_op = (BuildOp *) xmalloc (sizeof (BuildOp)); 673 674 b_op->op_num = op1; 675 b_op->typ = OP_LABEL; 676 b_op->op_data = 0; 677 b_op->next = NULL; 678 append_op (bi, b_op); 679 } 680 681 682 static void 683 append_constant_op (BuildInstr *bi, unsigned op1, unsigned cnst) 684 { 685 BuildOp *b_op = (BuildOp *) xmalloc (sizeof (BuildOp)); 686 687 b_op->op_num = op1; 688 b_op->typ = OP_CONSTANT; 689 b_op->op_data = cnst; 690 b_op->next = NULL; 691 append_op (bi, b_op); 692 } 693 694 695 static void 696 append_field_op (BuildInstr *bi, unsigned op1, unsigned src_op) 697 { 698 BuildOp *b_op = (BuildOp *) xmalloc (sizeof (BuildOp)); 699 700 b_op->op_num = op1; 701 b_op->typ = OP_OPERAND; 702 b_op->op_data = src_op; 703 b_op->next = NULL; 704 append_op (bi, b_op); 705 } 706 707 708 /* These could be generated but are not currently. */ 709 710 static void 711 append_user_fn_field_op (BuildInstr *bi, 712 unsigned op1, 713 OpType typ, 714 unsigned src_op) 715 { 716 BuildOp *b_op = (BuildOp *) xmalloc (sizeof (BuildOp)); 717 718 b_op->op_num = op1; 719 b_op->typ = typ; 720 b_op->op_data = src_op; 721 b_op->next = NULL; 722 append_op (bi, b_op); 723 } 724 725 726 /* These operand functions are the semantics of user-defined 727 operand functions. */ 728 729 static long 730 operand_function_HI24S (long a) 731 { 732 if (a & 0x80) 733 return (a & (~0xff)) + 0x100; 734 else 735 return (a & (~0xff)); 736 } 737 738 739 static long 740 operand_function_F32MINUS (long a) 741 { 742 return (32 - a); 743 } 744 745 746 static long 747 operand_function_LOW8 (long a) 748 { 749 if (a & 0x80) 750 return (a & 0xff) | ~0xff; 751 else 752 return (a & 0xff); 753 } 754 755 756 static long 757 operand_function_LOW16U (long a) 758 { 759 return (a & 0xffff); 760 } 761 762 763 static long 764 operand_function_HI16U (long a) 765 { 766 unsigned long b = a & 0xffff0000; 767 return (long) (b >> 16); 768 } 769 770 771 bfd_boolean 772 xg_has_userdef_op_fn (OpType op) 773 { 774 switch (op) 775 { 776 case OP_OPERAND_F32MINUS: 777 case OP_OPERAND_LOW8: 778 case OP_OPERAND_HI24S: 779 case OP_OPERAND_LOW16U: 780 case OP_OPERAND_HI16U: 781 return TRUE; 782 default: 783 break; 784 } 785 return FALSE; 786 } 787 788 789 long 790 xg_apply_userdef_op_fn (OpType op, long a) 791 { 792 switch (op) 793 { 794 case OP_OPERAND_F32MINUS: 795 return operand_function_F32MINUS (a); 796 case OP_OPERAND_LOW8: 797 return operand_function_LOW8 (a); 798 case OP_OPERAND_HI24S: 799 return operand_function_HI24S (a); 800 case OP_OPERAND_LOW16U: 801 return operand_function_LOW16U (a); 802 case OP_OPERAND_HI16U: 803 return operand_function_HI16U (a); 804 default: 805 break; 806 } 807 return FALSE; 808 } 809 810 811 /* Generate a transition table. */ 812 813 static const char * 814 enter_opname_n (const char *name, int len) 815 { 816 opname_e *op; 817 818 for (op = local_opnames; op != NULL; op = op->next) 819 { 820 if (strlen (op->opname) == (unsigned) len 821 && strncmp (op->opname, name, len) == 0) 822 return op->opname; 823 } 824 op = (opname_e *) xmalloc (sizeof (opname_e)); 825 op->opname = (char *) xmalloc (len + 1); 826 strncpy (op->opname, name, len); 827 op->opname[len] = '\0'; 828 return op->opname; 829 } 830 831 832 static const char * 833 enter_opname (const char *name) 834 { 835 opname_e *op; 836 837 for (op = local_opnames; op != NULL; op = op->next) 838 { 839 if (strcmp (op->opname, name) == 0) 840 return op->opname; 841 } 842 op = (opname_e *) xmalloc (sizeof (opname_e)); 843 op->opname = xstrdup (name); 844 return op->opname; 845 } 846 847 848 static void 849 init_opname_map (opname_map *m) 850 { 851 m->head = NULL; 852 m->tail = &m->head; 853 } 854 855 856 static void 857 clear_opname_map (opname_map *m) 858 { 859 opname_map_e *e; 860 861 while (m->head != NULL) 862 { 863 e = m->head; 864 m->head = e->next; 865 free (e); 866 } 867 m->tail = &m->head; 868 } 869 870 871 static bfd_boolean 872 same_operand_name (const opname_map_e *m1, const opname_map_e *m2) 873 { 874 if (m1->operand_name == NULL || m1->operand_name == NULL) 875 return FALSE; 876 return (m1->operand_name == m2->operand_name); 877 } 878 879 880 static opname_map_e * 881 get_opmatch (opname_map *map, const char *operand_name) 882 { 883 opname_map_e *m; 884 885 for (m = map->head; m != NULL; m = m->next) 886 { 887 if (strcmp (m->operand_name, operand_name) == 0) 888 return m; 889 } 890 return NULL; 891 } 892 893 894 static bfd_boolean 895 op_is_constant (const opname_map_e *m1) 896 { 897 return (m1->operand_name == NULL); 898 } 899 900 901 static unsigned 902 op_get_constant (const opname_map_e *m1) 903 { 904 gas_assert (m1->operand_name == NULL); 905 return m1->constant_value; 906 } 907 908 909 static void 910 init_precond_list (precond_list *l) 911 { 912 l->head = NULL; 913 l->tail = &l->head; 914 } 915 916 917 static void 918 clear_precond_list (precond_list *l) 919 { 920 precond_e *e; 921 922 while (l->head != NULL) 923 { 924 e = l->head; 925 l->head = e->next; 926 free (e); 927 } 928 l->tail = &l->head; 929 } 930 931 932 static void 933 init_insn_templ (insn_templ *t) 934 { 935 t->opcode_name = NULL; 936 init_opname_map (&t->operand_map); 937 } 938 939 940 static void 941 clear_insn_templ (insn_templ *t) 942 { 943 clear_opname_map (&t->operand_map); 944 } 945 946 947 static void 948 init_insn_pattern (insn_pattern *p) 949 { 950 init_insn_templ (&p->t); 951 init_precond_list (&p->preconds); 952 p->options = NULL; 953 } 954 955 956 static void 957 clear_insn_pattern (insn_pattern *p) 958 { 959 clear_insn_templ (&p->t); 960 clear_precond_list (&p->preconds); 961 } 962 963 964 static void 965 init_insn_repl (insn_repl *r) 966 { 967 r->head = NULL; 968 r->tail = &r->head; 969 } 970 971 972 static void 973 clear_insn_repl (insn_repl *r) 974 { 975 insn_repl_e *e; 976 977 while (r->head != NULL) 978 { 979 e = r->head; 980 r->head = e->next; 981 clear_insn_templ (&e->t); 982 } 983 r->tail = &r->head; 984 } 985 986 987 static int 988 insn_templ_operand_count (const insn_templ *t) 989 { 990 int i = 0; 991 const opname_map_e *op; 992 993 for (op = t->operand_map.head; op != NULL; op = op->next, i++) 994 ; 995 return i; 996 } 997 998 999 /* Convert a string to a number. E.G.: parse_constant("10", &num) */ 1000 1001 static bfd_boolean 1002 parse_constant (const char *in, unsigned *val_p) 1003 { 1004 unsigned val = 0; 1005 const char *p; 1006 1007 if (in == NULL) 1008 return FALSE; 1009 p = in; 1010 1011 while (*p != '\0') 1012 { 1013 if (*p >= '0' && *p <= '9') 1014 val = val * 10 + (*p - '0'); 1015 else 1016 return FALSE; 1017 ++p; 1018 } 1019 *val_p = val; 1020 return TRUE; 1021 } 1022 1023 1024 static bfd_boolean 1025 parse_special_fn (const char *name, 1026 const char **fn_name_p, 1027 const char **arg_name_p) 1028 { 1029 char *p_start; 1030 const char *p_end; 1031 1032 p_start = strchr (name, '('); 1033 if (p_start == NULL) 1034 return FALSE; 1035 1036 p_end = strchr (p_start, ')'); 1037 1038 if (p_end == NULL) 1039 return FALSE; 1040 1041 if (p_end[1] != '\0') 1042 return FALSE; 1043 1044 *fn_name_p = enter_opname_n (name, p_start - name); 1045 *arg_name_p = enter_opname_n (p_start + 1, p_end - p_start - 1); 1046 return TRUE; 1047 } 1048 1049 1050 static const char * 1051 skip_white (const char *p) 1052 { 1053 if (p == NULL) 1054 return p; 1055 while (*p == ' ') 1056 ++p; 1057 return p; 1058 } 1059 1060 1061 static void 1062 trim_whitespace (char *in) 1063 { 1064 char *last_white = NULL; 1065 char *p = in; 1066 1067 while (p && *p != '\0') 1068 { 1069 while (*p == ' ') 1070 { 1071 if (last_white == NULL) 1072 last_white = p; 1073 p++; 1074 } 1075 if (*p != '\0') 1076 { 1077 last_white = NULL; 1078 p++; 1079 } 1080 } 1081 if (last_white) 1082 *last_white = '\0'; 1083 } 1084 1085 1086 /* Split a string into component strings where "c" is the 1087 delimiter. Place the result in the split_rec. */ 1088 1089 static void 1090 split_string (split_rec *rec, 1091 const char *in, 1092 char c, 1093 bfd_boolean elide_whitespace) 1094 { 1095 int cnt = 0; 1096 int i; 1097 const char *p = in; 1098 1099 while (p != NULL && *p != '\0') 1100 { 1101 cnt++; 1102 p = strchr (p, c); 1103 if (p) 1104 p++; 1105 } 1106 rec->count = cnt; 1107 rec->vec = NULL; 1108 1109 if (rec->count == 0) 1110 return; 1111 1112 rec->vec = (char **) xmalloc (sizeof (char *) * cnt); 1113 for (i = 0; i < cnt; i++) 1114 rec->vec[i] = 0; 1115 1116 p = in; 1117 for (i = 0; i < cnt; i++) 1118 { 1119 const char *q; 1120 int len; 1121 1122 q = p; 1123 if (elide_whitespace) 1124 q = skip_white (q); 1125 1126 p = strchr (q, c); 1127 if (p == NULL) 1128 rec->vec[i] = xstrdup (q); 1129 else 1130 { 1131 len = p - q; 1132 rec->vec[i] = (char *) xmalloc (sizeof (char) * (len + 1)); 1133 strncpy (rec->vec[i], q, len); 1134 rec->vec[i][len] = '\0'; 1135 p++; 1136 } 1137 1138 if (elide_whitespace) 1139 trim_whitespace (rec->vec[i]); 1140 } 1141 } 1142 1143 1144 static void 1145 clear_split_rec (split_rec *rec) 1146 { 1147 int i; 1148 1149 for (i = 0; i < rec->count; i++) 1150 free (rec->vec[i]); 1151 1152 if (rec->count > 0) 1153 free (rec->vec); 1154 } 1155 1156 1157 /* Initialize a split record. The split record must be initialized 1158 before split_string is called. */ 1159 1160 static void 1161 init_split_rec (split_rec *rec) 1162 { 1163 rec->vec = NULL; 1164 rec->count = 0; 1165 } 1166 1167 1168 /* Parse an instruction template like "insn op1, op2, op3". */ 1169 1170 static bfd_boolean 1171 parse_insn_templ (const char *s, insn_templ *t) 1172 { 1173 const char *p = s; 1174 int insn_name_len; 1175 split_rec oprec; 1176 int i; 1177 1178 /* First find the first whitespace. */ 1179 1180 init_split_rec (&oprec); 1181 1182 p = skip_white (p); 1183 insn_name_len = strcspn (s, " "); 1184 if (insn_name_len == 0) 1185 return FALSE; 1186 1187 init_insn_templ (t); 1188 t->opcode_name = enter_opname_n (p, insn_name_len); 1189 1190 p = p + insn_name_len; 1191 1192 /* Split by ',' and skip beginning and trailing whitespace. */ 1193 split_string (&oprec, p, ',', TRUE); 1194 1195 for (i = 0; i < oprec.count; i++) 1196 { 1197 const char *opname = oprec.vec[i]; 1198 opname_map_e *e = (opname_map_e *) xmalloc (sizeof (opname_map_e)); 1199 e->next = NULL; 1200 e->operand_name = NULL; 1201 e->constant_value = 0; 1202 e->operand_num = i; 1203 1204 /* If it begins with a number, assume that it is a number. */ 1205 if (opname && opname[0] >= '0' && opname[0] <= '9') 1206 { 1207 unsigned val; 1208 1209 if (parse_constant (opname, &val)) 1210 e->constant_value = val; 1211 else 1212 { 1213 free (e); 1214 clear_split_rec (&oprec); 1215 clear_insn_templ (t); 1216 return FALSE; 1217 } 1218 } 1219 else 1220 e->operand_name = enter_opname (oprec.vec[i]); 1221 1222 *t->operand_map.tail = e; 1223 t->operand_map.tail = &e->next; 1224 } 1225 clear_split_rec (&oprec); 1226 return TRUE; 1227 } 1228 1229 1230 static bfd_boolean 1231 parse_precond (const char *s, precond_e *precond) 1232 { 1233 /* All preconditions are currently of the form: 1234 a == b or a != b or a == k (where k is a constant). 1235 Later we may use some special functions like DENSITY == 1 1236 to identify when density is available. */ 1237 1238 const char *p = s; 1239 int len; 1240 precond->opname1 = NULL; 1241 precond->opval1 = 0; 1242 precond->cmpop = OP_EQUAL; 1243 precond->opname2 = NULL; 1244 precond->opval2 = 0; 1245 precond->next = NULL; 1246 1247 p = skip_white (p); 1248 1249 len = strcspn (p, " !="); 1250 1251 if (len == 0) 1252 return FALSE; 1253 1254 precond->opname1 = enter_opname_n (p, len); 1255 p = p + len; 1256 p = skip_white (p); 1257 1258 /* Check for "==" and "!=". */ 1259 if (strncmp (p, "==", 2) == 0) 1260 precond->cmpop = OP_EQUAL; 1261 else if (strncmp (p, "!=", 2) == 0) 1262 precond->cmpop = OP_NOTEQUAL; 1263 else 1264 return FALSE; 1265 1266 p = p + 2; 1267 p = skip_white (p); 1268 1269 /* No trailing whitespace from earlier parsing. */ 1270 if (p[0] >= '0' && p[0] <= '9') 1271 { 1272 unsigned val; 1273 if (parse_constant (p, &val)) 1274 precond->opval2 = val; 1275 else 1276 return FALSE; 1277 } 1278 else 1279 precond->opname2 = enter_opname (p); 1280 return TRUE; 1281 } 1282 1283 1284 static void 1285 clear_req_or_option_list (ReqOrOption **r_p) 1286 { 1287 if (*r_p == NULL) 1288 return; 1289 1290 free ((*r_p)->option_name); 1291 clear_req_or_option_list (&(*r_p)->next); 1292 *r_p = NULL; 1293 } 1294 1295 1296 static void 1297 clear_req_option_list (ReqOption **r_p) 1298 { 1299 if (*r_p == NULL) 1300 return; 1301 1302 clear_req_or_option_list (&(*r_p)->or_option_terms); 1303 clear_req_option_list (&(*r_p)->next); 1304 *r_p = NULL; 1305 } 1306 1307 1308 static ReqOrOption * 1309 clone_req_or_option_list (ReqOrOption *req_or_option) 1310 { 1311 ReqOrOption *new_req_or_option; 1312 1313 if (req_or_option == NULL) 1314 return NULL; 1315 1316 new_req_or_option = (ReqOrOption *) xmalloc (sizeof (ReqOrOption)); 1317 new_req_or_option->option_name = xstrdup (req_or_option->option_name); 1318 new_req_or_option->is_true = req_or_option->is_true; 1319 new_req_or_option->next = NULL; 1320 new_req_or_option->next = clone_req_or_option_list (req_or_option->next); 1321 return new_req_or_option; 1322 } 1323 1324 1325 static ReqOption * 1326 clone_req_option_list (ReqOption *req_option) 1327 { 1328 ReqOption *new_req_option; 1329 1330 if (req_option == NULL) 1331 return NULL; 1332 1333 new_req_option = (ReqOption *) xmalloc (sizeof (ReqOption)); 1334 new_req_option->or_option_terms = NULL; 1335 new_req_option->next = NULL; 1336 new_req_option->or_option_terms = 1337 clone_req_or_option_list (req_option->or_option_terms); 1338 new_req_option->next = clone_req_option_list (req_option->next); 1339 return new_req_option; 1340 } 1341 1342 1343 static bfd_boolean 1344 parse_option_cond (const char *s, ReqOption *option) 1345 { 1346 int i; 1347 split_rec option_term_rec; 1348 1349 /* All option or conditions are of the form: 1350 optionA + no-optionB + ... 1351 "Ands" are divided by "?". */ 1352 1353 init_split_rec (&option_term_rec); 1354 split_string (&option_term_rec, s, '+', TRUE); 1355 1356 if (option_term_rec.count == 0) 1357 { 1358 clear_split_rec (&option_term_rec); 1359 return FALSE; 1360 } 1361 1362 for (i = 0; i < option_term_rec.count; i++) 1363 { 1364 char *option_name = option_term_rec.vec[i]; 1365 bfd_boolean is_true = TRUE; 1366 ReqOrOption *req; 1367 ReqOrOption **r_p; 1368 1369 if (strncmp (option_name, "no-", 3) == 0) 1370 { 1371 option_name = xstrdup (&option_name[3]); 1372 is_true = FALSE; 1373 } 1374 else 1375 option_name = xstrdup (option_name); 1376 1377 req = (ReqOrOption *) xmalloc (sizeof (ReqOrOption)); 1378 req->option_name = option_name; 1379 req->is_true = is_true; 1380 req->next = NULL; 1381 1382 /* Append to list. */ 1383 for (r_p = &option->or_option_terms; (*r_p) != NULL; 1384 r_p = &(*r_p)->next) 1385 ; 1386 (*r_p) = req; 1387 } 1388 return TRUE; 1389 } 1390 1391 1392 /* Parse a string like: 1393 "insn op1, op2, op3, op4 | op1 != op2 | op2 == op3 | op4 == 1". 1394 I.E., instruction "insn" with 4 operands where operand 1 and 2 are not 1395 the same and operand 2 and 3 are the same and operand 4 is 1. 1396 1397 or: 1398 1399 "insn op1 | op1 == 1 / density + boolean / no-useroption". 1400 i.e. instruction "insn" with 1 operands where operand 1 is 1 1401 when "density" or "boolean" options are available and 1402 "useroption" is not available. 1403 1404 Because the current implementation of this parsing scheme uses 1405 split_string, it requires that '|' and '?' are only used as 1406 delimiters for predicates and required options. */ 1407 1408 static bfd_boolean 1409 parse_insn_pattern (const char *in, insn_pattern *insn) 1410 { 1411 split_rec rec; 1412 split_rec optionrec; 1413 int i; 1414 1415 init_insn_pattern (insn); 1416 1417 init_split_rec (&optionrec); 1418 split_string (&optionrec, in, '?', TRUE); 1419 if (optionrec.count == 0) 1420 { 1421 clear_split_rec (&optionrec); 1422 return FALSE; 1423 } 1424 1425 init_split_rec (&rec); 1426 1427 split_string (&rec, optionrec.vec[0], '|', TRUE); 1428 1429 if (rec.count == 0) 1430 { 1431 clear_split_rec (&rec); 1432 clear_split_rec (&optionrec); 1433 return FALSE; 1434 } 1435 1436 if (!parse_insn_templ (rec.vec[0], &insn->t)) 1437 { 1438 clear_split_rec (&rec); 1439 clear_split_rec (&optionrec); 1440 return FALSE; 1441 } 1442 1443 for (i = 1; i < rec.count; i++) 1444 { 1445 precond_e *cond = (precond_e *) xmalloc (sizeof (precond_e)); 1446 1447 if (!parse_precond (rec.vec[i], cond)) 1448 { 1449 clear_split_rec (&rec); 1450 clear_split_rec (&optionrec); 1451 clear_insn_pattern (insn); 1452 return FALSE; 1453 } 1454 1455 /* Append the condition. */ 1456 *insn->preconds.tail = cond; 1457 insn->preconds.tail = &cond->next; 1458 } 1459 1460 for (i = 1; i < optionrec.count; i++) 1461 { 1462 /* Handle the option conditions. */ 1463 ReqOption **r_p; 1464 ReqOption *req_option = (ReqOption *) xmalloc (sizeof (ReqOption)); 1465 req_option->or_option_terms = NULL; 1466 req_option->next = NULL; 1467 1468 if (!parse_option_cond (optionrec.vec[i], req_option)) 1469 { 1470 clear_split_rec (&rec); 1471 clear_split_rec (&optionrec); 1472 clear_insn_pattern (insn); 1473 clear_req_option_list (&req_option); 1474 return FALSE; 1475 } 1476 1477 /* Append the condition. */ 1478 for (r_p = &insn->options; (*r_p) != NULL; r_p = &(*r_p)->next) 1479 ; 1480 1481 (*r_p) = req_option; 1482 } 1483 1484 clear_split_rec (&rec); 1485 clear_split_rec (&optionrec); 1486 return TRUE; 1487 } 1488 1489 1490 static bfd_boolean 1491 parse_insn_repl (const char *in, insn_repl *r_p) 1492 { 1493 /* This is a list of instruction templates separated by ';'. */ 1494 split_rec rec; 1495 int i; 1496 1497 split_string (&rec, in, ';', TRUE); 1498 1499 for (i = 0; i < rec.count; i++) 1500 { 1501 insn_repl_e *e = (insn_repl_e *) xmalloc (sizeof (insn_repl_e)); 1502 1503 e->next = NULL; 1504 1505 if (!parse_insn_templ (rec.vec[i], &e->t)) 1506 { 1507 free (e); 1508 clear_insn_repl (r_p); 1509 return FALSE; 1510 } 1511 *r_p->tail = e; 1512 r_p->tail = &e->next; 1513 } 1514 return TRUE; 1515 } 1516 1517 1518 static bfd_boolean 1519 transition_applies (insn_pattern *initial_insn, 1520 const char *from_string ATTRIBUTE_UNUSED, 1521 const char *to_string ATTRIBUTE_UNUSED) 1522 { 1523 ReqOption *req_option; 1524 1525 for (req_option = initial_insn->options; 1526 req_option != NULL; 1527 req_option = req_option->next) 1528 { 1529 ReqOrOption *req_or_option = req_option->or_option_terms; 1530 1531 if (req_or_option == NULL 1532 || req_or_option->next != NULL) 1533 continue; 1534 1535 if (strncmp (req_or_option->option_name, "IsaUse", 6) == 0) 1536 { 1537 bfd_boolean option_available = FALSE; 1538 char *option_name = req_or_option->option_name + 6; 1539 if (!strcmp (option_name, "DensityInstruction")) 1540 option_available = (XCHAL_HAVE_DENSITY == 1); 1541 else if (!strcmp (option_name, "L32R")) 1542 option_available = (XCHAL_HAVE_L32R == 1); 1543 else if (!strcmp (option_name, "Const16")) 1544 option_available = (XCHAL_HAVE_CONST16 == 1); 1545 else if (!strcmp (option_name, "Loops")) 1546 option_available = (XCHAL_HAVE_LOOPS == 1); 1547 else if (!strcmp (option_name, "WideBranches")) 1548 option_available 1549 = (XCHAL_HAVE_WIDE_BRANCHES == 1 && produce_flix == FLIX_ALL); 1550 else if (!strcmp (option_name, "PredictedBranches")) 1551 option_available 1552 = (XCHAL_HAVE_PREDICTED_BRANCHES == 1 1553 && produce_flix == FLIX_ALL); 1554 else if (!strcmp (option_name, "Booleans")) 1555 option_available = (XCHAL_HAVE_BOOLEANS == 1); 1556 else 1557 as_warn (_("invalid configuration option '%s' in transition rule '%s'"), 1558 req_or_option->option_name, from_string); 1559 if ((option_available ^ req_or_option->is_true) != 0) 1560 return FALSE; 1561 } 1562 else if (strcmp (req_or_option->option_name, "realnop") == 0) 1563 { 1564 bfd_boolean nop_available = 1565 (xtensa_opcode_lookup (xtensa_default_isa, "nop") 1566 != XTENSA_UNDEFINED); 1567 if ((nop_available ^ req_or_option->is_true) != 0) 1568 return FALSE; 1569 } 1570 } 1571 return TRUE; 1572 } 1573 1574 1575 static bfd_boolean 1576 wide_branch_opcode (const char *opcode_name, 1577 char *suffix, 1578 xtensa_opcode *popcode) 1579 { 1580 xtensa_isa isa = xtensa_default_isa; 1581 xtensa_opcode opcode; 1582 static char wbr_name_buf[20]; 1583 1584 if (strncmp (opcode_name, "WIDE.", 5) != 0) 1585 return FALSE; 1586 1587 strcpy (wbr_name_buf, opcode_name + 5); 1588 strcat (wbr_name_buf, suffix); 1589 opcode = xtensa_opcode_lookup (isa, wbr_name_buf); 1590 if (opcode != XTENSA_UNDEFINED) 1591 { 1592 *popcode = opcode; 1593 return TRUE; 1594 } 1595 1596 return FALSE; 1597 } 1598 1599 1600 static TransitionRule * 1601 build_transition (insn_pattern *initial_insn, 1602 insn_repl *replace_insns, 1603 const char *from_string, 1604 const char *to_string) 1605 { 1606 TransitionRule *tr = NULL; 1607 xtensa_opcode opcode; 1608 xtensa_isa isa = xtensa_default_isa; 1609 BuildInstr *literal_bi; 1610 1611 opname_map_e *op1; 1612 opname_map_e *op2; 1613 1614 precond_e *precond; 1615 insn_repl_e *r; 1616 1617 if (!wide_branch_opcode (initial_insn->t.opcode_name, ".w18", &opcode) 1618 && !wide_branch_opcode (initial_insn->t.opcode_name, ".w15", &opcode)) 1619 opcode = xtensa_opcode_lookup (isa, initial_insn->t.opcode_name); 1620 1621 if (opcode == XTENSA_UNDEFINED) 1622 { 1623 /* It is OK to not be able to translate some of these opcodes. */ 1624 return NULL; 1625 } 1626 1627 1628 if (xtensa_opcode_num_operands (isa, opcode) 1629 != insn_templ_operand_count (&initial_insn->t)) 1630 { 1631 /* This is also OK because there are opcodes that 1632 have different numbers of operands on different 1633 architecture variations. */ 1634 return NULL; 1635 } 1636 1637 tr = (TransitionRule *) xmalloc (sizeof (TransitionRule)); 1638 tr->opcode = opcode; 1639 tr->conditions = NULL; 1640 tr->to_instr = NULL; 1641 1642 /* Build the conditions. First, equivalent operand condition.... */ 1643 for (op1 = initial_insn->t.operand_map.head; op1 != NULL; op1 = op1->next) 1644 { 1645 for (op2 = op1->next; op2 != NULL; op2 = op2->next) 1646 { 1647 if (same_operand_name (op1, op2)) 1648 { 1649 append_value_condition (tr, OP_EQUAL, 1650 op1->operand_num, op2->operand_num); 1651 } 1652 } 1653 } 1654 1655 /* Now the condition that an operand value must be a constant.... */ 1656 for (op1 = initial_insn->t.operand_map.head; op1 != NULL; op1 = op1->next) 1657 { 1658 if (op_is_constant (op1)) 1659 { 1660 append_constant_value_condition (tr, 1661 OP_EQUAL, 1662 op1->operand_num, 1663 op_get_constant (op1)); 1664 } 1665 } 1666 1667 1668 /* Now add the explicit preconditions listed after the "|" in the spec. 1669 These are currently very limited, so we do a special case 1670 parse for them. We expect spaces, opname != opname. */ 1671 for (precond = initial_insn->preconds.head; 1672 precond != NULL; 1673 precond = precond->next) 1674 { 1675 op1 = NULL; 1676 op2 = NULL; 1677 1678 if (precond->opname1) 1679 { 1680 op1 = get_opmatch (&initial_insn->t.operand_map, precond->opname1); 1681 if (op1 == NULL) 1682 as_fatal (_("opcode '%s': no bound opname '%s' " 1683 "for precondition in '%s'"), 1684 xtensa_opcode_name (isa, opcode), 1685 precond->opname1, from_string); 1686 } 1687 1688 if (precond->opname2) 1689 { 1690 op2 = get_opmatch (&initial_insn->t.operand_map, precond->opname2); 1691 if (op2 == NULL) 1692 as_fatal (_("opcode '%s': no bound opname '%s' " 1693 "for precondition in %s"), 1694 xtensa_opcode_name (isa, opcode), 1695 precond->opname2, from_string); 1696 } 1697 1698 if (op1 == NULL && op2 == NULL) 1699 as_fatal (_("opcode '%s': precondition only contains " 1700 "constants in '%s'"), 1701 xtensa_opcode_name (isa, opcode), from_string); 1702 else if (op1 != NULL && op2 != NULL) 1703 append_value_condition (tr, precond->cmpop, 1704 op1->operand_num, op2->operand_num); 1705 else if (op2 == NULL) 1706 append_constant_value_condition (tr, precond->cmpop, 1707 op1->operand_num, precond->opval2); 1708 else 1709 append_constant_value_condition (tr, precond->cmpop, 1710 op2->operand_num, precond->opval1); 1711 } 1712 1713 tr->options = clone_req_option_list (initial_insn->options); 1714 1715 /* Generate the replacement instructions. Some of these 1716 "instructions" are actually labels and literals. There can be at 1717 most one literal and at most one label. A literal must be defined 1718 (e.g., "LITERAL %imm") before use (e.g., "%LITERAL"). The labels 1719 can be used before they are defined. Also there are a number of 1720 special operands (e.g., HI24S). */ 1721 1722 literal_bi = NULL; 1723 for (r = replace_insns->head; r != NULL; r = r->next) 1724 { 1725 BuildInstr *bi; 1726 const char *opcode_name; 1727 int operand_count; 1728 opname_map_e *op; 1729 const char *fn_name; 1730 const char *operand_arg_name; 1731 1732 bi = (BuildInstr *) xmalloc (sizeof (BuildInstr)); 1733 append_build_insn (tr, bi); 1734 1735 bi->opcode = XTENSA_UNDEFINED; 1736 bi->ops = NULL; 1737 bi->next = NULL; 1738 1739 opcode_name = r->t.opcode_name; 1740 operand_count = insn_templ_operand_count (&r->t); 1741 1742 if (strcmp (opcode_name, "LITERAL") == 0) 1743 { 1744 bi->typ = INSTR_LITERAL_DEF; 1745 if (operand_count != 1) 1746 as_fatal (_("expected one operand for generated literal")); 1747 literal_bi = bi; 1748 } 1749 else if (strcmp (opcode_name, "LABEL") == 0) 1750 { 1751 bi->typ = INSTR_LABEL_DEF; 1752 if (operand_count != 0) 1753 as_fatal (_("expected 0 operands for generated label")); 1754 } 1755 else 1756 { 1757 bi->typ = INSTR_INSTR; 1758 if (wide_branch_opcode (opcode_name, ".w18", &bi->opcode) 1759 || wide_branch_opcode (opcode_name, ".w15", &bi->opcode)) 1760 opcode_name = xtensa_opcode_name (isa, bi->opcode); 1761 else 1762 bi->opcode = xtensa_opcode_lookup (isa, opcode_name); 1763 1764 if (bi->opcode == XTENSA_UNDEFINED) 1765 { 1766 as_warn (_("invalid opcode '%s' in transition rule '%s'"), 1767 opcode_name, to_string); 1768 return NULL; 1769 } 1770 1771 /* Check for the right number of ops. */ 1772 if (xtensa_opcode_num_operands (isa, bi->opcode) 1773 != (int) operand_count) 1774 as_fatal (_("opcode '%s': replacement does not have %d ops"), 1775 opcode_name, 1776 xtensa_opcode_num_operands (isa, bi->opcode)); 1777 } 1778 1779 for (op = r->t.operand_map.head; op != NULL; op = op->next) 1780 { 1781 unsigned idnum; 1782 1783 if (op_is_constant (op)) 1784 append_constant_op (bi, op->operand_num, op_get_constant (op)); 1785 else if (strcmp (op->operand_name, "%LITERAL") == 0) 1786 { 1787 if (! literal_bi || ! literal_bi->ops || literal_bi->ops->next) 1788 as_fatal (_("opcode '%s': cannot find literal definition"), 1789 opcode_name); 1790 append_literal_op (bi, op->operand_num, 1791 literal_bi->ops->op_data); 1792 } 1793 else if (strcmp (op->operand_name, "%LABEL") == 0) 1794 append_label_op (bi, op->operand_num); 1795 else if (op->operand_name[0] == 'a' 1796 && parse_constant (op->operand_name + 1, &idnum)) 1797 append_constant_op (bi, op->operand_num, idnum); 1798 else if (op->operand_name[0] == '%') 1799 { 1800 opname_map_e *orig_op; 1801 orig_op = get_opmatch (&initial_insn->t.operand_map, 1802 op->operand_name); 1803 if (orig_op == NULL) 1804 as_fatal (_("opcode %s: unidentified operand '%s' in '%s'"), 1805 opcode_name, op->operand_name, to_string); 1806 append_field_op (bi, op->operand_num, orig_op->operand_num); 1807 } 1808 else if (strcmp (op->operand_name, "FREEREG") == 0) 1809 { 1810 append_user_fn_field_op (bi, op->operand_num, OP_FREEREG, 0); 1811 } 1812 else if (parse_special_fn (op->operand_name, 1813 &fn_name, &operand_arg_name)) 1814 { 1815 opname_map_e *orig_op; 1816 OpType typ = OP_CONSTANT; 1817 1818 if (strcmp (fn_name, "LOW8") == 0) 1819 typ = OP_OPERAND_LOW8; 1820 else if (strcmp (fn_name, "HI24S") == 0) 1821 typ = OP_OPERAND_HI24S; 1822 else if (strcmp (fn_name, "F32MINUS") == 0) 1823 typ = OP_OPERAND_F32MINUS; 1824 else if (strcmp (fn_name, "LOW16U") == 0) 1825 typ = OP_OPERAND_LOW16U; 1826 else if (strcmp (fn_name, "HI16U") == 0) 1827 typ = OP_OPERAND_HI16U; 1828 else 1829 as_fatal (_("unknown user-defined function %s"), fn_name); 1830 1831 orig_op = get_opmatch (&initial_insn->t.operand_map, 1832 operand_arg_name); 1833 if (orig_op == NULL) 1834 as_fatal (_("opcode %s: unidentified operand '%s' in '%s'"), 1835 opcode_name, op->operand_name, to_string); 1836 append_user_fn_field_op (bi, op->operand_num, 1837 typ, orig_op->operand_num); 1838 } 1839 else 1840 as_fatal (_("opcode %s: could not parse operand '%s' in '%s'"), 1841 opcode_name, op->operand_name, to_string); 1842 } 1843 } 1844 1845 return tr; 1846 } 1847 1848 1849 static TransitionTable * 1850 build_transition_table (const string_pattern_pair *transitions, 1851 int transition_count, 1852 transition_cmp_fn cmp) 1853 { 1854 TransitionTable *table = NULL; 1855 int num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa); 1856 int i, tnum; 1857 1858 if (table != NULL) 1859 return table; 1860 1861 /* Otherwise, build it now. */ 1862 table = (TransitionTable *) xmalloc (sizeof (TransitionTable)); 1863 table->num_opcodes = num_opcodes; 1864 table->table = 1865 (TransitionList **) xmalloc (sizeof (TransitionTable *) * num_opcodes); 1866 1867 for (i = 0; i < num_opcodes; i++) 1868 table->table[i] = NULL; 1869 1870 for (tnum = 0; tnum < transition_count; tnum++) 1871 { 1872 const char *from_string = transitions[tnum].pattern; 1873 const char *to_string = transitions[tnum].replacement; 1874 1875 insn_pattern initial_insn; 1876 insn_repl replace_insns; 1877 TransitionRule *tr; 1878 1879 init_insn_pattern (&initial_insn); 1880 if (!parse_insn_pattern (from_string, &initial_insn)) 1881 as_fatal (_("could not parse INSN_PATTERN '%s'"), from_string); 1882 1883 init_insn_repl (&replace_insns); 1884 if (!parse_insn_repl (to_string, &replace_insns)) 1885 as_fatal (_("could not parse INSN_REPL '%s'"), to_string); 1886 1887 if (transition_applies (&initial_insn, from_string, to_string)) 1888 { 1889 tr = build_transition (&initial_insn, &replace_insns, 1890 from_string, to_string); 1891 if (tr) 1892 append_transition (table, tr->opcode, tr, cmp); 1893 else 1894 { 1895 #if TENSILICA_DEBUG 1896 as_warn (_("could not build transition for %s => %s"), 1897 from_string, to_string); 1898 #endif 1899 } 1900 } 1901 1902 clear_insn_repl (&replace_insns); 1903 clear_insn_pattern (&initial_insn); 1904 } 1905 return table; 1906 } 1907 1908 1909 extern TransitionTable * 1911 xg_build_widen_table (transition_cmp_fn cmp) 1912 { 1913 static TransitionTable *table = NULL; 1914 if (table == NULL) 1915 table = build_transition_table (widen_spec_list, WIDEN_COUNT, cmp); 1916 return table; 1917 } 1918 1919 1920 extern TransitionTable * 1921 xg_build_simplify_table (transition_cmp_fn cmp) 1922 { 1923 static TransitionTable *table = NULL; 1924 if (table == NULL) 1925 table = build_transition_table (simplify_spec_list, SIMPLIFY_COUNT, cmp); 1926 return table; 1927 } 1928