blob: 995c42fa186652f786db2fd1bdf781931ad49b02 [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 Callanan5cf4a1c2010-07-03 01:35:46 +000015#include "llvm/Module.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000016#include "llvm/Target/TargetData.h"
17
18#include "clang/AST/ASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000019
20#include "lldb/Core/dwarf.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/Scalar.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Expression/ClangExpressionDeclMap.h"
25
26#include <map>
27
28using namespace llvm;
29
30IRForTarget::IRForTarget(const void *pid,
Sean Callanan8bce6652010-07-13 21:41:46 +000031 lldb_private::ClangExpressionDeclMap *decl_map,
Sean Callanan02fbafa2010-07-27 21:39:39 +000032 const TargetData *target_data) :
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000033 ModulePass(pid),
Sean Callanan8bce6652010-07-13 21:41:46 +000034 m_decl_map(decl_map),
Sean Callananf5857a02010-07-31 01:32:05 +000035 m_target_data(target_data),
36 m_sel_registerName(NULL)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000037{
38}
39
40IRForTarget::~IRForTarget()
41{
42}
43
Sean Callananf5857a02010-07-31 01:32:05 +000044static bool isObjCSelectorRef(Value *V)
45{
46 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
47
48 if (!GV || !GV->hasName() || !GV->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
49 return false;
50
51 return true;
52}
53
54bool
55IRForTarget::RewriteObjCSelector(Instruction* selector_load,
56 Module &M)
57{
58 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
59
60 LoadInst *load = dyn_cast<LoadInst>(selector_load);
61
62 if (!load)
63 return false;
64
65 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
66 //
67 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
68 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
69 //
70 // where %obj is the object pointer and %tmp is the selector.
71 //
72 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_METH_VAR_NAME_".
73 // @"\01L_OBJC_METH_VAR_NAME_" contains the string.
74
75 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
76
77 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
78
79 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
80 return false;
81
82 Constant *osr_initializer = _objc_selector_references_->getInitializer();
83
84 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
85
86 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
87 return false;
88
89 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
90
91 if (!osr_initializer_base)
92 return false;
93
94 // Find the string's initializer (a ConstantArray) and get the string from it
95
96 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
97
98 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
99 return false;
100
101 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
102
103 ConstantArray *omvn_initializer_array = dyn_cast<ConstantArray>(omvn_initializer);
104
105 if (!omvn_initializer_array->isString())
106 return false;
107
108 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
109
110 if (log)
111 log->Printf("Found Objective-C selector reference %s", omvn_initializer_string.c_str());
112
113 // Construct a call to sel_registerName
114
115 if (!m_sel_registerName)
116 {
117 uint64_t srN_addr;
118
119 if (!m_decl_map->GetFunctionAddress("sel_registerName", srN_addr))
120 return false;
121
122 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
123
124 // The below code would be "more correct," but in actuality what's required is uint8_t*
125 //Type *sel_type = StructType::get(M.getContext());
126 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
127 const Type *sel_ptr_type = Type::getInt8PtrTy(M.getContext());
128
129 std::vector <const Type *> srN_arg_types;
130 srN_arg_types.push_back(Type::getInt8PtrTy(M.getContext()));
131 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
132
133 // Build the constant containing the pointer to the function
134 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
135 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
136 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
137 Constant *srN_addr_int = ConstantInt::get(intptr_ty, srN_addr, false);
138 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
139 }
140
141 SmallVector <Value*, 1> srN_arguments;
142
143 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(M.getContext()));
144
145 srN_arguments.push_back(omvn_pointer);
146
147 CallInst *srN_call = CallInst::Create(m_sel_registerName,
148 srN_arguments.begin(),
149 srN_arguments.end(),
150 "srN",
151 selector_load);
152
153 // Replace the load with the call in all users
154
155 selector_load->replaceAllUsesWith(srN_call);
156
157 selector_load->eraseFromParent();
158
159 return true;
160}
161
162bool
163IRForTarget::rewriteObjCSelectors(Module &M,
164 BasicBlock &BB)
165{
166 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
167
168 BasicBlock::iterator ii;
169
170 typedef SmallVector <Instruction*, 2> InstrList;
171 typedef InstrList::iterator InstrIterator;
172
173 InstrList selector_loads;
174
175 for (ii = BB.begin();
176 ii != BB.end();
177 ++ii)
178 {
179 Instruction &inst = *ii;
180
181 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
182 if (isObjCSelectorRef(load->getPointerOperand()))
183 selector_loads.push_back(&inst);
184 }
185
186 InstrIterator iter;
187
188 for (iter = selector_loads.begin();
189 iter != selector_loads.end();
190 ++iter)
191 {
192 if (!RewriteObjCSelector(*iter, M))
193 {
194 if(log)
195 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
196 return false;
197 }
198 }
199
200 return true;
201}
202
Sean Callanan8bce6652010-07-13 21:41:46 +0000203static clang::NamedDecl *
Sean Callanan02fbafa2010-07-27 21:39:39 +0000204DeclForGlobalValue(Module &module,
205 GlobalValue *global_value)
Sean Callanan8bce6652010-07-13 21:41:46 +0000206{
207 NamedMDNode *named_metadata = module.getNamedMetadata("clang.global.decl.ptrs");
208
209 if (!named_metadata)
210 return NULL;
211
212 unsigned num_nodes = named_metadata->getNumOperands();
213 unsigned node_index;
214
215 for (node_index = 0;
216 node_index < num_nodes;
217 ++node_index)
218 {
219 MDNode *metadata_node = named_metadata->getOperand(node_index);
220
221 if (!metadata_node)
222 return NULL;
223
224 if (metadata_node->getNumOperands() != 2)
225 return NULL;
226
227 if (metadata_node->getOperand(0) != global_value)
228 continue;
229
230 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
231
232 if (!constant_int)
233 return NULL;
234
235 uintptr_t ptr = constant_int->getZExtValue();
236
237 return reinterpret_cast<clang::NamedDecl *>(ptr);
238 }
239
240 return NULL;
241}
242
243bool
244IRForTarget::MaybeHandleVariable(Module &M,
Sean Callanan02fbafa2010-07-27 21:39:39 +0000245 Value *V,
Sean Callanan8bce6652010-07-13 21:41:46 +0000246 bool Store)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000247{
Sean Callananf5857a02010-07-31 01:32:05 +0000248 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
249
Sean Callanan8bce6652010-07-13 21:41:46 +0000250 if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(V))
Sean Callananf5857a02010-07-31 01:32:05 +0000251 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000252 clang::NamedDecl *named_decl = DeclForGlobalValue(M, global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000253
Sean Callananf5857a02010-07-31 01:32:05 +0000254 if (!named_decl)
255 {
256 if (isObjCSelectorRef(V))
257 return true;
258
259 if (log)
260 log->Printf("Found global variable %s without metadata", global_variable->getName().str().c_str());
261 return false;
262 }
263
Sean Callanan810f22d2010-07-16 00:09:46 +0000264 std::string name = named_decl->getName().str();
265
266 void *qual_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +0000267 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000268
269 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +0000270 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000271 qual_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +0000272 ast_context = &value_decl->getASTContext();
273 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000274 else
Sean Callananf328c9f2010-07-20 23:31:16 +0000275 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000276 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +0000277 }
278
Sean Callanan02fbafa2010-07-27 21:39:39 +0000279 const Type *value_type = global_variable->getType();
Sean Callanan8bce6652010-07-13 21:41:46 +0000280
281 size_t value_size = m_target_data->getTypeStoreSize(value_type);
282 off_t value_alignment = m_target_data->getPrefTypeAlignment(value_type);
283
Sean Callananba992c52010-07-27 02:07:53 +0000284 if (named_decl && !m_decl_map->AddValueToStruct(V,
285 named_decl,
286 name,
287 qual_type,
288 ast_context,
289 value_size,
290 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +0000291 return false;
292 }
293
294 return true;
295}
296
297bool
Sean Callananba992c52010-07-27 02:07:53 +0000298IRForTarget::MaybeHandleCall(Module &M,
299 CallInst *C)
300{
301 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
302
Sean Callanan02fbafa2010-07-27 21:39:39 +0000303 Function *fun = C->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +0000304
305 if (fun == NULL)
306 return true;
307
308 clang::NamedDecl *fun_decl = DeclForGlobalValue(M, fun);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000309 uint64_t fun_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000310 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +0000311
Sean Callananf5857a02010-07-31 01:32:05 +0000312 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +0000313 {
Sean Callananf5857a02010-07-31 01:32:05 +0000314 if (!m_decl_map->GetFunctionInfo(fun_decl, fun_value_ptr, fun_addr))
315 {
316 if (log)
317 log->Printf("Function %s had no address", fun_decl->getNameAsCString());
318 return false;
319 }
320 }
321 else
322 {
323 if (!m_decl_map->GetFunctionAddress(fun->getName().str().c_str(), fun_addr))
324 {
325 if (log)
326 log->Printf("Metadataless function %s had no address", fun->getName().str().c_str());
327 return false;
328 }
Sean Callananba992c52010-07-27 02:07:53 +0000329 }
330
331 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000332 log->Printf("Found %s at %llx", fun->getName().str().c_str(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +0000333
Sean Callananf5857a02010-07-31 01:32:05 +0000334 Value *fun_addr_ptr;
335
336 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +0000337 {
338 std::vector<const Type*> params;
339
340 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
341 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
342
343 FunctionType *fun_ty = FunctionType::get(intptr_ty, params, true);
344 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
345 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000346 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
347
348 if (fun_value_ptr)
349 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000350 }
Sean Callananf5857a02010-07-31 01:32:05 +0000351
352 if (fun_value_ptr)
353 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000354
Sean Callananf5857a02010-07-31 01:32:05 +0000355 C->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000356
Sean Callananba992c52010-07-27 02:07:53 +0000357 return true;
358}
359
360bool
Sean Callananf5857a02010-07-31 01:32:05 +0000361IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
Sean Callanan8bce6652010-07-13 21:41:46 +0000362{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000363 /////////////////////////////////////////////////////////////////////////
364 // Prepare the current basic block for execution in the remote process
365 //
366
Sean Callanan02fbafa2010-07-27 21:39:39 +0000367 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +0000368
369 for (ii = BB.begin();
370 ii != BB.end();
371 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000372 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000373 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000374
Sean Callanan8bce6652010-07-13 21:41:46 +0000375 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Sean Callananba992c52010-07-27 02:07:53 +0000376 if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
Sean Callanan8bce6652010-07-13 21:41:46 +0000377 return false;
Sean Callananf5857a02010-07-31 01:32:05 +0000378
Sean Callanan8bce6652010-07-13 21:41:46 +0000379 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callananba992c52010-07-27 02:07:53 +0000380 if (!MaybeHandleVariable(M, store->getPointerOperand(), true))
381 return false;
382
383 if (CallInst *call = dyn_cast<CallInst>(&inst))
384 if (!MaybeHandleCall(M, call))
Sean Callanan8bce6652010-07-13 21:41:46 +0000385 return false;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000386 }
387
388 return true;
389}
390
Sean Callananbafd6852010-07-14 23:40:29 +0000391static std::string
Sean Callanan02fbafa2010-07-27 21:39:39 +0000392PrintValue(Value *V, bool truncate = false)
Sean Callanan8bce6652010-07-13 21:41:46 +0000393{
394 std::string s;
395 raw_string_ostream rso(s);
396 V->print(rso);
397 rso.flush();
398 if (truncate)
399 s.resize(s.length() - 1);
400 return s;
401}
402
Sean Callanan02fbafa2010-07-27 21:39:39 +0000403static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +0000404{
405 ConstantExpr *C = dyn_cast<ConstantExpr>(V);
406
407 if (!C || C->getOpcode() != Instruction::BitCast)
408 return false;
409
410 GlobalVariable *GV = dyn_cast<GlobalVariable>(C->getOperand(0));
411
412 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
413 return false;
414
415 return true;
416}
417
418static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &M)
419{
420 Constant* zero(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0, true));
421
422 Value::use_iterator ui;
423
424 for (ui = guard_load->use_begin();
425 ui != guard_load->use_end();
426 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +0000427 {
Greg Clayton6e713402010-07-30 20:30:44 +0000428 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +0000429 {
430 // do nothing for the moment
431 }
432 else
433 {
434 ui->replaceUsesOfWith(guard_load, zero);
435 }
436 }
Sean Callanan45839272010-07-24 01:37:44 +0000437
438 guard_load->eraseFromParent();
439}
440
441static void ExciseGuardStore(Instruction* guard_store)
442{
443 guard_store->eraseFromParent();
444}
445
446bool
447IRForTarget::removeGuards(Module &M, BasicBlock &BB)
448{
449 ///////////////////////////////////////////////////////
450 // Eliminate any reference to guard variables found.
451 //
452
Sean Callanan02fbafa2010-07-27 21:39:39 +0000453 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +0000454
Sean Callanan02fbafa2010-07-27 21:39:39 +0000455 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +0000456 typedef InstrList::iterator InstrIterator;
457
458 InstrList guard_loads;
459 InstrList guard_stores;
460
461 for (ii = BB.begin();
462 ii != BB.end();
463 ++ii)
464 {
465 Instruction &inst = *ii;
466
467 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
468 if (isGuardVariableRef(load->getPointerOperand()))
469 guard_loads.push_back(&inst);
470
471 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
472 if (isGuardVariableRef(store->getPointerOperand()))
473 guard_stores.push_back(&inst);
474 }
475
476 InstrIterator iter;
477
478 for (iter = guard_loads.begin();
479 iter != guard_loads.end();
480 ++iter)
481 TurnGuardLoadIntoZero(*iter, M);
482
483 for (iter = guard_stores.begin();
484 iter != guard_stores.end();
485 ++iter)
486 ExciseGuardStore(*iter);
487
488 return true;
489}
490
Sean Callananbafd6852010-07-14 23:40:29 +0000491// UnfoldConstant operates on a constant [C] which has just been replaced with a value
492// [new_value]. We assume that new_value has been properly placed early in the function,
493// most likely somewhere in front of the first instruction in the entry basic block
494// [first_entry_instruction].
495//
496// UnfoldConstant reads through the uses of C and replaces C in those uses with new_value.
497// Where those uses are constants, the function generates new instructions to compute the
498// result of the new, non-constant expression and places them before first_entry_instruction.
499// These instructions replace the constant uses, so UnfoldConstant calls itself recursively
500// for those.
501
502static bool
Sean Callanan02fbafa2010-07-27 21:39:39 +0000503UnfoldConstant(Constant *C, Value *new_value, Instruction *first_entry_instruction)
Sean Callananbafd6852010-07-14 23:40:29 +0000504{
505 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
506
507 Value::use_iterator ui;
508
509 for (ui = C->use_begin();
510 ui != C->use_end();
511 ++ui)
512 {
513 User *user = *ui;
514
515 if (Constant *constant = dyn_cast<Constant>(user))
516 {
517 // synthesize a new non-constant equivalent of the constant
518
519 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
520 {
521 switch (constant_expr->getOpcode())
522 {
523 default:
524 if (log)
525 log->Printf("Unhandled constant expression type: %s", PrintValue(constant_expr).c_str());
526 return false;
527 case Instruction::BitCast:
528 {
529 // UnaryExpr
530 // OperandList[0] is value
531
532 Value *s = constant_expr->getOperand(0);
533
534 if (s == C)
535 s = new_value;
536
537 BitCastInst *bit_cast(new BitCastInst(s, C->getType(), "", first_entry_instruction));
538
539 UnfoldConstant(constant_expr, bit_cast, first_entry_instruction);
540 }
541 break;
542 case Instruction::GetElementPtr:
543 {
544 // GetElementPtrConstantExpr
545 // OperandList[0] is base
546 // OperandList[1]... are indices
547
548 Value *ptr = constant_expr->getOperand(0);
549
550 if (ptr == C)
551 ptr = new_value;
552
553 SmallVector<Value*, 16> indices;
554
555 unsigned operand_index;
556 unsigned num_operands = constant_expr->getNumOperands();
557
558 for (operand_index = 1;
559 operand_index < num_operands;
560 ++operand_index)
561 {
562 Value *operand = constant_expr->getOperand(operand_index);
563
564 if (operand == C)
565 operand = new_value;
566
567 indices.push_back(operand);
568 }
569
570 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_instruction));
571
572 UnfoldConstant(constant_expr, get_element_ptr, first_entry_instruction);
573 }
574 break;
575 }
576 }
577 else
578 {
579 if (log)
580 log->Printf("Unhandled constant type: %s", PrintValue(constant).c_str());
581 return false;
582 }
583 }
584 else
585 {
586 // simple fall-through case for non-constants
587 user->replaceUsesOfWith(C, new_value);
588 }
589 }
590
591 return true;
592}
593
Sean Callanan8bce6652010-07-13 21:41:46 +0000594bool
Sean Callananf5857a02010-07-31 01:32:05 +0000595IRForTarget::replaceVariables(Module &M, Function &F)
Sean Callanan8bce6652010-07-13 21:41:46 +0000596{
597 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
598
599 m_decl_map->DoStructLayout();
600
601 if (log)
602 log->Printf("Element arrangement:");
603
604 uint32_t num_elements;
605 uint32_t element_index;
606
607 size_t size;
608 off_t alignment;
609
610 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
611 return false;
612
Sean Callananf5857a02010-07-31 01:32:05 +0000613 Function::arg_iterator iter(F.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +0000614
Sean Callananf5857a02010-07-31 01:32:05 +0000615 if (iter == F.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +0000616 return false;
617
Sean Callanan02fbafa2010-07-27 21:39:39 +0000618 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +0000619
620 if (!argument->getName().equals("___clang_arg"))
621 return false;
622
623 if (log)
624 log->Printf("Arg: %s", PrintValue(argument).c_str());
625
Sean Callananf5857a02010-07-31 01:32:05 +0000626 BasicBlock &entry_block(F.getEntryBlock());
Sean Callanan02fbafa2010-07-27 21:39:39 +0000627 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +0000628
629 if (!first_entry_instruction)
630 return false;
631
632 LLVMContext &context(M.getContext());
633 const IntegerType *offset_type(Type::getInt32Ty(context));
634
635 if (!offset_type)
636 return false;
637
638 for (element_index = 0; element_index < num_elements; ++element_index)
639 {
640 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000641 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +0000642 off_t offset;
643
644 if (!m_decl_map->GetStructElement (decl, value, offset, element_index))
645 return false;
646
647 if (log)
648 log->Printf(" %s (%s) placed at %d",
649 decl->getIdentifier()->getNameStart(),
650 PrintValue(value, true).c_str(),
651 offset);
652
653 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
654 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", first_entry_instruction);
655 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", first_entry_instruction);
656
Sean Callananbafd6852010-07-14 23:40:29 +0000657 if (Constant *constant = dyn_cast<Constant>(value))
658 UnfoldConstant(constant, bit_cast, first_entry_instruction);
659 else
660 value->replaceAllUsesWith(bit_cast);
Sean Callanan8bce6652010-07-13 21:41:46 +0000661 }
662
663 if (log)
664 log->Printf("Total structure [align %d, size %d]", alignment, size);
665
666 return true;
667}
668
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000669bool
670IRForTarget::runOnModule(Module &M)
671{
672 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
673
Sean Callanan02fbafa2010-07-27 21:39:39 +0000674 Function* function = M.getFunction(StringRef("___clang_expr"));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000675
676 if (!function)
677 {
678 if (log)
679 log->Printf("Couldn't find ___clang_expr() in the module");
680
681 return false;
682 }
683
Sean Callanan02fbafa2010-07-27 21:39:39 +0000684 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000685
Sean Callananf5857a02010-07-31 01:32:05 +0000686 //////////////////////////////////
687 // Run basic-block level passes
688 //
689
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000690 for (bbi = function->begin();
691 bbi != function->end();
692 ++bbi)
693 {
Sean Callananf5857a02010-07-31 01:32:05 +0000694 if (!rewriteObjCSelectors(M, *bbi))
695 return false;
696
697 if (!resolveExternals(M, *bbi))
Sean Callanan8bce6652010-07-13 21:41:46 +0000698 return false;
Sean Callanan45839272010-07-24 01:37:44 +0000699
700 if (!removeGuards(M, *bbi))
701 return false;
Sean Callanan8bce6652010-07-13 21:41:46 +0000702 }
703
Sean Callananf5857a02010-07-31 01:32:05 +0000704 ///////////////////////////////
705 // Run function-level passes
706 //
Sean Callanan321fe9e2010-07-28 01:00:59 +0000707
Sean Callananf5857a02010-07-31 01:32:05 +0000708 if (!replaceVariables(M, *function))
Sean Callanan8bce6652010-07-13 21:41:46 +0000709 return false;
710
711 if (log)
712 {
Sean Callanan321fe9e2010-07-28 01:00:59 +0000713 std::string s;
714 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +0000715
Sean Callanan321fe9e2010-07-28 01:00:59 +0000716 M.print(oss, NULL);
717
718 oss.flush();
719
720 log->Printf("Module after preparing for execution: \n%s", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000721 }
722
723 return true;
724}
725
726void
727IRForTarget::assignPassManager(PMStack &PMS,
Sean Callanan8bce6652010-07-13 21:41:46 +0000728 PassManagerType T)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000729{
730}
731
732PassManagerType
733IRForTarget::getPotentialPassManagerType() const
734{
735 return PMT_ModulePassManager;
736}