Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

[RFC][DRAFT][VM] Function pointer support in VM #989

Draft
wants to merge 11 commits into
base: aptos-main
Choose a base branch
from

Conversation

runtian-zhou
Copy link
Member

Motivation

This is the very first initial draft of how MoveVM can potentially support function pointer. We believe function pointer could be a very important corner stone of how interface should be implemented in the future.

In this implemetnation, we add three new opcodes and one more SignatureToken type:

  • GetFunctionPointer: Convert a FunctionHandle into a function pointer value on top of the stack
  • GetFunctionPointerGeneric: Convert a FunctionInstantiation into a function pointer value on top of the stack
  • CallFunctionPointer: Instruction to invoke the function pointer

Right now it has the following limitations:

  • No currying is allowed.
  • Only function with no acquire list can be obtained as function pointer to avoid reentrency issue and major refactor in our borrow semantics.

Have you read the Contributing Guidelines on pull requests?

Yes.

Test Plan

This implementation only passed compilation and is no way close to merging into main. The intention of this PR is just to assess the safety of this change and to collect usability feedbacks. I'll try to see how I can mod the IR to try the new feature out in the compiler

let a: u64;
label b0:
func = get_function_pointer(M.sum);
a = call_function_pointer<|u64, u64| (u64)>(0, 1, move(func));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refer to this example for how to use function pointer!

coin2 = BasicCoin1.mint<CoinType.Foo>(20);
interface = BasicCoin1.coin_interface<CoinType.Foo>();

v = GenericAdder.add_coins<BasicCoin1.Coin<CoinType.Foo>>(&interface, &coin1, &coin2);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example for how to implement interface with function pointer provided.

@@ -50,6 +50,7 @@ pub enum Type {
U16,
U32,
U256,
Function,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... no type parameters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I intentionally hided them because the runtime already carried the type information so there's no need to duplicate it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this may not be the right thing to do. Type is being used by native functions and should represent the full name of a type.

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

Successfully merging this pull request may close these issues.

2 participants