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.
4 
5 #ifndef V8_FRAMES_INL_H_
6 #define V8_FRAMES_INL_H_
7 
8 #include "src/frames.h"
9 #include "src/isolate.h"
10 #include "src/objects-inl.h"
11 #include "src/v8memory.h"
12 
13 #if V8_TARGET_ARCH_IA32
14 #include "src/ia32/frames-ia32.h"  // NOLINT
15 #elif V8_TARGET_ARCH_X64
16 #include "src/x64/frames-x64.h"  // NOLINT
17 #elif V8_TARGET_ARCH_ARM64
18 #include "src/arm64/frames-arm64.h"  // NOLINT
19 #elif V8_TARGET_ARCH_ARM
20 #include "src/arm/frames-arm.h"  // NOLINT
21 #elif V8_TARGET_ARCH_PPC
22 #include "src/ppc/frames-ppc.h"  // NOLINT
23 #elif V8_TARGET_ARCH_MIPS
24 #include "src/mips/frames-mips.h"  // NOLINT
25 #elif V8_TARGET_ARCH_MIPS64
26 #include "src/mips64/frames-mips64.h"  // NOLINT
27 #elif V8_TARGET_ARCH_X87
28 #include "src/x87/frames-x87.h"  // NOLINT
29 #else
30 #error Unsupported target architecture.
31 #endif
32 
33 namespace v8 {
34 namespace internal {
35 
36 
address()37 inline Address StackHandler::address() const {
38   return reinterpret_cast<Address>(const_cast<StackHandler*>(this));
39 }
40 
41 
next()42 inline StackHandler* StackHandler::next() const {
43   const int offset = StackHandlerConstants::kNextOffset;
44   return FromAddress(Memory::Address_at(address() + offset));
45 }
46 
47 
FromAddress(Address address)48 inline StackHandler* StackHandler::FromAddress(Address address) {
49   return reinterpret_cast<StackHandler*>(address);
50 }
51 
52 
StackFrame(StackFrameIteratorBase * iterator)53 inline StackFrame::StackFrame(StackFrameIteratorBase* iterator)
54     : iterator_(iterator), isolate_(iterator_->isolate()) {
55 }
56 
57 
top_handler()58 inline StackHandler* StackFrame::top_handler() const {
59   return iterator_->handler();
60 }
61 
62 
LookupCode()63 inline Code* StackFrame::LookupCode() const {
64   return GetContainingCode(isolate(), pc());
65 }
66 
67 
GetContainingCode(Isolate * isolate,Address pc)68 inline Code* StackFrame::GetContainingCode(Isolate* isolate, Address pc) {
69   return isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code;
70 }
71 
72 
ResolveReturnAddressLocation(Address * pc_address)73 inline Address* StackFrame::ResolveReturnAddressLocation(Address* pc_address) {
74   if (return_address_location_resolver_ == NULL) {
75     return pc_address;
76   } else {
77     return reinterpret_cast<Address*>(
78         return_address_location_resolver_(
79             reinterpret_cast<uintptr_t>(pc_address)));
80   }
81 }
82 
83 
EntryFrame(StackFrameIteratorBase * iterator)84 inline EntryFrame::EntryFrame(StackFrameIteratorBase* iterator)
85     : StackFrame(iterator) {
86 }
87 
88 
EntryConstructFrame(StackFrameIteratorBase * iterator)89 inline EntryConstructFrame::EntryConstructFrame(
90     StackFrameIteratorBase* iterator)
91     : EntryFrame(iterator) {
92 }
93 
94 
ExitFrame(StackFrameIteratorBase * iterator)95 inline ExitFrame::ExitFrame(StackFrameIteratorBase* iterator)
96     : StackFrame(iterator) {
97 }
98 
99 
StandardFrame(StackFrameIteratorBase * iterator)100 inline StandardFrame::StandardFrame(StackFrameIteratorBase* iterator)
101     : StackFrame(iterator) {
102 }
103 
104 
GetExpression(int index)105 inline Object* StandardFrame::GetExpression(int index) const {
106   return Memory::Object_at(GetExpressionAddress(index));
107 }
108 
109 
SetExpression(int index,Object * value)110 inline void StandardFrame::SetExpression(int index, Object* value) {
111   Memory::Object_at(GetExpressionAddress(index)) = value;
112 }
113 
114 
context()115 inline Object* StandardFrame::context() const {
116   const int offset = StandardFrameConstants::kContextOffset;
117   return Memory::Object_at(fp() + offset);
118 }
119 
120 
caller_fp()121 inline Address StandardFrame::caller_fp() const {
122   return Memory::Address_at(fp() + StandardFrameConstants::kCallerFPOffset);
123 }
124 
125 
caller_pc()126 inline Address StandardFrame::caller_pc() const {
127   return Memory::Address_at(ComputePCAddress(fp()));
128 }
129 
130 
ComputePCAddress(Address fp)131 inline Address StandardFrame::ComputePCAddress(Address fp) {
132   return fp + StandardFrameConstants::kCallerPCOffset;
133 }
134 
135 
ComputeConstantPoolAddress(Address fp)136 inline Address StandardFrame::ComputeConstantPoolAddress(Address fp) {
137   return fp + StandardFrameConstants::kConstantPoolOffset;
138 }
139 
140 
IsArgumentsAdaptorFrame(Address fp)141 inline bool StandardFrame::IsArgumentsAdaptorFrame(Address fp) {
142   Object* marker =
143       Memory::Object_at(fp + StandardFrameConstants::kContextOffset);
144   return marker == Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR);
145 }
146 
147 
IsConstructFrame(Address fp)148 inline bool StandardFrame::IsConstructFrame(Address fp) {
149   Object* marker =
150       Memory::Object_at(fp + StandardFrameConstants::kMarkerOffset);
151   return marker == Smi::FromInt(StackFrame::CONSTRUCT);
152 }
153 
154 
JavaScriptFrame(StackFrameIteratorBase * iterator)155 inline JavaScriptFrame::JavaScriptFrame(StackFrameIteratorBase* iterator)
156     : StandardFrame(iterator) {
157 }
158 
159 
GetParameterSlot(int index)160 Address JavaScriptFrame::GetParameterSlot(int index) const {
161   int param_count = ComputeParametersCount();
162   DCHECK(-1 <= index && index < param_count);
163   int parameter_offset = (param_count - index - 1) * kPointerSize;
164   return caller_sp() + parameter_offset;
165 }
166 
167 
GetParameter(int index)168 Object* JavaScriptFrame::GetParameter(int index) const {
169   return Memory::Object_at(GetParameterSlot(index));
170 }
171 
172 
GetOperandSlot(int index)173 inline Address JavaScriptFrame::GetOperandSlot(int index) const {
174   Address base = fp() + JavaScriptFrameConstants::kLocal0Offset;
175   DCHECK(IsAddressAligned(base, kPointerSize));
176   DCHECK_EQ(type(), JAVA_SCRIPT);
177   DCHECK_LT(index, ComputeOperandsCount());
178   DCHECK_LE(0, index);
179   // Operand stack grows down.
180   return base - index * kPointerSize;
181 }
182 
183 
GetOperand(int index)184 inline Object* JavaScriptFrame::GetOperand(int index) const {
185   return Memory::Object_at(GetOperandSlot(index));
186 }
187 
188 
ComputeOperandsCount()189 inline int JavaScriptFrame::ComputeOperandsCount() const {
190   Address base = fp() + JavaScriptFrameConstants::kLocal0Offset;
191   // Base points to low address of first operand and stack grows down, so add
192   // kPointerSize to get the actual stack size.
193   intptr_t stack_size_in_bytes = (base + kPointerSize) - sp();
194   DCHECK(IsAligned(stack_size_in_bytes, kPointerSize));
195   DCHECK(type() == JAVA_SCRIPT);
196   DCHECK(stack_size_in_bytes >= 0);
197   return static_cast<int>(stack_size_in_bytes >> kPointerSizeLog2);
198 }
199 
200 
receiver()201 inline Object* JavaScriptFrame::receiver() const {
202   return GetParameter(-1);
203 }
204 
205 
set_receiver(Object * value)206 inline void JavaScriptFrame::set_receiver(Object* value) {
207   Memory::Object_at(GetParameterSlot(-1)) = value;
208 }
209 
210 
has_adapted_arguments()211 inline bool JavaScriptFrame::has_adapted_arguments() const {
212   return IsArgumentsAdaptorFrame(caller_fp());
213 }
214 
215 
function()216 inline JSFunction* JavaScriptFrame::function() const {
217   return JSFunction::cast(function_slot_object());
218 }
219 
220 
function_slot_object()221 inline Object* JavaScriptFrame::function_slot_object() const {
222   const int offset = JavaScriptFrameConstants::kFunctionOffset;
223   return Memory::Object_at(fp() + offset);
224 }
225 
226 
StubFrame(StackFrameIteratorBase * iterator)227 inline StubFrame::StubFrame(StackFrameIteratorBase* iterator)
228     : StandardFrame(iterator) {
229 }
230 
231 
OptimizedFrame(StackFrameIteratorBase * iterator)232 inline OptimizedFrame::OptimizedFrame(StackFrameIteratorBase* iterator)
233     : JavaScriptFrame(iterator) {
234 }
235 
236 
InterpretedFrame(StackFrameIteratorBase * iterator)237 inline InterpretedFrame::InterpretedFrame(StackFrameIteratorBase* iterator)
238     : JavaScriptFrame(iterator) {}
239 
240 
ArgumentsAdaptorFrame(StackFrameIteratorBase * iterator)241 inline ArgumentsAdaptorFrame::ArgumentsAdaptorFrame(
242     StackFrameIteratorBase* iterator) : JavaScriptFrame(iterator) {
243 }
244 
245 
InternalFrame(StackFrameIteratorBase * iterator)246 inline InternalFrame::InternalFrame(StackFrameIteratorBase* iterator)
247     : StandardFrame(iterator) {
248 }
249 
250 
StubFailureTrampolineFrame(StackFrameIteratorBase * iterator)251 inline StubFailureTrampolineFrame::StubFailureTrampolineFrame(
252     StackFrameIteratorBase* iterator) : StandardFrame(iterator) {
253 }
254 
255 
ConstructFrame(StackFrameIteratorBase * iterator)256 inline ConstructFrame::ConstructFrame(StackFrameIteratorBase* iterator)
257     : InternalFrame(iterator) {
258 }
259 
260 
JavaScriptFrameIterator(Isolate * isolate)261 inline JavaScriptFrameIterator::JavaScriptFrameIterator(
262     Isolate* isolate)
263     : iterator_(isolate) {
264   if (!done()) Advance();
265 }
266 
267 
JavaScriptFrameIterator(Isolate * isolate,ThreadLocalTop * top)268 inline JavaScriptFrameIterator::JavaScriptFrameIterator(
269     Isolate* isolate, ThreadLocalTop* top)
270     : iterator_(isolate, top) {
271   if (!done()) Advance();
272 }
273 
274 
frame()275 inline JavaScriptFrame* JavaScriptFrameIterator::frame() const {
276   // TODO(1233797): The frame hierarchy needs to change. It's
277   // problematic that we can't use the safe-cast operator to cast to
278   // the JavaScript frame type, because we may encounter arguments
279   // adaptor frames.
280   StackFrame* frame = iterator_.frame();
281   DCHECK(frame->is_java_script() || frame->is_arguments_adaptor());
282   return static_cast<JavaScriptFrame*>(frame);
283 }
284 
285 
frame()286 inline StackFrame* SafeStackFrameIterator::frame() const {
287   DCHECK(!done());
288   DCHECK(frame_->is_java_script() || frame_->is_exit());
289   return frame_;
290 }
291 
292 
293 }  // namespace internal
294 }  // namespace v8
295 
296 #endif  // V8_FRAMES_INL_H_
297