blob: 49a7bf7d05dccddd2de13d05e96fd9255bfc3a9d [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
13#include "src/wasm/wasm-module.h"
14#include "src/wasm/wasm-opcodes.h"
15#include "src/wasm/wasm-result.h"
16
17namespace v8 {
18namespace internal {
19namespace wasm {
20
21class WasmModuleBuilder;
22
23class WasmFunctionEncoder : public ZoneObject {
24 public:
25 uint32_t HeaderSize() const;
26 uint32_t BodySize() const;
27 uint32_t NameSize() const;
28 void Serialize(byte* buffer, byte** header, byte** body) const;
29
30 private:
31 WasmFunctionEncoder(Zone* zone, LocalType return_type, bool exported,
32 bool external);
33 friend class WasmFunctionBuilder;
34 uint16_t signature_index_;
35 ZoneVector<LocalType> params_;
Ben Murdoch097c5b22016-05-18 11:27:45 +010036 uint16_t local_i32_count_;
37 uint16_t local_i64_count_;
38 uint16_t local_f32_count_;
39 uint16_t local_f64_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040 bool exported_;
41 bool external_;
42 ZoneVector<uint8_t> body_;
43 ZoneVector<char> name_;
44
Ben Murdoch097c5b22016-05-18 11:27:45 +010045 bool HasName() const { return (exported_ || external_) && name_.size() > 0; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046};
47
48class WasmFunctionBuilder : public ZoneObject {
49 public:
50 uint16_t AddParam(LocalType type);
51 uint16_t AddLocal(LocalType type);
52 void ReturnType(LocalType type);
53 void EmitCode(const byte* code, uint32_t code_size);
54 void EmitCode(const byte* code, uint32_t code_size,
55 const uint32_t* local_indices, uint32_t indices_size);
56 void Emit(WasmOpcode opcode);
57 void EmitWithU8(WasmOpcode opcode, const byte immediate);
Ben Murdochda12d292016-06-02 14:46:10 +010058 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
59 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate);
60 uint32_t EmitEditableVarIntImmediate();
61 void EditVarIntImmediate(uint32_t offset, const uint32_t immediate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062 void Exported(uint8_t flag);
63 void External(uint8_t flag);
64 void SetName(const unsigned char* name, int name_length);
65 WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const;
66
67 private:
68 explicit WasmFunctionBuilder(Zone* zone);
69 friend class WasmModuleBuilder;
70 LocalType return_type_;
71 struct Type;
72 ZoneVector<Type> locals_;
73 uint8_t exported_;
74 uint8_t external_;
75 ZoneVector<uint8_t> body_;
76 ZoneVector<uint32_t> local_indices_;
77 ZoneVector<char> name_;
78 uint16_t AddVar(LocalType type, bool param);
79 void IndexVars(WasmFunctionEncoder* e, uint16_t* var_index) const;
80};
81
82class WasmDataSegmentEncoder : public ZoneObject {
83 public:
84 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
85 uint32_t dest);
86 uint32_t HeaderSize() const;
87 uint32_t BodySize() const;
88 void Serialize(byte* buffer, byte** header, byte** body) const;
89
90 private:
91 ZoneVector<byte> data_;
92 uint32_t dest_;
93};
94
95class WasmModuleIndex : public ZoneObject {
96 public:
97 const byte* Begin() const { return begin_; }
98 const byte* End() const { return end_; }
99
100 private:
101 friend class WasmModuleWriter;
102 WasmModuleIndex(const byte* begin, const byte* end)
103 : begin_(begin), end_(end) {}
104 const byte* begin_;
105 const byte* end_;
106};
107
108class WasmModuleWriter : public ZoneObject {
109 public:
110 WasmModuleIndex* WriteTo(Zone* zone) const;
111
112 private:
113 friend class WasmModuleBuilder;
114 explicit WasmModuleWriter(Zone* zone);
115 ZoneVector<WasmFunctionEncoder*> functions_;
116 ZoneVector<WasmDataSegmentEncoder*> data_segments_;
117 ZoneVector<FunctionSig*> signatures_;
118 ZoneVector<uint16_t> indirect_functions_;
119 ZoneVector<std::pair<MachineType, bool>> globals_;
Ben Murdochda12d292016-06-02 14:46:10 +0100120 int start_function_index_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121};
122
123class WasmModuleBuilder : public ZoneObject {
124 public:
125 explicit WasmModuleBuilder(Zone* zone);
126 uint16_t AddFunction();
127 uint32_t AddGlobal(MachineType type, bool exported);
128 WasmFunctionBuilder* FunctionAt(size_t index);
129 void AddDataSegment(WasmDataSegmentEncoder* data);
130 uint16_t AddSignature(FunctionSig* sig);
131 void AddIndirectFunction(uint16_t index);
Ben Murdochda12d292016-06-02 14:46:10 +0100132 void MarkStartFunction(uint16_t index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133 WasmModuleWriter* Build(Zone* zone);
134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135 struct CompareFunctionSigs {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100136 bool operator()(FunctionSig* a, FunctionSig* b) const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 };
138 typedef ZoneMap<FunctionSig*, uint16_t, CompareFunctionSigs> SignatureMap;
139
Ben Murdoch097c5b22016-05-18 11:27:45 +0100140 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 Zone* zone_;
142 ZoneVector<FunctionSig*> signatures_;
143 ZoneVector<WasmFunctionBuilder*> functions_;
144 ZoneVector<WasmDataSegmentEncoder*> data_segments_;
145 ZoneVector<uint16_t> indirect_functions_;
146 ZoneVector<std::pair<MachineType, bool>> globals_;
147 SignatureMap signature_map_;
Ben Murdochda12d292016-06-02 14:46:10 +0100148 int start_function_index_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149};
150
151std::vector<uint8_t> UnsignedLEB128From(uint32_t result);
152} // namespace wasm
153} // namespace internal
154} // namespace v8
155
156#endif // V8_WASM_ENCODER_H_