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() || isAtomic(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
87protected:
88 void dumpMeta(CommaPrinter&, PrintStream&) const override;
89
90 Value* cloneImpl() const override;
91
92 template<typename Int, typename = IsLegalOffset<Int>, typename... Arguments>
93 MemoryValue(CheckedOpcodeTag, Kind kind, Type type, Origin origin, Int offset, HeapRange range, HeapRange fenceRange, Arguments... arguments)
94 : Value(CheckedOpcode, kind, type, origin, arguments...)
95 , m_offset(offset)
96 , m_range(range)
97 , m_fenceRange(fenceRange)
98 {
99 }
100
101private:
102 friend class Procedure;
103
104 bool isLegalOffsetImpl(int32_t offset) const;
105 bool isLegalOffsetImpl(int64_t offset) const;
106
107 enum MemoryValueLoad { MemoryValueLoadTag };
108 enum MemoryValueLoadImplied { MemoryValueLoadImpliedTag };
109 enum MemoryValueStore { MemoryValueStoreTag };
110
111 // Use this form for Load (but not Load8Z, Load8S, or any of the Loads that have a suffix that
112 // describes the returned type).
113 MemoryValue(Kind kind, Type type, Origin origin, Value* pointer)
114 : MemoryValue(MemoryValueLoadTag, kind, type, origin, pointer)
115 {
116 }
117 template<typename Int, typename = IsLegalOffset<Int>>
118 MemoryValue(Kind kind, Type type, Origin origin, Value* pointer, Int offset, HeapRange range = HeapRange::top(), HeapRange fenceRange = HeapRange())
119 : MemoryValue(MemoryValueLoadTag, kind, type, origin, pointer, offset, range, fenceRange)
120 {
121 }
122
123 // Use this form for loads where the return type is implied.
124 MemoryValue(Kind kind, Origin origin, Value* pointer)
125 : MemoryValue(MemoryValueLoadImpliedTag, kind, origin, pointer)
126 {
127 }
128 template<typename Int, typename = IsLegalOffset<Int>>
129 MemoryValue(Kind kind, Origin origin, Value* pointer, Int offset, HeapRange range = HeapRange::top(), HeapRange fenceRange = HeapRange())
130 : MemoryValue(MemoryValueLoadImpliedTag, kind, origin, pointer, offset, range, fenceRange)
131 {
132 }
133
134 // Use this form for stores.
135 MemoryValue(Kind kind, Origin origin, Value* value, Value* pointer)
136 : MemoryValue(MemoryValueStoreTag, kind, origin, value, pointer)
137 {
138 }
139 template<typename Int, typename = IsLegalOffset<Int>>
140 MemoryValue(Kind kind, Origin origin, Value* value, Value* pointer, Int offset, HeapRange range = HeapRange::top(), HeapRange fenceRange = HeapRange())
141 : MemoryValue(MemoryValueStoreTag, kind, origin, value, pointer, offset, range, fenceRange)
142 {
143 }
144
145 // The above templates forward to these implementations.
146 MemoryValue(MemoryValueLoad, Kind, Type, Origin, Value* pointer, OffsetType = 0, HeapRange = HeapRange::top(), HeapRange fenceRange = HeapRange());
147 MemoryValue(MemoryValueLoadImplied, Kind, Origin, Value* pointer, OffsetType = 0, HeapRange = HeapRange::top(), HeapRange fenceRange = HeapRange());
148 MemoryValue(MemoryValueStore, Kind, Origin, Value*, Value* pointer, OffsetType = 0, HeapRange = HeapRange::top(), HeapRange fenceRange = HeapRange());
149
150 OffsetType m_offset { 0 };
151 HeapRange m_range { HeapRange::top() };
152 HeapRange m_fenceRange { HeapRange() };
153};
154
155} } // namespace JSC::B3
156
157#endif // ENABLE(B3_JIT)
158