1 //===-- ABI.cpp -------------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/ABI.h"
10 #include "lldb/Core/PluginManager.h"
11 #include "lldb/Core/Value.h"
12 #include "lldb/Core/ValueObjectConstResult.h"
13 #include "lldb/Expression/ExpressionVariable.h"
14 #include "lldb/Symbol/CompilerType.h"
15 #include "lldb/Symbol/TypeSystem.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Thread.h"
20 using namespace lldb_private;
23 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
25 ABICreateInstance create_callback;
27 for (uint32_t idx = 0;
28 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
31 abi_sp = create_callback(process_sp, arch);
40 ABI::~ABI() = default;
42 bool ABI::GetRegisterInfoByName(ConstString name, RegisterInfo &info) {
44 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
45 if (register_info_array) {
46 const char *unique_name_cstr = name.GetCString();
48 for (i = 0; i < count; ++i) {
49 if (register_info_array[i].name == unique_name_cstr) {
50 info = register_info_array[i];
54 for (i = 0; i < count; ++i) {
55 if (register_info_array[i].alt_name == unique_name_cstr) {
56 info = register_info_array[i];
64 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
66 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
70 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
71 if (register_info_array) {
72 for (uint32_t i = 0; i < count; ++i) {
73 if (register_info_array[i].kinds[reg_kind] == reg_num) {
74 info = register_info_array[i];
82 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
83 bool persistent) const {
84 if (!ast_type.IsValid())
85 return ValueObjectSP();
87 ValueObjectSP return_valobj_sp;
89 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
90 if (!return_valobj_sp)
91 return return_valobj_sp;
93 // Now turn this into a persistent variable.
94 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
95 // used in similar form in a couple
96 // of other places. Figure out the correct Create function to do all this
100 Target &target = *thread.CalculateTarget();
101 PersistentExpressionState *persistent_expression_state =
102 target.GetPersistentExpressionStateForLanguage(
103 ast_type.GetMinimumLanguage());
105 if (!persistent_expression_state)
106 return ValueObjectSP();
108 auto prefix = persistent_expression_state->GetPersistentVariablePrefix();
109 ConstString persistent_variable_name =
110 persistent_expression_state->GetNextPersistentVariableName(target,
113 lldb::ValueObjectSP const_valobj_sp;
115 // Check in case our value is already a constant value
116 if (return_valobj_sp->GetIsConstant()) {
117 const_valobj_sp = return_valobj_sp;
118 const_valobj_sp->SetName(persistent_variable_name);
121 return_valobj_sp->CreateConstantValue(persistent_variable_name);
123 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
125 return_valobj_sp = const_valobj_sp;
127 ExpressionVariableSP expr_variable_sp(
128 persistent_expression_state->CreatePersistentVariable(
131 assert(expr_variable_sp);
133 // Set flags and live data as appropriate
135 const Value &result_value = live_valobj_sp->GetValue();
137 switch (result_value.GetValueType()) {
138 case Value::eValueTypeHostAddress:
139 case Value::eValueTypeFileAddress:
140 // we don't do anything with these for now
142 case Value::eValueTypeScalar:
143 case Value::eValueTypeVector:
144 expr_variable_sp->m_flags |=
145 ExpressionVariable::EVIsFreezeDried;
146 expr_variable_sp->m_flags |=
147 ExpressionVariable::EVIsLLDBAllocated;
148 expr_variable_sp->m_flags |=
149 ExpressionVariable::EVNeedsAllocation;
151 case Value::eValueTypeLoadAddress:
152 expr_variable_sp->m_live_sp = live_valobj_sp;
153 expr_variable_sp->m_flags |=
154 ExpressionVariable::EVIsProgramReference;
158 return_valobj_sp = expr_variable_sp->GetValueObject();
160 return return_valobj_sp;
163 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
164 bool persistent) const {
165 ValueObjectSP return_valobj_sp;
166 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
167 return return_valobj_sp;
170 // specialized to work with llvm IR types
172 // for now we will specify a default implementation so that we don't need to
174 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
175 llvm::Type &ir_type) const {
176 ValueObjectSP return_valobj_sp;
178 /* this is a dummy and will only be called if an ABI does not override this */
180 return return_valobj_sp;
183 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
184 lldb::addr_t functionAddress,
185 lldb::addr_t returnAddress, llvm::Type &returntype,
186 llvm::ArrayRef<ABI::CallArgument> args) const {
187 // dummy prepare trivial call
188 llvm_unreachable("Should never get here!");
191 bool ABI::GetFallbackRegisterLocation(
192 const RegisterInfo *reg_info,
193 UnwindPlan::Row::RegisterLocation &unwind_regloc) {
194 // Did the UnwindPlan fail to give us the caller's stack pointer? The stack
195 // pointer is defined to be the same as THIS frame's CFA, so return the CFA
196 // value as the caller's stack pointer. This is true on x86-32/x86-64 at
198 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
199 unwind_regloc.SetIsCFAPlusOffset(0);
203 // If a volatile register is being requested, we don't want to forward the
204 // next frame's register contents up the stack -- the register is not
205 // retrievable at this frame.
206 if (RegisterIsVolatile(reg_info)) {
207 unwind_regloc.SetUndefined();