13a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers/*
23a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * Copyright (C) 2012 The Android Open Source Project
33a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers *
43a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * Licensed under the Apache License, Version 2.0 (the "License");
53a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * you may not use this file except in compliance with the License.
63a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * You may obtain a copy of the License at
73a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers *
83a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers *      http://www.apache.org/licenses/LICENSE-2.0
93a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers *
103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * Unless required by applicable law or agreed to in writing, software
113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * distributed under the License is distributed on an "AS IS" BASIS,
123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * See the License for the specific language governing permissions and
143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers * limitations under the License.
153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers */
163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1702ed4c04468ca5f5540c5b704ac3e2f30eb9e8f4Ian Rogers#ifndef ART_DISASSEMBLER_DISASSEMBLER_H_
1802ed4c04468ca5f5540c5b704ac3e2f30eb9e8f4Ian Rogers#define ART_DISASSEMBLER_DISASSEMBLER_H_
193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
200f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes#include <stdint.h>
210f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes
220f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes#include <iosfwd>
230f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes
24761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
250f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes#include "instruction_set.h"
263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersnamespace art {
283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2934fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstromclass DisassemblerOptions {
3034fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom public:
3134fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  // Should the disassembler print absolute or relative addresses.
3234fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  const bool absolute_addresses_;
3334fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom
3434fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  // Base addess for calculating relative code offsets when absolute_addresses_ is false.
3534fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  const uint8_t* const base_address_;
3634fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom
3734fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  DisassemblerOptions(bool absolute_addresses, const uint8_t* base_address)
3834fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom      : absolute_addresses_(absolute_addresses), base_address_(base_address) {}
3934fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom
4034fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom private:
4134fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  DISALLOW_COPY_AND_ASSIGN(DisassemblerOptions);
4234fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom};
4334fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom
443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass Disassembler {
453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers public:
4634fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  // Creates a Disassembler for the given InstructionSet with the
4734fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  // non-null DisassemblerOptions which become owned by the
4834fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  // Disassembler.
4934fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  static Disassembler* Create(InstructionSet instruction_set, DisassemblerOptions* options);
5034fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom
5134fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  virtual ~Disassembler() {
5234fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom    delete disassembler_options_;
5334fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  }
543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
55b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  // Dump a single instruction returning the length of that instruction.
56b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  virtual size_t Dump(std::ostream& os, const uint8_t* begin) = 0;
57b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  // Dump instructions within a range.
583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  virtual void Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) = 0;
59105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes
60105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes protected:
6134fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  explicit Disassembler(DisassemblerOptions* disassembler_options)
6234fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom      : disassembler_options_(disassembler_options) {
6334fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom    CHECK(disassembler_options_ != nullptr);
6434fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  }
6534fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom
6634fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  std::string FormatInstructionPointer(const uint8_t* begin);
67105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes
68105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes private:
6934fa79ece5b3a1940d412cd94dbdcc4225aae72fBrian Carlstrom  DisassemblerOptions* disassembler_options_;
70105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes  DISALLOW_COPY_AND_ASSIGN(Disassembler);
713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers};
723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
733c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffraystatic inline bool HasBitSet(uint32_t value, uint32_t bit) {
743c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  return (value & (1 << bit)) != 0;
753c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray}
763c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers}  // namespace art
783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
7902ed4c04468ca5f5540c5b704ac3e2f30eb9e8f4Ian Rogers#endif  // ART_DISASSEMBLER_DISASSEMBLER_H_
80