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 <limits> |
| 6 |
| 7 #include "src/compiler/control-builders.h" |
| 8 #include "src/compiler/generic-node-inl.h" |
| 9 #include "src/compiler/node-properties-inl.h" |
| 10 #include "src/compiler/pipeline.h" |
| 11 #include "src/compiler/simplified-lowering.h" |
| 12 #include "src/compiler/simplified-node-factory.h" |
| 13 #include "src/compiler/typer.h" |
| 14 #include "src/compiler/verifier.h" |
| 15 #include "src/execution.h" |
| 16 #include "src/parser.h" |
| 17 #include "src/rewriter.h" |
| 18 #include "src/scopes.h" |
| 19 #include "test/cctest/cctest.h" |
| 20 #include "test/cctest/compiler/codegen-tester.h" |
| 21 #include "test/cctest/compiler/graph-builder-tester.h" |
| 22 #include "test/cctest/compiler/value-helper.h" |
| 23 |
| 24 using namespace v8::internal; |
| 25 using namespace v8::internal::compiler; |
| 26 |
| 27 template <typename ReturnType> |
| 28 class ChangesLoweringTester : public GraphBuilderTester<ReturnType> { |
| 29 public: |
| 30 explicit ChangesLoweringTester(MachineRepresentation p0 = kMachineLast) |
| 31 : GraphBuilderTester<ReturnType>(p0), |
| 32 typer(this->zone()), |
| 33 source_positions(this->graph()), |
| 34 jsgraph(this->graph(), this->common(), &typer), |
| 35 lowering(&jsgraph, &source_positions), |
| 36 function(Handle<JSFunction>::null()) {} |
| 37 |
| 38 Typer typer; |
| 39 SourcePositionTable source_positions; |
| 40 JSGraph jsgraph; |
| 41 SimplifiedLowering lowering; |
| 42 Handle<JSFunction> function; |
| 43 |
| 44 Node* start() { return this->graph()->start(); } |
| 45 |
| 46 template <typename T> |
| 47 T* CallWithPotentialGC() { |
| 48 // TODO(titzer): we need to wrap the code in a JSFunction and call it via |
| 49 // Execution::Call() so that the GC knows about the frame, can walk it, |
| 50 // relocate the code object if necessary, etc. |
| 51 // This is pretty ugly and at the least should be moved up to helpers. |
| 52 if (function.is_null()) { |
| 53 function = |
| 54 v8::Utils::OpenHandle(*v8::Handle<v8::Function>::Cast(CompileRun( |
| 55 "(function() { 'use strict'; return 2.7123; })"))); |
| 56 CompilationInfoWithZone info(function); |
| 57 CHECK(Parser::Parse(&info)); |
| 58 StrictMode strict_mode = info.function()->strict_mode(); |
| 59 info.SetStrictMode(strict_mode); |
| 60 info.SetOptimizing(BailoutId::None(), Handle<Code>(function->code())); |
| 61 CHECK(Rewriter::Rewrite(&info)); |
| 62 CHECK(Scope::Analyze(&info)); |
| 63 CHECK_NE(NULL, info.scope()); |
| 64 Pipeline pipeline(&info); |
| 65 Linkage linkage(&info); |
| 66 Handle<Code> code = |
| 67 pipeline.GenerateCodeForMachineGraph(&linkage, this->graph()); |
| 68 CHECK(!code.is_null()); |
| 69 function->ReplaceCode(*code); |
| 70 } |
| 71 Handle<Object>* args = NULL; |
| 72 MaybeHandle<Object> result = |
| 73 Execution::Call(this->isolate(), function, factory()->undefined_value(), |
| 74 0, args, false); |
| 75 return T::cast(*result.ToHandleChecked()); |
| 76 } |
| 77 |
| 78 void StoreFloat64(Node* node, double* ptr) { |
| 79 Node* ptr_node = this->PointerConstant(ptr); |
| 80 this->Store(kMachineFloat64, ptr_node, node); |
| 81 } |
| 82 |
| 83 Node* LoadInt32(int32_t* ptr) { |
| 84 Node* ptr_node = this->PointerConstant(ptr); |
| 85 return this->Load(kMachineWord32, ptr_node); |
| 86 } |
| 87 |
| 88 Node* LoadUint32(uint32_t* ptr) { |
| 89 Node* ptr_node = this->PointerConstant(ptr); |
| 90 return this->Load(kMachineWord32, ptr_node); |
| 91 } |
| 92 |
| 93 Node* LoadFloat64(double* ptr) { |
| 94 Node* ptr_node = this->PointerConstant(ptr); |
| 95 return this->Load(kMachineFloat64, ptr_node); |
| 96 } |
| 97 |
| 98 void CheckNumber(double expected, Object* number) { |
| 99 CHECK(this->isolate()->factory()->NewNumber(expected)->SameValue(number)); |
| 100 } |
| 101 |
| 102 void BuildAndLower(Operator* op) { |
| 103 // We build a graph by hand here, because the raw machine assembler |
| 104 // does not add the correct control and effect nodes. |
| 105 Node* p0 = this->Parameter(0); |
| 106 Node* change = this->graph()->NewNode(op, p0); |
| 107 Node* ret = this->graph()->NewNode(this->common()->Return(), change, |
| 108 this->start(), this->start()); |
| 109 Node* end = this->graph()->NewNode(this->common()->End(), ret); |
| 110 this->graph()->SetEnd(end); |
| 111 this->lowering.Lower(change); |
| 112 Verifier::Run(this->graph()); |
| 113 } |
| 114 |
| 115 void BuildStoreAndLower(Operator* op, Operator* store_op, void* location) { |
| 116 // We build a graph by hand here, because the raw machine assembler |
| 117 // does not add the correct control and effect nodes. |
| 118 Node* p0 = this->Parameter(0); |
| 119 Node* change = this->graph()->NewNode(op, p0); |
| 120 Node* store = this->graph()->NewNode( |
| 121 store_op, this->PointerConstant(location), this->Int32Constant(0), |
| 122 change, this->start(), this->start()); |
| 123 Node* ret = this->graph()->NewNode( |
| 124 this->common()->Return(), this->Int32Constant(0), store, this->start()); |
| 125 Node* end = this->graph()->NewNode(this->common()->End(), ret); |
| 126 this->graph()->SetEnd(end); |
| 127 this->lowering.Lower(change); |
| 128 Verifier::Run(this->graph()); |
| 129 } |
| 130 |
| 131 void BuildLoadAndLower(Operator* op, Operator* load_op, void* location) { |
| 132 // We build a graph by hand here, because the raw machine assembler |
| 133 // does not add the correct control and effect nodes. |
| 134 Node* load = |
| 135 this->graph()->NewNode(load_op, this->PointerConstant(location), |
| 136 this->Int32Constant(0), this->start()); |
| 137 Node* change = this->graph()->NewNode(op, load); |
| 138 Node* ret = this->graph()->NewNode(this->common()->Return(), change, |
| 139 this->start(), this->start()); |
| 140 Node* end = this->graph()->NewNode(this->common()->End(), ret); |
| 141 this->graph()->SetEnd(end); |
| 142 this->lowering.Lower(change); |
| 143 Verifier::Run(this->graph()); |
| 144 } |
| 145 |
| 146 Factory* factory() { return this->isolate()->factory(); } |
| 147 Heap* heap() { return this->isolate()->heap(); } |
| 148 }; |
| 149 |
| 150 |
| 151 TEST(RunChangeTaggedToInt32) { |
| 152 // Build and lower a graph by hand. |
| 153 ChangesLoweringTester<int32_t> t(kMachineTagged); |
| 154 t.BuildAndLower(t.simplified()->ChangeTaggedToInt32()); |
| 155 |
| 156 if (Pipeline::SupportedTarget()) { |
| 157 FOR_INT32_INPUTS(i) { |
| 158 int32_t input = *i; |
| 159 |
| 160 if (Smi::IsValid(input)) { |
| 161 int32_t result = t.Call(Smi::FromInt(input)); |
| 162 CHECK_EQ(input, result); |
| 163 } |
| 164 |
| 165 { |
| 166 Handle<Object> number = t.factory()->NewNumber(input); |
| 167 int32_t result = t.Call(*number); |
| 168 CHECK_EQ(input, result); |
| 169 } |
| 170 |
| 171 { |
| 172 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input); |
| 173 int32_t result = t.Call(*number); |
| 174 CHECK_EQ(input, result); |
| 175 } |
| 176 } |
| 177 } |
| 178 } |
| 179 |
| 180 |
| 181 TEST(RunChangeTaggedToUint32) { |
| 182 // Build and lower a graph by hand. |
| 183 ChangesLoweringTester<uint32_t> t(kMachineTagged); |
| 184 t.BuildAndLower(t.simplified()->ChangeTaggedToUint32()); |
| 185 |
| 186 if (Pipeline::SupportedTarget()) { |
| 187 FOR_UINT32_INPUTS(i) { |
| 188 uint32_t input = *i; |
| 189 |
| 190 if (Smi::IsValid(input)) { |
| 191 uint32_t result = t.Call(Smi::FromInt(input)); |
| 192 CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result)); |
| 193 } |
| 194 |
| 195 { |
| 196 Handle<Object> number = t.factory()->NewNumber(input); |
| 197 uint32_t result = t.Call(*number); |
| 198 CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result)); |
| 199 } |
| 200 |
| 201 { |
| 202 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input); |
| 203 uint32_t result = t.Call(*number); |
| 204 CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result)); |
| 205 } |
| 206 } |
| 207 } |
| 208 } |
| 209 |
| 210 |
| 211 TEST(RunChangeTaggedToFloat64) { |
| 212 ChangesLoweringTester<int32_t> t(kMachineTagged); |
| 213 double result; |
| 214 |
| 215 t.BuildStoreAndLower(t.simplified()->ChangeTaggedToFloat64(), |
| 216 t.machine()->Store(kMachineFloat64), &result); |
| 217 |
| 218 if (Pipeline::SupportedTarget()) { |
| 219 FOR_INT32_INPUTS(i) { |
| 220 int32_t input = *i; |
| 221 |
| 222 if (Smi::IsValid(input)) { |
| 223 t.Call(Smi::FromInt(input)); |
| 224 CHECK_EQ(input, static_cast<int32_t>(result)); |
| 225 } |
| 226 |
| 227 { |
| 228 Handle<Object> number = t.factory()->NewNumber(input); |
| 229 t.Call(*number); |
| 230 CHECK_EQ(input, static_cast<int32_t>(result)); |
| 231 } |
| 232 |
| 233 { |
| 234 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input); |
| 235 t.Call(*number); |
| 236 CHECK_EQ(input, static_cast<int32_t>(result)); |
| 237 } |
| 238 } |
| 239 } |
| 240 |
| 241 if (Pipeline::SupportedTarget()) { |
| 242 FOR_FLOAT64_INPUTS(i) { |
| 243 double input = *i; |
| 244 { |
| 245 Handle<Object> number = t.factory()->NewNumber(input); |
| 246 t.Call(*number); |
| 247 CHECK_EQ(input, result); |
| 248 } |
| 249 |
| 250 { |
| 251 Handle<HeapNumber> number = t.factory()->NewHeapNumber(input); |
| 252 t.Call(*number); |
| 253 CHECK_EQ(input, result); |
| 254 } |
| 255 } |
| 256 } |
| 257 } |
| 258 |
| 259 |
| 260 TEST(RunChangeBoolToBit) { |
| 261 ChangesLoweringTester<int32_t> t(kMachineTagged); |
| 262 t.BuildAndLower(t.simplified()->ChangeBoolToBit()); |
| 263 |
| 264 if (Pipeline::SupportedTarget()) { |
| 265 Object* true_obj = t.heap()->true_value(); |
| 266 int32_t result = t.Call(true_obj); |
| 267 CHECK_EQ(1, result); |
| 268 } |
| 269 |
| 270 if (Pipeline::SupportedTarget()) { |
| 271 Object* false_obj = t.heap()->false_value(); |
| 272 int32_t result = t.Call(false_obj); |
| 273 CHECK_EQ(0, result); |
| 274 } |
| 275 } |
| 276 |
| 277 |
| 278 TEST(RunChangeBitToBool) { |
| 279 ChangesLoweringTester<Object*> t(kMachineWord32); |
| 280 t.BuildAndLower(t.simplified()->ChangeBitToBool()); |
| 281 |
| 282 if (Pipeline::SupportedTarget()) { |
| 283 Object* result = t.Call(1); |
| 284 Object* true_obj = t.heap()->true_value(); |
| 285 CHECK_EQ(true_obj, result); |
| 286 } |
| 287 |
| 288 if (Pipeline::SupportedTarget()) { |
| 289 Object* result = t.Call(0); |
| 290 Object* false_obj = t.heap()->false_value(); |
| 291 CHECK_EQ(false_obj, result); |
| 292 } |
| 293 } |
| 294 |
| 295 |
| 296 // TODO(titzer): enable all UI32 -> Tagged checking when inline allocation |
| 297 // works. |
| 298 #define TODO_UI32_TO_TAGGED_WILL_WORK(v) Smi::IsValid(static_cast<double>(v)) |
| 299 |
| 300 TEST(RunChangeInt32ToTagged) { |
| 301 ChangesLoweringTester<Object*> t; |
| 302 int32_t input; |
| 303 t.BuildLoadAndLower(t.simplified()->ChangeInt32ToTagged(), |
| 304 t.machine()->Load(kMachineWord32), &input); |
| 305 |
| 306 if (Pipeline::SupportedTarget()) { |
| 307 FOR_INT32_INPUTS(i) { |
| 308 input = *i; |
| 309 Object* result = t.CallWithPotentialGC<Object>(); |
| 310 if (TODO_UI32_TO_TAGGED_WILL_WORK(input)) { |
| 311 t.CheckNumber(static_cast<double>(input), result); |
| 312 } |
| 313 } |
| 314 } |
| 315 |
| 316 if (Pipeline::SupportedTarget()) { |
| 317 FOR_INT32_INPUTS(i) { |
| 318 input = *i; |
| 319 SimulateFullSpace(CcTest::heap()->new_space()); |
| 320 Object* result = t.CallWithPotentialGC<Object>(); |
| 321 if (TODO_UI32_TO_TAGGED_WILL_WORK(input)) { |
| 322 t.CheckNumber(static_cast<double>(input), result); |
| 323 } |
| 324 } |
| 325 } |
| 326 } |
| 327 |
| 328 |
| 329 TEST(RunChangeUint32ToTagged) { |
| 330 ChangesLoweringTester<Object*> t; |
| 331 uint32_t input; |
| 332 t.BuildLoadAndLower(t.simplified()->ChangeUint32ToTagged(), |
| 333 t.machine()->Load(kMachineWord32), &input); |
| 334 |
| 335 if (Pipeline::SupportedTarget()) { |
| 336 FOR_UINT32_INPUTS(i) { |
| 337 input = *i; |
| 338 Object* result = t.CallWithPotentialGC<Object>(); |
| 339 double expected = static_cast<double>(input); |
| 340 if (TODO_UI32_TO_TAGGED_WILL_WORK(input)) { |
| 341 t.CheckNumber(expected, result); |
| 342 } |
| 343 } |
| 344 } |
| 345 |
| 346 if (Pipeline::SupportedTarget()) { |
| 347 FOR_UINT32_INPUTS(i) { |
| 348 input = *i; |
| 349 SimulateFullSpace(CcTest::heap()->new_space()); |
| 350 Object* result = t.CallWithPotentialGC<Object>(); |
| 351 double expected = static_cast<double>(static_cast<uint32_t>(input)); |
| 352 if (TODO_UI32_TO_TAGGED_WILL_WORK(input)) { |
| 353 t.CheckNumber(expected, result); |
| 354 } |
| 355 } |
| 356 } |
| 357 } |
| 358 |
| 359 |
| 360 // TODO(titzer): lowering of Float64->Tagged needs inline allocation. |
| 361 #define TODO_FLOAT64_TO_TAGGED false |
| 362 |
| 363 TEST(RunChangeFloat64ToTagged) { |
| 364 ChangesLoweringTester<Object*> t; |
| 365 double input; |
| 366 t.BuildLoadAndLower(t.simplified()->ChangeFloat64ToTagged(), |
| 367 t.machine()->Load(kMachineFloat64), &input); |
| 368 |
| 369 // TODO(titzer): need inline allocation to change float to tagged. |
| 370 if (TODO_FLOAT64_TO_TAGGED && Pipeline::SupportedTarget()) { |
| 371 FOR_FLOAT64_INPUTS(i) { |
| 372 input = *i; |
| 373 Object* result = t.CallWithPotentialGC<Object>(); |
| 374 t.CheckNumber(input, result); |
| 375 } |
| 376 } |
| 377 |
| 378 if (TODO_FLOAT64_TO_TAGGED && Pipeline::SupportedTarget()) { |
| 379 FOR_FLOAT64_INPUTS(i) { |
| 380 input = *i; |
| 381 SimulateFullSpace(CcTest::heap()->new_space()); |
| 382 Object* result = t.CallWithPotentialGC<Object>(); |
| 383 t.CheckNumber(input, result); |
| 384 } |
| 385 } |
| 386 } |
OLD | NEW |