blob: 47ec1061d02ca332f89a579e7bd250b903879369 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ABI.cpp -------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Target/ABI.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000010#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Core/PluginManager.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000012#include "lldb/Core/Value.h"
13#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytona1e5dc82015-08-11 22:53:00 +000014#include "lldb/Symbol/CompilerType.h"
Sean Callanan8f1f9a12015-09-30 19:57:57 +000015#include "lldb/Symbol/TypeSystem.h"
Jim Inghamef651602011-12-22 19:12:40 +000016#include "lldb/Target/Target.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000017#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
Greg Clayton31f1d2f2011-05-11 18:39:18 +000022ABISP
Jason Molenda43294c92017-06-29 02:57:03 +000023ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000024 ABISP abi_sp;
25 ABICreateInstance create_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 for (uint32_t idx = 0;
28 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
29 nullptr;
30 ++idx) {
Jason Molenda43294c92017-06-29 02:57:03 +000031 abi_sp = create_callback(process_sp, arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 if (abi_sp)
34 return abi_sp;
35 }
36 abi_sp.reset();
37 return abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038}
39
Eugene Zelenko9394d7722016-02-18 00:10:17 +000040ABI::~ABI() = default;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
43 uint32_t count = 0;
44 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
45 if (register_info_array) {
46 const char *unique_name_cstr = name.GetCString();
47 uint32_t i;
48 for (i = 0; i < count; ++i) {
49 if (register_info_array[i].name == unique_name_cstr) {
50 info = register_info_array[i];
51 return true;
52 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +000053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 for (i = 0; i < count; ++i) {
55 if (register_info_array[i].alt_name == unique_name_cstr) {
56 info = register_info_array[i];
57 return true;
58 }
59 }
60 }
61 return false;
62}
63
64bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
65 RegisterInfo &info) {
66 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
Greg Clayton56d9a1b2011-08-22 02:49:39 +000067 return false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 uint32_t count = 0;
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];
75 return true;
76 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +000077 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 }
79 return false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000080}
Jim Ingham73ca05a2011-12-17 01:35:57 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
83 bool persistent) const {
84 if (!ast_type.IsValid())
85 return ValueObjectSP();
Jim Inghamef651602011-12-22 19:12:40 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 ValueObjectSP return_valobj_sp;
Jim Inghamef651602011-12-22 19:12:40 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
90 if (!return_valobj_sp)
91 return return_valobj_sp;
Jim Inghamef651602011-12-22 19:12:40 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 // 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
97 // work.
Jim Inghamef651602011-12-22 19:12:40 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 if (persistent) {
Adrian Prantl5435f782018-04-30 23:59:15 +0000100 Target &target = *thread.CalculateTarget();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 PersistentExpressionState *persistent_expression_state =
Adrian Prantl5435f782018-04-30 23:59:15 +0000102 target.GetPersistentExpressionStateForLanguage(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 ast_type.GetMinimumLanguage());
104
105 if (!persistent_expression_state)
106 return ValueObjectSP();
107
Adrian Prantl03219f72018-04-30 23:59:17 +0000108 auto prefix = persistent_expression_state->GetPersistentVariablePrefix();
109 ConstString persistent_variable_name =
110 persistent_expression_state->GetNextPersistentVariableName(target,
111 prefix);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112
113 lldb::ValueObjectSP const_valobj_sp;
114
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);
119 } else
120 const_valobj_sp =
121 return_valobj_sp->CreateConstantValue(persistent_variable_name);
122
123 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
124
125 return_valobj_sp = const_valobj_sp;
126
127 ExpressionVariableSP clang_expr_variable_sp(
128 persistent_expression_state->CreatePersistentVariable(
129 return_valobj_sp));
130
131 assert(clang_expr_variable_sp);
132
133 // Set flags and live data as appropriate
134
135 const Value &result_value = live_valobj_sp->GetValue();
136
137 switch (result_value.GetValueType()) {
138 case Value::eValueTypeHostAddress:
139 case Value::eValueTypeFileAddress:
140 // we don't do anything with these for now
141 break;
142 case Value::eValueTypeScalar:
143 case Value::eValueTypeVector:
144 clang_expr_variable_sp->m_flags |=
145 ClangExpressionVariable::EVIsFreezeDried;
146 clang_expr_variable_sp->m_flags |=
147 ClangExpressionVariable::EVIsLLDBAllocated;
148 clang_expr_variable_sp->m_flags |=
149 ClangExpressionVariable::EVNeedsAllocation;
150 break;
151 case Value::eValueTypeLoadAddress:
152 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
153 clang_expr_variable_sp->m_flags |=
154 ClangExpressionVariable::EVIsProgramReference;
155 break;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157
158 return_valobj_sp = clang_expr_variable_sp->GetValueObject();
159 }
160 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163ValueObjectSP 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;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000168}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000169
Deepak Panickal2526ee52014-07-21 17:21:01 +0000170// specialized to work with llvm IR types
171//
172// for now we will specify a default implementation so that we don't need to
173// modify other ABIs
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
175 llvm::Type &ir_type) const {
176 ValueObjectSP return_valobj_sp;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 /* this is a dummy and will only be called if an ABI does not override this */
Deepak Panickal2526ee52014-07-21 17:21:01 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 return return_valobj_sp;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183bool 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
David Blaikiea322f362017-01-06 00:38:06 +0000188 llvm_unreachable("Should never get here!");
Deepak Panickal2526ee52014-07-21 17:21:01 +0000189}
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191bool ABI::GetFallbackRegisterLocation(
192 const RegisterInfo *reg_info,
193 UnwindPlan::Row::RegisterLocation &unwind_regloc) {
Adrian Prantl05097242018-04-30 16:49:04 +0000194 // 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
197 // least.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
199 unwind_regloc.SetIsCFAPlusOffset(0);
200 return true;
201 }
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 // If a volatile register is being requested, we don't want to forward the
Adrian Prantl05097242018-04-30 16:49:04 +0000204 // next frame's register contents up the stack -- the register is not
205 // retrievable at this frame.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 if (RegisterIsVolatile(reg_info)) {
207 unwind_regloc.SetUndefined();
208 return true;
209 }
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 return false;
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000212}