blob: b91e1b46f88ecdc8a0129251cbd5e744679ac0d0 [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 {
406 MDNode *metadata_node = named_metadata->getOperand(node_index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000407
Sean Callanan63697e52011-05-07 01:06:41 +0000408 if (!metadata_node)
409 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000410
Sean Callanan63697e52011-05-07 01:06:41 +0000411 if (metadata_node->getNumOperands() != 2)
412 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000413
Sean Callanan63697e52011-05-07 01:06:41 +0000414 if (metadata_node->getOperand(0) != global_val)
415 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000416
Sean Callanan63697e52011-05-07 01:06:41 +0000417 ConstantInt *constant_int = dyn_cast<ConstantInt>(metadata_node->getOperand(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000418
Sean Callanan63697e52011-05-07 01:06:41 +0000419 if (!constant_int)
420 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000421
Sean Callanan63697e52011-05-07 01:06:41 +0000422 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000423
Sean Callanan63697e52011-05-07 01:06:41 +0000424 return reinterpret_cast<clang::NamedDecl *>(ptr);
425 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000426
Sean Callanan63697e52011-05-07 01:06:41 +0000427 return NULL;
428}
429
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000430clang::NamedDecl *
431IRForTarget::DeclForGlobal (GlobalValue *global_val)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000432{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000433 return DeclForGlobal(global_val, m_module);
434}
435
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000436bool
Sean Callanan79763a42011-05-23 21:40:23 +0000437IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanand1e5b432010-08-12 01:56:52 +0000438{
Greg Clayton5160ce52013-03-27 23:08:40 +0000439 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000440
Sean Callanan9e6ed532010-09-13 21:34:21 +0000441 if (!m_resolve_vars)
442 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000443
Sean Callanan9e6ed532010-09-13 21:34:21 +0000444 // Find the result variable. If it doesn't exist, we can give up right here.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000445
Sean Callanan79763a42011-05-23 21:40:23 +0000446 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000447
Sean Callanancc427fa2011-07-30 02:42:06 +0000448 std::string result_name_str;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000449 const char *result_name = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000450
Sean Callananfc55f5d2010-09-21 00:44:12 +0000451 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
452 vi != ve;
453 ++vi)
454 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000455 result_name_str = vi->first().str();
456 const char *value_name = result_name_str.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000457
Sean Callanancc427fa2011-07-30 02:42:06 +0000458 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000459 strncmp(value_name, "_ZGV", 4))
Sean Callanan92adcac2011-01-13 08:53:35 +0000460 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000461 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000462 m_result_is_pointer = true;
463 break;
464 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000465
Sean Callanancc427fa2011-07-30 02:42:06 +0000466 if (strstr(value_name, "$__lldb_expr_result") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000467 strncmp(value_name, "_ZGV", 4))
Sean Callanan46ae9e52010-09-28 21:13:03 +0000468 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000469 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000470 m_result_is_pointer = false;
Sean Callanan46ae9e52010-09-28 21:13:03 +0000471 break;
472 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000473 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000474
Sean Callananfc55f5d2010-09-21 00:44:12 +0000475 if (!result_name)
476 {
477 if (log)
478 log->PutCString("Couldn't find result variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000479
Richard Mitton00dec202013-10-11 19:44:23 +0000480 return true;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000481 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000482
Sean Callanan46ae9e52010-09-28 21:13:03 +0000483 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000484 log->Printf("Result name: \"%s\"", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000485
Sean Callanan79763a42011-05-23 21:40:23 +0000486 Value *result_value = m_module->getNamedValue(result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000487
Sean Callanand1e5b432010-08-12 01:56:52 +0000488 if (!result_value)
489 {
490 if (log)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000491 log->PutCString("Result variable had no data");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000492
Sean Callanan3989fb92011-01-27 01:07:04 +0000493 if (m_error_stream)
494 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 +0000495
Sean Callananfc55f5d2010-09-21 00:44:12 +0000496 return false;
Sean Callanand1e5b432010-08-12 01:56:52 +0000497 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000498
Sean Callanand1e5b432010-08-12 01:56:52 +0000499 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000500 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000501
Sean Callanand1e5b432010-08-12 01:56:52 +0000502 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000503
Sean Callanand1e5b432010-08-12 01:56:52 +0000504 if (!result_global)
505 {
506 if (log)
507 log->PutCString("Result variable isn't a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000508
Sean Callanan3989fb92011-01-27 01:07:04 +0000509 if (m_error_stream)
510 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 +0000511
Sean Callanand1e5b432010-08-12 01:56:52 +0000512 return false;
513 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000514
Sean Callanan79763a42011-05-23 21:40:23 +0000515 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan63697e52011-05-07 01:06:41 +0000516 if (!result_decl)
Sean Callanand1e5b432010-08-12 01:56:52 +0000517 {
518 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000519 log->PutCString("Result variable doesn't have a corresponding Decl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000520
Sean Callanan3989fb92011-01-27 01:07:04 +0000521 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000522 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 +0000523
Sean Callanand1e5b432010-08-12 01:56:52 +0000524 return false;
525 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000526
Sean Callanan63697e52011-05-07 01:06:41 +0000527 if (log)
Sean Callanand1e5b432010-08-12 01:56:52 +0000528 {
Sean Callanan63697e52011-05-07 01:06:41 +0000529 std::string decl_desc_str;
530 raw_string_ostream decl_desc_stream(decl_desc_str);
531 result_decl->print(decl_desc_stream);
532 decl_desc_stream.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000533
Sean Callanan63697e52011-05-07 01:06:41 +0000534 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanand1e5b432010-08-12 01:56:52 +0000535 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000536
Sean Callanan63697e52011-05-07 01:06:41 +0000537 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
538 if (!result_var)
Sean Callanand1e5b432010-08-12 01:56:52 +0000539 {
540 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000541 log->PutCString("Result variable Decl isn't a VarDecl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000542
Sean Callanan3989fb92011-01-27 01:07:04 +0000543 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000544 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 +0000545
Sean Callanand1e5b432010-08-12 01:56:52 +0000546 return false;
547 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000548
Sean Callanand1e5b432010-08-12 01:56:52 +0000549 // Get the next available result name from m_decl_map and create the persistent
550 // variable for it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000551
Sean Callanan63697e52011-05-07 01:06:41 +0000552 // If the result is an Lvalue, it is emitted as a pointer; see
553 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan92adcac2011-01-13 08:53:35 +0000554 if (m_result_is_pointer)
555 {
Sean Callanan63697e52011-05-07 01:06:41 +0000556 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanan78e37602011-01-27 04:42:51 +0000557 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000558
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000559 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
560 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000561
Sean Callanan5780f9d2011-12-08 19:04:34 +0000562 if (pointer_pointertype)
563 {
564 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000565
Sean Callanan5780f9d2011-12-08 19:04:34 +0000566 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
567 &result_decl->getASTContext());
568 }
569 else if (pointer_objcobjpointertype)
570 {
571 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000572
Sean Callanan5780f9d2011-12-08 19:04:34 +0000573 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
574 &result_decl->getASTContext());
575 }
576 else
Sean Callanan92adcac2011-01-13 08:53:35 +0000577 {
578 if (log)
579 log->PutCString("Expected result to have pointer type, but it did not");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000580
Sean Callanan3989fb92011-01-27 01:07:04 +0000581 if (m_error_stream)
582 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 +0000583
Sean Callanan92adcac2011-01-13 08:53:35 +0000584 return false;
585 }
Sean Callanan92adcac2011-01-13 08:53:35 +0000586 }
587 else
588 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000589 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Sean Callanan00f43622011-11-18 03:28:09 +0000590 &result_decl->getASTContext());
591 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Greg Clayton57ee3062013-07-11 22:46:58 +0000593 if (m_result_type.GetBitSize() == 0)
Sean Callanan00f43622011-11-18 03:28:09 +0000594 {
595 lldb_private::StreamString type_desc_stream;
596 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000597
Sean Callanan00f43622011-11-18 03:28:09 +0000598 if (log)
599 log->Printf("Result type has size 0");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000600
Sean Callanan00f43622011-11-18 03:28:09 +0000601 if (m_error_stream)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000602 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
Sean Callanan00f43622011-11-18 03:28:09 +0000603 type_desc_stream.GetData());
Sean Callanan960534c2011-12-21 23:44:05 +0000604 return false;
Sean Callanan92adcac2011-01-13 08:53:35 +0000605 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000606
Sean Callanan63697e52011-05-07 01:06:41 +0000607 if (log)
608 {
609 lldb_private::StreamString type_desc_stream;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000610 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611
Sean Callanan00f43622011-11-18 03:28:09 +0000612 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
Sean Callanan63697e52011-05-07 01:06:41 +0000613 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000614
Sean Callanan1582ee62013-04-18 22:06:33 +0000615 m_result_name = lldb_private::ConstString("$RESULT_NAME");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000616
Sean Callanand1e5b432010-08-12 01:56:52 +0000617 if (log)
Greg Claytonfaac1112013-03-14 18:31:44 +0000618 log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000619 m_result_name.GetCString(),
Greg Clayton57ee3062013-07-11 22:46:58 +0000620 m_result_type.GetByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000621
Sean Callanand1e5b432010-08-12 01:56:52 +0000622 // Construct a new result global and set up its metadata
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000623
624 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanand1e5b432010-08-12 01:56:52 +0000625 result_global->getType()->getElementType(),
626 false, /* not constant */
627 GlobalValue::ExternalLinkage,
628 NULL, /* no initializer */
Sean Callanan92adcac2011-01-13 08:53:35 +0000629 m_result_name.GetCString ());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Sean Callanand1e5b432010-08-12 01:56:52 +0000631 // It's too late in compilation to create a new VarDecl for this, but we don't
632 // need to. We point the metadata at the old VarDecl. This creates an odd
633 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton7b462cc2010-10-15 22:48:33 +0000634 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanand1e5b432010-08-12 01:56:52 +0000635 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
636 // fixed up.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000637
Sean Callanan79763a42011-05-23 21:40:23 +0000638 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan63697e52011-05-07 01:06:41 +0000639 reinterpret_cast<uint64_t>(result_decl),
Sean Callanand1e5b432010-08-12 01:56:52 +0000640 false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000641
Sean Callanand1e5b432010-08-12 01:56:52 +0000642 llvm::Value* values[2];
643 values[0] = new_result_global;
644 values[1] = new_constant_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000645
Sean Callanand12cf8bb2011-05-15 22:34:38 +0000646 ArrayRef<Value*> value_ref(values, 2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000647
Sean Callanan79763a42011-05-23 21:40:23 +0000648 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
649 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanand1e5b432010-08-12 01:56:52 +0000650 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000651
Sean Callanand1e5b432010-08-12 01:56:52 +0000652 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000653 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan1e87fff2010-09-07 22:43:19 +0000654 PrintValue(result_global).c_str(),
Sean Callanand1e5b432010-08-12 01:56:52 +0000655 PrintValue(new_result_global).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000656
Sean Callanan339f6152014-03-11 19:19:16 +0000657 if (result_global->use_empty())
Sean Callanan1e87fff2010-09-07 22:43:19 +0000658 {
659 // We need to synthesize a store for this variable, because otherwise
660 // there's nothing to put into its equivalent persistent variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000661
Greg Clayton7b462cc2010-10-15 22:48:33 +0000662 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000663 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000664
Sean Callanan1e87fff2010-09-07 22:43:19 +0000665 if (!first_entry_instruction)
666 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000667
Sean Callanan1e87fff2010-09-07 22:43:19 +0000668 if (!result_global->hasInitializer())
669 {
670 if (log)
671 log->Printf("Couldn't find initializer for unused variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000672
Sean Callanan3989fb92011-01-27 01:07:04 +0000673 if (m_error_stream)
674 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 +0000675
Sean Callanan1e87fff2010-09-07 22:43:19 +0000676 return false;
677 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000678
Sean Callanan1e87fff2010-09-07 22:43:19 +0000679 Constant *initializer = result_global->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000680
Greg Clayton9c139312011-02-05 02:28:58 +0000681 StoreInst *synthesized_store = new StoreInst(initializer,
682 new_result_global,
683 first_entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000684
Sean Callanan1e87fff2010-09-07 22:43:19 +0000685 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000686 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000687 }
688 else
689 {
690 result_global->replaceAllUsesWith(new_result_global);
691 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000692
Sean Callanan1582ee62013-04-18 22:06:33 +0000693 if (!m_decl_map->AddPersistentVariable(result_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000694 m_result_name,
Sean Callanan1582ee62013-04-18 22:06:33 +0000695 m_result_type,
696 true,
697 m_result_is_pointer))
698 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000699
Sean Callanand1e5b432010-08-12 01:56:52 +0000700 result_global->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000701
Sean Callanand1e5b432010-08-12 01:56:52 +0000702 return true;
703}
704
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000705bool
Sean Callanan79763a42011-05-23 21:40:23 +0000706IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000707 llvm::GlobalVariable *cstr)
Sean Callananafe16a72010-11-17 23:00:36 +0000708{
Greg Clayton5160ce52013-03-27 23:08:40 +0000709 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000710
Sean Callanancc427fa2011-07-30 02:42:06 +0000711 Type *ns_str_ty = ns_str->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000712
Sean Callanancc427fa2011-07-30 02:42:06 +0000713 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +0000714 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
715 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000716
Sean Callananafe16a72010-11-17 23:00:36 +0000717 if (!m_CFStringCreateWithBytes)
718 {
719 lldb::addr_t CFStringCreateWithBytes_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000720
Sean Callananafe16a72010-11-17 23:00:36 +0000721 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000722
Sean Callananafe16a72010-11-17 23:00:36 +0000723 if (!m_decl_map->GetFunctionAddress (g_CFStringCreateWithBytes_str, CFStringCreateWithBytes_addr))
724 {
725 if (log)
726 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000727
Sean Callanan3989fb92011-01-27 01:07:04 +0000728 if (m_error_stream)
729 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000730
Sean Callananafe16a72010-11-17 23:00:36 +0000731 return false;
732 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000733
Sean Callananafe16a72010-11-17 23:00:36 +0000734 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000735 log->Printf("Found CFStringCreateWithBytes at 0x%" PRIx64, CFStringCreateWithBytes_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000736
Sean Callananafe16a72010-11-17 23:00:36 +0000737 // Build the function type:
738 //
739 // CFStringRef CFStringCreateWithBytes (
740 // CFAllocatorRef alloc,
741 // const UInt8 *bytes,
742 // CFIndex numBytes,
743 // CFStringEncoding encoding,
744 // Boolean isExternalRepresentation
745 // );
746 //
747 // We make the following substitutions:
748 //
749 // CFStringRef -> i8*
750 // CFAllocatorRef -> i8*
751 // UInt8 * -> i8*
752 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
753 // CFStringEncoding -> i32
754 // Boolean -> i8
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000755
Sean Callanancc427fa2011-07-30 02:42:06 +0000756 Type *arg_type_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000757
Sean Callanancc427fa2011-07-30 02:42:06 +0000758 arg_type_array[0] = i8_ptr_ty;
759 arg_type_array[1] = i8_ptr_ty;
Sean Callanan439dcae2013-12-20 19:55:02 +0000760 arg_type_array[2] = m_intptr_ty;
Sean Callanancc427fa2011-07-30 02:42:06 +0000761 arg_type_array[3] = i32_ty;
762 arg_type_array[4] = i8_ty;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000763
Sean Callanancc427fa2011-07-30 02:42:06 +0000764 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000765
Sean Callanan79763a42011-05-23 21:40:23 +0000766 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000767
Sean Callananafe16a72010-11-17 23:00:36 +0000768 // Build the constant containing the pointer to the function
769 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000770 Constant *CFSCWB_addr_int = ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000771 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
772 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000773
Sean Callanand2b465f2012-02-09 03:22:41 +0000774 ConstantDataSequential *string_array = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000775
Sean Callanan229ce2d2011-02-10 22:17:53 +0000776 if (cstr)
Sean Callanand2b465f2012-02-09 03:22:41 +0000777 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000778
Sean Callananafe16a72010-11-17 23:00:36 +0000779 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000780 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000781 Constant *numBytes_arg = ConstantInt::get(m_intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000782 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
783 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000784
Sean Callanancc427fa2011-07-30 02:42:06 +0000785 Value *argument_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000786
Sean Callanancc427fa2011-07-30 02:42:06 +0000787 argument_array[0] = alloc_arg;
788 argument_array[1] = bytes_arg;
789 argument_array[2] = numBytes_arg;
790 argument_array[3] = encoding_arg;
791 argument_array[4] = isExternal_arg;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000792
Sean Callanancc427fa2011-07-30 02:42:06 +0000793 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000794
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000795 FunctionValueCache CFSCWB_Caller ([this, &CFSCWB_arguments] (llvm::Function *function)->llvm::Value * {
796 return CallInst::Create(m_CFStringCreateWithBytes,
797 CFSCWB_arguments,
798 "CFStringCreateWithBytes",
799 llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function)));
800 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000801
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000802 if (!UnfoldConstant(ns_str, CFSCWB_Caller, m_entry_instruction_finder))
Sean Callananafe16a72010-11-17 23:00:36 +0000803 {
804 if (log)
805 log->PutCString("Couldn't replace the NSString with the result of the call");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000806
Sean Callanan3989fb92011-01-27 01:07:04 +0000807 if (m_error_stream)
808 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 +0000809
Sean Callananafe16a72010-11-17 23:00:36 +0000810 return false;
811 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000812
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000813 ns_str->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000814
Sean Callananafe16a72010-11-17 23:00:36 +0000815 return true;
816}
817
818bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000819IRForTarget::RewriteObjCConstStrings()
Sean Callananafe16a72010-11-17 23:00:36 +0000820{
Greg Clayton5160ce52013-03-27 23:08:40 +0000821 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000822
Sean Callanan79763a42011-05-23 21:40:23 +0000823 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000824
Sean Callananafe16a72010-11-17 23:00:36 +0000825 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
826 vi != ve;
827 ++vi)
828 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000829 std::string value_name = vi->first().str();
830 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000831
Sean Callanancc427fa2011-07-30 02:42:06 +0000832 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callananafe16a72010-11-17 23:00:36 +0000833 {
834 Value *nsstring_value = vi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000835
Sean Callananafe16a72010-11-17 23:00:36 +0000836 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000837
Sean Callananafe16a72010-11-17 23:00:36 +0000838 if (!nsstring_global)
839 {
840 if (log)
841 log->PutCString("NSString variable is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000842
Sean Callanan3989fb92011-01-27 01:07:04 +0000843 if (m_error_stream)
844 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 +0000845
Sean Callananafe16a72010-11-17 23:00:36 +0000846 return false;
847 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000848
Sean Callananafe16a72010-11-17 23:00:36 +0000849 if (!nsstring_global->hasInitializer())
850 {
851 if (log)
852 log->PutCString("NSString variable does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000853
Sean Callanan3989fb92011-01-27 01:07:04 +0000854 if (m_error_stream)
855 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 +0000856
Sean Callananafe16a72010-11-17 23:00:36 +0000857 return false;
858 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000859
Sean Callananafe16a72010-11-17 23:00:36 +0000860 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000861
Sean Callananafe16a72010-11-17 23:00:36 +0000862 if (!nsstring_struct)
863 {
864 if (log)
865 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000866
Sean Callanan3989fb92011-01-27 01:07:04 +0000867 if (m_error_stream)
868 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 +0000869
Sean Callananafe16a72010-11-17 23:00:36 +0000870 return false;
871 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000872
Sean Callananafe16a72010-11-17 23:00:36 +0000873 // We expect the following structure:
874 //
875 // struct {
876 // int *isa;
877 // int flags;
878 // char *str;
879 // long length;
880 // };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000881
Sean Callananafe16a72010-11-17 23:00:36 +0000882 if (nsstring_struct->getNumOperands() != 4)
883 {
884 if (log)
885 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 +0000886
Sean Callanan3989fb92011-01-27 01:07:04 +0000887 if (m_error_stream)
888 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 +0000889
Sean Callananafe16a72010-11-17 23:00:36 +0000890 return false;
891 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000892
Sean Callananafe16a72010-11-17 23:00:36 +0000893 Constant *nsstring_member = nsstring_struct->getOperand(2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000894
Sean Callananafe16a72010-11-17 23:00:36 +0000895 if (!nsstring_member)
896 {
897 if (log)
898 log->PutCString("NSString initializer's str element was empty");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000899
Sean Callanan3989fb92011-01-27 01:07:04 +0000900 if (m_error_stream)
901 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 +0000902
Sean Callananafe16a72010-11-17 23:00:36 +0000903 return false;
904 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000905
Sean Callananafe16a72010-11-17 23:00:36 +0000906 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000907
Sean Callananafe16a72010-11-17 23:00:36 +0000908 if (!nsstring_expr)
909 {
910 if (log)
911 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000912
Sean Callanan3989fb92011-01-27 01:07:04 +0000913 if (m_error_stream)
914 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 +0000915
Sean Callananafe16a72010-11-17 23:00:36 +0000916 return false;
917 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000918
Sean Callananafe16a72010-11-17 23:00:36 +0000919 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
920 {
921 if (log)
922 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 +0000923
Sean Callanan3989fb92011-01-27 01:07:04 +0000924 if (m_error_stream)
925 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 +0000926
Sean Callananafe16a72010-11-17 23:00:36 +0000927 return false;
928 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000929
Sean Callananafe16a72010-11-17 23:00:36 +0000930 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000931
Sean Callananafe16a72010-11-17 23:00:36 +0000932 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000933
Sean Callananafe16a72010-11-17 23:00:36 +0000934 if (!cstr_global)
935 {
936 if (log)
937 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000938
Sean Callanan3989fb92011-01-27 01:07:04 +0000939 if (m_error_stream)
940 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 +0000941
Sean Callananafe16a72010-11-17 23:00:36 +0000942 return false;
943 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000944
Sean Callananafe16a72010-11-17 23:00:36 +0000945 if (!cstr_global->hasInitializer())
946 {
947 if (log)
948 log->PutCString("NSString initializer's str element does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000949
Sean Callanan3989fb92011-01-27 01:07:04 +0000950 if (m_error_stream)
951 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 +0000952
Sean Callananafe16a72010-11-17 23:00:36 +0000953 return false;
954 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000955
Sean Callanan229ce2d2011-02-10 22:17:53 +0000956 /*
Sean Callananafe16a72010-11-17 23:00:36 +0000957 if (!cstr_array)
958 {
959 if (log)
960 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000961
Sean Callanan3989fb92011-01-27 01:07:04 +0000962 if (m_error_stream)
963 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 +0000964
Sean Callananafe16a72010-11-17 23:00:36 +0000965 return false;
966 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000967
Sean Callananafe16a72010-11-17 23:00:36 +0000968 if (!cstr_array->isCString())
969 {
970 if (log)
971 log->PutCString("NSString initializer's str element is not a C string array");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000972
Sean Callanan3989fb92011-01-27 01:07:04 +0000973 if (m_error_stream)
974 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 +0000975
Sean Callananafe16a72010-11-17 23:00:36 +0000976 return false;
977 }
Sean Callanan229ce2d2011-02-10 22:17:53 +0000978 */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000979
Sean Callanand2b465f2012-02-09 03:22:41 +0000980 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000981
Sean Callananafe16a72010-11-17 23:00:36 +0000982 if (log)
Sean Callanan229ce2d2011-02-10 22:17:53 +0000983 {
984 if (cstr_array)
Sean Callanand2b465f2012-02-09 03:22:41 +0000985 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 +0000986 else
Sean Callanancc427fa2011-07-30 02:42:06 +0000987 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000988 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000989
Sean Callanan229ce2d2011-02-10 22:17:53 +0000990 if (!cstr_array)
991 cstr_global = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000992
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000993 if (!RewriteObjCConstString(nsstring_global, cstr_global))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000994 {
Sean Callananafe16a72010-11-17 23:00:36 +0000995 if (log)
996 log->PutCString("Error rewriting the constant string");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000997
Sean Callanan3989fb92011-01-27 01:07:04 +0000998 // We don't print an error message here because RewriteObjCConstString has done so for us.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000999
Sean Callananafe16a72010-11-17 23:00:36 +00001000 return false;
1001 }
Sean Callananafe16a72010-11-17 23:00:36 +00001002 }
1003 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001004
Sean Callananafe16a72010-11-17 23:00:36 +00001005 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
1006 vi != ve;
1007 ++vi)
1008 {
Sean Callanancc427fa2011-07-30 02:42:06 +00001009 std::string value_name = vi->first().str();
1010 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001011
Sean Callanancc427fa2011-07-30 02:42:06 +00001012 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callananafe16a72010-11-17 23:00:36 +00001013 {
1014 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001015
Sean Callananafe16a72010-11-17 23:00:36 +00001016 if (!gv)
1017 {
1018 if (log)
1019 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001020
Sean Callanan3989fb92011-01-27 01:07:04 +00001021 if (m_error_stream)
1022 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 +00001023
Sean Callananafe16a72010-11-17 23:00:36 +00001024 return false;
1025 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001026
Sean Callananafe16a72010-11-17 23:00:36 +00001027 gv->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001028
Sean Callananafe16a72010-11-17 23:00:36 +00001029 break;
1030 }
1031 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001032
Sean Callananafe16a72010-11-17 23:00:36 +00001033 return true;
1034}
1035
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001036static bool IsObjCSelectorRef (Value *value)
Sean Callanan5300d372010-07-31 01:32:05 +00001037{
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001038 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001039
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001040 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("\01L_OBJC_SELECTOR_REFERENCES_"))
Sean Callanan5300d372010-07-31 01:32:05 +00001041 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001042
Sean Callanan5300d372010-07-31 01:32:05 +00001043 return true;
1044}
1045
Sean Callanan3989fb92011-01-27 01:07:04 +00001046// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001047bool
Sean Callanan79763a42011-05-23 21:40:23 +00001048IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callanan5300d372010-07-31 01:32:05 +00001049{
Greg Clayton5160ce52013-03-27 23:08:40 +00001050 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001051
1052 LoadInst *load = dyn_cast<LoadInst>(selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001053
Sean Callanan5300d372010-07-31 01:32:05 +00001054 if (!load)
1055 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001056
Sean Callanan5300d372010-07-31 01:32:05 +00001057 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
1058 //
1059 // %tmp = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_" ; <i8*>
1060 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
1061 //
1062 // where %obj is the object pointer and %tmp is the selector.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001063 //
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001064 // @"\01L_OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
1065 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001066
Sean Callanan5300d372010-07-31 01:32:05 +00001067 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001068
Sean Callanan5300d372010-07-31 01:32:05 +00001069 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001070
Sean Callanan5300d372010-07-31 01:32:05 +00001071 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
1072 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001073
Sean Callanan5300d372010-07-31 01:32:05 +00001074 Constant *osr_initializer = _objc_selector_references_->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001075
Sean Callanan5300d372010-07-31 01:32:05 +00001076 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001077
Sean Callanan5300d372010-07-31 01:32:05 +00001078 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
1079 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001080
Sean Callanan5300d372010-07-31 01:32:05 +00001081 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
1082
1083 if (!osr_initializer_base)
1084 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001085
Sean Callanan5300d372010-07-31 01:32:05 +00001086 // Find the string's initializer (a ConstantArray) and get the string from it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001087
Sean Callanan5300d372010-07-31 01:32:05 +00001088 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001089
Sean Callanan5300d372010-07-31 01:32:05 +00001090 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
1091 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001092
Sean Callanan5300d372010-07-31 01:32:05 +00001093 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
1094
Sean Callanand2b465f2012-02-09 03:22:41 +00001095 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001096
Sean Callanan5300d372010-07-31 01:32:05 +00001097 if (!omvn_initializer_array->isString())
1098 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001099
Sean Callanan5300d372010-07-31 01:32:05 +00001100 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001101
Sean Callanan5300d372010-07-31 01:32:05 +00001102 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001103 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001104
Sean Callanan5300d372010-07-31 01:32:05 +00001105 // Construct a call to sel_registerName
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001106
Sean Callanan5300d372010-07-31 01:32:05 +00001107 if (!m_sel_registerName)
1108 {
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001109 lldb::addr_t sel_registerName_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001110
Greg Clayton7b462cc2010-10-15 22:48:33 +00001111 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001112 if (!m_decl_map->GetFunctionAddress (g_sel_registerName_str, sel_registerName_addr))
Sean Callanan5300d372010-07-31 01:32:05 +00001113 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001114
Sean Callananbe3a1b12010-10-26 00:31:56 +00001115 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001116 log->Printf("Found sel_registerName at 0x%" PRIx64, sel_registerName_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001117
Sean Callanan5300d372010-07-31 01:32:05 +00001118 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001119
Sean Callanan5300d372010-07-31 01:32:05 +00001120 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callanan79763a42011-05-23 21:40:23 +00001121 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callanan5300d372010-07-31 01:32:05 +00001122 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanancc427fa2011-07-30 02:42:06 +00001123 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001124
Sean Callanancc427fa2011-07-30 02:42:06 +00001125 Type *type_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001126
Sean Callanancc427fa2011-07-30 02:42:06 +00001127 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001128
Sean Callanancc427fa2011-07-30 02:42:06 +00001129 ArrayRef<Type *> srN_arg_types(type_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001130
Sean Callanan5300d372010-07-31 01:32:05 +00001131 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001132
Sean Callanan5300d372010-07-31 01:32:05 +00001133 // Build the constant containing the pointer to the function
Sean Callanan5300d372010-07-31 01:32:05 +00001134 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Sean Callanan439dcae2013-12-20 19:55:02 +00001135 Constant *srN_addr_int = ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
Sean Callanan5300d372010-07-31 01:32:05 +00001136 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
1137 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001138
Sean Callanancc427fa2011-07-30 02:42:06 +00001139 Value *argument_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001140
Sean Callanan79763a42011-05-23 21:40:23 +00001141 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001142
Sean Callanancc427fa2011-07-30 02:42:06 +00001143 argument_array[0] = omvn_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001144
Sean Callanancc427fa2011-07-30 02:42:06 +00001145 ArrayRef<Value *> srN_arguments(argument_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001146
1147 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanancc427fa2011-07-30 02:42:06 +00001148 srN_arguments,
Sean Callananafe16a72010-11-17 23:00:36 +00001149 "sel_registerName",
Sean Callanan5300d372010-07-31 01:32:05 +00001150 selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001151
Sean Callanan5300d372010-07-31 01:32:05 +00001152 // Replace the load with the call in all users
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001153
Sean Callanan5300d372010-07-31 01:32:05 +00001154 selector_load->replaceAllUsesWith(srN_call);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001155
Sean Callanan5300d372010-07-31 01:32:05 +00001156 selector_load->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001157
Sean Callanan5300d372010-07-31 01:32:05 +00001158 return true;
1159}
1160
1161bool
Sean Callanan79763a42011-05-23 21:40:23 +00001162IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callanan5300d372010-07-31 01:32:05 +00001163{
Greg Clayton5160ce52013-03-27 23:08:40 +00001164 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +00001165
1166 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001167
Sean Callanan5300d372010-07-31 01:32:05 +00001168 typedef SmallVector <Instruction*, 2> InstrList;
1169 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001170
Sean Callanan5300d372010-07-31 01:32:05 +00001171 InstrList selector_loads;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001172
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001173 for (ii = basic_block.begin();
1174 ii != basic_block.end();
Sean Callanan5300d372010-07-31 01:32:05 +00001175 ++ii)
1176 {
1177 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001178
Sean Callanan5300d372010-07-31 01:32:05 +00001179 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001180 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callanan5300d372010-07-31 01:32:05 +00001181 selector_loads.push_back(&inst);
1182 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001183
Sean Callanan5300d372010-07-31 01:32:05 +00001184 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001185
Sean Callanan5300d372010-07-31 01:32:05 +00001186 for (iter = selector_loads.begin();
1187 iter != selector_loads.end();
1188 ++iter)
1189 {
Sean Callanan79763a42011-05-23 21:40:23 +00001190 if (!RewriteObjCSelector(*iter))
Sean Callanan5300d372010-07-31 01:32:05 +00001191 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001192 if (m_error_stream)
1193 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 +00001194
Enrico Granata20edcdb2011-07-19 18:03:25 +00001195 if (log)
Sean Callanan5300d372010-07-31 01:32:05 +00001196 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001197
Sean Callanan5300d372010-07-31 01:32:05 +00001198 return false;
1199 }
1200 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001201
Sean Callanan5300d372010-07-31 01:32:05 +00001202 return true;
1203}
1204
Sean Callanan3989fb92011-01-27 01:07:04 +00001205// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001206bool
Sean Callanan79763a42011-05-23 21:40:23 +00001207IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanan2235f322010-08-11 03:57:18 +00001208{
Greg Clayton5160ce52013-03-27 23:08:40 +00001209 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane1175b72011-01-13 21:23:32 +00001210
Sean Callanan2235f322010-08-11 03:57:18 +00001211 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001212
Sean Callanan2235f322010-08-11 03:57:18 +00001213 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1214
1215 if (!alloc_md || !alloc_md->getNumOperands())
1216 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001217
Sean Callanan2235f322010-08-11 03:57:18 +00001218 ConstantInt *constant_int = dyn_cast<ConstantInt>(alloc_md->getOperand(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001219
Sean Callanan2235f322010-08-11 03:57:18 +00001220 if (!constant_int)
1221 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001222
Sean Callanan2235f322010-08-11 03:57:18 +00001223 // We attempt to register this as a new persistent variable with the DeclMap.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001224
Sean Callanan2235f322010-08-11 03:57:18 +00001225 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001226
Sean Callanand1e5b432010-08-12 01:56:52 +00001227 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001228
Sean Callanand1e5b432010-08-12 01:56:52 +00001229 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
1230 &decl->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001231
Greg Clayton7b462cc2010-10-15 22:48:33 +00001232 StringRef decl_name (decl->getName());
1233 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan92adcac2011-01-13 08:53:35 +00001234 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanan2235f322010-08-11 03:57:18 +00001235 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001236
Sean Callanan79763a42011-05-23 21:40:23 +00001237 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanane1175b72011-01-13 21:23:32 +00001238 alloc->getType(),
Sean Callanan2235f322010-08-11 03:57:18 +00001239 false, /* not constant */
1240 GlobalValue::ExternalLinkage,
1241 NULL, /* no initializer */
1242 alloc->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001243
Sean Callanan2235f322010-08-11 03:57:18 +00001244 // What we're going to do here is make believe this was a regular old external
1245 // variable. That means we need to make the metadata valid.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001246
Sean Callanan585c0ec82012-07-04 01:26:26 +00001247 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001248
Sean Callanan2235f322010-08-11 03:57:18 +00001249 llvm::Value* values[2];
1250 values[0] = persistent_global;
1251 values[1] = constant_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001252
Sean Callanand12cf8bb2011-05-15 22:34:38 +00001253 ArrayRef<llvm::Value*> value_ref(values, 2);
Sean Callanan2235f322010-08-11 03:57:18 +00001254
Sean Callanan79763a42011-05-23 21:40:23 +00001255 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanan2235f322010-08-11 03:57:18 +00001256 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001257
Sean Callanane1175b72011-01-13 21:23:32 +00001258 // Now, since the variable is a pointer variable, we will drop in a load of that
1259 // pointer variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001260
Sean Callanane1175b72011-01-13 21:23:32 +00001261 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001262
Sean Callanane1175b72011-01-13 21:23:32 +00001263 if (log)
1264 log->Printf("Replacing \"%s\" with \"%s\"",
1265 PrintValue(alloc).c_str(),
1266 PrintValue(persistent_load).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001267
Sean Callanane1175b72011-01-13 21:23:32 +00001268 alloc->replaceAllUsesWith(persistent_load);
Sean Callanan2235f322010-08-11 03:57:18 +00001269 alloc->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001270
Sean Callanan2235f322010-08-11 03:57:18 +00001271 return true;
1272}
1273
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001274bool
Sean Callanan79763a42011-05-23 21:40:23 +00001275IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanan2235f322010-08-11 03:57:18 +00001276{
Sean Callanan9e6ed532010-09-13 21:34:21 +00001277 if (!m_resolve_vars)
1278 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001279
Greg Clayton5160ce52013-03-27 23:08:40 +00001280 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001281
Sean Callanan2235f322010-08-11 03:57:18 +00001282 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001283
Sean Callanan2235f322010-08-11 03:57:18 +00001284 typedef SmallVector <Instruction*, 2> InstrList;
1285 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001286
Sean Callanan2235f322010-08-11 03:57:18 +00001287 InstrList pvar_allocs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001288
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001289 for (ii = basic_block.begin();
1290 ii != basic_block.end();
Sean Callanan2235f322010-08-11 03:57:18 +00001291 ++ii)
1292 {
1293 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001294
Sean Callanan2235f322010-08-11 03:57:18 +00001295 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callananf694a552011-01-21 22:30:25 +00001296 {
1297 llvm::StringRef alloc_name = alloc->getName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001298
Sean Callananf694a552011-01-21 22:30:25 +00001299 if (alloc_name.startswith("$") &&
1300 !alloc_name.startswith("$__lldb"))
1301 {
1302 if (alloc_name.find_first_of("0123456789") == 1)
1303 {
1304 if (log)
1305 log->Printf("Rejecting a numeric persistent variable.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001306
Sean Callanan3989fb92011-01-27 01:07:04 +00001307 if (m_error_stream)
1308 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 +00001309
Sean Callananf694a552011-01-21 22:30:25 +00001310 return false;
1311 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001312
Sean Callanan2235f322010-08-11 03:57:18 +00001313 pvar_allocs.push_back(alloc);
Sean Callananf694a552011-01-21 22:30:25 +00001314 }
1315 }
Sean Callanan2235f322010-08-11 03:57:18 +00001316 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001317
Sean Callanan2235f322010-08-11 03:57:18 +00001318 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001319
Sean Callanan2235f322010-08-11 03:57:18 +00001320 for (iter = pvar_allocs.begin();
1321 iter != pvar_allocs.end();
1322 ++iter)
1323 {
Sean Callanan79763a42011-05-23 21:40:23 +00001324 if (!RewritePersistentAlloc(*iter))
Sean Callanan2235f322010-08-11 03:57:18 +00001325 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001326 if (m_error_stream)
1327 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001328
Enrico Granata20edcdb2011-07-19 18:03:25 +00001329 if (log)
Sean Callanan2235f322010-08-11 03:57:18 +00001330 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001331
Sean Callanan2235f322010-08-11 03:57:18 +00001332 return false;
1333 }
1334 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001335
Sean Callanan2235f322010-08-11 03:57:18 +00001336 return true;
1337}
1338
Sean Callananc70ed462011-10-25 18:36:40 +00001339bool
1340IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1341{
1342 if (!initializer)
1343 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001344
Greg Clayton5160ce52013-03-27 23:08:40 +00001345 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc70ed462011-10-25 18:36:40 +00001346
1347 if (log && log->GetVerbose())
1348 log->Printf(" MaterializeInitializer(%p, %s)", data, PrintValue(initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001349
Sean Callananc70ed462011-10-25 18:36:40 +00001350 Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001351
Sean Callananc70ed462011-10-25 18:36:40 +00001352 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1353 {
1354 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1355 return true;
1356 }
Sean Callanand2b465f2012-02-09 03:22:41 +00001357 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
Sean Callananc70ed462011-10-25 18:36:40 +00001358 {
1359 if (array_initializer->isString())
1360 {
1361 std::string array_initializer_string = array_initializer->getAsString();
1362 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1363 }
1364 else
1365 {
1366 ArrayType *array_initializer_type = array_initializer->getType();
1367 Type *array_element_type = array_initializer_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001368
Sean Callananc70ed462011-10-25 18:36:40 +00001369 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001370
Andy Gibbsa297a972013-06-19 19:04:53 +00001371 for (unsigned i = 0; i < array_initializer->getNumOperands(); ++i)
Sean Callananc70ed462011-10-25 18:36:40 +00001372 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001373 Value *operand_value = array_initializer->getOperand(i);
1374 Constant *operand_constant = dyn_cast<Constant>(operand_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001375
Sean Callanand2b465f2012-02-09 03:22:41 +00001376 if (!operand_constant)
1377 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001378
Sean Callanand2b465f2012-02-09 03:22:41 +00001379 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
Sean Callananc70ed462011-10-25 18:36:40 +00001380 return false;
1381 }
1382 }
1383 return true;
1384 }
1385 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1386 {
1387 StructType *struct_initializer_type = struct_initializer->getType();
1388 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1389
Andy Gibbsa297a972013-06-19 19:04:53 +00001390 for (unsigned i = 0;
Sean Callananc70ed462011-10-25 18:36:40 +00001391 i < struct_initializer->getNumOperands();
1392 ++i)
1393 {
1394 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1395 return false;
1396 }
1397 return true;
1398 }
Sean Callanan76ee3e72013-04-24 19:50:12 +00001399 else if (isa<ConstantAggregateZero>(initializer))
1400 {
1401 memset(data, 0, m_target_data->getTypeStoreSize(initializer_type));
1402 return true;
1403 }
Sean Callananc70ed462011-10-25 18:36:40 +00001404 return false;
1405}
1406
1407bool
1408IRForTarget::MaterializeInternalVariable (GlobalVariable *global_variable)
1409{
1410 if (GlobalVariable::isExternalLinkage(global_variable->getLinkage()))
1411 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001412
Sean Callananfe5d1392011-11-15 19:13:54 +00001413 if (global_variable == m_reloc_placeholder)
1414 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001415
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001416 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001417
Sean Callananc70ed462011-10-25 18:36:40 +00001418 llvm::Type *variable_type = global_variable->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001419
Sean Callananc70ed462011-10-25 18:36:40 +00001420 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001421
Sean Callananc70ed462011-10-25 18:36:40 +00001422 llvm::Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001423
Sean Callananc70ed462011-10-25 18:36:40 +00001424 size_t size = m_target_data->getTypeAllocSize(initializer_type);
Sean Callanane3333d62012-06-08 22:20:41 +00001425 size_t align = m_target_data->getPrefTypeAlignment(initializer_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001426
Sean Callanane3333d62012-06-08 22:20:41 +00001427 const size_t mask = (align - 1);
1428 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001429 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sean Callanane3333d62012-06-08 22:20:41 +00001430 offset = aligned_offset;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001431
Sean Callananc70ed462011-10-25 18:36:40 +00001432 lldb_private::DataBufferHeap data(size, '\0');
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001433
Sean Callananc70ed462011-10-25 18:36:40 +00001434 if (initializer)
1435 if (!MaterializeInitializer(data.GetBytes(), initializer))
1436 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001437
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001438 m_data_allocator.GetStream().Write(data.GetBytes(), data.GetByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001439
Sean Callananc70ed462011-10-25 18:36:40 +00001440 Constant *new_pointer = BuildRelocation(variable_type, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001441
Sean Callananc70ed462011-10-25 18:36:40 +00001442 global_variable->replaceAllUsesWith(new_pointer);
1443
1444 global_variable->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001445
Sean Callananc70ed462011-10-25 18:36:40 +00001446 return true;
1447}
1448
Sean Callanan3989fb92011-01-27 01:07:04 +00001449// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001450bool
Sean Callanan79763a42011-05-23 21:40:23 +00001451IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001452{
Greg Clayton5160ce52013-03-27 23:08:40 +00001453 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001454
Sean Callanand7a1ca22010-12-02 19:47:57 +00001455 if (log)
Sean Callanan88339f02010-12-06 22:16:55 +00001456 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001457
Greg Clayton7b462cc2010-10-15 22:48:33 +00001458 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callanan4cf04d22010-08-03 00:23:29 +00001459 {
Sean Callanan5666b672010-08-04 01:02:13 +00001460 switch (constant_expr->getOpcode())
Sean Callanan4cf04d22010-08-03 00:23:29 +00001461 {
Sean Callanan5666b672010-08-04 01:02:13 +00001462 default:
1463 break;
1464 case Instruction::GetElementPtr:
1465 case Instruction::BitCast:
Sean Callanan4cf04d22010-08-03 00:23:29 +00001466 Value *s = constant_expr->getOperand(0);
Sean Callanan79763a42011-05-23 21:40:23 +00001467 if (!MaybeHandleVariable(s))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001468 return false;
Sean Callanan4cf04d22010-08-03 00:23:29 +00001469 }
1470 }
Sean Callanand6e04ae2010-12-03 19:51:05 +00001471 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001472 {
Sean Callananc70ed462011-10-25 18:36:40 +00001473 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1474 return MaterializeInternalVariable(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001475
Sean Callanan79763a42011-05-23 21:40:23 +00001476 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001477
Sean Callanan5300d372010-07-31 01:32:05 +00001478 if (!named_decl)
1479 {
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001480 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001481 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001482
Sean Callanan14f0b0e2010-12-06 00:56:39 +00001483 if (!global_variable->hasExternalLinkage())
1484 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001485
Sean Callanan5300d372010-07-31 01:32:05 +00001486 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001487 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001488
Sean Callanan5300d372010-07-31 01:32:05 +00001489 return false;
1490 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001491
Greg Clayton7b462cc2010-10-15 22:48:33 +00001492 std::string name (named_decl->getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001493
Greg Clayton57ee3062013-07-11 22:46:58 +00001494 clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1495 if (value_decl == NULL)
Sean Callananea22d422010-07-16 00:09:46 +00001496 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001497
1498 lldb_private::ClangASTType clang_type(&value_decl->getASTContext(), value_decl->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001499
Sean Callanan77eaf442011-07-08 00:39:14 +00001500 const Type *value_type = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001501
Sean Callanane1175b72011-01-13 21:23:32 +00001502 if (name[0] == '$')
Sean Callanan92adcac2011-01-13 08:53:35 +00001503 {
1504 // The $__lldb_expr_result name indicates the the return value has allocated as
1505 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1506 // accesses to this static variable need to be redirected to the result of dereferencing
1507 // a pointer that is passed in as one of the arguments.
1508 //
1509 // Consequently, when reporting the size of the type, we report a pointer type pointing
1510 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanane1175b72011-01-13 21:23:32 +00001511 //
1512 // We also do this for any user-declared persistent variables.
Greg Clayton57ee3062013-07-11 22:46:58 +00001513 clang_type = clang_type.GetPointerType();
Sean Callanan92adcac2011-01-13 08:53:35 +00001514 value_type = PointerType::get(global_variable->getType(), 0);
1515 }
1516 else
1517 {
Sean Callanan92adcac2011-01-13 08:53:35 +00001518 value_type = global_variable->getType();
1519 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001520
Greg Clayton57ee3062013-07-11 22:46:58 +00001521 const uint64_t value_size = clang_type.GetByteSize();
Zachary Turnera746e8e2014-07-02 17:24:07 +00001522 lldb::offset_t value_alignment = (clang_type.GetTypeBitAlign() + 7ull) / 8ull;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001523
Sean Callanan038df5032010-09-30 21:18:25 +00001524 if (log)
Greg Clayton57ee3062013-07-11 22:46:58 +00001525 {
Zachary Turnera746e8e2014-07-02 17:24:07 +00001526 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %" PRIu64 ", align %" PRIu64 "]",
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001527 name.c_str(),
Greg Clayton57ee3062013-07-11 22:46:58 +00001528 clang_type.GetQualType().getAsString().c_str(),
1529 PrintType(value_type).c_str(),
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001530 value_size,
Sean Callanan038df5032010-09-30 21:18:25 +00001531 value_alignment);
Greg Clayton57ee3062013-07-11 22:46:58 +00001532 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001533
1534
Sean Callanan64dfc9a2010-08-23 23:09:38 +00001535 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001536 lldb_private::ConstString (name.c_str()),
1537 llvm_value_ptr,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001538 value_size,
Sean Callanan4edba2d2010-07-27 02:07:53 +00001539 value_alignment))
Sean Callananaf8e96c2011-08-01 17:41:38 +00001540 {
1541 if (!global_variable->hasExternalLinkage())
1542 return true;
Sean Callanan0ff3bf92013-04-11 17:57:16 +00001543 else if (HandleSymbol (global_variable))
1544 return true;
Sean Callananaf8e96c2011-08-01 17:41:38 +00001545 else
1546 return false;
1547 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001548 }
Sean Callanan4a5fcbb2010-12-03 03:02:31 +00001549 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001550 {
1551 if (log)
1552 log->Printf("Function pointers aren't handled right now");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001553
Sean Callanand7a1ca22010-12-02 19:47:57 +00001554 return false;
1555 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001556
Sean Callanan549c9f72010-07-13 21:41:46 +00001557 return true;
1558}
1559
Sean Callanan3989fb92011-01-27 01:07:04 +00001560// This function does not report errors; its callers are responsible.
Sean Callanan549c9f72010-07-13 21:41:46 +00001561bool
Sean Callanan79763a42011-05-23 21:40:23 +00001562IRForTarget::HandleSymbol (Value *symbol)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001563{
Greg Clayton5160ce52013-03-27 23:08:40 +00001564 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001565
Sean Callananc3a16002011-01-17 23:42:46 +00001566 lldb_private::ConstString name(symbol->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001567
Sean Callanan947ccc72011-12-01 02:04:16 +00001568 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001569
Greg Clayton084db102011-06-23 04:25:29 +00001570 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc3a16002011-01-17 23:42:46 +00001571 {
1572 if (log)
1573 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001574
Sean Callananc3a16002011-01-17 23:42:46 +00001575 return false;
1576 }
1577
1578 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001579 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001580
Sean Callanancc427fa2011-07-30 02:42:06 +00001581 Type *symbol_type = symbol->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001582
Sean Callanan439dcae2013-12-20 19:55:02 +00001583 Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001584
Sean Callananc3a16002011-01-17 23:42:46 +00001585 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001586
Sean Callananc3a16002011-01-17 23:42:46 +00001587 if (log)
1588 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001589
Sean Callananc3a16002011-01-17 23:42:46 +00001590 symbol->replaceAllUsesWith(symbol_addr_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001591
Sean Callananc3a16002011-01-17 23:42:46 +00001592 return true;
1593}
1594
1595bool
Sean Callanan79763a42011-05-23 21:40:23 +00001596IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanan85a0a832010-10-05 22:26:43 +00001597{
Greg Clayton5160ce52013-03-27 23:08:40 +00001598 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001599
Sean Callanand7a1ca22010-12-02 19:47:57 +00001600 if (log)
1601 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001602
Sean Callananafe16a72010-11-17 23:00:36 +00001603 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanan85a0a832010-10-05 22:26:43 +00001604 op_index < num_ops;
1605 ++op_index)
Sean Callanan79763a42011-05-23 21:40:23 +00001606 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan3989fb92011-01-27 01:07:04 +00001607 {
1608 if (m_error_stream)
1609 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 +00001610
Sean Callanan85a0a832010-10-05 22:26:43 +00001611 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001612 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001613
Sean Callanan85a0a832010-10-05 22:26:43 +00001614 return true;
1615}
1616
1617bool
Sean Callananfc89c142011-11-01 23:38:03 +00001618IRForTarget::HandleObjCClass(Value *classlist_reference)
1619{
Greg Clayton5160ce52013-03-27 23:08:40 +00001620 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc89c142011-11-01 23:38:03 +00001621
1622 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001623
Sean Callananfc89c142011-11-01 23:38:03 +00001624 if (!global_variable)
1625 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001626
Sean Callananfc89c142011-11-01 23:38:03 +00001627 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001628
Sean Callananfc89c142011-11-01 23:38:03 +00001629 if (!initializer)
1630 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001631
Sean Callananfc89c142011-11-01 23:38:03 +00001632 if (!initializer->hasName())
1633 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001634
Sean Callananfc89c142011-11-01 23:38:03 +00001635 StringRef name(initializer->getName());
1636 lldb_private::ConstString name_cstr(name.str().c_str());
Greg Clayton1075aca2011-12-03 20:02:42 +00001637 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001638
Sean Callananfc89c142011-11-01 23:38:03 +00001639 if (log)
1640 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 +00001641
Sean Callananfc89c142011-11-01 23:38:03 +00001642 if (class_ptr == LLDB_INVALID_ADDRESS)
1643 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001644
Ed Mastea8553092014-03-10 17:24:16 +00001645 if (global_variable->use_empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001646 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001647
Sean Callanan719a4d52013-03-23 01:01:16 +00001648 SmallVector<LoadInst *, 2> load_instructions;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001649
Ed Mastea8553092014-03-10 17:24:16 +00001650 for (llvm::User *u : global_variable->users())
Sean Callananfc89c142011-11-01 23:38:03 +00001651 {
Ed Mastea8553092014-03-10 17:24:16 +00001652 if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
Sean Callanan719a4d52013-03-23 01:01:16 +00001653 load_instructions.push_back(load_instruction);
Sean Callananfc89c142011-11-01 23:38:03 +00001654 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001655
Sean Callanan719a4d52013-03-23 01:01:16 +00001656 if (load_instructions.empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001657 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001658
Sean Callanan439dcae2013-12-20 19:55:02 +00001659 Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001660
Sean Callanan719a4d52013-03-23 01:01:16 +00001661 for (LoadInst *load_instruction : load_instructions)
1662 {
1663 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001664
Sean Callanan719a4d52013-03-23 01:01:16 +00001665 load_instruction->replaceAllUsesWith(class_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001666
Sean Callanan719a4d52013-03-23 01:01:16 +00001667 load_instruction->eraseFromParent();
1668 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001669
Sean Callananfc89c142011-11-01 23:38:03 +00001670 return true;
1671}
1672
1673bool
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001674IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1675{
1676 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001677
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001678 std::vector<CallInst *> calls_to_remove;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001679
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001680 for (ii = basic_block.begin();
1681 ii != basic_block.end();
1682 ++ii)
1683 {
1684 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001685
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001686 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001687
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001688 // MaybeHandleCallArguments handles error reporting; we are silent here
1689 if (!call)
1690 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001691
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001692 bool remove = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001693
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001694 llvm::Function *func = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001695
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001696 if (func && func->getName() == "__cxa_atexit")
1697 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001698
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001699 llvm::Value *val = call->getCalledValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001700
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001701 if (val && val->getName() == "__cxa_atexit")
1702 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001703
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001704 if (remove)
1705 calls_to_remove.push_back(call);
1706 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001707
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001708 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1709 ci != ce;
1710 ++ci)
1711 {
1712 (*ci)->eraseFromParent();
1713 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001714
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001715 return true;
1716}
1717
1718bool
Sean Callanan79763a42011-05-23 21:40:23 +00001719IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001720{
Sean Callanan2ab712f22010-07-03 01:35:46 +00001721 /////////////////////////////////////////////////////////////////////////
1722 // Prepare the current basic block for execution in the remote process
1723 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001724
Sean Callanan7ea35012010-07-27 21:39:39 +00001725 BasicBlock::iterator ii;
Sean Callanan549c9f72010-07-13 21:41:46 +00001726
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001727 for (ii = basic_block.begin();
1728 ii != basic_block.end();
Sean Callanan549c9f72010-07-13 21:41:46 +00001729 ++ii)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001730 {
Sean Callanan549c9f72010-07-13 21:41:46 +00001731 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001732
Sean Callanana4e55172010-11-08 00:31:32 +00001733 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001734
Sean Callanan3989fb92011-01-27 01:07:04 +00001735 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callanan79763a42011-05-23 21:40:23 +00001736 if (call && !MaybeHandleCallArguments(call))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001737 return false;
Sean Callanan2ab712f22010-07-03 01:35:46 +00001738 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001739
Sean Callanan2ab712f22010-07-03 01:35:46 +00001740 return true;
1741}
1742
Sean Callanana4e55172010-11-08 00:31:32 +00001743bool
Sean Callanan79763a42011-05-23 21:40:23 +00001744IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanana4e55172010-11-08 00:31:32 +00001745{
Greg Clayton5160ce52013-03-27 23:08:40 +00001746 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001747
Sean Callanan339f6152014-03-11 19:19:16 +00001748 for (GlobalVariable &global_var : m_module->globals())
Sean Callanana4e55172010-11-08 00:31:32 +00001749 {
Sean Callanan339f6152014-03-11 19:19:16 +00001750 std::string global_name = global_var.getName().str();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001751
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001752 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001753 log->Printf("Examining %s, DeclForGlobalValue returns %p",
Sean Callanan694e2442011-12-22 21:24:49 +00001754 global_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001755 static_cast<void*>(DeclForGlobal(&global_var)));
1756
Sean Callananfc89c142011-11-01 23:38:03 +00001757 if (global_name.find("OBJC_IVAR") == 0)
Sean Callananc3a16002011-01-17 23:42:46 +00001758 {
Sean Callanan339f6152014-03-11 19:19:16 +00001759 if (!HandleSymbol(&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001760 {
1761 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001762 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 +00001763
Sean Callananc3a16002011-01-17 23:42:46 +00001764 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001765 }
Sean Callananc3a16002011-01-17 23:42:46 +00001766 }
Sean Callananfc89c142011-11-01 23:38:03 +00001767 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1768 {
Sean Callanan339f6152014-03-11 19:19:16 +00001769 if (!HandleObjCClass(&global_var))
Sean Callananfc89c142011-11-01 23:38:03 +00001770 {
1771 if (m_error_stream)
1772 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 +00001773
Sean Callananfc89c142011-11-01 23:38:03 +00001774 return false;
1775 }
1776 }
Sean Callanan2ad66912013-04-24 21:25:20 +00001777 else if (global_name.find("OBJC_CLASSLIST_SUP_REFS_$") != global_name.npos)
1778 {
Sean Callanan339f6152014-03-11 19:19:16 +00001779 if (!HandleObjCClass(&global_var))
Sean Callanan2ad66912013-04-24 21:25:20 +00001780 {
1781 if (m_error_stream)
1782 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 +00001783
Sean Callanan2ad66912013-04-24 21:25:20 +00001784 return false;
1785 }
1786 }
Sean Callanan339f6152014-03-11 19:19:16 +00001787 else if (DeclForGlobal(&global_var))
Sean Callananc3a16002011-01-17 23:42:46 +00001788 {
Sean Callanan339f6152014-03-11 19:19:16 +00001789 if (!MaybeHandleVariable (&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001790 {
1791 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001792 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 +00001793
Sean Callananc3a16002011-01-17 23:42:46 +00001794 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001795 }
Sean Callananc3a16002011-01-17 23:42:46 +00001796 }
Sean Callanana4e55172010-11-08 00:31:32 +00001797 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001798
Sean Callanana4e55172010-11-08 00:31:32 +00001799 return true;
1800}
1801
Sean Callanan79763a42011-05-23 21:40:23 +00001802bool
1803IRForTarget::ReplaceStrings ()
1804{
Greg Clayton5160ce52013-03-27 23:08:40 +00001805 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001806
Sean Callanan79763a42011-05-23 21:40:23 +00001807 typedef std::map <GlobalVariable *, size_t> OffsetsTy;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001808
Sean Callanan79763a42011-05-23 21:40:23 +00001809 OffsetsTy offsets;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001810
Sean Callanan339f6152014-03-11 19:19:16 +00001811 for (GlobalVariable &gv : m_module->globals())
Sean Callanan79763a42011-05-23 21:40:23 +00001812 {
Sean Callanan339f6152014-03-11 19:19:16 +00001813 if (!gv.hasInitializer())
Sean Callanan79763a42011-05-23 21:40:23 +00001814 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001815
Sean Callanan339f6152014-03-11 19:19:16 +00001816 Constant *gc = gv.getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001817
Sean Callanan5207a342011-08-10 21:05:52 +00001818 std::string str;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001819
Sean Callanan5207a342011-08-10 21:05:52 +00001820 if (gc->isNullValue())
1821 {
1822 Type *gc_type = gc->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001823
Sean Callanan5207a342011-08-10 21:05:52 +00001824 ArrayType *gc_array_type = dyn_cast<ArrayType>(gc_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001825
Sean Callanan5207a342011-08-10 21:05:52 +00001826 if (!gc_array_type)
1827 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001828
Sean Callanan5207a342011-08-10 21:05:52 +00001829 Type *gc_element_type = gc_array_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001830
Sean Callanan5207a342011-08-10 21:05:52 +00001831 IntegerType *gc_integer_type = dyn_cast<IntegerType>(gc_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001832
Sean Callanan5207a342011-08-10 21:05:52 +00001833 if (gc_integer_type->getBitWidth() != 8)
1834 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001835
Sean Callanan5207a342011-08-10 21:05:52 +00001836 str = "";
1837 }
1838 else
1839 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001840 ConstantDataArray *gc_array = dyn_cast<ConstantDataArray>(gc);
Sean Callanan5207a342011-08-10 21:05:52 +00001841
1842 if (!gc_array)
1843 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001844
Sean Callanan5207a342011-08-10 21:05:52 +00001845 if (!gc_array->isCString())
1846 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001847
Sean Callanan5207a342011-08-10 21:05:52 +00001848 if (log)
1849 log->Printf("Found a GlobalVariable with string initializer %s", PrintValue(gc).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001850
Sean Callanan5207a342011-08-10 21:05:52 +00001851 str = gc_array->getAsString();
1852 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001853
Sean Callanan339f6152014-03-11 19:19:16 +00001854 offsets[&gv] = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001855
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001856 m_data_allocator.GetStream().Write(str.c_str(), str.length() + 1);
Sean Callanan79763a42011-05-23 21:40:23 +00001857 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001858
Sean Callanancc427fa2011-07-30 02:42:06 +00001859 Type *char_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001860
Sean Callanan79763a42011-05-23 21:40:23 +00001861 for (OffsetsTy::iterator oi = offsets.begin(), oe = offsets.end();
1862 oi != oe;
1863 ++oi)
1864 {
1865 GlobalVariable *gv = oi->first;
1866 size_t offset = oi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001867
Sean Callanan79763a42011-05-23 21:40:23 +00001868 Constant *new_initializer = BuildRelocation(char_ptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001869
Sean Callanan79763a42011-05-23 21:40:23 +00001870 if (log)
1871 log->Printf("Replacing GV %s with %s", PrintValue(gv).c_str(), PrintValue(new_initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001872
Ed Mastea8553092014-03-10 17:24:16 +00001873 for (llvm::User *u : gv->users())
Sean Callanan79763a42011-05-23 21:40:23 +00001874 {
1875 if (log)
Ed Mastea8553092014-03-10 17:24:16 +00001876 log->Printf("Found use %s", PrintValue(u).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001877
Ed Mastea8553092014-03-10 17:24:16 +00001878 ConstantExpr *const_expr = dyn_cast<ConstantExpr>(u);
1879 StoreInst *store_inst = dyn_cast<StoreInst>(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001880
Sean Callanan79763a42011-05-23 21:40:23 +00001881 if (const_expr)
1882 {
1883 if (const_expr->getOpcode() != Instruction::GetElementPtr)
1884 {
1885 if (log)
1886 log->Printf("Use (%s) of string variable is not a GetElementPtr constant", PrintValue(const_expr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001887
Sean Callanan79763a42011-05-23 21:40:23 +00001888 return false;
1889 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001890
Sean Callanan5207a342011-08-10 21:05:52 +00001891 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, const_expr->getOperand(0)->getType());
1892 Constant *new_gep = const_expr->getWithOperandReplaced(0, bit_cast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001893
Sean Callanan5207a342011-08-10 21:05:52 +00001894 const_expr->replaceAllUsesWith(new_gep);
Sean Callanan79763a42011-05-23 21:40:23 +00001895 }
1896 else if (store_inst)
1897 {
1898 Constant *bit_cast = ConstantExpr::getBitCast(new_initializer, store_inst->getValueOperand()->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001899
Sean Callanan79763a42011-05-23 21:40:23 +00001900 store_inst->setOperand(0, bit_cast);
1901 }
1902 else
1903 {
1904 if (log)
1905 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 +00001906
Sean Callanan79763a42011-05-23 21:40:23 +00001907 return false;
1908 }
1909 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001910
Sean Callanan79763a42011-05-23 21:40:23 +00001911 gv->eraseFromParent();
1912 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001913
Sean Callanan79763a42011-05-23 21:40:23 +00001914 return true;
1915}
1916
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001917bool
Sean Callanan79763a42011-05-23 21:40:23 +00001918IRForTarget::ReplaceStaticLiterals (llvm::BasicBlock &basic_block)
1919{
Greg Clayton5160ce52013-03-27 23:08:40 +00001920 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001921
Sean Callanan79763a42011-05-23 21:40:23 +00001922 typedef SmallVector <Value*, 2> ConstantList;
1923 typedef SmallVector <llvm::Instruction*, 2> UserList;
1924 typedef ConstantList::iterator ConstantIterator;
1925 typedef UserList::iterator UserIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001926
Sean Callanan79763a42011-05-23 21:40:23 +00001927 ConstantList static_constants;
1928 UserList static_users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001929
Sean Callanan79763a42011-05-23 21:40:23 +00001930 for (BasicBlock::iterator ii = basic_block.begin(), ie = basic_block.end();
1931 ii != ie;
1932 ++ii)
1933 {
1934 llvm::Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001935
Sean Callanan339f6152014-03-11 19:19:16 +00001936 for (Value *operand_val : inst.operand_values())
Sean Callanan79763a42011-05-23 21:40:23 +00001937 {
Sean Callanan79763a42011-05-23 21:40:23 +00001938 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001939
Sean Callanan822944c2012-04-26 20:51:20 +00001940 if (operand_constant_fp/* && operand_constant_fp->getType()->isX86_FP80Ty()*/)
Sean Callanan79763a42011-05-23 21:40:23 +00001941 {
1942 static_constants.push_back(operand_val);
1943 static_users.push_back(ii);
1944 }
1945 }
1946 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001947
Sean Callanan79763a42011-05-23 21:40:23 +00001948 ConstantIterator constant_iter;
1949 UserIterator user_iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001950
Sean Callanan79763a42011-05-23 21:40:23 +00001951 for (constant_iter = static_constants.begin(), user_iter = static_users.begin();
1952 constant_iter != static_constants.end();
1953 ++constant_iter, ++user_iter)
1954 {
1955 Value *operand_val = *constant_iter;
1956 llvm::Instruction *inst = *user_iter;
1957
1958 ConstantFP *operand_constant_fp = dyn_cast<ConstantFP>(operand_val);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001959
Sean Callanan79763a42011-05-23 21:40:23 +00001960 if (operand_constant_fp)
1961 {
Sean Callanan1582ee62013-04-18 22:06:33 +00001962 Type *operand_type = operand_constant_fp->getType();
1963
Sean Callanan79763a42011-05-23 21:40:23 +00001964 APFloat operand_apfloat = operand_constant_fp->getValueAPF();
1965 APInt operand_apint = operand_apfloat.bitcastToAPInt();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001966
Sean Callanan79763a42011-05-23 21:40:23 +00001967 const uint8_t* operand_raw_data = (const uint8_t*)operand_apint.getRawData();
1968 size_t operand_data_size = operand_apint.getBitWidth() / 8;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001969
Sean Callanan79763a42011-05-23 21:40:23 +00001970 if (log)
1971 {
1972 std::string s;
1973 raw_string_ostream ss(s);
1974 for (size_t index = 0;
1975 index < operand_data_size;
1976 ++index)
1977 {
1978 ss << (uint32_t)operand_raw_data[index];
1979 ss << " ";
1980 }
1981 ss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001982
Greg Clayton6fea17e2014-03-03 19:15:20 +00001983 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 +00001984 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001985
Sean Callanan79763a42011-05-23 21:40:23 +00001986 lldb_private::DataBufferHeap data(operand_data_size, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001987
Sean Callanan8dfb68e2013-03-19 00:10:07 +00001988 if (lldb::endian::InlHostByteOrder() != m_data_allocator.GetStream().GetByteOrder())
Sean Callanan79763a42011-05-23 21:40:23 +00001989 {
1990 uint8_t *data_bytes = data.GetBytes();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001991
Sean Callanan79763a42011-05-23 21:40:23 +00001992 for (size_t index = 0;
1993 index < operand_data_size;
1994 ++index)
1995 {
1996 data_bytes[index] = operand_raw_data[operand_data_size - (1 + index)];
1997 }
1998 }
1999 else
2000 {
2001 memcpy(data.GetBytes(), operand_raw_data, operand_data_size);
2002 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002003
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002004 uint64_t offset = m_data_allocator.GetStream().GetSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002005
Sean Callanane3333d62012-06-08 22:20:41 +00002006 size_t align = m_target_data->getPrefTypeAlignment(operand_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002007
Sean Callanane3333d62012-06-08 22:20:41 +00002008 const size_t mask = (align - 1);
2009 uint64_t aligned_offset = (offset + mask) & ~mask;
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002010 m_data_allocator.GetStream().PutNHex8(aligned_offset - offset, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002011
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002012 m_data_allocator.GetStream().Write(data.GetBytes(), operand_data_size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002013
Sean Callanancc427fa2011-07-30 02:42:06 +00002014 llvm::Type *fp_ptr_ty = operand_constant_fp->getType()->getPointerTo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002015
Sean Callanan1582ee62013-04-18 22:06:33 +00002016 Constant *new_pointer = BuildRelocation(fp_ptr_ty, aligned_offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002017
Sean Callanan79763a42011-05-23 21:40:23 +00002018 llvm::LoadInst *fp_load = new llvm::LoadInst(new_pointer, "fp_load", inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002019
Sean Callanan79763a42011-05-23 21:40:23 +00002020 operand_constant_fp->replaceAllUsesWith(fp_load);
2021 }
2022 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002023
Sean Callanan79763a42011-05-23 21:40:23 +00002024 return true;
2025}
2026
Sean Callanan7ea35012010-07-27 21:39:39 +00002027static bool isGuardVariableRef(Value *V)
Sean Callananddb46ef2010-07-24 01:37:44 +00002028{
Sean Callanan77eaf442011-07-08 00:39:14 +00002029 Constant *Old = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002030
Sean Callananafe16a72010-11-17 23:00:36 +00002031 if (!(Old = dyn_cast<Constant>(V)))
Sean Callananddb46ef2010-07-24 01:37:44 +00002032 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002033
Sean Callanan77eaf442011-07-08 00:39:14 +00002034 ConstantExpr *CE = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002035
Sean Callanane2ef6e32010-09-23 03:01:22 +00002036 if ((CE = dyn_cast<ConstantExpr>(V)))
2037 {
2038 if (CE->getOpcode() != Instruction::BitCast)
2039 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002040
Sean Callananafe16a72010-11-17 23:00:36 +00002041 Old = CE->getOperand(0);
Sean Callanane2ef6e32010-09-23 03:01:22 +00002042 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002043
Sean Callananafe16a72010-11-17 23:00:36 +00002044 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002045
Sean Callananddb46ef2010-07-24 01:37:44 +00002046 if (!GV || !GV->hasName() || !GV->getName().startswith("_ZGV"))
2047 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002048
Sean Callananddb46ef2010-07-24 01:37:44 +00002049 return true;
2050}
2051
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002052void
Sean Callanan79763a42011-05-23 21:40:23 +00002053IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callananddb46ef2010-07-24 01:37:44 +00002054{
Sean Callanan79763a42011-05-23 21:40:23 +00002055 Constant* zero(ConstantInt::get(Type::getInt8Ty(m_module->getContext()), 0, true));
Sean Callananddb46ef2010-07-24 01:37:44 +00002056
Ed Maste80e8cc62014-03-10 14:23:10 +00002057 for (llvm::User *u : guard_load->users())
Sean Callananb27a62f2010-07-27 01:17:28 +00002058 {
Ed Maste80e8cc62014-03-10 14:23:10 +00002059 if (isa<Constant>(u))
Sean Callananb27a62f2010-07-27 01:17:28 +00002060 {
2061 // do nothing for the moment
2062 }
2063 else
2064 {
Ed Maste80e8cc62014-03-10 14:23:10 +00002065 u->replaceUsesOfWith(guard_load, zero);
Sean Callananb27a62f2010-07-27 01:17:28 +00002066 }
2067 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002068
Sean Callananddb46ef2010-07-24 01:37:44 +00002069 guard_load->eraseFromParent();
2070}
2071
2072static void ExciseGuardStore(Instruction* guard_store)
2073{
2074 guard_store->eraseFromParent();
2075}
2076
2077bool
Sean Callanan79763a42011-05-23 21:40:23 +00002078IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002079{
Sean Callananddb46ef2010-07-24 01:37:44 +00002080 ///////////////////////////////////////////////////////
2081 // Eliminate any reference to guard variables found.
2082 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002083
Sean Callanan7ea35012010-07-27 21:39:39 +00002084 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002085
Sean Callanan7ea35012010-07-27 21:39:39 +00002086 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callananddb46ef2010-07-24 01:37:44 +00002087 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002088
Sean Callananddb46ef2010-07-24 01:37:44 +00002089 InstrList guard_loads;
2090 InstrList guard_stores;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002091
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002092 for (ii = basic_block.begin();
2093 ii != basic_block.end();
Sean Callananddb46ef2010-07-24 01:37:44 +00002094 ++ii)
2095 {
2096 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002097
Sean Callananddb46ef2010-07-24 01:37:44 +00002098 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
2099 if (isGuardVariableRef(load->getPointerOperand()))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002100 guard_loads.push_back(&inst);
2101
2102 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callananddb46ef2010-07-24 01:37:44 +00002103 if (isGuardVariableRef(store->getPointerOperand()))
2104 guard_stores.push_back(&inst);
2105 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002106
Sean Callananddb46ef2010-07-24 01:37:44 +00002107 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002108
Sean Callananddb46ef2010-07-24 01:37:44 +00002109 for (iter = guard_loads.begin();
2110 iter != guard_loads.end();
2111 ++iter)
Sean Callanan79763a42011-05-23 21:40:23 +00002112 TurnGuardLoadIntoZero(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002113
Sean Callananddb46ef2010-07-24 01:37:44 +00002114 for (iter = guard_stores.begin();
2115 iter != guard_stores.end();
2116 ++iter)
2117 ExciseGuardStore(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002118
Sean Callananddb46ef2010-07-24 01:37:44 +00002119 return true;
2120}
2121
Sean Callanan3989fb92011-01-27 01:07:04 +00002122// This function does not report errors; its callers are responsible.
Sean Callananafe16a72010-11-17 23:00:36 +00002123bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002124IRForTarget::UnfoldConstant(Constant *old_constant,
2125 FunctionValueCache &value_maker,
2126 FunctionValueCache &entry_instruction_finder)
Sean Callanan7618f4e2010-07-14 23:40:29 +00002127{
Greg Clayton5160ce52013-03-27 23:08:40 +00002128 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan7618f4e2010-07-14 23:40:29 +00002129
Sean Callanan2235f322010-08-11 03:57:18 +00002130 SmallVector<User*, 16> users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002131
Sean Callanan2235f322010-08-11 03:57:18 +00002132 // We do this because the use list might change, invalidating our iterator.
2133 // Much better to keep a work list ourselves.
Ed Maste80e8cc62014-03-10 14:23:10 +00002134 for (llvm::User *u : old_constant->users())
2135 users.push_back(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002136
Johnny Chen44805302011-07-19 19:48:13 +00002137 for (size_t i = 0;
Sean Callanan2235f322010-08-11 03:57:18 +00002138 i < users.size();
2139 ++i)
2140 {
2141 User *user = users[i];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002142
Sean Callanan7618f4e2010-07-14 23:40:29 +00002143 if (Constant *constant = dyn_cast<Constant>(user))
2144 {
2145 // synthesize a new non-constant equivalent of the constant
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002146
Sean Callanan7618f4e2010-07-14 23:40:29 +00002147 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
2148 {
2149 switch (constant_expr->getOpcode())
2150 {
2151 default:
2152 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002153 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002154 return false;
2155 case Instruction::BitCast:
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002156 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002157 FunctionValueCache bit_cast_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2158 // UnaryExpr
2159 // OperandList[0] is value
2160
2161 if (constant_expr->getOperand(0) != old_constant)
2162 return constant_expr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002163
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002164 return new BitCastInst(value_maker.GetValue(function),
2165 constant_expr->getType(),
2166 "",
2167 llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2168 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002169
Sean Callanan0e016fe2013-07-15 23:31:47 +00002170 if (!UnfoldConstant(constant_expr, bit_cast_maker, entry_instruction_finder))
2171 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002172 }
2173 break;
2174 case Instruction::GetElementPtr:
2175 {
2176 // GetElementPtrConstantExpr
2177 // OperandList[0] is base
2178 // OperandList[1]... are indices
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002179
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002180 FunctionValueCache get_element_pointer_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
2181 Value *ptr = constant_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002182
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002183 if (ptr == old_constant)
2184 ptr = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002185
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002186 std::vector<Value*> index_vector;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002187
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002188 unsigned operand_index;
2189 unsigned num_operands = constant_expr->getNumOperands();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002190
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002191 for (operand_index = 1;
2192 operand_index < num_operands;
2193 ++operand_index)
2194 {
2195 Value *operand = constant_expr->getOperand(operand_index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002196
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002197 if (operand == old_constant)
2198 operand = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002199
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002200 index_vector.push_back(operand);
2201 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002202
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002203 ArrayRef <Value*> indices(index_vector);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002204
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002205 return GetElementPtrInst::Create(ptr, indices, "", llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
2206 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002207
Sean Callanan0e016fe2013-07-15 23:31:47 +00002208 if (!UnfoldConstant(constant_expr, get_element_pointer_maker, entry_instruction_finder))
2209 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00002210 }
2211 break;
2212 }
2213 }
2214 else
2215 {
2216 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002217 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00002218 return false;
2219 }
2220 }
2221 else
2222 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002223 if (Instruction *inst = llvm::dyn_cast<Instruction>(user))
2224 {
2225 inst->replaceUsesOfWith(old_constant, value_maker.GetValue(inst->getParent()->getParent()));
2226 }
2227 else
2228 {
2229 if (log)
2230 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(user).c_str());
2231 return false;
2232 }
Sean Callanan7618f4e2010-07-14 23:40:29 +00002233 }
2234 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002235
Sean Callanan0e016fe2013-07-15 23:31:47 +00002236 if (!isa<GlobalValue>(old_constant))
2237 {
2238 old_constant->destroyConstant();
2239 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002240
Sean Callanan7618f4e2010-07-14 23:40:29 +00002241 return true;
2242}
2243
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002244bool
Sean Callanan79763a42011-05-23 21:40:23 +00002245IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan549c9f72010-07-13 21:41:46 +00002246{
Sean Callanan9e6ed532010-09-13 21:34:21 +00002247 if (!m_resolve_vars)
2248 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002249
Greg Clayton5160ce52013-03-27 23:08:40 +00002250 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan549c9f72010-07-13 21:41:46 +00002251
2252 m_decl_map->DoStructLayout();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002253
Sean Callanan549c9f72010-07-13 21:41:46 +00002254 if (log)
2255 log->Printf("Element arrangement:");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002256
Sean Callanan549c9f72010-07-13 21:41:46 +00002257 uint32_t num_elements;
2258 uint32_t element_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002259
Sean Callanan549c9f72010-07-13 21:41:46 +00002260 size_t size;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002261 lldb::offset_t alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002262
Sean Callanan549c9f72010-07-13 21:41:46 +00002263 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
2264 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002265
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002266 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002267
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002268 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002269 {
2270 if (m_error_stream)
2271 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 +00002272
Sean Callanan549c9f72010-07-13 21:41:46 +00002273 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002274 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002275
Sean Callanan7ea35012010-07-27 21:39:39 +00002276 Argument *argument = iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002277
Sean Callananfc55f5d2010-09-21 00:44:12 +00002278 if (argument->getName().equals("this"))
2279 {
2280 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002281
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002282 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002283 {
2284 if (m_error_stream)
2285 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 +00002286
Sean Callananfc55f5d2010-09-21 00:44:12 +00002287 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002288 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002289
Sean Callananfc55f5d2010-09-21 00:44:12 +00002290 argument = iter;
2291 }
Sean Callanan17827832010-12-13 22:46:15 +00002292 else if (argument->getName().equals("self"))
2293 {
2294 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002295
Sean Callanan17827832010-12-13 22:46:15 +00002296 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002297 {
2298 if (m_error_stream)
2299 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 +00002300
Sean Callanan17827832010-12-13 22:46:15 +00002301 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002302 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002303
Sean Callanan17827832010-12-13 22:46:15 +00002304 if (!iter->getName().equals("_cmd"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002305 {
2306 if (m_error_stream)
2307 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 +00002308
Sean Callanan17827832010-12-13 22:46:15 +00002309 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002310 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002311
Sean Callanan17827832010-12-13 22:46:15 +00002312 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002313
Sean Callanan17827832010-12-13 22:46:15 +00002314 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00002315 {
2316 if (m_error_stream)
2317 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 +00002318
Sean Callanan17827832010-12-13 22:46:15 +00002319 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002320 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002321
Sean Callanan17827832010-12-13 22:46:15 +00002322 argument = iter;
2323 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002324
Greg Clayton7b462cc2010-10-15 22:48:33 +00002325 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan3989fb92011-01-27 01:07:04 +00002326 {
2327 if (m_error_stream)
2328 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 +00002329
Sean Callanan549c9f72010-07-13 21:41:46 +00002330 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002331 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002332
Sean Callanan549c9f72010-07-13 21:41:46 +00002333 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002334 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002335
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002336 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callananafe16a72010-11-17 23:00:36 +00002337 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002338
Sean Callananafe16a72010-11-17 23:00:36 +00002339 if (!FirstEntryInstruction)
Sean Callanan3989fb92011-01-27 01:07:04 +00002340 {
2341 if (m_error_stream)
2342 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 +00002343
Sean Callanan549c9f72010-07-13 21:41:46 +00002344 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002345 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002346
Sean Callanan79763a42011-05-23 21:40:23 +00002347 LLVMContext &context(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +00002348 IntegerType *offset_type(Type::getInt32Ty(context));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002349
Sean Callanan549c9f72010-07-13 21:41:46 +00002350 if (!offset_type)
Sean Callanan3989fb92011-01-27 01:07:04 +00002351 {
2352 if (m_error_stream)
2353 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002354
Sean Callanan549c9f72010-07-13 21:41:46 +00002355 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002356 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002357
Sean Callanan549c9f72010-07-13 21:41:46 +00002358 for (element_index = 0; element_index < num_elements; ++element_index)
2359 {
Sean Callanan77eaf442011-07-08 00:39:14 +00002360 const clang::NamedDecl *decl = NULL;
2361 Value *value = NULL;
Zachary Turnera746e8e2014-07-02 17:24:07 +00002362 lldb::offset_t offset;
Greg Clayton7b462cc2010-10-15 22:48:33 +00002363 lldb_private::ConstString name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002364
Sean Callanan823bb4c2010-08-30 22:17:16 +00002365 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan3989fb92011-01-27 01:07:04 +00002366 {
2367 if (m_error_stream)
2368 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002369
Sean Callanan549c9f72010-07-13 21:41:46 +00002370 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00002371 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002372
Sean Callanan549c9f72010-07-13 21:41:46 +00002373 if (log)
Zachary Turnera746e8e2014-07-02 17:24:07 +00002374 log->Printf(" \"%s\" (\"%s\") placed at %" PRIu64,
Greg Clayton7b462cc2010-10-15 22:48:33 +00002375 name.GetCString(),
Sean Callananc70ed462011-10-25 18:36:40 +00002376 decl->getNameAsString().c_str(),
Sean Callanan549c9f72010-07-13 21:41:46 +00002377 offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002378
Sean Callananc70ed462011-10-25 18:36:40 +00002379 if (value)
Sean Callanan92adcac2011-01-13 08:53:35 +00002380 {
Sean Callananc70ed462011-10-25 18:36:40 +00002381 if (log)
2382 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002383
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002384 FunctionValueCache body_result_maker ([this, name, offset_type, offset, argument, value] (llvm::Function *function)->llvm::Value * {
2385 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
2386 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
2387 // entry in order to produce the static variable that the AST thinks it is accessing.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002388
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002389 llvm::Instruction *entry_instruction = llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002390
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002391 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
2392 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(argument,
2393 offset_int,
2394 "",
2395 entry_instruction);
2396
2397 if (name == m_result_name && !m_result_is_pointer)
2398 {
2399 BitCastInst *bit_cast = new BitCastInst(get_element_ptr,
2400 value->getType()->getPointerTo(),
2401 "",
2402 entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002403
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002404 LoadInst *load = new LoadInst(bit_cast, "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002405
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002406 return load;
2407 }
2408 else
2409 {
2410 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002411
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002412 return bit_cast;
2413 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002414 });
2415
Sean Callananc70ed462011-10-25 18:36:40 +00002416 if (Constant *constant = dyn_cast<Constant>(value))
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002417 {
2418 UnfoldConstant(constant, body_result_maker, m_entry_instruction_finder);
2419 }
2420 else if (Instruction *instruction = dyn_cast<Instruction>(value))
2421 {
2422 value->replaceAllUsesWith(body_result_maker.GetValue(instruction->getParent()->getParent()));
2423 }
Sean Callananc70ed462011-10-25 18:36:40 +00002424 else
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002425 {
2426 if (log)
2427 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(value).c_str());
2428 return false;
2429 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002430
Sean Callananc70ed462011-10-25 18:36:40 +00002431 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
2432 var->eraseFromParent();
2433 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002434 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002435
Sean Callanan549c9f72010-07-13 21:41:46 +00002436 if (log)
Greg Clayton6fea17e2014-03-03 19:15:20 +00002437 log->Printf("Total structure [align %" PRId64 ", size %" PRIu64 "]", (int64_t)alignment, (uint64_t)size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002438
Sean Callanan549c9f72010-07-13 21:41:46 +00002439 return true;
2440}
2441
Sean Callanan79763a42011-05-23 21:40:23 +00002442llvm::Constant *
Greg Clayton5160ce52013-03-27 23:08:40 +00002443IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
Sean Callanan79763a42011-05-23 21:40:23 +00002444{
Sean Callanan439dcae2013-12-20 19:55:02 +00002445 llvm::Constant *offset_int = ConstantInt::get(m_intptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002446
Sean Callanancc427fa2011-07-30 02:42:06 +00002447 llvm::Constant *offset_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002448
Sean Callanancc427fa2011-07-30 02:42:06 +00002449 offset_array[0] = offset_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002450
Sean Callanancc427fa2011-07-30 02:42:06 +00002451 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002452
Sean Callanancc427fa2011-07-30 02:42:06 +00002453 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(m_reloc_placeholder, offsets);
Sean Callanan79763a42011-05-23 21:40:23 +00002454 llvm::Constant *reloc_getbitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002455
Sean Callanan79763a42011-05-23 21:40:23 +00002456 return reloc_getbitcast;
2457}
2458
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002459bool
Sean Callanan79763a42011-05-23 21:40:23 +00002460IRForTarget::CompleteDataAllocation ()
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002461{
Greg Clayton5160ce52013-03-27 23:08:40 +00002462 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan79763a42011-05-23 21:40:23 +00002463
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002464 if (!m_data_allocator.GetStream().GetSize())
Sean Callanan79763a42011-05-23 21:40:23 +00002465 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002466
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002467 lldb::addr_t allocation = m_data_allocator.Allocate();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002468
Sean Callanan79763a42011-05-23 21:40:23 +00002469 if (log)
2470 {
2471 if (allocation)
2472 log->Printf("Allocated static data at 0x%llx", (unsigned long long)allocation);
2473 else
2474 log->Printf("Failed to allocate static data");
2475 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002476
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002477 if (!allocation || allocation == LLDB_INVALID_ADDRESS)
Sean Callanan79763a42011-05-23 21:40:23 +00002478 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002479
Sean Callanan439dcae2013-12-20 19:55:02 +00002480 Constant *relocated_addr = ConstantInt::get(m_intptr_ty, (uint64_t)allocation);
Sean Callanan79763a42011-05-23 21:40:23 +00002481 Constant *relocated_bitcast = ConstantExpr::getIntToPtr(relocated_addr, llvm::Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002482
Sean Callanan79763a42011-05-23 21:40:23 +00002483 m_reloc_placeholder->replaceAllUsesWith(relocated_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002484
Sean Callanan79763a42011-05-23 21:40:23 +00002485 m_reloc_placeholder->eraseFromParent();
2486
2487 return true;
2488}
2489
Sean Callanan2ab712f22010-07-03 01:35:46 +00002490bool
Sean Callanan3d654b32012-09-24 22:25:51 +00002491IRForTarget::StripAllGVs (Module &llvm_module)
2492{
Greg Clayton5160ce52013-03-27 23:08:40 +00002493 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan3d654b32012-09-24 22:25:51 +00002494 std::vector<GlobalVariable *> global_vars;
2495 std::set<GlobalVariable *>erased_vars;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002496
Sean Callanan3d654b32012-09-24 22:25:51 +00002497 bool erased = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002498
Sean Callanan3d654b32012-09-24 22:25:51 +00002499 while (erased)
2500 {
2501 erased = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002502
Sean Callanan339f6152014-03-11 19:19:16 +00002503 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002504 {
Sean Callanan339f6152014-03-11 19:19:16 +00002505 global_var.removeDeadConstantUsers();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002506
Sean Callanan339f6152014-03-11 19:19:16 +00002507 if (global_var.use_empty())
Sean Callanan3d654b32012-09-24 22:25:51 +00002508 {
2509 if (log)
2510 log->Printf("Did remove %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002511 PrintValue(&global_var).c_str());
2512 global_var.eraseFromParent();
Sean Callanan3d654b32012-09-24 22:25:51 +00002513 erased = true;
2514 break;
2515 }
2516 }
2517 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002518
Sean Callanan339f6152014-03-11 19:19:16 +00002519 for (GlobalVariable &global_var : llvm_module.globals())
Sean Callanan3d654b32012-09-24 22:25:51 +00002520 {
Sean Callanan339f6152014-03-11 19:19:16 +00002521 GlobalValue::user_iterator ui = global_var.user_begin();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002522
Jim Inghamd77557d2012-11-26 19:54:04 +00002523 if (log)
2524 log->Printf("Couldn't remove %s because of %s",
Sean Callanan339f6152014-03-11 19:19:16 +00002525 PrintValue(&global_var).c_str(),
Jim Inghamd77557d2012-11-26 19:54:04 +00002526 PrintValue(*ui).c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +00002527 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002528
Sean Callanan3d654b32012-09-24 22:25:51 +00002529 return true;
2530}
2531
2532bool
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002533IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002534{
Greg Clayton5160ce52013-03-27 23:08:40 +00002535 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002536
Sean Callanan79763a42011-05-23 21:40:23 +00002537 m_module = &llvm_module;
Micah Villmow8468dbe2012-10-08 16:28:57 +00002538 m_target_data.reset(new DataLayout(m_module));
Sean Callanan439dcae2013-12-20 19:55:02 +00002539 m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), m_target_data->getPointerSizeInBits());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002540
Sean Callananc70ed462011-10-25 18:36:40 +00002541 if (log)
2542 {
2543 std::string s;
2544 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002545
Sean Callananc70ed462011-10-25 18:36:40 +00002546 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002547
Sean Callananc70ed462011-10-25 18:36:40 +00002548 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002549
Sean Callananc70ed462011-10-25 18:36:40 +00002550 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
2551 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002552
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002553 Function* main_function = m_module->getFunction(StringRef(m_func_name.c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002554
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002555 if (!main_function)
2556 {
2557 if (log)
2558 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002559
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002560 if (m_error_stream)
2561 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module", m_func_name.c_str());
2562
2563 return false;
2564 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002565
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002566 if (!FixFunctionLinkage (*main_function))
2567 {
2568 if (log)
2569 log->Printf("Couldn't fix the linkage for the function");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002570
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002571 return false;
2572 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002573
Sean Callanan439dcae2013-12-20 19:55:02 +00002574 llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002575
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002576 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
Sean Callanan439dcae2013-12-20 19:55:02 +00002577 int8_ty,
Sean Callanan3d654b32012-09-24 22:25:51 +00002578 false /* IsConstant */,
Sean Callanan79763a42011-05-23 21:40:23 +00002579 GlobalVariable::InternalLinkage,
Sean Callanan439dcae2013-12-20 19:55:02 +00002580 Constant::getNullValue(int8_ty),
Sean Callanan79763a42011-05-23 21:40:23 +00002581 "reloc_placeholder",
2582 NULL /* InsertBefore */,
Sean Callanan3d654b32012-09-24 22:25:51 +00002583 GlobalVariable::NotThreadLocal /* ThreadLocal */,
Sean Callanan79763a42011-05-23 21:40:23 +00002584 0 /* AddressSpace */);
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002585
Sean Callanand1e5b432010-08-12 01:56:52 +00002586 ////////////////////////////////////////////////////////////
Greg Clayton7b462cc2010-10-15 22:48:33 +00002587 // Replace $__lldb_expr_result with a persistent variable
Sean Callanand1e5b432010-08-12 01:56:52 +00002588 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002589
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002590 if (!CreateResultVariable(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002591 {
2592 if (log)
2593 log->Printf("CreateResultVariable() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002594
Sean Callanan3989fb92011-01-27 01:07:04 +00002595 // CreateResultVariable() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002596
Sean Callanand1e5b432010-08-12 01:56:52 +00002597 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002598 }
Sean Callanan6e6d4a62012-07-21 02:02:15 +00002599
Sean Callananea685ae2011-11-01 17:33:54 +00002600 if (log && log->GetVerbose())
Sean Callanan79763a42011-05-23 21:40:23 +00002601 {
2602 std::string s;
2603 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002604
Sean Callanan79763a42011-05-23 21:40:23 +00002605 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002606
Sean Callanan79763a42011-05-23 21:40:23 +00002607 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002608
Sean Callanan79763a42011-05-23 21:40:23 +00002609 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2610 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002611
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002612 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2613 fi != fe;
2614 ++fi)
2615 {
2616 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002617
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002618 if (function->begin() == function->end())
2619 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002620
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002621 Function::iterator bbi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002622
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002623 for (bbi = function->begin();
2624 bbi != function->end();
2625 ++bbi)
2626 {
2627 if (!RemoveGuards(*bbi))
2628 {
2629 if (log)
2630 log->Printf("RemoveGuards() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002631
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002632 // RemoveGuards() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002633
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002634 return false;
2635 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002636
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002637 if (!RewritePersistentAllocs(*bbi))
2638 {
2639 if (log)
2640 log->Printf("RewritePersistentAllocs() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002641
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002642 // RewritePersistentAllocs() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002643
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002644 return false;
2645 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002646
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002647 if (!RemoveCXAAtExit(*bbi))
2648 {
2649 if (log)
2650 log->Printf("RemoveCXAAtExit() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002651
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002652 // RemoveCXAAtExit() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002653
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002654 return false;
2655 }
2656 }
2657 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002658
Sean Callananafe16a72010-11-17 23:00:36 +00002659 ///////////////////////////////////////////////////////////////////////////////
2660 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2661 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002662
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002663 if (!RewriteObjCConstStrings())
Sean Callanan17827832010-12-13 22:46:15 +00002664 {
2665 if (log)
2666 log->Printf("RewriteObjCConstStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002667
Sean Callanan3989fb92011-01-27 01:07:04 +00002668 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002669
Sean Callananafe16a72010-11-17 23:00:36 +00002670 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002671 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002672
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002673 ///////////////////////////////
2674 // Resolve function pointers
2675 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002676
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002677 if (!ResolveFunctionPointers(llvm_module))
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002678 {
2679 if (log)
2680 log->Printf("ResolveFunctionPointers() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002681
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002682 // ResolveFunctionPointers() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002683
Sean Callanan0c4d8d22011-08-04 21:37:47 +00002684 return false;
2685 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002686
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002687 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2688 fi != fe;
2689 ++fi)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002690 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002691 llvm::Function *function = fi;
2692
2693 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2694 bbi != bbe;
2695 ++bbi)
Sean Callanan17827832010-12-13 22:46:15 +00002696 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002697 if (!RewriteObjCSelectors(*bbi))
2698 {
2699 if (log)
2700 log->Printf("RewriteObjCSelectors() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002701
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002702 // RewriteObjCSelectors() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002703
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002704 return false;
2705 }
Sean Callanan17827832010-12-13 22:46:15 +00002706 }
Sean Callananbad134f2012-07-27 19:25:24 +00002707 }
Sean Callanan2235f322010-08-11 03:57:18 +00002708
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002709 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2710 fi != fe;
2711 ++fi)
Sean Callananbad134f2012-07-27 19:25:24 +00002712 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002713 llvm::Function *function = fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002714
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002715 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2716 bbi != bbe;
2717 ++bbi)
Sean Callanan79763a42011-05-23 21:40:23 +00002718 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002719 if (!ResolveCalls(*bbi))
2720 {
2721 if (log)
2722 log->Printf("ResolveCalls() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002723
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002724 // ResolveCalls() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002725
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002726 return false;
2727 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002728
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002729 if (!ReplaceStaticLiterals(*bbi))
2730 {
2731 if (log)
2732 log->Printf("ReplaceStaticLiterals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002733
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002734 return false;
2735 }
Sean Callanan79763a42011-05-23 21:40:23 +00002736 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002737 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002738
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002739 ////////////////////////////////////////////////////////////////////////
2740 // Run function-level passes that only make sense on the main function
Sean Callanan038df5032010-09-30 21:18:25 +00002741 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002742
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002743 if (!ResolveExternals(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002744 {
2745 if (log)
2746 log->Printf("ResolveExternals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002747
Sean Callanan3989fb92011-01-27 01:07:04 +00002748 // ResolveExternals() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002749
Sean Callanana4e55172010-11-08 00:31:32 +00002750 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002751 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002752
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002753 if (!ReplaceVariables(*main_function))
Sean Callanan17827832010-12-13 22:46:15 +00002754 {
2755 if (log)
2756 log->Printf("ReplaceVariables() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002757
Sean Callanan3989fb92011-01-27 01:07:04 +00002758 // ReplaceVariables() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002759
Sean Callanan038df5032010-09-30 21:18:25 +00002760 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002761 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002762
Sean Callanan79763a42011-05-23 21:40:23 +00002763 if (!ReplaceStrings())
2764 {
2765 if (log)
2766 log->Printf("ReplaceStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002767
Sean Callanan79763a42011-05-23 21:40:23 +00002768 return false;
2769 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002770
Sean Callanan79763a42011-05-23 21:40:23 +00002771 if (!CompleteDataAllocation())
2772 {
2773 if (log)
2774 log->Printf("CompleteDataAllocation() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002775
Sean Callanan79763a42011-05-23 21:40:23 +00002776 return false;
2777 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002778
Sean Callanan3d654b32012-09-24 22:25:51 +00002779 if (!StripAllGVs(llvm_module))
2780 {
2781 if (log)
2782 log->Printf("StripAllGVs() failed");
2783 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002784
Sean Callananea685ae2011-11-01 17:33:54 +00002785 if (log && log->GetVerbose())
Sean Callanan549c9f72010-07-13 21:41:46 +00002786 {
Sean Callanancc54bd32010-07-28 01:00:59 +00002787 std::string s;
2788 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002789
Sean Callanan79763a42011-05-23 21:40:23 +00002790 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002791
Sean Callanancc54bd32010-07-28 01:00:59 +00002792 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002793
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002794 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan2ab712f22010-07-03 01:35:46 +00002795 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002796
2797 return true;
Sean Callanan2ab712f22010-07-03 01:35:46 +00002798}
2799
2800void
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002801IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002802{
2803}
2804
2805PassManagerType
2806IRForTarget::getPotentialPassManagerType() const
2807{
2808 return PMT_ModulePassManager;
2809}