move assembly from assembler to machine

id now called position
This commit is contained in:
Torsten Ruger 2018-03-27 18:47:39 +03:00
parent 4cc1d8455e
commit 4253d7a6b9
4 changed files with 63 additions and 53 deletions

View File

@ -20,57 +20,8 @@ module Risc
@load_at = 0x8054 # this is linux/arm
end
def assemble
#need the initial jump at 0 and then functions
@machine.cpu_init.set_position( 0 )
at = @machine.cpu_init.byte_length
at = position_objects( at )
# and then everything code
position_code_from( at )
end
def position_objects( at )
at += 8 # thats the padding
# want to have the objects first in the executable
@objects.each do | id , objekt|
case objekt
when Risc::Label # will get assembled as method.cpu_instructions
Positioned.set_position(objekt,at)
when Parfait::BinaryCode
else
Positioned.set_position(objekt,at)
at += objekt.padded_length
end
end
at
end
def position_code_from( at )
@objects.each do |id , method|
next unless method.is_a? Parfait::TypedMethod
log.debug "CODE1 #{method.name}"
# create binary for assembly
binary = method.binary
Positioned.set_position(binary,at)
method.cpu_instructions.set_position( at + 12) # BinaryCode header
len = 4 * 14
at += binary.padded_length
nekst = binary.next
while(nekst)
Positioned.set_position(nekst , at)
at += binary.padded_length
nekst = nekst.next
len += 4 * 16
#puts "LENGTH #{len}"
end
log.debug "CODE2 #{method.name} at #{Positioned.position(binary)} len: #{len}"
end
at
end
# objects must be written in same order as positioned / assembled
def write_as_string
assemble
write_debug
write_create_binary
write_objects

View File

@ -54,7 +54,7 @@ module Risc
def byte_length
0
end
alias :padded_length :byte_length
end
def self.label( source , name , nekst = nil)

View File

@ -12,7 +12,6 @@ module Risc
#
class Machine
include Collector
include Logging
log_level :info
@ -20,13 +19,15 @@ module Risc
@booted = false
@constants = []
end
attr_reader :constants , :risc_init , :cpu_init , :booted
attr_reader :constants , :risc_init , :cpu_init
attr_reader :booted , :translated
# translate to arm, ie instantiate an arm translator and pass it to translate
#
# currently we have no machanism to translate to other cpu's (nor such translators)
# but the mechanism is ready
def translate_arm
@translated = true
translate(Arm::Translator.new)
end
@ -44,8 +45,57 @@ module Risc
end
end
# machine keeps a list of all objects. this is lazily created with a collector
def objects
@objects ||= Collector.collect_space
end
def position_all
translate_arm unless @translated
#need the initial jump at 0 and then functions
cpu_init.set_position( 0 )
at = cpu_init.byte_length
at = position_objects( at )
# and then everything code
position_code_from( at )
end
def position_objects( at )
at += 8 # thats the padding
# want to have the objects first in the executable
objects.each do | id , objekt|
case objekt
when Parfait::BinaryCode
when Risc::Label
else
Positioned.set_position(objekt,at)
at += objekt.padded_length
end
end
at
end
def position_code_from( at )
objects.each do |id , method|
next unless method.is_a? Parfait::TypedMethod
log.debug "CODE1 #{method.name}:#{at}"
method.cpu_instructions.set_position( at + 12) # BinaryCode header
before = at
nekst = method.binary
while(nekst)
Positioned.set_position(nekst , at)
at += nekst.padded_length
nekst = nekst.next
#puts "LENGTH #{len}"
end
log.debug "CODE2 #{method.name}:#{at} len: #{at - before}"
end
at
end
def boot
initialize
@objects = nil
boot_parfait!
@risc_init = Branch.new( "__initial_branch__" , Parfait.object_space.get_init.risc_instructions )
@booted = true

View File

@ -7,8 +7,17 @@ module Risc
@machine = Risc.machine.boot
end
def test_co
def test_objects
objects = @machine.objects
assert_equal Hash , objects.class
assert 400 < objects.length
end
def test_position
@machine.position_all
objects = @machine.objects
assert_equal Hash , objects.class
assert 400 < objects.length
end
end
end