Skip to content

Commit

Permalink
Add a pointer to the owning LLVMContext to Module. This requires thre…
Browse files Browse the repository at this point in the history
…ading LLVMContext through a lot

of the bitcode reader and ASM parser APIs, as well as supporting it in all of the tools.

Patches for Clang and LLVM-GCC to follow.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74614 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
resistor committed Jul 1, 2009
1 parent 4fb75e5 commit 8b477ed
Show file tree
Hide file tree
Showing 60 changed files with 277 additions and 146 deletions.
9 changes: 5 additions & 4 deletions examples/BrainF/BrainF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ const char *BrainF::headreg = "head";
const char *BrainF::label = "brainf";
const char *BrainF::testreg = "test";

Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf) {
Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf,
LLVMContext* Context) {
in = in1;
memtotal = mem;
comflag = cf;

header();
header(Context);
readloop(0, 0, 0);
delete builder;
return module;
}

void BrainF::header() {
module = new Module("BrainF");
void BrainF::header(LLVMContext* C) {
module = new Module("BrainF", C);

//Function prototypes

Expand Down
5 changes: 3 additions & 2 deletions examples/BrainF/BrainF.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef BRAINF_H
#define BRAINF_H

#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Support/IRBuilder.h"

Expand All @@ -38,7 +39,7 @@ class BrainF {
/// containing the resulting code.
/// On error, it calls abort.
/// The caller must delete the returned module.
Module *parse(std::istream *in1, int mem, CompileFlags cf);
Module *parse(std::istream *in1, int mem, CompileFlags cf, LLVMContext* C);

protected:
/// The different symbols in the BrainF language
Expand All @@ -64,7 +65,7 @@ class BrainF {
static const char *testreg;

/// Put the brainf function preamble and other fixed pieces of code
void header();
void header(LLVMContext* C);

/// The main loop for parsing. It calls itself recursively
/// to handle the depth of nesting of "[]".
Expand Down
4 changes: 3 additions & 1 deletion examples/BrainF/BrainFDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void addMainFunction(Module *mod) {
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");

LLVMContext Context;

if (InputFilename == "") {
std::cerr<<"Error: You must specify the filename of the program to "
"be compiled. Use --help to see the options.\n";
Expand Down Expand Up @@ -124,7 +126,7 @@ int main(int argc, char **argv) {

//Read the BrainF program
BrainF bf;
Module *mod = bf.parse(in, 65536, cf); //64 KiB
Module *mod = bf.parse(in, 65536, cf, &Context); //64 KiB
if (in != &std::cin) {delete in;}
addMainFunction(mod);

Expand Down
5 changes: 4 additions & 1 deletion examples/Fibonacci/fibonacci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
Expand Down Expand Up @@ -90,8 +91,10 @@ static Function *CreateFibFunction(Module *M) {
int main(int argc, char **argv) {
int n = argc > 1 ? atol(argv[1]) : 24;

LLVMContext Context;

// Create some module to put our function into it.
Module *M = new Module("test");
Module *M = new Module("test", &Context);

// We are about to create the "fib" function:
Function *FibF = CreateFibFunction(M);
Expand Down
5 changes: 4 additions & 1 deletion examples/HowToUseJIT/HowToUseJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
Expand All @@ -50,9 +51,11 @@ using namespace llvm;
int main() {

InitializeNativeTarget();

LLVMContext Context;

// Create some module to put our function into it.
Module *M = new Module("test");
Module *M = new Module("test", &Context);

// Create the add1 function entry and insert this entry into module M. The
// function will have a return type of "int" and take an argument of "int".
Expand Down
4 changes: 3 additions & 1 deletion examples/Kaleidoscope/toy.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "llvm/DerivedTypes.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/PassManager.h"
Expand Down Expand Up @@ -1083,6 +1084,7 @@ double printd(double X) {

int main() {
InitializeNativeTarget();
LLVMContext Context;

// Install standard binary operators.
// 1 is lowest precedence.
Expand All @@ -1097,7 +1099,7 @@ int main() {
getNextToken();

// Make the module, which holds all the code.
TheModule = new Module("my cool jit");
TheModule = new Module("my cool jit", &Context);

// Create the JIT.
TheExecutionEngine = ExecutionEngine::create(TheModule);
Expand Down
5 changes: 4 additions & 1 deletion examples/ModuleMaker/ModuleMaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
Expand All @@ -22,9 +23,11 @@
using namespace llvm;

int main() {
LLVMContext Context;

// Create the "module" or "program" or "translation unit" to hold the
// function
Module *M = new Module("test");
Module *M = new Module("test", &Context);

// Create the main function: first create the type 'int ()'
FunctionType *FT = FunctionType::get(Type::Int32Ty, /*not vararg*/false);
Expand Down
4 changes: 3 additions & 1 deletion examples/ParallelJIT/ParallelJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// same time). This test had assertion errors until I got the locking right.

#include <pthread.h>
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
Expand Down Expand Up @@ -232,9 +233,10 @@ void* callFunc( void* param )

int main() {
InitializeNativeTarget();
LLVMContext Context;

// Create some module to put our function into it.
Module *M = new Module("test");
Module *M = new Module("test", &Context);

Function* add1F = createAdd1( M );
Function* fibF = CreateFibFunction( M );
Expand Down
3 changes: 2 additions & 1 deletion include/llvm-c/BitReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ extern "C" {
/* Builds a module from the bitcode in the specified memory buffer, returning a
reference to the module via the OutModule parameter. Returns 0 on success.
Optionally returns a human-readable error message via OutMessage. */
int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMContextRef ContextRef,
LLVMModuleRef *OutModule, char **OutMessage);

/* Reads a module from the specified path, returning via the OutMP parameter
a module provider which performs lazy deserialization. Returns 0 on success.
Optionally returns a human-readable error message via OutMessage. */
int LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
LLVMContextRef ContextRef,
LLVMModuleProviderRef *OutMP,
char **OutMessage);

Expand Down
10 changes: 10 additions & 0 deletions include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ extern "C" {

/* Opaque types. */

/**
* The top-level container for all LLVM global data. See the LLVMContext class.
*/
typedef struct LLVMCtxt *LLVMContextRef;

/**
* The top-level container for all other LLVM Intermediate Representation (IR)
* objects. See the llvm::Module class.
Expand Down Expand Up @@ -188,6 +193,10 @@ void LLVMDisposeMessage(char *Message);

/*===-- Modules -----------------------------------------------------------===*/

/* Create and destroy contexts. */
LLVMContextRef LLVMContextCreate();
void LLVMContextDispose(LLVMContextRef C);

/* Create and destroy modules. */
/** See llvm::Module::Module. */
LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID);
Expand Down Expand Up @@ -815,6 +824,7 @@ namespace llvm {
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder, LLVMTypeHandleRef )
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider, LLVMModuleProviderRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef )
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef )
DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase, LLVMPassManagerRef )

#undef DEFINE_STDCXX_CONVERSION_FUNCTIONS
Expand Down
7 changes: 5 additions & 2 deletions include/llvm/Assembly/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace llvm {
class Module;
class ParseError;
class raw_ostream;
class LLVMContext;

/// This function is the main interface to the LLVM Assembly Parser. It parses
/// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
Expand All @@ -30,7 +31,8 @@ class raw_ostream;
/// @brief Parse LLVM Assembly from a file
Module *ParseAssemblyFile(
const std::string &Filename, ///< The name of the file to parse
ParseError &Error ///< If not null, an object to return errors in.
ParseError &Error, ///< If not null, an object to return errors in.
LLVMContext* Context ///< Context in which to allocate globals info.
);

/// The function is a secondary interface to the LLVM Assembly Parser. It parses
Expand All @@ -42,7 +44,8 @@ Module *ParseAssemblyFile(
Module *ParseAssemblyString(
const char *AsmString, ///< The string containing assembly
Module *M, ///< A module to add the assembly too.
ParseError &Error ///< If not null, an object to return errors in.
ParseError &Error, ///< If not null, an object to return errors in.
LLVMContext* Context
);

//===------------------------------------------------------------------------===
Expand Down
9 changes: 7 additions & 2 deletions include/llvm/Bitcode/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ModuleProvider; // From VMCore
class Module; // From VMCore
class Archive; // Declared below
class ArchiveMemberHeader; // Internal implementation class
class LLVMContext; // Global data

/// This class is the main class manipulated by users of the Archive class. It
/// holds information about one member of the Archive. It is also the element
Expand Down Expand Up @@ -278,7 +279,8 @@ class Archive {
/// @returns An Archive* that represents the new archive file.
/// @brief Create an empty Archive.
static Archive* CreateEmpty(
const sys::Path& Filename ///< Name of the archive to (eventually) create.
const sys::Path& Filename,///< Name of the archive to (eventually) create.
LLVMContext* C ///< Context to use for global information
);

/// Open an existing archive and load its contents in preparation for
Expand All @@ -289,6 +291,7 @@ class Archive {
/// @brief Open and load an archive file
static Archive* OpenAndLoad(
const sys::Path& filePath, ///< The file path to open and load
LLVMContext* C, ///< The context to use for global information
std::string* ErrorMessage ///< An optional error string
);

Expand All @@ -310,6 +313,7 @@ class Archive {
/// @brief Open an existing archive and load its symbols.
static Archive* OpenAndLoadSymbols(
const sys::Path& Filename, ///< Name of the archive file to open
LLVMContext* C, ///< The context to use for global info
std::string* ErrorMessage=0 ///< An optional error string
);

Expand Down Expand Up @@ -449,7 +453,7 @@ class Archive {
protected:
/// @brief Construct an Archive for \p filename and optionally map it
/// into memory.
explicit Archive(const sys::Path& filename);
explicit Archive(const sys::Path& filename, LLVMContext* C);

/// @param data The symbol table data to be parsed
/// @param len The length of the symbol table data
Expand Down Expand Up @@ -530,6 +534,7 @@ class Archive {
unsigned firstFileOffset; ///< Offset to first normal file.
ModuleMap modules; ///< The modules loaded via symbol lookup.
ArchiveMember* foreignST; ///< This holds the foreign symbol table.
LLVMContext* Context; ///< This holds global data.
/// @}
/// @name Hidden
/// @{
Expand Down
5 changes: 4 additions & 1 deletion include/llvm/Bitcode/ReaderWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace llvm {
class MemoryBuffer;
class ModulePass;
class BitstreamWriter;
class LLVMContext;
class raw_ostream;

/// getBitcodeModuleProvider - Read the header of the specified bitcode buffer
Expand All @@ -31,12 +32,14 @@ namespace llvm {
/// error, this returns null, *does not* take ownership of Buffer, and fills
/// in *ErrMsg with an error description if ErrMsg is non-null.
ModuleProvider *getBitcodeModuleProvider(MemoryBuffer *Buffer,
LLVMContext* Context,
std::string *ErrMsg = 0);

/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
/// If an error occurs, this returns null and fills in *ErrMsg if it is
/// non-null. This method *never* takes ownership of Buffer.
Module *ParseBitcodeFile(MemoryBuffer *Buffer, std::string *ErrMsg = 0);
Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext* Context,
std::string *ErrMsg = 0);

/// WriteBitcodeToFile - Write the specified module to the specified output
/// stream.
Expand Down
3 changes: 2 additions & 1 deletion include/llvm/Debugger/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace llvm {
class Module;
class InferiorProcess;
class LLVMContext;

/// Debugger class - This class implements the LLVM source-level debugger.
/// This allows clients to handle the user IO processing without having to
Expand Down Expand Up @@ -95,7 +96,7 @@ namespace llvm {
/// the PATH for the specified program, loading it when found. If the
/// specified program cannot be found, an exception is thrown to indicate
/// the error.
void loadProgram(const std::string &Path);
void loadProgram(const std::string &Path, LLVMContext* Context);

/// unloadProgram - If a program is running, kill it, then unload all traces
/// of the current program. If no program is loaded, this method silently
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/LinkAllVMCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace {
// to know that getenv() never returns -1, this will do the job.
if (std::getenv("bar") != (char*) -1)
return;
llvm::Module* M = new llvm::Module("");
llvm::Module* M = new llvm::Module("", 0);
(void)new llvm::UnreachableInst();
(void) llvm::createVerifierPass();
(void) new llvm::Mangler(*M,"");
Expand Down
3 changes: 3 additions & 0 deletions include/llvm/Linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace llvm {

class Module;
class LLVMContext;

/// This class provides the core functionality of linking in LLVM. It retains a
/// Module object which is the composite of the modules and libraries linked
Expand Down Expand Up @@ -66,6 +67,7 @@ class Linker {
Linker(
const std::string& progname, ///< name of tool running linker
const std::string& modulename, ///< name of linker's end-result module
LLVMContext* C, ///< Context for global info
unsigned Flags = 0 ///< ControlFlags (one or more |'d together)
);

Expand Down Expand Up @@ -283,6 +285,7 @@ class Linker {
/// @name Data
/// @{
private:
LLVMContext* Context; ///< The context for global information
Module* Composite; ///< The composite module linked together
std::vector<sys::Path> LibPaths; ///< The library search paths
unsigned Flags; ///< Flags to control optional behavior.
Expand Down
Loading

0 comments on commit 8b477ed

Please sign in to comment.