From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 28C41BC0A for ; Thu, 26 Apr 2007 11:17:11 +0200 (CEST) Received: from ipmail01.adl2.internode.on.net (ipmail01.adl2.internode.on.net [203.16.214.140]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l3Q9H8F7009913 for ; Thu, 26 Apr 2007 11:17:10 +0200 X-IronPort-AV: E=Sophos;i="4.14,453,1170595800"; d="scan'208";a="119727380" Received: from ppp8-148.lns1.syd7.internode.on.net (HELO [192.168.1.201]) ([59.167.8.148]) by ipmail01.adl2.internode.on.net with ESMTP; 26 Apr 2007 18:47:06 +0930 Subject: Re: [Caml-list] Function application implementation From: skaller To: Tom Cc: Caml List In-Reply-To: References: <1177557207.8088.27.camel@rosella.wigram> Content-Type: text/plain Date: Thu, 26 Apr 2007 19:17:02 +1000 Message-Id: <1177579022.8651.23.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.10.1 Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 46306E14.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; 0200,:01 ocaml:01 stack:01 compiler:01 compiler:01 inlining:01 ocaml:01 model:01 compilers:01 sourceforge:01 sourceforge:01 wrote:01 wrote:01 caml-list:01 inline:01 On Thu, 2007-04-26 at 10:52 +0200, Tom wrote: > > > On 26/04/07, skaller wrote: > It knows the type of the function expression, and that is all > that is required. Incidentally Ocaml evaluates right to left. > So > > f x y z > > will be roughly: > > push (eval z) > push (eval y) > push (eval x) > push (eval f) > apply > apply > apply > > But that doesn't explain how does each apply know what to do, either > to build a new closure (in the case above, the first two applies) or > to actually call the code (the third apply). push (eval f) calculates the expression f, which results in a closure. Apply, with the stack: closure f <-- top value 1 ... calculates apply(closure f, value 1) That is how functions are called. In practice, a compiler may do optimisations. In the Felix compiler for example, in the expression: apply(f,e) if the subexpresion f is a simple function constant, then the compiler can inline the function. Otherwise, a closure has to be formed. In Felix this means instantiating a C++ class (the function f) to make a closure (an object of the class). In Felix the actual C++ used is: (new f(environment)) -> apply (e) In other words, all compilers will look for optimisations such as are made possible when a direct call is detected, inlining in such cases being one possible optimisation which could be applied. the actual sequence I have above may not be how the Ocaml compiler organises it: the point is that the model is built to not need to make the distinction you're asking about: that's just an optimisation. -- John Skaller Felix, successor to C++: http://felix.sf.net