001/*
002 * Copyright (c) 2013, 2014, 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.asm.amd64.test;
024
025import com.oracle.graal.asm.amd64.*;
026import com.oracle.graal.asm.test.*;
027
028import static org.junit.Assume.*;
029
030import java.nio.*;
031
032import jdk.internal.jvmci.amd64.*;
033import jdk.internal.jvmci.code.*;
034import jdk.internal.jvmci.code.CompilationResult.*;
035import jdk.internal.jvmci.code.DataSection.*;
036import jdk.internal.jvmci.meta.*;
037
038import org.junit.*;
039
040public class SimpleAssemblerTest extends AssemblerTest {
041
042    @Before
043    public void checkAMD64() {
044        assumeTrue("skipping AMD64 specific test", codeCache.getTarget().arch instanceof AMD64);
045    }
046
047    @Test
048    public void intTest() {
049        CodeGenTest test = new CodeGenTest() {
050
051            @Override
052            public byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc) {
053                AMD64Assembler asm = new AMD64Assembler(target, registerConfig);
054                Register ret = registerConfig.getReturnRegister(Kind.Int);
055                asm.movl(ret, 8472);
056                asm.ret(0);
057                return asm.close(true);
058            }
059        };
060        assertReturn("intStub", test, 8472);
061    }
062
063    @Test
064    public void doubleTest() {
065        CodeGenTest test = new CodeGenTest() {
066
067            @Override
068            public byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc) {
069                AMD64MacroAssembler asm = new AMD64MacroAssembler(target, registerConfig);
070                Register ret = registerConfig.getReturnRegister(Kind.Double);
071                Data data = new Data(8, 8, DataBuilder.serializable(JavaConstant.forDouble(84.72)));
072                DataSectionReference ref = compResult.getDataSection().insertData(data);
073                compResult.recordDataPatch(asm.position(), ref);
074                asm.movdbl(ret, asm.getPlaceholder());
075                asm.ret(0);
076                return asm.close(true);
077            }
078        };
079        assertReturn("doubleStub", test, 84.72);
080    }
081
082    @Test
083    public void rawDoubleTest() {
084        CodeGenTest test = new CodeGenTest() {
085
086            @Override
087            public byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc) {
088                AMD64MacroAssembler asm = new AMD64MacroAssembler(target, registerConfig);
089                Register ret = registerConfig.getReturnRegister(Kind.Double);
090
091                byte[] rawBytes = new byte[8];
092                ByteBuffer.wrap(rawBytes).order(ByteOrder.nativeOrder()).putDouble(84.72);
093                Data data = new Data(8, 8, DataBuilder.raw(rawBytes));
094                DataSectionReference ref = compResult.getDataSection().insertData(data);
095                compResult.recordDataPatch(asm.position(), ref);
096                asm.movdbl(ret, asm.getPlaceholder());
097                asm.ret(0);
098                return asm.close(true);
099            }
100        };
101        assertReturn("doubleStub", test, 84.72);
102    }
103
104    public static int intStub() {
105        return 0;
106    }
107
108    public static double doubleStub() {
109        return 0.0;
110    }
111}