From 8f9bbe2f1dc0bc71eaf6dba5f692f7eda13dceeb Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 26 Aug 2014 16:36:12 +0300 Subject: [PATCH] fold old link and length into one function --- lib/register/assembler.rb | 60 +++++++++++++++++++++----------- lib/register/register_machine.rb | 2 +- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/lib/register/assembler.rb b/lib/register/assembler.rb index 25e9fef6..683ebeac 100644 --- a/lib/register/assembler.rb +++ b/lib/register/assembler.rb @@ -2,8 +2,9 @@ module Register class LinkSlot def initialize at @position = position + @length = 0 end - attr_accessor :position + attr_accessor :position , :length end class Assembler @@ -12,49 +13,68 @@ module Register @objects = {} end - def link at = 0 - link_object @space , at + def link + link_object(@space , 0) end - def link_object object , at + def link_object(object , at) slot = @objects[object.object_id] unless slot slot = LinkSlot.new at @objects[object.object_id] = slot end -# return if object.is_a? Instruction - clazz = object.class.name.split("::").last - send("link_#{clazz}".to_sym , object , at) + if object.is_a? Instruction + clazz = object.class.name.split("::").last + len = send("link_#{clazz}".to_sym , object , at) + else + len = 4 + end + slot.length = len + len end - def link_BootSpace space , at + def link_BootSpace(space , at) + len = 0 space.classes.values.each do |cl| - link_object cl , at + len += link_object(cl , at + len) end + len end - def link_BootClass clazz , at - link_object clazz.name , at - link_object clazz.super_class_name , at + def link_BootClass(clazz , at) + len = link_object(clazz.name , at) + len += link_object(clazz.super_class_name , at + len) clazz.instance_methods.each do |meth| - link_object meth , at + len += link_object(meth , at + len) end + len end - def link_MethodDefinition method , at - link_object method.name ,at + + def link_MethodDefinition(method , at) + len = link_object method.name ,at method.blocks.each do |block| - link_object block , at + link_object( block , at + len) end + len end - def link_Block block , at + + def link_Block(block , at) + len = 0 block.codes.each do |code| - link_object code , at + len += link_object(code , at + len) end + len end - def link_String str , at + + def link_String( str , at) + return (str.length / 4) + 1 + 2 end - def link_Symbol sym , at + + def link_Symbol(sym , at) + return link_String(sym.to_s , at) end + end + Sof::Volotile.add(Register::Assembler , [:objects]) end diff --git a/lib/register/register_machine.rb b/lib/register/register_machine.rb index ddb23206..119b216c 100644 --- a/lib/register/register_machine.rb +++ b/lib/register/register_machine.rb @@ -152,4 +152,4 @@ require_relative "call_implementation" require_relative "enter_implementation" require_relative "return_implementation" require "arm/arm_machine" -require_relative "assembler" \ No newline at end of file +require_relative "assembler"