blob: f5fd594877f113905179407aa706a7a9c2de3f88 [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"
15#include "lldb/Core/PluginManager.h"
Jim Ingham73ca05a2011-12-17 01:35:57 +000016#include "lldb/Core/Value.h"
17#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000018#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.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
Greg Clayton514487e2011-02-15 21:59:32 +000028ABI::FindPlugin (const ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029{
Greg Clayton31f1d2f2011-05-11 18:39:18 +000030 ABISP abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031 ABICreateInstance create_callback;
32
33 for (uint32_t idx = 0;
Eugene Zelenko9394d7722016-02-18 00:10:17 +000034 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) != nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035 ++idx)
36 {
Greg Clayton31f1d2f2011-05-11 18:39:18 +000037 abi_sp = create_callback(arch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
Greg Clayton31f1d2f2011-05-11 18:39:18 +000039 if (abi_sp)
40 return abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041 }
Greg Clayton31f1d2f2011-05-11 18:39:18 +000042 abi_sp.reset();
43 return abi_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044}
45
Eugene Zelenko9394d7722016-02-18 00:10:17 +000046ABI::ABI() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Eugene Zelenko9394d7722016-02-18 00:10:17 +000048ABI::~ABI() = default;
Greg Clayton56d9a1b2011-08-22 02:49:39 +000049
Greg Clayton56d9a1b2011-08-22 02:49:39 +000050bool
51ABI::GetRegisterInfoByName (const ConstString &name, RegisterInfo &info)
52{
53 uint32_t count = 0;
54 const RegisterInfo *register_info_array = GetRegisterInfoArray (count);
55 if (register_info_array)
56 {
57 const char *unique_name_cstr = name.GetCString();
58 uint32_t i;
Eugene Zelenko9394d7722016-02-18 00:10:17 +000059 for (i = 0; i < count; ++i)
Greg Clayton56d9a1b2011-08-22 02:49:39 +000060 {
61 if (register_info_array[i].name == unique_name_cstr)
62 {
63 info = register_info_array[i];
64 return true;
65 }
66 }
Eugene Zelenko9394d7722016-02-18 00:10:17 +000067 for (i = 0; i < count; ++i)
Greg Clayton56d9a1b2011-08-22 02:49:39 +000068 {
69 if (register_info_array[i].alt_name == unique_name_cstr)
70 {
71 info = register_info_array[i];
72 return true;
73 }
74 }
75 }
76 return false;
77}
78
79bool
80ABI::GetRegisterInfoByKind (RegisterKind reg_kind, uint32_t reg_num, RegisterInfo &info)
81{
Jason Molendaa18f7072015-08-15 01:21:01 +000082 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
Greg Clayton56d9a1b2011-08-22 02:49:39 +000083 return false;
84
85 uint32_t count = 0;
86 const RegisterInfo *register_info_array = GetRegisterInfoArray (count);
87 if (register_info_array)
88 {
Eugene Zelenko9394d7722016-02-18 00:10:17 +000089 for (uint32_t i = 0; i < count; ++i)
Greg Clayton56d9a1b2011-08-22 02:49:39 +000090 {
91 if (register_info_array[i].kinds[reg_kind] == reg_num)
92 {
93 info = register_info_array[i];
94 return true;
95 }
96 }
97 }
98 return false;
99}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000100
101ValueObjectSP
102ABI::GetReturnValueObject (Thread &thread,
Greg Claytona1e5dc82015-08-11 22:53:00 +0000103 CompilerType &ast_type,
Greg Clayton1ac04c32012-02-21 00:09:25 +0000104 bool persistent) const
Jim Ingham73ca05a2011-12-17 01:35:57 +0000105{
106 if (!ast_type.IsValid())
107 return ValueObjectSP();
108
Jim Inghamef651602011-12-22 19:12:40 +0000109 ValueObjectSP return_valobj_sp;
110
111 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
112 if (!return_valobj_sp)
113 return return_valobj_sp;
114
115 // Now turn this into a persistent variable.
116 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is used in similar form in a couple
117 // of other places. Figure out the correct Create function to do all this work.
118
119 if (persistent)
Jim Ingham73ca05a2011-12-17 01:35:57 +0000120 {
Sean Callananb92bd752015-10-01 16:28:02 +0000121 PersistentExpressionState *persistent_expression_state = thread.CalculateTarget()->GetPersistentExpressionStateForLanguage(ast_type.GetMinimumLanguage());
122
123 if (!persistent_expression_state)
124 return ValueObjectSP();
125
Sean Callanan8f1f9a12015-09-30 19:57:57 +0000126 ConstString persistent_variable_name (persistent_expression_state->GetNextPersistentVariableName());
Jim Inghamef651602011-12-22 19:12:40 +0000127
128 lldb::ValueObjectSP const_valobj_sp;
129
130 // Check in case our value is already a constant value
131 if (return_valobj_sp->GetIsConstant())
132 {
133 const_valobj_sp = return_valobj_sp;
134 const_valobj_sp->SetName (persistent_variable_name);
135 }
136 else
137 const_valobj_sp = return_valobj_sp->CreateConstantValue (persistent_variable_name);
138
139 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
140
141 return_valobj_sp = const_valobj_sp;
142
Sean Callanan9301ec12015-10-01 23:07:06 +0000143 ExpressionVariableSP clang_expr_variable_sp(persistent_expression_state->CreatePersistentVariable(return_valobj_sp));
Jim Inghamef651602011-12-22 19:12:40 +0000144
Eugene Zelenko9394d7722016-02-18 00:10:17 +0000145 assert (clang_expr_variable_sp);
Jim Inghamef651602011-12-22 19:12:40 +0000146
147 // Set flags and live data as appropriate
148
149 const Value &result_value = live_valobj_sp->GetValue();
150
151 switch (result_value.GetValueType())
152 {
153 case Value::eValueTypeHostAddress:
154 case Value::eValueTypeFileAddress:
155 // we don't do anything with these for now
156 break;
157 case Value::eValueTypeScalar:
Greg Clayton2fb45d02012-10-30 23:56:14 +0000158 case Value::eValueTypeVector:
Sean Callanan31a8d052012-01-05 01:11:09 +0000159 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsFreezeDried;
Jim Inghamef651602011-12-22 19:12:40 +0000160 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
161 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
162 break;
163 case Value::eValueTypeLoadAddress:
164 clang_expr_variable_sp->m_live_sp = live_valobj_sp;
165 clang_expr_variable_sp->m_flags |= ClangExpressionVariable::EVIsProgramReference;
166 break;
167 }
Sean Callanan31a8d052012-01-05 01:11:09 +0000168
Jim Inghamef651602011-12-22 19:12:40 +0000169 return_valobj_sp = clang_expr_variable_sp->GetValueObject();
Jim Ingham73ca05a2011-12-17 01:35:57 +0000170 }
Jim Inghamef651602011-12-22 19:12:40 +0000171 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +0000172}
173
Deepak Panickal2526ee52014-07-21 17:21:01 +0000174ValueObjectSP
175ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type, bool persistent) const
176{
177 ValueObjectSP return_valobj_sp;
178 return_valobj_sp = GetReturnValueObjectImpl( thread, ast_type );
179 return return_valobj_sp;
180}
Jim Ingham73ca05a2011-12-17 01:35:57 +0000181
Deepak Panickal2526ee52014-07-21 17:21:01 +0000182// specialized to work with llvm IR types
183//
184// for now we will specify a default implementation so that we don't need to
185// modify other ABIs
186lldb::ValueObjectSP
187ABI::GetReturnValueObjectImpl( Thread &thread, llvm::Type &ir_type ) const
188{
189 ValueObjectSP return_valobj_sp;
190
191 /* this is a dummy and will only be called if an ABI does not override this */
192
193 return return_valobj_sp;
194}
195
196bool
197ABI::PrepareTrivialCall (Thread &thread,
198 lldb::addr_t sp,
199 lldb::addr_t functionAddress,
200 lldb::addr_t returnAddress,
201 llvm::Type &returntype,
202 llvm::ArrayRef<ABI::CallArgument> args) const
203{
204 // dummy prepare trivial call
205 assert( !"Should never get here!" );
206 return false;
207}
Ulrich Weigand7311bb32016-04-14 14:25:20 +0000208
209bool
210ABI::GetFallbackRegisterLocation (const RegisterInfo *reg_info,
211 UnwindPlan::Row::RegisterLocation &unwind_regloc)
212{
213 // Did the UnwindPlan fail to give us the caller's stack pointer?
214 // The stack pointer is defined to be the same as THIS frame's CFA, so return the CFA value as
215 // the caller's stack pointer. This is true on x86-32/x86-64 at least.
216 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP)
217 {
218 unwind_regloc.SetIsCFAPlusOffset(0);
219 return true;
220 }
221
222 // If a volatile register is being requested, we don't want to forward the next frame's register contents
223 // up the stack -- the register is not retrievable at this frame.
224 if (RegisterIsVolatile(reg_info))
225 {
226 unwind_regloc.SetUndefined();
227 return true;
228 }
229
230 return false;
231}