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