LCOV - code coverage report
Current view: top level - src/wasm - wasm-text.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 64 85 75.3 %
Date: 2019-03-21 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // Copyright 2016 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             : #include "src/wasm/wasm-text.h"
       6             : 
       7             : #include "src/debug/interface-types.h"
       8             : #include "src/objects-inl.h"
       9             : #include "src/ostreams.h"
      10             : #include "src/vector.h"
      11             : #include "src/wasm/function-body-decoder-impl.h"
      12             : #include "src/wasm/function-body-decoder.h"
      13             : #include "src/wasm/wasm-module.h"
      14             : #include "src/wasm/wasm-opcodes.h"
      15             : #include "src/zone/zone.h"
      16             : 
      17             : namespace v8 {
      18             : namespace internal {
      19             : namespace wasm {
      20             : 
      21             : namespace {
      22          80 : bool IsValidFunctionName(const Vector<const char> &name) {
      23          80 :   if (name.is_empty()) return false;
      24             :   const char *special_chars = "_.+-*/\\^~=<>!?@#$%&|:'`";
      25         964 :   for (char c : name) {
      26         504 :     bool valid_char = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
      27         524 :                       (c >= 'A' && c <= 'Z') || strchr(special_chars, c);
      28         444 :     if (!valid_char) return false;
      29             :   }
      30             :   return true;
      31             : }
      32             : 
      33             : }  // namespace
      34             : 
      35          80 : void PrintWasmText(const WasmModule* module, const ModuleWireBytes& wire_bytes,
      36             :                    uint32_t func_index, std::ostream& os,
      37             :                    debug::WasmDisassembly::OffsetTable* offset_table) {
      38             :   DCHECK_NOT_NULL(module);
      39             :   DCHECK_GT(module->functions.size(), func_index);
      40          80 :   const WasmFunction *fun = &module->functions[func_index];
      41             : 
      42         160 :   AccountingAllocator allocator;
      43         160 :   Zone zone(&allocator, ZONE_NAME);
      44          80 :   int line_nr = 0;
      45             :   int control_depth = 1;
      46             : 
      47             :   // Print the function signature.
      48          80 :   os << "func";
      49          80 :   WasmName fun_name = wire_bytes.GetNameOrNull(fun, module);
      50          80 :   if (IsValidFunctionName(fun_name)) {
      51          76 :     os << " $";
      52          76 :     os.write(fun_name.start(), fun_name.length());
      53             :   }
      54          80 :   if (fun->sig->parameter_count()) {
      55          20 :     os << " (param";
      56          80 :     for (auto param : fun->sig->parameters())
      57          40 :       os << ' ' << ValueTypes::TypeName(param);
      58             :     os << ')';
      59             :   }
      60          80 :   if (fun->sig->return_count()) {
      61           4 :     os << " (result";
      62          16 :     for (auto ret : fun->sig->returns()) os << ' ' << ValueTypes::TypeName(ret);
      63             :     os << ')';
      64             :   }
      65          80 :   os << "\n";
      66          80 :   ++line_nr;
      67             : 
      68             :   // Print the local declarations.
      69             :   BodyLocalDecls decls(&zone);
      70             :   Vector<const byte> func_bytes = wire_bytes.GetFunctionBytes(fun);
      71          80 :   BytecodeIterator i(func_bytes.begin(), func_bytes.end(), &decls);
      72             :   DCHECK_LT(func_bytes.begin(), i.pc());
      73          80 :   if (!decls.type_list.empty()) {
      74           8 :     os << "(local";
      75          24 :     for (const ValueType &v : decls.type_list) {
      76          32 :       os << ' ' << ValueTypes::TypeName(v);
      77             :     }
      78           8 :     os << ")\n";
      79           8 :     ++line_nr;
      80             :   }
      81             : 
      82         832 :   for (; i.has_next(); i.next()) {
      83             :     WasmOpcode opcode = i.current();
      84         376 :     if (opcode == kExprElse || opcode == kExprCatch || opcode == kExprEnd) {
      85         116 :       --control_depth;
      86             :     }
      87             : 
      88             :     DCHECK_LE(0, control_depth);
      89         376 :     const int kMaxIndentation = 64;
      90         752 :     int indentation = std::min(kMaxIndentation, 2 * control_depth);
      91         376 :     if (offset_table) {
      92         376 :       offset_table->emplace_back(i.pc_offset(), line_nr, indentation);
      93             :     }
      94             : 
      95             :     // 64 whitespaces
      96             :     const char padding[kMaxIndentation + 1] =
      97         376 :         "                                                                ";
      98         376 :     os.write(padding, indentation);
      99             : 
     100         376 :     switch (opcode) {
     101             :       case kExprLoop:
     102             :       case kExprIf:
     103             :       case kExprBlock:
     104             :       case kExprTry: {
     105             :         BlockTypeImmediate<Decoder::kNoValidate> imm(kAllWasmFeatures, &i,
     106          36 :                                                      i.pc());
     107          36 :         os << WasmOpcodes::OpcodeName(opcode);
     108          36 :         if (imm.type == kWasmVar) {
     109           0 :           os << " (type " << imm.sig_index << ")";
     110          36 :         } else if (imm.out_arity() > 0) {
     111           0 :           os << " " << ValueTypes::TypeName(imm.out_type(0));
     112             :         }
     113          36 :         control_depth++;
     114             :         break;
     115             :       }
     116             :       case kExprBr:
     117             :       case kExprBrIf: {
     118             :         BranchDepthImmediate<Decoder::kNoValidate> imm(&i, i.pc());
     119           8 :         os << WasmOpcodes::OpcodeName(opcode) << ' ' << imm.depth;
     120             :         break;
     121             :       }
     122             :       case kExprBrOnExn: {
     123             :         BranchDepthImmediate<Decoder::kNoValidate> imm_br(&i, i.pc());
     124             :         ExceptionIndexImmediate<Decoder::kNoValidate> imm_idx(
     125           0 :             &i, i.pc() + imm_br.length);
     126           0 :         os << WasmOpcodes::OpcodeName(opcode) << ' ' << imm_br.depth << ' '
     127             :            << imm_idx.index;
     128             :         break;
     129             :       }
     130             :       case kExprElse:
     131             :       case kExprCatch:
     132           0 :         os << WasmOpcodes::OpcodeName(opcode);
     133           0 :         control_depth++;
     134           0 :         break;
     135             :       case kExprEnd:
     136         116 :         os << "end";
     137         116 :         break;
     138             :       case kExprBrTable: {
     139           0 :         BranchTableImmediate<Decoder::kNoValidate> imm(&i, i.pc());
     140             :         BranchTableIterator<Decoder::kNoValidate> iterator(&i, imm);
     141           0 :         os << "br_table";
     142           0 :         while (iterator.has_next()) os << ' ' << iterator.next();
     143             :         break;
     144             :       }
     145             :       case kExprCallIndirect: {
     146             :         CallIndirectImmediate<Decoder::kNoValidate> imm(kAllWasmFeatures, &i,
     147           4 :                                                         i.pc());
     148             :         DCHECK_EQ(0, imm.table_index);
     149           4 :         os << "call_indirect " << imm.sig_index;
     150             :         break;
     151             :       }
     152             :       case kExprCallFunction: {
     153             :         CallFunctionImmediate<Decoder::kNoValidate> imm(&i, i.pc());
     154             :         os << "call " << imm.index;
     155             :         break;
     156             :       }
     157             :       case kExprGetLocal:
     158             :       case kExprSetLocal:
     159             :       case kExprTeeLocal: {
     160             :         LocalIndexImmediate<Decoder::kNoValidate> imm(&i, i.pc());
     161          56 :         os << WasmOpcodes::OpcodeName(opcode) << ' ' << imm.index;
     162             :         break;
     163             :       }
     164             :       case kExprThrow: {
     165             :         ExceptionIndexImmediate<Decoder::kNoValidate> imm(&i, i.pc());
     166           0 :         os << WasmOpcodes::OpcodeName(opcode) << ' ' << imm.index;
     167             :         break;
     168             :       }
     169             :       case kExprGetGlobal:
     170             :       case kExprSetGlobal: {
     171             :         GlobalIndexImmediate<Decoder::kNoValidate> imm(&i, i.pc());
     172           0 :         os << WasmOpcodes::OpcodeName(opcode) << ' ' << imm.index;
     173             :         break;
     174             :       }
     175             : #define CASE_CONST(type, str, cast_type)                        \
     176             :   case kExpr##type##Const: {                                    \
     177             :     Imm##type##Immediate<Decoder::kNoValidate> imm(&i, i.pc()); \
     178             :     os << #str ".const " << static_cast<cast_type>(imm.value);  \
     179             :     break;                                                      \
     180             :   }
     181          44 :         CASE_CONST(I32, i32, int32_t)
     182             :         CASE_CONST(I64, i64, int64_t)
     183           0 :         CASE_CONST(F32, f32, float)
     184           0 :         CASE_CONST(F64, f64, double)
     185             : #undef CASE_CONST
     186             : 
     187             : #define CASE_OPCODE(opcode, _, __) case kExpr##opcode:
     188             :         FOREACH_LOAD_MEM_OPCODE(CASE_OPCODE)
     189             :         FOREACH_STORE_MEM_OPCODE(CASE_OPCODE) {
     190             :           MemoryAccessImmediate<Decoder::kNoValidate> imm(&i, i.pc(),
     191           0 :                                                           kMaxUInt32);
     192           0 :           os << WasmOpcodes::OpcodeName(opcode) << " offset=" << imm.offset
     193           0 :              << " align=" << (1ULL << imm.alignment);
     194             :           break;
     195             :         }
     196             : 
     197             :         FOREACH_SIMPLE_OPCODE(CASE_OPCODE)
     198             :       case kExprUnreachable:
     199             :       case kExprNop:
     200             :       case kExprReturn:
     201             :       case kExprMemorySize:
     202             :       case kExprMemoryGrow:
     203             :       case kExprDrop:
     204             :       case kExprSelect:
     205          56 :         os << WasmOpcodes::OpcodeName(opcode);
     206          56 :         break;
     207             :       case kAtomicPrefix: {
     208             :         WasmOpcode atomic_opcode = i.prefixed_opcode();
     209           8 :         switch (atomic_opcode) {
     210             :           FOREACH_ATOMIC_OPCODE(CASE_OPCODE) {
     211             :             MemoryAccessImmediate<Decoder::kNoValidate> imm(&i, i.pc(),
     212           8 :                                                             kMaxUInt32);
     213           8 :             os << WasmOpcodes::OpcodeName(atomic_opcode)
     214           8 :                << " offset=" << imm.offset
     215           8 :                << " align=" << (1ULL << imm.alignment);
     216             :             break;
     217             :           }
     218             :           default:
     219           0 :             UNREACHABLE();
     220             :             break;
     221             :         }
     222             :         break;
     223             :       }
     224             : 
     225             :         // This group is just printed by their internal opcode name, as they
     226             :         // should never be shown to end-users.
     227             :         FOREACH_ASMJS_COMPAT_OPCODE(CASE_OPCODE)
     228             :         // TODO(wasm): Add correct printing for SIMD and atomic opcodes once
     229             :         // they are publicly available.
     230             :         FOREACH_SIMD_0_OPERAND_OPCODE(CASE_OPCODE)
     231             :         FOREACH_SIMD_1_OPERAND_OPCODE(CASE_OPCODE)
     232             :         FOREACH_SIMD_MASK_OPERAND_OPCODE(CASE_OPCODE)
     233             :         FOREACH_SIMD_MEM_OPCODE(CASE_OPCODE)
     234           0 :         os << WasmOpcodes::OpcodeName(opcode);
     235           0 :         break;
     236             : #undef CASE_OPCODE
     237             : 
     238             :       default:
     239           0 :         UNREACHABLE();
     240             :         break;
     241             :     }
     242             :     os << '\n';
     243         376 :     ++line_nr;
     244             :   }
     245             :   DCHECK_EQ(0, control_depth);
     246             :   DCHECK(i.ok());
     247          80 : }
     248             : 
     249             : }  // namespace wasm
     250             : }  // namespace internal
     251      120216 : }  // namespace v8

Generated by: LCOV version 1.10