Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

asm.h

Go to the documentation of this file.
00001 /*
00002  *      The HT Editor
00003  *      asm.h
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifndef __ASM_H__
00022 #define __ASM_H__
00023 
00024 #include "common.h"
00025 
00026 #define CPU_X86 1
00027 
00028 #define MAX_INSN_SIZE   16
00029 
00030 struct CPU_ADDR {
00031         union {
00032                 struct {
00033                         word seg;
00034                         dword offset;
00035                 } addr32;
00036                 struct {
00037                         qword addr;
00038                 } flat64;
00039         };
00040 };
00041 
00042 struct asm_code {
00043         asm_code *next;
00044         dword size;
00045         byte data[MAX_INSN_SIZE];
00046         void *context;
00047 };
00048 
00049 typedef void dis_insn;
00050 typedef void asm_insn;
00051 
00052 /*
00053  *      CLASS assembler
00054  */
00055 
00056 class Assembler: public Object {
00057 protected:
00058         int (*imm_eval_proc)(void *context, char **s, dword *v);
00059         void *imm_eval_context;
00060         
00061         asm_code *codes;
00062         asm_code code;
00063         char error_msg[256];
00064         bool error;
00065         int options;
00066         bool bigendian;
00067 
00068                         void emitbyte(byte b);
00069                         void emitword(word w);
00070                         void emitdword(dword d);
00071                         void free_asm_codes();
00072                         void deletecode(asm_code *c);
00073                         void clearcode();
00074                         void newcode();
00075                         void pushcode();
00076 public:
00077                         Assembler(bool bigendian);
00078                         ~Assembler();
00079 /* new */
00080         virtual asm_insn *alloc_insn();
00081         virtual asm_code *encode(asm_insn *asm_insn, int options, CPU_ADDR cur_address);
00082                         char *get_error_msg();
00083         virtual char *get_name();
00084         virtual int translate_str(asm_insn *asm_insn, const char *s);
00085                         void set_error_msg(char *format, ...);
00086                         void set_imm_eval_proc(int (*imm_eval_proc)(void *context, char **s, dword *v), void *imm_eval_context);
00087                         asm_code *shortest(asm_code *codes);
00088 };
00089 
00090 /*
00091  *      CLASS disassembler
00092  */
00093 
00094 /* generic disassembler styles */
00095 #define DIS_STYLE_HIGHLIGHT                     0x80000000              /* create highlighting information in strf() */
00096 #define DIS_STYLE_HEX_CSTYLE                    0x40000000              /* IF SET: mov eax, 0x12345678          ELSE: mov eax, 12345678 */
00097 #define DIS_STYLE_HEX_ASMSTYLE          0x20000000              /* IF SET: mov eax, 12345678h           ELSE: mov eax, 12345678 */
00098 #define DIS_STYLE_HEX_UPPERCASE         0x10000000              /* IF SET: mov eax, 5678ABCD                    ELSE: mov eax, 5678abcd */
00099 #define DIS_STYLE_HEX_NOZEROPAD         0x08000000              /* IF SET: mov eax, 8002344                     ELSE: mov eax, 008002344 */
00100 #define DIS_STYLE_SIGNED                                0x04000000              /* IF SET: mov eax, -1                          ELSE: mov eax, 0ffffffffh */
00101 
00102 #define DIS_STYLE_TABSIZE                       8
00103 
00104 extern char* (*addr_sym_func)(CPU_ADDR addr, int *symstrlen, void *context);
00105 extern void* addr_sym_func_context;
00106 
00107 enum AsmSyntaxHighlightEnum {
00108         e_cs_default=0,
00109         e_cs_comment,
00110         e_cs_number,
00111         e_cs_symbol,
00112         e_cs_string
00113 };
00114 
00115 class Disassembler: public Object {
00116 protected:
00117         int options;
00118         bool highlight;
00119         
00120                         const char *get_cs(AsmSyntaxHighlightEnum style);
00121                         void hexd(char **s, int size, int options, int imm);
00122                         void enable_highlighting();
00123                         void disable_highlighting();
00124 public:
00125                         Disassembler();
00126                         ~Disassembler();
00127 /* new */
00128         virtual dis_insn *createInvalidInsn();
00129         virtual dis_insn *decode(byte *code, int maxlen, CPU_ADDR cur_address)=0;
00130         virtual dis_insn *duplicateInsn(dis_insn *disasm_insn)=0;
00131         virtual void    getOpcodeMetrics(int &min_length, int &max_length, int &min_look_ahead, int &avg_look_ahead, int &addr_align)=0;
00132         virtual byte getSize(dis_insn *disasm_insn)=0;
00133         virtual char *getName()=0;
00134         virtual bool selectNext(dis_insn *disasm_insn);
00135         virtual char *str(dis_insn *disasm_insn, int style);
00136         virtual char *strf(dis_insn *disasm_insn, int style, char *format)=0;
00137         virtual bool validInsn(dis_insn *disasm_insn)=0;
00138 };
00139 
00140 /*****************************************************************************
00141  *      The strf() format                                                       *
00142  *****************************************************************************
00143         String  Action
00144     --------------------------------------------------
00145         %x              substitute expression with symbol "x"
00146         ?xy...y if symbol "x" is undefined leave out the whole expression,
00147                         otherwise subsitute expression with string between the two "y"s
00148 
00149         Symbol  Desc
00150     --------------------------------------------------
00151         p               prefix
00152         n               name
00153         1               first operand
00154         2               second operand
00155         3               third operand
00156 */
00157 
00158 #define DISASM_STRF_VAR                 '%'
00159 #define DISASM_STRF_COND                        '?'
00160 
00161 #define DISASM_STRF_PREFIX              'p'
00162 #define DISASM_STRF_NAME                        'n'
00163 #define DISASM_STRF_FIRST               '1'
00164 #define DISASM_STRF_SECOND              '2'
00165 #define DISASM_STRF_THIRD               '3'
00166 
00167 #define DISASM_STRF_DEFAULT_FORMAT      "?p#%p #%n\t%1?2#, %2?3/, %3/#"
00168 #define DISASM_STRF_SMALL_FORMAT        "?p#%p #%n?1- %1?2#,%2?3/,%3/#-"
00169 
00170 #define ATOM_DISASM_X86 MAGICD("DIS\x01")
00171 #define ATOM_DISASM_ALPHA MAGICD("DIS\x02")
00172 #define ATOM_DISASM_JAVA MAGICD("DIS\x03")
00173 #define ATOM_DISASM_IA64 MAGICD("DIS\x04")
00174 #define ATOM_DISASM_IL MAGICD("DIS\x05")
00175 #define ATOM_DISASM_X86_VXD MAGICD("DIS\x06")
00176 #define ATOM_DISASM_PPC MAGICD("DIS\x07")
00177 
00178 #define ASM_SYNTAX_DEFAULT "\\@d"
00179 #define ASM_SYNTAX_COMMENT "\\@#"
00180 #define ASM_SYNTAX_NUMBER "\\@n"
00181 #define ASM_SYNTAX_SYMBOL "\\@c"
00182 #define ASM_SYNTAX_STRING "\\@s"
00183 
00184 bool init_asm();
00185 void done_asm();
00186 
00187 #endif /* __ASM_H__ */

Generated on Fri May 7 21:15:29 2004 by doxygen 1.3.5