blob: 1c1f120b86ccc1b53c93c2b1bd649aaa6ff7676b [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"
Sean Callanan3d654b32012-09-24 22:25:51 +000019#include "llvm/PassManager.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000020#include "llvm/Transforms/IPO.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000021#include "llvm/IR/ValueSymbolTable.h"
Sean Callanan549c9f72010-07-13 21:41:46 +000022
23#include "clang/AST/ASTContext.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000024
25#include "lldb/Core/dwarf.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000026#include "lldb/Core/ConstString.h"
27#include "lldb/Core/DataBufferHeap.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000028#include "lldb/Core/Log.h"
29#include "lldb/Core/Scalar.h"
30#include "lldb/Core/StreamString.h"
31#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000032#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000033#include "lldb/Expression/IRInterpreter.h"
Sean Callanan79763a42011-05-23 21:40:23 +000034#include "lldb/Host/Endian.h"
Sean Callanan63697e52011-05-07 01:06:41 +000035#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000036
37#include <map>
38
39using namespace llvm;
40
Sean Callananeaacbc92010-08-18 18:50:51 +000041static char ID;
42
Sean Callanan8dfb68e2013-03-19 00:10:07 +000043IRForTarget::StaticDataAllocator::StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit) :
44 m_execution_unit(execution_unit),
Sean Callanan1582ee62013-04-18 22:06:33 +000045 m_stream_string(lldb_private::Stream::eBinary, execution_unit.GetAddressByteSize(), execution_unit.GetByteOrder()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000046 m_allocation(LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +000047{
48}
49
Sean Callanan8dfb68e2013-03-19 00:10:07 +000050lldb::addr_t IRForTarget::StaticDataAllocator::Allocate()
Sean Callanan79763a42011-05-23 21:40:23 +000051{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000052 lldb_private::Error err;
53
54 if (m_allocation != LLDB_INVALID_ADDRESS)
55 {
56 m_execution_unit.FreeNow(m_allocation);
57 m_allocation = LLDB_INVALID_ADDRESS;
58 }
59
60 m_allocation = m_execution_unit.WriteNow((const uint8_t*)m_stream_string.GetData(), m_stream_string.GetSize(), err);
61
62 return m_allocation;
Sean Callanan79763a42011-05-23 21:40:23 +000063}
64
Greg Clayton1b95a6f2010-11-19 01:05:25 +000065IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
66 bool resolve_vars,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000067 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +000068 lldb_private::Stream *error_stream,
Greg Clayton1b95a6f2010-11-19 01:05:25 +000069 const char *func_name) :
Sean Callanane2ef6e32010-09-23 03:01:22 +000070 ModulePass(ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +000071 m_resolve_vars(resolve_vars),
72 m_func_name(func_name),
Sean Callanan79763a42011-05-23 21:40:23 +000073 m_module(NULL),
Johnny Chen44805302011-07-19 19:48:13 +000074 m_decl_map(decl_map),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000075 m_data_allocator(execution_unit),
Sean Callanan179b5482013-04-16 23:49:09 +000076 m_memory_map(execution_unit),
Sean Callananafe16a72010-11-17 23:00:36 +000077 m_CFStringCreateWithBytes(NULL),
Sean Callanan1a8d4092010-08-27 01:01:44 +000078 m_sel_registerName(NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000079 m_error_stream(error_stream),
Sean Callanan92adcac2011-01-13 08:53:35 +000080 m_has_side_effects(false),
Sean Callanan63697e52011-05-07 01:06:41 +000081 m_result_store(NULL),
82 m_result_is_pointer(false),
Sean Callanan79763a42011-05-23 21:40:23 +000083 m_reloc_placeholder(NULL)
Sean Callanan2ab712f22010-07-03 01:35:46 +000084{
85}
86
Sean Callanan038df5032010-09-30 21:18:25 +000087/* Handy utility functions used at several places in the code */
Sean Callanan2235f322010-08-11 03:57:18 +000088
89static std::string
Greg Clayton1b95a6f2010-11-19 01:05:25 +000090PrintValue(const Value *value, bool truncate = false)
Sean Callanan2235f322010-08-11 03:57:18 +000091{
92 std::string s;
Jim Ingham28eb5712012-10-12 17:34:26 +000093 if (value)
94 {
95 raw_string_ostream rso(s);
96 value->print(rso);
97 rso.flush();
98 if (truncate)
99 s.resize(s.length() - 1);
100 }
Sean Callanan2235f322010-08-11 03:57:18 +0000101 return s;
102}
103
Sean Callanan038df5032010-09-30 21:18:25 +0000104static std::string
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000105PrintType(const Type *type, bool truncate = false)
Sean Callanan038df5032010-09-30 21:18:25 +0000106{
107 std::string s;
108 raw_string_ostream rso(s);
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000109 type->print(rso);
Sean Callanan038df5032010-09-30 21:18:25 +0000110 rso.flush();
111 if (truncate)
112 s.resize(s.length() - 1);
113 return s;
114}
115
Sean Callanan2ab712f22010-07-03 01:35:46 +0000116IRForTarget::~IRForTarget()
117{
118}
119
Sean Callanan79763a42011-05-23 21:40:23 +0000120bool
121IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
122{
Sean Callanan79763a42011-05-23 21:40:23 +0000123 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
124
Sean Callanan7f27d602011-11-19 02:54:21 +0000125 std::string name = llvm_function.getName().str();
Sean Callanan79763a42011-05-23 21:40:23 +0000126
127 return true;
128}
129
Sean Callanand1e5b432010-08-12 01:56:52 +0000130bool
Sean Callanan79763a42011-05-23 21:40:23 +0000131IRForTarget::HasSideEffects (llvm::Function &llvm_function)
Sean Callanane4ec90e2010-12-16 03:17:46 +0000132{
133 llvm::Function::iterator bbi;
134 BasicBlock::iterator ii;
Sean Callanan63697e52011-05-07 01:06:41 +0000135
Sean Callanane4ec90e2010-12-16 03:17:46 +0000136 for (bbi = llvm_function.begin();
137 bbi != llvm_function.end();
138 ++bbi)
139 {
140 BasicBlock &basic_block = *bbi;
141
142 for (ii = basic_block.begin();
143 ii != basic_block.end();
144 ++ii)
145 {
146 switch (ii->getOpcode())
147 {
148 default:
149 return true;
150 case Instruction::Store:
151 {
152 StoreInst *store_inst = dyn_cast<StoreInst>(ii);
153
154 Value *store_ptr = store_inst->getPointerOperand();
155
Sean Callanan63697e52011-05-07 01:06:41 +0000156 std::string ptr_name;
157
158 if (store_ptr->hasName())
Sean Callanan7f27d602011-11-19 02:54:21 +0000159 ptr_name = store_ptr->getName().str();
Sean Callanan63697e52011-05-07 01:06:41 +0000160
161 if (isa <AllocaInst> (store_ptr))
Sean Callanane4ec90e2010-12-16 03:17:46 +0000162 break;
Sean Callanan63697e52011-05-07 01:06:41 +0000163
164 if (ptr_name.find("$__lldb_expr_result") != std::string::npos)
165 {
166 if (ptr_name.find("GV") == std::string::npos)
167 m_result_store = store_inst;
168 }
169 else
170 {
171 return true;
172 }
173
174 break;
Sean Callanane4ec90e2010-12-16 03:17:46 +0000175 }
176 case Instruction::Load:
177 case Instruction::Alloca:
178 case Instruction::GetElementPtr:
Sean Callanan63697e52011-05-07 01:06:41 +0000179 case Instruction::BitCast:
Sean Callanane4ec90e2010-12-16 03:17:46 +0000180 case Instruction::Ret:
Sean Callanan63697e52011-05-07 01:06:41 +0000181 case Instruction::ICmp:
182 case Instruction::Br:
Sean Callanane4ec90e2010-12-16 03:17:46 +0000183 break;
184 }
185 }
186 }
187
188 return false;
189}
190
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000191bool
192IRForTarget::GetFunctionAddress (llvm::Function *fun,
193 uint64_t &fun_addr,
194 lldb_private::ConstString &name,
195 Constant **&value_ptr)
196{
Greg Clayton5160ce52013-03-27 23:08:40 +0000197 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000198
199 fun_addr = LLDB_INVALID_ADDRESS;
200 name.Clear();
201 value_ptr = NULL;
202
203 if (fun->isIntrinsic())
204 {
205 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
206
207 switch (intrinsic_id)
208 {
209 default:
210 if (log)
211 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
212
213 if (m_error_stream)
214 m_error_stream->Printf("Internal error [IRForTarget]: Call to unhandled compiler intrinsic '%s'\n", Intrinsic::getName(intrinsic_id).c_str());
215
216 return false;
217 case Intrinsic::memcpy:
218 {
219 static lldb_private::ConstString g_memcpy_str ("memcpy");
220 name = g_memcpy_str;
221 }
222 break;
Sean Callanana6cbf062011-11-16 00:20:50 +0000223 case Intrinsic::memset:
224 {
225 static lldb_private::ConstString g_memset_str ("memset");
226 name = g_memset_str;
227 }
228 break;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000229 }
230
231 if (log && name)
232 log->Printf("Resolved intrinsic name \"%s\"", name.GetCString());
233 }
234 else
235 {
236 name.SetCStringWithLength (fun->getName().data(), fun->getName().size());
237 }
238
239 // Find the address of the function.
240
241 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000242
243 if (fun_decl)
244 {
Sean Callananc70ed462011-10-25 18:36:40 +0000245 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_addr))
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000246 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000247 lldb_private::ConstString altnernate_name;
Greg Claytonf0705c82011-10-22 03:33:13 +0000248 bool found_it = m_decl_map->GetFunctionAddress (name, fun_addr);
249 if (!found_it)
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000250 {
Greg Claytonf0705c82011-10-22 03:33:13 +0000251 // Check for an alternate mangling for "std::basic_string<char>"
252 // that is part of the itanium C++ name mangling scheme
253 const char *name_cstr = name.GetCString();
Jim Ingham28eb5712012-10-12 17:34:26 +0000254 if (name_cstr && strncmp(name_cstr, "_ZNKSbIcE", strlen("_ZNKSbIcE")) == 0)
Greg Claytonf0705c82011-10-22 03:33:13 +0000255 {
256 std::string alternate_mangling("_ZNKSs");
257 alternate_mangling.append (name_cstr + strlen("_ZNKSbIcE"));
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000258 altnernate_name.SetCString(alternate_mangling.c_str());
259 found_it = m_decl_map->GetFunctionAddress (altnernate_name, fun_addr);
Greg Claytonf0705c82011-10-22 03:33:13 +0000260 }
261 }
262
263 if (!found_it)
264 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000265 lldb_private::Mangled mangled_name(name);
266 lldb_private::Mangled alt_mangled_name(altnernate_name);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000267 if (log)
Greg Claytonf0705c82011-10-22 03:33:13 +0000268 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000269 if (alt_mangled_name)
270 log->Printf("Function \"%s\" (alternate name \"%s\") has no address",
271 mangled_name.GetName().GetCString(),
272 alt_mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000273 else
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000274 log->Printf("Function \"%s\" had no address",
275 mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000276 }
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000277
278 if (m_error_stream)
Greg Claytonf0705c82011-10-22 03:33:13 +0000279 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000280 if (alt_mangled_name)
281 m_error_stream->Printf("error: call to a function '%s' (alternate name '%s') that is not present in the target\n",
282 mangled_name.GetName().GetCString(),
283 alt_mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000284 else
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000285 m_error_stream->Printf("error: call to a function '%s' that is not present in the target\n",
286 mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000287 }
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000288 return false;
289 }
290 }
291 }
292 else
293 {
294 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
295 {
296 if (log)
297 log->Printf ("Metadataless function \"%s\" had no address", name.GetCString());
298
299 if (m_error_stream)
300 m_error_stream->Printf("Error [IRForTarget]: Call to a symbol-only function '%s' that is not present in the target\n", name.GetCString());
301
302 return false;
303 }
304 }
305
306 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000307 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), fun_addr);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000308
309 return true;
310}
311
312llvm::Constant *
313IRForTarget::BuildFunctionPointer (llvm::Type *type,
314 uint64_t ptr)
315{
316 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callanan95769bf2012-10-11 22:00:52 +0000317 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000318 PointerType *fun_ptr_ty = PointerType::getUnqual(type);
319 Constant *fun_addr_int = ConstantInt::get(intptr_ty, ptr, false);
320 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
321}
322
Sean Callananfc8feb82011-10-31 22:11:40 +0000323void
324IRForTarget::RegisterFunctionMetadata(LLVMContext &context,
325 llvm::Value *function_ptr,
326 const char *name)
327{
Sean Callananfc8feb82011-10-31 22:11:40 +0000328 for (Value::use_iterator i = function_ptr->use_begin(), e = function_ptr->use_end();
329 i != e;
330 ++i)
331 {
332 Value *user = *i;
333
334 if (Instruction *user_inst = dyn_cast<Instruction>(user))
335 {
Sean Callanand2b465f2012-02-09 03:22:41 +0000336 Constant *name_array = ConstantDataArray::getString(context, StringRef(name));
Sean Callananfc8feb82011-10-31 22:11:40 +0000337
338 ArrayRef<Value *> md_values(name_array);
339
340 MDNode *metadata = MDNode::get(context, md_values);
341
342 user_inst->setMetadata("lldb.call.realName", metadata);
343 }
344 else
345 {
346 RegisterFunctionMetadata (context, user, name);
347 }
348 }
349}
350
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000351bool
352IRForTarget::ResolveFunctionPointers(llvm::Module &llvm_module,
353 llvm::Function &llvm_function)
354{
Greg Clayton5160ce52013-03-27 23:08:40 +0000355 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000356
357 for (llvm::Module::iterator fi = llvm_module.begin();
358 fi != llvm_module.end();
359 ++fi)
360 {
361 Function *fun = fi;
362
363 bool is_decl = fun->isDeclaration();
364
365 if (log)
Sean Callanan7f27d602011-11-19 02:54:21 +0000366 log->Printf("Examining %s function %s", (is_decl ? "declaration" : "non-declaration"), fun->getName().str().c_str());
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000367
368 if (!is_decl)
369 continue;
370
371 if (fun->hasNUses(0))
372 continue; // ignore
373
374 uint64_t addr = LLDB_INVALID_ADDRESS;
375 lldb_private::ConstString name;
376 Constant **value_ptr = NULL;
377
378 if (!GetFunctionAddress(fun,
379 addr,
380 name,
381 value_ptr))
382 return false; // GetFunctionAddress reports its own errors
383
384 Constant *value = BuildFunctionPointer(fun->getFunctionType(), addr);
385
Sean Callananfc8feb82011-10-31 22:11:40 +0000386 RegisterFunctionMetadata (llvm_module.getContext(), fun, name.AsCString());
387
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000388 if (value_ptr)
389 *value_ptr = value;
390
391 fun->replaceAllUsesWith(value);
392 }
393
394 return true;
395}
396
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000397
Sean Callanan63697e52011-05-07 01:06:41 +0000398clang::NamedDecl *
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000399IRForTarget::DeclForGlobal (const GlobalValue *global_val, Module *module)
Sean Callanan63697e52011-05-07 01:06:41 +0000400{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000401 NamedMDNode *named_metadata = module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan63697e52011-05-07 01:06:41 +0000402
403 if (!named_metadata)
404 return NULL;
405
406 unsigned num_nodes = named_metadata->getNumOperands();
407 unsigned node_index;
408
409 for (node_index = 0;
410 node_index < num_nodes;
411 ++node_index)
412 {
413 MDNode *metadata_node = named_metadata->getOperand(node_index);
414
415 if (!metadata_node)
416 return NULL;
417
418 if (metadata_node->getNumOperands() != 2)
419 continue;
420
421 if (metadata_node->getOperand(0) != global_val)
422 continue;
423
424 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
425
426 if (!constant_int)
427 return NULL;
428
429 uintptr_t ptr = constant_int->getZExtValue();
430
431 return reinterpret_cast<clang::NamedDecl *>(ptr);
432 }
433
434 return NULL;
435}
436
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000437clang::NamedDecl *
438IRForTarget::DeclForGlobal (GlobalValue *global_val)
439{
440 return DeclForGlobal(global_val, m_module);
441}
442
Sean Callanane4ec90e2010-12-16 03:17:46 +0000443bool
Sean Callanan79763a42011-05-23 21:40:23 +0000444IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanand1e5b432010-08-12 01:56:52 +0000445{
Greg Clayton5160ce52013-03-27 23:08:40 +0000446 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand1e5b432010-08-12 01:56:52 +0000447
Sean Callanan9e6ed532010-09-13 21:34:21 +0000448 if (!m_resolve_vars)
449 return true;
450
451 // Find the result variable. If it doesn't exist, we can give up right here.
Sean Callananfc55f5d2010-09-21 00:44:12 +0000452
Sean Callanan79763a42011-05-23 21:40:23 +0000453 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callananfc55f5d2010-09-21 00:44:12 +0000454
Sean Callanancc427fa2011-07-30 02:42:06 +0000455 std::string result_name_str;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000456 const char *result_name = NULL;
457
458 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
459 vi != ve;
460 ++vi)
461 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000462 result_name_str = vi->first().str();
463 const char *value_name = result_name_str.c_str();
464
465 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000466 strncmp(value_name, "_ZGV", 4))
Sean Callanan92adcac2011-01-13 08:53:35 +0000467 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000468 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000469 m_result_is_pointer = true;
470 break;
471 }
472
Sean Callanancc427fa2011-07-30 02:42:06 +0000473 if (strstr(value_name, "$__lldb_expr_result") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000474 strncmp(value_name, "_ZGV", 4))
Sean Callanan46ae9e52010-09-28 21:13:03 +0000475 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000476 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000477 m_result_is_pointer = false;
Sean Callanan46ae9e52010-09-28 21:13:03 +0000478 break;
479 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000480 }
481
482 if (!result_name)
483 {
484 if (log)
485 log->PutCString("Couldn't find result variable");
486
487 return true;
488 }
489
Sean Callanan46ae9e52010-09-28 21:13:03 +0000490 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000491 log->Printf("Result name: \"%s\"", result_name);
Sean Callanan46ae9e52010-09-28 21:13:03 +0000492
Sean Callanan79763a42011-05-23 21:40:23 +0000493 Value *result_value = m_module->getNamedValue(result_name);
Sean Callanand1e5b432010-08-12 01:56:52 +0000494
495 if (!result_value)
496 {
497 if (log)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000498 log->PutCString("Result variable had no data");
Sean Callanan92adcac2011-01-13 08:53:35 +0000499
Sean Callanan3989fb92011-01-27 01:07:04 +0000500 if (m_error_stream)
501 m_error_stream->Printf("Internal error [IRForTarget]: Result variable's name (%s) exists, but not its definition\n", result_name);
502
Sean Callananfc55f5d2010-09-21 00:44:12 +0000503 return false;
Sean Callanand1e5b432010-08-12 01:56:52 +0000504 }
Sean Callanan9e6ed532010-09-13 21:34:21 +0000505
Sean Callanand1e5b432010-08-12 01:56:52 +0000506 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000507 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sean Callanand1e5b432010-08-12 01:56:52 +0000508
509 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
510
511 if (!result_global)
512 {
513 if (log)
514 log->PutCString("Result variable isn't a GlobalVariable");
Sean Callanan3989fb92011-01-27 01:07:04 +0000515
516 if (m_error_stream)
517 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) is defined, but is not a global variable\n", result_name);
518
Sean Callanand1e5b432010-08-12 01:56:52 +0000519 return false;
520 }
521
Sean Callanan79763a42011-05-23 21:40:23 +0000522 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan63697e52011-05-07 01:06:41 +0000523 if (!result_decl)
Sean Callanand1e5b432010-08-12 01:56:52 +0000524 {
525 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000526 log->PutCString("Result variable doesn't have a corresponding Decl");
Sean Callanand1e5b432010-08-12 01:56:52 +0000527
Sean Callanan3989fb92011-01-27 01:07:04 +0000528 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000529 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) does not have a corresponding Clang entity\n", result_name);
Sean Callanan3989fb92011-01-27 01:07:04 +0000530
Sean Callanand1e5b432010-08-12 01:56:52 +0000531 return false;
532 }
Sean Callanand1e5b432010-08-12 01:56:52 +0000533
Sean Callanan63697e52011-05-07 01:06:41 +0000534 if (log)
Sean Callanand1e5b432010-08-12 01:56:52 +0000535 {
Sean Callanan63697e52011-05-07 01:06:41 +0000536 std::string decl_desc_str;
537 raw_string_ostream decl_desc_stream(decl_desc_str);
538 result_decl->print(decl_desc_stream);
539 decl_desc_stream.flush();
Sean Callanand1e5b432010-08-12 01:56:52 +0000540
Sean Callanan63697e52011-05-07 01:06:41 +0000541 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanand1e5b432010-08-12 01:56:52 +0000542 }
543
Sean Callanan63697e52011-05-07 01:06:41 +0000544 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
545 if (!result_var)
Sean Callanand1e5b432010-08-12 01:56:52 +0000546 {
547 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000548 log->PutCString("Result variable Decl isn't a VarDecl");
Sean Callanan3989fb92011-01-27 01:07:04 +0000549
550 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000551 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s)'s corresponding Clang entity isn't a variable\n", result_name);
Sean Callanan3989fb92011-01-27 01:07:04 +0000552
Sean Callanand1e5b432010-08-12 01:56:52 +0000553 return false;
554 }
Sean Callanand1e5b432010-08-12 01:56:52 +0000555
Sean Callanand1e5b432010-08-12 01:56:52 +0000556 // Get the next available result name from m_decl_map and create the persistent
557 // variable for it
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000558
Sean Callanan63697e52011-05-07 01:06:41 +0000559 // If the result is an Lvalue, it is emitted as a pointer; see
560 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan92adcac2011-01-13 08:53:35 +0000561 if (m_result_is_pointer)
562 {
Sean Callanan63697e52011-05-07 01:06:41 +0000563 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanan78e37602011-01-27 04:42:51 +0000564 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
Sean Callanan92adcac2011-01-13 08:53:35 +0000565
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000566 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
567 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
Sean Callanan5780f9d2011-12-08 19:04:34 +0000568
569 if (pointer_pointertype)
570 {
571 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
572
573 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
574 &result_decl->getASTContext());
575 }
576 else if (pointer_objcobjpointertype)
577 {
578 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
579
580 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
581 &result_decl->getASTContext());
582 }
583 else
Sean Callanan92adcac2011-01-13 08:53:35 +0000584 {
585 if (log)
586 log->PutCString("Expected result to have pointer type, but it did not");
Sean Callanan3989fb92011-01-27 01:07:04 +0000587
588 if (m_error_stream)
589 m_error_stream->Printf("Internal error [IRForTarget]: Lvalue result (%s) is not a pointer variable\n", result_name);
590
Sean Callanan92adcac2011-01-13 08:53:35 +0000591 return false;
592 }
Sean Callanan92adcac2011-01-13 08:53:35 +0000593 }
594 else
595 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000596 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Sean Callanan00f43622011-11-18 03:28:09 +0000597 &result_decl->getASTContext());
598 }
599
600 if (m_result_type.GetClangTypeBitWidth() == 0)
601 {
602 lldb_private::StreamString type_desc_stream;
603 m_result_type.DumpTypeDescription(&type_desc_stream);
604
605 if (log)
606 log->Printf("Result type has size 0");
607
608 if (m_error_stream)
Sean Callanan960534c2011-12-21 23:44:05 +0000609 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
Sean Callanan00f43622011-11-18 03:28:09 +0000610 type_desc_stream.GetData());
Sean Callanan960534c2011-12-21 23:44:05 +0000611 return false;
Sean Callanan92adcac2011-01-13 08:53:35 +0000612 }
613
Sean Callanan63697e52011-05-07 01:06:41 +0000614 if (log)
615 {
616 lldb_private::StreamString type_desc_stream;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000617 m_result_type.DumpTypeDescription(&type_desc_stream);
Sean Callanan63697e52011-05-07 01:06:41 +0000618
Sean Callanan00f43622011-11-18 03:28:09 +0000619 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
Sean Callanan63697e52011-05-07 01:06:41 +0000620 }
621
Sean Callanan1582ee62013-04-18 22:06:33 +0000622 m_result_name = lldb_private::ConstString("$RESULT_NAME");
Sean Callanand1e5b432010-08-12 01:56:52 +0000623
624 if (log)
Greg Claytonfaac1112013-03-14 18:31:44 +0000625 log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000626 m_result_name.GetCString(),
627 m_result_type.GetClangTypeBitWidth() / 8);
Sean Callanand1e5b432010-08-12 01:56:52 +0000628
629 // Construct a new result global and set up its metadata
630
Sean Callanan79763a42011-05-23 21:40:23 +0000631 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanand1e5b432010-08-12 01:56:52 +0000632 result_global->getType()->getElementType(),
633 false, /* not constant */
634 GlobalValue::ExternalLinkage,
635 NULL, /* no initializer */
Sean Callanan92adcac2011-01-13 08:53:35 +0000636 m_result_name.GetCString ());
Sean Callanand1e5b432010-08-12 01:56:52 +0000637
638 // It's too late in compilation to create a new VarDecl for this, but we don't
639 // need to. We point the metadata at the old VarDecl. This creates an odd
640 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton7b462cc2010-10-15 22:48:33 +0000641 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanand1e5b432010-08-12 01:56:52 +0000642 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
643 // fixed up.
644
Sean Callanan79763a42011-05-23 21:40:23 +0000645 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan63697e52011-05-07 01:06:41 +0000646 reinterpret_cast<uint64_t>(result_decl),
Sean Callanand1e5b432010-08-12 01:56:52 +0000647 false);
648
649 llvm::Value* values[2];
650 values[0] = new_result_global;
651 values[1] = new_constant_int;
652
Sean Callanand12cf8bb2011-05-15 22:34:38 +0000653 ArrayRef<Value*> value_ref(values, 2);
654
Sean Callanan79763a42011-05-23 21:40:23 +0000655 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
656 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanand1e5b432010-08-12 01:56:52 +0000657 named_metadata->addOperand(persistent_global_md);
658
659 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000660 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan1e87fff2010-09-07 22:43:19 +0000661 PrintValue(result_global).c_str(),
Sean Callanand1e5b432010-08-12 01:56:52 +0000662 PrintValue(new_result_global).c_str());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000663
664 if (result_global->hasNUses(0))
665 {
666 // We need to synthesize a store for this variable, because otherwise
667 // there's nothing to put into its equivalent persistent variable.
Sean Callanand1e5b432010-08-12 01:56:52 +0000668
Greg Clayton7b462cc2010-10-15 22:48:33 +0000669 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000670 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
671
672 if (!first_entry_instruction)
673 return false;
674
675 if (!result_global->hasInitializer())
676 {
677 if (log)
678 log->Printf("Couldn't find initializer for unused variable");
Sean Callanan3989fb92011-01-27 01:07:04 +0000679
680 if (m_error_stream)
681 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) has no writes and no initializer\n", result_name);
682
Sean Callanan1e87fff2010-09-07 22:43:19 +0000683 return false;
684 }
685
686 Constant *initializer = result_global->getInitializer();
Sean Callanane4ec90e2010-12-16 03:17:46 +0000687
688 // Here we write the initializer into a result variable assuming it
689 // can be computed statically.
690
691 if (!m_has_side_effects)
692 {
Sean Callanan80c48c12011-10-21 05:18:02 +0000693 //MaybeSetConstantResult (initializer,
694 // m_result_name,
695 // m_result_type);
Sean Callanane4ec90e2010-12-16 03:17:46 +0000696 }
Sean Callanan1e87fff2010-09-07 22:43:19 +0000697
Greg Clayton9c139312011-02-05 02:28:58 +0000698 StoreInst *synthesized_store = new StoreInst(initializer,
699 new_result_global,
700 first_entry_instruction);
Sean Callanan1e87fff2010-09-07 22:43:19 +0000701
702 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000703 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000704 }
705 else
706 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000707 if (!m_has_side_effects && lldb_private::ClangASTContext::IsPointerType (m_result_type.GetOpaqueQualType()))
Sean Callanan63697e52011-05-07 01:06:41 +0000708 {
Sean Callanan80c48c12011-10-21 05:18:02 +0000709 //MaybeSetCastResult (m_result_type);
Sean Callanan63697e52011-05-07 01:06:41 +0000710 }
711
Sean Callanan1e87fff2010-09-07 22:43:19 +0000712 result_global->replaceAllUsesWith(new_result_global);
713 }
714
Sean Callanan1582ee62013-04-18 22:06:33 +0000715 if (!m_decl_map->AddPersistentVariable(result_decl,
716 m_result_name,
717 m_result_type,
718 true,
719 m_result_is_pointer))
720 return false;
721
Sean Callanand1e5b432010-08-12 01:56:52 +0000722 result_global->eraseFromParent();
723
724 return true;
725}
726
Johnny Chenee7a3592011-08-09 23:10:20 +0000727#if 0
Greg Clayton5160ce52013-03-27 23:08:40 +0000728static void DebugUsers(Log *log, Value *value, uint8_t depth)
Sean Callananafe16a72010-11-17 23:00:36 +0000729{
730 if (!depth)
731 return;
732
733 depth--;
734
Johnny Chenee7a3592011-08-09 23:10:20 +0000735 if (log)
736 log->Printf(" <Begin %d users>", value->getNumUses());
Sean Callananafe16a72010-11-17 23:00:36 +0000737
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000738 for (Value::use_iterator ui = value->use_begin(), ue = value->use_end();
Sean Callananafe16a72010-11-17 23:00:36 +0000739 ui != ue;
740 ++ui)
741 {
Johnny Chenee7a3592011-08-09 23:10:20 +0000742 if (log)
743 log->Printf(" <Use %p> %s", *ui, PrintValue(*ui).c_str());
Sean Callananafe16a72010-11-17 23:00:36 +0000744 DebugUsers(log, *ui, depth);
745 }
746
Johnny Chenee7a3592011-08-09 23:10:20 +0000747 if (log)
748 log->Printf(" <End uses>");
Sean Callananafe16a72010-11-17 23:00:36 +0000749}
Johnny Chenee7a3592011-08-09 23:10:20 +0000750#endif
Sean Callananafe16a72010-11-17 23:00:36 +0000751
752bool
Sean Callanan79763a42011-05-23 21:40:23 +0000753IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000754 llvm::GlobalVariable *cstr,
755 Instruction *FirstEntryInstruction)
Sean Callananafe16a72010-11-17 23:00:36 +0000756{
Greg Clayton5160ce52013-03-27 23:08:40 +0000757 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananafe16a72010-11-17 23:00:36 +0000758
Sean Callanancc427fa2011-07-30 02:42:06 +0000759 Type *ns_str_ty = ns_str->getType();
Sean Callanan79763a42011-05-23 21:40:23 +0000760
Sean Callanancc427fa2011-07-30 02:42:06 +0000761 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
762 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callanan95769bf2012-10-11 22:00:52 +0000763 (m_module->getPointerSize()
Micah Villmow08318972012-10-11 17:21:41 +0000764 == Module::Pointer64) ? 64 : 32);
Sean Callanancc427fa2011-07-30 02:42:06 +0000765 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
766 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sean Callananafe16a72010-11-17 23:00:36 +0000767
768 if (!m_CFStringCreateWithBytes)
769 {
770 lldb::addr_t CFStringCreateWithBytes_addr;
771
772 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
773
774 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
775 {
776 if (log)
777 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
778
Sean Callanan3989fb92011-01-27 01:07:04 +0000779 if (m_error_stream)
780 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
781
Sean Callananafe16a72010-11-17 23:00:36 +0000782 return false;
783 }
784
785 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000786 log->Printf("Found CFStringCreateWithBytes at 0x%" PRIx64, CFStringCreateWithBytes_addr);
Sean Callananafe16a72010-11-17 23:00:36 +0000787
788 // Build the function type:
789 //
790 // CFStringRef CFStringCreateWithBytes (
791 // CFAllocatorRef alloc,
792 // const UInt8 *bytes,
793 // CFIndex numBytes,
794 // CFStringEncoding encoding,
795 // Boolean isExternalRepresentation
796 // );
797 //
798 // We make the following substitutions:
799 //
800 // CFStringRef -> i8*
801 // CFAllocatorRef -> i8*
802 // UInt8 * -> i8*
803 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
804 // CFStringEncoding -> i32
805 // Boolean -> i8
806
Sean Callanancc427fa2011-07-30 02:42:06 +0000807 Type *arg_type_array[5];
808
809 arg_type_array[0] = i8_ptr_ty;
810 arg_type_array[1] = i8_ptr_ty;
811 arg_type_array[2] = intptr_ty;
812 arg_type_array[3] = i32_ty;
813 arg_type_array[4] = i8_ty;
814
815 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
816
Sean Callanan79763a42011-05-23 21:40:23 +0000817 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000818
819 // Build the constant containing the pointer to the function
820 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
821 Constant *CFSCWB_addr_int = ConstantInt::get(intptr_ty, CFStringCreateWithBytes_addr, false);
822 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
823 }
824
Sean Callanand2b465f2012-02-09 03:22:41 +0000825 ConstantDataSequential *string_array = NULL;
Sean Callanan229ce2d2011-02-10 22:17:53 +0000826
827 if (cstr)
Sean Callanand2b465f2012-02-09 03:22:41 +0000828 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
Sean Callanancc427fa2011-07-30 02:42:06 +0000829
Sean Callananafe16a72010-11-17 23:00:36 +0000830 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000831 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
Sean Callanand2b465f2012-02-09 03:22:41 +0000832 Constant *numBytes_arg = ConstantInt::get(intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000833 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
834 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
835
Sean Callanancc427fa2011-07-30 02:42:06 +0000836 Value *argument_array[5];
837
838 argument_array[0] = alloc_arg;
839 argument_array[1] = bytes_arg;
840 argument_array[2] = numBytes_arg;
841 argument_array[3] = encoding_arg;
842 argument_array[4] = isExternal_arg;
843
844 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sean Callananafe16a72010-11-17 23:00:36 +0000845
846 CallInst *CFSCWB_call = CallInst::Create(m_CFStringCreateWithBytes,
Sean Callanancc427fa2011-07-30 02:42:06 +0000847 CFSCWB_arguments,
Sean Callananafe16a72010-11-17 23:00:36 +0000848 "CFStringCreateWithBytes",
849 FirstEntryInstruction);
Sean Callanan7a55a322010-11-18 22:21:58 +0000850
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000851 if (!UnfoldConstant(ns_str, CFSCWB_call, FirstEntryInstruction))
Sean Callananafe16a72010-11-17 23:00:36 +0000852 {
853 if (log)
854 log->PutCString("Couldn't replace the NSString with the result of the call");
855
Sean Callanan3989fb92011-01-27 01:07:04 +0000856 if (m_error_stream)
857 m_error_stream->Printf("Error [IRForTarget]: Couldn't replace an Objective-C constant string with a dynamic string\n");
858
Sean Callananafe16a72010-11-17 23:00:36 +0000859 return false;
860 }
861
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000862 ns_str->eraseFromParent();
Sean Callananafe16a72010-11-17 23:00:36 +0000863
864 return true;
865}
866
867bool
Sean Callanan79763a42011-05-23 21:40:23 +0000868IRForTarget::RewriteObjCConstStrings(Function &llvm_function)
Sean Callananafe16a72010-11-17 23:00:36 +0000869{
Greg Clayton5160ce52013-03-27 23:08:40 +0000870 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananafe16a72010-11-17 23:00:36 +0000871
Sean Callanan79763a42011-05-23 21:40:23 +0000872 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callananafe16a72010-11-17 23:00:36 +0000873
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000874 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callananafe16a72010-11-17 23:00:36 +0000875 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
876
877 if (!FirstEntryInstruction)
878 {
879 if (log)
880 log->PutCString("Couldn't find first instruction for rewritten Objective-C strings");
881
Sean Callanan3989fb92011-01-27 01:07:04 +0000882 if (m_error_stream)
883 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the location for calls to CFStringCreateWithBytes\n");
884
Sean Callananafe16a72010-11-17 23:00:36 +0000885 return false;
886 }
887
888 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
889 vi != ve;
890 ++vi)
891 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000892 std::string value_name = vi->first().str();
893 const char *value_name_cstr = value_name.c_str();
894
895 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callananafe16a72010-11-17 23:00:36 +0000896 {
897 Value *nsstring_value = vi->second;
898
899 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
900
901 if (!nsstring_global)
902 {
903 if (log)
904 log->PutCString("NSString variable is not a GlobalVariable");
Sean Callanan3989fb92011-01-27 01:07:04 +0000905
906 if (m_error_stream)
907 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a global variable\n");
908
Sean Callananafe16a72010-11-17 23:00:36 +0000909 return false;
910 }
911
912 if (!nsstring_global->hasInitializer())
913 {
914 if (log)
915 log->PutCString("NSString variable does not have an initializer");
Sean Callanan3989fb92011-01-27 01:07:04 +0000916
917 if (m_error_stream)
918 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have an initializer\n");
919
Sean Callananafe16a72010-11-17 23:00:36 +0000920 return false;
921 }
922
923 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
924
925 if (!nsstring_struct)
926 {
927 if (log)
928 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sean Callanan3989fb92011-01-27 01:07:04 +0000929
930 if (m_error_stream)
931 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a structure constant\n");
932
Sean Callananafe16a72010-11-17 23:00:36 +0000933 return false;
934 }
935
936 // We expect the following structure:
937 //
938 // struct {
939 // int *isa;
940 // int flags;
941 // char *str;
942 // long length;
943 // };
944
945 if (nsstring_struct->getNumOperands() != 4)
946 {
947 if (log)
948 log->Printf("NSString variable's initializer structure has an unexpected number of members. Should be 4, is %d", nsstring_struct->getNumOperands());
Sean Callanan3989fb92011-01-27 01:07:04 +0000949
950 if (m_error_stream)
951 m_error_stream->Printf("Internal error [IRForTarget]: The struct for an Objective-C constant string is not as expected\n");
952
Sean Callananafe16a72010-11-17 23:00:36 +0000953 return false;
954 }
955
956 Constant *nsstring_member = nsstring_struct->getOperand(2);
957
958 if (!nsstring_member)
959 {
960 if (log)
961 log->PutCString("NSString initializer's str element was empty");
Sean Callanan3989fb92011-01-27 01:07:04 +0000962
963 if (m_error_stream)
964 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have a string initializer\n");
965
Sean Callananafe16a72010-11-17 23:00:36 +0000966 return false;
967 }
968
969 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
970
971 if (!nsstring_expr)
972 {
973 if (log)
974 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sean Callanan3989fb92011-01-27 01:07:04 +0000975
976 if (m_error_stream)
977 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not constant\n");
978
Sean Callananafe16a72010-11-17 23:00:36 +0000979 return false;
980 }
981
982 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
983 {
984 if (log)
985 log->Printf("NSString initializer's str element is not a GetElementPtr expression, it's a %s", nsstring_expr->getOpcodeName());
Sean Callanan3989fb92011-01-27 01:07:04 +0000986
987 if (m_error_stream)
988 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not an array\n");
989
Sean Callananafe16a72010-11-17 23:00:36 +0000990 return false;
991 }
992
993 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
994
995 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
996
997 if (!cstr_global)
998 {
999 if (log)
1000 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sean Callanan3989fb92011-01-27 01:07:04 +00001001
1002 if (m_error_stream)
1003 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a global\n");
Sean Callanan80eee3a2010-11-20 02:06:01 +00001004
Sean Callananafe16a72010-11-17 23:00:36 +00001005 return false;
1006 }
1007
1008 if (!cstr_global->hasInitializer())
1009 {
1010 if (log)
1011 log->PutCString("NSString initializer's str element does not have an initializer");
Sean Callanan3989fb92011-01-27 01:07:04 +00001012
1013 if (m_error_stream)
1014 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to initialized data\n");
1015
Sean Callananafe16a72010-11-17 23:00:36 +00001016 return false;
1017 }
Sean Callanan229ce2d2011-02-10 22:17:53 +00001018
1019 /*
Sean Callananafe16a72010-11-17 23:00:36 +00001020 if (!cstr_array)
1021 {
1022 if (log)
1023 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sean Callanan3989fb92011-01-27 01:07:04 +00001024
1025 if (m_error_stream)
1026 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to an array\n");
1027
Sean Callananafe16a72010-11-17 23:00:36 +00001028 return false;
1029 }
1030
1031 if (!cstr_array->isCString())
1032 {
1033 if (log)
1034 log->PutCString("NSString initializer's str element is not a C string array");
Sean Callanan3989fb92011-01-27 01:07:04 +00001035
1036 if (m_error_stream)
1037 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a C string\n");
1038
Sean Callananafe16a72010-11-17 23:00:36 +00001039 return false;
1040 }
Sean Callanan229ce2d2011-02-10 22:17:53 +00001041 */
1042
Sean Callanand2b465f2012-02-09 03:22:41 +00001043 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
Sean Callananafe16a72010-11-17 23:00:36 +00001044
1045 if (log)
Sean Callanan229ce2d2011-02-10 22:17:53 +00001046 {
1047 if (cstr_array)
Sean Callanand2b465f2012-02-09 03:22:41 +00001048 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 +00001049 else
Sean Callanancc427fa2011-07-30 02:42:06 +00001050 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan229ce2d2011-02-10 22:17:53 +00001051 }
1052
1053 if (!cstr_array)
1054 cstr_global = NULL;
Sean Callananafe16a72010-11-17 23:00:36 +00001055
Sean Callanan79763a42011-05-23 21:40:23 +00001056 if (!RewriteObjCConstString(nsstring_global, cstr_global, FirstEntryInstruction))
Sean Callanan3989fb92011-01-27 01:07:04 +00001057 {
Sean Callananafe16a72010-11-17 23:00:36 +00001058 if (log)
1059 log->PutCString("Error rewriting the constant string");
Sean Callanan3989fb92011-01-27 01:07:04 +00001060
1061 // We don't print an error message here because RewriteObjCConstString has done so for us.
1062
Sean Callananafe16a72010-11-17 23:00:36 +00001063 return false;
1064 }
Sean Callananafe16a72010-11-17 23:00:36 +00001065 }
1066 }
1067
1068 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1069 vi != ve;
1070 ++vi)
1071 {
Sean Callanancc427fa2011-07-30 02:42:06 +00001072 std::string value_name = vi->first().str();
1073 const char *value_name_cstr = value_name.c_str();
1074
1075 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callananafe16a72010-11-17 23:00:36 +00001076 {
1077 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
1078
1079 if (!gv)
1080 {
1081 if (log)
1082 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sean Callanan3989fb92011-01-27 01:07:04 +00001083
1084 if (m_error_stream)
1085 m_error_stream->Printf("Internal error [IRForTarget]: Found a CFConstantStringClassReference, but it is not a global object\n");
1086
Sean Callananafe16a72010-11-17 23:00:36 +00001087 return false;
1088 }
1089
1090 gv->eraseFromParent();
1091
1092 break;
1093 }
1094 }
1095
1096 return true;
1097}
1098
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001099static bool IsObjCSelectorRef (Value *value)
Sean Callanan5300d372010-07-31 01:32:05 +00001100{
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001101 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sean Callanan5300d372010-07-31 01:32:05 +00001102
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001103 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
Sean Callanan5300d372010-07-31 01:32:05 +00001104 return false;
1105
1106 return true;
1107}
1108
Sean Callanan3989fb92011-01-27 01:07:04 +00001109// This function does not report errors; its callers are responsible.
Sean Callanan5300d372010-07-31 01:32:05 +00001110bool
Sean Callanan79763a42011-05-23 21:40:23 +00001111IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callanan5300d372010-07-31 01:32:05 +00001112{
Greg Clayton5160ce52013-03-27 23:08:40 +00001113 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001114
1115 LoadInst *load = dyn_cast<LoadInst>(selector_load);
1116
1117 if (!load)
1118 return false;
1119
1120 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1121 //
1122 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
1123 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1124 //
1125 // where %obj is the object pointer and %tmp is the selector.
1126 //
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001127 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
1128 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sean Callanan5300d372010-07-31 01:32:05 +00001129
1130 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
1131
1132 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
1133
1134 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1135 return false;
1136
1137 Constant *osr_initializer = _objc_selector_references_->getInitializer();
1138
1139 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
1140
1141 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1142 return false;
1143
1144 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1145
1146 if (!osr_initializer_base)
1147 return false;
1148
1149 // Find the string's initializer (a ConstantArray) and get the string from it
1150
1151 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
1152
1153 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1154 return false;
1155
1156 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1157
Sean Callanand2b465f2012-02-09 03:22:41 +00001158 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
Sean Callanan5300d372010-07-31 01:32:05 +00001159
1160 if (!omvn_initializer_array->isString())
1161 return false;
1162
1163 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
1164
1165 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001166 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sean Callanan5300d372010-07-31 01:32:05 +00001167
1168 // Construct a call to sel_registerName
1169
1170 if (!m_sel_registerName)
1171 {
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001172 lldb::addr_t sel_registerName_addr;
Sean Callanan5300d372010-07-31 01:32:05 +00001173
Greg Clayton7b462cc2010-10-15 22:48:33 +00001174 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001175 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callanan5300d372010-07-31 01:32:05 +00001176 return false;
1177
Sean Callananbe3a1b12010-10-26 00:31:56 +00001178 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001179 log->Printf("Found sel_registerName at 0x%" PRIx64, sel_registerName_addr);
Sean Callananbe3a1b12010-10-26 00:31:56 +00001180
Sean Callanan5300d372010-07-31 01:32:05 +00001181 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
1182
1183 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callanan79763a42011-05-23 21:40:23 +00001184 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callanan5300d372010-07-31 01:32:05 +00001185 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanancc427fa2011-07-30 02:42:06 +00001186 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sean Callanan5300d372010-07-31 01:32:05 +00001187
Sean Callanancc427fa2011-07-30 02:42:06 +00001188 Type *type_array[1];
1189
1190 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
1191
1192 ArrayRef<Type *> srN_arg_types(type_array, 1);
1193
Sean Callanan5300d372010-07-31 01:32:05 +00001194 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
1195
1196 // Build the constant containing the pointer to the function
Sean Callanancc427fa2011-07-30 02:42:06 +00001197 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callanan95769bf2012-10-11 22:00:52 +00001198 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan5300d372010-07-31 01:32:05 +00001199 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001200 Constant *srN_addr_int = ConstantInt::get(intptr_ty, sel_registerName_addr, false);
Sean Callanan5300d372010-07-31 01:32:05 +00001201 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1202 }
1203
Sean Callanancc427fa2011-07-30 02:42:06 +00001204 Value *argument_array[1];
1205
Sean Callanan79763a42011-05-23 21:40:23 +00001206 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sean Callanan5300d372010-07-31 01:32:05 +00001207
Sean Callanancc427fa2011-07-30 02:42:06 +00001208 argument_array[0] = omvn_pointer;
Sean Callanan5300d372010-07-31 01:32:05 +00001209
Sean Callanancc427fa2011-07-30 02:42:06 +00001210 ArrayRef<Value *> srN_arguments(argument_array, 1);
1211
Sean Callanan5300d372010-07-31 01:32:05 +00001212 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanancc427fa2011-07-30 02:42:06 +00001213 srN_arguments,
Sean Callananafe16a72010-11-17 23:00:36 +00001214 "sel_registerName",
Sean Callanan5300d372010-07-31 01:32:05 +00001215 selector_load);
1216
1217 // Replace the load with the call in all users
1218
1219 selector_load->replaceAllUsesWith(srN_call);
1220
1221 selector_load->eraseFromParent();
1222
1223 return true;
1224}
1225
1226bool
Sean Callanan79763a42011-05-23 21:40:23 +00001227IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callanan5300d372010-07-31 01:32:05 +00001228{
Greg Clayton5160ce52013-03-27 23:08:40 +00001229 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001230
1231 BasicBlock::iterator ii;
1232
1233 typedef SmallVector <Instruction*, 2> InstrList;
1234 typedef InstrList::iterator InstrIterator;
1235
1236 InstrList selector_loads;
1237
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001238 for (ii = basic_block.begin();
1239 ii != basic_block.end();
Sean Callanan5300d372010-07-31 01:32:05 +00001240 ++ii)
1241 {
1242 Instruction &inst = *ii;
1243
1244 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001245 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callanan5300d372010-07-31 01:32:05 +00001246 selector_loads.push_back(&inst);
1247 }
1248
1249 InstrIterator iter;
1250
1251 for (iter = selector_loads.begin();
1252 iter != selector_loads.end();
1253 ++iter)
1254 {
Sean Callanan79763a42011-05-23 21:40:23 +00001255 if (!RewriteObjCSelector(*iter))
Sean Callanan5300d372010-07-31 01:32:05 +00001256 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001257 if (m_error_stream)
1258 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't change a static reference to an Objective-C selector to a dynamic reference\n");
1259
Enrico Granata20edcdb2011-07-19 18:03:25 +00001260 if (log)
Sean Callanan5300d372010-07-31 01:32:05 +00001261 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sean Callanan3989fb92011-01-27 01:07:04 +00001262
Sean Callanan5300d372010-07-31 01:32:05 +00001263 return false;
1264 }
1265 }
1266
1267 return true;
1268}
1269
Sean Callanan3989fb92011-01-27 01:07:04 +00001270// This function does not report errors; its callers are responsible.
Sean Callanan2235f322010-08-11 03:57:18 +00001271bool
Sean Callanan79763a42011-05-23 21:40:23 +00001272IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanan2235f322010-08-11 03:57:18 +00001273{
Greg Clayton5160ce52013-03-27 23:08:40 +00001274 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane1175b72011-01-13 21:23:32 +00001275
Sean Callanan2235f322010-08-11 03:57:18 +00001276 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1277
1278 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1279
1280 if (!alloc_md || !alloc_md->getNumOperands())
1281 return false;
1282
1283 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
1284
1285 if (!constant_int)
1286 return false;
1287
1288 // We attempt to register this as a new persistent variable with the DeclMap.
1289
1290 uintptr_t ptr = constant_int->getZExtValue();
1291
Sean Callanand1e5b432010-08-12 01:56:52 +00001292 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanan2235f322010-08-11 03:57:18 +00001293
Sean Callanand1e5b432010-08-12 01:56:52 +00001294 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1295 &decl->getASTContext());
1296
Greg Clayton7b462cc2010-10-15 22:48:33 +00001297 StringRef decl_name (decl->getName());
1298 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan92adcac2011-01-13 08:53:35 +00001299 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanan2235f322010-08-11 03:57:18 +00001300 return false;
1301
Sean Callanan79763a42011-05-23 21:40:23 +00001302 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanane1175b72011-01-13 21:23:32 +00001303 alloc->getType(),
Sean Callanan2235f322010-08-11 03:57:18 +00001304 false, /* not constant */
1305 GlobalValue::ExternalLinkage,
1306 NULL, /* no initializer */
1307 alloc->getName().str().c_str());
1308
1309 // What we're going to do here is make believe this was a regular old external
1310 // variable. That means we need to make the metadata valid.
1311
Sean Callanan585c0ec82012-07-04 01:26:26 +00001312 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
Sean Callanan2235f322010-08-11 03:57:18 +00001313
1314 llvm::Value* values[2];
1315 values[0] = persistent_global;
1316 values[1] = constant_int;
Sean Callanand12cf8bb2011-05-15 22:34:38 +00001317
1318 ArrayRef<llvm::Value*> value_ref(values, 2);
Sean Callanan2235f322010-08-11 03:57:18 +00001319
Sean Callanan79763a42011-05-23 21:40:23 +00001320 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanan2235f322010-08-11 03:57:18 +00001321 named_metadata->addOperand(persistent_global_md);
1322
Sean Callanane1175b72011-01-13 21:23:32 +00001323 // Now, since the variable is a pointer variable, we will drop in a load of that
1324 // pointer variable.
1325
1326 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
1327
1328 if (log)
1329 log->Printf("Replacing \"%s\" with \"%s\"",
1330 PrintValue(alloc).c_str(),
1331 PrintValue(persistent_load).c_str());
1332
1333 alloc->replaceAllUsesWith(persistent_load);
Sean Callanan2235f322010-08-11 03:57:18 +00001334 alloc->eraseFromParent();
1335
1336 return true;
1337}
1338
1339bool
Sean Callanan79763a42011-05-23 21:40:23 +00001340IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanan2235f322010-08-11 03:57:18 +00001341{
Sean Callanan9e6ed532010-09-13 21:34:21 +00001342 if (!m_resolve_vars)
1343 return true;
1344
Greg Clayton5160ce52013-03-27 23:08:40 +00001345 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan2235f322010-08-11 03:57:18 +00001346
1347 BasicBlock::iterator ii;
1348
1349 typedef SmallVector <Instruction*, 2> InstrList;
1350 typedef InstrList::iterator InstrIterator;
1351
1352 InstrList pvar_allocs;
1353
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001354 for (ii = basic_block.begin();
1355 ii != basic_block.end();
Sean Callanan2235f322010-08-11 03:57:18 +00001356 ++ii)
1357 {
1358 Instruction &inst = *ii;
1359
1360 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callananf694a552011-01-21 22:30:25 +00001361 {
1362 llvm::StringRef alloc_name = alloc->getName();
1363
1364 if (alloc_name.startswith("$") &&
1365 !alloc_name.startswith("$__lldb"))
1366 {
1367 if (alloc_name.find_first_of("0123456789") == 1)
1368 {
1369 if (log)
1370 log->Printf("Rejecting a numeric persistent variable.");
1371
Sean Callanan3989fb92011-01-27 01:07:04 +00001372 if (m_error_stream)
1373 m_error_stream->Printf("Error [IRForTarget]: Names starting with $0, $1, ... are reserved for use as result names\n");
1374
Sean Callananf694a552011-01-21 22:30:25 +00001375 return false;
1376 }
1377
Sean Callanan2235f322010-08-11 03:57:18 +00001378 pvar_allocs.push_back(alloc);
Sean Callananf694a552011-01-21 22:30:25 +00001379 }
1380 }
Sean Callanan2235f322010-08-11 03:57:18 +00001381 }
1382
1383 InstrIterator iter;
1384
1385 for (iter = pvar_allocs.begin();
1386 iter != pvar_allocs.end();
1387 ++iter)
1388 {
Sean Callanan79763a42011-05-23 21:40:23 +00001389 if (!RewritePersistentAlloc(*iter))
Sean Callanan2235f322010-08-11 03:57:18 +00001390 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001391 if (m_error_stream)
1392 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
1393
Enrico Granata20edcdb2011-07-19 18:03:25 +00001394 if (log)
Sean Callanan2235f322010-08-11 03:57:18 +00001395 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sean Callanan3989fb92011-01-27 01:07:04 +00001396
Sean Callanan2235f322010-08-11 03:57:18 +00001397 return false;
1398 }
1399 }
1400
1401 return true;
1402}
1403
Sean Callananc70ed462011-10-25 18:36:40 +00001404bool
1405IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1406{
1407 if (!initializer)
1408 return true;
1409
Greg Clayton5160ce52013-03-27 23:08:40 +00001410 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc70ed462011-10-25 18:36:40 +00001411
1412 if (log && log->GetVerbose())
1413 log->Printf(" MaterializeInitializer(%p, %s)", data, PrintValue(initializer).c_str());
1414
1415 Type *initializer_type = initializer->getType();
1416
1417 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1418 {
1419 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1420 return true;
1421 }
Sean Callanand2b465f2012-02-09 03:22:41 +00001422 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
Sean Callananc70ed462011-10-25 18:36:40 +00001423 {
1424 if (array_initializer->isString())
1425 {
1426 std::string array_initializer_string = array_initializer->getAsString();
1427 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1428 }
1429 else
1430 {
1431 ArrayType *array_initializer_type = array_initializer->getType();
1432 Type *array_element_type = array_initializer_type->getElementType();
1433
1434 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
1435
1436 for (int i = 0; i < array_initializer->getNumOperands(); ++i)
1437 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001438 Value *operand_value = array_initializer->getOperand(i);
1439 Constant *operand_constant = dyn_cast<Constant>(operand_value);
1440
1441 if (!operand_constant)
1442 return false;
1443
1444 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
Sean Callananc70ed462011-10-25 18:36:40 +00001445 return false;
1446 }
1447 }
1448 return true;
1449 }
1450 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1451 {
1452 StructType *struct_initializer_type = struct_initializer->getType();
1453 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1454
1455 for (int i = 0;
1456 i < struct_initializer->getNumOperands();
1457 ++i)
1458 {
1459 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1460 return false;
1461 }
1462 return true;
1463 }
1464 return false;
1465}
1466
1467bool
1468IRForTarget::MaterializeInternalVariable (GlobalVariable *global_variable)
1469{
1470 if (GlobalVariable::isExternalLinkage(global_variable->getLinkage()))
1471 return false;
1472
Sean Callananfe5d1392011-11-15 19:13:54 +00001473 if (global_variable == m_reloc_placeholder)
1474 return true;
1475
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001476 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sean Callananc70ed462011-10-25 18:36:40 +00001477
1478 llvm::Type *variable_type = global_variable->getType();
1479
1480 Constant *initializer = global_variable->getInitializer();
1481
1482 llvm::Type *initializer_type = initializer->getType();
1483
1484 size_t size = m_target_data->getTypeAllocSize(initializer_type);
Sean Callanane3333d62012-06-08 22:20:41 +00001485 size_t align = m_target_data->getPrefTypeAlignment(initializer_type);
1486
1487 const size_t mask = (align - 1);
1488 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001489 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sean Callanane3333d62012-06-08 22:20:41 +00001490 offset = aligned_offset;
Sean Callananc70ed462011-10-25 18:36:40 +00001491
1492 lldb_private::DataBufferHeap data(size, '\0');
1493
1494 if (initializer)
1495 if (!MaterializeInitializer(data.GetBytes(), initializer))
1496 return false;
1497
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001498 m_data_allocator.GetStream().Write(data.GetBytes(), data.GetByteSize());
Sean Callananc70ed462011-10-25 18:36:40 +00001499
1500 Constant *new_pointer = BuildRelocation(variable_type, offset);
1501
1502 global_variable->replaceAllUsesWith(new_pointer);
1503
1504 global_variable->eraseFromParent();
1505
1506 return true;
1507}
1508
Sean Callanan3989fb92011-01-27 01:07:04 +00001509// This function does not report errors; its callers are responsible.
Sean Callanan549c9f72010-07-13 21:41:46 +00001510bool
Sean Callanan79763a42011-05-23 21:40:23 +00001511IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001512{
Greg Clayton5160ce52013-03-27 23:08:40 +00001513 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand7a1ca22010-12-02 19:47:57 +00001514
1515 if (log)
Sean Callanan88339f02010-12-06 22:16:55 +00001516 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sean Callananaf8e96c2011-08-01 17:41:38 +00001517
Greg Clayton7b462cc2010-10-15 22:48:33 +00001518 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callanan4cf04d22010-08-03 00:23:29 +00001519 {
Sean Callanan5666b672010-08-04 01:02:13 +00001520 switch (constant_expr->getOpcode())
Sean Callanan4cf04d22010-08-03 00:23:29 +00001521 {
Sean Callanan5666b672010-08-04 01:02:13 +00001522 default:
1523 break;
1524 case Instruction::GetElementPtr:
1525 case Instruction::BitCast:
Sean Callanan4cf04d22010-08-03 00:23:29 +00001526 Value *s = constant_expr->getOperand(0);
Sean Callanan79763a42011-05-23 21:40:23 +00001527 if (!MaybeHandleVariable(s))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001528 return false;
Sean Callanan4cf04d22010-08-03 00:23:29 +00001529 }
1530 }
Sean Callanand6e04ae2010-12-03 19:51:05 +00001531 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001532 {
Sean Callananc70ed462011-10-25 18:36:40 +00001533 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1534 return MaterializeInternalVariable(global_variable);
1535
Sean Callanan79763a42011-05-23 21:40:23 +00001536 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sean Callanan2ab712f22010-07-03 01:35:46 +00001537
Sean Callanan5300d372010-07-31 01:32:05 +00001538 if (!named_decl)
1539 {
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001540 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001541 return true;
1542
Sean Callanan14f0b0e2010-12-06 00:56:39 +00001543 if (!global_variable->hasExternalLinkage())
1544 return true;
1545
Sean Callanan5300d372010-07-31 01:32:05 +00001546 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001547 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sean Callanan3989fb92011-01-27 01:07:04 +00001548
Sean Callanan5300d372010-07-31 01:32:05 +00001549 return false;
1550 }
1551
Greg Clayton7b462cc2010-10-15 22:48:33 +00001552 std::string name (named_decl->getName().str());
Sean Callananea22d422010-07-16 00:09:46 +00001553
Sean Callanan038df5032010-09-30 21:18:25 +00001554 void *opaque_type = NULL;
Sean Callanan1d180662010-07-20 23:31:16 +00001555 clang::ASTContext *ast_context = NULL;
Sean Callananea22d422010-07-16 00:09:46 +00001556
1557 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callanan1d180662010-07-20 23:31:16 +00001558 {
Sean Callanan038df5032010-09-30 21:18:25 +00001559 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callanan1d180662010-07-20 23:31:16 +00001560 ast_context = &value_decl->getASTContext();
1561 }
Sean Callananea22d422010-07-16 00:09:46 +00001562 else
Sean Callanan1d180662010-07-20 23:31:16 +00001563 {
Sean Callananea22d422010-07-16 00:09:46 +00001564 return false;
Sean Callanan1d180662010-07-20 23:31:16 +00001565 }
Sean Callanan038df5032010-09-30 21:18:25 +00001566
Sean Callanan92adcac2011-01-13 08:53:35 +00001567 clang::QualType qual_type;
Sean Callanan77eaf442011-07-08 00:39:14 +00001568 const Type *value_type = NULL;
Sean Callanan92adcac2011-01-13 08:53:35 +00001569
Sean Callanane1175b72011-01-13 21:23:32 +00001570 if (name[0] == '$')
Sean Callanan92adcac2011-01-13 08:53:35 +00001571 {
1572 // The $__lldb_expr_result name indicates the the return value has allocated as
1573 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1574 // accesses to this static variable need to be redirected to the result of dereferencing
1575 // a pointer that is passed in as one of the arguments.
1576 //
1577 // Consequently, when reporting the size of the type, we report a pointer type pointing
1578 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanane1175b72011-01-13 21:23:32 +00001579 //
1580 // We also do this for any user-declared persistent variables.
Sean Callanan1d180662010-07-20 23:31:16 +00001581
Sean Callanan92adcac2011-01-13 08:53:35 +00001582 qual_type = ast_context->getPointerType(clang::QualType::getFromOpaquePtr(opaque_type));
1583 value_type = PointerType::get(global_variable->getType(), 0);
1584 }
1585 else
1586 {
1587 qual_type = clang::QualType::getFromOpaquePtr(opaque_type);
1588 value_type = global_variable->getType();
1589 }
Sean Callanan038df5032010-09-30 21:18:25 +00001590
Greg Claytonfaac1112013-03-14 18:31:44 +00001591 uint64_t value_size = (ast_context->getTypeSize(qual_type) + 7ull) / 8ull;
1592 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7ull) / 8ull;
Sean Callanan17827832010-12-13 22:46:15 +00001593
Sean Callanan038df5032010-09-30 21:18:25 +00001594 if (log)
Greg Claytonfaac1112013-03-14 18:31:44 +00001595 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %" PRIu64 ", align %" PRId64 "]",
Sean Callanan038df5032010-09-30 21:18:25 +00001596 name.c_str(),
1597 qual_type.getAsString().c_str(),
1598 PrintType(value_type).c_str(),
1599 value_size,
1600 value_alignment);
Sean Callanan17827832010-12-13 22:46:15 +00001601
Sean Callanan549c9f72010-07-13 21:41:46 +00001602
Sean Callanan64dfc9a2010-08-23 23:09:38 +00001603 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001604 lldb_private::ConstString (name.c_str()),
1605 llvm_value_ptr,
Sean Callanan4edba2d2010-07-27 02:07:53 +00001606 value_size,
1607 value_alignment))
Sean Callananaf8e96c2011-08-01 17:41:38 +00001608 {
1609 if (!global_variable->hasExternalLinkage())
1610 return true;
Sean Callanan0ff3bf92013-04-11 17:57:16 +00001611 else if (HandleSymbol (global_variable))
1612 return true;
Sean Callananaf8e96c2011-08-01 17:41:38 +00001613 else
1614 return false;
1615 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001616 }
Sean Callanan4a5fcbb2010-12-03 03:02:31 +00001617 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001618 {
1619 if (log)
1620 log->Printf("Function pointers aren't handled right now");
1621
1622 return false;
1623 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001624
1625 return true;
1626}
1627
Sean Callanan3989fb92011-01-27 01:07:04 +00001628// This function does not report errors; its callers are responsible.
Sean Callanan549c9f72010-07-13 21:41:46 +00001629bool
Sean Callanan79763a42011-05-23 21:40:23 +00001630IRForTarget::HandleSymbol (Value *symbol)
Sean Callanand9ca42a2011-05-08 02:21:26 +00001631{
Greg Clayton5160ce52013-03-27 23:08:40 +00001632 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc3a16002011-01-17 23:42:46 +00001633
1634 lldb_private::ConstString name(symbol->getName().str().c_str());
1635
Sean Callanan947ccc72011-12-01 02:04:16 +00001636 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
Sean Callananc3a16002011-01-17 23:42:46 +00001637
Greg Clayton084db102011-06-23 04:25:29 +00001638 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc3a16002011-01-17 23:42:46 +00001639 {
1640 if (log)
1641 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
1642
1643 return false;
1644 }
1645
1646 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001647 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
Sean Callananc3a16002011-01-17 23:42:46 +00001648
Sean Callanancc427fa2011-07-30 02:42:06 +00001649 Type *symbol_type = symbol->getType();
Sean Callanancc427fa2011-07-30 02:42:06 +00001650 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callanan95769bf2012-10-11 22:00:52 +00001651 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc3a16002011-01-17 23:42:46 +00001652
1653 Constant *symbol_addr_int = ConstantInt::get(intptr_ty, symbol_addr, false);
1654
1655 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1656
1657 if (log)
1658 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
1659
1660 symbol->replaceAllUsesWith(symbol_addr_ptr);
1661
1662 return true;
1663}
1664
1665bool
Sean Callanan79763a42011-05-23 21:40:23 +00001666IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanan85a0a832010-10-05 22:26:43 +00001667{
Greg Clayton5160ce52013-03-27 23:08:40 +00001668 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand7a1ca22010-12-02 19:47:57 +00001669
1670 if (log)
1671 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sean Callanan85a0a832010-10-05 22:26:43 +00001672
Sean Callananafe16a72010-11-17 23:00:36 +00001673 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanan85a0a832010-10-05 22:26:43 +00001674 op_index < num_ops;
1675 ++op_index)
Sean Callanan79763a42011-05-23 21:40:23 +00001676 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan3989fb92011-01-27 01:07:04 +00001677 {
1678 if (m_error_stream)
1679 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite one of the arguments of a function call.\n");
1680
Sean Callanan85a0a832010-10-05 22:26:43 +00001681 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001682 }
1683
Sean Callanan85a0a832010-10-05 22:26:43 +00001684 return true;
1685}
1686
1687bool
Sean Callananfc89c142011-11-01 23:38:03 +00001688IRForTarget::HandleObjCClass(Value *classlist_reference)
1689{
Greg Clayton5160ce52013-03-27 23:08:40 +00001690 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc89c142011-11-01 23:38:03 +00001691
1692 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
1693
1694 if (!global_variable)
1695 return false;
1696
1697 Constant *initializer = global_variable->getInitializer();
1698
1699 if (!initializer)
1700 return false;
1701
1702 if (!initializer->hasName())
1703 return false;
1704
1705 StringRef name(initializer->getName());
1706 lldb_private::ConstString name_cstr(name.str().c_str());
Greg Clayton1075aca2011-12-03 20:02:42 +00001707 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
Sean Callananfc89c142011-11-01 23:38:03 +00001708
1709 if (log)
1710 log->Printf("Found reference to Objective-C class %s (0x%llx)", name_cstr.AsCString(), (unsigned long long)class_ptr);
1711
1712 if (class_ptr == LLDB_INVALID_ADDRESS)
1713 return false;
1714
1715 if (global_variable->use_begin() == global_variable->use_end())
1716 return false;
1717
Sean Callanan719a4d52013-03-23 01:01:16 +00001718 SmallVector<LoadInst *, 2> load_instructions;
1719
Sean Callananfc89c142011-11-01 23:38:03 +00001720 for (Value::use_iterator i = global_variable->use_begin(), e = global_variable->use_end();
1721 i != e;
1722 ++i)
1723 {
Sean Callanan719a4d52013-03-23 01:01:16 +00001724 if (LoadInst *load_instruction = dyn_cast<LoadInst>(*i))
1725 load_instructions.push_back(load_instruction);
Sean Callananfc89c142011-11-01 23:38:03 +00001726 }
1727
Sean Callanan719a4d52013-03-23 01:01:16 +00001728 if (load_instructions.empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001729 return false;
1730
1731 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callanan95769bf2012-10-11 22:00:52 +00001732 (m_module->getPointerSize()
Micah Villmow08318972012-10-11 17:21:41 +00001733 == Module::Pointer64) ? 64 : 32);
Sean Callananfc89c142011-11-01 23:38:03 +00001734
1735 Constant *class_addr = ConstantInt::get(intptr_ty, (uint64_t)class_ptr);
Sean Callananfc89c142011-11-01 23:38:03 +00001736
Sean Callanan719a4d52013-03-23 01:01:16 +00001737 for (LoadInst *load_instruction : load_instructions)
1738 {
1739 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
Sean Callananfc89c142011-11-01 23:38:03 +00001740
Sean Callanan719a4d52013-03-23 01:01:16 +00001741 load_instruction->replaceAllUsesWith(class_bitcast);
1742
1743 load_instruction->eraseFromParent();
1744 }
Sean Callananfc89c142011-11-01 23:38:03 +00001745
1746 return true;
1747}
1748
1749bool
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001750IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1751{
1752 BasicBlock::iterator ii;
1753
1754 std::vector<CallInst *> calls_to_remove;
1755
1756 for (ii = basic_block.begin();
1757 ii != basic_block.end();
1758 ++ii)
1759 {
1760 Instruction &inst = *ii;
1761
1762 CallInst *call = dyn_cast<CallInst>(&inst);
1763
1764 // MaybeHandleCallArguments handles error reporting; we are silent here
1765 if (!call)
1766 continue;
1767
1768 bool remove = false;
1769
1770 llvm::Function *func = call->getCalledFunction();
1771
1772 if (func && func->getName() == "__cxa_atexit")
1773 remove = true;
1774
1775 llvm::Value *val = call->getCalledValue();
1776
1777 if (val && val->getName() == "__cxa_atexit")
1778 remove = true;
1779
1780 if (remove)
1781 calls_to_remove.push_back(call);
1782 }
1783
1784 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1785 ci != ce;
1786 ++ci)
1787 {
1788 (*ci)->eraseFromParent();
1789 }
1790
1791 return true;
1792}
1793
1794bool
Sean Callanan79763a42011-05-23 21:40:23 +00001795IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sean Callanan549c9f72010-07-13 21:41:46 +00001796{
Sean Callanan2ab712f22010-07-03 01:35:46 +00001797 /////////////////////////////////////////////////////////////////////////
1798 // Prepare the current basic block for execution in the remote process
1799 //
1800
Sean Callanan7ea35012010-07-27 21:39:39 +00001801 BasicBlock::iterator ii;
Sean Callanan549c9f72010-07-13 21:41:46 +00001802
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001803 for (ii = basic_block.begin();
1804 ii != basic_block.end();
Sean Callanan549c9f72010-07-13 21:41:46 +00001805 ++ii)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001806 {
Sean Callanan549c9f72010-07-13 21:41:46 +00001807 Instruction &inst = *ii;
Sean Callanan2ab712f22010-07-03 01:35:46 +00001808
Sean Callanana4e55172010-11-08 00:31:32 +00001809 CallInst *call = dyn_cast<CallInst>(&inst);
Sean Callanan4edba2d2010-07-27 02:07:53 +00001810
Sean Callanan3989fb92011-01-27 01:07:04 +00001811 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callanan79763a42011-05-23 21:40:23 +00001812 if (call && !MaybeHandleCallArguments(call))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001813 return false;
Sean Callanan2ab712f22010-07-03 01:35:46 +00001814 }
1815
1816 return true;
1817}
1818
Sean Callanana4e55172010-11-08 00:31:32 +00001819bool
Sean Callanan79763a42011-05-23 21:40:23 +00001820IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanana4e55172010-11-08 00:31:32 +00001821{
Greg Clayton5160ce52013-03-27 23:08:40 +00001822 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan7a55a322010-11-18 22:21:58 +00001823
Sean Callanan79763a42011-05-23 21:40:23 +00001824 for (Module::global_iterator global = m_module->global_begin(), end = m_module->global_end();
Sean Callanana4e55172010-11-08 00:31:32 +00001825 global != end;
1826 ++global)
1827 {
Sean Callanan694e2442011-12-22 21:24:49 +00001828 if (!global)
1829 {
1830 if (m_error_stream)
1831 m_error_stream->Printf("Internal error [IRForTarget]: global variable is NULL");
1832
1833 return false;
1834 }
1835
1836 std::string global_name = (*global).getName().str();
1837
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001838 if (log)
1839 log->Printf("Examining %s, DeclForGlobalValue returns %p",
Sean Callanan694e2442011-12-22 21:24:49 +00001840 global_name.c_str(),
Sean Callanan79763a42011-05-23 21:40:23 +00001841 DeclForGlobal(global));
Sean Callananfc89c142011-11-01 23:38:03 +00001842
1843 if (global_name.find("OBJC_IVAR") == 0)
Sean Callananc3a16002011-01-17 23:42:46 +00001844 {
Sean Callanan79763a42011-05-23 21:40:23 +00001845 if (!HandleSymbol(global))
Sean Callanan3989fb92011-01-27 01:07:04 +00001846 {
1847 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001848 m_error_stream->Printf("Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol %s\n", global_name.c_str());
Sean Callanan3989fb92011-01-27 01:07:04 +00001849
Sean Callananc3a16002011-01-17 23:42:46 +00001850 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001851 }
Sean Callananc3a16002011-01-17 23:42:46 +00001852 }
Sean Callananfc89c142011-11-01 23:38:03 +00001853 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1854 {
1855 if (!HandleObjCClass(global))
1856 {
1857 if (m_error_stream)
1858 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
1859
1860 return false;
1861 }
1862 }
Sean Callanan79763a42011-05-23 21:40:23 +00001863 else if (DeclForGlobal(global))
Sean Callananc3a16002011-01-17 23:42:46 +00001864 {
Sean Callanan79763a42011-05-23 21:40:23 +00001865 if (!MaybeHandleVariable (global))
Sean Callanan3989fb92011-01-27 01:07:04 +00001866 {
1867 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001868 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite external variable %s\n", global_name.c_str());
Sean Callanan3989fb92011-01-27 01:07:04 +00001869
Sean Callananc3a16002011-01-17 23:42:46 +00001870 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001871 }
Sean Callananc3a16002011-01-17 23:42:46 +00001872 }
Sean Callanana4e55172010-11-08 00:31:32 +00001873 }
1874
1875 return true;
1876}
1877
Sean Callanan79763a42011-05-23 21:40:23 +00001878bool
1879IRForTarget::ReplaceStrings ()
1880{
Greg Clayton5160ce52013-03-27 23:08:40 +00001881 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan79763a42011-05-23 21:40:23 +00001882
Sean Callanan79763a42011-05-23 21:40:23 +00001883 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
1884
1885 OffsetsTy offsets;
1886
1887 for (Module::global_iterator gi = m_module->global_begin(), ge = m_module->global_end();
1888 gi != ge;
1889 ++gi)
1890 {
1891 GlobalVariable *gv = gi;
1892
1893 if (!gv->hasInitializer())
1894 continue;
1895
1896 Constant *gc = gv->getInitializer();
1897
Sean Callanan5207a342011-08-10 21:05:52 +00001898 std::string str;
Sean Callanan79763a42011-05-23 21:40:23 +00001899
Sean Callanan5207a342011-08-10 21:05:52 +00001900 if (gc->isNullValue())
1901 {
1902 Type *gc_type = gc->getType();
1903
1904 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
1905
1906 if (!gc_array_type)
1907 continue;
1908
1909 Type *gc_element_type = gc_array_type->getElementType();
1910
1911 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
1912
1913 if (gc_integer_type->getBitWidth() != 8)
1914 continue;
1915
1916 str = "";
1917 }
1918 else
1919 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001920 ConstantDataArray *gc_array = dyn_cast<ConstantDataArray>(gc);
Sean Callanan5207a342011-08-10 21:05:52 +00001921
1922 if (!gc_array)
1923 continue;
Sean Callanan79763a42011-05-23 21:40:23 +00001924
Sean Callanan5207a342011-08-10 21:05:52 +00001925 if (!gc_array->isCString())
1926 continue;
Sean Callanan79763a42011-05-23 21:40:23 +00001927
Sean Callanan5207a342011-08-10 21:05:52 +00001928 if (log)
1929 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
Sean Callanan79763a42011-05-23 21:40:23 +00001930
Sean Callanan5207a342011-08-10 21:05:52 +00001931 str = gc_array->getAsString();
1932 }
1933
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001934 offsets[gv] = m_data_allocator.GetStream().GetSize();
Sean Callanan79763a42011-05-23 21:40:23 +00001935
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001936 m_data_allocator.GetStream().Write(str.c_str(), str.length() + 1);
Sean Callanan79763a42011-05-23 21:40:23 +00001937 }
1938
Sean Callanancc427fa2011-07-30 02:42:06 +00001939 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callanan79763a42011-05-23 21:40:23 +00001940
1941 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1942 oi != oe;
1943 ++oi)
1944 {
1945 GlobalVariable *gv = oi->first;
1946 size_t offset = oi->second;
1947
1948 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
1949
1950 if (log)
1951 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
1952
1953 for (GlobalVariable::use_iterator ui = gv->use_begin(), ue = gv->use_end();
1954 ui != ue;
1955 ++ui)
1956 {
1957 if (log)
1958 log->Printf("Found use %s", PrintValue(*ui).c_str());
1959
1960 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(*ui);
1961 StoreInst *store_inst = dyn_cast<StoreInst>(*ui);
1962
1963 if (const_expr)
1964 {
1965 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1966 {
1967 if (log)
1968 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
1969
1970 return false;
1971 }
1972
Sean Callanan5207a342011-08-10 21:05:52 +00001973 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
1974 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
1975
1976 const_expr->replaceAllUsesWith(new_gep);
Sean Callanan79763a42011-05-23 21:40:23 +00001977 }
1978 else if (store_inst)
1979 {
1980 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
1981
1982 store_inst->setOperand(0, bit_cast);
1983 }
1984 else
1985 {
1986 if (log)
1987 log->Printf("Use (%s) of string variable is neither a constant nor a store", PrintValue(const_expr).c_str());
1988
1989 return false;
1990 }
1991 }
1992
1993 gv->eraseFromParent();
1994 }
1995
1996 return true;
1997}
1998
1999bool
2000IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
2001{
Greg Clayton5160ce52013-03-27 23:08:40 +00002002 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan79763a42011-05-23 21:40:23 +00002003
2004 typedef SmallVector <Value*, 2> ConstantList;
2005 typedef SmallVector <llvm::Instruction*, 2> UserList;
2006 typedef ConstantList::iterator ConstantIterator;
2007 typedef UserList::iterator UserIterator;
2008
2009 ConstantList static_constants;
2010 UserList static_users;
2011
2012 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
2013 ii != ie;
2014 ++ii)
2015 {
2016 llvm::Instruction &inst = *ii;
2017
2018 for (Instruction::op_iterator oi = inst.op_begin(), oe = inst.op_end();
2019 oi != oe;
2020 ++oi)
2021 {
2022 Value *operand_val = oi->get();
2023
2024 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
2025
Sean Callanan822944c2012-04-26 20:51:20 +00002026 if (operand_constant_fp/* && operand_constant_fp->getType()->isX86_FP80Ty()*/)
Sean Callanan79763a42011-05-23 21:40:23 +00002027 {
2028 static_constants.push_back(operand_val);
2029 static_users.push_back(ii);
2030 }
2031 }
2032 }
2033
2034 ConstantIterator constant_iter;
2035 UserIterator user_iter;
Greg Clayton9b72eb72011-05-24 23:06:02 +00002036
Sean Callanan79763a42011-05-23 21:40:23 +00002037 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
2038 constant_iter != static_constants.end();
2039 ++constant_iter, ++user_iter)
2040 {
2041 Value *operand_val = *constant_iter;
2042 llvm::Instruction *inst = *user_iter;
2043
2044 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
2045
2046 if (operand_constant_fp)
2047 {
Sean Callanan1582ee62013-04-18 22:06:33 +00002048 Type *operand_type = operand_constant_fp->getType();
2049
Sean Callanan79763a42011-05-23 21:40:23 +00002050 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
2051 APInt operand_apint = operand_apfloat.bitcastToAPInt();
2052
2053 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
2054 size_t operand_data_size = operand_apint.getBitWidth() / 8;
2055
2056 if (log)
2057 {
2058 std::string s;
2059 raw_string_ostream ss(s);
2060 for (size_t index = 0;
2061 index < operand_data_size;
2062 ++index)
2063 {
2064 ss << (uint32_t)operand_raw_data[index];
2065 ss << " ";
2066 }
2067 ss.flush();
2068
Jason Molendafd54b362011-09-20 21:44:10 +00002069 log->Printf("Found ConstantFP with size %lu and raw data %s", operand_data_size, s.c_str());
Sean Callanan79763a42011-05-23 21:40:23 +00002070 }
2071
2072 lldb_private::DataBufferHeap data(operand_data_size, 0);
2073
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002074 if (lldb::endian::InlHostByteOrder() != m_data_allocator.GetStream().GetByteOrder())
Sean Callanan79763a42011-05-23 21:40:23 +00002075 {
2076 uint8_t *data_bytes = data.GetBytes();
2077
2078 for (size_t index = 0;
2079 index < operand_data_size;
2080 ++index)
2081 {
2082 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
2083 }
2084 }
2085 else
2086 {
2087 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
2088 }
2089
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002090 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sean Callanan79763a42011-05-23 21:40:23 +00002091
Sean Callanane3333d62012-06-08 22:20:41 +00002092 size_t align = m_target_data->getPrefTypeAlignment(operand_type);
2093
2094 const size_t mask = (align - 1);
2095 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002096 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sean Callanane3333d62012-06-08 22:20:41 +00002097 offset = aligned_offset;
2098
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002099 m_data_allocator.GetStream().Write(data.GetBytes(), operand_data_size);
Sean Callanan79763a42011-05-23 21:40:23 +00002100
Sean Callanancc427fa2011-07-30 02:42:06 +00002101 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
Sean Callanan79763a42011-05-23 21:40:23 +00002102
Sean Callanan1582ee62013-04-18 22:06:33 +00002103 Constant *new_pointer = BuildRelocation(fp_ptr_ty, aligned_offset);
Sean Callanan79763a42011-05-23 21:40:23 +00002104
2105 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
2106
2107 operand_constant_fp->replaceAllUsesWith(fp_load);
2108 }
2109 }
2110
2111 return true;
2112}
2113
Sean Callanan7ea35012010-07-27 21:39:39 +00002114static bool isGuardVariableRef(Value *V)
Sean Callananddb46ef2010-07-24 01:37:44 +00002115{
Sean Callanan77eaf442011-07-08 00:39:14 +00002116 Constant *Old = NULL;
Sean Callananddb46ef2010-07-24 01:37:44 +00002117
Sean Callananafe16a72010-11-17 23:00:36 +00002118 if (!(Old = dyn_cast<Constant>(V)))
Sean Callananddb46ef2010-07-24 01:37:44 +00002119 return false;
2120
Sean Callanan77eaf442011-07-08 00:39:14 +00002121 ConstantExpr *CE = NULL;
Sean Callanane2ef6e32010-09-23 03:01:22 +00002122
2123 if ((CE = dyn_cast<ConstantExpr>(V)))
2124 {
2125 if (CE->getOpcode() != Instruction::BitCast)
2126 return false;
2127
Sean Callananafe16a72010-11-17 23:00:36 +00002128 Old = CE->getOperand(0);
Sean Callanane2ef6e32010-09-23 03:01:22 +00002129 }
2130
Sean Callananafe16a72010-11-17 23:00:36 +00002131 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sean Callananddb46ef2010-07-24 01:37:44 +00002132
2133 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
2134 return false;
2135
2136 return true;
2137}
2138
Sean Callanan79763a42011-05-23 21:40:23 +00002139void
2140IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callananddb46ef2010-07-24 01:37:44 +00002141{
Sean Callanan79763a42011-05-23 21:40:23 +00002142 Constant* zero(ConstantInt::get(Type::getInt8Ty(m_module->getContext()), 0, true));
Sean Callananddb46ef2010-07-24 01:37:44 +00002143
2144 Value::use_iterator ui;
2145
2146 for (ui = guard_load->use_begin();
2147 ui != guard_load->use_end();
2148 ++ui)
Sean Callananb27a62f2010-07-27 01:17:28 +00002149 {
Greg Claytone6371122010-07-30 20:30:44 +00002150 if (isa<Constant>(*ui))
Sean Callananb27a62f2010-07-27 01:17:28 +00002151 {
2152 // do nothing for the moment
2153 }
2154 else
2155 {
2156 ui->replaceUsesOfWith(guard_load, zero);
2157 }
2158 }
Sean Callananddb46ef2010-07-24 01:37:44 +00002159
2160 guard_load->eraseFromParent();
2161}
2162
2163static void ExciseGuardStore(Instruction* guard_store)
2164{
2165 guard_store->eraseFromParent();
2166}
2167
2168bool
Sean Callanan79763a42011-05-23 21:40:23 +00002169IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sean Callananddb46ef2010-07-24 01:37:44 +00002170{
2171 ///////////////////////////////////////////////////////
2172 // Eliminate any reference to guard variables found.
2173 //
2174
Sean Callanan7ea35012010-07-27 21:39:39 +00002175 BasicBlock::iterator ii;
Sean Callananddb46ef2010-07-24 01:37:44 +00002176
Sean Callanan7ea35012010-07-27 21:39:39 +00002177 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callananddb46ef2010-07-24 01:37:44 +00002178 typedef InstrList::iterator InstrIterator;
2179
2180 InstrList guard_loads;
2181 InstrList guard_stores;
2182
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002183 for (ii = basic_block.begin();
2184 ii != basic_block.end();
Sean Callananddb46ef2010-07-24 01:37:44 +00002185 ++ii)
2186 {
2187 Instruction &inst = *ii;
2188
2189 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
2190 if (isGuardVariableRef(load->getPointerOperand()))
2191 guard_loads.push_back(&inst);
2192
2193 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
2194 if (isGuardVariableRef(store->getPointerOperand()))
2195 guard_stores.push_back(&inst);
2196 }
2197
2198 InstrIterator iter;
2199
2200 for (iter = guard_loads.begin();
2201 iter != guard_loads.end();
2202 ++iter)
Sean Callanan79763a42011-05-23 21:40:23 +00002203 TurnGuardLoadIntoZero(*iter);
Sean Callananddb46ef2010-07-24 01:37:44 +00002204
2205 for (iter = guard_stores.begin();
2206 iter != guard_stores.end();
2207 ++iter)
2208 ExciseGuardStore(*iter);
2209
2210 return true;
2211}
2212
Sean Callanan3989fb92011-01-27 01:07:04 +00002213// This function does not report errors; its callers are responsible.
Sean Callananafe16a72010-11-17 23:00:36 +00002214bool
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002215IRForTarget::UnfoldConstant(Constant *old_constant, Value *new_constant, Instruction *first_entry_inst)
Sean Callanan7618f4e2010-07-14 23:40:29 +00002216{
Greg Clayton5160ce52013-03-27 23:08:40 +00002217 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan7618f4e2010-07-14 23:40:29 +00002218
2219 Value::use_iterator ui;
2220
Sean Callanan2235f322010-08-11 03:57:18 +00002221 SmallVector<User*, 16> users;
2222
2223 // We do this because the use list might change, invalidating our iterator.
2224 // Much better to keep a work list ourselves.
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002225 for (ui = old_constant->use_begin();
2226 ui != old_constant->use_end();
Sean Callanan7618f4e2010-07-14 23:40:29 +00002227 ++ui)
Sean Callanan2235f322010-08-11 03:57:18 +00002228 users.push_back(*ui);
Sean Callanan7618f4e2010-07-14 23:40:29 +00002229
Johnny Chen44805302011-07-19 19:48:13 +00002230 for (size_t i = 0;
Sean Callanan2235f322010-08-11 03:57:18 +00002231 i < users.size();
2232 ++i)
2233 {
2234 User *user = users[i];
2235
Sean Callanan7618f4e2010-07-14 23:40:29 +00002236 if (Constant *constant = dyn_cast<Constant>(user))
2237 {
2238 // synthesize a new non-constant equivalent of the constant
2239
2240 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
2241 {
2242 switch (constant_expr->getOpcode())
2243 {
2244 default:
2245 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002246 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002247 return false;
2248 case Instruction::BitCast:
2249 {
2250 // UnaryExpr
2251 // OperandList[0] is value
2252
2253 Value *s = constant_expr->getOperand(0);
2254
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002255 if (s == old_constant)
2256 s = new_constant;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002257
Sean Callanan79763a42011-05-23 21:40:23 +00002258 BitCastInst *bit_cast(new BitCastInst(s, constant_expr->getType(), "", first_entry_inst));
Sean Callanan7618f4e2010-07-14 23:40:29 +00002259
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002260 UnfoldConstant(constant_expr, bit_cast, first_entry_inst);
Sean Callanan7618f4e2010-07-14 23:40:29 +00002261 }
2262 break;
2263 case Instruction::GetElementPtr:
2264 {
2265 // GetElementPtrConstantExpr
2266 // OperandList[0] is base
2267 // OperandList[1]... are indices
2268
2269 Value *ptr = constant_expr->getOperand(0);
2270
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002271 if (ptr == old_constant)
2272 ptr = new_constant;
Sean Callanancc427fa2011-07-30 02:42:06 +00002273
2274 std::vector<Value*> index_vector;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002275
2276 unsigned operand_index;
2277 unsigned num_operands = constant_expr->getNumOperands();
2278
2279 for (operand_index = 1;
2280 operand_index < num_operands;
2281 ++operand_index)
2282 {
2283 Value *operand = constant_expr->getOperand(operand_index);
2284
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002285 if (operand == old_constant)
2286 operand = new_constant;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002287
Sean Callanancc427fa2011-07-30 02:42:06 +00002288 index_vector.push_back(operand);
Sean Callanan7618f4e2010-07-14 23:40:29 +00002289 }
2290
Sean Callanancc427fa2011-07-30 02:42:06 +00002291 ArrayRef <Value*> indices(index_vector);
2292
2293 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices, "", first_entry_inst));
Sean Callanan7618f4e2010-07-14 23:40:29 +00002294
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002295 UnfoldConstant(constant_expr, get_element_ptr, first_entry_inst);
Sean Callanan7618f4e2010-07-14 23:40:29 +00002296 }
2297 break;
2298 }
2299 }
2300 else
2301 {
2302 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002303 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002304 return false;
2305 }
2306 }
2307 else
2308 {
2309 // simple fall-through case for non-constants
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002310 user->replaceUsesOfWith(old_constant, new_constant);
Sean Callanan7618f4e2010-07-14 23:40:29 +00002311 }
2312 }
2313
2314 return true;
2315}
2316
Sean Callanan549c9f72010-07-13 21:41:46 +00002317bool
Sean Callanan79763a42011-05-23 21:40:23 +00002318IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan549c9f72010-07-13 21:41:46 +00002319{
Sean Callanan9e6ed532010-09-13 21:34:21 +00002320 if (!m_resolve_vars)
2321 return true;
2322
Greg Clayton5160ce52013-03-27 23:08:40 +00002323 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan549c9f72010-07-13 21:41:46 +00002324
2325 m_decl_map->DoStructLayout();
2326
2327 if (log)
2328 log->Printf("Element arrangement:");
2329
2330 uint32_t num_elements;
2331 uint32_t element_index;
2332
2333 size_t size;
2334 off_t alignment;
2335
2336 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2337 return false;
2338
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002339 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sean Callanan549c9f72010-07-13 21:41:46 +00002340
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002341 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002342 {
2343 if (m_error_stream)
2344 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes no arguments (should take at least a struct pointer)");
2345
Sean Callanan549c9f72010-07-13 21:41:46 +00002346 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002347 }
2348
Sean Callanan7ea35012010-07-27 21:39:39 +00002349 Argument *argument = iter;
Sean Callanan549c9f72010-07-13 21:41:46 +00002350
Sean Callananfc55f5d2010-09-21 00:44:12 +00002351 if (argument->getName().equals("this"))
2352 {
2353 ++iter;
2354
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002355 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002356 {
2357 if (m_error_stream)
2358 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'this' argument (should take a struct pointer too)");
2359
Sean Callananfc55f5d2010-09-21 00:44:12 +00002360 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002361 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00002362
2363 argument = iter;
2364 }
Sean Callanan17827832010-12-13 22:46:15 +00002365 else if (argument->getName().equals("self"))
2366 {
2367 ++iter;
2368
2369 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002370 {
2371 if (m_error_stream)
2372 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' argument (should take '_cmd' and a struct pointer too)");
2373
Sean Callanan17827832010-12-13 22:46:15 +00002374 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002375 }
Sean Callanan17827832010-12-13 22:46:15 +00002376
2377 if (!iter->getName().equals("_cmd"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002378 {
2379 if (m_error_stream)
2380 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes '%s' after 'self' argument (should take '_cmd')", iter->getName().str().c_str());
2381
Sean Callanan17827832010-12-13 22:46:15 +00002382 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002383 }
Sean Callanan17827832010-12-13 22:46:15 +00002384
2385 ++iter;
2386
2387 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002388 {
2389 if (m_error_stream)
2390 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' and '_cmd' arguments (should take a struct pointer too)");
2391
Sean Callanan17827832010-12-13 22:46:15 +00002392 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002393 }
Sean Callanan17827832010-12-13 22:46:15 +00002394
2395 argument = iter;
2396 }
Sean Callananfc55f5d2010-09-21 00:44:12 +00002397
Greg Clayton7b462cc2010-10-15 22:48:33 +00002398 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002399 {
2400 if (m_error_stream)
2401 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes an argument named '%s' instead of the struct pointer", argument->getName().str().c_str());
2402
Sean Callanan549c9f72010-07-13 21:41:46 +00002403 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002404 }
2405
Sean Callanan549c9f72010-07-13 21:41:46 +00002406 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002407 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sean Callanan549c9f72010-07-13 21:41:46 +00002408
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002409 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callananafe16a72010-11-17 23:00:36 +00002410 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan549c9f72010-07-13 21:41:46 +00002411
Sean Callananafe16a72010-11-17 23:00:36 +00002412 if (!FirstEntryInstruction)
Sean Callanan3989fb92011-01-27 01:07:04 +00002413 {
2414 if (m_error_stream)
2415 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the first instruction in the wrapper for use in rewriting");
2416
Sean Callanan549c9f72010-07-13 21:41:46 +00002417 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002418 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002419
Sean Callanan79763a42011-05-23 21:40:23 +00002420 LLVMContext &context(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +00002421 IntegerType *offset_type(Type::getInt32Ty(context));
Sean Callanan549c9f72010-07-13 21:41:46 +00002422
2423 if (!offset_type)
Sean Callanan3989fb92011-01-27 01:07:04 +00002424 {
2425 if (m_error_stream)
2426 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
2427
Sean Callanan549c9f72010-07-13 21:41:46 +00002428 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002429 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002430
2431 for (element_index = 0; element_index < num_elements; ++element_index)
2432 {
Sean Callanan77eaf442011-07-08 00:39:14 +00002433 const clang::NamedDecl *decl = NULL;
2434 Value *value = NULL;
Sean Callanan549c9f72010-07-13 21:41:46 +00002435 off_t offset;
Greg Clayton7b462cc2010-10-15 22:48:33 +00002436 lldb_private::ConstString name;
Sean Callanan549c9f72010-07-13 21:41:46 +00002437
Sean Callanan823bb4c2010-08-30 22:17:16 +00002438 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan3989fb92011-01-27 01:07:04 +00002439 {
2440 if (m_error_stream)
2441 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
2442
Sean Callanan549c9f72010-07-13 21:41:46 +00002443 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002444 }
2445
Sean Callanan549c9f72010-07-13 21:41:46 +00002446 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002447 log->Printf(" \"%s\" (\"%s\") placed at %" PRId64,
Greg Clayton7b462cc2010-10-15 22:48:33 +00002448 name.GetCString(),
Sean Callananc70ed462011-10-25 18:36:40 +00002449 decl->getNameAsString().c_str(),
Sean Callanan549c9f72010-07-13 21:41:46 +00002450 offset);
2451
Sean Callanancc427fa2011-07-30 02:42:06 +00002452 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
Sean Callananafe16a72010-11-17 23:00:36 +00002453 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", FirstEntryInstruction);
Sean Callanan92adcac2011-01-13 08:53:35 +00002454
Sean Callananc70ed462011-10-25 18:36:40 +00002455 if (value)
Sean Callanan92adcac2011-01-13 08:53:35 +00002456 {
Sean Callananc70ed462011-10-25 18:36:40 +00002457 Value *replacement = NULL;
Sean Callanan92adcac2011-01-13 08:53:35 +00002458
Sean Callananc70ed462011-10-25 18:36:40 +00002459 if (log)
2460 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
Sean Callanan92adcac2011-01-13 08:53:35 +00002461
Sean Callananc70ed462011-10-25 18:36:40 +00002462 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2463 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2464 // entry in order to produce the static variable that the AST thinks it is accessing.
2465 if (name == m_result_name && !m_result_is_pointer)
2466 {
2467 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType()->getPointerTo(), "", FirstEntryInstruction);
2468
2469 LoadInst *load = new LoadInst(bit_cast, "", FirstEntryInstruction);
2470
2471 replacement = load;
2472 }
2473 else
2474 {
2475 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", FirstEntryInstruction);
2476
2477 replacement = bit_cast;
2478 }
2479
2480 if (Constant *constant = dyn_cast<Constant>(value))
2481 UnfoldConstant(constant, replacement, FirstEntryInstruction);
2482 else
2483 value->replaceAllUsesWith(replacement);
2484
2485 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2486 var->eraseFromParent();
2487 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002488 }
2489
2490 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00002491 log->Printf("Total structure [align %" PRId64 ", size %lu]", alignment, size);
Sean Callanan549c9f72010-07-13 21:41:46 +00002492
2493 return true;
2494}
2495
Sean Callanan79763a42011-05-23 21:40:23 +00002496llvm::Constant *
Greg Clayton5160ce52013-03-27 23:08:40 +00002497IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
Sean Callanan79763a42011-05-23 21:40:23 +00002498{
Sean Callanancc427fa2011-07-30 02:42:06 +00002499 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callanan95769bf2012-10-11 22:00:52 +00002500 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan79763a42011-05-23 21:40:23 +00002501
2502 llvm::Constant *offset_int = ConstantInt::get(intptr_ty, offset);
Sean Callanancc427fa2011-07-30 02:42:06 +00002503
2504 llvm::Constant *offset_array[1];
2505
2506 offset_array[0] = offset_int;
2507
2508 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
2509
2510 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, offsets);
Sean Callanan79763a42011-05-23 21:40:23 +00002511 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
2512
2513 return reloc_getbitcast;
2514}
2515
2516bool
2517IRForTarget::CompleteDataAllocation ()
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002518{
Greg Clayton5160ce52013-03-27 23:08:40 +00002519 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan79763a42011-05-23 21:40:23 +00002520
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002521 if (!m_data_allocator.GetStream().GetSize())
Sean Callanan79763a42011-05-23 21:40:23 +00002522 return true;
2523
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002524 lldb::addr_t allocation = m_data_allocator.Allocate();
Sean Callanan79763a42011-05-23 21:40:23 +00002525
2526 if (log)
2527 {
2528 if (allocation)
2529 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2530 else
2531 log->Printf("Failed to allocate static data");
2532 }
2533
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002534 if (!allocation || allocation == LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +00002535 return false;
2536
Sean Callanancc427fa2011-07-30 02:42:06 +00002537 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callanan95769bf2012-10-11 22:00:52 +00002538 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan79763a42011-05-23 21:40:23 +00002539
2540 Constant *relocated_addr = ConstantInt::get(intptr_ty, (uint64_t)allocation);
2541 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
2542
2543 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
2544
2545 m_reloc_placeholder->eraseFromParent();
2546
2547 return true;
2548}
2549
Sean Callanan2ab712f22010-07-03 01:35:46 +00002550bool
Sean Callanan3d654b32012-09-24 22:25:51 +00002551IRForTarget::StripAllGVs (Module &llvm_module)
2552{
Greg Clayton5160ce52013-03-27 23:08:40 +00002553 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3d654b32012-09-24 22:25:51 +00002554 std::vector<GlobalVariable *> global_vars;
2555 std::set<GlobalVariable *>erased_vars;
2556
2557 bool erased = true;
2558
2559 while (erased)
2560 {
2561 erased = false;
2562
2563 for (Module::global_iterator gi = llvm_module.global_begin(), ge = llvm_module.global_end();
2564 gi != ge;
2565 ++gi)
2566 {
2567 GlobalVariable *global_var = dyn_cast<GlobalVariable>(gi);
2568
2569 global_var->removeDeadConstantUsers();
2570
2571 if (global_var->use_empty())
2572 {
2573 if (log)
2574 log->Printf("Did remove %s",
2575 PrintValue(global_var).c_str());
2576 global_var->eraseFromParent();
2577 erased = true;
2578 break;
2579 }
2580 }
2581 }
2582
2583 for (Module::global_iterator gi = llvm_module.global_begin(), ge = llvm_module.global_end();
2584 gi != ge;
2585 ++gi)
2586 {
2587 GlobalVariable *global_var = dyn_cast<GlobalVariable>(gi);
2588
2589 GlobalValue::use_iterator ui = global_var->use_begin();
2590
Jim Inghamd77557d2012-11-26 19:54:04 +00002591 if (log)
2592 log->Printf("Couldn't remove %s because of %s",
2593 PrintValue(global_var).c_str(),
2594 PrintValue(*ui).c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +00002595 }
2596
2597 return true;
2598}
2599
2600bool
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002601IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002602{
Greg Clayton5160ce52013-03-27 23:08:40 +00002603 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan2ab712f22010-07-03 01:35:46 +00002604
Sean Callanan79763a42011-05-23 21:40:23 +00002605 m_module = &llvm_module;
Micah Villmow8468dbe2012-10-08 16:28:57 +00002606 m_target_data.reset(new DataLayout(m_module));
Sean Callanan79763a42011-05-23 21:40:23 +00002607
2608 Function* function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sean Callanan2ab712f22010-07-03 01:35:46 +00002609
2610 if (!function)
2611 {
2612 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002613 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sean Callanan3989fb92011-01-27 01:07:04 +00002614
2615 if (m_error_stream)
Sean Callanan79763a42011-05-23 21:40:23 +00002616 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
Sean Callanan92adcac2011-01-13 08:53:35 +00002617
Sean Callanan2ab712f22010-07-03 01:35:46 +00002618 return false;
2619 }
Sean Callanan79763a42011-05-23 21:40:23 +00002620
2621 if (!FixFunctionLinkage (*function))
2622 {
2623 if (log)
2624 log->Printf("Couldn't fix the linkage for the function");
2625
2626 return false;
2627 }
2628
Sean Callananc70ed462011-10-25 18:36:40 +00002629 if (log)
2630 {
2631 std::string s;
2632 raw_string_ostream oss(s);
2633
2634 m_module->print(oss, NULL);
2635
2636 oss.flush();
2637
2638 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
2639 }
2640
Sean Callanancc427fa2011-07-30 02:42:06 +00002641 llvm::Type *intptr_ty = Type::getInt8Ty(m_module->getContext());
Sean Callanan79763a42011-05-23 21:40:23 +00002642
2643 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
2644 intptr_ty,
Sean Callanan3d654b32012-09-24 22:25:51 +00002645 false /* IsConstant */,
Sean Callanan79763a42011-05-23 21:40:23 +00002646 GlobalVariable::InternalLinkage,
2647 Constant::getNullValue(intptr_ty),
2648 "reloc_placeholder",
2649 NULL /* InsertBefore */,
Sean Callanan3d654b32012-09-24 22:25:51 +00002650 GlobalVariable::NotThreadLocal /* ThreadLocal */,
Sean Callanan79763a42011-05-23 21:40:23 +00002651 0 /* AddressSpace */);
Sean Callanan2ab712f22010-07-03 01:35:46 +00002652
Sean Callanan7ea35012010-07-27 21:39:39 +00002653 Function::iterator bbi;
Sean Callanan2ab712f22010-07-03 01:35:46 +00002654
Sean Callanan79763a42011-05-23 21:40:23 +00002655 m_has_side_effects = HasSideEffects(*function);
Sean Callanane4ec90e2010-12-16 03:17:46 +00002656
Sean Callanand1e5b432010-08-12 01:56:52 +00002657 ////////////////////////////////////////////////////////////
Greg Clayton7b462cc2010-10-15 22:48:33 +00002658 // Replace $__lldb_expr_result with a persistent variable
Sean Callanand1e5b432010-08-12 01:56:52 +00002659 //
2660
Sean Callanan79763a42011-05-23 21:40:23 +00002661 if (!CreateResultVariable(*function))
Sean Callanan17827832010-12-13 22:46:15 +00002662 {
2663 if (log)
2664 log->Printf("CreateResultVariable() failed");
Sean Callanan3989fb92011-01-27 01:07:04 +00002665
2666 // CreateResultVariable() reports its own errors, so we don't do so here
2667
Sean Callanand1e5b432010-08-12 01:56:52 +00002668 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002669 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00002670
2671 for (bbi = function->begin();
2672 bbi != function->end();
2673 ++bbi)
2674 {
2675 if (!RemoveGuards(*bbi))
2676 {
2677 if (log)
2678 log->Printf("RemoveGuards() failed");
2679
2680 // RemoveGuards() reports its own errors, so we don't do so here
2681
2682 return false;
2683 }
2684
2685 if (!RewritePersistentAllocs(*bbi))
2686 {
2687 if (log)
2688 log->Printf("RewritePersistentAllocs() failed");
2689
2690 // RewritePersistentAllocs() reports its own errors, so we don't do so here
2691
2692 return false;
2693 }
Sean Callanan6e6d4a62012-07-21 02:02:15 +00002694
2695 if (!RemoveCXAAtExit(*bbi))
2696 {
2697 if (log)
2698 log->Printf("RemoveCXAAtExit() failed");
2699
2700 // RemoveCXAAtExit() reports its own errors, so we don't do so here
2701
2702 return false;
2703 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +00002704 }
2705
Sean Callananea685ae2011-11-01 17:33:54 +00002706 if (log && log->GetVerbose())
Sean Callanan79763a42011-05-23 21:40:23 +00002707 {
2708 std::string s;
2709 raw_string_ostream oss(s);
2710
2711 m_module->print(oss, NULL);
2712
2713 oss.flush();
2714
2715 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2716 }
2717
Sean Callananafe16a72010-11-17 23:00:36 +00002718 ///////////////////////////////////////////////////////////////////////////////
2719 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2720 //
Sean Callananafe16a72010-11-17 23:00:36 +00002721
Sean Callanan79763a42011-05-23 21:40:23 +00002722 if (!RewriteObjCConstStrings(*function))
Sean Callanan17827832010-12-13 22:46:15 +00002723 {
2724 if (log)
2725 log->Printf("RewriteObjCConstStrings() failed");
Sean Callanan3989fb92011-01-27 01:07:04 +00002726
2727 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
2728
Sean Callananafe16a72010-11-17 23:00:36 +00002729 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002730 }
Sean Callananafe16a72010-11-17 23:00:36 +00002731
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002732 ///////////////////////////////
2733 // Resolve function pointers
2734 //
2735
2736 if (!ResolveFunctionPointers(llvm_module, *function))
2737 {
2738 if (log)
2739 log->Printf("ResolveFunctionPointers() failed");
2740
2741 // ResolveFunctionPointers() reports its own errors, so we don't do so here
2742
2743 return false;
2744 }
2745
Sean Callanan2ab712f22010-07-03 01:35:46 +00002746 for (bbi = function->begin();
2747 bbi != function->end();
2748 ++bbi)
2749 {
Sean Callanan79763a42011-05-23 21:40:23 +00002750 if (!RewriteObjCSelectors(*bbi))
Sean Callanan17827832010-12-13 22:46:15 +00002751 {
2752 if (log)
2753 log->Printf("RewriteObjCSelectors() failed");
Sean Callanan3989fb92011-01-27 01:07:04 +00002754
2755 // RewriteObjCSelectors() reports its own errors, so we don't do so here
2756
Sean Callanan2235f322010-08-11 03:57:18 +00002757 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002758 }
Sean Callananbad134f2012-07-27 19:25:24 +00002759 }
Sean Callanan2235f322010-08-11 03:57:18 +00002760
Sean Callananbad134f2012-07-27 19:25:24 +00002761 for (bbi = function->begin();
2762 bbi != function->end();
2763 ++bbi)
2764 {
Sean Callanan79763a42011-05-23 21:40:23 +00002765 if (!ResolveCalls(*bbi))
Sean Callanan17827832010-12-13 22:46:15 +00002766 {
2767 if (log)
2768 log->Printf("ResolveCalls() failed");
Sean Callanan3989fb92011-01-27 01:07:04 +00002769
2770 // ResolveCalls() reports its own errors, so we don't do so here
2771
Sean Callanan549c9f72010-07-13 21:41:46 +00002772 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002773 }
Sean Callanan79763a42011-05-23 21:40:23 +00002774
2775 if (!ReplaceStaticLiterals(*bbi))
2776 {
2777 if (log)
2778 log->Printf("ReplaceStaticLiterals() failed");
2779
2780 return false;
2781 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002782 }
2783
Sean Callanan038df5032010-09-30 21:18:25 +00002784 ///////////////////////////////
2785 // Run function-level passes
2786 //
2787
Sean Callanan79763a42011-05-23 21:40:23 +00002788 if (!ResolveExternals(*function))
Sean Callanan17827832010-12-13 22:46:15 +00002789 {
2790 if (log)
2791 log->Printf("ResolveExternals() failed");
Sean Callanan3989fb92011-01-27 01:07:04 +00002792
2793 // ResolveExternals() reports its own errors, so we don't do so here
2794
Sean Callanana4e55172010-11-08 00:31:32 +00002795 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002796 }
Sean Callanana4e55172010-11-08 00:31:32 +00002797
Sean Callanan79763a42011-05-23 21:40:23 +00002798 if (!ReplaceVariables(*function))
Sean Callanan17827832010-12-13 22:46:15 +00002799 {
2800 if (log)
2801 log->Printf("ReplaceVariables() failed");
Sean Callanan3989fb92011-01-27 01:07:04 +00002802
2803 // ReplaceVariables() reports its own errors, so we don't do so here
2804
Sean Callanan038df5032010-09-30 21:18:25 +00002805 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002806 }
Sean Callanan038df5032010-09-30 21:18:25 +00002807
Sean Callanan79763a42011-05-23 21:40:23 +00002808 if (!ReplaceStrings())
2809 {
2810 if (log)
2811 log->Printf("ReplaceStrings() failed");
2812
2813 return false;
2814 }
2815
2816 if (!CompleteDataAllocation())
2817 {
2818 if (log)
2819 log->Printf("CompleteDataAllocation() failed");
2820
2821 return false;
2822 }
2823
Sean Callanan3d654b32012-09-24 22:25:51 +00002824 if (!StripAllGVs(llvm_module))
2825 {
2826 if (log)
2827 log->Printf("StripAllGVs() failed");
2828 }
2829
Sean Callananea685ae2011-11-01 17:33:54 +00002830 if (log && log->GetVerbose())
Sean Callanan549c9f72010-07-13 21:41:46 +00002831 {
Sean Callanancc54bd32010-07-28 01:00:59 +00002832 std::string s;
2833 raw_string_ostream oss(s);
Sean Callanan549c9f72010-07-13 21:41:46 +00002834
Sean Callanan79763a42011-05-23 21:40:23 +00002835 m_module->print(oss, NULL);
Sean Callanancc54bd32010-07-28 01:00:59 +00002836
2837 oss.flush();
2838
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002839 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan2ab712f22010-07-03 01:35:46 +00002840 }
2841
2842 return true;
2843}
2844
2845void
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002846IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002847{
2848}
2849
2850PassManagerType
2851IRForTarget::getPotentialPassManagerType() const
2852{
2853 return PMT_ModulePassManager;
2854}