1// Copyright 2019 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/regexp/regexp-bytecodes.h"
6
7#include <cctype>
8
9#include "src/utils/utils.h"
10
11namespace v8 {
12namespace internal {
13
14void RegExpBytecodeDisassembleSingle(const byte* code_base, const byte* pc) {
15  int bytecode = *reinterpret_cast<const int32_t*>(pc) & BYTECODE_MASK;
16  PrintF("%s", RegExpBytecodeName(bytecode));
17
18  // Args and the bytecode as hex.
19  for (int i = 0; i < RegExpBytecodeLength(bytecode); i++) {
20    PrintF(", %02x", pc[i]);
21  }
22  PrintF(" ");
23
24  // Args as ascii.
25  for (int i = 1; i < RegExpBytecodeLength(bytecode); i++) {
26    unsigned char b = pc[i];
27    PrintF("%c", std::isprint(b) ? b : '.');
28  }
29  PrintF("\n");
30}
31
32void RegExpBytecodeDisassemble(const byte* code_base, int length,
33                               const char* pattern) {
34  PrintF("[generated bytecode for regexp pattern: '%s']\n", pattern);
35
36  ptrdiff_t offset = 0;
37
38  while (offset < length) {
39    const byte* const pc = code_base + offset;
40    PrintF("%p  %4" V8PRIxPTRDIFF "  ", pc, offset);
41    RegExpBytecodeDisassembleSingle(code_base, pc);
42    offset += RegExpBytecodeLength(*pc);
43  }
44}
45
46}  // namespace internal
47}  // namespace v8
48