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 "vm/globals.h" // Needed here to get TARGET_ARCH_X64. | 5 #include "vm/globals.h" // Needed here to get TARGET_ARCH_X64. |
6 #if defined(TARGET_ARCH_X64) | 6 #if defined(TARGET_ARCH_X64) |
7 | 7 |
8 #include "vm/code_generator.h" | 8 #include "vm/code_generator.h" |
9 | 9 |
10 #include "lib/error.h" | 10 #include "lib/error.h" |
11 #include "vm/ast_printer.h" | 11 #include "vm/ast_printer.h" |
12 #include "vm/class_finalizer.h" | 12 #include "vm/class_finalizer.h" |
13 #include "vm/dart_entry.h" | 13 #include "vm/dart_entry.h" |
14 #include "vm/debugger.h" | 14 #include "vm/debugger.h" |
15 #include "vm/longjump.h" | 15 #include "vm/longjump.h" |
16 #include "vm/object.h" | 16 #include "vm/object.h" |
17 #include "vm/object_store.h" | 17 #include "vm/object_store.h" |
18 #include "vm/parser.h" | 18 #include "vm/parser.h" |
19 #include "vm/resolver.h" | 19 #include "vm/resolver.h" |
20 #include "vm/stub_code.h" | 20 #include "vm/stub_code.h" |
21 | 21 |
22 namespace dart { | 22 namespace dart { |
23 | 23 |
24 DEFINE_FLAG(bool, print_ast, false, "Print abstract syntax tree."); | 24 DEFINE_FLAG(bool, print_ast, false, "Print abstract syntax tree."); |
25 DEFINE_FLAG(bool, print_scopes, false, "Print scopes of local variables."); | 25 DEFINE_FLAG(bool, print_scopes, false, "Print scopes of local variables."); |
26 DEFINE_FLAG(bool, trace_functions, false, "Trace entry of each function."); | 26 DEFINE_FLAG(bool, trace_functions, false, "Trace entry of each function."); |
27 DEFINE_FLAG(int, optimization_invocation_threshold, 1000, | |
28 "number of invocations before a function is optimized, -1 means never."); | |
29 DECLARE_FLAG(bool, enable_type_checks); | 27 DECLARE_FLAG(bool, enable_type_checks); |
30 DECLARE_FLAG(bool, report_invocation_count); | |
31 DECLARE_FLAG(bool, trace_compiler); | 28 DECLARE_FLAG(bool, trace_compiler); |
32 | 29 |
33 #define __ assembler_-> | 30 #define __ assembler_-> |
34 | 31 |
35 | 32 |
36 // TODO(regis): CodeGeneratorState, CodeGenerator::DescriptorList, and | 33 // TODO(regis): CodeGeneratorState, CodeGenerator::DescriptorList, and |
37 // CodeGenerator::HandlerList can probably be moved to code_generator.cc, since | 34 // CodeGenerator::HandlerList can probably be moved to code_generator.cc, since |
38 // they seem to be architecture independent. | 35 // they seem to be architecture independent. |
39 | 36 |
40 | 37 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 0, | 189 0, |
193 -1); | 190 -1); |
194 __ jmp(&StubCode::FixCallersTargetLabel()); | 191 __ jmp(&StubCode::FixCallersTargetLabel()); |
195 } | 192 } |
196 | 193 |
197 | 194 |
198 void CodeGenerator::GenerateDeferredCode() { | 195 void CodeGenerator::GenerateDeferredCode() { |
199 } | 196 } |
200 | 197 |
201 | 198 |
202 // Pre entry code is called before the frame has been constructed: | 199 // Pre entry code is called before the frame has been constructed. |
203 // - check for stack overflow. | |
204 // - optionally count function invocations. | |
205 // - optionally trigger optimizing compiler if invocation threshold has been | |
206 // reached. | |
207 // Note that first 13 bytes may be patched with a jump. | 200 // Note that first 13 bytes may be patched with a jump. |
208 // TODO(srdjan): Add check that no object is inlined in the first | 201 // TODO(srdjan): Add check that no object is inlined in the first |
209 // 13 bytes (length of a jump instruction). | 202 // 13 bytes (length of a jump instruction). |
210 void CodeGenerator::GeneratePreEntryCode() { | 203 void CodeGenerator::GeneratePreEntryCode() { |
211 // Do not optimize if: | 204 // Do not optimize if: |
212 // - we count invocations. | 205 // - we count invocations. |
213 // - optimization disabled via negative 'optimization_invocation_threshold; | 206 // - optimization disabled. |
214 // - function is marked as non-optimizable. | 207 // - function is marked as non-optimizable. |
215 // - type checks are enabled. | 208 // - type checks are enabled. |
216 const bool may_optimize = | 209 // TODO(srdjan): Nop's still needed? |
217 false && // TODO(srdjan): Remove once the optimizer is enabled on x64. | 210 __ nop(8); |
218 !FLAG_report_invocation_count && | 211 __ nop(5); |
219 (FLAG_optimization_invocation_threshold >= 0) && | |
220 !Isolate::Current()->debugger()->IsActive() && | |
221 parsed_function_.function().is_optimizable(); | |
222 // Count invocation and check. | |
223 if (FLAG_report_invocation_count || may_optimize) { | |
224 // TODO(turnidge): It would be nice to remove this nop. Right now | |
225 // we need it to make sure the function is still patchable. | |
226 __ nop(8); | |
227 __ nop(5); | |
228 const Function& function = | |
229 Function::ZoneHandle(parsed_function_.function().raw()); | |
230 __ LoadObject(RAX, function); | |
231 __ movq(R8, FieldAddress(RAX, Function::invocation_counter_offset())); | |
232 __ incq(R8); | |
233 if (may_optimize) { | |
234 __ cmpq(R8, Immediate(FLAG_optimization_invocation_threshold)); | |
235 __ j(GREATER, &StubCode::OptimizeInvokedFunctionLabel()); | |
236 } | |
237 // R8 contains an integer value, not an object. | |
238 __ movq(FieldAddress(RAX, Function::invocation_counter_offset()), R8); | |
239 } | |
240 } | 212 } |
241 | 213 |
242 | 214 |
243 // Verify assumptions (in debug mode only). | 215 // Verify assumptions (in debug mode only). |
244 // - No two deopt descriptors have the same node id (deoptimization). | 216 // - No two deopt descriptors have the same node id (deoptimization). |
245 // - No two ic-call descriptors have the same node id (type feedback). | 217 // - No two ic-call descriptors have the same node id (type feedback). |
246 // - No two descriptors of same kind have the same PC. | 218 // - No two descriptors of same kind have the same PC. |
247 // A function without unique ids is marked as non-optimizable (e.g., because of | 219 // A function without unique ids is marked as non-optimizable (e.g., because of |
248 // finally blocks). | 220 // finally blocks). |
249 static void VerifyPcDescriptors(const PcDescriptors& descriptors, | 221 static void VerifyPcDescriptors(const PcDescriptors& descriptors, |
(...skipping 1586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1836 | 1808 |
1837 // Call operator. | 1809 // Call operator. |
1838 GenerateBinaryOperatorCall(node->id(), node->token_index(), node->Name()); | 1810 GenerateBinaryOperatorCall(node->id(), node->token_index(), node->Name()); |
1839 // Result is in RAX. | 1811 // Result is in RAX. |
1840 if (IsResultNeeded(node)) { | 1812 if (IsResultNeeded(node)) { |
1841 __ pushq(RAX); | 1813 __ pushq(RAX); |
1842 } | 1814 } |
1843 } | 1815 } |
1844 | 1816 |
1845 | 1817 |
1846 void CodeGenerator::CountBackwardLoop() { | |
1847 Label done; | |
1848 const Function& function = | |
1849 Function::ZoneHandle(parsed_function_.function().raw()); | |
1850 __ LoadObject(RAX, function); | |
1851 __ movq(RBX, FieldAddress(RAX, Function::invocation_counter_offset())); | |
1852 __ incq(RBX); | |
1853 if (!FLAG_report_invocation_count) { | |
1854 // Prevent overflow. | |
1855 __ cmpq(RBX, Immediate(FLAG_optimization_invocation_threshold)); | |
1856 __ j(GREATER, &done); | |
1857 } | |
1858 // RBX contains an integer value, not an object. | |
1859 __ movq(FieldAddress(RAX, Function::invocation_counter_offset()), RBX); | |
1860 __ Bind(&done); | |
1861 } | |
1862 | |
1863 | |
1864 void CodeGenerator::VisitWhileNode(WhileNode* node) { | 1818 void CodeGenerator::VisitWhileNode(WhileNode* node) { |
1865 const Bool& bool_true = Bool::ZoneHandle(Bool::True()); | 1819 const Bool& bool_true = Bool::ZoneHandle(Bool::True()); |
1866 SourceLabel* label = node->label(); | 1820 SourceLabel* label = node->label(); |
1867 __ Bind(label->continue_label()); | 1821 __ Bind(label->continue_label()); |
1868 node->condition()->Visit(this); | 1822 node->condition()->Visit(this); |
1869 GenerateConditionTypeCheck(node->id(), node->condition()->token_index()); | 1823 GenerateConditionTypeCheck(node->id(), node->condition()->token_index()); |
1870 __ popq(RAX); | 1824 __ popq(RAX); |
1871 __ LoadObject(RDX, bool_true); | 1825 __ LoadObject(RDX, bool_true); |
1872 __ cmpq(RAX, RDX); | 1826 __ cmpq(RAX, RDX); |
1873 __ j(NOT_EQUAL, label->break_label()); | 1827 __ j(NOT_EQUAL, label->break_label()); |
1874 node->body()->Visit(this); | 1828 node->body()->Visit(this); |
1875 CountBackwardLoop(); | |
1876 __ jmp(label->continue_label()); | 1829 __ jmp(label->continue_label()); |
1877 __ Bind(label->break_label()); | 1830 __ Bind(label->break_label()); |
1878 } | 1831 } |
1879 | 1832 |
1880 | 1833 |
1881 void CodeGenerator::VisitDoWhileNode(DoWhileNode* node) { | 1834 void CodeGenerator::VisitDoWhileNode(DoWhileNode* node) { |
1882 const Bool& bool_true = Bool::ZoneHandle(Bool::True()); | 1835 const Bool& bool_true = Bool::ZoneHandle(Bool::True()); |
1883 SourceLabel* label = node->label(); | 1836 SourceLabel* label = node->label(); |
1884 Label loop; | 1837 Label loop; |
1885 __ Bind(&loop); | 1838 __ Bind(&loop); |
1886 node->body()->Visit(this); | 1839 node->body()->Visit(this); |
1887 CountBackwardLoop(); | |
1888 __ Bind(label->continue_label()); | 1840 __ Bind(label->continue_label()); |
1889 node->condition()->Visit(this); | 1841 node->condition()->Visit(this); |
1890 GenerateConditionTypeCheck(node->id(), node->condition()->token_index()); | 1842 GenerateConditionTypeCheck(node->id(), node->condition()->token_index()); |
1891 __ popq(RAX); | 1843 __ popq(RAX); |
1892 __ LoadObject(RDX, bool_true); | 1844 __ LoadObject(RDX, bool_true); |
1893 __ cmpq(RAX, RDX); | 1845 __ cmpq(RAX, RDX); |
1894 __ j(EQUAL, &loop); | 1846 __ j(EQUAL, &loop); |
1895 __ Bind(label->break_label()); | 1847 __ Bind(label->break_label()); |
1896 } | 1848 } |
1897 | 1849 |
1898 | 1850 |
1899 void CodeGenerator::VisitForNode(ForNode* node) { | 1851 void CodeGenerator::VisitForNode(ForNode* node) { |
1900 const Bool& bool_true = Bool::ZoneHandle(Bool::True()); | 1852 const Bool& bool_true = Bool::ZoneHandle(Bool::True()); |
1901 node->initializer()->Visit(this); | 1853 node->initializer()->Visit(this); |
1902 SourceLabel* label = node->label(); | 1854 SourceLabel* label = node->label(); |
1903 Label loop; | 1855 Label loop; |
1904 __ Bind(&loop); | 1856 __ Bind(&loop); |
1905 if (node->condition() != NULL) { | 1857 if (node->condition() != NULL) { |
1906 node->condition()->Visit(this); | 1858 node->condition()->Visit(this); |
1907 GenerateConditionTypeCheck(node->id(), node->condition()->token_index()); | 1859 GenerateConditionTypeCheck(node->id(), node->condition()->token_index()); |
1908 __ popq(RAX); | 1860 __ popq(RAX); |
1909 __ LoadObject(RDX, bool_true); | 1861 __ LoadObject(RDX, bool_true); |
1910 __ cmpq(RAX, RDX); | 1862 __ cmpq(RAX, RDX); |
1911 __ j(NOT_EQUAL, label->break_label()); | 1863 __ j(NOT_EQUAL, label->break_label()); |
1912 } | 1864 } |
1913 node->body()->Visit(this); | 1865 node->body()->Visit(this); |
1914 CountBackwardLoop(); | |
1915 __ Bind(label->continue_label()); | 1866 __ Bind(label->continue_label()); |
1916 node->increment()->Visit(this); | 1867 node->increment()->Visit(this); |
1917 __ jmp(&loop); | 1868 __ jmp(&loop); |
1918 __ Bind(label->break_label()); | 1869 __ Bind(label->break_label()); |
1919 } | 1870 } |
1920 | 1871 |
1921 | 1872 |
1922 void CodeGenerator::VisitJumpNode(JumpNode* node) { | 1873 void CodeGenerator::VisitJumpNode(JumpNode* node) { |
1923 SourceLabel* label = node->label(); | 1874 SourceLabel* label = node->label(); |
1924 | 1875 |
(...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2744 const Error& error = Error::Handle( | 2695 const Error& error = Error::Handle( |
2745 Parser::FormatError(script, token_index, "Error", format, args)); | 2696 Parser::FormatError(script, token_index, "Error", format, args)); |
2746 va_end(args); | 2697 va_end(args); |
2747 Isolate::Current()->long_jump_base()->Jump(1, error); | 2698 Isolate::Current()->long_jump_base()->Jump(1, error); |
2748 UNREACHABLE(); | 2699 UNREACHABLE(); |
2749 } | 2700 } |
2750 | 2701 |
2751 } // namespace dart | 2702 } // namespace dart |
2752 | 2703 |
2753 #endif // defined TARGET_ARCH_X64 | 2704 #endif // defined TARGET_ARCH_X64 |
OLD | NEW |