Skip to content

Instantly share code, notes, and snippets.

@motonacciu
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motonacciu/9844393 to your computer and use it in GitHub Desktop.
Save motonacciu/9844393 to your computer and use it in GitHub Desktop.
# 1 "main.c"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "main.c"
typedef enum
{
# 1 "operators.def" 1
# 11 "operators.def"
add,
sub,
bra,
# 8 "main.cpp" 2
} operator_t;
typedef enum
{
reg = 0x00001,
imm = 0x00010,
lab = 0x00100,
} type_t;
constexpr inline unsigned option()
{
return 0u;
}
template <class H, class... ARGS>
constexpr inline unsigned option(const H& head, const ARGS&... args)
{
return head | option(args...);
}
inline bool is_valid_operator(operator_t opcode, unsigned argidx, type_t type)
{
switch(opcode)
{
# 43 "main.cpp"
# 1 "operators.def" 1
# 11 "operators.def"
case add: { if (argidx >= 3) { return false; } constexpr std::array<unsigned, 3> types{ reg, reg, (option(reg, imm)) }; return types[argidx] & type ? true : false; }
case sub: { if (argidx >= 3) { return false; } constexpr std::array<unsigned, 3> types{ reg, reg, (option(reg, imm)) }; return types[argidx] & type ? true : false; }
case bra: { if (argidx >= 2) { return false; } constexpr std::array<unsigned, 2> types{ reg, lab }; return types[argidx] & type ? true : false; }
# 44 "main.cpp" 2
}
}
int main(int argc, char* argv[])
{
std::cout << "Is 'sub(_,_,_,reg)' valid? "
<< std::boolalpha << is_valid_operator(sub, 3, reg) << std::endl;
std::cout << "Is 'add(_,reg,_)' valid? "
<< std::boolalpha << is_valid_operator(add, 1, reg) << std::endl;
std::cout << "Is 'add(_,imm,_)' valid? "
<< std::boolalpha << is_valid_operator(add, 1, imm) << std::endl;
std::cout << "Is 'sub(_,_,imm)' valid? "
<< std::boolalpha << is_valid_operator(sub, 2, imm) << std::endl;
std::cout << "Is 'sub(_,_,lab)' valid? "
<< std::boolalpha << is_valid_operator(sub, 2, lab) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment