1/*
2 * Copyright (C) 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 FTLStackMaps_h
27#define FTLStackMaps_h
28
29#if ENABLE(FTL_JIT)
30
31#include "DataView.h"
32#include "FTLDWARFRegister.h"
33#include "GPRInfo.h"
34#include "RegisterSet.h"
35#include <wtf/HashMap.h>
36
37namespace JSC {
38
39class MacroAssembler;
40
41namespace FTL {
42
43struct StackMaps {
44    struct ParseContext {
45        unsigned version;
46        DataView* view;
47        unsigned offset;
48    };
49
50    struct Constant {
51        int64_t integer;
52
53        void parse(ParseContext&);
54        void dump(PrintStream& out) const;
55    };
56
57    struct StackSize {
58        uint64_t functionOffset;
59        uint64_t size;
60
61        void parse(ParseContext&);
62        void dump(PrintStream&) const;
63    };
64
65    struct Location {
66        enum Kind : int8_t {
67            Unprocessed,
68            Register,
69            Direct,
70            Indirect,
71            Constant,
72            ConstantIndex
73        };
74
75        DWARFRegister dwarfReg;
76        uint8_t size;
77        Kind kind;
78        int32_t offset;
79
80        void parse(ParseContext&);
81        void dump(PrintStream& out) const;
82
83        GPRReg directGPR() const;
84        void restoreInto(MacroAssembler&, StackMaps&, char* savedRegisters, GPRReg result) const;
85    };
86
87    // FIXME: Investigate how much memory this takes and possibly prune it from the
88    // format we keep around in FTL::JITCode. I suspect that it would be most awesome to
89    // have a CompactStackMaps struct that lossily stores only that subset of StackMaps
90    // and Record that we actually need for OSR exit.
91    // https://bugs.webkit.org/show_bug.cgi?id=130802
92    struct LiveOut {
93        DWARFRegister dwarfReg;
94        uint8_t size;
95
96        void parse(ParseContext&);
97        void dump(PrintStream& out) const;
98    };
99
100    struct Record {
101        uint32_t patchpointID;
102        uint32_t instructionOffset;
103        uint16_t flags;
104
105        Vector<Location> locations;
106        Vector<LiveOut> liveOuts;
107
108        bool parse(ParseContext&);
109        void dump(PrintStream&) const;
110
111        RegisterSet liveOutsSet() const;
112        RegisterSet locationSet() const;
113        RegisterSet usedRegisterSet() const;
114    };
115
116    unsigned version;
117    Vector<StackSize> stackSizes;
118    Vector<Constant> constants;
119    Vector<Record> records;
120
121    bool parse(DataView*); // Returns true on parse success, false on failure. Failure means that LLVM is signaling compile failure to us.
122    void dump(PrintStream&) const;
123    void dumpMultiline(PrintStream&, const char* prefix) const;
124
125    typedef HashMap<uint32_t, Vector<Record>, WTF::IntHash<uint32_t>, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>> RecordMap;
126
127    RecordMap computeRecordMap() const;
128
129    unsigned stackSize() const;
130};
131
132} } // namespace JSC::FTL
133
134namespace WTF {
135
136void printInternal(PrintStream&, JSC::FTL::StackMaps::Location::Kind);
137
138} // namespace WTF
139
140#endif // ENABLE(FTL_JIT)
141
142#endif // FTLStackMaps_h
143
144