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 ----- Original Message ----- From: "Michal Wallace" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Friday, August 01, 2003 10:49 AM Subject: imcc's "call" vs first class functions > > 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: [EMAIL PROTECTED] > hosting: http://www.cornerhost.com/ > my site: http://www.withoutane.com/ > -------------------------------------- > >