Skip to content

Commit

Permalink
serialize and deserialize LLVM bitcode to files
Browse files Browse the repository at this point in the history
  • Loading branch information
Luben Karavelov committed Mar 22, 2011
1 parent 0998fee commit 424fb27
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
6 changes: 6 additions & 0 deletions runtime/parrot/library/LLVM.pm
Expand Up @@ -921,6 +921,12 @@ typedef enum {
# for debugging. */
VerifyFunction => "ipi",
####### BitWriter.h
WriteBitcodeToFile => "ipt",
####### BitReader.h
GetBitcodeModule => "vPpB",
####### Core.h
LLVMCreateMemoryBufferWithContentsOfFile => "vPB",
);
for %funcs.kv -> $name, $signature {
Expand Down
25 changes: 22 additions & 3 deletions runtime/parrot/library/LLVM/Module.pm
@@ -1,7 +1,7 @@
#! nqp
=begin Description
LLVM Module.
=end Description
#=begin Description
#LLVM Module.
#=end Description


class LLVM::Module is LLVM::Opaque {
Expand Down Expand Up @@ -45,6 +45,25 @@ class LLVM::Module is LLVM::Opaque {
method get_type_name(Str $name) {
LLVM::call("GetTypeByName", self, $name);
}

# BitReader
# from file
method read(Str $path){
my $engine := pir::new__psp("LLVM_Engine", self);
my $module := $engine.load_module($path);
self.wrap($module);
}

# from STDIN
#multi method read(){
#}

# BitWriter
# to path
method write(Str $path){
LLVM::call("WriteBitcodeToFile", self, $path);
}

};

# vim: ft=perl6
20 changes: 20 additions & 0 deletions src/dynpmc/llvm_engine.pmc
Expand Up @@ -3,6 +3,7 @@
#include <limits.h>
#include <llvm-c/Core.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/BitReader.h>

pmclass LLVM_Engine dynpmc group llvm auto_attrs {
ATTR PMC *ptr;
Expand Down Expand Up @@ -33,4 +34,23 @@ pmclass LLVM_Engine dynpmc group llvm auto_attrs {
VTABLE_set_pointer_keyed_str(interp, ret, signature, f);
RETURN(PMC* ret);
}

METHOD load_module(STRING *path) {
char *err;
LLVMMemoryBufferRef buf;
LLVMModuleRef module;
PMC *ptr = Parrot_pmc_new(INTERP, enum_class_Ptr);
const char *cpath = Parrot_str_to_cstring(INTERP, path);


if (LLVMCreateMemoryBufferWithContentsOfFile(cpath, &buf, &err) != 0) {
fprintf(stderr, err);
};
if (LLVMGetBitcodeModule(buf, &module, &err) != 0) {
fprintf(stderr, err);
}
LLVMDisposeMemoryBuffer(buf);
VTABLE_set_pointer(INTERP, ptr, module);
RETURN(PMC* ptr);
}
};
17 changes: 17 additions & 0 deletions t/library/llvm/99-example.t
Expand Up @@ -144,6 +144,23 @@ $call := $engine.create_call($f2, "tt");
$res := $call("Hello from Parrot!\n");
is($res, "Hello from Parrot!\n", "Got same string back in optimized build");

$module.write("/tmp/parrot-llvm.bit");

ok(1,"Module serialized");

my $module1 := LLVM::Module.create("HELLO_resurected");
$module1.read("/tmp/parrot-llvm.bit");

ok(1,"Module loaded from file");

my $engine1 := pir::new__psp("LLVM_Engine", $module1);

ok(1,"Engine created");
my $call1 := $engine.create_call($function, "i");
my $res1 := -1;
$res1 := $call("resurected\n");
ok(1, "Function called");
is($res1, 42, "Proper answer found");

done_testing();

Expand Down

0 comments on commit 424fb27

Please sign in to comment.