Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dfe.h
Go to the documentation of this file.
1// Copyright (c) 2017, 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#ifndef RUNTIME_BIN_DFE_H_
6#define RUNTIME_BIN_DFE_H_
7
8#include <memory>
9
10#include "bin/thread.h"
11#include "include/dart_api.h"
13#include "platform/assert.h"
14#include "platform/globals.h"
15#include "platform/hashmap.h"
16#include "platform/utils.h"
17
18namespace dart {
19namespace bin {
20
21class AppSnapshot; // Forward declaration.
22
23class DFE {
24 public:
25 DFE();
26 ~DFE();
27
28 // Call Init before Dart_Initialize to prevent races between the
29 // different isolates.
30 void Init();
31
32 char* frontend_filename() const { return frontend_filename_; }
33
34 void set_frontend_filename(const char* name) {
35 if (frontend_filename_ != nullptr) {
36 free(frontend_filename_);
37 }
38 frontend_filename_ = Utils::StrDup(name);
40 }
41 void set_use_dfe(bool value = true) { use_dfe_ = value; }
42 bool UseDartFrontend() const { return use_dfe_; }
43
45 use_incremental_compiler_ = value;
46 }
47 bool use_incremental_compiler() const { return use_incremental_compiler_; }
48
52 Dart_KernelCompilationVerbosityLevel verbosity() const { return verbosity_; }
53
54 // Returns the platform binary file name if the path to
55 // kernel binaries was set using SetKernelBinaries.
57
58 // Set the kernel program for the main application if it was specified
59 // as a dill file.
60 void set_application_kernel_buffer(uint8_t* buffer, intptr_t size) {
61 application_kernel_buffer_ = buffer;
62 application_kernel_buffer_size_ = size;
63 }
64 void application_kernel_buffer(const uint8_t** buffer, intptr_t* size) const {
65 *buffer = application_kernel_buffer_;
66 *size = application_kernel_buffer_size_;
67 }
68
69 // Compiles specified script.
70 // Returns result from compiling the script.
71 //
72 // `snapshot` is used by the frontend to determine if compilation
73 // related information should be printed to console (e.g., null safety mode).
74 Dart_KernelCompilationResult CompileScript(const char* script_uri,
75 bool incremental,
76 const char* package_config,
77 bool for_snapshot,
78 bool embedd_sources);
79
80 // Compiles specified script and reads the resulting kernel file.
81 // If the compilation is successful, returns a valid in memory kernel
82 // representation of the script, nullptr otherwise
83 // 'error' and 'exit_code' have the error values in case of errors.
84 //
85 // `snapshot` is used by the frontend to determine if compilation
86 // related information should be printed to console (e.g., null safety mode).
87 void CompileAndReadScript(const char* script_uri,
88 uint8_t** kernel_buffer,
89 intptr_t* kernel_buffer_size,
90 char** error,
91 int* exit_code,
92 const char* package_config,
93 bool for_snapshot,
94 bool embed_sources);
95
96 // Reads the script kernel file if specified 'script_uri' is a kernel file.
97 // Returns an in memory kernel representation of the specified script is a
98 // valid kernel file, sets 'kernel_buffer' to nullptr otherwise.
99 //
100 // If 'kernel_blob_ptr' is not nullptr, then this function can also
101 // read kernel blobs. In such case it sets 'kernel_blob_ptr'
102 // to a shared pointer which owns the kernel buffer.
103 // Otherwise, the caller is responsible for free()ing 'kernel_buffer'.
104 void ReadScript(const char* script_uri,
105 const AppSnapshot* app_snapshot,
106 uint8_t** kernel_buffer,
107 intptr_t* kernel_buffer_size,
108 bool decode_uri = true,
109 std::shared_ptr<uint8_t>* kernel_blob_ptr = nullptr);
110
111 bool KernelServiceDillAvailable() const;
112
113 // Tries to read 'script_uri' as a Kernel IR file.
114 // Returns `true` if successful and sets 'kernel_buffer' and 'kernel_length'
115 // to be the kernel IR contents.
116 //
117 // If 'kernel_blob_ptr' is not nullptr, then this function can also
118 // read kernel blobs. In such case it sets 'kernel_blob_ptr'
119 // to a shared pointer which owns the kernel buffer.
120 // Otherwise, the caller is responsible for free()ing 'kernel_buffer'
121 // if `true` was returned.
122 bool TryReadKernelFile(const char* script_uri,
123 const AppSnapshot* app_snapshot,
124 uint8_t** kernel_buffer,
125 intptr_t* kernel_buffer_size,
126 bool decode_uri = true,
127 std::shared_ptr<uint8_t>* kernel_blob_ptr = nullptr);
128
129 // We distinguish between "intent to use Dart frontend" vs "can actually
130 // use Dart frontend". The method UseDartFrontend tells us about the
131 // intent to use DFE. This method tells us if Dart frontend can actually
132 // be used.
133 bool CanUseDartFrontend() const;
134
135 void LoadPlatform(const uint8_t** kernel_buffer,
136 intptr_t* kernel_buffer_size);
137 void LoadKernelService(const uint8_t** kernel_service_buffer,
138 intptr_t* kernel_service_buffer_size);
139
140 // Registers given kernel blob and returns blob URI which
141 // can be used in TryReadKernelFile later to load the given kernel.
142 // Data from [kernel_buffer] is copied, it doesn't need to stay alive.
143 // Returns nullptr if failed to allocate memory.
144 const char* RegisterKernelBlob(const uint8_t* kernel_buffer,
145 intptr_t kernel_buffer_size);
146
147 // Looks for kernel blob using the given [uri].
148 // Returns non-null pointer to the kernel blob if successful and
149 // sets [kernel_length].
150 std::shared_ptr<uint8_t> TryFindKernelBlob(const char* uri,
151 intptr_t* kernel_length);
152
153 // Unregisters kernel blob with given URI.
154 void UnregisterKernelBlob(const char* uri);
155
156 private:
157 bool use_dfe_;
158 bool use_incremental_compiler_;
159 char* frontend_filename_;
162
163 // Kernel binary specified on the cmd line.
164 uint8_t* application_kernel_buffer_;
165 intptr_t application_kernel_buffer_size_;
166
167 // Registry of kernel blobs. Maps URI (char *) to KernelBlob.
168 SimpleHashMap kernel_blobs_;
169 intptr_t kernel_blob_counter_ = 0;
170 Mutex kernel_blobs_lock_;
171
172 void InitKernelServiceAndPlatformDills();
173
175};
176
178 public:
179 // Takes ownership over [uri] and [buffer].
180 KernelBlob(char* uri, uint8_t* buffer, intptr_t size)
181 : uri_(uri, std::free), buffer_(buffer, std::free), size_(size) {}
182
183 std::shared_ptr<uint8_t> buffer() { return buffer_; }
184 intptr_t size() const { return size_; }
185
186 private:
188 std::shared_ptr<uint8_t> buffer_;
189 const intptr_t size_;
190
192};
193
195 public:
196 explicit PathSanitizer(const char* path);
197 const char* sanitized_uri() const;
198
199 private:
200#if defined(DART_HOST_OS_WINDOWS)
201 std::unique_ptr<char[]> sanitized_uri_;
202#else
203 const char* sanitized_uri_;
204#endif // defined(DART_HOST_OS_WINDOWS)
205
207};
208
209#if !defined(DART_PRECOMPILED_RUNTIME)
210extern DFE dfe;
211#endif
212
213} // namespace bin
214} // namespace dart
215
216#endif // RUNTIME_BIN_DFE_H_
static char * StrDup(const char *s)
std::unique_ptr< char, decltype(std::free) * > CStringUniquePtr
Definition utils.h:644
bool TryReadKernelFile(const char *script_uri, const AppSnapshot *app_snapshot, uint8_t **kernel_buffer, intptr_t *kernel_buffer_size, bool decode_uri=true, std::shared_ptr< uint8_t > *kernel_blob_ptr=nullptr)
Definition dfe.cc:437
void CompileAndReadScript(const char *script_uri, uint8_t **kernel_buffer, intptr_t *kernel_buffer_size, char **error, int *exit_code, const char *package_config, bool for_snapshot, bool embed_sources)
Definition dfe.cc:204
bool CanUseDartFrontend() const
Definition dfe.cc:143
void set_use_dfe(bool value=true)
Definition dfe.h:41
std::shared_ptr< uint8_t > TryFindKernelBlob(const char *uri, intptr_t *kernel_length)
Definition dfe.cc:508
const char * GetPlatformBinaryFilename()
void set_verbosity(Dart_KernelCompilationVerbosityLevel verbosity)
Definition dfe.h:49
Dart_KernelCompilationVerbosityLevel verbosity() const
Definition dfe.h:52
void set_frontend_filename(const char *name)
Definition dfe.h:34
void set_application_kernel_buffer(uint8_t *buffer, intptr_t size)
Definition dfe.h:60
void set_use_incremental_compiler(bool value)
Definition dfe.h:44
bool UseDartFrontend() const
Definition dfe.h:42
bool use_incremental_compiler() const
Definition dfe.h:47
void LoadPlatform(const uint8_t **kernel_buffer, intptr_t *kernel_buffer_size)
Definition dfe.cc:137
const char * RegisterKernelBlob(const uint8_t *kernel_buffer, intptr_t kernel_buffer_size)
Definition dfe.cc:482
Dart_KernelCompilationResult CompileScript(const char *script_uri, bool incremental, const char *package_config, bool for_snapshot, bool embedd_sources)
Definition dfe.cc:189
bool KernelServiceDillAvailable() const
Definition dfe.cc:127
void Init()
Definition dfe.cc:88
void application_kernel_buffer(const uint8_t **buffer, intptr_t *size) const
Definition dfe.h:64
void UnregisterKernelBlob(const char *uri)
Definition dfe.cc:531
void ReadScript(const char *script_uri, const AppSnapshot *app_snapshot, uint8_t **kernel_buffer, intptr_t *kernel_buffer_size, bool decode_uri=true, std::shared_ptr< uint8_t > *kernel_blob_ptr=nullptr)
Definition dfe.cc:241
void LoadKernelService(const uint8_t **kernel_service_buffer, intptr_t *kernel_service_buffer_size)
Definition dfe.cc:131
char * frontend_filename() const
Definition dfe.h:32
KernelBlob(char *uri, uint8_t *buffer, intptr_t size)
Definition dfe.h:180
intptr_t size() const
Definition dfe.h:184
std::shared_ptr< uint8_t > buffer()
Definition dfe.h:183
const char * sanitized_uri() const
Definition dfe.cc:181
Dart_KernelCompilationVerbosityLevel
Definition dart_api.h:3741
@ Dart_KernelCompilationVerbosityLevel_All
Definition dart_api.h:3745
static const uint8_t buffer[]
const uint8_t uint32_t uint32_t GError ** error
uint8_t value
DFE dfe
Definition dfe.cc:59
const char *const name
Definition ref_ptr.h:256
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581