19f2f44ceSEd Maste //===-- ExpressionVariable.cpp ----------------------------------*- C++ -*-===//
29f2f44ceSEd Maste //
39f2f44ceSEd Maste //                     The LLVM Compiler Infrastructure
49f2f44ceSEd Maste //
59f2f44ceSEd Maste // This file is distributed under the University of Illinois Open Source
69f2f44ceSEd Maste // License. See LICENSE.TXT for details.
79f2f44ceSEd Maste //
89f2f44ceSEd Maste //===----------------------------------------------------------------------===//
99f2f44ceSEd Maste 
109f2f44ceSEd Maste #include "lldb/Expression/ExpressionVariable.h"
114bb0738eSEd Maste #include "lldb/Expression/IRExecutionUnit.h"
12*4ba319b5SDimitry Andric #include "lldb/Target/Target.h"
13f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
149f2f44ceSEd Maste 
159f2f44ceSEd Maste using namespace lldb_private;
169f2f44ceSEd Maste 
~ExpressionVariable()17435933ddSDimitry Andric ExpressionVariable::~ExpressionVariable() {}
189f2f44ceSEd Maste 
GetValueBytes()19435933ddSDimitry Andric uint8_t *ExpressionVariable::GetValueBytes() {
209f2f44ceSEd Maste   const size_t byte_size = m_frozen_sp->GetByteSize();
21435933ddSDimitry Andric   if (byte_size > 0) {
22435933ddSDimitry Andric     if (m_frozen_sp->GetDataExtractor().GetByteSize() < byte_size) {
239f2f44ceSEd Maste       m_frozen_sp->GetValue().ResizeData(byte_size);
249f2f44ceSEd Maste       m_frozen_sp->GetValue().GetData(m_frozen_sp->GetDataExtractor());
259f2f44ceSEd Maste     }
26435933ddSDimitry Andric     return const_cast<uint8_t *>(
27435933ddSDimitry Andric         m_frozen_sp->GetDataExtractor().GetDataStart());
289f2f44ceSEd Maste   }
299f2f44ceSEd Maste   return NULL;
309f2f44ceSEd Maste }
319f2f44ceSEd Maste 
~PersistentExpressionState()32435933ddSDimitry Andric PersistentExpressionState::~PersistentExpressionState() {}
334bb0738eSEd Maste 
LookupSymbol(const ConstString & name)34435933ddSDimitry Andric lldb::addr_t PersistentExpressionState::LookupSymbol(const ConstString &name) {
354bb0738eSEd Maste   SymbolMap::iterator si = m_symbol_map.find(name.GetCString());
364bb0738eSEd Maste 
374bb0738eSEd Maste   if (si != m_symbol_map.end())
384bb0738eSEd Maste     return si->second;
394bb0738eSEd Maste   else
404bb0738eSEd Maste     return LLDB_INVALID_ADDRESS;
414bb0738eSEd Maste }
424bb0738eSEd Maste 
RegisterExecutionUnit(lldb::IRExecutionUnitSP & execution_unit_sp)43435933ddSDimitry Andric void PersistentExpressionState::RegisterExecutionUnit(
44435933ddSDimitry Andric     lldb::IRExecutionUnitSP &execution_unit_sp) {
454bb0738eSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
464bb0738eSEd Maste 
474bb0738eSEd Maste   m_execution_units.insert(execution_unit_sp);
484bb0738eSEd Maste 
494bb0738eSEd Maste   if (log)
504bb0738eSEd Maste     log->Printf("Registering JITted Functions:\n");
514bb0738eSEd Maste 
52435933ddSDimitry Andric   for (const IRExecutionUnit::JittedFunction &jitted_function :
53435933ddSDimitry Andric        execution_unit_sp->GetJittedFunctions()) {
544bb0738eSEd Maste     if (jitted_function.m_external &&
554bb0738eSEd Maste         jitted_function.m_name != execution_unit_sp->GetFunctionName() &&
56435933ddSDimitry Andric         jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
57435933ddSDimitry Andric       m_symbol_map[jitted_function.m_name.GetCString()] =
58435933ddSDimitry Andric           jitted_function.m_remote_addr;
594bb0738eSEd Maste       if (log)
60435933ddSDimitry Andric         log->Printf("  Function: %s at 0x%" PRIx64 ".",
61435933ddSDimitry Andric                     jitted_function.m_name.GetCString(),
62435933ddSDimitry Andric                     jitted_function.m_remote_addr);
634bb0738eSEd Maste     }
644bb0738eSEd Maste   }
654bb0738eSEd Maste 
664bb0738eSEd Maste   if (log)
674bb0738eSEd Maste     log->Printf("Registering JIIted Symbols:\n");
684bb0738eSEd Maste 
69435933ddSDimitry Andric   for (const IRExecutionUnit::JittedGlobalVariable &global_var :
70435933ddSDimitry Andric        execution_unit_sp->GetJittedGlobalVariables()) {
71435933ddSDimitry Andric     if (global_var.m_remote_addr != LLDB_INVALID_ADDRESS) {
72435933ddSDimitry Andric       // Demangle the name before inserting it, so that lookups by the ConstStr
73*4ba319b5SDimitry Andric       // of the demangled name will find the mangled one (needed for looking up
74*4ba319b5SDimitry Andric       // metadata pointers.)
754bb0738eSEd Maste       Mangled mangler(global_var.m_name);
764bb0738eSEd Maste       mangler.GetDemangledName(lldb::eLanguageTypeUnknown);
774bb0738eSEd Maste       m_symbol_map[global_var.m_name.GetCString()] = global_var.m_remote_addr;
784bb0738eSEd Maste       if (log)
79435933ddSDimitry Andric         log->Printf("  Symbol: %s at 0x%" PRIx64 ".",
80435933ddSDimitry Andric                     global_var.m_name.GetCString(), global_var.m_remote_addr);
814bb0738eSEd Maste     }
824bb0738eSEd Maste   }
834bb0738eSEd Maste }
84*4ba319b5SDimitry Andric 
GetNextPersistentVariableName(Target & target,llvm::StringRef Prefix)85*4ba319b5SDimitry Andric ConstString PersistentExpressionState::GetNextPersistentVariableName(
86*4ba319b5SDimitry Andric     Target &target, llvm::StringRef Prefix) {
87*4ba319b5SDimitry Andric   llvm::SmallString<64> name;
88*4ba319b5SDimitry Andric   {
89*4ba319b5SDimitry Andric     llvm::raw_svector_ostream os(name);
90*4ba319b5SDimitry Andric     os << Prefix << target.GetNextPersistentVariableIndex();
91*4ba319b5SDimitry Andric   }
92*4ba319b5SDimitry Andric   return ConstString(name);
93*4ba319b5SDimitry Andric }
94