Changeset 34319 in webkit for trunk/JavaScriptCore/VM


Ignore:
Timestamp:
Jun 2, 2008, 1:45:13 PM (17 years ago)
Author:
[email protected]
Message:

2008-06-02 Geoffrey Garen <[email protected]>

Reviewed by Darin Adler.


A little cleanup in the CodeGenerator.

  • VM/CodeGenerator.cpp: A few changes here.

(1) Removed remaining cases of the old hack of putting "this" into the
symbol table; replaced with explicit tracking of m_thisRegister.

(2) Made m_thisRegister behave the same for function, eval, and program
code, removing the static programCodeThis() function.

(3) Added a feature to nix a ScopeNode's declaration stacks when done
compiling, to save memory.

(4) Removed code that copied eval declarations into special vectors: we
just use the originals in the ScopeNode now.


  • VM/CodeGenerator.h: Removed unneded parameters from the CodeGenerator constructor: we just use get that data from the ScopeNode now.
  • VM/Machine.cpp: (KJS::Machine::execute): When executing an eval node, don't iterate a special copy of its declarations; iterate the originals, instead.
  • kjs/nodes.cpp: Moved responsibility for knowing what AST data to throw away into the CodeGenerator. Nodes no longer call shrinkCapacity on their data directly.


  • kjs/nodes.h: Changed FunctionStack to ref its contents, so declaration data stays around even after we've thrown away the AST, unless we explicitly throw away the declaration data, too. This is useful for eval code, which needs to reference its declaration data at execution time. (Soon, it will be useful for program code, too, since program code should do the same.)
Location:
trunk/JavaScriptCore/VM
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/VM/CodeBlock.h

    r33979 r34319  
    123123        {
    124124        }
    125 
    126         Vector<Identifier> declaredVariableNames;
    127         Vector<Identifier> declaredFunctionNames;
    128125    };
    129126
  • trunk/JavaScriptCore/VM/CodeGenerator.cpp

    r34303 r34319  
    130130{
    131131    m_codeBlock->numLocals = m_codeBlock->numVars + m_codeBlock->numParameters;
    132     m_codeBlock->thisRegister = m_codeType == FunctionCode ? -m_codeBlock->numLocals : Machine::ProgramCodeThisRegister;
     132    m_codeBlock->thisRegister = m_thisRegister.index();
    133133    if (m_shouldEmitDebugHooks)
    134134        m_codeBlock->needsFullScopeChain = true;
     
    143143#endif
    144144
    145     // Remove "this" from symbol table so it does not appear as a global object property at runtime.
    146     symbolTable().remove(m_propertyNames->thisIdentifier.ustring().rep());
     145    m_scopeNode->children().shrinkCapacity(0);
     146    if (m_codeType != EvalCode) { // eval code needs to hang on to its declaration stacks to keep declaration info alive until Machine::execute time.
     147        m_scopeNode->varStack().shrinkCapacity(0);
     148        m_scopeNode->functionStack().shrinkCapacity(0);
     149    }
    147150}
    148151
     
    164167    r0 = &m_locals[localsIndex(index)];
    165168    return result.second;
    166 }
    167 
    168 RegisterID* CodeGenerator::programCodeThis()
    169 {
    170     static RegisterID programThis(Machine::ProgramCodeThisRegister);
    171     return &programThis;
    172169}
    173170
     
    178175    , m_scopeNode(programNode)
    179176    , m_codeBlock(codeBlock)
     177    , m_thisRegister(Machine::ProgramCodeThisRegister)
    180178    , m_finallyDepth(0)
    181179    , m_dynamicScopeDepth(0)
     
    184182    , m_nextVar(-1)
    185183    , m_propertyNames(CommonIdentifiers::shared())
    186 
    187184{
    188185    // Global code can inherit previously defined symbols.
    189186    int size = symbolTable->size() + 1; // + 1 slot for  "this"
    190     m_thisRegister = programCodeThis();
    191187
    192188    // Add previously defined symbols to bookkeeping.
     
    202198   
    203199    ExecState* exec = globalObject->globalExec();
     200   
     201    // FIXME: Move the execution-related parts of this code to Machine::execute.
    204202
    205203    if (canCreateVariables) {
    206204        for (size_t i = 0; i < functionStack.size(); ++i) {
    207             FuncDeclNode* funcDecl = functionStack[i];
     205            FuncDeclNode* funcDecl = functionStack[i].get();
    208206            globalObject->removeDirect(funcDecl->m_ident); // Make sure our new function is not shadowed by an old property.
    209207            emitNewFunction(addVar(funcDecl->m_ident, false), funcDecl);
     
    216214    } else {
    217215        for (size_t i = 0; i < functionStack.size(); ++i) {
    218             FuncDeclNode* funcDecl = functionStack[i];
     216            FuncDeclNode* funcDecl = functionStack[i].get();
    219217            globalObject->putWithAttributes(exec, funcDecl->m_ident, funcDecl->makeFunction(exec, scopeChain.node()), DontDelete);
    220218        }
     
    230228}
    231229
    232 CodeGenerator::CodeGenerator(FunctionBodyNode* functionBody, const Debugger* debugger, const ScopeChain& scopeChain, SymbolTable* symbolTable, CodeBlock* codeBlock, VarStack& varStack, FunctionStack& functionStack, Vector<Identifier>& parameters)
     230CodeGenerator::CodeGenerator(FunctionBodyNode* functionBody, const Debugger* debugger, const ScopeChain& scopeChain, SymbolTable* symbolTable, CodeBlock* codeBlock)
    233231    : m_shouldEmitDebugHooks(!!debugger)
    234232    , m_scopeChain(&scopeChain)
     
    236234    , m_scopeNode(functionBody)
    237235    , m_codeBlock(codeBlock)
    238     , m_thisRegister(0)
    239236    , m_finallyDepth(0)
    240237    , m_dynamicScopeDepth(0)
     
    244241    , m_propertyNames(CommonIdentifiers::shared())
    245242{
     243    const Node::FunctionStack& functionStack = functionBody->functionStack();
    246244    for (size_t i = 0; i < functionStack.size(); ++i) {
    247         FuncDeclNode* funcDecl = functionStack[i];
     245        FuncDeclNode* funcDecl = functionStack[i].get();
    248246        const Identifier& ident = funcDecl->m_ident;
    249247       
     
    252250    }
    253251
     252    const Node::VarStack& varStack = functionBody->varStack();
    254253    for (size_t i = 0; i < varStack.size(); ++i) {
    255254        const Identifier& ident = varStack[i].first;
     
    262261    }
    263262
    264     m_nextParameter = m_nextVar - parameters.size();
    265     m_locals.resize(localsIndex(m_nextParameter) + 1);
    266 
    267     m_thisRegister = addParameter(m_propertyNames->thisIdentifier);
    268     for (size_t i = 0; i < parameters.size(); ++i) {
     263    Vector<Identifier>& parameters = functionBody->parameters();
     264    m_nextParameter = m_nextVar - parameters.size(); // parameters are allocated prior to vars
     265    m_locals.resize(localsIndex(m_nextParameter) + 1); // localsIndex of 0 => m_locals size of 1
     266
     267    // Add "this" as a parameter
     268    m_thisRegister.setIndex(m_nextParameter);
     269    ++m_nextParameter;
     270    ++m_codeBlock->numParameters;
     271   
     272    for (size_t i = 0; i < parameters.size(); ++i)
    269273        addParameter(parameters[i]);
    270     }
    271 }
    272 
    273 CodeGenerator::CodeGenerator(EvalNode* evalNode, const Debugger* debugger, const ScopeChain& scopeChain, SymbolTable* symbolTable, EvalCodeBlock* codeBlock, VarStack& varStack, FunctionStack& functionStack)
     274}
     275
     276CodeGenerator::CodeGenerator(EvalNode* evalNode, const Debugger* debugger, const ScopeChain& scopeChain, SymbolTable* symbolTable, EvalCodeBlock* codeBlock)
    274277    : m_shouldEmitDebugHooks(!!debugger)
    275278    , m_scopeChain(&scopeChain)
     
    277280    , m_scopeNode(evalNode)
    278281    , m_codeBlock(codeBlock)
    279     , m_thisRegister(0)
     282    , m_thisRegister(Machine::ProgramCodeThisRegister)
    280283    , m_finallyDepth(0)
    281284    , m_dynamicScopeDepth(0)
     
    285288    , m_propertyNames(CommonIdentifiers::shared())
    286289{
    287     addVar(m_propertyNames->thisIdentifier, m_thisRegister, false);
    288    
    289     for (size_t i = 0; i < varStack.size(); ++i)
    290         codeBlock->declaredVariableNames.append(varStack[i].first);
    291    
    292     for (size_t i = 0; i < functionStack.size(); ++i) {
    293         FuncDeclNode* funcDecl = functionStack[i];
    294         codeBlock->declaredFunctionNames.append(funcDecl->m_ident);
    295         addConstant(funcDecl);
    296     }
     290    m_codeBlock->numVars = 1; // Allocate space for "this"
    297291}
    298292
     
    325319        m_codeBlock->needsFullScopeChain = true;
    326320
    327     if (!shouldOptimizeLocals() && ident != m_propertyNames->thisIdentifier)
     321    if (ident == m_propertyNames->thisIdentifier)
     322        return &m_thisRegister;
     323
     324    if (!shouldOptimizeLocals())
    328325        return 0;
    329326
  • trunk/JavaScriptCore/VM/CodeGenerator.h

    r34250 r34319  
    7878       
    7979        CodeGenerator(ProgramNode*, const Debugger*, const ScopeChain&, SymbolTable*, CodeBlock*, VarStack&, FunctionStack&, bool canCreateGlobals);
    80         CodeGenerator(FunctionBodyNode*, const Debugger*, const ScopeChain&, SymbolTable*, CodeBlock*, VarStack&, FunctionStack&, Vector<Identifier>& parameters);
    81         CodeGenerator(EvalNode*, const Debugger*, const ScopeChain&, SymbolTable*, EvalCodeBlock*, VarStack&, FunctionStack& functionStack);
     80        CodeGenerator(FunctionBodyNode*, const Debugger*, const ScopeChain&, SymbolTable*, CodeBlock*);
     81        CodeGenerator(EvalNode*, const Debugger*, const ScopeChain&, SymbolTable*, EvalCodeBlock*);
    8282
    8383        ~CodeGenerator();
     
    107107
    108108        // Returns the register storing "this"
    109         RegisterID* thisRegister() { return m_thisRegister; }
     109        RegisterID* thisRegister() { return &m_thisRegister; }
    110110
    111111        bool isLocalConstant(const Identifier&);
     
    352352       
    353353        HashSet<RefPtr<UString::Rep>, IdentifierRepHash> m_functions;
    354         static RegisterID* programCodeThis();
    355         RegisterID* m_thisRegister;
     354        RegisterID m_thisRegister;
    356355        SegmentedVector<RegisterID, 512> m_locals;
    357356        SegmentedVector<RegisterID, 512> m_temporaries;
  • trunk/JavaScriptCore/VM/Machine.cpp

    r34303 r34319  
    763763    }
    764764   
    765     for (Vector<Identifier>::const_iterator iter = codeBlock->declaredVariableNames.begin(); iter != codeBlock->declaredVariableNames.end(); ++iter) {
    766         Identifier ident = *iter;
    767        
     765    const Node::VarStack& varStack = codeBlock->ownerNode->varStack();
     766    Node::VarStack::const_iterator varStackEnd = varStack.end();
     767    for (Node::VarStack::const_iterator it = varStack.begin(); it != varStackEnd; ++it) {
     768        const Identifier& ident = (*it).first;
    768769        if (!variableObject->hasProperty(exec, ident))
    769770            variableObject->put(exec, ident, jsUndefined());
    770771    }
    771772   
    772     ASSERT(codeBlock->functions.size() == codeBlock->declaredFunctionNames.size());
    773     for (size_t i = 0; i < codeBlock->functions.size(); ++i)
    774         variableObject->put(exec, codeBlock->declaredFunctionNames[i], codeBlock->functions[i]->makeFunction(exec, scopeChain));
     773    const Node::FunctionStack& functionStack = codeBlock->ownerNode->functionStack();
     774    Node::FunctionStack::const_iterator functionStackEnd = functionStack.end();
     775    for (Node::FunctionStack::const_iterator it = functionStack.begin(); it != functionStackEnd; ++it)
     776        variableObject->put(exec, (*it)->m_ident, (*it)->makeFunction(exec, scopeChain));
    775777   
    776778    size_t oldSize = registerFile->size();
Note: See TracChangeset for help on using the changeset viewer.