Giter Site home page Giter Site logo

p5-llvm's Introduction

NAME

LLVM - Perl bindings to the Low Level Virtual Machine

SYNOPSIS

use LLVM;

# create a new LLVM context and a module named "synopsis"
my $mod = LLVM::Module -> new("synopsis");

# create a new function type that takes 2 ints and returns one int
my $intt = LLVM::Type -> int(32);
my $funt = LLVM::Type -> func($intt, $intt, $intt);

# add a new function to the module with the just-created type
my $fun = $mod -> add_func("add", $funt);

# set function's parameters names
my $params = $fun -> func_params;

$params -> [0] -> name("x");
$params -> [1] -> name("y");

# create a new instruction builder
my $bld = LLVM::Builder -> new;

# create a new entry block for the "add" function and position the
# builder at its end
my $blk = $fun -> func_append("entry");
$bld -> position_at_end($blk);

# create an "add" intruction and use its return value as function return
my $tmp = $bld -> add($params -> [0], $params -> [1], "tmp");
$bld -> ret($tmp);

# dump the LLVM IR to stderr
$mod -> dump;

The output should look like this:

; ModuleID = 'synopsis'

define i32 @add(i32 %x, i32 %y) {
entry:
  %tmp = add i32 %x, %y
  ret i32 %tmp
}

Once the module is created, a number of optimizations can be applied:

# create a new LLVM::PassManager
my $mgr = LLVM::PassManager -> new;

# schedule a couple of passes
$mgr -> add("FunctionInlining");
$mgr -> add("GlobalDCE");

# run the pass manager on the module
$mgr -> run($mod);

And finally the module can be compiled in-memory and executed:

# create the arguments for the function call
my $arg1 = LLVM::GenericValue -> int($intt, 5);
my $arg2 = LLVM::GenericValue -> int($intt, 10);

# create an execution engine for the module
my $eng = LLVM::ExecutionEngine -> new($mod);

# call the function "add" and print the result
my $result = $eng -> run_func($fun, $arg1, $arg2);

say $result -> to_int;

Or compiled to object code:

# get the first target available
my $target = LLVM::Target -> targets -> [0];

# create a target machine
my $tm = LLVM::TargetMachine -> create(
  $target, 'x86_64-linux-gnu', 'penryn', ['64bit']
);

# emit the object code to disk
$tm -> emit($mod, 'synopsis.o', 1);

DESCRIPTION

The Low-Level Virtual Machine (LLVM) is a collection of libraries and tools that make it easy to build compilers, optimizers, Just-In-Time code generators, and many other compiler-related programs. This module provides Perl bindings to the LLVM API.

Note: LLVM v3.1 is required to build this distribution.

AUTHOR

Alessandro Ghedini <[email protected]>

LICENSE AND COPYRIGHT

Copyright 2012 Alessandro Ghedini.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

p5-llvm's People

Contributors

ghedo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

p5-llvm's Issues

Update for LLVM 3.8+

Any chance of getting this dist updated to work with the 3.8+ versions of LLVM?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.