-- X86 disassembler. -- Copyright (C) 2006 Tristan Gingold -- -- GHDL is free software; you can redistribute it and/or modify it under -- the terms of the GNU General Public License as published by the Free -- Software Foundation; either version 2, or (at your option) any later -- version. -- -- GHDL is distributed in the hope that it will be useful, but WITHOUT ANY -- WARRANTY; without even the implied warranty of MERCHANTABILITY or -- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- for more details. -- -- You should have received a copy of the GNU General Public License -- along with GCC; see the file COPYING. If not, write to the Free -- Software Foundation, 59 Temple Place - Suite 330, Boston, MA -- 02111-1307, USA. with System.Address_To_Access_Conversions; package body Disa_X86 is type Byte is new Interfaces.Unsigned_8; type Bf_2 is mod 2 ** 2; type Bf_3 is mod 2 ** 3; type Byte_Vector is array (Natural) of Byte; package Bv_Addr2acc is new System.Address_To_Access_Conversions (Object => Byte_Vector); use Bv_Addr2acc; type Cstring_Acc is access constant String; type Index_Type is ( N_None, N_Push, N_Pop, N_Ret, N_Mov, N_Add, N_Or, N_Adc, N_Sbb, N_And, N_Sub, N_Xor, N_Cmp, N_Into, N_Jmp, N_Jcc, N_Setcc, N_Call, N_Int, N_Cdq, N_Imul, N_Mul, N_Leave, N_Test, N_Lea, N_O, N_No, N_B, N_AE, N_E, N_Ne, N_Be, N_A, N_S, N_Ns, N_P, N_Np, N_L, N_Ge, N_Le, N_G, N_Not, N_Neg, N_Cbw, N_Div, N_Idiv, N_Movsx, N_Movzx, N_Nop, N_Hlt, N_Inc, N_Dec, N_Rol, N_Ror, N_Rcl, N_Rcr, N_Shl, N_Shr, N_Sar, N_Fadd, N_Fmul, N_Fcom, N_Fcomp, N_Fsub, N_Fsubr, N_Fdiv, N_Fdivr, G_1, G_2, G_3, G_5 ); type Names_Type is array (Index_Type range <>) of Cstring_Acc; subtype S is String; Names : constant Names_Type := (N_None => new S'("none"), N_Push => new S'("push"), N_Pop => new S'("pop"), N_Ret => new S'("ret"), N_Mov => new S'("mov"), N_Add => new S'("add"), N_Or => new S'("or"), N_Adc => new S'("adc"), N_Sbb => new S'("sbb"), N_And => new S'("and"), N_Sub => new S'("sub"), N_Xor => new S'("xor"), N_Cmp => new S'("cmp"), N_Into => new S'("into"), N_Jmp => new S'("jmp"), N_Jcc => new S'("j"), N_Int => new S'("int"), N_Cdq => new S'("cdq"), N_Call => new S'("call"), N_Imul => new S'("imul"), N_Mul => new S'("mul"), N_Leave => new S'("leave"), N_Test => new S'("test"), N_Setcc => new S'("set"), N_Lea => new S'("lea"), N_O => new S'("o"), N_No => new S'("no"), N_B => new S'("b"), N_AE => new S'("ae"), N_E => new S'("e"), N_Ne => new S'("ne"), N_Be => new S'("be"), N_A => new S'("a"), N_S => new S'("s"), N_Ns => new S'("ns"), N_P => new S'("p"), N_Np => new S'("np"), N_L => new S'("l"), N_Ge => new S'("ge"), N_Le => new S'("le"), N_G => new S'("g"), N_Not => new S'("not"), N_Neg => new S'("neg"), N_Cbw => new S'("cbw"), N_Div => new S'("div"), N_Idiv => new S'("idiv"), N_Movsx => new S'("movsx"), N_Movzx => new S'("movzx"), N_Nop => new S'("nop"), N_Hlt => new S'("hlt"), N_Inc => new S'("inc"), N_Dec => new S'("dec"), N_Rol => new S'("rol"), N_Ror => new S'("ror"), N_Rcl => new S'("rcl"), N_Rcr => new S'("rcr"), N_Shl => new S'("shl"), N_Shr => new S'("shr"), N_Sar => new S'("sar"), N_Fadd => new S'("fadd"), N_Fmul => new S'("fmul"), N_Fcom => new S'("fcom"), N_Fcomp => new S'("fcomp"), N_Fsub => new S'("fsub"), N_Fsubr => new S'("fsubr"), N_Fdiv => new S'("fdiv"), N_Fdivr => new S'("fdivr") ); -- Format of an instruction. -- MODRM_SRC_8 : modrm byte follow, and modrm is source, witdh = 8bits -- MODRM_DST_8 : modrm byte follow, and modrm is dest, width = 8 bits. -- MODRM_SRC_W : modrm byte follow, and modrm is source, width = 16/32 bits -- MODRM_DST_W : modrm byte follow, and modrm is dest, width =16/32 bits. -- MODRM_IMM_W : modrm byte follow, with an opcode in the reg field, -- followed by an immediat, width = 16/32 bits. -- MODRM_IMM_8 : modrm byte follow, with an opcode in the reg field, -- followed by an immediat, width = 8 bits. -- IMM : the opcode is followed by an immediate value. -- PREFIX : the opcode is a prefix (1 byte). -- OPCODE : inherent addressing. -- OPCODE2 : a second byte specify the instruction. -- REG_IMP : register is in the 3 LSB of the opcode. -- REG_IMM_W : register is in the 3 LSB of the opcode, followed by an -- immediat, width = 16/32 bits. -- DISP_W : a wide displacement (16/32 bits). -- DISP_8 : short displacement (8 bits). -- INVALID : bad opcode. type Format_Type is (Modrm_Src, Modrm_Dst, Modrm_Imm, Modrm_Imm_S, Modrm, Modrm_Ax, Modrm_Imm8, Imm, Imm_S, Imm_8, Eax_Imm, Prefix, Opcode, Opcode2, Reg_Imp, Reg_
#!/usr/bin/env python
#
# Copyright 2009, Google Inc.
# 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 Google Inc. nor the names of its
# 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.

"""upload_gmock.py v0.1.0 -- uploads a Google Mock patch for review.

This simple wrapper passes all command line flags and
--cc=googlemock@googlegroups.com to upload.py.

USAGE: upload_gmock.py [options for upload.py]
"""

__author__ = 'wan@google.com (Zhanyong Wan)'

import os
import sys

CC_FLAG = '--cc='
GMOCK_GROUP = 'googlemock@googlegroups.com'


def main():
  # Finds the path to upload.py, assuming it is in the same directory
  # as this file.
  my_dir = os.path.dirname(os.path.abspath(__file__))
  upload_py_path = os.path.join(my_dir, 'upload.py')

  # Adds Google Mock discussion group to the cc line if it's not there
  # already.
  upload_py_argv = [upload_py_path]
  found_cc_flag = False
  for arg in sys.argv[1:]:
    if arg.startswith(CC_FLAG):
      found_cc_flag = True
      cc_line = arg[len(CC_FLAG):]
      cc_list = [addr for addr in cc_line.split(',') if addr]
      if GMOCK_GROUP not in cc_list:
        cc_list.append(GMOCK_GROUP)
      upload_py_argv.append(CC_FLAG + ','.join(cc_list))
    else:
      upload_py_argv.append(arg)

  if not found_cc_flag:
    upload_py_argv.append(CC_FLAG + GMOCK_GROUP)

  # Invokes upload.py with the modified command line flags.
  os.execv(upload_py_path, upload_py_argv)


if __name__ == '__main__':
  main()
dd_Char (Width_Char (Width)); end if; Name_Align (L); end Add_Opcode; procedure Add_Cond_Opcode (Name : Index_Type; B : Byte) is L : constant Natural := Lo; begin Add_Name (Name); Add_Name (Index_Type'Val (Index_Type'Pos (N_O) + Byte'Pos (B and 16#0f#))); Name_Align (L); end Add_Cond_Opcode; procedure Decode_Reg_Field (F : Bf_3; W : Width_Type) is type Reg_Name2_Array is array (Bf_3) of String (1 .. 2); type Reg_Name3_Array is array (Bf_3) of String (1 .. 3); Regs_8 : constant Reg_Name2_Array := ("al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"); Regs_16 : constant Reg_Name2_Array := ("ax", "cx", "dx", "bx", "sp", "bp", "si", "di"); Regs_32 : constant Reg_Name3_Array := ("eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"); begin Add_Char ('%'); case W is when W_8 => Add_String (Regs_8 (F)); when W_16 => Add_String (Regs_16 (F)); when W_32 => Add_String (Regs_32 (F)); when W_None | W_Data => raise Program_Error; end case; end Decode_Reg_Field; procedure Decode_Val (Off : Natural; Width : Width_Type) is begin case Width is when W_8 => Add_Byte (Mem (Off)); when W_16 => Add_Byte (Mem (Off + 1)); Add_Byte (Mem (Off)); when W_32 => Add_Byte (Mem (Off + 3)); Add_Byte (Mem (Off + 2)); Add_Byte (Mem (Off + 1)); Add_Byte (Mem (Off + 0)); when W_None | W_Data => raise Program_Error; end case; end Decode_Val; function Decode_Val (Off : Natural; Width : Width_Type) return Unsigned_32 is V : Unsigned_32; begin case Width is when W_8 => V := Unsigned_32 (Mem (Off)); -- Sign extension. if V >= 16#80# then V := 16#Ffff_Ff00# or V; end if; return V; when W_16 => return Shift_Left (Unsigned_32 (Mem (Off + 1)), 8) or Unsigned_32 (Mem (Off)); when W_32 => return Shift_Left (Unsigned_32 (Mem (Off + 3)), 24) or Shift_Left (Unsigned_32 (Mem (Off + 2)), 16) or Shift_Left (Unsigned_32 (Mem (Off + 1)), 8) or Shift_Left (Unsigned_32 (Mem (Off + 0)), 0); when W_None | W_Data => raise Program_Error; end case; end Decode_Val; procedure Decode_Imm (Off : in out Natural; Width : Width_Type) is begin Add_String ("$0x"); Decode_Val (Off, Width); Off := Off + Width_Len (Width); end Decode_Imm; procedure Decode_Disp (Off : in out Natural; Width : Width_Type; Offset : Unsigned_32 := 0) is L : Natural; V : Unsigned_32; Off_Orig : constant Natural := Off; begin L := Lo; V := Decode_Val (Off, Width) + Offset; Off := Off + Width_Len (Width); if Proc_Cb /= null then Proc_Cb.all (Mem (Off)'Address, Line (Lo .. Line'Last), Lo); end if; if L /= Lo then if V = 0 then return; end if; Add_String (" + "); end if; Add_String ("0x"); if Offset = 0 then Decode_Val (Off_Orig, Width); else Add_Byte (Byte (Shift_Right (V, 24) and 16#Ff#)); Add_Byte (Byte (Shift_Right (V, 16) and 16#Ff#)); Add_Byte (Byte (Shift_Right (V, 8) and 16#Ff#)); Add_Byte (Byte (Shift_Right (V, 0) and 16#Ff#)); end if; end Decode_Disp; procedure Decode_Modrm_Reg (B : Byte; Width : Width_Type) is begin Decode_Reg_Field (Ext_Modrm_Reg (B), Width); end Decode_Modrm_Reg; procedure Decode_Sib (Sib : Byte; B_Mod : Bf_2) is S : Bf_2; I : Bf_3; B : Bf_3; begin S := Ext_Sib_Scale (Sib); B := Ext_Sib_Base (Sib); I := Ext_Sib_Index (Sib); Add_Char ('('); if B = 2#101# and then B_Mod /= 0 then Decode_Reg_Field (B, W_32); Add_Char (','); end if; if I /= 2#100# then Decode_Reg_Field (I, W_32); case S is when 2#00# => null; when 2#01# => Add_String (",2"); when 2#10# => Add_String (",4"); when 2#11# => Add_String (",8"); end case; end if; Add_Char (')'); end Decode_Sib; procedure Decode_Modrm_Mem (Off : in out Natural; Width : Width_Type) is B : Byte; B_Mod : Bf_2; B_Rm : Bf_3; Off_Orig : Natural; begin B := Mem (Off); B_Mod := Ext_Modrm_Mod (B); B_Rm := Ext_Modrm_Rm (B); Off_Orig := Off; case B_Mod is when 2#11# => Decode_Reg_Field (B_Rm, Width); Off := Off + 1; when 2#10# => if B_Rm = 2#100# then Off := Off + 2; Decode_Disp (Off, W_32); Decode_Sib (Mem (Off_Orig + 1), B_Mod); else Off := Off + 1; Decode_Disp (Off, W_32); Add_Char ('('); Decode_Reg_Field (B_Rm, W_32); Add_Char (')'); end if; when 2#01# => if B_Rm = 2#100# then Off := Off + 2; Decode_Disp (Off, W_8); Decode_Sib (Mem (Off_Orig + 1), B_Mod); else Off := Off + 1; Decode_Disp (Off, W_8); Add_Char ('('); Decode_Reg_Field (B_Rm, W_32); Add_Char (')'); end if; when 2#00# => if B_Rm = 2#100# then Off := Off + 2; Decode_Sib (Mem (Off_Orig + 1), B_Mod); elsif B_Rm = 2#101# then Off := Off + 1; Decode_Disp (Off, W_32); else Add_Char ('('); Decode_Reg_Field (B_Rm, W_32); Add_Char (')'); Off := Off + 1; end if; end case; end Decode_Modrm_Mem; -- Return the length of the modrm bytes. -- At least 1 (mod/rm), at most 6 (mod/rm + SUB + disp32). function Decode_Modrm_Len (Off : Natural) return Natural is B : Byte; M_Mod : Bf_2; M_Rm : Bf_3; begin B := Mem (Off); M_Mod := Ext_Modrm_Mod (B); M_Rm := Ext_Modrm_Rm (B); case M_Mod is when 2#11# => return 1; when 2#10# => if M_Rm = 2#100# then return 1 + 1 + 4; else return 1 + 4; end if; when 2#01# => if M_Rm = 2#100# then return 1 + 1 + 1; else return 1 + 1; end if; when 2#00# => if M_Rm = 2#101# then -- disp32. return 1 + 4; elsif M_Rm = 2#100# then -- SIB return 1 + 1; else return 1; end if; end case; end Decode_Modrm_Len; Off : Natural; B : Byte; B1 : Byte; Desc : Insn_Desc_Type; Name : Index_Type; W : Width_Type; begin Mem := To_Pointer (Addr); Off := 0; Lo := Line'First; B := Mem (0); if B = 2#0000_1111# then B := Mem (1); Off := 2; Insn_Len := 2; Desc := Insn_Desc_0F (B); else Off := 1; Insn_Len := 1; Desc := Insn_Desc (B); end if; if Desc.Name >= G_1 then B1 := Mem (Off); case Desc.Name is when G_1 | G_2 => Name := Group_Name (Desc.Name, Ext_543 (B1)); when G_3 => Desc := Insn_Desc_G3 (Ext_543 (B1)); Name := Desc.Name; when G_5 => Desc := Insn_Desc_G5 (Ext_543 (B1)); Name := Desc.Name; when others => Desc := Desc_Invalid; end case; else Name := Desc.Name; end if; case Desc.Width is when W_Data => W := W_Default; when W_8 | W_16 | W_32 => W := Desc.Width; when W_None => case Desc.Format is when Disp_8 | Cond_Disp_8 | Imm_8 => W := W_8; when Disp_W | Cond_Disp_W => W := W_Default; when Invalid | Opcode => W := W_None; when others => raise Program_Error; end case; end case; case Desc.Format is when Reg_Imp => Add_Opcode (Desc.Name, W_Default); Decode_Reg_Field (Ext_210 (B), W_Default); when Opcode => Add_Opcode (Desc.Name, W_None); when Modrm => Add_Opcode (Desc.Name, W); Decode_Modrm_Mem (Insn_Len, W); when Modrm_Src => Add_Opcode (Desc.Name, W); -- Disp source first. Decode_Modrm_Mem (Insn_Len, W); Add_Comma; B := Mem (Off); Decode_Modrm_Reg (Mem (Off), W); when Modrm_Dst => Add_Opcode (Desc.Name, W); -- Disp source first. B := Mem (Off); Decode_Modrm_Reg (B, W); Add_Comma; Decode_Modrm_Mem (Insn_Len, W); when Modrm_Imm => Add_Opcode (Name, W); Insn_Len := Off + Decode_Modrm_Len (Off); Decode_Imm (Insn_Len, W); Add_Comma; Decode_Modrm_Mem (Off, W); when Modrm_Imm_S => Add_Opcode (Name, W); Insn_Len := Off + Decode_Modrm_Len (Off); Decode_Imm (Insn_Len, W_8); Add_Comma; Decode_Modrm_Mem (Off, W); when Modrm_Imm8 => Add_Opcode (Name, W); Decode_Modrm_Mem (Off, W); Add_Comma; Decode_Imm (Off, W_8); when Reg_Imm => Add_Opcode (Desc.Name, W); Decode_Imm (Insn_Len, W); Add_Comma; Decode_Reg_Field (Ext_210 (B), W); when Eax_Imm => Add_Opcode (Desc.Name, W); Decode_Imm (Insn_Len, W); Add_Comma; Decode_Reg_Field (2#000#, W); when Disp_W | Disp_8 => Add_Opcode (Desc.Name, W_None); Decode_Disp (Insn_Len, W, Pc + Unsigned_32 (Insn_Len + Width_Len (W))); when Cond_Disp_8 | Cond_Disp_W => Add_Cond_Opcode (Desc.Name, B); Decode_Disp (Insn_Len, W, Pc + Unsigned_32 (Insn_Len + Width_Len (W))); when Cond_Modrm => Add_Cond_Opcode (Desc.Name, B); Decode_Modrm_Mem (Insn_Len, W); when Imm => Add_Opcode (Desc.Name, W); Decode_Imm (Insn_Len, W); when Imm_S | Imm_8 => Add_Opcode (Desc.Name, W); Decode_Imm (Insn_Len, W_8); when Modrm_Ax => if (B and 2#1#) = 2#0# then W := W_8; else W := W_Default; end if; Add_Opcode (Desc.Name, W); Decode_Reg_Field (0, W); Add_Comma; Decode_Modrm_Mem (Off, W); when Ax_Off_Src => Add_Opcode (Desc.Name, W); Decode_Disp (Insn_Len, W); Add_Comma; Decode_Reg_Field (0, W); when Ax_Off_Dst => Add_Opcode (Desc.Name, W); Decode_Reg_Field (0, W); Add_Comma; Decode_Disp (Insn_Len, W); when Imp => Add_Opcode (Desc.Name, W_Default); when Invalid | Prefix | Opcode2 => Add_String ("invalid "); if Insn_Len = 2 then Add_Byte (Mem (0)); end if; Add_Byte (B); Insn_Len := 1; end case; Line_Len := Lo - Line'First; end Disassemble_Insn; end Disa_X86;