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