<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/11/002147abdef4f6fe5248093cd8c75a75"
  from="Kim Nguyen &lt;nguyen@b...&gt;"
  author="Kim Nguyen"
  date="2003-11-22T00:18:27"
  subject="Re: [Caml-list] Accuracy of Gc.stat ()"
  prev="2003/11/6cb4163c04ade05bfaa283746117ab71"
  next="2003/11/129e434e9f65be9f23d03cd3e04f32ac"
  prev-in-thread="2003/11/974acf8ee1bf674c02076f709368a862"
  next-in-thread="2003/11/b0824ff17e581379b1c4a9a60ef71fb5"
  prev-thread="2003/11/b9a42a206431fb25d03c3e4bf43cc125"
  next-thread="2003/11/58c657c03c7683b0214401b6eee2a21f"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Accuracy of Gc.stat ()">
<msg 
  url="2003/11/f5ceb88194f4324b692f89b6f3398ff2"
  from="Daniel_Bünzli &lt;daniel.buenzli@e...&gt;"
  author="Daniel_Bünzli"
  date="2003-11-19T17:52:08"
  subject="[Caml-list] Accuracy of Gc.stat ()">
<msg 
  url="2003/11/974acf8ee1bf674c02076f709368a862"
  from="Damien Doligez &lt;damien.doligez@i...&gt;"
  author="Damien Doligez"
  date="2003-11-21T16:45:52"
  subject="Re: [Caml-list] Accuracy of Gc.stat ()">
</msg>
<msg 
  url="2003/11/002147abdef4f6fe5248093cd8c75a75"
  from="Kim Nguyen &lt;nguyen@b...&gt;"
  author="Kim Nguyen"
  date="2003-11-22T00:18:27"
  subject="Re: [Caml-list] Accuracy of Gc.stat ()">
<msg 
  url="2003/11/b0824ff17e581379b1c4a9a60ef71fb5"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-11-22T11:44:11"
  subject="Re: [Caml-list] Accuracy of Gc.stat ()">
<msg 
  url="2003/11/8f2405258c33a120ad0ca9f46f985eb6"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-11-22T11:49:12"
  subject="Re: [Caml-list] Accuracy of Gc.stat ()">
<msg 
  url="2003/11/47c7a7b20eb96e9e1835662fe1651255"
  from="Daniel_Bünzli &lt;daniel.buenzli@e...&gt;"
  author="Daniel_Bünzli"
  date="2003-11-22T14:20:36"
  subject="Self-detection of native code execution (Was Re: [Caml-list] Accuracy of Gc.stat ())">
<msg 
  url="2003/11/26ef24002e41d7ebabfb7229a3904059"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-11-22T14:28:54"
  subject="Re: Self-detection of native code execution (Was Re: [Caml-list] Accuracy of Gc.stat ())">
</msg>
</msg>
<msg 
  url="2003/11/92dbb3eff27c1305dfb13cc95807a0c3"
  from="Kim Nguyen &lt;nguyen@b...&gt;"
  author="Kim Nguyen"
  date="2003-11-22T14:25:55"
  subject="Re: [Caml-list] Accuracy of Gc.stat ()">
<msg 
  url="2003/11/6ee5dd7ed01812dc5e6b44c4bdc4eaff"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-11-22T14:31:14"
  subject="Re: [Caml-list] Accuracy of Gc.stat ()">
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
Hi,

On Wed, 19 Nov 2003 18:52:10 +0100
Daniel Bünzli &lt;daniel.buenzli@epfl.ch&gt; wrote:

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

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 -&gt; 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 &lt;caml/mlvalues.h&gt;

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.



</contents>

</message>

