From 1479007a335ab43af46f527d0543e254fc2a8e86 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 2 Apr 2020 18:22:04 -0700 Subject: [PATCH] Introduce Instruction enum in asmap --- src/util/asmap.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp index 60bd27bf9..2ffd61820 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -36,10 +36,18 @@ uint32_t DecodeBits(std::vector::const_iterator& bitpos, const std::vector return -1; } -const std::vector TYPE_BIT_SIZES{0, 0, 1}; -uint32_t DecodeType(std::vector::const_iterator& bitpos, const std::vector::const_iterator& endpos) +enum class Instruction : uint32_t { - return DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES); + RETURN = 0, + JUMP = 1, + MATCH = 2, + DEFAULT = 3, +}; + +const std::vector TYPE_BIT_SIZES{0, 0, 1}; +Instruction DecodeType(std::vector::const_iterator& bitpos, const std::vector::const_iterator& endpos) +{ + return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES)); } const std::vector ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; @@ -70,12 +78,13 @@ uint32_t Interpret(const std::vector &asmap, const std::vector &ip) const std::vector::const_iterator endpos = asmap.end(); uint8_t bits = ip.size(); uint32_t default_asn = 0; - uint32_t opcode, jump, match, matchlen; + uint32_t jump, match, matchlen; + Instruction opcode; while (pos != endpos) { opcode = DecodeType(pos, endpos); - if (opcode == 0) { + if (opcode == Instruction::RETURN) { return DecodeASN(pos, endpos); - } else if (opcode == 1) { + } else if (opcode == Instruction::JUMP) { jump = DecodeJump(pos, endpos); if (bits == 0) break; if (ip[ip.size() - bits]) { @@ -83,7 +92,7 @@ uint32_t Interpret(const std::vector &asmap, const std::vector &ip) pos += jump; } bits--; - } else if (opcode == 2) { + } else if (opcode == Instruction::MATCH) { match = DecodeMatch(pos, endpos); matchlen = CountBits(match) - 1; for (uint32_t bit = 0; bit < matchlen; bit++) { @@ -93,7 +102,7 @@ uint32_t Interpret(const std::vector &asmap, const std::vector &ip) } bits--; } - } else if (opcode == 3) { + } else if (opcode == Instruction::DEFAULT) { default_asn = DecodeASN(pos, endpos); } else { break;