1254721Semaste//===-- ClangExpressionParser.cpp -------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#include "lldb/lldb-python.h" 11254721Semaste 12254721Semaste#include "lldb/Expression/ClangExpressionParser.h" 13254721Semaste 14254721Semaste#include "lldb/Core/ArchSpec.h" 15254721Semaste#include "lldb/Core/DataBufferHeap.h" 16254721Semaste#include "lldb/Core/Debugger.h" 17254721Semaste#include "lldb/Core/Disassembler.h" 18254721Semaste#include "lldb/Core/Stream.h" 19269024Semaste#include "lldb/Core/StreamFile.h" 20254721Semaste#include "lldb/Core/StreamString.h" 21254721Semaste#include "lldb/Expression/ClangASTSource.h" 22254721Semaste#include "lldb/Expression/ClangExpression.h" 23254721Semaste#include "lldb/Expression/ClangExpressionDeclMap.h" 24254721Semaste#include "lldb/Expression/IRExecutionUnit.h" 25254721Semaste#include "lldb/Expression/IRDynamicChecks.h" 26254721Semaste#include "lldb/Expression/IRInterpreter.h" 27254721Semaste#include "lldb/Target/ExecutionContext.h" 28254721Semaste#include "lldb/Target/ObjCLanguageRuntime.h" 29254721Semaste#include "lldb/Target/Process.h" 30254721Semaste#include "lldb/Target/Target.h" 31254721Semaste 32254721Semaste#include "clang/AST/ASTContext.h" 33254721Semaste#include "clang/AST/ExternalASTSource.h" 34254721Semaste#include "clang/Basic/FileManager.h" 35254721Semaste#include "clang/Basic/TargetInfo.h" 36254721Semaste#include "clang/Basic/Version.h" 37254721Semaste#include "clang/CodeGen/CodeGenAction.h" 38254721Semaste#include "clang/CodeGen/ModuleBuilder.h" 39254721Semaste#include "clang/Frontend/CompilerInstance.h" 40254721Semaste#include "clang/Frontend/CompilerInvocation.h" 41254721Semaste#include "clang/Frontend/FrontendActions.h" 42254721Semaste#include "clang/Frontend/FrontendDiagnostic.h" 43254721Semaste#include "clang/Frontend/FrontendPluginRegistry.h" 44254721Semaste#include "clang/Frontend/TextDiagnosticBuffer.h" 45254721Semaste#include "clang/Frontend/TextDiagnosticPrinter.h" 46254721Semaste#include "clang/Lex/Preprocessor.h" 47254721Semaste#include "clang/Parse/ParseAST.h" 48254721Semaste#include "clang/Rewrite/Frontend/FrontendActions.h" 49254721Semaste#include "clang/Sema/SemaConsumer.h" 50254721Semaste#include "clang/StaticAnalyzer/Frontend/FrontendActions.h" 51254721Semaste 52254721Semaste#include "llvm/ADT/StringRef.h" 53254721Semaste#include "llvm/ExecutionEngine/ExecutionEngine.h" 54254721Semaste#include "llvm/Support/Debug.h" 55263508Sdim#include "llvm/Support/FileSystem.h" 56254721Semaste#include "llvm/Support/TargetSelect.h" 57254721Semaste 58254721Semaste#if defined (USE_STANDARD_JIT) 59254721Semaste#include "llvm/ExecutionEngine/JIT.h" 60254721Semaste#else 61254721Semaste#include "llvm/ExecutionEngine/MCJIT.h" 62254721Semaste#endif 63254721Semaste#include "llvm/IR/LLVMContext.h" 64254721Semaste#include "llvm/IR/Module.h" 65254721Semaste#include "llvm/Support/ErrorHandling.h" 66254721Semaste#include "llvm/Support/MemoryBuffer.h" 67254721Semaste#include "llvm/Support/DynamicLibrary.h" 68254721Semaste#include "llvm/Support/Host.h" 69254721Semaste#include "llvm/Support/Signals.h" 70254721Semaste 71254721Semasteusing namespace clang; 72254721Semasteusing namespace llvm; 73254721Semasteusing namespace lldb_private; 74254721Semaste 75254721Semaste//===----------------------------------------------------------------------===// 76254721Semaste// Utility Methods for Clang 77254721Semaste//===----------------------------------------------------------------------===// 78254721Semaste 79254721Semastestd::string GetBuiltinIncludePath(const char *Argv0) { 80263508Sdim SmallString<128> P(llvm::sys::fs::getMainExecutable( 81263508Sdim Argv0, (void *)(intptr_t) GetBuiltinIncludePath)); 82263508Sdim 83263508Sdim if (!P.empty()) { 84263508Sdim llvm::sys::path::remove_filename(P); // Remove /clang from foo/bin/clang 85263508Sdim llvm::sys::path::remove_filename(P); // Remove /bin from foo/bin 86263508Sdim 87254721Semaste // Get foo/lib/clang/<version>/include 88263508Sdim llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING, 89263508Sdim "include"); 90254721Semaste } 91254721Semaste 92254721Semaste return P.str(); 93254721Semaste} 94254721Semaste 95254721Semaste 96254721Semaste//===----------------------------------------------------------------------===// 97254721Semaste// Main driver for Clang 98254721Semaste//===----------------------------------------------------------------------===// 99254721Semaste 100254721Semastestatic void LLVMErrorHandler(void *UserData, const std::string &Message) { 101254721Semaste DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); 102254721Semaste 103254721Semaste Diags.Report(diag::err_fe_error_backend) << Message; 104254721Semaste 105254721Semaste // We cannot recover from llvm errors. 106254721Semaste assert(0); 107254721Semaste} 108254721Semaste 109254721Semastestatic FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { 110254721Semaste using namespace clang::frontend; 111254721Semaste 112254721Semaste switch (CI.getFrontendOpts().ProgramAction) { 113254721Semaste default: 114254721Semaste llvm_unreachable("Invalid program action!"); 115254721Semaste 116254721Semaste case ASTDump: return new ASTDumpAction(); 117254721Semaste case ASTPrint: return new ASTPrintAction(); 118254721Semaste case ASTView: return new ASTViewAction(); 119254721Semaste case DumpRawTokens: return new DumpRawTokensAction(); 120254721Semaste case DumpTokens: return new DumpTokensAction(); 121254721Semaste case EmitAssembly: return new EmitAssemblyAction(); 122254721Semaste case EmitBC: return new EmitBCAction(); 123254721Semaste case EmitHTML: return new HTMLPrintAction(); 124254721Semaste case EmitLLVM: return new EmitLLVMAction(); 125254721Semaste case EmitLLVMOnly: return new EmitLLVMOnlyAction(); 126254721Semaste case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); 127254721Semaste case EmitObj: return new EmitObjAction(); 128254721Semaste case FixIt: return new FixItAction(); 129254721Semaste case GeneratePCH: return new GeneratePCHAction(); 130254721Semaste case GeneratePTH: return new GeneratePTHAction(); 131254721Semaste case InitOnly: return new InitOnlyAction(); 132254721Semaste case ParseSyntaxOnly: return new SyntaxOnlyAction(); 133254721Semaste 134254721Semaste case PluginAction: { 135254721Semaste for (FrontendPluginRegistry::iterator it = 136254721Semaste FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); 137254721Semaste it != ie; ++it) { 138254721Semaste if (it->getName() == CI.getFrontendOpts().ActionName) { 139254721Semaste llvm::OwningPtr<PluginASTAction> P(it->instantiate()); 140254721Semaste if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) 141254721Semaste return 0; 142254721Semaste return P.take(); 143254721Semaste } 144254721Semaste } 145254721Semaste 146254721Semaste CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) 147254721Semaste << CI.getFrontendOpts().ActionName; 148254721Semaste return 0; 149254721Semaste } 150254721Semaste 151254721Semaste case PrintDeclContext: return new DeclContextPrintAction(); 152254721Semaste case PrintPreamble: return new PrintPreambleAction(); 153254721Semaste case PrintPreprocessedInput: return new PrintPreprocessedAction(); 154254721Semaste case RewriteMacros: return new RewriteMacrosAction(); 155254721Semaste case RewriteObjC: return new RewriteObjCAction(); 156254721Semaste case RewriteTest: return new RewriteTestAction(); 157254721Semaste //case RunAnalysis: return new AnalysisAction(); 158254721Semaste case RunPreprocessorOnly: return new PreprocessOnlyAction(); 159254721Semaste } 160254721Semaste} 161254721Semaste 162254721Semastestatic FrontendAction *CreateFrontendAction(CompilerInstance &CI) { 163254721Semaste // Create the underlying action. 164254721Semaste FrontendAction *Act = CreateFrontendBaseAction(CI); 165254721Semaste if (!Act) 166254721Semaste return 0; 167254721Semaste 168254721Semaste // If there are any AST files to merge, create a frontend action 169254721Semaste // adaptor to perform the merge. 170254721Semaste if (!CI.getFrontendOpts().ASTMergeFiles.empty()) 171254721Semaste Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles); 172254721Semaste 173254721Semaste return Act; 174254721Semaste} 175254721Semaste 176254721Semaste//===----------------------------------------------------------------------===// 177254721Semaste// Implementation of ClangExpressionParser 178254721Semaste//===----------------------------------------------------------------------===// 179254721Semaste 180254721SemasteClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope, 181254721Semaste ClangExpression &expr) : 182254721Semaste m_expr (expr), 183254721Semaste m_compiler (), 184254721Semaste m_code_generator () 185254721Semaste{ 186254721Semaste // Initialize targets first, so that --version shows registered targets. 187254721Semaste static struct InitializeLLVM { 188254721Semaste InitializeLLVM() { 189254721Semaste llvm::InitializeAllTargets(); 190254721Semaste llvm::InitializeAllAsmPrinters(); 191254721Semaste llvm::InitializeAllTargetMCs(); 192254721Semaste llvm::InitializeAllDisassemblers(); 193254721Semaste } 194254721Semaste } InitializeLLVM; 195254721Semaste 196254721Semaste // 1. Create a new compiler instance. 197254721Semaste m_compiler.reset(new CompilerInstance()); 198254721Semaste 199254721Semaste // 2. Install the target. 200254721Semaste 201254721Semaste lldb::TargetSP target_sp; 202254721Semaste if (exe_scope) 203254721Semaste target_sp = exe_scope->CalculateTarget(); 204254721Semaste 205254721Semaste // TODO: figure out what to really do when we don't have a valid target. 206254721Semaste // Sometimes this will be ok to just use the host target triple (when we 207254721Semaste // evaluate say "2+3", but other expressions like breakpoint conditions 208254721Semaste // and other things that _are_ target specific really shouldn't just be 209254721Semaste // using the host triple. This needs to be fixed in a better way. 210254721Semaste if (target_sp && target_sp->GetArchitecture().IsValid()) 211254721Semaste { 212254721Semaste std::string triple = target_sp->GetArchitecture().GetTriple().str(); 213254721Semaste 214254721Semaste int dash_count = 0; 215254721Semaste for (size_t i = 0; i < triple.size(); ++i) 216254721Semaste { 217254721Semaste if (triple[i] == '-') 218254721Semaste dash_count++; 219254721Semaste if (dash_count == 3) 220254721Semaste { 221254721Semaste triple.resize(i); 222254721Semaste break; 223254721Semaste } 224254721Semaste } 225254721Semaste 226254721Semaste m_compiler->getTargetOpts().Triple = triple; 227254721Semaste } 228254721Semaste else 229254721Semaste { 230254721Semaste m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple(); 231254721Semaste } 232254721Semaste 233254721Semaste if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 || 234254721Semaste target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64) 235254721Semaste { 236254721Semaste m_compiler->getTargetOpts().Features.push_back("+sse"); 237254721Semaste m_compiler->getTargetOpts().Features.push_back("+sse2"); 238254721Semaste } 239254721Semaste 240254721Semaste if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) 241254721Semaste m_compiler->getTargetOpts().ABI = "apcs-gnu"; 242254721Semaste 243254721Semaste m_compiler->createDiagnostics(); 244254721Semaste 245254721Semaste // Create the target instance. 246254721Semaste m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(), 247254721Semaste &m_compiler->getTargetOpts())); 248254721Semaste 249254721Semaste assert (m_compiler->hasTarget()); 250254721Semaste 251254721Semaste // 3. Set options. 252254721Semaste 253254721Semaste lldb::LanguageType language = expr.Language(); 254254721Semaste 255254721Semaste switch (language) 256254721Semaste { 257254721Semaste case lldb::eLanguageTypeC: 258254721Semaste break; 259254721Semaste case lldb::eLanguageTypeObjC: 260254721Semaste m_compiler->getLangOpts().ObjC1 = true; 261254721Semaste m_compiler->getLangOpts().ObjC2 = true; 262254721Semaste break; 263254721Semaste case lldb::eLanguageTypeC_plus_plus: 264254721Semaste m_compiler->getLangOpts().CPlusPlus = true; 265254721Semaste m_compiler->getLangOpts().CPlusPlus11 = true; 266254721Semaste break; 267254721Semaste case lldb::eLanguageTypeObjC_plus_plus: 268254721Semaste default: 269254721Semaste m_compiler->getLangOpts().ObjC1 = true; 270254721Semaste m_compiler->getLangOpts().ObjC2 = true; 271254721Semaste m_compiler->getLangOpts().CPlusPlus = true; 272254721Semaste m_compiler->getLangOpts().CPlusPlus11 = true; 273254721Semaste break; 274254721Semaste } 275254721Semaste 276254721Semaste m_compiler->getLangOpts().Bool = true; 277254721Semaste m_compiler->getLangOpts().WChar = true; 278254721Semaste m_compiler->getLangOpts().Blocks = true; 279254721Semaste m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients 280254721Semaste if (expr.DesiredResultType() == ClangExpression::eResultTypeId) 281254721Semaste m_compiler->getLangOpts().DebuggerCastResultToId = true; 282254721Semaste 283254721Semaste // Spell checking is a nice feature, but it ends up completing a 284254721Semaste // lot of types that we didn't strictly speaking need to complete. 285254721Semaste // As a result, we spend a long time parsing and importing debug 286254721Semaste // information. 287254721Semaste m_compiler->getLangOpts().SpellChecking = false; 288254721Semaste 289254721Semaste lldb::ProcessSP process_sp; 290254721Semaste if (exe_scope) 291254721Semaste process_sp = exe_scope->CalculateProcess(); 292254721Semaste 293254721Semaste if (process_sp && m_compiler->getLangOpts().ObjC1) 294254721Semaste { 295254721Semaste if (process_sp->GetObjCLanguageRuntime()) 296254721Semaste { 297254721Semaste if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2) 298254721Semaste m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7)); 299254721Semaste else 300254721Semaste m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7)); 301254721Semaste 302254721Semaste if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing()) 303254721Semaste m_compiler->getLangOpts().DebuggerObjCLiteral = true; 304254721Semaste } 305254721Semaste } 306254721Semaste 307254721Semaste m_compiler->getLangOpts().ThreadsafeStatics = false; 308254721Semaste m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access 309254721Semaste m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name 310254721Semaste 311254721Semaste // Set CodeGen options 312254721Semaste m_compiler->getCodeGenOpts().EmitDeclMetadata = true; 313254721Semaste m_compiler->getCodeGenOpts().InstrumentFunctions = false; 314263363Semaste m_compiler->getCodeGenOpts().DisableFPElim = true; 315263363Semaste m_compiler->getCodeGenOpts().OmitLeafFramePointer = false; 316254721Semaste 317254721Semaste // Disable some warnings. 318254721Semaste m_compiler->getDiagnostics().setDiagnosticGroupMapping("unused-value", clang::diag::MAP_IGNORE, SourceLocation()); 319254721Semaste m_compiler->getDiagnostics().setDiagnosticGroupMapping("odr", clang::diag::MAP_IGNORE, SourceLocation()); 320254721Semaste 321254721Semaste // Inform the target of the language options 322254721Semaste // 323254721Semaste // FIXME: We shouldn't need to do this, the target should be immutable once 324254721Semaste // created. This complexity should be lifted elsewhere. 325254721Semaste m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts()); 326254721Semaste 327254721Semaste // 4. Set up the diagnostic buffer for reporting errors 328254721Semaste 329254721Semaste m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer); 330254721Semaste 331254721Semaste // 5. Set up the source management objects inside the compiler 332254721Semaste 333254721Semaste clang::FileSystemOptions file_system_options; 334254721Semaste m_file_manager.reset(new clang::FileManager(file_system_options)); 335254721Semaste 336254721Semaste if (!m_compiler->hasSourceManager()) 337254721Semaste m_compiler->createSourceManager(*m_file_manager.get()); 338254721Semaste 339254721Semaste m_compiler->createFileManager(); 340254721Semaste m_compiler->createPreprocessor(); 341254721Semaste 342254721Semaste // 6. Most of this we get from the CompilerInstance, but we 343254721Semaste // also want to give the context an ExternalASTSource. 344254721Semaste m_selector_table.reset(new SelectorTable()); 345254721Semaste m_builtin_context.reset(new Builtin::Context()); 346254721Semaste 347254721Semaste std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(), 348254721Semaste m_compiler->getSourceManager(), 349254721Semaste &m_compiler->getTarget(), 350254721Semaste m_compiler->getPreprocessor().getIdentifierTable(), 351254721Semaste *m_selector_table.get(), 352254721Semaste *m_builtin_context.get(), 353254721Semaste 0)); 354254721Semaste 355254721Semaste ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); 356254721Semaste 357254721Semaste if (decl_map) 358254721Semaste { 359254721Semaste llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy()); 360254721Semaste decl_map->InstallASTContext(ast_context.get()); 361254721Semaste ast_context->setExternalSource(ast_source); 362254721Semaste } 363254721Semaste 364254721Semaste m_compiler->setASTContext(ast_context.release()); 365254721Semaste 366254721Semaste std::string module_name("$__lldb_module"); 367254721Semaste 368254721Semaste m_llvm_context.reset(new LLVMContext()); 369254721Semaste m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(), 370254721Semaste module_name, 371254721Semaste m_compiler->getCodeGenOpts(), 372254721Semaste m_compiler->getTargetOpts(), 373254721Semaste *m_llvm_context)); 374254721Semaste} 375254721Semaste 376254721SemasteClangExpressionParser::~ClangExpressionParser() 377254721Semaste{ 378254721Semaste} 379254721Semaste 380254721Semasteunsigned 381254721SemasteClangExpressionParser::Parse (Stream &stream) 382254721Semaste{ 383254721Semaste TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient()); 384254721Semaste 385254721Semaste diag_buf->FlushDiagnostics (m_compiler->getDiagnostics()); 386254721Semaste 387254721Semaste MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__); 388254721Semaste m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer); 389254721Semaste 390254721Semaste diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor()); 391254721Semaste 392254721Semaste ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get()); 393254721Semaste 394254721Semaste if (ast_transformer) 395254721Semaste ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext()); 396254721Semaste else 397254721Semaste ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext()); 398254721Semaste 399254721Semaste diag_buf->EndSourceFile(); 400254721Semaste 401254721Semaste TextDiagnosticBuffer::const_iterator diag_iterator; 402254721Semaste 403254721Semaste int num_errors = 0; 404254721Semaste 405254721Semaste for (diag_iterator = diag_buf->warn_begin(); 406254721Semaste diag_iterator != diag_buf->warn_end(); 407254721Semaste ++diag_iterator) 408254721Semaste stream.Printf("warning: %s\n", (*diag_iterator).second.c_str()); 409254721Semaste 410254721Semaste num_errors = 0; 411254721Semaste 412254721Semaste for (diag_iterator = diag_buf->err_begin(); 413254721Semaste diag_iterator != diag_buf->err_end(); 414254721Semaste ++diag_iterator) 415254721Semaste { 416254721Semaste num_errors++; 417254721Semaste stream.Printf("error: %s\n", (*diag_iterator).second.c_str()); 418254721Semaste } 419254721Semaste 420254721Semaste for (diag_iterator = diag_buf->note_begin(); 421254721Semaste diag_iterator != diag_buf->note_end(); 422254721Semaste ++diag_iterator) 423254721Semaste stream.Printf("note: %s\n", (*diag_iterator).second.c_str()); 424254721Semaste 425254721Semaste if (!num_errors) 426254721Semaste { 427254721Semaste if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes()) 428254721Semaste { 429254721Semaste stream.Printf("error: Couldn't infer the type of a variable\n"); 430254721Semaste num_errors++; 431254721Semaste } 432254721Semaste } 433254721Semaste 434254721Semaste return num_errors; 435254721Semaste} 436254721Semaste 437254721Semastestatic bool FindFunctionInModule (ConstString &mangled_name, 438254721Semaste llvm::Module *module, 439254721Semaste const char *orig_name) 440254721Semaste{ 441254721Semaste for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end(); 442254721Semaste fi != fe; 443254721Semaste ++fi) 444254721Semaste { 445254721Semaste if (fi->getName().str().find(orig_name) != std::string::npos) 446254721Semaste { 447254721Semaste mangled_name.SetCString(fi->getName().str().c_str()); 448254721Semaste return true; 449254721Semaste } 450254721Semaste } 451254721Semaste 452254721Semaste return false; 453254721Semaste} 454254721Semaste 455254721SemasteError 456254721SemasteClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr, 457254721Semaste lldb::addr_t &func_end, 458254721Semaste std::unique_ptr<IRExecutionUnit> &execution_unit_ap, 459254721Semaste ExecutionContext &exe_ctx, 460254721Semaste bool &can_interpret, 461254721Semaste ExecutionPolicy execution_policy) 462254721Semaste{ 463254721Semaste func_addr = LLDB_INVALID_ADDRESS; 464254721Semaste func_end = LLDB_INVALID_ADDRESS; 465254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 466254721Semaste 467254721Semaste std::unique_ptr<llvm::ExecutionEngine> execution_engine_ap; 468254721Semaste 469254721Semaste Error err; 470254721Semaste 471254721Semaste std::unique_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule()); 472254721Semaste 473254721Semaste if (!module_ap.get()) 474254721Semaste { 475254721Semaste err.SetErrorToGenericError(); 476254721Semaste err.SetErrorString("IR doesn't contain a module"); 477254721Semaste return err; 478254721Semaste } 479254721Semaste 480254721Semaste // Find the actual name of the function (it's often mangled somehow) 481254721Semaste 482254721Semaste ConstString function_name; 483254721Semaste 484254721Semaste if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName())) 485254721Semaste { 486254721Semaste err.SetErrorToGenericError(); 487254721Semaste err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName()); 488254721Semaste return err; 489254721Semaste } 490254721Semaste else 491254721Semaste { 492254721Semaste if (log) 493254721Semaste log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName()); 494254721Semaste } 495254721Semaste 496254721Semaste m_execution_unit.reset(new IRExecutionUnit(m_llvm_context, // handed off here 497254721Semaste module_ap, // handed off here 498254721Semaste function_name, 499254721Semaste exe_ctx.GetTargetSP(), 500254721Semaste m_compiler->getTargetOpts().Features)); 501254721Semaste 502254721Semaste ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL 503254721Semaste 504254721Semaste if (decl_map) 505254721Semaste { 506254721Semaste Stream *error_stream = NULL; 507254721Semaste Target *target = exe_ctx.GetTargetPtr(); 508254721Semaste if (target) 509269024Semaste error_stream = target->GetDebugger().GetErrorFile().get(); 510254721Semaste 511254721Semaste IRForTarget ir_for_target(decl_map, 512254721Semaste m_expr.NeedsVariableResolution(), 513254721Semaste *m_execution_unit, 514254721Semaste error_stream, 515254721Semaste function_name.AsCString()); 516254721Semaste 517254721Semaste bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule()); 518254721Semaste 519254721Semaste Error interpret_error; 520254721Semaste 521254721Semaste can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error); 522254721Semaste 523254721Semaste Process *process = exe_ctx.GetProcessPtr(); 524254721Semaste 525254721Semaste if (!ir_can_run) 526254721Semaste { 527254721Semaste err.SetErrorString("The expression could not be prepared to run in the target"); 528254721Semaste return err; 529254721Semaste } 530254721Semaste 531254721Semaste if (!can_interpret && execution_policy == eExecutionPolicyNever) 532254721Semaste { 533254721Semaste err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString()); 534254721Semaste return err; 535254721Semaste } 536254721Semaste 537254721Semaste if (!process && execution_policy == eExecutionPolicyAlways) 538254721Semaste { 539254721Semaste err.SetErrorString("Expression needed to run in the target, but the target can't be run"); 540254721Semaste return err; 541254721Semaste } 542254721Semaste 543254721Semaste if (execution_policy == eExecutionPolicyAlways || !can_interpret) 544254721Semaste { 545254721Semaste if (m_expr.NeedsValidation() && process) 546254721Semaste { 547254721Semaste if (!process->GetDynamicCheckers()) 548254721Semaste { 549254721Semaste DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions(); 550254721Semaste 551254721Semaste StreamString install_errors; 552254721Semaste 553254721Semaste if (!dynamic_checkers->Install(install_errors, exe_ctx)) 554254721Semaste { 555254721Semaste if (install_errors.GetString().empty()) 556254721Semaste err.SetErrorString ("couldn't install checkers, unknown error"); 557254721Semaste else 558254721Semaste err.SetErrorString (install_errors.GetString().c_str()); 559254721Semaste 560254721Semaste return err; 561254721Semaste } 562254721Semaste 563254721Semaste process->SetDynamicCheckers(dynamic_checkers); 564254721Semaste 565254721Semaste if (log) 566254721Semaste log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers =="); 567254721Semaste } 568254721Semaste 569254721Semaste IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString()); 570254721Semaste 571254721Semaste if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule())) 572254721Semaste { 573254721Semaste err.SetErrorToGenericError(); 574254721Semaste err.SetErrorString("Couldn't add dynamic checks to the expression"); 575254721Semaste return err; 576254721Semaste } 577254721Semaste } 578254721Semaste 579254721Semaste m_execution_unit->GetRunnableInfo(err, func_addr, func_end); 580254721Semaste } 581254721Semaste } 582254721Semaste else 583254721Semaste { 584254721Semaste m_execution_unit->GetRunnableInfo(err, func_addr, func_end); 585254721Semaste } 586254721Semaste 587254721Semaste execution_unit_ap.reset (m_execution_unit.release()); 588254721Semaste 589254721Semaste return err; 590254721Semaste} 591