Skip to content

Add extra parameter for initialized carrier #25

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

Open
wants to merge 1 commit into
base: matchers-experiment
Choose a base branch
from

Conversation

biboudis
Copy link
Owner

Existing Approach

Currently the pattern-declaration and pattern-use site look like the following:

  • Pattern-declaration site

Currently, on the pattern-declaration site the carrier creation is taking place inside the method site:

pattern Point(int x, int y) {
    match Point(1, 2);
}
// translates to
static Object \^dinit\_:Point:I:I(ClassPatternDeclarations$Point) {
    int x = 1;
    int y = 2;
    Object carrier = <ldc> [static returnType =  MethodType.methodType(Object.class, int.class, int.class)] Carriers.initializingConstructor(returnType);
    return carrier.invoke(x, y);
}
  • Pattern-use site

Let's observe how a pattern site is translated:

o instanceof Point(int x, int y)
// translates to
Point p = (Point) o;
Carrier c = <invokedynamic> <ConstantCallSite(Point.\^dinit\_:Point:I:I(p))>; // ConstantCallSite calculated from PatternBootstrap
...
MethodHandle component$0 = <ldc> [static arg = MethodType.methodType(Object.class, int.class, int.class)] Carriers.component(arg, 0);
MethodHandle component$1 = <ldc> [static arg = MethodType.methodType(Object.class, int.class, int.class)] Carriers.component(arg, 1);
...
int $0 = component$0.invoke(c);
// safeguard the cast for x -- identity here
int $1 = component$1.invoke(c);
// safeguard the cast for y -- also identity here
...

Adjustment

This PR adds one extra parameter on all translated methods in the end (the deconstructor/static method, the instance pattern/instance method, and the static pattern/static method) to decouple the creation of the carrier from the body of the pattern itself.

  • New Pattern-declaration site

We introduce an additional MethodHandle:

static Object \^dinit\_:Point:I:I(ClassPatternDeclarations$Point, MethodHandle carrier) {
    int x = 1;
    int y = 2;
    return carrier.invoke(x, y);
}
  • New Pattern-use site

The extractor should be calculated and shared per client use-site:

o instanceof Point(int x, int y)
// translates to
Point p = (Point) o;
...
MethodHandle carrierExtractor = <ldc>[static arg = MethodType.methodType(Object.class, int.class, int.class)] Carriers.initializingConstructor(arg);
Carrier c = <invokedynamic> <ConstantCallSite(Point.\^dinit\_:Point:I:I(p, carrierExtractor))>; // ConstantCallSite calculated from PatternBootstrap
...
MethodHandle component$0 = <ldc> [static arg = MethodType.methodType(Object.class, int.class, int.class)] Carriers.component(arg, 0);
MethodHandle component$1 = <ldc> [static arg = MethodType.methodType(Object.class, int.class, int.class)] Carriers.component(arg, 1);
...
int $0 = component$0.invoke(c);
// safeguard the cast for x -- identity here
int $1 = component$1.invoke(c);
// safeguard the cast for y -- also identity here
...

@biboudis biboudis marked this pull request as ready for review April 11, 2025 14:27
@biboudis biboudis force-pushed the matchers-experiment-decouple-carrier-init branch from f733da6 to 78251fd Compare April 11, 2025 14:43
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

Successfully merging this pull request may close these issues.

1 participant