src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core/src/org/graalvm/compiler/core/target/Backend.java
changeset 48861 47f19ff9903c
parent 47798 9fe9292f5931
child 49451 e06f9607f370
equal deleted inserted replaced
48860:5bce1b7e7800 48861:47f19ff9903c
    22  */
    22  */
    23 package org.graalvm.compiler.core.target;
    23 package org.graalvm.compiler.core.target;
    24 
    24 
    25 import java.util.ArrayList;
    25 import java.util.ArrayList;
    26 
    26 
       
    27 import org.graalvm.collections.EconomicSet;
    27 import org.graalvm.compiler.asm.Assembler;
    28 import org.graalvm.compiler.asm.Assembler;
    28 import org.graalvm.compiler.code.CompilationResult;
    29 import org.graalvm.compiler.code.CompilationResult;
    29 import org.graalvm.compiler.core.common.CompilationIdentifier;
    30 import org.graalvm.compiler.core.common.CompilationIdentifier;
    30 import org.graalvm.compiler.core.common.LIRKind;
    31 import org.graalvm.compiler.core.common.LIRKind;
    31 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
    32 import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
    42 import org.graalvm.compiler.nodes.StructuredGraph;
    43 import org.graalvm.compiler.nodes.StructuredGraph;
    43 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
    44 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
    44 import org.graalvm.compiler.phases.tiers.SuitesProvider;
    45 import org.graalvm.compiler.phases.tiers.SuitesProvider;
    45 import org.graalvm.compiler.phases.tiers.TargetProvider;
    46 import org.graalvm.compiler.phases.tiers.TargetProvider;
    46 import org.graalvm.compiler.phases.util.Providers;
    47 import org.graalvm.compiler.phases.util.Providers;
    47 import org.graalvm.util.EconomicSet;
       
    48 
    48 
    49 import jdk.vm.ci.code.BailoutException;
    49 import jdk.vm.ci.code.BailoutException;
    50 import jdk.vm.ci.code.CodeCacheProvider;
    50 import jdk.vm.ci.code.CodeCacheProvider;
    51 import jdk.vm.ci.code.CompilationRequest;
    51 import jdk.vm.ci.code.CompilationRequest;
    52 import jdk.vm.ci.code.CompiledCode;
    52 import jdk.vm.ci.code.CompiledCode;
   202                 tasks[i] = codeInstallationTaskFactories.get(i).create();
   202                 tasks[i] = codeInstallationTaskFactories.get(i).create();
   203             }
   203             }
   204         }
   204         }
   205         try (DebugContext.Scope s2 = debug.scope("CodeInstall", debugContext);
   205         try (DebugContext.Scope s2 = debug.scope("CodeInstall", debugContext);
   206                         DebugContext.Activation a = debug.activate()) {
   206                         DebugContext.Activation a = debug.activate()) {
   207             for (CodeInstallationTask task : tasks) {
   207             preCodeInstallationTasks(tasks, compilationResult);
   208                 task.preProcess(compilationResult);
   208 
   209             }
   209             InstalledCode installedCode;
   210 
       
   211             CompiledCode compiledCode = createCompiledCode(method, compilationRequest, compilationResult);
       
   212             InstalledCode installedCode = getProviders().getCodeCache().installCode(method, compiledCode, predefinedInstalledCode, speculationLog, isDefault);
       
   213 
       
   214             // Run post-code installation tasks.
       
   215             try {
   210             try {
   216                 for (CodeInstallationTask task : tasks) {
   211                 CompiledCode compiledCode = createCompiledCode(method, compilationRequest, compilationResult);
   217                     task.postProcess(installedCode);
   212                 installedCode = getProviders().getCodeCache().installCode(method, compiledCode, predefinedInstalledCode, speculationLog, isDefault);
   218                 }
       
   219                 for (CodeInstallationTask task : tasks) {
       
   220                     task.releaseInstallation(installedCode);
       
   221                 }
       
   222             } catch (Throwable t) {
   213             } catch (Throwable t) {
   223                 installedCode.invalidate();
   214                 failCodeInstallationTasks(tasks, t);
   224                 throw t;
   215                 throw t;
   225             }
   216             }
       
   217 
       
   218             postCodeInstallationTasks(tasks, installedCode);
       
   219 
   226             return installedCode;
   220             return installedCode;
   227         } catch (Throwable e) {
   221         } catch (Throwable e) {
   228             throw debug.handle(e);
   222             throw debug.handle(e);
       
   223         }
       
   224     }
       
   225 
       
   226     private static void failCodeInstallationTasks(CodeInstallationTask[] tasks, Throwable t) {
       
   227         for (CodeInstallationTask task : tasks) {
       
   228             task.installFailed(t);
       
   229         }
       
   230     }
       
   231 
       
   232     private static void preCodeInstallationTasks(CodeInstallationTask[] tasks, CompilationResult compilationResult) {
       
   233         for (CodeInstallationTask task : tasks) {
       
   234             task.preProcess(compilationResult);
       
   235         }
       
   236     }
       
   237 
       
   238     private static void postCodeInstallationTasks(CodeInstallationTask[] tasks, InstalledCode installedCode) {
       
   239         try {
       
   240             for (CodeInstallationTask task : tasks) {
       
   241                 task.postProcess(installedCode);
       
   242             }
       
   243         } catch (Throwable t) {
       
   244             installedCode.invalidate();
       
   245             throw t;
   229         }
   246         }
   230     }
   247     }
   231 
   248 
   232     /**
   249     /**
   233      * Installs code based on a given compilation result.
   250      * Installs code based on a given compilation result.
   299         @SuppressWarnings("unused")
   316         @SuppressWarnings("unused")
   300         public void postProcess(InstalledCode installedCode) {
   317         public void postProcess(InstalledCode installedCode) {
   301         }
   318         }
   302 
   319 
   303         /**
   320         /**
   304          * Task to run after all the post-code installation tasks are complete, used to release the
   321          * Invoked after {@link #preProcess} when code installation fails.
   305          * installed code.
       
   306          */
   322          */
   307         @SuppressWarnings("unused")
   323         @SuppressWarnings("unused")
   308         public void releaseInstallation(InstalledCode installedCode) {
   324         public void installFailed(Throwable t) {
   309         }
   325         }
   310     }
   326     }
   311 
   327 
   312     /**
   328     /**
   313      * Creates code installation tasks.
   329      * Creates code installation tasks.