caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Basile STARYNKEVITCH <basile@starynkevitch.net>
To: David Allsopp <dra-news@metastack.com>
Cc: 'Eray Ozkural' <examachine@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] How to write a CUDA kernel in ocaml?
Date: Wed, 16 Dec 2009 07:26:12 +0100	[thread overview]
Message-ID: <4B287D84.201@starynkevitch.net> (raw)
In-Reply-To: <017101ca7ddc$fb936b20$f2ba4160$@allsopp@metastack.com>

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*)
   else
      f g (x - 1)
;;

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

camlEsstr__f_58:
.L101:
	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
.L100:
	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.

Regards.

-- 
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} ***


      parent reply	other threads:[~2009-12-16  6:26 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-15 15:37 Eray Ozkural
2009-12-15 16:07 ` [Caml-list] " Basile STARYNKEVITCH
2009-12-15 16:20   ` Eray Ozkural
2009-12-15 16:29     ` Basile STARYNKEVITCH
2009-12-15 17:46       ` Eray Ozkural
2009-12-15 23:18       ` David Allsopp
2009-12-16  0:39         ` Jon Harrop
2009-12-16 13:41           ` Mattias Engdegård
2009-12-16 13:47             ` Eray Ozkural
2009-12-17  0:34               ` Philippe Wang
2009-12-17  6:45                 ` Eray Ozkural
2009-12-17 10:59                   ` Philippe Wang
2010-01-12  6:15             ` Eray Ozkural
2009-12-16  6:26         ` Basile STARYNKEVITCH [this message]

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=4B287D84.201@starynkevitch.net \
    --to=basile@starynkevitch.net \
    --cc=caml-list@inria.fr \
    --cc=dra-news@metastack.com \
    --cc=examachine@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).