Rizin
unix-like reverse engineering framework and cli tools
parse_6502_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 *_6502_tokenize(const char *assembly, size_t length);
17 
18 static const RzPseudoGrammar _6502_lexicon[] = {
19  RZ_PSEUDO_DEFINE_GRAMMAR("adc", "a += (1 + 2)"),
20  RZ_PSEUDO_DEFINE_GRAMMAR("and", "a &= (1 + 2)"),
21  RZ_PSEUDO_DEFINE_GRAMMAR("asl", "a = 1 << #1"),
22  RZ_PSEUDO_DEFINE_GRAMMAR("bcc", "if (carry == 0) goto 1"),
23  RZ_PSEUDO_DEFINE_GRAMMAR("bcs", "if (carry != 0) goto 1"),
24  RZ_PSEUDO_DEFINE_GRAMMAR("beq", "if (eq) goto 1"),
25  RZ_PSEUDO_DEFINE_GRAMMAR("bmi", "if (lt) goto 1"),
26  RZ_PSEUDO_DEFINE_GRAMMAR("bne", "if (ne) goto 1"),
27  RZ_PSEUDO_DEFINE_GRAMMAR("bpl", "if (gt) goto 1"),
28  RZ_PSEUDO_DEFINE_GRAMMAR("brk", "break"),
29  RZ_PSEUDO_DEFINE_GRAMMAR("clc", "carry = 0"),
30  RZ_PSEUDO_DEFINE_GRAMMAR("cld", "decimal = 0"),
31  RZ_PSEUDO_DEFINE_GRAMMAR("cli", "interrupt = 0"),
32  RZ_PSEUDO_DEFINE_GRAMMAR("clv", "overflow = 0"),
33  RZ_PSEUDO_DEFINE_GRAMMAR("cmp", "cmp (1, 2)"),
34  RZ_PSEUDO_DEFINE_GRAMMAR("cpx", "cmp (x, 1)"),
35  RZ_PSEUDO_DEFINE_GRAMMAR("cpy", "cmp (y, 1)"),
36  RZ_PSEUDO_DEFINE_GRAMMAR("dcx", "x--"),
37  RZ_PSEUDO_DEFINE_GRAMMAR("dcy", "y--"),
38  RZ_PSEUDO_DEFINE_GRAMMAR("dec", "(1 + 2)--"),
39  RZ_PSEUDO_DEFINE_GRAMMAR("dex", "x--"),
40  RZ_PSEUDO_DEFINE_GRAMMAR("dey", "y--"),
41  RZ_PSEUDO_DEFINE_GRAMMAR("eor", "a ^= (1 + 2)"),
42  RZ_PSEUDO_DEFINE_GRAMMAR("inc", "(1 + 2)++"),
43  RZ_PSEUDO_DEFINE_GRAMMAR("inc", "1++"),
44  RZ_PSEUDO_DEFINE_GRAMMAR("inx", "x++"),
45  RZ_PSEUDO_DEFINE_GRAMMAR("iny", "y++"),
46  RZ_PSEUDO_DEFINE_GRAMMAR("jmp", "goto 1"),
47  RZ_PSEUDO_DEFINE_GRAMMAR("jsr", "1 ()"),
48  RZ_PSEUDO_DEFINE_GRAMMAR("lda", "a = (1 + 2)"),
49  RZ_PSEUDO_DEFINE_GRAMMAR("ldx", "x = (1 + 2)"),
50  RZ_PSEUDO_DEFINE_GRAMMAR("ldy", "y = (1 + 2)"),
51  RZ_PSEUDO_DEFINE_GRAMMAR("nop", ""),
52  RZ_PSEUDO_DEFINE_GRAMMAR("ora", "a |= (1 + 2)"),
53  RZ_PSEUDO_DEFINE_GRAMMAR("pha", "push a"),
54  RZ_PSEUDO_DEFINE_GRAMMAR("rti", "return"),
55  RZ_PSEUDO_DEFINE_GRAMMAR("rts", "return"),
56  RZ_PSEUDO_DEFINE_GRAMMAR("sbc", "a -= (1 + 2)"),
57  RZ_PSEUDO_DEFINE_GRAMMAR("sec", "carry = #1"),
58  RZ_PSEUDO_DEFINE_GRAMMAR("sed", "decimal = #1"),
59  RZ_PSEUDO_DEFINE_GRAMMAR("sei", "interrupt = #1"),
60  RZ_PSEUDO_DEFINE_GRAMMAR("sta", "[1 + 2] = a"),
61  RZ_PSEUDO_DEFINE_GRAMMAR("stx", "[1 + 2] = x"),
62  RZ_PSEUDO_DEFINE_GRAMMAR("sty", "[1 + 2] = y"),
63  RZ_PSEUDO_DEFINE_GRAMMAR("tax", "x = a"),
64  RZ_PSEUDO_DEFINE_GRAMMAR("tay", "y = a"),
65  RZ_PSEUDO_DEFINE_GRAMMAR("tsx", "x = s"),
66  RZ_PSEUDO_DEFINE_GRAMMAR("txa", "a = x"),
67  RZ_PSEUDO_DEFINE_GRAMMAR("txs", "s = x"),
68  RZ_PSEUDO_DEFINE_GRAMMAR("tya", "a = y"),
69 };
70 
72 
73 RzList *_6502_tokenize(const char *assembly, size_t length) {
74  size_t i, p;
75  char *buf = NULL;
76  bool insert_zero = false;
77  RzList *tokens = NULL;
78 
79  buf = rz_str_ndup(assembly, length);
80  if (!buf) {
81  return NULL;
82  }
83 
84  for (i = 0, p = 0; p < length; ++i, ++p) {
85  if (buf[p] == ',') {
86  buf[p] = ' ';
87  } else if (buf[p] == '#') {
88  p++;
89  } else if (buf[p] == '(') {
90  buf[p] = ' ';
91  if (!IS_HEXCHAR(buf[p - 1])) {
92  p++;
93  insert_zero = true;
94  }
95  } else if (buf[p] == ')') {
96  buf[p] = 0;
97  }
98  if (p > i) {
99  buf[i] = buf[p];
100  }
101  }
102  buf[i] = 0;
103 
104  tokens = rz_str_split_duplist(buf, " ", true);
105  free(buf);
106  if (!tokens) {
107  return NULL;
108  }
109 
110  if (insert_zero) {
111  rz_list_insert(tokens, rz_list_length(tokens) - 1, strdup("0"));
112  }
113 
114  return tokens;
115 }
116 
117 static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb) {
118  return rz_pseudo_convert(&_6502_config, assembly, sb);
119 }
120 
122  .name = "6502.pseudo",
123  .desc = "6502 pseudo syntax",
124  .parse = parse,
125 };
126 
127 #ifndef RZ_PLUGIN_INCORE
131  .version = RZ_VERSION
132 };
133 #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 const RzPseudoConfig _6502_config
static const RzPseudoGrammar _6502_lexicon[]
RZ_API RzLibStruct rizin_plugin
static RzList * _6502_tokenize(const char *assembly, size_t length)
static bool parse(RzParse *parse, const char *assembly, RzStrBuf *sb)
RzParsePlugin rz_parse_plugin_6502_pseudo
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