001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.amd64.test;
024
025import static jdk.internal.jvmci.amd64.AMD64.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.code.*;
030import jdk.internal.jvmci.meta.*;
031
032import org.junit.*;
033
034import com.oracle.graal.asm.amd64.*;
035import com.oracle.graal.compiler.test.*;
036
037/**
038 * Ensures that frame omission works in cases where it is expected to.
039 */
040public class AMD64HotSpotFrameOmissionTest extends GraalCompilerTest {
041
042    interface CodeGenerator {
043
044        void generateCode(AMD64Assembler asm);
045    }
046
047    public static void test1snippet() {
048        return;
049    }
050
051    @Ignore
052    @Test
053    public void test1() {
054        testHelper("test1snippet", new CodeGenerator() {
055
056            @Override
057            public void generateCode(AMD64Assembler asm) {
058                asm.nop(5); // padding for mt-safe patching
059                asm.ret(0);
060            }
061        });
062    }
063
064    public static int test2snippet(int x) {
065        return x + 5;
066    }
067
068    @Ignore
069    @Test
070    public void test2() {
071        testHelper("test2snippet", new CodeGenerator() {
072
073            @Override
074            public void generateCode(AMD64Assembler asm) {
075                Register arg = getArgumentRegister(0, Kind.Int);
076                asm.nop(5); // padding for mt-safe patching
077                asm.addl(arg, 5);
078                asm.movl(rax, arg);
079                asm.ret(0);
080            }
081        });
082    }
083
084    public static long test3snippet(long x) {
085        return 1 + x;
086    }
087
088    @Ignore
089    @Test
090    public void test3() {
091        testHelper("test3snippet", new CodeGenerator() {
092
093            @Override
094            public void generateCode(AMD64Assembler asm) {
095                Register arg = getArgumentRegister(0, Kind.Long);
096                asm.nop(5); // padding for mt-safe patching
097                asm.addq(arg, 1);
098                asm.movq(rax, arg);
099                asm.ret(0);
100            }
101        });
102    }
103
104    private void testHelper(String name, CodeGenerator gen) {
105        ResolvedJavaMethod javaMethod = getResolvedJavaMethod(name);
106        InstalledCode installedCode = getCode(javaMethod);
107
108        TargetDescription target = getCodeCache().getTarget();
109        RegisterConfig registerConfig = getCodeCache().getRegisterConfig();
110        AMD64Assembler asm = new AMD64Assembler(target, registerConfig);
111
112        gen.generateCode(asm);
113        byte[] expectedCode = asm.close(true);
114
115        // Only compare up to expectedCode.length bytes to ignore
116        // padding instructions adding during code installation
117        byte[] actualCode = Arrays.copyOf(installedCode.getCode(), expectedCode.length);
118
119        Assert.assertArrayEquals(expectedCode, actualCode);
120    }
121
122    private Register getArgumentRegister(int index, Kind kind) {
123        Register[] regs = getCodeCache().getRegisterConfig().getCallingConventionRegisters(CallingConvention.Type.JavaCall, kind);
124        return regs[index];
125    }
126}