1/*
2 * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "CodeOrigin.h"
28
29#include "CallFrame.h"
30#include "CodeBlock.h"
31#include "Executable.h"
32#include "Operations.h"
33
34namespace JSC {
35
36unsigned CodeOrigin::inlineDepthForCallFrame(InlineCallFrame* inlineCallFrame)
37{
38    unsigned result = 1;
39    for (InlineCallFrame* current = inlineCallFrame; current; current = current->caller.inlineCallFrame)
40        result++;
41    return result;
42}
43
44unsigned CodeOrigin::inlineDepth() const
45{
46    return inlineDepthForCallFrame(inlineCallFrame);
47}
48
49Vector<CodeOrigin> CodeOrigin::inlineStack() const
50{
51    Vector<CodeOrigin> result(inlineDepth());
52    result.last() = *this;
53    unsigned index = result.size() - 2;
54    for (InlineCallFrame* current = inlineCallFrame; current; current = current->caller.inlineCallFrame)
55        result[index--] = current->caller;
56    RELEASE_ASSERT(!result[0].inlineCallFrame);
57    return result;
58}
59
60void CodeOrigin::dump(PrintStream& out) const
61{
62    Vector<CodeOrigin> stack = inlineStack();
63    for (unsigned i = 0; i < stack.size(); ++i) {
64        if (i)
65            out.print(" --> ");
66
67        if (InlineCallFrame* frame = stack[i].inlineCallFrame) {
68            out.print(frame->briefFunctionInformation(), ":<", RawPointer(frame->executable.get()), "> ");
69            if (frame->isClosureCall())
70                out.print("(closure) ");
71        }
72
73        out.print("bc#", stack[i].bytecodeIndex);
74    }
75}
76
77JSFunction* InlineCallFrame::calleeForCallFrame(ExecState* exec) const
78{
79    if (!isClosureCall())
80        return callee.get();
81
82    return jsCast<JSFunction*>((exec + stackOffset)->callee());
83}
84
85CodeBlockHash InlineCallFrame::hash() const
86{
87    return executable->hashFor(specializationKind());
88}
89
90String InlineCallFrame::inferredName() const
91{
92    return jsCast<FunctionExecutable*>(executable.get())->inferredName().string();
93}
94
95CodeBlock* InlineCallFrame::baselineCodeBlock() const
96{
97    return jsCast<FunctionExecutable*>(executable.get())->baselineCodeBlockFor(specializationKind());
98}
99
100void InlineCallFrame::dumpBriefFunctionInformation(PrintStream& out) const
101{
102    out.print(inferredName(), "#", hash());
103}
104
105void InlineCallFrame::dump(PrintStream& out) const
106{
107    out.print(briefFunctionInformation(), ":<", RawPointer(executable.get()), ", bc#", caller.bytecodeIndex, ", ", specializationKind());
108    if (callee)
109        out.print(", known callee: ", JSValue(callee.get()));
110    else
111        out.print(", closure call");
112    out.print(", numArgs+this = ", arguments.size());
113    out.print(", stack >= r", stackOffset);
114    out.print(">");
115}
116
117} // namespace JSC
118
119