1 |
/* |
2 |
* The HT Editor |
3 |
* asm.h |
4 |
* |
5 |
* Copyright (C) 1999-2002 Stefan Weyergraf |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License version 2 as |
9 |
* published by the Free Software Foundation. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#ifndef __ASM_H__ |
22 |
#define __ASM_H__ |
23 |
|
24 |
#include "tools/data.h" |
25 |
|
26 |
#define CPU_X86 1 |
27 |
|
28 |
#define MAX_INSN_SIZE 16 |
29 |
|
30 |
struct CPU_ADDR { |
31 |
union { |
32 |
struct { |
33 |
uint16 seg; |
34 |
uint32 offset; |
35 |
} addr32; |
36 |
struct { |
37 |
uint64 addr; |
38 |
} flat64; |
39 |
}; |
40 |
}; |
41 |
|
42 |
struct asm_code { |
43 |
asm_code *next; |
44 |
uint32 size; |
45 |
byte data[MAX_INSN_SIZE]; |
46 |
void *context; |
47 |
}; |
48 |
|
49 |
typedef void dis_insn; |
50 |
typedef void asm_insn; |
51 |
|
52 |
/* |
53 |
* CLASS assembler |
54 |
*/ |
55 |
|
56 |
class Assembler: public Object { |
57 |
protected: |
58 |
int (*imm_eval_proc)(void *context, char **s, uint32 *v); |
59 |
void *imm_eval_context; |
60 |
|
61 |
asm_code *codes; |
62 |
asm_code code; |
63 |
char error_msg[256]; |
64 |
bool error; |
65 |
int options; |
66 |
bool bigendian; |
67 |
|
68 |
void emitbyte(byte b); |
69 |
void emitword(uint16 w); |
70 |
void emitdword(uint32 d); |
71 |
void free_asm_codes(); |
72 |
void deletecode(asm_code *c); |
73 |
void clearcode(); |
74 |
void newcode(); |
75 |
void pushcode(); |
76 |
public: |
77 |
Assembler(bool bigendian); |
78 |
~Assembler(); |
79 |
/* new */ |
80 |
virtual asm_insn *alloc_insn(); |
81 |
virtual asm_code *encode(asm_insn *asm_insn, int options, CPU_ADDR cur_address); |
82 |
char *get_error_msg(); |
83 |
virtual char *get_name(); |
84 |
virtual int translate_str(asm_insn *asm_insn, const char *s); |
85 |
void set_error_msg(char *format, ...); |
86 |
void set_imm_eval_proc(int (*imm_eval_proc)(void *context, char **s, uint32 *v), void *imm_eval_context); |
87 |
asm_code *shortest(asm_code *codes); |
88 |
}; |
89 |
|
90 |
/* |
91 |
* CLASS disassembler |
92 |
*/ |
93 |
|
94 |
/* generic disassembler styles */ |
95 |
#define DIS_STYLE_HIGHLIGHT 0x80000000 /* create highlighting information in strf() */ |
96 |
#define DIS_STYLE_HEX_CSTYLE 0x40000000 /* IF SET: mov eax, 0x12345678 ELSE: mov eax, 12345678 */ |
97 |
#define DIS_STYLE_HEX_ASMSTYLE 0x20000000 /* IF SET: mov eax, 12345678h ELSE: mov eax, 12345678 */ |
98 |
#define DIS_STYLE_HEX_UPPERCASE 0x10000000 /* IF SET: mov eax, 5678ABCD ELSE: mov eax, 5678abcd */ |
99 |
#define DIS_STYLE_HEX_NOZEROPAD 0x08000000 /* IF SET: mov eax, 8002344 ELSE: mov eax, 008002344 */ |
100 |
#define DIS_STYLE_SIGNED 0x04000000 /* IF SET: mov eax, -1 ELSE: mov eax, 0ffffffffh */ |
101 |
|
102 |
#define DIS_STYLE_TABSIZE 8 |
103 |
|
104 |
extern char* (*addr_sym_func)(CPU_ADDR addr, int *symstrlen, void *context); |
105 |
extern void* addr_sym_func_context; |
106 |
|
107 |
enum AsmSyntaxHighlightEnum { |
108 |
e_cs_default=0, |
109 |
e_cs_comment, |
110 |
e_cs_number, |
111 |
e_cs_symbol, |
112 |
e_cs_string |
113 |
}; |
114 |
|
115 |
class Disassembler: public Object { |
116 |
protected: |
117 |
int options; |
118 |
bool highlight; |
119 |
|
120 |
const char *get_cs(AsmSyntaxHighlightEnum style); |
121 |
void hexd(char **s, int size, int options, int imm); |
122 |
void enable_highlighting(); |
123 |
void disable_highlighting(); |
124 |
public: |
125 |
Disassembler(); |
126 |
~Disassembler(); |
127 |
/* new */ |
128 |
virtual dis_insn *createInvalidInsn(); |
129 |
virtual dis_insn *decode(const byte *code, int maxlen, CPU_ADDR cur_address)=0; |
130 |
virtual dis_insn *duplicateInsn(dis_insn *disasm_insn)=0; |
131 |
virtual void getOpcodeMetrics(int &min_length, int &max_length, int &min_look_ahead, int &avg_look_ahead, int &addr_align)=0; |
132 |
virtual byte getSize(dis_insn *disasm_insn)=0; |
133 |
virtual char *getName()=0; |
134 |
virtual bool selectNext(dis_insn *disasm_insn); |
135 |
virtual char *str(dis_insn *disasm_insn, int style); |
136 |
virtual char *strf(dis_insn *disasm_insn, int style, char *format)=0; |
137 |
virtual bool validInsn(dis_insn *disasm_insn)=0; |
138 |
}; |
139 |
|
140 |
/***************************************************************************** |
141 |
* The strf() format * |
142 |
***************************************************************************** |
143 |
String Action |
144 |
-------------------------------------------------- |
145 |
%x substitute expression with symbol "x" |
146 |
?xy...y if symbol "x" is undefined leave out the whole expression, |
147 |
otherwise subsitute expression with string between the two "y"s |
148 |
|
149 |
Symbol Desc |
150 |
-------------------------------------------------- |
151 |
p prefix |
152 |
n name |
153 |
1 first operand |
154 |
2 second operand |
155 |
3 third operand |
156 |
*/ |
157 |
|
158 |
#define DISASM_STRF_VAR '%' |
159 |
#define DISASM_STRF_COND '?' |
160 |
|
161 |
#define DISASM_STRF_PREFIX 'p' |
162 |
#define DISASM_STRF_NAME 'n' |
163 |
#define DISASM_STRF_FIRST '1' |
164 |
#define DISASM_STRF_SECOND '2' |
165 |
#define DISASM_STRF_THIRD '3' |
166 |
|
167 |
#define DISASM_STRF_DEFAULT_FORMAT "?p#%p #%n\t%1?2#, %2?3/, %3/#" |
168 |
#define DISASM_STRF_SMALL_FORMAT "?p#%p #%n?1- %1?2#,%2?3/,%3/#-" |
169 |
|
170 |
#define ATOM_DISASM_X86 MAGICD("DIS\x01") |
171 |
#define ATOM_DISASM_ALPHA MAGICD("DIS\x02") |
172 |
#define ATOM_DISASM_JAVA MAGICD("DIS\x03") |
173 |
#define ATOM_DISASM_IA64 MAGICD("DIS\x04") |
174 |
#define ATOM_DISASM_IL MAGICD("DIS\x05") |
175 |
#define ATOM_DISASM_X86_VXD MAGICD("DIS\x06") |
176 |
|
177 |
#define ASM_SYNTAX_DEFAULT "\\@d" |
178 |
#define ASM_SYNTAX_COMMENT "\\@#" |
179 |
#define ASM_SYNTAX_NUMBER "\\@n" |
180 |
#define ASM_SYNTAX_SYMBOL "\\@c" |
181 |
#define ASM_SYNTAX_STRING "\\@s" |
182 |
|
183 |
bool init_asm(); |
184 |
void done_asm(); |
185 |
|
186 |
#endif /* __ASM_H__ */ |