Optimize constant pooling #112

Closed
CommanderTvis wants to merge 3 commits from adv-expr-lconst into adv-expr
3 changed files with 14 additions and 3 deletions

View File

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

View File

@ -257,9 +257,12 @@ internal class AsmBuilder<T> internal constructor(
if (sigLetter != null) { if (sigLetter != null) {
when (value) { when (value) {
is Byte -> invokeMethodVisitor.visitLdcOrIntConstant(value.toInt())
is Short -> invokeMethodVisitor.visitLdcOrIntConstant(value.toInt())
is Int -> invokeMethodVisitor.visitLdcOrIntConstant(value) is Int -> invokeMethodVisitor.visitLdcOrIntConstant(value)
is Double -> invokeMethodVisitor.visitLdcOrDoubleConstant(value) is Double -> invokeMethodVisitor.visitLdcOrDoubleConstant(value)
is Float -> invokeMethodVisitor.visitLdcOrFloatConstant(value) is Float -> invokeMethodVisitor.visitLdcOrFloatConstant(value)
is Long -> invokeMethodVisitor.visitLdcOrLongConstant(value)
else -> invokeMethodVisitor.visitLdcInsn(value) else -> invokeMethodVisitor.visitLdcInsn(value)
} }

View File

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