1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Fabrice Le Fessant, projet Gallium, INRIA Rocquencourt *)
(* *)
(* Copyright 2014 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
open X86_ast
type system =
| S_macosx
| S_gnu
| S_cygwin
| S_solaris
| S_beos
| S_win64
| S_linux
| S_mingw64
| S_freebsd
| S_netbsd
| S_openbsd
| S_unknown
let system = match Config.system with
| "macosx" -> S_macosx
| "gnu" -> S_gnu
| "cygwin" -> S_cygwin
| "solaris" -> S_solaris
| "beos" -> S_beos
| "win64" -> S_win64
| "linux" -> S_linux
| "mingw64" -> S_mingw64
| "freebsd" -> S_freebsd
| "netbsd" -> S_netbsd
| "openbsd" -> S_openbsd
| _ -> S_unknown
let windows =
match system with
| S_mingw64 | S_cygwin | S_win64 -> true
| _ -> false
let string_of_string_literal s =
let b = Buffer.create (String.length s + 2) in
let last_was_escape = ref false in
for i = 0 to String.length s - 1 do
let c = s.[i] in
if c >= '0' && c <= '9' then
if !last_was_escape
then Printf.bprintf b "\\%o" (Char.code c)
else Buffer.add_char b c
else if c >= ' ' && c <= '~' && c <> '"' (* '"' *) && c <> '\\' then begin
Buffer.add_char b c;
last_was_escape := false
end else begin
Printf.bprintf b "\\%o" (Char.code c);
last_was_escape := true
end
done;
Buffer.contents b
let string_of_symbol prefix s =
let is_special_char = function
| 'A'..'Z' | 'a'..'z' | '0'..'9' | '_' -> false
| c -> c <> Compilenv.symbol_separator
in
let spec = String.exists is_special_char s in
if not spec then if prefix = "" then s else prefix ^ s
else
let b = Buffer.create (String.length s + 10) in
Buffer.add_string b prefix;
String.iter
(fun c ->
if is_special_char c then
Printf.bprintf b "%s%02x" Compilenv.escape_prefix (Char.code c)
else
Buffer.add_char b c
)
s;
Buffer.contents b
let buf_bytes_directive b directive s =
let pos = ref 0 in
for i = 0 to String.length s - 1 do
if !pos = 0
then begin
if i > 0 then Buffer.add_char b '\n';
Buffer.add_char b '\t';
Buffer.add_string b directive;
Buffer.add_char b '\t';
end
else Buffer.add_char b ',';
Printf.bprintf b "%d" (Char.code s.[i]);
incr pos;
if !pos >= 16 then begin pos := 0 end
done
let string_of_reg64 = function
| RAX -> "rax"
| RBX -> "rbx"
| RDI -> "rdi"
| RSI -> "rsi"
| RDX -> "rdx"
| RCX -> "rcx"
| RBP -> "rbp"
| RSP -> "rsp"
| R8 -> "r8"
| R9 -> "r9"
| R10 -> "r10"
| R11 -> "r11"
| R12 -> "r12"
| R13 -> "r13"
| R14 -> "r14"
| R15 -> "r15"
let string_of_reg8l = function
| RAX -> "al"
| RBX -> "bl"
| RCX -> "cl"
| RDX -> "dl"
| RSP -> "spl"
| RBP -> "bpl"
| RSI -> "sil"
| RDI -> "dil"
| R8 -> "r8b"
| R9 -> "r9b"
| R10 -> "r10b"
| R11 -> "r11b"
| R12 -> "r12b"
| R13 -> "r13b"
| R14 -> "r14b"
| R15 -> "r15b"
let string_of_reg8h = function
| AH -> "ah"
| BH -> "bh"
| CH -> "ch"
| DH -> "dh"
let string_of_reg16 = function
| RAX -> "ax"
| RBX -> "bx"
| RCX -> "cx"
| RDX -> "dx"
| RSP -> "sp"
| RBP -> "bp"
| RSI -> "si"
| RDI -> "di"
| R8 -> "r8w"
| R9 -> "r9w"
| R10 -> "r10w"
| R11 -> "r11w"
| R12 -> "r12w"
| R13 -> "r13w"
| R14 -> "r14w"
| R15 -> "r15w"
let string_of_reg32 = function
| RAX -> "eax"
| RBX -> "ebx"
| RCX -> "ecx"
| RDX -> "edx"
| RSP -> "esp"
| RBP -> "ebp"
| RSI -> "esi"
| RDI -> "edi"
| R8 -> "r8d"
| R9 -> "r9d"
| R10 -> "r10d"
| R11 -> "r11d"
| R12 -> "r12d"
| R13 -> "r13d"
| R14 -> "r14d"
| R15 -> "r15d"
let string_of_registerf = function
| XMM n -> Printf.sprintf "xmm%d" n
| TOS -> Printf.sprintf "tos"
| ST n -> Printf.sprintf "st(%d)" n
let string_of_condition = function
| E -> "e"
| AE -> "ae"
| A -> "a"
| GE -> "ge"
| G -> "g"
| NE -> "ne"
| B -> "b"
| BE -> "be"
| L -> "l"
| LE -> "le"
| NP -> "np"
| P -> "p"
| NS -> "ns"
| S -> "s"
| NO -> "no"
| O -> "o"
let string_of_float_condition = function
| EQf -> "eq"
| LTf -> "lt"
| LEf -> "le"
| UNORDf -> "unord"
| NEQf -> "neq"
| NLTf -> "nlt"
| NLEf -> "nle"
| ORDf -> "ord"
let string_of_rounding = function
| RoundDown -> "roundsd.down"
| RoundUp -> "roundsd.up"
| RoundTruncate -> "roundsd.trunc"
| RoundNearest -> "roundsd.near"
let internal_assembler = ref None
let register_internal_assembler f = internal_assembler := Some f
let with_internal_assembler assemble k =
Misc.protect_refs [ R (internal_assembler, Some assemble) ] k
(* Which asm conventions to use *)
let masm =
match system with
| S_win64 -> true
| _ -> false
let use_plt =
match system with
| S_macosx | S_mingw64 | S_cygwin | S_win64 -> false
| _ -> !Clflags.dlcode
(* Shall we use an external assembler command ?
If [binary_content] contains some data, we can directly
save it. Otherwise, we have to ask an external command.
*)
let binary_content = ref None
let compile infile outfile =
if masm then
Ccomp.command (Config.asm ^
Filename.quote outfile ^ " " ^ Filename.quote infile ^
(if !Clflags.verbose then "" else ">NUL"))
else
Ccomp.command (Config.asm ^ " " ^
(String.concat " " (Misc.debug_prefix_map_flags ())) ^
" -o " ^ Filename.quote outfile ^ " " ^
Filename.quote infile)
let assemble_file infile outfile =
match !binary_content with
| None -> compile infile outfile
| Some content -> content outfile; binary_content := None; 0
let asm_code = ref []
let directive dir = asm_code := dir :: !asm_code
let emit ins = directive (Ins ins)
let reset_asm_code () = asm_code := []
let generate_code asm =
let instrs = List.rev !asm_code in
begin match asm with
| Some f -> f instrs
| None -> ()
end;
begin match !internal_assembler with
| Some f -> binary_content := Some (f instrs)
| None -> binary_content := None
end
|