Introduced IR CodegenFactory, added configuration key to enable it

This commit is contained in:
Michael Bogdanov
2016-09-20 10:27:25 +03:00
committed by Dmitry Petrov
parent 0c60b21cca
commit 978a4db07b
8 changed files with 73 additions and 14 deletions
+1
View File
@@ -13,5 +13,6 @@
<orderEntry type="module" module-name="serialization" />
<orderEntry type="module" module-name="backend-common" exported="" />
<orderEntry type="module" module-name="util" />
<orderEntry type="module" module-name="backend.jvm" />
</component>
</module>
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.codegen.state
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.ModificationTracker
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.codegen.`when`.MappingsClassesForWhenByEnum
@@ -69,7 +70,7 @@ class GenerationState @JvmOverloads constructor(
// TODO: get rid of it with the proper module infrastructure
val outDirectory: File? = null,
private val onIndependentPartCompilationEnd: GenerationStateEventCallback = GenerationStateEventCallback.DO_NOTHING,
val codegenFactory: CodegenFactory = DefaultCodegenFactory,
val codegenFactory: CodegenFactory = if (configuration.getBoolean(JVMConfigurationKeys.IR)) JvmIrCodegenFactory else DefaultCodegenFactory,
wantsDiagnostics: Boolean = true
) {
abstract class GenerateClassFilter {
@@ -82,4 +82,7 @@ public class JVMConfigurationKeys {
public static final CompilerConfigurationKey<List<String>> FRIEND_PATHS =
CompilerConfigurationKey.create("friend module paths");
public static final CompilerConfigurationKey<Boolean> IR =
CompilerConfigurationKey.create("IR");
}
@@ -54,7 +54,7 @@ object JvmBackendFacade {
}
}
private fun createJvmBackendContext(psi2ir: Psi2IrTranslator, state: GenerationState): JvmBackendContext {
fun createJvmBackendContext(psi2ir: Psi2IrTranslator, state: GenerationState): JvmBackendContext {
val jvmFileClassProvider = JvmFileClassProvider()
psi2ir.add(jvmFileClassProvider)
@@ -0,0 +1,53 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.backend.jvm
import org.jetbrains.kotlin.codegen.*
import org.jetbrains.kotlin.codegen.context.PackageContext
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile
object JvmIrCodegenFactory : CodegenFactory {
override fun createPackageCodegen(state: GenerationState, files: Collection<KtFile>, fqName: FqName, registry: PackagePartRegistry): PackageCodegen {
val impl = PackageCodegenImpl(state, files, fqName, registry)
// val psi2ir = Psi2IrTranslator()
// val psi2irContext = psi2ir.createGeneratorContext(state.module, state.bindingContext)
// val jvmBackendContext = JvmBackendFacade.createJvmBackendContext(psi2ir, state)
return object : PackageCodegen {
override fun generate(errorHandler: CompilationErrorHandler) {
JvmBackendFacade.doGenerateFiles(files, state, errorHandler)
}
override fun generateClassOrObject(classOrObject: KtClassOrObject, packagePartContext: PackageContext) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getPackageFragment(): PackageFragmentDescriptor {
return impl.packageFragment
}
}
}
override fun createMultifileClassCodegen(state: GenerationState, files: Collection<KtFile>, fqName: FqName, registry: PackagePartRegistry): MultifileClassCodegen {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
@@ -133,7 +133,11 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
configuration.put(JVMConfigurationKeys.JVM_TARGET, JvmTarget.JVM_1_8);
}
return getBytecodeForFile(ktFile, configuration, ir.isSelected());
if (ir.isSelected()) {
configuration.put(JVMConfigurationKeys.IR, true);
}
return getBytecodeForFile(ktFile, configuration);
}
@Override
@@ -241,10 +245,10 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
// public for tests
@NotNull
public static String getBytecodeForFile(@NotNull KtFile ktFile, @NotNull CompilerConfiguration configuration, boolean newIRGenerator) {
public static String getBytecodeForFile(@NotNull KtFile ktFile, @NotNull CompilerConfiguration configuration) {
GenerationState state;
try {
state = compileSingleFile(ktFile, configuration, newIRGenerator);
state = compileSingleFile(ktFile, configuration);
}
catch (ProcessCanceledException e) {
throw e;
@@ -284,8 +288,7 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
@NotNull
public static GenerationState compileSingleFile(
@NotNull final KtFile ktFile,
@NotNull CompilerConfiguration configuration,
boolean newIRGenerator
@NotNull CompilerConfiguration configuration
) {
ResolutionFacade resolutionFacade = ResolutionUtils.getResolutionFacade(ktFile);
@@ -324,11 +327,9 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
ktFile.getProject(), ClassBuilderFactories.TEST, resolutionFacade.getModuleDescriptor(), bindingContext, toProcess,
configuration, generateClassFilter
);
if (newIRGenerator) {
JvmBackendFacade.INSTANCE.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
} else {
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
}
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
return state;
}
@@ -72,7 +72,7 @@ class KotlinDecompilerServiceImpl : KotlinDecompilerService {
}
fun bytecodeMapForSourceFile(file: KtFile): Map<File, () -> ByteArray> {
val generationState = KotlinBytecodeToolWindow.compileSingleFile(file, CompilerConfiguration.EMPTY, false)
val generationState = KotlinBytecodeToolWindow.compileSingleFile(file, CompilerConfiguration.EMPTY)
val bytecodeMap = hashMapOf<File, () -> ByteArray>()
generationState.factory.asList().filter { FileUtilRt.extensionEquals(it.relativePath, "class") }.forEach {
@@ -44,7 +44,7 @@ abstract class AbstractBytecodeToolWindowTest: KotlinLightCodeInsightFixtureTest
if (InTextDirectivesUtils.getPrefixedBoolean(mainFileText, "// INLINE:") == false) {
configuration.put(CommonConfigurationKeys.DISABLE_INLINE, true)
}
val bytecodes = KotlinBytecodeToolWindow.getBytecodeForFile(file, configuration, false)
val bytecodes = KotlinBytecodeToolWindow.getBytecodeForFile(file, configuration)
assert(bytecodes.contains("// ================")) {
"The header \"// ================\" is missing.\n This means that there is an exception failed during compilation:\n$bytecodes"
}