blob: 7c327dd3216ba52fca83318fb5503fc226198bce [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "frames-inl.h"
31#include "mark-compact.h"
32#include "scopeinfo.h"
33#include "string-stream.h"
34#include "top.h"
35#include "zone-inl.h"
36
37namespace v8 {
38namespace internal {
39
40// Iterator that supports traversing the stack handlers of a
41// particular frame. Needs to know the top of the handler chain.
42class StackHandlerIterator BASE_EMBEDDED {
43 public:
44 StackHandlerIterator(const StackFrame* frame, StackHandler* handler)
45 : limit_(frame->fp()), handler_(handler) {
46 // Make sure the handler has already been unwound to this frame.
47 ASSERT(frame->sp() <= handler->address());
48 }
49
50 StackHandler* handler() const { return handler_; }
51
52 bool done() {
53 return handler_ == NULL || handler_->address() > limit_;
54 }
55 void Advance() {
56 ASSERT(!done());
57 handler_ = handler_->next();
58 }
59
60 private:
61 const Address limit_;
62 StackHandler* handler_;
63};
64
65
66// -------------------------------------------------------------------------
67
68
69#define INITIALIZE_SINGLETON(type, field) field##_(this),
70StackFrameIterator::StackFrameIterator()
71 : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
72 frame_(NULL), handler_(NULL), thread_(Top::GetCurrentThread()),
73 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
74 Reset();
75}
76StackFrameIterator::StackFrameIterator(ThreadLocalTop* t)
77 : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
78 frame_(NULL), handler_(NULL), thread_(t),
79 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
80 Reset();
81}
82StackFrameIterator::StackFrameIterator(bool use_top, Address fp, Address sp)
83 : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
84 frame_(NULL), handler_(NULL),
85 thread_(use_top ? Top::GetCurrentThread() : NULL),
86 fp_(use_top ? NULL : fp), sp_(sp),
87 advance_(use_top ? &StackFrameIterator::AdvanceWithHandler :
88 &StackFrameIterator::AdvanceWithoutHandler) {
89 if (use_top || fp != NULL) {
90 Reset();
91 }
92 JavaScriptFrame_.DisableHeapAccess();
93}
94
95#undef INITIALIZE_SINGLETON
96
97
98void StackFrameIterator::AdvanceWithHandler() {
99 ASSERT(!done());
100 // Compute the state of the calling frame before restoring
101 // callee-saved registers and unwinding handlers. This allows the
102 // frame code that computes the caller state to access the top
103 // handler and the value of any callee-saved register if needed.
104 StackFrame::State state;
105 StackFrame::Type type = frame_->GetCallerState(&state);
106
107 // Unwind handlers corresponding to the current frame.
108 StackHandlerIterator it(frame_, handler_);
109 while (!it.done()) it.Advance();
110 handler_ = it.handler();
111
112 // Advance to the calling frame.
113 frame_ = SingletonFor(type, &state);
114
115 // When we're done iterating over the stack frames, the handler
116 // chain must have been completely unwound.
117 ASSERT(!done() || handler_ == NULL);
118}
119
120
121void StackFrameIterator::AdvanceWithoutHandler() {
122 // A simpler version of Advance which doesn't care about handler.
123 ASSERT(!done());
124 StackFrame::State state;
125 StackFrame::Type type = frame_->GetCallerState(&state);
126 frame_ = SingletonFor(type, &state);
127}
128
129
130void StackFrameIterator::Reset() {
131 StackFrame::State state;
132 StackFrame::Type type;
133 if (thread_ != NULL) {
134 type = ExitFrame::GetStateForFramePointer(Top::c_entry_fp(thread_), &state);
135 handler_ = StackHandler::FromAddress(Top::handler(thread_));
136 } else {
137 ASSERT(fp_ != NULL);
138 state.fp = fp_;
139 state.sp = sp_;
140 state.pc_address =
141 reinterpret_cast<Address*>(StandardFrame::ComputePCAddress(fp_));
142 type = StackFrame::ComputeType(&state);
143 if (SingletonFor(type) == NULL) return;
144 }
145 frame_ = SingletonFor(type, &state);
146}
147
148
149StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type,
150 StackFrame::State* state) {
151 if (type == StackFrame::NONE) return NULL;
152 StackFrame* result = SingletonFor(type);
153 ASSERT(result != NULL);
154 result->state_ = *state;
155 return result;
156}
157
158
159StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type) {
160#define FRAME_TYPE_CASE(type, field) \
161 case StackFrame::type: result = &field##_; break;
162
163 StackFrame* result = NULL;
164 switch (type) {
165 case StackFrame::NONE: return NULL;
166 STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE)
167 default: break;
168 }
169 return result;
170
171#undef FRAME_TYPE_CASE
172}
173
174
175// -------------------------------------------------------------------------
176
177
178StackTraceFrameIterator::StackTraceFrameIterator() {
179 if (!done() && !frame()->function()->IsJSFunction()) Advance();
180}
181
182
183void StackTraceFrameIterator::Advance() {
184 while (true) {
185 JavaScriptFrameIterator::Advance();
186 if (done()) return;
187 if (frame()->function()->IsJSFunction()) return;
188 }
189}
190
191
192// -------------------------------------------------------------------------
193
194
195SafeStackFrameIterator::SafeStackFrameIterator(
196 Address fp, Address sp, Address low_bound, Address high_bound) :
197 low_bound_(low_bound), high_bound_(high_bound),
198 is_valid_top_(
199 IsWithinBounds(low_bound, high_bound,
200 Top::c_entry_fp(Top::GetCurrentThread())) &&
201 Top::handler(Top::GetCurrentThread()) != NULL),
202 is_valid_fp_(IsWithinBounds(low_bound, high_bound, fp)),
203 is_working_iterator_(is_valid_top_ || is_valid_fp_),
204 iteration_done_(!is_working_iterator_),
205 iterator_(is_valid_top_, is_valid_fp_ ? fp : NULL, sp) {
206}
207
208
209void SafeStackFrameIterator::Advance() {
210 ASSERT(is_working_iterator_);
211 ASSERT(!done());
212 StackFrame* last_frame = iterator_.frame();
213 Address last_sp = last_frame->sp(), last_fp = last_frame->fp();
214 // Before advancing to the next stack frame, perform pointer validity tests
215 iteration_done_ = !IsValidFrame(last_frame) ||
216 !CanIterateHandles(last_frame, iterator_.handler()) ||
217 !IsValidCaller(last_frame);
218 if (iteration_done_) return;
219
220 iterator_.Advance();
221 if (iterator_.done()) return;
222 // Check that we have actually moved to the previous frame in the stack
223 StackFrame* prev_frame = iterator_.frame();
224 iteration_done_ = prev_frame->sp() < last_sp || prev_frame->fp() < last_fp;
225}
226
227
228bool SafeStackFrameIterator::CanIterateHandles(StackFrame* frame,
229 StackHandler* handler) {
230 // If StackIterator iterates over StackHandles, verify that
231 // StackHandlerIterator can be instantiated (see StackHandlerIterator
232 // constructor.)
233 return !is_valid_top_ || (frame->sp() <= handler->address());
234}
235
236
237bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const {
238 return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp());
239}
240
241
242bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) {
243 StackFrame::State state;
244 if (frame->is_entry() || frame->is_entry_construct()) {
245 // See EntryFrame::GetCallerState. It computes the caller FP address
246 // and calls ExitFrame::GetStateForFramePointer on it. We need to be
247 // sure that caller FP address is valid.
248 Address caller_fp = Memory::Address_at(
249 frame->fp() + EntryFrameConstants::kCallerFPOffset);
250 if (!IsValidStackAddress(caller_fp)) {
251 return false;
252 }
253 } else if (frame->is_arguments_adaptor()) {
254 // See ArgumentsAdaptorFrame::GetCallerStackPointer. It assumes that
255 // the number of arguments is stored on stack as Smi. We need to check
256 // that it really an Smi.
257 Object* number_of_args = reinterpret_cast<ArgumentsAdaptorFrame*>(frame)->
258 GetExpression(0);
259 if (!number_of_args->IsSmi()) {
260 return false;
261 }
262 }
263 frame->ComputeCallerState(&state);
264 return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) &&
265 iterator_.SingletonFor(frame->GetCallerState(&state)) != NULL;
266}
267
268
269void SafeStackFrameIterator::Reset() {
270 if (is_working_iterator_) {
271 iterator_.Reset();
272 iteration_done_ = false;
273 }
274}
275
276
277// -------------------------------------------------------------------------
278
279
280#ifdef ENABLE_LOGGING_AND_PROFILING
281SafeStackTraceFrameIterator::SafeStackTraceFrameIterator(
282 Address fp, Address sp, Address low_bound, Address high_bound) :
283 SafeJavaScriptFrameIterator(fp, sp, low_bound, high_bound) {
284 if (!done() && !frame()->is_java_script()) Advance();
285}
286
287
288void SafeStackTraceFrameIterator::Advance() {
289 while (true) {
290 SafeJavaScriptFrameIterator::Advance();
291 if (done()) return;
292 if (frame()->is_java_script()) return;
293 }
294}
295#endif
296
297
298// -------------------------------------------------------------------------
299
300
301void StackHandler::Cook(Code* code) {
302 ASSERT(MarkCompactCollector::IsCompacting());
303 ASSERT(code->contains(pc()));
304 set_pc(AddressFrom<Address>(pc() - code->instruction_start()));
305}
306
307
308void StackHandler::Uncook(Code* code) {
309 ASSERT(MarkCompactCollector::IsCompacting());
310 set_pc(code->instruction_start() + OffsetFrom(pc()));
311 ASSERT(code->contains(pc()));
312}
313
314
315// -------------------------------------------------------------------------
316
317
318bool StackFrame::HasHandler() const {
319 StackHandlerIterator it(this, top_handler());
320 return !it.done();
321}
322
323
324void StackFrame::CookFramesForThread(ThreadLocalTop* thread) {
325 // Only cooking frames when the collector is compacting and thus moving code
326 // around.
327 ASSERT(MarkCompactCollector::IsCompacting());
328 ASSERT(!thread->stack_is_cooked());
329 for (StackFrameIterator it(thread); !it.done(); it.Advance()) {
330 it.frame()->Cook();
331 }
332 thread->set_stack_is_cooked(true);
333}
334
335
336void StackFrame::UncookFramesForThread(ThreadLocalTop* thread) {
337 // Only uncooking frames when the collector is compacting and thus moving code
338 // around.
339 ASSERT(MarkCompactCollector::IsCompacting());
340 ASSERT(thread->stack_is_cooked());
341 for (StackFrameIterator it(thread); !it.done(); it.Advance()) {
342 it.frame()->Uncook();
343 }
344 thread->set_stack_is_cooked(false);
345}
346
347
348void StackFrame::Cook() {
349 Code* code = this->code();
350 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
351 it.handler()->Cook(code);
352 }
353 ASSERT(code->contains(pc()));
354 set_pc(AddressFrom<Address>(pc() - code->instruction_start()));
355}
356
357
358void StackFrame::Uncook() {
359 Code* code = this->code();
360 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
361 it.handler()->Uncook(code);
362 }
363 set_pc(code->instruction_start() + OffsetFrom(pc()));
364 ASSERT(code->contains(pc()));
365}
366
367
368StackFrame::Type StackFrame::GetCallerState(State* state) const {
369 ComputeCallerState(state);
370 return ComputeType(state);
371}
372
373
374Code* EntryFrame::code() const {
375 return Heap::js_entry_code();
376}
377
378
379void EntryFrame::ComputeCallerState(State* state) const {
380 GetCallerState(state);
381}
382
383
384StackFrame::Type EntryFrame::GetCallerState(State* state) const {
385 const int offset = EntryFrameConstants::kCallerFPOffset;
386 Address fp = Memory::Address_at(this->fp() + offset);
387 return ExitFrame::GetStateForFramePointer(fp, state);
388}
389
390
391Code* EntryConstructFrame::code() const {
392 return Heap::js_construct_entry_code();
393}
394
395
Steve Blockd0582a62009-12-15 09:54:21 +0000396Object*& ExitFrame::code_slot() const {
397 const int offset = ExitFrameConstants::kCodeOffset;
398 return Memory::Object_at(fp() + offset);
399}
400
401
Steve Blocka7e24c12009-10-30 11:49:00 +0000402Code* ExitFrame::code() const {
Steve Blockd0582a62009-12-15 09:54:21 +0000403 Object* code = code_slot();
404 if (code->IsSmi()) {
405 return Heap::c_entry_debug_break_code();
406 } else {
407 return Code::cast(code);
408 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000409}
410
411
412void ExitFrame::ComputeCallerState(State* state) const {
413 // Setup the caller state.
414 state->sp = caller_sp();
415 state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset);
416 state->pc_address
417 = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset);
418}
419
420
421Address ExitFrame::GetCallerStackPointer() const {
422 return fp() + ExitFrameConstants::kCallerSPDisplacement;
423}
424
425
Steve Blocka7e24c12009-10-30 11:49:00 +0000426Address StandardFrame::GetExpressionAddress(int n) const {
427 const int offset = StandardFrameConstants::kExpressionsOffset;
428 return fp() + offset - n * kPointerSize;
429}
430
431
432int StandardFrame::ComputeExpressionsCount() const {
433 const int offset =
434 StandardFrameConstants::kExpressionsOffset + kPointerSize;
435 Address base = fp() + offset;
436 Address limit = sp();
437 ASSERT(base >= limit); // stack grows downwards
438 // Include register-allocated locals in number of expressions.
Steve Blockd0582a62009-12-15 09:54:21 +0000439 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000440}
441
442
443void StandardFrame::ComputeCallerState(State* state) const {
444 state->sp = caller_sp();
445 state->fp = caller_fp();
446 state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp()));
447}
448
449
450bool StandardFrame::IsExpressionInsideHandler(int n) const {
451 Address address = GetExpressionAddress(n);
452 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
453 if (it.handler()->includes(address)) return true;
454 }
455 return false;
456}
457
458
459Object* JavaScriptFrame::GetParameter(int index) const {
460 ASSERT(index >= 0 && index < ComputeParametersCount());
461 const int offset = JavaScriptFrameConstants::kParam0Offset;
462 return Memory::Object_at(caller_sp() + offset - (index * kPointerSize));
463}
464
465
466int JavaScriptFrame::ComputeParametersCount() const {
467 Address base = caller_sp() + JavaScriptFrameConstants::kReceiverOffset;
468 Address limit = fp() + JavaScriptFrameConstants::kSavedRegistersOffset;
Steve Blockd0582a62009-12-15 09:54:21 +0000469 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470}
471
472
473bool JavaScriptFrame::IsConstructor() const {
474 Address fp = caller_fp();
475 if (has_adapted_arguments()) {
476 // Skip the arguments adaptor frame and look at the real caller.
477 fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset);
478 }
479 return IsConstructFrame(fp);
480}
481
482
483Code* JavaScriptFrame::code() const {
484 JSFunction* function = JSFunction::cast(this->function());
485 return function->shared()->code();
486}
487
488
489Code* ArgumentsAdaptorFrame::code() const {
490 return Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline);
491}
492
493
494Code* InternalFrame::code() const {
495 const int offset = InternalFrameConstants::kCodeOffset;
496 Object* code = Memory::Object_at(fp() + offset);
497 ASSERT(code != NULL);
498 return Code::cast(code);
499}
500
501
502void StackFrame::PrintIndex(StringStream* accumulator,
503 PrintMode mode,
504 int index) {
505 accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index);
506}
507
508
509void JavaScriptFrame::Print(StringStream* accumulator,
510 PrintMode mode,
511 int index) const {
512 HandleScope scope;
513 Object* receiver = this->receiver();
514 Object* function = this->function();
515
516 accumulator->PrintSecurityTokenIfChanged(function);
517 PrintIndex(accumulator, mode, index);
518 Code* code = NULL;
519 if (IsConstructor()) accumulator->Add("new ");
520 accumulator->PrintFunction(function, receiver, &code);
521 accumulator->Add("(this=%o", receiver);
522
523 // Get scope information for nicer output, if possible. If code is
524 // NULL, or doesn't contain scope info, info will return 0 for the
525 // number of parameters, stack slots, or context slots.
526 ScopeInfo<PreallocatedStorage> info(code);
527
528 // Print the parameters.
529 int parameters_count = ComputeParametersCount();
530 for (int i = 0; i < parameters_count; i++) {
531 accumulator->Add(",");
532 // If we have a name for the parameter we print it. Nameless
533 // parameters are either because we have more actual parameters
534 // than formal parameters or because we have no scope information.
535 if (i < info.number_of_parameters()) {
536 accumulator->PrintName(*info.parameter_name(i));
537 accumulator->Add("=");
538 }
539 accumulator->Add("%o", GetParameter(i));
540 }
541
542 accumulator->Add(")");
543 if (mode == OVERVIEW) {
544 accumulator->Add("\n");
545 return;
546 }
547 accumulator->Add(" {\n");
548
549 // Compute the number of locals and expression stack elements.
550 int stack_locals_count = info.number_of_stack_slots();
551 int heap_locals_count = info.number_of_context_slots();
552 int expressions_count = ComputeExpressionsCount();
553
554 // Print stack-allocated local variables.
555 if (stack_locals_count > 0) {
556 accumulator->Add(" // stack-allocated locals\n");
557 }
558 for (int i = 0; i < stack_locals_count; i++) {
559 accumulator->Add(" var ");
560 accumulator->PrintName(*info.stack_slot_name(i));
561 accumulator->Add(" = ");
562 if (i < expressions_count) {
563 accumulator->Add("%o", GetExpression(i));
564 } else {
565 accumulator->Add("// no expression found - inconsistent frame?");
566 }
567 accumulator->Add("\n");
568 }
569
570 // Try to get hold of the context of this frame.
571 Context* context = NULL;
572 if (this->context() != NULL && this->context()->IsContext()) {
573 context = Context::cast(this->context());
574 }
575
576 // Print heap-allocated local variables.
577 if (heap_locals_count > Context::MIN_CONTEXT_SLOTS) {
578 accumulator->Add(" // heap-allocated locals\n");
579 }
580 for (int i = Context::MIN_CONTEXT_SLOTS; i < heap_locals_count; i++) {
581 accumulator->Add(" var ");
582 accumulator->PrintName(*info.context_slot_name(i));
583 accumulator->Add(" = ");
584 if (context != NULL) {
585 if (i < context->length()) {
586 accumulator->Add("%o", context->get(i));
587 } else {
588 accumulator->Add(
589 "// warning: missing context slot - inconsistent frame?");
590 }
591 } else {
592 accumulator->Add("// warning: no context found - inconsistent frame?");
593 }
594 accumulator->Add("\n");
595 }
596
597 // Print the expression stack.
598 int expressions_start = stack_locals_count;
599 if (expressions_start < expressions_count) {
600 accumulator->Add(" // expression stack (top to bottom)\n");
601 }
602 for (int i = expressions_count - 1; i >= expressions_start; i--) {
603 if (IsExpressionInsideHandler(i)) continue;
604 accumulator->Add(" [%02d] : %o\n", i, GetExpression(i));
605 }
606
607 // Print details about the function.
608 if (FLAG_max_stack_trace_source_length != 0 && code != NULL) {
609 SharedFunctionInfo* shared = JSFunction::cast(function)->shared();
610 accumulator->Add("--------- s o u r c e c o d e ---------\n");
611 shared->SourceCodePrint(accumulator, FLAG_max_stack_trace_source_length);
612 accumulator->Add("\n-----------------------------------------\n");
613 }
614
615 accumulator->Add("}\n\n");
616}
617
618
619void ArgumentsAdaptorFrame::Print(StringStream* accumulator,
620 PrintMode mode,
621 int index) const {
622 int actual = ComputeParametersCount();
623 int expected = -1;
624 Object* function = this->function();
625 if (function->IsJSFunction()) {
626 expected = JSFunction::cast(function)->shared()->formal_parameter_count();
627 }
628
629 PrintIndex(accumulator, mode, index);
630 accumulator->Add("arguments adaptor frame: %d->%d", actual, expected);
631 if (mode == OVERVIEW) {
632 accumulator->Add("\n");
633 return;
634 }
635 accumulator->Add(" {\n");
636
637 // Print actual arguments.
638 if (actual > 0) accumulator->Add(" // actual arguments\n");
639 for (int i = 0; i < actual; i++) {
640 accumulator->Add(" [%02d] : %o", i, GetParameter(i));
641 if (expected != -1 && i >= expected) {
642 accumulator->Add(" // not passed to callee");
643 }
644 accumulator->Add("\n");
645 }
646
647 accumulator->Add("}\n\n");
648}
649
650
651void EntryFrame::Iterate(ObjectVisitor* v) const {
652 StackHandlerIterator it(this, top_handler());
653 ASSERT(!it.done());
654 StackHandler* handler = it.handler();
655 ASSERT(handler->is_entry());
656 handler->Iterate(v);
657 // Make sure that there's the entry frame does not contain more than
658 // one stack handler.
659#ifdef DEBUG
660 it.Advance();
661 ASSERT(it.done());
662#endif
663}
664
665
666void StandardFrame::IterateExpressions(ObjectVisitor* v) const {
667 const int offset = StandardFrameConstants::kContextOffset;
668 Object** base = &Memory::Object_at(sp());
669 Object** limit = &Memory::Object_at(fp() + offset) + 1;
670 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
671 StackHandler* handler = it.handler();
672 // Traverse pointers down to - but not including - the next
673 // handler in the handler chain. Update the base to skip the
674 // handler and allow the handler to traverse its own pointers.
675 const Address address = handler->address();
676 v->VisitPointers(base, reinterpret_cast<Object**>(address));
677 base = reinterpret_cast<Object**>(address + StackHandlerConstants::kSize);
678 // Traverse the pointers in the handler itself.
679 handler->Iterate(v);
680 }
681 v->VisitPointers(base, limit);
682}
683
684
685void JavaScriptFrame::Iterate(ObjectVisitor* v) const {
686 IterateExpressions(v);
687
688 // Traverse callee-saved registers, receiver, and parameters.
689 const int kBaseOffset = JavaScriptFrameConstants::kSavedRegistersOffset;
690 const int kLimitOffset = JavaScriptFrameConstants::kReceiverOffset;
691 Object** base = &Memory::Object_at(fp() + kBaseOffset);
692 Object** limit = &Memory::Object_at(caller_sp() + kLimitOffset) + 1;
693 v->VisitPointers(base, limit);
694}
695
696
697void InternalFrame::Iterate(ObjectVisitor* v) const {
698 // Internal frames only have object pointers on the expression stack
699 // as they never have any arguments.
700 IterateExpressions(v);
701}
702
703
704// -------------------------------------------------------------------------
705
706
707JavaScriptFrame* StackFrameLocator::FindJavaScriptFrame(int n) {
708 ASSERT(n >= 0);
709 for (int i = 0; i <= n; i++) {
710 while (!iterator_.frame()->is_java_script()) iterator_.Advance();
711 if (i == n) return JavaScriptFrame::cast(iterator_.frame());
712 iterator_.Advance();
713 }
714 UNREACHABLE();
715 return NULL;
716}
717
718
719// -------------------------------------------------------------------------
720
721
722int NumRegs(RegList reglist) {
723 int n = 0;
724 while (reglist != 0) {
725 n++;
726 reglist &= reglist - 1; // clear one bit
727 }
728 return n;
729}
730
731
732int JSCallerSavedCode(int n) {
733 static int reg_code[kNumJSCallerSaved];
734 static bool initialized = false;
735 if (!initialized) {
736 initialized = true;
737 int i = 0;
738 for (int r = 0; r < kNumRegs; r++)
739 if ((kJSCallerSaved & (1 << r)) != 0)
740 reg_code[i++] = r;
741
742 ASSERT(i == kNumJSCallerSaved);
743 }
744 ASSERT(0 <= n && n < kNumJSCallerSaved);
745 return reg_code[n];
746}
747
748
749} } // namespace v8::internal