OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/builtins/builtins.h" |
| 6 #include "src/builtins/builtins-utils.h" |
| 7 #include "src/code-factory.h" |
| 8 |
| 9 namespace v8 { |
| 10 namespace internal { |
| 11 |
| 12 Handle<Code> Builtins::NonPrimitiveToPrimitive(ToPrimitiveHint hint) { |
| 13 switch (hint) { |
| 14 case ToPrimitiveHint::kDefault: |
| 15 return NonPrimitiveToPrimitive_Default(); |
| 16 case ToPrimitiveHint::kNumber: |
| 17 return NonPrimitiveToPrimitive_Number(); |
| 18 case ToPrimitiveHint::kString: |
| 19 return NonPrimitiveToPrimitive_String(); |
| 20 } |
| 21 UNREACHABLE(); |
| 22 return Handle<Code>::null(); |
| 23 } |
| 24 |
| 25 namespace { |
| 26 // ES6 section 7.1.1 ToPrimitive ( input [ , PreferredType ] ) |
| 27 void Generate_NonPrimitiveToPrimitive(CodeStubAssembler* assembler, |
| 28 ToPrimitiveHint hint) { |
| 29 typedef CodeStubAssembler::Label Label; |
| 30 typedef compiler::Node Node; |
| 31 |
| 32 Node* input = assembler->Parameter(0); |
| 33 Node* context = assembler->Parameter(1); |
| 34 |
| 35 // Lookup the @@toPrimitive property on the {input}. |
| 36 Callable callable = CodeFactory::GetProperty(assembler->isolate()); |
| 37 Node* to_primitive_symbol = |
| 38 assembler->HeapConstant(assembler->factory()->to_primitive_symbol()); |
| 39 Node* exotic_to_prim = |
| 40 assembler->CallStub(callable, context, input, to_primitive_symbol); |
| 41 |
| 42 // Check if {exotic_to_prim} is neither null nor undefined. |
| 43 Label ordinary_to_primitive(assembler); |
| 44 assembler->GotoIf( |
| 45 assembler->WordEqual(exotic_to_prim, assembler->NullConstant()), |
| 46 &ordinary_to_primitive); |
| 47 assembler->GotoIf( |
| 48 assembler->WordEqual(exotic_to_prim, assembler->UndefinedConstant()), |
| 49 &ordinary_to_primitive); |
| 50 { |
| 51 // Invoke the {exotic_to_prim} method on the {input} with a string |
| 52 // representation of the {hint}. |
| 53 Callable callable = CodeFactory::Call(assembler->isolate()); |
| 54 Node* hint_string = assembler->HeapConstant( |
| 55 assembler->factory()->ToPrimitiveHintString(hint)); |
| 56 Node* result = assembler->CallJS(callable, context, exotic_to_prim, input, |
| 57 hint_string); |
| 58 |
| 59 // Verify that the {result} is actually a primitive. |
| 60 Label if_resultisprimitive(assembler), |
| 61 if_resultisnotprimitive(assembler, Label::kDeferred); |
| 62 assembler->GotoIf(assembler->WordIsSmi(result), &if_resultisprimitive); |
| 63 Node* result_instance_type = assembler->LoadInstanceType(result); |
| 64 STATIC_ASSERT(FIRST_PRIMITIVE_TYPE == FIRST_TYPE); |
| 65 assembler->Branch(assembler->Int32LessThanOrEqual( |
| 66 result_instance_type, |
| 67 assembler->Int32Constant(LAST_PRIMITIVE_TYPE)), |
| 68 &if_resultisprimitive, &if_resultisnotprimitive); |
| 69 |
| 70 assembler->Bind(&if_resultisprimitive); |
| 71 { |
| 72 // Just return the {result}. |
| 73 assembler->Return(result); |
| 74 } |
| 75 |
| 76 assembler->Bind(&if_resultisnotprimitive); |
| 77 { |
| 78 // Somehow the @@toPrimitive method on {input} didn't yield a primitive. |
| 79 assembler->TailCallRuntime(Runtime::kThrowCannotConvertToPrimitive, |
| 80 context); |
| 81 } |
| 82 } |
| 83 |
| 84 // Convert using the OrdinaryToPrimitive algorithm instead. |
| 85 assembler->Bind(&ordinary_to_primitive); |
| 86 { |
| 87 Callable callable = CodeFactory::OrdinaryToPrimitive( |
| 88 assembler->isolate(), (hint == ToPrimitiveHint::kString) |
| 89 ? OrdinaryToPrimitiveHint::kString |
| 90 : OrdinaryToPrimitiveHint::kNumber); |
| 91 assembler->TailCallStub(callable, context, input); |
| 92 } |
| 93 } |
| 94 } // anonymous namespace |
| 95 |
| 96 void Builtins::Generate_NonPrimitiveToPrimitive_Default( |
| 97 CodeStubAssembler* assembler) { |
| 98 Generate_NonPrimitiveToPrimitive(assembler, ToPrimitiveHint::kDefault); |
| 99 } |
| 100 |
| 101 void Builtins::Generate_NonPrimitiveToPrimitive_Number( |
| 102 CodeStubAssembler* assembler) { |
| 103 Generate_NonPrimitiveToPrimitive(assembler, ToPrimitiveHint::kNumber); |
| 104 } |
| 105 |
| 106 void Builtins::Generate_NonPrimitiveToPrimitive_String( |
| 107 CodeStubAssembler* assembler) { |
| 108 Generate_NonPrimitiveToPrimitive(assembler, ToPrimitiveHint::kString); |
| 109 } |
| 110 |
| 111 // ES6 section 7.1.3 ToNumber ( argument ) |
| 112 void Builtins::Generate_NonNumberToNumber(CodeStubAssembler* assembler) { |
| 113 typedef CodeStubAssembler::Label Label; |
| 114 typedef compiler::Node Node; |
| 115 typedef CodeStubAssembler::Variable Variable; |
| 116 |
| 117 Node* input = assembler->Parameter(0); |
| 118 Node* context = assembler->Parameter(1); |
| 119 |
| 120 // We might need to loop once here due to ToPrimitive conversions. |
| 121 Variable var_input(assembler, MachineRepresentation::kTagged); |
| 122 Label loop(assembler, &var_input); |
| 123 var_input.Bind(input); |
| 124 assembler->Goto(&loop); |
| 125 assembler->Bind(&loop); |
| 126 { |
| 127 // Load the current {input} value (known to be a HeapObject). |
| 128 Node* input = var_input.value(); |
| 129 |
| 130 // Dispatch on the {input} instance type. |
| 131 Node* input_instance_type = assembler->LoadInstanceType(input); |
| 132 Label if_inputisstring(assembler), if_inputisoddball(assembler), |
| 133 if_inputisreceiver(assembler, Label::kDeferred), |
| 134 if_inputisother(assembler, Label::kDeferred); |
| 135 assembler->GotoIf(assembler->Int32LessThan( |
| 136 input_instance_type, |
| 137 assembler->Int32Constant(FIRST_NONSTRING_TYPE)), |
| 138 &if_inputisstring); |
| 139 assembler->GotoIf( |
| 140 assembler->Word32Equal(input_instance_type, |
| 141 assembler->Int32Constant(ODDBALL_TYPE)), |
| 142 &if_inputisoddball); |
| 143 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE); |
| 144 assembler->Branch(assembler->Int32GreaterThanOrEqual( |
| 145 input_instance_type, |
| 146 assembler->Int32Constant(FIRST_JS_RECEIVER_TYPE)), |
| 147 &if_inputisreceiver, &if_inputisother); |
| 148 |
| 149 assembler->Bind(&if_inputisstring); |
| 150 { |
| 151 // The {input} is a String, use the fast stub to convert it to a Number. |
| 152 // TODO(bmeurer): Consider inlining the StringToNumber logic here. |
| 153 Callable callable = CodeFactory::StringToNumber(assembler->isolate()); |
| 154 assembler->TailCallStub(callable, context, input); |
| 155 } |
| 156 |
| 157 assembler->Bind(&if_inputisoddball); |
| 158 { |
| 159 // The {input} is an Oddball, we just need to the Number value of it. |
| 160 Node* result = |
| 161 assembler->LoadObjectField(input, Oddball::kToNumberOffset); |
| 162 assembler->Return(result); |
| 163 } |
| 164 |
| 165 assembler->Bind(&if_inputisreceiver); |
| 166 { |
| 167 // The {input} is a JSReceiver, we need to convert it to a Primitive first |
| 168 // using the ToPrimitive type conversion, preferably yielding a Number. |
| 169 Callable callable = CodeFactory::NonPrimitiveToPrimitive( |
| 170 assembler->isolate(), ToPrimitiveHint::kNumber); |
| 171 Node* result = assembler->CallStub(callable, context, input); |
| 172 |
| 173 // Check if the {result} is already a Number. |
| 174 Label if_resultisnumber(assembler), if_resultisnotnumber(assembler); |
| 175 assembler->GotoIf(assembler->WordIsSmi(result), &if_resultisnumber); |
| 176 Node* result_map = assembler->LoadMap(result); |
| 177 assembler->Branch( |
| 178 assembler->WordEqual(result_map, assembler->HeapNumberMapConstant()), |
| 179 &if_resultisnumber, &if_resultisnotnumber); |
| 180 |
| 181 assembler->Bind(&if_resultisnumber); |
| 182 { |
| 183 // The ToPrimitive conversion already gave us a Number, so we're done. |
| 184 assembler->Return(result); |
| 185 } |
| 186 |
| 187 assembler->Bind(&if_resultisnotnumber); |
| 188 { |
| 189 // We now have a Primitive {result}, but it's not yet a Number. |
| 190 var_input.Bind(result); |
| 191 assembler->Goto(&loop); |
| 192 } |
| 193 } |
| 194 |
| 195 assembler->Bind(&if_inputisother); |
| 196 { |
| 197 // The {input} is something else (i.e. Symbol or Simd128Value), let the |
| 198 // runtime figure out the correct exception. |
| 199 // Note: We cannot tail call to the runtime here, as js-to-wasm |
| 200 // trampolines also use this code currently, and they declare all |
| 201 // outgoing parameters as untagged, while we would push a tagged |
| 202 // object here. |
| 203 Node* result = assembler->CallRuntime(Runtime::kToNumber, context, input); |
| 204 assembler->Return(result); |
| 205 } |
| 206 } |
| 207 } |
| 208 |
| 209 Handle<Code> Builtins::OrdinaryToPrimitive(OrdinaryToPrimitiveHint hint) { |
| 210 switch (hint) { |
| 211 case OrdinaryToPrimitiveHint::kNumber: |
| 212 return OrdinaryToPrimitive_Number(); |
| 213 case OrdinaryToPrimitiveHint::kString: |
| 214 return OrdinaryToPrimitive_String(); |
| 215 } |
| 216 UNREACHABLE(); |
| 217 return Handle<Code>::null(); |
| 218 } |
| 219 |
| 220 namespace { |
| 221 // 7.1.1.1 OrdinaryToPrimitive ( O, hint ) |
| 222 void Generate_OrdinaryToPrimitive(CodeStubAssembler* assembler, |
| 223 OrdinaryToPrimitiveHint hint) { |
| 224 typedef CodeStubAssembler::Label Label; |
| 225 typedef compiler::Node Node; |
| 226 typedef CodeStubAssembler::Variable Variable; |
| 227 |
| 228 Node* input = assembler->Parameter(0); |
| 229 Node* context = assembler->Parameter(1); |
| 230 |
| 231 Variable var_result(assembler, MachineRepresentation::kTagged); |
| 232 Label return_result(assembler, &var_result); |
| 233 |
| 234 Handle<String> method_names[2]; |
| 235 switch (hint) { |
| 236 case OrdinaryToPrimitiveHint::kNumber: |
| 237 method_names[0] = assembler->factory()->valueOf_string(); |
| 238 method_names[1] = assembler->factory()->toString_string(); |
| 239 break; |
| 240 case OrdinaryToPrimitiveHint::kString: |
| 241 method_names[0] = assembler->factory()->toString_string(); |
| 242 method_names[1] = assembler->factory()->valueOf_string(); |
| 243 break; |
| 244 } |
| 245 for (Handle<String> name : method_names) { |
| 246 // Lookup the {name} on the {input}. |
| 247 Callable callable = CodeFactory::GetProperty(assembler->isolate()); |
| 248 Node* name_string = assembler->HeapConstant(name); |
| 249 Node* method = assembler->CallStub(callable, context, input, name_string); |
| 250 |
| 251 // Check if the {method} is callable. |
| 252 Label if_methodiscallable(assembler), |
| 253 if_methodisnotcallable(assembler, Label::kDeferred); |
| 254 assembler->GotoIf(assembler->WordIsSmi(method), &if_methodisnotcallable); |
| 255 Node* method_map = assembler->LoadMap(method); |
| 256 Node* method_bit_field = assembler->LoadMapBitField(method_map); |
| 257 assembler->Branch( |
| 258 assembler->Word32Equal( |
| 259 assembler->Word32And(method_bit_field, assembler->Int32Constant( |
| 260 1 << Map::kIsCallable)), |
| 261 assembler->Int32Constant(0)), |
| 262 &if_methodisnotcallable, &if_methodiscallable); |
| 263 |
| 264 assembler->Bind(&if_methodiscallable); |
| 265 { |
| 266 // Call the {method} on the {input}. |
| 267 Callable callable = CodeFactory::Call(assembler->isolate()); |
| 268 Node* result = assembler->CallJS(callable, context, method, input); |
| 269 var_result.Bind(result); |
| 270 |
| 271 // Return the {result} if it is a primitive. |
| 272 assembler->GotoIf(assembler->WordIsSmi(result), &return_result); |
| 273 Node* result_instance_type = assembler->LoadInstanceType(result); |
| 274 STATIC_ASSERT(FIRST_PRIMITIVE_TYPE == FIRST_TYPE); |
| 275 assembler->GotoIf(assembler->Int32LessThanOrEqual( |
| 276 result_instance_type, |
| 277 assembler->Int32Constant(LAST_PRIMITIVE_TYPE)), |
| 278 &return_result); |
| 279 } |
| 280 |
| 281 // Just continue with the next {name} if the {method} is not callable. |
| 282 assembler->Goto(&if_methodisnotcallable); |
| 283 assembler->Bind(&if_methodisnotcallable); |
| 284 } |
| 285 |
| 286 assembler->TailCallRuntime(Runtime::kThrowCannotConvertToPrimitive, context); |
| 287 |
| 288 assembler->Bind(&return_result); |
| 289 assembler->Return(var_result.value()); |
| 290 } |
| 291 } // anonymous namespace |
| 292 |
| 293 void Builtins::Generate_OrdinaryToPrimitive_Number( |
| 294 CodeStubAssembler* assembler) { |
| 295 Generate_OrdinaryToPrimitive(assembler, OrdinaryToPrimitiveHint::kNumber); |
| 296 } |
| 297 |
| 298 void Builtins::Generate_OrdinaryToPrimitive_String( |
| 299 CodeStubAssembler* assembler) { |
| 300 Generate_OrdinaryToPrimitive(assembler, OrdinaryToPrimitiveHint::kString); |
| 301 } |
| 302 |
| 303 // ES6 section 7.1.2 ToBoolean ( argument ) |
| 304 void Builtins::Generate_ToBoolean(CodeStubAssembler* assembler) { |
| 305 typedef compiler::Node Node; |
| 306 typedef CodeStubAssembler::Label Label; |
| 307 |
| 308 Node* value = assembler->Parameter(0); |
| 309 |
| 310 Label return_true(assembler), return_false(assembler); |
| 311 assembler->BranchIfToBooleanIsTrue(value, &return_true, &return_false); |
| 312 |
| 313 assembler->Bind(&return_true); |
| 314 assembler->Return(assembler->BooleanConstant(true)); |
| 315 |
| 316 assembler->Bind(&return_false); |
| 317 assembler->Return(assembler->BooleanConstant(false)); |
| 318 } |
| 319 |
| 320 } // namespace internal |
| 321 } // namespace v8 |
OLD | NEW |