Add intel_proc.mli, get rid of arch64 bool ref, move StringSet/StringMap to misc.

git-svn-id: http://caml.inria.fr/svn/ocaml/branches/abstract_intel_emit@15159 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Alain Frisch 2014-08-29 14:35:10 +00:00
parent aab23e506e
commit e7ba464587
9 changed files with 135 additions and 63 deletions

29
.depend
View File

@ -600,6 +600,7 @@ asmcomp/debuginfo.cmi : parsing/location.cmi bytecomp/lambda.cmi
asmcomp/emit.cmi : asmcomp/linearize.cmi asmcomp/cmm.cmi
asmcomp/emitaux.cmi : asmcomp/debuginfo.cmi
asmcomp/intel_ast.cmi :
asmcomp/intel_proc.cmi :
asmcomp/interf.cmi : asmcomp/mach.cmi
asmcomp/linearize.cmi : asmcomp/reg.cmi asmcomp/mach.cmi bytecomp/lambda.cmi \
asmcomp/debuginfo.cmi
@ -740,28 +741,28 @@ asmcomp/debuginfo.cmx : parsing/location.cmx bytecomp/lambda.cmx \
asmcomp/debuginfo.cmi
asmcomp/emit.cmo : asmcomp/reg.cmi asmcomp/proc.cmi utils/misc.cmi \
asmcomp/mach.cmi asmcomp/linearize.cmi bytecomp/lambda.cmi \
asmcomp/intel_proc.cmo asmcomp/intel_masm.cmo asmcomp/intel_gas.cmo \
asmcomp/emitaux.cmi asmcomp/debuginfo.cmi utils/config.cmi \
asmcomp/compilenv.cmi asmcomp/cmm.cmi utils/clflags.cmi asmcomp/arch.cmo \
asmcomp/emit.cmi
asmcomp/intel_proc.cmi asmcomp/intel_masm.cmo asmcomp/intel_gas.cmo \
asmcomp/intel_ast.cmi asmcomp/emitaux.cmi asmcomp/debuginfo.cmi \
utils/config.cmi asmcomp/compilenv.cmi asmcomp/cmm.cmi utils/clflags.cmi \
asmcomp/arch.cmo asmcomp/emit.cmi
asmcomp/emit.cmx : asmcomp/reg.cmx asmcomp/proc.cmx utils/misc.cmx \
asmcomp/mach.cmx asmcomp/linearize.cmx bytecomp/lambda.cmx \
asmcomp/intel_proc.cmx asmcomp/intel_masm.cmx asmcomp/intel_gas.cmx \
asmcomp/emitaux.cmx asmcomp/debuginfo.cmx utils/config.cmx \
asmcomp/compilenv.cmx asmcomp/cmm.cmx utils/clflags.cmx asmcomp/arch.cmx \
asmcomp/emit.cmi
asmcomp/intel_ast.cmi asmcomp/emitaux.cmx asmcomp/debuginfo.cmx \
utils/config.cmx asmcomp/compilenv.cmx asmcomp/cmm.cmx utils/clflags.cmx \
asmcomp/arch.cmx asmcomp/emit.cmi
asmcomp/emitaux.cmo : asmcomp/linearize.cmi asmcomp/debuginfo.cmi \
utils/config.cmi utils/clflags.cmi asmcomp/arch.cmo asmcomp/emitaux.cmi
asmcomp/emitaux.cmx : asmcomp/linearize.cmx asmcomp/debuginfo.cmx \
utils/config.cmx utils/clflags.cmx asmcomp/arch.cmx asmcomp/emitaux.cmi
asmcomp/intel_gas.cmo : asmcomp/intel_proc.cmo
asmcomp/intel_gas.cmx : asmcomp/intel_proc.cmx
asmcomp/intel_masm.cmo : asmcomp/intel_proc.cmo
asmcomp/intel_masm.cmx : asmcomp/intel_proc.cmx
asmcomp/intel_gas.cmo : asmcomp/intel_proc.cmi asmcomp/intel_ast.cmi
asmcomp/intel_gas.cmx : asmcomp/intel_proc.cmx asmcomp/intel_ast.cmi
asmcomp/intel_masm.cmo : asmcomp/intel_proc.cmi asmcomp/intel_ast.cmi
asmcomp/intel_masm.cmx : asmcomp/intel_proc.cmx asmcomp/intel_ast.cmi
asmcomp/intel_proc.cmo : utils/misc.cmi asmcomp/intel_ast.cmi \
utils/config.cmi utils/clflags.cmi utils/ccomp.cmi
utils/config.cmi utils/clflags.cmi utils/ccomp.cmi asmcomp/intel_proc.cmi
asmcomp/intel_proc.cmx : utils/misc.cmx asmcomp/intel_ast.cmi \
utils/config.cmx utils/clflags.cmx utils/ccomp.cmx
utils/config.cmx utils/clflags.cmx utils/ccomp.cmx asmcomp/intel_proc.cmi
asmcomp/interf.cmo : asmcomp/reg.cmi asmcomp/proc.cmi asmcomp/mach.cmi \
asmcomp/interf.cmi
asmcomp/interf.cmx : asmcomp/reg.cmx asmcomp/proc.cmx asmcomp/mach.cmx \
@ -807,7 +808,7 @@ asmcomp/printmach.cmx : asmcomp/reg.cmx asmcomp/proc.cmx \
asmcomp/debuginfo.cmx asmcomp/cmm.cmx asmcomp/arch.cmx \
asmcomp/printmach.cmi
asmcomp/proc.cmo : asmcomp/reg.cmi utils/misc.cmi asmcomp/mach.cmi \
asmcomp/intel_proc.cmo utils/config.cmi asmcomp/cmm.cmi asmcomp/arch.cmo \
asmcomp/intel_proc.cmi utils/config.cmi asmcomp/cmm.cmi asmcomp/arch.cmo \
asmcomp/proc.cmi
asmcomp/proc.cmx : asmcomp/reg.cmx utils/misc.cmx asmcomp/mach.cmx \
asmcomp/intel_proc.cmx utils/config.cmx asmcomp/cmm.cmx asmcomp/arch.cmx \

View File

@ -13,6 +13,7 @@
(* Emission of Intel x86_64 assembly code *)
open Misc
open Cmm
open Arch
open Proc
@ -898,7 +899,6 @@ let emit_item = function
(* Beginning / end of an assembly file *)
let begin_assembly() =
arch64 := true;
Intel_proc.reset_asm_code ();
reset_debug_info(); (* PR#5603 *)
float_constants := [];

View File

@ -1042,7 +1042,6 @@ let data l =
(* Beginning / end of an assembly file *)
let begin_assembly() =
arch64 := false;
Intel_proc.reset_asm_code ();
reset_debug_info(); (* PR#5603 *)
float_constants := [];
@ -1136,6 +1135,3 @@ let end_assembly() =
else Intel_gas.bprint_instr
in
Intel_proc.generate_code oc bprint_instr
let _ =
arch64 := false

View File

@ -42,6 +42,9 @@ FDIVR ST(i), ST(0)
open Intel_ast
open Intel_proc
let tab b = Buffer.add_char b '\t'
let bprint b s = tab b; Buffer.add_string b s
let string_of_table = function
| Some PLT -> "@PLT"
| Some GOTPCREL -> "@GOTPCREL"
@ -143,7 +146,7 @@ let bprint_arg_mem b string_of_register ptr ( mem : 'a addr ) =
let bprint_arg arch b arg =
let bprint_arg b arg =
match arg with
(* | ConstantInt int -> Printf.bprintf b "$%d" int *)
| Imm (_, (None, int) ) ->
@ -178,25 +181,25 @@ let bprint_arg arch b arg =
| Mem (ptr, M64 addr) ->
bprint_arg_mem b string_of_register64 ptr addr
let bprint_args arch b instr args =
let bprint_args b instr args =
match args, instr with
| [], _ -> ()
| [ (* this is the encoding of jump labels: don't use * *)
Mem (_, M64 (Some (RIP, _, _), (Some _,_)))
| Mem (_, M32 (None, (Some _, _)))
as arg ], (CALL _ | JMP _)
-> tab b; bprint_arg arch b arg
-> tab b; bprint_arg b arg
| [ Reg32 _
| Reg64 _
| Mem _
as arg ], (CALL _ | JMP _) ->
tab b; Buffer.add_char b '*'; bprint_arg arch b arg
| [ arg ], _ -> tab b; bprint_arg arch b arg
tab b; Buffer.add_char b '*'; bprint_arg b arg
| [ arg ], _ -> tab b; bprint_arg b arg
| [ arg1; arg2 ], _ ->
tab b; bprint_arg arch b arg1;
tab b; bprint_arg b arg1;
Buffer.add_char b ',';
Buffer.add_char b ' ';
bprint_arg arch b arg2
bprint_arg b arg2
| _ -> assert false
let rec string_of_constant = function
@ -265,7 +268,7 @@ let auto_suffix ins arg =
let list_o arg = match arg with None -> [] | Some arg -> [arg]
let bprint_instr b arch instr =
let bprint_instr b instr =
begin
match instr with
Global s ->
@ -497,7 +500,7 @@ let bprint_instr b arch instr =
| BSWAP arg -> "bswap", [ arg ]
in
bprint b ins;
bprint_args arch b instr args;
bprint_args b instr args;
end;
Buffer.add_string b "\n"

View File

@ -13,6 +13,9 @@
open Intel_ast
open Intel_proc
let tab b = Buffer.add_char b '\t'
let bprint b s = tab b; Buffer.add_string b s
let string_of_data_size = function
| B64 -> "QWORD"
| B32 -> "DWORD"
@ -131,7 +134,7 @@ let bprint_arg_mem b string_of_register ptr mem =
offset
(* TODO: remove need of "ins" *)
let bprint_arg arch b arg =
let bprint_arg b arg =
match arg with
(* | ConstantInt int -> Printf.bprintf b "%d" int *)
| Imm ( (B8|B16), (None, int)) ->
@ -172,15 +175,15 @@ let bprint_arg arch b arg =
bprint_arg_mem b string_of_register64 ptr addr
let bprint_args arch b instr args =
let bprint_args b instr args =
match args with
| [] -> ()
| [ arg ] -> tab b; bprint_arg arch b arg
| [ arg ] -> tab b; bprint_arg b arg
| [ arg2; arg1 ] ->
tab b; bprint_arg arch b arg1;
tab b; bprint_arg b arg1;
Buffer.add_char b ',';
Buffer.add_char b ' ';
bprint_arg arch b arg2
bprint_arg b arg2
| _ -> assert false
let rec string_of_constant = function
@ -242,7 +245,7 @@ let buf_bytes_directive b directive s =
let list_o arg = match arg with None -> [] | Some arg -> [arg]
let bprint_instr_name b arch instr =
let bprint_instr_name b instr =
match instr with
| Global s ->
Printf.bprintf b "\tPUBLIC\t%s" s
@ -414,10 +417,10 @@ let bprint_instr_name b arch instr =
in
bprint b name;
bprint_args arch b instr args;
bprint_args b instr args;
()
let bprint_instr b arch instr =
bprint_instr_name b arch instr;
let bprint_instr b instr =
bprint_instr_name b instr;
Buffer.add_string b "\n"

View File

@ -10,6 +10,8 @@
(* *)
(***********************************************************************)
[@@@ocaml.warning "+A-42-4"]
(* OCAMLASM variable: a ':'-separated string of keywords
With Intel_assembler module:
@ -25,11 +27,7 @@
*)
module StringSet = Set.Make(struct type t = string let compare = compare end)
module StringMap = Map.Make(struct type t = string let compare = compare end)
module IntSet = Set.Make(struct type t = int let compare x y = x - y end)
module IntMap = Map.Make(struct type t = int let compare x y = x - y end)
open Misc
open Intel_ast
type system =
@ -238,12 +236,6 @@ let string_of_condition condition =
| O -> "o"
let tab b = Buffer.add_char b '\t'
let bprint b s = tab b; Buffer.add_string b s
(* Set in asmcomp/{amd64|i386}/emit.mlp at begin_assembly *)
let arch64 = ref true
(* [print_assembler] is used to decide whether assembly code
should be printed in the .s file or not. *)
let print_assembler = ref true
@ -253,7 +245,7 @@ let print_assembler = ref true
let assembler_passes = ref ([] : (asm_program -> asm_program) list)
exception AsmAborted
let final_assembler = ref (fun _ -> raise AsmAborted)
let final_assembler = ref None
(* Which asm conventions to use *)
let masm =
@ -336,17 +328,16 @@ let split_sections instrs =
let assemble_code instrs =
try
let assembler = !final_assembler system in
let assembler =
match !final_assembler with
| None -> raise AsmAborted
| Some f -> f
in
if List.mem "no" env_OCAMLASM then begin
Printf.eprintf "Warning: binary generation prevented by OCAMLASM\n%!";
raise AsmAborted;
end;
(* Printf.eprintf "Intel_assembler.assemble_code...\n%!"; *)
let machine = if !arch64 then X64 else X86 in
let sections = split_sections instrs in
let bin = assembler machine sections in
let bin = assembler (split_sections instrs) in
binary_content := Some bin
with AsmAborted ->
if List.mem "yes" env_OCAMLASM then begin
@ -362,12 +353,6 @@ let generate_code oc bprint_instr =
assemble_code instrs;
if !print_assembler then
let b = Buffer.create 10000 in
List.iter (bprint_instr b !arch64) instrs;
List.iter (bprint_instr b) instrs;
let s = Buffer.contents b in
output_string oc s
let string_of_data_size = function
| B8 -> "B8"
| B16 -> "B16"
| B32 -> "B32"
| B64 -> "B64"

74
asmcomp/intel_proc.mli Normal file
View File

@ -0,0 +1,74 @@
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Fabrice Le Fessant, 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. *)
(* *)
(***********************************************************************)
(** Definitions shared between the 32 and 64 bit Intel backends. *)
open Intel_ast
(** Helpers for textual emitters *)
val string_of_register8: register8 -> string
val string_of_register16: register16 -> string
val string_of_register32: register32 -> string
val string_of_register64: register64 -> string
val string_of_registerf: registerf -> string
val string_of_string_literal: string -> string
val string_of_condition: condition -> string
val string_of_datatype: data_type -> string
val string_of_symbol: (*prefix*) string -> string -> string
(** Buffer of assembly code *)
val emit: instruction -> unit
val directive: asm_line -> unit
val reset_asm_code: unit -> unit
(** Code emission *)
val assemble_file: (*infile*) string -> (*outfile*) string -> (*retcode*) int
val generate_code: out_channel -> (Buffer.t -> Intel_ast.asm_line -> unit) -> unit
(** System detection *)
type system =
(* 32 bits and 64 bits *)
| S_macosx
| S_gnu
| S_cygwin
(* 32 bits only *)
| S_solaris
| S_win32
| S_linux_elf
| S_bsd_elf
| S_beos
| S_mingw
(* 64 bits only *)
| S_win64
| S_linux
| S_mingw64
| S_unknown
val system: system
val masm: bool
(** Support for plumbing a binary code emitter *)
val final_assembler: (Intel_ast.section Misc.StringMap.t -> string) option ref
(** Hooks for rewriting the assembly code *)
val assembler_passes: (asm_program -> asm_program) list ref

View File

@ -325,3 +325,7 @@ let split s c =
let cut_at s c =
let pos = String.index s c in
String.sub s 0 pos, String.sub s (pos+1) (String.length s - pos - 1)
module StringSet = Set.Make(struct type t = string let compare = compare end)
module StringMap = Map.Make(struct type t = string let compare = compare end)

View File

@ -159,3 +159,9 @@ val cut_at : string -> char -> string * string
Raise [Not_found] if the character does not appear in the string
@since 4.01
*)
module StringSet: Set.S with type elt = string
module StringMap: Map.S with type key = string
(* TODO: replace all custom instantiations of StringSet/StringMap in various
compiler modules with this one. *)