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 /*
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_AARCH32
29 #include "aarch32/target.h"
30 #endif
31
32 #ifdef PANDA_COMPILER_TARGET_AARCH64
33 #include "aarch64/target.h"
34 #endif
35
36 #ifdef PANDA_COMPILER_TARGET_X86_64
37 #include "amd64/target.h"
38 #endif
39
40 #include "asm_printer.h"
41
42 #include "frame_info.h"
43
44 namespace panda::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 * arena_allocator,Arch arch,bool print_asm,bool js_number_cast)69 Encoder *Encoder::Create([[maybe_unused]] ArenaAllocator *arena_allocator, [[maybe_unused]] Arch arch,
70 [[maybe_unused]] bool print_asm, [[maybe_unused]] bool js_number_cast)
71 {
72 switch (arch) {
73 #ifdef PANDA_COMPILER_TARGET_AARCH32
74 case Arch::AARCH32: {
75 aarch32::Aarch32Encoder *enc = arena_allocator->New<aarch32::Aarch32Encoder>(arena_allocator);
76 enc->SetIsJsNumberCast(js_number_cast);
77 if (print_asm) {
78 return arena_allocator->New<aarch32::Aarch32Assembly>(arena_allocator, enc);
79 }
80 return enc;
81 }
82 #endif
83 #ifdef PANDA_COMPILER_TARGET_AARCH64
84 case Arch::AARCH64: {
85 aarch64::Aarch64Encoder *enc = arena_allocator->New<aarch64::Aarch64Encoder>(arena_allocator);
86 enc->SetIsJsNumberCast(js_number_cast);
87 if (print_asm) {
88 return arena_allocator->New<aarch64::Aarch64Assembly>(arena_allocator, enc);
89 }
90 return enc;
91 }
92 #endif
93 #ifdef PANDA_COMPILER_TARGET_X86_64
94 case Arch::X86_64: {
95 amd64::Amd64Encoder *enc =
96 arena_allocator->New<amd64::Amd64Encoder>(arena_allocator, Arch::X86_64, js_number_cast);
97 if (print_asm) {
98 return arena_allocator->New<amd64::Amd64Assembly>(arena_allocator, enc);
99 }
100 return enc;
101 }
102 #endif
103 default:
104 return nullptr;
105 }
106 }
107
Create(ArenaAllocator * arena_allocator,Arch arch)108 RegistersDescription *RegistersDescription::Create([[maybe_unused]] ArenaAllocator *arena_allocator,
109 [[maybe_unused]] Arch arch)
110 {
111 switch (arch) {
112 #ifdef PANDA_COMPILER_TARGET_AARCH32
113 case Arch::AARCH32: {
114 return arena_allocator->New<aarch32::Aarch32RegisterDescription>(arena_allocator);
115 }
116 #endif
117
118 #ifdef PANDA_COMPILER_TARGET_AARCH64
119 case Arch::AARCH64: {
120 return arena_allocator->New<aarch64::Aarch64RegisterDescription>(arena_allocator);
121 }
122 #endif
123 #ifdef PANDA_COMPILER_TARGET_X86_64
124 case Arch::X86_64: {
125 return arena_allocator->New<amd64::Amd64RegisterDescription>(arena_allocator);
126 }
127 #endif
128 default:
129 return nullptr;
130 }
131 }
132
Create(ArenaAllocator * arena_allocator,Encoder * enc,RegistersDescription * descr,Arch arch,bool is_panda_abi,bool is_osr,bool is_dyn,bool print_asm)133 CallingConvention *CallingConvention::Create([[maybe_unused]] ArenaAllocator *arena_allocator,
134 [[maybe_unused]] Encoder *enc,
135 [[maybe_unused]] RegistersDescription *descr, [[maybe_unused]] Arch arch,
136 bool is_panda_abi, bool is_osr, bool is_dyn,
137 [[maybe_unused]] bool print_asm)
138 {
139 [[maybe_unused]] auto mode =
140 CallConvMode::Panda(is_panda_abi) | CallConvMode::Osr(is_osr) | CallConvMode::Dyn(is_dyn);
141 switch (arch) {
142 #ifdef PANDA_COMPILER_TARGET_AARCH32
143 case Arch::AARCH32: {
144 if (print_asm) {
145 using printer_type =
146 PrinterCallingConvention<aarch32::Aarch32CallingConvention, aarch32::Aarch32Assembly>;
147 return arena_allocator->New<printer_type>(
148 arena_allocator, reinterpret_cast<aarch32::Aarch32Assembly *>(enc), descr, mode);
149 }
150 return arena_allocator->New<aarch32::Aarch32CallingConvention>(arena_allocator, enc, descr, mode);
151 }
152 #endif
153 #ifdef PANDA_COMPILER_TARGET_AARCH64
154 case Arch::AARCH64: {
155 if (print_asm) {
156 using printer_type =
157 PrinterCallingConvention<aarch64::Aarch64CallingConvention, aarch64::Aarch64Assembly>;
158 return arena_allocator->New<printer_type>(
159 arena_allocator, reinterpret_cast<aarch64::Aarch64Assembly *>(enc), descr, mode);
160 }
161 return arena_allocator->New<aarch64::Aarch64CallingConvention>(arena_allocator, enc, descr, mode);
162 }
163 #endif
164 #ifdef PANDA_COMPILER_TARGET_X86_64
165 case Arch::X86_64: {
166 if (print_asm) {
167 using printer_type = PrinterCallingConvention<amd64::Amd64CallingConvention, amd64::Amd64Assembly>;
168 return arena_allocator->New<printer_type>(arena_allocator,
169 reinterpret_cast<amd64::Amd64Assembly *>(enc), descr, mode);
170 }
171 return arena_allocator->New<amd64::Amd64CallingConvention>(arena_allocator, enc, descr, mode);
172 }
173 #endif
174 default:
175 return nullptr;
176 }
177 }
178 } // namespace panda::compiler
179