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