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

Free java #1271

Merged
merged 16 commits into from
Jan 4, 2025
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.cli.library.incremental;

Expand All @@ -8,7 +8,6 @@
import org.aya.cli.library.source.LibrarySource;
import org.aya.cli.utils.CompilerUtil;
import org.aya.compiler.CompiledModule;
import org.aya.compiler.free.morphism.SourceFreeJavaBuilder;
import org.aya.compiler.serializers.ModuleSerializer;
import org.aya.compiler.serializers.NameSerializer;
import org.aya.prelude.GeneratedVersion;
Expand Down Expand Up @@ -129,8 +128,8 @@ public void addURL(Path url) throws MalformedURLException {
@NotNull ImmutableSeq<TyckDef> defs,
@NotNull ModuleLoader recurseLoader
) throws IOException, ClassNotFoundException {
var javaCode = new ModuleSerializer<String>(resolveInfo.shapeFactory())
.serialize(SourceFreeJavaBuilder.create(), new ModuleSerializer.ModuleResult(
var javaCode = new ModuleSerializer(resolveInfo.shapeFactory())
.serializeWithBestBuilder(new ModuleSerializer.ModuleResult(
QPath.fileLevel(file.moduleName()),
defs.filterIsInstance(TopLevelDef.class)));
var libraryRoot = file.owner().outDir();
Expand Down
1 change: 0 additions & 1 deletion jit-compiler/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
exports org.aya.compiler;
exports org.aya.compiler.free;
exports org.aya.compiler.free.data;
exports org.aya.compiler.free.morphism;
exports org.aya.compiler.serializers;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.aya.compiler.free.data.LocalVariable;
import org.jetbrains.annotations.NotNull;

/**
* The implementation should be pure (at least, same input same output, some kind of side effect is acceptable)
*/
public interface ArgumentProvider {
interface Lambda extends ArgumentProvider {
@NotNull FreeJavaExpr capture(int nth);
Expand Down
50 changes: 25 additions & 25 deletions jit-compiler/src/main/java/org/aya/compiler/free/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import kala.collection.immutable.ImmutableTreeSeq;
import kala.collection.mutable.MutableSeq;
import kala.control.Result;
import org.aya.compiler.free.data.FieldRef;
import org.aya.compiler.free.data.MethodRef;
import org.aya.compiler.free.data.FieldRef;
import org.aya.syntax.compile.JitClass;
import org.aya.syntax.compile.JitCon;
import org.aya.syntax.compile.JitData;
Expand Down Expand Up @@ -41,30 +41,30 @@ private Constants() { }
public static final @NotNull ClassDesc CD_Result = FreeUtil.fromClass(Result.class);

// Term -> Term
public static final @NotNull MethodRef CLOSURE = new MethodRef.Default(
public static final @NotNull MethodRef CLOSURE = new MethodRef(
FreeUtil.fromClass(UnaryOperator.class),
"apply",
ConstantDescs.CD_Object, ImmutableSeq.of(ConstantDescs.CD_Object),
true
);

// () -> Term
public static final @NotNull MethodRef THUNK = new MethodRef.Default(
public static final @NotNull MethodRef THUNK = new MethodRef(
FreeUtil.fromClass(Supplier.class),
"get",
ConstantDescs.CD_Object, ImmutableSeq.empty(),
true
);

public static final @NotNull MethodRef FUNCTION = new MethodRef.Default(
public static final @NotNull MethodRef FUNCTION = new MethodRef(
FreeUtil.fromClass(Function.class),
"apply",
ConstantDescs.CD_Object, ImmutableSeq.of(ConstantDescs.CD_Object),
true
);

// ImmutableSeq from(Object[])
public static final @NotNull MethodRef IMMSEQ = new MethodRef.Default(
public static final @NotNull MethodRef IMMSEQ = new MethodRef(
CD_ImmutableSeq,
"from",
CD_ImmutableSeq, ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()),
Expand All @@ -74,7 +74,7 @@ private Constants() { }
/**
* @see MutableSeq#fill(int, Object)
*/
public static final @NotNull MethodRef MUTSEQ = new MethodRef.Default(
public static final @NotNull MethodRef MUTSEQ = new MethodRef(
CD_MutableSeq,
"fill",
CD_MutableSeq, ImmutableSeq.of(ConstantDescs.CD_int, ConstantDescs.CD_Object),
Expand All @@ -84,7 +84,7 @@ private Constants() { }
/**
* @see MutableSeq#set(int, Object)
*/
public static final @NotNull MethodRef MUTSEQ_SET = new MethodRef.Default(
public static final @NotNull MethodRef MUTSEQ_SET = new MethodRef(
CD_MutableSeq, "set", ConstantDescs.CD_void,
ImmutableSeq.of(ConstantDescs.CD_int, ConstantDescs.CD_Object),
true
Expand All @@ -95,7 +95,7 @@ private Constants() { }
*
* @see Seq#get(int)
*/
public static final @NotNull MethodRef SEQ_GET = new MethodRef.Default(
public static final @NotNull MethodRef SEQ_GET = new MethodRef(
CD_Seq, "get", ConstantDescs.CD_Object,
ImmutableSeq.of(ConstantDescs.CD_int),
true
Expand All @@ -104,19 +104,19 @@ private Constants() { }
/**
* @see Seq#toImmutableSeq()
*/
public static final @NotNull MethodRef SEQ_TOIMMSEQ = new MethodRef.Default(
public static final @NotNull MethodRef SEQ_TOIMMSEQ = new MethodRef(
CD_Seq, "toImmutableSeq", CD_ImmutableSeq, ImmutableSeq.empty(), true
);

public static final @NotNull MethodRef IMMTREESEQ = new MethodRef.Default(
public static final @NotNull MethodRef IMMTREESEQ = new MethodRef(
FreeUtil.fromClass(ImmutableTreeSeq.class),
"from",
FreeUtil.fromClass(ImmutableTreeSeq.class),
ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()),
false
);

public static final @NotNull MethodRef BETAMAKE = new MethodRef.Default(
public static final @NotNull MethodRef BETAMAKE = new MethodRef(
FreeUtil.fromClass(BetaRedex.class),
"make",
CD_Term, ImmutableSeq.empty(),
Expand All @@ -126,14 +126,14 @@ private Constants() { }
/**
* @see Term#elevate(int)
*/
public static final @NotNull MethodRef ELEVATE = new MethodRef.Default(
public static final @NotNull MethodRef ELEVATE = new MethodRef(
CD_Term, "elevate", CD_Term, ImmutableSeq.of(ConstantDescs.CD_int), true
);

/**
* @see RuleReducer#make()
*/
public static final @NotNull MethodRef RULEREDUCER_MAKE = new MethodRef.Default(
public static final @NotNull MethodRef RULEREDUCER_MAKE = new MethodRef(
FreeUtil.fromClass(RuleReducer.class),
"make",
CD_Term, ImmutableSeq.empty(),
Expand All @@ -143,7 +143,7 @@ private Constants() { }
/**
* @see Closure#mkConst(Term)
*/
public static final @NotNull MethodRef CLOSURE_MKCONST = new MethodRef.Default(
public static final @NotNull MethodRef CLOSURE_MKCONST = new MethodRef(
FreeUtil.fromClass(Closure.class),
"mkConst",
FreeUtil.fromClass(Closure.class),
Expand All @@ -154,15 +154,15 @@ private Constants() { }
/**
* @see Panic#unreachable()
*/
public static final @NotNull MethodRef PANIC = new MethodRef.Default(
public static final @NotNull MethodRef PANIC = new MethodRef(
FreeUtil.fromClass(Panic.class),
"unreachable",
ConstantDescs.CD_Object,
ImmutableSeq.empty(),
true
);

public static final @NotNull MethodRef INT_REPR = new MethodRef.Default(
public static final @NotNull MethodRef INT_REPR = new MethodRef(
FreeUtil.fromClass(IntegerTerm.class),
"repr",
ConstantDescs.CD_int,
Expand All @@ -173,7 +173,7 @@ private Constants() { }
/**
* @see ConCallLike#conArgs()
*/
public static final @NotNull MethodRef CONARGS = new MethodRef.Default(
public static final @NotNull MethodRef CONARGS = new MethodRef(
FreeUtil.fromClass(ConCallLike.class),
"conArgs",
CD_ImmutableSeq,
Expand All @@ -184,7 +184,7 @@ private Constants() { }
/**
* @see TupTerm#lhs()
*/
public static final @NotNull MethodRef TUP_LHS = new MethodRef.Default(
public static final @NotNull MethodRef TUP_LHS = new MethodRef(
FreeUtil.fromClass(TupTerm.class),
"lhs",
CD_Term,
Expand All @@ -195,7 +195,7 @@ private Constants() { }
/**
* @see TupTerm#rhs()
*/
public static final @NotNull MethodRef TUP_RHS = new MethodRef.Default(
public static final @NotNull MethodRef TUP_RHS = new MethodRef(
FreeUtil.fromClass(TupTerm.class),
"rhs",
CD_Term,
Expand All @@ -206,13 +206,13 @@ private Constants() { }
/**
* @see Result#ok(Object)
*/
public static final @NotNull MethodRef RESULT_OK = new MethodRef.Default(
public static final @NotNull MethodRef RESULT_OK = new MethodRef(
CD_Result, "ok",
CD_Result, ImmutableSeq.of(ConstantDescs.CD_Object),
true
);

public static final @NotNull MethodRef RESULT_ERR = new MethodRef.Default(
public static final @NotNull MethodRef RESULT_ERR = new MethodRef(
CD_Result, "err",
CD_Result, ImmutableSeq.of(ConstantDescs.CD_Object),
true
Expand All @@ -225,13 +225,13 @@ private Constants() { }
ConstantDescs.CD_int, ConstantDescs.CD_boolean.arrayType(), ConstantDescs.CD_String.arrayType()
);

public static final @NotNull FieldRef JITDATA_CONS = new FieldRef.Default(
public static final @NotNull FieldRef JITDATA_CONS = new FieldRef(
FreeUtil.fromClass(JitData.class),
FreeUtil.fromClass(JitCon.class).arrayType(),
"constructors"
);

public static final @NotNull FieldRef JITCLASS_MEMS = new FieldRef.Default(
public static final @NotNull FieldRef JITCLASS_MEMS = new FieldRef(
FreeUtil.fromClass(JitClass.class),
FreeUtil.fromClass(JitMember.class).arrayType(),
"members"
Expand All @@ -240,7 +240,7 @@ private Constants() { }
/**
* @see UnaryOperator#identity()
*/
public static final @NotNull MethodRef CLOSURE_ID = new MethodRef.Default(
public static final @NotNull MethodRef CLOSURE_ID = new MethodRef(
FreeUtil.fromClass(UnaryOperator.class),
"identity",
FreeUtil.fromClass(UnaryOperator.class),
Expand All @@ -251,7 +251,7 @@ private Constants() { }
/**
* @see PatMatcher#apply(ImmutableSeq, ImmutableSeq)
*/
public static final @NotNull MethodRef PATMATCHER_APPLY = new MethodRef.Default(
public static final @NotNull MethodRef PATMATCHER_APPLY = new MethodRef(
FreeUtil.fromClass(PatMatcher.class), "apply",
CD_Result, ImmutableSeq.of(CD_ImmutableSeq, CD_ImmutableSeq), false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

public interface FreeClassBuilder {
static @NotNull MethodRef makeConstructorRef(@NotNull ClassDesc owner, @NotNull ImmutableSeq<ClassDesc> parameterTypes) {
return new MethodRef.Default(owner, ConstantDescs.INIT_NAME, ConstantDescs.CD_void, parameterTypes, false);
return new MethodRef(owner, ConstantDescs.INIT_NAME, ConstantDescs.CD_void, parameterTypes, false);
}

void buildMetadata(@NotNull CompiledAya compiledAya);
void buildNestedClass(
@NotNull CompiledAya compiledAya,
@NotNull String name,
Expand All @@ -44,6 +43,4 @@ void buildNestedClass(
@NotNull String name,
@NotNull Function<FreeExprBuilder, FreeJavaExpr> initializer
);

@NotNull FreeJavaResolver resolver();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import kala.collection.immutable.ImmutableSeq;
import kala.collection.immutable.primitive.ImmutableIntSeq;
import org.aya.compiler.free.data.FieldRef;
import org.aya.compiler.free.data.LocalVariable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -27,10 +26,6 @@ default LocalVariable makeVar(@NotNull Class<?> type, @Nullable FreeJavaExpr ini

void updateArray(@NotNull FreeJavaExpr array, int idx, @NotNull FreeJavaExpr update);

void updateField(@NotNull FieldRef field, @NotNull FreeJavaExpr update);

void updateField(@NotNull FieldRef field, @NotNull FreeJavaExpr owner, @NotNull FreeJavaExpr update);

void ifNotTrue(@NotNull LocalVariable notTrue, @NotNull Consumer<FreeCodeBuilder> thenBlock, @Nullable Consumer<FreeCodeBuilder> elseBlock);

void ifTrue(@NotNull LocalVariable theTrue, @NotNull Consumer<FreeCodeBuilder> thenBlock, @Nullable Consumer<FreeCodeBuilder> elseBlock);
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.free;

Expand All @@ -7,6 +7,7 @@
import org.aya.compiler.free.data.LocalVariable;
import org.aya.compiler.free.data.MethodRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.constant.ClassDesc;
import java.util.Arrays;
Expand All @@ -16,8 +17,6 @@
* the result only depends on the {@link FreeCodeBuilder} that this builder derived from
*/
public interface FreeExprBuilder {
@NotNull FreeJavaResolver resolver();

/**
* A {@code new} expression on specified constructor.
*/
Expand All @@ -39,7 +38,9 @@ public interface FreeExprBuilder {
return mkNew(conRef, args);
}

@NotNull FreeJavaExpr refVar(@NotNull LocalVariable name);
default @NotNull FreeJavaExpr refVar(@NotNull LocalVariable name) {
return name.ref();
}

/**
* Invoke a (non-interface) method on {@param owner}.
Expand All @@ -62,6 +63,12 @@ public interface FreeExprBuilder {
return refEnum(cd, name);
}

/**
* Make a lambda expression
*
* @param builder the builder for building the lambda body, you should use local variable comes from this and the
* {@link ArgumentProvider.Lambda} ONLY, other variables introduced outside of the lambda is unavailable.
*/
@NotNull FreeJavaExpr mkLambda(
@NotNull ImmutableSeq<FreeJavaExpr> captures,
@NotNull MethodRef method,
Expand All @@ -85,7 +92,7 @@ public interface FreeExprBuilder {
*/
@NotNull FreeJavaExpr mkArray(
@NotNull ClassDesc type, int length,
@NotNull ImmutableSeq<FreeJavaExpr> initializer
@Nullable ImmutableSeq<FreeJavaExpr> initializer
);

@NotNull FreeJavaExpr getArray(@NotNull FreeJavaExpr array, int index);
Expand Down
Loading
Loading