| 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 "include/dart_api.h" |
| 11 | #include "include/dart_native_api.h" |
| 12 | #include "platform/assert.h" |
| 13 | #include "platform/globals.h" |
| 14 | #include "platform/utils.h" |
| 15 | |
| 16 | namespace dart { |
| 17 | namespace bin { |
| 18 | |
| 19 | class DFE { |
| 20 | public: |
| 21 | DFE(); |
| 22 | ~DFE(); |
| 23 | |
| 24 | // Call Init before Dart_Initialize to prevent races between the |
| 25 | // different isolates. |
| 26 | void Init(); |
| 27 | |
| 28 | char* frontend_filename() const { return frontend_filename_; } |
| 29 | |
| 30 | void set_frontend_filename(const char* name) { |
| 31 | if (frontend_filename_ != nullptr) { |
| 32 | free(frontend_filename_); |
| 33 | } |
| 34 | frontend_filename_ = Utils::StrDup(name); |
| 35 | set_use_dfe(); |
| 36 | } |
| 37 | void set_use_dfe(bool value = true) { use_dfe_ = value; } |
| 38 | bool UseDartFrontend() const { return use_dfe_; } |
| 39 | |
| 40 | void set_use_incremental_compiler(bool value) { |
| 41 | use_incremental_compiler_ = value; |
| 42 | } |
| 43 | bool use_incremental_compiler() const { return use_incremental_compiler_; } |
| 44 | |
| 45 | // Returns the platform binary file name if the path to |
| 46 | // kernel binaries was set using SetKernelBinaries. |
| 47 | const char* GetPlatformBinaryFilename(); |
| 48 | |
| 49 | // Set the kernel program for the main application if it was specified |
| 50 | // as a dill file. |
| 51 | void set_application_kernel_buffer(uint8_t* buffer, intptr_t size) { |
| 52 | application_kernel_buffer_ = buffer; |
| 53 | application_kernel_buffer_size_ = size; |
| 54 | } |
| 55 | void application_kernel_buffer(const uint8_t** buffer, intptr_t* size) const { |
| 56 | *buffer = application_kernel_buffer_; |
| 57 | *size = application_kernel_buffer_size_; |
| 58 | } |
| 59 | |
| 60 | // Compiles specified script. |
| 61 | // Returns result from compiling the script. |
| 62 | Dart_KernelCompilationResult CompileScript(const char* script_uri, |
| 63 | bool incremental, |
| 64 | const char* package_config); |
| 65 | |
| 66 | // Compiles specified script and reads the resulting kernel file. |
| 67 | // If the compilation is successful, returns a valid in memory kernel |
| 68 | // representation of the script, NULL otherwise |
| 69 | // 'error' and 'exit_code' have the error values in case of errors. |
| 70 | void CompileAndReadScript(const char* script_uri, |
| 71 | uint8_t** kernel_buffer, |
| 72 | intptr_t* kernel_buffer_size, |
| 73 | char** error, |
| 74 | int* exit_code, |
| 75 | const char* package_config); |
| 76 | |
| 77 | // Reads the script kernel file if specified 'script_uri' is a kernel file. |
| 78 | // Returns an in memory kernel representation of the specified script is a |
| 79 | // valid kernel file, false otherwise. |
| 80 | void ReadScript(const char* script_uri, |
| 81 | uint8_t** kernel_buffer, |
| 82 | intptr_t* kernel_buffer_size) const; |
| 83 | |
| 84 | bool KernelServiceDillAvailable() const; |
| 85 | |
| 86 | // Tries to read [script_uri] as a Kernel IR file. |
| 87 | // Returns `true` if successful and sets [kernel_file] and [kernel_length] |
| 88 | // to be the kernel IR contents. |
| 89 | // The caller is responsible for free()ing [kernel_file] if `true` |
| 90 | // was returned. |
| 91 | static bool TryReadKernelFile(const char* script_uri, |
| 92 | uint8_t** kernel_buffer, |
| 93 | intptr_t* kernel_buffer_size); |
| 94 | |
| 95 | // We distinguish between "intent to use Dart frontend" vs "can actually |
| 96 | // use Dart frontend". The method UseDartFrontend tells us about the |
| 97 | // intent to use DFE. This method tells us if Dart frontend can actually |
| 98 | // be used. |
| 99 | bool CanUseDartFrontend() const; |
| 100 | |
| 101 | void LoadPlatform(const uint8_t** kernel_buffer, |
| 102 | intptr_t* kernel_buffer_size); |
| 103 | void LoadKernelService(const uint8_t** kernel_service_buffer, |
| 104 | intptr_t* kernel_service_buffer_size); |
| 105 | |
| 106 | private: |
| 107 | bool use_dfe_; |
| 108 | bool use_incremental_compiler_; |
| 109 | char* frontend_filename_; |
| 110 | |
| 111 | // Kernel binary specified on the cmd line. |
| 112 | uint8_t* application_kernel_buffer_; |
| 113 | intptr_t application_kernel_buffer_size_; |
| 114 | |
| 115 | void InitKernelServiceAndPlatformDills(); |
| 116 | |
| 117 | DISALLOW_COPY_AND_ASSIGN(DFE); |
| 118 | }; |
| 119 | |
| 120 | class PathSanitizer { |
| 121 | public: |
| 122 | explicit PathSanitizer(const char* path); |
| 123 | const char* sanitized_uri() const; |
| 124 | |
| 125 | private: |
| 126 | #if defined(HOST_OS_WINDOWS) |
| 127 | std::unique_ptr<char[]> sanitized_uri_; |
| 128 | #else |
| 129 | const char* sanitized_uri_; |
| 130 | #endif // defined(HOST_OS_WINDOWS) |
| 131 | |
| 132 | DISALLOW_COPY_AND_ASSIGN(PathSanitizer); |
| 133 | }; |
| 134 | |
| 135 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 136 | extern DFE dfe; |
| 137 | #endif |
| 138 | |
| 139 | } // namespace bin |
| 140 | } // namespace dart |
| 141 | |
| 142 | #endif // RUNTIME_BIN_DFE_H_ |
| 143 | |