Move booting to RubyXCompiler init

Also pass the source into the compile method.
This way compiler can be reused for subsequent compile.
Does remove some double boots, but no major time save
This commit is contained in:
Torsten Ruger 2018-09-02 13:57:19 +03:00
parent d73e1526cd
commit 8a81d41d5e
14 changed files with 39 additions and 57 deletions

View File

@ -17,6 +17,11 @@ end
# of that subset that we need. # of that subset that we need.
# See risc/Readme # See risc/Readme
module Risc module Risc
# module method to reset, and init
def self.boot!
Position.clear_positions
Builtin.boot_functions
end
end end
require_relative "risc/padding" require_relative "risc/padding"

View File

@ -125,13 +125,5 @@ module Risc
risc_init = Branch.new( "__initial_branch__" , init.callable.binary ) risc_init = Branch.new( "__initial_branch__" , init.callable.binary )
@platform.translator.translate(risc_init) @platform.translator.translate(risc_init)
end end
end end
# module method to reset, and init
def self.boot!
Position.clear_positions
Builtin.boot_functions
end
end end

View File

@ -1,31 +1,29 @@
module RubyX module RubyX
class RubyXCompiler class RubyXCompiler
attr_reader :source
def initialize(source) def initialize
@source = source Parfait.boot!
Risc.boot!
end end
def ruby_to_vool def ruby_to_vool(ruby_source)
ruby = Ruby::RubyCompiler.compile( source ) ruby_tree = Ruby::RubyCompiler.compile( ruby_source )
vool = ruby.to_vool vool_tree = ruby_tree.to_vool
vool vool_tree
end end
def ruby_to_mom def ruby_to_mom(ruby)
vool = ruby_to_vool vool_tree = ruby_to_vool(ruby)
vool.to_mom(nil) vool_tree.to_mom(nil)
end end
def ruby_to_risc(platform) def ruby_to_risc(ruby, platform)
mom = ruby_to_mom mom = ruby_to_mom(ruby)
mom.translate(platform) mom.translate(platform)
end end
def ruby_to_binary(platform) def ruby_to_binary(ruby , platform)
Parfait.boot! linker = ruby_to_risc(ruby, platform)
Risc.boot!
linker = ruby_to_risc(platform)
linker.position_all linker.position_all
linker.create_binary linker.create_binary
linker linker

View File

@ -8,8 +8,6 @@ module Elf
DEBUG = false DEBUG = false
def setup def setup
Parfait.boot!
Risc.boot!
end end
def in_space(input) def in_space(input)
@ -19,7 +17,7 @@ module Elf
in_space("def main(arg);#{input};end") in_space("def main(arg);#{input};end")
end end
def check(input, file) def check(input, file)
linker = RubyX::RubyXCompiler.new(input).ruby_to_binary( :arm ) linker = RubyX::RubyXCompiler.new.ruby_to_binary( input , :arm )
writer = Elf::ObjectWriter.new(linker) writer = Elf::ObjectWriter.new(linker)
writer.save "test/#{file}.o" writer.save "test/#{file}.o"
end end

View File

@ -6,7 +6,7 @@ module Elf
def setup def setup
super super
@linker = RubyX::RubyXCompiler.new(as_main("return 1")).ruby_to_risc(:arm) @linker = RubyX::RubyXCompiler.new.ruby_to_risc(as_main("return 1"),:arm)
@linker.position_all @linker.position_all
@linker.create_binary @linker.create_binary
end end

View File

@ -4,8 +4,6 @@ module Risc
module Statements module Statements
def setup def setup
Parfait.boot!
Risc::Builtin.boot_functions
end end
def preamble def preamble
@ -31,7 +29,7 @@ module Risc
end end
def produce_instructions def produce_instructions
assert @expect , "No output given" assert @expect , "No output given"
linker = RubyX::RubyXCompiler.new(as_test_main).ruby_to_risc(:interpreter) linker = RubyX::RubyXCompiler.new.ruby_to_risc(as_test_main,:interpreter)
compiler = linker.assemblers.find{|c| c.callable.name == :main and c.callable.self_type.object_class.name == :Test} compiler = linker.assemblers.find{|c| c.callable.name == :main and c.callable.self_type.object_class.name == :Test}
compiler.instructions compiler.instructions
end end

View File

@ -19,7 +19,7 @@ module Risc
end end
class TestLinkerInit < MiniTest::Test class TestLinkerInit < MiniTest::Test
def setup def setup
@linker = RubyX::RubyXCompiler.new("class Space;def main;return 1;end;end").ruby_to_binary(:arm) @linker = RubyX::RubyXCompiler.new.ruby_to_binary("class Space;def main;return 1;end;end",:arm)
end end
def test_pos_cpu def test_pos_cpu
assert_equal 0 , Position.get(@linker.cpu_init).at assert_equal 0 , Position.get(@linker.cpu_init).at

View File

@ -3,9 +3,8 @@ require_relative "helper"
module Risc module Risc
class TestMachinePos < MiniTest::Test class TestMachinePos < MiniTest::Test
def setup def setup
Parfait.boot! code = "class Space; def main(arg);a = 1;return a;end;end"
Risc.boot! @linker = RubyX::RubyXCompiler.new.ruby_to_risc(code,:arm)
@linker = RubyX::RubyXCompiler.new("class Space; def main(arg);a = 1;return a;end;end").ruby_to_risc(:arm)
@linker.position_all @linker.position_all
end end
def test_positions_set def test_positions_set

View File

@ -5,11 +5,10 @@ module Risc
include ScopeHelper include ScopeHelper
def setup def setup
Parfait.boot!
end end
def in_test_vool(str) def in_test_vool(str)
vool = RubyX::RubyXCompiler.new(in_Test(str)).ruby_to_vool vool = RubyX::RubyXCompiler.new.ruby_to_vool(in_Test(str))
vool.to_mom(nil) vool.to_mom(nil)
vool vool
end end
@ -65,7 +64,7 @@ module Risc
assert_equal 2 , method.frame_type.variable_index(:a) assert_equal 2 , method.frame_type.variable_index(:a)
end end
def constant_setup(input) def constant_setup(input)
mom = RubyX::RubyXCompiler.new(in_Test(input)).ruby_to_mom mom = RubyX::RubyXCompiler.new.ruby_to_mom(in_Test(input))
assert_equal Mom::MomCompiler , mom.class assert_equal Mom::MomCompiler , mom.class
compiler = mom.method_compilers.first compiler = mom.method_compilers.first
assert_equal MethodCompiler , compiler.class assert_equal MethodCompiler , compiler.class

View File

@ -3,20 +3,18 @@ require_relative "../helper"
module RubyX module RubyX
module RubyXHelper module RubyXHelper
def setup def setup
Parfait.boot!
Risc.boot!
end end
def ruby_to_risc(input , platform) def ruby_to_risc(input , platform)
mom = ruby_to_mom(input) mom = ruby_to_mom(input)
mom.translate(platform) mom.translate(platform)
end end
def ruby_to_vool(input) def ruby_to_vool(input)
RubyXCompiler.new(input).ruby_to_vool RubyXCompiler.new.ruby_to_vool(input)
end end
def ruby_to_mom(input) def ruby_to_mom(input)
RubyXCompiler.new(input).ruby_to_mom RubyXCompiler.new.ruby_to_mom(input)
end end
def compile_in_test input def compile_in_test( input )
vool = ruby_to_vool in_Test(input) vool = ruby_to_vool in_Test(input)
vool.to_mom(nil) vool.to_mom(nil)
itest = Parfait.object_space.get_class_by_name(:Test) itest = Parfait.object_space.get_class_by_name(:Test)

View File

@ -15,13 +15,6 @@ module RubyX
assert itest.instance_type.names.include?(:trivar) , itest.instance_type.names.inspect assert itest.instance_type.names.include?(:trivar) , itest.instance_type.names.inspect
end end
def test_doesnt_create_existing_clas
space_class = Parfait.object_space.get_class_by_name(:Space)
ruby_to_vool "class Space ; end"
clazz = Parfait.object_space.get_class_by_name(:Space)
assert_equal clazz , space_class
end
def test_class_body_is_scope def test_class_body_is_scope
clazz = ruby_to_vool in_Test("def meth; @ivar = 5 ;end") clazz = ruby_to_vool in_Test("def meth; @ivar = 5 ;end")
assert_equal Vool::Statements , clazz.body.class assert_equal Vool::Statements , clazz.body.class
@ -29,15 +22,17 @@ module RubyX
end end
def test_space_is_unchanged_by_compile def test_space_is_unchanged_by_compile
compiler = RubyXCompiler.new
space1 = Parfait.object_space.get_class_by_name(:Space) space1 = Parfait.object_space.get_class_by_name(:Space)
ruby_to_vool "class Space ;end" compiler.ruby_to_vool "class Space ;end"
space2 = Parfait.object_space.get_class_by_name(:Space) space2 = Parfait.object_space.get_class_by_name(:Space)
assert_equal space1 , space2 assert_equal space1 , space2
end end
def test_space_type_is_unchanged_by_compile def test_space_type_is_unchanged_by_compile
compiler = RubyXCompiler.new
space1 = Parfait.object_space.get_type_by_class_name(:Space) space1 = Parfait.object_space.get_type_by_class_name(:Space)
ruby_to_vool "class Space ;end" compiler.ruby_to_vool "class Space ;end"
space2 = Parfait.object_space.get_type_by_class_name(:Space) space2 = Parfait.object_space.get_type_by_class_name(:Space)
assert_equal space1 , space2 assert_equal space1 , space2
end end

View File

@ -25,8 +25,9 @@ module RubyX
end end
def test_space_type_is_unchanged_by_compile def test_space_type_is_unchanged_by_compile
compiler = RubyXCompiler.new
space1 = Parfait.object_space.get_type_by_class_name(:Space) space1 = Parfait.object_space.get_type_by_class_name(:Space)
ruby_to_vool "class Space ;end" compiler.ruby_to_vool "class Space ;end"
space2 = Parfait.object_space.get_type_by_class_name(:Space) space2 = Parfait.object_space.get_type_by_class_name(:Space)
assert_equal space1 , space2 assert_equal space1 , space2
end end

View File

@ -22,7 +22,7 @@ module MomCompile
include ScopeHelper include ScopeHelper
def compile_method(input) def compile_method(input)
statements = RubyX::RubyXCompiler.new(input).ruby_to_vool statements = RubyX::RubyXCompiler.new.ruby_to_vool(input)
assert statements.is_a?(Vool::ClassStatement) assert statements.is_a?(Vool::ClassStatement)
ret = statements.to_mom(nil) ret = statements.to_mom(nil)
assert_equal Parfait::Class , statements.clazz.class , statements assert_equal Parfait::Class , statements.clazz.class , statements
@ -50,8 +50,7 @@ module MomCompile
block.body.to_mom(block_c) block.body.to_mom(block_c)
end end
def compile_mom(input) def compile_mom(input)
Risc.boot! RubyX::RubyXCompiler.new.ruby_to_mom(input)
RubyX::RubyXCompiler.new(input).ruby_to_mom
end end
def check_array( should , is ) def check_array( should , is )

View File

@ -7,7 +7,7 @@ module Risc
include ScopeHelper include ScopeHelper
def setup def setup
@linker = RubyX::RubyXCompiler.new(@string_input).ruby_to_binary( :interpreter) @linker = RubyX::RubyXCompiler.new.ruby_to_binary(@string_input, :interpreter)
@interpreter = Interpreter.new(@linker) @interpreter = Interpreter.new(@linker)
@interpreter.start_program @interpreter.start_program
end end