1 // Copyright 2014 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/raw-machine-assembler.h"
6
7 #include "src/compiler/node-properties.h"
8 #include "src/compiler/pipeline.h"
9 #include "src/compiler/scheduler.h"
10 #include "src/heap/factory-inl.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
RawMachineAssembler(Isolate * isolate,Graph * graph,CallDescriptor * call_descriptor,MachineRepresentation word,MachineOperatorBuilder::Flags flags,MachineOperatorBuilder::AlignmentRequirements alignment_requirements,PoisoningMitigationLevel poisoning_level)16 RawMachineAssembler::RawMachineAssembler(
17 Isolate* isolate, Graph* graph, CallDescriptor* call_descriptor,
18 MachineRepresentation word, MachineOperatorBuilder::Flags flags,
19 MachineOperatorBuilder::AlignmentRequirements alignment_requirements,
20 PoisoningMitigationLevel poisoning_level)
21 : isolate_(isolate),
22 graph_(graph),
23 schedule_(new (zone()) Schedule(zone())),
24 machine_(zone(), word, flags, alignment_requirements),
25 common_(zone()),
26 call_descriptor_(call_descriptor),
27 target_parameter_(nullptr),
28 parameters_(parameter_count(), zone()),
29 current_block_(schedule()->start()),
30 poisoning_level_(poisoning_level) {
31 int param_count = static_cast<int>(parameter_count());
32 // Add an extra input for the JSFunction parameter to the start node.
33 graph->SetStart(graph->NewNode(common_.Start(param_count + 1)));
34 if (call_descriptor->IsJSFunctionCall()) {
35 target_parameter_ = AddNode(
36 common()->Parameter(Linkage::kJSCallClosureParamIndex), graph->start());
37 }
38 for (size_t i = 0; i < parameter_count(); ++i) {
39 parameters_[i] =
40 AddNode(common()->Parameter(static_cast<int>(i)), graph->start());
41 }
42 graph->SetEnd(graph->NewNode(common_.End(0)));
43 }
44
NullConstant()45 Node* RawMachineAssembler::NullConstant() {
46 return HeapConstant(isolate()->factory()->null_value());
47 }
48
UndefinedConstant()49 Node* RawMachineAssembler::UndefinedConstant() {
50 return HeapConstant(isolate()->factory()->undefined_value());
51 }
52
RelocatableIntPtrConstant(intptr_t value,RelocInfo::Mode rmode)53 Node* RawMachineAssembler::RelocatableIntPtrConstant(intptr_t value,
54 RelocInfo::Mode rmode) {
55 return kPointerSize == 8
56 ? RelocatableInt64Constant(value, rmode)
57 : RelocatableInt32Constant(static_cast<int>(value), rmode);
58 }
59
Export()60 Schedule* RawMachineAssembler::Export() {
61 // Compute the correct codegen order.
62 DCHECK(schedule_->rpo_order()->empty());
63 if (FLAG_trace_turbo_scheduler) {
64 PrintF("--- RAW SCHEDULE -------------------------------------------\n");
65 StdoutStream{} << *schedule_;
66 }
67 schedule_->EnsureCFGWellFormedness();
68 Scheduler::ComputeSpecialRPO(zone(), schedule_);
69 schedule_->PropagateDeferredMark();
70 if (FLAG_trace_turbo_scheduler) {
71 PrintF("--- EDGE SPLIT AND PROPAGATED DEFERRED SCHEDULE ------------\n");
72 StdoutStream{} << *schedule_;
73 }
74 // Invalidate RawMachineAssembler.
75 Schedule* schedule = schedule_;
76 schedule_ = nullptr;
77 return schedule;
78 }
79
TargetParameter()80 Node* RawMachineAssembler::TargetParameter() {
81 DCHECK_NOT_NULL(target_parameter_);
82 return target_parameter_;
83 }
84
Parameter(size_t index)85 Node* RawMachineAssembler::Parameter(size_t index) {
86 DCHECK(index < parameter_count());
87 return parameters_[index];
88 }
89
90
Goto(RawMachineLabel * label)91 void RawMachineAssembler::Goto(RawMachineLabel* label) {
92 DCHECK(current_block_ != schedule()->end());
93 schedule()->AddGoto(CurrentBlock(), Use(label));
94 current_block_ = nullptr;
95 }
96
97
Branch(Node * condition,RawMachineLabel * true_val,RawMachineLabel * false_val)98 void RawMachineAssembler::Branch(Node* condition, RawMachineLabel* true_val,
99 RawMachineLabel* false_val) {
100 DCHECK(current_block_ != schedule()->end());
101 Node* branch = MakeNode(
102 common()->Branch(BranchHint::kNone, IsSafetyCheck::kNoSafetyCheck), 1,
103 &condition);
104 schedule()->AddBranch(CurrentBlock(), branch, Use(true_val), Use(false_val));
105 current_block_ = nullptr;
106 }
107
Continuations(Node * call,RawMachineLabel * if_success,RawMachineLabel * if_exception)108 void RawMachineAssembler::Continuations(Node* call, RawMachineLabel* if_success,
109 RawMachineLabel* if_exception) {
110 DCHECK_NOT_NULL(schedule_);
111 DCHECK_NOT_NULL(current_block_);
112 schedule()->AddCall(CurrentBlock(), call, Use(if_success), Use(if_exception));
113 current_block_ = nullptr;
114 }
115
Switch(Node * index,RawMachineLabel * default_label,const int32_t * case_values,RawMachineLabel ** case_labels,size_t case_count)116 void RawMachineAssembler::Switch(Node* index, RawMachineLabel* default_label,
117 const int32_t* case_values,
118 RawMachineLabel** case_labels,
119 size_t case_count) {
120 DCHECK_NE(schedule()->end(), current_block_);
121 size_t succ_count = case_count + 1;
122 Node* switch_node = AddNode(common()->Switch(succ_count), index);
123 BasicBlock** succ_blocks = zone()->NewArray<BasicBlock*>(succ_count);
124 for (size_t index = 0; index < case_count; ++index) {
125 int32_t case_value = case_values[index];
126 BasicBlock* case_block = schedule()->NewBasicBlock();
127 Node* case_node =
128 graph()->NewNode(common()->IfValue(case_value), switch_node);
129 schedule()->AddNode(case_block, case_node);
130 schedule()->AddGoto(case_block, Use(case_labels[index]));
131 succ_blocks[index] = case_block;
132 }
133 BasicBlock* default_block = schedule()->NewBasicBlock();
134 Node* default_node = graph()->NewNode(common()->IfDefault(), switch_node);
135 schedule()->AddNode(default_block, default_node);
136 schedule()->AddGoto(default_block, Use(default_label));
137 succ_blocks[case_count] = default_block;
138 schedule()->AddSwitch(CurrentBlock(), switch_node, succ_blocks, succ_count);
139 current_block_ = nullptr;
140 }
141
Return(Node * value)142 void RawMachineAssembler::Return(Node* value) {
143 Node* values[] = {Int32Constant(0), value};
144 Node* ret = MakeNode(common()->Return(1), 2, values);
145 schedule()->AddReturn(CurrentBlock(), ret);
146 current_block_ = nullptr;
147 }
148
Return(Node * v1,Node * v2)149 void RawMachineAssembler::Return(Node* v1, Node* v2) {
150 Node* values[] = {Int32Constant(0), v1, v2};
151 Node* ret = MakeNode(common()->Return(2), 3, values);
152 schedule()->AddReturn(CurrentBlock(), ret);
153 current_block_ = nullptr;
154 }
155
Return(Node * v1,Node * v2,Node * v3)156 void RawMachineAssembler::Return(Node* v1, Node* v2, Node* v3) {
157 Node* values[] = {Int32Constant(0), v1, v2, v3};
158 Node* ret = MakeNode(common()->Return(3), 4, values);
159 schedule()->AddReturn(CurrentBlock(), ret);
160 current_block_ = nullptr;
161 }
162
Return(Node * v1,Node * v2,Node * v3,Node * v4)163 void RawMachineAssembler::Return(Node* v1, Node* v2, Node* v3, Node* v4) {
164 Node* values[] = {Int32Constant(0), v1, v2, v3, v4};
165 Node* ret = MakeNode(common()->Return(4), 5, values);
166 schedule()->AddReturn(CurrentBlock(), ret);
167 current_block_ = nullptr;
168 }
169
Return(int count,Node * vs[])170 void RawMachineAssembler::Return(int count, Node* vs[]) {
171 typedef Node* Node_ptr;
172 Node** values = new Node_ptr[count + 1];
173 values[0] = Int32Constant(0);
174 for (int i = 0; i < count; ++i) values[i + 1] = vs[i];
175 Node* ret = MakeNode(common()->Return(count), count + 1, values);
176 schedule()->AddReturn(CurrentBlock(), ret);
177 current_block_ = nullptr;
178 delete[] values;
179 }
180
PopAndReturn(Node * pop,Node * value)181 void RawMachineAssembler::PopAndReturn(Node* pop, Node* value) {
182 Node* values[] = {pop, value};
183 Node* ret = MakeNode(common()->Return(1), 2, values);
184 schedule()->AddReturn(CurrentBlock(), ret);
185 current_block_ = nullptr;
186 }
187
PopAndReturn(Node * pop,Node * v1,Node * v2)188 void RawMachineAssembler::PopAndReturn(Node* pop, Node* v1, Node* v2) {
189 Node* values[] = {pop, v1, v2};
190 Node* ret = MakeNode(common()->Return(2), 3, values);
191 schedule()->AddReturn(CurrentBlock(), ret);
192 current_block_ = nullptr;
193 }
194
PopAndReturn(Node * pop,Node * v1,Node * v2,Node * v3)195 void RawMachineAssembler::PopAndReturn(Node* pop, Node* v1, Node* v2,
196 Node* v3) {
197 Node* values[] = {pop, v1, v2, v3};
198 Node* ret = MakeNode(common()->Return(3), 4, values);
199 schedule()->AddReturn(CurrentBlock(), ret);
200 current_block_ = nullptr;
201 }
202
PopAndReturn(Node * pop,Node * v1,Node * v2,Node * v3,Node * v4)203 void RawMachineAssembler::PopAndReturn(Node* pop, Node* v1, Node* v2, Node* v3,
204 Node* v4) {
205 Node* values[] = {pop, v1, v2, v3, v4};
206 Node* ret = MakeNode(common()->Return(4), 5, values);
207 schedule()->AddReturn(CurrentBlock(), ret);
208 current_block_ = nullptr;
209 }
210
DebugAbort(Node * message)211 void RawMachineAssembler::DebugAbort(Node* message) {
212 AddNode(machine()->DebugAbort(), message);
213 }
214
DebugBreak()215 void RawMachineAssembler::DebugBreak() { AddNode(machine()->DebugBreak()); }
216
Unreachable()217 void RawMachineAssembler::Unreachable() {
218 Node* ret = MakeNode(common()->Throw(), 0, nullptr);
219 schedule()->AddThrow(CurrentBlock(), ret);
220 current_block_ = nullptr;
221 }
222
Comment(const char * msg)223 void RawMachineAssembler::Comment(const char* msg) {
224 AddNode(machine()->Comment(msg));
225 }
226
CallN(CallDescriptor * call_descriptor,int input_count,Node * const * inputs)227 Node* RawMachineAssembler::CallN(CallDescriptor* call_descriptor,
228 int input_count, Node* const* inputs) {
229 DCHECK(!call_descriptor->NeedsFrameState());
230 // +1 is for target.
231 DCHECK_EQ(input_count, call_descriptor->ParameterCount() + 1);
232 return AddNode(common()->Call(call_descriptor), input_count, inputs);
233 }
234
CallNWithFrameState(CallDescriptor * call_descriptor,int input_count,Node * const * inputs)235 Node* RawMachineAssembler::CallNWithFrameState(CallDescriptor* call_descriptor,
236 int input_count,
237 Node* const* inputs) {
238 DCHECK(call_descriptor->NeedsFrameState());
239 // +2 is for target and frame state.
240 DCHECK_EQ(input_count, call_descriptor->ParameterCount() + 2);
241 return AddNode(common()->Call(call_descriptor), input_count, inputs);
242 }
243
TailCallN(CallDescriptor * call_descriptor,int input_count,Node * const * inputs)244 Node* RawMachineAssembler::TailCallN(CallDescriptor* call_descriptor,
245 int input_count, Node* const* inputs) {
246 // +1 is for target.
247 DCHECK_EQ(input_count, call_descriptor->ParameterCount() + 1);
248 Node* tail_call =
249 MakeNode(common()->TailCall(call_descriptor), input_count, inputs);
250 schedule()->AddTailCall(CurrentBlock(), tail_call);
251 current_block_ = nullptr;
252 return tail_call;
253 }
254
CallCFunction0(MachineType return_type,Node * function)255 Node* RawMachineAssembler::CallCFunction0(MachineType return_type,
256 Node* function) {
257 MachineSignature::Builder builder(zone(), 1, 0);
258 builder.AddReturn(return_type);
259 auto call_descriptor =
260 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
261
262 return AddNode(common()->Call(call_descriptor), function);
263 }
264
265
CallCFunction1(MachineType return_type,MachineType arg0_type,Node * function,Node * arg0)266 Node* RawMachineAssembler::CallCFunction1(MachineType return_type,
267 MachineType arg0_type, Node* function,
268 Node* arg0) {
269 MachineSignature::Builder builder(zone(), 1, 1);
270 builder.AddReturn(return_type);
271 builder.AddParam(arg0_type);
272 auto call_descriptor =
273 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
274
275 return AddNode(common()->Call(call_descriptor), function, arg0);
276 }
277
CallCFunction1WithCallerSavedRegisters(MachineType return_type,MachineType arg0_type,Node * function,Node * arg0,SaveFPRegsMode mode)278 Node* RawMachineAssembler::CallCFunction1WithCallerSavedRegisters(
279 MachineType return_type, MachineType arg0_type, Node* function, Node* arg0,
280 SaveFPRegsMode mode) {
281 MachineSignature::Builder builder(zone(), 1, 1);
282 builder.AddReturn(return_type);
283 builder.AddParam(arg0_type);
284 auto call_descriptor =
285 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
286
287 call_descriptor->set_save_fp_mode(mode);
288
289 return AddNode(common()->CallWithCallerSavedRegisters(call_descriptor),
290 function, arg0);
291 }
292
CallCFunction2(MachineType return_type,MachineType arg0_type,MachineType arg1_type,Node * function,Node * arg0,Node * arg1)293 Node* RawMachineAssembler::CallCFunction2(MachineType return_type,
294 MachineType arg0_type,
295 MachineType arg1_type, Node* function,
296 Node* arg0, Node* arg1) {
297 MachineSignature::Builder builder(zone(), 1, 2);
298 builder.AddReturn(return_type);
299 builder.AddParam(arg0_type);
300 builder.AddParam(arg1_type);
301 auto call_descriptor =
302 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
303
304 return AddNode(common()->Call(call_descriptor), function, arg0, arg1);
305 }
306
CallCFunction3(MachineType return_type,MachineType arg0_type,MachineType arg1_type,MachineType arg2_type,Node * function,Node * arg0,Node * arg1,Node * arg2)307 Node* RawMachineAssembler::CallCFunction3(MachineType return_type,
308 MachineType arg0_type,
309 MachineType arg1_type,
310 MachineType arg2_type, Node* function,
311 Node* arg0, Node* arg1, Node* arg2) {
312 MachineSignature::Builder builder(zone(), 1, 3);
313 builder.AddReturn(return_type);
314 builder.AddParam(arg0_type);
315 builder.AddParam(arg1_type);
316 builder.AddParam(arg2_type);
317 auto call_descriptor =
318 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
319
320 return AddNode(common()->Call(call_descriptor), function, arg0, arg1, arg2);
321 }
322
CallCFunction3WithCallerSavedRegisters(MachineType return_type,MachineType arg0_type,MachineType arg1_type,MachineType arg2_type,Node * function,Node * arg0,Node * arg1,Node * arg2,SaveFPRegsMode mode)323 Node* RawMachineAssembler::CallCFunction3WithCallerSavedRegisters(
324 MachineType return_type, MachineType arg0_type, MachineType arg1_type,
325 MachineType arg2_type, Node* function, Node* arg0, Node* arg1, Node* arg2,
326 SaveFPRegsMode mode) {
327 MachineSignature::Builder builder(zone(), 1, 3);
328 builder.AddReturn(return_type);
329 builder.AddParam(arg0_type);
330 builder.AddParam(arg1_type);
331 builder.AddParam(arg2_type);
332 auto call_descriptor =
333 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
334
335 call_descriptor->set_save_fp_mode(mode);
336
337 return AddNode(common()->CallWithCallerSavedRegisters(call_descriptor),
338 function, arg0, arg1, arg2);
339 }
340
CallCFunction4(MachineType return_type,MachineType arg0_type,MachineType arg1_type,MachineType arg2_type,MachineType arg3_type,Node * function,Node * arg0,Node * arg1,Node * arg2,Node * arg3)341 Node* RawMachineAssembler::CallCFunction4(
342 MachineType return_type, MachineType arg0_type, MachineType arg1_type,
343 MachineType arg2_type, MachineType arg3_type, Node* function, Node* arg0,
344 Node* arg1, Node* arg2, Node* arg3) {
345 MachineSignature::Builder builder(zone(), 1, 4);
346 builder.AddReturn(return_type);
347 builder.AddParam(arg0_type);
348 builder.AddParam(arg1_type);
349 builder.AddParam(arg2_type);
350 builder.AddParam(arg3_type);
351 auto call_descriptor =
352 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
353
354 return AddNode(common()->Call(call_descriptor), function, arg0, arg1, arg2,
355 arg3);
356 }
357
CallCFunction5(MachineType return_type,MachineType arg0_type,MachineType arg1_type,MachineType arg2_type,MachineType arg3_type,MachineType arg4_type,Node * function,Node * arg0,Node * arg1,Node * arg2,Node * arg3,Node * arg4)358 Node* RawMachineAssembler::CallCFunction5(
359 MachineType return_type, MachineType arg0_type, MachineType arg1_type,
360 MachineType arg2_type, MachineType arg3_type, MachineType arg4_type,
361 Node* function, Node* arg0, Node* arg1, Node* arg2, Node* arg3,
362 Node* arg4) {
363 MachineSignature::Builder builder(zone(), 1, 5);
364 builder.AddReturn(return_type);
365 builder.AddParam(arg0_type);
366 builder.AddParam(arg1_type);
367 builder.AddParam(arg2_type);
368 builder.AddParam(arg3_type);
369 builder.AddParam(arg4_type);
370 auto call_descriptor =
371 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
372
373 return AddNode(common()->Call(call_descriptor), function, arg0, arg1, arg2,
374 arg3, arg4);
375 }
376
CallCFunction6(MachineType return_type,MachineType arg0_type,MachineType arg1_type,MachineType arg2_type,MachineType arg3_type,MachineType arg4_type,MachineType arg5_type,Node * function,Node * arg0,Node * arg1,Node * arg2,Node * arg3,Node * arg4,Node * arg5)377 Node* RawMachineAssembler::CallCFunction6(
378 MachineType return_type, MachineType arg0_type, MachineType arg1_type,
379 MachineType arg2_type, MachineType arg3_type, MachineType arg4_type,
380 MachineType arg5_type, Node* function, Node* arg0, Node* arg1, Node* arg2,
381 Node* arg3, Node* arg4, Node* arg5) {
382 MachineSignature::Builder builder(zone(), 1, 6);
383 builder.AddReturn(return_type);
384 builder.AddParam(arg0_type);
385 builder.AddParam(arg1_type);
386 builder.AddParam(arg2_type);
387 builder.AddParam(arg3_type);
388 builder.AddParam(arg4_type);
389 builder.AddParam(arg5_type);
390 auto call_descriptor =
391 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
392
393 return AddNode(common()->Call(call_descriptor), function, arg0, arg1, arg2,
394 arg3, arg4, arg5);
395 }
396
CallCFunction8(MachineType return_type,MachineType arg0_type,MachineType arg1_type,MachineType arg2_type,MachineType arg3_type,MachineType arg4_type,MachineType arg5_type,MachineType arg6_type,MachineType arg7_type,Node * function,Node * arg0,Node * arg1,Node * arg2,Node * arg3,Node * arg4,Node * arg5,Node * arg6,Node * arg7)397 Node* RawMachineAssembler::CallCFunction8(
398 MachineType return_type, MachineType arg0_type, MachineType arg1_type,
399 MachineType arg2_type, MachineType arg3_type, MachineType arg4_type,
400 MachineType arg5_type, MachineType arg6_type, MachineType arg7_type,
401 Node* function, Node* arg0, Node* arg1, Node* arg2, Node* arg3, Node* arg4,
402 Node* arg5, Node* arg6, Node* arg7) {
403 MachineSignature::Builder builder(zone(), 1, 8);
404 builder.AddReturn(return_type);
405 builder.AddParam(arg0_type);
406 builder.AddParam(arg1_type);
407 builder.AddParam(arg2_type);
408 builder.AddParam(arg3_type);
409 builder.AddParam(arg4_type);
410 builder.AddParam(arg5_type);
411 builder.AddParam(arg6_type);
412 builder.AddParam(arg7_type);
413 Node* args[] = {function, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7};
414 auto call_descriptor =
415 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
416 return AddNode(common()->Call(call_descriptor), arraysize(args), args);
417 }
418
CallCFunction9(MachineType return_type,MachineType arg0_type,MachineType arg1_type,MachineType arg2_type,MachineType arg3_type,MachineType arg4_type,MachineType arg5_type,MachineType arg6_type,MachineType arg7_type,MachineType arg8_type,Node * function,Node * arg0,Node * arg1,Node * arg2,Node * arg3,Node * arg4,Node * arg5,Node * arg6,Node * arg7,Node * arg8)419 Node* RawMachineAssembler::CallCFunction9(
420 MachineType return_type, MachineType arg0_type, MachineType arg1_type,
421 MachineType arg2_type, MachineType arg3_type, MachineType arg4_type,
422 MachineType arg5_type, MachineType arg6_type, MachineType arg7_type,
423 MachineType arg8_type, Node* function, Node* arg0, Node* arg1, Node* arg2,
424 Node* arg3, Node* arg4, Node* arg5, Node* arg6, Node* arg7, Node* arg8) {
425 MachineSignature::Builder builder(zone(), 1, 9);
426 builder.AddReturn(return_type);
427 builder.AddParam(arg0_type);
428 builder.AddParam(arg1_type);
429 builder.AddParam(arg2_type);
430 builder.AddParam(arg3_type);
431 builder.AddParam(arg4_type);
432 builder.AddParam(arg5_type);
433 builder.AddParam(arg6_type);
434 builder.AddParam(arg7_type);
435 builder.AddParam(arg8_type);
436 Node* args[] = {function, arg0, arg1, arg2, arg3,
437 arg4, arg5, arg6, arg7, arg8};
438 auto call_descriptor =
439 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build());
440 return AddNode(common()->Call(call_descriptor), arraysize(args), args);
441 }
442
Use(RawMachineLabel * label)443 BasicBlock* RawMachineAssembler::Use(RawMachineLabel* label) {
444 label->used_ = true;
445 return EnsureBlock(label);
446 }
447
EnsureBlock(RawMachineLabel * label)448 BasicBlock* RawMachineAssembler::EnsureBlock(RawMachineLabel* label) {
449 if (label->block_ == nullptr) {
450 label->block_ = schedule()->NewBasicBlock();
451 }
452 return label->block_;
453 }
454
Bind(RawMachineLabel * label)455 void RawMachineAssembler::Bind(RawMachineLabel* label) {
456 DCHECK_NULL(current_block_);
457 DCHECK(!label->bound_);
458 label->bound_ = true;
459 current_block_ = EnsureBlock(label);
460 current_block_->set_deferred(label->deferred_);
461 }
462
463 #if DEBUG
Bind(RawMachineLabel * label,AssemblerDebugInfo info)464 void RawMachineAssembler::Bind(RawMachineLabel* label,
465 AssemblerDebugInfo info) {
466 if (current_block_ != nullptr) {
467 std::stringstream str;
468 str << "Binding label without closing previous block:"
469 << "\n# label: " << info
470 << "\n# previous block: " << *current_block_;
471 FATAL("%s", str.str().c_str());
472 }
473 Bind(label);
474 current_block_->set_debug_info(info);
475 }
476
PrintCurrentBlock(std::ostream & os)477 void RawMachineAssembler::PrintCurrentBlock(std::ostream& os) {
478 os << CurrentBlock();
479 }
480
InsideBlock()481 bool RawMachineAssembler::InsideBlock() { return current_block_ != nullptr; }
482
SetInitialDebugInformation(AssemblerDebugInfo debug_info)483 void RawMachineAssembler::SetInitialDebugInformation(
484 AssemblerDebugInfo debug_info) {
485 CurrentBlock()->set_debug_info(debug_info);
486 }
487 #endif // DEBUG
488
CurrentBlock()489 BasicBlock* RawMachineAssembler::CurrentBlock() {
490 DCHECK(current_block_);
491 return current_block_;
492 }
493
Phi(MachineRepresentation rep,int input_count,Node * const * inputs)494 Node* RawMachineAssembler::Phi(MachineRepresentation rep, int input_count,
495 Node* const* inputs) {
496 Node** buffer = new (zone()->New(sizeof(Node*) * (input_count + 1)))
497 Node*[input_count + 1];
498 std::copy(inputs, inputs + input_count, buffer);
499 buffer[input_count] = graph()->start();
500 return AddNode(common()->Phi(rep, input_count), input_count + 1, buffer);
501 }
502
AppendPhiInput(Node * phi,Node * new_input)503 void RawMachineAssembler::AppendPhiInput(Node* phi, Node* new_input) {
504 const Operator* op = phi->op();
505 const Operator* new_op = common()->ResizeMergeOrPhi(op, phi->InputCount());
506 phi->InsertInput(zone(), phi->InputCount() - 1, new_input);
507 NodeProperties::ChangeOp(phi, new_op);
508 }
509
AddNode(const Operator * op,int input_count,Node * const * inputs)510 Node* RawMachineAssembler::AddNode(const Operator* op, int input_count,
511 Node* const* inputs) {
512 DCHECK_NOT_NULL(schedule_);
513 DCHECK_NOT_NULL(current_block_);
514 Node* node = MakeNode(op, input_count, inputs);
515 schedule()->AddNode(CurrentBlock(), node);
516 return node;
517 }
518
MakeNode(const Operator * op,int input_count,Node * const * inputs)519 Node* RawMachineAssembler::MakeNode(const Operator* op, int input_count,
520 Node* const* inputs) {
521 // The raw machine assembler nodes do not have effect and control inputs,
522 // so we disable checking input counts here.
523 return graph()->NewNodeUnchecked(op, input_count, inputs);
524 }
525
~RawMachineLabel()526 RawMachineLabel::~RawMachineLabel() {
527 #if DEBUG
528 if (bound_ == used_) return;
529 std::stringstream str;
530 if (bound_) {
531 str << "A label has been bound but it's not used."
532 << "\n# label: " << *block_;
533 } else {
534 str << "A label has been used but it's not bound.";
535 }
536 FATAL("%s", str.str().c_str());
537 #endif // DEBUG
538 }
539
540 } // namespace compiler
541 } // namespace internal
542 } // namespace v8
543