• 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 "assembly-ins.h"
17 
18 namespace panda::pandasm {
19 
20 const std::string Ins::EMPTY_STRING = "";
21 
RegsToString(bool & first,bool print_args,size_t first_arg_idx) const22 std::string panda::pandasm::Ins::RegsToString(bool &first, bool print_args, size_t first_arg_idx) const
23 {
24     std::stringstream translator;
25     for (const auto &reg : this->Regs()) {
26         if (!first) {
27             translator << ",";
28         } else {
29             first = false;
30         }
31 
32         if (print_args && reg >= first_arg_idx) {
33             translator << " a" << reg - first_arg_idx;
34         } else {
35             translator << " v" << reg;
36         }
37     }
38     return translator.str();
39 }
40 
ImmsToString(bool & first) const41 std::string panda::pandasm::Ins::ImmsToString(bool &first) const
42 {
43     std::stringstream translator;
44     for (const auto &imm : this->Imms()) {
45         if (!first) {
46             translator << ",";
47         } else {
48             first = false;
49         }
50 
51         auto *number = std::get_if<double>(&imm);
52         if (number != nullptr) {
53             translator << " " << std::scientific << *number;
54         } else {
55             translator << " 0x" << std::hex << std::get<int64_t>(imm);
56         }
57         translator.clear();
58     }
59     return translator.str();
60 }
61 
IdsToString(bool & first) const62 std::string panda::pandasm::Ins::IdsToString(bool &first) const
63 {
64     std::stringstream translator;
65     for (const auto &id : this->Ids()) {
66         if (!first) {
67             translator << ",";
68         } else {
69             first = false;
70         }
71 
72         translator << " " << id;
73     }
74     return translator.str();
75 }
76 
OperandsToString(bool print_args,size_t first_arg_idx) const77 std::string panda::pandasm::Ins::OperandsToString(bool print_args, size_t first_arg_idx) const
78 {
79     bool first = true;
80     std::stringstream ss {};
81 
82     ss << this->RegsToString(first, print_args, first_arg_idx) << this->ImmsToString(first) << this->IdsToString(first);
83 
84     return ss.str();
85 }
86 
RegToString(size_t idx,bool is_first,bool print_args,size_t first_arg_idx) const87 std::string panda::pandasm::Ins::RegToString(size_t idx, bool is_first, bool print_args,
88                                              size_t first_arg_idx) const
89 {
90     if (idx >= Regs().size()) {
91         return std::string("");
92     }
93 
94     std::stringstream translator;
95 
96     if (!is_first) {
97         translator << ", ";
98     } else {
99         translator << " ";
100     }
101 
102     if (print_args && Regs()[idx] >= first_arg_idx) {
103         translator << "a" << Regs()[idx] - first_arg_idx;
104     } else {
105         translator << "v" << Regs()[idx];
106     }
107 
108     return translator.str();
109 }
110 
ImmToString(size_t idx,bool is_first) const111 std::string panda::pandasm::Ins::ImmToString(size_t idx, bool is_first) const
112 {
113     if (idx >= Imms().size()) {
114         return std::string("");
115     }
116 
117     auto imm = Imms()[idx];
118     auto *number = std::get_if<double>(&(imm));
119     std::stringstream translator;
120 
121     if (!is_first) {
122         translator << ", ";
123     } else {
124         translator << " ";
125     }
126 
127     if (number != nullptr) {
128         translator << std::scientific << *number;
129     } else {
130         translator << "0x" << std::hex << std::get<int64_t>(imm);
131     }
132 
133     return translator.str();
134 }
135 
IdToString(size_t idx,bool is_first) const136 std::string panda::pandasm::Ins::IdToString(size_t idx, bool is_first) const
137 {
138     if (idx >= Ids().size()) {
139         return std::string("");
140     }
141 
142     std::stringstream translator;
143 
144     if (!is_first) {
145         translator << ", ";
146     } else {
147         translator << " ";
148     }
149 
150     translator << Ids()[idx];
151 
152     return translator.str();
153 }
154 }  // namespace panda::pandasm
155