blob: 032f22f86fbae27c6dacbbfab4877a233092df81 [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))
497 if (alloc->getName().startswith("$"))
498 pvar_allocs.push_back(alloc);
499 }
500
501 InstrIterator iter;
502
503 for (iter = pvar_allocs.begin();
504 iter != pvar_allocs.end();
505 ++iter)
506 {
507 if (!RewritePersistentAlloc(*iter, M))
508 {
509 if(log)
510 log->PutCString("Couldn't rewrite the creation of a persistent variable");
511 return false;
512 }
513 }
514
515 return true;
516}
517
Sean Callanan8bce6652010-07-13 21:41:46 +0000518static clang::NamedDecl *
Sean Callanan02fbafa2010-07-27 21:39:39 +0000519DeclForGlobalValue(Module &module,
520 GlobalValue *global_value)
Sean Callanan8bce6652010-07-13 21:41:46 +0000521{
522 NamedMDNode *named_metadata = module.getNamedMetadata("clang.global.decl.ptrs");
523
524 if (!named_metadata)
525 return NULL;
526
527 unsigned num_nodes = named_metadata->getNumOperands();
528 unsigned node_index;
529
530 for (node_index = 0;
531 node_index < num_nodes;
532 ++node_index)
533 {
534 MDNode *metadata_node = named_metadata->getOperand(node_index);
535
536 if (!metadata_node)
537 return NULL;
538
539 if (metadata_node->getNumOperands() != 2)
Sean Callanana48fe162010-08-11 03:57:18 +0000540 continue;
Sean Callanan8bce6652010-07-13 21:41:46 +0000541
542 if (metadata_node->getOperand(0) != global_value)
543 continue;
544
545 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
546
547 if (!constant_int)
548 return NULL;
549
550 uintptr_t ptr = constant_int->getZExtValue();
551
552 return reinterpret_cast<clang::NamedDecl *>(ptr);
553 }
554
555 return NULL;
556}
557
558bool
Greg Clayton8de27c72010-10-15 22:48:33 +0000559IRForTarget::MaybeHandleVariable
560(
561 Module &llvm_module,
562 Value *llvm_value_ptr,
563 bool Store
564)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000565{
Sean Callananf5857a02010-07-31 01:32:05 +0000566 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
567
Greg Clayton8de27c72010-10-15 22:48:33 +0000568 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callananbc2928a2010-08-03 00:23:29 +0000569 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000570 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +0000571 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000572 default:
573 break;
574 case Instruction::GetElementPtr:
575 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +0000576 Value *s = constant_expr->getOperand(0);
Greg Clayton8de27c72010-10-15 22:48:33 +0000577 MaybeHandleVariable(llvm_module, s, Store);
Sean Callananbc2928a2010-08-03 00:23:29 +0000578 }
579 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000580 if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000581 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000582 clang::NamedDecl *named_decl = DeclForGlobalValue(llvm_module, global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000583
Sean Callananf5857a02010-07-31 01:32:05 +0000584 if (!named_decl)
585 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000586 if (isObjCSelectorRef(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000587 return true;
588
589 if (log)
590 log->Printf("Found global variable %s without metadata", global_variable->getName().str().c_str());
591 return false;
592 }
593
Greg Clayton8de27c72010-10-15 22:48:33 +0000594 std::string name (named_decl->getName().str());
Sean Callanan810f22d2010-07-16 00:09:46 +0000595
Sean Callanan771131d2010-09-30 21:18:25 +0000596 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +0000597 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000598
599 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +0000600 {
Sean Callanan771131d2010-09-30 21:18:25 +0000601 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +0000602 ast_context = &value_decl->getASTContext();
603 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000604 else
Sean Callananf328c9f2010-07-20 23:31:16 +0000605 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000606 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +0000607 }
Sean Callanan771131d2010-09-30 21:18:25 +0000608
609 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_type));
Sean Callananf328c9f2010-07-20 23:31:16 +0000610
Sean Callanan02fbafa2010-07-27 21:39:39 +0000611 const Type *value_type = global_variable->getType();
Sean Callanan771131d2010-09-30 21:18:25 +0000612
613 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
614 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
615
616 if (log)
617 log->Printf("Type of %s is [clang %s, lldb %s] [size %d, align %d]",
618 name.c_str(),
619 qual_type.getAsString().c_str(),
620 PrintType(value_type).c_str(),
621 value_size,
622 value_alignment);
623
Sean Callanan8bce6652010-07-13 21:41:46 +0000624
Sean Callanan8c127202010-08-23 23:09:38 +0000625 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton8de27c72010-10-15 22:48:33 +0000626 lldb_private::ConstString (name.c_str()),
627 llvm_value_ptr,
Sean Callananba992c52010-07-27 02:07:53 +0000628 value_size,
629 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +0000630 return false;
631 }
632
633 return true;
634}
635
636bool
Sean Callanandc27aba2010-10-05 22:26:43 +0000637IRForTarget::MaybeHandleCallArguments(Module &M,
638 CallInst *C)
639{
640 // lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
641
642 for (unsigned op_index = 0, num_ops = C->getNumArgOperands();
643 op_index < num_ops;
644 ++op_index)
645 if (!MaybeHandleVariable(M, C->getArgOperand(op_index), true)) // conservatively believe that this is a store
646 return false;
647
648 return true;
649}
650
651bool
Greg Clayton8de27c72010-10-15 22:48:33 +0000652IRForTarget::MaybeHandleCall(Module &llvm_module,
653 CallInst *llvm_call_inst)
Sean Callananba992c52010-07-27 02:07:53 +0000654{
655 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
656
Greg Clayton8de27c72010-10-15 22:48:33 +0000657 Function *fun = llvm_call_inst->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +0000658
659 if (fun == NULL)
Sean Callanan65af7342010-09-08 20:04:08 +0000660 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000661 Value *val = llvm_call_inst->getCalledValue();
Sean Callanan65af7342010-09-08 20:04:08 +0000662
663 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(val);
664
665 if (const_expr && const_expr->getOpcode() == Instruction::BitCast)
666 {
667 fun = dyn_cast<Function>(const_expr->getOperand(0));
668
669 if (!fun)
670 return true;
671 }
672 else
673 {
674 return true;
675 }
676 }
Sean Callananba992c52010-07-27 02:07:53 +0000677
Greg Clayton8de27c72010-10-15 22:48:33 +0000678 lldb_private::ConstString str;
Sean Callananc04743d2010-09-28 21:13:03 +0000679
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000680 if (fun->isIntrinsic())
Sean Callananc04743d2010-09-28 21:13:03 +0000681 {
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000682 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sean Callananc04743d2010-09-28 21:13:03 +0000683
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000684 switch (intrinsic_id)
685 {
686 default:
687 if (log)
688 log->Printf("Unresolved intrinsic %s", Intrinsic::getName(intrinsic_id).c_str());
689 return false;
690 case Intrinsic::memcpy:
Greg Clayton8de27c72010-10-15 22:48:33 +0000691 {
692 static lldb_private::ConstString g_memcpy_str ("memcpy");
693 str = g_memcpy_str;
694 }
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000695 break;
696 }
Sean Callananc04743d2010-09-28 21:13:03 +0000697
Greg Clayton8de27c72010-10-15 22:48:33 +0000698 if (log && str)
699 log->Printf("Resolved intrinsic name %s", str.GetCString());
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000700 }
701 else
702 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000703 str.SetCStringWithLength (fun->getName().data(), fun->getName().size());
Sean Callananc04743d2010-09-28 21:13:03 +0000704 }
705
Greg Clayton8de27c72010-10-15 22:48:33 +0000706 clang::NamedDecl *fun_decl = DeclForGlobalValue (llvm_module, fun);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000707 uint64_t fun_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000708 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +0000709
Sean Callananf5857a02010-07-31 01:32:05 +0000710 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +0000711 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000712 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_value_ptr, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000713 {
Sean Callanan92aa6662010-09-07 21:49:41 +0000714 fun_value_ptr = NULL;
715
Greg Clayton8de27c72010-10-15 22:48:33 +0000716 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callanan92aa6662010-09-07 21:49:41 +0000717 {
718 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000719 log->Printf("Function %s had no address", str.GetCString());
Sean Callanan92aa6662010-09-07 21:49:41 +0000720
721 return false;
722 }
Sean Callananf5857a02010-07-31 01:32:05 +0000723 }
724 }
725 else
726 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000727 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000728 {
729 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000730 log->Printf ("Metadataless function %s had no address", str.GetCString());
Sean Callananf5857a02010-07-31 01:32:05 +0000731 }
Sean Callananba992c52010-07-27 02:07:53 +0000732 }
733
734 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000735 log->Printf("Found %s at %llx", str.GetCString(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +0000736
Sean Callananf5857a02010-07-31 01:32:05 +0000737 Value *fun_addr_ptr;
738
739 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +0000740 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000741 const IntegerType *intptr_ty = Type::getIntNTy(llvm_module.getContext(),
742 (llvm_module.getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan92aa6662010-09-07 21:49:41 +0000743 const FunctionType *fun_ty = fun->getFunctionType();
Sean Callanan02fbafa2010-07-27 21:39:39 +0000744 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
745 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000746 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
747
748 if (fun_value_ptr)
749 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000750 }
Sean Callananf5857a02010-07-31 01:32:05 +0000751
752 if (fun_value_ptr)
753 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000754
Greg Clayton8de27c72010-10-15 22:48:33 +0000755 llvm_call_inst->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000756
Greg Clayton8de27c72010-10-15 22:48:33 +0000757 ConstantArray *func_name = (ConstantArray*)ConstantArray::get(llvm_module.getContext(), str.GetCString());
Sean Callanane8a59a82010-09-13 21:34:21 +0000758
759 Value *values[1];
760 values[0] = func_name;
Greg Clayton8de27c72010-10-15 22:48:33 +0000761 MDNode *func_metadata = MDNode::get(llvm_module.getContext(), values, 1);
Sean Callanane8a59a82010-09-13 21:34:21 +0000762
Greg Clayton8de27c72010-10-15 22:48:33 +0000763 llvm_call_inst->setMetadata("lldb.call.realName", func_metadata);
Sean Callanane8a59a82010-09-13 21:34:21 +0000764
765 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000766 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 +0000767
Sean Callananba992c52010-07-27 02:07:53 +0000768 return true;
769}
770
771bool
Sean Callananf5857a02010-07-31 01:32:05 +0000772IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
Sean Callanan8bce6652010-07-13 21:41:46 +0000773{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000774 /////////////////////////////////////////////////////////////////////////
775 // Prepare the current basic block for execution in the remote process
776 //
777
Sean Callanan02fbafa2010-07-27 21:39:39 +0000778 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +0000779
780 for (ii = BB.begin();
781 ii != BB.end();
782 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000783 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000784 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000785
Sean Callanane8a59a82010-09-13 21:34:21 +0000786 if (m_resolve_vars)
787 {
788 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
789 if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
790 return false;
Sean Callananf5857a02010-07-31 01:32:05 +0000791
Sean Callanane8a59a82010-09-13 21:34:21 +0000792 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
793 if (!MaybeHandleVariable(M, store->getPointerOperand(), true))
794 return false;
795 }
Sean Callananba992c52010-07-27 02:07:53 +0000796
797 if (CallInst *call = dyn_cast<CallInst>(&inst))
Sean Callanandc27aba2010-10-05 22:26:43 +0000798 {
799 if (!MaybeHandleCallArguments(M, call))
800 return false;
801
Sean Callananba992c52010-07-27 02:07:53 +0000802 if (!MaybeHandleCall(M, call))
Sean Callanan8bce6652010-07-13 21:41:46 +0000803 return false;
Sean Callanandc27aba2010-10-05 22:26:43 +0000804 }
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000805 }
806
807 return true;
808}
809
Sean Callanan02fbafa2010-07-27 21:39:39 +0000810static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +0000811{
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000812 Constant *C;
Sean Callanan45839272010-07-24 01:37:44 +0000813
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000814 if (!(C = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +0000815 return false;
816
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000817 ConstantExpr *CE;
818
819 if ((CE = dyn_cast<ConstantExpr>(V)))
820 {
821 if (CE->getOpcode() != Instruction::BitCast)
822 return false;
823
824 C = CE->getOperand(0);
825 }
826
827 GlobalVariable *GV = dyn_cast<GlobalVariable>(C);
Sean Callanan45839272010-07-24 01:37:44 +0000828
829 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
830 return false;
831
832 return true;
833}
834
835static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &M)
836{
837 Constant* zero(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0, true));
838
839 Value::use_iterator ui;
840
841 for (ui = guard_load->use_begin();
842 ui != guard_load->use_end();
843 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +0000844 {
Greg Clayton6e713402010-07-30 20:30:44 +0000845 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +0000846 {
847 // do nothing for the moment
848 }
849 else
850 {
851 ui->replaceUsesOfWith(guard_load, zero);
852 }
853 }
Sean Callanan45839272010-07-24 01:37:44 +0000854
855 guard_load->eraseFromParent();
856}
857
858static void ExciseGuardStore(Instruction* guard_store)
859{
860 guard_store->eraseFromParent();
861}
862
863bool
864IRForTarget::removeGuards(Module &M, BasicBlock &BB)
865{
866 ///////////////////////////////////////////////////////
867 // Eliminate any reference to guard variables found.
868 //
869
Sean Callanan02fbafa2010-07-27 21:39:39 +0000870 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +0000871
Sean Callanan02fbafa2010-07-27 21:39:39 +0000872 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +0000873 typedef InstrList::iterator InstrIterator;
874
875 InstrList guard_loads;
876 InstrList guard_stores;
877
878 for (ii = BB.begin();
879 ii != BB.end();
880 ++ii)
881 {
882 Instruction &inst = *ii;
883
884 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
885 if (isGuardVariableRef(load->getPointerOperand()))
886 guard_loads.push_back(&inst);
887
888 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
889 if (isGuardVariableRef(store->getPointerOperand()))
890 guard_stores.push_back(&inst);
891 }
892
893 InstrIterator iter;
894
895 for (iter = guard_loads.begin();
896 iter != guard_loads.end();
897 ++iter)
898 TurnGuardLoadIntoZero(*iter, M);
899
900 for (iter = guard_stores.begin();
901 iter != guard_stores.end();
902 ++iter)
903 ExciseGuardStore(*iter);
904
905 return true;
906}
907
Sean Callananbafd6852010-07-14 23:40:29 +0000908// UnfoldConstant operates on a constant [C] which has just been replaced with a value
909// [new_value]. We assume that new_value has been properly placed early in the function,
910// most likely somewhere in front of the first instruction in the entry basic block
911// [first_entry_instruction].
912//
913// UnfoldConstant reads through the uses of C and replaces C in those uses with new_value.
914// Where those uses are constants, the function generates new instructions to compute the
915// result of the new, non-constant expression and places them before first_entry_instruction.
916// These instructions replace the constant uses, so UnfoldConstant calls itself recursively
917// for those.
918
919static bool
Sean Callanan02fbafa2010-07-27 21:39:39 +0000920UnfoldConstant(Constant *C, Value *new_value, Instruction *first_entry_instruction)
Sean Callananbafd6852010-07-14 23:40:29 +0000921{
922 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
923
924 Value::use_iterator ui;
925
Sean Callanana48fe162010-08-11 03:57:18 +0000926 SmallVector<User*, 16> users;
927
928 // We do this because the use list might change, invalidating our iterator.
929 // Much better to keep a work list ourselves.
Sean Callananbafd6852010-07-14 23:40:29 +0000930 for (ui = C->use_begin();
931 ui != C->use_end();
932 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +0000933 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +0000934
Sean Callanana48fe162010-08-11 03:57:18 +0000935 for (int i = 0;
936 i < users.size();
937 ++i)
938 {
939 User *user = users[i];
940
Sean Callananbafd6852010-07-14 23:40:29 +0000941 if (Constant *constant = dyn_cast<Constant>(user))
942 {
943 // synthesize a new non-constant equivalent of the constant
944
945 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
946 {
947 switch (constant_expr->getOpcode())
948 {
949 default:
950 if (log)
951 log->Printf("Unhandled constant expression type: %s", PrintValue(constant_expr).c_str());
952 return false;
953 case Instruction::BitCast:
954 {
955 // UnaryExpr
956 // OperandList[0] is value
957
958 Value *s = constant_expr->getOperand(0);
959
960 if (s == C)
961 s = new_value;
962
963 BitCastInst *bit_cast(new BitCastInst(s, C->getType(), "", first_entry_instruction));
964
965 UnfoldConstant(constant_expr, bit_cast, first_entry_instruction);
966 }
967 break;
968 case Instruction::GetElementPtr:
969 {
970 // GetElementPtrConstantExpr
971 // OperandList[0] is base
972 // OperandList[1]... are indices
973
974 Value *ptr = constant_expr->getOperand(0);
975
976 if (ptr == C)
977 ptr = new_value;
978
979 SmallVector<Value*, 16> indices;
980
981 unsigned operand_index;
982 unsigned num_operands = constant_expr->getNumOperands();
983
984 for (operand_index = 1;
985 operand_index < num_operands;
986 ++operand_index)
987 {
988 Value *operand = constant_expr->getOperand(operand_index);
989
990 if (operand == C)
991 operand = new_value;
992
993 indices.push_back(operand);
994 }
995
996 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_instruction));
997
998 UnfoldConstant(constant_expr, get_element_ptr, first_entry_instruction);
999 }
1000 break;
1001 }
1002 }
1003 else
1004 {
1005 if (log)
1006 log->Printf("Unhandled constant type: %s", PrintValue(constant).c_str());
1007 return false;
1008 }
1009 }
1010 else
1011 {
1012 // simple fall-through case for non-constants
1013 user->replaceUsesOfWith(C, new_value);
1014 }
1015 }
1016
1017 return true;
1018}
1019
Sean Callanan8bce6652010-07-13 21:41:46 +00001020bool
Sean Callananf5857a02010-07-31 01:32:05 +00001021IRForTarget::replaceVariables(Module &M, Function &F)
Sean Callanan8bce6652010-07-13 21:41:46 +00001022{
Sean Callanane8a59a82010-09-13 21:34:21 +00001023 if (!m_resolve_vars)
1024 return true;
1025
Sean Callanan8bce6652010-07-13 21:41:46 +00001026 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
1027
1028 m_decl_map->DoStructLayout();
1029
1030 if (log)
1031 log->Printf("Element arrangement:");
1032
1033 uint32_t num_elements;
1034 uint32_t element_index;
1035
1036 size_t size;
1037 off_t alignment;
1038
1039 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
1040 return false;
1041
Sean Callananf5857a02010-07-31 01:32:05 +00001042 Function::arg_iterator iter(F.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00001043
Sean Callananf5857a02010-07-31 01:32:05 +00001044 if (iter == F.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +00001045 return false;
1046
Sean Callanan02fbafa2010-07-27 21:39:39 +00001047 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00001048
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001049 if (argument->getName().equals("this"))
1050 {
1051 ++iter;
1052
1053 if (iter == F.getArgumentList().end())
1054 return false;
1055
1056 argument = iter;
1057 }
1058
Greg Clayton8de27c72010-10-15 22:48:33 +00001059 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan8bce6652010-07-13 21:41:46 +00001060 return false;
1061
1062 if (log)
1063 log->Printf("Arg: %s", PrintValue(argument).c_str());
1064
Sean Callananf5857a02010-07-31 01:32:05 +00001065 BasicBlock &entry_block(F.getEntryBlock());
Sean Callanan02fbafa2010-07-27 21:39:39 +00001066 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00001067
1068 if (!first_entry_instruction)
1069 return false;
1070
1071 LLVMContext &context(M.getContext());
1072 const IntegerType *offset_type(Type::getInt32Ty(context));
1073
1074 if (!offset_type)
1075 return false;
1076
1077 for (element_index = 0; element_index < num_elements; ++element_index)
1078 {
1079 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001080 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +00001081 off_t offset;
Greg Clayton8de27c72010-10-15 22:48:33 +00001082 lldb_private::ConstString name;
Sean Callanan8bce6652010-07-13 21:41:46 +00001083
Sean Callanan45690fe2010-08-30 22:17:16 +00001084 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan8bce6652010-07-13 21:41:46 +00001085 return false;
1086
1087 if (log)
Sean Callanan45690fe2010-08-30 22:17:16 +00001088 log->Printf(" %s [%s] (%s) placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00001089 value->getName().str().c_str(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001090 name.GetCString(),
Sean Callanan8bce6652010-07-13 21:41:46 +00001091 PrintValue(value, true).c_str(),
1092 offset);
1093
1094 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
1095 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", first_entry_instruction);
1096 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", first_entry_instruction);
1097
Sean Callananbafd6852010-07-14 23:40:29 +00001098 if (Constant *constant = dyn_cast<Constant>(value))
1099 UnfoldConstant(constant, bit_cast, first_entry_instruction);
1100 else
1101 value->replaceAllUsesWith(bit_cast);
Sean Callanan8bce6652010-07-13 21:41:46 +00001102 }
1103
1104 if (log)
1105 log->Printf("Total structure [align %d, size %d]", alignment, size);
1106
1107 return true;
1108}
1109
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001110bool
1111IRForTarget::runOnModule(Module &M)
1112{
1113 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
1114
Sean Callanan65dafa82010-08-27 01:01:44 +00001115 Function* function = M.getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001116
1117 if (!function)
1118 {
1119 if (log)
Sean Callanan65dafa82010-08-27 01:01:44 +00001120 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001121
1122 return false;
1123 }
1124
Sean Callanan02fbafa2010-07-27 21:39:39 +00001125 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001126
Sean Callanan82b74c82010-08-12 01:56:52 +00001127 ////////////////////////////////////////////////////////////
Greg Clayton8de27c72010-10-15 22:48:33 +00001128 // Replace $__lldb_expr_result with a persistent variable
Sean Callanan82b74c82010-08-12 01:56:52 +00001129 //
1130
1131 if (!createResultVariable(M, *function))
1132 return false;
1133
Sean Callananf5857a02010-07-31 01:32:05 +00001134 //////////////////////////////////
1135 // Run basic-block level passes
1136 //
1137
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001138 for (bbi = function->begin();
1139 bbi != function->end();
1140 ++bbi)
1141 {
Sean Callanan8c127202010-08-23 23:09:38 +00001142 if (!removeGuards(M, *bbi))
1143 return false;
1144
Sean Callanana48fe162010-08-11 03:57:18 +00001145 if (!rewritePersistentAllocs(M, *bbi))
Sean Callananf5857a02010-07-31 01:32:05 +00001146 return false;
1147
Sean Callanana48fe162010-08-11 03:57:18 +00001148 if (!rewriteObjCSelectors(M, *bbi))
1149 return false;
1150
Sean Callananf5857a02010-07-31 01:32:05 +00001151 if (!resolveExternals(M, *bbi))
Sean Callanan8bce6652010-07-13 21:41:46 +00001152 return false;
1153 }
1154
Sean Callanan771131d2010-09-30 21:18:25 +00001155 ///////////////////////////////
1156 // Run function-level passes
1157 //
1158
1159 if (!replaceVariables(M, *function))
1160 return false;
1161
Sean Callanan8bce6652010-07-13 21:41:46 +00001162 if (log)
1163 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00001164 std::string s;
1165 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00001166
Sean Callanan321fe9e2010-07-28 01:00:59 +00001167 M.print(oss, NULL);
1168
1169 oss.flush();
1170
1171 log->Printf("Module after preparing for execution: \n%s", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001172 }
1173
1174 return true;
1175}
1176
1177void
1178IRForTarget::assignPassManager(PMStack &PMS,
Sean Callanan8bce6652010-07-13 21:41:46 +00001179 PassManagerType T)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001180{
1181}
1182
1183PassManagerType
1184IRForTarget::getPotentialPassManagerType() const
1185{
1186 return PMT_ModulePassManager;
1187}