llvm.org GIT mirror llvm / 31895e7
Hold the LLVMContext by reference rather than by pointer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74640 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
55 changed file(s) with 108 addition(s) and 99 deletion(s). Raw diff Collapse all Expand all
3636 const char *BrainF::testreg = "test";
3737
3838 Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf,
39 LLVMContext* Context) {
39 const LLVMContext& Context) {
4040 in = in1;
4141 memtotal = mem;
4242 comflag = cf;
4747 return module;
4848 }
4949
50 void BrainF::header(LLVMContext* C) {
50 void BrainF::header(const LLVMContext& C) {
5151 module = new Module("BrainF", C);
5252
5353 //Function prototypes
3838 /// containing the resulting code.
3939 /// On error, it calls abort.
4040 /// The caller must delete the returned module.
41 Module *parse(std::istream *in1, int mem, CompileFlags cf, LLVMContext* C);
41 Module *parse(std::istream *in1, int mem, CompileFlags cf,
42 const LLVMContext& C);
4243
4344 protected:
4445 /// The different symbols in the BrainF language
6465 static const char *testreg;
6566
6667 /// Put the brainf function preamble and other fixed pieces of code
67 void header(LLVMContext* C);
68 void header(const LLVMContext& C);
6869
6970 /// The main loop for parsing. It calls itself recursively
7071 /// to handle the depth of nesting of "[]".
125125
126126 //Read the BrainF program
127127 BrainF bf;
128 Module *mod = bf.parse(in, 65536, cf, &Context); //64 KiB
128 Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
129129 if (in != &std::cin) {delete in;}
130130 addMainFunction(mod);
131131
9393 LLVMContext Context;
9494
9595 // Create some module to put our function into it.
96 Module *M = new Module("test", &Context);
96 Module *M = new Module("test", Context);
9797
9898 // We are about to create the "fib" function:
9999 Function *FibF = CreateFibFunction(M);
5454 LLVMContext Context;
5555
5656 // Create some module to put our function into it.
57 Module *M = new Module("test", &Context);
57 Module *M = new Module("test", Context);
5858
5959 // Create the add1 function entry and insert this entry into module M. The
6060 // function will have a return type of "int" and take an argument of "int".
10981098 getNextToken();
10991099
11001100 // Make the module, which holds all the code.
1101 TheModule = new Module("my cool jit", &Context);
1101 TheModule = new Module("my cool jit", Context);
11021102
11031103 // Create the JIT.
11041104 TheExecutionEngine = ExecutionEngine::create(TheModule);
2626
2727 // Create the "module" or "program" or "translation unit" to hold the
2828 // function
29 Module *M = new Module("test", &Context);
29 Module *M = new Module("test", Context);
3030
3131 // Create the main function: first create the type 'int ()'
3232 FunctionType *FT = FunctionType::get(Type::Int32Ty, /*not vararg*/false);
235235 LLVMContext Context;
236236
237237 // Create some module to put our function into it.
238 Module *M = new Module("test", &Context);
238 Module *M = new Module("test", Context);
239239
240240 Function* add1F = createAdd1( M );
241241 Function* fibF = CreateFibFunction( M );
3131 Module *ParseAssemblyFile(
3232 const std::string &Filename, ///< The name of the file to parse
3333 ParseError &Error, ///< If not null, an object to return errors in.
34 LLVMContext* Context ///< Context in which to allocate globals info.
34 const LLVMContext& Context ///< Context in which to allocate globals info.
3535 );
3636
3737 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
4444 const char *AsmString, ///< The string containing assembly
4545 Module *M, ///< A module to add the assembly too.
4646 ParseError &Error, ///< If not null, an object to return errors in.
47 LLVMContext* Context
47 const LLVMContext& Context
4848 );
4949
5050 //===------------------------------------------------------------------------===
279279 /// @brief Create an empty Archive.
280280 static Archive* CreateEmpty(
281281 const sys::Path& Filename,///< Name of the archive to (eventually) create.
282 LLVMContext* C ///< Context to use for global information
282 const LLVMContext& C ///< Context to use for global information
283283 );
284284
285285 /// Open an existing archive and load its contents in preparation for
290290 /// @brief Open and load an archive file
291291 static Archive* OpenAndLoad(
292292 const sys::Path& filePath, ///< The file path to open and load
293 LLVMContext* C, ///< The context to use for global information
293 const LLVMContext& C, ///< The context to use for global information
294294 std::string* ErrorMessage ///< An optional error string
295295 );
296296
312312 /// @brief Open an existing archive and load its symbols.
313313 static Archive* OpenAndLoadSymbols(
314314 const sys::Path& Filename, ///< Name of the archive file to open
315 LLVMContext* C, ///< The context to use for global info
315 const LLVMContext& C, ///< The context to use for global info
316316 std::string* ErrorMessage=0 ///< An optional error string
317317 );
318318
452452 protected:
453453 /// @brief Construct an Archive for \p filename and optionally map it
454454 /// into memory.
455 explicit Archive(const sys::Path& filename, LLVMContext* C);
455 explicit Archive(const sys::Path& filename, const LLVMContext& C);
456456
457457 /// @param data The symbol table data to be parsed
458458 /// @param len The length of the symbol table data
533533 unsigned firstFileOffset; ///< Offset to first normal file.
534534 ModuleMap modules; ///< The modules loaded via symbol lookup.
535535 ArchiveMember* foreignST; ///< This holds the foreign symbol table.
536 LLVMContext* Context; ///< This holds global data.
536 const LLVMContext& Context; ///< This holds global data.
537537 /// @}
538538 /// @name Hidden
539539 /// @{
3131 /// error, this returns null, *does not* take ownership of Buffer, and fills
3232 /// in *ErrMsg with an error description if ErrMsg is non-null.
3333 ModuleProvider *getBitcodeModuleProvider(MemoryBuffer *Buffer,
34 LLVMContext* Context,
34 const LLVMContext& Context,
3535 std::string *ErrMsg = 0);
3636
3737 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
3838 /// If an error occurs, this returns null and fills in *ErrMsg if it is
3939 /// non-null. This method *never* takes ownership of Buffer.
40 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext* Context,
40 Module *ParseBitcodeFile(MemoryBuffer *Buffer, const LLVMContext& Context,
4141 std::string *ErrMsg = 0);
4242
4343 /// WriteBitcodeToFile - Write the specified module to the specified output
9595 /// the PATH for the specified program, loading it when found. If the
9696 /// specified program cannot be found, an exception is thrown to indicate
9797 /// the error.
98 void loadProgram(const std::string &Path, LLVMContext* Context);
98 void loadProgram(const std::string &Path, const LLVMContext& Context);
9999
100100 /// unloadProgram - If a program is running, kill it, then unload all traces
101101 /// of the current program. If no program is loaded, this method silently
197197 };
198198
199199 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
200 LLVMContext* getGlobalContext();
200 extern const LLVMContext& getGlobalContext();
201201
202202 }
203203
1515 #ifndef LLVM_LINKALLVMCORE_H
1616 #define LLVM_LINKALLVMCORE_H
1717
18 #include "llvm/LLVMContext.h"
1819 #include "llvm/Module.h"
1920 #include "llvm/Instructions.h"
2021 #include "llvm/IntrinsicInst.h"
4344 // to know that getenv() never returns -1, this will do the job.
4445 if (std::getenv("bar") != (char*) -1)
4546 return;
46 llvm::Module* M = new llvm::Module("", 0);
47 llvm::Module* M = new llvm::Module("", llvm::getGlobalContext());
4748 (void)new llvm::UnreachableInst();
4849 (void) llvm::createVerifierPass();
4950 (void) new llvm::Mangler(*M,"");
6666 Linker(
6767 const std::string& progname, ///< name of tool running linker
6868 const std::string& modulename, ///< name of linker's end-result module
69 LLVMContext* C, ///< Context for global info
69 const LLVMContext& C, ///< Context for global info
7070 unsigned Flags = 0 ///< ControlFlags (one or more |'d together)
7171 );
7272
284284 /// @name Data
285285 /// @{
286286 private:
287 LLVMContext* Context; ///< The context for global information
287 const LLVMContext& Context; ///< The context for global information
288288 Module* Composite; ///< The composite module linked together
289289 std::vector LibPaths; ///< The library search paths
290290 unsigned Flags; ///< Flags to control optional behavior.
109109 /// @name Member Variables
110110 /// @{
111111 private:
112 LLVMContext* Context; ///< The LLVMContext from which types and
112 const LLVMContext& Context; ///< The LLVMContext from which types and
113113 ///< constants are allocated.
114114 GlobalListType GlobalList; ///< The Global Variables in the module
115115 FunctionListType FunctionList; ///< The Functions in the module
130130 public:
131131 /// The Module constructor. Note that there is no default constructor. You
132132 /// must provide a name for the module upon construction.
133 explicit Module(const std::string &ModuleID, LLVMContext* C);
133 explicit Module(const std::string &ModuleID, const LLVMContext& C);
134134 /// The module destructor. This will dropAllReferences.
135135 ~Module();
136136
161161
162162 /// Get the global data context.
163163 /// @returns LLVMContext - a container for LLVM's global information
164 LLVMContext* getContext() const { return Context; }
164 const LLVMContext& getContext() const { return Context; }
165165
166166 /// Get any module-scope inline assembly blocks.
167167 /// @returns a string containing the module-scope inline assembly blocks.
5757 /** opaque reference to a code generator */
5858 typedef struct LTOCodeGenerator* lto_code_gen_t;
5959
60 typedef struct LTOContext* lto_context_t;
6061
6162 #ifdef __cplusplus
6263 extern "C" {
7475 */
7576 extern const char*
7677 lto_get_error_message(void);
77
7878
7979 /**
8080 * Checks if a file is a loadable object file.
137137 // Archive constructor - this is the only constructor that gets used for the
138138 // Archive class. Everything else (default,copy) is deprecated. This just
139139 // initializes and maps the file into memory, if requested.
140 Archive::Archive(const sys::Path& filename, LLVMContext* C)
140 Archive::Archive(const sys::Path& filename, const LLVMContext& C)
141141 : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
142142 symTabSize(0), firstFileOffset(0), modules(), foreignST(0), Context(C) {
143143 }
207207
208208 // Get just the externally visible defined symbols from the bitcode
209209 bool llvm::GetBitcodeSymbols(const sys::Path& fName,
210 LLVMContext* Context,
210 const LLVMContext& Context,
211211 std::vector& symbols,
212212 std::string* ErrMsg) {
213213 std::auto_ptr Buffer(
239239 ModuleProvider*
240240 llvm::GetBitcodeSymbols(const unsigned char *BufPtr, unsigned Length,
241241 const std::string& ModuleID,
242 LLVMContext* Context,
242 const LLVMContext& Context,
243243 std::vector& symbols,
244244 std::string* ErrMsg) {
245245 // Get the module provider
7272
7373 // Get just the externally visible defined symbols from the bitcode
7474 bool GetBitcodeSymbols(const sys::Path& fName,
75 LLVMContext* Context,
75 const LLVMContext& Context,
7676 std::vector& symbols,
7777 std::string* ErrMsg);
7878
7979 ModuleProvider* GetBitcodeSymbols(const unsigned char*Buffer,unsigned Length,
8080 const std::string& ModuleID,
81 LLVMContext* Context,
81 const LLVMContext& Context,
8282 std::vector& symbols,
8383 std::string* ErrMsg);
8484 }
326326
327327 // Open and completely load the archive file.
328328 Archive*
329 Archive::OpenAndLoad(const sys::Path& file, LLVMContext* C,
329 Archive::OpenAndLoad(const sys::Path& file, const LLVMContext& C,
330330 std::string* ErrorMessage) {
331331 std::auto_ptr result ( new Archive(file, C));
332332 if (result->mapToMemory(ErrorMessage))
440440 }
441441
442442 // Open the archive and load just the symbol tables
443 Archive* Archive::OpenAndLoadSymbols(const sys::Path& file, LLVMContext* C,
443 Archive* Archive::OpenAndLoadSymbols(const sys::Path& file,
444 const LLVMContext& C,
444445 std::string* ErrorMessage) {
445446 std::auto_ptr result ( new Archive(file, C) );
446447 if (result->mapToMemory(ErrorMessage))
6363 }
6464
6565 // Create an empty archive.
66 Archive* Archive::CreateEmpty(const sys::Path& FilePath, LLVMContext* C) {
66 Archive* Archive::CreateEmpty(const sys::Path& FilePath, const LLVMContext& C) {
6767 Archive* result = new Archive(FilePath, C);
6868 return result;
6969 }
2020 using namespace llvm;
2121
2222 Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err,
23 LLVMContext* Context) {
23 const LLVMContext& Context) {
2424 Err.setFilename(Filename);
2525
2626 std::string ErrorStr;
3838 }
3939
4040 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
41 ParseError &Err, LLVMContext* Context) {
41 ParseError &Err, const LLVMContext& Context) {
4242 Err.setFilename("");
4343
4444 OwningPtr
2121 LLVMModuleRef *OutModule, char **OutMessage) {
2222 std::string Message;
2323
24 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), unwrap(ContextRef),
24 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef),
2525 &Message));
2626 if (!*OutModule) {
2727 if (OutMessage)
4141 char **OutMessage) {
4242 std::string Message;
4343
44 *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), unwrap(ContextRef),
44 *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), *unwrap(ContextRef),
4545 &Message));
4646 if (!*OutMP) {
4747 if (OutMessage)
20892089 /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file.
20902090 ///
20912091 ModuleProvider *llvm::getBitcodeModuleProvider(MemoryBuffer *Buffer,
2092 LLVMContext* Context,
2092 const LLVMContext& Context,
20932093 std::string *ErrMsg) {
20942094 BitcodeReader *R = new BitcodeReader(Buffer, Context);
20952095 if (R->ParseBitcode()) {
21062106
21072107 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
21082108 /// If an error occurs, return null and fill in *ErrMsg if non-null.
2109 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext* Context,
2109 Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, const LLVMContext& Context,
21102110 std::string *ErrMsg){
21112111 BitcodeReader *R;
21122112 R = static_cast(getBitcodeModuleProvider(Buffer, Context,
8585 };
8686
8787 class BitcodeReader : public ModuleProvider {
88 LLVMContext* Context;
88 const LLVMContext& Context;
8989 MemoryBuffer *Buffer;
9090 BitstreamReader StreamFile;
9191 BitstreamCursor Stream;
124124 /// stream) and what linkage the original function had.
125125 DenseMap > DeferredFunctionInfo;
126126 public:
127 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext* C)
127 explicit BitcodeReader(MemoryBuffer *buffer, const LLVMContext& C)
128128 : Context(C), Buffer(buffer), ErrorString(0) {
129129 HasReversedFunctionsWithBodies = false;
130130 }
4545 }
4646
4747 static Module *
48 getMaterializedModuleProvider(const std::string &Filename, LLVMContext* C) {
48 getMaterializedModuleProvider(const std::string &Filename,
49 const LLVMContext& C) {
4950 std::auto_ptr Buffer;
5051 Buffer.reset(MemoryBuffer::getFileOrSTDIN(Filename.c_str()));
5152 if (Buffer.get())
5758 /// the PATH for the specified program, loading it when found. If the
5859 /// specified program cannot be found, an exception is thrown to indicate the
5960 /// error.
60 void Debugger::loadProgram(const std::string &Filename, LLVMContext* C) {
61 void Debugger::loadProgram(const std::string &Filename, const LLVMContext& C) {
6162 if ((Program = getMaterializedModuleProvider(Filename, C)) ||
6263 (Program = getMaterializedModuleProvider(Filename+".bc", C)))
6364 return; // Successfully loaded the program.
1919 using namespace llvm;
2020
2121 Linker::Linker(const std::string& progname, const std::string& modname,
22 LLVMContext* C, unsigned flags):
22 const LLVMContext& C, unsigned flags):
2323 Context(C),
2424 Composite(new Module(modname, C)),
2525 LibPaths(),
5252 /*===-- Operations on modules ---------------------------------------------===*/
5353
5454 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID, LLVMContextRef C) {
55 return wrap(new Module(ModuleID, unwrap(C)));
55 return wrap(new Module(ModuleID, *unwrap(C)));
5656 }
5757
5858 void LLVMDisposeModule(LLVMModuleRef M) {
2121
2222 static ManagedStatic GlobalContext;
2323
24 LLVMContext* getGlobalContext() {
25 return &*GlobalContext;
24 const LLVMContext& llvm::getGlobalContext() {
25 return *GlobalContext;
2626 }
2727
2828 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
5454 // Primitive Module methods.
5555 //
5656
57 Module::Module(const std::string &MID, LLVMContext* C)
57 Module::Module(const std::string &MID, const LLVMContext& C)
5858 : Context(C), ModuleID(MID), DataLayout("") {
5959 ValSymTab = new ValueSymbolTable();
6060 TypeSymTab = new TypeSymbolTable();
6363 }
6464
6565 BugDriver::BugDriver(const char *toolname, bool as_child, bool find_bugs,
66 unsigned timeout, unsigned memlimit, LLVMContext* ctxt)
66 unsigned timeout, unsigned memlimit,
67 const LLVMContext& ctxt)
6768 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile),
6869 Program(0), Interpreter(0), SafeInterpreter(0), gcc(0),
6970 run_as_child(as_child), run_find_bugs(find_bugs), Timeout(timeout),
7374 /// ParseInputFile - Given a bitcode or assembly input filename, parse and
7475 /// return it, or return null if not possible.
7576 ///
76 Module *llvm::ParseInputFile(const std::string &Filename, LLVMContext* Ctxt) {
77 Module *llvm::ParseInputFile(const std::string &Filename,
78 const LLVMContext& Ctxt) {
7779 std::auto_ptr Buffer(MemoryBuffer::getFileOrSTDIN(Filename));
7880 Module *Result = 0;
7981 if (Buffer.get())
4242 extern bool BugpointIsInterrupted;
4343
4444 class BugDriver {
45 LLVMContext* Context;
45 const LLVMContext& Context;
4646 const std::string ToolName; // Name of bugpoint
4747 std::string ReferenceOutputFile; // Name of `good' output file
4848 Module *Program; // The raw program, linked together
6161
6262 public:
6363 BugDriver(const char *toolname, bool as_child, bool find_bugs,
64 unsigned timeout, unsigned memlimit, LLVMContext* ctxt);
64 unsigned timeout, unsigned memlimit, const LLVMContext& ctxt);
6565
6666 const std::string &getToolName() const { return ToolName; }
6767
68 LLVMContext* getContext() { return Context; }
68 const LLVMContext& getContext() { return Context; }
6969
7070 // Set up methods... these methods are used to copy information about the
7171 // command line arguments into instance variables of BugDriver.
293293 /// ParseInputFile - Given a bitcode or assembly input filename, parse and
294294 /// return it, or return null if not possible.
295295 ///
296 Module *ParseInputFile(const std::string &InputFilename, LLVMContext* ctxt);
296 Module *ParseInputFile(const std::string &InputFilename,
297 const LLVMContext& ctxt);
297298
298299
299300 /// getPassesString - Turn a list of passes into a string which indicates the
7575 sys::SetInterruptFunction(BugpointInterruptFunction);
7676
7777 LLVMContext Context;
78 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit, &Context);
78 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit, Context);
7979 if (D.addSources(InputFilenames)) return 1;
8080 D.addPasses(PassList.begin(), PassList.end());
8181
226226 std::auto_ptr Buffer(
227227 MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage));
228228 if (Buffer.get())
229 M.reset(ParseBitcodeFile(Buffer.get(), &Context, &ErrorMessage));
229 M.reset(ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage));
230230 if (M.get() == 0) {
231231 std::cerr << argv[0] << ": bitcode didn't read correctly.\n";
232232 std::cerr << "Reason: " << ErrorMessage << "\n";
106106 std::string ErrorMsg;
107107 ModuleProvider *MP = NULL;
108108 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFile,&ErrorMsg)){
109 MP = getBitcodeModuleProvider(Buffer, &Context, &ErrorMsg);
109 MP = getBitcodeModuleProvider(Buffer, Context, &ErrorMsg);
110110 if (!MP) delete Buffer;
111111 }
112112
718718 // Produce a warning if we should and we're creating the archive
719719 if (!Create)
720720 std::cerr << argv[0] << ": creating " << ArchivePath.toString() << "\n";
721 TheArchive = Archive::CreateEmpty(ArchivePath, &Context);
721 TheArchive = Archive::CreateEmpty(ArchivePath, Context);
722722 TheArchive->writeToDisk();
723723 } else {
724724 std::string Error;
725 TheArchive = Archive::OpenAndLoad(ArchivePath, &Context, &Error);
725 TheArchive = Archive::OpenAndLoad(ArchivePath, Context, &Error);
726726 if (TheArchive == 0) {
727727 std::cerr << argv[0] << ": error loading '" << ArchivePath << "': "
728728 << Error << "!\n";
6464 try {
6565 // Parse the file now...
6666 ParseError Err;
67 std::auto_ptr M(ParseAssemblyFile(InputFilename, Err, &Context));
67 std::auto_ptr M(ParseAssemblyFile(InputFilename, Err, Context));
6868 if (M.get() == 0) {
6969 Err.PrintError(argv[0], errs());
7070 return 1;
2121 /// CLIDebugger constructor - This initializes the debugger to its default
2222 /// state, and initializes the command table.
2323 ///
24 CLIDebugger::CLIDebugger(LLVMContext* ctxt)
24 CLIDebugger::CLIDebugger(const LLVMContext& ctxt)
2525 : Context(ctxt), TheProgramInfo(0), TheRuntimeInfo(0),
2626 Prompt("(llvm-db) "), ListSize(10) {
2727 // Initialize instance variables
2828 /// CLIDebugger - This class implements the command line interface for the
2929 /// LLVM debugger.
3030 class CLIDebugger {
31 LLVMContext* Context;
31 const LLVMContext& Context;
3232
3333 /// Dbg - The low-level LLVM debugger object that we use to do our dirty
3434 /// work.
8181 const SourceLanguage *CurrentLanguage;
8282
8383 public:
84 CLIDebugger(LLVMContext* ctxt);
84 CLIDebugger(const LLVMContext& ctxt);
8585
8686 /// getDebugger - Return the current LLVM debugger implementation being
8787 /// used.
6969 InputArgs.push_back(InputFile);
7070
7171 // Create the CLI debugger...
72 CLIDebugger D(&Context);
72 CLIDebugger D(Context);
7373
7474 // Initialize the debugger with the command line options we read...
7575 Debugger &Dbg = D.getDebugger();
6262
6363 if (MemoryBuffer *Buffer
6464 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
65 M.reset(ParseBitcodeFile(Buffer, &Context, &ErrorMessage));
65 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
6666 delete Buffer;
6767 }
6868
7272 cerr << argv[0] << ": Error reading file '" + InputFilename + "'\n";
7373 return 1;
7474 } else {
75 M.reset(ParseBitcodeFile(Buffer, &Context));
75 M.reset(ParseBitcodeFile(Buffer, Context));
7676 }
7777 delete Buffer;
7878
516516 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
517517
518518 // Construct a Linker (now that Verbose is set)
519 Linker TheLinker(progname, OutputFilename, &Context, Verbose);
519 Linker TheLinker(progname, OutputFilename, Context, Verbose);
520520
521521 // Keep track of the native link items (versus the bitcode items)
522522 Linker::ItemList NativeLinkItems;
4848 // searches the link path for the specified file to try to find it...
4949 //
5050 static inline std::auto_ptr LoadFile(const std::string &FN,
51 LLVMContext* Context) {
51 const LLVMContext& Context) {
5252 sys::Path Filename;
5353 if (!Filename.set(FN)) {
5454 cerr << "Invalid file name: '" << FN << "'\n";
9292 unsigned BaseArg = 0;
9393 std::string ErrorMessage;
9494
95 std::auto_ptr Composite(LoadFile(InputFilenames[BaseArg], &Context));
95 std::auto_ptr Composite(LoadFile(InputFilenames[BaseArg], Context));
9696 if (Composite.get() == 0) {
9797 cerr << argv[0] << ": error loading file '"
9898 << InputFilenames[BaseArg] << "'\n";
100100 }
101101
102102 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
103 std::auto_ptr M(LoadFile(InputFilenames[i], &Context));
103 std::auto_ptr M(LoadFile(InputFilenames[i], Context));
104104 if (M.get() == 0) {
105105 cerr << argv[0] << ": error loading file '" <
106106 return 1;
141141 MemoryBuffer::getFileOrSTDIN(Filename, &ErrorMessage));
142142 Module *Result = 0;
143143 if (Buffer.get())
144 Result = ParseBitcodeFile(Buffer.get(), &Context, &ErrorMessage);
144 Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
145145
146146 if (Result)
147147 DumpSymbolNamesFromModule(Result);
152152
153153 } else if (aPath.isArchive()) {
154154 std::string ErrMsg;
155 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), &Context,
155 Archive* archive = Archive::OpenAndLoad(sys::Path(Filename), Context,
156156 &ErrorMessage);
157157 if (!archive)
158158 std::cerr << ToolName << ": " << Filename << ": " << ErrorMessage << "\n";
126126 Module *M = 0;
127127 if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
128128 &ErrorMessage)) {
129 M = ParseBitcodeFile(Buffer, &Context, &ErrorMessage);
129 M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
130130 delete Buffer;
131131 }
132132 if (M == 0) {
7474
7575 std::string err_msg;
7676 std::auto_ptr
77 AutoArchive(Archive::OpenAndLoad(ArchivePath, &Context, &err_msg));
77 AutoArchive(Archive::OpenAndLoad(ArchivePath, Context, &err_msg));
7878 Archive* TheArchive = AutoArchive.get();
7979 if (!TheArchive)
8080 throw err_msg;
6868 }
6969
7070
71 LTOCodeGenerator::LTOCodeGenerator()
72 : _context(new LLVMContext()),
71 LTOCodeGenerator::LTOCodeGenerator(const LLVMContext& Context)
72 : _context(Context),
7373 _linker("LinkTimeOptimizer", "ld-temp.o", _context), _target(NULL),
7474 _emitDwarfDebugInfo(false), _scopeRestrictionsDone(false),
7575 _codeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
3030 public:
3131 static const char* getVersionString();
3232
33 LTOCodeGenerator();
33 LTOCodeGenerator(const llvm::LLVMContext& Context);
3434 ~LTOCodeGenerator();
3535
3636 bool addModule(class LTOModule*, std::string& errMsg);
5353
5454 typedef llvm::StringMap StringSet;
5555
56 llvm::LLVMContext* _context;
56 const llvm::LLVMContext& _context;
5757 llvm::Linker _linker;
5858 llvm::TargetMachine* _target;
5959 bool _emitDwarfDebugInfo;
6868 bool LTOModule::isTargetMatch(MemoryBuffer* buffer, const char* triplePrefix)
6969 {
7070 OwningPtr mp(getBitcodeModuleProvider(buffer,
71 new LLVMContext()));
71 *new LLVMContext()));
7272 // on success, mp owns buffer and both are deleted at end of this method
7373 if ( !mp ) {
7474 delete buffer;
8585 {
8686 }
8787
88 LTOModule* LTOModule::makeLTOModule(const char* path, LLVMContext* Context,
88 LTOModule* LTOModule::makeLTOModule(const char* path,
89 const LLVMContext& Context,
8990 std::string& errMsg)
9091 {
9192 OwningPtr buffer(MemoryBuffer::getFile(path, &errMsg));
111112
112113
113114 LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
114 LLVMContext* Context,
115 const LLVMContext& Context,
115116 std::string& errMsg)
116117 {
117118 OwningPtr buffer(makeBuffer(mem, length));
139140 return Features.getString();
140141 }
141142
142 LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer, LLVMContext* Context,
143 LTOModule* LTOModule::makeLTOModule(MemoryBuffer* buffer,
144 const LLVMContext& Context,
143145 std::string& errMsg)
144146 {
145147 // parse bitcode buffer
5151 const char* triplePrefix);
5252
5353 static LTOModule* makeLTOModule(const char* path,
54 llvm::LLVMContext* Context,
54 const llvm::LLVMContext& Context,
5555 std::string& errMsg);
5656 static LTOModule* makeLTOModule(const void* mem, size_t length,
57 llvm::LLVMContext* Context,
57 const llvm::LLVMContext& Context,
5858 std::string& errMsg);
5959
6060 const char* getTargetTriple();
9090 const char* triplePrefix);
9191
9292 static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer,
93 llvm::LLVMContext* Context,
93 const llvm::LLVMContext& Context,
9494 std::string& errMsg);
9595 static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
9696
8787 //
8888 lto_module_t lto_module_create(const char* path, LLVMContextRef Ctxt)
8989 {
90 return LTOModule::makeLTOModule(path, llvm::unwrap(Ctxt),
90 return LTOModule::makeLTOModule(path, *llvm::unwrap(Ctxt),
9191 sLastErrorString);
9292 }
9393
9999 lto_module_t lto_module_create_from_memory(const void* mem, size_t length,
100100 LLVMContextRef Ctxt)
101101 {
102 return LTOModule::makeLTOModule(mem, length, llvm::unwrap(Ctxt),
102 return LTOModule::makeLTOModule(mem, length, *llvm::unwrap(Ctxt),
103103 sLastErrorString);
104104 }
105105
157157 // instantiates a code generator
158158 // returns NULL if there is an error
159159 //
160 lto_code_gen_t lto_codegen_create()
161 {
162 return new LTOCodeGenerator();
160 lto_code_gen_t lto_codegen_create(LLVMContextRef ContextRef)
161 {
162 return new LTOCodeGenerator(*llvm::unwrap(ContextRef));
163163 }
164164
165165
264264 lto_codegen_debug_options(lto_code_gen_t cg, const char * opt)
265265 {
266266 cg->setCodeGenDebugOptions(opt);
267 }
267 }
326326 std::auto_ptr M;
327327 if (MemoryBuffer *Buffer
328328 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
329 M.reset(ParseBitcodeFile(Buffer, &Context, &ErrorMessage));
329 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
330330 delete Buffer;
331331 }
332332
6464 class JITEventListenerTest : public testing::Test {
6565 protected:
6666 JITEventListenerTest()
67 : M(new Module("module", new LLVMContext())),
67 : M(new Module("module", *new LLVMContext())),
6868 EE(ExecutionEngine::createJIT(new ExistingModuleProvider(M))) {
6969 }
7070
271271 char OnTheFlyTest::ID=0;
272272
273273 TEST(PassManager, RunOnce) {
274 Module M("test-once", new LLVMContext());
274 Module M("test-once", *new LLVMContext());
275275 struct ModuleNDNM *mNDNM = new ModuleNDNM();
276276 struct ModuleDNM *mDNM = new ModuleDNM();
277277 struct ModuleNDM *mNDM = new ModuleNDM();
295295 }
296296
297297 TEST(PassManager, ReRun) {
298 Module M("test-rerun", new LLVMContext());
298 Module M("test-rerun", *new LLVMContext());
299299 struct ModuleNDNM *mNDNM = new ModuleNDNM();
300300 struct ModuleDNM *mDNM = new ModuleDNM();
301301 struct ModuleNDM *mNDM = new ModuleNDM();
386386
387387 Module* makeLLVMModule() {
388388 // Module Construction
389 Module* mod = new Module("test-mem", new LLVMContext());
389 Module* mod = new Module("test-mem", *new LLVMContext());
390390 mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
391391 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-"
392392 "a0:0:64-s0:64:64-f80:128:128");