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 #include "lldb/Target/StackID.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
1659e8fc1cSGreg Clayton #include "lldb/Core/Stream.h"
1759e8fc1cSGreg Clayton #include "lldb/Symbol/Block.h"
1859e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h"
1959e8fc1cSGreg Clayton #include "lldb/Symbol/SymbolContext.h"
2030fdc8d8SChris Lattner 
2130fdc8d8SChris Lattner using namespace lldb_private;
2230fdc8d8SChris Lattner 
2330fdc8d8SChris Lattner 
2459e8fc1cSGreg Clayton void
2559e8fc1cSGreg Clayton StackID::Dump (Stream *s)
2659e8fc1cSGreg Clayton {
27*d01b2953SDaniel Malea     s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64 ", symbol_scope = %p", (uint64_t)m_pc, (uint64_t)m_cfa, m_symbol_scope);
2859e8fc1cSGreg Clayton     if (m_symbol_scope)
2959e8fc1cSGreg Clayton     {
3059e8fc1cSGreg Clayton         SymbolContext sc;
3159e8fc1cSGreg Clayton 
3259e8fc1cSGreg Clayton         m_symbol_scope->CalculateSymbolContext (&sc);
3359e8fc1cSGreg Clayton         if (sc.block)
34*d01b2953SDaniel Malea             s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());
3559e8fc1cSGreg Clayton         else if (sc.symbol)
3659e8fc1cSGreg Clayton             s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());
3759e8fc1cSGreg Clayton     }
3859e8fc1cSGreg Clayton     s->PutCString(") ");
3959e8fc1cSGreg Clayton }
4059e8fc1cSGreg Clayton 
4130fdc8d8SChris Lattner bool
4230fdc8d8SChris Lattner lldb_private::operator== (const StackID& lhs, const StackID& rhs)
4330fdc8d8SChris Lattner {
446dadd508SGreg Clayton     if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
456dadd508SGreg Clayton         return false;
466dadd508SGreg Clayton 
476dadd508SGreg Clayton     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
486dadd508SGreg Clayton     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
496dadd508SGreg Clayton 
506dadd508SGreg Clayton     // Only compare the PC values if both symbol context scopes are NULL
516dadd508SGreg Clayton     if (lhs_scope == NULL && rhs_scope == NULL)
526dadd508SGreg Clayton         return lhs.GetPC() == rhs.GetPC();
536dadd508SGreg Clayton 
546dadd508SGreg Clayton     return lhs_scope == rhs_scope;
5530fdc8d8SChris Lattner }
5630fdc8d8SChris Lattner 
5730fdc8d8SChris Lattner bool
5830fdc8d8SChris Lattner lldb_private::operator!= (const StackID& lhs, const StackID& rhs)
5930fdc8d8SChris Lattner {
606dadd508SGreg Clayton     if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
616dadd508SGreg Clayton         return true;
626dadd508SGreg Clayton 
636dadd508SGreg Clayton     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
646dadd508SGreg Clayton     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
656dadd508SGreg Clayton 
666dadd508SGreg Clayton     if (lhs_scope == NULL && rhs_scope == NULL)
676dadd508SGreg Clayton         return lhs.GetPC() != rhs.GetPC();
686dadd508SGreg Clayton 
696dadd508SGreg Clayton     return lhs_scope != rhs_scope;
7030fdc8d8SChris Lattner }
7130fdc8d8SChris Lattner 
7230fdc8d8SChris Lattner bool
7330fdc8d8SChris Lattner lldb_private::operator< (const StackID& lhs, const StackID& rhs)
7430fdc8d8SChris Lattner {
756dadd508SGreg Clayton     const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
766dadd508SGreg Clayton     const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
776dadd508SGreg Clayton 
78b5c0d1ccSJim Ingham     // FIXME: We are assuming that the stacks grow downward in memory.  That's not necessary, but true on
79b5c0d1ccSJim Ingham     // all the machines we care about at present.  If this changes, we'll have to deal with that.  The ABI is the
80b5c0d1ccSJim Ingham     // agent who knows this ordering, but the StackID has no access to the ABI.  The most straightforward way
81b5c0d1ccSJim Ingham     // to handle this is to add a "m_grows_downward" bool to the StackID, and set it in the constructor.
82b5c0d1ccSJim Ingham     // But I'm not going to waste a bool per StackID on this till we need it.
83b5c0d1ccSJim Ingham 
846dadd508SGreg Clayton     if (lhs_cfa != rhs_cfa)
856dadd508SGreg Clayton         return lhs_cfa < rhs_cfa;
866dadd508SGreg Clayton 
876dadd508SGreg Clayton     SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
886dadd508SGreg Clayton     SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
896dadd508SGreg Clayton 
906dadd508SGreg Clayton     if (lhs_scope != NULL && rhs_scope != NULL)
916dadd508SGreg Clayton     {
926dadd508SGreg Clayton         // Same exact scope, lhs is not less than (younger than rhs)
936dadd508SGreg Clayton         if (lhs_scope == rhs_scope)
946dadd508SGreg Clayton             return false;
956dadd508SGreg Clayton 
966dadd508SGreg Clayton         SymbolContext lhs_sc;
976dadd508SGreg Clayton         SymbolContext rhs_sc;
986dadd508SGreg Clayton         lhs_scope->CalculateSymbolContext (&lhs_sc);
996dadd508SGreg Clayton         rhs_scope->CalculateSymbolContext (&rhs_sc);
1006dadd508SGreg Clayton 
1016dadd508SGreg Clayton         // Items with the same function can only be compared
1026dadd508SGreg Clayton         if (lhs_sc.function == rhs_sc.function &&
1036dadd508SGreg Clayton             lhs_sc.function != NULL && lhs_sc.block != NULL &&
1046dadd508SGreg Clayton             rhs_sc.function != NULL && rhs_sc.block != NULL)
1056dadd508SGreg Clayton         {
1066dadd508SGreg Clayton             return rhs_sc.block->Contains (lhs_sc.block);
1076dadd508SGreg Clayton         }
1086dadd508SGreg Clayton     }
1096dadd508SGreg Clayton     return false;
11030fdc8d8SChris Lattner }
111