1 | /* |
2 | * Copyright (C) 2011-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 | #include "BytecodeConventions.h" |
29 | #include "CallFrame.h" |
30 | #include <wtf/PrintStream.h> |
31 | |
32 | namespace JSC { |
33 | |
34 | inline bool operandIsLocal(int operand) |
35 | { |
36 | return operand < 0; |
37 | } |
38 | |
39 | inline bool operandIsArgument(int operand) |
40 | { |
41 | return operand >= 0; |
42 | } |
43 | |
44 | |
45 | class RegisterID; |
46 | |
47 | class VirtualRegister { |
48 | public: |
49 | friend VirtualRegister virtualRegisterForLocal(int); |
50 | friend VirtualRegister virtualRegisterForArgument(int, int); |
51 | |
52 | VirtualRegister(RegisterID*); |
53 | VirtualRegister(RefPtr<RegisterID>); |
54 | |
55 | VirtualRegister() |
56 | : m_virtualRegister(s_invalidVirtualRegister) |
57 | { } |
58 | |
59 | explicit VirtualRegister(int virtualRegister) |
60 | : m_virtualRegister(virtualRegister) |
61 | { } |
62 | |
63 | bool isValid() const { return (m_virtualRegister != s_invalidVirtualRegister); } |
64 | bool isLocal() const { return operandIsLocal(m_virtualRegister); } |
65 | bool isArgument() const { return operandIsArgument(m_virtualRegister); } |
66 | bool () const { return m_virtualRegister >= 0 && m_virtualRegister < CallFrameSlot::thisArgument; } |
67 | bool isConstant() const { return m_virtualRegister >= s_firstConstantRegisterIndex; } |
68 | int toLocal() const { ASSERT(isLocal()); return operandToLocal(m_virtualRegister); } |
69 | int toArgument() const { ASSERT(isArgument()); return operandToArgument(m_virtualRegister); } |
70 | int toConstantIndex() const { ASSERT(isConstant()); return m_virtualRegister - s_firstConstantRegisterIndex; } |
71 | int offset() const { return m_virtualRegister; } |
72 | int offsetInBytes() const { return m_virtualRegister * sizeof(Register); } |
73 | |
74 | bool operator==(VirtualRegister other) const { return m_virtualRegister == other.m_virtualRegister; } |
75 | bool operator!=(VirtualRegister other) const { return m_virtualRegister != other.m_virtualRegister; } |
76 | bool operator<(VirtualRegister other) const { return m_virtualRegister < other.m_virtualRegister; } |
77 | bool operator>(VirtualRegister other) const { return m_virtualRegister > other.m_virtualRegister; } |
78 | bool operator<=(VirtualRegister other) const { return m_virtualRegister <= other.m_virtualRegister; } |
79 | bool operator>=(VirtualRegister other) const { return m_virtualRegister >= other.m_virtualRegister; } |
80 | |
81 | VirtualRegister operator+(int value) const |
82 | { |
83 | return VirtualRegister(offset() + value); |
84 | } |
85 | VirtualRegister operator-(int value) const |
86 | { |
87 | return VirtualRegister(offset() - value); |
88 | } |
89 | VirtualRegister operator+(VirtualRegister value) const |
90 | { |
91 | return VirtualRegister(offset() + value.offset()); |
92 | } |
93 | VirtualRegister operator-(VirtualRegister value) const |
94 | { |
95 | return VirtualRegister(offset() - value.offset()); |
96 | } |
97 | VirtualRegister& operator+=(int value) |
98 | { |
99 | return *this = *this + value; |
100 | } |
101 | VirtualRegister& operator-=(int value) |
102 | { |
103 | return *this = *this - value; |
104 | } |
105 | |
106 | void dump(PrintStream& out) const; |
107 | |
108 | private: |
109 | static constexpr int s_invalidVirtualRegister = 0x3fffffff; |
110 | static constexpr int s_firstConstantRegisterIndex = FirstConstantRegisterIndex; |
111 | |
112 | static int localToOperand(int local) { return -1 - local; } |
113 | static int operandToLocal(int operand) { return -1 - operand; } |
114 | static int operandToArgument(int operand) { return operand - CallFrame::thisArgumentOffset(); } |
115 | static int argumentToOperand(int argument) { return argument + CallFrame::thisArgumentOffset(); } |
116 | |
117 | int m_virtualRegister; |
118 | }; |
119 | |
120 | COMPILE_ASSERT(sizeof(VirtualRegister) == sizeof(int), VirtualRegister_is_32bit); |
121 | |
122 | inline VirtualRegister virtualRegisterForLocal(int local) |
123 | { |
124 | return VirtualRegister(VirtualRegister::localToOperand(local)); |
125 | } |
126 | |
127 | inline VirtualRegister virtualRegisterForArgument(int argument, int offset = 0) |
128 | { |
129 | return VirtualRegister(VirtualRegister::argumentToOperand(argument) + offset); |
130 | } |
131 | |
132 | } // namespace JSC |
133 | |