-
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.
Ahhh!!!
- Loading branch information
Showing
10 changed files
with
79 additions
and
40 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
52 changes: 52 additions & 0 deletions
52
jit-compiler/src/main/java/org/aya/compiler/free/morphism/free/FreeOptimizer.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,52 @@ | ||
// 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.morphism.free; | ||
|
||
import kala.collection.immutable.ImmutableSeq; | ||
import org.aya.compiler.free.morphism.free.FreeDecl.Clazz; | ||
import org.aya.compiler.free.morphism.free.FreeDecl.ConstantField; | ||
import org.aya.compiler.free.morphism.free.FreeDecl.Method; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface FreeOptimizer { | ||
static @NotNull Clazz optimizeClass(Clazz clazz) { | ||
return (Clazz) optimize(clazz); | ||
} | ||
|
||
static @NotNull FreeDecl optimize(FreeDecl decl) { | ||
return switch (decl) { | ||
case Clazz(var metadata, var owner, var nested, var superclass, var members) -> { | ||
var newMembers = members.map(FreeOptimizer::optimize); | ||
yield new Clazz(metadata, owner, nested, superclass, newMembers); | ||
} | ||
case ConstantField field -> field; | ||
case Method(var signature, var body) -> new Method(signature, body.flatMap(FreeOptimizer::optimize)); | ||
}; | ||
} | ||
|
||
static @NotNull ImmutableSeq<FreeStmt> optimize(FreeStmt stmt) { | ||
return switch (stmt) { | ||
case FreeStmt.Switch(var elim, var cases, var branch, var defaultCase) -> { | ||
branch = branch.map(it -> it.flatMap(FreeOptimizer::optimize)); | ||
defaultCase = defaultCase.flatMap(FreeOptimizer::optimize); | ||
if (branch.isEmpty()) yield defaultCase; | ||
if (defaultCase.sizeEquals(1) && defaultCase.getFirst() == FreeStmt.Unreachable.INSTANCE) { | ||
if (branch.sizeEquals(1)) { | ||
yield branch.getFirst(); | ||
} else if (branch.sizeGreaterThan(1)) { | ||
yield ImmutableSeq.of(new FreeStmt.Switch(elim, cases.dropLast(1), branch.dropLast(1), branch.getLast())); | ||
} | ||
} | ||
yield ImmutableSeq.of(new FreeStmt.Switch(elim, cases, branch, defaultCase)); | ||
} | ||
case FreeStmt.Breakable(var stmts) -> | ||
ImmutableSeq.of(new FreeStmt.Breakable(stmts.flatMap(FreeOptimizer::optimize))); | ||
case FreeStmt.IfThenElse(var cond, var thenBlock, var elseBlock) -> { | ||
var newThenBlock = thenBlock.flatMap(FreeOptimizer::optimize); | ||
var newElseBlock = elseBlock == null ? null : elseBlock.flatMap(FreeOptimizer::optimize); | ||
yield ImmutableSeq.of(new FreeStmt.IfThenElse(cond, newThenBlock, newElseBlock)); | ||
} | ||
default -> ImmutableSeq.of(stmt); | ||
}; | ||
} | ||
} |
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