also separate risc and cpu inits for the machine

interpreter works on risc, but assembler off cpu
This commit is contained in:
Torsten Ruger 2018-03-25 19:36:00 +03:00
parent 3090ccffea
commit 3bd23cee28
3 changed files with 13 additions and 33 deletions

View File

@ -23,8 +23,8 @@ module Risc
def assemble def assemble
at = 0 at = 0
#need the initial jump at 0 and then functions #need the initial jump at 0 and then functions
@machine.init.set_position( 0) @machine.cpu_init.set_position( 0)
at = @machine.init.byte_length at = @machine.cpu_init.byte_length
at = assemble_objects( at ) at = assemble_objects( at )
# and then everything code # and then everything code
asseble_code_from( at ) asseble_code_from( at )
@ -38,7 +38,7 @@ module Risc
objekt.create_binary if objekt.is_a? Parfait::TypedMethod objekt.create_binary if objekt.is_a? Parfait::TypedMethod
binary = objekt.binary binary = objekt.binary
Positioned.set_position(binary,at) Positioned.set_position(binary,at)
objekt.instructions.set_position( at + 12) # BinaryCode header objekt.risc_instructions.set_position( at + 12) # BinaryCode header
len = 4 * 14 len = 4 * 14
at += binary.padded_length at += binary.padded_length
nekst = binary.next nekst = binary.next
@ -58,7 +58,7 @@ module Risc
at += 8 # thats the padding at += 8 # thats the padding
# want to have the objects first in the executable # want to have the objects first in the executable
@objects.each do | id , objekt| @objects.each do | id , objekt|
next if objekt.is_a? Risc::Label # will get assembled as method.instructions next if objekt.is_a? Risc::Label # will get assembled as method.risc_instructions
next if objekt.is_a? Parfait::BinaryCode next if objekt.is_a? Parfait::BinaryCode
Positioned.set_position(objekt,at) Positioned.set_position(objekt,at)
at += objekt.padded_length at += objekt.padded_length
@ -132,10 +132,10 @@ module Risc
# and then plonk that binary data into the method.code array # and then plonk that binary data into the method.code array
def assemble_binary_method method def assemble_binary_method method
stream = StringIO.new stream = StringIO.new
#puts "Method #{method.source.instructions.to_ac}" #puts "Method #{method.source.risc_instructions.to_ac}"
begin begin
#puts "assemble #{method.source.instructions}" #puts "assemble #{method.source.risc_instructions}"
method.instructions.assemble_all( stream ) method.risc_instructions.assemble_all( stream )
rescue => e rescue => e
log.debug "Assembly error #{method.name}\n#{method.to_rxf.to_s[0...2000]}" log.debug "Assembly error #{method.name}\n#{method.to_rxf.to_s[0...2000]}"
raise e raise e
@ -155,7 +155,7 @@ module Risc
stream.rewind stream.rewind
length = stream.length length = stream.length
binary = method.binary binary = method.binary
total_byte_length = method.instructions.total_byte_length total_byte_length = method.risc_instructions.total_byte_length
log.debug "Assembled code #{method.name} with length #{length}" log.debug "Assembled code #{method.name} with length #{length}"
raise "length error #{binary.char_length} != #{total_byte_length}" if binary.char_length <= total_byte_length raise "length error #{binary.char_length} != #{total_byte_length}" if binary.char_length <= total_byte_length
raise "length error #{length} != #{total_byte_length}" if total_byte_length != length raise "length error #{length} != #{total_byte_length}" if total_byte_length != length

View File

@ -20,39 +20,28 @@ module Risc
@booted = false @booted = false
@constants = [] @constants = []
end end
attr_reader :constants , :init , :booted attr_reader :constants , :risc_init , :cpu_init , :booted
# idea being that later method missing could catch translate_xxx and translate to target xxx # idea being that later method missing could catch translate_xxx and translate to target xxx
# now we just instantiate ArmTranslater and pass instructions # now we just instantiate ArmTranslater and pass instructions
def translate_arm def translate_arm
methods = Parfait.object_space.collect_methods methods = Parfait.object_space.collect_methods
translate_methods( methods ) translate_methods( methods )
label = @init.next @cpu_init = Arm::Translator.new.translate( @risc_init )
@init = Arm::Translator.new.translate( @init )
@init.append label
end end
def translate_methods(methods) def translate_methods(methods)
translator = Arm::Translator.new translator = Arm::Translator.new
methods.each do |method| methods.each do |method|
log.debug "Translate method #{method.name}" log.debug "Translate method #{method.name}"
instruction = method.instructions method.translate_cpu(translator)
while instruction.next
nekst = instruction.next
t = translator.translate(nekst) # returning nil means no replace
if t
nekst = t.last
instruction.replace_next(t)
end
instruction = nekst
end
end end
end end
def boot def boot
initialize initialize
boot_parfait! boot_parfait!
@init = Branch.new( "__initial_branch__" , Parfait.object_space.get_init.instructions ) @risc_init = Branch.new( "__initial_branch__" , Parfait.object_space.get_init.risc_instructions )
@booted = true @booted = true
self self
end end
@ -69,13 +58,4 @@ module Risc
end end
Parfait::TypedMethod.class_eval do
# for testing we need to reuse the main function (or do we?)
# so remove the code that is there
def clear_source
self.source.send :initialize , self
end
end
require_relative "boot" require_relative "boot"

View File

@ -13,7 +13,7 @@ module Risc
Vool::VoolCompiler.ruby_to_vool( @string_input ) Vool::VoolCompiler.ruby_to_vool( @string_input )
Collector.collect_space Collector.collect_space
@interpreter = Interpreter.new @interpreter = Interpreter.new
@interpreter.start Risc.machine.init @interpreter.start Risc.machine.risc_init
end end
# must be after boot, but before main compile, to define method # must be after boot, but before main compile, to define method