English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: [Caml-list] How to write a CUDA kernel in ocaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-12-16 (06:26)
From: Basile STARYNKEVITCH <basile@s...>
Subject: Re: [Caml-list] How to write a CUDA kernel in ocaml?
David Allsopp wrote:
> Basile Starynkevitch wrote: 
>> Eray Ozkural wrote:
>> Compiling Ocaml to efficient C is not easy and probably impossible (or
>> extremely difficult) in the general case. In
>> particular, tail recursive calls are essential in Ocaml, and are not
>> available in C in most compilers.
> What's this based on (out of interest)? Most C compilers don't necessarily
> identify tail recursion in *C* code but if you're emitting C as an OCaml
> backend then there's no reason not to convert tail recursive *OCaml*
> functions to C code based on goto or similar looping constructs (yes, you'd
> almost-always-virtually-never use goto in a hand-crafted C program without
> apologising profusely at Dijkstra's grave but if you're using C as an
> intermediate language then that's a different matter). If I recall correctly
> from an old post on this list, this is how Felix handles tail recursion when
> translating Felix to C++

I am not sure this can work when tail-calling an unknown function. How would you translate to C

let rec f g x =
   if x < 0 then
      g x (*tail recursive call to an unknown function*)
      f g (x - 1)

ocamlopt -S gives (I am keeping only the crucial code)

	movq	%rax, %rsi
	cmpq	$1, %rbx
	jge	.L100
	movq	(%rsi), %rdi
	movq	%rbx, %rax
	movq	%rsi, %rbx
	jmp	*%rdi                      ;;;; tail rec call to g
	.align	4
	addq	$-2, %rbx
	movq	%rsi, %rax
	jmp	.L101

As you can see, the tail recursive call is translated to an undirect jmp.

Please explain how you would translate the above example to portable & efficient C.


Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***