1 /*
   2  * Copyright (c) 2015, 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.hotspot;
  24 
  25 import static jdk.vm.ci.hotspot.HotSpotCompressedNullConstant.COMPRESSED_NULL;
  26 
  27 import java.nio.ByteBuffer;
  28 
  29 import org.graalvm.compiler.code.DataSection.Data;
  30 import org.graalvm.compiler.code.DataSection.Patches;
  31 import org.graalvm.compiler.code.DataSection.SerializableData;
  32 import org.graalvm.compiler.code.DataSection.ZeroData;
  33 import org.graalvm.compiler.debug.GraalError;
  34 import org.graalvm.compiler.lir.asm.DataBuilder;
  35 
  36 import jdk.vm.ci.code.TargetDescription;
  37 import jdk.vm.ci.hotspot.HotSpotConstant;
  38 import jdk.vm.ci.meta.Constant;
  39 import jdk.vm.ci.meta.JavaConstant;
  40 import jdk.vm.ci.meta.SerializableConstant;
  41 import jdk.vm.ci.meta.VMConstant;
  42 
  43 public class HotSpotDataBuilder extends DataBuilder {
  44 
  45     private final TargetDescription target;
  46 
  47     public HotSpotDataBuilder(TargetDescription target) {
  48         this.target = target;
  49     }
  50 
  51     @Override
  52     public boolean needDetailedPatchingInformation() {
  53         /* The HotSpot VM finds operands that need patching by decoding the instruction. */
  54         return false;
  55     }
  56 
  57     @Override
  58     public Data createDataItem(Constant constant) {
  59         int size;
  60         if (constant instanceof VMConstant) {
  61             VMConstant vmConstant = (VMConstant) constant;
  62             boolean compressed;
  63             if (constant instanceof HotSpotConstant) {
  64                 HotSpotConstant c = (HotSpotConstant) vmConstant;
  65                 compressed = c.isCompressed();
  66             } else {
  67                 throw new GraalError(String.valueOf(constant));
  68             }
  69 
  70             size = compressed ? 4 : target.wordSize;
  71             if (size == 4) {
  72                 return new Data(size, size) {
  73 
  74                     @Override
  75                     protected void emit(ByteBuffer buffer, Patches patches) {
  76                         patches.registerPatch(vmConstant);
  77                         buffer.putInt(0xDEADDEAD);
  78                     }
  79                 };
  80             } else {
  81                 return new Data(size, size) {
  82 
  83                     @Override
  84                     protected void emit(ByteBuffer buffer, Patches patches) {
  85                         patches.registerPatch(vmConstant);
  86                         buffer.putLong(0xDEADDEADDEADDEADL);
  87                     }
  88                 };
  89             }
  90         } else if (JavaConstant.isNull(constant)) {
  91             boolean compressed = COMPRESSED_NULL.equals(constant);
  92             size = compressed ? 4 : target.wordSize;
  93             return ZeroData.create(size, size);
  94         } else if (constant instanceof SerializableConstant) {
  95             SerializableConstant s = (SerializableConstant) constant;
  96             return new SerializableData(s);
  97         } else {
  98             throw new GraalError(String.valueOf(constant));
  99         }
 100     }
 101 }