Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ClangExpression.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 | // C Includes |
| 11 | #include <stdio.h> |
| 12 | #if HAVE_SYS_TYPES_H |
| 13 | # include <sys/types.h> |
| 14 | #endif |
| 15 | |
| 16 | // C++ Includes |
| 17 | #include <cstdlib> |
| 18 | #include <string> |
| 19 | #include <map> |
| 20 | |
| 21 | // Other libraries and framework includes |
| 22 | #include "clang/AST/ASTContext.h" |
| 23 | #include "clang/AST/ExternalASTSource.h" |
| 24 | #include "clang/Basic/FileManager.h" |
| 25 | #include "clang/Basic/TargetInfo.h" |
| 26 | #include "clang/Basic/Version.h" |
| 27 | #include "clang/CodeGen/ModuleBuilder.h" |
| 28 | #include "clang/Driver/CC1Options.h" |
| 29 | #include "clang/Driver/OptTable.h" |
| 30 | #include "clang/Frontend/CodeGenAction.h" |
| 31 | #include "clang/Frontend/CompilerInstance.h" |
| 32 | #include "clang/Frontend/CompilerInvocation.h" |
| 33 | #include "clang/Frontend/FrontendActions.h" |
| 34 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 35 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 36 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 37 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 38 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
| 39 | #include "clang/Lex/Preprocessor.h" |
| 40 | #include "clang/Sema/ParseAST.h" |
| 41 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 42 | #include "llvm/ExecutionEngine/JIT.h" |
| 43 | #include "llvm/Module.h" |
| 44 | #include "llvm/ADT/StringRef.h" |
| 45 | #include "llvm/LLVMContext.h" |
| 46 | #include "llvm/Support/MemoryBuffer.h" |
| 47 | #include "llvm/System/DynamicLibrary.h" |
| 48 | #include "llvm/System/Host.h" |
| 49 | #include "llvm/System/Signals.h" |
| 50 | #include "llvm/Target/TargetSelect.h" |
| 51 | |
| 52 | // Project includes |
| 53 | #include "lldb/Expression/ClangExpression.h" |
| 54 | #include "lldb/Expression/ClangASTSource.h" |
| 55 | #include "lldb/Expression/ClangStmtVisitor.h" |
| 56 | #include "lldb/Symbol/ClangASTContext.h" |
| 57 | #include "lldb/Expression/RecordingMemoryManager.h" |
| 58 | #include "lldb/Target/ExecutionContext.h" |
| 59 | #include "lldb/Target/Process.h" |
| 60 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | #include "lldb/Core/StreamString.h" |
| 62 | #include "lldb/Host/Mutex.h" |
| 63 | #include "lldb/Core/dwarf.h" |
| 64 | |
| 65 | |
| 66 | using namespace lldb_private; |
| 67 | using namespace clang; |
| 68 | using namespace llvm; |
| 69 | |
| 70 | namespace clang { |
| 71 | |
| 72 | class AnalyzerOptions; |
| 73 | class CodeGenOptions; |
| 74 | class DependencyOutputOptions; |
| 75 | class DiagnosticOptions; |
| 76 | class FrontendOptions; |
| 77 | class HeaderSearchOptions; |
| 78 | class LangOptions; |
| 79 | class PreprocessorOptions; |
| 80 | class PreprocessorOutputOptions; |
| 81 | class TargetInfo; |
| 82 | class TargetOptions; |
| 83 | |
| 84 | } // end namespace clang |
| 85 | |
| 86 | |
| 87 | |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | // Utility Methods |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | |
| 92 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 93 | llvm::sys::Path P = |
| 94 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 95 | (void*)(intptr_t) GetBuiltinIncludePath); |
| 96 | |
| 97 | if (!P.isEmpty()) { |
| 98 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 99 | P.eraseComponent(); // Remove /bin from foo/bin |
| 100 | |
| 101 | // Get foo/lib/clang/<version>/include |
| 102 | P.appendComponent("lib"); |
| 103 | P.appendComponent("clang"); |
| 104 | P.appendComponent(CLANG_VERSION_STRING); |
| 105 | P.appendComponent("include"); |
| 106 | } |
| 107 | |
| 108 | return P.str(); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | // Main driver |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | |
| 116 | void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 117 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 118 | |
| 119 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 120 | |
| 121 | // We cannot recover from llvm errors. |
| 122 | exit(1); |
| 123 | } |
| 124 | |
| 125 | static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { |
| 126 | using namespace clang::frontend; |
| 127 | |
| 128 | switch (CI.getFrontendOpts().ProgramAction) { |
| 129 | default: |
| 130 | llvm_unreachable("Invalid program action!"); |
| 131 | |
| 132 | case ASTDump: return new ASTDumpAction(); |
| 133 | case ASTPrint: return new ASTPrintAction(); |
| 134 | case ASTPrintXML: return new ASTPrintXMLAction(); |
| 135 | case ASTView: return new ASTViewAction(); |
| 136 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 137 | case DumpTokens: return new DumpTokensAction(); |
| 138 | case EmitAssembly: return new EmitAssemblyAction(); |
| 139 | case EmitBC: return new EmitBCAction(); |
| 140 | case EmitHTML: return new HTMLPrintAction(); |
| 141 | case EmitLLVM: return new EmitLLVMAction(); |
| 142 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 143 | case EmitObj: return new EmitObjAction(); |
| 144 | case FixIt: return new FixItAction(); |
| 145 | case GeneratePCH: return new GeneratePCHAction(); |
| 146 | case GeneratePTH: return new GeneratePTHAction(); |
| 147 | case InheritanceView: return new InheritanceViewAction(); |
| 148 | case InitOnly: return new InitOnlyAction(); |
| 149 | case ParseNoop: return new ParseOnlyAction(); |
| 150 | case ParsePrintCallbacks: return new PrintParseAction(); |
| 151 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
| 152 | |
| 153 | case PluginAction: { |
| 154 | if (CI.getFrontendOpts().ActionName == "help") { |
| 155 | llvm::errs() << "clang -cc1 plugins:\n"; |
| 156 | for (FrontendPluginRegistry::iterator it = |
| 157 | FrontendPluginRegistry::begin(), |
| 158 | ie = FrontendPluginRegistry::end(); |
| 159 | it != ie; ++it) |
| 160 | llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n"; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | for (FrontendPluginRegistry::iterator it = |
| 165 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 166 | it != ie; ++it) { |
| 167 | if (it->getName() == CI.getFrontendOpts().ActionName) |
| 168 | return it->instantiate(); |
| 169 | } |
| 170 | |
| 171 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 172 | << CI.getFrontendOpts().ActionName; |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 177 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 178 | case RewriteMacros: return new RewriteMacrosAction(); |
| 179 | case RewriteObjC: return new RewriteObjCAction(); |
| 180 | case RewriteTest: return new RewriteTestAction(); |
| 181 | case RunAnalysis: return new AnalysisAction(); |
| 182 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | //---------------------------------------------------------------------- |
| 187 | // ClangExpression constructor |
| 188 | //---------------------------------------------------------------------- |
| 189 | ClangExpression::ClangExpression(const char *target_triple, |
| 190 | ClangExpressionDeclMap *decl_map) : |
| 191 | m_target_triple (), |
| 192 | m_jit_mm_ptr (NULL), |
| 193 | m_code_generator_ptr (NULL), |
| 194 | m_decl_map (decl_map) |
| 195 | { |
| 196 | if (target_triple && target_triple[0]) |
| 197 | m_target_triple = target_triple; |
| 198 | else |
| 199 | m_target_triple = llvm::sys::getHostTriple(); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | //---------------------------------------------------------------------- |
| 204 | // Destructor |
| 205 | //---------------------------------------------------------------------- |
| 206 | ClangExpression::~ClangExpression() |
| 207 | { |
| 208 | if (m_code_generator_ptr && !m_execution_engine.get()) |
| 209 | delete m_code_generator_ptr; |
| 210 | } |
| 211 | |
| 212 | bool |
| 213 | ClangExpression::CreateCompilerInstance (bool &IsAST) |
| 214 | { |
| 215 | // Initialize targets first, so that --version shows registered targets. |
| 216 | static struct InitializeLLVM { |
| 217 | InitializeLLVM() { |
| 218 | llvm::InitializeAllTargets(); |
| 219 | llvm::InitializeAllAsmPrinters(); |
| 220 | } |
| 221 | } InitializeLLVM; |
| 222 | |
| 223 | // 1. Create a new compiler instance. |
| 224 | m_clang_ap.reset(new CompilerInstance()); |
| 225 | m_clang_ap->setLLVMContext(new LLVMContext()); |
| 226 | |
| 227 | // 2. Set options. |
| 228 | |
| 229 | // Parse expressions as Objective C++ regardless of context. |
| 230 | // Our hook into Clang's lookup mechanism only works in C++. |
| 231 | m_clang_ap->getLangOpts().CPlusPlus = true; |
| 232 | m_clang_ap->getLangOpts().ObjC1 = true; |
| 233 | |
| 234 | // Disable some warnings. |
| 235 | m_clang_ap->getDiagnosticOpts().Warnings.push_back("no-unused-value"); |
| 236 | |
| 237 | // Set the target triple. |
| 238 | m_clang_ap->getTargetOpts().Triple = m_target_triple; |
| 239 | |
| 240 | // 3. Set up various important bits of infrastructure. |
| 241 | |
| 242 | m_clang_ap->createDiagnostics(0, 0); |
| 243 | m_clang_ap->getLangOpts().CPlusPlus = true; |
| 244 | |
| 245 | // Create the target instance. |
| 246 | m_clang_ap->setTarget(TargetInfo::CreateTargetInfo(m_clang_ap->getDiagnostics(), |
| 247 | m_clang_ap->getTargetOpts())); |
| 248 | if (!m_clang_ap->hasTarget()) |
| 249 | { |
| 250 | m_clang_ap.reset(); |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | // Inform the target of the language options |
| 255 | // |
| 256 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 257 | // created. This complexity should be lifted elsewhere. |
| 258 | m_clang_ap->getTarget().setForcedLangOptions(m_clang_ap->getLangOpts()); |
| 259 | |
| 260 | return m_clang_ap.get(); |
| 261 | } |
| 262 | |
| 263 | Mutex & |
| 264 | ClangExpression::GetClangMutex () |
| 265 | { |
| 266 | static Mutex g_clang_mutex(Mutex::eMutexTypeRecursive); // Control access to the clang compiler |
| 267 | return g_clang_mutex; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | clang::ASTContext * |
| 272 | ClangExpression::GetASTContext () |
| 273 | { |
| 274 | CompilerInstance *compiler_instance = GetCompilerInstance(); |
| 275 | if (compiler_instance) |
| 276 | return &compiler_instance->getASTContext(); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | unsigned |
| 281 | ClangExpression::ParseExpression (const char *expr_text, Stream &stream) |
| 282 | { |
| 283 | // HACK: for now we have to make a function body around our expression |
| 284 | // since there is no way to parse a single expression line in LLVM/Clang. |
| 285 | std::string func_expr("void ___clang_expr()\n{\n\t"); |
| 286 | func_expr.append(expr_text); |
| 287 | func_expr.append(";\n}"); |
| 288 | return ParseBareExpression (func_expr, stream); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | unsigned |
| 293 | ClangExpression::ParseBareExpression (llvm::StringRef expr_text, Stream &stream) |
| 294 | { |
| 295 | Mutex::Locker locker(GetClangMutex ()); |
| 296 | |
| 297 | TextDiagnosticBuffer text_diagnostic_buffer; |
| 298 | |
| 299 | bool IsAST = false; |
| 300 | if (!CreateCompilerInstance (IsAST)) |
| 301 | { |
| 302 | stream.Printf("error: couldn't create compiler instance\n"); |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | // This code is matched below by a setClient to NULL. |
| 307 | // We cannot return out of this code without doing that. |
| 308 | m_clang_ap->getDiagnostics().setClient(&text_diagnostic_buffer); |
| 309 | text_diagnostic_buffer.FlushDiagnostics (m_clang_ap->getDiagnostics()); |
| 310 | |
| 311 | MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__); |
| 312 | |
| 313 | if (!m_clang_ap->hasSourceManager()) |
| 314 | m_clang_ap->createSourceManager(); |
| 315 | |
| 316 | m_clang_ap->createFileManager(); |
| 317 | m_clang_ap->createPreprocessor(); |
| 318 | |
| 319 | // Build the ASTContext. Most of this we inherit from the |
| 320 | // CompilerInstance, but we also want to give the context |
| 321 | // an ExternalASTSource. |
| 322 | SelectorTable selector_table; |
| 323 | std::auto_ptr<Builtin::Context> builtin_ap(new Builtin::Context(m_clang_ap->getTarget())); |
| 324 | ASTContext *Context = new ASTContext(m_clang_ap->getLangOpts(), |
| 325 | m_clang_ap->getSourceManager(), |
| 326 | m_clang_ap->getTarget(), |
| 327 | m_clang_ap->getPreprocessor().getIdentifierTable(), |
| 328 | selector_table, |
| 329 | *builtin_ap.get()); |
| 330 | |
| 331 | llvm::OwningPtr<ExternalASTSource> ASTSource(new ClangASTSource(*Context, *m_decl_map)); |
| 332 | |
| 333 | if (m_decl_map) |
| 334 | { |
| 335 | Context->setExternalSource(ASTSource); |
| 336 | } |
| 337 | |
| 338 | m_clang_ap->setASTContext(Context); |
| 339 | |
| 340 | FileID memory_buffer_file_id = m_clang_ap->getSourceManager().createMainFileIDForMemBuffer (memory_buffer); |
| 341 | std::string module_name("test_func"); |
| 342 | text_diagnostic_buffer.BeginSourceFile(m_clang_ap->getLangOpts(), &m_clang_ap->getPreprocessor()); |
| 343 | |
| 344 | if (m_code_generator_ptr) |
| 345 | delete m_code_generator_ptr; |
| 346 | |
| 347 | m_code_generator_ptr = CreateLLVMCodeGen(m_clang_ap->getDiagnostics(), |
| 348 | module_name, |
| 349 | m_clang_ap->getCodeGenOpts(), |
| 350 | m_clang_ap->getLLVMContext()); |
| 351 | |
| 352 | |
| 353 | // - CodeGeneration ASTConsumer (include/clang/ModuleBuilder.h), which will be passed in when you call... |
| 354 | // - Call clang::ParseAST (in lib/Sema/ParseAST.cpp) to parse the buffer. The CodeGenerator will generate code for __dbg_expr. |
| 355 | // - Once ParseAST completes, you can grab the llvm::Module from the CodeGenerator, which will have an llvm::Function you can hand off to the JIT. |
| 356 | ParseAST(m_clang_ap->getPreprocessor(), m_code_generator_ptr, m_clang_ap->getASTContext()); |
| 357 | |
| 358 | text_diagnostic_buffer.EndSourceFile(); |
| 359 | |
| 360 | //compiler_instance->getASTContext().getTranslationUnitDecl()->dump(); |
| 361 | |
| 362 | //if (compiler_instance->getFrontendOpts().ShowStats) { |
| 363 | // compiler_instance->getFileManager().PrintStats(); |
| 364 | // fprintf(stderr, "\n"); |
| 365 | //} |
| 366 | |
| 367 | // This code resolves the setClient above. |
| 368 | m_clang_ap->getDiagnostics().setClient(0); |
| 369 | |
| 370 | TextDiagnosticBuffer::const_iterator diag_iterator; |
| 371 | |
| 372 | int num_errors = 0; |
| 373 | |
| 374 | #ifdef COUNT_WARNINGS_AND_ERRORS |
| 375 | int num_warnings = 0; |
| 376 | |
| 377 | for (diag_iterator = text_diagnostic_buffer.warn_begin(); |
| 378 | diag_iterator != text_diagnostic_buffer.warn_end(); |
| 379 | ++diag_iterator) |
| 380 | num_warnings++; |
| 381 | |
| 382 | for (diag_iterator = text_diagnostic_buffer.err_begin(); |
| 383 | diag_iterator != text_diagnostic_buffer.err_end(); |
| 384 | ++diag_iterator) |
| 385 | num_errors++; |
| 386 | |
| 387 | if (num_warnings || num_errors) |
| 388 | { |
| 389 | if (num_warnings) |
| 390 | stream.Printf("%u warning%s%s", num_warnings, (num_warnings == 1 ? "" : "s"), (num_errors ? " and " : "")); |
| 391 | if (num_errors) |
| 392 | stream.Printf("%u error%s", num_errors, (num_errors == 1 ? "" : "s")); |
| 393 | stream.Printf("\n"); |
| 394 | } |
| 395 | #endif |
| 396 | |
| 397 | for (diag_iterator = text_diagnostic_buffer.warn_begin(); |
| 398 | diag_iterator != text_diagnostic_buffer.warn_end(); |
| 399 | ++diag_iterator) |
| 400 | stream.Printf("warning: %s\n", (*diag_iterator).second.c_str()); |
| 401 | |
| 402 | num_errors = 0; |
| 403 | |
| 404 | for (diag_iterator = text_diagnostic_buffer.err_begin(); |
| 405 | diag_iterator != text_diagnostic_buffer.err_end(); |
| 406 | ++diag_iterator) |
| 407 | { |
| 408 | num_errors++; |
| 409 | stream.Printf("error: %s\n", (*diag_iterator).second.c_str()); |
| 410 | } |
| 411 | |
| 412 | return num_errors; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | static FrontendAction * |
| 417 | CreateFrontendAction(CompilerInstance &CI) |
| 418 | { |
| 419 | // Create the underlying action. |
| 420 | FrontendAction *Act = CreateFrontendBaseAction(CI); |
| 421 | if (!Act) |
| 422 | return 0; |
| 423 | |
| 424 | // If there are any AST files to merge, create a frontend action |
| 425 | // adaptor to perform the merge. |
| 426 | if (!CI.getFrontendOpts().ASTMergeFiles.empty()) |
| 427 | Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0], |
| 428 | CI.getFrontendOpts().ASTMergeFiles.size()); |
| 429 | |
| 430 | return Act; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | unsigned |
| 435 | ClangExpression::ConvertExpressionToDWARF (ClangExpressionVariableList& expr_local_variable_list, |
| 436 | StreamString &dwarf_opcode_strm) |
| 437 | { |
| 438 | CompilerInstance *compiler_instance = GetCompilerInstance(); |
| 439 | |
| 440 | DeclarationName hack_func_name(&compiler_instance->getASTContext().Idents.get("___clang_expr")); |
| 441 | DeclContext::lookup_result result = compiler_instance->getASTContext().getTranslationUnitDecl()->lookup(hack_func_name); |
| 442 | |
| 443 | if (result.first != result.second) |
| 444 | { |
| 445 | Decl *decl = *result.first; |
| 446 | Stmt *decl_stmt = decl->getBody(); |
| 447 | if (decl_stmt) |
| 448 | { |
| 449 | ClangStmtVisitor visitor(compiler_instance->getASTContext(), expr_local_variable_list, m_decl_map, dwarf_opcode_strm); |
| 450 | |
| 451 | visitor.Visit (decl_stmt); |
| 452 | } |
| 453 | } |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | bool |
| 458 | ClangExpression::JITFunction (const ExecutionContext &exc_context, const char *name) |
| 459 | { |
| 460 | |
| 461 | llvm::Module *module = m_code_generator_ptr->GetModule(); |
| 462 | |
| 463 | if (module) |
| 464 | { |
| 465 | std::string error; |
| 466 | |
| 467 | if (m_jit_mm_ptr == NULL) |
| 468 | m_jit_mm_ptr = new RecordingMemoryManager(); |
| 469 | |
| 470 | //llvm::InitializeNativeTarget(); |
| 471 | if (m_execution_engine.get() == 0) |
| 472 | m_execution_engine.reset(llvm::ExecutionEngine::createJIT (module, &error, m_jit_mm_ptr)); |
| 473 | m_execution_engine->DisableLazyCompilation(); |
| 474 | llvm::Function *function = module->getFunction (llvm::StringRef (name)); |
| 475 | |
| 476 | // We don't actually need the function pointer here, this just forces it to get resolved. |
| 477 | void *fun_ptr = m_execution_engine->getPointerToFunction(function); |
| 478 | // Note, you probably won't get here on error, since the LLVM JIT tends to just |
| 479 | // exit on error at present... So be careful. |
| 480 | if (fun_ptr == 0) |
| 481 | return false; |
| 482 | m_jitted_functions.push_back(ClangExpression::JittedFunction(name, (lldb::addr_t) fun_ptr)); |
| 483 | |
| 484 | } |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | bool |
| 489 | ClangExpression::WriteJITCode (const ExecutionContext &exc_context) |
| 490 | { |
| 491 | if (m_jit_mm_ptr == NULL) |
| 492 | return false; |
| 493 | |
| 494 | if (exc_context.process == NULL) |
| 495 | return false; |
| 496 | |
| 497 | // Look over the regions allocated for the function compiled. The JIT |
| 498 | // tries to allocate the functions & stubs close together, so we should try to |
| 499 | // write them that way too... |
| 500 | // For now I only write functions with no stubs, globals, exception tables, |
| 501 | // etc. So I only need to write the functions. |
| 502 | |
| 503 | size_t size = 0; |
| 504 | std::map<uint8_t *, uint8_t *>::iterator fun_pos, fun_end = m_jit_mm_ptr->m_functions.end(); |
| 505 | for (fun_pos = m_jit_mm_ptr->m_functions.begin(); fun_pos != fun_end; fun_pos++) |
| 506 | { |
| 507 | size += (*fun_pos).second - (*fun_pos).first; |
| 508 | } |
| 509 | |
| 510 | Error error; |
| 511 | lldb::addr_t target_addr = exc_context.process->AllocateMemory (size, lldb::ePermissionsReadable|lldb::ePermissionsExecutable, error); |
| 512 | |
| 513 | if (target_addr == LLDB_INVALID_ADDRESS) |
| 514 | return false; |
| 515 | |
| 516 | lldb::addr_t cursor = target_addr; |
| 517 | for (fun_pos = m_jit_mm_ptr->m_functions.begin(); fun_pos != fun_end; fun_pos++) |
| 518 | { |
| 519 | lldb::addr_t lstart = (lldb::addr_t) (*fun_pos).first; |
| 520 | lldb::addr_t lend = (lldb::addr_t) (*fun_pos).second; |
| 521 | size_t size = lend - lstart; |
| 522 | exc_context.process->WriteMemory(cursor, (void *) lstart, size, error); |
| 523 | m_jit_mm_ptr->AddToLocalToRemoteMap (lstart, size, cursor); |
| 524 | cursor += size; |
| 525 | } |
| 526 | |
| 527 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 528 | |
| 529 | for (pos = m_jitted_functions.begin(); pos != end; pos++) |
| 530 | { |
| 531 | (*pos).m_remote_addr = m_jit_mm_ptr->GetRemoteAddressForLocal ((*pos).m_local_addr); |
| 532 | } |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | lldb::addr_t |
| 537 | ClangExpression::GetFunctionAddress (const char *name) |
| 538 | { |
| 539 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 540 | |
| 541 | for (pos = m_jitted_functions.begin(); pos < end; pos++) |
| 542 | { |
| 543 | if (strcmp ((*pos).m_name.c_str(), name) == 0) |
| 544 | return (*pos).m_remote_addr; |
| 545 | } |
| 546 | return LLDB_INVALID_ADDRESS; |
| 547 | } |
| 548 | |
| 549 | unsigned |
| 550 | ClangExpression::Compile() |
| 551 | { |
| 552 | Mutex::Locker locker(GetClangMutex ()); |
| 553 | bool IsAST = false; |
| 554 | |
| 555 | if (CreateCompilerInstance(IsAST)) |
| 556 | { |
| 557 | // Validate/process some options |
| 558 | if (m_clang_ap->getHeaderSearchOpts().Verbose) |
| 559 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 560 | << " based upon " << PACKAGE_STRING |
| 561 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 562 | |
| 563 | // Enforce certain implications. |
| 564 | if (!m_clang_ap->getFrontendOpts().ViewClassInheritance.empty()) |
| 565 | m_clang_ap->getFrontendOpts().ProgramAction = frontend::InheritanceView; |
| 566 | // if (!compiler_instance->getFrontendOpts().FixItSuffix.empty()) |
| 567 | // compiler_instance->getFrontendOpts().ProgramAction = frontend::FixIt; |
| 568 | |
| 569 | for (unsigned i = 0, e = m_clang_ap->getFrontendOpts().Inputs.size(); i != e; ++i) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 570 | |
| 571 | // If we aren't using an AST file, setup the file and source managers and |
| 572 | // the preprocessor. |
| 573 | if (!IsAST) { |
| 574 | if (!i) { |
| 575 | // Create a file manager object to provide access to and cache the |
| 576 | // filesystem. |
| 577 | m_clang_ap->createFileManager(); |
| 578 | |
| 579 | // Create the source manager. |
| 580 | m_clang_ap->createSourceManager(); |
| 581 | } else { |
| 582 | // Reset the ID tables if we are reusing the SourceManager. |
| 583 | m_clang_ap->getSourceManager().clearIDTables(); |
| 584 | } |
| 585 | |
| 586 | // Create the preprocessor. |
| 587 | m_clang_ap->createPreprocessor(); |
| 588 | } |
| 589 | |
| 590 | llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(*m_clang_ap.get())); |
| 591 | if (!Act) |
| 592 | break; |
| 593 | |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame^] | 594 | if (Act->BeginSourceFile(*m_clang_ap, |
| 595 | m_clang_ap->getFrontendOpts().Inputs[i].second, |
| 596 | m_clang_ap->getFrontendOpts().Inputs[i].first)) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | Act->Execute(); |
| 598 | Act->EndSourceFile(); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (m_clang_ap->getDiagnosticOpts().ShowCarets) |
| 603 | { |
| 604 | unsigned NumWarnings = m_clang_ap->getDiagnostics().getNumWarnings(); |
| 605 | unsigned NumErrors = m_clang_ap->getDiagnostics().getNumErrors() - |
| 606 | m_clang_ap->getDiagnostics().getNumErrorsSuppressed(); |
| 607 | |
| 608 | if (NumWarnings || NumErrors) |
| 609 | { |
| 610 | if (NumWarnings) |
| 611 | fprintf (stderr, "%u warning%s%s", NumWarnings, (NumWarnings == 1 ? "" : "s"), (NumErrors ? " and " : "")); |
| 612 | if (NumErrors) |
| 613 | fprintf (stderr, "%u error%s", NumErrors, (NumErrors == 1 ? "" : "s")); |
| 614 | fprintf (stderr, " generated.\n"); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (m_clang_ap->getFrontendOpts().ShowStats) { |
| 619 | m_clang_ap->getFileManager().PrintStats(); |
| 620 | fprintf(stderr, "\n"); |
| 621 | } |
| 622 | |
| 623 | // Return the appropriate status when verifying diagnostics. |
| 624 | // |
| 625 | // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need |
| 626 | // this. |
| 627 | if (m_clang_ap->getDiagnosticOpts().VerifyDiagnostics) |
| 628 | return static_cast<VerifyDiagnosticsClient&>(m_clang_ap->getDiagnosticClient()).HadErrors(); |
| 629 | |
| 630 | return m_clang_ap->getDiagnostics().getNumErrors(); |
| 631 | } |
| 632 | return 1; |
| 633 | } |