LCOV - code coverage report
Current view: top level - src/wasm - wasm-module-builder.h (source / functions) Hit Total Coverage
Test: app.info Lines: 64 73 87.7 %
Date: 2017-04-26 Functions: 5 5 100.0 %

          Line data    Source code
       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/local-decl-encoder.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       48822 :       : zone_(zone), buffer_(reinterpret_cast<byte*>(zone->New(initial))) {
      26       48822 :     pos_ = buffer_;
      27       48822 :     end_ = buffer_ + initial;
      28             :   }
      29             : 
      30             :   void write_u8(uint8_t x) {
      31     2746850 :     EnsureSpace(1);
      32     2746850 :     *(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        8284 :     EnsureSpace(4);
      43        8284 :     WriteLittleEndianValue<uint32_t>(pos_, x);
      44        8284 :     pos_ += 4;
      45             :   }
      46             : 
      47             :   void write_u64(uint64_t x) {
      48       10439 :     EnsureSpace(8);
      49       10439 :     WriteLittleEndianValue<uint64_t>(pos_, x);
      50       10439 :     pos_ += 8;
      51             :   }
      52             : 
      53      847813 :   void write_u32v(uint32_t val) {
      54      847813 :     EnsureSpace(kMaxVarInt32Size);
      55             :     LEBHelper::write_u32v(&pos_, val);
      56      847813 :   }
      57             : 
      58             :   void write_i32v(int32_t val) {
      59      594903 :     EnsureSpace(kMaxVarInt32Size);
      60      594903 :     LEBHelper::write_i32v(&pos_, val);
      61             :   }
      62             : 
      63             :   void write_u64v(uint64_t val) {
      64             :     EnsureSpace(kMaxVarInt64Size);
      65             :     LEBHelper::write_u64v(&pos_, val);
      66             :   }
      67             : 
      68             :   void write_i64v(int64_t val) {
      69           0 :     EnsureSpace(kMaxVarInt64Size);
      70           0 :     LEBHelper::write_i64v(&pos_, val);
      71             :   }
      72             : 
      73      117209 :   void write_size(size_t val) {
      74      117209 :     EnsureSpace(kMaxVarInt32Size);
      75             :     DCHECK_EQ(val, static_cast<uint32_t>(val));
      76      117209 :     LEBHelper::write_u32v(&pos_, static_cast<uint32_t>(val));
      77      117209 :   }
      78             : 
      79             :   void write_f32(float val) { write_u32(bit_cast<uint32_t>(val)); }
      80             : 
      81             :   void write_f64(double val) { write_u64(bit_cast<uint64_t>(val)); }
      82             : 
      83       90091 :   void write(const byte* data, size_t size) {
      84       90091 :     EnsureSpace(size);
      85       90091 :     memcpy(pos_, data, size);
      86       90091 :     pos_ += size;
      87       90091 :   }
      88             : 
      89       31143 :   size_t reserve_u32v() {
      90             :     size_t off = offset();
      91       31143 :     EnsureSpace(kMaxVarInt32Size);
      92       31143 :     pos_ += kMaxVarInt32Size;
      93             :     return off;
      94             :   }
      95             : 
      96             :   // Patch a (padded) u32v at the given offset to be the given value.
      97             :   void patch_u32v(size_t offset, uint32_t val) {
      98       57852 :     byte* ptr = buffer_ + offset;
      99      289260 :     for (size_t pos = 0; pos != kPaddedVarInt32Size; ++pos) {
     100      289260 :       uint32_t next = val >> 7;
     101      289260 :       byte out = static_cast<byte>(val & 0x7f);
     102      289260 :       if (pos != kPaddedVarInt32Size - 1) {
     103      231408 :         *(ptr++) = 0x80 | out;
     104             :         val = next;
     105             :       } else {
     106       57852 :         *(ptr++) = out;
     107             :       }
     108             :     }
     109             :   }
     110             : 
     111             :   void patch_u8(size_t offset, byte val) {
     112             :     DCHECK_GE(size(), offset);
     113        1133 :     buffer_[offset] = val;
     114             :   }
     115             : 
     116       82228 :   size_t offset() const { return static_cast<size_t>(pos_ - buffer_); }
     117      192040 :   size_t size() const { return static_cast<size_t>(pos_ - buffer_); }
     118             :   const byte* begin() const { return buffer_; }
     119             :   const byte* end() const { return pos_; }
     120             : 
     121     4466674 :   void EnsureSpace(size_t size) {
     122     4466674 :     if ((pos_ + size) > end_) {
     123        4423 :       size_t new_size = 4096 + size + (end_ - buffer_) * 3;
     124        4423 :       byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size));
     125        4423 :       memcpy(new_buffer, buffer_, (pos_ - buffer_));
     126        4423 :       pos_ = new_buffer + (pos_ - buffer_);
     127        4423 :       buffer_ = new_buffer;
     128        4423 :       end_ = new_buffer + new_size;
     129             :     }
     130             :     DCHECK(pos_ + size <= end_);
     131     4466674 :   }
     132             : 
     133             :   void Truncate(size_t size) {
     134             :     DCHECK_GE(offset(), size);
     135       29068 :     pos_ = buffer_ + size;
     136             :   }
     137             : 
     138             :   byte** pos_ptr() { return &pos_; }
     139             : 
     140             :  private:
     141             :   Zone* zone_;
     142             :   byte* buffer_;
     143             :   byte* pos_;
     144             :   byte* end_;
     145             : };
     146             : 
     147             : class WasmModuleBuilder;
     148             : 
     149             : class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject {
     150             :  public:
     151             :   // Building methods.
     152             :   void SetSignature(FunctionSig* sig);
     153             :   uint32_t AddLocal(ValueType type);
     154             :   void EmitI32V(int32_t val);
     155             :   void EmitU32V(uint32_t val);
     156             :   void EmitCode(const byte* code, uint32_t code_size);
     157             :   void Emit(WasmOpcode opcode);
     158             :   void EmitGetLocal(uint32_t index);
     159             :   void EmitSetLocal(uint32_t index);
     160             :   void EmitTeeLocal(uint32_t index);
     161             :   void EmitI32Const(int32_t val);
     162             :   void EmitI64Const(int64_t val);
     163             :   void EmitF32Const(float val);
     164             :   void EmitF64Const(double val);
     165             :   void EmitWithU8(WasmOpcode opcode, const byte immediate);
     166             :   void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
     167             :   void EmitWithI32V(WasmOpcode opcode, int32_t immediate);
     168             :   void EmitWithU32V(WasmOpcode opcode, uint32_t immediate);
     169             :   void EmitDirectCallIndex(uint32_t index);
     170             :   void ExportAs(Vector<const char> name);
     171             :   void SetName(Vector<const char> name);
     172             :   void AddAsmWasmOffset(int call_position, int to_number_position);
     173             :   void SetAsmFunctionStartPosition(int position);
     174             : 
     175       30216 :   size_t GetPosition() const { return body_.size(); }
     176             :   void FixupByte(size_t position, byte value) {
     177        1133 :     body_.patch_u8(position, value);
     178             :   }
     179             :   void DeleteCodeAfter(size_t position);
     180             : 
     181             :   void WriteSignature(ZoneBuffer& buffer) const;
     182             :   void WriteExports(ZoneBuffer& buffer) const;
     183             :   void WriteBody(ZoneBuffer& buffer) const;
     184             :   void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const;
     185             : 
     186             :   uint32_t func_index() { return func_index_; }
     187             :   FunctionSig* signature();
     188             : 
     189             :  private:
     190             :   explicit WasmFunctionBuilder(WasmModuleBuilder* builder);
     191             :   friend class WasmModuleBuilder;
     192             :   friend class WasmTemporary;
     193             : 
     194             :   struct DirectCallIndex {
     195             :     size_t offset;
     196             :     uint32_t direct_index;
     197             :   };
     198             : 
     199             :   WasmModuleBuilder* builder_;
     200             :   LocalDeclEncoder locals_;
     201             :   uint32_t signature_index_;
     202             :   uint32_t func_index_;
     203             :   ZoneBuffer body_;
     204             :   ZoneVector<char> name_;
     205             :   ZoneVector<ZoneVector<char>> exported_names_;
     206             :   ZoneVector<uint32_t> i32_temps_;
     207             :   ZoneVector<uint32_t> i64_temps_;
     208             :   ZoneVector<uint32_t> f32_temps_;
     209             :   ZoneVector<uint32_t> f64_temps_;
     210             :   ZoneVector<DirectCallIndex> direct_calls_;
     211             : 
     212             :   // Delta-encoded mapping from wasm bytes to asm.js source positions.
     213             :   ZoneBuffer asm_offsets_;
     214             :   uint32_t last_asm_byte_offset_ = 0;
     215             :   uint32_t last_asm_source_position_ = 0;
     216             :   uint32_t asm_func_start_source_position_ = 0;
     217             : };
     218             : 
     219             : class WasmTemporary {
     220             :  public:
     221        1234 :   WasmTemporary(WasmFunctionBuilder* builder, ValueType type) {
     222        1234 :     switch (type) {
     223             :       case kWasmI32:
     224        1234 :         temporary_ = &builder->i32_temps_;
     225        1234 :         break;
     226             :       case kWasmI64:
     227           0 :         temporary_ = &builder->i64_temps_;
     228           0 :         break;
     229             :       case kWasmF32:
     230           0 :         temporary_ = &builder->f32_temps_;
     231           0 :         break;
     232             :       case kWasmF64:
     233           0 :         temporary_ = &builder->f64_temps_;
     234           0 :         break;
     235             :       default:
     236           0 :         UNREACHABLE();
     237             :         temporary_ = nullptr;
     238             :     }
     239        2468 :     if (temporary_->size() == 0) {
     240             :       // Allocate a new temporary.
     241         574 :       index_ = builder->AddLocal(type);
     242             :     } else {
     243             :       // Reuse a previous temporary.
     244         660 :       index_ = temporary_->back();
     245             :       temporary_->pop_back();
     246             :     }
     247        1234 :   }
     248             :   ~WasmTemporary() {
     249        1206 :     temporary_->push_back(index_);  // return the temporary to the list.
     250             :   }
     251             :   uint32_t index() { return index_; }
     252             : 
     253             :  private:
     254             :   ZoneVector<uint32_t>* temporary_;
     255             :   uint32_t index_;
     256             : };
     257             : 
     258             : class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
     259             :  public:
     260             :   explicit WasmModuleBuilder(Zone* zone);
     261             : 
     262             :   // Building methods.
     263             :   // TODO(mstarzinger): Refactor to take Vector<const char> instead.
     264             :   uint32_t AddImport(const char* name, int name_length, FunctionSig* sig);
     265             :   void SetImportName(uint32_t index, const char* name, int name_length) {
     266        1794 :     function_imports_[index].name = name;
     267         897 :     function_imports_[index].name_length = name_length;
     268             :   }
     269             :   WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr);
     270             :   uint32_t AddGlobal(ValueType type, bool exported, bool mutability = true,
     271             :                      const WasmInitExpr& init = WasmInitExpr());
     272             :   // TODO(mstarzinger): Refactor to take Vector<const char> instead.
     273             :   uint32_t AddGlobalImport(const char* name, int name_length, ValueType type);
     274             :   void AddDataSegment(const byte* data, uint32_t size, uint32_t dest);
     275             :   uint32_t AddSignature(FunctionSig* sig);
     276             :   uint32_t AllocateIndirectFunctions(uint32_t count);
     277             :   void SetIndirectFunction(uint32_t indirect, uint32_t direct);
     278             :   void MarkStartFunction(WasmFunctionBuilder* builder);
     279             : 
     280             :   // Writing methods.
     281             :   void WriteTo(ZoneBuffer& buffer) const;
     282             :   void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const;
     283             : 
     284             :   // TODO(titzer): use SignatureMap from signature-map.h here.
     285             :   // This signature map is zone-allocated, but the other is heap allocated.
     286             :   struct CompareFunctionSigs {
     287             :     bool operator()(FunctionSig* a, FunctionSig* b) const;
     288             :   };
     289             :   typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap;
     290             : 
     291             :   Zone* zone() { return zone_; }
     292             : 
     293        2328 :   FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; }
     294             : 
     295             :  private:
     296             :   struct WasmFunctionImport {
     297             :     uint32_t sig_index;
     298             :     const char* name;
     299             :     int name_length;
     300             :   };
     301             : 
     302             :   struct WasmGlobalImport {
     303             :     ValueTypeCode type_code;
     304             :     const char* name;
     305             :     int name_length;
     306             :   };
     307             : 
     308             :   struct WasmGlobal {
     309             :     ValueType type;
     310             :     bool exported;
     311             :     bool mutability;
     312             :     WasmInitExpr init;
     313             :   };
     314             : 
     315          14 :   struct WasmDataSegment {
     316             :     ZoneVector<byte> data;
     317             :     uint32_t dest;
     318             :   };
     319             : 
     320             :   friend class WasmFunctionBuilder;
     321             :   Zone* zone_;
     322             :   ZoneVector<FunctionSig*> signatures_;
     323             :   ZoneVector<WasmFunctionImport> function_imports_;
     324             :   ZoneVector<WasmGlobalImport> global_imports_;
     325             :   ZoneVector<WasmFunctionBuilder*> functions_;
     326             :   ZoneVector<WasmDataSegment> data_segments_;
     327             :   ZoneVector<uint32_t> indirect_functions_;
     328             :   ZoneVector<WasmGlobal> globals_;
     329             :   SignatureMap signature_map_;
     330             :   int start_function_index_;
     331             : };
     332             : 
     333             : inline FunctionSig* WasmFunctionBuilder::signature() {
     334       45834 :   return builder_->signatures_[signature_index_];
     335             : }
     336             : 
     337             : }  // namespace wasm
     338             : }  // namespace internal
     339             : }  // namespace v8
     340             : 
     341             : #endif  // V8_WASM_WASM_MODULE_BUILDER_H_

Generated by: LCOV version 1.10