blob: 0f2118dd013cad493078f70eb74ff2e075fe26a8 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_WASM_ENCODER_H_
6#define V8_WASM_ENCODER_H_
7
8#include "src/signature.h"
9#include "src/zone-containers.h"
10
11#include "src/base/smart-pointers.h"
12
Ben Murdochc5610432016-08-08 18:44:38 +010013#include "src/wasm/wasm-macro-gen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/wasm/wasm-module.h"
15#include "src/wasm/wasm-opcodes.h"
16#include "src/wasm/wasm-result.h"
17
18namespace v8 {
19namespace internal {
20namespace wasm {
21
22class WasmModuleBuilder;
23
24class WasmFunctionEncoder : public ZoneObject {
25 public:
26 uint32_t HeaderSize() const;
27 uint32_t BodySize() const;
28 uint32_t NameSize() const;
29 void Serialize(byte* buffer, byte** header, byte** body) const;
30
31 private:
Ben Murdochc5610432016-08-08 18:44:38 +010032 WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals, bool exported);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033 friend class WasmFunctionBuilder;
Ben Murdochc5610432016-08-08 18:44:38 +010034 uint32_t signature_index_;
35 LocalDeclEncoder locals_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 bool exported_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 ZoneVector<uint8_t> body_;
38 ZoneVector<char> name_;
39
Ben Murdochc5610432016-08-08 18:44:38 +010040 bool HasName() const { return exported_ && name_.size() > 0; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041};
42
43class WasmFunctionBuilder : public ZoneObject {
44 public:
Ben Murdochc5610432016-08-08 18:44:38 +010045 void SetSignature(FunctionSig* sig);
46 uint32_t AddLocal(LocalType type);
47 void EmitVarInt(uint32_t val);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048 void EmitCode(const byte* code, uint32_t code_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049 void Emit(WasmOpcode opcode);
Ben Murdochc5610432016-08-08 18:44:38 +010050 void EmitGetLocal(uint32_t index);
51 void EmitSetLocal(uint32_t index);
52 void EmitI32Const(int32_t val);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 void EmitWithU8(WasmOpcode opcode, const byte immediate);
Ben Murdochda12d292016-06-02 14:46:10 +010054 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
55 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 void Exported(uint8_t flag);
Ben Murdochc5610432016-08-08 18:44:38 +010057 void SetName(const char* name, int name_length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const;
59
60 private:
61 explicit WasmFunctionBuilder(Zone* zone);
62 friend class WasmModuleBuilder;
Ben Murdochc5610432016-08-08 18:44:38 +010063 LocalDeclEncoder locals_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 uint8_t exported_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 ZoneVector<uint8_t> body_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 ZoneVector<char> name_;
Ben Murdochc5610432016-08-08 18:44:38 +010067 void IndexVars(WasmFunctionEncoder* e, uint32_t* var_index) const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068};
69
70class WasmDataSegmentEncoder : public ZoneObject {
71 public:
72 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
73 uint32_t dest);
74 uint32_t HeaderSize() const;
75 uint32_t BodySize() const;
76 void Serialize(byte* buffer, byte** header, byte** body) const;
77
78 private:
79 ZoneVector<byte> data_;
80 uint32_t dest_;
81};
82
83class WasmModuleIndex : public ZoneObject {
84 public:
85 const byte* Begin() const { return begin_; }
86 const byte* End() const { return end_; }
87
88 private:
89 friend class WasmModuleWriter;
90 WasmModuleIndex(const byte* begin, const byte* end)
91 : begin_(begin), end_(end) {}
92 const byte* begin_;
93 const byte* end_;
94};
95
Ben Murdochc5610432016-08-08 18:44:38 +010096struct WasmFunctionImport {
97 uint32_t sig_index;
98 const char* name;
99 int name_length;
100};
101
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102class WasmModuleWriter : public ZoneObject {
103 public:
104 WasmModuleIndex* WriteTo(Zone* zone) const;
105
106 private:
107 friend class WasmModuleBuilder;
108 explicit WasmModuleWriter(Zone* zone);
Ben Murdochc5610432016-08-08 18:44:38 +0100109 ZoneVector<WasmFunctionImport> imports_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000110 ZoneVector<WasmFunctionEncoder*> functions_;
111 ZoneVector<WasmDataSegmentEncoder*> data_segments_;
112 ZoneVector<FunctionSig*> signatures_;
Ben Murdochc5610432016-08-08 18:44:38 +0100113 ZoneVector<uint32_t> indirect_functions_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 ZoneVector<std::pair<MachineType, bool>> globals_;
Ben Murdochda12d292016-06-02 14:46:10 +0100115 int start_function_index_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116};
117
118class WasmModuleBuilder : public ZoneObject {
119 public:
120 explicit WasmModuleBuilder(Zone* zone);
Ben Murdochc5610432016-08-08 18:44:38 +0100121 uint32_t AddFunction();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 uint32_t AddGlobal(MachineType type, bool exported);
123 WasmFunctionBuilder* FunctionAt(size_t index);
124 void AddDataSegment(WasmDataSegmentEncoder* data);
Ben Murdochc5610432016-08-08 18:44:38 +0100125 uint32_t AddSignature(FunctionSig* sig);
126 void AddIndirectFunction(uint32_t index);
127 void MarkStartFunction(uint32_t index);
128 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 WasmModuleWriter* Build(Zone* zone);
130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131 struct CompareFunctionSigs {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100132 bool operator()(FunctionSig* a, FunctionSig* b) const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133 };
Ben Murdochc5610432016-08-08 18:44:38 +0100134 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135
Ben Murdoch097c5b22016-05-18 11:27:45 +0100136 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 Zone* zone_;
138 ZoneVector<FunctionSig*> signatures_;
Ben Murdochc5610432016-08-08 18:44:38 +0100139 ZoneVector<WasmFunctionImport> imports_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140 ZoneVector<WasmFunctionBuilder*> functions_;
141 ZoneVector<WasmDataSegmentEncoder*> data_segments_;
Ben Murdochc5610432016-08-08 18:44:38 +0100142 ZoneVector<uint32_t> indirect_functions_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143 ZoneVector<std::pair<MachineType, bool>> globals_;
144 SignatureMap signature_map_;
Ben Murdochda12d292016-06-02 14:46:10 +0100145 int start_function_index_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146};
147
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148} // namespace wasm
149} // namespace internal
150} // namespace v8
151
152#endif // V8_WASM_ENCODER_H_