2018-06-30 18:20:17 +02:00
|
|
|
module Mom
|
2018-07-01 20:26:45 +02:00
|
|
|
class MomCompiler
|
2018-07-02 14:50:54 +02:00
|
|
|
attr_reader :method_compilers
|
2018-06-30 18:20:17 +02:00
|
|
|
|
2018-07-01 20:26:45 +02:00
|
|
|
def initialize(compilers = [])
|
2018-07-04 08:17:30 +02:00
|
|
|
@method_compilers = compilers + Risc::Builtin.boot_functions
|
2018-06-30 18:20:17 +02:00
|
|
|
end
|
2018-07-01 10:56:09 +02:00
|
|
|
|
2018-07-03 09:12:22 +02:00
|
|
|
# collects constants from all compilers into one array
|
|
|
|
def constants
|
|
|
|
@method_compilers.inject([]){|sum ,comp| sum + comp.constants }
|
|
|
|
end
|
|
|
|
|
2018-06-30 21:53:32 +02:00
|
|
|
# Translate code to whatever cpu is specified.
|
|
|
|
# Currently only :arm and :interpret
|
|
|
|
#
|
|
|
|
# Translating means translating the initial jump
|
|
|
|
# and then translating all methods
|
2018-07-01 10:56:09 +02:00
|
|
|
def translate( platform_sym )
|
|
|
|
platform_sym = platform_sym.to_s.capitalize
|
|
|
|
platform = Risc::Platform.for(platform_sym)
|
2018-07-01 20:51:48 +02:00
|
|
|
assemblers = translate_methods( platform.translator )
|
2018-07-03 09:12:22 +02:00
|
|
|
Risc::Linker.new(platform , assemblers , constants)
|
2018-06-30 21:53:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# go through all methods and translate them to cpu, given the translator
|
|
|
|
def translate_methods(translator)
|
2018-07-01 10:56:09 +02:00
|
|
|
method_compilers.collect do |compiler|
|
|
|
|
#log.debug "Translate method #{compiler.method.name}"
|
|
|
|
translate_cpu(compiler , translator)
|
2018-06-30 21:53:32 +02:00
|
|
|
end
|
|
|
|
end
|
2018-07-01 10:56:09 +02:00
|
|
|
|
|
|
|
def translate_cpu(compiler , translator)
|
|
|
|
risc = compiler.risc_instructions
|
|
|
|
cpu_instructions = risc.to_cpu(translator)
|
|
|
|
nekst = risc.next
|
|
|
|
while(nekst)
|
|
|
|
cpu = nekst.to_cpu(translator) # returning nil means no replace
|
|
|
|
cpu_instructions << cpu if cpu
|
|
|
|
nekst = nekst.next
|
|
|
|
end
|
2018-07-03 09:12:22 +02:00
|
|
|
Risc::Assembler.new(compiler.method , cpu_instructions )
|
2018-07-01 10:56:09 +02:00
|
|
|
end
|
|
|
|
|
2018-06-30 18:20:17 +02:00
|
|
|
end
|
|
|
|
end
|