Home | History | Annotate | Download | only in wasm
      1 // 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 
     17 namespace v8 {
     18 namespace internal {
     19 namespace wasm {
     20 
     21 class WasmModuleBuilder;
     22 
     23 class 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_;
     36   uint16_t local_int32_count_;
     37   uint16_t local_int64_count_;
     38   uint16_t local_float32_count_;
     39   uint16_t local_float64_count_;
     40   bool exported_;
     41   bool external_;
     42   ZoneVector<uint8_t> body_;
     43   ZoneVector<char> name_;
     44 
     45   bool HasLocals() const {
     46     return (local_int32_count_ + local_int64_count_ + local_float32_count_ +
     47             local_float64_count_) > 0;
     48   }
     49 
     50   bool HasName() const { return exported_ && name_.size() > 0; }
     51 };
     52 
     53 class 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);
     63   void EmitWithLocal(WasmOpcode opcode);
     64   uint32_t EmitEditableImmediate(const byte immediate);
     65   void EditImmediate(uint32_t offset, const byte immediate);
     66   void Exported(uint8_t flag);
     67   void External(uint8_t flag);
     68   void SetName(const unsigned char* name, int name_length);
     69   WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const;
     70 
     71  private:
     72   explicit WasmFunctionBuilder(Zone* zone);
     73   friend class WasmModuleBuilder;
     74   LocalType return_type_;
     75   struct Type;
     76   ZoneVector<Type> locals_;
     77   uint8_t exported_;
     78   uint8_t external_;
     79   ZoneVector<uint8_t> body_;
     80   ZoneVector<uint32_t> local_indices_;
     81   ZoneVector<char> name_;
     82   uint16_t AddVar(LocalType type, bool param);
     83   void IndexVars(WasmFunctionEncoder* e, uint16_t* var_index) const;
     84 };
     85 
     86 class WasmDataSegmentEncoder : public ZoneObject {
     87  public:
     88   WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
     89                          uint32_t dest);
     90   uint32_t HeaderSize() const;
     91   uint32_t BodySize() const;
     92   void Serialize(byte* buffer, byte** header, byte** body) const;
     93 
     94  private:
     95   ZoneVector<byte> data_;
     96   uint32_t dest_;
     97 };
     98 
     99 class WasmModuleIndex : public ZoneObject {
    100  public:
    101   const byte* Begin() const { return begin_; }
    102   const byte* End() const { return end_; }
    103 
    104  private:
    105   friend class WasmModuleWriter;
    106   WasmModuleIndex(const byte* begin, const byte* end)
    107       : begin_(begin), end_(end) {}
    108   const byte* begin_;
    109   const byte* end_;
    110 };
    111 
    112 class WasmModuleWriter : public ZoneObject {
    113  public:
    114   WasmModuleIndex* WriteTo(Zone* zone) const;
    115 
    116  private:
    117   friend class WasmModuleBuilder;
    118   explicit WasmModuleWriter(Zone* zone);
    119   ZoneVector<WasmFunctionEncoder*> functions_;
    120   ZoneVector<WasmDataSegmentEncoder*> data_segments_;
    121   ZoneVector<FunctionSig*> signatures_;
    122   ZoneVector<uint16_t> indirect_functions_;
    123   ZoneVector<std::pair<MachineType, bool>> globals_;
    124 };
    125 
    126 class WasmModuleBuilder : public ZoneObject {
    127  public:
    128   explicit WasmModuleBuilder(Zone* zone);
    129   uint16_t AddFunction();
    130   uint32_t AddGlobal(MachineType type, bool exported);
    131   WasmFunctionBuilder* FunctionAt(size_t index);
    132   void AddDataSegment(WasmDataSegmentEncoder* data);
    133   uint16_t AddSignature(FunctionSig* sig);
    134   void AddIndirectFunction(uint16_t index);
    135   WasmModuleWriter* Build(Zone* zone);
    136 
    137  private:
    138   struct CompareFunctionSigs {
    139     int operator()(FunctionSig* a, FunctionSig* b) const;
    140   };
    141   typedef ZoneMap<FunctionSig*, uint16_t, CompareFunctionSigs> SignatureMap;
    142 
    143   Zone* zone_;
    144   ZoneVector<FunctionSig*> signatures_;
    145   ZoneVector<WasmFunctionBuilder*> functions_;
    146   ZoneVector<WasmDataSegmentEncoder*> data_segments_;
    147   ZoneVector<uint16_t> indirect_functions_;
    148   ZoneVector<std::pair<MachineType, bool>> globals_;
    149   SignatureMap signature_map_;
    150 };
    151 
    152 std::vector<uint8_t> UnsignedLEB128From(uint32_t result);
    153 }  // namespace wasm
    154 }  // namespace internal
    155 }  // namespace v8
    156 
    157 #endif  // V8_WASM_ENCODER_H_
    158