Advertisement
JetForMe

Untitled

Jan 8th, 2013
59
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "llvm/LLVMContext.h"
  2. #include "llvm/ExecutionEngine/GenericValue.h"
  3. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  4. #include "llvm/Module.h"
  5. #include "llvm/ADT/StringRef.h"
  6. #include "llvm/Support/IRReader.h"
  7. #include "llvm/Support/MemoryBuffer.h"
  8. #include "llvm/Support/TargetSelect.h"
  9.  
  10.  
  11.  
  12. @implementation AppDelegate
  13.  
  14. - (void)
  15. applicationDidFinishLaunching: (NSNotification*) inNotification
  16. {
  17.     llvm::InitializeNativeTarget();
  18.    
  19.     //  Load the HelloWorld source…
  20.    
  21.     llvm::MemoryBuffer* progBuf = NULL;
  22.     {
  23.         NSURL* url = [[NSBundle mainBundle] URLForResource: @"HelloWorld" withExtension: @"llvm"];
  24.         NSData* prog = [NSData dataWithContentsOfURL: url];
  25.         if (prog == nil)
  26.         {
  27.             NSLog(@"Unable to open file '%@'", url.path);
  28.             return;
  29.         }
  30.        
  31.         llvm::StringRef sr((const char*) prog.bytes, prog.length);
  32.         progBuf = llvm::MemoryBuffer::getMemBufferCopy(sr);
  33.     }
  34.    
  35.     //  Assemble it…
  36.    
  37.     llvm::LLVMContext& llvmCTX = llvm::getGlobalContext();
  38.    
  39.     llvm::SMDiagnostic smd;
  40.     llvm::Module* mod = new llvm::Module("helloWorldModule", llvmCTX);
  41.     llvm::ParseAssembly(progBuf, mod, smd, llvmCTX);
  42.     NSLog(@"Parse: %d:%d %s\n%s", smd.getLineNo(), smd.getColumnNo(), smd.getMessage().c_str(), smd.getLineContents().c_str());
  43.    
  44.     llvm::Module::global_iterator giter = mod->getGlobalList().begin();
  45.     for (; giter != mod->getGlobalList().end(); ++giter)
  46.     {
  47.         NSLog(@"Global!");
  48.     }
  49.    
  50.     //  Build an execution engine…
  51.    
  52.     llvm::EngineBuilder builder(mod);
  53.     //builder.setEngineKind(llvm::EngineKind::Interpreter);
  54.    
  55.     llvm::ExecutionEngine* ee = builder.create();
  56.     // ========= ee is always NULL :-(
  57.  
  58.     ee->runStaticConstructorsDestructors(false);
  59.    
  60.     llvm::Function* entryFunc = mod->getFunction("MyEntry");
  61.     ee->getPointerToFunction(entryFunc);
  62.    
  63.     std::vector<llvm::GenericValue>     args;
  64.     ee->runFunction(entryFunc, args);
  65.    
  66.     ee->runStaticConstructorsDestructors(true);
  67. }
  68.  
  69. @end
Advertisement
RAW Paste Data Copied
Advertisement