From d0036044ffc73ce258141ca4cc2c9c892b818b80 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 16 Oct 2024 20:38:39 -0300 Subject: [PATCH] refactor: fix readability-else-after-return lint errors --- .clang-tidy | 1 - src/back-merkle-tree.cpp | 3 +- src/base64.cpp | 8 +- src/clint.cpp | 4 +- src/clua-cartesi.cpp | 27 +++-- src/complete-merkle-tree.cpp | 3 +- src/interpret.cpp | 98 ++++++++--------- src/jsonrpc-machine-c-api.cpp | 3 +- src/jsonrpc-remote-machine.cpp | 12 ++- src/jsonrpc-virtual-machine.cpp | 9 +- src/machine-merkle-tree.cpp | 6 +- src/machine.cpp | 27 +++-- src/merkle-tree-hash.cpp | 7 +- src/os.cpp | 9 +- src/pma.cpp | 5 +- src/pma.h | 3 +- src/soft-float.h | 112 +++++++++---------- src/translate-virtual-address.h | 3 +- src/uarch-bridge.h | 12 ++- src/uarch-step.cpp | 156 ++++++++++++++++++--------- src/virtio-console.cpp | 11 +- src/virtio-net-carrier-tuntap.cpp | 6 +- src/virtio-net.cpp | 11 +- src/virtio-p9fs.cpp | 17 ++- tests/misc/test-merkle-tree-hash.cpp | 13 ++- 25 files changed, 290 insertions(+), 276 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 68ec3c8c3..38ab2abb8 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -37,7 +37,6 @@ Checks: >- -performance-enum-size, portability*, readability*, - -readability-else-after-return, -readability-function-cognitive-complexity, -readability-identifier-length, -readability-magic-numbers, diff --git a/src/back-merkle-tree.cpp b/src/back-merkle-tree.cpp index 639fae2a3..027edccb7 100644 --- a/src/back-merkle-tree.cpp +++ b/src/back-merkle-tree.cpp @@ -134,9 +134,8 @@ back_merkle_tree::hash_type back_merkle_tree::get_root_hash() const { } } return root; - } else { - return m_context[depth]; } + return m_context[depth]; } back_merkle_tree::proof_type back_merkle_tree::get_next_leaf_proof() const { diff --git a/src/base64.cpp b/src/base64.cpp index 256b717e6..10da6caf8 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -108,9 +108,8 @@ static size_t b64decode(uint8_t c, uint8_t *input, size_t size, std::ostringstre if (b64unbase[c] > 64) { if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r') { // ignore whitespace characters return size; - } else { // throw an error for invalid characters - throw std::domain_error(std::string("invalid base64 character code ") + std::to_string(c)); - } + } // throw an error for invalid characters + throw std::domain_error(std::string("invalid base64 character code ") + std::to_string(c)); } input[size++] = c; // decode atom @@ -142,9 +141,8 @@ static size_t b64decode(uint8_t c, uint8_t *input, size_t size, std::ostringstre sout << std::string_view(reinterpret_cast(decoded), valid); return 0; // need more data - } else { - return size; } + return size; } std::string encode_base64(const std::string_view &input) { diff --git a/src/clint.cpp b/src/clint.cpp index f9536990f..ff209a691 100644 --- a/src/clint.cpp +++ b/src/clint.cpp @@ -91,9 +91,9 @@ static execute_status clint_write(void *context, i_device_state_access *a, uint6 if ((val & 1) != 0) { a->set_mip(MIP_MSIP_MASK); return execute_status::success_and_serve_interrupts; - } else { - a->reset_mip(MIP_MSIP_MASK); } + a->reset_mip(MIP_MSIP_MASK); + return execute_status::success; } return execute_status::failure; diff --git a/src/clua-cartesi.cpp b/src/clua-cartesi.cpp index f6b89d557..d7c681921 100644 --- a/src/clua-cartesi.cpp +++ b/src/clua-cartesi.cpp @@ -75,21 +75,20 @@ static int cartesi_mod_keccak(lua_State *L) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) lua_pushlstring(L, reinterpret_cast(hash.data()), hash.size()); return 1; - } else { - h.begin(); - size_t len1 = 0; - const char *hash1 = luaL_checklstring(L, 1, &len1); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - h.add_data(reinterpret_cast(hash1), len1); - size_t len2 = 0; - const char *hash2 = luaL_optlstring(L, 2, "", &len2); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - h.add_data(reinterpret_cast(hash2), len2); - h.end(hash); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - lua_pushlstring(L, reinterpret_cast(hash.data()), hash.size()); - return 1; } + h.begin(); + size_t len1 = 0; + const char *hash1 = luaL_checklstring(L, 1, &len1); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + h.add_data(reinterpret_cast(hash1), len1); + size_t len2 = 0; + const char *hash2 = luaL_optlstring(L, 2, "", &len2); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + h.add_data(reinterpret_cast(hash2), len2); + h.end(hash); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + lua_pushlstring(L, reinterpret_cast(hash.data()), hash.size()); + return 1; } static int cartesi_mod_tobase64(lua_State *L) try { diff --git a/src/complete-merkle-tree.cpp b/src/complete-merkle-tree.cpp index 03bab6dfc..6ca632863 100644 --- a/src/complete-merkle-tree.cpp +++ b/src/complete-merkle-tree.cpp @@ -99,9 +99,8 @@ const complete_merkle_tree::hash_type &complete_merkle_tree::get_node_hash(addre } if (address < level.size()) { return level[address]; - } else { - return m_pristine.get_hash(log2_size); } + return m_pristine.get_hash(log2_size); } void complete_merkle_tree::bubble_up() { diff --git a/src/interpret.cpp b/src/interpret.cpp index d66d67fa8..6a915d1d0 100644 --- a/src/interpret.cpp +++ b/src/interpret.cpp @@ -842,7 +842,8 @@ static NO_INLINE std::pair read_virtual_memory_slow(STATE_ACCESS const uint64_t hoffset = vaddr & PAGE_OFFSET_MASK; a.read_memory_word(paddr, hpage, hoffset, pval); return {true, pc}; - } else if (likely(pma.get_istart_IO())) { + } + if (likely(pma.get_istart_IO())) { const uint64_t offset = paddr - pma.get_start(); uint64_t val{}; // If we do not know how to read, we treat this as a PMA violation @@ -917,7 +918,8 @@ static NO_INLINE std::pair write_virtual_memory_slow(S const uint64_t hoffset = vaddr & PAGE_OFFSET_MASK; a.write_memory_word(paddr, hpage, hoffset, static_cast(val64)); return {execute_status::success, pc}; - } else if (likely(pma.get_istart_IO())) { + } + if (likely(pma.get_istart_IO())) { const uint64_t offset = paddr - pma.get_start(); auto status = a.write_device(pma, mcycle, offset, static_cast(static_cast(val64)), log2_size::value); @@ -1192,9 +1194,8 @@ static FORCE_INLINE execute_status execute_AMOMIN_W(STATE_ACCESS &a, uint64_t &p return execute_AMO(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t { if (valm < valr) { return valm; - } else { - return valr; } + return valr; }); } @@ -1205,9 +1206,8 @@ static FORCE_INLINE execute_status execute_AMOMAX_W(STATE_ACCESS &a, uint64_t &p return execute_AMO(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t { if (valm > valr) { return valm; - } else { - return valr; } + return valr; }); } @@ -1218,9 +1218,8 @@ static FORCE_INLINE execute_status execute_AMOMINU_W(STATE_ACCESS &a, uint64_t & return execute_AMO(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t { if (static_cast(valm) < static_cast(valr)) { return valm; - } else { - return valr; } + return valr; }); } @@ -1231,9 +1230,8 @@ static FORCE_INLINE execute_status execute_AMOMAXU_W(STATE_ACCESS &a, uint64_t & return execute_AMO(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t { if (static_cast(valm) > static_cast(valr)) { return valm; - } else { - return valr; } + return valr; }); } @@ -1302,9 +1300,8 @@ static FORCE_INLINE execute_status execute_AMOMIN_D(STATE_ACCESS &a, uint64_t &p return execute_AMO(a, pc, mcycle, insn, [](int64_t valm, int64_t valr) -> int64_t { if (valm < valr) { return valm; - } else { - return valr; } + return valr; }); } @@ -1315,9 +1312,8 @@ static FORCE_INLINE execute_status execute_AMOMAX_D(STATE_ACCESS &a, uint64_t &p return execute_AMO(a, pc, mcycle, insn, [](int64_t valm, int64_t valr) -> int64_t { if (valm > valr) { return valm; - } else { - return valr; } + return valr; }); } @@ -1328,9 +1324,8 @@ static FORCE_INLINE execute_status execute_AMOMINU_D(STATE_ACCESS &a, uint64_t & return execute_AMO(a, pc, mcycle, insn, [](uint64_t valm, uint64_t valr) -> uint64_t { if (valm < valr) { return valm; - } else { - return valr; } + return valr; }); } @@ -1341,9 +1336,8 @@ static FORCE_INLINE execute_status execute_AMOMAXU_D(STATE_ACCESS &a, uint64_t & return execute_AMO(a, pc, mcycle, insn, [](uint64_t valm, uint64_t valr) -> uint64_t { if (valm > valr) { return valm; - } else { - return valr; } + return valr; }); } @@ -1433,11 +1427,11 @@ static FORCE_INLINE execute_status execute_DIVW(STATE_ACCESS &a, uint64_t &pc, u auto rs2w = static_cast(rs2); if (unlikely(rs2w == 0)) { return static_cast(-1); - } else if (unlikely(rs2w == -1 && rs1w == (static_cast(1) << (32 - 1)))) { + } + if (unlikely(rs2w == -1 && rs1w == (static_cast(1) << (32 - 1)))) { return static_cast(rs1w); - } else { - return static_cast(rs1w / rs2w); } + return static_cast(rs1w / rs2w); }); } @@ -1450,9 +1444,8 @@ static FORCE_INLINE execute_status execute_DIVUW(STATE_ACCESS &a, uint64_t &pc, auto rs2w = static_cast(rs2); if (unlikely(rs2w == 0)) { return static_cast(-1); - } else { - return static_cast(static_cast(rs1w / rs2w)); } + return static_cast(static_cast(rs1w / rs2w)); }); } @@ -1468,11 +1461,11 @@ static FORCE_INLINE execute_status execute_REMW(STATE_ACCESS &a, uint64_t &pc, u auto rs2w = static_cast(rs2); if (unlikely(rs2w == 0)) { return static_cast(rs1w); - } else if (unlikely(rs2w == -1 && rs1w == (static_cast(1) << (32 - 1)))) { + } + if (unlikely(rs2w == -1 && rs1w == (static_cast(1) << (32 - 1)))) { return static_cast(0); - } else { - return static_cast(rs1w % rs2w); } + return static_cast(rs1w % rs2w); }); } @@ -1491,9 +1484,8 @@ static FORCE_INLINE execute_status execute_REMUW(STATE_ACCESS &a, uint64_t &pc, auto rs2w = static_cast(rs2); if (unlikely(rs2w == 0)) { return static_cast(static_cast(rs1w)); - } else { - return static_cast(static_cast(rs1w % rs2w)); } + return static_cast(static_cast(rs1w % rs2w)); }); } @@ -1524,9 +1516,8 @@ template static inline uint64_t read_csr_cycle(STATE_ACCESS &a, uint64_t mcycle, bool *status) { if (rdcounteren(a, MCOUNTEREN_CY_MASK)) { return read_csr_success(mcycle, status); - } else { - return read_csr_fail(status); } + return read_csr_fail(status); } template @@ -2813,11 +2804,11 @@ static FORCE_INLINE execute_status execute_DIV(STATE_ACCESS &a, uint64_t &pc, ui auto srs2 = static_cast(rs2); if (unlikely(srs2 == 0)) { return static_cast(-1); - } else if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) { + } + if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) { return static_cast(srs1); - } else { - return static_cast(srs1 / srs2); } + return static_cast(srs1 / srs2); }); } @@ -2828,9 +2819,8 @@ static FORCE_INLINE execute_status execute_DIVU(STATE_ACCESS &a, uint64_t &pc, u return execute_arithmetic(a, pc, insn, [](uint64_t rs1, uint64_t rs2) -> uint64_t { if (unlikely(rs2 == 0)) { return static_cast(-1); - } else { - return rs1 / rs2; } + return rs1 / rs2; }); } @@ -2843,11 +2833,11 @@ static FORCE_INLINE execute_status execute_REM(STATE_ACCESS &a, uint64_t &pc, ui auto srs2 = static_cast(rs2); if (unlikely(srs2 == 0)) { return srs1; - } else if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) { + } + if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) { return 0; - } else { - return static_cast(srs1 % srs2); } + return static_cast(srs1 % srs2); }); } @@ -2858,9 +2848,8 @@ static FORCE_INLINE execute_status execute_REMU(STATE_ACCESS &a, uint64_t &pc, u return execute_arithmetic(a, pc, insn, [](uint64_t rs1, uint64_t rs2) -> uint64_t { if (unlikely(rs2 == 0)) { return rs1; - } else { - return rs1 % rs2; } + return rs1 % rs2; }); } @@ -5610,13 +5599,12 @@ static NO_INLINE execute_status interpret_loop(STATE_ACCESS &a, uint64_t mcycle_ if (likely(status == execute_status::success_and_serve_interrupts)) { // We have to break the inner loop to check and serve any pending interrupt immediately break; - } else { // execute_status::success_and_yield or execute_status::success_and_halt - // Commit machine state - a.write_pc(pc); - a.write_mcycle(mcycle); - // Got an interruption that must be handled externally - return status; - } + } // execute_status::success_and_yield or execute_status::success_and_halt + // Commit machine state + a.write_pc(pc); + a.write_mcycle(mcycle); + // Got an interruption that must be handled externally + return status; } } } @@ -5669,16 +5657,18 @@ interpreter_break_reason interpret(STATE_ACCESS &a, uint64_t mcycle_end) { // Detect and return the reason for stopping the interpreter loop if (a.read_iflags_H()) { return interpreter_break_reason::halted; - } else if (a.read_iflags_Y()) { + } + if (a.read_iflags_Y()) { return interpreter_break_reason::yielded_manually; - } else if (a.read_iflags_X()) { + } + if (a.read_iflags_X()) { return interpreter_break_reason::yielded_automatically; - } else if (status == execute_status::success_and_yield) { - return interpreter_break_reason::yielded_softly; - } else { // Reached mcycle_end - assert(a.read_mcycle() == mcycle_end); // LCOV_EXCL_LINE - return interpreter_break_reason::reached_target_mcycle; } + if (status == execute_status::success_and_yield) { + return interpreter_break_reason::yielded_softly; + } // Reached mcycle_end + assert(a.read_mcycle() == mcycle_end); // LCOV_EXCL_LINE + return interpreter_break_reason::reached_target_mcycle; } #ifdef MICROARCHITECTURE diff --git a/src/jsonrpc-machine-c-api.cpp b/src/jsonrpc-machine-c-api.cpp index 5a36e53e2..49e1e2948 100644 --- a/src/jsonrpc-machine-c-api.cpp +++ b/src/jsonrpc-machine-c-api.cpp @@ -172,7 +172,8 @@ cm_error cm_jsonrpc_spawn_server(const char *address, int detach_server, cm_json exit(1); }; return cm_result_success(); // code never reaches here - } else if (grand_child > 0) { // parent and double-fork() succeeded + } + if (grand_child > 0) { // parent and double-fork() succeeded restore_grand_child = true; // make sure grand-child is killed if we fail static THREAD_LOCAL std::string bound_address_storage = endpoint_to_string(a.local_endpoint()); a.close(); diff --git a/src/jsonrpc-remote-machine.cpp b/src/jsonrpc-remote-machine.cpp index a4d91a497..8a8b06a5d 100644 --- a/src/jsonrpc-remote-machine.cpp +++ b/src/jsonrpc-remote-machine.cpp @@ -175,10 +175,12 @@ struct http_session : std::enable_shared_from_this { // Check error code if (ec == asio::error::operation_aborted) { // Operation may be aborted return; - } else if (ec == http::error::end_of_stream) { // This means the connection was closed by the client + } + if (ec == http::error::end_of_stream) { // This means the connection was closed by the client shutdown_send(); return; - } else if (ec) { // Unexpected error + } + if (ec) { // Unexpected error SLOG(error) << "read request error:" << ec.what(); return; } @@ -214,7 +216,8 @@ struct http_session : std::enable_shared_from_this { // Check error code if (ec == asio::error::operation_aborted) { // Operation may be aborted return; - } else if (ec) { // Unexpected error + } + if (ec) { // Unexpected error SLOG(error) << "send response error:" << ec.what(); shutdown_send(); return; @@ -330,7 +333,8 @@ struct http_handler : std::enable_shared_from_this { // Operation may be aborted (e.g rebind() or stop() was called) if (ec == asio::error::operation_aborted) { return; - } else if (ec) { + } + if (ec) { SLOG(error) << local_endpoint << " accept error: " << ec.what(); return; } diff --git a/src/jsonrpc-virtual-machine.cpp b/src/jsonrpc-virtual-machine.cpp index ea85beae0..0ef97d4a5 100644 --- a/src/jsonrpc-virtual-machine.cpp +++ b/src/jsonrpc-virtual-machine.cpp @@ -104,7 +104,8 @@ static std::string json_post(beast::tcp_stream &stream, const std::string &remot stream.connect(remote_endpoint, ec); if (!ec) { // Success break; - } else if (ec == asio::error::interrupted) { + } + if (ec == asio::error::interrupted) { // Retry the operation during interrupts (SIGINT/SIGTERM), // otherwise we may leave dead zombies processes during fork requests. } else { // Unexpected error @@ -150,7 +151,8 @@ static std::string json_post(beast::tcp_stream &stream, const std::string &remot http::write(stream, req, ec); if (!ec) { // Success break; - } else if (ec == asio::error::interrupted) { + } + if (ec == asio::error::interrupted) { // Retry the operation during interrupts (SIGINT/SIGTERM), // otherwise we may leave dead zombies processes during fork requests. } else { // Unexpected error @@ -170,7 +172,8 @@ static std::string json_post(beast::tcp_stream &stream, const std::string &remot http::read(stream, buffer, res_parser, ec); if (!ec) { // Success break; - } else if (ec == asio::error::interrupted) { + } + if (ec == asio::error::interrupted) { // Retry the operation during interrupts (SIGINT/SIGTERM), // otherwise we may leave dead zombies processes during fork requests. } else { // Unexpected error diff --git a/src/machine-merkle-tree.cpp b/src/machine-merkle-tree.cpp index c353419b1..6dd8eaf31 100644 --- a/src/machine-merkle-tree.cpp +++ b/src/machine-merkle-tree.cpp @@ -42,9 +42,8 @@ machine_merkle_tree::tree_node *machine_merkle_tree::get_page_node(address_type auto it = m_page_node_map.find(page_index); if (it != m_page_node_map.end()) { return it->second; - } else { - return nullptr; } + return nullptr; } constexpr machine_merkle_tree::address_type machine_merkle_tree::get_offset_in_page(address_type address) { @@ -334,9 +333,8 @@ bool machine_merkle_tree::verify_tree(hasher_type &h, tree_node *node, int log2_ get_concat_hash(h, get_child_hash(child_log2_size, node, 0), get_child_hash(child_log2_size, node, 1), hash); return hash == node->hash; // Assume page nodes are correct - } else { - return true; } + return true; } machine_merkle_tree::proof_type machine_merkle_tree::get_proof(address_type target_address, int log2_target_size, diff --git a/src/machine.cpp b/src/machine.cpp index 3f06b2e4d..b9b96add6 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -672,14 +672,13 @@ static void store_device_pma(const machine &m, const pma_entry &pma, const std:: auto peek = pma.get_peek(); if (!peek(pma, m, page_start_in_range, &page_data, scratch.get())) { throw std::runtime_error{"peek failed"}; - } else { - if (page_data == nullptr) { - memset(scratch.get(), 0, PMA_PAGE_SIZE); - page_data = scratch.get(); - } - if (fwrite(page_data, 1, PMA_PAGE_SIZE, fp.get()) != PMA_PAGE_SIZE) { - throw std::system_error{errno, std::generic_category(), "error writing to '" + name + "'"}; - } + } + if (page_data == nullptr) { + memset(scratch.get(), 0, PMA_PAGE_SIZE); + page_data = scratch.get(); + } + if (fwrite(page_data, 1, PMA_PAGE_SIZE, fp.get()) != PMA_PAGE_SIZE) { + throw std::system_error{errno, std::generic_category(), "error writing to '" + name + "'"}; } } } @@ -2114,9 +2113,8 @@ machine_merkle_tree::proof_type machine::get_proof(uint64_t address, int log2_si return m_t.get_proof(address, log2_size, page_data); // If proof concerns range bigger than a page, we already have its hash // stored in the tree itself - } else { - return m_t.get_proof(address, log2_size, nullptr); } + return m_t.get_proof(address, log2_size, nullptr); } machine_merkle_tree::proof_type machine::get_proof(uint64_t address, int log2_size) const { @@ -2156,13 +2154,15 @@ void machine::read_memory(uint64_t address, unsigned char *data, uint64_t length if (bytes_to_write == PMA_PAGE_SIZE) { if (!peek(pma, *this, page_address, &page_data, data)) { throw std::runtime_error{"peek failed"}; - } else if (page_data == nullptr) { + } + if (page_data == nullptr) { memset(data, 0, bytes_to_write); } } else { if (!peek(pma, *this, page_address, &page_data, scratch.get())) { throw std::runtime_error{"peek failed"}; - } else if (page_data == nullptr) { + } + if (page_data == nullptr) { memset(data, 0, bytes_to_write); } else { memcpy(data, page_data + shift, bytes_to_write); @@ -2296,9 +2296,8 @@ uint64_t machine::read_word(uint64_t word_address) const { const uint64_t word_start_in_range = (word_address - pma.get_start()) & (PMA_PAGE_SIZE - 1); return aliased_aligned_read(page_data + word_start_in_range); // Otherwise, page is always pristine - } else { - return 0; } + return 0; } void machine::send_cmio_response(uint16_t reason, const unsigned char *data, uint64_t length) { diff --git a/src/merkle-tree-hash.cpp b/src/merkle-tree-hash.cpp index 7d0b32390..2b117bc2f 100644 --- a/src/merkle-tree-hash.cpp +++ b/src/merkle-tree-hash.cpp @@ -136,11 +136,10 @@ static hash_type get_leaf_hash(hasher_type &h, const unsigned char *leaf_data, i get_leaf_hash(h, leaf_data + (1 << (log2_leaf_size - 1)), log2_leaf_size - 1, log2_word_size); get_concat_hash(h, left, right, left); return left; - } else { - hash_type leaf; - get_word_hash(h, leaf_data, log2_word_size, leaf); - return leaf; } + hash_type leaf; + get_word_hash(h, leaf_data, log2_word_size, leaf); + return leaf; } /// \brief Computes the Merkle hash of a leaf of data diff --git a/src/os.cpp b/src/os.cpp index 7fc081715..7711d8e2f 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -874,9 +874,9 @@ int os_double_fork_or_throw(int newpgid) { fd[1] = -1; // we are done and can return to whatever caller wants to do as a child return 0; - } else { // intermediate child, fork either failed or succeeded - exit(0); // intermediate child exits right away - } + } // intermediate child, fork either failed or succeeded + exit(0); // intermediate child exits right away + } else if (ipid > 0) { // still parent (fork succeeded) waitpid(ipid, nullptr, 0); // wait on dead intermediate child so it doesn't become a zombie // set alarm so we can't hang while waiting to read final child pid from pipe @@ -899,9 +899,8 @@ int os_double_fork_or_throw(int newpgid) { auto e = errno; if (e == EINTR) { throw std::runtime_error{"parent gave up waiting for child pid"}; - } else { - throw std::system_error{e, std::generic_category(), "failed to read child pid"}; } + throw std::system_error{e, std::generic_category(), "failed to read child pid"}; } throw std::runtime_error{"failed to read child pid"}; } diff --git a/src/pma.cpp b/src/pma.cpp index c616c8630..0866d9569 100644 --- a/src/pma.cpp +++ b/src/pma.cpp @@ -196,10 +196,9 @@ static bool memory_peek(const pma_entry &pma, const machine &m, uint64_t page_ad *page_data = scratch; return true; // Otherwise, return pointer directly into host memory - } else { - *page_data = pma.get_memory().get_host_memory() + page_address; - return true; } + *page_data = pma.get_memory().get_host_memory() + page_address; + return true; } pma_entry make_mmapd_memory_pma_entry(const std::string &description, uint64_t start, uint64_t length, diff --git a/src/pma.h b/src/pma.h index 717c4963c..5d3df5f3b 100644 --- a/src/pma.h +++ b/src/pma.h @@ -479,9 +479,8 @@ class pma_entry final { auto map_index = page_number >> 3; assert(map_index < m_dirty_page_map.size()); return (m_dirty_page_map[map_index] & (1 << (page_number & 7))) != 0; - } else { - return true; } + return true; } /// \brief Marks all pages in range as clean diff --git a/src/soft-float.h b/src/soft-float.h index 492fe9df6..0354c36ae 100644 --- a/src/soft-float.h +++ b/src/soft-float.h @@ -197,10 +197,9 @@ struct i_sfloat { if (d != 0) { if (d >= F_SIZE) { return (a != 0); - } else { - const F_UINT mask = (static_cast(1) << d) - 1; - return (a >> d) | ((a & mask) != 0); } + const F_UINT mask = (static_cast(1) << d) - 1; + return (a >> d) | ((a & mask) != 0); } return a; } @@ -346,12 +345,12 @@ struct i_sfloat { *pfflags |= FFLAGS_NV_MASK; } return F_QNAN; - } else if (b_exp == EXP_MASK && a_sign != b_sign) { + } + if (b_exp == EXP_MASK && a_sign != b_sign) { *pfflags |= FFLAGS_NV_MASK; return F_QNAN; - } else { // infinity - return a; - } + } // infinity + return a; } if (a_exp == 0) { a_exp = 1; @@ -394,15 +393,13 @@ struct i_sfloat { *pfflags |= FFLAGS_NV_MASK; } return F_QNAN; - } else { // infinity - if ((a_exp == EXP_MASK && (b_exp == 0 && b_mant == 0)) || - (b_exp == EXP_MASK && (a_exp == 0 && a_mant == 0))) { - *pfflags |= FFLAGS_NV_MASK; - return F_QNAN; - } else { - return pack(r_sign, EXP_MASK, 0); - } + } // infinity + if ((a_exp == EXP_MASK && (b_exp == 0 && b_mant == 0)) || + (b_exp == EXP_MASK && (a_exp == 0 && a_mant == 0))) { + *pfflags |= FFLAGS_NV_MASK; + return F_QNAN; } + return pack(r_sign, EXP_MASK, 0); } if (a_exp == 0) { if (a_mant == 0) { // zero @@ -452,16 +449,15 @@ struct i_sfloat { *pfflags |= FFLAGS_NV_MASK; } return F_QNAN; - } else { // infinities - if ((a_exp == EXP_MASK || b_exp == EXP_MASK) && (c_exp == EXP_MASK && r_sign != c_sign)) { - *pfflags |= FFLAGS_NV_MASK; - return F_QNAN; - } else if (c_exp == EXP_MASK) { - return pack(c_sign, EXP_MASK, 0); - } else { - return pack(r_sign, EXP_MASK, 0); - } + } // infinities + if ((a_exp == EXP_MASK || b_exp == EXP_MASK) && (c_exp == EXP_MASK && r_sign != c_sign)) { + *pfflags |= FFLAGS_NV_MASK; + return F_QNAN; } + if (c_exp == EXP_MASK) { + return pack(c_sign, EXP_MASK, 0); + } + return pack(r_sign, EXP_MASK, 0); } if ((a_exp == 0 && a_mant == 0) || (b_exp == 0 && b_mant == 0)) { if (c_exp == 0 && c_mant == 0) { @@ -469,9 +465,8 @@ struct i_sfloat { r_sign = static_cast(rm == FRM_RDN); } return pack(r_sign, 0, 0); - } else { - return c; } + return c; } if (a_exp == 0) { a_mant = mant_normalize_subnormal(&a_exp, a_mant); @@ -570,31 +565,30 @@ struct i_sfloat { *pfflags |= FFLAGS_NV_MASK; } return F_QNAN; - } else if (b_exp == EXP_MASK) { + } + if (b_exp == EXP_MASK) { *pfflags |= FFLAGS_NV_MASK; return F_QNAN; - } else { - return pack(r_sign, EXP_MASK, 0); } - } else if (unlikely(b_exp == EXP_MASK)) { + return pack(r_sign, EXP_MASK, 0); + } + if (unlikely(b_exp == EXP_MASK)) { if (b_mant != 0) { if (issignan(b)) { *pfflags |= FFLAGS_NV_MASK; } return F_QNAN; - } else { - return pack(r_sign, 0, 0); } + return pack(r_sign, 0, 0); } if (b_exp == 0) { if (unlikely(b_mant == 0)) { // zero if (a_exp == 0 && a_mant == 0) { *pfflags |= FFLAGS_NV_MASK; return F_QNAN; - } else { - *pfflags |= FFLAGS_DZ_MASK; - return pack(r_sign, EXP_MASK, 0); } + *pfflags |= FFLAGS_DZ_MASK; + return pack(r_sign, EXP_MASK, 0); } b_mant = mant_normalize_subnormal(&b_exp, b_mant); } else { @@ -628,12 +622,12 @@ struct i_sfloat { *pfflags |= FFLAGS_NV_MASK; } return F_QNAN; - } else if (a_sign != 0) { + } + if (a_sign != 0) { *pfflags |= FFLAGS_NV_MASK; return F_QNAN; - } else { // infinity - return a; - } + } // infinity + return a; } if (a_sign != 0) { if (likely(a_exp == 0 && a_mant == 0)) { // zero @@ -672,12 +666,10 @@ struct i_sfloat { if (isnan(a)) { if (isnan(b)) { return F_QNAN; - } else { - return b; } - } else { - return a; + return b; } + return a; } /// \brief Min operation. @@ -689,9 +681,8 @@ struct i_sfloat { const uint32_t b_sign = b >> (F_SIZE - 1); if (a_sign != b_sign) { return (a_sign != 0) ? a : b; - } else { - return ((a < b) ^ a_sign) ? a : b; } + return ((a < b) ^ a_sign) ? a : b; } /// \brief Max operation. @@ -703,9 +694,8 @@ struct i_sfloat { const uint32_t b_sign = b >> (F_SIZE - 1); if (a_sign != b_sign) { return (a_sign != 0) ? b : a; - } else { - return ((a < b) ^ a_sign) ? b : a; } + return ((a < b) ^ a_sign) ? b : a; } /// \brief Equal operation. @@ -732,9 +722,8 @@ struct i_sfloat { const uint32_t b_sign = b >> (F_SIZE - 1); if (a_sign != b_sign) { return a_sign || (((a | b) << 1) == 0); - } else { - return (a_sign != 0) ? (a >= b) : (a <= b); } + return (a_sign != 0) ? (a >= b) : (a <= b); } /// \brief Less than operation. @@ -747,9 +736,8 @@ struct i_sfloat { const uint32_t b_sign = b >> (F_SIZE - 1); if (a_sign != b_sign) { return a_sign && (((a | b) << 1) != 0); - } else { - return (a_sign != 0) ? (a > b) : (a < b); } + return (a_sign != 0) ? (a > b) : (a < b); } /// \brief Retrieves float class. @@ -760,18 +748,16 @@ struct i_sfloat { if (unlikely(a_exp == EXP_MASK)) { if (a_mant != 0) { return (a_mant & QNAN_MASK) ? FCLASS_QNAN : FCLASS_SNAN; - } else { - return (a_sign != 0) ? FCLASS_NINF : FCLASS_PINF; } - } else if (a_exp == 0) { + return (a_sign != 0) ? FCLASS_NINF : FCLASS_PINF; + } + if (a_exp == 0) { if (a_mant == 0) { return (a_sign != 0) ? FCLASS_NZERO : FCLASS_PZERO; - } else { - return (a_sign != 0) ? FCLASS_NSUBNORMAL : FCLASS_PSUBNORMAL; } - } else { - return (a_sign != 0) ? FCLASS_NNORMAL : FCLASS_PNORMAL; + return (a_sign != 0) ? FCLASS_NSUBNORMAL : FCLASS_PSUBNORMAL; } + return (a_sign != 0) ? FCLASS_NNORMAL : FCLASS_PNORMAL; } /// \brief Conversion from float to integer. @@ -894,9 +880,8 @@ static uint64_t sfloat_cvt_f32_f64(uint32_t a, uint32_t *pfflags) { *pfflags |= FFLAGS_NV_MASK; } return i_sfloat64::F_QNAN; - } else { // infinity - return i_sfloat64::pack(a_sign, i_sfloat64::EXP_MASK, 0); - } + } // infinity + return i_sfloat64::pack(a_sign, i_sfloat64::EXP_MASK, 0); } if (a_exp == 0) { if (a_mant == 0) { // zero @@ -924,9 +909,8 @@ static uint32_t sfloat_cvt_f64_f32(uint64_t a, FRM_modes rm, uint32_t *pfflags) *pfflags |= FFLAGS_NV_MASK; } return i_sfloat32::F_QNAN; - } else { // infinity - return i_sfloat32::pack(a_sign, 0xff, 0); - } + } // infinity + return i_sfloat32::pack(a_sign, 0xff, 0); } if (a_exp == 0) { if (a_mant == 0) { // zero diff --git a/src/translate-virtual-address.h b/src/translate-virtual-address.h index d7e351d22..5044fdfd4 100644 --- a/src/translate-virtual-address.h +++ b/src/translate-virtual-address.h @@ -242,9 +242,8 @@ static NO_INLINE bool translate_virtual_address(STATE_ACCESS &a, uint64_t *ppadd *ppaddr = (vaddr & vaddr_mask) | (ppn & ~vaddr_mask); return true; // xwr == 0 means we have a pointer to the start of the next page table - } else { - pte_addr = ppn; } + pte_addr = ppn; } return false; } diff --git a/src/uarch-bridge.h b/src/uarch-bridge.h index 40a37068c..5ad34d46f 100644 --- a/src/uarch-bridge.h +++ b/src/uarch-bridge.h @@ -788,9 +788,8 @@ class uarch_bridge { auto word_index = (paddr - PMA_SHADOW_PMAS_START) >> 3; if ((word_index & 1) == 0) { return "pma.istart"; - } else { - return "pma.ilength"; } + return "pma.ilength"; } if (paddr >= PMA_SHADOW_TLB_START && paddr < PMA_SHADOW_TLB_START + PMA_SHADOW_TLB_LENGTH && @@ -798,7 +797,8 @@ class uarch_bridge { const uint64_t tlboff = paddr - PMA_SHADOW_TLB_START; if (tlboff < offsetof(shadow_tlb_state, cold)) { return "cold_tlb_entry_field"; - } else if (tlboff < sizeof(shadow_tlb_state)) { + } + if (tlboff < sizeof(shadow_tlb_state)) { return "hot_tlb_entry_field"; } } @@ -851,7 +851,8 @@ class uarch_bridge { const uint64_t eidx = etypeoff / sizeof(tlb_hot_entry); const uint64_t fieldoff = etypeoff % sizeof(tlb_hot_entry); return read_tlb_entry_field(s, true, etype, eidx, fieldoff, data); - } else if (tlboff < sizeof(shadow_tlb_state)) { // Cold entry + } + if (tlboff < sizeof(shadow_tlb_state)) { // Cold entry const uint64_t coldoff = tlboff - offsetof(shadow_tlb_state, cold); const uint64_t etype = coldoff / sizeof(std::array); const uint64_t etypeoff = coldoff % sizeof(std::array); @@ -882,7 +883,8 @@ class uarch_bridge { const uint64_t eidx = etypeoff / sizeof(tlb_hot_entry); const uint64_t fieldoff = etypeoff % sizeof(tlb_hot_entry); return write_tlb_entry_field(s, true, etype, eidx, fieldoff, data); - } else if (tlboff < sizeof(shadow_tlb_state)) { // Cold entry + } + if (tlboff < sizeof(shadow_tlb_state)) { // Cold entry const uint64_t coldoff = tlboff - offsetof(shadow_tlb_state, cold); const uint64_t etype = coldoff / sizeof(std::array); const uint64_t etypeoff = coldoff % sizeof(std::array); diff --git a/src/uarch-step.cpp b/src/uarch-step.cpp index 5c5e118bf..6963d9425 100644 --- a/src/uarch-step.cpp +++ b/src/uarch-step.cpp @@ -919,7 +919,8 @@ static inline void executeECALL(UarchState &a, uint64 pc) { uint64 fn = readX(a, 17); // a7 contains the function number if (fn == UARCH_ECALL_FN_HALT) { return setHaltFlag(a); - } else if (fn == UARCH_ECALL_FN_PUTCHAR) { + } + if (fn == UARCH_ECALL_FN_PUTCHAR) { uint64 character = readX(a, 16); // a6 contains the character to print putChar(a, uint8(character)); } else { @@ -964,107 +965,158 @@ template static inline void executeInsn(UarchState &a, uint32 insn, uint64 pc) { if (insnMatchOpcodeFunct3(insn, 0x13, 0x0)) { return executeADDI(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x3, 0x3)) { + } + if (insnMatchOpcodeFunct3(insn, 0x3, 0x3)) { return executeLD(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x63, 0x6)) { + } + if (insnMatchOpcodeFunct3(insn, 0x63, 0x6)) { return executeBLTU(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x63, 0x0)) { + } + if (insnMatchOpcodeFunct3(insn, 0x63, 0x0)) { return executeBEQ(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x13, 0x7)) { + } + if (insnMatchOpcodeFunct3(insn, 0x13, 0x7)) { return executeANDI(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x0, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x0, 0x0)) { return executeADD(a, insn, pc); - } else if (insnMatchOpcode(insn, 0x6f)) { + } + if (insnMatchOpcode(insn, 0x6f)) { return executeJAL(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7Sr1(insn, 0x13, 0x1, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7Sr1(insn, 0x13, 0x1, 0x0)) { return executeSLLI(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x7, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x7, 0x0)) { return executeAND(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x23, 0x3)) { + } + if (insnMatchOpcodeFunct3(insn, 0x23, 0x3)) { return executeSD(a, insn, pc); - } else if (insnMatchOpcode(insn, 0x37)) { + } + if (insnMatchOpcode(insn, 0x37)) { return executeLUI(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x67, 0x0)) { + } + if (insnMatchOpcodeFunct3(insn, 0x67, 0x0)) { return executeJALR(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x1b, 0x0)) { + } + if (insnMatchOpcodeFunct3(insn, 0x1b, 0x0)) { return executeADDIW(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7Sr1(insn, 0x13, 0x5, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7Sr1(insn, 0x13, 0x5, 0x0)) { return executeSRLI(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x1b, 0x5, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x1b, 0x5, 0x0)) { return executeSRLIW(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x63, 0x1)) { + } + if (insnMatchOpcodeFunct3(insn, 0x63, 0x1)) { return executeBNE(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x3, 0x2)) { + } + if (insnMatchOpcodeFunct3(insn, 0x3, 0x2)) { return executeLW(a, insn, pc); - } else if (insnMatchOpcode(insn, 0x17)) { + } + if (insnMatchOpcode(insn, 0x17)) { return executeAUIPC(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x63, 0x7)) { + } + if (insnMatchOpcodeFunct3(insn, 0x63, 0x7)) { return executeBGEU(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x0, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x0, 0x0)) { return executeADDW(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7Sr1(insn, 0x13, 0x5, 0x10)) { + } + if (insnMatchOpcodeFunct3Funct7Sr1(insn, 0x13, 0x5, 0x10)) { return executeSRAI(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x6, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x6, 0x0)) { return executeOR(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x1b, 0x5, 0x20)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x1b, 0x5, 0x20)) { return executeSRAIW(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x63, 0x5)) { + } + if (insnMatchOpcodeFunct3(insn, 0x63, 0x5)) { return executeBGE(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x0, 0x20)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x0, 0x20)) { return executeSUB(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x3, 0x4)) { + } + if (insnMatchOpcodeFunct3(insn, 0x3, 0x4)) { return executeLBU(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x1b, 0x1, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x1b, 0x1, 0x0)) { return executeSLLIW(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x5, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x5, 0x0)) { return executeSRL(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x4, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x4, 0x0)) { return executeXOR(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x23, 0x2)) { + } + if (insnMatchOpcodeFunct3(insn, 0x23, 0x2)) { return executeSW(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x1, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x1, 0x0)) { return executeSLL(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x63, 0x4)) { + } + if (insnMatchOpcodeFunct3(insn, 0x63, 0x4)) { return executeBLT(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x23, 0x0)) { + } + if (insnMatchOpcodeFunct3(insn, 0x23, 0x0)) { return executeSB(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x0, 0x20)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x0, 0x20)) { return executeSUBW(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x13, 0x4)) { + } + if (insnMatchOpcodeFunct3(insn, 0x13, 0x4)) { return executeXORI(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x5, 0x20)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x5, 0x20)) { return executeSRA(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x3, 0x5)) { + } + if (insnMatchOpcodeFunct3(insn, 0x3, 0x5)) { return executeLHU(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x23, 0x1)) { + } + if (insnMatchOpcodeFunct3(insn, 0x23, 0x1)) { return executeSH(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x5, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x5, 0x0)) { return executeSRLW(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x3, 0x6)) { + } + if (insnMatchOpcodeFunct3(insn, 0x3, 0x6)) { return executeLWU(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x1, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x1, 0x0)) { return executeSLLW(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x3, 0x0)) { + } + if (insnMatchOpcodeFunct3(insn, 0x3, 0x0)) { return executeLB(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x3, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x3, 0x0)) { return executeSLTU(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x5, 0x20)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x3b, 0x5, 0x20)) { return executeSRAW(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x3, 0x1)) { + } + if (insnMatchOpcodeFunct3(insn, 0x3, 0x1)) { return executeLH(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x13, 0x6)) { + } + if (insnMatchOpcodeFunct3(insn, 0x13, 0x6)) { return executeORI(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x13, 0x3)) { + } + if (insnMatchOpcodeFunct3(insn, 0x13, 0x3)) { return executeSLTIU(a, insn, pc); - } else if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x2, 0x0)) { + } + if (insnMatchOpcodeFunct3Funct7(insn, 0x33, 0x2, 0x0)) { return executeSLT(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0x13, 0x2)) { + } + if (insnMatchOpcodeFunct3(insn, 0x13, 0x2)) { return executeSLTI(a, insn, pc); - } else if (insnMatchOpcodeFunct3(insn, 0xf, 0x0)) { + } + if (insnMatchOpcodeFunct3(insn, 0xf, 0x0)) { return executeFENCE(a, pc); - } else if (insn == uint32(0x73)) { + } + if (insn == uint32(0x73)) { return executeECALL(a, pc); - } else if (insn == uint32(0x100073)) { + } + if (insn == uint32(0x100073)) { return executeEBREAK(a); } throwRuntimeError(a, "illegal instruction"); diff --git a/src/virtio-console.cpp b/src/virtio-console.cpp index b740390c7..74661650f 100644 --- a/src/virtio-console.cpp +++ b/src/virtio-console.cpp @@ -37,14 +37,13 @@ bool virtio_console::on_device_queue_available(i_device_state_access *a, uint32_ if (queue_idx == VIRTIO_CONSOLE_RECEIVEQ) { // Guest has a new slot available in the write queue // Do nothing, host stdin characters will be written to the guest in the next poll return false; - } else if (queue_idx == VIRTIO_CONSOLE_TRANSMITQ) { // Guest sent new characters to the host + } + if (queue_idx == VIRTIO_CONSOLE_TRANSMITQ) { // Guest sent new characters to the host // Write guest characters to host stdout return write_next_chars_to_host(a, queue_idx, desc_idx, read_avail_len); - } else { - // Other queues are unexpected - notify_device_needs_reset(a); - return false; - } + } // Other queues are unexpected + notify_device_needs_reset(a); + return false; } bool virtio_console::write_next_chars_to_host(i_device_state_access *a, uint32_t queue_idx, uint16_t desc_idx, diff --git a/src/virtio-net-carrier-tuntap.cpp b/src/virtio-net-carrier-tuntap.cpp index e7828e471..34c9de2f9 100644 --- a/src/virtio-net-carrier-tuntap.cpp +++ b/src/virtio-net-carrier-tuntap.cpp @@ -188,10 +188,8 @@ bool virtio_net_carrier_tuntap::read_packet_from_host(i_device_state_access *a, // There is no packet available. *pwritten_len = 0; return true; - } else { - // Unexpected error, return false to reset the device. - return false; - } + } // Unexpected error, return false to reset the device. + return false; } const auto packet_len = static_cast(read_len); // Is there enough space in the write buffer to write this packet? diff --git a/src/virtio-net.cpp b/src/virtio-net.cpp index 56ca234da..53c900469 100644 --- a/src/virtio-net.cpp +++ b/src/virtio-net.cpp @@ -39,7 +39,8 @@ bool virtio_net::on_device_queue_available(i_device_state_access *a, uint32_t qu if (queue_idx == VIRTIO_NET_RECEIVEQ) { // Guest has a new slot available in the write queue // Write any pending packets from host to guest return poll_nowait(a); - } else if (queue_idx == VIRTIO_NET_TRANSMITQ) { // Guest sent a new packet to the host + } + if (queue_idx == VIRTIO_NET_TRANSMITQ) { // Guest sent a new packet to the host if (write_next_packet_to_host(a, queue_idx, desc_idx, read_avail_len)) { // When a packet is just sent, poll for a response right-away. // This is necessary to have fast communication between the guest and its host @@ -48,11 +49,9 @@ bool virtio_net::on_device_queue_available(i_device_state_access *a, uint32_t qu return true; } return false; - } else { - // Other queues are unexpected - notify_device_needs_reset(a); - return false; - } + } // Other queues are unexpected + notify_device_needs_reset(a); + return false; } void virtio_net::prepare_select(select_fd_sets *fds, uint64_t *timeout_us) { diff --git a/src/virtio-p9fs.cpp b/src/virtio-p9fs.cpp index 1864dc8cd..22ce41c5c 100644 --- a/src/virtio-p9fs.cpp +++ b/src/virtio-p9fs.cpp @@ -480,13 +480,12 @@ static std::string join_path_name(const std::string &path, const std::string &na } if (path[path.length() - 1] == '/') { return path + name; - } else { - std::string s; - s.append(path); - s.append("/"); - s.append(name); - return s; } + std::string s; + s.append(path); + s.append("/"); + s.append(name); + return s; } static std::string remove_path_name(const std::string &path) { @@ -1668,10 +1667,8 @@ bool virtio_p9fs_device::op_walk(virtq_unserializer &&mmsg, uint16_t tag) { // Return an error only for the first walk if (nwalked == 0) { return send_error(msg, tag, host_errno_to_p9(errno)); - } else { - // Otherwise, stop walk on error - break; - } + } // Otherwise, stop walk on error + break; } path = std::move(next_path); } diff --git a/tests/misc/test-merkle-tree-hash.cpp b/tests/misc/test-merkle-tree-hash.cpp index e12ede900..5b90d6b0c 100644 --- a/tests/misc/test-merkle-tree-hash.cpp +++ b/tests/misc/test-merkle-tree-hash.cpp @@ -143,11 +143,10 @@ hash_type get_leaf_hash(hasher_type &h, int log2_word_size, const unsigned char get_leaf_hash(h, log2_word_size, leaf_data + (1 << (log2_leaf_size - 1)), log2_leaf_size - 1); get_concat_hash(h, left, right, left); return left; - } else { - hash_type leaf; - get_word_hash(h, leaf_data, log2_word_size, leaf); - return leaf; } + hash_type leaf; + get_word_hash(h, leaf_data, log2_word_size, leaf); + return leaf; } /// \brief Computes the Merkle hash of a leaf of data @@ -183,7 +182,8 @@ int main(int argc, char *argv[]) try { if (strcmp(argv[i], "--help") == 0) { help(argv[0]); return 1; - } else if (stringval("--input=", argv[i], &input_name)) { + } + if (stringval("--input=", argv[i], &input_name)) { ; } else if (intval("--log2-word-size=", argv[i], &log2_word_size)) { ; @@ -249,9 +249,8 @@ int main(int argc, char *argv[]) try { if (ferror(input_file.get()) != 0) { error("error reading input\n"); return 1; - } else { - break; } + break; } if (leaf_count >= max_leaves) { error("too many leaves for tree\n");