1/*
2 * Copyright (c) 2013, 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 */
23package org.graalvm.compiler.asm.amd64.test;
24
25import static org.junit.Assume.assumeTrue;
26
27import java.nio.ByteBuffer;
28import java.nio.ByteOrder;
29
30import org.graalvm.compiler.asm.amd64.AMD64Assembler;
31import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
32import org.graalvm.compiler.asm.test.AssemblerTest;
33import org.graalvm.compiler.code.CompilationResult;
34import org.graalvm.compiler.code.DataSection.Data;
35import org.graalvm.compiler.code.DataSection.RawData;
36import org.graalvm.compiler.code.DataSection.SerializableData;
37import org.junit.Before;
38import org.junit.Test;
39
40import jdk.vm.ci.amd64.AMD64;
41import jdk.vm.ci.code.CallingConvention;
42import jdk.vm.ci.code.Register;
43import jdk.vm.ci.code.RegisterConfig;
44import jdk.vm.ci.code.TargetDescription;
45import jdk.vm.ci.code.site.DataSectionReference;
46import jdk.vm.ci.meta.JavaConstant;
47import jdk.vm.ci.meta.JavaKind;
48
49public class SimpleAssemblerTest extends AssemblerTest {
50
51    @Before
52    public void checkAMD64() {
53        assumeTrue("skipping AMD64 specific test", codeCache.getTarget().arch instanceof AMD64);
54    }
55
56    @Test
57    public void intTest() {
58        CodeGenTest test = new CodeGenTest() {
59
60            @Override
61            public byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc) {
62                AMD64Assembler asm = new AMD64Assembler(target);
63                Register ret = registerConfig.getReturnRegister(JavaKind.Int);
64                asm.movl(ret, 8472);
65                asm.ret(0);
66                return asm.close(true);
67            }
68        };
69        assertReturn("intStub", test, 8472);
70    }
71
72    @Test
73    public void doubleTest() {
74        CodeGenTest test = new CodeGenTest() {
75
76            @Override
77            public byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc) {
78                AMD64MacroAssembler asm = new AMD64MacroAssembler(target);
79                Register ret = registerConfig.getReturnRegister(JavaKind.Double);
80                Data data = new SerializableData(JavaConstant.forDouble(84.72), 8);
81                DataSectionReference ref = compResult.getDataSection().insertData(data);
82                compResult.recordDataPatch(asm.position(), ref);
83                asm.movdbl(ret, asm.getPlaceholder(-1));
84                asm.ret(0);
85                return asm.close(true);
86            }
87        };
88        assertReturn("doubleStub", test, 84.72);
89    }
90
91    @Test
92    public void rawDoubleTest() {
93        CodeGenTest test = new CodeGenTest() {
94
95            @Override
96            public byte[] generateCode(CompilationResult compResult, TargetDescription target, RegisterConfig registerConfig, CallingConvention cc) {
97                AMD64MacroAssembler asm = new AMD64MacroAssembler(target);
98                Register ret = registerConfig.getReturnRegister(JavaKind.Double);
99
100                byte[] rawBytes = new byte[8];
101                ByteBuffer.wrap(rawBytes).order(ByteOrder.nativeOrder()).putDouble(84.72);
102                Data data = new RawData(rawBytes, 8);
103                DataSectionReference ref = compResult.getDataSection().insertData(data);
104                compResult.recordDataPatch(asm.position(), ref);
105                asm.movdbl(ret, asm.getPlaceholder(-1));
106                asm.ret(0);
107                return asm.close(true);
108            }
109        };
110        assertReturn("doubleStub", test, 84.72);
111    }
112
113    public static int intStub() {
114        return 0;
115    }
116
117    public static double doubleStub() {
118        return 0.0;
119    }
120}
121