add append to MomCompiler
also lazy init boot_function, so that compilers can be added up before going to translate, where the boot_compilers are used
This commit is contained in:
parent
1377bda641
commit
4ff84133d2
@ -1,14 +1,44 @@
|
|||||||
module Mom
|
module Mom
|
||||||
|
# The Compiler for the Mom level is a collection of Risc level Method compilers,
|
||||||
|
# plus functions to translate from the risc to cpu specific code.
|
||||||
|
#
|
||||||
|
# Builtin functions are created here, lazily, when translate is called.
|
||||||
|
# Instantiating builtin functions results in a MethodCompiler for that function, and
|
||||||
|
# to avoid confusion, these should be instantiated only once.
|
||||||
|
#
|
||||||
|
# As RubyCompiler pools source at the vool level, when several classes are compiled
|
||||||
|
# from vool to mom, several MomCompilers get instantiated. They must be merged before
|
||||||
|
# proceeding with translate. Thus we have a append method.
|
||||||
|
#
|
||||||
class MomCompiler
|
class MomCompiler
|
||||||
attr_reader :method_compilers
|
attr_reader :method_compilers
|
||||||
|
|
||||||
|
# Initialize with an array of risc MethodCompilers
|
||||||
def initialize(compilers = [])
|
def initialize(compilers = [])
|
||||||
@method_compilers = compilers + Risc::Builtin.boot_functions
|
@method_compilers = compilers
|
||||||
|
end
|
||||||
|
|
||||||
|
# lazily instantiate the compilers for boot functions
|
||||||
|
# (in the hope of only booting the functions once)
|
||||||
|
def boot_compilers
|
||||||
|
@boot_compilers ||= Risc::Builtin.boot_functions
|
||||||
|
end
|
||||||
|
|
||||||
|
# Return all compilers, namely the MethodCompilers passed in, plus the
|
||||||
|
# boot_function's compilers (boot_compilers)
|
||||||
|
def compilers
|
||||||
|
@method_compilers + boot_compilers
|
||||||
end
|
end
|
||||||
|
|
||||||
# collects constants from all compilers into one array
|
# collects constants from all compilers into one array
|
||||||
def constants
|
def constants
|
||||||
@method_compilers.inject([]){|sum ,comp| sum + comp.constants }
|
compilers.inject([]){|sum ,comp| sum + comp.constants }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Append another MomCompilers method_compilers to this one.
|
||||||
|
def append(mom_compiler)
|
||||||
|
@method_compilers += mom_compiler.method_compilers
|
||||||
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Translate code to whatever cpu is specified.
|
# Translate code to whatever cpu is specified.
|
||||||
@ -25,7 +55,7 @@ module Mom
|
|||||||
|
|
||||||
# go through all methods and translate them to cpu, given the translator
|
# go through all methods and translate them to cpu, given the translator
|
||||||
def translate_methods(translator)
|
def translate_methods(translator)
|
||||||
method_compilers.collect do |compiler|
|
compilers.collect do |compiler|
|
||||||
#log.debug "Translate method #{compiler.method.name}"
|
#log.debug "Translate method #{compiler.method.name}"
|
||||||
translate_method(compiler , translator)
|
translate_method(compiler , translator)
|
||||||
end.flatten
|
end.flatten
|
||||||
|
@ -13,10 +13,13 @@ module Mom
|
|||||||
assert_equal MomCompiler , @comp.class
|
assert_equal MomCompiler , @comp.class
|
||||||
end
|
end
|
||||||
def test_compilers
|
def test_compilers
|
||||||
assert_equal 23 , @comp.method_compilers.length
|
assert_equal 23 , @comp.compilers.length
|
||||||
|
end
|
||||||
|
def test_boot_compilers
|
||||||
|
assert_equal 22 , @comp.boot_compilers.length
|
||||||
end
|
end
|
||||||
def test_compilers_bare
|
def test_compilers_bare
|
||||||
assert_equal 22 , MomCompiler.new.method_compilers.length
|
assert_equal 22 , MomCompiler.new.compilers.length
|
||||||
end
|
end
|
||||||
def test_returns_constants
|
def test_returns_constants
|
||||||
assert_equal Array , @comp.constants.class
|
assert_equal Array , @comp.constants.class
|
||||||
@ -27,5 +30,11 @@ module Mom
|
|||||||
def test_has_translate
|
def test_has_translate
|
||||||
assert @comp.translate(:interpreter)
|
assert @comp.translate(:interpreter)
|
||||||
end
|
end
|
||||||
|
def test_append_class
|
||||||
|
assert_equal MomCompiler, (@comp.append @comp).class
|
||||||
|
end
|
||||||
|
def test_append_length
|
||||||
|
assert_equal 2 , @comp.append(@comp).method_compilers.length
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user