Rizin
unix-like reverse engineering framework and cli tools
parse_m68k_pseudo.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 
8 #include <rz_lib.h>
9 #include <rz_util.h>
10 #include <rz_flag.h>
11 #include <rz_analysis.h>
12 #include <rz_parse.h>
13 
14 #include "parse_common.c"
15 
16 static RzList *m68k_tokenize(const char *assembly, size_t length);
17 
18 static const RzPseudoGrammar m68k_lexicon[] = {
19  RZ_PSEUDO_DEFINE_GRAMMAR("add", "1 += 2"),
20  RZ_PSEUDO_DEFINE_GRAMMAR("adda", "1 += 2"),
21  RZ_PSEUDO_DEFINE_GRAMMAR("addi", "1 += 2"),
22  RZ_PSEUDO_DEFINE_GRAMMAR("addq", "1 += 2"),
23  RZ_PSEUDO_DEFINE_GRAMMAR("andi", "2 &= 1"),
24  RZ_PSEUDO_DEFINE_GRAMMAR("bcc", "if (cc) goto 1"),
25  RZ_PSEUDO_DEFINE_GRAMMAR("bcs", "if (cs) goto 1"),
26  RZ_PSEUDO_DEFINE_GRAMMAR("bne", "if (!=) goto 1"),
27  RZ_PSEUDO_DEFINE_GRAMMAR("beq", "if (==) goto 1"),
28  RZ_PSEUDO_DEFINE_GRAMMAR("bge", "if (>=) goto 1"),
29  RZ_PSEUDO_DEFINE_GRAMMAR("bgt", "if (>) goto 1"),
30  RZ_PSEUDO_DEFINE_GRAMMAR("ble", "if (<=) goto 1"),
31  RZ_PSEUDO_DEFINE_GRAMMAR("blt", "if (<) goto 1"),
32  RZ_PSEUDO_DEFINE_GRAMMAR("bra", "goto 1"),
33  RZ_PSEUDO_DEFINE_GRAMMAR("bsr", "call 1"),
34  RZ_PSEUDO_DEFINE_GRAMMAR("btst", "1 == 2"),
35  RZ_PSEUDO_DEFINE_GRAMMAR("cmp", "1 == 2"),
36  RZ_PSEUDO_DEFINE_GRAMMAR("cmpi", "2 == 1"),
37  RZ_PSEUDO_DEFINE_GRAMMAR("jmp", "goto 1"),
38  RZ_PSEUDO_DEFINE_GRAMMAR("jsr", "call 1"),
39  RZ_PSEUDO_DEFINE_GRAMMAR("lea", "2 = 1"),
40  RZ_PSEUDO_DEFINE_GRAMMAR("lsl", "2 <<= 1"),
41  RZ_PSEUDO_DEFINE_GRAMMAR("lsr", "2 >>= 1"),
42  RZ_PSEUDO_DEFINE_GRAMMAR("move", "2 = 1"),
43  RZ_PSEUDO_DEFINE_GRAMMAR("movea", "2 = 1"),
44  RZ_PSEUDO_DEFINE_GRAMMAR("movem", "2 = 1"),
45  RZ_PSEUDO_DEFINE_GRAMMAR("moveq", "2 = 1"),
46  RZ_PSEUDO_DEFINE_GRAMMAR("nop", ""),
47  RZ_PSEUDO_DEFINE_GRAMMAR("or", "2 |= 1"),
48  RZ_PSEUDO_DEFINE_GRAMMAR("ori", "2 |= 1"),
49  RZ_PSEUDO_DEFINE_GRAMMAR("rts", "ret"),
50  RZ_PSEUDO_DEFINE_GRAMMAR("sub", "1 += 2"),
51  RZ_PSEUDO_DEFINE_GRAMMAR("subq", "1 += 2"),
52  RZ_PSEUDO_DEFINE_GRAMMAR("tst", "1 == 0"),
53  RZ_PSEUDO_DEFINE_GRAMMAR("clr", "1 = 0"),
54 };
55 
56 static const RzPseudoReplace m68k_replace[] = {
57  RZ_PSEUDO_DEFINE_REPLACE("+ -", "- ", 1),
58 };
59 
61 
62 RzList *m68k_tokenize(const char *assembly, size_t length) {
63  size_t i, p;
64  char *buf = NULL;
65  RzList *tokens = NULL;
66 
67  buf = rz_str_ndup(assembly, length);
68  if (!buf) {
69  return NULL;
70  }
71 
72  for (i = 0, p = 0; p < length; ++i, ++p) {
73  if (buf[p] == ',') {
74  p++;
75  }
76  if (p > i) {
77  buf[i] = buf[p];
78  }
79  }
80  buf[i] = 0;
81 
82  tokens = rz_str_split_duplist(buf, " ", true);
83  free(buf);
84  return tokens;
85 }
86 
87 static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb) {
88  char *copy = strdup(assembly);
89  if (!copy) {
90  rz_strbuf_setf(sb, "asm(\"%s\")", assembly);
91  return true;
92  }
93  copy = rz_str_replace(copy, ".l", "", 0);
94  copy = rz_str_replace(copy, ".w", "", 0);
95  copy = rz_str_replace(copy, ".d", "", 0);
96  copy = rz_str_replace(copy, ".b", "", 0);
97  bool res = rz_pseudo_convert(&m68k_config, copy, sb);
98  free(copy);
99  return res;
100 }
101 
103  .name = "m68k.pseudo",
104  .desc = "M68K pseudo syntax",
105  .parse = parse,
106 };
107 
108 #ifndef RZ_PLUGIN_INCORE
112  .version = RZ_VERSION
113 };
114 #endif
lzma_index ** i
Definition: index.h:629
static SblHeader sb
Definition: bin_mbn.c:26
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
void * p
Definition: libc.cpp:67
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
#define RZ_PSEUDO_DEFINE_REPLACE(x, y, f)
Definition: parse_common.c:64
static bool rz_pseudo_convert(const RzPseudoConfig *config, const char *assembly, RzStrBuf *sb)
Definition: parse_common.c:103
#define RZ_PSEUDO_DEFINE_CONFIG_NO_DIRECT(l, r, m, t)
Definition: parse_common.c:79
#define RZ_PSEUDO_DEFINE_GRAMMAR(x, y)
Definition: parse_common.c:58
static RzList * m68k_tokenize(const char *assembly, size_t length)
static const RzPseudoReplace m68k_replace[]
RZ_API RzLibStruct rizin_plugin
static const RzPseudoConfig m68k_config
RzParsePlugin rz_parse_plugin_m68k_pseudo
static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb)
static const RzPseudoGrammar m68k_lexicon[]
@ RZ_LIB_TYPE_PARSE
Definition: rz_lib.h:74
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
RZ_API RzList * rz_str_split_duplist(const char *str, const char *c, bool trim)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3464
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110
RZ_API const char * rz_strbuf_setf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define RZ_VERSION
Definition: rz_version.h:8
Definition: regcomp.c:57