blob: 3ddb9efd40f67c852dc8c6ae7cfaf1ed875987a2 [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{
Greg Claytone005f2c2010-11-06 01:53:30 +000079 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan82b74c82010-08-12 01:56:52 +000080
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{
Greg Claytone005f2c2010-11-06 01:53:30 +0000273 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +0000274
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
Sean Callananc2c6f772010-10-26 00:31:56 +0000338 if (log)
339 log->Printf("Found sel_registerName at 0x%llx", srN_addr);
340
Sean Callananf5857a02010-07-31 01:32:05 +0000341 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
342
343 // The below code would be "more correct," but in actuality what's required is uint8_t*
344 //Type *sel_type = StructType::get(M.getContext());
345 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
346 const Type *sel_ptr_type = Type::getInt8PtrTy(M.getContext());
347
348 std::vector <const Type *> srN_arg_types;
349 srN_arg_types.push_back(Type::getInt8PtrTy(M.getContext()));
350 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
351
352 // Build the constant containing the pointer to the function
353 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
354 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
355 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
356 Constant *srN_addr_int = ConstantInt::get(intptr_ty, srN_addr, false);
357 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
358 }
359
360 SmallVector <Value*, 1> srN_arguments;
361
362 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(M.getContext()));
363
364 srN_arguments.push_back(omvn_pointer);
365
366 CallInst *srN_call = CallInst::Create(m_sel_registerName,
367 srN_arguments.begin(),
368 srN_arguments.end(),
369 "srN",
370 selector_load);
371
372 // Replace the load with the call in all users
373
374 selector_load->replaceAllUsesWith(srN_call);
375
376 selector_load->eraseFromParent();
377
378 return true;
379}
380
381bool
382IRForTarget::rewriteObjCSelectors(Module &M,
383 BasicBlock &BB)
384{
Greg Claytone005f2c2010-11-06 01:53:30 +0000385 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +0000386
387 BasicBlock::iterator ii;
388
389 typedef SmallVector <Instruction*, 2> InstrList;
390 typedef InstrList::iterator InstrIterator;
391
392 InstrList selector_loads;
393
394 for (ii = BB.begin();
395 ii != BB.end();
396 ++ii)
397 {
398 Instruction &inst = *ii;
399
400 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
401 if (isObjCSelectorRef(load->getPointerOperand()))
402 selector_loads.push_back(&inst);
403 }
404
405 InstrIterator iter;
406
407 for (iter = selector_loads.begin();
408 iter != selector_loads.end();
409 ++iter)
410 {
411 if (!RewriteObjCSelector(*iter, M))
412 {
413 if(log)
414 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
415 return false;
416 }
417 }
418
419 return true;
420}
421
Sean Callanana48fe162010-08-11 03:57:18 +0000422bool
423IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc,
Greg Clayton8de27c72010-10-15 22:48:33 +0000424 llvm::Module &llvm_module)
Sean Callanana48fe162010-08-11 03:57:18 +0000425{
426 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
427
428 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
429
430 if (!alloc_md || !alloc_md->getNumOperands())
431 return false;
432
433 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
434
435 if (!constant_int)
436 return false;
437
438 // We attempt to register this as a new persistent variable with the DeclMap.
439
440 uintptr_t ptr = constant_int->getZExtValue();
441
Sean Callanan82b74c82010-08-12 01:56:52 +0000442 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanana48fe162010-08-11 03:57:18 +0000443
Sean Callanan82b74c82010-08-12 01:56:52 +0000444 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
445 &decl->getASTContext());
446
Greg Clayton8de27c72010-10-15 22:48:33 +0000447 StringRef decl_name (decl->getName());
448 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
449 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type))
Sean Callanana48fe162010-08-11 03:57:18 +0000450 return false;
451
Greg Clayton8de27c72010-10-15 22:48:33 +0000452 GlobalVariable *persistent_global = new GlobalVariable(llvm_module,
Sean Callanana48fe162010-08-11 03:57:18 +0000453 alloc->getType()->getElementType(),
454 false, /* not constant */
455 GlobalValue::ExternalLinkage,
456 NULL, /* no initializer */
457 alloc->getName().str().c_str());
458
459 // What we're going to do here is make believe this was a regular old external
460 // variable. That means we need to make the metadata valid.
461
Greg Clayton8de27c72010-10-15 22:48:33 +0000462 NamedMDNode *named_metadata = llvm_module.getNamedMetadata("clang.global.decl.ptrs");
Sean Callanana48fe162010-08-11 03:57:18 +0000463
464 llvm::Value* values[2];
465 values[0] = persistent_global;
466 values[1] = constant_int;
467
Greg Clayton8de27c72010-10-15 22:48:33 +0000468 MDNode *persistent_global_md = MDNode::get(llvm_module.getContext(), values, 2);
Sean Callanana48fe162010-08-11 03:57:18 +0000469 named_metadata->addOperand(persistent_global_md);
470
471 alloc->replaceAllUsesWith(persistent_global);
472 alloc->eraseFromParent();
473
474 return true;
475}
476
477bool
478IRForTarget::rewritePersistentAllocs(llvm::Module &M,
479 llvm::BasicBlock &BB)
480{
Sean Callanane8a59a82010-09-13 21:34:21 +0000481 if (!m_resolve_vars)
482 return true;
483
Greg Claytone005f2c2010-11-06 01:53:30 +0000484 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanana48fe162010-08-11 03:57:18 +0000485
486 BasicBlock::iterator ii;
487
488 typedef SmallVector <Instruction*, 2> InstrList;
489 typedef InstrList::iterator InstrIterator;
490
491 InstrList pvar_allocs;
492
493 for (ii = BB.begin();
494 ii != BB.end();
495 ++ii)
496 {
497 Instruction &inst = *ii;
498
499 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callanan03c997b2010-10-21 22:41:32 +0000500 if (alloc->getName().startswith("$") &&
501 !alloc->getName().startswith("$__lldb"))
Sean Callanana48fe162010-08-11 03:57:18 +0000502 pvar_allocs.push_back(alloc);
503 }
504
505 InstrIterator iter;
506
507 for (iter = pvar_allocs.begin();
508 iter != pvar_allocs.end();
509 ++iter)
510 {
511 if (!RewritePersistentAlloc(*iter, M))
512 {
513 if(log)
514 log->PutCString("Couldn't rewrite the creation of a persistent variable");
515 return false;
516 }
517 }
518
519 return true;
520}
521
Sean Callanan8bce6652010-07-13 21:41:46 +0000522static clang::NamedDecl *
Sean Callanan02fbafa2010-07-27 21:39:39 +0000523DeclForGlobalValue(Module &module,
524 GlobalValue *global_value)
Sean Callanan8bce6652010-07-13 21:41:46 +0000525{
526 NamedMDNode *named_metadata = module.getNamedMetadata("clang.global.decl.ptrs");
527
528 if (!named_metadata)
529 return NULL;
530
531 unsigned num_nodes = named_metadata->getNumOperands();
532 unsigned node_index;
533
534 for (node_index = 0;
535 node_index < num_nodes;
536 ++node_index)
537 {
538 MDNode *metadata_node = named_metadata->getOperand(node_index);
539
540 if (!metadata_node)
541 return NULL;
542
543 if (metadata_node->getNumOperands() != 2)
Sean Callanana48fe162010-08-11 03:57:18 +0000544 continue;
Sean Callanan8bce6652010-07-13 21:41:46 +0000545
546 if (metadata_node->getOperand(0) != global_value)
547 continue;
548
549 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
550
551 if (!constant_int)
552 return NULL;
553
554 uintptr_t ptr = constant_int->getZExtValue();
555
556 return reinterpret_cast<clang::NamedDecl *>(ptr);
557 }
558
559 return NULL;
560}
561
562bool
Greg Clayton8de27c72010-10-15 22:48:33 +0000563IRForTarget::MaybeHandleVariable
564(
565 Module &llvm_module,
566 Value *llvm_value_ptr,
567 bool Store
568)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000569{
Greg Claytone005f2c2010-11-06 01:53:30 +0000570 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +0000571
Greg Clayton8de27c72010-10-15 22:48:33 +0000572 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callananbc2928a2010-08-03 00:23:29 +0000573 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000574 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +0000575 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000576 default:
577 break;
578 case Instruction::GetElementPtr:
579 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +0000580 Value *s = constant_expr->getOperand(0);
Greg Clayton8de27c72010-10-15 22:48:33 +0000581 MaybeHandleVariable(llvm_module, s, Store);
Sean Callananbc2928a2010-08-03 00:23:29 +0000582 }
583 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000584 if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000585 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000586 clang::NamedDecl *named_decl = DeclForGlobalValue(llvm_module, global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000587
Sean Callananf5857a02010-07-31 01:32:05 +0000588 if (!named_decl)
589 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000590 if (isObjCSelectorRef(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +0000591 return true;
592
593 if (log)
594 log->Printf("Found global variable %s without metadata", global_variable->getName().str().c_str());
595 return false;
596 }
597
Greg Clayton8de27c72010-10-15 22:48:33 +0000598 std::string name (named_decl->getName().str());
Sean Callanan810f22d2010-07-16 00:09:46 +0000599
Sean Callanan771131d2010-09-30 21:18:25 +0000600 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +0000601 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000602
603 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +0000604 {
Sean Callanan771131d2010-09-30 21:18:25 +0000605 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +0000606 ast_context = &value_decl->getASTContext();
607 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000608 else
Sean Callananf328c9f2010-07-20 23:31:16 +0000609 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000610 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +0000611 }
Sean Callanan771131d2010-09-30 21:18:25 +0000612
613 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_type));
Sean Callananf328c9f2010-07-20 23:31:16 +0000614
Sean Callanan02fbafa2010-07-27 21:39:39 +0000615 const Type *value_type = global_variable->getType();
Sean Callanan771131d2010-09-30 21:18:25 +0000616
617 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
618 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
619
620 if (log)
621 log->Printf("Type of %s is [clang %s, lldb %s] [size %d, align %d]",
622 name.c_str(),
623 qual_type.getAsString().c_str(),
624 PrintType(value_type).c_str(),
625 value_size,
626 value_alignment);
627
Sean Callanan8bce6652010-07-13 21:41:46 +0000628
Sean Callanan8c127202010-08-23 23:09:38 +0000629 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton8de27c72010-10-15 22:48:33 +0000630 lldb_private::ConstString (name.c_str()),
631 llvm_value_ptr,
Sean Callananba992c52010-07-27 02:07:53 +0000632 value_size,
633 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +0000634 return false;
635 }
636
637 return true;
638}
639
640bool
Sean Callanandc27aba2010-10-05 22:26:43 +0000641IRForTarget::MaybeHandleCallArguments(Module &M,
642 CallInst *C)
643{
Greg Claytone005f2c2010-11-06 01:53:30 +0000644 // lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanandc27aba2010-10-05 22:26:43 +0000645
646 for (unsigned op_index = 0, num_ops = C->getNumArgOperands();
647 op_index < num_ops;
648 ++op_index)
649 if (!MaybeHandleVariable(M, C->getArgOperand(op_index), true)) // conservatively believe that this is a store
650 return false;
651
652 return true;
653}
654
655bool
Greg Clayton8de27c72010-10-15 22:48:33 +0000656IRForTarget::MaybeHandleCall(Module &llvm_module,
657 CallInst *llvm_call_inst)
Sean Callananba992c52010-07-27 02:07:53 +0000658{
Greg Claytone005f2c2010-11-06 01:53:30 +0000659 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananba992c52010-07-27 02:07:53 +0000660
Greg Clayton8de27c72010-10-15 22:48:33 +0000661 Function *fun = llvm_call_inst->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +0000662
663 if (fun == NULL)
Sean Callanan65af7342010-09-08 20:04:08 +0000664 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000665 Value *val = llvm_call_inst->getCalledValue();
Sean Callanan65af7342010-09-08 20:04:08 +0000666
667 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(val);
668
669 if (const_expr && const_expr->getOpcode() == Instruction::BitCast)
670 {
671 fun = dyn_cast<Function>(const_expr->getOperand(0));
672
673 if (!fun)
674 return true;
675 }
676 else
677 {
678 return true;
679 }
680 }
Sean Callananba992c52010-07-27 02:07:53 +0000681
Greg Clayton8de27c72010-10-15 22:48:33 +0000682 lldb_private::ConstString str;
Sean Callananc04743d2010-09-28 21:13:03 +0000683
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000684 if (fun->isIntrinsic())
Sean Callananc04743d2010-09-28 21:13:03 +0000685 {
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000686 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sean Callananc04743d2010-09-28 21:13:03 +0000687
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000688 switch (intrinsic_id)
689 {
690 default:
691 if (log)
692 log->Printf("Unresolved intrinsic %s", Intrinsic::getName(intrinsic_id).c_str());
693 return false;
694 case Intrinsic::memcpy:
Greg Clayton8de27c72010-10-15 22:48:33 +0000695 {
696 static lldb_private::ConstString g_memcpy_str ("memcpy");
697 str = g_memcpy_str;
698 }
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000699 break;
700 }
Sean Callananc04743d2010-09-28 21:13:03 +0000701
Greg Clayton8de27c72010-10-15 22:48:33 +0000702 if (log && str)
703 log->Printf("Resolved intrinsic name %s", str.GetCString());
Sean Callanan3cb1fd82010-09-28 23:55:00 +0000704 }
705 else
706 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000707 str.SetCStringWithLength (fun->getName().data(), fun->getName().size());
Sean Callananc04743d2010-09-28 21:13:03 +0000708 }
709
Greg Clayton8de27c72010-10-15 22:48:33 +0000710 clang::NamedDecl *fun_decl = DeclForGlobalValue (llvm_module, fun);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000711 uint64_t fun_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000712 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +0000713
Sean Callananf5857a02010-07-31 01:32:05 +0000714 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +0000715 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000716 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_value_ptr, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000717 {
Sean Callanan92aa6662010-09-07 21:49:41 +0000718 fun_value_ptr = NULL;
719
Greg Clayton8de27c72010-10-15 22:48:33 +0000720 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callanan92aa6662010-09-07 21:49:41 +0000721 {
722 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000723 log->Printf("Function %s had no address", str.GetCString());
Sean Callanan92aa6662010-09-07 21:49:41 +0000724
725 return false;
726 }
Sean Callananf5857a02010-07-31 01:32:05 +0000727 }
728 }
729 else
730 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000731 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000732 {
733 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000734 log->Printf ("Metadataless function %s had no address", str.GetCString());
Sean Callananf5857a02010-07-31 01:32:05 +0000735 }
Sean Callananba992c52010-07-27 02:07:53 +0000736 }
737
738 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000739 log->Printf("Found %s at %llx", str.GetCString(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +0000740
Sean Callananf5857a02010-07-31 01:32:05 +0000741 Value *fun_addr_ptr;
742
743 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +0000744 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000745 const IntegerType *intptr_ty = Type::getIntNTy(llvm_module.getContext(),
746 (llvm_module.getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan92aa6662010-09-07 21:49:41 +0000747 const FunctionType *fun_ty = fun->getFunctionType();
Sean Callanan02fbafa2010-07-27 21:39:39 +0000748 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
749 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000750 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
751
752 if (fun_value_ptr)
753 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000754 }
Sean Callananf5857a02010-07-31 01:32:05 +0000755
756 if (fun_value_ptr)
757 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000758
Greg Clayton8de27c72010-10-15 22:48:33 +0000759 llvm_call_inst->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000760
Greg Clayton8de27c72010-10-15 22:48:33 +0000761 ConstantArray *func_name = (ConstantArray*)ConstantArray::get(llvm_module.getContext(), str.GetCString());
Sean Callanane8a59a82010-09-13 21:34:21 +0000762
763 Value *values[1];
764 values[0] = func_name;
Greg Clayton8de27c72010-10-15 22:48:33 +0000765 MDNode *func_metadata = MDNode::get(llvm_module.getContext(), values, 1);
Sean Callanane8a59a82010-09-13 21:34:21 +0000766
Greg Clayton8de27c72010-10-15 22:48:33 +0000767 llvm_call_inst->setMetadata("lldb.call.realName", func_metadata);
Sean Callanane8a59a82010-09-13 21:34:21 +0000768
769 if (log)
Greg Clayton8de27c72010-10-15 22:48:33 +0000770 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 +0000771
Sean Callananba992c52010-07-27 02:07:53 +0000772 return true;
773}
774
775bool
Sean Callananf5857a02010-07-31 01:32:05 +0000776IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
Sean Callanan8bce6652010-07-13 21:41:46 +0000777{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000778 /////////////////////////////////////////////////////////////////////////
779 // Prepare the current basic block for execution in the remote process
780 //
781
Sean Callanan02fbafa2010-07-27 21:39:39 +0000782 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +0000783
784 for (ii = BB.begin();
785 ii != BB.end();
786 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000787 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000788 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000789
Sean Callanane8a59a82010-09-13 21:34:21 +0000790 if (m_resolve_vars)
791 {
792 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
793 if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
794 return false;
Sean Callananf5857a02010-07-31 01:32:05 +0000795
Sean Callanane8a59a82010-09-13 21:34:21 +0000796 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callanan58a40342010-11-06 00:09:34 +0000797 if (!MaybeHandleVariable(M, store->getValueOperand(), true) ||
798 !MaybeHandleVariable(M, store->getPointerOperand(), true))
Sean Callanane8a59a82010-09-13 21:34:21 +0000799 return false;
800 }
Sean Callananba992c52010-07-27 02:07:53 +0000801
802 if (CallInst *call = dyn_cast<CallInst>(&inst))
Sean Callanandc27aba2010-10-05 22:26:43 +0000803 {
804 if (!MaybeHandleCallArguments(M, call))
805 return false;
806
Sean Callananba992c52010-07-27 02:07:53 +0000807 if (!MaybeHandleCall(M, call))
Sean Callanan8bce6652010-07-13 21:41:46 +0000808 return false;
Sean Callanandc27aba2010-10-05 22:26:43 +0000809 }
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000810 }
811
812 return true;
813}
814
Sean Callanan02fbafa2010-07-27 21:39:39 +0000815static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +0000816{
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000817 Constant *C;
Sean Callanan45839272010-07-24 01:37:44 +0000818
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000819 if (!(C = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +0000820 return false;
821
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000822 ConstantExpr *CE;
823
824 if ((CE = dyn_cast<ConstantExpr>(V)))
825 {
826 if (CE->getOpcode() != Instruction::BitCast)
827 return false;
828
829 C = CE->getOperand(0);
830 }
831
832 GlobalVariable *GV = dyn_cast<GlobalVariable>(C);
Sean Callanan45839272010-07-24 01:37:44 +0000833
834 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
835 return false;
836
837 return true;
838}
839
840static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &M)
841{
842 Constant* zero(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0, true));
843
844 Value::use_iterator ui;
845
846 for (ui = guard_load->use_begin();
847 ui != guard_load->use_end();
848 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +0000849 {
Greg Clayton6e713402010-07-30 20:30:44 +0000850 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +0000851 {
852 // do nothing for the moment
853 }
854 else
855 {
856 ui->replaceUsesOfWith(guard_load, zero);
857 }
858 }
Sean Callanan45839272010-07-24 01:37:44 +0000859
860 guard_load->eraseFromParent();
861}
862
863static void ExciseGuardStore(Instruction* guard_store)
864{
865 guard_store->eraseFromParent();
866}
867
868bool
869IRForTarget::removeGuards(Module &M, BasicBlock &BB)
870{
871 ///////////////////////////////////////////////////////
872 // Eliminate any reference to guard variables found.
873 //
874
Sean Callanan02fbafa2010-07-27 21:39:39 +0000875 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +0000876
Sean Callanan02fbafa2010-07-27 21:39:39 +0000877 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +0000878 typedef InstrList::iterator InstrIterator;
879
880 InstrList guard_loads;
881 InstrList guard_stores;
882
883 for (ii = BB.begin();
884 ii != BB.end();
885 ++ii)
886 {
887 Instruction &inst = *ii;
888
889 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
890 if (isGuardVariableRef(load->getPointerOperand()))
891 guard_loads.push_back(&inst);
892
893 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
894 if (isGuardVariableRef(store->getPointerOperand()))
895 guard_stores.push_back(&inst);
896 }
897
898 InstrIterator iter;
899
900 for (iter = guard_loads.begin();
901 iter != guard_loads.end();
902 ++iter)
903 TurnGuardLoadIntoZero(*iter, M);
904
905 for (iter = guard_stores.begin();
906 iter != guard_stores.end();
907 ++iter)
908 ExciseGuardStore(*iter);
909
910 return true;
911}
912
Sean Callananbafd6852010-07-14 23:40:29 +0000913// UnfoldConstant operates on a constant [C] which has just been replaced with a value
914// [new_value]. We assume that new_value has been properly placed early in the function,
915// most likely somewhere in front of the first instruction in the entry basic block
916// [first_entry_instruction].
917//
918// UnfoldConstant reads through the uses of C and replaces C in those uses with new_value.
919// Where those uses are constants, the function generates new instructions to compute the
920// result of the new, non-constant expression and places them before first_entry_instruction.
921// These instructions replace the constant uses, so UnfoldConstant calls itself recursively
922// for those.
923
924static bool
Sean Callanan02fbafa2010-07-27 21:39:39 +0000925UnfoldConstant(Constant *C, Value *new_value, Instruction *first_entry_instruction)
Sean Callananbafd6852010-07-14 23:40:29 +0000926{
Greg Claytone005f2c2010-11-06 01:53:30 +0000927 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbafd6852010-07-14 23:40:29 +0000928
929 Value::use_iterator ui;
930
Sean Callanana48fe162010-08-11 03:57:18 +0000931 SmallVector<User*, 16> users;
932
933 // We do this because the use list might change, invalidating our iterator.
934 // Much better to keep a work list ourselves.
Sean Callananbafd6852010-07-14 23:40:29 +0000935 for (ui = C->use_begin();
936 ui != C->use_end();
937 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +0000938 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +0000939
Sean Callanana48fe162010-08-11 03:57:18 +0000940 for (int i = 0;
941 i < users.size();
942 ++i)
943 {
944 User *user = users[i];
945
Sean Callananbafd6852010-07-14 23:40:29 +0000946 if (Constant *constant = dyn_cast<Constant>(user))
947 {
948 // synthesize a new non-constant equivalent of the constant
949
950 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
951 {
952 switch (constant_expr->getOpcode())
953 {
954 default:
955 if (log)
956 log->Printf("Unhandled constant expression type: %s", PrintValue(constant_expr).c_str());
957 return false;
958 case Instruction::BitCast:
959 {
960 // UnaryExpr
961 // OperandList[0] is value
962
963 Value *s = constant_expr->getOperand(0);
964
965 if (s == C)
966 s = new_value;
967
968 BitCastInst *bit_cast(new BitCastInst(s, C->getType(), "", first_entry_instruction));
969
970 UnfoldConstant(constant_expr, bit_cast, first_entry_instruction);
971 }
972 break;
973 case Instruction::GetElementPtr:
974 {
975 // GetElementPtrConstantExpr
976 // OperandList[0] is base
977 // OperandList[1]... are indices
978
979 Value *ptr = constant_expr->getOperand(0);
980
981 if (ptr == C)
982 ptr = new_value;
983
984 SmallVector<Value*, 16> indices;
985
986 unsigned operand_index;
987 unsigned num_operands = constant_expr->getNumOperands();
988
989 for (operand_index = 1;
990 operand_index < num_operands;
991 ++operand_index)
992 {
993 Value *operand = constant_expr->getOperand(operand_index);
994
995 if (operand == C)
996 operand = new_value;
997
998 indices.push_back(operand);
999 }
1000
1001 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_instruction));
1002
1003 UnfoldConstant(constant_expr, get_element_ptr, first_entry_instruction);
1004 }
1005 break;
1006 }
1007 }
1008 else
1009 {
1010 if (log)
1011 log->Printf("Unhandled constant type: %s", PrintValue(constant).c_str());
1012 return false;
1013 }
1014 }
1015 else
1016 {
1017 // simple fall-through case for non-constants
1018 user->replaceUsesOfWith(C, new_value);
1019 }
1020 }
1021
1022 return true;
1023}
1024
Sean Callanan8bce6652010-07-13 21:41:46 +00001025bool
Sean Callananf5857a02010-07-31 01:32:05 +00001026IRForTarget::replaceVariables(Module &M, Function &F)
Sean Callanan8bce6652010-07-13 21:41:46 +00001027{
Sean Callanane8a59a82010-09-13 21:34:21 +00001028 if (!m_resolve_vars)
1029 return true;
1030
Greg Claytone005f2c2010-11-06 01:53:30 +00001031 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8bce6652010-07-13 21:41:46 +00001032
1033 m_decl_map->DoStructLayout();
1034
1035 if (log)
1036 log->Printf("Element arrangement:");
1037
1038 uint32_t num_elements;
1039 uint32_t element_index;
1040
1041 size_t size;
1042 off_t alignment;
1043
1044 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
1045 return false;
1046
Sean Callananf5857a02010-07-31 01:32:05 +00001047 Function::arg_iterator iter(F.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00001048
Sean Callananf5857a02010-07-31 01:32:05 +00001049 if (iter == F.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +00001050 return false;
1051
Sean Callanan02fbafa2010-07-27 21:39:39 +00001052 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00001053
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00001054 if (argument->getName().equals("this"))
1055 {
1056 ++iter;
1057
1058 if (iter == F.getArgumentList().end())
1059 return false;
1060
1061 argument = iter;
1062 }
1063
Greg Clayton8de27c72010-10-15 22:48:33 +00001064 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan8bce6652010-07-13 21:41:46 +00001065 return false;
1066
1067 if (log)
1068 log->Printf("Arg: %s", PrintValue(argument).c_str());
1069
Sean Callananf5857a02010-07-31 01:32:05 +00001070 BasicBlock &entry_block(F.getEntryBlock());
Sean Callanan02fbafa2010-07-27 21:39:39 +00001071 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00001072
1073 if (!first_entry_instruction)
1074 return false;
1075
1076 LLVMContext &context(M.getContext());
1077 const IntegerType *offset_type(Type::getInt32Ty(context));
1078
1079 if (!offset_type)
1080 return false;
1081
1082 for (element_index = 0; element_index < num_elements; ++element_index)
1083 {
1084 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001085 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +00001086 off_t offset;
Greg Clayton8de27c72010-10-15 22:48:33 +00001087 lldb_private::ConstString name;
Sean Callanan8bce6652010-07-13 21:41:46 +00001088
Sean Callanan45690fe2010-08-30 22:17:16 +00001089 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan8bce6652010-07-13 21:41:46 +00001090 return false;
1091
1092 if (log)
Sean Callanan45690fe2010-08-30 22:17:16 +00001093 log->Printf(" %s [%s] (%s) placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00001094 value->getName().str().c_str(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001095 name.GetCString(),
Sean Callanan8bce6652010-07-13 21:41:46 +00001096 PrintValue(value, true).c_str(),
1097 offset);
1098
1099 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
1100 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", first_entry_instruction);
1101 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", first_entry_instruction);
1102
Sean Callananbafd6852010-07-14 23:40:29 +00001103 if (Constant *constant = dyn_cast<Constant>(value))
1104 UnfoldConstant(constant, bit_cast, first_entry_instruction);
1105 else
1106 value->replaceAllUsesWith(bit_cast);
Sean Callananb51ee982010-11-02 23:51:17 +00001107
1108 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
1109 var->eraseFromParent();
Sean Callanan8bce6652010-07-13 21:41:46 +00001110 }
1111
1112 if (log)
1113 log->Printf("Total structure [align %d, size %d]", alignment, size);
1114
1115 return true;
1116}
1117
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001118bool
1119IRForTarget::runOnModule(Module &M)
1120{
Greg Claytone005f2c2010-11-06 01:53:30 +00001121 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001122
Sean Callanan65dafa82010-08-27 01:01:44 +00001123 Function* function = M.getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001124
1125 if (!function)
1126 {
1127 if (log)
Sean Callanan65dafa82010-08-27 01:01:44 +00001128 log->Printf("Couldn't find %s() in the module", m_func_name.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001129
1130 return false;
1131 }
1132
Sean Callanan02fbafa2010-07-27 21:39:39 +00001133 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001134
Sean Callanan82b74c82010-08-12 01:56:52 +00001135 ////////////////////////////////////////////////////////////
Greg Clayton8de27c72010-10-15 22:48:33 +00001136 // Replace $__lldb_expr_result with a persistent variable
Sean Callanan82b74c82010-08-12 01:56:52 +00001137 //
1138
1139 if (!createResultVariable(M, *function))
1140 return false;
1141
Sean Callananf5857a02010-07-31 01:32:05 +00001142 //////////////////////////////////
1143 // Run basic-block level passes
1144 //
1145
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001146 for (bbi = function->begin();
1147 bbi != function->end();
1148 ++bbi)
1149 {
Sean Callanan8c127202010-08-23 23:09:38 +00001150 if (!removeGuards(M, *bbi))
1151 return false;
1152
Sean Callanana48fe162010-08-11 03:57:18 +00001153 if (!rewritePersistentAllocs(M, *bbi))
Sean Callananf5857a02010-07-31 01:32:05 +00001154 return false;
1155
Sean Callanana48fe162010-08-11 03:57:18 +00001156 if (!rewriteObjCSelectors(M, *bbi))
1157 return false;
1158
Sean Callananf5857a02010-07-31 01:32:05 +00001159 if (!resolveExternals(M, *bbi))
Sean Callanan8bce6652010-07-13 21:41:46 +00001160 return false;
1161 }
1162
Sean Callanan771131d2010-09-30 21:18:25 +00001163 ///////////////////////////////
1164 // Run function-level passes
1165 //
1166
1167 if (!replaceVariables(M, *function))
1168 return false;
1169
Sean Callanan8bce6652010-07-13 21:41:46 +00001170 if (log)
1171 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00001172 std::string s;
1173 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00001174
Sean Callanan321fe9e2010-07-28 01:00:59 +00001175 M.print(oss, NULL);
1176
1177 oss.flush();
1178
1179 log->Printf("Module after preparing for execution: \n%s", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001180 }
1181
1182 return true;
1183}
1184
1185void
1186IRForTarget::assignPassManager(PMStack &PMS,
Sean Callanan8bce6652010-07-13 21:41:46 +00001187 PassManagerType T)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001188{
1189}
1190
1191PassManagerType
1192IRForTarget::getPotentialPassManagerType() const
1193{
1194 return PMT_ModulePassManager;
1195}