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(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(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 ABI::~ABI() = default;
48 
49 bool 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       }
60     }
61     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 
71 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num,
72                                 RegisterInfo &info) {
73   if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds)
74     return false;
75 
76   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       }
84     }
85   }
86   return false;
87 }
88 
89 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
90                                         bool persistent) const {
91   if (!ast_type.IsValid())
92     return ValueObjectSP();
93 
94   ValueObjectSP return_valobj_sp;
95 
96   return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
97   if (!return_valobj_sp)
98     return return_valobj_sp;
99 
100   // 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.
105 
106   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;
160     }
161 
162     return_valobj_sp = clang_expr_variable_sp->GetValueObject();
163   }
164   return return_valobj_sp;
165 }
166 
167 ValueObjectSP 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;
172 }
173 
174 // 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
178 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
179                                                   llvm::Type &ir_type) const {
180   ValueObjectSP return_valobj_sp;
181 
182   /* this is a dummy and will only be called if an ABI does not override this */
183 
184   return return_valobj_sp;
185 }
186 
187 bool 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
192   assert(!"Should never get here!");
193   return false;
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?
200   // The stack pointer is defined to be the same as THIS frame's CFA, so return
201   // the CFA value as
202   // the caller's stack pointer.  This is true on x86-32/x86-64 at 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
210   // up the stack -- the register is not retrievable at this frame.
211   if (RegisterIsVolatile(reg_info)) {
212     unwind_regloc.SetUndefined();
213     return true;
214   }
215 
216   return false;
217 }
218