Skip to content

Commit

Permalink
Change decoder codes to make compliance with Google C++ Coding Style
Browse files Browse the repository at this point in the history
See also: #24
  • Loading branch information
wildlarva committed Jan 21, 2020
1 parent 30e67f7 commit ea03d5f
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 99 deletions.
25 changes: 15 additions & 10 deletions ctest/athrill_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
#include "dt16x2_mcdhelper.hpp"
#include "dt32x1_mcdhelper.hpp"

void setup_decoders()
namespace mcdhelper
{
arm::mcdhelper::setup_decoder();
ab::mcdhelper::setup_decoder();
at::mcdhelper::setup_decoder();
atb::mcdhelper::setup_decoder();
riscv::mcdhelper::setup_decoder();
pc::mcdhelper::setup_decoder();
cc::mcdhelper::setup_decoder();
dt16x2::mcdhelper::setup_decoder();
dt32x1::mcdhelper::setup_decoder();

void SetupDecoders()
{
arm::mcdhelper::SetupDecoder();
ab::mcdhelper::SetupDecoder();
at::mcdhelper::SetupDecoder();
atb::mcdhelper::SetupDecoder();
riscv::mcdhelper::SetupDecoder();
pc::mcdhelper::SetupDecoder();
cc::mcdhelper::SetupDecoder();
dt16x2::mcdhelper::SetupDecoder();
dt32x1::mcdhelper::SetupDecoder();
}

} // namespace mcdhelper
24 changes: 11 additions & 13 deletions ctest/features/step_definitions/mcdecoder_steps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ using cucumber::ScenarioScope;
struct McdCtx
{
std::string decoder_name;
bool result_code;
bool succeeded;
mcdhelper::DecodeResult result;
};

extern void setup_decoders();

BEFORE_ALL()
{
setup_decoders();
mcdhelper::SetupDecoders();
}

GIVEN("^decoding instructions with the decoder \"([^\"]+)\"$")
Expand All @@ -38,36 +36,36 @@ WHEN("^I decode \"([^\"]+)\"$")

// Get the byte length of code
// In hex string, 2 characters correspond to 1 byte
const int byte_char_len = 2;
int byte_length = std::min((int)(code_str.length() / byte_char_len), 4);
const int kByteCharLen = 2;
int byte_length = std::min((int)(code_str.length() / kByteCharLen), 4);

// Convert hex string to integer array
uint8_t codes[4];
for (int i = 0; i < byte_length; i++)
{
codes[i] = std::stoul(code_str.substr(i * byte_char_len, byte_char_len), nullptr, 16);
codes[i] = std::stoul(code_str.substr(i * kByteCharLen, kByteCharLen), nullptr, 16);
}

// Decode
mcdhelper::DecodeRequest request;
request.decoder_name = context->decoder_name;
request.codes = (const uint8_t *)&codes[0];
request.codes = (const uint8_t*)&codes[0];

context->result_code = mcdhelper::decode_instruction(request, &context->result);
context->succeeded = mcdhelper::DecodeInstruction(request, &context->result);
}

THEN("^the decoding should be succeeded$")
{
ScenarioScope<McdCtx> context;

EXPECT_TRUE(context->result_code);
EXPECT_TRUE(context->succeeded);
}

THEN("^the decoding should be failed$")
{
ScenarioScope<McdCtx> context;

EXPECT_FALSE(context->result_code);
EXPECT_FALSE(context->succeeded);
}

THEN("^the instruction should be \"([^\"]+)\"$")
Expand Down Expand Up @@ -97,8 +95,8 @@ THEN("^the fields \"([^\"]+)\" should be \"([^\"]+)\"$")
// Test the fields
for (int i = 0; i < fields.size(); i++)
{
const std::string &field = fields[i];
const std::string &str_expected_value = str_expected_values[i];
const std::string& field = fields[i];
const std::string& str_expected_value = str_expected_values[i];
uint32_t expected_value = std::stoul(str_expected_value, nullptr, 16);

EXPECT_EQ(expected_value, context->result.fields[field]);
Expand Down
25 changes: 15 additions & 10 deletions ctest/mcdecoder_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
#include "dt16x2_mcdhelper.hpp"
#include "dt32x1_mcdhelper.hpp"

void setup_decoders()
namespace mcdhelper
{
arm::mcdhelper::setup_decoder();
ab::mcdhelper::setup_decoder();
at::mcdhelper::setup_decoder();
atb::mcdhelper::setup_decoder();
riscv::mcdhelper::setup_decoder();
pc::mcdhelper::setup_decoder();
cc::mcdhelper::setup_decoder();
dt16x2::mcdhelper::setup_decoder();
dt32x1::mcdhelper::setup_decoder();

void SetupDecoders()
{
arm::mcdhelper::SetupDecoder();
ab::mcdhelper::SetupDecoder();
at::mcdhelper::SetupDecoder();
atb::mcdhelper::SetupDecoder();
riscv::mcdhelper::SetupDecoder();
pc::mcdhelper::SetupDecoder();
cc::mcdhelper::SetupDecoder();
dt16x2::mcdhelper::SetupDecoder();
dt32x1::mcdhelper::SetupDecoder();
}

} // namespace mcdhelper
8 changes: 4 additions & 4 deletions ctest/mcdhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace mcdhelper
{

#pragma region Global variables
#pragma region Internal global variables

static std::map<std::string, Decoder> decoders;

#pragma endregion Global variables
#pragma endregion Internal global variables

#pragma region External functions

bool decode_instruction(const DecodeRequest &request, DecodeResult *result)
bool DecodeInstruction(const DecodeRequest& request, DecodeResult* result)
{
std::map<std::string, Decoder>::iterator pair = decoders.find(request.decoder_name);
if (pair == decoders.end())
Expand All @@ -22,7 +22,7 @@ bool decode_instruction(const DecodeRequest &request, DecodeResult *result)
return pair->second.decode_function(request, result);
}

void regist_decoder(const Decoder &decoder)
void RegistDecoder(const Decoder& decoder)
{
decoders[decoder.decoder_name] = decoder;
}
Expand Down
60 changes: 43 additions & 17 deletions ctest/mcdhelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,60 @@
namespace mcdhelper
{

#pragma region Data types
#pragma region Types

typedef struct
/** Decoding request */
struct DecodeRequest
{
std::string decoder_name; // Name of a decoder
const uint8_t *codes; // Codes to be input
} DecodeRequest;
std::string decoder_name; /** Name of a decoder */
const uint8_t* codes; /** Codes to be input */
};

typedef struct
/** Decoding result */
struct DecodeResult
{
std::string instruction_name; // Name of a matched instruction
std::map<std::string, uint32_t> fields; // Decoded field values
} DecodeResult;
std::string instruction_name; /** Name of a matched instruction */
std::map<std::string, uint32_t> fields; /** Decoded field values */
};

typedef bool (*DecodeFunction)(const DecodeRequest &request, DecodeResult *result);
/**
* Decoding function
*
* @param request Decoding request
* @param result Decoding result
* @return True if decoding succeeded. False otherwise
*/
typedef bool (*DecodeFunction)(const DecodeRequest& request, DecodeResult* result);

typedef struct
/** Decoder information */
struct Decoder
{
std::string decoder_name; // Name of a decoder
DecodeFunction decode_function; // Function to decode an instruction
} Decoder;
std::string decoder_name; /** Name of a decoder */
DecodeFunction decode_function; /** Function to decode an instruction */
};

#pragma endregion Data types
#pragma endregion Types

#pragma region Functions

extern bool decode_instruction(const DecodeRequest &request, DecodeResult *result);
extern void regist_decoder(const Decoder &decoder);
/**
* Decode an instruction
*
* @param request Decoding request
* @param result Decoding result
* @return True if decoding succeeded. False otherwise
*/
extern bool DecodeInstruction(const DecodeRequest& request, DecodeResult* result);

/**
* Register a decoder
*
* @param decoder Decoder to be registered
*/
extern void RegistDecoder(const Decoder& decoder);

/** Hook to setup decoders */
extern void SetupDecoders();

#pragma endregion Functions

Expand Down
17 changes: 9 additions & 8 deletions ctest/templates/athrill_helper/{{ns}}mcdhelper.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "{{ ns }}mcdhelper.hpp"

#include "mcdhelper.hpp"

extern "C" {
Expand All @@ -10,26 +12,25 @@ namespace mcdhelper {
#pragma region Internal functions

{% for inst in instruction_decoders %}
static void convert_result_{{ inst.name }}(const {{ ns }}OpDecodedCodeType& decoded_code, ::mcdhelper::DecodeResult* result) {
static void ConvertResult_{{ inst.name }}(const {{ ns }}OpDecodedCodeType& decoded_code, ::mcdhelper::DecodeResult* result) {
result->instruction_name = "{{ inst.name }}";
{% for field in inst.field_decoders %}
result->fields["{{ field.name }}"] = decoded_code.code.{{ inst.name }}.{{ field.name }};
{% endfor %}
}
{% endfor %}

static bool decode_instruction(const ::mcdhelper::DecodeRequest& request, ::mcdhelper::DecodeResult* result) {
static bool DecodeInstruction(const ::mcdhelper::DecodeRequest& request, ::mcdhelper::DecodeResult* result) {
{{ ns }}uint16* code = ({{ ns }}uint16*) request.codes;
{{ ns }}OpDecodedCodeType decoded_code;
{{ ns }}OperationCodeType optype;
int result_code;

result_code = {{ ns }}op_parse(code, &decoded_code, &optype);
int result_code = {{ ns }}op_parse(code, &decoded_code, &optype);

switch (optype.code_id) {
{% for inst in instruction_decoders %}
case {{ ns }}OpCodeId_{{ inst.name }}:
convert_result_{{ inst.name }}(decoded_code, result);
ConvertResult_{{ inst.name }}(decoded_code, result);
break;
{% endfor %}
}
Expand All @@ -41,13 +42,13 @@ static bool decode_instruction(const ::mcdhelper::DecodeRequest& request, ::mcdh

#pragma region External functions

void setup_decoder(void) {
void SetupDecoder(void) {
::mcdhelper::Decoder decoder = {
"{{ mcdecoder.namespace }}",
decode_instruction,
DecodeInstruction,
};

::mcdhelper::regist_decoder(decoder);
::mcdhelper::RegistDecoder(decoder);
}

#pragma endregion External functions
Expand Down
3 changes: 2 additions & 1 deletion ctest/templates/athrill_helper/{{ns}}mcdhelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace mcdhelper {

#pragma region Functions

extern void setup_decoder(void);
/** Setup a decoder */
extern void SetupDecoder(void);

#pragma endregion Functions

Expand Down
20 changes: 9 additions & 11 deletions ctest/templates/mcdecoder_helper/{{ns}}mcdhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,43 @@ namespace mcdhelper {
#pragma region Internal functions

{% for inst in instruction_decoders -%}
static void convert_result_{{ inst.name }}(const {{ ns }}DecodeResult& concrete_result, ::mcdhelper::DecodeResult* result) {
static void ConvertResult_{{ inst.name }}(const {{ ns }}DecodeResult& concrete_result, ::mcdhelper::DecodeResult* result) {
result->instruction_name = "{{ inst.name }}";
{% for field in inst.field_decoders -%}
result->fields["{{ field.name }}"] = concrete_result.instruction.{{ inst.name }}.{{ field.name }};
{% endfor %}
}
{% endfor %}

static bool decode_instruction(const ::mcdhelper::DecodeRequest& request, ::mcdhelper::DecodeResult* result) {
static bool DecodeInstruction(const ::mcdhelper::DecodeRequest& request, ::mcdhelper::DecodeResult* result) {
{{ ns }}DecodeRequest concrete_request;
{{ ns }}DecodeResult concrete_result;
int result_code;

concrete_request.codes = request.codes;

result_code = {{ ns }}decode_instruction(&concrete_request, &concrete_result);
{{ ns }}DecodeResult concrete_result;
bool succeeded = {{ ns }}DecodeInstruction(&concrete_request, &concrete_result);

switch (concrete_result.instruction_id) {
{% for inst in instruction_decoders -%}
case {{ ns }}InstructionId_k_{{ inst.name }}:
convert_result_{{ inst.name }}(concrete_result, result);
ConvertResult_{{ inst.name }}(concrete_result, result);
break;
{% endfor %}
}

return result_code;
return succeeded;
}

#pragma endregion Internal functions

#pragma region External functions

void setup_decoder(void) {
void SetupDecoder(void) {
::mcdhelper::Decoder decoder = {
"{{ mcdecoder.namespace }}",
decode_instruction,
DecodeInstruction,
};

::mcdhelper::regist_decoder(decoder);
::mcdhelper::RegistDecoder(decoder);
}

#pragma endregion External functions
Expand Down
3 changes: 2 additions & 1 deletion ctest/templates/mcdecoder_helper/{{ns}}mcdhelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace mcdhelper {

#pragma region Functions

extern void setup_decoder(void);
/** Setup a decoder */
extern void SetupDecoder(void);

#pragma endregion Functions

Expand Down
Loading

0 comments on commit ea03d5f

Please sign in to comment.