From 40aea531e1746e230e36b158c7220933a9490bd1 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Thu, 25 May 2023 17:27:03 +0200 Subject: [PATCH] [IR] Drop `treatFloatInSpecialWay` interpreter's parameter Replaced it with more common `platform` parameter. --- .../kotlin/fir/backend/Fir2IrConverter.kt | 2 +- .../kotlin/ir/interpreter/CallInterceptor.kt | 3 ++- .../ir/interpreter/IrInterpreterConfiguration.kt | 6 ++++-- .../ir/interpreter/IrInterpreterEnvironment.kt | 14 +++++++------- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt index 52ba3cb01ee..6766e36ac27 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt @@ -425,8 +425,8 @@ class Fir2IrConverter( val intrinsicConstEvaluation = languageVersionSettings?.supportsFeature(LanguageFeature.IntrinsicConstEvaluation) == true val configuration = IrInterpreterConfiguration( + platform = targetPlatform, printOnlyExceptionMessage = true, - treatFloatInSpecialWay = targetPlatform.isJs() ) val interpreter = IrInterpreter(IrInterpreterEnvironment(irModuleFragment.irBuiltins, configuration)) val mode = if (intrinsicConstEvaluation) EvaluationMode.ONLY_INTRINSIC_CONST else EvaluationMode.ONLY_BUILTINS diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/CallInterceptor.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/CallInterceptor.kt index 2d75e0fb32b..c073222d8b4 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/CallInterceptor.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/CallInterceptor.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.hasAnnotation import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.platform.isJs import java.lang.invoke.MethodHandle internal interface CallInterceptor { @@ -166,7 +167,7 @@ internal class DefaultCallInterceptor(override val interpreter: IrInterpreter) : } private fun interpretBuiltinFunction(signature: Signature): Any? { - if (environment.configuration.treatFloatInSpecialWay) { + if (environment.configuration.platform.isJs()) { if (signature.name == "toString") return signature.args[0].value.specialToStringForJs() if (signature.name == "toFloat") signature.name = "toDouble" signature.args.filter { it.type == "Float" }.forEach { diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterConfiguration.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterConfiguration.kt index 08afe1f16fc..5ca85d32985 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterConfiguration.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterConfiguration.kt @@ -5,20 +5,22 @@ package org.jetbrains.kotlin.ir.interpreter +import org.jetbrains.kotlin.platform.TargetPlatform + /** + * @param platform is used to apply unique platform rules. For example, for JS we must handle `Float` values in special way. * @param maxStack describes the maximum allowed call stack size * @param maxCommands describes the maximum allowed number of simple instructions performed * @param createNonCompileTimeObjects * 'true' - interpreter will construct object and initialize its properties despite the fact it is not marked as compile time; * 'false' - interpreter will create a representation of empty object, that can be used to get const properties - * @param treatFloatInSpecialWay interpret float const as if it was a double and avoid `toFloat` evaluation if possible */ // TODO maybe create some sort of builder data class IrInterpreterConfiguration( + val platform: TargetPlatform? = null, val maxStack: Int = 10_000, val maxCommands: Int = 1_000_000, val createNonCompileTimeObjects: Boolean = false, val printOnlyExceptionMessage: Boolean = false, val collapseStackTraceFromJDK: Boolean = true, - val treatFloatInSpecialWay: Boolean = false, ) diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterEnvironment.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterEnvironment.kt index a5cb502b6e7..f6dc8265d73 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterEnvironment.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrInterpreterEnvironment.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.defaultType import org.jetbrains.kotlin.ir.util.isSubclassOf import org.jetbrains.kotlin.ir.util.properties +import org.jetbrains.kotlin.platform.isJs class IrInterpreterEnvironment( val irBuiltIns: IrBuiltIns, @@ -117,13 +118,12 @@ class IrInterpreterEnvironment( val end = original.endOffset val type = original.type.makeNotNull() return when (state) { - is Primitive<*> -> - when { - configuration.treatFloatInSpecialWay && state.value is Float -> IrConstImpl.float(start, end, type, state.value) - configuration.treatFloatInSpecialWay && state.value is Double -> IrConstImpl.double(start, end, type, state.value) - state.value == null || type.isPrimitiveType() || type.isString() -> state.value.toIrConst(type, start, end) - else -> original // TODO support for arrays - } + is Primitive<*> -> when { + configuration.platform.isJs() && state.value is Float -> IrConstImpl.float(start, end, type, state.value) + configuration.platform.isJs() && state.value is Double -> IrConstImpl.double(start, end, type, state.value) + state.value == null || type.isPrimitiveType() || type.isString() -> state.value.toIrConst(type, start, end) + else -> original // TODO support for arrays + } is ExceptionState -> { val message = if (configuration.printOnlyExceptionMessage) state.getShortDescription() else "\n" + state.getFullDescription() IrErrorExpressionImpl(original.startOffset, original.endOffset, original.type, message)