caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Interface between Ocaml and Assembler
@ 2008-01-04  9:42 David LONY
  2008-01-04  9:43 ` [Caml-list] " Alain Frisch
  0 siblings, 1 reply; 4+ messages in thread
From: David LONY @ 2008-01-04  9:42 UTC (permalink / raw)
  To: caml-list

Hi all,

Is it possible to interface assembler code with an Ocaml code ? I 
mean..is it possible to call a Ocaml function from an assembler code by 
using the asm instruction "call my_function" (gas asm)?

Thanks a lot.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Interface between Ocaml and Assembler
  2008-01-04  9:42 Interface between Ocaml and Assembler David LONY
@ 2008-01-04  9:43 ` Alain Frisch
  2008-01-04 16:45   ` David LONY
  0 siblings, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2008-01-04  9:43 UTC (permalink / raw)
  To: David LONY; +Cc: caml-list

David LONY wrote:
> Is it possible to interface assembler code with an Ocaml code ? I 
> mean..is it possible to call a Ocaml function from an assembler code by 
> using the asm instruction "call my_function" (gas asm)?

Yes, you can call an OCaml function from assembler using the same method 
as from C, that is through caml_callback* functions (see Section 18.7.1 
in the OCaml manual). However, dealing with the GC properly in assembler 
is tricky: you'll have to reproduce the behavior of the CAMLlocal and 
CAMLparam macros.

-- Alain


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Interface between Ocaml and Assembler
  2008-01-04  9:43 ` [Caml-list] " Alain Frisch
@ 2008-01-04 16:45   ` David LONY
  2008-01-04 16:58     ` Xavier Leroy
  0 siblings, 1 reply; 4+ messages in thread
From: David LONY @ 2008-01-04 16:45 UTC (permalink / raw)
  To: Alain Frisch; +Cc: caml-list

Ok ok..

But it will be easier to make a "bridge" like this one : "asm code" -> 
"C code"->"Ocaml code" ?
For instance I made 3 program :
The first one in assembly (gas.S):
.text

.global _start

_start:
        call main
        ret

The second in C (test.c):

#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>

int test_char(int n)
{
  static value * test_char_closure = NULL;
 
  if (test_char_closure == NULL)
        {
        test_char_closure = caml_named_value("caml_test_char");
        }
       
  return Int_val(caml_callback(*test_char_closure, Val_int(n)));
}

int main(int argc, char ** argv)
{
  int result;
  caml_startup(argv);
  result = test_char(10);
  printf("%d\n", result);
  return 0;
}

and the last in Ocaml (truc.ml) :

let test_char a = a+1
let _ = Callback.register "caml_test_char" test_char;;

But when I try to compile and link all object it fails...
as gas.S -o gas.o
gcc -c test.c -o test.o
ocamlopt -output-obj truc.ml -o truc_main.o
ld -o test gas.o test.o truc_main.o -L/usr/lib/ocaml/3.09.2/ -lasmrun 
-lm -ldl

/usr/lib/ocaml/3.09.2//libasmrun.a(ints.o): In function 
`caml_int64_of_string':
(.text+0xb4b): undefined reference to `__udivdi3'
/usr/lib/ocaml/3.09.2//libasmrun.a(ints.o): In function `caml_int64_mod':
(.text+0xe55): undefined reference to `__moddi3'
/usr/lib/ocaml/3.09.2//libasmrun.a(ints.o): In function `caml_int64_div':
(.text+0xe9e): undefined reference to `__divdi3'

Does anyone has a solution ?

Regards.
David LONY








Alain Frisch a écrit :
> David LONY wrote:
>> Is it possible to interface assembler code with an Ocaml code ? I 
>> mean..is it possible to call a Ocaml function from an assembler code 
>> by using the asm instruction "call my_function" (gas asm)?
>
> Yes, you can call an OCaml function from assembler using the same 
> method as from C, that is through caml_callback* functions (see 
> Section 18.7.1 in the OCaml manual). However, dealing with the GC 
> properly in assembler is tricky: you'll have to reproduce the behavior 
> of the CAMLlocal and CAMLparam macros.
>
> -- Alain
>
>
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Interface between Ocaml and Assembler
  2008-01-04 16:45   ` David LONY
@ 2008-01-04 16:58     ` Xavier Leroy
  0 siblings, 0 replies; 4+ messages in thread
From: Xavier Leroy @ 2008-01-04 16:58 UTC (permalink / raw)
  To: David LONY; +Cc: Alain Frisch, caml-list

> But it will be easier to make a "bridge" like this one : "asm code" ->
> "C code"->"Ocaml code" ?

Yes, very much easier.

> ld -o test gas.o test.o truc_main.o -L/usr/lib/ocaml/3.09.2/ -lasmrun
> -lm -ldl
>
> /usr/lib/ocaml/3.09.2//libasmrun.a(ints.o): In function
> `caml_int64_of_string':
> (.text+0xb4b): undefined reference to `__udivdi3'
>
> Does anyone has a solution ?

Use "gcc" instead of "ld" for the final linking step.

- Xavier Leroy


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-01-04 16:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-04  9:42 Interface between Ocaml and Assembler David LONY
2008-01-04  9:43 ` [Caml-list] " Alain Frisch
2008-01-04 16:45   ` David LONY
2008-01-04 16:58     ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).