1658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat/*
2658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * Copyright (C) 2013 The Android Open Source Project
3658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * All rights reserved.
4658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *
5658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * Redistribution and use in source and binary forms, with or without
6658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * modification, are permitted provided that the following conditions
7658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * are met:
8658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *  * Redistributions of source code must retain the above copyright
9658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *    notice, this list of conditions and the following disclaimer.
10658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *  * Redistributions in binary form must reproduce the above copyright
11658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *    notice, this list of conditions and the following disclaimer in
12658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *    the documentation and/or other materials provided with the
13658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *    distribution.
14658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat *
15658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat * SUCH DAMAGE.
27658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat */
28658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
29658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat#include <stdio.h>
30658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat#include <inttypes.h>
31658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat#include <string.h>
32658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
33658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstruct disasm_table_entry_t
34658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
35658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    uint32_t       mask;
36658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    uint32_t       value;
37658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    const char*    instr_template;
38658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat};
39658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
40658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
41658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstatic disasm_table_entry_t disasm_table[] =
42658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
43658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff000000, 0x91000000, "add <xd|sp>, <xn|sp>, #<imm1>, <shift1>"},
44658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff000000, 0xd1000000, "sub <xd|sp>, <xn|sp>, #<imm1>, <shift1>"},
45658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff200000, 0x8b000000, "add <xd>, <xn>, <xm>, <shift2> #<amt1>"},
46658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff200000, 0x0b000000, "add <wd>, <wn>, <wm>, <shift2> #<amt1>"},
47658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff200000, 0x4b000000, "sub <wd>, <wn>, <wm>, <shift2> #<amt1>"},
48658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff200000, 0x6b000000, "subs <wd>, <wn>, <wm>, <shift2> #<amt1>"},
49658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff200000, 0x0a000000, "and <wd>, <wn>, <wm>, <shift2> #<amt1>"},
50658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff200000, 0x2a000000, "orr <wd>, <wn>, <wm>, <shift2> #<amt1>"},
51658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff200000, 0x2a200000, "orn <wd>, <wn>, <wm>, <shift2> #<amt1>"},
52658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff800000, 0x72800000, "movk <wd>, #<imm2>, lsl #<shift3>"},
53658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff800000, 0x52800000, "movz <wd>, #<imm2>, lsl #<shift3>"},
54658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff800000, 0xd2800000, "movz <xd>, #<imm2>, lsl #<shift3>"},
55658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00c00, 0x1a800000, "csel <wd>, <wn>, <wm>, <cond1>"},
56658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00c00, 0x9a800000, "csel <xd>, <xn>, <xm>, <cond1>"},
57658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00c00, 0x5a800000, "csinv <wd>, <wn>, <wm>, <cond1>"},
58658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe08000, 0x1b000000, "madd <wd>, <wn>, <wm>, <wa>"},
59658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe08000, 0x9b200000, "smaddl <xd>, <wn>, <wm>, <xa>"},
60658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0xb8604800, "ldr <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt2>]"},
61658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0xb8204800, "str <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt2>]"},
62658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0xf8604800, "ldr <xt>, [<xn|sp>, <r1><m1>, <ext1> #<amt3>]"},
63658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0xf8204800, "str <xt>, [<xn|sp>, <r1><m1>, <ext1> #<amt3>]"},
64658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0x38604800, "ldrb <wt>, [<xn|sp>, <r1><m1>, <ext1> <amt5>]"},
65658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0x38204800, "strb <wt>, [<xn|sp>, <r1><m1>, <ext1> <amt5>]"},
66658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0x78604800, "ldrh <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt6>]"},
67658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe04c00, 0x78204800, "strh <wt>, [<xn|sp>, <r1><m1>, <ext1> #<amt6>]"},
68658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00c00, 0xb8400400, "ldr <wt>, [<xn|sp>], #<simm1>"},
69658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00c00, 0xb8000c00, "str <wt>, [<xn|sp>, #<simm1>]!"},
70658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffc00000, 0x13000000, "sbfm <wd>, <wn>, #<immr1>, #<imms1>"},
71658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffc00000, 0x53000000, "ubfm <wd>, <wn>, #<immr1>, #<imms1>"},
72658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffc00000, 0xd3400000, "ubfm <xd>, <xn>, #<immr1>, #<imms1>"},
73658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00000, 0x13800000, "extr <wd>, <wn>, <wm>, #<lsb1>"},
74658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xff000000, 0x54000000, "b.<cond2> <label1>"},
75658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xfffffc1f, 0xd65f0000, "ret <xn>"},
76658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00000, 0x8b200000, "add <xd|sp>, <xn|sp>, <r2><m1>, <ext2> #<amt4>"},
77658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {0xffe00000, 0xcb200000, "sub <xd|sp>, <xn|sp>, <r2><m1>, <ext2> #<amt4>"}
78658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat};
79658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
80658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstatic int32_t bits_signed(uint32_t instr, uint32_t msb, uint32_t lsb)
81658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
82658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    int32_t value;
83658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    value   = ((int32_t)instr) << (31 - msb);
84658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    value >>= (31 - msb);
85658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    value >>= lsb;
86658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    return value;
87658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat}
88658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstatic uint32_t bits_unsigned(uint32_t instr, uint32_t msb, uint32_t lsb)
89658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
90658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    uint32_t width = msb - lsb + 1;
91658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    uint32_t mask  = (1 << width) - 1;
92658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    return ((instr >> lsb) & mask);
93658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat}
94658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
95658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstatic void get_token(const char *instr, uint32_t index, char *token)
96658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
97658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    uint32_t i, j;
98658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    for(i = index, j = 0; i < strlen(instr); ++i)
99658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
100658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        if(instr[index] == '<' && instr[i] == '>')
101658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
102658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            token[j++] = instr[i];
103658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            break;
104658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        }
105658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        else if(instr[index] != '<' && instr[i] == '<')
106658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
107658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            break;
108658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        }
109658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        else
110658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
111658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            token[j++] = instr[i];
112658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        }
113658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
114658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    token[j] = '\0';
115658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    return;
116658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat}
117658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
118658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
119658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstatic const char * token_cc_table[] =
120658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
121658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    "eq", "ne", "cs", "cc", "mi",
122658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    "pl", "vs", "vc", "hi", "ls",
123658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    "ge", "lt", "gt", "le", "al", "nv"
124658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat};
125658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
126658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstatic void decode_rx_zr_token(uint32_t reg, const char *prefix, char *instr_part)
127658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
128658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    if(reg == 31)
129658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s%s", prefix, "zr");
130658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else
131658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s%d", prefix, reg);
132658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat}
133658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
134658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhatstatic void decode_token(uint32_t code, char *token, char *instr_part)
135658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
136658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    if(strcmp(token, "<imm1>") == 0)
137658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "0x%x", bits_unsigned(code, 21,10));
138658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<imm2>") == 0)
139658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "0x%x", bits_unsigned(code, 20,5));
140658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<shift1>") == 0)
141658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "lsl #%d", bits_unsigned(code, 23,22) * 12);
142658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<shift2>") == 0)
143658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
144658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        static const char * shift2_table[] = { "lsl", "lsr", "asr", "ror"};
145658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", shift2_table[bits_unsigned(code, 23,22)]);
146658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
147658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<shift3>") == 0)
148658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 22,21) * 16);
149658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<amt1>") == 0)
150658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 15,10));
151658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<amt2>") == 0)
152658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 12,12) * 2);
153658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<amt3>") == 0)
154658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 12,12) * 3);
155658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<amt4>") == 0)
156658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 12,10));
157658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<amt5>") == 0)
158658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
159658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        static const char * amt5_table[] = {"", "#0"};
160658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", amt5_table[bits_unsigned(code, 12,12)]);
161658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
162658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<amt6>") == 0)
163658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 12,12));
164658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<simm1>") == 0)
165658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_signed(code, 20,12));
166658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<immr1>") == 0)
167658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 21,16));
168658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<imms1>") == 0)
169658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 15,10));
170658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<lsb1>") == 0)
171658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%d", bits_unsigned(code, 15,10));
172658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<cond1>") == 0)
173658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", token_cc_table[bits_unsigned(code, 15,12)]);
174658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<cond2>") == 0)
175658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", token_cc_table[bits_unsigned(code, 4,0)]);
176658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<r1>") == 0)
177658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
178658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        const char * token_r1_table[] =
179658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
180658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            "reserved", "reserved", "w", "x",
181658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            "reserved", "reserved", "w", "x"
182658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        };
183658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", token_r1_table[bits_unsigned(code, 15,13)]);
184658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
185658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<r2>") == 0)
186658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
187658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        static const char * token_r2_table[] =
188658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
189658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat                "w","w","w", "x", "w", "w", "w", "x"
190658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        };
191658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", token_r2_table[bits_unsigned(code, 15,13)]);
192658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
193658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<m1>") == 0)
194658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
195658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        uint32_t reg = bits_unsigned(code, 20,16);
196658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        if(reg == 31)
197658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "%s", "zr");
198658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        else
199658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "%d", reg);
200658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
201658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<ext1>") == 0)
202658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
203658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        static const char * token_ext1_table[] =
204658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
205658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat             "reserved","reserved","uxtw", "lsl",
206658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat             "reserved","reserved", "sxtw", "sxtx"
207658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        };
208658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", token_ext1_table[bits_unsigned(code, 15,13)]);
209658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
210658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if(strcmp(token, "<ext2>") == 0)
211658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
212658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        static const char * token_ext2_table[] =
213658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
214658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat                "uxtb","uxth","uxtw","uxtx",
215658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat                "sxtb","sxth","sxtw","sxtx"
216658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        };
217658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "%s", token_ext2_table[bits_unsigned(code, 15,13)]);
218658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
219658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<label1>") == 0)
220658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
221658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        int32_t offset = bits_signed(code, 23,5) * 4;
222658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        if(offset > 0)
223658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "#.+%d", offset);
224658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        else
225658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "#.-%d", -offset);
226658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
227658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<xn|sp>") == 0)
228658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
229658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        uint32_t reg = bits_unsigned(code, 9, 5);
230658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        if(reg == 31)
231658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "%s", "sp");
232658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        else
233658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "x%d", reg);
234658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
235658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<xd|sp>") == 0)
236658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
237658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        uint32_t reg = bits_unsigned(code, 4, 0);
238658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        if(reg == 31)
239658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "%s", "sp");
240658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        else
241658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            sprintf(instr_part, "x%d", reg);
242658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
243658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<xn>") == 0)
244658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 9, 5), "x", instr_part);
245658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<xd>") == 0)
246658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 4, 0), "x", instr_part);
247658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<xm>") == 0)
248658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 20, 16), "x", instr_part);
249658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<xa>") == 0)
250658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 14, 10), "x", instr_part);
251658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<xt>") == 0)
252658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 4, 0), "x", instr_part);
253658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<wn>") == 0)
254658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 9, 5), "w", instr_part);
255658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<wd>") == 0)
256658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 4, 0), "w", instr_part);
257658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<wm>") == 0)
258658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 20, 16), "w", instr_part);
259658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<wa>") == 0)
260658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 14, 10), "w", instr_part);
261658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else if (strcmp(token, "<wt>") == 0)
262658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        decode_rx_zr_token(bits_unsigned(code, 4, 0), "w", instr_part);
263658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else
264658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
265658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        sprintf(instr_part, "error");
266658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
267658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    return;
268658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat}
269658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
270d4146e6091d6ed947ce9edd0f8ef3e5fe066d716Colin Crossint arm64_disassemble(uint32_t code, char* instr)
271658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat{
272658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    uint32_t i;
273658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    char token[256];
274658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    char instr_part[256];
275658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
276658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    if(instr == NULL)
277658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        return -1;
278658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat
279658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    bool matched = false;
280658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    disasm_table_entry_t *entry = NULL;
281658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    for(i = 0; i < sizeof(disasm_table)/sizeof(disasm_table_entry_t); ++i)
282658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
283658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        entry = &disasm_table[i];
284658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        if((code & entry->mask) == entry->value)
285658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
286658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            matched = true;
287658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            break;
288658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        }
289658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
290658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    if(matched == false)
291658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
292658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        strcpy(instr, "Unknown Instruction");
293658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        return -1;
294658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
295658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    else
296658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    {
297658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        uint32_t index = 0;
298658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        uint32_t length = strlen(entry->instr_template);
299658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        instr[0] = '\0';
300658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        do
301658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        {
302658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            get_token(entry->instr_template, index, token);
303658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            if(token[0] == '<')
304658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            {
305658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat                decode_token(code, token, instr_part);
306658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat                strcat(instr, instr_part);
307658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            }
308658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            else
309658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            {
310658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat                strcat(instr, token);
311658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            }
312658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat            index += strlen(token);
313658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        }while(index < length);
314658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat        return 0;
315658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat    }
316658f89dc5c418dbbc0c5d78f5861855b90ca8c9fAshok Bhat}
317