10ae96273SGreg Clayton //===-- Baton.cpp -----------------------------------------------*- C++ -*-===//
20ae96273SGreg Clayton //
30ae96273SGreg Clayton //                     The LLVM Compiler Infrastructure
40ae96273SGreg Clayton //
50ae96273SGreg Clayton // This file is distributed under the University of Illinois Open Source
60ae96273SGreg Clayton // License. See LICENSE.TXT for details.
70ae96273SGreg Clayton //
80ae96273SGreg Clayton //===----------------------------------------------------------------------===//
90ae96273SGreg Clayton 
100ae96273SGreg Clayton #include "lldb/Core/Opcode.h"
110ae96273SGreg Clayton 
120ae96273SGreg Clayton // C Includes
130ae96273SGreg Clayton // C++ Includes
140ae96273SGreg Clayton // Other libraries and framework includes
150ae96273SGreg Clayton // Project includes
161080edbcSGreg Clayton #include "lldb/Core/Stream.h"
17*8f7180b1SGreg Clayton #include "lldb/Host/Endian.h"
180ae96273SGreg Clayton 
190ae96273SGreg Clayton using namespace lldb;
200ae96273SGreg Clayton using namespace lldb_private;
211080edbcSGreg Clayton 
221080edbcSGreg Clayton 
231080edbcSGreg Clayton int
241080edbcSGreg Clayton Opcode::Dump (Stream *s, uint32_t min_byte_width)
251080edbcSGreg Clayton {
261080edbcSGreg Clayton     int bytes_written = 0;
271080edbcSGreg Clayton     switch (m_type)
281080edbcSGreg Clayton     {
291080edbcSGreg Clayton     case Opcode::eTypeInvalid:
301080edbcSGreg Clayton         bytes_written = s->PutCString ("<invalid>");
311080edbcSGreg Clayton         break;
321080edbcSGreg Clayton     case Opcode::eType8:
331080edbcSGreg Clayton         bytes_written = s->Printf ("0x%2.2x", m_data.inst8);
341080edbcSGreg Clayton         break;
351080edbcSGreg Clayton     case Opcode::eType16:
361080edbcSGreg Clayton         bytes_written = s->Printf ("0x%4.4x", m_data.inst16);
371080edbcSGreg Clayton         break;
381080edbcSGreg Clayton 
391080edbcSGreg Clayton     case Opcode::eType32:
401080edbcSGreg Clayton         bytes_written = s->Printf ("0x%8.8x", m_data.inst32);
411080edbcSGreg Clayton         break;
421080edbcSGreg Clayton 
431080edbcSGreg Clayton     case Opcode::eType64:
441080edbcSGreg Clayton         bytes_written = s->Printf ("0x%16.16llx", m_data.inst64);
451080edbcSGreg Clayton         break;
461080edbcSGreg Clayton 
471080edbcSGreg Clayton     case Opcode::eTypeBytes:
481080edbcSGreg Clayton         {
491080edbcSGreg Clayton             for (uint32_t i=0; i<m_data.inst.length; ++i)
501080edbcSGreg Clayton             {
511080edbcSGreg Clayton                 if (i > 0)
52357132ebSGreg Clayton                     bytes_written += s->PutChar (' ');
531080edbcSGreg Clayton                 bytes_written += s->Printf ("%2.2x", m_data.inst.bytes[i]);
541080edbcSGreg Clayton             }
551080edbcSGreg Clayton         }
561080edbcSGreg Clayton         break;
571080edbcSGreg Clayton     }
581080edbcSGreg Clayton 
591080edbcSGreg Clayton     // Add spaces to make sure bytes dispay comes out even in case opcodes
601080edbcSGreg Clayton     // aren't all the same size
611080edbcSGreg Clayton     if (bytes_written < min_byte_width)
621080edbcSGreg Clayton         bytes_written = s->Printf ("%*s", min_byte_width - bytes_written, "");
631080edbcSGreg Clayton     return bytes_written;
641080edbcSGreg Clayton }
651080edbcSGreg Clayton 
66*8f7180b1SGreg Clayton lldb::ByteOrder
67*8f7180b1SGreg Clayton Opcode::GetDataByteOrder () const
68*8f7180b1SGreg Clayton {
69*8f7180b1SGreg Clayton     switch (m_type)
70*8f7180b1SGreg Clayton     {
71*8f7180b1SGreg Clayton         case Opcode::eTypeInvalid: break;
72*8f7180b1SGreg Clayton         case Opcode::eType8:
73*8f7180b1SGreg Clayton         case Opcode::eType16:
74*8f7180b1SGreg Clayton         case Opcode::eType32:
75*8f7180b1SGreg Clayton         case Opcode::eType64:    return lldb::endian::InlHostByteOrder();
76*8f7180b1SGreg Clayton         case Opcode::eTypeBytes:
77*8f7180b1SGreg Clayton             break;
78*8f7180b1SGreg Clayton     }
79*8f7180b1SGreg Clayton     return eByteOrderInvalid;
80*8f7180b1SGreg Clayton }
81*8f7180b1SGreg Clayton 
82