1/*
2 * Copyright (c) 2001, 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
25package sun.jvm.hotspot.debugger.cdbg.basic;
26
27import sun.jvm.hotspot.debugger.*;
28import sun.jvm.hotspot.debugger.cdbg.*;
29import sun.jvm.hotspot.utilities.Assert;
30
31public class BasicBitType extends BasicIntType implements BitType {
32  // Integer type or lazy type
33  private Type underlyingType;
34  private int sizeInBits;
35  private int offset;
36
37  /** Underlying type of enum must be an integer type (or as yet
38      unresolved) */
39
40  public BasicBitType(Type underlyingType, int sizeInBits, int lsbOffset) {
41    this(underlyingType, sizeInBits, lsbOffset, 0);
42  }
43
44  private BasicBitType(Type underlyingType, int sizeInBits, int lsbOffset, int cvAttributes) {
45    super(null, 0, false, cvAttributes);
46    this.underlyingType = underlyingType;
47    this.sizeInBits = sizeInBits;
48    this.offset = lsbOffset;
49  }
50
51  public BitType asBit() { return this; }
52
53  public int     getSize() { return underlyingType.getSize(); }
54  public boolean isUnsigned() {
55    if (underlyingType.isInt()) {
56      return ((IntType) underlyingType).isUnsigned();
57    }
58    return false;
59  }
60
61  public int getSizeInBits() {
62    return sizeInBits;
63  }
64
65  public int getOffset() {
66    return offset;
67  }
68
69  Type resolveTypes(BasicCDebugInfoDataBase db, ResolveListener listener) {
70    super.resolveTypes(db, listener);
71    underlyingType = db.resolveType(this, underlyingType, listener, "resolving bit type");
72    setName(underlyingType.getName());
73    if (Assert.ASSERTS_ENABLED) {
74      BasicType b = (BasicType) underlyingType;
75      Assert.that(b.isLazy() || b.isInt(),
76                  "Underlying type of bitfield must be integer type (or unresolved due to error)");
77    }
78    return this;
79  }
80
81  public void iterateObject(Address a, ObjectVisitor v, FieldIdentifier f) {
82    long mask = maskFor(sizeInBits);
83    long val = ((a.getCIntegerAt(0, getSize(), isUnsigned())) >> getOffset()) & mask;
84    if (!isUnsigned()) {
85      if ((val & highBit(sizeInBits)) != 0) {
86        // Must sign extend
87        val = val | (~mask);
88      }
89    }
90    v.doBit(f, val);
91  }
92
93  protected Type createCVVariant(int cvAttributes) {
94    return new BasicBitType(underlyingType, getSizeInBits(), getOffset(), cvAttributes);
95  }
96
97  public void visit(TypeVisitor v) {
98    v.doBitType(this);
99  }
100
101  private static long maskFor(int sizeInBits) {
102    return ((1 << sizeInBits) - 1);
103  }
104
105  private static long highBit(int sizeInBits) {
106    return (1 << (sizeInBits - 1));
107  }
108}
109