1/*
2 * Copyright (C) 2012, 2013, 2014 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#ifndef DFGInsertionSet_h
27#define DFGInsertionSet_h
28
29#if ENABLE(DFG_JIT)
30
31#include "DFGGraph.h"
32#include <wtf/Insertion.h>
33#include <wtf/Vector.h>
34
35namespace JSC { namespace DFG {
36
37typedef WTF::Insertion<Node*> Insertion;
38
39class InsertionSet {
40public:
41    InsertionSet(Graph& graph)
42        : m_graph(graph)
43    {
44    }
45
46    Node* insert(const Insertion& insertion)
47    {
48        ASSERT(!m_insertions.size() || m_insertions.last().index() <= insertion.index());
49        m_insertions.append(insertion);
50        return insertion.element();
51    }
52
53    Node* insert(size_t index, Node* element)
54    {
55        return insert(Insertion(index, element));
56    }
57
58    template<typename... Params>
59    Node* insertNode(size_t index, SpeculatedType type, Params... params)
60    {
61        return insert(index, m_graph.addNode(type, params...));
62    }
63
64    Node* insertConstant(
65        size_t index, NodeOrigin origin, JSValue value,
66        NodeType op = JSConstant)
67    {
68        unsigned constantReg =
69            m_graph.constantRegisterForConstant(value);
70        return insertNode(
71            index, speculationFromValue(value), op, origin, OpInfo(constantReg));
72    }
73
74    Node* insertConstant(
75        size_t index, CodeOrigin origin, JSValue value, NodeType op = JSConstant)
76    {
77        return insertConstant(index, NodeOrigin(origin), value, op);
78    }
79
80    Edge insertConstantForUse(
81        size_t index, NodeOrigin origin, JSValue value, UseKind useKind)
82    {
83        NodeType op;
84        if (isDouble(useKind))
85            op = DoubleConstant;
86        else if (useKind == Int52RepUse)
87            op = Int52Constant;
88        else
89            op = JSConstant;
90        return Edge(insertConstant(index, origin, value, op), useKind);
91    }
92
93    Edge insertConstantForUse(
94        size_t index, CodeOrigin origin, JSValue value, UseKind useKind)
95    {
96        return insertConstantForUse(index, NodeOrigin(origin), value, useKind);
97    }
98
99    void execute(BasicBlock* block)
100    {
101        executeInsertions(*block, m_insertions);
102    }
103private:
104    Graph& m_graph;
105    Vector<Insertion, 8> m_insertions;
106};
107
108} } // namespace JSC::DFG
109
110#endif // ENABLE(DFG_JIT)
111
112#endif // DFGInsertionSet_h
113
114