Skip to content

Commit

Permalink
Better syntax for conditionals and switch/case constructions ##decomp…
Browse files Browse the repository at this point in the history
…iler
  • Loading branch information
radare authored and trufae committed Jun 11, 2024
1 parent 237f0c7 commit 7db2313
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
15 changes: 10 additions & 5 deletions libr/arch/p/arm/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ static int replace(int argc, const char *argv[], char *newstr) {
{ 0, "cbz", "if !# goto #", { 1, 2 } },
{ 0, "cbnz", "if # goto #", { 1, 2 } },
{ 0, "b.w", "goto #", { 1 } },
{ 0, "b.gt", "goto ifgt #", { 1 } },
{ 0, "b.le", "goto ifle #", { 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, "beq lr", "ifeq ret", {0} },
{ 0, "beq", "je #", { 1 } },
{ 0, "call", "# ()", { 1 } },
Expand All @@ -56,9 +58,12 @@ static int replace(int argc, const char *argv[], char *newstr) {
{ 0, "b.lo", "goto iflt #", { 1 } },
{ 0, "cmf", "if (# == #)", { 1, 2 } },
{ 0, "cmn", "if (# != #)", { 1, 2 } },
{ 0, "cmp", "if (# == #)", { 1, 2 } },
{ 0, "fcmp", "if (# == #)", { 1, 2 } },
{ 0, "tst", "if ((# & #) == 0)", { 1, 2 } },
{ 0, "cmp", "(a, b) = compare (#, #)", { 1, 2 } },
{ 0, "fcmp", "(a, b) = compare (#, #)", { 1, 2 } },
{ 0, "tst", "(a, b) = compare (#, #)", { 1, 2 } },
// { 0, "cmp", "if (# == #)", { 1, 2 } },
// { 0, "fcmp", "if (# == #)", { 1, 2 } },
//{ 0, "tst", "if ((# & #) == 0)", { 1, 2 } },
{ 4, "csel", "# = (#)? # : #", { 1, 4, 2, 3 } },
{ 2, "cset", "# = (#)? 1 : 0", { 1, 2 } },
{ 0, "dvf", "# = # / #", { 1, 2, 3 } },
Expand Down
27 changes: 24 additions & 3 deletions libr/core/pseudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
}
} else {
NEWLINE (bb->addr, indent);
PRINTF ("goto loc_0x%"PFMT64x, bb->fail);
PRINTF ("goto loc_0x%"PFMT64x";", bb->fail);
}
if (sdb_const_get (db, K_INDENT (bb->addr), 0)) {
// already analyzed, go pop and continue
Expand Down Expand Up @@ -549,10 +549,12 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
}
}
PRINTF ("goto loc_0x%"PFMT64x";", bb->fail);
#if 0
if (nindent != indent) {
NEWLINE (bb->addr, indent);
PRINTF ("} else {");
}
#endif
indent = nindent;
}
PRINTGOTO (bb->addr, gotoaddr);
Expand All @@ -577,7 +579,13 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
r_config_set_b (core->config, "scr.html", true);
}
s = r_str_replace (s, ";", "//", true);
s = r_str_replace (s, "goto ", "goto loc_", true);
char *lastgoto = strstr (s, "goto ");
if (lastgoto) {
if (!strchr (lastgoto, '\n')) {
*s = 0;
}
}
s = r_str_replace (s, "goto ", "// goto loc_", true);
if (show_addr) {
// indent with | or stgh
char *os = r_str_prefix_all (s, " ");
Expand Down Expand Up @@ -609,7 +617,20 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
} else {
NEWLINE (bb->addr, 1);
}
PRINTF ("loc_0x%08"PFMT64x": // orphan\n%s", bb->addr, s);
RFlagItem *fi = r_flag_get_i (core->flags, bb->addr);
if (fi && r_str_startswith (fi->name, "case.")) {
const char *val = r_str_lchr (fi->name, '.') + 1;
char *hex = r_str_newf ("0x%s", val);
int nval = r_num_get (NULL, hex);
free (hex);
if (IS_PRINTABLE (nval)) {
PRINTF ("loc_0x%08"PFMT64x": // case '%c'\n%s", bb->addr, nval, s);
} else {
PRINTF ("loc_0x%08"PFMT64x": // case %s\n%s", bb->addr, val, s);
}
} else {
PRINTF ("loc_0x%08"PFMT64x": // orphan\n%s", bb->addr, s);
}
ut64 nbbaddr = UT64_MAX;
if (iter->n) {
RAnalBlock *nbb = (RAnalBlock*)iter->n;
Expand Down
14 changes: 7 additions & 7 deletions test/db/cmd/cmd_pdc
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ int entry0 (int stack, int stack) {
si-
var = si - 0
if (var > 0) goto loc_0x402018 // unlikely
goto loc_0x402042
goto loc_0x402042;
// } while (?);
goto loc_0x402042
goto loc_0x402042;
}
return eax;
loc_0x402042:
Expand Down Expand Up @@ -171,12 +171,12 @@ int sym.func.100003a54 (int x0, int x1) {
x8 = [x8 + 0x60]
x9 = [x1 + 0x60] // arg2
x9 = [x9 + 0x60]
if (x8 == x9)
goto ifle 0x100003a74 // likely
goto loc_0x100003a6c
(a, b) = compare (x8, x9)
if (a <= b) 0x100003a74 // likely
goto loc_0x100003a6c;
loc_0x100003a74:
b.ge 0x100003a80 // likely
goto loc_0x100003a78
if (a >= b) 0x100003a80 // likely
goto loc_0x100003a78;
loc_0x100003a80:
x8 = x1 + 0x68 // arg2
x1 = x0 + 0x68 // arg1
Expand Down

0 comments on commit 7db2313

Please sign in to comment.