Module: Voodoo::CodeGenerator

Defined in:
lib/voodoo/code_generator.rb

Overview

Module for selecting code generators.

The code generation API is described in Voodoo::CommonCodeGenerator.

Constant Summary collapse

@@generators =

Hash using target architectures as keys. Each entry is a hash mapping output format to generator class.

{}

Class Method Summary collapse

Class Method Details

.architecture_supported?(arch) ⇒ Boolean

Tests if a given architecture is supported.

Returns:

  • (Boolean)


48
49
50
# File 'lib/voodoo/code_generator.rb', line 48

def architecture_supported? arch
  @@generators.has_key? arch.to_sym
end

.architecturesObject

Gets an array of supported architectures.



53
54
55
# File 'lib/voodoo/code_generator.rb', line 53

def architectures
  @@generators.keys
end

.format_supported?(arch, format) ⇒ Boolean

Tests if a given format is supported for a given architecture.

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/voodoo/code_generator.rb', line 58

def format_supported? arch, format
  architecture_supported?(arch.to_sym) &&
    @@generators[arch.to_sym].has_key?(format.to_sym)
end

.formats(architecture) ⇒ Object

Gets an array of supported formats for a given architecture.



64
65
66
# File 'lib/voodoo/code_generator.rb', line 64

def formats architecture
  @@generators[architecture.to_sym].keys
end

.get_generator(params = {}) ⇒ Object

Gets a code generator for the specified parameters.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/voodoo/code_generator.rb', line 32

def get_generator params = {}
  params[:architecture] = Config.default_architecture \
    unless params[:architecture]
  params[:format] = Config.default_format unless params[:format]
  arch = params[:architecture].to_sym
  format = params[:format].to_sym
  format_hash = @@generators[arch]
  klass = format_hash ? format_hash[format] : nil
  if klass
    return klass.new params
  else
    raise NotImplementedError, "No code generator for architecture #{arch} and format #{format}"
  end
end

.register_generator(klass, params) ⇒ Object

Registers a code generator. Example:

Voodoo::CodeGenerator.register_generator I386NasmGenerator,
                                         :architecture => :i386,
                                         :format => :nasm


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/voodoo/code_generator.rb', line 19

def register_generator klass, params
  if params.has_key?(:architecture) && params.has_key?(:format)
    arch = params[:architecture].to_sym
    format = params[:format].to_sym
    format_hash = @@generators[arch] || {}
    format_hash[format] = klass
    @@generators[arch] = format_hash
  else
    raise ArgumentError, "Need to specify :architecture and :format"
  end
end