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 "data.h" 25 26 #define MAX_INSN_SIZE 16 27 28 struct CPU_ADDR { 29 union { 30 struct { 31 uint16 seg; 32 uint32 offset; 33 } addr32; 34 struct { 35 uint64 addr; 36 } flat64; 37 }; 38 }; 39 40 struct asm_code { 41 asm_code *next; 42 int size; 43 byte data[MAX_INSN_SIZE]; 44 void *context; 45 }; 46 47 typedef void dis_insn; 48 typedef void asm_insn; 49 50 /* 51 * CLASS assembler 52 */ 53 54 class Assembler: public Object { 55 protected: 56 int (*imm_eval_proc)(void *context, const char *s, uint64 &v); 57 void *imm_eval_context; 58 59 asm_code *codes; 60 asm_code code; 61 char error_msg[256]; 62 bool error; 63 int options; 64 bool bigendian; 65 66 void emitbyte(byte b); 67 void emitword(uint16 w); 68 void emitdword(uint32 d); 69 void emitqword(uint64 q); 70 void free_asm_codes(); 71 void deletecode(asm_code *c); 72 void clearcode(); 73 void newcode(); 74 void pushcode(); 75 public: 76 Assembler(bool bigendian); Assembler(BuildCtorArg & a)77 Assembler(BuildCtorArg&a): Object(a) {}; 78 virtual ~Assembler(); 79 80 /* new */ 81 virtual asm_insn *alloc_insn(); 82 virtual asm_code *encode(asm_insn *asm_insn, int options, CPU_ADDR cur_address); 83 const char *get_error_msg(); 84 virtual const char *get_name(); 85 virtual bool translate_str(asm_insn *asm_insn, const char *s) = 0; 86 void set_error_msg(const char *format, ...); 87 void set_imm_eval_proc(int (*imm_eval_proc)(void *context, const char *s, uint64 &v), void *imm_eval_context); 88 asm_code *shortest(asm_code *codes); 89 }; 90 91 /* 92 * CLASS disassembler 93 */ 94 95 /* generic disassembler styles */ 96 #define DIS_STYLE_HIGHLIGHT 0x80000000 /* create highlighting information in strf() */ 97 #define DIS_STYLE_HEX_CSTYLE 0x40000000 /* IF SET: mov eax, 0x12345678 ELSE: mov eax, 12345678 */ 98 #define DIS_STYLE_HEX_ASMSTYLE 0x20000000 /* IF SET: mov eax, 12345678h ELSE: mov eax, 12345678 */ 99 #define DIS_STYLE_HEX_UPPERCASE 0x10000000 /* IF SET: mov eax, 5678ABCD ELSE: mov eax, 5678abcd */ 100 #define DIS_STYLE_HEX_NOZEROPAD 0x08000000 /* IF SET: mov eax, 8002344 ELSE: mov eax, 008002344 */ 101 #define DIS_STYLE_SIGNED 0x04000000 /* IF SET: mov eax, -1 ELSE: mov eax, 0ffffffffh */ 102 103 #define DIS_STYLE_TABSIZE 12 104 105 extern char* (*addr_sym_func)(CPU_ADDR addr, int *symstrlen, void *context); 106 extern void* addr_sym_func_context; 107 108 enum AsmSyntaxHighlightEnum { 109 e_cs_default=0, 110 e_cs_comment, 111 e_cs_number, 112 e_cs_symbol, 113 e_cs_string 114 }; 115 116 class Disassembler: public Object { 117 protected: 118 int options; 119 bool highlight; 120 121 const char *get_cs(AsmSyntaxHighlightEnum style); 122 void hexd(char **s, int size, int options, uint32 imm); 123 void hexq(char **s, int size, int options, uint64 imm); 124 void enable_highlighting(); 125 void disable_highlighting(); 126 public: 127 Disassembler(); Disassembler(BuildCtorArg & a)128 Disassembler(BuildCtorArg&a): Object(a) {}; 129 void load(ObjectStream &f); 130 /* new */ 131 virtual dis_insn *createInvalidInsn(); 132 virtual dis_insn *decode(byte *code, int maxlen, CPU_ADDR cur_address)=0; 133 virtual dis_insn *duplicateInsn(dis_insn *disasm_insn)=0; 134 virtual void getOpcodeMetrics(int &min_length, int &max_length, int &min_look_ahead, int &avg_look_ahead, int &addr_align)=0; 135 virtual byte getSize(dis_insn *disasm_insn)=0; 136 virtual const char *getName()=0; 137 virtual bool selectNext(dis_insn *disasm_insn); 138 virtual const char *str(dis_insn *disasm_insn, int style); 139 virtual const char *strf(dis_insn *disasm_insn, int style, const char *format)=0; 140 virtual bool validInsn(dis_insn *disasm_insn)=0; 141 }; 142 143 /***************************************************************************** 144 * The strf() format * 145 ***************************************************************************** 146 String Action 147 -------------------------------------------------- 148 %x substitute expression with symbol "x" 149 ?xy...y if symbol "x" is undefined leave out the whole expression, 150 otherwise subsitute expression with string between the two "y"s 151 152 Symbol Desc 153 -------------------------------------------------- 154 p prefix 155 n name 156 1 first operand 157 2 second operand 158 3 third operand 159 4 forth operand 160 */ 161 162 #define DISASM_STRF_VAR '%' 163 #define DISASM_STRF_COND '?' 164 165 #define DISASM_STRF_PREFIX 'p' 166 #define DISASM_STRF_NAME 'n' 167 #define DISASM_STRF_FIRST '1' 168 #define DISASM_STRF_SECOND '2' 169 #define DISASM_STRF_THIRD '3' 170 #define DISASM_STRF_FORTH '4' 171 #define DISASM_STRF_FIFTH '5' 172 173 //#define DISASM_STRF_DEFAULT_FORMAT "?p#%p #%n\t%1?2#, %2?3/, %3/?4-, %4-#" 174 #define DISASM_STRF_DEFAULT_FORMAT "?p#%p #%n\t%1?2#, %2#?3#, %3#?4#, %4#?5#, %5#" 175 #define DISASM_STRF_SMALL_FORMAT "?p#%p #%n?1# %1#?2#,%2#?3#,%3#?4#,%4#?5#,%5#" 176 177 #define ATOM_DISASM_X86 MAGIC32("DIS\x01") 178 #define ATOM_DISASM_ALPHA MAGIC32("DIS\x02") 179 #define ATOM_DISASM_JAVA MAGIC32("DIS\x03") 180 // 0x04 was IA64 181 #define ATOM_DISASM_IL MAGIC32("DIS\x05") 182 #define ATOM_DISASM_X86_VXD MAGIC32("DIS\x06") 183 #define ATOM_DISASM_PPC MAGIC32("DIS\x07") 184 #define ATOM_DISASM_X86_64 MAGIC32("DIS\x08") 185 #define ATOM_DISASM_ARM MAGIC32("DIS\x09") 186 #define ATOM_DISASM_AVR MAGIC32("DIS\x0a") 187 188 #define ASM_SYNTAX_DEFAULT "\\@d" 189 #define ASM_SYNTAX_COMMENT "\\@#" 190 #define ASM_SYNTAX_NUMBER "\\@n" 191 #define ASM_SYNTAX_SYMBOL "\\@c" 192 #define ASM_SYNTAX_STRING "\\@s" 193 194 bool init_asm(); 195 void done_asm(); 196 197 #endif /* __ASM_H__ */ 198