1 // Copyright 2012 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.
5 #ifndef V8_FRAMES_INL_H_
6 #define V8_FRAMES_INL_H_
8 #include "src/frames.h"
9 #include "src/isolate.h"
10 #include "src/v8memory.h"
12 #if V8_TARGET_ARCH_IA32
13 #include "src/ia32/frames-ia32.h" // NOLINT
14 #elif V8_TARGET_ARCH_X64
15 #include "src/x64/frames-x64.h" // NOLINT
16 #elif V8_TARGET_ARCH_ARM64
17 #include "src/arm64/frames-arm64.h" // NOLINT
18 #elif V8_TARGET_ARCH_ARM
19 #include "src/arm/frames-arm.h" // NOLINT
20 #elif V8_TARGET_ARCH_PPC
21 #include "src/ppc/frames-ppc.h" // NOLINT
22 #elif V8_TARGET_ARCH_MIPS
23 #include "src/mips/frames-mips.h" // NOLINT
24 #elif V8_TARGET_ARCH_MIPS64
25 #include "src/mips64/frames-mips64.h" // NOLINT
26 #elif V8_TARGET_ARCH_X87
27 #include "src/x87/frames-x87.h" // NOLINT
29 #error Unsupported target architecture.
36 inline Address StackHandler::address() const {
37 return reinterpret_cast<Address>(const_cast<StackHandler*>(this));
41 inline StackHandler* StackHandler::next() const {
42 const int offset = StackHandlerConstants::kNextOffset;
43 return FromAddress(Memory::Address_at(address() + offset));
47 inline bool StackHandler::includes(Address address) const {
48 Address start = this->address();
49 Address end = start + StackHandlerConstants::kSize;
50 return start <= address && address <= end;
54 inline void StackHandler::Iterate(ObjectVisitor* v, Code* holder) const {
55 v->VisitPointer(context_address());
56 v->VisitPointer(code_address());
60 inline StackHandler* StackHandler::FromAddress(Address address) {
61 return reinterpret_cast<StackHandler*>(address);
65 inline bool StackHandler::is_js_entry() const {
66 return kind() == JS_ENTRY;
70 inline bool StackHandler::is_catch() const {
71 return kind() == CATCH;
75 inline bool StackHandler::is_finally() const {
76 return kind() == FINALLY;
80 inline StackHandler::Kind StackHandler::kind() const {
81 const int offset = StackHandlerConstants::kStateIntOffset;
82 return KindField::decode(Memory::unsigned_at(address() + offset));
86 inline unsigned StackHandler::index() const {
87 const int offset = StackHandlerConstants::kStateIntOffset;
88 return IndexField::decode(Memory::unsigned_at(address() + offset));
92 inline Object** StackHandler::context_address() const {
93 const int offset = StackHandlerConstants::kContextOffset;
94 return reinterpret_cast<Object**>(address() + offset);
98 inline Object** StackHandler::code_address() const {
99 const int offset = StackHandlerConstants::kCodeOffset;
100 return reinterpret_cast<Object**>(address() + offset);
104 inline StackFrame::StackFrame(StackFrameIteratorBase* iterator)
105 : iterator_(iterator), isolate_(iterator_->isolate()) {
109 inline StackHandler* StackFrame::top_handler() const {
110 return iterator_->handler();
114 inline Code* StackFrame::LookupCode() const {
115 return GetContainingCode(isolate(), pc());
119 inline Code* StackFrame::GetContainingCode(Isolate* isolate, Address pc) {
120 return isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code;
124 inline Address* StackFrame::ResolveReturnAddressLocation(Address* pc_address) {
125 if (return_address_location_resolver_ == NULL) {
128 return reinterpret_cast<Address*>(
129 return_address_location_resolver_(
130 reinterpret_cast<uintptr_t>(pc_address)));
135 inline EntryFrame::EntryFrame(StackFrameIteratorBase* iterator)
136 : StackFrame(iterator) {
140 inline EntryConstructFrame::EntryConstructFrame(
141 StackFrameIteratorBase* iterator)
142 : EntryFrame(iterator) {
146 inline ExitFrame::ExitFrame(StackFrameIteratorBase* iterator)
147 : StackFrame(iterator) {
151 inline StandardFrame::StandardFrame(StackFrameIteratorBase* iterator)
152 : StackFrame(iterator) {
156 inline Object* StandardFrame::GetExpression(int index) const {
157 return Memory::Object_at(GetExpressionAddress(index));
161 inline void StandardFrame::SetExpression(int index, Object* value) {
162 Memory::Object_at(GetExpressionAddress(index)) = value;
166 inline Object* StandardFrame::context() const {
167 const int offset = StandardFrameConstants::kContextOffset;
168 return Memory::Object_at(fp() + offset);
172 inline Address StandardFrame::caller_fp() const {
173 return Memory::Address_at(fp() + StandardFrameConstants::kCallerFPOffset);
177 inline Address StandardFrame::caller_pc() const {
178 return Memory::Address_at(ComputePCAddress(fp()));
182 inline Address StandardFrame::ComputePCAddress(Address fp) {
183 return fp + StandardFrameConstants::kCallerPCOffset;
187 inline Address StandardFrame::ComputeConstantPoolAddress(Address fp) {
188 return fp + StandardFrameConstants::kConstantPoolOffset;
192 inline bool StandardFrame::IsArgumentsAdaptorFrame(Address fp) {
194 Memory::Object_at(fp + StandardFrameConstants::kContextOffset);
195 return marker == Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR);
199 inline bool StandardFrame::IsConstructFrame(Address fp) {
201 Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset);
202 return marker == Smi::FromInt(StackFrame::CONSTRUCT);
206 inline JavaScriptFrame::JavaScriptFrame(StackFrameIteratorBase* iterator)
207 : StandardFrame(iterator) {
211 Address JavaScriptFrame::GetParameterSlot(int index) const {
212 int param_count = ComputeParametersCount();
213 DCHECK(-1 <= index && index < param_count);
214 int parameter_offset = (param_count - index - 1) * kPointerSize;
215 return caller_sp() + parameter_offset;
219 Object* JavaScriptFrame::GetParameter(int index) const {
220 return Memory::Object_at(GetParameterSlot(index));
224 inline Address JavaScriptFrame::GetOperandSlot(int index) const {
225 Address base = fp() + JavaScriptFrameConstants::kLocal0Offset;
226 DCHECK(IsAddressAligned(base, kPointerSize));
227 DCHECK_EQ(type(), JAVA_SCRIPT);
228 DCHECK_LT(index, ComputeOperandsCount());
230 // Operand stack grows down.
231 return base - index * kPointerSize;
235 inline Object* JavaScriptFrame::GetOperand(int index) const {
236 return Memory::Object_at(GetOperandSlot(index));
240 inline int JavaScriptFrame::ComputeOperandsCount() const {
241 Address base = fp() + JavaScriptFrameConstants::kLocal0Offset;
242 // Base points to low address of first operand and stack grows down, so add
243 // kPointerSize to get the actual stack size.
244 intptr_t stack_size_in_bytes = (base + kPointerSize) - sp();
245 DCHECK(IsAligned(stack_size_in_bytes, kPointerSize));
246 DCHECK(type() == JAVA_SCRIPT);
247 DCHECK(stack_size_in_bytes >= 0);
248 return static_cast<int>(stack_size_in_bytes >> kPointerSizeLog2);
252 inline Object* JavaScriptFrame::receiver() const {
253 return GetParameter(-1);
257 inline void JavaScriptFrame::set_receiver(Object* value) {
258 Memory::Object_at(GetParameterSlot(-1)) = value;
262 inline bool JavaScriptFrame::has_adapted_arguments() const {
263 return IsArgumentsAdaptorFrame(caller_fp());
267 inline JSFunction* JavaScriptFrame::function() const {
268 return JSFunction::cast(function_slot_object());
272 inline StubFrame::StubFrame(StackFrameIteratorBase* iterator)
273 : StandardFrame(iterator) {
277 inline OptimizedFrame::OptimizedFrame(StackFrameIteratorBase* iterator)
278 : JavaScriptFrame(iterator) {
282 inline ArgumentsAdaptorFrame::ArgumentsAdaptorFrame(
283 StackFrameIteratorBase* iterator) : JavaScriptFrame(iterator) {
287 inline InternalFrame::InternalFrame(StackFrameIteratorBase* iterator)
288 : StandardFrame(iterator) {
292 inline StubFailureTrampolineFrame::StubFailureTrampolineFrame(
293 StackFrameIteratorBase* iterator) : StandardFrame(iterator) {
297 inline ConstructFrame::ConstructFrame(StackFrameIteratorBase* iterator)
298 : InternalFrame(iterator) {
302 inline JavaScriptFrameIterator::JavaScriptFrameIterator(
304 : iterator_(isolate) {
305 if (!done()) Advance();
309 inline JavaScriptFrameIterator::JavaScriptFrameIterator(
310 Isolate* isolate, ThreadLocalTop* top)
311 : iterator_(isolate, top) {
312 if (!done()) Advance();
316 inline JavaScriptFrame* JavaScriptFrameIterator::frame() const {
317 // TODO(1233797): The frame hierarchy needs to change. It's
318 // problematic that we can't use the safe-cast operator to cast to
319 // the JavaScript frame type, because we may encounter arguments
321 StackFrame* frame = iterator_.frame();
322 DCHECK(frame->is_java_script() || frame->is_arguments_adaptor());
323 return static_cast<JavaScriptFrame*>(frame);
327 inline StackFrame* SafeStackFrameIterator::frame() const {
329 DCHECK(frame_->is_java_script() || frame_->is_exit());
334 } } // namespace v8::internal
336 #endif // V8_FRAMES_INL_H_