Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facilitate Creation of Function Pointers / Lambdas? #146

Open
SebastienGllmt opened this issue Aug 29, 2018 · 3 comments
Open

Facilitate Creation of Function Pointers / Lambdas? #146

SebastienGllmt opened this issue Aug 29, 2018 · 3 comments

Comments

@SebastienGllmt
Copy link

SebastienGllmt commented Aug 29, 2018

The following is the smallest example of a lambda I could come up with (calling foo() and calling baz() will have the same result)

contract "lambdaTest" {

// this will be our lambda
define public @foo() {
entry:
  ret 1234
}

// In a real world setting, this function would return a different lambda based on some condition
define public @bar(){
entry:
  %this = call @iele.address()
  %f = calladdress @foo at %this
  ret %f
}

// fetch which lambda to use from bar and then call it
define public @baz() {
entry:
  %f = call @bar()
  
  %this = call @iele.address()
  %gas = call @iele.gas()
  %status, %result = call %f at %this () send 0, gaslimit %gas
  ret %result
}

define @init() {
entry:
  ret void
}

}

Unfortunately this is not very pretty because

  1. Calladdress requires an "at" instead of implicitly using @iele.address()
  2. You cannot use call on a register. You must use call at

I don't know why these restrictions exist.

Anyways this is a cool feature and it's much prettier than what you have to do in Solidity which involves mangling byte arrays 👍

@SebastienGllmt SebastienGllmt changed the title Facilitate Creation of Lambdas? Facilitate Creation of Function Pointers / Lambdas? Aug 30, 2018
@dwightguth
Copy link

It's definitely supposed to be possible to do:

call %ptr()

I don't have time to investigate this immediately, but I will look into it sometime in the next couple days. In the meantime, if you want to help, can you tell me what went wrong when you tried to do a local call on a function pointer?

@SebastienGllmt
Copy link
Author

I just tried it out and you are right. I must have made a typo or something.

I guess the only consideration now is do we want to make calladdress apply to @iele.address() by default.

@dwightguth
Copy link

dwightguth commented Aug 30, 2018

Probably the simplest solution is to make %foo = calladdress @bar assemble into

%this = call @iele.address()
%foo = calladdress @bar at %this

This would not require a change to the VM and is consistent with the other types of syntactic sugar the assembler offers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants