VirtualRegister.h
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 virtualRegisterIsLocal(int operand) 35 { 36 return operand < 0; 37 } 38 39 inline bool virtualRegisterIsArgument(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 virtualRegisterForArgumentIncludingThis(int, int); 51 52 static constexpr int invalidVirtualRegister = 0x3fffffff; 53 static constexpr int firstConstantRegisterIndex = FirstConstantRegisterIndex; 54 55 VirtualRegister(RegisterID*); 56 VirtualRegister(RefPtr<RegisterID>); 57 58 VirtualRegister() 59 : m_virtualRegister(invalidVirtualRegister) 60 { } 61 62 explicit VirtualRegister(int virtualRegister) 63 : m_virtualRegister(virtualRegister) 64 { } 65 66 VirtualRegister(CallFrameSlot slot) 67 : m_virtualRegister(static_cast<int>(slot)) 68 { } 69 70 bool isValid() const { return (m_virtualRegister != invalidVirtualRegister); } 71 bool isLocal() const { return virtualRegisterIsLocal(m_virtualRegister); } 72 bool isArgument() const { return virtualRegisterIsArgument(m_virtualRegister); } 73 bool isHeader() const { return m_virtualRegister >= 0 && m_virtualRegister < CallFrameSlot::thisArgument; } 74 bool isConstant() const { return m_virtualRegister >= firstConstantRegisterIndex; } 75 int toLocal() const { ASSERT(isLocal()); return operandToLocal(m_virtualRegister); } 76 int toArgument() const { ASSERT(isArgument()); return operandToArgument(m_virtualRegister); } 77 int toConstantIndex() const { ASSERT(isConstant()); return m_virtualRegister - firstConstantRegisterIndex; } 78 int offset() const { return m_virtualRegister; } 79 int offsetInBytes() const { return m_virtualRegister * sizeof(Register); } 80 81 bool operator==(VirtualRegister other) const { return m_virtualRegister == other.m_virtualRegister; } 82 bool operator!=(VirtualRegister other) const { return m_virtualRegister != other.m_virtualRegister; } 83 bool operator<(VirtualRegister other) const { return m_virtualRegister < other.m_virtualRegister; } 84 bool operator>(VirtualRegister other) const { return m_virtualRegister > other.m_virtualRegister; } 85 bool operator<=(VirtualRegister other) const { return m_virtualRegister <= other.m_virtualRegister; } 86 bool operator>=(VirtualRegister other) const { return m_virtualRegister >= other.m_virtualRegister; } 87 88 VirtualRegister operator+(int value) const 89 { 90 return VirtualRegister(offset() + value); 91 } 92 VirtualRegister operator-(int value) const 93 { 94 return VirtualRegister(offset() - value); 95 } 96 VirtualRegister operator+(VirtualRegister value) const 97 { 98 return VirtualRegister(offset() + value.offset()); 99 } 100 VirtualRegister operator-(VirtualRegister value) const 101 { 102 return VirtualRegister(offset() - value.offset()); 103 } 104 VirtualRegister& operator+=(int value) 105 { 106 return *this = *this + value; 107 } 108 VirtualRegister& operator-=(int value) 109 { 110 return *this = *this - value; 111 } 112 113 void dump(PrintStream& out) const; 114 115 private: 116 static int localToOperand(int local) { return -1 - local; } 117 static int operandToLocal(int operand) { return -1 - operand; } 118 static int operandToArgument(int operand) { return operand - CallFrame::thisArgumentOffset(); } 119 static int argumentToOperand(int argument) { return argument + CallFrame::thisArgumentOffset(); } 120 121 int m_virtualRegister; 122 }; 123 124 COMPILE_ASSERT(sizeof(VirtualRegister) == sizeof(int), VirtualRegister_is_32bit); 125 126 inline VirtualRegister virtualRegisterForLocal(int local) 127 { 128 return VirtualRegister(VirtualRegister::localToOperand(local)); 129 } 130 131 inline VirtualRegister virtualRegisterForArgumentIncludingThis(int argument, int offset = 0) 132 { 133 return VirtualRegister(VirtualRegister::argumentToOperand(argument) + offset); 134 } 135 136 } // namespace JSC