blob: 87363a3b8aeb8c57fc53461c16820d795f4d5ea4 [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
Eugene Zelenko9394d7722016-02-18 00:10:17 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Target/ABI.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000015#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/PluginManager.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000017#include "lldb/Core/Value.h"
18#include "lldb/Core/ValueObjectConstResult.h"
Greg Claytona1e5dc82015-08-11 22:53:00 +000019#include "lldb/Symbol/CompilerType.h"
Sean Callanan8f1f9a12015-09-30 19:57:57 +000020#include "lldb/Symbol/TypeSystem.h"
Jim Inghamef651602011-12-22 19:12:40 +000021#include "lldb/Target/Target.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000022#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
Greg Clayton31f1d2f2011-05-11 18:39:18 +000027ABISP
Kate Stoneb9c1b512016-09-06 20:57:50 +000028ABI::FindPlugin(const ArchSpec &arch) {
29 ABISP abi_sp;
30 ABICreateInstance create_callback;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 for (uint32_t idx = 0;
33 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
34 nullptr;
35 ++idx) {
36 abi_sp = create_callback(arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 if (abi_sp)
39 return abi_sp;
40 }
41 abi_sp.reset();
42 return abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
Eugene Zelenko9394d7722016-02-18 00:10:17 +000045ABI::ABI() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Eugene Zelenko9394d7722016-02-18 00:10:17 +000047ABI::~ABI() = default;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
50 uint32_t count = 0;
51 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
52 if (register_info_array) {
53 const char *unique_name_cstr = name.GetCString();
54 uint32_t i;
55 for (i = 0; i < count; ++i) {
56 if (register_info_array[i].name == unique_name_cstr) {
57 info = register_info_array[i];
58 return true;
59 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +000060 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 for (i = 0; i < count; ++i) {
62 if (register_info_array[i].alt_name == unique_name_cstr) {
63 info = register_info_array[i];
64 return true;
65 }
66 }
67 }
68 return false;
69}
70
71bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
72 RegisterInfo &info) {
73 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
Greg Clayton56d9a1b2011-08-22 02:49:39 +000074 return false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 uint32_t count = 0;
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];
82 return true;
83 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +000084 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 }
86 return false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000087}
Jim Ingham73ca05a2011-12-17 01:35:57 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
90 bool persistent) const {
91 if (!ast_type.IsValid())
92 return ValueObjectSP();
Jim Inghamef651602011-12-22 19:12:40 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 ValueObjectSP return_valobj_sp;
Jim Inghamef651602011-12-22 19:12:40 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
97 if (!return_valobj_sp)
98 return return_valobj_sp;
Jim Inghamef651602011-12-22 19:12:40 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 // 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
104 // work.
Jim Inghamef651602011-12-22 19:12:40 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 if (persistent) {
107 PersistentExpressionState *persistent_expression_state =
108 thread.CalculateTarget()->GetPersistentExpressionStateForLanguage(
109 ast_type.GetMinimumLanguage());
110
111 if (!persistent_expression_state)
112 return ValueObjectSP();
113
114 ConstString persistent_variable_name(
115 persistent_expression_state->GetNextPersistentVariableName());
116
117 lldb::ValueObjectSP const_valobj_sp;
118
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);
123 } else
124 const_valobj_sp =
125 return_valobj_sp->CreateConstantValue(persistent_variable_name);
126
127 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
128
129 return_valobj_sp = const_valobj_sp;
130
131 ExpressionVariableSP clang_expr_variable_sp(
132 persistent_expression_state->CreatePersistentVariable(
133 return_valobj_sp));
134
135 assert(clang_expr_variable_sp);
136
137 // Set flags and live data as appropriate
138
139 const Value &result_value = live_valobj_sp->GetValue();
140
141 switch (result_value.GetValueType()) {
142 case Value::eValueTypeHostAddress:
143 case Value::eValueTypeFileAddress:
144 // we don't do anything with these for now
145 break;
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;
154 break;
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;
159 break;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000160 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161
162 return_valobj_sp = clang_expr_variable_sp->GetValueObject();
163 }
164 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167ValueObjectSP 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;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000172}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000173
Deepak Panickal2526ee52014-07-21 17:21:01 +0000174// specialized to work with llvm IR types
175//
176// for now we will specify a default implementation so that we don't need to
177// modify other ABIs
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
179 llvm::Type &ir_type) const {
180 ValueObjectSP return_valobj_sp;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 /* this is a dummy and will only be called if an ABI does not override this */
Deepak Panickal2526ee52014-07-21 17:21:01 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 return return_valobj_sp;
Deepak Panickal2526ee52014-07-21 17:21:01 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187bool 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
David Blaikiea322f362017-01-06 00:38:06 +0000192 llvm_unreachable("Should never get here!");
Deepak Panickal2526ee52014-07-21 17:21:01 +0000193}
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195bool 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
200 // the CFA value as
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);
204 return true;
205 }
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 // 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();
212 return true;
213 }
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000214
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 return false;
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000216}