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.asm.BranchTargetOutOfBoundsException;
  29 import org.graalvm.compiler.debug.DebugContext;
  30 import org.graalvm.compiler.debug.DebugContext.Scope;
  31 import org.graalvm.compiler.lir.LIRInstruction;
  32 import org.graalvm.compiler.lir.LIRInstructionClass;
  33 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  34 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  35 import org.graalvm.compiler.nodes.StructuredGraph;
  36 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  37 import org.junit.Assume;
  38 import org.junit.Test;
  39 
  40 import jdk.vm.ci.code.BailoutException;
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 import jdk.vm.ci.meta.Value;
  43 import jdk.vm.ci.sparc.SPARC;
  44 
  45 /**
  46  * Tests the {@link BailoutException} thrown, when trying to compile huge methods, which have branch
  47  * displacements which does not fit into 19 bit signed.
  48  */
  49 public class SPARCBranchBailoutTest extends LIRTest {
  50     private static class BranchSpec extends LIRTestSpecification {
  51         private final int n;
  52 
  53         BranchSpec(int n) {
  54             super();
  55             this.n = n;
  56         }
  57 
  58         @Override
  59         public void generate(LIRGeneratorTool gen, Value a) {
  60             gen.append(new LargeOp(n));
  61             setResult(a);
  62         }
  63     }
  64 
  65     private static final BranchSpec spec = new BranchSpec(1 << 20);
  66 
  67     @LIRIntrinsic
  68     public static int branch(@SuppressWarnings("unused") BranchSpec s, int a) {
  69         return a;
  70     }
  71 
  72     public static int testBranch(int length) {
  73         int res = 1;
  74         if (length > 0) {
  75             res = branch(spec, 1);
  76         } else {
  77             res = branch(spec, 2);
  78         }
  79         return GraalDirectives.opaque(res);
  80     }
  81 
  82     @SuppressWarnings("try")
  83     @Test(expected = BranchTargetOutOfBoundsException.class)
  84     public void testBailoutOnBranchOverflow() throws Throwable {
  85         Assume.assumeTrue(getBackend().getTarget().arch instanceof SPARC);
  86         ResolvedJavaMethod m = getResolvedJavaMethod("testBranch");
  87         DebugContext debug = getDebugContext();
  88         try (Scope s = debug.disable()) {
  89             StructuredGraph graph = parseEager(m, AllowAssumptions.YES, debug);
  90             compile(m, graph);
  91         }
  92     }
  93 
  94     public static class LargeOp extends LIRInstruction {
  95         private static final LIRInstructionClass<LargeOp> TYPE = LIRInstructionClass.create(LargeOp.class);
  96         private final int n;
  97 
  98         public LargeOp(int n) {
  99             super(TYPE);
 100             this.n = n;
 101         }
 102 
 103         @Override
 104         public void emitCode(CompilationResultBuilder crb) {
 105             for (int i = 0; i < n; i++) {
 106                 crb.asm.emitInt(0);
 107             }
 108         }
 109     }
 110 }