1b8021494Sopenharmony_ci// Copyright 2017, VIXL authors 2b8021494Sopenharmony_ci// All rights reserved. 3b8021494Sopenharmony_ci// 4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met: 6b8021494Sopenharmony_ci// 7b8021494Sopenharmony_ci// * Redistributions of source code must retain the above copyright notice, 8b8021494Sopenharmony_ci// this list of conditions and the following disclaimer. 9b8021494Sopenharmony_ci// * Redistributions in binary form must reproduce the above copyright notice, 10b8021494Sopenharmony_ci// this list of conditions and the following disclaimer in the documentation 11b8021494Sopenharmony_ci// and/or other materials provided with the distribution. 12b8021494Sopenharmony_ci// * Neither the name of ARM Limited nor the names of its contributors may be 13b8021494Sopenharmony_ci// used to endorse or promote products derived from this software without 14b8021494Sopenharmony_ci// specific prior written permission. 15b8021494Sopenharmony_ci// 16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND 17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26b8021494Sopenharmony_ci 27b8021494Sopenharmony_ci#include <iostream> 28b8021494Sopenharmony_ci#include <map> 29b8021494Sopenharmony_ci#include <string> 30b8021494Sopenharmony_ci 31b8021494Sopenharmony_ci#include "aarch32/constants-aarch32.h" 32b8021494Sopenharmony_ci#include "aarch32/disasm-aarch32.h" 33b8021494Sopenharmony_ci#include "aarch32/instructions-aarch32.h" 34b8021494Sopenharmony_ci#include "aarch32/macro-assembler-aarch32.h" 35b8021494Sopenharmony_ci 36b8021494Sopenharmony_ci#define __ masm. 37b8021494Sopenharmony_ci 38b8021494Sopenharmony_cinamespace vixl { 39b8021494Sopenharmony_cinamespace aarch32 { 40b8021494Sopenharmony_ci 41b8021494Sopenharmony_ci// Example of disassembly customization. 42b8021494Sopenharmony_ci 43b8021494Sopenharmony_ciclass CustomStream : public Disassembler::DisassemblerStream { 44b8021494Sopenharmony_ci std::map<Location::Offset, const char*> symbols_; 45b8021494Sopenharmony_ci 46b8021494Sopenharmony_ci public: 47b8021494Sopenharmony_ci CustomStream() : Disassembler::DisassemblerStream(std::cout) {} 48b8021494Sopenharmony_ci std::map<Location::Offset, const char*>& GetSymbols() { return symbols_; } 49b8021494Sopenharmony_ci virtual DisassemblerStream& operator<<(const Disassembler::PrintLabel& label) 50b8021494Sopenharmony_ci VIXL_OVERRIDE { 51b8021494Sopenharmony_ci std::map<Location::Offset, const char*>::iterator symbol = 52b8021494Sopenharmony_ci symbols_.find(label.GetLocation()); 53b8021494Sopenharmony_ci // If the label was named, print the name instead of the address. 54b8021494Sopenharmony_ci if (symbol != symbols_.end()) { 55b8021494Sopenharmony_ci os() << symbol->second; 56b8021494Sopenharmony_ci return *this; 57b8021494Sopenharmony_ci } 58b8021494Sopenharmony_ci os() << label; 59b8021494Sopenharmony_ci return *this; 60b8021494Sopenharmony_ci } 61b8021494Sopenharmony_ci virtual DisassemblerStream& operator<<(const Register reg) VIXL_OVERRIDE { 62b8021494Sopenharmony_ci // Print all the core registers with an upper-case letter instead of the 63b8021494Sopenharmony_ci // default lower-case. 64b8021494Sopenharmony_ci os() << "R" << reg.GetCode(); 65b8021494Sopenharmony_ci return *this; 66b8021494Sopenharmony_ci } 67b8021494Sopenharmony_ci}; 68b8021494Sopenharmony_ci 69b8021494Sopenharmony_ciclass CustomDisassembler : public PrintDisassembler { 70b8021494Sopenharmony_ci public: 71b8021494Sopenharmony_ci explicit CustomDisassembler(CustomStream* stream) 72b8021494Sopenharmony_ci : PrintDisassembler(stream) {} 73b8021494Sopenharmony_ci CustomStream* GetStream() const { 74b8021494Sopenharmony_ci return reinterpret_cast<CustomStream*>(&os()); 75b8021494Sopenharmony_ci } 76b8021494Sopenharmony_ci virtual void PrintCodeAddress(uint32_t addr) VIXL_OVERRIDE { 77b8021494Sopenharmony_ci // If the address matches a label, then print the label. Otherwise, print 78b8021494Sopenharmony_ci // nothing. 79b8021494Sopenharmony_ci std::map<Location::Offset, const char*>::iterator symbol = 80b8021494Sopenharmony_ci GetStream()->GetSymbols().find(addr); 81b8021494Sopenharmony_ci if (symbol != GetStream()->GetSymbols().end()) { 82b8021494Sopenharmony_ci os().os() << symbol->second << ":" << std::endl; 83b8021494Sopenharmony_ci } 84b8021494Sopenharmony_ci // Add indentation for instructions. 85b8021494Sopenharmony_ci os() << " "; 86b8021494Sopenharmony_ci } 87b8021494Sopenharmony_ci virtual void PrintOpcode16(uint32_t opcode) VIXL_OVERRIDE { 88b8021494Sopenharmony_ci // Do not print opcodes. 89b8021494Sopenharmony_ci USE(opcode); 90b8021494Sopenharmony_ci } 91b8021494Sopenharmony_ci virtual void PrintOpcode32(uint32_t opcode) VIXL_OVERRIDE { 92b8021494Sopenharmony_ci // Do not print opcodes. 93b8021494Sopenharmony_ci USE(opcode); 94b8021494Sopenharmony_ci } 95b8021494Sopenharmony_ci}; 96b8021494Sopenharmony_ci 97b8021494Sopenharmony_ciclass NamedLabel : public Label { 98b8021494Sopenharmony_ci CustomStream* stream_; 99b8021494Sopenharmony_ci const char* name_; 100b8021494Sopenharmony_ci 101b8021494Sopenharmony_ci public: 102b8021494Sopenharmony_ci NamedLabel(CustomStream* stream, const char* name) 103b8021494Sopenharmony_ci : stream_(stream), name_(name) {} 104b8021494Sopenharmony_ci ~NamedLabel() { 105b8021494Sopenharmony_ci if (IsBound()) { 106b8021494Sopenharmony_ci stream_->GetSymbols().insert( 107b8021494Sopenharmony_ci std::pair<Location::Offset, const char*>(GetLocation(), name_)); 108b8021494Sopenharmony_ci } 109b8021494Sopenharmony_ci } 110b8021494Sopenharmony_ci}; 111b8021494Sopenharmony_ci 112b8021494Sopenharmony_civoid RunCustomDisassemblerTest() { 113b8021494Sopenharmony_ci CustomStream stream; 114b8021494Sopenharmony_ci MacroAssembler masm; 115b8021494Sopenharmony_ci { 116b8021494Sopenharmony_ci NamedLabel loop(&stream, "loop"); 117b8021494Sopenharmony_ci NamedLabel end(&stream, "end"); 118b8021494Sopenharmony_ci __ Mov(r0, 0); 119b8021494Sopenharmony_ci __ Mov(r1, 0); 120b8021494Sopenharmony_ci __ Bind(&loop); 121b8021494Sopenharmony_ci __ Cmp(r1, 20); 122b8021494Sopenharmony_ci __ B(gt, &end); 123b8021494Sopenharmony_ci __ Add(r0, r0, r1); 124b8021494Sopenharmony_ci __ Add(r1, r1, 1); 125b8021494Sopenharmony_ci __ B(&loop); 126b8021494Sopenharmony_ci __ Bind(&end); 127b8021494Sopenharmony_ci __ Bx(lr); 128b8021494Sopenharmony_ci __ FinalizeCode(); 129b8021494Sopenharmony_ci } 130b8021494Sopenharmony_ci std::cout << "Custom disassembly:" << std::endl; 131b8021494Sopenharmony_ci CustomDisassembler custom_disassembler(&stream); 132b8021494Sopenharmony_ci custom_disassembler 133b8021494Sopenharmony_ci .DisassembleA32Buffer(masm.GetBuffer()->GetOffsetAddress<uint32_t*>(0), 134b8021494Sopenharmony_ci masm.GetBuffer()->GetSizeInBytes()); 135b8021494Sopenharmony_ci std::cout << std::endl; 136b8021494Sopenharmony_ci std::cout << "Standard disassembly:" << std::endl; 137b8021494Sopenharmony_ci PrintDisassembler print_disassembler(std::cout); 138b8021494Sopenharmony_ci print_disassembler 139b8021494Sopenharmony_ci .DisassembleA32Buffer(masm.GetBuffer()->GetOffsetAddress<uint32_t*>(0), 140b8021494Sopenharmony_ci masm.GetBuffer()->GetSizeInBytes()); 141b8021494Sopenharmony_ci} 142b8021494Sopenharmony_ci 143b8021494Sopenharmony_ci} // namespace aarch32 144b8021494Sopenharmony_ci} // namespace vixl 145b8021494Sopenharmony_ci 146b8021494Sopenharmony_ci#ifndef TEST_EXAMPLES 147b8021494Sopenharmony_ciint main() { 148b8021494Sopenharmony_ci vixl::aarch32::RunCustomDisassemblerTest(); 149b8021494Sopenharmony_ci return 0; 150b8021494Sopenharmony_ci} 151b8021494Sopenharmony_ci#endif // TEST_EXAMPLES 152