• 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 #include "asm_printer.h"
17 
18 namespace panda::compiler {
19 #ifdef PANDA_COMPILER_TARGET_AARCH32
20 template <>
BindLabel(LabelId id)21 void PrinterLabelHolder<AssemblyPrinter<aarch32::Aarch32Encoder>>::BindLabel(LabelId id)
22 {
23     auto encoder = reinterpret_cast<AssemblyPrinter<aarch32::Aarch32Encoder> *>(GetEncoder());
24     auto stream = encoder->GetStream();
25     auto str = labels_[id];
26     *stream << "." << str << ":\n";
27 }
28 template <>
AssemblyPrinter(ArenaAllocator * aa,aarch32::Aarch32Encoder * enc)29 AssemblyPrinter<aarch32::Aarch32Encoder>::AssemblyPrinter(ArenaAllocator *aa, aarch32::Aarch32Encoder *enc)
30     : Encoder(aa, enc->GetArch()), enc_(enc)
31 {
32     labels_ = aa->template New<PrinterLabelHolder<AssemblyPrinter<aarch32::Aarch32Encoder>>>(this);
33 }
34 
35 template <>
MakeCall(const void * entry_point)36 void AssemblyPrinter<aarch32::Aarch32Encoder>::MakeCall(const void *entry_point)
37 {
38     *str_ << "blx " << std::hex << entry_point << "\n";
39 }
40 #endif
41 
42 #ifdef PANDA_COMPILER_TARGET_AARCH64
43 template <>
BindLabel(LabelId id)44 void PrinterLabelHolder<AssemblyPrinter<aarch64::Aarch64Encoder>>::BindLabel(LabelId id)
45 {
46     auto encoder = reinterpret_cast<AssemblyPrinter<aarch64::Aarch64Encoder> *>(GetEncoder());
47     auto stream = encoder->GetStream();
48     auto str = labels_[id];
49     *stream << "." << str << ":\n";
50 }
51 template <>
AssemblyPrinter(ArenaAllocator * aa,aarch64::Aarch64Encoder * enc)52 AssemblyPrinter<aarch64::Aarch64Encoder>::AssemblyPrinter(ArenaAllocator *aa, aarch64::Aarch64Encoder *enc)
53     : Encoder(aa, enc->GetArch()), enc_(enc)
54 {
55     labels_ = aa->template New<PrinterLabelHolder<AssemblyPrinter<aarch64::Aarch64Encoder>>>(this);
56 }
57 template <>
MakeCall(const void * entry_point)58 void AssemblyPrinter<aarch64::Aarch64Encoder>::MakeCall(const void *entry_point)
59 {
60     *str_ << "bl " << std::hex << entry_point << "\n";
61 }
62 #endif
63 
64 #ifdef PANDA_COMPILER_TARGET_X86_64
65 template <>
BindLabel(LabelId id)66 void PrinterLabelHolder<AssemblyPrinter<amd64::Amd64Encoder>>::BindLabel(LabelId id)
67 {
68     auto encoder = reinterpret_cast<AssemblyPrinter<amd64::Amd64Encoder> *>(GetEncoder());
69     auto stream = encoder->GetStream();
70     auto str = labels_[id];
71     *stream << "." << str << ":\n";
72 }
73 template <>
AssemblyPrinter(ArenaAllocator * aa,amd64::Amd64Encoder * enc)74 AssemblyPrinter<amd64::Amd64Encoder>::AssemblyPrinter(ArenaAllocator *aa, amd64::Amd64Encoder *enc)
75     : Encoder(aa, enc->GetArch()), enc_(enc)
76 {
77     labels_ = aa->template New<PrinterLabelHolder<AssemblyPrinter<amd64::Amd64Encoder>>>(this);
78 }
79 template <>
MakeCall(const void * entry_point)80 void AssemblyPrinter<amd64::Amd64Encoder>::MakeCall(const void *entry_point)
81 {
82     *str_ << "callq " << std::hex << entry_point << "@plt\n";
83 }
84 #endif
85 }  // namespace panda::compiler
86