]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/ABI.cpp
Merge ^/head r336870 through r337615.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / ABI.cpp
1 //===-- 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 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/Target/ABI.h"
15 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Symbol/CompilerType.h"
20 #include "lldb/Symbol/TypeSystem.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/Thread.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 ABISP
28 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
29   ABISP abi_sp;
30   ABICreateInstance create_callback;
31
32   for (uint32_t idx = 0;
33        (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
34        nullptr;
35        ++idx) {
36     abi_sp = create_callback(process_sp, arch);
37
38     if (abi_sp)
39       return abi_sp;
40   }
41   abi_sp.reset();
42   return abi_sp;
43 }
44
45 ABI::~ABI() = default;
46
47 bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) {
48   uint32_t count = 0;
49   const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
50   if (register_info_array) {
51     const char *unique_name_cstr = name.GetCString();
52     uint32_t i;
53     for (i = 0; i < count; ++i) {
54       if (register_info_array[i].name == unique_name_cstr) {
55         info = register_info_array[i];
56         return true;
57       }
58     }
59     for (i = 0; i < count; ++i) {
60       if (register_info_array[i].alt_name == unique_name_cstr) {
61         info = register_info_array[i];
62         return true;
63       }
64     }
65   }
66   return false;
67 }
68
69 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
70                                 RegisterInfo &info) {
71   if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
72     return false;
73
74   uint32_t count = 0;
75   const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
76   if (register_info_array) {
77     for (uint32_t i = 0; i < count; ++i) {
78       if (register_info_array[i].kinds[reg_kind] == reg_num) {
79         info = register_info_array[i];
80         return true;
81       }
82     }
83   }
84   return false;
85 }
86
87 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
88                                         bool persistent) const {
89   if (!ast_type.IsValid())
90     return ValueObjectSP();
91
92   ValueObjectSP return_valobj_sp;
93
94   return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
95   if (!return_valobj_sp)
96     return return_valobj_sp;
97
98   // Now turn this into a persistent variable.
99   // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
100   // used in similar form in a couple
101   // of other places.  Figure out the correct Create function to do all this
102   // work.
103
104   if (persistent) {
105     Target &target = *thread.CalculateTarget();
106     PersistentExpressionState *persistent_expression_state =
107         target.GetPersistentExpressionStateForLanguage(
108             ast_type.GetMinimumLanguage());
109
110     if (!persistent_expression_state)
111       return ValueObjectSP();
112
113     auto prefix = persistent_expression_state->GetPersistentVariablePrefix();
114     ConstString persistent_variable_name =
115         persistent_expression_state->GetNextPersistentVariableName(target,
116                                                                    prefix);
117
118     lldb::ValueObjectSP const_valobj_sp;
119
120     // Check in case our value is already a constant value
121     if (return_valobj_sp->GetIsConstant()) {
122       const_valobj_sp = return_valobj_sp;
123       const_valobj_sp->SetName(persistent_variable_name);
124     } else
125       const_valobj_sp =
126           return_valobj_sp->CreateConstantValue(persistent_variable_name);
127
128     lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
129
130     return_valobj_sp = const_valobj_sp;
131
132     ExpressionVariableSP clang_expr_variable_sp(
133         persistent_expression_state->CreatePersistentVariable(
134             return_valobj_sp));
135
136     assert(clang_expr_variable_sp);
137
138     // Set flags and live data as appropriate
139
140     const Value &result_value = live_valobj_sp->GetValue();
141
142     switch (result_value.GetValueType()) {
143     case Value::eValueTypeHostAddress:
144     case Value::eValueTypeFileAddress:
145       // we don't do anything with these for now
146       break;
147     case Value::eValueTypeScalar:
148     case Value::eValueTypeVector:
149       clang_expr_variable_sp->m_flags |=
150           ClangExpressionVariable::EVIsFreezeDried;
151       clang_expr_variable_sp->m_flags |=
152           ClangExpressionVariable::EVIsLLDBAllocated;
153       clang_expr_variable_sp->m_flags |=
154           ClangExpressionVariable::EVNeedsAllocation;
155       break;
156     case Value::eValueTypeLoadAddress:
157       clang_expr_variable_sp->m_live_sp = live_valobj_sp;
158       clang_expr_variable_sp->m_flags |=
159           ClangExpressionVariable::EVIsProgramReference;
160       break;
161     }
162
163     return_valobj_sp = clang_expr_variable_sp->GetValueObject();
164   }
165   return return_valobj_sp;
166 }
167
168 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type,
169                                         bool persistent) const {
170   ValueObjectSP return_valobj_sp;
171   return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
172   return return_valobj_sp;
173 }
174
175 // specialized to work with llvm IR types
176 //
177 // for now we will specify a default implementation so that we don't need to
178 // modify other ABIs
179 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
180                                                   llvm::Type &ir_type) const {
181   ValueObjectSP return_valobj_sp;
182
183   /* this is a dummy and will only be called if an ABI does not override this */
184
185   return return_valobj_sp;
186 }
187
188 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp,
189                              lldb::addr_t functionAddress,
190                              lldb::addr_t returnAddress, llvm::Type &returntype,
191                              llvm::ArrayRef<ABI::CallArgument> args) const {
192   // dummy prepare trivial call
193   llvm_unreachable("Should never get here!");
194 }
195
196 bool ABI::GetFallbackRegisterLocation(
197     const RegisterInfo *reg_info,
198     UnwindPlan::Row::RegisterLocation &unwind_regloc) {
199   // Did the UnwindPlan fail to give us the caller's stack pointer? The stack
200   // pointer is defined to be the same as THIS frame's CFA, so return the CFA
201   // value as the caller's stack pointer.  This is true on x86-32/x86-64 at
202   // least.
203   if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
204     unwind_regloc.SetIsCFAPlusOffset(0);
205     return true;
206   }
207
208   // If a volatile register is being requested, we don't want to forward the
209   // next frame's register contents up the stack -- the register is not
210   // retrievable at this frame.
211   if (RegisterIsVolatile(reg_info)) {
212     unwind_regloc.SetUndefined();
213     return true;
214   }
215
216   return false;
217 }