Rizin
unix-like reverse engineering framework and cli tools
parse_v850_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 <rz_lib.h>
5 #include <rz_flag.h>
6 #include <rz_analysis.h>
7 #include <rz_parse.h>
8 
9 // https://www.renesas.com/us/en/doc/products/mpumcu/doc/v850/r01us0037ej0100_v850e2.pdf
10 
11 #include "parse_common.c"
12 
13 static RzList *v850_tokenize(const char *assembly, size_t length);
14 
15 static const RzPseudoGrammar v850_lexicon[] = {
16  RZ_PSEUDO_DEFINE_GRAMMAR("add", "2 += 1"),
17  RZ_PSEUDO_DEFINE_GRAMMAR("addi", "3 = 2 + 1"),
18  RZ_PSEUDO_DEFINE_GRAMMAR("and", "3 = 2 & 1"),
19  RZ_PSEUDO_DEFINE_GRAMMAR("andi", "3 = 2 & 1"),
20  RZ_PSEUDO_DEFINE_GRAMMAR("clr1", "2 &= ~(#1 << 2)"),
21  RZ_PSEUDO_DEFINE_GRAMMAR("cmov", "4 == 1 ? 2 : 3"),
22  RZ_PSEUDO_DEFINE_GRAMMAR("cmp", "2 == 1"),
23  RZ_PSEUDO_DEFINE_GRAMMAR("di", "disable-interrupts"),
24  RZ_PSEUDO_DEFINE_GRAMMAR("divh", "2 /= 1"),
25  RZ_PSEUDO_DEFINE_GRAMMAR("divh", "3 = 2 / 1"),
26  RZ_PSEUDO_DEFINE_GRAMMAR("ei", "enable-interrupts"),
27  RZ_PSEUDO_DEFINE_GRAMMAR("jarl", "call 1 # 2"),
28  RZ_PSEUDO_DEFINE_GRAMMAR("jmp", "goto 1"),
29  RZ_PSEUDO_DEFINE_GRAMMAR("jr", "goto 1"),
30  RZ_PSEUDO_DEFINE_GRAMMAR("ld.b", "3 = (byte) *(2 + 1)"),
31  RZ_PSEUDO_DEFINE_GRAMMAR("ld.bu", "3 = (unsigned byte) *(2 + 1)"),
32  RZ_PSEUDO_DEFINE_GRAMMAR("ld.h", "3 = (Zhalf) *(2 + 1)"),
33  RZ_PSEUDO_DEFINE_GRAMMAR("ld.hu", "3 = (unsigned half) *(2 + 1)"),
34  RZ_PSEUDO_DEFINE_GRAMMAR("ld.w", "3 = (word) *(2 + 1)"),
35  RZ_PSEUDO_DEFINE_GRAMMAR("ldsr", "2 = 1"),
36  RZ_PSEUDO_DEFINE_GRAMMAR("mov", "2 = 1"),
37  RZ_PSEUDO_DEFINE_GRAMMAR("movea", "3 = 1 & 2"),
38  RZ_PSEUDO_DEFINE_GRAMMAR("movhi", "3 = (1 << XX) + 2"),
39  RZ_PSEUDO_DEFINE_GRAMMAR("mul", "3 = 2 * 1"),
40  RZ_PSEUDO_DEFINE_GRAMMAR("mulf.s", "3 = 2 * 1"),
41  RZ_PSEUDO_DEFINE_GRAMMAR("mulh", "2 *= 1"),
42  RZ_PSEUDO_DEFINE_GRAMMAR("not", "2 = ~1"),
43  RZ_PSEUDO_DEFINE_GRAMMAR("or", "3 = 2 | 1"),
44  RZ_PSEUDO_DEFINE_GRAMMAR("ori", "3 = 2 | 1"),
45  RZ_PSEUDO_DEFINE_GRAMMAR("reti", "return"),
46  RZ_PSEUDO_DEFINE_GRAMMAR("set1", "2 |= (#1 << 2)"),
47  RZ_PSEUDO_DEFINE_GRAMMAR("shl", "2 <<= 1"),
48  RZ_PSEUDO_DEFINE_GRAMMAR("shr", "2 >>= 1"),
49  RZ_PSEUDO_DEFINE_GRAMMAR("sld.b", "2 = (byte) 1"),
50  RZ_PSEUDO_DEFINE_GRAMMAR("sld.h", "2 = (half) 1"),
51  RZ_PSEUDO_DEFINE_GRAMMAR("sld.w", "2 = (word) 1"),
52  RZ_PSEUDO_DEFINE_GRAMMAR("sst.b", "2 = (byte) 1"),
53  RZ_PSEUDO_DEFINE_GRAMMAR("sst.h", "2 = (half) 1"),
54  RZ_PSEUDO_DEFINE_GRAMMAR("sst.w", "2 = (word) 1"),
55  RZ_PSEUDO_DEFINE_GRAMMAR("st.b", "*(3 + 2) = (byte) 1"),
56  RZ_PSEUDO_DEFINE_GRAMMAR("st.h", "*(3 + 2) = (half) 1"),
57  RZ_PSEUDO_DEFINE_GRAMMAR("st.w", "*(3 + 2) = (word) 1"),
58  RZ_PSEUDO_DEFINE_GRAMMAR("stsr", "2 = 1"),
59  RZ_PSEUDO_DEFINE_GRAMMAR("sub", "2 -= 1"),
60  RZ_PSEUDO_DEFINE_GRAMMAR("tst", "2 == 1"),
61  RZ_PSEUDO_DEFINE_GRAMMAR("tst1", "2 == 1"),
62  RZ_PSEUDO_DEFINE_GRAMMAR("xor", "2 ^= 1"),
63  RZ_PSEUDO_DEFINE_GRAMMAR("xori", "3 = 1 ^ 2"),
64  RZ_PSEUDO_DEFINE_GRAMMAR("zxb", "1 = 0"),
65  RZ_PSEUDO_DEFINE_GRAMMAR("zxh", "1 = 0"),
66  RZ_PSEUDO_DEFINE_GRAMMAR("zxw", "1 = 0"),
67 };
68 
69 static const RzPseudoReplace v850_replace[] = {
70  RZ_PSEUDO_DEFINE_REPLACE(" + 0)", ")", 0),
71  RZ_PSEUDO_DEFINE_REPLACE("+ -", "- ", 1),
72  RZ_PSEUDO_DEFINE_REPLACE(",", "", 1),
73 };
74 
76 
77 static const char *v850_short_op[] = {
78  "and",
79  "or",
80 };
81 
82 RzList *v850_tokenize(const char *assembly, size_t length) {
83  size_t i, p;
84  char *buf = NULL;
85  bool insert_zero = false;
86  RzList *tokens = NULL;
87 
88  buf = rz_str_ndup(assembly, length);
89  if (!buf) {
90  return NULL;
91  }
92 
93  for (i = 0, p = 0; p < length; ++i, ++p) {
94  if (buf[p] == ',') {
95  p++;
96  } else if (buf[p] == '[') {
97  buf[p] = ' ';
98  if (!IS_HEXCHAR(buf[p - 1])) {
99  p++;
100  insert_zero = true;
101  }
102  } else if (buf[p] == ']') {
103  buf[p] = ' ';
104  if (buf[p + 1] == ',') {
105  p++;
106  }
107  }
108  if (p > i) {
109  buf[i] = buf[p];
110  }
111  }
112  buf[i] = 0;
113 
114  tokens = rz_str_split_duplist(buf, " ", true);
115  free(buf);
116  if (!tokens) {
117  return NULL;
118  }
119 
120  buf = rz_list_first(tokens);
121  for (i = 0; i < RZ_ARRAY_SIZE(v850_short_op); ++i) {
122  if (!strcmp(buf, v850_short_op[i])) {
123  rz_list_insert(tokens, 1, strdup("0"));
124  break;
125  }
126  }
127 
128  if (insert_zero) {
129  rz_list_insert(tokens, rz_list_length(tokens) - 1, strdup("0"));
130  }
131 
132  return tokens;
133 }
134 
135 static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb) {
136  return rz_pseudo_convert(&v850_config, assembly, sb);
137 }
138 
140  .name = "v850.pseudo",
141  .desc = "v850 pseudo syntax",
142  .parse = parse,
143 };
144 
145 #ifndef RZ_PLUGIN_INCORE
149  .version = RZ_VERSION
150 };
151 #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
RZ_API RZ_BORROW RzListIter * rz_list_insert(RZ_NONNULL RzList *list, ut32 n, void *data)
Inserts a new element at the N-th position.
Definition: list.c:342
RZ_API RZ_BORROW void * rz_list_first(RZ_NONNULL const RzList *list)
Returns the first element of the list.
Definition: list.c:77
RZ_API ut32 rz_list_length(RZ_NONNULL const RzList *list)
Returns the length of the list.
Definition: list.c:109
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 const char * v850_short_op[]
static const RzPseudoConfig v850_config
RZ_API RzLibStruct rizin_plugin
RzParsePlugin rz_parse_plugin_v850_pseudo
static RzList * v850_tokenize(const char *assembly, size_t length)
static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb)
static const RzPseudoReplace v850_replace[]
static const RzPseudoGrammar v850_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
#define IS_HEXCHAR(x)
Definition: rz_str_util.h:9
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
#define RZ_VERSION
Definition: rz_version.h:8
Definition: regcomp.c:57