1 //===-- ABI.cpp -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ABI.h"
15 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Symbol/CompilerType.h"
20 #include "lldb/Symbol/TypeSystem.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/Thread.h"
25 using namespace lldb_private;
28 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
30 ABICreateInstance create_callback;
32 for (uint32_t idx = 0;
33 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
36 abi_sp = create_callback(process_sp, arch);
45 ABI::~ABI() = default;
47 bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
49 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
50 if (register_info_array) {
51 const char *unique_name_cstr = name.GetCString();
53 for (i = 0; i < count; ++i) {
54 if (register_info_array[i].name == unique_name_cstr) {
55 info = register_info_array[i];
59 for (i = 0; i < count; ++i) {
60 if (register_info_array[i].alt_name == unique_name_cstr) {
61 info = register_info_array[i];
69 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
71 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
75 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
76 if (register_info_array) {
77 for (uint32_t i = 0; i < count; ++i) {
78 if (register_info_array[i].kinds[reg_kind] == reg_num) {
79 info = register_info_array[i];
87 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
88 bool persistent) const {
89 if (!ast_type.IsValid())
90 return ValueObjectSP();
92 ValueObjectSP return_valobj_sp;
94 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
95 if (!return_valobj_sp)
96 return return_valobj_sp;
98 // Now turn this into a persistent variable.
99 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
100 // used in similar form in a couple
101 // of other places. Figure out the correct Create function to do all this
105 PersistentExpressionState *persistent_expression_state =
106 thread.CalculateTarget()->GetPersistentExpressionStateForLanguage(
107 ast_type.GetMinimumLanguage());
109 if (!persistent_expression_state)
110 return ValueObjectSP();
112 ConstString persistent_variable_name(
113 persistent_expression_state->GetNextPersistentVariableName());
115 lldb::ValueObjectSP const_valobj_sp;
117 // Check in case our value is already a constant value
118 if (return_valobj_sp->GetIsConstant()) {
119 const_valobj_sp = return_valobj_sp;
120 const_valobj_sp->SetName(persistent_variable_name);
123 return_valobj_sp->CreateConstantValue(persistent_variable_name);
125 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
127 return_valobj_sp = const_valobj_sp;
129 ExpressionVariableSP clang_expr_variable_sp(
130 persistent_expression_state->CreatePersistentVariable(
133 assert(clang_expr_variable_sp);
135 // Set flags and live data as appropriate
137 const Value &result_value = live_valobj_sp->GetValue();
139 switch (result_value.GetValueType()) {
140 case Value::eValueTypeHostAddress:
141 case Value::eValueTypeFileAddress:
142 // we don't do anything with these for now
144 case Value::eValueTypeScalar:
145 case Value::eValueTypeVector:
146 clang_expr_variable_sp->m_flags |=
147 ClangExpressionVariable::EVIsFreezeDried;
148 clang_expr_variable_sp->m_flags |=
149 ClangExpressionVariable::EVIsLLDBAllocated;
150 clang_expr_variable_sp->m_flags |=
151 ClangExpressionVariable::EVNeedsAllocation;
153 case Value::eValueTypeLoadAddress:
154 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
155 clang_expr_variable_sp->m_flags |=
156 ClangExpressionVariable::EVIsProgramReference;
160 return_valobj_sp = clang_expr_variable_sp->GetValueObject();
162 return return_valobj_sp;
165 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
166 bool persistent) const {
167 ValueObjectSP return_valobj_sp;
168 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
169 return return_valobj_sp;
172 // specialized to work with llvm IR types
174 // for now we will specify a default implementation so that we don't need to
176 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
177 llvm::Type &ir_type) const {
178 ValueObjectSP return_valobj_sp;
180 /* this is a dummy and will only be called if an ABI does not override this */
182 return return_valobj_sp;
185 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
186 lldb::addr_t functionAddress,
187 lldb::addr_t returnAddress, llvm::Type &returntype,
188 llvm::ArrayRef<ABI::CallArgument> args) const {
189 // dummy prepare trivial call
190 llvm_unreachable("Should never get here!");
193 bool ABI::GetFallbackRegisterLocation(
194 const RegisterInfo *reg_info,
195 UnwindPlan::Row::RegisterLocation &unwind_regloc) {
196 // Did the UnwindPlan fail to give us the caller's stack pointer?
197 // The stack pointer is defined to be the same as THIS frame's CFA, so return
199 // the caller's stack pointer. This is true on x86-32/x86-64 at least.
200 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
201 unwind_regloc.SetIsCFAPlusOffset(0);
205 // If a volatile register is being requested, we don't want to forward the
206 // next frame's register contents
207 // up the stack -- the register is not retrievable at this frame.
208 if (RegisterIsVolatile(reg_info)) {
209 unwind_regloc.SetUndefined();