blob: 80bacb2be3f3a9f1892ce80b056305cce3967a92 [file] [log] [blame]
Serban Constantinescue6622be2014-02-27 15:36:47 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "disassembler_arm64.h"
18
19#include <inttypes.h>
20
Zheng Xua34e7602015-02-03 12:03:15 +080021#include <sstream>
Serban Constantinescue6622be2014-02-27 15:36:47 +000022
23#include "base/logging.h"
24#include "base/stringprintf.h"
Serban Constantinescue6622be2014-02-27 15:36:47 +000025
Scott Wakeling97c72b72016-06-24 16:19:36 +010026using namespace vixl::aarch64; // NOLINT(build/namespaces)
27
Serban Constantinescue6622be2014-02-27 15:36:47 +000028namespace art {
29namespace arm64 {
30
Zheng Xua34e7602015-02-03 12:03:15 +080031// This enumeration should mirror the declarations in
32// runtime/arch/arm64/registers_arm64.h. We do not include that file to
33// avoid a dependency on libart.
34enum {
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010035 TR = 19,
Zheng Xua34e7602015-02-03 12:03:15 +080036 IP0 = 16,
37 IP1 = 17,
38 FP = 29,
39 LR = 30
40};
41
Scott Wakeling97c72b72016-06-24 16:19:36 +010042void CustomDisassembler::AppendRegisterNameToOutput(const Instruction* instr,
43 const CPURegister& reg) {
Alexandre Ramesa37d9252014-10-27 11:28:14 +000044 USE(instr);
Alexandre Ramesd737ab32015-03-06 09:11:12 +000045 if (reg.IsRegister() && reg.Is64Bits()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010046 if (reg.GetCode() == TR) {
Alexandre Ramesd737ab32015-03-06 09:11:12 +000047 AppendToOutput("tr");
48 return;
Scott Wakeling97c72b72016-06-24 16:19:36 +010049 } else if (reg.GetCode() == LR) {
Alexandre Ramesd737ab32015-03-06 09:11:12 +000050 AppendToOutput("lr");
51 return;
Alexandre Ramesa37d9252014-10-27 11:28:14 +000052 }
Alexandre Ramesd737ab32015-03-06 09:11:12 +000053 // Fall through.
Alexandre Ramesa37d9252014-10-27 11:28:14 +000054 }
55 // Print other register names as usual.
56 Disassembler::AppendRegisterNameToOutput(instr, reg);
57}
58
Scott Wakeling97c72b72016-06-24 16:19:36 +010059void CustomDisassembler::VisitLoadLiteral(const Instruction* instr) {
Alexandre Ramesa37d9252014-10-27 11:28:14 +000060 Disassembler::VisitLoadLiteral(instr);
61
62 if (!read_literals_) {
63 return;
64 }
65
Aart Bikd3059e72016-05-11 10:30:47 -070066 // Get address of literal. Bail if not within expected buffer range to
67 // avoid trying to fetch invalid literals (we can encounter this when
68 // interpreting raw data as instructions).
Scott Wakeling97c72b72016-06-24 16:19:36 +010069 void* data_address = instr->GetLiteralAddress<void*>();
Aart Bikd3059e72016-05-11 10:30:47 -070070 if (data_address < base_address_ || data_address >= end_address_) {
71 AppendToOutput(" (?)");
72 return;
73 }
Alexandre Ramesa37d9252014-10-27 11:28:14 +000074
Aart Bikd3059e72016-05-11 10:30:47 -070075 // Output information on literal.
Scott Wakeling97c72b72016-06-24 16:19:36 +010076 Instr op = instr->Mask(LoadLiteralMask);
Alexandre Ramesa37d9252014-10-27 11:28:14 +000077 switch (op) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010078 case LDR_w_lit:
79 case LDR_x_lit:
80 case LDRSW_x_lit: {
81 int64_t data = op == LDR_x_lit ? *reinterpret_cast<int64_t*>(data_address)
82 : *reinterpret_cast<int32_t*>(data_address);
Zheng Xua34e7602015-02-03 12:03:15 +080083 AppendToOutput(" (0x%" PRIx64 " / %" PRId64 ")", data, data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +000084 break;
85 }
Scott Wakeling97c72b72016-06-24 16:19:36 +010086 case LDR_s_lit:
87 case LDR_d_lit: {
88 double data = (op == LDR_s_lit) ? *reinterpret_cast<float*>(data_address)
89 : *reinterpret_cast<double*>(data_address);
Zheng Xua34e7602015-02-03 12:03:15 +080090 AppendToOutput(" (%g)", data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +000091 break;
92 }
93 default:
94 break;
95 }
96}
97
Scott Wakeling97c72b72016-06-24 16:19:36 +010098void CustomDisassembler::VisitLoadStoreUnsignedOffset(const Instruction* instr) {
Zheng Xua34e7602015-02-03 12:03:15 +080099 Disassembler::VisitLoadStoreUnsignedOffset(instr);
100
Scott Wakeling97c72b72016-06-24 16:19:36 +0100101 if (instr->GetRn() == TR) {
102 int64_t offset = instr->GetImmLSUnsigned() << instr->GetSizeLS();
Zheng Xua34e7602015-02-03 12:03:15 +0800103 std::ostringstream tmp_stream;
Andreas Gampe372f3a32016-08-19 10:49:06 -0700104 options_->thread_offset_name_function_(tmp_stream, static_cast<uint32_t>(offset));
Alexandre Rames5e2c8d32015-08-06 14:49:28 +0100105 AppendToOutput(" ; %s", tmp_stream.str().c_str());
Zheng Xua34e7602015-02-03 12:03:15 +0800106 }
107}
108
Serban Constantinescue6622be2014-02-27 15:36:47 +0000109size_t DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100110 const Instruction* instr = reinterpret_cast<const Instruction*>(begin);
Alexandre Ramesfef019c2014-10-10 17:14:18 +0100111 decoder.Decode(instr);
Alexandre Ramesd737ab32015-03-06 09:11:12 +0000112 os << FormatInstructionPointer(begin)
Scott Wakeling97c72b72016-06-24 16:19:36 +0100113 << StringPrintf(": %08x\t%s\n", instr->GetInstructionBits(), disasm.GetOutput());
114 return kInstructionSize;
Serban Constantinescue6622be2014-02-27 15:36:47 +0000115}
116
117void DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100118 for (const uint8_t* cur = begin; cur < end; cur += kInstructionSize) {
Serban Constantinescue6622be2014-02-27 15:36:47 +0000119 Dump(os, cur);
120 }
121}
122
123} // namespace arm64
124} // namespace art