develooper Front page | perl.perl6.internals | Postings from October 2001

Re: Simple sub support's now in!

Thread Previous | Thread Next
From:
Brian Wheeler
Date:
October 12, 2001 13:39
Subject:
Re: Simple sub support's now in!
Message ID:
1002919827.9768.29.camel@wombat.educ.indiana.edu
Neat-o, but I do have a question... how do I pass parameters to
recursive subroutines, and/or save registers and not clobber the
caller's?

Here's a factorial program I wrote:
### Compute the factorial recursively
### based on the scheme classic:	
### (define fact (lambda(n) (if (= 1 n) 1 (* n (fact (- n 1))))))
	
	set	I0,	3
	save	I0
	bsr	fact
	restore	I1
	print	"Factorial of "
	print	I0
	print   " is "
	print	I1
	print	"\n"
	end


fact:	
	push_i
	restore	I0
	print   "called fact! parameter="
	print   I0
	print   ", depth: "
	print   I31
	inc     I31,1
	eq	I0,0,$is_one
	eq	I0,1,$is_one
	set	I1,I0
	dec	I1,1
	save	I1
	bsr	fact
	restore	I1
	mul	I0,I0,I1
	save	I0
	branch	$done
	
	
$is_one:
	set	I0,1
	save	I0
$done:	
	pop_i
	ret



But alas, it cannot work:  since there's only a single stack, and
there's no way to peek down into it (that I could find), there's no way
to pass params via the stack...or not clobber another's registers.  It
might be interesting to have 3 stacks:
	* branches
	* interpreter contexts
	* data

Though that might become a little tedious...hehe, but it would allow my
above example to work :)

Brian






On Fri, 2001-10-12 at 15:00, Dan Sugalski wrote:
> Okay, I finished the support for the generic stack. We can now:
> 
>    *) save and restore registers (using the save and restore opcodes)
> 
>    *) return from subs using the address on the top of the stack (using
>       the ret opcode)
> 
>    *) Branch to a label using the bsr opcode, and remember where we were
>       for later returning
> 
> At the moment we've only got relative sub calls using the bsr (well, bsr_i 
> and bsr_ic opcodes, but close enough...) opcode. jsr will come as soon as 
> we can reasonably get absolute addresses. Soon, I think.
> 
> 					Dan
> 
> --------------------------------------"it's like this"-------------------
> Dan Sugalski                          even samurai
> dan@sidhe.org                         have teddy bears and even
>                                       teddy bears get drunk



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About