Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ABI.cpp -------------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Target/ABI.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10 | #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "lldb/Core/PluginManager.h" |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Value.h" |
| 13 | #include "lldb/Core/ValueObjectConstResult.h" |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/CompilerType.h" |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/TypeSystem.h" |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Target.h" |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Thread.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 22 | ABISP |
Jason Molenda | 43294c9 | 2017-06-29 02:57:03 +0000 | [diff] [blame] | 23 | ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | ABISP abi_sp; |
| 25 | ABICreateInstance create_callback; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | for (uint32_t idx = 0; |
| 28 | (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) != |
| 29 | nullptr; |
| 30 | ++idx) { |
Jason Molenda | 43294c9 | 2017-06-29 02:57:03 +0000 | [diff] [blame] | 31 | abi_sp = create_callback(process_sp, arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | if (abi_sp) |
| 34 | return abi_sp; |
| 35 | } |
| 36 | abi_sp.reset(); |
| 37 | return abi_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 40 | ABI::~ABI() = default; |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | bool ABI::GetRegisterInfoByName(const ConstString &name, RegisterInfo &info) { |
| 43 | uint32_t count = 0; |
| 44 | const RegisterInfo *register_info_array = GetRegisterInfoArray(count); |
| 45 | if (register_info_array) { |
| 46 | const char *unique_name_cstr = name.GetCString(); |
| 47 | uint32_t i; |
| 48 | for (i = 0; i < count; ++i) { |
| 49 | if (register_info_array[i].name == unique_name_cstr) { |
| 50 | info = register_info_array[i]; |
| 51 | return true; |
| 52 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 53 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | for (i = 0; i < count; ++i) { |
| 55 | if (register_info_array[i].alt_name == unique_name_cstr) { |
| 56 | info = register_info_array[i]; |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num, |
| 65 | RegisterInfo &info) { |
| 66 | if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds) |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 67 | return false; |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | uint32_t count = 0; |
| 70 | const RegisterInfo *register_info_array = GetRegisterInfoArray(count); |
| 71 | if (register_info_array) { |
| 72 | for (uint32_t i = 0; i < count; ++i) { |
| 73 | if (register_info_array[i].kinds[reg_kind] == reg_num) { |
| 74 | info = register_info_array[i]; |
| 75 | return true; |
| 76 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 77 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | } |
| 79 | return false; |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 80 | } |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type, |
| 83 | bool persistent) const { |
| 84 | if (!ast_type.IsValid()) |
| 85 | return ValueObjectSP(); |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | ValueObjectSP return_valobj_sp; |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type); |
| 90 | if (!return_valobj_sp) |
| 91 | return return_valobj_sp; |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | // Now turn this into a persistent variable. |
| 94 | // FIXME: This code is duplicated from Target::EvaluateExpression, and it is |
| 95 | // used in similar form in a couple |
| 96 | // of other places. Figure out the correct Create function to do all this |
| 97 | // work. |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | if (persistent) { |
Adrian Prantl | 5435f78 | 2018-04-30 23:59:15 +0000 | [diff] [blame] | 100 | Target &target = *thread.CalculateTarget(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | PersistentExpressionState *persistent_expression_state = |
Adrian Prantl | 5435f78 | 2018-04-30 23:59:15 +0000 | [diff] [blame] | 102 | target.GetPersistentExpressionStateForLanguage( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | ast_type.GetMinimumLanguage()); |
| 104 | |
| 105 | if (!persistent_expression_state) |
| 106 | return ValueObjectSP(); |
| 107 | |
Adrian Prantl | 03219f7 | 2018-04-30 23:59:17 +0000 | [diff] [blame] | 108 | auto prefix = persistent_expression_state->GetPersistentVariablePrefix(); |
| 109 | ConstString persistent_variable_name = |
| 110 | persistent_expression_state->GetNextPersistentVariableName(target, |
| 111 | prefix); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | |
| 113 | lldb::ValueObjectSP const_valobj_sp; |
| 114 | |
| 115 | // Check in case our value is already a constant value |
| 116 | if (return_valobj_sp->GetIsConstant()) { |
| 117 | const_valobj_sp = return_valobj_sp; |
| 118 | const_valobj_sp->SetName(persistent_variable_name); |
| 119 | } else |
| 120 | const_valobj_sp = |
| 121 | return_valobj_sp->CreateConstantValue(persistent_variable_name); |
| 122 | |
| 123 | lldb::ValueObjectSP live_valobj_sp = return_valobj_sp; |
| 124 | |
| 125 | return_valobj_sp = const_valobj_sp; |
| 126 | |
| 127 | ExpressionVariableSP clang_expr_variable_sp( |
| 128 | persistent_expression_state->CreatePersistentVariable( |
| 129 | return_valobj_sp)); |
| 130 | |
| 131 | assert(clang_expr_variable_sp); |
| 132 | |
| 133 | // Set flags and live data as appropriate |
| 134 | |
| 135 | const Value &result_value = live_valobj_sp->GetValue(); |
| 136 | |
| 137 | switch (result_value.GetValueType()) { |
| 138 | case Value::eValueTypeHostAddress: |
| 139 | case Value::eValueTypeFileAddress: |
| 140 | // we don't do anything with these for now |
| 141 | break; |
| 142 | case Value::eValueTypeScalar: |
| 143 | case Value::eValueTypeVector: |
| 144 | clang_expr_variable_sp->m_flags |= |
| 145 | ClangExpressionVariable::EVIsFreezeDried; |
| 146 | clang_expr_variable_sp->m_flags |= |
| 147 | ClangExpressionVariable::EVIsLLDBAllocated; |
| 148 | clang_expr_variable_sp->m_flags |= |
| 149 | ClangExpressionVariable::EVNeedsAllocation; |
| 150 | break; |
| 151 | case Value::eValueTypeLoadAddress: |
| 152 | clang_expr_variable_sp->m_live_sp = live_valobj_sp; |
| 153 | clang_expr_variable_sp->m_flags |= |
| 154 | ClangExpressionVariable::EVIsProgramReference; |
| 155 | break; |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 156 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | |
| 158 | return_valobj_sp = clang_expr_variable_sp->GetValueObject(); |
| 159 | } |
| 160 | return return_valobj_sp; |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type, |
| 164 | bool persistent) const { |
| 165 | ValueObjectSP return_valobj_sp; |
| 166 | return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type); |
| 167 | return return_valobj_sp; |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 168 | } |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 169 | |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 170 | // specialized to work with llvm IR types |
| 171 | // |
| 172 | // for now we will specify a default implementation so that we don't need to |
| 173 | // modify other ABIs |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread, |
| 175 | llvm::Type &ir_type) const { |
| 176 | ValueObjectSP return_valobj_sp; |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | /* this is a dummy and will only be called if an ABI does not override this */ |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | return return_valobj_sp; |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp, |
| 184 | lldb::addr_t functionAddress, |
| 185 | lldb::addr_t returnAddress, llvm::Type &returntype, |
| 186 | llvm::ArrayRef<ABI::CallArgument> args) const { |
| 187 | // dummy prepare trivial call |
David Blaikie | a322f36 | 2017-01-06 00:38:06 +0000 | [diff] [blame] | 188 | llvm_unreachable("Should never get here!"); |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 189 | } |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | bool ABI::GetFallbackRegisterLocation( |
| 192 | const RegisterInfo *reg_info, |
| 193 | UnwindPlan::Row::RegisterLocation &unwind_regloc) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 194 | // Did the UnwindPlan fail to give us the caller's stack pointer? The stack |
| 195 | // pointer is defined to be the same as THIS frame's CFA, so return the CFA |
| 196 | // value as the caller's stack pointer. This is true on x86-32/x86-64 at |
| 197 | // least. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) { |
| 199 | unwind_regloc.SetIsCFAPlusOffset(0); |
| 200 | return true; |
| 201 | } |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 203 | // If a volatile register is being requested, we don't want to forward the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 204 | // next frame's register contents up the stack -- the register is not |
| 205 | // retrievable at this frame. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | if (RegisterIsVolatile(reg_info)) { |
| 207 | unwind_regloc.SetUndefined(); |
| 208 | return true; |
| 209 | } |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 210 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 211 | return false; |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 212 | } |