• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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 &reg : 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(PrintKind print_kind,bool print_args,size_t first_arg_idx) const76 std::string panda::pandasm::Ins::OperandsToString(PrintKind print_kind, bool print_args, size_t first_arg_idx) const
77 {
78     bool first = true;
79     std::stringstream ss {};
80 
81     switch (print_kind) {
82         case PrintKind::CALL:
83             ss << this->IdsToString(first) << this->RegsToString(first, print_args, first_arg_idx);
84             if (!imms.empty()) {
85                 ss << ImmsToString(first);
86             }
87             break;
88         case PrintKind::CALLI:
89             ss << this->IdsToString(first) << this->ImmsToString(first)
90                << this->RegsToString(first, print_args, first_arg_idx);
91             break;
92         case PrintKind::DEFAULT:
93         default:
94             ss << this->RegsToString(first, print_args, first_arg_idx) << this->ImmsToString(first)
95                << this->IdsToString(first);
96     }
97 
98     return ss.str();
99 }
100 
101 }  // namespace panda::pandasm