blob: 5b98e2b6acb443d0e1f7400b43e56bb6664d81d6 [file] [log] [blame]
Sean Callanan47dc4572011-09-15 02:13:07 +00001//===-- IRForTarget.cpp -----------------------------------------*- C++ -*-===//
Sean Callanan5cf4a1c2010-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"
Sean Callanan2a8c3382011-04-14 02:01:31 +000013#include "llvm/Constants.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000014#include "llvm/InstrTypes.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000015#include "llvm/Instructions.h"
Sean Callanan3cb1fd82010-09-28 23:55:00 +000016#include "llvm/Intrinsics.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000017#include "llvm/Module.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000018#include "llvm/Target/TargetData.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000019#include "llvm/ValueSymbolTable.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000020
21#include "clang/AST/ASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000022
Greg Clayton8de27c72010-10-15 22:48:33 +000023#include "lldb/Core/ConstString.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000024#include "lldb/Core/dwarf.h"
25#include "lldb/Core/Log.h"
26#include "lldb/Core/Scalar.h"
27#include "lldb/Core/StreamString.h"
28#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan47dc4572011-09-15 02:13:07 +000029#include "lldb/Expression/IRInterpreter.h"
Sean Callananc0492742011-05-23 21:40:23 +000030#include "lldb/Host/Endian.h"
Sean Callanan696cf5f2011-05-07 01:06:41 +000031#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000032
33#include <map>
34
35using namespace llvm;
36
Sean Callanan3351dac2010-08-18 18:50:51 +000037static char ID;
38
Sean Callananc0492742011-05-23 21:40:23 +000039IRForTarget::StaticDataAllocator::StaticDataAllocator()
40{
41}
42
43IRForTarget::StaticDataAllocator::~StaticDataAllocator()
44{
45}
46
Greg Clayton3c7feb42010-11-19 01:05:25 +000047IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
48 bool resolve_vars,
Sean Callanan47dc4572011-09-15 02:13:07 +000049 lldb_private::ExecutionPolicy execution_policy,
Sean Callanan696cf5f2011-05-07 01:06:41 +000050 lldb::ClangExpressionVariableSP &const_result,
Sean Callananc0492742011-05-23 21:40:23 +000051 StaticDataAllocator *data_allocator,
Sean Callanan97c924e2011-01-27 01:07:04 +000052 lldb_private::Stream *error_stream,
Greg Clayton3c7feb42010-11-19 01:05:25 +000053 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +000054 ModulePass(ID),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000055 m_resolve_vars(resolve_vars),
Sean Callanan47dc4572011-09-15 02:13:07 +000056 m_execution_policy(execution_policy),
57 m_interpret_success(false),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000058 m_func_name(func_name),
Sean Callananc0492742011-05-23 21:40:23 +000059 m_module(NULL),
Johnny Chen2bc9eb32011-07-19 19:48:13 +000060 m_decl_map(decl_map),
61 m_data_allocator(data_allocator),
Sean Callanan6ba533e2010-11-17 23:00:36 +000062 m_CFStringCreateWithBytes(NULL),
Sean Callanan65dafa82010-08-27 01:01:44 +000063 m_sel_registerName(NULL),
Johnny Chen2bc9eb32011-07-19 19:48:13 +000064 m_const_result(const_result),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000065 m_error_stream(error_stream),
Sean Callanan6a925532011-01-13 08:53:35 +000066 m_has_side_effects(false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000067 m_result_store(NULL),
68 m_result_is_pointer(false),
Sean Callananc0492742011-05-23 21:40:23 +000069 m_reloc_placeholder(NULL)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000070{
71}
72
Sean Callanan771131d2010-09-30 21:18:25 +000073/* Handy utility functions used at several places in the code */
Sean Callanana48fe162010-08-11 03:57:18 +000074
75static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000076PrintValue(const Value *value, bool truncate = false)
Sean Callanana48fe162010-08-11 03:57:18 +000077{
78 std::string s;
79 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000080 value->print(rso);
Sean Callanana48fe162010-08-11 03:57:18 +000081 rso.flush();
82 if (truncate)
83 s.resize(s.length() - 1);
84 return s;
85}
86
Sean Callanan771131d2010-09-30 21:18:25 +000087static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000088PrintType(const Type *type, bool truncate = false)
Sean Callanan771131d2010-09-30 21:18:25 +000089{
90 std::string s;
91 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000092 type->print(rso);
Sean Callanan771131d2010-09-30 21:18:25 +000093 rso.flush();
94 if (truncate)
95 s.resize(s.length() - 1);
96 return s;
97}
98
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000099IRForTarget::~IRForTarget()
100{
101}
102
Sean Callananc0492742011-05-23 21:40:23 +0000103bool
104IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
105{
106 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
107
108 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
109
Sean Callananfecc09c2011-11-19 02:54:21 +0000110 std::string name = llvm_function.getName().str();
Sean Callananc0492742011-05-23 21:40:23 +0000111
112 return true;
113}
114
Sean Callanan82b74c82010-08-12 01:56:52 +0000115bool
Sean Callananc0492742011-05-23 21:40:23 +0000116IRForTarget::HasSideEffects (llvm::Function &llvm_function)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000117{
118 llvm::Function::iterator bbi;
119 BasicBlock::iterator ii;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000120
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000121 for (bbi = llvm_function.begin();
122 bbi != llvm_function.end();
123 ++bbi)
124 {
125 BasicBlock &basic_block = *bbi;
126
127 for (ii = basic_block.begin();
128 ii != basic_block.end();
129 ++ii)
130 {
131 switch (ii->getOpcode())
132 {
133 default:
134 return true;
135 case Instruction::Store:
136 {
137 StoreInst *store_inst = dyn_cast<StoreInst>(ii);
138
139 Value *store_ptr = store_inst->getPointerOperand();
140
Sean Callanan696cf5f2011-05-07 01:06:41 +0000141 std::string ptr_name;
142
143 if (store_ptr->hasName())
Sean Callananfecc09c2011-11-19 02:54:21 +0000144 ptr_name = store_ptr->getName().str();
Sean Callanan696cf5f2011-05-07 01:06:41 +0000145
146 if (isa <AllocaInst> (store_ptr))
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000147 break;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000148
149 if (ptr_name.find("$__lldb_expr_result") != std::string::npos)
150 {
151 if (ptr_name.find("GV") == std::string::npos)
152 m_result_store = store_inst;
153 }
154 else
155 {
156 return true;
157 }
158
159 break;
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000160 }
161 case Instruction::Load:
162 case Instruction::Alloca:
163 case Instruction::GetElementPtr:
Sean Callanan696cf5f2011-05-07 01:06:41 +0000164 case Instruction::BitCast:
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000165 case Instruction::Ret:
Sean Callanan696cf5f2011-05-07 01:06:41 +0000166 case Instruction::ICmp:
167 case Instruction::Br:
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000168 break;
169 }
170 }
171 }
172
173 return false;
174}
175
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000176bool
177IRForTarget::GetFunctionAddress (llvm::Function *fun,
178 uint64_t &fun_addr,
179 lldb_private::ConstString &name,
180 Constant **&value_ptr)
181{
182 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
183
184 fun_addr = LLDB_INVALID_ADDRESS;
185 name.Clear();
186 value_ptr = NULL;
187
188 if (fun->isIntrinsic())
189 {
190 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
191
192 switch (intrinsic_id)
193 {
194 default:
195 if (log)
196 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
197
198 if (m_error_stream)
199 m_error_stream->Printf("Internal error [IRForTarget]: Call to unhandled compiler intrinsic '%s'\n", Intrinsic::getName(intrinsic_id).c_str());
200
201 return false;
202 case Intrinsic::memcpy:
203 {
204 static lldb_private::ConstString g_memcpy_str ("memcpy");
205 name = g_memcpy_str;
206 }
207 break;
Sean Callanan7befa302011-11-16 00:20:50 +0000208 case Intrinsic::memset:
209 {
210 static lldb_private::ConstString g_memset_str ("memset");
211 name = g_memset_str;
212 }
213 break;
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000214 }
215
216 if (log && name)
217 log->Printf("Resolved intrinsic name \"%s\"", name.GetCString());
218 }
219 else
220 {
221 name.SetCStringWithLength (fun->getName().data(), fun->getName().size());
222 }
223
224 // Find the address of the function.
225
226 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000227
228 if (fun_decl)
229 {
Sean Callananc7ca4662011-10-25 18:36:40 +0000230 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_addr))
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000231 {
Greg Clayton9b98ffd2012-07-18 20:47:40 +0000232 lldb_private::ConstString altnernate_name;
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000233 bool found_it = m_decl_map->GetFunctionAddress (name, fun_addr);
234 if (!found_it)
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000235 {
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000236 // Check for an alternate mangling for "std::basic_string<char>"
237 // that is part of the itanium C++ name mangling scheme
238 const char *name_cstr = name.GetCString();
239 if (strncmp(name_cstr, "_ZNKSbIcE", strlen("_ZNKSbIcE")) == 0)
240 {
241 std::string alternate_mangling("_ZNKSs");
242 alternate_mangling.append (name_cstr + strlen("_ZNKSbIcE"));
Greg Clayton9b98ffd2012-07-18 20:47:40 +0000243 altnernate_name.SetCString(alternate_mangling.c_str());
244 found_it = m_decl_map->GetFunctionAddress (altnernate_name, fun_addr);
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000245 }
246 }
247
248 if (!found_it)
249 {
Greg Clayton9b98ffd2012-07-18 20:47:40 +0000250 lldb_private::Mangled mangled_name(name);
251 lldb_private::Mangled alt_mangled_name(altnernate_name);
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000252 if (log)
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000253 {
Greg Clayton9b98ffd2012-07-18 20:47:40 +0000254 if (alt_mangled_name)
255 log->Printf("Function \"%s\" (alternate name \"%s\") has no address",
256 mangled_name.GetName().GetCString(),
257 alt_mangled_name.GetName().GetCString());
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000258 else
Greg Clayton9b98ffd2012-07-18 20:47:40 +0000259 log->Printf("Function \"%s\" had no address",
260 mangled_name.GetName().GetCString());
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000261 }
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000262
263 if (m_error_stream)
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000264 {
Greg Clayton9b98ffd2012-07-18 20:47:40 +0000265 if (alt_mangled_name)
266 m_error_stream->Printf("error: call to a function '%s' (alternate name '%s') that is not present in the target\n",
267 mangled_name.GetName().GetCString(),
268 alt_mangled_name.GetName().GetCString());
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000269 else
Greg Clayton9b98ffd2012-07-18 20:47:40 +0000270 m_error_stream->Printf("error: call to a function '%s' that is not present in the target\n",
271 mangled_name.GetName().GetCString());
Greg Claytonf6a5bd72011-10-22 03:33:13 +0000272 }
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000273 return false;
274 }
275 }
276 }
277 else
278 {
279 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
280 {
281 if (log)
282 log->Printf ("Metadataless function \"%s\" had no address", name.GetCString());
283
284 if (m_error_stream)
285 m_error_stream->Printf("Error [IRForTarget]: Call to a symbol-only function '%s' that is not present in the target\n", name.GetCString());
286
287 return false;
288 }
289 }
290
291 if (log)
292 log->Printf("Found \"%s\" at 0x%llx", name.GetCString(), fun_addr);
293
294 return true;
295}
296
297llvm::Constant *
298IRForTarget::BuildFunctionPointer (llvm::Type *type,
299 uint64_t ptr)
300{
301 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
302 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
303 PointerType *fun_ptr_ty = PointerType::getUnqual(type);
304 Constant *fun_addr_int = ConstantInt::get(intptr_ty, ptr, false);
305 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
306}
307
Sean Callanan715f6b02011-10-31 22:11:40 +0000308void
309IRForTarget::RegisterFunctionMetadata(LLVMContext &context,
310 llvm::Value *function_ptr,
311 const char *name)
312{
313 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
314
315 for (Value::use_iterator i = function_ptr->use_begin(), e = function_ptr->use_end();
316 i != e;
317 ++i)
318 {
319 Value *user = *i;
320
321 if (Instruction *user_inst = dyn_cast<Instruction>(user))
322 {
Sean Callanan39a30342012-02-09 03:22:41 +0000323 Constant *name_array = ConstantDataArray::getString(context, StringRef(name));
Sean Callanan715f6b02011-10-31 22:11:40 +0000324
325 ArrayRef<Value *> md_values(name_array);
326
327 MDNode *metadata = MDNode::get(context, md_values);
328
329 user_inst->setMetadata("lldb.call.realName", metadata);
330 }
331 else
332 {
333 RegisterFunctionMetadata (context, user, name);
334 }
335 }
336}
337
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000338bool
339IRForTarget::ResolveFunctionPointers(llvm::Module &llvm_module,
340 llvm::Function &llvm_function)
341{
342 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
343
344 for (llvm::Module::iterator fi = llvm_module.begin();
345 fi != llvm_module.end();
346 ++fi)
347 {
348 Function *fun = fi;
349
350 bool is_decl = fun->isDeclaration();
351
352 if (log)
Sean Callananfecc09c2011-11-19 02:54:21 +0000353 log->Printf("Examining %s function %s", (is_decl ? "declaration" : "non-declaration"), fun->getName().str().c_str());
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000354
355 if (!is_decl)
356 continue;
357
358 if (fun->hasNUses(0))
359 continue; // ignore
360
361 uint64_t addr = LLDB_INVALID_ADDRESS;
362 lldb_private::ConstString name;
363 Constant **value_ptr = NULL;
364
365 if (!GetFunctionAddress(fun,
366 addr,
367 name,
368 value_ptr))
369 return false; // GetFunctionAddress reports its own errors
370
371 Constant *value = BuildFunctionPointer(fun->getFunctionType(), addr);
372
Sean Callanan715f6b02011-10-31 22:11:40 +0000373 RegisterFunctionMetadata (llvm_module.getContext(), fun, name.AsCString());
374
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000375 if (value_ptr)
376 *value_ptr = value;
377
378 fun->replaceAllUsesWith(value);
379 }
380
381 return true;
382}
383
Sean Callanan47dc4572011-09-15 02:13:07 +0000384
Sean Callanan696cf5f2011-05-07 01:06:41 +0000385clang::NamedDecl *
Sean Callanan47dc4572011-09-15 02:13:07 +0000386IRForTarget::DeclForGlobal (const GlobalValue *global_val, Module *module)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000387{
388 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
389
Sean Callanan47dc4572011-09-15 02:13:07 +0000390 NamedMDNode *named_metadata = module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan696cf5f2011-05-07 01:06:41 +0000391
392 if (!named_metadata)
393 return NULL;
394
395 unsigned num_nodes = named_metadata->getNumOperands();
396 unsigned node_index;
397
398 for (node_index = 0;
399 node_index < num_nodes;
400 ++node_index)
401 {
402 MDNode *metadata_node = named_metadata->getOperand(node_index);
403
404 if (!metadata_node)
405 return NULL;
406
407 if (metadata_node->getNumOperands() != 2)
408 continue;
409
410 if (metadata_node->getOperand(0) != global_val)
411 continue;
412
413 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
414
415 if (!constant_int)
416 return NULL;
417
418 uintptr_t ptr = constant_int->getZExtValue();
419
420 return reinterpret_cast<clang::NamedDecl *>(ptr);
421 }
422
423 return NULL;
424}
425
Sean Callanan47dc4572011-09-15 02:13:07 +0000426clang::NamedDecl *
427IRForTarget::DeclForGlobal (GlobalValue *global_val)
428{
429 return DeclForGlobal(global_val, m_module);
430}
431
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000432void
433IRForTarget::MaybeSetConstantResult (llvm::Constant *initializer,
434 const lldb_private::ConstString &name,
435 lldb_private::TypeFromParser type)
436{
Sean Callanan696cf5f2011-05-07 01:06:41 +0000437 if (llvm::ConstantExpr *init_expr = dyn_cast<llvm::ConstantExpr>(initializer))
438 {
439 switch (init_expr->getOpcode())
440 {
441 default:
442 return;
443 case Instruction::IntToPtr:
444 MaybeSetConstantResult (init_expr->getOperand(0), name, type);
445 return;
446 }
447 }
448 else if (llvm::ConstantInt *init_int = dyn_cast<llvm::ConstantInt>(initializer))
449 {
450 m_const_result = m_decl_map->BuildIntegerVariable(name, type, init_int->getValue());
451 }
452}
453
454void
Sean Callananc0492742011-05-23 21:40:23 +0000455IRForTarget::MaybeSetCastResult (lldb_private::TypeFromParser type)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000456{
457 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
458
459 if (!m_result_store)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000460 return;
461
Sean Callanan696cf5f2011-05-07 01:06:41 +0000462 LoadInst *original_load = NULL;
463
464 for (llvm::Value *current_value = m_result_store->getValueOperand(), *next_value;
465 current_value != NULL;
466 current_value = next_value)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000467 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000468 CastInst *cast_inst = dyn_cast<CastInst>(current_value);
469 LoadInst *load_inst = dyn_cast<LoadInst>(current_value);
470
471 if (cast_inst)
472 {
473 next_value = cast_inst->getOperand(0);
474 }
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000475 else if (load_inst)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000476 {
477 if (isa<LoadInst>(load_inst->getPointerOperand()))
478 {
479 next_value = load_inst->getPointerOperand();
480 }
481 else
482 {
483 original_load = load_inst;
484 break;
485 }
486 }
487 else
488 {
489 return;
490 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000491 }
Sean Callanan696cf5f2011-05-07 01:06:41 +0000492
493 Value *loaded_value = original_load->getPointerOperand();
494 GlobalVariable *loaded_global = dyn_cast<GlobalVariable>(loaded_value);
495
496 if (!loaded_global)
497 return;
498
Sean Callananc0492742011-05-23 21:40:23 +0000499 clang::NamedDecl *loaded_decl = DeclForGlobal(loaded_global);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000500
501 if (!loaded_decl)
502 return;
503
504 clang::VarDecl *loaded_var = dyn_cast<clang::VarDecl>(loaded_decl);
505
506 if (!loaded_var)
507 return;
508
509 if (log)
510 {
511 lldb_private::StreamString type_desc_stream;
512 type.DumpTypeDescription(&type_desc_stream);
513
514 log->Printf("Type to cast variable to: \"%s\"", type_desc_stream.GetString().c_str());
515 }
516
517 m_const_result = m_decl_map->BuildCastVariable(m_result_name, loaded_var, type);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000518}
519
520bool
Sean Callananc0492742011-05-23 21:40:23 +0000521IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanan82b74c82010-08-12 01:56:52 +0000522{
Greg Claytone005f2c2010-11-06 01:53:30 +0000523 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan82b74c82010-08-12 01:56:52 +0000524
Sean Callanane8a59a82010-09-13 21:34:21 +0000525 if (!m_resolve_vars)
526 return true;
527
528 // Find the result variable. If it doesn't exist, we can give up right here.
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000529
Sean Callananc0492742011-05-23 21:40:23 +0000530 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000531
Sean Callanan9b6898f2011-07-30 02:42:06 +0000532 std::string result_name_str;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000533 const char *result_name = NULL;
534
535 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
536 vi != ve;
537 ++vi)
538 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000539 result_name_str = vi->first().str();
540 const char *value_name = result_name_str.c_str();
541
542 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
543 !strstr(value_name, "GV"))
Sean Callanan6a925532011-01-13 08:53:35 +0000544 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000545 result_name = value_name;
Sean Callanan6a925532011-01-13 08:53:35 +0000546 m_result_is_pointer = true;
547 break;
548 }
549
Sean Callanan9b6898f2011-07-30 02:42:06 +0000550 if (strstr(value_name, "$__lldb_expr_result") &&
551 !strstr(value_name, "GV"))
Sean Callananc04743d2010-09-28 21:13:03 +0000552 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000553 result_name = value_name;
Sean Callanan6a925532011-01-13 08:53:35 +0000554 m_result_is_pointer = false;
Sean Callananc04743d2010-09-28 21:13:03 +0000555 break;
556 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000557 }
558
559 if (!result_name)
560 {
561 if (log)
562 log->PutCString("Couldn't find result variable");
563
564 return true;
565 }
566
Sean Callananc04743d2010-09-28 21:13:03 +0000567 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000568 log->Printf("Result name: \"%s\"", result_name);
Sean Callananc04743d2010-09-28 21:13:03 +0000569
Sean Callananc0492742011-05-23 21:40:23 +0000570 Value *result_value = m_module->getNamedValue(result_name);
Sean Callanan82b74c82010-08-12 01:56:52 +0000571
572 if (!result_value)
573 {
574 if (log)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000575 log->PutCString("Result variable had no data");
Sean Callanan6a925532011-01-13 08:53:35 +0000576
Sean Callanan97c924e2011-01-27 01:07:04 +0000577 if (m_error_stream)
578 m_error_stream->Printf("Internal error [IRForTarget]: Result variable's name (%s) exists, but not its definition\n", result_name);
579
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000580 return false;
Sean Callanan82b74c82010-08-12 01:56:52 +0000581 }
Sean Callanane8a59a82010-09-13 21:34:21 +0000582
Sean Callanan82b74c82010-08-12 01:56:52 +0000583 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000584 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sean Callanan82b74c82010-08-12 01:56:52 +0000585
586 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
587
588 if (!result_global)
589 {
590 if (log)
591 log->PutCString("Result variable isn't a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000592
593 if (m_error_stream)
594 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) is defined, but is not a global variable\n", result_name);
595
Sean Callanan82b74c82010-08-12 01:56:52 +0000596 return false;
597 }
598
Sean Callananc0492742011-05-23 21:40:23 +0000599 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000600 if (!result_decl)
Sean Callanan82b74c82010-08-12 01:56:52 +0000601 {
602 if (log)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000603 log->PutCString("Result variable doesn't have a corresponding Decl");
Sean Callanan82b74c82010-08-12 01:56:52 +0000604
Sean Callanan97c924e2011-01-27 01:07:04 +0000605 if (m_error_stream)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000606 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) does not have a corresponding Clang entity\n", result_name);
Sean Callanan97c924e2011-01-27 01:07:04 +0000607
Sean Callanan82b74c82010-08-12 01:56:52 +0000608 return false;
609 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000610
Sean Callanan696cf5f2011-05-07 01:06:41 +0000611 if (log)
Sean Callanan82b74c82010-08-12 01:56:52 +0000612 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000613 std::string decl_desc_str;
614 raw_string_ostream decl_desc_stream(decl_desc_str);
615 result_decl->print(decl_desc_stream);
616 decl_desc_stream.flush();
Sean Callanan82b74c82010-08-12 01:56:52 +0000617
Sean Callanan696cf5f2011-05-07 01:06:41 +0000618 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanan82b74c82010-08-12 01:56:52 +0000619 }
620
Sean Callanan696cf5f2011-05-07 01:06:41 +0000621 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
622 if (!result_var)
Sean Callanan82b74c82010-08-12 01:56:52 +0000623 {
624 if (log)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000625 log->PutCString("Result variable Decl isn't a VarDecl");
Sean Callanan97c924e2011-01-27 01:07:04 +0000626
627 if (m_error_stream)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000628 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s)'s corresponding Clang entity isn't a variable\n", result_name);
Sean Callanan97c924e2011-01-27 01:07:04 +0000629
Sean Callanan82b74c82010-08-12 01:56:52 +0000630 return false;
631 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000632
Sean Callanan82b74c82010-08-12 01:56:52 +0000633 // Get the next available result name from m_decl_map and create the persistent
634 // variable for it
Sean Callanan47dc4572011-09-15 02:13:07 +0000635
Sean Callanan696cf5f2011-05-07 01:06:41 +0000636 // If the result is an Lvalue, it is emitted as a pointer; see
637 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan6a925532011-01-13 08:53:35 +0000638 if (m_result_is_pointer)
639 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000640 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanand5b3c352011-01-27 04:42:51 +0000641 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
Sean Callanan6a925532011-01-13 08:53:35 +0000642
Sean Callanan21f2e192011-12-14 01:13:04 +0000643 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
644 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
Sean Callanan4fdf7952011-12-08 19:04:34 +0000645
646 if (pointer_pointertype)
647 {
648 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
649
650 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
651 &result_decl->getASTContext());
652 }
653 else if (pointer_objcobjpointertype)
654 {
655 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
656
657 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
658 &result_decl->getASTContext());
659 }
660 else
Sean Callanan6a925532011-01-13 08:53:35 +0000661 {
662 if (log)
663 log->PutCString("Expected result to have pointer type, but it did not");
Sean Callanan97c924e2011-01-27 01:07:04 +0000664
665 if (m_error_stream)
666 m_error_stream->Printf("Internal error [IRForTarget]: Lvalue result (%s) is not a pointer variable\n", result_name);
667
Sean Callanan6a925532011-01-13 08:53:35 +0000668 return false;
669 }
Sean Callanan6a925532011-01-13 08:53:35 +0000670 }
671 else
672 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000673 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000674 &result_decl->getASTContext());
675 }
676
677 if (m_result_type.GetClangTypeBitWidth() == 0)
678 {
679 lldb_private::StreamString type_desc_stream;
680 m_result_type.DumpTypeDescription(&type_desc_stream);
681
682 if (log)
683 log->Printf("Result type has size 0");
684
685 if (m_error_stream)
Sean Callananb6f9ee72011-12-21 23:44:05 +0000686 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000687 type_desc_stream.GetData());
Sean Callananb6f9ee72011-12-21 23:44:05 +0000688 return false;
Sean Callanan6a925532011-01-13 08:53:35 +0000689 }
690
Sean Callanan696cf5f2011-05-07 01:06:41 +0000691 if (log)
692 {
693 lldb_private::StreamString type_desc_stream;
Sean Callanan47dc4572011-09-15 02:13:07 +0000694 m_result_type.DumpTypeDescription(&type_desc_stream);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000695
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000696 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
Sean Callanan696cf5f2011-05-07 01:06:41 +0000697 }
698
Sean Callanan6a925532011-01-13 08:53:35 +0000699 m_result_name = m_decl_map->GetPersistentResultName();
Sean Callanan82b74c82010-08-12 01:56:52 +0000700
701 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000702 log->Printf("Creating a new result global: \"%s\" with size 0x%x",
703 m_result_name.GetCString(),
704 m_result_type.GetClangTypeBitWidth() / 8);
Sean Callanan82b74c82010-08-12 01:56:52 +0000705
706 // Construct a new result global and set up its metadata
707
Sean Callananc0492742011-05-23 21:40:23 +0000708 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanan82b74c82010-08-12 01:56:52 +0000709 result_global->getType()->getElementType(),
710 false, /* not constant */
711 GlobalValue::ExternalLinkage,
712 NULL, /* no initializer */
Sean Callanan6a925532011-01-13 08:53:35 +0000713 m_result_name.GetCString ());
Sean Callanan82b74c82010-08-12 01:56:52 +0000714
715 // It's too late in compilation to create a new VarDecl for this, but we don't
716 // need to. We point the metadata at the old VarDecl. This creates an odd
717 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton8de27c72010-10-15 22:48:33 +0000718 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanan82b74c82010-08-12 01:56:52 +0000719 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
720 // fixed up.
721
Sean Callananc0492742011-05-23 21:40:23 +0000722 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan696cf5f2011-05-07 01:06:41 +0000723 reinterpret_cast<uint64_t>(result_decl),
Sean Callanan82b74c82010-08-12 01:56:52 +0000724 false);
725
726 llvm::Value* values[2];
727 values[0] = new_result_global;
728 values[1] = new_constant_int;
729
Sean Callanan0de254a2011-05-15 22:34:38 +0000730 ArrayRef<Value*> value_ref(values, 2);
731
Sean Callananc0492742011-05-23 21:40:23 +0000732 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
733 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan82b74c82010-08-12 01:56:52 +0000734 named_metadata->addOperand(persistent_global_md);
735
736 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000737 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan2e2db532010-09-07 22:43:19 +0000738 PrintValue(result_global).c_str(),
Sean Callanan82b74c82010-08-12 01:56:52 +0000739 PrintValue(new_result_global).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000740
741 if (result_global->hasNUses(0))
742 {
743 // We need to synthesize a store for this variable, because otherwise
744 // there's nothing to put into its equivalent persistent variable.
Sean Callanan82b74c82010-08-12 01:56:52 +0000745
Greg Clayton8de27c72010-10-15 22:48:33 +0000746 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan2e2db532010-09-07 22:43:19 +0000747 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
748
749 if (!first_entry_instruction)
750 return false;
751
752 if (!result_global->hasInitializer())
753 {
754 if (log)
755 log->Printf("Couldn't find initializer for unused variable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000756
757 if (m_error_stream)
758 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) has no writes and no initializer\n", result_name);
759
Sean Callanan2e2db532010-09-07 22:43:19 +0000760 return false;
761 }
762
763 Constant *initializer = result_global->getInitializer();
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000764
765 // Here we write the initializer into a result variable assuming it
766 // can be computed statically.
767
768 if (!m_has_side_effects)
769 {
Sean Callanan557ccd62011-10-21 05:18:02 +0000770 //MaybeSetConstantResult (initializer,
771 // m_result_name,
772 // m_result_type);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000773 }
Sean Callanan2e2db532010-09-07 22:43:19 +0000774
Greg Clayton2c344ca2011-02-05 02:28:58 +0000775 StoreInst *synthesized_store = new StoreInst(initializer,
776 new_result_global,
777 first_entry_instruction);
Sean Callanan2e2db532010-09-07 22:43:19 +0000778
779 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000780 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000781 }
782 else
783 {
Sean Callanan47dc4572011-09-15 02:13:07 +0000784 if (!m_has_side_effects && lldb_private::ClangASTContext::IsPointerType (m_result_type.GetOpaqueQualType()))
Sean Callanan696cf5f2011-05-07 01:06:41 +0000785 {
Sean Callanan557ccd62011-10-21 05:18:02 +0000786 //MaybeSetCastResult (m_result_type);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000787 }
788
Sean Callanan2e2db532010-09-07 22:43:19 +0000789 result_global->replaceAllUsesWith(new_result_global);
790 }
Sean Callanan696cf5f2011-05-07 01:06:41 +0000791
792 if (!m_const_result)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000793 if (!m_decl_map->AddPersistentVariable(result_decl,
794 m_result_name,
795 m_result_type,
796 true,
797 m_result_is_pointer))
798 return false;
Sean Callanan2e2db532010-09-07 22:43:19 +0000799
Sean Callanan82b74c82010-08-12 01:56:52 +0000800 result_global->eraseFromParent();
801
802 return true;
803}
804
Johnny Chen58021cc2011-08-09 23:10:20 +0000805#if 0
Greg Clayton3c7feb42010-11-19 01:05:25 +0000806static void DebugUsers(lldb::LogSP &log, Value *value, uint8_t depth)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000807{
808 if (!depth)
809 return;
810
811 depth--;
812
Johnny Chen58021cc2011-08-09 23:10:20 +0000813 if (log)
814 log->Printf(" <Begin %d users>", value->getNumUses());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000815
Greg Clayton3c7feb42010-11-19 01:05:25 +0000816 for (Value::use_iterator ui = value->use_begin(), ue = value->use_end();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000817 ui != ue;
818 ++ui)
819 {
Johnny Chen58021cc2011-08-09 23:10:20 +0000820 if (log)
821 log->Printf(" <Use %p> %s", *ui, PrintValue(*ui).c_str());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000822 DebugUsers(log, *ui, depth);
823 }
824
Johnny Chen58021cc2011-08-09 23:10:20 +0000825 if (log)
826 log->Printf(" <End uses>");
Sean Callanan6ba533e2010-11-17 23:00:36 +0000827}
Johnny Chen58021cc2011-08-09 23:10:20 +0000828#endif
Sean Callanan6ba533e2010-11-17 23:00:36 +0000829
830bool
Sean Callananc0492742011-05-23 21:40:23 +0000831IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Greg Clayton3c7feb42010-11-19 01:05:25 +0000832 llvm::GlobalVariable *cstr,
833 Instruction *FirstEntryInstruction)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000834{
835 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
836
Sean Callanan9b6898f2011-07-30 02:42:06 +0000837 Type *ns_str_ty = ns_str->getType();
Sean Callananc0492742011-05-23 21:40:23 +0000838
Sean Callanan9b6898f2011-07-30 02:42:06 +0000839 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
840 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callananc0492742011-05-23 21:40:23 +0000841 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan9b6898f2011-07-30 02:42:06 +0000842 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
843 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000844
845 if (!m_CFStringCreateWithBytes)
846 {
847 lldb::addr_t CFStringCreateWithBytes_addr;
848
849 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
850
851 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
852 {
853 if (log)
854 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
855
Sean Callanan97c924e2011-01-27 01:07:04 +0000856 if (m_error_stream)
857 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
858
Sean Callanan6ba533e2010-11-17 23:00:36 +0000859 return false;
860 }
861
862 if (log)
863 log->Printf("Found CFStringCreateWithBytes at 0x%llx", CFStringCreateWithBytes_addr);
864
865 // Build the function type:
866 //
867 // CFStringRef CFStringCreateWithBytes (
868 // CFAllocatorRef alloc,
869 // const UInt8 *bytes,
870 // CFIndex numBytes,
871 // CFStringEncoding encoding,
872 // Boolean isExternalRepresentation
873 // );
874 //
875 // We make the following substitutions:
876 //
877 // CFStringRef -> i8*
878 // CFAllocatorRef -> i8*
879 // UInt8 * -> i8*
880 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
881 // CFStringEncoding -> i32
882 // Boolean -> i8
883
Sean Callanan9b6898f2011-07-30 02:42:06 +0000884 Type *arg_type_array[5];
885
886 arg_type_array[0] = i8_ptr_ty;
887 arg_type_array[1] = i8_ptr_ty;
888 arg_type_array[2] = intptr_ty;
889 arg_type_array[3] = i32_ty;
890 arg_type_array[4] = i8_ty;
891
892 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
893
Sean Callananc0492742011-05-23 21:40:23 +0000894 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000895
896 // Build the constant containing the pointer to the function
897 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
898 Constant *CFSCWB_addr_int = ConstantInt::get(intptr_ty, CFStringCreateWithBytes_addr, false);
899 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
900 }
901
Sean Callanan39a30342012-02-09 03:22:41 +0000902 ConstantDataSequential *string_array = NULL;
Sean Callanan0ece5262011-02-10 22:17:53 +0000903
904 if (cstr)
Sean Callanan39a30342012-02-09 03:22:41 +0000905 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
Sean Callanan9b6898f2011-07-30 02:42:06 +0000906
Sean Callanan6ba533e2010-11-17 23:00:36 +0000907 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan0ece5262011-02-10 22:17:53 +0000908 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
Sean Callanan39a30342012-02-09 03:22:41 +0000909 Constant *numBytes_arg = ConstantInt::get(intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000910 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
911 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
912
Sean Callanan9b6898f2011-07-30 02:42:06 +0000913 Value *argument_array[5];
914
915 argument_array[0] = alloc_arg;
916 argument_array[1] = bytes_arg;
917 argument_array[2] = numBytes_arg;
918 argument_array[3] = encoding_arg;
919 argument_array[4] = isExternal_arg;
920
921 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000922
923 CallInst *CFSCWB_call = CallInst::Create(m_CFStringCreateWithBytes,
Sean Callanan9b6898f2011-07-30 02:42:06 +0000924 CFSCWB_arguments,
Sean Callanan6ba533e2010-11-17 23:00:36 +0000925 "CFStringCreateWithBytes",
926 FirstEntryInstruction);
Sean Callananae71e302010-11-18 22:21:58 +0000927
Greg Clayton3c7feb42010-11-19 01:05:25 +0000928 if (!UnfoldConstant(ns_str, CFSCWB_call, FirstEntryInstruction))
Sean Callanan6ba533e2010-11-17 23:00:36 +0000929 {
930 if (log)
931 log->PutCString("Couldn't replace the NSString with the result of the call");
932
Sean Callanan97c924e2011-01-27 01:07:04 +0000933 if (m_error_stream)
934 m_error_stream->Printf("Error [IRForTarget]: Couldn't replace an Objective-C constant string with a dynamic string\n");
935
Sean Callanan6ba533e2010-11-17 23:00:36 +0000936 return false;
937 }
938
Greg Clayton3c7feb42010-11-19 01:05:25 +0000939 ns_str->eraseFromParent();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000940
941 return true;
942}
943
944bool
Sean Callananc0492742011-05-23 21:40:23 +0000945IRForTarget::RewriteObjCConstStrings(Function &llvm_function)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000946{
947 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
948
Sean Callananc0492742011-05-23 21:40:23 +0000949 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000950
Greg Clayton3c7feb42010-11-19 01:05:25 +0000951 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000952 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
953
954 if (!FirstEntryInstruction)
955 {
956 if (log)
957 log->PutCString("Couldn't find first instruction for rewritten Objective-C strings");
958
Sean Callanan97c924e2011-01-27 01:07:04 +0000959 if (m_error_stream)
960 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the location for calls to CFStringCreateWithBytes\n");
961
Sean Callanan6ba533e2010-11-17 23:00:36 +0000962 return false;
963 }
964
965 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
966 vi != ve;
967 ++vi)
968 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000969 std::string value_name = vi->first().str();
970 const char *value_name_cstr = value_name.c_str();
971
972 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callanan6ba533e2010-11-17 23:00:36 +0000973 {
974 Value *nsstring_value = vi->second;
975
976 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
977
978 if (!nsstring_global)
979 {
980 if (log)
981 log->PutCString("NSString variable is not a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000982
983 if (m_error_stream)
984 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a global variable\n");
985
Sean Callanan6ba533e2010-11-17 23:00:36 +0000986 return false;
987 }
988
989 if (!nsstring_global->hasInitializer())
990 {
991 if (log)
992 log->PutCString("NSString variable does not have an initializer");
Sean Callanan97c924e2011-01-27 01:07:04 +0000993
994 if (m_error_stream)
995 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have an initializer\n");
996
Sean Callanan6ba533e2010-11-17 23:00:36 +0000997 return false;
998 }
999
1000 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
1001
1002 if (!nsstring_struct)
1003 {
1004 if (log)
1005 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sean Callanan97c924e2011-01-27 01:07:04 +00001006
1007 if (m_error_stream)
1008 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a structure constant\n");
1009
Sean Callanan6ba533e2010-11-17 23:00:36 +00001010 return false;
1011 }
1012
1013 // We expect the following structure:
1014 //
1015 // struct {
1016 // int *isa;
1017 // int flags;
1018 // char *str;
1019 // long length;
1020 // };
1021
1022 if (nsstring_struct->getNumOperands() != 4)
1023 {
1024 if (log)
1025 log->Printf("NSString variable's initializer structure has an unexpected number of members. Should be 4, is %d", nsstring_struct->getNumOperands());
Sean Callanan97c924e2011-01-27 01:07:04 +00001026
1027 if (m_error_stream)
1028 m_error_stream->Printf("Internal error [IRForTarget]: The struct for an Objective-C constant string is not as expected\n");
1029
Sean Callanan6ba533e2010-11-17 23:00:36 +00001030 return false;
1031 }
1032
1033 Constant *nsstring_member = nsstring_struct->getOperand(2);
1034
1035 if (!nsstring_member)
1036 {
1037 if (log)
1038 log->PutCString("NSString initializer's str element was empty");
Sean Callanan97c924e2011-01-27 01:07:04 +00001039
1040 if (m_error_stream)
1041 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have a string initializer\n");
1042
Sean Callanan6ba533e2010-11-17 23:00:36 +00001043 return false;
1044 }
1045
1046 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
1047
1048 if (!nsstring_expr)
1049 {
1050 if (log)
1051 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sean Callanan97c924e2011-01-27 01:07:04 +00001052
1053 if (m_error_stream)
1054 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not constant\n");
1055
Sean Callanan6ba533e2010-11-17 23:00:36 +00001056 return false;
1057 }
1058
1059 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
1060 {
1061 if (log)
1062 log->Printf("NSString initializer's str element is not a GetElementPtr expression, it's a %s", nsstring_expr->getOpcodeName());
Sean Callanan97c924e2011-01-27 01:07:04 +00001063
1064 if (m_error_stream)
1065 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not an array\n");
1066
Sean Callanan6ba533e2010-11-17 23:00:36 +00001067 return false;
1068 }
1069
1070 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
1071
1072 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
1073
1074 if (!cstr_global)
1075 {
1076 if (log)
1077 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +00001078
1079 if (m_error_stream)
1080 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a global\n");
Sean Callanan65e2aee2010-11-20 02:06:01 +00001081
Sean Callanan6ba533e2010-11-17 23:00:36 +00001082 return false;
1083 }
1084
1085 if (!cstr_global->hasInitializer())
1086 {
1087 if (log)
1088 log->PutCString("NSString initializer's str element does not have an initializer");
Sean Callanan97c924e2011-01-27 01:07:04 +00001089
1090 if (m_error_stream)
1091 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to initialized data\n");
1092
Sean Callanan6ba533e2010-11-17 23:00:36 +00001093 return false;
1094 }
Sean Callanan0ece5262011-02-10 22:17:53 +00001095
1096 /*
Sean Callanan6ba533e2010-11-17 23:00:36 +00001097 if (!cstr_array)
1098 {
1099 if (log)
1100 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sean Callanan97c924e2011-01-27 01:07:04 +00001101
1102 if (m_error_stream)
1103 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to an array\n");
1104
Sean Callanan6ba533e2010-11-17 23:00:36 +00001105 return false;
1106 }
1107
1108 if (!cstr_array->isCString())
1109 {
1110 if (log)
1111 log->PutCString("NSString initializer's str element is not a C string array");
Sean Callanan97c924e2011-01-27 01:07:04 +00001112
1113 if (m_error_stream)
1114 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a C string\n");
1115
Sean Callanan6ba533e2010-11-17 23:00:36 +00001116 return false;
1117 }
Sean Callanan0ece5262011-02-10 22:17:53 +00001118 */
1119
Sean Callanan39a30342012-02-09 03:22:41 +00001120 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
Sean Callanan6ba533e2010-11-17 23:00:36 +00001121
1122 if (log)
Sean Callanan0ece5262011-02-10 22:17:53 +00001123 {
1124 if (cstr_array)
Sean Callanan39a30342012-02-09 03:22:41 +00001125 log->Printf("Found NSString constant %s, which contains \"%s\"", value_name_cstr, cstr_array->getAsString().str().c_str());
Sean Callanan0ece5262011-02-10 22:17:53 +00001126 else
Sean Callanan9b6898f2011-07-30 02:42:06 +00001127 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan0ece5262011-02-10 22:17:53 +00001128 }
1129
1130 if (!cstr_array)
1131 cstr_global = NULL;
Sean Callanan6ba533e2010-11-17 23:00:36 +00001132
Sean Callananc0492742011-05-23 21:40:23 +00001133 if (!RewriteObjCConstString(nsstring_global, cstr_global, FirstEntryInstruction))
Sean Callanan97c924e2011-01-27 01:07:04 +00001134 {
Sean Callanan6ba533e2010-11-17 23:00:36 +00001135 if (log)
1136 log->PutCString("Error rewriting the constant string");
Sean Callanan97c924e2011-01-27 01:07:04 +00001137
1138 // We don't print an error message here because RewriteObjCConstString has done so for us.
1139
Sean Callanan6ba533e2010-11-17 23:00:36 +00001140 return false;
1141 }
Sean Callanan6ba533e2010-11-17 23:00:36 +00001142 }
1143 }
1144
1145 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1146 vi != ve;
1147 ++vi)
1148 {
Sean Callanan9b6898f2011-07-30 02:42:06 +00001149 std::string value_name = vi->first().str();
1150 const char *value_name_cstr = value_name.c_str();
1151
1152 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callanan6ba533e2010-11-17 23:00:36 +00001153 {
1154 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
1155
1156 if (!gv)
1157 {
1158 if (log)
1159 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sean Callanan97c924e2011-01-27 01:07:04 +00001160
1161 if (m_error_stream)
1162 m_error_stream->Printf("Internal error [IRForTarget]: Found a CFConstantStringClassReference, but it is not a global object\n");
1163
Sean Callanan6ba533e2010-11-17 23:00:36 +00001164 return false;
1165 }
1166
1167 gv->eraseFromParent();
1168
1169 break;
1170 }
1171 }
1172
1173 return true;
1174}
1175
Greg Clayton3c7feb42010-11-19 01:05:25 +00001176static bool IsObjCSelectorRef (Value *value)
Sean Callananf5857a02010-07-31 01:32:05 +00001177{
Greg Clayton3c7feb42010-11-19 01:05:25 +00001178 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sean Callananf5857a02010-07-31 01:32:05 +00001179
Greg Clayton3c7feb42010-11-19 01:05:25 +00001180 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
Sean Callananf5857a02010-07-31 01:32:05 +00001181 return false;
1182
1183 return true;
1184}
1185
Sean Callanan97c924e2011-01-27 01:07:04 +00001186// This function does not report errors; its callers are responsible.
Sean Callananf5857a02010-07-31 01:32:05 +00001187bool
Sean Callananc0492742011-05-23 21:40:23 +00001188IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callananf5857a02010-07-31 01:32:05 +00001189{
Greg Claytone005f2c2010-11-06 01:53:30 +00001190 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +00001191
1192 LoadInst *load = dyn_cast<LoadInst>(selector_load);
1193
1194 if (!load)
1195 return false;
1196
1197 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1198 //
1199 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
1200 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1201 //
1202 // where %obj is the object pointer and %tmp is the selector.
1203 //
Greg Clayton3c7feb42010-11-19 01:05:25 +00001204 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
1205 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sean Callananf5857a02010-07-31 01:32:05 +00001206
1207 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
1208
1209 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
1210
1211 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1212 return false;
1213
1214 Constant *osr_initializer = _objc_selector_references_->getInitializer();
1215
1216 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
1217
1218 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1219 return false;
1220
1221 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1222
1223 if (!osr_initializer_base)
1224 return false;
1225
1226 // Find the string's initializer (a ConstantArray) and get the string from it
1227
1228 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
1229
1230 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1231 return false;
1232
1233 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1234
Sean Callanan39a30342012-02-09 03:22:41 +00001235 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
Sean Callananf5857a02010-07-31 01:32:05 +00001236
1237 if (!omvn_initializer_array->isString())
1238 return false;
1239
1240 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
1241
1242 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001243 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sean Callananf5857a02010-07-31 01:32:05 +00001244
1245 // Construct a call to sel_registerName
1246
1247 if (!m_sel_registerName)
1248 {
Greg Claytonb5037af2010-11-15 01:47:11 +00001249 lldb::addr_t sel_registerName_addr;
Sean Callananf5857a02010-07-31 01:32:05 +00001250
Greg Clayton8de27c72010-10-15 22:48:33 +00001251 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytonb5037af2010-11-15 01:47:11 +00001252 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callananf5857a02010-07-31 01:32:05 +00001253 return false;
1254
Sean Callananc2c6f772010-10-26 00:31:56 +00001255 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001256 log->Printf("Found sel_registerName at 0x%llx", sel_registerName_addr);
Sean Callananc2c6f772010-10-26 00:31:56 +00001257
Sean Callananf5857a02010-07-31 01:32:05 +00001258 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
1259
1260 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callananc0492742011-05-23 21:40:23 +00001261 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callananf5857a02010-07-31 01:32:05 +00001262 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanan9b6898f2011-07-30 02:42:06 +00001263 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sean Callananf5857a02010-07-31 01:32:05 +00001264
Sean Callanan9b6898f2011-07-30 02:42:06 +00001265 Type *type_array[1];
1266
1267 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
1268
1269 ArrayRef<Type *> srN_arg_types(type_array, 1);
1270
Sean Callananf5857a02010-07-31 01:32:05 +00001271 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
1272
1273 // Build the constant containing the pointer to the function
Sean Callanan9b6898f2011-07-30 02:42:06 +00001274 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1275 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananf5857a02010-07-31 01:32:05 +00001276 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Greg Claytonb5037af2010-11-15 01:47:11 +00001277 Constant *srN_addr_int = ConstantInt::get(intptr_ty, sel_registerName_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +00001278 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1279 }
1280
Sean Callanan9b6898f2011-07-30 02:42:06 +00001281 Value *argument_array[1];
1282
Sean Callananc0492742011-05-23 21:40:23 +00001283 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sean Callananf5857a02010-07-31 01:32:05 +00001284
Sean Callanan9b6898f2011-07-30 02:42:06 +00001285 argument_array[0] = omvn_pointer;
Sean Callananf5857a02010-07-31 01:32:05 +00001286
Sean Callanan9b6898f2011-07-30 02:42:06 +00001287 ArrayRef<Value *> srN_arguments(argument_array, 1);
1288
Sean Callananf5857a02010-07-31 01:32:05 +00001289 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanan9b6898f2011-07-30 02:42:06 +00001290 srN_arguments,
Sean Callanan6ba533e2010-11-17 23:00:36 +00001291 "sel_registerName",
Sean Callananf5857a02010-07-31 01:32:05 +00001292 selector_load);
1293
1294 // Replace the load with the call in all users
1295
1296 selector_load->replaceAllUsesWith(srN_call);
1297
1298 selector_load->eraseFromParent();
1299
1300 return true;
1301}
1302
1303bool
Sean Callananc0492742011-05-23 21:40:23 +00001304IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callananf5857a02010-07-31 01:32:05 +00001305{
Greg Claytone005f2c2010-11-06 01:53:30 +00001306 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +00001307
1308 BasicBlock::iterator ii;
1309
1310 typedef SmallVector <Instruction*, 2> InstrList;
1311 typedef InstrList::iterator InstrIterator;
1312
1313 InstrList selector_loads;
1314
Greg Clayton3c7feb42010-11-19 01:05:25 +00001315 for (ii = basic_block.begin();
1316 ii != basic_block.end();
Sean Callananf5857a02010-07-31 01:32:05 +00001317 ++ii)
1318 {
1319 Instruction &inst = *ii;
1320
1321 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton3c7feb42010-11-19 01:05:25 +00001322 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callananf5857a02010-07-31 01:32:05 +00001323 selector_loads.push_back(&inst);
1324 }
1325
1326 InstrIterator iter;
1327
1328 for (iter = selector_loads.begin();
1329 iter != selector_loads.end();
1330 ++iter)
1331 {
Sean Callananc0492742011-05-23 21:40:23 +00001332 if (!RewriteObjCSelector(*iter))
Sean Callananf5857a02010-07-31 01:32:05 +00001333 {
Sean Callanan97c924e2011-01-27 01:07:04 +00001334 if (m_error_stream)
1335 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't change a static reference to an Objective-C selector to a dynamic reference\n");
1336
Enrico Granata4c3fb4b2011-07-19 18:03:25 +00001337 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +00001338 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sean Callanan97c924e2011-01-27 01:07:04 +00001339
Sean Callananf5857a02010-07-31 01:32:05 +00001340 return false;
1341 }
1342 }
1343
1344 return true;
1345}
1346
Sean Callanan97c924e2011-01-27 01:07:04 +00001347// This function does not report errors; its callers are responsible.
Sean Callanana48fe162010-08-11 03:57:18 +00001348bool
Sean Callananc0492742011-05-23 21:40:23 +00001349IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanana48fe162010-08-11 03:57:18 +00001350{
Sean Callanan97678d12011-01-13 21:23:32 +00001351 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1352
Sean Callanana48fe162010-08-11 03:57:18 +00001353 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1354
1355 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1356
1357 if (!alloc_md || !alloc_md->getNumOperands())
1358 return false;
1359
1360 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
1361
1362 if (!constant_int)
1363 return false;
1364
1365 // We attempt to register this as a new persistent variable with the DeclMap.
1366
1367 uintptr_t ptr = constant_int->getZExtValue();
1368
Sean Callanan82b74c82010-08-12 01:56:52 +00001369 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanana48fe162010-08-11 03:57:18 +00001370
Sean Callanan82b74c82010-08-12 01:56:52 +00001371 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1372 &decl->getASTContext());
1373
Greg Clayton8de27c72010-10-15 22:48:33 +00001374 StringRef decl_name (decl->getName());
1375 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan6a925532011-01-13 08:53:35 +00001376 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanana48fe162010-08-11 03:57:18 +00001377 return false;
1378
Sean Callananc0492742011-05-23 21:40:23 +00001379 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanan97678d12011-01-13 21:23:32 +00001380 alloc->getType(),
Sean Callanana48fe162010-08-11 03:57:18 +00001381 false, /* not constant */
1382 GlobalValue::ExternalLinkage,
1383 NULL, /* no initializer */
1384 alloc->getName().str().c_str());
1385
1386 // What we're going to do here is make believe this was a regular old external
1387 // variable. That means we need to make the metadata valid.
1388
Sean Callanan2b03efe2012-07-04 01:26:26 +00001389 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
Sean Callanana48fe162010-08-11 03:57:18 +00001390
1391 llvm::Value* values[2];
1392 values[0] = persistent_global;
1393 values[1] = constant_int;
Sean Callanan0de254a2011-05-15 22:34:38 +00001394
1395 ArrayRef<llvm::Value*> value_ref(values, 2);
Sean Callanana48fe162010-08-11 03:57:18 +00001396
Sean Callananc0492742011-05-23 21:40:23 +00001397 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanana48fe162010-08-11 03:57:18 +00001398 named_metadata->addOperand(persistent_global_md);
1399
Sean Callanan97678d12011-01-13 21:23:32 +00001400 // Now, since the variable is a pointer variable, we will drop in a load of that
1401 // pointer variable.
1402
1403 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
1404
1405 if (log)
1406 log->Printf("Replacing \"%s\" with \"%s\"",
1407 PrintValue(alloc).c_str(),
1408 PrintValue(persistent_load).c_str());
1409
1410 alloc->replaceAllUsesWith(persistent_load);
Sean Callanana48fe162010-08-11 03:57:18 +00001411 alloc->eraseFromParent();
1412
1413 return true;
1414}
1415
1416bool
Sean Callananc0492742011-05-23 21:40:23 +00001417IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanana48fe162010-08-11 03:57:18 +00001418{
Sean Callanane8a59a82010-09-13 21:34:21 +00001419 if (!m_resolve_vars)
1420 return true;
1421
Greg Claytone005f2c2010-11-06 01:53:30 +00001422 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanana48fe162010-08-11 03:57:18 +00001423
1424 BasicBlock::iterator ii;
1425
1426 typedef SmallVector <Instruction*, 2> InstrList;
1427 typedef InstrList::iterator InstrIterator;
1428
1429 InstrList pvar_allocs;
1430
Greg Clayton3c7feb42010-11-19 01:05:25 +00001431 for (ii = basic_block.begin();
1432 ii != basic_block.end();
Sean Callanana48fe162010-08-11 03:57:18 +00001433 ++ii)
1434 {
1435 Instruction &inst = *ii;
1436
1437 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callanan237e4742011-01-21 22:30:25 +00001438 {
1439 llvm::StringRef alloc_name = alloc->getName();
1440
1441 if (alloc_name.startswith("$") &&
1442 !alloc_name.startswith("$__lldb"))
1443 {
1444 if (alloc_name.find_first_of("0123456789") == 1)
1445 {
1446 if (log)
1447 log->Printf("Rejecting a numeric persistent variable.");
1448
Sean Callanan97c924e2011-01-27 01:07:04 +00001449 if (m_error_stream)
1450 m_error_stream->Printf("Error [IRForTarget]: Names starting with $0, $1, ... are reserved for use as result names\n");
1451
Sean Callanan237e4742011-01-21 22:30:25 +00001452 return false;
1453 }
1454
Sean Callanana48fe162010-08-11 03:57:18 +00001455 pvar_allocs.push_back(alloc);
Sean Callanan237e4742011-01-21 22:30:25 +00001456 }
1457 }
Sean Callanana48fe162010-08-11 03:57:18 +00001458 }
1459
1460 InstrIterator iter;
1461
1462 for (iter = pvar_allocs.begin();
1463 iter != pvar_allocs.end();
1464 ++iter)
1465 {
Sean Callananc0492742011-05-23 21:40:23 +00001466 if (!RewritePersistentAlloc(*iter))
Sean Callanana48fe162010-08-11 03:57:18 +00001467 {
Sean Callanan97c924e2011-01-27 01:07:04 +00001468 if (m_error_stream)
1469 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
1470
Enrico Granata4c3fb4b2011-07-19 18:03:25 +00001471 if (log)
Sean Callanana48fe162010-08-11 03:57:18 +00001472 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sean Callanan97c924e2011-01-27 01:07:04 +00001473
Sean Callanana48fe162010-08-11 03:57:18 +00001474 return false;
1475 }
1476 }
1477
1478 return true;
1479}
1480
Sean Callananc7ca4662011-10-25 18:36:40 +00001481bool
1482IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1483{
1484 if (!initializer)
1485 return true;
1486
1487 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1488
1489 if (log && log->GetVerbose())
1490 log->Printf(" MaterializeInitializer(%p, %s)", data, PrintValue(initializer).c_str());
1491
1492 Type *initializer_type = initializer->getType();
1493
1494 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1495 {
1496 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1497 return true;
1498 }
Sean Callanan39a30342012-02-09 03:22:41 +00001499 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
Sean Callananc7ca4662011-10-25 18:36:40 +00001500 {
1501 if (array_initializer->isString())
1502 {
1503 std::string array_initializer_string = array_initializer->getAsString();
1504 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1505 }
1506 else
1507 {
1508 ArrayType *array_initializer_type = array_initializer->getType();
1509 Type *array_element_type = array_initializer_type->getElementType();
1510
1511 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
1512
1513 for (int i = 0; i < array_initializer->getNumOperands(); ++i)
1514 {
Sean Callanan39a30342012-02-09 03:22:41 +00001515 Value *operand_value = array_initializer->getOperand(i);
1516 Constant *operand_constant = dyn_cast<Constant>(operand_value);
1517
1518 if (!operand_constant)
1519 return false;
1520
1521 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
Sean Callananc7ca4662011-10-25 18:36:40 +00001522 return false;
1523 }
1524 }
1525 return true;
1526 }
1527 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1528 {
1529 StructType *struct_initializer_type = struct_initializer->getType();
1530 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1531
1532 for (int i = 0;
1533 i < struct_initializer->getNumOperands();
1534 ++i)
1535 {
1536 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1537 return false;
1538 }
1539 return true;
1540 }
1541 return false;
1542}
1543
1544bool
1545IRForTarget::MaterializeInternalVariable (GlobalVariable *global_variable)
1546{
1547 if (GlobalVariable::isExternalLinkage(global_variable->getLinkage()))
1548 return false;
1549
Sean Callananc41606f2011-11-15 19:13:54 +00001550 if (global_variable == m_reloc_placeholder)
1551 return true;
1552
Sean Callananc7ca4662011-10-25 18:36:40 +00001553 uint64_t offset = m_data_allocator->GetStream().GetSize();
1554
1555 llvm::Type *variable_type = global_variable->getType();
1556
1557 Constant *initializer = global_variable->getInitializer();
1558
1559 llvm::Type *initializer_type = initializer->getType();
1560
1561 size_t size = m_target_data->getTypeAllocSize(initializer_type);
Sean Callanan1d970e72012-06-08 22:20:41 +00001562 size_t align = m_target_data->getPrefTypeAlignment(initializer_type);
1563
1564 const size_t mask = (align - 1);
1565 uint64_t aligned_offset = (offset + mask) & ~mask;
1566 m_data_allocator->GetStream().PutNHex8(aligned_offset - offset, 0);
1567 offset = aligned_offset;
Sean Callananc7ca4662011-10-25 18:36:40 +00001568
1569 lldb_private::DataBufferHeap data(size, '\0');
1570
1571 if (initializer)
1572 if (!MaterializeInitializer(data.GetBytes(), initializer))
1573 return false;
1574
1575 m_data_allocator->GetStream().Write(data.GetBytes(), data.GetByteSize());
1576
1577 Constant *new_pointer = BuildRelocation(variable_type, offset);
1578
1579 global_variable->replaceAllUsesWith(new_pointer);
1580
1581 global_variable->eraseFromParent();
1582
1583 return true;
1584}
1585
Sean Callanan97c924e2011-01-27 01:07:04 +00001586// This function does not report errors; its callers are responsible.
Sean Callanan8bce6652010-07-13 21:41:46 +00001587bool
Sean Callananc0492742011-05-23 21:40:23 +00001588IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001589{
Greg Claytone005f2c2010-11-06 01:53:30 +00001590 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan48443652010-12-02 19:47:57 +00001591
1592 if (log)
Sean Callananb9f09a62010-12-06 22:16:55 +00001593 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sean Callanan9a877432011-08-01 17:41:38 +00001594
Greg Clayton8de27c72010-10-15 22:48:33 +00001595 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callananbc2928a2010-08-03 00:23:29 +00001596 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001597 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +00001598 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001599 default:
1600 break;
1601 case Instruction::GetElementPtr:
1602 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +00001603 Value *s = constant_expr->getOperand(0);
Sean Callananc0492742011-05-23 21:40:23 +00001604 if (!MaybeHandleVariable(s))
Sean Callanan48443652010-12-02 19:47:57 +00001605 return false;
Sean Callananbc2928a2010-08-03 00:23:29 +00001606 }
1607 }
Sean Callananf921cf52010-12-03 19:51:05 +00001608 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +00001609 {
Sean Callananc7ca4662011-10-25 18:36:40 +00001610 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1611 return MaterializeInternalVariable(global_variable);
1612
Sean Callananc0492742011-05-23 21:40:23 +00001613 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001614
Sean Callananf5857a02010-07-31 01:32:05 +00001615 if (!named_decl)
1616 {
Greg Clayton3c7feb42010-11-19 01:05:25 +00001617 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +00001618 return true;
1619
Sean Callanan7cd46742010-12-06 00:56:39 +00001620 if (!global_variable->hasExternalLinkage())
1621 return true;
1622
Sean Callananf5857a02010-07-31 01:32:05 +00001623 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001624 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00001625
Sean Callananf5857a02010-07-31 01:32:05 +00001626 return false;
1627 }
1628
Greg Clayton8de27c72010-10-15 22:48:33 +00001629 std::string name (named_decl->getName().str());
Sean Callanan810f22d2010-07-16 00:09:46 +00001630
Sean Callanan771131d2010-09-30 21:18:25 +00001631 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +00001632 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +00001633
1634 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +00001635 {
Sean Callanan771131d2010-09-30 21:18:25 +00001636 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +00001637 ast_context = &value_decl->getASTContext();
1638 }
Sean Callanan810f22d2010-07-16 00:09:46 +00001639 else
Sean Callananf328c9f2010-07-20 23:31:16 +00001640 {
Sean Callanan810f22d2010-07-16 00:09:46 +00001641 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +00001642 }
Sean Callanan771131d2010-09-30 21:18:25 +00001643
Sean Callanan6a925532011-01-13 08:53:35 +00001644 clang::QualType qual_type;
Sean Callanan58baaad2011-07-08 00:39:14 +00001645 const Type *value_type = NULL;
Sean Callanan6a925532011-01-13 08:53:35 +00001646
Sean Callanan97678d12011-01-13 21:23:32 +00001647 if (name[0] == '$')
Sean Callanan6a925532011-01-13 08:53:35 +00001648 {
1649 // The $__lldb_expr_result name indicates the the return value has allocated as
1650 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1651 // accesses to this static variable need to be redirected to the result of dereferencing
1652 // a pointer that is passed in as one of the arguments.
1653 //
1654 // Consequently, when reporting the size of the type, we report a pointer type pointing
1655 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanan97678d12011-01-13 21:23:32 +00001656 //
1657 // We also do this for any user-declared persistent variables.
Sean Callananf328c9f2010-07-20 23:31:16 +00001658
Sean Callanan6a925532011-01-13 08:53:35 +00001659 qual_type = ast_context->getPointerType(clang::QualType::getFromOpaquePtr(opaque_type));
1660 value_type = PointerType::get(global_variable->getType(), 0);
1661 }
1662 else
1663 {
1664 qual_type = clang::QualType::getFromOpaquePtr(opaque_type);
1665 value_type = global_variable->getType();
1666 }
Sean Callanan771131d2010-09-30 21:18:25 +00001667
1668 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
1669 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001670
Sean Callanan771131d2010-09-30 21:18:25 +00001671 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001672 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %lu, align %lld]",
Sean Callanan771131d2010-09-30 21:18:25 +00001673 name.c_str(),
1674 qual_type.getAsString().c_str(),
1675 PrintType(value_type).c_str(),
1676 value_size,
1677 value_alignment);
Sean Callanan3aa7da52010-12-13 22:46:15 +00001678
Sean Callanan8bce6652010-07-13 21:41:46 +00001679
Sean Callanan8c127202010-08-23 23:09:38 +00001680 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton8de27c72010-10-15 22:48:33 +00001681 lldb_private::ConstString (name.c_str()),
1682 llvm_value_ptr,
Sean Callananba992c52010-07-27 02:07:53 +00001683 value_size,
1684 value_alignment))
Sean Callanan9a877432011-08-01 17:41:38 +00001685 {
1686 if (!global_variable->hasExternalLinkage())
1687 return true;
1688 else
1689 return false;
1690 }
Sean Callanan8bce6652010-07-13 21:41:46 +00001691 }
Sean Callananf3143b72010-12-03 03:02:31 +00001692 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanan48443652010-12-02 19:47:57 +00001693 {
1694 if (log)
1695 log->Printf("Function pointers aren't handled right now");
1696
1697 return false;
1698 }
Sean Callanan8bce6652010-07-13 21:41:46 +00001699
1700 return true;
1701}
1702
Sean Callanan97c924e2011-01-27 01:07:04 +00001703// This function does not report errors; its callers are responsible.
Sean Callanan8bce6652010-07-13 21:41:46 +00001704bool
Sean Callananc0492742011-05-23 21:40:23 +00001705IRForTarget::HandleSymbol (Value *symbol)
Sean Callanan81974962011-05-08 02:21:26 +00001706{
Sean Callananc7674af2011-01-17 23:42:46 +00001707 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1708
1709 lldb_private::ConstString name(symbol->getName().str().c_str());
1710
Sean Callanan21ef5bb2011-12-01 02:04:16 +00001711 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
Sean Callananc7674af2011-01-17 23:42:46 +00001712
Greg Claytoncbc07cf2011-06-23 04:25:29 +00001713 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc7674af2011-01-17 23:42:46 +00001714 {
1715 if (log)
1716 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
1717
1718 return false;
1719 }
1720
1721 if (log)
1722 log->Printf("Found \"%s\" at 0x%llx", name.GetCString(), symbol_addr);
1723
Sean Callanan9b6898f2011-07-30 02:42:06 +00001724 Type *symbol_type = symbol->getType();
Sean Callananc7674af2011-01-17 23:42:46 +00001725
Sean Callanan9b6898f2011-07-30 02:42:06 +00001726 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1727 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc7674af2011-01-17 23:42:46 +00001728
1729 Constant *symbol_addr_int = ConstantInt::get(intptr_ty, symbol_addr, false);
1730
1731 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1732
1733 if (log)
1734 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
1735
1736 symbol->replaceAllUsesWith(symbol_addr_ptr);
1737
1738 return true;
1739}
1740
1741bool
Sean Callananc0492742011-05-23 21:40:23 +00001742IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanandc27aba2010-10-05 22:26:43 +00001743{
Sean Callanan48443652010-12-02 19:47:57 +00001744 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1745
1746 if (log)
1747 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sean Callanandc27aba2010-10-05 22:26:43 +00001748
Sean Callanan6ba533e2010-11-17 23:00:36 +00001749 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanandc27aba2010-10-05 22:26:43 +00001750 op_index < num_ops;
1751 ++op_index)
Sean Callananc0492742011-05-23 21:40:23 +00001752 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan97c924e2011-01-27 01:07:04 +00001753 {
1754 if (m_error_stream)
1755 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite one of the arguments of a function call.\n");
1756
Sean Callanandc27aba2010-10-05 22:26:43 +00001757 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001758 }
1759
Sean Callanandc27aba2010-10-05 22:26:43 +00001760 return true;
1761}
1762
1763bool
Sean Callanan9911d2f2011-11-01 23:38:03 +00001764IRForTarget::HandleObjCClass(Value *classlist_reference)
1765{
1766 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1767
1768 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
1769
1770 if (!global_variable)
1771 return false;
1772
1773 Constant *initializer = global_variable->getInitializer();
1774
1775 if (!initializer)
1776 return false;
1777
1778 if (!initializer->hasName())
1779 return false;
1780
1781 StringRef name(initializer->getName());
1782 lldb_private::ConstString name_cstr(name.str().c_str());
Greg Clayton16d21872011-12-03 20:02:42 +00001783 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
Sean Callanan9911d2f2011-11-01 23:38:03 +00001784
1785 if (log)
1786 log->Printf("Found reference to Objective-C class %s (0x%llx)", name_cstr.AsCString(), (unsigned long long)class_ptr);
1787
1788 if (class_ptr == LLDB_INVALID_ADDRESS)
1789 return false;
1790
1791 if (global_variable->use_begin() == global_variable->use_end())
1792 return false;
1793
1794 LoadInst *load_instruction = NULL;
1795
1796 for (Value::use_iterator i = global_variable->use_begin(), e = global_variable->use_end();
1797 i != e;
1798 ++i)
1799 {
1800 if ((load_instruction = dyn_cast<LoadInst>(*i)))
1801 break;
1802 }
1803
1804 if (!load_instruction)
1805 return false;
1806
1807 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1808 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
1809
1810 Constant *class_addr = ConstantInt::get(intptr_ty, (uint64_t)class_ptr);
1811 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
1812
1813 load_instruction->replaceAllUsesWith(class_bitcast);
1814
1815 load_instruction->eraseFromParent();
1816
1817 return true;
1818}
1819
1820bool
Sean Callanan1850f942012-07-21 02:02:15 +00001821IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1822{
1823 BasicBlock::iterator ii;
1824
1825 std::vector<CallInst *> calls_to_remove;
1826
1827 for (ii = basic_block.begin();
1828 ii != basic_block.end();
1829 ++ii)
1830 {
1831 Instruction &inst = *ii;
1832
1833 CallInst *call = dyn_cast<CallInst>(&inst);
1834
1835 // MaybeHandleCallArguments handles error reporting; we are silent here
1836 if (!call)
1837 continue;
1838
1839 bool remove = false;
1840
1841 llvm::Function *func = call->getCalledFunction();
1842
1843 if (func && func->getName() == "__cxa_atexit")
1844 remove = true;
1845
1846 llvm::Value *val = call->getCalledValue();
1847
1848 if (val && val->getName() == "__cxa_atexit")
1849 remove = true;
1850
1851 if (remove)
1852 calls_to_remove.push_back(call);
1853 }
1854
1855 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1856 ci != ce;
1857 ++ci)
1858 {
1859 (*ci)->eraseFromParent();
1860 }
1861
1862 return true;
1863}
1864
1865bool
Sean Callananc0492742011-05-23 21:40:23 +00001866IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sean Callanan8bce6652010-07-13 21:41:46 +00001867{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001868 /////////////////////////////////////////////////////////////////////////
1869 // Prepare the current basic block for execution in the remote process
1870 //
1871
Sean Callanan02fbafa2010-07-27 21:39:39 +00001872 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +00001873
Greg Clayton3c7feb42010-11-19 01:05:25 +00001874 for (ii = basic_block.begin();
1875 ii != basic_block.end();
Sean Callanan8bce6652010-07-13 21:41:46 +00001876 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001877 {
Sean Callanan8bce6652010-07-13 21:41:46 +00001878 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001879
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001880 CallInst *call = dyn_cast<CallInst>(&inst);
Sean Callananba992c52010-07-27 02:07:53 +00001881
Sean Callanan97c924e2011-01-27 01:07:04 +00001882 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callananc0492742011-05-23 21:40:23 +00001883 if (call && !MaybeHandleCallArguments(call))
Sean Callanan48443652010-12-02 19:47:57 +00001884 return false;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001885 }
1886
1887 return true;
1888}
1889
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001890bool
Sean Callananc0492742011-05-23 21:40:23 +00001891IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001892{
Sean Callananae71e302010-11-18 22:21:58 +00001893 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1894
Sean Callananc0492742011-05-23 21:40:23 +00001895 for (Module::global_iterator global = m_module->global_begin(), end = m_module->global_end();
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001896 global != end;
1897 ++global)
1898 {
Sean Callanan1c17a7e2011-12-22 21:24:49 +00001899 if (!global)
1900 {
1901 if (m_error_stream)
1902 m_error_stream->Printf("Internal error [IRForTarget]: global variable is NULL");
1903
1904 return false;
1905 }
1906
1907 std::string global_name = (*global).getName().str();
1908
Greg Clayton3c7feb42010-11-19 01:05:25 +00001909 if (log)
1910 log->Printf("Examining %s, DeclForGlobalValue returns %p",
Sean Callanan1c17a7e2011-12-22 21:24:49 +00001911 global_name.c_str(),
Sean Callananc0492742011-05-23 21:40:23 +00001912 DeclForGlobal(global));
Sean Callanan9911d2f2011-11-01 23:38:03 +00001913
1914 if (global_name.find("OBJC_IVAR") == 0)
Sean Callananc7674af2011-01-17 23:42:46 +00001915 {
Sean Callananc0492742011-05-23 21:40:23 +00001916 if (!HandleSymbol(global))
Sean Callanan97c924e2011-01-27 01:07:04 +00001917 {
1918 if (m_error_stream)
Sean Callanan1c17a7e2011-12-22 21:24:49 +00001919 m_error_stream->Printf("Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol %s\n", global_name.c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00001920
Sean Callananc7674af2011-01-17 23:42:46 +00001921 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001922 }
Sean Callananc7674af2011-01-17 23:42:46 +00001923 }
Sean Callanan9911d2f2011-11-01 23:38:03 +00001924 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1925 {
1926 if (!HandleObjCClass(global))
1927 {
1928 if (m_error_stream)
1929 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
1930
1931 return false;
1932 }
1933 }
Sean Callananc0492742011-05-23 21:40:23 +00001934 else if (DeclForGlobal(global))
Sean Callananc7674af2011-01-17 23:42:46 +00001935 {
Sean Callananc0492742011-05-23 21:40:23 +00001936 if (!MaybeHandleVariable (global))
Sean Callanan97c924e2011-01-27 01:07:04 +00001937 {
1938 if (m_error_stream)
Sean Callanan1c17a7e2011-12-22 21:24:49 +00001939 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite external variable %s\n", global_name.c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00001940
Sean Callananc7674af2011-01-17 23:42:46 +00001941 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001942 }
Sean Callananc7674af2011-01-17 23:42:46 +00001943 }
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001944 }
1945
1946 return true;
1947}
1948
Sean Callananc0492742011-05-23 21:40:23 +00001949bool
1950IRForTarget::ReplaceStrings ()
1951{
1952 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1953
1954 if (!m_data_allocator)
1955 return true; // hope for the best; some clients may not want static allocation!
1956
1957 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
1958
1959 OffsetsTy offsets;
1960
1961 for (Module::global_iterator gi = m_module->global_begin(), ge = m_module->global_end();
1962 gi != ge;
1963 ++gi)
1964 {
1965 GlobalVariable *gv = gi;
1966
1967 if (!gv->hasInitializer())
1968 continue;
1969
1970 Constant *gc = gv->getInitializer();
1971
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001972 std::string str;
Sean Callananc0492742011-05-23 21:40:23 +00001973
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001974 if (gc->isNullValue())
1975 {
1976 Type *gc_type = gc->getType();
1977
1978 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
1979
1980 if (!gc_array_type)
1981 continue;
1982
1983 Type *gc_element_type = gc_array_type->getElementType();
1984
1985 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
1986
1987 if (gc_integer_type->getBitWidth() != 8)
1988 continue;
1989
1990 str = "";
1991 }
1992 else
1993 {
Sean Callanan39a30342012-02-09 03:22:41 +00001994 ConstantDataArray *gc_array = dyn_cast<ConstantDataArray>(gc);
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001995
1996 if (!gc_array)
1997 continue;
Sean Callananc0492742011-05-23 21:40:23 +00001998
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001999 if (!gc_array->isCString())
2000 continue;
Sean Callananc0492742011-05-23 21:40:23 +00002001
Sean Callanan1b3d1df2011-08-10 21:05:52 +00002002 if (log)
2003 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
Sean Callananc0492742011-05-23 21:40:23 +00002004
Sean Callanan1b3d1df2011-08-10 21:05:52 +00002005 str = gc_array->getAsString();
2006 }
2007
Sean Callananc0492742011-05-23 21:40:23 +00002008 offsets[gv] = m_data_allocator->GetStream().GetSize();
2009
2010 m_data_allocator->GetStream().Write(str.c_str(), str.length() + 1);
2011 }
2012
Sean Callanan9b6898f2011-07-30 02:42:06 +00002013 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callananc0492742011-05-23 21:40:23 +00002014
2015 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
2016 oi != oe;
2017 ++oi)
2018 {
2019 GlobalVariable *gv = oi->first;
2020 size_t offset = oi->second;
2021
2022 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
2023
2024 if (log)
2025 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
2026
2027 for (GlobalVariable::use_iterator ui = gv->use_begin(), ue = gv->use_end();
2028 ui != ue;
2029 ++ui)
2030 {
2031 if (log)
2032 log->Printf("Found use %s", PrintValue(*ui).c_str());
2033
2034 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(*ui);
2035 StoreInst *store_inst = dyn_cast<StoreInst>(*ui);
2036
2037 if (const_expr)
2038 {
2039 if (const_expr->getOpcode() != Instruction::GetElementPtr)
2040 {
2041 if (log)
2042 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
2043
2044 return false;
2045 }
2046
Sean Callanan1b3d1df2011-08-10 21:05:52 +00002047 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
2048 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
2049
2050 const_expr->replaceAllUsesWith(new_gep);
Sean Callananc0492742011-05-23 21:40:23 +00002051 }
2052 else if (store_inst)
2053 {
2054 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
2055
2056 store_inst->setOperand(0, bit_cast);
2057 }
2058 else
2059 {
2060 if (log)
2061 log->Printf("Use (%s) of string variable is neither a constant nor a store", PrintValue(const_expr).c_str());
2062
2063 return false;
2064 }
2065 }
2066
2067 gv->eraseFromParent();
2068 }
2069
2070 return true;
2071}
2072
2073bool
2074IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
2075{
2076 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2077
2078 if (!m_data_allocator)
2079 return true;
2080
2081 typedef SmallVector <Value*, 2> ConstantList;
2082 typedef SmallVector <llvm::Instruction*, 2> UserList;
2083 typedef ConstantList::iterator ConstantIterator;
2084 typedef UserList::iterator UserIterator;
2085
2086 ConstantList static_constants;
2087 UserList static_users;
2088
2089 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
2090 ii != ie;
2091 ++ii)
2092 {
2093 llvm::Instruction &inst = *ii;
2094
2095 for (Instruction::op_iterator oi = inst.op_begin(), oe = inst.op_end();
2096 oi != oe;
2097 ++oi)
2098 {
2099 Value *operand_val = oi->get();
2100
2101 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
2102
Sean Callanan26c05d92012-04-26 20:51:20 +00002103 if (operand_constant_fp/* && operand_constant_fp->getType()->isX86_FP80Ty()*/)
Sean Callananc0492742011-05-23 21:40:23 +00002104 {
2105 static_constants.push_back(operand_val);
2106 static_users.push_back(ii);
2107 }
2108 }
2109 }
2110
2111 ConstantIterator constant_iter;
2112 UserIterator user_iter;
Greg Clayton54b38412011-05-24 23:06:02 +00002113
Sean Callananc0492742011-05-23 21:40:23 +00002114 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
2115 constant_iter != static_constants.end();
2116 ++constant_iter, ++user_iter)
2117 {
2118 Value *operand_val = *constant_iter;
2119 llvm::Instruction *inst = *user_iter;
2120
2121 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sean Callanan1d970e72012-06-08 22:20:41 +00002122 Type *operand_type = operand_constant_fp->getType();
Sean Callananc0492742011-05-23 21:40:23 +00002123
2124 if (operand_constant_fp)
2125 {
2126 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
2127 APInt operand_apint = operand_apfloat.bitcastToAPInt();
2128
2129 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
2130 size_t operand_data_size = operand_apint.getBitWidth() / 8;
2131
2132 if (log)
2133 {
2134 std::string s;
2135 raw_string_ostream ss(s);
2136 for (size_t index = 0;
2137 index < operand_data_size;
2138 ++index)
2139 {
2140 ss << (uint32_t)operand_raw_data[index];
2141 ss << " ";
2142 }
2143 ss.flush();
2144
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00002145 log->Printf("Found ConstantFP with size %lu and raw data %s", operand_data_size, s.c_str());
Sean Callananc0492742011-05-23 21:40:23 +00002146 }
2147
2148 lldb_private::DataBufferHeap data(operand_data_size, 0);
2149
2150 if (lldb::endian::InlHostByteOrder() != m_data_allocator->GetStream().GetByteOrder())
2151 {
2152 uint8_t *data_bytes = data.GetBytes();
2153
2154 for (size_t index = 0;
2155 index < operand_data_size;
2156 ++index)
2157 {
2158 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
2159 }
2160 }
2161 else
2162 {
2163 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
2164 }
2165
2166 uint64_t offset = m_data_allocator->GetStream().GetSize();
2167
Sean Callanan1d970e72012-06-08 22:20:41 +00002168 size_t align = m_target_data->getPrefTypeAlignment(operand_type);
2169
2170 const size_t mask = (align - 1);
2171 uint64_t aligned_offset = (offset + mask) & ~mask;
2172 m_data_allocator->GetStream().PutNHex8(aligned_offset - offset, 0);
2173 offset = aligned_offset;
2174
Sean Callananc0492742011-05-23 21:40:23 +00002175 m_data_allocator->GetStream().Write(data.GetBytes(), operand_data_size);
2176
Sean Callanan9b6898f2011-07-30 02:42:06 +00002177 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
Sean Callananc0492742011-05-23 21:40:23 +00002178
2179 Constant *new_pointer = BuildRelocation(fp_ptr_ty, offset);
2180
2181 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
2182
2183 operand_constant_fp->replaceAllUsesWith(fp_load);
2184 }
2185 }
2186
2187 return true;
2188}
2189
Sean Callanan02fbafa2010-07-27 21:39:39 +00002190static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +00002191{
Sean Callanan58baaad2011-07-08 00:39:14 +00002192 Constant *Old = NULL;
Sean Callanan45839272010-07-24 01:37:44 +00002193
Sean Callanan6ba533e2010-11-17 23:00:36 +00002194 if (!(Old = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +00002195 return false;
2196
Sean Callanan58baaad2011-07-08 00:39:14 +00002197 ConstantExpr *CE = NULL;
Sean Callanan47a5c4c2010-09-23 03:01:22 +00002198
2199 if ((CE = dyn_cast<ConstantExpr>(V)))
2200 {
2201 if (CE->getOpcode() != Instruction::BitCast)
2202 return false;
2203
Sean Callanan6ba533e2010-11-17 23:00:36 +00002204 Old = CE->getOperand(0);
Sean Callanan47a5c4c2010-09-23 03:01:22 +00002205 }
2206
Sean Callanan6ba533e2010-11-17 23:00:36 +00002207 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sean Callanan45839272010-07-24 01:37:44 +00002208
2209 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
2210 return false;
2211
2212 return true;
2213}
2214
Sean Callananc0492742011-05-23 21:40:23 +00002215void
2216IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callanan45839272010-07-24 01:37:44 +00002217{
Sean Callananc0492742011-05-23 21:40:23 +00002218 Constant* zero(ConstantInt::get(Type::getInt8Ty(m_module->getContext()), 0, true));
Sean Callanan45839272010-07-24 01:37:44 +00002219
2220 Value::use_iterator ui;
2221
2222 for (ui = guard_load->use_begin();
2223 ui != guard_load->use_end();
2224 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +00002225 {
Greg Clayton6e713402010-07-30 20:30:44 +00002226 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +00002227 {
2228 // do nothing for the moment
2229 }
2230 else
2231 {
2232 ui->replaceUsesOfWith(guard_load, zero);
2233 }
2234 }
Sean Callanan45839272010-07-24 01:37:44 +00002235
2236 guard_load->eraseFromParent();
2237}
2238
2239static void ExciseGuardStore(Instruction* guard_store)
2240{
2241 guard_store->eraseFromParent();
2242}
2243
2244bool
Sean Callananc0492742011-05-23 21:40:23 +00002245IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sean Callanan45839272010-07-24 01:37:44 +00002246{
2247 ///////////////////////////////////////////////////////
2248 // Eliminate any reference to guard variables found.
2249 //
2250
Sean Callanan02fbafa2010-07-27 21:39:39 +00002251 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +00002252
Sean Callanan02fbafa2010-07-27 21:39:39 +00002253 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +00002254 typedef InstrList::iterator InstrIterator;
2255
2256 InstrList guard_loads;
2257 InstrList guard_stores;
2258
Greg Clayton3c7feb42010-11-19 01:05:25 +00002259 for (ii = basic_block.begin();
2260 ii != basic_block.end();
Sean Callanan45839272010-07-24 01:37:44 +00002261 ++ii)
2262 {
2263 Instruction &inst = *ii;
2264
2265 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
2266 if (isGuardVariableRef(load->getPointerOperand()))
2267 guard_loads.push_back(&inst);
2268
2269 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
2270 if (isGuardVariableRef(store->getPointerOperand()))
2271 guard_stores.push_back(&inst);
2272 }
2273
2274 InstrIterator iter;
2275
2276 for (iter = guard_loads.begin();
2277 iter != guard_loads.end();
2278 ++iter)
Sean Callananc0492742011-05-23 21:40:23 +00002279 TurnGuardLoadIntoZero(*iter);
Sean Callanan45839272010-07-24 01:37:44 +00002280
2281 for (iter = guard_stores.begin();
2282 iter != guard_stores.end();
2283 ++iter)
2284 ExciseGuardStore(*iter);
2285
2286 return true;
2287}
2288
Sean Callanan97c924e2011-01-27 01:07:04 +00002289// This function does not report errors; its callers are responsible.
Sean Callanan6ba533e2010-11-17 23:00:36 +00002290bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00002291IRForTarget::UnfoldConstant(Constant *old_constant, Value *new_constant, Instruction *first_entry_inst)
Sean Callananbafd6852010-07-14 23:40:29 +00002292{
Greg Claytone005f2c2010-11-06 01:53:30 +00002293 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbafd6852010-07-14 23:40:29 +00002294
2295 Value::use_iterator ui;
2296
Sean Callanana48fe162010-08-11 03:57:18 +00002297 SmallVector<User*, 16> users;
2298
2299 // We do this because the use list might change, invalidating our iterator.
2300 // Much better to keep a work list ourselves.
Greg Clayton3c7feb42010-11-19 01:05:25 +00002301 for (ui = old_constant->use_begin();
2302 ui != old_constant->use_end();
Sean Callananbafd6852010-07-14 23:40:29 +00002303 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +00002304 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +00002305
Johnny Chen2bc9eb32011-07-19 19:48:13 +00002306 for (size_t i = 0;
Sean Callanana48fe162010-08-11 03:57:18 +00002307 i < users.size();
2308 ++i)
2309 {
2310 User *user = users[i];
2311
Sean Callananbafd6852010-07-14 23:40:29 +00002312 if (Constant *constant = dyn_cast<Constant>(user))
2313 {
2314 // synthesize a new non-constant equivalent of the constant
2315
2316 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
2317 {
2318 switch (constant_expr->getOpcode())
2319 {
2320 default:
2321 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002322 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00002323 return false;
2324 case Instruction::BitCast:
2325 {
2326 // UnaryExpr
2327 // OperandList[0] is value
2328
2329 Value *s = constant_expr->getOperand(0);
2330
Greg Clayton3c7feb42010-11-19 01:05:25 +00002331 if (s == old_constant)
2332 s = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00002333
Sean Callananc0492742011-05-23 21:40:23 +00002334 BitCastInst *bit_cast(new BitCastInst(s, constant_expr->getType(), "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00002335
Greg Clayton3c7feb42010-11-19 01:05:25 +00002336 UnfoldConstant(constant_expr, bit_cast, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00002337 }
2338 break;
2339 case Instruction::GetElementPtr:
2340 {
2341 // GetElementPtrConstantExpr
2342 // OperandList[0] is base
2343 // OperandList[1]... are indices
2344
2345 Value *ptr = constant_expr->getOperand(0);
2346
Greg Clayton3c7feb42010-11-19 01:05:25 +00002347 if (ptr == old_constant)
2348 ptr = new_constant;
Sean Callanan9b6898f2011-07-30 02:42:06 +00002349
2350 std::vector<Value*> index_vector;
Sean Callananbafd6852010-07-14 23:40:29 +00002351
2352 unsigned operand_index;
2353 unsigned num_operands = constant_expr->getNumOperands();
2354
2355 for (operand_index = 1;
2356 operand_index < num_operands;
2357 ++operand_index)
2358 {
2359 Value *operand = constant_expr->getOperand(operand_index);
2360
Greg Clayton3c7feb42010-11-19 01:05:25 +00002361 if (operand == old_constant)
2362 operand = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00002363
Sean Callanan9b6898f2011-07-30 02:42:06 +00002364 index_vector.push_back(operand);
Sean Callananbafd6852010-07-14 23:40:29 +00002365 }
2366
Sean Callanan9b6898f2011-07-30 02:42:06 +00002367 ArrayRef <Value*> indices(index_vector);
2368
2369 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices, "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00002370
Greg Clayton3c7feb42010-11-19 01:05:25 +00002371 UnfoldConstant(constant_expr, get_element_ptr, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00002372 }
2373 break;
2374 }
2375 }
2376 else
2377 {
2378 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002379 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00002380 return false;
2381 }
2382 }
2383 else
2384 {
2385 // simple fall-through case for non-constants
Greg Clayton3c7feb42010-11-19 01:05:25 +00002386 user->replaceUsesOfWith(old_constant, new_constant);
Sean Callananbafd6852010-07-14 23:40:29 +00002387 }
2388 }
2389
2390 return true;
2391}
2392
Sean Callanan8bce6652010-07-13 21:41:46 +00002393bool
Sean Callananc0492742011-05-23 21:40:23 +00002394IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan8bce6652010-07-13 21:41:46 +00002395{
Sean Callanane8a59a82010-09-13 21:34:21 +00002396 if (!m_resolve_vars)
2397 return true;
2398
Greg Claytone005f2c2010-11-06 01:53:30 +00002399 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8bce6652010-07-13 21:41:46 +00002400
2401 m_decl_map->DoStructLayout();
2402
2403 if (log)
2404 log->Printf("Element arrangement:");
2405
2406 uint32_t num_elements;
2407 uint32_t element_index;
2408
2409 size_t size;
2410 off_t alignment;
2411
2412 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2413 return false;
2414
Greg Clayton3c7feb42010-11-19 01:05:25 +00002415 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00002416
Greg Clayton3c7feb42010-11-19 01:05:25 +00002417 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002418 {
2419 if (m_error_stream)
2420 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes no arguments (should take at least a struct pointer)");
2421
Sean Callanan8bce6652010-07-13 21:41:46 +00002422 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002423 }
2424
Sean Callanan02fbafa2010-07-27 21:39:39 +00002425 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00002426
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002427 if (argument->getName().equals("this"))
2428 {
2429 ++iter;
2430
Greg Clayton3c7feb42010-11-19 01:05:25 +00002431 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002432 {
2433 if (m_error_stream)
2434 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'this' argument (should take a struct pointer too)");
2435
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002436 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002437 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002438
2439 argument = iter;
2440 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002441 else if (argument->getName().equals("self"))
2442 {
2443 ++iter;
2444
2445 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002446 {
2447 if (m_error_stream)
2448 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' argument (should take '_cmd' and a struct pointer too)");
2449
Sean Callanan3aa7da52010-12-13 22:46:15 +00002450 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002451 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002452
2453 if (!iter->getName().equals("_cmd"))
Sean Callanan97c924e2011-01-27 01:07:04 +00002454 {
2455 if (m_error_stream)
2456 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes '%s' after 'self' argument (should take '_cmd')", iter->getName().str().c_str());
2457
Sean Callanan3aa7da52010-12-13 22:46:15 +00002458 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002459 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002460
2461 ++iter;
2462
2463 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002464 {
2465 if (m_error_stream)
2466 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' and '_cmd' arguments (should take a struct pointer too)");
2467
Sean Callanan3aa7da52010-12-13 22:46:15 +00002468 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002469 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002470
2471 argument = iter;
2472 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002473
Greg Clayton8de27c72010-10-15 22:48:33 +00002474 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan97c924e2011-01-27 01:07:04 +00002475 {
2476 if (m_error_stream)
2477 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes an argument named '%s' instead of the struct pointer", argument->getName().str().c_str());
2478
Sean Callanan8bce6652010-07-13 21:41:46 +00002479 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002480 }
2481
Sean Callanan8bce6652010-07-13 21:41:46 +00002482 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002483 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sean Callanan8bce6652010-07-13 21:41:46 +00002484
Greg Clayton3c7feb42010-11-19 01:05:25 +00002485 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +00002486 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00002487
Sean Callanan6ba533e2010-11-17 23:00:36 +00002488 if (!FirstEntryInstruction)
Sean Callanan97c924e2011-01-27 01:07:04 +00002489 {
2490 if (m_error_stream)
2491 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the first instruction in the wrapper for use in rewriting");
2492
Sean Callanan8bce6652010-07-13 21:41:46 +00002493 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002494 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002495
Sean Callananc0492742011-05-23 21:40:23 +00002496 LLVMContext &context(m_module->getContext());
Sean Callanan9b6898f2011-07-30 02:42:06 +00002497 IntegerType *offset_type(Type::getInt32Ty(context));
Sean Callanan8bce6652010-07-13 21:41:46 +00002498
2499 if (!offset_type)
Sean Callanan97c924e2011-01-27 01:07:04 +00002500 {
2501 if (m_error_stream)
2502 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
2503
Sean Callanan8bce6652010-07-13 21:41:46 +00002504 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002505 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002506
2507 for (element_index = 0; element_index < num_elements; ++element_index)
2508 {
Sean Callanan58baaad2011-07-08 00:39:14 +00002509 const clang::NamedDecl *decl = NULL;
2510 Value *value = NULL;
Sean Callanan8bce6652010-07-13 21:41:46 +00002511 off_t offset;
Greg Clayton8de27c72010-10-15 22:48:33 +00002512 lldb_private::ConstString name;
Sean Callanan8bce6652010-07-13 21:41:46 +00002513
Sean Callanan45690fe2010-08-30 22:17:16 +00002514 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan97c924e2011-01-27 01:07:04 +00002515 {
2516 if (m_error_stream)
2517 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
2518
Sean Callanan8bce6652010-07-13 21:41:46 +00002519 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002520 }
2521
Sean Callanan8bce6652010-07-13 21:41:46 +00002522 if (log)
Sean Callananc7ca4662011-10-25 18:36:40 +00002523 log->Printf(" \"%s\" (\"%s\") placed at %lld",
Greg Clayton8de27c72010-10-15 22:48:33 +00002524 name.GetCString(),
Sean Callananc7ca4662011-10-25 18:36:40 +00002525 decl->getNameAsString().c_str(),
Sean Callanan8bce6652010-07-13 21:41:46 +00002526 offset);
2527
Sean Callanan9b6898f2011-07-30 02:42:06 +00002528 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
Sean Callanan6ba533e2010-11-17 23:00:36 +00002529 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", FirstEntryInstruction);
Sean Callanan6a925532011-01-13 08:53:35 +00002530
Sean Callananc7ca4662011-10-25 18:36:40 +00002531 if (value)
Sean Callanan6a925532011-01-13 08:53:35 +00002532 {
Sean Callananc7ca4662011-10-25 18:36:40 +00002533 Value *replacement = NULL;
Sean Callanan6a925532011-01-13 08:53:35 +00002534
Sean Callananc7ca4662011-10-25 18:36:40 +00002535 if (log)
2536 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
Sean Callanan6a925532011-01-13 08:53:35 +00002537
Sean Callananc7ca4662011-10-25 18:36:40 +00002538 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2539 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2540 // entry in order to produce the static variable that the AST thinks it is accessing.
2541 if (name == m_result_name && !m_result_is_pointer)
2542 {
2543 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType()->getPointerTo(), "", FirstEntryInstruction);
2544
2545 LoadInst *load = new LoadInst(bit_cast, "", FirstEntryInstruction);
2546
2547 replacement = load;
2548 }
2549 else
2550 {
2551 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", FirstEntryInstruction);
2552
2553 replacement = bit_cast;
2554 }
2555
2556 if (Constant *constant = dyn_cast<Constant>(value))
2557 UnfoldConstant(constant, replacement, FirstEntryInstruction);
2558 else
2559 value->replaceAllUsesWith(replacement);
2560
2561 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2562 var->eraseFromParent();
2563 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002564 }
2565
2566 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00002567 log->Printf("Total structure [align %lld, size %lu]", alignment, size);
Sean Callanan8bce6652010-07-13 21:41:46 +00002568
2569 return true;
2570}
2571
Sean Callananc0492742011-05-23 21:40:23 +00002572llvm::Constant *
Sean Callanan9b6898f2011-07-30 02:42:06 +00002573IRForTarget::BuildRelocation(llvm::Type *type,
Sean Callananc0492742011-05-23 21:40:23 +00002574 uint64_t offset)
2575{
2576 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2577
Sean Callanan9b6898f2011-07-30 02:42:06 +00002578 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
2579 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc0492742011-05-23 21:40:23 +00002580
2581 llvm::Constant *offset_int = ConstantInt::get(intptr_ty, offset);
Sean Callanan9b6898f2011-07-30 02:42:06 +00002582
2583 llvm::Constant *offset_array[1];
2584
2585 offset_array[0] = offset_int;
2586
2587 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
2588
2589 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, offsets);
Sean Callananc0492742011-05-23 21:40:23 +00002590 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
2591
2592 return reloc_getbitcast;
2593}
2594
2595bool
2596IRForTarget::CompleteDataAllocation ()
2597{
Sean Callanan47dc4572011-09-15 02:13:07 +00002598 if (!m_data_allocator)
2599 return true;
2600
Sean Callananc0492742011-05-23 21:40:23 +00002601 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2602
2603 if (!m_data_allocator->GetStream().GetSize())
2604 return true;
2605
2606 lldb::addr_t allocation = m_data_allocator->Allocate();
2607
2608 if (log)
2609 {
2610 if (allocation)
2611 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2612 else
2613 log->Printf("Failed to allocate static data");
2614 }
2615
2616 if (!allocation)
2617 return false;
2618
Sean Callanan9b6898f2011-07-30 02:42:06 +00002619 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
2620 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc0492742011-05-23 21:40:23 +00002621
2622 Constant *relocated_addr = ConstantInt::get(intptr_ty, (uint64_t)allocation);
2623 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
2624
2625 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
2626
2627 m_reloc_placeholder->eraseFromParent();
2628
2629 return true;
2630}
2631
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002632bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00002633IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002634{
Greg Claytone005f2c2010-11-06 01:53:30 +00002635 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002636
Sean Callananc0492742011-05-23 21:40:23 +00002637 m_module = &llvm_module;
Sean Callananc7ca4662011-10-25 18:36:40 +00002638 m_target_data.reset(new TargetData(m_module));
Sean Callananc0492742011-05-23 21:40:23 +00002639
2640 Function* function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002641
2642 if (!function)
2643 {
2644 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002645 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00002646
2647 if (m_error_stream)
Sean Callananc0492742011-05-23 21:40:23 +00002648 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
Sean Callanan6a925532011-01-13 08:53:35 +00002649
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002650 return false;
2651 }
Sean Callananc0492742011-05-23 21:40:23 +00002652
2653 if (!FixFunctionLinkage (*function))
2654 {
2655 if (log)
2656 log->Printf("Couldn't fix the linkage for the function");
2657
2658 return false;
2659 }
2660
Sean Callananc7ca4662011-10-25 18:36:40 +00002661 if (log)
2662 {
2663 std::string s;
2664 raw_string_ostream oss(s);
2665
2666 m_module->print(oss, NULL);
2667
2668 oss.flush();
2669
2670 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
2671 }
2672
Sean Callanan9b6898f2011-07-30 02:42:06 +00002673 llvm::Type *intptr_ty = Type::getInt8Ty(m_module->getContext());
Sean Callananc0492742011-05-23 21:40:23 +00002674
2675 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
2676 intptr_ty,
2677 false /* isConstant */,
2678 GlobalVariable::InternalLinkage,
2679 Constant::getNullValue(intptr_ty),
2680 "reloc_placeholder",
2681 NULL /* InsertBefore */,
2682 false /* ThreadLocal */,
2683 0 /* AddressSpace */);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002684
Sean Callanan02fbafa2010-07-27 21:39:39 +00002685 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002686
Sean Callananc0492742011-05-23 21:40:23 +00002687 m_has_side_effects = HasSideEffects(*function);
Sean Callanan05a5a1b2010-12-16 03:17:46 +00002688
Sean Callanan82b74c82010-08-12 01:56:52 +00002689 ////////////////////////////////////////////////////////////
Greg Clayton8de27c72010-10-15 22:48:33 +00002690 // Replace $__lldb_expr_result with a persistent variable
Sean Callanan82b74c82010-08-12 01:56:52 +00002691 //
2692
Sean Callananc0492742011-05-23 21:40:23 +00002693 if (!CreateResultVariable(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002694 {
2695 if (log)
2696 log->Printf("CreateResultVariable() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002697
2698 // CreateResultVariable() reports its own errors, so we don't do so here
2699
Sean Callanan82b74c82010-08-12 01:56:52 +00002700 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002701 }
Sean Callanan47dc4572011-09-15 02:13:07 +00002702
2703 if (m_const_result && m_execution_policy != lldb_private::eExecutionPolicyAlways)
2704 {
2705 m_interpret_success = true;
Sean Callanan696cf5f2011-05-07 01:06:41 +00002706 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +00002707 }
2708
2709 for (bbi = function->begin();
2710 bbi != function->end();
2711 ++bbi)
2712 {
2713 if (!RemoveGuards(*bbi))
2714 {
2715 if (log)
2716 log->Printf("RemoveGuards() failed");
2717
2718 // RemoveGuards() reports its own errors, so we don't do so here
2719
2720 return false;
2721 }
2722
2723 if (!RewritePersistentAllocs(*bbi))
2724 {
2725 if (log)
2726 log->Printf("RewritePersistentAllocs() failed");
2727
2728 // RewritePersistentAllocs() reports its own errors, so we don't do so here
2729
2730 return false;
2731 }
Sean Callanan1850f942012-07-21 02:02:15 +00002732
2733 if (!RemoveCXAAtExit(*bbi))
2734 {
2735 if (log)
2736 log->Printf("RemoveCXAAtExit() failed");
2737
2738 // RemoveCXAAtExit() reports its own errors, so we don't do so here
2739
2740 return false;
2741 }
Sean Callanan47dc4572011-09-15 02:13:07 +00002742 }
2743
2744 if (m_decl_map && m_execution_policy != lldb_private::eExecutionPolicyAlways)
2745 {
2746 IRInterpreter interpreter (*m_decl_map,
2747 m_error_stream);
2748
Sean Callananddf110d2012-01-24 22:06:48 +00002749 interpreter.maybeRunOnFunction(m_const_result, m_result_name, m_result_type, *function, llvm_module, m_interpreter_error);
2750
2751 if (m_interpreter_error.Success())
Sean Callanan47dc4572011-09-15 02:13:07 +00002752 return true;
Sean Callanan47dc4572011-09-15 02:13:07 +00002753 }
Sean Callanan696cf5f2011-05-07 01:06:41 +00002754
Sean Callanan8f2e3922012-02-04 08:49:35 +00002755 if (m_execution_policy == lldb_private::eExecutionPolicyNever) {
Sean Callanan08210572012-04-24 17:56:40 +00002756 if (m_result_name)
2757 m_decl_map->RemoveResultVariable(m_result_name);
Sean Callanan8f2e3922012-02-04 08:49:35 +00002758 return false;
2759 }
2760
Sean Callanan7b8eb762011-11-01 17:33:54 +00002761 if (log && log->GetVerbose())
Sean Callananc0492742011-05-23 21:40:23 +00002762 {
2763 std::string s;
2764 raw_string_ostream oss(s);
2765
2766 m_module->print(oss, NULL);
2767
2768 oss.flush();
2769
2770 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2771 }
2772
Sean Callanan6ba533e2010-11-17 23:00:36 +00002773 ///////////////////////////////////////////////////////////////////////////////
2774 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2775 //
Sean Callanan6ba533e2010-11-17 23:00:36 +00002776
Sean Callananc0492742011-05-23 21:40:23 +00002777 if (!RewriteObjCConstStrings(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002778 {
2779 if (log)
2780 log->Printf("RewriteObjCConstStrings() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002781
2782 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
2783
Sean Callanan6ba533e2010-11-17 23:00:36 +00002784 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002785 }
Sean Callanan6ba533e2010-11-17 23:00:36 +00002786
Sean Callanan5c9a3c72011-08-04 21:37:47 +00002787 ///////////////////////////////
2788 // Resolve function pointers
2789 //
2790
2791 if (!ResolveFunctionPointers(llvm_module, *function))
2792 {
2793 if (log)
2794 log->Printf("ResolveFunctionPointers() failed");
2795
2796 // ResolveFunctionPointers() reports its own errors, so we don't do so here
2797
2798 return false;
2799 }
2800
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002801 for (bbi = function->begin();
2802 bbi != function->end();
2803 ++bbi)
2804 {
Sean Callananc0492742011-05-23 21:40:23 +00002805 if (!RewriteObjCSelectors(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002806 {
2807 if (log)
2808 log->Printf("RewriteObjCSelectors() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002809
2810 // RewriteObjCSelectors() reports its own errors, so we don't do so here
2811
Sean Callanana48fe162010-08-11 03:57:18 +00002812 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002813 }
Sean Callanana48fe162010-08-11 03:57:18 +00002814
Sean Callananc0492742011-05-23 21:40:23 +00002815 if (!ResolveCalls(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002816 {
2817 if (log)
2818 log->Printf("ResolveCalls() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002819
2820 // ResolveCalls() reports its own errors, so we don't do so here
2821
Sean Callanan8bce6652010-07-13 21:41:46 +00002822 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002823 }
Sean Callananc0492742011-05-23 21:40:23 +00002824
2825 if (!ReplaceStaticLiterals(*bbi))
2826 {
2827 if (log)
2828 log->Printf("ReplaceStaticLiterals() failed");
2829
2830 return false;
2831 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002832 }
2833
Sean Callanan771131d2010-09-30 21:18:25 +00002834 ///////////////////////////////
2835 // Run function-level passes
2836 //
2837
Sean Callananc0492742011-05-23 21:40:23 +00002838 if (!ResolveExternals(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002839 {
2840 if (log)
2841 log->Printf("ResolveExternals() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002842
2843 // ResolveExternals() reports its own errors, so we don't do so here
2844
Sean Callanan1d1b39c2010-11-08 00:31:32 +00002845 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002846 }
Sean Callanan1d1b39c2010-11-08 00:31:32 +00002847
Sean Callananc0492742011-05-23 21:40:23 +00002848 if (!ReplaceVariables(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002849 {
2850 if (log)
2851 log->Printf("ReplaceVariables() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002852
2853 // ReplaceVariables() reports its own errors, so we don't do so here
2854
Sean Callanan771131d2010-09-30 21:18:25 +00002855 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002856 }
Sean Callanan771131d2010-09-30 21:18:25 +00002857
Sean Callananc0492742011-05-23 21:40:23 +00002858 if (!ReplaceStrings())
2859 {
2860 if (log)
2861 log->Printf("ReplaceStrings() failed");
2862
2863 return false;
2864 }
2865
2866 if (!CompleteDataAllocation())
2867 {
2868 if (log)
2869 log->Printf("CompleteDataAllocation() failed");
2870
2871 return false;
2872 }
2873
Sean Callanan7b8eb762011-11-01 17:33:54 +00002874 if (log && log->GetVerbose())
Sean Callanan8bce6652010-07-13 21:41:46 +00002875 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00002876 std::string s;
2877 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00002878
Sean Callananc0492742011-05-23 21:40:23 +00002879 m_module->print(oss, NULL);
Sean Callanan321fe9e2010-07-28 01:00:59 +00002880
2881 oss.flush();
2882
Greg Claytonb5037af2010-11-15 01:47:11 +00002883 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002884 }
2885
2886 return true;
2887}
2888
2889void
Greg Clayton3c7feb42010-11-19 01:05:25 +00002890IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002891{
2892}
2893
2894PassManagerType
2895IRForTarget::getPotentialPassManagerType() const
2896{
2897 return PMT_ModulePassManager;
2898}