Browse thread
Using OCaml's run-time from LLVM-generated native code
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jon Harrop <jon@f...> |
| Subject: | Re: [Caml-list] Using OCaml's run-time from LLVM-generated native code |
On Monday 04 February 2008 07:03:17 Alain Frisch wrote:
> Jon Harrop wrote:
> > How does OCaml's stack walker work with C code, for example? In
> > particular, how does it know what is a pointer into the heap from a C
> > stack frame? Must it be explicitly disabled?
>
> The OCaml runtime does not scan the stack frames corresponding to C
> functions.
How does it know which stack frames correspond to C functions?
> Jon, it is somewhat weird that you spend so much time writing about
> forking OCaml and do not take a few minutes to read the source code. The
> macros CAMLparam*, CAMLlocal* are not really that mysterious.
Despite the availability of that code it seems that few people can use it
correctly and I am one of them.
This seems to work even though it calls full_major aggressively:
#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/callback.h>
#include <caml/custom.h>
#include <caml/intext.h>
extern value caml_gc_full_major(value v);
CAMLprim value fib(value nv) {
CAMLparam1(nv);
CAMLlocal5(a, b, c, d, e);
int64 n = Int64_val(nv);
if (n < 2) CAMLreturn(nv);
a = copy_int64(n-1);
b = copy_int64(n-2);
c = fib(a);
d = fib(b);
e = copy_int64(Int64_val(c) + Int64_val(d));
caml_gc_full_major(0);
CAMLreturn(e);
}
int apply(int n) {
CAMLlocal2(nv, fibn);
nv = copy_int64(n);
fibn = fib(nv);
caml_gc_full_major(0);
return Int64_val(fib(nv));
}
int main(int argc, char* argv[]) {
caml_main(argv);
printf("%d\n", apply(argc == 2 ? atoi(argv[1]) : 10));
return 0;
}
Is that correct code?
Rather than messing around with these macros in each and every function it is
probably easier and more efficient to register a single global root at entry,
pointing to a shadow stack and push and pop elements to and from that
directly.
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e