Module: Voodoo

Defined in:
lib/voodoo.rb,
lib/voodoo/config.rb,
lib/voodoo/parser.rb,
lib/voodoo/compiler.rb,
lib/voodoo/validator.rb,
lib/voodoo/code_generator.rb,
lib/voodoo/generators/nasm_generator.rb,
lib/voodoo/generators/arm_elf_generator.rb,
lib/voodoo/generators/arm_gas_generator.rb,
lib/voodoo/generators/gas_elf_generator.rb,
lib/voodoo/generators/i386_elf_generator.rb,
lib/voodoo/generators/mips_elf_generator.rb,
lib/voodoo/generators/mips_gas_generator.rb,
lib/voodoo/generators/nasm_elf_generator.rb,
lib/voodoo/generators/amd64_elf_generator.rb,
lib/voodoo/generators/i386_nasm_generator.rb,
lib/voodoo/generators/amd64_nasm_generator.rb,
lib/voodoo/generators/command_postprocessor.rb,
lib/voodoo/generators/common_code_generator.rb

Overview

Voodoo - Code Generation for Multiple Target Platforms

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:

  1. The parser (Voodoo::Parser), which reads Voodoo source code and turns it into Ruby objects.

  2. The code generator (a class that implements the methods of Voodoo::CommonCodeGenerator), which provides methods that generate code for the target platform.

  3. The compiler driver (Voodoo::Compiler), which reads from the parser and calls the appropriate methods of the code generator.

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 }

Defined Under Namespace

Modules: CodeGenerator, CommandPostProcessor, Config, Validator Classes: AMD64ELFGenerator, AMD64NasmGenerator, ARMELFGenerator, ARMGasGenerator, CommonCodeGenerator, Compiler, GasELFGenerator, I386ELFGenerator, I386NasmGenerator, MIPSELFGenerator, MIPSGasGenerator, NasmELFGenerator, NasmGenerator, Parser