-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVerilogTypes.h
269 lines (222 loc) · 7.05 KB
/
VerilogTypes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-FileCopyrightText: 2023 The Naja verilog authors <https://github.com/najaeda/naja-verilog/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0
#ifndef __VERILOG_TYPES_H_
#define __VERILOG_TYPES_H_
#include <variant>
#include <vector>
#include <string>
#include <cassert>
namespace naja { namespace verilog {
struct Range {
Range() = default;
Range(const Range&) = default;
Range(int msb, int lsb):
valid_(true), singleValue_(false), msb_(msb), lsb_(lsb) {}
Range(int value):
valid_(true), singleValue_(true), msb_(value) {}
bool operator==(const Range& other) const {
return valid_ == other.valid_
&& singleValue_ == other.singleValue_
&& msb_ == other.msb_
&& lsb_ == other.lsb_;
}
std::string getString() const;
//Range has been parsed
bool valid_ {false};
bool singleValue_ {false};
int msb_ {0};
int lsb_ {0};
};
struct Identifier {
Identifier() = default;
Identifier(const Identifier&) = default;
Identifier(const std::string& name, bool escaped=false): name_(name), escaped_(escaped) {}
bool operator==(const Identifier& other) const {
return name_ == other.name_ && escaped_ == other.escaped_;
}
std::string getString() const;
std::string getDescription() const;
std::string name_;
bool escaped_ {false};
};
using Identifiers = std::vector<Identifier>;
struct Port {
class Direction {
public:
enum DirectionEnum {
Input, Output, InOut, Unknown
};
Direction() = default;
Direction(const DirectionEnum& dirEnum);
Direction(const Direction&) = default;
Direction(Direction&&) = default;
Direction& operator=(const Direction&) = default;
operator const DirectionEnum&() const {return dirEnum_;}
std::string getString() const;
private:
DirectionEnum dirEnum_ { Unknown };
};
Port() = default;
Port(const Port&) = default;
Port(const Identifier& identifier, Direction direction):
identifier_(identifier),
direction_(direction)
{}
Port(const Identifier& identifier, Direction direction, const Range& range):
identifier_(identifier),
range_(range),
direction_(direction)
{}
bool isBus() const { return range_.valid_; }
Identifier identifier_ {};
Range range_ {};
Direction direction_ {};
std::string getString() const;
};
using Ports = std::vector<Port>;
struct Net {
class Type {
public:
enum TypeEnum {
Wire, Supply0, Supply1, Unknown
};
Type() = default;
Type(const TypeEnum& dirEnum);
Type(const Type&) = default;
Type(Type&&) = default;
Type& operator=(const Type&) = default;
operator const TypeEnum&() const {return typeEnum_;}
std::string getString() const;
private:
TypeEnum typeEnum_ { Unknown };
};
Net() = default;
Net(const Net&) = default;
Net(const Identifier& identifier, const Range& range, Type type):
identifier_(identifier),
range_(range),
type_(type)
{}
bool isBus() const { return range_.valid_; }
Identifier identifier_ {};
Range range_ {};
Type type_ {};
std::string getString() const;
};
struct RangeIdentifier {
RangeIdentifier() = default;
RangeIdentifier(const RangeIdentifier&) = default;
RangeIdentifier(const Identifier& identifier): identifier_(identifier) {}
RangeIdentifier(const Identifier& identifier, const Range& range): identifier_(identifier), range_(range) {}
std::string getString() const;
std::string getDescription() const;
Identifier identifier_;
Range range_;
};
using RangeIdentifiers = std::vector<RangeIdentifier>;
struct BasedNumber {
BasedNumber() = default;
BasedNumber(bool isSigned, char base, const std::string& digits) {
hasSize_ = false;
signed_ = isSigned;
switch (base) {
case 'b': case 'B':
base_ = BINARY;
break;
case 'o': case 'O':
base_ = OCTAL;
break;
case 'h': case 'H':
base_ = HEX;
break;
case 'd': case 'D':
base_ = DECIMAL;
break;
}
digits_ = digits;
}
BasedNumber(const std::string& size, bool isSigned, char base, const std::string& digits):
BasedNumber(isSigned, base, digits) {
hasSize_ = true;
size_ = static_cast<unsigned>(std::stoul(size));
}
enum Base { BINARY, OCTAL, HEX, DECIMAL };
static std::string getBaseString(Base base);
std::string getString() const;
std::string getDescription() const;
bool hasSize_ {false};
size_t size_ {0};
bool signed_ {false};
Base base_ {};
std::string digits_ {};
};
struct Number {
Number() = default;
Number(const Number&) = default;
Number(const std::string& value) {
value_ = static_cast<unsigned>(std::stoul(value));
}
Number(const std::string& size, bool isSigned, char base, const std::string& value) {
value_ = BasedNumber(size, isSigned, base, value);
}
Number(bool isSigned, char base, const std::string& value) {
value_ = BasedNumber(isSigned, base, value);
}
std::string getString() const;
std::string getDescription() const;
int getInt() const;
enum Type { BASED, UNSIGNED };
using Value = std::variant<BasedNumber, unsigned>;
bool sign_ {true}; //true positive, false negative
Value value_ {};
};
struct Expression;
struct Concatenation {
using Expressions = std::vector<Expression>;
Concatenation() = default;
Concatenation(const Concatenation&) = default;
Concatenation(const Expressions& expressions);
std::string getString() const;
std::string getDescription() const;
Expressions expressions_ {};
};
struct Expression {
using Expressions = std::vector<Expression>;
Expression() = default;
Expression(const Expression&) = default;
std::string getString() const;
std::string getDescription() const;
enum Type { RANGEIDENTIFIER=0, NUMBER=1, STRING=2, CONCATENATION=3 };
using Value = std::variant<RangeIdentifier, Number, std::string, Concatenation>;
bool valid_ { false };
//If valid_ is true and supported_ is false, then this expression construction
//is not currently supported.
bool supported_ { true };
Value value_ {};
};
struct ConstantExpression {
ConstantExpression() = default;
ConstantExpression(const ConstantExpression&) = default;
enum Type { NUMBER=0, STRING=1 };
using Value = std::variant<Number, std::string>;
Type getType() const { return Type(value_.index()); }
std::string getString() const;
std::string getDescription() const;
bool valid_ { false };
Value value_ {};
};
struct Attribute {
Attribute() = default;
Attribute(const Attribute&) = default;
Attribute(const naja::verilog::Identifier& name, const ConstantExpression& expression):
name_(name),
expression_(expression)
{}
std::string getString() const;
std::string getDescription() const;
naja::verilog::Identifier name_ {};
ConstantExpression expression_ {};
};
}} // namespace verilog // namespace naja
#endif /* __VERILOG_TYPES_H_ */