blob: 7b651bf95eaf1874196f0af7b676fa7e2354006d [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
45 bool HasLocals() const {
Ben Murdoch097c5b22016-05-18 11:27:45 +010046 return (local_i32_count_ + local_i64_count_ + local_f32_count_ +
47 local_f64_count_) > 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048 }
49
Ben Murdoch097c5b22016-05-18 11:27:45 +010050 bool HasName() const { return (exported_ || external_) && name_.size() > 0; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000051};
52
53class WasmFunctionBuilder : public ZoneObject {
54 public:
55 uint16_t AddParam(LocalType type);
56 uint16_t AddLocal(LocalType type);
57 void ReturnType(LocalType type);
58 void EmitCode(const byte* code, uint32_t code_size);
59 void EmitCode(const byte* code, uint32_t code_size,
60 const uint32_t* local_indices, uint32_t indices_size);
61 void Emit(WasmOpcode opcode);
62 void EmitWithU8(WasmOpcode opcode, const byte immediate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000063 uint32_t EmitEditableImmediate(const byte immediate);
64 void EditImmediate(uint32_t offset, const byte immediate);
65 void Exported(uint8_t flag);
66 void External(uint8_t flag);
67 void SetName(const unsigned char* name, int name_length);
68 WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const;
69
70 private:
71 explicit WasmFunctionBuilder(Zone* zone);
72 friend class WasmModuleBuilder;
73 LocalType return_type_;
74 struct Type;
75 ZoneVector<Type> locals_;
76 uint8_t exported_;
77 uint8_t external_;
78 ZoneVector<uint8_t> body_;
79 ZoneVector<uint32_t> local_indices_;
80 ZoneVector<char> name_;
81 uint16_t AddVar(LocalType type, bool param);
82 void IndexVars(WasmFunctionEncoder* e, uint16_t* var_index) const;
83};
84
85class WasmDataSegmentEncoder : public ZoneObject {
86 public:
87 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
88 uint32_t dest);
89 uint32_t HeaderSize() const;
90 uint32_t BodySize() const;
91 void Serialize(byte* buffer, byte** header, byte** body) const;
92
93 private:
94 ZoneVector<byte> data_;
95 uint32_t dest_;
96};
97
98class WasmModuleIndex : public ZoneObject {
99 public:
100 const byte* Begin() const { return begin_; }
101 const byte* End() const { return end_; }
102
103 private:
104 friend class WasmModuleWriter;
105 WasmModuleIndex(const byte* begin, const byte* end)
106 : begin_(begin), end_(end) {}
107 const byte* begin_;
108 const byte* end_;
109};
110
111class WasmModuleWriter : public ZoneObject {
112 public:
113 WasmModuleIndex* WriteTo(Zone* zone) const;
114
115 private:
116 friend class WasmModuleBuilder;
117 explicit WasmModuleWriter(Zone* zone);
118 ZoneVector<WasmFunctionEncoder*> functions_;
119 ZoneVector<WasmDataSegmentEncoder*> data_segments_;
120 ZoneVector<FunctionSig*> signatures_;
121 ZoneVector<uint16_t> indirect_functions_;
122 ZoneVector<std::pair<MachineType, bool>> globals_;
123};
124
125class WasmModuleBuilder : public ZoneObject {
126 public:
127 explicit WasmModuleBuilder(Zone* zone);
128 uint16_t AddFunction();
129 uint32_t AddGlobal(MachineType type, bool exported);
130 WasmFunctionBuilder* FunctionAt(size_t index);
131 void AddDataSegment(WasmDataSegmentEncoder* data);
132 uint16_t AddSignature(FunctionSig* sig);
133 void AddIndirectFunction(uint16_t index);
134 WasmModuleWriter* Build(Zone* zone);
135
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 struct CompareFunctionSigs {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100137 bool operator()(FunctionSig* a, FunctionSig* b) const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 };
139 typedef ZoneMap<FunctionSig*, uint16_t, CompareFunctionSigs> SignatureMap;
140
Ben Murdoch097c5b22016-05-18 11:27:45 +0100141 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 Zone* zone_;
143 ZoneVector<FunctionSig*> signatures_;
144 ZoneVector<WasmFunctionBuilder*> functions_;
145 ZoneVector<WasmDataSegmentEncoder*> data_segments_;
146 ZoneVector<uint16_t> indirect_functions_;
147 ZoneVector<std::pair<MachineType, bool>> globals_;
148 SignatureMap signature_map_;
149};
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_