1 /* 2 * Copyright (c) 2021-2022 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 panda::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()43 Arch GetArch() const 44 { 45 return arch_; 46 } 47 48 private: 49 void CollectUsedRegisters(panda::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 panda::irtoc 69 70 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 71 #define COMPILE(name) \ 72 class name : public Function { \ 73 public: \ 74 using Function::Function; \ 75 void MakeGraphImpl() override; \ 76 const char *GetName() const override \ 77 { \ 78 return #name; \ 79 } \ 80 \ 81 private: \ 82 static int dummy; \ 83 }; \ 84 int name ::dummy = Compilation::RegisterUnit<name>(); \ 85 void name ::MakeGraphImpl() 86 87 #endif // PANDA_IRTOC_COMPILATION_H 88