disassembler.h revision a37d925d405be9f589ac282869a997e73414d859
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
292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass DisassemblerOptions {
302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // Should the disassembler print absolute or relative addresses.
322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // Base addess for calculating relative code offsets when absolute_addresses_ is false.
352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const uint8_t* const base_address_;
362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
37a37d925d405be9f589ac282869a997e73414d859Alexandre Rames  // If set, the disassembler is allowed to look at load targets in literal
38a37d925d405be9f589ac282869a997e73414d859Alexandre Rames  // pools.
39a37d925d405be9f589ac282869a997e73414d859Alexandre Rames  const bool can_read_literals_;
40a37d925d405be9f589ac282869a997e73414d859Alexandre Rames
41a37d925d405be9f589ac282869a997e73414d859Alexandre Rames  DisassemblerOptions(bool absolute_addresses, const uint8_t* base_address,
42a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                      bool can_read_literals)
43a37d925d405be9f589ac282869a997e73414d859Alexandre Rames      : absolute_addresses_(absolute_addresses), base_address_(base_address),
44a37d925d405be9f589ac282869a997e73414d859Alexandre Rames        can_read_literals_(can_read_literals) {}
452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom private:
472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  DISALLOW_COPY_AND_ASSIGN(DisassemblerOptions);
482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass Disassembler {
513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers public:
522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // Creates a Disassembler for the given InstructionSet with the
532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // non-null DisassemblerOptions which become owned by the
542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // Disassembler.
552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static Disassembler* Create(InstructionSet instruction_set, DisassemblerOptions* options);
562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  virtual ~Disassembler() {
582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_options_;
592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
61b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  // Dump a single instruction returning the length of that instruction.
62b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  virtual size_t Dump(std::ostream& os, const uint8_t* begin) = 0;
63b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  // Dump instructions within a range.
643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  virtual void Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) = 0;
65105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes
66105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes protected:
672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  explicit Disassembler(DisassemblerOptions* disassembler_options)
682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : disassembler_options_(disassembler_options) {
692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    CHECK(disassembler_options_ != nullptr);
702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::string FormatInstructionPointer(const uint8_t* begin);
73105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes
74105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes private:
752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  DisassemblerOptions* disassembler_options_;
76105afd2bd8f9f0ddfcfcb4b8db9f356ee82ae8cdElliott Hughes  DISALLOW_COPY_AND_ASSIGN(Disassembler);
773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers};
783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
793c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffraystatic inline bool HasBitSet(uint32_t value, uint32_t bit) {
803c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray  return (value & (1 << bit)) != 0;
813c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray}
823c7bb98698f77af10372cf31824d3bb115d9bf0fNicolas Geoffray
833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers}  // namespace art
843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
8502ed4c04468ca5f5540c5b704ac3e2f30eb9e8f4Ian Rogers#endif  // ART_DISASSEMBLER_DISASSEMBLER_H_
86