blob: fcb2fc6f779ae6f3ff8b939d5c8a2f3b0bf7d9e9 [file] [log] [blame]
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001//===-- IRForTarget.cpp -------------------------------------------*- C++ -*-===//
2//
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 Callananc0492742011-05-23 21:40:23 +000029#include "lldb/Host/Endian.h"
Sean Callanan696cf5f2011-05-07 01:06:41 +000030#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000031
32#include <map>
33
34using namespace llvm;
35
Sean Callanan3351dac2010-08-18 18:50:51 +000036static char ID;
37
Sean Callananc0492742011-05-23 21:40:23 +000038IRForTarget::StaticDataAllocator::StaticDataAllocator()
39{
40}
41
42IRForTarget::StaticDataAllocator::~StaticDataAllocator()
43{
44}
45
Greg Clayton3c7feb42010-11-19 01:05:25 +000046IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
47 bool resolve_vars,
Sean Callanan696cf5f2011-05-07 01:06:41 +000048 lldb::ClangExpressionVariableSP &const_result,
Sean Callananc0492742011-05-23 21:40:23 +000049 StaticDataAllocator *data_allocator,
Sean Callanan97c924e2011-01-27 01:07:04 +000050 lldb_private::Stream *error_stream,
Greg Clayton3c7feb42010-11-19 01:05:25 +000051 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +000052 ModulePass(ID),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000053 m_resolve_vars(resolve_vars),
54 m_func_name(func_name),
Sean Callananc0492742011-05-23 21:40:23 +000055 m_module(NULL),
Johnny Chen2bc9eb32011-07-19 19:48:13 +000056 m_decl_map(decl_map),
57 m_data_allocator(data_allocator),
Sean Callanan6ba533e2010-11-17 23:00:36 +000058 m_CFStringCreateWithBytes(NULL),
Sean Callanan65dafa82010-08-27 01:01:44 +000059 m_sel_registerName(NULL),
Johnny Chen2bc9eb32011-07-19 19:48:13 +000060 m_const_result(const_result),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000061 m_error_stream(error_stream),
Sean Callanan6a925532011-01-13 08:53:35 +000062 m_has_side_effects(false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000063 m_result_store(NULL),
64 m_result_is_pointer(false),
Sean Callananc0492742011-05-23 21:40:23 +000065 m_reloc_placeholder(NULL)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000066{
67}
68
Sean Callanan771131d2010-09-30 21:18:25 +000069/* Handy utility functions used at several places in the code */
Sean Callanana48fe162010-08-11 03:57:18 +000070
71static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000072PrintValue(const Value *value, bool truncate = false)
Sean Callanana48fe162010-08-11 03:57:18 +000073{
74 std::string s;
75 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000076 value->print(rso);
Sean Callanana48fe162010-08-11 03:57:18 +000077 rso.flush();
78 if (truncate)
79 s.resize(s.length() - 1);
80 return s;
81}
82
Sean Callanan771131d2010-09-30 21:18:25 +000083static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000084PrintType(const Type *type, bool truncate = false)
Sean Callanan771131d2010-09-30 21:18:25 +000085{
86 std::string s;
87 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000088 type->print(rso);
Sean Callanan771131d2010-09-30 21:18:25 +000089 rso.flush();
90 if (truncate)
91 s.resize(s.length() - 1);
92 return s;
93}
94
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000095IRForTarget::~IRForTarget()
96{
97}
98
Sean Callananc0492742011-05-23 21:40:23 +000099bool
100IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
101{
102 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
103
104 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
105
106 std::string name = llvm_function.getNameStr();
107
108 return true;
109}
110
Sean Callanan82b74c82010-08-12 01:56:52 +0000111bool
Sean Callananc0492742011-05-23 21:40:23 +0000112IRForTarget::HasSideEffects (llvm::Function &llvm_function)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000113{
114 llvm::Function::iterator bbi;
115 BasicBlock::iterator ii;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000116
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000117 for (bbi = llvm_function.begin();
118 bbi != llvm_function.end();
119 ++bbi)
120 {
121 BasicBlock &basic_block = *bbi;
122
123 for (ii = basic_block.begin();
124 ii != basic_block.end();
125 ++ii)
126 {
127 switch (ii->getOpcode())
128 {
129 default:
130 return true;
131 case Instruction::Store:
132 {
133 StoreInst *store_inst = dyn_cast<StoreInst>(ii);
134
135 Value *store_ptr = store_inst->getPointerOperand();
136
Sean Callanan696cf5f2011-05-07 01:06:41 +0000137 std::string ptr_name;
138
139 if (store_ptr->hasName())
140 ptr_name = store_ptr->getNameStr();
141
142 if (isa <AllocaInst> (store_ptr))
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000143 break;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000144
145 if (ptr_name.find("$__lldb_expr_result") != std::string::npos)
146 {
147 if (ptr_name.find("GV") == std::string::npos)
148 m_result_store = store_inst;
149 }
150 else
151 {
152 return true;
153 }
154
155 break;
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000156 }
157 case Instruction::Load:
158 case Instruction::Alloca:
159 case Instruction::GetElementPtr:
Sean Callanan696cf5f2011-05-07 01:06:41 +0000160 case Instruction::BitCast:
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000161 case Instruction::Ret:
Sean Callanan696cf5f2011-05-07 01:06:41 +0000162 case Instruction::ICmp:
163 case Instruction::Br:
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000164 break;
165 }
166 }
167 }
168
169 return false;
170}
171
Sean Callanan5c9a3c72011-08-04 21:37:47 +0000172bool
173IRForTarget::GetFunctionAddress (llvm::Function *fun,
174 uint64_t &fun_addr,
175 lldb_private::ConstString &name,
176 Constant **&value_ptr)
177{
178 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
179
180 fun_addr = LLDB_INVALID_ADDRESS;
181 name.Clear();
182 value_ptr = NULL;
183
184 if (fun->isIntrinsic())
185 {
186 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
187
188 switch (intrinsic_id)
189 {
190 default:
191 if (log)
192 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
193
194 if (m_error_stream)
195 m_error_stream->Printf("Internal error [IRForTarget]: Call to unhandled compiler intrinsic '%s'\n", Intrinsic::getName(intrinsic_id).c_str());
196
197 return false;
198 case Intrinsic::memcpy:
199 {
200 static lldb_private::ConstString g_memcpy_str ("memcpy");
201 name = g_memcpy_str;
202 }
203 break;
204 }
205
206 if (log && name)
207 log->Printf("Resolved intrinsic name \"%s\"", name.GetCString());
208 }
209 else
210 {
211 name.SetCStringWithLength (fun->getName().data(), fun->getName().size());
212 }
213
214 // Find the address of the function.
215
216 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
217 Value **fun_value_ptr = NULL;
218
219 if (fun_decl)
220 {
221 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_value_ptr, fun_addr))
222 {
223 fun_value_ptr = NULL;
224
225 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
226 {
227 if (log)
228 log->Printf("Function \"%s\" had no address", name.GetCString());
229
230 if (m_error_stream)
231 m_error_stream->Printf("Error [IRForTarget]: Call to a function '%s' that is not present in the target\n", name.GetCString());
232
233 return false;
234 }
235 }
236 }
237 else
238 {
239 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
240 {
241 if (log)
242 log->Printf ("Metadataless function \"%s\" had no address", name.GetCString());
243
244 if (m_error_stream)
245 m_error_stream->Printf("Error [IRForTarget]: Call to a symbol-only function '%s' that is not present in the target\n", name.GetCString());
246
247 return false;
248 }
249 }
250
251 if (log)
252 log->Printf("Found \"%s\" at 0x%llx", name.GetCString(), fun_addr);
253
254 return true;
255}
256
257llvm::Constant *
258IRForTarget::BuildFunctionPointer (llvm::Type *type,
259 uint64_t ptr)
260{
261 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
262 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
263 PointerType *fun_ptr_ty = PointerType::getUnqual(type);
264 Constant *fun_addr_int = ConstantInt::get(intptr_ty, ptr, false);
265 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
266}
267
268bool
269IRForTarget::ResolveFunctionPointers(llvm::Module &llvm_module,
270 llvm::Function &llvm_function)
271{
272 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
273
274 for (llvm::Module::iterator fi = llvm_module.begin();
275 fi != llvm_module.end();
276 ++fi)
277 {
278 Function *fun = fi;
279
280 bool is_decl = fun->isDeclaration();
281
282 if (log)
283 log->Printf("Examining %s function %s", (is_decl ? "declaration" : "non-declaration"), fun->getNameStr().c_str());
284
285 if (!is_decl)
286 continue;
287
288 if (fun->hasNUses(0))
289 continue; // ignore
290
291 uint64_t addr = LLDB_INVALID_ADDRESS;
292 lldb_private::ConstString name;
293 Constant **value_ptr = NULL;
294
295 if (!GetFunctionAddress(fun,
296 addr,
297 name,
298 value_ptr))
299 return false; // GetFunctionAddress reports its own errors
300
301 Constant *value = BuildFunctionPointer(fun->getFunctionType(), addr);
302
303 if (value_ptr)
304 *value_ptr = value;
305
306 fun->replaceAllUsesWith(value);
307 }
308
309 return true;
310}
311
Sean Callanan696cf5f2011-05-07 01:06:41 +0000312clang::NamedDecl *
Sean Callananc0492742011-05-23 21:40:23 +0000313IRForTarget::DeclForGlobal (GlobalValue *global_val)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000314{
315 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
316
Sean Callananc0492742011-05-23 21:40:23 +0000317 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan696cf5f2011-05-07 01:06:41 +0000318
319 if (!named_metadata)
320 return NULL;
321
322 unsigned num_nodes = named_metadata->getNumOperands();
323 unsigned node_index;
324
325 for (node_index = 0;
326 node_index < num_nodes;
327 ++node_index)
328 {
329 MDNode *metadata_node = named_metadata->getOperand(node_index);
330
331 if (!metadata_node)
332 return NULL;
333
334 if (metadata_node->getNumOperands() != 2)
335 continue;
336
337 if (metadata_node->getOperand(0) != global_val)
338 continue;
339
340 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
341
342 if (!constant_int)
343 return NULL;
344
345 uintptr_t ptr = constant_int->getZExtValue();
346
347 return reinterpret_cast<clang::NamedDecl *>(ptr);
348 }
349
350 return NULL;
351}
352
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000353void
354IRForTarget::MaybeSetConstantResult (llvm::Constant *initializer,
355 const lldb_private::ConstString &name,
356 lldb_private::TypeFromParser type)
357{
Sean Callanan696cf5f2011-05-07 01:06:41 +0000358 if (llvm::ConstantExpr *init_expr = dyn_cast<llvm::ConstantExpr>(initializer))
359 {
360 switch (init_expr->getOpcode())
361 {
362 default:
363 return;
364 case Instruction::IntToPtr:
365 MaybeSetConstantResult (init_expr->getOperand(0), name, type);
366 return;
367 }
368 }
369 else if (llvm::ConstantInt *init_int = dyn_cast<llvm::ConstantInt>(initializer))
370 {
371 m_const_result = m_decl_map->BuildIntegerVariable(name, type, init_int->getValue());
372 }
373}
374
375void
Sean Callananc0492742011-05-23 21:40:23 +0000376IRForTarget::MaybeSetCastResult (lldb_private::TypeFromParser type)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000377{
378 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
379
380 if (!m_result_store)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000381 return;
382
Sean Callanan696cf5f2011-05-07 01:06:41 +0000383 LoadInst *original_load = NULL;
384
385 for (llvm::Value *current_value = m_result_store->getValueOperand(), *next_value;
386 current_value != NULL;
387 current_value = next_value)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000388 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000389 CastInst *cast_inst = dyn_cast<CastInst>(current_value);
390 LoadInst *load_inst = dyn_cast<LoadInst>(current_value);
391
392 if (cast_inst)
393 {
394 next_value = cast_inst->getOperand(0);
395 }
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000396 else if (load_inst)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000397 {
398 if (isa<LoadInst>(load_inst->getPointerOperand()))
399 {
400 next_value = load_inst->getPointerOperand();
401 }
402 else
403 {
404 original_load = load_inst;
405 break;
406 }
407 }
408 else
409 {
410 return;
411 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000412 }
Sean Callanan696cf5f2011-05-07 01:06:41 +0000413
414 Value *loaded_value = original_load->getPointerOperand();
415 GlobalVariable *loaded_global = dyn_cast<GlobalVariable>(loaded_value);
416
417 if (!loaded_global)
418 return;
419
Sean Callananc0492742011-05-23 21:40:23 +0000420 clang::NamedDecl *loaded_decl = DeclForGlobal(loaded_global);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000421
422 if (!loaded_decl)
423 return;
424
425 clang::VarDecl *loaded_var = dyn_cast<clang::VarDecl>(loaded_decl);
426
427 if (!loaded_var)
428 return;
429
430 if (log)
431 {
432 lldb_private::StreamString type_desc_stream;
433 type.DumpTypeDescription(&type_desc_stream);
434
435 log->Printf("Type to cast variable to: \"%s\"", type_desc_stream.GetString().c_str());
436 }
437
438 m_const_result = m_decl_map->BuildCastVariable(m_result_name, loaded_var, type);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000439}
440
441bool
Sean Callananc0492742011-05-23 21:40:23 +0000442IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanan82b74c82010-08-12 01:56:52 +0000443{
Greg Claytone005f2c2010-11-06 01:53:30 +0000444 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan82b74c82010-08-12 01:56:52 +0000445
Sean Callanane8a59a82010-09-13 21:34:21 +0000446 if (!m_resolve_vars)
447 return true;
448
449 // Find the result variable. If it doesn't exist, we can give up right here.
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000450
Sean Callananc0492742011-05-23 21:40:23 +0000451 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000452
Sean Callanan9b6898f2011-07-30 02:42:06 +0000453 std::string result_name_str;
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000454 const char *result_name = NULL;
455
456 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
457 vi != ve;
458 ++vi)
459 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000460 result_name_str = vi->first().str();
461 const char *value_name = result_name_str.c_str();
462
463 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
464 !strstr(value_name, "GV"))
Sean Callanan6a925532011-01-13 08:53:35 +0000465 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000466 result_name = value_name;
Sean Callanan6a925532011-01-13 08:53:35 +0000467 m_result_is_pointer = true;
468 break;
469 }
470
Sean Callanan9b6898f2011-07-30 02:42:06 +0000471 if (strstr(value_name, "$__lldb_expr_result") &&
472 !strstr(value_name, "GV"))
Sean Callananc04743d2010-09-28 21:13:03 +0000473 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000474 result_name = value_name;
Sean Callanan6a925532011-01-13 08:53:35 +0000475 m_result_is_pointer = false;
Sean Callananc04743d2010-09-28 21:13:03 +0000476 break;
477 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000478 }
479
480 if (!result_name)
481 {
482 if (log)
483 log->PutCString("Couldn't find result variable");
484
485 return true;
486 }
487
Sean Callananc04743d2010-09-28 21:13:03 +0000488 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000489 log->Printf("Result name: \"%s\"", result_name);
Sean Callananc04743d2010-09-28 21:13:03 +0000490
Sean Callananc0492742011-05-23 21:40:23 +0000491 Value *result_value = m_module->getNamedValue(result_name);
Sean Callanan82b74c82010-08-12 01:56:52 +0000492
493 if (!result_value)
494 {
495 if (log)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000496 log->PutCString("Result variable had no data");
Sean Callanan6a925532011-01-13 08:53:35 +0000497
Sean Callanan97c924e2011-01-27 01:07:04 +0000498 if (m_error_stream)
499 m_error_stream->Printf("Internal error [IRForTarget]: Result variable's name (%s) exists, but not its definition\n", result_name);
500
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000501 return false;
Sean Callanan82b74c82010-08-12 01:56:52 +0000502 }
Sean Callanane8a59a82010-09-13 21:34:21 +0000503
Sean Callanan82b74c82010-08-12 01:56:52 +0000504 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000505 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sean Callanan82b74c82010-08-12 01:56:52 +0000506
507 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
508
509 if (!result_global)
510 {
511 if (log)
512 log->PutCString("Result variable isn't a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000513
514 if (m_error_stream)
515 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) is defined, but is not a global variable\n", result_name);
516
Sean Callanan82b74c82010-08-12 01:56:52 +0000517 return false;
518 }
519
Sean Callananc0492742011-05-23 21:40:23 +0000520 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000521 if (!result_decl)
Sean Callanan82b74c82010-08-12 01:56:52 +0000522 {
523 if (log)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000524 log->PutCString("Result variable doesn't have a corresponding Decl");
Sean Callanan82b74c82010-08-12 01:56:52 +0000525
Sean Callanan97c924e2011-01-27 01:07:04 +0000526 if (m_error_stream)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000527 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 +0000528
Sean Callanan82b74c82010-08-12 01:56:52 +0000529 return false;
530 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000531
Sean Callanan696cf5f2011-05-07 01:06:41 +0000532 if (log)
Sean Callanan82b74c82010-08-12 01:56:52 +0000533 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000534 std::string decl_desc_str;
535 raw_string_ostream decl_desc_stream(decl_desc_str);
536 result_decl->print(decl_desc_stream);
537 decl_desc_stream.flush();
Sean Callanan82b74c82010-08-12 01:56:52 +0000538
Sean Callanan696cf5f2011-05-07 01:06:41 +0000539 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanan82b74c82010-08-12 01:56:52 +0000540 }
541
Sean Callanan696cf5f2011-05-07 01:06:41 +0000542 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
543 if (!result_var)
Sean Callanan82b74c82010-08-12 01:56:52 +0000544 {
545 if (log)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000546 log->PutCString("Result variable Decl isn't a VarDecl");
Sean Callanan97c924e2011-01-27 01:07:04 +0000547
548 if (m_error_stream)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000549 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 +0000550
Sean Callanan82b74c82010-08-12 01:56:52 +0000551 return false;
552 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000553
Sean Callanan82b74c82010-08-12 01:56:52 +0000554 // Get the next available result name from m_decl_map and create the persistent
555 // variable for it
556
Sean Callanan6a925532011-01-13 08:53:35 +0000557 lldb_private::TypeFromParser result_decl_type;
558
Sean Callanan696cf5f2011-05-07 01:06:41 +0000559 // If the result is an Lvalue, it is emitted as a pointer; see
560 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan6a925532011-01-13 08:53:35 +0000561 if (m_result_is_pointer)
562 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000563 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanand5b3c352011-01-27 04:42:51 +0000564 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
565 const clang::PointerType *pointer_pointertype = dyn_cast<clang::PointerType>(pointer_type);
Sean Callanan6a925532011-01-13 08:53:35 +0000566
567 if (!pointer_pointertype)
568 {
569 if (log)
570 log->PutCString("Expected result to have pointer type, but it did not");
Sean Callanan97c924e2011-01-27 01:07:04 +0000571
572 if (m_error_stream)
573 m_error_stream->Printf("Internal error [IRForTarget]: Lvalue result (%s) is not a pointer variable\n", result_name);
574
Sean Callanan6a925532011-01-13 08:53:35 +0000575 return false;
576 }
577
578 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
579
580 result_decl_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
581 &result_decl->getASTContext());
582 }
583 else
584 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000585 result_decl_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Sean Callanan6a925532011-01-13 08:53:35 +0000586 &result_decl->getASTContext());
587 }
588
Sean Callanan696cf5f2011-05-07 01:06:41 +0000589 if (log)
590 {
591 lldb_private::StreamString type_desc_stream;
592 result_decl_type.DumpTypeDescription(&type_desc_stream);
593
594 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetString().c_str());
595 }
596
Sean Callanan6a925532011-01-13 08:53:35 +0000597 m_result_name = m_decl_map->GetPersistentResultName();
Sean Callanan82b74c82010-08-12 01:56:52 +0000598
599 if (log)
Sean Callanan6a925532011-01-13 08:53:35 +0000600 log->Printf("Creating a new result global: \"%s\"", m_result_name.GetCString());
Sean Callanan82b74c82010-08-12 01:56:52 +0000601
602 // Construct a new result global and set up its metadata
603
Sean Callananc0492742011-05-23 21:40:23 +0000604 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanan82b74c82010-08-12 01:56:52 +0000605 result_global->getType()->getElementType(),
606 false, /* not constant */
607 GlobalValue::ExternalLinkage,
608 NULL, /* no initializer */
Sean Callanan6a925532011-01-13 08:53:35 +0000609 m_result_name.GetCString ());
Sean Callanan82b74c82010-08-12 01:56:52 +0000610
611 // It's too late in compilation to create a new VarDecl for this, but we don't
612 // need to. We point the metadata at the old VarDecl. This creates an odd
613 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton8de27c72010-10-15 22:48:33 +0000614 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanan82b74c82010-08-12 01:56:52 +0000615 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
616 // fixed up.
617
Sean Callananc0492742011-05-23 21:40:23 +0000618 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan696cf5f2011-05-07 01:06:41 +0000619 reinterpret_cast<uint64_t>(result_decl),
Sean Callanan82b74c82010-08-12 01:56:52 +0000620 false);
621
622 llvm::Value* values[2];
623 values[0] = new_result_global;
624 values[1] = new_constant_int;
625
Sean Callanan0de254a2011-05-15 22:34:38 +0000626 ArrayRef<Value*> value_ref(values, 2);
627
Sean Callananc0492742011-05-23 21:40:23 +0000628 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
629 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan82b74c82010-08-12 01:56:52 +0000630 named_metadata->addOperand(persistent_global_md);
631
632 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000633 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan2e2db532010-09-07 22:43:19 +0000634 PrintValue(result_global).c_str(),
Sean Callanan82b74c82010-08-12 01:56:52 +0000635 PrintValue(new_result_global).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000636
637 if (result_global->hasNUses(0))
638 {
639 // We need to synthesize a store for this variable, because otherwise
640 // there's nothing to put into its equivalent persistent variable.
Sean Callanan82b74c82010-08-12 01:56:52 +0000641
Greg Clayton8de27c72010-10-15 22:48:33 +0000642 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan2e2db532010-09-07 22:43:19 +0000643 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
644
645 if (!first_entry_instruction)
646 return false;
647
648 if (!result_global->hasInitializer())
649 {
650 if (log)
651 log->Printf("Couldn't find initializer for unused variable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000652
653 if (m_error_stream)
654 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) has no writes and no initializer\n", result_name);
655
Sean Callanan2e2db532010-09-07 22:43:19 +0000656 return false;
657 }
658
659 Constant *initializer = result_global->getInitializer();
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000660
661 // Here we write the initializer into a result variable assuming it
662 // can be computed statically.
663
664 if (!m_has_side_effects)
665 {
666 MaybeSetConstantResult (initializer,
Sean Callanan6a925532011-01-13 08:53:35 +0000667 m_result_name,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000668 result_decl_type);
669 }
Sean Callanan2e2db532010-09-07 22:43:19 +0000670
Greg Clayton2c344ca2011-02-05 02:28:58 +0000671 StoreInst *synthesized_store = new StoreInst(initializer,
672 new_result_global,
673 first_entry_instruction);
Sean Callanan2e2db532010-09-07 22:43:19 +0000674
675 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000676 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000677 }
678 else
679 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000680 if (!m_has_side_effects && lldb_private::ClangASTContext::IsPointerType (result_decl_type.GetOpaqueQualType()))
681 {
Sean Callananc0492742011-05-23 21:40:23 +0000682 MaybeSetCastResult (result_decl_type);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000683 }
684
Sean Callanan2e2db532010-09-07 22:43:19 +0000685 result_global->replaceAllUsesWith(new_result_global);
686 }
Sean Callanan696cf5f2011-05-07 01:06:41 +0000687
688 if (!m_const_result)
689 m_decl_map->AddPersistentVariable(result_decl,
690 m_result_name,
691 result_decl_type,
692 true,
693 m_result_is_pointer);
Sean Callanan2e2db532010-09-07 22:43:19 +0000694
Sean Callanan82b74c82010-08-12 01:56:52 +0000695 result_global->eraseFromParent();
696
697 return true;
698}
699
Johnny Chen58021cc2011-08-09 23:10:20 +0000700#if 0
Greg Clayton3c7feb42010-11-19 01:05:25 +0000701static void DebugUsers(lldb::LogSP &log, Value *value, uint8_t depth)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000702{
703 if (!depth)
704 return;
705
706 depth--;
707
Johnny Chen58021cc2011-08-09 23:10:20 +0000708 if (log)
709 log->Printf(" <Begin %d users>", value->getNumUses());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000710
Greg Clayton3c7feb42010-11-19 01:05:25 +0000711 for (Value::use_iterator ui = value->use_begin(), ue = value->use_end();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000712 ui != ue;
713 ++ui)
714 {
Johnny Chen58021cc2011-08-09 23:10:20 +0000715 if (log)
716 log->Printf(" <Use %p> %s", *ui, PrintValue(*ui).c_str());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000717 DebugUsers(log, *ui, depth);
718 }
719
Johnny Chen58021cc2011-08-09 23:10:20 +0000720 if (log)
721 log->Printf(" <End uses>");
Sean Callanan6ba533e2010-11-17 23:00:36 +0000722}
Johnny Chen58021cc2011-08-09 23:10:20 +0000723#endif
Sean Callanan6ba533e2010-11-17 23:00:36 +0000724
725bool
Sean Callananc0492742011-05-23 21:40:23 +0000726IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Greg Clayton3c7feb42010-11-19 01:05:25 +0000727 llvm::GlobalVariable *cstr,
728 Instruction *FirstEntryInstruction)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000729{
730 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
731
Sean Callanan9b6898f2011-07-30 02:42:06 +0000732 Type *ns_str_ty = ns_str->getType();
Sean Callananc0492742011-05-23 21:40:23 +0000733
Sean Callanan9b6898f2011-07-30 02:42:06 +0000734 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
735 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
Sean Callananc0492742011-05-23 21:40:23 +0000736 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan9b6898f2011-07-30 02:42:06 +0000737 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
738 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000739
740 if (!m_CFStringCreateWithBytes)
741 {
742 lldb::addr_t CFStringCreateWithBytes_addr;
743
744 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
745
746 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
747 {
748 if (log)
749 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
750
Sean Callanan97c924e2011-01-27 01:07:04 +0000751 if (m_error_stream)
752 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
753
Sean Callanan6ba533e2010-11-17 23:00:36 +0000754 return false;
755 }
756
757 if (log)
758 log->Printf("Found CFStringCreateWithBytes at 0x%llx", CFStringCreateWithBytes_addr);
759
760 // Build the function type:
761 //
762 // CFStringRef CFStringCreateWithBytes (
763 // CFAllocatorRef alloc,
764 // const UInt8 *bytes,
765 // CFIndex numBytes,
766 // CFStringEncoding encoding,
767 // Boolean isExternalRepresentation
768 // );
769 //
770 // We make the following substitutions:
771 //
772 // CFStringRef -> i8*
773 // CFAllocatorRef -> i8*
774 // UInt8 * -> i8*
775 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
776 // CFStringEncoding -> i32
777 // Boolean -> i8
778
Sean Callanan9b6898f2011-07-30 02:42:06 +0000779 Type *arg_type_array[5];
780
781 arg_type_array[0] = i8_ptr_ty;
782 arg_type_array[1] = i8_ptr_ty;
783 arg_type_array[2] = intptr_ty;
784 arg_type_array[3] = i32_ty;
785 arg_type_array[4] = i8_ty;
786
787 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
788
Sean Callananc0492742011-05-23 21:40:23 +0000789 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000790
791 // Build the constant containing the pointer to the function
792 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
793 Constant *CFSCWB_addr_int = ConstantInt::get(intptr_ty, CFStringCreateWithBytes_addr, false);
794 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
795 }
796
Sean Callanan58baaad2011-07-08 00:39:14 +0000797 ConstantArray *string_array = NULL;
Sean Callanan0ece5262011-02-10 22:17:53 +0000798
799 if (cstr)
800 string_array = dyn_cast<ConstantArray>(cstr->getInitializer());
Sean Callanan9b6898f2011-07-30 02:42:06 +0000801
Sean Callanan6ba533e2010-11-17 23:00:36 +0000802 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan0ece5262011-02-10 22:17:53 +0000803 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
804 Constant *numBytes_arg = ConstantInt::get(intptr_ty, cstr ? string_array->getType()->getNumElements() - 1 : 0, false);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000805 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
806 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
807
Sean Callanan9b6898f2011-07-30 02:42:06 +0000808 Value *argument_array[5];
809
810 argument_array[0] = alloc_arg;
811 argument_array[1] = bytes_arg;
812 argument_array[2] = numBytes_arg;
813 argument_array[3] = encoding_arg;
814 argument_array[4] = isExternal_arg;
815
816 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000817
818 CallInst *CFSCWB_call = CallInst::Create(m_CFStringCreateWithBytes,
Sean Callanan9b6898f2011-07-30 02:42:06 +0000819 CFSCWB_arguments,
Sean Callanan6ba533e2010-11-17 23:00:36 +0000820 "CFStringCreateWithBytes",
821 FirstEntryInstruction);
Sean Callananae71e302010-11-18 22:21:58 +0000822
Greg Clayton3c7feb42010-11-19 01:05:25 +0000823 if (!UnfoldConstant(ns_str, CFSCWB_call, FirstEntryInstruction))
Sean Callanan6ba533e2010-11-17 23:00:36 +0000824 {
825 if (log)
826 log->PutCString("Couldn't replace the NSString with the result of the call");
827
Sean Callanan97c924e2011-01-27 01:07:04 +0000828 if (m_error_stream)
829 m_error_stream->Printf("Error [IRForTarget]: Couldn't replace an Objective-C constant string with a dynamic string\n");
830
Sean Callanan6ba533e2010-11-17 23:00:36 +0000831 return false;
832 }
833
Greg Clayton3c7feb42010-11-19 01:05:25 +0000834 ns_str->eraseFromParent();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000835
836 return true;
837}
838
839bool
Sean Callananc0492742011-05-23 21:40:23 +0000840IRForTarget::RewriteObjCConstStrings(Function &llvm_function)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000841{
842 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
843
Sean Callananc0492742011-05-23 21:40:23 +0000844 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000845
Greg Clayton3c7feb42010-11-19 01:05:25 +0000846 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000847 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
848
849 if (!FirstEntryInstruction)
850 {
851 if (log)
852 log->PutCString("Couldn't find first instruction for rewritten Objective-C strings");
853
Sean Callanan97c924e2011-01-27 01:07:04 +0000854 if (m_error_stream)
855 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the location for calls to CFStringCreateWithBytes\n");
856
Sean Callanan6ba533e2010-11-17 23:00:36 +0000857 return false;
858 }
859
860 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
861 vi != ve;
862 ++vi)
863 {
Sean Callanan9b6898f2011-07-30 02:42:06 +0000864 std::string value_name = vi->first().str();
865 const char *value_name_cstr = value_name.c_str();
866
867 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callanan6ba533e2010-11-17 23:00:36 +0000868 {
869 Value *nsstring_value = vi->second;
870
871 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
872
873 if (!nsstring_global)
874 {
875 if (log)
876 log->PutCString("NSString variable is not a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000877
878 if (m_error_stream)
879 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a global variable\n");
880
Sean Callanan6ba533e2010-11-17 23:00:36 +0000881 return false;
882 }
883
884 if (!nsstring_global->hasInitializer())
885 {
886 if (log)
887 log->PutCString("NSString variable does not have an initializer");
Sean Callanan97c924e2011-01-27 01:07:04 +0000888
889 if (m_error_stream)
890 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have an initializer\n");
891
Sean Callanan6ba533e2010-11-17 23:00:36 +0000892 return false;
893 }
894
895 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
896
897 if (!nsstring_struct)
898 {
899 if (log)
900 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sean Callanan97c924e2011-01-27 01:07:04 +0000901
902 if (m_error_stream)
903 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a structure constant\n");
904
Sean Callanan6ba533e2010-11-17 23:00:36 +0000905 return false;
906 }
907
908 // We expect the following structure:
909 //
910 // struct {
911 // int *isa;
912 // int flags;
913 // char *str;
914 // long length;
915 // };
916
917 if (nsstring_struct->getNumOperands() != 4)
918 {
919 if (log)
920 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 +0000921
922 if (m_error_stream)
923 m_error_stream->Printf("Internal error [IRForTarget]: The struct for an Objective-C constant string is not as expected\n");
924
Sean Callanan6ba533e2010-11-17 23:00:36 +0000925 return false;
926 }
927
928 Constant *nsstring_member = nsstring_struct->getOperand(2);
929
930 if (!nsstring_member)
931 {
932 if (log)
933 log->PutCString("NSString initializer's str element was empty");
Sean Callanan97c924e2011-01-27 01:07:04 +0000934
935 if (m_error_stream)
936 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have a string initializer\n");
937
Sean Callanan6ba533e2010-11-17 23:00:36 +0000938 return false;
939 }
940
941 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
942
943 if (!nsstring_expr)
944 {
945 if (log)
946 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sean Callanan97c924e2011-01-27 01:07:04 +0000947
948 if (m_error_stream)
949 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not constant\n");
950
Sean Callanan6ba533e2010-11-17 23:00:36 +0000951 return false;
952 }
953
954 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
955 {
956 if (log)
957 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 +0000958
959 if (m_error_stream)
960 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not an array\n");
961
Sean Callanan6ba533e2010-11-17 23:00:36 +0000962 return false;
963 }
964
965 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
966
967 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
968
969 if (!cstr_global)
970 {
971 if (log)
972 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000973
974 if (m_error_stream)
975 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 +0000976
Sean Callanan6ba533e2010-11-17 23:00:36 +0000977 return false;
978 }
979
980 if (!cstr_global->hasInitializer())
981 {
982 if (log)
983 log->PutCString("NSString initializer's str element does not have an initializer");
Sean Callanan97c924e2011-01-27 01:07:04 +0000984
985 if (m_error_stream)
986 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to initialized data\n");
987
Sean Callanan6ba533e2010-11-17 23:00:36 +0000988 return false;
989 }
Sean Callanan0ece5262011-02-10 22:17:53 +0000990
991 /*
Sean Callanan6ba533e2010-11-17 23:00:36 +0000992 if (!cstr_array)
993 {
994 if (log)
995 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sean Callanan97c924e2011-01-27 01:07:04 +0000996
997 if (m_error_stream)
998 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to an array\n");
999
Sean Callanan6ba533e2010-11-17 23:00:36 +00001000 return false;
1001 }
1002
1003 if (!cstr_array->isCString())
1004 {
1005 if (log)
1006 log->PutCString("NSString initializer's str element is not a C string array");
Sean Callanan97c924e2011-01-27 01:07:04 +00001007
1008 if (m_error_stream)
1009 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a C string\n");
1010
Sean Callanan6ba533e2010-11-17 23:00:36 +00001011 return false;
1012 }
Sean Callanan0ece5262011-02-10 22:17:53 +00001013 */
1014
1015 ConstantArray *cstr_array = dyn_cast<ConstantArray>(cstr_global->getInitializer());
Sean Callanan6ba533e2010-11-17 23:00:36 +00001016
1017 if (log)
Sean Callanan0ece5262011-02-10 22:17:53 +00001018 {
1019 if (cstr_array)
Sean Callanan9b6898f2011-07-30 02:42:06 +00001020 log->Printf("Found NSString constant %s, which contains \"%s\"", value_name_cstr, cstr_array->getAsString().c_str());
Sean Callanan0ece5262011-02-10 22:17:53 +00001021 else
Sean Callanan9b6898f2011-07-30 02:42:06 +00001022 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan0ece5262011-02-10 22:17:53 +00001023 }
1024
1025 if (!cstr_array)
1026 cstr_global = NULL;
Sean Callanan6ba533e2010-11-17 23:00:36 +00001027
Sean Callananc0492742011-05-23 21:40:23 +00001028 if (!RewriteObjCConstString(nsstring_global, cstr_global, FirstEntryInstruction))
Sean Callanan97c924e2011-01-27 01:07:04 +00001029 {
Sean Callanan6ba533e2010-11-17 23:00:36 +00001030 if (log)
1031 log->PutCString("Error rewriting the constant string");
Sean Callanan97c924e2011-01-27 01:07:04 +00001032
1033 // We don't print an error message here because RewriteObjCConstString has done so for us.
1034
Sean Callanan6ba533e2010-11-17 23:00:36 +00001035 return false;
1036 }
Sean Callanan6ba533e2010-11-17 23:00:36 +00001037 }
1038 }
1039
1040 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1041 vi != ve;
1042 ++vi)
1043 {
Sean Callanan9b6898f2011-07-30 02:42:06 +00001044 std::string value_name = vi->first().str();
1045 const char *value_name_cstr = value_name.c_str();
1046
1047 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callanan6ba533e2010-11-17 23:00:36 +00001048 {
1049 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
1050
1051 if (!gv)
1052 {
1053 if (log)
1054 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sean Callanan97c924e2011-01-27 01:07:04 +00001055
1056 if (m_error_stream)
1057 m_error_stream->Printf("Internal error [IRForTarget]: Found a CFConstantStringClassReference, but it is not a global object\n");
1058
Sean Callanan6ba533e2010-11-17 23:00:36 +00001059 return false;
1060 }
1061
1062 gv->eraseFromParent();
1063
1064 break;
1065 }
1066 }
1067
1068 return true;
1069}
1070
Greg Clayton3c7feb42010-11-19 01:05:25 +00001071static bool IsObjCSelectorRef (Value *value)
Sean Callananf5857a02010-07-31 01:32:05 +00001072{
Greg Clayton3c7feb42010-11-19 01:05:25 +00001073 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sean Callananf5857a02010-07-31 01:32:05 +00001074
Greg Clayton3c7feb42010-11-19 01:05:25 +00001075 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
Sean Callananf5857a02010-07-31 01:32:05 +00001076 return false;
1077
1078 return true;
1079}
1080
Sean Callanan97c924e2011-01-27 01:07:04 +00001081// This function does not report errors; its callers are responsible.
Sean Callananf5857a02010-07-31 01:32:05 +00001082bool
Sean Callananc0492742011-05-23 21:40:23 +00001083IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callananf5857a02010-07-31 01:32:05 +00001084{
Greg Claytone005f2c2010-11-06 01:53:30 +00001085 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +00001086
1087 LoadInst *load = dyn_cast<LoadInst>(selector_load);
1088
1089 if (!load)
1090 return false;
1091
1092 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1093 //
1094 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
1095 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1096 //
1097 // where %obj is the object pointer and %tmp is the selector.
1098 //
Greg Clayton3c7feb42010-11-19 01:05:25 +00001099 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
1100 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sean Callananf5857a02010-07-31 01:32:05 +00001101
1102 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
1103
1104 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
1105
1106 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1107 return false;
1108
1109 Constant *osr_initializer = _objc_selector_references_->getInitializer();
1110
1111 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
1112
1113 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1114 return false;
1115
1116 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1117
1118 if (!osr_initializer_base)
1119 return false;
1120
1121 // Find the string's initializer (a ConstantArray) and get the string from it
1122
1123 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
1124
1125 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1126 return false;
1127
1128 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1129
1130 ConstantArray *omvn_initializer_array = dyn_cast<ConstantArray>(omvn_initializer);
1131
1132 if (!omvn_initializer_array->isString())
1133 return false;
1134
1135 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
1136
1137 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001138 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sean Callananf5857a02010-07-31 01:32:05 +00001139
1140 // Construct a call to sel_registerName
1141
1142 if (!m_sel_registerName)
1143 {
Greg Claytonb5037af2010-11-15 01:47:11 +00001144 lldb::addr_t sel_registerName_addr;
Sean Callananf5857a02010-07-31 01:32:05 +00001145
Greg Clayton8de27c72010-10-15 22:48:33 +00001146 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytonb5037af2010-11-15 01:47:11 +00001147 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callananf5857a02010-07-31 01:32:05 +00001148 return false;
1149
Sean Callananc2c6f772010-10-26 00:31:56 +00001150 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001151 log->Printf("Found sel_registerName at 0x%llx", sel_registerName_addr);
Sean Callananc2c6f772010-10-26 00:31:56 +00001152
Sean Callananf5857a02010-07-31 01:32:05 +00001153 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
1154
1155 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callananc0492742011-05-23 21:40:23 +00001156 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callananf5857a02010-07-31 01:32:05 +00001157 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanan9b6898f2011-07-30 02:42:06 +00001158 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sean Callananf5857a02010-07-31 01:32:05 +00001159
Sean Callanan9b6898f2011-07-30 02:42:06 +00001160 Type *type_array[1];
1161
1162 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
1163
1164 ArrayRef<Type *> srN_arg_types(type_array, 1);
1165
Sean Callananf5857a02010-07-31 01:32:05 +00001166 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
1167
1168 // Build the constant containing the pointer to the function
Sean Callanan9b6898f2011-07-30 02:42:06 +00001169 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1170 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananf5857a02010-07-31 01:32:05 +00001171 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Greg Claytonb5037af2010-11-15 01:47:11 +00001172 Constant *srN_addr_int = ConstantInt::get(intptr_ty, sel_registerName_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +00001173 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1174 }
1175
Sean Callanan9b6898f2011-07-30 02:42:06 +00001176 Value *argument_array[1];
1177
Sean Callananc0492742011-05-23 21:40:23 +00001178 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sean Callananf5857a02010-07-31 01:32:05 +00001179
Sean Callanan9b6898f2011-07-30 02:42:06 +00001180 argument_array[0] = omvn_pointer;
Sean Callananf5857a02010-07-31 01:32:05 +00001181
Sean Callanan9b6898f2011-07-30 02:42:06 +00001182 ArrayRef<Value *> srN_arguments(argument_array, 1);
1183
Sean Callananf5857a02010-07-31 01:32:05 +00001184 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanan9b6898f2011-07-30 02:42:06 +00001185 srN_arguments,
Sean Callanan6ba533e2010-11-17 23:00:36 +00001186 "sel_registerName",
Sean Callananf5857a02010-07-31 01:32:05 +00001187 selector_load);
1188
1189 // Replace the load with the call in all users
1190
1191 selector_load->replaceAllUsesWith(srN_call);
1192
1193 selector_load->eraseFromParent();
1194
1195 return true;
1196}
1197
1198bool
Sean Callananc0492742011-05-23 21:40:23 +00001199IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callananf5857a02010-07-31 01:32:05 +00001200{
Greg Claytone005f2c2010-11-06 01:53:30 +00001201 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +00001202
1203 BasicBlock::iterator ii;
1204
1205 typedef SmallVector <Instruction*, 2> InstrList;
1206 typedef InstrList::iterator InstrIterator;
1207
1208 InstrList selector_loads;
1209
Greg Clayton3c7feb42010-11-19 01:05:25 +00001210 for (ii = basic_block.begin();
1211 ii != basic_block.end();
Sean Callananf5857a02010-07-31 01:32:05 +00001212 ++ii)
1213 {
1214 Instruction &inst = *ii;
1215
1216 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton3c7feb42010-11-19 01:05:25 +00001217 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callananf5857a02010-07-31 01:32:05 +00001218 selector_loads.push_back(&inst);
1219 }
1220
1221 InstrIterator iter;
1222
1223 for (iter = selector_loads.begin();
1224 iter != selector_loads.end();
1225 ++iter)
1226 {
Sean Callananc0492742011-05-23 21:40:23 +00001227 if (!RewriteObjCSelector(*iter))
Sean Callananf5857a02010-07-31 01:32:05 +00001228 {
Sean Callanan97c924e2011-01-27 01:07:04 +00001229 if (m_error_stream)
1230 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't change a static reference to an Objective-C selector to a dynamic reference\n");
1231
Enrico Granata4c3fb4b2011-07-19 18:03:25 +00001232 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +00001233 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sean Callanan97c924e2011-01-27 01:07:04 +00001234
Sean Callananf5857a02010-07-31 01:32:05 +00001235 return false;
1236 }
1237 }
1238
1239 return true;
1240}
1241
Sean Callanan97c924e2011-01-27 01:07:04 +00001242// This function does not report errors; its callers are responsible.
Sean Callanana48fe162010-08-11 03:57:18 +00001243bool
Sean Callananc0492742011-05-23 21:40:23 +00001244IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanana48fe162010-08-11 03:57:18 +00001245{
Sean Callanan97678d12011-01-13 21:23:32 +00001246 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1247
Sean Callanana48fe162010-08-11 03:57:18 +00001248 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1249
1250 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1251
1252 if (!alloc_md || !alloc_md->getNumOperands())
1253 return false;
1254
1255 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
1256
1257 if (!constant_int)
1258 return false;
1259
1260 // We attempt to register this as a new persistent variable with the DeclMap.
1261
1262 uintptr_t ptr = constant_int->getZExtValue();
1263
Sean Callanan82b74c82010-08-12 01:56:52 +00001264 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanana48fe162010-08-11 03:57:18 +00001265
Sean Callanan82b74c82010-08-12 01:56:52 +00001266 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1267 &decl->getASTContext());
1268
Greg Clayton8de27c72010-10-15 22:48:33 +00001269 StringRef decl_name (decl->getName());
1270 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan6a925532011-01-13 08:53:35 +00001271 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanana48fe162010-08-11 03:57:18 +00001272 return false;
1273
Sean Callananc0492742011-05-23 21:40:23 +00001274 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanan97678d12011-01-13 21:23:32 +00001275 alloc->getType(),
Sean Callanana48fe162010-08-11 03:57:18 +00001276 false, /* not constant */
1277 GlobalValue::ExternalLinkage,
1278 NULL, /* no initializer */
1279 alloc->getName().str().c_str());
1280
1281 // What we're going to do here is make believe this was a regular old external
1282 // variable. That means we need to make the metadata valid.
1283
Sean Callananc0492742011-05-23 21:40:23 +00001284 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanana48fe162010-08-11 03:57:18 +00001285
1286 llvm::Value* values[2];
1287 values[0] = persistent_global;
1288 values[1] = constant_int;
Sean Callanan0de254a2011-05-15 22:34:38 +00001289
1290 ArrayRef<llvm::Value*> value_ref(values, 2);
Sean Callanana48fe162010-08-11 03:57:18 +00001291
Sean Callananc0492742011-05-23 21:40:23 +00001292 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanana48fe162010-08-11 03:57:18 +00001293 named_metadata->addOperand(persistent_global_md);
1294
Sean Callanan97678d12011-01-13 21:23:32 +00001295 // Now, since the variable is a pointer variable, we will drop in a load of that
1296 // pointer variable.
1297
1298 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
1299
1300 if (log)
1301 log->Printf("Replacing \"%s\" with \"%s\"",
1302 PrintValue(alloc).c_str(),
1303 PrintValue(persistent_load).c_str());
1304
1305 alloc->replaceAllUsesWith(persistent_load);
Sean Callanana48fe162010-08-11 03:57:18 +00001306 alloc->eraseFromParent();
1307
1308 return true;
1309}
1310
1311bool
Sean Callananc0492742011-05-23 21:40:23 +00001312IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanana48fe162010-08-11 03:57:18 +00001313{
Sean Callanane8a59a82010-09-13 21:34:21 +00001314 if (!m_resolve_vars)
1315 return true;
1316
Greg Claytone005f2c2010-11-06 01:53:30 +00001317 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanana48fe162010-08-11 03:57:18 +00001318
1319 BasicBlock::iterator ii;
1320
1321 typedef SmallVector <Instruction*, 2> InstrList;
1322 typedef InstrList::iterator InstrIterator;
1323
1324 InstrList pvar_allocs;
1325
Greg Clayton3c7feb42010-11-19 01:05:25 +00001326 for (ii = basic_block.begin();
1327 ii != basic_block.end();
Sean Callanana48fe162010-08-11 03:57:18 +00001328 ++ii)
1329 {
1330 Instruction &inst = *ii;
1331
1332 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callanan237e4742011-01-21 22:30:25 +00001333 {
1334 llvm::StringRef alloc_name = alloc->getName();
1335
1336 if (alloc_name.startswith("$") &&
1337 !alloc_name.startswith("$__lldb"))
1338 {
1339 if (alloc_name.find_first_of("0123456789") == 1)
1340 {
1341 if (log)
1342 log->Printf("Rejecting a numeric persistent variable.");
1343
Sean Callanan97c924e2011-01-27 01:07:04 +00001344 if (m_error_stream)
1345 m_error_stream->Printf("Error [IRForTarget]: Names starting with $0, $1, ... are reserved for use as result names\n");
1346
Sean Callanan237e4742011-01-21 22:30:25 +00001347 return false;
1348 }
1349
Sean Callanana48fe162010-08-11 03:57:18 +00001350 pvar_allocs.push_back(alloc);
Sean Callanan237e4742011-01-21 22:30:25 +00001351 }
1352 }
Sean Callanana48fe162010-08-11 03:57:18 +00001353 }
1354
1355 InstrIterator iter;
1356
1357 for (iter = pvar_allocs.begin();
1358 iter != pvar_allocs.end();
1359 ++iter)
1360 {
Sean Callananc0492742011-05-23 21:40:23 +00001361 if (!RewritePersistentAlloc(*iter))
Sean Callanana48fe162010-08-11 03:57:18 +00001362 {
Sean Callanan97c924e2011-01-27 01:07:04 +00001363 if (m_error_stream)
1364 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
1365
Enrico Granata4c3fb4b2011-07-19 18:03:25 +00001366 if (log)
Sean Callanana48fe162010-08-11 03:57:18 +00001367 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sean Callanan97c924e2011-01-27 01:07:04 +00001368
Sean Callanana48fe162010-08-11 03:57:18 +00001369 return false;
1370 }
1371 }
1372
1373 return true;
1374}
1375
Sean Callanan97c924e2011-01-27 01:07:04 +00001376// This function does not report errors; its callers are responsible.
Sean Callanan8bce6652010-07-13 21:41:46 +00001377bool
Sean Callananc0492742011-05-23 21:40:23 +00001378IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001379{
Greg Claytone005f2c2010-11-06 01:53:30 +00001380 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan48443652010-12-02 19:47:57 +00001381
1382 if (log)
Sean Callananb9f09a62010-12-06 22:16:55 +00001383 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sean Callanan9a877432011-08-01 17:41:38 +00001384
Greg Clayton8de27c72010-10-15 22:48:33 +00001385 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callananbc2928a2010-08-03 00:23:29 +00001386 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001387 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +00001388 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001389 default:
1390 break;
1391 case Instruction::GetElementPtr:
1392 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +00001393 Value *s = constant_expr->getOperand(0);
Sean Callananc0492742011-05-23 21:40:23 +00001394 if (!MaybeHandleVariable(s))
Sean Callanan48443652010-12-02 19:47:57 +00001395 return false;
Sean Callananbc2928a2010-08-03 00:23:29 +00001396 }
1397 }
Sean Callananf921cf52010-12-03 19:51:05 +00001398 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +00001399 {
Sean Callananc0492742011-05-23 21:40:23 +00001400 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001401
Sean Callananf5857a02010-07-31 01:32:05 +00001402 if (!named_decl)
1403 {
Greg Clayton3c7feb42010-11-19 01:05:25 +00001404 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +00001405 return true;
1406
Sean Callanan7cd46742010-12-06 00:56:39 +00001407 if (!global_variable->hasExternalLinkage())
1408 return true;
1409
Sean Callananf5857a02010-07-31 01:32:05 +00001410 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001411 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00001412
Sean Callananf5857a02010-07-31 01:32:05 +00001413 return false;
1414 }
1415
Greg Clayton8de27c72010-10-15 22:48:33 +00001416 std::string name (named_decl->getName().str());
Sean Callanan810f22d2010-07-16 00:09:46 +00001417
Sean Callanan771131d2010-09-30 21:18:25 +00001418 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +00001419 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +00001420
1421 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +00001422 {
Sean Callanan771131d2010-09-30 21:18:25 +00001423 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +00001424 ast_context = &value_decl->getASTContext();
1425 }
Sean Callanan810f22d2010-07-16 00:09:46 +00001426 else
Sean Callananf328c9f2010-07-20 23:31:16 +00001427 {
Sean Callanan810f22d2010-07-16 00:09:46 +00001428 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +00001429 }
Sean Callanan771131d2010-09-30 21:18:25 +00001430
Sean Callanan6a925532011-01-13 08:53:35 +00001431 clang::QualType qual_type;
Sean Callanan58baaad2011-07-08 00:39:14 +00001432 const Type *value_type = NULL;
Sean Callanan6a925532011-01-13 08:53:35 +00001433
Sean Callanan97678d12011-01-13 21:23:32 +00001434 if (name[0] == '$')
Sean Callanan6a925532011-01-13 08:53:35 +00001435 {
1436 // The $__lldb_expr_result name indicates the the return value has allocated as
1437 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1438 // accesses to this static variable need to be redirected to the result of dereferencing
1439 // a pointer that is passed in as one of the arguments.
1440 //
1441 // Consequently, when reporting the size of the type, we report a pointer type pointing
1442 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanan97678d12011-01-13 21:23:32 +00001443 //
1444 // We also do this for any user-declared persistent variables.
Sean Callananf328c9f2010-07-20 23:31:16 +00001445
Sean Callanan6a925532011-01-13 08:53:35 +00001446 qual_type = ast_context->getPointerType(clang::QualType::getFromOpaquePtr(opaque_type));
1447 value_type = PointerType::get(global_variable->getType(), 0);
1448 }
1449 else
1450 {
1451 qual_type = clang::QualType::getFromOpaquePtr(opaque_type);
1452 value_type = global_variable->getType();
1453 }
Sean Callanan771131d2010-09-30 21:18:25 +00001454
1455 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
1456 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001457
Sean Callanan771131d2010-09-30 21:18:25 +00001458 if (log)
Sean Callanan97678d12011-01-13 21:23:32 +00001459 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %d, align %d]",
Sean Callanan771131d2010-09-30 21:18:25 +00001460 name.c_str(),
1461 qual_type.getAsString().c_str(),
1462 PrintType(value_type).c_str(),
1463 value_size,
1464 value_alignment);
Sean Callanan3aa7da52010-12-13 22:46:15 +00001465
Sean Callanan8bce6652010-07-13 21:41:46 +00001466
Sean Callanan8c127202010-08-23 23:09:38 +00001467 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton8de27c72010-10-15 22:48:33 +00001468 lldb_private::ConstString (name.c_str()),
1469 llvm_value_ptr,
Sean Callananba992c52010-07-27 02:07:53 +00001470 value_size,
1471 value_alignment))
Sean Callanan9a877432011-08-01 17:41:38 +00001472 {
1473 if (!global_variable->hasExternalLinkage())
1474 return true;
1475 else
1476 return false;
1477 }
Sean Callanan8bce6652010-07-13 21:41:46 +00001478 }
Sean Callananf3143b72010-12-03 03:02:31 +00001479 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanan48443652010-12-02 19:47:57 +00001480 {
1481 if (log)
1482 log->Printf("Function pointers aren't handled right now");
1483
1484 return false;
1485 }
Sean Callanan8bce6652010-07-13 21:41:46 +00001486
1487 return true;
1488}
1489
Sean Callanan97c924e2011-01-27 01:07:04 +00001490// This function does not report errors; its callers are responsible.
Sean Callanan8bce6652010-07-13 21:41:46 +00001491bool
Sean Callananc0492742011-05-23 21:40:23 +00001492IRForTarget::HandleSymbol (Value *symbol)
Sean Callanan81974962011-05-08 02:21:26 +00001493{
Sean Callananc7674af2011-01-17 23:42:46 +00001494 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1495
1496 lldb_private::ConstString name(symbol->getName().str().c_str());
1497
Greg Claytoncbc07cf2011-06-23 04:25:29 +00001498 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name);
Sean Callananc7674af2011-01-17 23:42:46 +00001499
Greg Claytoncbc07cf2011-06-23 04:25:29 +00001500 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc7674af2011-01-17 23:42:46 +00001501 {
1502 if (log)
1503 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
1504
1505 return false;
1506 }
1507
1508 if (log)
1509 log->Printf("Found \"%s\" at 0x%llx", name.GetCString(), symbol_addr);
1510
Sean Callanan9b6898f2011-07-30 02:42:06 +00001511 Type *symbol_type = symbol->getType();
Sean Callananc7674af2011-01-17 23:42:46 +00001512
Sean Callanan9b6898f2011-07-30 02:42:06 +00001513 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1514 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc7674af2011-01-17 23:42:46 +00001515
1516 Constant *symbol_addr_int = ConstantInt::get(intptr_ty, symbol_addr, false);
1517
1518 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1519
1520 if (log)
1521 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
1522
1523 symbol->replaceAllUsesWith(symbol_addr_ptr);
1524
1525 return true;
1526}
1527
1528bool
Sean Callananc0492742011-05-23 21:40:23 +00001529IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanandc27aba2010-10-05 22:26:43 +00001530{
Sean Callanan48443652010-12-02 19:47:57 +00001531 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1532
1533 if (log)
1534 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sean Callanandc27aba2010-10-05 22:26:43 +00001535
Sean Callanan6ba533e2010-11-17 23:00:36 +00001536 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanandc27aba2010-10-05 22:26:43 +00001537 op_index < num_ops;
1538 ++op_index)
Sean Callananc0492742011-05-23 21:40:23 +00001539 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan97c924e2011-01-27 01:07:04 +00001540 {
1541 if (m_error_stream)
1542 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite one of the arguments of a function call.\n");
1543
Sean Callanandc27aba2010-10-05 22:26:43 +00001544 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001545 }
1546
Sean Callanandc27aba2010-10-05 22:26:43 +00001547 return true;
1548}
1549
1550bool
Sean Callananc0492742011-05-23 21:40:23 +00001551IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sean Callanan8bce6652010-07-13 21:41:46 +00001552{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001553 /////////////////////////////////////////////////////////////////////////
1554 // Prepare the current basic block for execution in the remote process
1555 //
1556
Sean Callanan02fbafa2010-07-27 21:39:39 +00001557 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +00001558
Greg Clayton3c7feb42010-11-19 01:05:25 +00001559 for (ii = basic_block.begin();
1560 ii != basic_block.end();
Sean Callanan8bce6652010-07-13 21:41:46 +00001561 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001562 {
Sean Callanan8bce6652010-07-13 21:41:46 +00001563 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001564
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001565 CallInst *call = dyn_cast<CallInst>(&inst);
Sean Callananba992c52010-07-27 02:07:53 +00001566
Sean Callanan97c924e2011-01-27 01:07:04 +00001567 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callananc0492742011-05-23 21:40:23 +00001568 if (call && !MaybeHandleCallArguments(call))
Sean Callanan48443652010-12-02 19:47:57 +00001569 return false;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001570 }
1571
1572 return true;
1573}
1574
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001575bool
Sean Callananc0492742011-05-23 21:40:23 +00001576IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001577{
Sean Callananae71e302010-11-18 22:21:58 +00001578 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1579
Sean Callananc0492742011-05-23 21:40:23 +00001580 for (Module::global_iterator global = m_module->global_begin(), end = m_module->global_end();
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001581 global != end;
1582 ++global)
1583 {
Greg Clayton3c7feb42010-11-19 01:05:25 +00001584 if (log)
1585 log->Printf("Examining %s, DeclForGlobalValue returns %p",
1586 (*global).getName().str().c_str(),
Sean Callananc0492742011-05-23 21:40:23 +00001587 DeclForGlobal(global));
Greg Clayton3c7feb42010-11-19 01:05:25 +00001588
Sean Callananc7674af2011-01-17 23:42:46 +00001589 if ((*global).getName().str().find("OBJC_IVAR") == 0)
1590 {
Sean Callananc0492742011-05-23 21:40:23 +00001591 if (!HandleSymbol(global))
Sean Callanan97c924e2011-01-27 01:07:04 +00001592 {
1593 if (m_error_stream)
1594 m_error_stream->Printf("Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol %s\n", (*global).getName().str().c_str());
1595
Sean Callananc7674af2011-01-17 23:42:46 +00001596 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001597 }
Sean Callananc7674af2011-01-17 23:42:46 +00001598 }
Sean Callananc0492742011-05-23 21:40:23 +00001599 else if (DeclForGlobal(global))
Sean Callananc7674af2011-01-17 23:42:46 +00001600 {
Sean Callananc0492742011-05-23 21:40:23 +00001601 if (!MaybeHandleVariable (global))
Sean Callanan97c924e2011-01-27 01:07:04 +00001602 {
1603 if (m_error_stream)
1604 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite external variable %s\n", (*global).getName().str().c_str());
1605
Sean Callananc7674af2011-01-17 23:42:46 +00001606 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001607 }
Sean Callananc7674af2011-01-17 23:42:46 +00001608 }
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001609 }
1610
1611 return true;
1612}
1613
Sean Callananc0492742011-05-23 21:40:23 +00001614bool
1615IRForTarget::ReplaceStrings ()
1616{
1617 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1618
1619 if (!m_data_allocator)
1620 return true; // hope for the best; some clients may not want static allocation!
1621
1622 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
1623
1624 OffsetsTy offsets;
1625
1626 for (Module::global_iterator gi = m_module->global_begin(), ge = m_module->global_end();
1627 gi != ge;
1628 ++gi)
1629 {
1630 GlobalVariable *gv = gi;
1631
1632 if (!gv->hasInitializer())
1633 continue;
1634
1635 Constant *gc = gv->getInitializer();
1636
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001637 std::string str;
Sean Callananc0492742011-05-23 21:40:23 +00001638
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001639 if (gc->isNullValue())
1640 {
1641 Type *gc_type = gc->getType();
1642
1643 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
1644
1645 if (!gc_array_type)
1646 continue;
1647
1648 Type *gc_element_type = gc_array_type->getElementType();
1649
1650 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
1651
1652 if (gc_integer_type->getBitWidth() != 8)
1653 continue;
1654
1655 str = "";
1656 }
1657 else
1658 {
1659 ConstantArray *gc_array = dyn_cast<ConstantArray>(gc);
1660
1661 if (!gc_array)
1662 continue;
Sean Callananc0492742011-05-23 21:40:23 +00001663
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001664 if (!gc_array->isCString())
1665 continue;
Sean Callananc0492742011-05-23 21:40:23 +00001666
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001667 if (log)
1668 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
Sean Callananc0492742011-05-23 21:40:23 +00001669
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001670 str = gc_array->getAsString();
1671 }
1672
Sean Callananc0492742011-05-23 21:40:23 +00001673 offsets[gv] = m_data_allocator->GetStream().GetSize();
1674
1675 m_data_allocator->GetStream().Write(str.c_str(), str.length() + 1);
1676 }
1677
Sean Callanan9b6898f2011-07-30 02:42:06 +00001678 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callananc0492742011-05-23 21:40:23 +00001679
1680 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1681 oi != oe;
1682 ++oi)
1683 {
1684 GlobalVariable *gv = oi->first;
1685 size_t offset = oi->second;
1686
1687 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
1688
1689 if (log)
1690 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
1691
1692 for (GlobalVariable::use_iterator ui = gv->use_begin(), ue = gv->use_end();
1693 ui != ue;
1694 ++ui)
1695 {
1696 if (log)
1697 log->Printf("Found use %s", PrintValue(*ui).c_str());
1698
1699 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(*ui);
1700 StoreInst *store_inst = dyn_cast<StoreInst>(*ui);
1701
1702 if (const_expr)
1703 {
1704 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1705 {
1706 if (log)
1707 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
1708
1709 return false;
1710 }
1711
Sean Callanan1b3d1df2011-08-10 21:05:52 +00001712 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
1713 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
1714
1715 const_expr->replaceAllUsesWith(new_gep);
Sean Callananc0492742011-05-23 21:40:23 +00001716 }
1717 else if (store_inst)
1718 {
1719 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
1720
1721 store_inst->setOperand(0, bit_cast);
1722 }
1723 else
1724 {
1725 if (log)
1726 log->Printf("Use (%s) of string variable is neither a constant nor a store", PrintValue(const_expr).c_str());
1727
1728 return false;
1729 }
1730 }
1731
1732 gv->eraseFromParent();
1733 }
1734
1735 return true;
1736}
1737
1738bool
1739IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
1740{
1741 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1742
1743 if (!m_data_allocator)
1744 return true;
1745
1746 typedef SmallVector <Value*, 2> ConstantList;
1747 typedef SmallVector <llvm::Instruction*, 2> UserList;
1748 typedef ConstantList::iterator ConstantIterator;
1749 typedef UserList::iterator UserIterator;
1750
1751 ConstantList static_constants;
1752 UserList static_users;
1753
1754 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
1755 ii != ie;
1756 ++ii)
1757 {
1758 llvm::Instruction &inst = *ii;
1759
1760 for (Instruction::op_iterator oi = inst.op_begin(), oe = inst.op_end();
1761 oi != oe;
1762 ++oi)
1763 {
1764 Value *operand_val = oi->get();
1765
1766 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
1767
1768 if (operand_constant_fp && operand_constant_fp->getType()->isX86_FP80Ty())
1769 {
1770 static_constants.push_back(operand_val);
1771 static_users.push_back(ii);
1772 }
1773 }
1774 }
1775
1776 ConstantIterator constant_iter;
1777 UserIterator user_iter;
Greg Clayton54b38412011-05-24 23:06:02 +00001778
Sean Callananc0492742011-05-23 21:40:23 +00001779 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
1780 constant_iter != static_constants.end();
1781 ++constant_iter, ++user_iter)
1782 {
1783 Value *operand_val = *constant_iter;
1784 llvm::Instruction *inst = *user_iter;
1785
1786 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
1787
1788 if (operand_constant_fp)
1789 {
1790 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
1791 APInt operand_apint = operand_apfloat.bitcastToAPInt();
1792
1793 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
1794 size_t operand_data_size = operand_apint.getBitWidth() / 8;
1795
1796 if (log)
1797 {
1798 std::string s;
1799 raw_string_ostream ss(s);
1800 for (size_t index = 0;
1801 index < operand_data_size;
1802 ++index)
1803 {
1804 ss << (uint32_t)operand_raw_data[index];
1805 ss << " ";
1806 }
1807 ss.flush();
1808
1809 log->Printf("Found ConstantFP with size %d and raw data %s", operand_data_size, s.c_str());
1810 }
1811
1812 lldb_private::DataBufferHeap data(operand_data_size, 0);
1813
1814 if (lldb::endian::InlHostByteOrder() != m_data_allocator->GetStream().GetByteOrder())
1815 {
1816 uint8_t *data_bytes = data.GetBytes();
1817
1818 for (size_t index = 0;
1819 index < operand_data_size;
1820 ++index)
1821 {
1822 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
1823 }
1824 }
1825 else
1826 {
1827 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
1828 }
1829
1830 uint64_t offset = m_data_allocator->GetStream().GetSize();
1831
1832 m_data_allocator->GetStream().Write(data.GetBytes(), operand_data_size);
1833
Sean Callanan9b6898f2011-07-30 02:42:06 +00001834 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
Sean Callananc0492742011-05-23 21:40:23 +00001835
1836 Constant *new_pointer = BuildRelocation(fp_ptr_ty, offset);
1837
1838 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
1839
1840 operand_constant_fp->replaceAllUsesWith(fp_load);
1841 }
1842 }
1843
1844 return true;
1845}
1846
Sean Callanan02fbafa2010-07-27 21:39:39 +00001847static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +00001848{
Sean Callanan58baaad2011-07-08 00:39:14 +00001849 Constant *Old = NULL;
Sean Callanan45839272010-07-24 01:37:44 +00001850
Sean Callanan6ba533e2010-11-17 23:00:36 +00001851 if (!(Old = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +00001852 return false;
1853
Sean Callanan58baaad2011-07-08 00:39:14 +00001854 ConstantExpr *CE = NULL;
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001855
1856 if ((CE = dyn_cast<ConstantExpr>(V)))
1857 {
1858 if (CE->getOpcode() != Instruction::BitCast)
1859 return false;
1860
Sean Callanan6ba533e2010-11-17 23:00:36 +00001861 Old = CE->getOperand(0);
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001862 }
1863
Sean Callanan6ba533e2010-11-17 23:00:36 +00001864 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sean Callanan45839272010-07-24 01:37:44 +00001865
1866 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
1867 return false;
1868
1869 return true;
1870}
1871
Sean Callananc0492742011-05-23 21:40:23 +00001872void
1873IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callanan45839272010-07-24 01:37:44 +00001874{
Sean Callananc0492742011-05-23 21:40:23 +00001875 Constant* zero(ConstantInt::get(Type::getInt8Ty(m_module->getContext()), 0, true));
Sean Callanan45839272010-07-24 01:37:44 +00001876
1877 Value::use_iterator ui;
1878
1879 for (ui = guard_load->use_begin();
1880 ui != guard_load->use_end();
1881 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +00001882 {
Greg Clayton6e713402010-07-30 20:30:44 +00001883 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +00001884 {
1885 // do nothing for the moment
1886 }
1887 else
1888 {
1889 ui->replaceUsesOfWith(guard_load, zero);
1890 }
1891 }
Sean Callanan45839272010-07-24 01:37:44 +00001892
1893 guard_load->eraseFromParent();
1894}
1895
1896static void ExciseGuardStore(Instruction* guard_store)
1897{
1898 guard_store->eraseFromParent();
1899}
1900
1901bool
Sean Callananc0492742011-05-23 21:40:23 +00001902IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sean Callanan45839272010-07-24 01:37:44 +00001903{
1904 ///////////////////////////////////////////////////////
1905 // Eliminate any reference to guard variables found.
1906 //
1907
Sean Callanan02fbafa2010-07-27 21:39:39 +00001908 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +00001909
Sean Callanan02fbafa2010-07-27 21:39:39 +00001910 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +00001911 typedef InstrList::iterator InstrIterator;
1912
1913 InstrList guard_loads;
1914 InstrList guard_stores;
1915
Greg Clayton3c7feb42010-11-19 01:05:25 +00001916 for (ii = basic_block.begin();
1917 ii != basic_block.end();
Sean Callanan45839272010-07-24 01:37:44 +00001918 ++ii)
1919 {
1920 Instruction &inst = *ii;
1921
1922 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1923 if (isGuardVariableRef(load->getPointerOperand()))
1924 guard_loads.push_back(&inst);
1925
1926 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
1927 if (isGuardVariableRef(store->getPointerOperand()))
1928 guard_stores.push_back(&inst);
1929 }
1930
1931 InstrIterator iter;
1932
1933 for (iter = guard_loads.begin();
1934 iter != guard_loads.end();
1935 ++iter)
Sean Callananc0492742011-05-23 21:40:23 +00001936 TurnGuardLoadIntoZero(*iter);
Sean Callanan45839272010-07-24 01:37:44 +00001937
1938 for (iter = guard_stores.begin();
1939 iter != guard_stores.end();
1940 ++iter)
1941 ExciseGuardStore(*iter);
1942
1943 return true;
1944}
1945
Sean Callanan97c924e2011-01-27 01:07:04 +00001946// This function does not report errors; its callers are responsible.
Sean Callanan6ba533e2010-11-17 23:00:36 +00001947bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001948IRForTarget::UnfoldConstant(Constant *old_constant, Value *new_constant, Instruction *first_entry_inst)
Sean Callananbafd6852010-07-14 23:40:29 +00001949{
Greg Claytone005f2c2010-11-06 01:53:30 +00001950 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbafd6852010-07-14 23:40:29 +00001951
1952 Value::use_iterator ui;
1953
Sean Callanana48fe162010-08-11 03:57:18 +00001954 SmallVector<User*, 16> users;
1955
1956 // We do this because the use list might change, invalidating our iterator.
1957 // Much better to keep a work list ourselves.
Greg Clayton3c7feb42010-11-19 01:05:25 +00001958 for (ui = old_constant->use_begin();
1959 ui != old_constant->use_end();
Sean Callananbafd6852010-07-14 23:40:29 +00001960 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +00001961 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +00001962
Johnny Chen2bc9eb32011-07-19 19:48:13 +00001963 for (size_t i = 0;
Sean Callanana48fe162010-08-11 03:57:18 +00001964 i < users.size();
1965 ++i)
1966 {
1967 User *user = users[i];
1968
Sean Callananbafd6852010-07-14 23:40:29 +00001969 if (Constant *constant = dyn_cast<Constant>(user))
1970 {
1971 // synthesize a new non-constant equivalent of the constant
1972
1973 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
1974 {
1975 switch (constant_expr->getOpcode())
1976 {
1977 default:
1978 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001979 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00001980 return false;
1981 case Instruction::BitCast:
1982 {
1983 // UnaryExpr
1984 // OperandList[0] is value
1985
1986 Value *s = constant_expr->getOperand(0);
1987
Greg Clayton3c7feb42010-11-19 01:05:25 +00001988 if (s == old_constant)
1989 s = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00001990
Sean Callananc0492742011-05-23 21:40:23 +00001991 BitCastInst *bit_cast(new BitCastInst(s, constant_expr->getType(), "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00001992
Greg Clayton3c7feb42010-11-19 01:05:25 +00001993 UnfoldConstant(constant_expr, bit_cast, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00001994 }
1995 break;
1996 case Instruction::GetElementPtr:
1997 {
1998 // GetElementPtrConstantExpr
1999 // OperandList[0] is base
2000 // OperandList[1]... are indices
2001
2002 Value *ptr = constant_expr->getOperand(0);
2003
Greg Clayton3c7feb42010-11-19 01:05:25 +00002004 if (ptr == old_constant)
2005 ptr = new_constant;
Sean Callanan9b6898f2011-07-30 02:42:06 +00002006
2007 std::vector<Value*> index_vector;
Sean Callananbafd6852010-07-14 23:40:29 +00002008
2009 unsigned operand_index;
2010 unsigned num_operands = constant_expr->getNumOperands();
2011
2012 for (operand_index = 1;
2013 operand_index < num_operands;
2014 ++operand_index)
2015 {
2016 Value *operand = constant_expr->getOperand(operand_index);
2017
Greg Clayton3c7feb42010-11-19 01:05:25 +00002018 if (operand == old_constant)
2019 operand = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00002020
Sean Callanan9b6898f2011-07-30 02:42:06 +00002021 index_vector.push_back(operand);
Sean Callananbafd6852010-07-14 23:40:29 +00002022 }
2023
Sean Callanan9b6898f2011-07-30 02:42:06 +00002024 ArrayRef <Value*> indices(index_vector);
2025
2026 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices, "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00002027
Greg Clayton3c7feb42010-11-19 01:05:25 +00002028 UnfoldConstant(constant_expr, get_element_ptr, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00002029 }
2030 break;
2031 }
2032 }
2033 else
2034 {
2035 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002036 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00002037 return false;
2038 }
2039 }
2040 else
2041 {
2042 // simple fall-through case for non-constants
Greg Clayton3c7feb42010-11-19 01:05:25 +00002043 user->replaceUsesOfWith(old_constant, new_constant);
Sean Callananbafd6852010-07-14 23:40:29 +00002044 }
2045 }
2046
2047 return true;
2048}
2049
Sean Callanan8bce6652010-07-13 21:41:46 +00002050bool
Sean Callananc0492742011-05-23 21:40:23 +00002051IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan8bce6652010-07-13 21:41:46 +00002052{
Sean Callanane8a59a82010-09-13 21:34:21 +00002053 if (!m_resolve_vars)
2054 return true;
2055
Greg Claytone005f2c2010-11-06 01:53:30 +00002056 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8bce6652010-07-13 21:41:46 +00002057
2058 m_decl_map->DoStructLayout();
2059
2060 if (log)
2061 log->Printf("Element arrangement:");
2062
2063 uint32_t num_elements;
2064 uint32_t element_index;
2065
2066 size_t size;
2067 off_t alignment;
2068
2069 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2070 return false;
2071
Greg Clayton3c7feb42010-11-19 01:05:25 +00002072 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00002073
Greg Clayton3c7feb42010-11-19 01:05:25 +00002074 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002075 {
2076 if (m_error_stream)
2077 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes no arguments (should take at least a struct pointer)");
2078
Sean Callanan8bce6652010-07-13 21:41:46 +00002079 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002080 }
2081
Sean Callanan02fbafa2010-07-27 21:39:39 +00002082 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00002083
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002084 if (argument->getName().equals("this"))
2085 {
2086 ++iter;
2087
Greg Clayton3c7feb42010-11-19 01:05:25 +00002088 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002089 {
2090 if (m_error_stream)
2091 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'this' argument (should take a struct pointer too)");
2092
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002093 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002094 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002095
2096 argument = iter;
2097 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002098 else if (argument->getName().equals("self"))
2099 {
2100 ++iter;
2101
2102 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002103 {
2104 if (m_error_stream)
2105 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' argument (should take '_cmd' and a struct pointer too)");
2106
Sean Callanan3aa7da52010-12-13 22:46:15 +00002107 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002108 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002109
2110 if (!iter->getName().equals("_cmd"))
Sean Callanan97c924e2011-01-27 01:07:04 +00002111 {
2112 if (m_error_stream)
2113 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes '%s' after 'self' argument (should take '_cmd')", iter->getName().str().c_str());
2114
Sean Callanan3aa7da52010-12-13 22:46:15 +00002115 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002116 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002117
2118 ++iter;
2119
2120 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002121 {
2122 if (m_error_stream)
2123 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' and '_cmd' arguments (should take a struct pointer too)");
2124
Sean Callanan3aa7da52010-12-13 22:46:15 +00002125 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002126 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002127
2128 argument = iter;
2129 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002130
Greg Clayton8de27c72010-10-15 22:48:33 +00002131 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan97c924e2011-01-27 01:07:04 +00002132 {
2133 if (m_error_stream)
2134 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes an argument named '%s' instead of the struct pointer", argument->getName().str().c_str());
2135
Sean Callanan8bce6652010-07-13 21:41:46 +00002136 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002137 }
2138
Sean Callanan8bce6652010-07-13 21:41:46 +00002139 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002140 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sean Callanan8bce6652010-07-13 21:41:46 +00002141
Greg Clayton3c7feb42010-11-19 01:05:25 +00002142 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +00002143 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00002144
Sean Callanan6ba533e2010-11-17 23:00:36 +00002145 if (!FirstEntryInstruction)
Sean Callanan97c924e2011-01-27 01:07:04 +00002146 {
2147 if (m_error_stream)
2148 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the first instruction in the wrapper for use in rewriting");
2149
Sean Callanan8bce6652010-07-13 21:41:46 +00002150 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002151 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002152
Sean Callananc0492742011-05-23 21:40:23 +00002153 LLVMContext &context(m_module->getContext());
Sean Callanan9b6898f2011-07-30 02:42:06 +00002154 IntegerType *offset_type(Type::getInt32Ty(context));
Sean Callanan8bce6652010-07-13 21:41:46 +00002155
2156 if (!offset_type)
Sean Callanan97c924e2011-01-27 01:07:04 +00002157 {
2158 if (m_error_stream)
2159 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
2160
Sean Callanan8bce6652010-07-13 21:41:46 +00002161 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002162 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002163
2164 for (element_index = 0; element_index < num_elements; ++element_index)
2165 {
Sean Callanan58baaad2011-07-08 00:39:14 +00002166 const clang::NamedDecl *decl = NULL;
2167 Value *value = NULL;
Sean Callanan8bce6652010-07-13 21:41:46 +00002168 off_t offset;
Greg Clayton8de27c72010-10-15 22:48:33 +00002169 lldb_private::ConstString name;
Sean Callanan8bce6652010-07-13 21:41:46 +00002170
Sean Callanan45690fe2010-08-30 22:17:16 +00002171 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan97c924e2011-01-27 01:07:04 +00002172 {
2173 if (m_error_stream)
2174 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
2175
Sean Callanan8bce6652010-07-13 21:41:46 +00002176 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002177 }
2178
Sean Callanan8bce6652010-07-13 21:41:46 +00002179 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002180 log->Printf(" \"%s\" [\"%s\"] (\"%s\") placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00002181 value->getName().str().c_str(),
Greg Clayton8de27c72010-10-15 22:48:33 +00002182 name.GetCString(),
Sean Callanan8bce6652010-07-13 21:41:46 +00002183 PrintValue(value, true).c_str(),
2184 offset);
2185
Sean Callanan9b6898f2011-07-30 02:42:06 +00002186 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
Sean Callanan6ba533e2010-11-17 23:00:36 +00002187 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", FirstEntryInstruction);
Sean Callanan6a925532011-01-13 08:53:35 +00002188
Sean Callanan58baaad2011-07-08 00:39:14 +00002189 Value *replacement = NULL;
Sean Callanan8bce6652010-07-13 21:41:46 +00002190
Sean Callanan6a925532011-01-13 08:53:35 +00002191 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2192 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2193 // entry in order to produce the static variable that the AST thinks it is accessing.
2194 if (name == m_result_name && !m_result_is_pointer)
2195 {
2196 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType()->getPointerTo(), "", FirstEntryInstruction);
2197
2198 LoadInst *load = new LoadInst(bit_cast, "", FirstEntryInstruction);
2199
2200 replacement = load;
2201 }
Sean Callananbafd6852010-07-14 23:40:29 +00002202 else
Sean Callanan6a925532011-01-13 08:53:35 +00002203 {
2204 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", FirstEntryInstruction);
2205
2206 replacement = bit_cast;
2207 }
2208
2209 if (Constant *constant = dyn_cast<Constant>(value))
2210 UnfoldConstant(constant, replacement, FirstEntryInstruction);
2211 else
2212 value->replaceAllUsesWith(replacement);
Sean Callananb51ee982010-11-02 23:51:17 +00002213
2214 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2215 var->eraseFromParent();
Sean Callanan8bce6652010-07-13 21:41:46 +00002216 }
2217
2218 if (log)
2219 log->Printf("Total structure [align %d, size %d]", alignment, size);
2220
2221 return true;
2222}
2223
Sean Callananc0492742011-05-23 21:40:23 +00002224llvm::Constant *
Sean Callanan9b6898f2011-07-30 02:42:06 +00002225IRForTarget::BuildRelocation(llvm::Type *type,
Sean Callananc0492742011-05-23 21:40:23 +00002226 uint64_t offset)
2227{
2228 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2229
Sean Callanan9b6898f2011-07-30 02:42:06 +00002230 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
2231 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc0492742011-05-23 21:40:23 +00002232
2233 llvm::Constant *offset_int = ConstantInt::get(intptr_ty, offset);
Sean Callanan9b6898f2011-07-30 02:42:06 +00002234
2235 llvm::Constant *offset_array[1];
2236
2237 offset_array[0] = offset_int;
2238
2239 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
2240
2241 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, offsets);
Sean Callananc0492742011-05-23 21:40:23 +00002242 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
2243
2244 return reloc_getbitcast;
2245}
2246
2247bool
2248IRForTarget::CompleteDataAllocation ()
2249{
2250 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2251
2252 if (!m_data_allocator->GetStream().GetSize())
2253 return true;
2254
2255 lldb::addr_t allocation = m_data_allocator->Allocate();
2256
2257 if (log)
2258 {
2259 if (allocation)
2260 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2261 else
2262 log->Printf("Failed to allocate static data");
2263 }
2264
2265 if (!allocation)
2266 return false;
2267
Sean Callanan9b6898f2011-07-30 02:42:06 +00002268 IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
2269 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc0492742011-05-23 21:40:23 +00002270
2271 Constant *relocated_addr = ConstantInt::get(intptr_ty, (uint64_t)allocation);
2272 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
2273
2274 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
2275
2276 m_reloc_placeholder->eraseFromParent();
2277
2278 return true;
2279}
2280
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002281bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00002282IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002283{
Greg Claytone005f2c2010-11-06 01:53:30 +00002284 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002285
Sean Callananc0492742011-05-23 21:40:23 +00002286 m_module = &llvm_module;
2287
2288 Function* function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002289
2290 if (!function)
2291 {
2292 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002293 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00002294
2295 if (m_error_stream)
Sean Callananc0492742011-05-23 21:40:23 +00002296 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 +00002297
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002298 return false;
2299 }
Sean Callananc0492742011-05-23 21:40:23 +00002300
2301 if (!FixFunctionLinkage (*function))
2302 {
2303 if (log)
2304 log->Printf("Couldn't fix the linkage for the function");
2305
2306 return false;
2307 }
2308
Sean Callanan9b6898f2011-07-30 02:42:06 +00002309 llvm::Type *intptr_ty = Type::getInt8Ty(m_module->getContext());
Sean Callananc0492742011-05-23 21:40:23 +00002310
2311 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
2312 intptr_ty,
2313 false /* isConstant */,
2314 GlobalVariable::InternalLinkage,
2315 Constant::getNullValue(intptr_ty),
2316 "reloc_placeholder",
2317 NULL /* InsertBefore */,
2318 false /* ThreadLocal */,
2319 0 /* AddressSpace */);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002320
Sean Callanan02fbafa2010-07-27 21:39:39 +00002321 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002322
Sean Callananc0492742011-05-23 21:40:23 +00002323 m_has_side_effects = HasSideEffects(*function);
Sean Callanan05a5a1b2010-12-16 03:17:46 +00002324
Sean Callanan82b74c82010-08-12 01:56:52 +00002325 ////////////////////////////////////////////////////////////
Greg Clayton8de27c72010-10-15 22:48:33 +00002326 // Replace $__lldb_expr_result with a persistent variable
Sean Callanan82b74c82010-08-12 01:56:52 +00002327 //
2328
Sean Callananc0492742011-05-23 21:40:23 +00002329 if (!CreateResultVariable(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002330 {
2331 if (log)
2332 log->Printf("CreateResultVariable() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002333
2334 // CreateResultVariable() reports its own errors, so we don't do so here
2335
Sean Callanan82b74c82010-08-12 01:56:52 +00002336 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002337 }
Sean Callanan82b74c82010-08-12 01:56:52 +00002338
Sean Callanan696cf5f2011-05-07 01:06:41 +00002339 if (m_const_result)
2340 return true;
2341
Sean Callananc0492742011-05-23 21:40:23 +00002342 if (log)
2343 {
2344 std::string s;
2345 raw_string_ostream oss(s);
2346
2347 m_module->print(oss, NULL);
2348
2349 oss.flush();
2350
2351 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2352 }
2353
Sean Callanan6ba533e2010-11-17 23:00:36 +00002354 ///////////////////////////////////////////////////////////////////////////////
2355 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2356 //
Sean Callanan6ba533e2010-11-17 23:00:36 +00002357
Sean Callananc0492742011-05-23 21:40:23 +00002358 if (!RewriteObjCConstStrings(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002359 {
2360 if (log)
2361 log->Printf("RewriteObjCConstStrings() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002362
2363 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
2364
Sean Callanan6ba533e2010-11-17 23:00:36 +00002365 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002366 }
Sean Callanan6ba533e2010-11-17 23:00:36 +00002367
Sean Callanan5c9a3c72011-08-04 21:37:47 +00002368 ///////////////////////////////
2369 // Resolve function pointers
2370 //
2371
2372 if (!ResolveFunctionPointers(llvm_module, *function))
2373 {
2374 if (log)
2375 log->Printf("ResolveFunctionPointers() failed");
2376
2377 // ResolveFunctionPointers() reports its own errors, so we don't do so here
2378
2379 return false;
2380 }
2381
Sean Callananf5857a02010-07-31 01:32:05 +00002382 //////////////////////////////////
2383 // Run basic-block level passes
2384 //
2385
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002386 for (bbi = function->begin();
2387 bbi != function->end();
2388 ++bbi)
2389 {
Sean Callananc0492742011-05-23 21:40:23 +00002390 if (!RemoveGuards(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002391 {
2392 if (log)
2393 log->Printf("RemoveGuards() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002394
2395 // RemoveGuards() reports its own errors, so we don't do so here
2396
Sean Callanan8c127202010-08-23 23:09:38 +00002397 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002398 }
Sean Callanan8c127202010-08-23 23:09:38 +00002399
Sean Callananc0492742011-05-23 21:40:23 +00002400 if (!RewritePersistentAllocs(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002401 {
2402 if (log)
2403 log->Printf("RewritePersistentAllocs() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002404
2405 // RewritePersistentAllocs() reports its own errors, so we don't do so here
2406
Sean Callananf5857a02010-07-31 01:32:05 +00002407 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002408 }
Sean Callananf5857a02010-07-31 01:32:05 +00002409
Sean Callananc0492742011-05-23 21:40:23 +00002410 if (!RewriteObjCSelectors(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002411 {
2412 if (log)
2413 log->Printf("RewriteObjCSelectors() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002414
2415 // RewriteObjCSelectors() reports its own errors, so we don't do so here
2416
Sean Callanana48fe162010-08-11 03:57:18 +00002417 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002418 }
Sean Callanana48fe162010-08-11 03:57:18 +00002419
Sean Callananc0492742011-05-23 21:40:23 +00002420 if (!ResolveCalls(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002421 {
2422 if (log)
2423 log->Printf("ResolveCalls() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002424
2425 // ResolveCalls() reports its own errors, so we don't do so here
2426
Sean Callanan8bce6652010-07-13 21:41:46 +00002427 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002428 }
Sean Callananc0492742011-05-23 21:40:23 +00002429
2430 if (!ReplaceStaticLiterals(*bbi))
2431 {
2432 if (log)
2433 log->Printf("ReplaceStaticLiterals() failed");
2434
2435 return false;
2436 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002437 }
2438
Sean Callanan771131d2010-09-30 21:18:25 +00002439 ///////////////////////////////
2440 // Run function-level passes
2441 //
2442
Sean Callananc0492742011-05-23 21:40:23 +00002443 if (!ResolveExternals(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002444 {
2445 if (log)
2446 log->Printf("ResolveExternals() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002447
2448 // ResolveExternals() reports its own errors, so we don't do so here
2449
Sean Callanan1d1b39c2010-11-08 00:31:32 +00002450 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002451 }
Sean Callanan1d1b39c2010-11-08 00:31:32 +00002452
Sean Callananc0492742011-05-23 21:40:23 +00002453 if (!ReplaceVariables(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002454 {
2455 if (log)
2456 log->Printf("ReplaceVariables() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002457
2458 // ReplaceVariables() reports its own errors, so we don't do so here
2459
Sean Callanan771131d2010-09-30 21:18:25 +00002460 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002461 }
Sean Callanan771131d2010-09-30 21:18:25 +00002462
Sean Callananc0492742011-05-23 21:40:23 +00002463 if (!ReplaceStrings())
2464 {
2465 if (log)
2466 log->Printf("ReplaceStrings() failed");
2467
2468 return false;
2469 }
2470
2471 if (!CompleteDataAllocation())
2472 {
2473 if (log)
2474 log->Printf("CompleteDataAllocation() failed");
2475
2476 return false;
2477 }
2478
Sean Callanan8bce6652010-07-13 21:41:46 +00002479 if (log)
2480 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00002481 std::string s;
2482 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00002483
Sean Callananc0492742011-05-23 21:40:23 +00002484 m_module->print(oss, NULL);
Sean Callanan321fe9e2010-07-28 01:00:59 +00002485
2486 oss.flush();
2487
Greg Claytonb5037af2010-11-15 01:47:11 +00002488 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002489 }
2490
2491 return true;
2492}
2493
2494void
Greg Clayton3c7feb42010-11-19 01:05:25 +00002495IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002496{
2497}
2498
2499PassManagerType
2500IRForTarget::getPotentialPassManagerType() const
2501{
2502 return PMT_ModulePassManager;
2503}