• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 /*
17    Compiler -> Pass(Codegen)
18         Encoder = Fabric->getAsm
19 
20 Define arch-specific interfaces
21 */
22 
23 #include "operands.h"
24 #include "encode.h"
25 #include "compiler/optimizer/code_generator/callconv.h"
26 #include "registers_description.h"
27 
28 #ifdef PANDA_COMPILER_TARGET_X86_64
29 #include "amd64/target.h"
30 #endif
31 
32 #ifdef PANDA_COMPILER_TARGET_AARCH32
33 #include "aarch32/target.h"
34 #endif
35 
36 #ifdef PANDA_COMPILER_TARGET_AARCH64
37 #include "aarch64/target.h"
38 #endif
39 
40 #include "asm_printer.h"
41 
42 #include "frame_info.h"
43 
44 namespace ark::compiler {
BackendSupport(Arch arch)45 bool BackendSupport(Arch arch)
46 {
47     switch (arch) {
48 #ifdef PANDA_COMPILER_TARGET_AARCH32
49         // NOLINTNEXTLINE(bugprone-branch-clone)
50         case Arch::AARCH32: {
51             return true;
52         }
53 #endif
54 #ifdef PANDA_COMPILER_TARGET_AARCH64
55         case Arch::AARCH64: {
56             return true;
57         }
58 #endif
59 #ifdef PANDA_COMPILER_TARGET_X86_64
60         case Arch::X86_64: {
61             return true;
62         }
63 #endif
64         default:
65             return false;
66     }
67 }
68 
Create(ArenaAllocator * arenaAllocator,Arch arch,bool printAsm,bool jsNumberCast)69 Encoder *Encoder::Create([[maybe_unused]] ArenaAllocator *arenaAllocator, [[maybe_unused]] Arch arch,
70                          [[maybe_unused]] bool printAsm, [[maybe_unused]] bool jsNumberCast)
71 {
72     switch (arch) {
73 #ifdef PANDA_COMPILER_TARGET_AARCH32
74         case Arch::AARCH32: {
75             aarch32::Aarch32Encoder *enc = arenaAllocator->New<aarch32::Aarch32Encoder>(arenaAllocator);
76             ASSERT(enc != nullptr);
77             enc->SetIsJsNumberCast(jsNumberCast);
78             if (printAsm) {
79                 return arenaAllocator->New<aarch32::Aarch32Assembly>(arenaAllocator, enc);
80             }
81             return enc;
82         }
83 #endif
84 #ifdef PANDA_COMPILER_TARGET_AARCH64
85         case Arch::AARCH64: {
86             aarch64::Aarch64Encoder *enc = arenaAllocator->New<aarch64::Aarch64Encoder>(arenaAllocator);
87             ASSERT(enc != nullptr);
88             enc->SetIsJsNumberCast(jsNumberCast);
89             if (printAsm) {
90                 return arenaAllocator->New<aarch64::Aarch64Assembly>(arenaAllocator, enc);
91             }
92             return enc;
93         }
94 #endif
95 #ifdef PANDA_COMPILER_TARGET_X86_64
96         case Arch::X86_64: {
97             amd64::Amd64Encoder *enc =
98                 arenaAllocator->New<amd64::Amd64Encoder>(arenaAllocator, Arch::X86_64, jsNumberCast);
99             ASSERT(enc != nullptr);
100             enc->SetIsJsNumberCast(jsNumberCast);
101             if (printAsm) {
102                 return arenaAllocator->New<amd64::Amd64Assembly>(arenaAllocator, enc);
103             }
104             return enc;
105         }
106 #endif
107         default:
108             return nullptr;
109     }
110 }
111 
Create(ArenaAllocator * arenaAllocator,Arch arch)112 RegistersDescription *RegistersDescription::Create([[maybe_unused]] ArenaAllocator *arenaAllocator,
113                                                    [[maybe_unused]] Arch arch)
114 {
115     switch (arch) {
116 #ifdef PANDA_COMPILER_TARGET_AARCH32
117         case Arch::AARCH32: {
118             return arenaAllocator->New<aarch32::Aarch32RegisterDescription>(arenaAllocator);
119         }
120 #endif
121 
122 #ifdef PANDA_COMPILER_TARGET_AARCH64
123         case Arch::AARCH64: {
124             return arenaAllocator->New<aarch64::Aarch64RegisterDescription>(arenaAllocator);
125         }
126 #endif
127 #ifdef PANDA_COMPILER_TARGET_X86_64
128         case Arch::X86_64: {
129             return arenaAllocator->New<amd64::Amd64RegisterDescription>(arenaAllocator);
130         }
131 #endif
132         default:
133             return nullptr;
134     }
135 }
136 
Create(ArenaAllocator * arenaAllocator,Encoder * enc,RegistersDescription * descr,Arch arch,bool isPandaAbi,bool isOsr,bool isDyn,bool printAsm,bool isOptIrtoc)137 CallingConvention *CallingConvention::Create([[maybe_unused]] ArenaAllocator *arenaAllocator,
138                                              [[maybe_unused]] Encoder *enc,
139                                              [[maybe_unused]] RegistersDescription *descr, [[maybe_unused]] Arch arch,
140                                              bool isPandaAbi, bool isOsr, bool isDyn, [[maybe_unused]] bool printAsm,
141                                              bool isOptIrtoc)
142 {
143     [[maybe_unused]] auto mode = CallConvMode::Panda(isPandaAbi) | CallConvMode::Osr(isOsr) |
144                                  CallConvMode::DynCall(isDyn) | CallConvMode::OptIrtoc(isOptIrtoc);
145     switch (arch) {
146 #ifdef PANDA_COMPILER_TARGET_AARCH32
147         case Arch::AARCH32: {
148             if (printAsm) {
149                 using PrinterType =
150                     PrinterCallingConvention<aarch32::Aarch32CallingConvention, aarch32::Aarch32Assembly>;
151                 return arenaAllocator->New<PrinterType>(arenaAllocator,
152                                                         reinterpret_cast<aarch32::Aarch32Assembly *>(enc), descr, mode);
153             }
154             return arenaAllocator->New<aarch32::Aarch32CallingConvention>(arenaAllocator, enc, descr, mode);
155         }
156 #endif
157 #ifdef PANDA_COMPILER_TARGET_AARCH64
158         case Arch::AARCH64: {
159             if (printAsm) {
160                 using PrinterType =
161                     PrinterCallingConvention<aarch64::Aarch64CallingConvention, aarch64::Aarch64Assembly>;
162                 return arenaAllocator->New<PrinterType>(arenaAllocator,
163                                                         reinterpret_cast<aarch64::Aarch64Assembly *>(enc), descr, mode);
164             }
165             return arenaAllocator->New<aarch64::Aarch64CallingConvention>(arenaAllocator, enc, descr, mode);
166         }
167 #endif
168 #ifdef PANDA_COMPILER_TARGET_X86_64
169         case Arch::X86_64: {
170             if (printAsm) {
171                 using PrinterType = PrinterCallingConvention<amd64::Amd64CallingConvention, amd64::Amd64Assembly>;
172                 return arenaAllocator->New<PrinterType>(arenaAllocator, reinterpret_cast<amd64::Amd64Assembly *>(enc),
173                                                         descr, mode);
174             }
175             return arenaAllocator->New<amd64::Amd64CallingConvention>(arenaAllocator, enc, descr, mode);
176         }
177 #endif
178         default:
179             return nullptr;
180     }
181 }
182 }  // namespace ark::compiler
183