• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPILATION_ENTRY_H
17 #define PANDA_COMPILATION_ENTRY_H
18 
19 #include "compiler/optimizer/ir/ir_constructor.h"
20 #include "compiler/optimizer/code_generator/relocations.h"
21 #include "utils/expected.h"
22 #include "asm_defines.h"
23 #include "cross_values.h"
24 #include "runtime/include/managed_thread.h"
25 #include "source_languages.h"
26 #include "runtime/include/thread.h"
27 #include "runtime/include/coretypes/tagged_value.h"
28 
29 namespace panda::irtoc {
30 
31 using compiler::Graph;
32 
33 class CompilationUnit : public compiler::RelocationHandler {
34 public:
35     using Result = Expected<int, const char *>;
36 
CompilationUnit()37     CompilationUnit() {}
38 
39     virtual ~CompilationUnit() = default;
40 
41     virtual void MakeGraphImpl() = 0;
42     virtual const char *GetName() = 0;
43 
44     Result Compile(Arch arch);
45 
GetCode()46     auto GetCode()
47     {
48         return Span(code_);
49     }
50 
GetGraph()51     Graph *GetGraph()
52     {
53         return graph_;
54     }
55 
GetGraph()56     const Graph *GetGraph() const
57     {
58         return graph_;
59     }
60 
WordSize()61     size_t WordSize() const
62     {
63         return PointerSize(GetArch());
64     }
65 
66     void AddRelocation(const compiler::RelocationInfo &info) override;
67 
GetRelocations()68     const auto &GetRelocations() const
69     {
70         return relocation_entries_;
71     }
72 
GetExternalFunction(size_t index)73     const char *GetExternalFunction(size_t index) const
74     {
75         CHECK_LT(index, external_functions_.size());
76         return external_functions_[index].c_str();
77     }
78 
GetLanguage()79     compiler::SourceLanguage GetLanguage() const
80     {
81         return lang_;
82     }
83 
84 protected:
GetArch()85     Arch GetArch() const
86     {
87         return GetGraph()->GetArch();
88     }
89 
GetRuntime()90     compiler::RuntimeInterface *GetRuntime()
91     {
92         return GetGraph()->GetRuntime();
93     }
94 
SetExternalFunctions(std::initializer_list<std::string> funcs)95     void SetExternalFunctions(std::initializer_list<std::string> funcs)
96     {
97         external_functions_ = funcs;
98     }
99 
SetLanguage(compiler::SourceLanguage lang)100     void SetLanguage(compiler::SourceLanguage lang)
101     {
102         lang_ = lang;
103     }
104 
105 protected:
106     std::unique_ptr<compiler::IrConstructor> builder_;
107 
108 private:
109     Graph *graph_ {nullptr};
110     compiler::SourceLanguage lang_ {compiler::SourceLanguage::PANDA_ASSEMBLY};
111     std::vector<uint8_t> code_;
112     std::vector<std::string> external_functions_;
113     std::vector<compiler::RelocationInfo> relocation_entries_;
114 };
115 
116 class Compilation {
117 public:
118     using Result = Expected<int, const char *>;
119     Result Run(std::string_view output);
120 
121     Result MakeElf(std::string_view output);
122 
123     template <typename T, typename... Args>
RegisterUnit(Args &&...args)124     static int RegisterUnit(Args &&... args)
125     {
126         static_assert(std::is_base_of_v<CompilationUnit, T>);
127         units_.push_back(new T(std::forward<Args>(args)...));
128         return 0;
129     }
130 
131 private:
132     Result Compile();
133 
134 private:
135     static inline std::vector<CompilationUnit *> units_;
136     Arch arch_ {RUNTIME_ARCH};
137     std::regex methods_regex_;
138 };
139 
140 }  // namespace panda::irtoc
141 
142 #define COMPILE(name)                                     \
143     class name : public CompilationUnit {                 \
144     public:                                               \
145         using CompilationUnit::CompilationUnit;           \
146         void MakeGraphImpl() override;                    \
147         const char *GetName() override                    \
148         {                                                 \
149             return #name;                                 \
150         }                                                 \
151                                                           \
152     private:                                              \
153         static int dummy;                                 \
154     };                                                    \
155     int name ::dummy = Compilation::RegisterUnit<name>(); \
156     void name ::MakeGraphImpl()
157 
158 #endif  // PANDA_COMPILATION_ENTRY_H
159