added execute command and options

execute to compile and run
options to pass parfait factory levels in (as no gc)
This commit is contained in:
Torsten 2019-07-25 22:36:51 +03:00
parent 95af84e752
commit 74f3420d45

View File

@ -3,10 +3,12 @@ require "rubyx"
require "risc/interpreter" require "risc/interpreter"
class RubyXC < Thor class RubyXC < Thor
class_option :parfait , type: :numeric
desc "compile FILE" , "Compile given FILE to binary" desc "compile FILE" , "Compile given FILE to binary"
long_desc <<-LONGDESC long_desc <<-LONGDESC
Very basic cli to compile ruby programs. Compile the give file name to binary object file (see long descr.)
Currently only compile command supported without option.
Output will be elf object file of the same name, with .o, in root directory. Output will be elf object file of the same name, with .o, in root directory.
@ -23,13 +25,16 @@ class RubyXC < Thor
end end
puts "compiling #{file}" puts "compiling #{file}"
linker = ::RubyX::RubyXCompiler.new({}).ruby_to_binary( ruby , :arm ) linker = ::RubyX::RubyXCompiler.new(extract_options).ruby_to_binary( ruby , :arm )
writer = Elf::ObjectWriter.new(linker) writer = Elf::ObjectWriter.new(linker)
outfile = file.split("/").last.gsub(".rb" , ".o") outfile = file.split("/").last.gsub(".rb" , ".o")
writer.save outfile writer.save outfile
return outfile
end end
desc "interpret FILE" , "Interpret given FILE " desc "interpret FILE" , "Interpret given FILE "
long_desc <<-LONGDESC long_desc <<-LONGDESC
Compiles the given file to an intermediate RISC format, and runs the Compiles the given file to an intermediate RISC format, and runs the
@ -54,11 +59,7 @@ class RubyXC < Thor
rescue rescue
fail MalformattedArgumentError , "No such file #{file}" fail MalformattedArgumentError , "No such file #{file}"
end end
options = { compiler = RubyX::RubyXCompiler.new(extract_options)
parfait: { factory: 3*1024, },
load_parfait: false ,
}
compiler = RubyX::RubyXCompiler.new(options)
linker = compiler.ruby_to_binary(ruby, :interpreter) linker = compiler.ruby_to_binary(ruby, :interpreter)
puts "interpreting #{file}" puts "interpreting #{file}"
@ -68,4 +69,38 @@ class RubyXC < Thor
interpreter.tick while(interpreter.instruction) interpreter.tick while(interpreter.instruction)
end end
desc "execute FILE" , "Compile given FILE and execute resulting binary"
long_desc <<-LONGDESC
Just like the compile task, this compiles the file to an object/binary file.
Then rubyxc will link and run the resulting object file. For this to work,
qemu needs to be set up correctly on the system. Specifically, because of
bug #13, arm-linux-gnueabihf-ld needs to exist (it's part of the cross compiled
arm binutils).
The resulting a.out will be run via qemu-arm. This is part of the qemu "linux" package
and interprets the arm binary on the host, assuming a linux os.
This whole approach should only be used for preliminary checking that no core-dumps
are generated by the program, or when no benchmarking (as the times will be whatever).
For simple functional test though, it is a much much quicker way to run the binary
than transferring it to another machine. The a.out is left in place to be run again.
LONGDESC
def execute(file)
outfile = compile(file)
system "arm-linux-gnueabihf-ld -N #{outfile}"
puts "Linked ok, now running #{file}"
system "qemu-arm ./a.out"
end
private
def extract_options
opt = { factory: options[:parfait] || 1024 }
puts opt
return {parfait: opt}
end
end end