Skip to content

Commit

Permalink
Proper op-code metadata handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
asoffer committed Oct 2, 2023
1 parent efbb663 commit a2b2986
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions jasmin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ cc_library(
"@asoffer_nth//nth/debug",
"@asoffer_nth//nth/meta:sequence",
"@asoffer_nth//nth/meta:type",
"@com_google_absl//absl/container:flat_hash_map",
],
)

Expand Down
24 changes: 19 additions & 5 deletions jasmin/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stack>
#include <type_traits>

#include "absl/container/flat_hash_map.h"
#include "jasmin/call_stack.h"
#include "jasmin/execution_state.h"
#include "jasmin/internal/function_base.h"
Expand Down Expand Up @@ -272,7 +273,7 @@ struct StackMachineInstruction {
std::apply(
Inst::execute,
std::tuple<ValueStack &, typename Inst::execution_state &,
Ts...>{
typename Inst::function_state &, Ts...>{
value_stack,
state->exec_state
->template get<typename Inst::execution_state>(),
Expand Down Expand Up @@ -517,8 +518,12 @@ struct MakeInstructionSet : InstructionSetBase {
// Returns the number of instructions in the instruction set.
static constexpr size_t size() { return sizeof...(Is); }

static constexpr struct OpCodeMetadata OpCodeMetadata(Value v) {
return Metadata[v.as<uint64_t>()];
// Returns the `OpCodeMetadata` struct corresponding to the op-code as
// specified in the byte-code.
static struct OpCodeMetadata OpCodeMetadata(Value v) {
auto iter = Metadata.find(v.as<exec_fn_type>());
NTH_REQUIRE((v.when(internal::harden)), iter != Metadata.end());
return iter->second;
}

// Returns a `uint64_t` indicating the op-code for the given template
Expand Down Expand Up @@ -546,8 +551,8 @@ struct MakeInstructionSet : InstructionSetBase {
static constexpr exec_fn_type table[sizeof...(Is)] = {
&Is::template ExecuteImpl<MakeInstructionSet>...};

static constexpr struct OpCodeMetadata Metadata[sizeof...(Is)] = {
OpCodeMetadataFor<Is>()...};
static absl::flat_hash_map<exec_fn_type, struct OpCodeMetadata> const
Metadata;

template <nth::any_of<Is...> I>
static constexpr uint64_t OpCodeForImpl() {
Expand Down Expand Up @@ -588,6 +593,15 @@ constexpr auto FlattenInstructionList(nth::Sequence auto unprocessed,
constexpr auto BuiltinInstructionList =
nth::type_sequence<Call, Jump, JumpIf, Return>;

template <Instruction... Is>
absl::flat_hash_map<exec_fn_type, OpCodeMetadata> const
MakeInstructionSet<Is...>::Metadata = [] {
absl::flat_hash_map<exec_fn_type, struct OpCodeMetadata> result;
(result.emplace(&Is::template ExecuteImpl<self_type>,
OpCodeMetadataFor<Is>()),
...);
return result;
}();
} // namespace internal

template <internal::InstructionOrInstructionSet... Is>
Expand Down
9 changes: 9 additions & 0 deletions jasmin/instruction_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ TEST(Instruction, OpCodeMetadata) {
EXPECT_EQ(Set::OpCodeMetadataFor<F>().immediate_value_count, 1);
EXPECT_EQ(Set::OpCodeMetadataFor<EF>().op_code_value, 7);
EXPECT_EQ(Set::OpCodeMetadataFor<EF>().immediate_value_count, 1);

EXPECT_EQ(Set::OpCodeMetadata(None::ExecuteImpl<Set>),
Set::OpCodeMetadataFor<None>());
EXPECT_EQ(Set::OpCodeMetadata(E::ExecuteImpl<Set>),
Set::OpCodeMetadataFor<E>());
EXPECT_EQ(Set::OpCodeMetadata(F::ExecuteImpl<Set>),
Set::OpCodeMetadataFor<F>());
EXPECT_EQ(Set::OpCodeMetadata(EF::ExecuteImpl<Set>),
Set::OpCodeMetadataFor<EF>());
}

} // namespace
Expand Down
5 changes: 5 additions & 0 deletions jasmin/op_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ struct OpCodeRange {

// Represents information describing an op-code.
struct OpCodeMetadata {
friend bool operator==(OpCodeMetadata const &,
OpCodeMetadata const &) = default;
friend bool operator!=(OpCodeMetadata const &,
OpCodeMetadata const &) = default;

size_t op_code_value;
size_t immediate_value_count;
};
Expand Down

0 comments on commit a2b2986

Please sign in to comment.