~james/scope

scope/lib/address.ml -rw-r--r-- 864 bytes
6e057d53 — james chore(import): snapshot james.dad.town/scope at 034e6b67d44c 14 hours ago
                                                                                
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
module Virtual = struct
  type t = int64
  let of_int64 x = x
  let to_int64 x = x
end

module Physical = struct
  type t = int64
  let of_int64 x = x
  let to_int64 x = x
end

module Machine = struct
  type t = int64
  let of_int64 x = x
  let to_int64 x = x
end

let compare = Int64.unsigned_compare
let hex x = Printf.sprintf "0x%016Lx" x

let parse s =
  if String.length s = 0 || s.[0] = '-' || s.[0] = '+' then
    invalid_arg ("invalid unsigned address: " ^ s);
  try Int64.of_string (if String.starts_with ~prefix:"0x" s then s else "0u" ^ s)
  with Failure _ -> invalid_arg ("invalid unsigned address: " ^ s)

let bit x n = Int64.logand x (Int64.shift_left 1L n) <> 0L
let mask n = Int64.pred (Int64.shift_left 1L n)
let align x n = Int64.logand x (Int64.lognot (mask n))
let canonical x =
  let high = Int64.shift_right x 47 in
  high = 0L || high = -1L