<commit_before>(***********************************************************************)
(*                                                                     *)
(*                                OCaml                                *)
(*                                                                     *)
(*        <NAME>, <NAME>, INRIA Rocquencourt       *)
(*                                                                     *)
(*  Copyright 1996 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

[@@@ocaml.warning "+A-42-4"]

open Intel_ast
open Intel_proc

let tab b = Buffer.add_char b '\t'

let string_of_data_size = function
  | B64 -> "QWORD"
  | B32 -> "DWORD"
  | B16 -> "WORD"
  | B8 -> "BYTE"

let string_of_datatype_ptr = function
  | QWORD -> "QWORD PTR "
  | OWORD -> "OWORD PTR "
  | NO -> ""
  | REAL4 -> "REAL4 PTR "
  | REAL8 -> "REAL8 PTR "
  | REAL10 -> "REAL10 PTR "
  | BYTE -> "BYTE PTR "
  | TBYTE -> "TBYTE PTR "
  | WORD -> "WORD PTR "
  | DWORD -> "DWORD PTR "
  | NEAR -> "NEAR PTR "
  | PROC -> "PROC PTR "

let bprint_arg_mem b string_of_register mem =
  let ptr = mem.typ in
  match mem.idx, mem.scale, mem.base, mem.displ with
  | _, 0, None, (None, _) -> assert false (* not implemented *)
  | _, 0, None, (Some s , 0L) ->
      Printf.bprintf b "%s %s" (string_of_datatype_ptr ptr) s
  | _, 0, None, (Some s , d) ->
      if d > 0L then
        Printf.bprintf b "%s %s+%Ld" (string_of_datatype_ptr ptr) s d
      else
        Printf.bprintf b "%s %s%Ld" (string_of_datatype_ptr ptr) s d

  | reg1, 1, None, (None, 0L) ->
      Printf.bprintf b "%s[%s]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg1);

  | reg1, 1, None, (None, offset) ->
      Printf.bprintf b "%s[%s%s%Ld]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg1)
        (if offset > 0L then "+" else "")
        offset

  | reg1, scale, None, (None, 0L) ->
      Printf.bprintf b "%s[%s*%d]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg1)
        scale
  | reg1, scale, None, (None, offset) ->
      Printf.bprintf b "%s[%s*%d%s%Ld]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg1)
        scale
        (if offset > 0L then "+" else "")
        offset
  | reg1, 1, Some reg2, (None, 0L) ->
      Printf.bprintf b "%s[%s+%s]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg2)
        (string_of_register reg1)
  | reg1, 1, None, (Some s, 0L) ->
      Printf.bprintf b "%s[%s+%s]"
        (string_of_datatype_ptr ptr)
        s
        (string_of_register reg1)
  | reg1, 1, Some reg2, (None, offset) ->
      Printf.bprintf b "%s[%s+%s%s%Ld]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg2)
        (string_of_register reg1)
        (if offset > 0L then "+" else "")
        offset
  | reg1, 1, None, (Some s, offset ) ->
      Printf.bprintf b "%s[%s+%s%s%Ld]"
        (string_of_datatype_ptr ptr)
        s
        (string_of_register reg1)
        (if offset > 0L then "+" else "")
        offset
  | reg1, scale, Some reg2, (None, 0L) ->
      Printf.bprintf b "%s[%s+%s*%d]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg2)
        (string_of_register reg1)
        scale
  | reg1, scale, None, (Some s, 0L ) ->
      Printf.bprintf b "%s[%s+%s*%d]"
        (string_of_datatype_ptr ptr)
        s
        (string_of_register reg1)
        scale
  | reg1, scale, Some reg2, (None, offset) ->
      Printf.bprintf b "%s[%s+%s*%d%s%Ld]"
        (string_of_datatype_ptr ptr)
        (string_of_register reg2)
        (string_of_register reg1)
        scale
        (if offset > 0L then "+" else "")
        offset
  | reg1, scale, Some reg2, (Some s, offset) ->
      Printf.bprintf b "%s[%s+%s+%s*%d%s%Ld]"
        (string_of_datatype_ptr ptr)
        s
        (string_of_register reg2)
        (string_of_register reg1)
        scale
        (if offset > 0L then "+" else "")
        offset
  | reg1, scale, None, (Some s, offset) ->
      Printf.bprintf b "%s[%s+%s*%d%s%Ld]"
        (string_of_datatype_ptr ptr)
        s
        (string_of_register reg1)
        scale
        (if offset > 0L then "+" else "")
        offset

let bprint_arg b arg =
  match arg with
(*
  | Rel32 s ->
      Printf.bprintf b "%s" s
*)

  | Imm ( (B8|B16|B32), int) ->
      Printf.bprintf b "%Ld" int
  | Imm ( B64, int) ->
      (* force ml64 to use mov reg, imm64 instruction *)
      Printf.bprintf b "0%LxH" int
  | Sym s ->
      Printf.bprintf b "OFFSET %s" s

  | Reg8 register8 ->
      Printf.bprintf b "%s" (string_of_register8 register8)
  | Reg16 register16 ->
      Printf.bprintf b "%s" (string_of_register16 register16)
  | Reg32 register32 ->
      Printf.bprintf b "%s" (string_of_register32 register32)
  | Reg64 register ->
      Printf.bprintf b "%s" (string_of_register64 register)
  | Regf registerf ->
      Printf.bprintf b "%s" (string_of_registerf registerf)

  (* We don't need to specify RIP on Win64, since EXTERN will provide
     the list of external symbols that need this addressing mode, and
     MASM will automatically use RIP addressing when needed. *)<commit_msg>Factorize emission of addressing modes for masm.<commit_after>(***********************************************************************)
(*                                                                     *)
(*                                OCaml                                *)
(*                                                                     *)
(*        <NAME>, projet Gallium, INRIA Rocquencourt       *)
(*                                                                     *)
(*  Copyright 1996 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

[@@@ocaml.warning "+A-42-4"]

open Intel_ast
open Intel_proc

let tab b = Buffer.add_char b '\t'

let string_of_data_size = function
  | B64 -> "QWORD"
  | B32 -> "DWORD"
  | B16 -> "WORD"
  | B8 -> "BYTE"

let string_of_datatype_ptr = function
  | QWORD -> "QWORD PTR "
  | OWORD -> "OWORD PTR "
  | NO -> ""
  | REAL4 -> "REAL4 PTR "
  | REAL8 -> "REAL8 PTR "
  | REAL10 -> "REAL10 PTR "
  | BYTE -> "BYTE PTR "
  | TBYTE -> "TBYTE PTR "
  | WORD -> "WORD PTR "
  | DWORD -> "DWORD PTR "
  | NEAR -> "NEAR PTR "
  | PROC -> "PROC PTR "

let bprint_arg_mem b string_of_register {typ; idx; scale; base; displ} =
  Buffer.add_string b (string_of_datatype_ptr typ);
  Buffer.add_char b '[';
  let (s, o) = displ in
  begin match s with
  | None -> ()
  | Some s -> Buffer.add_string b s;
  end;
  if scale <> 0 then begin
    if s <> None then Buffer.add_char b '+';
    Buffer.add_string b (string_of_register idx);
    if scale <> 1 then Printf.bprintf b "*%d" scale;
  end;
  begin match base with
  | None -> ()
  | Some r ->
      assert(scale > 0);
      Buffer.add_char b '+';
      Buffer.add_string b (string_of_register r);
  end;
  begin if o > 0L then Printf.bprintf b "+%Ld" o
    else if o < 0L then Printf.bprintf b "%Ld" o
  end;
  Buffer.add_char b ']'

let bprint_arg b arg =
  match arg with
  | Imm ( (B8|B16|B32), int) ->
      Printf.bprintf b "%Ld" int
  | Imm ( B64, int) ->
      (* force ml64 to use mov reg, imm64 instruction *)
      Printf.bprintf b "0%LxH" int
  | Sym s ->
      Printf.bprintf b "OFFSET %s" s

  | Reg8 register8 ->
      Printf.bprintf b "%s" (string_of_register8 register8)
  | Reg16 register16 ->
      Printf.bprintf b "%s" (string_of_register16 register16)
  | Reg32 register32 ->
      Printf.bprintf b "%s" (string_of_register32 register32)
  | Reg64 register ->
      Printf.bprintf b "%s" (string_of_register64 register)
  | Regf registerf ->
      Printf.bprintf b "%s" (string_of_registerf registerf)

  (* We don't need to specify RIP on Win64, since EXTERN will provide
     the list of external symbols that need this addressing mode, and
     MASM will automatically use RIP addressing when needed. *)
{'fraction_non_alphanumeric': 0.15413622436954985, 'fraction_numerical': 0.022507659674758426, 'mean_word_length': 1.9581735796444755, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2446103', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2806, 'n_words': 787}