blob: 5a2f6e83837fe6ca9925b9b3b9a8a871bbea990d [file] [log] [blame]
Sean Callanan3bfdaa22011-09-15 02:13:07 +00001//===-- IRForTarget.cpp -----------------------------------------*- C++ -*-===//
Sean Callanan2ab712f22010-07-03 01:35:46 +00002//
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"
Chandler Carruth1e157582013-01-02 12:20:07 +000013#include "llvm/IR/Constants.h"
14#include "llvm/IR/DataLayout.h"
15#include "llvm/IR/InstrTypes.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/Intrinsics.h"
18#include "llvm/IR/Module.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000019#include "llvm/PassManager.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000020#include "llvm/Transforms/IPO.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000021#include "llvm/IR/ValueSymbolTable.h"
Sean Callanan549c9f72010-07-13 21:41:46 +000022
23#include "clang/AST/ASTContext.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000024
25#include "lldb/Core/dwarf.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000026#include "lldb/Core/ConstString.h"
27#include "lldb/Core/DataBufferHeap.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000028#include "lldb/Core/Log.h"
29#include "lldb/Core/Scalar.h"
30#include "lldb/Core/StreamString.h"
31#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000032#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000033#include "lldb/Expression/IRInterpreter.h"
Sean Callanan79763a42011-05-23 21:40:23 +000034#include "lldb/Host/Endian.h"
Sean Callanan63697e52011-05-07 01:06:41 +000035#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000036#include "lldb/Symbol/ClangASTType.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000037
38#include <map>
39
40using namespace llvm;
41
Sean Callananeaacbc92010-08-18 18:50:51 +000042static char ID;
43
Sean Callanan8dfb68e2013-03-19 00:10:07 +000044IRForTarget::StaticDataAllocator::StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit) :
45 m_execution_unit(execution_unit),
Sean Callanan1582ee62013-04-18 22:06:33 +000046 m_stream_string(lldb_private::Stream::eBinary, execution_unit.GetAddressByteSize(), execution_unit.GetByteOrder()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000047 m_allocation(LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +000048{
49}
50
Sean Callanan1f9db3e2013-06-28 21:44:15 +000051IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker) :
52 m_maker(maker),
53 m_values()
54{
55}
56
57IRForTarget::FunctionValueCache::~FunctionValueCache()
58{
59}
60
61llvm::Value *IRForTarget::FunctionValueCache::GetValue(llvm::Function *function)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000062{
Sean Callanan1f9db3e2013-06-28 21:44:15 +000063 if (!m_values.count(function))
64 {
65 llvm::Value *ret = m_maker(function);
66 m_values[function] = ret;
67 return ret;
68 }
69 return m_values[function];
70}
71
Sean Callanan8dfb68e2013-03-19 00:10:07 +000072lldb::addr_t IRForTarget::StaticDataAllocator::Allocate()
Sean Callanan79763a42011-05-23 21:40:23 +000073{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000074 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000075
Sean Callanan8dfb68e2013-03-19 00:10:07 +000076 if (m_allocation != LLDB_INVALID_ADDRESS)
77 {
78 m_execution_unit.FreeNow(m_allocation);
79 m_allocation = LLDB_INVALID_ADDRESS;
80 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000081
Sean Callanan8dfb68e2013-03-19 00:10:07 +000082 m_allocation = m_execution_unit.WriteNow((const uint8_t*)m_stream_string.GetData(), m_stream_string.GetSize(), err);
83
84 return m_allocation;
Sean Callanan79763a42011-05-23 21:40:23 +000085}
86
Sean Callanan1f9db3e2013-06-28 21:44:15 +000087static llvm::Value *FindEntryInstruction (llvm::Function *function)
88{
89 if (function->empty())
90 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000091
Sean Callanan1f9db3e2013-06-28 21:44:15 +000092 return function->getEntryBlock().getFirstNonPHIOrDbg();
93}
94
Greg Clayton1b95a6f2010-11-19 01:05:25 +000095IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
96 bool resolve_vars,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000097 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +000098 lldb_private::Stream *error_stream,
Greg Clayton1b95a6f2010-11-19 01:05:25 +000099 const char *func_name) :
Sean Callanane2ef6e32010-09-23 03:01:22 +0000100 ModulePass(ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000101 m_resolve_vars(resolve_vars),
102 m_func_name(func_name),
Sean Callanan79763a42011-05-23 21:40:23 +0000103 m_module(NULL),
Johnny Chen44805302011-07-19 19:48:13 +0000104 m_decl_map(decl_map),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000105 m_data_allocator(execution_unit),
Sean Callananafe16a72010-11-17 23:00:36 +0000106 m_CFStringCreateWithBytes(NULL),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000107 m_sel_registerName(NULL),
Sean Callanan439dcae2013-12-20 19:55:02 +0000108 m_intptr_ty(NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000109 m_error_stream(error_stream),
Sean Callanan63697e52011-05-07 01:06:41 +0000110 m_result_store(NULL),
111 m_result_is_pointer(false),
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000112 m_reloc_placeholder(NULL),
113 m_entry_instruction_finder (FindEntryInstruction)
Sean Callanan2ab712f22010-07-03 01:35:46 +0000114{
115}
116
Sean Callanan038df5032010-09-30 21:18:25 +0000117/* Handy utility functions used at several places in the code */
Sean Callanan2235f322010-08-11 03:57:18 +0000118
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000119static std::string
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000120PrintValue(const Value *value, bool truncate = false)
Sean Callanan2235f322010-08-11 03:57:18 +0000121{
122 std::string s;
Jim Ingham28eb5712012-10-12 17:34:26 +0000123 if (value)
124 {
125 raw_string_ostream rso(s);
126 value->print(rso);
127 rso.flush();
128 if (truncate)
129 s.resize(s.length() - 1);
130 }
Sean Callanan2235f322010-08-11 03:57:18 +0000131 return s;
132}
133
Sean Callanan038df5032010-09-30 21:18:25 +0000134static std::string
Greg Clayton57ee3062013-07-11 22:46:58 +0000135PrintType(const llvm::Type *type, bool truncate = false)
Sean Callanan038df5032010-09-30 21:18:25 +0000136{
137 std::string s;
138 raw_string_ostream rso(s);
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000139 type->print(rso);
Sean Callanan038df5032010-09-30 21:18:25 +0000140 rso.flush();
141 if (truncate)
142 s.resize(s.length() - 1);
143 return s;
144}
145
Sean Callanan2ab712f22010-07-03 01:35:46 +0000146IRForTarget::~IRForTarget()
147{
148}
149
Sean Callanan79763a42011-05-23 21:40:23 +0000150bool
151IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
152{
Sean Callanan79763a42011-05-23 21:40:23 +0000153 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000154
Sean Callanan7f27d602011-11-19 02:54:21 +0000155 std::string name = llvm_function.getName().str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Sean Callanan79763a42011-05-23 21:40:23 +0000157 return true;
158}
159
Greg Clayton23f8c952014-03-24 23:10:19 +0000160IRForTarget::LookupResult
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000161IRForTarget::GetFunctionAddress (llvm::Function *fun,
162 uint64_t &fun_addr,
163 lldb_private::ConstString &name,
164 Constant **&value_ptr)
165{
Greg Clayton5160ce52013-03-27 23:08:40 +0000166 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000167
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000168 fun_addr = LLDB_INVALID_ADDRESS;
169 name.Clear();
170 value_ptr = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000171
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000172 if (fun->isIntrinsic())
173 {
174 Intrinsic::ID intrinsic_id = (Intrinsic::ID)fun->getIntrinsicID();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000176 switch (intrinsic_id)
177 {
178 default:
179 if (log)
180 log->Printf("Unresolved intrinsic \"%s\"", Intrinsic::getName(intrinsic_id).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000181
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000182 if (m_error_stream)
183 m_error_stream->Printf("Internal error [IRForTarget]: Call to unhandled compiler intrinsic '%s'\n", Intrinsic::getName(intrinsic_id).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000184
Jason Molenda52d76032014-07-09 01:10:37 +0000185 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000186 case Intrinsic::memcpy:
187 {
188 static lldb_private::ConstString g_memcpy_str ("memcpy");
189 name = g_memcpy_str;
190 }
191 break;
Sean Callanana6cbf062011-11-16 00:20:50 +0000192 case Intrinsic::memset:
193 {
194 static lldb_private::ConstString g_memset_str ("memset");
195 name = g_memset_str;
196 }
197 break;
Greg Clayton23f8c952014-03-24 23:10:19 +0000198 case Intrinsic::dbg_declare:
Sean Callanan131be992014-04-09 00:59:41 +0000199 case Intrinsic::dbg_value:
Greg Clayton23f8c952014-03-24 23:10:19 +0000200 return LookupResult::Ignore;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000201 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000203 if (log && name)
204 log->Printf("Resolved intrinsic name \"%s\"", name.GetCString());
205 }
206 else
207 {
208 name.SetCStringWithLength (fun->getName().data(), fun->getName().size());
209 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000210
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000211 // Find the address of the function.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000212
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000213 clang::NamedDecl *fun_decl = DeclForGlobal (fun);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000214
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000215 if (fun_decl)
216 {
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000217 if (!m_decl_map->GetFunctionInfo (fun_decl, fun_addr))
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000218 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000219 lldb_private::ConstString altnernate_name;
Greg Claytonf0705c82011-10-22 03:33:13 +0000220 bool found_it = m_decl_map->GetFunctionAddress (name, fun_addr);
221 if (!found_it)
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000222 {
Greg Claytonf0705c82011-10-22 03:33:13 +0000223 // Check for an alternate mangling for "std::basic_string<char>"
224 // that is part of the itanium C++ name mangling scheme
225 const char *name_cstr = name.GetCString();
Jim Ingham28eb5712012-10-12 17:34:26 +0000226 if (name_cstr && strncmp(name_cstr, "_ZNKSbIcE", strlen("_ZNKSbIcE")) == 0)
Greg Claytonf0705c82011-10-22 03:33:13 +0000227 {
228 std::string alternate_mangling("_ZNKSs");
229 alternate_mangling.append (name_cstr + strlen("_ZNKSbIcE"));
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000230 altnernate_name.SetCString(alternate_mangling.c_str());
231 found_it = m_decl_map->GetFunctionAddress (altnernate_name, fun_addr);
Greg Claytonf0705c82011-10-22 03:33:13 +0000232 }
233 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Greg Claytonf0705c82011-10-22 03:33:13 +0000235 if (!found_it)
236 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000237 lldb_private::Mangled mangled_name(name);
238 lldb_private::Mangled alt_mangled_name(altnernate_name);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000239 if (log)
Greg Claytonf0705c82011-10-22 03:33:13 +0000240 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000241 if (alt_mangled_name)
242 log->Printf("Function \"%s\" (alternate name \"%s\") has no address",
243 mangled_name.GetName().GetCString(),
244 alt_mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000245 else
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000246 log->Printf("Function \"%s\" had no address",
247 mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000248 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000250 if (m_error_stream)
Greg Claytonf0705c82011-10-22 03:33:13 +0000251 {
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000252 if (alt_mangled_name)
253 m_error_stream->Printf("error: call to a function '%s' (alternate name '%s') that is not present in the target\n",
254 mangled_name.GetName().GetCString(),
255 alt_mangled_name.GetName().GetCString());
Greg Claytonda1eb042013-04-23 21:48:38 +0000256 else if (mangled_name.GetMangledName())
257 m_error_stream->Printf("error: call to a function '%s' ('%s') that is not present in the target\n",
258 mangled_name.GetName().GetCString(),
259 mangled_name.GetMangledName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000260 else
Greg Clayton5e0c5e82012-07-18 20:47:40 +0000261 m_error_stream->Printf("error: call to a function '%s' that is not present in the target\n",
262 mangled_name.GetName().GetCString());
Greg Claytonf0705c82011-10-22 03:33:13 +0000263 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000264 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000265 }
266 }
267 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000268 else
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000269 {
270 if (!m_decl_map->GetFunctionAddress (name, fun_addr))
271 {
272 if (log)
273 log->Printf ("Metadataless function \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000274
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000275 if (m_error_stream)
276 m_error_stream->Printf("Error [IRForTarget]: Call to a symbol-only function '%s' that is not present in the target\n", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000277
Greg Clayton23f8c952014-03-24 23:10:19 +0000278 return LookupResult::Fail;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000279 }
280 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000281
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000282 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000283 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), fun_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000284
Greg Clayton23f8c952014-03-24 23:10:19 +0000285 return LookupResult::Success;
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000286}
287
288llvm::Constant *
289IRForTarget::BuildFunctionPointer (llvm::Type *type,
290 uint64_t ptr)
291{
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000292 PointerType *fun_ptr_ty = PointerType::getUnqual(type);
Sean Callanan439dcae2013-12-20 19:55:02 +0000293 Constant *fun_addr_int = ConstantInt::get(m_intptr_ty, ptr, false);
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000294 return ConstantExpr::getIntToPtr(fun_addr_int, fun_ptr_ty);
295}
296
Sean Callananfc8feb82011-10-31 22:11:40 +0000297void
298IRForTarget::RegisterFunctionMetadata(LLVMContext &context,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000299 llvm::Value *function_ptr,
Sean Callananfc8feb82011-10-31 22:11:40 +0000300 const char *name)
301{
Ed Mastea8553092014-03-10 17:24:16 +0000302 for (llvm::User *user : function_ptr->users())
Sean Callananfc8feb82011-10-31 22:11:40 +0000303 {
Sean Callananfc8feb82011-10-31 22:11:40 +0000304 if (Instruction *user_inst = dyn_cast<Instruction>(user))
305 {
Daniel Maleaf051dbc2013-06-03 20:45:54 +0000306 MDString* md_name = MDString::get(context, StringRef(name));
307
308 MDNode *metadata = MDNode::get(context, md_name);
309
Sean Callananfc8feb82011-10-31 22:11:40 +0000310 user_inst->setMetadata("lldb.call.realName", metadata);
311 }
312 else
313 {
314 RegisterFunctionMetadata (context, user, name);
315 }
316 }
317}
318
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000320IRForTarget::ResolveFunctionPointers(llvm::Module &llvm_module)
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000321{
Greg Clayton5160ce52013-03-27 23:08:40 +0000322 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000323
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000324 for (llvm::Module::iterator fi = llvm_module.begin();
325 fi != llvm_module.end();
326 ++fi)
327 {
328 Function *fun = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000330 bool is_decl = fun->isDeclaration();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000331
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000332 if (log)
Sean Callanan7f27d602011-11-19 02:54:21 +0000333 log->Printf("Examining %s function %s", (is_decl ? "declaration" : "non-declaration"), fun->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000334
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000335 if (!is_decl)
336 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000337
Sean Callanan339f6152014-03-11 19:19:16 +0000338 if (fun->use_empty())
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000339 continue; // ignore
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000340
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000341 uint64_t addr = LLDB_INVALID_ADDRESS;
342 lldb_private::ConstString name;
343 Constant **value_ptr = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000344
Greg Clayton23f8c952014-03-24 23:10:19 +0000345 LookupResult result = GetFunctionAddress(fun,
346 addr,
347 name,
348 value_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Greg Clayton23f8c952014-03-24 23:10:19 +0000350 switch (result)
351 {
352 case LookupResult::Fail:
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000353 return false; // GetFunctionAddress reports its own errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000354
Greg Clayton23f8c952014-03-24 23:10:19 +0000355 case LookupResult::Ignore:
356 break; // Nothing to do
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000357
Greg Clayton23f8c952014-03-24 23:10:19 +0000358 case LookupResult::Success:
359 {
360 Constant *value = BuildFunctionPointer(fun->getFunctionType(), addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000361
Greg Clayton23f8c952014-03-24 23:10:19 +0000362 RegisterFunctionMetadata (llvm_module.getContext(), fun, name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000363
Greg Clayton23f8c952014-03-24 23:10:19 +0000364 if (value_ptr)
365 *value_ptr = value;
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000366
Greg Clayton23f8c952014-03-24 23:10:19 +0000367 // If we are replacing a function with the nobuiltin attribute, it may
368 // be called with the builtin attribute on call sites. Remove any such
369 // attributes since it's illegal to have a builtin call to something
370 // other than a nobuiltin function.
371 if (fun->hasFnAttribute(llvm::Attribute::NoBuiltin)) {
372 llvm::Attribute builtin = llvm::Attribute::get(fun->getContext(), llvm::Attribute::Builtin);
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000373
Greg Clayton23f8c952014-03-24 23:10:19 +0000374 for (auto u : fun->users()) {
375 if (auto call = dyn_cast<CallInst>(u)) {
376 call->removeAttribute(AttributeSet::FunctionIndex, builtin);
377 }
378 }
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000379 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Greg Clayton23f8c952014-03-24 23:10:19 +0000381 fun->replaceAllUsesWith(value);
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000382 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000383 break;
Stefanus Du Toitfc6b7a02013-07-23 21:34:03 +0000384 }
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000385 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000386
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000387 return true;
388}
389
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000390
Sean Callanan63697e52011-05-07 01:06:41 +0000391clang::NamedDecl *
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000392IRForTarget::DeclForGlobal (const GlobalValue *global_val, Module *module)
Sean Callanan63697e52011-05-07 01:06:41 +0000393{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000394 NamedMDNode *named_metadata = module->getNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000395
Sean Callanan63697e52011-05-07 01:06:41 +0000396 if (!named_metadata)
397 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000398
Sean Callanan63697e52011-05-07 01:06:41 +0000399 unsigned num_nodes = named_metadata->getNumOperands();
400 unsigned node_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000401
Sean Callanan63697e52011-05-07 01:06:41 +0000402 for (node_index = 0;
403 node_index < num_nodes;
404 ++node_index)
405 {
Zachary Turner0d594e12014-11-05 18:37:53 +0000406 llvm::MDNode *metadata_node = dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
Sean Callanan63697e52011-05-07 01:06:41 +0000407 if (!metadata_node)
408 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000409
Sean Callanan63697e52011-05-07 01:06:41 +0000410 if (metadata_node->getNumOperands() != 2)
411 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000412
Sean Callanan63697e52011-05-07 01:06:41 +0000413 if (metadata_node->getOperand(0) != global_val)
414 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000415
Sean Callanan63697e52011-05-07 01:06:41 +0000416 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000417
Sean Callanan63697e52011-05-07 01:06:41 +0000418 if (!constant_int)
419 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000420
Sean Callanan63697e52011-05-07 01:06:41 +0000421 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000422
Sean Callanan63697e52011-05-07 01:06:41 +0000423 return reinterpret_cast<clang::NamedDecl *>(ptr);
424 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425
Sean Callanan63697e52011-05-07 01:06:41 +0000426 return NULL;
427}
428
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000429clang::NamedDecl *
430IRForTarget::DeclForGlobal (GlobalValue *global_val)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000431{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000432 return DeclForGlobal(global_val, m_module);
433}
434
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000435bool
Sean Callanan79763a42011-05-23 21:40:23 +0000436IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanand1e5b432010-08-12 01:56:52 +0000437{
Greg Clayton5160ce52013-03-27 23:08:40 +0000438 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000439
Sean Callanan9e6ed532010-09-13 21:34:21 +0000440 if (!m_resolve_vars)
441 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000442
Sean Callanan9e6ed532010-09-13 21:34:21 +0000443 // Find the result variable. If it doesn't exist, we can give up right here.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000444
Sean Callanan79763a42011-05-23 21:40:23 +0000445 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000446
Sean Callanancc427fa2011-07-30 02:42:06 +0000447 std::string result_name_str;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000448 const char *result_name = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Sean Callananfc55f5d2010-09-21 00:44:12 +0000450 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
451 vi != ve;
452 ++vi)
453 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000454 result_name_str = vi->first().str();
455 const char *value_name = result_name_str.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000456
Sean Callanancc427fa2011-07-30 02:42:06 +0000457 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000458 strncmp(value_name, "_ZGV", 4))
Sean Callanan92adcac2011-01-13 08:53:35 +0000459 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000460 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000461 m_result_is_pointer = true;
462 break;
463 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000464
Sean Callanancc427fa2011-07-30 02:42:06 +0000465 if (strstr(value_name, "$__lldb_expr_result") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000466 strncmp(value_name, "_ZGV", 4))
Sean Callanan46ae9e52010-09-28 21:13:03 +0000467 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000468 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000469 m_result_is_pointer = false;
Sean Callanan46ae9e52010-09-28 21:13:03 +0000470 break;
471 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000472 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000473
Sean Callananfc55f5d2010-09-21 00:44:12 +0000474 if (!result_name)
475 {
476 if (log)
477 log->PutCString("Couldn't find result variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000478
Richard Mitton00dec202013-10-11 19:44:23 +0000479 return true;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000480 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000481
Sean Callanan46ae9e52010-09-28 21:13:03 +0000482 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000483 log->Printf("Result name: \"%s\"", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000484
Sean Callanan79763a42011-05-23 21:40:23 +0000485 Value *result_value = m_module->getNamedValue(result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000486
Sean Callanand1e5b432010-08-12 01:56:52 +0000487 if (!result_value)
488 {
489 if (log)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000490 log->PutCString("Result variable had no data");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000491
Sean Callanan3989fb92011-01-27 01:07:04 +0000492 if (m_error_stream)
493 m_error_stream->Printf("Internal error [IRForTarget]: Result variable's name (%s) exists, but not its definition\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000494
Sean Callananfc55f5d2010-09-21 00:44:12 +0000495 return false;
Sean Callanand1e5b432010-08-12 01:56:52 +0000496 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000497
Sean Callanand1e5b432010-08-12 01:56:52 +0000498 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000499 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000500
Sean Callanand1e5b432010-08-12 01:56:52 +0000501 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000502
Sean Callanand1e5b432010-08-12 01:56:52 +0000503 if (!result_global)
504 {
505 if (log)
506 log->PutCString("Result variable isn't a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000507
Sean Callanan3989fb92011-01-27 01:07:04 +0000508 if (m_error_stream)
509 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) is defined, but is not a global variable\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callanand1e5b432010-08-12 01:56:52 +0000511 return false;
512 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000513
Sean Callanan79763a42011-05-23 21:40:23 +0000514 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan63697e52011-05-07 01:06:41 +0000515 if (!result_decl)
Sean Callanand1e5b432010-08-12 01:56:52 +0000516 {
517 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000518 log->PutCString("Result variable doesn't have a corresponding Decl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000519
Sean Callanan3989fb92011-01-27 01:07:04 +0000520 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000521 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) does not have a corresponding Clang entity\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000522
Sean Callanand1e5b432010-08-12 01:56:52 +0000523 return false;
524 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000525
Sean Callanan63697e52011-05-07 01:06:41 +0000526 if (log)
Sean Callanand1e5b432010-08-12 01:56:52 +0000527 {
Sean Callanan63697e52011-05-07 01:06:41 +0000528 std::string decl_desc_str;
529 raw_string_ostream decl_desc_stream(decl_desc_str);
530 result_decl->print(decl_desc_stream);
531 decl_desc_stream.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000532
Sean Callanan63697e52011-05-07 01:06:41 +0000533 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanand1e5b432010-08-12 01:56:52 +0000534 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000535
Sean Callanan63697e52011-05-07 01:06:41 +0000536 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
537 if (!result_var)
Sean Callanand1e5b432010-08-12 01:56:52 +0000538 {
539 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000540 log->PutCString("Result variable Decl isn't a VarDecl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000541
Sean Callanan3989fb92011-01-27 01:07:04 +0000542 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000543 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s)'s corresponding Clang entity isn't a variable\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000544
Sean Callanand1e5b432010-08-12 01:56:52 +0000545 return false;
546 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000547
Sean Callanand1e5b432010-08-12 01:56:52 +0000548 // Get the next available result name from m_decl_map and create the persistent
549 // variable for it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000550
Sean Callanan63697e52011-05-07 01:06:41 +0000551 // If the result is an Lvalue, it is emitted as a pointer; see
552 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan92adcac2011-01-13 08:53:35 +0000553 if (m_result_is_pointer)
554 {
Sean Callanan63697e52011-05-07 01:06:41 +0000555 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanan78e37602011-01-27 04:42:51 +0000556 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000557
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000558 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
559 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000560
Sean Callanan5780f9d2011-12-08 19:04:34 +0000561 if (pointer_pointertype)
562 {
563 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000564
Sean Callanan5780f9d2011-12-08 19:04:34 +0000565 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
566 &result_decl->getASTContext());
567 }
568 else if (pointer_objcobjpointertype)
569 {
570 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000571
Sean Callanan5780f9d2011-12-08 19:04:34 +0000572 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
573 &result_decl->getASTContext());
574 }
575 else
Sean Callanan92adcac2011-01-13 08:53:35 +0000576 {
577 if (log)
578 log->PutCString("Expected result to have pointer type, but it did not");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000579
Sean Callanan3989fb92011-01-27 01:07:04 +0000580 if (m_error_stream)
581 m_error_stream->Printf("Internal error [IRForTarget]: Lvalue result (%s) is not a pointer variable\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000582
Sean Callanan92adcac2011-01-13 08:53:35 +0000583 return false;
584 }
Sean Callanan92adcac2011-01-13 08:53:35 +0000585 }
586 else
587 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000588 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Sean Callanan00f43622011-11-18 03:28:09 +0000589 &result_decl->getASTContext());
590 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000591
Greg Clayton57ee3062013-07-11 22:46:58 +0000592 if (m_result_type.GetBitSize() == 0)
Sean Callanan00f43622011-11-18 03:28:09 +0000593 {
594 lldb_private::StreamString type_desc_stream;
595 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000596
Sean Callanan00f43622011-11-18 03:28:09 +0000597 if (log)
598 log->Printf("Result type has size 0");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000599
Sean Callanan00f43622011-11-18 03:28:09 +0000600 if (m_error_stream)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000601 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
Sean Callanan00f43622011-11-18 03:28:09 +0000602 type_desc_stream.GetData());
Sean Callanan960534c2011-12-21 23:44:05 +0000603 return false;
Sean Callanan92adcac2011-01-13 08:53:35 +0000604 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000605
Sean Callanan63697e52011-05-07 01:06:41 +0000606 if (log)
607 {
608 lldb_private::StreamString type_desc_stream;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000609 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000610
Sean Callanan00f43622011-11-18 03:28:09 +0000611 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
Sean Callanan63697e52011-05-07 01:06:41 +0000612 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000613
Sean Callanan1582ee62013-04-18 22:06:33 +0000614 m_result_name = lldb_private::ConstString("$RESULT_NAME");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000615
Sean Callanand1e5b432010-08-12 01:56:52 +0000616 if (log)
Greg Claytonfaac1112013-03-14 18:31:44 +0000617 log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000618 m_result_name.GetCString(),
Greg Clayton57ee3062013-07-11 22:46:58 +0000619 m_result_type.GetByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000620
Sean Callanand1e5b432010-08-12 01:56:52 +0000621 // Construct a new result global and set up its metadata
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000622
623 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanand1e5b432010-08-12 01:56:52 +0000624 result_global->getType()->getElementType(),
625 false, /* not constant */
626 GlobalValue::ExternalLinkage,
627 NULL, /* no initializer */
Sean Callanan92adcac2011-01-13 08:53:35 +0000628 m_result_name.GetCString ());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000629
Sean Callanand1e5b432010-08-12 01:56:52 +0000630 // It's too late in compilation to create a new VarDecl for this, but we don't
631 // need to. We point the metadata at the old VarDecl. This creates an odd
632 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton7b462cc2010-10-15 22:48:33 +0000633 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanand1e5b432010-08-12 01:56:52 +0000634 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
635 // fixed up.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000636
Sean Callanan79763a42011-05-23 21:40:23 +0000637 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan63697e52011-05-07 01:06:41 +0000638 reinterpret_cast<uint64_t>(result_decl),
Sean Callanand1e5b432010-08-12 01:56:52 +0000639 false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000640
Sean Callanand1e5b432010-08-12 01:56:52 +0000641 llvm::Value* values[2];
642 values[0] = new_result_global;
643 values[1] = new_constant_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000644
Sean Callanand12cf8bb2011-05-15 22:34:38 +0000645 ArrayRef<Value*> value_ref(values, 2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000646
Sean Callanan79763a42011-05-23 21:40:23 +0000647 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
648 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanand1e5b432010-08-12 01:56:52 +0000649 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000650
Sean Callanand1e5b432010-08-12 01:56:52 +0000651 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000652 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan1e87fff2010-09-07 22:43:19 +0000653 PrintValue(result_global).c_str(),
Sean Callanand1e5b432010-08-12 01:56:52 +0000654 PrintValue(new_result_global).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000655
Sean Callanan339f6152014-03-11 19:19:16 +0000656 if (result_global->use_empty())
Sean Callanan1e87fff2010-09-07 22:43:19 +0000657 {
658 // We need to synthesize a store for this variable, because otherwise
659 // there's nothing to put into its equivalent persistent variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000660
Greg Clayton7b462cc2010-10-15 22:48:33 +0000661 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000662 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000663
Sean Callanan1e87fff2010-09-07 22:43:19 +0000664 if (!first_entry_instruction)
665 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000666
Sean Callanan1e87fff2010-09-07 22:43:19 +0000667 if (!result_global->hasInitializer())
668 {
669 if (log)
670 log->Printf("Couldn't find initializer for unused variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000671
Sean Callanan3989fb92011-01-27 01:07:04 +0000672 if (m_error_stream)
673 m_error_stream->Printf("Internal error [IRForTarget]: Result variable (%s) has no writes and no initializer\n", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000674
Sean Callanan1e87fff2010-09-07 22:43:19 +0000675 return false;
676 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000677
Sean Callanan1e87fff2010-09-07 22:43:19 +0000678 Constant *initializer = result_global->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000679
Greg Clayton9c139312011-02-05 02:28:58 +0000680 StoreInst *synthesized_store = new StoreInst(initializer,
681 new_result_global,
682 first_entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000683
Sean Callanan1e87fff2010-09-07 22:43:19 +0000684 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000685 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000686 }
687 else
688 {
689 result_global->replaceAllUsesWith(new_result_global);
690 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000691
Sean Callanan1582ee62013-04-18 22:06:33 +0000692 if (!m_decl_map->AddPersistentVariable(result_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000693 m_result_name,
Sean Callanan1582ee62013-04-18 22:06:33 +0000694 m_result_type,
695 true,
696 m_result_is_pointer))
697 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000698
Sean Callanand1e5b432010-08-12 01:56:52 +0000699 result_global->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000700
Sean Callanand1e5b432010-08-12 01:56:52 +0000701 return true;
702}
703
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000704bool
Sean Callanan79763a42011-05-23 21:40:23 +0000705IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000706 llvm::GlobalVariable *cstr)
Sean Callananafe16a72010-11-17 23:00:36 +0000707{
Greg Clayton5160ce52013-03-27 23:08:40 +0000708 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000709
Sean Callanancc427fa2011-07-30 02:42:06 +0000710 Type *ns_str_ty = ns_str->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000711
Sean Callanancc427fa2011-07-30 02:42:06 +0000712 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +0000713 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
714 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000715
Sean Callananafe16a72010-11-17 23:00:36 +0000716 if (!m_CFStringCreateWithBytes)
717 {
718 lldb::addr_t CFStringCreateWithBytes_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000719
Sean Callananafe16a72010-11-17 23:00:36 +0000720 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000721
Sean Callananafe16a72010-11-17 23:00:36 +0000722 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
723 {
724 if (log)
725 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000726
Sean Callanan3989fb92011-01-27 01:07:04 +0000727 if (m_error_stream)
728 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000729
Sean Callananafe16a72010-11-17 23:00:36 +0000730 return false;
731 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000732
Sean Callananafe16a72010-11-17 23:00:36 +0000733 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000734 log->Printf("Found CFStringCreateWithBytes at 0x%" PRIx64, CFStringCreateWithBytes_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000735
Sean Callananafe16a72010-11-17 23:00:36 +0000736 // Build the function type:
737 //
738 // CFStringRef CFStringCreateWithBytes (
739 // CFAllocatorRef alloc,
740 // const UInt8 *bytes,
741 // CFIndex numBytes,
742 // CFStringEncoding encoding,
743 // Boolean isExternalRepresentation
744 // );
745 //
746 // We make the following substitutions:
747 //
748 // CFStringRef -> i8*
749 // CFAllocatorRef -> i8*
750 // UInt8 * -> i8*
751 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
752 // CFStringEncoding -> i32
753 // Boolean -> i8
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000754
Sean Callanancc427fa2011-07-30 02:42:06 +0000755 Type *arg_type_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000756
Sean Callanancc427fa2011-07-30 02:42:06 +0000757 arg_type_array[0] = i8_ptr_ty;
758 arg_type_array[1] = i8_ptr_ty;
Sean Callanan439dcae2013-12-20 19:55:02 +0000759 arg_type_array[2] = m_intptr_ty;
Sean Callanancc427fa2011-07-30 02:42:06 +0000760 arg_type_array[3] = i32_ty;
761 arg_type_array[4] = i8_ty;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000762
Sean Callanancc427fa2011-07-30 02:42:06 +0000763 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000764
Sean Callanan79763a42011-05-23 21:40:23 +0000765 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000766
Sean Callananafe16a72010-11-17 23:00:36 +0000767 // Build the constant containing the pointer to the function
768 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000769 Constant *CFSCWB_addr_int = ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000770 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
771 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000772
Sean Callanand2b465f2012-02-09 03:22:41 +0000773 ConstantDataSequential *string_array = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000774
Sean Callanan229ce2d2011-02-10 22:17:53 +0000775 if (cstr)
Sean Callanand2b465f2012-02-09 03:22:41 +0000776 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000777
Sean Callananafe16a72010-11-17 23:00:36 +0000778 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000779 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000780 Constant *numBytes_arg = ConstantInt::get(m_intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000781 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
782 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000783
Sean Callanancc427fa2011-07-30 02:42:06 +0000784 Value *argument_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000785
Sean Callanancc427fa2011-07-30 02:42:06 +0000786 argument_array[0] = alloc_arg;
787 argument_array[1] = bytes_arg;
788 argument_array[2] = numBytes_arg;
789 argument_array[3] = encoding_arg;
790 argument_array[4] = isExternal_arg;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000791
Sean Callanancc427fa2011-07-30 02:42:06 +0000792 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000793
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000794 FunctionValueCache CFSCWB_Caller ([this, &CFSCWB_arguments] (llvm::Function *function)->llvm::Value * {
795 return CallInst::Create(m_CFStringCreateWithBytes,
796 CFSCWB_arguments,
797 "CFStringCreateWithBytes",
798 llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function)));
799 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000800
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000801 if (!UnfoldConstant(ns_str, CFSCWB_Caller, m_entry_instruction_finder))
Sean Callananafe16a72010-11-17 23:00:36 +0000802 {
803 if (log)
804 log->PutCString("Couldn't replace the NSString with the result of the call");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000805
Sean Callanan3989fb92011-01-27 01:07:04 +0000806 if (m_error_stream)
807 m_error_stream->Printf("Error [IRForTarget]: Couldn't replace an Objective-C constant string with a dynamic string\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000808
Sean Callananafe16a72010-11-17 23:00:36 +0000809 return false;
810 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000811
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000812 ns_str->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000813
Sean Callananafe16a72010-11-17 23:00:36 +0000814 return true;
815}
816
817bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000818IRForTarget::RewriteObjCConstStrings()
Sean Callananafe16a72010-11-17 23:00:36 +0000819{
Greg Clayton5160ce52013-03-27 23:08:40 +0000820 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000821
Sean Callanan79763a42011-05-23 21:40:23 +0000822 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000823
Sean Callananafe16a72010-11-17 23:00:36 +0000824 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
825 vi != ve;
826 ++vi)
827 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000828 std::string value_name = vi->first().str();
829 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000830
Sean Callanancc427fa2011-07-30 02:42:06 +0000831 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callananafe16a72010-11-17 23:00:36 +0000832 {
833 Value *nsstring_value = vi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000834
Sean Callananafe16a72010-11-17 23:00:36 +0000835 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000836
Sean Callananafe16a72010-11-17 23:00:36 +0000837 if (!nsstring_global)
838 {
839 if (log)
840 log->PutCString("NSString variable is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000841
Sean Callanan3989fb92011-01-27 01:07:04 +0000842 if (m_error_stream)
843 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a global variable\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000844
Sean Callananafe16a72010-11-17 23:00:36 +0000845 return false;
846 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000847
Sean Callananafe16a72010-11-17 23:00:36 +0000848 if (!nsstring_global->hasInitializer())
849 {
850 if (log)
851 log->PutCString("NSString variable does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000852
Sean Callanan3989fb92011-01-27 01:07:04 +0000853 if (m_error_stream)
854 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have an initializer\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000855
Sean Callananafe16a72010-11-17 23:00:36 +0000856 return false;
857 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000858
Sean Callananafe16a72010-11-17 23:00:36 +0000859 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000860
Sean Callananafe16a72010-11-17 23:00:36 +0000861 if (!nsstring_struct)
862 {
863 if (log)
864 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000865
Sean Callanan3989fb92011-01-27 01:07:04 +0000866 if (m_error_stream)
867 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string is not a structure constant\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000868
Sean Callananafe16a72010-11-17 23:00:36 +0000869 return false;
870 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000871
Sean Callananafe16a72010-11-17 23:00:36 +0000872 // We expect the following structure:
873 //
874 // struct {
875 // int *isa;
876 // int flags;
877 // char *str;
878 // long length;
879 // };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000880
Sean Callananafe16a72010-11-17 23:00:36 +0000881 if (nsstring_struct->getNumOperands() != 4)
882 {
883 if (log)
884 log->Printf("NSString variable's initializer structure has an unexpected number of members. Should be 4, is %d", nsstring_struct->getNumOperands());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000885
Sean Callanan3989fb92011-01-27 01:07:04 +0000886 if (m_error_stream)
887 m_error_stream->Printf("Internal error [IRForTarget]: The struct for an Objective-C constant string is not as expected\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000888
Sean Callananafe16a72010-11-17 23:00:36 +0000889 return false;
890 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000891
Sean Callananafe16a72010-11-17 23:00:36 +0000892 Constant *nsstring_member = nsstring_struct->getOperand(2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000893
Sean Callananafe16a72010-11-17 23:00:36 +0000894 if (!nsstring_member)
895 {
896 if (log)
897 log->PutCString("NSString initializer's str element was empty");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000898
Sean Callanan3989fb92011-01-27 01:07:04 +0000899 if (m_error_stream)
900 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string does not have a string initializer\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000901
Sean Callananafe16a72010-11-17 23:00:36 +0000902 return false;
903 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000904
Sean Callananafe16a72010-11-17 23:00:36 +0000905 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000906
Sean Callananafe16a72010-11-17 23:00:36 +0000907 if (!nsstring_expr)
908 {
909 if (log)
910 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000911
Sean Callanan3989fb92011-01-27 01:07:04 +0000912 if (m_error_stream)
913 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not constant\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000914
Sean Callananafe16a72010-11-17 23:00:36 +0000915 return false;
916 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000917
Sean Callananafe16a72010-11-17 23:00:36 +0000918 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
919 {
920 if (log)
921 log->Printf("NSString initializer's str element is not a GetElementPtr expression, it's a %s", nsstring_expr->getOpcodeName());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000922
Sean Callanan3989fb92011-01-27 01:07:04 +0000923 if (m_error_stream)
924 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer is not an array\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000925
Sean Callananafe16a72010-11-17 23:00:36 +0000926 return false;
927 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000928
Sean Callananafe16a72010-11-17 23:00:36 +0000929 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000930
Sean Callananafe16a72010-11-17 23:00:36 +0000931 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000932
Sean Callananafe16a72010-11-17 23:00:36 +0000933 if (!cstr_global)
934 {
935 if (log)
936 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000937
Sean Callanan3989fb92011-01-27 01:07:04 +0000938 if (m_error_stream)
939 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a global\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000940
Sean Callananafe16a72010-11-17 23:00:36 +0000941 return false;
942 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000943
Sean Callananafe16a72010-11-17 23:00:36 +0000944 if (!cstr_global->hasInitializer())
945 {
946 if (log)
947 log->PutCString("NSString initializer's str element does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000948
Sean Callanan3989fb92011-01-27 01:07:04 +0000949 if (m_error_stream)
950 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to initialized data\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000951
Sean Callananafe16a72010-11-17 23:00:36 +0000952 return false;
953 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000954
Sean Callanan229ce2d2011-02-10 22:17:53 +0000955 /*
Sean Callananafe16a72010-11-17 23:00:36 +0000956 if (!cstr_array)
957 {
958 if (log)
959 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000960
Sean Callanan3989fb92011-01-27 01:07:04 +0000961 if (m_error_stream)
962 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to an array\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000963
Sean Callananafe16a72010-11-17 23:00:36 +0000964 return false;
965 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000966
Sean Callananafe16a72010-11-17 23:00:36 +0000967 if (!cstr_array->isCString())
968 {
969 if (log)
970 log->PutCString("NSString initializer's str element is not a C string array");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000971
Sean Callanan3989fb92011-01-27 01:07:04 +0000972 if (m_error_stream)
973 m_error_stream->Printf("Internal error [IRForTarget]: An Objective-C constant string's string initializer doesn't point to a C string\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000974
Sean Callananafe16a72010-11-17 23:00:36 +0000975 return false;
976 }
Sean Callanan229ce2d2011-02-10 22:17:53 +0000977 */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000978
Sean Callanand2b465f2012-02-09 03:22:41 +0000979 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000980
Sean Callananafe16a72010-11-17 23:00:36 +0000981 if (log)
Sean Callanan229ce2d2011-02-10 22:17:53 +0000982 {
983 if (cstr_array)
Sean Callanand2b465f2012-02-09 03:22:41 +0000984 log->Printf("Found NSString constant %s, which contains \"%s\"", value_name_cstr, cstr_array->getAsString().str().c_str());
Sean Callanan229ce2d2011-02-10 22:17:53 +0000985 else
Sean Callanancc427fa2011-07-30 02:42:06 +0000986 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000987 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000988
Sean Callanan229ce2d2011-02-10 22:17:53 +0000989 if (!cstr_array)
990 cstr_global = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000991
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000992 if (!RewriteObjCConstString(nsstring_global, cstr_global))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000993 {
Sean Callananafe16a72010-11-17 23:00:36 +0000994 if (log)
995 log->PutCString("Error rewriting the constant string");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000996
Sean Callanan3989fb92011-01-27 01:07:04 +0000997 // We don't print an error message here because RewriteObjCConstString has done so for us.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000998
Sean Callananafe16a72010-11-17 23:00:36 +0000999 return false;
1000 }
Sean Callananafe16a72010-11-17 23:00:36 +00001001 }
1002 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001003
Sean Callananafe16a72010-11-17 23:00:36 +00001004 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1005 vi != ve;
1006 ++vi)
1007 {
Sean Callanancc427fa2011-07-30 02:42:06 +00001008 std::string value_name = vi->first().str();
1009 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001010
Sean Callanancc427fa2011-07-30 02:42:06 +00001011 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callananafe16a72010-11-17 23:00:36 +00001012 {
1013 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001014
Sean Callananafe16a72010-11-17 23:00:36 +00001015 if (!gv)
1016 {
1017 if (log)
1018 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001019
Sean Callanan3989fb92011-01-27 01:07:04 +00001020 if (m_error_stream)
1021 m_error_stream->Printf("Internal error [IRForTarget]: Found a CFConstantStringClassReference, but it is not a global object\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001022
Sean Callananafe16a72010-11-17 23:00:36 +00001023 return false;
1024 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001025
Sean Callananafe16a72010-11-17 23:00:36 +00001026 gv->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001027
Sean Callananafe16a72010-11-17 23:00:36 +00001028 break;
1029 }
1030 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001031
Sean Callananafe16a72010-11-17 23:00:36 +00001032 return true;
1033}
1034
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001035static bool IsObjCSelectorRef (Value *value)
Sean Callanan5300d372010-07-31 01:32:05 +00001036{
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001037 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001038
Greg Claytonbd549162014-11-10 21:45:59 +00001039 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("OBJC_SELECTOR_REFERENCES_"))
Sean Callanan5300d372010-07-31 01:32:05 +00001040 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001041
Sean Callanan5300d372010-07-31 01:32:05 +00001042 return true;
1043}
1044
Sean Callanan3989fb92011-01-27 01:07:04 +00001045// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001046bool
Sean Callanan79763a42011-05-23 21:40:23 +00001047IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callanan5300d372010-07-31 01:32:05 +00001048{
Greg Clayton5160ce52013-03-27 23:08:40 +00001049 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001050
1051 LoadInst *load = dyn_cast<LoadInst>(selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001052
Sean Callanan5300d372010-07-31 01:32:05 +00001053 if (!load)
1054 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001055
Sean Callanan5300d372010-07-31 01:32:05 +00001056 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1057 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +00001058 // %tmp = load i8** @"OBJC_SELECTOR_REFERENCES_" ; <i8*>
Sean Callanan5300d372010-07-31 01:32:05 +00001059 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1060 //
1061 // where %obj is the object pointer and %tmp is the selector.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001062 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +00001063 // @"OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001064 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001065
Sean Callanan5300d372010-07-31 01:32:05 +00001066 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001067
Sean Callanan5300d372010-07-31 01:32:05 +00001068 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001069
Sean Callanan5300d372010-07-31 01:32:05 +00001070 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1071 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001072
Sean Callanan5300d372010-07-31 01:32:05 +00001073 Constant *osr_initializer = _objc_selector_references_->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001074
Sean Callanan5300d372010-07-31 01:32:05 +00001075 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001076
Sean Callanan5300d372010-07-31 01:32:05 +00001077 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1078 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001079
Sean Callanan5300d372010-07-31 01:32:05 +00001080 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1081
1082 if (!osr_initializer_base)
1083 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001084
Sean Callanan5300d372010-07-31 01:32:05 +00001085 // Find the string's initializer (a ConstantArray) and get the string from it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001086
Sean Callanan5300d372010-07-31 01:32:05 +00001087 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001088
Sean Callanan5300d372010-07-31 01:32:05 +00001089 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1090 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001091
Sean Callanan5300d372010-07-31 01:32:05 +00001092 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1093
Sean Callanand2b465f2012-02-09 03:22:41 +00001094 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001095
Sean Callanan5300d372010-07-31 01:32:05 +00001096 if (!omvn_initializer_array->isString())
1097 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001098
Sean Callanan5300d372010-07-31 01:32:05 +00001099 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001100
Sean Callanan5300d372010-07-31 01:32:05 +00001101 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001102 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001103
Sean Callanan5300d372010-07-31 01:32:05 +00001104 // Construct a call to sel_registerName
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001105
Sean Callanan5300d372010-07-31 01:32:05 +00001106 if (!m_sel_registerName)
1107 {
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001108 lldb::addr_t sel_registerName_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001109
Greg Clayton7b462cc2010-10-15 22:48:33 +00001110 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001111 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callanan5300d372010-07-31 01:32:05 +00001112 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001113
Sean Callananbe3a1b12010-10-26 00:31:56 +00001114 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001115 log->Printf("Found sel_registerName at 0x%" PRIx64, sel_registerName_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001116
Sean Callanan5300d372010-07-31 01:32:05 +00001117 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001118
Sean Callanan5300d372010-07-31 01:32:05 +00001119 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callanan79763a42011-05-23 21:40:23 +00001120 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callanan5300d372010-07-31 01:32:05 +00001121 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanancc427fa2011-07-30 02:42:06 +00001122 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001123
Sean Callanancc427fa2011-07-30 02:42:06 +00001124 Type *type_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001125
Sean Callanancc427fa2011-07-30 02:42:06 +00001126 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001127
Sean Callanancc427fa2011-07-30 02:42:06 +00001128 ArrayRef<Type *> srN_arg_types(type_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001129
Sean Callanan5300d372010-07-31 01:32:05 +00001130 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001131
Sean Callanan5300d372010-07-31 01:32:05 +00001132 // Build the constant containing the pointer to the function
Sean Callanan5300d372010-07-31 01:32:05 +00001133 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Sean Callanan439dcae2013-12-20 19:55:02 +00001134 Constant *srN_addr_int = ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
Sean Callanan5300d372010-07-31 01:32:05 +00001135 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1136 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001137
Sean Callanancc427fa2011-07-30 02:42:06 +00001138 Value *argument_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001139
Sean Callanan79763a42011-05-23 21:40:23 +00001140 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001141
Sean Callanancc427fa2011-07-30 02:42:06 +00001142 argument_array[0] = omvn_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001143
Sean Callanancc427fa2011-07-30 02:42:06 +00001144 ArrayRef<Value *> srN_arguments(argument_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001145
1146 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanancc427fa2011-07-30 02:42:06 +00001147 srN_arguments,
Sean Callananafe16a72010-11-17 23:00:36 +00001148 "sel_registerName",
Sean Callanan5300d372010-07-31 01:32:05 +00001149 selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001150
Sean Callanan5300d372010-07-31 01:32:05 +00001151 // Replace the load with the call in all users
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001152
Sean Callanan5300d372010-07-31 01:32:05 +00001153 selector_load->replaceAllUsesWith(srN_call);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001154
Sean Callanan5300d372010-07-31 01:32:05 +00001155 selector_load->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001156
Sean Callanan5300d372010-07-31 01:32:05 +00001157 return true;
1158}
1159
1160bool
Sean Callanan79763a42011-05-23 21:40:23 +00001161IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callanan5300d372010-07-31 01:32:05 +00001162{
Greg Clayton5160ce52013-03-27 23:08:40 +00001163 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001164
1165 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001166
Sean Callanan5300d372010-07-31 01:32:05 +00001167 typedef SmallVector <Instruction*, 2> InstrList;
1168 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001169
Sean Callanan5300d372010-07-31 01:32:05 +00001170 InstrList selector_loads;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001171
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001172 for (ii = basic_block.begin();
1173 ii != basic_block.end();
Sean Callanan5300d372010-07-31 01:32:05 +00001174 ++ii)
1175 {
1176 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001177
Sean Callanan5300d372010-07-31 01:32:05 +00001178 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001179 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callanan5300d372010-07-31 01:32:05 +00001180 selector_loads.push_back(&inst);
1181 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001182
Sean Callanan5300d372010-07-31 01:32:05 +00001183 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001184
Sean Callanan5300d372010-07-31 01:32:05 +00001185 for (iter = selector_loads.begin();
1186 iter != selector_loads.end();
1187 ++iter)
1188 {
Sean Callanan79763a42011-05-23 21:40:23 +00001189 if (!RewriteObjCSelector(*iter))
Sean Callanan5300d372010-07-31 01:32:05 +00001190 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001191 if (m_error_stream)
1192 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't change a static reference to an Objective-C selector to a dynamic reference\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001193
Enrico Granata20edcdb2011-07-19 18:03:25 +00001194 if (log)
Sean Callanan5300d372010-07-31 01:32:05 +00001195 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001196
Sean Callanan5300d372010-07-31 01:32:05 +00001197 return false;
1198 }
1199 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001200
Sean Callanan5300d372010-07-31 01:32:05 +00001201 return true;
1202}
1203
Sean Callanan3989fb92011-01-27 01:07:04 +00001204// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001205bool
Sean Callanan79763a42011-05-23 21:40:23 +00001206IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanan2235f322010-08-11 03:57:18 +00001207{
Greg Clayton5160ce52013-03-27 23:08:40 +00001208 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane1175b72011-01-13 21:23:32 +00001209
Sean Callanan2235f322010-08-11 03:57:18 +00001210 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001211
Ed Masted455a1e2014-11-02 00:24:22 +00001212 MDNode *alloc_md = alloc->getMDNode("clang.decl.ptr");
Sean Callanan2235f322010-08-11 03:57:18 +00001213
1214 if (!alloc_md || !alloc_md->getNumOperands())
1215 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001216
Sean Callanan2235f322010-08-11 03:57:18 +00001217 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001218
Sean Callanan2235f322010-08-11 03:57:18 +00001219 if (!constant_int)
1220 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001221
Sean Callanan2235f322010-08-11 03:57:18 +00001222 // We attempt to register this as a new persistent variable with the DeclMap.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001223
Sean Callanan2235f322010-08-11 03:57:18 +00001224 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001225
Sean Callanand1e5b432010-08-12 01:56:52 +00001226 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001227
Sean Callanand1e5b432010-08-12 01:56:52 +00001228 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1229 &decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001230
Greg Clayton7b462cc2010-10-15 22:48:33 +00001231 StringRef decl_name (decl->getName());
1232 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan92adcac2011-01-13 08:53:35 +00001233 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanan2235f322010-08-11 03:57:18 +00001234 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001235
Sean Callanan79763a42011-05-23 21:40:23 +00001236 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanane1175b72011-01-13 21:23:32 +00001237 alloc->getType(),
Sean Callanan2235f322010-08-11 03:57:18 +00001238 false, /* not constant */
1239 GlobalValue::ExternalLinkage,
1240 NULL, /* no initializer */
1241 alloc->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001242
Sean Callanan2235f322010-08-11 03:57:18 +00001243 // What we're going to do here is make believe this was a regular old external
1244 // variable. That means we need to make the metadata valid.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001245
Sean Callanan585c0ec82012-07-04 01:26:26 +00001246 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001247
Sean Callanan2235f322010-08-11 03:57:18 +00001248 llvm::Value* values[2];
1249 values[0] = persistent_global;
1250 values[1] = constant_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001251
Sean Callanand12cf8bb2011-05-15 22:34:38 +00001252 ArrayRef<llvm::Value*> value_ref(values, 2);
Sean Callanan2235f322010-08-11 03:57:18 +00001253
Sean Callanan79763a42011-05-23 21:40:23 +00001254 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanan2235f322010-08-11 03:57:18 +00001255 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001256
Sean Callanane1175b72011-01-13 21:23:32 +00001257 // Now, since the variable is a pointer variable, we will drop in a load of that
1258 // pointer variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001259
Sean Callanane1175b72011-01-13 21:23:32 +00001260 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001261
Sean Callanane1175b72011-01-13 21:23:32 +00001262 if (log)
1263 log->Printf("Replacing \"%s\" with \"%s\"",
1264 PrintValue(alloc).c_str(),
1265 PrintValue(persistent_load).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001266
Sean Callanane1175b72011-01-13 21:23:32 +00001267 alloc->replaceAllUsesWith(persistent_load);
Sean Callanan2235f322010-08-11 03:57:18 +00001268 alloc->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001269
Sean Callanan2235f322010-08-11 03:57:18 +00001270 return true;
1271}
1272
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001273bool
Sean Callanan79763a42011-05-23 21:40:23 +00001274IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanan2235f322010-08-11 03:57:18 +00001275{
Sean Callanan9e6ed532010-09-13 21:34:21 +00001276 if (!m_resolve_vars)
1277 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001278
Greg Clayton5160ce52013-03-27 23:08:40 +00001279 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001280
Sean Callanan2235f322010-08-11 03:57:18 +00001281 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001282
Sean Callanan2235f322010-08-11 03:57:18 +00001283 typedef SmallVector <Instruction*, 2> InstrList;
1284 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001285
Sean Callanan2235f322010-08-11 03:57:18 +00001286 InstrList pvar_allocs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001287
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001288 for (ii = basic_block.begin();
1289 ii != basic_block.end();
Sean Callanan2235f322010-08-11 03:57:18 +00001290 ++ii)
1291 {
1292 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001293
Sean Callanan2235f322010-08-11 03:57:18 +00001294 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callananf694a552011-01-21 22:30:25 +00001295 {
1296 llvm::StringRef alloc_name = alloc->getName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001297
Sean Callananf694a552011-01-21 22:30:25 +00001298 if (alloc_name.startswith("$") &&
1299 !alloc_name.startswith("$__lldb"))
1300 {
1301 if (alloc_name.find_first_of("0123456789") == 1)
1302 {
1303 if (log)
1304 log->Printf("Rejecting a numeric persistent variable.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001305
Sean Callanan3989fb92011-01-27 01:07:04 +00001306 if (m_error_stream)
1307 m_error_stream->Printf("Error [IRForTarget]: Names starting with $0, $1, ... are reserved for use as result names\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001308
Sean Callananf694a552011-01-21 22:30:25 +00001309 return false;
1310 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001311
Sean Callanan2235f322010-08-11 03:57:18 +00001312 pvar_allocs.push_back(alloc);
Sean Callananf694a552011-01-21 22:30:25 +00001313 }
1314 }
Sean Callanan2235f322010-08-11 03:57:18 +00001315 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001316
Sean Callanan2235f322010-08-11 03:57:18 +00001317 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001318
Sean Callanan2235f322010-08-11 03:57:18 +00001319 for (iter = pvar_allocs.begin();
1320 iter != pvar_allocs.end();
1321 ++iter)
1322 {
Sean Callanan79763a42011-05-23 21:40:23 +00001323 if (!RewritePersistentAlloc(*iter))
Sean Callanan2235f322010-08-11 03:57:18 +00001324 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001325 if (m_error_stream)
1326 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001327
Enrico Granata20edcdb2011-07-19 18:03:25 +00001328 if (log)
Sean Callanan2235f322010-08-11 03:57:18 +00001329 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001330
Sean Callanan2235f322010-08-11 03:57:18 +00001331 return false;
1332 }
1333 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001334
Sean Callanan2235f322010-08-11 03:57:18 +00001335 return true;
1336}
1337
Sean Callananc70ed462011-10-25 18:36:40 +00001338bool
1339IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1340{
1341 if (!initializer)
1342 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001343
Greg Clayton5160ce52013-03-27 23:08:40 +00001344 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc70ed462011-10-25 18:36:40 +00001345
1346 if (log && log->GetVerbose())
1347 log->Printf(" MaterializeInitializer(%p, %s)", data, PrintValue(initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001348
Sean Callananc70ed462011-10-25 18:36:40 +00001349 Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001350
Sean Callananc70ed462011-10-25 18:36:40 +00001351 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1352 {
1353 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1354 return true;
1355 }
Sean Callanand2b465f2012-02-09 03:22:41 +00001356 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
Sean Callananc70ed462011-10-25 18:36:40 +00001357 {
1358 if (array_initializer->isString())
1359 {
1360 std::string array_initializer_string = array_initializer->getAsString();
1361 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1362 }
1363 else
1364 {
1365 ArrayType *array_initializer_type = array_initializer->getType();
1366 Type *array_element_type = array_initializer_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001367
Sean Callananc70ed462011-10-25 18:36:40 +00001368 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001369
Andy Gibbsa297a972013-06-19 19:04:53 +00001370 for (unsigned i = 0; i < array_initializer->getNumOperands(); ++i)
Sean Callananc70ed462011-10-25 18:36:40 +00001371 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001372 Value *operand_value = array_initializer->getOperand(i);
1373 Constant *operand_constant = dyn_cast<Constant>(operand_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001374
Sean Callanand2b465f2012-02-09 03:22:41 +00001375 if (!operand_constant)
1376 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001377
Sean Callanand2b465f2012-02-09 03:22:41 +00001378 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
Sean Callananc70ed462011-10-25 18:36:40 +00001379 return false;
1380 }
1381 }
1382 return true;
1383 }
1384 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1385 {
1386 StructType *struct_initializer_type = struct_initializer->getType();
1387 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1388
Andy Gibbsa297a972013-06-19 19:04:53 +00001389 for (unsigned i = 0;
Sean Callananc70ed462011-10-25 18:36:40 +00001390 i < struct_initializer->getNumOperands();
1391 ++i)
1392 {
1393 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1394 return false;
1395 }
1396 return true;
1397 }
Sean Callanan76ee3e72013-04-24 19:50:12 +00001398 else if (isa<ConstantAggregateZero>(initializer))
1399 {
1400 memset(data, 0, m_target_data->getTypeStoreSize(initializer_type));
1401 return true;
1402 }
Sean Callananc70ed462011-10-25 18:36:40 +00001403 return false;
1404}
1405
1406bool
1407IRForTarget::MaterializeInternalVariable (GlobalVariable *global_variable)
1408{
1409 if (GlobalVariable::isExternalLinkage(global_variable->getLinkage()))
1410 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001411
Sean Callananfe5d1392011-11-15 19:13:54 +00001412 if (global_variable == m_reloc_placeholder)
1413 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001414
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001415 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001416
Sean Callananc70ed462011-10-25 18:36:40 +00001417 llvm::Type *variable_type = global_variable->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001418
Sean Callananc70ed462011-10-25 18:36:40 +00001419 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001420
Sean Callananc70ed462011-10-25 18:36:40 +00001421 llvm::Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001422
Sean Callananc70ed462011-10-25 18:36:40 +00001423 size_t size = m_target_data->getTypeAllocSize(initializer_type);
Sean Callanane3333d62012-06-08 22:20:41 +00001424 size_t align = m_target_data->getPrefTypeAlignment(initializer_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001425
Sean Callanane3333d62012-06-08 22:20:41 +00001426 const size_t mask = (align - 1);
1427 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001428 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sean Callanane3333d62012-06-08 22:20:41 +00001429 offset = aligned_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001430
Sean Callananc70ed462011-10-25 18:36:40 +00001431 lldb_private::DataBufferHeap data(size, '\0');
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001432
Sean Callananc70ed462011-10-25 18:36:40 +00001433 if (initializer)
1434 if (!MaterializeInitializer(data.GetBytes(), initializer))
1435 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001436
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001437 m_data_allocator.GetStream().Write(data.GetBytes(), data.GetByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001438
Sean Callananc70ed462011-10-25 18:36:40 +00001439 Constant *new_pointer = BuildRelocation(variable_type, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001440
Sean Callananc70ed462011-10-25 18:36:40 +00001441 global_variable->replaceAllUsesWith(new_pointer);
1442
1443 global_variable->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001444
Sean Callananc70ed462011-10-25 18:36:40 +00001445 return true;
1446}
1447
Sean Callanan3989fb92011-01-27 01:07:04 +00001448// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001449bool
Sean Callanan79763a42011-05-23 21:40:23 +00001450IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001451{
Greg Clayton5160ce52013-03-27 23:08:40 +00001452 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001453
Sean Callanand7a1ca22010-12-02 19:47:57 +00001454 if (log)
Sean Callanan88339f02010-12-06 22:16:55 +00001455 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001456
Greg Clayton7b462cc2010-10-15 22:48:33 +00001457 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callanan4cf04d22010-08-03 00:23:29 +00001458 {
Sean Callanan5666b672010-08-04 01:02:13 +00001459 switch (constant_expr->getOpcode())
Sean Callanan4cf04d22010-08-03 00:23:29 +00001460 {
Sean Callanan5666b672010-08-04 01:02:13 +00001461 default:
1462 break;
1463 case Instruction::GetElementPtr:
1464 case Instruction::BitCast:
Sean Callanan4cf04d22010-08-03 00:23:29 +00001465 Value *s = constant_expr->getOperand(0);
Sean Callanan79763a42011-05-23 21:40:23 +00001466 if (!MaybeHandleVariable(s))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001467 return false;
Sean Callanan4cf04d22010-08-03 00:23:29 +00001468 }
1469 }
Sean Callanand6e04ae2010-12-03 19:51:05 +00001470 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001471 {
Sean Callananc70ed462011-10-25 18:36:40 +00001472 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1473 return MaterializeInternalVariable(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001474
Sean Callanan79763a42011-05-23 21:40:23 +00001475 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001476
Sean Callanan5300d372010-07-31 01:32:05 +00001477 if (!named_decl)
1478 {
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001479 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001480 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001481
Sean Callanan14f0b0e2010-12-06 00:56:39 +00001482 if (!global_variable->hasExternalLinkage())
1483 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001484
Sean Callanan5300d372010-07-31 01:32:05 +00001485 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001486 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001487
Sean Callanan5300d372010-07-31 01:32:05 +00001488 return false;
1489 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001490
Greg Clayton7b462cc2010-10-15 22:48:33 +00001491 std::string name (named_decl->getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001492
Greg Clayton57ee3062013-07-11 22:46:58 +00001493 clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1494 if (value_decl == NULL)
Sean Callananea22d422010-07-16 00:09:46 +00001495 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001496
1497 lldb_private::ClangASTType clang_type(&value_decl->getASTContext(), value_decl->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001498
Sean Callanan77eaf442011-07-08 00:39:14 +00001499 const Type *value_type = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001500
Sean Callanane1175b72011-01-13 21:23:32 +00001501 if (name[0] == '$')
Sean Callanan92adcac2011-01-13 08:53:35 +00001502 {
1503 // The $__lldb_expr_result name indicates the the return value has allocated as
1504 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1505 // accesses to this static variable need to be redirected to the result of dereferencing
1506 // a pointer that is passed in as one of the arguments.
1507 //
1508 // Consequently, when reporting the size of the type, we report a pointer type pointing
1509 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanane1175b72011-01-13 21:23:32 +00001510 //
1511 // We also do this for any user-declared persistent variables.
Greg Clayton57ee3062013-07-11 22:46:58 +00001512 clang_type = clang_type.GetPointerType();
Sean Callanan92adcac2011-01-13 08:53:35 +00001513 value_type = PointerType::get(global_variable->getType(), 0);
1514 }
1515 else
1516 {
Sean Callanan92adcac2011-01-13 08:53:35 +00001517 value_type = global_variable->getType();
1518 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001519
Greg Clayton57ee3062013-07-11 22:46:58 +00001520 const uint64_t value_size = clang_type.GetByteSize();
Zachary Turnera746e8e2014-07-02 17:24:07 +00001521 lldb::offset_t value_alignment = (clang_type.GetTypeBitAlign() + 7ull) / 8ull;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001522
Sean Callanan038df5032010-09-30 21:18:25 +00001523 if (log)
Greg Clayton57ee3062013-07-11 22:46:58 +00001524 {
Zachary Turnera746e8e2014-07-02 17:24:07 +00001525 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %" PRIu64 ", align %" PRIu64 "]",
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001526 name.c_str(),
Greg Clayton57ee3062013-07-11 22:46:58 +00001527 clang_type.GetQualType().getAsString().c_str(),
1528 PrintType(value_type).c_str(),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001529 value_size,
Sean Callanan038df5032010-09-30 21:18:25 +00001530 value_alignment);
Greg Clayton57ee3062013-07-11 22:46:58 +00001531 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001532
1533
Sean Callanan64dfc9a2010-08-23 23:09:38 +00001534 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001535 lldb_private::ConstString (name.c_str()),
1536 llvm_value_ptr,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001537 value_size,
Sean Callanan4edba2d2010-07-27 02:07:53 +00001538 value_alignment))
Sean Callananaf8e96c2011-08-01 17:41:38 +00001539 {
1540 if (!global_variable->hasExternalLinkage())
1541 return true;
Sean Callanan0ff3bf92013-04-11 17:57:16 +00001542 else if (HandleSymbol (global_variable))
1543 return true;
Sean Callananaf8e96c2011-08-01 17:41:38 +00001544 else
1545 return false;
1546 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001547 }
Sean Callanan4a5fcbb2010-12-03 03:02:31 +00001548 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001549 {
1550 if (log)
1551 log->Printf("Function pointers aren't handled right now");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001552
Sean Callanand7a1ca22010-12-02 19:47:57 +00001553 return false;
1554 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001555
Sean Callanan549c9f72010-07-13 21:41:46 +00001556 return true;
1557}
1558
Sean Callanan3989fb92011-01-27 01:07:04 +00001559// This function does not report errors; its callers are responsible.
Sean Callanan549c9f72010-07-13 21:41:46 +00001560bool
Sean Callanan79763a42011-05-23 21:40:23 +00001561IRForTarget::HandleSymbol (Value *symbol)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001562{
Greg Clayton5160ce52013-03-27 23:08:40 +00001563 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001564
Sean Callananc3a16002011-01-17 23:42:46 +00001565 lldb_private::ConstString name(symbol->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001566
Sean Callanan947ccc72011-12-01 02:04:16 +00001567 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001568
Greg Clayton084db102011-06-23 04:25:29 +00001569 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc3a16002011-01-17 23:42:46 +00001570 {
1571 if (log)
1572 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001573
Sean Callananc3a16002011-01-17 23:42:46 +00001574 return false;
1575 }
1576
1577 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001578 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001579
Sean Callanancc427fa2011-07-30 02:42:06 +00001580 Type *symbol_type = symbol->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001581
Sean Callanan439dcae2013-12-20 19:55:02 +00001582 Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001583
Sean Callananc3a16002011-01-17 23:42:46 +00001584 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001585
Sean Callananc3a16002011-01-17 23:42:46 +00001586 if (log)
1587 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001588
Sean Callananc3a16002011-01-17 23:42:46 +00001589 symbol->replaceAllUsesWith(symbol_addr_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001590
Sean Callananc3a16002011-01-17 23:42:46 +00001591 return true;
1592}
1593
1594bool
Sean Callanan79763a42011-05-23 21:40:23 +00001595IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanan85a0a832010-10-05 22:26:43 +00001596{
Greg Clayton5160ce52013-03-27 23:08:40 +00001597 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001598
Sean Callanand7a1ca22010-12-02 19:47:57 +00001599 if (log)
1600 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001601
Sean Callananafe16a72010-11-17 23:00:36 +00001602 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanan85a0a832010-10-05 22:26:43 +00001603 op_index < num_ops;
1604 ++op_index)
Sean Callanan79763a42011-05-23 21:40:23 +00001605 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan3989fb92011-01-27 01:07:04 +00001606 {
1607 if (m_error_stream)
1608 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite one of the arguments of a function call.\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001609
Sean Callanan85a0a832010-10-05 22:26:43 +00001610 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001611 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001612
Sean Callanan85a0a832010-10-05 22:26:43 +00001613 return true;
1614}
1615
1616bool
Sean Callananfc89c142011-11-01 23:38:03 +00001617IRForTarget::HandleObjCClass(Value *classlist_reference)
1618{
Greg Clayton5160ce52013-03-27 23:08:40 +00001619 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc89c142011-11-01 23:38:03 +00001620
1621 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001622
Sean Callananfc89c142011-11-01 23:38:03 +00001623 if (!global_variable)
1624 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001625
Sean Callananfc89c142011-11-01 23:38:03 +00001626 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001627
Sean Callananfc89c142011-11-01 23:38:03 +00001628 if (!initializer)
1629 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001630
Sean Callananfc89c142011-11-01 23:38:03 +00001631 if (!initializer->hasName())
1632 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001633
Sean Callananfc89c142011-11-01 23:38:03 +00001634 StringRef name(initializer->getName());
1635 lldb_private::ConstString name_cstr(name.str().c_str());
Greg Clayton1075aca2011-12-03 20:02:42 +00001636 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001637
Sean Callananfc89c142011-11-01 23:38:03 +00001638 if (log)
1639 log->Printf("Found reference to Objective-C class %s (0x%llx)", name_cstr.AsCString(), (unsigned long long)class_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001640
Sean Callananfc89c142011-11-01 23:38:03 +00001641 if (class_ptr == LLDB_INVALID_ADDRESS)
1642 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001643
Ed Mastea8553092014-03-10 17:24:16 +00001644 if (global_variable->use_empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001645 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001646
Sean Callanan719a4d52013-03-23 01:01:16 +00001647 SmallVector<LoadInst *, 2> load_instructions;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001648
Ed Mastea8553092014-03-10 17:24:16 +00001649 for (llvm::User *u : global_variable->users())
Sean Callananfc89c142011-11-01 23:38:03 +00001650 {
Ed Mastea8553092014-03-10 17:24:16 +00001651 if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
Sean Callanan719a4d52013-03-23 01:01:16 +00001652 load_instructions.push_back(load_instruction);
Sean Callananfc89c142011-11-01 23:38:03 +00001653 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001654
Sean Callanan719a4d52013-03-23 01:01:16 +00001655 if (load_instructions.empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001656 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001657
Sean Callanan439dcae2013-12-20 19:55:02 +00001658 Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001659
Sean Callanan719a4d52013-03-23 01:01:16 +00001660 for (LoadInst *load_instruction : load_instructions)
1661 {
1662 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001663
Sean Callanan719a4d52013-03-23 01:01:16 +00001664 load_instruction->replaceAllUsesWith(class_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001665
Sean Callanan719a4d52013-03-23 01:01:16 +00001666 load_instruction->eraseFromParent();
1667 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001668
Sean Callananfc89c142011-11-01 23:38:03 +00001669 return true;
1670}
1671
1672bool
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001673IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1674{
1675 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001676
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001677 std::vector<CallInst *> calls_to_remove;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001678
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001679 for (ii = basic_block.begin();
1680 ii != basic_block.end();
1681 ++ii)
1682 {
1683 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001684
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001685 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001686
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001687 // MaybeHandleCallArguments handles error reporting; we are silent here
1688 if (!call)
1689 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001690
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001691 bool remove = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001692
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001693 llvm::Function *func = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001694
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001695 if (func && func->getName() == "__cxa_atexit")
1696 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001697
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001698 llvm::Value *val = call->getCalledValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001699
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001700 if (val && val->getName() == "__cxa_atexit")
1701 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001702
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001703 if (remove)
1704 calls_to_remove.push_back(call);
1705 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001706
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001707 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1708 ci != ce;
1709 ++ci)
1710 {
1711 (*ci)->eraseFromParent();
1712 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001713
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001714 return true;
1715}
1716
1717bool
Sean Callanan79763a42011-05-23 21:40:23 +00001718IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001719{
Sean Callanan2ab712f22010-07-03 01:35:46 +00001720 /////////////////////////////////////////////////////////////////////////
1721 // Prepare the current basic block for execution in the remote process
1722 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001723
Sean Callanan7ea35012010-07-27 21:39:39 +00001724 BasicBlock::iterator ii;
Sean Callanan549c9f72010-07-13 21:41:46 +00001725
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001726 for (ii = basic_block.begin();
1727 ii != basic_block.end();
Sean Callanan549c9f72010-07-13 21:41:46 +00001728 ++ii)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001729 {
Sean Callanan549c9f72010-07-13 21:41:46 +00001730 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001731
Sean Callanana4e55172010-11-08 00:31:32 +00001732 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001733
Sean Callanan3989fb92011-01-27 01:07:04 +00001734 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callanan79763a42011-05-23 21:40:23 +00001735 if (call && !MaybeHandleCallArguments(call))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001736 return false;
Sean Callanan2ab712f22010-07-03 01:35:46 +00001737 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001738
Sean Callanan2ab712f22010-07-03 01:35:46 +00001739 return true;
1740}
1741
Sean Callanana4e55172010-11-08 00:31:32 +00001742bool
Sean Callanan79763a42011-05-23 21:40:23 +00001743IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanana4e55172010-11-08 00:31:32 +00001744{
Greg Clayton5160ce52013-03-27 23:08:40 +00001745 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001746
Sean Callanan339f6152014-03-11 19:19:16 +00001747 for (GlobalVariable &global_var : m_module->globals())
Sean Callanana4e55172010-11-08 00:31:32 +00001748 {
Sean Callanan339f6152014-03-11 19:19:16 +00001749 std::string global_name = global_var.getName().str();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001750
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001751 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001752 log->Printf("Examining %s, DeclForGlobalValue returns %p",
Sean Callanan694e2442011-12-22 21:24:49 +00001753 global_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001754 static_cast<void*>(DeclForGlobal(&global_var)));
1755
Sean Callananfc89c142011-11-01 23:38:03 +00001756 if (global_name.find("OBJC_IVAR") == 0)
Sean Callananc3a16002011-01-17 23:42:46 +00001757 {
Sean Callanan339f6152014-03-11 19:19:16 +00001758 if (!HandleSymbol(&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001759 {
1760 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001761 m_error_stream->Printf("Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol %s\n", global_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001762
Sean Callananc3a16002011-01-17 23:42:46 +00001763 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001764 }
Sean Callananc3a16002011-01-17 23:42:46 +00001765 }
Sean Callananfc89c142011-11-01 23:38:03 +00001766 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1767 {
Sean Callanan339f6152014-03-11 19:19:16 +00001768 if (!HandleObjCClass(&global_var))
Sean Callananfc89c142011-11-01 23:38:03 +00001769 {
1770 if (m_error_stream)
1771 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001772
Sean Callananfc89c142011-11-01 23:38:03 +00001773 return false;
1774 }
1775 }
Sean Callanan2ad66912013-04-24 21:25:20 +00001776 else if (global_name.find("OBJC_CLASSLIST_SUP_REFS_$") != global_name.npos)
1777 {
Sean Callanan339f6152014-03-11 19:19:16 +00001778 if (!HandleObjCClass(&global_var))
Sean Callanan2ad66912013-04-24 21:25:20 +00001779 {
1780 if (m_error_stream)
1781 m_error_stream->Printf("Error [IRForTarget]: Couldn't resolve the class for an Objective-C static method call\n");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001782
Sean Callanan2ad66912013-04-24 21:25:20 +00001783 return false;
1784 }
1785 }
Sean Callanan339f6152014-03-11 19:19:16 +00001786 else if (DeclForGlobal(&global_var))
Sean Callananc3a16002011-01-17 23:42:46 +00001787 {
Sean Callanan339f6152014-03-11 19:19:16 +00001788 if (!MaybeHandleVariable (&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001789 {
1790 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001791 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite external variable %s\n", global_name.c_str());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001792
Sean Callananc3a16002011-01-17 23:42:46 +00001793 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001794 }
Sean Callananc3a16002011-01-17 23:42:46 +00001795 }
Sean Callanana4e55172010-11-08 00:31:32 +00001796 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001797
Sean Callanana4e55172010-11-08 00:31:32 +00001798 return true;
1799}
1800
Sean Callanan79763a42011-05-23 21:40:23 +00001801bool
1802IRForTarget::ReplaceStrings ()
1803{
Greg Clayton5160ce52013-03-27 23:08:40 +00001804 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001805
Sean Callanan79763a42011-05-23 21:40:23 +00001806 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001807
Sean Callanan79763a42011-05-23 21:40:23 +00001808 OffsetsTy offsets;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001809
Sean Callanan339f6152014-03-11 19:19:16 +00001810 for (GlobalVariable &gv : m_module->globals())
Sean Callanan79763a42011-05-23 21:40:23 +00001811 {
Sean Callanan339f6152014-03-11 19:19:16 +00001812 if (!gv.hasInitializer())
Sean Callanan79763a42011-05-23 21:40:23 +00001813 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001814
Sean Callanan339f6152014-03-11 19:19:16 +00001815 Constant *gc = gv.getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001816
Sean Callanan5207a342011-08-10 21:05:52 +00001817 std::string str;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001818
Sean Callanan5207a342011-08-10 21:05:52 +00001819 if (gc->isNullValue())
1820 {
1821 Type *gc_type = gc->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001822
Sean Callanan5207a342011-08-10 21:05:52 +00001823 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001824
Sean Callanan5207a342011-08-10 21:05:52 +00001825 if (!gc_array_type)
1826 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001827
Sean Callanan5207a342011-08-10 21:05:52 +00001828 Type *gc_element_type = gc_array_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001829
Sean Callanan5207a342011-08-10 21:05:52 +00001830 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001831
Sean Callanan5207a342011-08-10 21:05:52 +00001832 if (gc_integer_type->getBitWidth() != 8)
1833 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001834
Sean Callanan5207a342011-08-10 21:05:52 +00001835 str = "";
1836 }
1837 else
1838 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001839 ConstantDataArray *gc_array = dyn_cast<ConstantDataArray>(gc);
Sean Callanan5207a342011-08-10 21:05:52 +00001840
1841 if (!gc_array)
1842 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001843
Sean Callanan5207a342011-08-10 21:05:52 +00001844 if (!gc_array->isCString())
1845 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001846
Sean Callanan5207a342011-08-10 21:05:52 +00001847 if (log)
1848 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001849
Sean Callanan5207a342011-08-10 21:05:52 +00001850 str = gc_array->getAsString();
1851 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001852
Sean Callanan339f6152014-03-11 19:19:16 +00001853 offsets[&gv] = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001854
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001855 m_data_allocator.GetStream().Write(str.c_str(), str.length() + 1);
Sean Callanan79763a42011-05-23 21:40:23 +00001856 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001857
Sean Callanancc427fa2011-07-30 02:42:06 +00001858 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001859
Sean Callanan79763a42011-05-23 21:40:23 +00001860 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1861 oi != oe;
1862 ++oi)
1863 {
1864 GlobalVariable *gv = oi->first;
1865 size_t offset = oi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001866
Sean Callanan79763a42011-05-23 21:40:23 +00001867 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001868
Sean Callanan79763a42011-05-23 21:40:23 +00001869 if (log)
1870 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001871
Ed Mastea8553092014-03-10 17:24:16 +00001872 for (llvm::User *u : gv->users())
Sean Callanan79763a42011-05-23 21:40:23 +00001873 {
1874 if (log)
Ed Mastea8553092014-03-10 17:24:16 +00001875 log->Printf("Found use %s", PrintValue(u).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001876
Ed Mastea8553092014-03-10 17:24:16 +00001877 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(u);
1878 StoreInst *store_inst = dyn_cast<StoreInst>(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001879
Sean Callanan79763a42011-05-23 21:40:23 +00001880 if (const_expr)
1881 {
1882 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1883 {
1884 if (log)
1885 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001886
Sean Callanan79763a42011-05-23 21:40:23 +00001887 return false;
1888 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001889
Sean Callanan5207a342011-08-10 21:05:52 +00001890 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
1891 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001892
Sean Callanan5207a342011-08-10 21:05:52 +00001893 const_expr->replaceAllUsesWith(new_gep);
Sean Callanan79763a42011-05-23 21:40:23 +00001894 }
1895 else if (store_inst)
1896 {
1897 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001898
Sean Callanan79763a42011-05-23 21:40:23 +00001899 store_inst->setOperand(0, bit_cast);
1900 }
1901 else
1902 {
1903 if (log)
1904 log->Printf("Use (%s) of string variable is neither a constant nor a store", PrintValue(const_expr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001905
Sean Callanan79763a42011-05-23 21:40:23 +00001906 return false;
1907 }
1908 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001909
Sean Callanan79763a42011-05-23 21:40:23 +00001910 gv->eraseFromParent();
1911 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001912
Sean Callanan79763a42011-05-23 21:40:23 +00001913 return true;
1914}
1915
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001916bool
Sean Callanan79763a42011-05-23 21:40:23 +00001917IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
1918{
Greg Clayton5160ce52013-03-27 23:08:40 +00001919 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001920
Sean Callanan79763a42011-05-23 21:40:23 +00001921 typedef SmallVector <Value*, 2> ConstantList;
1922 typedef SmallVector <llvm::Instruction*, 2> UserList;
1923 typedef ConstantList::iterator ConstantIterator;
1924 typedef UserList::iterator UserIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001925
Sean Callanan79763a42011-05-23 21:40:23 +00001926 ConstantList static_constants;
1927 UserList static_users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001928
Sean Callanan79763a42011-05-23 21:40:23 +00001929 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
1930 ii != ie;
1931 ++ii)
1932 {
1933 llvm::Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001934
Sean Callanan339f6152014-03-11 19:19:16 +00001935 for (Value *operand_val : inst.operand_values())
Sean Callanan79763a42011-05-23 21:40:23 +00001936 {
Sean Callanan79763a42011-05-23 21:40:23 +00001937 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001938
Sean Callanan822944c2012-04-26 20:51:20 +00001939 if (operand_constant_fp/* && operand_constant_fp->getType()->isX86_FP80Ty()*/)
Sean Callanan79763a42011-05-23 21:40:23 +00001940 {
1941 static_constants.push_back(operand_val);
1942 static_users.push_back(ii);
1943 }
1944 }
1945 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001946
Sean Callanan79763a42011-05-23 21:40:23 +00001947 ConstantIterator constant_iter;
1948 UserIterator user_iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001949
Sean Callanan79763a42011-05-23 21:40:23 +00001950 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
1951 constant_iter != static_constants.end();
1952 ++constant_iter, ++user_iter)
1953 {
1954 Value *operand_val = *constant_iter;
1955 llvm::Instruction *inst = *user_iter;
1956
1957 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001958
Sean Callanan79763a42011-05-23 21:40:23 +00001959 if (operand_constant_fp)
1960 {
Sean Callanan1582ee62013-04-18 22:06:33 +00001961 Type *operand_type = operand_constant_fp->getType();
1962
Sean Callanan79763a42011-05-23 21:40:23 +00001963 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
1964 APInt operand_apint = operand_apfloat.bitcastToAPInt();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001965
Sean Callanan79763a42011-05-23 21:40:23 +00001966 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
1967 size_t operand_data_size = operand_apint.getBitWidth() / 8;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001968
Sean Callanan79763a42011-05-23 21:40:23 +00001969 if (log)
1970 {
1971 std::string s;
1972 raw_string_ostream ss(s);
1973 for (size_t index = 0;
1974 index < operand_data_size;
1975 ++index)
1976 {
1977 ss << (uint32_t)operand_raw_data[index];
1978 ss << " ";
1979 }
1980 ss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001981
Greg Clayton6fea17e2014-03-03 19:15:20 +00001982 log->Printf("Found ConstantFP with size %" PRIu64 " and raw data %s", (uint64_t)operand_data_size, s.c_str());
Sean Callanan79763a42011-05-23 21:40:23 +00001983 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001984
Sean Callanan79763a42011-05-23 21:40:23 +00001985 lldb_private::DataBufferHeap data(operand_data_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001986
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001987 if (lldb::endian::InlHostByteOrder() != m_data_allocator.GetStream().GetByteOrder())
Sean Callanan79763a42011-05-23 21:40:23 +00001988 {
1989 uint8_t *data_bytes = data.GetBytes();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001990
Sean Callanan79763a42011-05-23 21:40:23 +00001991 for (size_t index = 0;
1992 index < operand_data_size;
1993 ++index)
1994 {
1995 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
1996 }
1997 }
1998 else
1999 {
2000 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
2001 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002002
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002003 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002004
Sean Callanane3333d62012-06-08 22:20:41 +00002005 size_t align = m_target_data->getPrefTypeAlignment(operand_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002006
Sean Callanane3333d62012-06-08 22:20:41 +00002007 const size_t mask = (align - 1);
2008 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002009 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002010
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002011 m_data_allocator.GetStream().Write(data.GetBytes(), operand_data_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002012
Sean Callanancc427fa2011-07-30 02:42:06 +00002013 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002014
Sean Callanan1582ee62013-04-18 22:06:33 +00002015 Constant *new_pointer = BuildRelocation(fp_ptr_ty, aligned_offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002016
Sean Callanan79763a42011-05-23 21:40:23 +00002017 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002018
Sean Callanan79763a42011-05-23 21:40:23 +00002019 operand_constant_fp->replaceAllUsesWith(fp_load);
2020 }
2021 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002022
Sean Callanan79763a42011-05-23 21:40:23 +00002023 return true;
2024}
2025
Sean Callanan7ea35012010-07-27 21:39:39 +00002026static bool isGuardVariableRef(Value *V)
Sean Callananddb46ef2010-07-24 01:37:44 +00002027{
Sean Callanan77eaf442011-07-08 00:39:14 +00002028 Constant *Old = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002029
Sean Callananafe16a72010-11-17 23:00:36 +00002030 if (!(Old = dyn_cast<Constant>(V)))
Sean Callananddb46ef2010-07-24 01:37:44 +00002031 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002032
Sean Callanan77eaf442011-07-08 00:39:14 +00002033 ConstantExpr *CE = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002034
Sean Callanane2ef6e32010-09-23 03:01:22 +00002035 if ((CE = dyn_cast<ConstantExpr>(V)))
2036 {
2037 if (CE->getOpcode() != Instruction::BitCast)
2038 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002039
Sean Callananafe16a72010-11-17 23:00:36 +00002040 Old = CE->getOperand(0);
Sean Callanane2ef6e32010-09-23 03:01:22 +00002041 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002042
Sean Callananafe16a72010-11-17 23:00:36 +00002043 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002044
Sean Callananddb46ef2010-07-24 01:37:44 +00002045 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
2046 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002047
Sean Callananddb46ef2010-07-24 01:37:44 +00002048 return true;
2049}
2050
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002051void
Sean Callanan79763a42011-05-23 21:40:23 +00002052IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callananddb46ef2010-07-24 01:37:44 +00002053{
Sean Callanan79763a42011-05-23 21:40:23 +00002054 Constant* zero(ConstantInt::get(Type::getInt8Ty(m_module->getContext()), 0, true));
Sean Callananddb46ef2010-07-24 01:37:44 +00002055
Ed Maste80e8cc62014-03-10 14:23:10 +00002056 for (llvm::User *u : guard_load->users())
Sean Callananb27a62f2010-07-27 01:17:28 +00002057 {
Ed Maste80e8cc62014-03-10 14:23:10 +00002058 if (isa<Constant>(u))
Sean Callananb27a62f2010-07-27 01:17:28 +00002059 {
2060 // do nothing for the moment
2061 }
2062 else
2063 {
Ed Maste80e8cc62014-03-10 14:23:10 +00002064 u->replaceUsesOfWith(guard_load, zero);
Sean Callananb27a62f2010-07-27 01:17:28 +00002065 }
2066 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002067
Sean Callananddb46ef2010-07-24 01:37:44 +00002068 guard_load->eraseFromParent();
2069}
2070
2071static void ExciseGuardStore(Instruction* guard_store)
2072{
2073 guard_store->eraseFromParent();
2074}
2075
2076bool
Sean Callanan79763a42011-05-23 21:40:23 +00002077IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002078{
Sean Callananddb46ef2010-07-24 01:37:44 +00002079 ///////////////////////////////////////////////////////
2080 // Eliminate any reference to guard variables found.
2081 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002082
Sean Callanan7ea35012010-07-27 21:39:39 +00002083 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002084
Sean Callanan7ea35012010-07-27 21:39:39 +00002085 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callananddb46ef2010-07-24 01:37:44 +00002086 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002087
Sean Callananddb46ef2010-07-24 01:37:44 +00002088 InstrList guard_loads;
2089 InstrList guard_stores;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002090
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002091 for (ii = basic_block.begin();
2092 ii != basic_block.end();
Sean Callananddb46ef2010-07-24 01:37:44 +00002093 ++ii)
2094 {
2095 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002096
Sean Callananddb46ef2010-07-24 01:37:44 +00002097 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
2098 if (isGuardVariableRef(load->getPointerOperand()))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002099 guard_loads.push_back(&inst);
2100
2101 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callananddb46ef2010-07-24 01:37:44 +00002102 if (isGuardVariableRef(store->getPointerOperand()))
2103 guard_stores.push_back(&inst);
2104 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002105
Sean Callananddb46ef2010-07-24 01:37:44 +00002106 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002107
Sean Callananddb46ef2010-07-24 01:37:44 +00002108 for (iter = guard_loads.begin();
2109 iter != guard_loads.end();
2110 ++iter)
Sean Callanan79763a42011-05-23 21:40:23 +00002111 TurnGuardLoadIntoZero(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002112
Sean Callananddb46ef2010-07-24 01:37:44 +00002113 for (iter = guard_stores.begin();
2114 iter != guard_stores.end();
2115 ++iter)
2116 ExciseGuardStore(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002117
Sean Callananddb46ef2010-07-24 01:37:44 +00002118 return true;
2119}
2120
Sean Callanan3989fb92011-01-27 01:07:04 +00002121// This function does not report errors; its callers are responsible.
Sean Callananafe16a72010-11-17 23:00:36 +00002122bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002123IRForTarget::UnfoldConstant(Constant *old_constant,
2124 FunctionValueCache &value_maker,
2125 FunctionValueCache &entry_instruction_finder)
Sean Callanan7618f4e2010-07-14 23:40:29 +00002126{
Greg Clayton5160ce52013-03-27 23:08:40 +00002127 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan7618f4e2010-07-14 23:40:29 +00002128
Sean Callanan2235f322010-08-11 03:57:18 +00002129 SmallVector<User*, 16> users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002130
Sean Callanan2235f322010-08-11 03:57:18 +00002131 // We do this because the use list might change, invalidating our iterator.
2132 // Much better to keep a work list ourselves.
Ed Maste80e8cc62014-03-10 14:23:10 +00002133 for (llvm::User *u : old_constant->users())
2134 users.push_back(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002135
Johnny Chen44805302011-07-19 19:48:13 +00002136 for (size_t i = 0;
Sean Callanan2235f322010-08-11 03:57:18 +00002137 i < users.size();
2138 ++i)
2139 {
2140 User *user = users[i];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002141
Sean Callanan7618f4e2010-07-14 23:40:29 +00002142 if (Constant *constant = dyn_cast<Constant>(user))
2143 {
2144 // synthesize a new non-constant equivalent of the constant
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002145
Sean Callanan7618f4e2010-07-14 23:40:29 +00002146 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
2147 {
2148 switch (constant_expr->getOpcode())
2149 {
2150 default:
2151 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002152 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002153 return false;
2154 case Instruction::BitCast:
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002155 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002156 FunctionValueCache bit_cast_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2157 // UnaryExpr
2158 // OperandList[0] is value
2159
2160 if (constant_expr->getOperand(0) != old_constant)
2161 return constant_expr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002162
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002163 return new BitCastInst(value_maker.GetValue(function),
2164 constant_expr->getType(),
2165 "",
2166 llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2167 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002168
Sean Callanan0e016fe2013-07-15 23:31:47 +00002169 if (!UnfoldConstant(constant_expr, bit_cast_maker, entry_instruction_finder))
2170 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002171 }
2172 break;
2173 case Instruction::GetElementPtr:
2174 {
2175 // GetElementPtrConstantExpr
2176 // OperandList[0] is base
2177 // OperandList[1]... are indices
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002178
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002179 FunctionValueCache get_element_pointer_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2180 Value *ptr = constant_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002181
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002182 if (ptr == old_constant)
2183 ptr = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002184
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002185 std::vector<Value*> index_vector;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002186
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002187 unsigned operand_index;
2188 unsigned num_operands = constant_expr->getNumOperands();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002189
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002190 for (operand_index = 1;
2191 operand_index < num_operands;
2192 ++operand_index)
2193 {
2194 Value *operand = constant_expr->getOperand(operand_index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002195
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002196 if (operand == old_constant)
2197 operand = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002198
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002199 index_vector.push_back(operand);
2200 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002201
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002202 ArrayRef <Value*> indices(index_vector);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002203
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002204 return GetElementPtrInst::Create(ptr, indices, "", llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2205 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002206
Sean Callanan0e016fe2013-07-15 23:31:47 +00002207 if (!UnfoldConstant(constant_expr, get_element_pointer_maker, entry_instruction_finder))
2208 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002209 }
2210 break;
2211 }
2212 }
2213 else
2214 {
2215 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002216 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002217 return false;
2218 }
2219 }
2220 else
2221 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002222 if (Instruction *inst = llvm::dyn_cast<Instruction>(user))
2223 {
2224 inst->replaceUsesOfWith(old_constant, value_maker.GetValue(inst->getParent()->getParent()));
2225 }
2226 else
2227 {
2228 if (log)
2229 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(user).c_str());
2230 return false;
2231 }
Sean Callanan7618f4e2010-07-14 23:40:29 +00002232 }
2233 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002234
Sean Callanan0e016fe2013-07-15 23:31:47 +00002235 if (!isa<GlobalValue>(old_constant))
2236 {
2237 old_constant->destroyConstant();
2238 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002239
Sean Callanan7618f4e2010-07-14 23:40:29 +00002240 return true;
2241}
2242
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002243bool
Sean Callanan79763a42011-05-23 21:40:23 +00002244IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan549c9f72010-07-13 21:41:46 +00002245{
Sean Callanan9e6ed532010-09-13 21:34:21 +00002246 if (!m_resolve_vars)
2247 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002248
Greg Clayton5160ce52013-03-27 23:08:40 +00002249 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan549c9f72010-07-13 21:41:46 +00002250
2251 m_decl_map->DoStructLayout();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002252
Sean Callanan549c9f72010-07-13 21:41:46 +00002253 if (log)
2254 log->Printf("Element arrangement:");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002255
Sean Callanan549c9f72010-07-13 21:41:46 +00002256 uint32_t num_elements;
2257 uint32_t element_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002258
Sean Callanan549c9f72010-07-13 21:41:46 +00002259 size_t size;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002260 lldb::offset_t alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002261
Sean Callanan549c9f72010-07-13 21:41:46 +00002262 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2263 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002264
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002265 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002266
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002267 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002268 {
2269 if (m_error_stream)
2270 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes no arguments (should take at least a struct pointer)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002271
Sean Callanan549c9f72010-07-13 21:41:46 +00002272 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002273 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002274
Sean Callanan7ea35012010-07-27 21:39:39 +00002275 Argument *argument = iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002276
Sean Callananfc55f5d2010-09-21 00:44:12 +00002277 if (argument->getName().equals("this"))
2278 {
2279 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002280
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002281 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002282 {
2283 if (m_error_stream)
2284 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'this' argument (should take a struct pointer too)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002285
Sean Callananfc55f5d2010-09-21 00:44:12 +00002286 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002287 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002288
Sean Callananfc55f5d2010-09-21 00:44:12 +00002289 argument = iter;
2290 }
Sean Callanan17827832010-12-13 22:46:15 +00002291 else if (argument->getName().equals("self"))
2292 {
2293 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002294
Sean Callanan17827832010-12-13 22:46:15 +00002295 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002296 {
2297 if (m_error_stream)
2298 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' argument (should take '_cmd' and a struct pointer too)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002299
Sean Callanan17827832010-12-13 22:46:15 +00002300 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002301 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002302
Sean Callanan17827832010-12-13 22:46:15 +00002303 if (!iter->getName().equals("_cmd"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002304 {
2305 if (m_error_stream)
2306 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes '%s' after 'self' argument (should take '_cmd')", iter->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002307
Sean Callanan17827832010-12-13 22:46:15 +00002308 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002309 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002310
Sean Callanan17827832010-12-13 22:46:15 +00002311 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002312
Sean Callanan17827832010-12-13 22:46:15 +00002313 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002314 {
2315 if (m_error_stream)
2316 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes only 'self' and '_cmd' arguments (should take a struct pointer too)");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002317
Sean Callanan17827832010-12-13 22:46:15 +00002318 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002319 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002320
Sean Callanan17827832010-12-13 22:46:15 +00002321 argument = iter;
2322 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002323
Greg Clayton7b462cc2010-10-15 22:48:33 +00002324 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002325 {
2326 if (m_error_stream)
2327 m_error_stream->Printf("Internal error [IRForTarget]: Wrapper takes an argument named '%s' instead of the struct pointer", argument->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002328
Sean Callanan549c9f72010-07-13 21:41:46 +00002329 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002330 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002331
Sean Callanan549c9f72010-07-13 21:41:46 +00002332 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002333 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002334
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002335 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callananafe16a72010-11-17 23:00:36 +00002336 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002337
Sean Callananafe16a72010-11-17 23:00:36 +00002338 if (!FirstEntryInstruction)
Sean Callanan3989fb92011-01-27 01:07:04 +00002339 {
2340 if (m_error_stream)
2341 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find the first instruction in the wrapper for use in rewriting");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002342
Sean Callanan549c9f72010-07-13 21:41:46 +00002343 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002344 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002345
Sean Callanan79763a42011-05-23 21:40:23 +00002346 LLVMContext &context(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +00002347 IntegerType *offset_type(Type::getInt32Ty(context));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002348
Sean Callanan549c9f72010-07-13 21:41:46 +00002349 if (!offset_type)
Sean Callanan3989fb92011-01-27 01:07:04 +00002350 {
2351 if (m_error_stream)
2352 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002353
Sean Callanan549c9f72010-07-13 21:41:46 +00002354 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002355 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002356
Sean Callanan549c9f72010-07-13 21:41:46 +00002357 for (element_index = 0; element_index < num_elements; ++element_index)
2358 {
Sean Callanan77eaf442011-07-08 00:39:14 +00002359 const clang::NamedDecl *decl = NULL;
2360 Value *value = NULL;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002361 lldb::offset_t offset;
Greg Clayton7b462cc2010-10-15 22:48:33 +00002362 lldb_private::ConstString name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002363
Sean Callanan823bb4c2010-08-30 22:17:16 +00002364 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan3989fb92011-01-27 01:07:04 +00002365 {
2366 if (m_error_stream)
2367 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002368
Sean Callanan549c9f72010-07-13 21:41:46 +00002369 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002370 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002371
Sean Callanan549c9f72010-07-13 21:41:46 +00002372 if (log)
Zachary Turnera746e8e2014-07-02 17:24:07 +00002373 log->Printf(" \"%s\" (\"%s\") placed at %" PRIu64,
Greg Clayton7b462cc2010-10-15 22:48:33 +00002374 name.GetCString(),
Sean Callananc70ed462011-10-25 18:36:40 +00002375 decl->getNameAsString().c_str(),
Sean Callanan549c9f72010-07-13 21:41:46 +00002376 offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002377
Sean Callananc70ed462011-10-25 18:36:40 +00002378 if (value)
Sean Callanan92adcac2011-01-13 08:53:35 +00002379 {
Sean Callananc70ed462011-10-25 18:36:40 +00002380 if (log)
2381 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002382
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002383 FunctionValueCache body_result_maker ([this, name, offset_type, offset, argument, value] (llvm::Function *function)->llvm::Value * {
2384 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2385 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2386 // entry in order to produce the static variable that the AST thinks it is accessing.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002387
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002388 llvm::Instruction *entry_instruction = llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002389
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002390 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
2391 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument,
2392 offset_int,
2393 "",
2394 entry_instruction);
2395
2396 if (name == m_result_name && !m_result_is_pointer)
2397 {
2398 BitCastInst *bit_cast = new BitCastInst(get_element_ptr,
2399 value->getType()->getPointerTo(),
2400 "",
2401 entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002402
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002403 LoadInst *load = new LoadInst(bit_cast, "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002404
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002405 return load;
2406 }
2407 else
2408 {
2409 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002410
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002411 return bit_cast;
2412 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002413 });
2414
Sean Callananc70ed462011-10-25 18:36:40 +00002415 if (Constant *constant = dyn_cast<Constant>(value))
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002416 {
2417 UnfoldConstant(constant, body_result_maker, m_entry_instruction_finder);
2418 }
2419 else if (Instruction *instruction = dyn_cast<Instruction>(value))
2420 {
2421 value->replaceAllUsesWith(body_result_maker.GetValue(instruction->getParent()->getParent()));
2422 }
Sean Callananc70ed462011-10-25 18:36:40 +00002423 else
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002424 {
2425 if (log)
2426 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(value).c_str());
2427 return false;
2428 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002429
Sean Callananc70ed462011-10-25 18:36:40 +00002430 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2431 var->eraseFromParent();
2432 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002433 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002434
Sean Callanan549c9f72010-07-13 21:41:46 +00002435 if (log)
Greg Clayton6fea17e2014-03-03 19:15:20 +00002436 log->Printf("Total structure [align %" PRId64 ", size %" PRIu64 "]", (int64_t)alignment, (uint64_t)size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002437
Sean Callanan549c9f72010-07-13 21:41:46 +00002438 return true;
2439}
2440
Sean Callanan79763a42011-05-23 21:40:23 +00002441llvm::Constant *
Greg Clayton5160ce52013-03-27 23:08:40 +00002442IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
Sean Callanan79763a42011-05-23 21:40:23 +00002443{
Sean Callanan439dcae2013-12-20 19:55:02 +00002444 llvm::Constant *offset_int = ConstantInt::get(m_intptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002445
Sean Callanancc427fa2011-07-30 02:42:06 +00002446 llvm::Constant *offset_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002447
Sean Callanancc427fa2011-07-30 02:42:06 +00002448 offset_array[0] = offset_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002449
Sean Callanancc427fa2011-07-30 02:42:06 +00002450 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002451
Sean Callanancc427fa2011-07-30 02:42:06 +00002452 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, offsets);
Sean Callanan79763a42011-05-23 21:40:23 +00002453 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002454
Sean Callanan79763a42011-05-23 21:40:23 +00002455 return reloc_getbitcast;
2456}
2457
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002458bool
Sean Callanan79763a42011-05-23 21:40:23 +00002459IRForTarget::CompleteDataAllocation ()
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002460{
Greg Clayton5160ce52013-03-27 23:08:40 +00002461 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan79763a42011-05-23 21:40:23 +00002462
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002463 if (!m_data_allocator.GetStream().GetSize())
Sean Callanan79763a42011-05-23 21:40:23 +00002464 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002465
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002466 lldb::addr_t allocation = m_data_allocator.Allocate();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002467
Sean Callanan79763a42011-05-23 21:40:23 +00002468 if (log)
2469 {
2470 if (allocation)
2471 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2472 else
2473 log->Printf("Failed to allocate static data");
2474 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002475
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002476 if (!allocation || allocation == LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +00002477 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002478
Sean Callanan439dcae2013-12-20 19:55:02 +00002479 Constant *relocated_addr = ConstantInt::get(m_intptr_ty, (uint64_t)allocation);
Sean Callanan79763a42011-05-23 21:40:23 +00002480 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002481
Sean Callanan79763a42011-05-23 21:40:23 +00002482 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002483
Sean Callanan79763a42011-05-23 21:40:23 +00002484 m_reloc_placeholder->eraseFromParent();
2485
2486 return true;
2487}
2488
Sean Callanan2ab712f22010-07-03 01:35:46 +00002489bool
Sean Callanan3d654b32012-09-24 22:25:51 +00002490IRForTarget::StripAllGVs (Module &llvm_module)
2491{
Greg Clayton5160ce52013-03-27 23:08:40 +00002492 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3d654b32012-09-24 22:25:51 +00002493 std::vector<GlobalVariable *> global_vars;
2494 std::set<GlobalVariable *>erased_vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002495
Sean Callanan3d654b32012-09-24 22:25:51 +00002496 bool erased = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002497
Sean Callanan3d654b32012-09-24 22:25:51 +00002498 while (erased)
2499 {
2500 erased = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002501
Sean Callanan339f6152014-03-11 19:19:16 +00002502 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002503 {
Sean Callanan339f6152014-03-11 19:19:16 +00002504 global_var.removeDeadConstantUsers();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002505
Sean Callanan339f6152014-03-11 19:19:16 +00002506 if (global_var.use_empty())
Sean Callanan3d654b32012-09-24 22:25:51 +00002507 {
2508 if (log)
2509 log->Printf("Did remove %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002510 PrintValue(&global_var).c_str());
2511 global_var.eraseFromParent();
Sean Callanan3d654b32012-09-24 22:25:51 +00002512 erased = true;
2513 break;
2514 }
2515 }
2516 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002517
Sean Callanan339f6152014-03-11 19:19:16 +00002518 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002519 {
Sean Callanan339f6152014-03-11 19:19:16 +00002520 GlobalValue::user_iterator ui = global_var.user_begin();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002521
Jim Inghamd77557d2012-11-26 19:54:04 +00002522 if (log)
2523 log->Printf("Couldn't remove %s because of %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002524 PrintValue(&global_var).c_str(),
Jim Inghamd77557d2012-11-26 19:54:04 +00002525 PrintValue(*ui).c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +00002526 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002527
Sean Callanan3d654b32012-09-24 22:25:51 +00002528 return true;
2529}
2530
2531bool
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002532IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002533{
Greg Clayton5160ce52013-03-27 23:08:40 +00002534 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002535
Sean Callanan79763a42011-05-23 21:40:23 +00002536 m_module = &llvm_module;
Micah Villmow8468dbe2012-10-08 16:28:57 +00002537 m_target_data.reset(new DataLayout(m_module));
Sean Callanan439dcae2013-12-20 19:55:02 +00002538 m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), m_target_data->getPointerSizeInBits());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002539
Sean Callananc70ed462011-10-25 18:36:40 +00002540 if (log)
2541 {
2542 std::string s;
2543 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002544
Sean Callananc70ed462011-10-25 18:36:40 +00002545 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002546
Sean Callananc70ed462011-10-25 18:36:40 +00002547 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002548
Sean Callananc70ed462011-10-25 18:36:40 +00002549 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
2550 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002551
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002552 Function* main_function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002553
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002554 if (!main_function)
2555 {
2556 if (log)
2557 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002558
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002559 if (m_error_stream)
2560 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
2561
2562 return false;
2563 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002564
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002565 if (!FixFunctionLinkage (*main_function))
2566 {
2567 if (log)
2568 log->Printf("Couldn't fix the linkage for the function");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002569
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002570 return false;
2571 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002572
Sean Callanan439dcae2013-12-20 19:55:02 +00002573 llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002574
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002575 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
Sean Callanan439dcae2013-12-20 19:55:02 +00002576 int8_ty,
Sean Callanan3d654b32012-09-24 22:25:51 +00002577 false /* IsConstant */,
Sean Callanan79763a42011-05-23 21:40:23 +00002578 GlobalVariable::InternalLinkage,
Sean Callanan439dcae2013-12-20 19:55:02 +00002579 Constant::getNullValue(int8_ty),
Sean Callanan79763a42011-05-23 21:40:23 +00002580 "reloc_placeholder",
2581 NULL /* InsertBefore */,
Sean Callanan3d654b32012-09-24 22:25:51 +00002582 GlobalVariable::NotThreadLocal /* ThreadLocal */,
Sean Callanan79763a42011-05-23 21:40:23 +00002583 0 /* AddressSpace */);
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002584
Sean Callanand1e5b432010-08-12 01:56:52 +00002585 ////////////////////////////////////////////////////////////
Greg Clayton7b462cc2010-10-15 22:48:33 +00002586 // Replace $__lldb_expr_result with a persistent variable
Sean Callanand1e5b432010-08-12 01:56:52 +00002587 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002588
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002589 if (!CreateResultVariable(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002590 {
2591 if (log)
2592 log->Printf("CreateResultVariable() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002593
Sean Callanan3989fb92011-01-27 01:07:04 +00002594 // CreateResultVariable() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002595
Sean Callanand1e5b432010-08-12 01:56:52 +00002596 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002597 }
Sean Callanan6e6d4a62012-07-21 02:02:15 +00002598
Sean Callananea685ae2011-11-01 17:33:54 +00002599 if (log && log->GetVerbose())
Sean Callanan79763a42011-05-23 21:40:23 +00002600 {
2601 std::string s;
2602 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002603
Sean Callanan79763a42011-05-23 21:40:23 +00002604 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002605
Sean Callanan79763a42011-05-23 21:40:23 +00002606 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002607
Sean Callanan79763a42011-05-23 21:40:23 +00002608 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2609 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002610
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002611 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2612 fi != fe;
2613 ++fi)
2614 {
2615 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002616
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002617 if (function->begin() == function->end())
2618 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002619
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002620 Function::iterator bbi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002621
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002622 for (bbi = function->begin();
2623 bbi != function->end();
2624 ++bbi)
2625 {
2626 if (!RemoveGuards(*bbi))
2627 {
2628 if (log)
2629 log->Printf("RemoveGuards() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002630
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002631 // RemoveGuards() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002632
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002633 return false;
2634 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002635
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002636 if (!RewritePersistentAllocs(*bbi))
2637 {
2638 if (log)
2639 log->Printf("RewritePersistentAllocs() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002640
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002641 // RewritePersistentAllocs() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002642
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002643 return false;
2644 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002645
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002646 if (!RemoveCXAAtExit(*bbi))
2647 {
2648 if (log)
2649 log->Printf("RemoveCXAAtExit() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002650
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002651 // RemoveCXAAtExit() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002652
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002653 return false;
2654 }
2655 }
2656 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002657
Sean Callananafe16a72010-11-17 23:00:36 +00002658 ///////////////////////////////////////////////////////////////////////////////
2659 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2660 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002661
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002662 if (!RewriteObjCConstStrings())
Sean Callanan17827832010-12-13 22:46:15 +00002663 {
2664 if (log)
2665 log->Printf("RewriteObjCConstStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002666
Sean Callanan3989fb92011-01-27 01:07:04 +00002667 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002668
Sean Callananafe16a72010-11-17 23:00:36 +00002669 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002670 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002671
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002672 ///////////////////////////////
2673 // Resolve function pointers
2674 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002675
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002676 if (!ResolveFunctionPointers(llvm_module))
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002677 {
2678 if (log)
2679 log->Printf("ResolveFunctionPointers() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002680
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002681 // ResolveFunctionPointers() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002682
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002683 return false;
2684 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002685
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002686 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2687 fi != fe;
2688 ++fi)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002689 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002690 llvm::Function *function = fi;
2691
2692 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2693 bbi != bbe;
2694 ++bbi)
Sean Callanan17827832010-12-13 22:46:15 +00002695 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002696 if (!RewriteObjCSelectors(*bbi))
2697 {
2698 if (log)
2699 log->Printf("RewriteObjCSelectors() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002700
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002701 // RewriteObjCSelectors() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002702
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002703 return false;
2704 }
Sean Callanan17827832010-12-13 22:46:15 +00002705 }
Sean Callananbad134f2012-07-27 19:25:24 +00002706 }
Sean Callanan2235f322010-08-11 03:57:18 +00002707
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002708 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2709 fi != fe;
2710 ++fi)
Sean Callananbad134f2012-07-27 19:25:24 +00002711 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002712 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002713
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002714 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2715 bbi != bbe;
2716 ++bbi)
Sean Callanan79763a42011-05-23 21:40:23 +00002717 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002718 if (!ResolveCalls(*bbi))
2719 {
2720 if (log)
2721 log->Printf("ResolveCalls() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002722
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002723 // ResolveCalls() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002724
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002725 return false;
2726 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002727
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002728 if (!ReplaceStaticLiterals(*bbi))
2729 {
2730 if (log)
2731 log->Printf("ReplaceStaticLiterals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002732
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002733 return false;
2734 }
Sean Callanan79763a42011-05-23 21:40:23 +00002735 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002736 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002737
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002738 ////////////////////////////////////////////////////////////////////////
2739 // Run function-level passes that only make sense on the main function
Sean Callanan038df5032010-09-30 21:18:25 +00002740 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002741
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002742 if (!ResolveExternals(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002743 {
2744 if (log)
2745 log->Printf("ResolveExternals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002746
Sean Callanan3989fb92011-01-27 01:07:04 +00002747 // ResolveExternals() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002748
Sean Callanana4e55172010-11-08 00:31:32 +00002749 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002750 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002751
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002752 if (!ReplaceVariables(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002753 {
2754 if (log)
2755 log->Printf("ReplaceVariables() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002756
Sean Callanan3989fb92011-01-27 01:07:04 +00002757 // ReplaceVariables() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002758
Sean Callanan038df5032010-09-30 21:18:25 +00002759 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002760 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002761
Sean Callanan79763a42011-05-23 21:40:23 +00002762 if (!ReplaceStrings())
2763 {
2764 if (log)
2765 log->Printf("ReplaceStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002766
Sean Callanan79763a42011-05-23 21:40:23 +00002767 return false;
2768 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002769
Sean Callanan79763a42011-05-23 21:40:23 +00002770 if (!CompleteDataAllocation())
2771 {
2772 if (log)
2773 log->Printf("CompleteDataAllocation() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002774
Sean Callanan79763a42011-05-23 21:40:23 +00002775 return false;
2776 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002777
Sean Callanan3d654b32012-09-24 22:25:51 +00002778 if (!StripAllGVs(llvm_module))
2779 {
2780 if (log)
2781 log->Printf("StripAllGVs() failed");
2782 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002783
Sean Callananea685ae2011-11-01 17:33:54 +00002784 if (log && log->GetVerbose())
Sean Callanan549c9f72010-07-13 21:41:46 +00002785 {
Sean Callanancc54bd32010-07-28 01:00:59 +00002786 std::string s;
2787 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002788
Sean Callanan79763a42011-05-23 21:40:23 +00002789 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002790
Sean Callanancc54bd32010-07-28 01:00:59 +00002791 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002792
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002793 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan2ab712f22010-07-03 01:35:46 +00002794 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002795
2796 return true;
Sean Callanan2ab712f22010-07-03 01:35:46 +00002797}
2798
2799void
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002800IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002801{
2802}
2803
2804PassManagerType
2805IRForTarget::getPotentialPassManagerType() const
2806{
2807 return PMT_ModulePassManager;
2808}