-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some cleanups and refactors to visitor trait.
- Loading branch information
Showing
17 changed files
with
90 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package lexi | ||
|
||
trait Visitor[T] { | ||
def visit(tree: Tree): T | ||
} |
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 |
---|---|---|
@@ -1,30 +1,27 @@ | ||
package lexi.backends.asm.ir | ||
|
||
import lexi.ir.{IrClass, IrTree} | ||
import lexi.{Tree, Visitor} | ||
import lexi.ir.IrClass | ||
import org.objectweb.asm.{ClassWriter, Opcodes} | ||
|
||
object IrClassVisitor extends IrVisitor[ClassWriter] { | ||
override def visit(ir: IrTree): ClassWriter = | ||
(((_: IrClass) => new ClassWriter(ClassWriter.COMPUTE_MAXS)) | ||
andThen ((writer) => { | ||
writer.visit( | ||
Opcodes.V1_8, | ||
Opcodes.ACC_PUBLIC, | ||
ir.asInstanceOf[IrClass].name.get, | ||
null, | ||
"java/lang/Object", | ||
null | ||
) | ||
ir.asInstanceOf[IrClass] | ||
.classBody | ||
.get | ||
.declarations | ||
.get | ||
.map(_.functionDeclaration) | ||
.filter(_ != None) | ||
.map(_.get) | ||
.foreach(IrMethodVisitor(writer).visit(_)) | ||
writer.visitEnd() | ||
writer | ||
}))(ir.asInstanceOf[IrClass]) | ||
object IrClassVisitor extends Visitor[ClassWriter] { | ||
override def visit(ir: Tree): ClassWriter = { | ||
val irClass = ir.asInstanceOf[IrClass] | ||
new ClassWriter(ClassWriter.COMPUTE_MAXS) { | ||
this.visit( | ||
Opcodes.V1_8, | ||
Opcodes.ACC_PUBLIC, | ||
ir.asInstanceOf[IrClass].name.get, | ||
null, | ||
"java/lang/Object", | ||
null | ||
) | ||
irClass.classBody.get.declarations.get | ||
.map(_.functionDeclaration) | ||
.filter(_ != None) | ||
.map(_.get) | ||
.foreach(IrMethodVisitor(this).visit(_)) | ||
visitEnd() | ||
} | ||
} | ||
} |
65 changes: 32 additions & 33 deletions
65
src/main/scala/lexi/backends/asm/ir/IrMethodVisitor.scala
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 |
---|---|---|
@@ -1,43 +1,42 @@ | ||
package lexi.backends.asm.ir | ||
|
||
import lexi.ir.{IrFunction, IrTree} | ||
import lexi.{Tree, Visitor} | ||
import lexi.ir.IrFunction | ||
import org.objectweb.asm.{ClassWriter, MethodVisitor, Opcodes} | ||
|
||
object IrMethodVisitor { | ||
def apply(classWriter: ClassWriter): IrMethodVisitor = | ||
new IrMethodVisitor(classWriter) | ||
} | ||
|
||
class IrMethodVisitor(classWriter: ClassWriter) extends IrVisitor[MethodVisitor] { | ||
override def visit(ir: IrTree): MethodVisitor = | ||
( | ||
(function: IrFunction) => { | ||
val method = classWriter.visitMethod( | ||
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, | ||
function.name.get, | ||
s"([Ljava/lang/${function.`type`};)V", | ||
null, | ||
null | ||
) | ||
method.visitCode() | ||
method.visitFieldInsn( | ||
Opcodes.GETSTATIC, | ||
"java/lang/System", | ||
"out", | ||
"Ljava/io/PrintStream;" | ||
) | ||
method.visitLdcInsn("Class compiled!") | ||
method.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/io/PrintStream", | ||
"println", | ||
"(Ljava/lang/String;)V", | ||
false | ||
) | ||
method.visitInsn(Opcodes.RETURN) | ||
method.visitMaxs(0, 0) | ||
method.visitEnd() | ||
method | ||
} | ||
)(ir.asInstanceOf[IrFunction]) | ||
class IrMethodVisitor(classWriter: ClassWriter) extends Visitor[MethodVisitor] { | ||
override def visit(ir: Tree): MethodVisitor = { | ||
val irFunction = ir.asInstanceOf[IrFunction] | ||
val method = classWriter.visitMethod( | ||
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, | ||
irFunction.name.get, | ||
s"([Ljava/lang/${irFunction.`type`};)V", | ||
null, | ||
null | ||
) | ||
method.visitCode() | ||
method.visitFieldInsn( | ||
Opcodes.GETSTATIC, | ||
"java/lang/System", | ||
"out", | ||
"Ljava/io/PrintStream;" | ||
) | ||
method.visitLdcInsn("Class compiled!") | ||
method.visitMethodInsn( | ||
Opcodes.INVOKEVIRTUAL, | ||
"java/io/PrintStream", | ||
"println", | ||
"(Ljava/lang/String;)V", | ||
false | ||
) | ||
method.visitInsn(Opcodes.RETURN) | ||
method.visitMaxs(0, 0) | ||
method.visitEnd() | ||
method | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
package lexi.frontends.kotlin.phases | ||
|
||
import lexi.{Phase, Tree, Visitor} | ||
import lexi.frontends.kotlin.transformations.ir.{KtClassVisitor, KtFileVisitor, KtFunctionVisitor, KtPropertyVisitor} | ||
import lexi.frontends.kotlin.transformations.ir.{ | ||
KtClassVisitor, | ||
KtFileVisitor, | ||
KtFunctionVisitor, | ||
KtPropertyVisitor | ||
} | ||
import lexi.frontends.kotlin.{AST, KtClass, KtFile, KtFunction, KtProperty} | ||
import lexi.ir.IrTree | ||
|
||
object Ir extends Phase { | ||
def apply(tree: Tree): Tree = | ||
visitorType(tree).visit(tree) | ||
|
||
private def visitorType(tree: Tree): Visitor = tree match { | ||
def apply(tree: Tree): Tree = tree.match { | ||
case _: KtFile => KtFileVisitor | ||
case _: KtClass => KtClassVisitor | ||
case _: KtFunction => KtFunctionVisitor | ||
case _: KtProperty => KtPropertyVisitor | ||
} | ||
}.visit(tree) | ||
} |
9 changes: 5 additions & 4 deletions
9
src/main/scala/lexi/frontends/kotlin/transformations/ir/KtBlockVisitor.scala
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package lexi.frontends.kotlin.transformations.ir | ||
|
||
import lexi.{Tree, Visitor} | ||
import lexi.frontends.kotlin.KtBlock | ||
import lexi.ir.IrBlock | ||
import lexi.{Tree, Visitor} | ||
|
||
object KtBlockVisitor extends Visitor { | ||
override def visit(ast: Tree): Tree = { | ||
object KtBlockVisitor extends Visitor[IrBlock] { | ||
override def visit(ast: Tree): IrBlock = { | ||
val ktBlock = ast.asInstanceOf[KtBlock] | ||
new lexi.ir.IrTree {} | ||
new lexi.ir.IrBlock {} | ||
} | ||
} |
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
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