1*bb16d227Schristos/* M32R opcode support. -*- C -*- 2*bb16d227Schristos 3*bb16d227Schristos Copyright 1998, 1999, 2000, 2001, 2004, 2005, 2007, 2009 4*bb16d227Schristos Free Software Foundation, Inc. 5*bb16d227Schristos 6*bb16d227Schristos Contributed by Red Hat Inc; developed under contract from 7*bb16d227Schristos Mitsubishi Electric Corporation. 8*bb16d227Schristos 9*bb16d227Schristos This file is part of the GNU Binutils. 10*bb16d227Schristos 11*bb16d227Schristos This program is free software; you can redistribute it and/or modify 12*bb16d227Schristos it under the terms of the GNU General Public License as published by 13*bb16d227Schristos the Free Software Foundation; either version 3 of the License, or 14*bb16d227Schristos (at your option) any later version. 15*bb16d227Schristos 16*bb16d227Schristos This program is distributed in the hope that it will be useful, 17*bb16d227Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 18*bb16d227Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19*bb16d227Schristos GNU General Public License for more details. 20*bb16d227Schristos 21*bb16d227Schristos You should have received a copy of the GNU General Public License 22*bb16d227Schristos along with this program; if not, write to the Free Software 23*bb16d227Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 24*bb16d227Schristos MA 02110-1301, USA. */ 25*bb16d227Schristos 26*bb16d227Schristos 27*bb16d227Schristos/* This file is an addendum to m32r.cpu. Heavy use of C code isn't 28*bb16d227Schristos appropriate in .cpu files, so it resides here. This especially applies 29*bb16d227Schristos to assembly/disassembly where parsing/printing can be quite involved. 30*bb16d227Schristos Such things aren't really part of the specification of the cpu, per se, 31*bb16d227Schristos so .cpu files provide the general framework and .opc files handle the 32*bb16d227Schristos nitty-gritty details as necessary. 33*bb16d227Schristos 34*bb16d227Schristos Each section is delimited with start and end markers. 35*bb16d227Schristos 36*bb16d227Schristos <arch>-opc.h additions use: "-- opc.h" 37*bb16d227Schristos <arch>-opc.c additions use: "-- opc.c" 38*bb16d227Schristos <arch>-asm.c additions use: "-- asm.c" 39*bb16d227Schristos <arch>-dis.c additions use: "-- dis.c" 40*bb16d227Schristos <arch>-ibd.h additions use: "-- ibd.h" */ 41*bb16d227Schristos 42*bb16d227Schristos/* -- opc.h */ 43*bb16d227Schristos 44*bb16d227Schristos#undef CGEN_DIS_HASH_SIZE 45*bb16d227Schristos#define CGEN_DIS_HASH_SIZE 256 46*bb16d227Schristos#undef CGEN_DIS_HASH 47*bb16d227Schristos#if 0 48*bb16d227Schristos#define X(b) (((unsigned char *) (b))[0] & 0xf0) 49*bb16d227Schristos#define CGEN_DIS_HASH(buffer, value) \ 50*bb16d227Schristos(X (buffer) | \ 51*bb16d227Schristos (X (buffer) == 0x40 || X (buffer) == 0xe0 || X (buffer) == 0x60 || X (buffer) == 0x50 ? 0 \ 52*bb16d227Schristos : X (buffer) == 0x70 || X (buffer) == 0xf0 ? (((unsigned char *) (buffer))[0] & 0xf) \ 53*bb16d227Schristos : X (buffer) == 0x30 ? ((((unsigned char *) (buffer))[1] & 0x70) >> 4) \ 54*bb16d227Schristos : ((((unsigned char *) (buffer))[1] & 0xf0) >> 4))) 55*bb16d227Schristos#else 56*bb16d227Schristos#define CGEN_DIS_HASH(buffer, value) m32r_cgen_dis_hash (buffer, value) 57*bb16d227Schristosextern unsigned int m32r_cgen_dis_hash (const char *, CGEN_INSN_INT); 58*bb16d227Schristos#endif 59*bb16d227Schristos 60*bb16d227Schristos/* -- */ 61*bb16d227Schristos 62*bb16d227Schristos/* -- opc.c */ 63*bb16d227Schristosunsigned int 64*bb16d227Schristosm32r_cgen_dis_hash (const char * buf ATTRIBUTE_UNUSED, CGEN_INSN_INT value) 65*bb16d227Schristos{ 66*bb16d227Schristos unsigned int x; 67*bb16d227Schristos 68*bb16d227Schristos if (value & 0xffff0000) /* 32bit instructions. */ 69*bb16d227Schristos value = (value >> 16) & 0xffff; 70*bb16d227Schristos 71*bb16d227Schristos x = (value >> 8) & 0xf0; 72*bb16d227Schristos if (x == 0x40 || x == 0xe0 || x == 0x60 || x == 0x50) 73*bb16d227Schristos return x; 74*bb16d227Schristos 75*bb16d227Schristos if (x == 0x70 || x == 0xf0) 76*bb16d227Schristos return x | ((value >> 8) & 0x0f); 77*bb16d227Schristos 78*bb16d227Schristos if (x == 0x30) 79*bb16d227Schristos return x | ((value & 0x70) >> 4); 80*bb16d227Schristos else 81*bb16d227Schristos return x | ((value & 0xf0) >> 4); 82*bb16d227Schristos} 83*bb16d227Schristos 84*bb16d227Schristos/* -- */ 85*bb16d227Schristos 86*bb16d227Schristos/* -- asm.c */ 87*bb16d227Schristosstatic const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'"); 88*bb16d227Schristos 89*bb16d227Schristos/* Handle '#' prefixes (i.e. skip over them). */ 90*bb16d227Schristos 91*bb16d227Schristosstatic const char * 92*bb16d227Schristosparse_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 93*bb16d227Schristos const char **strp, 94*bb16d227Schristos int opindex ATTRIBUTE_UNUSED, 95*bb16d227Schristos long *valuep ATTRIBUTE_UNUSED) 96*bb16d227Schristos{ 97*bb16d227Schristos if (**strp == '#') 98*bb16d227Schristos ++*strp; 99*bb16d227Schristos return NULL; 100*bb16d227Schristos} 101*bb16d227Schristos 102*bb16d227Schristos/* Handle shigh(), high(). */ 103*bb16d227Schristos 104*bb16d227Schristosstatic const char * 105*bb16d227Schristosparse_hi16 (CGEN_CPU_DESC cd, 106*bb16d227Schristos const char **strp, 107*bb16d227Schristos int opindex, 108*bb16d227Schristos unsigned long *valuep) 109*bb16d227Schristos{ 110*bb16d227Schristos const char *errmsg; 111*bb16d227Schristos enum cgen_parse_operand_result result_type; 112*bb16d227Schristos bfd_vma value; 113*bb16d227Schristos 114*bb16d227Schristos if (**strp == '#') 115*bb16d227Schristos ++*strp; 116*bb16d227Schristos 117*bb16d227Schristos if (strncasecmp (*strp, "high(", 5) == 0) 118*bb16d227Schristos { 119*bb16d227Schristos *strp += 5; 120*bb16d227Schristos errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_ULO, 121*bb16d227Schristos & result_type, & value); 122*bb16d227Schristos if (**strp != ')') 123*bb16d227Schristos return MISSING_CLOSING_PARENTHESIS; 124*bb16d227Schristos ++*strp; 125*bb16d227Schristos if (errmsg == NULL 126*bb16d227Schristos && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) 127*bb16d227Schristos { 128*bb16d227Schristos value >>= 16; 129*bb16d227Schristos value &= 0xffff; 130*bb16d227Schristos } 131*bb16d227Schristos *valuep = value; 132*bb16d227Schristos return errmsg; 133*bb16d227Schristos } 134*bb16d227Schristos else if (strncasecmp (*strp, "shigh(", 6) == 0) 135*bb16d227Schristos { 136*bb16d227Schristos *strp += 6; 137*bb16d227Schristos errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_HI16_SLO, 138*bb16d227Schristos & result_type, & value); 139*bb16d227Schristos if (**strp != ')') 140*bb16d227Schristos return MISSING_CLOSING_PARENTHESIS; 141*bb16d227Schristos ++*strp; 142*bb16d227Schristos if (errmsg == NULL 143*bb16d227Schristos && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) 144*bb16d227Schristos { 145*bb16d227Schristos value += 0x8000; 146*bb16d227Schristos value >>= 16; 147*bb16d227Schristos value &= 0xffff; 148*bb16d227Schristos } 149*bb16d227Schristos *valuep = value; 150*bb16d227Schristos return errmsg; 151*bb16d227Schristos } 152*bb16d227Schristos 153*bb16d227Schristos return cgen_parse_unsigned_integer (cd, strp, opindex, valuep); 154*bb16d227Schristos} 155*bb16d227Schristos 156*bb16d227Schristos/* Handle low() in a signed context. Also handle sda(). 157*bb16d227Schristos The signedness of the value doesn't matter to low(), but this also 158*bb16d227Schristos handles the case where low() isn't present. */ 159*bb16d227Schristos 160*bb16d227Schristosstatic const char * 161*bb16d227Schristosparse_slo16 (CGEN_CPU_DESC cd, 162*bb16d227Schristos const char ** strp, 163*bb16d227Schristos int opindex, 164*bb16d227Schristos long * valuep) 165*bb16d227Schristos{ 166*bb16d227Schristos const char *errmsg; 167*bb16d227Schristos enum cgen_parse_operand_result result_type; 168*bb16d227Schristos bfd_vma value; 169*bb16d227Schristos 170*bb16d227Schristos if (**strp == '#') 171*bb16d227Schristos ++*strp; 172*bb16d227Schristos 173*bb16d227Schristos if (strncasecmp (*strp, "low(", 4) == 0) 174*bb16d227Schristos { 175*bb16d227Schristos *strp += 4; 176*bb16d227Schristos errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16, 177*bb16d227Schristos & result_type, & value); 178*bb16d227Schristos if (**strp != ')') 179*bb16d227Schristos return MISSING_CLOSING_PARENTHESIS; 180*bb16d227Schristos ++*strp; 181*bb16d227Schristos if (errmsg == NULL 182*bb16d227Schristos && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) 183*bb16d227Schristos value = ((value & 0xffff) ^ 0x8000) - 0x8000; 184*bb16d227Schristos *valuep = value; 185*bb16d227Schristos return errmsg; 186*bb16d227Schristos } 187*bb16d227Schristos 188*bb16d227Schristos if (strncasecmp (*strp, "sda(", 4) == 0) 189*bb16d227Schristos { 190*bb16d227Schristos *strp += 4; 191*bb16d227Schristos errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_SDA16, 192*bb16d227Schristos NULL, & value); 193*bb16d227Schristos if (**strp != ')') 194*bb16d227Schristos return MISSING_CLOSING_PARENTHESIS; 195*bb16d227Schristos ++*strp; 196*bb16d227Schristos *valuep = value; 197*bb16d227Schristos return errmsg; 198*bb16d227Schristos } 199*bb16d227Schristos 200*bb16d227Schristos return cgen_parse_signed_integer (cd, strp, opindex, valuep); 201*bb16d227Schristos} 202*bb16d227Schristos 203*bb16d227Schristos/* Handle low() in an unsigned context. 204*bb16d227Schristos The signedness of the value doesn't matter to low(), but this also 205*bb16d227Schristos handles the case where low() isn't present. */ 206*bb16d227Schristos 207*bb16d227Schristosstatic const char * 208*bb16d227Schristosparse_ulo16 (CGEN_CPU_DESC cd, 209*bb16d227Schristos const char **strp, 210*bb16d227Schristos int opindex, 211*bb16d227Schristos unsigned long *valuep) 212*bb16d227Schristos{ 213*bb16d227Schristos const char *errmsg; 214*bb16d227Schristos enum cgen_parse_operand_result result_type; 215*bb16d227Schristos bfd_vma value; 216*bb16d227Schristos 217*bb16d227Schristos if (**strp == '#') 218*bb16d227Schristos ++*strp; 219*bb16d227Schristos 220*bb16d227Schristos if (strncasecmp (*strp, "low(", 4) == 0) 221*bb16d227Schristos { 222*bb16d227Schristos *strp += 4; 223*bb16d227Schristos errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_M32R_LO16, 224*bb16d227Schristos & result_type, & value); 225*bb16d227Schristos if (**strp != ')') 226*bb16d227Schristos return MISSING_CLOSING_PARENTHESIS; 227*bb16d227Schristos ++*strp; 228*bb16d227Schristos if (errmsg == NULL 229*bb16d227Schristos && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) 230*bb16d227Schristos value &= 0xffff; 231*bb16d227Schristos *valuep = value; 232*bb16d227Schristos return errmsg; 233*bb16d227Schristos } 234*bb16d227Schristos 235*bb16d227Schristos return cgen_parse_unsigned_integer (cd, strp, opindex, valuep); 236*bb16d227Schristos} 237*bb16d227Schristos 238*bb16d227Schristos/* -- */ 239*bb16d227Schristos 240*bb16d227Schristos/* -- dis.c */ 241*bb16d227Schristos 242*bb16d227Schristos/* Print signed operands with '#' prefixes. */ 243*bb16d227Schristos 244*bb16d227Schristosstatic void 245*bb16d227Schristosprint_signed_with_hash_prefix (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 246*bb16d227Schristos void * dis_info, 247*bb16d227Schristos long value, 248*bb16d227Schristos unsigned int attrs ATTRIBUTE_UNUSED, 249*bb16d227Schristos bfd_vma pc ATTRIBUTE_UNUSED, 250*bb16d227Schristos int length ATTRIBUTE_UNUSED) 251*bb16d227Schristos{ 252*bb16d227Schristos disassemble_info *info = (disassemble_info *) dis_info; 253*bb16d227Schristos 254*bb16d227Schristos (*info->fprintf_func) (info->stream, "#"); 255*bb16d227Schristos (*info->fprintf_func) (info->stream, "%ld", value); 256*bb16d227Schristos} 257*bb16d227Schristos 258*bb16d227Schristos/* Print unsigned operands with '#' prefixes. */ 259*bb16d227Schristos 260*bb16d227Schristosstatic void 261*bb16d227Schristosprint_unsigned_with_hash_prefix (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 262*bb16d227Schristos void * dis_info, 263*bb16d227Schristos long value, 264*bb16d227Schristos unsigned int attrs ATTRIBUTE_UNUSED, 265*bb16d227Schristos bfd_vma pc ATTRIBUTE_UNUSED, 266*bb16d227Schristos int length ATTRIBUTE_UNUSED) 267*bb16d227Schristos{ 268*bb16d227Schristos disassemble_info *info = (disassemble_info *) dis_info; 269*bb16d227Schristos 270*bb16d227Schristos (*info->fprintf_func) (info->stream, "#"); 271*bb16d227Schristos (*info->fprintf_func) (info->stream, "0x%lx", value); 272*bb16d227Schristos} 273*bb16d227Schristos 274*bb16d227Schristos/* Handle '#' prefixes as operands. */ 275*bb16d227Schristos 276*bb16d227Schristosstatic void 277*bb16d227Schristosprint_hash (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, 278*bb16d227Schristos void * dis_info, 279*bb16d227Schristos long value ATTRIBUTE_UNUSED, 280*bb16d227Schristos unsigned int attrs ATTRIBUTE_UNUSED, 281*bb16d227Schristos bfd_vma pc ATTRIBUTE_UNUSED, 282*bb16d227Schristos int length ATTRIBUTE_UNUSED) 283*bb16d227Schristos{ 284*bb16d227Schristos disassemble_info *info = (disassemble_info *) dis_info; 285*bb16d227Schristos 286*bb16d227Schristos (*info->fprintf_func) (info->stream, "#"); 287*bb16d227Schristos} 288*bb16d227Schristos 289*bb16d227Schristos#undef CGEN_PRINT_INSN 290*bb16d227Schristos#define CGEN_PRINT_INSN my_print_insn 291*bb16d227Schristos 292*bb16d227Schristosstatic int 293*bb16d227Schristosmy_print_insn (CGEN_CPU_DESC cd, 294*bb16d227Schristos bfd_vma pc, 295*bb16d227Schristos disassemble_info *info) 296*bb16d227Schristos{ 297*bb16d227Schristos bfd_byte buffer[CGEN_MAX_INSN_SIZE]; 298*bb16d227Schristos bfd_byte *buf = buffer; 299*bb16d227Schristos int status; 300*bb16d227Schristos int buflen = (pc & 3) == 0 ? 4 : 2; 301*bb16d227Schristos int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG; 302*bb16d227Schristos bfd_byte *x; 303*bb16d227Schristos 304*bb16d227Schristos /* Read the base part of the insn. */ 305*bb16d227Schristos 306*bb16d227Schristos status = (*info->read_memory_func) (pc - ((!big_p && (pc & 3) != 0) ? 2 : 0), 307*bb16d227Schristos buf, buflen, info); 308*bb16d227Schristos if (status != 0) 309*bb16d227Schristos { 310*bb16d227Schristos (*info->memory_error_func) (status, pc, info); 311*bb16d227Schristos return -1; 312*bb16d227Schristos } 313*bb16d227Schristos 314*bb16d227Schristos /* 32 bit insn? */ 315*bb16d227Schristos x = (big_p ? &buf[0] : &buf[3]); 316*bb16d227Schristos if ((pc & 3) == 0 && (*x & 0x80) != 0) 317*bb16d227Schristos return print_insn (cd, pc, info, buf, buflen); 318*bb16d227Schristos 319*bb16d227Schristos /* Print the first insn. */ 320*bb16d227Schristos if ((pc & 3) == 0) 321*bb16d227Schristos { 322*bb16d227Schristos buf += (big_p ? 0 : 2); 323*bb16d227Schristos if (print_insn (cd, pc, info, buf, 2) == 0) 324*bb16d227Schristos (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG); 325*bb16d227Schristos buf += (big_p ? 2 : -2); 326*bb16d227Schristos } 327*bb16d227Schristos 328*bb16d227Schristos x = (big_p ? &buf[0] : &buf[1]); 329*bb16d227Schristos if (*x & 0x80) 330*bb16d227Schristos { 331*bb16d227Schristos /* Parallel. */ 332*bb16d227Schristos (*info->fprintf_func) (info->stream, " || "); 333*bb16d227Schristos *x &= 0x7f; 334*bb16d227Schristos } 335*bb16d227Schristos else 336*bb16d227Schristos (*info->fprintf_func) (info->stream, " -> "); 337*bb16d227Schristos 338*bb16d227Schristos /* The "& 3" is to pass a consistent address. 339*bb16d227Schristos Parallel insns arguably both begin on the word boundary. 340*bb16d227Schristos Also, branch insns are calculated relative to the word boundary. */ 341*bb16d227Schristos if (print_insn (cd, pc & ~ (bfd_vma) 3, info, buf, 2) == 0) 342*bb16d227Schristos (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG); 343*bb16d227Schristos 344*bb16d227Schristos return (pc & 3) ? 2 : 4; 345*bb16d227Schristos} 346*bb16d227Schristos 347*bb16d227Schristos/* -- */ 348