blob: c8bd4e80828d92dbe0b5b3ebef211c6a7df409bc [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Block6ded16b2010-05-10 14:33:55 +01004
5#ifndef V8_VM_STATE_INL_H_
6#define V8_VM_STATE_INL_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/vm-state.h"
9#include "src/log.h"
10#include "src/simulator.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010011#include "src/tracing/trace-event.h"
Steve Block6ded16b2010-05-10 14:33:55 +010012
13namespace v8 {
14namespace 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//
Steve Block6ded16b2010-05-10 14:33:55 +010021inline const char* StateToString(StateTag state) {
22 switch (state) {
23 case JS:
24 return "JS";
25 case GC:
26 return "GC";
27 case COMPILER:
28 return "COMPILER";
29 case OTHER:
30 return "OTHER";
Ben Murdochb0fe1622011-05-05 13:52:32 +010031 case EXTERNAL:
32 return "EXTERNAL";
Steve Block6ded16b2010-05-10 14:33:55 +010033 default:
34 UNREACHABLE();
35 return NULL;
36 }
37}
38
Steve Block44f0eee2011-05-26 01:26:41 +010039
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040template <StateTag Tag>
41VMState<Tag>::VMState(Isolate* isolate)
Steve Block44f0eee2011-05-26 01:26:41 +010042 : isolate_(isolate), previous_tag_(isolate->current_vm_state()) {
Ben Murdochda12d292016-06-02 14:46:10 +010043 if (FLAG_log_timer_events && previous_tag_ != EXTERNAL && Tag == EXTERNAL) {
44 LOG(isolate_, TimerEvent(Logger::START, TimerEventExternal::name()));
Steve Block6ded16b2010-05-10 14:33:55 +010045 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 isolate_->set_current_vm_state(Tag);
Steve Block6ded16b2010-05-10 14:33:55 +010047}
48
49
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050template <StateTag Tag>
51VMState<Tag>::~VMState() {
Ben Murdochda12d292016-06-02 14:46:10 +010052 if (FLAG_log_timer_events && previous_tag_ != EXTERNAL && Tag == EXTERNAL) {
53 LOG(isolate_, TimerEvent(Logger::END, TimerEventExternal::name()));
Steve Block6ded16b2010-05-10 14:33:55 +010054 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 isolate_->set_current_vm_state(previous_tag_);
Steve Block6ded16b2010-05-10 14:33:55 +010056}
Ben Murdochb0fe1622011-05-05 13:52:32 +010057
Steve Block44f0eee2011-05-26 01:26:41 +010058ExternalCallbackScope::ExternalCallbackScope(Isolate* isolate, Address callback)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 : isolate_(isolate),
60 callback_(callback),
Ben Murdochda12d292016-06-02 14:46:10 +010061 previous_scope_(isolate->external_callback_scope()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062#ifdef USE_SIMULATOR
63 scope_address_ = Simulator::current(isolate)->get_sp();
64#endif
65 isolate_->set_external_callback_scope(this);
Ben Murdoch097c5b22016-05-18 11:27:45 +010066 if (FLAG_runtime_call_stats) {
Ben Murdochda12d292016-06-02 14:46:10 +010067 RuntimeCallStats* stats = isolate->counters()->runtime_call_stats();
68 timer_.Initialize(&stats->ExternalCallback, stats->current_timer());
69 stats->Enter(&timer_);
Ben Murdoch097c5b22016-05-18 11:27:45 +010070 }
Ben Murdochda12d292016-06-02 14:46:10 +010071 TRACE_EVENT_BEGIN0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"),
72 "V8.ExternalCallback");
Ben Murdochb0fe1622011-05-05 13:52:32 +010073}
74
75ExternalCallbackScope::~ExternalCallbackScope() {
Ben Murdoch097c5b22016-05-18 11:27:45 +010076 if (FLAG_runtime_call_stats) {
77 isolate_->counters()->runtime_call_stats()->Leave(&timer_);
78 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 isolate_->set_external_callback_scope(previous_scope_);
Ben Murdochda12d292016-06-02 14:46:10 +010080 TRACE_EVENT_END0(TRACE_DISABLED_BY_DEFAULT("v8.runtime"),
81 "V8.ExternalCallback");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082}
83
84Address ExternalCallbackScope::scope_address() {
85#ifdef USE_SIMULATOR
86 return scope_address_;
87#else
88 return reinterpret_cast<Address>(this);
89#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +010090}
91
Ben Murdochb0fe1622011-05-05 13:52:32 +010092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093} // namespace internal
94} // namespace v8
Steve Block6ded16b2010-05-10 14:33:55 +010095
96#endif // V8_VM_STATE_INL_H_