00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019
00020 #include "sysdep.h"
00021 #include "opcode/tic80.h"
00022 #include "dis-asm.h"
00023
00024 static int length;
00025
00026 static void print_operand_bitnum PARAMS ((struct disassemble_info *, long));
00027 static void print_operand_condition_code PARAMS ((struct disassemble_info *, long));
00028 static void print_operand_control_register PARAMS ((struct disassemble_info *, long));
00029 static void print_operand_float PARAMS ((struct disassemble_info *, long));
00030 static void print_operand_integer PARAMS ((struct disassemble_info *, long));
00031 static void print_operand PARAMS ((struct disassemble_info *, long, unsigned long,
00032 const struct tic80_operand *, bfd_vma));
00033 static int print_one_instruction PARAMS ((struct disassemble_info *, bfd_vma,
00034 unsigned long, const struct tic80_opcode *));
00035 static int print_instruction PARAMS ((struct disassemble_info *, bfd_vma, unsigned long,
00036 const struct tic80_opcode *));
00037 static int fill_instruction PARAMS ((struct disassemble_info *, bfd_vma,
00038 unsigned long *));
00039
00040
00041
00042
00043
00044
00045
00046 static void
00047 print_operand_integer (info, value)
00048 struct disassemble_info *info;
00049 long value;
00050 {
00051 if ((value > 9999 || value < -9999))
00052 {
00053 (*info->fprintf_func) (info->stream, "%#lx", value);
00054 }
00055 else
00056 {
00057 (*info->fprintf_func) (info->stream, "%ld", value);
00058 }
00059 }
00060
00061
00062
00063
00064
00065 static void
00066 print_operand_float (info, value)
00067 struct disassemble_info *info;
00068 long value;
00069 {
00070 union { float f; long l; } fval;
00071
00072 fval.l = value;
00073 (*info->fprintf_func) (info->stream, "%g", fval.f);
00074 }
00075
00076 static void
00077 print_operand_control_register (info, value)
00078 struct disassemble_info *info;
00079 long value;
00080 {
00081 const char *tmp;
00082
00083 tmp = tic80_value_to_symbol (value, TIC80_OPERAND_CR);
00084 if (tmp != NULL)
00085 {
00086 (*info->fprintf_func) (info->stream, "%s", tmp);
00087 }
00088 else
00089 {
00090 (*info->fprintf_func) (info->stream, "%#lx", value);
00091 }
00092 }
00093
00094 static void
00095 print_operand_condition_code (info, value)
00096 struct disassemble_info *info;
00097 long value;
00098 {
00099 const char *tmp;
00100
00101 tmp = tic80_value_to_symbol (value, TIC80_OPERAND_CC);
00102 if (tmp != NULL)
00103 {
00104 (*info->fprintf_func) (info->stream, "%s", tmp);
00105 }
00106 else
00107 {
00108 (*info->fprintf_func) (info->stream, "%ld", value);
00109 }
00110 }
00111
00112 static void
00113 print_operand_bitnum (info, value)
00114 struct disassemble_info *info;
00115 long value;
00116 {
00117 int bitnum;
00118 const char *tmp;
00119
00120 bitnum = ~value & 0x1F;
00121 tmp = tic80_value_to_symbol (bitnum, TIC80_OPERAND_BITNUM);
00122 if (tmp != NULL)
00123 {
00124 (*info->fprintf_func) (info->stream, "%s", tmp);
00125 }
00126 else
00127 {
00128 (*info->fprintf_func) (info->stream, "%ld", bitnum);
00129 }
00130 }
00131
00132
00133
00134 #define M_SI(insn,op) ((((op)->flags & TIC80_OPERAND_M_SI) != 0) && ((insn) & (1 << 17)))
00135 #define M_LI(insn,op) ((((op)->flags & TIC80_OPERAND_M_LI) != 0) && ((insn) & (1 << 15)))
00136 #define R_SCALED(insn,op) ((((op)->flags & TIC80_OPERAND_SCALED) != 0) && ((insn) & (1 << 11)))
00137
00138 static void
00139 print_operand (info, value, insn, operand, memaddr)
00140 struct disassemble_info *info;
00141 long value;
00142 unsigned long insn;
00143 const struct tic80_operand *operand;
00144 bfd_vma memaddr;
00145 {
00146 if ((operand->flags & TIC80_OPERAND_GPR) != 0)
00147 {
00148 (*info->fprintf_func) (info->stream, "r%ld", value);
00149 if (M_SI (insn, operand) || M_LI (insn, operand))
00150 {
00151 (*info->fprintf_func) (info->stream, ":m");
00152 }
00153 }
00154 else if ((operand->flags & TIC80_OPERAND_FPA) != 0)
00155 {
00156 (*info->fprintf_func) (info->stream, "a%ld", value);
00157 }
00158 else if ((operand->flags & TIC80_OPERAND_PCREL) != 0)
00159 {
00160 (*info->print_address_func) (memaddr + 4 * value, info);
00161 }
00162 else if ((operand->flags & TIC80_OPERAND_BASEREL) != 0)
00163 {
00164 (*info->print_address_func) (value, info);
00165 }
00166 else if ((operand->flags & TIC80_OPERAND_BITNUM) != 0)
00167 {
00168 print_operand_bitnum (info, value);
00169 }
00170 else if ((operand->flags & TIC80_OPERAND_CC) != 0)
00171 {
00172 print_operand_condition_code (info, value);
00173 }
00174 else if ((operand->flags & TIC80_OPERAND_CR) != 0)
00175 {
00176 print_operand_control_register (info, value);
00177 }
00178 else if ((operand->flags & TIC80_OPERAND_FLOAT) != 0)
00179 {
00180 print_operand_float (info, value);
00181 }
00182 else if ((operand->flags & TIC80_OPERAND_BITFIELD))
00183 {
00184 (*info->fprintf_func) (info->stream, "%#lx", value);
00185 }
00186 else
00187 {
00188 print_operand_integer (info, value);
00189 }
00190
00191
00192
00193 if (R_SCALED (insn, operand))
00194 {
00195 (*info->fprintf_func) (info->stream, ":s");
00196 }
00197 }
00198
00199
00200
00201 static int
00202 print_one_instruction (info, memaddr, insn, opcode)
00203 struct disassemble_info *info;
00204 bfd_vma memaddr;
00205 unsigned long insn;
00206 const struct tic80_opcode *opcode;
00207 {
00208 const struct tic80_operand *operand;
00209 long value;
00210 int status;
00211 const unsigned char *opindex;
00212 int close_paren;
00213
00214 (*info->fprintf_func) (info->stream, "%-10s", opcode->name);
00215
00216 for (opindex = opcode->operands; *opindex != 0; opindex++)
00217 {
00218 operand = tic80_operands + *opindex;
00219
00220
00221 if (operand->extract)
00222 {
00223 value = (*operand->extract) (insn, (int *) NULL);
00224 }
00225 else if (operand->bits == 32)
00226 {
00227 status = fill_instruction (info, memaddr, (unsigned long *) &value);
00228 if (status == -1)
00229 {
00230 return (status);
00231 }
00232 }
00233 else
00234 {
00235 value = (insn >> operand->shift) & ((1 << operand->bits) - 1);
00236 if ((operand->flags & TIC80_OPERAND_SIGNED) != 0
00237 && (value & (1 << (operand->bits - 1))) != 0)
00238 {
00239 value -= 1 << operand->bits;
00240 }
00241 }
00242
00243
00244
00245
00246
00247 if ((operand->flags & TIC80_OPERAND_PARENS) == 0)
00248 {
00249 close_paren = 0;
00250 if (opindex != opcode->operands)
00251 {
00252 (*info->fprintf_func) (info->stream, ",");
00253 }
00254 }
00255 else
00256 {
00257 close_paren = 1;
00258 (*info->fprintf_func) (info->stream, "(");
00259 }
00260
00261 print_operand (info, value, insn, operand, memaddr);
00262
00263
00264
00265
00266 if (close_paren)
00267 {
00268 (*info->fprintf_func) (info->stream, ")");
00269 }
00270 }
00271 return (length);
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 #define TWO_INSN(insn) ((((insn) & (0x1F << 27)) != 0) && (((insn) & (0x1F << 22)) != 0))
00281
00282 static int
00283 print_instruction (info, memaddr, insn, vec_opcode)
00284 struct disassemble_info *info;
00285 bfd_vma memaddr;
00286 unsigned long insn;
00287 const struct tic80_opcode *vec_opcode;
00288 {
00289 const struct tic80_opcode *opcode;
00290 const struct tic80_opcode *opcode_end;
00291
00292
00293
00294
00295
00296
00297
00298 opcode_end = tic80_opcodes + tic80_num_opcodes;
00299 for (opcode = tic80_opcodes; opcode < opcode_end; opcode++)
00300 {
00301 if ((insn & opcode->mask) == opcode->opcode &&
00302 opcode != vec_opcode)
00303 {
00304 break;
00305 }
00306 }
00307
00308 if (opcode == opcode_end)
00309 {
00310
00311 (*info->fprintf_func) (info->stream, ".word %#08lx", insn);
00312 }
00313 else
00314 {
00315
00316 length = print_one_instruction (info, memaddr, insn, opcode);
00317 if (opcode->flags & TIC80_VECTOR && vec_opcode == NULL && TWO_INSN (insn))
00318 {
00319
00320
00321
00322 (*info->fprintf_func) (info->stream, " || ");
00323 length = print_instruction (info, memaddr, insn, opcode);
00324 }
00325 }
00326 return (length);
00327 }
00328
00329
00330
00331
00332
00333 static int
00334 fill_instruction (info, memaddr, insnp)
00335 struct disassemble_info *info;
00336 bfd_vma memaddr;
00337 unsigned long *insnp;
00338 {
00339 bfd_byte buffer[4];
00340 int status;
00341
00342
00343
00344 status = (*info->read_memory_func) (memaddr + length, buffer, 4, info);
00345 if (status != 0)
00346 {
00347 (*info->memory_error_func) (status, memaddr, info);
00348 return (-1);
00349 }
00350
00351
00352
00353
00354 length += 4;
00355 if (info->endian == BFD_ENDIAN_LITTLE)
00356 {
00357 *insnp = bfd_getl32 (buffer);
00358 }
00359 else if (info->endian == BFD_ENDIAN_BIG)
00360 {
00361 *insnp = bfd_getb32 (buffer);
00362 }
00363 else
00364 {
00365
00366 abort ();
00367 }
00368 return (0);
00369 }
00370
00371 int
00372 print_insn_tic80 (memaddr, info)
00373 bfd_vma memaddr;
00374 struct disassemble_info *info;
00375 {
00376 unsigned long insn;
00377 int status;
00378
00379 length = 0;
00380 info->bytes_per_line = 8;
00381 status = fill_instruction (info, memaddr, &insn);
00382 if (status != -1)
00383 {
00384 status = print_instruction (info, memaddr, insn, NULL);
00385 }
00386 return (status);
00387 }