Rizin
unix-like reverse engineering framework and cli tools
amd29k.c File Reference
#include "amd29k.h"
#include "amd29k_internal.h"
#include <stdio.h>
#include <string.h>
#include <rz_analysis.h>

Go to the source code of this file.

Macros

#define CPU_ANY   "*"
 
#define N_AMD29K_INSTRUCTIONS   207
 
#define AMD29K_GET_TYPE(x, i)   ((x)->type[(i)])
 
#define AMD29K_GET_VALUE(x, i)   ((x)->operands[(i)])
 
#define AMD29K_SET_VALUE(x, i, v, t)
 
#define AMD29K_SET_INVALID(x, i)   ((x)->type[(i)] = AMD29K_TYPE_UNK)
 
#define AMD29K_HAS_BIT(x)   (((x)[0] & 1))
 
#define AMD29K_IS_REG_GR(x)   ((x) >= 0 && (x) < 128)
 
#define AMD29K_IS_REG_LR(x)   ((x) >= 128 && (x) < 256)
 
#define AMD29K_REGNAME(x)   (AMD29K_IS_REG_GR(x) ? "gr" : "lr")
 
#define AMD29K_LR(x)   (AMD29K_IS_REG_GR(x) ? (x) : (x)-127)
 
#define AMD29K_IS_6(a, b, c, d, e, f)   (t0 == (a) && t1 == (b) && t2 == (c) && t3 == (d) && t4 == (e) && t5 == (f))
 
#define AMD29K_IS_1(a)   AMD29K_IS_6(a, (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK))
 
#define AMD29K_IS_2(a, b)   AMD29K_IS_6(a, b, (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK))
 
#define AMD29K_IS_3(a, b, c)   AMD29K_IS_6(a, b, c, (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK))
 
#define AMD29K_IS_4(a, b, c, d)   AMD29K_IS_6(a, b, c, d, (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK))
 
#define AMD29K_IS_5(a, b, c, d, e)   AMD29K_IS_6(a, b, c, d, e, (AMD29K_TYPE_UNK))
 

Functions

static void decode_ra_rb_rci (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_ra_rb_rc (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_ra_imm16 (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_ra_i16_sh2 (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_imm16_sh2 (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_load_store (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_calli (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_rc_ra_imm (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_clz (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_convert (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_rc_ra (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_dmac_fmac (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_ra_rb (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_rb (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_rc_imm (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_ra_imm (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_mfsr (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_mtsr (amd29k_instr_t *instruction, const ut8 *buffer)
 
static void decode_none (amd29k_instr_t *instruction, const ut8 *buffer)
 
static bool is_cpu (const char *cpu, const amd29k_instruction_t *in)
 
bool amd29k_instr_decode (const ut8 *buffer, const ut32 buffer_size, amd29k_instr_t *instruction, const char *cpu)
 
bool amd29k_instr_is_ret (amd29k_instr_t *instruction)
 
ut64 amd29k_instr_jump (ut64 address, amd29k_instr_t *instruction)
 
void amd29k_instr_print (char *string, int string_size, ut64 address, amd29k_instr_t *instruction)
 

Variables

const amd29k_instruction_t amd29k_instructions [N_AMD29K_INSTRUCTIONS]
 

Macro Definition Documentation

◆ AMD29K_GET_TYPE

#define AMD29K_GET_TYPE (   x,
  i 
)    ((x)->type[(i)])

Definition at line 13 of file amd29k.c.

◆ AMD29K_GET_VALUE

#define AMD29K_GET_VALUE (   x,
  i 
)    ((x)->operands[(i)])

Definition at line 14 of file amd29k.c.

◆ AMD29K_HAS_BIT

#define AMD29K_HAS_BIT (   x)    (((x)[0] & 1))

Definition at line 19 of file amd29k.c.

◆ AMD29K_IS_1

Definition at line 467 of file amd29k.c.

◆ AMD29K_IS_2

#define AMD29K_IS_2 (   a,
  b 
)    AMD29K_IS_6(a, b, (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK))

Definition at line 468 of file amd29k.c.

◆ AMD29K_IS_3

#define AMD29K_IS_3 (   a,
  b,
  c 
)    AMD29K_IS_6(a, b, c, (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK))

Definition at line 469 of file amd29k.c.

◆ AMD29K_IS_4

#define AMD29K_IS_4 (   a,
  b,
  c,
  d 
)    AMD29K_IS_6(a, b, c, d, (AMD29K_TYPE_UNK), (AMD29K_TYPE_UNK))

Definition at line 470 of file amd29k.c.

◆ AMD29K_IS_5

#define AMD29K_IS_5 (   a,
  b,
  c,
  d,
  e 
)    AMD29K_IS_6(a, b, c, d, e, (AMD29K_TYPE_UNK))

Definition at line 471 of file amd29k.c.

◆ AMD29K_IS_6

#define AMD29K_IS_6 (   a,
  b,
  c,
  d,
  e,
  f 
)    (t0 == (a) && t1 == (b) && t2 == (c) && t3 == (d) && t4 == (e) && t5 == (f))

Definition at line 466 of file amd29k.c.

◆ AMD29K_IS_REG_GR

#define AMD29K_IS_REG_GR (   x)    ((x) >= 0 && (x) < 128)

Definition at line 21 of file amd29k.c.

◆ AMD29K_IS_REG_LR

#define AMD29K_IS_REG_LR (   x)    ((x) >= 128 && (x) < 256)

Definition at line 23 of file amd29k.c.

◆ AMD29K_LR

#define AMD29K_LR (   x)    (AMD29K_IS_REG_GR(x) ? (x) : (x)-127)

Definition at line 25 of file amd29k.c.

◆ AMD29K_REGNAME

#define AMD29K_REGNAME (   x)    (AMD29K_IS_REG_GR(x) ? "gr" : "lr")

Definition at line 24 of file amd29k.c.

◆ AMD29K_SET_INVALID

#define AMD29K_SET_INVALID (   x,
  i 
)    ((x)->type[(i)] = AMD29K_TYPE_UNK)

Definition at line 18 of file amd29k.c.

◆ AMD29K_SET_VALUE

#define AMD29K_SET_VALUE (   x,
  i,
  v,
 
)
Value:
((x)->operands[(i)] = (v)); \
((x)->type[(i)] = (t))
lzma_index ** i
Definition: index.h:629
const char * v
Definition: dsignal.c:12
int x
Definition: mipsasm.c:20
int type
Definition: mipsasm.c:17

Definition at line 15 of file amd29k.c.

◆ CPU_ANY

#define CPU_ANY   "*"

Definition at line 9 of file amd29k.c.

◆ N_AMD29K_INSTRUCTIONS

#define N_AMD29K_INSTRUCTIONS   207

Definition at line 11 of file amd29k.c.

Function Documentation

◆ amd29k_instr_decode()

bool amd29k_instr_decode ( const ut8 buffer,
const ut32  buffer_size,
amd29k_instr_t instruction,
const char *  cpu 
)

Definition at line 435 of file amd29k.c.

435  {
436  if (!buffer || buffer_size < 4 || !instruction || (cpu && strlen(cpu) < 5)) {
437  return false;
438  }
439  if (!cpu) {
440  cpu = CPU_29000;
441  }
442  if (buffer[0] == 0x70 && buffer[1] == 0x40 && buffer[2] == 0x01 && buffer[3] == 0x01) {
444  instruction->mnemonic = "nop";
446  return true;
447  }
448  int i;
449  for (i = 0; i < N_AMD29K_INSTRUCTIONS; i++) {
451  if (in->cpu[0] == '*' && in->mask == buffer[0]) {
452  in->decode(instruction, buffer);
453  instruction->mnemonic = in->mnemonic;
454  instruction->op_type = in->op_type;
455  return true;
456  } else if (in->cpu[0] != '*' && in->mask == buffer[0] && is_cpu(cpu, in)) {
457  in->decode(instruction, buffer);
458  instruction->mnemonic = in->mnemonic;
459  instruction->op_type = in->op_type;
460  return true;
461  }
462  }
463  return false;
464 }
static void decode_none(amd29k_instr_t *instruction, const ut8 *buffer)
Definition: amd29k.c:207
const amd29k_instruction_t amd29k_instructions[N_AMD29K_INSTRUCTIONS]
Definition: amd29k.c:217
#define N_AMD29K_INSTRUCTIONS
Definition: amd29k.c:11
static bool is_cpu(const char *cpu, const amd29k_instruction_t *in)
Definition: amd29k.c:427
#define CPU_29000
Definition: amd29k.h:13
static ut32 cpu[32]
Definition: analysis_or1k.c:21
const lzma_allocator const uint8_t * in
Definition: block.h:527
@ RZ_ANALYSIS_OP_TYPE_NOP
Definition: rz_analysis.h:389
Definition: buffer.h:15
#define buffer_size(buffer)

References amd29k_instructions, buffer_size, cpu, CPU_29000, decode_none(), i, in, is_cpu(), N_AMD29K_INSTRUCTIONS, and RZ_ANALYSIS_OP_TYPE_NOP.

Referenced by analop(), and disassemble().

◆ amd29k_instr_is_ret()

bool amd29k_instr_is_ret ( amd29k_instr_t instruction)

Definition at line 473 of file amd29k.c.

473  {
474  if (instruction && !strcmp(instruction->mnemonic, "calli") && instruction->operands[0] == 128 && instruction->operands[1] == 128) {
475  return true;
476  }
477  return false;
478 }

Referenced by analop().

◆ amd29k_instr_jump()

ut64 amd29k_instr_jump ( ut64  address,
amd29k_instr_t instruction 
)

Definition at line 480 of file amd29k.c.

480  {
481  if (!instruction) {
482  return UT64_MAX;
483  }
484  int t0 = AMD29K_GET_TYPE(instruction, 0);
485  int t1 = AMD29K_GET_TYPE(instruction, 1);
486  int t2 = AMD29K_GET_TYPE(instruction, 2);
487  int t3 = AMD29K_GET_TYPE(instruction, 3);
488  int t4 = AMD29K_GET_TYPE(instruction, 4);
489  int t5 = AMD29K_GET_TYPE(instruction, 5);
490 
491  int v0 = AMD29K_GET_VALUE(instruction, 0);
492  int v1 = AMD29K_GET_VALUE(instruction, 1);
494  return address + ((int)v0);
496  return address + ((int)v1);
497  }
498  return UT64_MAX;
499 }
#define AMD29K_IS_2(a, b)
Definition: amd29k.c:468
#define AMD29K_GET_VALUE(x, i)
Definition: amd29k.c:14
#define AMD29K_IS_1(a)
Definition: amd29k.c:467
#define AMD29K_GET_TYPE(x, i)
Definition: amd29k.c:13
@ AMD29K_TYPE_JMP
@ AMD29K_TYPE_REG
@ v0
Definition: lanai.h:84
@ v1
Definition: lanai.h:85
#define UT64_MAX
Definition: rz_types_base.h:86
static int
Definition: sfsocketcall.h:114

References AMD29K_GET_TYPE, AMD29K_GET_VALUE, AMD29K_IS_1, AMD29K_IS_2, AMD29K_TYPE_JMP, AMD29K_TYPE_REG, int, benchmark::t1, UT64_MAX, v0, and v1.

Referenced by analop().

◆ amd29k_instr_print()

void amd29k_instr_print ( char *  string,
int  string_size,
ut64  address,
amd29k_instr_t instruction 
)

Definition at line 501 of file amd29k.c.

501  {
502  if (!string || string_size < 0 || !instruction) {
503  return;
504  }
505  int t0 = AMD29K_GET_TYPE(instruction, 0);
506  int t1 = AMD29K_GET_TYPE(instruction, 1);
507  int t2 = AMD29K_GET_TYPE(instruction, 2);
508  int t3 = AMD29K_GET_TYPE(instruction, 3);
509  int t4 = AMD29K_GET_TYPE(instruction, 4);
510  int t5 = AMD29K_GET_TYPE(instruction, 5);
511 
512  int v0 = AMD29K_GET_VALUE(instruction, 0);
513  int v1 = AMD29K_GET_VALUE(instruction, 1);
514  int v2 = AMD29K_GET_VALUE(instruction, 2);
515  int v3 = AMD29K_GET_VALUE(instruction, 3);
516  int v4 = AMD29K_GET_VALUE(instruction, 4);
517  int v5 = AMD29K_GET_VALUE(instruction, 5);
518 
520  const char *p0 = AMD29K_REGNAME(v0);
521  snprintf(string, string_size, "%s %s%d", instruction->mnemonic, p0, AMD29K_LR(v0));
522 
523  } else if (AMD29K_IS_1(AMD29K_TYPE_IMM)) {
524  if (v0 >= 0) {
525  snprintf(string, string_size, "%s 0x%x", instruction->mnemonic, v0);
526  } else {
527  v0 = 0 - v0;
528  snprintf(string, string_size, "%s -0x%x", instruction->mnemonic, v0);
529  }
530 
531  } else if (AMD29K_IS_1(AMD29K_TYPE_JMP)) {
532  ut64 ptr = address + ((int)v0);
533  snprintf(string, string_size, "%s 0x%" PFMT64x, instruction->mnemonic, ptr);
534 
536  const char *p0 = AMD29K_REGNAME(v0);
537  const char *p1 = AMD29K_REGNAME(v1);
538  snprintf(string, string_size, "%s %s%d %s%d", instruction->mnemonic, p0, AMD29K_LR(v0), p1, AMD29K_LR(v1));
539 
541  const char *p0 = AMD29K_REGNAME(v0);
542  if (v1 >= 0) {
543  snprintf(string, string_size, "%s %s%d 0x%x", instruction->mnemonic, p0, AMD29K_LR(v0), v1);
544  } else {
545  v1 = 0 - v1;
546  snprintf(string, string_size, "%s %s%d -0x%x", instruction->mnemonic, p0, AMD29K_LR(v0), v1);
547  }
548 
550  const char *p0 = AMD29K_REGNAME(v0);
551  ut64 ptr = address + ((int)v1);
552  snprintf(string, string_size, "%s %s%d 0x%" PFMT64x, instruction->mnemonic, p0, AMD29K_LR(v0), ptr);
553 
555  const char *p0 = AMD29K_REGNAME(v0);
556  const char *p1 = AMD29K_REGNAME(v1);
557  const char *p2 = AMD29K_REGNAME(v2);
558  snprintf(string, string_size, "%s %s%d %s%d %s%d", instruction->mnemonic, p0, AMD29K_LR(v0), p1, AMD29K_LR(v1), p2, AMD29K_LR(v2));
559 
561  const char *p0 = AMD29K_REGNAME(v0);
562  const char *p1 = AMD29K_REGNAME(v1);
563  if (v2 >= 0) {
564  snprintf(string, string_size, "%s %s%d %s%d 0x%x", instruction->mnemonic, p0, AMD29K_LR(v0), p1, AMD29K_LR(v1), v2);
565  } else {
566  v2 = 0 - v2;
567  snprintf(string, string_size, "%s %s%d %s%d -0x%x", instruction->mnemonic, p0, AMD29K_LR(v0), p1, AMD29K_LR(v1), v2);
568  }
569 
571  const char *p2 = AMD29K_REGNAME(v2);
572  const char *p3 = AMD29K_REGNAME(v3);
573  snprintf(string, string_size, "%s %d %d %s%d %s%d", instruction->mnemonic, v0, v1, p2, AMD29K_LR(v2), p3, AMD29K_LR(v3));
574 
576  const char *p0 = AMD29K_REGNAME(v0);
577  const char *p1 = AMD29K_REGNAME(v1);
578  snprintf(string, string_size, "%s %s%d %s%d %d %d %d %d", instruction->mnemonic, p0, AMD29K_LR(v0), p1, AMD29K_LR(v1), v2, v3, v4, v5);
579 
580  } else {
581  snprintf(string, string_size, "%s", instruction->mnemonic);
582  }
583  return;
584 }
#define AMD29K_IS_4(a, b, c, d)
Definition: amd29k.c:470
#define AMD29K_LR(x)
Definition: amd29k.c:25
#define AMD29K_IS_3(a, b, c)
Definition: amd29k.c:469
#define AMD29K_REGNAME(x)
Definition: amd29k.c:24
#define AMD29K_IS_6(a, b, c, d, e, f)
Definition: amd29k.c:466
@ AMD29K_TYPE_IMM
snprintf
Definition: kernel.h:364
#define PFMT64x
Definition: rz_types.h:393
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References AMD29K_GET_TYPE, AMD29K_GET_VALUE, AMD29K_IS_1, AMD29K_IS_2, AMD29K_IS_3, AMD29K_IS_4, AMD29K_IS_6, AMD29K_LR, AMD29K_REGNAME, AMD29K_TYPE_IMM, AMD29K_TYPE_JMP, AMD29K_TYPE_REG, int, PFMT64x, snprintf, benchmark::t1, ut64(), v0, and v1.

Referenced by disassemble().

◆ decode_calli()

static void decode_calli ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

Definition at line 98 of file amd29k.c.

98  {
105 }
#define AMD29K_SET_VALUE(x, i, v, t)
Definition: amd29k.c:15
#define AMD29K_SET_INVALID(x, i)
Definition: amd29k.c:18

References AMD29K_SET_INVALID, AMD29K_SET_VALUE, and AMD29K_TYPE_REG.

◆ decode_clz()

static void decode_clz ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_convert()

static void decode_convert ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

Definition at line 125 of file amd29k.c.

125  {
126  // lambda w,ea: (w >> 24,[decode_byte1(w), decode_byte2(w), ('imm',False,(w&0x80)>>7), ('imm',False,(w&0x70)>>4), ('imm',False,(w&0xC)>>2), ('imm',False, w&3)])
129  AMD29K_SET_VALUE(instruction, 2, ((buffer[3] & 0x80) >> 7), AMD29K_TYPE_IMM);
130  AMD29K_SET_VALUE(instruction, 3, ((buffer[3] & 0x70) >> 4), AMD29K_TYPE_IMM);
131  AMD29K_SET_VALUE(instruction, 4, ((buffer[3] & 0x0c) >> 2), AMD29K_TYPE_IMM);
133 }

References AMD29K_SET_VALUE, AMD29K_TYPE_IMM, and AMD29K_TYPE_REG.

◆ decode_dmac_fmac()

static void decode_dmac_fmac ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_imm16_sh2()

static void decode_imm16_sh2 ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

Definition at line 76 of file amd29k.c.

76  {
77  int word = (buffer[1] << 10) + (buffer[3] << 2);
78  if (word & 0x20000) {
79  word = (int)(0xfffc0000 | word);
80  }
87 }

References AMD29K_HAS_BIT, AMD29K_SET_INVALID, AMD29K_SET_VALUE, AMD29K_TYPE_IMM, AMD29K_TYPE_JMP, and int.

◆ decode_load_store()

static void decode_load_store ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_mfsr()

static void decode_mfsr ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_mtsr()

static void decode_mtsr ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_none()

static void decode_none ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

Definition at line 207 of file amd29k.c.

207  {
208  // lambda w,ea: (w >> 24, None)
215 }

References AMD29K_SET_INVALID.

Referenced by amd29k_instr_decode().

◆ decode_ra_i16_sh2()

static void decode_ra_i16_sh2 ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

Definition at line 59 of file amd29k.c.

59  {
60  int word = (buffer[1] << 10) + (buffer[3] << 2);
61  if (word & 0x20000) {
62  word = (int)(0xfffc0000 | word);
63  }
65  if (AMD29K_HAS_BIT(buffer)) {
67  } else {
69  }
74 }
uint32_t ut32

References AMD29K_HAS_BIT, AMD29K_SET_INVALID, AMD29K_SET_VALUE, AMD29K_TYPE_IMM, AMD29K_TYPE_JMP, AMD29K_TYPE_REG, and int.

◆ decode_ra_imm()

static void decode_ra_imm ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_ra_imm16()

static void decode_ra_imm16 ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_ra_rb()

static void decode_ra_rb ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_ra_rb_rc()

static void decode_ra_rb_rc ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_ra_rb_rci()

static void decode_ra_rb_rci ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_rb()

static void decode_rb ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_rc_imm()

static void decode_rc_imm ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_rc_ra()

static void decode_rc_ra ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ decode_rc_ra_imm()

static void decode_rc_ra_imm ( amd29k_instr_t instruction,
const ut8 buffer 
)
static

◆ is_cpu()

static bool is_cpu ( const char *  cpu,
const amd29k_instruction_t in 
)
static

Definition at line 427 of file amd29k.c.

427  {
428  return cpu[0] == in->cpu[0] &&
429  cpu[1] == in->cpu[1] &&
430  cpu[2] == in->cpu[2] &&
431  cpu[3] == in->cpu[3] &&
432  cpu[4] == in->cpu[4];
433 }

References cpu, and in.

Referenced by amd29k_instr_decode().

Variable Documentation

◆ amd29k_instructions

Definition at line 217 of file amd29k.c.

Referenced by amd29k_instr_decode().