2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 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_ENCODER_H_
|
|
|
|
#define V8_WASM_ENCODER_H_
|
|
|
|
|
|
|
|
#include "src/signature.h"
|
|
|
|
#include "src/zone-containers.h"
|
|
|
|
|
|
|
|
#include "src/base/smart-pointers.h"
|
|
|
|
|
2016-05-17 17:53:46 +00:00
|
|
|
#include "src/wasm/wasm-macro-gen.h"
|
2015-12-11 12:26:16 +00:00
|
|
|
#include "src/wasm/wasm-module.h"
|
|
|
|
#include "src/wasm/wasm-opcodes.h"
|
|
|
|
#include "src/wasm/wasm-result.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class WasmModuleBuilder;
|
|
|
|
|
|
|
|
class WasmFunctionEncoder : public ZoneObject {
|
|
|
|
public:
|
|
|
|
uint32_t HeaderSize() const;
|
|
|
|
uint32_t BodySize() const;
|
|
|
|
uint32_t NameSize() const;
|
|
|
|
void Serialize(byte* buffer, byte** header, byte** body) const;
|
|
|
|
|
|
|
|
private:
|
2016-05-17 17:53:46 +00:00
|
|
|
WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals, bool exported);
|
2015-12-11 12:26:16 +00:00
|
|
|
friend class WasmFunctionBuilder;
|
2016-05-17 17:53:46 +00:00
|
|
|
uint32_t signature_index_;
|
|
|
|
LocalDeclEncoder locals_;
|
2015-12-11 12:26:16 +00:00
|
|
|
bool exported_;
|
|
|
|
ZoneVector<uint8_t> body_;
|
|
|
|
ZoneVector<char> name_;
|
|
|
|
|
2016-05-13 08:44:28 +00:00
|
|
|
bool HasName() const { return exported_ && name_.size() > 0; }
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WasmFunctionBuilder : public ZoneObject {
|
|
|
|
public:
|
2016-05-17 17:53:46 +00:00
|
|
|
void SetSignature(FunctionSig* sig);
|
|
|
|
uint32_t AddLocal(LocalType type);
|
2016-04-21 10:14:34 +00:00
|
|
|
void EmitVarInt(uint32_t val);
|
2015-12-11 12:26:16 +00:00
|
|
|
void EmitCode(const byte* code, uint32_t code_size);
|
|
|
|
void Emit(WasmOpcode opcode);
|
2016-04-18 20:57:05 +00:00
|
|
|
void EmitGetLocal(uint32_t index);
|
|
|
|
void EmitSetLocal(uint32_t index);
|
2016-04-29 09:15:26 +00:00
|
|
|
void EmitI32Const(int32_t val);
|
2015-12-11 12:26:16 +00:00
|
|
|
void EmitWithU8(WasmOpcode opcode, const byte immediate);
|
2016-03-09 18:51:28 +00:00
|
|
|
void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
|
2016-03-08 22:54:43 +00:00
|
|
|
void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate);
|
2015-12-11 12:26:16 +00:00
|
|
|
void Exported(uint8_t flag);
|
2016-05-13 08:44:28 +00:00
|
|
|
void SetName(const char* name, int name_length);
|
2015-12-11 12:26:16 +00:00
|
|
|
WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const;
|
|
|
|
|
|
|
|
private:
|
2016-01-13 01:23:43 +00:00
|
|
|
explicit WasmFunctionBuilder(Zone* zone);
|
2015-12-11 12:26:16 +00:00
|
|
|
friend class WasmModuleBuilder;
|
2016-05-17 17:53:46 +00:00
|
|
|
LocalDeclEncoder locals_;
|
2015-12-11 12:26:16 +00:00
|
|
|
uint8_t exported_;
|
|
|
|
ZoneVector<uint8_t> body_;
|
|
|
|
ZoneVector<char> name_;
|
2016-05-17 17:53:46 +00:00
|
|
|
void IndexVars(WasmFunctionEncoder* e, uint32_t* var_index) const;
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WasmDataSegmentEncoder : public ZoneObject {
|
|
|
|
public:
|
|
|
|
WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
|
|
|
|
uint32_t dest);
|
|
|
|
uint32_t HeaderSize() const;
|
|
|
|
uint32_t BodySize() const;
|
|
|
|
void Serialize(byte* buffer, byte** header, byte** body) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ZoneVector<byte> data_;
|
|
|
|
uint32_t dest_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WasmModuleIndex : public ZoneObject {
|
|
|
|
public:
|
|
|
|
const byte* Begin() const { return begin_; }
|
|
|
|
const byte* End() const { return end_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class WasmModuleWriter;
|
|
|
|
WasmModuleIndex(const byte* begin, const byte* end)
|
|
|
|
: begin_(begin), end_(end) {}
|
|
|
|
const byte* begin_;
|
|
|
|
const byte* end_;
|
|
|
|
};
|
|
|
|
|
2016-05-13 08:44:28 +00:00
|
|
|
struct WasmFunctionImport {
|
|
|
|
uint32_t sig_index;
|
|
|
|
const char* name;
|
|
|
|
int name_length;
|
|
|
|
};
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
class WasmModuleWriter : public ZoneObject {
|
|
|
|
public:
|
|
|
|
WasmModuleIndex* WriteTo(Zone* zone) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class WasmModuleBuilder;
|
|
|
|
explicit WasmModuleWriter(Zone* zone);
|
2016-05-13 08:44:28 +00:00
|
|
|
ZoneVector<WasmFunctionImport> imports_;
|
2015-12-11 12:26:16 +00:00
|
|
|
ZoneVector<WasmFunctionEncoder*> functions_;
|
|
|
|
ZoneVector<WasmDataSegmentEncoder*> data_segments_;
|
|
|
|
ZoneVector<FunctionSig*> signatures_;
|
2016-05-17 17:53:46 +00:00
|
|
|
ZoneVector<uint32_t> indirect_functions_;
|
2015-12-11 12:26:16 +00:00
|
|
|
ZoneVector<std::pair<MachineType, bool>> globals_;
|
2016-03-01 05:49:51 +00:00
|
|
|
int start_function_index_;
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WasmModuleBuilder : public ZoneObject {
|
|
|
|
public:
|
|
|
|
explicit WasmModuleBuilder(Zone* zone);
|
2016-05-13 08:44:28 +00:00
|
|
|
uint32_t AddFunction();
|
2015-12-11 12:26:16 +00:00
|
|
|
uint32_t AddGlobal(MachineType type, bool exported);
|
|
|
|
WasmFunctionBuilder* FunctionAt(size_t index);
|
|
|
|
void AddDataSegment(WasmDataSegmentEncoder* data);
|
2016-05-13 08:44:28 +00:00
|
|
|
uint32_t AddSignature(FunctionSig* sig);
|
2016-05-17 17:53:46 +00:00
|
|
|
void AddIndirectFunction(uint32_t index);
|
|
|
|
void MarkStartFunction(uint32_t index);
|
2016-05-13 08:44:28 +00:00
|
|
|
uint32_t AddImport(const char* name, int name_length, FunctionSig* sig);
|
2015-12-11 12:26:16 +00:00
|
|
|
WasmModuleWriter* Build(Zone* zone);
|
|
|
|
|
|
|
|
struct CompareFunctionSigs {
|
2016-01-20 23:36:42 +00:00
|
|
|
bool operator()(FunctionSig* a, FunctionSig* b) const;
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
2016-05-17 17:53:46 +00:00
|
|
|
typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-02-05 02:07:23 +00:00
|
|
|
private:
|
2015-12-11 12:26:16 +00:00
|
|
|
Zone* zone_;
|
|
|
|
ZoneVector<FunctionSig*> signatures_;
|
2016-05-13 08:44:28 +00:00
|
|
|
ZoneVector<WasmFunctionImport> imports_;
|
2015-12-11 12:26:16 +00:00
|
|
|
ZoneVector<WasmFunctionBuilder*> functions_;
|
|
|
|
ZoneVector<WasmDataSegmentEncoder*> data_segments_;
|
2016-05-17 17:53:46 +00:00
|
|
|
ZoneVector<uint32_t> indirect_functions_;
|
2015-12-11 12:26:16 +00:00
|
|
|
ZoneVector<std::pair<MachineType, bool>> globals_;
|
|
|
|
SignatureMap signature_map_;
|
2016-03-01 05:49:51 +00:00
|
|
|
int start_function_index_;
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_WASM_ENCODER_H_
|