blob: 62aa5b7fb886651b0ac0ed673b03104f89c8f162 [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
Sean Callanan4dbb2712015-09-25 20:35:58 +000010#include "IRForTarget.h"
11
12#include "ClangExpressionDeclMap.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000013
14#include "llvm/Support/raw_ostream.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/InstrTypes.h"
18#include "llvm/IR/Instructions.h"
19#include "llvm/IR/Intrinsics.h"
20#include "llvm/IR/Module.h"
Ilia Kc9a475d2015-02-13 10:49:18 +000021#include "llvm/IR/LegacyPassManager.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000022#include "llvm/Transforms/IPO.h"
Zachary Turner543afa12014-12-09 22:29:47 +000023#include "llvm/IR/Metadata.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000024#include "llvm/IR/ValueSymbolTable.h"
Sean Callanan549c9f72010-07-13 21:41:46 +000025
26#include "clang/AST/ASTContext.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000027
Sean Callanan8dfb68e2013-03-19 00:10:07 +000028#include "lldb/Core/ConstString.h"
29#include "lldb/Core/DataBufferHeap.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000030#include "lldb/Core/Log.h"
31#include "lldb/Core/Scalar.h"
32#include "lldb/Core/StreamString.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000033#include "lldb/Core/dwarf.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000034#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan3bfdaa22011-09-15 02:13:07 +000035#include "lldb/Expression/IRInterpreter.h"
Sean Callanan79763a42011-05-23 21:40:23 +000036#include "lldb/Host/Endian.h"
Sean Callanan63697e52011-05-07 01:06:41 +000037#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000038#include "lldb/Symbol/ClangUtil.h"
Greg Claytona1e5dc82015-08-11 22:53:00 +000039#include "lldb/Symbol/CompilerType.h"
Sean Callanan2ab712f22010-07-03 01:35:46 +000040
41#include <map>
42
43using namespace llvm;
44
Sean Callananeaacbc92010-08-18 18:50:51 +000045static char ID;
46
Sean Callanan1f9db3e2013-06-28 21:44:15 +000047IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker) :
48 m_maker(maker),
49 m_values()
50{
51}
52
53IRForTarget::FunctionValueCache::~FunctionValueCache()
54{
55}
56
Greg Clayton526ae042015-02-12 00:34:25 +000057llvm::Value *
58IRForTarget::FunctionValueCache::GetValue(llvm::Function *function)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000059{
Sean Callanan1f9db3e2013-06-28 21:44:15 +000060 if (!m_values.count(function))
61 {
62 llvm::Value *ret = m_maker(function);
63 m_values[function] = ret;
64 return ret;
65 }
66 return m_values[function];
67}
68
Greg Clayton526ae042015-02-12 00:34:25 +000069static llvm::Value *
70FindEntryInstruction (llvm::Function *function)
Sean Callanan1f9db3e2013-06-28 21:44:15 +000071{
72 if (function->empty())
73 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000074
Sean Callanan1f9db3e2013-06-28 21:44:15 +000075 return function->getEntryBlock().getFirstNonPHIOrDbg();
76}
77
Greg Clayton1b95a6f2010-11-19 01:05:25 +000078IRForTarget::IRForTarget (lldb_private::ClangExpressionDeclMap *decl_map,
79 bool resolve_vars,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000080 lldb_private::IRExecutionUnit &execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +000081 lldb_private::Stream *error_stream,
Greg Clayton1b95a6f2010-11-19 01:05:25 +000082 const char *func_name) :
Sean Callanane2ef6e32010-09-23 03:01:22 +000083 ModulePass(ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +000084 m_resolve_vars(resolve_vars),
85 m_func_name(func_name),
Sean Callanan79763a42011-05-23 21:40:23 +000086 m_module(NULL),
Johnny Chen44805302011-07-19 19:48:13 +000087 m_decl_map(decl_map),
Sean Callananafe16a72010-11-17 23:00:36 +000088 m_CFStringCreateWithBytes(NULL),
Sean Callanan1a8d4092010-08-27 01:01:44 +000089 m_sel_registerName(NULL),
Sean Callanan439dcae2013-12-20 19:55:02 +000090 m_intptr_ty(NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000091 m_error_stream(error_stream),
Saleem Abdulrasool2f1d3ac2016-02-15 03:23:14 +000092 m_execution_unit(execution_unit),
Sean Callanan63697e52011-05-07 01:06:41 +000093 m_result_store(NULL),
94 m_result_is_pointer(false),
Sean Callanan1f9db3e2013-06-28 21:44:15 +000095 m_reloc_placeholder(NULL),
96 m_entry_instruction_finder (FindEntryInstruction)
Sean Callanan2ab712f22010-07-03 01:35:46 +000097{
98}
99
Sean Callanan038df5032010-09-30 21:18:25 +0000100/* Handy utility functions used at several places in the code */
Sean Callanan2235f322010-08-11 03:57:18 +0000101
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000102static std::string
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000103PrintValue(const Value *value, bool truncate = false)
Sean Callanan2235f322010-08-11 03:57:18 +0000104{
105 std::string s;
Jim Ingham28eb5712012-10-12 17:34:26 +0000106 if (value)
107 {
108 raw_string_ostream rso(s);
109 value->print(rso);
110 rso.flush();
111 if (truncate)
112 s.resize(s.length() - 1);
113 }
Sean Callanan2235f322010-08-11 03:57:18 +0000114 return s;
115}
116
Sean Callanan038df5032010-09-30 21:18:25 +0000117static std::string
Greg Clayton57ee3062013-07-11 22:46:58 +0000118PrintType(const llvm::Type *type, bool truncate = false)
Sean Callanan038df5032010-09-30 21:18:25 +0000119{
120 std::string s;
121 raw_string_ostream rso(s);
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000122 type->print(rso);
Sean Callanan038df5032010-09-30 21:18:25 +0000123 rso.flush();
124 if (truncate)
125 s.resize(s.length() - 1);
126 return s;
127}
128
Sean Callanan2ab712f22010-07-03 01:35:46 +0000129IRForTarget::~IRForTarget()
130{
131}
132
Sean Callanan79763a42011-05-23 21:40:23 +0000133bool
134IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function)
135{
Sean Callanan79763a42011-05-23 21:40:23 +0000136 llvm_function.setLinkage(GlobalValue::ExternalLinkage);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000137
Sean Callanan79763a42011-05-23 21:40:23 +0000138 return true;
139}
140
Sean Callanan63697e52011-05-07 01:06:41 +0000141clang::NamedDecl *
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000142IRForTarget::DeclForGlobal (const GlobalValue *global_val, Module *module)
Sean Callanan63697e52011-05-07 01:06:41 +0000143{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000144 NamedMDNode *named_metadata = module->getNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000145
Sean Callanan63697e52011-05-07 01:06:41 +0000146 if (!named_metadata)
147 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000148
Sean Callanan63697e52011-05-07 01:06:41 +0000149 unsigned num_nodes = named_metadata->getNumOperands();
150 unsigned node_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000151
Sean Callanan63697e52011-05-07 01:06:41 +0000152 for (node_index = 0;
153 node_index < num_nodes;
154 ++node_index)
155 {
Zachary Turner0d594e12014-11-05 18:37:53 +0000156 llvm::MDNode *metadata_node = dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
Sean Callanan63697e52011-05-07 01:06:41 +0000157 if (!metadata_node)
158 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000159
Sean Callanan63697e52011-05-07 01:06:41 +0000160 if (metadata_node->getNumOperands() != 2)
161 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162
Zachary Turner543afa12014-12-09 22:29:47 +0000163 if (mdconst::dyn_extract_or_null<GlobalValue>(metadata_node->getOperand(0)) != global_val)
Sean Callanan63697e52011-05-07 01:06:41 +0000164 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000165
Zachary Turner543afa12014-12-09 22:29:47 +0000166 ConstantInt *constant_int = mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000167
Sean Callanan63697e52011-05-07 01:06:41 +0000168 if (!constant_int)
169 return NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000170
Sean Callanan63697e52011-05-07 01:06:41 +0000171 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000172
Sean Callanan63697e52011-05-07 01:06:41 +0000173 return reinterpret_cast<clang::NamedDecl *>(ptr);
174 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175
Sean Callanan63697e52011-05-07 01:06:41 +0000176 return NULL;
177}
178
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000179clang::NamedDecl *
180IRForTarget::DeclForGlobal (GlobalValue *global_val)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000181{
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000182 return DeclForGlobal(global_val, m_module);
183}
184
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000185bool
Sean Callanan79763a42011-05-23 21:40:23 +0000186IRForTarget::CreateResultVariable (llvm::Function &llvm_function)
Sean Callanand1e5b432010-08-12 01:56:52 +0000187{
Greg Clayton5160ce52013-03-27 23:08:40 +0000188 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000189
Sean Callanan9e6ed532010-09-13 21:34:21 +0000190 if (!m_resolve_vars)
191 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000192
Sean Callanan9e6ed532010-09-13 21:34:21 +0000193 // Find the result variable. If it doesn't exist, we can give up right here.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000194
Sean Callanan79763a42011-05-23 21:40:23 +0000195 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000196
Sean Callanancc427fa2011-07-30 02:42:06 +0000197 std::string result_name_str;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000198 const char *result_name = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000199
Sean Callananfc55f5d2010-09-21 00:44:12 +0000200 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
201 vi != ve;
202 ++vi)
203 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000204 result_name_str = vi->first().str();
205 const char *value_name = result_name_str.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000206
Sean Callanancc427fa2011-07-30 02:42:06 +0000207 if (strstr(value_name, "$__lldb_expr_result_ptr") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000208 strncmp(value_name, "_ZGV", 4))
Sean Callanan92adcac2011-01-13 08:53:35 +0000209 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000210 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000211 m_result_is_pointer = true;
212 break;
213 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000214
Sean Callanancc427fa2011-07-30 02:42:06 +0000215 if (strstr(value_name, "$__lldb_expr_result") &&
Sean Callanan7273e2d2012-11-02 22:28:08 +0000216 strncmp(value_name, "_ZGV", 4))
Sean Callanan46ae9e52010-09-28 21:13:03 +0000217 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000218 result_name = value_name;
Sean Callanan92adcac2011-01-13 08:53:35 +0000219 m_result_is_pointer = false;
Sean Callanan46ae9e52010-09-28 21:13:03 +0000220 break;
221 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000222 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000223
Sean Callananfc55f5d2010-09-21 00:44:12 +0000224 if (!result_name)
225 {
226 if (log)
227 log->PutCString("Couldn't find result variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000228
Richard Mitton00dec202013-10-11 19:44:23 +0000229 return true;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000230 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000231
Sean Callanan46ae9e52010-09-28 21:13:03 +0000232 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000233 log->Printf("Result name: \"%s\"", result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Sean Callanan79763a42011-05-23 21:40:23 +0000235 Value *result_value = m_module->getNamedValue(result_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000236
Sean Callanand1e5b432010-08-12 01:56:52 +0000237 if (!result_value)
238 {
239 if (log)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000240 log->PutCString("Result variable had no data");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000241
Sean Callanan3989fb92011-01-27 01:07:04 +0000242 if (m_error_stream)
243 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 +0000244
Sean Callananfc55f5d2010-09-21 00:44:12 +0000245 return false;
Sean Callanand1e5b432010-08-12 01:56:52 +0000246 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000247
Sean Callanand1e5b432010-08-12 01:56:52 +0000248 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000249 log->Printf("Found result in the IR: \"%s\"", PrintValue(result_value, false).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000250
Sean Callanand1e5b432010-08-12 01:56:52 +0000251 GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000252
Sean Callanand1e5b432010-08-12 01:56:52 +0000253 if (!result_global)
254 {
255 if (log)
256 log->PutCString("Result variable isn't a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000257
Sean Callanan3989fb92011-01-27 01:07:04 +0000258 if (m_error_stream)
259 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 +0000260
Sean Callanand1e5b432010-08-12 01:56:52 +0000261 return false;
262 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Sean Callanan79763a42011-05-23 21:40:23 +0000264 clang::NamedDecl *result_decl = DeclForGlobal (result_global);
Sean Callanan63697e52011-05-07 01:06:41 +0000265 if (!result_decl)
Sean Callanand1e5b432010-08-12 01:56:52 +0000266 {
267 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000268 log->PutCString("Result variable doesn't have a corresponding Decl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000269
Sean Callanan3989fb92011-01-27 01:07:04 +0000270 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000271 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 +0000272
Sean Callanand1e5b432010-08-12 01:56:52 +0000273 return false;
274 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan63697e52011-05-07 01:06:41 +0000276 if (log)
Sean Callanand1e5b432010-08-12 01:56:52 +0000277 {
Sean Callanan63697e52011-05-07 01:06:41 +0000278 std::string decl_desc_str;
279 raw_string_ostream decl_desc_stream(decl_desc_str);
280 result_decl->print(decl_desc_stream);
281 decl_desc_stream.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000282
Sean Callanan63697e52011-05-07 01:06:41 +0000283 log->Printf("Found result decl: \"%s\"", decl_desc_str.c_str());
Sean Callanand1e5b432010-08-12 01:56:52 +0000284 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000285
Sean Callanan63697e52011-05-07 01:06:41 +0000286 clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
287 if (!result_var)
Sean Callanand1e5b432010-08-12 01:56:52 +0000288 {
289 if (log)
Sean Callanan63697e52011-05-07 01:06:41 +0000290 log->PutCString("Result variable Decl isn't a VarDecl");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000291
Sean Callanan3989fb92011-01-27 01:07:04 +0000292 if (m_error_stream)
Sean Callanan63697e52011-05-07 01:06:41 +0000293 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 +0000294
Sean Callanand1e5b432010-08-12 01:56:52 +0000295 return false;
296 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297
Sean Callanand1e5b432010-08-12 01:56:52 +0000298 // Get the next available result name from m_decl_map and create the persistent
299 // variable for it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000300
Sean Callanan63697e52011-05-07 01:06:41 +0000301 // If the result is an Lvalue, it is emitted as a pointer; see
302 // ASTResultSynthesizer::SynthesizeBodyResult.
Sean Callanan92adcac2011-01-13 08:53:35 +0000303 if (m_result_is_pointer)
304 {
Sean Callanan63697e52011-05-07 01:06:41 +0000305 clang::QualType pointer_qual_type = result_var->getType();
Sean Callanan78e37602011-01-27 04:42:51 +0000306 const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000307
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000308 const clang::PointerType *pointer_pointertype = pointer_type->getAs<clang::PointerType>();
309 const clang::ObjCObjectPointerType *pointer_objcobjpointertype = pointer_type->getAs<clang::ObjCObjectPointerType>();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000310
Sean Callanan5780f9d2011-12-08 19:04:34 +0000311 if (pointer_pointertype)
312 {
313 clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000314
Sean Callanan5780f9d2011-12-08 19:04:34 +0000315 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000316 lldb_private::ClangASTContext::GetASTContext(&result_decl->getASTContext()));
Sean Callanan5780f9d2011-12-08 19:04:34 +0000317 }
318 else if (pointer_objcobjpointertype)
319 {
320 clang::QualType element_qual_type = clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000321
Sean Callanan5780f9d2011-12-08 19:04:34 +0000322 m_result_type = lldb_private::TypeFromParser(element_qual_type.getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000323 lldb_private::ClangASTContext::GetASTContext(&result_decl->getASTContext()));
Sean Callanan5780f9d2011-12-08 19:04:34 +0000324 }
325 else
Sean Callanan92adcac2011-01-13 08:53:35 +0000326 {
327 if (log)
328 log->PutCString("Expected result to have pointer type, but it did not");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329
Sean Callanan3989fb92011-01-27 01:07:04 +0000330 if (m_error_stream)
331 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 +0000332
Sean Callanan92adcac2011-01-13 08:53:35 +0000333 return false;
334 }
Sean Callanan92adcac2011-01-13 08:53:35 +0000335 }
336 else
337 {
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000338 m_result_type = lldb_private::TypeFromParser(result_var->getType().getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000339 lldb_private::ClangASTContext::GetASTContext(&result_decl->getASTContext()));
Sean Callanan00f43622011-11-18 03:28:09 +0000340 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000341
Greg Clayton526ae042015-02-12 00:34:25 +0000342
Sean Callanan2a8fa2a2016-02-13 00:01:46 +0000343 lldb::TargetSP target_sp (m_execution_unit.GetTarget());
Greg Claytonf9da9282015-02-27 00:12:22 +0000344 lldb_private::ExecutionContext exe_ctx (target_sp, true);
Greg Clayton526ae042015-02-12 00:34:25 +0000345 if (m_result_type.GetBitSize(exe_ctx.GetBestExecutionContextScope()) == 0)
Sean Callanan00f43622011-11-18 03:28:09 +0000346 {
347 lldb_private::StreamString type_desc_stream;
348 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Sean Callanan00f43622011-11-18 03:28:09 +0000350 if (log)
351 log->Printf("Result type has size 0");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000352
Sean Callanan00f43622011-11-18 03:28:09 +0000353 if (m_error_stream)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000354 m_error_stream->Printf("Error [IRForTarget]: Size of result type '%s' couldn't be determined\n",
Sean Callanan00f43622011-11-18 03:28:09 +0000355 type_desc_stream.GetData());
Sean Callanan960534c2011-12-21 23:44:05 +0000356 return false;
Sean Callanan92adcac2011-01-13 08:53:35 +0000357 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000358
Sean Callanan63697e52011-05-07 01:06:41 +0000359 if (log)
360 {
361 lldb_private::StreamString type_desc_stream;
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000362 m_result_type.DumpTypeDescription(&type_desc_stream);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000363
Sean Callanan00f43622011-11-18 03:28:09 +0000364 log->Printf("Result decl type: \"%s\"", type_desc_stream.GetData());
Sean Callanan63697e52011-05-07 01:06:41 +0000365 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000366
Sean Callanan1582ee62013-04-18 22:06:33 +0000367 m_result_name = lldb_private::ConstString("$RESULT_NAME");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000368
Sean Callanand1e5b432010-08-12 01:56:52 +0000369 if (log)
Greg Claytonfaac1112013-03-14 18:31:44 +0000370 log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
Sean Callanan00f43622011-11-18 03:28:09 +0000371 m_result_name.GetCString(),
Enrico Granata1cd5e922015-01-28 00:07:51 +0000372 m_result_type.GetByteSize(nullptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000373
Sean Callanand1e5b432010-08-12 01:56:52 +0000374 // Construct a new result global and set up its metadata
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000375
376 GlobalVariable *new_result_global = new GlobalVariable((*m_module),
Sean Callanand1e5b432010-08-12 01:56:52 +0000377 result_global->getType()->getElementType(),
378 false, /* not constant */
379 GlobalValue::ExternalLinkage,
380 NULL, /* no initializer */
Sean Callanan92adcac2011-01-13 08:53:35 +0000381 m_result_name.GetCString ());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000382
Sean Callanand1e5b432010-08-12 01:56:52 +0000383 // It's too late in compilation to create a new VarDecl for this, but we don't
384 // need to. We point the metadata at the old VarDecl. This creates an odd
385 // anomaly: a variable with a Value whose name is something like $0 and a
Greg Clayton7b462cc2010-10-15 22:48:33 +0000386 // Decl whose name is $__lldb_expr_result. This condition is handled in
Sean Callanand1e5b432010-08-12 01:56:52 +0000387 // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
388 // fixed up.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000389
Sean Callanan79763a42011-05-23 21:40:23 +0000390 ConstantInt *new_constant_int = ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
Sean Callanan63697e52011-05-07 01:06:41 +0000391 reinterpret_cast<uint64_t>(result_decl),
Sean Callanand1e5b432010-08-12 01:56:52 +0000392 false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000393
Zachary Turner543afa12014-12-09 22:29:47 +0000394 llvm::Metadata *values[2];
395 values[0] = ConstantAsMetadata::get(new_result_global);
396 values[1] = ConstantAsMetadata::get(new_constant_int);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000397
Zachary Turner543afa12014-12-09 22:29:47 +0000398 ArrayRef<Metadata *> value_ref(values, 2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000399
Sean Callanan79763a42011-05-23 21:40:23 +0000400 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
401 NamedMDNode *named_metadata = m_module->getNamedMetadata("clang.global.decl.ptrs");
Sean Callanand1e5b432010-08-12 01:56:52 +0000402 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000403
Sean Callanand1e5b432010-08-12 01:56:52 +0000404 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000405 log->Printf("Replacing \"%s\" with \"%s\"",
Sean Callanan1e87fff2010-09-07 22:43:19 +0000406 PrintValue(result_global).c_str(),
Sean Callanand1e5b432010-08-12 01:56:52 +0000407 PrintValue(new_result_global).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000408
Sean Callanan339f6152014-03-11 19:19:16 +0000409 if (result_global->use_empty())
Sean Callanan1e87fff2010-09-07 22:43:19 +0000410 {
411 // We need to synthesize a store for this variable, because otherwise
412 // there's nothing to put into its equivalent persistent variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000413
Greg Clayton7b462cc2010-10-15 22:48:33 +0000414 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000415 Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000416
Sean Callanan1e87fff2010-09-07 22:43:19 +0000417 if (!first_entry_instruction)
418 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000419
Sean Callanan1e87fff2010-09-07 22:43:19 +0000420 if (!result_global->hasInitializer())
421 {
422 if (log)
423 log->Printf("Couldn't find initializer for unused variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000424
Sean Callanan3989fb92011-01-27 01:07:04 +0000425 if (m_error_stream)
426 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 +0000427
Sean Callanan1e87fff2010-09-07 22:43:19 +0000428 return false;
429 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000430
Sean Callanan1e87fff2010-09-07 22:43:19 +0000431 Constant *initializer = result_global->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000432
Greg Clayton9c139312011-02-05 02:28:58 +0000433 StoreInst *synthesized_store = new StoreInst(initializer,
434 new_result_global,
435 first_entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000436
Sean Callanan1e87fff2010-09-07 22:43:19 +0000437 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000438 log->Printf("Synthesized result store \"%s\"\n", PrintValue(synthesized_store).c_str());
Sean Callanan1e87fff2010-09-07 22:43:19 +0000439 }
440 else
441 {
442 result_global->replaceAllUsesWith(new_result_global);
443 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000444
Sean Callanan1582ee62013-04-18 22:06:33 +0000445 if (!m_decl_map->AddPersistentVariable(result_decl,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000446 m_result_name,
Sean Callanan1582ee62013-04-18 22:06:33 +0000447 m_result_type,
448 true,
449 m_result_is_pointer))
450 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000451
Sean Callanand1e5b432010-08-12 01:56:52 +0000452 result_global->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000453
Sean Callanand1e5b432010-08-12 01:56:52 +0000454 return true;
455}
456
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000457bool
Sean Callanan79763a42011-05-23 21:40:23 +0000458IRForTarget::RewriteObjCConstString (llvm::GlobalVariable *ns_str,
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000459 llvm::GlobalVariable *cstr)
Sean Callananafe16a72010-11-17 23:00:36 +0000460{
Greg Clayton5160ce52013-03-27 23:08:40 +0000461 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000462
Sean Callanancc427fa2011-07-30 02:42:06 +0000463 Type *ns_str_ty = ns_str->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000464
Sean Callanancc427fa2011-07-30 02:42:06 +0000465 Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +0000466 Type *i32_ty = Type::getInt32Ty(m_module->getContext());
467 Type *i8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000468
Sean Callananafe16a72010-11-17 23:00:36 +0000469 if (!m_CFStringCreateWithBytes)
470 {
471 lldb::addr_t CFStringCreateWithBytes_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000472
Sean Callananafe16a72010-11-17 23:00:36 +0000473 static lldb_private::ConstString g_CFStringCreateWithBytes_str ("CFStringCreateWithBytes");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000474
Sean Callanan2a8fa2a2016-02-13 00:01:46 +0000475 CFStringCreateWithBytes_addr = m_execution_unit.FindSymbol (g_CFStringCreateWithBytes_str);
476 if (CFStringCreateWithBytes_addr == LLDB_INVALID_ADDRESS)
Sean Callananafe16a72010-11-17 23:00:36 +0000477 {
478 if (log)
479 log->PutCString("Couldn't find CFStringCreateWithBytes in the target");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000480
Sean Callanan3989fb92011-01-27 01:07:04 +0000481 if (m_error_stream)
482 m_error_stream->Printf("Error [IRForTarget]: Rewriting an Objective-C constant string requires CFStringCreateWithBytes\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000483
Sean Callananafe16a72010-11-17 23:00:36 +0000484 return false;
485 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000486
Sean Callananafe16a72010-11-17 23:00:36 +0000487 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000488 log->Printf("Found CFStringCreateWithBytes at 0x%" PRIx64, CFStringCreateWithBytes_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000489
Sean Callananafe16a72010-11-17 23:00:36 +0000490 // Build the function type:
491 //
492 // CFStringRef CFStringCreateWithBytes (
493 // CFAllocatorRef alloc,
494 // const UInt8 *bytes,
495 // CFIndex numBytes,
496 // CFStringEncoding encoding,
497 // Boolean isExternalRepresentation
498 // );
499 //
500 // We make the following substitutions:
501 //
502 // CFStringRef -> i8*
503 // CFAllocatorRef -> i8*
504 // UInt8 * -> i8*
505 // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its pointer size for now)
506 // CFStringEncoding -> i32
507 // Boolean -> i8
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000508
Sean Callanancc427fa2011-07-30 02:42:06 +0000509 Type *arg_type_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callanancc427fa2011-07-30 02:42:06 +0000511 arg_type_array[0] = i8_ptr_ty;
512 arg_type_array[1] = i8_ptr_ty;
Sean Callanan439dcae2013-12-20 19:55:02 +0000513 arg_type_array[2] = m_intptr_ty;
Sean Callanancc427fa2011-07-30 02:42:06 +0000514 arg_type_array[3] = i32_ty;
515 arg_type_array[4] = i8_ty;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000516
Sean Callanancc427fa2011-07-30 02:42:06 +0000517 ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000518
Sean Callanan79763a42011-05-23 21:40:23 +0000519 llvm::Type *CFSCWB_ty = FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000520
Sean Callananafe16a72010-11-17 23:00:36 +0000521 // Build the constant containing the pointer to the function
522 PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000523 Constant *CFSCWB_addr_int = ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000524 m_CFStringCreateWithBytes = ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty);
525 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000526
Sean Callanand2b465f2012-02-09 03:22:41 +0000527 ConstantDataSequential *string_array = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000528
Sean Callanan229ce2d2011-02-10 22:17:53 +0000529 if (cstr)
Sean Callanand2b465f2012-02-09 03:22:41 +0000530 string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000531
Sean Callananafe16a72010-11-17 23:00:36 +0000532 Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000533 Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty) : Constant::getNullValue(i8_ptr_ty);
Sean Callanan439dcae2013-12-20 19:55:02 +0000534 Constant *numBytes_arg = ConstantInt::get(m_intptr_ty, cstr ? string_array->getNumElements() - 1 : 0, false);
Sean Callananafe16a72010-11-17 23:00:36 +0000535 Constant *encoding_arg = ConstantInt::get(i32_ty, 0x0600, false); /* 0x0600 is kCFStringEncodingASCII */
536 Constant *isExternal_arg = ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000537
Sean Callanancc427fa2011-07-30 02:42:06 +0000538 Value *argument_array[5];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000539
Sean Callanancc427fa2011-07-30 02:42:06 +0000540 argument_array[0] = alloc_arg;
541 argument_array[1] = bytes_arg;
542 argument_array[2] = numBytes_arg;
543 argument_array[3] = encoding_arg;
544 argument_array[4] = isExternal_arg;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000545
Sean Callanancc427fa2011-07-30 02:42:06 +0000546 ArrayRef <Value *> CFSCWB_arguments(argument_array, 5);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000547
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000548 FunctionValueCache CFSCWB_Caller ([this, &CFSCWB_arguments] (llvm::Function *function)->llvm::Value * {
549 return CallInst::Create(m_CFStringCreateWithBytes,
550 CFSCWB_arguments,
551 "CFStringCreateWithBytes",
552 llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function)));
553 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000554
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000555 if (!UnfoldConstant(ns_str, CFSCWB_Caller, m_entry_instruction_finder))
Sean Callananafe16a72010-11-17 23:00:36 +0000556 {
557 if (log)
558 log->PutCString("Couldn't replace the NSString with the result of the call");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000559
Sean Callanan3989fb92011-01-27 01:07:04 +0000560 if (m_error_stream)
561 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 +0000562
Sean Callananafe16a72010-11-17 23:00:36 +0000563 return false;
564 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000565
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000566 ns_str->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000567
Sean Callananafe16a72010-11-17 23:00:36 +0000568 return true;
569}
570
571bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000572IRForTarget::RewriteObjCConstStrings()
Sean Callananafe16a72010-11-17 23:00:36 +0000573{
Greg Clayton5160ce52013-03-27 23:08:40 +0000574 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000575
Sean Callanan79763a42011-05-23 21:40:23 +0000576 ValueSymbolTable& value_symbol_table = m_module->getValueSymbolTable();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000577
Sean Callananafe16a72010-11-17 23:00:36 +0000578 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
579 vi != ve;
580 ++vi)
581 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000582 std::string value_name = vi->first().str();
583 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000584
Sean Callanancc427fa2011-07-30 02:42:06 +0000585 if (strstr(value_name_cstr, "_unnamed_cfstring_"))
Sean Callananafe16a72010-11-17 23:00:36 +0000586 {
587 Value *nsstring_value = vi->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000588
Sean Callananafe16a72010-11-17 23:00:36 +0000589 GlobalVariable *nsstring_global = dyn_cast<GlobalVariable>(nsstring_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000590
Sean Callananafe16a72010-11-17 23:00:36 +0000591 if (!nsstring_global)
592 {
593 if (log)
594 log->PutCString("NSString variable is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000595
Sean Callanan3989fb92011-01-27 01:07:04 +0000596 if (m_error_stream)
597 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 +0000598
Sean Callananafe16a72010-11-17 23:00:36 +0000599 return false;
600 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000601
Sean Callananafe16a72010-11-17 23:00:36 +0000602 if (!nsstring_global->hasInitializer())
603 {
604 if (log)
605 log->PutCString("NSString variable does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000606
Sean Callanan3989fb92011-01-27 01:07:04 +0000607 if (m_error_stream)
608 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 +0000609
Sean Callananafe16a72010-11-17 23:00:36 +0000610 return false;
611 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000612
Sean Callananafe16a72010-11-17 23:00:36 +0000613 ConstantStruct *nsstring_struct = dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000614
Sean Callananafe16a72010-11-17 23:00:36 +0000615 if (!nsstring_struct)
616 {
617 if (log)
618 log->PutCString("NSString variable's initializer is not a ConstantStruct");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000619
Sean Callanan3989fb92011-01-27 01:07:04 +0000620 if (m_error_stream)
621 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 +0000622
Sean Callananafe16a72010-11-17 23:00:36 +0000623 return false;
624 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000625
Sean Callananafe16a72010-11-17 23:00:36 +0000626 // We expect the following structure:
627 //
628 // struct {
629 // int *isa;
630 // int flags;
631 // char *str;
632 // long length;
633 // };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000634
Sean Callananafe16a72010-11-17 23:00:36 +0000635 if (nsstring_struct->getNumOperands() != 4)
636 {
637 if (log)
638 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 +0000639
Sean Callanan3989fb92011-01-27 01:07:04 +0000640 if (m_error_stream)
641 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 +0000642
Sean Callananafe16a72010-11-17 23:00:36 +0000643 return false;
644 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000645
Sean Callananafe16a72010-11-17 23:00:36 +0000646 Constant *nsstring_member = nsstring_struct->getOperand(2);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000647
Sean Callananafe16a72010-11-17 23:00:36 +0000648 if (!nsstring_member)
649 {
650 if (log)
651 log->PutCString("NSString initializer's str element was empty");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000652
Sean Callanan3989fb92011-01-27 01:07:04 +0000653 if (m_error_stream)
654 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 +0000655
Sean Callananafe16a72010-11-17 23:00:36 +0000656 return false;
657 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000658
Sean Callananafe16a72010-11-17 23:00:36 +0000659 ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000660
Sean Callananafe16a72010-11-17 23:00:36 +0000661 if (!nsstring_expr)
662 {
663 if (log)
664 log->PutCString("NSString initializer's str element is not a ConstantExpr");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000665
Sean Callanan3989fb92011-01-27 01:07:04 +0000666 if (m_error_stream)
667 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 +0000668
Sean Callananafe16a72010-11-17 23:00:36 +0000669 return false;
670 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000671
Sean Callananafe16a72010-11-17 23:00:36 +0000672 if (nsstring_expr->getOpcode() != Instruction::GetElementPtr)
673 {
674 if (log)
675 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 +0000676
Sean Callanan3989fb92011-01-27 01:07:04 +0000677 if (m_error_stream)
678 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 +0000679
Sean Callananafe16a72010-11-17 23:00:36 +0000680 return false;
681 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000682
Sean Callananafe16a72010-11-17 23:00:36 +0000683 Constant *nsstring_cstr = nsstring_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000684
Sean Callananafe16a72010-11-17 23:00:36 +0000685 GlobalVariable *cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000686
Sean Callananafe16a72010-11-17 23:00:36 +0000687 if (!cstr_global)
688 {
689 if (log)
690 log->PutCString("NSString initializer's str element is not a GlobalVariable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000691
Sean Callanan3989fb92011-01-27 01:07:04 +0000692 if (m_error_stream)
693 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 +0000694
Sean Callananafe16a72010-11-17 23:00:36 +0000695 return false;
696 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000697
Sean Callananafe16a72010-11-17 23:00:36 +0000698 if (!cstr_global->hasInitializer())
699 {
700 if (log)
701 log->PutCString("NSString initializer's str element does not have an initializer");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000702
Sean Callanan3989fb92011-01-27 01:07:04 +0000703 if (m_error_stream)
704 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 +0000705
Sean Callananafe16a72010-11-17 23:00:36 +0000706 return false;
707 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000708
Sean Callanan229ce2d2011-02-10 22:17:53 +0000709 /*
Sean Callananafe16a72010-11-17 23:00:36 +0000710 if (!cstr_array)
711 {
712 if (log)
713 log->PutCString("NSString initializer's str element is not a ConstantArray");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000714
Sean Callanan3989fb92011-01-27 01:07:04 +0000715 if (m_error_stream)
716 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 +0000717
Sean Callananafe16a72010-11-17 23:00:36 +0000718 return false;
719 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000720
Sean Callananafe16a72010-11-17 23:00:36 +0000721 if (!cstr_array->isCString())
722 {
723 if (log)
724 log->PutCString("NSString initializer's str element is not a C string array");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000725
Sean Callanan3989fb92011-01-27 01:07:04 +0000726 if (m_error_stream)
727 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 +0000728
Sean Callananafe16a72010-11-17 23:00:36 +0000729 return false;
730 }
Sean Callanan229ce2d2011-02-10 22:17:53 +0000731 */
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000732
Sean Callanand2b465f2012-02-09 03:22:41 +0000733 ConstantDataArray *cstr_array = dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000734
Sean Callananafe16a72010-11-17 23:00:36 +0000735 if (log)
Sean Callanan229ce2d2011-02-10 22:17:53 +0000736 {
737 if (cstr_array)
Sean Callanand2b465f2012-02-09 03:22:41 +0000738 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 +0000739 else
Sean Callanancc427fa2011-07-30 02:42:06 +0000740 log->Printf("Found NSString constant %s, which contains \"\"", value_name_cstr);
Sean Callanan229ce2d2011-02-10 22:17:53 +0000741 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000742
Sean Callanan229ce2d2011-02-10 22:17:53 +0000743 if (!cstr_array)
744 cstr_global = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000745
Sean Callanan1f9db3e2013-06-28 21:44:15 +0000746 if (!RewriteObjCConstString(nsstring_global, cstr_global))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000747 {
Sean Callananafe16a72010-11-17 23:00:36 +0000748 if (log)
749 log->PutCString("Error rewriting the constant string");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000750
Sean Callanan3989fb92011-01-27 01:07:04 +0000751 // We don't print an error message here because RewriteObjCConstString has done so for us.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000752
Sean Callananafe16a72010-11-17 23:00:36 +0000753 return false;
754 }
Sean Callananafe16a72010-11-17 23:00:36 +0000755 }
756 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000757
Sean Callananafe16a72010-11-17 23:00:36 +0000758 for (ValueSymbolTable::iterator vi = value_symbol_table.begin(), ve = value_symbol_table.end();
759 vi != ve;
760 ++vi)
761 {
Sean Callanancc427fa2011-07-30 02:42:06 +0000762 std::string value_name = vi->first().str();
763 const char *value_name_cstr = value_name.c_str();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000764
Sean Callanancc427fa2011-07-30 02:42:06 +0000765 if (!strcmp(value_name_cstr, "__CFConstantStringClassReference"))
Sean Callananafe16a72010-11-17 23:00:36 +0000766 {
767 GlobalVariable *gv = dyn_cast<GlobalVariable>(vi->second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000768
Sean Callananafe16a72010-11-17 23:00:36 +0000769 if (!gv)
770 {
771 if (log)
772 log->PutCString("__CFConstantStringClassReference is not a global variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000773
Sean Callanan3989fb92011-01-27 01:07:04 +0000774 if (m_error_stream)
775 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 +0000776
Sean Callananafe16a72010-11-17 23:00:36 +0000777 return false;
778 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000779
Sean Callananafe16a72010-11-17 23:00:36 +0000780 gv->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000781
Sean Callananafe16a72010-11-17 23:00:36 +0000782 break;
783 }
784 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000785
Sean Callananafe16a72010-11-17 23:00:36 +0000786 return true;
787}
788
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000789static bool IsObjCSelectorRef (Value *value)
Sean Callanan5300d372010-07-31 01:32:05 +0000790{
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000791 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000792
Greg Claytonbd549162014-11-10 21:45:59 +0000793 if (!global_variable || !global_variable->hasName() || !global_variable->getName().startswith("OBJC_SELECTOR_REFERENCES_"))
Sean Callanan5300d372010-07-31 01:32:05 +0000794 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000795
Sean Callanan5300d372010-07-31 01:32:05 +0000796 return true;
797}
798
Sean Callanan3989fb92011-01-27 01:07:04 +0000799// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000800bool
Sean Callanan79763a42011-05-23 21:40:23 +0000801IRForTarget::RewriteObjCSelector (Instruction* selector_load)
Sean Callanan5300d372010-07-31 01:32:05 +0000802{
Greg Clayton5160ce52013-03-27 23:08:40 +0000803 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +0000804
805 LoadInst *load = dyn_cast<LoadInst>(selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000806
Sean Callanan5300d372010-07-31 01:32:05 +0000807 if (!load)
808 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000809
Sean Callanan5300d372010-07-31 01:32:05 +0000810 // Unpack the message name from the selector. In LLVM IR, an objc_msgSend gets represented as
811 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +0000812 // %tmp = load i8** @"OBJC_SELECTOR_REFERENCES_" ; <i8*>
Sean Callanan5300d372010-07-31 01:32:05 +0000813 // %call = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
814 //
815 // where %obj is the object pointer and %tmp is the selector.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000816 //
Greg Clayton45f4f8b2014-11-10 21:48:12 +0000817 // @"OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000818 // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000819
Sean Callanan5300d372010-07-31 01:32:05 +0000820 // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr) and get the string from its target
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000821
Sean Callanan5300d372010-07-31 01:32:05 +0000822 GlobalVariable *_objc_selector_references_ = dyn_cast<GlobalVariable>(load->getPointerOperand());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000823
Sean Callanan5300d372010-07-31 01:32:05 +0000824 if (!_objc_selector_references_ || !_objc_selector_references_->hasInitializer())
825 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000826
Sean Callanan5300d372010-07-31 01:32:05 +0000827 Constant *osr_initializer = _objc_selector_references_->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000828
Sean Callanan5300d372010-07-31 01:32:05 +0000829 ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000830
Sean Callanan5300d372010-07-31 01:32:05 +0000831 if (!osr_initializer_expr || osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
832 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000833
Sean Callanan5300d372010-07-31 01:32:05 +0000834 Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
835
836 if (!osr_initializer_base)
837 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000838
Sean Callanan5300d372010-07-31 01:32:05 +0000839 // Find the string's initializer (a ConstantArray) and get the string from it
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000840
Sean Callanan5300d372010-07-31 01:32:05 +0000841 GlobalVariable *_objc_meth_var_name_ = dyn_cast<GlobalVariable>(osr_initializer_base);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000842
Sean Callanan5300d372010-07-31 01:32:05 +0000843 if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
844 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000845
Sean Callanan5300d372010-07-31 01:32:05 +0000846 Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
847
Sean Callanand2b465f2012-02-09 03:22:41 +0000848 ConstantDataArray *omvn_initializer_array = dyn_cast<ConstantDataArray>(omvn_initializer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000849
Sean Callanan5300d372010-07-31 01:32:05 +0000850 if (!omvn_initializer_array->isString())
851 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000852
Sean Callanan5300d372010-07-31 01:32:05 +0000853 std::string omvn_initializer_string = omvn_initializer_array->getAsString();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000854
Sean Callanan5300d372010-07-31 01:32:05 +0000855 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000856 log->Printf("Found Objective-C selector reference \"%s\"", omvn_initializer_string.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000857
Sean Callanan5300d372010-07-31 01:32:05 +0000858 // Construct a call to sel_registerName
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000859
Sean Callanan5300d372010-07-31 01:32:05 +0000860 if (!m_sel_registerName)
861 {
Greg Claytoncb7e3b32010-11-15 01:47:11 +0000862 lldb::addr_t sel_registerName_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000863
Greg Clayton7b462cc2010-10-15 22:48:33 +0000864 static lldb_private::ConstString g_sel_registerName_str ("sel_registerName");
Sean Callanan2a8fa2a2016-02-13 00:01:46 +0000865 sel_registerName_addr = m_execution_unit.FindSymbol (g_sel_registerName_str);
866 if (sel_registerName_addr == LLDB_INVALID_ADDRESS)
Sean Callanan5300d372010-07-31 01:32:05 +0000867 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000868
Sean Callananbe3a1b12010-10-26 00:31:56 +0000869 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000870 log->Printf("Found sel_registerName at 0x%" PRIx64, sel_registerName_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000871
Sean Callanan5300d372010-07-31 01:32:05 +0000872 // Build the function type: struct objc_selector *sel_registerName(uint8_t*)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000873
Sean Callanan5300d372010-07-31 01:32:05 +0000874 // The below code would be "more correct," but in actuality what's required is uint8_t*
Sean Callanan79763a42011-05-23 21:40:23 +0000875 //Type *sel_type = StructType::get(m_module->getContext());
Sean Callanan5300d372010-07-31 01:32:05 +0000876 //Type *sel_ptr_type = PointerType::getUnqual(sel_type);
Sean Callanancc427fa2011-07-30 02:42:06 +0000877 Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000878
Sean Callanancc427fa2011-07-30 02:42:06 +0000879 Type *type_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000880
Sean Callanancc427fa2011-07-30 02:42:06 +0000881 type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000882
Sean Callanancc427fa2011-07-30 02:42:06 +0000883 ArrayRef<Type *> srN_arg_types(type_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000884
Sean Callanan5300d372010-07-31 01:32:05 +0000885 llvm::Type *srN_type = FunctionType::get(sel_ptr_type, srN_arg_types, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000886
Sean Callanan5300d372010-07-31 01:32:05 +0000887 // Build the constant containing the pointer to the function
Sean Callanan5300d372010-07-31 01:32:05 +0000888 PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
Sean Callanan439dcae2013-12-20 19:55:02 +0000889 Constant *srN_addr_int = ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
Sean Callanan5300d372010-07-31 01:32:05 +0000890 m_sel_registerName = ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty);
891 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000892
Sean Callanancc427fa2011-07-30 02:42:06 +0000893 Value *argument_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000894
Sean Callanan79763a42011-05-23 21:40:23 +0000895 Constant *omvn_pointer = ConstantExpr::getBitCast(_objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000896
Sean Callanancc427fa2011-07-30 02:42:06 +0000897 argument_array[0] = omvn_pointer;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000898
Sean Callanancc427fa2011-07-30 02:42:06 +0000899 ArrayRef<Value *> srN_arguments(argument_array, 1);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000900
901 CallInst *srN_call = CallInst::Create(m_sel_registerName,
Sean Callanancc427fa2011-07-30 02:42:06 +0000902 srN_arguments,
Sean Callananafe16a72010-11-17 23:00:36 +0000903 "sel_registerName",
Sean Callanan5300d372010-07-31 01:32:05 +0000904 selector_load);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000905
Sean Callanan5300d372010-07-31 01:32:05 +0000906 // Replace the load with the call in all users
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000907
Sean Callanan5300d372010-07-31 01:32:05 +0000908 selector_load->replaceAllUsesWith(srN_call);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000909
Sean Callanan5300d372010-07-31 01:32:05 +0000910 selector_load->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000911
Sean Callanan5300d372010-07-31 01:32:05 +0000912 return true;
913}
914
915bool
Sean Callanan79763a42011-05-23 21:40:23 +0000916IRForTarget::RewriteObjCSelectors (BasicBlock &basic_block)
Sean Callanan5300d372010-07-31 01:32:05 +0000917{
Greg Clayton5160ce52013-03-27 23:08:40 +0000918 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5300d372010-07-31 01:32:05 +0000919
920 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000921
Sean Callanan5300d372010-07-31 01:32:05 +0000922 typedef SmallVector <Instruction*, 2> InstrList;
923 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000924
Sean Callanan5300d372010-07-31 01:32:05 +0000925 InstrList selector_loads;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000926
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000927 for (ii = basic_block.begin();
928 ii != basic_block.end();
Sean Callanan5300d372010-07-31 01:32:05 +0000929 ++ii)
930 {
931 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000932
Sean Callanan5300d372010-07-31 01:32:05 +0000933 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
Greg Clayton1b95a6f2010-11-19 01:05:25 +0000934 if (IsObjCSelectorRef(load->getPointerOperand()))
Sean Callanan5300d372010-07-31 01:32:05 +0000935 selector_loads.push_back(&inst);
936 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000937
Sean Callanan5300d372010-07-31 01:32:05 +0000938 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000939
Sean Callanan5300d372010-07-31 01:32:05 +0000940 for (iter = selector_loads.begin();
941 iter != selector_loads.end();
942 ++iter)
943 {
Sean Callanan79763a42011-05-23 21:40:23 +0000944 if (!RewriteObjCSelector(*iter))
Sean Callanan5300d372010-07-31 01:32:05 +0000945 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000946 if (m_error_stream)
947 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 +0000948
Enrico Granata20edcdb2011-07-19 18:03:25 +0000949 if (log)
Sean Callanan5300d372010-07-31 01:32:05 +0000950 log->PutCString("Couldn't rewrite a reference to an Objective-C selector");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000951
Sean Callanan5300d372010-07-31 01:32:05 +0000952 return false;
953 }
954 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000955
Sean Callanan5300d372010-07-31 01:32:05 +0000956 return true;
957}
958
Sean Callanan3989fb92011-01-27 01:07:04 +0000959// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000960bool
Sean Callanan79763a42011-05-23 21:40:23 +0000961IRForTarget::RewritePersistentAlloc (llvm::Instruction *persistent_alloc)
Sean Callanan2235f322010-08-11 03:57:18 +0000962{
Greg Clayton5160ce52013-03-27 23:08:40 +0000963 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane1175b72011-01-13 21:23:32 +0000964
Sean Callanan2235f322010-08-11 03:57:18 +0000965 AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000966
Duncan P. N. Exon Smith68caa7d2014-11-12 01:59:53 +0000967 MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
Sean Callanan2235f322010-08-11 03:57:18 +0000968
969 if (!alloc_md || !alloc_md->getNumOperands())
970 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000971
Zachary Turner543afa12014-12-09 22:29:47 +0000972 ConstantInt *constant_int = mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000973
Sean Callanan2235f322010-08-11 03:57:18 +0000974 if (!constant_int)
975 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000976
Sean Callanan2235f322010-08-11 03:57:18 +0000977 // We attempt to register this as a new persistent variable with the DeclMap.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000978
Sean Callanan2235f322010-08-11 03:57:18 +0000979 uintptr_t ptr = constant_int->getZExtValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000980
Sean Callanand1e5b432010-08-12 01:56:52 +0000981 clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000982
Sean Callanand1e5b432010-08-12 01:56:52 +0000983 lldb_private::TypeFromParser result_decl_type (decl->getType().getAsOpaquePtr(),
Greg Claytond8d4a572015-08-11 21:38:15 +0000984 lldb_private::ClangASTContext::GetASTContext(&decl->getASTContext()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000985
Greg Clayton7b462cc2010-10-15 22:48:33 +0000986 StringRef decl_name (decl->getName());
987 lldb_private::ConstString persistent_variable_name (decl_name.data(), decl_name.size());
Sean Callanan92adcac2011-01-13 08:53:35 +0000988 if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name, result_decl_type, false, false))
Sean Callanan2235f322010-08-11 03:57:18 +0000989 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000990
Sean Callanan79763a42011-05-23 21:40:23 +0000991 GlobalVariable *persistent_global = new GlobalVariable((*m_module),
Sean Callanane1175b72011-01-13 21:23:32 +0000992 alloc->getType(),
Sean Callanan2235f322010-08-11 03:57:18 +0000993 false, /* not constant */
994 GlobalValue::ExternalLinkage,
995 NULL, /* no initializer */
996 alloc->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000997
Sean Callanan2235f322010-08-11 03:57:18 +0000998 // What we're going to do here is make believe this was a regular old external
999 // variable. That means we need to make the metadata valid.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001000
Sean Callanan585c0ec82012-07-04 01:26:26 +00001001 NamedMDNode *named_metadata = m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001002
Zachary Turner543afa12014-12-09 22:29:47 +00001003 llvm::Metadata *values[2];
1004 values[0] = ConstantAsMetadata::get(persistent_global);
1005 values[1] = ConstantAsMetadata::get(constant_int);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001006
Zachary Turner543afa12014-12-09 22:29:47 +00001007 ArrayRef<llvm::Metadata *> value_ref(values, 2);
Sean Callanan2235f322010-08-11 03:57:18 +00001008
Sean Callanan79763a42011-05-23 21:40:23 +00001009 MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
Sean Callanan2235f322010-08-11 03:57:18 +00001010 named_metadata->addOperand(persistent_global_md);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001011
Sean Callanane1175b72011-01-13 21:23:32 +00001012 // Now, since the variable is a pointer variable, we will drop in a load of that
1013 // pointer variable.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001014
Sean Callanane1175b72011-01-13 21:23:32 +00001015 LoadInst *persistent_load = new LoadInst (persistent_global, "", alloc);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001016
Sean Callanane1175b72011-01-13 21:23:32 +00001017 if (log)
1018 log->Printf("Replacing \"%s\" with \"%s\"",
1019 PrintValue(alloc).c_str(),
1020 PrintValue(persistent_load).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001021
Sean Callanane1175b72011-01-13 21:23:32 +00001022 alloc->replaceAllUsesWith(persistent_load);
Sean Callanan2235f322010-08-11 03:57:18 +00001023 alloc->eraseFromParent();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001024
Sean Callanan2235f322010-08-11 03:57:18 +00001025 return true;
1026}
1027
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001028bool
Sean Callanan79763a42011-05-23 21:40:23 +00001029IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block)
Sean Callanan2235f322010-08-11 03:57:18 +00001030{
Sean Callanan9e6ed532010-09-13 21:34:21 +00001031 if (!m_resolve_vars)
1032 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001033
Greg Clayton5160ce52013-03-27 23:08:40 +00001034 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001035
Sean Callanan2235f322010-08-11 03:57:18 +00001036 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001037
Sean Callanan2235f322010-08-11 03:57:18 +00001038 typedef SmallVector <Instruction*, 2> InstrList;
1039 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001040
Sean Callanan2235f322010-08-11 03:57:18 +00001041 InstrList pvar_allocs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001042
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001043 for (ii = basic_block.begin();
1044 ii != basic_block.end();
Sean Callanan2235f322010-08-11 03:57:18 +00001045 ++ii)
1046 {
1047 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001048
Sean Callanan2235f322010-08-11 03:57:18 +00001049 if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst))
Sean Callananf694a552011-01-21 22:30:25 +00001050 {
1051 llvm::StringRef alloc_name = alloc->getName();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001052
Sean Callananf694a552011-01-21 22:30:25 +00001053 if (alloc_name.startswith("$") &&
1054 !alloc_name.startswith("$__lldb"))
1055 {
1056 if (alloc_name.find_first_of("0123456789") == 1)
1057 {
1058 if (log)
1059 log->Printf("Rejecting a numeric persistent variable.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001060
Sean Callanan3989fb92011-01-27 01:07:04 +00001061 if (m_error_stream)
1062 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 +00001063
Sean Callananf694a552011-01-21 22:30:25 +00001064 return false;
1065 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001066
Sean Callanan2235f322010-08-11 03:57:18 +00001067 pvar_allocs.push_back(alloc);
Sean Callananf694a552011-01-21 22:30:25 +00001068 }
1069 }
Sean Callanan2235f322010-08-11 03:57:18 +00001070 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001071
Sean Callanan2235f322010-08-11 03:57:18 +00001072 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001073
Sean Callanan2235f322010-08-11 03:57:18 +00001074 for (iter = pvar_allocs.begin();
1075 iter != pvar_allocs.end();
1076 ++iter)
1077 {
Sean Callanan79763a42011-05-23 21:40:23 +00001078 if (!RewritePersistentAlloc(*iter))
Sean Callanan2235f322010-08-11 03:57:18 +00001079 {
Sean Callanan3989fb92011-01-27 01:07:04 +00001080 if (m_error_stream)
1081 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't rewrite the creation of a persistent variable\n");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001082
Enrico Granata20edcdb2011-07-19 18:03:25 +00001083 if (log)
Sean Callanan2235f322010-08-11 03:57:18 +00001084 log->PutCString("Couldn't rewrite the creation of a persistent variable");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001085
Sean Callanan2235f322010-08-11 03:57:18 +00001086 return false;
1087 }
1088 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001089
Sean Callanan2235f322010-08-11 03:57:18 +00001090 return true;
1091}
1092
Sean Callananc70ed462011-10-25 18:36:40 +00001093bool
1094IRForTarget::MaterializeInitializer (uint8_t *data, Constant *initializer)
1095{
1096 if (!initializer)
1097 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001098
Greg Clayton5160ce52013-03-27 23:08:40 +00001099 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananc70ed462011-10-25 18:36:40 +00001100
1101 if (log && log->GetVerbose())
David Blaikie129b8392015-04-08 20:23:52 +00001102 log->Printf(" MaterializeInitializer(%p, %s)", (void *)data, PrintValue(initializer).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001103
Sean Callananc70ed462011-10-25 18:36:40 +00001104 Type *initializer_type = initializer->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001105
Sean Callananc70ed462011-10-25 18:36:40 +00001106 if (ConstantInt *int_initializer = dyn_cast<ConstantInt>(initializer))
1107 {
1108 memcpy (data, int_initializer->getValue().getRawData(), m_target_data->getTypeStoreSize(initializer_type));
1109 return true;
1110 }
Sean Callanand2b465f2012-02-09 03:22:41 +00001111 else if (ConstantDataArray *array_initializer = dyn_cast<ConstantDataArray>(initializer))
Sean Callananc70ed462011-10-25 18:36:40 +00001112 {
1113 if (array_initializer->isString())
1114 {
1115 std::string array_initializer_string = array_initializer->getAsString();
1116 memcpy (data, array_initializer_string.c_str(), m_target_data->getTypeStoreSize(initializer_type));
1117 }
1118 else
1119 {
1120 ArrayType *array_initializer_type = array_initializer->getType();
1121 Type *array_element_type = array_initializer_type->getElementType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001122
Sean Callananc70ed462011-10-25 18:36:40 +00001123 size_t element_size = m_target_data->getTypeAllocSize(array_element_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001124
Andy Gibbsa297a972013-06-19 19:04:53 +00001125 for (unsigned i = 0; i < array_initializer->getNumOperands(); ++i)
Sean Callananc70ed462011-10-25 18:36:40 +00001126 {
Sean Callanand2b465f2012-02-09 03:22:41 +00001127 Value *operand_value = array_initializer->getOperand(i);
1128 Constant *operand_constant = dyn_cast<Constant>(operand_value);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001129
Sean Callanand2b465f2012-02-09 03:22:41 +00001130 if (!operand_constant)
1131 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001132
Sean Callanand2b465f2012-02-09 03:22:41 +00001133 if (!MaterializeInitializer(data + (i * element_size), operand_constant))
Sean Callananc70ed462011-10-25 18:36:40 +00001134 return false;
1135 }
1136 }
1137 return true;
1138 }
1139 else if (ConstantStruct *struct_initializer = dyn_cast<ConstantStruct>(initializer))
1140 {
1141 StructType *struct_initializer_type = struct_initializer->getType();
1142 const StructLayout *struct_layout = m_target_data->getStructLayout(struct_initializer_type);
1143
Andy Gibbsa297a972013-06-19 19:04:53 +00001144 for (unsigned i = 0;
Sean Callananc70ed462011-10-25 18:36:40 +00001145 i < struct_initializer->getNumOperands();
1146 ++i)
1147 {
1148 if (!MaterializeInitializer(data + struct_layout->getElementOffset(i), struct_initializer->getOperand(i)))
1149 return false;
1150 }
1151 return true;
1152 }
Sean Callanan76ee3e72013-04-24 19:50:12 +00001153 else if (isa<ConstantAggregateZero>(initializer))
1154 {
1155 memset(data, 0, m_target_data->getTypeStoreSize(initializer_type));
1156 return true;
1157 }
Sean Callananc70ed462011-10-25 18:36:40 +00001158 return false;
1159}
1160
Sean Callanan3989fb92011-01-27 01:07:04 +00001161// This function does not report errors; its callers are responsible.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001162bool
Sean Callanan79763a42011-05-23 21:40:23 +00001163IRForTarget::MaybeHandleVariable (Value *llvm_value_ptr)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001164{
Greg Clayton5160ce52013-03-27 23:08:40 +00001165 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001166
Sean Callanand7a1ca22010-12-02 19:47:57 +00001167 if (log)
Sean Callanan88339f02010-12-06 22:16:55 +00001168 log->Printf("MaybeHandleVariable (%s)", PrintValue(llvm_value_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001169
Greg Clayton7b462cc2010-10-15 22:48:33 +00001170 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr))
Sean Callanan4cf04d22010-08-03 00:23:29 +00001171 {
Sean Callanan5666b672010-08-04 01:02:13 +00001172 switch (constant_expr->getOpcode())
Sean Callanan4cf04d22010-08-03 00:23:29 +00001173 {
Sean Callanan5666b672010-08-04 01:02:13 +00001174 default:
1175 break;
1176 case Instruction::GetElementPtr:
1177 case Instruction::BitCast:
Sean Callanan4cf04d22010-08-03 00:23:29 +00001178 Value *s = constant_expr->getOperand(0);
Sean Callanan79763a42011-05-23 21:40:23 +00001179 if (!MaybeHandleVariable(s))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001180 return false;
Sean Callanan4cf04d22010-08-03 00:23:29 +00001181 }
1182 }
Sean Callanand6e04ae2010-12-03 19:51:05 +00001183 else if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001184 {
Sean Callananc70ed462011-10-25 18:36:40 +00001185 if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00001186 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001187
Sean Callanan79763a42011-05-23 21:40:23 +00001188 clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001189
Sean Callanan5300d372010-07-31 01:32:05 +00001190 if (!named_decl)
1191 {
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001192 if (IsObjCSelectorRef(llvm_value_ptr))
Sean Callanan5300d372010-07-31 01:32:05 +00001193 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001194
Sean Callanan14f0b0e2010-12-06 00:56:39 +00001195 if (!global_variable->hasExternalLinkage())
1196 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001197
Sean Callanan5300d372010-07-31 01:32:05 +00001198 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001199 log->Printf("Found global variable \"%s\" without metadata", global_variable->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001200
Sean Callanan5300d372010-07-31 01:32:05 +00001201 return false;
1202 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001203
Greg Clayton7b462cc2010-10-15 22:48:33 +00001204 std::string name (named_decl->getName().str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001205
Greg Clayton57ee3062013-07-11 22:46:58 +00001206 clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1207 if (value_decl == NULL)
Sean Callananea22d422010-07-16 00:09:46 +00001208 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001209
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001210 lldb_private::CompilerType compiler_type(&value_decl->getASTContext(), value_decl->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001211
Sean Callanan77eaf442011-07-08 00:39:14 +00001212 const Type *value_type = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001213
Sean Callanane1175b72011-01-13 21:23:32 +00001214 if (name[0] == '$')
Sean Callanan92adcac2011-01-13 08:53:35 +00001215 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001216 // The $__lldb_expr_result name indicates the return value has allocated as
Sean Callanan92adcac2011-01-13 08:53:35 +00001217 // a static variable. Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1218 // accesses to this static variable need to be redirected to the result of dereferencing
1219 // a pointer that is passed in as one of the arguments.
1220 //
1221 // Consequently, when reporting the size of the type, we report a pointer type pointing
1222 // to the type of $__lldb_expr_result, not the type itself.
Sean Callanane1175b72011-01-13 21:23:32 +00001223 //
1224 // We also do this for any user-declared persistent variables.
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001225 compiler_type = compiler_type.GetPointerType();
Sean Callanan92adcac2011-01-13 08:53:35 +00001226 value_type = PointerType::get(global_variable->getType(), 0);
1227 }
1228 else
1229 {
Sean Callanan92adcac2011-01-13 08:53:35 +00001230 value_type = global_variable->getType();
1231 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001232
Bruce Mitchener3ad353f2015-09-24 03:54:50 +00001233 const uint64_t value_size = compiler_type.GetByteSize(nullptr);
1234 lldb::offset_t value_alignment = (compiler_type.GetTypeBitAlign() + 7ull) / 8ull;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001235
Sean Callanan038df5032010-09-30 21:18:25 +00001236 if (log)
Greg Clayton57ee3062013-07-11 22:46:58 +00001237 {
Zachary Turnera746e8e2014-07-02 17:24:07 +00001238 log->Printf("Type of \"%s\" is [clang \"%s\", llvm \"%s\"] [size %" PRIu64 ", align %" PRIu64 "]",
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001239 name.c_str(), lldb_private::ClangUtil::GetQualType(compiler_type).getAsString().c_str(),
1240 PrintType(value_type).c_str(), value_size, value_alignment);
Greg Clayton57ee3062013-07-11 22:46:58 +00001241 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001242
1243
Sean Callanan64dfc9a2010-08-23 23:09:38 +00001244 if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001245 lldb_private::ConstString (name.c_str()),
1246 llvm_value_ptr,
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001247 value_size,
Sean Callanan4edba2d2010-07-27 02:07:53 +00001248 value_alignment))
Sean Callananaf8e96c2011-08-01 17:41:38 +00001249 {
1250 if (!global_variable->hasExternalLinkage())
1251 return true;
1252 else
Sean Callanan2a8fa2a2016-02-13 00:01:46 +00001253 return true;
Sean Callananaf8e96c2011-08-01 17:41:38 +00001254 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001255 }
Sean Callanan4a5fcbb2010-12-03 03:02:31 +00001256 else if (dyn_cast<llvm::Function>(llvm_value_ptr))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001257 {
1258 if (log)
1259 log->Printf("Function pointers aren't handled right now");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001260
Sean Callanand7a1ca22010-12-02 19:47:57 +00001261 return false;
1262 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001263
Sean Callanan549c9f72010-07-13 21:41:46 +00001264 return true;
1265}
1266
Sean Callanan3989fb92011-01-27 01:07:04 +00001267// This function does not report errors; its callers are responsible.
Sean Callanan549c9f72010-07-13 21:41:46 +00001268bool
Sean Callanan79763a42011-05-23 21:40:23 +00001269IRForTarget::HandleSymbol (Value *symbol)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001270{
Greg Clayton5160ce52013-03-27 23:08:40 +00001271 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001272
Sean Callananc3a16002011-01-17 23:42:46 +00001273 lldb_private::ConstString name(symbol->getName().str().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001274
Sean Callanan947ccc72011-12-01 02:04:16 +00001275 lldb::addr_t symbol_addr = m_decl_map->GetSymbolAddress (name, lldb::eSymbolTypeAny);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001276
Greg Clayton084db102011-06-23 04:25:29 +00001277 if (symbol_addr == LLDB_INVALID_ADDRESS)
Sean Callananc3a16002011-01-17 23:42:46 +00001278 {
1279 if (log)
1280 log->Printf ("Symbol \"%s\" had no address", name.GetCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001281
Sean Callananc3a16002011-01-17 23:42:46 +00001282 return false;
1283 }
1284
1285 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00001286 log->Printf("Found \"%s\" at 0x%" PRIx64, name.GetCString(), symbol_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001287
Sean Callanancc427fa2011-07-30 02:42:06 +00001288 Type *symbol_type = symbol->getType();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001289
Sean Callanan439dcae2013-12-20 19:55:02 +00001290 Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001291
Sean Callananc3a16002011-01-17 23:42:46 +00001292 Value *symbol_addr_ptr = ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001293
Sean Callananc3a16002011-01-17 23:42:46 +00001294 if (log)
1295 log->Printf("Replacing %s with %s", PrintValue(symbol).c_str(), PrintValue(symbol_addr_ptr).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001296
Sean Callananc3a16002011-01-17 23:42:46 +00001297 symbol->replaceAllUsesWith(symbol_addr_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001298
Sean Callananc3a16002011-01-17 23:42:46 +00001299 return true;
1300}
1301
1302bool
Sean Callanan79763a42011-05-23 21:40:23 +00001303IRForTarget::MaybeHandleCallArguments (CallInst *Old)
Sean Callanan85a0a832010-10-05 22:26:43 +00001304{
Greg Clayton5160ce52013-03-27 23:08:40 +00001305 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001306
Sean Callanand7a1ca22010-12-02 19:47:57 +00001307 if (log)
1308 log->Printf("MaybeHandleCallArguments(%s)", PrintValue(Old).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001309
Sean Callananafe16a72010-11-17 23:00:36 +00001310 for (unsigned op_index = 0, num_ops = Old->getNumArgOperands();
Sean Callanan85a0a832010-10-05 22:26:43 +00001311 op_index < num_ops;
1312 ++op_index)
Sean Callanan79763a42011-05-23 21:40:23 +00001313 if (!MaybeHandleVariable(Old->getArgOperand(op_index))) // conservatively believe that this is a store
Sean Callanan3989fb92011-01-27 01:07:04 +00001314 {
1315 if (m_error_stream)
1316 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 +00001317
Sean Callanan85a0a832010-10-05 22:26:43 +00001318 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001319 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001320
Sean Callanan85a0a832010-10-05 22:26:43 +00001321 return true;
1322}
1323
1324bool
Sean Callananfc89c142011-11-01 23:38:03 +00001325IRForTarget::HandleObjCClass(Value *classlist_reference)
1326{
Greg Clayton5160ce52013-03-27 23:08:40 +00001327 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc89c142011-11-01 23:38:03 +00001328
1329 GlobalVariable *global_variable = dyn_cast<GlobalVariable>(classlist_reference);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001330
Sean Callananfc89c142011-11-01 23:38:03 +00001331 if (!global_variable)
1332 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001333
Sean Callananfc89c142011-11-01 23:38:03 +00001334 Constant *initializer = global_variable->getInitializer();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001335
Sean Callananfc89c142011-11-01 23:38:03 +00001336 if (!initializer)
1337 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001338
Sean Callananfc89c142011-11-01 23:38:03 +00001339 if (!initializer->hasName())
1340 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001341
Sean Callananfc89c142011-11-01 23:38:03 +00001342 StringRef name(initializer->getName());
1343 lldb_private::ConstString name_cstr(name.str().c_str());
Greg Clayton1075aca2011-12-03 20:02:42 +00001344 lldb::addr_t class_ptr = m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001345
Sean Callananfc89c142011-11-01 23:38:03 +00001346 if (log)
1347 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 +00001348
Sean Callananfc89c142011-11-01 23:38:03 +00001349 if (class_ptr == LLDB_INVALID_ADDRESS)
1350 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001351
Ed Mastea8553092014-03-10 17:24:16 +00001352 if (global_variable->use_empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001353 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001354
Sean Callanan719a4d52013-03-23 01:01:16 +00001355 SmallVector<LoadInst *, 2> load_instructions;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001356
Ed Mastea8553092014-03-10 17:24:16 +00001357 for (llvm::User *u : global_variable->users())
Sean Callananfc89c142011-11-01 23:38:03 +00001358 {
Ed Mastea8553092014-03-10 17:24:16 +00001359 if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
Sean Callanan719a4d52013-03-23 01:01:16 +00001360 load_instructions.push_back(load_instruction);
Sean Callananfc89c142011-11-01 23:38:03 +00001361 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001362
Sean Callanan719a4d52013-03-23 01:01:16 +00001363 if (load_instructions.empty())
Sean Callananfc89c142011-11-01 23:38:03 +00001364 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001365
Sean Callanan439dcae2013-12-20 19:55:02 +00001366 Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001367
Sean Callanan719a4d52013-03-23 01:01:16 +00001368 for (LoadInst *load_instruction : load_instructions)
1369 {
1370 Constant *class_bitcast = ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001371
Sean Callanan719a4d52013-03-23 01:01:16 +00001372 load_instruction->replaceAllUsesWith(class_bitcast);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001373
Sean Callanan719a4d52013-03-23 01:01:16 +00001374 load_instruction->eraseFromParent();
1375 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001376
Sean Callananfc89c142011-11-01 23:38:03 +00001377 return true;
1378}
1379
1380bool
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001381IRForTarget::RemoveCXAAtExit (BasicBlock &basic_block)
1382{
1383 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001384
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001385 std::vector<CallInst *> calls_to_remove;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001386
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001387 for (ii = basic_block.begin();
1388 ii != basic_block.end();
1389 ++ii)
1390 {
1391 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001392
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001393 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001394
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001395 // MaybeHandleCallArguments handles error reporting; we are silent here
1396 if (!call)
1397 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001398
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001399 bool remove = false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001400
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001401 llvm::Function *func = call->getCalledFunction();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001402
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001403 if (func && func->getName() == "__cxa_atexit")
1404 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001405
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001406 llvm::Value *val = call->getCalledValue();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001407
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001408 if (val && val->getName() == "__cxa_atexit")
1409 remove = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001410
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001411 if (remove)
1412 calls_to_remove.push_back(call);
1413 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001414
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001415 for (std::vector<CallInst *>::iterator ci = calls_to_remove.begin(), ce = calls_to_remove.end();
1416 ci != ce;
1417 ++ci)
1418 {
1419 (*ci)->eraseFromParent();
1420 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001421
Sean Callanan6e6d4a62012-07-21 02:02:15 +00001422 return true;
1423}
1424
1425bool
Sean Callanan79763a42011-05-23 21:40:23 +00001426IRForTarget::ResolveCalls(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001427{
Sean Callanan2ab712f22010-07-03 01:35:46 +00001428 /////////////////////////////////////////////////////////////////////////
1429 // Prepare the current basic block for execution in the remote process
1430 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001431
Sean Callanan7ea35012010-07-27 21:39:39 +00001432 BasicBlock::iterator ii;
Sean Callanan549c9f72010-07-13 21:41:46 +00001433
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001434 for (ii = basic_block.begin();
1435 ii != basic_block.end();
Sean Callanan549c9f72010-07-13 21:41:46 +00001436 ++ii)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001437 {
Sean Callanan549c9f72010-07-13 21:41:46 +00001438 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001439
Sean Callanana4e55172010-11-08 00:31:32 +00001440 CallInst *call = dyn_cast<CallInst>(&inst);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001441
Sean Callanan3989fb92011-01-27 01:07:04 +00001442 // MaybeHandleCallArguments handles error reporting; we are silent here
Sean Callanan79763a42011-05-23 21:40:23 +00001443 if (call && !MaybeHandleCallArguments(call))
Sean Callanand7a1ca22010-12-02 19:47:57 +00001444 return false;
Sean Callanan2ab712f22010-07-03 01:35:46 +00001445 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001446
Sean Callanan2ab712f22010-07-03 01:35:46 +00001447 return true;
1448}
1449
Sean Callanana4e55172010-11-08 00:31:32 +00001450bool
Sean Callanan79763a42011-05-23 21:40:23 +00001451IRForTarget::ResolveExternals (Function &llvm_function)
Sean Callanana4e55172010-11-08 00:31:32 +00001452{
Greg Clayton5160ce52013-03-27 23:08:40 +00001453 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001454
Sean Callanan339f6152014-03-11 19:19:16 +00001455 for (GlobalVariable &global_var : m_module->globals())
Sean Callanana4e55172010-11-08 00:31:32 +00001456 {
Sean Callanan339f6152014-03-11 19:19:16 +00001457 std::string global_name = global_var.getName().str();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001458
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001459 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001460 log->Printf("Examining %s, DeclForGlobalValue returns %p",
Sean Callanan694e2442011-12-22 21:24:49 +00001461 global_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001462 static_cast<void*>(DeclForGlobal(&global_var)));
1463
Sean Callananfc89c142011-11-01 23:38:03 +00001464 if (global_name.find("OBJC_IVAR") == 0)
Sean Callananc3a16002011-01-17 23:42:46 +00001465 {
Sean Callanan339f6152014-03-11 19:19:16 +00001466 if (!HandleSymbol(&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001467 {
1468 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001469 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 +00001470
Sean Callananc3a16002011-01-17 23:42:46 +00001471 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001472 }
Sean Callananc3a16002011-01-17 23:42:46 +00001473 }
Sean Callananfc89c142011-11-01 23:38:03 +00001474 else if (global_name.find("OBJC_CLASSLIST_REFERENCES_$") != global_name.npos)
1475 {
Sean Callanan339f6152014-03-11 19:19:16 +00001476 if (!HandleObjCClass(&global_var))
Sean Callananfc89c142011-11-01 23:38:03 +00001477 {
1478 if (m_error_stream)
1479 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 +00001480
Sean Callananfc89c142011-11-01 23:38:03 +00001481 return false;
1482 }
1483 }
Sean Callanan2ad66912013-04-24 21:25:20 +00001484 else if (global_name.find("OBJC_CLASSLIST_SUP_REFS_$") != global_name.npos)
1485 {
Sean Callanan339f6152014-03-11 19:19:16 +00001486 if (!HandleObjCClass(&global_var))
Sean Callanan2ad66912013-04-24 21:25:20 +00001487 {
1488 if (m_error_stream)
1489 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 +00001490
Sean Callanan2ad66912013-04-24 21:25:20 +00001491 return false;
1492 }
1493 }
Sean Callanan339f6152014-03-11 19:19:16 +00001494 else if (DeclForGlobal(&global_var))
Sean Callananc3a16002011-01-17 23:42:46 +00001495 {
Sean Callanan339f6152014-03-11 19:19:16 +00001496 if (!MaybeHandleVariable (&global_var))
Sean Callanan3989fb92011-01-27 01:07:04 +00001497 {
1498 if (m_error_stream)
Sean Callanan694e2442011-12-22 21:24:49 +00001499 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 +00001500
Sean Callananc3a16002011-01-17 23:42:46 +00001501 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001502 }
Sean Callananc3a16002011-01-17 23:42:46 +00001503 }
Sean Callanana4e55172010-11-08 00:31:32 +00001504 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001505
Sean Callanana4e55172010-11-08 00:31:32 +00001506 return true;
1507}
1508
Sean Callanan7ea35012010-07-27 21:39:39 +00001509static bool isGuardVariableRef(Value *V)
Sean Callananddb46ef2010-07-24 01:37:44 +00001510{
Sean Callanan77eaf442011-07-08 00:39:14 +00001511 Constant *Old = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001512
Sean Callananafe16a72010-11-17 23:00:36 +00001513 if (!(Old = dyn_cast<Constant>(V)))
Sean Callananddb46ef2010-07-24 01:37:44 +00001514 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001515
Sean Callanan77eaf442011-07-08 00:39:14 +00001516 ConstantExpr *CE = NULL;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001517
Sean Callanane2ef6e32010-09-23 03:01:22 +00001518 if ((CE = dyn_cast<ConstantExpr>(V)))
1519 {
1520 if (CE->getOpcode() != Instruction::BitCast)
1521 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001522
Sean Callananafe16a72010-11-17 23:00:36 +00001523 Old = CE->getOperand(0);
Sean Callanane2ef6e32010-09-23 03:01:22 +00001524 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001525
Sean Callananafe16a72010-11-17 23:00:36 +00001526 GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001527
Zachary Turnerf16ae602015-01-09 21:12:22 +00001528 if (!GV || !GV->hasName() ||
1529 (!GV->getName().startswith("_ZGV") && // Itanium ABI guard variable
1530 !GV->getName().endswith("@4IA"))) // Microsoft ABI guard variable
1531 {
Sean Callananddb46ef2010-07-24 01:37:44 +00001532 return false;
Zachary Turnerf16ae602015-01-09 21:12:22 +00001533 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001534
Sean Callananddb46ef2010-07-24 01:37:44 +00001535 return true;
1536}
1537
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001538void
Sean Callanan79763a42011-05-23 21:40:23 +00001539IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction* guard_load)
Sean Callananddb46ef2010-07-24 01:37:44 +00001540{
Zachary Turnerf16ae602015-01-09 21:12:22 +00001541 Constant *zero(Constant::getNullValue(guard_load->getType()));
1542 guard_load->replaceAllUsesWith(zero);
Sean Callananddb46ef2010-07-24 01:37:44 +00001543 guard_load->eraseFromParent();
1544}
1545
1546static void ExciseGuardStore(Instruction* guard_store)
1547{
1548 guard_store->eraseFromParent();
1549}
1550
1551bool
Sean Callanan79763a42011-05-23 21:40:23 +00001552IRForTarget::RemoveGuards(BasicBlock &basic_block)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001553{
Sean Callananddb46ef2010-07-24 01:37:44 +00001554 ///////////////////////////////////////////////////////
1555 // Eliminate any reference to guard variables found.
1556 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001557
Sean Callanan7ea35012010-07-27 21:39:39 +00001558 BasicBlock::iterator ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001559
Sean Callanan7ea35012010-07-27 21:39:39 +00001560 typedef SmallVector <Instruction*, 2> InstrList;
Sean Callananddb46ef2010-07-24 01:37:44 +00001561 typedef InstrList::iterator InstrIterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001562
Sean Callananddb46ef2010-07-24 01:37:44 +00001563 InstrList guard_loads;
1564 InstrList guard_stores;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001565
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001566 for (ii = basic_block.begin();
1567 ii != basic_block.end();
Sean Callananddb46ef2010-07-24 01:37:44 +00001568 ++ii)
1569 {
1570 Instruction &inst = *ii;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001571
Sean Callananddb46ef2010-07-24 01:37:44 +00001572 if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1573 if (isGuardVariableRef(load->getPointerOperand()))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001574 guard_loads.push_back(&inst);
1575
1576 if (StoreInst *store = dyn_cast<StoreInst>(&inst))
Sean Callananddb46ef2010-07-24 01:37:44 +00001577 if (isGuardVariableRef(store->getPointerOperand()))
1578 guard_stores.push_back(&inst);
1579 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001580
Sean Callananddb46ef2010-07-24 01:37:44 +00001581 InstrIterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001582
Sean Callananddb46ef2010-07-24 01:37:44 +00001583 for (iter = guard_loads.begin();
1584 iter != guard_loads.end();
1585 ++iter)
Sean Callanan79763a42011-05-23 21:40:23 +00001586 TurnGuardLoadIntoZero(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001587
Sean Callananddb46ef2010-07-24 01:37:44 +00001588 for (iter = guard_stores.begin();
1589 iter != guard_stores.end();
1590 ++iter)
1591 ExciseGuardStore(*iter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001592
Sean Callananddb46ef2010-07-24 01:37:44 +00001593 return true;
1594}
1595
Sean Callanan3989fb92011-01-27 01:07:04 +00001596// This function does not report errors; its callers are responsible.
Sean Callananafe16a72010-11-17 23:00:36 +00001597bool
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001598IRForTarget::UnfoldConstant(Constant *old_constant,
1599 FunctionValueCache &value_maker,
1600 FunctionValueCache &entry_instruction_finder)
Sean Callanan7618f4e2010-07-14 23:40:29 +00001601{
Greg Clayton5160ce52013-03-27 23:08:40 +00001602 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan7618f4e2010-07-14 23:40:29 +00001603
Sean Callanan2235f322010-08-11 03:57:18 +00001604 SmallVector<User*, 16> users;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001605
Sean Callanan2235f322010-08-11 03:57:18 +00001606 // We do this because the use list might change, invalidating our iterator.
1607 // Much better to keep a work list ourselves.
Ed Maste80e8cc62014-03-10 14:23:10 +00001608 for (llvm::User *u : old_constant->users())
1609 users.push_back(u);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001610
Johnny Chen44805302011-07-19 19:48:13 +00001611 for (size_t i = 0;
Sean Callanan2235f322010-08-11 03:57:18 +00001612 i < users.size();
1613 ++i)
1614 {
1615 User *user = users[i];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001616
Sean Callanan7618f4e2010-07-14 23:40:29 +00001617 if (Constant *constant = dyn_cast<Constant>(user))
1618 {
1619 // synthesize a new non-constant equivalent of the constant
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001620
Sean Callanan7618f4e2010-07-14 23:40:29 +00001621 if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
1622 {
1623 switch (constant_expr->getOpcode())
1624 {
1625 default:
1626 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001627 log->Printf("Unhandled constant expression type: \"%s\"", PrintValue(constant_expr).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00001628 return false;
1629 case Instruction::BitCast:
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001630 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001631 FunctionValueCache bit_cast_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
1632 // UnaryExpr
1633 // OperandList[0] is value
1634
1635 if (constant_expr->getOperand(0) != old_constant)
1636 return constant_expr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001637
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001638 return new BitCastInst(value_maker.GetValue(function),
1639 constant_expr->getType(),
1640 "",
1641 llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
1642 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001643
Sean Callanan0e016fe2013-07-15 23:31:47 +00001644 if (!UnfoldConstant(constant_expr, bit_cast_maker, entry_instruction_finder))
1645 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00001646 }
1647 break;
1648 case Instruction::GetElementPtr:
1649 {
1650 // GetElementPtrConstantExpr
1651 // OperandList[0] is base
1652 // OperandList[1]... are indices
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001653
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001654 FunctionValueCache get_element_pointer_maker ([&value_maker, &entry_instruction_finder, old_constant, constant_expr] (llvm::Function *function)->llvm::Value* {
1655 Value *ptr = constant_expr->getOperand(0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001656
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001657 if (ptr == old_constant)
1658 ptr = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001659
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001660 std::vector<Value*> index_vector;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001661
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001662 unsigned operand_index;
1663 unsigned num_operands = constant_expr->getNumOperands();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001664
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001665 for (operand_index = 1;
1666 operand_index < num_operands;
1667 ++operand_index)
1668 {
1669 Value *operand = constant_expr->getOperand(operand_index);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001670
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001671 if (operand == old_constant)
1672 operand = value_maker.GetValue(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001673
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001674 index_vector.push_back(operand);
1675 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001676
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001677 ArrayRef <Value*> indices(index_vector);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001678
Vince Harron0fc6c672015-03-16 03:54:22 +00001679 return GetElementPtrInst::Create(nullptr, ptr, indices, "", llvm::cast<Instruction>(entry_instruction_finder.GetValue(function)));
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001680 });
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001681
Sean Callanan0e016fe2013-07-15 23:31:47 +00001682 if (!UnfoldConstant(constant_expr, get_element_pointer_maker, entry_instruction_finder))
1683 return false;
Sean Callanan7618f4e2010-07-14 23:40:29 +00001684 }
1685 break;
1686 }
1687 }
1688 else
1689 {
1690 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001691 log->Printf("Unhandled constant type: \"%s\"", PrintValue(constant).c_str());
Sean Callanan7618f4e2010-07-14 23:40:29 +00001692 return false;
1693 }
1694 }
1695 else
1696 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001697 if (Instruction *inst = llvm::dyn_cast<Instruction>(user))
1698 {
1699 inst->replaceUsesOfWith(old_constant, value_maker.GetValue(inst->getParent()->getParent()));
1700 }
1701 else
1702 {
1703 if (log)
1704 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(user).c_str());
1705 return false;
1706 }
Sean Callanan7618f4e2010-07-14 23:40:29 +00001707 }
1708 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001709
Sean Callanan0e016fe2013-07-15 23:31:47 +00001710 if (!isa<GlobalValue>(old_constant))
1711 {
1712 old_constant->destroyConstant();
1713 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001714
Sean Callanan7618f4e2010-07-14 23:40:29 +00001715 return true;
1716}
1717
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001718bool
Sean Callanan79763a42011-05-23 21:40:23 +00001719IRForTarget::ReplaceVariables (Function &llvm_function)
Sean Callanan549c9f72010-07-13 21:41:46 +00001720{
Sean Callanan9e6ed532010-09-13 21:34:21 +00001721 if (!m_resolve_vars)
1722 return true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001723
Greg Clayton5160ce52013-03-27 23:08:40 +00001724 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan549c9f72010-07-13 21:41:46 +00001725
1726 m_decl_map->DoStructLayout();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001727
Sean Callanan549c9f72010-07-13 21:41:46 +00001728 if (log)
1729 log->Printf("Element arrangement:");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001730
Sean Callanan549c9f72010-07-13 21:41:46 +00001731 uint32_t num_elements;
1732 uint32_t element_index;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001733
Sean Callanan549c9f72010-07-13 21:41:46 +00001734 size_t size;
Zachary Turnera746e8e2014-07-02 17:24:07 +00001735 lldb::offset_t alignment;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001736
Sean Callanan549c9f72010-07-13 21:41:46 +00001737 if (!m_decl_map->GetStructInfo (num_elements, size, alignment))
1738 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001739
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001740 Function::arg_iterator iter(llvm_function.getArgumentList().begin());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001741
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001742 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00001743 {
1744 if (m_error_stream)
1745 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 +00001746
Sean Callanan549c9f72010-07-13 21:41:46 +00001747 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001748 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001749
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +00001750 Argument *argument = &*iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001751
Sean Callananfc55f5d2010-09-21 00:44:12 +00001752 if (argument->getName().equals("this"))
1753 {
1754 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001755
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001756 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00001757 {
1758 if (m_error_stream)
1759 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 +00001760
Sean Callananfc55f5d2010-09-21 00:44:12 +00001761 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001762 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001763
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +00001764 argument = &*iter;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001765 }
Sean Callanan17827832010-12-13 22:46:15 +00001766 else if (argument->getName().equals("self"))
1767 {
1768 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001769
Sean Callanan17827832010-12-13 22:46:15 +00001770 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00001771 {
1772 if (m_error_stream)
1773 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 +00001774
Sean Callanan17827832010-12-13 22:46:15 +00001775 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001776 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001777
Sean Callanan17827832010-12-13 22:46:15 +00001778 if (!iter->getName().equals("_cmd"))
Sean Callanan3989fb92011-01-27 01:07:04 +00001779 {
1780 if (m_error_stream)
1781 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 +00001782
Sean Callanan17827832010-12-13 22:46:15 +00001783 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001784 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001785
Sean Callanan17827832010-12-13 22:46:15 +00001786 ++iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001787
Sean Callanan17827832010-12-13 22:46:15 +00001788 if (iter == llvm_function.getArgumentList().end())
Sean Callanan3989fb92011-01-27 01:07:04 +00001789 {
1790 if (m_error_stream)
1791 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 +00001792
Sean Callanan17827832010-12-13 22:46:15 +00001793 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001794 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001795
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +00001796 argument = &*iter;
Sean Callanan17827832010-12-13 22:46:15 +00001797 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001798
Greg Clayton7b462cc2010-10-15 22:48:33 +00001799 if (!argument->getName().equals("$__lldb_arg"))
Sean Callanan3989fb92011-01-27 01:07:04 +00001800 {
1801 if (m_error_stream)
1802 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 +00001803
Sean Callanan549c9f72010-07-13 21:41:46 +00001804 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001805 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001806
Sean Callanan549c9f72010-07-13 21:41:46 +00001807 if (log)
Greg Claytoncb7e3b32010-11-15 01:47:11 +00001808 log->Printf("Arg: \"%s\"", PrintValue(argument).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001809
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001810 BasicBlock &entry_block(llvm_function.getEntryBlock());
Sean Callananafe16a72010-11-17 23:00:36 +00001811 Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001812
Sean Callananafe16a72010-11-17 23:00:36 +00001813 if (!FirstEntryInstruction)
Sean Callanan3989fb92011-01-27 01:07:04 +00001814 {
1815 if (m_error_stream)
1816 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 +00001817
Sean Callanan549c9f72010-07-13 21:41:46 +00001818 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001819 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001820
Sean Callanan79763a42011-05-23 21:40:23 +00001821 LLVMContext &context(m_module->getContext());
Sean Callanancc427fa2011-07-30 02:42:06 +00001822 IntegerType *offset_type(Type::getInt32Ty(context));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001823
Sean Callanan549c9f72010-07-13 21:41:46 +00001824 if (!offset_type)
Sean Callanan3989fb92011-01-27 01:07:04 +00001825 {
1826 if (m_error_stream)
1827 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't produce an offset type");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001828
Sean Callanan549c9f72010-07-13 21:41:46 +00001829 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001830 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001831
Sean Callanan549c9f72010-07-13 21:41:46 +00001832 for (element_index = 0; element_index < num_elements; ++element_index)
1833 {
Sean Callanan77eaf442011-07-08 00:39:14 +00001834 const clang::NamedDecl *decl = NULL;
1835 Value *value = NULL;
Zachary Turnera746e8e2014-07-02 17:24:07 +00001836 lldb::offset_t offset;
Greg Clayton7b462cc2010-10-15 22:48:33 +00001837 lldb_private::ConstString name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001838
Sean Callanan823bb4c2010-08-30 22:17:16 +00001839 if (!m_decl_map->GetStructElement (decl, value, offset, name, element_index))
Sean Callanan3989fb92011-01-27 01:07:04 +00001840 {
1841 if (m_error_stream)
1842 m_error_stream->Printf("Internal error [IRForTarget]: Structure information is incomplete");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001843
Sean Callanan549c9f72010-07-13 21:41:46 +00001844 return false;
Sean Callanan3989fb92011-01-27 01:07:04 +00001845 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001846
Sean Callanan549c9f72010-07-13 21:41:46 +00001847 if (log)
Zachary Turnera746e8e2014-07-02 17:24:07 +00001848 log->Printf(" \"%s\" (\"%s\") placed at %" PRIu64,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001849 name.GetCString(),
Sean Callananc70ed462011-10-25 18:36:40 +00001850 decl->getNameAsString().c_str(),
Sean Callanan549c9f72010-07-13 21:41:46 +00001851 offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001852
Sean Callananc70ed462011-10-25 18:36:40 +00001853 if (value)
Sean Callanan92adcac2011-01-13 08:53:35 +00001854 {
Sean Callananc70ed462011-10-25 18:36:40 +00001855 if (log)
1856 log->Printf(" Replacing [%s]", PrintValue(value).c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001857
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001858 FunctionValueCache body_result_maker ([this, name, offset_type, offset, argument, value] (llvm::Function *function)->llvm::Value * {
1859 // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult, in cases where the result
1860 // variable is an rvalue, we have to synthesize a dereference of the appropriate structure
1861 // entry in order to produce the static variable that the AST thinks it is accessing.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001862
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001863 llvm::Instruction *entry_instruction = llvm::cast<Instruction>(m_entry_instruction_finder.GetValue(function));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001864
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001865 ConstantInt *offset_int(ConstantInt::get(offset_type, offset, true));
Vince Harron0fc6c672015-03-16 03:54:22 +00001866 GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(nullptr,
1867 argument,
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001868 offset_int,
1869 "",
1870 entry_instruction);
1871
1872 if (name == m_result_name && !m_result_is_pointer)
1873 {
1874 BitCastInst *bit_cast = new BitCastInst(get_element_ptr,
1875 value->getType()->getPointerTo(),
1876 "",
1877 entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001878
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001879 LoadInst *load = new LoadInst(bit_cast, "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001880
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001881 return load;
1882 }
1883 else
1884 {
1885 BitCastInst *bit_cast = new BitCastInst(get_element_ptr, value->getType(), "", entry_instruction);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001886
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001887 return bit_cast;
1888 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001889 });
1890
Sean Callananc70ed462011-10-25 18:36:40 +00001891 if (Constant *constant = dyn_cast<Constant>(value))
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001892 {
1893 UnfoldConstant(constant, body_result_maker, m_entry_instruction_finder);
1894 }
1895 else if (Instruction *instruction = dyn_cast<Instruction>(value))
1896 {
1897 value->replaceAllUsesWith(body_result_maker.GetValue(instruction->getParent()->getParent()));
1898 }
Sean Callananc70ed462011-10-25 18:36:40 +00001899 else
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001900 {
1901 if (log)
1902 log->Printf("Unhandled non-constant type: \"%s\"", PrintValue(value).c_str());
1903 return false;
1904 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001905
Sean Callananc70ed462011-10-25 18:36:40 +00001906 if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
1907 var->eraseFromParent();
1908 }
Sean Callanan549c9f72010-07-13 21:41:46 +00001909 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001910
Sean Callanan549c9f72010-07-13 21:41:46 +00001911 if (log)
Greg Clayton6fea17e2014-03-03 19:15:20 +00001912 log->Printf("Total structure [align %" PRId64 ", size %" PRIu64 "]", (int64_t)alignment, (uint64_t)size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001913
Sean Callanan549c9f72010-07-13 21:41:46 +00001914 return true;
1915}
1916
Sean Callanan79763a42011-05-23 21:40:23 +00001917llvm::Constant *
Greg Clayton5160ce52013-03-27 23:08:40 +00001918IRForTarget::BuildRelocation(llvm::Type *type, uint64_t offset)
Sean Callanan79763a42011-05-23 21:40:23 +00001919{
Sean Callanan439dcae2013-12-20 19:55:02 +00001920 llvm::Constant *offset_int = ConstantInt::get(m_intptr_ty, offset);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001921
Sean Callanancc427fa2011-07-30 02:42:06 +00001922 llvm::Constant *offset_array[1];
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001923
Sean Callanancc427fa2011-07-30 02:42:06 +00001924 offset_array[0] = offset_int;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001925
Sean Callanancc427fa2011-07-30 02:42:06 +00001926 llvm::ArrayRef<llvm::Constant *> offsets(offset_array, 1);
Sean Callanan507b5882015-04-14 18:17:35 +00001927 llvm::Type *char_type = llvm::Type::getInt8Ty(m_module->getContext());
1928 llvm::Type *char_pointer_type = char_type->getPointerTo();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001929
Sean Callanan507b5882015-04-14 18:17:35 +00001930 llvm::Constant *reloc_placeholder_bitcast = ConstantExpr::getBitCast(m_reloc_placeholder, char_pointer_type);
1931 llvm::Constant *reloc_getelementptr = ConstantExpr::getGetElementPtr(char_type, reloc_placeholder_bitcast, offsets);
Sean Callanana85f0e82015-04-06 23:51:08 +00001932 llvm::Constant *reloc_bitcast = ConstantExpr::getBitCast(reloc_getelementptr, type);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001933
Sean Callanana85f0e82015-04-06 23:51:08 +00001934 return reloc_bitcast;
Sean Callanan79763a42011-05-23 21:40:23 +00001935}
1936
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001937bool
Greg Clayton1b95a6f2010-11-19 01:05:25 +00001938IRForTarget::runOnModule (Module &llvm_module)
Sean Callanan2ab712f22010-07-03 01:35:46 +00001939{
Greg Clayton5160ce52013-03-27 23:08:40 +00001940 lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001941
Sean Callanan79763a42011-05-23 21:40:23 +00001942 m_module = &llvm_module;
Micah Villmow8468dbe2012-10-08 16:28:57 +00001943 m_target_data.reset(new DataLayout(m_module));
Sean Callanan439dcae2013-12-20 19:55:02 +00001944 m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(), m_target_data->getPointerSizeInBits());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001945
Sean Callananc70ed462011-10-25 18:36:40 +00001946 if (log)
1947 {
1948 std::string s;
1949 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001950
Sean Callananc70ed462011-10-25 18:36:40 +00001951 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001952
Sean Callananc70ed462011-10-25 18:36:40 +00001953 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001954
Sean Callananc70ed462011-10-25 18:36:40 +00001955 log->Printf("Module as passed in to IRForTarget: \n\"%s\"", s.c_str());
1956 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001957
Sean Callanan00294b32016-03-22 21:05:51 +00001958 Function *const main_function = m_func_name.IsEmpty() ? nullptr : m_module->getFunction(m_func_name.GetStringRef());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001959
Sean Callanan00294b32016-03-22 21:05:51 +00001960 if (!m_func_name.IsEmpty() && !main_function)
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001961 {
1962 if (log)
Sean Callanan00294b32016-03-22 21:05:51 +00001963 log->Printf("Couldn't find \"%s()\" in the module", m_func_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001964
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001965 if (m_error_stream)
Sean Callanan00294b32016-03-22 21:05:51 +00001966 m_error_stream->Printf("Internal error [IRForTarget]: Couldn't find wrapper '%s' in the module",
1967 m_func_name.AsCString());
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001968
1969 return false;
1970 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001971
Sean Callanan00294b32016-03-22 21:05:51 +00001972 if (main_function)
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001973 {
Sean Callanan00294b32016-03-22 21:05:51 +00001974 if (!FixFunctionLinkage(*main_function))
1975 {
1976 if (log)
1977 log->Printf("Couldn't fix the linkage for the function");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001978
Sean Callanan00294b32016-03-22 21:05:51 +00001979 return false;
1980 }
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001981 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001982
Sean Callanan439dcae2013-12-20 19:55:02 +00001983 llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001984
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001985 m_reloc_placeholder = new llvm::GlobalVariable((*m_module),
Sean Callanan439dcae2013-12-20 19:55:02 +00001986 int8_ty,
Sean Callanan3d654b32012-09-24 22:25:51 +00001987 false /* IsConstant */,
Sean Callanan79763a42011-05-23 21:40:23 +00001988 GlobalVariable::InternalLinkage,
Sean Callanan439dcae2013-12-20 19:55:02 +00001989 Constant::getNullValue(int8_ty),
Sean Callanan79763a42011-05-23 21:40:23 +00001990 "reloc_placeholder",
1991 NULL /* InsertBefore */,
Sean Callanan3d654b32012-09-24 22:25:51 +00001992 GlobalVariable::NotThreadLocal /* ThreadLocal */,
Sean Callanan79763a42011-05-23 21:40:23 +00001993 0 /* AddressSpace */);
Sean Callanan1f9db3e2013-06-28 21:44:15 +00001994
Sean Callanand1e5b432010-08-12 01:56:52 +00001995 ////////////////////////////////////////////////////////////
Greg Clayton7b462cc2010-10-15 22:48:33 +00001996 // Replace $__lldb_expr_result with a persistent variable
Sean Callanand1e5b432010-08-12 01:56:52 +00001997 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001998
Sean Callanan00294b32016-03-22 21:05:51 +00001999 if (main_function)
Sean Callanan17827832010-12-13 22:46:15 +00002000 {
Sean Callanan00294b32016-03-22 21:05:51 +00002001 if (!CreateResultVariable(*main_function))
2002 {
2003 if (log)
2004 log->Printf("CreateResultVariable() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002005
Sean Callanan00294b32016-03-22 21:05:51 +00002006 // CreateResultVariable() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002007
Sean Callanan00294b32016-03-22 21:05:51 +00002008 return false;
2009 }
Sean Callanan17827832010-12-13 22:46:15 +00002010 }
Sean Callanan6e6d4a62012-07-21 02:02:15 +00002011
Sean Callananea685ae2011-11-01 17:33:54 +00002012 if (log && log->GetVerbose())
Sean Callanan79763a42011-05-23 21:40:23 +00002013 {
2014 std::string s;
2015 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002016
Sean Callanan79763a42011-05-23 21:40:23 +00002017 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002018
Sean Callanan79763a42011-05-23 21:40:23 +00002019 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002020
Sean Callanan79763a42011-05-23 21:40:23 +00002021 log->Printf("Module after creating the result variable: \n\"%s\"", s.c_str());
2022 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002023
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002024 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2025 fi != fe;
2026 ++fi)
2027 {
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +00002028 llvm::Function *function = &*fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002029
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002030 if (function->begin() == function->end())
2031 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002032
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002033 Function::iterator bbi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002034
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002035 for (bbi = function->begin();
2036 bbi != function->end();
2037 ++bbi)
2038 {
2039 if (!RemoveGuards(*bbi))
2040 {
2041 if (log)
2042 log->Printf("RemoveGuards() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002043
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002044 // RemoveGuards() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002045
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002046 return false;
2047 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002048
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002049 if (!RewritePersistentAllocs(*bbi))
2050 {
2051 if (log)
2052 log->Printf("RewritePersistentAllocs() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002053
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002054 // RewritePersistentAllocs() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002055
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002056 return false;
2057 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002058
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002059 if (!RemoveCXAAtExit(*bbi))
2060 {
2061 if (log)
2062 log->Printf("RemoveCXAAtExit() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002063
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002064 // RemoveCXAAtExit() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002065
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002066 return false;
2067 }
2068 }
2069 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002070
Sean Callananafe16a72010-11-17 23:00:36 +00002071 ///////////////////////////////////////////////////////////////////////////////
2072 // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
2073 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002074
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002075 if (!RewriteObjCConstStrings())
Sean Callanan17827832010-12-13 22:46:15 +00002076 {
2077 if (log)
2078 log->Printf("RewriteObjCConstStrings() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002079
Sean Callanan3989fb92011-01-27 01:07:04 +00002080 // RewriteObjCConstStrings() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002081
Sean Callananafe16a72010-11-17 23:00:36 +00002082 return false;
Sean Callanan17827832010-12-13 22:46:15 +00002083 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002084
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002085 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2086 fi != fe;
2087 ++fi)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002088 {
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +00002089 llvm::Function *function = &*fi;
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002090
2091 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2092 bbi != bbe;
2093 ++bbi)
Sean Callanan17827832010-12-13 22:46:15 +00002094 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002095 if (!RewriteObjCSelectors(*bbi))
2096 {
2097 if (log)
2098 log->Printf("RewriteObjCSelectors() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002099
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002100 // RewriteObjCSelectors() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002101
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002102 return false;
2103 }
Sean Callanan17827832010-12-13 22:46:15 +00002104 }
Sean Callananbad134f2012-07-27 19:25:24 +00002105 }
Sean Callanan2235f322010-08-11 03:57:18 +00002106
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002107 for (Module::iterator fi = m_module->begin(), fe = m_module->end();
2108 fi != fe;
2109 ++fi)
Sean Callananbad134f2012-07-27 19:25:24 +00002110 {
Duncan P. N. Exon Smith33e43ca2015-11-07 00:54:13 +00002111 llvm::Function *function = &*fi;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002112
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002113 for (llvm::Function::iterator bbi = function->begin(), bbe = function->end();
2114 bbi != bbe;
2115 ++bbi)
Sean Callanan79763a42011-05-23 21:40:23 +00002116 {
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002117 if (!ResolveCalls(*bbi))
2118 {
2119 if (log)
2120 log->Printf("ResolveCalls() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002121
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002122 // ResolveCalls() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002123
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002124 return false;
2125 }
Sean Callanan79763a42011-05-23 21:40:23 +00002126 }
Sean Callanan549c9f72010-07-13 21:41:46 +00002127 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002128
Sean Callanan1f9db3e2013-06-28 21:44:15 +00002129 ////////////////////////////////////////////////////////////////////////
2130 // Run function-level passes that only make sense on the main function
Sean Callanan038df5032010-09-30 21:18:25 +00002131 //
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002132
Sean Callanan00294b32016-03-22 21:05:51 +00002133 if (main_function)
Sean Callanan17827832010-12-13 22:46:15 +00002134 {
Sean Callanan00294b32016-03-22 21:05:51 +00002135 if (!ResolveExternals(*main_function))
2136 {
2137 if (log)
2138 log->Printf("ResolveExternals() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002139
Sean Callanan00294b32016-03-22 21:05:51 +00002140 // ResolveExternals() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002141
Sean Callanan00294b32016-03-22 21:05:51 +00002142 return false;
2143 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002144
Sean Callanan00294b32016-03-22 21:05:51 +00002145 if (!ReplaceVariables(*main_function))
2146 {
2147 if (log)
2148 log->Printf("ReplaceVariables() failed");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002149
Sean Callanan00294b32016-03-22 21:05:51 +00002150 // ReplaceVariables() reports its own errors, so we don't do so here
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002151
Sean Callanan00294b32016-03-22 21:05:51 +00002152 return false;
2153 }
Sean Callanan17827832010-12-13 22:46:15 +00002154 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002155
Sean Callananea685ae2011-11-01 17:33:54 +00002156 if (log && log->GetVerbose())
Sean Callanan549c9f72010-07-13 21:41:46 +00002157 {
Sean Callanancc54bd32010-07-28 01:00:59 +00002158 std::string s;
2159 raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002160
Sean Callanan79763a42011-05-23 21:40:23 +00002161 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002162
Sean Callanancc54bd32010-07-28 01:00:59 +00002163 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002164
Greg Claytoncb7e3b32010-11-15 01:47:11 +00002165 log->Printf("Module after preparing for execution: \n\"%s\"", s.c_str());
Sean Callanan2ab712f22010-07-03 01:35:46 +00002166 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00002167
2168 return true;
Sean Callanan2ab712f22010-07-03 01:35:46 +00002169}
2170
2171void
Greg Clayton1b95a6f2010-11-19 01:05:25 +00002172IRForTarget::assignPassManager (PMStack &pass_mgr_stack, PassManagerType pass_mgr_type)
Sean Callanan2ab712f22010-07-03 01:35:46 +00002173{
2174}
2175
2176PassManagerType
2177IRForTarget::getPotentialPassManagerType() const
2178{
2179 return PMT_ModulePassManager;
2180}