| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 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/compiler/code-stub-assembler.h" |
| 6 | |
| 7 | #include <ostream> |
| 8 | |
| 9 | #include "src/code-factory.h" |
| 10 | #include "src/compiler/graph.h" |
| 11 | #include "src/compiler/instruction-selector.h" |
| 12 | #include "src/compiler/linkage.h" |
| 13 | #include "src/compiler/pipeline.h" |
| 14 | #include "src/compiler/raw-machine-assembler.h" |
| 15 | #include "src/compiler/schedule.h" |
| 16 | #include "src/frames.h" |
| 17 | #include "src/interface-descriptors.h" |
| 18 | #include "src/interpreter/bytecodes.h" |
| 19 | #include "src/machine-type.h" |
| 20 | #include "src/macro-assembler.h" |
| 21 | #include "src/zone.h" |
| 22 | |
| 23 | namespace v8 { |
| 24 | namespace internal { |
| 25 | namespace compiler { |
| 26 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 27 | CodeStubAssembler::CodeStubAssembler(Isolate* isolate, Zone* zone, |
| 28 | const CallInterfaceDescriptor& descriptor, |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 29 | Code::Flags flags, const char* name, |
| 30 | size_t result_size) |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | : raw_assembler_(new RawMachineAssembler( |
| 32 | isolate, new (zone) Graph(zone), |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 33 | Linkage::GetStubCallDescriptor( |
| 34 | isolate, zone, descriptor, descriptor.GetStackParameterCount(), |
| 35 | CallDescriptor::kNoFlags, Operator::kNoProperties, |
| 36 | MachineType::AnyTagged(), result_size))), |
| 37 | flags_(flags), |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 38 | name_(name), |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 39 | code_generated_(false), |
| 40 | variables_(zone) {} |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | |
| 42 | CodeStubAssembler::~CodeStubAssembler() {} |
| 43 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 44 | void CodeStubAssembler::CallPrologue() {} |
| 45 | |
| 46 | void CodeStubAssembler::CallEpilogue() {} |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 47 | |
| 48 | Handle<Code> CodeStubAssembler::GenerateCode() { |
| 49 | DCHECK(!code_generated_); |
| 50 | |
| 51 | Schedule* schedule = raw_assembler_->Export(); |
| 52 | Handle<Code> code = Pipeline::GenerateCodeForCodeStub( |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 53 | isolate(), raw_assembler_->call_descriptor(), graph(), schedule, flags_, |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | name_); |
| 55 | |
| 56 | code_generated_ = true; |
| 57 | return code; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | Node* CodeStubAssembler::Int32Constant(int value) { |
| 62 | return raw_assembler_->Int32Constant(value); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | Node* CodeStubAssembler::IntPtrConstant(intptr_t value) { |
| 67 | return raw_assembler_->IntPtrConstant(value); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | Node* CodeStubAssembler::NumberConstant(double value) { |
| 72 | return raw_assembler_->NumberConstant(value); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | Node* CodeStubAssembler::HeapConstant(Handle<HeapObject> object) { |
| 77 | return raw_assembler_->HeapConstant(object); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | Node* CodeStubAssembler::BooleanConstant(bool value) { |
| 82 | return raw_assembler_->BooleanConstant(value); |
| 83 | } |
| 84 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 85 | Node* CodeStubAssembler::ExternalConstant(ExternalReference address) { |
| 86 | return raw_assembler_->ExternalConstant(address); |
| 87 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | |
| 89 | Node* CodeStubAssembler::Parameter(int value) { |
| 90 | return raw_assembler_->Parameter(value); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void CodeStubAssembler::Return(Node* value) { |
| 95 | return raw_assembler_->Return(value); |
| 96 | } |
| 97 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 98 | void CodeStubAssembler::Bind(CodeStubAssembler::Label* label) { |
| 99 | return label->Bind(); |
| 100 | } |
| 101 | |
| 102 | Node* CodeStubAssembler::LoadFramePointer() { |
| 103 | return raw_assembler_->LoadFramePointer(); |
| 104 | } |
| 105 | |
| 106 | Node* CodeStubAssembler::LoadParentFramePointer() { |
| 107 | return raw_assembler_->LoadParentFramePointer(); |
| 108 | } |
| 109 | |
| 110 | Node* CodeStubAssembler::LoadStackPointer() { |
| 111 | return raw_assembler_->LoadStackPointer(); |
| 112 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | |
| 114 | Node* CodeStubAssembler::SmiShiftBitsConstant() { |
| 115 | return Int32Constant(kSmiShiftSize + kSmiTagSize); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | Node* CodeStubAssembler::SmiTag(Node* value) { |
| 120 | return raw_assembler_->WordShl(value, SmiShiftBitsConstant()); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | Node* CodeStubAssembler::SmiUntag(Node* value) { |
| 125 | return raw_assembler_->WordSar(value, SmiShiftBitsConstant()); |
| 126 | } |
| 127 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 128 | #define DEFINE_CODE_STUB_ASSEMBER_BINARY_OP(name) \ |
| 129 | Node* CodeStubAssembler::name(Node* a, Node* b) { \ |
| 130 | return raw_assembler_->name(a, b); \ |
| 131 | } |
| 132 | CODE_STUB_ASSEMBLER_BINARY_OP_LIST(DEFINE_CODE_STUB_ASSEMBER_BINARY_OP) |
| 133 | #undef DEFINE_CODE_STUB_ASSEMBER_BINARY_OP |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 134 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 135 | Node* CodeStubAssembler::ChangeInt32ToInt64(Node* value) { |
| 136 | return raw_assembler_->ChangeInt32ToInt64(value); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 139 | Node* CodeStubAssembler::WordShl(Node* value, int shift) { |
| 140 | return raw_assembler_->WordShl(value, Int32Constant(shift)); |
| 141 | } |
| 142 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 143 | Node* CodeStubAssembler::WordIsSmi(Node* a) { |
| 144 | return WordEqual(raw_assembler_->WordAnd(a, Int32Constant(kSmiTagMask)), |
| 145 | Int32Constant(0)); |
| 146 | } |
| 147 | |
| 148 | Node* CodeStubAssembler::LoadBufferObject(Node* buffer, int offset) { |
| 149 | return raw_assembler_->Load(MachineType::AnyTagged(), buffer, |
| 150 | IntPtrConstant(offset)); |
| 151 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 152 | |
| 153 | Node* CodeStubAssembler::LoadObjectField(Node* object, int offset) { |
| 154 | return raw_assembler_->Load(MachineType::AnyTagged(), object, |
| 155 | IntPtrConstant(offset - kHeapObjectTag)); |
| 156 | } |
| 157 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 158 | Node* CodeStubAssembler::LoadFixedArrayElementSmiIndex(Node* object, |
| 159 | Node* smi_index, |
| 160 | int additional_offset) { |
| 161 | Node* header_size = raw_assembler_->Int32Constant( |
| 162 | additional_offset + FixedArray::kHeaderSize - kHeapObjectTag); |
| 163 | Node* scaled_index = |
| 164 | (kSmiShiftSize == 0) |
| 165 | ? raw_assembler_->Word32Shl( |
| 166 | smi_index, Int32Constant(kPointerSizeLog2 - kSmiTagSize)) |
| 167 | : raw_assembler_->Word32Shl(SmiUntag(smi_index), |
| 168 | Int32Constant(kPointerSizeLog2)); |
| 169 | Node* offset = raw_assembler_->Int32Add(scaled_index, header_size); |
| 170 | return raw_assembler_->Load(MachineType::AnyTagged(), object, offset); |
| 171 | } |
| 172 | |
| 173 | Node* CodeStubAssembler::LoadFixedArrayElementConstantIndex(Node* object, |
| 174 | int index) { |
| 175 | Node* offset = raw_assembler_->Int32Constant( |
| 176 | FixedArray::kHeaderSize - kHeapObjectTag + index * kPointerSize); |
| 177 | return raw_assembler_->Load(MachineType::AnyTagged(), object, offset); |
| 178 | } |
| 179 | |
| 180 | Node* CodeStubAssembler::LoadRoot(Heap::RootListIndex root_index) { |
| 181 | if (isolate()->heap()->RootCanBeTreatedAsConstant(root_index)) { |
| 182 | Handle<Object> root = isolate()->heap()->root_handle(root_index); |
| 183 | if (root->IsSmi()) { |
| 184 | return Int32Constant(Handle<Smi>::cast(root)->value()); |
| 185 | } else { |
| 186 | return HeapConstant(Handle<HeapObject>::cast(root)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | compiler::Node* roots_array_start = |
| 191 | ExternalConstant(ExternalReference::roots_array_start(isolate())); |
| 192 | USE(roots_array_start); |
| 193 | |
| 194 | // TODO(danno): Implement thee root-access case where the root is not constant |
| 195 | // and must be loaded from the root array. |
| 196 | UNIMPLEMENTED(); |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
| 200 | Node* CodeStubAssembler::Load(MachineType rep, Node* base) { |
| 201 | return raw_assembler_->Load(rep, base); |
| 202 | } |
| 203 | |
| 204 | Node* CodeStubAssembler::Load(MachineType rep, Node* base, Node* index) { |
| 205 | return raw_assembler_->Load(rep, base, index); |
| 206 | } |
| 207 | |
| 208 | Node* CodeStubAssembler::Store(MachineRepresentation rep, Node* base, |
| 209 | Node* value) { |
| 210 | return raw_assembler_->Store(rep, base, value, kFullWriteBarrier); |
| 211 | } |
| 212 | |
| 213 | Node* CodeStubAssembler::Store(MachineRepresentation rep, Node* base, |
| 214 | Node* index, Node* value) { |
| 215 | return raw_assembler_->Store(rep, base, index, value, kFullWriteBarrier); |
| 216 | } |
| 217 | |
| 218 | Node* CodeStubAssembler::StoreNoWriteBarrier(MachineRepresentation rep, |
| 219 | Node* base, Node* value) { |
| 220 | return raw_assembler_->Store(rep, base, value, kNoWriteBarrier); |
| 221 | } |
| 222 | |
| 223 | Node* CodeStubAssembler::StoreNoWriteBarrier(MachineRepresentation rep, |
| 224 | Node* base, Node* index, |
| 225 | Node* value) { |
| 226 | return raw_assembler_->Store(rep, base, index, value, kNoWriteBarrier); |
| 227 | } |
| 228 | |
| 229 | Node* CodeStubAssembler::Projection(int index, Node* value) { |
| 230 | return raw_assembler_->Projection(index, value); |
| 231 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 232 | |
| 233 | Node* CodeStubAssembler::CallN(CallDescriptor* descriptor, Node* code_target, |
| 234 | Node** args) { |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 235 | CallPrologue(); |
| 236 | Node* return_value = raw_assembler_->CallN(descriptor, code_target, args); |
| 237 | CallEpilogue(); |
| 238 | return return_value; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
| 242 | Node* CodeStubAssembler::TailCallN(CallDescriptor* descriptor, |
| 243 | Node* code_target, Node** args) { |
| 244 | return raw_assembler_->TailCallN(descriptor, code_target, args); |
| 245 | } |
| 246 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 247 | Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id, |
| 248 | Node* context) { |
| 249 | CallPrologue(); |
| 250 | Node* return_value = raw_assembler_->CallRuntime0(function_id, context); |
| 251 | CallEpilogue(); |
| 252 | return return_value; |
| 253 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 254 | |
| 255 | Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id, |
| 256 | Node* context, Node* arg1) { |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 257 | CallPrologue(); |
| 258 | Node* return_value = raw_assembler_->CallRuntime1(function_id, arg1, context); |
| 259 | CallEpilogue(); |
| 260 | return return_value; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 263 | Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id, |
| 264 | Node* context, Node* arg1, Node* arg2) { |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 265 | CallPrologue(); |
| 266 | Node* return_value = |
| 267 | raw_assembler_->CallRuntime2(function_id, arg1, arg2, context); |
| 268 | CallEpilogue(); |
| 269 | return return_value; |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 272 | Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id, |
| 273 | Node* context, Node* arg1, Node* arg2, |
| 274 | Node* arg3) { |
| 275 | CallPrologue(); |
| 276 | Node* return_value = |
| 277 | raw_assembler_->CallRuntime3(function_id, arg1, arg2, arg3, context); |
| 278 | CallEpilogue(); |
| 279 | return return_value; |
| 280 | } |
| 281 | |
| 282 | Node* CodeStubAssembler::CallRuntime(Runtime::FunctionId function_id, |
| 283 | Node* context, Node* arg1, Node* arg2, |
| 284 | Node* arg3, Node* arg4) { |
| 285 | CallPrologue(); |
| 286 | Node* return_value = raw_assembler_->CallRuntime4(function_id, arg1, arg2, |
| 287 | arg3, arg4, context); |
| 288 | CallEpilogue(); |
| 289 | return return_value; |
| 290 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | |
| 292 | Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id, |
| 293 | Node* context, Node* arg1) { |
| 294 | return raw_assembler_->TailCallRuntime1(function_id, arg1, context); |
| 295 | } |
| 296 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 297 | Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id, |
| 298 | Node* context, Node* arg1, |
| 299 | Node* arg2) { |
| 300 | return raw_assembler_->TailCallRuntime2(function_id, arg1, arg2, context); |
| 301 | } |
| 302 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 303 | Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id, |
| 304 | Node* context, Node* arg1, Node* arg2, |
| 305 | Node* arg3) { |
| 306 | return raw_assembler_->TailCallRuntime3(function_id, arg1, arg2, arg3, |
| 307 | context); |
| 308 | } |
| 309 | |
| 310 | Node* CodeStubAssembler::TailCallRuntime(Runtime::FunctionId function_id, |
| 311 | Node* context, Node* arg1, Node* arg2, |
| 312 | Node* arg3, Node* arg4) { |
| 313 | return raw_assembler_->TailCallRuntime4(function_id, arg1, arg2, arg3, arg4, |
| 314 | context); |
| 315 | } |
| 316 | |
| 317 | Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor, |
| 318 | Node* target, Node* context, Node* arg1, |
| 319 | size_t result_size) { |
| 320 | CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor( |
| 321 | isolate(), zone(), descriptor, descriptor.GetStackParameterCount(), |
| 322 | CallDescriptor::kNoFlags, Operator::kNoProperties, |
| 323 | MachineType::AnyTagged(), result_size); |
| 324 | |
| 325 | Node** args = zone()->NewArray<Node*>(2); |
| 326 | args[0] = arg1; |
| 327 | args[1] = context; |
| 328 | |
| 329 | return CallN(call_descriptor, target, args); |
| 330 | } |
| 331 | |
| 332 | Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor, |
| 333 | Node* target, Node* context, Node* arg1, |
| 334 | Node* arg2, size_t result_size) { |
| 335 | CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor( |
| 336 | isolate(), zone(), descriptor, descriptor.GetStackParameterCount(), |
| 337 | CallDescriptor::kNoFlags, Operator::kNoProperties, |
| 338 | MachineType::AnyTagged(), result_size); |
| 339 | |
| 340 | Node** args = zone()->NewArray<Node*>(3); |
| 341 | args[0] = arg1; |
| 342 | args[1] = arg2; |
| 343 | args[2] = context; |
| 344 | |
| 345 | return CallN(call_descriptor, target, args); |
| 346 | } |
| 347 | |
| 348 | Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor, |
| 349 | Node* target, Node* context, Node* arg1, |
| 350 | Node* arg2, Node* arg3, size_t result_size) { |
| 351 | CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor( |
| 352 | isolate(), zone(), descriptor, descriptor.GetStackParameterCount(), |
| 353 | CallDescriptor::kNoFlags, Operator::kNoProperties, |
| 354 | MachineType::AnyTagged(), result_size); |
| 355 | |
| 356 | Node** args = zone()->NewArray<Node*>(4); |
| 357 | args[0] = arg1; |
| 358 | args[1] = arg2; |
| 359 | args[2] = arg3; |
| 360 | args[3] = context; |
| 361 | |
| 362 | return CallN(call_descriptor, target, args); |
| 363 | } |
| 364 | |
| 365 | Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor, |
| 366 | Node* target, Node* context, Node* arg1, |
| 367 | Node* arg2, Node* arg3, Node* arg4, |
| 368 | size_t result_size) { |
| 369 | CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor( |
| 370 | isolate(), zone(), descriptor, descriptor.GetStackParameterCount(), |
| 371 | CallDescriptor::kNoFlags, Operator::kNoProperties, |
| 372 | MachineType::AnyTagged(), result_size); |
| 373 | |
| 374 | Node** args = zone()->NewArray<Node*>(5); |
| 375 | args[0] = arg1; |
| 376 | args[1] = arg2; |
| 377 | args[2] = arg3; |
| 378 | args[3] = arg4; |
| 379 | args[4] = context; |
| 380 | |
| 381 | return CallN(call_descriptor, target, args); |
| 382 | } |
| 383 | |
| 384 | Node* CodeStubAssembler::CallStub(const CallInterfaceDescriptor& descriptor, |
| 385 | Node* target, Node* context, Node* arg1, |
| 386 | Node* arg2, Node* arg3, Node* arg4, |
| 387 | Node* arg5, size_t result_size) { |
| 388 | CallDescriptor* call_descriptor = Linkage::GetStubCallDescriptor( |
| 389 | isolate(), zone(), descriptor, descriptor.GetStackParameterCount(), |
| 390 | CallDescriptor::kNoFlags, Operator::kNoProperties, |
| 391 | MachineType::AnyTagged(), result_size); |
| 392 | |
| 393 | Node** args = zone()->NewArray<Node*>(6); |
| 394 | args[0] = arg1; |
| 395 | args[1] = arg2; |
| 396 | args[2] = arg3; |
| 397 | args[3] = arg4; |
| 398 | args[4] = arg5; |
| 399 | args[5] = context; |
| 400 | |
| 401 | return CallN(call_descriptor, target, args); |
| 402 | } |
| 403 | |
| 404 | Node* CodeStubAssembler::TailCallStub(CodeStub& stub, Node** args) { |
| 405 | Node* code_target = HeapConstant(stub.GetCode()); |
| 406 | CallDescriptor* descriptor = Linkage::GetStubCallDescriptor( |
| 407 | isolate(), zone(), stub.GetCallInterfaceDescriptor(), |
| 408 | stub.GetStackParameterCount(), CallDescriptor::kSupportsTailCalls); |
| 409 | return raw_assembler_->TailCallN(descriptor, code_target, args); |
| 410 | } |
| 411 | |
| 412 | Node* CodeStubAssembler::TailCall( |
| 413 | const CallInterfaceDescriptor& interface_descriptor, Node* code_target, |
| 414 | Node** args, size_t result_size) { |
| 415 | CallDescriptor* descriptor = Linkage::GetStubCallDescriptor( |
| 416 | isolate(), zone(), interface_descriptor, |
| 417 | interface_descriptor.GetStackParameterCount(), |
| 418 | CallDescriptor::kSupportsTailCalls, Operator::kNoProperties, |
| 419 | MachineType::AnyTagged(), result_size); |
| 420 | return raw_assembler_->TailCallN(descriptor, code_target, args); |
| 421 | } |
| 422 | |
| 423 | void CodeStubAssembler::Goto(CodeStubAssembler::Label* label) { |
| 424 | label->MergeVariables(); |
| 425 | raw_assembler_->Goto(label->label_); |
| 426 | } |
| 427 | |
| 428 | void CodeStubAssembler::Branch(Node* condition, |
| 429 | CodeStubAssembler::Label* true_label, |
| 430 | CodeStubAssembler::Label* false_label) { |
| 431 | true_label->MergeVariables(); |
| 432 | false_label->MergeVariables(); |
| 433 | return raw_assembler_->Branch(condition, true_label->label_, |
| 434 | false_label->label_); |
| 435 | } |
| 436 | |
| 437 | void CodeStubAssembler::Switch(Node* index, Label* default_label, |
| 438 | int32_t* case_values, Label** case_labels, |
| 439 | size_t case_count) { |
| 440 | RawMachineLabel** labels = |
| 441 | new (zone()->New(sizeof(RawMachineLabel*) * case_count)) |
| 442 | RawMachineLabel*[case_count]; |
| 443 | for (size_t i = 0; i < case_count; ++i) { |
| 444 | labels[i] = case_labels[i]->label_; |
| 445 | case_labels[i]->MergeVariables(); |
| 446 | default_label->MergeVariables(); |
| 447 | } |
| 448 | return raw_assembler_->Switch(index, default_label->label_, case_values, |
| 449 | labels, case_count); |
| 450 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 451 | |
| 452 | // RawMachineAssembler delegate helpers: |
| 453 | Isolate* CodeStubAssembler::isolate() { return raw_assembler_->isolate(); } |
| 454 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 455 | Graph* CodeStubAssembler::graph() { return raw_assembler_->graph(); } |
| 456 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 457 | Zone* CodeStubAssembler::zone() { return raw_assembler_->zone(); } |
| 458 | |
| Ben Murdoch | 109988c | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 459 | // The core implementation of Variable is stored through an indirection so |
| 460 | // that it can outlive the often block-scoped Variable declarations. This is |
| 461 | // needed to ensure that variable binding and merging through phis can |
| 462 | // properly be verified. |
| 463 | class CodeStubAssembler::Variable::Impl : public ZoneObject { |
| 464 | public: |
| 465 | explicit Impl(MachineRepresentation rep) : value_(nullptr), rep_(rep) {} |
| 466 | Node* value_; |
| 467 | MachineRepresentation rep_; |
| 468 | }; |
| 469 | |
| 470 | CodeStubAssembler::Variable::Variable(CodeStubAssembler* assembler, |
| 471 | MachineRepresentation rep) |
| 472 | : impl_(new (assembler->zone()) Impl(rep)) { |
| 473 | assembler->variables_.push_back(impl_); |
| 474 | } |
| 475 | |
| 476 | void CodeStubAssembler::Variable::Bind(Node* value) { impl_->value_ = value; } |
| 477 | |
| 478 | Node* CodeStubAssembler::Variable::value() const { |
| 479 | DCHECK_NOT_NULL(impl_->value_); |
| 480 | return impl_->value_; |
| 481 | } |
| 482 | |
| 483 | MachineRepresentation CodeStubAssembler::Variable::rep() const { |
| 484 | return impl_->rep_; |
| 485 | } |
| 486 | |
| 487 | bool CodeStubAssembler::Variable::IsBound() const { |
| 488 | return impl_->value_ != nullptr; |
| 489 | } |
| 490 | |
| 491 | CodeStubAssembler::Label::Label(CodeStubAssembler* assembler) |
| 492 | : bound_(false), merge_count_(0), assembler_(assembler), label_(nullptr) { |
| 493 | void* buffer = assembler->zone()->New(sizeof(RawMachineLabel)); |
| 494 | label_ = new (buffer) RawMachineLabel(); |
| 495 | } |
| 496 | |
| 497 | CodeStubAssembler::Label::Label(CodeStubAssembler* assembler, |
| 498 | int merged_value_count, |
| 499 | CodeStubAssembler::Variable** merged_variables) |
| 500 | : bound_(false), merge_count_(0), assembler_(assembler), label_(nullptr) { |
| 501 | void* buffer = assembler->zone()->New(sizeof(RawMachineLabel)); |
| 502 | label_ = new (buffer) RawMachineLabel(); |
| 503 | for (int i = 0; i < merged_value_count; ++i) { |
| 504 | variable_phis_[merged_variables[i]->impl_] = nullptr; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | CodeStubAssembler::Label::Label(CodeStubAssembler* assembler, |
| 509 | CodeStubAssembler::Variable* merged_variable) |
| 510 | : CodeStubAssembler::Label(assembler, 1, &merged_variable) {} |
| 511 | |
| 512 | void CodeStubAssembler::Label::MergeVariables() { |
| 513 | ++merge_count_; |
| 514 | for (auto var : assembler_->variables_) { |
| 515 | size_t count = 0; |
| 516 | Node* node = var->value_; |
| 517 | if (node != nullptr) { |
| 518 | auto i = variable_merges_.find(var); |
| 519 | if (i != variable_merges_.end()) { |
| 520 | i->second.push_back(node); |
| 521 | count = i->second.size(); |
| 522 | } else { |
| 523 | count = 1; |
| 524 | variable_merges_[var] = std::vector<Node*>(1, node); |
| 525 | } |
| 526 | } |
| 527 | // If the following asserts, then you've jumped to a label without a bound |
| 528 | // variable along that path that expects to merge its value into a phi. |
| 529 | DCHECK(variable_phis_.find(var) == variable_phis_.end() || |
| 530 | count == merge_count_); |
| 531 | USE(count); |
| 532 | |
| 533 | // If the label is already bound, we already know the set of variables to |
| 534 | // merge and phi nodes have already been created. |
| 535 | if (bound_) { |
| 536 | auto phi = variable_phis_.find(var); |
| 537 | if (phi != variable_phis_.end()) { |
| 538 | DCHECK_NOT_NULL(phi->second); |
| 539 | assembler_->raw_assembler_->AppendPhiInput(phi->second, node); |
| 540 | } else { |
| 541 | auto i = variable_merges_.find(var); |
| 542 | USE(i); |
| 543 | // If the following assert fires, then you've declared a variable that |
| 544 | // has the same bound value along all paths up until the point you bound |
| 545 | // this label, but then later merged a path with a new value for the |
| 546 | // variable after the label bind (it's not possible to add phis to the |
| 547 | // bound label after the fact, just make sure to list the variable in |
| 548 | // the label's constructor's list of merged variables). |
| 549 | DCHECK(find_if(i->second.begin(), i->second.end(), |
| 550 | [node](Node* e) -> bool { return node != e; }) == |
| 551 | i->second.end()); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void CodeStubAssembler::Label::Bind() { |
| 558 | DCHECK(!bound_); |
| 559 | assembler_->raw_assembler_->Bind(label_); |
| 560 | |
| 561 | // Make sure that all variables that have changed along any path up to this |
| 562 | // point are marked as merge variables. |
| 563 | for (auto var : assembler_->variables_) { |
| 564 | Node* shared_value = nullptr; |
| 565 | auto i = variable_merges_.find(var); |
| 566 | if (i != variable_merges_.end()) { |
| 567 | for (auto value : i->second) { |
| 568 | DCHECK(value != nullptr); |
| 569 | if (value != shared_value) { |
| 570 | if (shared_value == nullptr) { |
| 571 | shared_value = value; |
| 572 | } else { |
| 573 | variable_phis_[var] = nullptr; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | for (auto var : variable_phis_) { |
| 581 | CodeStubAssembler::Variable::Impl* var_impl = var.first; |
| 582 | auto i = variable_merges_.find(var_impl); |
| 583 | // If the following assert fires, then a variable that has been marked as |
| 584 | // being merged at the label--either by explicitly marking it so in the |
| 585 | // label constructor or by having seen different bound values at branches |
| 586 | // into the label--doesn't have a bound value along all of the paths that |
| 587 | // have been merged into the label up to this point. |
| 588 | DCHECK(i != variable_merges_.end() && i->second.size() == merge_count_); |
| 589 | Node* phi = assembler_->raw_assembler_->Phi( |
| 590 | var.first->rep_, static_cast<int>(merge_count_), &(i->second[0])); |
| 591 | variable_phis_[var_impl] = phi; |
| 592 | } |
| 593 | |
| 594 | // Bind all variables to a merge phi, the common value along all paths or |
| 595 | // null. |
| 596 | for (auto var : assembler_->variables_) { |
| 597 | auto i = variable_phis_.find(var); |
| 598 | if (i != variable_phis_.end()) { |
| 599 | var->value_ = i->second; |
| 600 | } else { |
| 601 | auto j = variable_merges_.find(var); |
| 602 | if (j != variable_merges_.end() && j->second.size() == merge_count_) { |
| 603 | var->value_ = j->second.back(); |
| 604 | } else { |
| 605 | var->value_ = nullptr; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | bound_ = true; |
| 611 | } |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 612 | |
| 613 | } // namespace compiler |
| 614 | } // namespace internal |
| 615 | } // namespace v8 |