]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - interpreter/CallFrame.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / interpreter / CallFrame.cpp
index a5ffaee8d61499f4d696575623ac8d3d3093a48c..259061df49d1a1ef1921f676fe3c252ddf7e83f7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2008, 2013, 2014 Apple Inc. All Rights Reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #include "config.h"
 #include "CallFrame.h"
 
+#include "CallFrameInlines.h"
 #include "CodeBlock.h"
 #include "Interpreter.h"
+#include "JSLexicalEnvironment.h"
+#include "JSCInlines.h"
+#include "VMEntryScope.h"
+#include <wtf/StringPrintStream.h>
 
 namespace JSC {
 
 #ifndef NDEBUG
-void CallFrame::dumpCaller()
+JSStack* CallFrame::stack()
 {
-    int signedLineNumber;
-    intptr_t sourceID;
-    UString urlString;
-    JSValue function;
-    
-    interpreter()->retrieveLastCaller(this, signedLineNumber, sourceID, urlString, function);
-    dataLog("Callpoint => %s:%d\n", urlString.utf8().data(), signedLineNumber);
-}
-
-RegisterFile* CallFrame::registerFile()
-{
-    return &interpreter()->registerFile();
+    return &interpreter()->stack();
 }
 
 #endif
 
 #if USE(JSVALUE32_64)
-unsigned CallFrame::bytecodeOffsetForNonDFGCode() const
+unsigned CallFrame::locationAsBytecodeOffset() const
 {
     ASSERT(codeBlock());
+    ASSERT(hasLocationAsBytecodeOffset());
     return currentVPC() - codeBlock()->instructions().begin();
 }
 
-void CallFrame::setBytecodeOffsetForNonDFGCode(unsigned offset)
+void CallFrame::setLocationAsBytecodeOffset(unsigned offset)
 {
     ASSERT(codeBlock());
     setCurrentVPC(codeBlock()->instructions().begin() + offset);
+    ASSERT(hasLocationAsBytecodeOffset());
 }
 #else
 Instruction* CallFrame::currentVPC() const
 {
-    return codeBlock()->instructions().begin() + bytecodeOffsetForNonDFGCode();
+    return codeBlock()->instructions().begin() + locationAsBytecodeOffset();
 }
 void CallFrame::setCurrentVPC(Instruction* vpc)
 {
-    setBytecodeOffsetForNonDFGCode(vpc - codeBlock()->instructions().begin());
+    setLocationAsBytecodeOffset(vpc - codeBlock()->instructions().begin());
 }
 #endif
     
 #if ENABLE(DFG_JIT)
-bool CallFrame::isInlineCallFrameSlow()
+unsigned CallFrame::bytecodeOffsetFromCodeOriginIndex()
 {
-    if (!callee())
-        return false;
-    JSCell* calleeAsFunctionCell = getJSFunction(callee());
-    if (!calleeAsFunctionCell)
-        return false;
-    JSFunction* calleeAsFunction = jsCast<JSFunction*>(calleeAsFunctionCell);
-    return calleeAsFunction->executable() != codeBlock()->ownerExecutable();
-}
+    ASSERT(hasLocationAsCodeOriginIndex());
+    CodeBlock* codeBlock = this->codeBlock();
+    ASSERT(codeBlock);
 
-CallFrame* CallFrame::trueCallFrame(AbstractPC pc)
-{
-    // Am I an inline call frame? If so, we're done.
-    if (isInlineCallFrame())
-        return this;
-    
-    // If I don't have a code block, then I'm not DFG code, so I'm the true call frame.
-    CodeBlock* machineCodeBlock = codeBlock();
-    if (!machineCodeBlock)
-        return this;
-    
-    // If the code block does not have any code origins, then there was no inlining, so
-    // I'm done.
-    if (!machineCodeBlock->hasCodeOrigins())
-        return this;
-    
-    // At this point the PC must be due either to the DFG, or it must be unset.
-    ASSERT(pc.hasJITReturnAddress() || !pc);
-    
-    // Try to determine the CodeOrigin. If we don't have a pc set then the only way
-    // that this makes sense is if the CodeOrigin index was set in the call frame.
-    // FIXME: Note that you will see "Not currently in inlined code" comments below.
-    // Currently, we do not record code origins for code that is not inlined, because
-    // the only thing that we use code origins for is determining the inline stack.
-    // But in the future, we'll want to use this same functionality (having a code
-    // origin mapping for any calls out of JIT code) to determine the PC at any point
-    // in the stack even if not in inlined code. When that happens, the code below
-    // will have to change the way it detects the presence of inlining: it will always
-    // get a code origin, but sometimes, that code origin will not have an inline call
-    // frame. In that case, this method should bail and return this.
     CodeOrigin codeOrigin;
-    if (pc.isSet()) {
-        ReturnAddressPtr currentReturnPC = pc.jitReturnAddress();
-        
-        bool hasCodeOrigin = machineCodeBlock->codeOriginForReturn(currentReturnPC, codeOrigin);
-        ASSERT_UNUSED(hasCodeOrigin, hasCodeOrigin);
-    } else {
-        unsigned index = codeOriginIndexForDFG();
-        codeOrigin = machineCodeBlock->codeOrigin(index);
-    }
+    unsigned index = locationAsCodeOriginIndex();
+    ASSERT(codeBlock->canGetCodeOrigin(index));
+    codeOrigin = codeBlock->codeOrigin(index);
 
-    if (!codeOrigin.inlineCallFrame)
-        return this; // Not currently in inlined code.
-    
     for (InlineCallFrame* inlineCallFrame = codeOrigin.inlineCallFrame; inlineCallFrame;) {
-        InlineCallFrame* nextInlineCallFrame = inlineCallFrame->caller.inlineCallFrame;
-        
-        CallFrame* inlinedCaller = this + inlineCallFrame->stackOffset;
-        
-        JSFunction* calleeAsFunction = inlineCallFrame->callee.get();
-        
-        // Fill in the inlinedCaller
-        inlinedCaller->setCodeBlock(machineCodeBlock);
-        
-        inlinedCaller->setScopeChain(calleeAsFunction->scope());
-        if (nextInlineCallFrame)
-            inlinedCaller->setCallerFrame(this + nextInlineCallFrame->stackOffset);
-        else
-            inlinedCaller->setCallerFrame(this);
-        
-        inlinedCaller->setInlineCallFrame(inlineCallFrame);
-        inlinedCaller->setArgumentCountIncludingThis(inlineCallFrame->arguments.size());
-        inlinedCaller->setCallee(calleeAsFunction);
-        
-        inlineCallFrame = nextInlineCallFrame;
+        if (inlineCallFrame->baselineCodeBlock() == codeBlock)
+            return codeOrigin.bytecodeIndex;
+
+        codeOrigin = inlineCallFrame->caller;
+        inlineCallFrame = codeOrigin.inlineCallFrame;
     }
-    
-    return this + codeOrigin.inlineCallFrame->stackOffset;
+    return codeOrigin.bytecodeIndex;
 }
-        
-CallFrame* CallFrame::trueCallerFrame()
-{
-    // this -> The callee; this is either an inlined callee in which case it already has
-    //    a pointer to the true caller. Otherwise it contains current PC in the machine
-    //    caller.
-    //
-    // machineCaller -> The caller according to the machine, which may be zero or
-    //    more frames above the true caller due to inlining.
 
-    // Am I an inline call frame? If so, we're done.
-    if (isInlineCallFrame())
-        return callerFrame()->removeHostCallFrameFlag();
-    
-    // I am a machine call frame, so the question is: is my caller a machine call frame
-    // that has inlines or a machine call frame that doesn't?
-    CallFrame* machineCaller = callerFrame()->removeHostCallFrameFlag();
-    if (!machineCaller)
+#endif // ENABLE(DFG_JIT)
+
+unsigned CallFrame::bytecodeOffset()
+{
+    if (!codeBlock())
         return 0;
-    ASSERT(!machineCaller->isInlineCallFrame());
-    
-    // Figure out how we want to get the current code location.
-    if (!hasReturnPC() || returnAddressIsInCtiTrampoline(returnPC()))
-        return machineCaller->trueCallFrameFromVMCode()->removeHostCallFrameFlag();
-    
-    return machineCaller->trueCallFrame(returnPC())->removeHostCallFrameFlag();
+#if ENABLE(DFG_JIT)
+    if (hasLocationAsCodeOriginIndex())
+        return bytecodeOffsetFromCodeOriginIndex();
+#endif
+    return locationAsBytecodeOffset();
 }
+
+CodeOrigin CallFrame::codeOrigin()
+{
+    if (!codeBlock())
+        return CodeOrigin(0);
+#if ENABLE(DFG_JIT)
+    if (hasLocationAsCodeOriginIndex()) {
+        unsigned index = locationAsCodeOriginIndex();
+        ASSERT(codeBlock()->canGetCodeOrigin(index));
+        return codeBlock()->codeOrigin(index);
+    }
 #endif
+    return CodeOrigin(locationAsBytecodeOffset());
+}
 
-Register* CallFrame::frameExtentInternal()
+Register* CallFrame::topOfFrameInternal()
 {
     CodeBlock* codeBlock = this->codeBlock();
     ASSERT(codeBlock);
-    return registers() + codeBlock->m_numCalleeRegisters;
+    return registers() + codeBlock->stackPointerOffset();
+}
+
+JSGlobalObject* CallFrame::vmEntryGlobalObject()
+{
+    if (this == lexicalGlobalObject()->globalExec())
+        return lexicalGlobalObject();
+
+    // For any ExecState that's not a globalExec, the 
+    // dynamic global object must be set since code is running
+    ASSERT(vm().entryScope);
+    return vm().entryScope->globalObject();
+}
+
+CallFrame* CallFrame::callerFrame(VMEntryFrame*& currVMEntryFrame)
+{
+    if (callerFrameOrVMEntryFrame() == currVMEntryFrame) {
+        VMEntryRecord* currVMEntryRecord = vmEntryRecord(currVMEntryFrame);
+        currVMEntryFrame = currVMEntryRecord->prevTopVMEntryFrame();
+        return currVMEntryRecord->prevTopCallFrame();
+    }
+    return static_cast<CallFrame*>(callerFrameOrVMEntryFrame());
+}
+
+JSLexicalEnvironment* CallFrame::lexicalEnvironment() const
+{
+    CodeBlock* codeBlock = this->codeBlock();
+    RELEASE_ASSERT(codeBlock->needsActivation());
+    VirtualRegister activationRegister = codeBlock->activationRegister();
+    return registers()[activationRegister.offset()].Register::lexicalEnvironment();
+}
+
+JSLexicalEnvironment* CallFrame::lexicalEnvironmentOrNullptr() const
+{
+    CodeBlock* codeBlock = this->codeBlock();
+    return codeBlock->needsActivation() ? lexicalEnvironment() : nullptr;
+}
+    
+void CallFrame::setActivation(JSLexicalEnvironment* lexicalEnvironment)
+{
+    CodeBlock* codeBlock = this->codeBlock();
+    RELEASE_ASSERT(codeBlock->needsActivation());
+    VirtualRegister activationRegister = codeBlock->activationRegister();
+    registers()[activationRegister.offset()] = lexicalEnvironment;
+}
+
+void CallFrame::dump(PrintStream& out)
+{
+    if (CodeBlock* codeBlock = this->codeBlock()) {
+        out.print(codeBlock->inferredName(), "#", codeBlock->hashAsStringIfPossible(), " [", codeBlock->jitType(), "]");
+
+        out.print("(");
+        thisValue().dumpForBacktrace(out);
+
+        for (size_t i = 0; i < argumentCount(); ++i) {
+            out.print(", ");
+            JSValue value = argument(i);
+            value.dumpForBacktrace(out);
+        }
+
+        out.print(")");
+
+        return;
+    }
+
+    out.print(returnPC());
 }
 
+const char* CallFrame::describeFrame()
+{
+    const size_t bufferSize = 200;
+    static char buffer[bufferSize + 1];
+    
+    WTF::StringPrintStream stringStream;
+
+    dump(stringStream);
+
+    strncpy(buffer, stringStream.toCString().data(), bufferSize);
+    buffer[bufferSize] = '\0';
+    
+    return buffer;
 }
+
+} // namespace JSC