1 /*
   2  * Copyright (c) 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.foreign.layout;
  26 
  27 import java.util.Map;
  28 import java.util.Optional;
  29 import jdk.internal.misc.Unsafe;
  30 
  31 /**
  32  * A value layout. This layout is used to model basic native types, such as integral values and floating point numbers.
  33  * There are three kind of supported value layouts: integer signed, integer unsigned and floating point. Each
  34  * value layout also has a size and an endianness (which could be either big-endian or little-endian), as well as
  35  * an optional substructure layout (see {@link Value#contents()}).
  36  */
  37 public class Value extends AbstractDescriptor<Value> implements Layout {
  38 
  39     /**
  40      * The value kind.
  41      */
  42     public enum Kind {
  43         /** Kind of signed integral. */
  44         INTEGRAL_UNSIGNED("u"),
  45         /** Kind of signed integral. */
  46         INTEGRAL_SIGNED("i"),
  47         /** Kind of floating-point value. */
  48         FLOATING_POINT("f");
  49 
  50         String tag;
  51 
  52         Kind(String tag) {
  53             this.tag = tag;
  54         }
  55     }
  56 
  57     /**
  58      * value endianness.
  59      */
  60     public enum Endianness {
  61         /** Least-significant-bit stored at lowest address. */
  62         LITTLE_ENDIAN,
  63         /** Most-significant-bit stored at lowest address. */
  64         BIG_ENDIAN;
  65 
  66         /**
  67          * Platform-dependent endianness value.
  68          * @return the platform-dependent endianness value.
  69          */
  70         public static Endianness hostEndian() {
  71             return Unsafe.getUnsafe().isBigEndian() ? BIG_ENDIAN : LITTLE_ENDIAN;
  72         }
  73     }
  74 
  75     private final Kind kind;
  76     private final Endianness endianness;
  77     private final long size;
  78     private final Optional<Group> contents;
  79 
  80     Value(Kind kind, Endianness endianness, long size, Optional<Group> contents, Map<String, String> annotations) {
  81         super(annotations);
  82         this.kind = kind;
  83         this.endianness = endianness;
  84         this.size = size;
  85         this.contents = contents;
  86     }
  87 
  88     /**
  89      * Returns the value kind (e.g. integral vs. floating point).
  90      * @return the value kind.
  91      */
  92     public Kind kind() {
  93         return kind;
  94     }
  95 
  96     /**
  97      * Returns the value endianness.
  98      * @return the value endianness.
  99      */
 100     public Endianness endianness() {
 101         return endianness;
 102     }
 103 
 104     /**
 105      * Is this value a signed layout?
 106      * @return true, if this value layout is signed.
 107      */
 108     public boolean isSigned() {
 109         return kind != Kind.INTEGRAL_UNSIGNED;
 110     }
 111 
 112     /**
 113      * Returns true if the value endianness match native byte order
 114      * @return true if endianness match system architecture.
 115      */
 116     public boolean isNativeByteOrder() {
 117         return endianness == Endianness.hostEndian();
 118     }
 119 
 120     @Override
 121     public long bitsSize() {
 122         return size;
 123     }
 124 
 125     @Override
 126     public boolean isPartial() {
 127         return contents.map(Layout::isPartial).orElse(false);
 128     }
 129 
 130     /**
 131      * Return optional group overlay associated with this value layout. This is useful to view a value
 132      * as an aggregate.
 133      * @return group overlay.
 134      */
 135     public Optional<Group> contents() {
 136         return contents;
 137     }
 138 
 139     /**
 140      * Attach a sub-group layout to this value layout.
 141      * @param contents the sub-group overlay attached to this (container) layout.
 142      * @return a new container layout with associated group substructure.
 143      */
 144     public Value withContents(Group contents) {
 145         return new Value(kind, endianness, size, Optional.of(contents), annotations());
 146     }
 147 
 148     /**
 149      * Create a floating-point value of given size.
 150      * @param size the floating-polong size.
 151      * @return the new value layout.
 152      */
 153     public static Value ofFloatingPoint(long size) {
 154         return ofFloatingPoint(Endianness.hostEndian(), size);
 155     }
 156 
 157     /**
 158      * Create a floating-point value of given endianness and size.
 159      * @param endianness the floating-point endianness
 160      * @param size the floating-point size.
 161      * @return the new value layout.
 162      */
 163     public static Value ofFloatingPoint(Endianness endianness, long size) {
 164         return new Value(Kind.FLOATING_POINT, endianness, size, Optional.empty(), NO_ANNOS);
 165     }
 166 
 167     /**
 168      * Create an unsigned integral value of given size.
 169      * @param size the integral size.
 170      * @return the new value layout.
 171      */
 172     public static Value ofUnsignedInt(long size) {
 173         return ofUnsignedInt(Endianness.hostEndian(), size);
 174     }
 175 
 176     /**
 177      * Create an unsigned integral value of given endianness and size.
 178      * @param endianness the integral endianness
 179      * @param size the integral size.
 180      * @return the new value layout.
 181      */
 182     public static Value ofUnsignedInt(Endianness endianness, long size) {
 183         return new Value(Kind.INTEGRAL_UNSIGNED, endianness, size, Optional.empty(), NO_ANNOS);
 184     }
 185 
 186     /**
 187      * Create an signed integral value of given size.
 188      * @param size the integral size.
 189      * @return the new value layout.
 190      */
 191     public static Value ofSignedInt(long size) {
 192         return ofSignedInt(Endianness.hostEndian(), size);
 193     }
 194 
 195     /**
 196      * Create an signed integral value of given endianness and size.
 197      * @param endianness the integral endianness
 198      * @param size the integral size.
 199      * @return the new value layout.
 200      */
 201     public static Value ofSignedInt(Endianness endianness, long size) {
 202         return new Value(Kind.INTEGRAL_SIGNED, endianness, size, Optional.empty(), NO_ANNOS);
 203     }
 204 
 205     @Override
 206     public String toString() {
 207         String prefix = wrapWithAnnotations(String.format("%s%d",
 208                 endianness == Endianness.BIG_ENDIAN ?
 209                         kind.tag.toUpperCase() : kind.tag,
 210                 size));
 211         return contents().map(g -> prefix + "=" + g).orElse(prefix);
 212     }
 213 
 214     @Override
 215     public boolean equals(Object other) {
 216         if (this == other) {
 217             return true;
 218         }
 219         if (!super.equals(other)) {
 220             return false;
 221         }
 222         if (!(other instanceof Value)) {
 223             return false;
 224         }
 225         Value v = (Value)other;
 226         return kind.equals(v.kind) && endianness.equals(v.endianness) &&
 227             size == v.size && contents.equals(v.contents);
 228     }
 229 
 230     @Override
 231     public int hashCode() {
 232         return super.hashCode() ^ kind.hashCode() ^ endianness.hashCode() ^
 233             Long.hashCode(size) ^ contents.hashCode();
 234     }
 235 
 236     @Override
 237     Value withAnnotations(Map<String, String> annotations) {
 238         return new Value(kind, endianness, size, contents, annotations);
 239     }
 240 }