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