1*80814287SRaphael Isemann //===-- StackID.cpp -------------------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9d70a6e71SEugene Zelenko #include "lldb/Target/StackID.h"
1059e8fc1cSGreg Clayton #include "lldb/Symbol/Block.h"
1159e8fc1cSGreg Clayton #include "lldb/Symbol/Symbol.h"
1259e8fc1cSGreg Clayton #include "lldb/Symbol/SymbolContext.h"
13bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
1430fdc8d8SChris Lattner 
1530fdc8d8SChris Lattner using namespace lldb_private;
1630fdc8d8SChris Lattner 
Dump(Stream * s)17b9c1b51eSKate Stone void StackID::Dump(Stream *s) {
18b9c1b51eSKate Stone   s->Printf("StackID (pc = 0x%16.16" PRIx64 ", cfa = 0x%16.16" PRIx64
19b9c1b51eSKate Stone             ", symbol_scope = %p",
20324a1036SSaleem Abdulrasool             m_pc, m_cfa, static_cast<void *>(m_symbol_scope));
21b9c1b51eSKate Stone   if (m_symbol_scope) {
2259e8fc1cSGreg Clayton     SymbolContext sc;
2359e8fc1cSGreg Clayton 
2459e8fc1cSGreg Clayton     m_symbol_scope->CalculateSymbolContext(&sc);
2559e8fc1cSGreg Clayton     if (sc.block)
26d01b2953SDaniel Malea       s->Printf(" (Block {0x%8.8" PRIx64 "})", sc.block->GetID());
2759e8fc1cSGreg Clayton     else if (sc.symbol)
2859e8fc1cSGreg Clayton       s->Printf(" (Symbol{0x%8.8x})", sc.symbol->GetID());
2959e8fc1cSGreg Clayton   }
3059e8fc1cSGreg Clayton   s->PutCString(") ");
3159e8fc1cSGreg Clayton }
3259e8fc1cSGreg Clayton 
operator ==(const StackID & lhs,const StackID & rhs)33b9c1b51eSKate Stone bool lldb_private::operator==(const StackID &lhs, const StackID &rhs) {
346dadd508SGreg Clayton   if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
356dadd508SGreg Clayton     return false;
366dadd508SGreg Clayton 
376dadd508SGreg Clayton   SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
386dadd508SGreg Clayton   SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
396dadd508SGreg Clayton 
40d70a6e71SEugene Zelenko   // Only compare the PC values if both symbol context scopes are nullptr
41d70a6e71SEugene Zelenko   if (lhs_scope == nullptr && rhs_scope == nullptr)
426dadd508SGreg Clayton     return lhs.GetPC() == rhs.GetPC();
436dadd508SGreg Clayton 
446dadd508SGreg Clayton   return lhs_scope == rhs_scope;
4530fdc8d8SChris Lattner }
4630fdc8d8SChris Lattner 
operator !=(const StackID & lhs,const StackID & rhs)47b9c1b51eSKate Stone bool lldb_private::operator!=(const StackID &lhs, const StackID &rhs) {
486dadd508SGreg Clayton   if (lhs.GetCallFrameAddress() != rhs.GetCallFrameAddress())
496dadd508SGreg Clayton     return true;
506dadd508SGreg Clayton 
516dadd508SGreg Clayton   SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
526dadd508SGreg Clayton   SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
536dadd508SGreg Clayton 
54d70a6e71SEugene Zelenko   if (lhs_scope == nullptr && rhs_scope == nullptr)
556dadd508SGreg Clayton     return lhs.GetPC() != rhs.GetPC();
566dadd508SGreg Clayton 
576dadd508SGreg Clayton   return lhs_scope != rhs_scope;
5830fdc8d8SChris Lattner }
5930fdc8d8SChris Lattner 
operator <(const StackID & lhs,const StackID & rhs)60b9c1b51eSKate Stone bool lldb_private::operator<(const StackID &lhs, const StackID &rhs) {
616dadd508SGreg Clayton   const lldb::addr_t lhs_cfa = lhs.GetCallFrameAddress();
626dadd508SGreg Clayton   const lldb::addr_t rhs_cfa = rhs.GetCallFrameAddress();
636dadd508SGreg Clayton 
64b9c1b51eSKate Stone   // FIXME: We are assuming that the stacks grow downward in memory.  That's not
65b9c1b51eSKate Stone   // necessary, but true on
66b9c1b51eSKate Stone   // all the machines we care about at present.  If this changes, we'll have to
6705097246SAdrian Prantl   // deal with that.  The ABI is the agent who knows this ordering, but the
6805097246SAdrian Prantl   // StackID has no access to the ABI. The most straightforward way to handle
6905097246SAdrian Prantl   // this is to add a "m_grows_downward" bool to the StackID, and set it in the
7005097246SAdrian Prantl   // constructor. But I'm not going to waste a bool per StackID on this till we
7105097246SAdrian Prantl   // need it.
72b5c0d1ccSJim Ingham 
736dadd508SGreg Clayton   if (lhs_cfa != rhs_cfa)
746dadd508SGreg Clayton     return lhs_cfa < rhs_cfa;
756dadd508SGreg Clayton 
766dadd508SGreg Clayton   SymbolContextScope *lhs_scope = lhs.GetSymbolContextScope();
776dadd508SGreg Clayton   SymbolContextScope *rhs_scope = rhs.GetSymbolContextScope();
786dadd508SGreg Clayton 
79b9c1b51eSKate Stone   if (lhs_scope != nullptr && rhs_scope != nullptr) {
806dadd508SGreg Clayton     // Same exact scope, lhs is not less than (younger than rhs)
816dadd508SGreg Clayton     if (lhs_scope == rhs_scope)
826dadd508SGreg Clayton       return false;
836dadd508SGreg Clayton 
846dadd508SGreg Clayton     SymbolContext lhs_sc;
856dadd508SGreg Clayton     SymbolContext rhs_sc;
866dadd508SGreg Clayton     lhs_scope->CalculateSymbolContext(&lhs_sc);
876dadd508SGreg Clayton     rhs_scope->CalculateSymbolContext(&rhs_sc);
886dadd508SGreg Clayton 
896dadd508SGreg Clayton     // Items with the same function can only be compared
90b9c1b51eSKate Stone     if (lhs_sc.function == rhs_sc.function && lhs_sc.function != nullptr &&
91b9c1b51eSKate Stone         lhs_sc.block != nullptr && rhs_sc.function != nullptr &&
92b9c1b51eSKate Stone         rhs_sc.block != nullptr) {
936dadd508SGreg Clayton       return rhs_sc.block->Contains(lhs_sc.block);
946dadd508SGreg Clayton     }
956dadd508SGreg Clayton   }
966dadd508SGreg Clayton   return false;
9730fdc8d8SChris Lattner }
98