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