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

Some leaveover of #1326 #1327

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jit-compiler/src/main/java/org/aya/compiler/MethodRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package org.aya.compiler;

import kala.collection.immutable.ImmutableSeq;
import org.aya.compiler.morphism.JavaExpr;
import org.jetbrains.annotations.NotNull;

import java.lang.constant.ClassDesc;
Expand All @@ -18,4 +19,7 @@ public record MethodRef(
public boolean isConstructor() {
return name().equals(ConstantDescs.INIT_NAME);
}
public boolean checkArguments(@NotNull ImmutableSeq<JavaExpr> args) {
return paramTypes.sizeEquals(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public record AstCodeBuilder(
@Override
public void invokeSuperCon(@NotNull ImmutableSeq<ClassDesc> superConParams, @NotNull ImmutableSeq<JavaExpr> superConArgs) {
assert isConstructor;
assert superConParams.sizeEquals(superConArgs);
stmts.append(new AstStmt.Super(superConParams, assertFreeExpr(superConArgs)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ public enum AstExprBuilder implements ExprBuilder {
INSTANCE;

@Override public @NotNull JavaExpr mkNew(@NotNull MethodRef conRef, @NotNull ImmutableSeq<JavaExpr> args) {
assert conRef.checkArguments(args);
return new AstExpr.New(conRef, assertFreeExpr(args));
}

@Override
public @NotNull JavaExpr invoke(@NotNull MethodRef method, @NotNull JavaExpr owner, @NotNull ImmutableSeq<JavaExpr> args) {
assert method.checkArguments(args);
return new AstExpr.Invoke(method, assertFreeExpr(owner), assertFreeExpr(args));
}

@Override public @NotNull JavaExpr invoke(@NotNull MethodRef method, @NotNull ImmutableSeq<JavaExpr> args) {
assert method.checkArguments(args);
return new AstExpr.Invoke(method, null, assertFreeExpr(args));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.compiler.serializers;

import kala.collection.SeqView;
import kala.collection.immutable.ImmutableSeq;
import org.aya.compiler.MethodRef;
import org.aya.compiler.morphism.*;
Expand Down Expand Up @@ -151,4 +152,14 @@ protected AbstractExprializer(@NotNull ExprBuilder builder, @NotNull SerializerC
* Prepare and perform {@link #doSerialize}
*/
public abstract @NotNull JavaExpr serialize(T unit);

public static @NotNull JavaExpr makeCallInvoke(
@NotNull ExprBuilder builder,
@NotNull MethodRef ref,
@NotNull JavaExpr instance,
@NotNull JavaExpr normalizer,
@NotNull SeqView<JavaExpr> args
) {
return builder.invoke(ref, instance, InvokeSignatureHelper.args(normalizer, args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.compiler.serializers;

import kala.collection.Seq;
import kala.collection.immutable.ImmutableSeq;
import kala.control.Either;
import org.aya.compiler.LocalVariable;
import org.aya.compiler.MethodRef;
import org.aya.compiler.morphism.ClassBuilder;
import org.aya.compiler.morphism.CodeBuilder;
import org.aya.compiler.morphism.Constants;
import org.aya.compiler.morphism.JavaExpr;
import org.aya.compiler.morphism.*;
import org.aya.generic.Modifier;
import org.aya.primitive.ShapeFactory;
import org.aya.syntax.compile.JitFn;
Expand All @@ -32,12 +28,6 @@ public FnSerializer(@NotNull ShapeFactory shapeFactory, ModuleSerializer.@NotNul
this.shapeFactory = shapeFactory;
}

/// @see JitFn#invoke(java.util.function.UnaryOperator, Seq)
public static @NotNull MethodRef resolveInvoke(@NotNull ClassDesc owner, int argc) {
return new MethodRef(
owner, "invoke", Constants.CD_Term, InvokeSignatureHelper.parameters(ImmutableSeq.fill(argc, Constants.CD_Term).view()), false);
}

public static int modifierFlags(@NotNull EnumSet<Modifier> modies) {
var flag = 0;
for (var mody : modies) flag |= 1 << mody.ordinal();
Expand All @@ -55,6 +45,22 @@ public static int modifierFlags(@NotNull EnumSet<Modifier> modies) {
.appended(builder.iconst(modifierFlags(unit.modifiers())));
}

public static @NotNull JavaExpr makeInvoke(
@NotNull ExprBuilder builder,
@NotNull ClassDesc owner,
@NotNull JavaExpr normalizer,
@NotNull ImmutableSeq<JavaExpr> args
) {
var ref = new MethodRef(
owner, "invoke", Constants.CD_Term,
InvokeSignatureHelper.parameters(ImmutableSeq.fill(args.size(), Constants.CD_Term).view()),
false
);

var instance = TermExprializer.getInstance(builder, owner);
return AbstractExprializer.makeCallInvoke(builder, ref, instance, normalizer, args.view());
}

/**
* Build fixed argument `invoke`
*/
Expand Down Expand Up @@ -110,8 +116,7 @@ private void buildInvoke(
) {
var teleSize = unit.telescope().size();
var args = AbstractExprializer.fromSeq(builder, Constants.CD_Term, argsTerm.ref(), teleSize);
var fullArgs = InvokeSignatureHelper.args(normalizerTerm.ref(), args.view());
var result = builder.invoke(invokeMethod, builder.thisRef(), fullArgs);
var result = AbstractExprializer.makeCallInvoke(builder, invokeMethod, builder.thisRef(), normalizerTerm.ref(), args.view());
builder.returnWith(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
package org.aya.compiler.serializers;

import kala.collection.Seq;
import kala.collection.SeqView;
import kala.collection.immutable.ImmutableSeq;
import org.aya.compiler.LocalVariable;
import org.aya.compiler.MethodRef;
import org.aya.compiler.morphism.ClassBuilder;
import org.aya.compiler.morphism.CodeBuilder;
import org.aya.compiler.morphism.Constants;
import org.aya.compiler.morphism.*;
import org.aya.syntax.compile.AyaMetadata;
import org.aya.syntax.compile.JitMatchy;
import org.aya.syntax.core.def.Matchy;
Expand Down Expand Up @@ -41,16 +38,26 @@ public MatchySerializer(ModuleSerializer.@NotNull MatchyRecorder recorder) {
return NameSerializer.javifyClassName(unit.matchy.qualifiedName().module(), unit.matchy.qualifiedName().name());
}

private static @NotNull InvokeSignatureHelper makeHelper(int capturec, int argc) {
return new InvokeSignatureHelper(ImmutableSeq.fill(capturec + argc, Constants.CD_Term));
private static @NotNull ImmutableSeq<ClassDesc> makeInvokeParameters(int capturec, int argc) {
return InvokeSignatureHelper.parameters(ImmutableSeq.fill(capturec + argc, Constants.CD_Term).view());
}

public static @NotNull MethodRef resolveInvoke(@NotNull ClassDesc owner, int capturec, int argc) {
return new MethodRef(
public static @NotNull JavaExpr makeInvoke(
@NotNull ExprBuilder builder,
@NotNull ClassDesc owner,
@NotNull JavaExpr normalizer,
@NotNull ImmutableSeq<JavaExpr> captures,
@NotNull ImmutableSeq<JavaExpr> args
) {
var instance = TermExprializer.getInstance(builder, owner);
var ref = new MethodRef(
owner, "invoke",
Constants.CD_Term, makeHelper(capturec, argc).parameters(),
Constants.CD_Term,
makeInvokeParameters(captures.size(), args.size()),
false
);

return AbstractExprializer.makeCallInvoke(builder, ref, instance, normalizer, captures.view().appendedAll(args));
}

private void buildInvoke(
Expand Down Expand Up @@ -100,13 +107,14 @@ private void buildInvoke(
* @see JitMatchy#invoke(java.util.function.UnaryOperator, Seq, Seq)
*/
private void buildInvoke(
@NotNull CodeBuilder builder, @NotNull MatchyData data,
@NotNull CodeBuilder builder,
@NotNull MatchyData data,
@NotNull MethodRef invokeRef,
@NotNull LocalVariable normalizer,
@NotNull LocalVariable captures, @NotNull LocalVariable args
) {
var capturec = data.capturesSize;
int argc = data.argsSize;
var invokeRef = resolveInvoke(NameSerializer.getClassDesc(data.matchy), capturec, argc);
var preArgs = AbstractExprializer.fromSeq(builder, Constants.CD_Term, captures.ref(), capturec)
.view()
.appendedAll(AbstractExprializer.fromSeq(builder, Constants.CD_Term, args.ref(), argc));
Expand Down Expand Up @@ -138,9 +146,8 @@ private void buildType(@NotNull CodeBuilder builder, @NotNull MatchyData data, @
var capturec = unit.capturesSize;
var argc = unit.argsSize;

var helper = makeHelper(capturec, argc);

builder.buildMethod(Constants.CD_Term, "invoke", helper.parameters(), (ap, cb) -> {
var fixedInvokeRef = builder.buildMethod(Constants.CD_Term, "invoke",
makeInvokeParameters(capturec, argc), (ap, cb) -> {
var pre = InvokeSignatureHelper.normalizer(ap);
var captures = ImmutableSeq.fill(capturec, i -> InvokeSignatureHelper.arg(ap, i));
var args = ImmutableSeq.fill(argc, i -> InvokeSignatureHelper.arg(ap, i + capturec));
Expand All @@ -153,7 +160,7 @@ private void buildType(@NotNull CodeBuilder builder, @NotNull MatchyData data, @
var pre = ap.arg(0);
var captures = ap.arg(1);
var args = ap.arg(2);
buildInvoke(cb, unit, pre, captures, args);
buildInvoke(cb, unit, fixedInvokeRef, pre, captures, args);
});

builder.buildMethod(Constants.CD_Term, "type", ImmutableSeq.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,19 @@ private TermExprializer(
));
}

private @NotNull JavaExpr
buildFnInvoke(@NotNull ClassDesc defClass, int ulift, @NotNull ImmutableSeq<JavaExpr> args) {
private @NotNull JavaExpr getNormalizer() {
var normalizer = context.normalizer();
if (normalizer == null) {
normalizer = Constants.unaryOperatorIdentity(builder);
}

var invokeExpr = builder.invoke(
FnSerializer.resolveInvoke(defClass, args.size()),
getInstance(builder, defClass),
InvokeSignatureHelper.args(normalizer, args.view()));
return normalizer;
}

private @NotNull JavaExpr
buildFnInvoke(@NotNull ClassDesc defClass, int ulift, @NotNull ImmutableSeq<JavaExpr> args) {
var normalizer = getNormalizer();
var invokeExpr = FnSerializer.makeInvoke(builder, defClass, normalizer, args);

if (ulift != 0) {
return builder.invoke(Constants.ELEVATE, invokeExpr, ImmutableSeq.of(builder.iconst(ulift)));
Expand All @@ -128,19 +130,8 @@ private TermExprializer(
@NotNull ImmutableSeq<JavaExpr> args,
@NotNull ImmutableSeq<JavaExpr> captures
) {
// TODO: unify this with [buildFnInvoke]
var normalizer = context.normalizer();
if (normalizer == null) {
normalizer = Constants.unaryOperatorIdentity(builder);
}

var fullArgs = InvokeSignatureHelper.args(normalizer, captures.view().appendedAll(args));

return builder.invoke(
MatchySerializer.resolveInvoke(matchyClass, captures.size(), args.size()),
getInstance(builder, matchyClass),
fullArgs
);
var normalizer = getNormalizer();
return MatchySerializer.makeInvoke(builder, matchyClass, normalizer, captures, args);
}

@Override protected @NotNull JavaExpr doSerialize(@NotNull Term term) {
Expand Down
Loading