summaryrefslogtreecommitdiff
path: root/Source/JavaScriptCore/bytecode/ValueRecovery.h
blob: ebca661d0538435355bbe51aaba8da4ef2294d8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright (C) 2011 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#ifndef ValueRecovery_h
#define ValueRecovery_h

#include "DataFormat.h"
#include "JSValue.h"
#include "MacroAssembler.h"
#include "VirtualRegister.h"
#include <stdio.h>
#include <wtf/Platform.h>

namespace JSC {

// Describes how to recover a given bytecode virtual register at a given
// code point.
enum ValueRecoveryTechnique {
    // It's already in the register file at the right location.
    AlreadyInRegisterFile,
    // It's already in the register file but unboxed.
    AlreadyInRegisterFileAsUnboxedInt32,
    AlreadyInRegisterFileAsUnboxedCell,
    AlreadyInRegisterFileAsUnboxedBoolean,
    AlreadyInRegisterFileAsUnboxedDouble,
    // It's in a register.
    InGPR,
    UnboxedInt32InGPR,
    UnboxedBooleanInGPR,
#if USE(JSVALUE32_64)
    InPair,
#endif
    InFPR,
    UInt32InGPR,
    // It's in the register file, but at a different location.
    DisplacedInRegisterFile,
    // It's in the register file, at a different location, and it's unboxed.
    Int32DisplacedInRegisterFile,
    DoubleDisplacedInRegisterFile,
    CellDisplacedInRegisterFile,
    BooleanDisplacedInRegisterFile,
    // It's an Arguments object.
    ArgumentsThatWereNotCreated,
    // It's a constant.
    Constant,
    // Don't know how to recover it.
    DontKnow
};

class ValueRecovery {
public:
    ValueRecovery()
        : m_technique(DontKnow)
    {
    }
    
    static ValueRecovery alreadyInRegisterFile()
    {
        ValueRecovery result;
        result.m_technique = AlreadyInRegisterFile;
        return result;
    }
    
    static ValueRecovery alreadyInRegisterFileAsUnboxedInt32()
    {
        ValueRecovery result;
        result.m_technique = AlreadyInRegisterFileAsUnboxedInt32;
        return result;
    }
    
    static ValueRecovery alreadyInRegisterFileAsUnboxedCell()
    {
        ValueRecovery result;
        result.m_technique = AlreadyInRegisterFileAsUnboxedCell;
        return result;
    }
    
    static ValueRecovery alreadyInRegisterFileAsUnboxedBoolean()
    {
        ValueRecovery result;
        result.m_technique = AlreadyInRegisterFileAsUnboxedBoolean;
        return result;
    }
    
    static ValueRecovery alreadyInRegisterFileAsUnboxedDouble()
    {
        ValueRecovery result;
        result.m_technique = AlreadyInRegisterFileAsUnboxedDouble;
        return result;
    }
    
    static ValueRecovery inGPR(MacroAssembler::RegisterID gpr, DataFormat dataFormat)
    {
        ASSERT(dataFormat != DataFormatNone);
#if USE(JSVALUE32_64)
        ASSERT(dataFormat == DataFormatInteger || dataFormat == DataFormatCell || dataFormat == DataFormatBoolean);
#endif
        ValueRecovery result;
        if (dataFormat == DataFormatInteger)
            result.m_technique = UnboxedInt32InGPR;
        else if (dataFormat == DataFormatBoolean)
            result.m_technique = UnboxedBooleanInGPR;
        else
            result.m_technique = InGPR;
        result.m_source.gpr = gpr;
        return result;
    }
    
    static ValueRecovery uint32InGPR(MacroAssembler::RegisterID gpr)
    {
        ValueRecovery result;
        result.m_technique = UInt32InGPR;
        result.m_source.gpr = gpr;
        return result;
    }
    
#if USE(JSVALUE32_64)
    static ValueRecovery inPair(MacroAssembler::RegisterID tagGPR, MacroAssembler::RegisterID payloadGPR)
    {
        ValueRecovery result;
        result.m_technique = InPair;
        result.m_source.pair.tagGPR = tagGPR;
        result.m_source.pair.payloadGPR = payloadGPR;
        return result;
    }
#endif

    static ValueRecovery inFPR(MacroAssembler::FPRegisterID fpr)
    {
        ValueRecovery result;
        result.m_technique = InFPR;
        result.m_source.fpr = fpr;
        return result;
    }
    
    static ValueRecovery displacedInRegisterFile(VirtualRegister virtualReg, DataFormat dataFormat)
    {
        ValueRecovery result;
        switch (dataFormat) {
        case DataFormatInteger:
            result.m_technique = Int32DisplacedInRegisterFile;
            break;
            
        case DataFormatDouble:
            result.m_technique = DoubleDisplacedInRegisterFile;
            break;

        case DataFormatCell:
            result.m_technique = CellDisplacedInRegisterFile;
            break;
            
        case DataFormatBoolean:
            result.m_technique = BooleanDisplacedInRegisterFile;
            break;
            
        default:
            ASSERT(dataFormat != DataFormatNone && dataFormat != DataFormatStorage);
            result.m_technique = DisplacedInRegisterFile;
            break;
        }
        result.m_source.virtualReg = virtualReg;
        return result;
    }
    
    static ValueRecovery constant(JSValue value)
    {
        ValueRecovery result;
        result.m_technique = Constant;
        result.m_source.constant = JSValue::encode(value);
        return result;
    }
    
    static ValueRecovery argumentsThatWereNotCreated()
    {
        ValueRecovery result;
        result.m_technique = ArgumentsThatWereNotCreated;
        return result;
    }
    
    ValueRecoveryTechnique technique() const { return m_technique; }
    
    bool isConstant() const { return m_technique == Constant; }
    
    bool isInRegisters() const
    {
        switch (m_technique) {
        case InGPR:
        case UnboxedInt32InGPR:
        case UnboxedBooleanInGPR:
#if USE(JSVALUE32_64)
        case InPair:
#endif
        case InFPR:
            return true;
        default:
            return false;
        }
    }
    
    bool isAlreadyInRegisterFile() const
    {
        switch (technique()) {
        case AlreadyInRegisterFile:
        case AlreadyInRegisterFileAsUnboxedInt32:
        case AlreadyInRegisterFileAsUnboxedCell:
        case AlreadyInRegisterFileAsUnboxedBoolean:
        case AlreadyInRegisterFileAsUnboxedDouble:
            return true;
        default:
            return false;
        }
    }
    
    MacroAssembler::RegisterID gpr() const
    {
        ASSERT(m_technique == InGPR || m_technique == UnboxedInt32InGPR || m_technique == UnboxedBooleanInGPR || m_technique == UInt32InGPR);
        return m_source.gpr;
    }
    
#if USE(JSVALUE32_64)
    MacroAssembler::RegisterID tagGPR() const
    {
        ASSERT(m_technique == InPair);
        return m_source.pair.tagGPR;
    }
    
    MacroAssembler::RegisterID payloadGPR() const
    {
        ASSERT(m_technique == InPair);
        return m_source.pair.payloadGPR;
    }
#endif
    
    MacroAssembler::FPRegisterID fpr() const
    {
        ASSERT(m_technique == InFPR);
        return m_source.fpr;
    }
    
    VirtualRegister virtualRegister() const
    {
        ASSERT(m_technique == DisplacedInRegisterFile || m_technique == Int32DisplacedInRegisterFile || m_technique == DoubleDisplacedInRegisterFile || m_technique == CellDisplacedInRegisterFile || m_technique == BooleanDisplacedInRegisterFile);
        return m_source.virtualReg;
    }
    
    JSValue constant() const
    {
        ASSERT(m_technique == Constant);
        return JSValue::decode(m_source.constant);
    }
    
    void dump(FILE* out) const
    {
        switch (technique()) {
        case AlreadyInRegisterFile:
            fprintf(out, "-");
            break;
        case AlreadyInRegisterFileAsUnboxedInt32:
            fprintf(out, "(int32)");
            break;
        case AlreadyInRegisterFileAsUnboxedCell:
            fprintf(out, "(cell)");
            break;
        case AlreadyInRegisterFileAsUnboxedBoolean:
            fprintf(out, "(bool)");
            break;
        case AlreadyInRegisterFileAsUnboxedDouble:
            fprintf(out, "(double)");
            break;
        case InGPR:
            fprintf(out, "%%r%d", gpr());
            break;
        case UnboxedInt32InGPR:
            fprintf(out, "int32(%%r%d)", gpr());
            break;
        case UnboxedBooleanInGPR:
            fprintf(out, "bool(%%r%d)", gpr());
            break;
        case UInt32InGPR:
            fprintf(out, "uint32(%%r%d)", gpr());
            break;
        case InFPR:
            fprintf(out, "%%fr%d", fpr());
            break;
#if USE(JSVALUE32_64)
        case InPair:
            fprintf(out, "pair(%%r%d, %%r%d)", tagGPR(), payloadGPR());
            break;
#endif
        case DisplacedInRegisterFile:
            fprintf(out, "*%d", virtualRegister());
            break;
        case Int32DisplacedInRegisterFile:
            fprintf(out, "*int32(%d)", virtualRegister());
            break;
        case DoubleDisplacedInRegisterFile:
            fprintf(out, "*double(%d)", virtualRegister());
            break;
        case CellDisplacedInRegisterFile:
            fprintf(out, "*cell(%d)", virtualRegister());
            break;
        case BooleanDisplacedInRegisterFile:
            fprintf(out, "*bool(%d)", virtualRegister());
            break;
        case ArgumentsThatWereNotCreated:
            fprintf(out, "arguments");
            break;
        case Constant:
            fprintf(out, "[%s]", constant().description());
            break;
        case DontKnow:
            fprintf(out, "!");
            break;
        default:
            fprintf(out, "?%d", technique());
            break;
        }
    }
    
private:
    ValueRecoveryTechnique m_technique;
    union {
        MacroAssembler::RegisterID gpr;
        MacroAssembler::FPRegisterID fpr;
#if USE(JSVALUE32_64)
        struct {
            MacroAssembler::RegisterID tagGPR;
            MacroAssembler::RegisterID payloadGPR;
        } pair;
#endif
        VirtualRegister virtualReg;
        EncodedJSValue constant;
    } m_source;
};

} // namespace JSC

#endif // ValueRecovery_h