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