11cb0ef41Sopenharmony_ci// Copyright 2020 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/experimental/experimental-bytecode.h" 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci#include <cctype> 81cb0ef41Sopenharmony_ci#include <iomanip> 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cinamespace v8 { 111cb0ef41Sopenharmony_cinamespace internal { 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cinamespace { 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cistd::ostream& PrintAsciiOrHex(std::ostream& os, base::uc16 c) { 161cb0ef41Sopenharmony_ci if (c < 128 && std::isprint(c)) { 171cb0ef41Sopenharmony_ci os << static_cast<char>(c); 181cb0ef41Sopenharmony_ci } else { 191cb0ef41Sopenharmony_ci os << "0x" << std::hex << static_cast<int>(c); 201cb0ef41Sopenharmony_ci } 211cb0ef41Sopenharmony_ci return os; 221cb0ef41Sopenharmony_ci} 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci} // namespace 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& os, const RegExpInstruction& inst) { 271cb0ef41Sopenharmony_ci switch (inst.opcode) { 281cb0ef41Sopenharmony_ci case RegExpInstruction::CONSUME_RANGE: { 291cb0ef41Sopenharmony_ci os << "CONSUME_RANGE ["; 301cb0ef41Sopenharmony_ci PrintAsciiOrHex(os, inst.payload.consume_range.min); 311cb0ef41Sopenharmony_ci os << ", "; 321cb0ef41Sopenharmony_ci PrintAsciiOrHex(os, inst.payload.consume_range.max); 331cb0ef41Sopenharmony_ci os << "]"; 341cb0ef41Sopenharmony_ci break; 351cb0ef41Sopenharmony_ci } 361cb0ef41Sopenharmony_ci case RegExpInstruction::ASSERTION: 371cb0ef41Sopenharmony_ci os << "ASSERTION "; 381cb0ef41Sopenharmony_ci switch (inst.payload.assertion_type) { 391cb0ef41Sopenharmony_ci case RegExpAssertion::Type::START_OF_INPUT: 401cb0ef41Sopenharmony_ci os << "START_OF_INPUT"; 411cb0ef41Sopenharmony_ci break; 421cb0ef41Sopenharmony_ci case RegExpAssertion::Type::END_OF_INPUT: 431cb0ef41Sopenharmony_ci os << "END_OF_INPUT"; 441cb0ef41Sopenharmony_ci break; 451cb0ef41Sopenharmony_ci case RegExpAssertion::Type::START_OF_LINE: 461cb0ef41Sopenharmony_ci os << "START_OF_LINE"; 471cb0ef41Sopenharmony_ci break; 481cb0ef41Sopenharmony_ci case RegExpAssertion::Type::END_OF_LINE: 491cb0ef41Sopenharmony_ci os << "END_OF_LINE"; 501cb0ef41Sopenharmony_ci break; 511cb0ef41Sopenharmony_ci case RegExpAssertion::Type::BOUNDARY: 521cb0ef41Sopenharmony_ci os << "BOUNDARY"; 531cb0ef41Sopenharmony_ci break; 541cb0ef41Sopenharmony_ci case RegExpAssertion::Type::NON_BOUNDARY: 551cb0ef41Sopenharmony_ci os << "NON_BOUNDARY"; 561cb0ef41Sopenharmony_ci break; 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci break; 591cb0ef41Sopenharmony_ci case RegExpInstruction::FORK: 601cb0ef41Sopenharmony_ci os << "FORK " << inst.payload.pc; 611cb0ef41Sopenharmony_ci break; 621cb0ef41Sopenharmony_ci case RegExpInstruction::JMP: 631cb0ef41Sopenharmony_ci os << "JMP " << inst.payload.pc; 641cb0ef41Sopenharmony_ci break; 651cb0ef41Sopenharmony_ci case RegExpInstruction::ACCEPT: 661cb0ef41Sopenharmony_ci os << "ACCEPT"; 671cb0ef41Sopenharmony_ci break; 681cb0ef41Sopenharmony_ci case RegExpInstruction::SET_REGISTER_TO_CP: 691cb0ef41Sopenharmony_ci os << "SET_REGISTER_TO_CP " << inst.payload.register_index; 701cb0ef41Sopenharmony_ci break; 711cb0ef41Sopenharmony_ci case RegExpInstruction::CLEAR_REGISTER: 721cb0ef41Sopenharmony_ci os << "CLEAR_REGISTER " << inst.payload.register_index; 731cb0ef41Sopenharmony_ci break; 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci return os; 761cb0ef41Sopenharmony_ci} 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_cinamespace { 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci// The maximum number of digits required to display a non-negative number < n 811cb0ef41Sopenharmony_ci// in base 10. 821cb0ef41Sopenharmony_ciint DigitsRequiredBelow(int n) { 831cb0ef41Sopenharmony_ci DCHECK_GE(n, 0); 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci int result = 1; 861cb0ef41Sopenharmony_ci for (int i = 10; i < n; i *= 10) { 871cb0ef41Sopenharmony_ci result += 1; 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci return result; 901cb0ef41Sopenharmony_ci} 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci} // namespace 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_cistd::ostream& operator<<(std::ostream& os, 951cb0ef41Sopenharmony_ci base::Vector<const RegExpInstruction> insts) { 961cb0ef41Sopenharmony_ci int inst_num = insts.length(); 971cb0ef41Sopenharmony_ci int line_digit_num = DigitsRequiredBelow(inst_num); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci for (int i = 0; i != inst_num; ++i) { 1001cb0ef41Sopenharmony_ci const RegExpInstruction& inst = insts[i]; 1011cb0ef41Sopenharmony_ci os << std::setfill('0') << std::setw(line_digit_num) << i << ": " << inst 1021cb0ef41Sopenharmony_ci << std::endl; 1031cb0ef41Sopenharmony_ci } 1041cb0ef41Sopenharmony_ci return os; 1051cb0ef41Sopenharmony_ci} 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci} // namespace internal 1081cb0ef41Sopenharmony_ci} // namespace v8 109