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