-
Notifications
You must be signed in to change notification settings - Fork 0
/
disassembler.h
108 lines (87 loc) · 2.46 KB
/
disassembler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include "platform.h"
#include "instruction_types.h"
#include "cpu_types.h"
#include <cstring>
#include "magic_enum.hpp"
struct EncodingDescriptor
{
InstructionType type{};
const char* debug_format{};
cpu_func_t func;
uint32_t mask;
uint32_t value;
EncodingDescriptor() = default;
constexpr EncodingDescriptor(InstructionType inst, const char* descriptor, const char* debug_format_string, cpu_func_t _func) :
type(inst),
debug_format(debug_format_string),
func(_func),
mask(gen_mask(descriptor)),
value(gen_value(descriptor))
{
if (strlen(descriptor) != 32)
{
printf("Invalid bit count for opcode descriptor '%s'\n", magic_enum::enum_name(inst).data());
assert(false);
}
}
constexpr bool match(uint32_t op) const
{
return (op & mask) == value;
}
constexpr uint32_t gen_mask(const char* str)
{
uint32_t mask{};
uint32_t bit_index{31};
while (*str)
{
switch (*str)
{
case '0':
case '1':
mask |= 1 << bit_index;
break;
case 's':
case 't':
case 'd':
case 'j':
case 'i':
case 'a':
break;
default:
printf("unknown opcode bit type '%c'\n", *str);
assert(false);
break;
}
str++;
bit_index--;
}
return mask;
}
constexpr uint32_t gen_value(const char* str)
{
uint32_t value{};
uint32_t bit_index{31};
while (*str)
{
switch (*str)
{
case '0':
break;
case '1':
value |= 1 << bit_index;
break;
default:
break;
}
str++;
bit_index--;
}
return value;
}
};
void disassembler_init();
const EncodingDescriptor* disassembler_find_descriptor(InstructionType type);
const EncodingDescriptor* disassembler_decode_instruction(uint32_t opcode);
// dissasemble a single instruction into text form using symbolic register names
bool disassembler_parse_instruction(uint32_t opcode, const EncodingDescriptor* desc, char* dst_buf, int);