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 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ABI.h"
11 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Core/Value.h"
14 #include "lldb/Core/ValueObjectConstResult.h"
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Symbol/TypeSystem.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Thread.h"
21 using namespace lldb_private;
24 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
26 ABICreateInstance create_callback;
28 for (uint32_t idx = 0;
29 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
32 abi_sp = create_callback(process_sp, arch);
41 ABI::~ABI() = default;
43 bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
45 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
46 if (register_info_array) {
47 const char *unique_name_cstr = name.GetCString();
49 for (i = 0; i < count; ++i) {
50 if (register_info_array[i].name == unique_name_cstr) {
51 info = register_info_array[i];
55 for (i = 0; i < count; ++i) {
56 if (register_info_array[i].alt_name == unique_name_cstr) {
57 info = register_info_array[i];
65 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
67 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
71 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
72 if (register_info_array) {
73 for (uint32_t i = 0; i < count; ++i) {
74 if (register_info_array[i].kinds[reg_kind] == reg_num) {
75 info = register_info_array[i];
83 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
84 bool persistent) const {
85 if (!ast_type.IsValid())
86 return ValueObjectSP();
88 ValueObjectSP return_valobj_sp;
90 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
91 if (!return_valobj_sp)
92 return return_valobj_sp;
94 // Now turn this into a persistent variable.
95 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
96 // used in similar form in a couple
97 // of other places. Figure out the correct Create function to do all this
101 Target &target = *thread.CalculateTarget();
102 PersistentExpressionState *persistent_expression_state =
103 target.GetPersistentExpressionStateForLanguage(
104 ast_type.GetMinimumLanguage());
106 if (!persistent_expression_state)
107 return ValueObjectSP();
109 auto prefix = persistent_expression_state->GetPersistentVariablePrefix();
110 ConstString persistent_variable_name =
111 persistent_expression_state->GetNextPersistentVariableName(target,
114 lldb::ValueObjectSP const_valobj_sp;
116 // Check in case our value is already a constant value
117 if (return_valobj_sp->GetIsConstant()) {
118 const_valobj_sp = return_valobj_sp;
119 const_valobj_sp->SetName(persistent_variable_name);
122 return_valobj_sp->CreateConstantValue(persistent_variable_name);
124 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
126 return_valobj_sp = const_valobj_sp;
128 ExpressionVariableSP clang_expr_variable_sp(
129 persistent_expression_state->CreatePersistentVariable(
132 assert(clang_expr_variable_sp);
134 // Set flags and live data as appropriate
136 const Value &result_value = live_valobj_sp->GetValue();
138 switch (result_value.GetValueType()) {
139 case Value::eValueTypeHostAddress:
140 case Value::eValueTypeFileAddress:
141 // we don't do anything with these for now
143 case Value::eValueTypeScalar:
144 case Value::eValueTypeVector:
145 clang_expr_variable_sp->m_flags |=
146 ClangExpressionVariable::EVIsFreezeDried;
147 clang_expr_variable_sp->m_flags |=
148 ClangExpressionVariable::EVIsLLDBAllocated;
149 clang_expr_variable_sp->m_flags |=
150 ClangExpressionVariable::EVNeedsAllocation;
152 case Value::eValueTypeLoadAddress:
153 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
154 clang_expr_variable_sp->m_flags |=
155 ClangExpressionVariable::EVIsProgramReference;
159 return_valobj_sp = clang_expr_variable_sp->GetValueObject();
161 return return_valobj_sp;
164 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
165 bool persistent) const {
166 ValueObjectSP return_valobj_sp;
167 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
168 return return_valobj_sp;
171 // specialized to work with llvm IR types
173 // for now we will specify a default implementation so that we don't need to
175 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
176 llvm::Type &ir_type) const {
177 ValueObjectSP return_valobj_sp;
179 /* this is a dummy and will only be called if an ABI does not override this */
181 return return_valobj_sp;
184 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
185 lldb::addr_t functionAddress,
186 lldb::addr_t returnAddress, llvm::Type &returntype,
187 llvm::ArrayRef<ABI::CallArgument> args) const {
188 // dummy prepare trivial call
189 llvm_unreachable("Should never get here!");
192 bool ABI::GetFallbackRegisterLocation(
193 const RegisterInfo *reg_info,
194 UnwindPlan::Row::RegisterLocation &unwind_regloc) {
195 // Did the UnwindPlan fail to give us the caller's stack pointer? The stack
196 // pointer is defined to be the same as THIS frame's CFA, so return the CFA
197 // value as the caller's stack pointer. This is true on x86-32/x86-64 at
199 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
200 unwind_regloc.SetIsCFAPlusOffset(0);
204 // If a volatile register is being requested, we don't want to forward the
205 // next frame's register contents up the stack -- the register is not
206 // retrievable at this frame.
207 if (RegisterIsVolatile(reg_info)) {
208 unwind_regloc.SetUndefined();