Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionParser.cpp -------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Expression/ClangExpressionParser.h" |
| 11 | |
| 12 | #include "lldb/Core/ArchSpec.h" |
| 13 | #include "lldb/Core/DataBufferHeap.h" |
Sean Callanan | 97c924e | 2011-01-27 01:07:04 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Debugger.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Disassembler.h" |
| 16 | #include "lldb/Core/Stream.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StreamString.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 18 | #include "lldb/Expression/ClangASTSource.h" |
| 19 | #include "lldb/Expression/ClangExpression.h" |
Sean Callanan | fb3058e | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 20 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 21 | #include "lldb/Expression/IRDynamicChecks.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/RecordingMemoryManager.h" |
| 23 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 24 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Process.h" |
| 26 | #include "lldb/Target/Target.h" |
| 27 | |
| 28 | #include "clang/AST/ASTContext.h" |
| 29 | #include "clang/AST/ExternalASTSource.h" |
| 30 | #include "clang/Basic/FileManager.h" |
| 31 | #include "clang/Basic/TargetInfo.h" |
| 32 | #include "clang/Basic/Version.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 33 | #include "clang/CodeGen/CodeGenAction.h" |
| 34 | #include "clang/CodeGen/ModuleBuilder.h" |
| 35 | #include "clang/Driver/CC1Options.h" |
| 36 | #include "clang/Driver/OptTable.h" |
| 37 | #include "clang/Frontend/CompilerInstance.h" |
| 38 | #include "clang/Frontend/CompilerInvocation.h" |
| 39 | #include "clang/Frontend/FrontendActions.h" |
| 40 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 41 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 42 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 43 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 44 | #include "clang/Lex/Preprocessor.h" |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 45 | #include "clang/Parse/ParseAST.h" |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 46 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 47 | #include "clang/Sema/SemaConsumer.h" |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 48 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 49 | |
| 50 | #include "llvm/ADT/StringRef.h" |
| 51 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 52 | #include "llvm/Support/Debug.h" |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 53 | #include "llvm/Support/TargetSelect.h" |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 54 | |
Peter Collingbourne | c3f5573 | 2011-06-03 20:40:12 +0000 | [diff] [blame] | 55 | #if !defined(__APPLE__) |
| 56 | #define USE_STANDARD_JIT |
| 57 | #endif |
| 58 | |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 59 | #if defined (USE_STANDARD_JIT) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 60 | #include "llvm/ExecutionEngine/JIT.h" |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 61 | #else |
| 62 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 63 | #endif |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 64 | #include "llvm/LLVMContext.h" |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 65 | #include "llvm/Module.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 66 | #include "llvm/Support/ErrorHandling.h" |
| 67 | #include "llvm/Support/MemoryBuffer.h" |
Greg Clayton | 22defe8 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 68 | #include "llvm/Support/DynamicLibrary.h" |
| 69 | #include "llvm/Support/Host.h" |
| 70 | #include "llvm/Support/Signals.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 71 | |
| 72 | using namespace clang; |
| 73 | using namespace llvm; |
| 74 | using namespace lldb_private; |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // Utility Methods for Clang |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| 80 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 81 | llvm::sys::Path P = |
| 82 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 83 | (void*)(intptr_t) GetBuiltinIncludePath); |
| 84 | |
| 85 | if (!P.isEmpty()) { |
| 86 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 87 | P.eraseComponent(); // Remove /bin from foo/bin |
| 88 | |
| 89 | // Get foo/lib/clang/<version>/include |
| 90 | P.appendComponent("lib"); |
| 91 | P.appendComponent("clang"); |
| 92 | P.appendComponent(CLANG_VERSION_STRING); |
| 93 | P.appendComponent("include"); |
| 94 | } |
| 95 | |
| 96 | return P.str(); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | // Main driver for Clang |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
| 104 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 105 | DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 106 | |
| 107 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 108 | |
| 109 | // We cannot recover from llvm errors. |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 110 | assert(0); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { |
| 114 | using namespace clang::frontend; |
| 115 | |
| 116 | switch (CI.getFrontendOpts().ProgramAction) { |
| 117 | default: |
| 118 | llvm_unreachable("Invalid program action!"); |
| 119 | |
| 120 | case ASTDump: return new ASTDumpAction(); |
| 121 | case ASTPrint: return new ASTPrintAction(); |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 122 | case ASTDumpXML: return new ASTDumpXMLAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 123 | case ASTView: return new ASTViewAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 124 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 125 | case DumpTokens: return new DumpTokensAction(); |
| 126 | case EmitAssembly: return new EmitAssemblyAction(); |
| 127 | case EmitBC: return new EmitBCAction(); |
| 128 | case EmitHTML: return new HTMLPrintAction(); |
| 129 | case EmitLLVM: return new EmitLLVMAction(); |
| 130 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 131 | case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); |
| 132 | case EmitObj: return new EmitObjAction(); |
| 133 | case FixIt: return new FixItAction(); |
Sean Callanan | 0523083 | 2011-12-01 04:31:46 +0000 | [diff] [blame] | 134 | case GeneratePCH: return new GeneratePCHAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 135 | case GeneratePTH: return new GeneratePTHAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 136 | case InitOnly: return new InitOnlyAction(); |
| 137 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
| 138 | |
| 139 | case PluginAction: { |
| 140 | for (FrontendPluginRegistry::iterator it = |
| 141 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 142 | it != ie; ++it) { |
| 143 | if (it->getName() == CI.getFrontendOpts().ActionName) { |
| 144 | llvm::OwningPtr<PluginASTAction> P(it->instantiate()); |
| 145 | if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) |
| 146 | return 0; |
| 147 | return P.take(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 152 | << CI.getFrontendOpts().ActionName; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 157 | case PrintPreamble: return new PrintPreambleAction(); |
| 158 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 159 | case RewriteMacros: return new RewriteMacrosAction(); |
| 160 | case RewriteObjC: return new RewriteObjCAction(); |
| 161 | case RewriteTest: return new RewriteTestAction(); |
Sean Callanan | ad29309 | 2011-01-18 23:32:05 +0000 | [diff] [blame] | 162 | //case RunAnalysis: return new AnalysisAction(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 163 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
| 168 | // Create the underlying action. |
| 169 | FrontendAction *Act = CreateFrontendBaseAction(CI); |
| 170 | if (!Act) |
| 171 | return 0; |
| 172 | |
| 173 | // If there are any AST files to merge, create a frontend action |
| 174 | // adaptor to perform the merge. |
| 175 | if (!CI.getFrontendOpts().ASTMergeFiles.empty()) |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 176 | Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 177 | |
| 178 | return Act; |
| 179 | } |
| 180 | |
| 181 | //===----------------------------------------------------------------------===// |
| 182 | // Implementation of ClangExpressionParser |
| 183 | //===----------------------------------------------------------------------===// |
| 184 | |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 185 | ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope, |
| 186 | ClangExpression &expr) : |
| 187 | m_expr (expr), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 188 | m_compiler (), |
| 189 | m_code_generator (NULL), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 190 | m_jitted_functions () |
| 191 | { |
| 192 | // Initialize targets first, so that --version shows registered targets. |
| 193 | static struct InitializeLLVM { |
| 194 | InitializeLLVM() { |
| 195 | llvm::InitializeAllTargets(); |
| 196 | llvm::InitializeAllAsmPrinters(); |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 197 | llvm::InitializeAllTargetMCs(); |
Sean Callanan | 0523083 | 2011-12-01 04:31:46 +0000 | [diff] [blame] | 198 | llvm::InitializeAllDisassemblers(); |
Sean Callanan | 877e57b | 2012-09-06 01:39:02 +0000 | [diff] [blame] | 199 | |
| 200 | llvm::DisablePrettyStackTrace = true; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 201 | } |
| 202 | } InitializeLLVM; |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 203 | |
| 204 | llvm::setCurrentDebugType("internalize"); |
| 205 | llvm::DebugFlag = true; |
| 206 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 207 | // 1. Create a new compiler instance. |
| 208 | m_compiler.reset(new CompilerInstance()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 209 | |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 210 | // 2. Install the target. |
| 211 | |
| 212 | lldb::TargetSP target_sp; |
| 213 | if (exe_scope) |
| 214 | target_sp = exe_scope->CalculateTarget(); |
| 215 | |
| 216 | // TODO: figure out what to really do when we don't have a valid target. |
| 217 | // Sometimes this will be ok to just use the host target triple (when we |
| 218 | // evaluate say "2+3", but other expressions like breakpoint conditions |
| 219 | // and other things that _are_ target specific really shouldn't just be |
| 220 | // using the host triple. This needs to be fixed in a better way. |
| 221 | if (target_sp && target_sp->GetArchitecture().IsValid()) |
| 222 | { |
| 223 | std::string triple = target_sp->GetArchitecture().GetTriple().str(); |
| 224 | |
| 225 | int dash_count = 0; |
| 226 | for (size_t i = 0; i < triple.size(); ++i) |
| 227 | { |
| 228 | if (triple[i] == '-') |
| 229 | dash_count++; |
| 230 | if (dash_count == 3) |
| 231 | { |
| 232 | triple.resize(i); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | m_compiler->getTargetOpts().Triple = triple; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple(); |
| 242 | } |
| 243 | |
| 244 | if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) |
| 245 | m_compiler->getTargetOpts().ABI = "apcs-gnu"; |
| 246 | |
| 247 | m_compiler->createDiagnostics(0, 0); |
| 248 | |
| 249 | // Create the target instance. |
| 250 | m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(), |
| 251 | m_compiler->getTargetOpts())); |
| 252 | |
| 253 | assert (m_compiler->hasTarget()); |
| 254 | |
| 255 | // 3. Set options. |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 256 | |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 257 | lldb::LanguageType language = expr.Language(); |
Greg Clayton | f51ed30 | 2011-01-15 01:32:14 +0000 | [diff] [blame] | 258 | |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 259 | switch (language) |
| 260 | { |
| 261 | case lldb::eLanguageTypeC: |
| 262 | break; |
| 263 | case lldb::eLanguageTypeObjC: |
| 264 | m_compiler->getLangOpts().ObjC1 = true; |
| 265 | m_compiler->getLangOpts().ObjC2 = true; |
| 266 | break; |
| 267 | case lldb::eLanguageTypeC_plus_plus: |
| 268 | m_compiler->getLangOpts().CPlusPlus = true; |
Sean Callanan | f061318 | 2012-05-16 21:03:38 +0000 | [diff] [blame] | 269 | m_compiler->getLangOpts().CPlusPlus0x = true; |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 270 | break; |
| 271 | case lldb::eLanguageTypeObjC_plus_plus: |
| 272 | default: |
| 273 | m_compiler->getLangOpts().ObjC1 = true; |
| 274 | m_compiler->getLangOpts().ObjC2 = true; |
| 275 | m_compiler->getLangOpts().CPlusPlus = true; |
Sean Callanan | f061318 | 2012-05-16 21:03:38 +0000 | [diff] [blame] | 276 | m_compiler->getLangOpts().CPlusPlus0x = true; |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 277 | break; |
| 278 | } |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 279 | |
Sean Callanan | 6e12c7a | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 280 | m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients |
| 281 | if (expr.DesiredResultType() == ClangExpression::eResultTypeId) |
| 282 | m_compiler->getLangOpts().DebuggerCastResultToId = true; |
| 283 | |
Sean Callanan | 6c797dc | 2012-04-17 00:49:48 +0000 | [diff] [blame] | 284 | // Spell checking is a nice feature, but it ends up completing a |
| 285 | // lot of types that we didn't strictly speaking need to complete. |
| 286 | // As a result, we spend a long time parsing and importing debug |
| 287 | // information. |
| 288 | m_compiler->getLangOpts().SpellChecking = false; |
| 289 | |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 290 | lldb::ProcessSP process_sp; |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 291 | if (exe_scope) |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 292 | process_sp = exe_scope->CalculateProcess(); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 293 | |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 294 | if (process_sp && m_compiler->getLangOpts().ObjC1) |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 295 | { |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 296 | if (process_sp->GetObjCLanguageRuntime()) |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 297 | { |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 298 | if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2) |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 299 | m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7)); |
| 300 | else |
| 301 | m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7)); |
Sean Callanan | 6e12c7a | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 302 | |
| 303 | if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing()) |
Sean Callanan | 6e12c7a | 2012-03-08 02:39:03 +0000 | [diff] [blame] | 304 | m_compiler->getLangOpts().DebuggerObjCLiteral = true; |
Sean Callanan | c7674af | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
Greg Clayton | f51ed30 | 2011-01-15 01:32:14 +0000 | [diff] [blame] | 307 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 308 | m_compiler->getLangOpts().ThreadsafeStatics = false; |
| 309 | m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access |
| 310 | m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 311 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 312 | // Set CodeGen options |
| 313 | m_compiler->getCodeGenOpts().EmitDeclMetadata = true; |
| 314 | m_compiler->getCodeGenOpts().InstrumentFunctions = false; |
| 315 | |
| 316 | // Disable some warnings. |
| 317 | m_compiler->getDiagnosticOpts().Warnings.push_back("no-unused-value"); |
| 318 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 319 | // Inform the target of the language options |
| 320 | // |
| 321 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 322 | // created. This complexity should be lifted elsewhere. |
| 323 | m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts()); |
| 324 | |
| 325 | // 4. Set up the diagnostic buffer for reporting errors |
| 326 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 327 | m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 328 | |
| 329 | // 5. Set up the source management objects inside the compiler |
| 330 | |
Greg Clayton | 22defe8 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 331 | clang::FileSystemOptions file_system_options; |
| 332 | m_file_manager.reset(new clang::FileManager(file_system_options)); |
Sean Callanan | 8a3b0a8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 333 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 334 | if (!m_compiler->hasSourceManager()) |
Greg Clayton | 22defe8 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 335 | m_compiler->createSourceManager(*m_file_manager.get()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 336 | |
| 337 | m_compiler->createFileManager(); |
| 338 | m_compiler->createPreprocessor(); |
| 339 | |
| 340 | // 6. Most of this we get from the CompilerInstance, but we |
| 341 | // also want to give the context an ExternalASTSource. |
Sean Callanan | ee8fc72 | 2010-11-19 20:20:02 +0000 | [diff] [blame] | 342 | m_selector_table.reset(new SelectorTable()); |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 343 | m_builtin_context.reset(new Builtin::Context()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 344 | |
| 345 | std::auto_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(), |
| 346 | m_compiler->getSourceManager(), |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 347 | &m_compiler->getTarget(), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 348 | m_compiler->getPreprocessor().getIdentifierTable(), |
Sean Callanan | ee8fc72 | 2010-11-19 20:20:02 +0000 | [diff] [blame] | 349 | *m_selector_table.get(), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 350 | *m_builtin_context.get(), |
| 351 | 0)); |
| 352 | |
| 353 | ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); |
| 354 | |
| 355 | if (decl_map) |
| 356 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 357 | llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy()); |
| 358 | decl_map->InstallASTContext(ast_context.get()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 359 | ast_context->setExternalSource(ast_source); |
| 360 | } |
| 361 | |
| 362 | m_compiler->setASTContext(ast_context.release()); |
| 363 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 364 | std::string module_name("$__lldb_module"); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 365 | |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 366 | m_llvm_context.reset(new LLVMContext()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 367 | m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(), |
| 368 | module_name, |
| 369 | m_compiler->getCodeGenOpts(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 370 | *m_llvm_context)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | ClangExpressionParser::~ClangExpressionParser() |
| 374 | { |
| 375 | } |
| 376 | |
| 377 | unsigned |
| 378 | ClangExpressionParser::Parse (Stream &stream) |
| 379 | { |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 380 | TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient()); |
| 381 | |
| 382 | diag_buf->FlushDiagnostics (m_compiler->getDiagnostics()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 383 | |
| 384 | MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__); |
Sean Callanan | 142f94c | 2012-03-01 02:03:47 +0000 | [diff] [blame] | 385 | m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 386 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 387 | diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 388 | |
| 389 | ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get()); |
| 390 | |
| 391 | if (ast_transformer) |
| 392 | ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext()); |
| 393 | else |
| 394 | ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext()); |
| 395 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 396 | diag_buf->EndSourceFile(); |
Sean Callanan | fb3058e | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 397 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 398 | TextDiagnosticBuffer::const_iterator diag_iterator; |
| 399 | |
| 400 | int num_errors = 0; |
Sean Callanan | 7617c29 | 2010-11-01 20:28:09 +0000 | [diff] [blame] | 401 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 402 | for (diag_iterator = diag_buf->warn_begin(); |
| 403 | diag_iterator != diag_buf->warn_end(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 404 | ++diag_iterator) |
| 405 | stream.Printf("warning: %s\n", (*diag_iterator).second.c_str()); |
| 406 | |
| 407 | num_errors = 0; |
| 408 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 409 | for (diag_iterator = diag_buf->err_begin(); |
| 410 | diag_iterator != diag_buf->err_end(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 411 | ++diag_iterator) |
| 412 | { |
| 413 | num_errors++; |
| 414 | stream.Printf("error: %s\n", (*diag_iterator).second.c_str()); |
| 415 | } |
| 416 | |
Sean Callanan | 7617c29 | 2010-11-01 20:28:09 +0000 | [diff] [blame] | 417 | for (diag_iterator = diag_buf->note_begin(); |
| 418 | diag_iterator != diag_buf->note_end(); |
| 419 | ++diag_iterator) |
| 420 | stream.Printf("note: %s\n", (*diag_iterator).second.c_str()); |
| 421 | |
Sean Callanan | fb3058e | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 422 | if (!num_errors) |
| 423 | { |
| 424 | if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes()) |
| 425 | { |
| 426 | stream.Printf("error: Couldn't infer the type of a variable\n"); |
| 427 | num_errors++; |
| 428 | } |
| 429 | } |
| 430 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 431 | return num_errors; |
| 432 | } |
| 433 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 434 | static bool FindFunctionInModule (std::string &mangled_name, |
| 435 | llvm::Module *module, |
| 436 | const char *orig_name) |
| 437 | { |
| 438 | for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end(); |
| 439 | fi != fe; |
| 440 | ++fi) |
| 441 | { |
| 442 | if (fi->getName().str().find(orig_name) != std::string::npos) |
| 443 | { |
| 444 | mangled_name = fi->getName().str(); |
| 445 | return true; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return false; |
| 450 | } |
| 451 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 452 | Error |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 453 | ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_allocation_addr, |
| 454 | lldb::addr_t &func_addr, |
| 455 | lldb::addr_t &func_end, |
| 456 | ExecutionContext &exe_ctx, |
| 457 | IRForTarget::StaticDataAllocator *data_allocator, |
| 458 | bool &evaluated_statically, |
| 459 | lldb::ClangExpressionVariableSP &const_result, |
| 460 | ExecutionPolicy execution_policy) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 461 | { |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 462 | func_allocation_addr = LLDB_INVALID_ADDRESS; |
| 463 | func_addr = LLDB_INVALID_ADDRESS; |
| 464 | func_end = LLDB_INVALID_ADDRESS; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 465 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 466 | |
Sean Callanan | ddf110d | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 467 | std::auto_ptr<llvm::ExecutionEngine> execution_engine; |
| 468 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 469 | Error err; |
| 470 | |
| 471 | llvm::Module *module = m_code_generator->ReleaseModule(); |
| 472 | |
| 473 | if (!module) |
| 474 | { |
| 475 | err.SetErrorToGenericError(); |
| 476 | err.SetErrorString("IR doesn't contain a module"); |
| 477 | return err; |
| 478 | } |
| 479 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 480 | // Find the actual name of the function (it's often mangled somehow) |
| 481 | |
| 482 | std::string function_name; |
| 483 | |
| 484 | if (!FindFunctionInModule(function_name, module, m_expr.FunctionName())) |
| 485 | { |
| 486 | err.SetErrorToGenericError(); |
| 487 | err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName()); |
| 488 | return err; |
| 489 | } |
| 490 | else |
| 491 | { |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 492 | if (log) |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 493 | log->Printf("Found function %s for %s", function_name.c_str(), m_expr.FunctionName()); |
| 494 | } |
| 495 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 496 | ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL |
| 497 | |
| 498 | if (decl_map) |
| 499 | { |
Sean Callanan | 97c924e | 2011-01-27 01:07:04 +0000 | [diff] [blame] | 500 | Stream *error_stream = NULL; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 501 | Target *target = exe_ctx.GetTargetPtr(); |
| 502 | if (target) |
| 503 | error_stream = &target->GetDebugger().GetErrorStream(); |
Sean Callanan | 97c924e | 2011-01-27 01:07:04 +0000 | [diff] [blame] | 504 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 505 | IRForTarget ir_for_target(decl_map, |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 506 | m_expr.NeedsVariableResolution(), |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 507 | execution_policy, |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 508 | const_result, |
Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 509 | data_allocator, |
Sean Callanan | 97c924e | 2011-01-27 01:07:04 +0000 | [diff] [blame] | 510 | error_stream, |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 511 | function_name.c_str()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 512 | |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 513 | bool ir_can_run = ir_for_target.runOnModule(*module); |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 514 | |
Sean Callanan | ddf110d | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 515 | Error &interpreter_error(ir_for_target.getInterpreterError()); |
| 516 | |
| 517 | if (execution_policy != eExecutionPolicyAlways && interpreter_error.Success()) |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 518 | { |
Johnny Chen | c613496 | 2012-01-06 00:35:38 +0000 | [diff] [blame] | 519 | if (const_result) |
| 520 | const_result->TransferAddress(); |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 521 | evaluated_statically = true; |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 522 | err.Clear(); |
| 523 | return err; |
| 524 | } |
| 525 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 526 | Process *process = exe_ctx.GetProcessPtr(); |
| 527 | |
| 528 | if (!process || execution_policy == eExecutionPolicyNever) |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 529 | { |
| 530 | err.SetErrorToGenericError(); |
Sean Callanan | ddf110d | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 531 | if (execution_policy == eExecutionPolicyAlways) |
| 532 | err.SetErrorString("Execution needed to run in the target, but the target can't be run"); |
| 533 | else |
| 534 | err.SetErrorStringWithFormat("Interpreting the expression locally failed: %s", interpreter_error.AsCString()); |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 535 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 536 | return err; |
| 537 | } |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 538 | else if (!ir_can_run) |
| 539 | { |
| 540 | err.SetErrorToGenericError(); |
| 541 | err.SetErrorString("The expression could not be prepared to run in the target"); |
| 542 | |
| 543 | return err; |
| 544 | } |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 545 | |
Sean Callanan | 6cf6c47 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 546 | if (execution_policy != eExecutionPolicyNever && |
| 547 | m_expr.NeedsValidation() && |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 548 | process) |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 549 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 550 | if (!process->GetDynamicCheckers()) |
Sean Callanan | 6cf6c47 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 551 | { |
| 552 | DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions(); |
| 553 | |
| 554 | StreamString install_errors; |
| 555 | |
| 556 | if (!dynamic_checkers->Install(install_errors, exe_ctx)) |
| 557 | { |
| 558 | if (install_errors.GetString().empty()) |
| 559 | err.SetErrorString ("couldn't install checkers, unknown error"); |
| 560 | else |
| 561 | err.SetErrorString (install_errors.GetString().c_str()); |
| 562 | |
| 563 | return err; |
| 564 | } |
| 565 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 566 | process->SetDynamicCheckers(dynamic_checkers); |
Sean Callanan | 6cf6c47 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 567 | |
| 568 | if (log) |
| 569 | log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers =="); |
| 570 | } |
| 571 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 572 | IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.c_str()); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 573 | |
| 574 | if (!ir_dynamic_checks.runOnModule(*module)) |
| 575 | { |
| 576 | err.SetErrorToGenericError(); |
| 577 | err.SetErrorString("Couldn't add dynamic checks to the expression"); |
| 578 | return err; |
| 579 | } |
| 580 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 583 | // llvm will own this pointer when llvm::ExecutionEngine::createJIT is called |
| 584 | // below so we don't need to free it. |
| 585 | RecordingMemoryManager *jit_memory_manager = new RecordingMemoryManager(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 586 | |
| 587 | std::string error_string; |
Sean Callanan | 7b8eb76 | 2011-11-01 17:33:54 +0000 | [diff] [blame] | 588 | |
| 589 | if (log) |
| 590 | { |
| 591 | std::string s; |
| 592 | raw_string_ostream oss(s); |
| 593 | |
| 594 | module->print(oss, NULL); |
| 595 | |
| 596 | oss.flush(); |
| 597 | |
| 598 | log->Printf ("Module being sent to JIT: \n%s", s.c_str()); |
| 599 | } |
| 600 | |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 601 | EngineBuilder builder(module); |
| 602 | builder.setEngineKind(EngineKind::JIT) |
| 603 | .setErrorStr(&error_string) |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 604 | .setRelocationModel(llvm::Reloc::PIC_) |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 605 | .setJITMemoryManager(jit_memory_manager) |
| 606 | .setOptLevel(CodeGenOpt::Less) |
| 607 | .setAllocateGVsWithCode(true) |
| 608 | .setCodeModel(CodeModel::Small) |
Sean Callanan | 16b53ab | 2011-10-12 00:12:34 +0000 | [diff] [blame] | 609 | .setUseMCJIT(true); |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame^] | 610 | |
| 611 | llvm::Triple triple(module->getTargetTriple()); |
| 612 | StringRef mArch; |
| 613 | StringRef mCPU; |
| 614 | SmallVector<std::string, 0> mAttrs; |
| 615 | |
| 616 | TargetMachine *target_machine = builder.selectTarget(triple, |
| 617 | mArch, |
| 618 | mCPU, |
| 619 | mAttrs); |
| 620 | |
| 621 | execution_engine.reset(builder.create(target_machine)); |
Sean Callanan | 9ac3a96 | 2010-11-02 23:20:00 +0000 | [diff] [blame] | 622 | |
Sean Callanan | ddf110d | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 623 | if (!execution_engine.get()) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 624 | { |
| 625 | err.SetErrorToGenericError(); |
| 626 | err.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str()); |
| 627 | return err; |
| 628 | } |
| 629 | |
Sean Callanan | ddf110d | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 630 | execution_engine->DisableLazyCompilation(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 631 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 632 | llvm::Function *function = module->getFunction (function_name.c_str()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 633 | |
| 634 | // We don't actually need the function pointer here, this just forces it to get resolved. |
| 635 | |
Sean Callanan | ddf110d | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 636 | void *fun_ptr = execution_engine->getPointerToFunction(function); |
| 637 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 638 | // Errors usually cause failures in the JIT, but if we're lucky we get here. |
| 639 | |
Sean Callanan | 7b8eb76 | 2011-11-01 17:33:54 +0000 | [diff] [blame] | 640 | if (!function) |
| 641 | { |
| 642 | err.SetErrorToGenericError(); |
| 643 | err.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", function_name.c_str()); |
| 644 | return err; |
| 645 | } |
| 646 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 647 | if (!fun_ptr) |
| 648 | { |
| 649 | err.SetErrorToGenericError(); |
Sean Callanan | 7b8eb76 | 2011-11-01 17:33:54 +0000 | [diff] [blame] | 650 | err.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", function_name.c_str()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 651 | return err; |
| 652 | } |
| 653 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 654 | m_jitted_functions.push_back (ClangExpressionParser::JittedFunction(function_name.c_str(), (lldb::addr_t)fun_ptr)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 655 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 656 | |
| 657 | Process *process = exe_ctx.GetProcessPtr(); |
| 658 | if (process == NULL) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 659 | { |
| 660 | err.SetErrorToGenericError(); |
| 661 | err.SetErrorString("Couldn't write the JIT compiled code into the target because there is no target"); |
| 662 | return err; |
| 663 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 664 | |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 665 | jit_memory_manager->CommitAllocations(*process); |
Sean Callanan | 142f94c | 2012-03-01 02:03:47 +0000 | [diff] [blame] | 666 | jit_memory_manager->ReportAllocations(*execution_engine); |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 667 | jit_memory_manager->WriteData(*process); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 668 | |
| 669 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 670 | |
| 671 | for (pos = m_jitted_functions.begin(); pos != end; pos++) |
| 672 | { |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 673 | (*pos).m_remote_addr = jit_memory_manager->GetRemoteAddressForLocal ((*pos).m_local_addr); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 674 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 675 | if (!(*pos).m_name.compare(function_name.c_str())) |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 676 | { |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 677 | RecordingMemoryManager::AddrRange func_range = jit_memory_manager->GetRemoteRangeForLocal((*pos).m_local_addr); |
| 678 | func_end = func_range.first + func_range.second; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 679 | func_addr = (*pos).m_remote_addr; |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 680 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Sean Callanan | 6dff827 | 2010-11-08 03:49:50 +0000 | [diff] [blame] | 683 | if (log) |
| 684 | { |
| 685 | log->Printf("Code can be run in the target."); |
| 686 | |
| 687 | StreamString disassembly_stream; |
| 688 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 689 | Error err = DisassembleFunction(disassembly_stream, exe_ctx, jit_memory_manager); |
Sean Callanan | 6dff827 | 2010-11-08 03:49:50 +0000 | [diff] [blame] | 690 | |
| 691 | if (!err.Success()) |
| 692 | { |
| 693 | log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); |
| 694 | } |
| 695 | else |
| 696 | { |
| 697 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 698 | } |
| 699 | } |
| 700 | |
Sean Callanan | ddf110d | 2012-01-24 22:06:48 +0000 | [diff] [blame] | 701 | execution_engine.reset(); |
| 702 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 703 | err.Clear(); |
| 704 | return err; |
| 705 | } |
| 706 | |
| 707 | Error |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 708 | ClangExpressionParser::DisassembleFunction (Stream &stream, ExecutionContext &exe_ctx, RecordingMemoryManager *jit_memory_manager) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 709 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 710 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 711 | |
| 712 | const char *name = m_expr.FunctionName(); |
| 713 | |
| 714 | Error ret; |
| 715 | |
| 716 | ret.Clear(); |
| 717 | |
| 718 | lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; |
| 719 | lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; |
| 720 | |
| 721 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 722 | |
| 723 | for (pos = m_jitted_functions.begin(); pos < end; pos++) |
| 724 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 725 | if (strstr(pos->m_name.c_str(), name)) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 726 | { |
| 727 | func_local_addr = pos->m_local_addr; |
| 728 | func_remote_addr = pos->m_remote_addr; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | if (func_local_addr == LLDB_INVALID_ADDRESS) |
| 733 | { |
| 734 | ret.SetErrorToGenericError(); |
| 735 | ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", name); |
| 736 | return ret; |
| 737 | } |
| 738 | |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 739 | if (log) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 740 | log->Printf("Found function, has local address 0x%llx and remote address 0x%llx", (uint64_t)func_local_addr, (uint64_t)func_remote_addr); |
| 741 | |
| 742 | std::pair <lldb::addr_t, lldb::addr_t> func_range; |
| 743 | |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 744 | func_range = jit_memory_manager->GetRemoteRangeForLocal(func_local_addr); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 745 | |
| 746 | if (func_range.first == 0 && func_range.second == 0) |
| 747 | { |
| 748 | ret.SetErrorToGenericError(); |
| 749 | ret.SetErrorStringWithFormat("Couldn't find code range for function %s", name); |
| 750 | return ret; |
| 751 | } |
| 752 | |
Enrico Granata | 4c3fb4b | 2011-07-19 18:03:25 +0000 | [diff] [blame] | 753 | if (log) |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 754 | log->Printf("Function's code range is [0x%llx+0x%llx]", func_range.first, func_range.second); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 755 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 756 | Target *target = exe_ctx.GetTargetPtr(); |
| 757 | if (!target) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 758 | { |
| 759 | ret.SetErrorToGenericError(); |
| 760 | ret.SetErrorString("Couldn't find the target"); |
| 761 | } |
| 762 | |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 763 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 764 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 765 | Process *process = exe_ctx.GetProcessPtr(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 766 | Error err; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 767 | process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 768 | |
| 769 | if (!err.Success()) |
| 770 | { |
| 771 | ret.SetErrorToGenericError(); |
| 772 | ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error")); |
| 773 | return ret; |
| 774 | } |
| 775 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 776 | ArchSpec arch(target->GetArchitecture()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 777 | |
Sean Callanan | 4f28c31 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 778 | lldb::DisassemblerSP disassembler = Disassembler::FindPlugin(arch, NULL); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 779 | |
Sean Callanan | b386d82 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 780 | if (!disassembler) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 781 | { |
| 782 | ret.SetErrorToGenericError(); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 783 | ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 784 | return ret; |
| 785 | } |
| 786 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 787 | if (!process) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 788 | { |
| 789 | ret.SetErrorToGenericError(); |
| 790 | ret.SetErrorString("Couldn't find the process"); |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | DataExtractor extractor(buffer_sp, |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 795 | process->GetByteOrder(), |
| 796 | target->GetArchitecture().GetAddressByteSize()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 797 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 798 | if (log) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 799 | { |
| 800 | log->Printf("Function data has contents:"); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 801 | extractor.PutToLog (log.get(), |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 802 | 0, |
| 803 | extractor.GetByteSize(), |
| 804 | func_remote_addr, |
| 805 | 16, |
| 806 | DataExtractor::TypeUInt8); |
| 807 | } |
| 808 | |
Greg Clayton | 3508c38 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 809 | disassembler->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 810 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 811 | InstructionList &instruction_list = disassembler->GetInstructionList(); |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 812 | const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 813 | for (uint32_t instruction_index = 0, num_instructions = instruction_list.GetSize(); |
| 814 | instruction_index < num_instructions; |
| 815 | ++instruction_index) |
| 816 | { |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 817 | Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 818 | instruction->Dump (&stream, |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 819 | max_opcode_byte_size, |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 820 | true, |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 821 | true, |
Greg Clayton | 0fef968 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 822 | &exe_ctx); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 823 | stream.PutChar('\n'); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | return ret; |
| 827 | } |