Skip to content

Commit 6701795

Browse files
authored
Merge pull request #37 from OFFTKP/copt
Implement automatic compression mode
2 parents 303056b + 5b72c34 commit 6701795

6 files changed

Lines changed: 993 additions & 1 deletion

File tree

include/biscuit/assembler.hpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,77 @@
1212

1313
namespace biscuit {
1414

15+
/**
16+
* Biscuit can attempt to optimize emitted assembly. These optimization modes are disabled by default.
17+
* To enable an optimization mode, use the EnableOptimization function:
18+
*
19+
* @code{.cpp}
20+
* biscuit::Assembler as;
21+
* as.EnableOptimization(Optimization::AutoCompress);
22+
* @endcode
23+
*
24+
* In some cases, it may be preferrable to disable an optimization for a short while. This can be done like so:
25+
*
26+
* @code{.cpp}
27+
* as.DisableOptimization(Optimization::AutoCompress);
28+
* // ...
29+
* as.EnableOptimization(Optimization::AutoCompress);
30+
* @endcode
31+
*
32+
* The Optimization enum can be used as a bitmask, which means it is possible to enable or disable multiple optimizations at once:
33+
*
34+
* @code{.cpp}
35+
* as.EnableOptimization(Optimization::AutoCompress | Optimization::Placeholder);
36+
* @endcode
37+
*/
38+
enum class Optimization : uint32_t {
39+
None = 0,
40+
41+
/**
42+
* Automatically converts instructions to their compressed 2-byte form whenever possible.
43+
* For example, this optimization mode will convert a MV instruction to a C.MV instruction
44+
* as long as rd and rs are not the zero register.
45+
*/
46+
AutoCompress = 1,
47+
};
48+
49+
constexpr Optimization operator|(Optimization lhs, Optimization rhs) {
50+
return static_cast<Optimization>(
51+
static_cast<std::underlying_type_t<Optimization>>(lhs) | static_cast<std::underlying_type_t<Optimization>>(rhs)
52+
);
53+
}
54+
55+
constexpr Optimization operator&(Optimization lhs, Optimization rhs) {
56+
return static_cast<Optimization>(
57+
static_cast<std::underlying_type_t<Optimization>>(lhs) & static_cast<std::underlying_type_t<Optimization>>(rhs)
58+
);
59+
}
60+
61+
constexpr Optimization operator^(Optimization lhs, Optimization rhs) {
62+
return static_cast<Optimization>(
63+
static_cast<std::underlying_type_t<Optimization>>(lhs) ^ static_cast<std::underlying_type_t<Optimization>>(rhs)
64+
);
65+
}
66+
67+
constexpr Optimization operator~(Optimization opt) {
68+
return static_cast<Optimization>(~static_cast<std::underlying_type_t<Optimization>>(opt));
69+
}
70+
71+
constexpr Optimization& operator|=(Optimization& lhs, Optimization rhs) {
72+
lhs = lhs | rhs;
73+
return lhs;
74+
}
75+
76+
constexpr Optimization& operator&=(Optimization& lhs, Optimization rhs) {
77+
lhs = lhs & rhs;
78+
return lhs;
79+
}
80+
81+
constexpr Optimization& operator^=(Optimization& lhs, Optimization rhs) {
82+
lhs = lhs ^ rhs;
83+
return lhs;
84+
}
85+
1586
/**
1687
* Defines the set of features that a particular assembler instance
1788
* would like to assemble for.
@@ -150,6 +221,35 @@ class Assembler {
150221
return m_buffer.GetOffsetPointer(offset);
151222
}
152223

224+
/**
225+
* Checks whether the optimizations set in a bitmask are enabled
226+
*
227+
* @param opt The bitmask to check.
228+
*
229+
* @returns Whether all of the optimizations in the bitmask were enabled
230+
*/
231+
[[nodiscard]] bool IsOptimizationEnabled(Optimization opt) const noexcept {
232+
return (m_optimizations & opt) == opt;
233+
}
234+
235+
/**
236+
* Enables the optimizations set in a bitmask
237+
*
238+
* @param opt The bitmask to enable.
239+
*/
240+
void EnableOptimization(Optimization opt) noexcept {
241+
m_optimizations |= opt;
242+
}
243+
244+
/**
245+
* Disables the optimizations set in a bitmask
246+
*
247+
* @param opt The bitmask to disable.
248+
*/
249+
void DisableOptimization(Optimization opt) noexcept {
250+
m_optimizations &= ~opt;
251+
}
252+
153253
/**
154254
* Binds a label to the current offset within the code buffer
155255
*
@@ -1622,6 +1722,7 @@ class Assembler {
16221722

16231723
CodeBuffer m_buffer;
16241724
ArchFeature m_features = ArchFeature::RV64;
1725+
Optimization m_optimizations = Optimization::None;
16251726
};
16261727

16271728
} // namespace biscuit

0 commit comments

Comments
 (0)