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
19namespace panda::pandasm {
20
21std::string panda::pandasm::Ins::ToString(std::string endline, bool print_args /* = false */,
22                                          size_t first_arg_idx /* = 0*/) const {
23    std::string full_operation = "";
24    std::string reg_case = "";
25    if (this->set_label) {
26        full_operation += this->label +": ";
27    }
28    switch(this->opcode) {
29
30% Panda::instructions.group_by(&:mnemonic).each do |mnemonic, group|
31%  insn = group.first
32%  formats = group.map(&:format)
33%  sig_split = insn["sig"].split(' ')
34%  operands = insn.operands
35%  properties = insn.properties
36        case panda::pandasm::Opcode::<%= insn.asm_token%>: {
37            full_operation += "<%= insn.mnemonic%>";
38% idx_imm = 0
39% idx_reg = 0
40% idx_ids = 0
41% sig_split.length.times do |index|
42% item = sig_split[index]
43% next if index == 0
44% #TODO(knazarov): refactor next line
45% if (item.include?("id") || item.start_with?("imm") && insn.jump?)
46            full_operation += IdToString(<%= idx_ids%>, <%= index == 1 ? "true" : "false"%>);
47%           idx_ids += 1
48% elsif item.start_with?("imm")
49            full_operation += ImmToString(<%= idx_imm%>, <%= index == 1 ? "true" : "false"%>);
50%           idx_imm += 1
51% elsif item.start_with?("v")
52            full_operation += RegToString(<%= idx_reg%>, <%= index == 1 ? "true" : "false"%>, print_args, first_arg_idx);
53%           idx_reg += 1
54% end
55% end
56        } break;
57% end
58% Panda::pseudo_instructions.each do |insn|
59        case panda::pandasm::Opcode::<%= insn.opcode %>: {
60            full_operation += "<%= insn.opcode %>";
61            full_operation += this->OperandsToString(print_args, first_arg_idx);
62        } break;
63% end
64        case panda::pandasm::Opcode::INVALID: {
65            full_operation += "";
66        } break;
67    }
68    return full_operation + endline;
69}
70
71} // namespace panda::pandasm
72