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