blob: 63752e161b5feffbd4222dbc2d7d4820ac474282 [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 Callananbc2928a2010-08-03 00:23:29 +0000250 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(V))
251 {
252 if (constant_expr->getOpcode() == Instruction::GetElementPtr)
253 {
254 Value *s = constant_expr->getOperand(0);
255 MaybeHandleVariable(M, s, Store);
256 }
257 }
Sean Callanan8bce6652010-07-13 21:41:46 +0000258 if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(V))
Sean Callananf5857a02010-07-31 01:32:05 +0000259 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000260 clang::NamedDecl *named_decl = DeclForGlobalValue(M, global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000261
Sean Callananf5857a02010-07-31 01:32:05 +0000262 if (!named_decl)
263 {
264 if (isObjCSelectorRef(V))
265 return true;
266
267 if (log)
268 log->Printf("Found global variable %s without metadata", global_variable->getName().str().c_str());
269 return false;
270 }
271
Sean Callanan810f22d2010-07-16 00:09:46 +0000272 std::string name = named_decl->getName().str();
273
274 void *qual_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +0000275 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +0000276
277 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +0000278 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000279 qual_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +0000280 ast_context = &value_decl->getASTContext();
281 }
Sean Callanan810f22d2010-07-16 00:09:46 +0000282 else
Sean Callananf328c9f2010-07-20 23:31:16 +0000283 {
Sean Callanan810f22d2010-07-16 00:09:46 +0000284 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +0000285 }
286
Sean Callanan02fbafa2010-07-27 21:39:39 +0000287 const Type *value_type = global_variable->getType();
Sean Callanan8bce6652010-07-13 21:41:46 +0000288
289 size_t value_size = m_target_data->getTypeStoreSize(value_type);
290 off_t value_alignment = m_target_data->getPrefTypeAlignment(value_type);
291
Sean Callananba992c52010-07-27 02:07:53 +0000292 if (named_decl && !m_decl_map->AddValueToStruct(V,
293 named_decl,
294 name,
295 qual_type,
296 ast_context,
297 value_size,
298 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +0000299 return false;
300 }
301
302 return true;
303}
304
305bool
Sean Callananba992c52010-07-27 02:07:53 +0000306IRForTarget::MaybeHandleCall(Module &M,
307 CallInst *C)
308{
309 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
310
Sean Callanan02fbafa2010-07-27 21:39:39 +0000311 Function *fun = C->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +0000312
313 if (fun == NULL)
314 return true;
315
316 clang::NamedDecl *fun_decl = DeclForGlobalValue(M, fun);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000317 uint64_t fun_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000318 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +0000319
Sean Callananf5857a02010-07-31 01:32:05 +0000320 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +0000321 {
Sean Callananf5857a02010-07-31 01:32:05 +0000322 if (!m_decl_map->GetFunctionInfo(fun_decl, fun_value_ptr, fun_addr))
323 {
324 if (log)
325 log->Printf("Function %s had no address", fun_decl->getNameAsCString());
326 return false;
327 }
328 }
329 else
330 {
331 if (!m_decl_map->GetFunctionAddress(fun->getName().str().c_str(), fun_addr))
332 {
333 if (log)
334 log->Printf("Metadataless function %s had no address", fun->getName().str().c_str());
335 return false;
336 }
Sean Callananba992c52010-07-27 02:07:53 +0000337 }
338
339 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +0000340 log->Printf("Found %s at %llx", fun->getName().str().c_str(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +0000341
Sean Callananf5857a02010-07-31 01:32:05 +0000342 Value *fun_addr_ptr;
343
344 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +0000345 {
346 std::vector<const Type*> params;
347
348 const IntegerType *intptr_ty = Type::getIntNTy(M.getContext(),
349 (M.getPointerSize() == Module::Pointer64) ? 64 : 32);
350
351 FunctionType *fun_ty = FunctionType::get(intptr_ty, params, true);
352 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
353 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +0000354 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
355
356 if (fun_value_ptr)
357 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000358 }
Sean Callananf5857a02010-07-31 01:32:05 +0000359
360 if (fun_value_ptr)
361 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000362
Sean Callananf5857a02010-07-31 01:32:05 +0000363 C->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +0000364
Sean Callananba992c52010-07-27 02:07:53 +0000365 return true;
366}
367
368bool
Sean Callananf5857a02010-07-31 01:32:05 +0000369IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
Sean Callanan8bce6652010-07-13 21:41:46 +0000370{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000371 /////////////////////////////////////////////////////////////////////////
372 // Prepare the current basic block for execution in the remote process
373 //
374
Sean Callanan02fbafa2010-07-27 21:39:39 +0000375 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +0000376
377 for (ii = BB.begin();
378 ii != BB.end();
379 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000380 {
Sean Callanan8bce6652010-07-13 21:41:46 +0000381 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000382
Sean Callanan8bce6652010-07-13 21:41:46 +0000383 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Sean Callananba992c52010-07-27 02:07:53 +0000384 if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
Sean Callanan8bce6652010-07-13 21:41:46 +0000385 return false;
Sean Callananf5857a02010-07-31 01:32:05 +0000386
Sean Callanan8bce6652010-07-13 21:41:46 +0000387 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callananba992c52010-07-27 02:07:53 +0000388 if (!MaybeHandleVariable(M, store->getPointerOperand(), true))
389 return false;
390
391 if (CallInst *call = dyn_cast<CallInst>(&inst))
392 if (!MaybeHandleCall(M, call))
Sean Callanan8bce6652010-07-13 21:41:46 +0000393 return false;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000394 }
395
396 return true;
397}
398
Sean Callananbafd6852010-07-14 23:40:29 +0000399static std::string
Sean Callanan02fbafa2010-07-27 21:39:39 +0000400PrintValue(Value *V, bool truncate = false)
Sean Callanan8bce6652010-07-13 21:41:46 +0000401{
402 std::string s;
403 raw_string_ostream rso(s);
404 V->print(rso);
405 rso.flush();
406 if (truncate)
407 s.resize(s.length() - 1);
408 return s;
409}
410
Sean Callanan02fbafa2010-07-27 21:39:39 +0000411static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +0000412{
413 ConstantExpr *C = dyn_cast<ConstantExpr>(V);
414
415 if (!C || C->getOpcode() != Instruction::BitCast)
416 return false;
417
418 GlobalVariable *GV = dyn_cast<GlobalVariable>(C->getOperand(0));
419
420 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
421 return false;
422
423 return true;
424}
425
426static void TurnGuardLoadIntoZero(Instruction* guard_load, Module &M)
427{
428 Constant* zero(ConstantInt::get(Type::getInt8Ty(M.getContext()), 0, true));
429
430 Value::use_iterator ui;
431
432 for (ui = guard_load->use_begin();
433 ui != guard_load->use_end();
434 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +0000435 {
Greg Clayton6e713402010-07-30 20:30:44 +0000436 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +0000437 {
438 // do nothing for the moment
439 }
440 else
441 {
442 ui->replaceUsesOfWith(guard_load, zero);
443 }
444 }
Sean Callanan45839272010-07-24 01:37:44 +0000445
446 guard_load->eraseFromParent();
447}
448
449static void ExciseGuardStore(Instruction* guard_store)
450{
451 guard_store->eraseFromParent();
452}
453
454bool
455IRForTarget::removeGuards(Module &M, BasicBlock &BB)
456{
457 ///////////////////////////////////////////////////////
458 // Eliminate any reference to guard variables found.
459 //
460
Sean Callanan02fbafa2010-07-27 21:39:39 +0000461 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +0000462
Sean Callanan02fbafa2010-07-27 21:39:39 +0000463 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +0000464 typedef InstrList::iterator InstrIterator;
465
466 InstrList guard_loads;
467 InstrList guard_stores;
468
469 for (ii = BB.begin();
470 ii != BB.end();
471 ++ii)
472 {
473 Instruction &inst = *ii;
474
475 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
476 if (isGuardVariableRef(load->getPointerOperand()))
477 guard_loads.push_back(&inst);
478
479 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
480 if (isGuardVariableRef(store->getPointerOperand()))
481 guard_stores.push_back(&inst);
482 }
483
484 InstrIterator iter;
485
486 for (iter = guard_loads.begin();
487 iter != guard_loads.end();
488 ++iter)
489 TurnGuardLoadIntoZero(*iter, M);
490
491 for (iter = guard_stores.begin();
492 iter != guard_stores.end();
493 ++iter)
494 ExciseGuardStore(*iter);
495
496 return true;
497}
498
Sean Callananbafd6852010-07-14 23:40:29 +0000499// UnfoldConstant operates on a constant [C] which has just been replaced with a value
500// [new_value]. We assume that new_value has been properly placed early in the function,
501// most likely somewhere in front of the first instruction in the entry basic block
502// [first_entry_instruction].
503//
504// UnfoldConstant reads through the uses of C and replaces C in those uses with new_value.
505// Where those uses are constants, the function generates new instructions to compute the
506// result of the new, non-constant expression and places them before first_entry_instruction.
507// These instructions replace the constant uses, so UnfoldConstant calls itself recursively
508// for those.
509
510static bool
Sean Callanan02fbafa2010-07-27 21:39:39 +0000511UnfoldConstant(Constant *C, Value *new_value, Instruction *first_entry_instruction)
Sean Callananbafd6852010-07-14 23:40:29 +0000512{
513 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
514
515 Value::use_iterator ui;
516
517 for (ui = C->use_begin();
518 ui != C->use_end();
519 ++ui)
520 {
521 User *user = *ui;
522
523 if (Constant *constant = dyn_cast<Constant>(user))
524 {
525 // synthesize a new non-constant equivalent of the constant
526
527 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
528 {
529 switch (constant_expr->getOpcode())
530 {
531 default:
532 if (log)
533 log->Printf("Unhandled constant expression type: %s", PrintValue(constant_expr).c_str());
534 return false;
535 case Instruction::BitCast:
536 {
537 // UnaryExpr
538 // OperandList[0] is value
539
540 Value *s = constant_expr->getOperand(0);
541
542 if (s == C)
543 s = new_value;
544
545 BitCastInst *bit_cast(new BitCastInst(s, C->getType(), "", first_entry_instruction));
546
547 UnfoldConstant(constant_expr, bit_cast, first_entry_instruction);
548 }
549 break;
550 case Instruction::GetElementPtr:
551 {
552 // GetElementPtrConstantExpr
553 // OperandList[0] is base
554 // OperandList[1]... are indices
555
556 Value *ptr = constant_expr->getOperand(0);
557
558 if (ptr == C)
559 ptr = new_value;
560
561 SmallVector<Value*, 16> indices;
562
563 unsigned operand_index;
564 unsigned num_operands = constant_expr->getNumOperands();
565
566 for (operand_index = 1;
567 operand_index < num_operands;
568 ++operand_index)
569 {
570 Value *operand = constant_expr->getOperand(operand_index);
571
572 if (operand == C)
573 operand = new_value;
574
575 indices.push_back(operand);
576 }
577
578 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_instruction));
579
580 UnfoldConstant(constant_expr, get_element_ptr, first_entry_instruction);
581 }
582 break;
583 }
584 }
585 else
586 {
587 if (log)
588 log->Printf("Unhandled constant type: %s", PrintValue(constant).c_str());
589 return false;
590 }
591 }
592 else
593 {
594 // simple fall-through case for non-constants
595 user->replaceUsesOfWith(C, new_value);
596 }
597 }
598
599 return true;
600}
601
Sean Callanan8bce6652010-07-13 21:41:46 +0000602bool
Sean Callananf5857a02010-07-31 01:32:05 +0000603IRForTarget::replaceVariables(Module &M, Function &F)
Sean Callanan8bce6652010-07-13 21:41:46 +0000604{
605 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
606
607 m_decl_map->DoStructLayout();
608
609 if (log)
610 log->Printf("Element arrangement:");
611
612 uint32_t num_elements;
613 uint32_t element_index;
614
615 size_t size;
616 off_t alignment;
617
618 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
619 return false;
620
Sean Callananf5857a02010-07-31 01:32:05 +0000621 Function::arg_iterator iter(F.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +0000622
Sean Callananf5857a02010-07-31 01:32:05 +0000623 if (iter == F.getArgumentList().end())
Sean Callanan8bce6652010-07-13 21:41:46 +0000624 return false;
625
Sean Callanan02fbafa2010-07-27 21:39:39 +0000626 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +0000627
628 if (!argument->getName().equals("___clang_arg"))
629 return false;
630
631 if (log)
632 log->Printf("Arg: %s", PrintValue(argument).c_str());
633
Sean Callananf5857a02010-07-31 01:32:05 +0000634 BasicBlock &entry_block(F.getEntryBlock());
Sean Callanan02fbafa2010-07-27 21:39:39 +0000635 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +0000636
637 if (!first_entry_instruction)
638 return false;
639
640 LLVMContext &context(M.getContext());
641 const IntegerType *offset_type(Type::getInt32Ty(context));
642
643 if (!offset_type)
644 return false;
645
646 for (element_index = 0; element_index < num_elements; ++element_index)
647 {
648 const clang::NamedDecl *decl;
Sean Callanan02fbafa2010-07-27 21:39:39 +0000649 Value *value;
Sean Callanan8bce6652010-07-13 21:41:46 +0000650 off_t offset;
651
652 if (!m_decl_map->GetStructElement (decl, value, offset, element_index))
653 return false;
654
655 if (log)
656 log->Printf(" %s (%s) placed at %d",
657 decl->getIdentifier()->getNameStart(),
658 PrintValue(value, true).c_str(),
659 offset);
660
661 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
662 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", first_entry_instruction);
663 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", first_entry_instruction);
664
Sean Callananbafd6852010-07-14 23:40:29 +0000665 if (Constant *constant = dyn_cast<Constant>(value))
666 UnfoldConstant(constant, bit_cast, first_entry_instruction);
667 else
668 value->replaceAllUsesWith(bit_cast);
Sean Callanan8bce6652010-07-13 21:41:46 +0000669 }
670
671 if (log)
672 log->Printf("Total structure [align %d, size %d]", alignment, size);
673
674 return true;
675}
676
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000677bool
678IRForTarget::runOnModule(Module &M)
679{
680 lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
681
Sean Callanan02fbafa2010-07-27 21:39:39 +0000682 Function* function = M.getFunction(StringRef("___clang_expr"));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000683
684 if (!function)
685 {
686 if (log)
687 log->Printf("Couldn't find ___clang_expr() in the module");
688
689 return false;
690 }
691
Sean Callanan02fbafa2010-07-27 21:39:39 +0000692 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000693
Sean Callananf5857a02010-07-31 01:32:05 +0000694 //////////////////////////////////
695 // Run basic-block level passes
696 //
697
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000698 for (bbi = function->begin();
699 bbi != function->end();
700 ++bbi)
701 {
Sean Callananf5857a02010-07-31 01:32:05 +0000702 if (!rewriteObjCSelectors(M, *bbi))
703 return false;
704
705 if (!resolveExternals(M, *bbi))
Sean Callanan8bce6652010-07-13 21:41:46 +0000706 return false;
Sean Callanan45839272010-07-24 01:37:44 +0000707
708 if (!removeGuards(M, *bbi))
709 return false;
Sean Callanan8bce6652010-07-13 21:41:46 +0000710 }
711
Sean Callananf5857a02010-07-31 01:32:05 +0000712 ///////////////////////////////
713 // Run function-level passes
714 //
Sean Callanan321fe9e2010-07-28 01:00:59 +0000715
Sean Callananf5857a02010-07-31 01:32:05 +0000716 if (!replaceVariables(M, *function))
Sean Callanan8bce6652010-07-13 21:41:46 +0000717 return false;
718
719 if (log)
720 {
Sean Callanan321fe9e2010-07-28 01:00:59 +0000721 std::string s;
722 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +0000723
Sean Callanan321fe9e2010-07-28 01:00:59 +0000724 M.print(oss, NULL);
725
726 oss.flush();
727
728 log->Printf("Module after preparing for execution: \n%s", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000729 }
730
731 return true;
732}
733
734void
735IRForTarget::assignPassManager(PMStack &PMS,
Sean Callanan8bce6652010-07-13 21:41:46 +0000736 PassManagerType T)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +0000737{
738}
739
740PassManagerType
741IRForTarget::getPotentialPassManagerType() const
742{
743 return PMT_ModulePassManager;
744}