Skip to content

Commit

Permalink
merge: Fix #1274 (#1280)
Browse files Browse the repository at this point in the history
Fix #1274
  • Loading branch information
ice1000 authored Jan 6, 2025
2 parents 6af9f5e + 511c97f commit 7e00dc3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
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

0 comments on commit 7e00dc3

Please sign in to comment.