rubyx/lib/register/builtin/kernel.rb

72 lines
3.2 KiB
Ruby
Raw Normal View History

module Register
module Builtin
module Kernel
module ClassMethods
# this is the really really first place the machine starts (apart from the jump here)
# it isn't really a function, ie it is jumped to (not called), exits and may not return
# so it is responsible for initial setup
def __init__ context
2015-07-03 19:13:03 +02:00
function = Virtual::MethodSource.create_method(:Kernel,:__init__ , [])
function.source.return_type = Virtual::Integer
# no method enter or return (automatically added), remove
2015-07-03 19:13:03 +02:00
function.source.blocks.first.codes.pop # no Method enter
function.source.blocks.last.codes.pop # no Method return
#Set up the Space as self upon init
space = Parfait::Space.object_space
2015-07-03 19:13:03 +02:00
function.source.add_code LoadConstant.new( space , Register.self_reg)
message_ind = Register.resolve_index( :space , :first_message )
2015-06-30 08:37:25 +02:00
# Load the message to new message register (r3)
2015-07-03 19:13:03 +02:00
function.source.add_code Register.get_slot( :self , message_ind , :new_message)
# And store the space as the new self (so the call can move it back as self)
2015-07-03 19:13:03 +02:00
function.source.add_code Register.set_slot( :self , :new_message , :receiver)
# now we are set up to issue a call to the main
2015-07-03 19:13:03 +02:00
function.source.add_code Virtual::MethodCall.new(Virtual.machine.space.get_main)
emit_syscall( function , :exit )
return function
end
def exit context
2015-07-03 19:13:03 +02:00
function = Virtual::MethodSource.create_method(:Kernel,:exit , [])
function.source.return_type = Virtual::Integer
return function
ret = Virtual::RegisterMachine.instance.exit(function)
function.set_return ret
function
end
def __send context
2015-07-03 19:13:03 +02:00
function = Virtual::MethodSource.create_method(:Kernel ,:__send , [] )
function.source.return_type = Virtual::Integer
return function
end
2015-06-22 21:48:42 +02:00
def emit_syscall function , name
save_message( function )
2015-07-03 19:13:03 +02:00
function.source.add_code Syscall.new( name )
restore_message(function)
end
# save the current message, as the syscall destroys all context
#
# This relies on linux to save and restore all registers
#
def save_message(function)
2015-07-03 19:13:03 +02:00
function.source.add_code RegisterTransfer.new( Register.message_reg , :r8 )
end
def restore_message(function)
return_tmp = Register.tmp_reg
# get the sys return out of the way
2015-07-03 19:13:03 +02:00
function.source.add_code RegisterTransfer.new( Register.message_reg , return_tmp )
# load the stored message into the base RegisterMachine
2015-07-03 19:13:03 +02:00
function.source.add_code RegisterTransfer.new( :r8 , Register.message_reg )
# save the return value into the message
2015-07-03 19:13:03 +02:00
function.source.add_code Register.set_slot( return_tmp , :message , :return_value )
# and "unroll" self and frame
2015-07-03 19:13:03 +02:00
function.source.add_code Register.get_slot(:message , :receiver, :self )
function.source.add_code Register.get_slot(:message , :frame , :frame)
end
2015-06-22 21:48:42 +02:00
end
extend ClassMethods
2014-09-10 20:35:52 +02:00
end
end
end