~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/wasm/WasmFunctionCodeBlock.h

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 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
#pragma once
 
27
 
 
28
#if ENABLE(WEBASSEMBLY)
 
29
 
 
30
#include "BytecodeConventions.h"
 
31
#include "InstructionStream.h"
 
32
#include "MacroAssemblerCodeRef.h"
 
33
#include "WasmLLIntTierUpCounter.h"
 
34
#include <wtf/HashMap.h>
 
35
#include <wtf/Vector.h>
 
36
 
 
37
namespace JSC {
 
38
 
 
39
class JITCode;
 
40
class LLIntOffsetsExtractor;
 
41
 
 
42
template<typename Traits>
 
43
class BytecodeGeneratorBase;
 
44
 
 
45
namespace Wasm {
 
46
 
 
47
class Signature;
 
48
struct GeneratorTraits;
 
49
enum Type : int8_t;
 
50
 
 
51
// FIXME: Consider merging this with LLIntCallee
 
52
// https://bugs.webkit.org/show_bug.cgi?id=203691
 
53
class FunctionCodeBlock {
 
54
    WTF_MAKE_FAST_ALLOCATED;
 
55
    WTF_MAKE_NONCOPYABLE(FunctionCodeBlock);
 
56
 
 
57
    friend BytecodeGeneratorBase<GeneratorTraits>;
 
58
    friend LLIntOffsetsExtractor;
 
59
    friend class LLIntGenerator;
 
60
 
 
61
public:
 
62
    FunctionCodeBlock(uint32_t functionIndex)
 
63
        : m_functionIndex(functionIndex)
 
64
    {
 
65
    }
 
66
 
 
67
    uint32_t functionIndex() const { return m_functionIndex; }
 
68
    int numVars() const { return m_numVars; }
 
69
    int numCalleeLocals() const { return m_numCalleeLocals; }
 
70
    uint32_t numArguments() const { return m_numArguments; }
 
71
    const Vector<Type>& constantTypes() const { return m_constantTypes; }
 
72
    const Vector<uint64_t>& constants() const { return m_constants; }
 
73
    const InstructionStream& instructions() const { return *m_instructions; }
 
74
 
 
75
    void setNumVars(int numVars) { m_numVars = numVars; }
 
76
    void setNumCalleeLocals(int numCalleeLocals) { m_numCalleeLocals = numCalleeLocals; }
 
77
 
 
78
    ALWAYS_INLINE uint64_t getConstant(VirtualRegister reg) const { return m_constants[reg.toConstantIndex()]; }
 
79
    ALWAYS_INLINE Type getConstantType(VirtualRegister reg) const
 
80
    {
 
81
        ASSERT(Options::dumpGeneratedWasmBytecodes());
 
82
        return m_constantTypes[reg.toConstantIndex()];
 
83
    }
 
84
 
 
85
    void setInstructions(std::unique_ptr<InstructionStream>);
 
86
    void addJumpTarget(InstructionStream::Offset jumpTarget) { m_jumpTargets.append(jumpTarget); }
 
87
    InstructionStream::Offset numberOfJumpTargets() { return m_jumpTargets.size(); }
 
88
    InstructionStream::Offset lastJumpTarget() { return m_jumpTargets.last(); }
 
89
 
 
90
    void addOutOfLineJumpTarget(InstructionStream::Offset, int target);
 
91
    const Instruction* outOfLineJumpTarget(const Instruction*);
 
92
    InstructionStream::Offset outOfLineJumpOffset(InstructionStream::Offset);
 
93
    InstructionStream::Offset outOfLineJumpOffset(const InstructionStream::Ref& instruction)
 
94
    {
 
95
        return outOfLineJumpOffset(instruction.offset());
 
96
    }
 
97
 
 
98
    inline InstructionStream::Offset bytecodeOffset(const Instruction* returnAddress)
 
99
    {
 
100
        const auto* instructionsBegin = m_instructions->at(0).ptr();
 
101
        const auto* instructionsEnd = reinterpret_cast<const Instruction*>(reinterpret_cast<uintptr_t>(instructionsBegin) + m_instructions->size());
 
102
        RELEASE_ASSERT(returnAddress >= instructionsBegin && returnAddress < instructionsEnd);
 
103
        return returnAddress - instructionsBegin;
 
104
    }
 
105
 
 
106
    LLIntTierUpCounter& tierUpCounter() { return m_tierUpCounter; }
 
107
 
 
108
    unsigned addSignature(const Signature&);
 
109
    const Signature& signature(unsigned index) const;
 
110
 
 
111
    struct JumpTableEntry {
 
112
        int target { 0 };
 
113
        unsigned startOffset;
 
114
        unsigned dropCount;
 
115
        unsigned keepCount;
 
116
    };
 
117
 
 
118
    using JumpTable = Vector<JumpTableEntry>;
 
119
    JumpTable& addJumpTable(size_t numberOfEntries);
 
120
    const JumpTable& jumpTable(unsigned tableIndex) const;
 
121
    unsigned numberOfJumpTables() const;
 
122
 
 
123
private:
 
124
    using OutOfLineJumpTargets = HashMap<InstructionStream::Offset, int>;
 
125
 
 
126
    uint32_t m_functionIndex;
 
127
 
 
128
    // Used for the number of WebAssembly locals, as in https://webassembly.github.io/spec/core/syntax/modules.html#syntax-local
 
129
    int m_numVars { 0 };
 
130
    // Number of VirtualRegister. The naming is unfortunate, but has to match UnlinkedCodeBlock
 
131
    int m_numCalleeLocals { 0 };
 
132
    uint32_t m_numArguments { 0 };
 
133
    Vector<Type> m_constantTypes;
 
134
    Vector<uint64_t> m_constants;
 
135
    std::unique_ptr<InstructionStream> m_instructions;
 
136
    const void* m_instructionsRawPointer { nullptr };
 
137
    Vector<InstructionStream::Offset> m_jumpTargets;
 
138
    Vector<const Signature*> m_signatures;
 
139
    OutOfLineJumpTargets m_outOfLineJumpTargets;
 
140
    LLIntTierUpCounter m_tierUpCounter;
 
141
    Vector<JumpTable> m_jumpTables;
 
142
};
 
143
 
 
144
} } // namespace JSC::Wasm
 
145
 
 
146
#endif // ENABLE(WEBASSEMBLY)