Skip to content

Commit aca5a08

Browse files
author
Derek Hower
committed
Fix gen problems
1 parent a6911d2 commit aca5a08

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

backends/cpp_hart_gen/cpp/include/udb/types.hpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,34 @@ namespace udb {
123123
BitfieldMember& operator=(const _Bits<N, Signed>& value);
124124

125125
template <std::integral Type>
126-
bool operator==(const Type& other) { return other == static_cast<Bits<Size>>(*this); }
126+
bool operator==(const Type& other) const { return other == static_cast<Bits<Size>>(*this); }
127127

128128
template <unsigned N, bool Signed>
129-
bool operator==(const _Bits<N, Signed>& other) { return other == static_cast<Bits<Size>>(*this); }
129+
bool operator==(const _Bits<N, Signed>& other) const { return other == static_cast<Bits<Size>>(*this); }
130130

131131
template <unsigned N, bool Signed>
132-
bool operator>(const _Bits<N, Signed>& other) { return static_cast<Bits<Size>>(*this) > other; }
132+
bool operator>(const _Bits<N, Signed>& other) const { return static_cast<Bits<Size>>(*this) > other; }
133133

134134
template <unsigned N, bool Signed>
135-
bool operator>=(const _Bits<N, Signed>& other) { return static_cast<Bits<Size>>(*this) >= other; }
135+
bool operator>=(const _Bits<N, Signed>& other) const { return static_cast<Bits<Size>>(*this) >= other; }
136136

137137
template <unsigned N, bool Signed>
138-
bool operator<=(const _Bits<N, Signed>& other) { return static_cast<Bits<Size>>(*this) <= other; }
138+
bool operator<=(const _Bits<N, Signed>& other) const { return static_cast<Bits<Size>>(*this) <= other; }
139139

140140
template <unsigned OtherParentSize, unsigned OtherStart, unsigned OtherSize>
141-
bool operator<(const BitfieldMember<OtherParentSize, OtherStart, OtherSize>& other) { return static_cast<Bits<Size>>(*this) > static_cast<Bits<OtherSize>>(*this); }
141+
bool operator<(const BitfieldMember<OtherParentSize, OtherStart, OtherSize>& other) const { return static_cast<Bits<Size>>(*this) > static_cast<Bits<OtherSize>>(*this); }
142+
143+
144+
145+
template <unsigned N, bool Signed>
146+
Bits<Size> operator>>(const _Bits<N, Signed>& shamt) const { return static_cast<Bits<Size>>(*this) >> shamt; }
147+
template <typename T>
148+
requires (std::integral<T>)
149+
Bits<Size> operator>>(const T& shamt) const { return static_cast<Bits<Size>>(*this) >> shamt; }
150+
142151

143152
template <unsigned N, bool Signed>
144-
Bits<Size> operator&(const _Bits<N, Signed>& other) { return static_cast<Bits<Size>>(*this) & other; }
153+
Bits<Size> operator&(const _Bits<N, Signed>& other) const { return static_cast<Bits<Size>>(*this) & other; }
145154

146155
Bits<Bits<Size>::InfinitePrecision> operator<<(const int& shamt) const { return static_cast<Bits<Size>>(*this) << shamt; }
147156

backends/cpp_hart_gen/cpp/include/udb/util.hpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace udb {
3434
if constexpr (size == bits_len) {
3535
return value;
3636
} else {
37-
constexpr Bits<bits_len> mask = (static_cast<Bits<bits_len>>(1).template const_sll<size>()) - 1;
37+
constexpr Bits<bits_len> mask = (static_cast<Bits<bits_len>>(1).template sll<size>()) - 1;
3838
return (value >> start) & mask;
3939
}
4040
}
@@ -48,12 +48,13 @@ namespace udb {
4848
if constexpr (size == BitfieldMemberSize) {
4949
return value;
5050
} else {
51-
constexpr Bits<BitfieldMemberSize> mask = (static_cast<Bits<BitfieldMemberSize>>(1).template const_sll<size>()) - 1;
51+
constexpr Bits<BitfieldMemberSize> mask = (static_cast<Bits<BitfieldMemberSize>>(1).template sll<size>()) - 1;
5252
return (value >> start) & mask;
5353
}
5454
}
5555
// extract bits, where the extraction is not known at compile time
5656
template <typename T>
57+
requires (std::integral<T>)
5758
T extract(T value, unsigned start, unsigned size)
5859
{
5960
udb_assert((start + size) <= sizeof(T)*8, "extraction out of bound");
@@ -66,6 +67,24 @@ namespace udb {
6667
}
6768
}
6869

70+
template<unsigned P, unsigned N, unsigned M, typename StartType, typename SizeType>
71+
Bits<BitfieldMember<P, N, M>::Width> extract(const BitfieldMember<P, N, M>& value, const StartType& start, const SizeType& size)
72+
{
73+
udb_assert((start + size) <= (BitfieldMember<P, N, M>::Width), "extraction out of bound");
74+
75+
if (size == BitfieldMember<P, N, M>::Width) {
76+
return static_cast<const Bits<BitfieldMember<P, N, M>::Width>>(value);
77+
} else {
78+
if constexpr (BitfieldMember<P, N, M>::Width < 64) {
79+
uint64_t mask = (1ull << size) - 1;
80+
return (value >> start) & mask;
81+
} else {
82+
Bits<BitfieldMember<P, N, M>::Width> mask = (static_cast<Bits<BitfieldMember<P, N, M>::Width>>(1) << size) - 1;
83+
return (value >> start) & mask;
84+
}
85+
}
86+
}
87+
6988
template <unsigned MSB, unsigned LSB, unsigned T>
7089
constexpr Bits<T> bit_insert(const Bits<T>& target, const Bits<MSB - LSB + 1>& value)
7190
{

backends/cpp_hart_gen/templates/csrs.cxx.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CsrFieldType <%= name_of(:csr_field, cfg_arch, csr.name, field.name) %>::type(co
2929
<%- if field.defined_in_all_bases? && cfg_arch.multi_xlen? -%>
3030
if (xlen == 32) {
3131
<%= field.type_to_cpp(32) %>
32-
} else { // if (xlen == 64)
32+
} else { // if (xlen == 64)
3333
<%= field.type_to_cpp(64) %>
3434
}
3535
<%- else -%>

0 commit comments

Comments
 (0)