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