This module implements a compiler for the Voodoo programming language, a simple programming language designed to be a thin abstraction of the CPU‘s native instruction set.
The compiler consists of three parts:
Both the parser and the code generators can be used on their own. For example, you could use the code generator to generate native code for your own programming language without first creating a Voodoo program.
Instead of instantiating a code generator directly, it is recommended that you use Voodoo::CodeGenerator.get_generator to obtain a suitable code generator for your target platform.
A few examples to clarify the usage of the module:
The following code compiles the source file test.voo to an ELF object file called test.o containing object code for i386:
require 'voodoo'
File.open('test.voo') do |infile|
parser = Voodoo::Parser.new infile
generator = Voodoo::CodeGenerator.get_generator :architecture => :i386,
:format => :elf
File.open('test.o', 'w') do |outfile|
compiler = Voodoo::Compiler.new parser, generator, outfile
compiler.compile
end
end
The following code uses the code generator API to create an object file without the need to create a Voodoo program:
require 'voodoo'
generator = Voodoo::CodeGenerator.get_generator
generator.add :functions, [:export, :fact], [:label, :fact]
generator.add_function [:n],
[:ifle, [:n, 1],
# then
[[:return, 1]],
# else
[[:let, :x, :sub, :n, 1],
[:set, :x, :call, :fact, :x],
[:return, :mul, :n, :x]]]
File.open('fact.o', 'w') { |outfile| generator.write outfile }