11cb0ef41Sopenharmony_ci// Copyright 2017 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#ifndef V8_INTERPRETER_BYTECODE_SOURCE_INFO_H_
61cb0ef41Sopenharmony_ci#define V8_INTERPRETER_BYTECODE_SOURCE_INFO_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/common/globals.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace v8 {
111cb0ef41Sopenharmony_cinamespace internal {
121cb0ef41Sopenharmony_cinamespace interpreter {
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci// Source code position information.
151cb0ef41Sopenharmony_ciclass BytecodeSourceInfo final {
161cb0ef41Sopenharmony_ci public:
171cb0ef41Sopenharmony_ci  static const int kUninitializedPosition = -1;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  BytecodeSourceInfo()
201cb0ef41Sopenharmony_ci      : position_type_(PositionType::kNone),
211cb0ef41Sopenharmony_ci        source_position_(kUninitializedPosition) {}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  BytecodeSourceInfo(int source_position, bool is_statement)
241cb0ef41Sopenharmony_ci      : position_type_(is_statement ? PositionType::kStatement
251cb0ef41Sopenharmony_ci                                    : PositionType::kExpression),
261cb0ef41Sopenharmony_ci        source_position_(source_position) {
271cb0ef41Sopenharmony_ci    DCHECK_GE(source_position, 0);
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // Makes instance into a statement position.
311cb0ef41Sopenharmony_ci  void MakeStatementPosition(int source_position) {
321cb0ef41Sopenharmony_ci    // Statement positions can be replaced by other statement
331cb0ef41Sopenharmony_ci    // positions. For example , "for (x = 0; x < 3; ++x) 7;" has a
341cb0ef41Sopenharmony_ci    // statement position associated with 7 but no bytecode associated
351cb0ef41Sopenharmony_ci    // with it. Then Next is emitted after the body and has
361cb0ef41Sopenharmony_ci    // statement position and overrides the existing one.
371cb0ef41Sopenharmony_ci    position_type_ = PositionType::kStatement;
381cb0ef41Sopenharmony_ci    source_position_ = source_position;
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  // Makes instance into an expression position. Instance should not
421cb0ef41Sopenharmony_ci  // be a statement position otherwise it could be lost and impair the
431cb0ef41Sopenharmony_ci  // debugging experience.
441cb0ef41Sopenharmony_ci  void MakeExpressionPosition(int source_position) {
451cb0ef41Sopenharmony_ci    DCHECK(!is_statement());
461cb0ef41Sopenharmony_ci    position_type_ = PositionType::kExpression;
471cb0ef41Sopenharmony_ci    source_position_ = source_position;
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  // Forces an instance into an expression position.
511cb0ef41Sopenharmony_ci  void ForceExpressionPosition(int source_position) {
521cb0ef41Sopenharmony_ci    position_type_ = PositionType::kExpression;
531cb0ef41Sopenharmony_ci    source_position_ = source_position;
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  int source_position() const {
571cb0ef41Sopenharmony_ci    DCHECK(is_valid());
581cb0ef41Sopenharmony_ci    return source_position_;
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  bool is_statement() const {
621cb0ef41Sopenharmony_ci    return position_type_ == PositionType::kStatement;
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci  bool is_expression() const {
651cb0ef41Sopenharmony_ci    return position_type_ == PositionType::kExpression;
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  bool is_valid() const { return position_type_ != PositionType::kNone; }
691cb0ef41Sopenharmony_ci  void set_invalid() {
701cb0ef41Sopenharmony_ci    position_type_ = PositionType::kNone;
711cb0ef41Sopenharmony_ci    source_position_ = kUninitializedPosition;
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  bool operator==(const BytecodeSourceInfo& other) const {
751cb0ef41Sopenharmony_ci    return position_type_ == other.position_type_ &&
761cb0ef41Sopenharmony_ci           source_position_ == other.source_position_;
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  bool operator!=(const BytecodeSourceInfo& other) const {
801cb0ef41Sopenharmony_ci    return position_type_ != other.position_type_ ||
811cb0ef41Sopenharmony_ci           source_position_ != other.source_position_;
821cb0ef41Sopenharmony_ci  }
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci private:
851cb0ef41Sopenharmony_ci  enum class PositionType : uint8_t { kNone, kExpression, kStatement };
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  PositionType position_type_;
881cb0ef41Sopenharmony_ci  int source_position_;
891cb0ef41Sopenharmony_ci};
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ciV8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
921cb0ef41Sopenharmony_ci                                           const BytecodeSourceInfo& info);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci}  // namespace interpreter
951cb0ef41Sopenharmony_ci}  // namespace internal
961cb0ef41Sopenharmony_ci}  // namespace v8
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci#endif  // V8_INTERPRETER_BYTECODE_SOURCE_INFO_H_
99