Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sys.word_size and related fields should be constants #5355

Closed
vicuna opened this issue Sep 5, 2011 · 2 comments
Closed

Sys.word_size and related fields should be constants #5355

vicuna opened this issue Sep 5, 2011 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Sep 5, 2011

Original bug ID: 5355
Reporter: jfc
Status: closed (set by @xavierleroy on 2015-12-11T18:04:22Z)
Resolution: not a bug
Priority: normal
Severity: minor
Version: 3.12.1
Category: ~DO NOT USE (was: OCaml general)
Monitored by: "Pascal Cuoq" @ygrek

Bug description

I want to make some code conditional at compile time on the word size, i.e. I don't want the comparison word_size=64 to be done at runtime. In OCaml 3.12 Sys.word_size is initialized by a function call at program startup. The value is not available to the compiler. However, the value will not change from compile to runtime. It should be treated as a constant the same as sizeof() in C.

I have sample code showing where this matters. I attached a diff showing how to make Sys.word_size a constant. Pervasives.max_int and related values should be treated the same way. See caml-list traffic of September 5, 2011 for more background.

Additional information

module type M = sig type t val add : t -> t -> t val of_int : int -> t end;;
module Int : M = struct type t = int let add = (+) let of_int x = x end;;
module M = (val (if Sys.word_size = 64 then (module Int : M) else (module Int32 : M)) : M);;

(* The intent is for the module M to provide the usual int
operations on a 64 bit system and degenerate to boxed
integers and function calls on 32 bit systems. This is
appropriate when an integer of 32-63 bits is required.

In OCaml 3.12 the test (Sys.word_size = 64) is done at
runtime and the functions below make indirect calls
through the module.

If Sys.word_size is changed to a compile time constant
ocamlopt looks through the module pack and unpack and,
on a 64 bit system, compiles these functions to use
plain "int" addition. *)

let f x = M.add (M.of_int x) (M.of_int 1)

let g x = x + Sys.word_size

File attachments

@vicuna
Copy link
Author

vicuna commented Sep 6, 2011

Comment author: @xavierleroy

This patch would break something much more important: bytecode executables generated by ocamlc are portable across 32 and 64-bit platforms. Therefore, Sys.word_size must be determined at program start-up time, and cannot be a link-time constant. (Otherwise, a bytecode executable generated on a 32-bit machine would run with the wrong word_size on a 64-bit machine.) So, thanks, but no, thanks.

A slightly more acceptable approach would be

let word_size = if 1 lsl 32 = 0 then 32 else 64

As long as the bytecode compiler doesn't do compile-time constant folding, the right value will be computed at program start-up time. On the other hand, ocamlopt folds the constant at compile-time. This won't help with native cross-compilation between 32 and 64-bit platforms, but such cross-compilation is already broken in many other ways.

At any rate, your particular problem should be solved by writing

module M = (val (if 1 lsl 32 <> 0 then (module Int : M) else (module Int32 : M)) : M);;

@vicuna
Copy link
Author

vicuna commented Mar 14, 2012

Comment author: @xavierleroy

In my proposed approach, please replace "1 lsl 32" with "1 lsl 31" to avoid running into "unspecified behavior" territory.

In the absence of further discussion, I'm moving this PR to "no change required".

@vicuna vicuna closed this as completed Dec 11, 2015
@vicuna vicuna added the bug label Mar 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant