Rizin
unix-like reverse engineering framework and cli tools
parse_tms320_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.ti.com/lit/ug/spru732j/spru732j.pdf
10 #include "parse_common.c"
11 
12 static RzList *tms320_tokenize(const char *assembly, size_t length);
13 
14 static const RzPseudoGrammar tms320_lexicon[] = {
15  RZ_PSEUDO_DEFINE_GRAMMAR("add", "3 = 1 + 2"),
16  RZ_PSEUDO_DEFINE_GRAMMAR("addab", "3 = 1 + 2"),
17  RZ_PSEUDO_DEFINE_GRAMMAR("addah", "3 = 1 + 2"),
18  RZ_PSEUDO_DEFINE_GRAMMAR("addaw", "3 = 1 + 2"),
19  RZ_PSEUDO_DEFINE_GRAMMAR("addk", "2 += 1"),
20  RZ_PSEUDO_DEFINE_GRAMMAR("addu", "3 = 1 + 2"),
21  RZ_PSEUDO_DEFINE_GRAMMAR("addw", "3 = 1 + 2"),
22  RZ_PSEUDO_DEFINE_GRAMMAR("andn", "3 = 1 ~ 2"),
23  RZ_PSEUDO_DEFINE_GRAMMAR("avg2", "3 = 1 avg 2"),
24  RZ_PSEUDO_DEFINE_GRAMMAR("b", "goto 1"),
25  RZ_PSEUDO_DEFINE_GRAMMAR("band", "3 = 1 & 2"),
26  RZ_PSEUDO_DEFINE_GRAMMAR("clr", "4 = 2 .bitclear 1 .. 2"),
27  RZ_PSEUDO_DEFINE_GRAMMAR("cmpeq", "3 = 1 == 2"),
28  RZ_PSEUDO_DEFINE_GRAMMAR("cmpge", "3 = 1 >= 2"),
29  RZ_PSEUDO_DEFINE_GRAMMAR("cmpgtu", "3 = 1 > 2"),
30  RZ_PSEUDO_DEFINE_GRAMMAR("cmplt", "3 = 1 < 2"),
31  RZ_PSEUDO_DEFINE_GRAMMAR("ext", "4 = 2 ext 1 .. 3"),
32  RZ_PSEUDO_DEFINE_GRAMMAR("extu", "4 = 2 ext 1 .. 3"),
33  RZ_PSEUDO_DEFINE_GRAMMAR("ldb", "2 = (byte) 1"),
34  RZ_PSEUDO_DEFINE_GRAMMAR("ldbu", "2 = (byte) 1"),
35  RZ_PSEUDO_DEFINE_GRAMMAR("lddw", "2 = (word) 1"),
36  RZ_PSEUDO_DEFINE_GRAMMAR("ldh", "2 = (half) 1"),
37  RZ_PSEUDO_DEFINE_GRAMMAR("ldhu", "2 = (half) 1"),
38  RZ_PSEUDO_DEFINE_GRAMMAR("ldndw", "2 = 1"),
39  RZ_PSEUDO_DEFINE_GRAMMAR("ldnw", "2 = 1"),
40  RZ_PSEUDO_DEFINE_GRAMMAR("ldw", "2 = (word) 1"),
41  RZ_PSEUDO_DEFINE_GRAMMAR("max2", "3 = max(1, 2)"),
42  RZ_PSEUDO_DEFINE_GRAMMAR("mpy", "3 = 1 * 2"),
43  RZ_PSEUDO_DEFINE_GRAMMAR("mpy2", "3 = 1 * 2"),
44  RZ_PSEUDO_DEFINE_GRAMMAR("mpyh", "3 = 1 * 2"),
45  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhi", "3 = 1 * 2"),
46  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhir", "3 = 1 * 2"),
47  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhl", "3 = 1 * 2"),
48  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhl", "3 = 1 * 2"),
49  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhlu", "3 = 1 * 2"),
50  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhslu", "3 = 1 * 2"),
51  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhsu", "3 = 1 * 2"),
52  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhu", "3 = 1 * 2"),
53  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhul", "3 = 1 * 2"),
54  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhuls", "3 = 1 * 2"),
55  RZ_PSEUDO_DEFINE_GRAMMAR("mpyhus", "3 = 1 * 2"),
56  RZ_PSEUDO_DEFINE_GRAMMAR("mpylh", "3 = 1 * 2"),
57  RZ_PSEUDO_DEFINE_GRAMMAR("mpyli", "3 = 1 * 2"),
58  RZ_PSEUDO_DEFINE_GRAMMAR("mpylir", "3 = 1 * 2"),
59  RZ_PSEUDO_DEFINE_GRAMMAR("mpyluhs", "3 = 1 * 2"),
60  RZ_PSEUDO_DEFINE_GRAMMAR("mpysu", "3 = 1 * 2"),
61  RZ_PSEUDO_DEFINE_GRAMMAR("mpyu", "3 = 1 * 2"),
62  RZ_PSEUDO_DEFINE_GRAMMAR("mpyu4", "3 = 1 * 2"),
63  RZ_PSEUDO_DEFINE_GRAMMAR("mv", "2 = 1"),
64  RZ_PSEUDO_DEFINE_GRAMMAR("mvk", "2 = 1"),
65  RZ_PSEUDO_DEFINE_GRAMMAR("mvklh", "2 = (half) 1"),
66  RZ_PSEUDO_DEFINE_GRAMMAR("nop", ""),
67  RZ_PSEUDO_DEFINE_GRAMMAR("or", "3 = 2 | 1"),
68  RZ_PSEUDO_DEFINE_GRAMMAR("pack2", "3 = 1 pack 2"),
69  RZ_PSEUDO_DEFINE_GRAMMAR("reti", "return"),
70  RZ_PSEUDO_DEFINE_GRAMMAR("sadd", "3 = 1 + 2"),
71  RZ_PSEUDO_DEFINE_GRAMMAR("sadd2", "3 = 1 + 2"),
72  RZ_PSEUDO_DEFINE_GRAMMAR("set", "4 = 2 .bitset 1 .. 2"),
73  RZ_PSEUDO_DEFINE_GRAMMAR("shl", "3 = (2 & #0xffffff) << 1"),
74  RZ_PSEUDO_DEFINE_GRAMMAR("shlmb", "3 = << 1"),
75  RZ_PSEUDO_DEFINE_GRAMMAR("shr", "3 = (2 & #0xffffff) << 1"),
76  RZ_PSEUDO_DEFINE_GRAMMAR("smpy", "3 = 1 * 2"),
77  RZ_PSEUDO_DEFINE_GRAMMAR("smpy", "3 = 1 * 2"),
78  RZ_PSEUDO_DEFINE_GRAMMAR("smpyh", "3 = 1 * 2"),
79  RZ_PSEUDO_DEFINE_GRAMMAR("smpyhl", "3 = 1 * 2"),
80  RZ_PSEUDO_DEFINE_GRAMMAR("smpylh", "3 = 1 * 2"),
81  RZ_PSEUDO_DEFINE_GRAMMAR("ssub", "3 = 1 - 2"),
82  RZ_PSEUDO_DEFINE_GRAMMAR("stb", "2 = (byte) 1"),
83  RZ_PSEUDO_DEFINE_GRAMMAR("stdw", "2 = (half) 1"),
84  RZ_PSEUDO_DEFINE_GRAMMAR("sth", "2 = (half) 1"),
85  RZ_PSEUDO_DEFINE_GRAMMAR("stndw", "2 = (half) 1"),
86  RZ_PSEUDO_DEFINE_GRAMMAR("stnw", "2 = (word) 1"),
87  RZ_PSEUDO_DEFINE_GRAMMAR("stw", "2 = (word) 1"),
88  RZ_PSEUDO_DEFINE_GRAMMAR("sub", "3 = 1 - 2"),
89  RZ_PSEUDO_DEFINE_GRAMMAR("sub2", "3 = 1 - 2"),
90  RZ_PSEUDO_DEFINE_GRAMMAR("subab", "3 = 1 - 2"),
91  RZ_PSEUDO_DEFINE_GRAMMAR("subu", "3 = 1 - 2"),
92  RZ_PSEUDO_DEFINE_GRAMMAR("zero", "1 = 0"),
93 };
94 
96 
97 RzList *tms320_tokenize(const char *assembly, size_t length) {
98  char *buf = NULL, *sp = NULL;
99  RzList *tokens = NULL;
100  buf = rz_str_ndup(assembly, length);
101  if (!buf) {
102  return NULL;
103  }
104 
105  sp = strchr(buf, ' ');
106  if (sp) {
107  *sp = ',';
108  }
109 
110  tokens = rz_str_split_duplist(buf, ",", true);
111  free(buf);
112 
113  return tokens;
114 }
115 
116 static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb) {
117  return rz_pseudo_convert(&tms320_config, assembly, sb);
118 }
119 
121  .name = "tms320.pseudo",
122  .desc = "tms320 pseudo syntax",
123  .parse = parse,
124 };
125 
126 #ifndef RZ_PLUGIN_INCORE
130  .version = RZ_VERSION
131 };
132 #endif
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
static bool rz_pseudo_convert(const RzPseudoConfig *config, const char *assembly, RzStrBuf *sb)
Definition: parse_common.c:103
#define RZ_PSEUDO_DEFINE_GRAMMAR(x, y)
Definition: parse_common.c:58
#define RZ_PSEUDO_DEFINE_CONFIG_ONLY_LEXICON(l, m, t)
Definition: parse_common.c:91
static RzList * tms320_tokenize(const char *assembly, size_t length)
RZ_API RzLibStruct rizin_plugin
static const RzPseudoConfig tms320_config
static const RzPseudoGrammar tms320_lexicon[]
static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb)
RzParsePlugin rz_parse_plugin_tms320_pseudo
@ 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 RZ_VERSION
Definition: rz_version.h:8
Definition: regcomp.c:57
static int sp
Definition: z80asm.c:91