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