1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_IRTOC_COMPILATION_H
17 #define PANDA_IRTOC_COMPILATION_H
18 
19 #include "function.h"
20 
21 namespace ark::irtoc {
22 using compiler::Graph;
23 
24 class Compilation {
25 public:
26     using Result = Expected<int, const char *>;
27 
28     Compilation() = default;
29 
30     Result Run();
31 
32     Result MakeElf(std::string_view output);
33 
34     template <typename T, typename... Args>
RegisterUnit(Args &&....args)35     static int RegisterUnit(Args &&...args)
36     {
37         static_assert(std::is_base_of_v<Function, T>);
38         // CODECHECK-NOLINTNEXTLINE(CPP_RULE_ID_SMARTPOINTER_INSTEADOF_ORIGINPOINTER)
39         units_.push_back(new T(std::forward<Args>(args)...));
40         return 0;
41     }
42 
GetArch() const43     Arch GetArch() const
44     {
45         return arch_;
46     }
47 
48 private:
49     void CollectUsedRegisters(ark::ArenaAllocator *allocator);
50     void CheckUsedRegisters();
51     Result Compile();
52 
53 private:
54     // NOLINTNEXTLINE(fuchsia-statically-constructed-objects)
55     static inline std::vector<Function *> units_;
56     static inline bool initialized_ = false;
57     Arch arch_ {RUNTIME_ARCH};
58     std::regex methodsRegex_;
59     std::unique_ptr<ArenaAllocator> allocator_;
60     std::unique_ptr<ArenaAllocator> localAllocator_;
61 #ifdef PANDA_COMPILER_DEBUG_INFO
62     bool hasDebugInfo_ {false};
63 #endif
64 #ifdef LLVM_INTERPRETER_CHECK_REGS_MASK
65     UsedRegisters usedRegisters_;
66 #endif
67 };
68 }  // namespace ark::irtoc
69 
70 // CC-OFFNXT(G.PRE.06) code generation
71 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
72 #define COMPILE(name)                                     \
73     /* CC-OFFNXT(G.PRE.02) part name */                   \
74     class name : public Function {                        \
75     public:                                               \
76         using Function::Function;                         \
77         void MakeGraphImpl() override;                    \
78         const char *GetName() const override              \
79         {                                                 \
80             /* CC-OFFNXT(G.PRE.05) function gen */        \
81             return #name;                                 \
82         }                                                 \
83                                                           \
84     private:                                              \
85         static int dummy;                                 \
86     };                                                    \
87     /* CC-OFFNXT(G.PRE.02) part name */                   \
88     int name ::dummy = Compilation::RegisterUnit<name>(); \
89     /* CC-OFFNXT(G.PRE.02) part name */                   \
90     void name ::MakeGraphImpl()
91 
92 #endif  // PANDA_IRTOC_COMPILATION_H
93