1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_COMPILER_MACHINE_TYPE_H 17 #define ECMASCRIPT_COMPILER_MACHINE_TYPE_H 18 19 #include "ecmascript/compiler/gate.h" 20 21 namespace panda::ecmascript::kungfu { 22 class VariableType { 23 public: VariableType()24 VariableType() 25 : machineType_(MachineType::NOVALUE), gateType_(GateType::Empty()) 26 { 27 } 28 VariableType(MachineType machine_type,GateType gate_type)29 VariableType(MachineType machine_type, GateType gate_type) 30 : machineType_(machine_type), gateType_(gate_type) 31 { 32 } 33 GetMachineType()34 [[nodiscard]] MachineType GetMachineType() const 35 { 36 return machineType_; 37 } 38 GetGateType()39 [[nodiscard]] GateType GetGateType() const 40 { 41 return gateType_; 42 } 43 VOID()44 static VariableType VOID() 45 { 46 return VariableType(MachineType::NOVALUE, GateType::Empty()); 47 } 48 BOOL()49 static VariableType BOOL() 50 { 51 return VariableType(MachineType::I1, GateType::NJSValue()); 52 } 53 INT8()54 static VariableType INT8() 55 { 56 return VariableType(MachineType::I8, GateType::NJSValue()); 57 } 58 INT16()59 static VariableType INT16() 60 { 61 return VariableType(MachineType::I16, GateType::NJSValue()); 62 } 63 INT32()64 static VariableType INT32() 65 { 66 return VariableType(MachineType::I32, GateType::NJSValue()); 67 } 68 INT64()69 static VariableType INT64() 70 { 71 return VariableType(MachineType::I64, GateType::NJSValue()); 72 } 73 FLOAT32()74 static VariableType FLOAT32() 75 { 76 return VariableType(MachineType::F32, GateType::NJSValue()); 77 } 78 FLOAT64()79 static VariableType FLOAT64() 80 { 81 return VariableType(MachineType::F64, GateType::NJSValue()); 82 } 83 NATIVE_POINTER()84 static VariableType NATIVE_POINTER() 85 { 86 return VariableType(MachineType::ARCH, GateType::NJSValue()); 87 } 88 JS_ANY()89 static VariableType JS_ANY() 90 { 91 return VariableType(MachineType::I64, GateType::TaggedValue()); 92 } 93 JS_POINTER()94 static VariableType JS_POINTER() 95 { 96 return VariableType(MachineType::I64, GateType::TaggedPointer()); 97 } 98 JS_NOT_POINTER()99 static VariableType JS_NOT_POINTER() 100 { 101 return VariableType(MachineType::I64, GateType::TaggedNPointer()); 102 } 103 104 bool operator==(const VariableType &rhs) const 105 { 106 return (machineType_ == rhs.machineType_) && (gateType_ == rhs.gateType_); 107 } 108 109 bool operator!=(const VariableType &rhs) const 110 { 111 return (machineType_ != rhs.machineType_) || (gateType_ != rhs.gateType_); 112 } 113 114 bool operator<(const VariableType &rhs) const 115 { 116 return (machineType_ < rhs.machineType_) || (machineType_ == rhs.machineType_ && gateType_ < rhs.gateType_); 117 } 118 119 bool operator>(const VariableType &rhs) const 120 { 121 return (machineType_ > rhs.machineType_) || (machineType_ == rhs.machineType_ && gateType_ > rhs.gateType_); 122 } 123 124 bool operator<=(const VariableType &rhs) const 125 { 126 return (machineType_ < rhs.machineType_) || (machineType_ == rhs.machineType_ && gateType_ <= rhs.gateType_); 127 } 128 129 bool operator>=(const VariableType &rhs) const 130 { 131 return (machineType_ > rhs.machineType_) || (machineType_ == rhs.machineType_ && gateType_ >= rhs.gateType_); 132 } 133 134 private: 135 MachineType machineType_; 136 GateType gateType_; 137 }; 138 } // namespace panda::ecmascript::kungfu 139 #endif // ECMASCRIPT_COMPILER_MACHINE_TYPE_H