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

Fix #1274 #1280

Merged
merged 2 commits into from
Jan 6, 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
10 changes: 6 additions & 4 deletions jit-compiler/src/main/java/org/aya/compiler/free/Constants.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.compiler.free;

Expand Down Expand Up @@ -39,6 +39,8 @@ private Constants() { }
public static final @NotNull ClassDesc CD_MutableSeq = FreeUtil.fromClass(MutableSeq.class);
public static final @NotNull ClassDesc CD_Thunk = FreeUtil.fromClass(Supplier.class);
public static final @NotNull ClassDesc CD_Result = FreeUtil.fromClass(Result.class);
public static final @NotNull String NAME_OF = "of";
public static final @NotNull String NAME_EMPTY = "empty";

// Term -> Term
public static final @NotNull MethodRef CLOSURE = new MethodRef(
Expand All @@ -63,10 +65,10 @@ private Constants() { }
true
);

// ImmutableSeq from(Object[])
/// @see ImmutableSeq#of(Object[])
public static final @NotNull MethodRef IMMSEQ = new MethodRef(
CD_ImmutableSeq,
"from",
NAME_OF,
CD_ImmutableSeq, ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()),
true
);
Expand Down Expand Up @@ -110,7 +112,7 @@ private Constants() { }

public static final @NotNull MethodRef IMMTREESEQ = new MethodRef(
FreeUtil.fromClass(ImmutableTreeSeq.class),
"from",
NAME_OF,
FreeUtil.fromClass(ImmutableTreeSeq.class),
ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()),
false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// 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;

Expand All @@ -10,6 +10,7 @@
import org.jetbrains.annotations.NotNull;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;

public abstract class AbstractExprializer<T> {
protected final @NotNull FreeExprBuilder builder;
Expand Down Expand Up @@ -39,14 +40,48 @@ public abstract class AbstractExprializer<T> {
return makeImmutableSeq(builder, Constants.IMMSEQ, typeName, terms);
}

/// @param con the factory function used for constructing an {@link ImmutableSeq}, usually it is {@link ImmutableSeq#of}.
/// This function may re-resolve the factory function to fixed size parameters one
/// with the name of {@param con}
/// in case {@param terms} is very small.
/// @see ImmutableSeq#empty()
/// @see ImmutableSeq#of(Object)
/// @see ImmutableSeq#of(Object, Object)
/// @see ImmutableSeq#of(Object, Object, Object)
/// @see ImmutableSeq#of(Object, Object, Object, Object)
/// @see ImmutableSeq#of(Object, Object, Object, Object, Object)
/// @see ImmutableSeq#of(Object[])
public static @NotNull FreeJavaExpr makeImmutableSeq(
@NotNull FreeExprBuilder builder,
@NotNull MethodRef con,
@NotNull Class<?> typeName,
@NotNull ImmutableSeq<FreeJavaExpr> terms
) {
var args = builder.mkArray(FreeUtil.fromClass(typeName), terms.size(), terms);
return builder.invoke(con, ImmutableSeq.of(args));
ImmutableSeq<FreeJavaExpr> args;

if (terms.size() <= 5) {
String name = con.name();
ImmutableSeq<ClassDesc> params;

// re-resolve
if (terms.isEmpty()) {
name = Constants.NAME_EMPTY;
params = ImmutableSeq.empty();
} else {
params = ImmutableSeq.fill(terms.size(), ConstantDescs.CD_Object);
}

con = FreeJavaResolver.resolve(
con.owner(), name,
con.returnType(), params,
con.isInterface());

args = terms;
} else {
args = ImmutableSeq.of(builder.mkArray(FreeUtil.fromClass(typeName), terms.size(), terms));
}

return builder.invoke(con, args);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public PatternSerializer serialize(@NotNull FreeCodeBuilder builder, @NotNull Im
assert i > 0;
var realIdx = i - 1;
unit.get(realIdx).onSucc.accept(this, mBuilder, bindSize.get(realIdx));
}, builder1 -> builder1.unreachable());
}, FreeCodeBuilder::unreachable);

return this;
}
Expand Down
Loading