| Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 1 | //===-- llvm/LLVMContext.h - Class for managing "global" state --*- 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 | //===----------------------------------------------------------------------===// |
| Owen Anderson | 5217007 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file declares LLVMContext, a container of "global" state in LLVM, such |
| 11 | // as the global type and constant uniquing tables. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 14 | |
| Jakub Staszak | 674be02 | 2013-01-10 00:45:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_IR_LLVMCONTEXT_H |
| 16 | #define LLVM_IR_LLVMCONTEXT_H |
| Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 17 | |
| Filip Pizlo | 40be1e8 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CBindingWrapping.h" |
| Craig Topper | 9f9ce61 | 2012-09-17 07:16:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
| Filip Pizlo | 40be1e8 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 20 | #include "llvm-c/Core.h" |
| Craig Topper | 9f9ce61 | 2012-09-17 07:16:40 +0000 | [diff] [blame] | 21 | |
| Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | |
| Benjamin Kramer | 12ddd40 | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 24 | class LLVMContextImpl; |
| Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 25 | class StringRef; |
| Chris Lattner | 3a4c60c | 2012-01-03 23:47:05 +0000 | [diff] [blame] | 26 | class Twine; |
| Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 27 | class Instruction; |
| Owen Anderson | 30268be | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 28 | class Module; |
| Chris Lattner | 4afa128 | 2010-11-17 08:13:01 +0000 | [diff] [blame] | 29 | class SMDiagnostic; |
| Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 30 | template <typename T> class SmallVectorImpl; |
| Dan Gohman | 82a47e9 | 2009-10-06 17:43:57 +0000 | [diff] [blame] | 31 | |
| Owen Anderson | 5217007 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 32 | /// This is an important class for using LLVM in a threaded context. It |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 33 | /// (opaquely) owns and manages the core "global" data of LLVM's core |
| Owen Anderson | 5217007 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 34 | /// infrastructure, including the type and constant uniquing tables. |
| 35 | /// LLVMContext itself provides no locking guarantees, so you should be careful |
| 36 | /// to have one context per thread. |
| Benjamin Kramer | 12ddd40 | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 37 | class LLVMContext { |
| Owen Anderson | 1e8d5d2 | 2010-09-08 18:22:11 +0000 | [diff] [blame] | 38 | public: |
| 39 | LLVMContextImpl *const pImpl; |
| 40 | LLVMContext(); |
| 41 | ~LLVMContext(); |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 42 | |
| Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 43 | // Pinned metadata names, which always have the same value. This is a |
| 44 | // compile-time performance optimization, not a correctness optimization. |
| 45 | enum { |
| Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 46 | MD_dbg = 0, // "dbg" |
| Jakub Staszak | 9da9934 | 2011-07-06 18:22:43 +0000 | [diff] [blame] | 47 | MD_tbaa = 1, // "tbaa" |
| Peter Collingbourne | 999f90b | 2011-10-27 19:19:14 +0000 | [diff] [blame] | 48 | MD_prof = 2, // "prof" |
| Duncan Sands | 5e5c5f8 | 2012-04-14 12:36:06 +0000 | [diff] [blame] | 49 | MD_fpmath = 3, // "fpmath" |
| Dan Gohman | b54834b | 2012-09-13 17:56:17 +0000 | [diff] [blame] | 50 | MD_range = 4, // "range" |
| Shuxin Yang | 985dac6 | 2013-03-06 17:48:48 +0000 | [diff] [blame] | 51 | MD_tbaa_struct = 5, // "tbaa.struct" |
| 52 | MD_invariant_load = 6 // "invariant.load" |
| Chris Lattner | ec39f09 | 2010-03-30 23:03:27 +0000 | [diff] [blame] | 53 | }; |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 54 | |
| Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 55 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 56 | /// This ID is uniqued across modules in the current LLVMContext. |
| 57 | unsigned getMDKindID(StringRef Name) const; |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 58 | |
| Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 59 | /// getMDKindNames - Populate client supplied SmallVector with the name for |
| Dan Gohman | 5d80911 | 2010-07-20 21:45:17 +0000 | [diff] [blame] | 60 | /// custom metadata IDs registered in this LLVMContext. |
| Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 61 | void getMDKindNames(SmallVectorImpl<StringRef> &Result) const; |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 62 | |
| 63 | |
| Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 64 | typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context, |
| 65 | unsigned LocCookie); |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 66 | |
| Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 67 | /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked |
| 68 | /// when problems with inline asm are detected by the backend. The first |
| 69 | /// argument is a function pointer and the second is a context pointer that |
| 70 | /// gets passed into the DiagHandler. |
| Chris Lattner | 42a4ee0 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 71 | /// |
| Chris Lattner | 4afa128 | 2010-11-17 08:13:01 +0000 | [diff] [blame] | 72 | /// LLVMContext doesn't take ownership or interpret either of these |
| Chris Lattner | 42a4ee0 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 73 | /// pointers. |
| Bob Wilson | 04de315 | 2012-12-25 00:07:12 +0000 | [diff] [blame] | 74 | void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, |
| Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 75 | void *DiagContext = 0); |
| 76 | |
| 77 | /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by |
| 78 | /// setInlineAsmDiagnosticHandler. |
| 79 | InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const; |
| 80 | |
| 81 | /// getInlineAsmDiagnosticContext - Return the diagnostic context set by |
| 82 | /// setInlineAsmDiagnosticHandler. |
| 83 | void *getInlineAsmDiagnosticContext() const; |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 84 | |
| 85 | |
| Chris Lattner | 38686bd | 2010-04-07 23:40:44 +0000 | [diff] [blame] | 86 | /// emitError - Emit an error message to the currently installed error handler |
| 87 | /// with optional location information. This function returns, so code should |
| 88 | /// be prepared to drop the erroneous construct on the floor and "not crash". |
| 89 | /// The generated code need not be correct. The error message will be |
| 90 | /// implicitly prefixed with "error: " and should not end with a ".". |
| Chris Lattner | 3a4c60c | 2012-01-03 23:47:05 +0000 | [diff] [blame] | 91 | void emitError(unsigned LocCookie, const Twine &ErrorStr); |
| 92 | void emitError(const Instruction *I, const Twine &ErrorStr); |
| 93 | void emitError(const Twine &ErrorStr); |
| Owen Anderson | eb92330 | 2010-09-08 18:41:07 +0000 | [diff] [blame] | 94 | |
| 95 | private: |
| Craig Topper | 9f9ce61 | 2012-09-17 07:16:40 +0000 | [diff] [blame] | 96 | LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION; |
| 97 | void operator=(LLVMContext&) LLVM_DELETED_FUNCTION; |
| Owen Anderson | eb92330 | 2010-09-08 18:41:07 +0000 | [diff] [blame] | 98 | |
| 99 | /// addModule - Register a module as being instantiated in this context. If |
| 100 | /// the context is deleted, the module will be deleted as well. |
| 101 | void addModule(Module*); |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 102 | |
| Owen Anderson | eb92330 | 2010-09-08 18:41:07 +0000 | [diff] [blame] | 103 | /// removeModule - Unregister a module from this context. |
| 104 | void removeModule(Module*); |
| Jim Grosbach | 4e505a4 | 2013-05-24 22:53:06 +0000 | [diff] [blame^] | 105 | |
| Owen Anderson | eb92330 | 2010-09-08 18:41:07 +0000 | [diff] [blame] | 106 | // Module needs access to the add/removeModule methods. |
| 107 | friend class Module; |
| Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 110 | /// getGlobalContext - Returns a global context. This is for LLVM clients that |
| 111 | /// only care about operating on a single thread. |
| 112 | extern LLVMContext &getGlobalContext(); |
| Owen Anderson | db47ed0 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 113 | |
| Filip Pizlo | 40be1e8 | 2013-05-01 20:59:00 +0000 | [diff] [blame] | 114 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 115 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef) |
| 116 | |
| 117 | /* Specialized opaque context conversions. |
| 118 | */ |
| 119 | inline LLVMContext **unwrap(LLVMContextRef* Tys) { |
| 120 | return reinterpret_cast<LLVMContext**>(Tys); |
| 121 | } |
| 122 | |
| 123 | inline LLVMContextRef *wrap(const LLVMContext **Tys) { |
| 124 | return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys)); |
| 125 | } |
| 126 | |
| Owen Anderson | 2bc29dc | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | #endif |