⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emit-inline.c

📁 二进制翻译的一个软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2005, Johns Hopkins University and The EROS Group, LLC. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * *  * Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. *  * Redistributions in binary form must reproduce the above *    copyright notice, this list of conditions and the following *    disclaimer in the documentation and/or other materials provided *    with the distribution. * *  * Neither the name of the Johns Hopkins University, nor the name *    of The EROS Group, LLC, nor the names of their contributors may *    be used to endorse or promote products derived from this *    software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. *//** BASIC EMITTER FUNCTIONS. THESE ARE ALL THINGS THAT SHOULD BE     INLINED IF POSSIBLE. */INLINE voidbb_emit_byte(machine_t *M, unsigned char c){  /* We don't bother to signal an error -- overrun will be checked by     the basic block translator procedure. Since the decoder is never     a consumer of the output bytes, it's okay to just suppress output     on overrun. Therefore, simply make sure that we do not run off     the end of the basic block. */  if (M->bbOut != M->bbLimit)    *M->bbOut++ = c;}INLINE voidbb_emit_w16(machine_t *M, unsigned long ul){  bb_emit_byte(M, ul & 0xffu);  bb_emit_byte(M, (ul >> 8) & 0xffu);}INLINE voidbb_emit_w32(machine_t *M, unsigned long ul){  bb_emit_byte(M, ul & 0xffu);  bb_emit_byte(M, (ul >> 8) & 0xffu);  bb_emit_byte(M, (ul >> 16) & 0xffu);  bb_emit_byte(M, (ul >> 24) & 0xffu);}INLINE voidbb_emit_jump(machine_t *M, unsigned char *dest){  unsigned long next_instr;  unsigned long moffset;    bb_emit_byte(M, 0xe9u);  /* Specification for jump target is that it is an offset relative to     the address of the NEXT instruction, so we need to compute what     the address of the hypothetical next instruction would be, which     is now bbOut + 4. */  next_instr = (unsigned long) M->bbOut + 4;  moffset = (unsigned long)dest - next_instr;  bb_emit_w32(M, moffset);}INLINE voidbb_emit_call(machine_t *M, unsigned char *dest){  unsigned long next_instr;  unsigned long moffset;    bb_emit_byte(M, 0xe8u);  /* Specification for jump target is that it is an offset relative to     the address of the NEXT instruction, so we need to compute what     the address of the hypothetical next instruction would be, which     is now bbOut + 4. */  next_instr = (unsigned long) M->bbOut + 4;  moffset = (unsigned long)dest - next_instr;  bb_emit_w32(M, moffset);}INLINE voidbb_emit_save_reg_to(machine_t *M, unsigned long whichReg, unsigned long addr){  //bb_emit_byte(M, 0x65u); /* GS Segment Override Prefix - for accessing the M structure */  switch(whichReg) {  case GP_REG_EAX:    bb_emit_byte(M, 0xa3u);    bb_emit_w32(M, addr);    break;  case GP_REG_EBX:    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x1du);    bb_emit_w32(M, addr);    break;  case GP_REG_ECX:    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x0du);    bb_emit_w32(M, addr);    break;  case GP_REG_EDX:    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x15u);    bb_emit_w32(M, addr);    break;  case GP_REG_ESI:    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x35u);    bb_emit_w32(M, addr);    break;  case GP_REG_EDI:    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x3du);    bb_emit_w32(M, addr);    break;  case GP_REG_EBP:    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x2du);    bb_emit_w32(M, addr);    break;  case GP_REG_ESP:    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x25u);    bb_emit_w32(M, addr);    break;  default:    panic("bb_emit_save_reg_to() called with unknown register\n");    break;  }}INLINE voidbb_emit_16_bit_save_reg_to(machine_t *M, unsigned long whichReg, unsigned long addr){  //bb_emit_byte(M, 0x65u); /* GS Segment Override Prefix - for accessing the M structure */  switch(whichReg) {  case GP_REG_EAX:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0xa3u);    bb_emit_w32(M, addr);    break;  case GP_REG_EBX:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x1du);    bb_emit_w32(M, addr);    break;  case GP_REG_ECX:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x0du);    bb_emit_w32(M, addr);    break;  case GP_REG_EDX:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x15u);    bb_emit_w32(M, addr);    break;  case GP_REG_ESI:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x35u);    bb_emit_w32(M, addr);    break;  case GP_REG_EDI:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x3du);    bb_emit_w32(M, addr);    break;  case GP_REG_EBP:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x2du);    bb_emit_w32(M, addr);    break;  case GP_REG_ESP:    bb_emit_byte(M, 0x66u); /* Operand-size Override Prefix - to indicate 16-bit mov */    bb_emit_byte(M, 0x89u);    bb_emit_byte(M, 0x25u);    bb_emit_w32(M, addr);    break;  default:    panic("bb_emit_save_reg_to() called with unknown register\n");    break;  }}INLINE voidbb_emit_restore_reg_from(machine_t *M, unsigned long whichReg, unsigned long addr){  //bb_emit_byte(M, 0x65u); /* GS Segment Override Prefix - for accessing the M structure */  switch(whichReg) {  case GP_REG_EAX:    bb_emit_byte(M, 0xa1u);    bb_emit_w32(M, addr);    break;  case GP_REG_EBX:    bb_emit_byte(M, 0x8bu);    bb_emit_byte(M, 0x1du);    bb_emit_w32(M, addr);    break;  case GP_REG_ECX:    bb_emit_byte(M, 0x8bu);    bb_emit_byte(M, 0x0du);    bb_emit_w32(M, addr);    break;  case GP_REG_EDX:    bb_emit_byte(M, 0x8bu);    bb_emit_byte(M, 0x15u);    bb_emit_w32(M, addr);    break;  case GP_REG_ESI:    bb_emit_byte(M, 0x8bu);    bb_emit_byte(M, 0x35u);    bb_emit_w32(M, addr);    break;  case GP_REG_EDI:    bb_emit_byte(M, 0x8bu);    bb_emit_byte(M, 0x3du);    bb_emit_w32(M, addr);    break;  case GP_REG_EBP:    bb_emit_byte(M, 0x8bu);    bb_emit_byte(M, 0x2du);    bb_emit_w32(M, addr);    break;  case GP_REG_ESP:    bb_emit_byte(M, 0x8bu);    bb_emit_byte(M, 0x25u);    bb_emit_w32(M, addr);    break;  default:    panic("bb_emit_restore_reg_from() called with unknown register\n");    break;  }}INLINE voidbb_emit_save_reg(machine_t *M, unsigned long whichReg){  unsigned long save_to;  switch(whichReg) {  case GP_REG_EAX:    save_to = MREG(M, eax);        break;  case GP_REG_EBX:    save_to = MREG(M, ebx);        break;  case GP_REG_ECX:    save_to = MREG(M, ecx);        break;  case GP_REG_EDX:    save_to = MREG(M, edx);        break;  case GP_REG_ESI:    save_to = MREG(M, esi);        break;  case GP_REG_EDI:    save_to = MREG(M, edi);        break;  case GP_REG_EBP:    save_to = MREG(M, ebp);        break;  case GP_REG_ESP:    save_to = MREG(M, esp);        break;  default:    panic("bb_emit_save_reg() called with unknown register\n");    break;  }  bb_emit_save_reg_to(M, whichReg, save_to);}INLINE voidbb_emit_restore_reg(machine_t *M, unsigned long whichReg){  unsigned long restore_from;  switch(whichReg) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -