| OLD | NEW |
| (Empty) |
| 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/code-factory.h" | |
| 6 #include "src/compiler/pipeline.h" | |
| 7 #include "src/compiler/scheduler.h" | |
| 8 #include "test/unittests/compiler/raw-machine-assembler.h" | |
| 9 | |
| 10 namespace v8 { | |
| 11 namespace internal { | |
| 12 namespace compiler { | |
| 13 | |
| 14 RawMachineAssembler::RawMachineAssembler(Isolate* isolate, Graph* graph, | |
| 15 const MachineSignature* machine_sig, | |
| 16 MachineType word, | |
| 17 MachineOperatorBuilder::Flags flags) | |
| 18 : GraphBuilder(isolate, graph), | |
| 19 schedule_(new (zone()) Schedule(zone())), | |
| 20 machine_(zone(), word, flags), | |
| 21 common_(zone()), | |
| 22 machine_sig_(machine_sig), | |
| 23 call_descriptor_( | |
| 24 Linkage::GetSimplifiedCDescriptor(graph->zone(), machine_sig)), | |
| 25 parameters_(NULL), | |
| 26 current_block_(schedule()->start()) { | |
| 27 int param_count = static_cast<int>(parameter_count()); | |
| 28 Node* s = graph->NewNode(common_.Start(param_count)); | |
| 29 graph->SetStart(s); | |
| 30 if (parameter_count() == 0) return; | |
| 31 parameters_ = zone()->NewArray<Node*>(param_count); | |
| 32 for (size_t i = 0; i < parameter_count(); ++i) { | |
| 33 parameters_[i] = | |
| 34 NewNode(common()->Parameter(static_cast<int>(i)), graph->start()); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 | |
| 39 Schedule* RawMachineAssembler::Export() { | |
| 40 // Compute the correct codegen order. | |
| 41 DCHECK(schedule_->rpo_order()->empty()); | |
| 42 Scheduler::ComputeSpecialRPO(zone(), schedule_); | |
| 43 // Invalidate MachineAssembler. | |
| 44 Schedule* schedule = schedule_; | |
| 45 schedule_ = NULL; | |
| 46 return schedule; | |
| 47 } | |
| 48 | |
| 49 | |
| 50 Node* RawMachineAssembler::Parameter(size_t index) { | |
| 51 DCHECK(index < parameter_count()); | |
| 52 return parameters_[index]; | |
| 53 } | |
| 54 | |
| 55 | |
| 56 void RawMachineAssembler::Goto(Label* label) { | |
| 57 DCHECK(current_block_ != schedule()->end()); | |
| 58 schedule()->AddGoto(CurrentBlock(), Use(label)); | |
| 59 current_block_ = NULL; | |
| 60 } | |
| 61 | |
| 62 | |
| 63 void RawMachineAssembler::Branch(Node* condition, Label* true_val, | |
| 64 Label* false_val) { | |
| 65 DCHECK(current_block_ != schedule()->end()); | |
| 66 Node* branch = NewNode(common()->Branch(), condition); | |
| 67 schedule()->AddBranch(CurrentBlock(), branch, Use(true_val), Use(false_val)); | |
| 68 current_block_ = NULL; | |
| 69 } | |
| 70 | |
| 71 | |
| 72 void RawMachineAssembler::Switch(Node* index, Label* default_label, | |
| 73 int32_t* case_values, Label** case_labels, | |
| 74 size_t case_count) { | |
| 75 DCHECK_NE(schedule()->end(), current_block_); | |
| 76 size_t succ_count = case_count + 1; | |
| 77 Node* switch_node = NewNode(common()->Switch(succ_count), index); | |
| 78 BasicBlock** succ_blocks = zone()->NewArray<BasicBlock*>(succ_count); | |
| 79 for (size_t index = 0; index < case_count; ++index) { | |
| 80 int32_t case_value = case_values[index]; | |
| 81 BasicBlock* case_block = Use(case_labels[index]); | |
| 82 Node* case_node = | |
| 83 graph()->NewNode(common()->IfValue(case_value), switch_node); | |
| 84 schedule()->AddNode(case_block, case_node); | |
| 85 succ_blocks[index] = case_block; | |
| 86 } | |
| 87 BasicBlock* default_block = Use(default_label); | |
| 88 Node* default_node = graph()->NewNode(common()->IfDefault(), switch_node); | |
| 89 schedule()->AddNode(default_block, default_node); | |
| 90 succ_blocks[case_count] = default_block; | |
| 91 schedule()->AddSwitch(CurrentBlock(), switch_node, succ_blocks, succ_count); | |
| 92 current_block_ = nullptr; | |
| 93 } | |
| 94 | |
| 95 | |
| 96 void RawMachineAssembler::Return(Node* value) { | |
| 97 Node* ret = NewNode(common()->Return(), value); | |
| 98 schedule()->AddReturn(CurrentBlock(), ret); | |
| 99 current_block_ = NULL; | |
| 100 } | |
| 101 | |
| 102 | |
| 103 Node* RawMachineAssembler::CallFunctionStub0(Node* function, Node* receiver, | |
| 104 Node* context, Node* frame_state, | |
| 105 CallFunctionFlags flags) { | |
| 106 Callable callable = CodeFactory::CallFunction(isolate(), 0, flags); | |
| 107 CallDescriptor* desc = Linkage::GetStubCallDescriptor( | |
| 108 isolate(), zone(), callable.descriptor(), 1, | |
| 109 CallDescriptor::kNeedsFrameState, Operator::kNoProperties); | |
| 110 Node* stub_code = HeapConstant(callable.code()); | |
| 111 Node* call = graph()->NewNode(common()->Call(desc), stub_code, function, | |
| 112 receiver, context, frame_state); | |
| 113 schedule()->AddNode(CurrentBlock(), call); | |
| 114 return call; | |
| 115 } | |
| 116 | |
| 117 | |
| 118 Node* RawMachineAssembler::CallJS0(Node* function, Node* receiver, | |
| 119 Node* context, Node* frame_state) { | |
| 120 CallDescriptor* descriptor = Linkage::GetJSCallDescriptor( | |
| 121 zone(), false, 1, CallDescriptor::kNeedsFrameState); | |
| 122 Node* call = graph()->NewNode(common()->Call(descriptor), function, receiver, | |
| 123 context, frame_state); | |
| 124 schedule()->AddNode(CurrentBlock(), call); | |
| 125 return call; | |
| 126 } | |
| 127 | |
| 128 | |
| 129 Node* RawMachineAssembler::CallRuntime1(Runtime::FunctionId function, | |
| 130 Node* arg0, Node* context, | |
| 131 Node* frame_state) { | |
| 132 CallDescriptor* descriptor = Linkage::GetRuntimeCallDescriptor( | |
| 133 zone(), function, 1, Operator::kNoProperties); | |
| 134 | |
| 135 Node* centry = HeapConstant(CEntryStub(isolate(), 1).GetCode()); | |
| 136 Node* ref = NewNode( | |
| 137 common()->ExternalConstant(ExternalReference(function, isolate()))); | |
| 138 Node* arity = Int32Constant(1); | |
| 139 | |
| 140 Node* call = graph()->NewNode(common()->Call(descriptor), centry, arg0, ref, | |
| 141 arity, context, frame_state); | |
| 142 schedule()->AddNode(CurrentBlock(), call); | |
| 143 return call; | |
| 144 } | |
| 145 | |
| 146 | |
| 147 Node* RawMachineAssembler::CallCFunction0(MachineType return_type, | |
| 148 Node* function) { | |
| 149 MachineSignature::Builder builder(zone(), 1, 0); | |
| 150 builder.AddReturn(return_type); | |
| 151 const CallDescriptor* descriptor = | |
| 152 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build()); | |
| 153 | |
| 154 Node* call = graph()->NewNode(common()->Call(descriptor), function); | |
| 155 schedule()->AddNode(CurrentBlock(), call); | |
| 156 return call; | |
| 157 } | |
| 158 | |
| 159 | |
| 160 Node* RawMachineAssembler::CallCFunction1(MachineType return_type, | |
| 161 MachineType arg0_type, Node* function, | |
| 162 Node* arg0) { | |
| 163 MachineSignature::Builder builder(zone(), 1, 1); | |
| 164 builder.AddReturn(return_type); | |
| 165 builder.AddParam(arg0_type); | |
| 166 const CallDescriptor* descriptor = | |
| 167 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build()); | |
| 168 | |
| 169 Node* call = graph()->NewNode(common()->Call(descriptor), function, arg0); | |
| 170 schedule()->AddNode(CurrentBlock(), call); | |
| 171 return call; | |
| 172 } | |
| 173 | |
| 174 | |
| 175 Node* RawMachineAssembler::CallCFunction2(MachineType return_type, | |
| 176 MachineType arg0_type, | |
| 177 MachineType arg1_type, Node* function, | |
| 178 Node* arg0, Node* arg1) { | |
| 179 MachineSignature::Builder builder(zone(), 1, 2); | |
| 180 builder.AddReturn(return_type); | |
| 181 builder.AddParam(arg0_type); | |
| 182 builder.AddParam(arg1_type); | |
| 183 const CallDescriptor* descriptor = | |
| 184 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build()); | |
| 185 | |
| 186 Node* call = | |
| 187 graph()->NewNode(common()->Call(descriptor), function, arg0, arg1); | |
| 188 schedule()->AddNode(CurrentBlock(), call); | |
| 189 return call; | |
| 190 } | |
| 191 | |
| 192 | |
| 193 Node* RawMachineAssembler::CallCFunction8( | |
| 194 MachineType return_type, MachineType arg0_type, MachineType arg1_type, | |
| 195 MachineType arg2_type, MachineType arg3_type, MachineType arg4_type, | |
| 196 MachineType arg5_type, MachineType arg6_type, MachineType arg7_type, | |
| 197 Node* function, Node* arg0, Node* arg1, Node* arg2, Node* arg3, Node* arg4, | |
| 198 Node* arg5, Node* arg6, Node* arg7) { | |
| 199 MachineSignature::Builder builder(zone(), 1, 8); | |
| 200 builder.AddReturn(return_type); | |
| 201 builder.AddParam(arg0_type); | |
| 202 builder.AddParam(arg1_type); | |
| 203 builder.AddParam(arg2_type); | |
| 204 builder.AddParam(arg3_type); | |
| 205 builder.AddParam(arg4_type); | |
| 206 builder.AddParam(arg5_type); | |
| 207 builder.AddParam(arg6_type); | |
| 208 builder.AddParam(arg7_type); | |
| 209 const CallDescriptor* descriptor = | |
| 210 Linkage::GetSimplifiedCDescriptor(zone(), builder.Build()); | |
| 211 | |
| 212 Node* call = graph()->NewNode(common()->Call(descriptor), function, arg0, | |
| 213 arg1, arg2, arg3, arg4, arg5, arg6, arg7); | |
| 214 schedule()->AddNode(CurrentBlock(), call); | |
| 215 return call; | |
| 216 } | |
| 217 | |
| 218 | |
| 219 void RawMachineAssembler::Bind(Label* label) { | |
| 220 DCHECK(current_block_ == NULL); | |
| 221 DCHECK(!label->bound_); | |
| 222 label->bound_ = true; | |
| 223 current_block_ = EnsureBlock(label); | |
| 224 } | |
| 225 | |
| 226 | |
| 227 BasicBlock* RawMachineAssembler::Use(Label* label) { | |
| 228 label->used_ = true; | |
| 229 return EnsureBlock(label); | |
| 230 } | |
| 231 | |
| 232 | |
| 233 BasicBlock* RawMachineAssembler::EnsureBlock(Label* label) { | |
| 234 if (label->block_ == NULL) label->block_ = schedule()->NewBasicBlock(); | |
| 235 return label->block_; | |
| 236 } | |
| 237 | |
| 238 | |
| 239 BasicBlock* RawMachineAssembler::CurrentBlock() { | |
| 240 DCHECK(current_block_); | |
| 241 return current_block_; | |
| 242 } | |
| 243 | |
| 244 | |
| 245 Node* RawMachineAssembler::MakeNode(const Operator* op, int input_count, | |
| 246 Node** inputs, bool incomplete) { | |
| 247 DCHECK(ScheduleValid()); | |
| 248 DCHECK(current_block_ != NULL); | |
| 249 Node* node = graph()->NewNode(op, input_count, inputs, incomplete); | |
| 250 BasicBlock* block = op->opcode() == IrOpcode::kParameter ? schedule()->start() | |
| 251 : CurrentBlock(); | |
| 252 if (op->opcode() != IrOpcode::kReturn) { | |
| 253 schedule()->AddNode(block, node); | |
| 254 } | |
| 255 return node; | |
| 256 } | |
| 257 | |
| 258 } // namespace compiler | |
| 259 } // namespace internal | |
| 260 } // namespace v8 | |
| OLD | NEW |