From 60fe0dfd1c987290ba01770a1c98936197e5f6d9 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 1 Jul 2015 19:27:52 +0300 Subject: [PATCH] separate New frame and message and also much much shorter now, as all is static --- lib/register/passes/frame_implementation.rb | 51 ++++--------------- lib/register/passes/message_implementation.rb | 32 ++++++++++++ 2 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 lib/register/passes/message_implementation.rb diff --git a/lib/register/passes/frame_implementation.rb b/lib/register/passes/frame_implementation.rb index b0f5187a..44c59141 100644 --- a/lib/register/passes/frame_implementation.rb +++ b/lib/register/passes/frame_implementation.rb @@ -1,52 +1,19 @@ module Register - # This implements the creation of new frame and message object + # This implements the creation of a new frame object. + # Or to be more precise, it makes the frame available in a register, as the message (any message) + # carries a frame around which is reused. - # Frames and Message are very similar apart from the class name - # - All existing instances are stored in the space for both - # - Size is currently 2, ie 16 words - # (TODO a little flexibility here would not hurt, but the mountain is big) - # - Unused instances for a linked list with their first instance variable. - # This is HARD coded to avoid any lookup + # Just as a reminder: a message object is used to send and holds return address/message + # and arguments + self + # frames are used by a method and hold local and temporary variables - # Just as a reminder: a message object is created before a send and holds return address/message - # and arguments + self frames are created upon entering a method and hold local and temporary - # variables as a result one of each is created for every single method call. - # A LOT, so make it fast luke - # Note: this is off course the reason for stack based implementations that just increment - # a known pointer/register or something. But i think most programs are memory bound - # and a few extra instructions don't hurt. - # After all, we are buying a big prize:oo, otherwise known as sanity. - # TODO: This is too complicated, which means it should be ruby code and "inlined" - # then it would move to Virtual class FrameImplementation def run block block.codes.dup.each do |code| - if code.is_a?(Virtual::NewFrame) - kind = :next_frame - elsif code.is_a?(Virtual::NewMessage) - kind = :next_message - else - next - end - # a place to store a reference to the space, we grab the next_frame from the space - space_tmp = Register.tmp_reg - # move the spave to it's register (mov instruction gets the address of the object) - new_codes = [ LoadConstant.new( Parfait::Space.object_space , space_tmp )] - # find index in the space where to grab frame/message - space_index = Register.resolve_index( :space , kind ) - raise "index not found for #{kind}.#{kind.class}" unless space_index - # load the frame/message from space by index - new_codes << GetSlot.new( space_tmp , space_index , Register.resolve_to_register(:frame) ) - # a temporary place to store the new frame - frame_tmp = space_tmp.next_reg_use - # get the next_frame - from = Parfait::Space.object_space.send( kind ) - kind_index = from.get_layout().index_of( kind ) + 1 # should be resolve_index to hide the +1 - raise "index not found for #{kind}.#{kind.class}" unless kind_index - new_codes << GetSlot.new( Register.frame_reg , kind_index , frame_tmp) - # save next frame into space - new_codes << SetSlot.new( frame_tmp , space_tmp , space_index) + next unless code.is_a?(Virtual::NewFrame) + # load the frame from message by index, simple get_slot + new_codes = [ Register.get_slot( :message , :frame , Register.resolve_to_register(:frame))] block.replace(code , new_codes ) end end diff --git a/lib/register/passes/message_implementation.rb b/lib/register/passes/message_implementation.rb new file mode 100644 index 00000000..38a03e34 --- /dev/null +++ b/lib/register/passes/message_implementation.rb @@ -0,0 +1,32 @@ +module Register + # This implements the creation of a new message object + # + # It does so by loading the next message into the new_message register. + # + # Messages are created at compile time and form a linked list which actually never changes. + # We just grab the next item of the list. + + # Just as a reminder: a message object is used for a send and holds return address/message + # and arguments + self + # while frames are used by a method and hold local and temporary variables + + # This was at first a little surprising, but it actually similar in c. When a c function pops to + # stack, it doesn't create a new stack. Just increments some index. The storage/stack is reused, + # stays constant. (until such time it runs out, which we haven't covered yet) + # + # Even stranger at first was the notion that the caller does not have to be set either. + # That is contstant (a compile time property) too. It's a bit like when calling someone, + # + + class MessageImplementation + def run block + block.codes.dup.each do |code| + next unless code.is_a?(Virtual::NewMessage) + # load the new_message from message by index, simple get_slot + new_codes = [ Register.get_slot( :message , :next_message , Register.resolve_to_register(:new_message))] + block.replace(code , new_codes ) + end + end + end + Virtual.machine.add_pass "Register::MessageImplementation" +end