| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "vm/globals.h" | |
| 9 #include "vm/assembler.h" | 8 #include "vm/assembler.h" |
| 10 #include "vm/ast.h" | 9 #include "vm/ast.h" |
| 11 #include "vm/class_finalizer.h" | 10 #include "vm/class_finalizer.h" |
| 12 #include "vm/compiler.h" | 11 #include "vm/compiler.h" |
| 13 #include "vm/dart_entry.h" | 12 #include "vm/dart_entry.h" |
| 13 #include "vm/globals.h" |
| 14 #include "vm/native_entry.h" | 14 #include "vm/native_entry.h" |
| 15 #include "vm/native_entry_test.h" | 15 #include "vm/native_entry_test.h" |
| 16 #include "vm/runtime_entry.h" | 16 #include "vm/runtime_entry.h" |
| 17 #include "vm/symbols.h" | 17 #include "vm/symbols.h" |
| 18 #include "vm/unit_test.h" | 18 #include "vm/unit_test.h" |
| 19 #include "vm/virtual_memory.h" | 19 #include "vm/virtual_memory.h" |
| 20 | 20 |
| 21 namespace dart { | 21 namespace dart { |
| 22 | 22 |
| 23 static const TokenPosition kPos = TokenPosition::kMinSource; | 23 static const TokenPosition kPos = TokenPosition::kMinSource; |
| 24 | 24 |
| 25 | |
| 26 CODEGEN_TEST_GENERATE(SimpleReturnCodegen, test) { | 25 CODEGEN_TEST_GENERATE(SimpleReturnCodegen, test) { |
| 27 test->node_sequence()->Add(new ReturnNode(kPos)); | 26 test->node_sequence()->Add(new ReturnNode(kPos)); |
| 28 } | 27 } |
| 29 CODEGEN_TEST_RUN(SimpleReturnCodegen, Instance::null()) | 28 CODEGEN_TEST_RUN(SimpleReturnCodegen, Instance::null()) |
| 30 | 29 |
| 31 | |
| 32 CODEGEN_TEST_GENERATE(SmiReturnCodegen, test) { | 30 CODEGEN_TEST_GENERATE(SmiReturnCodegen, test) { |
| 33 LiteralNode* l = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); | 31 LiteralNode* l = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); |
| 34 test->node_sequence()->Add(new ReturnNode(kPos, l)); | 32 test->node_sequence()->Add(new ReturnNode(kPos, l)); |
| 35 } | 33 } |
| 36 CODEGEN_TEST_RUN(SmiReturnCodegen, Smi::New(3)) | 34 CODEGEN_TEST_RUN(SmiReturnCodegen, Smi::New(3)) |
| 37 | 35 |
| 38 | |
| 39 CODEGEN_TEST2_GENERATE(SimpleStaticCallCodegen, function, test) { | 36 CODEGEN_TEST2_GENERATE(SimpleStaticCallCodegen, function, test) { |
| 40 // Wrap the SmiReturnCodegen test above as a static function and call it. | 37 // Wrap the SmiReturnCodegen test above as a static function and call it. |
| 41 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); | 38 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
| 42 test->node_sequence()->Add( | 39 test->node_sequence()->Add( |
| 43 new ReturnNode(kPos, new StaticCallNode(kPos, function, no_arguments))); | 40 new ReturnNode(kPos, new StaticCallNode(kPos, function, no_arguments))); |
| 44 } | 41 } |
| 45 CODEGEN_TEST2_RUN(SimpleStaticCallCodegen, SmiReturnCodegen, Smi::New(3)) | 42 CODEGEN_TEST2_RUN(SimpleStaticCallCodegen, SmiReturnCodegen, Smi::New(3)) |
| 46 | 43 |
| 47 | |
| 48 // Helper to allocate and return a LocalVariable. | 44 // Helper to allocate and return a LocalVariable. |
| 49 static LocalVariable* NewTestLocalVariable(const char* name) { | 45 static LocalVariable* NewTestLocalVariable(const char* name) { |
| 50 const String& variable_name = | 46 const String& variable_name = |
| 51 String::ZoneHandle(Symbols::New(Thread::Current(), name)); | 47 String::ZoneHandle(Symbols::New(Thread::Current(), name)); |
| 52 const Type& variable_type = Type::ZoneHandle(Type::DynamicType()); | 48 const Type& variable_type = Type::ZoneHandle(Type::DynamicType()); |
| 53 return new LocalVariable(kPos, kPos, variable_name, variable_type); | 49 return new LocalVariable(kPos, kPos, variable_name, variable_type); |
| 54 } | 50 } |
| 55 | 51 |
| 56 | |
| 57 CODEGEN_TEST_GENERATE(ReturnParameterCodegen, test) { | 52 CODEGEN_TEST_GENERATE(ReturnParameterCodegen, test) { |
| 58 SequenceNode* node_seq = test->node_sequence(); | 53 SequenceNode* node_seq = test->node_sequence(); |
| 59 const int num_params = 1; | 54 const int num_params = 1; |
| 60 LocalVariable* parameter = NewTestLocalVariable("parameter"); | 55 LocalVariable* parameter = NewTestLocalVariable("parameter"); |
| 61 LocalScope* local_scope = node_seq->scope(); | 56 LocalScope* local_scope = node_seq->scope(); |
| 62 local_scope->InsertParameterAt(0, parameter); | 57 local_scope->InsertParameterAt(0, parameter); |
| 63 ASSERT(local_scope->num_variables() == num_params); | 58 ASSERT(local_scope->num_variables() == num_params); |
| 64 const Function& function = test->function(); | 59 const Function& function = test->function(); |
| 65 function.set_num_fixed_parameters(num_params); | 60 function.set_num_fixed_parameters(num_params); |
| 66 ASSERT(!function.HasOptionalParameters()); | 61 ASSERT(!function.HasOptionalParameters()); |
| 67 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, parameter))); | 62 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, parameter))); |
| 68 } | 63 } |
| 69 | 64 |
| 70 | |
| 71 CODEGEN_TEST2_GENERATE(StaticCallReturnParameterCodegen, function, test) { | 65 CODEGEN_TEST2_GENERATE(StaticCallReturnParameterCodegen, function, test) { |
| 72 // Wrap and call the ReturnParameterCodegen test above as a static function. | 66 // Wrap and call the ReturnParameterCodegen test above as a static function. |
| 73 SequenceNode* node_seq = test->node_sequence(); | 67 SequenceNode* node_seq = test->node_sequence(); |
| 74 ArgumentListNode* arguments = new ArgumentListNode(kPos); | 68 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 75 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); | 69 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
| 76 node_seq->Add( | 70 node_seq->Add( |
| 77 new ReturnNode(kPos, new StaticCallNode(kPos, function, arguments))); | 71 new ReturnNode(kPos, new StaticCallNode(kPos, function, arguments))); |
| 78 } | 72 } |
| 79 CODEGEN_TEST2_RUN(StaticCallReturnParameterCodegen, | 73 CODEGEN_TEST2_RUN(StaticCallReturnParameterCodegen, |
| 80 ReturnParameterCodegen, | 74 ReturnParameterCodegen, |
| 81 Smi::New(3)) | 75 Smi::New(3)) |
| 82 | 76 |
| 83 | |
| 84 CODEGEN_TEST_GENERATE(SmiParamSumCodegen, test) { | 77 CODEGEN_TEST_GENERATE(SmiParamSumCodegen, test) { |
| 85 SequenceNode* node_seq = test->node_sequence(); | 78 SequenceNode* node_seq = test->node_sequence(); |
| 86 const int num_params = 2; | 79 const int num_params = 2; |
| 87 LocalVariable* param1 = NewTestLocalVariable("param1"); | 80 LocalVariable* param1 = NewTestLocalVariable("param1"); |
| 88 LocalVariable* param2 = NewTestLocalVariable("param2"); | 81 LocalVariable* param2 = NewTestLocalVariable("param2"); |
| 89 const int num_locals = 1; | 82 const int num_locals = 1; |
| 90 LocalVariable* sum = NewTestLocalVariable("sum"); | 83 LocalVariable* sum = NewTestLocalVariable("sum"); |
| 91 LocalScope* local_scope = node_seq->scope(); | 84 LocalScope* local_scope = node_seq->scope(); |
| 92 local_scope->InsertParameterAt(0, param1); | 85 local_scope->InsertParameterAt(0, param1); |
| 93 local_scope->InsertParameterAt(1, param2); | 86 local_scope->InsertParameterAt(1, param2); |
| 94 local_scope->AddVariable(sum); | 87 local_scope->AddVariable(sum); |
| 95 ASSERT(local_scope->num_variables() == num_params + num_locals); | 88 ASSERT(local_scope->num_variables() == num_params + num_locals); |
| 96 const Function& function = test->function(); | 89 const Function& function = test->function(); |
| 97 function.set_num_fixed_parameters(num_params); | 90 function.set_num_fixed_parameters(num_params); |
| 98 ASSERT(!function.HasOptionalParameters()); | 91 ASSERT(!function.HasOptionalParameters()); |
| 99 BinaryOpNode* add = | 92 BinaryOpNode* add = |
| 100 new BinaryOpNode(kPos, Token::kADD, new LoadLocalNode(kPos, param1), | 93 new BinaryOpNode(kPos, Token::kADD, new LoadLocalNode(kPos, param1), |
| 101 new LoadLocalNode(kPos, param2)); | 94 new LoadLocalNode(kPos, param2)); |
| 102 node_seq->Add(new StoreLocalNode(kPos, sum, add)); | 95 node_seq->Add(new StoreLocalNode(kPos, sum, add)); |
| 103 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, sum))); | 96 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, sum))); |
| 104 } | 97 } |
| 105 | 98 |
| 106 | |
| 107 CODEGEN_TEST2_GENERATE(StaticCallSmiParamSumCodegen, function, test) { | 99 CODEGEN_TEST2_GENERATE(StaticCallSmiParamSumCodegen, function, test) { |
| 108 // Wrap and call the SmiParamSumCodegen test above as a static function. | 100 // Wrap and call the SmiParamSumCodegen test above as a static function. |
| 109 SequenceNode* node_seq = test->node_sequence(); | 101 SequenceNode* node_seq = test->node_sequence(); |
| 110 ArgumentListNode* arguments = new ArgumentListNode(kPos); | 102 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 111 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); | 103 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
| 112 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2)))); | 104 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2)))); |
| 113 node_seq->Add( | 105 node_seq->Add( |
| 114 new ReturnNode(kPos, new StaticCallNode(kPos, function, arguments))); | 106 new ReturnNode(kPos, new StaticCallNode(kPos, function, arguments))); |
| 115 } | 107 } |
| 116 CODEGEN_TEST2_RUN(StaticCallSmiParamSumCodegen, SmiParamSumCodegen, Smi::New(5)) | 108 CODEGEN_TEST2_RUN(StaticCallSmiParamSumCodegen, SmiParamSumCodegen, Smi::New(5)) |
| 117 | 109 |
| 118 | |
| 119 CODEGEN_TEST_GENERATE(SmiAddCodegen, test) { | 110 CODEGEN_TEST_GENERATE(SmiAddCodegen, test) { |
| 120 SequenceNode* node_seq = test->node_sequence(); | 111 SequenceNode* node_seq = test->node_sequence(); |
| 121 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); | 112 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); |
| 122 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); | 113 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 123 BinaryOpNode* add_node = new BinaryOpNode(kPos, Token::kADD, a, b); | 114 BinaryOpNode* add_node = new BinaryOpNode(kPos, Token::kADD, a, b); |
| 124 node_seq->Add(new ReturnNode(kPos, add_node)); | 115 node_seq->Add(new ReturnNode(kPos, add_node)); |
| 125 } | 116 } |
| 126 CODEGEN_TEST_RUN(SmiAddCodegen, Smi::New(5)) | 117 CODEGEN_TEST_RUN(SmiAddCodegen, Smi::New(5)) |
| 127 | 118 |
| 128 | |
| 129 CODEGEN_TEST_GENERATE(GenericAddCodegen, test) { | 119 CODEGEN_TEST_GENERATE(GenericAddCodegen, test) { |
| 130 SequenceNode* node_seq = test->node_sequence(); | 120 SequenceNode* node_seq = test->node_sequence(); |
| 131 LiteralNode* a = | 121 LiteralNode* a = |
| 132 new LiteralNode(kPos, Double::ZoneHandle(Double::New(12.2, Heap::kOld))); | 122 new LiteralNode(kPos, Double::ZoneHandle(Double::New(12.2, Heap::kOld))); |
| 133 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); | 123 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 134 BinaryOpNode* add_node_1 = new BinaryOpNode(kPos, Token::kADD, a, b); | 124 BinaryOpNode* add_node_1 = new BinaryOpNode(kPos, Token::kADD, a, b); |
| 135 LiteralNode* c = | 125 LiteralNode* c = |
| 136 new LiteralNode(kPos, Double::ZoneHandle(Double::New(0.8, Heap::kOld))); | 126 new LiteralNode(kPos, Double::ZoneHandle(Double::New(0.8, Heap::kOld))); |
| 137 BinaryOpNode* add_node_2 = new BinaryOpNode(kPos, Token::kADD, add_node_1, c); | 127 BinaryOpNode* add_node_2 = new BinaryOpNode(kPos, Token::kADD, add_node_1, c); |
| 138 node_seq->Add(new ReturnNode(kPos, add_node_2)); | 128 node_seq->Add(new ReturnNode(kPos, add_node_2)); |
| 139 } | 129 } |
| 140 CODEGEN_TEST_RUN(GenericAddCodegen, Double::New(15.0)) | 130 CODEGEN_TEST_RUN(GenericAddCodegen, Double::New(15.0)) |
| 141 | 131 |
| 142 | |
| 143 CODEGEN_TEST_GENERATE(SmiBinaryOpCodegen, test) { | 132 CODEGEN_TEST_GENERATE(SmiBinaryOpCodegen, test) { |
| 144 SequenceNode* node_seq = test->node_sequence(); | 133 SequenceNode* node_seq = test->node_sequence(); |
| 145 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(4))); | 134 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(4))); |
| 146 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); | 135 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 147 LiteralNode* c = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); | 136 LiteralNode* c = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); |
| 148 BinaryOpNode* sub_node = | 137 BinaryOpNode* sub_node = |
| 149 new BinaryOpNode(kPos, Token::kSUB, a, b); // 4 - 2 -> 2. | 138 new BinaryOpNode(kPos, Token::kSUB, a, b); // 4 - 2 -> 2. |
| 150 BinaryOpNode* mul_node = | 139 BinaryOpNode* mul_node = |
| 151 new BinaryOpNode(kPos, Token::kMUL, sub_node, c); // 2 * 3 -> 6. | 140 new BinaryOpNode(kPos, Token::kMUL, sub_node, c); // 2 * 3 -> 6. |
| 152 BinaryOpNode* div_node = | 141 BinaryOpNode* div_node = |
| 153 new BinaryOpNode(kPos, Token::kTRUNCDIV, mul_node, b); // 6 ~/ 2 -> 3. | 142 new BinaryOpNode(kPos, Token::kTRUNCDIV, mul_node, b); // 6 ~/ 2 -> 3. |
| 154 node_seq->Add(new ReturnNode(kPos, div_node)); | 143 node_seq->Add(new ReturnNode(kPos, div_node)); |
| 155 } | 144 } |
| 156 CODEGEN_TEST_RUN(SmiBinaryOpCodegen, Smi::New(3)) | 145 CODEGEN_TEST_RUN(SmiBinaryOpCodegen, Smi::New(3)) |
| 157 | 146 |
| 158 | |
| 159 CODEGEN_TEST_GENERATE(BoolNotCodegen, test) { | 147 CODEGEN_TEST_GENERATE(BoolNotCodegen, test) { |
| 160 SequenceNode* node_seq = test->node_sequence(); | 148 SequenceNode* node_seq = test->node_sequence(); |
| 161 LiteralNode* b = new LiteralNode(kPos, Bool::False()); | 149 LiteralNode* b = new LiteralNode(kPos, Bool::False()); |
| 162 UnaryOpNode* not_node = new UnaryOpNode(kPos, Token::kNOT, b); | 150 UnaryOpNode* not_node = new UnaryOpNode(kPos, Token::kNOT, b); |
| 163 node_seq->Add(new ReturnNode(kPos, not_node)); | 151 node_seq->Add(new ReturnNode(kPos, not_node)); |
| 164 } | 152 } |
| 165 CODEGEN_TEST_RUN(BoolNotCodegen, Bool::True().raw()) | 153 CODEGEN_TEST_RUN(BoolNotCodegen, Bool::True().raw()) |
| 166 | 154 |
| 167 | |
| 168 CODEGEN_TEST_GENERATE(BoolAndCodegen, test) { | 155 CODEGEN_TEST_GENERATE(BoolAndCodegen, test) { |
| 169 SequenceNode* node_seq = test->node_sequence(); | 156 SequenceNode* node_seq = test->node_sequence(); |
| 170 LiteralNode* a = new LiteralNode(kPos, Bool::True()); | 157 LiteralNode* a = new LiteralNode(kPos, Bool::True()); |
| 171 LiteralNode* b = new LiteralNode(kPos, Bool::False()); | 158 LiteralNode* b = new LiteralNode(kPos, Bool::False()); |
| 172 BinaryOpNode* and_node = new BinaryOpNode(kPos, Token::kAND, a, b); | 159 BinaryOpNode* and_node = new BinaryOpNode(kPos, Token::kAND, a, b); |
| 173 node_seq->Add(new ReturnNode(kPos, and_node)); | 160 node_seq->Add(new ReturnNode(kPos, and_node)); |
| 174 } | 161 } |
| 175 CODEGEN_TEST_RUN(BoolAndCodegen, Bool::False().raw()) | 162 CODEGEN_TEST_RUN(BoolAndCodegen, Bool::False().raw()) |
| 176 | 163 |
| 177 | |
| 178 CODEGEN_TEST_GENERATE(BinaryOpCodegen, test) { | 164 CODEGEN_TEST_GENERATE(BinaryOpCodegen, test) { |
| 179 SequenceNode* node_seq = test->node_sequence(); | 165 SequenceNode* node_seq = test->node_sequence(); |
| 180 LiteralNode* a = | 166 LiteralNode* a = |
| 181 new LiteralNode(kPos, Double::ZoneHandle(Double::New(12, Heap::kOld))); | 167 new LiteralNode(kPos, Double::ZoneHandle(Double::New(12, Heap::kOld))); |
| 182 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); | 168 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 183 LiteralNode* c = | 169 LiteralNode* c = |
| 184 new LiteralNode(kPos, Double::ZoneHandle(Double::New(0.5, Heap::kOld))); | 170 new LiteralNode(kPos, Double::ZoneHandle(Double::New(0.5, Heap::kOld))); |
| 185 BinaryOpNode* sub_node = new BinaryOpNode(kPos, Token::kSUB, a, b); | 171 BinaryOpNode* sub_node = new BinaryOpNode(kPos, Token::kSUB, a, b); |
| 186 BinaryOpNode* mul_node = new BinaryOpNode(kPos, Token::kMUL, sub_node, c); | 172 BinaryOpNode* mul_node = new BinaryOpNode(kPos, Token::kMUL, sub_node, c); |
| 187 BinaryOpNode* div_node = new BinaryOpNode(kPos, Token::kDIV, mul_node, b); | 173 BinaryOpNode* div_node = new BinaryOpNode(kPos, Token::kDIV, mul_node, b); |
| 188 node_seq->Add(new ReturnNode(kPos, div_node)); | 174 node_seq->Add(new ReturnNode(kPos, div_node)); |
| 189 } | 175 } |
| 190 CODEGEN_TEST_RUN(BinaryOpCodegen, Double::New(2.5)); | 176 CODEGEN_TEST_RUN(BinaryOpCodegen, Double::New(2.5)); |
| 191 | 177 |
| 192 | |
| 193 CODEGEN_TEST_GENERATE(SmiUnaryOpCodegen, test) { | 178 CODEGEN_TEST_GENERATE(SmiUnaryOpCodegen, test) { |
| 194 SequenceNode* node_seq = test->node_sequence(); | 179 SequenceNode* node_seq = test->node_sequence(); |
| 195 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(12))); | 180 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(12))); |
| 196 UnaryOpNode* neg_node = new UnaryOpNode(kPos, Token::kNEGATE, a); | 181 UnaryOpNode* neg_node = new UnaryOpNode(kPos, Token::kNEGATE, a); |
| 197 node_seq->Add(new ReturnNode(kPos, neg_node)); | 182 node_seq->Add(new ReturnNode(kPos, neg_node)); |
| 198 } | 183 } |
| 199 CODEGEN_TEST_RUN(SmiUnaryOpCodegen, Smi::New(-12)) | 184 CODEGEN_TEST_RUN(SmiUnaryOpCodegen, Smi::New(-12)) |
| 200 | 185 |
| 201 | |
| 202 CODEGEN_TEST_GENERATE(DoubleUnaryOpCodegen, test) { | 186 CODEGEN_TEST_GENERATE(DoubleUnaryOpCodegen, test) { |
| 203 SequenceNode* node_seq = test->node_sequence(); | 187 SequenceNode* node_seq = test->node_sequence(); |
| 204 LiteralNode* a = | 188 LiteralNode* a = |
| 205 new LiteralNode(kPos, Double::ZoneHandle(Double::New(12.0, Heap::kOld))); | 189 new LiteralNode(kPos, Double::ZoneHandle(Double::New(12.0, Heap::kOld))); |
| 206 UnaryOpNode* neg_node = new UnaryOpNode(kPos, Token::kNEGATE, a); | 190 UnaryOpNode* neg_node = new UnaryOpNode(kPos, Token::kNEGATE, a); |
| 207 node_seq->Add(new ReturnNode(kPos, neg_node)); | 191 node_seq->Add(new ReturnNode(kPos, neg_node)); |
| 208 } | 192 } |
| 209 CODEGEN_TEST_RUN(DoubleUnaryOpCodegen, Double::New(-12.0)) | 193 CODEGEN_TEST_RUN(DoubleUnaryOpCodegen, Double::New(-12.0)) |
| 210 | 194 |
| 211 | |
| 212 static Library& MakeTestLibrary(const char* url) { | 195 static Library& MakeTestLibrary(const char* url) { |
| 213 Thread* thread = Thread::Current(); | 196 Thread* thread = Thread::Current(); |
| 214 Zone* zone = thread->zone(); | 197 Zone* zone = thread->zone(); |
| 215 | 198 |
| 216 const String& lib_url = String::ZoneHandle(zone, Symbols::New(thread, url)); | 199 const String& lib_url = String::ZoneHandle(zone, Symbols::New(thread, url)); |
| 217 Library& lib = Library::ZoneHandle(zone, Library::New(lib_url)); | 200 Library& lib = Library::ZoneHandle(zone, Library::New(lib_url)); |
| 218 lib.Register(thread); | 201 lib.Register(thread); |
| 219 Library& core_lib = Library::Handle(zone, Library::CoreLibrary()); | 202 Library& core_lib = Library::Handle(zone, Library::CoreLibrary()); |
| 220 ASSERT(!core_lib.IsNull()); | 203 ASSERT(!core_lib.IsNull()); |
| 221 const Namespace& core_ns = Namespace::Handle( | 204 const Namespace& core_ns = Namespace::Handle( |
| 222 zone, Namespace::New(core_lib, Array::Handle(zone), Array::Handle(zone))); | 205 zone, Namespace::New(core_lib, Array::Handle(zone), Array::Handle(zone))); |
| 223 lib.AddImport(core_ns); | 206 lib.AddImport(core_ns); |
| 224 return lib; | 207 return lib; |
| 225 } | 208 } |
| 226 | 209 |
| 227 | |
| 228 static RawClass* LookupClass(const Library& lib, const char* name) { | 210 static RawClass* LookupClass(const Library& lib, const char* name) { |
| 229 const String& cls_name = | 211 const String& cls_name = |
| 230 String::ZoneHandle(Symbols::New(Thread::Current(), name)); | 212 String::ZoneHandle(Symbols::New(Thread::Current(), name)); |
| 231 return lib.LookupClass(cls_name); | 213 return lib.LookupClass(cls_name); |
| 232 } | 214 } |
| 233 | 215 |
| 234 | |
| 235 CODEGEN_TEST_GENERATE(StaticCallCodegen, test) { | 216 CODEGEN_TEST_GENERATE(StaticCallCodegen, test) { |
| 236 const char* kScriptChars = | 217 const char* kScriptChars = |
| 237 "class A {\n" | 218 "class A {\n" |
| 238 " static bar() { return 42; }\n" | 219 " static bar() { return 42; }\n" |
| 239 " static fly() { return 5; }\n" | 220 " static fly() { return 5; }\n" |
| 240 "}\n"; | 221 "}\n"; |
| 241 | 222 |
| 242 String& url = String::Handle(String::New("dart-test:CompileScript")); | 223 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 243 String& source = String::Handle(String::New(kScriptChars)); | 224 String& source = String::Handle(String::New(kScriptChars)); |
| 244 Script& script = | 225 Script& script = |
| (...skipping 25 matching lines...) Expand all Loading... |
| 270 StaticCallNode* call_fly = | 251 StaticCallNode* call_fly = |
| 271 new StaticCallNode(kPos, function_fly, no_arguments); | 252 new StaticCallNode(kPos, function_fly, no_arguments); |
| 272 | 253 |
| 273 BinaryOpNode* add_node = | 254 BinaryOpNode* add_node = |
| 274 new BinaryOpNode(kPos, Token::kADD, call_bar, call_fly); | 255 new BinaryOpNode(kPos, Token::kADD, call_bar, call_fly); |
| 275 | 256 |
| 276 test->node_sequence()->Add(new ReturnNode(kPos, add_node)); | 257 test->node_sequence()->Add(new ReturnNode(kPos, add_node)); |
| 277 } | 258 } |
| 278 CODEGEN_TEST_RUN(StaticCallCodegen, Smi::New(42 + 5)) | 259 CODEGEN_TEST_RUN(StaticCallCodegen, Smi::New(42 + 5)) |
| 279 | 260 |
| 280 | |
| 281 CODEGEN_TEST_GENERATE(InstanceCallCodegen, test) { | 261 CODEGEN_TEST_GENERATE(InstanceCallCodegen, test) { |
| 282 const char* kScriptChars = | 262 const char* kScriptChars = |
| 283 "class A {\n" | 263 "class A {\n" |
| 284 " A() {}\n" | 264 " A() {}\n" |
| 285 " int bar() { return 42; }\n" | 265 " int bar() { return 42; }\n" |
| 286 "}\n"; | 266 "}\n"; |
| 287 | 267 |
| 288 String& url = String::Handle(String::New("dart-test:CompileScript")); | 268 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 289 String& source = String::Handle(String::New(kScriptChars)); | 269 String& source = String::Handle(String::New(kScriptChars)); |
| 290 Script& script = | 270 Script& script = |
| 291 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); | 271 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); |
| 292 Library& lib = MakeTestLibrary("TestLib"); | 272 Library& lib = MakeTestLibrary("TestLib"); |
| 293 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 273 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 294 EXPECT(ClassFinalizer::ProcessPendingClasses()); | 274 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
| 295 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); | 275 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); |
| 296 EXPECT(!cls.IsNull()); | 276 EXPECT(!cls.IsNull()); |
| 297 | 277 |
| 298 String& constructor_name = String::Handle(String::New("A.")); | 278 String& constructor_name = String::Handle(String::New("A.")); |
| 299 Function& constructor = | 279 Function& constructor = |
| 300 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); | 280 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); |
| 301 EXPECT(!constructor.IsNull()); | 281 EXPECT(!constructor.IsNull()); |
| 302 | 282 |
| 303 // The unit test creates an instance of class A and calls function 'bar'. | 283 // The unit test creates an instance of class A and calls function 'bar'. |
| 304 String& function_bar_name = | 284 String& function_bar_name = |
| 305 String::ZoneHandle(Symbols::New(Thread::Current(), "bar")); | 285 String::ZoneHandle(Symbols::New(Thread::Current(), "bar")); |
| 306 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); | 286 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
| 307 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); | 287 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); |
| 308 InstanceCallNode* call_bar = new InstanceCallNode( | 288 InstanceCallNode* call_bar = |
| 309 kPos, new ConstructorCallNode(kPos, no_type_arguments, constructor, | 289 new InstanceCallNode(kPos, |
| 310 no_arguments), | 290 new ConstructorCallNode(kPos, no_type_arguments, |
| 311 function_bar_name, no_arguments); | 291 constructor, no_arguments), |
| 292 function_bar_name, no_arguments); |
| 312 | 293 |
| 313 test->node_sequence()->Add(new ReturnNode(kPos, call_bar)); | 294 test->node_sequence()->Add(new ReturnNode(kPos, call_bar)); |
| 314 } | 295 } |
| 315 CODEGEN_TEST_RUN(InstanceCallCodegen, Smi::New(42)) | 296 CODEGEN_TEST_RUN(InstanceCallCodegen, Smi::New(42)) |
| 316 | 297 |
| 317 | |
| 318 // Test allocation of dart objects. | 298 // Test allocation of dart objects. |
| 319 CODEGEN_TEST_GENERATE(AllocateNewObjectCodegen, test) { | 299 CODEGEN_TEST_GENERATE(AllocateNewObjectCodegen, test) { |
| 320 const char* kScriptChars = | 300 const char* kScriptChars = |
| 321 "class A {\n" | 301 "class A {\n" |
| 322 " A() {}\n" | 302 " A() {}\n" |
| 323 " static bar() { return 42; }\n" | 303 " static bar() { return 42; }\n" |
| 324 "}\n"; | 304 "}\n"; |
| 325 | 305 |
| 326 String& url = String::Handle(String::New("dart-test:CompileScript")); | 306 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 327 String& source = String::Handle(String::New(kScriptChars)); | 307 String& source = String::Handle(String::New(kScriptChars)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 338 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); | 318 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); |
| 339 EXPECT(!constructor.IsNull()); | 319 EXPECT(!constructor.IsNull()); |
| 340 | 320 |
| 341 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); | 321 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); |
| 342 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); | 322 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
| 343 test->node_sequence()->Add( | 323 test->node_sequence()->Add( |
| 344 new ReturnNode(kPos, new ConstructorCallNode(kPos, no_type_arguments, | 324 new ReturnNode(kPos, new ConstructorCallNode(kPos, no_type_arguments, |
| 345 constructor, no_arguments))); | 325 constructor, no_arguments))); |
| 346 } | 326 } |
| 347 | 327 |
| 348 | |
| 349 CODEGEN_TEST_RAW_RUN(AllocateNewObjectCodegen, function) { | 328 CODEGEN_TEST_RAW_RUN(AllocateNewObjectCodegen, function) { |
| 350 const Object& result = Object::Handle( | 329 const Object& result = Object::Handle( |
| 351 DartEntry::InvokeFunction(function, Object::empty_array())); | 330 DartEntry::InvokeFunction(function, Object::empty_array())); |
| 352 EXPECT(!result.IsError()); | 331 EXPECT(!result.IsError()); |
| 353 const GrowableObjectArray& libs = GrowableObjectArray::Handle( | 332 const GrowableObjectArray& libs = GrowableObjectArray::Handle( |
| 354 Isolate::Current()->object_store()->libraries()); | 333 Isolate::Current()->object_store()->libraries()); |
| 355 ASSERT(!libs.IsNull()); | 334 ASSERT(!libs.IsNull()); |
| 356 // App lib is the last one that was loaded. | 335 // App lib is the last one that was loaded. |
| 357 intptr_t num_libs = libs.Length(); | 336 intptr_t num_libs = libs.Length(); |
| 358 Library& app_lib = Library::Handle(); | 337 Library& app_lib = Library::Handle(); |
| 359 app_lib ^= libs.At(num_libs - 1); | 338 app_lib ^= libs.At(num_libs - 1); |
| 360 ASSERT(!app_lib.IsNull()); | 339 ASSERT(!app_lib.IsNull()); |
| 361 const Class& cls = Class::Handle(app_lib.LookupClass( | 340 const Class& cls = Class::Handle(app_lib.LookupClass( |
| 362 String::Handle(Symbols::New(Thread::Current(), "A")))); | 341 String::Handle(Symbols::New(Thread::Current(), "A")))); |
| 363 EXPECT_EQ(cls.raw(), result.clazz()); | 342 EXPECT_EQ(cls.raw(), result.clazz()); |
| 364 } | 343 } |
| 365 | 344 |
| 366 } // namespace dart | 345 } // namespace dart |
| OLD | NEW |