Optimize constant pooling

This commit is contained in:
Iaroslav 2020-06-22 04:05:52 +07:00
parent 8adfef22a2
commit e99f7ad360
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
3 changed files with 12 additions and 3 deletions

View File

@ -19,11 +19,11 @@ fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<
when (node) {
is MST.Symbolic -> loadVariable(node.value)
is MST.Numeric -> {
val constant = if (algebra is NumericAlgebra<T>) {
val constant = if (algebra is NumericAlgebra<T>)
algebra.number(node.value)
} else {
else
error("Number literals are not supported in $algebra")
}
loadTConstant(constant)
}
is MST.Unary -> {

View File

@ -260,6 +260,7 @@ internal class AsmBuilder<T> internal constructor(
is Int -> invokeMethodVisitor.visitLdcOrIntConstant(value)
is Double -> invokeMethodVisitor.visitLdcOrDoubleConstant(value)
is Float -> invokeMethodVisitor.visitLdcOrFloatConstant(value)
is Long -> invokeMethodVisitor.visitLdcOrLongConstant(value)
else -> invokeMethodVisitor.visitLdcInsn(value)
}

View File

@ -11,6 +11,8 @@ internal fun MethodVisitor.visitLdcOrIntConstant(value: Int): Unit = when (value
3 -> visitInsn(ICONST_3)
4 -> visitInsn(ICONST_4)
5 -> visitInsn(ICONST_5)
in -128..127 -> visitIntInsn(BIPUSH, value)
in -32768..32767 -> visitIntInsn(SIPUSH, value)
else -> visitLdcInsn(value)
}
@ -20,6 +22,12 @@ internal fun MethodVisitor.visitLdcOrDoubleConstant(value: Double): Unit = when
else -> visitLdcInsn(value)
}
internal fun MethodVisitor.visitLdcOrLongConstant(value: Long): Unit = when (value) {
0L -> visitInsn(LCONST_0)
1L -> visitInsn(LCONST_1)
else -> visitLdcInsn(value)
}
internal fun MethodVisitor.visitLdcOrFloatConstant(value: Float): Unit = when (value) {
0f -> visitInsn(FCONST_0)
1f -> visitInsn(FCONST_1)