Re: Lexical variables, scratchpads, closures, ...

2002-08-04 Thread Jerome Vouillon
On Sat, Aug 03, 2002 at 01:42:06AM -0400, Melvin Smith wrote: > Here is an attempt. I'm assuming the stack of pads is a singly-linked list > of pads with children pointing to parents, and multiple children can refer > to the same parent. If they are created on the fly, the creation of > a Sub or C

Re: Lexical variables, scratchpads, closures, ...

2002-08-04 Thread Jerome Vouillon
On Fri, Aug 02, 2002 at 09:06:31PM +0100, Nicholas Clark wrote: > On Fri, Aug 02, 2002 at 06:43:49PM +0200, Jerome Vouillon wrote: > > Allocating a hash will certainly remain a lot more expensive than > > allocating an array. And we are going to allocate pads pretty > > often... > > Are we? Or a

Re: Lexical variables, scratchpads, closures, ...

2002-08-03 Thread Simon Cozens
[EMAIL PROTECTED] (Dave Mitchell) writes: > > Or would that make access by name too slow? > It's how Perl5 does it (very roughly speaking) This is a reminder that people new to developing VMs may find it useful to leaf through http://www.netthink.co.uk/downloads/internals.pdf and http://www.netth

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Melvin Smith
At 06:14 PM 8/1/2002 +0200, Jerome Vouillon wrote: >On Wed, Jul 31, 2002 at 11:40:39AM -0600, Jonathan Sillito wrote: > > So here is my take on a slightly simpler example: > > > > sub foo { > > my $x = 13; > > return sub { print "$x\n"; }; > > } > > > > $foo() > >Melvin, I think it w

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Nicholas Clark
On Fri, Aug 02, 2002 at 06:43:49PM +0200, Jerome Vouillon wrote: > On Fri, Aug 02, 2002 at 08:50:27AM -0700, Sean O'Rourke wrote: > > I don't see how you can cope with '%MY' unless you have a hash. You could > > have a hash in addition to the array, I suppose. > > Sure, you need a hash. But th

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Dave Mitchell
On Fri, Aug 02, 2002 at 11:15:09AM -0600, Jonathan Sillito wrote: > Could two parallel arrays work? One stores the lexicals (accessed by > index) and the other stores the names of the lexicals. Then to access a > lexical by name involves a sequential search through the (probably not > large) array

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Jonathan Sillito
On Fri, 2002-08-02 at 10:43, Jerome Vouillon wrote: > Sure, you need a hash. But this can be a statically allocated hash, > mapping variable names to indices. Could two parallel arrays work? One stores the lexicals (accessed by index) and the other stores the names of the lexicals. Then to acces

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Melvin Smith
At 06:14 PM 8/1/2002 +0200, Jerome Vouillon wrote: >Melvin, I think it would really help if you could explain us how you >would compile this code. Also, you should describe precisely what >"invoke" and "new_pad" (and maybe the other scratchpad-related >opcodes) do as far as scratchpads are concer

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Sean O'Rourke
On Fri, 2 Aug 2002, Melvin Smith wrote: > At 08:50 AM 8/2/2002 -0700, Sean O'Rourke wrote: > >Without performance numbers, this is hard to test, but it can potentially > >turn a single "a = b + c", which is just "add P0, P1, P2" if a, b, and c > >have been referenced, into a hideous five instructi

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Melvin Smith
At 08:50 AM 8/2/2002 -0700, Sean O'Rourke wrote: >Without performance numbers, this is hard to test, but it can potentially >turn a single "a = b + c", which is just "add P0, P1, P2" if a, b, and c >have been referenced, into a hideous five instructions: > > fetch_lex P0, 'a' # Because how w

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Jerome Vouillon
On Fri, Aug 02, 2002 at 08:50:27AM -0700, Sean O'Rourke wrote: > On Fri, 2 Aug 2002, Jerome Vouillon wrote: > > > On Thu, Aug 01, 2002 at 10:57:34AM -0700, Sean O'Rourke wrote: > > > My naive implementation would have an array of hashes for each sub, with > > > one entry for each level of scope w

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Sean O'Rourke
On Fri, 2 Aug 2002, Jerome Vouillon wrote: > On Thu, Aug 01, 2002 at 10:57:34AM -0700, Sean O'Rourke wrote: > > My naive implementation would have an array of hashes for each sub, with > > one entry for each level of scope within. > > I would use an array of arrays or a linked-list of arrays. Th

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Nicholas Clark
On Fri, Aug 02, 2002 at 05:20:45PM +0200, Jerome Vouillon wrote: > On Thu, Aug 01, 2002 at 10:57:34AM -0700, Sean O'Rourke wrote: > > My naive implementation would have an array of hashes for each sub, with > > one entry for each level of scope within. > > I would use an array of arrays or a link

Re: Lexical variables, scratchpads, closures, ...

2002-08-02 Thread Jerome Vouillon
On Thu, Aug 01, 2002 at 10:57:34AM -0700, Sean O'Rourke wrote: > My naive implementation would have an array of hashes for each sub, with > one entry for each level of scope within. I would use an array of arrays or a linked-list of arrays. This is hardly more difficult to implement (you just ne

Re: Lexical variables, scratchpads, closures, ...

2002-08-01 Thread Dan Sugalski
At 11:22 PM -0400 7/31/02, Melvin Smith wrote: >>Conclusion >> >> It seems to me that to implement lexical variables, we only need to >> implement the set_pmc method and to extend the Sub class so that it >> contains both a code pointer and a scratchpad. > >I agree with you. It can be done w

Re: Lexical variables, scratchpads, closures, ...

2002-08-01 Thread Sean O'Rourke
On Thu, 1 Aug 2002, Melvin Smith wrote: > Jerome Vouillon writes: > >On Wed, Jul 31, 2002 at 11:22:56PM -0400, Melvin Smith wrote: > >> And they need to be COW, as closures have access to their > >> own copies of lexicals. I asked Jonathan to reuse the stack code > >> I had already written becau

Re: Lexical variables, scratchpads, closures, ...

2002-08-01 Thread Sean O'Rourke
On Thu, 1 Aug 2002, Jerome Vouillon wrote: > On Wed, Jul 31, 2002 at 11:22:56PM -0400, Melvin Smith wrote: > > We chose to implement > > the access as ops, and you prefer using a PMC Array directly. I can > > at least see one advantage to the explicit ops: they don't require > > a register to use

Re: Lexical variables, scratchpads, closures, ...

2002-08-01 Thread Sean O'Rourke
On 31 Jul 2002, Jonathan Sillito wrote: > > > invoke# assumes sub is in P0 > > > # on invoke the sub pmc fixes the current > > > # context to have the correct lexicals > > > > Can you elaborate on this? What is done precisely to fix the current > > context? > >

Re: Lexical variables, scratchpads, closures, ...

2002-08-01 Thread Melvin Smith
Jerome Vouillon writes: >On Wed, Jul 31, 2002 at 11:22:56PM -0400, Melvin Smith wrote: >> At 06:25 PM 7/31/2002 +0200, Jerome Vouillon wrote: >> >Closures >> > >> > A subroutine must have access to the scratchpads of all the >> > englobing blocks. As the scratchpads are linked, it is sufficient

Re: Lexical variables, scratchpads, closures, ...

2002-08-01 Thread Jerome Vouillon
On Wed, Jul 31, 2002 at 11:40:39AM -0600, Jonathan Sillito wrote: > So here is my take on a slightly simpler example: > > sub foo { > my $x = 13; > return sub { print "$x\n"; }; > } > > $foo() Melvin, I think it would really help if you could explain us how you would compile this

Re: Lexical variables, scratchpads, closures, ...

2002-08-01 Thread Jerome Vouillon
On Wed, Jul 31, 2002 at 11:22:56PM -0400, Melvin Smith wrote: > At 06:25 PM 7/31/2002 +0200, Jerome Vouillon wrote: > >Closures > > > > A subroutine must have access to the scratchpads of all the > > englobing blocks. As the scratchpads are linked, it is sufficient > > to add a pointer to the

Re: Lexical variables, scratchpads, closures, ...

2002-07-31 Thread Melvin Smith
At 06:25 PM 7/31/2002 +0200, Jerome Vouillon wrote: >Scratchpads > > We need to allocate an area in the heap for each lexical variable. > Instead of allocating this area one variable at a time, we can > allocate a single "scratchpad" value for all variables of a block: > this is more effic

Re: Lexical variables, scratchpads, closures, ...

2002-07-31 Thread Jonathan Sillito
On Wed, 2002-07-31 at 13:49, Jerome Vouillon wrote: > On Wed, Jul 31, 2002 at 11:40:39AM -0600, Jonathan Sillito wrote: > > new_pad # push this on the lexical stack > > # some constant descriptor should also be passed > > # to the new_pad op which would then know

Re: Lexical variables, scratchpads, closures, ...

2002-07-31 Thread Jerome Vouillon
On Wed, Jul 31, 2002 at 11:40:39AM -0600, Jonathan Sillito wrote: > new_pad # push this on the lexical stack > # some constant descriptor should also be passed > # to the new_pad op which would then know about > # the lexical variable 'x', and would

Re: Lexical variables, scratchpads, closures, ...

2002-07-31 Thread Jonathan Sillito
On Wed, 2002-07-31 at 10:25, Jerome Vouillon wrote: > > Let us think a bit about the implementation of lexical variables. Thanks for spelling this out in such detail. Here is a variation based on the lexical ops (new_pad, pop_pad, store_lex, find_lex) committed yesterday. Note that lexical pad

Lexical variables, scratchpads, closures, ...

2002-07-31 Thread Jerome Vouillon
Let us think a bit about the implementation of lexical variables. Assignement First, let us consider how to compile a variable assignement such as: $x = $y where both $x and $y are lexical variables. At first, one may think that this can be compiled simply into a register assignme