-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry picked from #1134
- Loading branch information
Showing
23 changed files
with
381 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
jit-compiler/src/main/java/org/aya/compiler/ClassSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2020-2024 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; | ||
|
||
import kala.collection.immutable.ImmutableSeq; | ||
import org.aya.syntax.compile.JitClass; | ||
import org.aya.syntax.compile.JitMember; | ||
import org.aya.syntax.core.def.ClassDef; | ||
import org.aya.syntax.core.term.call.ClassCall; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static org.aya.compiler.AyaSerializer.CLASS_IMMSEQ; | ||
import static org.aya.compiler.ExprializeUtils.getJavaReference; | ||
import static org.aya.compiler.NameSerializer.getClassReference; | ||
|
||
public final class ClassSerializer extends JitDefSerializer<ClassDef> { | ||
public static final String CLASS_JITMEMBERS = getJavaReference(JitMember.class); | ||
public static final String CLASS_CLASSCALL = getJavaReference(ClassCall.class); | ||
public static final String FIELD_MEMBERS = "members"; | ||
public static final String METHOD_MEMBARS = "membars"; | ||
|
||
public ClassSerializer(@NotNull SourceBuilder builder) { super(builder, JitClass.class); } | ||
@Override protected @NotNull String callClass() { return CLASS_CLASSCALL; } | ||
@Override protected void buildConstructor(ClassDef unit) { buildSuperCall(ImmutableSeq.empty()); } | ||
|
||
@Override | ||
protected boolean shouldBuildEmptyCall(@NotNull ClassDef unit) { | ||
return true; | ||
} | ||
|
||
private void buildMembers(ClassDef unit) { | ||
buildIf(STR."\{FIELD_MEMBERS} == null", () -> | ||
buildUpdate(FIELD_MEMBERS, ExprializeUtils.makeArrayFrom(CLASS_JITMEMBERS, unit.members().map(mem -> | ||
ExprializeUtils.getInstance(getClassReference(mem.ref()))) | ||
))); | ||
|
||
buildReturn(FIELD_MEMBERS); | ||
} | ||
|
||
@Override public AbstractSerializer<ClassDef> serialize(ClassDef unit) { | ||
buildFramework(unit, () -> | ||
buildMethod(METHOD_MEMBARS, ImmutableSeq.empty(), STR."\{CLASS_IMMSEQ}<\{CLASS_JITMEMBERS}>", true, | ||
() -> buildMembers(unit))); | ||
|
||
return this; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
jit-compiler/src/main/java/org/aya/compiler/JitDefSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2020-2024 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; | ||
|
||
import kala.collection.immutable.ImmutableSeq; | ||
import org.aya.syntax.compile.CompiledAya; | ||
import org.aya.syntax.core.def.TyckDef; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static org.aya.compiler.AyaSerializer.*; | ||
import static org.aya.compiler.NameSerializer.javifyClassName; | ||
|
||
public abstract class JitDefSerializer<T extends TyckDef> extends AbstractSerializer<T> { | ||
public static final String CLASS_METADATA = ExprializeUtils.getJavaReference(CompiledAya.class); | ||
|
||
protected final @NotNull Class<?> superClass; | ||
|
||
protected JitDefSerializer(@NotNull SourceBuilder builder, @NotNull Class<?> superClass) { | ||
super(builder); | ||
this.superClass = superClass; | ||
} | ||
|
||
/** | ||
* @see CompiledAya | ||
*/ | ||
protected void buildMetadata(@NotNull T unit) { | ||
var ref = unit.ref(); | ||
var module = ref.module; | ||
var assoc = ref.assoc(); | ||
var assocIdx = assoc == null ? -1 : assoc.ordinal(); | ||
assert module != null; | ||
appendLine(STR."@\{CLASS_METADATA}("); | ||
var modPath = module.module().module(); | ||
appendMetadataRecord("module", ExprializeUtils.makeHalfArrayFrom(modPath.view().map(ExprializeUtils::makeString)), true); | ||
// Assumption: module.take(fileModule.size).equals(fileModule) | ||
appendMetadataRecord("fileModuleSize", Integer.toString(module.fileModuleSize()), false); | ||
appendMetadataRecord("name", ExprializeUtils.makeString(ref.name()), false); | ||
appendMetadataRecord("assoc", Integer.toString(assocIdx), false); | ||
buildShape(unit); | ||
|
||
appendLine(")"); | ||
} | ||
|
||
protected void buildShape(T unit) { | ||
appendMetadataRecord("shape", "-1", false); | ||
appendMetadataRecord("recognition", ExprializeUtils.makeHalfArrayFrom(ImmutableSeq.empty()), false); | ||
} | ||
|
||
protected void appendMetadataRecord(@NotNull String name, @NotNull String value, boolean isFirst) { | ||
var prepend = isFirst ? "" : ", "; | ||
appendLine(STR."\{prepend}\{name} = \{value}"); | ||
} | ||
|
||
public void buildInstance(@NotNull String className) { | ||
buildConstantField(className, STATIC_FIELD_INSTANCE, ExprializeUtils.makeNew(className)); | ||
} | ||
|
||
public void buildSuperCall(@NotNull ImmutableSeq<String> args) { | ||
appendLine(STR."super(\{args.joinToString(", ")});"); | ||
} | ||
|
||
protected abstract boolean shouldBuildEmptyCall(@NotNull T unit); | ||
|
||
protected void buildFramework(@NotNull T unit, @NotNull Runnable continuation) { | ||
var className = javifyClassName(unit.ref()); | ||
buildMetadata(unit); | ||
buildInnerClass(className, superClass, () -> { | ||
buildInstance(className); | ||
appendLine(); | ||
// empty return type for constructor | ||
buildMethod(className, ImmutableSeq.empty(), "/*constructor*/", false, () -> buildConstructor(unit)); | ||
appendLine(); | ||
if (shouldBuildEmptyCall(unit)) { | ||
buildConstantField(callClass(), FIELD_EMPTYCALL, ExprializeUtils.makeNew( | ||
callClass(), ExprializeUtils.getInstance(className))); | ||
} | ||
appendLine(); | ||
continuation.run(); | ||
}); | ||
} | ||
|
||
protected abstract @NotNull String callClass(); | ||
|
||
/** | ||
* @see org.aya.syntax.compile.JitDef | ||
*/ | ||
protected abstract void buildConstructor(T unit); | ||
} |
Oops, something went wrong.