Skip to content

Commit

Permalink
jit: actually compile to better matchy calls
Browse files Browse the repository at this point in the history
Co-authored-by: ice1000 <ice1000kotlin@foxmail.com>
  • Loading branch information
HoshinoTented and ice1000 committed Dec 17, 2024
1 parent 814430b commit 94ec18b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
16 changes: 4 additions & 12 deletions jit-compiler/src/main/java/org/aya/compiler/free/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import kala.control.Result;
import org.aya.compiler.free.data.FieldRef;
import org.aya.compiler.free.data.MethodRef;
import org.aya.generic.stmt.Shaped;
import org.aya.syntax.compile.*;
import org.aya.syntax.compile.JitClass;
import org.aya.syntax.compile.JitCon;
import org.aya.syntax.compile.JitData;
import org.aya.syntax.compile.JitMember;
import org.aya.syntax.core.Closure;
import org.aya.syntax.core.pat.PatMatcher;
import org.aya.syntax.core.term.Term;
Expand Down Expand Up @@ -253,14 +255,4 @@ private Constants() { }
FreeUtil.fromClass(PatMatcher.class), "apply",
CD_Result, ImmutableSeq.of(CD_ImmutableSeq, CD_ImmutableSeq), false
);

/**
* @see org.aya.syntax.compile.JitMatchy#invoke(Seq, Seq)
*/
public static final @NotNull MethodRef MATCHY_INVOKE = new MethodRef.Default(
FreeUtil.fromClass(JitMatchy.class),
"invoke",
CD_Term, ImmutableSeq.of(CD_Seq, CD_Seq),
false
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.aya.syntax.core.term.call.MatchCall;
import org.jetbrains.annotations.NotNull;

import java.lang.constant.ClassDesc;
import java.util.function.Consumer;

public class MatchySerializer extends ClassTargetSerializer<MatchySerializer.MatchyData> {
Expand All @@ -40,10 +41,9 @@ public MatchySerializer(ModuleSerializer.@NotNull MatchyRecorder recorder) {
return NameSerializer.javifyClassName(unit.matchy.qualifiedName().module(), unit.matchy.qualifiedName().name());
}

public static @NotNull MethodRef resolveInvoke(@NotNull MatchyLike owner, int capturec, int argc) {
var ownerName = NameSerializer.getClassDesc(owner);
public static @NotNull MethodRef resolveInvoke(@NotNull ClassDesc owner, int capturec, int argc) {
return new MethodRef.Default(
ownerName, "invoke",
owner, "invoke",
Constants.CD_Term, ImmutableSeq.fill(capturec + argc, Constants.CD_Term),
false
);
Expand Down Expand Up @@ -92,10 +92,9 @@ private void buildInvoke(
@NotNull FreeCodeBuilder builder, @NotNull MatchyData data,
@NotNull LocalVariable captures, @NotNull LocalVariable args
) {
var unit = data.matchy;
var capturec = data.capturesSize;
int argc = data.argsSize;
var invokeRef = resolveInvoke(data.matchy, capturec, argc);
var invokeRef = resolveInvoke(NameSerializer.getClassDesc(data.matchy), capturec, argc);
var invokeExpr = builder.invoke(invokeRef, builder.thisRef(),
AbstractExprializer.fromSeq(builder, Constants.CD_Term, captures.ref(), capturec)
.appendedAll(AbstractExprializer.fromSeq(builder, Constants.CD_Term, args.ref(), argc))
Expand Down Expand Up @@ -128,7 +127,7 @@ private void buildType(@NotNull FreeCodeBuilder builder, @NotNull MatchyData dat

builder.buildMethod(Constants.CD_Term, "invoke", ImmutableSeq.fill(capturec + argc, Constants.CD_Term),
(ap, cb) -> {
var captures = ImmutableSeq.fill(capturec, i -> ap.arg(i));
var captures = ImmutableSeq.fill(capturec, ap::arg);
var args = ImmutableSeq.fill(argc, i -> ap.arg(i + capturec));
buildInvoke(cb, unit, captures, args);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ private TermExprializer(
@NotNull ImmutableSeq<FreeJavaExpr> captures
) {
return builder.invoke(
Constants.MATCHY_INVOKE,
MatchySerializer.resolveInvoke(matchyClass, captures.size(), args.size()),
getInstance(builder, matchyClass),
ImmutableSeq.of(
makeImmutableSeq(Term.class, captures),
makeImmutableSeq(Term.class, args)
));
captures.appendedAll(args)
);
}

@Override protected @NotNull FreeJavaExpr doSerialize(@NotNull Term term) {
Expand Down Expand Up @@ -185,7 +183,7 @@ case FnCall(var ref, var ulift, var args) -> buildFnInvoke(
));
yield builder.invoke(Constants.RULEREDUCER_MAKE, onStuck, ImmutableSeq.empty());
}
case RuleReducer.Fn (var rule, int ulift, var args) -> {
case RuleReducer.Fn(var rule, int ulift, var args) -> {
var onStuck = builder.mkNew(RuleReducer.Fn.class, ImmutableSeq.of(
serializeApplicable(rule),
builder.iconst(ulift),
Expand Down

0 comments on commit 94ec18b

Please sign in to comment.