blob: 6037cd0c342ed61d5e9f0194628e0bf51a1ac6b3 [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"
Sean Callanan2a8c3382011-04-14 02:01:31 +000013#include "llvm/Constants.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000014#include "llvm/InstrTypes.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000015#include "llvm/Instructions.h"
Sean Callanan3cb1fd82010-09-28 23:55:00 +000016#include "llvm/Intrinsics.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000017#include "llvm/Module.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000018#include "llvm/Target/TargetData.h"
Sean Callanan82b74c82010-08-12 01:56:52 +000019#include "llvm/ValueSymbolTable.h"
Sean Callanan8bce6652010-07-13 21:41:46 +000020
21#include "clang/AST/ASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000022
Greg Clayton8de27c72010-10-15 22:48:33 +000023#include "lldb/Core/ConstString.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000024#include "lldb/Core/dwarf.h"
25#include "lldb/Core/Log.h"
26#include "lldb/Core/Scalar.h"
27#include "lldb/Core/StreamString.h"
28#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callananc0492742011-05-23 21:40:23 +000029#include "lldb/Host/Endian.h"
Sean Callanan696cf5f2011-05-07 01:06:41 +000030#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000031
32#include <map>
33
34using namespace llvm;
35
Sean Callanan3351dac2010-08-18 18:50:51 +000036static char ID;
37
Sean Callananc0492742011-05-23 21:40:23 +000038IRForTarget::StaticDataAllocator::StaticDataAllocator()
39{
40}
41
42IRForTarget::StaticDataAllocator::~StaticDataAllocator()
43{
44}
45
Greg Clayton3c7feb42010-11-19 01:05:25 +000046IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
47 bool resolve_vars,
Sean Callanan696cf5f2011-05-07 01:06:41 +000048 lldb::ClangExpressionVariableSP &const_result,
Sean Callananc0492742011-05-23 21:40:23 +000049 StaticDataAllocator *data_allocator,
Sean Callanan97c924e2011-01-27 01:07:04 +000050 lldb_private::Stream *error_stream,
Greg Clayton3c7feb42010-11-19 01:05:25 +000051 const char *func_name) :
Sean Callanan47a5c4c2010-09-23 03:01:22 +000052 ModulePass(ID),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000053 m_resolve_vars(resolve_vars),
54 m_func_name(func_name),
Sean Callananc0492742011-05-23 21:40:23 +000055 m_module(NULL),
Johnny Chen2bc9eb32011-07-19 19:48:13 +000056 m_decl_map(decl_map),
57 m_data_allocator(data_allocator),
Sean Callanan6ba533e2010-11-17 23:00:36 +000058 m_CFStringCreateWithBytes(NULL),
Sean Callanan65dafa82010-08-27 01:01:44 +000059 m_sel_registerName(NULL),
Johnny Chen2bc9eb32011-07-19 19:48:13 +000060 m_const_result(const_result),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000061 m_error_stream(error_stream),
Sean Callanan6a925532011-01-13 08:53:35 +000062 m_has_side_effects(false),
Sean Callanan696cf5f2011-05-07 01:06:41 +000063 m_result_store(NULL),
64 m_result_is_pointer(false),
Sean Callananc0492742011-05-23 21:40:23 +000065 m_reloc_placeholder(NULL)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000066{
67}
68
Sean Callanan771131d2010-09-30 21:18:25 +000069/* Handy utility functions used at several places in the code */
Sean Callanana48fe162010-08-11 03:57:18 +000070
71static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000072PrintValue(const Value *value, bool truncate = false)
Sean Callanana48fe162010-08-11 03:57:18 +000073{
74 std::string s;
75 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000076 value->print(rso);
Sean Callanana48fe162010-08-11 03:57:18 +000077 rso.flush();
78 if (truncate)
79 s.resize(s.length() - 1);
80 return s;
81}
82
Sean Callanan771131d2010-09-30 21:18:25 +000083static std::string
Greg Clayton3c7feb42010-11-19 01:05:25 +000084PrintType(const Type *type, bool truncate = false)
Sean Callanan771131d2010-09-30 21:18:25 +000085{
86 std::string s;
87 raw_string_ostream rso(s);
Greg Clayton3c7feb42010-11-19 01:05:25 +000088 type->print(rso);
Sean Callanan771131d2010-09-30 21:18:25 +000089 rso.flush();
90 if (truncate)
91 s.resize(s.length() - 1);
92 return s;
93}
94
Sean Callanan5cf4a1c2010-07-03 01:35:46 +000095IRForTarget::~IRForTarget()
96{
97}
98
Sean Callananc0492742011-05-23 21:40:23 +000099bool
100IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
101{
102 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
103
104 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
105
106 std::string name = llvm_function.getNameStr();
107
108 return true;
109}
110
Sean Callanan82b74c82010-08-12 01:56:52 +0000111bool
Sean Callananc0492742011-05-23 21:40:23 +0000112IRForTarget::HasSideEffects (llvm::Function &llvm_function)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000113{
114 llvm::Function::iterator bbi;
115 BasicBlock::iterator ii;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000116
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000117 for (bbi = llvm_function.begin();
118 bbi != llvm_function.end();
119 ++bbi)
120 {
121 BasicBlock &basic_block = *bbi;
122
123 for (ii = basic_block.begin();
124 ii != basic_block.end();
125 ++ii)
126 {
127 switch (ii->getOpcode())
128 {
129 default:
130 return true;
131 case Instruction::Store:
132 {
133 StoreInst *store_inst = dyn_cast<StoreInst>(ii);
134
135 Value *store_ptr = store_inst->getPointerOperand();
136
Sean Callanan696cf5f2011-05-07 01:06:41 +0000137 std::string ptr_name;
138
139 if (store_ptr->hasName())
140 ptr_name = store_ptr->getNameStr();
141
142 if (isa <AllocaInst> (store_ptr))
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000143 break;
Sean Callanan696cf5f2011-05-07 01:06:41 +0000144
145 if (ptr_name.find("$__lldb_expr_result") != std::string::npos)
146 {
147 if (ptr_name.find("GV") == std::string::npos)
148 m_result_store = store_inst;
149 }
150 else
151 {
152 return true;
153 }
154
155 break;
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000156 }
157 case Instruction::Load:
158 case Instruction::Alloca:
159 case Instruction::GetElementPtr:
Sean Callanan696cf5f2011-05-07 01:06:41 +0000160 case Instruction::BitCast:
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000161 case Instruction::Ret:
Sean Callanan696cf5f2011-05-07 01:06:41 +0000162 case Instruction::ICmp:
163 case Instruction::Br:
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000164 break;
165 }
166 }
167 }
168
169 return false;
170}
171
Sean Callanan696cf5f2011-05-07 01:06:41 +0000172clang::NamedDecl *
Sean Callananc0492742011-05-23 21:40:23 +0000173IRForTarget::DeclForGlobal (GlobalValue *global_val)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000174{
175 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
176
Sean Callananc0492742011-05-23 21:40:23 +0000177 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan696cf5f2011-05-07 01:06:41 +0000178
179 if (!named_metadata)
180 return NULL;
181
182 unsigned num_nodes = named_metadata->getNumOperands();
183 unsigned node_index;
184
185 for (node_index = 0;
186 node_index < num_nodes;
187 ++node_index)
188 {
189 MDNode *metadata_node = named_metadata->getOperand(node_index);
190
191 if (!metadata_node)
192 return NULL;
193
194 if (metadata_node->getNumOperands() != 2)
195 continue;
196
197 if (metadata_node->getOperand(0) != global_val)
198 continue;
199
200 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
201
202 if (!constant_int)
203 return NULL;
204
205 uintptr_t ptr = constant_int->getZExtValue();
206
207 return reinterpret_cast<clang::NamedDecl *>(ptr);
208 }
209
210 return NULL;
211}
212
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000213void
214IRForTarget::MaybeSetConstantResult (llvm::Constant *initializer,
215 const lldb_private::ConstString &name,
216 lldb_private::TypeFromParser type)
217{
Sean Callanan696cf5f2011-05-07 01:06:41 +0000218 if (llvm::ConstantExpr *init_expr = dyn_cast<llvm::ConstantExpr>(initializer))
219 {
220 switch (init_expr->getOpcode())
221 {
222 default:
223 return;
224 case Instruction::IntToPtr:
225 MaybeSetConstantResult (init_expr->getOperand(0), name, type);
226 return;
227 }
228 }
229 else if (llvm::ConstantInt *init_int = dyn_cast<llvm::ConstantInt>(initializer))
230 {
231 m_const_result = m_decl_map->BuildIntegerVariable(name, type, init_int->getValue());
232 }
233}
234
235void
Sean Callananc0492742011-05-23 21:40:23 +0000236IRForTarget::MaybeSetCastResult (lldb_private::TypeFromParser type)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000237{
238 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
239
240 if (!m_result_store)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000241 return;
242
Sean Callanan696cf5f2011-05-07 01:06:41 +0000243 LoadInst *original_load = NULL;
244
245 for (llvm::Value *current_value = m_result_store->getValueOperand(), *next_value;
246 current_value != NULL;
247 current_value = next_value)
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000248 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000249 CastInst *cast_inst = dyn_cast<CastInst>(current_value);
250 LoadInst *load_inst = dyn_cast<LoadInst>(current_value);
251
252 if (cast_inst)
253 {
254 next_value = cast_inst->getOperand(0);
255 }
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000256 else if (load_inst)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000257 {
258 if (isa<LoadInst>(load_inst->getPointerOperand()))
259 {
260 next_value = load_inst->getPointerOperand();
261 }
262 else
263 {
264 original_load = load_inst;
265 break;
266 }
267 }
268 else
269 {
270 return;
271 }
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000272 }
Sean Callanan696cf5f2011-05-07 01:06:41 +0000273
274 Value *loaded_value = original_load->getPointerOperand();
275 GlobalVariable *loaded_global = dyn_cast<GlobalVariable>(loaded_value);
276
277 if (!loaded_global)
278 return;
279
Sean Callananc0492742011-05-23 21:40:23 +0000280 clang::NamedDecl *loaded_decl = DeclForGlobal(loaded_global);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000281
282 if (!loaded_decl)
283 return;
284
285 clang::VarDecl *loaded_var = dyn_cast<clang::VarDecl>(loaded_decl);
286
287 if (!loaded_var)
288 return;
289
290 if (log)
291 {
292 lldb_private::StreamString type_desc_stream;
293 type.DumpTypeDescription(&type_desc_stream);
294
295 log->Printf("Type to cast variable to: \"%s\"", type_desc_stream.GetString().c_str());
296 }
297
298 m_const_result = m_decl_map->BuildCastVariable(m_result_name, loaded_var, type);
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000299}
300
301bool
Sean Callananc0492742011-05-23 21:40:23 +0000302IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanan82b74c82010-08-12 01:56:52 +0000303{
Greg Claytone005f2c2010-11-06 01:53:30 +0000304 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan82b74c82010-08-12 01:56:52 +0000305
Sean Callanane8a59a82010-09-13 21:34:21 +0000306 if (!m_resolve_vars)
307 return true;
308
309 // Find the result variable. If it doesn't exist, we can give up right here.
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000310
Sean Callananc0492742011-05-23 21:40:23 +0000311 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000312
313 const char *result_name = NULL;
314
315 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
316 vi != ve;
317 ++vi)
318 {
Sean Callanan6a925532011-01-13 08:53:35 +0000319 if (strstr(vi->first(), "$__lldb_expr_result_ptr") &&
320 !strstr(vi->first(), "GV"))
321 {
322 result_name = vi->first();
323 m_result_is_pointer = true;
324 break;
325 }
326
Greg Clayton8de27c72010-10-15 22:48:33 +0000327 if (strstr(vi->first(), "$__lldb_expr_result") &&
Sean Callananc04743d2010-09-28 21:13:03 +0000328 !strstr(vi->first(), "GV"))
329 {
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000330 result_name = vi->first();
Sean Callanan6a925532011-01-13 08:53:35 +0000331 m_result_is_pointer = false;
Sean Callananc04743d2010-09-28 21:13:03 +0000332 break;
333 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000334 }
335
336 if (!result_name)
337 {
338 if (log)
339 log->PutCString("Couldn't find result variable");
340
341 return true;
342 }
343
Sean Callananc04743d2010-09-28 21:13:03 +0000344 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000345 log->Printf("Result name: \"%s\"", result_name);
Sean Callananc04743d2010-09-28 21:13:03 +0000346
Sean Callananc0492742011-05-23 21:40:23 +0000347 Value *result_value = m_module->getNamedValue(result_name);
Sean Callanan82b74c82010-08-12 01:56:52 +0000348
349 if (!result_value)
350 {
351 if (log)
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000352 log->PutCString("Result variable had no data");
Sean Callanan6a925532011-01-13 08:53:35 +0000353
Sean Callanan97c924e2011-01-27 01:07:04 +0000354 if (m_error_stream)
355 m_error_stream->Printf("Internal error [IRForTarget]: Result variable's name (%s) exists, but not its definition\n", result_name);
356
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000357 return false;
Sean Callanan82b74c82010-08-12 01:56:52 +0000358 }
Sean Callanane8a59a82010-09-13 21:34:21 +0000359
Sean Callanan82b74c82010-08-12 01:56:52 +0000360 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000361 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sean Callanan82b74c82010-08-12 01:56:52 +0000362
363 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
364
365 if (!result_global)
366 {
367 if (log)
368 log->PutCString("Result variable isn't a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000369
370 if (m_error_stream)
371 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) is defined, but is not a global variable\n", result_name);
372
Sean Callanan82b74c82010-08-12 01:56:52 +0000373 return false;
374 }
375
Sean Callananc0492742011-05-23 21:40:23 +0000376 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000377 if (!result_decl)
Sean Callanan82b74c82010-08-12 01:56:52 +0000378 {
379 if (log)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000380 log->PutCString("Result variable doesn't have a corresponding Decl");
Sean Callanan82b74c82010-08-12 01:56:52 +0000381
Sean Callanan97c924e2011-01-27 01:07:04 +0000382 if (m_error_stream)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000383 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) does not have a corresponding Clang entity\n", result_name);
Sean Callanan97c924e2011-01-27 01:07:04 +0000384
Sean Callanan82b74c82010-08-12 01:56:52 +0000385 return false;
386 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000387
Sean Callanan696cf5f2011-05-07 01:06:41 +0000388 if (log)
Sean Callanan82b74c82010-08-12 01:56:52 +0000389 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000390 std::string decl_desc_str;
391 raw_string_ostream decl_desc_stream(decl_desc_str);
392 result_decl->print(decl_desc_stream);
393 decl_desc_stream.flush();
Sean Callanan82b74c82010-08-12 01:56:52 +0000394
Sean Callanan696cf5f2011-05-07 01:06:41 +0000395 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanan82b74c82010-08-12 01:56:52 +0000396 }
397
Sean Callanan696cf5f2011-05-07 01:06:41 +0000398 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
399 if (!result_var)
Sean Callanan82b74c82010-08-12 01:56:52 +0000400 {
401 if (log)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000402 log->PutCString("Result variable Decl isn't a VarDecl");
Sean Callanan97c924e2011-01-27 01:07:04 +0000403
404 if (m_error_stream)
Sean Callanan696cf5f2011-05-07 01:06:41 +0000405 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s)'s corresponding Clang entity isn't a variable\n", result_name);
Sean Callanan97c924e2011-01-27 01:07:04 +0000406
Sean Callanan82b74c82010-08-12 01:56:52 +0000407 return false;
408 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000409
Sean Callanan82b74c82010-08-12 01:56:52 +0000410 // Get the next available result name from m_decl_map and create the persistent
411 // variable for it
412
Sean Callanan6a925532011-01-13 08:53:35 +0000413 lldb_private::TypeFromParser result_decl_type;
414
Sean Callanan696cf5f2011-05-07 01:06:41 +0000415 // If the result is an Lvalue, it is emitted as a pointer; see
416 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan6a925532011-01-13 08:53:35 +0000417 if (m_result_is_pointer)
418 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000419 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanand5b3c352011-01-27 04:42:51 +0000420 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
421 const clang::PointerType *pointer_pointertype = dyn_cast<clang::PointerType>(pointer_type);
Sean Callanan6a925532011-01-13 08:53:35 +0000422
423 if (!pointer_pointertype)
424 {
425 if (log)
426 log->PutCString("Expected result to have pointer type, but it did not");
Sean Callanan97c924e2011-01-27 01:07:04 +0000427
428 if (m_error_stream)
429 m_error_stream->Printf("Internal error [IRForTarget]: Lvalue result (%s) is not a pointer variable\n", result_name);
430
Sean Callanan6a925532011-01-13 08:53:35 +0000431 return false;
432 }
433
434 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
435
436 result_decl_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
437 &result_decl->getASTContext());
438 }
439 else
440 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000441 result_decl_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Sean Callanan6a925532011-01-13 08:53:35 +0000442 &result_decl->getASTContext());
443 }
444
Sean Callanan696cf5f2011-05-07 01:06:41 +0000445 if (log)
446 {
447 lldb_private::StreamString type_desc_stream;
448 result_decl_type.DumpTypeDescription(&type_desc_stream);
449
450 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetString().c_str());
451 }
452
Sean Callanan6a925532011-01-13 08:53:35 +0000453 m_result_name = m_decl_map->GetPersistentResultName();
Sean Callanan82b74c82010-08-12 01:56:52 +0000454
455 if (log)
Sean Callanan6a925532011-01-13 08:53:35 +0000456 log->Printf("Creating a new result global: \"%s\"", m_result_name.GetCString());
Sean Callanan82b74c82010-08-12 01:56:52 +0000457
458 // Construct a new result global and set up its metadata
459
Sean Callananc0492742011-05-23 21:40:23 +0000460 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanan82b74c82010-08-12 01:56:52 +0000461 result_global->getType()->getElementType(),
462 false, /* not constant */
463 GlobalValue::ExternalLinkage,
464 NULL, /* no initializer */
Sean Callanan6a925532011-01-13 08:53:35 +0000465 m_result_name.GetCString ());
Sean Callanan82b74c82010-08-12 01:56:52 +0000466
467 // It's too late in compilation to create a new VarDecl for this, but we don't
468 // need to. We point the metadata at the old VarDecl. This creates an odd
469 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton8de27c72010-10-15 22:48:33 +0000470 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanan82b74c82010-08-12 01:56:52 +0000471 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
472 // fixed up.
473
Sean Callananc0492742011-05-23 21:40:23 +0000474 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan696cf5f2011-05-07 01:06:41 +0000475 reinterpret_cast<uint64_t>(result_decl),
Sean Callanan82b74c82010-08-12 01:56:52 +0000476 false);
477
478 llvm::Value* values[2];
479 values[0] = new_result_global;
480 values[1] = new_constant_int;
481
Sean Callanan0de254a2011-05-15 22:34:38 +0000482 ArrayRef<Value*> value_ref(values, 2);
483
Sean Callananc0492742011-05-23 21:40:23 +0000484 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
485 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanan82b74c82010-08-12 01:56:52 +0000486 named_metadata->addOperand(persistent_global_md);
487
488 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000489 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan2e2db532010-09-07 22:43:19 +0000490 PrintValue(result_global).c_str(),
Sean Callanan82b74c82010-08-12 01:56:52 +0000491 PrintValue(new_result_global).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000492
493 if (result_global->hasNUses(0))
494 {
495 // We need to synthesize a store for this variable, because otherwise
496 // there's nothing to put into its equivalent persistent variable.
Sean Callanan82b74c82010-08-12 01:56:52 +0000497
Greg Clayton8de27c72010-10-15 22:48:33 +0000498 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan2e2db532010-09-07 22:43:19 +0000499 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
500
501 if (!first_entry_instruction)
502 return false;
503
504 if (!result_global->hasInitializer())
505 {
506 if (log)
507 log->Printf("Couldn't find initializer for unused variable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000508
509 if (m_error_stream)
510 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) has no writes and no initializer\n", result_name);
511
Sean Callanan2e2db532010-09-07 22:43:19 +0000512 return false;
513 }
514
515 Constant *initializer = result_global->getInitializer();
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000516
517 // Here we write the initializer into a result variable assuming it
518 // can be computed statically.
519
520 if (!m_has_side_effects)
521 {
522 MaybeSetConstantResult (initializer,
Sean Callanan6a925532011-01-13 08:53:35 +0000523 m_result_name,
Sean Callanan05a5a1b2010-12-16 03:17:46 +0000524 result_decl_type);
525 }
Sean Callanan2e2db532010-09-07 22:43:19 +0000526
Greg Clayton2c344ca2011-02-05 02:28:58 +0000527 StoreInst *synthesized_store = new StoreInst(initializer,
528 new_result_global,
529 first_entry_instruction);
Sean Callanan2e2db532010-09-07 22:43:19 +0000530
531 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000532 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan2e2db532010-09-07 22:43:19 +0000533 }
534 else
535 {
Sean Callanan696cf5f2011-05-07 01:06:41 +0000536 if (!m_has_side_effects && lldb_private::ClangASTContext::IsPointerType (result_decl_type.GetOpaqueQualType()))
537 {
Sean Callananc0492742011-05-23 21:40:23 +0000538 MaybeSetCastResult (result_decl_type);
Sean Callanan696cf5f2011-05-07 01:06:41 +0000539 }
540
Sean Callanan2e2db532010-09-07 22:43:19 +0000541 result_global->replaceAllUsesWith(new_result_global);
542 }
Sean Callanan696cf5f2011-05-07 01:06:41 +0000543
544 if (!m_const_result)
545 m_decl_map->AddPersistentVariable(result_decl,
546 m_result_name,
547 result_decl_type,
548 true,
549 m_result_is_pointer);
Sean Callanan2e2db532010-09-07 22:43:19 +0000550
Sean Callanan82b74c82010-08-12 01:56:52 +0000551 result_global->eraseFromParent();
552
553 return true;
554}
555
Greg Clayton3c7feb42010-11-19 01:05:25 +0000556static void DebugUsers(lldb::LogSP &log, Value *value, uint8_t depth)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000557{
558 if (!depth)
559 return;
560
561 depth--;
562
Greg Clayton3c7feb42010-11-19 01:05:25 +0000563 log->Printf(" <Begin %d users>", value->getNumUses());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000564
Greg Clayton3c7feb42010-11-19 01:05:25 +0000565 for (Value::use_iterator ui = value->use_begin(), ue = value->use_end();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000566 ui != ue;
567 ++ui)
568 {
569 log->Printf(" <Use %p> %s", *ui, PrintValue(*ui).c_str());
570 DebugUsers(log, *ui, depth);
571 }
572
573 log->Printf(" <End uses>");
574}
575
576bool
Sean Callananc0492742011-05-23 21:40:23 +0000577IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Greg Clayton3c7feb42010-11-19 01:05:25 +0000578 llvm::GlobalVariable *cstr,
579 Instruction *FirstEntryInstruction)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000580{
581 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
582
Sean Callananc0492742011-05-23 21:40:23 +0000583 const Type *ns_str_ty = ns_str->getType();
584
585 const Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
586 const IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
587 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
588 const Type *i32_ty = Type::getInt32Ty(m_module->getContext());
589 const Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000590
591 if (!m_CFStringCreateWithBytes)
592 {
593 lldb::addr_t CFStringCreateWithBytes_addr;
594
595 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
596
597 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
598 {
599 if (log)
600 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
601
Sean Callanan97c924e2011-01-27 01:07:04 +0000602 if (m_error_stream)
603 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
604
Sean Callanan6ba533e2010-11-17 23:00:36 +0000605 return false;
606 }
607
608 if (log)
609 log->Printf("Found CFStringCreateWithBytes at 0x%llx", CFStringCreateWithBytes_addr);
610
611 // Build the function type:
612 //
613 // CFStringRef CFStringCreateWithBytes (
614 // CFAllocatorRef alloc,
615 // const UInt8 *bytes,
616 // CFIndex numBytes,
617 // CFStringEncoding encoding,
618 // Boolean isExternalRepresentation
619 // );
620 //
621 // We make the following substitutions:
622 //
623 // CFStringRef -> i8*
624 // CFAllocatorRef -> i8*
625 // UInt8 * -> i8*
626 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
627 // CFStringEncoding -> i32
628 // Boolean -> i8
629
630 std::vector <const Type *> CFSCWB_arg_types;
631 CFSCWB_arg_types.push_back(i8_ptr_ty);
632 CFSCWB_arg_types.push_back(i8_ptr_ty);
633 CFSCWB_arg_types.push_back(intptr_ty);
634 CFSCWB_arg_types.push_back(i32_ty);
635 CFSCWB_arg_types.push_back(i8_ty);
Sean Callananc0492742011-05-23 21:40:23 +0000636 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000637
638 // Build the constant containing the pointer to the function
639 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
640 Constant *CFSCWB_addr_int = ConstantInt::get(intptr_ty, CFStringCreateWithBytes_addr, false);
641 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
642 }
643
Sean Callanan58baaad2011-07-08 00:39:14 +0000644 ConstantArray *string_array = NULL;
Sean Callanan0ece5262011-02-10 22:17:53 +0000645
646 if (cstr)
647 string_array = dyn_cast<ConstantArray>(cstr->getInitializer());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000648
649 SmallVector <Value*, 5> CFSCWB_arguments;
650
651 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan0ece5262011-02-10 22:17:53 +0000652 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
653 Constant *numBytes_arg = ConstantInt::get(intptr_ty, cstr ? string_array->getType()->getNumElements() - 1 : 0, false);
Sean Callanan6ba533e2010-11-17 23:00:36 +0000654 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
655 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
656
657 CFSCWB_arguments.push_back(alloc_arg);
658 CFSCWB_arguments.push_back(bytes_arg);
659 CFSCWB_arguments.push_back(numBytes_arg);
660 CFSCWB_arguments.push_back(encoding_arg);
661 CFSCWB_arguments.push_back(isExternal_arg);
662
663 CallInst *CFSCWB_call = CallInst::Create(m_CFStringCreateWithBytes,
664 CFSCWB_arguments.begin(),
665 CFSCWB_arguments.end(),
666 "CFStringCreateWithBytes",
667 FirstEntryInstruction);
Sean Callananae71e302010-11-18 22:21:58 +0000668
Greg Clayton3c7feb42010-11-19 01:05:25 +0000669 if (!UnfoldConstant(ns_str, CFSCWB_call, FirstEntryInstruction))
Sean Callanan6ba533e2010-11-17 23:00:36 +0000670 {
671 if (log)
672 log->PutCString("Couldn't replace the NSString with the result of the call");
673
Sean Callanan97c924e2011-01-27 01:07:04 +0000674 if (m_error_stream)
675 m_error_stream->Printf("Error [IRForTarget]: Couldn't replace an Objective-C constant string with a dynamic string\n");
676
Sean Callanan6ba533e2010-11-17 23:00:36 +0000677 return false;
678 }
679
Greg Clayton3c7feb42010-11-19 01:05:25 +0000680 ns_str->eraseFromParent();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000681
682 return true;
683}
684
685bool
Sean Callananc0492742011-05-23 21:40:23 +0000686IRForTarget::RewriteObjCConstStrings(Function &llvm_function)
Sean Callanan6ba533e2010-11-17 23:00:36 +0000687{
688 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
689
Sean Callananc0492742011-05-23 21:40:23 +0000690 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sean Callanan6ba533e2010-11-17 23:00:36 +0000691
Greg Clayton3c7feb42010-11-19 01:05:25 +0000692 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000693 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
694
695 if (!FirstEntryInstruction)
696 {
697 if (log)
698 log->PutCString("Couldn't find first instruction for rewritten Objective-C strings");
699
Sean Callanan97c924e2011-01-27 01:07:04 +0000700 if (m_error_stream)
701 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the location for calls to CFStringCreateWithBytes\n");
702
Sean Callanan6ba533e2010-11-17 23:00:36 +0000703 return false;
704 }
705
706 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
707 vi != ve;
708 ++vi)
709 {
710 if (strstr(vi->first(), "_unnamed_cfstring_"))
711 {
712 Value *nsstring_value = vi->second;
713
714 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
715
716 if (!nsstring_global)
717 {
718 if (log)
719 log->PutCString("NSString variable is not a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000720
721 if (m_error_stream)
722 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a global variable\n");
723
Sean Callanan6ba533e2010-11-17 23:00:36 +0000724 return false;
725 }
726
727 if (!nsstring_global->hasInitializer())
728 {
729 if (log)
730 log->PutCString("NSString variable does not have an initializer");
Sean Callanan97c924e2011-01-27 01:07:04 +0000731
732 if (m_error_stream)
733 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have an initializer\n");
734
Sean Callanan6ba533e2010-11-17 23:00:36 +0000735 return false;
736 }
737
738 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
739
740 if (!nsstring_struct)
741 {
742 if (log)
743 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sean Callanan97c924e2011-01-27 01:07:04 +0000744
745 if (m_error_stream)
746 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a structure constant\n");
747
Sean Callanan6ba533e2010-11-17 23:00:36 +0000748 return false;
749 }
750
751 // We expect the following structure:
752 //
753 // struct {
754 // int *isa;
755 // int flags;
756 // char *str;
757 // long length;
758 // };
759
760 if (nsstring_struct->getNumOperands() != 4)
761 {
762 if (log)
763 log->Printf("NSString variable's initializer structure has an unexpected number of members. Should be 4, is %d", nsstring_struct->getNumOperands());
Sean Callanan97c924e2011-01-27 01:07:04 +0000764
765 if (m_error_stream)
766 m_error_stream->Printf("Internal error [IRForTarget]: The struct for an Objective-C constant string is not as expected\n");
767
Sean Callanan6ba533e2010-11-17 23:00:36 +0000768 return false;
769 }
770
771 Constant *nsstring_member = nsstring_struct->getOperand(2);
772
773 if (!nsstring_member)
774 {
775 if (log)
776 log->PutCString("NSString initializer's str element was empty");
Sean Callanan97c924e2011-01-27 01:07:04 +0000777
778 if (m_error_stream)
779 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have a string initializer\n");
780
Sean Callanan6ba533e2010-11-17 23:00:36 +0000781 return false;
782 }
783
784 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
785
786 if (!nsstring_expr)
787 {
788 if (log)
789 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sean Callanan97c924e2011-01-27 01:07:04 +0000790
791 if (m_error_stream)
792 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not constant\n");
793
Sean Callanan6ba533e2010-11-17 23:00:36 +0000794 return false;
795 }
796
797 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
798 {
799 if (log)
800 log->Printf("NSString initializer's str element is not a GetElementPtr expression, it's a %s", nsstring_expr->getOpcodeName());
Sean Callanan97c924e2011-01-27 01:07:04 +0000801
802 if (m_error_stream)
803 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not an array\n");
804
Sean Callanan6ba533e2010-11-17 23:00:36 +0000805 return false;
806 }
807
808 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
809
810 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
811
812 if (!cstr_global)
813 {
814 if (log)
815 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000816
817 if (m_error_stream)
818 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a global\n");
Sean Callanan65e2aee2010-11-20 02:06:01 +0000819
Sean Callanan6ba533e2010-11-17 23:00:36 +0000820 return false;
821 }
822
823 if (!cstr_global->hasInitializer())
824 {
825 if (log)
826 log->PutCString("NSString initializer's str element does not have an initializer");
Sean Callanan97c924e2011-01-27 01:07:04 +0000827
828 if (m_error_stream)
829 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to initialized data\n");
830
Sean Callanan6ba533e2010-11-17 23:00:36 +0000831 return false;
832 }
Sean Callanan0ece5262011-02-10 22:17:53 +0000833
834 /*
Sean Callanan6ba533e2010-11-17 23:00:36 +0000835 if (!cstr_array)
836 {
837 if (log)
838 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sean Callanan97c924e2011-01-27 01:07:04 +0000839
840 if (m_error_stream)
841 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to an array\n");
842
Sean Callanan6ba533e2010-11-17 23:00:36 +0000843 return false;
844 }
845
846 if (!cstr_array->isCString())
847 {
848 if (log)
849 log->PutCString("NSString initializer's str element is not a C string array");
Sean Callanan97c924e2011-01-27 01:07:04 +0000850
851 if (m_error_stream)
852 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a C string\n");
853
Sean Callanan6ba533e2010-11-17 23:00:36 +0000854 return false;
855 }
Sean Callanan0ece5262011-02-10 22:17:53 +0000856 */
857
858 ConstantArray *cstr_array = dyn_cast<ConstantArray>(cstr_global->getInitializer());
Sean Callanan6ba533e2010-11-17 23:00:36 +0000859
860 if (log)
Sean Callanan0ece5262011-02-10 22:17:53 +0000861 {
862 if (cstr_array)
863 log->Printf("Found NSString constant %s, which contains \"%s\"", vi->first(), cstr_array->getAsString().c_str());
864 else
865 log->Printf("Found NSString constant %s, which contains \"\"", vi->first());
866 }
867
868 if (!cstr_array)
869 cstr_global = NULL;
Sean Callanan6ba533e2010-11-17 23:00:36 +0000870
Sean Callananc0492742011-05-23 21:40:23 +0000871 if (!RewriteObjCConstString(nsstring_global, cstr_global, FirstEntryInstruction))
Sean Callanan97c924e2011-01-27 01:07:04 +0000872 {
Sean Callanan6ba533e2010-11-17 23:00:36 +0000873 if (log)
874 log->PutCString("Error rewriting the constant string");
Sean Callanan97c924e2011-01-27 01:07:04 +0000875
876 // We don't print an error message here because RewriteObjCConstString has done so for us.
877
Sean Callanan6ba533e2010-11-17 23:00:36 +0000878 return false;
879 }
Sean Callanan6ba533e2010-11-17 23:00:36 +0000880 }
881 }
882
883 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
884 vi != ve;
885 ++vi)
886 {
887 if (!strcmp(vi->first(), "__CFConstantStringClassReference"))
888 {
889 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
890
891 if (!gv)
892 {
893 if (log)
894 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sean Callanan97c924e2011-01-27 01:07:04 +0000895
896 if (m_error_stream)
897 m_error_stream->Printf("Internal error [IRForTarget]: Found a CFConstantStringClassReference, but it is not a global object\n");
898
Sean Callanan6ba533e2010-11-17 23:00:36 +0000899 return false;
900 }
901
902 gv->eraseFromParent();
903
904 break;
905 }
906 }
907
908 return true;
909}
910
Greg Clayton3c7feb42010-11-19 01:05:25 +0000911static bool IsObjCSelectorRef (Value *value)
Sean Callananf5857a02010-07-31 01:32:05 +0000912{
Greg Clayton3c7feb42010-11-19 01:05:25 +0000913 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sean Callananf5857a02010-07-31 01:32:05 +0000914
Greg Clayton3c7feb42010-11-19 01:05:25 +0000915 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
Sean Callananf5857a02010-07-31 01:32:05 +0000916 return false;
917
918 return true;
919}
920
Sean Callanan97c924e2011-01-27 01:07:04 +0000921// This function does not report errors; its callers are responsible.
Sean Callananf5857a02010-07-31 01:32:05 +0000922bool
Sean Callananc0492742011-05-23 21:40:23 +0000923IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callananf5857a02010-07-31 01:32:05 +0000924{
Greg Claytone005f2c2010-11-06 01:53:30 +0000925 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +0000926
927 LoadInst *load = dyn_cast<LoadInst>(selector_load);
928
929 if (!load)
930 return false;
931
932 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
933 //
934 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
935 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
936 //
937 // where %obj is the object pointer and %tmp is the selector.
938 //
Greg Clayton3c7feb42010-11-19 01:05:25 +0000939 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
940 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sean Callananf5857a02010-07-31 01:32:05 +0000941
942 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
943
944 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
945
946 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
947 return false;
948
949 Constant *osr_initializer = _objc_selector_references_->getInitializer();
950
951 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
952
953 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
954 return false;
955
956 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
957
958 if (!osr_initializer_base)
959 return false;
960
961 // Find the string's initializer (a ConstantArray) and get the string from it
962
963 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
964
965 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
966 return false;
967
968 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
969
970 ConstantArray *omvn_initializer_array = dyn_cast<ConstantArray>(omvn_initializer);
971
972 if (!omvn_initializer_array->isString())
973 return false;
974
975 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
976
977 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000978 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sean Callananf5857a02010-07-31 01:32:05 +0000979
980 // Construct a call to sel_registerName
981
982 if (!m_sel_registerName)
983 {
Greg Claytonb5037af2010-11-15 01:47:11 +0000984 lldb::addr_t sel_registerName_addr;
Sean Callananf5857a02010-07-31 01:32:05 +0000985
Greg Clayton8de27c72010-10-15 22:48:33 +0000986 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytonb5037af2010-11-15 01:47:11 +0000987 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callananf5857a02010-07-31 01:32:05 +0000988 return false;
989
Sean Callananc2c6f772010-10-26 00:31:56 +0000990 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +0000991 log->Printf("Found sel_registerName at 0x%llx", sel_registerName_addr);
Sean Callananc2c6f772010-10-26 00:31:56 +0000992
Sean Callananf5857a02010-07-31 01:32:05 +0000993 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
994
995 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callananc0492742011-05-23 21:40:23 +0000996 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callananf5857a02010-07-31 01:32:05 +0000997 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callananc0492742011-05-23 21:40:23 +0000998 const Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sean Callananf5857a02010-07-31 01:32:05 +0000999
1000 std::vector <const Type *> srN_arg_types;
Sean Callananc0492742011-05-23 21:40:23 +00001001 srN_arg_types.push_back(Type::getInt8PtrTy(m_module->getContext()));
Sean Callananf5857a02010-07-31 01:32:05 +00001002 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
1003
1004 // Build the constant containing the pointer to the function
Sean Callananc0492742011-05-23 21:40:23 +00001005 const IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1006 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananf5857a02010-07-31 01:32:05 +00001007 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Greg Claytonb5037af2010-11-15 01:47:11 +00001008 Constant *srN_addr_int = ConstantInt::get(intptr_ty, sel_registerName_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +00001009 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1010 }
1011
1012 SmallVector <Value*, 1> srN_arguments;
1013
Sean Callananc0492742011-05-23 21:40:23 +00001014 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sean Callananf5857a02010-07-31 01:32:05 +00001015
1016 srN_arguments.push_back(omvn_pointer);
1017
1018 CallInst *srN_call = CallInst::Create(m_sel_registerName,
1019 srN_arguments.begin(),
1020 srN_arguments.end(),
Sean Callanan6ba533e2010-11-17 23:00:36 +00001021 "sel_registerName",
Sean Callananf5857a02010-07-31 01:32:05 +00001022 selector_load);
1023
1024 // Replace the load with the call in all users
1025
1026 selector_load->replaceAllUsesWith(srN_call);
1027
1028 selector_load->eraseFromParent();
1029
1030 return true;
1031}
1032
1033bool
Sean Callananc0492742011-05-23 21:40:23 +00001034IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callananf5857a02010-07-31 01:32:05 +00001035{
Greg Claytone005f2c2010-11-06 01:53:30 +00001036 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananf5857a02010-07-31 01:32:05 +00001037
1038 BasicBlock::iterator ii;
1039
1040 typedef SmallVector <Instruction*, 2> InstrList;
1041 typedef InstrList::iterator InstrIterator;
1042
1043 InstrList selector_loads;
1044
Greg Clayton3c7feb42010-11-19 01:05:25 +00001045 for (ii = basic_block.begin();
1046 ii != basic_block.end();
Sean Callananf5857a02010-07-31 01:32:05 +00001047 ++ii)
1048 {
1049 Instruction &inst = *ii;
1050
1051 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton3c7feb42010-11-19 01:05:25 +00001052 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callananf5857a02010-07-31 01:32:05 +00001053 selector_loads.push_back(&inst);
1054 }
1055
1056 InstrIterator iter;
1057
1058 for (iter = selector_loads.begin();
1059 iter != selector_loads.end();
1060 ++iter)
1061 {
Sean Callananc0492742011-05-23 21:40:23 +00001062 if (!RewriteObjCSelector(*iter))
Sean Callananf5857a02010-07-31 01:32:05 +00001063 {
Sean Callanan97c924e2011-01-27 01:07:04 +00001064 if (m_error_stream)
1065 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't change a static reference to an Objective-C selector to a dynamic reference\n");
1066
Enrico Granata4c3fb4b2011-07-19 18:03:25 +00001067 if (log)
Sean Callananf5857a02010-07-31 01:32:05 +00001068 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sean Callanan97c924e2011-01-27 01:07:04 +00001069
Sean Callananf5857a02010-07-31 01:32:05 +00001070 return false;
1071 }
1072 }
1073
1074 return true;
1075}
1076
Sean Callanan97c924e2011-01-27 01:07:04 +00001077// This function does not report errors; its callers are responsible.
Sean Callanana48fe162010-08-11 03:57:18 +00001078bool
Sean Callananc0492742011-05-23 21:40:23 +00001079IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanana48fe162010-08-11 03:57:18 +00001080{
Sean Callanan97678d12011-01-13 21:23:32 +00001081 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1082
Sean Callanana48fe162010-08-11 03:57:18 +00001083 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1084
1085 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1086
1087 if (!alloc_md || !alloc_md->getNumOperands())
1088 return false;
1089
1090 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
1091
1092 if (!constant_int)
1093 return false;
1094
1095 // We attempt to register this as a new persistent variable with the DeclMap.
1096
1097 uintptr_t ptr = constant_int->getZExtValue();
1098
Sean Callanan82b74c82010-08-12 01:56:52 +00001099 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sean Callanana48fe162010-08-11 03:57:18 +00001100
Sean Callanan82b74c82010-08-12 01:56:52 +00001101 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1102 &decl->getASTContext());
1103
Greg Clayton8de27c72010-10-15 22:48:33 +00001104 StringRef decl_name (decl->getName());
1105 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan6a925532011-01-13 08:53:35 +00001106 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanana48fe162010-08-11 03:57:18 +00001107 return false;
1108
Sean Callananc0492742011-05-23 21:40:23 +00001109 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanan97678d12011-01-13 21:23:32 +00001110 alloc->getType(),
Sean Callanana48fe162010-08-11 03:57:18 +00001111 false, /* not constant */
1112 GlobalValue::ExternalLinkage,
1113 NULL, /* no initializer */
1114 alloc->getName().str().c_str());
1115
1116 // What we're going to do here is make believe this was a regular old external
1117 // variable. That means we need to make the metadata valid.
1118
Sean Callananc0492742011-05-23 21:40:23 +00001119 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanana48fe162010-08-11 03:57:18 +00001120
1121 llvm::Value* values[2];
1122 values[0] = persistent_global;
1123 values[1] = constant_int;
Sean Callanan0de254a2011-05-15 22:34:38 +00001124
1125 ArrayRef<llvm::Value*> value_ref(values, 2);
Sean Callanana48fe162010-08-11 03:57:18 +00001126
Sean Callananc0492742011-05-23 21:40:23 +00001127 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanana48fe162010-08-11 03:57:18 +00001128 named_metadata->addOperand(persistent_global_md);
1129
Sean Callanan97678d12011-01-13 21:23:32 +00001130 // Now, since the variable is a pointer variable, we will drop in a load of that
1131 // pointer variable.
1132
1133 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
1134
1135 if (log)
1136 log->Printf("Replacing \"%s\" with \"%s\"",
1137 PrintValue(alloc).c_str(),
1138 PrintValue(persistent_load).c_str());
1139
1140 alloc->replaceAllUsesWith(persistent_load);
Sean Callanana48fe162010-08-11 03:57:18 +00001141 alloc->eraseFromParent();
1142
1143 return true;
1144}
1145
1146bool
Sean Callananc0492742011-05-23 21:40:23 +00001147IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanana48fe162010-08-11 03:57:18 +00001148{
Sean Callanane8a59a82010-09-13 21:34:21 +00001149 if (!m_resolve_vars)
1150 return true;
1151
Greg Claytone005f2c2010-11-06 01:53:30 +00001152 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanana48fe162010-08-11 03:57:18 +00001153
1154 BasicBlock::iterator ii;
1155
1156 typedef SmallVector <Instruction*, 2> InstrList;
1157 typedef InstrList::iterator InstrIterator;
1158
1159 InstrList pvar_allocs;
1160
Greg Clayton3c7feb42010-11-19 01:05:25 +00001161 for (ii = basic_block.begin();
1162 ii != basic_block.end();
Sean Callanana48fe162010-08-11 03:57:18 +00001163 ++ii)
1164 {
1165 Instruction &inst = *ii;
1166
1167 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callanan237e4742011-01-21 22:30:25 +00001168 {
1169 llvm::StringRef alloc_name = alloc->getName();
1170
1171 if (alloc_name.startswith("$") &&
1172 !alloc_name.startswith("$__lldb"))
1173 {
1174 if (alloc_name.find_first_of("0123456789") == 1)
1175 {
1176 if (log)
1177 log->Printf("Rejecting a numeric persistent variable.");
1178
Sean Callanan97c924e2011-01-27 01:07:04 +00001179 if (m_error_stream)
1180 m_error_stream->Printf("Error [IRForTarget]: Names starting with $0, $1, ... are reserved for use as result names\n");
1181
Sean Callanan237e4742011-01-21 22:30:25 +00001182 return false;
1183 }
1184
Sean Callanana48fe162010-08-11 03:57:18 +00001185 pvar_allocs.push_back(alloc);
Sean Callanan237e4742011-01-21 22:30:25 +00001186 }
1187 }
Sean Callanana48fe162010-08-11 03:57:18 +00001188 }
1189
1190 InstrIterator iter;
1191
1192 for (iter = pvar_allocs.begin();
1193 iter != pvar_allocs.end();
1194 ++iter)
1195 {
Sean Callananc0492742011-05-23 21:40:23 +00001196 if (!RewritePersistentAlloc(*iter))
Sean Callanana48fe162010-08-11 03:57:18 +00001197 {
Sean Callanan97c924e2011-01-27 01:07:04 +00001198 if (m_error_stream)
1199 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
1200
Enrico Granata4c3fb4b2011-07-19 18:03:25 +00001201 if (log)
Sean Callanana48fe162010-08-11 03:57:18 +00001202 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sean Callanan97c924e2011-01-27 01:07:04 +00001203
Sean Callanana48fe162010-08-11 03:57:18 +00001204 return false;
1205 }
1206 }
1207
1208 return true;
1209}
1210
Sean Callanan97c924e2011-01-27 01:07:04 +00001211// This function does not report errors; its callers are responsible.
Sean Callanan8bce6652010-07-13 21:41:46 +00001212bool
Sean Callananc0492742011-05-23 21:40:23 +00001213IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001214{
Greg Claytone005f2c2010-11-06 01:53:30 +00001215 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan48443652010-12-02 19:47:57 +00001216
1217 if (log)
Sean Callananb9f09a62010-12-06 22:16:55 +00001218 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sean Callananf5857a02010-07-31 01:32:05 +00001219
Greg Clayton8de27c72010-10-15 22:48:33 +00001220 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callananbc2928a2010-08-03 00:23:29 +00001221 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001222 switch (constant_expr->getOpcode())
Sean Callananbc2928a2010-08-03 00:23:29 +00001223 {
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001224 default:
1225 break;
1226 case Instruction::GetElementPtr:
1227 case Instruction::BitCast:
Sean Callananbc2928a2010-08-03 00:23:29 +00001228 Value *s = constant_expr->getOperand(0);
Sean Callananc0492742011-05-23 21:40:23 +00001229 if (!MaybeHandleVariable(s))
Sean Callanan48443652010-12-02 19:47:57 +00001230 return false;
Sean Callananbc2928a2010-08-03 00:23:29 +00001231 }
1232 }
Sean Callananf921cf52010-12-03 19:51:05 +00001233 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +00001234 {
Sean Callananc0492742011-05-23 21:40:23 +00001235 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001236
Sean Callananf5857a02010-07-31 01:32:05 +00001237 if (!named_decl)
1238 {
Greg Clayton3c7feb42010-11-19 01:05:25 +00001239 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callananf5857a02010-07-31 01:32:05 +00001240 return true;
1241
Sean Callanan7cd46742010-12-06 00:56:39 +00001242 if (!global_variable->hasExternalLinkage())
1243 return true;
1244
Sean Callananf5857a02010-07-31 01:32:05 +00001245 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001246 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00001247
Sean Callananf5857a02010-07-31 01:32:05 +00001248 return false;
1249 }
1250
Greg Clayton8de27c72010-10-15 22:48:33 +00001251 std::string name (named_decl->getName().str());
Sean Callanan810f22d2010-07-16 00:09:46 +00001252
Sean Callanan771131d2010-09-30 21:18:25 +00001253 void *opaque_type = NULL;
Sean Callananf328c9f2010-07-20 23:31:16 +00001254 clang::ASTContext *ast_context = NULL;
Sean Callanan810f22d2010-07-16 00:09:46 +00001255
1256 if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
Sean Callananf328c9f2010-07-20 23:31:16 +00001257 {
Sean Callanan771131d2010-09-30 21:18:25 +00001258 opaque_type = value_decl->getType().getAsOpaquePtr();
Sean Callananf328c9f2010-07-20 23:31:16 +00001259 ast_context = &value_decl->getASTContext();
1260 }
Sean Callanan810f22d2010-07-16 00:09:46 +00001261 else
Sean Callananf328c9f2010-07-20 23:31:16 +00001262 {
Sean Callanan810f22d2010-07-16 00:09:46 +00001263 return false;
Sean Callananf328c9f2010-07-20 23:31:16 +00001264 }
Sean Callanan771131d2010-09-30 21:18:25 +00001265
Sean Callanan6a925532011-01-13 08:53:35 +00001266 clang::QualType qual_type;
Sean Callanan58baaad2011-07-08 00:39:14 +00001267 const Type *value_type = NULL;
Sean Callanan6a925532011-01-13 08:53:35 +00001268
Sean Callanan97678d12011-01-13 21:23:32 +00001269 if (name[0] == '$')
Sean Callanan6a925532011-01-13 08:53:35 +00001270 {
1271 // The $__lldb_expr_result name indicates the the return value has allocated as
1272 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1273 // accesses to this static variable need to be redirected to the result of dereferencing
1274 // a pointer that is passed in as one of the arguments.
1275 //
1276 // Consequently, when reporting the size of the type, we report a pointer type pointing
1277 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanan97678d12011-01-13 21:23:32 +00001278 //
1279 // We also do this for any user-declared persistent variables.
Sean Callananf328c9f2010-07-20 23:31:16 +00001280
Sean Callanan6a925532011-01-13 08:53:35 +00001281 qual_type = ast_context->getPointerType(clang::QualType::getFromOpaquePtr(opaque_type));
1282 value_type = PointerType::get(global_variable->getType(), 0);
1283 }
1284 else
1285 {
1286 qual_type = clang::QualType::getFromOpaquePtr(opaque_type);
1287 value_type = global_variable->getType();
1288 }
Sean Callanan771131d2010-09-30 21:18:25 +00001289
1290 size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
1291 off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
Sean Callanan3aa7da52010-12-13 22:46:15 +00001292
Sean Callanan771131d2010-09-30 21:18:25 +00001293 if (log)
Sean Callanan97678d12011-01-13 21:23:32 +00001294 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %d, align %d]",
Sean Callanan771131d2010-09-30 21:18:25 +00001295 name.c_str(),
1296 qual_type.getAsString().c_str(),
1297 PrintType(value_type).c_str(),
1298 value_size,
1299 value_alignment);
Sean Callanan3aa7da52010-12-13 22:46:15 +00001300
Sean Callanan8bce6652010-07-13 21:41:46 +00001301
Sean Callanan8c127202010-08-23 23:09:38 +00001302 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton8de27c72010-10-15 22:48:33 +00001303 lldb_private::ConstString (name.c_str()),
1304 llvm_value_ptr,
Sean Callananba992c52010-07-27 02:07:53 +00001305 value_size,
1306 value_alignment))
Sean Callanan8bce6652010-07-13 21:41:46 +00001307 return false;
1308 }
Sean Callananf3143b72010-12-03 03:02:31 +00001309 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanan48443652010-12-02 19:47:57 +00001310 {
1311 if (log)
1312 log->Printf("Function pointers aren't handled right now");
1313
1314 return false;
1315 }
Sean Callanan8bce6652010-07-13 21:41:46 +00001316
1317 return true;
1318}
1319
Sean Callanan97c924e2011-01-27 01:07:04 +00001320// This function does not report errors; its callers are responsible.
Sean Callanan8bce6652010-07-13 21:41:46 +00001321bool
Sean Callananc0492742011-05-23 21:40:23 +00001322IRForTarget::HandleSymbol (Value *symbol)
Sean Callanan81974962011-05-08 02:21:26 +00001323{
Sean Callananc7674af2011-01-17 23:42:46 +00001324 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1325
1326 lldb_private::ConstString name(symbol->getName().str().c_str());
1327
Greg Claytoncbc07cf2011-06-23 04:25:29 +00001328 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name);
Sean Callananc7674af2011-01-17 23:42:46 +00001329
Greg Claytoncbc07cf2011-06-23 04:25:29 +00001330 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc7674af2011-01-17 23:42:46 +00001331 {
1332 if (log)
1333 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
1334
1335 return false;
1336 }
1337
1338 if (log)
1339 log->Printf("Found \"%s\" at 0x%llx", name.GetCString(), symbol_addr);
1340
1341 const Type *symbol_type = symbol->getType();
1342
Sean Callananc0492742011-05-23 21:40:23 +00001343 const IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1344 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callananc7674af2011-01-17 23:42:46 +00001345
1346 Constant *symbol_addr_int = ConstantInt::get(intptr_ty, symbol_addr, false);
1347
1348 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1349
1350 if (log)
1351 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
1352
1353 symbol->replaceAllUsesWith(symbol_addr_ptr);
1354
1355 return true;
1356}
1357
1358bool
Sean Callananc0492742011-05-23 21:40:23 +00001359IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanandc27aba2010-10-05 22:26:43 +00001360{
Sean Callanan48443652010-12-02 19:47:57 +00001361 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1362
1363 if (log)
1364 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sean Callanandc27aba2010-10-05 22:26:43 +00001365
Sean Callanan6ba533e2010-11-17 23:00:36 +00001366 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanandc27aba2010-10-05 22:26:43 +00001367 op_index < num_ops;
1368 ++op_index)
Sean Callananc0492742011-05-23 21:40:23 +00001369 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan97c924e2011-01-27 01:07:04 +00001370 {
1371 if (m_error_stream)
1372 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite one of the arguments of a function call.\n");
1373
Sean Callanandc27aba2010-10-05 22:26:43 +00001374 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001375 }
1376
Sean Callanandc27aba2010-10-05 22:26:43 +00001377 return true;
1378}
1379
1380bool
Sean Callananc0492742011-05-23 21:40:23 +00001381IRForTarget::MaybeHandleCall (CallInst *llvm_call_inst)
Sean Callananba992c52010-07-27 02:07:53 +00001382{
Greg Claytone005f2c2010-11-06 01:53:30 +00001383 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananba992c52010-07-27 02:07:53 +00001384
Greg Clayton8de27c72010-10-15 22:48:33 +00001385 Function *fun = llvm_call_inst->getCalledFunction();
Sean Callananba992c52010-07-27 02:07:53 +00001386
1387 if (fun == NULL)
Sean Callanan65af7342010-09-08 20:04:08 +00001388 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001389 Value *val = llvm_call_inst->getCalledValue();
Sean Callanan65af7342010-09-08 20:04:08 +00001390
1391 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(val);
Sean Callanand5b3c352011-01-27 04:42:51 +00001392 LoadInst *load_inst = dyn_cast<LoadInst>(val);
Sean Callanan65af7342010-09-08 20:04:08 +00001393
1394 if (const_expr && const_expr->getOpcode() == Instruction::BitCast)
1395 {
1396 fun = dyn_cast<Function>(const_expr->getOperand(0));
1397
1398 if (!fun)
Sean Callanan97c924e2011-01-27 01:07:04 +00001399 {
1400 if (m_error_stream)
1401 m_error_stream->Printf("Internal error [IRForTarget]: Called entity is a cast of something not a function\n");
1402
Sean Callanan48443652010-12-02 19:47:57 +00001403 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001404 }
Sean Callanan65af7342010-09-08 20:04:08 +00001405 }
Sean Callananc4217a62010-12-06 23:53:20 +00001406 else if (const_expr && const_expr->getOpcode() == Instruction::IntToPtr)
1407 {
1408 return true; // already resolved
1409 }
Sean Callanand5b3c352011-01-27 04:42:51 +00001410 else if (load_inst)
1411 {
1412 return true; // virtual method call
1413 }
Sean Callanan65af7342010-09-08 20:04:08 +00001414 else
1415 {
Sean Callanan97c924e2011-01-27 01:07:04 +00001416 if (m_error_stream)
1417 m_error_stream->Printf("Internal error [IRForTarget]: Called entity is not a function\n");
1418
Sean Callanan48443652010-12-02 19:47:57 +00001419 return false;
Sean Callanan65af7342010-09-08 20:04:08 +00001420 }
1421 }
Sean Callananba992c52010-07-27 02:07:53 +00001422
Greg Clayton8de27c72010-10-15 22:48:33 +00001423 lldb_private::ConstString str;
Sean Callananc04743d2010-09-28 21:13:03 +00001424
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001425 if (fun->isIntrinsic())
Sean Callananc04743d2010-09-28 21:13:03 +00001426 {
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001427 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sean Callananc04743d2010-09-28 21:13:03 +00001428
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001429 switch (intrinsic_id)
1430 {
1431 default:
1432 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001433 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00001434
1435 if (m_error_stream)
1436 m_error_stream->Printf("Internal error [IRForTarget]: Call to unhandled compiler intrinsic '%s'\n", Intrinsic::getName(intrinsic_id).c_str());
1437
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001438 return false;
1439 case Intrinsic::memcpy:
Greg Clayton8de27c72010-10-15 22:48:33 +00001440 {
1441 static lldb_private::ConstString g_memcpy_str ("memcpy");
1442 str = g_memcpy_str;
1443 }
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001444 break;
1445 }
Sean Callananc04743d2010-09-28 21:13:03 +00001446
Greg Clayton8de27c72010-10-15 22:48:33 +00001447 if (log && str)
Greg Claytonb5037af2010-11-15 01:47:11 +00001448 log->Printf("Resolved intrinsic name \"%s\"", str.GetCString());
Sean Callanan3cb1fd82010-09-28 23:55:00 +00001449 }
1450 else
1451 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001452 str.SetCStringWithLength (fun->getName().data(), fun->getName().size());
Sean Callananc04743d2010-09-28 21:13:03 +00001453 }
1454
Sean Callananc0492742011-05-23 21:40:23 +00001455 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
Greg Claytonb5037af2010-11-15 01:47:11 +00001456 lldb::addr_t fun_addr = LLDB_INVALID_ADDRESS;
Sean Callananf5857a02010-07-31 01:32:05 +00001457 Value **fun_value_ptr = NULL;
Sean Callananba992c52010-07-27 02:07:53 +00001458
Sean Callananf5857a02010-07-31 01:32:05 +00001459 if (fun_decl)
Sean Callananba992c52010-07-27 02:07:53 +00001460 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001461 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_value_ptr, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +00001462 {
Sean Callanan92aa6662010-09-07 21:49:41 +00001463 fun_value_ptr = NULL;
1464
Greg Clayton8de27c72010-10-15 22:48:33 +00001465 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callanan92aa6662010-09-07 21:49:41 +00001466 {
1467 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001468 log->Printf("Function \"%s\" had no address", str.GetCString());
Sean Callanan92aa6662010-09-07 21:49:41 +00001469
Sean Callanan97c924e2011-01-27 01:07:04 +00001470 if (m_error_stream)
1471 m_error_stream->Printf("Error [IRForTarget]: Call to a function '%s' that is not present in the target\n", str.GetCString());
1472
Sean Callanan92aa6662010-09-07 21:49:41 +00001473 return false;
1474 }
Sean Callananf5857a02010-07-31 01:32:05 +00001475 }
1476 }
1477 else
1478 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001479 if (!m_decl_map->GetFunctionAddress (str, fun_addr))
Sean Callananf5857a02010-07-31 01:32:05 +00001480 {
1481 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001482 log->Printf ("Metadataless function \"%s\" had no address", str.GetCString());
Sean Callanan97c924e2011-01-27 01:07:04 +00001483
1484 if (m_error_stream)
1485 m_error_stream->Printf("Error [IRForTarget]: Call to a symbol-only function '%s' that is not present in the target\n", str.GetCString());
1486
1487 return false;
Sean Callananf5857a02010-07-31 01:32:05 +00001488 }
Sean Callananba992c52010-07-27 02:07:53 +00001489 }
1490
1491 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001492 log->Printf("Found \"%s\" at 0x%llx", str.GetCString(), fun_addr);
Sean Callananba992c52010-07-27 02:07:53 +00001493
Sean Callanan58baaad2011-07-08 00:39:14 +00001494 Value *fun_addr_ptr = NULL;
Sean Callananf5857a02010-07-31 01:32:05 +00001495
1496 if (!fun_value_ptr || !*fun_value_ptr)
Sean Callanan02fbafa2010-07-27 21:39:39 +00001497 {
Sean Callananc0492742011-05-23 21:40:23 +00001498 const IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
1499 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
Sean Callanan92aa6662010-09-07 21:49:41 +00001500 const FunctionType *fun_ty = fun->getFunctionType();
Sean Callanan02fbafa2010-07-27 21:39:39 +00001501 PointerType *fun_ptr_ty = PointerType::getUnqual(fun_ty);
1502 Constant *fun_addr_int = ConstantInt::get(intptr_ty, fun_addr, false);
Sean Callananf5857a02010-07-31 01:32:05 +00001503 fun_addr_ptr = ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
1504
1505 if (fun_value_ptr)
1506 *fun_value_ptr = fun_addr_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001507 }
Sean Callananf5857a02010-07-31 01:32:05 +00001508
1509 if (fun_value_ptr)
1510 fun_addr_ptr = *fun_value_ptr;
Sean Callanan02fbafa2010-07-27 21:39:39 +00001511
Greg Clayton8de27c72010-10-15 22:48:33 +00001512 llvm_call_inst->setCalledFunction(fun_addr_ptr);
Sean Callanan02fbafa2010-07-27 21:39:39 +00001513
Sean Callananc0492742011-05-23 21:40:23 +00001514 ConstantArray *func_name = (ConstantArray*)ConstantArray::get(m_module->getContext(), str.GetCString());
Sean Callanane8a59a82010-09-13 21:34:21 +00001515
1516 Value *values[1];
1517 values[0] = func_name;
Sean Callanan0de254a2011-05-15 22:34:38 +00001518 ArrayRef<Value*> value_ref(values, 1);
1519
Sean Callananc0492742011-05-23 21:40:23 +00001520 MDNode *func_metadata = MDNode::get(m_module->getContext(), value_ref);
Sean Callanane8a59a82010-09-13 21:34:21 +00001521
Greg Clayton8de27c72010-10-15 22:48:33 +00001522 llvm_call_inst->setMetadata("lldb.call.realName", func_metadata);
Sean Callanane8a59a82010-09-13 21:34:21 +00001523
1524 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001525 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 +00001526
Sean Callananba992c52010-07-27 02:07:53 +00001527 return true;
1528}
1529
1530bool
Sean Callananc0492742011-05-23 21:40:23 +00001531IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sean Callanan8bce6652010-07-13 21:41:46 +00001532{
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001533 /////////////////////////////////////////////////////////////////////////
1534 // Prepare the current basic block for execution in the remote process
1535 //
1536
Sean Callanan02fbafa2010-07-27 21:39:39 +00001537 BasicBlock::iterator ii;
Sean Callanan8bce6652010-07-13 21:41:46 +00001538
Greg Clayton3c7feb42010-11-19 01:05:25 +00001539 for (ii = basic_block.begin();
1540 ii != basic_block.end();
Sean Callanan8bce6652010-07-13 21:41:46 +00001541 ++ii)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001542 {
Sean Callanan8bce6652010-07-13 21:41:46 +00001543 Instruction &inst = *ii;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001544
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001545 CallInst *call = dyn_cast<CallInst>(&inst);
Sean Callananba992c52010-07-27 02:07:53 +00001546
Sean Callanan97c924e2011-01-27 01:07:04 +00001547 // MaybeHandleCall handles error reporting; we are silent here
Sean Callananc0492742011-05-23 21:40:23 +00001548 if (call && !MaybeHandleCall(call))
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001549 return false;
Sean Callanan48443652010-12-02 19:47:57 +00001550
Sean Callanan97c924e2011-01-27 01:07:04 +00001551 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callananc0492742011-05-23 21:40:23 +00001552 if (call && !MaybeHandleCallArguments(call))
Sean Callanan48443652010-12-02 19:47:57 +00001553 return false;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00001554 }
1555
1556 return true;
1557}
1558
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001559bool
Sean Callananc0492742011-05-23 21:40:23 +00001560IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001561{
Sean Callananae71e302010-11-18 22:21:58 +00001562 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1563
Sean Callananc0492742011-05-23 21:40:23 +00001564 for (Module::global_iterator global = m_module->global_begin(), end = m_module->global_end();
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001565 global != end;
1566 ++global)
1567 {
Greg Clayton3c7feb42010-11-19 01:05:25 +00001568 if (log)
1569 log->Printf("Examining %s, DeclForGlobalValue returns %p",
1570 (*global).getName().str().c_str(),
Sean Callananc0492742011-05-23 21:40:23 +00001571 DeclForGlobal(global));
Greg Clayton3c7feb42010-11-19 01:05:25 +00001572
Sean Callananc7674af2011-01-17 23:42:46 +00001573 if ((*global).getName().str().find("OBJC_IVAR") == 0)
1574 {
Sean Callananc0492742011-05-23 21:40:23 +00001575 if (!HandleSymbol(global))
Sean Callanan97c924e2011-01-27 01:07:04 +00001576 {
1577 if (m_error_stream)
1578 m_error_stream->Printf("Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol %s\n", (*global).getName().str().c_str());
1579
Sean Callananc7674af2011-01-17 23:42:46 +00001580 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001581 }
Sean Callananc7674af2011-01-17 23:42:46 +00001582 }
Sean Callananc0492742011-05-23 21:40:23 +00001583 else if (DeclForGlobal(global))
Sean Callananc7674af2011-01-17 23:42:46 +00001584 {
Sean Callananc0492742011-05-23 21:40:23 +00001585 if (!MaybeHandleVariable (global))
Sean Callanan97c924e2011-01-27 01:07:04 +00001586 {
1587 if (m_error_stream)
1588 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite external variable %s\n", (*global).getName().str().c_str());
1589
Sean Callananc7674af2011-01-17 23:42:46 +00001590 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00001591 }
Sean Callananc7674af2011-01-17 23:42:46 +00001592 }
Sean Callanan1d1b39c2010-11-08 00:31:32 +00001593 }
1594
1595 return true;
1596}
1597
Sean Callananc0492742011-05-23 21:40:23 +00001598bool
1599IRForTarget::ReplaceStrings ()
1600{
1601 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1602
1603 if (!m_data_allocator)
1604 return true; // hope for the best; some clients may not want static allocation!
1605
1606 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
1607
1608 OffsetsTy offsets;
1609
1610 for (Module::global_iterator gi = m_module->global_begin(), ge = m_module->global_end();
1611 gi != ge;
1612 ++gi)
1613 {
1614 GlobalVariable *gv = gi;
1615
1616 if (!gv->hasInitializer())
1617 continue;
1618
1619 Constant *gc = gv->getInitializer();
1620
1621 ConstantArray *gc_array = dyn_cast<ConstantArray>(gc);
1622
1623 if (!gc_array)
1624 continue;
1625
1626 if (!gc_array->isCString())
1627 continue;
1628
1629 if (log)
1630 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
1631
1632 std::string str = gc_array->getAsString();
1633
1634 offsets[gv] = m_data_allocator->GetStream().GetSize();
1635
1636 m_data_allocator->GetStream().Write(str.c_str(), str.length() + 1);
1637 }
1638
1639 const Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
1640
1641 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1642 oi != oe;
1643 ++oi)
1644 {
1645 GlobalVariable *gv = oi->first;
1646 size_t offset = oi->second;
1647
1648 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
1649
1650 if (log)
1651 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
1652
1653 for (GlobalVariable::use_iterator ui = gv->use_begin(), ue = gv->use_end();
1654 ui != ue;
1655 ++ui)
1656 {
1657 if (log)
1658 log->Printf("Found use %s", PrintValue(*ui).c_str());
1659
1660 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(*ui);
1661 StoreInst *store_inst = dyn_cast<StoreInst>(*ui);
1662
1663 if (const_expr)
1664 {
1665 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1666 {
1667 if (log)
1668 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
1669
1670 return false;
1671 }
1672
1673 const_expr->replaceAllUsesWith(new_initializer);
1674 }
1675 else if (store_inst)
1676 {
1677 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
1678
1679 store_inst->setOperand(0, bit_cast);
1680 }
1681 else
1682 {
1683 if (log)
1684 log->Printf("Use (%s) of string variable is neither a constant nor a store", PrintValue(const_expr).c_str());
1685
1686 return false;
1687 }
1688 }
1689
1690 gv->eraseFromParent();
1691 }
1692
1693 return true;
1694}
1695
1696bool
1697IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
1698{
1699 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1700
1701 if (!m_data_allocator)
1702 return true;
1703
1704 typedef SmallVector <Value*, 2> ConstantList;
1705 typedef SmallVector <llvm::Instruction*, 2> UserList;
1706 typedef ConstantList::iterator ConstantIterator;
1707 typedef UserList::iterator UserIterator;
1708
1709 ConstantList static_constants;
1710 UserList static_users;
1711
1712 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
1713 ii != ie;
1714 ++ii)
1715 {
1716 llvm::Instruction &inst = *ii;
1717
1718 for (Instruction::op_iterator oi = inst.op_begin(), oe = inst.op_end();
1719 oi != oe;
1720 ++oi)
1721 {
1722 Value *operand_val = oi->get();
1723
1724 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
1725
1726 if (operand_constant_fp && operand_constant_fp->getType()->isX86_FP80Ty())
1727 {
1728 static_constants.push_back(operand_val);
1729 static_users.push_back(ii);
1730 }
1731 }
1732 }
1733
1734 ConstantIterator constant_iter;
1735 UserIterator user_iter;
Greg Clayton54b38412011-05-24 23:06:02 +00001736
Sean Callananc0492742011-05-23 21:40:23 +00001737 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
1738 constant_iter != static_constants.end();
1739 ++constant_iter, ++user_iter)
1740 {
1741 Value *operand_val = *constant_iter;
1742 llvm::Instruction *inst = *user_iter;
1743
1744 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
1745
1746 if (operand_constant_fp)
1747 {
1748 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
1749 APInt operand_apint = operand_apfloat.bitcastToAPInt();
1750
1751 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
1752 size_t operand_data_size = operand_apint.getBitWidth() / 8;
1753
1754 if (log)
1755 {
1756 std::string s;
1757 raw_string_ostream ss(s);
1758 for (size_t index = 0;
1759 index < operand_data_size;
1760 ++index)
1761 {
1762 ss << (uint32_t)operand_raw_data[index];
1763 ss << " ";
1764 }
1765 ss.flush();
1766
1767 log->Printf("Found ConstantFP with size %d and raw data %s", operand_data_size, s.c_str());
1768 }
1769
1770 lldb_private::DataBufferHeap data(operand_data_size, 0);
1771
1772 if (lldb::endian::InlHostByteOrder() != m_data_allocator->GetStream().GetByteOrder())
1773 {
1774 uint8_t *data_bytes = data.GetBytes();
1775
1776 for (size_t index = 0;
1777 index < operand_data_size;
1778 ++index)
1779 {
1780 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
1781 }
1782 }
1783 else
1784 {
1785 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
1786 }
1787
1788 uint64_t offset = m_data_allocator->GetStream().GetSize();
1789
1790 m_data_allocator->GetStream().Write(data.GetBytes(), operand_data_size);
1791
1792 const llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
1793
1794 Constant *new_pointer = BuildRelocation(fp_ptr_ty, offset);
1795
1796 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
1797
1798 operand_constant_fp->replaceAllUsesWith(fp_load);
1799 }
1800 }
1801
1802 return true;
1803}
1804
Sean Callanan02fbafa2010-07-27 21:39:39 +00001805static bool isGuardVariableRef(Value *V)
Sean Callanan45839272010-07-24 01:37:44 +00001806{
Sean Callanan58baaad2011-07-08 00:39:14 +00001807 Constant *Old = NULL;
Sean Callanan45839272010-07-24 01:37:44 +00001808
Sean Callanan6ba533e2010-11-17 23:00:36 +00001809 if (!(Old = dyn_cast<Constant>(V)))
Sean Callanan45839272010-07-24 01:37:44 +00001810 return false;
1811
Sean Callanan58baaad2011-07-08 00:39:14 +00001812 ConstantExpr *CE = NULL;
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001813
1814 if ((CE = dyn_cast<ConstantExpr>(V)))
1815 {
1816 if (CE->getOpcode() != Instruction::BitCast)
1817 return false;
1818
Sean Callanan6ba533e2010-11-17 23:00:36 +00001819 Old = CE->getOperand(0);
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001820 }
1821
Sean Callanan6ba533e2010-11-17 23:00:36 +00001822 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sean Callanan45839272010-07-24 01:37:44 +00001823
1824 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
1825 return false;
1826
1827 return true;
1828}
1829
Sean Callananc0492742011-05-23 21:40:23 +00001830void
1831IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callanan45839272010-07-24 01:37:44 +00001832{
Sean Callananc0492742011-05-23 21:40:23 +00001833 Constant* zero(ConstantInt::get(Type::getInt8Ty(m_module->getContext()), 0, true));
Sean Callanan45839272010-07-24 01:37:44 +00001834
1835 Value::use_iterator ui;
1836
1837 for (ui = guard_load->use_begin();
1838 ui != guard_load->use_end();
1839 ++ui)
Sean Callananb5b749c2010-07-27 01:17:28 +00001840 {
Greg Clayton6e713402010-07-30 20:30:44 +00001841 if (isa<Constant>(*ui))
Sean Callananb5b749c2010-07-27 01:17:28 +00001842 {
1843 // do nothing for the moment
1844 }
1845 else
1846 {
1847 ui->replaceUsesOfWith(guard_load, zero);
1848 }
1849 }
Sean Callanan45839272010-07-24 01:37:44 +00001850
1851 guard_load->eraseFromParent();
1852}
1853
1854static void ExciseGuardStore(Instruction* guard_store)
1855{
1856 guard_store->eraseFromParent();
1857}
1858
1859bool
Sean Callananc0492742011-05-23 21:40:23 +00001860IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sean Callanan45839272010-07-24 01:37:44 +00001861{
1862 ///////////////////////////////////////////////////////
1863 // Eliminate any reference to guard variables found.
1864 //
1865
Sean Callanan02fbafa2010-07-27 21:39:39 +00001866 BasicBlock::iterator ii;
Sean Callanan45839272010-07-24 01:37:44 +00001867
Sean Callanan02fbafa2010-07-27 21:39:39 +00001868 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callanan45839272010-07-24 01:37:44 +00001869 typedef InstrList::iterator InstrIterator;
1870
1871 InstrList guard_loads;
1872 InstrList guard_stores;
1873
Greg Clayton3c7feb42010-11-19 01:05:25 +00001874 for (ii = basic_block.begin();
1875 ii != basic_block.end();
Sean Callanan45839272010-07-24 01:37:44 +00001876 ++ii)
1877 {
1878 Instruction &inst = *ii;
1879
1880 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1881 if (isGuardVariableRef(load->getPointerOperand()))
1882 guard_loads.push_back(&inst);
1883
1884 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
1885 if (isGuardVariableRef(store->getPointerOperand()))
1886 guard_stores.push_back(&inst);
1887 }
1888
1889 InstrIterator iter;
1890
1891 for (iter = guard_loads.begin();
1892 iter != guard_loads.end();
1893 ++iter)
Sean Callananc0492742011-05-23 21:40:23 +00001894 TurnGuardLoadIntoZero(*iter);
Sean Callanan45839272010-07-24 01:37:44 +00001895
1896 for (iter = guard_stores.begin();
1897 iter != guard_stores.end();
1898 ++iter)
1899 ExciseGuardStore(*iter);
1900
1901 return true;
1902}
1903
Sean Callanan97c924e2011-01-27 01:07:04 +00001904// This function does not report errors; its callers are responsible.
Sean Callanan6ba533e2010-11-17 23:00:36 +00001905bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00001906IRForTarget::UnfoldConstant(Constant *old_constant, Value *new_constant, Instruction *first_entry_inst)
Sean Callananbafd6852010-07-14 23:40:29 +00001907{
Greg Claytone005f2c2010-11-06 01:53:30 +00001908 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbafd6852010-07-14 23:40:29 +00001909
1910 Value::use_iterator ui;
1911
Sean Callanana48fe162010-08-11 03:57:18 +00001912 SmallVector<User*, 16> users;
1913
1914 // We do this because the use list might change, invalidating our iterator.
1915 // Much better to keep a work list ourselves.
Greg Clayton3c7feb42010-11-19 01:05:25 +00001916 for (ui = old_constant->use_begin();
1917 ui != old_constant->use_end();
Sean Callananbafd6852010-07-14 23:40:29 +00001918 ++ui)
Sean Callanana48fe162010-08-11 03:57:18 +00001919 users.push_back(*ui);
Sean Callananbafd6852010-07-14 23:40:29 +00001920
Johnny Chen2bc9eb32011-07-19 19:48:13 +00001921 for (size_t i = 0;
Sean Callanana48fe162010-08-11 03:57:18 +00001922 i < users.size();
1923 ++i)
1924 {
1925 User *user = users[i];
1926
Sean Callananbafd6852010-07-14 23:40:29 +00001927 if (Constant *constant = dyn_cast<Constant>(user))
1928 {
1929 // synthesize a new non-constant equivalent of the constant
1930
1931 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
1932 {
1933 switch (constant_expr->getOpcode())
1934 {
1935 default:
1936 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001937 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00001938 return false;
1939 case Instruction::BitCast:
1940 {
1941 // UnaryExpr
1942 // OperandList[0] is value
1943
1944 Value *s = constant_expr->getOperand(0);
1945
Greg Clayton3c7feb42010-11-19 01:05:25 +00001946 if (s == old_constant)
1947 s = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00001948
Sean Callananc0492742011-05-23 21:40:23 +00001949 BitCastInst *bit_cast(new BitCastInst(s, constant_expr->getType(), "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00001950
Greg Clayton3c7feb42010-11-19 01:05:25 +00001951 UnfoldConstant(constant_expr, bit_cast, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00001952 }
1953 break;
1954 case Instruction::GetElementPtr:
1955 {
1956 // GetElementPtrConstantExpr
1957 // OperandList[0] is base
1958 // OperandList[1]... are indices
1959
1960 Value *ptr = constant_expr->getOperand(0);
1961
Greg Clayton3c7feb42010-11-19 01:05:25 +00001962 if (ptr == old_constant)
1963 ptr = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00001964
1965 SmallVector<Value*, 16> indices;
1966
1967 unsigned operand_index;
1968 unsigned num_operands = constant_expr->getNumOperands();
1969
1970 for (operand_index = 1;
1971 operand_index < num_operands;
1972 ++operand_index)
1973 {
1974 Value *operand = constant_expr->getOperand(operand_index);
1975
Greg Clayton3c7feb42010-11-19 01:05:25 +00001976 if (operand == old_constant)
1977 operand = new_constant;
Sean Callananbafd6852010-07-14 23:40:29 +00001978
1979 indices.push_back(operand);
1980 }
1981
Greg Clayton3c7feb42010-11-19 01:05:25 +00001982 GetElementPtrInst *get_element_ptr(GetElementPtrInst::Create(ptr, indices.begin(), indices.end(), "", first_entry_inst));
Sean Callananbafd6852010-07-14 23:40:29 +00001983
Greg Clayton3c7feb42010-11-19 01:05:25 +00001984 UnfoldConstant(constant_expr, get_element_ptr, first_entry_inst);
Sean Callananbafd6852010-07-14 23:40:29 +00001985 }
1986 break;
1987 }
1988 }
1989 else
1990 {
1991 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00001992 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callananbafd6852010-07-14 23:40:29 +00001993 return false;
1994 }
1995 }
1996 else
1997 {
1998 // simple fall-through case for non-constants
Greg Clayton3c7feb42010-11-19 01:05:25 +00001999 user->replaceUsesOfWith(old_constant, new_constant);
Sean Callananbafd6852010-07-14 23:40:29 +00002000 }
2001 }
2002
2003 return true;
2004}
2005
Sean Callanan8bce6652010-07-13 21:41:46 +00002006bool
Sean Callananc0492742011-05-23 21:40:23 +00002007IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan8bce6652010-07-13 21:41:46 +00002008{
Sean Callanane8a59a82010-09-13 21:34:21 +00002009 if (!m_resolve_vars)
2010 return true;
2011
Greg Claytone005f2c2010-11-06 01:53:30 +00002012 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8bce6652010-07-13 21:41:46 +00002013
2014 m_decl_map->DoStructLayout();
2015
2016 if (log)
2017 log->Printf("Element arrangement:");
2018
2019 uint32_t num_elements;
2020 uint32_t element_index;
2021
2022 size_t size;
2023 off_t alignment;
2024
2025 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2026 return false;
2027
Greg Clayton3c7feb42010-11-19 01:05:25 +00002028 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sean Callanan8bce6652010-07-13 21:41:46 +00002029
Greg Clayton3c7feb42010-11-19 01:05:25 +00002030 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002031 {
2032 if (m_error_stream)
2033 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes no arguments (should take at least a struct pointer)");
2034
Sean Callanan8bce6652010-07-13 21:41:46 +00002035 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002036 }
2037
Sean Callanan02fbafa2010-07-27 21:39:39 +00002038 Argument *argument = iter;
Sean Callanan8bce6652010-07-13 21:41:46 +00002039
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002040 if (argument->getName().equals("this"))
2041 {
2042 ++iter;
2043
Greg Clayton3c7feb42010-11-19 01:05:25 +00002044 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002045 {
2046 if (m_error_stream)
2047 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'this' argument (should take a struct pointer too)");
2048
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002049 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002050 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002051
2052 argument = iter;
2053 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002054 else if (argument->getName().equals("self"))
2055 {
2056 ++iter;
2057
2058 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002059 {
2060 if (m_error_stream)
2061 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' argument (should take '_cmd' and a struct pointer too)");
2062
Sean Callanan3aa7da52010-12-13 22:46:15 +00002063 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002064 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002065
2066 if (!iter->getName().equals("_cmd"))
Sean Callanan97c924e2011-01-27 01:07:04 +00002067 {
2068 if (m_error_stream)
2069 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes '%s' after 'self' argument (should take '_cmd')", iter->getName().str().c_str());
2070
Sean Callanan3aa7da52010-12-13 22:46:15 +00002071 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002072 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002073
2074 ++iter;
2075
2076 if (iter == llvm_function.getArgumentList().end())
Sean Callanan97c924e2011-01-27 01:07:04 +00002077 {
2078 if (m_error_stream)
2079 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' and '_cmd' arguments (should take a struct pointer too)");
2080
Sean Callanan3aa7da52010-12-13 22:46:15 +00002081 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002082 }
Sean Callanan3aa7da52010-12-13 22:46:15 +00002083
2084 argument = iter;
2085 }
Sean Callanan3c9c5eb2010-09-21 00:44:12 +00002086
Greg Clayton8de27c72010-10-15 22:48:33 +00002087 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan97c924e2011-01-27 01:07:04 +00002088 {
2089 if (m_error_stream)
2090 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes an argument named '%s' instead of the struct pointer", argument->getName().str().c_str());
2091
Sean Callanan8bce6652010-07-13 21:41:46 +00002092 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002093 }
2094
Sean Callanan8bce6652010-07-13 21:41:46 +00002095 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002096 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sean Callanan8bce6652010-07-13 21:41:46 +00002097
Greg Clayton3c7feb42010-11-19 01:05:25 +00002098 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan6ba533e2010-11-17 23:00:36 +00002099 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sean Callanan8bce6652010-07-13 21:41:46 +00002100
Sean Callanan6ba533e2010-11-17 23:00:36 +00002101 if (!FirstEntryInstruction)
Sean Callanan97c924e2011-01-27 01:07:04 +00002102 {
2103 if (m_error_stream)
2104 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the first instruction in the wrapper for use in rewriting");
2105
Sean Callanan8bce6652010-07-13 21:41:46 +00002106 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002107 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002108
Sean Callananc0492742011-05-23 21:40:23 +00002109 LLVMContext &context(m_module->getContext());
Sean Callanan8bce6652010-07-13 21:41:46 +00002110 const IntegerType *offset_type(Type::getInt32Ty(context));
2111
2112 if (!offset_type)
Sean Callanan97c924e2011-01-27 01:07:04 +00002113 {
2114 if (m_error_stream)
2115 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
2116
Sean Callanan8bce6652010-07-13 21:41:46 +00002117 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002118 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002119
2120 for (element_index = 0; element_index < num_elements; ++element_index)
2121 {
Sean Callanan58baaad2011-07-08 00:39:14 +00002122 const clang::NamedDecl *decl = NULL;
2123 Value *value = NULL;
Sean Callanan8bce6652010-07-13 21:41:46 +00002124 off_t offset;
Greg Clayton8de27c72010-10-15 22:48:33 +00002125 lldb_private::ConstString name;
Sean Callanan8bce6652010-07-13 21:41:46 +00002126
Sean Callanan45690fe2010-08-30 22:17:16 +00002127 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan97c924e2011-01-27 01:07:04 +00002128 {
2129 if (m_error_stream)
2130 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
2131
Sean Callanan8bce6652010-07-13 21:41:46 +00002132 return false;
Sean Callanan97c924e2011-01-27 01:07:04 +00002133 }
2134
Sean Callanan8bce6652010-07-13 21:41:46 +00002135 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002136 log->Printf(" \"%s\" [\"%s\"] (\"%s\") placed at %d",
Sean Callanan82b74c82010-08-12 01:56:52 +00002137 value->getName().str().c_str(),
Greg Clayton8de27c72010-10-15 22:48:33 +00002138 name.GetCString(),
Sean Callanan8bce6652010-07-13 21:41:46 +00002139 PrintValue(value, true).c_str(),
2140 offset);
2141
2142 ConstantInt *offset_int(ConstantInt::getSigned(offset_type, offset));
Sean Callanan6ba533e2010-11-17 23:00:36 +00002143 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument, offset_int, "", FirstEntryInstruction);
Sean Callanan6a925532011-01-13 08:53:35 +00002144
Sean Callanan58baaad2011-07-08 00:39:14 +00002145 Value *replacement = NULL;
Sean Callanan8bce6652010-07-13 21:41:46 +00002146
Sean Callanan6a925532011-01-13 08:53:35 +00002147 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2148 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2149 // entry in order to produce the static variable that the AST thinks it is accessing.
2150 if (name == m_result_name && !m_result_is_pointer)
2151 {
2152 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType()->getPointerTo(), "", FirstEntryInstruction);
2153
2154 LoadInst *load = new LoadInst(bit_cast, "", FirstEntryInstruction);
2155
2156 replacement = load;
2157 }
Sean Callananbafd6852010-07-14 23:40:29 +00002158 else
Sean Callanan6a925532011-01-13 08:53:35 +00002159 {
2160 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", FirstEntryInstruction);
2161
2162 replacement = bit_cast;
2163 }
2164
2165 if (Constant *constant = dyn_cast<Constant>(value))
2166 UnfoldConstant(constant, replacement, FirstEntryInstruction);
2167 else
2168 value->replaceAllUsesWith(replacement);
Sean Callananb51ee982010-11-02 23:51:17 +00002169
2170 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2171 var->eraseFromParent();
Sean Callanan8bce6652010-07-13 21:41:46 +00002172 }
2173
2174 if (log)
2175 log->Printf("Total structure [align %d, size %d]", alignment, size);
2176
2177 return true;
2178}
2179
Sean Callananc0492742011-05-23 21:40:23 +00002180llvm::Constant *
2181IRForTarget::BuildRelocation(const llvm::Type *type,
2182 uint64_t offset)
2183{
2184 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2185
2186 const IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
2187 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
2188
2189 llvm::Constant *offset_int = ConstantInt::get(intptr_ty, offset);
2190 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, &offset_int, 1);
2191 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
2192
2193 return reloc_getbitcast;
2194}
2195
2196bool
2197IRForTarget::CompleteDataAllocation ()
2198{
2199 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
2200
2201 if (!m_data_allocator->GetStream().GetSize())
2202 return true;
2203
2204 lldb::addr_t allocation = m_data_allocator->Allocate();
2205
2206 if (log)
2207 {
2208 if (allocation)
2209 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2210 else
2211 log->Printf("Failed to allocate static data");
2212 }
2213
2214 if (!allocation)
2215 return false;
2216
2217 const IntegerType *intptr_ty = Type::getIntNTy(m_module->getContext(),
2218 (m_module->getPointerSize() == Module::Pointer64) ? 64 : 32);
2219
2220 Constant *relocated_addr = ConstantInt::get(intptr_ty, (uint64_t)allocation);
2221 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
2222
2223 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
2224
2225 m_reloc_placeholder->eraseFromParent();
2226
2227 return true;
2228}
2229
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002230bool
Greg Clayton3c7feb42010-11-19 01:05:25 +00002231IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002232{
Greg Claytone005f2c2010-11-06 01:53:30 +00002233 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002234
Sean Callananc0492742011-05-23 21:40:23 +00002235 m_module = &llvm_module;
2236
2237 Function* function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002238
2239 if (!function)
2240 {
2241 if (log)
Greg Claytonb5037af2010-11-15 01:47:11 +00002242 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sean Callanan97c924e2011-01-27 01:07:04 +00002243
2244 if (m_error_stream)
Sean Callananc0492742011-05-23 21:40:23 +00002245 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
Sean Callanan6a925532011-01-13 08:53:35 +00002246
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002247 return false;
2248 }
Sean Callananc0492742011-05-23 21:40:23 +00002249
2250 if (!FixFunctionLinkage (*function))
2251 {
2252 if (log)
2253 log->Printf("Couldn't fix the linkage for the function");
2254
2255 return false;
2256 }
2257
2258 const llvm::Type *intptr_ty = Type::getInt8Ty(m_module->getContext());
2259
2260 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
2261 intptr_ty,
2262 false /* isConstant */,
2263 GlobalVariable::InternalLinkage,
2264 Constant::getNullValue(intptr_ty),
2265 "reloc_placeholder",
2266 NULL /* InsertBefore */,
2267 false /* ThreadLocal */,
2268 0 /* AddressSpace */);
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002269
Sean Callanan02fbafa2010-07-27 21:39:39 +00002270 Function::iterator bbi;
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002271
Sean Callananc0492742011-05-23 21:40:23 +00002272 m_has_side_effects = HasSideEffects(*function);
Sean Callanan05a5a1b2010-12-16 03:17:46 +00002273
Sean Callanan82b74c82010-08-12 01:56:52 +00002274 ////////////////////////////////////////////////////////////
Greg Clayton8de27c72010-10-15 22:48:33 +00002275 // Replace $__lldb_expr_result with a persistent variable
Sean Callanan82b74c82010-08-12 01:56:52 +00002276 //
2277
Sean Callananc0492742011-05-23 21:40:23 +00002278 if (!CreateResultVariable(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002279 {
2280 if (log)
2281 log->Printf("CreateResultVariable() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002282
2283 // CreateResultVariable() reports its own errors, so we don't do so here
2284
Sean Callanan82b74c82010-08-12 01:56:52 +00002285 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002286 }
Sean Callanan82b74c82010-08-12 01:56:52 +00002287
Sean Callanan696cf5f2011-05-07 01:06:41 +00002288 if (m_const_result)
2289 return true;
2290
Sean Callananc0492742011-05-23 21:40:23 +00002291 if (log)
2292 {
2293 std::string s;
2294 raw_string_ostream oss(s);
2295
2296 m_module->print(oss, NULL);
2297
2298 oss.flush();
2299
2300 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2301 }
2302
Sean Callanan6ba533e2010-11-17 23:00:36 +00002303 ///////////////////////////////////////////////////////////////////////////////
2304 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2305 //
Sean Callanan6ba533e2010-11-17 23:00:36 +00002306
Sean Callananc0492742011-05-23 21:40:23 +00002307 if (!RewriteObjCConstStrings(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002308 {
2309 if (log)
2310 log->Printf("RewriteObjCConstStrings() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002311
2312 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
2313
Sean Callanan6ba533e2010-11-17 23:00:36 +00002314 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002315 }
Sean Callanan6ba533e2010-11-17 23:00:36 +00002316
Sean Callananf5857a02010-07-31 01:32:05 +00002317 //////////////////////////////////
2318 // Run basic-block level passes
2319 //
2320
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002321 for (bbi = function->begin();
2322 bbi != function->end();
2323 ++bbi)
2324 {
Sean Callananc0492742011-05-23 21:40:23 +00002325 if (!RemoveGuards(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002326 {
2327 if (log)
2328 log->Printf("RemoveGuards() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002329
2330 // RemoveGuards() reports its own errors, so we don't do so here
2331
Sean Callanan8c127202010-08-23 23:09:38 +00002332 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002333 }
Sean Callanan8c127202010-08-23 23:09:38 +00002334
Sean Callananc0492742011-05-23 21:40:23 +00002335 if (!RewritePersistentAllocs(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002336 {
2337 if (log)
2338 log->Printf("RewritePersistentAllocs() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002339
2340 // RewritePersistentAllocs() reports its own errors, so we don't do so here
2341
Sean Callananf5857a02010-07-31 01:32:05 +00002342 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002343 }
Sean Callananf5857a02010-07-31 01:32:05 +00002344
Sean Callananc0492742011-05-23 21:40:23 +00002345 if (!RewriteObjCSelectors(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002346 {
2347 if (log)
2348 log->Printf("RewriteObjCSelectors() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002349
2350 // RewriteObjCSelectors() reports its own errors, so we don't do so here
2351
Sean Callanana48fe162010-08-11 03:57:18 +00002352 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002353 }
Sean Callanana48fe162010-08-11 03:57:18 +00002354
Sean Callananc0492742011-05-23 21:40:23 +00002355 if (!ResolveCalls(*bbi))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002356 {
2357 if (log)
2358 log->Printf("ResolveCalls() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002359
2360 // ResolveCalls() reports its own errors, so we don't do so here
2361
Sean Callanan8bce6652010-07-13 21:41:46 +00002362 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002363 }
Sean Callananc0492742011-05-23 21:40:23 +00002364
2365 if (!ReplaceStaticLiterals(*bbi))
2366 {
2367 if (log)
2368 log->Printf("ReplaceStaticLiterals() failed");
2369
2370 return false;
2371 }
Sean Callanan8bce6652010-07-13 21:41:46 +00002372 }
2373
Sean Callanan771131d2010-09-30 21:18:25 +00002374 ///////////////////////////////
2375 // Run function-level passes
2376 //
2377
Sean Callananc0492742011-05-23 21:40:23 +00002378 if (!ResolveExternals(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002379 {
2380 if (log)
2381 log->Printf("ResolveExternals() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002382
2383 // ResolveExternals() reports its own errors, so we don't do so here
2384
Sean Callanan1d1b39c2010-11-08 00:31:32 +00002385 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002386 }
Sean Callanan1d1b39c2010-11-08 00:31:32 +00002387
Sean Callananc0492742011-05-23 21:40:23 +00002388 if (!ReplaceVariables(*function))
Sean Callanan3aa7da52010-12-13 22:46:15 +00002389 {
2390 if (log)
2391 log->Printf("ReplaceVariables() failed");
Sean Callanan97c924e2011-01-27 01:07:04 +00002392
2393 // ReplaceVariables() reports its own errors, so we don't do so here
2394
Sean Callanan771131d2010-09-30 21:18:25 +00002395 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +00002396 }
Sean Callanan771131d2010-09-30 21:18:25 +00002397
Sean Callananc0492742011-05-23 21:40:23 +00002398 if (!ReplaceStrings())
2399 {
2400 if (log)
2401 log->Printf("ReplaceStrings() failed");
2402
2403 return false;
2404 }
2405
2406 if (!CompleteDataAllocation())
2407 {
2408 if (log)
2409 log->Printf("CompleteDataAllocation() failed");
2410
2411 return false;
2412 }
2413
Sean Callanan8bce6652010-07-13 21:41:46 +00002414 if (log)
2415 {
Sean Callanan321fe9e2010-07-28 01:00:59 +00002416 std::string s;
2417 raw_string_ostream oss(s);
Sean Callanan8bce6652010-07-13 21:41:46 +00002418
Sean Callananc0492742011-05-23 21:40:23 +00002419 m_module->print(oss, NULL);
Sean Callanan321fe9e2010-07-28 01:00:59 +00002420
2421 oss.flush();
2422
Greg Claytonb5037af2010-11-15 01:47:11 +00002423 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002424 }
2425
2426 return true;
2427}
2428
2429void
Greg Clayton3c7feb42010-11-19 01:05:25 +00002430IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan5cf4a1c2010-07-03 01:35:46 +00002431{
2432}
2433
2434PassManagerType
2435IRForTarget::getPotentialPassManagerType() const
2436{
2437 return PMT_ModulePassManager;
2438}