blob: 96e09b75094aff8fa6048b5811fbeb518c874f01 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ABI.cpp -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/ABI.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/PluginManager.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000013#include "lldb/Core/Value.h"
14#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytona1e5dc82015-08-11 22:53:00 +000015#include "lldb/Symbol/CompilerType.h"
Sean Callanan8f1f9a12015-09-30 19:57:57 +000016#include "lldb/Symbol/TypeSystem.h"
Jim Inghamef651602011-12-22 19:12:40 +000017#include "lldb/Target/Target.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000018#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb;
21using namespace lldb_private;
22
Greg Clayton31f1d2f2011-05-11 18:39:18 +000023ABISP
Jason Molenda43294c92017-06-29 02:57:03 +000024ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000025 ABISP abi_sp;
26 ABICreateInstance create_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028 for (uint32_t idx = 0;
29 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
30 nullptr;
31 ++idx) {
Jason Molenda43294c92017-06-29 02:57:03 +000032 abi_sp = create_callback(process_sp, arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Kate Stoneb9c1b512016-09-06 20:57:50 +000034 if (abi_sp)
35 return abi_sp;
36 }
37 abi_sp.reset();
38 return abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039}
40
Eugene Zelenko9394d7722016-02-18 00:10:17 +000041ABI::~ABI() = default;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
44 uint32_t count = 0;
45 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
46 if (register_info_array) {
47 const char *unique_name_cstr = name.GetCString();
48 uint32_t i;
49 for (i = 0; i < count; ++i) {
50 if (register_info_array[i].name == unique_name_cstr) {
51 info = register_info_array[i];
52 return true;
53 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +000054 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 for (i = 0; i < count; ++i) {
56 if (register_info_array[i].alt_name == unique_name_cstr) {
57 info = register_info_array[i];
58 return true;
59 }
60 }
61 }
62 return false;
63}
64
65bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
66 RegisterInfo &info) {
67 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
Greg Clayton56d9a1b2011-08-22 02:49:39 +000068 return false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 uint32_t count = 0;
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];
76 return true;
77 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +000078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 }
80 return false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000081}
Jim Ingham73ca05a2011-12-17 01:35:57 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
84 bool persistent) const {
85 if (!ast_type.IsValid())
86 return ValueObjectSP();
Jim Inghamef651602011-12-22 19:12:40 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 ValueObjectSP return_valobj_sp;
Jim Inghamef651602011-12-22 19:12:40 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
91 if (!return_valobj_sp)
92 return return_valobj_sp;
Jim Inghamef651602011-12-22 19:12:40 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 // 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
98 // work.
Jim Inghamef651602011-12-22 19:12:40 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 if (persistent) {
Adrian Prantl5435f782018-04-30 23:59:15 +0000101 Target &target = *thread.CalculateTarget();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 PersistentExpressionState *persistent_expression_state =
Adrian Prantl5435f782018-04-30 23:59:15 +0000103 target.GetPersistentExpressionStateForLanguage(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 ast_type.GetMinimumLanguage());
105
106 if (!persistent_expression_state)
107 return ValueObjectSP();
108
Adrian Prantl03219f72018-04-30 23:59:17 +0000109 auto prefix = persistent_expression_state->GetPersistentVariablePrefix();
110 ConstString persistent_variable_name =
111 persistent_expression_state->GetNextPersistentVariableName(target,
112 prefix);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113
114 lldb::ValueObjectSP const_valobj_sp;
115
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);
120 } else
121 const_valobj_sp =
122 return_valobj_sp->CreateConstantValue(persistent_variable_name);
123
124 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
125
126 return_valobj_sp = const_valobj_sp;
127
128 ExpressionVariableSP clang_expr_variable_sp(
129 persistent_expression_state->CreatePersistentVariable(
130 return_valobj_sp));
131
132 assert(clang_expr_variable_sp);
133
134 // Set flags and live data as appropriate
135
136 const Value &result_value = live_valobj_sp->GetValue();
137
138 switch (result_value.GetValueType()) {
139 case Value::eValueTypeHostAddress:
140 case Value::eValueTypeFileAddress:
141 // we don't do anything with these for now
142 break;
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;
151 break;
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;
156 break;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000157 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158
159 return_valobj_sp = clang_expr_variable_sp->GetValueObject();
160 }
161 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164ValueObjectSP 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;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000169}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000170
Deepak Panickal2526ee52014-07-21 17:21:01 +0000171// specialized to work with llvm IR types
172//
173// for now we will specify a default implementation so that we don't need to
174// modify other ABIs
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
176 llvm::Type &ir_type) const {
177 ValueObjectSP return_valobj_sp;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 /* this is a dummy and will only be called if an ABI does not override this */
Deepak Panickal2526ee52014-07-21 17:21:01 +0000180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 return return_valobj_sp;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184bool 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
David Blaikiea322f362017-01-06 00:38:06 +0000189 llvm_unreachable("Should never get here!");
Deepak Panickal2526ee52014-07-21 17:21:01 +0000190}
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192bool ABI::GetFallbackRegisterLocation(
193 const RegisterInfo *reg_info,
194 UnwindPlan::Row::RegisterLocation &unwind_regloc) {
Adrian Prantl05097242018-04-30 16:49:04 +0000195 // 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
198 // least.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
200 unwind_regloc.SetIsCFAPlusOffset(0);
201 return true;
202 }
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 // If a volatile register is being requested, we don't want to forward the
Adrian Prantl05097242018-04-30 16:49:04 +0000205 // next frame's register contents up the stack -- the register is not
206 // retrievable at this frame.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 if (RegisterIsVolatile(reg_info)) {
208 unwind_regloc.SetUndefined();
209 return true;
210 }
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 return false;
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000213}