11cb0ef41Sopenharmony_ci// Copyright 2019 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/regexp/regexp-bytecodes.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include <cctype>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "src/utils/utils.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cinamespace v8 {
121cb0ef41Sopenharmony_cinamespace internal {
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_civoid RegExpBytecodeDisassembleSingle(const byte* code_base, const byte* pc) {
151cb0ef41Sopenharmony_ci  int bytecode = *reinterpret_cast<const int32_t*>(pc) & BYTECODE_MASK;
161cb0ef41Sopenharmony_ci  PrintF("%s", RegExpBytecodeName(bytecode));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  // Args and the bytecode as hex.
191cb0ef41Sopenharmony_ci  for (int i = 0; i < RegExpBytecodeLength(bytecode); i++) {
201cb0ef41Sopenharmony_ci    PrintF(", %02x", pc[i]);
211cb0ef41Sopenharmony_ci  }
221cb0ef41Sopenharmony_ci  PrintF(" ");
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  // Args as ascii.
251cb0ef41Sopenharmony_ci  for (int i = 1; i < RegExpBytecodeLength(bytecode); i++) {
261cb0ef41Sopenharmony_ci    unsigned char b = pc[i];
271cb0ef41Sopenharmony_ci    PrintF("%c", std::isprint(b) ? b : '.');
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci  PrintF("\n");
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_civoid RegExpBytecodeDisassemble(const byte* code_base, int length,
331cb0ef41Sopenharmony_ci                               const char* pattern) {
341cb0ef41Sopenharmony_ci  PrintF("[generated bytecode for regexp pattern: '%s']\n", pattern);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  ptrdiff_t offset = 0;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  while (offset < length) {
391cb0ef41Sopenharmony_ci    const byte* const pc = code_base + offset;
401cb0ef41Sopenharmony_ci    PrintF("%p  %4" V8PRIxPTRDIFF "  ", pc, offset);
411cb0ef41Sopenharmony_ci    RegExpBytecodeDisassembleSingle(code_base, pc);
421cb0ef41Sopenharmony_ci    offset += RegExpBytecodeLength(*pc);
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci}  // namespace internal
471cb0ef41Sopenharmony_ci}  // namespace v8
48