blob: 5b189492b852b5654f33239d9b96da4318650198 [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
35IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
Sean Callanane8a59a82010-09-13 21:34:21 +000036 bool resolve_vars,
Sean Callanan65dafa82010-08-27 01:01:44 +000037 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +000038 ModulePass(ID),
Sean Callanan8bce6652010-07-13 21:41:46 +000039 m_decl_map(decl_map),
Sean Callanan65dafa82010-08-27 01:01:44 +000040 m_sel_registerName(NULL),
Sean Callanane8a59a82010-09-13 21:34:21 +000041 m_func_name(func_name),
42 m_resolve_vars(resolve_vars)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000043{
44}
45
Sean Callanan771131d2010-09-30 21:18:25 +000046/* Handy utility functions used at several places in the code */
Sean Callanana48fe162010-08-11 03:57:18 +000047
48static std::string
Sean Callanan771131d2010-09-30 21:18:25 +000049PrintValue(const Value *V, bool truncate = false)
Sean Callanana48fe162010-08-11 03:57:18 +000050{
51 std::string s;
52 raw_string_ostream rso(s);
53 V->print(rso);
54 rso.flush();
55 if (truncate)
56 s.resize(s.length() - 1);
57 return s;
58}
59
Sean Callanan771131d2010-09-30 21:18:25 +000060static std::string
61PrintType(const Type *T, bool truncate = false)
62{
63 std::string s;
64 raw_string_ostream rso(s);
65 T->print(rso);
66 rso.flush();
67 if (truncate)
68 s.resize(s.length() - 1);
69 return s;
70}
71
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000072IRForTarget::~IRForTarget()
73{
74}
75
Sean Callanan82b74c82010-08-12 01:56:52 +000076bool
Greg Clayton8de27c72010-10-15 22:48:33 +000077IRForTarget::createResultVariable (llvm::Module &llvm_module, llvm::Function &llvm_function)
Sean Callanan82b74c82010-08-12 01:56:52 +000078{
79 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
80
Sean Callanane8a59a82010-09-13 21:34:21 +000081 if (!m_resolve_vars)
82 return true;
83
84 // Find the result variable. If it doesn't exist, we can give up right here.
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000085
Greg Clayton8de27c72010-10-15 22:48:33 +000086 ValueSymbolTable& value_symbol_table = llvm_module.getValueSymbolTable();
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000087
88 const char *result_name = NULL;
89
90 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
91 vi != ve;
92 ++vi)
93 {
Greg Clayton8de27c72010-10-15 22:48:33 +000094 if (strstr(vi->first(), "$__lldb_expr_result") &&
Sean Callananc04743d2010-09-28 21:13:03 +000095 !strstr(vi->first(), "GV"))
96 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000097 result_name = vi->first();
Sean Callananc04743d2010-09-28 21:13:03 +000098 break;
99 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000100 }
101
102 if (!result_name)
103 {
104 if (log)
105 log->PutCString("Couldn't find result variable");
106
107 return true;
108 }
109
Sean Callananc04743d2010-09-28 21:13:03 +0000110 if (log)
111 log->Printf("Result name: %s", result_name);
112
Greg Clayton8de27c72010-10-15 22:48:33 +0000113 Value *result_value = llvm_module.getNamedValue(result_name);
Sean Callanan82b74c82010-08-12 01:56:52 +0000114
115 if (!result_value)
116 {
117 if (log)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000118 log->PutCString("Result variable had no data");
Sean Callanane8a59a82010-09-13 21:34:21 +0000119
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000120 return false;
Sean Callanan82b74c82010-08-12 01:56:52 +0000121 }
Sean Callanane8a59a82010-09-13 21:34:21 +0000122
Sean Callanan82b74c82010-08-12 01:56:52 +0000123 if (log)
124 log->Printf("Found result in the IR: %s", PrintValue(result_value, false).c_str());
125
126 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
127
128 if (!result_global)
129 {
130 if (log)
131 log->PutCString("Result variable isn't a GlobalVariable");
132 return false;
133 }
134
135 // Find the metadata and follow it to the VarDecl
136
Greg Clayton8de27c72010-10-15 22:48:33 +0000137 NamedMDNode *named_metadata = llvm_module.getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan82b74c82010-08-12 01:56:52 +0000138
139 if (!named_metadata)
140 {
141 if (log)
142 log->PutCString("No global metadata");
143
144 return false;
145 }
146
147 unsigned num_nodes = named_metadata->getNumOperands();
148 unsigned node_index;
149
150 MDNode *metadata_node = NULL;
151
152 for (node_index = 0;
153 node_index < num_nodes;
154 ++node_index)
155 {
156 metadata_node = named_metadata->getOperand(node_index);
157
158 if (metadata_node->getNumOperands() != 2)
159 continue;
160
161 if (metadata_node->getOperand(0) == result_global)
162 break;
163 }
164
165 if (!metadata_node)
166 {
167 if (log)
168 log->PutCString("Couldn't find result metadata");
169 return false;
170 }
171
172 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
173
174 uint64_t result_decl_intptr = constant_int->getZExtValue();
175
176 clang::VarDecl *result_decl = reinterpret_cast<clang::VarDecl *>(result_decl_intptr);
177
178 // Get the next available result name from m_decl_map and create the persistent
179 // variable for it
180
181 lldb_private::TypeFromParser result_decl_type (result_decl->getType().getAsOpaquePtr(),
182 &result_decl->getASTContext());
Greg Clayton8de27c72010-10-15 22:48:33 +0000183
184 lldb_private::ConstString new_result_name (m_decl_map->GetPersistentResultName());
185 m_decl_map->AddPersistentVariable(result_decl, new_result_name, result_decl_type);
Sean Callanan82b74c82010-08-12 01:56:52 +0000186
187 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000188 log->Printf("Creating a new result global: %s", new_result_name.GetCString());
Sean Callanan82b74c82010-08-12 01:56:52 +0000189
190 // Construct a new result global and set up its metadata
191
Greg Clayton8de27c72010-10-15 22:48:33 +0000192 GlobalVariable *new_result_global = new GlobalVariable(llvm_module,
Sean Callanan82b74c82010-08-12 01:56:52 +0000193 result_global->getType()->getElementType(),
194 false, /* not constant */
195 GlobalValue::ExternalLinkage,
196 NULL, /* no initializer */
Greg Clayton8de27c72010-10-15 22:48:33 +0000197 new_result_name.GetCString ());
Sean Callanan82b74c82010-08-12 01:56:52 +0000198
199 // It's too late in compilation to create a new VarDecl for this, but we don't
200 // need to. We point the metadata at the old VarDecl. This creates an odd
201 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton8de27c72010-10-15 22:48:33 +0000202 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanan82b74c82010-08-12 01:56:52 +0000203 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
204 // fixed up.
205
206 ConstantInt *new_constant_int = ConstantInt::get(constant_int->getType(),
207 result_decl_intptr,
208 false);
209
210 llvm::Value* values[2];
211 values[0] = new_result_global;
212 values[1] = new_constant_int;
213
Greg Clayton8de27c72010-10-15 22:48:33 +0000214 MDNode *persistent_global_md = MDNode::get(llvm_module.getContext(), values, 2);
Sean Callanan82b74c82010-08-12 01:56:52 +0000215 named_metadata->addOperand(persistent_global_md);
216
217 if (log)
Sean Callanan2e2db532010-09-07 22:43:19 +0000218 log->Printf("Replacing %s with %s",
219 PrintValue(result_global).c_str(),
Sean Callanan82b74c82010-08-12 01:56:52 +0000220 PrintValue(new_result_global).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000221
222 if (result_global->hasNUses(0))
223 {
224 // We need to synthesize a store for this variable, because otherwise
225 // there's nothing to put into its equivalent persistent variable.
Sean Callanan82b74c82010-08-12 01:56:52 +0000226
Greg Clayton8de27c72010-10-15 22:48:33 +0000227 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan2e2db532010-09-07 22:43:19 +0000228 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
229
230 if (!first_entry_instruction)
231 return false;
232
233 if (!result_global->hasInitializer())
234 {
235 if (log)
236 log->Printf("Couldn't find initializer for unused variable");
237 return false;
238 }
239
240 Constant *initializer = result_global->getInitializer();
241
242 StoreInst *synthesized_store = new StoreInst::StoreInst(initializer,
243 new_result_global,
244 first_entry_instruction);
245
246 if (log)
247 log->Printf("Synthesized result store %s\n", PrintValue(synthesized_store).c_str());
248 }
249 else
250 {
251 result_global->replaceAllUsesWith(new_result_global);
252 }
253
Sean Callanan82b74c82010-08-12 01:56:52 +0000254 result_global->eraseFromParent();
255
256 return true;
257}
258
Sean Callananf5857a02010-07-31 01:32:05 +0000259static bool isObjCSelectorRef(Value *V)
260{
261 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
262
263 if (!GV || !GV->hasName() || !GV->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
264 return false;
265
266 return true;
267}
268
269bool
270IRForTarget::RewriteObjCSelector(Instruction* selector_load,
271 Module &M)
272{
273 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
274
275 LoadInst *load = dyn_cast<LoadInst>(selector_load);
276
277 if (!load)
278 return false;
279
280 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
281 //
282 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
283 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
284 //
285 // where %obj is the object pointer and %tmp is the selector.
286 //
287 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_METH_VAR_NAME_".
288 // @"\01L_OBJC_METH_VAR_NAME_" contains the string.
289
290 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
291
292 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
293
294 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
295 return false;
296
297 Constant *osr_initializer = _objc_selector_references_->getInitializer();
298
299 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
300
301 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
302 return false;
303
304 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
305
306 if (!osr_initializer_base)
307 return false;
308
309 // Find the string's initializer (a ConstantArray) and get the string from it
310
311 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
312
313 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
314 return false;
315
316 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
317
318 ConstantArray *omvn_initializer_array = dyn_cast<ConstantArray>(omvn_initializer);
319
320 if (!omvn_initializer_array->isString())
321 return false;
322
323 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
324
325 if (log)
326 log->Printf("Found Objective-C selector reference %s", omvn_initializer_string.c_str());
327
328 // Construct a call to sel_registerName
329
330 if (!m_sel_registerName)
331 {
332 uint64_t srN_addr;
333
Greg Clayton8de27c72010-10-15 22:48:33 +0000334 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
335 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, srN_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000336 return false;
337
338 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
339
340 // The below code would be "more correct," but in actuality what's required is uint8_t*
341 //Type *sel_type = StructType::get(M.getContext());
342 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
343 const Type *sel_ptr_type = Type::getInt8PtrTy(M.getContext());
344
345 std::vector <const Type *> srN_arg_types;
346 srN_arg_types.push_back(Type::getInt8PtrTy(M.getContext()));
347 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
348
349 // Build the constant containing the pointer to the function
350 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
351 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
352 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
353 Constant *srN_addr_int = ConstantInt::get(intptr_ty, srN_addr, false);
354 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
355 }
356
357 SmallVector <Value*, 1> srN_arguments;
358
359 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(M.getContext()));
360
361 srN_arguments.push_back(omvn_pointer);
362
363 CallInst *srN_call = CallInst::Create(m_sel_registerName,
364 srN_arguments.begin(),
365 srN_arguments.end(),
366 "srN",
367 selector_load);
368
369 // Replace the load with the call in all users
370
371 selector_load->replaceAllUsesWith(srN_call);
372
373 selector_load->eraseFromParent();
374
375 return true;
376}
377
378bool
379IRForTarget::rewriteObjCSelectors(Module &M,
380 BasicBlock &BB)
381{
382 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
383
384 BasicBlock::iterator ii;
385
386 typedef SmallVector <Instruction*, 2> InstrList;
387 typedef InstrList::iterator InstrIterator;
388
389 InstrList selector_loads;
390
391 for (ii = BB.begin();
392 ii != BB.end();
393 ++ii)
394 {
395 Instruction &inst = *ii;
396
397 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
398 if (isObjCSelectorRef(load->getPointerOperand()))
399 selector_loads.push_back(&inst);
400 }
401
402 InstrIterator iter;
403
404 for (iter = selector_loads.begin();
405 iter != selector_loads.end();
406 ++iter)
407 {
408 if (!RewriteObjCSelector(*iter, M))
409 {
410 if(log)
411 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
412 return false;
413 }
414 }
415
416 return true;
417}
418
Sean Callanana48fe162010-08-11 03:57:18 +0000419bool
420IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc,
Greg Clayton8de27c72010-10-15 22:48:33 +0000421 llvm::Module &llvm_module)
Sean Callanana48fe162010-08-11 03:57:18 +0000422{
423 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
424
425 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
426
427 if (!alloc_md || !alloc_md->getNumOperands())
428 return false;
429
430 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
431
432 if (!constant_int)
433 return false;
434
435 // We attempt to register this as a new persistent variable with the DeclMap.
436
437 uintptr_t ptr = constant_int->getZExtValue();
438
Sean Callanan82b74c82010-08-12 01:56:52 +0000439 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanana48fe162010-08-11 03:57:18 +0000440
Sean Callanan82b74c82010-08-12 01:56:52 +0000441 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
442 &decl->getASTContext());
443
Greg Clayton8de27c72010-10-15 22:48:33 +0000444 StringRef decl_name (decl->getName());
445 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
446 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type))
Sean Callanana48fe162010-08-11 03:57:18 +0000447 return false;
448
Greg Clayton8de27c72010-10-15 22:48:33 +0000449 GlobalVariable *persistent_global = new GlobalVariable(llvm_module,
Sean Callanana48fe162010-08-11 03:57:18 +0000450 alloc->getType()->getElementType(),
451 false, /* not constant */
452 GlobalValue::ExternalLinkage,
453 NULL, /* no initializer */
454 alloc->getName().str().c_str());
455
456 // What we're going to do here is make believe this was a regular old external
457 // variable. That means we need to make the metadata valid.
458
Greg Clayton8de27c72010-10-15 22:48:33 +0000459 NamedMDNode *named_metadata = llvm_module.getNamedMetadata("clang.global.decl.ptrs");
Sean Callanana48fe162010-08-11 03:57:18 +0000460
461 llvm::Value* values[2];
462 values[0] = persistent_global;
463 values[1] = constant_int;
464
Greg Clayton8de27c72010-10-15 22:48:33 +0000465 MDNode *persistent_global_md = MDNode::get(llvm_module.getContext(), values, 2);
Sean Callanana48fe162010-08-11 03:57:18 +0000466 named_metadata->addOperand(persistent_global_md);
467
468 alloc->replaceAllUsesWith(persistent_global);
469 alloc->eraseFromParent();
470
471 return true;
472}
473
474bool
475IRForTarget::rewritePersistentAllocs(llvm::Module &M,
476 llvm::BasicBlock &BB)
477{
Sean Callanane8a59a82010-09-13 21:34:21 +0000478 if (!m_resolve_vars)
479 return true;
480
Sean Callanana48fe162010-08-11 03:57:18 +0000481 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
482
483 BasicBlock::iterator ii;
484
485 typedef SmallVector <Instruction*, 2> InstrList;
486 typedef InstrList::iterator InstrIterator;
487
488 InstrList pvar_allocs;
489
490 for (ii = BB.begin();
491 ii != BB.end();
492 ++ii)
493 {
494 Instruction &inst = *ii;
495
496 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callanan03c997b2010-10-21 22:41:32 +0000497 if (alloc->getName().startswith("$") &&
498 !alloc->getName().startswith("$__lldb"))
Sean Callanana48fe162010-08-11 03:57:18 +0000499 pvar_allocs.push_back(alloc);
500 }
501
502 InstrIterator iter;
503
504 for (iter = pvar_allocs.begin();
505 iter != pvar_allocs.end();
506 ++iter)
507 {
508 if (!RewritePersistentAlloc(*iter, M))
509 {
510 if(log)
511 log->PutCString("Couldn't rewrite the creation of a persistent variable");
512 return false;
513 }
514 }
515
516 return true;
517}
518
Sean Callanan8bce6652010-07-13 21:41:46 +0000519static clang::NamedDecl *
Sean Callanan02fbafa2010-07-27 21:39:39 +0000520DeclForGlobalValue(Module &module,
521 GlobalValue *global_value)
Sean Callanan8bce6652010-07-13 21:41:46 +0000522{
523 NamedMDNode *named_metadata = module.getNamedMetadata("clang.global.decl.ptrs");
524
525 if (!named_metadata)
526 return NULL;
527
528 unsigned num_nodes = named_metadata->getNumOperands();
529 unsigned node_index;
530
531 for (node_index = 0;
532 node_index < num_nodes;
533 ++node_index)
534 {
535 MDNode *metadata_node = named_metadata->getOperand(node_index);
536
537 if (!metadata_node)
538 return NULL;
539
540 if (metadata_node->getNumOperands() != 2)
Sean Callanana48fe162010-08-11 03:57:18 +0000541 continue;
Sean Callanan8bce6652010-07-13 21:41:46 +0000542
543 if (metadata_node->getOperand(0) != global_value)
544 continue;
545
546 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
547
548 if (!constant_int)
549 return NULL;
550
551 uintptr_t ptr = constant_int->getZExtValue();
552
553 return reinterpret_cast<clang::NamedDecl *>(ptr);
554 }
555
556 return NULL;
557}
558
559bool
Greg Clayton8de27c72010-10-15 22:48:33 +0000560IRForTarget::MaybeHandleVariable
561(
562 Module &llvm_module,
563 Value *llvm_value_ptr,
564 bool Store
565)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000566{
Sean Callananf5857a02010-07-31 01:32:05 +0000567 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
568
Greg Clayton8de27c72010-10-15 22:48:33 +0000569 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callananbc2928a2010-08-03 00:23:29 +0000570 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000571 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +0000572 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000573 default:
574 break;
575 case Instruction::GetElementPtr:
576 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +0000577 Value *s = constant_expr->getOperand(0);
Greg Clayton8de27c72010-10-15 22:48:33 +0000578 MaybeHandleVariable(llvm_module, s, Store);
Sean Callananbc2928a2010-08-03 00:23:29 +0000579 }
580 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000581 if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000582 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000583 clang::NamedDecl *named_decl = DeclForGlobalValue(llvm_module, global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000584
Sean Callananf5857a02010-07-31 01:32:05 +0000585 if (!named_decl)
586 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000587 if (isObjCSelectorRef(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000588 return true;
589
590 if (log)
591 log->Printf("Found global variable %s without metadata", global_variable->getName().str().c_str());
592 return false;
593 }
594
Greg Clayton8de27c72010-10-15 22:48:33 +0000595 std::string name (named_decl->getName().str());
Sean Callanan810f22d2010-07-16 00:09:46 +0000596
Sean Callanan771131d2010-09-30 21:18:25 +0000597 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +0000598 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000599
600 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +0000601 {
Sean Callanan771131d2010-09-30 21:18:25 +0000602 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +0000603 ast_context = &value_decl->getASTContext();
604 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000605 else
Sean Callananf328c9f2010-07-20 23:31:16 +0000606 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000607 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +0000608 }
Sean Callanan771131d2010-09-30 21:18:25 +0000609
610 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_type));
Sean Callananf328c9f2010-07-20 23:31:16 +0000611
Sean Callanan02fbafa2010-07-27 21:39:39 +0000612 const Type *value_type = global_variable->getType();
Sean Callanan771131d2010-09-30 21:18:25 +0000613
614 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
615 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
616
617 if (log)
618 log->Printf("Type of %s is [clang %s, lldb %s] [size %d, align %d]",
619 name.c_str(),
620 qual_type.getAsString().c_str(),
621 PrintType(value_type).c_str(),
622 value_size,
623 value_alignment);
624
Sean Callanan8bce6652010-07-13 21:41:46 +0000625
Sean Callanan8c127202010-08-23 23:09:38 +0000626 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton8de27c72010-10-15 22:48:33 +0000627 lldb_private::ConstString (name.c_str()),
628 llvm_value_ptr,
Sean Callananba992c52010-07-27 02:07:53 +0000629 value_size,
630 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +0000631 return false;
632 }
633
634 return true;
635}
636
637bool
Sean Callanandc27aba2010-10-05 22:26:43 +0000638IRForTarget::MaybeHandleCallArguments(Module &M,
639 CallInst *C)
640{
641 // lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
642
643 for (unsigned op_index = 0, num_ops = C->getNumArgOperands();
644 op_index < num_ops;
645 ++op_index)
646 if (!MaybeHandleVariable(M, C->getArgOperand(op_index), true)) // conservatively believe that this is a store
647 return false;
648
649 return true;
650}
651
652bool
Greg Clayton8de27c72010-10-15 22:48:33 +0000653IRForTarget::MaybeHandleCall(Module &llvm_module,
654 CallInst *llvm_call_inst)
Sean Callananba992c52010-07-27 02:07:53 +0000655{
656 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
657
Greg Clayton8de27c72010-10-15 22:48:33 +0000658 Function *fun = llvm_call_inst->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +0000659
660 if (fun == NULL)
Sean Callanan65af7342010-09-08 20:04:08 +0000661 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000662 Value *val = llvm_call_inst->getCalledValue();
Sean Callanan65af7342010-09-08 20:04:08 +0000663
664 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(val);
665
666 if (const_expr && const_expr->getOpcode() == Instruction::BitCast)
667 {
668 fun = dyn_cast<Function>(const_expr->getOperand(0));
669
670 if (!fun)
671 return true;
672 }
673 else
674 {
675 return true;
676 }
677 }
Sean Callananba992c52010-07-27 02:07:53 +0000678
Greg Clayton8de27c72010-10-15 22:48:33 +0000679 lldb_private::ConstString str;
Sean Callananc04743d2010-09-28 21:13:03 +0000680
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000681 if (fun->isIntrinsic())
Sean Callananc04743d2010-09-28 21:13:03 +0000682 {
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000683 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sean Callananc04743d2010-09-28 21:13:03 +0000684
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000685 switch (intrinsic_id)
686 {
687 default:
688 if (log)
689 log->Printf("Unresolved intrinsic %s", Intrinsic::getName(intrinsic_id).c_str());
690 return false;
691 case Intrinsic::memcpy:
Greg Clayton8de27c72010-10-15 22:48:33 +0000692 {
693 static lldb_private::ConstString g_memcpy_str ("memcpy");
694 str = g_memcpy_str;
695 }
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000696 break;
697 }
Sean Callananc04743d2010-09-28 21:13:03 +0000698
Greg Clayton8de27c72010-10-15 22:48:33 +0000699 if (log && str)
700 log->Printf("Resolved intrinsic name %s", str.GetCString());
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000701 }
702 else
703 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000704 str.SetCStringWithLength (fun->getName().data(), fun->getName().size());
Sean Callananc04743d2010-09-28 21:13:03 +0000705 }
706
Greg Clayton8de27c72010-10-15 22:48:33 +0000707 clang::NamedDecl *fun_decl = DeclForGlobalValue (llvm_module, fun);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000708 uint64_t fun_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000709 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +0000710
Sean Callananf5857a02010-07-31 01:32:05 +0000711 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +0000712 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000713 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_value_ptr, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000714 {
Sean Callanan92aa6662010-09-07 21:49:41 +0000715 fun_value_ptr = NULL;
716
Greg Clayton8de27c72010-10-15 22:48:33 +0000717 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callanan92aa6662010-09-07 21:49:41 +0000718 {
719 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000720 log->Printf("Function %s had no address", str.GetCString());
Sean Callanan92aa6662010-09-07 21:49:41 +0000721
722 return false;
723 }
Sean Callananf5857a02010-07-31 01:32:05 +0000724 }
725 }
726 else
727 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000728 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000729 {
730 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000731 log->Printf ("Metadataless function %s had no address", str.GetCString());
Sean Callananf5857a02010-07-31 01:32:05 +0000732 }
Sean Callananba992c52010-07-27 02:07:53 +0000733 }
734
735 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000736 log->Printf("Found %s at %llx", str.GetCString(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +0000737
Sean Callananf5857a02010-07-31 01:32:05 +0000738 Value *fun_addr_ptr;
739
740 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +0000741 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000742 const IntegerType *intptr_ty = Type::getIntNTy(llvm_module.getContext(),
743 (llvm_module.getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan92aa6662010-09-07 21:49:41 +0000744 const FunctionType *fun_ty = fun->getFunctionType();
Sean Callanan02fbafa2010-07-27 21:39:39 +0000745 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
746 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000747 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
748
749 if (fun_value_ptr)
750 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000751 }
Sean Callananf5857a02010-07-31 01:32:05 +0000752
753 if (fun_value_ptr)
754 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000755
Greg Clayton8de27c72010-10-15 22:48:33 +0000756 llvm_call_inst->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000757
Greg Clayton8de27c72010-10-15 22:48:33 +0000758 ConstantArray *func_name = (ConstantArray*)ConstantArray::get(llvm_module.getContext(), str.GetCString());
Sean Callanane8a59a82010-09-13 21:34:21 +0000759
760 Value *values[1];
761 values[0] = func_name;
Greg Clayton8de27c72010-10-15 22:48:33 +0000762 MDNode *func_metadata = MDNode::get(llvm_module.getContext(), values, 1);
Sean Callanane8a59a82010-09-13 21:34:21 +0000763
Greg Clayton8de27c72010-10-15 22:48:33 +0000764 llvm_call_inst->setMetadata("lldb.call.realName", func_metadata);
Sean Callanane8a59a82010-09-13 21:34:21 +0000765
766 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000767 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 +0000768
Sean Callananba992c52010-07-27 02:07:53 +0000769 return true;
770}
771
772bool
Sean Callananf5857a02010-07-31 01:32:05 +0000773IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
Sean Callanan8bce6652010-07-13 21:41:46 +0000774{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000775 /////////////////////////////////////////////////////////////////////////
776 // Prepare the current basic block for execution in the remote process
777 //
778
Sean Callanan02fbafa2010-07-27 21:39:39 +0000779 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +0000780
781 for (ii = BB.begin();
782 ii != BB.end();
783 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000784 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000785 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000786
Sean Callanane8a59a82010-09-13 21:34:21 +0000787 if (m_resolve_vars)
788 {
789 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
790 if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
791 return false;
Sean Callananf5857a02010-07-31 01:32:05 +0000792
Sean Callanane8a59a82010-09-13 21:34:21 +0000793 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
794 if (!MaybeHandleVariable(M, store->getPointerOperand(), true))
795 return false;
796 }
Sean Callananba992c52010-07-27 02:07:53 +0000797
798 if (CallInst *call = dyn_cast<CallInst>(&inst))
Sean Callanandc27aba2010-10-05 22:26:43 +0000799 {
800 if (!MaybeHandleCallArguments(M, call))
801 return false;
802
Sean Callananba992c52010-07-27 02:07:53 +0000803 if (!MaybeHandleCall(M, call))
Sean Callanan8bce6652010-07-13 21:41:46 +0000804 return false;
Sean Callanandc27aba2010-10-05 22:26:43 +0000805 }
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000806 }
807
808 return true;
809}
810
Sean Callanan02fbafa2010-07-27 21:39:39 +0000811static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +0000812{
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000813 Constant *C;
Sean Callanan45839272010-07-24 01:37:44 +0000814
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000815 if (!(C = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +0000816 return false;
817
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000818 ConstantExpr *CE;
819
820 if ((CE = dyn_cast<ConstantExpr>(V)))
821 {
822 if (CE->getOpcode() != Instruction::BitCast)
823 return false;
824
825 C = CE->getOperand(0);
826 }
827
828 GlobalVariable *GV = dyn_cast<GlobalVariable>(C);
Sean Callanan45839272010-07-24 01:37:44 +0000829
830 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
831 return false;
832
833 return true;
834}
835
836static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &M)
837{
838 Constant* zero(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0, true));
839
840 Value::use_iterator ui;
841
842 for (ui = guard_load->use_begin();
843 ui != guard_load->use_end();
844 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +0000845 {
Greg Clayton6e713402010-07-30 20:30:44 +0000846 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +0000847 {
848 // do nothing for the moment
849 }
850 else
851 {
852 ui->replaceUsesOfWith(guard_load, zero);
853 }
854 }
Sean Callanan45839272010-07-24 01:37:44 +0000855
856 guard_load->eraseFromParent();
857}
858
859static void ExciseGuardStore(Instruction* guard_store)
860{
861 guard_store->eraseFromParent();
862}
863
864bool
865IRForTarget::removeGuards(Module &M, BasicBlock &BB)
866{
867 ///////////////////////////////////////////////////////
868 // Eliminate any reference to guard variables found.
869 //
870
Sean Callanan02fbafa2010-07-27 21:39:39 +0000871 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +0000872
Sean Callanan02fbafa2010-07-27 21:39:39 +0000873 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +0000874 typedef InstrList::iterator InstrIterator;
875
876 InstrList guard_loads;
877 InstrList guard_stores;
878
879 for (ii = BB.begin();
880 ii != BB.end();
881 ++ii)
882 {
883 Instruction &inst = *ii;
884
885 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
886 if (isGuardVariableRef(load->getPointerOperand()))
887 guard_loads.push_back(&inst);
888
889 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
890 if (isGuardVariableRef(store->getPointerOperand()))
891 guard_stores.push_back(&inst);
892 }
893
894 InstrIterator iter;
895
896 for (iter = guard_loads.begin();
897 iter != guard_loads.end();
898 ++iter)
899 TurnGuardLoadIntoZero(*iter, M);
900
901 for (iter = guard_stores.begin();
902 iter != guard_stores.end();
903 ++iter)
904 ExciseGuardStore(*iter);
905
906 return true;
907}
908
Sean Callananbafd6852010-07-14 23:40:29 +0000909// UnfoldConstant operates on a constant [C] which has just been replaced with a value
910// [new_value]. We assume that new_value has been properly placed early in the function,
911// most likely somewhere in front of the first instruction in the entry basic block
912// [first_entry_instruction].
913//
914// UnfoldConstant reads through the uses of C and replaces C in those uses with new_value.
915// Where those uses are constants, the function generates new instructions to compute the
916// result of the new, non-constant expression and places them before first_entry_instruction.
917// These instructions replace the constant uses, so UnfoldConstant calls itself recursively
918// for those.
919
920static bool
Sean Callanan02fbafa2010-07-27 21:39:39 +0000921UnfoldConstant(Constant *C, Value *new_value, Instruction *first_entry_instruction)
Sean Callananbafd6852010-07-14 23:40:29 +0000922{
923 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
924
925 Value::use_iterator ui;
926
Sean Callanana48fe162010-08-11 03:57:18 +0000927 SmallVector<User*, 16> users;
928
929 // We do this because the use list might change, invalidating our iterator.
930 // Much better to keep a work list ourselves.
Sean Callananbafd6852010-07-14 23:40:29 +0000931 for (ui = C->use_begin();
932 ui != C->use_end();
933 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +0000934 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +0000935
Sean Callanana48fe162010-08-11 03:57:18 +0000936 for (int i = 0;
937 i < users.size();
938 ++i)
939 {
940 User *user = users[i];
941
Sean Callananbafd6852010-07-14 23:40:29 +0000942 if (Constant *constant = dyn_cast<Constant>(user))
943 {
944 // synthesize a new non-constant equivalent of the constant
945
946 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
947 {
948 switch (constant_expr->getOpcode())
949 {
950 default:
951 if (log)
952 log->Printf("Unhandled constant expression type: %s", PrintValue(constant_expr).c_str());
953 return false;
954 case Instruction::BitCast:
955 {
956 // UnaryExpr
957 // OperandList[0] is value
958
959 Value *s = constant_expr->getOperand(0);
960
961 if (s == C)
962 s = new_value;
963
964 BitCastInst *bit_cast(new BitCastInst(s, C->getType(), "", first_entry_instruction));
965
966 UnfoldConstant(constant_expr, bit_cast, first_entry_instruction);
967 }
968 break;
969 case Instruction::GetElementPtr:
970 {
971 // GetElementPtrConstantExpr
972 // OperandList[0] is base
973 // OperandList[1]... are indices
974
975 Value *ptr = constant_expr->getOperand(0);
976
977 if (ptr == C)
978 ptr = new_value;
979
980 SmallVector<Value*, 16> indices;
981
982 unsigned operand_index;
983 unsigned num_operands = constant_expr->getNumOperands();
984
985 for (operand_index = 1;
986 operand_index < num_operands;
987 ++operand_index)
988 {
989 Value *operand = constant_expr->getOperand(operand_index);
990
991 if (operand == C)
992 operand = new_value;
993
994 indices.push_back(operand);
995 }
996
997 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_instruction));
998
999 UnfoldConstant(constant_expr, get_element_ptr, first_entry_instruction);
1000 }
1001 break;
1002 }
1003 }
1004 else
1005 {
1006 if (log)
1007 log->Printf("Unhandled constant type: %s", PrintValue(constant).c_str());
1008 return false;
1009 }
1010 }
1011 else
1012 {
1013 // simple fall-through case for non-constants
1014 user->replaceUsesOfWith(C, new_value);
1015 }
1016 }
1017
1018 return true;
1019}
1020
Sean Callanan8bce6652010-07-13 21:41:46 +00001021bool
Sean Callananf5857a02010-07-31 01:32:05 +00001022IRForTarget::replaceVariables(Module &M, Function &F)
Sean Callanan8bce6652010-07-13 21:41:46 +00001023{
Sean Callanane8a59a82010-09-13 21:34:21 +00001024 if (!m_resolve_vars)
1025 return true;
1026
Sean Callanan8bce6652010-07-13 21:41:46 +00001027 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
1028
1029 m_decl_map->DoStructLayout();
1030
1031 if (log)
1032 log->Printf("Element arrangement:");
1033
1034 uint32_t num_elements;
1035 uint32_t element_index;
1036
1037 size_t size;
1038 off_t alignment;
1039
1040 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
1041 return false;
1042
Sean Callananf5857a02010-07-31 01:32:05 +00001043 Function::arg_iterator iter(F.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00001044
Sean Callananf5857a02010-07-31 01:32:05 +00001045 if (iter == F.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +00001046 return false;
1047
Sean Callanan02fbafa2010-07-27 21:39:39 +00001048 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00001049
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001050 if (argument->getName().equals("this"))
1051 {
1052 ++iter;
1053
1054 if (iter == F.getArgumentList().end())
1055 return false;
1056
1057 argument = iter;
1058 }
1059
Greg Clayton8de27c72010-10-15 22:48:33 +00001060 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan8bce6652010-07-13 21:41:46 +00001061 return false;
1062
1063 if (log)
1064 log->Printf("Arg: %s", PrintValue(argument).c_str());
1065
Sean Callananf5857a02010-07-31 01:32:05 +00001066 BasicBlock &entry_block(F.getEntryBlock());
Sean Callanan02fbafa2010-07-27 21:39:39 +00001067 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00001068
1069 if (!first_entry_instruction)
1070 return false;
1071
1072 LLVMContext &context(M.getContext());
1073 const IntegerType *offset_type(Type::getInt32Ty(context));
1074
1075 if (!offset_type)
1076 return false;
1077
1078 for (element_index = 0; element_index < num_elements; ++element_index)
1079 {
1080 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001081 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +00001082 off_t offset;
Greg Clayton8de27c72010-10-15 22:48:33 +00001083 lldb_private::ConstString name;
Sean Callanan8bce6652010-07-13 21:41:46 +00001084
Sean Callanan45690fe2010-08-30 22:17:16 +00001085 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan8bce6652010-07-13 21:41:46 +00001086 return false;
1087
1088 if (log)
Sean Callanan45690fe2010-08-30 22:17:16 +00001089 log->Printf(" %s [%s] (%s) placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00001090 value->getName().str().c_str(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001091 name.GetCString(),
Sean Callanan8bce6652010-07-13 21:41:46 +00001092 PrintValue(value, true).c_str(),
1093 offset);
1094
1095 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
1096 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", first_entry_instruction);
1097 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", first_entry_instruction);
1098
Sean Callananbafd6852010-07-14 23:40:29 +00001099 if (Constant *constant = dyn_cast<Constant>(value))
1100 UnfoldConstant(constant, bit_cast, first_entry_instruction);
1101 else
1102 value->replaceAllUsesWith(bit_cast);
Sean Callanan8bce6652010-07-13 21:41:46 +00001103 }
1104
1105 if (log)
1106 log->Printf("Total structure [align %d, size %d]", alignment, size);
1107
1108 return true;
1109}
1110
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001111bool
1112IRForTarget::runOnModule(Module &M)
1113{
1114 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
1115
Sean Callanan65dafa82010-08-27 01:01:44 +00001116 Function* function = M.getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001117
1118 if (!function)
1119 {
1120 if (log)
Sean Callanan65dafa82010-08-27 01:01:44 +00001121 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001122
1123 return false;
1124 }
1125
Sean Callanan02fbafa2010-07-27 21:39:39 +00001126 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001127
Sean Callanan82b74c82010-08-12 01:56:52 +00001128 ////////////////////////////////////////////////////////////
Greg Clayton8de27c72010-10-15 22:48:33 +00001129 // Replace $__lldb_expr_result with a persistent variable
Sean Callanan82b74c82010-08-12 01:56:52 +00001130 //
1131
1132 if (!createResultVariable(M, *function))
1133 return false;
1134
Sean Callananf5857a02010-07-31 01:32:05 +00001135 //////////////////////////////////
1136 // Run basic-block level passes
1137 //
1138
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001139 for (bbi = function->begin();
1140 bbi != function->end();
1141 ++bbi)
1142 {
Sean Callanan8c127202010-08-23 23:09:38 +00001143 if (!removeGuards(M, *bbi))
1144 return false;
1145
Sean Callanana48fe162010-08-11 03:57:18 +00001146 if (!rewritePersistentAllocs(M, *bbi))
Sean Callananf5857a02010-07-31 01:32:05 +00001147 return false;
1148
Sean Callanana48fe162010-08-11 03:57:18 +00001149 if (!rewriteObjCSelectors(M, *bbi))
1150 return false;
1151
Sean Callananf5857a02010-07-31 01:32:05 +00001152 if (!resolveExternals(M, *bbi))
Sean Callanan8bce6652010-07-13 21:41:46 +00001153 return false;
1154 }
1155
Sean Callanan771131d2010-09-30 21:18:25 +00001156 ///////////////////////////////
1157 // Run function-level passes
1158 //
1159
1160 if (!replaceVariables(M, *function))
1161 return false;
1162
Sean Callanan8bce6652010-07-13 21:41:46 +00001163 if (log)
1164 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00001165 std::string s;
1166 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00001167
Sean Callanan321fe9e2010-07-28 01:00:59 +00001168 M.print(oss, NULL);
1169
1170 oss.flush();
1171
1172 log->Printf("Module after preparing for execution: \n%s", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001173 }
1174
1175 return true;
1176}
1177
1178void
1179IRForTarget::assignPassManager(PMStack &PMS,
Sean Callanan8bce6652010-07-13 21:41:46 +00001180 PassManagerType T)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001181{
1182}
1183
1184PassManagerType
1185IRForTarget::getPotentialPassManagerType() const
1186{
1187 return PMT_ModulePassManager;
1188}