diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-03 10:14:03 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-03 10:14:03 -0800 |
commit | 3b0fc2374d042ba81e16ee09f57cdef9891be065 (patch) | |
tree | 0e2397e4c00f88b13614ae66c92c81a9c4778859 | |
parent | a18f879612876860429aa153336be293a9368310 (diff) | |
download | binaryen-3b0fc2374d042ba81e16ee09f57cdef9891be065.tar.gz binaryen-3b0fc2374d042ba81e16ee09f57cdef9891be065.tar.bz2 binaryen-3b0fc2374d042ba81e16ee09f57cdef9891be065.zip |
refactor arena code
-rw-r--r-- | src/asm2wasm.h | 11 | ||||
-rw-r--r-- | src/mixed_arena.h | 35 | ||||
-rw-r--r-- | src/wasm.h | 36 |
3 files changed, 38 insertions, 44 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 65d837496..2d045401a 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -1,6 +1,7 @@ #include "wasm.h" #include "optimizer.h" +#include "mixed_arena.h" namespace wasm { @@ -69,7 +70,7 @@ std::vector<Ref> AstStackHelper::astStack; class Asm2WasmBuilder { Module& wasm; - wasm::Arena allocator; + MixedArena allocator; // globals @@ -307,8 +308,6 @@ private: return -1; // avoid warning } - wasm::Arena tempAllocator; - std::map<unsigned, Ref> tempNums; Literal getLiteral(Ref ast) { @@ -503,10 +502,6 @@ void Asm2WasmBuilder::processAsm(Ref ast) { for (auto curr : toErase) { wasm.imports.erase(curr); } - - // cleanups - - tempAllocator.clear(); } Function* Asm2WasmBuilder::processFunction(Ref ast) { @@ -1063,8 +1058,6 @@ void Asm2WasmBuilder::optimize() { // Optimization passes. Note: no effort is made to free nodes that are no longer held on to. struct BlockBreakOptimizer : public WasmWalker { - BlockBreakOptimizer() : WasmWalker(nullptr) {} - void visitBlock(Block *curr) override { // if the block ends in a break on this very block, then just put the value there Break *last = curr->list[curr->list.size()-1]->dyn_cast<Break>(); diff --git a/src/mixed_arena.h b/src/mixed_arena.h new file mode 100644 index 000000000..a92026580 --- /dev/null +++ b/src/mixed_arena.h @@ -0,0 +1,35 @@ + +#include <vector> + +// Arena allocation for mixed-type data. +struct MixedArena { + std::vector<char*> chunks; + int index; // in last chunk + + template<class T> + T* alloc() { + const size_t CHUNK = 10000; + size_t currSize = (sizeof(T) + 7) & (-8); // same alignment as malloc TODO optimize? + assert(currSize < CHUNK); + if (chunks.size() == 0 || index + currSize >= CHUNK) { + chunks.push_back(new char[CHUNK]); + index = 0; + } + T* ret = (T*)(chunks.back() + index); + index += currSize; + new (ret) T(); + return ret; + } + + void clear() { + for (char* chunk : chunks) { + delete[] chunk; + } + chunks.clear(); + } + + ~MixedArena() { + clear(); + } +}; + diff --git a/src/wasm.h b/src/wasm.h index ecf44e72e..1fce6692b 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -18,38 +18,6 @@ namespace wasm { // Utilities -// Arena allocation for mixed-type data. -struct Arena { - std::vector<char*> chunks; - int index; // in last chunk - - template<class T> - T* alloc() { - const size_t CHUNK = 10000; - size_t currSize = (sizeof(T) + 7) & (-8); // same alignment as malloc TODO optimize? - assert(currSize < CHUNK); - if (chunks.size() == 0 || index + currSize >= CHUNK) { - chunks.push_back(new char[CHUNK]); - index = 0; - } - T* ret = (T*)(chunks.back() + index); - index += currSize; - new (ret) T(); - return ret; - } - - void clear() { - for (char* chunk : chunks) { - delete[] chunk; - } - chunks.clear(); - } - - ~Arena() { - clear(); - } -}; - std::ostream &doIndent(std::ostream &o, unsigned indent) { for (unsigned i = 0; i < indent; i++) { o << " "; @@ -1004,11 +972,9 @@ std::ostream& Expression::print(std::ostream &o, unsigned indent) { // struct WasmWalker : public WasmVisitor<void> { - wasm::Arena* allocator; // use an existing allocator, or null if no allocations Expression* replace; - WasmWalker() : allocator(nullptr), replace(nullptr) {} - WasmWalker(wasm::Arena* allocator) : allocator(allocator), replace(nullptr) {} + WasmWalker() : replace(nullptr) {} // the visit* methods can call this to replace the current node void replaceCurrent(Expression *expression) { |