130fdc8d8SChris Lattner //===-- StackID.cpp ---------------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner // Other libraries and framework includes
1330fdc8d8SChris Lattner // Project includes
14*d70a6e71SEugene Zelenko #include "lldb/Target/StackID.h"
1559e8fc1cSGreg Clayton #include "lldb/Core/Stream.h"
1659e8fc1cSGreg Clayton #include "lldb/Symbol/Block.h"
1759e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h"
1859e8fc1cSGreg Clayton #include "lldb/Symbol/SymbolContext.h"
1930fdc8d8SChris Lattner 
2030fdc8d8SChris Lattner using namespace lldb_private;
2130fdc8d8SChris Lattner 
2259e8fc1cSGreg Clayton void
2359e8fc1cSGreg Clayton StackID::Dump (Stream *s)
2459e8fc1cSGreg Clayton {
25324a1036SSaleem Abdulrasool     s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64 ", symbol_scope = %p",
26324a1036SSaleem Abdulrasool               m_pc, m_cfa, static_cast<void*>(m_symbol_scope));
2759e8fc1cSGreg Clayton     if (m_symbol_scope)
2859e8fc1cSGreg Clayton     {
2959e8fc1cSGreg Clayton         SymbolContext sc;
3059e8fc1cSGreg Clayton 
3159e8fc1cSGreg Clayton         m_symbol_scope->CalculateSymbolContext (&sc);
3259e8fc1cSGreg Clayton         if (sc.block)
33d01b2953SDaniel Malea             s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());
3459e8fc1cSGreg Clayton         else if (sc.symbol)
3559e8fc1cSGreg Clayton             s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());
3659e8fc1cSGreg Clayton     }
3759e8fc1cSGreg Clayton     s->PutCString(") ");
3859e8fc1cSGreg Clayton }
3959e8fc1cSGreg Clayton 
4030fdc8d8SChris Lattner bool
4130fdc8d8SChris Lattner lldb_private::operator== (const StackID& lhs, const StackID& rhs)
4230fdc8d8SChris Lattner {
436dadd508SGreg Clayton     if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
446dadd508SGreg Clayton         return false;
456dadd508SGreg Clayton 
466dadd508SGreg Clayton     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
476dadd508SGreg Clayton     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
486dadd508SGreg Clayton 
49*d70a6e71SEugene Zelenko     // Only compare the PC values if both symbol context scopes are nullptr
50*d70a6e71SEugene Zelenko     if (lhs_scope == nullptr && rhs_scope == nullptr)
516dadd508SGreg Clayton         return lhs.GetPC() == rhs.GetPC();
526dadd508SGreg Clayton 
536dadd508SGreg Clayton     return lhs_scope == rhs_scope;
5430fdc8d8SChris Lattner }
5530fdc8d8SChris Lattner 
5630fdc8d8SChris Lattner bool
5730fdc8d8SChris Lattner lldb_private::operator!= (const StackID& lhs, const StackID& rhs)
5830fdc8d8SChris Lattner {
596dadd508SGreg Clayton     if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
606dadd508SGreg Clayton         return true;
616dadd508SGreg Clayton 
626dadd508SGreg Clayton     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
636dadd508SGreg Clayton     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
646dadd508SGreg Clayton 
65*d70a6e71SEugene Zelenko     if (lhs_scope == nullptr && rhs_scope == nullptr)
666dadd508SGreg Clayton         return lhs.GetPC() != rhs.GetPC();
676dadd508SGreg Clayton 
686dadd508SGreg Clayton     return lhs_scope != rhs_scope;
6930fdc8d8SChris Lattner }
7030fdc8d8SChris Lattner 
7130fdc8d8SChris Lattner bool
7230fdc8d8SChris Lattner lldb_private::operator< (const StackID& lhs, const StackID& rhs)
7330fdc8d8SChris Lattner {
746dadd508SGreg Clayton     const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
756dadd508SGreg Clayton     const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
766dadd508SGreg Clayton 
77b5c0d1ccSJim Ingham     // FIXME: We are assuming that the stacks grow downward in memory.  That's not necessary, but true on
78b5c0d1ccSJim Ingham     // all the machines we care about at present.  If this changes, we'll have to deal with that.  The ABI is the
79b5c0d1ccSJim Ingham     // agent who knows this ordering, but the StackID has no access to the ABI.  The most straightforward way
80b5c0d1ccSJim Ingham     // to handle this is to add a "m_grows_downward" bool to the StackID, and set it in the constructor.
81b5c0d1ccSJim Ingham     // But I'm not going to waste a bool per StackID on this till we need it.
82b5c0d1ccSJim Ingham 
836dadd508SGreg Clayton     if (lhs_cfa != rhs_cfa)
846dadd508SGreg Clayton         return lhs_cfa < rhs_cfa;
856dadd508SGreg Clayton 
866dadd508SGreg Clayton     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
876dadd508SGreg Clayton     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
886dadd508SGreg Clayton 
89*d70a6e71SEugene Zelenko     if (lhs_scope != nullptr && rhs_scope != nullptr)
906dadd508SGreg Clayton     {
916dadd508SGreg Clayton         // Same exact scope, lhs is not less than (younger than rhs)
926dadd508SGreg Clayton         if (lhs_scope == rhs_scope)
936dadd508SGreg Clayton             return false;
946dadd508SGreg Clayton 
956dadd508SGreg Clayton         SymbolContext lhs_sc;
966dadd508SGreg Clayton         SymbolContext rhs_sc;
976dadd508SGreg Clayton         lhs_scope->CalculateSymbolContext (&lhs_sc);
986dadd508SGreg Clayton         rhs_scope->CalculateSymbolContext (&rhs_sc);
996dadd508SGreg Clayton 
1006dadd508SGreg Clayton         // Items with the same function can only be compared
1016dadd508SGreg Clayton         if (lhs_sc.function == rhs_sc.function &&
102*d70a6e71SEugene Zelenko             lhs_sc.function != nullptr && lhs_sc.block != nullptr &&
103*d70a6e71SEugene Zelenko             rhs_sc.function != nullptr && rhs_sc.block != nullptr)
1046dadd508SGreg Clayton         {
1056dadd508SGreg Clayton             return rhs_sc.block->Contains (lhs_sc.block);
1066dadd508SGreg Clayton         }
1076dadd508SGreg Clayton     }
1086dadd508SGreg Clayton     return false;
10930fdc8d8SChris Lattner }
110