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(const ArchSpec &arch) {
30 ABICreateInstance create_callback;
32 for (uint32_t idx = 0;
33 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
36 abi_sp = create_callback(arch);
47 ABI::~ABI() = default;
49 bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
51 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
52 if (register_info_array) {
53 const char *unique_name_cstr = name.GetCString();
55 for (i = 0; i < count; ++i) {
56 if (register_info_array[i].name == unique_name_cstr) {
57 info = register_info_array[i];
61 for (i = 0; i < count; ++i) {
62 if (register_info_array[i].alt_name == unique_name_cstr) {
63 info = register_info_array[i];
71 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
73 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
77 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
78 if (register_info_array) {
79 for (uint32_t i = 0; i < count; ++i) {
80 if (register_info_array[i].kinds[reg_kind] == reg_num) {
81 info = register_info_array[i];
89 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
90 bool persistent) const {
91 if (!ast_type.IsValid())
92 return ValueObjectSP();
94 ValueObjectSP return_valobj_sp;
96 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
97 if (!return_valobj_sp)
98 return return_valobj_sp;
100 // Now turn this into a persistent variable.
101 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
102 // used in similar form in a couple
103 // of other places. Figure out the correct Create function to do all this
107 PersistentExpressionState *persistent_expression_state =
108 thread.CalculateTarget()->GetPersistentExpressionStateForLanguage(
109 ast_type.GetMinimumLanguage());
111 if (!persistent_expression_state)
112 return ValueObjectSP();
114 ConstString persistent_variable_name(
115 persistent_expression_state->GetNextPersistentVariableName());
117 lldb::ValueObjectSP const_valobj_sp;
119 // Check in case our value is already a constant value
120 if (return_valobj_sp->GetIsConstant()) {
121 const_valobj_sp = return_valobj_sp;
122 const_valobj_sp->SetName(persistent_variable_name);
125 return_valobj_sp->CreateConstantValue(persistent_variable_name);
127 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
129 return_valobj_sp = const_valobj_sp;
131 ExpressionVariableSP clang_expr_variable_sp(
132 persistent_expression_state->CreatePersistentVariable(
135 assert(clang_expr_variable_sp);
137 // Set flags and live data as appropriate
139 const Value &result_value = live_valobj_sp->GetValue();
141 switch (result_value.GetValueType()) {
142 case Value::eValueTypeHostAddress:
143 case Value::eValueTypeFileAddress:
144 // we don't do anything with these for now
146 case Value::eValueTypeScalar:
147 case Value::eValueTypeVector:
148 clang_expr_variable_sp->m_flags |=
149 ClangExpressionVariable::EVIsFreezeDried;
150 clang_expr_variable_sp->m_flags |=
151 ClangExpressionVariable::EVIsLLDBAllocated;
152 clang_expr_variable_sp->m_flags |=
153 ClangExpressionVariable::EVNeedsAllocation;
155 case Value::eValueTypeLoadAddress:
156 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
157 clang_expr_variable_sp->m_flags |=
158 ClangExpressionVariable::EVIsProgramReference;
162 return_valobj_sp = clang_expr_variable_sp->GetValueObject();
164 return return_valobj_sp;
167 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
168 bool persistent) const {
169 ValueObjectSP return_valobj_sp;
170 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
171 return return_valobj_sp;
174 // specialized to work with llvm IR types
176 // for now we will specify a default implementation so that we don't need to
178 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
179 llvm::Type &ir_type) const {
180 ValueObjectSP return_valobj_sp;
182 /* this is a dummy and will only be called if an ABI does not override this */
184 return return_valobj_sp;
187 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
188 lldb::addr_t functionAddress,
189 lldb::addr_t returnAddress, llvm::Type &returntype,
190 llvm::ArrayRef<ABI::CallArgument> args) const {
191 // dummy prepare trivial call
192 llvm_unreachable("Should never get here!");
195 bool ABI::GetFallbackRegisterLocation(
196 const RegisterInfo *reg_info,
197 UnwindPlan::Row::RegisterLocation &unwind_regloc) {
198 // Did the UnwindPlan fail to give us the caller's stack pointer?
199 // The stack pointer is defined to be the same as THIS frame's CFA, so return
201 // the caller's stack pointer. This is true on x86-32/x86-64 at least.
202 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
203 unwind_regloc.SetIsCFAPlusOffset(0);
207 // If a volatile register is being requested, we don't want to forward the
208 // next frame's register contents
209 // up the stack -- the register is not retrievable at this frame.
210 if (RegisterIsVolatile(reg_info)) {
211 unwind_regloc.SetUndefined();