Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Kim Nguyen <nguyen@bk.ru>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Accuracy of Gc.stat ()
Date: Sat, 22 Nov 2003 01:20:54 +0100	[thread overview]
Message-ID: <20031122012054.0dbd44e2.nguyen@bk.ru> (raw)
In-Reply-To: <1970C334-1AB9-11D8-ADB3-000393DBC266@epfl.ch>

[-- Attachment #1: Type: text/plain, Size: 1645 bytes --]

Hi,

On Wed, 19 Nov 2003 18:52:10 +0100
Daniel Bünzli <daniel.buenzli@epfl.ch> wrote:

> 
> 3) Is it possible to know at runtime whether we are running native code 
> or interpreted bytecode ?
> 

There is actualy an (ugly) hack which seems to work, but it requires few lines of C codes :

--- prog.ml --

(* An external declaration, first C function is used when compiled to bytecode, 
    second when compiled to native code.
*)

external is_bytecode : unit -> bool = "is_bytecode_bytecode" "is_bytecode_native"

(* And now you can use it... *)
let _ = 
	if is_bytecode () 
	then 
	print_endline "Bytecode !!!"
	else
	print_endline "Native code !!!"

-----------

--- bytecode.c -----

#include <caml/mlvalues.h>

CAMLprim value is_bytecode_bytecode(value unit){
  return Val_true;
}
CAMLprim value is_bytecode_native(value unit){
  return Val_false;
}

-------------

Et voilà !

Linking native caml code with this C code isn't a big deal but, linking with
bytecode make you somehow loose the portability of the later. 
See the corresponding chapter in the Ocaml manual (Chapter 18).

$ gcc -c bytecode.c
$ ocamlc -make-runtime -o myruntime bytecode.o
$ ocamlc -o prog -use-runtime ./myruntime prog.ml
$ ocamlopt -o prog.opt bytecode.o prog.ml
$ ./prog
Bytecode !!!
$ ./prog.opt
Native code !!!

I wonder if there is a cleaner way to do this. Maybe there could be a flag like
 the Sys.interactive one. I'd like to know how "safe" is this code, the behavior 
of the compiler is only described for function with more than 5 arguments in 
the manual.


Cheers.

Kim Nguyen.



[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

  parent reply	other threads:[~2003-11-22  0:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-19 17:52 Daniel Bünzli
2003-11-21 16:46 ` Damien Doligez
2003-11-22  0:20 ` Kim Nguyen [this message]
2003-11-22 11:43   ` Richard Jones
2003-11-22 11:49     ` Richard Jones
2003-11-22 14:20       ` Self-detection of native code execution (Was Re: [Caml-list] Accuracy of Gc.stat ()) Daniel Bünzli
2003-11-22 14:28         ` Richard Jones
2003-11-22 14:28       ` [Caml-list] Accuracy of Gc.stat () Kim Nguyen
2003-11-22 14:31         ` Richard Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031122012054.0dbd44e2.nguyen@bk.ru \
    --to=nguyen@bk.ru \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox