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_WASM_MODULE_BUILDER_H_
      6 #define V8_WASM_WASM_MODULE_BUILDER_H_
      7 
      8 #include "src/signature.h"
      9 #include "src/zone/zone-containers.h"
     10 
     11 #include "src/wasm/leb-helper.h"
     12 #include "src/wasm/wasm-macro-gen.h"
     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 ZoneBuffer : public ZoneObject {
     22  public:
     23   static const uint32_t kInitialSize = 4096;
     24   explicit ZoneBuffer(Zone* zone, size_t initial = kInitialSize)
     25       : zone_(zone), buffer_(reinterpret_cast<byte*>(zone->New(initial))) {
     26     pos_ = buffer_;
     27     end_ = buffer_ + initial;
     28   }
     29 
     30   void write_u8(uint8_t x) {
     31     EnsureSpace(1);
     32     *(pos_++) = x;
     33   }
     34 
     35   void write_u16(uint16_t x) {
     36     EnsureSpace(2);
     37     WriteLittleEndianValue<uint16_t>(pos_, x);
     38     pos_ += 2;
     39   }
     40 
     41   void write_u32(uint32_t x) {
     42     EnsureSpace(4);
     43     WriteLittleEndianValue<uint32_t>(pos_, x);
     44     pos_ += 4;
     45   }
     46 
     47   void write_u32v(uint32_t val) {
     48     EnsureSpace(kMaxVarInt32Size);
     49     LEBHelper::write_u32v(&pos_, val);
     50   }
     51 
     52   void write_i32v(int32_t val) {
     53     EnsureSpace(kMaxVarInt32Size);
     54     LEBHelper::write_i32v(&pos_, val);
     55   }
     56 
     57   void write_size(size_t val) {
     58     EnsureSpace(kMaxVarInt32Size);
     59     DCHECK_EQ(val, static_cast<uint32_t>(val));
     60     LEBHelper::write_u32v(&pos_, static_cast<uint32_t>(val));
     61   }
     62 
     63   void write(const byte* data, size_t size) {
     64     EnsureSpace(size);
     65     memcpy(pos_, data, size);
     66     pos_ += size;
     67   }
     68 
     69   size_t reserve_u32v() {
     70     size_t off = offset();
     71     EnsureSpace(kMaxVarInt32Size);
     72     pos_ += kMaxVarInt32Size;
     73     return off;
     74   }
     75 
     76   // Patch a (padded) u32v at the given offset to be the given value.
     77   void patch_u32v(size_t offset, uint32_t val) {
     78     byte* ptr = buffer_ + offset;
     79     for (size_t pos = 0; pos != kPaddedVarInt32Size; ++pos) {
     80       uint32_t next = val >> 7;
     81       byte out = static_cast<byte>(val & 0x7f);
     82       if (pos != kPaddedVarInt32Size - 1) {
     83         *(ptr++) = 0x80 | out;
     84         val = next;
     85       } else {
     86         *(ptr++) = out;
     87       }
     88     }
     89   }
     90 
     91   size_t offset() const { return static_cast<size_t>(pos_ - buffer_); }
     92   size_t size() const { return static_cast<size_t>(pos_ - buffer_); }
     93   const byte* begin() const { return buffer_; }
     94   const byte* end() const { return pos_; }
     95 
     96   void EnsureSpace(size_t size) {
     97     if ((pos_ + size) > end_) {
     98       size_t new_size = 4096 + size + (end_ - buffer_) * 3;
     99       byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size));
    100       memcpy(new_buffer, buffer_, (pos_ - buffer_));
    101       pos_ = new_buffer + (pos_ - buffer_);
    102       buffer_ = new_buffer;
    103       end_ = new_buffer + new_size;
    104     }
    105     DCHECK(pos_ + size <= end_);
    106   }
    107 
    108   byte** pos_ptr() { return &pos_; }
    109 
    110  private:
    111   Zone* zone_;
    112   byte* buffer_;
    113   byte* pos_;
    114   byte* end_;
    115 };
    116 
    117 class WasmModuleBuilder;
    118 
    119 class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject {
    120  public:
    121   // Building methods.
    122   void SetSignature(FunctionSig* sig);
    123   uint32_t AddLocal(ValueType type);
    124   void EmitVarInt(int32_t val);
    125   void EmitVarUint(uint32_t val);
    126   void EmitCode(const byte* code, uint32_t code_size);
    127   void Emit(WasmOpcode opcode);
    128   void EmitGetLocal(uint32_t index);
    129   void EmitSetLocal(uint32_t index);
    130   void EmitTeeLocal(uint32_t index);
    131   void EmitI32Const(int32_t val);
    132   void EmitWithU8(WasmOpcode opcode, const byte immediate);
    133   void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
    134   void EmitWithVarInt(WasmOpcode opcode, int32_t immediate);
    135   void EmitWithVarUint(WasmOpcode opcode, uint32_t immediate);
    136   void EmitDirectCallIndex(uint32_t index);
    137   void ExportAs(Vector<const char> name);
    138   void SetName(Vector<const char> name);
    139   void AddAsmWasmOffset(int call_position, int to_number_position);
    140   void SetAsmFunctionStartPosition(int position);
    141 
    142   void WriteSignature(ZoneBuffer& buffer) const;
    143   void WriteExports(ZoneBuffer& buffer) const;
    144   void WriteBody(ZoneBuffer& buffer) const;
    145   void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const;
    146 
    147   uint32_t func_index() { return func_index_; }
    148   FunctionSig* signature();
    149 
    150  private:
    151   explicit WasmFunctionBuilder(WasmModuleBuilder* builder);
    152   friend class WasmModuleBuilder;
    153   friend class WasmTemporary;
    154 
    155   struct DirectCallIndex {
    156     size_t offset;
    157     uint32_t direct_index;
    158   };
    159 
    160   WasmModuleBuilder* builder_;
    161   LocalDeclEncoder locals_;
    162   uint32_t signature_index_;
    163   uint32_t func_index_;
    164   ZoneVector<uint8_t> body_;
    165   ZoneVector<char> name_;
    166   ZoneVector<ZoneVector<char>> exported_names_;
    167   ZoneVector<uint32_t> i32_temps_;
    168   ZoneVector<uint32_t> i64_temps_;
    169   ZoneVector<uint32_t> f32_temps_;
    170   ZoneVector<uint32_t> f64_temps_;
    171   ZoneVector<DirectCallIndex> direct_calls_;
    172 
    173   // Delta-encoded mapping from wasm bytes to asm.js source positions.
    174   ZoneBuffer asm_offsets_;
    175   uint32_t last_asm_byte_offset_ = 0;
    176   uint32_t last_asm_source_position_ = 0;
    177   uint32_t asm_func_start_source_position_ = 0;
    178 };
    179 
    180 class WasmTemporary {
    181  public:
    182   WasmTemporary(WasmFunctionBuilder* builder, ValueType type) {
    183     switch (type) {
    184       case kWasmI32:
    185         temporary_ = &builder->i32_temps_;
    186         break;
    187       case kWasmI64:
    188         temporary_ = &builder->i64_temps_;
    189         break;
    190       case kWasmF32:
    191         temporary_ = &builder->f32_temps_;
    192         break;
    193       case kWasmF64:
    194         temporary_ = &builder->f64_temps_;
    195         break;
    196       default:
    197         UNREACHABLE();
    198         temporary_ = nullptr;
    199     }
    200     if (temporary_->size() == 0) {
    201       // Allocate a new temporary.
    202       index_ = builder->AddLocal(type);
    203     } else {
    204       // Reuse a previous temporary.
    205       index_ = temporary_->back();
    206       temporary_->pop_back();
    207     }
    208   }
    209   ~WasmTemporary() {
    210     temporary_->push_back(index_);  // return the temporary to the list.
    211   }
    212   uint32_t index() { return index_; }
    213 
    214  private:
    215   ZoneVector<uint32_t>* temporary_;
    216   uint32_t index_;
    217 };
    218 
    219 class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
    220  public:
    221   explicit WasmModuleBuilder(Zone* zone);
    222 
    223   // Building methods.
    224   uint32_t AddImport(const char* name, int name_length, FunctionSig* sig);
    225   void SetImportName(uint32_t index, const char* name, int name_length) {
    226     imports_[index].name = name;
    227     imports_[index].name_length = name_length;
    228   }
    229   WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr);
    230   uint32_t AddGlobal(ValueType type, bool exported, bool mutability = true,
    231                      const WasmInitExpr& init = WasmInitExpr());
    232   void AddDataSegment(const byte* data, uint32_t size, uint32_t dest);
    233   uint32_t AddSignature(FunctionSig* sig);
    234   uint32_t AllocateIndirectFunctions(uint32_t count);
    235   void SetIndirectFunction(uint32_t indirect, uint32_t direct);
    236   void MarkStartFunction(WasmFunctionBuilder* builder);
    237 
    238   // Writing methods.
    239   void WriteTo(ZoneBuffer& buffer) const;
    240   void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const;
    241 
    242   // TODO(titzer): use SignatureMap from signature-map.h here.
    243   // This signature map is zone-allocated, but the other is heap allocated.
    244   struct CompareFunctionSigs {
    245     bool operator()(FunctionSig* a, FunctionSig* b) const;
    246   };
    247   typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap;
    248 
    249   Zone* zone() { return zone_; }
    250 
    251   FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; }
    252 
    253  private:
    254   struct WasmFunctionImport {
    255     uint32_t sig_index;
    256     const char* name;
    257     int name_length;
    258   };
    259 
    260   struct WasmGlobal {
    261     ValueType type;
    262     bool exported;
    263     bool mutability;
    264     WasmInitExpr init;
    265   };
    266 
    267   struct WasmDataSegment {
    268     ZoneVector<byte> data;
    269     uint32_t dest;
    270   };
    271 
    272   friend class WasmFunctionBuilder;
    273   Zone* zone_;
    274   ZoneVector<FunctionSig*> signatures_;
    275   ZoneVector<WasmFunctionImport> imports_;
    276   ZoneVector<WasmFunctionBuilder*> functions_;
    277   ZoneVector<WasmDataSegment> data_segments_;
    278   ZoneVector<uint32_t> indirect_functions_;
    279   ZoneVector<WasmGlobal> globals_;
    280   SignatureMap signature_map_;
    281   int start_function_index_;
    282 };
    283 
    284 inline FunctionSig* WasmFunctionBuilder::signature() {
    285   return builder_->signatures_[signature_index_];
    286 }
    287 
    288 }  // namespace wasm
    289 }  // namespace internal
    290 }  // namespace v8
    291 
    292 #endif  // V8_WASM_WASM_MODULE_BUILDER_H_
    293