1 | /* |
2 | * Copyright (C) 2011, 2015-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 | #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 | |
54 | VirtualRegister() |
55 | : m_virtualRegister(s_invalidVirtualRegister) |
56 | { } |
57 | |
58 | explicit VirtualRegister(int virtualRegister) |
59 | : m_virtualRegister(virtualRegister) |
60 | { } |
61 | |
62 | bool isValid() const { return (m_virtualRegister != s_invalidVirtualRegister); } |
63 | bool isLocal() const { return operandIsLocal(m_virtualRegister); } |
64 | bool isArgument() const { return operandIsArgument(m_virtualRegister); } |
65 | bool () const { return m_virtualRegister >= 0 && m_virtualRegister < CallFrameSlot::thisArgument; } |
66 | bool isConstant() const { return m_virtualRegister >= s_firstConstantRegisterIndex; } |
67 | int toLocal() const { ASSERT(isLocal()); return operandToLocal(m_virtualRegister); } |
68 | int toArgument() const { ASSERT(isArgument()); return operandToArgument(m_virtualRegister); } |
69 | int toConstantIndex() const { ASSERT(isConstant()); return m_virtualRegister - s_firstConstantRegisterIndex; } |
70 | int offset() const { return m_virtualRegister; } |
71 | int offsetInBytes() const { return m_virtualRegister * sizeof(Register); } |
72 | |
73 | bool operator==(VirtualRegister other) const { return m_virtualRegister == other.m_virtualRegister; } |
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 | |
80 | VirtualRegister operator+(int value) const |
81 | { |
82 | return VirtualRegister(offset() + value); |
83 | } |
84 | VirtualRegister operator-(int value) const |
85 | { |
86 | return VirtualRegister(offset() - value); |
87 | } |
88 | VirtualRegister operator+(VirtualRegister value) const |
89 | { |
90 | return VirtualRegister(offset() + value.offset()); |
91 | } |
92 | VirtualRegister operator-(VirtualRegister value) const |
93 | { |
94 | return VirtualRegister(offset() - value.offset()); |
95 | } |
96 | VirtualRegister& operator+=(int value) |
97 | { |
98 | return *this = *this + value; |
99 | } |
100 | VirtualRegister& operator-=(int value) |
101 | { |
102 | return *this = *this - value; |
103 | } |
104 | |
105 | void dump(PrintStream& out) const; |
106 | |
107 | private: |
108 | static const int s_invalidVirtualRegister = 0x3fffffff; |
109 | static const int s_firstConstantRegisterIndex = FirstConstantRegisterIndex; |
110 | |
111 | static int localToOperand(int local) { return -1 - local; } |
112 | static int operandToLocal(int operand) { return -1 - operand; } |
113 | static int operandToArgument(int operand) { return operand - CallFrame::thisArgumentOffset(); } |
114 | static int argumentToOperand(int argument) { return argument + CallFrame::thisArgumentOffset(); } |
115 | |
116 | int m_virtualRegister; |
117 | }; |
118 | |
119 | COMPILE_ASSERT(sizeof(VirtualRegister) == sizeof(int), VirtualRegister_is_32bit); |
120 | |
121 | inline VirtualRegister virtualRegisterForLocal(int local) |
122 | { |
123 | return VirtualRegister(VirtualRegister::localToOperand(local)); |
124 | } |
125 | |
126 | inline VirtualRegister virtualRegisterForArgument(int argument, int offset = 0) |
127 | { |
128 | return VirtualRegister(VirtualRegister::argumentToOperand(argument) + offset); |
129 | } |
130 | |
131 | } // namespace JSC |
132 | |