[LLVMdev] Lightweight code loader

Alexander Friedman alex at inga.mit.edu
Fri May 27 15:10:47 PDT 2005


On May 26, Reid Spencer wrote:
> Alexander,
> 
> Yes, a patch like that would be accepted. Fewer dependencies = good :)
> 
> Some notes on doing this:
> 
> (1) Please make sure you use the std c++ iostream libraries for doing
> I/O. No native calls (we end up with portability problems). If you need
> something that must be ported, please add it to lib/System

Sure. What counts as a 'native call' ? :)

> (2) You should also use the sys::Path class (include/llvm/System/Path.h)
> for handling paths in a platform independent way.
> 
> (3) Your patch should be an incremental add to LLVM, not removing any
> existing JIT functionality. 

I was actually going to make something orthogonal to the JIT. It would
not support nearly as much functionality as the JIT (though it would
have similar code).

Here is a very loose use case - does this seem reasonable?      

****

  #include modulecompiler

  #include module_loader

  char * countdown_function =
    "int %countdown (int %AnArg) {\n"
    " %result = call fastcc int %foo-int (int %AnArg) \n"
    " ret int %result\n"
    "}\n"
    "fastcc int %foo-int (int %AnArg) {\n"
    "EntryBlock:\n"
    "  %cond = setle int %AnArg, 2\n"
    "  br bool %cond, label %return, label %recur\n"
    "return:\n"
    "  ret int 1\n"
    "recur:\n"
    "  %sub1 = sub int %AnArg, 1\n"
    "  %result = tail call fastcc int %foo-int (int %sub1)\n"
    "  ret int %result\n"
    "}\n"


  Module * M = parseAsmText(countdown_function);

  RelocatableCode * code = compileModule(M);

  WriteRelocatableCodeToFile (code,"file");

  // now load. This part should be as light as possible - that is, very small
  // in size of the compiled binary. The resulting "module" is fairly static, not like
  // standard llvm modules. 

  // should be able to get pointers to functions and change value of
  // global pointers. 


  NativeModule * mod = ReadAndRelocateCodeFromFile ("file");

  int (* countdown) (int);

  countdown = (int (*)(int)) GetPointerToFunction("countdown",mod);

  printf("result of countdown: %d\n", countdown (1000000));

***

There are a few problems that I need some input on.

First, I want the loader to be as tiny as possible. So, I don't want
to link in VMcore and friends.  Is it possible to just link in
selected object files instead of entire libraries?

Second, there is functionality that the loader needs to have that
depends on VMCore, but doesn't actually need it for my purposes. The
main thing is the 'relocate' function in each (System)JITInfo.cpp
file. I would like to be able to get the correct JITInfo object
(really just the function) without having to link in extra stuff,
instantiate modules,targets, etc. Ideally this would not require
duplicating any code :) How does one go about doing this?

-- 


-Alex




More information about the llvm-dev mailing list