1 /*
   2  * Copyright (c) 2012, 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 jdk.internal.vm.compiler.word;
  26 
  27 import jdk.internal.vm.compiler.word.impl.WordBoxFactory;
  28 import jdk.internal.vm.compiler.word.impl.WordFactoryOpcode;
  29 import jdk.internal.vm.compiler.word.impl.WordFactoryOperation;
  30 
  31 /**
  32  * Provides factory method to create machine-word-sized values.
  33  *
  34  * @since 1.0
  35  */
  36 public final class WordFactory {
  37 
  38     private WordFactory() {
  39     }
  40 
  41     /**
  42      * The constant 0, i.e., the word with no bits set. There is no difference between a signed and
  43      * unsigned zero.
  44      *
  45      * @return the constant 0.
  46      *
  47      * @since 1.0
  48      */
  49     @WordFactoryOperation(opcode = WordFactoryOpcode.ZERO)
  50     public static <T extends WordBase> T zero() {
  51         return WordBoxFactory.box(0L);
  52     }
  53 
  54     /**
  55      * The null pointer, i.e., the pointer with no bits set. There is no difference to a signed or
  56      * unsigned {@link #zero}.
  57      *
  58      * @return the null pointer.
  59      *
  60      * @since 1.0
  61      */
  62     @WordFactoryOperation(opcode = WordFactoryOpcode.ZERO)
  63     public static <T extends PointerBase> T nullPointer() {
  64         return WordBoxFactory.box(0L);
  65     }
  66 
  67     /**
  68      * Unsafe conversion from a Java long value to a Word. The parameter is treated as an unsigned
  69      * 64-bit value (in contrast to the semantics of a Java long).
  70      *
  71      * @param val a 64 bit unsigned value
  72      * @return the value cast to Word
  73      *
  74      * @since 1.0
  75      */
  76     @WordFactoryOperation(opcode = WordFactoryOpcode.FROM_UNSIGNED)
  77     public static <T extends UnsignedWord> T unsigned(long val) {
  78         return WordBoxFactory.box(val);
  79     }
  80 
  81     /**
  82      * Unsafe conversion from a Java long value to a {@link PointerBase pointer}. The parameter is
  83      * treated as an unsigned 64-bit value (in contrast to the semantics of a Java long).
  84      *
  85      * @param val a 64 bit unsigned value
  86      * @return the value cast to PointerBase
  87      *
  88      * @since 1.0
  89      */
  90     @WordFactoryOperation(opcode = WordFactoryOpcode.FROM_UNSIGNED)
  91     public static <T extends PointerBase> T pointer(long val) {
  92         return WordBoxFactory.box(val);
  93     }
  94 
  95     /**
  96      * Unsafe conversion from a Java int value to a Word. The parameter is treated as an unsigned
  97      * 32-bit value (in contrast to the semantics of a Java int).
  98      *
  99      * @param val a 32 bit unsigned value
 100      * @return the value cast to Word
 101      *
 102      * @since 1.0
 103      */
 104     @WordFactoryOperation(opcode = WordFactoryOpcode.FROM_UNSIGNED)
 105     public static <T extends UnsignedWord> T unsigned(int val) {
 106         return WordBoxFactory.box(val & 0xffffffffL);
 107     }
 108 
 109     /**
 110      * Unsafe conversion from a Java long value to a Word. The parameter is treated as a signed
 111      * 64-bit value (unchanged semantics of a Java long).
 112      *
 113      * @param val a 64 bit signed value
 114      * @return the value cast to Word
 115      *
 116      * @since 1.0
 117      */
 118     @WordFactoryOperation(opcode = WordFactoryOpcode.FROM_SIGNED)
 119     public static <T extends SignedWord> T signed(long val) {
 120         return WordBoxFactory.box(val);
 121     }
 122 
 123     /**
 124      * Unsafe conversion from a Java int value to a Word. The parameter is treated as a signed
 125      * 32-bit value (unchanged semantics of a Java int).
 126      *
 127      * @param val a 32 bit signed value
 128      * @return the value cast to Word
 129      *
 130      * @since 1.0
 131      */
 132     @WordFactoryOperation(opcode = WordFactoryOpcode.FROM_SIGNED)
 133     public static <T extends SignedWord> T signed(int val) {
 134         return WordBoxFactory.box(val);
 135     }
 136 }