fix constant propagation through the layers

so they can end up in the binary
This commit is contained in:
Torsten Ruger 2018-07-03 10:12:22 +03:00
parent 63dfee0978
commit bb1d1495db
5 changed files with 29 additions and 9 deletions

View File

@ -6,6 +6,11 @@ module Mom
@method_compilers = Risc::Builtin.boot_functions + compilers @method_compilers = Risc::Builtin.boot_functions + compilers
end end
# collects constants from all compilers into one array
def constants
@method_compilers.inject([]){|sum ,comp| sum + comp.constants }
end
# Translate code to whatever cpu is specified. # Translate code to whatever cpu is specified.
# Currently only :arm and :interpret # Currently only :arm and :interpret
# #
@ -15,7 +20,7 @@ module Mom
platform_sym = platform_sym.to_s.capitalize platform_sym = platform_sym.to_s.capitalize
platform = Risc::Platform.for(platform_sym) platform = Risc::Platform.for(platform_sym)
assemblers = translate_methods( platform.translator ) assemblers = translate_methods( platform.translator )
Risc::Linker.new(platform , assemblers) Risc::Linker.new(platform , assemblers , constants)
end end
# go through all methods and translate them to cpu, given the translator # go through all methods and translate them to cpu, given the translator
@ -35,7 +40,7 @@ module Mom
cpu_instructions << cpu if cpu cpu_instructions << cpu if cpu
nekst = nekst.next nekst = nekst.next
end end
Risc::Assembler.new(compiler.method , cpu_instructions , compiler.constants) Risc::Assembler.new(compiler.method , cpu_instructions )
end end
end end

View File

@ -1,10 +1,10 @@
module Risc module Risc
class Assembler class Assembler
attr_reader :method , :instructions , :constants attr_reader :method , :instructions
def initialize( method , instructions, constants)
def initialize( method , instructions)
@method = method @method = method
@instructions = instructions @instructions = instructions
@constants = constants
total = instructions.total_byte_length / 4 + 1 total = instructions.total_byte_length / 4 + 1
method.binary.extend_to( total ) method.binary.extend_to( total )
end end

View File

@ -13,7 +13,7 @@ module Risc
include Util::Logging include Util::Logging
log_level :info log_level :info
def initialize(platform , assemblers) def initialize(platform , assemblers , constants)
if(platform.is_a?(Symbol)) if(platform.is_a?(Symbol))
platform = platform.to_s.capitalize platform = platform.to_s.capitalize
platform = Risc::Platform.for(platform) platform = Risc::Platform.for(platform)
@ -21,7 +21,7 @@ module Risc
raise "Platform must be platform, not #{platform.class}" unless platform.is_a?(Platform) raise "Platform must be platform, not #{platform.class}" unless platform.is_a?(Platform)
@platform = platform @platform = platform
@assemblers = assemblers @assemblers = assemblers
@constants = [] @constants = constants
@cpu_init = cpu_init_init @cpu_init = cpu_init_init
end end

View File

@ -6,7 +6,7 @@ module Mom
def setup def setup
Parfait.boot! Parfait.boot!
@comp = compile_mom( "class Test ; def main(); return 1; end; end;") @comp = compile_mom( "class Test ; def main(); return 'Hi'; end; end;")
end end
def test_class def test_class
@ -15,6 +15,12 @@ module Mom
def test_compilers def test_compilers
assert_equal 24 , @comp.method_compilers.length assert_equal 24 , @comp.method_compilers.length
end end
def test_returns_constants
assert_equal Array , @comp.constants.class
end
def test_has_constant
assert_equal "Hi" , @comp.constants.first.to_string
end
def test_has_translate def test_has_translate
assert @comp.translate(:interpreter) assert @comp.translate(:interpreter)
end end

View File

@ -6,13 +6,22 @@ module Mom
def setup def setup
Parfait.boot! Parfait.boot!
@comp = compile_mom( "class Test ; def main(); return 1; end; end;") @comp = compile_mom( "class Test ; def main(); return 'Hi'; end; end;")
@linker = @comp.translate(:interpreter) @linker = @comp.translate(:interpreter)
end end
def test_translate_class def test_translate_class
assert_equal Risc::Linker , @linker.class assert_equal Risc::Linker , @linker.class
end end
def test_linker_has_constants
assert_equal Array , @linker.constants.class
end
def test_linker_constants_not_empty
assert !@linker.constants.empty?
end
def test_linker_constants_contains_hi
assert @linker.constants.include?("Hi")
end
def test_translate_platform def test_translate_platform
assert_kind_of Risc::Platform , @linker.platform assert_kind_of Risc::Platform , @linker.platform
end end