Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

imcc's "call" vs first class functions

5 views
Skip to first unread message

Michal Wallace

unread,
Aug 1, 2003, 1:49:07 PM8/1/03
to perl6-i...@perl.org

Hey all,

I've got lambda (single-expression anonymous subroutine)
working in pirate now, but I wasn't sure how to get it
to do the correct calling convention with IMCC's "call".

For example, pirate turns this:

print (lambda x: x+1)(0) # prints "1\n"

into this: (the commented line is the important one)

.sub __main__
setline 1
$I00002 = addr _sub00001
$I00001 = $I00002
.local object arg00001
arg00001 = new PerlInt
arg00001 = 0
.arg arg00001

jsr $I00001 #### :( why can't I "call"?#######

.result $P00001
.arg $P00001
call __py__print
print "\n"
end
.end
.sub _sub00001
# lambda from line 1
saveall
.param object x
.local object res00001
$P00001 = new PerlInt
$P00001 = x
$P00002 = new PerlInt
$P00002 = new PerlInt # yes, this is a bug :)
$P00002 = 1
$P00003 = new PerlInt
$P00003 = $P00001 + $P00002
res00001 = $P00003
.return res00001
restoreall
ret
.end


This code works, but it doesn't follow the convention.
I was hoping to use call for this, but I couldn't get it
to work, because I'm not directly calling _sub00001.

Actually, in this case I probably could do that, since
it's anonymous, but python has first-class functions,
which means you can do this:

a = b = c = lambda x: x+1
assert a(1) == b(1) == c(1)

And then you can call a() or b() or whatever. Basically,
python needs to look up the function's address at runtime
and I couldn't figure out how to make that happen with
call... So I just did jsr for now, hoping someone could
tell me how to fix it.

Is that even possible with "call"? If not, we can just
hard code the instructions, but it seems like it really
ought to be in imcc. :)


Sincerely,

Michal J Wallace
Sabren Enterprises, Inc.
-------------------------------------
contact: mic...@sabren.com
hosting: http://www.cornerhost.com/
my site: http://www.withoutane.com/
--------------------------------------

K Stol

unread,
Aug 2, 2003, 12:56:24 AM8/2/03
to Michal Wallace, perl6-i...@perl.org
If I understood well, the problem is that subroutines can be saved in
variables, right (or registers for that matter)?
So, if there is some subroutine "f", you could just do:

$I1 = addr _f # get address of subroutine f
$P1 = new Sub # create a new Sub PMC
$P1 = $I1 # store address of f into this Sub
global "f" = $P1 # now the variable "f" represents this sub.

When calling f, one could do:

$P0 = global "f" # get current 'value' of "f"
# do calling conv. stuff
invoke # or invokecc? I don't know which invoke op exactly

Correct me if I'm wrong, but IMCC's "call" is translated to a "bsr" op, so
it's just a jump.
I don't know if IMCC already has support for invoke and the like...(I'm not
the expert ;-)

Well, I used this solution, don't know if it's any good...(worked for me
though)

Klaas-Jan

Leopold Toetsch

unread,
Aug 1, 2003, 3:59:08 PM8/1/03
to Michal Wallace, perl6-i...@perl.org
Michal Wallace <mic...@sabren.com> wrote:

> Hey all,

> I've got lambda (single-expression anonymous subroutine)
> working in pirate now, but I wasn't sure how to get it
> to do the correct calling convention with IMCC's "call".

I'm pretty sure, that you should use Parrot calling conventions for all
subs, the more that subs are objects inside Pie-thon.

> print (lambda x: x+1)(0) # prints "1\n"

> jsr $I00001 #### :( why can't I "call"?#######

The register based j* branches take an absolute address. Imcc has no way
to find out where the branch will go in the general case. Using these
opcodes is almost unsopported inside imcc.

With Parrot calling conventions and an appropriate subroutine object you
can achieve the same effect, with additional context saving as needed.

While this jump opcode may be needed somewhen (what for? - or even get
tossed), all effort currently goes into PCC and optimizing that stuff
finally.

Further information:
docs/pdds/pdd03_calling_conventions.pod
docs/pmc/sub.pod
languages/imcc/docs/calling_conventions.pod

> Sincerely,
>
> Michal J Wallace

leo

0 new messages