regexp-bytecodes.cc 1.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/regexp/regexp-bytecodes.h"

#include <cctype>

#include "src/utils/utils.h"

namespace v8 {
namespace internal {

void RegExpBytecodeDisassembleSingle(const byte* code_base, const byte* pc) {
15 16
  int bytecode = *reinterpret_cast<const int32_t*>(pc) & BYTECODE_MASK;
  PrintF("%s", RegExpBytecodeName(bytecode));
17 18

  // Args and the bytecode as hex.
19
  for (int i = 0; i < RegExpBytecodeLength(bytecode); i++) {
20 21 22 23 24
    PrintF(", %02x", pc[i]);
  }
  PrintF(" ");

  // Args as ascii.
25
  for (int i = 1; i < RegExpBytecodeLength(bytecode); i++) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    unsigned char b = pc[i];
    PrintF("%c", std::isprint(b) ? b : '.');
  }
  PrintF("\n");
}

void RegExpBytecodeDisassemble(const byte* code_base, int length,
                               const char* pattern) {
  PrintF("[generated bytecode for regexp pattern: '%s']\n", pattern);

  ptrdiff_t offset = 0;

  while (offset < length) {
    const byte* const pc = code_base + offset;
    PrintF("%p  %4" V8PRIxPTRDIFF "  ", pc, offset);
    RegExpBytecodeDisassembleSingle(code_base, pc);
    offset += RegExpBytecodeLength(*pc);
  }
}

}  // namespace internal
}  // namespace v8