1 /*
   2  * Copyright (c) 2013, 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.replacements;
  26 
  27 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_METAACCESS;
  28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.referentOffset;
  29 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
  30 
  31 import org.graalvm.compiler.api.replacements.Snippet;
  32 import org.graalvm.compiler.debug.DebugHandlersFactory;
  33 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  34 import org.graalvm.compiler.nodes.extended.FixedValueAnchorNode;
  35 import org.graalvm.compiler.nodes.extended.RawLoadNode;
  36 import org.graalvm.compiler.nodes.memory.HeapAccess.BarrierType;
  37 import org.graalvm.compiler.nodes.spi.LoweringTool;
  38 import org.graalvm.compiler.options.OptionValues;
  39 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
  40 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
  41 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
  42 import org.graalvm.compiler.replacements.Snippets;
  43 import org.graalvm.compiler.word.Word;
  44 
  45 import jdk.vm.ci.code.TargetDescription;
  46 
  47 public class UnsafeLoadSnippets implements Snippets {
  48 
  49     @Snippet
  50     public static Object lowerUnsafeLoad(Object object, long offset) {
  51         Object fixedObject = FixedValueAnchorNode.getObject(object);
  52         if (object instanceof java.lang.ref.Reference && referentOffset(INJECTED_METAACCESS) == offset) {
  53             return Word.objectToTrackedPointer(fixedObject).readObject((int) offset, BarrierType.PRECISE);
  54         } else {
  55             return Word.objectToTrackedPointer(fixedObject).readObject((int) offset, BarrierType.NONE);
  56         }
  57     }
  58 
  59     public static class Templates extends AbstractTemplates {
  60 
  61         private final SnippetInfo unsafeLoad = snippet(UnsafeLoadSnippets.class, "lowerUnsafeLoad");
  62 
  63         public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, TargetDescription target) {
  64             super(options, factories, providers, providers.getSnippetReflection(), target);
  65         }
  66 
  67         public void lower(RawLoadNode load, LoweringTool tool) {
  68             Arguments args = new Arguments(unsafeLoad, load.graph().getGuardsStage(), tool.getLoweringStage());
  69             args.add("object", load.object());
  70             args.add("offset", load.offset());
  71             template(load, args).instantiate(providers.getMetaAccess(), load, DEFAULT_REPLACER, args);
  72         }
  73     }
  74 }