1/*
2 * Copyright (C) 2015-2017 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(B3_JIT)
29
30#include "AirArg.h"
31#include "B3Bank.h"
32#include "B3HeapRange.h"
33#include "B3Value.h"
34#include <type_traits>
35
36namespace JSC { namespace B3 {
37
38class JS_EXPORT_PRIVATE MemoryValue : public Value {
39public:
40 static bool accepts(Kind kind)
41 {
42 return isMemoryAccess(kind.opcode());
43 }
44
45 ~MemoryValue();
46
47 OffsetType offset() const { return m_offset; }
48 template<typename Int, typename = IsLegalOffset<Int>>
49 void setOffset(Int offset) { m_offset = offset; }
50
51 // You don't have to worry about using legal offsets unless you've entered quirks mode.
52 template<typename Int,
53 typename = typename std::enable_if<std::is_integral<Int>::value>::type,
54 typename = typename std::enable_if<std::is_signed<Int>::value>::type
55 >
56 bool isLegalOffset(Int offset) const { return isLegalOffsetImpl(offset); }
57
58 // A necessary consequence of MemoryValue having an offset is that it participates in instruction
59 // selection. This tells you if this will get lowered to something that requires an offsetless
60 // address.
61 bool requiresSimpleAddr() const;
62
63 const HeapRange& range() const { return m_range; }
64 void setRange(const HeapRange& range) { m_range = range; }
65
66 // This is an alias for range.
67 const HeapRange& accessRange() const { return range(); }
68 void setAccessRange(const HeapRange& range) { setRange(range); }
69
70 const HeapRange& fenceRange() const { return m_fenceRange; }
71 void setFenceRange(const HeapRange& range) { m_fenceRange = range; }
72
73 bool isStore() const { return B3::isStore(opcode()); }
74 bool isLoad() const { return B3::isLoad(opcode()); }
75
76 bool hasFence() const { return !!fenceRange(); }
77 bool isExotic() const { return hasFence() || isAtom(opcode()); }
78
79 Type accessType() const;
80 Bank accessBank() const;
81 size_t accessByteSize() const;
82
83 Width accessWidth() const;
84
85 bool isCanonicalWidth() const { return B3::isCanonicalWidth(accessWidth()); }
86
87 B3_SPECIALIZE_VALUE_FOR_NON_VARARGS_CHILDREN
88
89protected:
90 void dumpMeta(CommaPrinter&, PrintStream&) const override;
91
92 template<typename Int, typename = IsLegalOffset<Int>, typename... Arguments>
93 MemoryValue(CheckedOpcodeTag, Kind kind, Type type, NumChildren numChildren, Origin origin, Int offset, HeapRange range, HeapRange fenceRange, Arguments... arguments)
94 : Value(CheckedOpcode, kind, type, numChildren, origin, static_cast<Value*>(arguments)...)
95 , m_offset(offset)
96 , m_range(range)
97 , m_fenceRange(fenceRange)
98 {
99 }
100
101private:
102 friend class Procedure;
103 friend class Value;
104
105 bool isLegalOffsetImpl(int32_t offset) const;
106 bool isLegalOffsetImpl(int64_t offset) const;
107
108 enum MemoryValueLoad { MemoryValueLoadTag };
109 enum MemoryValueLoadImplied { MemoryValueLoadImpliedTag };
110 enum MemoryValueStore { MemoryValueStoreTag };
111
112 // Use this form for Load (but not Load8Z, Load8S, or any of the Loads that have a suffix that
113 // describes the returned type).
114 MemoryValue(Kind kind, Type type, Origin origin, Value* pointer)
115 : MemoryValue(MemoryValueLoadTag, kind, type, origin, pointer)
116 {
117 }
118 template<typename Int, typename = IsLegalOffset<Int>>
119 MemoryValue(Kind kind, Type type, Origin origin, Value* pointer, Int offset, HeapRange range = HeapRange::top(), HeapRange fenceRange = HeapRange())
120 : MemoryValue(MemoryValueLoadTag, kind, type, origin, pointer, offset, range, fenceRange)
121 {
122 }
123
124 // Use this form for loads where the return type is implied.
125 MemoryValue(Kind kind, Origin origin, Value* pointer)
126 : MemoryValue(MemoryValueLoadImpliedTag, kind, origin, pointer)
127 {
128 }
129 template<typename Int, typename = IsLegalOffset<Int>>
130 MemoryValue(Kind kind, Origin origin, Value* pointer, Int offset, HeapRange range = HeapRange::top(), HeapRange fenceRange = HeapRange())
131 : MemoryValue(MemoryValueLoadImpliedTag, kind, origin, pointer, offset, range, fenceRange)
132 {
133 }
134
135 // Use this form for stores.
136 MemoryValue(Kind kind, Origin origin, Value* value, Value* pointer)
137 : MemoryValue(MemoryValueStoreTag, kind, origin, value, pointer)
138 {
139 }
140 template<typename Int, typename = IsLegalOffset<Int>>
141 MemoryValue(Kind kind, Origin origin, Value* value, Value* pointer, Int offset, HeapRange range = HeapRange::top(), HeapRange fenceRange = HeapRange())
142 : MemoryValue(MemoryValueStoreTag, kind, origin, value, pointer, offset, range, fenceRange)
143 {
144 }
145
146 // The above templates forward to these implementations.
147 MemoryValue(MemoryValueLoad, Kind, Type, Origin, Value* pointer, OffsetType = 0, HeapRange = HeapRange::top(), HeapRange fenceRange = HeapRange());
148 MemoryValue(MemoryValueLoadImplied, Kind, Origin, Value* pointer, OffsetType = 0, HeapRange = HeapRange::top(), HeapRange fenceRange = HeapRange());
149 MemoryValue(MemoryValueStore, Kind, Origin, Value*, Value* pointer, OffsetType = 0, HeapRange = HeapRange::top(), HeapRange fenceRange = HeapRange());
150
151 OffsetType m_offset { 0 };
152 HeapRange m_range { HeapRange::top() };
153 HeapRange m_fenceRange { HeapRange() };
154};
155
156} } // namespace JSC::B3
157
158#endif // ENABLE(B3_JIT)
159