1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.lir.jtt;
  26 
  27 import org.graalvm.compiler.api.directives.GraalDirectives;
  28 import org.graalvm.compiler.core.common.PermanentBailoutException;
  29 import org.graalvm.compiler.debug.DebugContext;
  30 import org.graalvm.compiler.debug.DebugContext.Scope;
  31 import org.graalvm.compiler.debug.GraalError;
  32 import org.graalvm.compiler.lir.LIRInstruction;
  33 import org.graalvm.compiler.lir.LIRInstructionClass;
  34 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  35 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  38 import org.junit.Assert;
  39 import org.junit.Assume;
  40 import org.junit.Test;
  41 
  42 import jdk.vm.ci.code.BailoutException;
  43 import jdk.vm.ci.meta.ResolvedJavaMethod;
  44 import jdk.vm.ci.meta.Value;
  45 import jdk.vm.ci.sparc.SPARC;
  46 
  47 /**
  48  * Tests the {@link BailoutException} thrown, when trying to compile huge methods, which have branch
  49  * displacements which does not fit into 19 bit signed.
  50  */
  51 public class SPARCBranchBailoutTest extends LIRTest {
  52     private static class BranchSpec extends LIRTestSpecification {
  53         private final int n;
  54 
  55         BranchSpec(int n) {
  56             super();
  57             this.n = n;
  58         }
  59 
  60         @Override
  61         public void generate(LIRGeneratorTool gen, Value a) {
  62             gen.append(new LargeOp(n));
  63             setResult(a);
  64         }
  65     }
  66 
  67     private static final BranchSpec spec = new BranchSpec(1 << 20);
  68 
  69     @LIRIntrinsic
  70     public static int branch(@SuppressWarnings("unused") BranchSpec s, int a) {
  71         return a;
  72     }
  73 
  74     public static int testBranch(int length) {
  75         int res = 1;
  76         if (length > 0) {
  77             res = branch(spec, 1);
  78         } else {
  79             res = branch(spec, 2);
  80         }
  81         return GraalDirectives.opaque(res);
  82     }
  83 
  84     @SuppressWarnings("try")
  85     @Test
  86     public void testBailoutOnBranchOverflow() throws Throwable {
  87         Assume.assumeTrue(getBackend().getTarget().arch instanceof SPARC);
  88         ResolvedJavaMethod m = getResolvedJavaMethod("testBranch");
  89         DebugContext debug = getDebugContext();
  90         try {
  91             try (Scope s = debug.disable()) {
  92                 StructuredGraph graph = parseEager(m, AllowAssumptions.YES, debug);
  93                 compile(m, graph);
  94             }
  95         } catch (GraalError e) {
  96             Assert.assertEquals(PermanentBailoutException.class, e.getCause().getClass());
  97         }
  98     }
  99 
 100     public static class LargeOp extends LIRInstruction {
 101         private static final LIRInstructionClass<LargeOp> TYPE = LIRInstructionClass.create(LargeOp.class);
 102         private final int n;
 103 
 104         public LargeOp(int n) {
 105             super(TYPE);
 106             this.n = n;
 107         }
 108 
 109         @Override
 110         public void emitCode(CompilationResultBuilder crb) {
 111             for (int i = 0; i < n; i++) {
 112                 crb.asm.emitInt(0);
 113             }
 114         }
 115     }
 116 }