OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 #include "vm/precompiler.h" |
| 6 |
| 7 #include "vm/isolate.h" |
| 8 #include "vm/object.h" |
| 9 #include "vm/object_store.h" |
| 10 #include "vm/compiler.h" |
| 11 #include "vm/longjump.h" |
| 12 |
| 13 namespace dart { |
| 14 |
| 15 |
| 16 DEFINE_FLAG(bool, trace_precompiler, false, "Trace precompiler."); |
| 17 |
| 18 |
| 19 static RawError* CompileFunction(const Function& func) { |
| 20 Thread* thread = Thread::Current(); |
| 21 Error& error = Error::Handle(); |
| 22 if (func.is_abstract() || func.IsRedirectingFactory()) { |
| 23 return error.raw(); |
| 24 } |
| 25 |
| 26 if (!func.HasCode()) { |
| 27 if (FLAG_trace_precompiler) { |
| 28 OS::Print(" Precompiling %s\n", func.ToQualifiedCString()); |
| 29 } |
| 30 error = Compiler::CompileFunction(thread, func); |
| 31 if (!error.IsNull()) { |
| 32 return error.raw(); |
| 33 } |
| 34 } |
| 35 |
| 36 if ((func.kind() == RawFunction::kRegularFunction) && !func.is_static()) { |
| 37 const String& name = String::Handle(func.name()); |
| 38 if (!Field::IsGetterName(name) && !Field::IsSetterName(name)) { |
| 39 const Function& closure_func = |
| 40 Function::Handle(func.ImplicitClosureFunction()); |
| 41 ASSERT(!closure_func.IsNull()); |
| 42 error = CompileFunction(closure_func); |
| 43 } |
| 44 } |
| 45 |
| 46 return error.raw(); |
| 47 } |
| 48 |
| 49 |
| 50 static RawError* CompileClass(const Class& cls) { |
| 51 if (FLAG_trace_precompiler) { |
| 52 OS::Print(" Precompiling %s\n", cls.ToCString()); |
| 53 } |
| 54 |
| 55 Error& error = Error::Handle(); |
| 56 error = cls.EnsureIsFinalized(Isolate::Current()); |
| 57 if (!error.IsNull()) { |
| 58 return error.raw(); |
| 59 } |
| 60 |
| 61 const Array& fields = Array::Handle(cls.fields()); |
| 62 Field& field = Field::Handle(); |
| 63 for (intptr_t i = 0; i < fields.Length(); i++) { |
| 64 field ^= fields.At(i); |
| 65 ASSERT(!field.IsNull()); |
| 66 if (field.is_static() && field.has_initializer()) { |
| 67 if (FLAG_trace_precompiler) { |
| 68 OS::Print(" Precompiling initializer for %s\n", field.ToCString()); |
| 69 } |
| 70 Compiler::CompileStaticInitializer(field); |
| 71 } |
| 72 } |
| 73 |
| 74 const Array& functions = Array::Handle(cls.functions()); |
| 75 Function& func = Function::Handle(); |
| 76 for (intptr_t i = 0; i < functions.Length(); i++) { |
| 77 func ^= functions.At(i); |
| 78 ASSERT(!func.IsNull()); |
| 79 error = CompileFunction(func); |
| 80 if (!error.IsNull()) { |
| 81 return error.raw(); |
| 82 } |
| 83 } |
| 84 |
| 85 return error.raw(); |
| 86 } |
| 87 |
| 88 |
| 89 static RawError* CompileLibrary(const Library& lib) { |
| 90 if (FLAG_trace_precompiler) { |
| 91 OS::Print("Precompiling %s\n", lib.ToCString()); |
| 92 } |
| 93 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); |
| 94 Class& cls = Class::Handle(); |
| 95 Error& error = Error::Handle(); |
| 96 while (it.HasNext()) { |
| 97 cls = it.GetNextClass(); |
| 98 error = CompileClass(cls); |
| 99 if (!error.IsNull()) { |
| 100 return error.raw(); |
| 101 } |
| 102 } |
| 103 |
| 104 return error.raw(); |
| 105 } |
| 106 |
| 107 |
| 108 static RawError* InnerCompileAll() { |
| 109 Error& error = Error::Handle(); |
| 110 const GrowableObjectArray& libs = GrowableObjectArray::Handle( |
| 111 Isolate::Current()->object_store()->libraries()); |
| 112 Library& lib = Library::Handle(); |
| 113 for (int i = 0; i < libs.Length(); i++) { |
| 114 lib ^= libs.At(i); |
| 115 error = CompileLibrary(lib); |
| 116 if (!error.IsNull()) { |
| 117 return error.raw(); |
| 118 } |
| 119 } |
| 120 |
| 121 if (FLAG_trace_precompiler) { |
| 122 OS::Print("** Precompiling collected closures **\n"); |
| 123 } |
| 124 |
| 125 const GrowableObjectArray& closures = |
| 126 GrowableObjectArray::Handle(Isolate::Current()->collected_closures()); |
| 127 Function& func = Function::Handle(); |
| 128 if (!closures.IsNull()) { |
| 129 for (int i = 0; i < closures.Length(); i++) { |
| 130 func ^= closures.At(i); |
| 131 error = CompileFunction(func); |
| 132 if (!error.IsNull()) { |
| 133 return error.raw(); |
| 134 } |
| 135 } |
| 136 } |
| 137 Isolate::Current()->set_collected_closures(GrowableObjectArray::Handle()); |
| 138 |
| 139 if (FLAG_trace_precompiler) { |
| 140 OS::Print("*** Done precompiling ***\n"); |
| 141 } |
| 142 Isolate::Current()->set_compilation_allowed(false); |
| 143 return error.raw(); |
| 144 } |
| 145 |
| 146 |
| 147 RawError* Precompiler::CompileAll() { |
| 148 LongJumpScope jump; |
| 149 if (setjmp(*jump.Set()) == 0) { |
| 150 return InnerCompileAll(); |
| 151 } else { |
| 152 Isolate* isolate = Isolate::Current(); |
| 153 const Error& error = Error::Handle(isolate, |
| 154 isolate->object_store()->sticky_error()); |
| 155 isolate->object_store()->clear_sticky_error(); |
| 156 return error.raw(); |
| 157 } |
| 158 } |
| 159 |
| 160 } // namespace dart |
OLD | NEW |