Skip to content

Commit

Permalink
Initial support for stm8 pseudo ##decompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jun 11, 2024
1 parent 32565da commit 8e867df
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 38 deletions.
1 change: 1 addition & 0 deletions dist/plugins-cfg/plugins.def.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ lang.v
lang.vala
lang.zig
parse.6502_pseudo
parse.stm8_pseudo
parse.arm_pseudo
parse.att2intel
parse.avr_pseudo
Expand Down
12 changes: 6 additions & 6 deletions libr/arch/p/arm/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ static int replace(int argc, const char *argv[], char *newstr) {
{ 0, "asrs", "# = # >> #", { 1, 2, 3 } },
{ 0, "asr", "# = # >> #", { 1, 2, 3 } },
{ 0, "b", "goto #", { 1 } },
{ 0, "cbz", "if !# goto #", { 1, 2 } },
{ 0, "cbnz", "if # goto #", { 1, 2 } },
{ 0, "cbz", "if (!#) goto #", { 1, 2 } },
{ 0, "cbnz", "if (#) goto #", { 1, 2 } },
{ 0, "b.w", "goto #", { 1 } },
{ 0, "b.gt", "if (a > b) #", { 1 } },
{ 0, "b.le", "if (a <= b) #", { 1 } },
{ 0, "b.lt", "if (a < b) #", { 1 } },
{ 0, "b.ge", "if (a >= b) #", { 1 } },
{ 0, "b.gt", "if (a > b) goto #", { 1 } },
{ 0, "b.le", "if (a <= b) goto #", { 1 } },
{ 0, "b.lt", "if (a < b) goto #", { 1 } },
{ 0, "b.ge", "if (a >= b) goto #", { 1 } },
{ 0, "beq lr", "ifeq ret", {0} },
{ 0, "beq", "je #", { 1 } },
{ 0, "call", "# ()", { 1 } },
Expand Down
1 change: 1 addition & 0 deletions libr/arch/p/stm8.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
OBJ_STM8=p/stm8/plugin.o
OBJ_STM8+=p/stm8/pseudo.o

STATIC_OBJ+=$(OBJ_STM8)
TARGET_STM8=arch_stm8.$(EXT_SO)
Expand Down
48 changes: 25 additions & 23 deletions libr/arch/p/stm8/gmtdisas/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ char *stm8_disasm(ut64 pc, const ut8 *data, int size, unsigned int *type, ut64 *
r_strbuf_appendf (sb, "%s", ins.text);

if (n == 1) {
for (; n < ins.size; n++)
for (; n < ins.size; n++) {
oc[n+1] = *(block->data + cnt + n);
}
} else {
for (; n < ins.size; n++)
for (; n < ins.size; n++) {
oc[n] = *(block->data + cnt + n);
}
}
switch (ins.des) {
case STM8_NONE:
Expand Down Expand Up @@ -128,10 +130,10 @@ char *stm8_disasm(ut64 pc, const ut8 *data, int size, unsigned int *type, ut64 *
r_strbuf_appendf (sb, " 0x%02x%02x", oc[2], oc[3]);
break;
case STM8_PTR_X:
r_strbuf_append (sb, " (x)");
r_strbuf_append (sb, " [x]");
break;
case STM8_PTR_Y:
r_strbuf_append (sb, " (y)");
r_strbuf_append (sb, " [y]");
break;
case SHORTMEM_2:
r_strbuf_appendf (sb, " 0x%02x", oc[2]);
Expand Down Expand Up @@ -181,25 +183,25 @@ char *stm8_disasm(ut64 pc, const ut8 *data, int size, unsigned int *type, ut64 *
*jump = add + ins.size + n;
break;
case SHORTOFF_X_2:
r_strbuf_appendf (sb, " (0x%02x, x)", oc[2]);
r_strbuf_appendf (sb, " [x + 0x%02x]", oc[2]);
break;
case SHORTOFF_Y_2:
r_strbuf_appendf (sb, " (0x%02x, y)", oc[2]);
r_strbuf_appendf (sb, " [y + 0x%02x]", oc[2]);
break;
case SHORTOFF_SP_2:
r_strbuf_appendf (sb, " (0x%02x, sp)", oc[2]);
r_strbuf_appendf (sb, " [sp + 0x%02x]", oc[2]);
break;
case LONGOFF_X_23:
r_strbuf_appendf (sb, " (0x%02x%02x, x)", oc[2], oc[3]);
r_strbuf_appendf (sb, " [x + 0x%02x%02x]", oc[2], oc[3]);
break;
case LONGOFF_Y_23:
r_strbuf_appendf (sb, " (0x%02x%02x, y)", oc[2], oc[3]);
r_strbuf_appendf (sb, " [y + 0x%02x%02x]", oc[2], oc[3]);
break;
case EXTOFF_X_234:
r_strbuf_appendf (sb, " (0x%02x%02x%02x, x)", oc[2], oc[3], oc[4]);
r_strbuf_appendf (sb, " [x + 0x%02x%02x%02x]", oc[2], oc[3], oc[4]);
break;
case EXTOFF_Y_234:
r_strbuf_appendf (sb, " (0x%02x%02x%02x, y)", oc[2], oc[3], oc[4]);
r_strbuf_appendf (sb, " [y + 0x%02x%02x%02x]", oc[2], oc[3], oc[4]);
break;
case SHORTPTR_2:
r_strbuf_appendf (sb, " [0x%02x]", oc[2]);
Expand All @@ -208,16 +210,16 @@ char *stm8_disasm(ut64 pc, const ut8 *data, int size, unsigned int *type, ut64 *
r_strbuf_appendf (sb, " [0x%02x%02x]", oc[2], oc[3]);
break;
case SHORTPTR_OFF_X_2:
r_strbuf_appendf (sb, " ([0x%02x], x)", oc[2]);
r_strbuf_appendf (sb, " x + [0x%02x]", oc[2]);
break;
case SHORTPTR_OFF_Y_2:
r_strbuf_appendf (sb, " ([0x%02x], y)", oc[2]);
r_strbuf_appendf (sb, " y + [0x%02x]", oc[2]);
break;
case LONGPTR_OFF_X_23:
r_strbuf_appendf (sb, " ([0x%02x%02x], x)", oc[2], oc[3]);
r_strbuf_appendf (sb, " x + [0x%02x%02x]", oc[2], oc[3]);
break;
case LONGPTR_OFF_Y_23:
r_strbuf_appendf (sb, " ([0x%02x%02x], y)", oc[2], oc[3]);
r_strbuf_appendf (sb, " y + [0x%02x%02x]", oc[2], oc[3]);
break;
case LONGMEM_BIT_123:
// ioreg
Expand Down Expand Up @@ -310,25 +312,25 @@ char *stm8_disasm(ut64 pc, const ut8 *data, int size, unsigned int *type, ut64 *
*jump = add + ins.size + n;
break;
case SHORTOFF_X_2:
r_strbuf_appendf (sb, ", (0x%02x, x)", oc[2]);
r_strbuf_appendf (sb, ", [x + 0x%02x]", oc[2]);
break;
case SHORTOFF_Y_2:
r_strbuf_appendf (sb, ", (0x%02x, y)", oc[2]);
r_strbuf_appendf (sb, ", [y + 0x%02x]", oc[2]);
break;
case SHORTOFF_SP_2:
r_strbuf_appendf (sb, ", (0x%02x, sp)", oc[2]);
r_strbuf_appendf (sb, ", [sp + 0x%02x]", oc[2]);
break;
case LONGOFF_X_23:
r_strbuf_appendf (sb, ", (0x%02x%02x, x)", oc[2], oc[3]);
r_strbuf_appendf (sb, ", [x + 0x%02x%02x]", oc[2], oc[3]);
break;
case LONGOFF_Y_23:
r_strbuf_appendf (sb, ", (0x%02x%02x, y)", oc[2], oc[3]);
r_strbuf_appendf (sb, ", [y + 0x%02x%02x]", oc[2], oc[3]);
break;
case EXTOFF_X_234:
r_strbuf_appendf (sb, ", (0x%02x%02x%02x, x)", oc[2], oc[3], oc[4]);
r_strbuf_appendf (sb, ", [x + 0x%02x%02x%02x]", oc[2], oc[3], oc[4]);
break;
case EXTOFF_Y_234:
r_strbuf_appendf (sb, ", (0x%02x%02x%02x, y)", oc[2], oc[3], oc[4]);
r_strbuf_appendf (sb, ", [y + 0x%02x%02x%02x]", oc[2], oc[3], oc[4]);
break;
case SHORTPTR_2:
r_strbuf_appendf (sb, ", [0x%02x]", oc[2]);
Expand All @@ -337,7 +339,7 @@ char *stm8_disasm(ut64 pc, const ut8 *data, int size, unsigned int *type, ut64 *
r_strbuf_appendf (sb, ", [0x%02x%02x]", oc[2], oc[3]);
break;
case SHORTPTR_OFF_X_2:
r_strbuf_appendf (sb, ", ([0x%02x], x)", oc[2]);
r_strbuf_appendf (sb, ", [0x%02x] + x", oc[2]);
break;
case SHORTPTR_OFF_Y_2:
r_strbuf_appendf (sb, ", ([0x%02x], y)", oc[2]);
Expand Down
Loading

0 comments on commit 8e867df

Please sign in to comment.