jon.recoil.org

Source file sys.ml

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
(* stdlib/sys.ml.  Generated from sys.ml.in by configure. *)
#3 "sys.ml.in"
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*             Xavier Leroy, projet Cristal, 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 GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

open! Stdlib

type backend_type =
  | Native
  | Bytecode
  | Other of string
(* System interface *)

external get_config: unit -> string * int * bool @@ portable = "caml_sys_get_config"
external get_executable_name : unit -> string @@ portable = "caml_sys_executable_name"
external argv : string array @@ portable = "%sys_argv"
external big_endian : unit -> bool @@ portable = "%big_endian"
external word_size : unit -> int @@ portable = "%word_size"
external int_size : unit -> int @@ portable = "%int_size"
external max_wosize : unit -> int @@ portable = "%max_wosize"
external unix : unit -> bool @@ portable = "%ostype_unix"
external win32 : unit -> bool @@ portable = "%ostype_win32"
external cygwin : unit -> bool @@ portable = "%ostype_cygwin"
external get_backend_type : unit -> backend_type @@ portable = "%backend_type"

let executable_name = get_executable_name()
let (os_type, _, _) = get_config()
(* CR with-kinds: This reassignment is necessary to let the [string] mode cross. *)
let os_type = os_type
let backend_type = get_backend_type ()
let big_endian = big_endian ()
let word_size = word_size ()
let int_size = int_size ()
let unix = unix ()
let win32 = win32 ()
let cygwin = cygwin ()
let max_array_length = max_wosize ()
let max_floatarray_length = max_array_length / (64 / word_size)
let max_string_length = word_size / 8 * max_array_length - 1

(* In bytecode, [float# array] is treated as [float array].
   Using [max_floatarray_length] assumes flat float arrays are enabled. *)
let max_unboxed_float_array_length = max_floatarray_length


let max_unboxed_float32_array_length =
  match backend_type with
  | Native -> max_array_length * (word_size / 32)
  | Bytecode | Other _ -> max_array_length

let max_unboxed_int64_array_length =
  match backend_type with
  | Native -> max_array_length / (64 / word_size)
  | Bytecode | Other _ -> max_array_length

let max_unboxed_int32_array_length =
  match backend_type with
  | Native -> max_array_length * (word_size / 32)
  | Bytecode | Other _ -> max_array_length

let max_untagged_int16_array_length =
  match backend_type with
  | Native -> max_array_length * (word_size / 16)
  | Bytecode | Other _ -> max_array_length

let max_untagged_int8_array_length =
  match backend_type with
  | Native -> max_array_length * (word_size / 8)
  | Bytecode | Other _ -> max_array_length

let max_untagged_int_array_length =
  match backend_type with
  | Native -> max_array_length
  | Bytecode | Other _ -> max_array_length

let max_unboxed_nativeint_array_length =
  match backend_type with
  | Native -> max_array_length
  | Bytecode | Other _ -> max_array_length

let max_unboxed_vec128_array_length =
  match backend_type with
  | Native -> max_array_length / (128 / word_size)
  | Bytecode | Other _ -> max_array_length

let max_unboxed_vec256_array_length =
  match backend_type with
  | Native -> max_array_length / (256 / word_size)
  | Bytecode | Other _ -> max_array_length

external runtime_variant : unit -> string @@ portable = "caml_runtime_variant"
external runtime_parameters : unit -> string @@ portable = "caml_runtime_parameters"
external poll_actions : unit -> unit @@ portable = "%poll"

external file_exists: string -> bool @@ portable = "caml_sys_file_exists"
external is_directory : string -> bool @@ portable = "caml_sys_is_directory"
external is_regular_file : string -> bool @@ portable = "caml_sys_is_regular_file"
external remove: string -> unit @@ portable = "caml_sys_remove"
external rename : string -> string -> unit @@ portable = "caml_sys_rename"
external getenv: string -> string @@ portable = "caml_sys_getenv"

let getenv_opt s =
  (* TODO: expose a non-raising primitive directly. *)
  try Some (getenv s)
  with Not_found -> None

external command: string -> int @@ portable = "caml_sys_system_command"
external time: unit -> (float [@unboxed]) @@ portable =
  "caml_sys_time" "caml_sys_time_unboxed" [@@noalloc]
external chdir: string -> unit @@ portable = "caml_sys_chdir"
external mkdir: string -> int -> unit @@ portable = "caml_sys_mkdir"
external rmdir: string -> unit @@ portable = "caml_sys_rmdir"
external getcwd: unit -> string @@ portable = "caml_sys_getcwd"
external readdir : string -> string array @@ portable = "caml_sys_read_directory"

let interactive = ref false

type signal_behavior =
    Signal_default
  | Signal_ignore
  | Signal_handle of (int -> unit)

module Safe = struct
  external signal
    : int -> signal_behavior @ portable -> signal_behavior @ portable @@ portable
    = "caml_install_signal_handler"

  let set_signal sig_num sig_beh = ignore(signal sig_num sig_beh)
end

include Safe

let sigabrt = -1
let sigalrm = -2
let sigfpe = -3
let sighup = -4
let sigill = -5
let sigint = -6
let sigkill = -7
let sigpipe = -8
let sigquit = -9
let sigsegv = -10
let sigterm = -11
let sigusr1 = -12
let sigusr2 = -13
let sigchld = -14
let sigcont = -15
let sigstop = -16
let sigtstp = -17
let sigttin = -18
let sigttou = -19
let sigvtalrm = -20
let sigprof = -21
let sigbus = -22
let sigpoll = -23
let sigsys = -24
let sigtrap = -25
let sigurg = -26
let sigxcpu = -27
let sigxfsz = -28

exception Break
(* We don't use [Callback] or [Obj] because of circular dependencies.
   The string "Sys.Break" must match runtime/fail.c. *)
external register_named_value : string -> 'a -> unit
                              = "caml_register_named_value"
(* Various places in the runtime rely on the "Sys.Break" name. *)
let () = register_named_value "Sys.Break" Break

let catch_break on =
  if on then
    set_signal sigint (Signal_handle(fun _ -> raise Break))
  else
    set_signal sigint Signal_default

external with_async_exns : (unit -> 'a) -> 'a @@ portable = "caml_with_async_exns"

external enable_runtime_warnings: bool -> unit @@ portable =
  "caml_ml_enable_runtime_warnings"
external runtime_warnings_enabled: unit -> bool @@ portable =
  "caml_ml_runtime_warnings_enabled"

(* The version string is found in file ../VERSION *)

let ocaml_version = "5.2.0+ox"

let development_version = false

type extra_prefix = Plus | Tilde

type extra_info = extra_prefix * string

type ocaml_release_info = {
  major : int;
  minor : int;
  patchlevel : int;
  extra : extra_info option
}

let ocaml_release = {
  major = 5;
  minor = 2;
  patchlevel = 0;
  extra = Some (Plus, "ox")
}

(* Optimization *)

external[@layout_poly] opaque_identity :
  ('a : any). ('a[@local_opt]) -> ('a[@local_opt]) @@ portable = "%opaque"

module Immediate64 = struct
  module type Non_immediate = sig
    type t
  end
  module type Immediate = sig
    type t [@@immediate]
  end

  module Make(Immediate : Immediate)(Non_immediate : Non_immediate) = struct
    type t [@@immediate64]
    type 'a repr =
      | Immediate : Immediate.t repr
      | Non_immediate : Non_immediate.t repr
    external magic : _ repr -> t repr @@ portable = "%identity"
    let repr =
      if word_size = 64 then
        magic Immediate
      else
        magic Non_immediate
  end
end

(* Unsafe implementations of [signal] *)

external signal
  : int -> signal_behavior -> signal_behavior
  = "caml_install_signal_handler"

let set_signal sig_num sig_beh = ignore(signal sig_num sig_beh)

(* Expose the value of [Config.architecture] *)
(* CR-someday gyorsh: utils/target_system.ml already has [architecture].
   Consider moving to [config.ml] or here. *)
type arch =
  | Amd64
  | Arm64

external amd64 : unit -> bool @@ portable = "%arch_amd64"
external arm64 : unit -> bool @@ portable = "%arch_arm64"

let amd64 = amd64 ()
let arm64 = arm64 ()

let arch =
  match amd64, arm64 with
  | true, true -> failwith "Cannot determine architecture: both amd64 and arm64 detected"
  | false, false -> failwith "Cannot determine architecture: must be amd64 or arm64"
  | true, false -> Amd64
  | false, true -> Arm64
;;