First support for arm architecture
[voodoo-lang.git] / lib / voodoo / generators / gas_elf_generator.rb
blob81fc5ca7d80898994308ac39c357e1a7df1f52d5
1 require 'voodoo/config'
2 require 'voodoo/generators/command_postprocessor'
4 module Voodoo
5   # Class that generates ELF object files by invoking the GNU assembler on
6   # the output of a code generator that generates GNU assembly.
7   class GasELFGenerator
8     def initialize asmgenerator, extra_args = ""
9       @asmgenerator = asmgenerator
10       @extra_args = extra_args
11       @output_file_suffix = '.o'
12     end
14     include CommandPostProcessor
16     def output_file_name input_name
17       input_name.sub(/\.voo$/, '') + @output_file_suffix
18     end
20     def output_file_suffix
21       @output_file_suffix
22     end
24     # Writes the generated code to the given IO handle
25     def write io
26       # Create temporary file to write assembly code to
27       if io.respond_to? :path
28         base = File.basename(io.path).sub(/([^.]*).*/, "\\1")
29       else
30         base = self.class.name
31       end
32       
33       Tempfile.open(base + '.s') do |asmfile|
34         Tempfile.open(base + '.o') do |elffile|
35           elffile.close
36           # Write assembly code to nsamfile
37           @asmgenerator.write asmfile
38           asmfile.close
39           # Find out the name of the GNU assembler
40           gas = Voodoo::Config.gas_command
41           # Invoke gas on asmfile
42           command = "#{gas} #{@extra_args}" +
43             " -o #{shell_encode elffile.path}" +
44             " #{shell_encode asmfile.path}"
45           if system(command)
46             write_file_to_io elffile.path, io
47           end
48           elffile.unlink
49         end
50         asmfile.unlink
51       end
52     end
53   end
54 end