Remove separate block_compiler lists

both in mom and risc method complers
alll just compilers now, all linked
Required to move some code down into callable_compiler but all in all quite little. cleaner
This commit is contained in:
Torsten Rüger 2019-09-28 17:24:10 +03:00
parent dcbc3e17be
commit 1e5073200c
9 changed files with 25 additions and 42 deletions

View File

@ -16,8 +16,8 @@ module Mom
"#{@method.self_type.name}.init" "#{@method.self_type.name}.init"
end end
def to_risc(in_method) def to_risc
risc_compiler = Risc::BlockCompiler.new(@callable , in_method , mom_instructions) risc_compiler = Risc::BlockCompiler.new(@callable , @method , mom_instructions)
instructions_to_risc(risc_compiler) instructions_to_risc(risc_compiler)
#recursive blocks not done #recursive blocks not done
risc_compiler risc_compiler

View File

@ -7,11 +7,11 @@ module Mom
# Instructions derive from class Instruction and form a linked list # Instructions derive from class Instruction and form a linked list
class CallableCompiler class CallableCompiler
include Util::CompilerList
def initialize( callable ) def initialize( callable )
@callable = callable @callable = callable
@constants = [] @constants = []
@block_compilers = []
@mom_instructions = Label.new(source_name, source_name) @mom_instructions = Label.new(source_name, source_name)
@current = start = @mom_instructions @current = start = @mom_instructions
add_code Label.new( source_name, "return_label") add_code Label.new( source_name, "return_label")
@ -19,7 +19,7 @@ module Mom
add_code Label.new( source_name, "unreachable") add_code Label.new( source_name, "unreachable")
@current = start @current = start
end end
attr_reader :mom_instructions , :constants , :block_compilers , :callable , :current attr_reader :mom_instructions , :constants , :callable , :current
def return_label def return_label
@mom_instructions.each do |ins| @mom_instructions.each do |ins|

View File

@ -4,8 +4,7 @@ module Mom
# and to instantiate the methods correctly. # and to instantiate the methods correctly.
class MethodCompiler < CallableCompiler class MethodCompiler < CallableCompiler
include Util::CompilerList
def initialize( method ) def initialize( method )
super(method) super(method)
end end
@ -28,9 +27,6 @@ module Mom
def to_risc def to_risc
risc_compiler = Risc::MethodCompiler.new(@callable , mom_instructions) risc_compiler = Risc::MethodCompiler.new(@callable , mom_instructions)
instructions_to_risc(risc_compiler) instructions_to_risc(risc_compiler)
block_compilers.each do |m_comp|
risc_compiler.block_compilers << m_comp.to_risc(@callable)
end
risc_compiler risc_compiler
end end
@ -77,10 +73,6 @@ module Mom
return ["local#{index}".to_sym] return ["local#{index}".to_sym]
end end
def add_block_compiler(compiler)
@block_compilers << compiler
end
# return true or false if the given name is in scope (arg/local) # return true or false if the given name is in scope (arg/local)
def in_scope?(name) def in_scope?(name)
ret = true if @callable.arguments_type.variable_index(name) ret = true if @callable.arguments_type.variable_index(name)

View File

@ -1,6 +1,7 @@
module Risc module Risc
# A BlockCompiler is much like a Mehtodcompiler, exept for blocks # A BlockCompiler is much like a MethodCompiler, exept for it's for blocks
# This only changes scoping for variables, lsee slot_type
# #
class BlockCompiler < CallableCompiler class BlockCompiler < CallableCompiler

View File

@ -10,6 +10,7 @@ module Risc
# - current instruction is where addidion happens # - current instruction is where addidion happens
# #
class CallableCompiler class CallableCompiler
include Util::CompilerList
# Must pass the callable (method/block) # Must pass the callable (method/block)
# Also start instuction, usually a label is mandatory # Also start instuction, usually a label is mandatory
@ -18,11 +19,10 @@ module Risc
@callable = callable @callable = callable
@regs = [] @regs = []
@constants = [] @constants = []
@block_compilers = []
@current = @risc_instructions = mom_label.risc_label(self) @current = @risc_instructions = mom_label.risc_label(self)
reset_regs reset_regs
end end
attr_reader :risc_instructions , :constants , :block_compilers , :callable , :current attr_reader :risc_instructions , :constants , :callable , :current
def return_label def return_label
@risc_instructions.each do |ins| @risc_instructions.each do |ins|
@ -138,5 +138,12 @@ module Risc
Risc::Assembler.new(@callable , cpu_instructions ) Risc::Assembler.new(@callable , cpu_instructions )
end end
# translate this method, which means the method itself and all blocks inside it
# returns the array (of assemblers) that you pass in as collection
def translate_method( translator , collection)
collection << translate_cpu( translator )
collection
end
end end
end end

View File

@ -4,7 +4,6 @@ module Risc
# and to instantiate the methods correctly. # and to instantiate the methods correctly.
class MethodCompiler < CallableCompiler class MethodCompiler < CallableCompiler
include Util::CompilerList
# Methods starts with a Label, both in risc and mom. # Methods starts with a Label, both in risc and mom.
# Pass in the callable(method) and the mom label that the method starts with # Pass in the callable(method) and the mom label that the method starts with
@ -12,11 +11,6 @@ module Risc
super(method , mom_label) super(method , mom_label)
end end
#include block_compilers constants
def constants
block_compilers.inject(@constants.dup){|all, compiler| all += compiler.constants}
end
def source_name def source_name
"#{@callable.self_type.name}.#{@callable.name}" "#{@callable.self_type.name}.#{@callable.name}"
end end
@ -26,18 +20,5 @@ module Risc
@callable @callable
end end
def add_block_compiler(compiler)
@block_compilers << compiler
end
# translate this method, which means the method itself and all blocks inside it
# returns the array (of assemblers) that you pass in as collection
def translate_method( translator , collection)
collection << translate_cpu( translator )
@block_compilers.each do |block_compiler|
collection << block_compiler.translate_cpu(translator)
end
collection
end
end end
end end

View File

@ -23,10 +23,10 @@ module Vool
def compile( compiler ) def compile( compiler )
parfait_block = self.parfait_block(compiler) parfait_block = self.parfait_block(compiler)
block_compiler = Mom::BlockCompiler.new( parfait_block , compiler.get_method ) block_compiler = Mom::BlockCompiler.new( parfait_block , compiler.get_method )
compiler.add_block_compiler(block_compiler)
head = body.to_mom( block_compiler ) head = body.to_mom( block_compiler )
block_compiler.add_code(head) block_compiler.add_code(head)
block_compiler compiler.add_method_compiler(block_compiler)
nil
end end
def each(&block) def each(&block)

View File

@ -16,8 +16,9 @@ module Mom
assert_equal :main , @risc.method_compilers.callable.name assert_equal :main , @risc.method_compilers.callable.name
end end
def test_main_block_compiler def test_main_block_compiler
assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name main_block = @risc.method_compilers.find_compiler_name(:main_block)
assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name assert_equal :main_block , main_block.callable.name
assert_equal :main , main_block.in_method.name
end end
end end
class TestBlockCompiler2 < MiniTest::Test class TestBlockCompiler2 < MiniTest::Test
@ -35,8 +36,9 @@ module Mom
assert_equal :main , @risc.method_compilers.callable.name assert_equal :main , @risc.method_compilers.callable.name
end end
def test_main_block_compiler def test_main_block_compiler
assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name main_block = @risc.method_compilers.find_compiler_name(:main_block)
assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name assert_equal :main_block , main_block.callable.name
assert_equal :main , main_block.in_method.name
end end
end end
end end

View File

@ -45,7 +45,7 @@ module VoolCompile
source = get_preload(preload) + as_main("#{method_input} ; self.main{|val| #{block_input}}") source = get_preload(preload) + as_main("#{method_input} ; self.main{|val| #{block_input}}")
mom_col = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom( source ) mom_col = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom( source )
compiler = mom_col.method_compilers.find_compiler_name(:main) compiler = mom_col.method_compilers.find_compiler_name(:main)
block = compiler.block_compilers.first block = mom_col.method_compilers.find_compiler_name(:main_block)
assert block assert block
block.mom_instructions.next block.mom_instructions.next
end end