1b1994897Sopenharmony_ci/**
2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#include <iostream>
17b1994897Sopenharmony_ci#include "assembly-ins.h"
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_cinamespace panda::pandasm {
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_cistd::string panda::pandasm::Ins::RegsToString(bool &first, bool print_args, size_t first_arg_idx) const
22b1994897Sopenharmony_ci{
23b1994897Sopenharmony_ci    std::stringstream translator;
24b1994897Sopenharmony_ci    for (const auto &reg : this->regs) {
25b1994897Sopenharmony_ci        if (!first) {
26b1994897Sopenharmony_ci            translator << ",";
27b1994897Sopenharmony_ci        } else {
28b1994897Sopenharmony_ci            first = false;
29b1994897Sopenharmony_ci        }
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_ci        if (print_args && reg >= first_arg_idx) {
32b1994897Sopenharmony_ci            translator << " a" << reg - first_arg_idx;
33b1994897Sopenharmony_ci        } else {
34b1994897Sopenharmony_ci            translator << " v" << reg;
35b1994897Sopenharmony_ci        }
36b1994897Sopenharmony_ci    }
37b1994897Sopenharmony_ci    return translator.str();
38b1994897Sopenharmony_ci}
39b1994897Sopenharmony_ci
40b1994897Sopenharmony_cistd::string panda::pandasm::Ins::ImmsToString(bool &first) const
41b1994897Sopenharmony_ci{
42b1994897Sopenharmony_ci    std::stringstream translator;
43b1994897Sopenharmony_ci    for (const auto &imm : this->imms) {
44b1994897Sopenharmony_ci        if (!first) {
45b1994897Sopenharmony_ci            translator << ",";
46b1994897Sopenharmony_ci        } else {
47b1994897Sopenharmony_ci            first = false;
48b1994897Sopenharmony_ci        }
49b1994897Sopenharmony_ci
50b1994897Sopenharmony_ci        auto *number = std::get_if<double>(&imm);
51b1994897Sopenharmony_ci        if (number != nullptr) {
52b1994897Sopenharmony_ci            translator << " " << std::scientific << *number;
53b1994897Sopenharmony_ci        } else {
54b1994897Sopenharmony_ci            translator << " 0x" << std::hex << std::get<int64_t>(imm);
55b1994897Sopenharmony_ci        }
56b1994897Sopenharmony_ci        translator.clear();
57b1994897Sopenharmony_ci    }
58b1994897Sopenharmony_ci    return translator.str();
59b1994897Sopenharmony_ci}
60b1994897Sopenharmony_ci
61b1994897Sopenharmony_cistd::string panda::pandasm::Ins::IdsToString(bool &first) const
62b1994897Sopenharmony_ci{
63b1994897Sopenharmony_ci    std::stringstream translator;
64b1994897Sopenharmony_ci    for (const auto &id : this->ids) {
65b1994897Sopenharmony_ci        if (!first) {
66b1994897Sopenharmony_ci            translator << ",";
67b1994897Sopenharmony_ci        } else {
68b1994897Sopenharmony_ci            first = false;
69b1994897Sopenharmony_ci        }
70b1994897Sopenharmony_ci
71b1994897Sopenharmony_ci        translator << " " << id;
72b1994897Sopenharmony_ci    }
73b1994897Sopenharmony_ci    return translator.str();
74b1994897Sopenharmony_ci}
75b1994897Sopenharmony_ci
76b1994897Sopenharmony_cistd::string panda::pandasm::Ins::OperandsToString(bool print_args, size_t first_arg_idx) const
77b1994897Sopenharmony_ci{
78b1994897Sopenharmony_ci    bool first = true;
79b1994897Sopenharmony_ci    std::stringstream ss {};
80b1994897Sopenharmony_ci
81b1994897Sopenharmony_ci    ss << this->RegsToString(first, print_args, first_arg_idx) << this->ImmsToString(first) << this->IdsToString(first);
82b1994897Sopenharmony_ci
83b1994897Sopenharmony_ci    return ss.str();
84b1994897Sopenharmony_ci}
85b1994897Sopenharmony_ci
86b1994897Sopenharmony_cistd::string panda::pandasm::Ins::RegToString(size_t idx, bool is_first, bool print_args,
87b1994897Sopenharmony_ci                                             size_t first_arg_idx) const
88b1994897Sopenharmony_ci{
89b1994897Sopenharmony_ci    if (idx >= regs.size()) {
90b1994897Sopenharmony_ci        return std::string("");
91b1994897Sopenharmony_ci    }
92b1994897Sopenharmony_ci
93b1994897Sopenharmony_ci    std::stringstream translator;
94b1994897Sopenharmony_ci
95b1994897Sopenharmony_ci    if (!is_first) {
96b1994897Sopenharmony_ci        translator << ", ";
97b1994897Sopenharmony_ci    } else {
98b1994897Sopenharmony_ci        translator << " ";
99b1994897Sopenharmony_ci    }
100b1994897Sopenharmony_ci
101b1994897Sopenharmony_ci    if (print_args && regs[idx] >= first_arg_idx) {
102b1994897Sopenharmony_ci        translator << "a" << regs[idx] - first_arg_idx;
103b1994897Sopenharmony_ci    } else {
104b1994897Sopenharmony_ci        translator << "v" << regs[idx];
105b1994897Sopenharmony_ci    }
106b1994897Sopenharmony_ci
107b1994897Sopenharmony_ci    return translator.str();
108b1994897Sopenharmony_ci}
109b1994897Sopenharmony_ci
110b1994897Sopenharmony_cistd::string panda::pandasm::Ins::ImmToString(size_t idx, bool is_first) const
111b1994897Sopenharmony_ci{
112b1994897Sopenharmony_ci    if (idx >= imms.size()) {
113b1994897Sopenharmony_ci        return std::string("");
114b1994897Sopenharmony_ci    }
115b1994897Sopenharmony_ci
116b1994897Sopenharmony_ci    auto *number = std::get_if<double>(&(imms[idx]));
117b1994897Sopenharmony_ci    std::stringstream translator;
118b1994897Sopenharmony_ci
119b1994897Sopenharmony_ci    if (!is_first) {
120b1994897Sopenharmony_ci        translator << ", ";
121b1994897Sopenharmony_ci    } else {
122b1994897Sopenharmony_ci        translator << " ";
123b1994897Sopenharmony_ci    }
124b1994897Sopenharmony_ci
125b1994897Sopenharmony_ci    if (number != nullptr) {
126b1994897Sopenharmony_ci        translator << std::scientific << *number;
127b1994897Sopenharmony_ci    } else {
128b1994897Sopenharmony_ci        translator << "0x" << std::hex << std::get<int64_t>(imms[idx]);
129b1994897Sopenharmony_ci    }
130b1994897Sopenharmony_ci
131b1994897Sopenharmony_ci    return translator.str();
132b1994897Sopenharmony_ci}
133b1994897Sopenharmony_ci
134b1994897Sopenharmony_cistd::string panda::pandasm::Ins::IdToString(size_t idx, bool is_first) const
135b1994897Sopenharmony_ci{
136b1994897Sopenharmony_ci    if (idx >= ids.size()) {
137b1994897Sopenharmony_ci        return std::string("");
138b1994897Sopenharmony_ci    }
139b1994897Sopenharmony_ci
140b1994897Sopenharmony_ci    std::stringstream translator;
141b1994897Sopenharmony_ci
142b1994897Sopenharmony_ci    if (!is_first) {
143b1994897Sopenharmony_ci        translator << ", ";
144b1994897Sopenharmony_ci    } else {
145b1994897Sopenharmony_ci        translator << " ";
146b1994897Sopenharmony_ci    }
147b1994897Sopenharmony_ci
148b1994897Sopenharmony_ci    translator << ids[idx];
149b1994897Sopenharmony_ci
150b1994897Sopenharmony_ci    return translator.str();
151b1994897Sopenharmony_ci}
152b1994897Sopenharmony_ci}  // namespace panda::pandasm
153