| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
n |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
open Names |
| 16 |
open Vmvalues |
| 17 |
open Vmbytecodes |
| 18 |
open Vmopcodes |
| 19 |
open Mod_subst |
| 20 |
open CPrimitives |
| 21 |
|
| 22 |
type emitcodes = String.t |
| 23 |
|
| 24 |
external tcode_of_code : Bytes.t -> Vmvalues.tcode = "rocq_tcode_of_code" |
| 25 |
|
| 26 |
|
| 27 |
type reloc_info = |
| 28 |
| Reloc_annot of annot_switch |
| 29 |
| Reloc_const of structured_constant |
| 30 |
| Reloc_getglobal of Names.Constant.t |
| 31 |
| Reloc_caml_prim of caml_prim |
| 32 |
|
| 33 |
let eq_reloc_info r1 r2 = match r1, r2 with |
| 34 |
| Reloc_annot sw1, Reloc_annot sw2 -> eq_annot_switch sw1 sw2 |
| 35 |
| Reloc_annot _, _ -> false |
| 36 |
| Reloc_const c1, Reloc_const c2 -> eq_structured_constant c1 c2 |
| 37 |
| Reloc_const _, _ -> false |
| 38 |
| Reloc_getglobal c1, Reloc_getglobal c2 -> Constant.CanOrd.equal c1 c2 |
| 39 |
| Reloc_getglobal _, _ -> false |
| 40 |
| Reloc_caml_prim p1, Reloc_caml_prim p2 -> CPrimitives.equal (caml_prim_to_prim p1) (caml_prim_to_prim p2) |
| 41 |
| Reloc_caml_prim _, _ -> false |
| 42 |
|
| 43 |
let hash_reloc_info r = |
| 44 |
let open Hashset.Combine in |
| 45 |
match r with |
| 46 |
| Reloc_annot sw -> combinesmall 1 (hash_annot_switch sw) |
| 47 |
| Reloc_const c -> combinesmall 2 (hash_structured_constant c) |
| 48 |
| Reloc_getglobal c -> combinesmall 3 (Constant.CanOrd.hash c) |
| 49 |
| Reloc_caml_prim p -> combinesmall 4 (CPrimitives.hash (caml_prim_to_prim p)) |
| 50 |
|
| 51 |
module RelocTable = Hashtbl.Make(struct |
| 52 |
type t = reloc_info |
| 53 |
let equal = eq_reloc_info |
| 54 |
let hash = hash_reloc_info |
| 55 |
end) |
| 56 |
|
| 57 |
module Positions : |
| 58 |
sig |
| 59 |
type t |
| 60 |
val of_list : int list -> t |
| 61 |
val iter : (int -> unit) -> t -> unit |
| 62 |
end = |
| 63 |
struct |
| 64 |
|
| 65 |
type t = string |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
let output buf n = |
| 73 |
if n <= 0xFF then Buffer.add_uint8 buf n |
| 74 |
else |
| 75 |
let () = Buffer.add_uint8 buf 0x00 in |
| 76 |
Buffer.add_int32_be buf (Int32.of_int n) |
| 77 |
|
| 78 |
let input s pos = |
| 79 |
let c = Char.code s.[!pos] in |
| 80 |
if Int.equal c 0x00 then |
| 81 |
let c = String.get_int32_be s (!pos+1) in |
| 82 |
let () = pos := !pos + 5 in |
| 83 |
Int32.to_int c |
| 84 |
else |
| 85 |
let () = pos := !pos + 1 in |
| 86 |
c |
| 87 |
|
| 88 |
let of_list l = match l with |
| 89 |
| [] -> "" |
| 90 |
| n :: l -> |
| 91 |
let buf = Buffer.create 16 in |
| 92 |
let () = assert (0 < n) in |
| 93 |
let () = output buf n in |
| 94 |
let rec aux cur l = match l with |
| 95 |
| [] -> () |
| 96 |
| n :: l -> |
| 97 |
let () = assert (cur < n) in |
| 98 |
let () = output buf (n - cur) in |
| 99 |
aux n l |
| 100 |
in |
| 101 |
let () = aux n l in |
| 102 |
Buffer.contents buf |
| 103 |
|
| 104 |
let iter f s = |
| 105 |
let pos = ref 0 in |
| 106 |
let len = String.length s in |
| 107 |
let cur = ref 0 in |
| 108 |
while !pos < len do |
| 109 |
let n = input s pos in |
| 110 |
let () = cur := n + !cur in |
| 111 |
f !cur |
| 112 |
done |
| 113 |
|
| 114 |
end |
| 115 |
|
| 116 |
module NonSubstReloc = |
| 117 |
struct |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
type t = |
| 122 |
| SReloc_Const_sort of Sorts.t |
| 123 |
| SReloc_Const_evar of Evar.t |
| 124 |
| SReloc_Const_b0 of tag |
| 125 |
| SReloc_Const_univ_instance of UVars.Instance.t |
| 126 |
| SReloc_Const_val of structured_values |
| 127 |
| SReloc_Const_uint of Uint63.t |
| 128 |
| SReloc_Const_float of Float64.t |
| 129 |
| SReloc_Const_string of Pstring.t |
| 130 |
| SReloc_annot of annot_switch |
| 131 |
| SReloc_caml_prim of caml_prim |
| 132 |
|
| 133 |
let to_reloc = function |
| 134 |
| SReloc_Const_sort s -> Reloc_const (Const_sort s) |
| 135 |
| SReloc_Const_evar e -> Reloc_const (Const_evar e) |
| 136 |
| SReloc_Const_b0 tag -> Reloc_const (Const_b0 tag) |
| 137 |
| SReloc_Const_univ_instance u -> Reloc_const (Const_univ_instance u) |
| 138 |
| SReloc_Const_val v -> Reloc_const (Const_val v) |
| 139 |
| SReloc_Const_uint i -> Reloc_const (Const_uint i) |
| 140 |
| SReloc_Const_float f -> Reloc_const (Const_float f) |
| 141 |
| SReloc_Const_string s -> Reloc_const (Const_string s) |
| 142 |
| SReloc_annot annot -> Reloc_annot annot |
| 143 |
| SReloc_caml_prim prm -> Reloc_caml_prim prm |
| 144 |
|
| 145 |
end |
| 146 |
|
| 147 |
module Reloc = |
| 148 |
struct |
| 149 |
|
| 150 |
type t = |
| 151 |
| SReloc_Const_ind of inductive |
| 152 |
| SReloc_getglobal of Names.Constant.t |
| 153 |
| SReloc_indirect of int |
| 154 |
|
| 155 |
let to_reloc table = function |
| 156 |
| SReloc_Const_ind ind -> Reloc_const (Const_ind ind) |
| 157 |
| SReloc_getglobal cst -> Reloc_getglobal cst |
| 158 |
| SReloc_indirect i -> NonSubstReloc.to_reloc table.(i) |
| 159 |
|
| 160 |
let subst s reloc = match reloc with |
| 161 |
| SReloc_Const_ind ind -> |
| 162 |
let ind' = Mod_subst.subst_ind s ind in |
| 163 |
if ind' == ind then reloc else SReloc_Const_ind ind' |
| 164 |
| SReloc_getglobal cst -> |
| 165 |
let cst' = Mod_subst.subst_constant s cst in |
| 166 |
if cst' == cst then reloc else SReloc_getglobal cst' |
| 167 |
| SReloc_indirect _ -> reloc |
| 168 |
|
| 169 |
end |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
let compress_code src sz = |
| 178 |
let buf = Buffer.create (sz * 3 / 8) in |
| 179 |
for i = 0 to sz / 4 - 1 do |
| 180 |
let c01 = Bytes.get_uint16_le src (i * 4) in |
| 181 |
let c23 = Bytes.get_uint16_le src (i * 4 + 2) in |
| 182 |
if c23 = 0 then |
| 183 |
if c01 < 252 then |
| 184 |
Buffer.add_uint8 buf c01 |
| 185 |
else |
| 186 |
begin |
| 187 |
Buffer.add_uint8 buf 253; |
| 188 |
Buffer.add_uint16_le buf c01; |
| 189 |
end |
| 190 |
else if c23 = 0xffff && c01 >= 0xff00 then |
| 191 |
begin |
| 192 |
Buffer.add_uint8 buf 252; |
| 193 |
Buffer.add_uint8 buf c01; |
| 194 |
end |
| 195 |
else if c23 <= 0xff then |
| 196 |
begin |
| 197 |
Buffer.add_uint8 buf 254; |
| 198 |
Buffer.add_uint16_le buf c01; |
| 199 |
Buffer.add_uint8 buf c23; |
| 200 |
end |
| 201 |
else |
| 202 |
begin |
| 203 |
Buffer.add_uint8 buf 255; |
| 204 |
Buffer.add_uint16_le buf c01; |
| 205 |
Buffer.add_uint16_le buf c23; |
| 206 |
end |
| 207 |
done; |
| 208 |
Buffer.contents buf |
| 209 |
|
| 210 |
let decompress_code src = |
| 211 |
let sz = String.length src in |
| 212 |
let buf = Buffer.create (sz * 4) in |
| 213 |
let i = ref 0 in |
| 214 |
while !i < sz do |
| 215 |
let c01, c23 = |
| 216 |
match String.get src !i with |
| 217 |
| '\000' .. '\251' as c -> |
| 218 |
i := !i + 1; |
| 219 |
(Char.code c, 0) |
| 220 |
| '\252' -> |
| 221 |
i := !i + 2; |
| 222 |
(String.get_uint8 src (!i - 1) + 0xff00, 0xffff) |
| 223 |
| '\253' -> |
| 224 |
i := !i + 3; |
| 225 |
(String.get_uint16_le src (!i - 2), 0) |
| 226 |
| '\254' -> |
| 227 |
i := !i + 4; |
| 228 |
(String.get_uint16_le src (!i - 3), String.get_uint8 src (!i - 1)) |
| 229 |
| '\255' -> |
| 230 |
i := !i + 5; |
| 231 |
(String.get_uint16_le src (!i - 4), String.get_int16_le src (!i - 2)) |
| 232 |
in |
| 233 |
Buffer.add_uint16_le buf c01; |
| 234 |
Buffer.add_uint16_le buf c23; |
| 235 |
done; |
| 236 |
Buffer.to_bytes buf |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
type patches = { |
| 241 |
reloc_infos : Reloc.t array; |
| 242 |
} |
| 243 |
|
| 244 |
type to_patch = { |
| 245 |
tp_code : emitcodes; |
| 246 |
tp_fv : fv; |
| 247 |
tp_pos : Positions.t; |
| 248 |
tp_reloc : NonSubstReloc.t array; |
| 249 |
} |
| 250 |
|
| 251 |
let patch_int tp reloc = |
| 252 |
let buff = decompress_code tp.tp_code in |
| 253 |
let iter pos = |
| 254 |
let id = Bytes.get_int32_le buff pos in |
| 255 |
let reloc = reloc.(Int32.to_int id) in |
| 256 |
Bytes.set_int32_le buff pos (Int32.of_int reloc) |
| 257 |
in |
| 258 |
let () = Positions.iter iter tp.tp_pos in |
| 259 |
buff |
| 260 |
|
| 261 |
let patch (tp, pl) f = |
| 262 |
let f r = f (Reloc.to_reloc tp.tp_reloc r) in |
| 263 |
let reloc = CArray.map_left f pl.reloc_infos in |
| 264 |
let buff = patch_int tp reloc in |
| 265 |
tcode_of_code buff, tp.tp_fv |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
type label_definition = |
| 270 |
Label_defined of int |
| 271 |
| Label_undefined of (int * int) list |
| 272 |
|
| 273 |
type env = { |
| 274 |
mutable out_buffer : Bytes.t; |
| 275 |
mutable out_position : int; |
| 276 |
mutable reloc_pos : int list; |
| 277 |
mutable reloc_id : int; |
| 278 |
mutable label_table : label_definition array; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
reloc_info : int RelocTable.t; |
| 285 |
} |
| 286 |
|
| 287 |
let out_word env b1 b2 b3 b4 = |
| 288 |
let p = env.out_position in |
| 289 |
let buf = |
| 290 |
let len = Bytes.length env.out_buffer in |
| 291 |
if p + 3 < len then env.out_buffer |
| 292 |
else |
| 293 |
let new_len = min (Sys.max_string_length) (2 * len) in |
| 294 |
|
| 295 |
let () = if not (p + 3 < new_len) then Vmerrors.too_large_code() in |
| 296 |
let new_buffer = Bytes.create new_len in |
| 297 |
let () = Bytes.blit env.out_buffer 0 new_buffer 0 len in |
| 298 |
let () = env.out_buffer <- new_buffer in |
| 299 |
new_buffer |
| 300 |
in |
| 301 |
let () = Bytes.set_uint8 buf p b1 in |
| 302 |
let () = Bytes.set_uint8 buf (p + 1) b2 in |
| 303 |
let () = Bytes.set_uint8 buf (p + 2) b3 in |
| 304 |
let () = Bytes.set_uint8 buf (p + 3) b4 in |
| 305 |
env.out_position <- p + 4 |
| 306 |
|
| 307 |
let out env opcode = |
| 308 |
out_word env opcode 0 0 0 |
| 309 |
|
| 310 |
let is_immed i = Uint63.le (Uint63.of_int i) Uint63.maxuint31 |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
let is_accu_dead = function |
| 316 |
| [] -> false |
| 317 |
| c :: _ -> |
| 318 |
match c with |
| 319 |
| Kacc _ | Kenvacc _ | Kconst _ | Koffsetclosure _ | Kgetglobal _ -> true |
| 320 |
| _ -> false |
| 321 |
|
| 322 |
let out_int env n = |
| 323 |
out_word env n (n asr 8) (n asr 16) (n asr 24) |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
let extend_label_table env needed = |
| 328 |
let new_size = ref(Array.length env.label_table) in |
| 329 |
while needed >= !new_size do new_size := 2 * !new_size done; |
| 330 |
let new_table = Array.make !new_size (Label_undefined []) in |
| 331 |
Array.blit env.label_table 0 new_table 0 (Array.length env.label_table); |
| 332 |
env.label_table <- new_table |
| 333 |
|
| 334 |
let backpatch env (pos, orig) = |
| 335 |
let displ = (env.out_position - orig) asr 2 in |
| 336 |
Bytes.set_int32_le env.out_buffer pos (Int32.of_int displ) |
| 337 |
|
| 338 |
let define_label env lbl = |
| 339 |
if lbl >= Array.length env.label_table then extend_label_table env lbl; |
| 340 |
match (env.label_table).(lbl) with |
| 341 |
Label_defined _ -> |
| 342 |
raise(Failure "CEmitcode.define_label") |
| 343 |
| Label_undefined patchlist -> |
| 344 |
List.iter (fun p -> backpatch env p) patchlist; |
| 345 |
(env.label_table).(lbl) <- Label_defined env.out_position |
| 346 |
|
| 347 |
let out_label_with_orig env orig lbl = |
| 348 |
if lbl >= Array.length env.label_table then extend_label_table env lbl; |
| 349 |
match (env.label_table).(lbl) with |
| 350 |
Label_defined def -> |
| 351 |
out_int env ((def - orig) asr 2) |
| 352 |
| Label_undefined patchlist -> |
| 353 |
(env.label_table).(lbl) <- |
| 354 |
Label_undefined((env.out_position, orig) :: patchlist); |
| 355 |
out_int env 0 |
| 356 |
|
| 357 |
let out_label env l = out_label_with_orig env env.out_position l |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
let enter env info = |
| 362 |
let pos = env.out_position in |
| 363 |
let () = env.reloc_pos <- pos :: env.reloc_pos in |
| 364 |
try RelocTable.find env.reloc_info info |
| 365 |
with Not_found -> |
| 366 |
let id = env.reloc_id in |
| 367 |
let () = env.reloc_id <- id + 1 in |
| 368 |
let () = RelocTable.add env.reloc_info info id in |
| 369 |
id |
| 370 |
|
| 371 |
let slot_for env r = out_int env (enter env r) |
| 372 |
|
| 373 |
let slot_for_const env c = slot_for env (Reloc_const c) |
| 374 |
let slot_for_annot env a = slot_for env (Reloc_annot a) |
| 375 |
let slot_for_getglobal env p = slot_for env (Reloc_getglobal p) |
| 376 |
let slot_for_caml_prim env op = slot_for env (Reloc_caml_prim op) |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
let check_prim_op = function |
| 381 |
| Int63head0 -> opCHECKHEAD0INT63 |
| 382 |
| Int63tail0 -> opCHECKTAIL0INT63 |
| 383 |
| Int63add -> opCHECKADDINT63 |
| 384 |
| Int63sub -> opCHECKSUBINT63 |
| 385 |
| Int63mul -> opCHECKMULINT63 |
| 386 |
| Int63div -> opCHECKDIVINT63 |
| 387 |
| Int63mod -> opCHECKMODINT63 |
| 388 |
| Int63divs -> opCHECKDIVSINT63 |
| 389 |
| Int63mods -> opCHECKMODSINT63 |
| 390 |
| Int63lsr -> opCHECKLSRINT63 |
| 391 |
| Int63lsl -> opCHECKLSLINT63 |
| 392 |
| Int63asr -> opCHECKASRINT63 |
| 393 |
| Int63land -> opCHECKLANDINT63 |
| 394 |
| Int63lor -> opCHECKLORINT63 |
| 395 |
| Int63lxor -> opCHECKLXORINT63 |
| 396 |
| Int63addc -> opCHECKADDCINT63 |
| 397 |
| Int63subc -> opCHECKSUBCINT63 |
| 398 |
| Int63addCarryC -> opCHECKADDCARRYCINT63 |
| 399 |
| Int63subCarryC -> opCHECKSUBCARRYCINT63 |
| 400 |
| Int63mulc -> opCHECKMULCINT63 |
| 401 |
| Int63diveucl -> opCHECKDIVEUCLINT63 |
| 402 |
| Int63div21 -> opCHECKDIV21INT63 |
| 403 |
| Int63addMulDiv -> opCHECKADDMULDIVINT63 |
| 404 |
| Int63eq -> opCHECKEQINT63 |
| 405 |
| Int63lt -> opCHECKLTINT63 |
| 406 |
| Int63le -> opCHECKLEINT63 |
| 407 |
| Int63lts -> opCHECKLTSINT63 |
| 408 |
| Int63les -> opCHECKLESINT63 |
| 409 |
| Int63compare -> opCHECKCOMPAREINT63 |
| 410 |
| Int63compares -> opCHECKCOMPARESINT63 |
| 411 |
| Float64opp -> opCHECKOPPFLOAT |
| 412 |
| Float64abs -> opCHECKABSFLOAT |
| 413 |
| Float64eq -> opCHECKEQFLOAT |
| 414 |
| Float64lt -> opCHECKLTFLOAT |
| 415 |
| Float64le -> opCHECKLEFLOAT |
| 416 |
| Float64compare -> opCHECKCOMPAREFLOAT |
| 417 |
| Float64equal -> opCHECKEQUALFLOAT |
| 418 |
| Float64classify -> opCHECKCLASSIFYFLOAT |
| 419 |
| Float64add -> opCHECKADDFLOAT |
| 420 |
| Float64sub -> opCHECKSUBFLOAT |
| 421 |
| Float64mul -> opCHECKMULFLOAT |
| 422 |
| Float64div -> opCHECKDIVFLOAT |
| 423 |
| Float64sqrt -> opCHECKSQRTFLOAT |
| 424 |
| Float64ofUint63 -> opCHECKFLOATOFINT63 |
| 425 |
| Float64normfr_mantissa -> opCHECKFLOATNORMFRMANTISSA |
| 426 |
| Float64frshiftexp -> opCHECKFRSHIFTEXP |
| 427 |
| Float64ldshiftexp -> opCHECKLDSHIFTEXP |
| 428 |
| Float64next_up -> opCHECKNEXTUPFLOAT |
| 429 |
| Float64next_down -> opCHECKNEXTDOWNFLOAT |
| 430 |
| Arraymake | Arrayget | Arrayset | Arraydefault | Arraycopy | Arraylength |
| 431 |
| Stringmake | Stringlength | Stringget | Stringsub | Stringcat | Stringcompare |
| 432 |
-> assert false |
| 433 |
|
| 434 |
let check_caml_prim_op = function |
| 435 |
| CAML_Arraymake -> opCHECKCAMLCALL2_1 |
| 436 |
| CAML_Arrayget -> opCHECKCAMLCALL2 |
| 437 |
| CAML_Arrayset -> opCHECKCAMLCALL3_1 |
| 438 |
| CAML_Arraydefault | CAML_Arraycopy | CAML_Arraylength -> opCHECKCAMLCALL1 |
| 439 |
| CAML_Stringmake -> opCHECKCAMLCALL2 |
| 440 |
| CAML_Stringlength -> opCHECKCAMLCALL1 |
| 441 |
| CAML_Stringget -> opCHECKCAMLCALL2 |
| 442 |
| CAML_Stringsub -> opCHECKCAMLCALL3 |
| 443 |
| CAML_Stringcat | CAML_Stringcompare -> opCHECKCAMLCALL2 |
| 444 |
|
| 445 |
let inplace_prim_op = function |
| 446 |
| Float64next_up | Float64next_down -> true |
| 447 |
| _ -> false |
| 448 |
|
| 449 |
let check_prim_op_inplace = function |
| 450 |
| Float64next_up -> opCHECKNEXTUPFLOATINPLACE |
| 451 |
| Float64next_down -> opCHECKNEXTDOWNFLOATINPLACE |
| 452 |
| _ -> assert false |
| 453 |
|
| 454 |
let emit_instr env = function |
| 455 |
| Klabel lbl -> define_label env lbl |
| 456 |
| Kacc n -> |
| 457 |
if n < 8 then out env(opACC0 + n) else (out env opACC; out_int env n) |
| 458 |
| Kenvacc n -> |
| 459 |
if n >= 0 && n <= 3 |
| 460 |
then out env(opENVACC0 + n) |
| 461 |
else (out env opENVACC; out_int env n) |
| 462 |
| Koffsetclosure ofs -> |
| 463 |
if Int.equal ofs 0 || Int.equal ofs 1 |
| 464 |
then out env (opOFFSETCLOSURE0 + ofs) |
| 465 |
else (out env opOFFSETCLOSURE; out_int env ofs) |
| 466 |
| Kpush -> |
| 467 |
out env opPUSH |
| 468 |
| Kpop n -> |
| 469 |
out env opPOP; out_int env n |
| 470 |
| Kpush_retaddr lbl -> |
| 471 |
out env opPUSH_RETADDR; out_label env lbl |
| 472 |
| Kshort_apply n -> |
| 473 |
assert (1 <= n && n <= 4); |
| 474 |
out env(opAPPLY1 + n - 1) |
| 475 |
| Kapply n -> |
| 476 |
out env opAPPLY; out_int env n |
| 477 |
| Kappterm(n, sz) -> |
| 478 |
if n < 4 then (out env(opAPPTERM1 + n - 1); out_int env sz) |
| 479 |
else (out env opAPPTERM; out_int env n; out_int env sz) |
| 480 |
| Kreturn n -> |
| 481 |
out env opRETURN; out_int env n |
| 482 |
| Kjump -> |
| 483 |
out env opRETURN; out_int env 0 |
| 484 |
| Krestart -> |
| 485 |
out env opRESTART |
| 486 |
| Kgrab n -> |
| 487 |
out env opGRAB; out_int env n |
| 488 |
| Kgrabrec(rec_arg) -> |
| 489 |
out env opGRABREC; out_int env rec_arg |
| 490 |
| Kclosure(lbl, n) -> |
| 491 |
out env opCLOSURE; out_int env n; out_label env lbl |
| 492 |
| Kclosurerec(nfv,init,lbl_types,lbl_bodies) -> |
| 493 |
out env opCLOSUREREC;out_int env (Array.length lbl_bodies); |
| 494 |
out_int env nfv; out_int env init; |
| 495 |
let org = env.out_position in |
| 496 |
Array.iter (out_label_with_orig env org) lbl_types; |
| 497 |
let org = env.out_position in |
| 498 |
Array.iter (out_label_with_orig env org) lbl_bodies |
| 499 |
| Kclosurecofix(nfv,init,lbl_types,lbl_bodies) -> |
| 500 |
out env opCLOSURECOFIX;out_int env (Array.length lbl_bodies); |
| 501 |
out_int env nfv; out_int env init; |
| 502 |
let org = env.out_position in |
| 503 |
Array.iter (out_label_with_orig env org) lbl_types; |
| 504 |
let org = env.out_position in |
| 505 |
Array.iter (out_label_with_orig env org) lbl_bodies |
| 506 |
| Kgetglobal q -> |
| 507 |
out env opGETGLOBAL; slot_for_getglobal env q |
| 508 |
| Ksubstinstance u -> |
| 509 |
out env opSUBSTINSTANCE; slot_for_const env (Const_univ_instance u) |
| 510 |
| Kconst (Const_b0 i) when is_immed i -> |
| 511 |
if i >= 0 && i <= 3 |
| 512 |
then out env (opCONST0 + i) |
| 513 |
else (out env opCONSTINT; out_int env i) |
| 514 |
| Kconst c -> |
| 515 |
out env opGETGLOBAL; slot_for_const env c |
| 516 |
| Kmakeblock(n, t) -> |
| 517 |
if 0 < n && n < 4 then (out env(opMAKEBLOCK1 + n - 1); out_int env t) |
| 518 |
else (out env opMAKEBLOCK; out_int env n; out_int env t) |
| 519 |
| Kmakeswitchblock(typlbl,swlbl,annot,sz) -> |
| 520 |
out env opMAKESWITCHBLOCK; |
| 521 |
out_label env typlbl; out_label env swlbl; |
| 522 |
slot_for_annot env annot;out_int env sz |
| 523 |
| Kswitch (tbl_const, tbl_block) -> |
| 524 |
let lenb = Array.length tbl_block in |
| 525 |
let lenc = Array.length tbl_const in |
| 526 |
assert (lenb < 0x100 && lenc < 0x1000000); |
| 527 |
out env opSWITCH; |
| 528 |
out_word env lenb lenc (lenc asr 8) (lenc asr 16); |
| 529 |
|
| 530 |
let org = env.out_position in |
| 531 |
Array.iter (out_label_with_orig env org) tbl_const; |
| 532 |
Array.iter (out_label_with_orig env org) tbl_block |
| 533 |
| Kpushfields n -> |
| 534 |
out env opPUSHFIELDS;out_int env n |
| 535 |
| Kfield n -> |
| 536 |
if n <= 1 then out env (opGETFIELD0+n) |
| 537 |
else (out env opGETFIELD;out_int env n) |
| 538 |
| Ksetfield n -> |
| 539 |
out env opSETFIELD; out_int env n |
| 540 |
| Ksequence _ -> invalid_arg "Vmemitcodes.emit_instr" |
| 541 |
| Kproj p -> out env opPROJ; out_int env p |
| 542 |
| Kensurestackcapacity size -> out env opENSURESTACKCAPACITY; out_int env size |
| 543 |
| Kbranch lbl -> out env opBRANCH; out_label env lbl |
| 544 |
| Kprim (op, (q,_u)) -> |
| 545 |
out env (check_prim_op op); |
| 546 |
slot_for_getglobal env q |
| 547 |
|
| 548 |
| Kcamlprim (op,lbl) -> |
| 549 |
out env (check_caml_prim_op op); |
| 550 |
out_label env lbl; |
| 551 |
slot_for_caml_prim env op |
| 552 |
|
| 553 |
| Kstop -> out env opSTOP |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
let rec emit env insns remaining = match insns with |
| 558 |
| [] -> |
| 559 |
(match remaining with |
| 560 |
[] -> () |
| 561 |
| (first::rest) -> emit env first rest) |
| 562 |
|
| 563 |
| Kpush :: Kacc n :: c -> |
| 564 |
let rec aux n c nb = |
| 565 |
match c with |
| 566 |
| Kpush :: Kacc j :: c when j = n -> aux n c (nb + 1) |
| 567 |
| _ -> (nb, c) in |
| 568 |
let (nb, c') = aux n c 1 in |
| 569 |
if nb >= 3 || (nb >= 2 && n > 7) |
| 570 |
then ( |
| 571 |
out env opPUSHACCMANY; out_int env n; out_int env nb; |
| 572 |
emit env c' remaining) |
| 573 |
else ( |
| 574 |
if n = 0 then out env opPUSH |
| 575 |
else if n < 8 then out env (opPUSHACC1 + n - 1) |
| 576 |
else (out env opPUSHACC; out_int env n); |
| 577 |
emit env c remaining) |
| 578 |
| Kpush :: Kenvacc n :: c -> |
| 579 |
let rec aux n c nb = |
| 580 |
match c with |
| 581 |
| Kpush :: Kenvacc j :: c when j = n - nb -> aux n c (nb + 1) |
| 582 |
| _ -> (nb, c) in |
| 583 |
let (nb, c') = aux n c 1 in |
| 584 |
if nb >= 3 || (nb >= 2 && n > 3) |
| 585 |
then ( |
| 586 |
out env opPUSHENVACCMANY; out_int env (n - nb + 1); out_int env nb; |
| 587 |
emit env c' remaining) |
| 588 |
else ( |
| 589 |
if n >= 0 && n <= 3 |
| 590 |
then out env (opPUSHENVACC0 + n) |
| 591 |
else (out env opPUSHENVACC; out_int env n); |
| 592 |
emit env c remaining) |
| 593 |
| Kpush :: Koffsetclosure ofs :: c -> |
| 594 |
if Int.equal ofs 0 || Int.equal ofs 1 |
| 595 |
then out env(opPUSHOFFSETCLOSURE0 + ofs) |
| 596 |
else (out env opPUSHOFFSETCLOSURE; out_int env ofs); |
| 597 |
emit env c remaining |
| 598 |
| Kpush :: Kgetglobal id :: c -> |
| 599 |
out env opPUSHGETGLOBAL; slot_for_getglobal env id; emit env c remaining |
| 600 |
| Kpush :: Kconst (Const_b0 i) :: c when is_immed i -> |
| 601 |
if i >= 0 && i <= 3 |
| 602 |
then out env (opPUSHCONST0 + i) |
| 603 |
else (out env opPUSHCONSTINT; out_int env i); |
| 604 |
emit env c remaining |
| 605 |
| Kpush :: Kconst const :: c -> |
| 606 |
out env opPUSHGETGLOBAL; slot_for_const env const; |
| 607 |
emit env c remaining |
| 608 |
| Kpushfields 1 :: c when is_accu_dead c -> |
| 609 |
out env opGETFIELD0; |
| 610 |
emit env (Kpush :: c) remaining |
| 611 |
| Kpop n :: Kjump :: c -> |
| 612 |
out env opRETURN; out_int env n; emit env c remaining |
| 613 |
| Ksequence c1 :: c -> |
| 614 |
emit env c1 (c :: remaining) |
| 615 |
| Kprim (op1, (q1, _)) :: Kprim (op2, (q2, _)) :: c when inplace_prim_op op2 -> |
| 616 |
out env (check_prim_op op1); |
| 617 |
slot_for_getglobal env q1; |
| 618 |
out env (check_prim_op_inplace op2); |
| 619 |
slot_for_getglobal env q2; |
| 620 |
emit env c remaining |
| 621 |
|
| 622 |
| instr :: c -> |
| 623 |
emit_instr env instr; emit env c remaining |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
let subst_patches subst p = |
| 628 |
let infos = CArray.Smart.map (fun r -> Reloc.subst subst r) p.reloc_infos in |
| 629 |
{ reloc_infos = infos } |
| 630 |
|
| 631 |
type 'a pbody_code = |
| 632 |
| BCdefined of bool array * 'a * patches |
| 633 |
| BCalias of Names.Constant.t |
| 634 |
| BCconstant |
| 635 |
|
| 636 |
type body_code = to_patch pbody_code |
| 637 |
|
| 638 |
let subst_body_code s = function |
| 639 |
| BCdefined (m, x, tp) -> BCdefined (m, x, subst_patches s tp) |
| 640 |
| BCalias cu -> BCalias (subst_constant s cu) |
| 641 |
| BCconstant -> BCconstant |
| 642 |
|
| 643 |
let to_memory fv code = |
| 644 |
let env = { |
| 645 |
out_buffer = Bytes.create 1024; |
| 646 |
out_position = 0; |
| 647 |
reloc_id = 0; |
| 648 |
reloc_pos = []; |
| 649 |
label_table = Array.make 16 (Label_undefined []); |
| 650 |
reloc_info = RelocTable.create 91; |
| 651 |
} in |
| 652 |
emit env code []; |
| 653 |
let code = compress_code env.out_buffer env.out_position in |
| 654 |
let _, code = CString.hcons code in |
| 655 |
let fold reloc id accu = (id, reloc) :: accu in |
| 656 |
let reloc = RelocTable.fold fold env.reloc_info [] in |
| 657 |
let reloc = List.sort (fun (id1, _) (id2, _) -> Int.compare id1 id2) reloc in |
| 658 |
let uid = ref 0 in |
| 659 |
let table = ref [] in |
| 660 |
let push r = |
| 661 |
let id = !uid in |
| 662 |
let () = table := r :: !table in |
| 663 |
let () = incr uid in |
| 664 |
Reloc.SReloc_indirect id |
| 665 |
in |
| 666 |
let map (_, r) = |
| 667 |
let open NonSubstReloc in |
| 668 |
match r with |
| 669 |
| Reloc_getglobal cst -> Reloc.SReloc_getglobal cst |
| 670 |
| Reloc_const (Const_ind ind) -> Reloc.SReloc_Const_ind ind |
| 671 |
| Reloc_annot annot -> push (SReloc_annot annot) |
| 672 |
| Reloc_caml_prim prm -> push (SReloc_caml_prim prm) |
| 673 |
| Reloc_const (Const_sort s) -> push (SReloc_Const_sort s) |
| 674 |
| Reloc_const (Const_evar e) -> push (SReloc_Const_evar e) |
| 675 |
| Reloc_const (Const_b0 tag) -> push (SReloc_Const_b0 tag) |
| 676 |
| Reloc_const (Const_univ_instance u) -> push (SReloc_Const_univ_instance u) |
| 677 |
| Reloc_const (Const_val v) -> push (SReloc_Const_val v) |
| 678 |
| Reloc_const (Const_uint i) -> push (SReloc_Const_uint i) |
| 679 |
| Reloc_const (Const_float f) -> push (SReloc_Const_float f) |
| 680 |
| Reloc_const (Const_string s) -> push (SReloc_Const_string s) |
| 681 |
in |
| 682 |
let reloc_infos = CArray.map_of_list map reloc in |
| 683 |
let positions = Positions.of_list (List.rev env.reloc_pos) in |
| 684 |
let reloc = { reloc_infos } in |
| 685 |
let to_patch = { |
| 686 |
tp_code = code; |
| 687 |
tp_fv = fv; |
| 688 |
tp_pos = positions; |
| 689 |
tp_reloc = CArray.rev_of_list !table; |
| 690 |
} in |
| 691 |
Array.iter (fun lbl -> |
| 692 |
(match lbl with |
| 693 |
Label_defined _ -> assert true |
| 694 |
| Label_undefined patchlist -> |
| 695 |
assert (patchlist = []))) env.label_table; |
| 696 |
(to_patch, reloc) |