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 java.util.Arrays; 27 import java.util.function.IntFunction; 28 import java.util.stream.IntStream; 29 import java.util.stream.Stream; 30 31 public class BitFields implements Type { 32 final Scalar storage; 33 // fields is a list of bits used 34 final int[] fields; 35 36 public BitFields(Scalar storage, int[] fields) { 37 this.storage = storage; 38 this.fields = fields; 39 } 40 41 public Scalar getStorage() { 42 return storage; 43 } 44 45 public Stream<BitField> fields() { 46 IntFunction<BitField> toBitField = new IntFunction<BitField>() { 47 int used = 0; 48 49 @Override 50 public BitField apply(int v) { 51 BitField rv = new BitField(storage, used, v); 52 used += v; 53 return rv; 54 } 55 }; 56 57 return IntStream.of(fields).mapToObj(toBitField); 58 } 59 60 @Override 61 public long getSize() { 62 return storage.getSize(); 63 } 64 65 @Override 66 public int hashCode() { 67 return 31 * Arrays.hashCode(fields) + storage.hashCode(); 68 } 69 70 @Override 71 public boolean equals(Object o) { 72 if (!(o instanceof jdk.internal.nicl.types.BitFields)) { 73 return false; 74 } 75 jdk.internal.nicl.types.BitFields other = (jdk.internal.nicl.types.BitFields) o; 76 if (!other.storage.equals(storage)) { 77 return false; 78 } 79 return Arrays.equals(fields, other.fields); 80 } 81 82 @Override 83 public String toString() { 84 StringBuffer sb = new StringBuffer(); 85 sb.append(storage.toString()); 86 sb.append(':'); 87 for (int count : fields) { 88 sb.append(count); 89 sb.append('b'); 90 } 91 return sb.toString(); 92 } 93 94 public static class BitField implements Type { 95 final Scalar storage; 96 final int start_bit; 97 final int bits; 98 99 BitField(Scalar storage, int start, int bits) { 100 this.storage = storage; 101 this.start_bit = start; 102 this.bits = bits; 103 } 104 105 @Override 106 public long getSize() { 107 return storage.getSize(); 108 } 109 110 @Override 111 public int hashCode() { 112 return 31 * start_bit + 13 * bits + storage.hashCode(); 113 } 114 115 @Override 116 public boolean equals(Object o) { 117 if (! (o instanceof BitField)) { 118 return false; 119 } 120 BitField other = (BitField) o; 121 if (! other.storage.equals(storage)) { 122 return false; 123 } 124 return start_bit == other.start_bit && bits == other.bits; 125 } 126 127 @Override 128 public String toString() { 129 return storage.toString() + ":" + bits + "b@" + start_bit; 130 } 131 } 132 } --- EOF ---