blob: 27a0f4aab6810084e4ec5ef5c3489825286b21cf [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"
13#include "llvm/InstrTypes.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000014#include "llvm/Instructions.h"
Sean Callanan3cb1fd82010-09-28 23:55:00 +000015#include "llvm/Intrinsics.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000016#include "llvm/Module.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000017#include "llvm/Target/TargetData.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000018#include "llvm/ValueSymbolTable.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000019
20#include "clang/AST/ASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000021
Greg Clayton8de27c72010-10-15 22:48:33 +000022#include "lldb/Core/ConstString.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000023#include "lldb/Core/dwarf.h"
24#include "lldb/Core/Log.h"
25#include "lldb/Core/Scalar.h"
26#include "lldb/Core/StreamString.h"
27#include "lldb/Expression/ClangExpressionDeclMap.h"
28
29#include <map>
30
31using namespace llvm;
32
Sean Callanan3351dac2010-08-18 18:50:51 +000033static char ID;
34
Greg Clayton3c7feb42010-11-19 01:05:25 +000035IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
36 bool resolve_vars,
Sean Callanan05a5a1b2010-12-16 03:17:46 +000037 lldb::ClangExpressionVariableSP *const_result,
Greg Clayton3c7feb42010-11-19 01:05:25 +000038 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +000039 ModulePass(ID),
Sean Callanan8bce6652010-07-13 21:41:46 +000040 m_decl_map(decl_map),
Sean Callanan6ba533e2010-11-17 23:00:36 +000041 m_CFStringCreateWithBytes(NULL),
Sean Callanan65dafa82010-08-27 01:01:44 +000042 m_sel_registerName(NULL),
Sean Callanane8a59a82010-09-13 21:34:21 +000043 m_func_name(func_name),
Sean Callanan05a5a1b2010-12-16 03:17:46 +000044 m_resolve_vars(resolve_vars),
45 m_const_result(const_result),
Sean Callanan6a925532011-01-13 08:53:35 +000046 m_has_side_effects(false),
47 m_result_is_pointer(false)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000048{
49}
50
Sean Callanan771131d2010-09-30 21:18:25 +000051/* Handy utility functions used at several places in the code */
Sean Callanana48fe162010-08-11 03:57:18 +000052
53static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000054PrintValue(const Value *value, bool truncate = false)
Sean Callanana48fe162010-08-11 03:57:18 +000055{
56 std::string s;
57 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000058 value->print(rso);
Sean Callanana48fe162010-08-11 03:57:18 +000059 rso.flush();
60 if (truncate)
61 s.resize(s.length() - 1);
62 return s;
63}
64
Sean Callanan771131d2010-09-30 21:18:25 +000065static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000066PrintType(const Type *type, bool truncate = false)
Sean Callanan771131d2010-09-30 21:18:25 +000067{
68 std::string s;
69 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000070 type->print(rso);
Sean Callanan771131d2010-09-30 21:18:25 +000071 rso.flush();
72 if (truncate)
73 s.resize(s.length() - 1);
74 return s;
75}
76
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000077IRForTarget::~IRForTarget()
78{
79}
80
Sean Callanan82b74c82010-08-12 01:56:52 +000081bool
Sean Callanan05a5a1b2010-12-16 03:17:46 +000082IRForTarget::HasSideEffects (llvm::Module &llvm_module,
83 llvm::Function &llvm_function)
84{
85 llvm::Function::iterator bbi;
86 BasicBlock::iterator ii;
87
88 for (bbi = llvm_function.begin();
89 bbi != llvm_function.end();
90 ++bbi)
91 {
92 BasicBlock &basic_block = *bbi;
93
94 for (ii = basic_block.begin();
95 ii != basic_block.end();
96 ++ii)
97 {
98 switch (ii->getOpcode())
99 {
100 default:
101 return true;
102 case Instruction::Store:
103 {
104 StoreInst *store_inst = dyn_cast<StoreInst>(ii);
105
106 Value *store_ptr = store_inst->getPointerOperand();
107
108 if (!isa <AllocaInst> (store_ptr))
109 return true;
110 else
111 break;
112 }
113 case Instruction::Load:
114 case Instruction::Alloca:
115 case Instruction::GetElementPtr:
116 case Instruction::Ret:
117 break;
118 }
119 }
120 }
121
122 return false;
123}
124
125void
126IRForTarget::MaybeSetConstantResult (llvm::Constant *initializer,
127 const lldb_private::ConstString &name,
128 lldb_private::TypeFromParser type)
129{
130 if (!m_const_result)
131 return;
132
133 if (llvm::ConstantInt *init_int = dyn_cast<llvm::ConstantInt>(initializer))
134 {
135 *m_const_result = m_decl_map->BuildIntegerVariable(name, type, init_int->getValue());
136 }
137}
138
139bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000140IRForTarget::CreateResultVariable (llvm::Module &llvm_module, llvm::Function &llvm_function)
Sean Callanan82b74c82010-08-12 01:56:52 +0000141{
Greg Claytone005f2c2010-11-06 01:53:30 +0000142 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan82b74c82010-08-12 01:56:52 +0000143
Sean Callanane8a59a82010-09-13 21:34:21 +0000144 if (!m_resolve_vars)
145 return true;
146
147 // Find the result variable. If it doesn't exist, we can give up right here.
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000148
Greg Clayton8de27c72010-10-15 22:48:33 +0000149 ValueSymbolTable& value_symbol_table = llvm_module.getValueSymbolTable();
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000150
151 const char *result_name = NULL;
152
153 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
154 vi != ve;
155 ++vi)
156 {
Sean Callanan6a925532011-01-13 08:53:35 +0000157 if (strstr(vi->first(), "$__lldb_expr_result_ptr") &&
158 !strstr(vi->first(), "GV"))
159 {
160 result_name = vi->first();
161 m_result_is_pointer = true;
162 break;
163 }
164
Greg Clayton8de27c72010-10-15 22:48:33 +0000165 if (strstr(vi->first(), "$__lldb_expr_result") &&
Sean Callananc04743d2010-09-28 21:13:03 +0000166 !strstr(vi->first(), "GV"))
167 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000168 result_name = vi->first();
Sean Callanan6a925532011-01-13 08:53:35 +0000169 m_result_is_pointer = false;
Sean Callananc04743d2010-09-28 21:13:03 +0000170 break;
171 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000172 }
173
174 if (!result_name)
175 {
176 if (log)
177 log->PutCString("Couldn't find result variable");
178
179 return true;
180 }
181
Sean Callananc04743d2010-09-28 21:13:03 +0000182 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000183 log->Printf("Result name: \"%s\"", result_name);
Sean Callananc04743d2010-09-28 21:13:03 +0000184
Greg Clayton8de27c72010-10-15 22:48:33 +0000185 Value *result_value = llvm_module.getNamedValue(result_name);
Sean Callanan82b74c82010-08-12 01:56:52 +0000186
187 if (!result_value)
188 {
189 if (log)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000190 log->PutCString("Result variable had no data");
Sean Callanan6a925532011-01-13 08:53:35 +0000191
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000192 return false;
Sean Callanan82b74c82010-08-12 01:56:52 +0000193 }
Sean Callanane8a59a82010-09-13 21:34:21 +0000194
Sean Callanan82b74c82010-08-12 01:56:52 +0000195 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000196 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sean Callanan82b74c82010-08-12 01:56:52 +0000197
198 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
199
200 if (!result_global)
201 {
202 if (log)
203 log->PutCString("Result variable isn't a GlobalVariable");
204 return false;
205 }
206
207 // Find the metadata and follow it to the VarDecl
208
Greg Clayton8de27c72010-10-15 22:48:33 +0000209 NamedMDNode *named_metadata = llvm_module.getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan82b74c82010-08-12 01:56:52 +0000210
211 if (!named_metadata)
212 {
213 if (log)
214 log->PutCString("No global metadata");
215
216 return false;
217 }
218
219 unsigned num_nodes = named_metadata->getNumOperands();
220 unsigned node_index;
221
222 MDNode *metadata_node = NULL;
223
224 for (node_index = 0;
225 node_index < num_nodes;
226 ++node_index)
227 {
228 metadata_node = named_metadata->getOperand(node_index);
229
230 if (metadata_node->getNumOperands() != 2)
231 continue;
232
233 if (metadata_node->getOperand(0) == result_global)
234 break;
235 }
236
237 if (!metadata_node)
238 {
239 if (log)
240 log->PutCString("Couldn't find result metadata");
241 return false;
242 }
243
244 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
245
Greg Claytonb5037af2010-11-15 01:47:11 +0000246 lldb::addr_t result_decl_intptr = constant_int->getZExtValue();
Sean Callanan82b74c82010-08-12 01:56:52 +0000247
248 clang::VarDecl *result_decl = reinterpret_cast<clang::VarDecl *>(result_decl_intptr);
249
250 // Get the next available result name from m_decl_map and create the persistent
251 // variable for it
252
Sean Callanan6a925532011-01-13 08:53:35 +0000253 lldb_private::TypeFromParser result_decl_type;
254
255 if (m_result_is_pointer)
256 {
257 clang::QualType pointer_qual_type = result_decl->getType();
258 clang::Type *pointer_type = pointer_qual_type.getTypePtr();
259 clang::PointerType *pointer_pointertype = dyn_cast<clang::PointerType>(pointer_type);
260
261 if (!pointer_pointertype)
262 {
263 if (log)
264 log->PutCString("Expected result to have pointer type, but it did not");
265 return false;
266 }
267
268 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
269
270 result_decl_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
271 &result_decl->getASTContext());
272 }
273 else
274 {
275 result_decl_type = lldb_private::TypeFromParser(result_decl->getType().getAsOpaquePtr(),
276 &result_decl->getASTContext());
277 }
278
279 m_result_name = m_decl_map->GetPersistentResultName();
280 // If the result is an Lvalue, it is emitted as a pointer; see
281 // ASTResultSynthesizer::SynthesizeBodyResult.
282 m_decl_map->AddPersistentVariable(result_decl,
283 m_result_name,
284 result_decl_type,
285 true,
286 m_result_is_pointer);
Sean Callanan82b74c82010-08-12 01:56:52 +0000287
288 if (log)
Sean Callanan6a925532011-01-13 08:53:35 +0000289 log->Printf("Creating a new result global: \"%s\"", m_result_name.GetCString());
Sean Callanan82b74c82010-08-12 01:56:52 +0000290
291 // Construct a new result global and set up its metadata
292
Greg Clayton8de27c72010-10-15 22:48:33 +0000293 GlobalVariable *new_result_global = new GlobalVariable(llvm_module,
Sean Callanan82b74c82010-08-12 01:56:52 +0000294 result_global->getType()->getElementType(),
295 false, /* not constant */
296 GlobalValue::ExternalLinkage,
297 NULL, /* no initializer */
Sean Callanan6a925532011-01-13 08:53:35 +0000298 m_result_name.GetCString ());
Sean Callanan82b74c82010-08-12 01:56:52 +0000299
300 // It's too late in compilation to create a new VarDecl for this, but we don't
301 // need to. We point the metadata at the old VarDecl. This creates an odd
302 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton8de27c72010-10-15 22:48:33 +0000303 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanan82b74c82010-08-12 01:56:52 +0000304 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
305 // fixed up.
306
307 ConstantInt *new_constant_int = ConstantInt::get(constant_int->getType(),
308 result_decl_intptr,
309 false);
310
311 llvm::Value* values[2];
312 values[0] = new_result_global;
313 values[1] = new_constant_int;
314
Greg Clayton8de27c72010-10-15 22:48:33 +0000315 MDNode *persistent_global_md = MDNode::get(llvm_module.getContext(), values, 2);
Sean Callanan82b74c82010-08-12 01:56:52 +0000316 named_metadata->addOperand(persistent_global_md);
317
318 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000319 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan2e2db532010-09-07 22:43:19 +0000320 PrintValue(result_global).c_str(),
Sean Callanan82b74c82010-08-12 01:56:52 +0000321 PrintValue(new_result_global).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000322
323 if (result_global->hasNUses(0))
324 {
325 // We need to synthesize a store for this variable, because otherwise
326 // there's nothing to put into its equivalent persistent variable.
Sean Callanan82b74c82010-08-12 01:56:52 +0000327
Greg Clayton8de27c72010-10-15 22:48:33 +0000328 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan2e2db532010-09-07 22:43:19 +0000329 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
330
331 if (!first_entry_instruction)
332 return false;
333
334 if (!result_global->hasInitializer())
335 {
336 if (log)
337 log->Printf("Couldn't find initializer for unused variable");
338 return false;
339 }
340
341 Constant *initializer = result_global->getInitializer();
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000342
343 // Here we write the initializer into a result variable assuming it
344 // can be computed statically.
345
346 if (!m_has_side_effects)
347 {
348 MaybeSetConstantResult (initializer,
Sean Callanan6a925532011-01-13 08:53:35 +0000349 m_result_name,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000350 result_decl_type);
351 }
Sean Callanan2e2db532010-09-07 22:43:19 +0000352
353 StoreInst *synthesized_store = new StoreInst::StoreInst(initializer,
354 new_result_global,
355 first_entry_instruction);
356
357 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000358 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000359 }
360 else
361 {
362 result_global->replaceAllUsesWith(new_result_global);
363 }
364
Sean Callanan82b74c82010-08-12 01:56:52 +0000365 result_global->eraseFromParent();
366
367 return true;
368}
369
Greg Clayton3c7feb42010-11-19 01:05:25 +0000370static void DebugUsers(lldb::LogSP &log, Value *value, uint8_t depth)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000371{
372 if (!depth)
373 return;
374
375 depth--;
376
Greg Clayton3c7feb42010-11-19 01:05:25 +0000377 log->Printf(" <Begin %d users>", value->getNumUses());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000378
Greg Clayton3c7feb42010-11-19 01:05:25 +0000379 for (Value::use_iterator ui = value->use_begin(), ue = value->use_end();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000380 ui != ue;
381 ++ui)
382 {
383 log->Printf(" <Use %p> %s", *ui, PrintValue(*ui).c_str());
384 DebugUsers(log, *ui, depth);
385 }
386
387 log->Printf(" <End uses>");
388}
389
390bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000391IRForTarget::RewriteObjCConstString (llvm::Module &llvm_module,
392 llvm::GlobalVariable *ns_str,
393 llvm::GlobalVariable *cstr,
394 Instruction *FirstEntryInstruction)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000395{
396 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
397
Greg Clayton3c7feb42010-11-19 01:05:25 +0000398 const Type *i8_ptr_ty = Type::getInt8PtrTy(llvm_module.getContext());
399 const IntegerType *intptr_ty = Type::getIntNTy(llvm_module.getContext(),
400 (llvm_module.getPointerSize() == Module::Pointer64) ? 64 : 32);
401 const Type *i32_ty = Type::getInt32Ty(llvm_module.getContext());
402 const Type *i8_ty = Type::getInt8Ty(llvm_module.getContext());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000403
404 if (!m_CFStringCreateWithBytes)
405 {
406 lldb::addr_t CFStringCreateWithBytes_addr;
407
408 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
409
410 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
411 {
412 if (log)
413 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
414
415 return false;
416 }
417
418 if (log)
419 log->Printf("Found CFStringCreateWithBytes at 0x%llx", CFStringCreateWithBytes_addr);
420
421 // Build the function type:
422 //
423 // CFStringRef CFStringCreateWithBytes (
424 // CFAllocatorRef alloc,
425 // const UInt8 *bytes,
426 // CFIndex numBytes,
427 // CFStringEncoding encoding,
428 // Boolean isExternalRepresentation
429 // );
430 //
431 // We make the following substitutions:
432 //
433 // CFStringRef -> i8*
434 // CFAllocatorRef -> i8*
435 // UInt8 * -> i8*
436 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
437 // CFStringEncoding -> i32
438 // Boolean -> i8
439
440 std::vector <const Type *> CFSCWB_arg_types;
441 CFSCWB_arg_types.push_back(i8_ptr_ty);
442 CFSCWB_arg_types.push_back(i8_ptr_ty);
443 CFSCWB_arg_types.push_back(intptr_ty);
444 CFSCWB_arg_types.push_back(i32_ty);
445 CFSCWB_arg_types.push_back(i8_ty);
446 llvm::Type *CFSCWB_ty = FunctionType::get(i8_ptr_ty, CFSCWB_arg_types, false);
447
448 // Build the constant containing the pointer to the function
449 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
450 Constant *CFSCWB_addr_int = ConstantInt::get(intptr_ty, CFStringCreateWithBytes_addr, false);
451 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
452 }
453
Greg Clayton3c7feb42010-11-19 01:05:25 +0000454 ConstantArray *string_array = dyn_cast<ConstantArray>(cstr->getInitializer());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000455
456 SmallVector <Value*, 5> CFSCWB_arguments;
457
458 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Greg Clayton3c7feb42010-11-19 01:05:25 +0000459 Constant *bytes_arg = ConstantExpr::getBitCast(cstr, i8_ptr_ty);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000460 Constant *numBytes_arg = ConstantInt::get(intptr_ty, string_array->getType()->getNumElements(), false);
461 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
462 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
463
464 CFSCWB_arguments.push_back(alloc_arg);
465 CFSCWB_arguments.push_back(bytes_arg);
466 CFSCWB_arguments.push_back(numBytes_arg);
467 CFSCWB_arguments.push_back(encoding_arg);
468 CFSCWB_arguments.push_back(isExternal_arg);
469
470 CallInst *CFSCWB_call = CallInst::Create(m_CFStringCreateWithBytes,
471 CFSCWB_arguments.begin(),
472 CFSCWB_arguments.end(),
473 "CFStringCreateWithBytes",
474 FirstEntryInstruction);
Sean Callananae71e302010-11-18 22:21:58 +0000475
Greg Clayton3c7feb42010-11-19 01:05:25 +0000476 if (!UnfoldConstant(ns_str, CFSCWB_call, FirstEntryInstruction))
Sean Callanan6ba533e2010-11-17 23:00:36 +0000477 {
478 if (log)
479 log->PutCString("Couldn't replace the NSString with the result of the call");
480
481 return false;
482 }
483
Greg Clayton3c7feb42010-11-19 01:05:25 +0000484 ns_str->eraseFromParent();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000485
486 return true;
487}
488
489bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000490IRForTarget::RewriteObjCConstStrings(Module &llvm_module, Function &llvm_function)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000491{
492 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
493
Greg Clayton3c7feb42010-11-19 01:05:25 +0000494 ValueSymbolTable& value_symbol_table = llvm_module.getValueSymbolTable();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000495
Greg Clayton3c7feb42010-11-19 01:05:25 +0000496 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000497 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
498
499 if (!FirstEntryInstruction)
500 {
501 if (log)
502 log->PutCString("Couldn't find first instruction for rewritten Objective-C strings");
503
504 return false;
505 }
506
507 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
508 vi != ve;
509 ++vi)
510 {
511 if (strstr(vi->first(), "_unnamed_cfstring_"))
512 {
513 Value *nsstring_value = vi->second;
514
515 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
516
517 if (!nsstring_global)
518 {
519 if (log)
520 log->PutCString("NSString variable is not a GlobalVariable");
521 return false;
522 }
523
524 if (!nsstring_global->hasInitializer())
525 {
526 if (log)
527 log->PutCString("NSString variable does not have an initializer");
528 return false;
529 }
530
531 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
532
533 if (!nsstring_struct)
534 {
535 if (log)
536 log->PutCString("NSString variable's initializer is not a ConstantStruct");
537 return false;
538 }
539
540 // We expect the following structure:
541 //
542 // struct {
543 // int *isa;
544 // int flags;
545 // char *str;
546 // long length;
547 // };
548
549 if (nsstring_struct->getNumOperands() != 4)
550 {
551 if (log)
552 log->Printf("NSString variable's initializer structure has an unexpected number of members. Should be 4, is %d", nsstring_struct->getNumOperands());
553 return false;
554 }
555
556 Constant *nsstring_member = nsstring_struct->getOperand(2);
557
558 if (!nsstring_member)
559 {
560 if (log)
561 log->PutCString("NSString initializer's str element was empty");
562 return false;
563 }
564
565 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
566
567 if (!nsstring_expr)
568 {
569 if (log)
570 log->PutCString("NSString initializer's str element is not a ConstantExpr");
571 return false;
572 }
573
574 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
575 {
576 if (log)
577 log->Printf("NSString initializer's str element is not a GetElementPtr expression, it's a %s", nsstring_expr->getOpcodeName());
578 return false;
579 }
580
581 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
582
583 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
584
585 if (!cstr_global)
586 {
587 if (log)
588 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sean Callanan65e2aee2010-11-20 02:06:01 +0000589
Sean Callanan6ba533e2010-11-17 23:00:36 +0000590 return false;
591 }
592
593 if (!cstr_global->hasInitializer())
594 {
595 if (log)
596 log->PutCString("NSString initializer's str element does not have an initializer");
597 return false;
598 }
599
600 ConstantArray *cstr_array = dyn_cast<ConstantArray>(cstr_global->getInitializer());
601
602 if (!cstr_array)
603 {
604 if (log)
605 log->PutCString("NSString initializer's str element is not a ConstantArray");
606 return false;
607 }
608
609 if (!cstr_array->isCString())
610 {
611 if (log)
612 log->PutCString("NSString initializer's str element is not a C string array");
613 return false;
614 }
615
616 if (log)
617 log->Printf("Found NSString constant %s, which contains \"%s\"", vi->first(), cstr_array->getAsString().c_str());
618
Greg Clayton3c7feb42010-11-19 01:05:25 +0000619 if (!RewriteObjCConstString(llvm_module, nsstring_global, cstr_global, FirstEntryInstruction))
Sean Callanan6ba533e2010-11-17 23:00:36 +0000620 {
621 if (log)
622 log->PutCString("Error rewriting the constant string");
623 return false;
624 }
625
626
627 }
628 }
629
630 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
631 vi != ve;
632 ++vi)
633 {
634 if (!strcmp(vi->first(), "__CFConstantStringClassReference"))
635 {
636 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
637
638 if (!gv)
639 {
640 if (log)
641 log->PutCString("__CFConstantStringClassReference is not a global variable");
642 return false;
643 }
644
645 gv->eraseFromParent();
646
647 break;
648 }
649 }
650
651 return true;
652}
653
Greg Clayton3c7feb42010-11-19 01:05:25 +0000654static bool IsObjCSelectorRef (Value *value)
Sean Callananf5857a02010-07-31 01:32:05 +0000655{
Greg Clayton3c7feb42010-11-19 01:05:25 +0000656 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sean Callananf5857a02010-07-31 01:32:05 +0000657
Greg Clayton3c7feb42010-11-19 01:05:25 +0000658 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
Sean Callananf5857a02010-07-31 01:32:05 +0000659 return false;
660
661 return true;
662}
663
664bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000665IRForTarget::RewriteObjCSelector (Instruction* selector_load, Module &llvm_module)
Sean Callananf5857a02010-07-31 01:32:05 +0000666{
Greg Claytone005f2c2010-11-06 01:53:30 +0000667 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +0000668
669 LoadInst *load = dyn_cast<LoadInst>(selector_load);
670
671 if (!load)
672 return false;
673
674 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
675 //
676 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
677 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
678 //
679 // where %obj is the object pointer and %tmp is the selector.
680 //
Greg Clayton3c7feb42010-11-19 01:05:25 +0000681 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
682 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sean Callananf5857a02010-07-31 01:32:05 +0000683
684 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
685
686 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
687
688 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
689 return false;
690
691 Constant *osr_initializer = _objc_selector_references_->getInitializer();
692
693 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
694
695 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
696 return false;
697
698 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
699
700 if (!osr_initializer_base)
701 return false;
702
703 // Find the string's initializer (a ConstantArray) and get the string from it
704
705 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
706
707 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
708 return false;
709
710 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
711
712 ConstantArray *omvn_initializer_array = dyn_cast<ConstantArray>(omvn_initializer);
713
714 if (!omvn_initializer_array->isString())
715 return false;
716
717 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
718
719 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000720 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sean Callananf5857a02010-07-31 01:32:05 +0000721
722 // Construct a call to sel_registerName
723
724 if (!m_sel_registerName)
725 {
Greg Claytonb5037af2010-11-15 01:47:11 +0000726 lldb::addr_t sel_registerName_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000727
Greg Clayton8de27c72010-10-15 22:48:33 +0000728 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytonb5037af2010-11-15 01:47:11 +0000729 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000730 return false;
731
Sean Callananc2c6f772010-10-26 00:31:56 +0000732 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000733 log->Printf("Found sel_registerName at 0x%llx", sel_registerName_addr);
Sean Callananc2c6f772010-10-26 00:31:56 +0000734
Sean Callananf5857a02010-07-31 01:32:05 +0000735 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
736
737 // The below code would be "more correct," but in actuality what's required is uint8_t*
Greg Clayton3c7feb42010-11-19 01:05:25 +0000738 //Type *sel_type = StructType::get(llvm_module.getContext());
Sean Callananf5857a02010-07-31 01:32:05 +0000739 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Greg Clayton3c7feb42010-11-19 01:05:25 +0000740 const Type *sel_ptr_type = Type::getInt8PtrTy(llvm_module.getContext());
Sean Callananf5857a02010-07-31 01:32:05 +0000741
742 std::vector <const Type *> srN_arg_types;
Greg Clayton3c7feb42010-11-19 01:05:25 +0000743 srN_arg_types.push_back(Type::getInt8PtrTy(llvm_module.getContext()));
Sean Callananf5857a02010-07-31 01:32:05 +0000744 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
745
746 // Build the constant containing the pointer to the function
Greg Clayton3c7feb42010-11-19 01:05:25 +0000747 const IntegerType *intptr_ty = Type::getIntNTy(llvm_module.getContext(),
748 (llvm_module.getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananf5857a02010-07-31 01:32:05 +0000749 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Greg Claytonb5037af2010-11-15 01:47:11 +0000750 Constant *srN_addr_int = ConstantInt::get(intptr_ty, sel_registerName_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000751 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
752 }
753
754 SmallVector <Value*, 1> srN_arguments;
755
Greg Clayton3c7feb42010-11-19 01:05:25 +0000756 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(llvm_module.getContext()));
Sean Callananf5857a02010-07-31 01:32:05 +0000757
758 srN_arguments.push_back(omvn_pointer);
759
760 CallInst *srN_call = CallInst::Create(m_sel_registerName,
761 srN_arguments.begin(),
762 srN_arguments.end(),
Sean Callanan6ba533e2010-11-17 23:00:36 +0000763 "sel_registerName",
Sean Callananf5857a02010-07-31 01:32:05 +0000764 selector_load);
765
766 // Replace the load with the call in all users
767
768 selector_load->replaceAllUsesWith(srN_call);
769
770 selector_load->eraseFromParent();
771
772 return true;
773}
774
775bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000776IRForTarget::RewriteObjCSelectors (Module &llvm_module, BasicBlock &basic_block)
Sean Callananf5857a02010-07-31 01:32:05 +0000777{
Greg Claytone005f2c2010-11-06 01:53:30 +0000778 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +0000779
780 BasicBlock::iterator ii;
781
782 typedef SmallVector <Instruction*, 2> InstrList;
783 typedef InstrList::iterator InstrIterator;
784
785 InstrList selector_loads;
786
Greg Clayton3c7feb42010-11-19 01:05:25 +0000787 for (ii = basic_block.begin();
788 ii != basic_block.end();
Sean Callananf5857a02010-07-31 01:32:05 +0000789 ++ii)
790 {
791 Instruction &inst = *ii;
792
793 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton3c7feb42010-11-19 01:05:25 +0000794 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callananf5857a02010-07-31 01:32:05 +0000795 selector_loads.push_back(&inst);
796 }
797
798 InstrIterator iter;
799
800 for (iter = selector_loads.begin();
801 iter != selector_loads.end();
802 ++iter)
803 {
Greg Clayton3c7feb42010-11-19 01:05:25 +0000804 if (!RewriteObjCSelector(*iter, llvm_module))
Sean Callananf5857a02010-07-31 01:32:05 +0000805 {
806 if(log)
807 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
808 return false;
809 }
810 }
811
812 return true;
813}
814
Sean Callanana48fe162010-08-11 03:57:18 +0000815bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000816IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc,
817 llvm::Module &llvm_module)
Sean Callanana48fe162010-08-11 03:57:18 +0000818{
819 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
820
821 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
822
823 if (!alloc_md || !alloc_md->getNumOperands())
824 return false;
825
826 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
827
828 if (!constant_int)
829 return false;
830
831 // We attempt to register this as a new persistent variable with the DeclMap.
832
833 uintptr_t ptr = constant_int->getZExtValue();
834
Sean Callanan82b74c82010-08-12 01:56:52 +0000835 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanana48fe162010-08-11 03:57:18 +0000836
Sean Callanan82b74c82010-08-12 01:56:52 +0000837 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
838 &decl->getASTContext());
839
Greg Clayton8de27c72010-10-15 22:48:33 +0000840 StringRef decl_name (decl->getName());
841 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan6a925532011-01-13 08:53:35 +0000842 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanana48fe162010-08-11 03:57:18 +0000843 return false;
844
Greg Clayton8de27c72010-10-15 22:48:33 +0000845 GlobalVariable *persistent_global = new GlobalVariable(llvm_module,
Sean Callanana48fe162010-08-11 03:57:18 +0000846 alloc->getType()->getElementType(),
847 false, /* not constant */
848 GlobalValue::ExternalLinkage,
849 NULL, /* no initializer */
850 alloc->getName().str().c_str());
851
852 // What we're going to do here is make believe this was a regular old external
853 // variable. That means we need to make the metadata valid.
854
Greg Clayton8de27c72010-10-15 22:48:33 +0000855 NamedMDNode *named_metadata = llvm_module.getNamedMetadata("clang.global.decl.ptrs");
Sean Callanana48fe162010-08-11 03:57:18 +0000856
857 llvm::Value* values[2];
858 values[0] = persistent_global;
859 values[1] = constant_int;
860
Greg Clayton8de27c72010-10-15 22:48:33 +0000861 MDNode *persistent_global_md = MDNode::get(llvm_module.getContext(), values, 2);
Sean Callanana48fe162010-08-11 03:57:18 +0000862 named_metadata->addOperand(persistent_global_md);
863
864 alloc->replaceAllUsesWith(persistent_global);
865 alloc->eraseFromParent();
866
867 return true;
868}
869
870bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000871IRForTarget::RewritePersistentAllocs(llvm::Module &llvm_module, llvm::BasicBlock &basic_block)
Sean Callanana48fe162010-08-11 03:57:18 +0000872{
Sean Callanane8a59a82010-09-13 21:34:21 +0000873 if (!m_resolve_vars)
874 return true;
875
Greg Claytone005f2c2010-11-06 01:53:30 +0000876 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanana48fe162010-08-11 03:57:18 +0000877
878 BasicBlock::iterator ii;
879
880 typedef SmallVector <Instruction*, 2> InstrList;
881 typedef InstrList::iterator InstrIterator;
882
883 InstrList pvar_allocs;
884
Greg Clayton3c7feb42010-11-19 01:05:25 +0000885 for (ii = basic_block.begin();
886 ii != basic_block.end();
Sean Callanana48fe162010-08-11 03:57:18 +0000887 ++ii)
888 {
889 Instruction &inst = *ii;
890
891 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callanan03c997b2010-10-21 22:41:32 +0000892 if (alloc->getName().startswith("$") &&
893 !alloc->getName().startswith("$__lldb"))
Sean Callanana48fe162010-08-11 03:57:18 +0000894 pvar_allocs.push_back(alloc);
895 }
896
897 InstrIterator iter;
898
899 for (iter = pvar_allocs.begin();
900 iter != pvar_allocs.end();
901 ++iter)
902 {
Greg Clayton3c7feb42010-11-19 01:05:25 +0000903 if (!RewritePersistentAlloc(*iter, llvm_module))
Sean Callanana48fe162010-08-11 03:57:18 +0000904 {
905 if(log)
906 log->PutCString("Couldn't rewrite the creation of a persistent variable");
907 return false;
908 }
909 }
910
911 return true;
912}
913
Sean Callanan8bce6652010-07-13 21:41:46 +0000914static clang::NamedDecl *
Greg Clayton3c7feb42010-11-19 01:05:25 +0000915DeclForGlobalValue(Module &module, GlobalValue *global_value)
Sean Callanan8bce6652010-07-13 21:41:46 +0000916{
917 NamedMDNode *named_metadata = module.getNamedMetadata("clang.global.decl.ptrs");
918
919 if (!named_metadata)
920 return NULL;
921
922 unsigned num_nodes = named_metadata->getNumOperands();
923 unsigned node_index;
924
925 for (node_index = 0;
926 node_index < num_nodes;
927 ++node_index)
928 {
929 MDNode *metadata_node = named_metadata->getOperand(node_index);
930
931 if (!metadata_node)
932 return NULL;
933
934 if (metadata_node->getNumOperands() != 2)
Sean Callanana48fe162010-08-11 03:57:18 +0000935 continue;
Sean Callanan8bce6652010-07-13 21:41:46 +0000936
937 if (metadata_node->getOperand(0) != global_value)
938 continue;
939
940 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
941
942 if (!constant_int)
943 return NULL;
944
945 uintptr_t ptr = constant_int->getZExtValue();
946
947 return reinterpret_cast<clang::NamedDecl *>(ptr);
948 }
949
950 return NULL;
951}
952
953bool
Greg Clayton3c7feb42010-11-19 01:05:25 +0000954IRForTarget::MaybeHandleVariable (Module &llvm_module, Value *llvm_value_ptr)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000955{
Greg Claytone005f2c2010-11-06 01:53:30 +0000956 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan48443652010-12-02 19:47:57 +0000957
958 if (log)
Sean Callananb9f09a62010-12-06 22:16:55 +0000959 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sean Callananf5857a02010-07-31 01:32:05 +0000960
Greg Clayton8de27c72010-10-15 22:48:33 +0000961 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callananbc2928a2010-08-03 00:23:29 +0000962 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000963 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +0000964 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000965 default:
966 break;
967 case Instruction::GetElementPtr:
968 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +0000969 Value *s = constant_expr->getOperand(0);
Sean Callanan48443652010-12-02 19:47:57 +0000970 if (!MaybeHandleVariable(llvm_module, s))
971 return false;
Sean Callananbc2928a2010-08-03 00:23:29 +0000972 }
973 }
Sean Callananf921cf52010-12-03 19:51:05 +0000974 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000975 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000976 clang::NamedDecl *named_decl = DeclForGlobalValue(llvm_module, global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000977
Sean Callananf5857a02010-07-31 01:32:05 +0000978 if (!named_decl)
979 {
Greg Clayton3c7feb42010-11-19 01:05:25 +0000980 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000981 return true;
982
Sean Callanan7cd46742010-12-06 00:56:39 +0000983 if (!global_variable->hasExternalLinkage())
984 return true;
985
Sean Callananf5857a02010-07-31 01:32:05 +0000986 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000987 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sean Callananf5857a02010-07-31 01:32:05 +0000988 return false;
989 }
990
Greg Clayton8de27c72010-10-15 22:48:33 +0000991 std::string name (named_decl->getName().str());
Sean Callanan810f22d2010-07-16 00:09:46 +0000992
Sean Callanan771131d2010-09-30 21:18:25 +0000993 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +0000994 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000995
996 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +0000997 {
Sean Callanan771131d2010-09-30 21:18:25 +0000998 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +0000999 ast_context = &value_decl->getASTContext();
1000 }
Sean Callanan810f22d2010-07-16 00:09:46 +00001001 else
Sean Callananf328c9f2010-07-20 23:31:16 +00001002 {
Sean Callanan810f22d2010-07-16 00:09:46 +00001003 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +00001004 }
Sean Callanan771131d2010-09-30 21:18:25 +00001005
Sean Callanan6a925532011-01-13 08:53:35 +00001006 clang::QualType qual_type;
1007 const Type *value_type;
1008
1009 if (!name.compare("$__lldb_expr_result"))
1010 {
1011 // The $__lldb_expr_result name indicates the the return value has allocated as
1012 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1013 // accesses to this static variable need to be redirected to the result of dereferencing
1014 // a pointer that is passed in as one of the arguments.
1015 //
1016 // Consequently, when reporting the size of the type, we report a pointer type pointing
1017 // to the type of $__lldb_expr_result, not the type itself.
Sean Callananf328c9f2010-07-20 23:31:16 +00001018
Sean Callanan6a925532011-01-13 08:53:35 +00001019 qual_type = ast_context->getPointerType(clang::QualType::getFromOpaquePtr(opaque_type));
1020 value_type = PointerType::get(global_variable->getType(), 0);
1021 }
1022 else
1023 {
1024 qual_type = clang::QualType::getFromOpaquePtr(opaque_type);
1025 value_type = global_variable->getType();
1026 }
Sean Callanan771131d2010-09-30 21:18:25 +00001027
1028 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
1029 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001030
Sean Callanan771131d2010-09-30 21:18:25 +00001031 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001032 log->Printf("Type of \"%s\" is [clang \"%s\", lldb \"%s\"] [size %d, align %d]",
Sean Callanan771131d2010-09-30 21:18:25 +00001033 name.c_str(),
1034 qual_type.getAsString().c_str(),
1035 PrintType(value_type).c_str(),
1036 value_size,
1037 value_alignment);
Sean Callanan3aa7da52010-12-13 22:46:15 +00001038
Sean Callanan8bce6652010-07-13 21:41:46 +00001039
Sean Callanan8c127202010-08-23 23:09:38 +00001040 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton8de27c72010-10-15 22:48:33 +00001041 lldb_private::ConstString (name.c_str()),
1042 llvm_value_ptr,
Sean Callananba992c52010-07-27 02:07:53 +00001043 value_size,
1044 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +00001045 return false;
1046 }
Sean Callananf3143b72010-12-03 03:02:31 +00001047 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanan48443652010-12-02 19:47:57 +00001048 {
1049 if (log)
1050 log->Printf("Function pointers aren't handled right now");
1051
1052 return false;
1053 }
Sean Callanan8bce6652010-07-13 21:41:46 +00001054
1055 return true;
1056}
1057
1058bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001059IRForTarget::MaybeHandleCallArguments (Module &llvm_module, CallInst *Old)
Sean Callanandc27aba2010-10-05 22:26:43 +00001060{
Sean Callanan48443652010-12-02 19:47:57 +00001061 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1062
1063 if (log)
1064 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sean Callanandc27aba2010-10-05 22:26:43 +00001065
Sean Callanan6ba533e2010-11-17 23:00:36 +00001066 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanandc27aba2010-10-05 22:26:43 +00001067 op_index < num_ops;
1068 ++op_index)
Greg Clayton3c7feb42010-11-19 01:05:25 +00001069 if (!MaybeHandleVariable(llvm_module, Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanandc27aba2010-10-05 22:26:43 +00001070 return false;
1071
1072 return true;
1073}
1074
1075bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001076IRForTarget::MaybeHandleCall (Module &llvm_module, CallInst *llvm_call_inst)
Sean Callananba992c52010-07-27 02:07:53 +00001077{
Greg Claytone005f2c2010-11-06 01:53:30 +00001078 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananba992c52010-07-27 02:07:53 +00001079
Greg Clayton8de27c72010-10-15 22:48:33 +00001080 Function *fun = llvm_call_inst->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +00001081
1082 if (fun == NULL)
Sean Callanan65af7342010-09-08 20:04:08 +00001083 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001084 Value *val = llvm_call_inst->getCalledValue();
Sean Callanan65af7342010-09-08 20:04:08 +00001085
1086 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(val);
1087
1088 if (const_expr && const_expr->getOpcode() == Instruction::BitCast)
1089 {
1090 fun = dyn_cast<Function>(const_expr->getOperand(0));
1091
1092 if (!fun)
Sean Callanan48443652010-12-02 19:47:57 +00001093 return false;
Sean Callanan65af7342010-09-08 20:04:08 +00001094 }
Sean Callananc4217a62010-12-06 23:53:20 +00001095 else if (const_expr && const_expr->getOpcode() == Instruction::IntToPtr)
1096 {
1097 return true; // already resolved
1098 }
Sean Callanan65af7342010-09-08 20:04:08 +00001099 else
1100 {
Sean Callanan48443652010-12-02 19:47:57 +00001101 return false;
Sean Callanan65af7342010-09-08 20:04:08 +00001102 }
1103 }
Sean Callananba992c52010-07-27 02:07:53 +00001104
Greg Clayton8de27c72010-10-15 22:48:33 +00001105 lldb_private::ConstString str;
Sean Callananc04743d2010-09-28 21:13:03 +00001106
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001107 if (fun->isIntrinsic())
Sean Callananc04743d2010-09-28 21:13:03 +00001108 {
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001109 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sean Callananc04743d2010-09-28 21:13:03 +00001110
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001111 switch (intrinsic_id)
1112 {
1113 default:
1114 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001115 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001116 return false;
1117 case Intrinsic::memcpy:
Greg Clayton8de27c72010-10-15 22:48:33 +00001118 {
1119 static lldb_private::ConstString g_memcpy_str ("memcpy");
1120 str = g_memcpy_str;
1121 }
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001122 break;
1123 }
Sean Callananc04743d2010-09-28 21:13:03 +00001124
Greg Clayton8de27c72010-10-15 22:48:33 +00001125 if (log && str)
Greg Claytonb5037af2010-11-15 01:47:11 +00001126 log->Printf("Resolved intrinsic name \"%s\"", str.GetCString());
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001127 }
1128 else
1129 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001130 str.SetCStringWithLength (fun->getName().data(), fun->getName().size());
Sean Callananc04743d2010-09-28 21:13:03 +00001131 }
1132
Greg Clayton8de27c72010-10-15 22:48:33 +00001133 clang::NamedDecl *fun_decl = DeclForGlobalValue (llvm_module, fun);
Greg Claytonb5037af2010-11-15 01:47:11 +00001134 lldb::addr_t fun_addr = LLDB_INVALID_ADDRESS;
Sean Callananf5857a02010-07-31 01:32:05 +00001135 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +00001136
Sean Callananf5857a02010-07-31 01:32:05 +00001137 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +00001138 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001139 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_value_ptr, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +00001140 {
Sean Callanan92aa6662010-09-07 21:49:41 +00001141 fun_value_ptr = NULL;
1142
Greg Clayton8de27c72010-10-15 22:48:33 +00001143 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callanan92aa6662010-09-07 21:49:41 +00001144 {
1145 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001146 log->Printf("Function \"%s\" had no address", str.GetCString());
Sean Callanan92aa6662010-09-07 21:49:41 +00001147
1148 return false;
1149 }
Sean Callananf5857a02010-07-31 01:32:05 +00001150 }
1151 }
1152 else
1153 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001154 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +00001155 {
1156 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001157 log->Printf ("Metadataless function \"%s\" had no address", str.GetCString());
Sean Callananf5857a02010-07-31 01:32:05 +00001158 }
Sean Callananba992c52010-07-27 02:07:53 +00001159 }
1160
1161 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001162 log->Printf("Found \"%s\" at 0x%llx", str.GetCString(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +00001163
Sean Callananf5857a02010-07-31 01:32:05 +00001164 Value *fun_addr_ptr;
1165
1166 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +00001167 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001168 const IntegerType *intptr_ty = Type::getIntNTy(llvm_module.getContext(),
1169 (llvm_module.getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan92aa6662010-09-07 21:49:41 +00001170 const FunctionType *fun_ty = fun->getFunctionType();
Sean Callanan02fbafa2010-07-27 21:39:39 +00001171 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
1172 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +00001173 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
1174
1175 if (fun_value_ptr)
1176 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001177 }
Sean Callananf5857a02010-07-31 01:32:05 +00001178
1179 if (fun_value_ptr)
1180 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001181
Greg Clayton8de27c72010-10-15 22:48:33 +00001182 llvm_call_inst->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +00001183
Greg Clayton8de27c72010-10-15 22:48:33 +00001184 ConstantArray *func_name = (ConstantArray*)ConstantArray::get(llvm_module.getContext(), str.GetCString());
Sean Callanane8a59a82010-09-13 21:34:21 +00001185
1186 Value *values[1];
1187 values[0] = func_name;
Greg Clayton8de27c72010-10-15 22:48:33 +00001188 MDNode *func_metadata = MDNode::get(llvm_module.getContext(), values, 1);
Sean Callanane8a59a82010-09-13 21:34:21 +00001189
Greg Clayton8de27c72010-10-15 22:48:33 +00001190 llvm_call_inst->setMetadata("lldb.call.realName", func_metadata);
Sean Callanane8a59a82010-09-13 21:34:21 +00001191
1192 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001193 log->Printf("Set metadata for %p [%d, \"%s\"]", llvm_call_inst, func_name->isString(), func_name->getAsString().c_str());
Sean Callanane8a59a82010-09-13 21:34:21 +00001194
Sean Callananba992c52010-07-27 02:07:53 +00001195 return true;
1196}
1197
1198bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001199IRForTarget::ResolveCalls(Module &llvm_module, BasicBlock &basic_block)
Sean Callanan8bce6652010-07-13 21:41:46 +00001200{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001201 /////////////////////////////////////////////////////////////////////////
1202 // Prepare the current basic block for execution in the remote process
1203 //
1204
Sean Callanan02fbafa2010-07-27 21:39:39 +00001205 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +00001206
Greg Clayton3c7feb42010-11-19 01:05:25 +00001207 for (ii = basic_block.begin();
1208 ii != basic_block.end();
Sean Callanan8bce6652010-07-13 21:41:46 +00001209 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001210 {
Sean Callanan8bce6652010-07-13 21:41:46 +00001211 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001212
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001213 CallInst *call = dyn_cast<CallInst>(&inst);
Sean Callananba992c52010-07-27 02:07:53 +00001214
Greg Clayton3c7feb42010-11-19 01:05:25 +00001215 if (call && !MaybeHandleCall(llvm_module, call))
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001216 return false;
Sean Callanan48443652010-12-02 19:47:57 +00001217
1218 if (call && !MaybeHandleCallArguments(llvm_module, call))
1219 return false;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001220 }
1221
1222 return true;
1223}
1224
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001225bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001226IRForTarget::ResolveExternals (Module &llvm_module, Function &llvm_function)
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001227{
Sean Callananae71e302010-11-18 22:21:58 +00001228 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1229
Greg Clayton3c7feb42010-11-19 01:05:25 +00001230 for (Module::global_iterator global = llvm_module.global_begin(), end = llvm_module.global_end();
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001231 global != end;
1232 ++global)
1233 {
Greg Clayton3c7feb42010-11-19 01:05:25 +00001234 if (log)
1235 log->Printf("Examining %s, DeclForGlobalValue returns %p",
1236 (*global).getName().str().c_str(),
1237 DeclForGlobalValue(llvm_module, global));
1238
1239 if (DeclForGlobalValue(llvm_module, global) &&
1240 !MaybeHandleVariable (llvm_module, global))
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001241 return false;
1242 }
1243
1244 return true;
1245}
1246
Sean Callanan02fbafa2010-07-27 21:39:39 +00001247static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +00001248{
Sean Callanan6ba533e2010-11-17 23:00:36 +00001249 Constant *Old;
Sean Callanan45839272010-07-24 01:37:44 +00001250
Sean Callanan6ba533e2010-11-17 23:00:36 +00001251 if (!(Old = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +00001252 return false;
1253
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001254 ConstantExpr *CE;
1255
1256 if ((CE = dyn_cast<ConstantExpr>(V)))
1257 {
1258 if (CE->getOpcode() != Instruction::BitCast)
1259 return false;
1260
Sean Callanan6ba533e2010-11-17 23:00:36 +00001261 Old = CE->getOperand(0);
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001262 }
1263
Sean Callanan6ba533e2010-11-17 23:00:36 +00001264 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sean Callanan45839272010-07-24 01:37:44 +00001265
1266 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
1267 return false;
1268
1269 return true;
1270}
1271
Greg Clayton3c7feb42010-11-19 01:05:25 +00001272static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &llvm_module)
Sean Callanan45839272010-07-24 01:37:44 +00001273{
Greg Clayton3c7feb42010-11-19 01:05:25 +00001274 Constant* zero(ConstantInt::get(Type::getInt8Ty(llvm_module.getContext()), 0, true));
Sean Callanan45839272010-07-24 01:37:44 +00001275
1276 Value::use_iterator ui;
1277
1278 for (ui = guard_load->use_begin();
1279 ui != guard_load->use_end();
1280 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +00001281 {
Greg Clayton6e713402010-07-30 20:30:44 +00001282 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +00001283 {
1284 // do nothing for the moment
1285 }
1286 else
1287 {
1288 ui->replaceUsesOfWith(guard_load, zero);
1289 }
1290 }
Sean Callanan45839272010-07-24 01:37:44 +00001291
1292 guard_load->eraseFromParent();
1293}
1294
1295static void ExciseGuardStore(Instruction* guard_store)
1296{
1297 guard_store->eraseFromParent();
1298}
1299
1300bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001301IRForTarget::RemoveGuards(Module &llvm_module, BasicBlock &basic_block)
Sean Callanan45839272010-07-24 01:37:44 +00001302{
1303 ///////////////////////////////////////////////////////
1304 // Eliminate any reference to guard variables found.
1305 //
1306
Sean Callanan02fbafa2010-07-27 21:39:39 +00001307 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +00001308
Sean Callanan02fbafa2010-07-27 21:39:39 +00001309 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +00001310 typedef InstrList::iterator InstrIterator;
1311
1312 InstrList guard_loads;
1313 InstrList guard_stores;
1314
Greg Clayton3c7feb42010-11-19 01:05:25 +00001315 for (ii = basic_block.begin();
1316 ii != basic_block.end();
Sean Callanan45839272010-07-24 01:37:44 +00001317 ++ii)
1318 {
1319 Instruction &inst = *ii;
1320
1321 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1322 if (isGuardVariableRef(load->getPointerOperand()))
1323 guard_loads.push_back(&inst);
1324
1325 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
1326 if (isGuardVariableRef(store->getPointerOperand()))
1327 guard_stores.push_back(&inst);
1328 }
1329
1330 InstrIterator iter;
1331
1332 for (iter = guard_loads.begin();
1333 iter != guard_loads.end();
1334 ++iter)
Greg Clayton3c7feb42010-11-19 01:05:25 +00001335 TurnGuardLoadIntoZero(*iter, llvm_module);
Sean Callanan45839272010-07-24 01:37:44 +00001336
1337 for (iter = guard_stores.begin();
1338 iter != guard_stores.end();
1339 ++iter)
1340 ExciseGuardStore(*iter);
1341
1342 return true;
1343}
1344
Sean Callanan6ba533e2010-11-17 23:00:36 +00001345bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001346IRForTarget::UnfoldConstant(Constant *old_constant, Value *new_constant, Instruction *first_entry_inst)
Sean Callananbafd6852010-07-14 23:40:29 +00001347{
Greg Claytone005f2c2010-11-06 01:53:30 +00001348 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbafd6852010-07-14 23:40:29 +00001349
1350 Value::use_iterator ui;
1351
Sean Callanana48fe162010-08-11 03:57:18 +00001352 SmallVector<User*, 16> users;
1353
1354 // We do this because the use list might change, invalidating our iterator.
1355 // Much better to keep a work list ourselves.
Greg Clayton3c7feb42010-11-19 01:05:25 +00001356 for (ui = old_constant->use_begin();
1357 ui != old_constant->use_end();
Sean Callananbafd6852010-07-14 23:40:29 +00001358 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +00001359 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +00001360
Sean Callanana48fe162010-08-11 03:57:18 +00001361 for (int i = 0;
1362 i < users.size();
1363 ++i)
1364 {
1365 User *user = users[i];
1366
Sean Callananbafd6852010-07-14 23:40:29 +00001367 if (Constant *constant = dyn_cast<Constant>(user))
1368 {
1369 // synthesize a new non-constant equivalent of the constant
1370
1371 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
1372 {
1373 switch (constant_expr->getOpcode())
1374 {
1375 default:
1376 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001377 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00001378 return false;
1379 case Instruction::BitCast:
1380 {
1381 // UnaryExpr
1382 // OperandList[0] is value
1383
1384 Value *s = constant_expr->getOperand(0);
1385
Greg Clayton3c7feb42010-11-19 01:05:25 +00001386 if (s == old_constant)
1387 s = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00001388
Greg Clayton3c7feb42010-11-19 01:05:25 +00001389 BitCastInst *bit_cast(new BitCastInst(s, old_constant->getType(), "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00001390
Greg Clayton3c7feb42010-11-19 01:05:25 +00001391 UnfoldConstant(constant_expr, bit_cast, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00001392 }
1393 break;
1394 case Instruction::GetElementPtr:
1395 {
1396 // GetElementPtrConstantExpr
1397 // OperandList[0] is base
1398 // OperandList[1]... are indices
1399
1400 Value *ptr = constant_expr->getOperand(0);
1401
Greg Clayton3c7feb42010-11-19 01:05:25 +00001402 if (ptr == old_constant)
1403 ptr = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00001404
1405 SmallVector<Value*, 16> indices;
1406
1407 unsigned operand_index;
1408 unsigned num_operands = constant_expr->getNumOperands();
1409
1410 for (operand_index = 1;
1411 operand_index < num_operands;
1412 ++operand_index)
1413 {
1414 Value *operand = constant_expr->getOperand(operand_index);
1415
Greg Clayton3c7feb42010-11-19 01:05:25 +00001416 if (operand == old_constant)
1417 operand = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00001418
1419 indices.push_back(operand);
1420 }
1421
Greg Clayton3c7feb42010-11-19 01:05:25 +00001422 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00001423
Greg Clayton3c7feb42010-11-19 01:05:25 +00001424 UnfoldConstant(constant_expr, get_element_ptr, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00001425 }
1426 break;
1427 }
1428 }
1429 else
1430 {
1431 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001432 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00001433 return false;
1434 }
1435 }
1436 else
1437 {
1438 // simple fall-through case for non-constants
Greg Clayton3c7feb42010-11-19 01:05:25 +00001439 user->replaceUsesOfWith(old_constant, new_constant);
Sean Callananbafd6852010-07-14 23:40:29 +00001440 }
1441 }
1442
1443 return true;
1444}
1445
Sean Callanan8bce6652010-07-13 21:41:46 +00001446bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001447IRForTarget::ReplaceVariables (Module &llvm_module, Function &llvm_function)
Sean Callanan8bce6652010-07-13 21:41:46 +00001448{
Sean Callanane8a59a82010-09-13 21:34:21 +00001449 if (!m_resolve_vars)
1450 return true;
1451
Greg Claytone005f2c2010-11-06 01:53:30 +00001452 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8bce6652010-07-13 21:41:46 +00001453
1454 m_decl_map->DoStructLayout();
1455
1456 if (log)
1457 log->Printf("Element arrangement:");
1458
1459 uint32_t num_elements;
1460 uint32_t element_index;
1461
1462 size_t size;
1463 off_t alignment;
1464
1465 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
1466 return false;
1467
Greg Clayton3c7feb42010-11-19 01:05:25 +00001468 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00001469
Greg Clayton3c7feb42010-11-19 01:05:25 +00001470 if (iter == llvm_function.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +00001471 return false;
1472
Sean Callanan02fbafa2010-07-27 21:39:39 +00001473 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00001474
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001475 if (argument->getName().equals("this"))
1476 {
1477 ++iter;
1478
Greg Clayton3c7feb42010-11-19 01:05:25 +00001479 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001480 return false;
1481
1482 argument = iter;
1483 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00001484 else if (argument->getName().equals("self"))
1485 {
1486 ++iter;
1487
1488 if (iter == llvm_function.getArgumentList().end())
1489 return false;
1490
1491 if (!iter->getName().equals("_cmd"))
1492 return false;
1493
1494 ++iter;
1495
1496 if (iter == llvm_function.getArgumentList().end())
1497 return false;
1498
1499 argument = iter;
1500 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001501
Greg Clayton8de27c72010-10-15 22:48:33 +00001502 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan8bce6652010-07-13 21:41:46 +00001503 return false;
1504
1505 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001506 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sean Callanan8bce6652010-07-13 21:41:46 +00001507
Greg Clayton3c7feb42010-11-19 01:05:25 +00001508 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +00001509 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00001510
Sean Callanan6ba533e2010-11-17 23:00:36 +00001511 if (!FirstEntryInstruction)
Sean Callanan8bce6652010-07-13 21:41:46 +00001512 return false;
1513
Greg Clayton3c7feb42010-11-19 01:05:25 +00001514 LLVMContext &context(llvm_module.getContext());
Sean Callanan8bce6652010-07-13 21:41:46 +00001515 const IntegerType *offset_type(Type::getInt32Ty(context));
1516
1517 if (!offset_type)
1518 return false;
1519
1520 for (element_index = 0; element_index < num_elements; ++element_index)
1521 {
1522 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001523 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +00001524 off_t offset;
Greg Clayton8de27c72010-10-15 22:48:33 +00001525 lldb_private::ConstString name;
Sean Callanan8bce6652010-07-13 21:41:46 +00001526
Sean Callanan45690fe2010-08-30 22:17:16 +00001527 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan8bce6652010-07-13 21:41:46 +00001528 return false;
1529
1530 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001531 log->Printf(" \"%s\" [\"%s\"] (\"%s\") placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00001532 value->getName().str().c_str(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001533 name.GetCString(),
Sean Callanan8bce6652010-07-13 21:41:46 +00001534 PrintValue(value, true).c_str(),
1535 offset);
1536
1537 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
Sean Callanan6ba533e2010-11-17 23:00:36 +00001538 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", FirstEntryInstruction);
Sean Callanan6a925532011-01-13 08:53:35 +00001539
1540 Value *replacement;
Sean Callanan8bce6652010-07-13 21:41:46 +00001541
Sean Callanan6a925532011-01-13 08:53:35 +00001542 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
1543 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
1544 // entry in order to produce the static variable that the AST thinks it is accessing.
1545 if (name == m_result_name && !m_result_is_pointer)
1546 {
1547 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType()->getPointerTo(), "", FirstEntryInstruction);
1548
1549 LoadInst *load = new LoadInst(bit_cast, "", FirstEntryInstruction);
1550
1551 replacement = load;
1552 }
Sean Callananbafd6852010-07-14 23:40:29 +00001553 else
Sean Callanan6a925532011-01-13 08:53:35 +00001554 {
1555 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", FirstEntryInstruction);
1556
1557 replacement = bit_cast;
1558 }
1559
1560 if (Constant *constant = dyn_cast<Constant>(value))
1561 UnfoldConstant(constant, replacement, FirstEntryInstruction);
1562 else
1563 value->replaceAllUsesWith(replacement);
Sean Callananb51ee982010-11-02 23:51:17 +00001564
1565 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
1566 var->eraseFromParent();
Sean Callanan8bce6652010-07-13 21:41:46 +00001567 }
1568
1569 if (log)
1570 log->Printf("Total structure [align %d, size %d]", alignment, size);
1571
1572 return true;
1573}
1574
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001575bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001576IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001577{
Greg Claytone005f2c2010-11-06 01:53:30 +00001578 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001579
Greg Clayton3c7feb42010-11-19 01:05:25 +00001580 Function* function = llvm_module.getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001581
1582 if (!function)
1583 {
1584 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001585 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sean Callanan6a925532011-01-13 08:53:35 +00001586
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001587 return false;
1588 }
1589
Sean Callanan02fbafa2010-07-27 21:39:39 +00001590 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001591
Sean Callanan05a5a1b2010-12-16 03:17:46 +00001592 m_has_side_effects = HasSideEffects(llvm_module, *function);
1593
Sean Callanan82b74c82010-08-12 01:56:52 +00001594 ////////////////////////////////////////////////////////////
Greg Clayton8de27c72010-10-15 22:48:33 +00001595 // Replace $__lldb_expr_result with a persistent variable
Sean Callanan82b74c82010-08-12 01:56:52 +00001596 //
1597
Greg Clayton3c7feb42010-11-19 01:05:25 +00001598 if (!CreateResultVariable(llvm_module, *function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001599 {
1600 if (log)
1601 log->Printf("CreateResultVariable() failed");
Sean Callanan82b74c82010-08-12 01:56:52 +00001602 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001603 }
Sean Callanan82b74c82010-08-12 01:56:52 +00001604
Sean Callanan6ba533e2010-11-17 23:00:36 +00001605 ///////////////////////////////////////////////////////////////////////////////
1606 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
1607 //
Sean Callanan6ba533e2010-11-17 23:00:36 +00001608
Greg Clayton3c7feb42010-11-19 01:05:25 +00001609 if (!RewriteObjCConstStrings(llvm_module, *function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001610 {
1611 if (log)
1612 log->Printf("RewriteObjCConstStrings() failed");
Sean Callanan6ba533e2010-11-17 23:00:36 +00001613 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001614 }
Sean Callanan6ba533e2010-11-17 23:00:36 +00001615
Sean Callananf5857a02010-07-31 01:32:05 +00001616 //////////////////////////////////
1617 // Run basic-block level passes
1618 //
1619
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001620 for (bbi = function->begin();
1621 bbi != function->end();
1622 ++bbi)
1623 {
Greg Clayton3c7feb42010-11-19 01:05:25 +00001624 if (!RemoveGuards(llvm_module, *bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001625 {
1626 if (log)
1627 log->Printf("RemoveGuards() failed");
Sean Callanan8c127202010-08-23 23:09:38 +00001628 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001629 }
Sean Callanan8c127202010-08-23 23:09:38 +00001630
Greg Clayton3c7feb42010-11-19 01:05:25 +00001631 if (!RewritePersistentAllocs(llvm_module, *bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001632 {
1633 if (log)
1634 log->Printf("RewritePersistentAllocs() failed");
Sean Callananf5857a02010-07-31 01:32:05 +00001635 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001636 }
Sean Callananf5857a02010-07-31 01:32:05 +00001637
Greg Clayton3c7feb42010-11-19 01:05:25 +00001638 if (!RewriteObjCSelectors(llvm_module, *bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001639 {
1640 if (log)
1641 log->Printf("RewriteObjCSelectors() failed");
Sean Callanana48fe162010-08-11 03:57:18 +00001642 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001643 }
Sean Callanana48fe162010-08-11 03:57:18 +00001644
Greg Clayton3c7feb42010-11-19 01:05:25 +00001645 if (!ResolveCalls(llvm_module, *bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001646 {
1647 if (log)
1648 log->Printf("ResolveCalls() failed");
Sean Callanan8bce6652010-07-13 21:41:46 +00001649 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001650 }
Sean Callanan8bce6652010-07-13 21:41:46 +00001651 }
1652
Sean Callanan771131d2010-09-30 21:18:25 +00001653 ///////////////////////////////
1654 // Run function-level passes
1655 //
1656
Greg Clayton3c7feb42010-11-19 01:05:25 +00001657 if (!ResolveExternals(llvm_module, *function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001658 {
1659 if (log)
1660 log->Printf("ResolveExternals() failed");
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001661 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001662 }
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001663
Greg Clayton3c7feb42010-11-19 01:05:25 +00001664 if (!ReplaceVariables(llvm_module, *function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00001665 {
1666 if (log)
1667 log->Printf("ReplaceVariables() failed");
Sean Callanan771131d2010-09-30 21:18:25 +00001668 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001669 }
Sean Callanan771131d2010-09-30 21:18:25 +00001670
Sean Callanan8bce6652010-07-13 21:41:46 +00001671 if (log)
1672 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00001673 std::string s;
1674 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00001675
Greg Clayton3c7feb42010-11-19 01:05:25 +00001676 llvm_module.print(oss, NULL);
Sean Callanan321fe9e2010-07-28 01:00:59 +00001677
1678 oss.flush();
1679
Greg Claytonb5037af2010-11-15 01:47:11 +00001680 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001681 }
1682
1683 return true;
1684}
1685
1686void
Greg Clayton3c7feb42010-11-19 01:05:25 +00001687IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001688{
1689}
1690
1691PassManagerType
1692IRForTarget::getPotentialPassManagerType() const
1693{
1694 return PMT_ModulePassManager;
1695}