Skip to content

Commit

Permalink
Adding support for CDQ, CDQE, CQO, CWD, CWDE, CBW opcodes (#833)
Browse files Browse the repository at this point in the history
* Adding a first implementation for CDQ.

* Run ocp-indent.
  • Loading branch information
ethan42 authored and gitoleg committed May 7, 2018
1 parent b9cf186 commit 7de7c81
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions oasis/x86
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ Library x86_plugin
X86_asm_reg,
X86_asm_reg_types,
X86_btx,
X86_cdq,
X86_cmpxchg,
X86_disasm,
X86_lifter,
X86_main,
X86_mov,
X86_mov_offset,
X86_opcode_btx,
X86_opcode_cdq,
X86_opcode_cmps,
X86_opcode_cmpxchg,
X86_opcode_ins,
Expand Down
46 changes: 46 additions & 0 deletions plugins/x86/x86_cdq.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
open Core_kernel.Std
open Bap.Std
open X86_opcode_cdq

module Make (Tools : X86_tools.S) (Backend : X86_backend.S) = struct
open Tools

let reg = RR.of_asm_exn

let cdq (op:cdq) mem insn =
let open Or_error in
let ( ==> ) src dst =
let src = reg src
and dst = reg dst in
let src_width = (RR.width src) |> Size.in_bits
and dst_width = (RR.width dst) |> Size.in_bits in
let extended_width = 2 * src_width in
let extended_e = Bil.cast SIGNED extended_width @@ RR.get src in
if dst_width = extended_width then
RR.set dst extended_e
else
RR.set dst @@ Bil.cast HIGH dst_width extended_e
in
let stmt = match op with
| `CDQ -> `EAX ==> `EDX
| `CDQE -> `EAX ==> `RAX
| `CQO -> `RAX ==> `RDX
| `CWD -> `AX ==> `DX
| `CWDE -> `AX ==> `EAX
| `CBW -> `AL ==> `AX
in
Ok [stmt]

let register what =
let name op = sexp_of_cdq (op :> cdq) |> Sexp.to_string in
List.iter (what :> cdq list)
~f:(fun op -> Backend.register (name op) (cdq op))

end

module IA32 = Make (X86_tools.IA32) (X86_backend.IA32)
module AMD64 = Make (X86_tools.AMD64) (X86_backend.AMD64)

let () =
IA32.register all_of_cdq;
AMD64.register all_of_cdq
1 change: 1 addition & 0 deletions plugins/x86/x86_cdq.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(* CDQ, CDQE, CQO, CWD, CWDE, CBW lifter *)
10 changes: 10 additions & 0 deletions plugins/x86/x86_opcode_cdq.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
open Core_kernel.Std

type cdq = [
| `CBW
| `CWD
| `CWDE
| `CDQ
| `CDQE
| `CQO
] [@@deriving bin_io, sexp, compare, enumerate]

0 comments on commit 7de7c81

Please sign in to comment.