From 72ccdca7959d326197e9cd4638d41f6c9f9c59e4 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 8 Jun 2019 22:50:59 +0800 Subject: [PATCH] Use ArrayList instead of std::vector --- src/bytecode.cpp | 22 +++++++++++++--------- src/bytecode.hpp | 12 +++++++----- src/compiler.cpp | 9 ++++----- src/util/array.hpp | 27 +++++++++++++++++++++++---- test/test_bytecode.cpp | 2 +- test/test_util.cpp | 18 ++++++++++++++++++ 6 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/bytecode.cpp b/src/bytecode.cpp index 4727aee7..66f77932 100644 --- a/src/bytecode.cpp +++ b/src/bytecode.cpp @@ -24,6 +24,9 @@ #include #include +template class ArrayList; +template class ArrayList; + namespace { class Dumper { public: @@ -622,7 +625,8 @@ void Bytecode::Assembler::mul(Register dst, int64_t value) void Bytecode::Assembler::data(const uint8_t *bytes, size_t len) { - data_.insert(data_.end(), bytes, bytes + len); + for (size_t i = 0; i < len; i++) + data_.add(bytes[i]); } void Bytecode::Assembler::enter(uint16_t frame_size) @@ -660,21 +664,21 @@ void Bytecode::Assembler::emit_reg(Register reg) void Bytecode::Assembler::emit_u8(uint8_t byte) { - code_.push_back(byte); + code_.add(byte); } void Bytecode::Assembler::emit_i32(int32_t value) { - code_.push_back(value & 0xff); - code_.push_back((value >> 8) & 0xff); - code_.push_back((value >> 16) & 0xff); - code_.push_back((value >> 24) & 0xff); + code_.add(value & 0xff); + code_.add((value >> 8) & 0xff); + code_.add((value >> 16) & 0xff); + code_.add((value >> 24) & 0xff); } void Bytecode::Assembler::emit_i16(int16_t value) { - code_.push_back(value & 0xff); - code_.push_back((value >> 8) & 0xff); + code_.add(value & 0xff); + code_.add((value >> 8) & 0xff); } void Bytecode::Assembler::bind(Label &label) @@ -724,7 +728,7 @@ Bytecode::Label::~Label() void Bytecode::Label::add_patch(unsigned offset) { - patch_list_.push_back(offset); + patch_list_.add(offset); } unsigned Bytecode::Label::target() const diff --git a/src/bytecode.hpp b/src/bytecode.hpp index ba651d91..e52b12d5 100644 --- a/src/bytecode.hpp +++ b/src/bytecode.hpp @@ -1,4 +1,3 @@ - // // Copyright (C) 2019 Nick Gasson // @@ -21,8 +20,8 @@ #include "util.h" #include "vcode.h" #include "util/printer.hpp" +#include "util/array.hpp" -#include #include class Machine { @@ -148,7 +147,7 @@ public: private: int bound_ = -1; - std::vector patch_list_; + ArrayList patch_list_; }; class Assembler { @@ -200,8 +199,8 @@ public: Assembler(const Assembler &) = delete; Assembler(Assembler &&) = default; - std::vector code_; - std::vector data_; + ArrayList code_; + ArrayList data_; const Machine machine_; #if DEBUG @@ -244,3 +243,6 @@ private: std::map comments_; #endif }; + +extern template class ArrayList; +extern template class ArrayList; diff --git a/src/compiler.cpp b/src/compiler.cpp index 4f4a7832..a4fab7cf 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -21,7 +21,6 @@ #include "phase.h" #include "common.h" -#include #include #include #include @@ -158,8 +157,8 @@ namespace { const Machine machine_; Bytecode::Assembler asm_; std::map var_map_; - std::vector reg_map_; - std::vector block_map_; + ArrayList reg_map_; + ArrayList block_map_; std::set live_; Bitmask allocated_; int op_ = -1; @@ -449,7 +448,7 @@ Bytecode *Compiler::compile(vcode_unit_t unit) const int nregs = vcode_count_regs(); for (int i = 0; i < nregs; i++) - reg_map_.emplace_back(Mapping(Mapping::TEMP, size_of(vcode_reg_type(i)))); + reg_map_.add(Mapping(Mapping::TEMP, size_of(vcode_reg_type(i)))); int param_offset = machine_.frame_reserved(); const int nparams = vcode_count_params(); @@ -522,7 +521,7 @@ Bytecode *Compiler::compile(vcode_unit_t unit) __ enter(-local_offset - machine_.word_size()); for (int i = 0; i < nblocks; i++) - block_map_.push_back(Bytecode::Label()); + block_map_.add(Bytecode::Label()); for (int i = 0; i < nblocks; i++) { vcode_select_block(i); diff --git a/src/util/array.hpp b/src/util/array.hpp index dbc332ef..3cb1dfbb 100644 --- a/src/util/array.hpp +++ b/src/util/array.hpp @@ -27,13 +27,17 @@ template class ArrayList { public: ArrayList(); - ArrayList(const ArrayList&) = delete; - ArrayList(ArrayList&&); + ArrayList(const ArrayList&) = delete; + ArrayList(ArrayList&&); ~ArrayList(); ArrayList& add(const T& item); ArrayList& add(T&& item); unsigned size() const { return count_; } + void clear(); + + T *data() { return items_; } + const T *data() const { return items_; } T& get(unsigned n); const T& get(unsigned n) const; @@ -112,9 +116,14 @@ ArrayList::ArrayList() } template -ArrayList::ArrayList(ArrayList&& other) +ArrayList::ArrayList(ArrayList&& other) + : items_(other.items_), + max_(other.max_), + count_(other.count_) { - assert(false); + other.items_ = nullptr; + other.max_ = 0; + other.count_ = 0; } template @@ -162,6 +171,16 @@ ArrayList& ArrayList::add(T&& item) return *this; } +template +void ArrayList::clear() +{ + for (unsigned i = 0; i < count_; i++) { + items_[i].~T(); + } + + count_ = 0; +} + template T& ArrayList::get(unsigned n) { diff --git a/test/test_bytecode.cpp b/test/test_bytecode.cpp index 1675cf80..b2901461 100644 --- a/test/test_bytecode.cpp +++ b/test/test_bytecode.cpp @@ -41,7 +41,7 @@ static void teardown() } static void check_bytecodes(const Bytecode *b, - const std::vector&& expect) + const std::initializer_list&& expect) { const uint8_t *p = b->code(); std::map match; diff --git a/test/test_util.cpp b/test/test_util.cpp index 00b918f9..b6c731a7 100644 --- a/test/test_util.cpp +++ b/test/test_util.cpp @@ -92,6 +92,23 @@ START_TEST(test_array2) } END_TEST +START_TEST(test_array_move) +{ + ArrayList a1; + a1.add(1).add(2).add(3); + + ck_assert_int_eq(3, a1.size()); + + ArrayList a2(std::move(a1)); + + ck_assert_int_eq(3, a2.size()); + ck_assert_int_eq(0, a1.size()); + ck_assert_int_eq(1, a2[0]); + ck_assert_int_eq(2, a2[1]); + ck_assert_int_eq(3, a2[2]); +} +END_TEST + static int test_two_line, test_one_line; __attribute__((noinline)) @@ -166,6 +183,7 @@ extern "C" Suite *get_util_tests(void) TCase *tc_array = nvc_unit_test("array"); tcase_add_test(tc_array, test_array1); tcase_add_test(tc_array, test_array2); + tcase_add_test(tc_array, test_array_move); suite_add_tcase(s, tc_array); TCase *tc_trace = nvc_unit_test("stacktrace"); -- 2.39.2