1 // Copyright 2017-2019 VMware, Inc.
2 // SPDX-License-Identifier: BSD-2-Clause
3 //
4 // The BSD-2 license (the License) set forth below applies to all parts of the
5 // Cascade project.  You may not use this file except in compliance with the
6 // License.
7 //
8 // BSD-2 License
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this
14 // list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice,
17 // this list of conditions and the following disclaimer in the documentation
18 // and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
21 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef CASCADE_SRC_VERILOG_AST_RANGE_EXPRESSION_H
32 #define CASCADE_SRC_VERILOG_AST_RANGE_EXPRESSION_H
33 
34 #include "common/bits.h"
35 #include "verilog/ast/types/expression.h"
36 #include "verilog/ast/types/macro.h"
37 
38 namespace cascade {
39 
40 // TODO(eschkufz) Making this class a subclass of Expression places an unused
41 // Bits decoration in this node. If we need to shrink cascade's memory
42 // footprint, a little shuffling of the Expression hierarchy can fix this.
43 
44 class RangeExpression : public Expression {
45   public:
46     // Supporting Concepts:
47     enum class Type : uint8_t {
48       CONSTANT = 0,
49       PLUS,
50       MINUS
51     };
52 
53     // Constructors:
54     RangeExpression(size_t i__, size_t j__ = 0);
55     RangeExpression(Expression* upper__, Type type__, Expression* lower__);
56     ~RangeExpression() override;
57 
58     // Node Interface:
59     NODE(RangeExpression)
60     RangeExpression* clone() const override;
61 
62     // Get/Set:
63     PTR_GET_SET(RangeExpression, Expression, upper)
64     VAL_GET_SET(RangeExpression, Type, type)
65     PTR_GET_SET(RangeExpression, Expression, lower)
66 
67   private:
68     PTR_ATTR(Expression, upper);
69     VAL_ATTR(Type, type);
70     PTR_ATTR(Expression, lower);
71 
72     friend class Evaluate;
73     DECORATION(uint32_t, vupper);
74     DECORATION(uint32_t, vlower);
75 };
76 
RangeExpression(size_t i__,size_t j__)77 inline RangeExpression::RangeExpression(size_t i__, size_t j__) : Expression(Node::Tag::range_expression) {
78   upper_ = new Number(Bits(32, i__-1), Number::Format::UNBASED);
79   type_ = RangeExpression::Type::CONSTANT;
80   lower_ = new Number(Bits(32, j__), Number::Format::UNBASED);
81 }
82 
RangeExpression(Expression * upper__,Type type__,Expression * lower__)83 inline RangeExpression::RangeExpression(Expression* upper__, Type type__, Expression* lower__) : Expression(Node::Tag::range_expression) {
84   parent_ = nullptr;
85   PTR_SETUP(upper);
86   VAL_SETUP(type);
87   PTR_SETUP(lower);
88 }
89 
~RangeExpression()90 inline RangeExpression::~RangeExpression() {
91   PTR_TEARDOWN(upper);
92   VAL_TEARDOWN(type);
93   PTR_TEARDOWN(lower);
94 }
95 
clone()96 inline RangeExpression* RangeExpression::clone() const {
97   return new RangeExpression(upper_->clone(), type_, lower_->clone());
98 }
99 
100 } // namespace cascade
101 
102 #endif
103