blob: 862f63b290813092941349585d51db159cb124b8 [file] [log] [blame]
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001//===-- IRForTarget.cpp -----------------------------------------*- C++ -*-===//
Sean Callanan2ab712f22010-07-03 01:35:46 +00002//
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
Sean Callanan4dbb2712015-09-25 20:35:58 +000010#include "IRForTarget.h"
11
12#include "ClangExpressionDeclMap.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000013
14#include "llvm/Support/raw_ostream.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/InstrTypes.h"
18#include "llvm/IR/Instructions.h"
19#include "llvm/IR/Intrinsics.h"
20#include "llvm/IR/Module.h"
Ilia Kc9a475d2015-02-13 10:49:18 +000021#include "llvm/IR/LegacyPassManager.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000022#include "llvm/Transforms/IPO.h"
Zachary Turner543afa12014-12-09 22:29:47 +000023#include "llvm/IR/Metadata.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000024#include "llvm/IR/ValueSymbolTable.h"
Sean Callanan549c9f72010-07-13 21:41:46 +000025
26#include "clang/AST/ASTContext.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000027
28#include "lldb/Core/dwarf.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000029#include "lldb/Core/ConstString.h"
30#include "lldb/Core/DataBufferHeap.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000031#include "lldb/Core/Log.h"
32#include "lldb/Core/Scalar.h"
33#include "lldb/Core/StreamString.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000034#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000035#include "lldb/Expression/IRInterpreter.h"
Sean Callanan79763a42011-05-23 21:40:23 +000036#include "lldb/Host/Endian.h"
Sean Callanan63697e52011-05-07 01:06:41 +000037#include "lldb/Symbol/ClangASTContext.h"
Greg Claytona1e5dc82015-08-11 22:53:00 +000038#include "lldb/Symbol/CompilerType.h"
Siva Chandra0f404e02015-04-09 18:48:34 +000039#include "lldb/Target/CPPLanguageRuntime.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000040
41#include <map>
42
43using namespace llvm;
44
Sean Callananeaacbc92010-08-18 18:50:51 +000045static char ID;
46
Sean Callanan8dfb68e2013-03-19 00:10:07 +000047IRForTarget::StaticDataAllocator::StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit) :
48 m_execution_unit(execution_unit),
Sean Callanan1582ee62013-04-18 22:06:33 +000049 m_stream_string(lldb_private::Stream::eBinary, execution_unit.GetAddressByteSize(), execution_unit.GetByteOrder()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000050 m_allocation(LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +000051{
52}
53
Sean Callanan1f9db3e2013-06-28 21:44:15 +000054IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker) :
55 m_maker(maker),
56 m_values()
57{
58}
59
60IRForTarget::FunctionValueCache::~FunctionValueCache()
61{
62}
63
Greg Clayton526ae042015-02-12 00:34:25 +000064llvm::Value *
65IRForTarget::FunctionValueCache::GetValue(llvm::Function *function)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000066{
Sean Callanan1f9db3e2013-06-28 21:44:15 +000067 if (!m_values.count(function))
68 {
69 llvm::Value *ret = m_maker(function);
70 m_values[function] = ret;
71 return ret;
72 }
73 return m_values[function];
74}
75
Greg Clayton526ae042015-02-12 00:34:25 +000076lldb::addr_t
77IRForTarget::StaticDataAllocator::Allocate()
Sean Callanan79763a42011-05-23 21:40:23 +000078{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000079 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000080
Sean Callanan8dfb68e2013-03-19 00:10:07 +000081 if (m_allocation != LLDB_INVALID_ADDRESS)
82 {
83 m_execution_unit.FreeNow(m_allocation);
84 m_allocation = LLDB_INVALID_ADDRESS;
85 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000086
Sean Callanan8dfb68e2013-03-19 00:10:07 +000087 m_allocation = m_execution_unit.WriteNow((const uint8_t*)m_stream_string.GetData(), m_stream_string.GetSize(), err);
88
89 return m_allocation;
Sean Callanan79763a42011-05-23 21:40:23 +000090}
91
Greg Clayton526ae042015-02-12 00:34:25 +000092lldb::TargetSP
93IRForTarget::StaticDataAllocator::GetTarget()
94{
95 return m_execution_unit.GetTarget();
96}
97
98static llvm::Value *
99FindEntryInstruction (llvm::Function *function)
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000100{
101 if (function->empty())
102 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000103
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000104 return function->getEntryBlock().getFirstNonPHIOrDbg();
105}
106
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000107IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
108 bool resolve_vars,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000109 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +0000110 lldb_private::Stream *error_stream,
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000111 const char *func_name) :
Sean Callanane2ef6e32010-09-23 03:01:22 +0000112 ModulePass(ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000113 m_resolve_vars(resolve_vars),
114 m_func_name(func_name),
Sean Callanan79763a42011-05-23 21:40:23 +0000115 m_module(NULL),
Johnny Chen44805302011-07-19 19:48:13 +0000116 m_decl_map(decl_map),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000117 m_data_allocator(execution_unit),
Sean Callananafe16a72010-11-17 23:00:36 +0000118 m_CFStringCreateWithBytes(NULL),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000119 m_sel_registerName(NULL),
Sean Callanan439dcae2013-12-20 19:55:02 +0000120 m_intptr_ty(NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000121 m_error_stream(error_stream),
Sean Callanan63697e52011-05-07 01:06:41 +0000122 m_result_store(NULL),
123 m_result_is_pointer(false),
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000124 m_reloc_placeholder(NULL),
125 m_entry_instruction_finder (FindEntryInstruction)
Sean Callanan2ab712f22010-07-03 01:35:46 +0000126{
127}
128
Sean Callanan038df5032010-09-30 21:18:25 +0000129/* Handy utility functions used at several places in the code */
Sean Callanan2235f322010-08-11 03:57:18 +0000130
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000131static std::string
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000132PrintValue(const Value *value, bool truncate = false)
Sean Callanan2235f322010-08-11 03:57:18 +0000133{
134 std::string s;
Jim Ingham28eb5712012-10-12 17:34:26 +0000135 if (value)
136 {
137 raw_string_ostream rso(s);
138 value->print(rso);
139 rso.flush();
140 if (truncate)
141 s.resize(s.length() - 1);
142 }
Sean Callanan2235f322010-08-11 03:57:18 +0000143 return s;
144}
145
Sean Callanan038df5032010-09-30 21:18:25 +0000146static std::string
Greg Clayton57ee3062013-07-11 22:46:58 +0000147PrintType(const llvm::Type *type, bool truncate = false)
Sean Callanan038df5032010-09-30 21:18:25 +0000148{
149 std::string s;
150 raw_string_ostream rso(s);
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000151 type->print(rso);
Sean Callanan038df5032010-09-30 21:18:25 +0000152 rso.flush();
153 if (truncate)
154 s.resize(s.length() - 1);
155 return s;
156}
157
Sean Callanan2ab712f22010-07-03 01:35:46 +0000158IRForTarget::~IRForTarget()
159{
160}
161
Sean Callanan79763a42011-05-23 21:40:23 +0000162bool
163IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
164{
Sean Callanan79763a42011-05-23 21:40:23 +0000165 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000166
Sean Callanan7f27d602011-11-19 02:54:21 +0000167 std::string name = llvm_function.getName().str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000168
Sean Callanan79763a42011-05-23 21:40:23 +0000169 return true;
170}
171
Greg Clayton23f8c952014-03-24 23:10:19 +0000172IRForTarget::LookupResult
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000173IRForTarget::GetFunctionAddress (llvm::Function *fun,
174 uint64_t &fun_addr,
175 lldb_private::ConstString &name,
176 Constant **&value_ptr)
177{
Greg Clayton5160ce52013-03-27 23:08:40 +0000178 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000179
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000180 fun_addr = LLDB_INVALID_ADDRESS;
181 name.Clear();
182 value_ptr = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000183
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000184 if (fun->isIntrinsic())
185 {
186 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000187
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000188 switch (intrinsic_id)
189 {
190 default:
191 if (log)
192 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000193
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000194 if (m_error_stream)
195 m_error_stream->Printf("Internal error [IRForTarget]: Call to unhandled compiler intrinsic '%s'\n", Intrinsic::getName(intrinsic_id).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000196
Jason Molenda52d76032014-07-09 01:10:37 +0000197 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000198 case Intrinsic::memcpy:
199 {
200 static lldb_private::ConstString g_memcpy_str ("memcpy");
201 name = g_memcpy_str;
202 }
203 break;
Sean Callanana6cbf062011-11-16 00:20:50 +0000204 case Intrinsic::memset:
205 {
206 static lldb_private::ConstString g_memset_str ("memset");
207 name = g_memset_str;
208 }
209 break;
Greg Clayton23f8c952014-03-24 23:10:19 +0000210 case Intrinsic::dbg_declare:
Sean Callanan131be992014-04-09 00:59:41 +0000211 case Intrinsic::dbg_value:
Greg Clayton23f8c952014-03-24 23:10:19 +0000212 return LookupResult::Ignore;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000213 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000214
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000215 if (log && name)
216 log->Printf("Resolved intrinsic name \"%s\"", name.GetCString());
217 }
218 else
219 {
220 name.SetCStringWithLength (fun->getName().data(), fun->getName().size());
221 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000222
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000223 // Find the address of the function.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000225 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000226
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000227 if (fun_decl)
228 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000229 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_addr))
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000230 {
Siva Chandra0f404e02015-04-09 18:48:34 +0000231 std::vector<lldb_private::ConstString> alternates;
Greg Claytonf0705c82011-10-22 03:33:13 +0000232 bool found_it = m_decl_map->GetFunctionAddress (name, fun_addr);
233 if (!found_it)
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000234 {
Siva Chandra0f404e02015-04-09 18:48:34 +0000235 if (log)
236 log->Printf("Address of function \"%s\" not found.\n", name.GetCString());
237 // Check for an alternate mangling for names from the standard library.
238 // For example, "std::basic_string<...>" has an alternate mangling scheme per
239 // the Itanium C++ ABI.
240 lldb::ProcessSP process_sp = m_data_allocator.GetTarget()->GetProcessSP();
Sean Callanan235de0a2015-05-28 20:06:40 +0000241 if (process_sp)
Greg Claytonf0705c82011-10-22 03:33:13 +0000242 {
Sean Callanan235de0a2015-05-28 20:06:40 +0000243 lldb_private::CPPLanguageRuntime *cpp_runtime = process_sp->GetCPPLanguageRuntime();
244 if (cpp_runtime && cpp_runtime->GetAlternateManglings(name, alternates))
Siva Chandra0f404e02015-04-09 18:48:34 +0000245 {
Sean Callanan235de0a2015-05-28 20:06:40 +0000246 for (size_t i = 0; i < alternates.size(); ++i)
Siva Chandra0f404e02015-04-09 18:48:34 +0000247 {
Sean Callanan235de0a2015-05-28 20:06:40 +0000248 const lldb_private::ConstString &alternate_name = alternates[i];
Siva Chandra0f404e02015-04-09 18:48:34 +0000249 if (log)
Sean Callanan235de0a2015-05-28 20:06:40 +0000250 log->Printf("Looking up address of function \"%s\" with alternate name \"%s\"",
Siva Chandra0f404e02015-04-09 18:48:34 +0000251 name.GetCString(), alternate_name.GetCString());
Sean Callanan235de0a2015-05-28 20:06:40 +0000252 if ((found_it = m_decl_map->GetFunctionAddress (alternate_name, fun_addr)))
253 {
254 if (log)
255 log->Printf("Found address of function \"%s\" with alternate name \"%s\"",
256 name.GetCString(), alternate_name.GetCString());
257 break;
258 }
Siva Chandra0f404e02015-04-09 18:48:34 +0000259 }
260 }
Greg Claytonf0705c82011-10-22 03:33:13 +0000261 }
262 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Greg Claytonf0705c82011-10-22 03:33:13 +0000264 if (!found_it)
265 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000266 lldb_private::Mangled mangled_name(name);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000267 if (m_error_stream)
Greg Claytonf0705c82011-10-22 03:33:13 +0000268 {
Siva Chandra0f404e02015-04-09 18:48:34 +0000269 if (mangled_name.GetMangledName())
Greg Claytonda1eb042013-04-23 21:48:38 +0000270 m_error_stream->Printf("error: call to a function '%s' ('%s') that is not present in the target\n",
Greg Claytonddaf6a72015-07-08 22:32:23 +0000271 mangled_name.GetName(lldb::eLanguageTypeObjC_plus_plus).GetCString(),
Greg Claytonda1eb042013-04-23 21:48:38 +0000272 mangled_name.GetMangledName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000273 else
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000274 m_error_stream->Printf("error: call to a function '%s' that is not present in the target\n",
Greg Claytonddaf6a72015-07-08 22:32:23 +0000275 mangled_name.GetName(lldb::eLanguageTypeObjC_plus_plus).GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000276 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000277 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000278 }
279 }
280 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000281 else
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000282 {
283 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
284 {
285 if (log)
286 log->Printf ("Metadataless function \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000287
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000288 if (m_error_stream)
289 m_error_stream->Printf("Error [IRForTarget]: Call to a symbol-only function '%s' that is not present in the target\n", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000290
Greg Clayton23f8c952014-03-24 23:10:19 +0000291 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000292 }
293 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000294
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000295 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000296 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), fun_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297
Greg Clayton23f8c952014-03-24 23:10:19 +0000298 return LookupResult::Success;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000299}
300
301llvm::Constant *
302IRForTarget::BuildFunctionPointer (llvm::Type *type,
303 uint64_t ptr)
304{
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000305 PointerType *fun_ptr_ty = PointerType::getUnqual(type);
Sean Callanan439dcae2013-12-20 19:55:02 +0000306 Constant *fun_addr_int = ConstantInt::get(m_intptr_ty, ptr, false);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000307 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
308}
309
Sean Callananfc8feb82011-10-31 22:11:40 +0000310void
311IRForTarget::RegisterFunctionMetadata(LLVMContext &context,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312 llvm::Value *function_ptr,
Sean Callananfc8feb82011-10-31 22:11:40 +0000313 const char *name)
314{
Ed Mastea8553092014-03-10 17:24:16 +0000315 for (llvm::User *user : function_ptr->users())
Sean Callananfc8feb82011-10-31 22:11:40 +0000316 {
Sean Callananfc8feb82011-10-31 22:11:40 +0000317 if (Instruction *user_inst = dyn_cast<Instruction>(user))
318 {
Daniel Maleaf051dbc2013-06-03 20:45:54 +0000319 MDString* md_name = MDString::get(context, StringRef(name));
320
321 MDNode *metadata = MDNode::get(context, md_name);
322
Sean Callananfc8feb82011-10-31 22:11:40 +0000323 user_inst->setMetadata("lldb.call.realName", metadata);
324 }
325 else
326 {
327 RegisterFunctionMetadata (context, user, name);
328 }
329 }
330}
331
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000332bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000333IRForTarget::ResolveFunctionPointers(llvm::Module &llvm_module)
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000334{
Greg Clayton5160ce52013-03-27 23:08:40 +0000335 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000337 for (llvm::Module::iterator fi = llvm_module.begin();
338 fi != llvm_module.end();
339 ++fi)
340 {
341 Function *fun = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000342
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000343 bool is_decl = fun->isDeclaration();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000345 if (log)
Sean Callanan7f27d602011-11-19 02:54:21 +0000346 log->Printf("Examining %s function %s", (is_decl ? "declaration" : "non-declaration"), fun->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000347
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000348 if (!is_decl)
349 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000350
Sean Callanan339f6152014-03-11 19:19:16 +0000351 if (fun->use_empty())
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000352 continue; // ignore
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000353
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000354 uint64_t addr = LLDB_INVALID_ADDRESS;
355 lldb_private::ConstString name;
356 Constant **value_ptr = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000357
Greg Clayton23f8c952014-03-24 23:10:19 +0000358 LookupResult result = GetFunctionAddress(fun,
359 addr,
360 name,
361 value_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000362
Greg Clayton23f8c952014-03-24 23:10:19 +0000363 switch (result)
364 {
365 case LookupResult::Fail:
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000366 return false; // GetFunctionAddress reports its own errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000367
Greg Clayton23f8c952014-03-24 23:10:19 +0000368 case LookupResult::Ignore:
369 break; // Nothing to do
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000370
Greg Clayton23f8c952014-03-24 23:10:19 +0000371 case LookupResult::Success:
372 {
373 Constant *value = BuildFunctionPointer(fun->getFunctionType(), addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000374
Greg Clayton23f8c952014-03-24 23:10:19 +0000375 RegisterFunctionMetadata (llvm_module.getContext(), fun, name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000376
Greg Clayton23f8c952014-03-24 23:10:19 +0000377 if (value_ptr)
378 *value_ptr = value;
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000379
Greg Clayton23f8c952014-03-24 23:10:19 +0000380 // If we are replacing a function with the nobuiltin attribute, it may
381 // be called with the builtin attribute on call sites. Remove any such
382 // attributes since it's illegal to have a builtin call to something
383 // other than a nobuiltin function.
384 if (fun->hasFnAttribute(llvm::Attribute::NoBuiltin)) {
385 llvm::Attribute builtin = llvm::Attribute::get(fun->getContext(), llvm::Attribute::Builtin);
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000386
Greg Clayton23f8c952014-03-24 23:10:19 +0000387 for (auto u : fun->users()) {
388 if (auto call = dyn_cast<CallInst>(u)) {
389 call->removeAttribute(AttributeSet::FunctionIndex, builtin);
390 }
391 }
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000392 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000393
Greg Clayton23f8c952014-03-24 23:10:19 +0000394 fun->replaceAllUsesWith(value);
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000395 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000396 break;
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000397 }
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000398 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000399
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000400 return true;
401}
402
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000403
Sean Callanan63697e52011-05-07 01:06:41 +0000404clang::NamedDecl *
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000405IRForTarget::DeclForGlobal (const GlobalValue *global_val, Module *module)
Sean Callanan63697e52011-05-07 01:06:41 +0000406{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000407 NamedMDNode *named_metadata = module->getNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408
Sean Callanan63697e52011-05-07 01:06:41 +0000409 if (!named_metadata)
410 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000411
Sean Callanan63697e52011-05-07 01:06:41 +0000412 unsigned num_nodes = named_metadata->getNumOperands();
413 unsigned node_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000414
Sean Callanan63697e52011-05-07 01:06:41 +0000415 for (node_index = 0;
416 node_index < num_nodes;
417 ++node_index)
418 {
Zachary Turner0d594e12014-11-05 18:37:53 +0000419 llvm::MDNode *metadata_node = dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
Sean Callanan63697e52011-05-07 01:06:41 +0000420 if (!metadata_node)
421 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000422
Sean Callanan63697e52011-05-07 01:06:41 +0000423 if (metadata_node->getNumOperands() != 2)
424 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425
Zachary Turner543afa12014-12-09 22:29:47 +0000426 if (mdconst::dyn_extract_or_null<GlobalValue>(metadata_node->getOperand(0)) != global_val)
Sean Callanan63697e52011-05-07 01:06:41 +0000427 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000428
Zachary Turner543afa12014-12-09 22:29:47 +0000429 ConstantInt *constant_int = mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000430
Sean Callanan63697e52011-05-07 01:06:41 +0000431 if (!constant_int)
432 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000433
Sean Callanan63697e52011-05-07 01:06:41 +0000434 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000435
Sean Callanan63697e52011-05-07 01:06:41 +0000436 return reinterpret_cast<clang::NamedDecl *>(ptr);
437 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000438
Sean Callanan63697e52011-05-07 01:06:41 +0000439 return NULL;
440}
441
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000442clang::NamedDecl *
443IRForTarget::DeclForGlobal (GlobalValue *global_val)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000444{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000445 return DeclForGlobal(global_val, m_module);
446}
447
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000448bool
Sean Callanan79763a42011-05-23 21:40:23 +0000449IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanand1e5b432010-08-12 01:56:52 +0000450{
Greg Clayton5160ce52013-03-27 23:08:40 +0000451 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000452
Sean Callanan9e6ed532010-09-13 21:34:21 +0000453 if (!m_resolve_vars)
454 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000455
Sean Callanan9e6ed532010-09-13 21:34:21 +0000456 // Find the result variable. If it doesn't exist, we can give up right here.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000457
Sean Callanan79763a42011-05-23 21:40:23 +0000458 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000459
Sean Callanancc427fa2011-07-30 02:42:06 +0000460 std::string result_name_str;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000461 const char *result_name = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000462
Sean Callananfc55f5d2010-09-21 00:44:12 +0000463 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
464 vi != ve;
465 ++vi)
466 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000467 result_name_str = vi->first().str();
468 const char *value_name = result_name_str.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000469
Sean Callanancc427fa2011-07-30 02:42:06 +0000470 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000471 strncmp(value_name, "_ZGV", 4))
Sean Callanan92adcac2011-01-13 08:53:35 +0000472 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000473 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000474 m_result_is_pointer = true;
475 break;
476 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000477
Sean Callanancc427fa2011-07-30 02:42:06 +0000478 if (strstr(value_name, "$__lldb_expr_result") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000479 strncmp(value_name, "_ZGV", 4))
Sean Callanan46ae9e52010-09-28 21:13:03 +0000480 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000481 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000482 m_result_is_pointer = false;
Sean Callanan46ae9e52010-09-28 21:13:03 +0000483 break;
484 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000485 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000486
Sean Callananfc55f5d2010-09-21 00:44:12 +0000487 if (!result_name)
488 {
489 if (log)
490 log->PutCString("Couldn't find result variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000491
Richard Mitton00dec202013-10-11 19:44:23 +0000492 return true;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000493 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000494
Sean Callanan46ae9e52010-09-28 21:13:03 +0000495 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000496 log->Printf("Result name: \"%s\"", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000497
Sean Callanan79763a42011-05-23 21:40:23 +0000498 Value *result_value = m_module->getNamedValue(result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000499
Sean Callanand1e5b432010-08-12 01:56:52 +0000500 if (!result_value)
501 {
502 if (log)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000503 log->PutCString("Result variable had no data");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000504
Sean Callanan3989fb92011-01-27 01:07:04 +0000505 if (m_error_stream)
506 m_error_stream->Printf("Internal error [IRForTarget]: Result variable's name (%s) exists, but not its definition\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000507
Sean Callananfc55f5d2010-09-21 00:44:12 +0000508 return false;
Sean Callanand1e5b432010-08-12 01:56:52 +0000509 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callanand1e5b432010-08-12 01:56:52 +0000511 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000512 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000513
Sean Callanand1e5b432010-08-12 01:56:52 +0000514 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000515
Sean Callanand1e5b432010-08-12 01:56:52 +0000516 if (!result_global)
517 {
518 if (log)
519 log->PutCString("Result variable isn't a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000520
Sean Callanan3989fb92011-01-27 01:07:04 +0000521 if (m_error_stream)
522 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) is defined, but is not a global variable\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000523
Sean Callanand1e5b432010-08-12 01:56:52 +0000524 return false;
525 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000526
Sean Callanan79763a42011-05-23 21:40:23 +0000527 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan63697e52011-05-07 01:06:41 +0000528 if (!result_decl)
Sean Callanand1e5b432010-08-12 01:56:52 +0000529 {
530 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000531 log->PutCString("Result variable doesn't have a corresponding Decl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000532
Sean Callanan3989fb92011-01-27 01:07:04 +0000533 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000534 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) does not have a corresponding Clang entity\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000535
Sean Callanand1e5b432010-08-12 01:56:52 +0000536 return false;
537 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000538
Sean Callanan63697e52011-05-07 01:06:41 +0000539 if (log)
Sean Callanand1e5b432010-08-12 01:56:52 +0000540 {
Sean Callanan63697e52011-05-07 01:06:41 +0000541 std::string decl_desc_str;
542 raw_string_ostream decl_desc_stream(decl_desc_str);
543 result_decl->print(decl_desc_stream);
544 decl_desc_stream.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000545
Sean Callanan63697e52011-05-07 01:06:41 +0000546 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanand1e5b432010-08-12 01:56:52 +0000547 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000548
Sean Callanan63697e52011-05-07 01:06:41 +0000549 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
550 if (!result_var)
Sean Callanand1e5b432010-08-12 01:56:52 +0000551 {
552 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000553 log->PutCString("Result variable Decl isn't a VarDecl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000554
Sean Callanan3989fb92011-01-27 01:07:04 +0000555 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000556 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s)'s corresponding Clang entity isn't a variable\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000557
Sean Callanand1e5b432010-08-12 01:56:52 +0000558 return false;
559 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000560
Sean Callanand1e5b432010-08-12 01:56:52 +0000561 // Get the next available result name from m_decl_map and create the persistent
562 // variable for it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000563
Sean Callanan63697e52011-05-07 01:06:41 +0000564 // If the result is an Lvalue, it is emitted as a pointer; see
565 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan92adcac2011-01-13 08:53:35 +0000566 if (m_result_is_pointer)
567 {
Sean Callanan63697e52011-05-07 01:06:41 +0000568 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanan78e37602011-01-27 04:42:51 +0000569 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000570
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000571 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
572 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000573
Sean Callanan5780f9d2011-12-08 19:04:34 +0000574 if (pointer_pointertype)
575 {
576 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000577
Sean Callanan5780f9d2011-12-08 19:04:34 +0000578 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000579 lldb_private::ClangASTContext::GetASTContext(&result_decl->getASTContext()));
Sean Callanan5780f9d2011-12-08 19:04:34 +0000580 }
581 else if (pointer_objcobjpointertype)
582 {
583 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000584
Sean Callanan5780f9d2011-12-08 19:04:34 +0000585 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000586 lldb_private::ClangASTContext::GetASTContext(&result_decl->getASTContext()));
Sean Callanan5780f9d2011-12-08 19:04:34 +0000587 }
588 else
Sean Callanan92adcac2011-01-13 08:53:35 +0000589 {
590 if (log)
591 log->PutCString("Expected result to have pointer type, but it did not");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Sean Callanan3989fb92011-01-27 01:07:04 +0000593 if (m_error_stream)
594 m_error_stream->Printf("Internal error [IRForTarget]: Lvalue result (%s) is not a pointer variable\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000595
Sean Callanan92adcac2011-01-13 08:53:35 +0000596 return false;
597 }
Sean Callanan92adcac2011-01-13 08:53:35 +0000598 }
599 else
600 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000601 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000602 lldb_private::ClangASTContext::GetASTContext(&result_decl->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000603 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000604
Greg Clayton526ae042015-02-12 00:34:25 +0000605
606 lldb::TargetSP target_sp (m_data_allocator.GetTarget());
Greg Claytonf9da9282015-02-27 00:12:22 +0000607 lldb_private::ExecutionContext exe_ctx (target_sp, true);
Greg Clayton526ae042015-02-12 00:34:25 +0000608 if (m_result_type.GetBitSize(exe_ctx.GetBestExecutionContextScope()) == 0)
Sean Callanan00f43622011-11-18 03:28:09 +0000609 {
610 lldb_private::StreamString type_desc_stream;
611 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000612
Sean Callanan00f43622011-11-18 03:28:09 +0000613 if (log)
614 log->Printf("Result type has size 0");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000615
Sean Callanan00f43622011-11-18 03:28:09 +0000616 if (m_error_stream)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000617 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
Sean Callanan00f43622011-11-18 03:28:09 +0000618 type_desc_stream.GetData());
Sean Callanan960534c2011-12-21 23:44:05 +0000619 return false;
Sean Callanan92adcac2011-01-13 08:53:35 +0000620 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000621
Sean Callanan63697e52011-05-07 01:06:41 +0000622 if (log)
623 {
624 lldb_private::StreamString type_desc_stream;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000625 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000626
Sean Callanan00f43622011-11-18 03:28:09 +0000627 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
Sean Callanan63697e52011-05-07 01:06:41 +0000628 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000629
Sean Callanan1582ee62013-04-18 22:06:33 +0000630 m_result_name = lldb_private::ConstString("$RESULT_NAME");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000631
Sean Callanand1e5b432010-08-12 01:56:52 +0000632 if (log)
Greg Claytonfaac1112013-03-14 18:31:44 +0000633 log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000634 m_result_name.GetCString(),
Enrico Granata1cd5e922015-01-28 00:07:51 +0000635 m_result_type.GetByteSize(nullptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000636
Sean Callanand1e5b432010-08-12 01:56:52 +0000637 // Construct a new result global and set up its metadata
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000638
639 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanand1e5b432010-08-12 01:56:52 +0000640 result_global->getType()->getElementType(),
641 false, /* not constant */
642 GlobalValue::ExternalLinkage,
643 NULL, /* no initializer */
Sean Callanan92adcac2011-01-13 08:53:35 +0000644 m_result_name.GetCString ());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000645
Sean Callanand1e5b432010-08-12 01:56:52 +0000646 // It's too late in compilation to create a new VarDecl for this, but we don't
647 // need to. We point the metadata at the old VarDecl. This creates an odd
648 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton7b462cc2010-10-15 22:48:33 +0000649 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanand1e5b432010-08-12 01:56:52 +0000650 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
651 // fixed up.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000652
Sean Callanan79763a42011-05-23 21:40:23 +0000653 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan63697e52011-05-07 01:06:41 +0000654 reinterpret_cast<uint64_t>(result_decl),
Sean Callanand1e5b432010-08-12 01:56:52 +0000655 false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000656
Zachary Turner543afa12014-12-09 22:29:47 +0000657 llvm::Metadata *values[2];
658 values[0] = ConstantAsMetadata::get(new_result_global);
659 values[1] = ConstantAsMetadata::get(new_constant_int);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000660
Zachary Turner543afa12014-12-09 22:29:47 +0000661 ArrayRef<Metadata *> value_ref(values, 2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000662
Sean Callanan79763a42011-05-23 21:40:23 +0000663 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
664 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanand1e5b432010-08-12 01:56:52 +0000665 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000666
Sean Callanand1e5b432010-08-12 01:56:52 +0000667 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000668 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan1e87fff2010-09-07 22:43:19 +0000669 PrintValue(result_global).c_str(),
Sean Callanand1e5b432010-08-12 01:56:52 +0000670 PrintValue(new_result_global).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000671
Sean Callanan339f6152014-03-11 19:19:16 +0000672 if (result_global->use_empty())
Sean Callanan1e87fff2010-09-07 22:43:19 +0000673 {
674 // We need to synthesize a store for this variable, because otherwise
675 // there's nothing to put into its equivalent persistent variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000676
Greg Clayton7b462cc2010-10-15 22:48:33 +0000677 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000678 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000679
Sean Callanan1e87fff2010-09-07 22:43:19 +0000680 if (!first_entry_instruction)
681 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000682
Sean Callanan1e87fff2010-09-07 22:43:19 +0000683 if (!result_global->hasInitializer())
684 {
685 if (log)
686 log->Printf("Couldn't find initializer for unused variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000687
Sean Callanan3989fb92011-01-27 01:07:04 +0000688 if (m_error_stream)
689 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) has no writes and no initializer\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000690
Sean Callanan1e87fff2010-09-07 22:43:19 +0000691 return false;
692 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000693
Sean Callanan1e87fff2010-09-07 22:43:19 +0000694 Constant *initializer = result_global->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000695
Greg Clayton9c139312011-02-05 02:28:58 +0000696 StoreInst *synthesized_store = new StoreInst(initializer,
697 new_result_global,
698 first_entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000699
Sean Callanan1e87fff2010-09-07 22:43:19 +0000700 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000701 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000702 }
703 else
704 {
705 result_global->replaceAllUsesWith(new_result_global);
706 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000707
Sean Callanan1582ee62013-04-18 22:06:33 +0000708 if (!m_decl_map->AddPersistentVariable(result_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000709 m_result_name,
Sean Callanan1582ee62013-04-18 22:06:33 +0000710 m_result_type,
711 true,
712 m_result_is_pointer))
713 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000714
Sean Callanand1e5b432010-08-12 01:56:52 +0000715 result_global->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000716
Sean Callanand1e5b432010-08-12 01:56:52 +0000717 return true;
718}
719
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000720bool
Sean Callanan79763a42011-05-23 21:40:23 +0000721IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000722 llvm::GlobalVariable *cstr)
Sean Callananafe16a72010-11-17 23:00:36 +0000723{
Greg Clayton5160ce52013-03-27 23:08:40 +0000724 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000725
Sean Callanancc427fa2011-07-30 02:42:06 +0000726 Type *ns_str_ty = ns_str->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000727
Sean Callanancc427fa2011-07-30 02:42:06 +0000728 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +0000729 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
730 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000731
Sean Callananafe16a72010-11-17 23:00:36 +0000732 if (!m_CFStringCreateWithBytes)
733 {
734 lldb::addr_t CFStringCreateWithBytes_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000735
Sean Callananafe16a72010-11-17 23:00:36 +0000736 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000737
Sean Callananafe16a72010-11-17 23:00:36 +0000738 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
739 {
740 if (log)
741 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000742
Sean Callanan3989fb92011-01-27 01:07:04 +0000743 if (m_error_stream)
744 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000745
Sean Callananafe16a72010-11-17 23:00:36 +0000746 return false;
747 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000748
Sean Callananafe16a72010-11-17 23:00:36 +0000749 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000750 log->Printf("Found CFStringCreateWithBytes at 0x%" PRIx64, CFStringCreateWithBytes_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000751
Sean Callananafe16a72010-11-17 23:00:36 +0000752 // Build the function type:
753 //
754 // CFStringRef CFStringCreateWithBytes (
755 // CFAllocatorRef alloc,
756 // const UInt8 *bytes,
757 // CFIndex numBytes,
758 // CFStringEncoding encoding,
759 // Boolean isExternalRepresentation
760 // );
761 //
762 // We make the following substitutions:
763 //
764 // CFStringRef -> i8*
765 // CFAllocatorRef -> i8*
766 // UInt8 * -> i8*
767 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
768 // CFStringEncoding -> i32
769 // Boolean -> i8
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000770
Sean Callanancc427fa2011-07-30 02:42:06 +0000771 Type *arg_type_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000772
Sean Callanancc427fa2011-07-30 02:42:06 +0000773 arg_type_array[0] = i8_ptr_ty;
774 arg_type_array[1] = i8_ptr_ty;
Sean Callanan439dcae2013-12-20 19:55:02 +0000775 arg_type_array[2] = m_intptr_ty;
Sean Callanancc427fa2011-07-30 02:42:06 +0000776 arg_type_array[3] = i32_ty;
777 arg_type_array[4] = i8_ty;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000778
Sean Callanancc427fa2011-07-30 02:42:06 +0000779 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000780
Sean Callanan79763a42011-05-23 21:40:23 +0000781 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000782
Sean Callananafe16a72010-11-17 23:00:36 +0000783 // Build the constant containing the pointer to the function
784 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000785 Constant *CFSCWB_addr_int = ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000786 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
787 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000788
Sean Callanand2b465f2012-02-09 03:22:41 +0000789 ConstantDataSequential *string_array = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000790
Sean Callanan229ce2d2011-02-10 22:17:53 +0000791 if (cstr)
Sean Callanand2b465f2012-02-09 03:22:41 +0000792 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000793
Sean Callananafe16a72010-11-17 23:00:36 +0000794 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000795 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000796 Constant *numBytes_arg = ConstantInt::get(m_intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000797 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
798 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000799
Sean Callanancc427fa2011-07-30 02:42:06 +0000800 Value *argument_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000801
Sean Callanancc427fa2011-07-30 02:42:06 +0000802 argument_array[0] = alloc_arg;
803 argument_array[1] = bytes_arg;
804 argument_array[2] = numBytes_arg;
805 argument_array[3] = encoding_arg;
806 argument_array[4] = isExternal_arg;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000807
Sean Callanancc427fa2011-07-30 02:42:06 +0000808 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000809
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000810 FunctionValueCache CFSCWB_Caller ([this, &CFSCWB_arguments] (llvm::Function *function)->llvm::Value * {
811 return CallInst::Create(m_CFStringCreateWithBytes,
812 CFSCWB_arguments,
813 "CFStringCreateWithBytes",
814 llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function)));
815 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000816
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000817 if (!UnfoldConstant(ns_str, CFSCWB_Caller, m_entry_instruction_finder))
Sean Callananafe16a72010-11-17 23:00:36 +0000818 {
819 if (log)
820 log->PutCString("Couldn't replace the NSString with the result of the call");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000821
Sean Callanan3989fb92011-01-27 01:07:04 +0000822 if (m_error_stream)
823 m_error_stream->Printf("Error [IRForTarget]: Couldn't replace an Objective-C constant string with a dynamic string\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000824
Sean Callananafe16a72010-11-17 23:00:36 +0000825 return false;
826 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000827
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000828 ns_str->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000829
Sean Callananafe16a72010-11-17 23:00:36 +0000830 return true;
831}
832
833bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000834IRForTarget::RewriteObjCConstStrings()
Sean Callananafe16a72010-11-17 23:00:36 +0000835{
Greg Clayton5160ce52013-03-27 23:08:40 +0000836 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000837
Sean Callanan79763a42011-05-23 21:40:23 +0000838 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000839
Sean Callananafe16a72010-11-17 23:00:36 +0000840 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
841 vi != ve;
842 ++vi)
843 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000844 std::string value_name = vi->first().str();
845 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000846
Sean Callanancc427fa2011-07-30 02:42:06 +0000847 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callananafe16a72010-11-17 23:00:36 +0000848 {
849 Value *nsstring_value = vi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000850
Sean Callananafe16a72010-11-17 23:00:36 +0000851 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000852
Sean Callananafe16a72010-11-17 23:00:36 +0000853 if (!nsstring_global)
854 {
855 if (log)
856 log->PutCString("NSString variable is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000857
Sean Callanan3989fb92011-01-27 01:07:04 +0000858 if (m_error_stream)
859 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a global variable\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000860
Sean Callananafe16a72010-11-17 23:00:36 +0000861 return false;
862 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000863
Sean Callananafe16a72010-11-17 23:00:36 +0000864 if (!nsstring_global->hasInitializer())
865 {
866 if (log)
867 log->PutCString("NSString variable does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000868
Sean Callanan3989fb92011-01-27 01:07:04 +0000869 if (m_error_stream)
870 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have an initializer\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000871
Sean Callananafe16a72010-11-17 23:00:36 +0000872 return false;
873 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000874
Sean Callananafe16a72010-11-17 23:00:36 +0000875 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000876
Sean Callananafe16a72010-11-17 23:00:36 +0000877 if (!nsstring_struct)
878 {
879 if (log)
880 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000881
Sean Callanan3989fb92011-01-27 01:07:04 +0000882 if (m_error_stream)
883 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a structure constant\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000884
Sean Callananafe16a72010-11-17 23:00:36 +0000885 return false;
886 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000887
Sean Callananafe16a72010-11-17 23:00:36 +0000888 // We expect the following structure:
889 //
890 // struct {
891 // int *isa;
892 // int flags;
893 // char *str;
894 // long length;
895 // };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000896
Sean Callananafe16a72010-11-17 23:00:36 +0000897 if (nsstring_struct->getNumOperands() != 4)
898 {
899 if (log)
900 log->Printf("NSString variable's initializer structure has an unexpected number of members. Should be 4, is %d", nsstring_struct->getNumOperands());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000901
Sean Callanan3989fb92011-01-27 01:07:04 +0000902 if (m_error_stream)
903 m_error_stream->Printf("Internal error [IRForTarget]: The struct for an Objective-C constant string is not as expected\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000904
Sean Callananafe16a72010-11-17 23:00:36 +0000905 return false;
906 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000907
Sean Callananafe16a72010-11-17 23:00:36 +0000908 Constant *nsstring_member = nsstring_struct->getOperand(2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000909
Sean Callananafe16a72010-11-17 23:00:36 +0000910 if (!nsstring_member)
911 {
912 if (log)
913 log->PutCString("NSString initializer's str element was empty");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000914
Sean Callanan3989fb92011-01-27 01:07:04 +0000915 if (m_error_stream)
916 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have a string initializer\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000917
Sean Callananafe16a72010-11-17 23:00:36 +0000918 return false;
919 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000920
Sean Callananafe16a72010-11-17 23:00:36 +0000921 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000922
Sean Callananafe16a72010-11-17 23:00:36 +0000923 if (!nsstring_expr)
924 {
925 if (log)
926 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000927
Sean Callanan3989fb92011-01-27 01:07:04 +0000928 if (m_error_stream)
929 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not constant\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000930
Sean Callananafe16a72010-11-17 23:00:36 +0000931 return false;
932 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000933
Sean Callananafe16a72010-11-17 23:00:36 +0000934 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
935 {
936 if (log)
937 log->Printf("NSString initializer's str element is not a GetElementPtr expression, it's a %s", nsstring_expr->getOpcodeName());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000938
Sean Callanan3989fb92011-01-27 01:07:04 +0000939 if (m_error_stream)
940 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not an array\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000941
Sean Callananafe16a72010-11-17 23:00:36 +0000942 return false;
943 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000944
Sean Callananafe16a72010-11-17 23:00:36 +0000945 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000946
Sean Callananafe16a72010-11-17 23:00:36 +0000947 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000948
Sean Callananafe16a72010-11-17 23:00:36 +0000949 if (!cstr_global)
950 {
951 if (log)
952 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000953
Sean Callanan3989fb92011-01-27 01:07:04 +0000954 if (m_error_stream)
955 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a global\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000956
Sean Callananafe16a72010-11-17 23:00:36 +0000957 return false;
958 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000959
Sean Callananafe16a72010-11-17 23:00:36 +0000960 if (!cstr_global->hasInitializer())
961 {
962 if (log)
963 log->PutCString("NSString initializer's str element does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000964
Sean Callanan3989fb92011-01-27 01:07:04 +0000965 if (m_error_stream)
966 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to initialized data\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000967
Sean Callananafe16a72010-11-17 23:00:36 +0000968 return false;
969 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000970
Sean Callanan229ce2d2011-02-10 22:17:53 +0000971 /*
Sean Callananafe16a72010-11-17 23:00:36 +0000972 if (!cstr_array)
973 {
974 if (log)
975 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000976
Sean Callanan3989fb92011-01-27 01:07:04 +0000977 if (m_error_stream)
978 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to an array\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000979
Sean Callananafe16a72010-11-17 23:00:36 +0000980 return false;
981 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000982
Sean Callananafe16a72010-11-17 23:00:36 +0000983 if (!cstr_array->isCString())
984 {
985 if (log)
986 log->PutCString("NSString initializer's str element is not a C string array");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000987
Sean Callanan3989fb92011-01-27 01:07:04 +0000988 if (m_error_stream)
989 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a C string\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000990
Sean Callananafe16a72010-11-17 23:00:36 +0000991 return false;
992 }
Sean Callanan229ce2d2011-02-10 22:17:53 +0000993 */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000994
Sean Callanand2b465f2012-02-09 03:22:41 +0000995 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000996
Sean Callananafe16a72010-11-17 23:00:36 +0000997 if (log)
Sean Callanan229ce2d2011-02-10 22:17:53 +0000998 {
999 if (cstr_array)
Sean Callanand2b465f2012-02-09 03:22:41 +00001000 log->Printf("Found NSString constant %s, which contains \"%s\"", value_name_cstr, cstr_array->getAsString().str().c_str());
Sean Callanan229ce2d2011-02-10 22:17:53 +00001001 else
Sean Callanancc427fa2011-07-30 02:42:06 +00001002 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan229ce2d2011-02-10 22:17:53 +00001003 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001004
Sean Callanan229ce2d2011-02-10 22:17:53 +00001005 if (!cstr_array)
1006 cstr_global = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001007
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001008 if (!RewriteObjCConstString(nsstring_global, cstr_global))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001009 {
Sean Callananafe16a72010-11-17 23:00:36 +00001010 if (log)
1011 log->PutCString("Error rewriting the constant string");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001012
Sean Callanan3989fb92011-01-27 01:07:04 +00001013 // We don't print an error message here because RewriteObjCConstString has done so for us.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001014
Sean Callananafe16a72010-11-17 23:00:36 +00001015 return false;
1016 }
Sean Callananafe16a72010-11-17 23:00:36 +00001017 }
1018 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001019
Sean Callananafe16a72010-11-17 23:00:36 +00001020 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1021 vi != ve;
1022 ++vi)
1023 {
Sean Callanancc427fa2011-07-30 02:42:06 +00001024 std::string value_name = vi->first().str();
1025 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001026
Sean Callanancc427fa2011-07-30 02:42:06 +00001027 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callananafe16a72010-11-17 23:00:36 +00001028 {
1029 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001030
Sean Callananafe16a72010-11-17 23:00:36 +00001031 if (!gv)
1032 {
1033 if (log)
1034 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001035
Sean Callanan3989fb92011-01-27 01:07:04 +00001036 if (m_error_stream)
1037 m_error_stream->Printf("Internal error [IRForTarget]: Found a CFConstantStringClassReference, but it is not a global object\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001038
Sean Callananafe16a72010-11-17 23:00:36 +00001039 return false;
1040 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001041
Sean Callananafe16a72010-11-17 23:00:36 +00001042 gv->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001043
Sean Callananafe16a72010-11-17 23:00:36 +00001044 break;
1045 }
1046 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001047
Sean Callananafe16a72010-11-17 23:00:36 +00001048 return true;
1049}
1050
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001051static bool IsObjCSelectorRef (Value *value)
Sean Callanan5300d372010-07-31 01:32:05 +00001052{
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001053 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001054
Greg Claytonbd549162014-11-10 21:45:59 +00001055 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("OBJC_SELECTOR_REFERENCES_"))
Sean Callanan5300d372010-07-31 01:32:05 +00001056 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001057
Sean Callanan5300d372010-07-31 01:32:05 +00001058 return true;
1059}
1060
Sean Callanan3989fb92011-01-27 01:07:04 +00001061// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001062bool
Sean Callanan79763a42011-05-23 21:40:23 +00001063IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callanan5300d372010-07-31 01:32:05 +00001064{
Greg Clayton5160ce52013-03-27 23:08:40 +00001065 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001066
1067 LoadInst *load = dyn_cast<LoadInst>(selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001068
Sean Callanan5300d372010-07-31 01:32:05 +00001069 if (!load)
1070 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001071
Sean Callanan5300d372010-07-31 01:32:05 +00001072 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1073 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +00001074 // %tmp = load i8** @"OBJC_SELECTOR_REFERENCES_" ; <i8*>
Sean Callanan5300d372010-07-31 01:32:05 +00001075 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1076 //
1077 // where %obj is the object pointer and %tmp is the selector.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001078 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +00001079 // @"OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001080 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001081
Sean Callanan5300d372010-07-31 01:32:05 +00001082 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001083
Sean Callanan5300d372010-07-31 01:32:05 +00001084 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001085
Sean Callanan5300d372010-07-31 01:32:05 +00001086 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1087 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001088
Sean Callanan5300d372010-07-31 01:32:05 +00001089 Constant *osr_initializer = _objc_selector_references_->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001090
Sean Callanan5300d372010-07-31 01:32:05 +00001091 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001092
Sean Callanan5300d372010-07-31 01:32:05 +00001093 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1094 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001095
Sean Callanan5300d372010-07-31 01:32:05 +00001096 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1097
1098 if (!osr_initializer_base)
1099 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001100
Sean Callanan5300d372010-07-31 01:32:05 +00001101 // Find the string's initializer (a ConstantArray) and get the string from it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001102
Sean Callanan5300d372010-07-31 01:32:05 +00001103 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001104
Sean Callanan5300d372010-07-31 01:32:05 +00001105 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1106 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001107
Sean Callanan5300d372010-07-31 01:32:05 +00001108 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1109
Sean Callanand2b465f2012-02-09 03:22:41 +00001110 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001111
Sean Callanan5300d372010-07-31 01:32:05 +00001112 if (!omvn_initializer_array->isString())
1113 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001114
Sean Callanan5300d372010-07-31 01:32:05 +00001115 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001116
Sean Callanan5300d372010-07-31 01:32:05 +00001117 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001118 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001119
Sean Callanan5300d372010-07-31 01:32:05 +00001120 // Construct a call to sel_registerName
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001121
Sean Callanan5300d372010-07-31 01:32:05 +00001122 if (!m_sel_registerName)
1123 {
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001124 lldb::addr_t sel_registerName_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001125
Greg Clayton7b462cc2010-10-15 22:48:33 +00001126 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001127 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callanan5300d372010-07-31 01:32:05 +00001128 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001129
Sean Callananbe3a1b12010-10-26 00:31:56 +00001130 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001131 log->Printf("Found sel_registerName at 0x%" PRIx64, sel_registerName_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001132
Sean Callanan5300d372010-07-31 01:32:05 +00001133 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001134
Sean Callanan5300d372010-07-31 01:32:05 +00001135 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callanan79763a42011-05-23 21:40:23 +00001136 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callanan5300d372010-07-31 01:32:05 +00001137 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanancc427fa2011-07-30 02:42:06 +00001138 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001139
Sean Callanancc427fa2011-07-30 02:42:06 +00001140 Type *type_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001141
Sean Callanancc427fa2011-07-30 02:42:06 +00001142 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001143
Sean Callanancc427fa2011-07-30 02:42:06 +00001144 ArrayRef<Type *> srN_arg_types(type_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001145
Sean Callanan5300d372010-07-31 01:32:05 +00001146 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001147
Sean Callanan5300d372010-07-31 01:32:05 +00001148 // Build the constant containing the pointer to the function
Sean Callanan5300d372010-07-31 01:32:05 +00001149 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Sean Callanan439dcae2013-12-20 19:55:02 +00001150 Constant *srN_addr_int = ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
Sean Callanan5300d372010-07-31 01:32:05 +00001151 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1152 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001153
Sean Callanancc427fa2011-07-30 02:42:06 +00001154 Value *argument_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001155
Sean Callanan79763a42011-05-23 21:40:23 +00001156 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001157
Sean Callanancc427fa2011-07-30 02:42:06 +00001158 argument_array[0] = omvn_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001159
Sean Callanancc427fa2011-07-30 02:42:06 +00001160 ArrayRef<Value *> srN_arguments(argument_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001161
1162 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanancc427fa2011-07-30 02:42:06 +00001163 srN_arguments,
Sean Callananafe16a72010-11-17 23:00:36 +00001164 "sel_registerName",
Sean Callanan5300d372010-07-31 01:32:05 +00001165 selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001166
Sean Callanan5300d372010-07-31 01:32:05 +00001167 // Replace the load with the call in all users
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001168
Sean Callanan5300d372010-07-31 01:32:05 +00001169 selector_load->replaceAllUsesWith(srN_call);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001170
Sean Callanan5300d372010-07-31 01:32:05 +00001171 selector_load->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001172
Sean Callanan5300d372010-07-31 01:32:05 +00001173 return true;
1174}
1175
1176bool
Sean Callanan79763a42011-05-23 21:40:23 +00001177IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callanan5300d372010-07-31 01:32:05 +00001178{
Greg Clayton5160ce52013-03-27 23:08:40 +00001179 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001180
1181 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001182
Sean Callanan5300d372010-07-31 01:32:05 +00001183 typedef SmallVector <Instruction*, 2> InstrList;
1184 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001185
Sean Callanan5300d372010-07-31 01:32:05 +00001186 InstrList selector_loads;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001187
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001188 for (ii = basic_block.begin();
1189 ii != basic_block.end();
Sean Callanan5300d372010-07-31 01:32:05 +00001190 ++ii)
1191 {
1192 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001193
Sean Callanan5300d372010-07-31 01:32:05 +00001194 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001195 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callanan5300d372010-07-31 01:32:05 +00001196 selector_loads.push_back(&inst);
1197 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001198
Sean Callanan5300d372010-07-31 01:32:05 +00001199 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001200
Sean Callanan5300d372010-07-31 01:32:05 +00001201 for (iter = selector_loads.begin();
1202 iter != selector_loads.end();
1203 ++iter)
1204 {
Sean Callanan79763a42011-05-23 21:40:23 +00001205 if (!RewriteObjCSelector(*iter))
Sean Callanan5300d372010-07-31 01:32:05 +00001206 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001207 if (m_error_stream)
1208 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't change a static reference to an Objective-C selector to a dynamic reference\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001209
Enrico Granata20edcdb2011-07-19 18:03:25 +00001210 if (log)
Sean Callanan5300d372010-07-31 01:32:05 +00001211 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001212
Sean Callanan5300d372010-07-31 01:32:05 +00001213 return false;
1214 }
1215 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001216
Sean Callanan5300d372010-07-31 01:32:05 +00001217 return true;
1218}
1219
Sean Callanan3989fb92011-01-27 01:07:04 +00001220// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001221bool
Sean Callanan79763a42011-05-23 21:40:23 +00001222IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanan2235f322010-08-11 03:57:18 +00001223{
Greg Clayton5160ce52013-03-27 23:08:40 +00001224 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane1175b72011-01-13 21:23:32 +00001225
Sean Callanan2235f322010-08-11 03:57:18 +00001226 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001227
Duncan P. N. Exon Smith68caa7d2014-11-12 01:59:53 +00001228 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
Sean Callanan2235f322010-08-11 03:57:18 +00001229
1230 if (!alloc_md || !alloc_md->getNumOperands())
1231 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001232
Zachary Turner543afa12014-12-09 22:29:47 +00001233 ConstantInt *constant_int = mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001234
Sean Callanan2235f322010-08-11 03:57:18 +00001235 if (!constant_int)
1236 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001237
Sean Callanan2235f322010-08-11 03:57:18 +00001238 // We attempt to register this as a new persistent variable with the DeclMap.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001239
Sean Callanan2235f322010-08-11 03:57:18 +00001240 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001241
Sean Callanand1e5b432010-08-12 01:56:52 +00001242 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001243
Sean Callanand1e5b432010-08-12 01:56:52 +00001244 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +00001245 lldb_private::ClangASTContext::GetASTContext(&decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001246
Greg Clayton7b462cc2010-10-15 22:48:33 +00001247 StringRef decl_name (decl->getName());
1248 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan92adcac2011-01-13 08:53:35 +00001249 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanan2235f322010-08-11 03:57:18 +00001250 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001251
Sean Callanan79763a42011-05-23 21:40:23 +00001252 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanane1175b72011-01-13 21:23:32 +00001253 alloc->getType(),
Sean Callanan2235f322010-08-11 03:57:18 +00001254 false, /* not constant */
1255 GlobalValue::ExternalLinkage,
1256 NULL, /* no initializer */
1257 alloc->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001258
Sean Callanan2235f322010-08-11 03:57:18 +00001259 // What we're going to do here is make believe this was a regular old external
1260 // variable. That means we need to make the metadata valid.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001261
Sean Callanan585c0ec82012-07-04 01:26:26 +00001262 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001263
Zachary Turner543afa12014-12-09 22:29:47 +00001264 llvm::Metadata *values[2];
1265 values[0] = ConstantAsMetadata::get(persistent_global);
1266 values[1] = ConstantAsMetadata::get(constant_int);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001267
Zachary Turner543afa12014-12-09 22:29:47 +00001268 ArrayRef<llvm::Metadata *> value_ref(values, 2);
Sean Callanan2235f322010-08-11 03:57:18 +00001269
Sean Callanan79763a42011-05-23 21:40:23 +00001270 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanan2235f322010-08-11 03:57:18 +00001271 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001272
Sean Callanane1175b72011-01-13 21:23:32 +00001273 // Now, since the variable is a pointer variable, we will drop in a load of that
1274 // pointer variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001275
Sean Callanane1175b72011-01-13 21:23:32 +00001276 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001277
Sean Callanane1175b72011-01-13 21:23:32 +00001278 if (log)
1279 log->Printf("Replacing \"%s\" with \"%s\"",
1280 PrintValue(alloc).c_str(),
1281 PrintValue(persistent_load).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001282
Sean Callanane1175b72011-01-13 21:23:32 +00001283 alloc->replaceAllUsesWith(persistent_load);
Sean Callanan2235f322010-08-11 03:57:18 +00001284 alloc->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001285
Sean Callanan2235f322010-08-11 03:57:18 +00001286 return true;
1287}
1288
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001289bool
Sean Callanan79763a42011-05-23 21:40:23 +00001290IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanan2235f322010-08-11 03:57:18 +00001291{
Sean Callanan9e6ed532010-09-13 21:34:21 +00001292 if (!m_resolve_vars)
1293 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001294
Greg Clayton5160ce52013-03-27 23:08:40 +00001295 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001296
Sean Callanan2235f322010-08-11 03:57:18 +00001297 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001298
Sean Callanan2235f322010-08-11 03:57:18 +00001299 typedef SmallVector <Instruction*, 2> InstrList;
1300 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001301
Sean Callanan2235f322010-08-11 03:57:18 +00001302 InstrList pvar_allocs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001303
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001304 for (ii = basic_block.begin();
1305 ii != basic_block.end();
Sean Callanan2235f322010-08-11 03:57:18 +00001306 ++ii)
1307 {
1308 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001309
Sean Callanan2235f322010-08-11 03:57:18 +00001310 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callananf694a552011-01-21 22:30:25 +00001311 {
1312 llvm::StringRef alloc_name = alloc->getName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001313
Sean Callananf694a552011-01-21 22:30:25 +00001314 if (alloc_name.startswith("$") &&
1315 !alloc_name.startswith("$__lldb"))
1316 {
1317 if (alloc_name.find_first_of("0123456789") == 1)
1318 {
1319 if (log)
1320 log->Printf("Rejecting a numeric persistent variable.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001321
Sean Callanan3989fb92011-01-27 01:07:04 +00001322 if (m_error_stream)
1323 m_error_stream->Printf("Error [IRForTarget]: Names starting with $0, $1, ... are reserved for use as result names\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001324
Sean Callananf694a552011-01-21 22:30:25 +00001325 return false;
1326 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001327
Sean Callanan2235f322010-08-11 03:57:18 +00001328 pvar_allocs.push_back(alloc);
Sean Callananf694a552011-01-21 22:30:25 +00001329 }
1330 }
Sean Callanan2235f322010-08-11 03:57:18 +00001331 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001332
Sean Callanan2235f322010-08-11 03:57:18 +00001333 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001334
Sean Callanan2235f322010-08-11 03:57:18 +00001335 for (iter = pvar_allocs.begin();
1336 iter != pvar_allocs.end();
1337 ++iter)
1338 {
Sean Callanan79763a42011-05-23 21:40:23 +00001339 if (!RewritePersistentAlloc(*iter))
Sean Callanan2235f322010-08-11 03:57:18 +00001340 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001341 if (m_error_stream)
1342 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001343
Enrico Granata20edcdb2011-07-19 18:03:25 +00001344 if (log)
Sean Callanan2235f322010-08-11 03:57:18 +00001345 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001346
Sean Callanan2235f322010-08-11 03:57:18 +00001347 return false;
1348 }
1349 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001350
Sean Callanan2235f322010-08-11 03:57:18 +00001351 return true;
1352}
1353
Sean Callananc70ed462011-10-25 18:36:40 +00001354bool
1355IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1356{
1357 if (!initializer)
1358 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001359
Greg Clayton5160ce52013-03-27 23:08:40 +00001360 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc70ed462011-10-25 18:36:40 +00001361
1362 if (log && log->GetVerbose())
David Blaikie129b8392015-04-08 20:23:52 +00001363 log->Printf(" MaterializeInitializer(%p, %s)", (void *)data, PrintValue(initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001364
Sean Callananc70ed462011-10-25 18:36:40 +00001365 Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001366
Sean Callananc70ed462011-10-25 18:36:40 +00001367 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1368 {
1369 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1370 return true;
1371 }
Sean Callanand2b465f2012-02-09 03:22:41 +00001372 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
Sean Callananc70ed462011-10-25 18:36:40 +00001373 {
1374 if (array_initializer->isString())
1375 {
1376 std::string array_initializer_string = array_initializer->getAsString();
1377 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1378 }
1379 else
1380 {
1381 ArrayType *array_initializer_type = array_initializer->getType();
1382 Type *array_element_type = array_initializer_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001383
Sean Callananc70ed462011-10-25 18:36:40 +00001384 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001385
Andy Gibbsa297a972013-06-19 19:04:53 +00001386 for (unsigned i = 0; i < array_initializer->getNumOperands(); ++i)
Sean Callananc70ed462011-10-25 18:36:40 +00001387 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001388 Value *operand_value = array_initializer->getOperand(i);
1389 Constant *operand_constant = dyn_cast<Constant>(operand_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001390
Sean Callanand2b465f2012-02-09 03:22:41 +00001391 if (!operand_constant)
1392 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001393
Sean Callanand2b465f2012-02-09 03:22:41 +00001394 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
Sean Callananc70ed462011-10-25 18:36:40 +00001395 return false;
1396 }
1397 }
1398 return true;
1399 }
1400 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1401 {
1402 StructType *struct_initializer_type = struct_initializer->getType();
1403 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1404
Andy Gibbsa297a972013-06-19 19:04:53 +00001405 for (unsigned i = 0;
Sean Callananc70ed462011-10-25 18:36:40 +00001406 i < struct_initializer->getNumOperands();
1407 ++i)
1408 {
1409 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1410 return false;
1411 }
1412 return true;
1413 }
Sean Callanan76ee3e72013-04-24 19:50:12 +00001414 else if (isa<ConstantAggregateZero>(initializer))
1415 {
1416 memset(data, 0, m_target_data->getTypeStoreSize(initializer_type));
1417 return true;
1418 }
Sean Callananc70ed462011-10-25 18:36:40 +00001419 return false;
1420}
1421
1422bool
1423IRForTarget::MaterializeInternalVariable (GlobalVariable *global_variable)
1424{
1425 if (GlobalVariable::isExternalLinkage(global_variable->getLinkage()))
1426 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001427
Sean Callananfe5d1392011-11-15 19:13:54 +00001428 if (global_variable == m_reloc_placeholder)
1429 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001430
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001431 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001432
Sean Callananc70ed462011-10-25 18:36:40 +00001433 llvm::Type *variable_type = global_variable->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001434
Sean Callananc70ed462011-10-25 18:36:40 +00001435 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001436
Sean Callananc70ed462011-10-25 18:36:40 +00001437 llvm::Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001438
Sean Callananc70ed462011-10-25 18:36:40 +00001439 size_t size = m_target_data->getTypeAllocSize(initializer_type);
Sean Callanane3333d62012-06-08 22:20:41 +00001440 size_t align = m_target_data->getPrefTypeAlignment(initializer_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001441
Sean Callanane3333d62012-06-08 22:20:41 +00001442 const size_t mask = (align - 1);
1443 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001444 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sean Callanane3333d62012-06-08 22:20:41 +00001445 offset = aligned_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001446
Sean Callananc70ed462011-10-25 18:36:40 +00001447 lldb_private::DataBufferHeap data(size, '\0');
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001448
Sean Callananc70ed462011-10-25 18:36:40 +00001449 if (initializer)
1450 if (!MaterializeInitializer(data.GetBytes(), initializer))
1451 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001452
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001453 m_data_allocator.GetStream().Write(data.GetBytes(), data.GetByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001454
Sean Callananc70ed462011-10-25 18:36:40 +00001455 Constant *new_pointer = BuildRelocation(variable_type, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001456
Sean Callananc70ed462011-10-25 18:36:40 +00001457 global_variable->replaceAllUsesWith(new_pointer);
1458
1459 global_variable->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001460
Sean Callananc70ed462011-10-25 18:36:40 +00001461 return true;
1462}
1463
Sean Callanan3989fb92011-01-27 01:07:04 +00001464// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001465bool
Sean Callanan79763a42011-05-23 21:40:23 +00001466IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001467{
Greg Clayton5160ce52013-03-27 23:08:40 +00001468 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001469
Sean Callanand7a1ca22010-12-02 19:47:57 +00001470 if (log)
Sean Callanan88339f02010-12-06 22:16:55 +00001471 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001472
Greg Clayton7b462cc2010-10-15 22:48:33 +00001473 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callanan4cf04d22010-08-03 00:23:29 +00001474 {
Sean Callanan5666b672010-08-04 01:02:13 +00001475 switch (constant_expr->getOpcode())
Sean Callanan4cf04d22010-08-03 00:23:29 +00001476 {
Sean Callanan5666b672010-08-04 01:02:13 +00001477 default:
1478 break;
1479 case Instruction::GetElementPtr:
1480 case Instruction::BitCast:
Sean Callanan4cf04d22010-08-03 00:23:29 +00001481 Value *s = constant_expr->getOperand(0);
Sean Callanan79763a42011-05-23 21:40:23 +00001482 if (!MaybeHandleVariable(s))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001483 return false;
Sean Callanan4cf04d22010-08-03 00:23:29 +00001484 }
1485 }
Sean Callanand6e04ae2010-12-03 19:51:05 +00001486 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001487 {
Sean Callananc70ed462011-10-25 18:36:40 +00001488 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1489 return MaterializeInternalVariable(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001490
Sean Callanan79763a42011-05-23 21:40:23 +00001491 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001492
Sean Callanan5300d372010-07-31 01:32:05 +00001493 if (!named_decl)
1494 {
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001495 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001496 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001497
Sean Callanan14f0b0e2010-12-06 00:56:39 +00001498 if (!global_variable->hasExternalLinkage())
1499 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001500
Sean Callanan5300d372010-07-31 01:32:05 +00001501 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001502 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001503
Sean Callanan5300d372010-07-31 01:32:05 +00001504 return false;
1505 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001506
Greg Clayton7b462cc2010-10-15 22:48:33 +00001507 std::string name (named_decl->getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001508
Greg Clayton57ee3062013-07-11 22:46:58 +00001509 clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1510 if (value_decl == NULL)
Sean Callananea22d422010-07-16 00:09:46 +00001511 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001512
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001513 lldb_private::CompilerType compiler_type(&value_decl->getASTContext(), value_decl->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001514
Sean Callanan77eaf442011-07-08 00:39:14 +00001515 const Type *value_type = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001516
Sean Callanane1175b72011-01-13 21:23:32 +00001517 if (name[0] == '$')
Sean Callanan92adcac2011-01-13 08:53:35 +00001518 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001519 // The $__lldb_expr_result name indicates the return value has allocated as
Sean Callanan92adcac2011-01-13 08:53:35 +00001520 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1521 // accesses to this static variable need to be redirected to the result of dereferencing
1522 // a pointer that is passed in as one of the arguments.
1523 //
1524 // Consequently, when reporting the size of the type, we report a pointer type pointing
1525 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanane1175b72011-01-13 21:23:32 +00001526 //
1527 // We also do this for any user-declared persistent variables.
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001528 compiler_type = compiler_type.GetPointerType();
Sean Callanan92adcac2011-01-13 08:53:35 +00001529 value_type = PointerType::get(global_variable->getType(), 0);
1530 }
1531 else
1532 {
Sean Callanan92adcac2011-01-13 08:53:35 +00001533 value_type = global_variable->getType();
1534 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001535
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001536 const uint64_t value_size = compiler_type.GetByteSize(nullptr);
1537 lldb::offset_t value_alignment = (compiler_type.GetTypeBitAlign() + 7ull) / 8ull;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001538
Sean Callanan038df5032010-09-30 21:18:25 +00001539 if (log)
Greg Clayton57ee3062013-07-11 22:46:58 +00001540 {
Zachary Turnera746e8e2014-07-02 17:24:07 +00001541 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %" PRIu64 ", align %" PRIu64 "]",
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001542 name.c_str(),
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001543 lldb_private::ClangASTContext::GetQualType(compiler_type).getAsString().c_str(),
Greg Clayton57ee3062013-07-11 22:46:58 +00001544 PrintType(value_type).c_str(),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001545 value_size,
Sean Callanan038df5032010-09-30 21:18:25 +00001546 value_alignment);
Greg Clayton57ee3062013-07-11 22:46:58 +00001547 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001548
1549
Sean Callanan64dfc9a2010-08-23 23:09:38 +00001550 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001551 lldb_private::ConstString (name.c_str()),
1552 llvm_value_ptr,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001553 value_size,
Sean Callanan4edba2d2010-07-27 02:07:53 +00001554 value_alignment))
Sean Callananaf8e96c2011-08-01 17:41:38 +00001555 {
1556 if (!global_variable->hasExternalLinkage())
1557 return true;
Sean Callanan0ff3bf92013-04-11 17:57:16 +00001558 else if (HandleSymbol (global_variable))
1559 return true;
Sean Callananaf8e96c2011-08-01 17:41:38 +00001560 else
1561 return false;
1562 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001563 }
Sean Callanan4a5fcbb2010-12-03 03:02:31 +00001564 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001565 {
1566 if (log)
1567 log->Printf("Function pointers aren't handled right now");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001568
Sean Callanand7a1ca22010-12-02 19:47:57 +00001569 return false;
1570 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001571
Sean Callanan549c9f72010-07-13 21:41:46 +00001572 return true;
1573}
1574
Sean Callanan3989fb92011-01-27 01:07:04 +00001575// This function does not report errors; its callers are responsible.
Sean Callanan549c9f72010-07-13 21:41:46 +00001576bool
Sean Callanan79763a42011-05-23 21:40:23 +00001577IRForTarget::HandleSymbol (Value *symbol)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001578{
Greg Clayton5160ce52013-03-27 23:08:40 +00001579 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001580
Sean Callananc3a16002011-01-17 23:42:46 +00001581 lldb_private::ConstString name(symbol->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001582
Sean Callanan947ccc72011-12-01 02:04:16 +00001583 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001584
Greg Clayton084db102011-06-23 04:25:29 +00001585 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc3a16002011-01-17 23:42:46 +00001586 {
1587 if (log)
1588 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001589
Sean Callananc3a16002011-01-17 23:42:46 +00001590 return false;
1591 }
1592
1593 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001594 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001595
Sean Callanancc427fa2011-07-30 02:42:06 +00001596 Type *symbol_type = symbol->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001597
Sean Callanan439dcae2013-12-20 19:55:02 +00001598 Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001599
Sean Callananc3a16002011-01-17 23:42:46 +00001600 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001601
Sean Callananc3a16002011-01-17 23:42:46 +00001602 if (log)
1603 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001604
Sean Callananc3a16002011-01-17 23:42:46 +00001605 symbol->replaceAllUsesWith(symbol_addr_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001606
Sean Callananc3a16002011-01-17 23:42:46 +00001607 return true;
1608}
1609
1610bool
Sean Callanan79763a42011-05-23 21:40:23 +00001611IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanan85a0a832010-10-05 22:26:43 +00001612{
Greg Clayton5160ce52013-03-27 23:08:40 +00001613 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001614
Sean Callanand7a1ca22010-12-02 19:47:57 +00001615 if (log)
1616 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001617
Sean Callananafe16a72010-11-17 23:00:36 +00001618 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanan85a0a832010-10-05 22:26:43 +00001619 op_index < num_ops;
1620 ++op_index)
Sean Callanan79763a42011-05-23 21:40:23 +00001621 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan3989fb92011-01-27 01:07:04 +00001622 {
1623 if (m_error_stream)
1624 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite one of the arguments of a function call.\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001625
Sean Callanan85a0a832010-10-05 22:26:43 +00001626 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001627 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001628
Sean Callanan85a0a832010-10-05 22:26:43 +00001629 return true;
1630}
1631
1632bool
Sean Callananfc89c142011-11-01 23:38:03 +00001633IRForTarget::HandleObjCClass(Value *classlist_reference)
1634{
Greg Clayton5160ce52013-03-27 23:08:40 +00001635 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc89c142011-11-01 23:38:03 +00001636
1637 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001638
Sean Callananfc89c142011-11-01 23:38:03 +00001639 if (!global_variable)
1640 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001641
Sean Callananfc89c142011-11-01 23:38:03 +00001642 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001643
Sean Callananfc89c142011-11-01 23:38:03 +00001644 if (!initializer)
1645 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001646
Sean Callananfc89c142011-11-01 23:38:03 +00001647 if (!initializer->hasName())
1648 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001649
Sean Callananfc89c142011-11-01 23:38:03 +00001650 StringRef name(initializer->getName());
1651 lldb_private::ConstString name_cstr(name.str().c_str());
Greg Clayton1075aca2011-12-03 20:02:42 +00001652 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001653
Sean Callananfc89c142011-11-01 23:38:03 +00001654 if (log)
1655 log->Printf("Found reference to Objective-C class %s (0x%llx)", name_cstr.AsCString(), (unsigned long long)class_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001656
Sean Callananfc89c142011-11-01 23:38:03 +00001657 if (class_ptr == LLDB_INVALID_ADDRESS)
1658 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001659
Ed Mastea8553092014-03-10 17:24:16 +00001660 if (global_variable->use_empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001661 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001662
Sean Callanan719a4d52013-03-23 01:01:16 +00001663 SmallVector<LoadInst *, 2> load_instructions;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001664
Ed Mastea8553092014-03-10 17:24:16 +00001665 for (llvm::User *u : global_variable->users())
Sean Callananfc89c142011-11-01 23:38:03 +00001666 {
Ed Mastea8553092014-03-10 17:24:16 +00001667 if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
Sean Callanan719a4d52013-03-23 01:01:16 +00001668 load_instructions.push_back(load_instruction);
Sean Callananfc89c142011-11-01 23:38:03 +00001669 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001670
Sean Callanan719a4d52013-03-23 01:01:16 +00001671 if (load_instructions.empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001672 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001673
Sean Callanan439dcae2013-12-20 19:55:02 +00001674 Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001675
Sean Callanan719a4d52013-03-23 01:01:16 +00001676 for (LoadInst *load_instruction : load_instructions)
1677 {
1678 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001679
Sean Callanan719a4d52013-03-23 01:01:16 +00001680 load_instruction->replaceAllUsesWith(class_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001681
Sean Callanan719a4d52013-03-23 01:01:16 +00001682 load_instruction->eraseFromParent();
1683 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001684
Sean Callananfc89c142011-11-01 23:38:03 +00001685 return true;
1686}
1687
1688bool
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001689IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1690{
1691 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001692
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001693 std::vector<CallInst *> calls_to_remove;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001694
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001695 for (ii = basic_block.begin();
1696 ii != basic_block.end();
1697 ++ii)
1698 {
1699 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001700
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001701 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001702
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001703 // MaybeHandleCallArguments handles error reporting; we are silent here
1704 if (!call)
1705 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001706
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001707 bool remove = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001708
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001709 llvm::Function *func = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001710
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001711 if (func && func->getName() == "__cxa_atexit")
1712 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001713
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001714 llvm::Value *val = call->getCalledValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001715
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001716 if (val && val->getName() == "__cxa_atexit")
1717 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001718
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001719 if (remove)
1720 calls_to_remove.push_back(call);
1721 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001722
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001723 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1724 ci != ce;
1725 ++ci)
1726 {
1727 (*ci)->eraseFromParent();
1728 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001729
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001730 return true;
1731}
1732
1733bool
Sean Callanan79763a42011-05-23 21:40:23 +00001734IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001735{
Sean Callanan2ab712f22010-07-03 01:35:46 +00001736 /////////////////////////////////////////////////////////////////////////
1737 // Prepare the current basic block for execution in the remote process
1738 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001739
Sean Callanan7ea35012010-07-27 21:39:39 +00001740 BasicBlock::iterator ii;
Sean Callanan549c9f72010-07-13 21:41:46 +00001741
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001742 for (ii = basic_block.begin();
1743 ii != basic_block.end();
Sean Callanan549c9f72010-07-13 21:41:46 +00001744 ++ii)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001745 {
Sean Callanan549c9f72010-07-13 21:41:46 +00001746 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001747
Sean Callanana4e55172010-11-08 00:31:32 +00001748 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001749
Sean Callanan3989fb92011-01-27 01:07:04 +00001750 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callanan79763a42011-05-23 21:40:23 +00001751 if (call && !MaybeHandleCallArguments(call))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001752 return false;
Sean Callanan2ab712f22010-07-03 01:35:46 +00001753 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001754
Sean Callanan2ab712f22010-07-03 01:35:46 +00001755 return true;
1756}
1757
Sean Callanana4e55172010-11-08 00:31:32 +00001758bool
Sean Callanan79763a42011-05-23 21:40:23 +00001759IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanana4e55172010-11-08 00:31:32 +00001760{
Greg Clayton5160ce52013-03-27 23:08:40 +00001761 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001762
Sean Callanan339f6152014-03-11 19:19:16 +00001763 for (GlobalVariable &global_var : m_module->globals())
Sean Callanana4e55172010-11-08 00:31:32 +00001764 {
Sean Callanan339f6152014-03-11 19:19:16 +00001765 std::string global_name = global_var.getName().str();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001766
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001767 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001768 log->Printf("Examining %s, DeclForGlobalValue returns %p",
Sean Callanan694e2442011-12-22 21:24:49 +00001769 global_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001770 static_cast<void*>(DeclForGlobal(&global_var)));
1771
Sean Callananfc89c142011-11-01 23:38:03 +00001772 if (global_name.find("OBJC_IVAR") == 0)
Sean Callananc3a16002011-01-17 23:42:46 +00001773 {
Sean Callanan339f6152014-03-11 19:19:16 +00001774 if (!HandleSymbol(&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001775 {
1776 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001777 m_error_stream->Printf("Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol %s\n", global_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001778
Sean Callananc3a16002011-01-17 23:42:46 +00001779 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001780 }
Sean Callananc3a16002011-01-17 23:42:46 +00001781 }
Sean Callananfc89c142011-11-01 23:38:03 +00001782 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1783 {
Sean Callanan339f6152014-03-11 19:19:16 +00001784 if (!HandleObjCClass(&global_var))
Sean Callananfc89c142011-11-01 23:38:03 +00001785 {
1786 if (m_error_stream)
1787 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001788
Sean Callananfc89c142011-11-01 23:38:03 +00001789 return false;
1790 }
1791 }
Sean Callanan2ad66912013-04-24 21:25:20 +00001792 else if (global_name.find("OBJC_CLASSLIST_SUP_REFS_$") != global_name.npos)
1793 {
Sean Callanan339f6152014-03-11 19:19:16 +00001794 if (!HandleObjCClass(&global_var))
Sean Callanan2ad66912013-04-24 21:25:20 +00001795 {
1796 if (m_error_stream)
1797 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001798
Sean Callanan2ad66912013-04-24 21:25:20 +00001799 return false;
1800 }
1801 }
Sean Callanan339f6152014-03-11 19:19:16 +00001802 else if (DeclForGlobal(&global_var))
Sean Callananc3a16002011-01-17 23:42:46 +00001803 {
Sean Callanan339f6152014-03-11 19:19:16 +00001804 if (!MaybeHandleVariable (&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001805 {
1806 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001807 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite external variable %s\n", global_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001808
Sean Callananc3a16002011-01-17 23:42:46 +00001809 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001810 }
Sean Callananc3a16002011-01-17 23:42:46 +00001811 }
Sean Callanana4e55172010-11-08 00:31:32 +00001812 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001813
Sean Callanana4e55172010-11-08 00:31:32 +00001814 return true;
1815}
1816
Sean Callanan79763a42011-05-23 21:40:23 +00001817bool
1818IRForTarget::ReplaceStrings ()
1819{
Greg Clayton5160ce52013-03-27 23:08:40 +00001820 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001821
Sean Callanan79763a42011-05-23 21:40:23 +00001822 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001823
Sean Callanan79763a42011-05-23 21:40:23 +00001824 OffsetsTy offsets;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001825
Sean Callanan339f6152014-03-11 19:19:16 +00001826 for (GlobalVariable &gv : m_module->globals())
Sean Callanan79763a42011-05-23 21:40:23 +00001827 {
Sean Callanan339f6152014-03-11 19:19:16 +00001828 if (!gv.hasInitializer())
Sean Callanan79763a42011-05-23 21:40:23 +00001829 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001830
Sean Callanan339f6152014-03-11 19:19:16 +00001831 Constant *gc = gv.getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001832
Sean Callanan5207a342011-08-10 21:05:52 +00001833 std::string str;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001834
Sean Callanan5207a342011-08-10 21:05:52 +00001835 if (gc->isNullValue())
1836 {
1837 Type *gc_type = gc->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001838
Sean Callanan5207a342011-08-10 21:05:52 +00001839 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001840
Sean Callanan5207a342011-08-10 21:05:52 +00001841 if (!gc_array_type)
1842 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001843
Sean Callanan5207a342011-08-10 21:05:52 +00001844 Type *gc_element_type = gc_array_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001845
Sean Callanan5207a342011-08-10 21:05:52 +00001846 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001847
Sean Callanan5207a342011-08-10 21:05:52 +00001848 if (gc_integer_type->getBitWidth() != 8)
1849 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001850
Sean Callanan5207a342011-08-10 21:05:52 +00001851 str = "";
1852 }
1853 else
1854 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001855 ConstantDataArray *gc_array = dyn_cast<ConstantDataArray>(gc);
Sean Callanan5207a342011-08-10 21:05:52 +00001856
1857 if (!gc_array)
1858 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001859
Sean Callanan5207a342011-08-10 21:05:52 +00001860 if (!gc_array->isCString())
1861 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001862
Sean Callanan5207a342011-08-10 21:05:52 +00001863 if (log)
1864 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001865
Sean Callanan5207a342011-08-10 21:05:52 +00001866 str = gc_array->getAsString();
1867 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001868
Sean Callanan339f6152014-03-11 19:19:16 +00001869 offsets[&gv] = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001870
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001871 m_data_allocator.GetStream().Write(str.c_str(), str.length() + 1);
Sean Callanan79763a42011-05-23 21:40:23 +00001872 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001873
Sean Callanancc427fa2011-07-30 02:42:06 +00001874 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001875
Sean Callanan79763a42011-05-23 21:40:23 +00001876 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1877 oi != oe;
1878 ++oi)
1879 {
1880 GlobalVariable *gv = oi->first;
1881 size_t offset = oi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001882
Sean Callanan79763a42011-05-23 21:40:23 +00001883 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001884
Sean Callanan79763a42011-05-23 21:40:23 +00001885 if (log)
1886 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001887
Ed Mastea8553092014-03-10 17:24:16 +00001888 for (llvm::User *u : gv->users())
Sean Callanan79763a42011-05-23 21:40:23 +00001889 {
1890 if (log)
Ed Mastea8553092014-03-10 17:24:16 +00001891 log->Printf("Found use %s", PrintValue(u).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001892
Ed Mastea8553092014-03-10 17:24:16 +00001893 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(u);
1894 StoreInst *store_inst = dyn_cast<StoreInst>(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001895
Sean Callanan79763a42011-05-23 21:40:23 +00001896 if (const_expr)
1897 {
1898 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1899 {
1900 if (log)
1901 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001902
Sean Callanan79763a42011-05-23 21:40:23 +00001903 return false;
1904 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001905
Sean Callanan5207a342011-08-10 21:05:52 +00001906 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
1907 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001908
Sean Callanan5207a342011-08-10 21:05:52 +00001909 const_expr->replaceAllUsesWith(new_gep);
Sean Callanan79763a42011-05-23 21:40:23 +00001910 }
1911 else if (store_inst)
1912 {
1913 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001914
Sean Callanan79763a42011-05-23 21:40:23 +00001915 store_inst->setOperand(0, bit_cast);
1916 }
1917 else
1918 {
1919 if (log)
1920 log->Printf("Use (%s) of string variable is neither a constant nor a store", PrintValue(const_expr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001921
Sean Callanan79763a42011-05-23 21:40:23 +00001922 return false;
1923 }
1924 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001925
Sean Callanan79763a42011-05-23 21:40:23 +00001926 gv->eraseFromParent();
1927 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001928
Sean Callanan79763a42011-05-23 21:40:23 +00001929 return true;
1930}
1931
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001932bool
Sean Callanan79763a42011-05-23 21:40:23 +00001933IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
1934{
Greg Clayton5160ce52013-03-27 23:08:40 +00001935 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001936
Sean Callanan79763a42011-05-23 21:40:23 +00001937 typedef SmallVector <Value*, 2> ConstantList;
1938 typedef SmallVector <llvm::Instruction*, 2> UserList;
1939 typedef ConstantList::iterator ConstantIterator;
1940 typedef UserList::iterator UserIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001941
Sean Callanan79763a42011-05-23 21:40:23 +00001942 ConstantList static_constants;
1943 UserList static_users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001944
Sean Callanan79763a42011-05-23 21:40:23 +00001945 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
1946 ii != ie;
1947 ++ii)
1948 {
1949 llvm::Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001950
Sean Callanan339f6152014-03-11 19:19:16 +00001951 for (Value *operand_val : inst.operand_values())
Sean Callanan79763a42011-05-23 21:40:23 +00001952 {
Sean Callanan79763a42011-05-23 21:40:23 +00001953 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001954
Sean Callanan822944c2012-04-26 20:51:20 +00001955 if (operand_constant_fp/* && operand_constant_fp->getType()->isX86_FP80Ty()*/)
Sean Callanan79763a42011-05-23 21:40:23 +00001956 {
1957 static_constants.push_back(operand_val);
1958 static_users.push_back(ii);
1959 }
1960 }
1961 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001962
Sean Callanan79763a42011-05-23 21:40:23 +00001963 ConstantIterator constant_iter;
1964 UserIterator user_iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001965
Sean Callanan79763a42011-05-23 21:40:23 +00001966 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
1967 constant_iter != static_constants.end();
1968 ++constant_iter, ++user_iter)
1969 {
1970 Value *operand_val = *constant_iter;
1971 llvm::Instruction *inst = *user_iter;
1972
1973 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001974
Sean Callanan79763a42011-05-23 21:40:23 +00001975 if (operand_constant_fp)
1976 {
Sean Callanan1582ee62013-04-18 22:06:33 +00001977 Type *operand_type = operand_constant_fp->getType();
1978
Sean Callanan79763a42011-05-23 21:40:23 +00001979 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
1980 APInt operand_apint = operand_apfloat.bitcastToAPInt();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001981
Sean Callanan79763a42011-05-23 21:40:23 +00001982 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
1983 size_t operand_data_size = operand_apint.getBitWidth() / 8;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001984
Sean Callanan79763a42011-05-23 21:40:23 +00001985 if (log)
1986 {
1987 std::string s;
1988 raw_string_ostream ss(s);
1989 for (size_t index = 0;
1990 index < operand_data_size;
1991 ++index)
1992 {
1993 ss << (uint32_t)operand_raw_data[index];
1994 ss << " ";
1995 }
1996 ss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001997
Greg Clayton6fea17e2014-03-03 19:15:20 +00001998 log->Printf("Found ConstantFP with size %" PRIu64 " and raw data %s", (uint64_t)operand_data_size, s.c_str());
Sean Callanan79763a42011-05-23 21:40:23 +00001999 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002000
Sean Callanan79763a42011-05-23 21:40:23 +00002001 lldb_private::DataBufferHeap data(operand_data_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002002
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002003 if (lldb::endian::InlHostByteOrder() != m_data_allocator.GetStream().GetByteOrder())
Sean Callanan79763a42011-05-23 21:40:23 +00002004 {
2005 uint8_t *data_bytes = data.GetBytes();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002006
Sean Callanan79763a42011-05-23 21:40:23 +00002007 for (size_t index = 0;
2008 index < operand_data_size;
2009 ++index)
2010 {
2011 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
2012 }
2013 }
2014 else
2015 {
2016 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
2017 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002018
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002019 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002020
Sean Callanane3333d62012-06-08 22:20:41 +00002021 size_t align = m_target_data->getPrefTypeAlignment(operand_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002022
Sean Callanane3333d62012-06-08 22:20:41 +00002023 const size_t mask = (align - 1);
2024 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002025 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002026
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002027 m_data_allocator.GetStream().Write(data.GetBytes(), operand_data_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002028
Sean Callanancc427fa2011-07-30 02:42:06 +00002029 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002030
Sean Callanan1582ee62013-04-18 22:06:33 +00002031 Constant *new_pointer = BuildRelocation(fp_ptr_ty, aligned_offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002032
Sean Callanan79763a42011-05-23 21:40:23 +00002033 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002034
Sean Callanan79763a42011-05-23 21:40:23 +00002035 operand_constant_fp->replaceAllUsesWith(fp_load);
2036 }
2037 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002038
Sean Callanan79763a42011-05-23 21:40:23 +00002039 return true;
2040}
2041
Sean Callanan7ea35012010-07-27 21:39:39 +00002042static bool isGuardVariableRef(Value *V)
Sean Callananddb46ef2010-07-24 01:37:44 +00002043{
Sean Callanan77eaf442011-07-08 00:39:14 +00002044 Constant *Old = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002045
Sean Callananafe16a72010-11-17 23:00:36 +00002046 if (!(Old = dyn_cast<Constant>(V)))
Sean Callananddb46ef2010-07-24 01:37:44 +00002047 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002048
Sean Callanan77eaf442011-07-08 00:39:14 +00002049 ConstantExpr *CE = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002050
Sean Callanane2ef6e32010-09-23 03:01:22 +00002051 if ((CE = dyn_cast<ConstantExpr>(V)))
2052 {
2053 if (CE->getOpcode() != Instruction::BitCast)
2054 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002055
Sean Callananafe16a72010-11-17 23:00:36 +00002056 Old = CE->getOperand(0);
Sean Callanane2ef6e32010-09-23 03:01:22 +00002057 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002058
Sean Callananafe16a72010-11-17 23:00:36 +00002059 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002060
Zachary Turnerf16ae602015-01-09 21:12:22 +00002061 if (!GV || !GV->hasName() ||
2062 (!GV->getName().startswith("_ZGV") && // Itanium ABI guard variable
2063 !GV->getName().endswith("@4IA"))) // Microsoft ABI guard variable
2064 {
Sean Callananddb46ef2010-07-24 01:37:44 +00002065 return false;
Zachary Turnerf16ae602015-01-09 21:12:22 +00002066 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002067
Sean Callananddb46ef2010-07-24 01:37:44 +00002068 return true;
2069}
2070
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002071void
Sean Callanan79763a42011-05-23 21:40:23 +00002072IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callananddb46ef2010-07-24 01:37:44 +00002073{
Zachary Turnerf16ae602015-01-09 21:12:22 +00002074 Constant *zero(Constant::getNullValue(guard_load->getType()));
2075 guard_load->replaceAllUsesWith(zero);
Sean Callananddb46ef2010-07-24 01:37:44 +00002076 guard_load->eraseFromParent();
2077}
2078
2079static void ExciseGuardStore(Instruction* guard_store)
2080{
2081 guard_store->eraseFromParent();
2082}
2083
2084bool
Sean Callanan79763a42011-05-23 21:40:23 +00002085IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002086{
Sean Callananddb46ef2010-07-24 01:37:44 +00002087 ///////////////////////////////////////////////////////
2088 // Eliminate any reference to guard variables found.
2089 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002090
Sean Callanan7ea35012010-07-27 21:39:39 +00002091 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002092
Sean Callanan7ea35012010-07-27 21:39:39 +00002093 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callananddb46ef2010-07-24 01:37:44 +00002094 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002095
Sean Callananddb46ef2010-07-24 01:37:44 +00002096 InstrList guard_loads;
2097 InstrList guard_stores;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002098
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002099 for (ii = basic_block.begin();
2100 ii != basic_block.end();
Sean Callananddb46ef2010-07-24 01:37:44 +00002101 ++ii)
2102 {
2103 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002104
Sean Callananddb46ef2010-07-24 01:37:44 +00002105 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
2106 if (isGuardVariableRef(load->getPointerOperand()))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002107 guard_loads.push_back(&inst);
2108
2109 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callananddb46ef2010-07-24 01:37:44 +00002110 if (isGuardVariableRef(store->getPointerOperand()))
2111 guard_stores.push_back(&inst);
2112 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002113
Sean Callananddb46ef2010-07-24 01:37:44 +00002114 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002115
Sean Callananddb46ef2010-07-24 01:37:44 +00002116 for (iter = guard_loads.begin();
2117 iter != guard_loads.end();
2118 ++iter)
Sean Callanan79763a42011-05-23 21:40:23 +00002119 TurnGuardLoadIntoZero(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002120
Sean Callananddb46ef2010-07-24 01:37:44 +00002121 for (iter = guard_stores.begin();
2122 iter != guard_stores.end();
2123 ++iter)
2124 ExciseGuardStore(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002125
Sean Callananddb46ef2010-07-24 01:37:44 +00002126 return true;
2127}
2128
Sean Callanan3989fb92011-01-27 01:07:04 +00002129// This function does not report errors; its callers are responsible.
Sean Callananafe16a72010-11-17 23:00:36 +00002130bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002131IRForTarget::UnfoldConstant(Constant *old_constant,
2132 FunctionValueCache &value_maker,
2133 FunctionValueCache &entry_instruction_finder)
Sean Callanan7618f4e2010-07-14 23:40:29 +00002134{
Greg Clayton5160ce52013-03-27 23:08:40 +00002135 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan7618f4e2010-07-14 23:40:29 +00002136
Sean Callanan2235f322010-08-11 03:57:18 +00002137 SmallVector<User*, 16> users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002138
Sean Callanan2235f322010-08-11 03:57:18 +00002139 // We do this because the use list might change, invalidating our iterator.
2140 // Much better to keep a work list ourselves.
Ed Maste80e8cc62014-03-10 14:23:10 +00002141 for (llvm::User *u : old_constant->users())
2142 users.push_back(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002143
Johnny Chen44805302011-07-19 19:48:13 +00002144 for (size_t i = 0;
Sean Callanan2235f322010-08-11 03:57:18 +00002145 i < users.size();
2146 ++i)
2147 {
2148 User *user = users[i];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002149
Sean Callanan7618f4e2010-07-14 23:40:29 +00002150 if (Constant *constant = dyn_cast<Constant>(user))
2151 {
2152 // synthesize a new non-constant equivalent of the constant
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002153
Sean Callanan7618f4e2010-07-14 23:40:29 +00002154 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
2155 {
2156 switch (constant_expr->getOpcode())
2157 {
2158 default:
2159 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002160 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002161 return false;
2162 case Instruction::BitCast:
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002163 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002164 FunctionValueCache bit_cast_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2165 // UnaryExpr
2166 // OperandList[0] is value
2167
2168 if (constant_expr->getOperand(0) != old_constant)
2169 return constant_expr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002170
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002171 return new BitCastInst(value_maker.GetValue(function),
2172 constant_expr->getType(),
2173 "",
2174 llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2175 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002176
Sean Callanan0e016fe2013-07-15 23:31:47 +00002177 if (!UnfoldConstant(constant_expr, bit_cast_maker, entry_instruction_finder))
2178 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002179 }
2180 break;
2181 case Instruction::GetElementPtr:
2182 {
2183 // GetElementPtrConstantExpr
2184 // OperandList[0] is base
2185 // OperandList[1]... are indices
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002186
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002187 FunctionValueCache get_element_pointer_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2188 Value *ptr = constant_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002189
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002190 if (ptr == old_constant)
2191 ptr = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002192
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002193 std::vector<Value*> index_vector;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002194
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002195 unsigned operand_index;
2196 unsigned num_operands = constant_expr->getNumOperands();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002197
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002198 for (operand_index = 1;
2199 operand_index < num_operands;
2200 ++operand_index)
2201 {
2202 Value *operand = constant_expr->getOperand(operand_index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002203
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002204 if (operand == old_constant)
2205 operand = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002206
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002207 index_vector.push_back(operand);
2208 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002209
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002210 ArrayRef <Value*> indices(index_vector);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002211
Vince Harron0fc6c672015-03-16 03:54:22 +00002212 return GetElementPtrInst::Create(nullptr, ptr, indices, "", llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002213 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002214
Sean Callanan0e016fe2013-07-15 23:31:47 +00002215 if (!UnfoldConstant(constant_expr, get_element_pointer_maker, entry_instruction_finder))
2216 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002217 }
2218 break;
2219 }
2220 }
2221 else
2222 {
2223 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002224 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002225 return false;
2226 }
2227 }
2228 else
2229 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002230 if (Instruction *inst = llvm::dyn_cast<Instruction>(user))
2231 {
2232 inst->replaceUsesOfWith(old_constant, value_maker.GetValue(inst->getParent()->getParent()));
2233 }
2234 else
2235 {
2236 if (log)
2237 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(user).c_str());
2238 return false;
2239 }
Sean Callanan7618f4e2010-07-14 23:40:29 +00002240 }
2241 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002242
Sean Callanan0e016fe2013-07-15 23:31:47 +00002243 if (!isa<GlobalValue>(old_constant))
2244 {
2245 old_constant->destroyConstant();
2246 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002247
Sean Callanan7618f4e2010-07-14 23:40:29 +00002248 return true;
2249}
2250
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002251bool
Sean Callanan79763a42011-05-23 21:40:23 +00002252IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan549c9f72010-07-13 21:41:46 +00002253{
Sean Callanan9e6ed532010-09-13 21:34:21 +00002254 if (!m_resolve_vars)
2255 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002256
Greg Clayton5160ce52013-03-27 23:08:40 +00002257 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan549c9f72010-07-13 21:41:46 +00002258
2259 m_decl_map->DoStructLayout();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002260
Sean Callanan549c9f72010-07-13 21:41:46 +00002261 if (log)
2262 log->Printf("Element arrangement:");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002263
Sean Callanan549c9f72010-07-13 21:41:46 +00002264 uint32_t num_elements;
2265 uint32_t element_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002266
Sean Callanan549c9f72010-07-13 21:41:46 +00002267 size_t size;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002268 lldb::offset_t alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002269
Sean Callanan549c9f72010-07-13 21:41:46 +00002270 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2271 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002272
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002273 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002274
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002275 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002276 {
2277 if (m_error_stream)
2278 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes no arguments (should take at least a struct pointer)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002279
Sean Callanan549c9f72010-07-13 21:41:46 +00002280 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002281 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002282
Sean Callanan7ea35012010-07-27 21:39:39 +00002283 Argument *argument = iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002284
Sean Callananfc55f5d2010-09-21 00:44:12 +00002285 if (argument->getName().equals("this"))
2286 {
2287 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002288
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002289 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002290 {
2291 if (m_error_stream)
2292 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'this' argument (should take a struct pointer too)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002293
Sean Callananfc55f5d2010-09-21 00:44:12 +00002294 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002295 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002296
Sean Callananfc55f5d2010-09-21 00:44:12 +00002297 argument = iter;
2298 }
Sean Callanan17827832010-12-13 22:46:15 +00002299 else if (argument->getName().equals("self"))
2300 {
2301 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002302
Sean Callanan17827832010-12-13 22:46:15 +00002303 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002304 {
2305 if (m_error_stream)
2306 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' argument (should take '_cmd' and a struct pointer too)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002307
Sean Callanan17827832010-12-13 22:46:15 +00002308 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002309 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002310
Sean Callanan17827832010-12-13 22:46:15 +00002311 if (!iter->getName().equals("_cmd"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002312 {
2313 if (m_error_stream)
2314 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes '%s' after 'self' argument (should take '_cmd')", iter->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002315
Sean Callanan17827832010-12-13 22:46:15 +00002316 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002317 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002318
Sean Callanan17827832010-12-13 22:46:15 +00002319 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002320
Sean Callanan17827832010-12-13 22:46:15 +00002321 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002322 {
2323 if (m_error_stream)
2324 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' and '_cmd' arguments (should take a struct pointer too)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002325
Sean Callanan17827832010-12-13 22:46:15 +00002326 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002327 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002328
Sean Callanan17827832010-12-13 22:46:15 +00002329 argument = iter;
2330 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002331
Greg Clayton7b462cc2010-10-15 22:48:33 +00002332 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002333 {
2334 if (m_error_stream)
2335 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes an argument named '%s' instead of the struct pointer", argument->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002336
Sean Callanan549c9f72010-07-13 21:41:46 +00002337 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002338 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002339
Sean Callanan549c9f72010-07-13 21:41:46 +00002340 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002341 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002342
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002343 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callananafe16a72010-11-17 23:00:36 +00002344 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002345
Sean Callananafe16a72010-11-17 23:00:36 +00002346 if (!FirstEntryInstruction)
Sean Callanan3989fb92011-01-27 01:07:04 +00002347 {
2348 if (m_error_stream)
2349 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the first instruction in the wrapper for use in rewriting");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002350
Sean Callanan549c9f72010-07-13 21:41:46 +00002351 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002352 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002353
Sean Callanan79763a42011-05-23 21:40:23 +00002354 LLVMContext &context(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +00002355 IntegerType *offset_type(Type::getInt32Ty(context));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002356
Sean Callanan549c9f72010-07-13 21:41:46 +00002357 if (!offset_type)
Sean Callanan3989fb92011-01-27 01:07:04 +00002358 {
2359 if (m_error_stream)
2360 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002361
Sean Callanan549c9f72010-07-13 21:41:46 +00002362 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002363 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002364
Sean Callanan549c9f72010-07-13 21:41:46 +00002365 for (element_index = 0; element_index < num_elements; ++element_index)
2366 {
Sean Callanan77eaf442011-07-08 00:39:14 +00002367 const clang::NamedDecl *decl = NULL;
2368 Value *value = NULL;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002369 lldb::offset_t offset;
Greg Clayton7b462cc2010-10-15 22:48:33 +00002370 lldb_private::ConstString name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002371
Sean Callanan823bb4c2010-08-30 22:17:16 +00002372 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan3989fb92011-01-27 01:07:04 +00002373 {
2374 if (m_error_stream)
2375 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002376
Sean Callanan549c9f72010-07-13 21:41:46 +00002377 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002378 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002379
Sean Callanan549c9f72010-07-13 21:41:46 +00002380 if (log)
Zachary Turnera746e8e2014-07-02 17:24:07 +00002381 log->Printf(" \"%s\" (\"%s\") placed at %" PRIu64,
Greg Clayton7b462cc2010-10-15 22:48:33 +00002382 name.GetCString(),
Sean Callananc70ed462011-10-25 18:36:40 +00002383 decl->getNameAsString().c_str(),
Sean Callanan549c9f72010-07-13 21:41:46 +00002384 offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002385
Sean Callananc70ed462011-10-25 18:36:40 +00002386 if (value)
Sean Callanan92adcac2011-01-13 08:53:35 +00002387 {
Sean Callananc70ed462011-10-25 18:36:40 +00002388 if (log)
2389 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002390
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002391 FunctionValueCache body_result_maker ([this, name, offset_type, offset, argument, value] (llvm::Function *function)->llvm::Value * {
2392 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2393 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2394 // entry in order to produce the static variable that the AST thinks it is accessing.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002395
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002396 llvm::Instruction *entry_instruction = llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002397
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002398 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
Vince Harron0fc6c672015-03-16 03:54:22 +00002399 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(nullptr,
2400 argument,
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002401 offset_int,
2402 "",
2403 entry_instruction);
2404
2405 if (name == m_result_name && !m_result_is_pointer)
2406 {
2407 BitCastInst *bit_cast = new BitCastInst(get_element_ptr,
2408 value->getType()->getPointerTo(),
2409 "",
2410 entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002411
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002412 LoadInst *load = new LoadInst(bit_cast, "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002413
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002414 return load;
2415 }
2416 else
2417 {
2418 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002419
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002420 return bit_cast;
2421 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002422 });
2423
Sean Callananc70ed462011-10-25 18:36:40 +00002424 if (Constant *constant = dyn_cast<Constant>(value))
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002425 {
2426 UnfoldConstant(constant, body_result_maker, m_entry_instruction_finder);
2427 }
2428 else if (Instruction *instruction = dyn_cast<Instruction>(value))
2429 {
2430 value->replaceAllUsesWith(body_result_maker.GetValue(instruction->getParent()->getParent()));
2431 }
Sean Callananc70ed462011-10-25 18:36:40 +00002432 else
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002433 {
2434 if (log)
2435 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(value).c_str());
2436 return false;
2437 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002438
Sean Callananc70ed462011-10-25 18:36:40 +00002439 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2440 var->eraseFromParent();
2441 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002442 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002443
Sean Callanan549c9f72010-07-13 21:41:46 +00002444 if (log)
Greg Clayton6fea17e2014-03-03 19:15:20 +00002445 log->Printf("Total structure [align %" PRId64 ", size %" PRIu64 "]", (int64_t)alignment, (uint64_t)size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002446
Sean Callanan549c9f72010-07-13 21:41:46 +00002447 return true;
2448}
2449
Sean Callanan79763a42011-05-23 21:40:23 +00002450llvm::Constant *
Greg Clayton5160ce52013-03-27 23:08:40 +00002451IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
Sean Callanan79763a42011-05-23 21:40:23 +00002452{
Sean Callanan439dcae2013-12-20 19:55:02 +00002453 llvm::Constant *offset_int = ConstantInt::get(m_intptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002454
Sean Callanancc427fa2011-07-30 02:42:06 +00002455 llvm::Constant *offset_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002456
Sean Callanancc427fa2011-07-30 02:42:06 +00002457 offset_array[0] = offset_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002458
Sean Callanancc427fa2011-07-30 02:42:06 +00002459 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
Sean Callanan507b5882015-04-14 18:17:35 +00002460 llvm::Type *char_type = llvm::Type::getInt8Ty(m_module->getContext());
2461 llvm::Type *char_pointer_type = char_type->getPointerTo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002462
Sean Callanan507b5882015-04-14 18:17:35 +00002463 llvm::Constant *reloc_placeholder_bitcast = ConstantExpr::getBitCast(m_reloc_placeholder, char_pointer_type);
2464 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(char_type, reloc_placeholder_bitcast, offsets);
Sean Callanana85f0e82015-04-06 23:51:08 +00002465 llvm::Constant *reloc_bitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002466
Sean Callanana85f0e82015-04-06 23:51:08 +00002467 return reloc_bitcast;
Sean Callanan79763a42011-05-23 21:40:23 +00002468}
2469
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002470bool
Sean Callanan79763a42011-05-23 21:40:23 +00002471IRForTarget::CompleteDataAllocation ()
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002472{
Greg Clayton5160ce52013-03-27 23:08:40 +00002473 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan79763a42011-05-23 21:40:23 +00002474
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002475 if (!m_data_allocator.GetStream().GetSize())
Sean Callanan79763a42011-05-23 21:40:23 +00002476 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002477
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002478 lldb::addr_t allocation = m_data_allocator.Allocate();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002479
Sean Callanan79763a42011-05-23 21:40:23 +00002480 if (log)
2481 {
2482 if (allocation)
2483 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2484 else
2485 log->Printf("Failed to allocate static data");
2486 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002487
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002488 if (!allocation || allocation == LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +00002489 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002490
Sean Callanan439dcae2013-12-20 19:55:02 +00002491 Constant *relocated_addr = ConstantInt::get(m_intptr_ty, (uint64_t)allocation);
Sean Callanan79763a42011-05-23 21:40:23 +00002492 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002493
Sean Callanan79763a42011-05-23 21:40:23 +00002494 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002495
Sean Callanan79763a42011-05-23 21:40:23 +00002496 m_reloc_placeholder->eraseFromParent();
2497
2498 return true;
2499}
2500
Sean Callanan2ab712f22010-07-03 01:35:46 +00002501bool
Sean Callanan3d654b32012-09-24 22:25:51 +00002502IRForTarget::StripAllGVs (Module &llvm_module)
2503{
Greg Clayton5160ce52013-03-27 23:08:40 +00002504 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3d654b32012-09-24 22:25:51 +00002505 std::vector<GlobalVariable *> global_vars;
2506 std::set<GlobalVariable *>erased_vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002507
Sean Callanan3d654b32012-09-24 22:25:51 +00002508 bool erased = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002509
Sean Callanan3d654b32012-09-24 22:25:51 +00002510 while (erased)
2511 {
2512 erased = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002513
Sean Callanan339f6152014-03-11 19:19:16 +00002514 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002515 {
Sean Callanan339f6152014-03-11 19:19:16 +00002516 global_var.removeDeadConstantUsers();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002517
Sean Callanan339f6152014-03-11 19:19:16 +00002518 if (global_var.use_empty())
Sean Callanan3d654b32012-09-24 22:25:51 +00002519 {
2520 if (log)
2521 log->Printf("Did remove %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002522 PrintValue(&global_var).c_str());
2523 global_var.eraseFromParent();
Sean Callanan3d654b32012-09-24 22:25:51 +00002524 erased = true;
2525 break;
2526 }
2527 }
2528 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002529
Sean Callanan339f6152014-03-11 19:19:16 +00002530 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002531 {
Sean Callanan339f6152014-03-11 19:19:16 +00002532 GlobalValue::user_iterator ui = global_var.user_begin();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002533
Jim Inghamd77557d2012-11-26 19:54:04 +00002534 if (log)
2535 log->Printf("Couldn't remove %s because of %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002536 PrintValue(&global_var).c_str(),
Jim Inghamd77557d2012-11-26 19:54:04 +00002537 PrintValue(*ui).c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +00002538 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002539
Sean Callanan3d654b32012-09-24 22:25:51 +00002540 return true;
2541}
2542
2543bool
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002544IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002545{
Greg Clayton5160ce52013-03-27 23:08:40 +00002546 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002547
Sean Callanan79763a42011-05-23 21:40:23 +00002548 m_module = &llvm_module;
Micah Villmow8468dbe2012-10-08 16:28:57 +00002549 m_target_data.reset(new DataLayout(m_module));
Sean Callanan439dcae2013-12-20 19:55:02 +00002550 m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), m_target_data->getPointerSizeInBits());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002551
Sean Callananc70ed462011-10-25 18:36:40 +00002552 if (log)
2553 {
2554 std::string s;
2555 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002556
Sean Callananc70ed462011-10-25 18:36:40 +00002557 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002558
Sean Callananc70ed462011-10-25 18:36:40 +00002559 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002560
Sean Callananc70ed462011-10-25 18:36:40 +00002561 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
2562 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002563
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002564 Function* main_function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002565
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002566 if (!main_function)
2567 {
2568 if (log)
2569 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002570
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002571 if (m_error_stream)
2572 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
2573
2574 return false;
2575 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002576
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002577 if (!FixFunctionLinkage (*main_function))
2578 {
2579 if (log)
2580 log->Printf("Couldn't fix the linkage for the function");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002581
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002582 return false;
2583 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002584
Sean Callanan439dcae2013-12-20 19:55:02 +00002585 llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002586
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002587 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
Sean Callanan439dcae2013-12-20 19:55:02 +00002588 int8_ty,
Sean Callanan3d654b32012-09-24 22:25:51 +00002589 false /* IsConstant */,
Sean Callanan79763a42011-05-23 21:40:23 +00002590 GlobalVariable::InternalLinkage,
Sean Callanan439dcae2013-12-20 19:55:02 +00002591 Constant::getNullValue(int8_ty),
Sean Callanan79763a42011-05-23 21:40:23 +00002592 "reloc_placeholder",
2593 NULL /* InsertBefore */,
Sean Callanan3d654b32012-09-24 22:25:51 +00002594 GlobalVariable::NotThreadLocal /* ThreadLocal */,
Sean Callanan79763a42011-05-23 21:40:23 +00002595 0 /* AddressSpace */);
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002596
Sean Callanand1e5b432010-08-12 01:56:52 +00002597 ////////////////////////////////////////////////////////////
Greg Clayton7b462cc2010-10-15 22:48:33 +00002598 // Replace $__lldb_expr_result with a persistent variable
Sean Callanand1e5b432010-08-12 01:56:52 +00002599 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002600
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002601 if (!CreateResultVariable(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002602 {
2603 if (log)
2604 log->Printf("CreateResultVariable() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002605
Sean Callanan3989fb92011-01-27 01:07:04 +00002606 // CreateResultVariable() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002607
Sean Callanand1e5b432010-08-12 01:56:52 +00002608 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002609 }
Sean Callanan6e6d4a62012-07-21 02:02:15 +00002610
Sean Callananea685ae2011-11-01 17:33:54 +00002611 if (log && log->GetVerbose())
Sean Callanan79763a42011-05-23 21:40:23 +00002612 {
2613 std::string s;
2614 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002615
Sean Callanan79763a42011-05-23 21:40:23 +00002616 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002617
Sean Callanan79763a42011-05-23 21:40:23 +00002618 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002619
Sean Callanan79763a42011-05-23 21:40:23 +00002620 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2621 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002622
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002623 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2624 fi != fe;
2625 ++fi)
2626 {
2627 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002628
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002629 if (function->begin() == function->end())
2630 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002631
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002632 Function::iterator bbi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002633
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002634 for (bbi = function->begin();
2635 bbi != function->end();
2636 ++bbi)
2637 {
2638 if (!RemoveGuards(*bbi))
2639 {
2640 if (log)
2641 log->Printf("RemoveGuards() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002642
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002643 // RemoveGuards() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002644
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002645 return false;
2646 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002647
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002648 if (!RewritePersistentAllocs(*bbi))
2649 {
2650 if (log)
2651 log->Printf("RewritePersistentAllocs() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002652
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002653 // RewritePersistentAllocs() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002654
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002655 return false;
2656 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002657
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002658 if (!RemoveCXAAtExit(*bbi))
2659 {
2660 if (log)
2661 log->Printf("RemoveCXAAtExit() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002662
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002663 // RemoveCXAAtExit() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002664
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002665 return false;
2666 }
2667 }
2668 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002669
Sean Callananafe16a72010-11-17 23:00:36 +00002670 ///////////////////////////////////////////////////////////////////////////////
2671 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2672 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002673
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002674 if (!RewriteObjCConstStrings())
Sean Callanan17827832010-12-13 22:46:15 +00002675 {
2676 if (log)
2677 log->Printf("RewriteObjCConstStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002678
Sean Callanan3989fb92011-01-27 01:07:04 +00002679 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002680
Sean Callananafe16a72010-11-17 23:00:36 +00002681 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002682 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002683
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002684 ///////////////////////////////
2685 // Resolve function pointers
2686 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002687
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002688 if (!ResolveFunctionPointers(llvm_module))
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002689 {
2690 if (log)
2691 log->Printf("ResolveFunctionPointers() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002692
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002693 // ResolveFunctionPointers() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002694
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002695 return false;
2696 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002697
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002698 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2699 fi != fe;
2700 ++fi)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002701 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002702 llvm::Function *function = fi;
2703
2704 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2705 bbi != bbe;
2706 ++bbi)
Sean Callanan17827832010-12-13 22:46:15 +00002707 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002708 if (!RewriteObjCSelectors(*bbi))
2709 {
2710 if (log)
2711 log->Printf("RewriteObjCSelectors() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002712
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002713 // RewriteObjCSelectors() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002714
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002715 return false;
2716 }
Sean Callanan17827832010-12-13 22:46:15 +00002717 }
Sean Callananbad134f2012-07-27 19:25:24 +00002718 }
Sean Callanan2235f322010-08-11 03:57:18 +00002719
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002720 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2721 fi != fe;
2722 ++fi)
Sean Callananbad134f2012-07-27 19:25:24 +00002723 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002724 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002725
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002726 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2727 bbi != bbe;
2728 ++bbi)
Sean Callanan79763a42011-05-23 21:40:23 +00002729 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002730 if (!ResolveCalls(*bbi))
2731 {
2732 if (log)
2733 log->Printf("ResolveCalls() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002734
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002735 // ResolveCalls() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002736
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002737 return false;
2738 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002739
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002740 if (!ReplaceStaticLiterals(*bbi))
2741 {
2742 if (log)
2743 log->Printf("ReplaceStaticLiterals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002744
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002745 return false;
2746 }
Sean Callanan79763a42011-05-23 21:40:23 +00002747 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002748 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002749
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002750 ////////////////////////////////////////////////////////////////////////
2751 // Run function-level passes that only make sense on the main function
Sean Callanan038df5032010-09-30 21:18:25 +00002752 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002753
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002754 if (!ResolveExternals(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002755 {
2756 if (log)
2757 log->Printf("ResolveExternals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002758
Sean Callanan3989fb92011-01-27 01:07:04 +00002759 // ResolveExternals() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002760
Sean Callanana4e55172010-11-08 00:31:32 +00002761 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002762 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002763
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002764 if (!ReplaceVariables(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002765 {
2766 if (log)
2767 log->Printf("ReplaceVariables() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002768
Sean Callanan3989fb92011-01-27 01:07:04 +00002769 // ReplaceVariables() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002770
Sean Callanan038df5032010-09-30 21:18:25 +00002771 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002772 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002773
Sean Callanan79763a42011-05-23 21:40:23 +00002774 if (!ReplaceStrings())
2775 {
2776 if (log)
2777 log->Printf("ReplaceStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002778
Sean Callanan79763a42011-05-23 21:40:23 +00002779 return false;
2780 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002781
Sean Callanan79763a42011-05-23 21:40:23 +00002782 if (!CompleteDataAllocation())
2783 {
2784 if (log)
2785 log->Printf("CompleteDataAllocation() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002786
Sean Callanan79763a42011-05-23 21:40:23 +00002787 return false;
2788 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002789
Sean Callanan3d654b32012-09-24 22:25:51 +00002790 if (!StripAllGVs(llvm_module))
2791 {
2792 if (log)
2793 log->Printf("StripAllGVs() failed");
2794 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002795
Sean Callananea685ae2011-11-01 17:33:54 +00002796 if (log && log->GetVerbose())
Sean Callanan549c9f72010-07-13 21:41:46 +00002797 {
Sean Callanancc54bd32010-07-28 01:00:59 +00002798 std::string s;
2799 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002800
Sean Callanan79763a42011-05-23 21:40:23 +00002801 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002802
Sean Callanancc54bd32010-07-28 01:00:59 +00002803 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002804
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002805 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan2ab712f22010-07-03 01:35:46 +00002806 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002807
2808 return true;
Sean Callanan2ab712f22010-07-03 01:35:46 +00002809}
2810
2811void
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002812IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002813{
2814}
2815
2816PassManagerType
2817IRForTarget::getPotentialPassManagerType() const
2818{
2819 return PMT_ModulePassManager;
2820}