cleaner interfaces for rubyXcompiler

store the vool
seperate api for ruby -> X and stored vool -> X
This commit is contained in:
Torsten Ruger 2018-11-02 12:19:13 -07:00
parent 87fc91cd5c
commit 52f6f1eaa8

View File

@ -4,11 +4,20 @@ module RubyX
# There are methods to go from ruby to any of the layers in the system
# (mainly for testing). ruby_to_binary creates actual binary code
# for a given platform.
# The compiler keeps the vool source as an instance.
# To compile several sources, more vool can be added, ie ruby_to_vool
# can be called several times.
#
# All other methods come in pairs, one takes ruby source (those are for testing)
# and the other uses the stored vool source for further processing.
#
# Only builtin is loaded, so no runtime , but the compiler
# can be used to read the runtime and then any other code
#
class RubyXCompiler
attr_reader :vool
# initialize boots Parfait and Risc (ie load Builin)
def initialize
Parfait.boot!
@ -22,9 +31,19 @@ module RubyX
#
# A Linker is returned that may be used to create an elf binay
#
# The compiling is done by ruby_to_risc
# The compiling is done by to_binary
def ruby_to_binary(ruby , platform)
linker = ruby_to_risc(ruby, platform)
ruby_to_vool(ruby)
to_binary(platform)
end
# Process previously stored vool source to binary.
# Binary code is generated byu calling to_risc, then positioning and calling
# create_binary on the linker. The linker may then be used to creat a binary file.
# The biary the method name refers to is binary code in memory, or in BinaryCode
# objects to be precise.
def to_binary(platform)
linker = to_risc(platform)
linker.position_all
linker.create_binary
linker
@ -33,40 +52,49 @@ module RubyX
# ruby_to_risc creates Risc instructions (as the name implies), but also
# translates those to the platform given
#
# The higher level translation is done by ruby_to_mom
# After creating vool, we call to_risc
def ruby_to_risc(ruby, platform)
mom = ruby_to_mom(ruby)
ruby_to_vool(ruby)
to_risc(platform)
end
# Process previously stored vool source. First to mom, then to platform.
# Translating to platform returns a linker that is returned and can be used
# to generate binaries
def to_risc(platform)
mom = to_mom
mom.translate(platform)
end
# ruby_to_mom does exactly that, it transform the incoming ruby source (string)
# to mom
#
# the method calls ruby_to_vool to compile the first layer
# The vool is stored using ruby_to_vool, and if there was previous source,
# this will also be momed
def ruby_to_mom(ruby)
vool_tree = ruby_to_vool(ruby)
vool_tree.to_mom(nil)
ruby_to_vool(ruby)
to_mom
end
# return mom for the previously stored vool source.
def to_mom
@vool.to_mom(nil)
end
# ruby_to_vool compiles the ruby to ast, and then to vool
def ruby_to_vool(ruby_source)
ruby_tree = Ruby::RubyCompiler.compile( ruby_source )
vool_tree = ruby_tree.to_vool
vool_tree
@vool = ruby_tree.to_vool
end
def self.ruby_to_binary( ruby , platform)
compiler = RubyXCompiler.new
vool_tree = compiler.ruby_to_vool(ruby)
compiler.ruby_to_vool(ruby)
# integrate other sources into vool tree
mom = vool_tree.to_mom(nil)
linker = mom.translate(platform)
linker.position_all
linker.create_binary
linker
compiler.to_binary(platform)
end
end
end