Skip to content

Instantly share code, notes, and snippets.

template <typename curr_machine_state, typename all_opcodes, typename opcodes = all_opcodes>
struct execute_impl
{
using next_machine_state = execute_instruction<curr_machine_state, opcodes>;
using next_opcodes = get_next_instruction<next_machine_state, all_opcodes>;
static constexpr auto ret_val = execute_impl<next_machine_state, all_opcodes, next_opcodes>::ret_val;
};
template <typename curr_machine_state, typename all_opcodes, size_t ...rest_of_instructions>
struct execute_impl<curr_machine_state,
all_opcodes,
values_container<inst::to_size<inst::id_t::EXIT>, rest_of_instructions...>>
{
using regs_state = typename to_machine_state<curr_machine_state>::registers_state_t;
static constexpr auto ret_val = get_reg<regs_state, regs::id_t::EAX>;
};
namespace details
{
template <typename tokens, typename instructions_t = values_container<>>
struct assemble_impl;
template <typename curr_instructions>
struct assemble_impl<tuple<>, curr_instructions>
{
using instructions_result = curr_instructions;
};
template <char ...label_chars, typename ...rest_of_tokens, typename labels, typename ...current_tokens>
struct substitute_labels_impl<
tuple<string<'.', label_chars...>, rest_of_tokens...>,
labels,
tuple<current_tokens...>>
{
static constexpr auto ip = labels_get_ip<labels, string<'.', label_chars...>>;
using str_ip = string_from_int<ip>;
using substitued = substitute_labels_impl<
template <size_t current_ip, typename current_token, typename ...rest_of_tokens, typename ...result_tokens, typename result_labels>
struct extract_labels_impl<
tuple<current_token, rest_of_tokens...>,
current_ip,
tuple<result_tokens...>,
result_labels>
{
using instruction = instruction_match<tuple<current_token, rest_of_tokens...>>;
using next_tokens = tuple_merge<tuple<result_tokens...>, typename instruction::instruction_tokens>;
static constexpr auto nex_ip = current_ip + instruction::eip_change;
tuple<rest_of_tokens...> //tokens for next iteration<br>
current_ip //this case is for label declaration match, and they doesn't count as a instructions thus we don't increase instruction pointer<br>
tuple<current_tokens...> //result tokens without current token, because current token is a label declaration, which we want to erase<br>
tuple<current_labels..., label_metadata<string<'.', label_chars...>, current_ip>> //this one is more interesting. Here we appends labels with label we just discovered. We're creating tuple with labels that we've already detect plus this one that we just found.
template <size_t current_ip, char ...label_chars, typename ...rest_of_tokens, typename ...current_tokens, typename ...current_labels>
struct extract_labels_impl<
tuple<string<':', label_chars...>, rest_of_tokens...>,
current_ip,
tuple<current_tokens...>,
tuple<current_labels...>>
{
using extracted = extract_labels_impl<
tuple<rest_of_tokens...>,
current_ip,
template <size_t ip, typename final_tokens, typename final_labels>
struct extract_labels_impl<tuple<>, ip, final_tokens, final_labels>
{
using tokens = final_tokens;
using labels = final_labels;
};
template <typename, size_t current_ip = 0, typename final_tokens = tuple<>, typename final_labels = tuple<>>
struct extract_labels_impl;
struct matcher_impl<tuple<
tokens::tok_mov, // mov
reg_token, // eax
tokens::tok_comma, // , Yes. Comma is separate token
mem_size_token, // DWORD
tokens::tok_ptr, // PTR
tokens::tok_square_bracket_open, // [ YES. Oppening square bracket is also separate token
mem_ptr_reg, // ecx
plus_minus_token, // +
mem_ptr_const, // 4