1 /* 2 * Copyright (c) 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 package jdk.internal.nicl.types; 25 26 import jdk.internal.nicl.Platform; 27 28 public class Scalar implements Type { 29 final char type; 30 final Endianness endianness; 31 final long size; 32 33 private final static String noEndianness = "BVcxv"; 34 35 public Scalar(char type) { 36 this(type, Endianness.NATIVE); 37 } 38 39 public Scalar(char type, Endianness endianness) { 40 this.type = type; 41 this.endianness = (noEndianness.indexOf(type) == -1) ? 42 Endianness.NATIVE : endianness; 43 this.size = Platform.getInstance().getABI().definedSize(type); 44 } 45 46 public Scalar(char type, Endianness endianness, int bits) { 47 this.type = type; 48 this.size = ((bits & 7) != 0) ? (bits >> 3) + 1 : bits >> 3; 49 this.endianness = (noEndianness.indexOf(type) != -1) ? 50 Endianness.NATIVE : endianness; 51 } 52 53 public Endianness getEndianness() { 54 return endianness; 55 } 56 57 public char typeCode() { 58 return type; 59 } 60 61 public boolean isSigned() { 62 return Character.isLowerCase(type); 63 } 64 65 @Override 66 public long getSize() { 67 return size; 68 } 69 70 @Override 71 public int hashCode() { 72 return (type & 0xFF) | ((int) size << 8) | (endianness.ordinal() << 16); 73 } 74 75 @Override 76 public boolean equals(Object o) { 77 if (!(o instanceof jdk.internal.nicl.types.Scalar)) { 78 return false; 79 } 80 jdk.internal.nicl.types.Scalar other = (jdk.internal.nicl.types.Scalar) o; 81 if (type != other.type) { 82 return false; 83 } 84 if (endianness != other.endianness) { 85 return false; 86 } 87 if (size != other.size) { 88 return false; 89 } 90 return true; 91 } 92 93 @Override 94 public String toString() { 95 StringBuffer sb = new StringBuffer(); 96 if (Endianness.NATIVE != endianness) { 97 sb.append(endianness.modifier); 98 } 99 if (type == 'i' || type == 'f' || type == 'v' || type == 'I' || type == 'F') { 100 sb.append('='); 101 sb.append(size << 3); 102 } 103 sb.append(type); 104 return sb.toString(); 105 } 106 }