Debian Codegraph @

Source/Core/Common/Assembler/GekkoAssembler.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/Assembler/GekkoAssembler.h"
5
6 #include <array>
7 #include <string>
8 #include <vector>
9
10 #include <fmt/format.h>
11
12 #include "Common/Assembler/AssemblerShared.h"
13 #include "Common/Assembler/AssemblerTables.h"
14 #include "Common/Assembler/GekkoIRGen.h"
15 #include "Common/Assert.h"
16 #include "Common/CommonTypes.h"
17
18 namespace Common::GekkoAssembler
19 {
20 namespace
21 {
22 using namespace Common::GekkoAssembler::detail;
23
24 FailureOr<u32> FillInstruction(const MnemonicDesc& desc, const OperandList& operands,
25 std::string_view inst_line)
26 {
27 // Parser shouldn't allow this to pass
28 ASSERT_MSG(COMMON, desc.operand_count == operands.count && !operands.overfill,
29 "Unexpected operand count mismatch for instruction {}. Expected {} but found {}",
30 inst_line, desc.operand_count, operands.overfill ? 6 : operands.count);
31
32 u32 instruction = desc.initial_value;
33 for (u32 i = 0; i < operands.count; i++)
34 {
35 if (!desc.operand_masks[i].Fits(operands[i]))
36 {
37 std::string message;
38 const u32 trunc_bits = desc.operand_masks[i].TruncBits();
39 if (trunc_bits == 0)
40 {
41 if (desc.operand_masks[i].is_signed)
42 {
43 message = fmt::format("{:#x} not between {:#x} and {:#x}", static_cast<s32>(operands[i]),
44 static_cast<s32>(desc.operand_masks[i].MinVal()),
45 static_cast<s32>(desc.operand_masks[i].MaxVal()));
46 }
47 else
48 {
49 message = fmt::format("{:#x} not between {:#x} and {:#x}", operands[i],
50 desc.operand_masks[i].MinVal(), desc.operand_masks[i].MaxVal());
51 }
52 }
53 else
54 {
55 if (desc.operand_masks[i].is_signed)
56 {
57 message = fmt::format("{:#x} not between {:#x} and {:#x} or not aligned to {}",
58 static_cast<s32>(operands[i]),
59 static_cast<s32>(desc.operand_masks[i].MinVal()),
60 static_cast<s32>(desc.operand_masks[i].MaxVal()), trunc_bits + 1);
61 }
62 else
63 {
64 message = fmt::format("{:#x} not between {:#x} and {:#x} or not aligned to {}",
65 operands[i], desc.operand_masks[i].MinVal(),
66 desc.operand_masks[i].MaxVal(), trunc_bits + 1);
67 }
68 }
69 return AssemblerError{std::move(message), "", 0, TagOf(operands.list[i]).begin,
70 TagOf(operands.list[i]).len};
71 }
72 instruction |= desc.operand_masks[i].Fit(operands[i]);
73 }
74 return instruction;
75 }
76
77 void AdjustOperandsForGas(GekkoMnemonic mnemonic, OperandList& ops_list)
78 {
79 switch (mnemonic)
80 {
81 case GekkoMnemonic::Cmp:
82 case GekkoMnemonic::Cmpl:
83 case GekkoMnemonic::Cmpi:
84 case GekkoMnemonic::Cmpli:
85 if (ops_list.count < 4)
86 {
87 ops_list.Insert(0, 0);
88 }
89 break;
90
91 case GekkoMnemonic::Addis:
92 // Because GAS wants to allow for addis and lis to work nice with absolute addresses, the
93 // immediate operand should also "fit" into the _UIMM field, so just turn a valid UIMM into a
94 // SIMM
95 if (ops_list[2] >= 0x8000 && ops_list[2] <= 0xffff)
96 {
97 ops_list[2] = ops_list[2] - 0x10000;
98 }
99 break;
100
101 default:
102 break;
103 }
104 }
105
106 } // namespace
107
108 void CodeBlock::PushBigEndian(u32 val)
109 {
110 instructions.push_back((val >> 24) & 0xff);
111 instructions.push_back((val >> 16) & 0xff);
112 instructions.push_back((val >> 8) & 0xff);
113 instructions.push_back(val & 0xff);
114 }
115
116 FailureOr<std::vector<CodeBlock>> Assemble(std::string_view instruction,
117 u32 current_instruction_address)
118 {
119 FailureOr<detail::GekkoIR> parse_result =
120 detail::ParseToIR(instruction, current_instruction_address);
121 if (IsFailure(parse_result))
122 {
123 return GetFailure(parse_result);
124 }
125
126 const auto& parsed_blocks = GetT(parse_result).blocks;
127 const auto& operands = GetT(parse_result).operand_pool;
128 std::vector<CodeBlock> out_blocks;
129
130 for (const detail::IRBlock& parsed_block : parsed_blocks)
131 {
132 CodeBlock new_block(parsed_block.block_address);
133 for (const detail::ChunkVariant& chunk : parsed_block.chunks)
134 {
135 if (std::holds_alternative<detail::InstChunk>(chunk))
136 {
137 for (const detail::GekkoInstruction& parsed_inst : std::get<detail::InstChunk>(chunk))
138 {
139 OperandList adjusted_ops;
140 ASSERT(parsed_inst.op_interval.len <= MAX_OPERANDS);
141 adjusted_ops.Copy(operands.begin() + parsed_inst.op_interval.begin,
142 operands.begin() + parsed_inst.op_interval.End());
143
144 size_t idx = parsed_inst.mnemonic_index;
145 if (parsed_inst.is_extended)
146 {
147 extended_mnemonics[idx].transform_operands(adjusted_ops);
148 idx = extended_mnemonics[idx].mnemonic_index;
149 }
150
151 AdjustOperandsForGas(static_cast<GekkoMnemonic>(idx >> 2), adjusted_ops);
152
153 FailureOr<u32> inst = FillInstruction(mnemonics[idx], adjusted_ops, parsed_inst.raw_text);
154 if (IsFailure(inst))
155 {
156 GetFailure(inst).error_line = parsed_inst.raw_text;
157 GetFailure(inst).line = parsed_inst.line_number;
158 return GetFailure(inst);
159 }
160
161 new_block.PushBigEndian(GetT(inst));
162 }
163 }
164 else if (std::holds_alternative<detail::ByteChunk>(chunk))
165 {
166 detail::ByteChunk byte_arr = std::get<detail::ByteChunk>(chunk);
167 new_block.instructions.insert(new_block.instructions.end(), byte_arr.begin(),
168 byte_arr.end());
169 }
170 else if (std::holds_alternative<detail::PadChunk>(chunk))
171 {
172 detail::PadChunk pad_len = std::get<detail::PadChunk>(chunk);
173 new_block.instructions.insert(new_block.instructions.end(), pad_len, 0);
174 }
175 else
176 {
177 ASSERT(false);
178 }
179 }
180
181 if (!new_block.instructions.empty())
182 {
183 out_blocks.emplace_back(std::move(new_block));
184 }
185 }
186 return out_blocks;
187 }
188 } // namespace Common::GekkoAssembler