1/*
2 * Copyright (C) 2013, 2014, 2016 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(DFG_JIT)
29
30#include "DFGCommon.h"
31#include "DFGFrozenValue.h"
32#include <wtf/text/StringImpl.h>
33
34namespace JSC {
35
36class CCallHelpers;
37
38namespace DFG {
39
40class Graph;
41
42// Represents either a JSValue, or for JSValues that require allocation in the heap,
43// it tells you everything you'd need to know in order to allocate it.
44
45class LazyJSValue {
46public:
47 enum LazinessKind {
48 KnownValue,
49 SingleCharacterString,
50 KnownStringImpl,
51 NewStringImpl
52 };
53
54 LazyJSValue(FrozenValue* value = FrozenValue::emptySingleton())
55 : m_kind(KnownValue)
56 {
57 u.value = value;
58 }
59
60 static LazyJSValue singleCharacterString(UChar character)
61 {
62 LazyJSValue result;
63 result.m_kind = SingleCharacterString;
64 result.u.character = character;
65 return result;
66 }
67
68 static LazyJSValue knownStringImpl(StringImpl* string)
69 {
70 LazyJSValue result;
71 result.m_kind = KnownStringImpl;
72 result.u.stringImpl = string;
73 return result;
74 }
75
76 static LazyJSValue newString(Graph&, const String&);
77
78 LazinessKind kind() const { return m_kind; }
79
80 FrozenValue* tryGetValue(Graph&) const
81 {
82 if (m_kind == KnownValue)
83 return value();
84 return nullptr;
85 }
86
87 JSValue getValue(VM&) const;
88
89 FrozenValue* value() const
90 {
91 ASSERT(m_kind == KnownValue);
92 return u.value;
93 }
94
95 UChar character() const
96 {
97 ASSERT(m_kind == SingleCharacterString);
98 return u.character;
99 }
100
101 const StringImpl* tryGetStringImpl(VM&) const;
102
103 String tryGetString(Graph&) const;
104
105 StringImpl* stringImpl() const
106 {
107 ASSERT(m_kind == KnownStringImpl || m_kind == NewStringImpl);
108 return u.stringImpl;
109 }
110
111 TriState strictEqual(const LazyJSValue& other) const;
112
113 uintptr_t switchLookupValue(SwitchKind) const;
114
115 void emit(CCallHelpers&, JSValueRegs) const;
116
117 void dump(PrintStream&) const;
118 void dumpInContext(PrintStream&, DumpContext*) const;
119
120private:
121 union {
122 FrozenValue* value;
123 UChar character;
124 StringImpl* stringImpl;
125 } u;
126 LazinessKind m_kind;
127};
128
129} } // namespace JSC::DFG
130
131#endif // ENABLE(DFG_JIT)
132