1 /********************* */
2 /*! \file theory_bv_type_rules.h
4 ** Original author: dejan
5 ** Major contributors: mdeters, cconway
6 ** Minor contributors (to current version): none
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010, 2011 The Analysis of Computer Systems Group (ACSys)
9 ** Courant Institute of Mathematical Sciences
10 ** New York University
11 ** See the file COPYING in the top-level source directory for licensing
12 ** information.\endverbatim
14 ** \brief Bitvector theory.
19 #include "cvc4_private.h"
23 #ifndef __CVC4__THEORY__BV__THEORY_BV_TYPE_RULES_H
24 #define __CVC4__THEORY__BV__THEORY_BV_TYPE_RULES_H
30 class BitVectorConstantTypeRule
{
32 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
33 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
34 return nodeManager
->mkBitVectorType(n
.getConst
<BitVector
>().getSize());
39 class BitVectorBitOfTypeRule
{
41 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
42 throw (TypeCheckingExceptionPrivate
) {
45 BitVectorBitOf info
= n
.getOperator().getConst
<BitVectorBitOf
>();
46 TypeNode t
= n
[0].getType(check
);
48 if (!t
.isBitVector()) {
49 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector term");
51 if (info
.bitIndex
>= t
.getBitVectorSize()) {
52 throw TypeCheckingExceptionPrivate(n
, "extract index is larger than the bitvector size");
55 return nodeManager
->booleanType();
60 class BitVectorCompRule
{
62 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
63 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
65 TypeNode lhs
= n
[0].getType(check
);
66 TypeNode rhs
= n
[1].getType(check
);
67 if (!lhs
.isBitVector() || lhs
!= rhs
) {
68 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector terms of the same width");
71 return nodeManager
->mkBitVectorType(1);
75 class BitVectorArithRule
{
77 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
78 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
79 unsigned maxWidth
= 0;
80 TNode::iterator it
= n
.begin();
81 TNode::iterator it_end
= n
.end();
82 // TODO: optimize unary neg
83 for (; it
!= it_end
; ++ it
) {
84 TypeNode t
= (*it
).getType(check
);
85 if (check
&& !t
.isBitVector()) {
86 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector terms");
88 maxWidth
= std::max( maxWidth
, t
.getBitVectorSize() );
90 return nodeManager
->mkBitVectorType(maxWidth
);
94 class BitVectorFixedWidthTypeRule
{
96 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
97 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
98 TNode::iterator it
= n
.begin();
99 TypeNode t
= (*it
).getType(check
);
101 if (!t
.isBitVector()) {
102 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector terms");
104 TNode::iterator it_end
= n
.end();
105 for (++ it
; it
!= it_end
; ++ it
) {
106 if ((*it
).getType(check
) != t
) {
107 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector terms of the same width");
115 class BitVectorPredicateTypeRule
{
117 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
118 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
120 TypeNode lhsType
= n
[0].getType(check
);
121 if (!lhsType
.isBitVector()) {
122 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector terms");
124 TypeNode rhsType
= n
[1].getType(check
);
125 if (lhsType
!= rhsType
) {
126 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector terms of the same width");
129 return nodeManager
->booleanType();
133 class BitVectorExtractTypeRule
{
135 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
136 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
137 BitVectorExtract extractInfo
= n
.getOperator().getConst
<BitVectorExtract
>();
139 // NOTE: We're throwing a type-checking exception here even
140 // if check is false, bc if we allow high < low the resulting
141 // type will be illegal
142 if (extractInfo
.high
< extractInfo
.low
) {
143 throw TypeCheckingExceptionPrivate(n
, "high extract index is smaller than the low extract index");
147 TypeNode t
= n
[0].getType(check
);
148 if (!t
.isBitVector()) {
149 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector term");
151 if (extractInfo
.high
>= t
.getBitVectorSize()) {
152 throw TypeCheckingExceptionPrivate(n
, "high extract index is bigger than the size of the bit-vector");
155 return nodeManager
->mkBitVectorType(extractInfo
.high
- extractInfo
.low
+ 1);
159 class BitVectorConcatRule
{
161 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
162 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
164 TNode::iterator it
= n
.begin();
165 TNode::iterator it_end
= n
.end();
166 for (; it
!= it_end
; ++ it
) {
167 TypeNode t
= (*it
).getType(check
);
168 // NOTE: We're throwing a type-checking exception here even
169 // when check is false, bc if we don't check that the arguments
170 // are bit-vectors the result type will be inaccurate
171 if (!t
.isBitVector()) {
172 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector terms");
174 size
+= t
.getBitVectorSize();
176 return nodeManager
->mkBitVectorType(size
);
180 class BitVectorRepeatTypeRule
{
182 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
183 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
184 TypeNode t
= n
[0].getType(check
);
185 // NOTE: We're throwing a type-checking exception here even
186 // when check is false, bc if the argument isn't a bit-vector
187 // the result type will be inaccurate
188 if (!t
.isBitVector()) {
189 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector term");
191 unsigned repeatAmount
= n
.getOperator().getConst
<BitVectorRepeat
>();
192 return nodeManager
->mkBitVectorType(repeatAmount
* t
.getBitVectorSize());
196 class BitVectorExtendTypeRule
{
198 inline static TypeNode
computeType(NodeManager
* nodeManager
, TNode n
, bool check
)
199 throw (TypeCheckingExceptionPrivate
, AssertionException
) {
200 TypeNode t
= n
[0].getType(check
);
201 // NOTE: We're throwing a type-checking exception here even
202 // when check is false, bc if the argument isn't a bit-vector
203 // the result type will be inaccurate
204 if (!t
.isBitVector()) {
205 throw TypeCheckingExceptionPrivate(n
, "expecting bit-vector term");
207 unsigned extendAmount
= n
.getKind() == kind::BITVECTOR_SIGN_EXTEND
?
208 (unsigned) n
.getOperator().getConst
<BitVectorSignExtend
>() :
209 (unsigned) n
.getOperator().getConst
<BitVectorZeroExtend
>();
210 return nodeManager
->mkBitVectorType(extendAmount
+ t
.getBitVectorSize());
214 class CardinalityComputer
{
216 inline static Cardinality
computeCardinality(TypeNode type
) {
217 Assert(type
.getKind() == kind::BITVECTOR_TYPE
);
219 unsigned size
= type
.getConst
<BitVectorSize
>();
223 Integer i
= Integer(2).pow(size
);
226 };/* class CardinalityComputer */
228 }/* CVC4::theory::bv namespace */
229 }/* CVC4::theory namespace */
230 }/* CVC4 namespace */
232 #endif /* __CVC4__THEORY__BV__THEORY_BV_TYPE_RULES_H */