Commit e8df147f authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] [cleanup] Move LocalDeclEncoder to own compilation unit

wasm-macro-gen.h is mainly used from tests, but LocalDeclEncoder is
also used from various other places.
This CL moves the LocalDeclEncoder to an own compilation unit. We want
to later move wasm-macro-gen.h to the tests folder.
It also refactors the LocalDeclEncoder to reuse the
LEBHelper::write_u32v and LEBHelper::sizeof_u32v methods instead of
reimplementing it.

R=ahaas@chromium.org

Change-Id: Ia4651436f0544578da7c1c43596d343571942e97
Reviewed-on: https://chromium-review.googlesource.com/486724Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44838}
parent 92bf8327
......@@ -1940,6 +1940,8 @@ v8_source_set("v8_base") {
"src/wasm/function-body-decoder.cc",
"src/wasm/function-body-decoder.h",
"src/wasm/leb-helper.h",
"src/wasm/local-decl-encoder.cc",
"src/wasm/local-decl-encoder.h",
"src/wasm/module-decoder.cc",
"src/wasm/module-decoder.h",
"src/wasm/signature-map.cc",
......
......@@ -1352,6 +1352,8 @@
'wasm/function-body-decoder.h',
'wasm/function-body-decoder-impl.h',
'wasm/leb-helper.h',
'wasm/local-decl-encoder.cc',
'wasm/local-decl-encoder.h',
'wasm/module-decoder.cc',
'wasm/module-decoder.h',
'wasm/signature-map.cc',
......
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/wasm/local-decl-encoder.h"
#include "src/wasm/leb-helper.h"
using namespace v8::internal;
using namespace v8::internal::wasm;
void LocalDeclEncoder::Prepend(Zone* zone, const byte** start,
const byte** end) const {
size_t size = (*end - *start);
byte* buffer = reinterpret_cast<byte*>(zone->New(Size() + size));
size_t pos = Emit(buffer);
memcpy(buffer + pos, *start, size);
pos += size;
*start = buffer;
*end = buffer + pos;
}
size_t LocalDeclEncoder::Emit(byte* buffer) const {
byte* pos = buffer;
LEBHelper::write_u32v(&pos, static_cast<uint32_t>(local_decls.size()));
for (auto& local_decl : local_decls) {
LEBHelper::write_u32v(&pos, local_decl.first);
*pos = WasmOpcodes::ValueTypeCodeFor(local_decl.second);
++pos;
}
DCHECK_EQ(Size(), pos - buffer);
return static_cast<size_t>(pos - buffer);
}
uint32_t LocalDeclEncoder::AddLocals(uint32_t count, ValueType type) {
uint32_t result =
static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
total += count;
if (local_decls.size() > 0 && local_decls.back().second == type) {
count += local_decls.back().first;
local_decls.pop_back();
}
local_decls.push_back(std::pair<uint32_t, ValueType>(count, type));
return result;
}
size_t LocalDeclEncoder::Size() const {
size_t size = LEBHelper::sizeof_u32v(local_decls.size());
for (auto p : local_decls) size += 1 + LEBHelper::sizeof_u32v(p.first);
return size;
}
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_WASM_LOCAL_DECL_ENCODER_H_
#define V8_WASM_LOCAL_DECL_ENCODER_H_
#include "src/wasm/wasm-opcodes.h"
#include "src/zone/zone-containers.h"
#include "src/zone/zone.h"
namespace v8 {
namespace internal {
namespace wasm {
// A helper for encoding local declarations prepended to the body of a
// function.
class LocalDeclEncoder {
public:
explicit LocalDeclEncoder(Zone* zone, FunctionSig* s = nullptr)
: sig(s), local_decls(zone), total(0) {}
// Prepend local declarations by creating a new buffer and copying data
// over. The new buffer must be delete[]'d by the caller.
void Prepend(Zone* zone, const byte** start, const byte** end) const;
size_t Emit(byte* buffer) const;
// Add locals declarations to this helper. Return the index of the newly added
// local(s), with an optional adjustment for the parameters.
uint32_t AddLocals(uint32_t count, ValueType type);
size_t Size() const;
bool has_sig() const { return sig != nullptr; }
FunctionSig* get_sig() const { return sig; }
void set_sig(FunctionSig* s) { sig = s; }
private:
FunctionSig* sig;
ZoneVector<std::pair<uint32_t, ValueType>> local_decls;
size_t total;
};
} // namespace wasm
} // namespace internal
} // namespace v8
#endif // V8_WASM_LOCAL_DECL_ENCODER_H_
......@@ -170,90 +170,6 @@ inline void CheckI64v(int64_t value, int length) {
DCHECK(length == 10 || I64V_IN_RANGE(value, length));
}
// A helper for encoding local declarations prepended to the body of a
// function.
// TODO(titzer): move this to an appropriate header.
class LocalDeclEncoder {
public:
explicit LocalDeclEncoder(Zone* zone, FunctionSig* s = nullptr)
: sig(s), local_decls(zone), total(0) {}
// Prepend local declarations by creating a new buffer and copying data
// over. The new buffer must be delete[]'d by the caller.
void Prepend(Zone* zone, const byte** start, const byte** end) const {
size_t size = (*end - *start);
byte* buffer = reinterpret_cast<byte*>(zone->New(Size() + size));
size_t pos = Emit(buffer);
memcpy(buffer + pos, *start, size);
pos += size;
*start = buffer;
*end = buffer + pos;
}
size_t Emit(byte* buffer) const {
size_t pos = 0;
pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size()));
for (size_t i = 0; i < local_decls.size(); ++i) {
pos = WriteUint32v(buffer, pos, local_decls[i].first);
buffer[pos++] = WasmOpcodes::ValueTypeCodeFor(local_decls[i].second);
}
DCHECK_EQ(Size(), pos);
return pos;
}
// Add locals declarations to this helper. Return the index of the newly added
// local(s), with an optional adjustment for the parameters.
uint32_t AddLocals(uint32_t count, ValueType type) {
uint32_t result =
static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0));
total += count;
if (local_decls.size() > 0 && local_decls.back().second == type) {
count += local_decls.back().first;
local_decls.pop_back();
}
local_decls.push_back(std::pair<uint32_t, ValueType>(count, type));
return result;
}
size_t Size() const {
size_t size = SizeofUint32v(static_cast<uint32_t>(local_decls.size()));
for (auto p : local_decls) size += 1 + SizeofUint32v(p.first);
return size;
}
bool has_sig() const { return sig != nullptr; }
FunctionSig* get_sig() const { return sig; }
void set_sig(FunctionSig* s) { sig = s; }
private:
FunctionSig* sig;
ZoneVector<std::pair<uint32_t, ValueType>> local_decls;
size_t total;
size_t SizeofUint32v(uint32_t val) const {
size_t size = 1;
while (true) {
byte b = val & MASK_7;
if (b == val) return size;
size++;
val = val >> 7;
}
}
// TODO(titzer): lift encoding of u32v to a common place.
size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const {
while (true) {
byte b = val & MASK_7;
if (b == val) {
buffer[pos++] = b;
break;
}
buffer[pos++] = 0x80 | b;
val = val >> 7;
}
return pos;
}
};
} // namespace wasm
} // namespace internal
} // namespace v8
......
......@@ -9,6 +9,7 @@
#include "src/zone/zone-containers.h"
#include "src/wasm/leb-helper.h"
#include "src/wasm/local-decl-encoder.h"
#include "src/wasm/wasm-macro-gen.h"
#include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes.h"
......
......@@ -25,6 +25,7 @@
#include "src/compiler/zone-stats.h"
#include "src/trap-handler/trap-handler.h"
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/local-decl-encoder.h"
#include "src/wasm/wasm-external-refs.h"
#include "src/wasm/wasm-interpreter.h"
#include "src/wasm/wasm-js.h"
......
......@@ -13,6 +13,7 @@
#include "src/wasm/function-body-decoder-impl.h"
#include "src/wasm/function-body-decoder.h"
#include "src/wasm/local-decl-encoder.h"
#include "src/wasm/signature-map.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-macro-gen.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment