blob: 85ddcfa25954c70283f5800ccb0b6a0ce2707172 [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
10#include "lldb/Expression/IRForTarget.h"
11
12#include "llvm/Support/raw_ostream.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000013#include "llvm/IR/Constants.h"
14#include "llvm/IR/DataLayout.h"
15#include "llvm/IR/InstrTypes.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/Intrinsics.h"
18#include "llvm/IR/Module.h"
Ilia Kc9a475d2015-02-13 10:49:18 +000019#include "llvm/IR/LegacyPassManager.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000020#include "llvm/Transforms/IPO.h"
Zachary Turner543afa12014-12-09 22:29:47 +000021#include "llvm/IR/Metadata.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000022#include "llvm/IR/ValueSymbolTable.h"
Sean Callanan549c9f72010-07-13 21:41:46 +000023
24#include "clang/AST/ASTContext.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000025
26#include "lldb/Core/dwarf.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000027#include "lldb/Core/ConstString.h"
28#include "lldb/Core/DataBufferHeap.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000029#include "lldb/Core/Log.h"
30#include "lldb/Core/Scalar.h"
31#include "lldb/Core/StreamString.h"
32#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000033#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000034#include "lldb/Expression/IRInterpreter.h"
Sean Callanan79763a42011-05-23 21:40:23 +000035#include "lldb/Host/Endian.h"
Sean Callanan63697e52011-05-07 01:06:41 +000036#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000037#include "lldb/Symbol/ClangASTType.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000038
39#include <map>
40
41using namespace llvm;
42
Sean Callananeaacbc92010-08-18 18:50:51 +000043static char ID;
44
Sean Callanan8dfb68e2013-03-19 00:10:07 +000045IRForTarget::StaticDataAllocator::StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit) :
46 m_execution_unit(execution_unit),
Sean Callanan1582ee62013-04-18 22:06:33 +000047 m_stream_string(lldb_private::Stream::eBinary, execution_unit.GetAddressByteSize(), execution_unit.GetByteOrder()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000048 m_allocation(LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +000049{
50}
51
Sean Callanan1f9db3e2013-06-28 21:44:15 +000052IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker) :
53 m_maker(maker),
54 m_values()
55{
56}
57
58IRForTarget::FunctionValueCache::~FunctionValueCache()
59{
60}
61
Greg Clayton526ae042015-02-12 00:34:25 +000062llvm::Value *
63IRForTarget::FunctionValueCache::GetValue(llvm::Function *function)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000064{
Sean Callanan1f9db3e2013-06-28 21:44:15 +000065 if (!m_values.count(function))
66 {
67 llvm::Value *ret = m_maker(function);
68 m_values[function] = ret;
69 return ret;
70 }
71 return m_values[function];
72}
73
Greg Clayton526ae042015-02-12 00:34:25 +000074lldb::addr_t
75IRForTarget::StaticDataAllocator::Allocate()
Sean Callanan79763a42011-05-23 21:40:23 +000076{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000077 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000078
Sean Callanan8dfb68e2013-03-19 00:10:07 +000079 if (m_allocation != LLDB_INVALID_ADDRESS)
80 {
81 m_execution_unit.FreeNow(m_allocation);
82 m_allocation = LLDB_INVALID_ADDRESS;
83 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000084
Sean Callanan8dfb68e2013-03-19 00:10:07 +000085 m_allocation = m_execution_unit.WriteNow((const uint8_t*)m_stream_string.GetData(), m_stream_string.GetSize(), err);
86
87 return m_allocation;
Sean Callanan79763a42011-05-23 21:40:23 +000088}
89
Greg Clayton526ae042015-02-12 00:34:25 +000090lldb::TargetSP
91IRForTarget::StaticDataAllocator::GetTarget()
92{
93 return m_execution_unit.GetTarget();
94}
95
96static llvm::Value *
97FindEntryInstruction (llvm::Function *function)
Sean Callanan1f9db3e2013-06-28 21:44:15 +000098{
99 if (function->empty())
100 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000101
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000102 return function->getEntryBlock().getFirstNonPHIOrDbg();
103}
104
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000105IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
106 bool resolve_vars,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000107 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +0000108 lldb_private::Stream *error_stream,
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000109 const char *func_name) :
Sean Callanane2ef6e32010-09-23 03:01:22 +0000110 ModulePass(ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000111 m_resolve_vars(resolve_vars),
112 m_func_name(func_name),
Sean Callanan79763a42011-05-23 21:40:23 +0000113 m_module(NULL),
Johnny Chen44805302011-07-19 19:48:13 +0000114 m_decl_map(decl_map),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000115 m_data_allocator(execution_unit),
Sean Callananafe16a72010-11-17 23:00:36 +0000116 m_CFStringCreateWithBytes(NULL),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000117 m_sel_registerName(NULL),
Sean Callanan439dcae2013-12-20 19:55:02 +0000118 m_intptr_ty(NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000119 m_error_stream(error_stream),
Sean Callanan63697e52011-05-07 01:06:41 +0000120 m_result_store(NULL),
121 m_result_is_pointer(false),
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000122 m_reloc_placeholder(NULL),
123 m_entry_instruction_finder (FindEntryInstruction)
Sean Callanan2ab712f22010-07-03 01:35:46 +0000124{
125}
126
Sean Callanan038df5032010-09-30 21:18:25 +0000127/* Handy utility functions used at several places in the code */
Sean Callanan2235f322010-08-11 03:57:18 +0000128
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000129static std::string
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000130PrintValue(const Value *value, bool truncate = false)
Sean Callanan2235f322010-08-11 03:57:18 +0000131{
132 std::string s;
Jim Ingham28eb5712012-10-12 17:34:26 +0000133 if (value)
134 {
135 raw_string_ostream rso(s);
136 value->print(rso);
137 rso.flush();
138 if (truncate)
139 s.resize(s.length() - 1);
140 }
Sean Callanan2235f322010-08-11 03:57:18 +0000141 return s;
142}
143
Sean Callanan038df5032010-09-30 21:18:25 +0000144static std::string
Greg Clayton57ee3062013-07-11 22:46:58 +0000145PrintType(const llvm::Type *type, bool truncate = false)
Sean Callanan038df5032010-09-30 21:18:25 +0000146{
147 std::string s;
148 raw_string_ostream rso(s);
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000149 type->print(rso);
Sean Callanan038df5032010-09-30 21:18:25 +0000150 rso.flush();
151 if (truncate)
152 s.resize(s.length() - 1);
153 return s;
154}
155
Sean Callanan2ab712f22010-07-03 01:35:46 +0000156IRForTarget::~IRForTarget()
157{
158}
159
Sean Callanan79763a42011-05-23 21:40:23 +0000160bool
161IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
162{
Sean Callanan79763a42011-05-23 21:40:23 +0000163 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000164
Sean Callanan7f27d602011-11-19 02:54:21 +0000165 std::string name = llvm_function.getName().str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000166
Sean Callanan79763a42011-05-23 21:40:23 +0000167 return true;
168}
169
Greg Clayton23f8c952014-03-24 23:10:19 +0000170IRForTarget::LookupResult
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000171IRForTarget::GetFunctionAddress (llvm::Function *fun,
172 uint64_t &fun_addr,
173 lldb_private::ConstString &name,
174 Constant **&value_ptr)
175{
Greg Clayton5160ce52013-03-27 23:08:40 +0000176 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000177
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000178 fun_addr = LLDB_INVALID_ADDRESS;
179 name.Clear();
180 value_ptr = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000181
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000182 if (fun->isIntrinsic())
183 {
184 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000185
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000186 switch (intrinsic_id)
187 {
188 default:
189 if (log)
190 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000191
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000192 if (m_error_stream)
193 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 +0000194
Jason Molenda52d76032014-07-09 01:10:37 +0000195 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000196 case Intrinsic::memcpy:
197 {
198 static lldb_private::ConstString g_memcpy_str ("memcpy");
199 name = g_memcpy_str;
200 }
201 break;
Sean Callanana6cbf062011-11-16 00:20:50 +0000202 case Intrinsic::memset:
203 {
204 static lldb_private::ConstString g_memset_str ("memset");
205 name = g_memset_str;
206 }
207 break;
Greg Clayton23f8c952014-03-24 23:10:19 +0000208 case Intrinsic::dbg_declare:
Sean Callanan131be992014-04-09 00:59:41 +0000209 case Intrinsic::dbg_value:
Greg Clayton23f8c952014-03-24 23:10:19 +0000210 return LookupResult::Ignore;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000211 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000212
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000213 if (log && name)
214 log->Printf("Resolved intrinsic name \"%s\"", name.GetCString());
215 }
216 else
217 {
218 name.SetCStringWithLength (fun->getName().data(), fun->getName().size());
219 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000220
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000221 // Find the address of the function.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000222
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000223 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000225 if (fun_decl)
226 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000227 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_addr))
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000228 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000229 lldb_private::ConstString altnernate_name;
Greg Claytonf0705c82011-10-22 03:33:13 +0000230 bool found_it = m_decl_map->GetFunctionAddress (name, fun_addr);
231 if (!found_it)
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000232 {
Greg Claytonf0705c82011-10-22 03:33:13 +0000233 // Check for an alternate mangling for "std::basic_string<char>"
234 // that is part of the itanium C++ name mangling scheme
235 const char *name_cstr = name.GetCString();
Jim Ingham28eb5712012-10-12 17:34:26 +0000236 if (name_cstr && strncmp(name_cstr, "_ZNKSbIcE", strlen("_ZNKSbIcE")) == 0)
Greg Claytonf0705c82011-10-22 03:33:13 +0000237 {
238 std::string alternate_mangling("_ZNKSs");
239 alternate_mangling.append (name_cstr + strlen("_ZNKSbIcE"));
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000240 altnernate_name.SetCString(alternate_mangling.c_str());
241 found_it = m_decl_map->GetFunctionAddress (altnernate_name, fun_addr);
Greg Claytonf0705c82011-10-22 03:33:13 +0000242 }
243 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000244
Greg Claytonf0705c82011-10-22 03:33:13 +0000245 if (!found_it)
246 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000247 lldb_private::Mangled mangled_name(name);
248 lldb_private::Mangled alt_mangled_name(altnernate_name);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000249 if (log)
Greg Claytonf0705c82011-10-22 03:33:13 +0000250 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000251 if (alt_mangled_name)
252 log->Printf("Function \"%s\" (alternate name \"%s\") has no address",
253 mangled_name.GetName().GetCString(),
254 alt_mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000255 else
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000256 log->Printf("Function \"%s\" had no address",
257 mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000258 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000259
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000260 if (m_error_stream)
Greg Claytonf0705c82011-10-22 03:33:13 +0000261 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000262 if (alt_mangled_name)
263 m_error_stream->Printf("error: call to a function '%s' (alternate name '%s') that is not present in the target\n",
264 mangled_name.GetName().GetCString(),
265 alt_mangled_name.GetName().GetCString());
Greg Claytonda1eb042013-04-23 21:48:38 +0000266 else if (mangled_name.GetMangledName())
267 m_error_stream->Printf("error: call to a function '%s' ('%s') that is not present in the target\n",
268 mangled_name.GetName().GetCString(),
269 mangled_name.GetMangledName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000270 else
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000271 m_error_stream->Printf("error: call to a function '%s' that is not present in the target\n",
272 mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000273 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000274 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000275 }
276 }
277 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278 else
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000279 {
280 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
281 {
282 if (log)
283 log->Printf ("Metadataless function \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000284
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000285 if (m_error_stream)
286 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 +0000287
Greg Clayton23f8c952014-03-24 23:10:19 +0000288 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000289 }
290 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000291
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000292 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000293 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), fun_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000294
Greg Clayton23f8c952014-03-24 23:10:19 +0000295 return LookupResult::Success;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000296}
297
298llvm::Constant *
299IRForTarget::BuildFunctionPointer (llvm::Type *type,
300 uint64_t ptr)
301{
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000302 PointerType *fun_ptr_ty = PointerType::getUnqual(type);
Sean Callanan439dcae2013-12-20 19:55:02 +0000303 Constant *fun_addr_int = ConstantInt::get(m_intptr_ty, ptr, false);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000304 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
305}
306
Sean Callananfc8feb82011-10-31 22:11:40 +0000307void
308IRForTarget::RegisterFunctionMetadata(LLVMContext &context,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000309 llvm::Value *function_ptr,
Sean Callananfc8feb82011-10-31 22:11:40 +0000310 const char *name)
311{
Ed Mastea8553092014-03-10 17:24:16 +0000312 for (llvm::User *user : function_ptr->users())
Sean Callananfc8feb82011-10-31 22:11:40 +0000313 {
Sean Callananfc8feb82011-10-31 22:11:40 +0000314 if (Instruction *user_inst = dyn_cast<Instruction>(user))
315 {
Daniel Maleaf051dbc2013-06-03 20:45:54 +0000316 MDString* md_name = MDString::get(context, StringRef(name));
317
318 MDNode *metadata = MDNode::get(context, md_name);
319
Sean Callananfc8feb82011-10-31 22:11:40 +0000320 user_inst->setMetadata("lldb.call.realName", metadata);
321 }
322 else
323 {
324 RegisterFunctionMetadata (context, user, name);
325 }
326 }
327}
328
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000330IRForTarget::ResolveFunctionPointers(llvm::Module &llvm_module)
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000331{
Greg Clayton5160ce52013-03-27 23:08:40 +0000332 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000333
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000334 for (llvm::Module::iterator fi = llvm_module.begin();
335 fi != llvm_module.end();
336 ++fi)
337 {
338 Function *fun = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000339
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000340 bool is_decl = fun->isDeclaration();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000341
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000342 if (log)
Sean Callanan7f27d602011-11-19 02:54:21 +0000343 log->Printf("Examining %s function %s", (is_decl ? "declaration" : "non-declaration"), fun->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000345 if (!is_decl)
346 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000347
Sean Callanan339f6152014-03-11 19:19:16 +0000348 if (fun->use_empty())
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000349 continue; // ignore
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000350
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000351 uint64_t addr = LLDB_INVALID_ADDRESS;
352 lldb_private::ConstString name;
353 Constant **value_ptr = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000354
Greg Clayton23f8c952014-03-24 23:10:19 +0000355 LookupResult result = GetFunctionAddress(fun,
356 addr,
357 name,
358 value_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000359
Greg Clayton23f8c952014-03-24 23:10:19 +0000360 switch (result)
361 {
362 case LookupResult::Fail:
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000363 return false; // GetFunctionAddress reports its own errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000364
Greg Clayton23f8c952014-03-24 23:10:19 +0000365 case LookupResult::Ignore:
366 break; // Nothing to do
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000367
Greg Clayton23f8c952014-03-24 23:10:19 +0000368 case LookupResult::Success:
369 {
370 Constant *value = BuildFunctionPointer(fun->getFunctionType(), addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Greg Clayton23f8c952014-03-24 23:10:19 +0000372 RegisterFunctionMetadata (llvm_module.getContext(), fun, name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000373
Greg Clayton23f8c952014-03-24 23:10:19 +0000374 if (value_ptr)
375 *value_ptr = value;
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000376
Greg Clayton23f8c952014-03-24 23:10:19 +0000377 // If we are replacing a function with the nobuiltin attribute, it may
378 // be called with the builtin attribute on call sites. Remove any such
379 // attributes since it's illegal to have a builtin call to something
380 // other than a nobuiltin function.
381 if (fun->hasFnAttribute(llvm::Attribute::NoBuiltin)) {
382 llvm::Attribute builtin = llvm::Attribute::get(fun->getContext(), llvm::Attribute::Builtin);
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000383
Greg Clayton23f8c952014-03-24 23:10:19 +0000384 for (auto u : fun->users()) {
385 if (auto call = dyn_cast<CallInst>(u)) {
386 call->removeAttribute(AttributeSet::FunctionIndex, builtin);
387 }
388 }
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000389 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000390
Greg Clayton23f8c952014-03-24 23:10:19 +0000391 fun->replaceAllUsesWith(value);
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000392 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000393 break;
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000394 }
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000395 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000396
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000397 return true;
398}
399
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000400
Sean Callanan63697e52011-05-07 01:06:41 +0000401clang::NamedDecl *
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000402IRForTarget::DeclForGlobal (const GlobalValue *global_val, Module *module)
Sean Callanan63697e52011-05-07 01:06:41 +0000403{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000404 NamedMDNode *named_metadata = module->getNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000405
Sean Callanan63697e52011-05-07 01:06:41 +0000406 if (!named_metadata)
407 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408
Sean Callanan63697e52011-05-07 01:06:41 +0000409 unsigned num_nodes = named_metadata->getNumOperands();
410 unsigned node_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000411
Sean Callanan63697e52011-05-07 01:06:41 +0000412 for (node_index = 0;
413 node_index < num_nodes;
414 ++node_index)
415 {
Zachary Turner0d594e12014-11-05 18:37:53 +0000416 llvm::MDNode *metadata_node = dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
Sean Callanan63697e52011-05-07 01:06:41 +0000417 if (!metadata_node)
418 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000419
Sean Callanan63697e52011-05-07 01:06:41 +0000420 if (metadata_node->getNumOperands() != 2)
421 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000422
Zachary Turner543afa12014-12-09 22:29:47 +0000423 if (mdconst::dyn_extract_or_null<GlobalValue>(metadata_node->getOperand(0)) != global_val)
Sean Callanan63697e52011-05-07 01:06:41 +0000424 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425
Zachary Turner543afa12014-12-09 22:29:47 +0000426 ConstantInt *constant_int = mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000427
Sean Callanan63697e52011-05-07 01:06:41 +0000428 if (!constant_int)
429 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000430
Sean Callanan63697e52011-05-07 01:06:41 +0000431 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000432
Sean Callanan63697e52011-05-07 01:06:41 +0000433 return reinterpret_cast<clang::NamedDecl *>(ptr);
434 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000435
Sean Callanan63697e52011-05-07 01:06:41 +0000436 return NULL;
437}
438
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000439clang::NamedDecl *
440IRForTarget::DeclForGlobal (GlobalValue *global_val)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000441{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000442 return DeclForGlobal(global_val, m_module);
443}
444
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000445bool
Sean Callanan79763a42011-05-23 21:40:23 +0000446IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanand1e5b432010-08-12 01:56:52 +0000447{
Greg Clayton5160ce52013-03-27 23:08:40 +0000448 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Sean Callanan9e6ed532010-09-13 21:34:21 +0000450 if (!m_resolve_vars)
451 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000452
Sean Callanan9e6ed532010-09-13 21:34:21 +0000453 // Find the result variable. If it doesn't exist, we can give up right here.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000454
Sean Callanan79763a42011-05-23 21:40:23 +0000455 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000456
Sean Callanancc427fa2011-07-30 02:42:06 +0000457 std::string result_name_str;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000458 const char *result_name = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000459
Sean Callananfc55f5d2010-09-21 00:44:12 +0000460 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
461 vi != ve;
462 ++vi)
463 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000464 result_name_str = vi->first().str();
465 const char *value_name = result_name_str.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000466
Sean Callanancc427fa2011-07-30 02:42:06 +0000467 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000468 strncmp(value_name, "_ZGV", 4))
Sean Callanan92adcac2011-01-13 08:53:35 +0000469 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000470 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000471 m_result_is_pointer = true;
472 break;
473 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000474
Sean Callanancc427fa2011-07-30 02:42:06 +0000475 if (strstr(value_name, "$__lldb_expr_result") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000476 strncmp(value_name, "_ZGV", 4))
Sean Callanan46ae9e52010-09-28 21:13:03 +0000477 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000478 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000479 m_result_is_pointer = false;
Sean Callanan46ae9e52010-09-28 21:13:03 +0000480 break;
481 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000482 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000483
Sean Callananfc55f5d2010-09-21 00:44:12 +0000484 if (!result_name)
485 {
486 if (log)
487 log->PutCString("Couldn't find result variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000488
Richard Mitton00dec202013-10-11 19:44:23 +0000489 return true;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000490 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000491
Sean Callanan46ae9e52010-09-28 21:13:03 +0000492 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000493 log->Printf("Result name: \"%s\"", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000494
Sean Callanan79763a42011-05-23 21:40:23 +0000495 Value *result_value = m_module->getNamedValue(result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000496
Sean Callanand1e5b432010-08-12 01:56:52 +0000497 if (!result_value)
498 {
499 if (log)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000500 log->PutCString("Result variable had no data");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000501
Sean Callanan3989fb92011-01-27 01:07:04 +0000502 if (m_error_stream)
503 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 +0000504
Sean Callananfc55f5d2010-09-21 00:44:12 +0000505 return false;
Sean Callanand1e5b432010-08-12 01:56:52 +0000506 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000507
Sean Callanand1e5b432010-08-12 01:56:52 +0000508 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000509 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callanand1e5b432010-08-12 01:56:52 +0000511 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000512
Sean Callanand1e5b432010-08-12 01:56:52 +0000513 if (!result_global)
514 {
515 if (log)
516 log->PutCString("Result variable isn't a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000517
Sean Callanan3989fb92011-01-27 01:07:04 +0000518 if (m_error_stream)
519 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 +0000520
Sean Callanand1e5b432010-08-12 01:56:52 +0000521 return false;
522 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000523
Sean Callanan79763a42011-05-23 21:40:23 +0000524 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan63697e52011-05-07 01:06:41 +0000525 if (!result_decl)
Sean Callanand1e5b432010-08-12 01:56:52 +0000526 {
527 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000528 log->PutCString("Result variable doesn't have a corresponding Decl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000529
Sean Callanan3989fb92011-01-27 01:07:04 +0000530 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000531 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 +0000532
Sean Callanand1e5b432010-08-12 01:56:52 +0000533 return false;
534 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000535
Sean Callanan63697e52011-05-07 01:06:41 +0000536 if (log)
Sean Callanand1e5b432010-08-12 01:56:52 +0000537 {
Sean Callanan63697e52011-05-07 01:06:41 +0000538 std::string decl_desc_str;
539 raw_string_ostream decl_desc_stream(decl_desc_str);
540 result_decl->print(decl_desc_stream);
541 decl_desc_stream.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000542
Sean Callanan63697e52011-05-07 01:06:41 +0000543 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanand1e5b432010-08-12 01:56:52 +0000544 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000545
Sean Callanan63697e52011-05-07 01:06:41 +0000546 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
547 if (!result_var)
Sean Callanand1e5b432010-08-12 01:56:52 +0000548 {
549 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000550 log->PutCString("Result variable Decl isn't a VarDecl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000551
Sean Callanan3989fb92011-01-27 01:07:04 +0000552 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000553 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 +0000554
Sean Callanand1e5b432010-08-12 01:56:52 +0000555 return false;
556 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000557
Sean Callanand1e5b432010-08-12 01:56:52 +0000558 // Get the next available result name from m_decl_map and create the persistent
559 // variable for it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000560
Sean Callanan63697e52011-05-07 01:06:41 +0000561 // If the result is an Lvalue, it is emitted as a pointer; see
562 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan92adcac2011-01-13 08:53:35 +0000563 if (m_result_is_pointer)
564 {
Sean Callanan63697e52011-05-07 01:06:41 +0000565 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanan78e37602011-01-27 04:42:51 +0000566 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000567
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000568 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
569 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000570
Sean Callanan5780f9d2011-12-08 19:04:34 +0000571 if (pointer_pointertype)
572 {
573 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000574
Sean Callanan5780f9d2011-12-08 19:04:34 +0000575 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
576 &result_decl->getASTContext());
577 }
578 else if (pointer_objcobjpointertype)
579 {
580 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000581
Sean Callanan5780f9d2011-12-08 19:04:34 +0000582 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
583 &result_decl->getASTContext());
584 }
585 else
Sean Callanan92adcac2011-01-13 08:53:35 +0000586 {
587 if (log)
588 log->PutCString("Expected result to have pointer type, but it did not");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000589
Sean Callanan3989fb92011-01-27 01:07:04 +0000590 if (m_error_stream)
591 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 +0000592
Sean Callanan92adcac2011-01-13 08:53:35 +0000593 return false;
594 }
Sean Callanan92adcac2011-01-13 08:53:35 +0000595 }
596 else
597 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000598 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Sean Callanan00f43622011-11-18 03:28:09 +0000599 &result_decl->getASTContext());
600 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000601
Greg Clayton526ae042015-02-12 00:34:25 +0000602
603 lldb::TargetSP target_sp (m_data_allocator.GetTarget());
Greg Claytonf9da9282015-02-27 00:12:22 +0000604 lldb_private::ExecutionContext exe_ctx (target_sp, true);
Greg Clayton526ae042015-02-12 00:34:25 +0000605 if (m_result_type.GetBitSize(exe_ctx.GetBestExecutionContextScope()) == 0)
Sean Callanan00f43622011-11-18 03:28:09 +0000606 {
607 lldb_private::StreamString type_desc_stream;
608 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000609
Sean Callanan00f43622011-11-18 03:28:09 +0000610 if (log)
611 log->Printf("Result type has size 0");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000612
Sean Callanan00f43622011-11-18 03:28:09 +0000613 if (m_error_stream)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000614 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
Sean Callanan00f43622011-11-18 03:28:09 +0000615 type_desc_stream.GetData());
Sean Callanan960534c2011-12-21 23:44:05 +0000616 return false;
Sean Callanan92adcac2011-01-13 08:53:35 +0000617 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000618
Sean Callanan63697e52011-05-07 01:06:41 +0000619 if (log)
620 {
621 lldb_private::StreamString type_desc_stream;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000622 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000623
Sean Callanan00f43622011-11-18 03:28:09 +0000624 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
Sean Callanan63697e52011-05-07 01:06:41 +0000625 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000626
Sean Callanan1582ee62013-04-18 22:06:33 +0000627 m_result_name = lldb_private::ConstString("$RESULT_NAME");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000628
Sean Callanand1e5b432010-08-12 01:56:52 +0000629 if (log)
Greg Claytonfaac1112013-03-14 18:31:44 +0000630 log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000631 m_result_name.GetCString(),
Enrico Granata1cd5e922015-01-28 00:07:51 +0000632 m_result_type.GetByteSize(nullptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000633
Sean Callanand1e5b432010-08-12 01:56:52 +0000634 // Construct a new result global and set up its metadata
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000635
636 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanand1e5b432010-08-12 01:56:52 +0000637 result_global->getType()->getElementType(),
638 false, /* not constant */
639 GlobalValue::ExternalLinkage,
640 NULL, /* no initializer */
Sean Callanan92adcac2011-01-13 08:53:35 +0000641 m_result_name.GetCString ());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000642
Sean Callanand1e5b432010-08-12 01:56:52 +0000643 // It's too late in compilation to create a new VarDecl for this, but we don't
644 // need to. We point the metadata at the old VarDecl. This creates an odd
645 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton7b462cc2010-10-15 22:48:33 +0000646 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanand1e5b432010-08-12 01:56:52 +0000647 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
648 // fixed up.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000649
Sean Callanan79763a42011-05-23 21:40:23 +0000650 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan63697e52011-05-07 01:06:41 +0000651 reinterpret_cast<uint64_t>(result_decl),
Sean Callanand1e5b432010-08-12 01:56:52 +0000652 false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000653
Zachary Turner543afa12014-12-09 22:29:47 +0000654 llvm::Metadata *values[2];
655 values[0] = ConstantAsMetadata::get(new_result_global);
656 values[1] = ConstantAsMetadata::get(new_constant_int);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000657
Zachary Turner543afa12014-12-09 22:29:47 +0000658 ArrayRef<Metadata *> value_ref(values, 2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000659
Sean Callanan79763a42011-05-23 21:40:23 +0000660 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
661 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanand1e5b432010-08-12 01:56:52 +0000662 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000663
Sean Callanand1e5b432010-08-12 01:56:52 +0000664 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000665 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan1e87fff2010-09-07 22:43:19 +0000666 PrintValue(result_global).c_str(),
Sean Callanand1e5b432010-08-12 01:56:52 +0000667 PrintValue(new_result_global).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000668
Sean Callanan339f6152014-03-11 19:19:16 +0000669 if (result_global->use_empty())
Sean Callanan1e87fff2010-09-07 22:43:19 +0000670 {
671 // We need to synthesize a store for this variable, because otherwise
672 // there's nothing to put into its equivalent persistent variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000673
Greg Clayton7b462cc2010-10-15 22:48:33 +0000674 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000675 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000676
Sean Callanan1e87fff2010-09-07 22:43:19 +0000677 if (!first_entry_instruction)
678 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000679
Sean Callanan1e87fff2010-09-07 22:43:19 +0000680 if (!result_global->hasInitializer())
681 {
682 if (log)
683 log->Printf("Couldn't find initializer for unused variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000684
Sean Callanan3989fb92011-01-27 01:07:04 +0000685 if (m_error_stream)
686 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 +0000687
Sean Callanan1e87fff2010-09-07 22:43:19 +0000688 return false;
689 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000690
Sean Callanan1e87fff2010-09-07 22:43:19 +0000691 Constant *initializer = result_global->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000692
Greg Clayton9c139312011-02-05 02:28:58 +0000693 StoreInst *synthesized_store = new StoreInst(initializer,
694 new_result_global,
695 first_entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000696
Sean Callanan1e87fff2010-09-07 22:43:19 +0000697 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000698 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000699 }
700 else
701 {
702 result_global->replaceAllUsesWith(new_result_global);
703 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000704
Sean Callanan1582ee62013-04-18 22:06:33 +0000705 if (!m_decl_map->AddPersistentVariable(result_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000706 m_result_name,
Sean Callanan1582ee62013-04-18 22:06:33 +0000707 m_result_type,
708 true,
709 m_result_is_pointer))
710 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000711
Sean Callanand1e5b432010-08-12 01:56:52 +0000712 result_global->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000713
Sean Callanand1e5b432010-08-12 01:56:52 +0000714 return true;
715}
716
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000717bool
Sean Callanan79763a42011-05-23 21:40:23 +0000718IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000719 llvm::GlobalVariable *cstr)
Sean Callananafe16a72010-11-17 23:00:36 +0000720{
Greg Clayton5160ce52013-03-27 23:08:40 +0000721 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000722
Sean Callanancc427fa2011-07-30 02:42:06 +0000723 Type *ns_str_ty = ns_str->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000724
Sean Callanancc427fa2011-07-30 02:42:06 +0000725 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +0000726 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
727 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000728
Sean Callananafe16a72010-11-17 23:00:36 +0000729 if (!m_CFStringCreateWithBytes)
730 {
731 lldb::addr_t CFStringCreateWithBytes_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000732
Sean Callananafe16a72010-11-17 23:00:36 +0000733 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000734
Sean Callananafe16a72010-11-17 23:00:36 +0000735 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
736 {
737 if (log)
738 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000739
Sean Callanan3989fb92011-01-27 01:07:04 +0000740 if (m_error_stream)
741 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000742
Sean Callananafe16a72010-11-17 23:00:36 +0000743 return false;
744 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000745
Sean Callananafe16a72010-11-17 23:00:36 +0000746 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000747 log->Printf("Found CFStringCreateWithBytes at 0x%" PRIx64, CFStringCreateWithBytes_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000748
Sean Callananafe16a72010-11-17 23:00:36 +0000749 // Build the function type:
750 //
751 // CFStringRef CFStringCreateWithBytes (
752 // CFAllocatorRef alloc,
753 // const UInt8 *bytes,
754 // CFIndex numBytes,
755 // CFStringEncoding encoding,
756 // Boolean isExternalRepresentation
757 // );
758 //
759 // We make the following substitutions:
760 //
761 // CFStringRef -> i8*
762 // CFAllocatorRef -> i8*
763 // UInt8 * -> i8*
764 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
765 // CFStringEncoding -> i32
766 // Boolean -> i8
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000767
Sean Callanancc427fa2011-07-30 02:42:06 +0000768 Type *arg_type_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000769
Sean Callanancc427fa2011-07-30 02:42:06 +0000770 arg_type_array[0] = i8_ptr_ty;
771 arg_type_array[1] = i8_ptr_ty;
Sean Callanan439dcae2013-12-20 19:55:02 +0000772 arg_type_array[2] = m_intptr_ty;
Sean Callanancc427fa2011-07-30 02:42:06 +0000773 arg_type_array[3] = i32_ty;
774 arg_type_array[4] = i8_ty;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000775
Sean Callanancc427fa2011-07-30 02:42:06 +0000776 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000777
Sean Callanan79763a42011-05-23 21:40:23 +0000778 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000779
Sean Callananafe16a72010-11-17 23:00:36 +0000780 // Build the constant containing the pointer to the function
781 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000782 Constant *CFSCWB_addr_int = ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000783 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
784 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000785
Sean Callanand2b465f2012-02-09 03:22:41 +0000786 ConstantDataSequential *string_array = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000787
Sean Callanan229ce2d2011-02-10 22:17:53 +0000788 if (cstr)
Sean Callanand2b465f2012-02-09 03:22:41 +0000789 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000790
Sean Callananafe16a72010-11-17 23:00:36 +0000791 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000792 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000793 Constant *numBytes_arg = ConstantInt::get(m_intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000794 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
795 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000796
Sean Callanancc427fa2011-07-30 02:42:06 +0000797 Value *argument_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000798
Sean Callanancc427fa2011-07-30 02:42:06 +0000799 argument_array[0] = alloc_arg;
800 argument_array[1] = bytes_arg;
801 argument_array[2] = numBytes_arg;
802 argument_array[3] = encoding_arg;
803 argument_array[4] = isExternal_arg;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000804
Sean Callanancc427fa2011-07-30 02:42:06 +0000805 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000806
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000807 FunctionValueCache CFSCWB_Caller ([this, &CFSCWB_arguments] (llvm::Function *function)->llvm::Value * {
808 return CallInst::Create(m_CFStringCreateWithBytes,
809 CFSCWB_arguments,
810 "CFStringCreateWithBytes",
811 llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function)));
812 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000813
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000814 if (!UnfoldConstant(ns_str, CFSCWB_Caller, m_entry_instruction_finder))
Sean Callananafe16a72010-11-17 23:00:36 +0000815 {
816 if (log)
817 log->PutCString("Couldn't replace the NSString with the result of the call");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000818
Sean Callanan3989fb92011-01-27 01:07:04 +0000819 if (m_error_stream)
820 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 +0000821
Sean Callananafe16a72010-11-17 23:00:36 +0000822 return false;
823 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000824
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000825 ns_str->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000826
Sean Callananafe16a72010-11-17 23:00:36 +0000827 return true;
828}
829
830bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000831IRForTarget::RewriteObjCConstStrings()
Sean Callananafe16a72010-11-17 23:00:36 +0000832{
Greg Clayton5160ce52013-03-27 23:08:40 +0000833 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000834
Sean Callanan79763a42011-05-23 21:40:23 +0000835 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000836
Sean Callananafe16a72010-11-17 23:00:36 +0000837 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
838 vi != ve;
839 ++vi)
840 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000841 std::string value_name = vi->first().str();
842 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000843
Sean Callanancc427fa2011-07-30 02:42:06 +0000844 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callananafe16a72010-11-17 23:00:36 +0000845 {
846 Value *nsstring_value = vi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000847
Sean Callananafe16a72010-11-17 23:00:36 +0000848 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000849
Sean Callananafe16a72010-11-17 23:00:36 +0000850 if (!nsstring_global)
851 {
852 if (log)
853 log->PutCString("NSString variable is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000854
Sean Callanan3989fb92011-01-27 01:07:04 +0000855 if (m_error_stream)
856 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 +0000857
Sean Callananafe16a72010-11-17 23:00:36 +0000858 return false;
859 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000860
Sean Callananafe16a72010-11-17 23:00:36 +0000861 if (!nsstring_global->hasInitializer())
862 {
863 if (log)
864 log->PutCString("NSString variable does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000865
Sean Callanan3989fb92011-01-27 01:07:04 +0000866 if (m_error_stream)
867 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 +0000868
Sean Callananafe16a72010-11-17 23:00:36 +0000869 return false;
870 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000871
Sean Callananafe16a72010-11-17 23:00:36 +0000872 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000873
Sean Callananafe16a72010-11-17 23:00:36 +0000874 if (!nsstring_struct)
875 {
876 if (log)
877 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000878
Sean Callanan3989fb92011-01-27 01:07:04 +0000879 if (m_error_stream)
880 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 +0000881
Sean Callananafe16a72010-11-17 23:00:36 +0000882 return false;
883 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000884
Sean Callananafe16a72010-11-17 23:00:36 +0000885 // We expect the following structure:
886 //
887 // struct {
888 // int *isa;
889 // int flags;
890 // char *str;
891 // long length;
892 // };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Sean Callananafe16a72010-11-17 23:00:36 +0000894 if (nsstring_struct->getNumOperands() != 4)
895 {
896 if (log)
897 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 +0000898
Sean Callanan3989fb92011-01-27 01:07:04 +0000899 if (m_error_stream)
900 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 +0000901
Sean Callananafe16a72010-11-17 23:00:36 +0000902 return false;
903 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000904
Sean Callananafe16a72010-11-17 23:00:36 +0000905 Constant *nsstring_member = nsstring_struct->getOperand(2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000906
Sean Callananafe16a72010-11-17 23:00:36 +0000907 if (!nsstring_member)
908 {
909 if (log)
910 log->PutCString("NSString initializer's str element was empty");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000911
Sean Callanan3989fb92011-01-27 01:07:04 +0000912 if (m_error_stream)
913 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 +0000914
Sean Callananafe16a72010-11-17 23:00:36 +0000915 return false;
916 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000917
Sean Callananafe16a72010-11-17 23:00:36 +0000918 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000919
Sean Callananafe16a72010-11-17 23:00:36 +0000920 if (!nsstring_expr)
921 {
922 if (log)
923 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000924
Sean Callanan3989fb92011-01-27 01:07:04 +0000925 if (m_error_stream)
926 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 +0000927
Sean Callananafe16a72010-11-17 23:00:36 +0000928 return false;
929 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000930
Sean Callananafe16a72010-11-17 23:00:36 +0000931 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
932 {
933 if (log)
934 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 +0000935
Sean Callanan3989fb92011-01-27 01:07:04 +0000936 if (m_error_stream)
937 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 +0000938
Sean Callananafe16a72010-11-17 23:00:36 +0000939 return false;
940 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000941
Sean Callananafe16a72010-11-17 23:00:36 +0000942 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000943
Sean Callananafe16a72010-11-17 23:00:36 +0000944 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000945
Sean Callananafe16a72010-11-17 23:00:36 +0000946 if (!cstr_global)
947 {
948 if (log)
949 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000950
Sean Callanan3989fb92011-01-27 01:07:04 +0000951 if (m_error_stream)
952 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 +0000953
Sean Callananafe16a72010-11-17 23:00:36 +0000954 return false;
955 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000956
Sean Callananafe16a72010-11-17 23:00:36 +0000957 if (!cstr_global->hasInitializer())
958 {
959 if (log)
960 log->PutCString("NSString initializer's str element does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000961
Sean Callanan3989fb92011-01-27 01:07:04 +0000962 if (m_error_stream)
963 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 +0000964
Sean Callananafe16a72010-11-17 23:00:36 +0000965 return false;
966 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000967
Sean Callanan229ce2d2011-02-10 22:17:53 +0000968 /*
Sean Callananafe16a72010-11-17 23:00:36 +0000969 if (!cstr_array)
970 {
971 if (log)
972 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000973
Sean Callanan3989fb92011-01-27 01:07:04 +0000974 if (m_error_stream)
975 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 +0000976
Sean Callananafe16a72010-11-17 23:00:36 +0000977 return false;
978 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000979
Sean Callananafe16a72010-11-17 23:00:36 +0000980 if (!cstr_array->isCString())
981 {
982 if (log)
983 log->PutCString("NSString initializer's str element is not a C string array");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000984
Sean Callanan3989fb92011-01-27 01:07:04 +0000985 if (m_error_stream)
986 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 +0000987
Sean Callananafe16a72010-11-17 23:00:36 +0000988 return false;
989 }
Sean Callanan229ce2d2011-02-10 22:17:53 +0000990 */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000991
Sean Callanand2b465f2012-02-09 03:22:41 +0000992 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000993
Sean Callananafe16a72010-11-17 23:00:36 +0000994 if (log)
Sean Callanan229ce2d2011-02-10 22:17:53 +0000995 {
996 if (cstr_array)
Sean Callanand2b465f2012-02-09 03:22:41 +0000997 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 +0000998 else
Sean Callanancc427fa2011-07-30 02:42:06 +0000999 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan229ce2d2011-02-10 22:17:53 +00001000 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001001
Sean Callanan229ce2d2011-02-10 22:17:53 +00001002 if (!cstr_array)
1003 cstr_global = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001004
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001005 if (!RewriteObjCConstString(nsstring_global, cstr_global))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001006 {
Sean Callananafe16a72010-11-17 23:00:36 +00001007 if (log)
1008 log->PutCString("Error rewriting the constant string");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001009
Sean Callanan3989fb92011-01-27 01:07:04 +00001010 // We don't print an error message here because RewriteObjCConstString has done so for us.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001011
Sean Callananafe16a72010-11-17 23:00:36 +00001012 return false;
1013 }
Sean Callananafe16a72010-11-17 23:00:36 +00001014 }
1015 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001016
Sean Callananafe16a72010-11-17 23:00:36 +00001017 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1018 vi != ve;
1019 ++vi)
1020 {
Sean Callanancc427fa2011-07-30 02:42:06 +00001021 std::string value_name = vi->first().str();
1022 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001023
Sean Callanancc427fa2011-07-30 02:42:06 +00001024 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callananafe16a72010-11-17 23:00:36 +00001025 {
1026 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001027
Sean Callananafe16a72010-11-17 23:00:36 +00001028 if (!gv)
1029 {
1030 if (log)
1031 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001032
Sean Callanan3989fb92011-01-27 01:07:04 +00001033 if (m_error_stream)
1034 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 +00001035
Sean Callananafe16a72010-11-17 23:00:36 +00001036 return false;
1037 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001038
Sean Callananafe16a72010-11-17 23:00:36 +00001039 gv->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001040
Sean Callananafe16a72010-11-17 23:00:36 +00001041 break;
1042 }
1043 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001044
Sean Callananafe16a72010-11-17 23:00:36 +00001045 return true;
1046}
1047
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001048static bool IsObjCSelectorRef (Value *value)
Sean Callanan5300d372010-07-31 01:32:05 +00001049{
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001050 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001051
Greg Claytonbd549162014-11-10 21:45:59 +00001052 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("OBJC_SELECTOR_REFERENCES_"))
Sean Callanan5300d372010-07-31 01:32:05 +00001053 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001054
Sean Callanan5300d372010-07-31 01:32:05 +00001055 return true;
1056}
1057
Sean Callanan3989fb92011-01-27 01:07:04 +00001058// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001059bool
Sean Callanan79763a42011-05-23 21:40:23 +00001060IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callanan5300d372010-07-31 01:32:05 +00001061{
Greg Clayton5160ce52013-03-27 23:08:40 +00001062 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001063
1064 LoadInst *load = dyn_cast<LoadInst>(selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001065
Sean Callanan5300d372010-07-31 01:32:05 +00001066 if (!load)
1067 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001068
Sean Callanan5300d372010-07-31 01:32:05 +00001069 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1070 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +00001071 // %tmp = load i8** @"OBJC_SELECTOR_REFERENCES_" ; <i8*>
Sean Callanan5300d372010-07-31 01:32:05 +00001072 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1073 //
1074 // where %obj is the object pointer and %tmp is the selector.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001075 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +00001076 // @"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 +00001077 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001078
Sean Callanan5300d372010-07-31 01:32:05 +00001079 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001080
Sean Callanan5300d372010-07-31 01:32:05 +00001081 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001082
Sean Callanan5300d372010-07-31 01:32:05 +00001083 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1084 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001085
Sean Callanan5300d372010-07-31 01:32:05 +00001086 Constant *osr_initializer = _objc_selector_references_->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001087
Sean Callanan5300d372010-07-31 01:32:05 +00001088 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001089
Sean Callanan5300d372010-07-31 01:32:05 +00001090 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1091 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001092
Sean Callanan5300d372010-07-31 01:32:05 +00001093 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1094
1095 if (!osr_initializer_base)
1096 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001097
Sean Callanan5300d372010-07-31 01:32:05 +00001098 // Find the string's initializer (a ConstantArray) and get the string from it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001099
Sean Callanan5300d372010-07-31 01:32:05 +00001100 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001101
Sean Callanan5300d372010-07-31 01:32:05 +00001102 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1103 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001104
Sean Callanan5300d372010-07-31 01:32:05 +00001105 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1106
Sean Callanand2b465f2012-02-09 03:22:41 +00001107 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001108
Sean Callanan5300d372010-07-31 01:32:05 +00001109 if (!omvn_initializer_array->isString())
1110 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001111
Sean Callanan5300d372010-07-31 01:32:05 +00001112 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001113
Sean Callanan5300d372010-07-31 01:32:05 +00001114 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001115 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001116
Sean Callanan5300d372010-07-31 01:32:05 +00001117 // Construct a call to sel_registerName
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001118
Sean Callanan5300d372010-07-31 01:32:05 +00001119 if (!m_sel_registerName)
1120 {
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001121 lldb::addr_t sel_registerName_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001122
Greg Clayton7b462cc2010-10-15 22:48:33 +00001123 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001124 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callanan5300d372010-07-31 01:32:05 +00001125 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001126
Sean Callananbe3a1b12010-10-26 00:31:56 +00001127 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001128 log->Printf("Found sel_registerName at 0x%" PRIx64, sel_registerName_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001129
Sean Callanan5300d372010-07-31 01:32:05 +00001130 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001131
Sean Callanan5300d372010-07-31 01:32:05 +00001132 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callanan79763a42011-05-23 21:40:23 +00001133 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callanan5300d372010-07-31 01:32:05 +00001134 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanancc427fa2011-07-30 02:42:06 +00001135 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001136
Sean Callanancc427fa2011-07-30 02:42:06 +00001137 Type *type_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001138
Sean Callanancc427fa2011-07-30 02:42:06 +00001139 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001140
Sean Callanancc427fa2011-07-30 02:42:06 +00001141 ArrayRef<Type *> srN_arg_types(type_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001142
Sean Callanan5300d372010-07-31 01:32:05 +00001143 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001144
Sean Callanan5300d372010-07-31 01:32:05 +00001145 // Build the constant containing the pointer to the function
Sean Callanan5300d372010-07-31 01:32:05 +00001146 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Sean Callanan439dcae2013-12-20 19:55:02 +00001147 Constant *srN_addr_int = ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
Sean Callanan5300d372010-07-31 01:32:05 +00001148 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1149 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001150
Sean Callanancc427fa2011-07-30 02:42:06 +00001151 Value *argument_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001152
Sean Callanan79763a42011-05-23 21:40:23 +00001153 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001154
Sean Callanancc427fa2011-07-30 02:42:06 +00001155 argument_array[0] = omvn_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001156
Sean Callanancc427fa2011-07-30 02:42:06 +00001157 ArrayRef<Value *> srN_arguments(argument_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001158
1159 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanancc427fa2011-07-30 02:42:06 +00001160 srN_arguments,
Sean Callananafe16a72010-11-17 23:00:36 +00001161 "sel_registerName",
Sean Callanan5300d372010-07-31 01:32:05 +00001162 selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001163
Sean Callanan5300d372010-07-31 01:32:05 +00001164 // Replace the load with the call in all users
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001165
Sean Callanan5300d372010-07-31 01:32:05 +00001166 selector_load->replaceAllUsesWith(srN_call);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001167
Sean Callanan5300d372010-07-31 01:32:05 +00001168 selector_load->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001169
Sean Callanan5300d372010-07-31 01:32:05 +00001170 return true;
1171}
1172
1173bool
Sean Callanan79763a42011-05-23 21:40:23 +00001174IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callanan5300d372010-07-31 01:32:05 +00001175{
Greg Clayton5160ce52013-03-27 23:08:40 +00001176 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001177
1178 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001179
Sean Callanan5300d372010-07-31 01:32:05 +00001180 typedef SmallVector <Instruction*, 2> InstrList;
1181 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001182
Sean Callanan5300d372010-07-31 01:32:05 +00001183 InstrList selector_loads;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001184
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001185 for (ii = basic_block.begin();
1186 ii != basic_block.end();
Sean Callanan5300d372010-07-31 01:32:05 +00001187 ++ii)
1188 {
1189 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001190
Sean Callanan5300d372010-07-31 01:32:05 +00001191 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001192 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callanan5300d372010-07-31 01:32:05 +00001193 selector_loads.push_back(&inst);
1194 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001195
Sean Callanan5300d372010-07-31 01:32:05 +00001196 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001197
Sean Callanan5300d372010-07-31 01:32:05 +00001198 for (iter = selector_loads.begin();
1199 iter != selector_loads.end();
1200 ++iter)
1201 {
Sean Callanan79763a42011-05-23 21:40:23 +00001202 if (!RewriteObjCSelector(*iter))
Sean Callanan5300d372010-07-31 01:32:05 +00001203 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001204 if (m_error_stream)
1205 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 +00001206
Enrico Granata20edcdb2011-07-19 18:03:25 +00001207 if (log)
Sean Callanan5300d372010-07-31 01:32:05 +00001208 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001209
Sean Callanan5300d372010-07-31 01:32:05 +00001210 return false;
1211 }
1212 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001213
Sean Callanan5300d372010-07-31 01:32:05 +00001214 return true;
1215}
1216
Sean Callanan3989fb92011-01-27 01:07:04 +00001217// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001218bool
Sean Callanan79763a42011-05-23 21:40:23 +00001219IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanan2235f322010-08-11 03:57:18 +00001220{
Greg Clayton5160ce52013-03-27 23:08:40 +00001221 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane1175b72011-01-13 21:23:32 +00001222
Sean Callanan2235f322010-08-11 03:57:18 +00001223 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001224
Duncan P. N. Exon Smith68caa7d2014-11-12 01:59:53 +00001225 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
Sean Callanan2235f322010-08-11 03:57:18 +00001226
1227 if (!alloc_md || !alloc_md->getNumOperands())
1228 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001229
Zachary Turner543afa12014-12-09 22:29:47 +00001230 ConstantInt *constant_int = mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001231
Sean Callanan2235f322010-08-11 03:57:18 +00001232 if (!constant_int)
1233 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001234
Sean Callanan2235f322010-08-11 03:57:18 +00001235 // We attempt to register this as a new persistent variable with the DeclMap.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001236
Sean Callanan2235f322010-08-11 03:57:18 +00001237 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001238
Sean Callanand1e5b432010-08-12 01:56:52 +00001239 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001240
Sean Callanand1e5b432010-08-12 01:56:52 +00001241 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1242 &decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001243
Greg Clayton7b462cc2010-10-15 22:48:33 +00001244 StringRef decl_name (decl->getName());
1245 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan92adcac2011-01-13 08:53:35 +00001246 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanan2235f322010-08-11 03:57:18 +00001247 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001248
Sean Callanan79763a42011-05-23 21:40:23 +00001249 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanane1175b72011-01-13 21:23:32 +00001250 alloc->getType(),
Sean Callanan2235f322010-08-11 03:57:18 +00001251 false, /* not constant */
1252 GlobalValue::ExternalLinkage,
1253 NULL, /* no initializer */
1254 alloc->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001255
Sean Callanan2235f322010-08-11 03:57:18 +00001256 // What we're going to do here is make believe this was a regular old external
1257 // variable. That means we need to make the metadata valid.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001258
Sean Callanan585c0ec82012-07-04 01:26:26 +00001259 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001260
Zachary Turner543afa12014-12-09 22:29:47 +00001261 llvm::Metadata *values[2];
1262 values[0] = ConstantAsMetadata::get(persistent_global);
1263 values[1] = ConstantAsMetadata::get(constant_int);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001264
Zachary Turner543afa12014-12-09 22:29:47 +00001265 ArrayRef<llvm::Metadata *> value_ref(values, 2);
Sean Callanan2235f322010-08-11 03:57:18 +00001266
Sean Callanan79763a42011-05-23 21:40:23 +00001267 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanan2235f322010-08-11 03:57:18 +00001268 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001269
Sean Callanane1175b72011-01-13 21:23:32 +00001270 // Now, since the variable is a pointer variable, we will drop in a load of that
1271 // pointer variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001272
Sean Callanane1175b72011-01-13 21:23:32 +00001273 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001274
Sean Callanane1175b72011-01-13 21:23:32 +00001275 if (log)
1276 log->Printf("Replacing \"%s\" with \"%s\"",
1277 PrintValue(alloc).c_str(),
1278 PrintValue(persistent_load).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001279
Sean Callanane1175b72011-01-13 21:23:32 +00001280 alloc->replaceAllUsesWith(persistent_load);
Sean Callanan2235f322010-08-11 03:57:18 +00001281 alloc->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001282
Sean Callanan2235f322010-08-11 03:57:18 +00001283 return true;
1284}
1285
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001286bool
Sean Callanan79763a42011-05-23 21:40:23 +00001287IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanan2235f322010-08-11 03:57:18 +00001288{
Sean Callanan9e6ed532010-09-13 21:34:21 +00001289 if (!m_resolve_vars)
1290 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001291
Greg Clayton5160ce52013-03-27 23:08:40 +00001292 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001293
Sean Callanan2235f322010-08-11 03:57:18 +00001294 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001295
Sean Callanan2235f322010-08-11 03:57:18 +00001296 typedef SmallVector <Instruction*, 2> InstrList;
1297 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001298
Sean Callanan2235f322010-08-11 03:57:18 +00001299 InstrList pvar_allocs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001300
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001301 for (ii = basic_block.begin();
1302 ii != basic_block.end();
Sean Callanan2235f322010-08-11 03:57:18 +00001303 ++ii)
1304 {
1305 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001306
Sean Callanan2235f322010-08-11 03:57:18 +00001307 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callananf694a552011-01-21 22:30:25 +00001308 {
1309 llvm::StringRef alloc_name = alloc->getName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001310
Sean Callananf694a552011-01-21 22:30:25 +00001311 if (alloc_name.startswith("$") &&
1312 !alloc_name.startswith("$__lldb"))
1313 {
1314 if (alloc_name.find_first_of("0123456789") == 1)
1315 {
1316 if (log)
1317 log->Printf("Rejecting a numeric persistent variable.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001318
Sean Callanan3989fb92011-01-27 01:07:04 +00001319 if (m_error_stream)
1320 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 +00001321
Sean Callananf694a552011-01-21 22:30:25 +00001322 return false;
1323 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001324
Sean Callanan2235f322010-08-11 03:57:18 +00001325 pvar_allocs.push_back(alloc);
Sean Callananf694a552011-01-21 22:30:25 +00001326 }
1327 }
Sean Callanan2235f322010-08-11 03:57:18 +00001328 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001329
Sean Callanan2235f322010-08-11 03:57:18 +00001330 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001331
Sean Callanan2235f322010-08-11 03:57:18 +00001332 for (iter = pvar_allocs.begin();
1333 iter != pvar_allocs.end();
1334 ++iter)
1335 {
Sean Callanan79763a42011-05-23 21:40:23 +00001336 if (!RewritePersistentAlloc(*iter))
Sean Callanan2235f322010-08-11 03:57:18 +00001337 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001338 if (m_error_stream)
1339 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001340
Enrico Granata20edcdb2011-07-19 18:03:25 +00001341 if (log)
Sean Callanan2235f322010-08-11 03:57:18 +00001342 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001343
Sean Callanan2235f322010-08-11 03:57:18 +00001344 return false;
1345 }
1346 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001347
Sean Callanan2235f322010-08-11 03:57:18 +00001348 return true;
1349}
1350
Sean Callananc70ed462011-10-25 18:36:40 +00001351bool
1352IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1353{
1354 if (!initializer)
1355 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001356
Greg Clayton5160ce52013-03-27 23:08:40 +00001357 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc70ed462011-10-25 18:36:40 +00001358
1359 if (log && log->GetVerbose())
1360 log->Printf(" MaterializeInitializer(%p, %s)", data, PrintValue(initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001361
Sean Callananc70ed462011-10-25 18:36:40 +00001362 Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001363
Sean Callananc70ed462011-10-25 18:36:40 +00001364 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1365 {
1366 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1367 return true;
1368 }
Sean Callanand2b465f2012-02-09 03:22:41 +00001369 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
Sean Callananc70ed462011-10-25 18:36:40 +00001370 {
1371 if (array_initializer->isString())
1372 {
1373 std::string array_initializer_string = array_initializer->getAsString();
1374 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1375 }
1376 else
1377 {
1378 ArrayType *array_initializer_type = array_initializer->getType();
1379 Type *array_element_type = array_initializer_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001380
Sean Callananc70ed462011-10-25 18:36:40 +00001381 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001382
Andy Gibbsa297a972013-06-19 19:04:53 +00001383 for (unsigned i = 0; i < array_initializer->getNumOperands(); ++i)
Sean Callananc70ed462011-10-25 18:36:40 +00001384 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001385 Value *operand_value = array_initializer->getOperand(i);
1386 Constant *operand_constant = dyn_cast<Constant>(operand_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001387
Sean Callanand2b465f2012-02-09 03:22:41 +00001388 if (!operand_constant)
1389 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001390
Sean Callanand2b465f2012-02-09 03:22:41 +00001391 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
Sean Callananc70ed462011-10-25 18:36:40 +00001392 return false;
1393 }
1394 }
1395 return true;
1396 }
1397 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1398 {
1399 StructType *struct_initializer_type = struct_initializer->getType();
1400 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1401
Andy Gibbsa297a972013-06-19 19:04:53 +00001402 for (unsigned i = 0;
Sean Callananc70ed462011-10-25 18:36:40 +00001403 i < struct_initializer->getNumOperands();
1404 ++i)
1405 {
1406 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1407 return false;
1408 }
1409 return true;
1410 }
Sean Callanan76ee3e72013-04-24 19:50:12 +00001411 else if (isa<ConstantAggregateZero>(initializer))
1412 {
1413 memset(data, 0, m_target_data->getTypeStoreSize(initializer_type));
1414 return true;
1415 }
Sean Callananc70ed462011-10-25 18:36:40 +00001416 return false;
1417}
1418
1419bool
1420IRForTarget::MaterializeInternalVariable (GlobalVariable *global_variable)
1421{
1422 if (GlobalVariable::isExternalLinkage(global_variable->getLinkage()))
1423 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001424
Sean Callananfe5d1392011-11-15 19:13:54 +00001425 if (global_variable == m_reloc_placeholder)
1426 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001427
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001428 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001429
Sean Callananc70ed462011-10-25 18:36:40 +00001430 llvm::Type *variable_type = global_variable->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001431
Sean Callananc70ed462011-10-25 18:36:40 +00001432 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001433
Sean Callananc70ed462011-10-25 18:36:40 +00001434 llvm::Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001435
Sean Callananc70ed462011-10-25 18:36:40 +00001436 size_t size = m_target_data->getTypeAllocSize(initializer_type);
Sean Callanane3333d62012-06-08 22:20:41 +00001437 size_t align = m_target_data->getPrefTypeAlignment(initializer_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001438
Sean Callanane3333d62012-06-08 22:20:41 +00001439 const size_t mask = (align - 1);
1440 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001441 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sean Callanane3333d62012-06-08 22:20:41 +00001442 offset = aligned_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001443
Sean Callananc70ed462011-10-25 18:36:40 +00001444 lldb_private::DataBufferHeap data(size, '\0');
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001445
Sean Callananc70ed462011-10-25 18:36:40 +00001446 if (initializer)
1447 if (!MaterializeInitializer(data.GetBytes(), initializer))
1448 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001449
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001450 m_data_allocator.GetStream().Write(data.GetBytes(), data.GetByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001451
Sean Callananc70ed462011-10-25 18:36:40 +00001452 Constant *new_pointer = BuildRelocation(variable_type, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001453
Sean Callananc70ed462011-10-25 18:36:40 +00001454 global_variable->replaceAllUsesWith(new_pointer);
1455
1456 global_variable->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001457
Sean Callananc70ed462011-10-25 18:36:40 +00001458 return true;
1459}
1460
Sean Callanan3989fb92011-01-27 01:07:04 +00001461// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001462bool
Sean Callanan79763a42011-05-23 21:40:23 +00001463IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001464{
Greg Clayton5160ce52013-03-27 23:08:40 +00001465 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001466
Sean Callanand7a1ca22010-12-02 19:47:57 +00001467 if (log)
Sean Callanan88339f02010-12-06 22:16:55 +00001468 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001469
Greg Clayton7b462cc2010-10-15 22:48:33 +00001470 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callanan4cf04d22010-08-03 00:23:29 +00001471 {
Sean Callanan5666b672010-08-04 01:02:13 +00001472 switch (constant_expr->getOpcode())
Sean Callanan4cf04d22010-08-03 00:23:29 +00001473 {
Sean Callanan5666b672010-08-04 01:02:13 +00001474 default:
1475 break;
1476 case Instruction::GetElementPtr:
1477 case Instruction::BitCast:
Sean Callanan4cf04d22010-08-03 00:23:29 +00001478 Value *s = constant_expr->getOperand(0);
Sean Callanan79763a42011-05-23 21:40:23 +00001479 if (!MaybeHandleVariable(s))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001480 return false;
Sean Callanan4cf04d22010-08-03 00:23:29 +00001481 }
1482 }
Sean Callanand6e04ae2010-12-03 19:51:05 +00001483 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001484 {
Sean Callananc70ed462011-10-25 18:36:40 +00001485 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1486 return MaterializeInternalVariable(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001487
Sean Callanan79763a42011-05-23 21:40:23 +00001488 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001489
Sean Callanan5300d372010-07-31 01:32:05 +00001490 if (!named_decl)
1491 {
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001492 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001493 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001494
Sean Callanan14f0b0e2010-12-06 00:56:39 +00001495 if (!global_variable->hasExternalLinkage())
1496 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001497
Sean Callanan5300d372010-07-31 01:32:05 +00001498 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001499 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001500
Sean Callanan5300d372010-07-31 01:32:05 +00001501 return false;
1502 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001503
Greg Clayton7b462cc2010-10-15 22:48:33 +00001504 std::string name (named_decl->getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001505
Greg Clayton57ee3062013-07-11 22:46:58 +00001506 clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1507 if (value_decl == NULL)
Sean Callananea22d422010-07-16 00:09:46 +00001508 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001509
1510 lldb_private::ClangASTType clang_type(&value_decl->getASTContext(), value_decl->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001511
Sean Callanan77eaf442011-07-08 00:39:14 +00001512 const Type *value_type = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001513
Sean Callanane1175b72011-01-13 21:23:32 +00001514 if (name[0] == '$')
Sean Callanan92adcac2011-01-13 08:53:35 +00001515 {
1516 // The $__lldb_expr_result name indicates the the return value has allocated as
1517 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1518 // accesses to this static variable need to be redirected to the result of dereferencing
1519 // a pointer that is passed in as one of the arguments.
1520 //
1521 // Consequently, when reporting the size of the type, we report a pointer type pointing
1522 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanane1175b72011-01-13 21:23:32 +00001523 //
1524 // We also do this for any user-declared persistent variables.
Greg Clayton57ee3062013-07-11 22:46:58 +00001525 clang_type = clang_type.GetPointerType();
Sean Callanan92adcac2011-01-13 08:53:35 +00001526 value_type = PointerType::get(global_variable->getType(), 0);
1527 }
1528 else
1529 {
Sean Callanan92adcac2011-01-13 08:53:35 +00001530 value_type = global_variable->getType();
1531 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001532
Enrico Granata1cd5e922015-01-28 00:07:51 +00001533 const uint64_t value_size = clang_type.GetByteSize(nullptr);
Zachary Turnera746e8e2014-07-02 17:24:07 +00001534 lldb::offset_t value_alignment = (clang_type.GetTypeBitAlign() + 7ull) / 8ull;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001535
Sean Callanan038df5032010-09-30 21:18:25 +00001536 if (log)
Greg Clayton57ee3062013-07-11 22:46:58 +00001537 {
Zachary Turnera746e8e2014-07-02 17:24:07 +00001538 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %" PRIu64 ", align %" PRIu64 "]",
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001539 name.c_str(),
Greg Clayton57ee3062013-07-11 22:46:58 +00001540 clang_type.GetQualType().getAsString().c_str(),
1541 PrintType(value_type).c_str(),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001542 value_size,
Sean Callanan038df5032010-09-30 21:18:25 +00001543 value_alignment);
Greg Clayton57ee3062013-07-11 22:46:58 +00001544 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001545
1546
Sean Callanan64dfc9a2010-08-23 23:09:38 +00001547 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001548 lldb_private::ConstString (name.c_str()),
1549 llvm_value_ptr,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001550 value_size,
Sean Callanan4edba2d2010-07-27 02:07:53 +00001551 value_alignment))
Sean Callananaf8e96c2011-08-01 17:41:38 +00001552 {
1553 if (!global_variable->hasExternalLinkage())
1554 return true;
Sean Callanan0ff3bf92013-04-11 17:57:16 +00001555 else if (HandleSymbol (global_variable))
1556 return true;
Sean Callananaf8e96c2011-08-01 17:41:38 +00001557 else
1558 return false;
1559 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001560 }
Sean Callanan4a5fcbb2010-12-03 03:02:31 +00001561 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001562 {
1563 if (log)
1564 log->Printf("Function pointers aren't handled right now");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001565
Sean Callanand7a1ca22010-12-02 19:47:57 +00001566 return false;
1567 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001568
Sean Callanan549c9f72010-07-13 21:41:46 +00001569 return true;
1570}
1571
Sean Callanan3989fb92011-01-27 01:07:04 +00001572// This function does not report errors; its callers are responsible.
Sean Callanan549c9f72010-07-13 21:41:46 +00001573bool
Sean Callanan79763a42011-05-23 21:40:23 +00001574IRForTarget::HandleSymbol (Value *symbol)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001575{
Greg Clayton5160ce52013-03-27 23:08:40 +00001576 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001577
Sean Callananc3a16002011-01-17 23:42:46 +00001578 lldb_private::ConstString name(symbol->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001579
Sean Callanan947ccc72011-12-01 02:04:16 +00001580 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001581
Greg Clayton084db102011-06-23 04:25:29 +00001582 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc3a16002011-01-17 23:42:46 +00001583 {
1584 if (log)
1585 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001586
Sean Callananc3a16002011-01-17 23:42:46 +00001587 return false;
1588 }
1589
1590 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001591 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001592
Sean Callanancc427fa2011-07-30 02:42:06 +00001593 Type *symbol_type = symbol->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001594
Sean Callanan439dcae2013-12-20 19:55:02 +00001595 Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001596
Sean Callananc3a16002011-01-17 23:42:46 +00001597 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001598
Sean Callananc3a16002011-01-17 23:42:46 +00001599 if (log)
1600 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001601
Sean Callananc3a16002011-01-17 23:42:46 +00001602 symbol->replaceAllUsesWith(symbol_addr_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001603
Sean Callananc3a16002011-01-17 23:42:46 +00001604 return true;
1605}
1606
1607bool
Sean Callanan79763a42011-05-23 21:40:23 +00001608IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanan85a0a832010-10-05 22:26:43 +00001609{
Greg Clayton5160ce52013-03-27 23:08:40 +00001610 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001611
Sean Callanand7a1ca22010-12-02 19:47:57 +00001612 if (log)
1613 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001614
Sean Callananafe16a72010-11-17 23:00:36 +00001615 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanan85a0a832010-10-05 22:26:43 +00001616 op_index < num_ops;
1617 ++op_index)
Sean Callanan79763a42011-05-23 21:40:23 +00001618 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan3989fb92011-01-27 01:07:04 +00001619 {
1620 if (m_error_stream)
1621 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 +00001622
Sean Callanan85a0a832010-10-05 22:26:43 +00001623 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001624 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001625
Sean Callanan85a0a832010-10-05 22:26:43 +00001626 return true;
1627}
1628
1629bool
Sean Callananfc89c142011-11-01 23:38:03 +00001630IRForTarget::HandleObjCClass(Value *classlist_reference)
1631{
Greg Clayton5160ce52013-03-27 23:08:40 +00001632 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc89c142011-11-01 23:38:03 +00001633
1634 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001635
Sean Callananfc89c142011-11-01 23:38:03 +00001636 if (!global_variable)
1637 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001638
Sean Callananfc89c142011-11-01 23:38:03 +00001639 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001640
Sean Callananfc89c142011-11-01 23:38:03 +00001641 if (!initializer)
1642 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001643
Sean Callananfc89c142011-11-01 23:38:03 +00001644 if (!initializer->hasName())
1645 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001646
Sean Callananfc89c142011-11-01 23:38:03 +00001647 StringRef name(initializer->getName());
1648 lldb_private::ConstString name_cstr(name.str().c_str());
Greg Clayton1075aca2011-12-03 20:02:42 +00001649 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001650
Sean Callananfc89c142011-11-01 23:38:03 +00001651 if (log)
1652 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 +00001653
Sean Callananfc89c142011-11-01 23:38:03 +00001654 if (class_ptr == LLDB_INVALID_ADDRESS)
1655 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001656
Ed Mastea8553092014-03-10 17:24:16 +00001657 if (global_variable->use_empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001658 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001659
Sean Callanan719a4d52013-03-23 01:01:16 +00001660 SmallVector<LoadInst *, 2> load_instructions;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001661
Ed Mastea8553092014-03-10 17:24:16 +00001662 for (llvm::User *u : global_variable->users())
Sean Callananfc89c142011-11-01 23:38:03 +00001663 {
Ed Mastea8553092014-03-10 17:24:16 +00001664 if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
Sean Callanan719a4d52013-03-23 01:01:16 +00001665 load_instructions.push_back(load_instruction);
Sean Callananfc89c142011-11-01 23:38:03 +00001666 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001667
Sean Callanan719a4d52013-03-23 01:01:16 +00001668 if (load_instructions.empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001669 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001670
Sean Callanan439dcae2013-12-20 19:55:02 +00001671 Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001672
Sean Callanan719a4d52013-03-23 01:01:16 +00001673 for (LoadInst *load_instruction : load_instructions)
1674 {
1675 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001676
Sean Callanan719a4d52013-03-23 01:01:16 +00001677 load_instruction->replaceAllUsesWith(class_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001678
Sean Callanan719a4d52013-03-23 01:01:16 +00001679 load_instruction->eraseFromParent();
1680 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001681
Sean Callananfc89c142011-11-01 23:38:03 +00001682 return true;
1683}
1684
1685bool
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001686IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1687{
1688 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001689
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001690 std::vector<CallInst *> calls_to_remove;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001691
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001692 for (ii = basic_block.begin();
1693 ii != basic_block.end();
1694 ++ii)
1695 {
1696 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001697
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001698 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001699
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001700 // MaybeHandleCallArguments handles error reporting; we are silent here
1701 if (!call)
1702 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001703
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001704 bool remove = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001705
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001706 llvm::Function *func = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001707
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001708 if (func && func->getName() == "__cxa_atexit")
1709 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001710
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001711 llvm::Value *val = call->getCalledValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001712
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001713 if (val && val->getName() == "__cxa_atexit")
1714 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001715
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001716 if (remove)
1717 calls_to_remove.push_back(call);
1718 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001719
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001720 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1721 ci != ce;
1722 ++ci)
1723 {
1724 (*ci)->eraseFromParent();
1725 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001726
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001727 return true;
1728}
1729
1730bool
Sean Callanan79763a42011-05-23 21:40:23 +00001731IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001732{
Sean Callanan2ab712f22010-07-03 01:35:46 +00001733 /////////////////////////////////////////////////////////////////////////
1734 // Prepare the current basic block for execution in the remote process
1735 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001736
Sean Callanan7ea35012010-07-27 21:39:39 +00001737 BasicBlock::iterator ii;
Sean Callanan549c9f72010-07-13 21:41:46 +00001738
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001739 for (ii = basic_block.begin();
1740 ii != basic_block.end();
Sean Callanan549c9f72010-07-13 21:41:46 +00001741 ++ii)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001742 {
Sean Callanan549c9f72010-07-13 21:41:46 +00001743 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001744
Sean Callanana4e55172010-11-08 00:31:32 +00001745 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001746
Sean Callanan3989fb92011-01-27 01:07:04 +00001747 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callanan79763a42011-05-23 21:40:23 +00001748 if (call && !MaybeHandleCallArguments(call))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001749 return false;
Sean Callanan2ab712f22010-07-03 01:35:46 +00001750 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001751
Sean Callanan2ab712f22010-07-03 01:35:46 +00001752 return true;
1753}
1754
Sean Callanana4e55172010-11-08 00:31:32 +00001755bool
Sean Callanan79763a42011-05-23 21:40:23 +00001756IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanana4e55172010-11-08 00:31:32 +00001757{
Greg Clayton5160ce52013-03-27 23:08:40 +00001758 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001759
Sean Callanan339f6152014-03-11 19:19:16 +00001760 for (GlobalVariable &global_var : m_module->globals())
Sean Callanana4e55172010-11-08 00:31:32 +00001761 {
Sean Callanan339f6152014-03-11 19:19:16 +00001762 std::string global_name = global_var.getName().str();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001763
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001764 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001765 log->Printf("Examining %s, DeclForGlobalValue returns %p",
Sean Callanan694e2442011-12-22 21:24:49 +00001766 global_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001767 static_cast<void*>(DeclForGlobal(&global_var)));
1768
Sean Callananfc89c142011-11-01 23:38:03 +00001769 if (global_name.find("OBJC_IVAR") == 0)
Sean Callananc3a16002011-01-17 23:42:46 +00001770 {
Sean Callanan339f6152014-03-11 19:19:16 +00001771 if (!HandleSymbol(&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001772 {
1773 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001774 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 +00001775
Sean Callananc3a16002011-01-17 23:42:46 +00001776 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001777 }
Sean Callananc3a16002011-01-17 23:42:46 +00001778 }
Sean Callananfc89c142011-11-01 23:38:03 +00001779 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1780 {
Sean Callanan339f6152014-03-11 19:19:16 +00001781 if (!HandleObjCClass(&global_var))
Sean Callananfc89c142011-11-01 23:38:03 +00001782 {
1783 if (m_error_stream)
1784 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 +00001785
Sean Callananfc89c142011-11-01 23:38:03 +00001786 return false;
1787 }
1788 }
Sean Callanan2ad66912013-04-24 21:25:20 +00001789 else if (global_name.find("OBJC_CLASSLIST_SUP_REFS_$") != global_name.npos)
1790 {
Sean Callanan339f6152014-03-11 19:19:16 +00001791 if (!HandleObjCClass(&global_var))
Sean Callanan2ad66912013-04-24 21:25:20 +00001792 {
1793 if (m_error_stream)
1794 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 +00001795
Sean Callanan2ad66912013-04-24 21:25:20 +00001796 return false;
1797 }
1798 }
Sean Callanan339f6152014-03-11 19:19:16 +00001799 else if (DeclForGlobal(&global_var))
Sean Callananc3a16002011-01-17 23:42:46 +00001800 {
Sean Callanan339f6152014-03-11 19:19:16 +00001801 if (!MaybeHandleVariable (&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001802 {
1803 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001804 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 +00001805
Sean Callananc3a16002011-01-17 23:42:46 +00001806 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001807 }
Sean Callananc3a16002011-01-17 23:42:46 +00001808 }
Sean Callanana4e55172010-11-08 00:31:32 +00001809 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001810
Sean Callanana4e55172010-11-08 00:31:32 +00001811 return true;
1812}
1813
Sean Callanan79763a42011-05-23 21:40:23 +00001814bool
1815IRForTarget::ReplaceStrings ()
1816{
Greg Clayton5160ce52013-03-27 23:08:40 +00001817 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001818
Sean Callanan79763a42011-05-23 21:40:23 +00001819 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001820
Sean Callanan79763a42011-05-23 21:40:23 +00001821 OffsetsTy offsets;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001822
Sean Callanan339f6152014-03-11 19:19:16 +00001823 for (GlobalVariable &gv : m_module->globals())
Sean Callanan79763a42011-05-23 21:40:23 +00001824 {
Sean Callanan339f6152014-03-11 19:19:16 +00001825 if (!gv.hasInitializer())
Sean Callanan79763a42011-05-23 21:40:23 +00001826 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001827
Sean Callanan339f6152014-03-11 19:19:16 +00001828 Constant *gc = gv.getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001829
Sean Callanan5207a342011-08-10 21:05:52 +00001830 std::string str;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001831
Sean Callanan5207a342011-08-10 21:05:52 +00001832 if (gc->isNullValue())
1833 {
1834 Type *gc_type = gc->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001835
Sean Callanan5207a342011-08-10 21:05:52 +00001836 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001837
Sean Callanan5207a342011-08-10 21:05:52 +00001838 if (!gc_array_type)
1839 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001840
Sean Callanan5207a342011-08-10 21:05:52 +00001841 Type *gc_element_type = gc_array_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001842
Sean Callanan5207a342011-08-10 21:05:52 +00001843 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001844
Sean Callanan5207a342011-08-10 21:05:52 +00001845 if (gc_integer_type->getBitWidth() != 8)
1846 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001847
Sean Callanan5207a342011-08-10 21:05:52 +00001848 str = "";
1849 }
1850 else
1851 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001852 ConstantDataArray *gc_array = dyn_cast<ConstantDataArray>(gc);
Sean Callanan5207a342011-08-10 21:05:52 +00001853
1854 if (!gc_array)
1855 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001856
Sean Callanan5207a342011-08-10 21:05:52 +00001857 if (!gc_array->isCString())
1858 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001859
Sean Callanan5207a342011-08-10 21:05:52 +00001860 if (log)
1861 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001862
Sean Callanan5207a342011-08-10 21:05:52 +00001863 str = gc_array->getAsString();
1864 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001865
Sean Callanan339f6152014-03-11 19:19:16 +00001866 offsets[&gv] = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001867
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001868 m_data_allocator.GetStream().Write(str.c_str(), str.length() + 1);
Sean Callanan79763a42011-05-23 21:40:23 +00001869 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001870
Sean Callanancc427fa2011-07-30 02:42:06 +00001871 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001872
Sean Callanan79763a42011-05-23 21:40:23 +00001873 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1874 oi != oe;
1875 ++oi)
1876 {
1877 GlobalVariable *gv = oi->first;
1878 size_t offset = oi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001879
Sean Callanan79763a42011-05-23 21:40:23 +00001880 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001881
Sean Callanan79763a42011-05-23 21:40:23 +00001882 if (log)
1883 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001884
Ed Mastea8553092014-03-10 17:24:16 +00001885 for (llvm::User *u : gv->users())
Sean Callanan79763a42011-05-23 21:40:23 +00001886 {
1887 if (log)
Ed Mastea8553092014-03-10 17:24:16 +00001888 log->Printf("Found use %s", PrintValue(u).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001889
Ed Mastea8553092014-03-10 17:24:16 +00001890 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(u);
1891 StoreInst *store_inst = dyn_cast<StoreInst>(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001892
Sean Callanan79763a42011-05-23 21:40:23 +00001893 if (const_expr)
1894 {
1895 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1896 {
1897 if (log)
1898 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001899
Sean Callanan79763a42011-05-23 21:40:23 +00001900 return false;
1901 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001902
Sean Callanan5207a342011-08-10 21:05:52 +00001903 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
1904 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001905
Sean Callanan5207a342011-08-10 21:05:52 +00001906 const_expr->replaceAllUsesWith(new_gep);
Sean Callanan79763a42011-05-23 21:40:23 +00001907 }
1908 else if (store_inst)
1909 {
1910 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001911
Sean Callanan79763a42011-05-23 21:40:23 +00001912 store_inst->setOperand(0, bit_cast);
1913 }
1914 else
1915 {
1916 if (log)
1917 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 +00001918
Sean Callanan79763a42011-05-23 21:40:23 +00001919 return false;
1920 }
1921 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001922
Sean Callanan79763a42011-05-23 21:40:23 +00001923 gv->eraseFromParent();
1924 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001925
Sean Callanan79763a42011-05-23 21:40:23 +00001926 return true;
1927}
1928
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001929bool
Sean Callanan79763a42011-05-23 21:40:23 +00001930IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
1931{
Greg Clayton5160ce52013-03-27 23:08:40 +00001932 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001933
Sean Callanan79763a42011-05-23 21:40:23 +00001934 typedef SmallVector <Value*, 2> ConstantList;
1935 typedef SmallVector <llvm::Instruction*, 2> UserList;
1936 typedef ConstantList::iterator ConstantIterator;
1937 typedef UserList::iterator UserIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001938
Sean Callanan79763a42011-05-23 21:40:23 +00001939 ConstantList static_constants;
1940 UserList static_users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001941
Sean Callanan79763a42011-05-23 21:40:23 +00001942 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
1943 ii != ie;
1944 ++ii)
1945 {
1946 llvm::Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001947
Sean Callanan339f6152014-03-11 19:19:16 +00001948 for (Value *operand_val : inst.operand_values())
Sean Callanan79763a42011-05-23 21:40:23 +00001949 {
Sean Callanan79763a42011-05-23 21:40:23 +00001950 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001951
Sean Callanan822944c2012-04-26 20:51:20 +00001952 if (operand_constant_fp/* && operand_constant_fp->getType()->isX86_FP80Ty()*/)
Sean Callanan79763a42011-05-23 21:40:23 +00001953 {
1954 static_constants.push_back(operand_val);
1955 static_users.push_back(ii);
1956 }
1957 }
1958 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001959
Sean Callanan79763a42011-05-23 21:40:23 +00001960 ConstantIterator constant_iter;
1961 UserIterator user_iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001962
Sean Callanan79763a42011-05-23 21:40:23 +00001963 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
1964 constant_iter != static_constants.end();
1965 ++constant_iter, ++user_iter)
1966 {
1967 Value *operand_val = *constant_iter;
1968 llvm::Instruction *inst = *user_iter;
1969
1970 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001971
Sean Callanan79763a42011-05-23 21:40:23 +00001972 if (operand_constant_fp)
1973 {
Sean Callanan1582ee62013-04-18 22:06:33 +00001974 Type *operand_type = operand_constant_fp->getType();
1975
Sean Callanan79763a42011-05-23 21:40:23 +00001976 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
1977 APInt operand_apint = operand_apfloat.bitcastToAPInt();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001978
Sean Callanan79763a42011-05-23 21:40:23 +00001979 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
1980 size_t operand_data_size = operand_apint.getBitWidth() / 8;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001981
Sean Callanan79763a42011-05-23 21:40:23 +00001982 if (log)
1983 {
1984 std::string s;
1985 raw_string_ostream ss(s);
1986 for (size_t index = 0;
1987 index < operand_data_size;
1988 ++index)
1989 {
1990 ss << (uint32_t)operand_raw_data[index];
1991 ss << " ";
1992 }
1993 ss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001994
Greg Clayton6fea17e2014-03-03 19:15:20 +00001995 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 +00001996 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001997
Sean Callanan79763a42011-05-23 21:40:23 +00001998 lldb_private::DataBufferHeap data(operand_data_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001999
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002000 if (lldb::endian::InlHostByteOrder() != m_data_allocator.GetStream().GetByteOrder())
Sean Callanan79763a42011-05-23 21:40:23 +00002001 {
2002 uint8_t *data_bytes = data.GetBytes();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002003
Sean Callanan79763a42011-05-23 21:40:23 +00002004 for (size_t index = 0;
2005 index < operand_data_size;
2006 ++index)
2007 {
2008 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
2009 }
2010 }
2011 else
2012 {
2013 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
2014 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002015
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002016 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002017
Sean Callanane3333d62012-06-08 22:20:41 +00002018 size_t align = m_target_data->getPrefTypeAlignment(operand_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002019
Sean Callanane3333d62012-06-08 22:20:41 +00002020 const size_t mask = (align - 1);
2021 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002022 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002023
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002024 m_data_allocator.GetStream().Write(data.GetBytes(), operand_data_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002025
Sean Callanancc427fa2011-07-30 02:42:06 +00002026 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002027
Sean Callanan1582ee62013-04-18 22:06:33 +00002028 Constant *new_pointer = BuildRelocation(fp_ptr_ty, aligned_offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002029
Sean Callanan79763a42011-05-23 21:40:23 +00002030 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002031
Sean Callanan79763a42011-05-23 21:40:23 +00002032 operand_constant_fp->replaceAllUsesWith(fp_load);
2033 }
2034 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002035
Sean Callanan79763a42011-05-23 21:40:23 +00002036 return true;
2037}
2038
Sean Callanan7ea35012010-07-27 21:39:39 +00002039static bool isGuardVariableRef(Value *V)
Sean Callananddb46ef2010-07-24 01:37:44 +00002040{
Sean Callanan77eaf442011-07-08 00:39:14 +00002041 Constant *Old = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002042
Sean Callananafe16a72010-11-17 23:00:36 +00002043 if (!(Old = dyn_cast<Constant>(V)))
Sean Callananddb46ef2010-07-24 01:37:44 +00002044 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002045
Sean Callanan77eaf442011-07-08 00:39:14 +00002046 ConstantExpr *CE = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002047
Sean Callanane2ef6e32010-09-23 03:01:22 +00002048 if ((CE = dyn_cast<ConstantExpr>(V)))
2049 {
2050 if (CE->getOpcode() != Instruction::BitCast)
2051 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002052
Sean Callananafe16a72010-11-17 23:00:36 +00002053 Old = CE->getOperand(0);
Sean Callanane2ef6e32010-09-23 03:01:22 +00002054 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002055
Sean Callananafe16a72010-11-17 23:00:36 +00002056 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002057
Zachary Turnerf16ae602015-01-09 21:12:22 +00002058 if (!GV || !GV->hasName() ||
2059 (!GV->getName().startswith("_ZGV") && // Itanium ABI guard variable
2060 !GV->getName().endswith("@4IA"))) // Microsoft ABI guard variable
2061 {
Sean Callananddb46ef2010-07-24 01:37:44 +00002062 return false;
Zachary Turnerf16ae602015-01-09 21:12:22 +00002063 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002064
Sean Callananddb46ef2010-07-24 01:37:44 +00002065 return true;
2066}
2067
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002068void
Sean Callanan79763a42011-05-23 21:40:23 +00002069IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callananddb46ef2010-07-24 01:37:44 +00002070{
Zachary Turnerf16ae602015-01-09 21:12:22 +00002071 Constant *zero(Constant::getNullValue(guard_load->getType()));
2072 guard_load->replaceAllUsesWith(zero);
Sean Callananddb46ef2010-07-24 01:37:44 +00002073 guard_load->eraseFromParent();
2074}
2075
2076static void ExciseGuardStore(Instruction* guard_store)
2077{
2078 guard_store->eraseFromParent();
2079}
2080
2081bool
Sean Callanan79763a42011-05-23 21:40:23 +00002082IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002083{
Sean Callananddb46ef2010-07-24 01:37:44 +00002084 ///////////////////////////////////////////////////////
2085 // Eliminate any reference to guard variables found.
2086 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002087
Sean Callanan7ea35012010-07-27 21:39:39 +00002088 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002089
Sean Callanan7ea35012010-07-27 21:39:39 +00002090 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callananddb46ef2010-07-24 01:37:44 +00002091 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002092
Sean Callananddb46ef2010-07-24 01:37:44 +00002093 InstrList guard_loads;
2094 InstrList guard_stores;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002095
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002096 for (ii = basic_block.begin();
2097 ii != basic_block.end();
Sean Callananddb46ef2010-07-24 01:37:44 +00002098 ++ii)
2099 {
2100 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002101
Sean Callananddb46ef2010-07-24 01:37:44 +00002102 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
2103 if (isGuardVariableRef(load->getPointerOperand()))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002104 guard_loads.push_back(&inst);
2105
2106 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callananddb46ef2010-07-24 01:37:44 +00002107 if (isGuardVariableRef(store->getPointerOperand()))
2108 guard_stores.push_back(&inst);
2109 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002110
Sean Callananddb46ef2010-07-24 01:37:44 +00002111 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002112
Sean Callananddb46ef2010-07-24 01:37:44 +00002113 for (iter = guard_loads.begin();
2114 iter != guard_loads.end();
2115 ++iter)
Sean Callanan79763a42011-05-23 21:40:23 +00002116 TurnGuardLoadIntoZero(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002117
Sean Callananddb46ef2010-07-24 01:37:44 +00002118 for (iter = guard_stores.begin();
2119 iter != guard_stores.end();
2120 ++iter)
2121 ExciseGuardStore(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002122
Sean Callananddb46ef2010-07-24 01:37:44 +00002123 return true;
2124}
2125
Sean Callanan3989fb92011-01-27 01:07:04 +00002126// This function does not report errors; its callers are responsible.
Sean Callananafe16a72010-11-17 23:00:36 +00002127bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002128IRForTarget::UnfoldConstant(Constant *old_constant,
2129 FunctionValueCache &value_maker,
2130 FunctionValueCache &entry_instruction_finder)
Sean Callanan7618f4e2010-07-14 23:40:29 +00002131{
Greg Clayton5160ce52013-03-27 23:08:40 +00002132 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan7618f4e2010-07-14 23:40:29 +00002133
Sean Callanan2235f322010-08-11 03:57:18 +00002134 SmallVector<User*, 16> users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002135
Sean Callanan2235f322010-08-11 03:57:18 +00002136 // We do this because the use list might change, invalidating our iterator.
2137 // Much better to keep a work list ourselves.
Ed Maste80e8cc62014-03-10 14:23:10 +00002138 for (llvm::User *u : old_constant->users())
2139 users.push_back(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002140
Johnny Chen44805302011-07-19 19:48:13 +00002141 for (size_t i = 0;
Sean Callanan2235f322010-08-11 03:57:18 +00002142 i < users.size();
2143 ++i)
2144 {
2145 User *user = users[i];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002146
Sean Callanan7618f4e2010-07-14 23:40:29 +00002147 if (Constant *constant = dyn_cast<Constant>(user))
2148 {
2149 // synthesize a new non-constant equivalent of the constant
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002150
Sean Callanan7618f4e2010-07-14 23:40:29 +00002151 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
2152 {
2153 switch (constant_expr->getOpcode())
2154 {
2155 default:
2156 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002157 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002158 return false;
2159 case Instruction::BitCast:
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002160 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002161 FunctionValueCache bit_cast_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2162 // UnaryExpr
2163 // OperandList[0] is value
2164
2165 if (constant_expr->getOperand(0) != old_constant)
2166 return constant_expr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002167
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002168 return new BitCastInst(value_maker.GetValue(function),
2169 constant_expr->getType(),
2170 "",
2171 llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2172 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002173
Sean Callanan0e016fe2013-07-15 23:31:47 +00002174 if (!UnfoldConstant(constant_expr, bit_cast_maker, entry_instruction_finder))
2175 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002176 }
2177 break;
2178 case Instruction::GetElementPtr:
2179 {
2180 // GetElementPtrConstantExpr
2181 // OperandList[0] is base
2182 // OperandList[1]... are indices
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002183
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002184 FunctionValueCache get_element_pointer_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2185 Value *ptr = constant_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002186
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002187 if (ptr == old_constant)
2188 ptr = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002189
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002190 std::vector<Value*> index_vector;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002191
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002192 unsigned operand_index;
2193 unsigned num_operands = constant_expr->getNumOperands();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002194
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002195 for (operand_index = 1;
2196 operand_index < num_operands;
2197 ++operand_index)
2198 {
2199 Value *operand = constant_expr->getOperand(operand_index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002200
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002201 if (operand == old_constant)
2202 operand = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002203
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002204 index_vector.push_back(operand);
2205 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002206
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002207 ArrayRef <Value*> indices(index_vector);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002208
Vince Harron0fc6c672015-03-16 03:54:22 +00002209 return GetElementPtrInst::Create(nullptr, ptr, indices, "", llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002210 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002211
Sean Callanan0e016fe2013-07-15 23:31:47 +00002212 if (!UnfoldConstant(constant_expr, get_element_pointer_maker, entry_instruction_finder))
2213 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002214 }
2215 break;
2216 }
2217 }
2218 else
2219 {
2220 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002221 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002222 return false;
2223 }
2224 }
2225 else
2226 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002227 if (Instruction *inst = llvm::dyn_cast<Instruction>(user))
2228 {
2229 inst->replaceUsesOfWith(old_constant, value_maker.GetValue(inst->getParent()->getParent()));
2230 }
2231 else
2232 {
2233 if (log)
2234 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(user).c_str());
2235 return false;
2236 }
Sean Callanan7618f4e2010-07-14 23:40:29 +00002237 }
2238 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002239
Sean Callanan0e016fe2013-07-15 23:31:47 +00002240 if (!isa<GlobalValue>(old_constant))
2241 {
2242 old_constant->destroyConstant();
2243 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002244
Sean Callanan7618f4e2010-07-14 23:40:29 +00002245 return true;
2246}
2247
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002248bool
Sean Callanan79763a42011-05-23 21:40:23 +00002249IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan549c9f72010-07-13 21:41:46 +00002250{
Sean Callanan9e6ed532010-09-13 21:34:21 +00002251 if (!m_resolve_vars)
2252 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002253
Greg Clayton5160ce52013-03-27 23:08:40 +00002254 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan549c9f72010-07-13 21:41:46 +00002255
2256 m_decl_map->DoStructLayout();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002257
Sean Callanan549c9f72010-07-13 21:41:46 +00002258 if (log)
2259 log->Printf("Element arrangement:");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002260
Sean Callanan549c9f72010-07-13 21:41:46 +00002261 uint32_t num_elements;
2262 uint32_t element_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002263
Sean Callanan549c9f72010-07-13 21:41:46 +00002264 size_t size;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002265 lldb::offset_t alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002266
Sean Callanan549c9f72010-07-13 21:41:46 +00002267 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2268 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002269
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002270 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002271
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002272 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002273 {
2274 if (m_error_stream)
2275 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 +00002276
Sean Callanan549c9f72010-07-13 21:41:46 +00002277 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002278 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002279
Sean Callanan7ea35012010-07-27 21:39:39 +00002280 Argument *argument = iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002281
Sean Callananfc55f5d2010-09-21 00:44:12 +00002282 if (argument->getName().equals("this"))
2283 {
2284 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002285
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002286 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002287 {
2288 if (m_error_stream)
2289 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 +00002290
Sean Callananfc55f5d2010-09-21 00:44:12 +00002291 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002292 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002293
Sean Callananfc55f5d2010-09-21 00:44:12 +00002294 argument = iter;
2295 }
Sean Callanan17827832010-12-13 22:46:15 +00002296 else if (argument->getName().equals("self"))
2297 {
2298 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002299
Sean Callanan17827832010-12-13 22:46:15 +00002300 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002301 {
2302 if (m_error_stream)
2303 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 +00002304
Sean Callanan17827832010-12-13 22:46:15 +00002305 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002306 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002307
Sean Callanan17827832010-12-13 22:46:15 +00002308 if (!iter->getName().equals("_cmd"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002309 {
2310 if (m_error_stream)
2311 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 +00002312
Sean Callanan17827832010-12-13 22:46:15 +00002313 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002314 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002315
Sean Callanan17827832010-12-13 22:46:15 +00002316 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002317
Sean Callanan17827832010-12-13 22:46:15 +00002318 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002319 {
2320 if (m_error_stream)
2321 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 +00002322
Sean Callanan17827832010-12-13 22:46:15 +00002323 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002324 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002325
Sean Callanan17827832010-12-13 22:46:15 +00002326 argument = iter;
2327 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002328
Greg Clayton7b462cc2010-10-15 22:48:33 +00002329 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002330 {
2331 if (m_error_stream)
2332 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 +00002333
Sean Callanan549c9f72010-07-13 21:41:46 +00002334 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002335 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002336
Sean Callanan549c9f72010-07-13 21:41:46 +00002337 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002338 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002339
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002340 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callananafe16a72010-11-17 23:00:36 +00002341 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002342
Sean Callananafe16a72010-11-17 23:00:36 +00002343 if (!FirstEntryInstruction)
Sean Callanan3989fb92011-01-27 01:07:04 +00002344 {
2345 if (m_error_stream)
2346 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 +00002347
Sean Callanan549c9f72010-07-13 21:41:46 +00002348 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002349 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002350
Sean Callanan79763a42011-05-23 21:40:23 +00002351 LLVMContext &context(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +00002352 IntegerType *offset_type(Type::getInt32Ty(context));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002353
Sean Callanan549c9f72010-07-13 21:41:46 +00002354 if (!offset_type)
Sean Callanan3989fb92011-01-27 01:07:04 +00002355 {
2356 if (m_error_stream)
2357 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002358
Sean Callanan549c9f72010-07-13 21:41:46 +00002359 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002360 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002361
Sean Callanan549c9f72010-07-13 21:41:46 +00002362 for (element_index = 0; element_index < num_elements; ++element_index)
2363 {
Sean Callanan77eaf442011-07-08 00:39:14 +00002364 const clang::NamedDecl *decl = NULL;
2365 Value *value = NULL;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002366 lldb::offset_t offset;
Greg Clayton7b462cc2010-10-15 22:48:33 +00002367 lldb_private::ConstString name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002368
Sean Callanan823bb4c2010-08-30 22:17:16 +00002369 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan3989fb92011-01-27 01:07:04 +00002370 {
2371 if (m_error_stream)
2372 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002373
Sean Callanan549c9f72010-07-13 21:41:46 +00002374 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002375 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002376
Sean Callanan549c9f72010-07-13 21:41:46 +00002377 if (log)
Zachary Turnera746e8e2014-07-02 17:24:07 +00002378 log->Printf(" \"%s\" (\"%s\") placed at %" PRIu64,
Greg Clayton7b462cc2010-10-15 22:48:33 +00002379 name.GetCString(),
Sean Callananc70ed462011-10-25 18:36:40 +00002380 decl->getNameAsString().c_str(),
Sean Callanan549c9f72010-07-13 21:41:46 +00002381 offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002382
Sean Callananc70ed462011-10-25 18:36:40 +00002383 if (value)
Sean Callanan92adcac2011-01-13 08:53:35 +00002384 {
Sean Callananc70ed462011-10-25 18:36:40 +00002385 if (log)
2386 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002387
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002388 FunctionValueCache body_result_maker ([this, name, offset_type, offset, argument, value] (llvm::Function *function)->llvm::Value * {
2389 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2390 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2391 // entry in order to produce the static variable that the AST thinks it is accessing.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002392
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002393 llvm::Instruction *entry_instruction = llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002394
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002395 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
Vince Harron0fc6c672015-03-16 03:54:22 +00002396 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(nullptr,
2397 argument,
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002398 offset_int,
2399 "",
2400 entry_instruction);
2401
2402 if (name == m_result_name && !m_result_is_pointer)
2403 {
2404 BitCastInst *bit_cast = new BitCastInst(get_element_ptr,
2405 value->getType()->getPointerTo(),
2406 "",
2407 entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002408
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002409 LoadInst *load = new LoadInst(bit_cast, "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002410
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002411 return load;
2412 }
2413 else
2414 {
2415 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002416
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002417 return bit_cast;
2418 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002419 });
2420
Sean Callananc70ed462011-10-25 18:36:40 +00002421 if (Constant *constant = dyn_cast<Constant>(value))
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002422 {
2423 UnfoldConstant(constant, body_result_maker, m_entry_instruction_finder);
2424 }
2425 else if (Instruction *instruction = dyn_cast<Instruction>(value))
2426 {
2427 value->replaceAllUsesWith(body_result_maker.GetValue(instruction->getParent()->getParent()));
2428 }
Sean Callananc70ed462011-10-25 18:36:40 +00002429 else
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002430 {
2431 if (log)
2432 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(value).c_str());
2433 return false;
2434 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002435
Sean Callananc70ed462011-10-25 18:36:40 +00002436 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2437 var->eraseFromParent();
2438 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002439 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002440
Sean Callanan549c9f72010-07-13 21:41:46 +00002441 if (log)
Greg Clayton6fea17e2014-03-03 19:15:20 +00002442 log->Printf("Total structure [align %" PRId64 ", size %" PRIu64 "]", (int64_t)alignment, (uint64_t)size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002443
Sean Callanan549c9f72010-07-13 21:41:46 +00002444 return true;
2445}
2446
Sean Callanan79763a42011-05-23 21:40:23 +00002447llvm::Constant *
Greg Clayton5160ce52013-03-27 23:08:40 +00002448IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
Sean Callanan79763a42011-05-23 21:40:23 +00002449{
Sean Callanan439dcae2013-12-20 19:55:02 +00002450 llvm::Constant *offset_int = ConstantInt::get(m_intptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002451
Sean Callanancc427fa2011-07-30 02:42:06 +00002452 llvm::Constant *offset_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002453
Sean Callanancc427fa2011-07-30 02:42:06 +00002454 offset_array[0] = offset_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002455
Sean Callanancc427fa2011-07-30 02:42:06 +00002456 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002457
Sean Callanancc427fa2011-07-30 02:42:06 +00002458 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, offsets);
Sean Callanan79763a42011-05-23 21:40:23 +00002459 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002460
Sean Callanan79763a42011-05-23 21:40:23 +00002461 return reloc_getbitcast;
2462}
2463
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002464bool
Sean Callanan79763a42011-05-23 21:40:23 +00002465IRForTarget::CompleteDataAllocation ()
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002466{
Greg Clayton5160ce52013-03-27 23:08:40 +00002467 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan79763a42011-05-23 21:40:23 +00002468
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002469 if (!m_data_allocator.GetStream().GetSize())
Sean Callanan79763a42011-05-23 21:40:23 +00002470 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002471
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002472 lldb::addr_t allocation = m_data_allocator.Allocate();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002473
Sean Callanan79763a42011-05-23 21:40:23 +00002474 if (log)
2475 {
2476 if (allocation)
2477 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2478 else
2479 log->Printf("Failed to allocate static data");
2480 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002481
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002482 if (!allocation || allocation == LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +00002483 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002484
Sean Callanan439dcae2013-12-20 19:55:02 +00002485 Constant *relocated_addr = ConstantInt::get(m_intptr_ty, (uint64_t)allocation);
Sean Callanan79763a42011-05-23 21:40:23 +00002486 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002487
Sean Callanan79763a42011-05-23 21:40:23 +00002488 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002489
Sean Callanan79763a42011-05-23 21:40:23 +00002490 m_reloc_placeholder->eraseFromParent();
2491
2492 return true;
2493}
2494
Sean Callanan2ab712f22010-07-03 01:35:46 +00002495bool
Sean Callanan3d654b32012-09-24 22:25:51 +00002496IRForTarget::StripAllGVs (Module &llvm_module)
2497{
Greg Clayton5160ce52013-03-27 23:08:40 +00002498 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3d654b32012-09-24 22:25:51 +00002499 std::vector<GlobalVariable *> global_vars;
2500 std::set<GlobalVariable *>erased_vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002501
Sean Callanan3d654b32012-09-24 22:25:51 +00002502 bool erased = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002503
Sean Callanan3d654b32012-09-24 22:25:51 +00002504 while (erased)
2505 {
2506 erased = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002507
Sean Callanan339f6152014-03-11 19:19:16 +00002508 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002509 {
Sean Callanan339f6152014-03-11 19:19:16 +00002510 global_var.removeDeadConstantUsers();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002511
Sean Callanan339f6152014-03-11 19:19:16 +00002512 if (global_var.use_empty())
Sean Callanan3d654b32012-09-24 22:25:51 +00002513 {
2514 if (log)
2515 log->Printf("Did remove %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002516 PrintValue(&global_var).c_str());
2517 global_var.eraseFromParent();
Sean Callanan3d654b32012-09-24 22:25:51 +00002518 erased = true;
2519 break;
2520 }
2521 }
2522 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002523
Sean Callanan339f6152014-03-11 19:19:16 +00002524 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002525 {
Sean Callanan339f6152014-03-11 19:19:16 +00002526 GlobalValue::user_iterator ui = global_var.user_begin();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002527
Jim Inghamd77557d2012-11-26 19:54:04 +00002528 if (log)
2529 log->Printf("Couldn't remove %s because of %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002530 PrintValue(&global_var).c_str(),
Jim Inghamd77557d2012-11-26 19:54:04 +00002531 PrintValue(*ui).c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +00002532 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002533
Sean Callanan3d654b32012-09-24 22:25:51 +00002534 return true;
2535}
2536
2537bool
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002538IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002539{
Greg Clayton5160ce52013-03-27 23:08:40 +00002540 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002541
Sean Callanan79763a42011-05-23 21:40:23 +00002542 m_module = &llvm_module;
Micah Villmow8468dbe2012-10-08 16:28:57 +00002543 m_target_data.reset(new DataLayout(m_module));
Sean Callanan439dcae2013-12-20 19:55:02 +00002544 m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), m_target_data->getPointerSizeInBits());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002545
Sean Callananc70ed462011-10-25 18:36:40 +00002546 if (log)
2547 {
2548 std::string s;
2549 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002550
Sean Callananc70ed462011-10-25 18:36:40 +00002551 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002552
Sean Callananc70ed462011-10-25 18:36:40 +00002553 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002554
Sean Callananc70ed462011-10-25 18:36:40 +00002555 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
2556 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002557
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002558 Function* main_function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002559
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002560 if (!main_function)
2561 {
2562 if (log)
2563 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002564
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002565 if (m_error_stream)
2566 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
2567
2568 return false;
2569 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002570
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002571 if (!FixFunctionLinkage (*main_function))
2572 {
2573 if (log)
2574 log->Printf("Couldn't fix the linkage for the function");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002575
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002576 return false;
2577 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002578
Sean Callanan439dcae2013-12-20 19:55:02 +00002579 llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002580
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002581 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
Sean Callanan439dcae2013-12-20 19:55:02 +00002582 int8_ty,
Sean Callanan3d654b32012-09-24 22:25:51 +00002583 false /* IsConstant */,
Sean Callanan79763a42011-05-23 21:40:23 +00002584 GlobalVariable::InternalLinkage,
Sean Callanan439dcae2013-12-20 19:55:02 +00002585 Constant::getNullValue(int8_ty),
Sean Callanan79763a42011-05-23 21:40:23 +00002586 "reloc_placeholder",
2587 NULL /* InsertBefore */,
Sean Callanan3d654b32012-09-24 22:25:51 +00002588 GlobalVariable::NotThreadLocal /* ThreadLocal */,
Sean Callanan79763a42011-05-23 21:40:23 +00002589 0 /* AddressSpace */);
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002590
Sean Callanand1e5b432010-08-12 01:56:52 +00002591 ////////////////////////////////////////////////////////////
Greg Clayton7b462cc2010-10-15 22:48:33 +00002592 // Replace $__lldb_expr_result with a persistent variable
Sean Callanand1e5b432010-08-12 01:56:52 +00002593 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002594
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002595 if (!CreateResultVariable(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002596 {
2597 if (log)
2598 log->Printf("CreateResultVariable() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002599
Sean Callanan3989fb92011-01-27 01:07:04 +00002600 // CreateResultVariable() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002601
Sean Callanand1e5b432010-08-12 01:56:52 +00002602 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002603 }
Sean Callanan6e6d4a62012-07-21 02:02:15 +00002604
Sean Callananea685ae2011-11-01 17:33:54 +00002605 if (log && log->GetVerbose())
Sean Callanan79763a42011-05-23 21:40:23 +00002606 {
2607 std::string s;
2608 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002609
Sean Callanan79763a42011-05-23 21:40:23 +00002610 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002611
Sean Callanan79763a42011-05-23 21:40:23 +00002612 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002613
Sean Callanan79763a42011-05-23 21:40:23 +00002614 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2615 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002616
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002617 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2618 fi != fe;
2619 ++fi)
2620 {
2621 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002622
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002623 if (function->begin() == function->end())
2624 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002625
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002626 Function::iterator bbi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002627
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002628 for (bbi = function->begin();
2629 bbi != function->end();
2630 ++bbi)
2631 {
2632 if (!RemoveGuards(*bbi))
2633 {
2634 if (log)
2635 log->Printf("RemoveGuards() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002636
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002637 // RemoveGuards() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002638
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002639 return false;
2640 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002641
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002642 if (!RewritePersistentAllocs(*bbi))
2643 {
2644 if (log)
2645 log->Printf("RewritePersistentAllocs() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002646
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002647 // RewritePersistentAllocs() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002648
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002649 return false;
2650 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002651
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002652 if (!RemoveCXAAtExit(*bbi))
2653 {
2654 if (log)
2655 log->Printf("RemoveCXAAtExit() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002656
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002657 // RemoveCXAAtExit() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002658
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002659 return false;
2660 }
2661 }
2662 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002663
Sean Callananafe16a72010-11-17 23:00:36 +00002664 ///////////////////////////////////////////////////////////////////////////////
2665 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2666 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002667
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002668 if (!RewriteObjCConstStrings())
Sean Callanan17827832010-12-13 22:46:15 +00002669 {
2670 if (log)
2671 log->Printf("RewriteObjCConstStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002672
Sean Callanan3989fb92011-01-27 01:07:04 +00002673 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002674
Sean Callananafe16a72010-11-17 23:00:36 +00002675 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002676 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002677
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002678 ///////////////////////////////
2679 // Resolve function pointers
2680 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002681
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002682 if (!ResolveFunctionPointers(llvm_module))
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002683 {
2684 if (log)
2685 log->Printf("ResolveFunctionPointers() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002686
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002687 // ResolveFunctionPointers() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002688
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002689 return false;
2690 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002691
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002692 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2693 fi != fe;
2694 ++fi)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002695 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002696 llvm::Function *function = fi;
2697
2698 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2699 bbi != bbe;
2700 ++bbi)
Sean Callanan17827832010-12-13 22:46:15 +00002701 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002702 if (!RewriteObjCSelectors(*bbi))
2703 {
2704 if (log)
2705 log->Printf("RewriteObjCSelectors() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002706
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002707 // RewriteObjCSelectors() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002708
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002709 return false;
2710 }
Sean Callanan17827832010-12-13 22:46:15 +00002711 }
Sean Callananbad134f2012-07-27 19:25:24 +00002712 }
Sean Callanan2235f322010-08-11 03:57:18 +00002713
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002714 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2715 fi != fe;
2716 ++fi)
Sean Callananbad134f2012-07-27 19:25:24 +00002717 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002718 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002719
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002720 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2721 bbi != bbe;
2722 ++bbi)
Sean Callanan79763a42011-05-23 21:40:23 +00002723 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002724 if (!ResolveCalls(*bbi))
2725 {
2726 if (log)
2727 log->Printf("ResolveCalls() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002728
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002729 // ResolveCalls() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002730
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002731 return false;
2732 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002733
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002734 if (!ReplaceStaticLiterals(*bbi))
2735 {
2736 if (log)
2737 log->Printf("ReplaceStaticLiterals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002738
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002739 return false;
2740 }
Sean Callanan79763a42011-05-23 21:40:23 +00002741 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002742 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002743
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002744 ////////////////////////////////////////////////////////////////////////
2745 // Run function-level passes that only make sense on the main function
Sean Callanan038df5032010-09-30 21:18:25 +00002746 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002747
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002748 if (!ResolveExternals(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002749 {
2750 if (log)
2751 log->Printf("ResolveExternals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002752
Sean Callanan3989fb92011-01-27 01:07:04 +00002753 // ResolveExternals() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002754
Sean Callanana4e55172010-11-08 00:31:32 +00002755 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002756 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002757
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002758 if (!ReplaceVariables(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002759 {
2760 if (log)
2761 log->Printf("ReplaceVariables() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002762
Sean Callanan3989fb92011-01-27 01:07:04 +00002763 // ReplaceVariables() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002764
Sean Callanan038df5032010-09-30 21:18:25 +00002765 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002766 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002767
Sean Callanan79763a42011-05-23 21:40:23 +00002768 if (!ReplaceStrings())
2769 {
2770 if (log)
2771 log->Printf("ReplaceStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002772
Sean Callanan79763a42011-05-23 21:40:23 +00002773 return false;
2774 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002775
Sean Callanan79763a42011-05-23 21:40:23 +00002776 if (!CompleteDataAllocation())
2777 {
2778 if (log)
2779 log->Printf("CompleteDataAllocation() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002780
Sean Callanan79763a42011-05-23 21:40:23 +00002781 return false;
2782 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002783
Sean Callanan3d654b32012-09-24 22:25:51 +00002784 if (!StripAllGVs(llvm_module))
2785 {
2786 if (log)
2787 log->Printf("StripAllGVs() failed");
2788 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002789
Sean Callananea685ae2011-11-01 17:33:54 +00002790 if (log && log->GetVerbose())
Sean Callanan549c9f72010-07-13 21:41:46 +00002791 {
Sean Callanancc54bd32010-07-28 01:00:59 +00002792 std::string s;
2793 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002794
Sean Callanan79763a42011-05-23 21:40:23 +00002795 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002796
Sean Callanancc54bd32010-07-28 01:00:59 +00002797 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002798
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002799 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan2ab712f22010-07-03 01:35:46 +00002800 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002801
2802 return true;
Sean Callanan2ab712f22010-07-03 01:35:46 +00002803}
2804
2805void
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002806IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002807{
2808}
2809
2810PassManagerType
2811IRForTarget::getPotentialPassManagerType() const
2812{
2813 return PMT_ModulePassManager;
2814}