1 /*
   2  * Copyright (c) 2013, 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.hotspot.amd64.test;
  26 
  27 import static jdk.vm.ci.amd64.AMD64.rax;
  28 
  29 import java.util.Arrays;
  30 
  31 import org.junit.Assert;
  32 import org.junit.Ignore;
  33 import org.junit.Test;
  34 
  35 import org.graalvm.compiler.asm.amd64.AMD64Assembler;
  36 import org.graalvm.compiler.core.test.GraalCompilerTest;
  37 
  38 import jdk.vm.ci.code.InstalledCode;
  39 import jdk.vm.ci.code.Register;
  40 import jdk.vm.ci.code.RegisterArray;
  41 import jdk.vm.ci.code.TargetDescription;
  42 import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
  43 import jdk.vm.ci.meta.JavaKind;
  44 import jdk.vm.ci.meta.ResolvedJavaMethod;
  45 
  46 /**
  47  * Ensures that frame omission works in cases where it is expected to.
  48  */
  49 public class AMD64HotSpotFrameOmissionTest extends GraalCompilerTest {
  50 
  51     interface CodeGenerator {
  52 
  53         void generateCode(AMD64Assembler asm);
  54     }
  55 
  56     public static void test1snippet() {
  57         return;
  58     }
  59 
  60     @Ignore
  61     @Test
  62     public void test1() {
  63         testHelper("test1snippet", new CodeGenerator() {
  64 
  65             @Override
  66             public void generateCode(AMD64Assembler asm) {
  67                 asm.nop(5); // padding for mt-safe patching
  68                 asm.ret(0);
  69             }
  70         });
  71     }
  72 
  73     public static int test2snippet(int x) {
  74         return x + 5;
  75     }
  76 
  77     @Ignore
  78     @Test
  79     public void test2() {
  80         testHelper("test2snippet", new CodeGenerator() {
  81 
  82             @Override
  83             public void generateCode(AMD64Assembler asm) {
  84                 Register arg = getArgumentRegister(0, JavaKind.Int);
  85                 asm.nop(5); // padding for mt-safe patching
  86                 asm.addl(arg, 5);
  87                 asm.movl(rax, arg);
  88                 asm.ret(0);
  89             }
  90         });
  91     }
  92 
  93     public static long test3snippet(long x) {
  94         return 1 + x;
  95     }
  96 
  97     @Ignore
  98     @Test
  99     public void test3() {
 100         testHelper("test3snippet", new CodeGenerator() {
 101 
 102             @Override
 103             public void generateCode(AMD64Assembler asm) {
 104                 Register arg = getArgumentRegister(0, JavaKind.Long);
 105                 asm.nop(5); // padding for mt-safe patching
 106                 asm.addq(arg, 1);
 107                 asm.movq(rax, arg);
 108                 asm.ret(0);
 109             }
 110         });
 111     }
 112 
 113     private void testHelper(String name, CodeGenerator gen) {
 114         ResolvedJavaMethod javaMethod = getResolvedJavaMethod(name);
 115         InstalledCode installedCode = getCode(javaMethod);
 116 
 117         TargetDescription target = getCodeCache().getTarget();
 118         AMD64Assembler asm = new AMD64Assembler(target);
 119 
 120         gen.generateCode(asm);
 121         byte[] expectedCode = asm.close(true);
 122 
 123         // Only compare up to expectedCode.length bytes to ignore
 124         // padding instructions adding during code installation
 125         byte[] actualCode = Arrays.copyOf(installedCode.getCode(), expectedCode.length);
 126 
 127         Assert.assertArrayEquals(expectedCode, actualCode);
 128     }
 129 
 130     private Register getArgumentRegister(int index, JavaKind kind) {
 131         RegisterArray regs = getCodeCache().getRegisterConfig().getCallingConventionRegisters(HotSpotCallingConventionType.JavaCall, kind);
 132         return regs.get(index);
 133     }
 134 }