Skip to content

Commit

Permalink
Implement Builder.call. Also factor out common functionality to covert
Browse files Browse the repository at this point in the history
@list to LLVM calling conventions.
  • Loading branch information
bacek authored and Reini Urban committed Oct 2, 2012
1 parent 28455e9 commit 6bed9fe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
28 changes: 28 additions & 0 deletions runtime/parrot/library/LLVM.pm
@@ -1,6 +1,33 @@
module LLVM {
our %F;

sub convert_to_struct(@args) {
pir::say("# Got { +@args } args");
my @init;
for @args {
@init.push(-100); # INTVAL. BAD. WE NEED PTR
@init.push(0);
@init.push(0);
}
my $struct := pir::new__psp('ManagedStruct', @init);

my $count := 0;
for @args {
Q:PIR{
.local pmc args, count, arg
args = find_lex '$struct'
count = find_lex '$count'
$I0 = count
arg = find_lex '$_'
$I1 = get_addr arg
args[$I0] = $I1
};
$count++;
}
$struct;
}
INIT {
pir::load_bytecode("nqp-setting.pbc");

Expand Down Expand Up @@ -269,3 +296,4 @@ module LLVM {
}
}

# vim: ft=perl6
10 changes: 10 additions & 0 deletions runtime/parrot/library/LLVM/Builder.pm
Expand Up @@ -124,6 +124,16 @@ class LLVM::Builder {
# # Miscellaneous instructions
# LLVMBuildPhi => "pppt",
# LLVMBuildCall => "pppp3t", #FIXME

method call($func, *@args, :$name?) {
%LLVM::F<LLVMBuildCall>(
$!ref,
$func._get_ptr,
LLVM::convert_to_struct(@args),
+@args,
$name // ""
);
}
# LLVMBuildSelect => "pppppt",
# LLVMBuildVAArg => "ppppt",
# LLVMBuildExtractElement => "ppppt",
Expand Down
11 changes: 5 additions & 6 deletions runtime/parrot/library/LLVM/Module.pm
Expand Up @@ -16,13 +16,12 @@ class LLVM::Module {
%LLVM::F<LLVMDumpModule>($!ref);
}

method add_function ($name, $signature) {
# FIXME. Total hack for now.
method add_function ($name, *@args, :$va_args?) {
my $type := %LLVM::F<LLVMFunctionType>(
LLVM::Type::void(), # return
undef, # parameters
0, # number of parameters
0, # is var args
LLVM::Type::void(), # return
LLVM::convert_to_struct(@args), # parameters
+@args, # number of parameters
+$va_args, # is var args
);

LLVM::Function.new.BUILD(%LLVM::F<LLVMAddFunction>($!ref, $name, $type));
Expand Down
13 changes: 11 additions & 2 deletions t/library/llvm.t
Expand Up @@ -12,11 +12,13 @@ my $module := LLVM::Module.new.BUILD("HELLO");
ok(pir::defined($module), "LLVM::Module created");
ok( $module ~~ LLVM::Module, ".. with proper type");

my $function := $module.add_function("hello", "");
my $printf := $module.add_function("printf", :va_args<1>);
$printf.set_linkage(9);

my $function := $module.add_function("hello");
ok(pir::defined($function), "Function created");
ok( $function ~~ LLVM::Function, ".. with proper type");


my $bb := $function.append_basic_block("the_block");
ok( pir::defined($bb), "BasicBlock appended");
ok( $bb ~~ LLVM::BasicBlock, ".. with proper type");
Expand All @@ -28,6 +30,13 @@ ok( $builder ~~ LLVM::Builder, ".. with proper type");
$builder.set_position($bb);
ok( 1, "Builder positioned");

$builder.call($function, :name<foo>);
ok( 1, "Call created");

$builder.call($printf, LLVM::Constant::string("Hello World\n"));
ok( 1, "Call created with args");


$builder.ret();
ok( 1, "return created");

Expand Down

0 comments on commit 6bed9fe

Please sign in to comment.