Skip to content

Commit 384d152

Browse files
author
Derek Hower
committed
Fix remaining IDL translation errors
1 parent aca5a08 commit 384d152

File tree

7 files changed

+106
-21
lines changed

7 files changed

+106
-21
lines changed

backends/cpp_hart_gen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ set(CONFIG_LIST _)
7070

7171
set(GENERATED_SRCS "")
7272
foreach(config ${CONFIG_LIST})
73-
list(APPEND GENERATED_SRCS "${CMAKE_SOURCE_DIR}/src/cfgs/${config}/csrs.cxx")
7473
list(APPEND GENERATED_SRCS "${CMAKE_SOURCE_DIR}/src/cfgs/${config}/decode.cxx")
74+
list(APPEND GENERATED_SRCS "${CMAKE_SOURCE_DIR}/src/cfgs/${config}/csrs.cxx")
7575
# list(APPEND GENERATED_SRCS "${CMAKE_SOURCE_DIR}/src/cfgs/${config}/params.cxx")
7676
# list(APPEND GENERATED_SRCS "${CMAKE_SOURCE_DIR}/src/cfgs/${config}/hart.cxx")
7777
endforeach()

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

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// we need this to be true for GMP
1414
static_assert(sizeof(long unsigned int) == sizeof(long long unsigned int));
1515

16+
// we make this assumption frequently
17+
static_assert(sizeof(1ull) == 8);
18+
1619
namespace udb
1720
{
1821

@@ -54,6 +57,9 @@ namespace udb
5457
static_assert(N > 0);
5558

5659
public:
60+
// used for template concept resolution
61+
constexpr static bool IsABits = true;
62+
5763
// value of N that represents unknown precision (happens when there is a left shift by unknown value)
5864
constexpr static unsigned InfinitePrecision = BitsInfinitePrecision;
5965

@@ -64,6 +70,8 @@ namespace udb
6470
// advertise the width
6571
constexpr static unsigned Width = N;
6672

73+
constexpr static unsigned width() { return N; }
74+
6775
using StorageType = typename BitsStorageType<N>::type;
6876
using SignedStorageType = typename BitsSignedStorageType<N>::type;
6977

@@ -78,9 +86,7 @@ namespace udb
7886
{
7987
using _StorageType = typename BitsStorageType<_N>::type;
8088

81-
if constexpr (_N == InfinitePrecision)
82-
{
83-
// infinite bits, so there is no masking
89+
if constexpr (_N == InfinitePrecision) {
8490
return false;
8591
}
8692
else if constexpr (_N > MaxNativePrecision)
@@ -996,6 +1002,8 @@ namespace udb
9961002

9971003
static_assert((0x0_b).Width == 1);
9981004
static_assert((0x1_b).Width == 1);
1005+
static_assert((0_b).Width == 1);
1006+
static_assert((1_b).Width == 1);
9991007
static_assert((0x2_b).Width == 2);
10001008
static_assert((0x7_b).Width == 3);
10011009
static_assert((0x8_b).Width == 4);
@@ -1247,3 +1255,49 @@ namespace udb {
12471255

12481256
using PossiblyUndefinedBits = Bits<66>;
12491257
}
1258+
1259+
namespace udb {
1260+
// Bits where the width is only known at runtime (usually because the width is parameter-dependent)
1261+
template <bool Signed>
1262+
class _RuntimeBits {
1263+
public:
1264+
template <unsigned N, bool _Signed>
1265+
_RuntimeBits(const _Bits<N, _Signed>& initial_value)
1266+
: m_value(initial_value), m_width(N)
1267+
{}
1268+
1269+
template <typename T>
1270+
_RuntimeBits(const T& initial_value, unsigned initial_width)
1271+
: m_value(initial_value), m_width(initial_width)
1272+
{}
1273+
1274+
unsigned width() const { return m_width; }
1275+
auto value() const { return m_value; }
1276+
1277+
template <typename T>
1278+
_RuntimeBits operator<<(const T& shamt) {
1279+
return {m_value << shamt, m_width + shamt};
1280+
}
1281+
1282+
template <unsigned N, bool _Signed>
1283+
_RuntimeBits operator|(const _Bits<N, _Signed>& other) {
1284+
return {m_value | other, std::max(N, m_width)};
1285+
}
1286+
1287+
_RuntimeBits operator|(const _RuntimeBits& other) {
1288+
return {m_value | other.m_value, std::max(other.m_width, m_width)};
1289+
}
1290+
1291+
private:
1292+
_Bits<BitsInfinitePrecision, Signed> m_value;
1293+
unsigned m_width;
1294+
};
1295+
1296+
template <unsigned N, bool ASigned, bool BSigned>
1297+
bool operator==(const _Bits<N, ASigned>& a, const _RuntimeBits<BSigned>& b)
1298+
{
1299+
return a == b.value();
1300+
}
1301+
1302+
using RuntimeBits = _RuntimeBits<false>;
1303+
}

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,20 @@ namespace udb {
5252
return (value >> start) & mask;
5353
}
5454
}
55+
5556
// extract bits, where the extraction is not known at compile time
56-
template <typename T>
57-
requires (std::integral<T>)
58-
T extract(T value, unsigned start, unsigned size)
57+
template <typename ValueType, typename StartType, typename SizeType>
58+
requires ((std::integral<ValueType> || ValueType::IsABits) &&
59+
(std::integral<StartType> || StartType::IsABits) &&
60+
(std::integral<SizeType> || SizeType::IsABits))
61+
ValueType extract(const ValueType& value, const StartType& start, const SizeType& size)
5962
{
60-
udb_assert((start + size) <= sizeof(T)*8, "extraction out of bound");
63+
udb_assert((start + size) <= sizeof(ValueType)*8, "extraction out of bound");
6164

62-
if (size == sizeof(T)*8) {
65+
if (size == sizeof(ValueType)*8) {
6366
return value;
6467
} else {
65-
T mask = (static_cast<T>(1) << size) - 1;
68+
ValueType mask = (static_cast<ValueType>(1) << size) - 1;
6669
return (value >> start) & mask;
6770
}
6871
}
@@ -126,7 +129,7 @@ namespace udb {
126129
}
127130

128131
template <unsigned M, typename T>
129-
constexpr Bits<BitsInfinitePrecision> replicate(const Bits<M>& value, const T& N)
132+
constexpr RuntimeBits replicate(const Bits<M>& value, const T& N)
130133
{
131134
udb_assert(N > 0, "Must replicate at least once");
132135
static_assert(M < BitsMaxNativePrecision, "Please don't replicate multiprecision numbers ;(");
@@ -135,7 +138,7 @@ namespace udb {
135138
for (unsigned i=1; i<N; i++) {
136139
result |= value << (i*M);
137140
}
138-
return result;
141+
return {result, M * N};
139142
}
140143

141144

@@ -178,10 +181,27 @@ namespace udb {
178181
}
179182

180183
template <typename... BitsTypes>
184+
requires ((BitsTypes::Width != BitsInfinitePrecision) && ...)
181185
constexpr Bits<ConcatWidth<BitsTypes...>::Width> concat(BitsTypes... bits) {
182186
return __concat(bits...);
183187
}
184188

189+
template <typename BitsType, typename... BitsTypes>
190+
RuntimeBits __runtime_concat(const BitsType& a, const BitsTypes&... bits) {
191+
if constexpr (sizeof...(BitsTypes) == 0) {
192+
return a;
193+
} else {
194+
auto shamt = (bits.width() + ...);
195+
return (RuntimeBits{a} << shamt) | __runtime_concat(bits...);
196+
}
197+
}
198+
199+
template <typename... BitsTypes>
200+
requires ((std::same_as<BitsTypes, RuntimeBits> || ...))
201+
RuntimeBits concat(BitsTypes... bits) {
202+
return __runtime_concat(bits...);
203+
}
204+
185205
static_assert(std::is_same_v<decltype(concat(Bits<4>{1}, Bits<4>(2), Bits<4>(3))), Bits<12>>);
186206
static_assert(concat(Bits<4>{1}, Bits<4>(2), Bits<4>(3)) == Bits<12>(0x123));
187207
}

backends/cpp_hart_gen/lib/constexpr_pass.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ def constexpr?(symtab)
1616
return true if sym.nil? # assuming undefined syms are local (be sure to type check first!!)
1717
return true if sym.is_a?(Type)
1818

19-
!sym.type.global?
19+
if sym.param?
20+
symtab.cfg_arch.params_with_value.any? { |p| p.name == text_value }
21+
else
22+
!sym.type.global?
23+
end
2024
end
2125
end
2226
class PcAssignmentAst

backends/cpp_hart_gen/lib/gen_cpp.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,10 @@ def gen_cpp(symtab, indent = 0, indent_spaces: 2)
300300
var = symtab.get(text_value)
301301

302302
if !var.nil? && var.param?
303-
if symtab.cfg_arch.params_without_value.any? { |p| p.name == text_value }
304-
"#{' ' * indent}__UDB_RUNTIME_PARAM(#{text_value})"
305-
else
303+
if constexpr?(symtab)
306304
"#{' ' * indent}__UDB_STATIC_PARAM(#{text_value}) /* #{var.value} */"
305+
else
306+
"#{' ' * indent}__UDB_RUNTIME_PARAM(#{text_value})"
307307
end
308308
elsif !var.nil? && var.type.global?
309309
if var.type.const?
@@ -457,7 +457,11 @@ def gen_cpp(symtab, indent = 0, indent_spaces: 2)
457457

458458
class BinaryExpressionAst
459459
def gen_cpp(symtab, indent = 0, indent_spaces: 2)
460-
"#{' '*indent}(#{lhs.gen_cpp(symtab, 0, indent_spaces:)} #{op} #{rhs.gen_cpp(symtab, 0, indent_spaces:)})"
460+
if op == ">>>"
461+
"#{' '*indent}(#{lhs.gen_cpp(symtab, 0, indent_spaces:)}.sra(#{rhs.gen_cpp(symtab, 0, indent_spaces:)}))"
462+
else
463+
"#{' '*indent}(#{lhs.gen_cpp(symtab, 0, indent_spaces:)} #{op} #{rhs.gen_cpp(symtab, 0, indent_spaces:)})"
464+
end
461465
end
462466
end
463467

backends/cpp_hart_gen/templates/inst.hxx.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ namespace udb {
7979
#define __UDB_MUTABLE_GLOBAL(x) m_parent->x
8080
#define __UDB_CONSTEXPR_FUNC_CALL <%= name_of(:hart, cfg_arch)%>::
8181
#define __UDB_CONST_GLOBAL(F) <%= name_of(:hart, cfg_arch)%>:: F
82+
#define __UDB_XLEN m_parent->xlen()
8283

8384
void execute() override {
8485

@@ -117,6 +118,7 @@ namespace udb {
117118
#undef __UDB_CONSTEXPR_FUNC_CALL
118119
#undef __UDB_SET_PC
119120
#undef __UDB_CONST_GLOBAL
121+
#undef __UDB_XLEN
120122

121123

122124
private:

bin/setup

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,15 @@ else
202202
MAKE="singularity run ${HOME_OPT} ${CONTAINER_PATH} make"
203203
fi
204204

205-
if [ ! -f $ROOT/.git/hooks/pre-commit ]; then
206-
cat << HOOK > $ROOT/.git/hooks/pre-commit
205+
GIT_REPO_ROOT=`git rev-parse --path-format=absolute --git-common-dir | tr -d '\n'`
206+
if [ ! -f $GIT_REPO_ROOT/hooks/pre-commit ]; then
207+
cat << HOOK > $GIT_REPO_ROOT/hooks/pre-commit
207208
#!/usr/bin/env bash
208209
# File generated by pre-commit: https://pre-commit.com
209210
# ID: 138fd403232d2ddd5efb44317e38bf03
210211
211212
# start templated
212-
INSTALL_PYTHON=$ROOT/bin/python
213+
INSTALL_PYTHON=\$(realpath ./bin/python)
213214
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
214215
# end templated
215216
@@ -225,5 +226,5 @@ else
225226
exit 1
226227
fi
227228
HOOK
228-
chmod +x $ROOT/.git/hooks/pre-commit
229+
chmod +x $GIT_REPO_ROOT/hooks/pre-commit
229230
fi

0 commit comments

Comments
 (0)