Rizin
unix-like reverse engineering framework and cli tools
parse_avr_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 *avr_tokenize(const char *assembly, size_t length);
17 
18 static const RzPseudoGrammar avr_lexicon[] = {
19  RZ_PSEUDO_DEFINE_GRAMMAR("adc", "1 += 2 + carry"),
20  RZ_PSEUDO_DEFINE_GRAMMAR("add", "1 += 2"),
21  RZ_PSEUDO_DEFINE_GRAMMAR("adiw", "1+#1:1 += 2"),
22  RZ_PSEUDO_DEFINE_GRAMMAR("and", "1 &= 2"),
23  RZ_PSEUDO_DEFINE_GRAMMAR("andi", "1 &= 2"),
24  RZ_PSEUDO_DEFINE_GRAMMAR("asr", "1 >>= 1"),
25  RZ_PSEUDO_DEFINE_GRAMMAR("breq", "if(!var) goto 1"),
26  RZ_PSEUDO_DEFINE_GRAMMAR("brge", "if(var >= 0) goto 1"),
27  RZ_PSEUDO_DEFINE_GRAMMAR("brlo", "if(var < 0) goto 1"),
28  RZ_PSEUDO_DEFINE_GRAMMAR("brlt", "if(var < 0) goto 1"),
29  RZ_PSEUDO_DEFINE_GRAMMAR("brmi", "if(var < 0) goto 1"),
30  RZ_PSEUDO_DEFINE_GRAMMAR("brne", "if(var) goto 1"),
31  RZ_PSEUDO_DEFINE_GRAMMAR("brpl", "if(var > 0) goto 1"),
32  RZ_PSEUDO_DEFINE_GRAMMAR("brsh", "if(var >= 0) goto 1"),
33  RZ_PSEUDO_DEFINE_GRAMMAR("call", "goto 1"),
34  RZ_PSEUDO_DEFINE_GRAMMAR("cbr", "1 &= (#0xff - 2)"),
35  RZ_PSEUDO_DEFINE_GRAMMAR("clc", "c = #0"),
36  RZ_PSEUDO_DEFINE_GRAMMAR("clh", "h = #0"),
37  RZ_PSEUDO_DEFINE_GRAMMAR("cli", "i = #0"),
38  RZ_PSEUDO_DEFINE_GRAMMAR("cln", "n = #0"),
39  RZ_PSEUDO_DEFINE_GRAMMAR("clr", "1 ^= 1"),
40  RZ_PSEUDO_DEFINE_GRAMMAR("cls", "s = #0"),
41  RZ_PSEUDO_DEFINE_GRAMMAR("clt", "t = #0"),
42  RZ_PSEUDO_DEFINE_GRAMMAR("clv", "v = #0"),
43  RZ_PSEUDO_DEFINE_GRAMMAR("clz", "z = #0"),
44  RZ_PSEUDO_DEFINE_GRAMMAR("com", "1 = #0xff - 1"),
45  RZ_PSEUDO_DEFINE_GRAMMAR("cp", "var = 1 - 2"),
46  RZ_PSEUDO_DEFINE_GRAMMAR("cpc", "var = 1 - 2 - carry"),
47  RZ_PSEUDO_DEFINE_GRAMMAR("cpi", "var = 1 - 2"),
48  RZ_PSEUDO_DEFINE_GRAMMAR("cpse", "if(1 == 2)"),
49  RZ_PSEUDO_DEFINE_GRAMMAR("dec", "1--"),
50  RZ_PSEUDO_DEFINE_GRAMMAR("eor", "1 ^= 2"),
51  RZ_PSEUDO_DEFINE_GRAMMAR("halt", "_halt()"),
52  RZ_PSEUDO_DEFINE_GRAMMAR("icall", "goto z"),
53  RZ_PSEUDO_DEFINE_GRAMMAR("ijmp", "goto z"),
54  RZ_PSEUDO_DEFINE_GRAMMAR("in", "1 = 2"),
55  RZ_PSEUDO_DEFINE_GRAMMAR("inc", "1++"),
56  RZ_PSEUDO_DEFINE_GRAMMAR("iret", "return_interrupt()"),
57  RZ_PSEUDO_DEFINE_GRAMMAR("jmp", "goto 1"),
58  RZ_PSEUDO_DEFINE_GRAMMAR("ld", "1 = *(2)"),
59  RZ_PSEUDO_DEFINE_GRAMMAR("ldd", "1 = *(2)"),
60  RZ_PSEUDO_DEFINE_GRAMMAR("ldi", "1 = 2"),
61  RZ_PSEUDO_DEFINE_GRAMMAR("lds", "1 = *(2)"),
62  RZ_PSEUDO_DEFINE_GRAMMAR("lpm", "r0 = z"),
63  RZ_PSEUDO_DEFINE_GRAMMAR("lsl", "1 <<= #1"),
64  RZ_PSEUDO_DEFINE_GRAMMAR("lsr", "1 >>= #1"),
65  RZ_PSEUDO_DEFINE_GRAMMAR("mov", "1 = 2"),
66  RZ_PSEUDO_DEFINE_GRAMMAR("movw", "1+#1:1 = 2+#1:2"),
67  RZ_PSEUDO_DEFINE_GRAMMAR("mul", "#r1:r0 = 1 * 2"),
68  RZ_PSEUDO_DEFINE_GRAMMAR("neg", "1 = -1"),
69  RZ_PSEUDO_DEFINE_GRAMMAR("nop", ""),
70  RZ_PSEUDO_DEFINE_GRAMMAR("or", "1 |= 2"),
71  RZ_PSEUDO_DEFINE_GRAMMAR("ori", "1 |= 2"),
72  RZ_PSEUDO_DEFINE_GRAMMAR("out", "1 = 2"),
73  RZ_PSEUDO_DEFINE_GRAMMAR("pop", "1 = pop()"),
74  RZ_PSEUDO_DEFINE_GRAMMAR("push", "push(1)"),
75  RZ_PSEUDO_DEFINE_GRAMMAR("rcall", "goto 1"),
76  RZ_PSEUDO_DEFINE_GRAMMAR("ret", "return"),
77  RZ_PSEUDO_DEFINE_GRAMMAR("rjmp", "goto 1"),
78  RZ_PSEUDO_DEFINE_GRAMMAR("rol", "1 = (1 << #1) | (1 >> #7)"),
79  RZ_PSEUDO_DEFINE_GRAMMAR("ror", "1 = (1 << #7) | (1 >> #1)"),
80  RZ_PSEUDO_DEFINE_GRAMMAR("sbc", "1 -= (2 + carry)"),
81  RZ_PSEUDO_DEFINE_GRAMMAR("sbci", "1 -= (2 + carry)"),
82  RZ_PSEUDO_DEFINE_GRAMMAR("sbiw", "1+#1:1 -= 2"),
83  RZ_PSEUDO_DEFINE_GRAMMAR("sbrc", "if((1 & (#1 << 2)) != #0)"),
84  RZ_PSEUDO_DEFINE_GRAMMAR("sbrs", "if((1 & (#1 << 2)) != #1)"),
85  RZ_PSEUDO_DEFINE_GRAMMAR("sbr", "1 |= 2"),
86  RZ_PSEUDO_DEFINE_GRAMMAR("sec", "c = #1"),
87  RZ_PSEUDO_DEFINE_GRAMMAR("seh", "h = #1"),
88  RZ_PSEUDO_DEFINE_GRAMMAR("sei", "i = #1"),
89  RZ_PSEUDO_DEFINE_GRAMMAR("sen", "n = #1"),
90  RZ_PSEUDO_DEFINE_GRAMMAR("ser", "1 = #0xff"),
91  RZ_PSEUDO_DEFINE_GRAMMAR("ses", "s = #1"),
92  RZ_PSEUDO_DEFINE_GRAMMAR("set", "t = #1"),
93  RZ_PSEUDO_DEFINE_GRAMMAR("sev", "v = #1"),
94  RZ_PSEUDO_DEFINE_GRAMMAR("sez", "z = #1"),
95  RZ_PSEUDO_DEFINE_GRAMMAR("st", "*(1) = 2"),
96  RZ_PSEUDO_DEFINE_GRAMMAR("std", "*(1) = 2"),
97  RZ_PSEUDO_DEFINE_GRAMMAR("sts", "*(1) = 2"),
98  RZ_PSEUDO_DEFINE_GRAMMAR("sub", "1 -= 2"),
99  RZ_PSEUDO_DEFINE_GRAMMAR("subi", "1 -= 2"),
100  RZ_PSEUDO_DEFINE_GRAMMAR("swap", "1 = ((1 & #0xf0) >> #4) | ((1 & #0x0f) << #4)"),
101  RZ_PSEUDO_DEFINE_GRAMMAR("tst", "1 &= 1"),
102  RZ_PSEUDO_DEFINE_GRAMMAR("wdr", "_watchdog_reset()"),
103 };
104 
106 
107 RzList *avr_tokenize(const char *assembly, size_t length) {
108  size_t i, p;
109  char *buf = NULL;
110  bool insert_zero = false;
111  RzList *tokens = NULL;
112 
113  buf = rz_str_ndup(assembly, length);
114  if (!buf) {
115  return NULL;
116  }
117 
118  for (i = 0, p = 0; p < length; ++i, ++p) {
119  if (buf[p] == ',') {
120  p++;
121  } else if (buf[p] == '(') {
122  buf[p] = ' ';
123  if (!IS_HEXCHAR(buf[p - 1])) {
124  p++;
125  insert_zero = true;
126  }
127  } else if (buf[p] == ')') {
128  buf[p] = 0;
129  }
130  if (p > i) {
131  buf[i] = buf[p];
132  }
133  }
134  buf[i] = 0;
135 
136  tokens = rz_str_split_duplist(buf, " ", true);
137  free(buf);
138  if (!tokens) {
139  return NULL;
140  }
141 
142  if (insert_zero) {
143  rz_list_insert(tokens, rz_list_length(tokens) - 1, strdup("0"));
144  }
145 
146  return tokens;
147 }
148 
149 static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb) {
150  return rz_pseudo_convert(&avr_config, assembly, sb);
151 }
152 
154  .name = "avr.pseudo",
155  .desc = "AVR pseudo syntax",
156  .parse = parse
157 };
158 
159 #ifndef RZ_PLUGIN_INCORE
163  .version = RZ_VERSION
164 };
165 #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 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")
static RzList * avr_tokenize(const char *assembly, size_t length)
static const RzPseudoGrammar avr_lexicon[]
RZ_API RzLibStruct rizin_plugin
RzParsePlugin rz_parse_plugin_avr_pseudo
static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb)
static const RzPseudoConfig avr_config
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
@ 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_VERSION
Definition: rz_version.h:8
Definition: regcomp.c:57