| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_COMPILER_JS_OPERATOR_H_ | 5 #ifndef V8_COMPILER_JS_OPERATOR_H_ |
| 6 #define V8_COMPILER_JS_OPERATOR_H_ | 6 #define V8_COMPILER_JS_OPERATOR_H_ |
| 7 | 7 |
| 8 #include "src/compiler/linkage.h" | 8 #include "src/runtime/runtime.h" |
| 9 #include "src/compiler/opcodes.h" | |
| 10 #include "src/compiler/operator.h" | |
| 11 #include "src/unique.h" | 9 #include "src/unique.h" |
| 12 #include "src/zone.h" | |
| 13 | 10 |
| 14 namespace v8 { | 11 namespace v8 { |
| 15 namespace internal { | 12 namespace internal { |
| 16 namespace compiler { | 13 namespace compiler { |
| 17 | 14 |
| 15 // Forward declarations. |
| 16 class Operator; |
| 17 struct JSOperatorBuilderImpl; |
| 18 |
| 19 |
| 20 // Defines the arity and the call flags for a JavaScript function call. This is |
| 21 // used as a parameter by JSCallFunction operators. |
| 22 class CallFunctionParameters FINAL { |
| 23 public: |
| 24 CallFunctionParameters(size_t arity, CallFunctionFlags flags) |
| 25 : arity_(arity), flags_(flags) {} |
| 26 |
| 27 size_t arity() const { return arity_; } |
| 28 CallFunctionFlags flags() const { return flags_; } |
| 29 |
| 30 private: |
| 31 const size_t arity_; |
| 32 const CallFunctionFlags flags_; |
| 33 }; |
| 34 |
| 35 const CallFunctionParameters& CallFunctionParametersOf(const Operator* op); |
| 36 |
| 37 |
| 38 // Defines the arity and the ID for a runtime function call. This is used as a |
| 39 // parameter by JSCallRuntime operators. |
| 40 class CallRuntimeParameters FINAL { |
| 41 public: |
| 42 CallRuntimeParameters(Runtime::FunctionId id, size_t arity) |
| 43 : id_(id), arity_(arity) {} |
| 44 |
| 45 Runtime::FunctionId id() const { return id_; } |
| 46 size_t arity() const { return arity_; } |
| 47 |
| 48 private: |
| 49 const Runtime::FunctionId id_; |
| 50 const size_t arity_; |
| 51 }; |
| 52 |
| 53 const CallRuntimeParameters& CallRuntimeParametersOf(const Operator* op); |
| 54 |
| 55 |
| 18 // Defines the location of a context slot relative to a specific scope. This is | 56 // Defines the location of a context slot relative to a specific scope. This is |
| 19 // used as a parameter by JSLoadContext and JSStoreContext operators and allows | 57 // used as a parameter by JSLoadContext and JSStoreContext operators and allows |
| 20 // accessing a context-allocated variable without keeping track of the scope. | 58 // accessing a context-allocated variable without keeping track of the scope. |
| 21 class ContextAccess { | 59 class ContextAccess FINAL { |
| 22 public: | 60 public: |
| 23 ContextAccess(int depth, int index, bool immutable) | 61 ContextAccess(size_t depth, size_t index, bool immutable); |
| 24 : immutable_(immutable), depth_(depth), index_(index) { | 62 |
| 25 DCHECK(0 <= depth && depth <= kMaxUInt16); | 63 size_t depth() const { return depth_; } |
| 26 DCHECK(0 <= index && static_cast<uint32_t>(index) <= kMaxUInt32); | 64 size_t index() const { return index_; } |
| 27 } | |
| 28 int depth() const { return depth_; } | |
| 29 int index() const { return index_; } | |
| 30 bool immutable() const { return immutable_; } | 65 bool immutable() const { return immutable_; } |
| 31 | 66 |
| 32 private: | 67 private: |
| 33 // For space reasons, we keep this tightly packed, otherwise we could just use | 68 // For space reasons, we keep this tightly packed, otherwise we could just use |
| 34 // a simple int/int/bool POD. | 69 // a simple int/int/bool POD. |
| 35 const bool immutable_; | 70 const bool immutable_; |
| 36 const uint16_t depth_; | 71 const uint16_t depth_; |
| 37 const uint32_t index_; | 72 const uint32_t index_; |
| 38 }; | 73 }; |
| 39 | 74 |
| 75 bool operator==(const ContextAccess& lhs, const ContextAccess& rhs); |
| 76 bool operator!=(const ContextAccess& lhs, const ContextAccess& rhs); |
| 77 |
| 78 const ContextAccess& ContextAccessOf(const Operator* op); |
| 79 |
| 80 |
| 40 // Defines the property being loaded from an object by a named load. This is | 81 // Defines the property being loaded from an object by a named load. This is |
| 41 // used as a parameter by JSLoadNamed operators. | 82 // used as a parameter by JSLoadNamed operators. |
| 42 struct LoadNamedParameters { | 83 class LoadNamedParameters FINAL { |
| 43 Unique<Name> name; | 84 public: |
| 44 ContextualMode contextual_mode; | 85 LoadNamedParameters(const Unique<Name>& name, ContextualMode contextual_mode) |
| 86 : name_(name), contextual_mode_(contextual_mode) {} |
| 87 |
| 88 const Unique<Name>& name() const { return name_; } |
| 89 ContextualMode contextual_mode() const { return contextual_mode_; } |
| 90 |
| 91 private: |
| 92 const Unique<Name> name_; |
| 93 const ContextualMode contextual_mode_; |
| 45 }; | 94 }; |
| 46 | 95 |
| 47 // Defines the arity and the call flags for a JavaScript function call. This is | 96 const LoadNamedParameters& LoadNamedParametersOf(const Operator* op); |
| 48 // used as a parameter by JSCall operators. | 97 |
| 49 struct CallParameters { | |
| 50 int arity; | |
| 51 CallFunctionFlags flags; | |
| 52 }; | |
| 53 | 98 |
| 54 // Defines the property being stored to an object by a named store. This is | 99 // Defines the property being stored to an object by a named store. This is |
| 55 // used as a parameter by JSStoreNamed operators. | 100 // used as a parameter by JSStoreNamed operators. |
| 56 struct StoreNamedParameters { | 101 class StoreNamedParameters FINAL { |
| 57 StrictMode strict_mode; | 102 public: |
| 58 Unique<Name> name; | 103 StoreNamedParameters(StrictMode strict_mode, const Unique<Name>& name) |
| 104 : strict_mode_(strict_mode), name_(name) {} |
| 105 |
| 106 StrictMode strict_mode() const { return strict_mode_; } |
| 107 const Unique<Name>& name() const { return name_; } |
| 108 |
| 109 private: |
| 110 const StrictMode strict_mode_; |
| 111 const Unique<Name> name_; |
| 59 }; | 112 }; |
| 60 | 113 |
| 114 const StoreNamedParameters& StoreNamedParametersOf(const Operator* op); |
| 115 |
| 116 |
| 61 // Interface for building JavaScript-level operators, e.g. directly from the | 117 // Interface for building JavaScript-level operators, e.g. directly from the |
| 62 // AST. Most operators have no parameters, thus can be globally shared for all | 118 // AST. Most operators have no parameters, thus can be globally shared for all |
| 63 // graphs. | 119 // graphs. |
| 64 class JSOperatorBuilder { | 120 class JSOperatorBuilder FINAL { |
| 65 public: | 121 public: |
| 66 explicit JSOperatorBuilder(Zone* zone) : zone_(zone) {} | 122 explicit JSOperatorBuilder(Zone* zone); |
| 67 | 123 |
| 68 #define SIMPLE(name, properties, inputs, outputs) \ | 124 const Operator* Equal(); |
| 69 return new (zone_) \ | 125 const Operator* NotEqual(); |
| 70 SimpleOperator(IrOpcode::k##name, properties, inputs, outputs, #name); | 126 const Operator* StrictEqual(); |
| 127 const Operator* StrictNotEqual(); |
| 128 const Operator* LessThan(); |
| 129 const Operator* GreaterThan(); |
| 130 const Operator* LessThanOrEqual(); |
| 131 const Operator* GreaterThanOrEqual(); |
| 132 const Operator* BitwiseOr(); |
| 133 const Operator* BitwiseXor(); |
| 134 const Operator* BitwiseAnd(); |
| 135 const Operator* ShiftLeft(); |
| 136 const Operator* ShiftRight(); |
| 137 const Operator* ShiftRightLogical(); |
| 138 const Operator* Add(); |
| 139 const Operator* Subtract(); |
| 140 const Operator* Multiply(); |
| 141 const Operator* Divide(); |
| 142 const Operator* Modulus(); |
| 71 | 143 |
| 72 #define NOPROPS(name, inputs, outputs) \ | 144 const Operator* UnaryNot(); |
| 73 SIMPLE(name, Operator::kNoProperties, inputs, outputs) | 145 const Operator* ToBoolean(); |
| 146 const Operator* ToNumber(); |
| 147 const Operator* ToString(); |
| 148 const Operator* ToName(); |
| 149 const Operator* ToObject(); |
| 150 const Operator* Yield(); |
| 74 | 151 |
| 75 #define OP1(name, ptype, pname, properties, inputs, outputs) \ | 152 const Operator* Create(); |
| 76 return new (zone_) Operator1<ptype>(IrOpcode::k##name, properties, inputs, \ | |
| 77 outputs, #name, pname) | |
| 78 | 153 |
| 79 #define BINOP(name) NOPROPS(name, 2, 1) | 154 const Operator* CallFunction(size_t arity, CallFunctionFlags flags); |
| 80 #define UNOP(name) NOPROPS(name, 1, 1) | 155 const Operator* CallRuntime(Runtime::FunctionId id, size_t arity); |
| 81 | 156 |
| 82 #define PURE_BINOP(name) SIMPLE(name, Operator::kPure, 2, 1) | 157 const Operator* CallConstruct(int arguments); |
| 83 | 158 |
| 84 const Operator* Equal() { BINOP(JSEqual); } | 159 const Operator* LoadProperty(); |
| 85 const Operator* NotEqual() { BINOP(JSNotEqual); } | 160 const Operator* LoadNamed(const Unique<Name>& name, |
| 86 const Operator* StrictEqual() { PURE_BINOP(JSStrictEqual); } | 161 ContextualMode contextual_mode = NOT_CONTEXTUAL); |
| 87 const Operator* StrictNotEqual() { PURE_BINOP(JSStrictNotEqual); } | |
| 88 const Operator* LessThan() { BINOP(JSLessThan); } | |
| 89 const Operator* GreaterThan() { BINOP(JSGreaterThan); } | |
| 90 const Operator* LessThanOrEqual() { BINOP(JSLessThanOrEqual); } | |
| 91 const Operator* GreaterThanOrEqual() { BINOP(JSGreaterThanOrEqual); } | |
| 92 const Operator* BitwiseOr() { BINOP(JSBitwiseOr); } | |
| 93 const Operator* BitwiseXor() { BINOP(JSBitwiseXor); } | |
| 94 const Operator* BitwiseAnd() { BINOP(JSBitwiseAnd); } | |
| 95 const Operator* ShiftLeft() { BINOP(JSShiftLeft); } | |
| 96 const Operator* ShiftRight() { BINOP(JSShiftRight); } | |
| 97 const Operator* ShiftRightLogical() { BINOP(JSShiftRightLogical); } | |
| 98 const Operator* Add() { BINOP(JSAdd); } | |
| 99 const Operator* Subtract() { BINOP(JSSubtract); } | |
| 100 const Operator* Multiply() { BINOP(JSMultiply); } | |
| 101 const Operator* Divide() { BINOP(JSDivide); } | |
| 102 const Operator* Modulus() { BINOP(JSModulus); } | |
| 103 | 162 |
| 104 const Operator* UnaryNot() { UNOP(JSUnaryNot); } | 163 const Operator* StoreProperty(StrictMode strict_mode); |
| 105 const Operator* ToBoolean() { UNOP(JSToBoolean); } | 164 const Operator* StoreNamed(StrictMode strict_mode, const Unique<Name>& name); |
| 106 const Operator* ToNumber() { UNOP(JSToNumber); } | |
| 107 const Operator* ToString() { UNOP(JSToString); } | |
| 108 const Operator* ToName() { UNOP(JSToName); } | |
| 109 const Operator* ToObject() { UNOP(JSToObject); } | |
| 110 const Operator* Yield() { UNOP(JSYield); } | |
| 111 | 165 |
| 112 const Operator* Create() { SIMPLE(JSCreate, Operator::kEliminatable, 0, 1); } | 166 const Operator* DeleteProperty(StrictMode strict_mode); |
| 113 | 167 |
| 114 const Operator* Call(int arguments, CallFunctionFlags flags) { | 168 const Operator* HasProperty(); |
| 115 CallParameters parameters = {arguments, flags}; | |
| 116 OP1(JSCallFunction, CallParameters, parameters, Operator::kNoProperties, | |
| 117 arguments, 1); | |
| 118 } | |
| 119 | 169 |
| 120 const Operator* CallNew(int arguments) { | 170 const Operator* LoadContext(size_t depth, size_t index, bool immutable); |
| 121 return new (zone_) | 171 const Operator* StoreContext(size_t depth, size_t index); |
| 122 Operator1<int>(IrOpcode::kJSCallConstruct, Operator::kNoProperties, | |
| 123 arguments, 1, "JSCallConstruct", arguments); | |
| 124 } | |
| 125 | 172 |
| 126 const Operator* LoadProperty() { BINOP(JSLoadProperty); } | 173 const Operator* TypeOf(); |
| 127 const Operator* LoadNamed(Unique<Name> name, | 174 const Operator* InstanceOf(); |
| 128 ContextualMode contextual_mode = NOT_CONTEXTUAL) { | 175 const Operator* Debugger(); |
| 129 LoadNamedParameters parameters = {name, contextual_mode}; | |
| 130 OP1(JSLoadNamed, LoadNamedParameters, parameters, Operator::kNoProperties, | |
| 131 1, 1); | |
| 132 } | |
| 133 | |
| 134 const Operator* StoreProperty(StrictMode strict_mode) { | |
| 135 OP1(JSStoreProperty, StrictMode, strict_mode, Operator::kNoProperties, 3, | |
| 136 0); | |
| 137 } | |
| 138 | |
| 139 const Operator* StoreNamed(StrictMode strict_mode, Unique<Name> name) { | |
| 140 StoreNamedParameters parameters = {strict_mode, name}; | |
| 141 OP1(JSStoreNamed, StoreNamedParameters, parameters, Operator::kNoProperties, | |
| 142 2, 0); | |
| 143 } | |
| 144 | |
| 145 const Operator* DeleteProperty(StrictMode strict_mode) { | |
| 146 OP1(JSDeleteProperty, StrictMode, strict_mode, Operator::kNoProperties, 2, | |
| 147 1); | |
| 148 } | |
| 149 | |
| 150 const Operator* HasProperty() { NOPROPS(JSHasProperty, 2, 1); } | |
| 151 | |
| 152 const Operator* LoadContext(uint16_t depth, uint32_t index, bool immutable) { | |
| 153 ContextAccess access(depth, index, immutable); | |
| 154 OP1(JSLoadContext, ContextAccess, access, | |
| 155 Operator::kEliminatable | Operator::kNoWrite, 1, 1); | |
| 156 } | |
| 157 const Operator* StoreContext(uint16_t depth, uint32_t index) { | |
| 158 ContextAccess access(depth, index, false); | |
| 159 OP1(JSStoreContext, ContextAccess, access, Operator::kNoProperties, 2, 0); | |
| 160 } | |
| 161 | |
| 162 const Operator* TypeOf() { SIMPLE(JSTypeOf, Operator::kPure, 1, 1); } | |
| 163 const Operator* InstanceOf() { NOPROPS(JSInstanceOf, 2, 1); } | |
| 164 const Operator* Debugger() { NOPROPS(JSDebugger, 0, 0); } | |
| 165 | 176 |
| 166 // TODO(titzer): nail down the static parts of each of these context flavors. | 177 // TODO(titzer): nail down the static parts of each of these context flavors. |
| 167 const Operator* CreateFunctionContext() { | 178 const Operator* CreateFunctionContext(); |
| 168 NOPROPS(JSCreateFunctionContext, 1, 1); | 179 const Operator* CreateCatchContext(const Unique<String>& name); |
| 169 } | 180 const Operator* CreateWithContext(); |
| 170 const Operator* CreateCatchContext(Unique<String> name) { | 181 const Operator* CreateBlockContext(); |
| 171 OP1(JSCreateCatchContext, Unique<String>, name, Operator::kNoProperties, 1, | 182 const Operator* CreateModuleContext(); |
| 172 1); | 183 const Operator* CreateGlobalContext(); |
| 173 } | |
| 174 const Operator* CreateWithContext() { NOPROPS(JSCreateWithContext, 2, 1); } | |
| 175 const Operator* CreateBlockContext() { NOPROPS(JSCreateBlockContext, 2, 1); } | |
| 176 const Operator* CreateModuleContext() { | |
| 177 NOPROPS(JSCreateModuleContext, 2, 1); | |
| 178 } | |
| 179 const Operator* CreateGlobalContext() { | |
| 180 NOPROPS(JSCreateGlobalContext, 2, 1); | |
| 181 } | |
| 182 | |
| 183 const Operator* Runtime(Runtime::FunctionId function, int arguments) { | |
| 184 const Runtime::Function* f = Runtime::FunctionForId(function); | |
| 185 DCHECK(f->nargs == -1 || f->nargs == arguments); | |
| 186 OP1(JSCallRuntime, Runtime::FunctionId, function, Operator::kNoProperties, | |
| 187 arguments, f->result_size); | |
| 188 } | |
| 189 | |
| 190 #undef SIMPLE | |
| 191 #undef NOPROPS | |
| 192 #undef OP1 | |
| 193 #undef BINOP | |
| 194 #undef UNOP | |
| 195 | 184 |
| 196 private: | 185 private: |
| 197 Zone* zone_; | 186 Zone* zone() const { return zone_; } |
| 198 }; | |
| 199 | 187 |
| 200 // Specialization for static parameters of type {ContextAccess}. | 188 const JSOperatorBuilderImpl& impl_; |
| 201 template <> | 189 Zone* const zone_; |
| 202 struct StaticParameterTraits<ContextAccess> { | |
| 203 static std::ostream& PrintTo(std::ostream& os, ContextAccess val) { // NOLINT | |
| 204 return os << val.depth() << "," << val.index() | |
| 205 << (val.immutable() ? ",imm" : ""); | |
| 206 } | |
| 207 static int HashCode(ContextAccess val) { | |
| 208 return (val.depth() << 16) | (val.index() & 0xffff); | |
| 209 } | |
| 210 static bool Equals(ContextAccess a, ContextAccess b) { | |
| 211 return a.immutable() == b.immutable() && a.depth() == b.depth() && | |
| 212 a.index() == b.index(); | |
| 213 } | |
| 214 }; | |
| 215 | |
| 216 // Specialization for static parameters of type {Runtime::FunctionId}. | |
| 217 template <> | |
| 218 struct StaticParameterTraits<Runtime::FunctionId> { | |
| 219 static std::ostream& PrintTo(std::ostream& os, | |
| 220 Runtime::FunctionId val) { // NOLINT | |
| 221 const Runtime::Function* f = Runtime::FunctionForId(val); | |
| 222 return os << (f->name ? f->name : "?Runtime?"); | |
| 223 } | |
| 224 static int HashCode(Runtime::FunctionId val) { return static_cast<int>(val); } | |
| 225 static bool Equals(Runtime::FunctionId a, Runtime::FunctionId b) { | |
| 226 return a == b; | |
| 227 } | |
| 228 }; | 190 }; |
| 229 | 191 |
| 230 } // namespace compiler | 192 } // namespace compiler |
| 231 } // namespace internal | 193 } // namespace internal |
| 232 } // namespace v8 | 194 } // namespace v8 |
| 233 | 195 |
| 234 #endif // V8_COMPILER_JS_OPERATOR_H_ | 196 #endif // V8_COMPILER_JS_OPERATOR_H_ |
| OLD | NEW |