1 /*
2 * Copyright (c) 2021-2024 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 ark::pandasm {
20
RegsToString(bool & first,bool printArgs,size_t firstArgIdx) const21 std::string ark::pandasm::Ins::RegsToString(bool &first, bool printArgs, size_t firstArgIdx) 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 (printArgs && reg >= firstArgIdx) {
32 translator << " a" << reg - firstArgIdx;
33 } else {
34 translator << " v" << reg;
35 }
36 }
37 return translator.str();
38 }
39
ImmsToString(bool & first) const40 std::string ark::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 ark::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 printArgs,size_t firstArgIdx) const76 std::string ark::pandasm::Ins::OperandsToString(bool printArgs, size_t firstArgIdx) const
77 {
78 bool first = true;
79 std::stringstream ss {};
80
81 ss << this->RegsToString(first, printArgs, firstArgIdx) << this->ImmsToString(first) << this->IdsToString(first);
82
83 return ss.str();
84 }
85
RegToString(size_t idx,bool isFirst,bool printArgs,size_t firstArgIdx) const86 std::string ark::pandasm::Ins::RegToString(size_t idx, bool isFirst, bool printArgs, size_t firstArgIdx) const
87 {
88 if (idx >= regs.size()) {
89 return std::string("");
90 }
91
92 std::stringstream translator;
93
94 if (!isFirst) {
95 translator << ", ";
96 } else {
97 translator << " ";
98 }
99
100 if (printArgs && regs[idx] >= firstArgIdx) {
101 translator << "a" << regs[idx] - firstArgIdx;
102 } else {
103 translator << "v" << regs[idx];
104 }
105
106 return translator.str();
107 }
108
ImmToString(size_t idx,bool isFirst) const109 std::string ark::pandasm::Ins::ImmToString(size_t idx, bool isFirst) 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 (!isFirst) {
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 isFirst) const133 std::string ark::pandasm::Ins::IdToString(size_t idx, bool isFirst) const
134 {
135 if (idx >= ids.size()) {
136 return std::string("");
137 }
138
139 std::stringstream translator;
140
141 if (!isFirst) {
142 translator << ", ";
143 } else {
144 translator << " ";
145 }
146
147 translator << ids[idx];
148
149 return translator.str();
150 }
151 } // namespace ark::pandasm
152