/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "DFGStackLayoutPhase.h"

#if ENABLE(DFG_JIT)

#include "DFGGraph.h"
#include "DFGPhase.h"
#include "DFGValueSource.h"
#include "Operations.h"

namespace JSC { namespace DFG {

namespace {

class StackLayoutPhase : public Phase {
    static const bool verbose = false;
    
public:
    StackLayoutPhase(Graph& graph)
        : Phase(graph, "stack layout")
    {
    }
    
    bool run()
    {
        // This enumerates the locals that we actually care about and packs them. So for example
        // if we use local 1, 3, 4, 5, 7, then we remap them: 1->0, 3->1, 4->2, 5->3, 7->4. We
        // treat a variable as being "used" if there exists an access to it (SetLocal, GetLocal,
        // Flush, PhantomLocal).
        
        BitVector usedLocals;
        
        // Collect those variables that are used from IR.
        for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
            BasicBlock* block = m_graph.block(blockIndex);
            if (!block)
                continue;
            for (unsigned nodeIndex = block->size(); nodeIndex--;) {
                Node* node = block->at(nodeIndex);
                switch (node->op()) {
                case GetLocal:
                case SetLocal:
                case Flush:
                case PhantomLocal: {
                    VariableAccessData* variable = node->variableAccessData();
                    if (operandIsArgument(variable->local()))
                        break;
                    usedLocals.set(operandToLocal(variable->local()));
                    break;
                }

                case PutStack:
                case GetStack: {
                    StackAccessData* stack = node->stackAccessData();
                    if (VirtualRegister_(stack->local).isArgument())
                        break;
                    usedLocals.set(VirtualRegister_(stack->local).toLocal());
                    break;
                }

                default:
                    break;
                }
            }
        }
        
        for (InlineCallFrameSet::iterator iter = m_graph.m_inlineCallFrames->begin(); !!iter; ++iter) {
			InlineCallFrame* inlineCallFrame = *iter;
            
            for (unsigned argument = inlineCallFrame->arguments.size(); argument-- > 1;) {
                usedLocals.set(operandToLocal((
                    virtualRegisterForArgument(argument) +
                    inlineCallFrame->stackOffset)));
            }
        }
        
        Vector<unsigned> allocation(usedLocals.size());
        m_graph.m_nextMachineLocal = 0;
        for (unsigned i = 0; i < usedLocals.size(); ++i) {
            if (!usedLocals.get(i)) {
                allocation[i] = UINT_MAX;
                continue;
            }
            
            allocation[i] = m_graph.m_nextMachineLocal++;
        }
        
        for (unsigned i = m_graph.m_variableAccessData.size(); i--;) {
            VariableAccessData* variable = &m_graph.m_variableAccessData[i];
            if (!variable->isRoot())
                continue;
            
            if (operandIsArgument(variable->local())) {
                variable->machineLocal() = variable->local();
                continue;
            }
            
            size_t local = operandToLocal(variable->local());
            if (local >= allocation.size())
                continue;
            
            if (allocation[local] == UINT_MAX)
                continue;
            
            variable->machineLocal() = static_cast<VirtualRegister>(virtualRegisterForLocal(
                allocation[operandToLocal(variable->local())]));
        }

        for (auto iter = m_graph.m_stackAccessData.begin(); iter != m_graph.m_stackAccessData.end(); ++iter) {
        	StackAccessData* data = *iter;
            if (!VirtualRegister_(data->local).isLocal()) {
                data->machineLocal = data->local;
                continue;
            }

            if (static_cast<size_t>(VirtualRegister_(data->local).toLocal()) >= allocation.size())
                continue;
            if (allocation[VirtualRegister_(data->local).toLocal()] == UINT_MAX)
                continue;

            data->machineLocal = static_cast<VirtualRegister>(assign(allocation, VirtualRegister_(data->local)).offset());
        }
        
        if (LIKELY(!m_graph.hasDebuggerEnabled()))
            codeBlock()->setScopeRegister(VirtualRegister_());
        else
            codeBlock()->setScopeRegister(assign(allocation, codeBlock()->scopeRegister()));


        for (unsigned i = m_graph.m_inlineVariableData.size(); i--;) {
            InlineVariableData data = m_graph.m_inlineVariableData[i];
            InlineCallFrame* inlineCallFrame = data.inlineCallFrame;
            
			for (unsigned argument = inlineCallFrame->arguments.size(); argument-- > 1;) {
				ArgumentPosition& position = m_graph.m_argumentPositions[
					data.argumentPositionStart + argument];
				VariableAccessData* variable = position.someVariable();
				ValueSource source;
				if (!variable)
					source = ValueSource(SourceIsDead);
				else {
					source = ValueSource::forFlushFormat(
						variable->machineLocal(), variable->flushFormat());
				}
				inlineCallFrame->arguments[argument] = source.valueRecovery();
			}

			RELEASE_ASSERT(inlineCallFrame->isClosureCall == !!data.calleeVariable);

			if (inlineCallFrame->isClosureCall) {
                VariableAccessData* variable = data.calleeVariable->find();
				ValueSource source = ValueSource::forFlushFormat(
					variable->machineLocal(),
					variable->flushFormat());
				inlineCallFrame->calleeRecovery = source.valueRecovery();
			} else
				RELEASE_ASSERT(inlineCallFrame->calleeRecovery.isConstant());
		}
        
        return true;
    }

    VirtualRegister_ assign(const Vector<unsigned>& allocation, VirtualRegister_ src)
    {
        VirtualRegister_ result = src;
        if (result.isLocal()) {
            unsigned myAllocation = allocation[result.toLocal()];
            if (myAllocation == UINT_MAX)
                result = VirtualRegister_();
            else
                result = VirtualRegister_(virtualRegisterForLocal(myAllocation));
        }
        return result;
    }
};

} // Anonymous namespace.

bool performStackLayout(Graph& graph)
{
    SamplingRegion samplingRegion("DFG Stack Layout Phase");
    return runPhase<StackLayoutPhase>(graph);
}

} } // namespace JSC::DFG

#endif // ENABLE(DFG_JIT)

