1 /*
   2  * Copyright (c) 2015, 2019, 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 Data createDataItem(Constant constant) {
  55         if (JavaConstant.isNull(constant)) {
  56             boolean compressed = COMPRESSED_NULL.equals(constant);
  57             int size = compressed ? 4 : target.wordSize;
  58             return ZeroData.create(size, size);
  59         } else if (constant instanceof VMConstant) {
  60             VMConstant vmConstant = (VMConstant) constant;
  61             if (!(constant instanceof HotSpotConstant)) {
  62                 throw new GraalError(String.valueOf(constant));
  63             }
  64 
  65             HotSpotConstant c = (HotSpotConstant) vmConstant;
  66             int size = c.isCompressed() ? 4 : target.wordSize;
  67             return new Data(size, size) {
  68                 @Override
  69                 protected void emit(ByteBuffer buffer, Patches patches) {
  70                     int position = buffer.position();
  71                     if (getSize() == Integer.BYTES) {
  72                         buffer.putInt(0xDEADDEAD);
  73                     } else {
  74                         buffer.putLong(0xDEADDEADDEADDEADL);
  75                     }
  76                     patches.registerPatch(position, vmConstant);
  77                 }
  78             };
  79         } else if (constant instanceof SerializableConstant) {
  80             SerializableConstant s = (SerializableConstant) constant;
  81             return new SerializableData(s);
  82         } else {
  83             throw new GraalError(String.valueOf(constant));
  84         }
  85     }
  86 }