1// Copyright 2010 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#ifndef V8_VM_STATE_INL_H_
6#define V8_VM_STATE_INL_H_
7
8#include "src/vm-state.h"
9#include "src/log.h"
10#include "src/simulator.h"
11
12namespace v8 {
13namespace internal {
14
15//
16// VMState class implementation.  A simple stack of VM states held by the
17// logger and partially threaded through the call stack.  States are pushed by
18// VMState construction and popped by destruction.
19//
20inline const char* StateToString(StateTag state) {
21  switch (state) {
22    case JS:
23      return "JS";
24    case GC:
25      return "GC";
26    case COMPILER:
27      return "COMPILER";
28    case OTHER:
29      return "OTHER";
30    case EXTERNAL:
31      return "EXTERNAL";
32    default:
33      UNREACHABLE();
34      return NULL;
35  }
36}
37
38
39template <StateTag Tag>
40VMState<Tag>::VMState(Isolate* isolate)
41    : isolate_(isolate), previous_tag_(isolate->current_vm_state()) {
42  if (FLAG_log_timer_events && previous_tag_ != EXTERNAL && Tag == EXTERNAL) {
43    LOG(isolate_,
44        TimerEvent(Logger::START, Logger::TimerEventScope::v8_external));
45  }
46  isolate_->set_current_vm_state(Tag);
47}
48
49
50template <StateTag Tag>
51VMState<Tag>::~VMState() {
52  if (FLAG_log_timer_events && previous_tag_ != EXTERNAL && Tag == EXTERNAL) {
53    LOG(isolate_,
54        TimerEvent(Logger::END, Logger::TimerEventScope::v8_external));
55  }
56  isolate_->set_current_vm_state(previous_tag_);
57}
58
59
60ExternalCallbackScope::ExternalCallbackScope(Isolate* isolate, Address callback)
61    : isolate_(isolate),
62      callback_(callback),
63      previous_scope_(isolate->external_callback_scope()) {
64#ifdef USE_SIMULATOR
65  scope_address_ = Simulator::current(isolate)->get_sp();
66#endif
67  isolate_->set_external_callback_scope(this);
68}
69
70ExternalCallbackScope::~ExternalCallbackScope() {
71  isolate_->set_external_callback_scope(previous_scope_);
72}
73
74Address ExternalCallbackScope::scope_address() {
75#ifdef USE_SIMULATOR
76  return scope_address_;
77#else
78  return reinterpret_cast<Address>(this);
79#endif
80}
81
82
83} }  // namespace v8::internal
84
85#endif  // V8_VM_STATE_INL_H_
86