mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-27 23:45:52 +00:00
refactor: port composite generation to Java
This commit is contained in:
1 parent
1c9cd65a74
commit
e632135393
5 files changed
+235
-180
No files matched your search
+3
-3
@@ -25,9 +25,9 @@ No trustworthy tool will produce a maintainable Java port automatically. A decom
|
||||
|
||||
Work continues on `algent/java`. The low-coupling queue, core part/tile types, registries, handlers, factories,
|
||||
placement/render helpers, both generators and all built-in tile traits are Java. Six generated microblock trait
|
||||
files and the compiler/signature/analyser compatibility shells remain Scala. The next bounded target is
|
||||
`ASMMixinCompiler.mixinClasses`; `define` was extracted into `ClassBytes` in `cf8b2f9` after the separate
|
||||
characterization commit `4d59900`.
|
||||
files and the compiler/signature/analyser compatibility shells remain Scala. Composite generation now delegates to
|
||||
Java `MixinClassGenerator`; the next bounded target is `ASMMixinCompiler.registerJavaTrait`, characterized before
|
||||
its mechanical extraction and kept separate from planned compiler algorithm changes.
|
||||
|
||||
Start with [JAVA_MIGRATION_HANDOFF.md](JAVA_MIGRATION_HANDOFF.md) for the exact source/test baseline, workflow and
|
||||
Java-source limitations. Read both the [ABI inventory](JAVA_MIGRATION_ABI_INVENTORY.md) and
|
||||
|
||||
+10
-10
@@ -16,18 +16,18 @@ migration checkout; `codex/tile-compatibility-fixes` was deleted after its fixes
|
||||
|
||||
## Current state and next target
|
||||
|
||||
**333 plain-JVM tests and 183 Java 8 Forge tests pass, with zero failures/errors/skips.** Sources total **211 Java
|
||||
files and 9 Scala files / 1,583 nonblank Scala lines**. The packaged inventory has 456 classes.
|
||||
**333 plain-JVM tests and 191 Java 8 Forge tests pass, with zero failures/errors/skips.** Sources total **212 Java
|
||||
files and 9 Scala files / 1,444 nonblank Scala lines**. The packaged inventory has 442 classes.
|
||||
|
||||
Latest bounded port: `ASMMixinCompiler.define` delegates to Java `ClassBytes` (`cf8b2f9`), after ten characterization
|
||||
tests passed against untouched Scala (`4d59900`). Clean verification after stopping Gradle matched 426 non-closure
|
||||
APIs, all 30 compiler closures, 3,628 other method bodies and all 42 generated dump names/hashes. The five `@Mod`
|
||||
annotations match both packaged versions. Local reference jars, reports and scripts are in
|
||||
`run/migration-define-reference/`; `clean` preserves this ignored directory.
|
||||
Latest bounded port: `ASMMixinCompiler.mixinClasses` delegates to Java `MixinClassGenerator`, after eight
|
||||
characterization tests passed against untouched Scala (`1c9cd65`). Clean verification after stopping Gradle matched
|
||||
426 non-closure APIs, all 13 non-target compiler closures, 3,628 non-target method bodies and all 79 generated dump
|
||||
names/hashes. The five `@Mod` annotations match both packaged versions. Local reference jars, reports and scripts are
|
||||
in `run/migration-composition-reference/`; `clean` preserves this ignored directory.
|
||||
|
||||
**Next: `ASMMixinCompiler.mixinClasses` composite generation.** Characterize constructor selection, trait
|
||||
linearization, field initialization, method/super dispatch, failure ordering and generated output before extraction.
|
||||
Preserve the existing algorithm. Keep Java-trait rewriting and startup initialization separate.
|
||||
**Next: `ASMMixinCompiler.registerJavaTrait` rewriting.** Characterize rejected shapes, cloning, initialization,
|
||||
field/method/super rewriting, publication and failure ordering before extraction. Preserve the existing algorithm;
|
||||
keep abstract-mixin support, side-only member filtering and other compiler changes separate.
|
||||
|
||||
Remaining Scala units:
|
||||
|
||||
|
||||
@@ -929,3 +929,23 @@ differences belong in the [divergence ledger](../../JAVA_MIGRATION_DIVERGENCES.m
|
||||
- Consolidated the active plan/handoff after the reflective-definition port. The dated findings remain intact in
|
||||
this history, duplicated completed-port handoff summaries were removed, and current phase/status text was refreshed.
|
||||
The manual checklist now names verified consumer items, including the inverted ProjectRed lamp requirement.
|
||||
|
||||
### 2026-09-03 — composite class generation
|
||||
|
||||
- Added eight Forge characterization tests for `ASMMixinCompiler.mixinClasses`, passing on untouched Scala and
|
||||
committed first as `1c9cd65`. They execute generated JVM classes and freeze empty selection, constructor forwarding,
|
||||
diamond linearization, initialization, field storage and mangling, method/super dispatch, covariant bridges,
|
||||
implemented-interface order and failures before or during definition. The reference also confirms the existing
|
||||
verifier failure for generated `long` and `double` field getters whose maximum stack remains one.
|
||||
- Moved only composite generation into Java `MixinClassGenerator`, retaining the Scala entry point and the unusual
|
||||
public compiler-local `allParents` helper descriptor. External Scala-trait registration, Java-trait rewriting and
|
||||
compiler algorithms are unchanged. The 456-class reference becomes 442 classes: 16 unreferenced target closures
|
||||
become the Java helper and one private callback. The shared classfile ledger covers this with no new effective
|
||||
divergence.
|
||||
- Saved the pre-port source/jar, reports, 79 generated outputs and checks in ignored
|
||||
`run/migration-composition-reference/`. Clean verification after stopping Gradle matches all 426 non-closure APIs,
|
||||
3,628 non-target method bodies, 13 other compiler closures and every generated output name/hash. Formatting,
|
||||
checkstyle, build and Forge pass: 333 JVM / 191 Forge, zero failures/errors/skips. All five packaged `@Mod` versions,
|
||||
the forced Scala-compilation guard and dev config remain correct. Sources total 212 Java files and 9 Scala files /
|
||||
1,444 nonblank Scala lines. Next: `ASMMixinCompiler.registerJavaTrait`, characterized before extraction; abstract
|
||||
mixins, side-only filtering and the wide-field defect remain separate compiler changes.
|
||||
@@ -207,175 +207,17 @@ object ASMMixinCompiler {
|
||||
superClass: String,
|
||||
traits: Seq[String]
|
||||
): Class[_] = {
|
||||
if (traits.isEmpty)
|
||||
return cl.findClass(superClass.name.replace('/', '.'))
|
||||
|
||||
val startTime = System.currentTimeMillis
|
||||
|
||||
val baseTraits = traits.map(mixinMap)
|
||||
val mixinInfos = baseTraits.flatMap(_.linearise).distinct
|
||||
val baseInfo = getClassInfo(superClass)
|
||||
val traitInfos = mixinInfos.map(i => getClassInfo(i.name))
|
||||
|
||||
val cnode = new ClassNode()
|
||||
// implements list
|
||||
cnode.visit(
|
||||
V1_6,
|
||||
ACC_PUBLIC,
|
||||
name,
|
||||
null,
|
||||
superClass,
|
||||
baseTraits.map(_.name).toArray[String]
|
||||
)
|
||||
|
||||
val cinit = baseInfo.methods.find(_.name == "<init>").get
|
||||
val minit = cnode
|
||||
.visitMethod(ACC_PUBLIC, "<init>", cinit.desc, null, null)
|
||||
.asInstanceOf[MethodNode]
|
||||
writeBridge(
|
||||
minit,
|
||||
cinit.desc,
|
||||
INVOKESPECIAL,
|
||||
superClass,
|
||||
"<init>",
|
||||
cinit.desc
|
||||
)
|
||||
minit.instructions.remove(
|
||||
minit.instructions.getLast
|
||||
) // remove the RETURN from writeBridge
|
||||
|
||||
val prevInfos = MList[MixinInfo]()
|
||||
|
||||
mixinInfos.foreach { t =>
|
||||
minit.visitVarInsn(ALOAD, 0)
|
||||
minit.visitMethodInsn(
|
||||
INVOKESTATIC,
|
||||
t.tname,
|
||||
"$init$",
|
||||
"(L" + t.name + ";)V",
|
||||
false
|
||||
)
|
||||
|
||||
t.fields.foreach { f =>
|
||||
val fv = cnode
|
||||
.visitField(ACC_PRIVATE, f.accessName(t.name), f.desc, null, null)
|
||||
.asInstanceOf[FieldNode]
|
||||
|
||||
val ftype = getType(fv.desc)
|
||||
var mv =
|
||||
cnode.visitMethod(ACC_PUBLIC, fv.name, "()" + f.desc, null, null)
|
||||
mv.visitVarInsn(ALOAD, 0)
|
||||
mv.visitFieldInsn(GETFIELD, name, fv.name, fv.desc)
|
||||
mv.visitInsn(ftype.getOpcode(IRETURN))
|
||||
mv.visitMaxs(1, 1)
|
||||
|
||||
mv = cnode.visitMethod(
|
||||
ACC_PUBLIC,
|
||||
fv.name + "_$eq",
|
||||
"(" + f.desc + ")V",
|
||||
null,
|
||||
null
|
||||
)
|
||||
mv.visitVarInsn(ALOAD, 0)
|
||||
mv.visitVarInsn(ftype.getOpcode(ILOAD), 1)
|
||||
mv.visitFieldInsn(PUTFIELD, name, fv.name, fv.desc)
|
||||
mv.visitInsn(RETURN)
|
||||
mv.visitMaxs(width(ftype) + 1, width(ftype) + 1)
|
||||
}
|
||||
|
||||
t.supers.foreach { s =>
|
||||
val (name, desc) = seperateDesc(s)
|
||||
val mv = cnode
|
||||
.visitMethod(
|
||||
ACC_PUBLIC,
|
||||
t.name.replace('/', '$') + "$$super$" + name,
|
||||
desc,
|
||||
null,
|
||||
null
|
||||
)
|
||||
.asInstanceOf[MethodNode]
|
||||
|
||||
prevInfos.reverse.find(
|
||||
_.methods.exists(m => m.name == name && m.desc == desc)
|
||||
) match {
|
||||
// each super goes to the one before
|
||||
case Some(st) => writeStaticBridge(mv, name, st)
|
||||
case None =>
|
||||
writeBridge(
|
||||
mv,
|
||||
desc,
|
||||
INVOKESPECIAL,
|
||||
baseInfo.findPublicImpl(name, desc).get.owner.name,
|
||||
name,
|
||||
desc
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
prevInfos += t
|
||||
}
|
||||
|
||||
val methodSigs = MSet[String]()
|
||||
mixinInfos.reverse.foreach { t => // last trait gets first pick on methods
|
||||
t.methods.foreach { m =>
|
||||
if (!methodSigs(m.name + m.desc)) {
|
||||
val mv = cnode
|
||||
.visitMethod(
|
||||
ACC_PUBLIC,
|
||||
m.name,
|
||||
m.desc,
|
||||
null,
|
||||
Array(m.exceptions: _*)
|
||||
)
|
||||
.asInstanceOf[MethodNode]
|
||||
writeStaticBridge(mv, m.name, t)
|
||||
methodSigs += m.name + m.desc
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
minit.visitInsn(RETURN)
|
||||
|
||||
// generate synthetic bridge methods for covariant return types
|
||||
// Retain the compiler-emitted public local-helper binary name.
|
||||
def allParents(info: ClassInfo): Iterable[ClassInfo] =
|
||||
info +: (info.superClass ++ info.interfaces).toSeq.flatMap(allParents)
|
||||
val allParentInfos = (baseInfo +: traitInfos).flatMap(allParents).distinct
|
||||
val allParentMethods = allParentInfos.flatMap(_.methods)
|
||||
methodSigs.toSeq.foreach { nameDesc =>
|
||||
val (name, desc) = seperateDesc(nameDesc)
|
||||
val pDesc = desc.substring(0, desc.lastIndexOf(')') + 1)
|
||||
|
||||
allParentMethods
|
||||
.filter(m => m.name == name && m.desc.startsWith(pDesc))
|
||||
.foreach { m =>
|
||||
if (!methodSigs(m.name + m.desc)) {
|
||||
val mv = cnode
|
||||
.visitMethod(
|
||||
ACC_PUBLIC | ACC_SYNTHETIC | ACC_BRIDGE,
|
||||
m.name,
|
||||
m.desc,
|
||||
null,
|
||||
m.exceptions
|
||||
)
|
||||
.asInstanceOf[MethodNode]
|
||||
writeBridge(mv, mv.desc, INVOKEVIRTUAL, cnode.name, name, desc)
|
||||
methodSigs += m.name + m.desc
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val c = define(name, createBytes(cnode, 0))
|
||||
|
||||
DebugPrinter$.MODULE$
|
||||
.logger()
|
||||
.debug(
|
||||
"Generation [" + superClass + " with " + traits.mkString(
|
||||
", "
|
||||
) + "] took " + (System.currentTimeMillis - startTime) + "ms"
|
||||
)
|
||||
c
|
||||
MixinClassGenerator.allParents(info)
|
||||
MixinClassGenerator.mixinClasses(
|
||||
name,
|
||||
superClass,
|
||||
traits,
|
||||
mixinMap,
|
||||
allParents
|
||||
)
|
||||
}
|
||||
|
||||
def seperateDesc(nameDesc: String) = ASMBridgeEmitter.seperateDesc(nameDesc)
|
||||
|
||||
def staticDesc(owner: String, desc: String) =
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
package codechicken.multipart.asm;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Type;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.FieldNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
import codechicken.lib.asm.ASMHelper;
|
||||
import codechicken.multipart.asm.ASMMixinCompiler.ClassInfo;
|
||||
import codechicken.multipart.asm.ASMMixinCompiler.FieldMixin;
|
||||
import codechicken.multipart.asm.ASMMixinCompiler.MethodInfo;
|
||||
import codechicken.multipart.asm.ASMMixinCompiler.MixinInfo;
|
||||
import scala.Function1;
|
||||
import scala.Option;
|
||||
import scala.Option$;
|
||||
import scala.Tuple2;
|
||||
import scala.collection.GenTraversableOnce;
|
||||
import scala.collection.Iterable;
|
||||
import scala.collection.Iterable$;
|
||||
import scala.collection.JavaConversions;
|
||||
import scala.collection.Seq;
|
||||
import scala.collection.Seq$;
|
||||
import scala.collection.TraversableLike;
|
||||
import scala.collection.mutable.ListBuffer;
|
||||
import scala.collection.mutable.Map;
|
||||
import scala.collection.mutable.Set;
|
||||
import scala.collection.mutable.Set$;
|
||||
import scala.runtime.AbstractFunction1;
|
||||
|
||||
/** Composite generation behind the retained Scala entry point and metadata models. */
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
final class MixinClassGenerator {
|
||||
|
||||
private MixinClassGenerator() {}
|
||||
|
||||
static Class<?> mixinClasses(String name, String superClass, Seq<String> traits, Map<String, MixinInfo> mixins,
|
||||
Function1<ClassInfo, Iterable<ClassInfo>> parents) throws ClassNotFoundException {
|
||||
ASMMixinCompiler$ compiler = ASMMixinCompiler$.MODULE$;
|
||||
if (traits.isEmpty())
|
||||
return compiler.cl().findClass(compiler.getClassInfo(superClass).name().replace('/', '.'));
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
Seq<MixinInfo> baseTraits = map(traits, mixins::apply);
|
||||
Seq<MixinInfo> mixinInfos = flatMap(baseTraits, MixinInfo::linearise).distinct();
|
||||
ClassInfo baseInfo = compiler.getClassInfo(superClass);
|
||||
Seq<ClassInfo> traitInfos = map(mixinInfos, i -> compiler.getClassInfo(i.name()));
|
||||
ClassNode node = new ClassNode();
|
||||
node.visit(
|
||||
V1_6,
|
||||
ACC_PUBLIC,
|
||||
name,
|
||||
null,
|
||||
superClass,
|
||||
JavaConversions.seqAsJavaList(map(baseTraits, MixinInfo::name)).toArray(new String[0]));
|
||||
|
||||
MethodInfo constructor = baseInfo.methods().find(fn(m -> Objects.equals(m.name(), "<init>"))).get();
|
||||
MethodNode init = (MethodNode) node.visitMethod(ACC_PUBLIC, "<init>", constructor.desc(), null, null);
|
||||
compiler.writeBridge(init, constructor.desc(), INVOKESPECIAL, superClass, "<init>", constructor.desc());
|
||||
init.instructions.remove(init.instructions.getLast());
|
||||
|
||||
ListBuffer<MixinInfo> previous = new ListBuffer<>();
|
||||
for (MixinInfo trait : JavaConversions.seqAsJavaList(mixinInfos)) {
|
||||
init.visitVarInsn(ALOAD, 0);
|
||||
init.visitMethodInsn(INVOKESTATIC, trait.tname(), "$init$", "(L" + trait.name() + ";)V", false);
|
||||
for (FieldMixin field : JavaConversions.seqAsJavaList(trait.fields())) {
|
||||
FieldNode storage = (FieldNode) node
|
||||
.visitField(ACC_PRIVATE, field.accessName(trait.name()), field.desc(), null, null);
|
||||
Type type = Type.getType(storage.desc);
|
||||
MethodVisitor method = node.visitMethod(ACC_PUBLIC, storage.name, "()" + field.desc(), null, null);
|
||||
method.visitVarInsn(ALOAD, 0);
|
||||
method.visitFieldInsn(GETFIELD, name, storage.name, storage.desc);
|
||||
method.visitInsn(type.getOpcode(IRETURN));
|
||||
method.visitMaxs(1, 1);
|
||||
method = node.visitMethod(ACC_PUBLIC, storage.name + "_$eq", "(" + field.desc() + ")V", null, null);
|
||||
method.visitVarInsn(ALOAD, 0);
|
||||
method.visitVarInsn(type.getOpcode(ILOAD), 1);
|
||||
method.visitFieldInsn(PUTFIELD, name, storage.name, storage.desc);
|
||||
method.visitInsn(RETURN);
|
||||
method.visitMaxs(StackAnalyser.width(type) + 1, StackAnalyser.width(type) + 1);
|
||||
}
|
||||
for (String signature : JavaConversions.seqAsJavaList(trait.supers())) {
|
||||
Tuple2<String, String> split = compiler.seperateDesc(signature);
|
||||
String methodName = split._1();
|
||||
String descriptor = split._2();
|
||||
MethodNode method = (MethodNode) node.visitMethod(
|
||||
ACC_PUBLIC,
|
||||
trait.name().replace('/', '$') + "$$super$" + methodName,
|
||||
descriptor,
|
||||
null,
|
||||
null);
|
||||
Option<MixinInfo> prior = ((Seq<MixinInfo>) previous.reverse()).find(
|
||||
fn(
|
||||
t -> t.methods().exists(
|
||||
fn(
|
||||
m -> Objects.equals(m.name, methodName)
|
||||
&& Objects.equals(m.desc, descriptor)))));
|
||||
if (prior.isDefined()) compiler.writeStaticBridge(method, methodName, prior.get());
|
||||
else compiler.writeBridge(
|
||||
method,
|
||||
descriptor,
|
||||
INVOKESPECIAL,
|
||||
baseInfo.findPublicImpl(methodName, descriptor).get().owner().name(),
|
||||
methodName,
|
||||
descriptor);
|
||||
}
|
||||
previous.$plus$eq(trait);
|
||||
}
|
||||
|
||||
Set<String> signatures = Set$.MODULE$.empty();
|
||||
for (MixinInfo trait : JavaConversions.seqAsJavaList(mixinInfos.reverse())) {
|
||||
for (MethodNode method : JavaConversions.seqAsJavaList(trait.methods())) {
|
||||
if (!signatures.apply(method.name + method.desc)) {
|
||||
MethodNode bridge = (MethodNode) node.visitMethod(
|
||||
ACC_PUBLIC,
|
||||
method.name,
|
||||
method.desc,
|
||||
null,
|
||||
(String[]) method.exceptions.toArray(new String[0]));
|
||||
compiler.writeStaticBridge(bridge, method.name, trait);
|
||||
signatures.$plus$eq(method.name + method.desc);
|
||||
}
|
||||
}
|
||||
}
|
||||
init.visitInsn(RETURN);
|
||||
|
||||
Seq<ClassInfo> allParents = flatMap(prepend(baseInfo, traitInfos), parents::apply).distinct();
|
||||
Seq<MethodInfo> allMethods = flatMap(allParents, ClassInfo::methods);
|
||||
for (String signature : JavaConversions.seqAsJavaList(signatures.toSeq())) {
|
||||
Tuple2<String, String> split = compiler.seperateDesc(signature);
|
||||
String methodName = split._1();
|
||||
String descriptor = split._2();
|
||||
String parameters = descriptor.substring(0, descriptor.lastIndexOf(')') + 1);
|
||||
Seq<MethodInfo> matching = (Seq<MethodInfo>) allMethods
|
||||
.filter(fn(m -> Objects.equals(m.name(), methodName) && m.desc().startsWith(parameters)));
|
||||
for (MethodInfo method : JavaConversions.seqAsJavaList(matching)) {
|
||||
if (!signatures.apply(method.name() + method.desc())) {
|
||||
MethodNode bridge = (MethodNode) node.visitMethod(
|
||||
ACC_PUBLIC | ACC_SYNTHETIC | ACC_BRIDGE,
|
||||
method.name(),
|
||||
method.desc(),
|
||||
null,
|
||||
method.exceptions());
|
||||
compiler.writeBridge(bridge, bridge.desc, INVOKEVIRTUAL, node.name, methodName, descriptor);
|
||||
signatures.$plus$eq(method.name() + method.desc());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Class<?> result = compiler.define(name, ASMHelper.createBytes(node, 0));
|
||||
DebugPrinter$.MODULE$.logger().debug(
|
||||
"Generation [" + superClass
|
||||
+ " with "
|
||||
+ traits.mkString(", ")
|
||||
+ "] took "
|
||||
+ (System.currentTimeMillis() - startTime)
|
||||
+ "ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
static Iterable<ClassInfo> allParents(ClassInfo info) {
|
||||
Iterable<ClassInfo> parents = (Iterable<ClassInfo>) ((TraversableLike) Option$.MODULE$
|
||||
.option2Iterable(info.superClass())).$plus$plus(info.interfaces(), Iterable$.MODULE$.canBuildFrom());
|
||||
return prepend(info, flatMap(parents.toSeq(), MixinClassGenerator::allParents));
|
||||
}
|
||||
|
||||
private static <A, B> Seq<B> map(Seq<A> values, Function<A, B> function) {
|
||||
return (Seq<B>) ((TraversableLike) values).map(fn(function), Seq$.MODULE$.canBuildFrom());
|
||||
}
|
||||
|
||||
private static <A, B> Seq<B> flatMap(Seq<A> values, Function<A, GenTraversableOnce<B>> function) {
|
||||
return (Seq<B>) ((TraversableLike) values).flatMap(fn(function), Seq$.MODULE$.canBuildFrom());
|
||||
}
|
||||
|
||||
private static <A> Seq<A> prepend(A value, Seq<A> values) {
|
||||
return (Seq<A>) ((scala.collection.SeqLike) values).$plus$colon(value, Seq$.MODULE$.canBuildFrom());
|
||||
}
|
||||
|
||||
private static <A, B> Function1<A, B> fn(Function<A, B> function) {
|
||||
return new AbstractFunction1<A, B>() {
|
||||
|
||||
@Override
|
||||
public B apply(A value) {
|
||||
return function.apply(value);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user