/src/WasmEdge/include/llvm/compiler.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | |
4 | | //===-- wasmedge/llvm/compiler.h - Compiler class definition --------------===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file defines the Compiler class. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/module.h" |
17 | | #include "common/configure.h" |
18 | | #include "common/errcode.h" |
19 | | #include "common/filesystem.h" |
20 | | #include "common/span.h" |
21 | | #include "llvm/data.h" |
22 | | |
23 | | #include <mutex> |
24 | | |
25 | | namespace WasmEdge::LLVM { |
26 | | |
27 | | class Module; |
28 | | class TargetMachine; |
29 | | |
30 | | /// Compiling Module into LLVM Module. |
31 | | class Compiler { |
32 | | public: |
33 | 2.30k | Compiler(const Configure &Conf) noexcept : Context(nullptr), Conf(Conf) {} |
34 | | |
35 | | Expect<void> checkConfigure() noexcept; |
36 | | |
37 | | /// Compile the whole module. |
38 | | Expect<Data> compile(const AST::Module &Module) noexcept; |
39 | | |
40 | | struct CompileContext; |
41 | | |
42 | | /// Compile only the infrastructure (types, imports, globals, etc.) without |
43 | | /// function bodies. |
44 | | Expect<Data> compileInfrastructure(const AST::Module &Module) noexcept; |
45 | | /// Compile multiple function bodies in one LLVM module for lazy JIT. |
46 | | /// \p LocalFuncIndices are indices of defined functions (not imports). |
47 | | Expect<Data> compileFunctions(Data &&LLData, const AST::Module &Module, |
48 | | Span<const uint32_t> LocalFuncIndices) noexcept; |
49 | | |
50 | | private: |
51 | | void compile(const AST::ImportSection &ImportSection) noexcept; |
52 | | void compile(const AST::ExportSection &ExportSection) noexcept; |
53 | | void compile(const AST::TypeSection &TypeSection, |
54 | | bool DeclarationsOnly = false) noexcept; |
55 | | void compile(const AST::GlobalSection &GlobalSection) noexcept; |
56 | | void compile(const AST::MemorySection &MemorySection, |
57 | | const AST::DataSection &DataSection) noexcept; |
58 | | void compile(const AST::TableSection &TableSection, |
59 | | const AST::ElementSection &ElementSection) noexcept; |
60 | | |
61 | | /// Compile all sections and create the function declarations. When |
62 | | /// \p DeclarationsOnly is set, the type wrappers are emitted as external |
63 | | /// declarations resolved against another module in the same JIT session. |
64 | | void compileSections(const AST::Module &Module, |
65 | | bool DeclarationsOnly) noexcept; |
66 | | void compileFunctionDeclarations(const AST::FunctionSection &FunctionSec, |
67 | | const AST::CodeSection &CodeSec) noexcept; |
68 | | Expect<void> compileFunctionBody(uint32_t LocalFuncIndex) noexcept; |
69 | | Expect<void> optimize(Module &LLModule, TargetMachine &TM) noexcept; |
70 | | |
71 | | std::mutex Mutex; |
72 | | CompileContext *Context; |
73 | | const Configure Conf; |
74 | | }; |
75 | | |
76 | | } // namespace WasmEdge::LLVM |